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 * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD$"); 401556Srgrimes 4117987Speter#include <stdio.h> /* defines BUFSIZ */ 4217987Speter#include <fcntl.h> 4317987Speter#include <errno.h> 4417987Speter#include <unistd.h> 4517987Speter#include <stdlib.h> 4617987Speter#include <string.h> 4717987Speter 481556Srgrimes/* 491556Srgrimes * This file implements the input routines used by the parser. 501556Srgrimes */ 511556Srgrimes 521556Srgrimes#include "shell.h" 5317987Speter#include "redir.h" 541556Srgrimes#include "syntax.h" 551556Srgrimes#include "input.h" 561556Srgrimes#include "output.h" 571556Srgrimes#include "options.h" 581556Srgrimes#include "memalloc.h" 591556Srgrimes#include "error.h" 601556Srgrimes#include "alias.h" 611556Srgrimes#include "parser.h" 621556Srgrimes#include "myhistedit.h" 63100588Stjr#include "trap.h" 641556Srgrimes 651556Srgrimes#define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 661556Srgrimes 671556SrgrimesMKINIT 681556Srgrimesstruct strpush { 691556Srgrimes struct strpush *prev; /* preceding string on stack */ 701556Srgrimes char *prevstring; 711556Srgrimes int prevnleft; 7212043Speter int prevlleft; 731556Srgrimes struct alias *ap; /* if push was associated with an alias */ 741556Srgrimes}; 751556Srgrimes 761556Srgrimes/* 771556Srgrimes * The parsefile structure pointed to by the global variable parsefile 781556Srgrimes * contains information about the current file being read. 791556Srgrimes */ 801556Srgrimes 811556SrgrimesMKINIT 821556Srgrimesstruct parsefile { 831556Srgrimes struct parsefile *prev; /* preceding file on stack */ 841556Srgrimes int linno; /* current line */ 851556Srgrimes int fd; /* file descriptor (or -1 if string) */ 8620425Ssteve int nleft; /* number of chars left in this line */ 8720425Ssteve int lleft; /* number of lines left in this buffer */ 881556Srgrimes char *nextc; /* next char in buffer */ 891556Srgrimes char *buf; /* input buffer */ 901556Srgrimes struct strpush *strpush; /* for pushing strings at this level */ 911556Srgrimes struct strpush basestrpush; /* so pushing one is fast */ 921556Srgrimes}; 931556Srgrimes 941556Srgrimes 951556Srgrimesint plinno = 1; /* input line number */ 96201053Sjillesint parsenleft; /* copy of parsefile->nleft */ 9712043SpeterMKINIT int parselleft; /* copy of parsefile->lleft */ 981556Srgrimeschar *parsenextc; /* copy of parsefile->nextc */ 991556SrgrimesMKINIT struct parsefile basepf; /* top level input file */ 1001556Srgrimeschar basebuf[BUFSIZ]; /* buffer for top level input file */ 101213760Sobrienstatic struct parsefile *parsefile = &basepf; /* current input file */ 1021556Srgrimesint init_editline = 0; /* editline library initialized? */ 1031556Srgrimesint whichprompt; /* 1 == PS1, 2 == PS2 */ 1041556Srgrimes 1051556SrgrimesEditLine *el; /* cookie for editline package */ 1061556Srgrimes 107213811Sobrienstatic void pushfile(void); 108213811Sobrienstatic int preadfd(void); 109231790Sjillesstatic void popstring(void); 1101556Srgrimes 1111556Srgrimes#ifdef mkinit 1121556SrgrimesINCLUDE "input.h" 1131556SrgrimesINCLUDE "error.h" 1141556Srgrimes 115201053SjillesMKINIT char basebuf[]; 116201053Sjilles 1171556SrgrimesINIT { 1181556Srgrimes basepf.nextc = basepf.buf = basebuf; 1191556Srgrimes} 1201556Srgrimes 1211556SrgrimesRESET { 122194406Sjilles popallfiles(); 123218306Sjilles parselleft = parsenleft = 0; /* clear input buffer */ 1241556Srgrimes} 1251556Srgrimes#endif 1261556Srgrimes 1271556Srgrimes 1281556Srgrimes/* 1291556Srgrimes * Read a line from the script. 1301556Srgrimes */ 1311556Srgrimes 1321556Srgrimeschar * 13390111Simppfgets(char *line, int len) 13417987Speter{ 13525225Ssteve char *p = line; 1361556Srgrimes int nleft = len; 1371556Srgrimes int c; 1381556Srgrimes 1391556Srgrimes while (--nleft > 0) { 1401556Srgrimes c = pgetc_macro(); 1411556Srgrimes if (c == PEOF) { 1421556Srgrimes if (p == line) 1431556Srgrimes return NULL; 1441556Srgrimes break; 1451556Srgrimes } 1461556Srgrimes *p++ = c; 1471556Srgrimes if (c == '\n') 1481556Srgrimes break; 1491556Srgrimes } 1501556Srgrimes *p = '\0'; 1511556Srgrimes return line; 1521556Srgrimes} 1531556Srgrimes 1541556Srgrimes 1551556Srgrimes 1561556Srgrimes/* 1571556Srgrimes * Read a character from the script, returning PEOF on end of file. 1581556Srgrimes * Nul characters in the input are silently discarded. 1591556Srgrimes */ 1601556Srgrimes 1611556Srgrimesint 16290111Simppgetc(void) 16320425Ssteve{ 1641556Srgrimes return pgetc_macro(); 1651556Srgrimes} 1661556Srgrimes 16720425Ssteve 168213811Sobrienstatic int 16990111Simppreadfd(void) 17012043Speter{ 17112043Speter int nr; 17220425Ssteve parsenextc = parsefile->buf; 1731556Srgrimes 174100588Stjr#ifndef NO_HISTORY 175100588Stjr if (el != NULL && gotwinch) { 176100588Stjr gotwinch = 0; 177100588Stjr el_resize(el); 178100588Stjr } 179100588Stjr#endif 18012043Speterretry: 18125225Ssteve#ifndef NO_HISTORY 18212043Speter if (parsefile->fd == 0 && el) { 183158143Sstefanf static const char *rl_cp; 184158143Sstefanf static int el_len; 18512043Speter 18612043Speter if (rl_cp == NULL) 187158143Sstefanf rl_cp = el_gets(el, &el_len); 188158143Sstefanf if (rl_cp == NULL) 189239542Spfg nr = el_len == 0 ? 0 : -1; 19012043Speter else { 191158143Sstefanf nr = el_len; 192158143Sstefanf if (nr > BUFSIZ - 1) 193158143Sstefanf nr = BUFSIZ - 1; 194158143Sstefanf memcpy(parsenextc, rl_cp, nr); 195158143Sstefanf if (nr != el_len) { 196158143Sstefanf el_len -= nr; 197158143Sstefanf rl_cp += nr; 198158143Sstefanf } else 199158143Sstefanf rl_cp = NULL; 20012043Speter } 20125225Ssteve } else 20225225Ssteve#endif 20312043Speter nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 20412043Speter 20512043Speter if (nr <= 0) { 20612043Speter if (nr < 0) { 20712043Speter if (errno == EINTR) 20812043Speter goto retry; 20912043Speter if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 21012043Speter int flags = fcntl(0, F_GETFL, 0); 21112043Speter if (flags >= 0 && flags & O_NONBLOCK) { 21212043Speter flags &=~ O_NONBLOCK; 21312043Speter if (fcntl(0, F_SETFL, flags) >= 0) { 214199629Sjilles out2fmt_flush("sh: turning off NDELAY mode\n"); 21512043Speter goto retry; 21612043Speter } 21712043Speter } 21812043Speter } 21912043Speter } 22020425Ssteve nr = -1; 22112043Speter } 22212043Speter return nr; 22312043Speter} 22412043Speter 2251556Srgrimes/* 2261556Srgrimes * Refill the input buffer and return the next input character: 2271556Srgrimes * 2281556Srgrimes * 1) If a string was pushed back on the input, pop it; 2291556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2301556Srgrimes * from a string so we can't refill the buffer, return EOF. 23120425Ssteve * 3) If there is more in this buffer, use it else call read to fill it. 23220425Ssteve * 4) Process input up to the next newline, deleting nul characters. 2331556Srgrimes */ 2341556Srgrimes 2351556Srgrimesint 23690111Simppreadbuffer(void) 23720425Ssteve{ 23812043Speter char *p, *q; 23912043Speter int more; 24012043Speter int something; 24112043Speter char savec; 2421556Srgrimes 2431556Srgrimes if (parsefile->strpush) { 2441556Srgrimes popstring(); 2451556Srgrimes if (--parsenleft >= 0) 2461556Srgrimes return (*parsenextc++); 2471556Srgrimes } 2481556Srgrimes if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2491556Srgrimes return PEOF; 2501556Srgrimes flushout(&output); 2511556Srgrimes flushout(&errout); 2521556Srgrimes 25312043Speteragain: 25412043Speter if (parselleft <= 0) { 25525225Ssteve if ((parselleft = preadfd()) == -1) { 25612043Speter parselleft = parsenleft = EOF_NLEFT; 25712043Speter return PEOF; 2581556Srgrimes } 2591556Srgrimes } 2601556Srgrimes 26112043Speter q = p = parsenextc; 26212043Speter 2631556Srgrimes /* delete nul characters */ 2641556Srgrimes something = 0; 26512043Speter for (more = 1; more;) { 26612043Speter switch (*p) { 26712043Speter case '\0': 26812043Speter p++; /* Skip nul */ 26912043Speter goto check; 27012043Speter 27112043Speter case '\t': 27212043Speter case ' ': 2731556Srgrimes break; 27412043Speter 27512043Speter case '\n': 27612043Speter parsenleft = q - parsenextc; 27712043Speter more = 0; /* Stop processing here */ 27812043Speter break; 27912043Speter 28012043Speter default: 2811556Srgrimes something = 1; 28212043Speter break; 2831556Srgrimes } 28412043Speter 28512043Speter *q++ = *p++; 28612043Spetercheck: 28712043Speter if (--parselleft <= 0) { 28812043Speter parsenleft = q - parsenextc - 1; 28912043Speter if (parsenleft < 0) 29012043Speter goto again; 29112043Speter *q = '\0'; 29212043Speter more = 0; 29312043Speter } 2941556Srgrimes } 29512043Speter 29612043Speter savec = *q; 2971556Srgrimes *q = '\0'; 2981556Srgrimes 29918018Speter#ifndef NO_HISTORY 3001556Srgrimes if (parsefile->fd == 0 && hist && something) { 30184261Sobrien HistEvent he; 3021556Srgrimes INTOFF; 30384261Sobrien history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD, 30484261Sobrien parsenextc); 3051556Srgrimes INTON; 3061556Srgrimes } 30718018Speter#endif 30812043Speter 3091556Srgrimes if (vflag) { 31012043Speter out2str(parsenextc); 3111556Srgrimes flushout(out2); 3121556Srgrimes } 31312043Speter 31412043Speter *q = savec; 31512043Speter 3161556Srgrimes return *parsenextc++; 3171556Srgrimes} 3181556Srgrimes 3191556Srgrimes/* 320194128Sjilles * Returns if we are certain we are at EOF. Does not cause any more input 321194128Sjilles * to be read from the outside world. 322194128Sjilles */ 323194128Sjilles 324194128Sjillesint 325194128Sjillespreadateof(void) 326194128Sjilles{ 327194128Sjilles if (parsenleft > 0) 328194128Sjilles return 0; 329194128Sjilles if (parsefile->strpush) 330194128Sjilles return 0; 331194128Sjilles if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 332194128Sjilles return 1; 333194128Sjilles return 0; 334194128Sjilles} 335194128Sjilles 336194128Sjilles/* 3371556Srgrimes * Undo the last call to pgetc. Only one character may be pushed back. 3381556Srgrimes * PEOF may be pushed back. 3391556Srgrimes */ 3401556Srgrimes 3411556Srgrimesvoid 34290111Simppungetc(void) 34390111Simp{ 3441556Srgrimes parsenleft++; 3451556Srgrimes parsenextc--; 3461556Srgrimes} 3471556Srgrimes 3481556Srgrimes/* 3491556Srgrimes * Push a string back onto the input at this current parsefile level. 3501556Srgrimes * We handle aliases this way. 3511556Srgrimes */ 3521556Srgrimesvoid 35390111Simppushstring(char *s, int len, void *ap) 35490111Simp{ 3551556Srgrimes struct strpush *sp; 3561556Srgrimes 3571556Srgrimes INTOFF; 358199629Sjilles/*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/ 3591556Srgrimes if (parsefile->strpush) { 3601556Srgrimes sp = ckmalloc(sizeof (struct strpush)); 3611556Srgrimes sp->prev = parsefile->strpush; 3621556Srgrimes parsefile->strpush = sp; 3631556Srgrimes } else 3641556Srgrimes sp = parsefile->strpush = &(parsefile->basestrpush); 3651556Srgrimes sp->prevstring = parsenextc; 3661556Srgrimes sp->prevnleft = parsenleft; 36712043Speter sp->prevlleft = parselleft; 3681556Srgrimes sp->ap = (struct alias *)ap; 3691556Srgrimes if (ap) 3701556Srgrimes ((struct alias *)ap)->flag |= ALIASINUSE; 3711556Srgrimes parsenextc = s; 3721556Srgrimes parsenleft = len; 3731556Srgrimes INTON; 3741556Srgrimes} 3751556Srgrimes 376231790Sjillesstatic void 37790111Simppopstring(void) 3781556Srgrimes{ 3791556Srgrimes struct strpush *sp = parsefile->strpush; 3801556Srgrimes 3811556Srgrimes INTOFF; 3821556Srgrimes parsenextc = sp->prevstring; 3831556Srgrimes parsenleft = sp->prevnleft; 38412043Speter parselleft = sp->prevlleft; 385199629Sjilles/*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3861556Srgrimes if (sp->ap) 3871556Srgrimes sp->ap->flag &= ~ALIASINUSE; 3881556Srgrimes parsefile->strpush = sp->prev; 3891556Srgrimes if (sp != &(parsefile->basestrpush)) 3901556Srgrimes ckfree(sp); 3911556Srgrimes INTON; 3921556Srgrimes} 3931556Srgrimes 3941556Srgrimes/* 3951556Srgrimes * Set the input to take input from a file. If push is set, push the 3961556Srgrimes * old input onto the stack first. 3971556Srgrimes */ 3981556Srgrimes 3991556Srgrimesvoid 400200956Sjillessetinputfile(const char *fname, int push) 40117987Speter{ 4021556Srgrimes int fd; 4031556Srgrimes int fd2; 4041556Srgrimes 4051556Srgrimes INTOFF; 4061556Srgrimes if ((fd = open(fname, O_RDONLY)) < 0) 407222684Sjilles error("cannot open %s: %s", fname, strerror(errno)); 4081556Srgrimes if (fd < 10) { 409124780Sdes fd2 = fcntl(fd, F_DUPFD, 10); 4101556Srgrimes close(fd); 4111556Srgrimes if (fd2 < 0) 4121556Srgrimes error("Out of file descriptors"); 4131556Srgrimes fd = fd2; 4141556Srgrimes } 4151556Srgrimes setinputfd(fd, push); 4161556Srgrimes INTON; 4171556Srgrimes} 4181556Srgrimes 4191556Srgrimes 4201556Srgrimes/* 4211556Srgrimes * Like setinputfile, but takes an open file descriptor. Call this with 4221556Srgrimes * interrupts off. 4231556Srgrimes */ 4241556Srgrimes 4251556Srgrimesvoid 42690111Simpsetinputfd(int fd, int push) 42717987Speter{ 42825225Ssteve (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 4291556Srgrimes if (push) { 4301556Srgrimes pushfile(); 4311556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 4321556Srgrimes } 4331556Srgrimes if (parsefile->fd > 0) 4341556Srgrimes close(parsefile->fd); 4351556Srgrimes parsefile->fd = fd; 4361556Srgrimes if (parsefile->buf == NULL) 4371556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 43812043Speter parselleft = parsenleft = 0; 4391556Srgrimes plinno = 1; 4401556Srgrimes} 4411556Srgrimes 4421556Srgrimes 4431556Srgrimes/* 4441556Srgrimes * Like setinputfile, but takes input from a string. 4451556Srgrimes */ 4461556Srgrimes 4471556Srgrimesvoid 44890111Simpsetinputstring(char *string, int push) 44990111Simp{ 4501556Srgrimes INTOFF; 4511556Srgrimes if (push) 4521556Srgrimes pushfile(); 4531556Srgrimes parsenextc = string; 45412043Speter parselleft = parsenleft = strlen(string); 4551556Srgrimes parsefile->buf = NULL; 4561556Srgrimes plinno = 1; 4571556Srgrimes INTON; 4581556Srgrimes} 4591556Srgrimes 4601556Srgrimes 4611556Srgrimes 4621556Srgrimes/* 4631556Srgrimes * To handle the "." command, a stack of input files is used. Pushfile 4641556Srgrimes * adds a new entry to the stack and popfile restores the previous level. 4651556Srgrimes */ 4661556Srgrimes 467213811Sobrienstatic void 46890111Simppushfile(void) 46990111Simp{ 4701556Srgrimes struct parsefile *pf; 4711556Srgrimes 4721556Srgrimes parsefile->nleft = parsenleft; 47312043Speter parsefile->lleft = parselleft; 4741556Srgrimes parsefile->nextc = parsenextc; 4751556Srgrimes parsefile->linno = plinno; 4761556Srgrimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 4771556Srgrimes pf->prev = parsefile; 4781556Srgrimes pf->fd = -1; 4791556Srgrimes pf->strpush = NULL; 4801556Srgrimes pf->basestrpush.prev = NULL; 4811556Srgrimes parsefile = pf; 4821556Srgrimes} 4831556Srgrimes 4841556Srgrimes 4851556Srgrimesvoid 48690111Simppopfile(void) 48790111Simp{ 4881556Srgrimes struct parsefile *pf = parsefile; 4891556Srgrimes 4901556Srgrimes INTOFF; 4911556Srgrimes if (pf->fd >= 0) 4921556Srgrimes close(pf->fd); 4931556Srgrimes if (pf->buf) 4941556Srgrimes ckfree(pf->buf); 4951556Srgrimes while (pf->strpush) 4961556Srgrimes popstring(); 4971556Srgrimes parsefile = pf->prev; 4981556Srgrimes ckfree(pf); 4991556Srgrimes parsenleft = parsefile->nleft; 50012043Speter parselleft = parsefile->lleft; 5011556Srgrimes parsenextc = parsefile->nextc; 5021556Srgrimes plinno = parsefile->linno; 5031556Srgrimes INTON; 5041556Srgrimes} 5051556Srgrimes 5061556Srgrimes 5071556Srgrimes/* 508199647Sjilles * Return current file (to go back to it later using popfilesupto()). 509199647Sjilles */ 510199647Sjilles 511199647Sjillesstruct parsefile * 512199647Sjillesgetcurrentfile(void) 513199647Sjilles{ 514199647Sjilles return parsefile; 515199647Sjilles} 516199647Sjilles 517199647Sjilles 518199647Sjilles/* 519199647Sjilles * Pop files until the given file is on top again. Useful for regular 520199647Sjilles * builtins that read shell commands from files or strings. 521199647Sjilles * If the given file is not an active file, an error is raised. 522199647Sjilles */ 523199647Sjilles 524199647Sjillesvoid 525199647Sjillespopfilesupto(struct parsefile *file) 526199647Sjilles{ 527199647Sjilles while (parsefile != file && parsefile != &basepf) 528199647Sjilles popfile(); 529199647Sjilles if (parsefile != file) 530199647Sjilles error("popfilesupto() misused"); 531199647Sjilles} 532199647Sjilles 533199647Sjilles/* 5341556Srgrimes * Return to top level. 5351556Srgrimes */ 5361556Srgrimes 5371556Srgrimesvoid 53890111Simppopallfiles(void) 53990111Simp{ 5401556Srgrimes while (parsefile != &basepf) 5411556Srgrimes popfile(); 5421556Srgrimes} 5431556Srgrimes 5441556Srgrimes 5451556Srgrimes 5461556Srgrimes/* 5471556Srgrimes * Close the file(s) that the shell is reading commands from. Called 5481556Srgrimes * after a fork is done. 5491556Srgrimes */ 5501556Srgrimes 5511556Srgrimesvoid 55290111Simpclosescript(void) 55390111Simp{ 5541556Srgrimes popallfiles(); 5551556Srgrimes if (parsefile->fd > 0) { 5561556Srgrimes close(parsefile->fd); 5571556Srgrimes parsefile->fd = 0; 5581556Srgrimes } 5591556Srgrimes} 560