11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
4117987Speter#include <stdio.h>	/* defines BUFSIZ */
4217987Speter#include <fcntl.h>
4317987Speter#include <errno.h>
4417987Speter#include <unistd.h>
4517987Speter#include <stdlib.h>
4617987Speter#include <string.h>
4717987Speter
481556Srgrimes/*
491556Srgrimes * This file implements the input routines used by the parser.
501556Srgrimes */
511556Srgrimes
521556Srgrimes#include "shell.h"
5317987Speter#include "redir.h"
541556Srgrimes#include "syntax.h"
551556Srgrimes#include "input.h"
561556Srgrimes#include "output.h"
571556Srgrimes#include "options.h"
581556Srgrimes#include "memalloc.h"
591556Srgrimes#include "error.h"
601556Srgrimes#include "alias.h"
611556Srgrimes#include "parser.h"
621556Srgrimes#include "myhistedit.h"
63100588Stjr#include "trap.h"
641556Srgrimes
651556Srgrimes#define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
661556Srgrimes
671556Srgrimesstruct strpush {
681556Srgrimes	struct strpush *prev;	/* preceding string on stack */
69248980Sjilles	const char *prevstring;
701556Srgrimes	int prevnleft;
7112043Speter	int prevlleft;
721556Srgrimes	struct alias *ap;	/* if push was associated with an alias */
731556Srgrimes};
741556Srgrimes
751556Srgrimes/*
761556Srgrimes * The parsefile structure pointed to by the global variable parsefile
771556Srgrimes * contains information about the current file being read.
781556Srgrimes */
791556Srgrimes
801556Srgrimesstruct parsefile {
811556Srgrimes	struct parsefile *prev;	/* preceding file on stack */
821556Srgrimes	int linno;		/* current line */
831556Srgrimes	int fd;			/* file descriptor (or -1 if string) */
8420425Ssteve	int nleft;		/* number of chars left in this line */
8520425Ssteve	int lleft;		/* number of lines left in this buffer */
86248980Sjilles	const char *nextc;	/* next char in buffer */
871556Srgrimes	char *buf;		/* input buffer */
881556Srgrimes	struct strpush *strpush; /* for pushing strings at this level */
891556Srgrimes	struct strpush basestrpush; /* so pushing one is fast */
901556Srgrimes};
911556Srgrimes
921556Srgrimes
931556Srgrimesint plinno = 1;			/* input line number */
94201053Sjillesint parsenleft;			/* copy of parsefile->nleft */
95253658Sjillesstatic int parselleft;		/* copy of parsefile->lleft */
96248980Sjillesconst char *parsenextc;		/* copy of parsefile->nextc */
97245676Sjillesstatic char basebuf[BUFSIZ + 1];/* buffer for top level input file */
98245676Sjillesstatic struct parsefile basepf = {	/* top level input file */
99245676Sjilles	.nextc = basebuf,
100245676Sjilles	.buf = basebuf
101245676Sjilles};
102213760Sobrienstatic struct parsefile *parsefile = &basepf;	/* current input file */
1031556Srgrimesint whichprompt;		/* 1 == PS1, 2 == PS2 */
1041556Srgrimes
1051556SrgrimesEditLine *el;			/* cookie for editline package */
1061556Srgrimes
107213811Sobrienstatic void pushfile(void);
108213811Sobrienstatic int preadfd(void);
109229220Sjillesstatic void popstring(void);
1101556Srgrimes
111253650Sjillesvoid
112253650Sjillesresetinput(void)
113253650Sjilles{
114194406Sjilles	popallfiles();
115218306Sjilles	parselleft = parsenleft = 0;	/* clear input buffer */
1161556Srgrimes}
1171556Srgrimes
1181556Srgrimes
1191556Srgrimes/*
1201556Srgrimes * Read a line from the script.
1211556Srgrimes */
1221556Srgrimes
1231556Srgrimeschar *
12490111Simppfgets(char *line, int len)
12517987Speter{
12625225Ssteve	char *p = line;
1271556Srgrimes	int nleft = len;
1281556Srgrimes	int c;
1291556Srgrimes
1301556Srgrimes	while (--nleft > 0) {
1311556Srgrimes		c = pgetc_macro();
1321556Srgrimes		if (c == PEOF) {
1331556Srgrimes			if (p == line)
1341556Srgrimes				return NULL;
1351556Srgrimes			break;
1361556Srgrimes		}
1371556Srgrimes		*p++ = c;
1381556Srgrimes		if (c == '\n')
1391556Srgrimes			break;
1401556Srgrimes	}
1411556Srgrimes	*p = '\0';
1421556Srgrimes	return line;
1431556Srgrimes}
1441556Srgrimes
1451556Srgrimes
1461556Srgrimes
1471556Srgrimes/*
1481556Srgrimes * Read a character from the script, returning PEOF on end of file.
1491556Srgrimes * Nul characters in the input are silently discarded.
1501556Srgrimes */
1511556Srgrimes
1521556Srgrimesint
15390111Simppgetc(void)
15420425Ssteve{
1551556Srgrimes	return pgetc_macro();
1561556Srgrimes}
1571556Srgrimes
15820425Ssteve
159213811Sobrienstatic int
16090111Simppreadfd(void)
16112043Speter{
16212043Speter	int nr;
16320425Ssteve	parsenextc = parsefile->buf;
1641556Srgrimes
165100588Stjr#ifndef NO_HISTORY
166100588Stjr	if (el != NULL && gotwinch) {
167100588Stjr		gotwinch = 0;
168100588Stjr		el_resize(el);
169100588Stjr	}
170100588Stjr#endif
17112043Speterretry:
17225225Ssteve#ifndef NO_HISTORY
17312043Speter	if (parsefile->fd == 0 && el) {
174158143Sstefanf		static const char *rl_cp;
175158143Sstefanf		static int el_len;
17612043Speter
17712043Speter		if (rl_cp == NULL)
178158143Sstefanf			rl_cp = el_gets(el, &el_len);
179158143Sstefanf		if (rl_cp == NULL)
180238377Spfg			nr = el_len == 0 ? 0 : -1;
18112043Speter		else {
182158143Sstefanf			nr = el_len;
183230118Sjilles			if (nr > BUFSIZ)
184230118Sjilles				nr = BUFSIZ;
185248980Sjilles			memcpy(parsefile->buf, rl_cp, nr);
186158143Sstefanf			if (nr != el_len) {
187158143Sstefanf				el_len -= nr;
188158143Sstefanf				rl_cp += nr;
189158143Sstefanf			} else
190158143Sstefanf				rl_cp = NULL;
19112043Speter		}
19225225Ssteve	} else
19325225Ssteve#endif
194248980Sjilles		nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
19512043Speter
19612043Speter	if (nr <= 0) {
19712043Speter                if (nr < 0) {
19812043Speter                        if (errno == EINTR)
19912043Speter                                goto retry;
20012043Speter                        if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
20112043Speter                                int flags = fcntl(0, F_GETFL, 0);
20212043Speter                                if (flags >= 0 && flags & O_NONBLOCK) {
20312043Speter                                        flags &=~ O_NONBLOCK;
20412043Speter                                        if (fcntl(0, F_SETFL, flags) >= 0) {
205199629Sjilles						out2fmt_flush("sh: turning off NDELAY mode\n");
20612043Speter                                                goto retry;
20712043Speter                                        }
20812043Speter                                }
20912043Speter                        }
21012043Speter                }
21120425Ssteve                nr = -1;
21212043Speter	}
21312043Speter	return nr;
21412043Speter}
21512043Speter
2161556Srgrimes/*
2171556Srgrimes * Refill the input buffer and return the next input character:
2181556Srgrimes *
2191556Srgrimes * 1) If a string was pushed back on the input, pop it;
2201556Srgrimes * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
2211556Srgrimes *    from a string so we can't refill the buffer, return EOF.
22220425Ssteve * 3) If there is more in this buffer, use it else call read to fill it.
22320425Ssteve * 4) Process input up to the next newline, deleting nul characters.
2241556Srgrimes */
2251556Srgrimes
2261556Srgrimesint
22790111Simppreadbuffer(void)
22820425Ssteve{
22912043Speter	char *p, *q;
23012043Speter	int more;
23112043Speter	int something;
23212043Speter	char savec;
2331556Srgrimes
2341556Srgrimes	if (parsefile->strpush) {
2351556Srgrimes		popstring();
2361556Srgrimes		if (--parsenleft >= 0)
2371556Srgrimes			return (*parsenextc++);
2381556Srgrimes	}
2391556Srgrimes	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
2401556Srgrimes		return PEOF;
2411556Srgrimes	flushout(&output);
2421556Srgrimes	flushout(&errout);
2431556Srgrimes
24412043Speteragain:
24512043Speter	if (parselleft <= 0) {
24625225Ssteve		if ((parselleft = preadfd()) == -1) {
24712043Speter			parselleft = parsenleft = EOF_NLEFT;
24812043Speter			return PEOF;
2491556Srgrimes		}
2501556Srgrimes	}
2511556Srgrimes
252248980Sjilles	q = p = parsefile->buf + (parsenextc - parsefile->buf);
25312043Speter
2541556Srgrimes	/* delete nul characters */
2551556Srgrimes	something = 0;
25612043Speter	for (more = 1; more;) {
25712043Speter		switch (*p) {
25812043Speter		case '\0':
25912043Speter			p++;	/* Skip nul */
26012043Speter			goto check;
26112043Speter
26212043Speter		case '\t':
26312043Speter		case ' ':
2641556Srgrimes			break;
26512043Speter
26612043Speter		case '\n':
26712043Speter			parsenleft = q - parsenextc;
26812043Speter			more = 0; /* Stop processing here */
26912043Speter			break;
27012043Speter
27112043Speter		default:
2721556Srgrimes			something = 1;
27312043Speter			break;
2741556Srgrimes		}
27512043Speter
27612043Speter		*q++ = *p++;
27712043Spetercheck:
27812043Speter		if (--parselleft <= 0) {
27912043Speter			parsenleft = q - parsenextc - 1;
28012043Speter			if (parsenleft < 0)
28112043Speter				goto again;
28212043Speter			*q = '\0';
28312043Speter			more = 0;
28412043Speter		}
2851556Srgrimes	}
28612043Speter
28712043Speter	savec = *q;
2881556Srgrimes	*q = '\0';
2891556Srgrimes
29018018Speter#ifndef NO_HISTORY
2911556Srgrimes	if (parsefile->fd == 0 && hist && something) {
29284261Sobrien		HistEvent he;
2931556Srgrimes		INTOFF;
29484261Sobrien		history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
29584261Sobrien		    parsenextc);
2961556Srgrimes		INTON;
2971556Srgrimes	}
29818018Speter#endif
29912043Speter
3001556Srgrimes	if (vflag) {
30112043Speter		out2str(parsenextc);
3021556Srgrimes		flushout(out2);
3031556Srgrimes	}
30412043Speter
30512043Speter	*q = savec;
30612043Speter
3071556Srgrimes	return *parsenextc++;
3081556Srgrimes}
3091556Srgrimes
3101556Srgrimes/*
311194128Sjilles * Returns if we are certain we are at EOF. Does not cause any more input
312194128Sjilles * to be read from the outside world.
313194128Sjilles */
314194128Sjilles
315194128Sjillesint
316194128Sjillespreadateof(void)
317194128Sjilles{
318194128Sjilles	if (parsenleft > 0)
319194128Sjilles		return 0;
320194128Sjilles	if (parsefile->strpush)
321194128Sjilles		return 0;
322194128Sjilles	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
323194128Sjilles		return 1;
324194128Sjilles	return 0;
325194128Sjilles}
326194128Sjilles
327194128Sjilles/*
3281556Srgrimes * Undo the last call to pgetc.  Only one character may be pushed back.
3291556Srgrimes * PEOF may be pushed back.
3301556Srgrimes */
3311556Srgrimes
3321556Srgrimesvoid
33390111Simppungetc(void)
33490111Simp{
3351556Srgrimes	parsenleft++;
3361556Srgrimes	parsenextc--;
3371556Srgrimes}
3381556Srgrimes
3391556Srgrimes/*
3401556Srgrimes * Push a string back onto the input at this current parsefile level.
3411556Srgrimes * We handle aliases this way.
3421556Srgrimes */
3431556Srgrimesvoid
344242895Sjillespushstring(char *s, int len, struct alias *ap)
34590111Simp{
3461556Srgrimes	struct strpush *sp;
3471556Srgrimes
3481556Srgrimes	INTOFF;
349199629Sjilles/*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
3501556Srgrimes	if (parsefile->strpush) {
3511556Srgrimes		sp = ckmalloc(sizeof (struct strpush));
3521556Srgrimes		sp->prev = parsefile->strpush;
3531556Srgrimes		parsefile->strpush = sp;
3541556Srgrimes	} else
3551556Srgrimes		sp = parsefile->strpush = &(parsefile->basestrpush);
3561556Srgrimes	sp->prevstring = parsenextc;
3571556Srgrimes	sp->prevnleft = parsenleft;
35812043Speter	sp->prevlleft = parselleft;
359242895Sjilles	sp->ap = ap;
3601556Srgrimes	if (ap)
361242895Sjilles		ap->flag |= ALIASINUSE;
3621556Srgrimes	parsenextc = s;
3631556Srgrimes	parsenleft = len;
3641556Srgrimes	INTON;
3651556Srgrimes}
3661556Srgrimes
367229220Sjillesstatic void
36890111Simppopstring(void)
3691556Srgrimes{
3701556Srgrimes	struct strpush *sp = parsefile->strpush;
3711556Srgrimes
3721556Srgrimes	INTOFF;
3731556Srgrimes	parsenextc = sp->prevstring;
3741556Srgrimes	parsenleft = sp->prevnleft;
37512043Speter	parselleft = sp->prevlleft;
376199629Sjilles/*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
3771556Srgrimes	if (sp->ap)
3781556Srgrimes		sp->ap->flag &= ~ALIASINUSE;
3791556Srgrimes	parsefile->strpush = sp->prev;
3801556Srgrimes	if (sp != &(parsefile->basestrpush))
3811556Srgrimes		ckfree(sp);
3821556Srgrimes	INTON;
3831556Srgrimes}
3841556Srgrimes
3851556Srgrimes/*
3861556Srgrimes * Set the input to take input from a file.  If push is set, push the
3871556Srgrimes * old input onto the stack first.
3881556Srgrimes */
3891556Srgrimes
3901556Srgrimesvoid
391200956Sjillessetinputfile(const char *fname, int push)
39217987Speter{
3931556Srgrimes	int fd;
3941556Srgrimes	int fd2;
3951556Srgrimes
3961556Srgrimes	INTOFF;
397250267Sjilles	if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0)
398222684Sjilles		error("cannot open %s: %s", fname, strerror(errno));
3991556Srgrimes	if (fd < 10) {
400250267Sjilles		fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
4011556Srgrimes		close(fd);
4021556Srgrimes		if (fd2 < 0)
4031556Srgrimes			error("Out of file descriptors");
4041556Srgrimes		fd = fd2;
4051556Srgrimes	}
4061556Srgrimes	setinputfd(fd, push);
4071556Srgrimes	INTON;
4081556Srgrimes}
4091556Srgrimes
4101556Srgrimes
4111556Srgrimes/*
412250267Sjilles * Like setinputfile, but takes an open file descriptor (which should have
413250267Sjilles * its FD_CLOEXEC flag already set).  Call this with interrupts off.
4141556Srgrimes */
4151556Srgrimes
4161556Srgrimesvoid
41790111Simpsetinputfd(int fd, int push)
41817987Speter{
4191556Srgrimes	if (push) {
4201556Srgrimes		pushfile();
421230118Sjilles		parsefile->buf = ckmalloc(BUFSIZ + 1);
4221556Srgrimes	}
4231556Srgrimes	if (parsefile->fd > 0)
4241556Srgrimes		close(parsefile->fd);
4251556Srgrimes	parsefile->fd = fd;
4261556Srgrimes	if (parsefile->buf == NULL)
427230118Sjilles		parsefile->buf = ckmalloc(BUFSIZ + 1);
42812043Speter	parselleft = parsenleft = 0;
4291556Srgrimes	plinno = 1;
4301556Srgrimes}
4311556Srgrimes
4321556Srgrimes
4331556Srgrimes/*
4341556Srgrimes * Like setinputfile, but takes input from a string.
4351556Srgrimes */
4361556Srgrimes
4371556Srgrimesvoid
438248980Sjillessetinputstring(const char *string, int push)
43990111Simp{
4401556Srgrimes	INTOFF;
4411556Srgrimes	if (push)
4421556Srgrimes		pushfile();
4431556Srgrimes	parsenextc = string;
44412043Speter	parselleft = parsenleft = strlen(string);
4451556Srgrimes	parsefile->buf = NULL;
4461556Srgrimes	plinno = 1;
4471556Srgrimes	INTON;
4481556Srgrimes}
4491556Srgrimes
4501556Srgrimes
4511556Srgrimes
4521556Srgrimes/*
4531556Srgrimes * To handle the "." command, a stack of input files is used.  Pushfile
4541556Srgrimes * adds a new entry to the stack and popfile restores the previous level.
4551556Srgrimes */
4561556Srgrimes
457213811Sobrienstatic void
45890111Simppushfile(void)
45990111Simp{
4601556Srgrimes	struct parsefile *pf;
4611556Srgrimes
4621556Srgrimes	parsefile->nleft = parsenleft;
46312043Speter	parsefile->lleft = parselleft;
4641556Srgrimes	parsefile->nextc = parsenextc;
4651556Srgrimes	parsefile->linno = plinno;
4661556Srgrimes	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
4671556Srgrimes	pf->prev = parsefile;
4681556Srgrimes	pf->fd = -1;
4691556Srgrimes	pf->strpush = NULL;
4701556Srgrimes	pf->basestrpush.prev = NULL;
4711556Srgrimes	parsefile = pf;
4721556Srgrimes}
4731556Srgrimes
4741556Srgrimes
4751556Srgrimesvoid
47690111Simppopfile(void)
47790111Simp{
4781556Srgrimes	struct parsefile *pf = parsefile;
4791556Srgrimes
4801556Srgrimes	INTOFF;
4811556Srgrimes	if (pf->fd >= 0)
4821556Srgrimes		close(pf->fd);
4831556Srgrimes	if (pf->buf)
4841556Srgrimes		ckfree(pf->buf);
4851556Srgrimes	while (pf->strpush)
4861556Srgrimes		popstring();
4871556Srgrimes	parsefile = pf->prev;
4881556Srgrimes	ckfree(pf);
4891556Srgrimes	parsenleft = parsefile->nleft;
49012043Speter	parselleft = parsefile->lleft;
4911556Srgrimes	parsenextc = parsefile->nextc;
4921556Srgrimes	plinno = parsefile->linno;
4931556Srgrimes	INTON;
4941556Srgrimes}
4951556Srgrimes
4961556Srgrimes
4971556Srgrimes/*
498199647Sjilles * Return current file (to go back to it later using popfilesupto()).
499199647Sjilles */
500199647Sjilles
501199647Sjillesstruct parsefile *
502199647Sjillesgetcurrentfile(void)
503199647Sjilles{
504199647Sjilles	return parsefile;
505199647Sjilles}
506199647Sjilles
507199647Sjilles
508199647Sjilles/*
509199647Sjilles * Pop files until the given file is on top again. Useful for regular
510199647Sjilles * builtins that read shell commands from files or strings.
511199647Sjilles * If the given file is not an active file, an error is raised.
512199647Sjilles */
513199647Sjilles
514199647Sjillesvoid
515199647Sjillespopfilesupto(struct parsefile *file)
516199647Sjilles{
517199647Sjilles	while (parsefile != file && parsefile != &basepf)
518199647Sjilles		popfile();
519199647Sjilles	if (parsefile != file)
520199647Sjilles		error("popfilesupto() misused");
521199647Sjilles}
522199647Sjilles
523199647Sjilles/*
5241556Srgrimes * Return to top level.
5251556Srgrimes */
5261556Srgrimes
5271556Srgrimesvoid
52890111Simppopallfiles(void)
52990111Simp{
5301556Srgrimes	while (parsefile != &basepf)
5311556Srgrimes		popfile();
5321556Srgrimes}
5331556Srgrimes
5341556Srgrimes
5351556Srgrimes
5361556Srgrimes/*
5371556Srgrimes * Close the file(s) that the shell is reading commands from.  Called
5381556Srgrimes * after a fork is done.
5391556Srgrimes */
5401556Srgrimes
5411556Srgrimesvoid
54290111Simpclosescript(void)
54390111Simp{
5441556Srgrimes	popallfiles();
5451556Srgrimes	if (parsefile->fd > 0) {
5461556Srgrimes		close(parsefile->fd);
5471556Srgrimes		parsefile->fd = 0;
5481556Srgrimes	}
5491556Srgrimes}
550