input.c revision 18018
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 * 3618018Speter * $Id: input.c,v 1.5 1996/09/01 10:20:18 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 18212043Speter rl_cp = el_gets(el, &nr); 18312043Speter if (rl_cp == NULL) 18412043Speter nr = 0; 18512043Speter else { 18612043Speter /* XXX - BUFSIZE should redesign so not necessary */ 18718018Speter (void)strcpy(parsenextc, rl_cp); 18812043Speter } 18912043Speter 19012043Speter } else { 19112043Speter nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 19212043Speter } 19312043Speter 19412043Speter if (nr <= 0) { 19512043Speter if (nr < 0) { 19612043Speter if (errno == EINTR) 19712043Speter goto retry; 19812043Speter if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 19912043Speter int flags = fcntl(0, F_GETFL, 0); 20012043Speter if (flags >= 0 && flags & O_NONBLOCK) { 20112043Speter flags &=~ O_NONBLOCK; 20212043Speter if (fcntl(0, F_SETFL, flags) >= 0) { 20312043Speter out2str("sh: turning off NDELAY mode\n"); 20412043Speter goto retry; 20512043Speter } 20612043Speter } 20712043Speter } 20812043Speter } 20912043Speter nr = -1; 21012043Speter } 21112043Speter return nr; 21212043Speter} 21312043Speter 2141556Srgrimes/* 2151556Srgrimes * Refill the input buffer and return the next input character: 2161556Srgrimes * 2171556Srgrimes * 1) If a string was pushed back on the input, pop it; 2181556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2191556Srgrimes * from a string so we can't refill the buffer, return EOF. 22012043Speter * 3) If there is more in the buffer, use it; else call read to fill it. 22112043Speter * 4) Process input up to next newline, deleting nul characters. 2221556Srgrimes */ 2231556Srgrimes 2241556Srgrimesint 2251556Srgrimespreadbuffer() { 22612043Speter char *p, *q; 22712043Speter int more; 22812043Speter int something; 2291556Srgrimes extern EditLine *el; 23012043Speter char savec; 2311556Srgrimes 2321556Srgrimes if (parsefile->strpush) { 2331556Srgrimes popstring(); 2341556Srgrimes if (--parsenleft >= 0) 2351556Srgrimes return (*parsenextc++); 2361556Srgrimes } 2371556Srgrimes if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2381556Srgrimes return PEOF; 2391556Srgrimes flushout(&output); 2401556Srgrimes flushout(&errout); 2411556Srgrimes 24212043Speteragain: 24312043Speter if (parselleft <= 0) { 24412043Speter if ((parselleft = pread()) == -1) { 24512043Speter parselleft = parsenleft = EOF_NLEFT; 24612043Speter return PEOF; 2471556Srgrimes } 2481556Srgrimes } 2491556Srgrimes 25012043Speter q = p = parsenextc; 25112043Speter 2521556Srgrimes /* delete nul characters */ 2531556Srgrimes something = 0; 25412043Speter for (more = 1; more;) { 25512043Speter switch (*p) { 25612043Speter case '\0': 25712043Speter p++; /* Skip nul */ 25812043Speter goto check; 25912043Speter 26012043Speter case '\t': 26112043Speter case ' ': 2621556Srgrimes break; 26312043Speter 26412043Speter case '\n': 26512043Speter parsenleft = q - parsenextc; 26612043Speter more = 0; /* Stop processing here */ 26712043Speter break; 26812043Speter 26912043Speter default: 2701556Srgrimes something = 1; 27112043Speter break; 2721556Srgrimes } 27312043Speter 27412043Speter *q++ = *p++; 27512043Spetercheck: 27612043Speter if (--parselleft <= 0) { 27712043Speter parsenleft = q - parsenextc - 1; 27812043Speter if (parsenleft < 0) 27912043Speter goto again; 28012043Speter *q = '\0'; 28112043Speter more = 0; 28212043Speter } 2831556Srgrimes } 28412043Speter 28512043Speter savec = *q; 2861556Srgrimes *q = '\0'; 2871556Srgrimes 28818018Speter#ifndef NO_HISTORY 2891556Srgrimes if (parsefile->fd == 0 && hist && something) { 2901556Srgrimes INTOFF; 29112043Speter history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc); 2921556Srgrimes INTON; 2931556Srgrimes } 29418018Speter#endif 29512043Speter 2961556Srgrimes if (vflag) { 29712043Speter out2str(parsenextc); 2981556Srgrimes flushout(out2); 2991556Srgrimes } 30012043Speter 30112043Speter *q = savec; 30212043Speter 3031556Srgrimes return *parsenextc++; 3041556Srgrimes} 3051556Srgrimes 3061556Srgrimes/* 3071556Srgrimes * Undo the last call to pgetc. Only one character may be pushed back. 3081556Srgrimes * PEOF may be pushed back. 3091556Srgrimes */ 3101556Srgrimes 3111556Srgrimesvoid 3121556Srgrimespungetc() { 3131556Srgrimes parsenleft++; 3141556Srgrimes parsenextc--; 3151556Srgrimes} 3161556Srgrimes 3171556Srgrimes/* 3181556Srgrimes * Push a string back onto the input at this current parsefile level. 3191556Srgrimes * We handle aliases this way. 3201556Srgrimes */ 3211556Srgrimesvoid 3221556Srgrimespushstring(s, len, ap) 3231556Srgrimes char *s; 3241556Srgrimes int len; 3251556Srgrimes void *ap; 3261556Srgrimes { 3271556Srgrimes struct strpush *sp; 3281556Srgrimes 3291556Srgrimes INTOFF; 3301556Srgrimes/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 3311556Srgrimes if (parsefile->strpush) { 3321556Srgrimes sp = ckmalloc(sizeof (struct strpush)); 3331556Srgrimes sp->prev = parsefile->strpush; 3341556Srgrimes parsefile->strpush = sp; 3351556Srgrimes } else 3361556Srgrimes sp = parsefile->strpush = &(parsefile->basestrpush); 3371556Srgrimes sp->prevstring = parsenextc; 3381556Srgrimes sp->prevnleft = parsenleft; 33912043Speter sp->prevlleft = parselleft; 3401556Srgrimes sp->ap = (struct alias *)ap; 3411556Srgrimes if (ap) 3421556Srgrimes ((struct alias *)ap)->flag |= ALIASINUSE; 3431556Srgrimes parsenextc = s; 3441556Srgrimes parsenleft = len; 3451556Srgrimes INTON; 3461556Srgrimes} 3471556Srgrimes 34817987Spetervoid 3491556Srgrimespopstring() 3501556Srgrimes{ 3511556Srgrimes struct strpush *sp = parsefile->strpush; 3521556Srgrimes 3531556Srgrimes INTOFF; 3541556Srgrimes parsenextc = sp->prevstring; 3551556Srgrimes parsenleft = sp->prevnleft; 35612043Speter parselleft = sp->prevlleft; 3571556Srgrimes/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3581556Srgrimes if (sp->ap) 3591556Srgrimes sp->ap->flag &= ~ALIASINUSE; 3601556Srgrimes parsefile->strpush = sp->prev; 3611556Srgrimes if (sp != &(parsefile->basestrpush)) 3621556Srgrimes ckfree(sp); 3631556Srgrimes INTON; 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes/* 3671556Srgrimes * Set the input to take input from a file. If push is set, push the 3681556Srgrimes * old input onto the stack first. 3691556Srgrimes */ 3701556Srgrimes 3711556Srgrimesvoid 3721556Srgrimessetinputfile(fname, push) 3731556Srgrimes char *fname; 37417987Speter int push; 37517987Speter{ 3761556Srgrimes int fd; 3771556Srgrimes int fd2; 3781556Srgrimes 3791556Srgrimes INTOFF; 3801556Srgrimes if ((fd = open(fname, O_RDONLY)) < 0) 3811556Srgrimes error("Can't open %s", fname); 3821556Srgrimes if (fd < 10) { 3831556Srgrimes fd2 = copyfd(fd, 10); 3841556Srgrimes close(fd); 3851556Srgrimes if (fd2 < 0) 3861556Srgrimes error("Out of file descriptors"); 3871556Srgrimes fd = fd2; 3881556Srgrimes } 3891556Srgrimes setinputfd(fd, push); 3901556Srgrimes INTON; 3911556Srgrimes} 3921556Srgrimes 3931556Srgrimes 3941556Srgrimes/* 3951556Srgrimes * Like setinputfile, but takes an open file descriptor. Call this with 3961556Srgrimes * interrupts off. 3971556Srgrimes */ 3981556Srgrimes 3991556Srgrimesvoid 40017987Spetersetinputfd(fd, push) 40117987Speter int fd, push; 40217987Speter{ 4031556Srgrimes if (push) { 4041556Srgrimes pushfile(); 4051556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 4061556Srgrimes } 4071556Srgrimes if (parsefile->fd > 0) 4081556Srgrimes close(parsefile->fd); 4091556Srgrimes parsefile->fd = fd; 4101556Srgrimes if (parsefile->buf == NULL) 4111556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 41212043Speter parselleft = parsenleft = 0; 4131556Srgrimes plinno = 1; 4141556Srgrimes} 4151556Srgrimes 4161556Srgrimes 4171556Srgrimes/* 4181556Srgrimes * Like setinputfile, but takes input from a string. 4191556Srgrimes */ 4201556Srgrimes 4211556Srgrimesvoid 4221556Srgrimessetinputstring(string, push) 4231556Srgrimes char *string; 42417987Speter int push; 4251556Srgrimes { 4261556Srgrimes INTOFF; 4271556Srgrimes if (push) 4281556Srgrimes pushfile(); 4291556Srgrimes parsenextc = string; 43012043Speter parselleft = parsenleft = strlen(string); 4311556Srgrimes parsefile->buf = NULL; 4321556Srgrimes plinno = 1; 4331556Srgrimes INTON; 4341556Srgrimes} 4351556Srgrimes 4361556Srgrimes 4371556Srgrimes 4381556Srgrimes/* 4391556Srgrimes * To handle the "." command, a stack of input files is used. Pushfile 4401556Srgrimes * adds a new entry to the stack and popfile restores the previous level. 4411556Srgrimes */ 4421556Srgrimes 4431556SrgrimesSTATIC void 4441556Srgrimespushfile() { 4451556Srgrimes struct parsefile *pf; 4461556Srgrimes 4471556Srgrimes parsefile->nleft = parsenleft; 44812043Speter parsefile->lleft = parselleft; 4491556Srgrimes parsefile->nextc = parsenextc; 4501556Srgrimes parsefile->linno = plinno; 4511556Srgrimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 4521556Srgrimes pf->prev = parsefile; 4531556Srgrimes pf->fd = -1; 4541556Srgrimes pf->strpush = NULL; 4551556Srgrimes pf->basestrpush.prev = NULL; 4561556Srgrimes parsefile = pf; 4571556Srgrimes} 4581556Srgrimes 4591556Srgrimes 4601556Srgrimesvoid 4611556Srgrimespopfile() { 4621556Srgrimes struct parsefile *pf = parsefile; 4631556Srgrimes 4641556Srgrimes INTOFF; 4651556Srgrimes if (pf->fd >= 0) 4661556Srgrimes close(pf->fd); 4671556Srgrimes if (pf->buf) 4681556Srgrimes ckfree(pf->buf); 4691556Srgrimes while (pf->strpush) 4701556Srgrimes popstring(); 4711556Srgrimes parsefile = pf->prev; 4721556Srgrimes ckfree(pf); 4731556Srgrimes parsenleft = parsefile->nleft; 47412043Speter parselleft = parsefile->lleft; 4751556Srgrimes parsenextc = parsefile->nextc; 4761556Srgrimes plinno = parsefile->linno; 4771556Srgrimes INTON; 4781556Srgrimes} 4791556Srgrimes 4801556Srgrimes 4811556Srgrimes/* 4821556Srgrimes * Return to top level. 4831556Srgrimes */ 4841556Srgrimes 4851556Srgrimesvoid 4861556Srgrimespopallfiles() { 4871556Srgrimes while (parsefile != &basepf) 4881556Srgrimes popfile(); 4891556Srgrimes} 4901556Srgrimes 4911556Srgrimes 4921556Srgrimes 4931556Srgrimes/* 4941556Srgrimes * Close the file(s) that the shell is reading commands from. Called 4951556Srgrimes * after a fork is done. 4961556Srgrimes */ 4971556Srgrimes 4981556Srgrimesvoid 4991556Srgrimesclosescript() { 5001556Srgrimes popallfiles(); 5011556Srgrimes if (parsefile->fd > 0) { 5021556Srgrimes close(parsefile->fd); 5031556Srgrimes parsefile->fd = 0; 5041556Srgrimes } 5051556Srgrimes} 506