Home | History | Annotate | Line # | Download | only in sh
input.c revision 1.56.2.1
      1  1.56.2.1       snj /*	$NetBSD: input.c,v 1.56.2.1 2017/07/23 14:58:14 snj 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.56.2.1       snj __RCSID("$NetBSD: input.c,v 1.56.2.1 2017/07/23 14:58:14 snj 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.56.2.1       snj #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.56.2.1       snj #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.1       cgd 		if (c == PEOF) {
    149       1.1       cgd 			if (p == line)
    150       1.1       cgd 				return NULL;
    151       1.1       cgd 			break;
    152       1.1       cgd 		}
    153       1.1       cgd 		*p++ = c;
    154       1.1       cgd 		if (c == '\n')
    155       1.1       cgd 			break;
    156       1.1       cgd 	}
    157       1.1       cgd 	*p = '\0';
    158       1.1       cgd 	return line;
    159       1.1       cgd }
    160  1.56.2.1       snj #endif
    161       1.1       cgd 
    162       1.1       cgd 
    163       1.1       cgd /*
    164       1.1       cgd  * Read a character from the script, returning PEOF on end of file.
    165       1.1       cgd  * Nul characters in the input are silently discarded.
    166       1.1       cgd  */
    167       1.1       cgd 
    168       1.1       cgd int
    169      1.37  christos pgetc(void)
    170      1.17  christos {
    171       1.1       cgd 	return pgetc_macro();
    172       1.1       cgd }
    173       1.1       cgd 
    174       1.1       cgd 
    175      1.17  christos static int
    176      1.37  christos preadfd(void)
    177      1.17  christos {
    178      1.17  christos 	int nr;
    179      1.32  christos 	char *buf =  parsefile->buf;
    180      1.32  christos 	parsenextc = buf;
    181      1.17  christos 
    182  1.56.2.1       snj  retry:
    183      1.25  christos #ifndef SMALL
    184      1.17  christos 	if (parsefile->fd == 0 && el) {
    185      1.37  christos 		static const char *rl_cp;
    186      1.37  christos 		static int el_len;
    187      1.17  christos 
    188      1.37  christos 		if (rl_cp == NULL)
    189      1.37  christos 			rl_cp = el_gets(el, &el_len);
    190      1.17  christos 		if (rl_cp == NULL)
    191      1.42       roy 			nr = el_len == 0 ? 0 : -1;
    192      1.17  christos 		else {
    193      1.37  christos 			nr = el_len;
    194      1.37  christos 			if (nr > BUFSIZ - 8)
    195      1.37  christos 				nr = BUFSIZ - 8;
    196      1.37  christos 			memcpy(buf, rl_cp, nr);
    197      1.37  christos 			if (nr != el_len) {
    198      1.37  christos 				el_len -= nr;
    199      1.37  christos 				rl_cp += nr;
    200      1.37  christos 			} else
    201      1.37  christos 				rl_cp = 0;
    202      1.17  christos 		}
    203      1.37  christos 
    204      1.24  christos 	} else
    205      1.24  christos #endif
    206      1.37  christos 		nr = read(parsefile->fd, buf, BUFSIZ - 8);
    207      1.24  christos 
    208      1.17  christos 
    209      1.17  christos 	if (nr <= 0) {
    210      1.17  christos                 if (nr < 0) {
    211      1.17  christos                         if (errno == EINTR)
    212      1.17  christos                                 goto retry;
    213      1.17  christos                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    214      1.17  christos                                 int flags = fcntl(0, F_GETFL, 0);
    215  1.56.2.1       snj 
    216      1.17  christos                                 if (flags >= 0 && flags & O_NONBLOCK) {
    217      1.17  christos                                         flags &=~ O_NONBLOCK;
    218      1.17  christos                                         if (fcntl(0, F_SETFL, flags) >= 0) {
    219      1.17  christos 						out2str("sh: turning off NDELAY mode\n");
    220      1.17  christos                                                 goto retry;
    221      1.17  christos                                         }
    222      1.17  christos                                 }
    223      1.17  christos                         }
    224      1.17  christos                 }
    225      1.17  christos                 nr = -1;
    226      1.17  christos 	}
    227      1.17  christos 	return nr;
    228      1.17  christos }
    229      1.17  christos 
    230       1.1       cgd /*
    231       1.1       cgd  * Refill the input buffer and return the next input character:
    232       1.1       cgd  *
    233       1.5       jtc  * 1) If a string was pushed back on the input, pop it;
    234       1.1       cgd  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
    235       1.1       cgd  *    from a string so we can't refill the buffer, return EOF.
    236      1.17  christos  * 3) If the is more stuff in this buffer, use it else call read to fill it.
    237      1.17  christos  * 4) Process input up to the next newline, deleting nul characters.
    238       1.1       cgd  */
    239       1.1       cgd 
    240       1.1       cgd int
    241      1.37  christos preadbuffer(void)
    242      1.17  christos {
    243      1.17  christos 	char *p, *q;
    244      1.17  christos 	int more;
    245      1.46       mrg #ifndef SMALL
    246      1.17  christos 	int something;
    247      1.46       mrg #endif
    248      1.17  christos 	char savec;
    249       1.1       cgd 
    250      1.56       kre 	while (parsefile->strpush) {
    251       1.5       jtc 		popstring();
    252       1.1       cgd 		if (--parsenleft >= 0)
    253       1.5       jtc 			return (*parsenextc++);
    254       1.1       cgd 	}
    255       1.1       cgd 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    256       1.1       cgd 		return PEOF;
    257       1.1       cgd 	flushout(&output);
    258       1.1       cgd 	flushout(&errout);
    259       1.5       jtc 
    260  1.56.2.1       snj  again:
    261      1.17  christos 	if (parselleft <= 0) {
    262      1.26  christos 		if ((parselleft = preadfd()) == -1) {
    263      1.17  christos 			parselleft = parsenleft = EOF_NLEFT;
    264      1.17  christos 			return PEOF;
    265       1.5       jtc 		}
    266      1.17  christos 	}
    267       1.5       jtc 
    268      1.55       kre 		/* p = (not const char *)parsenextc; */
    269      1.55       kre 	p = parsefile->buf + (parsenextc - parsefile->buf);
    270      1.55       kre 	q = p;
    271       1.1       cgd 
    272       1.1       cgd 	/* delete nul characters */
    273      1.46       mrg #ifndef SMALL
    274       1.5       jtc 	something = 0;
    275      1.46       mrg #endif
    276      1.17  christos 	for (more = 1; more;) {
    277      1.17  christos 		switch (*p) {
    278      1.17  christos 		case '\0':
    279      1.17  christos 			p++;	/* Skip nul */
    280      1.17  christos 			goto check;
    281      1.20  christos 
    282      1.17  christos 		case '\t':
    283      1.17  christos 		case ' ':
    284       1.1       cgd 			break;
    285      1.17  christos 
    286      1.17  christos 		case '\n':
    287      1.17  christos 			parsenleft = q - parsenextc;
    288      1.17  christos 			more = 0; /* Stop processing here */
    289      1.17  christos 			break;
    290      1.17  christos 
    291      1.17  christos 		default:
    292      1.46       mrg #ifndef SMALL
    293       1.5       jtc 			something = 1;
    294      1.46       mrg #endif
    295      1.17  christos 			break;
    296      1.17  christos 		}
    297      1.17  christos 
    298      1.17  christos 		*q++ = *p++;
    299  1.56.2.1       snj  check:
    300      1.17  christos 		if (--parselleft <= 0) {
    301      1.17  christos 			parsenleft = q - parsenextc - 1;
    302      1.17  christos 			if (parsenleft < 0)
    303      1.17  christos 				goto again;
    304      1.17  christos 			*q = '\0';
    305      1.17  christos 			more = 0;
    306       1.5       jtc 		}
    307       1.1       cgd 	}
    308      1.17  christos 
    309      1.17  christos 	savec = *q;
    310       1.5       jtc 	*q = '\0';
    311       1.5       jtc 
    312      1.25  christos #ifndef SMALL
    313       1.5       jtc 	if (parsefile->fd == 0 && hist && something) {
    314      1.28  christos 		HistEvent he;
    315       1.5       jtc 		INTOFF;
    316      1.31  christos 		history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
    317      1.31  christos 		    parsenextc);
    318       1.5       jtc 		INTON;
    319       1.5       jtc 	}
    320      1.19  christos #endif
    321      1.17  christos 
    322       1.5       jtc 	if (vflag) {
    323      1.17  christos 		out2str(parsenextc);
    324       1.5       jtc 		flushout(out2);
    325       1.5       jtc 	}
    326      1.17  christos 
    327      1.17  christos 	*q = savec;
    328      1.17  christos 
    329       1.1       cgd 	return *parsenextc++;
    330       1.1       cgd }
    331       1.1       cgd 
    332       1.1       cgd /*
    333       1.1       cgd  * Undo the last call to pgetc.  Only one character may be pushed back.
    334       1.1       cgd  * PEOF may be pushed back.
    335       1.1       cgd  */
    336       1.1       cgd 
    337       1.1       cgd void
    338      1.37  christos pungetc(void)
    339      1.37  christos {
    340       1.1       cgd 	parsenleft++;
    341       1.1       cgd 	parsenextc--;
    342       1.1       cgd }
    343       1.1       cgd 
    344       1.1       cgd /*
    345       1.5       jtc  * Push a string back onto the input at this current parsefile level.
    346       1.5       jtc  * We handle aliases this way.
    347       1.1       cgd  */
    348       1.1       cgd void
    349      1.55       kre pushstring(const char *s, int len, struct alias *ap)
    350      1.37  christos {
    351       1.5       jtc 	struct strpush *sp;
    352       1.5       jtc 
    353  1.56.2.1       snj 	VTRACE(DBG_INPUT,
    354  1.56.2.1       snj 	    ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n",
    355  1.56.2.1       snj 	    len, s, len, ap ? " for alias:'" : "",
    356  1.56.2.1       snj 	    ap ? ap->name : "", ap ? "'" : "",
    357  1.56.2.1       snj 	    parsenleft, parselleft, parsenleft, parsenextc));
    358  1.56.2.1       snj 
    359       1.5       jtc 	INTOFF;
    360       1.5       jtc 	if (parsefile->strpush) {
    361       1.5       jtc 		sp = ckmalloc(sizeof (struct strpush));
    362       1.5       jtc 		sp->prev = parsefile->strpush;
    363       1.5       jtc 		parsefile->strpush = sp;
    364       1.5       jtc 	} else
    365       1.5       jtc 		sp = parsefile->strpush = &(parsefile->basestrpush);
    366  1.56.2.1       snj 
    367       1.5       jtc 	sp->prevstring = parsenextc;
    368       1.5       jtc 	sp->prevnleft = parsenleft;
    369      1.17  christos 	sp->prevlleft = parselleft;
    370      1.53       kre 	sp->ap = ap;
    371       1.5       jtc 	if (ap)
    372      1.53       kre 		ap->flag |= ALIASINUSE;
    373       1.5       jtc 	parsenextc = s;
    374       1.5       jtc 	parsenleft = len;
    375       1.5       jtc 	INTON;
    376       1.1       cgd }
    377       1.1       cgd 
    378      1.11       cgd void
    379      1.37  christos popstring(void)
    380       1.5       jtc {
    381       1.5       jtc 	struct strpush *sp = parsefile->strpush;
    382       1.1       cgd 
    383       1.5       jtc 	INTOFF;
    384       1.5       jtc 	parsenextc = sp->prevstring;
    385       1.5       jtc 	parsenleft = sp->prevnleft;
    386      1.17  christos 	parselleft = sp->prevlleft;
    387  1.56.2.1       snj 
    388  1.56.2.1       snj 	VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n",
    389  1.56.2.1       snj 	    sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "",
    390  1.56.2.1       snj 	    sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc));
    391  1.56.2.1       snj 
    392       1.5       jtc 	if (sp->ap)
    393       1.5       jtc 		sp->ap->flag &= ~ALIASINUSE;
    394       1.5       jtc 	parsefile->strpush = sp->prev;
    395       1.5       jtc 	if (sp != &(parsefile->basestrpush))
    396       1.5       jtc 		ckfree(sp);
    397       1.5       jtc 	INTON;
    398       1.5       jtc }
    399       1.1       cgd 
    400       1.1       cgd /*
    401       1.1       cgd  * Set the input to take input from a file.  If push is set, push the
    402       1.1       cgd  * old input onto the stack first.
    403       1.1       cgd  */
    404       1.1       cgd 
    405       1.1       cgd void
    406      1.37  christos setinputfile(const char *fname, int push)
    407      1.11       cgd {
    408      1.40    rillig 	unsigned char magic[4];
    409       1.1       cgd 	int fd;
    410       1.1       cgd 	int fd2;
    411      1.51       kre 	struct stat sb;
    412       1.1       cgd 
    413  1.56.2.1       snj 	CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no"));
    414  1.56.2.1       snj 
    415       1.1       cgd 	INTOFF;
    416       1.1       cgd 	if ((fd = open(fname, O_RDONLY)) < 0)
    417       1.1       cgd 		error("Can't open %s", fname);
    418      1.40    rillig 
    419      1.40    rillig 	/* Since the message "Syntax error: "(" unexpected" is not very
    420      1.40    rillig 	 * helpful, we check if the file starts with the ELF magic to
    421      1.40    rillig 	 * avoid that message. The first lseek tries to make sure that
    422      1.40    rillig 	 * we can later rewind the file.
    423      1.40    rillig 	 */
    424      1.51       kre 	if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) &&
    425      1.51       kre 	    lseek(fd, 0, SEEK_SET) == 0) {
    426      1.40    rillig 		if (read(fd, magic, 4) == 4) {
    427      1.50       kre 			if (memcmp(magic, "\177ELF", 4) == 0) {
    428      1.50       kre 				(void)close(fd);
    429      1.40    rillig 				error("Cannot execute ELF binary %s", fname);
    430      1.50       kre 			}
    431      1.40    rillig 		}
    432      1.50       kre 		if (lseek(fd, 0, SEEK_SET) != 0) {
    433      1.50       kre 			(void)close(fd);
    434      1.40    rillig 			error("Cannot rewind the file %s", fname);
    435      1.50       kre 		}
    436      1.40    rillig 	}
    437      1.40    rillig 
    438      1.49  christos 	fd2 = to_upper_fd(fd);	/* closes fd, returns higher equiv */
    439      1.49  christos 	if (fd2 == fd) {
    440      1.49  christos 		(void) close(fd);
    441      1.49  christos 		error("Out of file descriptors");
    442       1.1       cgd 	}
    443      1.49  christos 
    444      1.49  christos 	setinputfd(fd2, push);
    445       1.1       cgd 	INTON;
    446       1.1       cgd }
    447       1.1       cgd 
    448      1.52       kre /*
    449      1.52       kre  * When a shell fd needs to be altered (when the user wants to use
    450      1.52       kre  * the same fd - rare, but happens - we need to locate the ref to
    451      1.52       kre  * the fd, and update it.  This happens via a callback.
    452      1.52       kre  * This is the callback func for fd's used for shell input
    453      1.52       kre  */
    454      1.52       kre static void
    455      1.52       kre input_fd_swap(int from, int to)
    456      1.52       kre {
    457      1.52       kre 	struct parsefile *pf;
    458      1.52       kre 
    459      1.52       kre 	pf = parsefile;
    460      1.52       kre 	while (pf != NULL) {		/* don't need to stop at basepf */
    461      1.52       kre 		if (pf->fd == from)
    462      1.52       kre 			pf->fd = to;
    463      1.52       kre 		pf = pf->prev;
    464      1.52       kre 	}
    465      1.52       kre }
    466       1.1       cgd 
    467       1.1       cgd /*
    468       1.1       cgd  * Like setinputfile, but takes an open file descriptor.  Call this with
    469       1.1       cgd  * interrupts off.
    470       1.1       cgd  */
    471       1.1       cgd 
    472       1.1       cgd void
    473      1.37  christos setinputfd(int fd, int push)
    474      1.11       cgd {
    475  1.56.2.1       snj 	VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no"));
    476  1.56.2.1       snj 
    477      1.52       kre 	register_sh_fd(fd, input_fd_swap);
    478      1.23   mycroft 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
    479  1.56.2.1       snj 	if (push)
    480       1.1       cgd 		pushfile();
    481       1.1       cgd 	if (parsefile->fd > 0)
    482      1.52       kre 		sh_close(parsefile->fd);
    483       1.1       cgd 	parsefile->fd = fd;
    484       1.1       cgd 	if (parsefile->buf == NULL)
    485       1.1       cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    486      1.17  christos 	parselleft = parsenleft = 0;
    487       1.1       cgd 	plinno = 1;
    488  1.56.2.1       snj 
    489  1.56.2.1       snj 	CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd,
    490  1.56.2.1       snj 	    push ? "" : "no"));
    491       1.1       cgd }
    492       1.1       cgd 
    493       1.1       cgd 
    494       1.1       cgd /*
    495       1.1       cgd  * Like setinputfile, but takes input from a string.
    496       1.1       cgd  */
    497       1.1       cgd 
    498       1.1       cgd void
    499  1.56.2.1       snj setinputstring(char *string, int push, int line1)
    500      1.37  christos {
    501      1.48  christos 
    502       1.1       cgd 	INTOFF;
    503  1.56.2.1       snj 	if (push)		/* XXX: always, as it happens */
    504       1.1       cgd 		pushfile();
    505       1.1       cgd 	parsenextc = string;
    506      1.17  christos 	parselleft = parsenleft = strlen(string);
    507  1.56.2.1       snj 	plinno = line1;
    508  1.56.2.1       snj 
    509  1.56.2.1       snj 	CTRACE(DBG_INPUT,
    510  1.56.2.1       snj 	    ("setinputstring(\"%.20s%s\" (%d), %push, @ %d)\n", string,
    511  1.56.2.1       snj 	    (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1));
    512       1.1       cgd 	INTON;
    513       1.1       cgd }
    514       1.1       cgd 
    515       1.1       cgd 
    516       1.1       cgd 
    517       1.1       cgd /*
    518       1.1       cgd  * To handle the "." command, a stack of input files is used.  Pushfile
    519       1.1       cgd  * adds a new entry to the stack and popfile restores the previous level.
    520       1.1       cgd  */
    521       1.1       cgd 
    522       1.1       cgd STATIC void
    523      1.37  christos pushfile(void)
    524      1.37  christos {
    525       1.1       cgd 	struct parsefile *pf;
    526       1.1       cgd 
    527  1.56.2.1       snj 	VTRACE(DBG_INPUT,
    528  1.56.2.1       snj 	    ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n",
    529  1.56.2.1       snj 	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
    530  1.56.2.1       snj 	    parsenleft, parsenextc, plinno));
    531  1.56.2.1       snj 
    532       1.1       cgd 	parsefile->nleft = parsenleft;
    533      1.17  christos 	parsefile->lleft = parselleft;
    534       1.1       cgd 	parsefile->nextc = parsenextc;
    535       1.1       cgd 	parsefile->linno = plinno;
    536       1.1       cgd 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    537       1.1       cgd 	pf->prev = parsefile;
    538       1.1       cgd 	pf->fd = -1;
    539       1.5       jtc 	pf->strpush = NULL;
    540       1.5       jtc 	pf->basestrpush.prev = NULL;
    541  1.56.2.1       snj 	pf->buf = NULL;
    542       1.1       cgd 	parsefile = pf;
    543       1.1       cgd }
    544       1.1       cgd 
    545       1.1       cgd 
    546       1.1       cgd void
    547      1.37  christos popfile(void)
    548      1.37  christos {
    549       1.1       cgd 	struct parsefile *pf = parsefile;
    550       1.1       cgd 
    551       1.1       cgd 	INTOFF;
    552       1.1       cgd 	if (pf->fd >= 0)
    553      1.52       kre 		sh_close(pf->fd);
    554       1.1       cgd 	if (pf->buf)
    555       1.1       cgd 		ckfree(pf->buf);
    556       1.5       jtc 	while (pf->strpush)
    557       1.5       jtc 		popstring();
    558       1.1       cgd 	parsefile = pf->prev;
    559       1.1       cgd 	ckfree(pf);
    560       1.1       cgd 	parsenleft = parsefile->nleft;
    561      1.17  christos 	parselleft = parsefile->lleft;
    562       1.1       cgd 	parsenextc = parsefile->nextc;
    563  1.56.2.1       snj 
    564  1.56.2.1       snj 	VTRACE(DBG_INPUT,
    565  1.56.2.1       snj 	    ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n",
    566  1.56.2.1       snj 	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
    567  1.56.2.1       snj 	    parsenleft, parsenextc, plinno, parsefile->linno));
    568  1.56.2.1       snj 
    569       1.1       cgd 	plinno = parsefile->linno;
    570       1.1       cgd 	INTON;
    571       1.1       cgd }
    572       1.1       cgd 
    573  1.56.2.1       snj /*
    574  1.56.2.1       snj  * Return current file (to go back to it later using popfilesupto()).
    575  1.56.2.1       snj  */
    576  1.56.2.1       snj 
    577  1.56.2.1       snj struct parsefile *
    578  1.56.2.1       snj getcurrentfile(void)
    579  1.56.2.1       snj {
    580  1.56.2.1       snj 	return parsefile;
    581  1.56.2.1       snj }
    582  1.56.2.1       snj 
    583  1.56.2.1       snj 
    584  1.56.2.1       snj /*
    585  1.56.2.1       snj  * Pop files until the given file is on top again. Useful for regular
    586  1.56.2.1       snj  * builtins that read shell commands from files or strings.
    587  1.56.2.1       snj  * If the given file is not an active file, an error is raised.
    588  1.56.2.1       snj  */
    589  1.56.2.1       snj 
    590  1.56.2.1       snj void
    591  1.56.2.1       snj popfilesupto(struct parsefile *file)
    592  1.56.2.1       snj {
    593  1.56.2.1       snj 	while (parsefile != file && parsefile != &basepf)
    594  1.56.2.1       snj 		popfile();
    595  1.56.2.1       snj 	if (parsefile != file)
    596  1.56.2.1       snj 		error("popfilesupto() misused");
    597  1.56.2.1       snj }
    598  1.56.2.1       snj 
    599       1.1       cgd 
    600       1.1       cgd /*
    601       1.1       cgd  * Return to top level.
    602       1.1       cgd  */
    603       1.1       cgd 
    604       1.1       cgd void
    605      1.37  christos popallfiles(void)
    606      1.37  christos {
    607       1.1       cgd 	while (parsefile != &basepf)
    608       1.1       cgd 		popfile();
    609       1.1       cgd }
    610       1.1       cgd 
    611       1.1       cgd 
    612       1.1       cgd 
    613       1.1       cgd /*
    614       1.1       cgd  * Close the file(s) that the shell is reading commands from.  Called
    615       1.1       cgd  * after a fork is done.
    616      1.36  christos  *
    617      1.36  christos  * Takes one arg, vfork, which tells it to not modify its global vars
    618      1.36  christos  * as it is still running in the parent.
    619      1.38       dsl  *
    620      1.38       dsl  * This code is (probably) unnecessary as the 'close on exec' flag is
    621      1.38       dsl  * set and should be enough.  In the vfork case it is definitely wrong
    622      1.38       dsl  * to close the fds as another fork() may be done later to feed data
    623      1.38       dsl  * from a 'here' document into a pipe and we don't want to close the
    624      1.38       dsl  * pipe!
    625       1.1       cgd  */
    626       1.1       cgd 
    627       1.1       cgd void
    628      1.37  christos closescript(int vforked)
    629      1.37  christos {
    630      1.38       dsl 	if (vforked)
    631      1.36  christos 		return;
    632       1.1       cgd 	popallfiles();
    633       1.1       cgd 	if (parsefile->fd > 0) {
    634      1.52       kre 		sh_close(parsefile->fd);
    635       1.1       cgd 		parsefile->fd = 0;
    636       1.1       cgd 	}
    637       1.1       cgd }
    638