parser.c revision 66612
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[] =
4250471Speter  "$FreeBSD: head/bin/sh/parser.c 66612 2000-10-03 23:13:14Z brian $";
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"
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
1111556SrgrimesSTATIC union node *list __P((int));
1121556SrgrimesSTATIC union node *andor __P((void));
1131556SrgrimesSTATIC union node *pipeline __P((void));
1141556SrgrimesSTATIC union node *command __P((void));
1151556SrgrimesSTATIC union node *simplecmd __P((union node **, union node *));
11617987SpeterSTATIC union node *makename __P((void));
1171556SrgrimesSTATIC void parsefname __P((void));
1181556SrgrimesSTATIC void parseheredoc __P((void));
11917987SpeterSTATIC int peektoken __P((void));
1201556SrgrimesSTATIC int readtoken __P((void));
12117987SpeterSTATIC int xxreadtoken __P((void));
1221556SrgrimesSTATIC int readtoken1 __P((int, char const *, char *, int));
1231556SrgrimesSTATIC int noexpand __P((char *));
1241556SrgrimesSTATIC void synexpect __P((int));
1251556SrgrimesSTATIC void synerror __P((char *));
12620425SsteveSTATIC void setprompt __P((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 *
13520425Ssteveparsecmd(interact)
13617987Speter	int interact;
13717987Speter{
1381556Srgrimes	int t;
1391556Srgrimes
14060593Scracauer	tokpushback = 0;
1411556Srgrimes	doprompt = interact;
1421556Srgrimes	if (doprompt)
1431556Srgrimes		setprompt(1);
1441556Srgrimes	else
1451556Srgrimes		setprompt(0);
1461556Srgrimes	needprompt = 0;
1471556Srgrimes	t = readtoken();
1481556Srgrimes	if (t == TEOF)
1491556Srgrimes		return NEOF;
1501556Srgrimes	if (t == TNL)
1511556Srgrimes		return NULL;
1521556Srgrimes	tokpushback++;
1531556Srgrimes	return list(1);
1541556Srgrimes}
1551556Srgrimes
1561556Srgrimes
1571556SrgrimesSTATIC union node *
15820425Sstevelist(nlflag)
15917987Speter	int nlflag;
16017987Speter{
1611556Srgrimes	union node *n1, *n2, *n3;
16217987Speter	int tok;
1631556Srgrimes
1641556Srgrimes	checkkwd = 2;
1651556Srgrimes	if (nlflag == 0 && tokendlist[peektoken()])
1661556Srgrimes		return NULL;
16717987Speter	n1 = NULL;
1681556Srgrimes	for (;;) {
16917987Speter		n2 = andor();
17017987Speter		tok = readtoken();
17117987Speter		if (tok == TBACKGND) {
17217987Speter			if (n2->type == NCMD || n2->type == NPIPE) {
17317987Speter				n2->ncmd.backgnd = 1;
17417987Speter			} else if (n2->type == NREDIR) {
17517987Speter				n2->type = NBACKGND;
17617987Speter			} else {
17717987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
17817987Speter				n3->type = NBACKGND;
17917987Speter				n3->nredir.n = n2;
18017987Speter				n3->nredir.redirect = NULL;
18117987Speter				n2 = n3;
18217987Speter			}
18317987Speter		}
18417987Speter		if (n1 == NULL) {
18517987Speter			n1 = n2;
18617987Speter		}
18717987Speter		else {
18817987Speter			n3 = (union node *)stalloc(sizeof (struct nbinary));
18917987Speter			n3->type = NSEMI;
19017987Speter			n3->nbinary.ch1 = n1;
19117987Speter			n3->nbinary.ch2 = n2;
19217987Speter			n1 = n3;
19317987Speter		}
19417987Speter		switch (tok) {
19513882Sjoerg		case TBACKGND:
19617987Speter		case TSEMI:
19717987Speter			tok = readtoken();
19817987Speter			/* fall through */
1991556Srgrimes		case TNL:
20017987Speter			if (tok == TNL) {
20117987Speter				parseheredoc();
20217987Speter				if (nlflag)
20317987Speter					return n1;
20417987Speter			} else {
20517987Speter				tokpushback++;
20617987Speter			}
2071556Srgrimes			checkkwd = 2;
2081556Srgrimes			if (tokendlist[peektoken()])
2091556Srgrimes				return n1;
2101556Srgrimes			break;
2111556Srgrimes		case TEOF:
2121556Srgrimes			if (heredoclist)
2131556Srgrimes				parseheredoc();
2141556Srgrimes			else
2151556Srgrimes				pungetc();		/* push back EOF on input */
2161556Srgrimes			return n1;
2171556Srgrimes		default:
2181556Srgrimes			if (nlflag)
2191556Srgrimes				synexpect(-1);
2201556Srgrimes			tokpushback++;
2211556Srgrimes			return n1;
2221556Srgrimes		}
2231556Srgrimes	}
2241556Srgrimes}
2251556Srgrimes
2261556Srgrimes
2271556Srgrimes
2281556SrgrimesSTATIC union node *
2291556Srgrimesandor() {
2301556Srgrimes	union node *n1, *n2, *n3;
2311556Srgrimes	int t;
2321556Srgrimes
2331556Srgrimes	n1 = pipeline();
2341556Srgrimes	for (;;) {
2351556Srgrimes		if ((t = readtoken()) == TAND) {
2361556Srgrimes			t = NAND;
2371556Srgrimes		} else if (t == TOR) {
2381556Srgrimes			t = NOR;
2391556Srgrimes		} else {
2401556Srgrimes			tokpushback++;
2411556Srgrimes			return n1;
2421556Srgrimes		}
2431556Srgrimes		n2 = pipeline();
2441556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
2451556Srgrimes		n3->type = t;
2461556Srgrimes		n3->nbinary.ch1 = n1;
2471556Srgrimes		n3->nbinary.ch2 = n2;
2481556Srgrimes		n1 = n3;
2491556Srgrimes	}
2501556Srgrimes}
2511556Srgrimes
2521556Srgrimes
2531556Srgrimes
2541556SrgrimesSTATIC union node *
2551556Srgrimespipeline() {
25625230Ssteve	union node *n1, *pipenode, *notnode;
2571556Srgrimes	struct nodelist *lp, *prev;
25825230Ssteve	int negate = 0;
2591556Srgrimes
2601556Srgrimes	TRACE(("pipeline: entered\n"));
26125230Ssteve	while (readtoken() == TNOT) {
26225230Ssteve		TRACE(("pipeline: TNOT recognized\n"));
26325230Ssteve		negate = !negate;
26425230Ssteve	}
26525230Ssteve	tokpushback++;
2661556Srgrimes	n1 = command();
2671556Srgrimes	if (readtoken() == TPIPE) {
2681556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
2691556Srgrimes		pipenode->type = NPIPE;
2701556Srgrimes		pipenode->npipe.backgnd = 0;
2711556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2721556Srgrimes		pipenode->npipe.cmdlist = lp;
2731556Srgrimes		lp->n = n1;
2741556Srgrimes		do {
2751556Srgrimes			prev = lp;
2761556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2771556Srgrimes			lp->n = command();
2781556Srgrimes			prev->next = lp;
2791556Srgrimes		} while (readtoken() == TPIPE);
2801556Srgrimes		lp->next = NULL;
2811556Srgrimes		n1 = pipenode;
2821556Srgrimes	}
2831556Srgrimes	tokpushback++;
28425230Ssteve	if (negate) {
28525230Ssteve		notnode = (union node *)stalloc(sizeof(struct nnot));
28625230Ssteve		notnode->type = NNOT;
28725230Ssteve		notnode->nnot.com = n1;
28825230Ssteve		n1 = notnode;
28925230Ssteve	}
2901556Srgrimes	return n1;
2911556Srgrimes}
2921556Srgrimes
2931556Srgrimes
2941556Srgrimes
2951556SrgrimesSTATIC union node *
2961556Srgrimescommand() {
2971556Srgrimes	union node *n1, *n2;
2981556Srgrimes	union node *ap, **app;
2991556Srgrimes	union node *cp, **cpp;
3001556Srgrimes	union node *redir, **rpp;
30125230Ssteve	int t;
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
3161556Srgrimes	switch (readtoken()) {
3171556Srgrimes	case TIF:
3181556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3191556Srgrimes		n1->type = NIF;
3201556Srgrimes		n1->nif.test = list(0);
3211556Srgrimes		if (readtoken() != TTHEN)
3221556Srgrimes			synexpect(TTHEN);
3231556Srgrimes		n1->nif.ifpart = list(0);
3241556Srgrimes		n2 = n1;
3251556Srgrimes		while (readtoken() == TELIF) {
3261556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
3271556Srgrimes			n2 = n2->nif.elsepart;
3281556Srgrimes			n2->type = NIF;
3291556Srgrimes			n2->nif.test = list(0);
3301556Srgrimes			if (readtoken() != TTHEN)
3311556Srgrimes				synexpect(TTHEN);
3321556Srgrimes			n2->nif.ifpart = list(0);
3331556Srgrimes		}
3341556Srgrimes		if (lasttoken == TELSE)
3351556Srgrimes			n2->nif.elsepart = list(0);
3361556Srgrimes		else {
3371556Srgrimes			n2->nif.elsepart = NULL;
3381556Srgrimes			tokpushback++;
3391556Srgrimes		}
3401556Srgrimes		if (readtoken() != TFI)
3411556Srgrimes			synexpect(TFI);
3421556Srgrimes		checkkwd = 1;
3431556Srgrimes		break;
3441556Srgrimes	case TWHILE:
3451556Srgrimes	case TUNTIL: {
3461556Srgrimes		int got;
3471556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
3481556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
3491556Srgrimes		n1->nbinary.ch1 = list(0);
3501556Srgrimes		if ((got=readtoken()) != TDO) {
3511556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
3521556Srgrimes			synexpect(TDO);
3531556Srgrimes		}
3541556Srgrimes		n1->nbinary.ch2 = list(0);
3551556Srgrimes		if (readtoken() != TDONE)
3561556Srgrimes			synexpect(TDONE);
3571556Srgrimes		checkkwd = 1;
3581556Srgrimes		break;
3591556Srgrimes	}
3601556Srgrimes	case TFOR:
3611556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
3621556Srgrimes			synerror("Bad for loop variable");
3631556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
3641556Srgrimes		n1->type = NFOR;
3651556Srgrimes		n1->nfor.var = wordtext;
3661556Srgrimes		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
3671556Srgrimes			app = &ap;
3681556Srgrimes			while (readtoken() == TWORD) {
3691556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
3701556Srgrimes				n2->type = NARG;
3711556Srgrimes				n2->narg.text = wordtext;
3721556Srgrimes				n2->narg.backquote = backquotelist;
3731556Srgrimes				*app = n2;
3741556Srgrimes				app = &n2->narg.next;
3751556Srgrimes			}
3761556Srgrimes			*app = NULL;
3771556Srgrimes			n1->nfor.args = ap;
3781556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3791556Srgrimes				synexpect(-1);
3801556Srgrimes		} else {
3811556Srgrimes#ifndef GDB_HACK
3821556Srgrimes			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
3831556Srgrimes								   '@', '=', '\0'};
3841556Srgrimes#endif
3851556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
3861556Srgrimes			n2->type = NARG;
3871556Srgrimes			n2->narg.text = (char *)argvars;
3881556Srgrimes			n2->narg.backquote = NULL;
3891556Srgrimes			n2->narg.next = NULL;
3901556Srgrimes			n1->nfor.args = n2;
3911556Srgrimes			/*
3921556Srgrimes			 * Newline or semicolon here is optional (but note
3931556Srgrimes			 * that the original Bourne shell only allowed NL).
3941556Srgrimes			 */
3951556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3961556Srgrimes				tokpushback++;
3971556Srgrimes		}
3981556Srgrimes		checkkwd = 2;
3991556Srgrimes		if ((t = readtoken()) == TDO)
4001556Srgrimes			t = TDONE;
4011556Srgrimes		else if (t == TBEGIN)
4021556Srgrimes			t = TEND;
4031556Srgrimes		else
4041556Srgrimes			synexpect(-1);
4051556Srgrimes		n1->nfor.body = list(0);
4061556Srgrimes		if (readtoken() != t)
4071556Srgrimes			synexpect(t);
4081556Srgrimes		checkkwd = 1;
4091556Srgrimes		break;
4101556Srgrimes	case TCASE:
4111556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4121556Srgrimes		n1->type = NCASE;
4131556Srgrimes		if (readtoken() != TWORD)
4141556Srgrimes			synexpect(TWORD);
4151556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
4161556Srgrimes		n2->type = NARG;
4171556Srgrimes		n2->narg.text = wordtext;
4181556Srgrimes		n2->narg.backquote = backquotelist;
4191556Srgrimes		n2->narg.next = NULL;
4201556Srgrimes		while (readtoken() == TNL);
4211556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
4221556Srgrimes			synerror("expecting \"in\"");
4231556Srgrimes		cpp = &n1->ncase.cases;
42418018Speter		noaliases = 1;	/* turn off alias expansion */
4252760Ssef		checkkwd = 2, readtoken();
4262760Ssef		do {
4271556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
4281556Srgrimes			cp->type = NCLIST;
4291556Srgrimes			app = &cp->nclist.pattern;
4301556Srgrimes			for (;;) {
4311556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
4321556Srgrimes				ap->type = NARG;
4331556Srgrimes				ap->narg.text = wordtext;
4341556Srgrimes				ap->narg.backquote = backquotelist;
4352760Ssef				if (checkkwd = 2, readtoken() != TPIPE)
4361556Srgrimes					break;
4371556Srgrimes				app = &ap->narg.next;
4382760Ssef				readtoken();
4391556Srgrimes			}
4401556Srgrimes			ap->narg.next = NULL;
4411556Srgrimes			if (lasttoken != TRP)
44218018Speter				noaliases = 0, synexpect(TRP);
4431556Srgrimes			cp->nclist.body = list(0);
4442760Ssef
4452760Ssef			checkkwd = 2;
4462760Ssef			if ((t = readtoken()) != TESAC) {
4472760Ssef				if (t != TENDCASE)
44818018Speter					noaliases = 0, synexpect(TENDCASE);
4492760Ssef				else
4502760Ssef					checkkwd = 2, readtoken();
4512760Ssef			}
4521556Srgrimes			cpp = &cp->nclist.next;
4532760Ssef		} while(lasttoken != TESAC);
45418018Speter		noaliases = 0;	/* reset alias expansion */
4551556Srgrimes		*cpp = NULL;
4561556Srgrimes		checkkwd = 1;
4571556Srgrimes		break;
4581556Srgrimes	case TLP:
4591556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
4601556Srgrimes		n1->type = NSUBSHELL;
4611556Srgrimes		n1->nredir.n = list(0);
4621556Srgrimes		n1->nredir.redirect = NULL;
4631556Srgrimes		if (readtoken() != TRP)
4641556Srgrimes			synexpect(TRP);
4651556Srgrimes		checkkwd = 1;
4661556Srgrimes		break;
4671556Srgrimes	case TBEGIN:
4681556Srgrimes		n1 = list(0);
4691556Srgrimes		if (readtoken() != TEND)
4701556Srgrimes			synexpect(TEND);
4711556Srgrimes		checkkwd = 1;
4721556Srgrimes		break;
4731556Srgrimes	/* Handle an empty command like other simple commands.  */
47417987Speter	case TSEMI:
47517987Speter		/*
47617987Speter		 * An empty command before a ; doesn't make much sense, and
47717987Speter		 * should certainly be disallowed in the case of `if ;'.
47817987Speter		 */
47917987Speter		if (!redir)
48017987Speter			synexpect(-1);
48120425Ssteve	case TAND:
48220425Ssteve	case TOR:
4831556Srgrimes	case TNL:
48410399Sjoerg	case TEOF:
4851556Srgrimes	case TWORD:
48617987Speter	case TRP:
4871556Srgrimes		tokpushback++;
48825230Ssteve		return simplecmd(rpp, redir);
4891556Srgrimes	default:
4901556Srgrimes		synexpect(-1);
4911556Srgrimes	}
4921556Srgrimes
4931556Srgrimes	/* Now check for redirection which may follow command */
4941556Srgrimes	while (readtoken() == TREDIR) {
4951556Srgrimes		*rpp = n2 = redirnode;
4961556Srgrimes		rpp = &n2->nfile.next;
4971556Srgrimes		parsefname();
4981556Srgrimes	}
4991556Srgrimes	tokpushback++;
5001556Srgrimes	*rpp = NULL;
5011556Srgrimes	if (redir) {
5021556Srgrimes		if (n1->type != NSUBSHELL) {
5031556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5041556Srgrimes			n2->type = NREDIR;
5051556Srgrimes			n2->nredir.n = n1;
5061556Srgrimes			n1 = n2;
5071556Srgrimes		}
5081556Srgrimes		n1->nredir.redirect = redir;
5091556Srgrimes	}
51025230Ssteve	return n1;
5111556Srgrimes}
5121556Srgrimes
5131556Srgrimes
5141556SrgrimesSTATIC union node *
5158855Srgrimessimplecmd(rpp, redir)
5161556Srgrimes	union node **rpp, *redir;
5171556Srgrimes	{
5181556Srgrimes	union node *args, **app;
5191556Srgrimes	union node **orig_rpp = rpp;
52025230Ssteve	union node *n = NULL;
5211556Srgrimes
5221556Srgrimes	/* If we don't have any redirections already, then we must reset */
5231556Srgrimes	/* rpp to be the address of the local redir variable.  */
5241556Srgrimes	if (redir == 0)
5251556Srgrimes		rpp = &redir;
5261556Srgrimes
5271556Srgrimes	args = NULL;
5281556Srgrimes	app = &args;
5298855Srgrimes	/*
5301556Srgrimes	 * We save the incoming value, because we need this for shell
5311556Srgrimes	 * functions.  There can not be a redirect or an argument between
5328855Srgrimes	 * the function name and the open parenthesis.
5331556Srgrimes	 */
5341556Srgrimes	orig_rpp = rpp;
5351556Srgrimes
5361556Srgrimes	for (;;) {
5371556Srgrimes		if (readtoken() == TWORD) {
5381556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
5391556Srgrimes			n->type = NARG;
5401556Srgrimes			n->narg.text = wordtext;
5411556Srgrimes			n->narg.backquote = backquotelist;
5421556Srgrimes			*app = n;
5431556Srgrimes			app = &n->narg.next;
5441556Srgrimes		} else if (lasttoken == TREDIR) {
5451556Srgrimes			*rpp = n = redirnode;
5461556Srgrimes			rpp = &n->nfile.next;
5471556Srgrimes			parsefname();	/* read name of redirection file */
5481556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
5491556Srgrimes					    && rpp == orig_rpp) {
5501556Srgrimes			/* We have a function */
5511556Srgrimes			if (readtoken() != TRP)
5521556Srgrimes				synexpect(TRP);
5531556Srgrimes#ifdef notdef
5541556Srgrimes			if (! goodname(n->narg.text))
5551556Srgrimes				synerror("Bad function name");
5561556Srgrimes#endif
5571556Srgrimes			n->type = NDEFUN;
5581556Srgrimes			n->narg.next = command();
55925230Ssteve			return n;
5601556Srgrimes		} else {
5611556Srgrimes			tokpushback++;
5621556Srgrimes			break;
5631556Srgrimes		}
5641556Srgrimes	}
5651556Srgrimes	*app = NULL;
5661556Srgrimes	*rpp = NULL;
5671556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
5681556Srgrimes	n->type = NCMD;
5691556Srgrimes	n->ncmd.backgnd = 0;
5701556Srgrimes	n->ncmd.args = args;
5711556Srgrimes	n->ncmd.redirect = redir;
57225230Ssteve	return n;
5731556Srgrimes}
5741556Srgrimes
57517987SpeterSTATIC union node *
57617987Spetermakename() {
57717987Speter	union node *n;
5781556Srgrimes
57917987Speter	n = (union node *)stalloc(sizeof (struct narg));
58017987Speter	n->type = NARG;
58117987Speter	n->narg.next = NULL;
58217987Speter	n->narg.text = wordtext;
58317987Speter	n->narg.backquote = backquotelist;
58417987Speter	return n;
58517987Speter}
58617987Speter
58717987Spetervoid fixredir(n, text, err)
58817987Speter	union node *n;
58917987Speter	const char *text;
59017987Speter	int err;
59117987Speter	{
59217987Speter	TRACE(("Fix redir %s %d\n", text, err));
59317987Speter	if (!err)
59417987Speter		n->ndup.vname = NULL;
59517987Speter
59617987Speter	if (is_digit(text[0]) && text[1] == '\0')
59717987Speter		n->ndup.dupfd = digit_val(text[0]);
59817987Speter	else if (text[0] == '-' && text[1] == '\0')
59917987Speter		n->ndup.dupfd = -1;
60017987Speter	else {
60120425Ssteve
60217987Speter		if (err)
60317987Speter			synerror("Bad fd number");
60417987Speter		else
60517987Speter			n->ndup.vname = makename();
60617987Speter	}
60717987Speter}
60817987Speter
60917987Speter
6101556SrgrimesSTATIC void
6111556Srgrimesparsefname() {
6121556Srgrimes	union node *n = redirnode;
6131556Srgrimes
6141556Srgrimes	if (readtoken() != TWORD)
6151556Srgrimes		synexpect(-1);
6161556Srgrimes	if (n->type == NHERE) {
6171556Srgrimes		struct heredoc *here = heredoc;
6181556Srgrimes		struct heredoc *p;
6191556Srgrimes		int i;
6201556Srgrimes
6211556Srgrimes		if (quoteflag == 0)
6221556Srgrimes			n->type = NXHERE;
6231556Srgrimes		TRACE(("Here document %d\n", n->type));
6241556Srgrimes		if (here->striptabs) {
6251556Srgrimes			while (*wordtext == '\t')
6261556Srgrimes				wordtext++;
6271556Srgrimes		}
6281556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
6291556Srgrimes			synerror("Illegal eof marker for << redirection");
6301556Srgrimes		rmescapes(wordtext);
6311556Srgrimes		here->eofmark = wordtext;
6321556Srgrimes		here->next = NULL;
6331556Srgrimes		if (heredoclist == NULL)
6341556Srgrimes			heredoclist = here;
6351556Srgrimes		else {
6361556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
6371556Srgrimes			p->next = here;
6381556Srgrimes		}
6391556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
64017987Speter		fixredir(n, wordtext, 0);
6411556Srgrimes	} else {
64217987Speter		n->nfile.fname = makename();
6431556Srgrimes	}
6441556Srgrimes}
6451556Srgrimes
6461556Srgrimes
6471556Srgrimes/*
6481556Srgrimes * Input any here documents.
6491556Srgrimes */
6501556Srgrimes
6511556SrgrimesSTATIC void
6521556Srgrimesparseheredoc() {
6531556Srgrimes	struct heredoc *here;
6541556Srgrimes	union node *n;
6551556Srgrimes
6561556Srgrimes	while (heredoclist) {
6571556Srgrimes		here = heredoclist;
6581556Srgrimes		heredoclist = here->next;
6591556Srgrimes		if (needprompt) {
6601556Srgrimes			setprompt(2);
6611556Srgrimes			needprompt = 0;
6621556Srgrimes		}
6631556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
6641556Srgrimes				here->eofmark, here->striptabs);
6651556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
6661556Srgrimes		n->narg.type = NARG;
6671556Srgrimes		n->narg.next = NULL;
6681556Srgrimes		n->narg.text = wordtext;
6691556Srgrimes		n->narg.backquote = backquotelist;
6701556Srgrimes		here->here->nhere.doc = n;
6711556Srgrimes	}
6721556Srgrimes}
6731556Srgrimes
6741556SrgrimesSTATIC int
6751556Srgrimespeektoken() {
6761556Srgrimes	int t;
6771556Srgrimes
6781556Srgrimes	t = readtoken();
6791556Srgrimes	tokpushback++;
6801556Srgrimes	return (t);
6811556Srgrimes}
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 */
87154679Scracauer	int synentry;
87217987Speter#if __GNUC__
87317987Speter	/* Avoid longjmp clobbering */
87417987Speter	(void) &out;
87517987Speter	(void) &quotef;
87617987Speter	(void) &dblquote;
87717987Speter	(void) &varnest;
87817987Speter	(void) &arinest;
87917987Speter	(void) &parenlevel;
88017987Speter	(void) &oldstyle;
88117987Speter	(void) &prevsyntax;
88217987Speter	(void) &syntax;
88354679Scracauer	(void) &synentry;
88417987Speter#endif
8851556Srgrimes
8861556Srgrimes	startlinno = plinno;
8871556Srgrimes	dblquote = 0;
8881556Srgrimes	if (syntax == DQSYNTAX)
8891556Srgrimes		dblquote = 1;
8901556Srgrimes	quotef = 0;
8911556Srgrimes	bqlist = NULL;
8921556Srgrimes	varnest = 0;
8931556Srgrimes	arinest = 0;
8941556Srgrimes	parenlevel = 0;
8951556Srgrimes
8961556Srgrimes	STARTSTACKSTR(out);
8971556Srgrimes	loop: {	/* for each line, until end of word */
8981556Srgrimes#if ATTY
8991556Srgrimes		if (c == '\034' && doprompt
9001556Srgrimes		 && attyset() && ! equal(termval(), "emacs")) {
9011556Srgrimes			attyline();
9021556Srgrimes			if (syntax == BASESYNTAX)
9031556Srgrimes				return readtoken();
9041556Srgrimes			c = pgetc();
9051556Srgrimes			goto loop;
9061556Srgrimes		}
9071556Srgrimes#endif
9081556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
9091556Srgrimes		for (;;) {	/* until end of line or end of word */
9101556Srgrimes			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
91154679Scracauer
91264705Scracauer			synentry = syntax[c];
91354679Scracauer
91454679Scracauer			switch(synentry) {
9151556Srgrimes			case CNL:	/* '\n' */
9161556Srgrimes				if (syntax == BASESYNTAX)
9171556Srgrimes					goto endword;	/* exit outer loop */
9181556Srgrimes				USTPUTC(c, out);
9191556Srgrimes				plinno++;
9201556Srgrimes				if (doprompt)
9211556Srgrimes					setprompt(2);
9221556Srgrimes				else
9231556Srgrimes					setprompt(0);
9241556Srgrimes				c = pgetc();
9251556Srgrimes				goto loop;		/* continue outer loop */
9261556Srgrimes			case CWORD:
9271556Srgrimes				USTPUTC(c, out);
9281556Srgrimes				break;
9291556Srgrimes			case CCTL:
9301556Srgrimes				if (eofmark == NULL || dblquote)
9311556Srgrimes					USTPUTC(CTLESC, out);
9321556Srgrimes				USTPUTC(c, out);
9331556Srgrimes				break;
9341556Srgrimes			case CBACK:	/* backslash */
9351556Srgrimes				c = pgetc();
9361556Srgrimes				if (c == PEOF) {
9371556Srgrimes					USTPUTC('\\', out);
9381556Srgrimes					pungetc();
9391556Srgrimes				} else if (c == '\n') {
9401556Srgrimes					if (doprompt)
9411556Srgrimes						setprompt(2);
9421556Srgrimes					else
9431556Srgrimes						setprompt(0);
9441556Srgrimes				} else {
94554631Scracauer					if (dblquote && c != '\\' &&
94654631Scracauer					    c != '`' && c != '$' &&
94754631Scracauer					    (c != '"' || eofmark != NULL))
9481556Srgrimes						USTPUTC('\\', out);
94954631Scracauer					if (c >= 0 && SQSYNTAX[c] == CCTL)
9501556Srgrimes						USTPUTC(CTLESC, out);
95139137Stegge					else if (eofmark == NULL)
95238887Stegge						USTPUTC(CTLQUOTEMARK, out);
9531556Srgrimes					USTPUTC(c, out);
9541556Srgrimes					quotef++;
9551556Srgrimes				}
9561556Srgrimes				break;
9571556Srgrimes			case CSQUOTE:
95839137Stegge				if (eofmark == NULL)
95939137Stegge					USTPUTC(CTLQUOTEMARK, out);
9601556Srgrimes				syntax = SQSYNTAX;
9611556Srgrimes				break;
9621556Srgrimes			case CDQUOTE:
96339137Stegge				if (eofmark == NULL)
96439137Stegge					USTPUTC(CTLQUOTEMARK, out);
9651556Srgrimes				syntax = DQSYNTAX;
9661556Srgrimes				dblquote = 1;
9671556Srgrimes				break;
9681556Srgrimes			case CENDQUOTE:
96939137Stegge				if (eofmark != NULL && arinest == 0 &&
97039137Stegge				    varnest == 0) {
9711556Srgrimes					USTPUTC(c, out);
9721556Srgrimes				} else {
97339137Stegge					if (arinest) {
9741556Srgrimes						syntax = ARISYNTAX;
97539137Stegge						dblquote = 0;
97639137Stegge					} else if (eofmark == NULL) {
9771556Srgrimes						syntax = BASESYNTAX;
97839137Stegge						dblquote = 0;
97939137Stegge					}
9801556Srgrimes					quotef++;
9811556Srgrimes				}
9821556Srgrimes				break;
9831556Srgrimes			case CVAR:	/* '$' */
9841556Srgrimes				PARSESUB();		/* parse substitution */
9851556Srgrimes				break;
9861556Srgrimes			case CENDVAR:	/* '}' */
9871556Srgrimes				if (varnest > 0) {
9881556Srgrimes					varnest--;
9891556Srgrimes					USTPUTC(CTLENDVAR, out);
9901556Srgrimes				} else {
9911556Srgrimes					USTPUTC(c, out);
9921556Srgrimes				}
9931556Srgrimes				break;
9941556Srgrimes			case CLP:	/* '(' in arithmetic */
9951556Srgrimes				parenlevel++;
9961556Srgrimes				USTPUTC(c, out);
9971556Srgrimes				break;
9981556Srgrimes			case CRP:	/* ')' in arithmetic */
9991556Srgrimes				if (parenlevel > 0) {
10001556Srgrimes					USTPUTC(c, out);
10011556Srgrimes					--parenlevel;
10021556Srgrimes				} else {
10031556Srgrimes					if (pgetc() == ')') {
10041556Srgrimes						if (--arinest == 0) {
10051556Srgrimes							USTPUTC(CTLENDARI, out);
10061556Srgrimes							syntax = prevsyntax;
100739137Stegge							if (syntax == DQSYNTAX)
100839137Stegge								dblquote = 1;
100939137Stegge							else
101039137Stegge								dblquote = 0;
10111556Srgrimes						} else
10121556Srgrimes							USTPUTC(')', out);
10131556Srgrimes					} else {
10148855Srgrimes						/*
10151556Srgrimes						 * unbalanced parens
10161556Srgrimes						 *  (don't 2nd guess - no error)
10171556Srgrimes						 */
10181556Srgrimes						pungetc();
10191556Srgrimes						USTPUTC(')', out);
10201556Srgrimes					}
10211556Srgrimes				}
10221556Srgrimes				break;
10231556Srgrimes			case CBQUOTE:	/* '`' */
10241556Srgrimes				PARSEBACKQOLD();
10251556Srgrimes				break;
10261556Srgrimes			case CEOF:
10271556Srgrimes				goto endword;		/* exit outer loop */
10281556Srgrimes			default:
10291556Srgrimes				if (varnest == 0)
10301556Srgrimes					goto endword;	/* exit outer loop */
10311556Srgrimes				USTPUTC(c, out);
10321556Srgrimes			}
10331556Srgrimes			c = pgetc_macro();
10341556Srgrimes		}
10351556Srgrimes	}
10361556Srgrimesendword:
10371556Srgrimes	if (syntax == ARISYNTAX)
10381556Srgrimes		synerror("Missing '))'");
10391556Srgrimes	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
10401556Srgrimes		synerror("Unterminated quoted string");
10411556Srgrimes	if (varnest != 0) {
10421556Srgrimes		startlinno = plinno;
10431556Srgrimes		synerror("Missing '}'");
10441556Srgrimes	}
10451556Srgrimes	USTPUTC('\0', out);
10461556Srgrimes	len = out - stackblock();
10471556Srgrimes	out = stackblock();
10481556Srgrimes	if (eofmark == NULL) {
10491556Srgrimes		if ((c == '>' || c == '<')
10501556Srgrimes		 && quotef == 0
10511556Srgrimes		 && len <= 2
10521556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
10531556Srgrimes			PARSEREDIR();
10541556Srgrimes			return lasttoken = TREDIR;
10551556Srgrimes		} else {
10561556Srgrimes			pungetc();
10571556Srgrimes		}
10581556Srgrimes	}
10591556Srgrimes	quoteflag = quotef;
10601556Srgrimes	backquotelist = bqlist;
10611556Srgrimes	grabstackblock(len);
10621556Srgrimes	wordtext = out;
10631556Srgrimes	return lasttoken = TWORD;
10641556Srgrimes/* end of readtoken routine */
10651556Srgrimes
10661556Srgrimes
10671556Srgrimes
10681556Srgrimes/*
10691556Srgrimes * Check to see whether we are at the end of the here document.  When this
10701556Srgrimes * is called, c is set to the first character of the next input line.  If
10711556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
10721556Srgrimes */
10731556Srgrimes
10741556Srgrimescheckend: {
10751556Srgrimes	if (eofmark) {
10761556Srgrimes		if (striptabs) {
10771556Srgrimes			while (c == '\t')
10781556Srgrimes				c = pgetc();
10791556Srgrimes		}
10801556Srgrimes		if (c == *eofmark) {
10811556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
108225230Ssteve				char *p, *q;
10831556Srgrimes
10841556Srgrimes				p = line;
10851556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
10861556Srgrimes				if (*p == '\n' && *q == '\0') {
10871556Srgrimes					c = PEOF;
10881556Srgrimes					plinno++;
10891556Srgrimes					needprompt = doprompt;
10901556Srgrimes				} else {
10911556Srgrimes					pushstring(line, strlen(line), NULL);
10921556Srgrimes				}
10931556Srgrimes			}
10941556Srgrimes		}
10951556Srgrimes	}
10961556Srgrimes	goto checkend_return;
10971556Srgrimes}
10981556Srgrimes
10991556Srgrimes
11001556Srgrimes/*
11011556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
11021556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
11031556Srgrimes * first character of the redirection operator.
11041556Srgrimes */
11051556Srgrimes
11061556Srgrimesparseredir: {
11071556Srgrimes	char fd = *out;
11081556Srgrimes	union node *np;
11091556Srgrimes
11101556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
11111556Srgrimes	if (c == '>') {
11121556Srgrimes		np->nfile.fd = 1;
11131556Srgrimes		c = pgetc();
11141556Srgrimes		if (c == '>')
11151556Srgrimes			np->type = NAPPEND;
11161556Srgrimes		else if (c == '&')
11171556Srgrimes			np->type = NTOFD;
11181556Srgrimes		else {
11191556Srgrimes			np->type = NTO;
11201556Srgrimes			pungetc();
11211556Srgrimes		}
11221556Srgrimes	} else {	/* c == '<' */
11231556Srgrimes		np->nfile.fd = 0;
11241556Srgrimes		c = pgetc();
11251556Srgrimes		if (c == '<') {
11261556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
11271556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
11281556Srgrimes				np->nfile.fd = 0;
11291556Srgrimes			}
11301556Srgrimes			np->type = NHERE;
11311556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
11321556Srgrimes			heredoc->here = np;
11331556Srgrimes			if ((c = pgetc()) == '-') {
11341556Srgrimes				heredoc->striptabs = 1;
11351556Srgrimes			} else {
11361556Srgrimes				heredoc->striptabs = 0;
11371556Srgrimes				pungetc();
11381556Srgrimes			}
11391556Srgrimes		} else if (c == '&')
11401556Srgrimes			np->type = NFROMFD;
114166612Sbrian		else if (c == '>')
114266612Sbrian			np->type = NFROMTO;
11431556Srgrimes		else {
11441556Srgrimes			np->type = NFROM;
11451556Srgrimes			pungetc();
11461556Srgrimes		}
11471556Srgrimes	}
11481556Srgrimes	if (fd != '\0')
11491556Srgrimes		np->nfile.fd = digit_val(fd);
11501556Srgrimes	redirnode = np;
11511556Srgrimes	goto parseredir_return;
11521556Srgrimes}
11531556Srgrimes
11541556Srgrimes
11551556Srgrimes/*
11561556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
11571556Srgrimes * and nothing else.
11581556Srgrimes */
11591556Srgrimes
11601556Srgrimesparsesub: {
11611556Srgrimes	int subtype;
11621556Srgrimes	int typeloc;
11631556Srgrimes	int flags;
11641556Srgrimes	char *p;
11651556Srgrimes#ifndef GDB_HACK
11661556Srgrimes	static const char types[] = "}-+?=";
11671556Srgrimes#endif
116818202Speter       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
11691556Srgrimes
11701556Srgrimes	c = pgetc();
11711556Srgrimes	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
11721556Srgrimes		USTPUTC('$', out);
11731556Srgrimes		pungetc();
11741556Srgrimes	} else if (c == '(') {	/* $(command) or $((arith)) */
11751556Srgrimes		if (pgetc() == '(') {
11761556Srgrimes			PARSEARITH();
11771556Srgrimes		} else {
11781556Srgrimes			pungetc();
11791556Srgrimes			PARSEBACKQNEW();
11801556Srgrimes		}
11811556Srgrimes	} else {
11821556Srgrimes		USTPUTC(CTLVAR, out);
11831556Srgrimes		typeloc = out - stackblock();
11841556Srgrimes		USTPUTC(VSNORMAL, out);
11851556Srgrimes		subtype = VSNORMAL;
11861556Srgrimes		if (c == '{') {
118718202Speter			bracketed_name = 1;
11881556Srgrimes			c = pgetc();
118917987Speter			if (c == '#') {
119017987Speter				if ((c = pgetc()) == '}')
119117987Speter					c = '#';
119217987Speter				else
119317987Speter					subtype = VSLENGTH;
119417987Speter			}
119517987Speter			else
119617987Speter				subtype = 0;
11971556Srgrimes		}
11981556Srgrimes		if (is_name(c)) {
11991556Srgrimes			do {
12001556Srgrimes				STPUTC(c, out);
12011556Srgrimes				c = pgetc();
12021556Srgrimes			} while (is_in_name(c));
120318202Speter		} else if (is_digit(c)) {
120418202Speter			if (bracketed_name) {
120518202Speter				do {
120618202Speter					STPUTC(c, out);
120718202Speter					c = pgetc();
120818202Speter				} while (is_digit(c));
120918202Speter			} else {
121018202Speter				STPUTC(c, out);
121118202Speter				c = pgetc();
121218202Speter			}
12131556Srgrimes		} else {
12141556Srgrimes			if (! is_special(c))
12151556Srgrimesbadsub:				synerror("Bad substitution");
12161556Srgrimes			USTPUTC(c, out);
12171556Srgrimes			c = pgetc();
12181556Srgrimes		}
12191556Srgrimes		STPUTC('=', out);
12201556Srgrimes		flags = 0;
12211556Srgrimes		if (subtype == 0) {
122217987Speter			switch (c) {
122317987Speter			case ':':
12241556Srgrimes				flags = VSNUL;
12251556Srgrimes				c = pgetc();
122617987Speter				/*FALLTHROUGH*/
122717987Speter			default:
122817987Speter				p = strchr(types, c);
122917987Speter				if (p == NULL)
123017987Speter					goto badsub;
123117987Speter				subtype = p - types + VSNORMAL;
123217987Speter				break;
123317987Speter			case '%':
123420425Ssteve			case '#':
123517987Speter				{
123617987Speter					int cc = c;
123717987Speter					subtype = c == '#' ? VSTRIMLEFT :
123817987Speter							     VSTRIMRIGHT;
123917987Speter					c = pgetc();
124017987Speter					if (c == cc)
124117987Speter						subtype++;
124217987Speter					else
124317987Speter						pungetc();
124417987Speter					break;
124517987Speter				}
12461556Srgrimes			}
12471556Srgrimes		} else {
12481556Srgrimes			pungetc();
12491556Srgrimes		}
125057225Scracauer		if (subtype != VSLENGTH && (dblquote || arinest))
12511556Srgrimes			flags |= VSQUOTE;
12521556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
12531556Srgrimes		if (subtype != VSNORMAL)
12541556Srgrimes			varnest++;
12551556Srgrimes	}
12561556Srgrimes	goto parsesub_return;
12571556Srgrimes}
12581556Srgrimes
12591556Srgrimes
12601556Srgrimes/*
12611556Srgrimes * Called to parse command substitutions.  Newstyle is set if the command
12621556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12631556Srgrimes * list of commands (passed by reference), and savelen is the number of
12641556Srgrimes * characters on the top of the stack which must be preserved.
12651556Srgrimes */
12661556Srgrimes
12671556Srgrimesparsebackq: {
12681556Srgrimes	struct nodelist **nlpp;
12691556Srgrimes	int savepbq;
12701556Srgrimes	union node *n;
12711556Srgrimes	char *volatile str;
12721556Srgrimes	struct jmploc jmploc;
12731556Srgrimes	struct jmploc *volatile savehandler;
12741556Srgrimes	int savelen;
127520425Ssteve	int saveprompt;
127620425Ssteve#if __GNUC__
127720425Ssteve	/* Avoid longjmp clobbering */
127820425Ssteve	(void) &saveprompt;
127920425Ssteve#endif
12801556Srgrimes
12811556Srgrimes	savepbq = parsebackquote;
12821556Srgrimes	if (setjmp(jmploc.loc)) {
12831556Srgrimes		if (str)
12841556Srgrimes			ckfree(str);
12851556Srgrimes		parsebackquote = 0;
12861556Srgrimes		handler = savehandler;
12871556Srgrimes		longjmp(handler->loc, 1);
12881556Srgrimes	}
12891556Srgrimes	INTOFF;
12901556Srgrimes	str = NULL;
12911556Srgrimes	savelen = out - stackblock();
12921556Srgrimes	if (savelen > 0) {
12931556Srgrimes		str = ckmalloc(savelen);
129417987Speter		memcpy(str, stackblock(), savelen);
12951556Srgrimes	}
12961556Srgrimes	savehandler = handler;
12971556Srgrimes	handler = &jmploc;
12981556Srgrimes	INTON;
12991556Srgrimes        if (oldstyle) {
13001556Srgrimes                /* We must read until the closing backquote, giving special
13011556Srgrimes                   treatment to some slashes, and then push the string and
13021556Srgrimes                   reread it as input, interpreting it normally.  */
130325230Ssteve                char *out;
130425230Ssteve                int c;
13051556Srgrimes                int savelen;
13061556Srgrimes                char *str;
13078855Srgrimes
130820425Ssteve
13091556Srgrimes                STARTSTACKSTR(out);
131020425Ssteve		for (;;) {
131120425Ssteve			if (needprompt) {
131220425Ssteve				setprompt(2);
131320425Ssteve				needprompt = 0;
131420425Ssteve			}
131520425Ssteve			switch (c = pgetc()) {
131620425Ssteve			case '`':
131720425Ssteve				goto done;
131820425Ssteve
131920425Ssteve			case '\\':
132020425Ssteve                                if ((c = pgetc()) == '\n') {
132120425Ssteve					plinno++;
132220425Ssteve					if (doprompt)
132320425Ssteve						setprompt(2);
132420425Ssteve					else
132520425Ssteve						setprompt(0);
132620425Ssteve					/*
132720425Ssteve					 * If eating a newline, avoid putting
132820425Ssteve					 * the newline into the new character
132920425Ssteve					 * stream (via the STPUTC after the
133020425Ssteve					 * switch).
133120425Ssteve					 */
133220425Ssteve					continue;
133320425Ssteve				}
133417987Speter                                if (c != '\\' && c != '`' && c != '$'
13351556Srgrimes                                    && (!dblquote || c != '"'))
13361556Srgrimes                                        STPUTC('\\', out);
133720425Ssteve				break;
133820425Ssteve
133920425Ssteve			case '\n':
134020425Ssteve				plinno++;
134120425Ssteve				needprompt = doprompt;
134220425Ssteve				break;
134320425Ssteve
134420425Ssteve			case PEOF:
134520425Ssteve			        startlinno = plinno;
134620425Ssteve				synerror("EOF in backquote substitution");
134720425Ssteve 				break;
134820425Ssteve
134920425Ssteve			default:
135020425Ssteve				break;
135120425Ssteve			}
135220425Ssteve			STPUTC(c, out);
13531556Srgrimes                }
135420425Sstevedone:
13551556Srgrimes                STPUTC('\0', out);
13561556Srgrimes                savelen = out - stackblock();
13571556Srgrimes                if (savelen > 0) {
13581556Srgrimes                        str = ckmalloc(savelen);
135917987Speter                        memcpy(str, stackblock(), savelen);
136017987Speter			setinputstring(str, 1);
13611556Srgrimes                }
13621556Srgrimes        }
13631556Srgrimes	nlpp = &bqlist;
13641556Srgrimes	while (*nlpp)
13651556Srgrimes		nlpp = &(*nlpp)->next;
13661556Srgrimes	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
13671556Srgrimes	(*nlpp)->next = NULL;
13681556Srgrimes	parsebackquote = oldstyle;
136920425Ssteve
137020425Ssteve	if (oldstyle) {
137120425Ssteve		saveprompt = doprompt;
137220425Ssteve		doprompt = 0;
137320425Ssteve	}
137420425Ssteve
13751556Srgrimes	n = list(0);
137620425Ssteve
137720425Ssteve	if (oldstyle)
137820425Ssteve		doprompt = saveprompt;
137920425Ssteve	else {
138020425Ssteve		if (readtoken() != TRP)
138120425Ssteve			synexpect(TRP);
138220425Ssteve	}
138320425Ssteve
13841556Srgrimes	(*nlpp)->n = n;
138520425Ssteve        if (oldstyle) {
138620425Ssteve		/*
138720425Ssteve		 * Start reading from old file again, ignoring any pushed back
138820425Ssteve		 * tokens left from the backquote parsing
138920425Ssteve		 */
13901556Srgrimes                popfile();
139120425Ssteve		tokpushback = 0;
139220425Ssteve	}
13931556Srgrimes	while (stackblocksize() <= savelen)
13941556Srgrimes		growstackblock();
13951556Srgrimes	STARTSTACKSTR(out);
13961556Srgrimes	if (str) {
139717987Speter		memcpy(out, str, savelen);
13981556Srgrimes		STADJUST(savelen, out);
13991556Srgrimes		INTOFF;
14001556Srgrimes		ckfree(str);
14011556Srgrimes		str = NULL;
14021556Srgrimes		INTON;
14031556Srgrimes	}
14041556Srgrimes	parsebackquote = savepbq;
14051556Srgrimes	handler = savehandler;
14061556Srgrimes	if (arinest || dblquote)
14071556Srgrimes		USTPUTC(CTLBACKQ | CTLQUOTE, out);
14081556Srgrimes	else
14091556Srgrimes		USTPUTC(CTLBACKQ, out);
14101556Srgrimes	if (oldstyle)
14111556Srgrimes		goto parsebackq_oldreturn;
14121556Srgrimes	else
14131556Srgrimes		goto parsebackq_newreturn;
14141556Srgrimes}
14151556Srgrimes
14161556Srgrimes/*
14171556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
14181556Srgrimes */
14191556Srgrimesparsearith: {
14201556Srgrimes
14211556Srgrimes	if (++arinest == 1) {
14221556Srgrimes		prevsyntax = syntax;
14231556Srgrimes		syntax = ARISYNTAX;
14241556Srgrimes		USTPUTC(CTLARI, out);
142538887Stegge		if (dblquote)
142638887Stegge			USTPUTC('"',out);
142738887Stegge		else
142838887Stegge			USTPUTC(' ',out);
14291556Srgrimes	} else {
14301556Srgrimes		/*
14311556Srgrimes		 * we collapse embedded arithmetic expansion to
14321556Srgrimes		 * parenthesis, which should be equivalent
14331556Srgrimes		 */
14341556Srgrimes		USTPUTC('(', out);
14351556Srgrimes	}
14361556Srgrimes	goto parsearith_return;
14371556Srgrimes}
14381556Srgrimes
14391556Srgrimes} /* end of readtoken */
14401556Srgrimes
14411556Srgrimes
14421556Srgrimes
14431556Srgrimes#ifdef mkinit
14441556SrgrimesRESET {
14451556Srgrimes	tokpushback = 0;
14461556Srgrimes	checkkwd = 0;
14471556Srgrimes}
14481556Srgrimes#endif
14491556Srgrimes
14501556Srgrimes/*
14511556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
14521556Srgrimes * or backquotes).
14531556Srgrimes */
14541556Srgrimes
14551556SrgrimesSTATIC int
14561556Srgrimesnoexpand(text)
14571556Srgrimes	char *text;
14581556Srgrimes	{
145925230Ssteve	char *p;
146025230Ssteve	char c;
14611556Srgrimes
14621556Srgrimes	p = text;
14631556Srgrimes	while ((c = *p++) != '\0') {
146439137Stegge		if ( c == CTLQUOTEMARK)
146539137Stegge			continue;
14661556Srgrimes		if (c == CTLESC)
14671556Srgrimes			p++;
146854631Scracauer		else if (c >= 0 && BASESYNTAX[(int)c] == CCTL)
14691556Srgrimes			return 0;
14701556Srgrimes	}
14711556Srgrimes	return 1;
14721556Srgrimes}
14731556Srgrimes
14741556Srgrimes
14751556Srgrimes/*
14761556Srgrimes * Return true if the argument is a legal variable name (a letter or
14771556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
14781556Srgrimes */
14791556Srgrimes
14801556Srgrimesint
14811556Srgrimesgoodname(name)
14821556Srgrimes	char *name;
14831556Srgrimes	{
148425230Ssteve	char *p;
14851556Srgrimes
14861556Srgrimes	p = name;
14871556Srgrimes	if (! is_name(*p))
14881556Srgrimes		return 0;
14891556Srgrimes	while (*++p) {
14901556Srgrimes		if (! is_in_name(*p))
14911556Srgrimes			return 0;
14921556Srgrimes	}
14931556Srgrimes	return 1;
14941556Srgrimes}
14951556Srgrimes
14961556Srgrimes
14971556Srgrimes/*
14981556Srgrimes * Called when an unexpected token is read during the parse.  The argument
14991556Srgrimes * is the token that is expected, or -1 if more than one type of token can
15001556Srgrimes * occur at this point.
15011556Srgrimes */
15021556Srgrimes
15031556SrgrimesSTATIC void
150420425Sstevesynexpect(token)
150517987Speter	int token;
150617987Speter{
15071556Srgrimes	char msg[64];
15081556Srgrimes
15091556Srgrimes	if (token >= 0) {
15101556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
15111556Srgrimes			tokname[lasttoken], tokname[token]);
15121556Srgrimes	} else {
15131556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
15141556Srgrimes	}
15151556Srgrimes	synerror(msg);
15161556Srgrimes}
15171556Srgrimes
15181556Srgrimes
15191556SrgrimesSTATIC void
15201556Srgrimessynerror(msg)
15211556Srgrimes	char *msg;
15221556Srgrimes	{
15231556Srgrimes	if (commandname)
15241556Srgrimes		outfmt(&errout, "%s: %d: ", commandname, startlinno);
15251556Srgrimes	outfmt(&errout, "Syntax error: %s\n", msg);
15261556Srgrimes	error((char *)NULL);
15271556Srgrimes}
15281556Srgrimes
15291556SrgrimesSTATIC void
15301556Srgrimessetprompt(which)
15311556Srgrimes	int which;
15321556Srgrimes	{
15331556Srgrimes	whichprompt = which;
15341556Srgrimes
153517987Speter#ifndef NO_HISTORY
15361556Srgrimes	if (!el)
153717987Speter#endif
15381556Srgrimes		out2str(getprompt(NULL));
15391556Srgrimes}
15401556Srgrimes
15411556Srgrimes/*
15421556Srgrimes * called by editline -- any expansions to the prompt
15431556Srgrimes *    should be added here.
15441556Srgrimes */
15451556Srgrimeschar *
15461556Srgrimesgetprompt(unused)
154725905Ssteve	void *unused __unused;
154825905Ssteve{
15491556Srgrimes	switch (whichprompt) {
15501556Srgrimes	case 0:
15511556Srgrimes		return "";
15521556Srgrimes	case 1:
15531556Srgrimes		return ps1val();
15541556Srgrimes	case 2:
15551556Srgrimes		return ps2val();
15561556Srgrimes	default:
15571556Srgrimes		return "<internal prompt error>";
15581556Srgrimes	}
15591556Srgrimes}
1560