input.c revision 222684
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: head/bin/sh/input.c 222684 2011-06-04 15:05:52Z jilles $"); 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); 1091556Srgrimes 1101556Srgrimes#ifdef mkinit 1111556SrgrimesINCLUDE "input.h" 1121556SrgrimesINCLUDE "error.h" 1131556Srgrimes 114201053SjillesMKINIT char basebuf[]; 115201053Sjilles 1161556SrgrimesINIT { 1171556Srgrimes basepf.nextc = basepf.buf = basebuf; 1181556Srgrimes} 1191556Srgrimes 1201556SrgrimesRESET { 121194406Sjilles popallfiles(); 122218306Sjilles parselleft = parsenleft = 0; /* clear input buffer */ 1231556Srgrimes} 1241556Srgrimes#endif 1251556Srgrimes 1261556Srgrimes 1271556Srgrimes/* 1281556Srgrimes * Read a line from the script. 1291556Srgrimes */ 1301556Srgrimes 1311556Srgrimeschar * 13290111Simppfgets(char *line, int len) 13317987Speter{ 13425225Ssteve char *p = line; 1351556Srgrimes int nleft = len; 1361556Srgrimes int c; 1371556Srgrimes 1381556Srgrimes while (--nleft > 0) { 1391556Srgrimes c = pgetc_macro(); 1401556Srgrimes if (c == PEOF) { 1411556Srgrimes if (p == line) 1421556Srgrimes return NULL; 1431556Srgrimes break; 1441556Srgrimes } 1451556Srgrimes *p++ = c; 1461556Srgrimes if (c == '\n') 1471556Srgrimes break; 1481556Srgrimes } 1491556Srgrimes *p = '\0'; 1501556Srgrimes return line; 1511556Srgrimes} 1521556Srgrimes 1531556Srgrimes 1541556Srgrimes 1551556Srgrimes/* 1561556Srgrimes * Read a character from the script, returning PEOF on end of file. 1571556Srgrimes * Nul characters in the input are silently discarded. 1581556Srgrimes */ 1591556Srgrimes 1601556Srgrimesint 16190111Simppgetc(void) 16220425Ssteve{ 1631556Srgrimes return pgetc_macro(); 1641556Srgrimes} 1651556Srgrimes 16620425Ssteve 167213811Sobrienstatic int 16890111Simppreadfd(void) 16912043Speter{ 17012043Speter int nr; 17120425Ssteve parsenextc = parsefile->buf; 1721556Srgrimes 173100588Stjr#ifndef NO_HISTORY 174100588Stjr if (el != NULL && gotwinch) { 175100588Stjr gotwinch = 0; 176100588Stjr el_resize(el); 177100588Stjr } 178100588Stjr#endif 17912043Speterretry: 18025225Ssteve#ifndef NO_HISTORY 18112043Speter if (parsefile->fd == 0 && el) { 182158143Sstefanf static const char *rl_cp; 183158143Sstefanf static int el_len; 18412043Speter 18512043Speter if (rl_cp == NULL) 186158143Sstefanf rl_cp = el_gets(el, &el_len); 187158143Sstefanf if (rl_cp == NULL) 18812043Speter nr = 0; 18912043Speter else { 190158143Sstefanf nr = el_len; 191158143Sstefanf if (nr > BUFSIZ - 1) 192158143Sstefanf nr = BUFSIZ - 1; 193158143Sstefanf memcpy(parsenextc, rl_cp, nr); 194158143Sstefanf if (nr != el_len) { 195158143Sstefanf el_len -= nr; 196158143Sstefanf rl_cp += nr; 197158143Sstefanf } else 198158143Sstefanf rl_cp = NULL; 19912043Speter } 20025225Ssteve } else 20125225Ssteve#endif 20212043Speter nr = read(parsefile->fd, parsenextc, BUFSIZ - 1); 20312043Speter 20412043Speter if (nr <= 0) { 20512043Speter if (nr < 0) { 20612043Speter if (errno == EINTR) 20712043Speter goto retry; 20812043Speter if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 20912043Speter int flags = fcntl(0, F_GETFL, 0); 21012043Speter if (flags >= 0 && flags & O_NONBLOCK) { 21112043Speter flags &=~ O_NONBLOCK; 21212043Speter if (fcntl(0, F_SETFL, flags) >= 0) { 213199629Sjilles out2fmt_flush("sh: turning off NDELAY mode\n"); 21412043Speter goto retry; 21512043Speter } 21612043Speter } 21712043Speter } 21812043Speter } 21920425Ssteve nr = -1; 22012043Speter } 22112043Speter return nr; 22212043Speter} 22312043Speter 2241556Srgrimes/* 2251556Srgrimes * Refill the input buffer and return the next input character: 2261556Srgrimes * 2271556Srgrimes * 1) If a string was pushed back on the input, pop it; 2281556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2291556Srgrimes * from a string so we can't refill the buffer, return EOF. 23020425Ssteve * 3) If there is more in this buffer, use it else call read to fill it. 23120425Ssteve * 4) Process input up to the next newline, deleting nul characters. 2321556Srgrimes */ 2331556Srgrimes 2341556Srgrimesint 23590111Simppreadbuffer(void) 23620425Ssteve{ 23712043Speter char *p, *q; 23812043Speter int more; 23912043Speter int something; 24012043Speter char savec; 2411556Srgrimes 2421556Srgrimes if (parsefile->strpush) { 2431556Srgrimes popstring(); 2441556Srgrimes if (--parsenleft >= 0) 2451556Srgrimes return (*parsenextc++); 2461556Srgrimes } 2471556Srgrimes if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2481556Srgrimes return PEOF; 2491556Srgrimes flushout(&output); 2501556Srgrimes flushout(&errout); 2511556Srgrimes 25212043Speteragain: 25312043Speter if (parselleft <= 0) { 25425225Ssteve if ((parselleft = preadfd()) == -1) { 25512043Speter parselleft = parsenleft = EOF_NLEFT; 25612043Speter return PEOF; 2571556Srgrimes } 2581556Srgrimes } 2591556Srgrimes 26012043Speter q = p = parsenextc; 26112043Speter 2621556Srgrimes /* delete nul characters */ 2631556Srgrimes something = 0; 26412043Speter for (more = 1; more;) { 26512043Speter switch (*p) { 26612043Speter case '\0': 26712043Speter p++; /* Skip nul */ 26812043Speter goto check; 26912043Speter 27012043Speter case '\t': 27112043Speter case ' ': 2721556Srgrimes break; 27312043Speter 27412043Speter case '\n': 27512043Speter parsenleft = q - parsenextc; 27612043Speter more = 0; /* Stop processing here */ 27712043Speter break; 27812043Speter 27912043Speter default: 2801556Srgrimes something = 1; 28112043Speter break; 2821556Srgrimes } 28312043Speter 28412043Speter *q++ = *p++; 28512043Spetercheck: 28612043Speter if (--parselleft <= 0) { 28712043Speter parsenleft = q - parsenextc - 1; 28812043Speter if (parsenleft < 0) 28912043Speter goto again; 29012043Speter *q = '\0'; 29112043Speter more = 0; 29212043Speter } 2931556Srgrimes } 29412043Speter 29512043Speter savec = *q; 2961556Srgrimes *q = '\0'; 2971556Srgrimes 29818018Speter#ifndef NO_HISTORY 2991556Srgrimes if (parsefile->fd == 0 && hist && something) { 30084261Sobrien HistEvent he; 3011556Srgrimes INTOFF; 30284261Sobrien history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD, 30384261Sobrien parsenextc); 3041556Srgrimes INTON; 3051556Srgrimes } 30618018Speter#endif 30712043Speter 3081556Srgrimes if (vflag) { 30912043Speter out2str(parsenextc); 3101556Srgrimes flushout(out2); 3111556Srgrimes } 31212043Speter 31312043Speter *q = savec; 31412043Speter 3151556Srgrimes return *parsenextc++; 3161556Srgrimes} 3171556Srgrimes 3181556Srgrimes/* 319194128Sjilles * Returns if we are certain we are at EOF. Does not cause any more input 320194128Sjilles * to be read from the outside world. 321194128Sjilles */ 322194128Sjilles 323194128Sjillesint 324194128Sjillespreadateof(void) 325194128Sjilles{ 326194128Sjilles if (parsenleft > 0) 327194128Sjilles return 0; 328194128Sjilles if (parsefile->strpush) 329194128Sjilles return 0; 330194128Sjilles if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 331194128Sjilles return 1; 332194128Sjilles return 0; 333194128Sjilles} 334194128Sjilles 335194128Sjilles/* 3361556Srgrimes * Undo the last call to pgetc. Only one character may be pushed back. 3371556Srgrimes * PEOF may be pushed back. 3381556Srgrimes */ 3391556Srgrimes 3401556Srgrimesvoid 34190111Simppungetc(void) 34290111Simp{ 3431556Srgrimes parsenleft++; 3441556Srgrimes parsenextc--; 3451556Srgrimes} 3461556Srgrimes 3471556Srgrimes/* 3481556Srgrimes * Push a string back onto the input at this current parsefile level. 3491556Srgrimes * We handle aliases this way. 3501556Srgrimes */ 3511556Srgrimesvoid 35290111Simppushstring(char *s, int len, void *ap) 35390111Simp{ 3541556Srgrimes struct strpush *sp; 3551556Srgrimes 3561556Srgrimes INTOFF; 357199629Sjilles/*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/ 3581556Srgrimes if (parsefile->strpush) { 3591556Srgrimes sp = ckmalloc(sizeof (struct strpush)); 3601556Srgrimes sp->prev = parsefile->strpush; 3611556Srgrimes parsefile->strpush = sp; 3621556Srgrimes } else 3631556Srgrimes sp = parsefile->strpush = &(parsefile->basestrpush); 3641556Srgrimes sp->prevstring = parsenextc; 3651556Srgrimes sp->prevnleft = parsenleft; 36612043Speter sp->prevlleft = parselleft; 3671556Srgrimes sp->ap = (struct alias *)ap; 3681556Srgrimes if (ap) 3691556Srgrimes ((struct alias *)ap)->flag |= ALIASINUSE; 3701556Srgrimes parsenextc = s; 3711556Srgrimes parsenleft = len; 3721556Srgrimes INTON; 3731556Srgrimes} 3741556Srgrimes 37517987Spetervoid 37690111Simppopstring(void) 3771556Srgrimes{ 3781556Srgrimes struct strpush *sp = parsefile->strpush; 3791556Srgrimes 3801556Srgrimes INTOFF; 3811556Srgrimes parsenextc = sp->prevstring; 3821556Srgrimes parsenleft = sp->prevnleft; 38312043Speter parselleft = sp->prevlleft; 384199629Sjilles/*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3851556Srgrimes if (sp->ap) 3861556Srgrimes sp->ap->flag &= ~ALIASINUSE; 3871556Srgrimes parsefile->strpush = sp->prev; 3881556Srgrimes if (sp != &(parsefile->basestrpush)) 3891556Srgrimes ckfree(sp); 3901556Srgrimes INTON; 3911556Srgrimes} 3921556Srgrimes 3931556Srgrimes/* 3941556Srgrimes * Set the input to take input from a file. If push is set, push the 3951556Srgrimes * old input onto the stack first. 3961556Srgrimes */ 3971556Srgrimes 3981556Srgrimesvoid 399200956Sjillessetinputfile(const char *fname, int push) 40017987Speter{ 4011556Srgrimes int fd; 4021556Srgrimes int fd2; 4031556Srgrimes 4041556Srgrimes INTOFF; 4051556Srgrimes if ((fd = open(fname, O_RDONLY)) < 0) 406222684Sjilles error("cannot open %s: %s", fname, strerror(errno)); 4071556Srgrimes if (fd < 10) { 408124780Sdes fd2 = fcntl(fd, F_DUPFD, 10); 4091556Srgrimes close(fd); 4101556Srgrimes if (fd2 < 0) 4111556Srgrimes error("Out of file descriptors"); 4121556Srgrimes fd = fd2; 4131556Srgrimes } 4141556Srgrimes setinputfd(fd, push); 4151556Srgrimes INTON; 4161556Srgrimes} 4171556Srgrimes 4181556Srgrimes 4191556Srgrimes/* 4201556Srgrimes * Like setinputfile, but takes an open file descriptor. Call this with 4211556Srgrimes * interrupts off. 4221556Srgrimes */ 4231556Srgrimes 4241556Srgrimesvoid 42590111Simpsetinputfd(int fd, int push) 42617987Speter{ 42725225Ssteve (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 4281556Srgrimes if (push) { 4291556Srgrimes pushfile(); 4301556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 4311556Srgrimes } 4321556Srgrimes if (parsefile->fd > 0) 4331556Srgrimes close(parsefile->fd); 4341556Srgrimes parsefile->fd = fd; 4351556Srgrimes if (parsefile->buf == NULL) 4361556Srgrimes parsefile->buf = ckmalloc(BUFSIZ); 43712043Speter parselleft = parsenleft = 0; 4381556Srgrimes plinno = 1; 4391556Srgrimes} 4401556Srgrimes 4411556Srgrimes 4421556Srgrimes/* 4431556Srgrimes * Like setinputfile, but takes input from a string. 4441556Srgrimes */ 4451556Srgrimes 4461556Srgrimesvoid 44790111Simpsetinputstring(char *string, int push) 44890111Simp{ 4491556Srgrimes INTOFF; 4501556Srgrimes if (push) 4511556Srgrimes pushfile(); 4521556Srgrimes parsenextc = string; 45312043Speter parselleft = parsenleft = strlen(string); 4541556Srgrimes parsefile->buf = NULL; 4551556Srgrimes plinno = 1; 4561556Srgrimes INTON; 4571556Srgrimes} 4581556Srgrimes 4591556Srgrimes 4601556Srgrimes 4611556Srgrimes/* 4621556Srgrimes * To handle the "." command, a stack of input files is used. Pushfile 4631556Srgrimes * adds a new entry to the stack and popfile restores the previous level. 4641556Srgrimes */ 4651556Srgrimes 466213811Sobrienstatic void 46790111Simppushfile(void) 46890111Simp{ 4691556Srgrimes struct parsefile *pf; 4701556Srgrimes 4711556Srgrimes parsefile->nleft = parsenleft; 47212043Speter parsefile->lleft = parselleft; 4731556Srgrimes parsefile->nextc = parsenextc; 4741556Srgrimes parsefile->linno = plinno; 4751556Srgrimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 4761556Srgrimes pf->prev = parsefile; 4771556Srgrimes pf->fd = -1; 4781556Srgrimes pf->strpush = NULL; 4791556Srgrimes pf->basestrpush.prev = NULL; 4801556Srgrimes parsefile = pf; 4811556Srgrimes} 4821556Srgrimes 4831556Srgrimes 4841556Srgrimesvoid 48590111Simppopfile(void) 48690111Simp{ 4871556Srgrimes struct parsefile *pf = parsefile; 4881556Srgrimes 4891556Srgrimes INTOFF; 4901556Srgrimes if (pf->fd >= 0) 4911556Srgrimes close(pf->fd); 4921556Srgrimes if (pf->buf) 4931556Srgrimes ckfree(pf->buf); 4941556Srgrimes while (pf->strpush) 4951556Srgrimes popstring(); 4961556Srgrimes parsefile = pf->prev; 4971556Srgrimes ckfree(pf); 4981556Srgrimes parsenleft = parsefile->nleft; 49912043Speter parselleft = parsefile->lleft; 5001556Srgrimes parsenextc = parsefile->nextc; 5011556Srgrimes plinno = parsefile->linno; 5021556Srgrimes INTON; 5031556Srgrimes} 5041556Srgrimes 5051556Srgrimes 5061556Srgrimes/* 507199647Sjilles * Return current file (to go back to it later using popfilesupto()). 508199647Sjilles */ 509199647Sjilles 510199647Sjillesstruct parsefile * 511199647Sjillesgetcurrentfile(void) 512199647Sjilles{ 513199647Sjilles return parsefile; 514199647Sjilles} 515199647Sjilles 516199647Sjilles 517199647Sjilles/* 518199647Sjilles * Pop files until the given file is on top again. Useful for regular 519199647Sjilles * builtins that read shell commands from files or strings. 520199647Sjilles * If the given file is not an active file, an error is raised. 521199647Sjilles */ 522199647Sjilles 523199647Sjillesvoid 524199647Sjillespopfilesupto(struct parsefile *file) 525199647Sjilles{ 526199647Sjilles while (parsefile != file && parsefile != &basepf) 527199647Sjilles popfile(); 528199647Sjilles if (parsefile != file) 529199647Sjilles error("popfilesupto() misused"); 530199647Sjilles} 531199647Sjilles 532199647Sjilles/* 5331556Srgrimes * Return to top level. 5341556Srgrimes */ 5351556Srgrimes 5361556Srgrimesvoid 53790111Simppopallfiles(void) 53890111Simp{ 5391556Srgrimes while (parsefile != &basepf) 5401556Srgrimes popfile(); 5411556Srgrimes} 5421556Srgrimes 5431556Srgrimes 5441556Srgrimes 5451556Srgrimes/* 5461556Srgrimes * Close the file(s) that the shell is reading commands from. Called 5471556Srgrimes * after a fork is done. 5481556Srgrimes */ 5491556Srgrimes 5501556Srgrimesvoid 55190111Simpclosescript(void) 55290111Simp{ 5531556Srgrimes popallfiles(); 5541556Srgrimes if (parsefile->fd > 0) { 5551556Srgrimes close(parsefile->fd); 5561556Srgrimes parsefile->fd = 0; 5571556Srgrimes } 5581556Srgrimes} 559