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