blob: e4c38e35a25da3411e899d13fab9ec9fcd4760b3 [file] [log] [blame]
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001/* $OpenBSD: syn.c,v 1.30 2015/09/01 13:12:31 tedu Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002
3/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009,
Elliott Hughesdd4abe02018-02-05 15:55:19 -08005 * 2011, 2012, 2013, 2014, 2015, 2016, 2017,
6 * 2018
Elliott Hughesfc0307d2016-02-02 15:26:47 -08007 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07008 *
9 * Provided that these terms and disclaimer and all copyright notices
10 * are retained or reproduced in an accompanying document, permission
11 * is granted to deal in this work without restriction, including un-
12 * limited rights to use, publicly perform, distribute, sell, modify,
13 * merge, give away, or sublicence.
14 *
15 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
16 * the utmost extent permitted by applicable law, neither express nor
17 * implied; without malicious intent or gross negligence. In no event
18 * may a licensor, author or contributor be held liable for indirect,
19 * direct, other damage, loss, or other issues arising in any way out
20 * of dealing in the work, even if advised of the possibility of such
21 * damage or existence of a defect, except proven that it results out
22 * of said person's immediate fault when using the work as intended.
23 */
24
25#include "sh.h"
26
Elliott Hughesdd4abe02018-02-05 15:55:19 -080027__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.127 2018/01/14 00:22:30 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070028
29struct nesting_state {
30 int start_token; /* token than began nesting (eg, FOR) */
31 int start_line; /* line nesting began on */
32};
33
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000034struct yyrecursive_state {
Elliott Hughes77740fc2016-08-12 15:06:53 -070035 struct ioword *old_heres[HERES];
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000036 struct yyrecursive_state *next;
37 struct ioword **old_herep;
38 int old_symbol;
Elliott Hughesdd4abe02018-02-05 15:55:19 -080039 unsigned int old_nesting_type;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000040 bool old_reject;
41};
42
Elliott Hughesa3c3f962017-04-12 16:52:30 -070043static void yyparse(bool);
44static struct op *pipeline(int, int);
45static struct op *andor(int);
46static struct op *c_list(int, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070047static struct ioword *synio(int);
Elliott Hughesa3c3f962017-04-12 16:52:30 -070048static struct op *nested(int, int, int, int);
49static struct op *get_command(int, int);
50static struct op *dogroup(int);
51static struct op *thenpart(int);
52static struct op *elsepart(int);
53static struct op *caselist(int);
54static struct op *casepart(int, int);
55static struct op *function_body(char *, int, bool);
56static char **wordlist(int);
Thorsten Glaser811a5752013-07-25 14:24:45 +000057static struct op *block(int, struct op *, struct op *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070058static struct op *newtp(int);
59static void syntaxerr(const char *) MKSH_A_NORETURN;
60static void nesting_push(struct nesting_state *, int);
61static void nesting_pop(struct nesting_state *);
Elliott Hughes737fdce2014-08-07 12:59:26 -070062static int inalias(struct source *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070063static Test_op dbtestp_isa(Test_env *, Test_meta);
64static const char *dbtestp_getopnd(Test_env *, Test_op, bool);
65static int dbtestp_eval(Test_env *, Test_op, const char *,
66 const char *, bool);
67static void dbtestp_error(Test_env *, int, const char *) MKSH_A_NORETURN;
68
69static struct op *outtree; /* yyparse output */
70static struct nesting_state nesting; /* \n changed to ; */
71
Geremy Condra03ebf062011-10-12 18:17:24 -070072static bool reject; /* token(cf) gets symbol again */
73static int symbol; /* yylex value */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070074
Geremy Condra03ebf062011-10-12 18:17:24 -070075#define REJECT (reject = true)
76#define ACCEPT (reject = false)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070077#define token(cf) ((reject) ? (ACCEPT, symbol) : (symbol = yylex(cf)))
78#define tpeek(cf) ((reject) ? (symbol) : (REJECT, symbol = yylex(cf)))
Elliott Hughesdd4abe02018-02-05 15:55:19 -080079#define musthave(c,cf) do { \
80 if ((unsigned int)token(cf) != (unsigned int)(c)) \
81 syntaxerr(NULL); \
82} while (/* CONSTCOND */ 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070083
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000084static const char Tcbrace[] = "}";
85static const char Tesac[] = "esac";
86
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070087static void
Elliott Hughesa3c3f962017-04-12 16:52:30 -070088yyparse(bool doalias)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070089{
90 int c;
91
92 ACCEPT;
93
Elliott Hughesa3c3f962017-04-12 16:52:30 -070094 outtree = c_list(doalias ? ALIAS : 0, source->type == SSTRING);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070095 c = tpeek(0);
96 if (c == 0 && !outtree)
97 outtree = newtp(TEOF);
Elliott Hughesdd4abe02018-02-05 15:55:19 -080098 else if (!cinttype(c, C_LF | C_NUL))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070099 syntaxerr(NULL);
100}
101
102static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700103pipeline(int cf, int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700104{
105 struct op *t, *p, *tl = NULL;
106
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700107 t = get_command(cf, sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700108 if (t != NULL) {
109 while (token(0) == '|') {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700110 if ((p = get_command(CONTIN, sALIAS)) == NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700111 syntaxerr(NULL);
112 if (tl == NULL)
Thorsten Glaser811a5752013-07-25 14:24:45 +0000113 t = tl = block(TPIPE, t, p);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700114 else
Thorsten Glaser811a5752013-07-25 14:24:45 +0000115 tl = tl->right = block(TPIPE, tl->right, p);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700116 }
117 REJECT;
118 }
119 return (t);
120}
121
122static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700123andor(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700124{
125 struct op *t, *p;
126 int c;
127
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700128 t = pipeline(0, sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700129 if (t != NULL) {
130 while ((c = token(0)) == LOGAND || c == LOGOR) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700131 if ((p = pipeline(CONTIN, sALIAS)) == NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700132 syntaxerr(NULL);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000133 t = block(c == LOGAND? TAND: TOR, t, p);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700134 }
135 REJECT;
136 }
137 return (t);
138}
139
140static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700141c_list(int sALIAS, bool multi)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700142{
143 struct op *t = NULL, *p, *tl = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -0700144 int c;
145 bool have_sep;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700146
Geremy Condra03ebf062011-10-12 18:17:24 -0700147 while (/* CONSTCOND */ 1) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700148 p = andor(sALIAS);
Geremy Condra03ebf062011-10-12 18:17:24 -0700149 /*
150 * Token has always been read/rejected at this point, so
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700151 * we don't worry about what flags to pass token()
152 */
153 c = token(0);
Geremy Condra03ebf062011-10-12 18:17:24 -0700154 have_sep = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700155 if (c == '\n' && (multi || inalias(source))) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700156 if (!p)
157 /* ignore blank lines */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700158 continue;
159 } else if (!p)
160 break;
161 else if (c == '&' || c == COPROC)
Thorsten Glaser811a5752013-07-25 14:24:45 +0000162 p = block(c == '&' ? TASYNC : TCOPROC, p, NULL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700163 else if (c != ';')
Geremy Condra03ebf062011-10-12 18:17:24 -0700164 have_sep = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700165 if (!t)
166 t = p;
167 else if (!tl)
Thorsten Glaser811a5752013-07-25 14:24:45 +0000168 t = tl = block(TLIST, t, p);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700169 else
Thorsten Glaser811a5752013-07-25 14:24:45 +0000170 tl = tl->right = block(TLIST, tl->right, p);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700171 if (!have_sep)
172 break;
173 }
174 REJECT;
175 return (t);
176}
177
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800178static const char IONDELIM_delim[] = { CHAR, '<', CHAR, '<', EOS };
179
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700180static struct ioword *
181synio(int cf)
182{
183 struct ioword *iop;
Geremy Condra03ebf062011-10-12 18:17:24 -0700184 static struct ioword *nextiop;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700185 bool ishere;
186
187 if (nextiop != NULL) {
188 iop = nextiop;
189 nextiop = NULL;
190 return (iop);
191 }
192
193 if (tpeek(cf) != REDIR)
194 return (NULL);
195 ACCEPT;
196 iop = yylval.iop;
Elliott Hughesb27ce952015-04-21 13:39:18 -0700197 ishere = (iop->ioflag & IOTYPE) == IOHERE;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800198 if (iop->ioflag & IOHERESTR) {
199 musthave(LWORD, 0);
200 } else if (ishere && tpeek(HEREDELIM) == '\n') {
201 ACCEPT;
202 yylval.cp = wdcopy(IONDELIM_delim, ATEMP);
203 iop->ioflag |= IOEVAL | IONDELIM;
204 } else
205 musthave(LWORD, ishere ? HEREDELIM : 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700206 if (ishere) {
207 iop->delim = yylval.cp;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800208 if (*ident != 0 && !(iop->ioflag & IOHERESTR)) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700209 /* unquoted */
Elliott Hughesb27ce952015-04-21 13:39:18 -0700210 iop->ioflag |= IOEVAL;
Elliott Hughes50012062015-03-10 22:22:24 -0700211 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700212 if (herep > &heres[HERES - 1])
Elliott Hughes77740fc2016-08-12 15:06:53 -0700213 yyerror(Tf_toomany, "<<");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700214 *herep++ = iop;
215 } else
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800216 iop->ioname = yylval.cp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700217
Elliott Hughesb27ce952015-04-21 13:39:18 -0700218 if (iop->ioflag & IOBASH) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700219 char *cp;
220
221 nextiop = alloc(sizeof(*iop), ATEMP);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800222 nextiop->ioname = cp = alloc(3, ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700223 *cp++ = CHAR;
Elliott Hughes96b43632015-07-17 11:39:41 -0700224 *cp++ = digits_lc[iop->unit % 10];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700225 *cp = EOS;
226
Elliott Hughesb27ce952015-04-21 13:39:18 -0700227 iop->ioflag &= ~IOBASH;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700228 nextiop->unit = 2;
Elliott Hughesb27ce952015-04-21 13:39:18 -0700229 nextiop->ioflag = IODUP;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700230 nextiop->delim = NULL;
231 nextiop->heredoc = NULL;
232 }
233 return (iop);
234}
235
236static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700237nested(int type, int smark, int emark, int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700238{
239 struct op *t;
240 struct nesting_state old_nesting;
241
242 nesting_push(&old_nesting, smark);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700243 t = c_list(sALIAS, true);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000244 musthave(emark, KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700245 nesting_pop(&old_nesting);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000246 return (block(type, t, NULL));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700247}
248
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700249static const char builtin_cmd[] = {
250 QCHAR, '\\', CHAR, 'b', CHAR, 'u', CHAR, 'i',
251 CHAR, 'l', CHAR, 't', CHAR, 'i', CHAR, 'n', EOS
252};
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000253static const char let_cmd[] = {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700254 CHAR, 'l', CHAR, 'e', CHAR, 't', EOS
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000255};
256static const char setA_cmd0[] = {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700257 CHAR, 's', CHAR, 'e', CHAR, 't', EOS
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000258};
259static const char setA_cmd1[] = {
260 CHAR, '-', CHAR, 'A', EOS
261};
262static const char setA_cmd2[] = {
263 CHAR, '-', CHAR, '-', EOS
264};
265
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700266static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700267get_command(int cf, int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700268{
269 struct op *t;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000270 int c, iopn = 0, syniocf, lno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700271 struct ioword *iop, **iops;
272 XPtrV args, vars;
273 struct nesting_state old_nesting;
274
Geremy Condra03ebf062011-10-12 18:17:24 -0700275 /* NUFILE is small enough to leave this addition unchecked */
276 iops = alloc2((NUFILE + 1), sizeof(struct ioword *), ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700277 XPinit(args, 16);
278 XPinit(vars, 16);
279
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000280 syniocf = KEYWORD|sALIAS;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800281 switch (c = token(cf|KEYWORD|sALIAS|CMDASN)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700282 default:
283 REJECT;
284 afree(iops, ATEMP);
285 XPfree(args);
286 XPfree(vars);
Geremy Condra03ebf062011-10-12 18:17:24 -0700287 /* empty line */
288 return (NULL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700289
290 case LWORD:
291 case REDIR:
292 REJECT;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000293 syniocf &= ~(KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700294 t = newtp(TCOM);
295 t->lineno = source->line;
Elliott Hughes966dd552016-12-08 15:56:04 -0800296 goto get_command_start;
Geremy Condra03ebf062011-10-12 18:17:24 -0700297 while (/* CONSTCOND */ 1) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700298 bool check_decl_utility;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800299
300 if (XPsize(args) == 0) {
Elliott Hughes966dd552016-12-08 15:56:04 -0800301 get_command_start:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700302 check_decl_utility = true;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800303 cf = sALIAS | CMDASN;
304 } else if (t->u.evalflags)
305 cf = CMDWORD | CMDASN;
306 else
307 cf = CMDWORD;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700308 switch (tpeek(cf)) {
309 case REDIR:
310 while ((iop = synio(cf)) != NULL) {
311 if (iopn >= NUFILE)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700312 yyerror(Tf_toomany,
313 Tredirection);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700314 iops[iopn++] = iop;
315 }
316 break;
317
318 case LWORD:
319 ACCEPT;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700320 if (check_decl_utility) {
321 struct tbl *tt = get_builtin(ident);
322 uint32_t flag;
323
324 flag = tt ? tt->flag : 0;
325 if (flag & DECL_UTIL)
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800326 t->u.evalflags = DOVACHECK;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700327 if (!(flag & DECL_FWDR))
328 check_decl_utility = false;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800329 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700330 if ((XPsize(args) == 0 || Flag(FKEYWORD)) &&
331 is_wdvarassign(yylval.cp))
332 XPput(vars, yylval.cp);
333 else
334 XPput(args, yylval.cp);
335 break;
336
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800337 case ORD('(' /*)*/):
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000338 if (XPsize(args) == 0 && XPsize(vars) == 1 &&
339 is_wdvarassign(yylval.cp)) {
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800340 char *tcp;
341
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000342 /* wdarrassign: foo=(bar) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700343 ACCEPT;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000344
345 /* manipulate the vars string */
346 tcp = XPptrv(vars)[(vars.len = 0)];
347 /* 'varname=' -> 'varname' */
348 tcp[wdscan(tcp, EOS) - tcp - 3] = EOS;
349
350 /* construct new args strings */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700351 XPput(args, wdcopy(builtin_cmd, ATEMP));
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000352 XPput(args, wdcopy(setA_cmd0, ATEMP));
353 XPput(args, wdcopy(setA_cmd1, ATEMP));
354 XPput(args, tcp);
355 XPput(args, wdcopy(setA_cmd2, ATEMP));
356
357 /* slurp in words till closing paren */
358 while (token(CONTIN) == LWORD)
359 XPput(args, yylval.cp);
360 if (symbol != /*(*/ ')')
361 syntaxerr(NULL);
362 } else {
363 /*
364 * Check for "> foo (echo hi)"
365 * which AT&T ksh allows (not
366 * POSIX, but not disallowed)
367 */
368 afree(t, ATEMP);
369 if (XPsize(args) == 0 &&
370 XPsize(vars) == 0) {
371 ACCEPT;
372 goto Subshell;
373 }
374
375 /* must be a function */
376 if (iopn != 0 || XPsize(args) != 1 ||
377 XPsize(vars) != 0)
378 syntaxerr(NULL);
379 ACCEPT;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700380 musthave(/*(*/ ')', 0);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700381 t = function_body(XPptrv(args)[0],
382 sALIAS, false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700383 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700384 goto Leave;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700385
386 default:
387 goto Leave;
388 }
389 }
390 Leave:
391 break;
392
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800393 case ORD('(' /*)*/): {
394 unsigned int subshell_nesting_type_saved;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700395 Subshell:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000396 subshell_nesting_type_saved = subshell_nesting_type;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800397 subshell_nesting_type = ORD(')');
398 t = nested(TPAREN, ORD('('), ORD(')'), sALIAS);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000399 subshell_nesting_type = subshell_nesting_type_saved;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700400 break;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000401 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700402
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800403 case ORD('{' /*}*/):
404 t = nested(TBRACE, ORD('{'), ORD('}'), sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700405 break;
406
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000407 case MDPAREN:
Geremy Condra03ebf062011-10-12 18:17:24 -0700408 /* leave KEYWORD in syniocf (allow if (( 1 )) then ...) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700409 lno = source->line;
410 ACCEPT;
411 switch (token(LETEXPR)) {
412 case LWORD:
413 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800414 case ORD('(' /*)*/):
415 c = ORD('(');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700416 goto Subshell;
417 default:
418 syntaxerr(NULL);
419 }
420 t = newtp(TCOM);
421 t->lineno = lno;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700422 XPput(args, wdcopy(builtin_cmd, ATEMP));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700423 XPput(args, wdcopy(let_cmd, ATEMP));
424 XPput(args, yylval.cp);
425 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700426
427 case DBRACKET: /* [[ .. ]] */
Geremy Condra03ebf062011-10-12 18:17:24 -0700428 /* leave KEYWORD in syniocf (allow if [[ -n 1 ]] then ...) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700429 t = newtp(TDBRACKET);
430 ACCEPT;
431 {
432 Test_env te;
433
434 te.flags = TEF_DBRACKET;
435 te.pos.av = &args;
436 te.isa = dbtestp_isa;
437 te.getopnd = dbtestp_getopnd;
438 te.eval = dbtestp_eval;
439 te.error = dbtestp_error;
440
441 test_parse(&te);
442 }
443 break;
444
445 case FOR:
446 case SELECT:
447 t = newtp((c == FOR) ? TFOR : TSELECT);
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800448 musthave(LWORD, CMDASN);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700449 if (!is_wdvarname(yylval.cp, true))
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700450 yyerror("%s: bad identifier",
Elliott Hughes77740fc2016-08-12 15:06:53 -0700451 c == FOR ? "for" : Tselect);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700452 strdupx(t->str, ident, ATEMP);
453 nesting_push(&old_nesting, c);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700454 t->vars = wordlist(sALIAS);
455 t->left = dogroup(sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700456 nesting_pop(&old_nesting);
457 break;
458
459 case WHILE:
460 case UNTIL:
461 nesting_push(&old_nesting, c);
462 t = newtp((c == WHILE) ? TWHILE : TUNTIL);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700463 t->left = c_list(sALIAS, true);
464 t->right = dogroup(sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700465 nesting_pop(&old_nesting);
466 break;
467
468 case CASE:
469 t = newtp(TCASE);
470 musthave(LWORD, 0);
471 t->str = yylval.cp;
472 nesting_push(&old_nesting, c);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700473 t->left = caselist(sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700474 nesting_pop(&old_nesting);
475 break;
476
477 case IF:
478 nesting_push(&old_nesting, c);
479 t = newtp(TIF);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700480 t->left = c_list(sALIAS, true);
481 t->right = thenpart(sALIAS);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000482 musthave(FI, KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700483 nesting_pop(&old_nesting);
484 break;
485
486 case BANG:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000487 syniocf &= ~(KEYWORD|sALIAS);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700488 t = pipeline(0, sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700489 if (t == NULL)
490 syntaxerr(NULL);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000491 t = block(TBANG, NULL, t);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700492 break;
493
494 case TIME:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000495 syniocf &= ~(KEYWORD|sALIAS);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700496 t = pipeline(0, sALIAS);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000497 if (t && t->type == TCOM) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700498 t->str = alloc(2, ATEMP);
Geremy Condra03ebf062011-10-12 18:17:24 -0700499 /* TF_* flags */
500 t->str[0] = '\0';
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700501 t->str[1] = '\0';
502 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000503 t = block(TTIME, t, NULL);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700504 break;
505
506 case FUNCTION:
507 musthave(LWORD, 0);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700508 t = function_body(yylval.cp, sALIAS, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700509 break;
510 }
511
512 while ((iop = synio(syniocf)) != NULL) {
513 if (iopn >= NUFILE)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700514 yyerror(Tf_toomany, Tredirection);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700515 iops[iopn++] = iop;
516 }
517
518 if (iopn == 0) {
519 afree(iops, ATEMP);
520 t->ioact = NULL;
521 } else {
522 iops[iopn++] = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -0700523 iops = aresize2(iops, iopn, sizeof(struct ioword *), ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700524 t->ioact = iops;
525 }
526
527 if (t->type == TCOM || t->type == TDBRACKET) {
528 XPput(args, NULL);
529 t->args = (const char **)XPclose(args);
530 XPput(vars, NULL);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000531 t->vars = (char **)XPclose(vars);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700532 } else {
533 XPfree(args);
534 XPfree(vars);
535 }
536
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800537 if (c == MDPAREN) {
538 t = block(TBRACE, t, NULL);
539 t->ioact = t->left->ioact;
540 t->left->ioact = NULL;
541 }
542
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700543 return (t);
544}
545
546static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700547dogroup(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700548{
549 int c;
550 struct op *list;
551
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000552 c = token(CONTIN|KEYWORD|sALIAS);
Geremy Condra03ebf062011-10-12 18:17:24 -0700553 /*
554 * A {...} can be used instead of do...done for for/select loops
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700555 * but not for while/until loops - we don't need to check if it
556 * is a while loop because it would have been parsed as part of
557 * the conditional command list...
558 */
559 if (c == DO)
560 c = DONE;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800561 else if ((unsigned int)c == ORD('{'))
562 c = ORD('}');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700563 else
564 syntaxerr(NULL);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700565 list = c_list(sALIAS, true);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000566 musthave(c, KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700567 return (list);
568}
569
570static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700571thenpart(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700572{
573 struct op *t;
574
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000575 musthave(THEN, KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700576 t = newtp(0);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700577 t->left = c_list(sALIAS, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700578 if (t->left == NULL)
579 syntaxerr(NULL);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700580 t->right = elsepart(sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700581 return (t);
582}
583
584static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700585elsepart(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700586{
587 struct op *t;
588
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800589 switch (token(KEYWORD|sALIAS|CMDASN)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700590 case ELSE:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700591 if ((t = c_list(sALIAS, true)) == NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700592 syntaxerr(NULL);
593 return (t);
594
595 case ELIF:
596 t = newtp(TELIF);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700597 t->left = c_list(sALIAS, true);
598 t->right = thenpart(sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700599 return (t);
600
601 default:
602 REJECT;
603 }
604 return (NULL);
605}
606
607static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700608caselist(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700609{
610 struct op *t, *tl;
611 int c;
612
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000613 c = token(CONTIN|KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700614 /* A {...} can be used instead of in...esac for case statements */
615 if (c == IN)
616 c = ESAC;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800617 else if ((unsigned int)c == ORD('{'))
618 c = ORD('}');
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700619 else
620 syntaxerr(NULL);
621 t = tl = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -0700622 /* no ALIAS here */
623 while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700624 struct op *tc = casepart(c, sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700625 if (tl == NULL)
626 t = tl = tc, tl->right = NULL;
627 else
628 tl->right = tc, tl = tc;
629 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000630 musthave(c, KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700631 return (t);
632}
633
634static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700635casepart(int endtok, int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700636{
637 struct op *t;
638 XPtrV ptns;
639
640 XPinit(ptns, 16);
641 t = newtp(TPAT);
642 /* no ALIAS here */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800643 if ((unsigned int)token(CONTIN | KEYWORD) != ORD('('))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700644 REJECT;
645 do {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000646 switch (token(0)) {
647 case LWORD:
648 break;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800649 case ORD('}'):
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000650 case ESAC:
651 if (symbol != endtok) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800652 strdupx(yylval.cp, (unsigned int)symbol ==
653 ORD('}') ? Tcbrace : Tesac, ATEMP);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000654 break;
655 }
656 /* FALLTHROUGH */
657 default:
658 syntaxerr(NULL);
659 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700660 XPput(ptns, yylval.cp);
661 } while (token(0) == '|');
662 REJECT;
663 XPput(ptns, NULL);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000664 t->vars = (char **)XPclose(ptns);
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800665 musthave(ORD(')'), 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700666
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700667 t->left = c_list(sALIAS, true);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000668
669 /* initialise to default for ;; or omitted */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800670 t->u.charflag = ORD(';');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000671 /* SUSv4 requires the ;; except in the last casepart */
672 if ((tpeek(CONTIN|KEYWORD|sALIAS)) != endtok)
Geremy Condra03ebf062011-10-12 18:17:24 -0700673 switch (symbol) {
674 default:
675 syntaxerr(NULL);
Geremy Condra03ebf062011-10-12 18:17:24 -0700676 case BRKEV:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800677 t->u.charflag = ORD('|');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000678 if (0)
679 /* FALLTHROUGH */
Geremy Condra03ebf062011-10-12 18:17:24 -0700680 case BRKFT:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800681 t->u.charflag = ORD('&');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000682 /* FALLTHROUGH */
683 case BREAK:
684 /* initialised above, but we need to eat the token */
Geremy Condra03ebf062011-10-12 18:17:24 -0700685 ACCEPT;
686 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700687 return (t);
688}
689
690static struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700691function_body(char *name, int sALIAS,
Geremy Condra03ebf062011-10-12 18:17:24 -0700692 /* function foo { ... } vs foo() { .. } */
693 bool ksh_func)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700694{
695 char *sname, *p;
696 struct op *t;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700697
Geremy Condra03ebf062011-10-12 18:17:24 -0700698 sname = wdstrip(name, 0);
699 /*-
700 * Check for valid characters in name. POSIX and AT&T ksh93 say
701 * only allow [a-zA-Z_0-9] but this allows more as old pdkshs
702 * have allowed more; the following were never allowed:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700703 * NUL TAB NL SP " $ & ' ( ) ; < = > \ ` |
Elliott Hughes23925bb2017-09-22 16:04:20 -0700704 * C_QUOTE|C_SPC covers all but adds # * ? [ ]
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700705 */
706 for (p = sname; *p; p++)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700707 if (ctype(*p, C_QUOTE | C_SPC))
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700708 yyerror(Tinvname, sname, Tfunction);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700709
Geremy Condra03ebf062011-10-12 18:17:24 -0700710 /*
711 * Note that POSIX allows only compound statements after foo(),
712 * sh and AT&T ksh allow any command, go with the later since it
713 * shouldn't break anything. However, for function foo, AT&T ksh
714 * only accepts an open-brace.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700715 */
716 if (ksh_func) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800717 if ((unsigned int)tpeek(CONTIN|KEYWORD|sALIAS) == ORD('(' /*)*/)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000718 /* function foo () { //}*/
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700719 ACCEPT;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800720 musthave(ORD(/*(*/ ')'), 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700721 /* degrade to POSIX function */
722 ksh_func = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700723 }
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800724 musthave(ORD('{' /*}*/), CONTIN|KEYWORD|sALIAS);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700725 REJECT;
726 }
727
728 t = newtp(TFUNCT);
729 t->str = sname;
Geremy Condra03ebf062011-10-12 18:17:24 -0700730 t->u.ksh_func = tobool(ksh_func);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700731 t->lineno = source->line;
732
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700733 if ((t->left = get_command(CONTIN, sALIAS)) == NULL) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700734 char *tv;
735 /*
Geremy Condra03ebf062011-10-12 18:17:24 -0700736 * Probably something like foo() followed by EOF or ';'.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700737 * This is accepted by sh and ksh88.
738 * To make "typeset -f foo" work reliably (so its output can
739 * be used as input), we pretend there is a colon here.
740 */
741 t->left = newtp(TCOM);
Geremy Condra03ebf062011-10-12 18:17:24 -0700742 /* (2 * sizeof(char *)) is small enough */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700743 t->left->args = alloc(2 * sizeof(char *), ATEMP);
744 t->left->args[0] = tv = alloc(3, ATEMP);
Elliott Hughes96b43632015-07-17 11:39:41 -0700745 tv[0] = QCHAR;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700746 tv[1] = ':';
747 tv[2] = EOS;
748 t->left->args[1] = NULL;
749 t->left->vars = alloc(sizeof(char *), ATEMP);
750 t->left->vars[0] = NULL;
751 t->left->lineno = 1;
752 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700753
754 return (t);
755}
756
757static char **
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700758wordlist(int sALIAS)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700759{
760 int c;
761 XPtrV args;
762
763 XPinit(args, 16);
764 /* POSIX does not do alias expansion here... */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000765 if ((c = token(CONTIN|KEYWORD|sALIAS)) != IN) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700766 if (c != ';')
767 /* non-POSIX, but AT&T ksh accepts a ; here */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700768 REJECT;
769 return (NULL);
770 }
771 while ((c = token(0)) == LWORD)
772 XPput(args, yylval.cp);
773 if (c != '\n' && c != ';')
774 syntaxerr(NULL);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000775 XPput(args, NULL);
776 return ((char **)XPclose(args));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700777}
778
779/*
780 * supporting functions
781 */
782
783static struct op *
Thorsten Glaser811a5752013-07-25 14:24:45 +0000784block(int type, struct op *t1, struct op *t2)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700785{
786 struct op *t;
787
788 t = newtp(type);
789 t->left = t1;
790 t->right = t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700791 return (t);
792}
793
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000794static const struct tokeninfo {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700795 const char *name;
796 short val;
797 short reserved;
798} tokentab[] = {
799 /* Reserved words */
800 { "if", IF, true },
801 { "then", THEN, true },
802 { "else", ELSE, true },
803 { "elif", ELIF, true },
804 { "fi", FI, true },
805 { "case", CASE, true },
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000806 { Tesac, ESAC, true },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700807 { "for", FOR, true },
Geremy Condra03ebf062011-10-12 18:17:24 -0700808 { Tselect, SELECT, true },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700809 { "while", WHILE, true },
810 { "until", UNTIL, true },
811 { "do", DO, true },
812 { "done", DONE, true },
813 { "in", IN, true },
Geremy Condra03ebf062011-10-12 18:17:24 -0700814 { Tfunction, FUNCTION, true },
Elliott Hughes77740fc2016-08-12 15:06:53 -0700815 { Ttime, TIME, true },
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800816 { "{", ORD('{'), true },
817 { Tcbrace, ORD('}'), true },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700818 { "!", BANG, true },
819 { "[[", DBRACKET, true },
820 /* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */
821 { "&&", LOGAND, false },
822 { "||", LOGOR, false },
823 { ";;", BREAK, false },
Geremy Condra03ebf062011-10-12 18:17:24 -0700824 { ";|", BRKEV, false },
825 { ";&", BRKFT, false },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700826 { "((", MDPAREN, false },
827 { "|&", COPROC, false },
828 /* and some special cases... */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800829 { "newline", ORD('\n'), false },
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700830 { NULL, 0, false }
831};
832
833void
834initkeywords(void)
835{
836 struct tokeninfo const *tt;
837 struct tbl *p;
838
Geremy Condra03ebf062011-10-12 18:17:24 -0700839 ktinit(APERM, &keywords,
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000840 /* currently 28 keywords: 75% of 64 = 2^6 */
Geremy Condra03ebf062011-10-12 18:17:24 -0700841 6);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700842 for (tt = tokentab; tt->name; tt++) {
843 if (tt->reserved) {
844 p = ktenter(&keywords, tt->name, hash(tt->name));
845 p->flag |= DEFINED|ISSET;
846 p->type = CKEYWD;
847 p->val.i = tt->val;
848 }
849 }
850}
851
852static void
853syntaxerr(const char *what)
854{
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800855 /* 23<<- is the longest redirection, I think */
856 char redir[8];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700857 const char *s;
858 struct tokeninfo const *tt;
859 int c;
860
861 if (!what)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700862 what = Tunexpected;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700863 REJECT;
864 c = token(0);
865 Again:
866 switch (c) {
867 case 0:
868 if (nesting.start_token) {
869 c = nesting.start_token;
870 source->errline = nesting.start_line;
871 what = "unmatched";
872 goto Again;
873 }
874 /* don't quote the EOF */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700875 yyerror("%s: unexpected EOF", Tsynerr);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700876 /* NOTREACHED */
877
878 case LWORD:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700879 s = snptreef(NULL, 32, Tf_S, yylval.cp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700880 break;
881
882 case REDIR:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700883 s = snptreef(redir, sizeof(redir), Tft_R, yylval.iop);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700884 break;
885
886 default:
887 for (tt = tokentab; tt->name; tt++)
888 if (tt->val == c)
889 break;
890 if (tt->name)
891 s = tt->name;
892 else {
893 if (c > 0 && c < 256) {
894 redir[0] = c;
895 redir[1] = '\0';
896 } else
897 shf_snprintf(redir, sizeof(redir),
898 "?%d", c);
899 s = redir;
900 }
901 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700902 yyerror(Tf_sD_s_qs, Tsynerr, what, s);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700903}
904
905static void
906nesting_push(struct nesting_state *save, int tok)
907{
908 *save = nesting;
909 nesting.start_token = tok;
910 nesting.start_line = source->line;
911}
912
913static void
914nesting_pop(struct nesting_state *saved)
915{
916 nesting = *saved;
917}
918
919static struct op *
920newtp(int type)
921{
922 struct op *t;
923
924 t = alloc(sizeof(struct op), ATEMP);
925 t->type = type;
926 t->u.evalflags = 0;
927 t->args = NULL;
928 t->vars = NULL;
929 t->ioact = NULL;
930 t->left = t->right = NULL;
931 t->str = NULL;
932 return (t);
933}
934
935struct op *
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700936compile(Source *s, bool skiputf8bom, bool doalias)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700937{
938 nesting.start_token = 0;
939 nesting.start_line = 0;
940 herep = heres;
941 source = s;
Geremy Condra03ebf062011-10-12 18:17:24 -0700942 if (skiputf8bom)
943 yyskiputf8bom();
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700944 yyparse(doalias);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700945 return (outtree);
946}
947
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700948/* Check if we are in the middle of reading an alias */
949static int
950inalias(struct source *s)
951{
Elliott Hughes77740fc2016-08-12 15:06:53 -0700952 while (s && s->type == SALIAS) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700953 if (!(s->flags & SF_ALIASEND))
954 return (1);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700955 s = s->next;
956 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700957 return (0);
958}
959
960
Geremy Condra03ebf062011-10-12 18:17:24 -0700961/*
962 * Order important - indexed by Test_meta values
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700963 * Note that ||, &&, ( and ) can't appear in as unquoted strings
964 * in normal shell input, so these can be interpreted unambiguously
965 * in the evaluation pass.
966 */
967static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS };
968static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS };
969static const char dbtest_not[] = { CHAR, '!', EOS };
970static const char dbtest_oparen[] = { CHAR, '(', EOS };
971static const char dbtest_cparen[] = { CHAR, ')', EOS };
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000972const char * const dbtest_tokens[] = {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700973 dbtest_or, dbtest_and, dbtest_not,
974 dbtest_oparen, dbtest_cparen
975};
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000976static const char db_close[] = { CHAR, ']', CHAR, ']', EOS };
977static const char db_lthan[] = { CHAR, '<', EOS };
978static const char db_gthan[] = { CHAR, '>', EOS };
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700979
980/*
981 * Test if the current token is a whatever. Accepts the current token if
982 * it is. Returns 0 if it is not, non-zero if it is (in the case of
983 * TM_UNOP and TM_BINOP, the returned value is a Test_op).
984 */
985static Test_op
986dbtestp_isa(Test_env *te, Test_meta meta)
987{
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800988 int c = tpeek(CMDASN | (meta == TM_BINOP ? 0 : CONTIN));
Elliott Hughes737fdce2014-08-07 12:59:26 -0700989 bool uqword;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700990 char *save = NULL;
991 Test_op ret = TO_NONOP;
992
993 /* unquoted word? */
994 uqword = c == LWORD && *ident;
995
996 if (meta == TM_OR)
997 ret = c == LOGOR ? TO_NONNULL : TO_NONOP;
998 else if (meta == TM_AND)
999 ret = c == LOGAND ? TO_NONNULL : TO_NONOP;
1000 else if (meta == TM_NOT)
1001 ret = (uqword && !strcmp(yylval.cp,
1002 dbtest_tokens[(int)TM_NOT])) ? TO_NONNULL : TO_NONOP;
1003 else if (meta == TM_OPAREN)
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001004 ret = (unsigned int)c == ORD('(') /*)*/ ? TO_NONNULL : TO_NONOP;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001005 else if (meta == TM_CPAREN)
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001006 ret = (unsigned int)c == /*(*/ ORD(')') ? TO_NONNULL : TO_NONOP;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001007 else if (meta == TM_UNOP || meta == TM_BINOP) {
1008 if (meta == TM_BINOP && c == REDIR &&
Elliott Hughesb27ce952015-04-21 13:39:18 -07001009 (yylval.iop->ioflag == IOREAD ||
1010 yylval.iop->ioflag == IOWRITE)) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001011 ret = TO_NONNULL;
Elliott Hughesb27ce952015-04-21 13:39:18 -07001012 save = wdcopy(yylval.iop->ioflag == IOREAD ?
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001013 db_lthan : db_gthan, ATEMP);
1014 } else if (uqword && (ret = test_isop(meta, ident)))
1015 save = yylval.cp;
Geremy Condra03ebf062011-10-12 18:17:24 -07001016 } else
1017 /* meta == TM_END */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001018 ret = (uqword && !strcmp(yylval.cp,
1019 db_close)) ? TO_NONNULL : TO_NONOP;
1020 if (ret != TO_NONOP) {
1021 ACCEPT;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001022 if ((unsigned int)meta < NELEM(dbtest_tokens))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001023 save = wdcopy(dbtest_tokens[(int)meta], ATEMP);
1024 if (save)
1025 XPput(*te->pos.av, save);
1026 }
1027 return (ret);
1028}
1029
1030static const char *
1031dbtestp_getopnd(Test_env *te, Test_op op MKSH_A_UNUSED,
1032 bool do_eval MKSH_A_UNUSED)
1033{
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001034 int c = tpeek(CMDASN);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001035
1036 if (c != LWORD)
1037 return (NULL);
1038
1039 ACCEPT;
1040 XPput(*te->pos.av, yylval.cp);
1041
1042 return (null);
1043}
1044
1045static int
1046dbtestp_eval(Test_env *te MKSH_A_UNUSED, Test_op op MKSH_A_UNUSED,
1047 const char *opnd1 MKSH_A_UNUSED, const char *opnd2 MKSH_A_UNUSED,
1048 bool do_eval MKSH_A_UNUSED)
1049{
1050 return (1);
1051}
1052
1053static void
1054dbtestp_error(Test_env *te, int offset, const char *msg)
1055{
1056 te->flags |= TEF_ERROR;
1057
1058 if (offset < 0) {
1059 REJECT;
1060 /* Kludgy to say the least... */
1061 symbol = LWORD;
1062 yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av) +
1063 offset);
1064 }
1065 syntaxerr(msg);
1066}
Geremy Condra03ebf062011-10-12 18:17:24 -07001067
1068#if HAVE_SELECT
1069
1070#ifndef EOVERFLOW
1071#ifdef ERANGE
1072#define EOVERFLOW ERANGE
1073#else
1074#define EOVERFLOW EINVAL
1075#endif
1076#endif
1077
1078bool
1079parse_usec(const char *s, struct timeval *tv)
1080{
1081 struct timeval tt;
1082 int i;
1083
1084 tv->tv_sec = 0;
1085 /* parse integral part */
Elliott Hughes23925bb2017-09-22 16:04:20 -07001086 while (ctype(*s, C_DIGIT)) {
Elliott Hughes96b43632015-07-17 11:39:41 -07001087 tt.tv_sec = tv->tv_sec * 10 + ksh_numdig(*s++);
1088 /*XXX this overflow check maybe UB */
Geremy Condra03ebf062011-10-12 18:17:24 -07001089 if (tt.tv_sec / 10 != tv->tv_sec) {
1090 errno = EOVERFLOW;
1091 return (true);
1092 }
1093 tv->tv_sec = tt.tv_sec;
1094 }
1095
1096 tv->tv_usec = 0;
1097 if (!*s)
1098 /* no decimal fraction */
1099 return (false);
1100 else if (*s++ != '.') {
1101 /* junk after integral part */
1102 errno = EINVAL;
1103 return (true);
1104 }
1105
1106 /* parse decimal fraction */
1107 i = 100000;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001108 while (ctype(*s, C_DIGIT)) {
Elliott Hughes96b43632015-07-17 11:39:41 -07001109 tv->tv_usec += i * ksh_numdig(*s++);
Geremy Condra03ebf062011-10-12 18:17:24 -07001110 if (i == 1)
1111 break;
1112 i /= 10;
1113 }
1114 /* check for junk after fractional part */
Elliott Hughes23925bb2017-09-22 16:04:20 -07001115 while (ctype(*s, C_DIGIT))
Geremy Condra03ebf062011-10-12 18:17:24 -07001116 ++s;
1117 if (*s) {
1118 errno = EINVAL;
1119 return (true);
1120 }
1121
1122 /* end of input string reached, no errors */
1123 return (false);
1124}
1125#endif
1126
1127/*
1128 * Helper function called from within lex.c:yylex() to parse
1129 * a COMSUB recursively using the main shell parser and lexer
1130 */
1131char *
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001132yyrecursive(int subtype)
Geremy Condra03ebf062011-10-12 18:17:24 -07001133{
1134 struct op *t;
1135 char *cp;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001136 struct yyrecursive_state *ys;
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001137 unsigned int stok, etok;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001138
Thorsten Glaser811a5752013-07-25 14:24:45 +00001139 if (subtype != COMSUB) {
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001140 stok = ORD('{');
1141 etok = ORD('}');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001142 } else {
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001143 stok = ORD('(');
1144 etok = ORD(')');
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001145 }
1146
1147 ys = alloc(sizeof(struct yyrecursive_state), ATEMP);
Geremy Condra03ebf062011-10-12 18:17:24 -07001148
1149 /* tell the lexer to accept a closing parenthesis as EOD */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001150 ys->old_nesting_type = subshell_nesting_type;
1151 subshell_nesting_type = etok;
Geremy Condra03ebf062011-10-12 18:17:24 -07001152
1153 /* push reject state, parse recursively, pop reject state */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001154 ys->old_reject = reject;
1155 ys->old_symbol = symbol;
Geremy Condra03ebf062011-10-12 18:17:24 -07001156 ACCEPT;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001157 memcpy(ys->old_heres, heres, sizeof(heres));
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001158 ys->old_herep = herep;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001159 herep = heres;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001160 ys->next = e->yyrecursive_statep;
1161 e->yyrecursive_statep = ys;
Geremy Condra03ebf062011-10-12 18:17:24 -07001162 /* we use TPAREN as a helper container here */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001163 t = nested(TPAREN, stok, etok, ALIAS);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001164 yyrecursive_pop(false);
Geremy Condra03ebf062011-10-12 18:17:24 -07001165
1166 /* t->left because nested(TPAREN, ...) hides our goodies there */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001167 cp = snptreef(NULL, 0, Tf_T, t->left);
Geremy Condra03ebf062011-10-12 18:17:24 -07001168 tfree(t, ATEMP);
1169
Geremy Condra03ebf062011-10-12 18:17:24 -07001170 return (cp);
1171}
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001172
1173void
1174yyrecursive_pop(bool popall)
1175{
1176 struct yyrecursive_state *ys;
1177
1178 popnext:
1179 if (!(ys = e->yyrecursive_statep))
1180 return;
1181 e->yyrecursive_statep = ys->next;
1182
Elliott Hughes77740fc2016-08-12 15:06:53 -07001183 memcpy(heres, ys->old_heres, sizeof(heres));
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001184 herep = ys->old_herep;
1185 reject = ys->old_reject;
1186 symbol = ys->old_symbol;
1187
1188 subshell_nesting_type = ys->old_nesting_type;
1189
1190 afree(ys, ATEMP);
1191 if (popall)
1192 goto popnext;
1193}