parser.c revision 101662
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
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/parser.c 101662 2002-08-11 03:04:23Z tjr $");
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"
6259436Scracauer#include "eval.h"
6317987Speter#ifndef NO_HISTORY
641556Srgrimes#include "myhistedit.h"
6517987Speter#endif
661556Srgrimes
671556Srgrimes/*
681556Srgrimes * Shell command parser.
691556Srgrimes */
701556Srgrimes
711556Srgrimes#define EOFMARKLEN 79
721556Srgrimes
731556Srgrimes/* values returned by readtoken */
7417987Speter#include "token.h"
751556Srgrimes
761556Srgrimes
771556Srgrimes
781556Srgrimesstruct heredoc {
791556Srgrimes	struct heredoc *next;	/* next here document in list */
801556Srgrimes	union node *here;		/* redirection node */
811556Srgrimes	char *eofmark;		/* string indicating end of input */
821556Srgrimes	int striptabs;		/* if set, strip leading tabs */
831556Srgrimes};
841556Srgrimes
851556Srgrimes
861556Srgrimes
871556Srgrimesstruct heredoc *heredoclist;	/* list of here documents to read */
881556Srgrimesint parsebackquote;		/* nonzero if we are inside backquotes */
891556Srgrimesint doprompt;			/* if set, prompt the user */
901556Srgrimesint needprompt;			/* true if interactive and at start of line */
911556Srgrimesint lasttoken;			/* last token read */
921556SrgrimesMKINIT int tokpushback;		/* last token pushed back */
931556Srgrimeschar *wordtext;			/* text of last word returned by readtoken */
941556SrgrimesMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
951556Srgrimesstruct nodelist *backquotelist;
961556Srgrimesunion node *redirnode;
971556Srgrimesstruct heredoc *heredoc;
981556Srgrimesint quoteflag;			/* set if (part of) last token was quoted */
991556Srgrimesint startlinno;			/* line # where last token started */
1001556Srgrimes
10118018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
10218018Speterstatic int noaliases = 0;
1031556Srgrimes
1041556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
1051556Srgrimes#ifdef GDB_HACK
1061556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
1071556Srgrimesstatic const char types[] = "}-+?=";
1081556Srgrimes#endif
1091556Srgrimes
1101556Srgrimes
11190111SimpSTATIC union node *list(int);
11290111SimpSTATIC union node *andor(void);
11390111SimpSTATIC union node *pipeline(void);
11490111SimpSTATIC union node *command(void);
11590111SimpSTATIC union node *simplecmd(union node **, union node *);
11690111SimpSTATIC union node *makename(void);
11790111SimpSTATIC void parsefname(void);
11890111SimpSTATIC void parseheredoc(void);
11990111SimpSTATIC int peektoken(void);
12090111SimpSTATIC int readtoken(void);
12190111SimpSTATIC int xxreadtoken(void);
12290111SimpSTATIC int readtoken1(int, char const *, char *, int);
12390111SimpSTATIC int noexpand(char *);
12490111SimpSTATIC void synexpect(int);
12590111SimpSTATIC void synerror(char *);
12690111SimpSTATIC void setprompt(int);
1271556Srgrimes
12817987Speter
1291556Srgrimes/*
1301556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
1311556Srgrimes * valid parse tree indicating a blank line.)
1321556Srgrimes */
1331556Srgrimes
1341556Srgrimesunion node *
13590111Simpparsecmd(int interact)
13617987Speter{
1371556Srgrimes	int t;
1381556Srgrimes
13960593Scracauer	tokpushback = 0;
1401556Srgrimes	doprompt = interact;
1411556Srgrimes	if (doprompt)
1421556Srgrimes		setprompt(1);
1431556Srgrimes	else
1441556Srgrimes		setprompt(0);
1451556Srgrimes	needprompt = 0;
1461556Srgrimes	t = readtoken();
1471556Srgrimes	if (t == TEOF)
1481556Srgrimes		return NEOF;
1491556Srgrimes	if (t == TNL)
1501556Srgrimes		return NULL;
1511556Srgrimes	tokpushback++;
1521556Srgrimes	return list(1);
1531556Srgrimes}
1541556Srgrimes
1551556Srgrimes
1561556SrgrimesSTATIC union node *
15790111Simplist(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 *
22790111Simpandor(void)
22890111Simp{
2291556Srgrimes	union node *n1, *n2, *n3;
2301556Srgrimes	int t;
2311556Srgrimes
2321556Srgrimes	n1 = pipeline();
2331556Srgrimes	for (;;) {
2341556Srgrimes		if ((t = readtoken()) == TAND) {
2351556Srgrimes			t = NAND;
2361556Srgrimes		} else if (t == TOR) {
2371556Srgrimes			t = NOR;
2381556Srgrimes		} else {
2391556Srgrimes			tokpushback++;
2401556Srgrimes			return n1;
2411556Srgrimes		}
2421556Srgrimes		n2 = pipeline();
2431556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
2441556Srgrimes		n3->type = t;
2451556Srgrimes		n3->nbinary.ch1 = n1;
2461556Srgrimes		n3->nbinary.ch2 = n2;
2471556Srgrimes		n1 = n3;
2481556Srgrimes	}
2491556Srgrimes}
2501556Srgrimes
2511556Srgrimes
2521556Srgrimes
2531556SrgrimesSTATIC union node *
25490111Simppipeline(void)
25590111Simp{
25675336Sbrian	union node *n1, *n2, *pipenode;
2571556Srgrimes	struct nodelist *lp, *prev;
25875336Sbrian	int negate;
2591556Srgrimes
26075336Sbrian	negate = 0;
2611556Srgrimes	TRACE(("pipeline: entered\n"));
26275336Sbrian	while (readtoken() == TNOT)
26375336Sbrian		negate = !negate;
26475336Sbrian	tokpushback++;
2651556Srgrimes	n1 = command();
2661556Srgrimes	if (readtoken() == TPIPE) {
2671556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
2681556Srgrimes		pipenode->type = NPIPE;
2691556Srgrimes		pipenode->npipe.backgnd = 0;
2701556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2711556Srgrimes		pipenode->npipe.cmdlist = lp;
2721556Srgrimes		lp->n = n1;
2731556Srgrimes		do {
2741556Srgrimes			prev = lp;
2751556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2761556Srgrimes			lp->n = command();
2771556Srgrimes			prev->next = lp;
2781556Srgrimes		} while (readtoken() == TPIPE);
2791556Srgrimes		lp->next = NULL;
2801556Srgrimes		n1 = pipenode;
2811556Srgrimes	}
2821556Srgrimes	tokpushback++;
28375336Sbrian	if (negate) {
28475336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
28575336Sbrian		n2->type = NNOT;
28675336Sbrian		n2->nnot.com = n1;
28775336Sbrian		return n2;
28875336Sbrian	} else
28975336Sbrian		return n1;
2901556Srgrimes}
2911556Srgrimes
2921556Srgrimes
2931556Srgrimes
2941556SrgrimesSTATIC union node *
29590111Simpcommand(void)
29690111Simp{
2971556Srgrimes	union node *n1, *n2;
2981556Srgrimes	union node *ap, **app;
2991556Srgrimes	union node *cp, **cpp;
3001556Srgrimes	union node *redir, **rpp;
30175160Sbrian	int t, negate = 0;
3021556Srgrimes
3031556Srgrimes	checkkwd = 2;
30417987Speter	redir = NULL;
30517987Speter	n1 = NULL;
3061556Srgrimes	rpp = &redir;
30720425Ssteve
3081556Srgrimes	/* Check for redirection which may precede command */
3091556Srgrimes	while (readtoken() == TREDIR) {
3101556Srgrimes		*rpp = n2 = redirnode;
3111556Srgrimes		rpp = &n2->nfile.next;
3121556Srgrimes		parsefname();
3131556Srgrimes	}
3141556Srgrimes	tokpushback++;
3151556Srgrimes
31675160Sbrian	while (readtoken() == TNOT) {
31775160Sbrian		TRACE(("command: TNOT recognized\n"));
31875160Sbrian		negate = !negate;
31975160Sbrian	}
32075160Sbrian	tokpushback++;
32175160Sbrian
3221556Srgrimes	switch (readtoken()) {
3231556Srgrimes	case TIF:
3241556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3251556Srgrimes		n1->type = NIF;
3261556Srgrimes		n1->nif.test = list(0);
3271556Srgrimes		if (readtoken() != TTHEN)
3281556Srgrimes			synexpect(TTHEN);
3291556Srgrimes		n1->nif.ifpart = list(0);
3301556Srgrimes		n2 = n1;
3311556Srgrimes		while (readtoken() == TELIF) {
3321556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
3331556Srgrimes			n2 = n2->nif.elsepart;
3341556Srgrimes			n2->type = NIF;
3351556Srgrimes			n2->nif.test = list(0);
3361556Srgrimes			if (readtoken() != TTHEN)
3371556Srgrimes				synexpect(TTHEN);
3381556Srgrimes			n2->nif.ifpart = list(0);
3391556Srgrimes		}
3401556Srgrimes		if (lasttoken == TELSE)
3411556Srgrimes			n2->nif.elsepart = list(0);
3421556Srgrimes		else {
3431556Srgrimes			n2->nif.elsepart = NULL;
3441556Srgrimes			tokpushback++;
3451556Srgrimes		}
3461556Srgrimes		if (readtoken() != TFI)
3471556Srgrimes			synexpect(TFI);
3481556Srgrimes		checkkwd = 1;
3491556Srgrimes		break;
3501556Srgrimes	case TWHILE:
3511556Srgrimes	case TUNTIL: {
3521556Srgrimes		int got;
3531556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
3541556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
3551556Srgrimes		n1->nbinary.ch1 = list(0);
3561556Srgrimes		if ((got=readtoken()) != TDO) {
3571556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
3581556Srgrimes			synexpect(TDO);
3591556Srgrimes		}
3601556Srgrimes		n1->nbinary.ch2 = list(0);
3611556Srgrimes		if (readtoken() != TDONE)
3621556Srgrimes			synexpect(TDONE);
3631556Srgrimes		checkkwd = 1;
3641556Srgrimes		break;
3651556Srgrimes	}
3661556Srgrimes	case TFOR:
3671556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
3681556Srgrimes			synerror("Bad for loop variable");
3691556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
3701556Srgrimes		n1->type = NFOR;
3711556Srgrimes		n1->nfor.var = wordtext;
3721556Srgrimes		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
3731556Srgrimes			app = &ap;
3741556Srgrimes			while (readtoken() == TWORD) {
3751556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
3761556Srgrimes				n2->type = NARG;
3771556Srgrimes				n2->narg.text = wordtext;
3781556Srgrimes				n2->narg.backquote = backquotelist;
3791556Srgrimes				*app = n2;
3801556Srgrimes				app = &n2->narg.next;
3811556Srgrimes			}
3821556Srgrimes			*app = NULL;
3831556Srgrimes			n1->nfor.args = ap;
3841556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3851556Srgrimes				synexpect(-1);
3861556Srgrimes		} else {
3871556Srgrimes#ifndef GDB_HACK
3881556Srgrimes			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
3891556Srgrimes								   '@', '=', '\0'};
3901556Srgrimes#endif
3911556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
3921556Srgrimes			n2->type = NARG;
3931556Srgrimes			n2->narg.text = (char *)argvars;
3941556Srgrimes			n2->narg.backquote = NULL;
3951556Srgrimes			n2->narg.next = NULL;
3961556Srgrimes			n1->nfor.args = n2;
3971556Srgrimes			/*
3981556Srgrimes			 * Newline or semicolon here is optional (but note
3991556Srgrimes			 * that the original Bourne shell only allowed NL).
4001556Srgrimes			 */
4011556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4021556Srgrimes				tokpushback++;
4031556Srgrimes		}
4041556Srgrimes		checkkwd = 2;
4051556Srgrimes		if ((t = readtoken()) == TDO)
4061556Srgrimes			t = TDONE;
4071556Srgrimes		else if (t == TBEGIN)
4081556Srgrimes			t = TEND;
4091556Srgrimes		else
4101556Srgrimes			synexpect(-1);
4111556Srgrimes		n1->nfor.body = list(0);
4121556Srgrimes		if (readtoken() != t)
4131556Srgrimes			synexpect(t);
4141556Srgrimes		checkkwd = 1;
4151556Srgrimes		break;
4161556Srgrimes	case TCASE:
4171556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4181556Srgrimes		n1->type = NCASE;
4191556Srgrimes		if (readtoken() != TWORD)
4201556Srgrimes			synexpect(TWORD);
4211556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
4221556Srgrimes		n2->type = NARG;
4231556Srgrimes		n2->narg.text = wordtext;
4241556Srgrimes		n2->narg.backquote = backquotelist;
4251556Srgrimes		n2->narg.next = NULL;
4261556Srgrimes		while (readtoken() == TNL);
4271556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
4281556Srgrimes			synerror("expecting \"in\"");
4291556Srgrimes		cpp = &n1->ncase.cases;
43018018Speter		noaliases = 1;	/* turn off alias expansion */
4312760Ssef		checkkwd = 2, readtoken();
4322760Ssef		do {
4331556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
4341556Srgrimes			cp->type = NCLIST;
4351556Srgrimes			app = &cp->nclist.pattern;
4361556Srgrimes			for (;;) {
4371556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
4381556Srgrimes				ap->type = NARG;
4391556Srgrimes				ap->narg.text = wordtext;
4401556Srgrimes				ap->narg.backquote = backquotelist;
4412760Ssef				if (checkkwd = 2, readtoken() != TPIPE)
4421556Srgrimes					break;
4431556Srgrimes				app = &ap->narg.next;
4442760Ssef				readtoken();
4451556Srgrimes			}
4461556Srgrimes			ap->narg.next = NULL;
4471556Srgrimes			if (lasttoken != TRP)
44818018Speter				noaliases = 0, synexpect(TRP);
4491556Srgrimes			cp->nclist.body = list(0);
4502760Ssef
4512760Ssef			checkkwd = 2;
4522760Ssef			if ((t = readtoken()) != TESAC) {
4532760Ssef				if (t != TENDCASE)
45418018Speter					noaliases = 0, synexpect(TENDCASE);
4552760Ssef				else
4562760Ssef					checkkwd = 2, readtoken();
4572760Ssef			}
4581556Srgrimes			cpp = &cp->nclist.next;
4592760Ssef		} while(lasttoken != TESAC);
46018018Speter		noaliases = 0;	/* reset alias expansion */
4611556Srgrimes		*cpp = NULL;
4621556Srgrimes		checkkwd = 1;
4631556Srgrimes		break;
4641556Srgrimes	case TLP:
4651556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
4661556Srgrimes		n1->type = NSUBSHELL;
4671556Srgrimes		n1->nredir.n = list(0);
4681556Srgrimes		n1->nredir.redirect = NULL;
4691556Srgrimes		if (readtoken() != TRP)
4701556Srgrimes			synexpect(TRP);
4711556Srgrimes		checkkwd = 1;
4721556Srgrimes		break;
4731556Srgrimes	case TBEGIN:
4741556Srgrimes		n1 = list(0);
4751556Srgrimes		if (readtoken() != TEND)
4761556Srgrimes			synexpect(TEND);
4771556Srgrimes		checkkwd = 1;
4781556Srgrimes		break;
4791556Srgrimes	/* Handle an empty command like other simple commands.  */
48017987Speter	case TSEMI:
481101662Stjr	case TAND:
482101662Stjr	case TOR:
48317987Speter		/*
48417987Speter		 * An empty command before a ; doesn't make much sense, and
48517987Speter		 * should certainly be disallowed in the case of `if ;'.
48617987Speter		 */
48717987Speter		if (!redir)
48817987Speter			synexpect(-1);
4891556Srgrimes	case TNL:
49010399Sjoerg	case TEOF:
4911556Srgrimes	case TWORD:
49217987Speter	case TRP:
4931556Srgrimes		tokpushback++;
49475160Sbrian		n1 = simplecmd(rpp, redir);
49575160Sbrian		goto checkneg;
4961556Srgrimes	default:
4971556Srgrimes		synexpect(-1);
4981556Srgrimes	}
4991556Srgrimes
5001556Srgrimes	/* Now check for redirection which may follow command */
5011556Srgrimes	while (readtoken() == TREDIR) {
5021556Srgrimes		*rpp = n2 = redirnode;
5031556Srgrimes		rpp = &n2->nfile.next;
5041556Srgrimes		parsefname();
5051556Srgrimes	}
5061556Srgrimes	tokpushback++;
5071556Srgrimes	*rpp = NULL;
5081556Srgrimes	if (redir) {
5091556Srgrimes		if (n1->type != NSUBSHELL) {
5101556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5111556Srgrimes			n2->type = NREDIR;
5121556Srgrimes			n2->nredir.n = n1;
5131556Srgrimes			n1 = n2;
5141556Srgrimes		}
5151556Srgrimes		n1->nredir.redirect = redir;
5161556Srgrimes	}
51775160Sbrian
51875160Sbriancheckneg:
51975160Sbrian	if (negate) {
52075160Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
52175160Sbrian		n2->type = NNOT;
52275160Sbrian		n2->nnot.com = n1;
52375160Sbrian		return n2;
52475160Sbrian	}
52575160Sbrian	else
52675160Sbrian		return n1;
5271556Srgrimes}
5281556Srgrimes
5291556Srgrimes
5301556SrgrimesSTATIC union node *
53190111Simpsimplecmd(union node **rpp, union node *redir)
53290111Simp{
5331556Srgrimes	union node *args, **app;
5341556Srgrimes	union node **orig_rpp = rpp;
53575160Sbrian	union node *n = NULL, *n2;
53675160Sbrian	int negate = 0;
5371556Srgrimes
5381556Srgrimes	/* If we don't have any redirections already, then we must reset */
5391556Srgrimes	/* rpp to be the address of the local redir variable.  */
5401556Srgrimes	if (redir == 0)
5411556Srgrimes		rpp = &redir;
5421556Srgrimes
5431556Srgrimes	args = NULL;
5441556Srgrimes	app = &args;
5458855Srgrimes	/*
5461556Srgrimes	 * We save the incoming value, because we need this for shell
5471556Srgrimes	 * functions.  There can not be a redirect or an argument between
5488855Srgrimes	 * the function name and the open parenthesis.
5491556Srgrimes	 */
5501556Srgrimes	orig_rpp = rpp;
5511556Srgrimes
55275160Sbrian	while (readtoken() == TNOT) {
55375160Sbrian		TRACE(("command: TNOT recognized\n"));
55475160Sbrian		negate = !negate;
55575160Sbrian	}
55675160Sbrian	tokpushback++;
55775160Sbrian
5581556Srgrimes	for (;;) {
5591556Srgrimes		if (readtoken() == TWORD) {
5601556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
5611556Srgrimes			n->type = NARG;
5621556Srgrimes			n->narg.text = wordtext;
5631556Srgrimes			n->narg.backquote = backquotelist;
5641556Srgrimes			*app = n;
5651556Srgrimes			app = &n->narg.next;
5661556Srgrimes		} else if (lasttoken == TREDIR) {
5671556Srgrimes			*rpp = n = redirnode;
5681556Srgrimes			rpp = &n->nfile.next;
5691556Srgrimes			parsefname();	/* read name of redirection file */
5701556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
5711556Srgrimes					    && rpp == orig_rpp) {
5721556Srgrimes			/* We have a function */
5731556Srgrimes			if (readtoken() != TRP)
5741556Srgrimes				synexpect(TRP);
5751556Srgrimes#ifdef notdef
5761556Srgrimes			if (! goodname(n->narg.text))
5771556Srgrimes				synerror("Bad function name");
5781556Srgrimes#endif
5791556Srgrimes			n->type = NDEFUN;
5801556Srgrimes			n->narg.next = command();
58175160Sbrian			goto checkneg;
5821556Srgrimes		} else {
5831556Srgrimes			tokpushback++;
5841556Srgrimes			break;
5851556Srgrimes		}
5861556Srgrimes	}
5871556Srgrimes	*app = NULL;
5881556Srgrimes	*rpp = NULL;
5891556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
5901556Srgrimes	n->type = NCMD;
5911556Srgrimes	n->ncmd.backgnd = 0;
5921556Srgrimes	n->ncmd.args = args;
5931556Srgrimes	n->ncmd.redirect = redir;
59475160Sbrian
59575160Sbriancheckneg:
59675160Sbrian	if (negate) {
59775160Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
59875160Sbrian		n2->type = NNOT;
59975160Sbrian		n2->nnot.com = n;
60075160Sbrian		return n2;
60175160Sbrian	}
60275160Sbrian	else
60375160Sbrian		return n;
6041556Srgrimes}
6051556Srgrimes
60617987SpeterSTATIC union node *
60790111Simpmakename(void)
60890111Simp{
60917987Speter	union node *n;
6101556Srgrimes
61117987Speter	n = (union node *)stalloc(sizeof (struct narg));
61217987Speter	n->type = NARG;
61317987Speter	n->narg.next = NULL;
61417987Speter	n->narg.text = wordtext;
61517987Speter	n->narg.backquote = backquotelist;
61617987Speter	return n;
61717987Speter}
61817987Speter
61990111Simpvoid fixredir(union node *n, const char *text, int err)
62090111Simp{
62117987Speter	TRACE(("Fix redir %s %d\n", text, err));
62217987Speter	if (!err)
62317987Speter		n->ndup.vname = NULL;
62417987Speter
62517987Speter	if (is_digit(text[0]) && text[1] == '\0')
62617987Speter		n->ndup.dupfd = digit_val(text[0]);
62717987Speter	else if (text[0] == '-' && text[1] == '\0')
62817987Speter		n->ndup.dupfd = -1;
62917987Speter	else {
63020425Ssteve
63117987Speter		if (err)
63217987Speter			synerror("Bad fd number");
63317987Speter		else
63417987Speter			n->ndup.vname = makename();
63517987Speter	}
63617987Speter}
63717987Speter
63817987Speter
6391556SrgrimesSTATIC void
64090111Simpparsefname(void)
64190111Simp{
6421556Srgrimes	union node *n = redirnode;
6431556Srgrimes
6441556Srgrimes	if (readtoken() != TWORD)
6451556Srgrimes		synexpect(-1);
6461556Srgrimes	if (n->type == NHERE) {
6471556Srgrimes		struct heredoc *here = heredoc;
6481556Srgrimes		struct heredoc *p;
6491556Srgrimes		int i;
6501556Srgrimes
6511556Srgrimes		if (quoteflag == 0)
6521556Srgrimes			n->type = NXHERE;
6531556Srgrimes		TRACE(("Here document %d\n", n->type));
6541556Srgrimes		if (here->striptabs) {
6551556Srgrimes			while (*wordtext == '\t')
6561556Srgrimes				wordtext++;
6571556Srgrimes		}
6581556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
6591556Srgrimes			synerror("Illegal eof marker for << redirection");
6601556Srgrimes		rmescapes(wordtext);
6611556Srgrimes		here->eofmark = wordtext;
6621556Srgrimes		here->next = NULL;
6631556Srgrimes		if (heredoclist == NULL)
6641556Srgrimes			heredoclist = here;
6651556Srgrimes		else {
6661556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
6671556Srgrimes			p->next = here;
6681556Srgrimes		}
6691556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
67017987Speter		fixredir(n, wordtext, 0);
6711556Srgrimes	} else {
67217987Speter		n->nfile.fname = makename();
6731556Srgrimes	}
6741556Srgrimes}
6751556Srgrimes
6761556Srgrimes
6771556Srgrimes/*
6781556Srgrimes * Input any here documents.
6791556Srgrimes */
6801556Srgrimes
6811556SrgrimesSTATIC void
68290111Simpparseheredoc(void)
68390111Simp{
6841556Srgrimes	struct heredoc *here;
6851556Srgrimes	union node *n;
6861556Srgrimes
6871556Srgrimes	while (heredoclist) {
6881556Srgrimes		here = heredoclist;
6891556Srgrimes		heredoclist = here->next;
6901556Srgrimes		if (needprompt) {
6911556Srgrimes			setprompt(2);
6921556Srgrimes			needprompt = 0;
6931556Srgrimes		}
6941556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
6951556Srgrimes				here->eofmark, here->striptabs);
6961556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
6971556Srgrimes		n->narg.type = NARG;
6981556Srgrimes		n->narg.next = NULL;
6991556Srgrimes		n->narg.text = wordtext;
7001556Srgrimes		n->narg.backquote = backquotelist;
7011556Srgrimes		here->here->nhere.doc = n;
7021556Srgrimes	}
7031556Srgrimes}
7041556Srgrimes
7051556SrgrimesSTATIC int
70690111Simppeektoken(void)
70790111Simp{
7081556Srgrimes	int t;
7091556Srgrimes
7101556Srgrimes	t = readtoken();
7111556Srgrimes	tokpushback++;
7121556Srgrimes	return (t);
7131556Srgrimes}
7141556Srgrimes
7151556SrgrimesSTATIC int
71690111Simpreadtoken(void)
71790111Simp{
7181556Srgrimes	int t;
7191556Srgrimes	int savecheckkwd = checkkwd;
7201556Srgrimes	struct alias *ap;
7211556Srgrimes#ifdef DEBUG
7221556Srgrimes	int alreadyseen = tokpushback;
7231556Srgrimes#endif
7248855Srgrimes
7251556Srgrimes	top:
7261556Srgrimes	t = xxreadtoken();
7271556Srgrimes
7281556Srgrimes	if (checkkwd) {
7291556Srgrimes		/*
7301556Srgrimes		 * eat newlines
7311556Srgrimes		 */
7321556Srgrimes		if (checkkwd == 2) {
7331556Srgrimes			checkkwd = 0;
7341556Srgrimes			while (t == TNL) {
7351556Srgrimes				parseheredoc();
7361556Srgrimes				t = xxreadtoken();
7371556Srgrimes			}
7381556Srgrimes		} else
7391556Srgrimes			checkkwd = 0;
7401556Srgrimes		/*
7411556Srgrimes		 * check for keywords and aliases
7421556Srgrimes		 */
74320425Ssteve		if (t == TWORD && !quoteflag)
74417987Speter		{
74598463Sjmallett			const char * const *pp;
7461556Srgrimes
74798463Sjmallett			for (pp = parsekwd; *pp; pp++) {
74820425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
74917987Speter				{
7501556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
7511556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
7521556Srgrimes					goto out;
7531556Srgrimes				}
7541556Srgrimes			}
75518018Speter			if (noaliases == 0 &&
75618018Speter			    (ap = lookupalias(wordtext, 1)) != NULL) {
7571556Srgrimes				pushstring(ap->val, strlen(ap->val), ap);
7581556Srgrimes				checkkwd = savecheckkwd;
7591556Srgrimes				goto top;
7601556Srgrimes			}
7611556Srgrimes		}
7621556Srgrimesout:
76375160Sbrian		checkkwd = (t == TNOT) ? savecheckkwd : 0;
7641556Srgrimes	}
7651556Srgrimes#ifdef DEBUG
7661556Srgrimes	if (!alreadyseen)
7671556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7681556Srgrimes	else
7691556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7701556Srgrimes#endif
7711556Srgrimes	return (t);
7721556Srgrimes}
7731556Srgrimes
7741556Srgrimes
7751556Srgrimes/*
7761556Srgrimes * Read the next input token.
7771556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
7781556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
7791556Srgrimes *	quoted.
7801556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
7811556Srgrimes *	the redirection.
7821556Srgrimes * In all cases, the variable startlinno is set to the number of the line
7831556Srgrimes *	on which the token starts.
7841556Srgrimes *
7851556Srgrimes * [Change comment:  here documents and internal procedures]
7861556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
7871556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
7881556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
7891556Srgrimes *  We could also make parseoperator in essence the main routine, and
7901556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
7911556Srgrimes */
7921556Srgrimes
7931556Srgrimes#define RETURN(token)	return lasttoken = token
7941556Srgrimes
7951556SrgrimesSTATIC int
79690111Simpxxreadtoken(void)
79790111Simp{
79825230Ssteve	int c;
7991556Srgrimes
8001556Srgrimes	if (tokpushback) {
8011556Srgrimes		tokpushback = 0;
8021556Srgrimes		return lasttoken;
8031556Srgrimes	}
8041556Srgrimes	if (needprompt) {
8051556Srgrimes		setprompt(2);
8061556Srgrimes		needprompt = 0;
8071556Srgrimes	}
8081556Srgrimes	startlinno = plinno;
8091556Srgrimes	for (;;) {	/* until token or start of word found */
8101556Srgrimes		c = pgetc_macro();
8111556Srgrimes		if (c == ' ' || c == '\t')
8121556Srgrimes			continue;		/* quick check for white space first */
8131556Srgrimes		switch (c) {
8141556Srgrimes		case ' ': case '\t':
8151556Srgrimes			continue;
8161556Srgrimes		case '#':
8171556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
8181556Srgrimes			pungetc();
8191556Srgrimes			continue;
8201556Srgrimes		case '\\':
8211556Srgrimes			if (pgetc() == '\n') {
8221556Srgrimes				startlinno = ++plinno;
8231556Srgrimes				if (doprompt)
8241556Srgrimes					setprompt(2);
8251556Srgrimes				else
8261556Srgrimes					setprompt(0);
8271556Srgrimes				continue;
8281556Srgrimes			}
8291556Srgrimes			pungetc();
8301556Srgrimes			goto breakloop;
8311556Srgrimes		case '\n':
8321556Srgrimes			plinno++;
8331556Srgrimes			needprompt = doprompt;
8341556Srgrimes			RETURN(TNL);
8351556Srgrimes		case PEOF:
8361556Srgrimes			RETURN(TEOF);
8371556Srgrimes		case '&':
8381556Srgrimes			if (pgetc() == '&')
8391556Srgrimes				RETURN(TAND);
8401556Srgrimes			pungetc();
8411556Srgrimes			RETURN(TBACKGND);
8421556Srgrimes		case '|':
8431556Srgrimes			if (pgetc() == '|')
8441556Srgrimes				RETURN(TOR);
8451556Srgrimes			pungetc();
8461556Srgrimes			RETURN(TPIPE);
8471556Srgrimes		case ';':
8481556Srgrimes			if (pgetc() == ';')
8491556Srgrimes				RETURN(TENDCASE);
8501556Srgrimes			pungetc();
8511556Srgrimes			RETURN(TSEMI);
8521556Srgrimes		case '(':
8531556Srgrimes			RETURN(TLP);
8541556Srgrimes		case ')':
8551556Srgrimes			RETURN(TRP);
8561556Srgrimes		default:
8571556Srgrimes			goto breakloop;
8581556Srgrimes		}
8591556Srgrimes	}
8601556Srgrimesbreakloop:
8611556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
8621556Srgrimes#undef RETURN
8631556Srgrimes}
8641556Srgrimes
8651556Srgrimes
8661556Srgrimes
8671556Srgrimes/*
8681556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
8691556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
8701556Srgrimes * word which marks the end of the document and striptabs is true if
8711556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
8721556Srgrimes * is the first character of the input token or document.
8731556Srgrimes *
8741556Srgrimes * Because C does not have internal subroutines, I have simulated them
8751556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
8761556Srgrimes * will run code that appears at the end of readtoken1.
8771556Srgrimes */
8781556Srgrimes
8791556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
8801556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
8811556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
8821556Srgrimes#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
8831556Srgrimes#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
8841556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
8851556Srgrimes
8861556SrgrimesSTATIC int
88790111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
88890111Simp{
88917987Speter	int c = firstc;
89017987Speter	char *out;
8911556Srgrimes	int len;
8921556Srgrimes	char line[EOFMARKLEN + 1];
8931556Srgrimes	struct nodelist *bqlist;
8941556Srgrimes	int quotef;
8951556Srgrimes	int dblquote;
8961556Srgrimes	int varnest;	/* levels of variables expansion */
8971556Srgrimes	int arinest;	/* levels of arithmetic expansion */
8981556Srgrimes	int parenlevel;	/* levels of parens in arithmetic */
8991556Srgrimes	int oldstyle;
9001556Srgrimes	char const *prevsyntax;	/* syntax before arithmetic */
90154679Scracauer	int synentry;
90217987Speter#if __GNUC__
90317987Speter	/* Avoid longjmp clobbering */
90417987Speter	(void) &out;
90517987Speter	(void) &quotef;
90617987Speter	(void) &dblquote;
90717987Speter	(void) &varnest;
90817987Speter	(void) &arinest;
90917987Speter	(void) &parenlevel;
91017987Speter	(void) &oldstyle;
91117987Speter	(void) &prevsyntax;
91217987Speter	(void) &syntax;
91354679Scracauer	(void) &synentry;
91417987Speter#endif
9151556Srgrimes
9161556Srgrimes	startlinno = plinno;
9171556Srgrimes	dblquote = 0;
9181556Srgrimes	if (syntax == DQSYNTAX)
9191556Srgrimes		dblquote = 1;
9201556Srgrimes	quotef = 0;
9211556Srgrimes	bqlist = NULL;
9221556Srgrimes	varnest = 0;
9231556Srgrimes	arinest = 0;
9241556Srgrimes	parenlevel = 0;
9251556Srgrimes
9261556Srgrimes	STARTSTACKSTR(out);
9271556Srgrimes	loop: {	/* for each line, until end of word */
9281556Srgrimes#if ATTY
9291556Srgrimes		if (c == '\034' && doprompt
9301556Srgrimes		 && attyset() && ! equal(termval(), "emacs")) {
9311556Srgrimes			attyline();
9321556Srgrimes			if (syntax == BASESYNTAX)
9331556Srgrimes				return readtoken();
9341556Srgrimes			c = pgetc();
9351556Srgrimes			goto loop;
9361556Srgrimes		}
9371556Srgrimes#endif
9381556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
9391556Srgrimes		for (;;) {	/* until end of line or end of word */
9401556Srgrimes			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
94154679Scracauer
94264705Scracauer			synentry = syntax[c];
94354679Scracauer
94454679Scracauer			switch(synentry) {
9451556Srgrimes			case CNL:	/* '\n' */
9461556Srgrimes				if (syntax == BASESYNTAX)
9471556Srgrimes					goto endword;	/* exit outer loop */
9481556Srgrimes				USTPUTC(c, out);
9491556Srgrimes				plinno++;
9501556Srgrimes				if (doprompt)
9511556Srgrimes					setprompt(2);
9521556Srgrimes				else
9531556Srgrimes					setprompt(0);
9541556Srgrimes				c = pgetc();
9551556Srgrimes				goto loop;		/* continue outer loop */
9561556Srgrimes			case CWORD:
9571556Srgrimes				USTPUTC(c, out);
9581556Srgrimes				break;
9591556Srgrimes			case CCTL:
9601556Srgrimes				if (eofmark == NULL || dblquote)
9611556Srgrimes					USTPUTC(CTLESC, out);
9621556Srgrimes				USTPUTC(c, out);
9631556Srgrimes				break;
9641556Srgrimes			case CBACK:	/* backslash */
9651556Srgrimes				c = pgetc();
9661556Srgrimes				if (c == PEOF) {
9671556Srgrimes					USTPUTC('\\', out);
9681556Srgrimes					pungetc();
9691556Srgrimes				} else if (c == '\n') {
9701556Srgrimes					if (doprompt)
9711556Srgrimes						setprompt(2);
9721556Srgrimes					else
9731556Srgrimes						setprompt(0);
9741556Srgrimes				} else {
97554631Scracauer					if (dblquote && c != '\\' &&
97654631Scracauer					    c != '`' && c != '$' &&
97754631Scracauer					    (c != '"' || eofmark != NULL))
9781556Srgrimes						USTPUTC('\\', out);
97983675Stegge					if (SQSYNTAX[c] == CCTL)
9801556Srgrimes						USTPUTC(CTLESC, out);
98139137Stegge					else if (eofmark == NULL)
98238887Stegge						USTPUTC(CTLQUOTEMARK, out);
9831556Srgrimes					USTPUTC(c, out);
9841556Srgrimes					quotef++;
9851556Srgrimes				}
9861556Srgrimes				break;
9871556Srgrimes			case CSQUOTE:
98839137Stegge				if (eofmark == NULL)
98939137Stegge					USTPUTC(CTLQUOTEMARK, out);
9901556Srgrimes				syntax = SQSYNTAX;
9911556Srgrimes				break;
9921556Srgrimes			case CDQUOTE:
99339137Stegge				if (eofmark == NULL)
99439137Stegge					USTPUTC(CTLQUOTEMARK, out);
9951556Srgrimes				syntax = DQSYNTAX;
9961556Srgrimes				dblquote = 1;
9971556Srgrimes				break;
9981556Srgrimes			case CENDQUOTE:
99939137Stegge				if (eofmark != NULL && arinest == 0 &&
100039137Stegge				    varnest == 0) {
10011556Srgrimes					USTPUTC(c, out);
10021556Srgrimes				} else {
100339137Stegge					if (arinest) {
10041556Srgrimes						syntax = ARISYNTAX;
100539137Stegge						dblquote = 0;
100639137Stegge					} else if (eofmark == NULL) {
10071556Srgrimes						syntax = BASESYNTAX;
100839137Stegge						dblquote = 0;
100939137Stegge					}
10101556Srgrimes					quotef++;
10111556Srgrimes				}
10121556Srgrimes				break;
10131556Srgrimes			case CVAR:	/* '$' */
10141556Srgrimes				PARSESUB();		/* parse substitution */
10151556Srgrimes				break;
10161556Srgrimes			case CENDVAR:	/* '}' */
10171556Srgrimes				if (varnest > 0) {
10181556Srgrimes					varnest--;
10191556Srgrimes					USTPUTC(CTLENDVAR, out);
10201556Srgrimes				} else {
10211556Srgrimes					USTPUTC(c, out);
10221556Srgrimes				}
10231556Srgrimes				break;
10241556Srgrimes			case CLP:	/* '(' in arithmetic */
10251556Srgrimes				parenlevel++;
10261556Srgrimes				USTPUTC(c, out);
10271556Srgrimes				break;
10281556Srgrimes			case CRP:	/* ')' in arithmetic */
10291556Srgrimes				if (parenlevel > 0) {
10301556Srgrimes					USTPUTC(c, out);
10311556Srgrimes					--parenlevel;
10321556Srgrimes				} else {
10331556Srgrimes					if (pgetc() == ')') {
10341556Srgrimes						if (--arinest == 0) {
10351556Srgrimes							USTPUTC(CTLENDARI, out);
10361556Srgrimes							syntax = prevsyntax;
103739137Stegge							if (syntax == DQSYNTAX)
103839137Stegge								dblquote = 1;
103939137Stegge							else
104039137Stegge								dblquote = 0;
10411556Srgrimes						} else
10421556Srgrimes							USTPUTC(')', out);
10431556Srgrimes					} else {
10448855Srgrimes						/*
10451556Srgrimes						 * unbalanced parens
10461556Srgrimes						 *  (don't 2nd guess - no error)
10471556Srgrimes						 */
10481556Srgrimes						pungetc();
10491556Srgrimes						USTPUTC(')', out);
10501556Srgrimes					}
10511556Srgrimes				}
10521556Srgrimes				break;
10531556Srgrimes			case CBQUOTE:	/* '`' */
10541556Srgrimes				PARSEBACKQOLD();
10551556Srgrimes				break;
10561556Srgrimes			case CEOF:
10571556Srgrimes				goto endword;		/* exit outer loop */
10581556Srgrimes			default:
10591556Srgrimes				if (varnest == 0)
10601556Srgrimes					goto endword;	/* exit outer loop */
10611556Srgrimes				USTPUTC(c, out);
10621556Srgrimes			}
10631556Srgrimes			c = pgetc_macro();
10641556Srgrimes		}
10651556Srgrimes	}
10661556Srgrimesendword:
10671556Srgrimes	if (syntax == ARISYNTAX)
10681556Srgrimes		synerror("Missing '))'");
10691556Srgrimes	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
10701556Srgrimes		synerror("Unterminated quoted string");
10711556Srgrimes	if (varnest != 0) {
10721556Srgrimes		startlinno = plinno;
10731556Srgrimes		synerror("Missing '}'");
10741556Srgrimes	}
10751556Srgrimes	USTPUTC('\0', out);
10761556Srgrimes	len = out - stackblock();
10771556Srgrimes	out = stackblock();
10781556Srgrimes	if (eofmark == NULL) {
10791556Srgrimes		if ((c == '>' || c == '<')
10801556Srgrimes		 && quotef == 0
10811556Srgrimes		 && len <= 2
10821556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
10831556Srgrimes			PARSEREDIR();
10841556Srgrimes			return lasttoken = TREDIR;
10851556Srgrimes		} else {
10861556Srgrimes			pungetc();
10871556Srgrimes		}
10881556Srgrimes	}
10891556Srgrimes	quoteflag = quotef;
10901556Srgrimes	backquotelist = bqlist;
10911556Srgrimes	grabstackblock(len);
10921556Srgrimes	wordtext = out;
10931556Srgrimes	return lasttoken = TWORD;
10941556Srgrimes/* end of readtoken routine */
10951556Srgrimes
10961556Srgrimes
10971556Srgrimes
10981556Srgrimes/*
10991556Srgrimes * Check to see whether we are at the end of the here document.  When this
11001556Srgrimes * is called, c is set to the first character of the next input line.  If
11011556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
11021556Srgrimes */
11031556Srgrimes
11041556Srgrimescheckend: {
11051556Srgrimes	if (eofmark) {
11061556Srgrimes		if (striptabs) {
11071556Srgrimes			while (c == '\t')
11081556Srgrimes				c = pgetc();
11091556Srgrimes		}
11101556Srgrimes		if (c == *eofmark) {
11111556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
111225230Ssteve				char *p, *q;
11131556Srgrimes
11141556Srgrimes				p = line;
11151556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
11161556Srgrimes				if (*p == '\n' && *q == '\0') {
11171556Srgrimes					c = PEOF;
11181556Srgrimes					plinno++;
11191556Srgrimes					needprompt = doprompt;
11201556Srgrimes				} else {
11211556Srgrimes					pushstring(line, strlen(line), NULL);
11221556Srgrimes				}
11231556Srgrimes			}
11241556Srgrimes		}
11251556Srgrimes	}
11261556Srgrimes	goto checkend_return;
11271556Srgrimes}
11281556Srgrimes
11291556Srgrimes
11301556Srgrimes/*
11311556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
11321556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
11331556Srgrimes * first character of the redirection operator.
11341556Srgrimes */
11351556Srgrimes
11361556Srgrimesparseredir: {
11371556Srgrimes	char fd = *out;
11381556Srgrimes	union node *np;
11391556Srgrimes
11401556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
11411556Srgrimes	if (c == '>') {
11421556Srgrimes		np->nfile.fd = 1;
11431556Srgrimes		c = pgetc();
11441556Srgrimes		if (c == '>')
11451556Srgrimes			np->type = NAPPEND;
11461556Srgrimes		else if (c == '&')
11471556Srgrimes			np->type = NTOFD;
114896922Stjr		else if (c == '|')
114996922Stjr			np->type = NCLOBBER;
11501556Srgrimes		else {
11511556Srgrimes			np->type = NTO;
11521556Srgrimes			pungetc();
11531556Srgrimes		}
11541556Srgrimes	} else {	/* c == '<' */
11551556Srgrimes		np->nfile.fd = 0;
11561556Srgrimes		c = pgetc();
11571556Srgrimes		if (c == '<') {
11581556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
11591556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
11601556Srgrimes				np->nfile.fd = 0;
11611556Srgrimes			}
11621556Srgrimes			np->type = NHERE;
11631556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
11641556Srgrimes			heredoc->here = np;
11651556Srgrimes			if ((c = pgetc()) == '-') {
11661556Srgrimes				heredoc->striptabs = 1;
11671556Srgrimes			} else {
11681556Srgrimes				heredoc->striptabs = 0;
11691556Srgrimes				pungetc();
11701556Srgrimes			}
11711556Srgrimes		} else if (c == '&')
11721556Srgrimes			np->type = NFROMFD;
117366612Sbrian		else if (c == '>')
117466612Sbrian			np->type = NFROMTO;
11751556Srgrimes		else {
11761556Srgrimes			np->type = NFROM;
11771556Srgrimes			pungetc();
11781556Srgrimes		}
11791556Srgrimes	}
11801556Srgrimes	if (fd != '\0')
11811556Srgrimes		np->nfile.fd = digit_val(fd);
11821556Srgrimes	redirnode = np;
11831556Srgrimes	goto parseredir_return;
11841556Srgrimes}
11851556Srgrimes
11861556Srgrimes
11871556Srgrimes/*
11881556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
11891556Srgrimes * and nothing else.
11901556Srgrimes */
11911556Srgrimes
11921556Srgrimesparsesub: {
11931556Srgrimes	int subtype;
11941556Srgrimes	int typeloc;
11951556Srgrimes	int flags;
11961556Srgrimes	char *p;
11971556Srgrimes#ifndef GDB_HACK
11981556Srgrimes	static const char types[] = "}-+?=";
11991556Srgrimes#endif
120018202Speter       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
12011556Srgrimes
12021556Srgrimes	c = pgetc();
12031556Srgrimes	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
12041556Srgrimes		USTPUTC('$', out);
12051556Srgrimes		pungetc();
12061556Srgrimes	} else if (c == '(') {	/* $(command) or $((arith)) */
12071556Srgrimes		if (pgetc() == '(') {
12081556Srgrimes			PARSEARITH();
12091556Srgrimes		} else {
12101556Srgrimes			pungetc();
12111556Srgrimes			PARSEBACKQNEW();
12121556Srgrimes		}
12131556Srgrimes	} else {
12141556Srgrimes		USTPUTC(CTLVAR, out);
12151556Srgrimes		typeloc = out - stackblock();
12161556Srgrimes		USTPUTC(VSNORMAL, out);
12171556Srgrimes		subtype = VSNORMAL;
12181556Srgrimes		if (c == '{') {
121918202Speter			bracketed_name = 1;
12201556Srgrimes			c = pgetc();
122117987Speter			if (c == '#') {
122217987Speter				if ((c = pgetc()) == '}')
122317987Speter					c = '#';
122417987Speter				else
122517987Speter					subtype = VSLENGTH;
122617987Speter			}
122717987Speter			else
122817987Speter				subtype = 0;
12291556Srgrimes		}
12301556Srgrimes		if (is_name(c)) {
12311556Srgrimes			do {
12321556Srgrimes				STPUTC(c, out);
12331556Srgrimes				c = pgetc();
12341556Srgrimes			} while (is_in_name(c));
123518202Speter		} else if (is_digit(c)) {
123618202Speter			if (bracketed_name) {
123718202Speter				do {
123818202Speter					STPUTC(c, out);
123918202Speter					c = pgetc();
124018202Speter				} while (is_digit(c));
124118202Speter			} else {
124218202Speter				STPUTC(c, out);
124318202Speter				c = pgetc();
124418202Speter			}
12451556Srgrimes		} else {
12461556Srgrimes			if (! is_special(c))
12471556Srgrimesbadsub:				synerror("Bad substitution");
12481556Srgrimes			USTPUTC(c, out);
12491556Srgrimes			c = pgetc();
12501556Srgrimes		}
12511556Srgrimes		STPUTC('=', out);
12521556Srgrimes		flags = 0;
12531556Srgrimes		if (subtype == 0) {
125417987Speter			switch (c) {
125517987Speter			case ':':
12561556Srgrimes				flags = VSNUL;
12571556Srgrimes				c = pgetc();
125817987Speter				/*FALLTHROUGH*/
125917987Speter			default:
126017987Speter				p = strchr(types, c);
126117987Speter				if (p == NULL)
126217987Speter					goto badsub;
126317987Speter				subtype = p - types + VSNORMAL;
126417987Speter				break;
126517987Speter			case '%':
126620425Ssteve			case '#':
126717987Speter				{
126817987Speter					int cc = c;
126917987Speter					subtype = c == '#' ? VSTRIMLEFT :
127017987Speter							     VSTRIMRIGHT;
127117987Speter					c = pgetc();
127217987Speter					if (c == cc)
127317987Speter						subtype++;
127417987Speter					else
127517987Speter						pungetc();
127617987Speter					break;
127717987Speter				}
12781556Srgrimes			}
12791556Srgrimes		} else {
12801556Srgrimes			pungetc();
12811556Srgrimes		}
128257225Scracauer		if (subtype != VSLENGTH && (dblquote || arinest))
12831556Srgrimes			flags |= VSQUOTE;
12841556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
12851556Srgrimes		if (subtype != VSNORMAL)
12861556Srgrimes			varnest++;
12871556Srgrimes	}
12881556Srgrimes	goto parsesub_return;
12891556Srgrimes}
12901556Srgrimes
12911556Srgrimes
12921556Srgrimes/*
12931556Srgrimes * Called to parse command substitutions.  Newstyle is set if the command
12941556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12951556Srgrimes * list of commands (passed by reference), and savelen is the number of
12961556Srgrimes * characters on the top of the stack which must be preserved.
12971556Srgrimes */
12981556Srgrimes
12991556Srgrimesparsebackq: {
13001556Srgrimes	struct nodelist **nlpp;
13011556Srgrimes	int savepbq;
13021556Srgrimes	union node *n;
13031556Srgrimes	char *volatile str;
13041556Srgrimes	struct jmploc jmploc;
13051556Srgrimes	struct jmploc *volatile savehandler;
13061556Srgrimes	int savelen;
130720425Ssteve	int saveprompt;
130820425Ssteve#if __GNUC__
130920425Ssteve	/* Avoid longjmp clobbering */
131020425Ssteve	(void) &saveprompt;
131120425Ssteve#endif
13121556Srgrimes
13131556Srgrimes	savepbq = parsebackquote;
13141556Srgrimes	if (setjmp(jmploc.loc)) {
13151556Srgrimes		if (str)
13161556Srgrimes			ckfree(str);
13171556Srgrimes		parsebackquote = 0;
13181556Srgrimes		handler = savehandler;
13191556Srgrimes		longjmp(handler->loc, 1);
13201556Srgrimes	}
13211556Srgrimes	INTOFF;
13221556Srgrimes	str = NULL;
13231556Srgrimes	savelen = out - stackblock();
13241556Srgrimes	if (savelen > 0) {
13251556Srgrimes		str = ckmalloc(savelen);
132617987Speter		memcpy(str, stackblock(), savelen);
13271556Srgrimes	}
13281556Srgrimes	savehandler = handler;
13291556Srgrimes	handler = &jmploc;
13301556Srgrimes	INTON;
13311556Srgrimes        if (oldstyle) {
13321556Srgrimes                /* We must read until the closing backquote, giving special
13331556Srgrimes                   treatment to some slashes, and then push the string and
13341556Srgrimes                   reread it as input, interpreting it normally.  */
133525230Ssteve                char *out;
133625230Ssteve                int c;
13371556Srgrimes                int savelen;
13381556Srgrimes                char *str;
13398855Srgrimes
134020425Ssteve
13411556Srgrimes                STARTSTACKSTR(out);
134220425Ssteve		for (;;) {
134320425Ssteve			if (needprompt) {
134420425Ssteve				setprompt(2);
134520425Ssteve				needprompt = 0;
134620425Ssteve			}
134720425Ssteve			switch (c = pgetc()) {
134820425Ssteve			case '`':
134920425Ssteve				goto done;
135020425Ssteve
135120425Ssteve			case '\\':
135220425Ssteve                                if ((c = pgetc()) == '\n') {
135320425Ssteve					plinno++;
135420425Ssteve					if (doprompt)
135520425Ssteve						setprompt(2);
135620425Ssteve					else
135720425Ssteve						setprompt(0);
135820425Ssteve					/*
135920425Ssteve					 * If eating a newline, avoid putting
136020425Ssteve					 * the newline into the new character
136120425Ssteve					 * stream (via the STPUTC after the
136220425Ssteve					 * switch).
136320425Ssteve					 */
136420425Ssteve					continue;
136520425Ssteve				}
136617987Speter                                if (c != '\\' && c != '`' && c != '$'
13671556Srgrimes                                    && (!dblquote || c != '"'))
13681556Srgrimes                                        STPUTC('\\', out);
136920425Ssteve				break;
137020425Ssteve
137120425Ssteve			case '\n':
137220425Ssteve				plinno++;
137320425Ssteve				needprompt = doprompt;
137420425Ssteve				break;
137520425Ssteve
137620425Ssteve			case PEOF:
137720425Ssteve			        startlinno = plinno;
137820425Ssteve				synerror("EOF in backquote substitution");
137920425Ssteve 				break;
138020425Ssteve
138120425Ssteve			default:
138220425Ssteve				break;
138320425Ssteve			}
138420425Ssteve			STPUTC(c, out);
13851556Srgrimes                }
138620425Sstevedone:
13871556Srgrimes                STPUTC('\0', out);
13881556Srgrimes                savelen = out - stackblock();
13891556Srgrimes                if (savelen > 0) {
13901556Srgrimes                        str = ckmalloc(savelen);
139117987Speter                        memcpy(str, stackblock(), savelen);
139217987Speter			setinputstring(str, 1);
13931556Srgrimes                }
13941556Srgrimes        }
13951556Srgrimes	nlpp = &bqlist;
13961556Srgrimes	while (*nlpp)
13971556Srgrimes		nlpp = &(*nlpp)->next;
13981556Srgrimes	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
13991556Srgrimes	(*nlpp)->next = NULL;
14001556Srgrimes	parsebackquote = oldstyle;
140120425Ssteve
140220425Ssteve	if (oldstyle) {
140320425Ssteve		saveprompt = doprompt;
140420425Ssteve		doprompt = 0;
140520425Ssteve	}
140620425Ssteve
14071556Srgrimes	n = list(0);
140820425Ssteve
140920425Ssteve	if (oldstyle)
141020425Ssteve		doprompt = saveprompt;
141120425Ssteve	else {
141220425Ssteve		if (readtoken() != TRP)
141320425Ssteve			synexpect(TRP);
141420425Ssteve	}
141520425Ssteve
14161556Srgrimes	(*nlpp)->n = n;
141720425Ssteve        if (oldstyle) {
141820425Ssteve		/*
141920425Ssteve		 * Start reading from old file again, ignoring any pushed back
142020425Ssteve		 * tokens left from the backquote parsing
142120425Ssteve		 */
14221556Srgrimes                popfile();
142320425Ssteve		tokpushback = 0;
142420425Ssteve	}
14251556Srgrimes	while (stackblocksize() <= savelen)
14261556Srgrimes		growstackblock();
14271556Srgrimes	STARTSTACKSTR(out);
14281556Srgrimes	if (str) {
142917987Speter		memcpy(out, str, savelen);
14301556Srgrimes		STADJUST(savelen, out);
14311556Srgrimes		INTOFF;
14321556Srgrimes		ckfree(str);
14331556Srgrimes		str = NULL;
14341556Srgrimes		INTON;
14351556Srgrimes	}
14361556Srgrimes	parsebackquote = savepbq;
14371556Srgrimes	handler = savehandler;
14381556Srgrimes	if (arinest || dblquote)
14391556Srgrimes		USTPUTC(CTLBACKQ | CTLQUOTE, out);
14401556Srgrimes	else
14411556Srgrimes		USTPUTC(CTLBACKQ, out);
14421556Srgrimes	if (oldstyle)
14431556Srgrimes		goto parsebackq_oldreturn;
14441556Srgrimes	else
14451556Srgrimes		goto parsebackq_newreturn;
14461556Srgrimes}
14471556Srgrimes
14481556Srgrimes/*
14491556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
14501556Srgrimes */
14511556Srgrimesparsearith: {
14521556Srgrimes
14531556Srgrimes	if (++arinest == 1) {
14541556Srgrimes		prevsyntax = syntax;
14551556Srgrimes		syntax = ARISYNTAX;
14561556Srgrimes		USTPUTC(CTLARI, out);
145738887Stegge		if (dblquote)
145838887Stegge			USTPUTC('"',out);
145938887Stegge		else
146038887Stegge			USTPUTC(' ',out);
14611556Srgrimes	} else {
14621556Srgrimes		/*
14631556Srgrimes		 * we collapse embedded arithmetic expansion to
14641556Srgrimes		 * parenthesis, which should be equivalent
14651556Srgrimes		 */
14661556Srgrimes		USTPUTC('(', out);
14671556Srgrimes	}
14681556Srgrimes	goto parsearith_return;
14691556Srgrimes}
14701556Srgrimes
14711556Srgrimes} /* end of readtoken */
14721556Srgrimes
14731556Srgrimes
14741556Srgrimes
14751556Srgrimes#ifdef mkinit
14761556SrgrimesRESET {
14771556Srgrimes	tokpushback = 0;
14781556Srgrimes	checkkwd = 0;
14791556Srgrimes}
14801556Srgrimes#endif
14811556Srgrimes
14821556Srgrimes/*
14831556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
14841556Srgrimes * or backquotes).
14851556Srgrimes */
14861556Srgrimes
14871556SrgrimesSTATIC int
148890111Simpnoexpand(char *text)
148990111Simp{
149025230Ssteve	char *p;
149125230Ssteve	char c;
14921556Srgrimes
14931556Srgrimes	p = text;
14941556Srgrimes	while ((c = *p++) != '\0') {
149539137Stegge		if ( c == CTLQUOTEMARK)
149639137Stegge			continue;
14971556Srgrimes		if (c == CTLESC)
14981556Srgrimes			p++;
149983675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
15001556Srgrimes			return 0;
15011556Srgrimes	}
15021556Srgrimes	return 1;
15031556Srgrimes}
15041556Srgrimes
15051556Srgrimes
15061556Srgrimes/*
15071556Srgrimes * Return true if the argument is a legal variable name (a letter or
15081556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
15091556Srgrimes */
15101556Srgrimes
15111556Srgrimesint
151290111Simpgoodname(char *name)
151390111Simp{
151425230Ssteve	char *p;
15151556Srgrimes
15161556Srgrimes	p = name;
15171556Srgrimes	if (! is_name(*p))
15181556Srgrimes		return 0;
15191556Srgrimes	while (*++p) {
15201556Srgrimes		if (! is_in_name(*p))
15211556Srgrimes			return 0;
15221556Srgrimes	}
15231556Srgrimes	return 1;
15241556Srgrimes}
15251556Srgrimes
15261556Srgrimes
15271556Srgrimes/*
15281556Srgrimes * Called when an unexpected token is read during the parse.  The argument
15291556Srgrimes * is the token that is expected, or -1 if more than one type of token can
15301556Srgrimes * occur at this point.
15311556Srgrimes */
15321556Srgrimes
15331556SrgrimesSTATIC void
153490111Simpsynexpect(int token)
153517987Speter{
15361556Srgrimes	char msg[64];
15371556Srgrimes
15381556Srgrimes	if (token >= 0) {
15391556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
15401556Srgrimes			tokname[lasttoken], tokname[token]);
15411556Srgrimes	} else {
15421556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
15431556Srgrimes	}
15441556Srgrimes	synerror(msg);
15451556Srgrimes}
15461556Srgrimes
15471556Srgrimes
15481556SrgrimesSTATIC void
154990111Simpsynerror(char *msg)
155090111Simp{
15511556Srgrimes	if (commandname)
15521556Srgrimes		outfmt(&errout, "%s: %d: ", commandname, startlinno);
15531556Srgrimes	outfmt(&errout, "Syntax error: %s\n", msg);
15541556Srgrimes	error((char *)NULL);
15551556Srgrimes}
15561556Srgrimes
15571556SrgrimesSTATIC void
155890111Simpsetprompt(int which)
155990111Simp{
15601556Srgrimes	whichprompt = which;
15611556Srgrimes
156217987Speter#ifndef NO_HISTORY
15631556Srgrimes	if (!el)
156417987Speter#endif
15651556Srgrimes		out2str(getprompt(NULL));
15661556Srgrimes}
15671556Srgrimes
15681556Srgrimes/*
15691556Srgrimes * called by editline -- any expansions to the prompt
15701556Srgrimes *    should be added here.
15711556Srgrimes */
15721556Srgrimeschar *
157390111Simpgetprompt(void *unused __unused)
157425905Ssteve{
15751556Srgrimes	switch (whichprompt) {
15761556Srgrimes	case 0:
15771556Srgrimes		return "";
15781556Srgrimes	case 1:
15791556Srgrimes		return ps1val();
15801556Srgrimes	case 2:
15811556Srgrimes		return ps2val();
15821556Srgrimes	default:
15831556Srgrimes		return "<internal prompt error>";
15841556Srgrimes	}
15851556Srgrimes}
1586