1 /* $OpenBSD: syn.c,v 1.30 2015/09/01 13:12:31 tedu Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 * 2011, 2012, 2013, 2014, 2015, 2016, 2017,
7 * mirabilos <m@mirbsd.org>
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.
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.
27 __RCSID("$MirOS: src/bin/mksh/syn.c,v 1.127 2018/01/14 00:22:30 tg Exp $");
29 struct nesting_state {
30 int start_token; /* token than began nesting (eg, FOR) */
31 int start_line; /* line nesting began on */
34 struct yyrecursive_state {
35 struct ioword *old_heres[HERES];
36 struct yyrecursive_state *next;
37 struct ioword **old_herep;
39 unsigned int old_nesting_type;
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 *,
67 static void dbtestp_error(Test_env *, int, const char *) MKSH_A_NORETURN;
69 static struct op *outtree; /* yyparse output */
70 static struct nesting_state nesting; /* \n changed to ; */
72 static bool reject; /* token(cf) gets symbol again */
73 static int symbol; /* yylex value */
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)) \
82 } while (/* CONSTCOND */ 0)
84 static const char Tcbrace[] = "}";
85 static const char Tesac[] = "esac";
94 outtree = c_list(doalias ? ALIAS : 0, source->type == SSTRING);
96 if (c == 0 && !outtree)
97 outtree = newtp(TEOF);
98 else if (!cinttype(c, C_LF | C_NUL))
103 pipeline(int cf, int sALIAS)
105 struct op *t, *p, *tl = NULL;
107 t = get_command(cf, sALIAS);
109 while (token(0) == '|') {
110 if ((p = get_command(CONTIN, sALIAS)) == NULL)
113 t = tl = block(TPIPE, t, p);
115 tl = tl->right = block(TPIPE, tl->right, p);
128 t = pipeline(0, sALIAS);
130 while ((c = token(0)) == LOGAND || c == LOGOR) {
131 if ((p = pipeline(CONTIN, sALIAS)) == NULL)
133 t = block(c == LOGAND? TAND: TOR, t, p);
141 c_list(int sALIAS, bool multi)
143 struct op *t = NULL, *p, *tl = NULL;
147 while (/* CONSTCOND */ 1) {
150 * Token has always been read/rejected at this point, so
151 * we don't worry about what flags to pass token()
155 if (c == '\n' && (multi || inalias(source))) {
157 /* ignore blank lines */
161 else if (c == '&' || c == COPROC)
162 p = block(c == '&' ? TASYNC : TCOPROC, p, NULL);
168 t = tl = block(TLIST, t, p);
170 tl = tl->right = block(TLIST, tl->right, p);
178 static const char IONDELIM_delim[] = { CHAR, '<', CHAR, '<', EOS };
180 static struct ioword *
184 static struct ioword *nextiop;
187 if (nextiop != NULL) {
193 if (tpeek(cf) != REDIR)
197 ishere = (iop->ioflag & IOTYPE) == IOHERE;
198 if (iop->ioflag & IOHERESTR) {
200 } else if (ishere && tpeek(HEREDELIM) == '\n') {
202 yylval.cp = wdcopy(IONDELIM_delim, ATEMP);
203 iop->ioflag |= IOEVAL | IONDELIM;
205 musthave(LWORD, ishere ? HEREDELIM : 0);
207 iop->delim = yylval.cp;
208 if (*ident != 0 && !(iop->ioflag & IOHERESTR)) {
210 iop->ioflag |= IOEVAL;
212 if (herep > &heres[HERES - 1])
213 yyerror(Tf_toomany, "<<");
216 iop->ioname = yylval.cp;
218 if (iop->ioflag & IOBASH) {
221 nextiop = alloc(sizeof(*iop), ATEMP);
222 nextiop->ioname = cp = alloc(3, ATEMP);
224 *cp++ = digits_lc[iop->unit % 10];
227 iop->ioflag &= ~IOBASH;
229 nextiop->ioflag = IODUP;
230 nextiop->delim = NULL;
231 nextiop->heredoc = NULL;
237 nested(int type, int smark, int emark, int sALIAS)
240 struct nesting_state old_nesting;
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));
249 static const char builtin_cmd[] = {
250 QCHAR, '\\', CHAR, 'b', CHAR, 'u', CHAR, 'i',
251 CHAR, 'l', CHAR, 't', CHAR, 'i', CHAR, 'n', EOS
253 static const char let_cmd[] = {
254 CHAR, 'l', CHAR, 'e', CHAR, 't', EOS
256 static const char setA_cmd0[] = {
257 CHAR, 's', CHAR, 'e', CHAR, 't', EOS
259 static const char setA_cmd1[] = {
260 CHAR, '-', CHAR, 'A', EOS
262 static const char setA_cmd2[] = {
263 CHAR, '-', CHAR, '-', EOS
267 get_command(int cf, int sALIAS)
270 int c, iopn = 0, syniocf, lno;
271 struct ioword *iop, **iops;
273 struct nesting_state old_nesting;
275 /* NUFILE is small enough to leave this addition unchecked */
276 iops = alloc2((NUFILE + 1), sizeof(struct ioword *), ATEMP);
280 syniocf = KEYWORD|sALIAS;
281 switch (c = token(cf|KEYWORD|sALIAS|CMDASN)) {
293 syniocf &= ~(KEYWORD|sALIAS);
295 t->lineno = source->line;
296 goto get_command_start;
297 while (/* CONSTCOND */ 1) {
298 bool check_decl_utility;
300 if (XPsize(args) == 0) {
302 check_decl_utility = true;
303 cf = sALIAS | CMDASN;
304 } else if (t->u.evalflags)
305 cf = CMDWORD | CMDASN;
310 while ((iop = synio(cf)) != NULL) {
320 if (check_decl_utility) {
321 struct tbl *tt = get_builtin(ident);
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;
330 if ((XPsize(args) == 0 || Flag(FKEYWORD)) &&
331 is_wdvarassign(yylval.cp))
332 XPput(vars, yylval.cp);
334 XPput(args, yylval.cp);
338 if (XPsize(args) == 0 && XPsize(vars) == 1 &&
339 is_wdvarassign(yylval.cp)) {
342 /* wdarrassign: foo=(bar) */
345 /* manipulate the vars string */
346 tcp = XPptrv(vars)[(vars.len = 0)];
347 /* 'varname=' -> 'varname' */
348 tcp[wdscan(tcp, EOS) - tcp - 3] = EOS;
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));
355 XPput(args, wdcopy(setA_cmd2, ATEMP));
357 /* slurp in words till closing paren */
358 while (token(CONTIN) == LWORD)
359 XPput(args, yylval.cp);
360 if (symbol != /*(*/ ')')
364 * Check for "> foo (echo hi)"
365 * which AT&T ksh allows (not
366 * POSIX, but not disallowed)
369 if (XPsize(args) == 0 &&
375 /* must be a function */
376 if (iopn != 0 || XPsize(args) != 1 ||
380 musthave(/*(*/ ')', 0);
381 t = function_body(XPptrv(args)[0],
393 case ORD('(' /*)*/): {
394 unsigned int subshell_nesting_type_saved;
396 subshell_nesting_type_saved = subshell_nesting_type;
397 subshell_nesting_type = ORD(')');
398 t = nested(TPAREN, ORD('('), ORD(')'), sALIAS);
399 subshell_nesting_type = subshell_nesting_type_saved;
404 t = nested(TBRACE, ORD('{'), ORD('}'), sALIAS);
408 /* leave KEYWORD in syniocf (allow if (( 1 )) then ...) */
411 switch (token(LETEXPR)) {
422 XPput(args, wdcopy(builtin_cmd, ATEMP));
423 XPput(args, wdcopy(let_cmd, ATEMP));
424 XPput(args, yylval.cp);
427 case DBRACKET: /* [[ .. ]] */
428 /* leave KEYWORD in syniocf (allow if [[ -n 1 ]] then ...) */
429 t = newtp(TDBRACKET);
434 te.flags = TEF_DBRACKET;
436 te.isa = dbtestp_isa;
437 te.getopnd = dbtestp_getopnd;
438 te.eval = dbtestp_eval;
439 te.error = dbtestp_error;
447 t = newtp((c == FOR) ? TFOR : TSELECT);
448 musthave(LWORD, CMDASN);
449 if (!is_wdvarname(yylval.cp, true))
450 yyerror("%s: bad identifier",
451 c == FOR ? "for" : Tselect);
452 strdupx(t->str, ident, ATEMP);
453 nesting_push(&old_nesting, c);
454 t->vars = wordlist(sALIAS);
455 t->left = dogroup(sALIAS);
456 nesting_pop(&old_nesting);
461 nesting_push(&old_nesting, c);
462 t = newtp((c == WHILE) ? TWHILE : TUNTIL);
463 t->left = c_list(sALIAS, true);
464 t->right = dogroup(sALIAS);
465 nesting_pop(&old_nesting);
472 nesting_push(&old_nesting, c);
473 t->left = caselist(sALIAS);
474 nesting_pop(&old_nesting);
478 nesting_push(&old_nesting, c);
480 t->left = c_list(sALIAS, true);
481 t->right = thenpart(sALIAS);
482 musthave(FI, KEYWORD|sALIAS);
483 nesting_pop(&old_nesting);
487 syniocf &= ~(KEYWORD|sALIAS);
488 t = pipeline(0, sALIAS);
491 t = block(TBANG, NULL, t);
495 syniocf &= ~(KEYWORD|sALIAS);
496 t = pipeline(0, sALIAS);
497 if (t && t->type == TCOM) {
498 t->str = alloc(2, ATEMP);
503 t = block(TTIME, t, NULL);
508 t = function_body(yylval.cp, sALIAS, true);
512 while ((iop = synio(syniocf)) != NULL) {
514 yyerror(Tf_toomany, Tredirection);
523 iops = aresize2(iops, iopn, sizeof(struct ioword *), ATEMP);
527 if (t->type == TCOM || t->type == TDBRACKET) {
529 t->args = (const char **)XPclose(args);
531 t->vars = (char **)XPclose(vars);
538 t = block(TBRACE, t, NULL);
539 t->ioact = t->left->ioact;
540 t->left->ioact = NULL;
552 c = token(CONTIN|KEYWORD|sALIAS);
554 * A {...} can be used instead of do...done for for/select loops
555 * 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...
561 else if ((unsigned int)c == ORD('{'))
565 list = c_list(sALIAS, true);
566 musthave(c, KEYWORD|sALIAS);
575 musthave(THEN, KEYWORD|sALIAS);
577 t->left = c_list(sALIAS, true);
580 t->right = elsepart(sALIAS);
589 switch (token(KEYWORD|sALIAS|CMDASN)) {
591 if ((t = c_list(sALIAS, true)) == NULL)
597 t->left = c_list(sALIAS, true);
598 t->right = thenpart(sALIAS);
613 c = token(CONTIN|KEYWORD|sALIAS);
614 /* A {...} can be used instead of in...esac for case statements */
617 else if ((unsigned int)c == ORD('{'))
623 while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) {
624 struct op *tc = casepart(c, sALIAS);
626 t = tl = tc, tl->right = NULL;
628 tl->right = tc, tl = tc;
630 musthave(c, KEYWORD|sALIAS);
635 casepart(int endtok, int sALIAS)
643 if ((unsigned int)token(CONTIN | KEYWORD) != ORD('('))
651 if (symbol != endtok) {
652 strdupx(yylval.cp, (unsigned int)symbol ==
653 ORD('}') ? Tcbrace : Tesac, ATEMP);
660 XPput(ptns, yylval.cp);
661 } while (token(0) == '|');
664 t->vars = (char **)XPclose(ptns);
665 musthave(ORD(')'), 0);
667 t->left = c_list(sALIAS, true);
669 /* initialise to default for ;; or omitted */
670 t->u.charflag = ORD(';');
671 /* SUSv4 requires the ;; except in the last casepart */
672 if ((tpeek(CONTIN|KEYWORD|sALIAS)) != endtok)
677 t->u.charflag = ORD('|');
681 t->u.charflag = ORD('&');
684 /* initialised above, but we need to eat the token */
691 function_body(char *name, int sALIAS,
692 /* function foo { ... } vs foo() { .. } */
698 sname = wdstrip(name, 0);
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:
703 * NUL TAB NL SP " $ & ' ( ) ; < = > \ ` |
704 * C_QUOTE|C_SPC covers all but adds # * ? [ ]
706 for (p = sname; *p; p++)
707 if (ctype(*p, C_QUOTE | C_SPC))
708 yyerror(Tinvname, sname, Tfunction);
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.
717 if ((unsigned int)tpeek(CONTIN|KEYWORD|sALIAS) == ORD('(' /*)*/)) {
718 /* function foo () { //}*/
720 musthave(ORD(/*(*/ ')'), 0);
721 /* degrade to POSIX function */
724 musthave(ORD('{' /*}*/), CONTIN|KEYWORD|sALIAS);
730 t->u.ksh_func = tobool(ksh_func);
731 t->lineno = source->line;
733 if ((t->left = get_command(CONTIN, sALIAS)) == NULL) {
736 * Probably something like foo() followed by EOF or ';'.
737 * 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.
741 t->left = newtp(TCOM);
742 /* (2 * sizeof(char *)) is small enough */
743 t->left->args = alloc(2 * sizeof(char *), ATEMP);
744 t->left->args[0] = tv = alloc(3, ATEMP);
748 t->left->args[1] = NULL;
749 t->left->vars = alloc(sizeof(char *), ATEMP);
750 t->left->vars[0] = NULL;
764 /* POSIX does not do alias expansion here... */
765 if ((c = token(CONTIN|KEYWORD|sALIAS)) != IN) {
767 /* non-POSIX, but AT&T ksh accepts a ; here */
771 while ((c = token(0)) == LWORD)
772 XPput(args, yylval.cp);
773 if (c != '\n' && c != ';')
776 return ((char **)XPclose(args));
780 * supporting functions
784 block(int type, struct op *t1, struct op *t2)
794 static const struct tokeninfo {
801 { "then", THEN, true },
802 { "else", ELSE, true },
803 { "elif", ELIF, true },
805 { "case", CASE, true },
806 { Tesac, ESAC, true },
807 { "for", FOR, true },
808 { Tselect, SELECT, true },
809 { "while", WHILE, true },
810 { "until", UNTIL, true },
812 { "done", DONE, true },
814 { Tfunction, FUNCTION, true },
815 { Ttime, TIME, true },
816 { "{", ORD('{'), true },
817 { Tcbrace, ORD('}'), true },
819 { "[[", DBRACKET, true },
820 /* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */
821 { "&&", LOGAND, false },
822 { "||", LOGOR, false },
823 { ";;", BREAK, false },
824 { ";|", BRKEV, false },
825 { ";&", BRKFT, false },
826 { "((", MDPAREN, false },
827 { "|&", COPROC, false },
828 /* and some special cases... */
829 { "newline", ORD('\n'), false },
836 struct tokeninfo const *tt;
839 ktinit(APERM, &keywords,
840 /* currently 28 keywords: 75% of 64 = 2^6 */
842 for (tt = tokentab; tt->name; tt++) {
844 p = ktenter(&keywords, tt->name, hash(tt->name));
845 p->flag |= DEFINED|ISSET;
853 syntaxerr(const char *what)
855 /* 23<<- is the longest redirection, I think */
858 struct tokeninfo const *tt;
868 if (nesting.start_token) {
869 c = nesting.start_token;
870 source->errline = nesting.start_line;
874 /* don't quote the EOF */
875 yyerror("%s: unexpected EOF", Tsynerr);
879 s = snptreef(NULL, 32, Tf_S, yylval.cp);
883 s = snptreef(redir, sizeof(redir), Tft_R, yylval.iop);
887 for (tt = tokentab; tt->name; tt++)
893 if (c > 0 && c < 256) {
897 shf_snprintf(redir, sizeof(redir),
902 yyerror(Tf_sD_s_qs, Tsynerr, what, s);
906 nesting_push(struct nesting_state *save, int tok)
909 nesting.start_token = tok;
910 nesting.start_line = source->line;
914 nesting_pop(struct nesting_state *saved)
924 t = alloc(sizeof(struct op), ATEMP);
930 t->left = t->right = NULL;
936 compile(Source *s, bool skiputf8bom, bool doalias)
938 nesting.start_token = 0;
939 nesting.start_line = 0;
948 /* Check if we are in the middle of reading an alias */
950 inalias(struct source *s)
952 while (s && s->type == SALIAS) {
953 if (!(s->flags & SF_ALIASEND))
962 * Order important - indexed by Test_meta values
963 * 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.
967 static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS };
968 static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS };
969 static const char dbtest_not[] = { CHAR, '!', EOS };
970 static const char dbtest_oparen[] = { CHAR, '(', EOS };
971 static const char dbtest_cparen[] = { CHAR, ')', EOS };
972 const char * const dbtest_tokens[] = {
973 dbtest_or, dbtest_and, dbtest_not,
974 dbtest_oparen, dbtest_cparen
976 static const char db_close[] = { CHAR, ']', CHAR, ']', EOS };
977 static const char db_lthan[] = { CHAR, '<', EOS };
978 static const char db_gthan[] = { CHAR, '>', EOS };
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).
986 dbtestp_isa(Test_env *te, Test_meta meta)
988 int c = tpeek(CMDASN | (meta == TM_BINOP ? 0 : CONTIN));
991 Test_op ret = TO_NONOP;
994 uqword = c == LWORD && *ident;
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)
1004 ret = (unsigned int)c == ORD('(') /*)*/ ? TO_NONNULL : TO_NONOP;
1005 else if (meta == TM_CPAREN)
1006 ret = (unsigned int)c == /*(*/ ORD(')') ? TO_NONNULL : TO_NONOP;
1007 else if (meta == TM_UNOP || meta == TM_BINOP) {
1008 if (meta == TM_BINOP && c == REDIR &&
1009 (yylval.iop->ioflag == IOREAD ||
1010 yylval.iop->ioflag == IOWRITE)) {
1012 save = wdcopy(yylval.iop->ioflag == IOREAD ?
1013 db_lthan : db_gthan, ATEMP);
1014 } else if (uqword && (ret = test_isop(meta, ident)))
1017 /* meta == TM_END */
1018 ret = (uqword && !strcmp(yylval.cp,
1019 db_close)) ? TO_NONNULL : TO_NONOP;
1020 if (ret != TO_NONOP) {
1022 if ((unsigned int)meta < NELEM(dbtest_tokens))
1023 save = wdcopy(dbtest_tokens[(int)meta], ATEMP);
1025 XPput(*te->pos.av, save);
1031 dbtestp_getopnd(Test_env *te, Test_op op MKSH_A_UNUSED,
1032 bool do_eval MKSH_A_UNUSED)
1034 int c = tpeek(CMDASN);
1040 XPput(*te->pos.av, yylval.cp);
1046 dbtestp_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)
1054 dbtestp_error(Test_env *te, int offset, const char *msg)
1056 te->flags |= TEF_ERROR;
1060 /* Kludgy to say the least... */
1062 yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av) +
1072 #define EOVERFLOW ERANGE
1074 #define EOVERFLOW EINVAL
1079 parse_usec(const char *s, struct timeval *tv)
1085 /* parse integral part */
1086 while (ctype(*s, C_DIGIT)) {
1087 tt.tv_sec = tv->tv_sec * 10 + ksh_numdig(*s++);
1088 /*XXX this overflow check maybe UB */
1089 if (tt.tv_sec / 10 != tv->tv_sec) {
1093 tv->tv_sec = tt.tv_sec;
1098 /* no decimal fraction */
1100 else if (*s++ != '.') {
1101 /* junk after integral part */
1106 /* parse decimal fraction */
1108 while (ctype(*s, C_DIGIT)) {
1109 tv->tv_usec += i * ksh_numdig(*s++);
1114 /* check for junk after fractional part */
1115 while (ctype(*s, C_DIGIT))
1122 /* end of input string reached, no errors */
1128 * Helper function called from within lex.c:yylex() to parse
1129 * a COMSUB recursively using the main shell parser and lexer
1132 yyrecursive(int subtype)
1136 struct yyrecursive_state *ys;
1137 unsigned int stok, etok;
1139 if (subtype != COMSUB) {
1147 ys = alloc(sizeof(struct yyrecursive_state), ATEMP);
1149 /* tell the lexer to accept a closing parenthesis as EOD */
1150 ys->old_nesting_type = subshell_nesting_type;
1151 subshell_nesting_type = etok;
1153 /* push reject state, parse recursively, pop reject state */
1154 ys->old_reject = reject;
1155 ys->old_symbol = symbol;
1157 memcpy(ys->old_heres, heres, sizeof(heres));
1158 ys->old_herep = herep;
1160 ys->next = e->yyrecursive_statep;
1161 e->yyrecursive_statep = ys;
1162 /* we use TPAREN as a helper container here */
1163 t = nested(TPAREN, stok, etok, ALIAS);
1164 yyrecursive_pop(false);
1166 /* t->left because nested(TPAREN, ...) hides our goodies there */
1167 cp = snptreef(NULL, 0, Tf_T, t->left);
1174 yyrecursive_pop(bool popall)
1176 struct yyrecursive_state *ys;
1179 if (!(ys = e->yyrecursive_statep))
1181 e->yyrecursive_statep = ys->next;
1183 memcpy(heres, ys->old_heres, sizeof(heres));
1184 herep = ys->old_herep;
1185 reject = ys->old_reject;
1186 symbol = ys->old_symbol;
1188 subshell_nesting_type = ys->old_nesting_type;