Home | History | Annotate | Line # | Download | only in sh
input.c revision 1.65
      1 /*	$NetBSD: input.c,v 1.65 2019/01/09 11:04:54 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
     39 #else
     40 __RCSID("$NetBSD: input.c,v 1.65 2019/01/09 11:04:54 kre Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <stdio.h>	/* defines BUFSIZ */
     45 #include <fcntl.h>
     46 #include <errno.h>
     47 #include <unistd.h>
     48 #include <limits.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <sys/stat.h>
     52 
     53 /*
     54  * This file implements the input routines used by the parser.
     55  */
     56 
     57 #include "shell.h"
     58 #include "redir.h"
     59 #include "syntax.h"
     60 #include "input.h"
     61 #include "output.h"
     62 #include "options.h"
     63 #include "memalloc.h"
     64 #include "error.h"
     65 #include "alias.h"
     66 #include "parser.h"
     67 #include "myhistedit.h"
     68 #include "show.h"
     69 
     70 #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
     71 
     72 MKINIT
     73 struct strpush {
     74 	struct strpush *prev;	/* preceding string on stack */
     75 	const char *prevstring;
     76 	int prevnleft;
     77 	int prevlleft;
     78 	struct alias *ap;	/* if push was associated with an alias */
     79 };
     80 
     81 /*
     82  * The parsefile structure pointed to by the global variable parsefile
     83  * contains information about the current file being read.
     84  */
     85 
     86 MKINIT
     87 struct parsefile {
     88 	struct parsefile *prev;	/* preceding file on stack */
     89 	int linno;		/* current line */
     90 	int fd;			/* file descriptor (or -1 if string) */
     91 	int nleft;		/* number of chars left in this line */
     92 	int lleft;		/* number of chars left in this buffer */
     93 	const char *nextc;	/* next char in buffer */
     94 	char *buf;		/* input buffer */
     95 	struct strpush *strpush; /* for pushing strings at this level */
     96 	struct strpush basestrpush; /* so pushing one is fast */
     97 };
     98 
     99 
    100 int plinno = 1;			/* input line number */
    101 int parsenleft;			/* copy of parsefile->nleft */
    102 MKINIT int parselleft;		/* copy of parsefile->lleft */
    103 const char *parsenextc;		/* copy of parsefile->nextc */
    104 MKINIT struct parsefile basepf;	/* top level input file */
    105 MKINIT char basebuf[BUFSIZ];	/* buffer for top level input file */
    106 struct parsefile *parsefile = &basepf;	/* current input file */
    107 int init_editline = 0;		/* editline library initialized? */
    108 int whichprompt;		/* 1 == PS1, 2 == PS2 */
    109 
    110 STATIC void pushfile(void);
    111 static int preadfd(void);
    112 
    113 #ifdef mkinit
    114 INCLUDE <stdio.h>
    115 INCLUDE "input.h"
    116 INCLUDE "error.h"
    117 
    118 INIT {
    119 	basepf.nextc = basepf.buf = basebuf;
    120 }
    121 
    122 RESET {
    123 	if (exception != EXSHELLPROC)
    124 		parselleft = parsenleft = 0;	/* clear input buffer */
    125 	popallfiles();
    126 }
    127 
    128 SHELLPROC {
    129 	popallfiles();
    130 }
    131 #endif
    132 
    133 
    134 #if 0		/* this is unused */
    135 /*
    136  * Read a line from the script.
    137  */
    138 
    139 char *
    140 pfgets(char *line, int len)
    141 {
    142 	char *p = line;
    143 	int nleft = len;
    144 	int c;
    145 
    146 	while (--nleft > 0) {
    147 		c = pgetc_macro();
    148 		if (c == PEOF) {
    149 			if (p == line)
    150 				return NULL;
    151 			break;
    152 		}
    153 		if (c == PFAKE) {
    154 			++nleft;
    155 			continue;
    156 		}
    157 		*p++ = c;
    158 		if (c == '\n')
    159 			break;
    160 	}
    161 	*p = '\0';
    162 	return line;
    163 }
    164 #endif
    165 
    166 
    167 /*
    168  * Read a character from the script, returning PEOF on end of file.
    169  * Nul characters in the input are silently discarded.
    170  */
    171 
    172 int
    173 pgetc(void)
    174 {
    175 	int c;
    176 
    177 	c = pgetc_macro();
    178 	if (c == PFAKE)
    179 		c = pgetc_macro();
    180 	return c;
    181 }
    182 
    183 
    184 static int
    185 preadfd(void)
    186 {
    187 	int nr;
    188 	char *buf =  parsefile->buf;
    189 	parsenextc = buf;
    190 
    191  retry:
    192 #ifndef SMALL
    193 	if (parsefile->fd == 0 && el) {
    194 		static const char *rl_cp;
    195 		static int el_len;
    196 
    197 		if (rl_cp == NULL)
    198 			rl_cp = el_gets(el, &el_len);
    199 		if (rl_cp == NULL)
    200 			nr = el_len == 0 ? 0 : -1;
    201 		else {
    202 			nr = el_len;
    203 			if (nr > BUFSIZ - 8)
    204 				nr = BUFSIZ - 8;
    205 			memcpy(buf, rl_cp, nr);
    206 			if (nr != el_len) {
    207 				el_len -= nr;
    208 				rl_cp += nr;
    209 			} else
    210 				rl_cp = 0;
    211 		}
    212 
    213 	} else
    214 #endif
    215 		nr = read(parsefile->fd, buf, BUFSIZ - 8);
    216 
    217 
    218 	if (nr <= 0) {
    219                 if (nr < 0) {
    220                         if (errno == EINTR)
    221                                 goto retry;
    222                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    223                                 int flags = fcntl(0, F_GETFL, 0);
    224 
    225                                 if (flags >= 0 && flags & O_NONBLOCK) {
    226                                         flags &=~ O_NONBLOCK;
    227                                         if (fcntl(0, F_SETFL, flags) >= 0) {
    228 						out2str("sh: turning off NDELAY mode\n");
    229                                                 goto retry;
    230                                         }
    231                                 }
    232                         }
    233                 }
    234                 nr = -1;
    235 	}
    236 	return nr;
    237 }
    238 
    239 /*
    240  * Refill the input buffer and return the next input character:
    241  *
    242  * 1) If a string was pushed back on the input, pop it;
    243  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
    244  *    from a string so we can't refill the buffer, return EOF.
    245  * 3) If the is more stuff in this buffer, use it else call read to fill it.
    246  * 4) Process input up to the next newline, deleting nul characters.
    247  */
    248 
    249 int
    250 preadbuffer(void)
    251 {
    252 	char *p, *q;
    253 	int more;
    254 #ifndef SMALL
    255 	int something;
    256 #endif
    257 	char savec;
    258 
    259 	while (parsefile->strpush) {
    260 		if (parsenleft == -1 && parsefile->strpush->ap != NULL)
    261 			return PFAKE;
    262 		popstring();
    263 		if (--parsenleft >= 0)
    264 			return (*parsenextc++);
    265 	}
    266 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    267 		return PEOF;
    268 	flushout(&output);
    269 	flushout(&errout);
    270 
    271  again:
    272 	if (parselleft <= 0) {
    273 		if ((parselleft = preadfd()) == -1) {
    274 			parselleft = parsenleft = EOF_NLEFT;
    275 			return PEOF;
    276 		}
    277 	}
    278 
    279 		/* p = (not const char *)parsenextc; */
    280 	p = parsefile->buf + (parsenextc - parsefile->buf);
    281 	q = p;
    282 
    283 	/* delete nul characters */
    284 #ifndef SMALL
    285 	something = 0;
    286 #endif
    287 	for (more = 1; more;) {
    288 		switch (*p) {
    289 		case '\0':
    290 			p++;	/* Skip nul */
    291 			goto check;
    292 
    293 		case '\t':
    294 		case ' ':
    295 			break;
    296 
    297 		case '\n':
    298 			parsenleft = q - parsenextc;
    299 			more = 0; /* Stop processing here */
    300 			break;
    301 
    302 		default:
    303 #ifndef SMALL
    304 			something = 1;
    305 #endif
    306 			break;
    307 		}
    308 
    309 		*q++ = *p++;
    310  check:
    311 		if (--parselleft <= 0) {
    312 			parsenleft = q - parsenextc - 1;
    313 			if (parsenleft < 0)
    314 				goto again;
    315 			*q = '\0';
    316 			more = 0;
    317 		}
    318 	}
    319 
    320 	savec = *q;
    321 	*q = '\0';
    322 
    323 #ifndef SMALL
    324 	if (parsefile->fd == 0 && hist && (something || whichprompt == 2)) {
    325 		HistEvent he;
    326 
    327 		INTOFF;
    328 		history(hist, &he, whichprompt != 2 ? H_ENTER : H_APPEND,
    329 		    parsenextc);
    330 		INTON;
    331 	}
    332 #endif
    333 
    334 	if (vflag) {
    335 		out2str(parsenextc);
    336 		flushout(out2);
    337 	}
    338 
    339 	*q = savec;
    340 
    341 	return *parsenextc++;
    342 }
    343 
    344 /*
    345  * Test whether we have reached EOF on input stream.
    346  * Return true only if certain (without attempting a read).
    347  *
    348  * Note the similarity to the opening section of preadbuffer()
    349  */
    350 int
    351 at_eof(void)
    352 {
    353 	struct strpush *sp = parsefile->strpush;
    354 
    355 	if (parsenleft > 0)	/* more chars are in the buffer */
    356 		return 0;
    357 
    358 	while (sp != NULL) {
    359 		/*
    360 		 * If any pushed string has any remaining data,
    361 		 * then we are not at EOF  (simulating popstring())
    362 		 */
    363 		if (sp->prevnleft > 0)
    364 			return 0;
    365 		sp = sp->prev;
    366 	}
    367 
    368 	/*
    369 	 * If we reached real EOF and pushed it back,
    370 	 * or if we are just processing a string (not reading a file)
    371 	 * then there is no more.   Note that if a file pushes a
    372 	 * string, the file's ->buf remains present.
    373 	 */
    374 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    375 		return 1;
    376 
    377 	/*
    378 	 * In other cases, there might be more
    379 	 */
    380 	return 0;
    381 }
    382 
    383 /*
    384  * Undo the last call to pgetc.  Only one character may be pushed back.
    385  * PEOF may be pushed back.
    386  */
    387 
    388 void
    389 pungetc(void)
    390 {
    391 	parsenleft++;
    392 	parsenextc--;
    393 }
    394 
    395 /*
    396  * Push a string back onto the input at this current parsefile level.
    397  * We handle aliases this way.
    398  */
    399 void
    400 pushstring(const char *s, int len, struct alias *ap)
    401 {
    402 	struct strpush *sp;
    403 
    404 	VTRACE(DBG_INPUT,
    405 	    ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n",
    406 	    len, s, len, ap ? " for alias:'" : "",
    407 	    ap ? ap->name : "", ap ? "'" : "",
    408 	    parsenleft, parselleft, parsenleft, parsenextc));
    409 
    410 	INTOFF;
    411 	if (parsefile->strpush) {
    412 		sp = ckmalloc(sizeof (struct strpush));
    413 		sp->prev = parsefile->strpush;
    414 		parsefile->strpush = sp;
    415 	} else
    416 		sp = parsefile->strpush = &(parsefile->basestrpush);
    417 
    418 	sp->prevstring = parsenextc;
    419 	sp->prevnleft = parsenleft;
    420 	sp->prevlleft = parselleft;
    421 	sp->ap = ap;
    422 	if (ap)
    423 		ap->flag |= ALIASINUSE;
    424 	parsenextc = s;
    425 	parsenleft = len;
    426 	INTON;
    427 }
    428 
    429 void
    430 popstring(void)
    431 {
    432 	struct strpush *sp = parsefile->strpush;
    433 
    434 	INTOFF;
    435 	if (sp->ap) {
    436 		if (parsenextc != sp->ap->val &&
    437 		   (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
    438 			checkkwd |= CHKALIAS;
    439 		sp->ap->flag &= ~ALIASINUSE;
    440 	}
    441 	parsenextc = sp->prevstring;
    442 	parsenleft = sp->prevnleft;
    443 	parselleft = sp->prevlleft;
    444 
    445 	VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n",
    446 	    sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "",
    447 	    sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc));
    448 
    449 	parsefile->strpush = sp->prev;
    450 	if (sp != &(parsefile->basestrpush))
    451 		ckfree(sp);
    452 	INTON;
    453 }
    454 
    455 /*
    456  * Set the input to take input from a file.  If push is set, push the
    457  * old input onto the stack first.
    458  */
    459 
    460 void
    461 setinputfile(const char *fname, int push)
    462 {
    463 	unsigned char magic[4];
    464 	int fd;
    465 	int fd2;
    466 	struct stat sb;
    467 
    468 	CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no"));
    469 
    470 	INTOFF;
    471 	if ((fd = open(fname, O_RDONLY)) < 0)
    472 		error("Can't open %s", fname);
    473 
    474 	/* Since the message "Syntax error: "(" unexpected" is not very
    475 	 * helpful, we check if the file starts with the ELF magic to
    476 	 * avoid that message. The first lseek tries to make sure that
    477 	 * we can later rewind the file.
    478 	 */
    479 	if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) &&
    480 	    lseek(fd, 0, SEEK_SET) == 0) {
    481 		if (read(fd, magic, 4) == 4) {
    482 			if (memcmp(magic, "\177ELF", 4) == 0) {
    483 				(void)close(fd);
    484 				error("Cannot execute ELF binary %s", fname);
    485 			}
    486 		}
    487 		if (lseek(fd, 0, SEEK_SET) != 0) {
    488 			(void)close(fd);
    489 			error("Cannot rewind the file %s", fname);
    490 		}
    491 	}
    492 
    493 	fd2 = to_upper_fd(fd);	/* closes fd, returns higher equiv */
    494 	if (fd2 == fd) {
    495 		(void) close(fd);
    496 		error("Out of file descriptors");
    497 	}
    498 
    499 	setinputfd(fd2, push);
    500 	INTON;
    501 }
    502 
    503 /*
    504  * When a shell fd needs to be altered (when the user wants to use
    505  * the same fd - rare, but happens - we need to locate the ref to
    506  * the fd, and update it.  This happens via a callback.
    507  * This is the callback func for fd's used for shell input
    508  */
    509 static void
    510 input_fd_swap(int from, int to)
    511 {
    512 	struct parsefile *pf;
    513 
    514 	pf = parsefile;
    515 	while (pf != NULL) {		/* don't need to stop at basepf */
    516 		if (pf->fd == from)
    517 			pf->fd = to;
    518 		pf = pf->prev;
    519 	}
    520 }
    521 
    522 /*
    523  * Like setinputfile, but takes an open file descriptor.  Call this with
    524  * interrupts off.
    525  */
    526 
    527 void
    528 setinputfd(int fd, int push)
    529 {
    530 	VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no"));
    531 
    532 	register_sh_fd(fd, input_fd_swap);
    533 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
    534 	if (push)
    535 		pushfile();
    536 	if (parsefile->fd > 0)
    537 		sh_close(parsefile->fd);
    538 	parsefile->fd = fd;
    539 	if (parsefile->buf == NULL)
    540 		parsefile->buf = ckmalloc(BUFSIZ);
    541 	parselleft = parsenleft = 0;
    542 	plinno = 1;
    543 
    544 	CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd,
    545 	    push ? "" : "no"));
    546 }
    547 
    548 
    549 /*
    550  * Like setinputfile, but takes input from a string.
    551  */
    552 
    553 void
    554 setinputstring(char *string, int push, int line1)
    555 {
    556 
    557 	INTOFF;
    558 	if (push)		/* XXX: always, as it happens */
    559 		pushfile();
    560 	parsenextc = string;
    561 	parselleft = parsenleft = strlen(string);
    562 	plinno = line1;
    563 
    564 	CTRACE(DBG_INPUT,
    565 	    ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string,
    566 	    (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1));
    567 	INTON;
    568 }
    569 
    570 
    571 
    572 /*
    573  * To handle the "." command, a stack of input files is used.  Pushfile
    574  * adds a new entry to the stack and popfile restores the previous level.
    575  */
    576 
    577 STATIC void
    578 pushfile(void)
    579 {
    580 	struct parsefile *pf;
    581 
    582 	VTRACE(DBG_INPUT,
    583 	    ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n",
    584 	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
    585 	    parsenleft, parsenextc, plinno));
    586 
    587 	parsefile->nleft = parsenleft;
    588 	parsefile->lleft = parselleft;
    589 	parsefile->nextc = parsenextc;
    590 	parsefile->linno = plinno;
    591 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    592 	pf->prev = parsefile;
    593 	pf->fd = -1;
    594 	pf->strpush = NULL;
    595 	pf->basestrpush.prev = NULL;
    596 	pf->buf = NULL;
    597 	parsefile = pf;
    598 }
    599 
    600 
    601 void
    602 popfile(void)
    603 {
    604 	struct parsefile *pf = parsefile;
    605 
    606 	INTOFF;
    607 	if (pf->fd >= 0)
    608 		sh_close(pf->fd);
    609 	if (pf->buf)
    610 		ckfree(pf->buf);
    611 	while (pf->strpush)
    612 		popstring();
    613 	parsefile = pf->prev;
    614 	ckfree(pf);
    615 	parsenleft = parsefile->nleft;
    616 	parselleft = parsefile->lleft;
    617 	parsenextc = parsefile->nextc;
    618 
    619 	VTRACE(DBG_INPUT,
    620 	    ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n",
    621 	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
    622 	    parsenleft, parsenextc, plinno, parsefile->linno));
    623 
    624 	plinno = parsefile->linno;
    625 	INTON;
    626 }
    627 
    628 /*
    629  * Return current file (to go back to it later using popfilesupto()).
    630  */
    631 
    632 struct parsefile *
    633 getcurrentfile(void)
    634 {
    635 	return parsefile;
    636 }
    637 
    638 
    639 /*
    640  * Pop files until the given file is on top again. Useful for regular
    641  * builtins that read shell commands from files or strings.
    642  * If the given file is not an active file, an error is raised.
    643  */
    644 
    645 void
    646 popfilesupto(struct parsefile *file)
    647 {
    648 	while (parsefile != file && parsefile != &basepf)
    649 		popfile();
    650 	if (parsefile != file)
    651 		error("popfilesupto() misused");
    652 }
    653 
    654 
    655 /*
    656  * Return to top level.
    657  */
    658 
    659 void
    660 popallfiles(void)
    661 {
    662 	while (parsefile != &basepf)
    663 		popfile();
    664 }
    665 
    666 
    667 
    668 /*
    669  * Close the file(s) that the shell is reading commands from.  Called
    670  * after a fork is done.
    671  *
    672  * Takes one arg, vfork, which tells it to not modify its global vars
    673  * as it is still running in the parent.
    674  *
    675  * This code is (probably) unnecessary as the 'close on exec' flag is
    676  * set and should be enough.  In the vfork case it is definitely wrong
    677  * to close the fds as another fork() may be done later to feed data
    678  * from a 'here' document into a pipe and we don't want to close the
    679  * pipe!
    680  */
    681 
    682 void
    683 closescript(int vforked)
    684 {
    685 	if (vforked)
    686 		return;
    687 	popallfiles();
    688 	if (parsefile->fd > 0) {
    689 		sh_close(parsefile->fd);
    690 		parsefile->fd = 0;
    691 	}
    692 }
    693