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