Home | History | Annotate | Line # | Download | only in sh
input.c revision 1.10
      1   1.1      cgd /*-
      2   1.5      jtc  * Copyright (c) 1991, 1993
      3   1.5      jtc  *	The Regents of the University of California.  All rights reserved.
      4   1.1      cgd  *
      5   1.1      cgd  * This code is derived from software contributed to Berkeley by
      6   1.1      cgd  * Kenneth Almquist.
      7   1.1      cgd  *
      8   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1      cgd  * modification, are permitted provided that the following conditions
     10   1.1      cgd  * are met:
     11   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1      cgd  *    must display the following acknowledgement:
     18   1.1      cgd  *	This product includes software developed by the University of
     19   1.1      cgd  *	California, Berkeley and its contributors.
     20   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1      cgd  *    may be used to endorse or promote products derived from this software
     22   1.1      cgd  *    without specific prior written permission.
     23   1.1      cgd  *
     24   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1      cgd  * SUCH DAMAGE.
     35   1.1      cgd  */
     36   1.1      cgd 
     37   1.1      cgd #ifndef lint
     38  1.10  mycroft /*static char sccsid[] = "from: @(#)input.c	8.1 (Berkeley) 5/31/93";*/
     39  1.10  mycroft static char *rcsid = "$Id: input.c,v 1.10 1994/06/11 16:11:59 mycroft Exp $";
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd /*
     43   1.1      cgd  * This file implements the input routines used by the parser.
     44   1.1      cgd  */
     45   1.1      cgd 
     46   1.1      cgd #include <stdio.h>	/* defines BUFSIZ */
     47   1.1      cgd #include <fcntl.h>
     48   1.1      cgd #include <errno.h>
     49   1.6      jtc #include <unistd.h>
     50   1.6      jtc #include "shell.h"
     51   1.1      cgd #include "syntax.h"
     52   1.1      cgd #include "input.h"
     53   1.1      cgd #include "output.h"
     54   1.5      jtc #include "options.h"
     55   1.1      cgd #include "memalloc.h"
     56   1.1      cgd #include "error.h"
     57   1.5      jtc #include "alias.h"
     58   1.5      jtc #include "parser.h"
     59   1.8      cgd #ifndef NO_HISTORY
     60   1.5      jtc #include "myhistedit.h"
     61   1.8      cgd #endif
     62   1.1      cgd 
     63   1.1      cgd #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
     64   1.1      cgd 
     65   1.5      jtc MKINIT
     66   1.5      jtc struct strpush {
     67   1.5      jtc 	struct strpush *prev;	/* preceding string on stack */
     68   1.5      jtc 	char *prevstring;
     69   1.5      jtc 	int prevnleft;
     70   1.5      jtc 	struct alias *ap;	/* if push was associated with an alias */
     71   1.5      jtc };
     72   1.1      cgd 
     73   1.1      cgd /*
     74   1.1      cgd  * The parsefile structure pointed to by the global variable parsefile
     75   1.1      cgd  * contains information about the current file being read.
     76   1.1      cgd  */
     77   1.1      cgd 
     78   1.1      cgd MKINIT
     79   1.1      cgd struct parsefile {
     80   1.5      jtc 	struct parsefile *prev;	/* preceding file on stack */
     81   1.1      cgd 	int linno;		/* current line */
     82   1.1      cgd 	int fd;			/* file descriptor (or -1 if string) */
     83   1.1      cgd 	int nleft;		/* number of chars left in buffer */
     84   1.1      cgd 	char *nextc;		/* next char in buffer */
     85   1.1      cgd 	char *buf;		/* input buffer */
     86   1.5      jtc 	struct strpush *strpush; /* for pushing strings at this level */
     87   1.5      jtc 	struct strpush basestrpush; /* so pushing one is fast */
     88   1.1      cgd };
     89   1.1      cgd 
     90   1.1      cgd 
     91   1.1      cgd int plinno = 1;			/* input line number */
     92   1.1      cgd MKINIT int parsenleft;		/* copy of parsefile->nleft */
     93   1.1      cgd char *parsenextc;		/* copy of parsefile->nextc */
     94   1.1      cgd MKINIT struct parsefile basepf;	/* top level input file */
     95   1.1      cgd char basebuf[BUFSIZ];		/* buffer for top level input file */
     96   1.1      cgd struct parsefile *parsefile = &basepf;	/* current input file */
     97   1.1      cgd char *pushedstring;		/* copy of parsenextc when text pushed back */
     98   1.1      cgd int pushednleft;		/* copy of parsenleft when text pushed back */
     99   1.5      jtc int init_editline = 0;		/* editline library initialized? */
    100   1.5      jtc int whichprompt;		/* 1 == PS1, 2 == PS2 */
    101   1.5      jtc 
    102   1.8      cgd #ifndef NO_HISTORY
    103   1.5      jtc EditLine *el;			/* cookie for editline package */
    104   1.8      cgd #endif
    105   1.1      cgd 
    106   1.1      cgd #ifdef __STDC__
    107   1.1      cgd STATIC void pushfile(void);
    108   1.1      cgd #else
    109   1.1      cgd STATIC void pushfile();
    110   1.1      cgd #endif
    111   1.1      cgd 
    112   1.1      cgd 
    113   1.1      cgd 
    114   1.1      cgd #ifdef mkinit
    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 	extern char basebuf[];
    120   1.1      cgd 
    121   1.1      cgd 	basepf.nextc = basepf.buf = basebuf;
    122   1.1      cgd }
    123   1.1      cgd 
    124   1.1      cgd RESET {
    125   1.1      cgd 	if (exception != EXSHELLPROC)
    126   1.1      cgd 		parsenleft = 0;            /* clear input buffer */
    127   1.1      cgd 	popallfiles();
    128   1.1      cgd }
    129   1.1      cgd 
    130   1.1      cgd SHELLPROC {
    131   1.1      cgd 	popallfiles();
    132   1.1      cgd }
    133   1.1      cgd #endif
    134   1.1      cgd 
    135   1.1      cgd 
    136   1.1      cgd /*
    137   1.1      cgd  * Read a line from the script.
    138   1.1      cgd  */
    139   1.1      cgd 
    140   1.1      cgd char *
    141   1.1      cgd pfgets(line, len)
    142   1.1      cgd 	char *line;
    143   1.1      cgd 	{
    144   1.1      cgd 	register char *p = line;
    145   1.1      cgd 	int nleft = len;
    146   1.1      cgd 	int c;
    147   1.1      cgd 
    148   1.1      cgd 	while (--nleft > 0) {
    149   1.1      cgd 		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.1      cgd 		if (c == '\n')
    157   1.1      cgd 			break;
    158   1.1      cgd 	}
    159   1.1      cgd 	*p = '\0';
    160   1.1      cgd 	return line;
    161   1.1      cgd }
    162   1.1      cgd 
    163   1.1      cgd 
    164   1.1      cgd 
    165   1.1      cgd /*
    166   1.1      cgd  * Read a character from the script, returning PEOF on end of file.
    167   1.1      cgd  * Nul characters in the input are silently discarded.
    168   1.1      cgd  */
    169   1.1      cgd 
    170   1.1      cgd int
    171   1.1      cgd pgetc() {
    172   1.1      cgd 	return pgetc_macro();
    173   1.1      cgd }
    174   1.1      cgd 
    175   1.1      cgd 
    176   1.1      cgd /*
    177   1.1      cgd  * Refill the input buffer and return the next input character:
    178   1.1      cgd  *
    179   1.5      jtc  * 1) If a string was pushed back on the input, pop it;
    180   1.1      cgd  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
    181   1.1      cgd  *    from a string so we can't refill the buffer, return EOF.
    182   1.1      cgd  * 3) Call read to read in the characters.
    183   1.1      cgd  * 4) Delete all nul characters from the buffer.
    184   1.1      cgd  */
    185   1.1      cgd 
    186   1.1      cgd int
    187   1.1      cgd preadbuffer() {
    188   1.1      cgd 	register char *p, *q;
    189   1.1      cgd 	register int i;
    190   1.5      jtc 	register int something;
    191   1.7      cgd #ifndef NO_HISTORY
    192   1.5      jtc 	extern EditLine *el;
    193   1.7      cgd #endif
    194   1.1      cgd 
    195   1.5      jtc 	if (parsefile->strpush) {
    196   1.5      jtc 		popstring();
    197   1.1      cgd 		if (--parsenleft >= 0)
    198   1.5      jtc 			return (*parsenextc++);
    199   1.1      cgd 	}
    200   1.1      cgd 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
    201   1.1      cgd 		return PEOF;
    202   1.1      cgd 	flushout(&output);
    203   1.1      cgd 	flushout(&errout);
    204   1.1      cgd retry:
    205   1.9      jtc 	p = parsenextc = parsefile->buf;
    206   1.7      cgd #ifndef NO_HISTORY
    207   1.5      jtc 	if (parsefile->fd == 0 && el) {
    208   1.5      jtc 		const char *rl_cp;
    209   1.5      jtc 		int len;
    210   1.5      jtc 
    211   1.5      jtc 		rl_cp = el_gets(el, &len);
    212   1.5      jtc 		if (rl_cp == NULL) {
    213   1.5      jtc 			i = 0;
    214   1.5      jtc 			goto eof;
    215   1.5      jtc 		}
    216   1.5      jtc 		strcpy(p, rl_cp);  /* XXX - BUFSIZE should redesign so not necessary */
    217   1.5      jtc 		i = len;
    218   1.5      jtc 
    219   1.5      jtc 	} else {
    220   1.7      cgd #endif
    221   1.5      jtc regular_read:
    222   1.5      jtc 		i = read(parsefile->fd, p, BUFSIZ - 1);
    223   1.7      cgd #ifndef NO_HISTORY
    224   1.5      jtc 	}
    225   1.7      cgd #endif
    226   1.5      jtc eof:
    227   1.1      cgd 	if (i <= 0) {
    228   1.1      cgd                 if (i < 0) {
    229   1.1      cgd                         if (errno == EINTR)
    230   1.1      cgd                                 goto retry;
    231   1.1      cgd                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    232   1.1      cgd                                 int flags = fcntl(0, F_GETFL, 0);
    233   1.1      cgd                                 if (flags >= 0 && flags & O_NONBLOCK) {
    234   1.1      cgd                                         flags &=~ O_NONBLOCK;
    235   1.1      cgd                                         if (fcntl(0, F_SETFL, flags) >= 0) {
    236   1.1      cgd 						out2str("sh: turning off NDELAY mode\n");
    237   1.1      cgd                                                 goto retry;
    238   1.1      cgd                                         }
    239   1.1      cgd                                 }
    240   1.1      cgd                         }
    241   1.1      cgd                 }
    242   1.1      cgd                 parsenleft = EOF_NLEFT;
    243   1.1      cgd                 return PEOF;
    244   1.1      cgd 	}
    245   1.5      jtc 	parsenleft = i - 1;	/* we're returning one char in this call */
    246   1.1      cgd 
    247   1.1      cgd 	/* delete nul characters */
    248   1.5      jtc 	something = 0;
    249   1.1      cgd 	for (;;) {
    250   1.5      jtc 		if (*p == '\0')
    251   1.1      cgd 			break;
    252   1.5      jtc 		if (*p != ' ' && *p != '\t' && *p != '\n')
    253   1.5      jtc 			something = 1;
    254   1.5      jtc 		p++;
    255   1.5      jtc 		if (--i <= 0) {
    256   1.5      jtc 			*p = '\0';
    257   1.5      jtc 			goto done;		/* no nul characters */
    258   1.5      jtc 		}
    259   1.1      cgd 	}
    260   1.5      jtc 	/*
    261   1.5      jtc 	 * remove nuls
    262   1.5      jtc 	 */
    263   1.5      jtc 	q = p++;
    264   1.1      cgd 	while (--i > 0) {
    265   1.1      cgd 		if (*p != '\0')
    266   1.1      cgd 			*q++ = *p;
    267   1.1      cgd 		p++;
    268   1.1      cgd 	}
    269   1.5      jtc 	*q = '\0';
    270   1.1      cgd 	if (q == parsefile->buf)
    271   1.1      cgd 		goto retry;			/* buffer contained nothing but nuls */
    272   1.1      cgd 	parsenleft = q - parsefile->buf - 1;
    273   1.5      jtc 
    274   1.5      jtc done:
    275   1.7      cgd #ifndef NO_HISTORY
    276   1.5      jtc 	if (parsefile->fd == 0 && hist && something) {
    277   1.5      jtc 		INTOFF;
    278   1.5      jtc 		history(hist, whichprompt == 1 ? H_ENTER : H_ADD,
    279   1.5      jtc 			   parsefile->buf);
    280   1.5      jtc 		INTON;
    281   1.5      jtc 	}
    282   1.7      cgd #endif
    283   1.5      jtc 	if (vflag) {
    284   1.5      jtc 		/*
    285   1.5      jtc 		 * This isn't right.  Most shells coordinate it with
    286   1.5      jtc 		 * reading a line at a time.  I honestly don't know if its
    287   1.5      jtc 		 * worth it.
    288   1.5      jtc 		 */
    289   1.5      jtc 		i = parsenleft + 1;
    290   1.5      jtc 		p = parsefile->buf;
    291   1.5      jtc 		for (; i--; p++)
    292   1.5      jtc 			out2c(*p)
    293   1.5      jtc 		flushout(out2);
    294   1.5      jtc 	}
    295   1.1      cgd 	return *parsenextc++;
    296   1.1      cgd }
    297   1.1      cgd 
    298   1.1      cgd /*
    299   1.1      cgd  * Undo the last call to pgetc.  Only one character may be pushed back.
    300   1.1      cgd  * PEOF may be pushed back.
    301   1.1      cgd  */
    302   1.1      cgd 
    303   1.1      cgd void
    304   1.1      cgd pungetc() {
    305   1.1      cgd 	parsenleft++;
    306   1.1      cgd 	parsenextc--;
    307   1.1      cgd }
    308   1.1      cgd 
    309   1.1      cgd /*
    310   1.5      jtc  * Push a string back onto the input at this current parsefile level.
    311   1.5      jtc  * We handle aliases this way.
    312   1.1      cgd  */
    313   1.1      cgd void
    314   1.5      jtc pushstring(s, len, ap)
    315   1.5      jtc 	char *s;
    316   1.5      jtc 	int len;
    317   1.5      jtc 	void *ap;
    318   1.1      cgd 	{
    319   1.5      jtc 	struct strpush *sp;
    320   1.5      jtc 
    321   1.5      jtc 	INTOFF;
    322   1.5      jtc /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
    323   1.5      jtc 	if (parsefile->strpush) {
    324   1.5      jtc 		sp = ckmalloc(sizeof (struct strpush));
    325   1.5      jtc 		sp->prev = parsefile->strpush;
    326   1.5      jtc 		parsefile->strpush = sp;
    327   1.5      jtc 	} else
    328   1.5      jtc 		sp = parsefile->strpush = &(parsefile->basestrpush);
    329   1.5      jtc 	sp->prevstring = parsenextc;
    330   1.5      jtc 	sp->prevnleft = parsenleft;
    331   1.5      jtc 	sp->ap = (struct alias *)ap;
    332   1.5      jtc 	if (ap)
    333   1.5      jtc 		((struct alias *)ap)->flag |= ALIASINUSE;
    334   1.5      jtc 	parsenextc = s;
    335   1.5      jtc 	parsenleft = len;
    336   1.5      jtc 	INTON;
    337   1.1      cgd }
    338   1.1      cgd 
    339   1.5      jtc popstring()
    340   1.5      jtc {
    341   1.5      jtc 	struct strpush *sp = parsefile->strpush;
    342   1.1      cgd 
    343   1.5      jtc 	INTOFF;
    344   1.5      jtc 	parsenextc = sp->prevstring;
    345   1.5      jtc 	parsenleft = sp->prevnleft;
    346   1.5      jtc /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
    347   1.5      jtc 	if (sp->ap)
    348   1.5      jtc 		sp->ap->flag &= ~ALIASINUSE;
    349   1.5      jtc 	parsefile->strpush = sp->prev;
    350   1.5      jtc 	if (sp != &(parsefile->basestrpush))
    351   1.5      jtc 		ckfree(sp);
    352   1.5      jtc 	INTON;
    353   1.5      jtc }
    354   1.1      cgd 
    355   1.1      cgd /*
    356   1.1      cgd  * Set the input to take input from a file.  If push is set, push the
    357   1.1      cgd  * old input onto the stack first.
    358   1.1      cgd  */
    359   1.1      cgd 
    360   1.1      cgd void
    361   1.1      cgd setinputfile(fname, push)
    362   1.1      cgd 	char *fname;
    363   1.1      cgd 	{
    364   1.1      cgd 	int fd;
    365   1.1      cgd 	int fd2;
    366   1.1      cgd 
    367   1.1      cgd 	INTOFF;
    368   1.1      cgd 	if ((fd = open(fname, O_RDONLY)) < 0)
    369   1.1      cgd 		error("Can't open %s", fname);
    370   1.1      cgd 	if (fd < 10) {
    371   1.1      cgd 		fd2 = copyfd(fd, 10);
    372   1.1      cgd 		close(fd);
    373   1.1      cgd 		if (fd2 < 0)
    374   1.1      cgd 			error("Out of file descriptors");
    375   1.1      cgd 		fd = fd2;
    376   1.1      cgd 	}
    377   1.1      cgd 	setinputfd(fd, push);
    378   1.1      cgd 	INTON;
    379   1.1      cgd }
    380   1.1      cgd 
    381   1.1      cgd 
    382   1.1      cgd /*
    383   1.1      cgd  * Like setinputfile, but takes an open file descriptor.  Call this with
    384   1.1      cgd  * interrupts off.
    385   1.1      cgd  */
    386   1.1      cgd 
    387   1.1      cgd void
    388   1.1      cgd setinputfd(fd, push) {
    389   1.1      cgd 	if (push) {
    390   1.1      cgd 		pushfile();
    391   1.1      cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    392   1.1      cgd 	}
    393   1.1      cgd 	if (parsefile->fd > 0)
    394   1.1      cgd 		close(parsefile->fd);
    395   1.1      cgd 	parsefile->fd = fd;
    396   1.1      cgd 	if (parsefile->buf == NULL)
    397   1.1      cgd 		parsefile->buf = ckmalloc(BUFSIZ);
    398   1.1      cgd 	parsenleft = 0;
    399   1.1      cgd 	plinno = 1;
    400   1.1      cgd }
    401   1.1      cgd 
    402   1.1      cgd 
    403   1.1      cgd /*
    404   1.1      cgd  * Like setinputfile, but takes input from a string.
    405   1.1      cgd  */
    406   1.1      cgd 
    407   1.1      cgd void
    408   1.1      cgd setinputstring(string, push)
    409   1.1      cgd 	char *string;
    410   1.1      cgd 	{
    411   1.1      cgd 	INTOFF;
    412   1.1      cgd 	if (push)
    413   1.1      cgd 		pushfile();
    414   1.1      cgd 	parsenextc = string;
    415   1.1      cgd 	parsenleft = strlen(string);
    416   1.1      cgd 	parsefile->buf = NULL;
    417   1.1      cgd 	plinno = 1;
    418   1.1      cgd 	INTON;
    419   1.1      cgd }
    420   1.1      cgd 
    421   1.1      cgd 
    422   1.1      cgd 
    423   1.1      cgd /*
    424   1.1      cgd  * To handle the "." command, a stack of input files is used.  Pushfile
    425   1.1      cgd  * adds a new entry to the stack and popfile restores the previous level.
    426   1.1      cgd  */
    427   1.1      cgd 
    428   1.1      cgd STATIC void
    429   1.1      cgd pushfile() {
    430   1.1      cgd 	struct parsefile *pf;
    431   1.1      cgd 
    432   1.1      cgd 	parsefile->nleft = parsenleft;
    433   1.1      cgd 	parsefile->nextc = parsenextc;
    434   1.1      cgd 	parsefile->linno = plinno;
    435   1.1      cgd 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    436   1.1      cgd 	pf->prev = parsefile;
    437   1.1      cgd 	pf->fd = -1;
    438   1.5      jtc 	pf->strpush = NULL;
    439   1.5      jtc 	pf->basestrpush.prev = NULL;
    440   1.1      cgd 	parsefile = pf;
    441   1.1      cgd }
    442   1.1      cgd 
    443   1.1      cgd 
    444   1.1      cgd void
    445   1.1      cgd popfile() {
    446   1.1      cgd 	struct parsefile *pf = parsefile;
    447   1.1      cgd 
    448   1.1      cgd 	INTOFF;
    449   1.1      cgd 	if (pf->fd >= 0)
    450   1.1      cgd 		close(pf->fd);
    451   1.1      cgd 	if (pf->buf)
    452   1.1      cgd 		ckfree(pf->buf);
    453   1.5      jtc 	while (pf->strpush)
    454   1.5      jtc 		popstring();
    455   1.1      cgd 	parsefile = pf->prev;
    456   1.1      cgd 	ckfree(pf);
    457   1.1      cgd 	parsenleft = parsefile->nleft;
    458   1.1      cgd 	parsenextc = parsefile->nextc;
    459   1.1      cgd 	plinno = parsefile->linno;
    460   1.1      cgd 	INTON;
    461   1.1      cgd }
    462   1.1      cgd 
    463   1.1      cgd 
    464   1.1      cgd /*
    465   1.1      cgd  * Return to top level.
    466   1.1      cgd  */
    467   1.1      cgd 
    468   1.1      cgd void
    469   1.1      cgd popallfiles() {
    470   1.1      cgd 	while (parsefile != &basepf)
    471   1.1      cgd 		popfile();
    472   1.1      cgd }
    473   1.1      cgd 
    474   1.1      cgd 
    475   1.1      cgd 
    476   1.1      cgd /*
    477   1.1      cgd  * Close the file(s) that the shell is reading commands from.  Called
    478   1.1      cgd  * after a fork is done.
    479   1.1      cgd  */
    480   1.1      cgd 
    481   1.1      cgd void
    482   1.1      cgd closescript() {
    483   1.1      cgd 	popallfiles();
    484   1.1      cgd 	if (parsefile->fd > 0) {
    485   1.1      cgd 		close(parsefile->fd);
    486   1.1      cgd 		parsefile->fd = 0;
    487   1.1      cgd 	}
    488   1.1      cgd }
    489