input.c revision 99110
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. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 4036150Scharnier#endif 411556Srgrimes#endif /* not lint */ 4299110Sobrien#include <sys/cdefs.h> 4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/input.c 99110 2002-06-30 05:15:05Z obrien $"); 441556Srgrimes 4517987Speter#include <stdio.h> /* defines BUFSIZ */ 4617987Speter#include <fcntl.h> 4717987Speter#include <errno.h> 4817987Speter#include <unistd.h> 4917987Speter#include <stdlib.h> 5017987Speter#include <string.h> 5117987Speter 521556Srgrimes/* 531556Srgrimes * This file implements the input routines used by the parser. 541556Srgrimes */ 551556Srgrimes 561556Srgrimes#include "shell.h" 5717987Speter#include "redir.h" 581556Srgrimes#include "syntax.h" 591556Srgrimes#include "input.h" 601556Srgrimes#include "output.h" 611556Srgrimes#include "options.h" 621556Srgrimes#include "memalloc.h" 631556Srgrimes#include "error.h" 641556Srgrimes#include "alias.h" 651556Srgrimes#include "parser.h" 661556Srgrimes#include "myhistedit.h" 671556Srgrimes 681556Srgrimes#define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 691556Srgrimes 701556SrgrimesMKINIT 711556Srgrimesstruct strpush { 721556Srgrimes struct strpush *prev; /* preceding string on stack */ 731556Srgrimes char *prevstring; 741556Srgrimes int prevnleft; 7512043Speter int prevlleft; 761556Srgrimes struct alias *ap; /* if push was associated with an alias */ 771556Srgrimes}; 781556Srgrimes 791556Srgrimes/* 801556Srgrimes * The parsefile structure pointed to by the global variable parsefile 811556Srgrimes * contains information about the current file being read. 821556Srgrimes */ 831556Srgrimes 841556SrgrimesMKINIT 851556Srgrimesstruct parsefile { 861556Srgrimes struct parsefile *prev; /* preceding file on stack */ 871556Srgrimes int linno; /* current line */ 881556Srgrimes int fd; /* file descriptor (or -1 if string) */ 8920425Ssteve int nleft; /* number of chars left in this line */ 9020425Ssteve int lleft; /* number of lines left in this buffer */ 911556Srgrimes char *nextc; /* next char in buffer */ 921556Srgrimes char *buf; /* input buffer */ 931556Srgrimes struct strpush *strpush; /* for pushing strings at this level */ 941556Srgrimes struct strpush basestrpush; /* so pushing one is fast */ 951556Srgrimes}; 961556Srgrimes 971556Srgrimes 981556Srgrimesint plinno = 1; /* input line number */ 991556SrgrimesMKINIT int parsenleft; /* copy of parsefile->nleft */ 10012043SpeterMKINIT int parselleft; /* copy of parsefile->lleft */ 1011556Srgrimeschar *parsenextc; /* copy of parsefile->nextc */ 1021556SrgrimesMKINIT struct parsefile basepf; /* top level input file */ 1031556Srgrimeschar basebuf[BUFSIZ]; /* buffer for top level input file */ 1041556Srgrimesstruct parsefile *parsefile = &basepf; /* current input file */ 1051556Srgrimesint init_editline = 0; /* editline library initialized? */ 1061556Srgrimesint whichprompt; /* 1 == PS1, 2 == PS2 */ 1071556Srgrimes 1081556SrgrimesEditLine *el; /* cookie for editline package */ 1091556Srgrimes 11090111SimpSTATIC void pushfile(void); 11190111Simpstatic int preadfd(void); 1121556Srgrimes 1131556Srgrimes#ifdef mkinit 1141556SrgrimesINCLUDE "input.h" 1151556SrgrimesINCLUDE "error.h" 1161556Srgrimes 1171556SrgrimesINIT { 1181556Srgrimes extern char basebuf[]; 1191556Srgrimes 1201556Srgrimes basepf.nextc = basepf.buf = basebuf; 1211556Srgrimes} 1221556Srgrimes 1231556SrgrimesRESET { 1241556Srgrimes if (exception != EXSHELLPROC) 12512043Speter parselleft = parsenleft = 0; /* clear input buffer */ 1261556Srgrimes popallfiles(); 1271556Srgrimes} 1281556Srgrimes 1291556SrgrimesSHELLPROC { 1301556Srgrimes popallfiles(); 1311556Srgrimes} 1321556Srgrimes#endif 1331556Srgrimes 1341556Srgrimes 1351556Srgrimes/* 1361556Srgrimes * Read a line from the script. 1371556Srgrimes */ 1381556Srgrimes 1391556Srgrimeschar * 14090111Simppfgets(char *line, int len) 14117987Speter{ 14225225Ssteve char *p = line; 1431556Srgrimes int nleft = len; 1441556Srgrimes int c; 1451556Srgrimes 1461556Srgrimes while (--nleft > 0) { 1471556Srgrimes c = pgetc_macro(); 1481556Srgrimes if (c == PEOF) { 1491556Srgrimes if (p == line) 1501556Srgrimes return NULL; 1511556Srgrimes break; 1521556Srgrimes } 1531556Srgrimes *p++ = c; 1541556Srgrimes if (c == '\n') 1551556Srgrimes break; 1561556Srgrimes } 1571556Srgrimes *p = '\0'; 1581556Srgrimes return line; 1591556Srgrimes} 1601556Srgrimes 1611556Srgrimes 1621556Srgrimes 1631556Srgrimes/* 1641556Srgrimes * Read a character from the script, returning PEOF on end of file. 1651556Srgrimes * Nul characters in the input are silently discarded. 1661556Srgrimes */ 1671556Srgrimes 1681556Srgrimesint 16990111Simppgetc(void) 17020425Ssteve{ 1711556Srgrimes return pgetc_macro(); 1721556Srgrimes} 1731556Srgrimes 17420425Ssteve 17512043Speterstatic int 17690111Simppreadfd(void) 17712043Speter{ 17812043Speter int nr; 17920425Ssteve parsenextc = parsefile->buf; 1801556Srgrimes 18112043Speterretry: 18225225Ssteve#ifndef NO_HISTORY 18312043Speter if (parsefile->fd == 0 && el) { 18412043Speter const char *rl_cp; 18512043Speter 18612043Speter rl_cp = el_gets(el, &nr); 18712043Speter if (rl_cp == NULL) 18812043Speter nr = 0; 18912043Speter else { 19012043Speter /* XXX - BUFSIZE should redesign so not necessary */ 19120425Ssteve (void) strcpy(parsenextc, rl_cp); 19212043Speter } 19325225Ssteve } else 19425225Ssteve#endif 19512043Speter nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 19612043Speter 19712043Speter if (nr <= 0) { 19812043Speter if (nr < 0) { 19912043Speter if (errno == EINTR) 20012043Speter goto retry; 20112043Speter if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 20212043Speter int flags = fcntl(0, F_GETFL, 0); 20312043Speter if (flags >= 0 && flags & O_NONBLOCK) { 20412043Speter flags &=~ O_NONBLOCK; 20512043Speter if (fcntl(0, F_SETFL, flags) >= 0) { 20612043Speter out2str("sh: turning off NDELAY mode\n"); 20712043Speter goto retry; 20812043Speter } 20912043Speter } 21012043Speter } 21112043Speter } 21220425Ssteve nr = -1; 21312043Speter } 21412043Speter return nr; 21512043Speter} 21612043Speter 2171556Srgrimes/* 2181556Srgrimes * Refill the input buffer and return the next input character: 2191556Srgrimes * 2201556Srgrimes * 1) If a string was pushed back on the input, pop it; 2211556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2221556Srgrimes * from a string so we can't refill the buffer, return EOF. 22320425Ssteve * 3) If there is more in this buffer, use it else call read to fill it. 22420425Ssteve * 4) Process input up to the next newline, deleting nul characters. 2251556Srgrimes */ 2261556Srgrimes 2271556Srgrimesint 22890111Simppreadbuffer(void) 22920425Ssteve{ 23012043Speter char *p, *q; 23112043Speter int more; 23212043Speter int something; 23312043Speter char savec; 2341556Srgrimes 2351556Srgrimes if (parsefile->strpush) { 2361556Srgrimes popstring(); 2371556Srgrimes if (--parsenleft >= 0) 2381556Srgrimes return (*parsenextc++); 2391556Srgrimes } 2401556Srgrimes if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2411556Srgrimes return PEOF; 2421556Srgrimes flushout(&output); 2431556Srgrimes flushout(&errout); 2441556Srgrimes 24512043Speteragain: 24612043Speter if (parselleft <= 0) { 24725225Ssteve if ((parselleft = preadfd()) == -1) { 24812043Speter parselleft = parsenleft = EOF_NLEFT; 24912043Speter return PEOF; 2501556Srgrimes } 2511556Srgrimes } 2521556Srgrimes 25312043Speter q = p = parsenextc; 25412043Speter 2551556Srgrimes /* delete nul characters */ 2561556Srgrimes something = 0; 25712043Speter for (more = 1; more;) { 25812043Speter switch (*p) { 25912043Speter case '\0': 26012043Speter p++; /* Skip nul */ 26112043Speter goto check; 26212043Speter 26312043Speter case '\t': 26412043Speter case ' ': 2651556Srgrimes break; 26612043Speter 26712043Speter case '\n': 26812043Speter parsenleft = q - parsenextc; 26912043Speter more = 0; /* Stop processing here */ 27012043Speter break; 27112043Speter 27212043Speter default: 2731556Srgrimes something = 1; 27412043Speter break; 2751556Srgrimes } 27612043Speter 27712043Speter *q++ = *p++; 27812043Spetercheck: 27912043Speter if (--parselleft <= 0) { 28012043Speter parsenleft = q - parsenextc - 1; 28112043Speter if (parsenleft < 0) 28212043Speter goto again; 28312043Speter *q = '\0'; 28412043Speter more = 0; 28512043Speter } 2861556Srgrimes } 28712043Speter 28812043Speter savec = *q; 2891556Srgrimes *q = '\0'; 2901556Srgrimes 29118018Speter#ifndef NO_HISTORY 2921556Srgrimes if (parsefile->fd == 0 && hist && something) { 29384261Sobrien HistEvent he; 2941556Srgrimes INTOFF; 29584261Sobrien history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD, 29684261Sobrien parsenextc); 2971556Srgrimes INTON; 2981556Srgrimes } 29918018Speter#endif 30012043Speter 3011556Srgrimes if (vflag) { 30212043Speter out2str(parsenextc); 3031556Srgrimes flushout(out2); 3041556Srgrimes } 30512043Speter 30612043Speter *q = savec; 30712043Speter 3081556Srgrimes return *parsenextc++; 3091556Srgrimes} 3101556Srgrimes 3111556Srgrimes/* 3121556Srgrimes * Undo the last call to pgetc. Only one character may be pushed back. 3131556Srgrimes * PEOF may be pushed back. 3141556Srgrimes */ 3151556Srgrimes 3161556Srgrimesvoid 31790111Simppungetc(void) 31890111Simp{ 3191556Srgrimes parsenleft++; 3201556Srgrimes parsenextc--; 3211556Srgrimes} 3221556Srgrimes 3231556Srgrimes/* 3241556Srgrimes * Push a string back onto the input at this current parsefile level. 3251556Srgrimes * We handle aliases this way. 3261556Srgrimes */ 3271556Srgrimesvoid 32890111Simppushstring(char *s, int len, void *ap) 32990111Simp{ 3301556Srgrimes struct strpush *sp; 3311556Srgrimes 3321556Srgrimes INTOFF; 3331556Srgrimes/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/ 3341556Srgrimes if (parsefile->strpush) { 3351556Srgrimes sp = ckmalloc(sizeof (struct strpush)); 3361556Srgrimes sp->prev = parsefile->strpush; 3371556Srgrimes parsefile->strpush = sp; 3381556Srgrimes } else 3391556Srgrimes sp = parsefile->strpush = &(parsefile->basestrpush); 3401556Srgrimes sp->prevstring = parsenextc; 3411556Srgrimes sp->prevnleft = parsenleft; 34212043Speter sp->prevlleft = parselleft; 3431556Srgrimes sp->ap = (struct alias *)ap; 3441556Srgrimes if (ap) 3451556Srgrimes ((struct alias *)ap)->flag |= ALIASINUSE; 3461556Srgrimes parsenextc = s; 3471556Srgrimes parsenleft = len; 3481556Srgrimes INTON; 3491556Srgrimes} 3501556Srgrimes 35117987Spetervoid 35290111Simppopstring(void) 3531556Srgrimes{ 3541556Srgrimes struct strpush *sp = parsefile->strpush; 3551556Srgrimes 3561556Srgrimes INTOFF; 3571556Srgrimes parsenextc = sp->prevstring; 3581556Srgrimes parsenleft = sp->prevnleft; 35912043Speter parselleft = sp->prevlleft; 3601556Srgrimes/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3611556Srgrimes if (sp->ap) 3621556Srgrimes sp->ap->flag &= ~ALIASINUSE; 3631556Srgrimes parsefile->strpush = sp->prev; 3641556Srgrimes if (sp != &(parsefile->basestrpush)) 3651556Srgrimes ckfree(sp); 3661556Srgrimes INTON; 3671556Srgrimes} 3681556Srgrimes 3691556Srgrimes/* 3701556Srgrimes * Set the input to take input from a file. If push is set, push the 3711556Srgrimes * old input onto the stack first. 3721556Srgrimes */ 3731556Srgrimes 3741556Srgrimesvoid 37590111Simpsetinputfile(char *fname, int push) 37617987Speter{ 3771556Srgrimes int fd; 3781556Srgrimes int fd2; 3791556Srgrimes 3801556Srgrimes INTOFF; 3811556Srgrimes if ((fd = open(fname, O_RDONLY)) < 0) 38253891Scracauer error("Can't open %s: %s", fname, strerror(errno)); 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 40190111Simpsetinputfd(int fd, int push) 40217987Speter{ 40325225Ssteve (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 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 42390111Simpsetinputstring(char *string, int push) 42490111Simp{ 4251556Srgrimes INTOFF; 4261556Srgrimes if (push) 4271556Srgrimes pushfile(); 4281556Srgrimes parsenextc = string; 42912043Speter parselleft = parsenleft = strlen(string); 4301556Srgrimes parsefile->buf = NULL; 4311556Srgrimes plinno = 1; 4321556Srgrimes INTON; 4331556Srgrimes} 4341556Srgrimes 4351556Srgrimes 4361556Srgrimes 4371556Srgrimes/* 4381556Srgrimes * To handle the "." command, a stack of input files is used. Pushfile 4391556Srgrimes * adds a new entry to the stack and popfile restores the previous level. 4401556Srgrimes */ 4411556Srgrimes 4421556SrgrimesSTATIC void 44390111Simppushfile(void) 44490111Simp{ 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 46190111Simppopfile(void) 46290111Simp{ 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 48790111Simppopallfiles(void) 48890111Simp{ 4891556Srgrimes while (parsefile != &basepf) 4901556Srgrimes popfile(); 4911556Srgrimes} 4921556Srgrimes 4931556Srgrimes 4941556Srgrimes 4951556Srgrimes/* 4961556Srgrimes * Close the file(s) that the shell is reading commands from. Called 4971556Srgrimes * after a fork is done. 4981556Srgrimes */ 4991556Srgrimes 5001556Srgrimesvoid 50190111Simpclosescript(void) 50290111Simp{ 5031556Srgrimes popallfiles(); 5041556Srgrimes if (parsefile->fd > 0) { 5051556Srgrimes close(parsefile->fd); 5061556Srgrimes parsefile->fd = 0; 5071556Srgrimes } 5081556Srgrimes} 509