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 * 4. 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 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <stdio.h> /* defines BUFSIZ */ 42aa9caaf6SPeter Wemm #include <fcntl.h> 43aa9caaf6SPeter Wemm #include <errno.h> 44aa9caaf6SPeter Wemm #include <unistd.h> 45aa9caaf6SPeter Wemm #include <stdlib.h> 46aa9caaf6SPeter Wemm #include <string.h> 47aa9caaf6SPeter Wemm 484b88c807SRodney W. Grimes /* 494b88c807SRodney W. Grimes * This file implements the input routines used by the parser. 504b88c807SRodney W. Grimes */ 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes #include "shell.h" 53aa9caaf6SPeter Wemm #include "redir.h" 544b88c807SRodney W. Grimes #include "syntax.h" 554b88c807SRodney W. Grimes #include "input.h" 564b88c807SRodney W. Grimes #include "output.h" 574b88c807SRodney W. Grimes #include "options.h" 584b88c807SRodney W. Grimes #include "memalloc.h" 594b88c807SRodney W. Grimes #include "error.h" 604b88c807SRodney W. Grimes #include "alias.h" 614b88c807SRodney W. Grimes #include "parser.h" 624b88c807SRodney W. Grimes #include "myhistedit.h" 63be58cc48STim J. Robbins #include "trap.h" 644b88c807SRodney W. Grimes 654b88c807SRodney W. Grimes #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes struct strpush { 684b88c807SRodney W. Grimes struct strpush *prev; /* preceding string on stack */ 6946c6b52dSJilles Tjoelker const char *prevstring; 704b88c807SRodney W. Grimes int prevnleft; 718f08f33eSPeter Wemm int prevlleft; 724b88c807SRodney W. Grimes struct alias *ap; /* if push was associated with an alias */ 734b88c807SRodney W. Grimes }; 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes /* 764b88c807SRodney W. Grimes * The parsefile structure pointed to by the global variable parsefile 774b88c807SRodney W. Grimes * contains information about the current file being read. 784b88c807SRodney W. Grimes */ 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes struct parsefile { 814b88c807SRodney W. Grimes struct parsefile *prev; /* preceding file on stack */ 824b88c807SRodney W. Grimes int linno; /* current line */ 834b88c807SRodney W. Grimes int fd; /* file descriptor (or -1 if string) */ 84ab0a2172SSteve Price int nleft; /* number of chars left in this line */ 85ab0a2172SSteve Price int lleft; /* number of lines left in this buffer */ 8646c6b52dSJilles Tjoelker const char *nextc; /* next char in buffer */ 874b88c807SRodney W. Grimes char *buf; /* input buffer */ 884b88c807SRodney W. Grimes struct strpush *strpush; /* for pushing strings at this level */ 894b88c807SRodney W. Grimes struct strpush basestrpush; /* so pushing one is fast */ 904b88c807SRodney W. Grimes }; 914b88c807SRodney W. Grimes 924b88c807SRodney W. Grimes 934b88c807SRodney W. Grimes int plinno = 1; /* input line number */ 94384aedabSJilles Tjoelker int parsenleft; /* copy of parsefile->nleft */ 950bdd3871SJilles Tjoelker static int parselleft; /* copy of parsefile->lleft */ 9646c6b52dSJilles Tjoelker const char *parsenextc; /* copy of parsefile->nextc */ 9760a6bf2aSJilles Tjoelker static char basebuf[BUFSIZ + 1];/* buffer for top level input file */ 9860a6bf2aSJilles Tjoelker static struct parsefile basepf = { /* top level input file */ 9960a6bf2aSJilles Tjoelker .nextc = basebuf, 10060a6bf2aSJilles Tjoelker .buf = basebuf 10160a6bf2aSJilles Tjoelker }; 102aa7b6f82SDavid E. O'Brien static struct parsefile *parsefile = &basepf; /* current input file */ 1034b88c807SRodney W. Grimes int whichprompt; /* 1 == PS1, 2 == PS2 */ 1044b88c807SRodney W. Grimes 1054b88c807SRodney W. Grimes EditLine *el; /* cookie for editline package */ 1064b88c807SRodney W. Grimes 10788328642SDavid E. O'Brien static void pushfile(void); 10888328642SDavid E. O'Brien static int preadfd(void); 109260fc3f4SJilles Tjoelker static void popstring(void); 1104b88c807SRodney W. Grimes 111338b821bSJilles Tjoelker void 112338b821bSJilles Tjoelker resetinput(void) 113338b821bSJilles Tjoelker { 1143055b7c6SJilles Tjoelker popallfiles(); 1158f08f33eSPeter Wemm parselleft = parsenleft = 0; /* clear input buffer */ 1164b88c807SRodney W. Grimes } 1174b88c807SRodney W. Grimes 1184b88c807SRodney W. Grimes 1194b88c807SRodney W. Grimes /* 1204b88c807SRodney W. Grimes * Read a line from the script. 1214b88c807SRodney W. Grimes */ 1224b88c807SRodney W. Grimes 1234b88c807SRodney W. Grimes char * 1245134c3f7SWarner Losh pfgets(char *line, int len) 1254b88c807SRodney W. Grimes { 1264ca7fe3bSSteve Price char *p = line; 1274b88c807SRodney W. Grimes int nleft = len; 1284b88c807SRodney W. Grimes int c; 1294b88c807SRodney W. Grimes 1304b88c807SRodney W. Grimes while (--nleft > 0) { 1314b88c807SRodney W. Grimes c = pgetc_macro(); 1324b88c807SRodney W. Grimes if (c == PEOF) { 1334b88c807SRodney W. Grimes if (p == line) 1344b88c807SRodney W. Grimes return NULL; 1354b88c807SRodney W. Grimes break; 1364b88c807SRodney W. Grimes } 1374b88c807SRodney W. Grimes *p++ = c; 1384b88c807SRodney W. Grimes if (c == '\n') 1394b88c807SRodney W. Grimes break; 1404b88c807SRodney W. Grimes } 1414b88c807SRodney W. Grimes *p = '\0'; 1424b88c807SRodney W. Grimes return line; 1434b88c807SRodney W. Grimes } 1444b88c807SRodney W. Grimes 1454b88c807SRodney W. Grimes 1464b88c807SRodney W. Grimes 1474b88c807SRodney W. Grimes /* 1484b88c807SRodney W. Grimes * Read a character from the script, returning PEOF on end of file. 1494b88c807SRodney W. Grimes * Nul characters in the input are silently discarded. 1504b88c807SRodney W. Grimes */ 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes int 1535134c3f7SWarner Losh pgetc(void) 154ab0a2172SSteve Price { 1554b88c807SRodney W. Grimes return pgetc_macro(); 1564b88c807SRodney W. Grimes } 1574b88c807SRodney W. Grimes 158ab0a2172SSteve Price 15988328642SDavid E. O'Brien static int 1605134c3f7SWarner Losh preadfd(void) 1618f08f33eSPeter Wemm { 1628f08f33eSPeter Wemm int nr; 1638f08f33eSPeter Wemm parsenextc = parsefile->buf; 164ab0a2172SSteve Price 1654b88c807SRodney W. Grimes retry: 1664ca7fe3bSSteve Price #ifndef NO_HISTORY 1674b88c807SRodney W. Grimes if (parsefile->fd == 0 && el) { 168896229d9SStefan Farfeleder static const char *rl_cp; 169896229d9SStefan Farfeleder static int el_len; 1704b88c807SRodney W. Grimes 171*d78fdfdeSJilles Tjoelker if (rl_cp == NULL) { 172*d78fdfdeSJilles Tjoelker el_resize(el); 173896229d9SStefan Farfeleder rl_cp = el_gets(el, &el_len); 174*d78fdfdeSJilles Tjoelker } 1758f08f33eSPeter Wemm if (rl_cp == NULL) 17600c43e0cSPedro F. Giffuni nr = el_len == 0 ? 0 : -1; 1778f08f33eSPeter Wemm else { 178896229d9SStefan Farfeleder nr = el_len; 1797f40c1f8SJilles Tjoelker if (nr > BUFSIZ) 1807f40c1f8SJilles Tjoelker nr = BUFSIZ; 18146c6b52dSJilles Tjoelker memcpy(parsefile->buf, rl_cp, nr); 182896229d9SStefan Farfeleder if (nr != el_len) { 183896229d9SStefan Farfeleder el_len -= nr; 184896229d9SStefan Farfeleder rl_cp += nr; 185896229d9SStefan Farfeleder } else 186896229d9SStefan Farfeleder rl_cp = NULL; 1874b88c807SRodney W. Grimes } 1884ca7fe3bSSteve Price } else 1894ca7fe3bSSteve Price #endif 19046c6b52dSJilles Tjoelker nr = read(parsefile->fd, parsefile->buf, BUFSIZ); 1918f08f33eSPeter Wemm 1928f08f33eSPeter Wemm if (nr <= 0) { 1938f08f33eSPeter Wemm if (nr < 0) { 1944b88c807SRodney W. Grimes if (errno == EINTR) 1954b88c807SRodney W. Grimes goto retry; 1964b88c807SRodney W. Grimes if (parsefile->fd == 0 && errno == EWOULDBLOCK) { 1974b88c807SRodney W. Grimes int flags = fcntl(0, F_GETFL, 0); 1984b88c807SRodney W. Grimes if (flags >= 0 && flags & O_NONBLOCK) { 1994b88c807SRodney W. Grimes flags &=~ O_NONBLOCK; 2004b88c807SRodney W. Grimes if (fcntl(0, F_SETFL, flags) >= 0) { 201c6204d4aSJilles Tjoelker out2fmt_flush("sh: turning off NDELAY mode\n"); 2024b88c807SRodney W. Grimes goto retry; 2034b88c807SRodney W. Grimes } 2044b88c807SRodney W. Grimes } 2054b88c807SRodney W. Grimes } 2064b88c807SRodney W. Grimes } 2078f08f33eSPeter Wemm nr = -1; 2088f08f33eSPeter Wemm } 2098f08f33eSPeter Wemm return nr; 2108f08f33eSPeter Wemm } 2118f08f33eSPeter Wemm 2128f08f33eSPeter Wemm /* 2138f08f33eSPeter Wemm * Refill the input buffer and return the next input character: 2148f08f33eSPeter Wemm * 2158f08f33eSPeter Wemm * 1) If a string was pushed back on the input, pop it; 2168f08f33eSPeter Wemm * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading 2178f08f33eSPeter Wemm * from a string so we can't refill the buffer, return EOF. 218ab0a2172SSteve Price * 3) If there is more in this buffer, use it else call read to fill it. 219ab0a2172SSteve Price * 4) Process input up to the next newline, deleting nul characters. 2208f08f33eSPeter Wemm */ 2218f08f33eSPeter Wemm 2228f08f33eSPeter Wemm int 2235134c3f7SWarner Losh preadbuffer(void) 224ab0a2172SSteve Price { 2258f08f33eSPeter Wemm char *p, *q; 2268f08f33eSPeter Wemm int more; 2278f08f33eSPeter Wemm char savec; 2288f08f33eSPeter Wemm 2298f08f33eSPeter Wemm if (parsefile->strpush) { 2308f08f33eSPeter Wemm popstring(); 2318f08f33eSPeter Wemm if (--parsenleft >= 0) 2328f08f33eSPeter Wemm return (*parsenextc++); 2338f08f33eSPeter Wemm } 2348f08f33eSPeter Wemm if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 2358f08f33eSPeter Wemm return PEOF; 2368f08f33eSPeter Wemm flushout(&output); 2378f08f33eSPeter Wemm flushout(&errout); 2388f08f33eSPeter Wemm 2398f08f33eSPeter Wemm again: 2408f08f33eSPeter Wemm if (parselleft <= 0) { 2414ca7fe3bSSteve Price if ((parselleft = preadfd()) == -1) { 2428f08f33eSPeter Wemm parselleft = parsenleft = EOF_NLEFT; 2434b88c807SRodney W. Grimes return PEOF; 2444b88c807SRodney W. Grimes } 2458f08f33eSPeter Wemm } 2468f08f33eSPeter Wemm 24746c6b52dSJilles Tjoelker q = p = parsefile->buf + (parsenextc - parsefile->buf); 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes /* delete nul characters */ 2508f08f33eSPeter Wemm for (more = 1; more;) { 2518f08f33eSPeter Wemm switch (*p) { 2528f08f33eSPeter Wemm case '\0': 2538f08f33eSPeter Wemm p++; /* Skip nul */ 2548f08f33eSPeter Wemm goto check; 2554b88c807SRodney W. Grimes 2568f08f33eSPeter Wemm case '\n': 2578f08f33eSPeter Wemm parsenleft = q - parsenextc; 2588f08f33eSPeter Wemm more = 0; /* Stop processing here */ 2598f08f33eSPeter Wemm break; 2608f08f33eSPeter Wemm 2618f08f33eSPeter Wemm default: 2628f08f33eSPeter Wemm break; 2638f08f33eSPeter Wemm } 2648f08f33eSPeter Wemm 2658f08f33eSPeter Wemm *q++ = *p++; 2668f08f33eSPeter Wemm check: 2678f08f33eSPeter Wemm if (--parselleft <= 0) { 2688f08f33eSPeter Wemm parsenleft = q - parsenextc - 1; 2698f08f33eSPeter Wemm if (parsenleft < 0) 2708f08f33eSPeter Wemm goto again; 2718f08f33eSPeter Wemm *q = '\0'; 2728f08f33eSPeter Wemm more = 0; 2738f08f33eSPeter Wemm } 2748f08f33eSPeter Wemm } 2758f08f33eSPeter Wemm 2768f08f33eSPeter Wemm savec = *q; 2778f08f33eSPeter Wemm *q = '\0'; 2788f08f33eSPeter Wemm 2794417f629SPeter Wemm #ifndef NO_HISTORY 280cac001aaSJilles Tjoelker if (parsefile->fd == 0 && hist && 281cac001aaSJilles Tjoelker parsenextc[strspn(parsenextc, " \t\n")] != '\0') { 282757eeda0SDavid E. O'Brien HistEvent he; 2834b88c807SRodney W. Grimes INTOFF; 284757eeda0SDavid E. O'Brien history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD, 285757eeda0SDavid E. O'Brien parsenextc); 2864b88c807SRodney W. Grimes INTON; 2874b88c807SRodney W. Grimes } 2884417f629SPeter Wemm #endif 2898f08f33eSPeter Wemm 2904b88c807SRodney W. Grimes if (vflag) { 2918f08f33eSPeter Wemm out2str(parsenextc); 2924b88c807SRodney W. Grimes flushout(out2); 2934b88c807SRodney W. Grimes } 2948f08f33eSPeter Wemm 2958f08f33eSPeter Wemm *q = savec; 2968f08f33eSPeter Wemm 2974b88c807SRodney W. Grimes return *parsenextc++; 2984b88c807SRodney W. Grimes } 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes /* 301960da934SJilles Tjoelker * Returns if we are certain we are at EOF. Does not cause any more input 302960da934SJilles Tjoelker * to be read from the outside world. 303960da934SJilles Tjoelker */ 304960da934SJilles Tjoelker 305960da934SJilles Tjoelker int 306960da934SJilles Tjoelker preadateof(void) 307960da934SJilles Tjoelker { 308960da934SJilles Tjoelker if (parsenleft > 0) 309960da934SJilles Tjoelker return 0; 310960da934SJilles Tjoelker if (parsefile->strpush) 311960da934SJilles Tjoelker return 0; 312960da934SJilles Tjoelker if (parsenleft == EOF_NLEFT || parsefile->buf == NULL) 313960da934SJilles Tjoelker return 1; 314960da934SJilles Tjoelker return 0; 315960da934SJilles Tjoelker } 316960da934SJilles Tjoelker 317960da934SJilles Tjoelker /* 3184b88c807SRodney W. Grimes * Undo the last call to pgetc. Only one character may be pushed back. 3194b88c807SRodney W. Grimes * PEOF may be pushed back. 3204b88c807SRodney W. Grimes */ 3214b88c807SRodney W. Grimes 3224b88c807SRodney W. Grimes void 3235134c3f7SWarner Losh pungetc(void) 3245134c3f7SWarner Losh { 3254b88c807SRodney W. Grimes parsenleft++; 3264b88c807SRodney W. Grimes parsenextc--; 3274b88c807SRodney W. Grimes } 3284b88c807SRodney W. Grimes 3294b88c807SRodney W. Grimes /* 3304b88c807SRodney W. Grimes * Push a string back onto the input at this current parsefile level. 3314b88c807SRodney W. Grimes * We handle aliases this way. 3324b88c807SRodney W. Grimes */ 3334b88c807SRodney W. Grimes void 33435dab859SJilles Tjoelker pushstring(char *s, int len, struct alias *ap) 3354b88c807SRodney W. Grimes { 3364b88c807SRodney W. Grimes struct strpush *sp; 3374b88c807SRodney W. Grimes 3384b88c807SRodney W. Grimes INTOFF; 339c6204d4aSJilles Tjoelker /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/ 3404b88c807SRodney W. Grimes if (parsefile->strpush) { 3414b88c807SRodney W. Grimes sp = ckmalloc(sizeof (struct strpush)); 3424b88c807SRodney W. Grimes sp->prev = parsefile->strpush; 3434b88c807SRodney W. Grimes parsefile->strpush = sp; 3444b88c807SRodney W. Grimes } else 3454b88c807SRodney W. Grimes sp = parsefile->strpush = &(parsefile->basestrpush); 3464b88c807SRodney W. Grimes sp->prevstring = parsenextc; 3474b88c807SRodney W. Grimes sp->prevnleft = parsenleft; 3488f08f33eSPeter Wemm sp->prevlleft = parselleft; 34935dab859SJilles Tjoelker sp->ap = ap; 3504b88c807SRodney W. Grimes if (ap) 35135dab859SJilles Tjoelker ap->flag |= ALIASINUSE; 3524b88c807SRodney W. Grimes parsenextc = s; 3534b88c807SRodney W. Grimes parsenleft = len; 3544b88c807SRodney W. Grimes INTON; 3554b88c807SRodney W. Grimes } 3564b88c807SRodney W. Grimes 357260fc3f4SJilles Tjoelker static void 3585134c3f7SWarner Losh popstring(void) 3594b88c807SRodney W. Grimes { 3604b88c807SRodney W. Grimes struct strpush *sp = parsefile->strpush; 3614b88c807SRodney W. Grimes 3624b88c807SRodney W. Grimes INTOFF; 3634b88c807SRodney W. Grimes parsenextc = sp->prevstring; 3644b88c807SRodney W. Grimes parsenleft = sp->prevnleft; 3658f08f33eSPeter Wemm parselleft = sp->prevlleft; 366c6204d4aSJilles Tjoelker /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/ 3674b88c807SRodney W. Grimes if (sp->ap) 3684b88c807SRodney W. Grimes sp->ap->flag &= ~ALIASINUSE; 3694b88c807SRodney W. Grimes parsefile->strpush = sp->prev; 3704b88c807SRodney W. Grimes if (sp != &(parsefile->basestrpush)) 3714b88c807SRodney W. Grimes ckfree(sp); 3724b88c807SRodney W. Grimes INTON; 3734b88c807SRodney W. Grimes } 3744b88c807SRodney W. Grimes 3754b88c807SRodney W. Grimes /* 3764b88c807SRodney W. Grimes * Set the input to take input from a file. If push is set, push the 3774b88c807SRodney W. Grimes * old input onto the stack first. 3784b88c807SRodney W. Grimes */ 3794b88c807SRodney W. Grimes 3804b88c807SRodney W. Grimes void 3812cac6e36SJilles Tjoelker setinputfile(const char *fname, int push) 3824b88c807SRodney W. Grimes { 3834b88c807SRodney W. Grimes int fd; 3844b88c807SRodney W. Grimes int fd2; 3854b88c807SRodney W. Grimes 3864b88c807SRodney W. Grimes INTOFF; 3875aa6dfdaSJilles Tjoelker if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) 38847a5ab29SJilles Tjoelker error("cannot open %s: %s", fname, strerror(errno)); 3894b88c807SRodney W. Grimes if (fd < 10) { 3905aa6dfdaSJilles Tjoelker fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10); 3914b88c807SRodney W. Grimes close(fd); 3924b88c807SRodney W. Grimes if (fd2 < 0) 3934b88c807SRodney W. Grimes error("Out of file descriptors"); 3944b88c807SRodney W. Grimes fd = fd2; 3954b88c807SRodney W. Grimes } 3964b88c807SRodney W. Grimes setinputfd(fd, push); 3974b88c807SRodney W. Grimes INTON; 3984b88c807SRodney W. Grimes } 3994b88c807SRodney W. Grimes 4004b88c807SRodney W. Grimes 4014b88c807SRodney W. Grimes /* 4025aa6dfdaSJilles Tjoelker * Like setinputfile, but takes an open file descriptor (which should have 4035aa6dfdaSJilles Tjoelker * its FD_CLOEXEC flag already set). Call this with interrupts off. 4044b88c807SRodney W. Grimes */ 4054b88c807SRodney W. Grimes 4064b88c807SRodney W. Grimes void 4075134c3f7SWarner Losh setinputfd(int fd, int push) 408aa9caaf6SPeter Wemm { 4094b88c807SRodney W. Grimes if (push) { 4104b88c807SRodney W. Grimes pushfile(); 4117f40c1f8SJilles Tjoelker parsefile->buf = ckmalloc(BUFSIZ + 1); 4124b88c807SRodney W. Grimes } 4134b88c807SRodney W. Grimes if (parsefile->fd > 0) 4144b88c807SRodney W. Grimes close(parsefile->fd); 4154b88c807SRodney W. Grimes parsefile->fd = fd; 4164b88c807SRodney W. Grimes if (parsefile->buf == NULL) 4177f40c1f8SJilles Tjoelker parsefile->buf = ckmalloc(BUFSIZ + 1); 4188f08f33eSPeter Wemm parselleft = parsenleft = 0; 4194b88c807SRodney W. Grimes plinno = 1; 4204b88c807SRodney W. Grimes } 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes /* 4244b88c807SRodney W. Grimes * Like setinputfile, but takes input from a string. 4254b88c807SRodney W. Grimes */ 4264b88c807SRodney W. Grimes 4274b88c807SRodney W. Grimes void 42846c6b52dSJilles Tjoelker setinputstring(const char *string, int push) 4294b88c807SRodney W. Grimes { 4304b88c807SRodney W. Grimes INTOFF; 4314b88c807SRodney W. Grimes if (push) 4324b88c807SRodney W. Grimes pushfile(); 4334b88c807SRodney W. Grimes parsenextc = string; 4348f08f33eSPeter Wemm parselleft = parsenleft = strlen(string); 4354b88c807SRodney W. Grimes parsefile->buf = NULL; 4364b88c807SRodney W. Grimes plinno = 1; 4374b88c807SRodney W. Grimes INTON; 4384b88c807SRodney W. Grimes } 4394b88c807SRodney W. Grimes 4404b88c807SRodney W. Grimes 4414b88c807SRodney W. Grimes 4424b88c807SRodney W. Grimes /* 4434b88c807SRodney W. Grimes * To handle the "." command, a stack of input files is used. Pushfile 4444b88c807SRodney W. Grimes * adds a new entry to the stack and popfile restores the previous level. 4454b88c807SRodney W. Grimes */ 4464b88c807SRodney W. Grimes 44788328642SDavid E. O'Brien static void 4485134c3f7SWarner Losh pushfile(void) 4495134c3f7SWarner Losh { 4504b88c807SRodney W. Grimes struct parsefile *pf; 4514b88c807SRodney W. Grimes 4524b88c807SRodney W. Grimes parsefile->nleft = parsenleft; 4538f08f33eSPeter Wemm parsefile->lleft = parselleft; 4544b88c807SRodney W. Grimes parsefile->nextc = parsenextc; 4554b88c807SRodney W. Grimes parsefile->linno = plinno; 4564b88c807SRodney W. Grimes pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); 4574b88c807SRodney W. Grimes pf->prev = parsefile; 4584b88c807SRodney W. Grimes pf->fd = -1; 4594b88c807SRodney W. Grimes pf->strpush = NULL; 4604b88c807SRodney W. Grimes pf->basestrpush.prev = NULL; 4614b88c807SRodney W. Grimes parsefile = pf; 4624b88c807SRodney W. Grimes } 4634b88c807SRodney W. Grimes 4644b88c807SRodney W. Grimes 4654b88c807SRodney W. Grimes void 4665134c3f7SWarner Losh popfile(void) 4675134c3f7SWarner Losh { 4684b88c807SRodney W. Grimes struct parsefile *pf = parsefile; 4694b88c807SRodney W. Grimes 4704b88c807SRodney W. Grimes INTOFF; 4714b88c807SRodney W. Grimes if (pf->fd >= 0) 4724b88c807SRodney W. Grimes close(pf->fd); 4734b88c807SRodney W. Grimes if (pf->buf) 4744b88c807SRodney W. Grimes ckfree(pf->buf); 4754b88c807SRodney W. Grimes while (pf->strpush) 4764b88c807SRodney W. Grimes popstring(); 4774b88c807SRodney W. Grimes parsefile = pf->prev; 4784b88c807SRodney W. Grimes ckfree(pf); 4794b88c807SRodney W. Grimes parsenleft = parsefile->nleft; 4808f08f33eSPeter Wemm parselleft = parsefile->lleft; 4814b88c807SRodney W. Grimes parsenextc = parsefile->nextc; 4824b88c807SRodney W. Grimes plinno = parsefile->linno; 4834b88c807SRodney W. Grimes INTON; 4844b88c807SRodney W. Grimes } 4854b88c807SRodney W. Grimes 4864b88c807SRodney W. Grimes 4874b88c807SRodney W. Grimes /* 488eaa34893SJilles Tjoelker * Return current file (to go back to it later using popfilesupto()). 489eaa34893SJilles Tjoelker */ 490eaa34893SJilles Tjoelker 491eaa34893SJilles Tjoelker struct parsefile * 492eaa34893SJilles Tjoelker getcurrentfile(void) 493eaa34893SJilles Tjoelker { 494eaa34893SJilles Tjoelker return parsefile; 495eaa34893SJilles Tjoelker } 496eaa34893SJilles Tjoelker 497eaa34893SJilles Tjoelker 498eaa34893SJilles Tjoelker /* 499eaa34893SJilles Tjoelker * Pop files until the given file is on top again. Useful for regular 500eaa34893SJilles Tjoelker * builtins that read shell commands from files or strings. 501eaa34893SJilles Tjoelker * If the given file is not an active file, an error is raised. 502eaa34893SJilles Tjoelker */ 503eaa34893SJilles Tjoelker 504eaa34893SJilles Tjoelker void 505eaa34893SJilles Tjoelker popfilesupto(struct parsefile *file) 506eaa34893SJilles Tjoelker { 507eaa34893SJilles Tjoelker while (parsefile != file && parsefile != &basepf) 508eaa34893SJilles Tjoelker popfile(); 509eaa34893SJilles Tjoelker if (parsefile != file) 510eaa34893SJilles Tjoelker error("popfilesupto() misused"); 511eaa34893SJilles Tjoelker } 512eaa34893SJilles Tjoelker 513eaa34893SJilles Tjoelker /* 5144b88c807SRodney W. Grimes * Return to top level. 5154b88c807SRodney W. Grimes */ 5164b88c807SRodney W. Grimes 5174b88c807SRodney W. Grimes void 5185134c3f7SWarner Losh popallfiles(void) 5195134c3f7SWarner Losh { 5204b88c807SRodney W. Grimes while (parsefile != &basepf) 5214b88c807SRodney W. Grimes popfile(); 5224b88c807SRodney W. Grimes } 5234b88c807SRodney W. Grimes 5244b88c807SRodney W. Grimes 5254b88c807SRodney W. Grimes 5264b88c807SRodney W. Grimes /* 5274b88c807SRodney W. Grimes * Close the file(s) that the shell is reading commands from. Called 5284b88c807SRodney W. Grimes * after a fork is done. 5294b88c807SRodney W. Grimes */ 5304b88c807SRodney W. Grimes 5314b88c807SRodney W. Grimes void 5325134c3f7SWarner Losh closescript(void) 5335134c3f7SWarner Losh { 5344b88c807SRodney W. Grimes popallfiles(); 5354b88c807SRodney W. Grimes if (parsefile->fd > 0) { 5364b88c807SRodney W. Grimes close(parsefile->fd); 5374b88c807SRodney W. Grimes parsefile->fd = 0; 5384b88c807SRodney W. Grimes } 5394b88c807SRodney W. Grimes } 540