Home | History | Annotate | Line # | Download | only in sh
input.c revision 1.23
      1  1.23   mycroft /*	$NetBSD: input.c,v 1.23 1997/03/13 20:07:49 mycroft 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.13       cgd #if 0
     41  1.16  christos static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
     42  1.13       cgd #else
     43  1.23   mycroft static char rcsid[] = "$NetBSD: input.c,v 1.23 1997/03/13 20:07:49 mycroft Exp $";
     44  1.13       cgd #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47  1.14  christos #include <stdio.h>	/* defines BUFSIZ */
     48  1.14  christos #include <fcntl.h>
     49  1.14  christos #include <errno.h>
     50  1.14  christos #include <unistd.h>
     51  1.14  christos #include <stdlib.h>
     52  1.15       cgd #include <string.h>
     53  1.14  christos 
     54   1.1       cgd /*
     55   1.1       cgd  * This file implements the input routines used by the parser.
     56   1.1       cgd  */
     57   1.1       cgd 
     58   1.6       jtc #include "shell.h"
     59  1.14  christos #include "redir.h"
     60   1.1       cgd #include "syntax.h"
     61   1.1       cgd #include "input.h"
     62   1.1       cgd #include "output.h"
     63   1.5       jtc #include "options.h"
     64   1.1       cgd #include "memalloc.h"
     65   1.1       cgd #include "error.h"
     66   1.5       jtc #include "alias.h"
     67   1.5       jtc #include "parser.h"
     68   1.5       jtc #include "myhistedit.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.5       jtc 	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.1       cgd 	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.1       cgd MKINIT int parsenleft;		/* copy of parsefile->nleft */
    102  1.17  christos MKINIT int parselleft;		/* copy of parsefile->lleft */
    103   1.1       cgd char *parsenextc;		/* copy of parsefile->nextc */
    104   1.1       cgd MKINIT struct parsefile basepf;	/* top level input file */
    105   1.1       cgd 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.5       jtc EditLine *el;			/* cookie for editline package */
    111   1.1       cgd 
    112  1.14  christos STATIC void pushfile __P((void));
    113  1.17  christos static int pread __P((void));
    114   1.1       cgd 
    115   1.1       cgd #ifdef mkinit
    116   1.1       cgd INCLUDE "input.h"
    117   1.1       cgd INCLUDE "error.h"
    118   1.1       cgd 
    119   1.1       cgd INIT {
    120   1.1       cgd 	extern char basebuf[];
    121   1.1       cgd 
    122   1.1       cgd 	basepf.nextc = basepf.buf = basebuf;
    123   1.1       cgd }
    124   1.1       cgd 
    125   1.1       cgd RESET {
    126   1.1       cgd 	if (exception != EXSHELLPROC)
    127  1.17  christos 		parselleft = parsenleft = 0;	/* clear input buffer */
    128   1.1       cgd 	popallfiles();
    129   1.1       cgd }
    130   1.1       cgd 
    131   1.1       cgd SHELLPROC {
    132   1.1       cgd 	popallfiles();
    133   1.1       cgd }
    134   1.1       cgd #endif
    135   1.1       cgd 
    136   1.1       cgd 
    137   1.1       cgd /*
    138   1.1       cgd  * Read a line from the script.
    139   1.1       cgd  */
    140   1.1       cgd 
    141   1.1       cgd char *
    142   1.1       cgd pfgets(line, len)
    143   1.1       cgd 	char *line;
    144  1.11       cgd 	int len;
    145  1.11       cgd {
    146  1.22       tls 	char *p = line;
    147   1.1       cgd 	int nleft = len;
    148   1.1       cgd 	int c;
    149   1.1       cgd 
    150   1.1       cgd 	while (--nleft > 0) {
    151   1.1       cgd 		c = pgetc_macro();
    152   1.1       cgd 		if (c == PEOF) {
    153   1.1       cgd 			if (p == line)
    154   1.1       cgd 				return NULL;
    155   1.1       cgd 			break;
    156   1.1       cgd 		}
    157   1.1       cgd 		*p++ = c;
    158   1.1       cgd 		if (c == '\n')
    159   1.1       cgd 			break;
    160   1.1       cgd 	}
    161   1.1       cgd 	*p = '\0';
    162   1.1       cgd 	return line;
    163   1.1       cgd }
    164   1.1       cgd 
    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.17  christos pgetc()
    174  1.17  christos {
    175   1.1       cgd 	return pgetc_macro();
    176   1.1       cgd }
    177   1.1       cgd 
    178   1.1       cgd 
    179  1.17  christos static int
    180  1.20  christos pread()
    181  1.17  christos {
    182  1.17  christos 	int nr;
    183  1.17  christos 	parsenextc = parsefile->buf;
    184  1.17  christos 
    185  1.17  christos retry:
    186  1.17  christos 	if (parsefile->fd == 0 && el) {
    187  1.17  christos 		const char *rl_cp;
    188  1.17  christos 
    189  1.18  christos 		rl_cp = el_gets(el, &nr);
    190  1.17  christos 		if (rl_cp == NULL)
    191  1.17  christos 			nr = 0;
    192  1.17  christos 		else {
    193  1.17  christos 			/* XXX - BUFSIZE should redesign so not necessary */
    194  1.17  christos 			(void) strcpy(parsenextc, rl_cp);
    195  1.17  christos 		}
    196  1.17  christos 	} else {
    197  1.17  christos 		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
    198  1.17  christos 	}
    199  1.17  christos 
    200  1.17  christos 	if (nr <= 0) {
    201  1.17  christos                 if (nr < 0) {
    202  1.17  christos                         if (errno == EINTR)
    203  1.17  christos                                 goto retry;
    204  1.17  christos                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    205  1.17  christos                                 int flags = fcntl(0, F_GETFL, 0);
    206  1.17  christos                                 if (flags >= 0 && flags & O_NONBLOCK) {
    207  1.17  christos                                         flags &=~ O_NONBLOCK;
    208  1.17  christos                                         if (fcntl(0, F_SETFL, flags) >= 0) {
    209  1.17  christos 						out2str("sh: turning off NDELAY mode\n");
    210  1.17  christos                                                 goto retry;
    211  1.17  christos                                         }
    212  1.17  christos                                 }
    213  1.17  christos                         }
    214  1.17  christos                 }
    215  1.17  christos                 nr = -1;
    216  1.17  christos 	}
    217  1.17  christos 	return nr;
    218  1.17  christos }
    219  1.17  christos 
    220   1.1       cgd /*
    221   1.1       cgd  * Refill the input buffer and return the next input character:
    222   1.1       cgd  *
    223   1.5       jtc  * 1) If a string was pushed back on the input, pop it;
    224   1.1       cgd  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
    225   1.1       cgd  *    from a string so we can't refill the buffer, return EOF.
    226  1.17  christos  * 3) If the is more stuff in this buffer, use it else call read to fill it.
    227  1.17  christos  * 4) Process input up to the next newline, deleting nul characters.
    228   1.1       cgd  */
    229   1.1       cgd 
    230   1.1       cgd int
    231  1.17  christos preadbuffer()
    232  1.17  christos {
    233  1.17  christos 	char *p, *q;
    234  1.17  christos 	int more;
    235  1.17  christos 	int something;
    236   1.5       jtc 	extern EditLine *el;
    237  1.17  christos 	char savec;
    238   1.1       cgd 
    239   1.5       jtc 	if (parsefile->strpush) {
    240   1.5       jtc 		popstring();
    241   1.1       cgd 		if (--parsenleft >= 0)
    242   1.5       jtc 			return (*parsenextc++);
    243   1.1       cgd 	}
    244   1.1       cgd 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    245   1.1       cgd 		return PEOF;
    246   1.1       cgd 	flushout(&output);
    247   1.1       cgd 	flushout(&errout);
    248   1.5       jtc 
    249  1.17  christos again:
    250  1.17  christos 	if (parselleft <= 0) {
    251  1.17  christos 		if ((parselleft = pread()) == -1) {
    252  1.17  christos 			parselleft = parsenleft = EOF_NLEFT;
    253  1.17  christos 			return PEOF;
    254   1.5       jtc 		}
    255  1.17  christos 	}
    256   1.5       jtc 
    257  1.17  christos 	q = p = parsenextc;
    258   1.1       cgd 
    259   1.1       cgd 	/* delete nul characters */
    260   1.5       jtc 	something = 0;
    261  1.17  christos 	for (more = 1; more;) {
    262  1.17  christos 		switch (*p) {
    263  1.17  christos 		case '\0':
    264  1.17  christos 			p++;	/* Skip nul */
    265  1.17  christos 			goto check;
    266  1.20  christos 
    267  1.17  christos 		case '\t':
    268  1.17  christos 		case ' ':
    269   1.1       cgd 			break;
    270  1.17  christos 
    271  1.17  christos 		case '\n':
    272  1.17  christos 			parsenleft = q - parsenextc;
    273  1.17  christos 			more = 0; /* Stop processing here */
    274  1.17  christos 			break;
    275  1.17  christos 
    276  1.17  christos 		default:
    277   1.5       jtc 			something = 1;
    278  1.17  christos 			break;
    279  1.17  christos 		}
    280  1.17  christos 
    281  1.17  christos 		*q++ = *p++;
    282  1.17  christos check:
    283  1.17  christos 		if (--parselleft <= 0) {
    284  1.17  christos 			parsenleft = q - parsenextc - 1;
    285  1.17  christos 			if (parsenleft < 0)
    286  1.17  christos 				goto again;
    287  1.17  christos 			*q = '\0';
    288  1.17  christos 			more = 0;
    289   1.5       jtc 		}
    290   1.1       cgd 	}
    291  1.17  christos 
    292  1.17  christos 	savec = *q;
    293   1.5       jtc 	*q = '\0';
    294   1.5       jtc 
    295  1.19  christos #ifndef NO_HISTORY
    296   1.5       jtc 	if (parsefile->fd == 0 && hist && something) {
    297   1.5       jtc 		INTOFF;
    298  1.17  christos 		history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc);
    299   1.5       jtc 		INTON;
    300   1.5       jtc 	}
    301  1.19  christos #endif
    302  1.17  christos 
    303   1.5       jtc 	if (vflag) {
    304  1.17  christos 		out2str(parsenextc);
    305   1.5       jtc 		flushout(out2);
    306   1.5       jtc 	}
    307  1.17  christos 
    308  1.17  christos 	*q = savec;
    309  1.17  christos 
    310   1.1       cgd 	return *parsenextc++;
    311   1.1       cgd }
    312   1.1       cgd 
    313   1.1       cgd /*
    314   1.1       cgd  * Undo the last call to pgetc.  Only one character may be pushed back.
    315   1.1       cgd  * PEOF may be pushed back.
    316   1.1       cgd  */
    317   1.1       cgd 
    318   1.1       cgd void
    319   1.1       cgd pungetc() {
    320   1.1       cgd 	parsenleft++;
    321   1.1       cgd 	parsenextc--;
    322   1.1       cgd }
    323   1.1       cgd 
    324   1.1       cgd /*
    325   1.5       jtc  * Push a string back onto the input at this current parsefile level.
    326   1.5       jtc  * We handle aliases this way.
    327   1.1       cgd  */
    328   1.1       cgd void
    329   1.5       jtc pushstring(s, len, ap)
    330   1.5       jtc 	char *s;
    331   1.5       jtc 	int len;
    332   1.5       jtc 	void *ap;
    333   1.1       cgd 	{
    334   1.5       jtc 	struct strpush *sp;
    335   1.5       jtc 
    336   1.5       jtc 	INTOFF;
    337   1.5       jtc /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
    338   1.5       jtc 	if (parsefile->strpush) {
    339   1.5       jtc 		sp = ckmalloc(sizeof (struct strpush));
    340   1.5       jtc 		sp->prev = parsefile->strpush;
    341   1.5       jtc 		parsefile->strpush = sp;
    342   1.5       jtc 	} else
    343   1.5       jtc 		sp = parsefile->strpush = &(parsefile->basestrpush);
    344   1.5       jtc 	sp->prevstring = parsenextc;
    345   1.5       jtc 	sp->prevnleft = parsenleft;
    346  1.17  christos 	sp->prevlleft = parselleft;
    347   1.5       jtc 	sp->ap = (struct alias *)ap;
    348   1.5       jtc 	if (ap)
    349   1.5       jtc 		((struct alias *)ap)->flag |= ALIASINUSE;
    350   1.5       jtc 	parsenextc = s;
    351   1.5       jtc 	parsenleft = len;
    352   1.5       jtc 	INTON;
    353   1.1       cgd }
    354   1.1       cgd 
    355  1.11       cgd void
    356   1.5       jtc popstring()
    357   1.5       jtc {
    358   1.5       jtc 	struct strpush *sp = parsefile->strpush;
    359   1.1       cgd 
    360   1.5       jtc 	INTOFF;
    361   1.5       jtc 	parsenextc = sp->prevstring;
    362   1.5       jtc 	parsenleft = sp->prevnleft;
    363  1.17  christos 	parselleft = sp->prevlleft;
    364   1.5       jtc /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
    365   1.5       jtc 	if (sp->ap)
    366   1.5       jtc 		sp->ap->flag &= ~ALIASINUSE;
    367   1.5       jtc 	parsefile->strpush = sp->prev;
    368   1.5       jtc 	if (sp != &(parsefile->basestrpush))
    369   1.5       jtc 		ckfree(sp);
    370   1.5       jtc 	INTON;
    371   1.5       jtc }
    372   1.1       cgd 
    373   1.1       cgd /*
    374   1.1       cgd  * Set the input to take input from a file.  If push is set, push the
    375   1.1       cgd  * old input onto the stack first.
    376   1.1       cgd  */
    377   1.1       cgd 
    378   1.1       cgd void
    379   1.1       cgd setinputfile(fname, push)
    380   1.1       cgd 	char *fname;
    381  1.11       cgd 	int push;
    382  1.11       cgd {
    383   1.1       cgd 	int fd;
    384   1.1       cgd 	int fd2;
    385   1.1       cgd 
    386   1.1       cgd 	INTOFF;
    387   1.1       cgd 	if ((fd = open(fname, O_RDONLY)) < 0)
    388   1.1       cgd 		error("Can't open %s", fname);
    389   1.1       cgd 	if (fd < 10) {
    390   1.1       cgd 		fd2 = copyfd(fd, 10);
    391   1.1       cgd 		close(fd);
    392   1.1       cgd 		if (fd2 < 0)
    393   1.1       cgd 			error("Out of file descriptors");
    394   1.1       cgd 		fd = fd2;
    395   1.1       cgd 	}
    396   1.1       cgd 	setinputfd(fd, push);
    397   1.1       cgd 	INTON;
    398   1.1       cgd }
    399   1.1       cgd 
    400   1.1       cgd 
    401   1.1       cgd /*
    402   1.1       cgd  * Like setinputfile, but takes an open file descriptor.  Call this with
    403   1.1       cgd  * interrupts off.
    404   1.1       cgd  */
    405   1.1       cgd 
    406   1.1       cgd void
    407  1.11       cgd setinputfd(fd, push)
    408  1.14  christos 	int fd, push;
    409  1.11       cgd {
    410  1.23   mycroft 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
    411   1.1       cgd 	if (push) {
    412   1.1       cgd 		pushfile();
    413   1.1       cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    414   1.1       cgd 	}
    415   1.1       cgd 	if (parsefile->fd > 0)
    416   1.1       cgd 		close(parsefile->fd);
    417   1.1       cgd 	parsefile->fd = fd;
    418   1.1       cgd 	if (parsefile->buf == NULL)
    419   1.1       cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    420  1.17  christos 	parselleft = parsenleft = 0;
    421   1.1       cgd 	plinno = 1;
    422   1.1       cgd }
    423   1.1       cgd 
    424   1.1       cgd 
    425   1.1       cgd /*
    426   1.1       cgd  * Like setinputfile, but takes input from a string.
    427   1.1       cgd  */
    428   1.1       cgd 
    429   1.1       cgd void
    430   1.1       cgd setinputstring(string, push)
    431   1.1       cgd 	char *string;
    432  1.11       cgd 	int push;
    433  1.14  christos 	{
    434   1.1       cgd 	INTOFF;
    435   1.1       cgd 	if (push)
    436   1.1       cgd 		pushfile();
    437   1.1       cgd 	parsenextc = string;
    438  1.17  christos 	parselleft = parsenleft = strlen(string);
    439   1.1       cgd 	parsefile->buf = NULL;
    440   1.1       cgd 	plinno = 1;
    441   1.1       cgd 	INTON;
    442   1.1       cgd }
    443   1.1       cgd 
    444   1.1       cgd 
    445   1.1       cgd 
    446   1.1       cgd /*
    447   1.1       cgd  * To handle the "." command, a stack of input files is used.  Pushfile
    448   1.1       cgd  * adds a new entry to the stack and popfile restores the previous level.
    449   1.1       cgd  */
    450   1.1       cgd 
    451   1.1       cgd STATIC void
    452   1.1       cgd pushfile() {
    453   1.1       cgd 	struct parsefile *pf;
    454   1.1       cgd 
    455   1.1       cgd 	parsefile->nleft = parsenleft;
    456  1.17  christos 	parsefile->lleft = parselleft;
    457   1.1       cgd 	parsefile->nextc = parsenextc;
    458   1.1       cgd 	parsefile->linno = plinno;
    459   1.1       cgd 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    460   1.1       cgd 	pf->prev = parsefile;
    461   1.1       cgd 	pf->fd = -1;
    462   1.5       jtc 	pf->strpush = NULL;
    463   1.5       jtc 	pf->basestrpush.prev = NULL;
    464   1.1       cgd 	parsefile = pf;
    465   1.1       cgd }
    466   1.1       cgd 
    467   1.1       cgd 
    468   1.1       cgd void
    469   1.1       cgd popfile() {
    470   1.1       cgd 	struct parsefile *pf = parsefile;
    471   1.1       cgd 
    472   1.1       cgd 	INTOFF;
    473   1.1       cgd 	if (pf->fd >= 0)
    474   1.1       cgd 		close(pf->fd);
    475   1.1       cgd 	if (pf->buf)
    476   1.1       cgd 		ckfree(pf->buf);
    477   1.5       jtc 	while (pf->strpush)
    478   1.5       jtc 		popstring();
    479   1.1       cgd 	parsefile = pf->prev;
    480   1.1       cgd 	ckfree(pf);
    481   1.1       cgd 	parsenleft = parsefile->nleft;
    482  1.17  christos 	parselleft = parsefile->lleft;
    483   1.1       cgd 	parsenextc = parsefile->nextc;
    484   1.1       cgd 	plinno = parsefile->linno;
    485   1.1       cgd 	INTON;
    486   1.1       cgd }
    487   1.1       cgd 
    488   1.1       cgd 
    489   1.1       cgd /*
    490   1.1       cgd  * Return to top level.
    491   1.1       cgd  */
    492   1.1       cgd 
    493   1.1       cgd void
    494   1.1       cgd popallfiles() {
    495   1.1       cgd 	while (parsefile != &basepf)
    496   1.1       cgd 		popfile();
    497   1.1       cgd }
    498   1.1       cgd 
    499   1.1       cgd 
    500   1.1       cgd 
    501   1.1       cgd /*
    502   1.1       cgd  * Close the file(s) that the shell is reading commands from.  Called
    503   1.1       cgd  * after a fork is done.
    504   1.1       cgd  */
    505   1.1       cgd 
    506   1.1       cgd void
    507   1.1       cgd closescript() {
    508   1.1       cgd 	popallfiles();
    509   1.1       cgd 	if (parsefile->fd > 0) {
    510   1.1       cgd 		close(parsefile->fd);
    511   1.1       cgd 		parsefile->fd = 0;
    512   1.1       cgd 	}
    513   1.1       cgd }
    514