xref: /aosp_15_r20/external/mksh/src/syn.c (revision 7c356e860f31eadd15fd599fcfdb9fd21f16a9d4)
1*7c356e86SAndroid Build Coastguard Worker /*	$OpenBSD: syn.c,v 1.30 2015/09/01 13:12:31 tedu Exp $	*/
2*7c356e86SAndroid Build Coastguard Worker 
3*7c356e86SAndroid Build Coastguard Worker /*-
4*7c356e86SAndroid Build Coastguard Worker  * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5*7c356e86SAndroid Build Coastguard Worker  *		 2011, 2012, 2013, 2014, 2015, 2016, 2017,
6*7c356e86SAndroid Build Coastguard Worker  *		 2018, 2020
7*7c356e86SAndroid Build Coastguard Worker  *	mirabilos <[email protected]>
8*7c356e86SAndroid Build Coastguard Worker  *
9*7c356e86SAndroid Build Coastguard Worker  * Provided that these terms and disclaimer and all copyright notices
10*7c356e86SAndroid Build Coastguard Worker  * are retained or reproduced in an accompanying document, permission
11*7c356e86SAndroid Build Coastguard Worker  * is granted to deal in this work without restriction, including un-
12*7c356e86SAndroid Build Coastguard Worker  * limited rights to use, publicly perform, distribute, sell, modify,
13*7c356e86SAndroid Build Coastguard Worker  * merge, give away, or sublicence.
14*7c356e86SAndroid Build Coastguard Worker  *
15*7c356e86SAndroid Build Coastguard Worker  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
16*7c356e86SAndroid Build Coastguard Worker  * the utmost extent permitted by applicable law, neither express nor
17*7c356e86SAndroid Build Coastguard Worker  * implied; without malicious intent or gross negligence. In no event
18*7c356e86SAndroid Build Coastguard Worker  * may a licensor, author or contributor be held liable for indirect,
19*7c356e86SAndroid Build Coastguard Worker  * direct, other damage, loss, or other issues arising in any way out
20*7c356e86SAndroid Build Coastguard Worker  * of dealing in the work, even if advised of the possibility of such
21*7c356e86SAndroid Build Coastguard Worker  * damage or existence of a defect, except proven that it results out
22*7c356e86SAndroid Build Coastguard Worker  * of said person's immediate fault when using the work as intended.
23*7c356e86SAndroid Build Coastguard Worker  */
24*7c356e86SAndroid Build Coastguard Worker 
25*7c356e86SAndroid Build Coastguard Worker #include "sh.h"
26*7c356e86SAndroid Build Coastguard Worker 
27*7c356e86SAndroid Build Coastguard Worker __RCSID("$MirOS: src/bin/mksh/syn.c,v 1.129 2020/10/31 01:21:58 tg Exp $");
28*7c356e86SAndroid Build Coastguard Worker 
29*7c356e86SAndroid Build Coastguard Worker struct nesting_state {
30*7c356e86SAndroid Build Coastguard Worker 	int start_token;	/* token than began nesting (eg, FOR) */
31*7c356e86SAndroid Build Coastguard Worker 	int start_line;		/* line nesting began on */
32*7c356e86SAndroid Build Coastguard Worker };
33*7c356e86SAndroid Build Coastguard Worker 
34*7c356e86SAndroid Build Coastguard Worker struct yyrecursive_state {
35*7c356e86SAndroid Build Coastguard Worker 	struct ioword *old_heres[HERES];
36*7c356e86SAndroid Build Coastguard Worker 	struct yyrecursive_state *next;
37*7c356e86SAndroid Build Coastguard Worker 	struct ioword **old_herep;
38*7c356e86SAndroid Build Coastguard Worker 	int old_symbol;
39*7c356e86SAndroid Build Coastguard Worker 	unsigned int old_nesting_type;
40*7c356e86SAndroid Build Coastguard Worker 	bool old_reject;
41*7c356e86SAndroid Build Coastguard Worker };
42*7c356e86SAndroid Build Coastguard Worker 
43*7c356e86SAndroid Build Coastguard Worker static void yyparse(bool);
44*7c356e86SAndroid Build Coastguard Worker static struct op *pipeline(int, int);
45*7c356e86SAndroid Build Coastguard Worker static struct op *andor(int);
46*7c356e86SAndroid Build Coastguard Worker static struct op *c_list(int, bool);
47*7c356e86SAndroid Build Coastguard Worker static struct ioword *synio(int);
48*7c356e86SAndroid Build Coastguard Worker static struct op *nested(int, int, int, int);
49*7c356e86SAndroid Build Coastguard Worker static struct op *get_command(int, int);
50*7c356e86SAndroid Build Coastguard Worker static struct op *dogroup(int);
51*7c356e86SAndroid Build Coastguard Worker static struct op *thenpart(int);
52*7c356e86SAndroid Build Coastguard Worker static struct op *elsepart(int);
53*7c356e86SAndroid Build Coastguard Worker static struct op *caselist(int);
54*7c356e86SAndroid Build Coastguard Worker static struct op *casepart(int, int);
55*7c356e86SAndroid Build Coastguard Worker static struct op *function_body(char *, int, bool);
56*7c356e86SAndroid Build Coastguard Worker static char **wordlist(int);
57*7c356e86SAndroid Build Coastguard Worker static struct op *block(int, struct op *, struct op *);
58*7c356e86SAndroid Build Coastguard Worker static struct op *newtp(int);
59*7c356e86SAndroid Build Coastguard Worker static void syntaxerr(const char *) MKSH_A_NORETURN;
60*7c356e86SAndroid Build Coastguard Worker static void nesting_push(struct nesting_state *, int);
61*7c356e86SAndroid Build Coastguard Worker static void nesting_pop(struct nesting_state *);
62*7c356e86SAndroid Build Coastguard Worker static int inalias(struct source *) MKSH_A_PURE;
63*7c356e86SAndroid Build Coastguard Worker static Test_op dbtestp_isa(Test_env *, Test_meta);
64*7c356e86SAndroid Build Coastguard Worker static const char *dbtestp_getopnd(Test_env *, Test_op, bool);
65*7c356e86SAndroid Build Coastguard Worker static int dbtestp_eval(Test_env *, Test_op, const char *,
66*7c356e86SAndroid Build Coastguard Worker     const char *, bool);
67*7c356e86SAndroid Build Coastguard Worker static void dbtestp_error(Test_env *, int, const char *) MKSH_A_NORETURN;
68*7c356e86SAndroid Build Coastguard Worker 
69*7c356e86SAndroid Build Coastguard Worker static struct op *outtree;		/* yyparse output */
70*7c356e86SAndroid Build Coastguard Worker static struct nesting_state nesting;	/* \n changed to ; */
71*7c356e86SAndroid Build Coastguard Worker 
72*7c356e86SAndroid Build Coastguard Worker static bool reject;			/* token(cf) gets symbol again */
73*7c356e86SAndroid Build Coastguard Worker static int symbol;			/* yylex value */
74*7c356e86SAndroid Build Coastguard Worker 
75*7c356e86SAndroid Build Coastguard Worker #define REJECT		(reject = true)
76*7c356e86SAndroid Build Coastguard Worker #define ACCEPT		(reject = false)
77*7c356e86SAndroid Build Coastguard Worker #define token(cf)	((reject) ? (ACCEPT, symbol) : (symbol = yylex(cf)))
78*7c356e86SAndroid Build Coastguard Worker #define tpeek(cf)	((reject) ? (symbol) : (REJECT, symbol = yylex(cf)))
79*7c356e86SAndroid Build Coastguard Worker #define musthave(c,cf)	do { 					\
80*7c356e86SAndroid Build Coastguard Worker 	if ((unsigned int)token(cf) != (unsigned int)(c))	\
81*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);				\
82*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
83*7c356e86SAndroid Build Coastguard Worker 
84*7c356e86SAndroid Build Coastguard Worker static const char Tcbrace[] = "}";
85*7c356e86SAndroid Build Coastguard Worker static const char Tesac[] = "esac";
86*7c356e86SAndroid Build Coastguard Worker 
87*7c356e86SAndroid Build Coastguard Worker static void
yyparse(bool doalias)88*7c356e86SAndroid Build Coastguard Worker yyparse(bool doalias)
89*7c356e86SAndroid Build Coastguard Worker {
90*7c356e86SAndroid Build Coastguard Worker 	int c;
91*7c356e86SAndroid Build Coastguard Worker 
92*7c356e86SAndroid Build Coastguard Worker 	ACCEPT;
93*7c356e86SAndroid Build Coastguard Worker 
94*7c356e86SAndroid Build Coastguard Worker 	outtree = c_list(doalias ? ALIAS : 0, source->type == SSTRING);
95*7c356e86SAndroid Build Coastguard Worker 	c = tpeek(0);
96*7c356e86SAndroid Build Coastguard Worker 	if (c == 0 && !outtree)
97*7c356e86SAndroid Build Coastguard Worker 		outtree = newtp(TEOF);
98*7c356e86SAndroid Build Coastguard Worker 	else if (!cinttype(c, C_LF | C_NUL))
99*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);
100*7c356e86SAndroid Build Coastguard Worker }
101*7c356e86SAndroid Build Coastguard Worker 
102*7c356e86SAndroid Build Coastguard Worker static struct op *
pipeline(int cf,int sALIAS)103*7c356e86SAndroid Build Coastguard Worker pipeline(int cf, int sALIAS)
104*7c356e86SAndroid Build Coastguard Worker {
105*7c356e86SAndroid Build Coastguard Worker 	struct op *t, *p, *tl = NULL;
106*7c356e86SAndroid Build Coastguard Worker 
107*7c356e86SAndroid Build Coastguard Worker 	t = get_command(cf, sALIAS);
108*7c356e86SAndroid Build Coastguard Worker 	if (t != NULL) {
109*7c356e86SAndroid Build Coastguard Worker 		while (token(0) == '|') {
110*7c356e86SAndroid Build Coastguard Worker 			if ((p = get_command(CONTIN, sALIAS)) == NULL)
111*7c356e86SAndroid Build Coastguard Worker 				syntaxerr(NULL);
112*7c356e86SAndroid Build Coastguard Worker 			if (tl == NULL)
113*7c356e86SAndroid Build Coastguard Worker 				t = tl = block(TPIPE, t, p);
114*7c356e86SAndroid Build Coastguard Worker 			else
115*7c356e86SAndroid Build Coastguard Worker 				tl = tl->right = block(TPIPE, tl->right, p);
116*7c356e86SAndroid Build Coastguard Worker 		}
117*7c356e86SAndroid Build Coastguard Worker 		REJECT;
118*7c356e86SAndroid Build Coastguard Worker 	}
119*7c356e86SAndroid Build Coastguard Worker 	return (t);
120*7c356e86SAndroid Build Coastguard Worker }
121*7c356e86SAndroid Build Coastguard Worker 
122*7c356e86SAndroid Build Coastguard Worker static struct op *
andor(int sALIAS)123*7c356e86SAndroid Build Coastguard Worker andor(int sALIAS)
124*7c356e86SAndroid Build Coastguard Worker {
125*7c356e86SAndroid Build Coastguard Worker 	struct op *t, *p;
126*7c356e86SAndroid Build Coastguard Worker 	int c;
127*7c356e86SAndroid Build Coastguard Worker 
128*7c356e86SAndroid Build Coastguard Worker 	t = pipeline(0, sALIAS);
129*7c356e86SAndroid Build Coastguard Worker 	if (t != NULL) {
130*7c356e86SAndroid Build Coastguard Worker 		while ((c = token(0)) == LOGAND || c == LOGOR) {
131*7c356e86SAndroid Build Coastguard Worker 			if ((p = pipeline(CONTIN, sALIAS)) == NULL)
132*7c356e86SAndroid Build Coastguard Worker 				syntaxerr(NULL);
133*7c356e86SAndroid Build Coastguard Worker 			t = block(c == LOGAND? TAND: TOR, t, p);
134*7c356e86SAndroid Build Coastguard Worker 		}
135*7c356e86SAndroid Build Coastguard Worker 		REJECT;
136*7c356e86SAndroid Build Coastguard Worker 	}
137*7c356e86SAndroid Build Coastguard Worker 	return (t);
138*7c356e86SAndroid Build Coastguard Worker }
139*7c356e86SAndroid Build Coastguard Worker 
140*7c356e86SAndroid Build Coastguard Worker static struct op *
c_list(int sALIAS,bool multi)141*7c356e86SAndroid Build Coastguard Worker c_list(int sALIAS, bool multi)
142*7c356e86SAndroid Build Coastguard Worker {
143*7c356e86SAndroid Build Coastguard Worker 	struct op *t = NULL, *p, *tl = NULL;
144*7c356e86SAndroid Build Coastguard Worker 	int c;
145*7c356e86SAndroid Build Coastguard Worker 	bool have_sep;
146*7c356e86SAndroid Build Coastguard Worker 
147*7c356e86SAndroid Build Coastguard Worker 	while (/* CONSTCOND */ 1) {
148*7c356e86SAndroid Build Coastguard Worker 		p = andor(sALIAS);
149*7c356e86SAndroid Build Coastguard Worker 		/*
150*7c356e86SAndroid Build Coastguard Worker 		 * Token has always been read/rejected at this point, so
151*7c356e86SAndroid Build Coastguard Worker 		 * we don't worry about what flags to pass token()
152*7c356e86SAndroid Build Coastguard Worker 		 */
153*7c356e86SAndroid Build Coastguard Worker 		c = token(0);
154*7c356e86SAndroid Build Coastguard Worker 		have_sep = true;
155*7c356e86SAndroid Build Coastguard Worker 		if (c == '\n' && (multi || inalias(source))) {
156*7c356e86SAndroid Build Coastguard Worker 			if (!p)
157*7c356e86SAndroid Build Coastguard Worker 				/* ignore blank lines */
158*7c356e86SAndroid Build Coastguard Worker 				continue;
159*7c356e86SAndroid Build Coastguard Worker 		} else if (!p)
160*7c356e86SAndroid Build Coastguard Worker 			break;
161*7c356e86SAndroid Build Coastguard Worker 		else if (c == '&' || c == COPROC)
162*7c356e86SAndroid Build Coastguard Worker 			p = block(c == '&' ? TASYNC : TCOPROC, p, NULL);
163*7c356e86SAndroid Build Coastguard Worker 		else if (c != ';')
164*7c356e86SAndroid Build Coastguard Worker 			have_sep = false;
165*7c356e86SAndroid Build Coastguard Worker 		if (!t)
166*7c356e86SAndroid Build Coastguard Worker 			t = p;
167*7c356e86SAndroid Build Coastguard Worker 		else if (!tl)
168*7c356e86SAndroid Build Coastguard Worker 			t = tl = block(TLIST, t, p);
169*7c356e86SAndroid Build Coastguard Worker 		else
170*7c356e86SAndroid Build Coastguard Worker 			tl = tl->right = block(TLIST, tl->right, p);
171*7c356e86SAndroid Build Coastguard Worker 		if (!have_sep)
172*7c356e86SAndroid Build Coastguard Worker 			break;
173*7c356e86SAndroid Build Coastguard Worker 	}
174*7c356e86SAndroid Build Coastguard Worker 	REJECT;
175*7c356e86SAndroid Build Coastguard Worker 	return (t);
176*7c356e86SAndroid Build Coastguard Worker }
177*7c356e86SAndroid Build Coastguard Worker 
178*7c356e86SAndroid Build Coastguard Worker static const char IONDELIM_delim[] = { CHAR, '<', CHAR, '<', EOS };
179*7c356e86SAndroid Build Coastguard Worker 
180*7c356e86SAndroid Build Coastguard Worker static struct ioword *
synio(int cf)181*7c356e86SAndroid Build Coastguard Worker synio(int cf)
182*7c356e86SAndroid Build Coastguard Worker {
183*7c356e86SAndroid Build Coastguard Worker 	struct ioword *iop;
184*7c356e86SAndroid Build Coastguard Worker 	static struct ioword *nextiop;
185*7c356e86SAndroid Build Coastguard Worker 	bool ishere;
186*7c356e86SAndroid Build Coastguard Worker 
187*7c356e86SAndroid Build Coastguard Worker 	if (nextiop != NULL) {
188*7c356e86SAndroid Build Coastguard Worker 		iop = nextiop;
189*7c356e86SAndroid Build Coastguard Worker 		nextiop = NULL;
190*7c356e86SAndroid Build Coastguard Worker 		return (iop);
191*7c356e86SAndroid Build Coastguard Worker 	}
192*7c356e86SAndroid Build Coastguard Worker 
193*7c356e86SAndroid Build Coastguard Worker 	if (tpeek(cf) != REDIR)
194*7c356e86SAndroid Build Coastguard Worker 		return (NULL);
195*7c356e86SAndroid Build Coastguard Worker 	ACCEPT;
196*7c356e86SAndroid Build Coastguard Worker 	iop = yylval.iop;
197*7c356e86SAndroid Build Coastguard Worker 	ishere = (iop->ioflag & IOTYPE) == IOHERE;
198*7c356e86SAndroid Build Coastguard Worker 	if (iop->ioflag & IOHERESTR) {
199*7c356e86SAndroid Build Coastguard Worker 		musthave(LWORD, 0);
200*7c356e86SAndroid Build Coastguard Worker 	} else if (ishere && tpeek(HEREDELIM) == '\n') {
201*7c356e86SAndroid Build Coastguard Worker 		ACCEPT;
202*7c356e86SAndroid Build Coastguard Worker 		yylval.cp = wdcopy(IONDELIM_delim, ATEMP);
203*7c356e86SAndroid Build Coastguard Worker 		iop->ioflag |= IOEVAL | IONDELIM;
204*7c356e86SAndroid Build Coastguard Worker 	} else
205*7c356e86SAndroid Build Coastguard Worker 		musthave(LWORD, ishere ? HEREDELIM : 0);
206*7c356e86SAndroid Build Coastguard Worker 	if (ishere) {
207*7c356e86SAndroid Build Coastguard Worker 		iop->delim = yylval.cp;
208*7c356e86SAndroid Build Coastguard Worker 		if (*ident != 0 && !(iop->ioflag & IOHERESTR)) {
209*7c356e86SAndroid Build Coastguard Worker 			/* unquoted */
210*7c356e86SAndroid Build Coastguard Worker 			iop->ioflag |= IOEVAL;
211*7c356e86SAndroid Build Coastguard Worker 		}
212*7c356e86SAndroid Build Coastguard Worker 		if (herep > &heres[HERES - 1])
213*7c356e86SAndroid Build Coastguard Worker 			yyerror(Tf_toomany, "<<");
214*7c356e86SAndroid Build Coastguard Worker 		*herep++ = iop;
215*7c356e86SAndroid Build Coastguard Worker 	} else
216*7c356e86SAndroid Build Coastguard Worker 		iop->ioname = yylval.cp;
217*7c356e86SAndroid Build Coastguard Worker 
218*7c356e86SAndroid Build Coastguard Worker 	if (iop->ioflag & IOBASH) {
219*7c356e86SAndroid Build Coastguard Worker 		char *cp;
220*7c356e86SAndroid Build Coastguard Worker 
221*7c356e86SAndroid Build Coastguard Worker 		nextiop = alloc(sizeof(*iop), ATEMP);
222*7c356e86SAndroid Build Coastguard Worker 		nextiop->ioname = cp = alloc(3, ATEMP);
223*7c356e86SAndroid Build Coastguard Worker 		*cp++ = CHAR;
224*7c356e86SAndroid Build Coastguard Worker 		*cp++ = digits_lc[iop->unit % 10];
225*7c356e86SAndroid Build Coastguard Worker 		*cp = EOS;
226*7c356e86SAndroid Build Coastguard Worker 
227*7c356e86SAndroid Build Coastguard Worker 		iop->ioflag &= ~IOBASH;
228*7c356e86SAndroid Build Coastguard Worker 		nextiop->unit = 2;
229*7c356e86SAndroid Build Coastguard Worker 		nextiop->ioflag = IODUP;
230*7c356e86SAndroid Build Coastguard Worker 		nextiop->delim = NULL;
231*7c356e86SAndroid Build Coastguard Worker 		nextiop->heredoc = NULL;
232*7c356e86SAndroid Build Coastguard Worker 	}
233*7c356e86SAndroid Build Coastguard Worker 	return (iop);
234*7c356e86SAndroid Build Coastguard Worker }
235*7c356e86SAndroid Build Coastguard Worker 
236*7c356e86SAndroid Build Coastguard Worker static struct op *
nested(int type,int smark,int emark,int sALIAS)237*7c356e86SAndroid Build Coastguard Worker nested(int type, int smark, int emark, int sALIAS)
238*7c356e86SAndroid Build Coastguard Worker {
239*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
240*7c356e86SAndroid Build Coastguard Worker 	struct nesting_state old_nesting;
241*7c356e86SAndroid Build Coastguard Worker 
242*7c356e86SAndroid Build Coastguard Worker 	nesting_push(&old_nesting, smark);
243*7c356e86SAndroid Build Coastguard Worker 	t = c_list(sALIAS, true);
244*7c356e86SAndroid Build Coastguard Worker 	musthave(emark, KEYWORD|sALIAS);
245*7c356e86SAndroid Build Coastguard Worker 	nesting_pop(&old_nesting);
246*7c356e86SAndroid Build Coastguard Worker 	return (block(type, t, NULL));
247*7c356e86SAndroid Build Coastguard Worker }
248*7c356e86SAndroid Build Coastguard Worker 
249*7c356e86SAndroid Build Coastguard Worker static const char builtin_cmd[] = {
250*7c356e86SAndroid Build Coastguard Worker 	QCHAR, '\\', CHAR, 'b', CHAR, 'u', CHAR, 'i',
251*7c356e86SAndroid Build Coastguard Worker 	CHAR, 'l', CHAR, 't', CHAR, 'i', CHAR, 'n', EOS
252*7c356e86SAndroid Build Coastguard Worker };
253*7c356e86SAndroid Build Coastguard Worker static const char let_cmd[] = {
254*7c356e86SAndroid Build Coastguard Worker 	CHAR, 'l', CHAR, 'e', CHAR, 't', EOS
255*7c356e86SAndroid Build Coastguard Worker };
256*7c356e86SAndroid Build Coastguard Worker static const char setA_cmd0[] = {
257*7c356e86SAndroid Build Coastguard Worker 	CHAR, 's', CHAR, 'e', CHAR, 't', EOS
258*7c356e86SAndroid Build Coastguard Worker };
259*7c356e86SAndroid Build Coastguard Worker static const char setA_cmd1[] = {
260*7c356e86SAndroid Build Coastguard Worker 	CHAR, '-', CHAR, 'A', EOS
261*7c356e86SAndroid Build Coastguard Worker };
262*7c356e86SAndroid Build Coastguard Worker static const char setA_cmd2[] = {
263*7c356e86SAndroid Build Coastguard Worker 	CHAR, '-', CHAR, '-', EOS
264*7c356e86SAndroid Build Coastguard Worker };
265*7c356e86SAndroid Build Coastguard Worker 
266*7c356e86SAndroid Build Coastguard Worker static struct op *
get_command(int cf,int sALIAS)267*7c356e86SAndroid Build Coastguard Worker get_command(int cf, int sALIAS)
268*7c356e86SAndroid Build Coastguard Worker {
269*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
270*7c356e86SAndroid Build Coastguard Worker 	int c, iopn = 0, syniocf, lno;
271*7c356e86SAndroid Build Coastguard Worker 	struct ioword *iop;
272*7c356e86SAndroid Build Coastguard Worker 	XPtrV args, vars;
273*7c356e86SAndroid Build Coastguard Worker 	struct nesting_state old_nesting;
274*7c356e86SAndroid Build Coastguard Worker 	bool check_decl_utility;
275*7c356e86SAndroid Build Coastguard Worker 	static struct ioword *iops[NUFILE + 1];
276*7c356e86SAndroid Build Coastguard Worker 
277*7c356e86SAndroid Build Coastguard Worker 	XPinit(args, 16);
278*7c356e86SAndroid Build Coastguard Worker 	XPinit(vars, 16);
279*7c356e86SAndroid Build Coastguard Worker 
280*7c356e86SAndroid Build Coastguard Worker 	syniocf = KEYWORD|sALIAS;
281*7c356e86SAndroid Build Coastguard Worker 	switch (c = token(cf|KEYWORD|sALIAS|CMDASN)) {
282*7c356e86SAndroid Build Coastguard Worker 	default:
283*7c356e86SAndroid Build Coastguard Worker 		REJECT;
284*7c356e86SAndroid Build Coastguard Worker 		XPfree(args);
285*7c356e86SAndroid Build Coastguard Worker 		XPfree(vars);
286*7c356e86SAndroid Build Coastguard Worker 		/* empty line */
287*7c356e86SAndroid Build Coastguard Worker 		return (NULL);
288*7c356e86SAndroid Build Coastguard Worker 
289*7c356e86SAndroid Build Coastguard Worker 	case LWORD:
290*7c356e86SAndroid Build Coastguard Worker 	case REDIR:
291*7c356e86SAndroid Build Coastguard Worker 		REJECT;
292*7c356e86SAndroid Build Coastguard Worker 		syniocf &= ~(KEYWORD|sALIAS);
293*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TCOM);
294*7c356e86SAndroid Build Coastguard Worker 		t->lineno = source->line;
295*7c356e86SAndroid Build Coastguard Worker 		goto get_command_start;
296*7c356e86SAndroid Build Coastguard Worker 
297*7c356e86SAndroid Build Coastguard Worker  get_command_loop:
298*7c356e86SAndroid Build Coastguard Worker 		if (XPsize(args) == 0) {
299*7c356e86SAndroid Build Coastguard Worker  get_command_start:
300*7c356e86SAndroid Build Coastguard Worker 			check_decl_utility = true;
301*7c356e86SAndroid Build Coastguard Worker 			cf = sALIAS | CMDASN;
302*7c356e86SAndroid Build Coastguard Worker 		} else if (t->u.evalflags)
303*7c356e86SAndroid Build Coastguard Worker 			cf = CMDWORD | CMDASN;
304*7c356e86SAndroid Build Coastguard Worker 		else
305*7c356e86SAndroid Build Coastguard Worker 			cf = CMDWORD;
306*7c356e86SAndroid Build Coastguard Worker 
307*7c356e86SAndroid Build Coastguard Worker 		switch (tpeek(cf)) {
308*7c356e86SAndroid Build Coastguard Worker 		case REDIR:
309*7c356e86SAndroid Build Coastguard Worker 			while ((iop = synio(cf)) != NULL) {
310*7c356e86SAndroid Build Coastguard Worker 				if (iopn >= NUFILE)
311*7c356e86SAndroid Build Coastguard Worker 					yyerror(Tf_toomany, Tredirection);
312*7c356e86SAndroid Build Coastguard Worker 				iops[iopn++] = iop;
313*7c356e86SAndroid Build Coastguard Worker 			}
314*7c356e86SAndroid Build Coastguard Worker 			goto get_command_loop;
315*7c356e86SAndroid Build Coastguard Worker 
316*7c356e86SAndroid Build Coastguard Worker 		case LWORD:
317*7c356e86SAndroid Build Coastguard Worker 			ACCEPT;
318*7c356e86SAndroid Build Coastguard Worker 			if (check_decl_utility) {
319*7c356e86SAndroid Build Coastguard Worker 				struct tbl *tt = get_builtin(ident);
320*7c356e86SAndroid Build Coastguard Worker 				uint32_t flag;
321*7c356e86SAndroid Build Coastguard Worker 
322*7c356e86SAndroid Build Coastguard Worker 				flag = tt ? tt->flag : 0;
323*7c356e86SAndroid Build Coastguard Worker 				if (flag & DECL_UTIL)
324*7c356e86SAndroid Build Coastguard Worker 					t->u.evalflags = DOVACHECK;
325*7c356e86SAndroid Build Coastguard Worker 				if (!(flag & DECL_FWDR))
326*7c356e86SAndroid Build Coastguard Worker 					check_decl_utility = false;
327*7c356e86SAndroid Build Coastguard Worker 			}
328*7c356e86SAndroid Build Coastguard Worker 			if ((XPsize(args) == 0 || Flag(FKEYWORD)) &&
329*7c356e86SAndroid Build Coastguard Worker 			    is_wdvarassign(yylval.cp))
330*7c356e86SAndroid Build Coastguard Worker 				XPput(vars, yylval.cp);
331*7c356e86SAndroid Build Coastguard Worker 			else
332*7c356e86SAndroid Build Coastguard Worker 				XPput(args, yylval.cp);
333*7c356e86SAndroid Build Coastguard Worker 			goto get_command_loop;
334*7c356e86SAndroid Build Coastguard Worker 
335*7c356e86SAndroid Build Coastguard Worker 		case ORD('(' /*)*/):
336*7c356e86SAndroid Build Coastguard Worker 			if (XPsize(args) == 0 && XPsize(vars) == 1 &&
337*7c356e86SAndroid Build Coastguard Worker 			    is_wdvarassign(yylval.cp)) {
338*7c356e86SAndroid Build Coastguard Worker 				char *tcp;
339*7c356e86SAndroid Build Coastguard Worker 
340*7c356e86SAndroid Build Coastguard Worker 				/* wdarrassign: foo=(bar) */
341*7c356e86SAndroid Build Coastguard Worker 				ACCEPT;
342*7c356e86SAndroid Build Coastguard Worker 
343*7c356e86SAndroid Build Coastguard Worker 				/* manipulate the vars string */
344*7c356e86SAndroid Build Coastguard Worker 				tcp = XPptrv(vars)[(vars.len = 0)];
345*7c356e86SAndroid Build Coastguard Worker 				/* 'varname=' -> 'varname' */
346*7c356e86SAndroid Build Coastguard Worker 				tcp[wdscan(tcp, EOS) - tcp - 3] = EOS;
347*7c356e86SAndroid Build Coastguard Worker 
348*7c356e86SAndroid Build Coastguard Worker 				/* construct new args strings */
349*7c356e86SAndroid Build Coastguard Worker 				XPput(args, wdcopy(builtin_cmd, ATEMP));
350*7c356e86SAndroid Build Coastguard Worker 				XPput(args, wdcopy(setA_cmd0, ATEMP));
351*7c356e86SAndroid Build Coastguard Worker 				XPput(args, wdcopy(setA_cmd1, ATEMP));
352*7c356e86SAndroid Build Coastguard Worker 				XPput(args, tcp);
353*7c356e86SAndroid Build Coastguard Worker 				XPput(args, wdcopy(setA_cmd2, ATEMP));
354*7c356e86SAndroid Build Coastguard Worker 
355*7c356e86SAndroid Build Coastguard Worker 				/* slurp in words till closing paren */
356*7c356e86SAndroid Build Coastguard Worker 				while (token(CONTIN) == LWORD)
357*7c356e86SAndroid Build Coastguard Worker 					XPput(args, yylval.cp);
358*7c356e86SAndroid Build Coastguard Worker 				if (symbol != /*(*/ ')')
359*7c356e86SAndroid Build Coastguard Worker 					syntaxerr(NULL);
360*7c356e86SAndroid Build Coastguard Worker 				break;
361*7c356e86SAndroid Build Coastguard Worker 			}
362*7c356e86SAndroid Build Coastguard Worker 
363*7c356e86SAndroid Build Coastguard Worker 			afree(t, ATEMP);
364*7c356e86SAndroid Build Coastguard Worker 
365*7c356e86SAndroid Build Coastguard Worker 			/*
366*7c356e86SAndroid Build Coastguard Worker 			 * Check for "> foo (echo hi)" which AT&T ksh allows
367*7c356e86SAndroid Build Coastguard Worker 			 * (not POSIX, but not disallowed)
368*7c356e86SAndroid Build Coastguard Worker 			 */
369*7c356e86SAndroid Build Coastguard Worker 			if (XPsize(args) == 0 && XPsize(vars) == 0) {
370*7c356e86SAndroid Build Coastguard Worker 				ACCEPT;
371*7c356e86SAndroid Build Coastguard Worker 				goto Subshell;
372*7c356e86SAndroid Build Coastguard Worker 			}
373*7c356e86SAndroid Build Coastguard Worker 
374*7c356e86SAndroid Build Coastguard Worker 			/* must be a function */
375*7c356e86SAndroid Build Coastguard Worker 			if (iopn != 0 || XPsize(args) != 1 || XPsize(vars) != 0)
376*7c356e86SAndroid Build Coastguard Worker 				syntaxerr(NULL);
377*7c356e86SAndroid Build Coastguard Worker 			ACCEPT;
378*7c356e86SAndroid Build Coastguard Worker 			musthave(/*(*/ ')', 0);
379*7c356e86SAndroid Build Coastguard Worker 			t = function_body(XPptrv(args)[0],
380*7c356e86SAndroid Build Coastguard Worker 			    sALIAS, false);
381*7c356e86SAndroid Build Coastguard Worker 			break;
382*7c356e86SAndroid Build Coastguard Worker 		}
383*7c356e86SAndroid Build Coastguard Worker 		break;
384*7c356e86SAndroid Build Coastguard Worker 
385*7c356e86SAndroid Build Coastguard Worker 	case ORD('(' /*)*/): {
386*7c356e86SAndroid Build Coastguard Worker 		unsigned int subshell_nesting_type_saved;
387*7c356e86SAndroid Build Coastguard Worker  Subshell:
388*7c356e86SAndroid Build Coastguard Worker 		subshell_nesting_type_saved = subshell_nesting_type;
389*7c356e86SAndroid Build Coastguard Worker 		subshell_nesting_type = ORD(')');
390*7c356e86SAndroid Build Coastguard Worker 		t = nested(TPAREN, ORD('('), ORD(')'), sALIAS);
391*7c356e86SAndroid Build Coastguard Worker 		subshell_nesting_type = subshell_nesting_type_saved;
392*7c356e86SAndroid Build Coastguard Worker 		break;
393*7c356e86SAndroid Build Coastguard Worker 	    }
394*7c356e86SAndroid Build Coastguard Worker 
395*7c356e86SAndroid Build Coastguard Worker 	case ORD('{' /*}*/):
396*7c356e86SAndroid Build Coastguard Worker 		t = nested(TBRACE, ORD('{'), ORD('}'), sALIAS);
397*7c356e86SAndroid Build Coastguard Worker 		break;
398*7c356e86SAndroid Build Coastguard Worker 
399*7c356e86SAndroid Build Coastguard Worker 	case MDPAREN:
400*7c356e86SAndroid Build Coastguard Worker 		/* leave KEYWORD in syniocf (allow if (( 1 )) then ...) */
401*7c356e86SAndroid Build Coastguard Worker 		lno = source->line;
402*7c356e86SAndroid Build Coastguard Worker 		ACCEPT;
403*7c356e86SAndroid Build Coastguard Worker 		switch (token(LETEXPR)) {
404*7c356e86SAndroid Build Coastguard Worker 		case LWORD:
405*7c356e86SAndroid Build Coastguard Worker 			break;
406*7c356e86SAndroid Build Coastguard Worker 		case ORD('(' /*)*/):
407*7c356e86SAndroid Build Coastguard Worker 			c = ORD('(');
408*7c356e86SAndroid Build Coastguard Worker 			goto Subshell;
409*7c356e86SAndroid Build Coastguard Worker 		default:
410*7c356e86SAndroid Build Coastguard Worker 			syntaxerr(NULL);
411*7c356e86SAndroid Build Coastguard Worker 		}
412*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TCOM);
413*7c356e86SAndroid Build Coastguard Worker 		t->lineno = lno;
414*7c356e86SAndroid Build Coastguard Worker 		XPput(args, wdcopy(builtin_cmd, ATEMP));
415*7c356e86SAndroid Build Coastguard Worker 		XPput(args, wdcopy(let_cmd, ATEMP));
416*7c356e86SAndroid Build Coastguard Worker 		XPput(args, yylval.cp);
417*7c356e86SAndroid Build Coastguard Worker 		break;
418*7c356e86SAndroid Build Coastguard Worker 
419*7c356e86SAndroid Build Coastguard Worker 	case DBRACKET: /* [[ .. ]] */
420*7c356e86SAndroid Build Coastguard Worker 		/* leave KEYWORD in syniocf (allow if [[ -n 1 ]] then ...) */
421*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TDBRACKET);
422*7c356e86SAndroid Build Coastguard Worker 		ACCEPT;
423*7c356e86SAndroid Build Coastguard Worker 		{
424*7c356e86SAndroid Build Coastguard Worker 			Test_env te;
425*7c356e86SAndroid Build Coastguard Worker 
426*7c356e86SAndroid Build Coastguard Worker 			te.flags = TEF_DBRACKET;
427*7c356e86SAndroid Build Coastguard Worker 			te.pos.av = &args;
428*7c356e86SAndroid Build Coastguard Worker 			te.isa = dbtestp_isa;
429*7c356e86SAndroid Build Coastguard Worker 			te.getopnd = dbtestp_getopnd;
430*7c356e86SAndroid Build Coastguard Worker 			te.eval = dbtestp_eval;
431*7c356e86SAndroid Build Coastguard Worker 			te.error = dbtestp_error;
432*7c356e86SAndroid Build Coastguard Worker 
433*7c356e86SAndroid Build Coastguard Worker 			test_parse(&te);
434*7c356e86SAndroid Build Coastguard Worker 		}
435*7c356e86SAndroid Build Coastguard Worker 		break;
436*7c356e86SAndroid Build Coastguard Worker 
437*7c356e86SAndroid Build Coastguard Worker 	case FOR:
438*7c356e86SAndroid Build Coastguard Worker 	case SELECT:
439*7c356e86SAndroid Build Coastguard Worker 		t = newtp((c == FOR) ? TFOR : TSELECT);
440*7c356e86SAndroid Build Coastguard Worker 		musthave(LWORD, CMDASN);
441*7c356e86SAndroid Build Coastguard Worker 		if (!is_wdvarname(yylval.cp, true))
442*7c356e86SAndroid Build Coastguard Worker 			yyerror("%s: bad identifier",
443*7c356e86SAndroid Build Coastguard Worker 			    c == FOR ? "for" : Tselect);
444*7c356e86SAndroid Build Coastguard Worker 		strdupx(t->str, ident, ATEMP);
445*7c356e86SAndroid Build Coastguard Worker 		nesting_push(&old_nesting, c);
446*7c356e86SAndroid Build Coastguard Worker 		t->vars = wordlist(sALIAS);
447*7c356e86SAndroid Build Coastguard Worker 		t->left = dogroup(sALIAS);
448*7c356e86SAndroid Build Coastguard Worker 		nesting_pop(&old_nesting);
449*7c356e86SAndroid Build Coastguard Worker 		break;
450*7c356e86SAndroid Build Coastguard Worker 
451*7c356e86SAndroid Build Coastguard Worker 	case WHILE:
452*7c356e86SAndroid Build Coastguard Worker 	case UNTIL:
453*7c356e86SAndroid Build Coastguard Worker 		nesting_push(&old_nesting, c);
454*7c356e86SAndroid Build Coastguard Worker 		t = newtp((c == WHILE) ? TWHILE : TUNTIL);
455*7c356e86SAndroid Build Coastguard Worker 		t->left = c_list(sALIAS, true);
456*7c356e86SAndroid Build Coastguard Worker 		t->right = dogroup(sALIAS);
457*7c356e86SAndroid Build Coastguard Worker 		nesting_pop(&old_nesting);
458*7c356e86SAndroid Build Coastguard Worker 		break;
459*7c356e86SAndroid Build Coastguard Worker 
460*7c356e86SAndroid Build Coastguard Worker 	case CASE:
461*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TCASE);
462*7c356e86SAndroid Build Coastguard Worker 		musthave(LWORD, 0);
463*7c356e86SAndroid Build Coastguard Worker 		t->str = yylval.cp;
464*7c356e86SAndroid Build Coastguard Worker 		nesting_push(&old_nesting, c);
465*7c356e86SAndroid Build Coastguard Worker 		t->left = caselist(sALIAS);
466*7c356e86SAndroid Build Coastguard Worker 		nesting_pop(&old_nesting);
467*7c356e86SAndroid Build Coastguard Worker 		break;
468*7c356e86SAndroid Build Coastguard Worker 
469*7c356e86SAndroid Build Coastguard Worker 	case IF:
470*7c356e86SAndroid Build Coastguard Worker 		nesting_push(&old_nesting, c);
471*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TIF);
472*7c356e86SAndroid Build Coastguard Worker 		t->left = c_list(sALIAS, true);
473*7c356e86SAndroid Build Coastguard Worker 		t->right = thenpart(sALIAS);
474*7c356e86SAndroid Build Coastguard Worker 		musthave(FI, KEYWORD|sALIAS);
475*7c356e86SAndroid Build Coastguard Worker 		nesting_pop(&old_nesting);
476*7c356e86SAndroid Build Coastguard Worker 		break;
477*7c356e86SAndroid Build Coastguard Worker 
478*7c356e86SAndroid Build Coastguard Worker 	case BANG:
479*7c356e86SAndroid Build Coastguard Worker 		syniocf &= ~(KEYWORD|sALIAS);
480*7c356e86SAndroid Build Coastguard Worker 		t = pipeline(0, sALIAS);
481*7c356e86SAndroid Build Coastguard Worker 		if (t == NULL)
482*7c356e86SAndroid Build Coastguard Worker 			syntaxerr(NULL);
483*7c356e86SAndroid Build Coastguard Worker 		t = block(TBANG, NULL, t);
484*7c356e86SAndroid Build Coastguard Worker 		break;
485*7c356e86SAndroid Build Coastguard Worker 
486*7c356e86SAndroid Build Coastguard Worker 	case TIME:
487*7c356e86SAndroid Build Coastguard Worker 		syniocf &= ~(KEYWORD|sALIAS);
488*7c356e86SAndroid Build Coastguard Worker 		t = pipeline(0, sALIAS);
489*7c356e86SAndroid Build Coastguard Worker 		if (t && t->type == TCOM) {
490*7c356e86SAndroid Build Coastguard Worker 			t->str = alloc(2, ATEMP);
491*7c356e86SAndroid Build Coastguard Worker 			/* TF_* flags */
492*7c356e86SAndroid Build Coastguard Worker 			t->str[0] = '\0';
493*7c356e86SAndroid Build Coastguard Worker 			t->str[1] = '\0';
494*7c356e86SAndroid Build Coastguard Worker 		}
495*7c356e86SAndroid Build Coastguard Worker 		t = block(TTIME, t, NULL);
496*7c356e86SAndroid Build Coastguard Worker 		break;
497*7c356e86SAndroid Build Coastguard Worker 
498*7c356e86SAndroid Build Coastguard Worker 	case FUNCTION:
499*7c356e86SAndroid Build Coastguard Worker 		musthave(LWORD, 0);
500*7c356e86SAndroid Build Coastguard Worker 		t = function_body(yylval.cp, sALIAS, true);
501*7c356e86SAndroid Build Coastguard Worker 		break;
502*7c356e86SAndroid Build Coastguard Worker 	}
503*7c356e86SAndroid Build Coastguard Worker 
504*7c356e86SAndroid Build Coastguard Worker 	while ((iop = synio(syniocf)) != NULL) {
505*7c356e86SAndroid Build Coastguard Worker 		if (iopn >= NUFILE)
506*7c356e86SAndroid Build Coastguard Worker 			yyerror(Tf_toomany, Tredirection);
507*7c356e86SAndroid Build Coastguard Worker 		iops[iopn++] = iop;
508*7c356e86SAndroid Build Coastguard Worker 	}
509*7c356e86SAndroid Build Coastguard Worker 
510*7c356e86SAndroid Build Coastguard Worker 	if (iopn == 0) {
511*7c356e86SAndroid Build Coastguard Worker 		t->ioact = NULL;
512*7c356e86SAndroid Build Coastguard Worker 	} else {
513*7c356e86SAndroid Build Coastguard Worker 		iops[iopn++] = NULL;
514*7c356e86SAndroid Build Coastguard Worker 		t->ioact = alloc2(iopn, sizeof(struct ioword *), ATEMP);
515*7c356e86SAndroid Build Coastguard Worker 		memcpy(t->ioact, iops, iopn * sizeof(struct ioword *));
516*7c356e86SAndroid Build Coastguard Worker 	}
517*7c356e86SAndroid Build Coastguard Worker 
518*7c356e86SAndroid Build Coastguard Worker 	if (t->type == TCOM || t->type == TDBRACKET) {
519*7c356e86SAndroid Build Coastguard Worker 		XPput(args, NULL);
520*7c356e86SAndroid Build Coastguard Worker 		t->args = (const char **)XPclose(args);
521*7c356e86SAndroid Build Coastguard Worker 		XPput(vars, NULL);
522*7c356e86SAndroid Build Coastguard Worker 		t->vars = (char **)XPclose(vars);
523*7c356e86SAndroid Build Coastguard Worker 	} else {
524*7c356e86SAndroid Build Coastguard Worker 		XPfree(args);
525*7c356e86SAndroid Build Coastguard Worker 		XPfree(vars);
526*7c356e86SAndroid Build Coastguard Worker 	}
527*7c356e86SAndroid Build Coastguard Worker 
528*7c356e86SAndroid Build Coastguard Worker 	if (c == MDPAREN) {
529*7c356e86SAndroid Build Coastguard Worker 		t = block(TBRACE, t, NULL);
530*7c356e86SAndroid Build Coastguard Worker 		t->ioact = t->left->ioact;
531*7c356e86SAndroid Build Coastguard Worker 		t->left->ioact = NULL;
532*7c356e86SAndroid Build Coastguard Worker 	}
533*7c356e86SAndroid Build Coastguard Worker 
534*7c356e86SAndroid Build Coastguard Worker 	return (t);
535*7c356e86SAndroid Build Coastguard Worker }
536*7c356e86SAndroid Build Coastguard Worker 
537*7c356e86SAndroid Build Coastguard Worker static struct op *
dogroup(int sALIAS)538*7c356e86SAndroid Build Coastguard Worker dogroup(int sALIAS)
539*7c356e86SAndroid Build Coastguard Worker {
540*7c356e86SAndroid Build Coastguard Worker 	int c;
541*7c356e86SAndroid Build Coastguard Worker 	struct op *list;
542*7c356e86SAndroid Build Coastguard Worker 
543*7c356e86SAndroid Build Coastguard Worker 	c = token(CONTIN|KEYWORD|sALIAS);
544*7c356e86SAndroid Build Coastguard Worker 	/*
545*7c356e86SAndroid Build Coastguard Worker 	 * A {...} can be used instead of do...done for for/select loops
546*7c356e86SAndroid Build Coastguard Worker 	 * but not for while/until loops - we don't need to check if it
547*7c356e86SAndroid Build Coastguard Worker 	 * is a while loop because it would have been parsed as part of
548*7c356e86SAndroid Build Coastguard Worker 	 * the conditional command list...
549*7c356e86SAndroid Build Coastguard Worker 	 */
550*7c356e86SAndroid Build Coastguard Worker 	if (c == DO)
551*7c356e86SAndroid Build Coastguard Worker 		c = DONE;
552*7c356e86SAndroid Build Coastguard Worker 	else if ((unsigned int)c == ORD('{'))
553*7c356e86SAndroid Build Coastguard Worker 		c = ORD('}');
554*7c356e86SAndroid Build Coastguard Worker 	else
555*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);
556*7c356e86SAndroid Build Coastguard Worker 	list = c_list(sALIAS, true);
557*7c356e86SAndroid Build Coastguard Worker 	musthave(c, KEYWORD|sALIAS);
558*7c356e86SAndroid Build Coastguard Worker 	return (list);
559*7c356e86SAndroid Build Coastguard Worker }
560*7c356e86SAndroid Build Coastguard Worker 
561*7c356e86SAndroid Build Coastguard Worker static struct op *
thenpart(int sALIAS)562*7c356e86SAndroid Build Coastguard Worker thenpart(int sALIAS)
563*7c356e86SAndroid Build Coastguard Worker {
564*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
565*7c356e86SAndroid Build Coastguard Worker 
566*7c356e86SAndroid Build Coastguard Worker 	musthave(THEN, KEYWORD|sALIAS);
567*7c356e86SAndroid Build Coastguard Worker 	t = newtp(0);
568*7c356e86SAndroid Build Coastguard Worker 	t->left = c_list(sALIAS, true);
569*7c356e86SAndroid Build Coastguard Worker 	if (t->left == NULL)
570*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);
571*7c356e86SAndroid Build Coastguard Worker 	t->right = elsepart(sALIAS);
572*7c356e86SAndroid Build Coastguard Worker 	return (t);
573*7c356e86SAndroid Build Coastguard Worker }
574*7c356e86SAndroid Build Coastguard Worker 
575*7c356e86SAndroid Build Coastguard Worker static struct op *
elsepart(int sALIAS)576*7c356e86SAndroid Build Coastguard Worker elsepart(int sALIAS)
577*7c356e86SAndroid Build Coastguard Worker {
578*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
579*7c356e86SAndroid Build Coastguard Worker 
580*7c356e86SAndroid Build Coastguard Worker 	switch (token(KEYWORD|sALIAS|CMDASN)) {
581*7c356e86SAndroid Build Coastguard Worker 	case ELSE:
582*7c356e86SAndroid Build Coastguard Worker 		if ((t = c_list(sALIAS, true)) == NULL)
583*7c356e86SAndroid Build Coastguard Worker 			syntaxerr(NULL);
584*7c356e86SAndroid Build Coastguard Worker 		return (t);
585*7c356e86SAndroid Build Coastguard Worker 
586*7c356e86SAndroid Build Coastguard Worker 	case ELIF:
587*7c356e86SAndroid Build Coastguard Worker 		t = newtp(TELIF);
588*7c356e86SAndroid Build Coastguard Worker 		t->left = c_list(sALIAS, true);
589*7c356e86SAndroid Build Coastguard Worker 		t->right = thenpart(sALIAS);
590*7c356e86SAndroid Build Coastguard Worker 		return (t);
591*7c356e86SAndroid Build Coastguard Worker 
592*7c356e86SAndroid Build Coastguard Worker 	default:
593*7c356e86SAndroid Build Coastguard Worker 		REJECT;
594*7c356e86SAndroid Build Coastguard Worker 	}
595*7c356e86SAndroid Build Coastguard Worker 	return (NULL);
596*7c356e86SAndroid Build Coastguard Worker }
597*7c356e86SAndroid Build Coastguard Worker 
598*7c356e86SAndroid Build Coastguard Worker static struct op *
caselist(int sALIAS)599*7c356e86SAndroid Build Coastguard Worker caselist(int sALIAS)
600*7c356e86SAndroid Build Coastguard Worker {
601*7c356e86SAndroid Build Coastguard Worker 	struct op *t, *tl;
602*7c356e86SAndroid Build Coastguard Worker 	int c;
603*7c356e86SAndroid Build Coastguard Worker 
604*7c356e86SAndroid Build Coastguard Worker 	c = token(CONTIN|KEYWORD|sALIAS);
605*7c356e86SAndroid Build Coastguard Worker 	/* A {...} can be used instead of in...esac for case statements */
606*7c356e86SAndroid Build Coastguard Worker 	if (c == IN)
607*7c356e86SAndroid Build Coastguard Worker 		c = ESAC;
608*7c356e86SAndroid Build Coastguard Worker 	else if ((unsigned int)c == ORD('{'))
609*7c356e86SAndroid Build Coastguard Worker 		c = ORD('}');
610*7c356e86SAndroid Build Coastguard Worker 	else
611*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);
612*7c356e86SAndroid Build Coastguard Worker 	t = tl = NULL;
613*7c356e86SAndroid Build Coastguard Worker 	/* no ALIAS here */
614*7c356e86SAndroid Build Coastguard Worker 	while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) {
615*7c356e86SAndroid Build Coastguard Worker 		struct op *tc = casepart(c, sALIAS);
616*7c356e86SAndroid Build Coastguard Worker 		if (tl == NULL)
617*7c356e86SAndroid Build Coastguard Worker 			t = tl = tc, tl->right = NULL;
618*7c356e86SAndroid Build Coastguard Worker 		else
619*7c356e86SAndroid Build Coastguard Worker 			tl->right = tc, tl = tc;
620*7c356e86SAndroid Build Coastguard Worker 	}
621*7c356e86SAndroid Build Coastguard Worker 	musthave(c, KEYWORD|sALIAS);
622*7c356e86SAndroid Build Coastguard Worker 	return (t);
623*7c356e86SAndroid Build Coastguard Worker }
624*7c356e86SAndroid Build Coastguard Worker 
625*7c356e86SAndroid Build Coastguard Worker static struct op *
casepart(int endtok,int sALIAS)626*7c356e86SAndroid Build Coastguard Worker casepart(int endtok, int sALIAS)
627*7c356e86SAndroid Build Coastguard Worker {
628*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
629*7c356e86SAndroid Build Coastguard Worker 	XPtrV ptns;
630*7c356e86SAndroid Build Coastguard Worker 
631*7c356e86SAndroid Build Coastguard Worker 	XPinit(ptns, 16);
632*7c356e86SAndroid Build Coastguard Worker 	t = newtp(TPAT);
633*7c356e86SAndroid Build Coastguard Worker 	/* no ALIAS here */
634*7c356e86SAndroid Build Coastguard Worker 	if ((unsigned int)token(CONTIN | KEYWORD) != ORD('('))
635*7c356e86SAndroid Build Coastguard Worker 		REJECT;
636*7c356e86SAndroid Build Coastguard Worker 	do {
637*7c356e86SAndroid Build Coastguard Worker 		switch (token(0)) {
638*7c356e86SAndroid Build Coastguard Worker 		case LWORD:
639*7c356e86SAndroid Build Coastguard Worker 			break;
640*7c356e86SAndroid Build Coastguard Worker 		case ORD('}'):
641*7c356e86SAndroid Build Coastguard Worker 		case ESAC:
642*7c356e86SAndroid Build Coastguard Worker 			if (symbol != endtok) {
643*7c356e86SAndroid Build Coastguard Worker 				strdupx(yylval.cp, (unsigned int)symbol ==
644*7c356e86SAndroid Build Coastguard Worker 				    ORD('}') ? Tcbrace : Tesac, ATEMP);
645*7c356e86SAndroid Build Coastguard Worker 				break;
646*7c356e86SAndroid Build Coastguard Worker 			}
647*7c356e86SAndroid Build Coastguard Worker 			/* FALLTHROUGH */
648*7c356e86SAndroid Build Coastguard Worker 		default:
649*7c356e86SAndroid Build Coastguard Worker 			syntaxerr(NULL);
650*7c356e86SAndroid Build Coastguard Worker 		}
651*7c356e86SAndroid Build Coastguard Worker 		XPput(ptns, yylval.cp);
652*7c356e86SAndroid Build Coastguard Worker 	} while (token(0) == '|');
653*7c356e86SAndroid Build Coastguard Worker 	REJECT;
654*7c356e86SAndroid Build Coastguard Worker 	XPput(ptns, NULL);
655*7c356e86SAndroid Build Coastguard Worker 	t->vars = (char **)XPclose(ptns);
656*7c356e86SAndroid Build Coastguard Worker 	musthave(ORD(')'), 0);
657*7c356e86SAndroid Build Coastguard Worker 
658*7c356e86SAndroid Build Coastguard Worker 	t->left = c_list(sALIAS, true);
659*7c356e86SAndroid Build Coastguard Worker 
660*7c356e86SAndroid Build Coastguard Worker 	/* initialise to default for ;; or omitted */
661*7c356e86SAndroid Build Coastguard Worker 	t->u.charflag = ORD(';');
662*7c356e86SAndroid Build Coastguard Worker 	/* SUSv4 requires the ;; except in the last casepart */
663*7c356e86SAndroid Build Coastguard Worker 	if ((tpeek(CONTIN|KEYWORD|sALIAS)) != endtok)
664*7c356e86SAndroid Build Coastguard Worker 		switch (symbol) {
665*7c356e86SAndroid Build Coastguard Worker 		default:
666*7c356e86SAndroid Build Coastguard Worker 			syntaxerr(NULL);
667*7c356e86SAndroid Build Coastguard Worker 		case BRKEV:
668*7c356e86SAndroid Build Coastguard Worker 			t->u.charflag = ORD('|');
669*7c356e86SAndroid Build Coastguard Worker 			if (0)
670*7c356e86SAndroid Build Coastguard Worker 				/* FALLTHROUGH */
671*7c356e86SAndroid Build Coastguard Worker 		case BRKFT:
672*7c356e86SAndroid Build Coastguard Worker 			  t->u.charflag = ORD('&');
673*7c356e86SAndroid Build Coastguard Worker 			/* FALLTHROUGH */
674*7c356e86SAndroid Build Coastguard Worker 		case BREAK:
675*7c356e86SAndroid Build Coastguard Worker 			/* initialised above, but we need to eat the token */
676*7c356e86SAndroid Build Coastguard Worker 			ACCEPT;
677*7c356e86SAndroid Build Coastguard Worker 		}
678*7c356e86SAndroid Build Coastguard Worker 	return (t);
679*7c356e86SAndroid Build Coastguard Worker }
680*7c356e86SAndroid Build Coastguard Worker 
681*7c356e86SAndroid Build Coastguard Worker static struct op *
function_body(char * name,int sALIAS,bool ksh_func)682*7c356e86SAndroid Build Coastguard Worker function_body(char *name, int sALIAS,
683*7c356e86SAndroid Build Coastguard Worker     /* function foo { ... } vs foo() { .. } */
684*7c356e86SAndroid Build Coastguard Worker     bool ksh_func)
685*7c356e86SAndroid Build Coastguard Worker {
686*7c356e86SAndroid Build Coastguard Worker 	char *sname, *p;
687*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
688*7c356e86SAndroid Build Coastguard Worker 
689*7c356e86SAndroid Build Coastguard Worker 	sname = wdstrip(name, 0);
690*7c356e86SAndroid Build Coastguard Worker 	/*-
691*7c356e86SAndroid Build Coastguard Worker 	 * Check for valid characters in name. POSIX and AT&T ksh93 say
692*7c356e86SAndroid Build Coastguard Worker 	 * only allow [a-zA-Z_0-9] but this allows more as old pdkshs
693*7c356e86SAndroid Build Coastguard Worker 	 * have allowed more; the following were never allowed:
694*7c356e86SAndroid Build Coastguard Worker 	 *	NUL TAB NL SP " $ & ' ( ) ; < = > \ ` |
695*7c356e86SAndroid Build Coastguard Worker 	 * C_QUOTE|C_SPC covers all but adds # * ? [ ]
696*7c356e86SAndroid Build Coastguard Worker 	 */
697*7c356e86SAndroid Build Coastguard Worker 	for (p = sname; *p; p++)
698*7c356e86SAndroid Build Coastguard Worker 		if (ctype(*p, C_QUOTE | C_SPC))
699*7c356e86SAndroid Build Coastguard Worker 			yyerror(Tinvname, sname, Tfunction);
700*7c356e86SAndroid Build Coastguard Worker 
701*7c356e86SAndroid Build Coastguard Worker 	/*
702*7c356e86SAndroid Build Coastguard Worker 	 * Note that POSIX allows only compound statements after foo(),
703*7c356e86SAndroid Build Coastguard Worker 	 * sh and AT&T ksh allow any command, go with the later since it
704*7c356e86SAndroid Build Coastguard Worker 	 * shouldn't break anything. However, for function foo, AT&T ksh
705*7c356e86SAndroid Build Coastguard Worker 	 * only accepts an open-brace.
706*7c356e86SAndroid Build Coastguard Worker 	 */
707*7c356e86SAndroid Build Coastguard Worker 	if (ksh_func) {
708*7c356e86SAndroid Build Coastguard Worker 		if ((unsigned int)tpeek(CONTIN|KEYWORD|sALIAS) == ORD('(' /*)*/)) {
709*7c356e86SAndroid Build Coastguard Worker 			/* function foo () { //}*/
710*7c356e86SAndroid Build Coastguard Worker 			ACCEPT;
711*7c356e86SAndroid Build Coastguard Worker 			musthave(ORD(/*(*/ ')'), 0);
712*7c356e86SAndroid Build Coastguard Worker 			/* degrade to POSIX function */
713*7c356e86SAndroid Build Coastguard Worker 			ksh_func = false;
714*7c356e86SAndroid Build Coastguard Worker 		}
715*7c356e86SAndroid Build Coastguard Worker 		musthave(ORD('{' /*}*/), CONTIN|KEYWORD|sALIAS);
716*7c356e86SAndroid Build Coastguard Worker 		REJECT;
717*7c356e86SAndroid Build Coastguard Worker 	}
718*7c356e86SAndroid Build Coastguard Worker 
719*7c356e86SAndroid Build Coastguard Worker 	t = newtp(TFUNCT);
720*7c356e86SAndroid Build Coastguard Worker 	t->str = sname;
721*7c356e86SAndroid Build Coastguard Worker 	t->u.ksh_func = tobool(ksh_func);
722*7c356e86SAndroid Build Coastguard Worker 	t->lineno = source->line;
723*7c356e86SAndroid Build Coastguard Worker 
724*7c356e86SAndroid Build Coastguard Worker 	if ((t->left = get_command(CONTIN, sALIAS)) == NULL) {
725*7c356e86SAndroid Build Coastguard Worker 		char *tv;
726*7c356e86SAndroid Build Coastguard Worker 		/*
727*7c356e86SAndroid Build Coastguard Worker 		 * Probably something like foo() followed by EOF or ';'.
728*7c356e86SAndroid Build Coastguard Worker 		 * This is accepted by sh and ksh88.
729*7c356e86SAndroid Build Coastguard Worker 		 * To make "typeset -f foo" work reliably (so its output can
730*7c356e86SAndroid Build Coastguard Worker 		 * be used as input), we pretend there is a colon here.
731*7c356e86SAndroid Build Coastguard Worker 		 */
732*7c356e86SAndroid Build Coastguard Worker 		t->left = newtp(TCOM);
733*7c356e86SAndroid Build Coastguard Worker 		/* (2 * sizeof(char *)) is small enough */
734*7c356e86SAndroid Build Coastguard Worker 		t->left->args = alloc(2 * sizeof(char *), ATEMP);
735*7c356e86SAndroid Build Coastguard Worker 		t->left->args[0] = tv = alloc(3, ATEMP);
736*7c356e86SAndroid Build Coastguard Worker 		tv[0] = QCHAR;
737*7c356e86SAndroid Build Coastguard Worker 		tv[1] = ':';
738*7c356e86SAndroid Build Coastguard Worker 		tv[2] = EOS;
739*7c356e86SAndroid Build Coastguard Worker 		t->left->args[1] = NULL;
740*7c356e86SAndroid Build Coastguard Worker 		t->left->vars = alloc(sizeof(char *), ATEMP);
741*7c356e86SAndroid Build Coastguard Worker 		t->left->vars[0] = NULL;
742*7c356e86SAndroid Build Coastguard Worker 		t->left->lineno = 1;
743*7c356e86SAndroid Build Coastguard Worker 	}
744*7c356e86SAndroid Build Coastguard Worker 
745*7c356e86SAndroid Build Coastguard Worker 	return (t);
746*7c356e86SAndroid Build Coastguard Worker }
747*7c356e86SAndroid Build Coastguard Worker 
748*7c356e86SAndroid Build Coastguard Worker static char **
wordlist(int sALIAS)749*7c356e86SAndroid Build Coastguard Worker wordlist(int sALIAS)
750*7c356e86SAndroid Build Coastguard Worker {
751*7c356e86SAndroid Build Coastguard Worker 	int c;
752*7c356e86SAndroid Build Coastguard Worker 	XPtrV args;
753*7c356e86SAndroid Build Coastguard Worker 
754*7c356e86SAndroid Build Coastguard Worker 	XPinit(args, 16);
755*7c356e86SAndroid Build Coastguard Worker 	/* POSIX does not do alias expansion here... */
756*7c356e86SAndroid Build Coastguard Worker 	if ((c = token(CONTIN|KEYWORD|sALIAS)) != IN) {
757*7c356e86SAndroid Build Coastguard Worker 		if (c != ';')
758*7c356e86SAndroid Build Coastguard Worker 			/* non-POSIX, but AT&T ksh accepts a ; here */
759*7c356e86SAndroid Build Coastguard Worker 			REJECT;
760*7c356e86SAndroid Build Coastguard Worker 		return (NULL);
761*7c356e86SAndroid Build Coastguard Worker 	}
762*7c356e86SAndroid Build Coastguard Worker 	while ((c = token(0)) == LWORD)
763*7c356e86SAndroid Build Coastguard Worker 		XPput(args, yylval.cp);
764*7c356e86SAndroid Build Coastguard Worker 	if (c != '\n' && c != ';')
765*7c356e86SAndroid Build Coastguard Worker 		syntaxerr(NULL);
766*7c356e86SAndroid Build Coastguard Worker 	XPput(args, NULL);
767*7c356e86SAndroid Build Coastguard Worker 	return ((char **)XPclose(args));
768*7c356e86SAndroid Build Coastguard Worker }
769*7c356e86SAndroid Build Coastguard Worker 
770*7c356e86SAndroid Build Coastguard Worker /*
771*7c356e86SAndroid Build Coastguard Worker  * supporting functions
772*7c356e86SAndroid Build Coastguard Worker  */
773*7c356e86SAndroid Build Coastguard Worker 
774*7c356e86SAndroid Build Coastguard Worker static struct op *
block(int type,struct op * t1,struct op * t2)775*7c356e86SAndroid Build Coastguard Worker block(int type, struct op *t1, struct op *t2)
776*7c356e86SAndroid Build Coastguard Worker {
777*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
778*7c356e86SAndroid Build Coastguard Worker 
779*7c356e86SAndroid Build Coastguard Worker 	t = newtp(type);
780*7c356e86SAndroid Build Coastguard Worker 	t->left = t1;
781*7c356e86SAndroid Build Coastguard Worker 	t->right = t2;
782*7c356e86SAndroid Build Coastguard Worker 	return (t);
783*7c356e86SAndroid Build Coastguard Worker }
784*7c356e86SAndroid Build Coastguard Worker 
785*7c356e86SAndroid Build Coastguard Worker static const struct tokeninfo {
786*7c356e86SAndroid Build Coastguard Worker 	const char *name;
787*7c356e86SAndroid Build Coastguard Worker 	short val;
788*7c356e86SAndroid Build Coastguard Worker 	short reserved;
789*7c356e86SAndroid Build Coastguard Worker } tokentab[] = {
790*7c356e86SAndroid Build Coastguard Worker 	/* Reserved words */
791*7c356e86SAndroid Build Coastguard Worker 	{ "if",		IF,	true },
792*7c356e86SAndroid Build Coastguard Worker 	{ "then",	THEN,	true },
793*7c356e86SAndroid Build Coastguard Worker 	{ "else",	ELSE,	true },
794*7c356e86SAndroid Build Coastguard Worker 	{ "elif",	ELIF,	true },
795*7c356e86SAndroid Build Coastguard Worker 	{ "fi",		FI,	true },
796*7c356e86SAndroid Build Coastguard Worker 	{ "case",	CASE,	true },
797*7c356e86SAndroid Build Coastguard Worker 	{ Tesac,	ESAC,	true },
798*7c356e86SAndroid Build Coastguard Worker 	{ "for",	FOR,	true },
799*7c356e86SAndroid Build Coastguard Worker 	{ Tselect,	SELECT,	true },
800*7c356e86SAndroid Build Coastguard Worker 	{ "while",	WHILE,	true },
801*7c356e86SAndroid Build Coastguard Worker 	{ "until",	UNTIL,	true },
802*7c356e86SAndroid Build Coastguard Worker 	{ "do",		DO,	true },
803*7c356e86SAndroid Build Coastguard Worker 	{ "done",	DONE,	true },
804*7c356e86SAndroid Build Coastguard Worker 	{ "in",		IN,	true },
805*7c356e86SAndroid Build Coastguard Worker 	{ Tfunction,	FUNCTION, true },
806*7c356e86SAndroid Build Coastguard Worker 	{ Ttime,	TIME,	true },
807*7c356e86SAndroid Build Coastguard Worker 	{ "{",		ORD('{'), true },
808*7c356e86SAndroid Build Coastguard Worker 	{ Tcbrace,	ORD('}'), true },
809*7c356e86SAndroid Build Coastguard Worker 	{ "!",		BANG,	true },
810*7c356e86SAndroid Build Coastguard Worker 	{ "[[",		DBRACKET, true },
811*7c356e86SAndroid Build Coastguard Worker 	/* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */
812*7c356e86SAndroid Build Coastguard Worker 	{ "&&",		LOGAND,	false },
813*7c356e86SAndroid Build Coastguard Worker 	{ "||",		LOGOR,	false },
814*7c356e86SAndroid Build Coastguard Worker 	{ ";;",		BREAK,	false },
815*7c356e86SAndroid Build Coastguard Worker 	{ ";|",		BRKEV,	false },
816*7c356e86SAndroid Build Coastguard Worker 	{ ";&",		BRKFT,	false },
817*7c356e86SAndroid Build Coastguard Worker 	{ "((",		MDPAREN, false },
818*7c356e86SAndroid Build Coastguard Worker 	{ "|&",		COPROC,	false },
819*7c356e86SAndroid Build Coastguard Worker 	/* and some special cases... */
820*7c356e86SAndroid Build Coastguard Worker 	{ "newline",	ORD('\n'), false },
821*7c356e86SAndroid Build Coastguard Worker 	{ NULL,		0,	false }
822*7c356e86SAndroid Build Coastguard Worker };
823*7c356e86SAndroid Build Coastguard Worker 
824*7c356e86SAndroid Build Coastguard Worker void
initkeywords(void)825*7c356e86SAndroid Build Coastguard Worker initkeywords(void)
826*7c356e86SAndroid Build Coastguard Worker {
827*7c356e86SAndroid Build Coastguard Worker 	struct tokeninfo const *tt;
828*7c356e86SAndroid Build Coastguard Worker 	struct tbl *p;
829*7c356e86SAndroid Build Coastguard Worker 
830*7c356e86SAndroid Build Coastguard Worker 	ktinit(APERM, &keywords,
831*7c356e86SAndroid Build Coastguard Worker 	    /* currently 28 keywords: 75% of 64 = 2^6 */
832*7c356e86SAndroid Build Coastguard Worker 	    6);
833*7c356e86SAndroid Build Coastguard Worker 	for (tt = tokentab; tt->name; tt++) {
834*7c356e86SAndroid Build Coastguard Worker 		if (tt->reserved) {
835*7c356e86SAndroid Build Coastguard Worker 			p = ktenter(&keywords, tt->name, hash(tt->name));
836*7c356e86SAndroid Build Coastguard Worker 			p->flag |= DEFINED|ISSET;
837*7c356e86SAndroid Build Coastguard Worker 			p->type = CKEYWD;
838*7c356e86SAndroid Build Coastguard Worker 			p->val.i = tt->val;
839*7c356e86SAndroid Build Coastguard Worker 		}
840*7c356e86SAndroid Build Coastguard Worker 	}
841*7c356e86SAndroid Build Coastguard Worker }
842*7c356e86SAndroid Build Coastguard Worker 
843*7c356e86SAndroid Build Coastguard Worker static void
syntaxerr(const char * what)844*7c356e86SAndroid Build Coastguard Worker syntaxerr(const char *what)
845*7c356e86SAndroid Build Coastguard Worker {
846*7c356e86SAndroid Build Coastguard Worker 	/* 23<<- is the longest redirection, I think */
847*7c356e86SAndroid Build Coastguard Worker 	char redir[8];
848*7c356e86SAndroid Build Coastguard Worker 	const char *s;
849*7c356e86SAndroid Build Coastguard Worker 	struct tokeninfo const *tt;
850*7c356e86SAndroid Build Coastguard Worker 	int c;
851*7c356e86SAndroid Build Coastguard Worker 
852*7c356e86SAndroid Build Coastguard Worker 	if (!what)
853*7c356e86SAndroid Build Coastguard Worker 		what = Tunexpected;
854*7c356e86SAndroid Build Coastguard Worker 	REJECT;
855*7c356e86SAndroid Build Coastguard Worker 	c = token(0);
856*7c356e86SAndroid Build Coastguard Worker  Again:
857*7c356e86SAndroid Build Coastguard Worker 	switch (c) {
858*7c356e86SAndroid Build Coastguard Worker 	case 0:
859*7c356e86SAndroid Build Coastguard Worker 		if (nesting.start_token) {
860*7c356e86SAndroid Build Coastguard Worker 			c = nesting.start_token;
861*7c356e86SAndroid Build Coastguard Worker 			source->errline = nesting.start_line;
862*7c356e86SAndroid Build Coastguard Worker 			what = "unmatched";
863*7c356e86SAndroid Build Coastguard Worker 			goto Again;
864*7c356e86SAndroid Build Coastguard Worker 		}
865*7c356e86SAndroid Build Coastguard Worker 		/* don't quote the EOF */
866*7c356e86SAndroid Build Coastguard Worker 		yyerror("%s: unexpected EOF", Tsynerr);
867*7c356e86SAndroid Build Coastguard Worker 		/* NOTREACHED */
868*7c356e86SAndroid Build Coastguard Worker 
869*7c356e86SAndroid Build Coastguard Worker 	case LWORD:
870*7c356e86SAndroid Build Coastguard Worker 		s = snptreef(NULL, 32, Tf_S, yylval.cp);
871*7c356e86SAndroid Build Coastguard Worker 		break;
872*7c356e86SAndroid Build Coastguard Worker 
873*7c356e86SAndroid Build Coastguard Worker 	case REDIR:
874*7c356e86SAndroid Build Coastguard Worker 		s = snptreef(redir, sizeof(redir), Tft_R, yylval.iop);
875*7c356e86SAndroid Build Coastguard Worker 		break;
876*7c356e86SAndroid Build Coastguard Worker 
877*7c356e86SAndroid Build Coastguard Worker 	default:
878*7c356e86SAndroid Build Coastguard Worker 		for (tt = tokentab; tt->name; tt++)
879*7c356e86SAndroid Build Coastguard Worker 			if (tt->val == c)
880*7c356e86SAndroid Build Coastguard Worker 			    break;
881*7c356e86SAndroid Build Coastguard Worker 		if (tt->name)
882*7c356e86SAndroid Build Coastguard Worker 			s = tt->name;
883*7c356e86SAndroid Build Coastguard Worker 		else {
884*7c356e86SAndroid Build Coastguard Worker 			if (c > 0 && c < 256) {
885*7c356e86SAndroid Build Coastguard Worker 				redir[0] = c;
886*7c356e86SAndroid Build Coastguard Worker 				redir[1] = '\0';
887*7c356e86SAndroid Build Coastguard Worker 			} else
888*7c356e86SAndroid Build Coastguard Worker 				shf_snprintf(redir, sizeof(redir),
889*7c356e86SAndroid Build Coastguard Worker 					"?%d", c);
890*7c356e86SAndroid Build Coastguard Worker 			s = redir;
891*7c356e86SAndroid Build Coastguard Worker 		}
892*7c356e86SAndroid Build Coastguard Worker 	}
893*7c356e86SAndroid Build Coastguard Worker 	yyerror(Tf_sD_s_qs, Tsynerr, what, s);
894*7c356e86SAndroid Build Coastguard Worker }
895*7c356e86SAndroid Build Coastguard Worker 
896*7c356e86SAndroid Build Coastguard Worker static void
nesting_push(struct nesting_state * save,int tok)897*7c356e86SAndroid Build Coastguard Worker nesting_push(struct nesting_state *save, int tok)
898*7c356e86SAndroid Build Coastguard Worker {
899*7c356e86SAndroid Build Coastguard Worker 	*save = nesting;
900*7c356e86SAndroid Build Coastguard Worker 	nesting.start_token = tok;
901*7c356e86SAndroid Build Coastguard Worker 	nesting.start_line = source->line;
902*7c356e86SAndroid Build Coastguard Worker }
903*7c356e86SAndroid Build Coastguard Worker 
904*7c356e86SAndroid Build Coastguard Worker static void
nesting_pop(struct nesting_state * saved)905*7c356e86SAndroid Build Coastguard Worker nesting_pop(struct nesting_state *saved)
906*7c356e86SAndroid Build Coastguard Worker {
907*7c356e86SAndroid Build Coastguard Worker 	nesting = *saved;
908*7c356e86SAndroid Build Coastguard Worker }
909*7c356e86SAndroid Build Coastguard Worker 
910*7c356e86SAndroid Build Coastguard Worker static struct op *
newtp(int type)911*7c356e86SAndroid Build Coastguard Worker newtp(int type)
912*7c356e86SAndroid Build Coastguard Worker {
913*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
914*7c356e86SAndroid Build Coastguard Worker 
915*7c356e86SAndroid Build Coastguard Worker 	t = alloc(sizeof(struct op), ATEMP);
916*7c356e86SAndroid Build Coastguard Worker 	t->type = type;
917*7c356e86SAndroid Build Coastguard Worker 	t->u.evalflags = 0;
918*7c356e86SAndroid Build Coastguard Worker 	t->args = NULL;
919*7c356e86SAndroid Build Coastguard Worker 	t->vars = NULL;
920*7c356e86SAndroid Build Coastguard Worker 	t->ioact = NULL;
921*7c356e86SAndroid Build Coastguard Worker 	t->left = t->right = NULL;
922*7c356e86SAndroid Build Coastguard Worker 	t->str = NULL;
923*7c356e86SAndroid Build Coastguard Worker 	return (t);
924*7c356e86SAndroid Build Coastguard Worker }
925*7c356e86SAndroid Build Coastguard Worker 
926*7c356e86SAndroid Build Coastguard Worker struct op *
compile(Source * s,bool skiputf8bom,bool doalias)927*7c356e86SAndroid Build Coastguard Worker compile(Source *s, bool skiputf8bom, bool doalias)
928*7c356e86SAndroid Build Coastguard Worker {
929*7c356e86SAndroid Build Coastguard Worker 	nesting.start_token = 0;
930*7c356e86SAndroid Build Coastguard Worker 	nesting.start_line = 0;
931*7c356e86SAndroid Build Coastguard Worker 	herep = heres;
932*7c356e86SAndroid Build Coastguard Worker 	source = s;
933*7c356e86SAndroid Build Coastguard Worker 	if (skiputf8bom)
934*7c356e86SAndroid Build Coastguard Worker 		yyskiputf8bom();
935*7c356e86SAndroid Build Coastguard Worker 	yyparse(doalias);
936*7c356e86SAndroid Build Coastguard Worker 	return (outtree);
937*7c356e86SAndroid Build Coastguard Worker }
938*7c356e86SAndroid Build Coastguard Worker 
939*7c356e86SAndroid Build Coastguard Worker /* Check if we are in the middle of reading an alias */
940*7c356e86SAndroid Build Coastguard Worker static int
inalias(struct source * s)941*7c356e86SAndroid Build Coastguard Worker inalias(struct source *s)
942*7c356e86SAndroid Build Coastguard Worker {
943*7c356e86SAndroid Build Coastguard Worker 	while (s && s->type == SALIAS) {
944*7c356e86SAndroid Build Coastguard Worker 		if (!(s->flags & SF_ALIASEND))
945*7c356e86SAndroid Build Coastguard Worker 			return (1);
946*7c356e86SAndroid Build Coastguard Worker 		s = s->next;
947*7c356e86SAndroid Build Coastguard Worker 	}
948*7c356e86SAndroid Build Coastguard Worker 	return (0);
949*7c356e86SAndroid Build Coastguard Worker }
950*7c356e86SAndroid Build Coastguard Worker 
951*7c356e86SAndroid Build Coastguard Worker 
952*7c356e86SAndroid Build Coastguard Worker /*
953*7c356e86SAndroid Build Coastguard Worker  * Order important - indexed by Test_meta values
954*7c356e86SAndroid Build Coastguard Worker  * Note that ||, &&, ( and ) can't appear in as unquoted strings
955*7c356e86SAndroid Build Coastguard Worker  * in normal shell input, so these can be interpreted unambiguously
956*7c356e86SAndroid Build Coastguard Worker  * in the evaluation pass.
957*7c356e86SAndroid Build Coastguard Worker  */
958*7c356e86SAndroid Build Coastguard Worker static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS };
959*7c356e86SAndroid Build Coastguard Worker static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS };
960*7c356e86SAndroid Build Coastguard Worker static const char dbtest_not[] = { CHAR, '!', EOS };
961*7c356e86SAndroid Build Coastguard Worker static const char dbtest_oparen[] = { CHAR, '(', EOS };
962*7c356e86SAndroid Build Coastguard Worker static const char dbtest_cparen[] = { CHAR, ')', EOS };
963*7c356e86SAndroid Build Coastguard Worker const char * const dbtest_tokens[] = {
964*7c356e86SAndroid Build Coastguard Worker 	dbtest_or, dbtest_and, dbtest_not,
965*7c356e86SAndroid Build Coastguard Worker 	dbtest_oparen, dbtest_cparen
966*7c356e86SAndroid Build Coastguard Worker };
967*7c356e86SAndroid Build Coastguard Worker static const char db_close[] = { CHAR, ']', CHAR, ']', EOS };
968*7c356e86SAndroid Build Coastguard Worker static const char db_lthan[] = { CHAR, '<', EOS };
969*7c356e86SAndroid Build Coastguard Worker static const char db_gthan[] = { CHAR, '>', EOS };
970*7c356e86SAndroid Build Coastguard Worker 
971*7c356e86SAndroid Build Coastguard Worker /*
972*7c356e86SAndroid Build Coastguard Worker  * Test if the current token is a whatever. Accepts the current token if
973*7c356e86SAndroid Build Coastguard Worker  * it is. Returns 0 if it is not, non-zero if it is (in the case of
974*7c356e86SAndroid Build Coastguard Worker  * TM_UNOP and TM_BINOP, the returned value is a Test_op).
975*7c356e86SAndroid Build Coastguard Worker  */
976*7c356e86SAndroid Build Coastguard Worker static Test_op
dbtestp_isa(Test_env * te,Test_meta meta)977*7c356e86SAndroid Build Coastguard Worker dbtestp_isa(Test_env *te, Test_meta meta)
978*7c356e86SAndroid Build Coastguard Worker {
979*7c356e86SAndroid Build Coastguard Worker 	int c = tpeek(CMDASN | (meta == TM_BINOP ? 0 : CONTIN));
980*7c356e86SAndroid Build Coastguard Worker 	bool uqword;
981*7c356e86SAndroid Build Coastguard Worker 	char *save = NULL;
982*7c356e86SAndroid Build Coastguard Worker 	Test_op ret = TO_NONOP;
983*7c356e86SAndroid Build Coastguard Worker 
984*7c356e86SAndroid Build Coastguard Worker 	/* unquoted word? */
985*7c356e86SAndroid Build Coastguard Worker 	uqword = c == LWORD && *ident;
986*7c356e86SAndroid Build Coastguard Worker 
987*7c356e86SAndroid Build Coastguard Worker 	if (meta == TM_OR)
988*7c356e86SAndroid Build Coastguard Worker 		ret = c == LOGOR ? TO_NONNULL : TO_NONOP;
989*7c356e86SAndroid Build Coastguard Worker 	else if (meta == TM_AND)
990*7c356e86SAndroid Build Coastguard Worker 		ret = c == LOGAND ? TO_NONNULL : TO_NONOP;
991*7c356e86SAndroid Build Coastguard Worker 	else if (meta == TM_NOT)
992*7c356e86SAndroid Build Coastguard Worker 		ret = (uqword && !strcmp(yylval.cp,
993*7c356e86SAndroid Build Coastguard Worker 		    dbtest_tokens[(int)TM_NOT])) ? TO_NONNULL : TO_NONOP;
994*7c356e86SAndroid Build Coastguard Worker 	else if (meta == TM_OPAREN)
995*7c356e86SAndroid Build Coastguard Worker 		ret = (unsigned int)c == ORD('(') /*)*/ ? TO_NONNULL : TO_NONOP;
996*7c356e86SAndroid Build Coastguard Worker 	else if (meta == TM_CPAREN)
997*7c356e86SAndroid Build Coastguard Worker 		ret = (unsigned int)c == /*(*/ ORD(')') ? TO_NONNULL : TO_NONOP;
998*7c356e86SAndroid Build Coastguard Worker 	else if (meta == TM_UNOP || meta == TM_BINOP) {
999*7c356e86SAndroid Build Coastguard Worker 		if (meta == TM_BINOP && c == REDIR &&
1000*7c356e86SAndroid Build Coastguard Worker 		    (yylval.iop->ioflag == IOREAD ||
1001*7c356e86SAndroid Build Coastguard Worker 		    yylval.iop->ioflag == IOWRITE)) {
1002*7c356e86SAndroid Build Coastguard Worker 			ret = TO_NONNULL;
1003*7c356e86SAndroid Build Coastguard Worker 			save = wdcopy(yylval.iop->ioflag == IOREAD ?
1004*7c356e86SAndroid Build Coastguard Worker 			    db_lthan : db_gthan, ATEMP);
1005*7c356e86SAndroid Build Coastguard Worker 		} else if (uqword && (ret = test_isop(meta, ident)))
1006*7c356e86SAndroid Build Coastguard Worker 			save = yylval.cp;
1007*7c356e86SAndroid Build Coastguard Worker 	} else
1008*7c356e86SAndroid Build Coastguard Worker 		/* meta == TM_END */
1009*7c356e86SAndroid Build Coastguard Worker 		ret = (uqword && !strcmp(yylval.cp,
1010*7c356e86SAndroid Build Coastguard Worker 		    db_close)) ? TO_NONNULL : TO_NONOP;
1011*7c356e86SAndroid Build Coastguard Worker 	if (ret != TO_NONOP) {
1012*7c356e86SAndroid Build Coastguard Worker 		ACCEPT;
1013*7c356e86SAndroid Build Coastguard Worker 		if ((unsigned int)meta < NELEM(dbtest_tokens))
1014*7c356e86SAndroid Build Coastguard Worker 			save = wdcopy(dbtest_tokens[(int)meta], ATEMP);
1015*7c356e86SAndroid Build Coastguard Worker 		if (save)
1016*7c356e86SAndroid Build Coastguard Worker 			XPput(*te->pos.av, save);
1017*7c356e86SAndroid Build Coastguard Worker 	}
1018*7c356e86SAndroid Build Coastguard Worker 	return (ret);
1019*7c356e86SAndroid Build Coastguard Worker }
1020*7c356e86SAndroid Build Coastguard Worker 
1021*7c356e86SAndroid Build Coastguard Worker static const char *
dbtestp_getopnd(Test_env * te,Test_op op MKSH_A_UNUSED,bool do_eval MKSH_A_UNUSED)1022*7c356e86SAndroid Build Coastguard Worker dbtestp_getopnd(Test_env *te, Test_op op MKSH_A_UNUSED,
1023*7c356e86SAndroid Build Coastguard Worker     bool do_eval MKSH_A_UNUSED)
1024*7c356e86SAndroid Build Coastguard Worker {
1025*7c356e86SAndroid Build Coastguard Worker 	int c = tpeek(CMDASN);
1026*7c356e86SAndroid Build Coastguard Worker 
1027*7c356e86SAndroid Build Coastguard Worker 	if (c != LWORD)
1028*7c356e86SAndroid Build Coastguard Worker 		return (NULL);
1029*7c356e86SAndroid Build Coastguard Worker 
1030*7c356e86SAndroid Build Coastguard Worker 	ACCEPT;
1031*7c356e86SAndroid Build Coastguard Worker 	XPput(*te->pos.av, yylval.cp);
1032*7c356e86SAndroid Build Coastguard Worker 
1033*7c356e86SAndroid Build Coastguard Worker 	return (null);
1034*7c356e86SAndroid Build Coastguard Worker }
1035*7c356e86SAndroid Build Coastguard Worker 
1036*7c356e86SAndroid Build Coastguard Worker static int
dbtestp_eval(Test_env * te MKSH_A_UNUSED,Test_op op MKSH_A_UNUSED,const char * opnd1 MKSH_A_UNUSED,const char * opnd2 MKSH_A_UNUSED,bool do_eval MKSH_A_UNUSED)1037*7c356e86SAndroid Build Coastguard Worker dbtestp_eval(Test_env *te MKSH_A_UNUSED, Test_op op MKSH_A_UNUSED,
1038*7c356e86SAndroid Build Coastguard Worker     const char *opnd1 MKSH_A_UNUSED, const char *opnd2 MKSH_A_UNUSED,
1039*7c356e86SAndroid Build Coastguard Worker     bool do_eval MKSH_A_UNUSED)
1040*7c356e86SAndroid Build Coastguard Worker {
1041*7c356e86SAndroid Build Coastguard Worker 	return (1);
1042*7c356e86SAndroid Build Coastguard Worker }
1043*7c356e86SAndroid Build Coastguard Worker 
1044*7c356e86SAndroid Build Coastguard Worker static void
dbtestp_error(Test_env * te,int offset,const char * msg)1045*7c356e86SAndroid Build Coastguard Worker dbtestp_error(Test_env *te, int offset, const char *msg)
1046*7c356e86SAndroid Build Coastguard Worker {
1047*7c356e86SAndroid Build Coastguard Worker 	te->flags |= TEF_ERROR;
1048*7c356e86SAndroid Build Coastguard Worker 
1049*7c356e86SAndroid Build Coastguard Worker 	if (offset < 0) {
1050*7c356e86SAndroid Build Coastguard Worker 		REJECT;
1051*7c356e86SAndroid Build Coastguard Worker 		/* Kludgy to say the least... */
1052*7c356e86SAndroid Build Coastguard Worker 		symbol = LWORD;
1053*7c356e86SAndroid Build Coastguard Worker 		yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av) +
1054*7c356e86SAndroid Build Coastguard Worker 		    offset);
1055*7c356e86SAndroid Build Coastguard Worker 	}
1056*7c356e86SAndroid Build Coastguard Worker 	syntaxerr(msg);
1057*7c356e86SAndroid Build Coastguard Worker }
1058*7c356e86SAndroid Build Coastguard Worker 
1059*7c356e86SAndroid Build Coastguard Worker #if HAVE_SELECT
1060*7c356e86SAndroid Build Coastguard Worker 
1061*7c356e86SAndroid Build Coastguard Worker #ifndef EOVERFLOW
1062*7c356e86SAndroid Build Coastguard Worker #ifdef ERANGE
1063*7c356e86SAndroid Build Coastguard Worker #define EOVERFLOW	ERANGE
1064*7c356e86SAndroid Build Coastguard Worker #else
1065*7c356e86SAndroid Build Coastguard Worker #define EOVERFLOW	EINVAL
1066*7c356e86SAndroid Build Coastguard Worker #endif
1067*7c356e86SAndroid Build Coastguard Worker #endif
1068*7c356e86SAndroid Build Coastguard Worker 
1069*7c356e86SAndroid Build Coastguard Worker bool
parse_usec(const char * s,struct timeval * tv)1070*7c356e86SAndroid Build Coastguard Worker parse_usec(const char *s, struct timeval *tv)
1071*7c356e86SAndroid Build Coastguard Worker {
1072*7c356e86SAndroid Build Coastguard Worker 	struct timeval tt;
1073*7c356e86SAndroid Build Coastguard Worker 	int i;
1074*7c356e86SAndroid Build Coastguard Worker 
1075*7c356e86SAndroid Build Coastguard Worker 	tv->tv_sec = 0;
1076*7c356e86SAndroid Build Coastguard Worker 	/* parse integral part */
1077*7c356e86SAndroid Build Coastguard Worker 	while (ctype(*s, C_DIGIT)) {
1078*7c356e86SAndroid Build Coastguard Worker 		tt.tv_sec = tv->tv_sec * 10 + ksh_numdig(*s++);
1079*7c356e86SAndroid Build Coastguard Worker 		/*XXX this overflow check maybe UB */
1080*7c356e86SAndroid Build Coastguard Worker 		if (tt.tv_sec / 10 != tv->tv_sec) {
1081*7c356e86SAndroid Build Coastguard Worker 			errno = EOVERFLOW;
1082*7c356e86SAndroid Build Coastguard Worker 			return (true);
1083*7c356e86SAndroid Build Coastguard Worker 		}
1084*7c356e86SAndroid Build Coastguard Worker 		tv->tv_sec = tt.tv_sec;
1085*7c356e86SAndroid Build Coastguard Worker 	}
1086*7c356e86SAndroid Build Coastguard Worker 
1087*7c356e86SAndroid Build Coastguard Worker 	tv->tv_usec = 0;
1088*7c356e86SAndroid Build Coastguard Worker 	if (!*s)
1089*7c356e86SAndroid Build Coastguard Worker 		/* no decimal fraction */
1090*7c356e86SAndroid Build Coastguard Worker 		return (false);
1091*7c356e86SAndroid Build Coastguard Worker 	else if (*s++ != '.') {
1092*7c356e86SAndroid Build Coastguard Worker 		/* junk after integral part */
1093*7c356e86SAndroid Build Coastguard Worker 		errno = EINVAL;
1094*7c356e86SAndroid Build Coastguard Worker 		return (true);
1095*7c356e86SAndroid Build Coastguard Worker 	}
1096*7c356e86SAndroid Build Coastguard Worker 
1097*7c356e86SAndroid Build Coastguard Worker 	/* parse decimal fraction */
1098*7c356e86SAndroid Build Coastguard Worker 	i = 100000;
1099*7c356e86SAndroid Build Coastguard Worker 	while (ctype(*s, C_DIGIT)) {
1100*7c356e86SAndroid Build Coastguard Worker 		tv->tv_usec += i * ksh_numdig(*s++);
1101*7c356e86SAndroid Build Coastguard Worker 		if (i == 1)
1102*7c356e86SAndroid Build Coastguard Worker 			break;
1103*7c356e86SAndroid Build Coastguard Worker 		i /= 10;
1104*7c356e86SAndroid Build Coastguard Worker 	}
1105*7c356e86SAndroid Build Coastguard Worker 	/* check for junk after fractional part */
1106*7c356e86SAndroid Build Coastguard Worker 	while (ctype(*s, C_DIGIT))
1107*7c356e86SAndroid Build Coastguard Worker 		++s;
1108*7c356e86SAndroid Build Coastguard Worker 	if (*s) {
1109*7c356e86SAndroid Build Coastguard Worker 		errno = EINVAL;
1110*7c356e86SAndroid Build Coastguard Worker 		return (true);
1111*7c356e86SAndroid Build Coastguard Worker 	}
1112*7c356e86SAndroid Build Coastguard Worker 
1113*7c356e86SAndroid Build Coastguard Worker 	/* end of input string reached, no errors */
1114*7c356e86SAndroid Build Coastguard Worker 	return (false);
1115*7c356e86SAndroid Build Coastguard Worker }
1116*7c356e86SAndroid Build Coastguard Worker #endif
1117*7c356e86SAndroid Build Coastguard Worker 
1118*7c356e86SAndroid Build Coastguard Worker /*
1119*7c356e86SAndroid Build Coastguard Worker  * Helper function called from within lex.c:yylex() to parse
1120*7c356e86SAndroid Build Coastguard Worker  * a COMSUB recursively using the main shell parser and lexer
1121*7c356e86SAndroid Build Coastguard Worker  */
1122*7c356e86SAndroid Build Coastguard Worker char *
yyrecursive(int subtype)1123*7c356e86SAndroid Build Coastguard Worker yyrecursive(int subtype)
1124*7c356e86SAndroid Build Coastguard Worker {
1125*7c356e86SAndroid Build Coastguard Worker 	struct op *t;
1126*7c356e86SAndroid Build Coastguard Worker 	char *cp;
1127*7c356e86SAndroid Build Coastguard Worker 	struct yyrecursive_state *ys;
1128*7c356e86SAndroid Build Coastguard Worker 	unsigned int stok, etok;
1129*7c356e86SAndroid Build Coastguard Worker 
1130*7c356e86SAndroid Build Coastguard Worker 	if (subtype != COMSUB) {
1131*7c356e86SAndroid Build Coastguard Worker 		stok = ORD('{');
1132*7c356e86SAndroid Build Coastguard Worker 		etok = ORD('}');
1133*7c356e86SAndroid Build Coastguard Worker 	} else {
1134*7c356e86SAndroid Build Coastguard Worker 		stok = ORD('(');
1135*7c356e86SAndroid Build Coastguard Worker 		etok = ORD(')');
1136*7c356e86SAndroid Build Coastguard Worker 	}
1137*7c356e86SAndroid Build Coastguard Worker 
1138*7c356e86SAndroid Build Coastguard Worker 	ys = alloc(sizeof(struct yyrecursive_state), ATEMP);
1139*7c356e86SAndroid Build Coastguard Worker 
1140*7c356e86SAndroid Build Coastguard Worker 	/* tell the lexer to accept a closing parenthesis as EOD */
1141*7c356e86SAndroid Build Coastguard Worker 	ys->old_nesting_type = subshell_nesting_type;
1142*7c356e86SAndroid Build Coastguard Worker 	subshell_nesting_type = etok;
1143*7c356e86SAndroid Build Coastguard Worker 
1144*7c356e86SAndroid Build Coastguard Worker 	/* push reject state, parse recursively, pop reject state */
1145*7c356e86SAndroid Build Coastguard Worker 	ys->old_reject = reject;
1146*7c356e86SAndroid Build Coastguard Worker 	ys->old_symbol = symbol;
1147*7c356e86SAndroid Build Coastguard Worker 	ACCEPT;
1148*7c356e86SAndroid Build Coastguard Worker 	memcpy(ys->old_heres, heres, sizeof(heres));
1149*7c356e86SAndroid Build Coastguard Worker 	ys->old_herep = herep;
1150*7c356e86SAndroid Build Coastguard Worker 	herep = heres;
1151*7c356e86SAndroid Build Coastguard Worker 	ys->next = e->yyrecursive_statep;
1152*7c356e86SAndroid Build Coastguard Worker 	e->yyrecursive_statep = ys;
1153*7c356e86SAndroid Build Coastguard Worker 	/* we use TPAREN as a helper container here */
1154*7c356e86SAndroid Build Coastguard Worker 	t = nested(TPAREN, stok, etok, ALIAS);
1155*7c356e86SAndroid Build Coastguard Worker 	yyrecursive_pop(false);
1156*7c356e86SAndroid Build Coastguard Worker 
1157*7c356e86SAndroid Build Coastguard Worker 	/* t->left because nested(TPAREN, ...) hides our goodies there */
1158*7c356e86SAndroid Build Coastguard Worker 	cp = snptreef(NULL, 0, Tf_T, t->left);
1159*7c356e86SAndroid Build Coastguard Worker 	tfree(t, ATEMP);
1160*7c356e86SAndroid Build Coastguard Worker 
1161*7c356e86SAndroid Build Coastguard Worker 	return (cp);
1162*7c356e86SAndroid Build Coastguard Worker }
1163*7c356e86SAndroid Build Coastguard Worker 
1164*7c356e86SAndroid Build Coastguard Worker void
yyrecursive_pop(bool popall)1165*7c356e86SAndroid Build Coastguard Worker yyrecursive_pop(bool popall)
1166*7c356e86SAndroid Build Coastguard Worker {
1167*7c356e86SAndroid Build Coastguard Worker 	struct yyrecursive_state *ys;
1168*7c356e86SAndroid Build Coastguard Worker 
1169*7c356e86SAndroid Build Coastguard Worker  popnext:
1170*7c356e86SAndroid Build Coastguard Worker 	if (!(ys = e->yyrecursive_statep))
1171*7c356e86SAndroid Build Coastguard Worker 		return;
1172*7c356e86SAndroid Build Coastguard Worker 	e->yyrecursive_statep = ys->next;
1173*7c356e86SAndroid Build Coastguard Worker 
1174*7c356e86SAndroid Build Coastguard Worker 	memcpy(heres, ys->old_heres, sizeof(heres));
1175*7c356e86SAndroid Build Coastguard Worker 	herep = ys->old_herep;
1176*7c356e86SAndroid Build Coastguard Worker 	reject = ys->old_reject;
1177*7c356e86SAndroid Build Coastguard Worker 	symbol = ys->old_symbol;
1178*7c356e86SAndroid Build Coastguard Worker 
1179*7c356e86SAndroid Build Coastguard Worker 	subshell_nesting_type = ys->old_nesting_type;
1180*7c356e86SAndroid Build Coastguard Worker 
1181*7c356e86SAndroid Build Coastguard Worker 	afree(ys, ATEMP);
1182*7c356e86SAndroid Build Coastguard Worker 	if (popall)
1183*7c356e86SAndroid Build Coastguard Worker 		goto popnext;
1184*7c356e86SAndroid Build Coastguard Worker }
1185