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$");
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"
60214304Sjilles#include "exec.h"	/* to check for special builtins */
6117987Speter#ifndef NO_HISTORY
621556Srgrimes#include "myhistedit.h"
6317987Speter#endif
641556Srgrimes
651556Srgrimes/*
661556Srgrimes * Shell command parser.
671556Srgrimes */
681556Srgrimes
69142845Sobrien#define	EOFMARKLEN	79
70142845Sobrien#define	PROMPTLEN	128
711556Srgrimes
72214709Sjilles/* values of checkkwd variable */
73214709Sjilles#define CHKALIAS	0x1
74214709Sjilles#define CHKKWD		0x2
75214709Sjilles#define CHKNL		0x4
76214709Sjilles
771556Srgrimes/* values returned by readtoken */
7817987Speter#include "token.h"
791556Srgrimes
801556Srgrimes
811556Srgrimes
821556Srgrimesstruct heredoc {
831556Srgrimes	struct heredoc *next;	/* next here document in list */
841556Srgrimes	union node *here;		/* redirection node */
851556Srgrimes	char *eofmark;		/* string indicating end of input */
861556Srgrimes	int striptabs;		/* if set, strip leading tabs */
871556Srgrimes};
881556Srgrimes
89206145Sjillesstruct parser_temp {
90206145Sjilles	struct parser_temp *next;
91206145Sjilles	void *data;
92206145Sjilles};
931556Srgrimes
941556Srgrimes
95213760Sobrienstatic struct heredoc *heredoclist;	/* list of here documents to read */
96213760Sobrienstatic int doprompt;		/* if set, prompt the user */
97213760Sobrienstatic int needprompt;		/* true if interactive and at start of line */
98213760Sobrienstatic int lasttoken;		/* last token read */
99255068Sjillesstatic int tokpushback;		/* last token pushed back */
100213760Sobrienstatic char *wordtext;		/* text of last word returned by readtoken */
101253659Sjillesstatic int checkkwd;
102213760Sobrienstatic struct nodelist *backquotelist;
103213760Sobrienstatic union node *redirnode;
104213760Sobrienstatic struct heredoc *heredoc;
105213760Sobrienstatic int quoteflag;		/* set if (part of) last token was quoted */
106213760Sobrienstatic int startlinno;		/* line # where last token started */
107213760Sobrienstatic int funclinno;		/* line # where the current function started */
108213760Sobrienstatic struct parser_temp *parser_temp;
1091556Srgrimes
1101556Srgrimes
111255087Sjillesstatic union node *list(int);
112213811Sobrienstatic union node *andor(void);
113213811Sobrienstatic union node *pipeline(void);
114213811Sobrienstatic union node *command(void);
115213811Sobrienstatic union node *simplecmd(union node **, union node *);
116213811Sobrienstatic union node *makename(void);
117255085Sjillesstatic union node *makebinary(int type, union node *n1, union node *n2);
118213811Sobrienstatic void parsefname(void);
119213811Sobrienstatic void parseheredoc(void);
120213811Sobrienstatic int peektoken(void);
121213811Sobrienstatic int readtoken(void);
122213811Sobrienstatic int xxreadtoken(void);
123248980Sjillesstatic int readtoken1(int, const char *, const char *, int);
124213811Sobrienstatic int noexpand(char *);
125255073Sjillesstatic void consumetoken(int);
126213811Sobrienstatic void synexpect(int) __dead2;
127213811Sobrienstatic void synerror(const char *) __dead2;
128213811Sobrienstatic void setprompt(int);
1291556Srgrimes
13017987Speter
131213811Sobrienstatic void *
132206145Sjillesparser_temp_alloc(size_t len)
133206145Sjilles{
134206145Sjilles	struct parser_temp *t;
135206145Sjilles
136206145Sjilles	INTOFF;
137206145Sjilles	t = ckmalloc(sizeof(*t));
138206145Sjilles	t->data = NULL;
139206145Sjilles	t->next = parser_temp;
140206145Sjilles	parser_temp = t;
141206145Sjilles	t->data = ckmalloc(len);
142206145Sjilles	INTON;
143206145Sjilles	return t->data;
144206145Sjilles}
145206145Sjilles
146206145Sjilles
147213811Sobrienstatic void *
148206145Sjillesparser_temp_realloc(void *ptr, size_t len)
149206145Sjilles{
150206145Sjilles	struct parser_temp *t;
151206145Sjilles
152206145Sjilles	INTOFF;
153206145Sjilles	t = parser_temp;
154206145Sjilles	if (ptr != t->data)
155206145Sjilles		error("bug: parser_temp_realloc misused");
156206145Sjilles	t->data = ckrealloc(t->data, len);
157206145Sjilles	INTON;
158206145Sjilles	return t->data;
159206145Sjilles}
160206145Sjilles
161206145Sjilles
162213811Sobrienstatic void
163206145Sjillesparser_temp_free_upto(void *ptr)
164206145Sjilles{
165206145Sjilles	struct parser_temp *t;
166206145Sjilles	int done = 0;
167206145Sjilles
168206145Sjilles	INTOFF;
169206145Sjilles	while (parser_temp != NULL && !done) {
170206145Sjilles		t = parser_temp;
171206145Sjilles		parser_temp = t->next;
172206145Sjilles		done = t->data == ptr;
173206145Sjilles		ckfree(t->data);
174206145Sjilles		ckfree(t);
175206145Sjilles	}
176206145Sjilles	INTON;
177206145Sjilles	if (!done)
178206145Sjilles		error("bug: parser_temp_free_upto misused");
179206145Sjilles}
180206145Sjilles
181206145Sjilles
182213811Sobrienstatic void
183206145Sjillesparser_temp_free_all(void)
184206145Sjilles{
185206145Sjilles	struct parser_temp *t;
186206145Sjilles
187206145Sjilles	INTOFF;
188206145Sjilles	while (parser_temp != NULL) {
189206145Sjilles		t = parser_temp;
190206145Sjilles		parser_temp = t->next;
191206145Sjilles		ckfree(t->data);
192206145Sjilles		ckfree(t);
193206145Sjilles	}
194206145Sjilles	INTON;
195206145Sjilles}
196206145Sjilles
197206145Sjilles
1981556Srgrimes/*
1991556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
2001556Srgrimes * valid parse tree indicating a blank line.)
2011556Srgrimes */
2021556Srgrimes
2031556Srgrimesunion node *
20490111Simpparsecmd(int interact)
20517987Speter{
2061556Srgrimes	int t;
2071556Srgrimes
208206145Sjilles	/* This assumes the parser is not re-entered,
209206145Sjilles	 * which could happen if we add command substitution on PS1/PS2.
210206145Sjilles	 */
211206145Sjilles	parser_temp_free_all();
212208656Sjilles	heredoclist = NULL;
213206145Sjilles
21460593Scracauer	tokpushback = 0;
215254426Sjilles	checkkwd = 0;
2161556Srgrimes	doprompt = interact;
2171556Srgrimes	if (doprompt)
2181556Srgrimes		setprompt(1);
2191556Srgrimes	else
2201556Srgrimes		setprompt(0);
2211556Srgrimes	needprompt = 0;
2221556Srgrimes	t = readtoken();
2231556Srgrimes	if (t == TEOF)
2241556Srgrimes		return NEOF;
2251556Srgrimes	if (t == TNL)
2261556Srgrimes		return NULL;
2271556Srgrimes	tokpushback++;
228255087Sjilles	return list(1);
2291556Srgrimes}
2301556Srgrimes
2311556Srgrimes
232213811Sobrienstatic union node *
233255087Sjilleslist(int nlflag)
23417987Speter{
235214599Sjilles	union node *ntop, *n1, *n2, *n3;
23617987Speter	int tok;
2371556Srgrimes
238214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
239255087Sjilles	if (!nlflag && tokendlist[peektoken()])
2401556Srgrimes		return NULL;
241214599Sjilles	ntop = n1 = NULL;
2421556Srgrimes	for (;;) {
24317987Speter		n2 = andor();
24417987Speter		tok = readtoken();
24517987Speter		if (tok == TBACKGND) {
246245382Sjilles			if (n2 != NULL && n2->type == NPIPE) {
247223282Sjilles				n2->npipe.backgnd = 1;
248245382Sjilles			} else if (n2 != NULL && n2->type == NREDIR) {
24917987Speter				n2->type = NBACKGND;
25017987Speter			} else {
25117987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
25217987Speter				n3->type = NBACKGND;
25317987Speter				n3->nredir.n = n2;
25417987Speter				n3->nredir.redirect = NULL;
25517987Speter				n2 = n3;
25617987Speter			}
25717987Speter		}
258214599Sjilles		if (ntop == NULL)
259214599Sjilles			ntop = n2;
260214599Sjilles		else if (n1 == NULL) {
261255085Sjilles			n1 = makebinary(NSEMI, ntop, n2);
262214599Sjilles			ntop = n1;
26317987Speter		}
26417987Speter		else {
265255085Sjilles			n3 = makebinary(NSEMI, n1->nbinary.ch2, n2);
266214599Sjilles			n1->nbinary.ch2 = n3;
26717987Speter			n1 = n3;
26817987Speter		}
26917987Speter		switch (tok) {
27013882Sjoerg		case TBACKGND:
27117987Speter		case TSEMI:
27217987Speter			tok = readtoken();
273102410Scharnier			/* FALLTHROUGH */
2741556Srgrimes		case TNL:
27517987Speter			if (tok == TNL) {
27617987Speter				parseheredoc();
27717987Speter				if (nlflag)
278214599Sjilles					return ntop;
279210488Sjilles			} else if (tok == TEOF && nlflag) {
280210488Sjilles				parseheredoc();
281214599Sjilles				return ntop;
28217987Speter			} else {
28317987Speter				tokpushback++;
28417987Speter			}
285214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
286255087Sjilles			if (!nlflag && tokendlist[peektoken()])
287214599Sjilles				return ntop;
2881556Srgrimes			break;
2891556Srgrimes		case TEOF:
2901556Srgrimes			if (heredoclist)
2911556Srgrimes				parseheredoc();
2921556Srgrimes			else
2931556Srgrimes				pungetc();		/* push back EOF on input */
294214599Sjilles			return ntop;
2951556Srgrimes		default:
296255087Sjilles			if (nlflag)
2971556Srgrimes				synexpect(-1);
2981556Srgrimes			tokpushback++;
299214599Sjilles			return ntop;
3001556Srgrimes		}
3011556Srgrimes	}
3021556Srgrimes}
3031556Srgrimes
3041556Srgrimes
3051556Srgrimes
306213811Sobrienstatic union node *
30790111Simpandor(void)
30890111Simp{
309255085Sjilles	union node *n;
3101556Srgrimes	int t;
3111556Srgrimes
312255085Sjilles	n = pipeline();
3131556Srgrimes	for (;;) {
3141556Srgrimes		if ((t = readtoken()) == TAND) {
3151556Srgrimes			t = NAND;
3161556Srgrimes		} else if (t == TOR) {
3171556Srgrimes			t = NOR;
3181556Srgrimes		} else {
3191556Srgrimes			tokpushback++;
320255085Sjilles			return n;
3211556Srgrimes		}
322255085Sjilles		n = makebinary(t, n, pipeline());
3231556Srgrimes	}
3241556Srgrimes}
3251556Srgrimes
3261556Srgrimes
3271556Srgrimes
328213811Sobrienstatic union node *
32990111Simppipeline(void)
33090111Simp{
33175336Sbrian	union node *n1, *n2, *pipenode;
3321556Srgrimes	struct nodelist *lp, *prev;
333214281Sjilles	int negate, t;
3341556Srgrimes
33575336Sbrian	negate = 0;
336214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
3371556Srgrimes	TRACE(("pipeline: entered\n"));
33875336Sbrian	while (readtoken() == TNOT)
33975336Sbrian		negate = !negate;
34075336Sbrian	tokpushback++;
3411556Srgrimes	n1 = command();
3421556Srgrimes	if (readtoken() == TPIPE) {
3431556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3441556Srgrimes		pipenode->type = NPIPE;
3451556Srgrimes		pipenode->npipe.backgnd = 0;
3461556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3471556Srgrimes		pipenode->npipe.cmdlist = lp;
3481556Srgrimes		lp->n = n1;
3491556Srgrimes		do {
3501556Srgrimes			prev = lp;
3511556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
352214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
353214281Sjilles			t = readtoken();
354214281Sjilles			tokpushback++;
355214281Sjilles			if (t == TNOT)
356214281Sjilles				lp->n = pipeline();
357214281Sjilles			else
358214281Sjilles				lp->n = command();
3591556Srgrimes			prev->next = lp;
3601556Srgrimes		} while (readtoken() == TPIPE);
3611556Srgrimes		lp->next = NULL;
3621556Srgrimes		n1 = pipenode;
3631556Srgrimes	}
3641556Srgrimes	tokpushback++;
36575336Sbrian	if (negate) {
36675336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
36775336Sbrian		n2->type = NNOT;
36875336Sbrian		n2->nnot.com = n1;
36975336Sbrian		return n2;
37075336Sbrian	} else
37175336Sbrian		return n1;
3721556Srgrimes}
3731556Srgrimes
3741556Srgrimes
3751556Srgrimes
376213811Sobrienstatic union node *
37790111Simpcommand(void)
37890111Simp{
3791556Srgrimes	union node *n1, *n2;
3801556Srgrimes	union node *ap, **app;
3811556Srgrimes	union node *cp, **cpp;
3821556Srgrimes	union node *redir, **rpp;
383214281Sjilles	int t;
384218325Sjilles	int is_subshell;
3851556Srgrimes
386214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
387218325Sjilles	is_subshell = 0;
38817987Speter	redir = NULL;
38917987Speter	n1 = NULL;
3901556Srgrimes	rpp = &redir;
39120425Ssteve
3921556Srgrimes	/* Check for redirection which may precede command */
3931556Srgrimes	while (readtoken() == TREDIR) {
3941556Srgrimes		*rpp = n2 = redirnode;
3951556Srgrimes		rpp = &n2->nfile.next;
3961556Srgrimes		parsefname();
3971556Srgrimes	}
3981556Srgrimes	tokpushback++;
3991556Srgrimes
4001556Srgrimes	switch (readtoken()) {
4011556Srgrimes	case TIF:
4021556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
4031556Srgrimes		n1->type = NIF;
404255087Sjilles		if ((n1->nif.test = list(0)) == NULL)
405104554Stjr			synexpect(-1);
406255073Sjilles		consumetoken(TTHEN);
407255087Sjilles		n1->nif.ifpart = list(0);
4081556Srgrimes		n2 = n1;
4091556Srgrimes		while (readtoken() == TELIF) {
4101556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4111556Srgrimes			n2 = n2->nif.elsepart;
4121556Srgrimes			n2->type = NIF;
413255087Sjilles			if ((n2->nif.test = list(0)) == NULL)
414104554Stjr				synexpect(-1);
415255073Sjilles			consumetoken(TTHEN);
416255087Sjilles			n2->nif.ifpart = list(0);
4171556Srgrimes		}
4181556Srgrimes		if (lasttoken == TELSE)
419255087Sjilles			n2->nif.elsepart = list(0);
4201556Srgrimes		else {
4211556Srgrimes			n2->nif.elsepart = NULL;
4221556Srgrimes			tokpushback++;
4231556Srgrimes		}
424255073Sjilles		consumetoken(TFI);
425214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4261556Srgrimes		break;
4271556Srgrimes	case TWHILE:
428255073Sjilles	case TUNTIL:
429255085Sjilles		t = lasttoken;
430255087Sjilles		if ((n1 = list(0)) == NULL)
431104554Stjr			synexpect(-1);
432255073Sjilles		consumetoken(TDO);
433255087Sjilles		n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0));
434255073Sjilles		consumetoken(TDONE);
435214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4361556Srgrimes		break;
4371556Srgrimes	case TFOR:
4381556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4391556Srgrimes			synerror("Bad for loop variable");
4401556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4411556Srgrimes		n1->type = NFOR;
4421556Srgrimes		n1->nfor.var = wordtext;
443199282Sjilles		while (readtoken() == TNL)
444199282Sjilles			;
445199282Sjilles		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4461556Srgrimes			app = &ap;
4471556Srgrimes			while (readtoken() == TWORD) {
448255081Sjilles				n2 = makename();
4491556Srgrimes				*app = n2;
4501556Srgrimes				app = &n2->narg.next;
4511556Srgrimes			}
4521556Srgrimes			*app = NULL;
4531556Srgrimes			n1->nfor.args = ap;
4541556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4551556Srgrimes				synexpect(-1);
4561556Srgrimes		} else {
457149096Sstefanf			static char argvars[5] = {
458149096Sstefanf				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
459149096Sstefanf			};
4601556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4611556Srgrimes			n2->type = NARG;
462149096Sstefanf			n2->narg.text = argvars;
4631556Srgrimes			n2->narg.backquote = NULL;
4641556Srgrimes			n2->narg.next = NULL;
4651556Srgrimes			n1->nfor.args = n2;
4661556Srgrimes			/*
4671556Srgrimes			 * Newline or semicolon here is optional (but note
4681556Srgrimes			 * that the original Bourne shell only allowed NL).
4691556Srgrimes			 */
4701556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4711556Srgrimes				tokpushback++;
4721556Srgrimes		}
473214709Sjilles		checkkwd = CHKNL | CHKKWD | CHKALIAS;
4741556Srgrimes		if ((t = readtoken()) == TDO)
4751556Srgrimes			t = TDONE;
4761556Srgrimes		else if (t == TBEGIN)
4771556Srgrimes			t = TEND;
4781556Srgrimes		else
4791556Srgrimes			synexpect(-1);
480255087Sjilles		n1->nfor.body = list(0);
481255073Sjilles		consumetoken(t);
482214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4831556Srgrimes		break;
4841556Srgrimes	case TCASE:
4851556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4861556Srgrimes		n1->type = NCASE;
487255073Sjilles		consumetoken(TWORD);
488255081Sjilles		n1->ncase.expr = makename();
4891556Srgrimes		while (readtoken() == TNL);
4901556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
4911556Srgrimes			synerror("expecting \"in\"");
4921556Srgrimes		cpp = &n1->ncase.cases;
493214709Sjilles		checkkwd = CHKNL | CHKKWD, readtoken();
494104202Stjr		while (lasttoken != TESAC) {
4951556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
4961556Srgrimes			cp->type = NCLIST;
4971556Srgrimes			app = &cp->nclist.pattern;
498104207Stjr			if (lasttoken == TLP)
499104207Stjr				readtoken();
5001556Srgrimes			for (;;) {
501255081Sjilles				*app = ap = makename();
502214709Sjilles				checkkwd = CHKNL | CHKKWD;
503214709Sjilles				if (readtoken() != TPIPE)
5041556Srgrimes					break;
5051556Srgrimes				app = &ap->narg.next;
5062760Ssef				readtoken();
5071556Srgrimes			}
5081556Srgrimes			ap->narg.next = NULL;
5091556Srgrimes			if (lasttoken != TRP)
510214709Sjilles				synexpect(TRP);
511255087Sjilles			cp->nclist.body = list(0);
5122760Ssef
513214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
5142760Ssef			if ((t = readtoken()) != TESAC) {
515223186Sjilles				if (t == TENDCASE)
516223186Sjilles					;
517223186Sjilles				else if (t == TFALLTHRU)
518223186Sjilles					cp->type = NCLISTFALLTHRU;
519223186Sjilles				else
520214709Sjilles					synexpect(TENDCASE);
521223186Sjilles				checkkwd = CHKNL | CHKKWD, readtoken();
5222760Ssef			}
5231556Srgrimes			cpp = &cp->nclist.next;
524104202Stjr		}
5251556Srgrimes		*cpp = NULL;
526214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5271556Srgrimes		break;
5281556Srgrimes	case TLP:
5291556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5301556Srgrimes		n1->type = NSUBSHELL;
531255087Sjilles		n1->nredir.n = list(0);
5321556Srgrimes		n1->nredir.redirect = NULL;
533255073Sjilles		consumetoken(TRP);
534214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
535218325Sjilles		is_subshell = 1;
5361556Srgrimes		break;
5371556Srgrimes	case TBEGIN:
538255087Sjilles		n1 = list(0);
539255073Sjilles		consumetoken(TEND);
540214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5411556Srgrimes		break;
542254843Sjilles	/* A simple command must have at least one redirection or word. */
543210221Sjilles	case TBACKGND:
54417987Speter	case TSEMI:
545101662Stjr	case TAND:
546101662Stjr	case TOR:
547254335Sjilles	case TPIPE:
548254335Sjilles	case TENDCASE:
549254335Sjilles	case TFALLTHRU:
550254843Sjilles	case TEOF:
551254843Sjilles	case TNL:
552254843Sjilles	case TRP:
55317987Speter		if (!redir)
55417987Speter			synexpect(-1);
5551556Srgrimes	case TWORD:
5561556Srgrimes		tokpushback++;
55775160Sbrian		n1 = simplecmd(rpp, redir);
558214281Sjilles		return n1;
5591556Srgrimes	default:
5601556Srgrimes		synexpect(-1);
5611556Srgrimes	}
5621556Srgrimes
5631556Srgrimes	/* Now check for redirection which may follow command */
5641556Srgrimes	while (readtoken() == TREDIR) {
5651556Srgrimes		*rpp = n2 = redirnode;
5661556Srgrimes		rpp = &n2->nfile.next;
5671556Srgrimes		parsefname();
5681556Srgrimes	}
5691556Srgrimes	tokpushback++;
5701556Srgrimes	*rpp = NULL;
5711556Srgrimes	if (redir) {
572218325Sjilles		if (!is_subshell) {
5731556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5741556Srgrimes			n2->type = NREDIR;
5751556Srgrimes			n2->nredir.n = n1;
5761556Srgrimes			n1 = n2;
5771556Srgrimes		}
5781556Srgrimes		n1->nredir.redirect = redir;
5791556Srgrimes	}
58075160Sbrian
581214281Sjilles	return n1;
5821556Srgrimes}
5831556Srgrimes
5841556Srgrimes
585213811Sobrienstatic union node *
58690111Simpsimplecmd(union node **rpp, union node *redir)
58790111Simp{
5881556Srgrimes	union node *args, **app;
5891556Srgrimes	union node **orig_rpp = rpp;
590210087Sjilles	union node *n = NULL;
591214304Sjilles	int special;
592222165Sjilles	int savecheckkwd;
5931556Srgrimes
5941556Srgrimes	/* If we don't have any redirections already, then we must reset */
5951556Srgrimes	/* rpp to be the address of the local redir variable.  */
5961556Srgrimes	if (redir == 0)
5971556Srgrimes		rpp = &redir;
5981556Srgrimes
5991556Srgrimes	args = NULL;
6001556Srgrimes	app = &args;
6018855Srgrimes	/*
6021556Srgrimes	 * We save the incoming value, because we need this for shell
6031556Srgrimes	 * functions.  There can not be a redirect or an argument between
6048855Srgrimes	 * the function name and the open parenthesis.
6051556Srgrimes	 */
6061556Srgrimes	orig_rpp = rpp;
6071556Srgrimes
608222165Sjilles	savecheckkwd = CHKALIAS;
609222165Sjilles
6101556Srgrimes	for (;;) {
611222165Sjilles		checkkwd = savecheckkwd;
6121556Srgrimes		if (readtoken() == TWORD) {
613255081Sjilles			n = makename();
6141556Srgrimes			*app = n;
6151556Srgrimes			app = &n->narg.next;
616222165Sjilles			if (savecheckkwd != 0 && !isassignment(wordtext))
617222165Sjilles				savecheckkwd = 0;
6181556Srgrimes		} else if (lasttoken == TREDIR) {
6191556Srgrimes			*rpp = n = redirnode;
6201556Srgrimes			rpp = &n->nfile.next;
6211556Srgrimes			parsefname();	/* read name of redirection file */
6221556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6231556Srgrimes					    && rpp == orig_rpp) {
6241556Srgrimes			/* We have a function */
625255073Sjilles			consumetoken(TRP);
626179022Sstefanf			funclinno = plinno;
627214291Sjilles			/*
628214291Sjilles			 * - Require plain text.
629214291Sjilles			 * - Functions with '/' cannot be called.
630214534Sjilles			 * - Reject name=().
631214534Sjilles			 * - Reject ksh extended glob patterns.
632214291Sjilles			 */
633214291Sjilles			if (!noexpand(n->narg.text) || quoteflag ||
634214534Sjilles			    strchr(n->narg.text, '/') ||
635214534Sjilles			    strchr("!%*+-=?@}~",
636214534Sjilles				n->narg.text[strlen(n->narg.text) - 1]))
6371556Srgrimes				synerror("Bad function name");
638214291Sjilles			rmescapes(n->narg.text);
639214304Sjilles			if (find_builtin(n->narg.text, &special) >= 0 &&
640214304Sjilles			    special)
641214304Sjilles				synerror("Cannot override a special builtin with a function");
6421556Srgrimes			n->type = NDEFUN;
6431556Srgrimes			n->narg.next = command();
644179022Sstefanf			funclinno = 0;
645210087Sjilles			return n;
6461556Srgrimes		} else {
6471556Srgrimes			tokpushback++;
6481556Srgrimes			break;
6491556Srgrimes		}
6501556Srgrimes	}
6511556Srgrimes	*app = NULL;
6521556Srgrimes	*rpp = NULL;
6531556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6541556Srgrimes	n->type = NCMD;
6551556Srgrimes	n->ncmd.args = args;
6561556Srgrimes	n->ncmd.redirect = redir;
657210087Sjilles	return n;
6581556Srgrimes}
6591556Srgrimes
660213811Sobrienstatic union node *
66190111Simpmakename(void)
66290111Simp{
66317987Speter	union node *n;
6641556Srgrimes
66517987Speter	n = (union node *)stalloc(sizeof (struct narg));
66617987Speter	n->type = NARG;
66717987Speter	n->narg.next = NULL;
66817987Speter	n->narg.text = wordtext;
66917987Speter	n->narg.backquote = backquotelist;
67017987Speter	return n;
67117987Speter}
67217987Speter
673255085Sjillesstatic union node *
674255085Sjillesmakebinary(int type, union node *n1, union node *n2)
675255085Sjilles{
676255085Sjilles	union node *n;
677255085Sjilles
678255085Sjilles	n = (union node *)stalloc(sizeof (struct nbinary));
679255085Sjilles	n->type = type;
680255085Sjilles	n->nbinary.ch1 = n1;
681255085Sjilles	n->nbinary.ch2 = n2;
682255085Sjilles	return (n);
683255085Sjilles}
684255085Sjilles
685213760Sobrienvoid
686262951Sjmmvforcealias(void)
687262951Sjmmv{
688262951Sjmmv	checkkwd |= CHKALIAS;
689262951Sjmmv}
690262951Sjmmv
691262951Sjmmvvoid
692213760Sobrienfixredir(union node *n, const char *text, int err)
69390111Simp{
69417987Speter	TRACE(("Fix redir %s %d\n", text, err));
69517987Speter	if (!err)
69617987Speter		n->ndup.vname = NULL;
69717987Speter
69817987Speter	if (is_digit(text[0]) && text[1] == '\0')
69917987Speter		n->ndup.dupfd = digit_val(text[0]);
70017987Speter	else if (text[0] == '-' && text[1] == '\0')
70117987Speter		n->ndup.dupfd = -1;
70217987Speter	else {
70320425Ssteve
70417987Speter		if (err)
70517987Speter			synerror("Bad fd number");
70617987Speter		else
70717987Speter			n->ndup.vname = makename();
70817987Speter	}
70917987Speter}
71017987Speter
71117987Speter
712213811Sobrienstatic void
71390111Simpparsefname(void)
71490111Simp{
7151556Srgrimes	union node *n = redirnode;
7161556Srgrimes
717255073Sjilles	consumetoken(TWORD);
7181556Srgrimes	if (n->type == NHERE) {
7191556Srgrimes		struct heredoc *here = heredoc;
7201556Srgrimes		struct heredoc *p;
7211556Srgrimes		int i;
7221556Srgrimes
7231556Srgrimes		if (quoteflag == 0)
7241556Srgrimes			n->type = NXHERE;
7251556Srgrimes		TRACE(("Here document %d\n", n->type));
7261556Srgrimes		if (here->striptabs) {
7271556Srgrimes			while (*wordtext == '\t')
7281556Srgrimes				wordtext++;
7291556Srgrimes		}
7301556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7311556Srgrimes			synerror("Illegal eof marker for << redirection");
7321556Srgrimes		rmescapes(wordtext);
7331556Srgrimes		here->eofmark = wordtext;
7341556Srgrimes		here->next = NULL;
7351556Srgrimes		if (heredoclist == NULL)
7361556Srgrimes			heredoclist = here;
7371556Srgrimes		else {
7381556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
7391556Srgrimes			p->next = here;
7401556Srgrimes		}
7411556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
74217987Speter		fixredir(n, wordtext, 0);
7431556Srgrimes	} else {
74417987Speter		n->nfile.fname = makename();
7451556Srgrimes	}
7461556Srgrimes}
7471556Srgrimes
7481556Srgrimes
7491556Srgrimes/*
7501556Srgrimes * Input any here documents.
7511556Srgrimes */
7521556Srgrimes
753213811Sobrienstatic void
75490111Simpparseheredoc(void)
75590111Simp{
7561556Srgrimes	struct heredoc *here;
7571556Srgrimes	union node *n;
7581556Srgrimes
7591556Srgrimes	while (heredoclist) {
7601556Srgrimes		here = heredoclist;
7611556Srgrimes		heredoclist = here->next;
7621556Srgrimes		if (needprompt) {
7631556Srgrimes			setprompt(2);
7641556Srgrimes			needprompt = 0;
7651556Srgrimes		}
7661556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
7671556Srgrimes				here->eofmark, here->striptabs);
768255081Sjilles		n = makename();
7691556Srgrimes		here->here->nhere.doc = n;
7701556Srgrimes	}
7711556Srgrimes}
7721556Srgrimes
773213811Sobrienstatic int
77490111Simppeektoken(void)
77590111Simp{
7761556Srgrimes	int t;
7771556Srgrimes
7781556Srgrimes	t = readtoken();
7791556Srgrimes	tokpushback++;
7801556Srgrimes	return (t);
7811556Srgrimes}
7821556Srgrimes
783213811Sobrienstatic int
78490111Simpreadtoken(void)
78590111Simp{
7861556Srgrimes	int t;
7871556Srgrimes	struct alias *ap;
7881556Srgrimes#ifdef DEBUG
7891556Srgrimes	int alreadyseen = tokpushback;
7901556Srgrimes#endif
7918855Srgrimes
7921556Srgrimes	top:
7931556Srgrimes	t = xxreadtoken();
7941556Srgrimes
795214709Sjilles	/*
796214709Sjilles	 * eat newlines
797214709Sjilles	 */
798214709Sjilles	if (checkkwd & CHKNL) {
799214709Sjilles		while (t == TNL) {
800214709Sjilles			parseheredoc();
801214709Sjilles			t = xxreadtoken();
802214709Sjilles		}
803214709Sjilles	}
8041556Srgrimes
805214709Sjilles	/*
806214709Sjilles	 * check for keywords and aliases
807214709Sjilles	 */
808214709Sjilles	if (t == TWORD && !quoteflag)
809214709Sjilles	{
810214709Sjilles		const char * const *pp;
811214709Sjilles
812214709Sjilles		if (checkkwd & CHKKWD)
81398463Sjmallett			for (pp = parsekwd; *pp; pp++) {
81420425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
81517987Speter				{
8161556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8171556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8181556Srgrimes					goto out;
8191556Srgrimes				}
8201556Srgrimes			}
821214709Sjilles		if (checkkwd & CHKALIAS &&
822214709Sjilles		    (ap = lookupalias(wordtext, 1)) != NULL) {
823214709Sjilles			pushstring(ap->val, strlen(ap->val), ap);
824214709Sjilles			goto top;
8251556Srgrimes		}
826214709Sjilles	}
8271556Srgrimesout:
828214709Sjilles	if (t != TNOT)
829214709Sjilles		checkkwd = 0;
830214709Sjilles
8311556Srgrimes#ifdef DEBUG
8321556Srgrimes	if (!alreadyseen)
8331556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8341556Srgrimes	else
8351556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8361556Srgrimes#endif
8371556Srgrimes	return (t);
8381556Srgrimes}
8391556Srgrimes
8401556Srgrimes
8411556Srgrimes/*
8421556Srgrimes * Read the next input token.
8431556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8441556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8451556Srgrimes *	quoted.
8461556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8471556Srgrimes *	the redirection.
8481556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8491556Srgrimes *	on which the token starts.
8501556Srgrimes *
8511556Srgrimes * [Change comment:  here documents and internal procedures]
8521556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8531556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8541556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8551556Srgrimes *  We could also make parseoperator in essence the main routine, and
8561556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8571556Srgrimes */
8581556Srgrimes
8591556Srgrimes#define RETURN(token)	return lasttoken = token
8601556Srgrimes
861213811Sobrienstatic int
86290111Simpxxreadtoken(void)
86390111Simp{
86425230Ssteve	int c;
8651556Srgrimes
8661556Srgrimes	if (tokpushback) {
8671556Srgrimes		tokpushback = 0;
8681556Srgrimes		return lasttoken;
8691556Srgrimes	}
8701556Srgrimes	if (needprompt) {
8711556Srgrimes		setprompt(2);
8721556Srgrimes		needprompt = 0;
8731556Srgrimes	}
8741556Srgrimes	startlinno = plinno;
8751556Srgrimes	for (;;) {	/* until token or start of word found */
8761556Srgrimes		c = pgetc_macro();
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 ';':
912223186Sjilles			c = pgetc();
913223186Sjilles			if (c == ';')
9141556Srgrimes				RETURN(TENDCASE);
915223186Sjilles			else if (c == '&')
916223186Sjilles				RETURN(TFALLTHRU);
9171556Srgrimes			pungetc();
9181556Srgrimes			RETURN(TSEMI);
9191556Srgrimes		case '(':
9201556Srgrimes			RETURN(TLP);
9211556Srgrimes		case ')':
9221556Srgrimes			RETURN(TRP);
9231556Srgrimes		default:
9241556Srgrimes			goto breakloop;
9251556Srgrimes		}
9261556Srgrimes	}
9271556Srgrimesbreakloop:
9281556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9291556Srgrimes#undef RETURN
9301556Srgrimes}
9311556Srgrimes
9321556Srgrimes
933213811Sobrien#define MAXNEST_static 8
934206145Sjillesstruct tokenstate
935206145Sjilles{
936206145Sjilles	const char *syntax; /* *SYNTAX */
937206145Sjilles	int parenlevel; /* levels of parentheses in arithmetic */
938206145Sjilles	enum tokenstate_category
939206145Sjilles	{
940206145Sjilles		TSTATE_TOP,
941206145Sjilles		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
942206145Sjilles		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
943206145Sjilles		TSTATE_ARITH
944206145Sjilles	} category;
945206145Sjilles};
946206145Sjilles
947206145Sjilles
948205130Sjilles/*
949205130Sjilles * Called to parse command substitutions.
950205130Sjilles */
9511556Srgrimes
952213811Sobrienstatic char *
953205130Sjillesparsebackq(char *out, struct nodelist **pbqlist,
954205130Sjilles		int oldstyle, int dblquote, int quoted)
955205130Sjilles{
956205130Sjilles	struct nodelist **nlpp;
957205130Sjilles	union node *n;
958205130Sjilles	char *volatile str;
959205130Sjilles	struct jmploc jmploc;
960205130Sjilles	struct jmploc *const savehandler = handler;
961248980Sjilles	size_t savelen;
962205130Sjilles	int saveprompt;
963205130Sjilles	const int bq_startlinno = plinno;
964205130Sjilles	char *volatile ostr = NULL;
965205130Sjilles	struct parsefile *const savetopfile = getcurrentfile();
966208655Sjilles	struct heredoc *const saveheredoclist = heredoclist;
967208655Sjilles	struct heredoc *here;
968205130Sjilles
969205130Sjilles	str = NULL;
970205130Sjilles	if (setjmp(jmploc.loc)) {
971205130Sjilles		popfilesupto(savetopfile);
972205130Sjilles		if (str)
973205130Sjilles			ckfree(str);
974205130Sjilles		if (ostr)
975205130Sjilles			ckfree(ostr);
976208655Sjilles		heredoclist = saveheredoclist;
977205130Sjilles		handler = savehandler;
978205130Sjilles		if (exception == EXERROR) {
979205130Sjilles			startlinno = bq_startlinno;
980205130Sjilles			synerror("Error in command substitution");
981205130Sjilles		}
982205130Sjilles		longjmp(handler->loc, 1);
983205130Sjilles	}
984205130Sjilles	INTOFF;
985205130Sjilles	savelen = out - stackblock();
986205130Sjilles	if (savelen > 0) {
987205130Sjilles		str = ckmalloc(savelen);
988205130Sjilles		memcpy(str, stackblock(), savelen);
989205130Sjilles	}
990205130Sjilles	handler = &jmploc;
991208655Sjilles	heredoclist = NULL;
992205130Sjilles	INTON;
993205130Sjilles        if (oldstyle) {
994205130Sjilles                /* We must read until the closing backquote, giving special
995205130Sjilles                   treatment to some slashes, and then push the string and
996205130Sjilles                   reread it as input, interpreting it normally.  */
997205130Sjilles                char *oout;
998205130Sjilles                int c;
999205130Sjilles                int olen;
1000205130Sjilles
1001205130Sjilles
1002205130Sjilles                STARTSTACKSTR(oout);
1003205130Sjilles		for (;;) {
1004205130Sjilles			if (needprompt) {
1005205130Sjilles				setprompt(2);
1006205130Sjilles				needprompt = 0;
1007205130Sjilles			}
1008215783Sjilles			CHECKSTRSPACE(2, oout);
1009205130Sjilles			switch (c = pgetc()) {
1010205130Sjilles			case '`':
1011205130Sjilles				goto done;
1012205130Sjilles
1013205130Sjilles			case '\\':
1014205130Sjilles                                if ((c = pgetc()) == '\n') {
1015205130Sjilles					plinno++;
1016205130Sjilles					if (doprompt)
1017205130Sjilles						setprompt(2);
1018205130Sjilles					else
1019205130Sjilles						setprompt(0);
1020205130Sjilles					/*
1021205130Sjilles					 * If eating a newline, avoid putting
1022205130Sjilles					 * the newline into the new character
1023215783Sjilles					 * stream (via the USTPUTC after the
1024205130Sjilles					 * switch).
1025205130Sjilles					 */
1026205130Sjilles					continue;
1027205130Sjilles				}
1028205130Sjilles                                if (c != '\\' && c != '`' && c != '$'
1029205130Sjilles                                    && (!dblquote || c != '"'))
1030215783Sjilles                                        USTPUTC('\\', oout);
1031205130Sjilles				break;
1032205130Sjilles
1033205130Sjilles			case '\n':
1034205130Sjilles				plinno++;
1035205130Sjilles				needprompt = doprompt;
1036205130Sjilles				break;
1037205130Sjilles
1038205130Sjilles			case PEOF:
1039205130Sjilles			        startlinno = plinno;
1040205130Sjilles				synerror("EOF in backquote substitution");
1041205130Sjilles 				break;
1042205130Sjilles
1043205130Sjilles			default:
1044205130Sjilles				break;
1045205130Sjilles			}
1046215783Sjilles			USTPUTC(c, oout);
1047205130Sjilles                }
1048205130Sjillesdone:
1049215783Sjilles                USTPUTC('\0', oout);
1050205130Sjilles                olen = oout - stackblock();
1051205130Sjilles		INTOFF;
1052205130Sjilles		ostr = ckmalloc(olen);
1053205130Sjilles		memcpy(ostr, stackblock(), olen);
1054205130Sjilles		setinputstring(ostr, 1);
1055205130Sjilles		INTON;
1056205130Sjilles        }
1057205130Sjilles	nlpp = pbqlist;
1058205130Sjilles	while (*nlpp)
1059205130Sjilles		nlpp = &(*nlpp)->next;
1060205130Sjilles	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1061205130Sjilles	(*nlpp)->next = NULL;
1062205130Sjilles
1063205130Sjilles	if (oldstyle) {
1064205130Sjilles		saveprompt = doprompt;
1065205130Sjilles		doprompt = 0;
1066205130Sjilles	}
1067205130Sjilles
1068255087Sjilles	n = list(0);
1069205130Sjilles
1070255087Sjilles	if (oldstyle) {
1071255087Sjilles		if (peektoken() != TEOF)
1072255087Sjilles			synexpect(-1);
1073205130Sjilles		doprompt = saveprompt;
1074255087Sjilles	} else
1075255073Sjilles		consumetoken(TRP);
1076205130Sjilles
1077205130Sjilles	(*nlpp)->n = n;
1078205130Sjilles        if (oldstyle) {
1079205130Sjilles		/*
1080205130Sjilles		 * Start reading from old file again, ignoring any pushed back
1081205130Sjilles		 * tokens left from the backquote parsing
1082205130Sjilles		 */
1083205130Sjilles                popfile();
1084205130Sjilles		tokpushback = 0;
1085205130Sjilles	}
1086205130Sjilles	STARTSTACKSTR(out);
1087216706Sjilles	CHECKSTRSPACE(savelen + 1, out);
1088208655Sjilles	INTOFF;
1089205130Sjilles	if (str) {
1090205130Sjilles		memcpy(out, str, savelen);
1091205130Sjilles		STADJUST(savelen, out);
1092205130Sjilles		ckfree(str);
1093205130Sjilles		str = NULL;
1094205130Sjilles	}
1095205130Sjilles	if (ostr) {
1096205130Sjilles		ckfree(ostr);
1097205130Sjilles		ostr = NULL;
1098205130Sjilles	}
1099208655Sjilles	here = saveheredoclist;
1100208655Sjilles	if (here != NULL) {
1101208655Sjilles		while (here->next != NULL)
1102208655Sjilles			here = here->next;
1103208655Sjilles		here->next = heredoclist;
1104208655Sjilles		heredoclist = saveheredoclist;
1105208655Sjilles	}
1106205130Sjilles	handler = savehandler;
1107208655Sjilles	INTON;
1108205130Sjilles	if (quoted)
1109205130Sjilles		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1110205130Sjilles	else
1111205130Sjilles		USTPUTC(CTLBACKQ, out);
1112205130Sjilles	return out;
1113205130Sjilles}
1114205130Sjilles
1115205130Sjilles
11161556Srgrimes/*
1117221513Sjilles * Called to parse a backslash escape sequence inside $'...'.
1118221513Sjilles * The backslash has already been read.
1119221513Sjilles */
1120221513Sjillesstatic char *
1121221513Sjillesreadcstyleesc(char *out)
1122221513Sjilles{
1123221513Sjilles	int c, v, i, n;
1124221513Sjilles
1125221513Sjilles	c = pgetc();
1126221513Sjilles	switch (c) {
1127221513Sjilles	case '\0':
1128221513Sjilles		synerror("Unterminated quoted string");
1129221513Sjilles	case '\n':
1130221513Sjilles		plinno++;
1131221513Sjilles		if (doprompt)
1132221513Sjilles			setprompt(2);
1133221513Sjilles		else
1134221513Sjilles			setprompt(0);
1135221513Sjilles		return out;
1136221513Sjilles	case '\\':
1137221513Sjilles	case '\'':
1138221513Sjilles	case '"':
1139221513Sjilles		v = c;
1140221513Sjilles		break;
1141221513Sjilles	case 'a': v = '\a'; break;
1142221513Sjilles	case 'b': v = '\b'; break;
1143221513Sjilles	case 'e': v = '\033'; break;
1144221513Sjilles	case 'f': v = '\f'; break;
1145221513Sjilles	case 'n': v = '\n'; break;
1146221513Sjilles	case 'r': v = '\r'; break;
1147221513Sjilles	case 't': v = '\t'; break;
1148221513Sjilles	case 'v': v = '\v'; break;
1149221513Sjilles	case 'x':
1150221513Sjilles		  v = 0;
1151221513Sjilles		  for (;;) {
1152221513Sjilles			  c = pgetc();
1153221513Sjilles			  if (c >= '0' && c <= '9')
1154221513Sjilles				  v = (v << 4) + c - '0';
1155221513Sjilles			  else if (c >= 'A' && c <= 'F')
1156221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1157221513Sjilles			  else if (c >= 'a' && c <= 'f')
1158221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1159221513Sjilles			  else
1160221513Sjilles				  break;
1161221513Sjilles		  }
1162221513Sjilles		  pungetc();
1163221513Sjilles		  break;
1164221513Sjilles	case '0': case '1': case '2': case '3':
1165221513Sjilles	case '4': case '5': case '6': case '7':
1166221513Sjilles		  v = c - '0';
1167221513Sjilles		  c = pgetc();
1168221513Sjilles		  if (c >= '0' && c <= '7') {
1169221513Sjilles			  v <<= 3;
1170221513Sjilles			  v += c - '0';
1171221513Sjilles			  c = pgetc();
1172221513Sjilles			  if (c >= '0' && c <= '7') {
1173221513Sjilles				  v <<= 3;
1174221513Sjilles				  v += c - '0';
1175221513Sjilles			  } else
1176221513Sjilles				  pungetc();
1177221513Sjilles		  } else
1178221513Sjilles			  pungetc();
1179221513Sjilles		  break;
1180221513Sjilles	case 'c':
1181221513Sjilles		  c = pgetc();
1182221513Sjilles		  if (c < 0x3f || c > 0x7a || c == 0x60)
1183221513Sjilles			  synerror("Bad escape sequence");
1184221513Sjilles		  if (c == '\\' && pgetc() != '\\')
1185221513Sjilles			  synerror("Bad escape sequence");
1186221513Sjilles		  if (c == '?')
1187221513Sjilles			  v = 127;
1188221513Sjilles		  else
1189221513Sjilles			  v = c & 0x1f;
1190221513Sjilles		  break;
1191221513Sjilles	case 'u':
1192221513Sjilles	case 'U':
1193221513Sjilles		  n = c == 'U' ? 8 : 4;
1194221513Sjilles		  v = 0;
1195221513Sjilles		  for (i = 0; i < n; i++) {
1196221513Sjilles			  c = pgetc();
1197221513Sjilles			  if (c >= '0' && c <= '9')
1198221513Sjilles				  v = (v << 4) + c - '0';
1199221513Sjilles			  else if (c >= 'A' && c <= 'F')
1200221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1201221513Sjilles			  else if (c >= 'a' && c <= 'f')
1202221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1203221513Sjilles			  else
1204221513Sjilles				  synerror("Bad escape sequence");
1205221513Sjilles		  }
1206221513Sjilles		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1207221513Sjilles			  synerror("Bad escape sequence");
1208221513Sjilles		  /* We really need iconv here. */
1209221669Sjilles		  if (initial_localeisutf8 && v > 127) {
1210221669Sjilles			  CHECKSTRSPACE(4, out);
1211221669Sjilles			  /*
1212221669Sjilles			   * We cannot use wctomb() as the locale may have
1213221669Sjilles			   * changed.
1214221669Sjilles			   */
1215221669Sjilles			  if (v <= 0x7ff) {
1216221669Sjilles				  USTPUTC(0xc0 | v >> 6, out);
1217221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1218221669Sjilles				  return out;
1219221669Sjilles			  } else if (v <= 0xffff) {
1220221669Sjilles				  USTPUTC(0xe0 | v >> 12, out);
1221221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1222221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1223221669Sjilles				  return out;
1224221669Sjilles			  } else if (v <= 0x10ffff) {
1225221669Sjilles				  USTPUTC(0xf0 | v >> 18, out);
1226221669Sjilles				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1227221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1228221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1229221669Sjilles				  return out;
1230221669Sjilles			  }
1231221669Sjilles		  }
1232221513Sjilles		  if (v > 127)
1233221513Sjilles			  v = '?';
1234221513Sjilles		  break;
1235221513Sjilles	default:
1236221513Sjilles		  synerror("Bad escape sequence");
1237221513Sjilles	}
1238221513Sjilles	v = (char)v;
1239221513Sjilles	/*
1240221513Sjilles	 * We can't handle NUL bytes.
1241221513Sjilles	 * POSIX says we should skip till the closing quote.
1242221513Sjilles	 */
1243221513Sjilles	if (v == '\0') {
1244221513Sjilles		while ((c = pgetc()) != '\'') {
1245221513Sjilles			if (c == '\\')
1246221513Sjilles				c = pgetc();
1247221513Sjilles			if (c == PEOF)
1248221513Sjilles				synerror("Unterminated quoted string");
1249221513Sjilles		}
1250221513Sjilles		pungetc();
1251221513Sjilles		return out;
1252221513Sjilles	}
1253221513Sjilles	if (SQSYNTAX[v] == CCTL)
1254221513Sjilles		USTPUTC(CTLESC, out);
1255221513Sjilles	USTPUTC(v, out);
1256221513Sjilles	return out;
1257221513Sjilles}
1258221513Sjilles
1259221513Sjilles
1260221513Sjilles/*
12611556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
12621556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
12631556Srgrimes * word which marks the end of the document and striptabs is true if
12641556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
12651556Srgrimes * is the first character of the input token or document.
12661556Srgrimes *
12671556Srgrimes * Because C does not have internal subroutines, I have simulated them
12681556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
12691556Srgrimes * will run code that appears at the end of readtoken1.
12701556Srgrimes */
12711556Srgrimes
12721556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
12731556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
12741556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
12751556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
12761556Srgrimes
1277213811Sobrienstatic int
1278248980Sjillesreadtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1279248980Sjilles    int striptabs)
128090111Simp{
128117987Speter	int c = firstc;
128217987Speter	char *out;
12831556Srgrimes	int len;
12841556Srgrimes	char line[EOFMARKLEN + 1];
12851556Srgrimes	struct nodelist *bqlist;
12861556Srgrimes	int quotef;
1287206145Sjilles	int newvarnest;
1288206145Sjilles	int level;
128954679Scracauer	int synentry;
1290213811Sobrien	struct tokenstate state_static[MAXNEST_static];
1291213811Sobrien	int maxnest = MAXNEST_static;
1292206145Sjilles	struct tokenstate *state = state_static;
1293221513Sjilles	int sqiscstyle = 0;
12941556Srgrimes
12951556Srgrimes	startlinno = plinno;
12961556Srgrimes	quotef = 0;
12971556Srgrimes	bqlist = NULL;
1298206145Sjilles	newvarnest = 0;
1299206145Sjilles	level = 0;
1300206145Sjilles	state[level].syntax = initialsyntax;
1301206145Sjilles	state[level].parenlevel = 0;
1302206145Sjilles	state[level].category = TSTATE_TOP;
13031556Srgrimes
13041556Srgrimes	STARTSTACKSTR(out);
13051556Srgrimes	loop: {	/* for each line, until end of word */
13061556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
13071556Srgrimes		for (;;) {	/* until end of line or end of word */
1308214512Sjilles			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
130954679Scracauer
1310206145Sjilles			synentry = state[level].syntax[c];
131154679Scracauer
131254679Scracauer			switch(synentry) {
13131556Srgrimes			case CNL:	/* '\n' */
1314206145Sjilles				if (state[level].syntax == BASESYNTAX)
13151556Srgrimes					goto endword;	/* exit outer loop */
13161556Srgrimes				USTPUTC(c, out);
13171556Srgrimes				plinno++;
13181556Srgrimes				if (doprompt)
13191556Srgrimes					setprompt(2);
13201556Srgrimes				else
13211556Srgrimes					setprompt(0);
13221556Srgrimes				c = pgetc();
13231556Srgrimes				goto loop;		/* continue outer loop */
1324221513Sjilles			case CSBACK:
1325221513Sjilles				if (sqiscstyle) {
1326221513Sjilles					out = readcstyleesc(out);
1327221513Sjilles					break;
1328221513Sjilles				}
1329221513Sjilles				/* FALLTHROUGH */
13301556Srgrimes			case CWORD:
13311556Srgrimes				USTPUTC(c, out);
13321556Srgrimes				break;
13331556Srgrimes			case CCTL:
1334206145Sjilles				if (eofmark == NULL || initialsyntax != SQSYNTAX)
13351556Srgrimes					USTPUTC(CTLESC, out);
13361556Srgrimes				USTPUTC(c, out);
13371556Srgrimes				break;
13381556Srgrimes			case CBACK:	/* backslash */
13391556Srgrimes				c = pgetc();
13401556Srgrimes				if (c == PEOF) {
13411556Srgrimes					USTPUTC('\\', out);
13421556Srgrimes					pungetc();
13431556Srgrimes				} else if (c == '\n') {
1344160849Syar					plinno++;
13451556Srgrimes					if (doprompt)
13461556Srgrimes						setprompt(2);
13471556Srgrimes					else
13481556Srgrimes						setprompt(0);
13491556Srgrimes				} else {
1350206145Sjilles					if (state[level].syntax == DQSYNTAX &&
1351206145Sjilles					    c != '\\' && c != '`' && c != '$' &&
1352206145Sjilles					    (c != '"' || (eofmark != NULL &&
1353206145Sjilles						newvarnest == 0)) &&
1354206145Sjilles					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
13551556Srgrimes						USTPUTC('\\', out);
1356214512Sjilles					if ((eofmark == NULL ||
1357214512Sjilles					    newvarnest > 0) &&
1358214512Sjilles					    state[level].syntax == BASESYNTAX)
1359214512Sjilles						USTPUTC(CTLQUOTEMARK, out);
136083675Stegge					if (SQSYNTAX[c] == CCTL)
13611556Srgrimes						USTPUTC(CTLESC, out);
13621556Srgrimes					USTPUTC(c, out);
1363214512Sjilles					if ((eofmark == NULL ||
1364214512Sjilles					    newvarnest > 0) &&
1365214512Sjilles					    state[level].syntax == BASESYNTAX &&
1366214512Sjilles					    state[level].category == TSTATE_VAR_OLD)
1367214512Sjilles						USTPUTC(CTLQUOTEEND, out);
13681556Srgrimes					quotef++;
13691556Srgrimes				}
13701556Srgrimes				break;
13711556Srgrimes			case CSQUOTE:
1372206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1373206145Sjilles				state[level].syntax = SQSYNTAX;
1374221513Sjilles				sqiscstyle = 0;
13751556Srgrimes				break;
13761556Srgrimes			case CDQUOTE:
1377206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1378206145Sjilles				state[level].syntax = DQSYNTAX;
13791556Srgrimes				break;
13801556Srgrimes			case CENDQUOTE:
1381206145Sjilles				if (eofmark != NULL && newvarnest == 0)
13821556Srgrimes					USTPUTC(c, out);
1383206145Sjilles				else {
1384214512Sjilles					if (state[level].category == TSTATE_VAR_OLD)
1385214512Sjilles						USTPUTC(CTLQUOTEEND, out);
1386214305Sjilles					state[level].syntax = BASESYNTAX;
13871556Srgrimes					quotef++;
13881556Srgrimes				}
13891556Srgrimes				break;
13901556Srgrimes			case CVAR:	/* '$' */
13911556Srgrimes				PARSESUB();		/* parse substitution */
13921556Srgrimes				break;
13931556Srgrimes			case CENDVAR:	/* '}' */
1394206145Sjilles				if (level > 0 &&
1395214492Sjilles				    ((state[level].category == TSTATE_VAR_OLD &&
1396214492Sjilles				      state[level].syntax ==
1397214492Sjilles				      state[level - 1].syntax) ||
1398214490Sjilles				    (state[level].category == TSTATE_VAR_NEW &&
1399214490Sjilles				     state[level].syntax == BASESYNTAX))) {
1400214492Sjilles					if (state[level].category == TSTATE_VAR_NEW)
1401206145Sjilles						newvarnest--;
1402206145Sjilles					level--;
14031556Srgrimes					USTPUTC(CTLENDVAR, out);
14041556Srgrimes				} else {
14051556Srgrimes					USTPUTC(c, out);
14061556Srgrimes				}
14071556Srgrimes				break;
14081556Srgrimes			case CLP:	/* '(' in arithmetic */
1409206145Sjilles				state[level].parenlevel++;
14101556Srgrimes				USTPUTC(c, out);
14111556Srgrimes				break;
14121556Srgrimes			case CRP:	/* ')' in arithmetic */
1413206145Sjilles				if (state[level].parenlevel > 0) {
14141556Srgrimes					USTPUTC(c, out);
1415206145Sjilles					--state[level].parenlevel;
14161556Srgrimes				} else {
14171556Srgrimes					if (pgetc() == ')') {
1418206145Sjilles						if (level > 0 &&
1419206145Sjilles						    state[level].category == TSTATE_ARITH) {
1420206145Sjilles							level--;
14211556Srgrimes							USTPUTC(CTLENDARI, out);
14221556Srgrimes						} else
14231556Srgrimes							USTPUTC(')', out);
14241556Srgrimes					} else {
14258855Srgrimes						/*
14261556Srgrimes						 * unbalanced parens
14271556Srgrimes						 *  (don't 2nd guess - no error)
14281556Srgrimes						 */
14291556Srgrimes						pungetc();
14301556Srgrimes						USTPUTC(')', out);
14311556Srgrimes					}
14321556Srgrimes				}
14331556Srgrimes				break;
14341556Srgrimes			case CBQUOTE:	/* '`' */
1435206145Sjilles				out = parsebackq(out, &bqlist, 1,
1436206145Sjilles				    state[level].syntax == DQSYNTAX &&
1437206145Sjilles				    (eofmark == NULL || newvarnest > 0),
1438206145Sjilles				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
14391556Srgrimes				break;
14401556Srgrimes			case CEOF:
14411556Srgrimes				goto endword;		/* exit outer loop */
1442214305Sjilles			case CIGN:
1443214305Sjilles				break;
14441556Srgrimes			default:
1445206145Sjilles				if (level == 0)
14461556Srgrimes					goto endword;	/* exit outer loop */
14471556Srgrimes				USTPUTC(c, out);
14481556Srgrimes			}
14491556Srgrimes			c = pgetc_macro();
14501556Srgrimes		}
14511556Srgrimes	}
14521556Srgrimesendword:
1453206145Sjilles	if (state[level].syntax == ARISYNTAX)
14541556Srgrimes		synerror("Missing '))'");
1455206145Sjilles	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
14561556Srgrimes		synerror("Unterminated quoted string");
1457206145Sjilles	if (state[level].category == TSTATE_VAR_OLD ||
1458206145Sjilles	    state[level].category == TSTATE_VAR_NEW) {
14591556Srgrimes		startlinno = plinno;
14601556Srgrimes		synerror("Missing '}'");
14611556Srgrimes	}
1462206145Sjilles	if (state != state_static)
1463206145Sjilles		parser_temp_free_upto(state);
14641556Srgrimes	USTPUTC('\0', out);
14651556Srgrimes	len = out - stackblock();
14661556Srgrimes	out = stackblock();
14671556Srgrimes	if (eofmark == NULL) {
14681556Srgrimes		if ((c == '>' || c == '<')
14691556Srgrimes		 && quotef == 0
14701556Srgrimes		 && len <= 2
14711556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
14721556Srgrimes			PARSEREDIR();
14731556Srgrimes			return lasttoken = TREDIR;
14741556Srgrimes		} else {
14751556Srgrimes			pungetc();
14761556Srgrimes		}
14771556Srgrimes	}
14781556Srgrimes	quoteflag = quotef;
14791556Srgrimes	backquotelist = bqlist;
14801556Srgrimes	grabstackblock(len);
14811556Srgrimes	wordtext = out;
14821556Srgrimes	return lasttoken = TWORD;
14831556Srgrimes/* end of readtoken routine */
14841556Srgrimes
14851556Srgrimes
14861556Srgrimes/*
14871556Srgrimes * Check to see whether we are at the end of the here document.  When this
14881556Srgrimes * is called, c is set to the first character of the next input line.  If
14891556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
14901556Srgrimes */
14911556Srgrimes
14921556Srgrimescheckend: {
14931556Srgrimes	if (eofmark) {
14941556Srgrimes		if (striptabs) {
14951556Srgrimes			while (c == '\t')
14961556Srgrimes				c = pgetc();
14971556Srgrimes		}
14981556Srgrimes		if (c == *eofmark) {
14991556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
1500248980Sjilles				const char *p, *q;
15011556Srgrimes
15021556Srgrimes				p = line;
15031556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1504222134Sjilles				if ((*p == '\0' || *p == '\n') && *q == '\0') {
15051556Srgrimes					c = PEOF;
1506222134Sjilles					if (*p == '\n') {
1507222134Sjilles						plinno++;
1508222134Sjilles						needprompt = doprompt;
1509222134Sjilles					}
15101556Srgrimes				} else {
15111556Srgrimes					pushstring(line, strlen(line), NULL);
15121556Srgrimes				}
15131556Srgrimes			}
15141556Srgrimes		}
15151556Srgrimes	}
15161556Srgrimes	goto checkend_return;
15171556Srgrimes}
15181556Srgrimes
15191556Srgrimes
15201556Srgrimes/*
15211556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
15221556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
15231556Srgrimes * first character of the redirection operator.
15241556Srgrimes */
15251556Srgrimes
15261556Srgrimesparseredir: {
15271556Srgrimes	char fd = *out;
15281556Srgrimes	union node *np;
15291556Srgrimes
15301556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
15311556Srgrimes	if (c == '>') {
15321556Srgrimes		np->nfile.fd = 1;
15331556Srgrimes		c = pgetc();
15341556Srgrimes		if (c == '>')
15351556Srgrimes			np->type = NAPPEND;
15361556Srgrimes		else if (c == '&')
15371556Srgrimes			np->type = NTOFD;
153896922Stjr		else if (c == '|')
153996922Stjr			np->type = NCLOBBER;
15401556Srgrimes		else {
15411556Srgrimes			np->type = NTO;
15421556Srgrimes			pungetc();
15431556Srgrimes		}
15441556Srgrimes	} else {	/* c == '<' */
15451556Srgrimes		np->nfile.fd = 0;
15461556Srgrimes		c = pgetc();
15471556Srgrimes		if (c == '<') {
15481556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
15491556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
15501556Srgrimes				np->nfile.fd = 0;
15511556Srgrimes			}
15521556Srgrimes			np->type = NHERE;
15531556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
15541556Srgrimes			heredoc->here = np;
15551556Srgrimes			if ((c = pgetc()) == '-') {
15561556Srgrimes				heredoc->striptabs = 1;
15571556Srgrimes			} else {
15581556Srgrimes				heredoc->striptabs = 0;
15591556Srgrimes				pungetc();
15601556Srgrimes			}
15611556Srgrimes		} else if (c == '&')
15621556Srgrimes			np->type = NFROMFD;
156366612Sbrian		else if (c == '>')
156466612Sbrian			np->type = NFROMTO;
15651556Srgrimes		else {
15661556Srgrimes			np->type = NFROM;
15671556Srgrimes			pungetc();
15681556Srgrimes		}
15691556Srgrimes	}
15701556Srgrimes	if (fd != '\0')
15711556Srgrimes		np->nfile.fd = digit_val(fd);
15721556Srgrimes	redirnode = np;
15731556Srgrimes	goto parseredir_return;
15741556Srgrimes}
15751556Srgrimes
15761556Srgrimes
15771556Srgrimes/*
15781556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
15791556Srgrimes * and nothing else.
15801556Srgrimes */
15811556Srgrimes
15821556Srgrimesparsesub: {
1583179022Sstefanf	char buf[10];
15841556Srgrimes	int subtype;
15851556Srgrimes	int typeloc;
15861556Srgrimes	int flags;
15871556Srgrimes	char *p;
15881556Srgrimes	static const char types[] = "}-+?=";
1589179022Sstefanf	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1590179022Sstefanf	int linno;
1591179387Sstefanf	int length;
1592219623Sjilles	int c1;
15931556Srgrimes
15941556Srgrimes	c = pgetc();
1595221513Sjilles	if (c == '(') {	/* $(command) or $((arith)) */
15961556Srgrimes		if (pgetc() == '(') {
15971556Srgrimes			PARSEARITH();
15981556Srgrimes		} else {
15991556Srgrimes			pungetc();
1600206145Sjilles			out = parsebackq(out, &bqlist, 0,
1601206145Sjilles			    state[level].syntax == DQSYNTAX &&
1602206145Sjilles			    (eofmark == NULL || newvarnest > 0),
1603206145Sjilles			    state[level].syntax == DQSYNTAX ||
1604206145Sjilles			    state[level].syntax == ARISYNTAX);
16051556Srgrimes		}
1606221513Sjilles	} else if (c == '{' || is_name(c) || is_special(c)) {
16071556Srgrimes		USTPUTC(CTLVAR, out);
16081556Srgrimes		typeloc = out - stackblock();
16091556Srgrimes		USTPUTC(VSNORMAL, out);
16101556Srgrimes		subtype = VSNORMAL;
1611179022Sstefanf		flags = 0;
16121556Srgrimes		if (c == '{') {
161318202Speter			bracketed_name = 1;
16141556Srgrimes			c = pgetc();
1615219623Sjilles			subtype = 0;
16161556Srgrimes		}
1617219623Sjillesvarname:
1618149026Sstefanf		if (!is_eof(c) && is_name(c)) {
1619179387Sstefanf			length = 0;
16201556Srgrimes			do {
16211556Srgrimes				STPUTC(c, out);
16221556Srgrimes				c = pgetc();
1623179387Sstefanf				length++;
1624149026Sstefanf			} while (!is_eof(c) && is_in_name(c));
1625179387Sstefanf			if (length == 6 &&
1626179387Sstefanf			    strncmp(out - length, "LINENO", length) == 0) {
1627179022Sstefanf				/* Replace the variable name with the
1628179022Sstefanf				 * current line number. */
1629179022Sstefanf				linno = plinno;
1630179022Sstefanf				if (funclinno != 0)
1631179022Sstefanf					linno -= funclinno - 1;
1632179022Sstefanf				snprintf(buf, sizeof(buf), "%d", linno);
1633179022Sstefanf				STADJUST(-6, out);
1634215783Sjilles				STPUTS(buf, out);
1635179022Sstefanf				flags |= VSLINENO;
1636179022Sstefanf			}
163718202Speter		} else if (is_digit(c)) {
163818202Speter			if (bracketed_name) {
163918202Speter				do {
164018202Speter					STPUTC(c, out);
164118202Speter					c = pgetc();
164218202Speter				} while (is_digit(c));
164318202Speter			} else {
164418202Speter				STPUTC(c, out);
164518202Speter				c = pgetc();
164618202Speter			}
1647219623Sjilles		} else if (is_special(c)) {
1648219623Sjilles			c1 = c;
1649219623Sjilles			c = pgetc();
1650219623Sjilles			if (subtype == 0 && c1 == '#') {
1651219623Sjilles				subtype = VSLENGTH;
1652219623Sjilles				if (strchr(types, c) == NULL && c != ':' &&
1653219623Sjilles				    c != '#' && c != '%')
1654219623Sjilles					goto varname;
1655219623Sjilles				c1 = c;
1656219623Sjilles				c = pgetc();
1657219623Sjilles				if (c1 != '}' && c == '}') {
1658219623Sjilles					pungetc();
1659219623Sjilles					c = c1;
1660219623Sjilles					goto varname;
1661219623Sjilles				}
1662219623Sjilles				pungetc();
1663219623Sjilles				c = c1;
1664219623Sjilles				c1 = '#';
1665219623Sjilles				subtype = 0;
1666219623Sjilles			}
1667219623Sjilles			USTPUTC(c1, out);
16681556Srgrimes		} else {
1669219623Sjilles			subtype = VSERROR;
1670219623Sjilles			if (c == '}')
1671219623Sjilles				pungetc();
1672219623Sjilles			else if (c == '\n' || c == PEOF)
1673219623Sjilles				synerror("Unexpected end of line in substitution");
1674219623Sjilles			else
1675164003Sstefanf				USTPUTC(c, out);
16761556Srgrimes		}
16771556Srgrimes		if (subtype == 0) {
167817987Speter			switch (c) {
167917987Speter			case ':':
1680179022Sstefanf				flags |= VSNUL;
16811556Srgrimes				c = pgetc();
168217987Speter				/*FALLTHROUGH*/
168317987Speter			default:
168417987Speter				p = strchr(types, c);
1685164003Sstefanf				if (p == NULL) {
1686206144Sjilles					if (c == '\n' || c == PEOF)
1687206144Sjilles						synerror("Unexpected end of line in substitution");
1688164003Sstefanf					if (flags == VSNUL)
1689164003Sstefanf						STPUTC(':', out);
1690164003Sstefanf					STPUTC(c, out);
1691164003Sstefanf					subtype = VSERROR;
1692164003Sstefanf				} else
1693164003Sstefanf					subtype = p - types + VSNORMAL;
169417987Speter				break;
169517987Speter			case '%':
169620425Ssteve			case '#':
169717987Speter				{
169817987Speter					int cc = c;
169917987Speter					subtype = c == '#' ? VSTRIMLEFT :
170017987Speter							     VSTRIMRIGHT;
170117987Speter					c = pgetc();
170217987Speter					if (c == cc)
170317987Speter						subtype++;
170417987Speter					else
170517987Speter						pungetc();
170617987Speter					break;
170717987Speter				}
17081556Srgrimes			}
1709164003Sstefanf		} else if (subtype != VSERROR) {
1710221461Sjilles			if (subtype == VSLENGTH && c != '}')
1711221461Sjilles				subtype = VSERROR;
17121556Srgrimes			pungetc();
17131556Srgrimes		}
1714164003Sstefanf		STPUTC('=', out);
1715220903Sjilles		if (state[level].syntax == DQSYNTAX ||
1716220903Sjilles		    state[level].syntax == ARISYNTAX)
17171556Srgrimes			flags |= VSQUOTE;
17181556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
1719206145Sjilles		if (subtype != VSNORMAL) {
1720206145Sjilles			if (level + 1 >= maxnest) {
1721206145Sjilles				maxnest *= 2;
1722206145Sjilles				if (state == state_static) {
1723206145Sjilles					state = parser_temp_alloc(
1724206145Sjilles					    maxnest * sizeof(*state));
1725206145Sjilles					memcpy(state, state_static,
1726213811Sobrien					    MAXNEST_static * sizeof(*state));
1727206145Sjilles				} else
1728206145Sjilles					state = parser_temp_realloc(state,
1729206145Sjilles					    maxnest * sizeof(*state));
1730206145Sjilles			}
1731206145Sjilles			level++;
1732206145Sjilles			state[level].parenlevel = 0;
1733206145Sjilles			if (subtype == VSMINUS || subtype == VSPLUS ||
1734206145Sjilles			    subtype == VSQUESTION || subtype == VSASSIGN) {
1735206145Sjilles				/*
1736206145Sjilles				 * For operators that were in the Bourne shell,
1737206145Sjilles				 * inherit the double-quote state.
1738206145Sjilles				 */
1739206145Sjilles				state[level].syntax = state[level - 1].syntax;
1740206145Sjilles				state[level].category = TSTATE_VAR_OLD;
1741206145Sjilles			} else {
1742206145Sjilles				/*
1743206145Sjilles				 * The other operators take a pattern,
1744206145Sjilles				 * so go to BASESYNTAX.
1745206145Sjilles				 * Also, ' and " are now special, even
1746206145Sjilles				 * in here documents.
1747206145Sjilles				 */
1748206145Sjilles				state[level].syntax = BASESYNTAX;
1749206145Sjilles				state[level].category = TSTATE_VAR_NEW;
1750206145Sjilles				newvarnest++;
1751206145Sjilles			}
1752206145Sjilles		}
1753221513Sjilles	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1754221513Sjilles		/* $'cstylequotes' */
1755221513Sjilles		USTPUTC(CTLQUOTEMARK, out);
1756221513Sjilles		state[level].syntax = SQSYNTAX;
1757221513Sjilles		sqiscstyle = 1;
1758221513Sjilles	} else {
1759221513Sjilles		USTPUTC('$', out);
1760221513Sjilles		pungetc();
17611556Srgrimes	}
17621556Srgrimes	goto parsesub_return;
17631556Srgrimes}
17641556Srgrimes
17651556Srgrimes
17661556Srgrimes/*
17671556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
17681556Srgrimes */
17691556Srgrimesparsearith: {
17701556Srgrimes
1771206145Sjilles	if (level + 1 >= maxnest) {
1772206145Sjilles		maxnest *= 2;
1773206145Sjilles		if (state == state_static) {
1774206145Sjilles			state = parser_temp_alloc(
1775206145Sjilles			    maxnest * sizeof(*state));
1776206145Sjilles			memcpy(state, state_static,
1777213811Sobrien			    MAXNEST_static * sizeof(*state));
1778206145Sjilles		} else
1779206145Sjilles			state = parser_temp_realloc(state,
1780206145Sjilles			    maxnest * sizeof(*state));
17811556Srgrimes	}
1782206145Sjilles	level++;
1783206145Sjilles	state[level].syntax = ARISYNTAX;
1784206145Sjilles	state[level].parenlevel = 0;
1785206145Sjilles	state[level].category = TSTATE_ARITH;
1786206145Sjilles	USTPUTC(CTLARI, out);
1787206145Sjilles	if (state[level - 1].syntax == DQSYNTAX)
1788206145Sjilles		USTPUTC('"',out);
1789206145Sjilles	else
1790206145Sjilles		USTPUTC(' ',out);
17911556Srgrimes	goto parsearith_return;
17921556Srgrimes}
17931556Srgrimes
17941556Srgrimes} /* end of readtoken */
17951556Srgrimes
17961556Srgrimes
17971556Srgrimes/*
17981556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
17991556Srgrimes * or backquotes).
18001556Srgrimes */
18011556Srgrimes
1802213811Sobrienstatic int
180390111Simpnoexpand(char *text)
180490111Simp{
180525230Ssteve	char *p;
180625230Ssteve	char c;
18071556Srgrimes
18081556Srgrimes	p = text;
18091556Srgrimes	while ((c = *p++) != '\0') {
181039137Stegge		if ( c == CTLQUOTEMARK)
181139137Stegge			continue;
18121556Srgrimes		if (c == CTLESC)
18131556Srgrimes			p++;
181483675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
18151556Srgrimes			return 0;
18161556Srgrimes	}
18171556Srgrimes	return 1;
18181556Srgrimes}
18191556Srgrimes
18201556Srgrimes
18211556Srgrimes/*
18221556Srgrimes * Return true if the argument is a legal variable name (a letter or
18231556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
18241556Srgrimes */
18251556Srgrimes
18261556Srgrimesint
1827200956Sjillesgoodname(const char *name)
182890111Simp{
1829200956Sjilles	const char *p;
18301556Srgrimes
18311556Srgrimes	p = name;
18321556Srgrimes	if (! is_name(*p))
18331556Srgrimes		return 0;
18341556Srgrimes	while (*++p) {
18351556Srgrimes		if (! is_in_name(*p))
18361556Srgrimes			return 0;
18371556Srgrimes	}
18381556Srgrimes	return 1;
18391556Srgrimes}
18401556Srgrimes
18411556Srgrimes
1842222165Sjillesint
1843222165Sjillesisassignment(const char *p)
1844222165Sjilles{
1845222165Sjilles	if (!is_name(*p))
1846222165Sjilles		return 0;
1847222165Sjilles	p++;
1848222165Sjilles	for (;;) {
1849222165Sjilles		if (*p == '=')
1850222165Sjilles			return 1;
1851222165Sjilles		else if (!is_in_name(*p))
1852222165Sjilles			return 0;
1853222165Sjilles		p++;
1854222165Sjilles	}
1855222165Sjilles}
1856222165Sjilles
1857222165Sjilles
1858255073Sjillesstatic void
1859255073Sjillesconsumetoken(int token)
1860255073Sjilles{
1861255073Sjilles	if (readtoken() != token)
1862255073Sjilles		synexpect(token);
1863255073Sjilles}
1864255073Sjilles
1865255073Sjilles
18661556Srgrimes/*
18671556Srgrimes * Called when an unexpected token is read during the parse.  The argument
18681556Srgrimes * is the token that is expected, or -1 if more than one type of token can
18691556Srgrimes * occur at this point.
18701556Srgrimes */
18711556Srgrimes
1872213811Sobrienstatic void
187390111Simpsynexpect(int token)
187417987Speter{
18751556Srgrimes	char msg[64];
18761556Srgrimes
18771556Srgrimes	if (token >= 0) {
18781556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
18791556Srgrimes			tokname[lasttoken], tokname[token]);
18801556Srgrimes	} else {
18811556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
18821556Srgrimes	}
18831556Srgrimes	synerror(msg);
18841556Srgrimes}
18851556Srgrimes
18861556Srgrimes
1887213811Sobrienstatic void
1888201053Sjillessynerror(const char *msg)
188990111Simp{
18901556Srgrimes	if (commandname)
1891201366Sjilles		outfmt(out2, "%s: %d: ", commandname, startlinno);
1892201366Sjilles	outfmt(out2, "Syntax error: %s\n", msg);
18931556Srgrimes	error((char *)NULL);
18941556Srgrimes}
18951556Srgrimes
1896213811Sobrienstatic void
189790111Simpsetprompt(int which)
189890111Simp{
18991556Srgrimes	whichprompt = which;
19001556Srgrimes
190117987Speter#ifndef NO_HISTORY
19021556Srgrimes	if (!el)
190317987Speter#endif
1904199629Sjilles	{
19051556Srgrimes		out2str(getprompt(NULL));
1906199629Sjilles		flushout(out2);
1907199629Sjilles	}
19081556Srgrimes}
19091556Srgrimes
19101556Srgrimes/*
19111556Srgrimes * called by editline -- any expansions to the prompt
19121556Srgrimes *    should be added here.
19131556Srgrimes */
19141556Srgrimeschar *
191590111Simpgetprompt(void *unused __unused)
191625905Ssteve{
1917142845Sobrien	static char ps[PROMPTLEN];
1918142845Sobrien	char *fmt;
1919209653Sjilles	const char *pwd;
1920209653Sjilles	int i, trim;
1921214538Sjilles	static char internal_error[] = "??";
1922142845Sobrien
1923142845Sobrien	/*
1924142845Sobrien	 * Select prompt format.
1925142845Sobrien	 */
19261556Srgrimes	switch (whichprompt) {
19271556Srgrimes	case 0:
1928201053Sjilles		fmt = nullstr;
1929142845Sobrien		break;
19301556Srgrimes	case 1:
1931142845Sobrien		fmt = ps1val();
1932142845Sobrien		break;
19331556Srgrimes	case 2:
1934142845Sobrien		fmt = ps2val();
1935142845Sobrien		break;
19361556Srgrimes	default:
1937201053Sjilles		return internal_error;
19381556Srgrimes	}
1939142845Sobrien
1940142845Sobrien	/*
1941142845Sobrien	 * Format prompt string.
1942142845Sobrien	 */
1943142845Sobrien	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1944142845Sobrien		if (*fmt == '\\')
1945142845Sobrien			switch (*++fmt) {
1946142845Sobrien
1947142845Sobrien				/*
1948142845Sobrien				 * Hostname.
1949142845Sobrien				 *
1950142845Sobrien				 * \h specifies just the local hostname,
1951142845Sobrien				 * \H specifies fully-qualified hostname.
1952142845Sobrien				 */
1953142845Sobrien			case 'h':
1954142845Sobrien			case 'H':
1955149024Sstefanf				ps[i] = '\0';
1956142845Sobrien				gethostname(&ps[i], PROMPTLEN - i);
1957142845Sobrien				/* Skip to end of hostname. */
1958142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1959142845Sobrien				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1960142845Sobrien					i++;
1961142845Sobrien				break;
1962142845Sobrien
1963142845Sobrien				/*
1964142845Sobrien				 * Working directory.
1965142845Sobrien				 *
1966142845Sobrien				 * \W specifies just the final component,
1967142845Sobrien				 * \w specifies the entire path.
1968142845Sobrien				 */
1969142845Sobrien			case 'W':
1970142845Sobrien			case 'w':
1971209653Sjilles				pwd = lookupvar("PWD");
1972209653Sjilles				if (pwd == NULL)
1973209653Sjilles					pwd = "?";
1974209653Sjilles				if (*fmt == 'W' &&
1975209653Sjilles				    *pwd == '/' && pwd[1] != '\0')
1976209653Sjilles					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1977209653Sjilles					    PROMPTLEN - i);
1978209653Sjilles				else
1979209653Sjilles					strlcpy(&ps[i], pwd, PROMPTLEN - i);
1980142845Sobrien				/* Skip to end of path. */
1981142845Sobrien				while (ps[i + 1] != '\0')
1982142845Sobrien					i++;
1983142845Sobrien				break;
1984142845Sobrien
1985142845Sobrien				/*
1986142845Sobrien				 * Superuser status.
1987142845Sobrien				 *
1988142845Sobrien				 * '$' for normal users, '#' for root.
1989142845Sobrien				 */
1990142845Sobrien			case '$':
1991142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
1992142845Sobrien				break;
1993142845Sobrien
1994142845Sobrien				/*
1995142845Sobrien				 * A literal \.
1996142845Sobrien				 */
1997142845Sobrien			case '\\':
1998142845Sobrien				ps[i] = '\\';
1999142845Sobrien				break;
2000142845Sobrien
2001142845Sobrien				/*
2002142845Sobrien				 * Emit unrecognized formats verbatim.
2003142845Sobrien				 */
2004142845Sobrien			default:
2005142845Sobrien				ps[i++] = '\\';
2006142845Sobrien				ps[i] = *fmt;
2007142845Sobrien				break;
2008142845Sobrien			}
2009142845Sobrien		else
2010142845Sobrien			ps[i] = *fmt;
2011142845Sobrien	ps[i] = '\0';
2012142845Sobrien	return (ps);
20131556Srgrimes}
2014222907Sjilles
2015222907Sjilles
2016222907Sjillesconst char *
2017248980Sjillesexpandstr(const char *ps)
2018222907Sjilles{
2019222907Sjilles	union node n;
2020222907Sjilles	struct jmploc jmploc;
2021222907Sjilles	struct jmploc *const savehandler = handler;
2022222907Sjilles	const int saveprompt = doprompt;
2023222907Sjilles	struct parsefile *const savetopfile = getcurrentfile();
2024222907Sjilles	struct parser_temp *const saveparser_temp = parser_temp;
2025222907Sjilles	const char *result = NULL;
2026222907Sjilles
2027222907Sjilles	if (!setjmp(jmploc.loc)) {
2028222907Sjilles		handler = &jmploc;
2029222907Sjilles		parser_temp = NULL;
2030222907Sjilles		setinputstring(ps, 1);
2031222907Sjilles		doprompt = 0;
2032222907Sjilles		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2033222907Sjilles		if (backquotelist != NULL)
2034222907Sjilles			error("Command substitution not allowed here");
2035222907Sjilles
2036222907Sjilles		n.narg.type = NARG;
2037222907Sjilles		n.narg.next = NULL;
2038222907Sjilles		n.narg.text = wordtext;
2039222907Sjilles		n.narg.backquote = backquotelist;
2040222907Sjilles
2041222907Sjilles		expandarg(&n, NULL, 0);
2042222907Sjilles		result = stackblock();
2043222907Sjilles		INTOFF;
2044222907Sjilles	}
2045222907Sjilles	handler = savehandler;
2046222907Sjilles	doprompt = saveprompt;
2047222907Sjilles	popfilesupto(savetopfile);
2048222907Sjilles	if (parser_temp != saveparser_temp) {
2049222907Sjilles		parser_temp_free_all();
2050222907Sjilles		parser_temp = saveparser_temp;
2051222907Sjilles	}
2052222907Sjilles	if (result != NULL) {
2053222907Sjilles		INTON;
2054222907Sjilles	} else if (exception == EXINT)
2055222907Sjilles		raise(SIGINT);
2056222907Sjilles	return result;
2057222907Sjilles}
2058