input.c revision 17987
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 353044Sdg * 3617987Speter * $Id: input.c,v 1.4 1995/11/03 18:50:14 peter Exp $ 371556Srgrimes */ 381556Srgrimes 391556Srgrimes#ifndef lint 4017987Speterstatic char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 4317987Speter#include <stdio.h> /* defines BUFSIZ */ 4417987Speter#include <fcntl.h> 4517987Speter#include <errno.h> 4617987Speter#include <unistd.h> 4717987Speter#include <stdlib.h> 4817987Speter#include <string.h> 4917987Speter 501556Srgrimes/* 511556Srgrimes * This file implements the input routines used by the parser. 521556Srgrimes */ 531556Srgrimes 541556Srgrimes#include "shell.h" 5517987Speter#include "redir.h" 561556Srgrimes#include "syntax.h" 571556Srgrimes#include "input.h" 581556Srgrimes#include "output.h" 591556Srgrimes#include "options.h" 601556Srgrimes#include "memalloc.h" 611556Srgrimes#include "error.h" 621556Srgrimes#include "alias.h" 631556Srgrimes#include "parser.h" 641556Srgrimes#include "myhistedit.h" 651556Srgrimes 661556Srgrimes#define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 671556Srgrimes 681556SrgrimesMKINIT 691556Srgrimesstruct strpush { 701556Srgrimes struct strpush *prev; /* preceding string on stack */ 711556Srgrimes char *prevstring; 721556Srgrimes int prevnleft; 7312043Speter int prevlleft; 741556Srgrimes struct alias *ap; /* if push was associated with an alias */ 751556Srgrimes}; 761556Srgrimes 771556Srgrimes/* 781556Srgrimes * The parsefile structure pointed to by the global variable parsefile 791556Srgrimes * contains information about the current file being read. 801556Srgrimes */ 811556Srgrimes 821556SrgrimesMKINIT 831556Srgrimesstruct parsefile { 841556Srgrimes struct parsefile *prev; /* preceding file on stack */ 851556Srgrimes int linno; /* current line */ 861556Srgrimes int fd; /* file descriptor (or -1 if string) */ 8712043Speter int nleft; /* number of chars left in line */ 8812043Speter int lleft; /* number of lines left in buffer */ 891556Srgrimes char *nextc; /* next char in buffer */ 901556Srgrimes char *buf; /* input buffer */ 911556Srgrimes struct strpush *strpush; /* for pushing strings at this level */ 921556Srgrimes struct strpush basestrpush; /* so pushing one is fast */ 931556Srgrimes}; 941556Srgrimes 951556Srgrimes 961556Srgrimesint plinno = 1; /* input line number */ 971556SrgrimesMKINIT int parsenleft; /* copy of parsefile->nleft */ 9812043SpeterMKINIT int parselleft; /* copy of parsefile->lleft */ 991556Srgrimeschar *parsenextc; /* copy of parsefile->nextc */ 1001556SrgrimesMKINIT struct parsefile basepf; /* top level input file */ 1011556Srgrimeschar basebuf[BUFSIZ]; /* buffer for top level input file */ 1021556Srgrimesstruct parsefile *parsefile = &basepf; /* current input file */ 1031556Srgrimesint init_editline = 0; /* editline library initialized? */ 1041556Srgrimesint whichprompt; /* 1 == PS1, 2 == PS2 */ 1051556Srgrimes 1061556SrgrimesEditLine *el; /* cookie for editline package */ 1071556Srgrimes 10817987SpeterSTATIC void pushfile __P((void)); 1091556Srgrimes 1101556Srgrimes#ifdef mkinit 1111556SrgrimesINCLUDE "input.h" 1121556SrgrimesINCLUDE "error.h" 1131556Srgrimes 1141556SrgrimesINIT { 1151556Srgrimes extern char basebuf[]; 1161556Srgrimes 1171556Srgrimes basepf.nextc = basepf.buf = basebuf; 1181556Srgrimes} 1191556Srgrimes 1201556SrgrimesRESET { 1211556Srgrimes if (exception != EXSHELLPROC) 12212043Speter parselleft = parsenleft = 0; /* clear input buffer */ 1231556Srgrimes popallfiles(); 1241556Srgrimes} 1251556Srgrimes 1261556SrgrimesSHELLPROC { 1271556Srgrimes popallfiles(); 1281556Srgrimes} 1291556Srgrimes#endif 1301556Srgrimes 1311556Srgrimes 1321556Srgrimes/* 1331556Srgrimes * Read a line from the script. 1341556Srgrimes */ 1351556Srgrimes 1361556Srgrimeschar * 1371556Srgrimespfgets(line, len) 1381556Srgrimes char *line; 13917987Speter int len; 14017987Speter{ 1411556Srgrimes register char *p = line; 1421556Srgrimes int nleft = len; 1431556Srgrimes int c; 1441556Srgrimes 1451556Srgrimes while (--nleft > 0) { 1461556Srgrimes c = pgetc_macro(); 1471556Srgrimes if (c == PEOF) { 1481556Srgrimes if (p == line) 1491556Srgrimes return NULL; 1501556Srgrimes break; 1511556Srgrimes } 1521556Srgrimes *p++ = c; 1531556Srgrimes if (c == '\n') 1541556Srgrimes break; 1551556Srgrimes } 1561556Srgrimes *p = '\0'; 1571556Srgrimes return line; 1581556Srgrimes} 1591556Srgrimes 1601556Srgrimes 1611556Srgrimes 1621556Srgrimes/* 1631556Srgrimes * Read a character from the script, returning PEOF on end of file. 1641556Srgrimes * Nul characters in the input are silently discarded. 1651556Srgrimes */ 1661556Srgrimes 1671556Srgrimesint 1681556Srgrimespgetc() { 1691556Srgrimes return pgetc_macro(); 1701556Srgrimes} 1711556Srgrimes 17212043Speterstatic int 17312043Speterpread() 17412043Speter{ 17512043Speter int nr; 1761556Srgrimes 17712043Speter parsenextc = parsefile->buf; 17812043Speterretry: 17912043Speter if (parsefile->fd == 0 && el) { 18012043Speter const char *rl_cp; 18112043Speter int len; 18212043Speter 18312043Speter rl_cp = el_gets(el, &nr); 18412043Speter if (rl_cp == NULL) 18512043Speter nr = 0; 18612043Speter else { 18712043Speter /* XXX - BUFSIZE should redesign so not necessary */ 18812043Speter strcpy(parsenextc, rl_cp); 18912043Speter } 19012043Speter 19112043Speter } else { 19212043Speter nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 19312043Speter } 19412043Speter 19512043Speter if (nr <= 0) { 19612043Speter if (nr < 0) { 19712043Speter if (errno == EINTR) 19812043Speter goto retry; 19912043Speter if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 20012043Speter int flags = fcntl(0, F_GETFL, 0); 20112043Speter if (flags >= 0 && flags & O_NONBLOCK) { 20212043Speter flags &=~ O_NONBLOCK; 20312043Speter if (fcntl(0, F_SETFL, flags) >= 0) { 20412043Speter out2str("sh: turning off NDELAY mode\n"); 20512043Speter goto retry; 20612043Speter } 20712043Speter } 20812043Speter } 20912043Speter } 21012043Speter nr = -1; 21112043Speter } 21212043Speter return nr; 21312043Speter} 21412043Speter 2151556Srgrimes/* 2161556Srgrimes * Refill the input buffer and return the next input character: 2171556Srgrimes * 2181556Srgrimes * 1) If a string was pushed back on the input, pop it; 2191556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2201556Srgrimes * from a string so we can't refill the buffer, return EOF. 22112043Speter * 3) If there is more in the buffer, use it; else call read to fill it. 22212043Speter * 4) Process input up to next newline, deleting nul characters. 2231556Srgrimes */ 2241556Srgrimes 2251556Srgrimesint 2261556Srgrimespreadbuffer() { 22712043Speter char *p, *q; 22812043Speter int more; 22912043Speter int something; 2301556Srgrimes extern EditLine *el; 23112043Speter char savec; 2321556Srgrimes 2331556Srgrimes if (parsefile->strpush) { 2341556Srgrimes popstring(); 2351556Srgrimes if (--parsenleft >= 0) 2361556Srgrimes return (*parsenextc++); 2371556Srgrimes } 2381556Srgrimes if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2391556Srgrimes return PEOF; 2401556Srgrimes flushout(&output); 2411556Srgrimes flushout(&errout); 2421556Srgrimes 24312043Speteragain: 24412043Speter if (parselleft <= 0) { 24512043Speter if ((parselleft = pread()) == -1) { 24612043Speter parselleft = parsenleft = EOF_NLEFT; 24712043Speter return PEOF; 2481556Srgrimes } 2491556Srgrimes } 2501556Srgrimes 25112043Speter q = p = parsenextc; 25212043Speter 2531556Srgrimes /* delete nul characters */ 2541556Srgrimes something = 0; 25512043Speter for (more = 1; more;) { 25612043Speter switch (*p) { 25712043Speter case '\0': 25812043Speter p++; /* Skip nul */ 25912043Speter goto check; 26012043Speter 26112043Speter case '\t': 26212043Speter case ' ': 2631556Srgrimes break; 26412043Speter 26512043Speter case '\n': 26612043Speter parsenleft = q - parsenextc; 26712043Speter more = 0; /* Stop processing here */ 26812043Speter break; 26912043Speter 27012043Speter default: 2711556Srgrimes something = 1; 27212043Speter break; 2731556Srgrimes } 27412043Speter 27512043Speter *q++ = *p++; 27612043Spetercheck: 27712043Speter if (--parselleft <= 0) { 27812043Speter parsenleft = q - parsenextc - 1; 27912043Speter if (parsenleft < 0) 28012043Speter goto again; 28112043Speter *q = '\0'; 28212043Speter more = 0; 28312043Speter } 2841556Srgrimes } 28512043Speter 28612043Speter savec = *q; 2871556Srgrimes *q = '\0'; 2881556Srgrimes 28912043Speter 2901556Srgrimes if (parsefile->fd == 0 && hist && something) { 2911556Srgrimes INTOFF; 29212043Speter history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc); 2931556Srgrimes INTON; 2941556Srgrimes } 29512043Speter 29612043Speter 2971556Srgrimes if (vflag) { 29812043Speter out2str(parsenextc); 2991556Srgrimes flushout(out2); 3001556Srgrimes } 30112043Speter 30212043Speter *q = savec; 30312043Speter 3041556Srgrimes return *parsenextc++; 3051556Srgrimes} 3061556Srgrimes 3071556Srgrimes/* 3081556Srgrimes * Undo the last call to pgetc. Only one character may be pushed back. 3091556Srgrimes * PEOF may be pushed back. 3101556Srgrimes */ 3111556Srgrimes 3121556Srgrimesvoid 3131556Srgrimespungetc() { 3141556Srgrimes parsenleft++; 3151556Srgrimes parsenextc--; 3161556Srgrimes} 3171556Srgrimes 3181556Srgrimes/* 3191556Srgrimes * Push a string back onto the input at this current parsefile level. 3201556Srgrimes * We handle aliases this way. 3211556Srgrimes */ 3221556Srgrimesvoid 3231556Srgrimespushstring(s, len, ap) 3241556Srgrimes char *s; 3251556Srgrimes int len; 3261556Srgrimes void *ap; 3271556Srgrimes { 3281556Srgrimes struct strpush *sp; 3291556Srgrimes 3301556Srgrimes INTOFF; 3311556Srgrimes/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 3321556Srgrimes if (parsefile->strpush) { 3331556Srgrimes sp = ckmalloc(sizeof (struct strpush)); 3341556Srgrimes sp->prev = parsefile->strpush; 3351556Srgrimes parsefile->strpush = sp; 3361556Srgrimes } else 3371556Srgrimes sp = parsefile->strpush = &(parsefile->basestrpush); 3381556Srgrimes sp->prevstring = parsenextc; 3391556Srgrimes sp->prevnleft = parsenleft; 34012043Speter sp->prevlleft = parselleft; 3411556Srgrimes sp->ap = (struct alias *)ap; 3421556Srgrimes if (ap) 3431556Srgrimes ((struct alias *)ap)->flag |= ALIASINUSE; 3441556Srgrimes parsenextc = s; 3451556Srgrimes parsenleft = len; 3461556Srgrimes INTON; 3471556Srgrimes} 3481556Srgrimes 34917987Spetervoid 3501556Srgrimespopstring() 3511556Srgrimes{ 3521556Srgrimes struct strpush *sp = parsefile->strpush; 3531556Srgrimes 3541556Srgrimes INTOFF; 3551556Srgrimes parsenextc = sp->prevstring; 3561556Srgrimes parsenleft = sp->prevnleft; 35712043Speter parselleft = sp->prevlleft; 3581556Srgrimes/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3591556Srgrimes if (sp->ap) 3601556Srgrimes sp->ap->flag &= ~ALIASINUSE; 3611556Srgrimes parsefile->strpush = sp->prev; 3621556Srgrimes if (sp != &(parsefile->basestrpush)) 3631556Srgrimes ckfree(sp); 3641556Srgrimes INTON; 3651556Srgrimes} 3661556Srgrimes 3671556Srgrimes/* 3681556Srgrimes * Set the input to take input from a file. If push is set, push the 3691556Srgrimes * old input onto the stack first. 3701556Srgrimes */ 3711556Srgrimes 3721556Srgrimesvoid 3731556Srgrimessetinputfile(fname, push) 3741556Srgrimes char *fname; 37517987Speter int push; 37617987Speter{ 3771556Srgrimes int fd; 3781556Srgrimes int fd2; 3791556Srgrimes 3801556Srgrimes INTOFF; 3811556Srgrimes if ((fd = open(fname, O_RDONLY)) < 0) 3821556Srgrimes error("Can't open %s", fname); 3831556Srgrimes if (fd < 10) { 3841556Srgrimes fd2 = copyfd(fd, 10); 3851556Srgrimes close(fd); 3861556Srgrimes if (fd2 < 0) 3871556Srgrimes error("Out of file descriptors"); 3881556Srgrimes fd = fd2; 3891556Srgrimes } 3901556Srgrimes setinputfd(fd, push); 3911556Srgrimes INTON; 3921556Srgrimes} 3931556Srgrimes 3941556Srgrimes 3951556Srgrimes/* 3961556Srgrimes * Like setinputfile, but takes an open file descriptor. Call this with 3971556Srgrimes * interrupts off. 3981556Srgrimes */ 3991556Srgrimes 4001556Srgrimesvoid 40117987Spetersetinputfd(fd, push) 40217987Speter int fd, push; 40317987Speter{ 4041556Srgrimes if (push) { 4051556Srgrimes pushfile(); 4061556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 4071556Srgrimes } 4081556Srgrimes if (parsefile->fd > 0) 4091556Srgrimes close(parsefile->fd); 4101556Srgrimes parsefile->fd = fd; 4111556Srgrimes if (parsefile->buf == NULL) 4121556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 41312043Speter parselleft = parsenleft = 0; 4141556Srgrimes plinno = 1; 4151556Srgrimes} 4161556Srgrimes 4171556Srgrimes 4181556Srgrimes/* 4191556Srgrimes * Like setinputfile, but takes input from a string. 4201556Srgrimes */ 4211556Srgrimes 4221556Srgrimesvoid 4231556Srgrimessetinputstring(string, push) 4241556Srgrimes char *string; 42517987Speter int push; 4261556Srgrimes { 4271556Srgrimes INTOFF; 4281556Srgrimes if (push) 4291556Srgrimes pushfile(); 4301556Srgrimes parsenextc = string; 43112043Speter parselleft = parsenleft = strlen(string); 4321556Srgrimes parsefile->buf = NULL; 4331556Srgrimes plinno = 1; 4341556Srgrimes INTON; 4351556Srgrimes} 4361556Srgrimes 4371556Srgrimes 4381556Srgrimes 4391556Srgrimes/* 4401556Srgrimes * To handle the "." command, a stack of input files is used. Pushfile 4411556Srgrimes * adds a new entry to the stack and popfile restores the previous level. 4421556Srgrimes */ 4431556Srgrimes 4441556SrgrimesSTATIC void 4451556Srgrimespushfile() { 4461556Srgrimes struct parsefile *pf; 4471556Srgrimes 4481556Srgrimes parsefile->nleft = parsenleft; 44912043Speter parsefile->lleft = parselleft; 4501556Srgrimes parsefile->nextc = parsenextc; 4511556Srgrimes parsefile->linno = plinno; 4521556Srgrimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 4531556Srgrimes pf->prev = parsefile; 4541556Srgrimes pf->fd = -1; 4551556Srgrimes pf->strpush = NULL; 4561556Srgrimes pf->basestrpush.prev = NULL; 4571556Srgrimes parsefile = pf; 4581556Srgrimes} 4591556Srgrimes 4601556Srgrimes 4611556Srgrimesvoid 4621556Srgrimespopfile() { 4631556Srgrimes struct parsefile *pf = parsefile; 4641556Srgrimes 4651556Srgrimes INTOFF; 4661556Srgrimes if (pf->fd >= 0) 4671556Srgrimes close(pf->fd); 4681556Srgrimes if (pf->buf) 4691556Srgrimes ckfree(pf->buf); 4701556Srgrimes while (pf->strpush) 4711556Srgrimes popstring(); 4721556Srgrimes parsefile = pf->prev; 4731556Srgrimes ckfree(pf); 4741556Srgrimes parsenleft = parsefile->nleft; 47512043Speter parselleft = parsefile->lleft; 4761556Srgrimes parsenextc = parsefile->nextc; 4771556Srgrimes plinno = parsefile->linno; 4781556Srgrimes INTON; 4791556Srgrimes} 4801556Srgrimes 4811556Srgrimes 4821556Srgrimes/* 4831556Srgrimes * Return to top level. 4841556Srgrimes */ 4851556Srgrimes 4861556Srgrimesvoid 4871556Srgrimespopallfiles() { 4881556Srgrimes while (parsefile != &basepf) 4891556Srgrimes popfile(); 4901556Srgrimes} 4911556Srgrimes 4921556Srgrimes 4931556Srgrimes 4941556Srgrimes/* 4951556Srgrimes * Close the file(s) that the shell is reading commands from. Called 4961556Srgrimes * after a fork is done. 4971556Srgrimes */ 4981556Srgrimes 4991556Srgrimesvoid 5001556Srgrimesclosescript() { 5011556Srgrimes popallfiles(); 5021556Srgrimes if (parsefile->fd > 0) { 5031556Srgrimes close(parsefile->fd); 5041556Srgrimes parsefile->fd = 0; 5051556Srgrimes } 5061556Srgrimes} 507