parser.c revision 214291
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/parser.c 214291 2010-10-24 20:45:13Z jilles $");
401556Srgrimes
4117987Speter#include <stdlib.h>
42149017Sstefanf#include <unistd.h>
43209337Sjilles#include <stdio.h>
4417987Speter
451556Srgrimes#include "shell.h"
461556Srgrimes#include "parser.h"
471556Srgrimes#include "nodes.h"
481556Srgrimes#include "expand.h"	/* defines rmescapes() */
491556Srgrimes#include "syntax.h"
501556Srgrimes#include "options.h"
511556Srgrimes#include "input.h"
521556Srgrimes#include "output.h"
531556Srgrimes#include "var.h"
541556Srgrimes#include "error.h"
551556Srgrimes#include "memalloc.h"
561556Srgrimes#include "mystring.h"
571556Srgrimes#include "alias.h"
5817987Speter#include "show.h"
5959436Scracauer#include "eval.h"
6017987Speter#ifndef NO_HISTORY
611556Srgrimes#include "myhistedit.h"
6217987Speter#endif
631556Srgrimes
641556Srgrimes/*
651556Srgrimes * Shell command parser.
661556Srgrimes */
671556Srgrimes
68142845Sobrien#define	EOFMARKLEN	79
69142845Sobrien#define	PROMPTLEN	128
701556Srgrimes
711556Srgrimes/* values returned by readtoken */
7217987Speter#include "token.h"
731556Srgrimes
741556Srgrimes
751556Srgrimes
761556Srgrimesstruct heredoc {
771556Srgrimes	struct heredoc *next;	/* next here document in list */
781556Srgrimes	union node *here;		/* redirection node */
791556Srgrimes	char *eofmark;		/* string indicating end of input */
801556Srgrimes	int striptabs;		/* if set, strip leading tabs */
811556Srgrimes};
821556Srgrimes
83206145Sjillesstruct parser_temp {
84206145Sjilles	struct parser_temp *next;
85206145Sjilles	void *data;
86206145Sjilles};
871556Srgrimes
881556Srgrimes
89213760Sobrienstatic struct heredoc *heredoclist;	/* list of here documents to read */
90213760Sobrienstatic int doprompt;		/* if set, prompt the user */
91213760Sobrienstatic int needprompt;		/* true if interactive and at start of line */
92213760Sobrienstatic int lasttoken;		/* last token read */
931556SrgrimesMKINIT int tokpushback;		/* last token pushed back */
94213760Sobrienstatic char *wordtext;		/* text of last word returned by readtoken */
951556SrgrimesMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
96213760Sobrienstatic struct nodelist *backquotelist;
97213760Sobrienstatic union node *redirnode;
98213760Sobrienstatic struct heredoc *heredoc;
99213760Sobrienstatic int quoteflag;		/* set if (part of) last token was quoted */
100213760Sobrienstatic int startlinno;		/* line # where last token started */
101213760Sobrienstatic int funclinno;		/* line # where the current function started */
102213760Sobrienstatic struct parser_temp *parser_temp;
1031556Srgrimes
10418018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
10518018Speterstatic int noaliases = 0;
1061556Srgrimes
1071556Srgrimes
108213811Sobrienstatic union node *list(int);
109213811Sobrienstatic union node *andor(void);
110213811Sobrienstatic union node *pipeline(void);
111213811Sobrienstatic union node *command(void);
112213811Sobrienstatic union node *simplecmd(union node **, union node *);
113213811Sobrienstatic union node *makename(void);
114213811Sobrienstatic void parsefname(void);
115213811Sobrienstatic void parseheredoc(void);
116213811Sobrienstatic int peektoken(void);
117213811Sobrienstatic int readtoken(void);
118213811Sobrienstatic int xxreadtoken(void);
119213811Sobrienstatic int readtoken1(int, char const *, char *, int);
120213811Sobrienstatic int noexpand(char *);
121213811Sobrienstatic void synexpect(int) __dead2;
122213811Sobrienstatic void synerror(const char *) __dead2;
123213811Sobrienstatic void setprompt(int);
1241556Srgrimes
12517987Speter
126213811Sobrienstatic void *
127206145Sjillesparser_temp_alloc(size_t len)
128206145Sjilles{
129206145Sjilles	struct parser_temp *t;
130206145Sjilles
131206145Sjilles	INTOFF;
132206145Sjilles	t = ckmalloc(sizeof(*t));
133206145Sjilles	t->data = NULL;
134206145Sjilles	t->next = parser_temp;
135206145Sjilles	parser_temp = t;
136206145Sjilles	t->data = ckmalloc(len);
137206145Sjilles	INTON;
138206145Sjilles	return t->data;
139206145Sjilles}
140206145Sjilles
141206145Sjilles
142213811Sobrienstatic void *
143206145Sjillesparser_temp_realloc(void *ptr, size_t len)
144206145Sjilles{
145206145Sjilles	struct parser_temp *t;
146206145Sjilles
147206145Sjilles	INTOFF;
148206145Sjilles	t = parser_temp;
149206145Sjilles	if (ptr != t->data)
150206145Sjilles		error("bug: parser_temp_realloc misused");
151206145Sjilles	t->data = ckrealloc(t->data, len);
152206145Sjilles	INTON;
153206145Sjilles	return t->data;
154206145Sjilles}
155206145Sjilles
156206145Sjilles
157213811Sobrienstatic void
158206145Sjillesparser_temp_free_upto(void *ptr)
159206145Sjilles{
160206145Sjilles	struct parser_temp *t;
161206145Sjilles	int done = 0;
162206145Sjilles
163206145Sjilles	INTOFF;
164206145Sjilles	while (parser_temp != NULL && !done) {
165206145Sjilles		t = parser_temp;
166206145Sjilles		parser_temp = t->next;
167206145Sjilles		done = t->data == ptr;
168206145Sjilles		ckfree(t->data);
169206145Sjilles		ckfree(t);
170206145Sjilles	}
171206145Sjilles	INTON;
172206145Sjilles	if (!done)
173206145Sjilles		error("bug: parser_temp_free_upto misused");
174206145Sjilles}
175206145Sjilles
176206145Sjilles
177213811Sobrienstatic void
178206145Sjillesparser_temp_free_all(void)
179206145Sjilles{
180206145Sjilles	struct parser_temp *t;
181206145Sjilles
182206145Sjilles	INTOFF;
183206145Sjilles	while (parser_temp != NULL) {
184206145Sjilles		t = parser_temp;
185206145Sjilles		parser_temp = t->next;
186206145Sjilles		ckfree(t->data);
187206145Sjilles		ckfree(t);
188206145Sjilles	}
189206145Sjilles	INTON;
190206145Sjilles}
191206145Sjilles
192206145Sjilles
1931556Srgrimes/*
1941556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
1951556Srgrimes * valid parse tree indicating a blank line.)
1961556Srgrimes */
1971556Srgrimes
1981556Srgrimesunion node *
19990111Simpparsecmd(int interact)
20017987Speter{
2011556Srgrimes	int t;
2021556Srgrimes
203206145Sjilles	/* This assumes the parser is not re-entered,
204206145Sjilles	 * which could happen if we add command substitution on PS1/PS2.
205206145Sjilles	 */
206206145Sjilles	parser_temp_free_all();
207208656Sjilles	heredoclist = NULL;
208206145Sjilles
20960593Scracauer	tokpushback = 0;
2101556Srgrimes	doprompt = interact;
2111556Srgrimes	if (doprompt)
2121556Srgrimes		setprompt(1);
2131556Srgrimes	else
2141556Srgrimes		setprompt(0);
2151556Srgrimes	needprompt = 0;
2161556Srgrimes	t = readtoken();
2171556Srgrimes	if (t == TEOF)
2181556Srgrimes		return NEOF;
2191556Srgrimes	if (t == TNL)
2201556Srgrimes		return NULL;
2211556Srgrimes	tokpushback++;
2221556Srgrimes	return list(1);
2231556Srgrimes}
2241556Srgrimes
2251556Srgrimes
226213811Sobrienstatic union node *
22790111Simplist(int nlflag)
22817987Speter{
2291556Srgrimes	union node *n1, *n2, *n3;
23017987Speter	int tok;
2311556Srgrimes
2321556Srgrimes	checkkwd = 2;
2331556Srgrimes	if (nlflag == 0 && tokendlist[peektoken()])
2341556Srgrimes		return NULL;
23517987Speter	n1 = NULL;
2361556Srgrimes	for (;;) {
23717987Speter		n2 = andor();
23817987Speter		tok = readtoken();
23917987Speter		if (tok == TBACKGND) {
24017987Speter			if (n2->type == NCMD || n2->type == NPIPE) {
24117987Speter				n2->ncmd.backgnd = 1;
24217987Speter			} else if (n2->type == NREDIR) {
24317987Speter				n2->type = NBACKGND;
24417987Speter			} else {
24517987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
24617987Speter				n3->type = NBACKGND;
24717987Speter				n3->nredir.n = n2;
24817987Speter				n3->nredir.redirect = NULL;
24917987Speter				n2 = n3;
25017987Speter			}
25117987Speter		}
25217987Speter		if (n1 == NULL) {
25317987Speter			n1 = n2;
25417987Speter		}
25517987Speter		else {
25617987Speter			n3 = (union node *)stalloc(sizeof (struct nbinary));
25717987Speter			n3->type = NSEMI;
25817987Speter			n3->nbinary.ch1 = n1;
25917987Speter			n3->nbinary.ch2 = n2;
26017987Speter			n1 = n3;
26117987Speter		}
26217987Speter		switch (tok) {
26313882Sjoerg		case TBACKGND:
26417987Speter		case TSEMI:
26517987Speter			tok = readtoken();
266102410Scharnier			/* FALLTHROUGH */
2671556Srgrimes		case TNL:
26817987Speter			if (tok == TNL) {
26917987Speter				parseheredoc();
27017987Speter				if (nlflag)
27117987Speter					return n1;
272210488Sjilles			} else if (tok == TEOF && nlflag) {
273210488Sjilles				parseheredoc();
274210488Sjilles				return n1;
27517987Speter			} else {
27617987Speter				tokpushback++;
27717987Speter			}
2781556Srgrimes			checkkwd = 2;
2791556Srgrimes			if (tokendlist[peektoken()])
2801556Srgrimes				return n1;
2811556Srgrimes			break;
2821556Srgrimes		case TEOF:
2831556Srgrimes			if (heredoclist)
2841556Srgrimes				parseheredoc();
2851556Srgrimes			else
2861556Srgrimes				pungetc();		/* push back EOF on input */
2871556Srgrimes			return n1;
2881556Srgrimes		default:
2891556Srgrimes			if (nlflag)
2901556Srgrimes				synexpect(-1);
2911556Srgrimes			tokpushback++;
2921556Srgrimes			return n1;
2931556Srgrimes		}
2941556Srgrimes	}
2951556Srgrimes}
2961556Srgrimes
2971556Srgrimes
2981556Srgrimes
299213811Sobrienstatic union node *
30090111Simpandor(void)
30190111Simp{
3021556Srgrimes	union node *n1, *n2, *n3;
3031556Srgrimes	int t;
3041556Srgrimes
3051556Srgrimes	n1 = pipeline();
3061556Srgrimes	for (;;) {
3071556Srgrimes		if ((t = readtoken()) == TAND) {
3081556Srgrimes			t = NAND;
3091556Srgrimes		} else if (t == TOR) {
3101556Srgrimes			t = NOR;
3111556Srgrimes		} else {
3121556Srgrimes			tokpushback++;
3131556Srgrimes			return n1;
3141556Srgrimes		}
3151556Srgrimes		n2 = pipeline();
3161556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
3171556Srgrimes		n3->type = t;
3181556Srgrimes		n3->nbinary.ch1 = n1;
3191556Srgrimes		n3->nbinary.ch2 = n2;
3201556Srgrimes		n1 = n3;
3211556Srgrimes	}
3221556Srgrimes}
3231556Srgrimes
3241556Srgrimes
3251556Srgrimes
326213811Sobrienstatic union node *
32790111Simppipeline(void)
32890111Simp{
32975336Sbrian	union node *n1, *n2, *pipenode;
3301556Srgrimes	struct nodelist *lp, *prev;
331214281Sjilles	int negate, t;
3321556Srgrimes
33375336Sbrian	negate = 0;
334191009Sstefanf	checkkwd = 2;
3351556Srgrimes	TRACE(("pipeline: entered\n"));
33675336Sbrian	while (readtoken() == TNOT)
33775336Sbrian		negate = !negate;
33875336Sbrian	tokpushback++;
3391556Srgrimes	n1 = command();
3401556Srgrimes	if (readtoken() == TPIPE) {
3411556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3421556Srgrimes		pipenode->type = NPIPE;
3431556Srgrimes		pipenode->npipe.backgnd = 0;
3441556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3451556Srgrimes		pipenode->npipe.cmdlist = lp;
3461556Srgrimes		lp->n = n1;
3471556Srgrimes		do {
3481556Srgrimes			prev = lp;
3491556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
350214281Sjilles			checkkwd = 2;
351214281Sjilles			t = readtoken();
352214281Sjilles			tokpushback++;
353214281Sjilles			if (t == TNOT)
354214281Sjilles				lp->n = pipeline();
355214281Sjilles			else
356214281Sjilles				lp->n = command();
3571556Srgrimes			prev->next = lp;
3581556Srgrimes		} while (readtoken() == TPIPE);
3591556Srgrimes		lp->next = NULL;
3601556Srgrimes		n1 = pipenode;
3611556Srgrimes	}
3621556Srgrimes	tokpushback++;
36375336Sbrian	if (negate) {
36475336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
36575336Sbrian		n2->type = NNOT;
36675336Sbrian		n2->nnot.com = n1;
36775336Sbrian		return n2;
36875336Sbrian	} else
36975336Sbrian		return n1;
3701556Srgrimes}
3711556Srgrimes
3721556Srgrimes
3731556Srgrimes
374213811Sobrienstatic union node *
37590111Simpcommand(void)
37690111Simp{
3771556Srgrimes	union node *n1, *n2;
3781556Srgrimes	union node *ap, **app;
3791556Srgrimes	union node *cp, **cpp;
3801556Srgrimes	union node *redir, **rpp;
381214281Sjilles	int t;
3821556Srgrimes
3831556Srgrimes	checkkwd = 2;
38417987Speter	redir = NULL;
38517987Speter	n1 = NULL;
3861556Srgrimes	rpp = &redir;
38720425Ssteve
3881556Srgrimes	/* Check for redirection which may precede command */
3891556Srgrimes	while (readtoken() == TREDIR) {
3901556Srgrimes		*rpp = n2 = redirnode;
3911556Srgrimes		rpp = &n2->nfile.next;
3921556Srgrimes		parsefname();
3931556Srgrimes	}
3941556Srgrimes	tokpushback++;
3951556Srgrimes
3961556Srgrimes	switch (readtoken()) {
3971556Srgrimes	case TIF:
3981556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3991556Srgrimes		n1->type = NIF;
400104554Stjr		if ((n1->nif.test = list(0)) == NULL)
401104554Stjr			synexpect(-1);
4021556Srgrimes		if (readtoken() != TTHEN)
4031556Srgrimes			synexpect(TTHEN);
4041556Srgrimes		n1->nif.ifpart = list(0);
4051556Srgrimes		n2 = n1;
4061556Srgrimes		while (readtoken() == TELIF) {
4071556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4081556Srgrimes			n2 = n2->nif.elsepart;
4091556Srgrimes			n2->type = NIF;
410104554Stjr			if ((n2->nif.test = list(0)) == NULL)
411104554Stjr				synexpect(-1);
4121556Srgrimes			if (readtoken() != TTHEN)
4131556Srgrimes				synexpect(TTHEN);
4141556Srgrimes			n2->nif.ifpart = list(0);
4151556Srgrimes		}
4161556Srgrimes		if (lasttoken == TELSE)
4171556Srgrimes			n2->nif.elsepart = list(0);
4181556Srgrimes		else {
4191556Srgrimes			n2->nif.elsepart = NULL;
4201556Srgrimes			tokpushback++;
4211556Srgrimes		}
4221556Srgrimes		if (readtoken() != TFI)
4231556Srgrimes			synexpect(TFI);
4241556Srgrimes		checkkwd = 1;
4251556Srgrimes		break;
4261556Srgrimes	case TWHILE:
4271556Srgrimes	case TUNTIL: {
4281556Srgrimes		int got;
4291556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
4301556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
431104554Stjr		if ((n1->nbinary.ch1 = list(0)) == NULL)
432104554Stjr			synexpect(-1);
4331556Srgrimes		if ((got=readtoken()) != TDO) {
4341556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
4351556Srgrimes			synexpect(TDO);
4361556Srgrimes		}
4371556Srgrimes		n1->nbinary.ch2 = list(0);
4381556Srgrimes		if (readtoken() != TDONE)
4391556Srgrimes			synexpect(TDONE);
4401556Srgrimes		checkkwd = 1;
4411556Srgrimes		break;
4421556Srgrimes	}
4431556Srgrimes	case TFOR:
4441556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4451556Srgrimes			synerror("Bad for loop variable");
4461556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4471556Srgrimes		n1->type = NFOR;
4481556Srgrimes		n1->nfor.var = wordtext;
449199282Sjilles		while (readtoken() == TNL)
450199282Sjilles			;
451199282Sjilles		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4521556Srgrimes			app = &ap;
4531556Srgrimes			while (readtoken() == TWORD) {
4541556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
4551556Srgrimes				n2->type = NARG;
4561556Srgrimes				n2->narg.text = wordtext;
4571556Srgrimes				n2->narg.backquote = backquotelist;
4581556Srgrimes				*app = n2;
4591556Srgrimes				app = &n2->narg.next;
4601556Srgrimes			}
4611556Srgrimes			*app = NULL;
4621556Srgrimes			n1->nfor.args = ap;
4631556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4641556Srgrimes				synexpect(-1);
4651556Srgrimes		} else {
466149096Sstefanf			static char argvars[5] = {
467149096Sstefanf				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
468149096Sstefanf			};
4691556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4701556Srgrimes			n2->type = NARG;
471149096Sstefanf			n2->narg.text = argvars;
4721556Srgrimes			n2->narg.backquote = NULL;
4731556Srgrimes			n2->narg.next = NULL;
4741556Srgrimes			n1->nfor.args = n2;
4751556Srgrimes			/*
4761556Srgrimes			 * Newline or semicolon here is optional (but note
4771556Srgrimes			 * that the original Bourne shell only allowed NL).
4781556Srgrimes			 */
4791556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4801556Srgrimes				tokpushback++;
4811556Srgrimes		}
4821556Srgrimes		checkkwd = 2;
4831556Srgrimes		if ((t = readtoken()) == TDO)
4841556Srgrimes			t = TDONE;
4851556Srgrimes		else if (t == TBEGIN)
4861556Srgrimes			t = TEND;
4871556Srgrimes		else
4881556Srgrimes			synexpect(-1);
4891556Srgrimes		n1->nfor.body = list(0);
4901556Srgrimes		if (readtoken() != t)
4911556Srgrimes			synexpect(t);
4921556Srgrimes		checkkwd = 1;
4931556Srgrimes		break;
4941556Srgrimes	case TCASE:
4951556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4961556Srgrimes		n1->type = NCASE;
4971556Srgrimes		if (readtoken() != TWORD)
4981556Srgrimes			synexpect(TWORD);
4991556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
5001556Srgrimes		n2->type = NARG;
5011556Srgrimes		n2->narg.text = wordtext;
5021556Srgrimes		n2->narg.backquote = backquotelist;
5031556Srgrimes		n2->narg.next = NULL;
5041556Srgrimes		while (readtoken() == TNL);
5051556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
5061556Srgrimes			synerror("expecting \"in\"");
5071556Srgrimes		cpp = &n1->ncase.cases;
50818018Speter		noaliases = 1;	/* turn off alias expansion */
5092760Ssef		checkkwd = 2, readtoken();
510104202Stjr		while (lasttoken != TESAC) {
5111556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5121556Srgrimes			cp->type = NCLIST;
5131556Srgrimes			app = &cp->nclist.pattern;
514104207Stjr			if (lasttoken == TLP)
515104207Stjr				readtoken();
5161556Srgrimes			for (;;) {
5171556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
5181556Srgrimes				ap->type = NARG;
5191556Srgrimes				ap->narg.text = wordtext;
5201556Srgrimes				ap->narg.backquote = backquotelist;
5212760Ssef				if (checkkwd = 2, readtoken() != TPIPE)
5221556Srgrimes					break;
5231556Srgrimes				app = &ap->narg.next;
5242760Ssef				readtoken();
5251556Srgrimes			}
5261556Srgrimes			ap->narg.next = NULL;
5271556Srgrimes			if (lasttoken != TRP)
52818018Speter				noaliases = 0, synexpect(TRP);
5291556Srgrimes			cp->nclist.body = list(0);
5302760Ssef
5312760Ssef			checkkwd = 2;
5322760Ssef			if ((t = readtoken()) != TESAC) {
5332760Ssef				if (t != TENDCASE)
53418018Speter					noaliases = 0, synexpect(TENDCASE);
5352760Ssef				else
5362760Ssef					checkkwd = 2, readtoken();
5372760Ssef			}
5381556Srgrimes			cpp = &cp->nclist.next;
539104202Stjr		}
54018018Speter		noaliases = 0;	/* reset alias expansion */
5411556Srgrimes		*cpp = NULL;
5421556Srgrimes		checkkwd = 1;
5431556Srgrimes		break;
5441556Srgrimes	case TLP:
5451556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5461556Srgrimes		n1->type = NSUBSHELL;
5471556Srgrimes		n1->nredir.n = list(0);
5481556Srgrimes		n1->nredir.redirect = NULL;
5491556Srgrimes		if (readtoken() != TRP)
5501556Srgrimes			synexpect(TRP);
5511556Srgrimes		checkkwd = 1;
5521556Srgrimes		break;
5531556Srgrimes	case TBEGIN:
5541556Srgrimes		n1 = list(0);
5551556Srgrimes		if (readtoken() != TEND)
5561556Srgrimes			synexpect(TEND);
5571556Srgrimes		checkkwd = 1;
5581556Srgrimes		break;
5591556Srgrimes	/* Handle an empty command like other simple commands.  */
560210221Sjilles	case TBACKGND:
56117987Speter	case TSEMI:
562101662Stjr	case TAND:
563101662Stjr	case TOR:
56417987Speter		/*
56517987Speter		 * An empty command before a ; doesn't make much sense, and
56617987Speter		 * should certainly be disallowed in the case of `if ;'.
56717987Speter		 */
56817987Speter		if (!redir)
56917987Speter			synexpect(-1);
5701556Srgrimes	case TNL:
57110399Sjoerg	case TEOF:
5721556Srgrimes	case TWORD:
57317987Speter	case TRP:
5741556Srgrimes		tokpushback++;
57575160Sbrian		n1 = simplecmd(rpp, redir);
576214281Sjilles		return n1;
5771556Srgrimes	default:
5781556Srgrimes		synexpect(-1);
5791556Srgrimes	}
5801556Srgrimes
5811556Srgrimes	/* Now check for redirection which may follow command */
5821556Srgrimes	while (readtoken() == TREDIR) {
5831556Srgrimes		*rpp = n2 = redirnode;
5841556Srgrimes		rpp = &n2->nfile.next;
5851556Srgrimes		parsefname();
5861556Srgrimes	}
5871556Srgrimes	tokpushback++;
5881556Srgrimes	*rpp = NULL;
5891556Srgrimes	if (redir) {
5901556Srgrimes		if (n1->type != NSUBSHELL) {
5911556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5921556Srgrimes			n2->type = NREDIR;
5931556Srgrimes			n2->nredir.n = n1;
5941556Srgrimes			n1 = n2;
5951556Srgrimes		}
5961556Srgrimes		n1->nredir.redirect = redir;
5971556Srgrimes	}
59875160Sbrian
599214281Sjilles	return n1;
6001556Srgrimes}
6011556Srgrimes
6021556Srgrimes
603213811Sobrienstatic union node *
60490111Simpsimplecmd(union node **rpp, union node *redir)
60590111Simp{
6061556Srgrimes	union node *args, **app;
6071556Srgrimes	union node **orig_rpp = rpp;
608210087Sjilles	union node *n = NULL;
6091556Srgrimes
6101556Srgrimes	/* If we don't have any redirections already, then we must reset */
6111556Srgrimes	/* rpp to be the address of the local redir variable.  */
6121556Srgrimes	if (redir == 0)
6131556Srgrimes		rpp = &redir;
6141556Srgrimes
6151556Srgrimes	args = NULL;
6161556Srgrimes	app = &args;
6178855Srgrimes	/*
6181556Srgrimes	 * We save the incoming value, because we need this for shell
6191556Srgrimes	 * functions.  There can not be a redirect or an argument between
6208855Srgrimes	 * the function name and the open parenthesis.
6211556Srgrimes	 */
6221556Srgrimes	orig_rpp = rpp;
6231556Srgrimes
6241556Srgrimes	for (;;) {
6251556Srgrimes		if (readtoken() == TWORD) {
6261556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
6271556Srgrimes			n->type = NARG;
6281556Srgrimes			n->narg.text = wordtext;
6291556Srgrimes			n->narg.backquote = backquotelist;
6301556Srgrimes			*app = n;
6311556Srgrimes			app = &n->narg.next;
6321556Srgrimes		} else if (lasttoken == TREDIR) {
6331556Srgrimes			*rpp = n = redirnode;
6341556Srgrimes			rpp = &n->nfile.next;
6351556Srgrimes			parsefname();	/* read name of redirection file */
6361556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6371556Srgrimes					    && rpp == orig_rpp) {
6381556Srgrimes			/* We have a function */
6391556Srgrimes			if (readtoken() != TRP)
6401556Srgrimes				synexpect(TRP);
641179022Sstefanf			funclinno = plinno;
642214291Sjilles			/*
643214291Sjilles			 * - Require plain text.
644214291Sjilles			 * - Functions with '/' cannot be called.
645214291Sjilles			 */
646214291Sjilles			if (!noexpand(n->narg.text) || quoteflag ||
647214291Sjilles			    strchr(n->narg.text, '/'))
6481556Srgrimes				synerror("Bad function name");
649214291Sjilles			rmescapes(n->narg.text);
6501556Srgrimes			n->type = NDEFUN;
6511556Srgrimes			n->narg.next = command();
652179022Sstefanf			funclinno = 0;
653210087Sjilles			return n;
6541556Srgrimes		} else {
6551556Srgrimes			tokpushback++;
6561556Srgrimes			break;
6571556Srgrimes		}
6581556Srgrimes	}
6591556Srgrimes	*app = NULL;
6601556Srgrimes	*rpp = NULL;
6611556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6621556Srgrimes	n->type = NCMD;
6631556Srgrimes	n->ncmd.backgnd = 0;
6641556Srgrimes	n->ncmd.args = args;
6651556Srgrimes	n->ncmd.redirect = redir;
666210087Sjilles	return n;
6671556Srgrimes}
6681556Srgrimes
669213811Sobrienstatic union node *
67090111Simpmakename(void)
67190111Simp{
67217987Speter	union node *n;
6731556Srgrimes
67417987Speter	n = (union node *)stalloc(sizeof (struct narg));
67517987Speter	n->type = NARG;
67617987Speter	n->narg.next = NULL;
67717987Speter	n->narg.text = wordtext;
67817987Speter	n->narg.backquote = backquotelist;
67917987Speter	return n;
68017987Speter}
68117987Speter
682213760Sobrienvoid
683213760Sobrienfixredir(union node *n, const char *text, int err)
68490111Simp{
68517987Speter	TRACE(("Fix redir %s %d\n", text, err));
68617987Speter	if (!err)
68717987Speter		n->ndup.vname = NULL;
68817987Speter
68917987Speter	if (is_digit(text[0]) && text[1] == '\0')
69017987Speter		n->ndup.dupfd = digit_val(text[0]);
69117987Speter	else if (text[0] == '-' && text[1] == '\0')
69217987Speter		n->ndup.dupfd = -1;
69317987Speter	else {
69420425Ssteve
69517987Speter		if (err)
69617987Speter			synerror("Bad fd number");
69717987Speter		else
69817987Speter			n->ndup.vname = makename();
69917987Speter	}
70017987Speter}
70117987Speter
70217987Speter
703213811Sobrienstatic void
70490111Simpparsefname(void)
70590111Simp{
7061556Srgrimes	union node *n = redirnode;
7071556Srgrimes
7081556Srgrimes	if (readtoken() != TWORD)
7091556Srgrimes		synexpect(-1);
7101556Srgrimes	if (n->type == NHERE) {
7111556Srgrimes		struct heredoc *here = heredoc;
7121556Srgrimes		struct heredoc *p;
7131556Srgrimes		int i;
7141556Srgrimes
7151556Srgrimes		if (quoteflag == 0)
7161556Srgrimes			n->type = NXHERE;
7171556Srgrimes		TRACE(("Here document %d\n", n->type));
7181556Srgrimes		if (here->striptabs) {
7191556Srgrimes			while (*wordtext == '\t')
7201556Srgrimes				wordtext++;
7211556Srgrimes		}
7221556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7231556Srgrimes			synerror("Illegal eof marker for << redirection");
7241556Srgrimes		rmescapes(wordtext);
7251556Srgrimes		here->eofmark = wordtext;
7261556Srgrimes		here->next = NULL;
7271556Srgrimes		if (heredoclist == NULL)
7281556Srgrimes			heredoclist = here;
7291556Srgrimes		else {
7301556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
7311556Srgrimes			p->next = here;
7321556Srgrimes		}
7331556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
73417987Speter		fixredir(n, wordtext, 0);
7351556Srgrimes	} else {
73617987Speter		n->nfile.fname = makename();
7371556Srgrimes	}
7381556Srgrimes}
7391556Srgrimes
7401556Srgrimes
7411556Srgrimes/*
7421556Srgrimes * Input any here documents.
7431556Srgrimes */
7441556Srgrimes
745213811Sobrienstatic void
74690111Simpparseheredoc(void)
74790111Simp{
7481556Srgrimes	struct heredoc *here;
7491556Srgrimes	union node *n;
7501556Srgrimes
7511556Srgrimes	while (heredoclist) {
7521556Srgrimes		here = heredoclist;
7531556Srgrimes		heredoclist = here->next;
7541556Srgrimes		if (needprompt) {
7551556Srgrimes			setprompt(2);
7561556Srgrimes			needprompt = 0;
7571556Srgrimes		}
7581556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
7591556Srgrimes				here->eofmark, here->striptabs);
7601556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
7611556Srgrimes		n->narg.type = NARG;
7621556Srgrimes		n->narg.next = NULL;
7631556Srgrimes		n->narg.text = wordtext;
7641556Srgrimes		n->narg.backquote = backquotelist;
7651556Srgrimes		here->here->nhere.doc = n;
7661556Srgrimes	}
7671556Srgrimes}
7681556Srgrimes
769213811Sobrienstatic int
77090111Simppeektoken(void)
77190111Simp{
7721556Srgrimes	int t;
7731556Srgrimes
7741556Srgrimes	t = readtoken();
7751556Srgrimes	tokpushback++;
7761556Srgrimes	return (t);
7771556Srgrimes}
7781556Srgrimes
779213811Sobrienstatic int
78090111Simpreadtoken(void)
78190111Simp{
7821556Srgrimes	int t;
7831556Srgrimes	int savecheckkwd = checkkwd;
7841556Srgrimes	struct alias *ap;
7851556Srgrimes#ifdef DEBUG
7861556Srgrimes	int alreadyseen = tokpushback;
7871556Srgrimes#endif
7888855Srgrimes
7891556Srgrimes	top:
7901556Srgrimes	t = xxreadtoken();
7911556Srgrimes
7921556Srgrimes	if (checkkwd) {
7931556Srgrimes		/*
7941556Srgrimes		 * eat newlines
7951556Srgrimes		 */
7961556Srgrimes		if (checkkwd == 2) {
7971556Srgrimes			checkkwd = 0;
7981556Srgrimes			while (t == TNL) {
7991556Srgrimes				parseheredoc();
8001556Srgrimes				t = xxreadtoken();
8011556Srgrimes			}
8021556Srgrimes		} else
8031556Srgrimes			checkkwd = 0;
8041556Srgrimes		/*
8051556Srgrimes		 * check for keywords and aliases
8061556Srgrimes		 */
80720425Ssteve		if (t == TWORD && !quoteflag)
80817987Speter		{
80998463Sjmallett			const char * const *pp;
8101556Srgrimes
81198463Sjmallett			for (pp = parsekwd; *pp; pp++) {
81220425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
81317987Speter				{
8141556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8151556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8161556Srgrimes					goto out;
8171556Srgrimes				}
8181556Srgrimes			}
81918018Speter			if (noaliases == 0 &&
82018018Speter			    (ap = lookupalias(wordtext, 1)) != NULL) {
8211556Srgrimes				pushstring(ap->val, strlen(ap->val), ap);
8221556Srgrimes				checkkwd = savecheckkwd;
8231556Srgrimes				goto top;
8241556Srgrimes			}
8251556Srgrimes		}
8261556Srgrimesout:
82775160Sbrian		checkkwd = (t == TNOT) ? savecheckkwd : 0;
8281556Srgrimes	}
8291556Srgrimes#ifdef DEBUG
8301556Srgrimes	if (!alreadyseen)
8311556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8321556Srgrimes	else
8331556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8341556Srgrimes#endif
8351556Srgrimes	return (t);
8361556Srgrimes}
8371556Srgrimes
8381556Srgrimes
8391556Srgrimes/*
8401556Srgrimes * Read the next input token.
8411556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8421556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8431556Srgrimes *	quoted.
8441556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8451556Srgrimes *	the redirection.
8461556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8471556Srgrimes *	on which the token starts.
8481556Srgrimes *
8491556Srgrimes * [Change comment:  here documents and internal procedures]
8501556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8511556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8521556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8531556Srgrimes *  We could also make parseoperator in essence the main routine, and
8541556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8551556Srgrimes */
8561556Srgrimes
8571556Srgrimes#define RETURN(token)	return lasttoken = token
8581556Srgrimes
859213811Sobrienstatic int
86090111Simpxxreadtoken(void)
86190111Simp{
86225230Ssteve	int c;
8631556Srgrimes
8641556Srgrimes	if (tokpushback) {
8651556Srgrimes		tokpushback = 0;
8661556Srgrimes		return lasttoken;
8671556Srgrimes	}
8681556Srgrimes	if (needprompt) {
8691556Srgrimes		setprompt(2);
8701556Srgrimes		needprompt = 0;
8711556Srgrimes	}
8721556Srgrimes	startlinno = plinno;
8731556Srgrimes	for (;;) {	/* until token or start of word found */
8741556Srgrimes		c = pgetc_macro();
8751556Srgrimes		if (c == ' ' || c == '\t')
8761556Srgrimes			continue;		/* quick check for white space first */
8771556Srgrimes		switch (c) {
8781556Srgrimes		case ' ': case '\t':
8791556Srgrimes			continue;
8801556Srgrimes		case '#':
8811556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
8821556Srgrimes			pungetc();
8831556Srgrimes			continue;
8841556Srgrimes		case '\\':
8851556Srgrimes			if (pgetc() == '\n') {
8861556Srgrimes				startlinno = ++plinno;
8871556Srgrimes				if (doprompt)
8881556Srgrimes					setprompt(2);
8891556Srgrimes				else
8901556Srgrimes					setprompt(0);
8911556Srgrimes				continue;
8921556Srgrimes			}
8931556Srgrimes			pungetc();
8941556Srgrimes			goto breakloop;
8951556Srgrimes		case '\n':
8961556Srgrimes			plinno++;
8971556Srgrimes			needprompt = doprompt;
8981556Srgrimes			RETURN(TNL);
8991556Srgrimes		case PEOF:
9001556Srgrimes			RETURN(TEOF);
9011556Srgrimes		case '&':
9021556Srgrimes			if (pgetc() == '&')
9031556Srgrimes				RETURN(TAND);
9041556Srgrimes			pungetc();
9051556Srgrimes			RETURN(TBACKGND);
9061556Srgrimes		case '|':
9071556Srgrimes			if (pgetc() == '|')
9081556Srgrimes				RETURN(TOR);
9091556Srgrimes			pungetc();
9101556Srgrimes			RETURN(TPIPE);
9111556Srgrimes		case ';':
9121556Srgrimes			if (pgetc() == ';')
9131556Srgrimes				RETURN(TENDCASE);
9141556Srgrimes			pungetc();
9151556Srgrimes			RETURN(TSEMI);
9161556Srgrimes		case '(':
9171556Srgrimes			RETURN(TLP);
9181556Srgrimes		case ')':
9191556Srgrimes			RETURN(TRP);
9201556Srgrimes		default:
9211556Srgrimes			goto breakloop;
9221556Srgrimes		}
9231556Srgrimes	}
9241556Srgrimesbreakloop:
9251556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9261556Srgrimes#undef RETURN
9271556Srgrimes}
9281556Srgrimes
9291556Srgrimes
930213811Sobrien#define MAXNEST_static 8
931206145Sjillesstruct tokenstate
932206145Sjilles{
933206145Sjilles	const char *syntax; /* *SYNTAX */
934206145Sjilles	int parenlevel; /* levels of parentheses in arithmetic */
935206145Sjilles	enum tokenstate_category
936206145Sjilles	{
937206145Sjilles		TSTATE_TOP,
938206145Sjilles		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
939206145Sjilles		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
940206145Sjilles		TSTATE_ARITH
941206145Sjilles	} category;
942206145Sjilles};
943206145Sjilles
944206145Sjilles
945205130Sjilles/*
946205130Sjilles * Called to parse command substitutions.
947205130Sjilles */
9481556Srgrimes
949213811Sobrienstatic char *
950205130Sjillesparsebackq(char *out, struct nodelist **pbqlist,
951205130Sjilles		int oldstyle, int dblquote, int quoted)
952205130Sjilles{
953205130Sjilles	struct nodelist **nlpp;
954205130Sjilles	union node *n;
955205130Sjilles	char *volatile str;
956205130Sjilles	struct jmploc jmploc;
957205130Sjilles	struct jmploc *const savehandler = handler;
958205130Sjilles	int savelen;
959205130Sjilles	int saveprompt;
960205130Sjilles	const int bq_startlinno = plinno;
961205130Sjilles	char *volatile ostr = NULL;
962205130Sjilles	struct parsefile *const savetopfile = getcurrentfile();
963208655Sjilles	struct heredoc *const saveheredoclist = heredoclist;
964208655Sjilles	struct heredoc *here;
965205130Sjilles
966205130Sjilles	str = NULL;
967205130Sjilles	if (setjmp(jmploc.loc)) {
968205130Sjilles		popfilesupto(savetopfile);
969205130Sjilles		if (str)
970205130Sjilles			ckfree(str);
971205130Sjilles		if (ostr)
972205130Sjilles			ckfree(ostr);
973208655Sjilles		heredoclist = saveheredoclist;
974205130Sjilles		handler = savehandler;
975205130Sjilles		if (exception == EXERROR) {
976205130Sjilles			startlinno = bq_startlinno;
977205130Sjilles			synerror("Error in command substitution");
978205130Sjilles		}
979205130Sjilles		longjmp(handler->loc, 1);
980205130Sjilles	}
981205130Sjilles	INTOFF;
982205130Sjilles	savelen = out - stackblock();
983205130Sjilles	if (savelen > 0) {
984205130Sjilles		str = ckmalloc(savelen);
985205130Sjilles		memcpy(str, stackblock(), savelen);
986205130Sjilles	}
987205130Sjilles	handler = &jmploc;
988208655Sjilles	heredoclist = NULL;
989205130Sjilles	INTON;
990205130Sjilles        if (oldstyle) {
991205130Sjilles                /* We must read until the closing backquote, giving special
992205130Sjilles                   treatment to some slashes, and then push the string and
993205130Sjilles                   reread it as input, interpreting it normally.  */
994205130Sjilles                char *oout;
995205130Sjilles                int c;
996205130Sjilles                int olen;
997205130Sjilles
998205130Sjilles
999205130Sjilles                STARTSTACKSTR(oout);
1000205130Sjilles		for (;;) {
1001205130Sjilles			if (needprompt) {
1002205130Sjilles				setprompt(2);
1003205130Sjilles				needprompt = 0;
1004205130Sjilles			}
1005205130Sjilles			switch (c = pgetc()) {
1006205130Sjilles			case '`':
1007205130Sjilles				goto done;
1008205130Sjilles
1009205130Sjilles			case '\\':
1010205130Sjilles                                if ((c = pgetc()) == '\n') {
1011205130Sjilles					plinno++;
1012205130Sjilles					if (doprompt)
1013205130Sjilles						setprompt(2);
1014205130Sjilles					else
1015205130Sjilles						setprompt(0);
1016205130Sjilles					/*
1017205130Sjilles					 * If eating a newline, avoid putting
1018205130Sjilles					 * the newline into the new character
1019205130Sjilles					 * stream (via the STPUTC after the
1020205130Sjilles					 * switch).
1021205130Sjilles					 */
1022205130Sjilles					continue;
1023205130Sjilles				}
1024205130Sjilles                                if (c != '\\' && c != '`' && c != '$'
1025205130Sjilles                                    && (!dblquote || c != '"'))
1026205130Sjilles                                        STPUTC('\\', oout);
1027205130Sjilles				break;
1028205130Sjilles
1029205130Sjilles			case '\n':
1030205130Sjilles				plinno++;
1031205130Sjilles				needprompt = doprompt;
1032205130Sjilles				break;
1033205130Sjilles
1034205130Sjilles			case PEOF:
1035205130Sjilles			        startlinno = plinno;
1036205130Sjilles				synerror("EOF in backquote substitution");
1037205130Sjilles 				break;
1038205130Sjilles
1039205130Sjilles			default:
1040205130Sjilles				break;
1041205130Sjilles			}
1042205130Sjilles			STPUTC(c, oout);
1043205130Sjilles                }
1044205130Sjillesdone:
1045205130Sjilles                STPUTC('\0', oout);
1046205130Sjilles                olen = oout - stackblock();
1047205130Sjilles		INTOFF;
1048205130Sjilles		ostr = ckmalloc(olen);
1049205130Sjilles		memcpy(ostr, stackblock(), olen);
1050205130Sjilles		setinputstring(ostr, 1);
1051205130Sjilles		INTON;
1052205130Sjilles        }
1053205130Sjilles	nlpp = pbqlist;
1054205130Sjilles	while (*nlpp)
1055205130Sjilles		nlpp = &(*nlpp)->next;
1056205130Sjilles	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1057205130Sjilles	(*nlpp)->next = NULL;
1058205130Sjilles
1059205130Sjilles	if (oldstyle) {
1060205130Sjilles		saveprompt = doprompt;
1061205130Sjilles		doprompt = 0;
1062205130Sjilles	}
1063205130Sjilles
1064205130Sjilles	n = list(0);
1065205130Sjilles
1066205130Sjilles	if (oldstyle)
1067205130Sjilles		doprompt = saveprompt;
1068205130Sjilles	else {
1069205130Sjilles		if (readtoken() != TRP)
1070205130Sjilles			synexpect(TRP);
1071205130Sjilles	}
1072205130Sjilles
1073205130Sjilles	(*nlpp)->n = n;
1074205130Sjilles        if (oldstyle) {
1075205130Sjilles		/*
1076205130Sjilles		 * Start reading from old file again, ignoring any pushed back
1077205130Sjilles		 * tokens left from the backquote parsing
1078205130Sjilles		 */
1079205130Sjilles                popfile();
1080205130Sjilles		tokpushback = 0;
1081205130Sjilles	}
1082205130Sjilles	while (stackblocksize() <= savelen)
1083205130Sjilles		growstackblock();
1084205130Sjilles	STARTSTACKSTR(out);
1085208655Sjilles	INTOFF;
1086205130Sjilles	if (str) {
1087205130Sjilles		memcpy(out, str, savelen);
1088205130Sjilles		STADJUST(savelen, out);
1089205130Sjilles		ckfree(str);
1090205130Sjilles		str = NULL;
1091205130Sjilles	}
1092205130Sjilles	if (ostr) {
1093205130Sjilles		ckfree(ostr);
1094205130Sjilles		ostr = NULL;
1095205130Sjilles	}
1096208655Sjilles	here = saveheredoclist;
1097208655Sjilles	if (here != NULL) {
1098208655Sjilles		while (here->next != NULL)
1099208655Sjilles			here = here->next;
1100208655Sjilles		here->next = heredoclist;
1101208655Sjilles		heredoclist = saveheredoclist;
1102208655Sjilles	}
1103205130Sjilles	handler = savehandler;
1104208655Sjilles	INTON;
1105205130Sjilles	if (quoted)
1106205130Sjilles		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1107205130Sjilles	else
1108205130Sjilles		USTPUTC(CTLBACKQ, out);
1109205130Sjilles	return out;
1110205130Sjilles}
1111205130Sjilles
1112205130Sjilles
11131556Srgrimes/*
11141556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
11151556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
11161556Srgrimes * word which marks the end of the document and striptabs is true if
11171556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
11181556Srgrimes * is the first character of the input token or document.
11191556Srgrimes *
11201556Srgrimes * Because C does not have internal subroutines, I have simulated them
11211556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
11221556Srgrimes * will run code that appears at the end of readtoken1.
11231556Srgrimes */
11241556Srgrimes
11251556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
11261556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
11271556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
11281556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
11291556Srgrimes
1130213811Sobrienstatic int
1131206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
113290111Simp{
113317987Speter	int c = firstc;
113417987Speter	char *out;
11351556Srgrimes	int len;
11361556Srgrimes	char line[EOFMARKLEN + 1];
11371556Srgrimes	struct nodelist *bqlist;
11381556Srgrimes	int quotef;
1139206145Sjilles	int newvarnest;
1140206145Sjilles	int level;
114154679Scracauer	int synentry;
1142213811Sobrien	struct tokenstate state_static[MAXNEST_static];
1143213811Sobrien	int maxnest = MAXNEST_static;
1144206145Sjilles	struct tokenstate *state = state_static;
11451556Srgrimes
11461556Srgrimes	startlinno = plinno;
11471556Srgrimes	quotef = 0;
11481556Srgrimes	bqlist = NULL;
1149206145Sjilles	newvarnest = 0;
1150206145Sjilles	level = 0;
1151206145Sjilles	state[level].syntax = initialsyntax;
1152206145Sjilles	state[level].parenlevel = 0;
1153206145Sjilles	state[level].category = TSTATE_TOP;
11541556Srgrimes
11551556Srgrimes	STARTSTACKSTR(out);
11561556Srgrimes	loop: {	/* for each line, until end of word */
11571556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
11581556Srgrimes		for (;;) {	/* until end of line or end of word */
11591556Srgrimes			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
116054679Scracauer
1161206145Sjilles			synentry = state[level].syntax[c];
116254679Scracauer
116354679Scracauer			switch(synentry) {
11641556Srgrimes			case CNL:	/* '\n' */
1165206145Sjilles				if (state[level].syntax == BASESYNTAX)
11661556Srgrimes					goto endword;	/* exit outer loop */
11671556Srgrimes				USTPUTC(c, out);
11681556Srgrimes				plinno++;
11691556Srgrimes				if (doprompt)
11701556Srgrimes					setprompt(2);
11711556Srgrimes				else
11721556Srgrimes					setprompt(0);
11731556Srgrimes				c = pgetc();
11741556Srgrimes				goto loop;		/* continue outer loop */
11751556Srgrimes			case CWORD:
11761556Srgrimes				USTPUTC(c, out);
11771556Srgrimes				break;
11781556Srgrimes			case CCTL:
1179206145Sjilles				if (eofmark == NULL || initialsyntax != SQSYNTAX)
11801556Srgrimes					USTPUTC(CTLESC, out);
11811556Srgrimes				USTPUTC(c, out);
11821556Srgrimes				break;
11831556Srgrimes			case CBACK:	/* backslash */
11841556Srgrimes				c = pgetc();
11851556Srgrimes				if (c == PEOF) {
11861556Srgrimes					USTPUTC('\\', out);
11871556Srgrimes					pungetc();
11881556Srgrimes				} else if (c == '\n') {
1189160849Syar					plinno++;
11901556Srgrimes					if (doprompt)
11911556Srgrimes						setprompt(2);
11921556Srgrimes					else
11931556Srgrimes						setprompt(0);
11941556Srgrimes				} else {
1195206145Sjilles					if (state[level].syntax == DQSYNTAX &&
1196206145Sjilles					    c != '\\' && c != '`' && c != '$' &&
1197206145Sjilles					    (c != '"' || (eofmark != NULL &&
1198206145Sjilles						newvarnest == 0)) &&
1199206145Sjilles					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
12001556Srgrimes						USTPUTC('\\', out);
120183675Stegge					if (SQSYNTAX[c] == CCTL)
12021556Srgrimes						USTPUTC(CTLESC, out);
1203206145Sjilles					else if (eofmark == NULL ||
1204206145Sjilles					    newvarnest > 0)
120538887Stegge						USTPUTC(CTLQUOTEMARK, out);
12061556Srgrimes					USTPUTC(c, out);
12071556Srgrimes					quotef++;
12081556Srgrimes				}
12091556Srgrimes				break;
12101556Srgrimes			case CSQUOTE:
1211206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1212206145Sjilles				state[level].syntax = SQSYNTAX;
12131556Srgrimes				break;
12141556Srgrimes			case CDQUOTE:
1215206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1216206145Sjilles				state[level].syntax = DQSYNTAX;
12171556Srgrimes				break;
12181556Srgrimes			case CENDQUOTE:
1219206145Sjilles				if (eofmark != NULL && newvarnest == 0)
12201556Srgrimes					USTPUTC(c, out);
1221206145Sjilles				else {
1222206473Sjilles					if (state[level].category == TSTATE_ARITH)
1223206473Sjilles						state[level].syntax = ARISYNTAX;
1224206473Sjilles					else
1225206473Sjilles						state[level].syntax = BASESYNTAX;
12261556Srgrimes					quotef++;
12271556Srgrimes				}
12281556Srgrimes				break;
12291556Srgrimes			case CVAR:	/* '$' */
12301556Srgrimes				PARSESUB();		/* parse substitution */
12311556Srgrimes				break;
12321556Srgrimes			case CENDVAR:	/* '}' */
1233206145Sjilles				if (level > 0 &&
1234206145Sjilles				    (state[level].category == TSTATE_VAR_OLD ||
1235206145Sjilles				    state[level].category == TSTATE_VAR_NEW)) {
1236206145Sjilles					if (state[level].category == TSTATE_VAR_OLD)
1237206145Sjilles						state[level - 1].syntax = state[level].syntax;
1238206145Sjilles					else
1239206145Sjilles						newvarnest--;
1240206145Sjilles					level--;
12411556Srgrimes					USTPUTC(CTLENDVAR, out);
12421556Srgrimes				} else {
12431556Srgrimes					USTPUTC(c, out);
12441556Srgrimes				}
12451556Srgrimes				break;
12461556Srgrimes			case CLP:	/* '(' in arithmetic */
1247206145Sjilles				state[level].parenlevel++;
12481556Srgrimes				USTPUTC(c, out);
12491556Srgrimes				break;
12501556Srgrimes			case CRP:	/* ')' in arithmetic */
1251206145Sjilles				if (state[level].parenlevel > 0) {
12521556Srgrimes					USTPUTC(c, out);
1253206145Sjilles					--state[level].parenlevel;
12541556Srgrimes				} else {
12551556Srgrimes					if (pgetc() == ')') {
1256206145Sjilles						if (level > 0 &&
1257206145Sjilles						    state[level].category == TSTATE_ARITH) {
1258206145Sjilles							level--;
12591556Srgrimes							USTPUTC(CTLENDARI, out);
12601556Srgrimes						} else
12611556Srgrimes							USTPUTC(')', out);
12621556Srgrimes					} else {
12638855Srgrimes						/*
12641556Srgrimes						 * unbalanced parens
12651556Srgrimes						 *  (don't 2nd guess - no error)
12661556Srgrimes						 */
12671556Srgrimes						pungetc();
12681556Srgrimes						USTPUTC(')', out);
12691556Srgrimes					}
12701556Srgrimes				}
12711556Srgrimes				break;
12721556Srgrimes			case CBQUOTE:	/* '`' */
1273206145Sjilles				out = parsebackq(out, &bqlist, 1,
1274206145Sjilles				    state[level].syntax == DQSYNTAX &&
1275206145Sjilles				    (eofmark == NULL || newvarnest > 0),
1276206145Sjilles				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
12771556Srgrimes				break;
12781556Srgrimes			case CEOF:
12791556Srgrimes				goto endword;		/* exit outer loop */
12801556Srgrimes			default:
1281206145Sjilles				if (level == 0)
12821556Srgrimes					goto endword;	/* exit outer loop */
12831556Srgrimes				USTPUTC(c, out);
12841556Srgrimes			}
12851556Srgrimes			c = pgetc_macro();
12861556Srgrimes		}
12871556Srgrimes	}
12881556Srgrimesendword:
1289206145Sjilles	if (state[level].syntax == ARISYNTAX)
12901556Srgrimes		synerror("Missing '))'");
1291206145Sjilles	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
12921556Srgrimes		synerror("Unterminated quoted string");
1293206145Sjilles	if (state[level].category == TSTATE_VAR_OLD ||
1294206145Sjilles	    state[level].category == TSTATE_VAR_NEW) {
12951556Srgrimes		startlinno = plinno;
12961556Srgrimes		synerror("Missing '}'");
12971556Srgrimes	}
1298206145Sjilles	if (state != state_static)
1299206145Sjilles		parser_temp_free_upto(state);
13001556Srgrimes	USTPUTC('\0', out);
13011556Srgrimes	len = out - stackblock();
13021556Srgrimes	out = stackblock();
13031556Srgrimes	if (eofmark == NULL) {
13041556Srgrimes		if ((c == '>' || c == '<')
13051556Srgrimes		 && quotef == 0
13061556Srgrimes		 && len <= 2
13071556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
13081556Srgrimes			PARSEREDIR();
13091556Srgrimes			return lasttoken = TREDIR;
13101556Srgrimes		} else {
13111556Srgrimes			pungetc();
13121556Srgrimes		}
13131556Srgrimes	}
13141556Srgrimes	quoteflag = quotef;
13151556Srgrimes	backquotelist = bqlist;
13161556Srgrimes	grabstackblock(len);
13171556Srgrimes	wordtext = out;
13181556Srgrimes	return lasttoken = TWORD;
13191556Srgrimes/* end of readtoken routine */
13201556Srgrimes
13211556Srgrimes
13221556Srgrimes/*
13231556Srgrimes * Check to see whether we are at the end of the here document.  When this
13241556Srgrimes * is called, c is set to the first character of the next input line.  If
13251556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
13261556Srgrimes */
13271556Srgrimes
13281556Srgrimescheckend: {
13291556Srgrimes	if (eofmark) {
13301556Srgrimes		if (striptabs) {
13311556Srgrimes			while (c == '\t')
13321556Srgrimes				c = pgetc();
13331556Srgrimes		}
13341556Srgrimes		if (c == *eofmark) {
13351556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
133625230Ssteve				char *p, *q;
13371556Srgrimes
13381556Srgrimes				p = line;
13391556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
13401556Srgrimes				if (*p == '\n' && *q == '\0') {
13411556Srgrimes					c = PEOF;
13421556Srgrimes					plinno++;
13431556Srgrimes					needprompt = doprompt;
13441556Srgrimes				} else {
13451556Srgrimes					pushstring(line, strlen(line), NULL);
13461556Srgrimes				}
13471556Srgrimes			}
13481556Srgrimes		}
13491556Srgrimes	}
13501556Srgrimes	goto checkend_return;
13511556Srgrimes}
13521556Srgrimes
13531556Srgrimes
13541556Srgrimes/*
13551556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
13561556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
13571556Srgrimes * first character of the redirection operator.
13581556Srgrimes */
13591556Srgrimes
13601556Srgrimesparseredir: {
13611556Srgrimes	char fd = *out;
13621556Srgrimes	union node *np;
13631556Srgrimes
13641556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
13651556Srgrimes	if (c == '>') {
13661556Srgrimes		np->nfile.fd = 1;
13671556Srgrimes		c = pgetc();
13681556Srgrimes		if (c == '>')
13691556Srgrimes			np->type = NAPPEND;
13701556Srgrimes		else if (c == '&')
13711556Srgrimes			np->type = NTOFD;
137296922Stjr		else if (c == '|')
137396922Stjr			np->type = NCLOBBER;
13741556Srgrimes		else {
13751556Srgrimes			np->type = NTO;
13761556Srgrimes			pungetc();
13771556Srgrimes		}
13781556Srgrimes	} else {	/* c == '<' */
13791556Srgrimes		np->nfile.fd = 0;
13801556Srgrimes		c = pgetc();
13811556Srgrimes		if (c == '<') {
13821556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
13831556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
13841556Srgrimes				np->nfile.fd = 0;
13851556Srgrimes			}
13861556Srgrimes			np->type = NHERE;
13871556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
13881556Srgrimes			heredoc->here = np;
13891556Srgrimes			if ((c = pgetc()) == '-') {
13901556Srgrimes				heredoc->striptabs = 1;
13911556Srgrimes			} else {
13921556Srgrimes				heredoc->striptabs = 0;
13931556Srgrimes				pungetc();
13941556Srgrimes			}
13951556Srgrimes		} else if (c == '&')
13961556Srgrimes			np->type = NFROMFD;
139766612Sbrian		else if (c == '>')
139866612Sbrian			np->type = NFROMTO;
13991556Srgrimes		else {
14001556Srgrimes			np->type = NFROM;
14011556Srgrimes			pungetc();
14021556Srgrimes		}
14031556Srgrimes	}
14041556Srgrimes	if (fd != '\0')
14051556Srgrimes		np->nfile.fd = digit_val(fd);
14061556Srgrimes	redirnode = np;
14071556Srgrimes	goto parseredir_return;
14081556Srgrimes}
14091556Srgrimes
14101556Srgrimes
14111556Srgrimes/*
14121556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
14131556Srgrimes * and nothing else.
14141556Srgrimes */
14151556Srgrimes
14161556Srgrimesparsesub: {
1417179022Sstefanf	char buf[10];
14181556Srgrimes	int subtype;
14191556Srgrimes	int typeloc;
14201556Srgrimes	int flags;
14211556Srgrimes	char *p;
14221556Srgrimes	static const char types[] = "}-+?=";
1423179022Sstefanf	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1424179022Sstefanf	int i;
1425179022Sstefanf	int linno;
1426179387Sstefanf	int length;
14271556Srgrimes
14281556Srgrimes	c = pgetc();
1429149026Sstefanf	if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1430149026Sstefanf	    !is_special(c)) {
14311556Srgrimes		USTPUTC('$', out);
14321556Srgrimes		pungetc();
14331556Srgrimes	} else if (c == '(') {	/* $(command) or $((arith)) */
14341556Srgrimes		if (pgetc() == '(') {
14351556Srgrimes			PARSEARITH();
14361556Srgrimes		} else {
14371556Srgrimes			pungetc();
1438206145Sjilles			out = parsebackq(out, &bqlist, 0,
1439206145Sjilles			    state[level].syntax == DQSYNTAX &&
1440206145Sjilles			    (eofmark == NULL || newvarnest > 0),
1441206145Sjilles			    state[level].syntax == DQSYNTAX ||
1442206145Sjilles			    state[level].syntax == ARISYNTAX);
14431556Srgrimes		}
14441556Srgrimes	} else {
14451556Srgrimes		USTPUTC(CTLVAR, out);
14461556Srgrimes		typeloc = out - stackblock();
14471556Srgrimes		USTPUTC(VSNORMAL, out);
14481556Srgrimes		subtype = VSNORMAL;
1449179022Sstefanf		flags = 0;
14501556Srgrimes		if (c == '{') {
145118202Speter			bracketed_name = 1;
14521556Srgrimes			c = pgetc();
145317987Speter			if (c == '#') {
145417987Speter				if ((c = pgetc()) == '}')
145517987Speter					c = '#';
145617987Speter				else
145717987Speter					subtype = VSLENGTH;
145817987Speter			}
145917987Speter			else
146017987Speter				subtype = 0;
14611556Srgrimes		}
1462149026Sstefanf		if (!is_eof(c) && is_name(c)) {
1463179387Sstefanf			length = 0;
14641556Srgrimes			do {
14651556Srgrimes				STPUTC(c, out);
14661556Srgrimes				c = pgetc();
1467179387Sstefanf				length++;
1468149026Sstefanf			} while (!is_eof(c) && is_in_name(c));
1469179387Sstefanf			if (length == 6 &&
1470179387Sstefanf			    strncmp(out - length, "LINENO", length) == 0) {
1471179022Sstefanf				/* Replace the variable name with the
1472179022Sstefanf				 * current line number. */
1473179022Sstefanf				linno = plinno;
1474179022Sstefanf				if (funclinno != 0)
1475179022Sstefanf					linno -= funclinno - 1;
1476179022Sstefanf				snprintf(buf, sizeof(buf), "%d", linno);
1477179022Sstefanf				STADJUST(-6, out);
1478179022Sstefanf				for (i = 0; buf[i] != '\0'; i++)
1479179022Sstefanf					STPUTC(buf[i], out);
1480179022Sstefanf				flags |= VSLINENO;
1481179022Sstefanf			}
148218202Speter		} else if (is_digit(c)) {
148318202Speter			if (bracketed_name) {
148418202Speter				do {
148518202Speter					STPUTC(c, out);
148618202Speter					c = pgetc();
148718202Speter				} while (is_digit(c));
148818202Speter			} else {
148918202Speter				STPUTC(c, out);
149018202Speter				c = pgetc();
149118202Speter			}
14921556Srgrimes		} else {
1493164003Sstefanf			if (! is_special(c)) {
1494164003Sstefanf				subtype = VSERROR;
1495164003Sstefanf				if (c == '}')
1496164003Sstefanf					pungetc();
1497206144Sjilles				else if (c == '\n' || c == PEOF)
1498206144Sjilles					synerror("Unexpected end of line in substitution");
1499164003Sstefanf				else
1500164003Sstefanf					USTPUTC(c, out);
1501164003Sstefanf			} else {
1502164003Sstefanf				USTPUTC(c, out);
1503164003Sstefanf				c = pgetc();
1504164003Sstefanf			}
15051556Srgrimes		}
15061556Srgrimes		if (subtype == 0) {
150717987Speter			switch (c) {
150817987Speter			case ':':
1509179022Sstefanf				flags |= VSNUL;
15101556Srgrimes				c = pgetc();
151117987Speter				/*FALLTHROUGH*/
151217987Speter			default:
151317987Speter				p = strchr(types, c);
1514164003Sstefanf				if (p == NULL) {
1515206144Sjilles					if (c == '\n' || c == PEOF)
1516206144Sjilles						synerror("Unexpected end of line in substitution");
1517164003Sstefanf					if (flags == VSNUL)
1518164003Sstefanf						STPUTC(':', out);
1519164003Sstefanf					STPUTC(c, out);
1520164003Sstefanf					subtype = VSERROR;
1521164003Sstefanf				} else
1522164003Sstefanf					subtype = p - types + VSNORMAL;
152317987Speter				break;
152417987Speter			case '%':
152520425Ssteve			case '#':
152617987Speter				{
152717987Speter					int cc = c;
152817987Speter					subtype = c == '#' ? VSTRIMLEFT :
152917987Speter							     VSTRIMRIGHT;
153017987Speter					c = pgetc();
153117987Speter					if (c == cc)
153217987Speter						subtype++;
153317987Speter					else
153417987Speter						pungetc();
153517987Speter					break;
153617987Speter				}
15371556Srgrimes			}
1538164003Sstefanf		} else if (subtype != VSERROR) {
15391556Srgrimes			pungetc();
15401556Srgrimes		}
1541164003Sstefanf		STPUTC('=', out);
1542206145Sjilles		if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX ||
1543206145Sjilles		    state[level].syntax == ARISYNTAX))
15441556Srgrimes			flags |= VSQUOTE;
15451556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
1546206145Sjilles		if (subtype != VSNORMAL) {
1547206145Sjilles			if (level + 1 >= maxnest) {
1548206145Sjilles				maxnest *= 2;
1549206145Sjilles				if (state == state_static) {
1550206145Sjilles					state = parser_temp_alloc(
1551206145Sjilles					    maxnest * sizeof(*state));
1552206145Sjilles					memcpy(state, state_static,
1553213811Sobrien					    MAXNEST_static * sizeof(*state));
1554206145Sjilles				} else
1555206145Sjilles					state = parser_temp_realloc(state,
1556206145Sjilles					    maxnest * sizeof(*state));
1557206145Sjilles			}
1558206145Sjilles			level++;
1559206145Sjilles			state[level].parenlevel = 0;
1560206145Sjilles			if (subtype == VSMINUS || subtype == VSPLUS ||
1561206145Sjilles			    subtype == VSQUESTION || subtype == VSASSIGN) {
1562206145Sjilles				/*
1563206145Sjilles				 * For operators that were in the Bourne shell,
1564206145Sjilles				 * inherit the double-quote state.
1565206145Sjilles				 */
1566206145Sjilles				state[level].syntax = state[level - 1].syntax;
1567206145Sjilles				state[level].category = TSTATE_VAR_OLD;
1568206145Sjilles			} else {
1569206145Sjilles				/*
1570206145Sjilles				 * The other operators take a pattern,
1571206145Sjilles				 * so go to BASESYNTAX.
1572206145Sjilles				 * Also, ' and " are now special, even
1573206145Sjilles				 * in here documents.
1574206145Sjilles				 */
1575206145Sjilles				state[level].syntax = BASESYNTAX;
1576206145Sjilles				state[level].category = TSTATE_VAR_NEW;
1577206145Sjilles				newvarnest++;
1578206145Sjilles			}
1579206145Sjilles		}
15801556Srgrimes	}
15811556Srgrimes	goto parsesub_return;
15821556Srgrimes}
15831556Srgrimes
15841556Srgrimes
15851556Srgrimes/*
15861556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
15871556Srgrimes */
15881556Srgrimesparsearith: {
15891556Srgrimes
1590206145Sjilles	if (level + 1 >= maxnest) {
1591206145Sjilles		maxnest *= 2;
1592206145Sjilles		if (state == state_static) {
1593206145Sjilles			state = parser_temp_alloc(
1594206145Sjilles			    maxnest * sizeof(*state));
1595206145Sjilles			memcpy(state, state_static,
1596213811Sobrien			    MAXNEST_static * sizeof(*state));
1597206145Sjilles		} else
1598206145Sjilles			state = parser_temp_realloc(state,
1599206145Sjilles			    maxnest * sizeof(*state));
16001556Srgrimes	}
1601206145Sjilles	level++;
1602206145Sjilles	state[level].syntax = ARISYNTAX;
1603206145Sjilles	state[level].parenlevel = 0;
1604206145Sjilles	state[level].category = TSTATE_ARITH;
1605206145Sjilles	USTPUTC(CTLARI, out);
1606206145Sjilles	if (state[level - 1].syntax == DQSYNTAX)
1607206145Sjilles		USTPUTC('"',out);
1608206145Sjilles	else
1609206145Sjilles		USTPUTC(' ',out);
16101556Srgrimes	goto parsearith_return;
16111556Srgrimes}
16121556Srgrimes
16131556Srgrimes} /* end of readtoken */
16141556Srgrimes
16151556Srgrimes
16161556Srgrimes
16171556Srgrimes#ifdef mkinit
16181556SrgrimesRESET {
16191556Srgrimes	tokpushback = 0;
16201556Srgrimes	checkkwd = 0;
16211556Srgrimes}
16221556Srgrimes#endif
16231556Srgrimes
16241556Srgrimes/*
16251556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
16261556Srgrimes * or backquotes).
16271556Srgrimes */
16281556Srgrimes
1629213811Sobrienstatic int
163090111Simpnoexpand(char *text)
163190111Simp{
163225230Ssteve	char *p;
163325230Ssteve	char c;
16341556Srgrimes
16351556Srgrimes	p = text;
16361556Srgrimes	while ((c = *p++) != '\0') {
163739137Stegge		if ( c == CTLQUOTEMARK)
163839137Stegge			continue;
16391556Srgrimes		if (c == CTLESC)
16401556Srgrimes			p++;
164183675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
16421556Srgrimes			return 0;
16431556Srgrimes	}
16441556Srgrimes	return 1;
16451556Srgrimes}
16461556Srgrimes
16471556Srgrimes
16481556Srgrimes/*
16491556Srgrimes * Return true if the argument is a legal variable name (a letter or
16501556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
16511556Srgrimes */
16521556Srgrimes
16531556Srgrimesint
1654200956Sjillesgoodname(const char *name)
165590111Simp{
1656200956Sjilles	const char *p;
16571556Srgrimes
16581556Srgrimes	p = name;
16591556Srgrimes	if (! is_name(*p))
16601556Srgrimes		return 0;
16611556Srgrimes	while (*++p) {
16621556Srgrimes		if (! is_in_name(*p))
16631556Srgrimes			return 0;
16641556Srgrimes	}
16651556Srgrimes	return 1;
16661556Srgrimes}
16671556Srgrimes
16681556Srgrimes
16691556Srgrimes/*
16701556Srgrimes * Called when an unexpected token is read during the parse.  The argument
16711556Srgrimes * is the token that is expected, or -1 if more than one type of token can
16721556Srgrimes * occur at this point.
16731556Srgrimes */
16741556Srgrimes
1675213811Sobrienstatic void
167690111Simpsynexpect(int token)
167717987Speter{
16781556Srgrimes	char msg[64];
16791556Srgrimes
16801556Srgrimes	if (token >= 0) {
16811556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
16821556Srgrimes			tokname[lasttoken], tokname[token]);
16831556Srgrimes	} else {
16841556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
16851556Srgrimes	}
16861556Srgrimes	synerror(msg);
16871556Srgrimes}
16881556Srgrimes
16891556Srgrimes
1690213811Sobrienstatic void
1691201053Sjillessynerror(const char *msg)
169290111Simp{
16931556Srgrimes	if (commandname)
1694201366Sjilles		outfmt(out2, "%s: %d: ", commandname, startlinno);
1695201366Sjilles	outfmt(out2, "Syntax error: %s\n", msg);
16961556Srgrimes	error((char *)NULL);
16971556Srgrimes}
16981556Srgrimes
1699213811Sobrienstatic void
170090111Simpsetprompt(int which)
170190111Simp{
17021556Srgrimes	whichprompt = which;
17031556Srgrimes
170417987Speter#ifndef NO_HISTORY
17051556Srgrimes	if (!el)
170617987Speter#endif
1707199629Sjilles	{
17081556Srgrimes		out2str(getprompt(NULL));
1709199629Sjilles		flushout(out2);
1710199629Sjilles	}
17111556Srgrimes}
17121556Srgrimes
17131556Srgrimes/*
17141556Srgrimes * called by editline -- any expansions to the prompt
17151556Srgrimes *    should be added here.
17161556Srgrimes */
17171556Srgrimeschar *
171890111Simpgetprompt(void *unused __unused)
171925905Ssteve{
1720142845Sobrien	static char ps[PROMPTLEN];
1721142845Sobrien	char *fmt;
1722209653Sjilles	const char *pwd;
1723209653Sjilles	int i, trim;
1724201053Sjilles	static char internal_error[] = "<internal prompt error>";
1725142845Sobrien
1726142845Sobrien	/*
1727142845Sobrien	 * Select prompt format.
1728142845Sobrien	 */
17291556Srgrimes	switch (whichprompt) {
17301556Srgrimes	case 0:
1731201053Sjilles		fmt = nullstr;
1732142845Sobrien		break;
17331556Srgrimes	case 1:
1734142845Sobrien		fmt = ps1val();
1735142845Sobrien		break;
17361556Srgrimes	case 2:
1737142845Sobrien		fmt = ps2val();
1738142845Sobrien		break;
17391556Srgrimes	default:
1740201053Sjilles		return internal_error;
17411556Srgrimes	}
1742142845Sobrien
1743142845Sobrien	/*
1744142845Sobrien	 * Format prompt string.
1745142845Sobrien	 */
1746142845Sobrien	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1747142845Sobrien		if (*fmt == '\\')
1748142845Sobrien			switch (*++fmt) {
1749142845Sobrien
1750142845Sobrien				/*
1751142845Sobrien				 * Hostname.
1752142845Sobrien				 *
1753142845Sobrien				 * \h specifies just the local hostname,
1754142845Sobrien				 * \H specifies fully-qualified hostname.
1755142845Sobrien				 */
1756142845Sobrien			case 'h':
1757142845Sobrien			case 'H':
1758149024Sstefanf				ps[i] = '\0';
1759142845Sobrien				gethostname(&ps[i], PROMPTLEN - i);
1760142845Sobrien				/* Skip to end of hostname. */
1761142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1762142845Sobrien				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1763142845Sobrien					i++;
1764142845Sobrien				break;
1765142845Sobrien
1766142845Sobrien				/*
1767142845Sobrien				 * Working directory.
1768142845Sobrien				 *
1769142845Sobrien				 * \W specifies just the final component,
1770142845Sobrien				 * \w specifies the entire path.
1771142845Sobrien				 */
1772142845Sobrien			case 'W':
1773142845Sobrien			case 'w':
1774209653Sjilles				pwd = lookupvar("PWD");
1775209653Sjilles				if (pwd == NULL)
1776209653Sjilles					pwd = "?";
1777209653Sjilles				if (*fmt == 'W' &&
1778209653Sjilles				    *pwd == '/' && pwd[1] != '\0')
1779209653Sjilles					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1780209653Sjilles					    PROMPTLEN - i);
1781209653Sjilles				else
1782209653Sjilles					strlcpy(&ps[i], pwd, PROMPTLEN - i);
1783142845Sobrien				/* Skip to end of path. */
1784142845Sobrien				while (ps[i + 1] != '\0')
1785142845Sobrien					i++;
1786142845Sobrien				break;
1787142845Sobrien
1788142845Sobrien				/*
1789142845Sobrien				 * Superuser status.
1790142845Sobrien				 *
1791142845Sobrien				 * '$' for normal users, '#' for root.
1792142845Sobrien				 */
1793142845Sobrien			case '$':
1794142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
1795142845Sobrien				break;
1796142845Sobrien
1797142845Sobrien				/*
1798142845Sobrien				 * A literal \.
1799142845Sobrien				 */
1800142845Sobrien			case '\\':
1801142845Sobrien				ps[i] = '\\';
1802142845Sobrien				break;
1803142845Sobrien
1804142845Sobrien				/*
1805142845Sobrien				 * Emit unrecognized formats verbatim.
1806142845Sobrien				 */
1807142845Sobrien			default:
1808142845Sobrien				ps[i++] = '\\';
1809142845Sobrien				ps[i] = *fmt;
1810142845Sobrien				break;
1811142845Sobrien			}
1812142845Sobrien		else
1813142845Sobrien			ps[i] = *fmt;
1814142845Sobrien	ps[i] = '\0';
1815142845Sobrien	return (ps);
18161556Srgrimes}
1817