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