parser.c revision 36150
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 * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4236150Scharnier	"$Id$";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <stdlib.h>
4617987Speter
471556Srgrimes#include "shell.h"
481556Srgrimes#include "parser.h"
491556Srgrimes#include "nodes.h"
501556Srgrimes#include "expand.h"	/* defines rmescapes() */
511556Srgrimes#include "redir.h"	/* defines copyfd() */
521556Srgrimes#include "syntax.h"
531556Srgrimes#include "options.h"
541556Srgrimes#include "input.h"
551556Srgrimes#include "output.h"
561556Srgrimes#include "var.h"
571556Srgrimes#include "error.h"
581556Srgrimes#include "memalloc.h"
591556Srgrimes#include "mystring.h"
601556Srgrimes#include "alias.h"
6117987Speter#include "show.h"
6217987Speter#ifndef NO_HISTORY
631556Srgrimes#include "myhistedit.h"
6417987Speter#endif
651556Srgrimes
661556Srgrimes/*
671556Srgrimes * Shell command parser.
681556Srgrimes */
691556Srgrimes
701556Srgrimes#define EOFMARKLEN 79
711556Srgrimes
721556Srgrimes/* values returned by readtoken */
7317987Speter#include "token.h"
741556Srgrimes
751556Srgrimes
761556Srgrimes
771556Srgrimesstruct heredoc {
781556Srgrimes	struct heredoc *next;	/* next here document in list */
791556Srgrimes	union node *here;		/* redirection node */
801556Srgrimes	char *eofmark;		/* string indicating end of input */
811556Srgrimes	int striptabs;		/* if set, strip leading tabs */
821556Srgrimes};
831556Srgrimes
841556Srgrimes
851556Srgrimes
861556Srgrimesstruct heredoc *heredoclist;	/* list of here documents to read */
871556Srgrimesint parsebackquote;		/* nonzero if we are inside backquotes */
881556Srgrimesint doprompt;			/* if set, prompt the user */
891556Srgrimesint needprompt;			/* true if interactive and at start of line */
901556Srgrimesint lasttoken;			/* last token read */
911556SrgrimesMKINIT int tokpushback;		/* last token pushed back */
921556Srgrimeschar *wordtext;			/* text of last word returned by readtoken */
931556SrgrimesMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
941556Srgrimesstruct nodelist *backquotelist;
951556Srgrimesunion node *redirnode;
961556Srgrimesstruct heredoc *heredoc;
971556Srgrimesint quoteflag;			/* set if (part of) last token was quoted */
981556Srgrimesint startlinno;			/* line # where last token started */
991556Srgrimes
10018018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
10118018Speterstatic int noaliases = 0;
1021556Srgrimes
1031556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
1041556Srgrimes#ifdef GDB_HACK
1051556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
1061556Srgrimesstatic const char types[] = "}-+?=";
1071556Srgrimes#endif
1081556Srgrimes
1091556Srgrimes
1101556SrgrimesSTATIC union node *list __P((int));
1111556SrgrimesSTATIC union node *andor __P((void));
1121556SrgrimesSTATIC union node *pipeline __P((void));
1131556SrgrimesSTATIC union node *command __P((void));
1141556SrgrimesSTATIC union node *simplecmd __P((union node **, union node *));
11517987SpeterSTATIC union node *makename __P((void));
1161556SrgrimesSTATIC void parsefname __P((void));
1171556SrgrimesSTATIC void parseheredoc __P((void));
11817987SpeterSTATIC int peektoken __P((void));
1191556SrgrimesSTATIC int readtoken __P((void));
12017987SpeterSTATIC int xxreadtoken __P((void));
1211556SrgrimesSTATIC int readtoken1 __P((int, char const *, char *, int));
1221556SrgrimesSTATIC int noexpand __P((char *));
1231556SrgrimesSTATIC void synexpect __P((int));
1241556SrgrimesSTATIC void synerror __P((char *));
12520425SsteveSTATIC void setprompt __P((int));
1261556Srgrimes
12717987Speter
1281556Srgrimes/*
1291556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
1301556Srgrimes * valid parse tree indicating a blank line.)
1311556Srgrimes */
1321556Srgrimes
1331556Srgrimesunion node *
13420425Ssteveparsecmd(interact)
13517987Speter	int interact;
13617987Speter{
1371556Srgrimes	int t;
1381556Srgrimes
1391556Srgrimes	doprompt = interact;
1401556Srgrimes	if (doprompt)
1411556Srgrimes		setprompt(1);
1421556Srgrimes	else
1431556Srgrimes		setprompt(0);
1441556Srgrimes	needprompt = 0;
1451556Srgrimes	t = readtoken();
1461556Srgrimes	if (t == TEOF)
1471556Srgrimes		return NEOF;
1481556Srgrimes	if (t == TNL)
1491556Srgrimes		return NULL;
1501556Srgrimes	tokpushback++;
1511556Srgrimes	return list(1);
1521556Srgrimes}
1531556Srgrimes
1541556Srgrimes
1551556SrgrimesSTATIC union node *
15620425Sstevelist(nlflag)
15717987Speter	int nlflag;
15817987Speter{
1591556Srgrimes	union node *n1, *n2, *n3;
16017987Speter	int tok;
1611556Srgrimes
1621556Srgrimes	checkkwd = 2;
1631556Srgrimes	if (nlflag == 0 && tokendlist[peektoken()])
1641556Srgrimes		return NULL;
16517987Speter	n1 = NULL;
1661556Srgrimes	for (;;) {
16717987Speter		n2 = andor();
16817987Speter		tok = readtoken();
16917987Speter		if (tok == TBACKGND) {
17017987Speter			if (n2->type == NCMD || n2->type == NPIPE) {
17117987Speter				n2->ncmd.backgnd = 1;
17217987Speter			} else if (n2->type == NREDIR) {
17317987Speter				n2->type = NBACKGND;
17417987Speter			} else {
17517987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
17617987Speter				n3->type = NBACKGND;
17717987Speter				n3->nredir.n = n2;
17817987Speter				n3->nredir.redirect = NULL;
17917987Speter				n2 = n3;
18017987Speter			}
18117987Speter		}
18217987Speter		if (n1 == NULL) {
18317987Speter			n1 = n2;
18417987Speter		}
18517987Speter		else {
18617987Speter			n3 = (union node *)stalloc(sizeof (struct nbinary));
18717987Speter			n3->type = NSEMI;
18817987Speter			n3->nbinary.ch1 = n1;
18917987Speter			n3->nbinary.ch2 = n2;
19017987Speter			n1 = n3;
19117987Speter		}
19217987Speter		switch (tok) {
19313882Sjoerg		case TBACKGND:
19417987Speter		case TSEMI:
19517987Speter			tok = readtoken();
19617987Speter			/* fall through */
1971556Srgrimes		case TNL:
19817987Speter			if (tok == TNL) {
19917987Speter				parseheredoc();
20017987Speter				if (nlflag)
20117987Speter					return n1;
20217987Speter			} else {
20317987Speter				tokpushback++;
20417987Speter			}
2051556Srgrimes			checkkwd = 2;
2061556Srgrimes			if (tokendlist[peektoken()])
2071556Srgrimes				return n1;
2081556Srgrimes			break;
2091556Srgrimes		case TEOF:
2101556Srgrimes			if (heredoclist)
2111556Srgrimes				parseheredoc();
2121556Srgrimes			else
2131556Srgrimes				pungetc();		/* push back EOF on input */
2141556Srgrimes			return n1;
2151556Srgrimes		default:
2161556Srgrimes			if (nlflag)
2171556Srgrimes				synexpect(-1);
2181556Srgrimes			tokpushback++;
2191556Srgrimes			return n1;
2201556Srgrimes		}
2211556Srgrimes	}
2221556Srgrimes}
2231556Srgrimes
2241556Srgrimes
2251556Srgrimes
2261556SrgrimesSTATIC union node *
2271556Srgrimesandor() {
2281556Srgrimes	union node *n1, *n2, *n3;
2291556Srgrimes	int t;
2301556Srgrimes
2311556Srgrimes	n1 = pipeline();
2321556Srgrimes	for (;;) {
2331556Srgrimes		if ((t = readtoken()) == TAND) {
2341556Srgrimes			t = NAND;
2351556Srgrimes		} else if (t == TOR) {
2361556Srgrimes			t = NOR;
2371556Srgrimes		} else {
2381556Srgrimes			tokpushback++;
2391556Srgrimes			return n1;
2401556Srgrimes		}
2411556Srgrimes		n2 = pipeline();
2421556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
2431556Srgrimes		n3->type = t;
2441556Srgrimes		n3->nbinary.ch1 = n1;
2451556Srgrimes		n3->nbinary.ch2 = n2;
2461556Srgrimes		n1 = n3;
2471556Srgrimes	}
2481556Srgrimes}
2491556Srgrimes
2501556Srgrimes
2511556Srgrimes
2521556SrgrimesSTATIC union node *
2531556Srgrimespipeline() {
25425230Ssteve	union node *n1, *pipenode, *notnode;
2551556Srgrimes	struct nodelist *lp, *prev;
25625230Ssteve	int negate = 0;
2571556Srgrimes
2581556Srgrimes	TRACE(("pipeline: entered\n"));
25925230Ssteve	while (readtoken() == TNOT) {
26025230Ssteve		TRACE(("pipeline: TNOT recognized\n"));
26125230Ssteve		negate = !negate;
26225230Ssteve	}
26325230Ssteve	tokpushback++;
2641556Srgrimes	n1 = command();
2651556Srgrimes	if (readtoken() == TPIPE) {
2661556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
2671556Srgrimes		pipenode->type = NPIPE;
2681556Srgrimes		pipenode->npipe.backgnd = 0;
2691556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2701556Srgrimes		pipenode->npipe.cmdlist = lp;
2711556Srgrimes		lp->n = n1;
2721556Srgrimes		do {
2731556Srgrimes			prev = lp;
2741556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2751556Srgrimes			lp->n = command();
2761556Srgrimes			prev->next = lp;
2771556Srgrimes		} while (readtoken() == TPIPE);
2781556Srgrimes		lp->next = NULL;
2791556Srgrimes		n1 = pipenode;
2801556Srgrimes	}
2811556Srgrimes	tokpushback++;
28225230Ssteve	if (negate) {
28325230Ssteve		notnode = (union node *)stalloc(sizeof(struct nnot));
28425230Ssteve		notnode->type = NNOT;
28525230Ssteve		notnode->nnot.com = n1;
28625230Ssteve		n1 = notnode;
28725230Ssteve	}
2881556Srgrimes	return n1;
2891556Srgrimes}
2901556Srgrimes
2911556Srgrimes
2921556Srgrimes
2931556SrgrimesSTATIC union node *
2941556Srgrimescommand() {
2951556Srgrimes	union node *n1, *n2;
2961556Srgrimes	union node *ap, **app;
2971556Srgrimes	union node *cp, **cpp;
2981556Srgrimes	union node *redir, **rpp;
29925230Ssteve	int t;
3001556Srgrimes
3011556Srgrimes	checkkwd = 2;
30217987Speter	redir = NULL;
30317987Speter	n1 = NULL;
3041556Srgrimes	rpp = &redir;
30520425Ssteve
3061556Srgrimes	/* Check for redirection which may precede command */
3071556Srgrimes	while (readtoken() == TREDIR) {
3081556Srgrimes		*rpp = n2 = redirnode;
3091556Srgrimes		rpp = &n2->nfile.next;
3101556Srgrimes		parsefname();
3111556Srgrimes	}
3121556Srgrimes	tokpushback++;
3131556Srgrimes
3141556Srgrimes	switch (readtoken()) {
3151556Srgrimes	case TIF:
3161556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3171556Srgrimes		n1->type = NIF;
3181556Srgrimes		n1->nif.test = list(0);
3191556Srgrimes		if (readtoken() != TTHEN)
3201556Srgrimes			synexpect(TTHEN);
3211556Srgrimes		n1->nif.ifpart = list(0);
3221556Srgrimes		n2 = n1;
3231556Srgrimes		while (readtoken() == TELIF) {
3241556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
3251556Srgrimes			n2 = n2->nif.elsepart;
3261556Srgrimes			n2->type = NIF;
3271556Srgrimes			n2->nif.test = list(0);
3281556Srgrimes			if (readtoken() != TTHEN)
3291556Srgrimes				synexpect(TTHEN);
3301556Srgrimes			n2->nif.ifpart = list(0);
3311556Srgrimes		}
3321556Srgrimes		if (lasttoken == TELSE)
3331556Srgrimes			n2->nif.elsepart = list(0);
3341556Srgrimes		else {
3351556Srgrimes			n2->nif.elsepart = NULL;
3361556Srgrimes			tokpushback++;
3371556Srgrimes		}
3381556Srgrimes		if (readtoken() != TFI)
3391556Srgrimes			synexpect(TFI);
3401556Srgrimes		checkkwd = 1;
3411556Srgrimes		break;
3421556Srgrimes	case TWHILE:
3431556Srgrimes	case TUNTIL: {
3441556Srgrimes		int got;
3451556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
3461556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
3471556Srgrimes		n1->nbinary.ch1 = list(0);
3481556Srgrimes		if ((got=readtoken()) != TDO) {
3491556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
3501556Srgrimes			synexpect(TDO);
3511556Srgrimes		}
3521556Srgrimes		n1->nbinary.ch2 = list(0);
3531556Srgrimes		if (readtoken() != TDONE)
3541556Srgrimes			synexpect(TDONE);
3551556Srgrimes		checkkwd = 1;
3561556Srgrimes		break;
3571556Srgrimes	}
3581556Srgrimes	case TFOR:
3591556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
3601556Srgrimes			synerror("Bad for loop variable");
3611556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
3621556Srgrimes		n1->type = NFOR;
3631556Srgrimes		n1->nfor.var = wordtext;
3641556Srgrimes		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
3651556Srgrimes			app = &ap;
3661556Srgrimes			while (readtoken() == TWORD) {
3671556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
3681556Srgrimes				n2->type = NARG;
3691556Srgrimes				n2->narg.text = wordtext;
3701556Srgrimes				n2->narg.backquote = backquotelist;
3711556Srgrimes				*app = n2;
3721556Srgrimes				app = &n2->narg.next;
3731556Srgrimes			}
3741556Srgrimes			*app = NULL;
3751556Srgrimes			n1->nfor.args = ap;
3761556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3771556Srgrimes				synexpect(-1);
3781556Srgrimes		} else {
3791556Srgrimes#ifndef GDB_HACK
3801556Srgrimes			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
3811556Srgrimes								   '@', '=', '\0'};
3821556Srgrimes#endif
3831556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
3841556Srgrimes			n2->type = NARG;
3851556Srgrimes			n2->narg.text = (char *)argvars;
3861556Srgrimes			n2->narg.backquote = NULL;
3871556Srgrimes			n2->narg.next = NULL;
3881556Srgrimes			n1->nfor.args = n2;
3891556Srgrimes			/*
3901556Srgrimes			 * Newline or semicolon here is optional (but note
3911556Srgrimes			 * that the original Bourne shell only allowed NL).
3921556Srgrimes			 */
3931556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3941556Srgrimes				tokpushback++;
3951556Srgrimes		}
3961556Srgrimes		checkkwd = 2;
3971556Srgrimes		if ((t = readtoken()) == TDO)
3981556Srgrimes			t = TDONE;
3991556Srgrimes		else if (t == TBEGIN)
4001556Srgrimes			t = TEND;
4011556Srgrimes		else
4021556Srgrimes			synexpect(-1);
4031556Srgrimes		n1->nfor.body = list(0);
4041556Srgrimes		if (readtoken() != t)
4051556Srgrimes			synexpect(t);
4061556Srgrimes		checkkwd = 1;
4071556Srgrimes		break;
4081556Srgrimes	case TCASE:
4091556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4101556Srgrimes		n1->type = NCASE;
4111556Srgrimes		if (readtoken() != TWORD)
4121556Srgrimes			synexpect(TWORD);
4131556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
4141556Srgrimes		n2->type = NARG;
4151556Srgrimes		n2->narg.text = wordtext;
4161556Srgrimes		n2->narg.backquote = backquotelist;
4171556Srgrimes		n2->narg.next = NULL;
4181556Srgrimes		while (readtoken() == TNL);
4191556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
4201556Srgrimes			synerror("expecting \"in\"");
4211556Srgrimes		cpp = &n1->ncase.cases;
42218018Speter		noaliases = 1;	/* turn off alias expansion */
4232760Ssef		checkkwd = 2, readtoken();
4242760Ssef		do {
4251556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
4261556Srgrimes			cp->type = NCLIST;
4271556Srgrimes			app = &cp->nclist.pattern;
4281556Srgrimes			for (;;) {
4291556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
4301556Srgrimes				ap->type = NARG;
4311556Srgrimes				ap->narg.text = wordtext;
4321556Srgrimes				ap->narg.backquote = backquotelist;
4332760Ssef				if (checkkwd = 2, readtoken() != TPIPE)
4341556Srgrimes					break;
4351556Srgrimes				app = &ap->narg.next;
4362760Ssef				readtoken();
4371556Srgrimes			}
4381556Srgrimes			ap->narg.next = NULL;
4391556Srgrimes			if (lasttoken != TRP)
44018018Speter				noaliases = 0, synexpect(TRP);
4411556Srgrimes			cp->nclist.body = list(0);
4422760Ssef
4432760Ssef			checkkwd = 2;
4442760Ssef			if ((t = readtoken()) != TESAC) {
4452760Ssef				if (t != TENDCASE)
44618018Speter					noaliases = 0, synexpect(TENDCASE);
4472760Ssef				else
4482760Ssef					checkkwd = 2, readtoken();
4492760Ssef			}
4501556Srgrimes			cpp = &cp->nclist.next;
4512760Ssef		} while(lasttoken != TESAC);
45218018Speter		noaliases = 0;	/* reset alias expansion */
4531556Srgrimes		*cpp = NULL;
4541556Srgrimes		checkkwd = 1;
4551556Srgrimes		break;
4561556Srgrimes	case TLP:
4571556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
4581556Srgrimes		n1->type = NSUBSHELL;
4591556Srgrimes		n1->nredir.n = list(0);
4601556Srgrimes		n1->nredir.redirect = NULL;
4611556Srgrimes		if (readtoken() != TRP)
4621556Srgrimes			synexpect(TRP);
4631556Srgrimes		checkkwd = 1;
4641556Srgrimes		break;
4651556Srgrimes	case TBEGIN:
4661556Srgrimes		n1 = list(0);
4671556Srgrimes		if (readtoken() != TEND)
4681556Srgrimes			synexpect(TEND);
4691556Srgrimes		checkkwd = 1;
4701556Srgrimes		break;
4711556Srgrimes	/* Handle an empty command like other simple commands.  */
47217987Speter	case TSEMI:
47317987Speter		/*
47417987Speter		 * An empty command before a ; doesn't make much sense, and
47517987Speter		 * should certainly be disallowed in the case of `if ;'.
47617987Speter		 */
47717987Speter		if (!redir)
47817987Speter			synexpect(-1);
47920425Ssteve	case TAND:
48020425Ssteve	case TOR:
4811556Srgrimes	case TNL:
48210399Sjoerg	case TEOF:
4831556Srgrimes	case TWORD:
48417987Speter	case TRP:
4851556Srgrimes		tokpushback++;
48625230Ssteve		return simplecmd(rpp, redir);
4871556Srgrimes	default:
4881556Srgrimes		synexpect(-1);
4891556Srgrimes	}
4901556Srgrimes
4911556Srgrimes	/* Now check for redirection which may follow command */
4921556Srgrimes	while (readtoken() == TREDIR) {
4931556Srgrimes		*rpp = n2 = redirnode;
4941556Srgrimes		rpp = &n2->nfile.next;
4951556Srgrimes		parsefname();
4961556Srgrimes	}
4971556Srgrimes	tokpushback++;
4981556Srgrimes	*rpp = NULL;
4991556Srgrimes	if (redir) {
5001556Srgrimes		if (n1->type != NSUBSHELL) {
5011556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5021556Srgrimes			n2->type = NREDIR;
5031556Srgrimes			n2->nredir.n = n1;
5041556Srgrimes			n1 = n2;
5051556Srgrimes		}
5061556Srgrimes		n1->nredir.redirect = redir;
5071556Srgrimes	}
50825230Ssteve	return n1;
5091556Srgrimes}
5101556Srgrimes
5111556Srgrimes
5121556SrgrimesSTATIC union node *
5138855Srgrimessimplecmd(rpp, redir)
5141556Srgrimes	union node **rpp, *redir;
5151556Srgrimes	{
5161556Srgrimes	union node *args, **app;
5171556Srgrimes	union node **orig_rpp = rpp;
51825230Ssteve	union node *n = NULL;
5191556Srgrimes
5201556Srgrimes	/* If we don't have any redirections already, then we must reset */
5211556Srgrimes	/* rpp to be the address of the local redir variable.  */
5221556Srgrimes	if (redir == 0)
5231556Srgrimes		rpp = &redir;
5241556Srgrimes
5251556Srgrimes	args = NULL;
5261556Srgrimes	app = &args;
5278855Srgrimes	/*
5281556Srgrimes	 * We save the incoming value, because we need this for shell
5291556Srgrimes	 * functions.  There can not be a redirect or an argument between
5308855Srgrimes	 * the function name and the open parenthesis.
5311556Srgrimes	 */
5321556Srgrimes	orig_rpp = rpp;
5331556Srgrimes
5341556Srgrimes	for (;;) {
5351556Srgrimes		if (readtoken() == TWORD) {
5361556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
5371556Srgrimes			n->type = NARG;
5381556Srgrimes			n->narg.text = wordtext;
5391556Srgrimes			n->narg.backquote = backquotelist;
5401556Srgrimes			*app = n;
5411556Srgrimes			app = &n->narg.next;
5421556Srgrimes		} else if (lasttoken == TREDIR) {
5431556Srgrimes			*rpp = n = redirnode;
5441556Srgrimes			rpp = &n->nfile.next;
5451556Srgrimes			parsefname();	/* read name of redirection file */
5461556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
5471556Srgrimes					    && rpp == orig_rpp) {
5481556Srgrimes			/* We have a function */
5491556Srgrimes			if (readtoken() != TRP)
5501556Srgrimes				synexpect(TRP);
5511556Srgrimes#ifdef notdef
5521556Srgrimes			if (! goodname(n->narg.text))
5531556Srgrimes				synerror("Bad function name");
5541556Srgrimes#endif
5551556Srgrimes			n->type = NDEFUN;
5561556Srgrimes			n->narg.next = command();
55725230Ssteve			return n;
5581556Srgrimes		} else {
5591556Srgrimes			tokpushback++;
5601556Srgrimes			break;
5611556Srgrimes		}
5621556Srgrimes	}
5631556Srgrimes	*app = NULL;
5641556Srgrimes	*rpp = NULL;
5651556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
5661556Srgrimes	n->type = NCMD;
5671556Srgrimes	n->ncmd.backgnd = 0;
5681556Srgrimes	n->ncmd.args = args;
5691556Srgrimes	n->ncmd.redirect = redir;
57025230Ssteve	return n;
5711556Srgrimes}
5721556Srgrimes
57317987SpeterSTATIC union node *
57417987Spetermakename() {
57517987Speter	union node *n;
5761556Srgrimes
57717987Speter	n = (union node *)stalloc(sizeof (struct narg));
57817987Speter	n->type = NARG;
57917987Speter	n->narg.next = NULL;
58017987Speter	n->narg.text = wordtext;
58117987Speter	n->narg.backquote = backquotelist;
58217987Speter	return n;
58317987Speter}
58417987Speter
58517987Spetervoid fixredir(n, text, err)
58617987Speter	union node *n;
58717987Speter	const char *text;
58817987Speter	int err;
58917987Speter	{
59017987Speter	TRACE(("Fix redir %s %d\n", text, err));
59117987Speter	if (!err)
59217987Speter		n->ndup.vname = NULL;
59317987Speter
59417987Speter	if (is_digit(text[0]) && text[1] == '\0')
59517987Speter		n->ndup.dupfd = digit_val(text[0]);
59617987Speter	else if (text[0] == '-' && text[1] == '\0')
59717987Speter		n->ndup.dupfd = -1;
59817987Speter	else {
59920425Ssteve
60017987Speter		if (err)
60117987Speter			synerror("Bad fd number");
60217987Speter		else
60317987Speter			n->ndup.vname = makename();
60417987Speter	}
60517987Speter}
60617987Speter
60717987Speter
6081556SrgrimesSTATIC void
6091556Srgrimesparsefname() {
6101556Srgrimes	union node *n = redirnode;
6111556Srgrimes
6121556Srgrimes	if (readtoken() != TWORD)
6131556Srgrimes		synexpect(-1);
6141556Srgrimes	if (n->type == NHERE) {
6151556Srgrimes		struct heredoc *here = heredoc;
6161556Srgrimes		struct heredoc *p;
6171556Srgrimes		int i;
6181556Srgrimes
6191556Srgrimes		if (quoteflag == 0)
6201556Srgrimes			n->type = NXHERE;
6211556Srgrimes		TRACE(("Here document %d\n", n->type));
6221556Srgrimes		if (here->striptabs) {
6231556Srgrimes			while (*wordtext == '\t')
6241556Srgrimes				wordtext++;
6251556Srgrimes		}
6261556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
6271556Srgrimes			synerror("Illegal eof marker for << redirection");
6281556Srgrimes		rmescapes(wordtext);
6291556Srgrimes		here->eofmark = wordtext;
6301556Srgrimes		here->next = NULL;
6311556Srgrimes		if (heredoclist == NULL)
6321556Srgrimes			heredoclist = here;
6331556Srgrimes		else {
6341556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
6351556Srgrimes			p->next = here;
6361556Srgrimes		}
6371556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
63817987Speter		fixredir(n, wordtext, 0);
6391556Srgrimes	} else {
64017987Speter		n->nfile.fname = makename();
6411556Srgrimes	}
6421556Srgrimes}
6431556Srgrimes
6441556Srgrimes
6451556Srgrimes/*
6461556Srgrimes * Input any here documents.
6471556Srgrimes */
6481556Srgrimes
6491556SrgrimesSTATIC void
6501556Srgrimesparseheredoc() {
6511556Srgrimes	struct heredoc *here;
6521556Srgrimes	union node *n;
6531556Srgrimes
6541556Srgrimes	while (heredoclist) {
6551556Srgrimes		here = heredoclist;
6561556Srgrimes		heredoclist = here->next;
6571556Srgrimes		if (needprompt) {
6581556Srgrimes			setprompt(2);
6591556Srgrimes			needprompt = 0;
6601556Srgrimes		}
6611556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
6621556Srgrimes				here->eofmark, here->striptabs);
6631556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
6641556Srgrimes		n->narg.type = NARG;
6651556Srgrimes		n->narg.next = NULL;
6661556Srgrimes		n->narg.text = wordtext;
6671556Srgrimes		n->narg.backquote = backquotelist;
6681556Srgrimes		here->here->nhere.doc = n;
6691556Srgrimes	}
6701556Srgrimes}
6711556Srgrimes
6721556SrgrimesSTATIC int
6731556Srgrimespeektoken() {
6741556Srgrimes	int t;
6751556Srgrimes
6761556Srgrimes	t = readtoken();
6771556Srgrimes	tokpushback++;
6781556Srgrimes	return (t);
6791556Srgrimes}
6801556Srgrimes
6811556SrgrimesSTATIC int xxreadtoken();
6821556Srgrimes
6831556SrgrimesSTATIC int
6841556Srgrimesreadtoken() {
6851556Srgrimes	int t;
6861556Srgrimes	int savecheckkwd = checkkwd;
6871556Srgrimes	struct alias *ap;
6881556Srgrimes#ifdef DEBUG
6891556Srgrimes	int alreadyseen = tokpushback;
6901556Srgrimes#endif
6918855Srgrimes
6921556Srgrimes	top:
6931556Srgrimes	t = xxreadtoken();
6941556Srgrimes
6951556Srgrimes	if (checkkwd) {
6961556Srgrimes		/*
6971556Srgrimes		 * eat newlines
6981556Srgrimes		 */
6991556Srgrimes		if (checkkwd == 2) {
7001556Srgrimes			checkkwd = 0;
7011556Srgrimes			while (t == TNL) {
7021556Srgrimes				parseheredoc();
7031556Srgrimes				t = xxreadtoken();
7041556Srgrimes			}
7051556Srgrimes		} else
7061556Srgrimes			checkkwd = 0;
7071556Srgrimes		/*
7081556Srgrimes		 * check for keywords and aliases
7091556Srgrimes		 */
71020425Ssteve		if (t == TWORD && !quoteflag)
71117987Speter		{
71225230Ssteve			char * const *pp;
7131556Srgrimes
7141556Srgrimes			for (pp = (char **)parsekwd; *pp; pp++) {
71520425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
71617987Speter				{
7171556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
7181556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
7191556Srgrimes					goto out;
7201556Srgrimes				}
7211556Srgrimes			}
72218018Speter			if (noaliases == 0 &&
72318018Speter			    (ap = lookupalias(wordtext, 1)) != NULL) {
7241556Srgrimes				pushstring(ap->val, strlen(ap->val), ap);
7251556Srgrimes				checkkwd = savecheckkwd;
7261556Srgrimes				goto top;
7271556Srgrimes			}
7281556Srgrimes		}
7291556Srgrimesout:
73025230Ssteve		checkkwd = 0;
7311556Srgrimes	}
7321556Srgrimes#ifdef DEBUG
7331556Srgrimes	if (!alreadyseen)
7341556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7351556Srgrimes	else
7361556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7371556Srgrimes#endif
7381556Srgrimes	return (t);
7391556Srgrimes}
7401556Srgrimes
7411556Srgrimes
7421556Srgrimes/*
7431556Srgrimes * Read the next input token.
7441556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
7451556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
7461556Srgrimes *	quoted.
7471556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
7481556Srgrimes *	the redirection.
7491556Srgrimes * In all cases, the variable startlinno is set to the number of the line
7501556Srgrimes *	on which the token starts.
7511556Srgrimes *
7521556Srgrimes * [Change comment:  here documents and internal procedures]
7531556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
7541556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
7551556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
7561556Srgrimes *  We could also make parseoperator in essence the main routine, and
7571556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
7581556Srgrimes */
7591556Srgrimes
7601556Srgrimes#define RETURN(token)	return lasttoken = token
7611556Srgrimes
7621556SrgrimesSTATIC int
7631556Srgrimesxxreadtoken() {
76425230Ssteve	int c;
7651556Srgrimes
7661556Srgrimes	if (tokpushback) {
7671556Srgrimes		tokpushback = 0;
7681556Srgrimes		return lasttoken;
7691556Srgrimes	}
7701556Srgrimes	if (needprompt) {
7711556Srgrimes		setprompt(2);
7721556Srgrimes		needprompt = 0;
7731556Srgrimes	}
7741556Srgrimes	startlinno = plinno;
7751556Srgrimes	for (;;) {	/* until token or start of word found */
7761556Srgrimes		c = pgetc_macro();
7771556Srgrimes		if (c == ' ' || c == '\t')
7781556Srgrimes			continue;		/* quick check for white space first */
7791556Srgrimes		switch (c) {
7801556Srgrimes		case ' ': case '\t':
7811556Srgrimes			continue;
7821556Srgrimes		case '#':
7831556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
7841556Srgrimes			pungetc();
7851556Srgrimes			continue;
7861556Srgrimes		case '\\':
7871556Srgrimes			if (pgetc() == '\n') {
7881556Srgrimes				startlinno = ++plinno;
7891556Srgrimes				if (doprompt)
7901556Srgrimes					setprompt(2);
7911556Srgrimes				else
7921556Srgrimes					setprompt(0);
7931556Srgrimes				continue;
7941556Srgrimes			}
7951556Srgrimes			pungetc();
7961556Srgrimes			goto breakloop;
7971556Srgrimes		case '\n':
7981556Srgrimes			plinno++;
7991556Srgrimes			needprompt = doprompt;
8001556Srgrimes			RETURN(TNL);
8011556Srgrimes		case PEOF:
8021556Srgrimes			RETURN(TEOF);
8031556Srgrimes		case '&':
8041556Srgrimes			if (pgetc() == '&')
8051556Srgrimes				RETURN(TAND);
8061556Srgrimes			pungetc();
8071556Srgrimes			RETURN(TBACKGND);
8081556Srgrimes		case '|':
8091556Srgrimes			if (pgetc() == '|')
8101556Srgrimes				RETURN(TOR);
8111556Srgrimes			pungetc();
8121556Srgrimes			RETURN(TPIPE);
8131556Srgrimes		case ';':
8141556Srgrimes			if (pgetc() == ';')
8151556Srgrimes				RETURN(TENDCASE);
8161556Srgrimes			pungetc();
8171556Srgrimes			RETURN(TSEMI);
8181556Srgrimes		case '(':
8191556Srgrimes			RETURN(TLP);
8201556Srgrimes		case ')':
8211556Srgrimes			RETURN(TRP);
8221556Srgrimes		default:
8231556Srgrimes			goto breakloop;
8241556Srgrimes		}
8251556Srgrimes	}
8261556Srgrimesbreakloop:
8271556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
8281556Srgrimes#undef RETURN
8291556Srgrimes}
8301556Srgrimes
8311556Srgrimes
8321556Srgrimes
8331556Srgrimes/*
8341556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
8351556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
8361556Srgrimes * word which marks the end of the document and striptabs is true if
8371556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
8381556Srgrimes * is the first character of the input token or document.
8391556Srgrimes *
8401556Srgrimes * Because C does not have internal subroutines, I have simulated them
8411556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
8421556Srgrimes * will run code that appears at the end of readtoken1.
8431556Srgrimes */
8441556Srgrimes
8451556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
8461556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
8471556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
8481556Srgrimes#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
8491556Srgrimes#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
8501556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
8511556Srgrimes
8521556SrgrimesSTATIC int
8531556Srgrimesreadtoken1(firstc, syntax, eofmark, striptabs)
8541556Srgrimes	int firstc;
8551556Srgrimes	char const *syntax;
8561556Srgrimes	char *eofmark;
8571556Srgrimes	int striptabs;
8581556Srgrimes	{
85917987Speter	int c = firstc;
86017987Speter	char *out;
8611556Srgrimes	int len;
8621556Srgrimes	char line[EOFMARKLEN + 1];
8631556Srgrimes	struct nodelist *bqlist;
8641556Srgrimes	int quotef;
8651556Srgrimes	int dblquote;
8661556Srgrimes	int varnest;	/* levels of variables expansion */
8671556Srgrimes	int arinest;	/* levels of arithmetic expansion */
8681556Srgrimes	int parenlevel;	/* levels of parens in arithmetic */
8691556Srgrimes	int oldstyle;
8701556Srgrimes	char const *prevsyntax;	/* syntax before arithmetic */
87117987Speter#if __GNUC__
87217987Speter	/* Avoid longjmp clobbering */
87317987Speter	(void) &out;
87417987Speter	(void) &quotef;
87517987Speter	(void) &dblquote;
87617987Speter	(void) &varnest;
87717987Speter	(void) &arinest;
87817987Speter	(void) &parenlevel;
87917987Speter	(void) &oldstyle;
88017987Speter	(void) &prevsyntax;
88117987Speter	(void) &syntax;
88217987Speter#endif
8831556Srgrimes
8841556Srgrimes	startlinno = plinno;
8851556Srgrimes	dblquote = 0;
8861556Srgrimes	if (syntax == DQSYNTAX)
8871556Srgrimes		dblquote = 1;
8881556Srgrimes	quotef = 0;
8891556Srgrimes	bqlist = NULL;
8901556Srgrimes	varnest = 0;
8911556Srgrimes	arinest = 0;
8921556Srgrimes	parenlevel = 0;
8931556Srgrimes
8941556Srgrimes	STARTSTACKSTR(out);
8951556Srgrimes	loop: {	/* for each line, until end of word */
8961556Srgrimes#if ATTY
8971556Srgrimes		if (c == '\034' && doprompt
8981556Srgrimes		 && attyset() && ! equal(termval(), "emacs")) {
8991556Srgrimes			attyline();
9001556Srgrimes			if (syntax == BASESYNTAX)
9011556Srgrimes				return readtoken();
9021556Srgrimes			c = pgetc();
9031556Srgrimes			goto loop;
9041556Srgrimes		}
9051556Srgrimes#endif
9061556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
9071556Srgrimes		for (;;) {	/* until end of line or end of word */
9081556Srgrimes			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
9091556Srgrimes			switch(syntax[c]) {
9101556Srgrimes			case CNL:	/* '\n' */
9111556Srgrimes				if (syntax == BASESYNTAX)
9121556Srgrimes					goto endword;	/* exit outer loop */
9131556Srgrimes				USTPUTC(c, out);
9141556Srgrimes				plinno++;
9151556Srgrimes				if (doprompt)
9161556Srgrimes					setprompt(2);
9171556Srgrimes				else
9181556Srgrimes					setprompt(0);
9191556Srgrimes				c = pgetc();
9201556Srgrimes				goto loop;		/* continue outer loop */
9211556Srgrimes			case CWORD:
9221556Srgrimes				USTPUTC(c, out);
9231556Srgrimes				break;
9241556Srgrimes			case CCTL:
9251556Srgrimes				if (eofmark == NULL || dblquote)
9261556Srgrimes					USTPUTC(CTLESC, out);
9271556Srgrimes				USTPUTC(c, out);
9281556Srgrimes				break;
9291556Srgrimes			case CBACK:	/* backslash */
9301556Srgrimes				c = pgetc();
9311556Srgrimes				if (c == PEOF) {
9321556Srgrimes					USTPUTC('\\', out);
9331556Srgrimes					pungetc();
9341556Srgrimes				} else if (c == '\n') {
9351556Srgrimes					if (doprompt)
9361556Srgrimes						setprompt(2);
9371556Srgrimes					else
9381556Srgrimes						setprompt(0);
9391556Srgrimes				} else {
9401556Srgrimes					if (dblquote && c != '\\' && c != '`' && c != '$'
9411556Srgrimes							 && (c != '"' || eofmark != NULL))
9421556Srgrimes						USTPUTC('\\', out);
9431556Srgrimes					if (SQSYNTAX[c] == CCTL)
9441556Srgrimes						USTPUTC(CTLESC, out);
9451556Srgrimes					USTPUTC(c, out);
9461556Srgrimes					quotef++;
9471556Srgrimes				}
9481556Srgrimes				break;
9491556Srgrimes			case CSQUOTE:
9501556Srgrimes				syntax = SQSYNTAX;
9511556Srgrimes				break;
9521556Srgrimes			case CDQUOTE:
9531556Srgrimes				syntax = DQSYNTAX;
9541556Srgrimes				dblquote = 1;
9551556Srgrimes				break;
9561556Srgrimes			case CENDQUOTE:
9571556Srgrimes				if (eofmark) {
9581556Srgrimes					USTPUTC(c, out);
9591556Srgrimes				} else {
9601556Srgrimes					if (arinest)
9611556Srgrimes						syntax = ARISYNTAX;
9621556Srgrimes					else
9631556Srgrimes						syntax = BASESYNTAX;
9641556Srgrimes					quotef++;
9651556Srgrimes					dblquote = 0;
9661556Srgrimes				}
9671556Srgrimes				break;
9681556Srgrimes			case CVAR:	/* '$' */
9691556Srgrimes				PARSESUB();		/* parse substitution */
9701556Srgrimes				break;
9711556Srgrimes			case CENDVAR:	/* '}' */
9721556Srgrimes				if (varnest > 0) {
9731556Srgrimes					varnest--;
9741556Srgrimes					USTPUTC(CTLENDVAR, out);
9751556Srgrimes				} else {
9761556Srgrimes					USTPUTC(c, out);
9771556Srgrimes				}
9781556Srgrimes				break;
9791556Srgrimes			case CLP:	/* '(' in arithmetic */
9801556Srgrimes				parenlevel++;
9811556Srgrimes				USTPUTC(c, out);
9821556Srgrimes				break;
9831556Srgrimes			case CRP:	/* ')' in arithmetic */
9841556Srgrimes				if (parenlevel > 0) {
9851556Srgrimes					USTPUTC(c, out);
9861556Srgrimes					--parenlevel;
9871556Srgrimes				} else {
9881556Srgrimes					if (pgetc() == ')') {
9891556Srgrimes						if (--arinest == 0) {
9901556Srgrimes							USTPUTC(CTLENDARI, out);
9911556Srgrimes							syntax = prevsyntax;
9921556Srgrimes						} else
9931556Srgrimes							USTPUTC(')', out);
9941556Srgrimes					} else {
9958855Srgrimes						/*
9961556Srgrimes						 * unbalanced parens
9971556Srgrimes						 *  (don't 2nd guess - no error)
9981556Srgrimes						 */
9991556Srgrimes						pungetc();
10001556Srgrimes						USTPUTC(')', out);
10011556Srgrimes					}
10021556Srgrimes				}
10031556Srgrimes				break;
10041556Srgrimes			case CBQUOTE:	/* '`' */
10051556Srgrimes				PARSEBACKQOLD();
10061556Srgrimes				break;
10071556Srgrimes			case CEOF:
10081556Srgrimes				goto endword;		/* exit outer loop */
10091556Srgrimes			default:
10101556Srgrimes				if (varnest == 0)
10111556Srgrimes					goto endword;	/* exit outer loop */
10121556Srgrimes				USTPUTC(c, out);
10131556Srgrimes			}
10141556Srgrimes			c = pgetc_macro();
10151556Srgrimes		}
10161556Srgrimes	}
10171556Srgrimesendword:
10181556Srgrimes	if (syntax == ARISYNTAX)
10191556Srgrimes		synerror("Missing '))'");
10201556Srgrimes	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
10211556Srgrimes		synerror("Unterminated quoted string");
10221556Srgrimes	if (varnest != 0) {
10231556Srgrimes		startlinno = plinno;
10241556Srgrimes		synerror("Missing '}'");
10251556Srgrimes	}
10261556Srgrimes	USTPUTC('\0', out);
10271556Srgrimes	len = out - stackblock();
10281556Srgrimes	out = stackblock();
10291556Srgrimes	if (eofmark == NULL) {
10301556Srgrimes		if ((c == '>' || c == '<')
10311556Srgrimes		 && quotef == 0
10321556Srgrimes		 && len <= 2
10331556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
10341556Srgrimes			PARSEREDIR();
10351556Srgrimes			return lasttoken = TREDIR;
10361556Srgrimes		} else {
10371556Srgrimes			pungetc();
10381556Srgrimes		}
10391556Srgrimes	}
10401556Srgrimes	quoteflag = quotef;
10411556Srgrimes	backquotelist = bqlist;
10421556Srgrimes	grabstackblock(len);
10431556Srgrimes	wordtext = out;
10441556Srgrimes	return lasttoken = TWORD;
10451556Srgrimes/* end of readtoken routine */
10461556Srgrimes
10471556Srgrimes
10481556Srgrimes
10491556Srgrimes/*
10501556Srgrimes * Check to see whether we are at the end of the here document.  When this
10511556Srgrimes * is called, c is set to the first character of the next input line.  If
10521556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
10531556Srgrimes */
10541556Srgrimes
10551556Srgrimescheckend: {
10561556Srgrimes	if (eofmark) {
10571556Srgrimes		if (striptabs) {
10581556Srgrimes			while (c == '\t')
10591556Srgrimes				c = pgetc();
10601556Srgrimes		}
10611556Srgrimes		if (c == *eofmark) {
10621556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
106325230Ssteve				char *p, *q;
10641556Srgrimes
10651556Srgrimes				p = line;
10661556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
10671556Srgrimes				if (*p == '\n' && *q == '\0') {
10681556Srgrimes					c = PEOF;
10691556Srgrimes					plinno++;
10701556Srgrimes					needprompt = doprompt;
10711556Srgrimes				} else {
10721556Srgrimes					pushstring(line, strlen(line), NULL);
10731556Srgrimes				}
10741556Srgrimes			}
10751556Srgrimes		}
10761556Srgrimes	}
10771556Srgrimes	goto checkend_return;
10781556Srgrimes}
10791556Srgrimes
10801556Srgrimes
10811556Srgrimes/*
10821556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
10831556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
10841556Srgrimes * first character of the redirection operator.
10851556Srgrimes */
10861556Srgrimes
10871556Srgrimesparseredir: {
10881556Srgrimes	char fd = *out;
10891556Srgrimes	union node *np;
10901556Srgrimes
10911556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
10921556Srgrimes	if (c == '>') {
10931556Srgrimes		np->nfile.fd = 1;
10941556Srgrimes		c = pgetc();
10951556Srgrimes		if (c == '>')
10961556Srgrimes			np->type = NAPPEND;
10971556Srgrimes		else if (c == '&')
10981556Srgrimes			np->type = NTOFD;
10991556Srgrimes		else {
11001556Srgrimes			np->type = NTO;
11011556Srgrimes			pungetc();
11021556Srgrimes		}
11031556Srgrimes	} else {	/* c == '<' */
11041556Srgrimes		np->nfile.fd = 0;
11051556Srgrimes		c = pgetc();
11061556Srgrimes		if (c == '<') {
11071556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
11081556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
11091556Srgrimes				np->nfile.fd = 0;
11101556Srgrimes			}
11111556Srgrimes			np->type = NHERE;
11121556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
11131556Srgrimes			heredoc->here = np;
11141556Srgrimes			if ((c = pgetc()) == '-') {
11151556Srgrimes				heredoc->striptabs = 1;
11161556Srgrimes			} else {
11171556Srgrimes				heredoc->striptabs = 0;
11181556Srgrimes				pungetc();
11191556Srgrimes			}
11201556Srgrimes		} else if (c == '&')
11211556Srgrimes			np->type = NFROMFD;
11221556Srgrimes		else {
11231556Srgrimes			np->type = NFROM;
11241556Srgrimes			pungetc();
11251556Srgrimes		}
11261556Srgrimes	}
11271556Srgrimes	if (fd != '\0')
11281556Srgrimes		np->nfile.fd = digit_val(fd);
11291556Srgrimes	redirnode = np;
11301556Srgrimes	goto parseredir_return;
11311556Srgrimes}
11321556Srgrimes
11331556Srgrimes
11341556Srgrimes/*
11351556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
11361556Srgrimes * and nothing else.
11371556Srgrimes */
11381556Srgrimes
11391556Srgrimesparsesub: {
11401556Srgrimes	int subtype;
11411556Srgrimes	int typeloc;
11421556Srgrimes	int flags;
11431556Srgrimes	char *p;
11441556Srgrimes#ifndef GDB_HACK
11451556Srgrimes	static const char types[] = "}-+?=";
11461556Srgrimes#endif
114718202Speter       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
11481556Srgrimes
11491556Srgrimes	c = pgetc();
11501556Srgrimes	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
11511556Srgrimes		USTPUTC('$', out);
11521556Srgrimes		pungetc();
11531556Srgrimes	} else if (c == '(') {	/* $(command) or $((arith)) */
11541556Srgrimes		if (pgetc() == '(') {
11551556Srgrimes			PARSEARITH();
11561556Srgrimes		} else {
11571556Srgrimes			pungetc();
11581556Srgrimes			PARSEBACKQNEW();
11591556Srgrimes		}
11601556Srgrimes	} else {
11611556Srgrimes		USTPUTC(CTLVAR, out);
11621556Srgrimes		typeloc = out - stackblock();
11631556Srgrimes		USTPUTC(VSNORMAL, out);
11641556Srgrimes		subtype = VSNORMAL;
11651556Srgrimes		if (c == '{') {
116618202Speter			bracketed_name = 1;
11671556Srgrimes			c = pgetc();
116817987Speter			if (c == '#') {
116917987Speter				if ((c = pgetc()) == '}')
117017987Speter					c = '#';
117117987Speter				else
117217987Speter					subtype = VSLENGTH;
117317987Speter			}
117417987Speter			else
117517987Speter				subtype = 0;
11761556Srgrimes		}
11771556Srgrimes		if (is_name(c)) {
11781556Srgrimes			do {
11791556Srgrimes				STPUTC(c, out);
11801556Srgrimes				c = pgetc();
11811556Srgrimes			} while (is_in_name(c));
118218202Speter		} else if (is_digit(c)) {
118318202Speter			if (bracketed_name) {
118418202Speter				do {
118518202Speter					STPUTC(c, out);
118618202Speter					c = pgetc();
118718202Speter				} while (is_digit(c));
118818202Speter			} else {
118918202Speter				STPUTC(c, out);
119018202Speter				c = pgetc();
119118202Speter			}
11921556Srgrimes		} else {
11931556Srgrimes			if (! is_special(c))
11941556Srgrimesbadsub:				synerror("Bad substitution");
11951556Srgrimes			USTPUTC(c, out);
11961556Srgrimes			c = pgetc();
11971556Srgrimes		}
11981556Srgrimes		STPUTC('=', out);
11991556Srgrimes		flags = 0;
12001556Srgrimes		if (subtype == 0) {
120117987Speter			switch (c) {
120217987Speter			case ':':
12031556Srgrimes				flags = VSNUL;
12041556Srgrimes				c = pgetc();
120517987Speter				/*FALLTHROUGH*/
120617987Speter			default:
120717987Speter				p = strchr(types, c);
120817987Speter				if (p == NULL)
120917987Speter					goto badsub;
121017987Speter				subtype = p - types + VSNORMAL;
121117987Speter				break;
121217987Speter			case '%':
121320425Ssteve			case '#':
121417987Speter				{
121517987Speter					int cc = c;
121617987Speter					subtype = c == '#' ? VSTRIMLEFT :
121717987Speter							     VSTRIMRIGHT;
121817987Speter					c = pgetc();
121917987Speter					if (c == cc)
122017987Speter						subtype++;
122117987Speter					else
122217987Speter						pungetc();
122317987Speter					break;
122417987Speter				}
12251556Srgrimes			}
12261556Srgrimes		} else {
12271556Srgrimes			pungetc();
12281556Srgrimes		}
12291556Srgrimes		if (dblquote || arinest)
12301556Srgrimes			flags |= VSQUOTE;
12311556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
12321556Srgrimes		if (subtype != VSNORMAL)
12331556Srgrimes			varnest++;
12341556Srgrimes	}
12351556Srgrimes	goto parsesub_return;
12361556Srgrimes}
12371556Srgrimes
12381556Srgrimes
12391556Srgrimes/*
12401556Srgrimes * Called to parse command substitutions.  Newstyle is set if the command
12411556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12421556Srgrimes * list of commands (passed by reference), and savelen is the number of
12431556Srgrimes * characters on the top of the stack which must be preserved.
12441556Srgrimes */
12451556Srgrimes
12461556Srgrimesparsebackq: {
12471556Srgrimes	struct nodelist **nlpp;
12481556Srgrimes	int savepbq;
12491556Srgrimes	union node *n;
12501556Srgrimes	char *volatile str;
12511556Srgrimes	struct jmploc jmploc;
12521556Srgrimes	struct jmploc *volatile savehandler;
12531556Srgrimes	int savelen;
125420425Ssteve	int saveprompt;
125520425Ssteve#if __GNUC__
125620425Ssteve	/* Avoid longjmp clobbering */
125720425Ssteve	(void) &saveprompt;
125820425Ssteve#endif
12591556Srgrimes
12601556Srgrimes	savepbq = parsebackquote;
12611556Srgrimes	if (setjmp(jmploc.loc)) {
12621556Srgrimes		if (str)
12631556Srgrimes			ckfree(str);
12641556Srgrimes		parsebackquote = 0;
12651556Srgrimes		handler = savehandler;
12661556Srgrimes		longjmp(handler->loc, 1);
12671556Srgrimes	}
12681556Srgrimes	INTOFF;
12691556Srgrimes	str = NULL;
12701556Srgrimes	savelen = out - stackblock();
12711556Srgrimes	if (savelen > 0) {
12721556Srgrimes		str = ckmalloc(savelen);
127317987Speter		memcpy(str, stackblock(), savelen);
12741556Srgrimes	}
12751556Srgrimes	savehandler = handler;
12761556Srgrimes	handler = &jmploc;
12771556Srgrimes	INTON;
12781556Srgrimes        if (oldstyle) {
12791556Srgrimes                /* We must read until the closing backquote, giving special
12801556Srgrimes                   treatment to some slashes, and then push the string and
12811556Srgrimes                   reread it as input, interpreting it normally.  */
128225230Ssteve                char *out;
128325230Ssteve                int c;
12841556Srgrimes                int savelen;
12851556Srgrimes                char *str;
12868855Srgrimes
128720425Ssteve
12881556Srgrimes                STARTSTACKSTR(out);
128920425Ssteve		for (;;) {
129020425Ssteve			if (needprompt) {
129120425Ssteve				setprompt(2);
129220425Ssteve				needprompt = 0;
129320425Ssteve			}
129420425Ssteve			switch (c = pgetc()) {
129520425Ssteve			case '`':
129620425Ssteve				goto done;
129720425Ssteve
129820425Ssteve			case '\\':
129920425Ssteve                                if ((c = pgetc()) == '\n') {
130020425Ssteve					plinno++;
130120425Ssteve					if (doprompt)
130220425Ssteve						setprompt(2);
130320425Ssteve					else
130420425Ssteve						setprompt(0);
130520425Ssteve					/*
130620425Ssteve					 * If eating a newline, avoid putting
130720425Ssteve					 * the newline into the new character
130820425Ssteve					 * stream (via the STPUTC after the
130920425Ssteve					 * switch).
131020425Ssteve					 */
131120425Ssteve					continue;
131220425Ssteve				}
131317987Speter                                if (c != '\\' && c != '`' && c != '$'
13141556Srgrimes                                    && (!dblquote || c != '"'))
13151556Srgrimes                                        STPUTC('\\', out);
131620425Ssteve				break;
131720425Ssteve
131820425Ssteve			case '\n':
131920425Ssteve				plinno++;
132020425Ssteve				needprompt = doprompt;
132120425Ssteve				break;
132220425Ssteve
132320425Ssteve			case PEOF:
132420425Ssteve			        startlinno = plinno;
132520425Ssteve				synerror("EOF in backquote substitution");
132620425Ssteve 				break;
132720425Ssteve
132820425Ssteve			default:
132920425Ssteve				break;
133020425Ssteve			}
133120425Ssteve			STPUTC(c, out);
13321556Srgrimes                }
133320425Sstevedone:
13341556Srgrimes                STPUTC('\0', out);
13351556Srgrimes                savelen = out - stackblock();
13361556Srgrimes                if (savelen > 0) {
13371556Srgrimes                        str = ckmalloc(savelen);
133817987Speter                        memcpy(str, stackblock(), savelen);
133917987Speter			setinputstring(str, 1);
13401556Srgrimes                }
13411556Srgrimes        }
13421556Srgrimes	nlpp = &bqlist;
13431556Srgrimes	while (*nlpp)
13441556Srgrimes		nlpp = &(*nlpp)->next;
13451556Srgrimes	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
13461556Srgrimes	(*nlpp)->next = NULL;
13471556Srgrimes	parsebackquote = oldstyle;
134820425Ssteve
134920425Ssteve	if (oldstyle) {
135020425Ssteve		saveprompt = doprompt;
135120425Ssteve		doprompt = 0;
135220425Ssteve	}
135320425Ssteve
13541556Srgrimes	n = list(0);
135520425Ssteve
135620425Ssteve	if (oldstyle)
135720425Ssteve		doprompt = saveprompt;
135820425Ssteve	else {
135920425Ssteve		if (readtoken() != TRP)
136020425Ssteve			synexpect(TRP);
136120425Ssteve	}
136220425Ssteve
13631556Srgrimes	(*nlpp)->n = n;
136420425Ssteve        if (oldstyle) {
136520425Ssteve		/*
136620425Ssteve		 * Start reading from old file again, ignoring any pushed back
136720425Ssteve		 * tokens left from the backquote parsing
136820425Ssteve		 */
13691556Srgrimes                popfile();
137020425Ssteve		tokpushback = 0;
137120425Ssteve	}
13721556Srgrimes	while (stackblocksize() <= savelen)
13731556Srgrimes		growstackblock();
13741556Srgrimes	STARTSTACKSTR(out);
13751556Srgrimes	if (str) {
137617987Speter		memcpy(out, str, savelen);
13771556Srgrimes		STADJUST(savelen, out);
13781556Srgrimes		INTOFF;
13791556Srgrimes		ckfree(str);
13801556Srgrimes		str = NULL;
13811556Srgrimes		INTON;
13821556Srgrimes	}
13831556Srgrimes	parsebackquote = savepbq;
13841556Srgrimes	handler = savehandler;
13851556Srgrimes	if (arinest || dblquote)
13861556Srgrimes		USTPUTC(CTLBACKQ | CTLQUOTE, out);
13871556Srgrimes	else
13881556Srgrimes		USTPUTC(CTLBACKQ, out);
13891556Srgrimes	if (oldstyle)
13901556Srgrimes		goto parsebackq_oldreturn;
13911556Srgrimes	else
13921556Srgrimes		goto parsebackq_newreturn;
13931556Srgrimes}
13941556Srgrimes
13951556Srgrimes/*
13961556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
13971556Srgrimes */
13981556Srgrimesparsearith: {
13991556Srgrimes
14001556Srgrimes	if (++arinest == 1) {
14011556Srgrimes		prevsyntax = syntax;
14021556Srgrimes		syntax = ARISYNTAX;
14031556Srgrimes		USTPUTC(CTLARI, out);
14041556Srgrimes	} else {
14051556Srgrimes		/*
14061556Srgrimes		 * we collapse embedded arithmetic expansion to
14071556Srgrimes		 * parenthesis, which should be equivalent
14081556Srgrimes		 */
14091556Srgrimes		USTPUTC('(', out);
14101556Srgrimes	}
14111556Srgrimes	goto parsearith_return;
14121556Srgrimes}
14131556Srgrimes
14141556Srgrimes} /* end of readtoken */
14151556Srgrimes
14161556Srgrimes
14171556Srgrimes
14181556Srgrimes#ifdef mkinit
14191556SrgrimesRESET {
14201556Srgrimes	tokpushback = 0;
14211556Srgrimes	checkkwd = 0;
14221556Srgrimes}
14231556Srgrimes#endif
14241556Srgrimes
14251556Srgrimes/*
14261556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
14271556Srgrimes * or backquotes).
14281556Srgrimes */
14291556Srgrimes
14301556SrgrimesSTATIC int
14311556Srgrimesnoexpand(text)
14321556Srgrimes	char *text;
14331556Srgrimes	{
143425230Ssteve	char *p;
143525230Ssteve	char c;
14361556Srgrimes
14371556Srgrimes	p = text;
14381556Srgrimes	while ((c = *p++) != '\0') {
14391556Srgrimes		if (c == CTLESC)
14401556Srgrimes			p++;
14411556Srgrimes		else if (BASESYNTAX[c] == CCTL)
14421556Srgrimes			return 0;
14431556Srgrimes	}
14441556Srgrimes	return 1;
14451556Srgrimes}
14461556Srgrimes
14471556Srgrimes
14481556Srgrimes/*
14491556Srgrimes * Return true if the argument is a legal variable name (a letter or
14501556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
14511556Srgrimes */
14521556Srgrimes
14531556Srgrimesint
14541556Srgrimesgoodname(name)
14551556Srgrimes	char *name;
14561556Srgrimes	{
145725230Ssteve	char *p;
14581556Srgrimes
14591556Srgrimes	p = name;
14601556Srgrimes	if (! is_name(*p))
14611556Srgrimes		return 0;
14621556Srgrimes	while (*++p) {
14631556Srgrimes		if (! is_in_name(*p))
14641556Srgrimes			return 0;
14651556Srgrimes	}
14661556Srgrimes	return 1;
14671556Srgrimes}
14681556Srgrimes
14691556Srgrimes
14701556Srgrimes/*
14711556Srgrimes * Called when an unexpected token is read during the parse.  The argument
14721556Srgrimes * is the token that is expected, or -1 if more than one type of token can
14731556Srgrimes * occur at this point.
14741556Srgrimes */
14751556Srgrimes
14761556SrgrimesSTATIC void
147720425Sstevesynexpect(token)
147817987Speter	int token;
147917987Speter{
14801556Srgrimes	char msg[64];
14811556Srgrimes
14821556Srgrimes	if (token >= 0) {
14831556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
14841556Srgrimes			tokname[lasttoken], tokname[token]);
14851556Srgrimes	} else {
14861556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
14871556Srgrimes	}
14881556Srgrimes	synerror(msg);
14891556Srgrimes}
14901556Srgrimes
14911556Srgrimes
14921556SrgrimesSTATIC void
14931556Srgrimessynerror(msg)
14941556Srgrimes	char *msg;
14951556Srgrimes	{
14961556Srgrimes	if (commandname)
14971556Srgrimes		outfmt(&errout, "%s: %d: ", commandname, startlinno);
14981556Srgrimes	outfmt(&errout, "Syntax error: %s\n", msg);
14991556Srgrimes	error((char *)NULL);
15001556Srgrimes}
15011556Srgrimes
15021556SrgrimesSTATIC void
15031556Srgrimessetprompt(which)
15041556Srgrimes	int which;
15051556Srgrimes	{
15061556Srgrimes	whichprompt = which;
15071556Srgrimes
150817987Speter#ifndef NO_HISTORY
15091556Srgrimes	if (!el)
151017987Speter#endif
15111556Srgrimes		out2str(getprompt(NULL));
15121556Srgrimes}
15131556Srgrimes
15141556Srgrimes/*
15151556Srgrimes * called by editline -- any expansions to the prompt
15161556Srgrimes *    should be added here.
15171556Srgrimes */
15181556Srgrimeschar *
15191556Srgrimesgetprompt(unused)
152025905Ssteve	void *unused __unused;
152125905Ssteve{
15221556Srgrimes	switch (whichprompt) {
15231556Srgrimes	case 0:
15241556Srgrimes		return "";
15251556Srgrimes	case 1:
15261556Srgrimes		return ps1val();
15271556Srgrimes	case 2:
15281556Srgrimes		return ps2val();
15291556Srgrimes	default:
15301556Srgrimes		return "<internal prompt error>";
15311556Srgrimes	}
15321556Srgrimes}
1533