xref: /freebsd/bin/sh/input.c (revision 5134c3f7990c353d2442ddd8a9a1d30b80099e43)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <stdio.h>	/* defines BUFSIZ */
46aa9caaf6SPeter Wemm #include <fcntl.h>
47aa9caaf6SPeter Wemm #include <errno.h>
48aa9caaf6SPeter Wemm #include <unistd.h>
49aa9caaf6SPeter Wemm #include <stdlib.h>
50aa9caaf6SPeter Wemm #include <string.h>
51aa9caaf6SPeter Wemm 
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes  * This file implements the input routines used by the parser.
544b88c807SRodney W. Grimes  */
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes #include "shell.h"
57aa9caaf6SPeter Wemm #include "redir.h"
584b88c807SRodney W. Grimes #include "syntax.h"
594b88c807SRodney W. Grimes #include "input.h"
604b88c807SRodney W. Grimes #include "output.h"
614b88c807SRodney W. Grimes #include "options.h"
624b88c807SRodney W. Grimes #include "memalloc.h"
634b88c807SRodney W. Grimes #include "error.h"
644b88c807SRodney W. Grimes #include "alias.h"
654b88c807SRodney W. Grimes #include "parser.h"
664b88c807SRodney W. Grimes #include "myhistedit.h"
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes MKINIT
714b88c807SRodney W. Grimes struct strpush {
724b88c807SRodney W. Grimes 	struct strpush *prev;	/* preceding string on stack */
734b88c807SRodney W. Grimes 	char *prevstring;
744b88c807SRodney W. Grimes 	int prevnleft;
758f08f33eSPeter Wemm 	int prevlleft;
764b88c807SRodney W. Grimes 	struct alias *ap;	/* if push was associated with an alias */
774b88c807SRodney W. Grimes };
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes /*
804b88c807SRodney W. Grimes  * The parsefile structure pointed to by the global variable parsefile
814b88c807SRodney W. Grimes  * contains information about the current file being read.
824b88c807SRodney W. Grimes  */
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes MKINIT
854b88c807SRodney W. Grimes struct parsefile {
864b88c807SRodney W. Grimes 	struct parsefile *prev;	/* preceding file on stack */
874b88c807SRodney W. Grimes 	int linno;		/* current line */
884b88c807SRodney W. Grimes 	int fd;			/* file descriptor (or -1 if string) */
89ab0a2172SSteve Price 	int nleft;		/* number of chars left in this line */
90ab0a2172SSteve Price 	int lleft;		/* number of lines left in this buffer */
914b88c807SRodney W. Grimes 	char *nextc;		/* next char in buffer */
924b88c807SRodney W. Grimes 	char *buf;		/* input buffer */
934b88c807SRodney W. Grimes 	struct strpush *strpush; /* for pushing strings at this level */
944b88c807SRodney W. Grimes 	struct strpush basestrpush; /* so pushing one is fast */
954b88c807SRodney W. Grimes };
964b88c807SRodney W. Grimes 
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes int plinno = 1;			/* input line number */
994b88c807SRodney W. Grimes MKINIT int parsenleft;		/* copy of parsefile->nleft */
1008f08f33eSPeter Wemm MKINIT int parselleft;		/* copy of parsefile->lleft */
1014b88c807SRodney W. Grimes char *parsenextc;		/* copy of parsefile->nextc */
1024b88c807SRodney W. Grimes MKINIT struct parsefile basepf;	/* top level input file */
1034b88c807SRodney W. Grimes char basebuf[BUFSIZ];		/* buffer for top level input file */
1044b88c807SRodney W. Grimes struct parsefile *parsefile = &basepf;	/* current input file */
1054b88c807SRodney W. Grimes int init_editline = 0;		/* editline library initialized? */
1064b88c807SRodney W. Grimes int whichprompt;		/* 1 == PS1, 2 == PS2 */
1074b88c807SRodney W. Grimes 
1084b88c807SRodney W. Grimes EditLine *el;			/* cookie for editline package */
1094b88c807SRodney W. Grimes 
1105134c3f7SWarner Losh STATIC void pushfile(void);
1115134c3f7SWarner Losh static int preadfd(void);
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes #ifdef mkinit
1144b88c807SRodney W. Grimes INCLUDE "input.h"
1154b88c807SRodney W. Grimes INCLUDE "error.h"
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes INIT {
1184b88c807SRodney W. Grimes 	extern char basebuf[];
1194b88c807SRodney W. Grimes 
1204b88c807SRodney W. Grimes 	basepf.nextc = basepf.buf = basebuf;
1214b88c807SRodney W. Grimes }
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes RESET {
1244b88c807SRodney W. Grimes 	if (exception != EXSHELLPROC)
1258f08f33eSPeter Wemm 		parselleft = parsenleft = 0;	/* clear input buffer */
1264b88c807SRodney W. Grimes 	popallfiles();
1274b88c807SRodney W. Grimes }
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes SHELLPROC {
1304b88c807SRodney W. Grimes 	popallfiles();
1314b88c807SRodney W. Grimes }
1324b88c807SRodney W. Grimes #endif
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes 
1354b88c807SRodney W. Grimes /*
1364b88c807SRodney W. Grimes  * Read a line from the script.
1374b88c807SRodney W. Grimes  */
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes char *
1405134c3f7SWarner Losh pfgets(char *line, int len)
1414b88c807SRodney W. Grimes {
1424ca7fe3bSSteve Price 	char *p = line;
1434b88c807SRodney W. Grimes 	int nleft = len;
1444b88c807SRodney W. Grimes 	int c;
1454b88c807SRodney W. Grimes 
1464b88c807SRodney W. Grimes 	while (--nleft > 0) {
1474b88c807SRodney W. Grimes 		c = pgetc_macro();
1484b88c807SRodney W. Grimes 		if (c == PEOF) {
1494b88c807SRodney W. Grimes 			if (p == line)
1504b88c807SRodney W. Grimes 				return NULL;
1514b88c807SRodney W. Grimes 			break;
1524b88c807SRodney W. Grimes 		}
1534b88c807SRodney W. Grimes 		*p++ = c;
1544b88c807SRodney W. Grimes 		if (c == '\n')
1554b88c807SRodney W. Grimes 			break;
1564b88c807SRodney W. Grimes 	}
1574b88c807SRodney W. Grimes 	*p = '\0';
1584b88c807SRodney W. Grimes 	return line;
1594b88c807SRodney W. Grimes }
1604b88c807SRodney W. Grimes 
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes 
1634b88c807SRodney W. Grimes /*
1644b88c807SRodney W. Grimes  * Read a character from the script, returning PEOF on end of file.
1654b88c807SRodney W. Grimes  * Nul characters in the input are silently discarded.
1664b88c807SRodney W. Grimes  */
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes int
1695134c3f7SWarner Losh pgetc(void)
170ab0a2172SSteve Price {
1714b88c807SRodney W. Grimes 	return pgetc_macro();
1724b88c807SRodney W. Grimes }
1734b88c807SRodney W. Grimes 
174ab0a2172SSteve Price 
1758f08f33eSPeter Wemm static int
1765134c3f7SWarner Losh preadfd(void)
1778f08f33eSPeter Wemm {
1788f08f33eSPeter Wemm 	int nr;
1798f08f33eSPeter Wemm 	parsenextc = parsefile->buf;
180ab0a2172SSteve Price 
1814b88c807SRodney W. Grimes retry:
1824ca7fe3bSSteve Price #ifndef NO_HISTORY
1834b88c807SRodney W. Grimes 	if (parsefile->fd == 0 && el) {
1844b88c807SRodney W. Grimes 		const char *rl_cp;
1854b88c807SRodney W. Grimes 
1868f08f33eSPeter Wemm 		rl_cp = el_gets(el, &nr);
1878f08f33eSPeter Wemm 		if (rl_cp == NULL)
1888f08f33eSPeter Wemm 			nr = 0;
1898f08f33eSPeter Wemm 		else {
1908f08f33eSPeter Wemm 			/* XXX - BUFSIZE should redesign so not necessary */
1914417f629SPeter Wemm 			(void) strcpy(parsenextc, rl_cp);
1924b88c807SRodney W. Grimes 		}
1934ca7fe3bSSteve Price 	} else
1944ca7fe3bSSteve Price #endif
1958f08f33eSPeter Wemm 		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
1968f08f33eSPeter Wemm 
1978f08f33eSPeter Wemm 	if (nr <= 0) {
1988f08f33eSPeter Wemm                 if (nr < 0) {
1994b88c807SRodney W. Grimes                         if (errno == EINTR)
2004b88c807SRodney W. Grimes                                 goto retry;
2014b88c807SRodney W. Grimes                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
2024b88c807SRodney W. Grimes                                 int flags = fcntl(0, F_GETFL, 0);
2034b88c807SRodney W. Grimes                                 if (flags >= 0 && flags & O_NONBLOCK) {
2044b88c807SRodney W. Grimes                                         flags &=~ O_NONBLOCK;
2054b88c807SRodney W. Grimes                                         if (fcntl(0, F_SETFL, flags) >= 0) {
2064b88c807SRodney W. Grimes 						out2str("sh: turning off NDELAY mode\n");
2074b88c807SRodney W. Grimes                                                 goto retry;
2084b88c807SRodney W. Grimes                                         }
2094b88c807SRodney W. Grimes                                 }
2104b88c807SRodney W. Grimes                         }
2114b88c807SRodney W. Grimes                 }
2128f08f33eSPeter Wemm                 nr = -1;
2138f08f33eSPeter Wemm 	}
2148f08f33eSPeter Wemm 	return nr;
2158f08f33eSPeter Wemm }
2168f08f33eSPeter Wemm 
2178f08f33eSPeter Wemm /*
2188f08f33eSPeter Wemm  * Refill the input buffer and return the next input character:
2198f08f33eSPeter Wemm  *
2208f08f33eSPeter Wemm  * 1) If a string was pushed back on the input, pop it;
2218f08f33eSPeter Wemm  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
2228f08f33eSPeter Wemm  *    from a string so we can't refill the buffer, return EOF.
223ab0a2172SSteve Price  * 3) If there is more in this buffer, use it else call read to fill it.
224ab0a2172SSteve Price  * 4) Process input up to the next newline, deleting nul characters.
2258f08f33eSPeter Wemm  */
2268f08f33eSPeter Wemm 
2278f08f33eSPeter Wemm int
2285134c3f7SWarner Losh preadbuffer(void)
229ab0a2172SSteve Price {
2308f08f33eSPeter Wemm 	char *p, *q;
2318f08f33eSPeter Wemm 	int more;
2328f08f33eSPeter Wemm 	int something;
2338f08f33eSPeter Wemm 	char savec;
2348f08f33eSPeter Wemm 
2358f08f33eSPeter Wemm 	if (parsefile->strpush) {
2368f08f33eSPeter Wemm 		popstring();
2378f08f33eSPeter Wemm 		if (--parsenleft >= 0)
2388f08f33eSPeter Wemm 			return (*parsenextc++);
2398f08f33eSPeter Wemm 	}
2408f08f33eSPeter Wemm 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
2418f08f33eSPeter Wemm 		return PEOF;
2428f08f33eSPeter Wemm 	flushout(&output);
2438f08f33eSPeter Wemm 	flushout(&errout);
2448f08f33eSPeter Wemm 
2458f08f33eSPeter Wemm again:
2468f08f33eSPeter Wemm 	if (parselleft <= 0) {
2474ca7fe3bSSteve Price 		if ((parselleft = preadfd()) == -1) {
2488f08f33eSPeter Wemm 			parselleft = parsenleft = EOF_NLEFT;
2494b88c807SRodney W. Grimes 			return PEOF;
2504b88c807SRodney W. Grimes 		}
2518f08f33eSPeter Wemm 	}
2528f08f33eSPeter Wemm 
2538f08f33eSPeter Wemm 	q = p = parsenextc;
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes 	/* delete nul characters */
2564b88c807SRodney W. Grimes 	something = 0;
2578f08f33eSPeter Wemm 	for (more = 1; more;) {
2588f08f33eSPeter Wemm 		switch (*p) {
2598f08f33eSPeter Wemm 		case '\0':
2608f08f33eSPeter Wemm 			p++;	/* Skip nul */
2618f08f33eSPeter Wemm 			goto check;
2624b88c807SRodney W. Grimes 
2638f08f33eSPeter Wemm 		case '\t':
2648f08f33eSPeter Wemm 		case ' ':
2658f08f33eSPeter Wemm 			break;
2668f08f33eSPeter Wemm 
2678f08f33eSPeter Wemm 		case '\n':
2688f08f33eSPeter Wemm 			parsenleft = q - parsenextc;
2698f08f33eSPeter Wemm 			more = 0; /* Stop processing here */
2708f08f33eSPeter Wemm 			break;
2718f08f33eSPeter Wemm 
2728f08f33eSPeter Wemm 		default:
2738f08f33eSPeter Wemm 			something = 1;
2748f08f33eSPeter Wemm 			break;
2758f08f33eSPeter Wemm 		}
2768f08f33eSPeter Wemm 
2778f08f33eSPeter Wemm 		*q++ = *p++;
2788f08f33eSPeter Wemm check:
2798f08f33eSPeter Wemm 		if (--parselleft <= 0) {
2808f08f33eSPeter Wemm 			parsenleft = q - parsenextc - 1;
2818f08f33eSPeter Wemm 			if (parsenleft < 0)
2828f08f33eSPeter Wemm 				goto again;
2838f08f33eSPeter Wemm 			*q = '\0';
2848f08f33eSPeter Wemm 			more = 0;
2858f08f33eSPeter Wemm 		}
2868f08f33eSPeter Wemm 	}
2878f08f33eSPeter Wemm 
2888f08f33eSPeter Wemm 	savec = *q;
2898f08f33eSPeter Wemm 	*q = '\0';
2908f08f33eSPeter Wemm 
2914417f629SPeter Wemm #ifndef NO_HISTORY
2924b88c807SRodney W. Grimes 	if (parsefile->fd == 0 && hist && something) {
293757eeda0SDavid E. O'Brien 		HistEvent he;
2944b88c807SRodney W. Grimes 		INTOFF;
295757eeda0SDavid E. O'Brien 		history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
296757eeda0SDavid E. O'Brien 		    parsenextc);
2974b88c807SRodney W. Grimes 		INTON;
2984b88c807SRodney W. Grimes 	}
2994417f629SPeter Wemm #endif
3008f08f33eSPeter Wemm 
3014b88c807SRodney W. Grimes 	if (vflag) {
3028f08f33eSPeter Wemm 		out2str(parsenextc);
3034b88c807SRodney W. Grimes 		flushout(out2);
3044b88c807SRodney W. Grimes 	}
3058f08f33eSPeter Wemm 
3068f08f33eSPeter Wemm 	*q = savec;
3078f08f33eSPeter Wemm 
3084b88c807SRodney W. Grimes 	return *parsenextc++;
3094b88c807SRodney W. Grimes }
3104b88c807SRodney W. Grimes 
3114b88c807SRodney W. Grimes /*
3124b88c807SRodney W. Grimes  * Undo the last call to pgetc.  Only one character may be pushed back.
3134b88c807SRodney W. Grimes  * PEOF may be pushed back.
3144b88c807SRodney W. Grimes  */
3154b88c807SRodney W. Grimes 
3164b88c807SRodney W. Grimes void
3175134c3f7SWarner Losh pungetc(void)
3185134c3f7SWarner Losh {
3194b88c807SRodney W. Grimes 	parsenleft++;
3204b88c807SRodney W. Grimes 	parsenextc--;
3214b88c807SRodney W. Grimes }
3224b88c807SRodney W. Grimes 
3234b88c807SRodney W. Grimes /*
3244b88c807SRodney W. Grimes  * Push a string back onto the input at this current parsefile level.
3254b88c807SRodney W. Grimes  * We handle aliases this way.
3264b88c807SRodney W. Grimes  */
3274b88c807SRodney W. Grimes void
3285134c3f7SWarner Losh pushstring(char *s, int len, void *ap)
3294b88c807SRodney W. Grimes {
3304b88c807SRodney W. Grimes 	struct strpush *sp;
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes 	INTOFF;
3334b88c807SRodney W. Grimes /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
3344b88c807SRodney W. Grimes 	if (parsefile->strpush) {
3354b88c807SRodney W. Grimes 		sp = ckmalloc(sizeof (struct strpush));
3364b88c807SRodney W. Grimes 		sp->prev = parsefile->strpush;
3374b88c807SRodney W. Grimes 		parsefile->strpush = sp;
3384b88c807SRodney W. Grimes 	} else
3394b88c807SRodney W. Grimes 		sp = parsefile->strpush = &(parsefile->basestrpush);
3404b88c807SRodney W. Grimes 	sp->prevstring = parsenextc;
3414b88c807SRodney W. Grimes 	sp->prevnleft = parsenleft;
3428f08f33eSPeter Wemm 	sp->prevlleft = parselleft;
3434b88c807SRodney W. Grimes 	sp->ap = (struct alias *)ap;
3444b88c807SRodney W. Grimes 	if (ap)
3454b88c807SRodney W. Grimes 		((struct alias *)ap)->flag |= ALIASINUSE;
3464b88c807SRodney W. Grimes 	parsenextc = s;
3474b88c807SRodney W. Grimes 	parsenleft = len;
3484b88c807SRodney W. Grimes 	INTON;
3494b88c807SRodney W. Grimes }
3504b88c807SRodney W. Grimes 
351aa9caaf6SPeter Wemm void
3525134c3f7SWarner Losh popstring(void)
3534b88c807SRodney W. Grimes {
3544b88c807SRodney W. Grimes 	struct strpush *sp = parsefile->strpush;
3554b88c807SRodney W. Grimes 
3564b88c807SRodney W. Grimes 	INTOFF;
3574b88c807SRodney W. Grimes 	parsenextc = sp->prevstring;
3584b88c807SRodney W. Grimes 	parsenleft = sp->prevnleft;
3598f08f33eSPeter Wemm 	parselleft = sp->prevlleft;
3604b88c807SRodney W. Grimes /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
3614b88c807SRodney W. Grimes 	if (sp->ap)
3624b88c807SRodney W. Grimes 		sp->ap->flag &= ~ALIASINUSE;
3634b88c807SRodney W. Grimes 	parsefile->strpush = sp->prev;
3644b88c807SRodney W. Grimes 	if (sp != &(parsefile->basestrpush))
3654b88c807SRodney W. Grimes 		ckfree(sp);
3664b88c807SRodney W. Grimes 	INTON;
3674b88c807SRodney W. Grimes }
3684b88c807SRodney W. Grimes 
3694b88c807SRodney W. Grimes /*
3704b88c807SRodney W. Grimes  * Set the input to take input from a file.  If push is set, push the
3714b88c807SRodney W. Grimes  * old input onto the stack first.
3724b88c807SRodney W. Grimes  */
3734b88c807SRodney W. Grimes 
3744b88c807SRodney W. Grimes void
3755134c3f7SWarner Losh setinputfile(char *fname, int push)
3764b88c807SRodney W. Grimes {
3774b88c807SRodney W. Grimes 	int fd;
3784b88c807SRodney W. Grimes 	int fd2;
3794b88c807SRodney W. Grimes 
3804b88c807SRodney W. Grimes 	INTOFF;
3814b88c807SRodney W. Grimes 	if ((fd = open(fname, O_RDONLY)) < 0)
3826c48b6cfSMartin Cracauer 		error("Can't open %s: %s", fname, strerror(errno));
3834b88c807SRodney W. Grimes 	if (fd < 10) {
3844b88c807SRodney W. Grimes 		fd2 = copyfd(fd, 10);
3854b88c807SRodney W. Grimes 		close(fd);
3864b88c807SRodney W. Grimes 		if (fd2 < 0)
3874b88c807SRodney W. Grimes 			error("Out of file descriptors");
3884b88c807SRodney W. Grimes 		fd = fd2;
3894b88c807SRodney W. Grimes 	}
3904b88c807SRodney W. Grimes 	setinputfd(fd, push);
3914b88c807SRodney W. Grimes 	INTON;
3924b88c807SRodney W. Grimes }
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 
3954b88c807SRodney W. Grimes /*
3964b88c807SRodney W. Grimes  * Like setinputfile, but takes an open file descriptor.  Call this with
3974b88c807SRodney W. Grimes  * interrupts off.
3984b88c807SRodney W. Grimes  */
3994b88c807SRodney W. Grimes 
4004b88c807SRodney W. Grimes void
4015134c3f7SWarner Losh setinputfd(int fd, int push)
402aa9caaf6SPeter Wemm {
4034ca7fe3bSSteve Price 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
4044b88c807SRodney W. Grimes 	if (push) {
4054b88c807SRodney W. Grimes 		pushfile();
4064b88c807SRodney W. Grimes 		parsefile->buf = ckmalloc(BUFSIZ);
4074b88c807SRodney W. Grimes 	}
4084b88c807SRodney W. Grimes 	if (parsefile->fd > 0)
4094b88c807SRodney W. Grimes 		close(parsefile->fd);
4104b88c807SRodney W. Grimes 	parsefile->fd = fd;
4114b88c807SRodney W. Grimes 	if (parsefile->buf == NULL)
4124b88c807SRodney W. Grimes 		parsefile->buf = ckmalloc(BUFSIZ);
4138f08f33eSPeter Wemm 	parselleft = parsenleft = 0;
4144b88c807SRodney W. Grimes 	plinno = 1;
4154b88c807SRodney W. Grimes }
4164b88c807SRodney W. Grimes 
4174b88c807SRodney W. Grimes 
4184b88c807SRodney W. Grimes /*
4194b88c807SRodney W. Grimes  * Like setinputfile, but takes input from a string.
4204b88c807SRodney W. Grimes  */
4214b88c807SRodney W. Grimes 
4224b88c807SRodney W. Grimes void
4235134c3f7SWarner Losh setinputstring(char *string, int push)
4244b88c807SRodney W. Grimes {
4254b88c807SRodney W. Grimes 	INTOFF;
4264b88c807SRodney W. Grimes 	if (push)
4274b88c807SRodney W. Grimes 		pushfile();
4284b88c807SRodney W. Grimes 	parsenextc = string;
4298f08f33eSPeter Wemm 	parselleft = parsenleft = strlen(string);
4304b88c807SRodney W. Grimes 	parsefile->buf = NULL;
4314b88c807SRodney W. Grimes 	plinno = 1;
4324b88c807SRodney W. Grimes 	INTON;
4334b88c807SRodney W. Grimes }
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes 
4374b88c807SRodney W. Grimes /*
4384b88c807SRodney W. Grimes  * To handle the "." command, a stack of input files is used.  Pushfile
4394b88c807SRodney W. Grimes  * adds a new entry to the stack and popfile restores the previous level.
4404b88c807SRodney W. Grimes  */
4414b88c807SRodney W. Grimes 
4424b88c807SRodney W. Grimes STATIC void
4435134c3f7SWarner Losh pushfile(void)
4445134c3f7SWarner Losh {
4454b88c807SRodney W. Grimes 	struct parsefile *pf;
4464b88c807SRodney W. Grimes 
4474b88c807SRodney W. Grimes 	parsefile->nleft = parsenleft;
4488f08f33eSPeter Wemm 	parsefile->lleft = parselleft;
4494b88c807SRodney W. Grimes 	parsefile->nextc = parsenextc;
4504b88c807SRodney W. Grimes 	parsefile->linno = plinno;
4514b88c807SRodney W. Grimes 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
4524b88c807SRodney W. Grimes 	pf->prev = parsefile;
4534b88c807SRodney W. Grimes 	pf->fd = -1;
4544b88c807SRodney W. Grimes 	pf->strpush = NULL;
4554b88c807SRodney W. Grimes 	pf->basestrpush.prev = NULL;
4564b88c807SRodney W. Grimes 	parsefile = pf;
4574b88c807SRodney W. Grimes }
4584b88c807SRodney W. Grimes 
4594b88c807SRodney W. Grimes 
4604b88c807SRodney W. Grimes void
4615134c3f7SWarner Losh popfile(void)
4625134c3f7SWarner Losh {
4634b88c807SRodney W. Grimes 	struct parsefile *pf = parsefile;
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 	INTOFF;
4664b88c807SRodney W. Grimes 	if (pf->fd >= 0)
4674b88c807SRodney W. Grimes 		close(pf->fd);
4684b88c807SRodney W. Grimes 	if (pf->buf)
4694b88c807SRodney W. Grimes 		ckfree(pf->buf);
4704b88c807SRodney W. Grimes 	while (pf->strpush)
4714b88c807SRodney W. Grimes 		popstring();
4724b88c807SRodney W. Grimes 	parsefile = pf->prev;
4734b88c807SRodney W. Grimes 	ckfree(pf);
4744b88c807SRodney W. Grimes 	parsenleft = parsefile->nleft;
4758f08f33eSPeter Wemm 	parselleft = parsefile->lleft;
4764b88c807SRodney W. Grimes 	parsenextc = parsefile->nextc;
4774b88c807SRodney W. Grimes 	plinno = parsefile->linno;
4784b88c807SRodney W. Grimes 	INTON;
4794b88c807SRodney W. Grimes }
4804b88c807SRodney W. Grimes 
4814b88c807SRodney W. Grimes 
4824b88c807SRodney W. Grimes /*
4834b88c807SRodney W. Grimes  * Return to top level.
4844b88c807SRodney W. Grimes  */
4854b88c807SRodney W. Grimes 
4864b88c807SRodney W. Grimes void
4875134c3f7SWarner Losh popallfiles(void)
4885134c3f7SWarner Losh {
4894b88c807SRodney W. Grimes 	while (parsefile != &basepf)
4904b88c807SRodney W. Grimes 		popfile();
4914b88c807SRodney W. Grimes }
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes 
4944b88c807SRodney W. Grimes 
4954b88c807SRodney W. Grimes /*
4964b88c807SRodney W. Grimes  * Close the file(s) that the shell is reading commands from.  Called
4974b88c807SRodney W. Grimes  * after a fork is done.
4984b88c807SRodney W. Grimes  */
4994b88c807SRodney W. Grimes 
5004b88c807SRodney W. Grimes void
5015134c3f7SWarner Losh closescript(void)
5025134c3f7SWarner Losh {
5034b88c807SRodney W. Grimes 	popallfiles();
5044b88c807SRodney W. Grimes 	if (parsefile->fd > 0) {
5054b88c807SRodney W. Grimes 		close(parsefile->fd);
5064b88c807SRodney W. Grimes 		parsefile->fd = 0;
5074b88c807SRodney W. Grimes 	}
5084b88c807SRodney W. Grimes }
509