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.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes * without specific prior written permission.
194b88c807SRodney W. Grimes *
204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes * SUCH DAMAGE.
314b88c807SRodney W. Grimes */
324b88c807SRodney W. Grimes
33aa9caaf6SPeter Wemm #include <stdio.h> /* defines BUFSIZ */
34aa9caaf6SPeter Wemm #include <fcntl.h>
35aa9caaf6SPeter Wemm #include <errno.h>
36aa9caaf6SPeter Wemm #include <unistd.h>
37aa9caaf6SPeter Wemm #include <stdlib.h>
38aa9caaf6SPeter Wemm #include <string.h>
39aa9caaf6SPeter Wemm
404b88c807SRodney W. Grimes /*
414b88c807SRodney W. Grimes * This file implements the input routines used by the parser.
424b88c807SRodney W. Grimes */
434b88c807SRodney W. Grimes
444b88c807SRodney W. Grimes #include "shell.h"
45aa9caaf6SPeter Wemm #include "redir.h"
464b88c807SRodney W. Grimes #include "syntax.h"
474b88c807SRodney W. Grimes #include "input.h"
484b88c807SRodney W. Grimes #include "output.h"
494b88c807SRodney W. Grimes #include "options.h"
504b88c807SRodney W. Grimes #include "memalloc.h"
514b88c807SRodney W. Grimes #include "error.h"
524b88c807SRodney W. Grimes #include "alias.h"
534b88c807SRodney W. Grimes #include "parser.h"
54*971677d5SBryan Drewery #ifndef NO_HISTORY
554b88c807SRodney W. Grimes #include "myhistedit.h"
56*971677d5SBryan Drewery #endif
57be58cc48STim J. Robbins #include "trap.h"
584b88c807SRodney W. Grimes
594b88c807SRodney W. Grimes #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
604b88c807SRodney W. Grimes
614b88c807SRodney W. Grimes struct strpush {
624b88c807SRodney W. Grimes struct strpush *prev; /* preceding string on stack */
6346c6b52dSJilles Tjoelker const char *prevstring;
644b88c807SRodney W. Grimes int prevnleft;
658f08f33eSPeter Wemm int prevlleft;
664b88c807SRodney W. Grimes struct alias *ap; /* if push was associated with an alias */
674b88c807SRodney W. Grimes };
684b88c807SRodney W. Grimes
694b88c807SRodney W. Grimes /*
704b88c807SRodney W. Grimes * The parsefile structure pointed to by the global variable parsefile
714b88c807SRodney W. Grimes * contains information about the current file being read.
724b88c807SRodney W. Grimes */
734b88c807SRodney W. Grimes
744b88c807SRodney W. Grimes struct parsefile {
754b88c807SRodney W. Grimes struct parsefile *prev; /* preceding file on stack */
764b88c807SRodney W. Grimes int linno; /* current line */
774b88c807SRodney W. Grimes int fd; /* file descriptor (or -1 if string) */
78ab0a2172SSteve Price int nleft; /* number of chars left in this line */
79ab0a2172SSteve Price int lleft; /* number of lines left in this buffer */
8046c6b52dSJilles Tjoelker const char *nextc; /* next char in buffer */
814b88c807SRodney W. Grimes char *buf; /* input buffer */
824b88c807SRodney W. Grimes struct strpush *strpush; /* for pushing strings at this level */
834b88c807SRodney W. Grimes struct strpush basestrpush; /* so pushing one is fast */
844b88c807SRodney W. Grimes };
854b88c807SRodney W. Grimes
864b88c807SRodney W. Grimes
874b88c807SRodney W. Grimes int plinno = 1; /* input line number */
88384aedabSJilles Tjoelker int parsenleft; /* copy of parsefile->nleft */
890bdd3871SJilles Tjoelker static int parselleft; /* copy of parsefile->lleft */
9046c6b52dSJilles Tjoelker const char *parsenextc; /* copy of parsefile->nextc */
9160a6bf2aSJilles Tjoelker static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
9260a6bf2aSJilles Tjoelker static struct parsefile basepf = { /* top level input file */
9360a6bf2aSJilles Tjoelker .nextc = basebuf,
9460a6bf2aSJilles Tjoelker .buf = basebuf
9560a6bf2aSJilles Tjoelker };
96aa7b6f82SDavid E. O'Brien static struct parsefile *parsefile = &basepf; /* current input file */
974b88c807SRodney W. Grimes int whichprompt; /* 1 == PS1, 2 == PS2 */
984b88c807SRodney W. Grimes
9988328642SDavid E. O'Brien static void pushfile(void);
10088328642SDavid E. O'Brien static int preadfd(void);
101260fc3f4SJilles Tjoelker static void popstring(void);
1024b88c807SRodney W. Grimes
103338b821bSJilles Tjoelker void
resetinput(void)104338b821bSJilles Tjoelker resetinput(void)
105338b821bSJilles Tjoelker {
1063055b7c6SJilles Tjoelker popallfiles();
1078f08f33eSPeter Wemm parselleft = parsenleft = 0; /* clear input buffer */
1084b88c807SRodney W. Grimes }
1094b88c807SRodney W. Grimes
1104b88c807SRodney W. Grimes
1114b88c807SRodney W. Grimes
1124b88c807SRodney W. Grimes /*
1134b88c807SRodney W. Grimes * Read a character from the script, returning PEOF on end of file.
1144b88c807SRodney W. Grimes * Nul characters in the input are silently discarded.
1154b88c807SRodney W. Grimes */
1164b88c807SRodney W. Grimes
1174b88c807SRodney W. Grimes int
pgetc(void)1185134c3f7SWarner Losh pgetc(void)
119ab0a2172SSteve Price {
1204b88c807SRodney W. Grimes return pgetc_macro();
1214b88c807SRodney W. Grimes }
1224b88c807SRodney W. Grimes
123ab0a2172SSteve Price
12488328642SDavid E. O'Brien static int
preadfd(void)1255134c3f7SWarner Losh preadfd(void)
1268f08f33eSPeter Wemm {
1278f08f33eSPeter Wemm int nr;
1288f08f33eSPeter Wemm parsenextc = parsefile->buf;
129ab0a2172SSteve Price
1304b88c807SRodney W. Grimes retry:
1314ca7fe3bSSteve Price #ifndef NO_HISTORY
1324b88c807SRodney W. Grimes if (parsefile->fd == 0 && el) {
133896229d9SStefan Farfeleder static const char *rl_cp;
134896229d9SStefan Farfeleder static int el_len;
1354b88c807SRodney W. Grimes
136d78fdfdeSJilles Tjoelker if (rl_cp == NULL) {
137d78fdfdeSJilles Tjoelker el_resize(el);
138896229d9SStefan Farfeleder rl_cp = el_gets(el, &el_len);
139d78fdfdeSJilles Tjoelker }
1408f08f33eSPeter Wemm if (rl_cp == NULL)
14100c43e0cSPedro F. Giffuni nr = el_len == 0 ? 0 : -1;
1428f08f33eSPeter Wemm else {
143896229d9SStefan Farfeleder nr = el_len;
1447f40c1f8SJilles Tjoelker if (nr > BUFSIZ)
1457f40c1f8SJilles Tjoelker nr = BUFSIZ;
14646c6b52dSJilles Tjoelker memcpy(parsefile->buf, rl_cp, nr);
147896229d9SStefan Farfeleder if (nr != el_len) {
148896229d9SStefan Farfeleder el_len -= nr;
149896229d9SStefan Farfeleder rl_cp += nr;
150896229d9SStefan Farfeleder } else
151896229d9SStefan Farfeleder rl_cp = NULL;
1524b88c807SRodney W. Grimes }
1534ca7fe3bSSteve Price } else
1544ca7fe3bSSteve Price #endif
15546c6b52dSJilles Tjoelker nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
1568f08f33eSPeter Wemm
1578f08f33eSPeter Wemm if (nr <= 0) {
1588f08f33eSPeter Wemm if (nr < 0) {
1594b88c807SRodney W. Grimes if (errno == EINTR)
1604b88c807SRodney W. Grimes goto retry;
1614b88c807SRodney W. Grimes if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
1624b88c807SRodney W. Grimes int flags = fcntl(0, F_GETFL, 0);
1634b88c807SRodney W. Grimes if (flags >= 0 && flags & O_NONBLOCK) {
1644b88c807SRodney W. Grimes flags &=~ O_NONBLOCK;
1654b88c807SRodney W. Grimes if (fcntl(0, F_SETFL, flags) >= 0) {
166c6204d4aSJilles Tjoelker out2fmt_flush("sh: turning off NDELAY mode\n");
1674b88c807SRodney W. Grimes goto retry;
1684b88c807SRodney W. Grimes }
1694b88c807SRodney W. Grimes }
1704b88c807SRodney W. Grimes }
1714b88c807SRodney W. Grimes }
1728f08f33eSPeter Wemm nr = -1;
1738f08f33eSPeter Wemm }
1748f08f33eSPeter Wemm return nr;
1758f08f33eSPeter Wemm }
1768f08f33eSPeter Wemm
1778f08f33eSPeter Wemm /*
1788f08f33eSPeter Wemm * Refill the input buffer and return the next input character:
1798f08f33eSPeter Wemm *
1808f08f33eSPeter Wemm * 1) If a string was pushed back on the input, pop it;
1818f08f33eSPeter Wemm * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
1828f08f33eSPeter Wemm * from a string so we can't refill the buffer, return EOF.
183ab0a2172SSteve Price * 3) If there is more in this buffer, use it else call read to fill it.
184ab0a2172SSteve Price * 4) Process input up to the next newline, deleting nul characters.
1858f08f33eSPeter Wemm */
1868f08f33eSPeter Wemm
1878f08f33eSPeter Wemm int
preadbuffer(void)1885134c3f7SWarner Losh preadbuffer(void)
189ab0a2172SSteve Price {
190dcd95d8aSJilles Tjoelker char *p, *q, *r, *end;
1918f08f33eSPeter Wemm char savec;
1928f08f33eSPeter Wemm
1934b489a60SJilles Tjoelker while (parsefile->strpush) {
1944b489a60SJilles Tjoelker /*
1954b489a60SJilles Tjoelker * Add a space to the end of an alias to ensure that the
1964b489a60SJilles Tjoelker * alias remains in use while parsing its last word.
1974b489a60SJilles Tjoelker * This avoids alias recursions.
1984b489a60SJilles Tjoelker */
1994b489a60SJilles Tjoelker if (parsenleft == -1 && parsefile->strpush->ap != NULL)
2004b489a60SJilles Tjoelker return ' ';
2018f08f33eSPeter Wemm popstring();
2028f08f33eSPeter Wemm if (--parsenleft >= 0)
2038f08f33eSPeter Wemm return (*parsenextc++);
2048f08f33eSPeter Wemm }
2058f08f33eSPeter Wemm if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
2068f08f33eSPeter Wemm return PEOF;
2078f08f33eSPeter Wemm
2088f08f33eSPeter Wemm again:
2098f08f33eSPeter Wemm if (parselleft <= 0) {
2104ca7fe3bSSteve Price if ((parselleft = preadfd()) == -1) {
2118f08f33eSPeter Wemm parselleft = parsenleft = EOF_NLEFT;
2124b88c807SRodney W. Grimes return PEOF;
2134b88c807SRodney W. Grimes }
2148f08f33eSPeter Wemm }
2158f08f33eSPeter Wemm
216dcd95d8aSJilles Tjoelker p = parsefile->buf + (parsenextc - parsefile->buf);
217dcd95d8aSJilles Tjoelker end = p + parselleft;
218dcd95d8aSJilles Tjoelker *end = '\0';
219dcd95d8aSJilles Tjoelker q = strchrnul(p, '\n');
220dcd95d8aSJilles Tjoelker if (q != end && *q == '\0') {
2214b88c807SRodney W. Grimes /* delete nul characters */
222dcd95d8aSJilles Tjoelker for (r = q; q != end; q++) {
223dcd95d8aSJilles Tjoelker if (*q != '\0')
224dcd95d8aSJilles Tjoelker *r++ = *q;
2258f08f33eSPeter Wemm }
226dcd95d8aSJilles Tjoelker parselleft -= end - r;
227dcd95d8aSJilles Tjoelker if (parselleft == 0)
2288f08f33eSPeter Wemm goto again;
229dcd95d8aSJilles Tjoelker end = p + parselleft;
230dcd95d8aSJilles Tjoelker *end = '\0';
231dcd95d8aSJilles Tjoelker q = strchrnul(p, '\n');
2328f08f33eSPeter Wemm }
233dcd95d8aSJilles Tjoelker if (q == end) {
234dcd95d8aSJilles Tjoelker parsenleft = parselleft;
235dcd95d8aSJilles Tjoelker parselleft = 0;
236dcd95d8aSJilles Tjoelker } else /* *q == '\n' */ {
23784f18910SJilles Tjoelker q++;
23884f18910SJilles Tjoelker parsenleft = q - parsenextc;
239dcd95d8aSJilles Tjoelker parselleft -= parsenleft;
2408f08f33eSPeter Wemm }
241dcd95d8aSJilles Tjoelker parsenleft--;
2428f08f33eSPeter Wemm
2438f08f33eSPeter Wemm savec = *q;
2448f08f33eSPeter Wemm *q = '\0';
2458f08f33eSPeter Wemm
2464417f629SPeter Wemm #ifndef NO_HISTORY
247cac001aaSJilles Tjoelker if (parsefile->fd == 0 && hist &&
248cac001aaSJilles Tjoelker parsenextc[strspn(parsenextc, " \t\n")] != '\0') {
249757eeda0SDavid E. O'Brien HistEvent he;
2504b88c807SRodney W. Grimes INTOFF;
251757eeda0SDavid E. O'Brien history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
252757eeda0SDavid E. O'Brien parsenextc);
2534b88c807SRodney W. Grimes INTON;
2544b88c807SRodney W. Grimes }
2554417f629SPeter Wemm #endif
2568f08f33eSPeter Wemm
2574b88c807SRodney W. Grimes if (vflag) {
2588f08f33eSPeter Wemm out2str(parsenextc);
2594b88c807SRodney W. Grimes flushout(out2);
2604b88c807SRodney W. Grimes }
2618f08f33eSPeter Wemm
2628f08f33eSPeter Wemm *q = savec;
2638f08f33eSPeter Wemm
2644b88c807SRodney W. Grimes return *parsenextc++;
2654b88c807SRodney W. Grimes }
2664b88c807SRodney W. Grimes
2674b88c807SRodney W. Grimes /*
268960da934SJilles Tjoelker * Returns if we are certain we are at EOF. Does not cause any more input
269960da934SJilles Tjoelker * to be read from the outside world.
270960da934SJilles Tjoelker */
271960da934SJilles Tjoelker
272960da934SJilles Tjoelker int
preadateof(void)273960da934SJilles Tjoelker preadateof(void)
274960da934SJilles Tjoelker {
275960da934SJilles Tjoelker if (parsenleft > 0)
276960da934SJilles Tjoelker return 0;
277960da934SJilles Tjoelker if (parsefile->strpush)
278960da934SJilles Tjoelker return 0;
279960da934SJilles Tjoelker if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
280960da934SJilles Tjoelker return 1;
281960da934SJilles Tjoelker return 0;
282960da934SJilles Tjoelker }
283960da934SJilles Tjoelker
284960da934SJilles Tjoelker /*
2854b88c807SRodney W. Grimes * Undo the last call to pgetc. Only one character may be pushed back.
2864b88c807SRodney W. Grimes * PEOF may be pushed back.
2874b88c807SRodney W. Grimes */
2884b88c807SRodney W. Grimes
2894b88c807SRodney W. Grimes void
pungetc(void)2905134c3f7SWarner Losh pungetc(void)
2915134c3f7SWarner Losh {
2924b88c807SRodney W. Grimes parsenleft++;
2934b88c807SRodney W. Grimes parsenextc--;
2944b88c807SRodney W. Grimes }
2954b88c807SRodney W. Grimes
2964b88c807SRodney W. Grimes /*
2974b88c807SRodney W. Grimes * Push a string back onto the input at this current parsefile level.
2984b88c807SRodney W. Grimes * We handle aliases this way.
2994b88c807SRodney W. Grimes */
3004b88c807SRodney W. Grimes void
pushstring(const char * s,int len,struct alias * ap)3015545faddSJilles Tjoelker pushstring(const char *s, int len, struct alias *ap)
3024b88c807SRodney W. Grimes {
3034b88c807SRodney W. Grimes struct strpush *sp;
3044b88c807SRodney W. Grimes
3054b88c807SRodney W. Grimes INTOFF;
306c6204d4aSJilles Tjoelker /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
3074b88c807SRodney W. Grimes if (parsefile->strpush) {
3084b88c807SRodney W. Grimes sp = ckmalloc(sizeof (struct strpush));
3094b88c807SRodney W. Grimes sp->prev = parsefile->strpush;
3104b88c807SRodney W. Grimes parsefile->strpush = sp;
3114b88c807SRodney W. Grimes } else
3124b88c807SRodney W. Grimes sp = parsefile->strpush = &(parsefile->basestrpush);
3134b88c807SRodney W. Grimes sp->prevstring = parsenextc;
3144b88c807SRodney W. Grimes sp->prevnleft = parsenleft;
3158f08f33eSPeter Wemm sp->prevlleft = parselleft;
31635dab859SJilles Tjoelker sp->ap = ap;
3174b88c807SRodney W. Grimes if (ap)
31835dab859SJilles Tjoelker ap->flag |= ALIASINUSE;
3194b88c807SRodney W. Grimes parsenextc = s;
3204b88c807SRodney W. Grimes parsenleft = len;
3214b88c807SRodney W. Grimes INTON;
3224b88c807SRodney W. Grimes }
3234b88c807SRodney W. Grimes
324260fc3f4SJilles Tjoelker static void
popstring(void)3255134c3f7SWarner Losh popstring(void)
3264b88c807SRodney W. Grimes {
3274b88c807SRodney W. Grimes struct strpush *sp = parsefile->strpush;
3284b88c807SRodney W. Grimes
3294b88c807SRodney W. Grimes INTOFF;
33048f49aacSJilles Tjoelker if (sp->ap) {
33148f49aacSJilles Tjoelker if (parsenextc != sp->ap->val &&
33248f49aacSJilles Tjoelker (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
33348f49aacSJilles Tjoelker forcealias();
33448f49aacSJilles Tjoelker sp->ap->flag &= ~ALIASINUSE;
33548f49aacSJilles Tjoelker }
3364b88c807SRodney W. Grimes parsenextc = sp->prevstring;
3374b88c807SRodney W. Grimes parsenleft = sp->prevnleft;
3388f08f33eSPeter Wemm parselleft = sp->prevlleft;
339c6204d4aSJilles Tjoelker /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
3404b88c807SRodney W. Grimes parsefile->strpush = sp->prev;
3414b88c807SRodney W. Grimes if (sp != &(parsefile->basestrpush))
3424b88c807SRodney W. Grimes ckfree(sp);
3434b88c807SRodney W. Grimes INTON;
3444b88c807SRodney W. Grimes }
3454b88c807SRodney W. Grimes
3464b88c807SRodney W. Grimes /*
3474b88c807SRodney W. Grimes * Set the input to take input from a file. If push is set, push the
3484b88c807SRodney W. Grimes * old input onto the stack first.
349d2c23317SStephane Rochoy * About verify:
350d2c23317SStephane Rochoy * -1: Obey verifyflag
351d2c23317SStephane Rochoy * 0: Do not verify
352d2c23317SStephane Rochoy * 1: Do verify
3534b88c807SRodney W. Grimes */
3544b88c807SRodney W. Grimes
3554b88c807SRodney W. Grimes void
setinputfile(const char * fname,int push,int verify)356d2c23317SStephane Rochoy setinputfile(const char *fname, int push, int verify)
3574b88c807SRodney W. Grimes {
35877da4a95SJilles Tjoelker int e;
3594b88c807SRodney W. Grimes int fd;
3604b88c807SRodney W. Grimes int fd2;
361d2c23317SStephane Rochoy int oflags = O_RDONLY | O_CLOEXEC;
362d2c23317SStephane Rochoy
363d2c23317SStephane Rochoy if (verify == 1 || (verify == -1 && verifyflag))
364d2c23317SStephane Rochoy oflags |= O_VERIFY;
3654b88c807SRodney W. Grimes
3664b88c807SRodney W. Grimes INTOFF;
367d2c23317SStephane Rochoy if ((fd = open(fname, oflags)) < 0) {
36877da4a95SJilles Tjoelker e = errno;
36977da4a95SJilles Tjoelker errorwithstatus(e == ENOENT || e == ENOTDIR ? 127 : 126,
37077da4a95SJilles Tjoelker "cannot open %s: %s", fname, strerror(e));
37177da4a95SJilles Tjoelker }
3724b88c807SRodney W. Grimes if (fd < 10) {
3735aa6dfdaSJilles Tjoelker fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
3744b88c807SRodney W. Grimes close(fd);
3754b88c807SRodney W. Grimes if (fd2 < 0)
3764b88c807SRodney W. Grimes error("Out of file descriptors");
3774b88c807SRodney W. Grimes fd = fd2;
3784b88c807SRodney W. Grimes }
3794b88c807SRodney W. Grimes setinputfd(fd, push);
3804b88c807SRodney W. Grimes INTON;
3814b88c807SRodney W. Grimes }
3824b88c807SRodney W. Grimes
3834b88c807SRodney W. Grimes
3844b88c807SRodney W. Grimes /*
3855aa6dfdaSJilles Tjoelker * Like setinputfile, but takes an open file descriptor (which should have
3865aa6dfdaSJilles Tjoelker * its FD_CLOEXEC flag already set). Call this with interrupts off.
3874b88c807SRodney W. Grimes */
3884b88c807SRodney W. Grimes
3894b88c807SRodney W. Grimes void
setinputfd(int fd,int push)3905134c3f7SWarner Losh setinputfd(int fd, int push)
391aa9caaf6SPeter Wemm {
3924b88c807SRodney W. Grimes if (push) {
3934b88c807SRodney W. Grimes pushfile();
3947f40c1f8SJilles Tjoelker parsefile->buf = ckmalloc(BUFSIZ + 1);
3954b88c807SRodney W. Grimes }
3964b88c807SRodney W. Grimes if (parsefile->fd > 0)
3974b88c807SRodney W. Grimes close(parsefile->fd);
3984b88c807SRodney W. Grimes parsefile->fd = fd;
3994b88c807SRodney W. Grimes if (parsefile->buf == NULL)
4007f40c1f8SJilles Tjoelker parsefile->buf = ckmalloc(BUFSIZ + 1);
4018f08f33eSPeter Wemm parselleft = parsenleft = 0;
4024b88c807SRodney W. Grimes plinno = 1;
4034b88c807SRodney W. Grimes }
4044b88c807SRodney W. Grimes
4054b88c807SRodney W. Grimes
4064b88c807SRodney W. Grimes /*
4074b88c807SRodney W. Grimes * Like setinputfile, but takes input from a string.
4084b88c807SRodney W. Grimes */
4094b88c807SRodney W. Grimes
4104b88c807SRodney W. Grimes void
setinputstring(const char * string,int push)41146c6b52dSJilles Tjoelker setinputstring(const char *string, int push)
4124b88c807SRodney W. Grimes {
4134b88c807SRodney W. Grimes INTOFF;
4144b88c807SRodney W. Grimes if (push)
4154b88c807SRodney W. Grimes pushfile();
4164b88c807SRodney W. Grimes parsenextc = string;
4178f08f33eSPeter Wemm parselleft = parsenleft = strlen(string);
4184b88c807SRodney W. Grimes parsefile->buf = NULL;
4194b88c807SRodney W. Grimes plinno = 1;
4204b88c807SRodney W. Grimes INTON;
4214b88c807SRodney W. Grimes }
4224b88c807SRodney W. Grimes
4234b88c807SRodney W. Grimes
4244b88c807SRodney W. Grimes
4254b88c807SRodney W. Grimes /*
4264b88c807SRodney W. Grimes * To handle the "." command, a stack of input files is used. Pushfile
4274b88c807SRodney W. Grimes * adds a new entry to the stack and popfile restores the previous level.
4284b88c807SRodney W. Grimes */
4294b88c807SRodney W. Grimes
43088328642SDavid E. O'Brien static void
pushfile(void)4315134c3f7SWarner Losh pushfile(void)
4325134c3f7SWarner Losh {
4334b88c807SRodney W. Grimes struct parsefile *pf;
4344b88c807SRodney W. Grimes
4354b88c807SRodney W. Grimes parsefile->nleft = parsenleft;
4368f08f33eSPeter Wemm parsefile->lleft = parselleft;
4374b88c807SRodney W. Grimes parsefile->nextc = parsenextc;
4384b88c807SRodney W. Grimes parsefile->linno = plinno;
4394b88c807SRodney W. Grimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
4404b88c807SRodney W. Grimes pf->prev = parsefile;
4414b88c807SRodney W. Grimes pf->fd = -1;
4424b88c807SRodney W. Grimes pf->strpush = NULL;
4434b88c807SRodney W. Grimes pf->basestrpush.prev = NULL;
4444b88c807SRodney W. Grimes parsefile = pf;
4454b88c807SRodney W. Grimes }
4464b88c807SRodney W. Grimes
4474b88c807SRodney W. Grimes
4484b88c807SRodney W. Grimes void
popfile(void)4495134c3f7SWarner Losh popfile(void)
4505134c3f7SWarner Losh {
4514b88c807SRodney W. Grimes struct parsefile *pf = parsefile;
4524b88c807SRodney W. Grimes
4534b88c807SRodney W. Grimes INTOFF;
4544b88c807SRodney W. Grimes if (pf->fd >= 0)
4554b88c807SRodney W. Grimes close(pf->fd);
4564b88c807SRodney W. Grimes if (pf->buf)
4574b88c807SRodney W. Grimes ckfree(pf->buf);
4584b88c807SRodney W. Grimes while (pf->strpush)
4594b88c807SRodney W. Grimes popstring();
4604b88c807SRodney W. Grimes parsefile = pf->prev;
4614b88c807SRodney W. Grimes ckfree(pf);
4624b88c807SRodney W. Grimes parsenleft = parsefile->nleft;
4638f08f33eSPeter Wemm parselleft = parsefile->lleft;
4644b88c807SRodney W. Grimes parsenextc = parsefile->nextc;
4654b88c807SRodney W. Grimes plinno = parsefile->linno;
4664b88c807SRodney W. Grimes INTON;
4674b88c807SRodney W. Grimes }
4684b88c807SRodney W. Grimes
4694b88c807SRodney W. Grimes
4704b88c807SRodney W. Grimes /*
471eaa34893SJilles Tjoelker * Return current file (to go back to it later using popfilesupto()).
472eaa34893SJilles Tjoelker */
473eaa34893SJilles Tjoelker
474eaa34893SJilles Tjoelker struct parsefile *
getcurrentfile(void)475eaa34893SJilles Tjoelker getcurrentfile(void)
476eaa34893SJilles Tjoelker {
477eaa34893SJilles Tjoelker return parsefile;
478eaa34893SJilles Tjoelker }
479eaa34893SJilles Tjoelker
480eaa34893SJilles Tjoelker
481eaa34893SJilles Tjoelker /*
482eaa34893SJilles Tjoelker * Pop files until the given file is on top again. Useful for regular
483eaa34893SJilles Tjoelker * builtins that read shell commands from files or strings.
484eaa34893SJilles Tjoelker * If the given file is not an active file, an error is raised.
485eaa34893SJilles Tjoelker */
486eaa34893SJilles Tjoelker
487eaa34893SJilles Tjoelker void
popfilesupto(struct parsefile * file)488eaa34893SJilles Tjoelker popfilesupto(struct parsefile *file)
489eaa34893SJilles Tjoelker {
490eaa34893SJilles Tjoelker while (parsefile != file && parsefile != &basepf)
491eaa34893SJilles Tjoelker popfile();
492eaa34893SJilles Tjoelker if (parsefile != file)
493eaa34893SJilles Tjoelker error("popfilesupto() misused");
494eaa34893SJilles Tjoelker }
495eaa34893SJilles Tjoelker
496eaa34893SJilles Tjoelker /*
4974b88c807SRodney W. Grimes * Return to top level.
4984b88c807SRodney W. Grimes */
4994b88c807SRodney W. Grimes
5004b88c807SRodney W. Grimes void
popallfiles(void)5015134c3f7SWarner Losh popallfiles(void)
5025134c3f7SWarner Losh {
5034b88c807SRodney W. Grimes while (parsefile != &basepf)
5044b88c807SRodney W. Grimes popfile();
5054b88c807SRodney W. Grimes }
5064b88c807SRodney W. Grimes
5074b88c807SRodney W. Grimes
5084b88c807SRodney W. Grimes
5094b88c807SRodney W. Grimes /*
5104b88c807SRodney W. Grimes * Close the file(s) that the shell is reading commands from. Called
5114b88c807SRodney W. Grimes * after a fork is done.
5124b88c807SRodney W. Grimes */
5134b88c807SRodney W. Grimes
5144b88c807SRodney W. Grimes void
closescript(void)5155134c3f7SWarner Losh closescript(void)
5165134c3f7SWarner Losh {
5174b88c807SRodney W. Grimes popallfiles();
5184b88c807SRodney W. Grimes if (parsefile->fd > 0) {
5194b88c807SRodney W. Grimes close(parsefile->fd);
5204b88c807SRodney W. Grimes parsefile->fd = 0;
5214b88c807SRodney W. Grimes }
5224b88c807SRodney W. Grimes }
523