1 /* $NetBSD: syn.c,v 1.6 2004/07/07 19:20:09 mycroft Exp $ */ 2 3 /* 4 * shell parser (C version) 5 */ 6 #include <sys/cdefs.h> 7 8 #ifndef lint 9 __RCSID("$NetBSD: syn.c,v 1.6 2004/07/07 19:20:09 mycroft Exp $"); 10 #endif 11 12 13 #include "sh.h" 14 #include "c_test.h" 15 16 struct nesting_state { 17 int start_token; /* token than began nesting (eg, FOR) */ 18 int start_line; /* line nesting began on */ 19 }; 20 21 static void yyparse ARGS((void)); 22 static struct op *pipeline ARGS((int cf)); 23 static struct op *andor ARGS((void)); 24 static struct op *c_list ARGS((int multi)); 25 static struct ioword *synio ARGS((int cf)); 26 static void musthave ARGS((int c, int cf)); 27 static struct op *nested ARGS((int type, int smark, int emark)); 28 static struct op *get_command ARGS((int cf)); 29 static struct op *dogroup ARGS((void)); 30 static struct op *thenpart ARGS((void)); 31 static struct op *elsepart ARGS((void)); 32 static struct op *caselist ARGS((void)); 33 static struct op *casepart ARGS((int endtok)); 34 static struct op *function_body ARGS((char *name, int ksh_func)); 35 static char ** wordlist ARGS((void)); 36 static struct op *block ARGS((int type, struct op *t1, struct op *t2, 37 char **wp)); 38 static struct op *newtp ARGS((int type)); 39 static void syntaxerr ARGS((const char *what)) 40 GCC_FUNC_ATTR(noreturn); 41 static void nesting_push ARGS((struct nesting_state *save, int tok)); 42 static void nesting_pop ARGS((struct nesting_state *saved)); 43 static int assign_command ARGS((char *s)); 44 static int inalias ARGS((struct source *s)); 45 #ifdef KSH 46 static int dbtestp_isa ARGS((Test_env *te, Test_meta meta)); 47 static const char *dbtestp_getopnd ARGS((Test_env *te, Test_op op, 48 int do_eval)); 49 static int dbtestp_eval ARGS((Test_env *te, Test_op op, const char *opnd1, 50 const char *opnd2, int do_eval)); 51 static void dbtestp_error ARGS((Test_env *te, int offset, const char *msg)); 52 #endif /* KSH */ 53 54 static struct op *outtree; /* yyparse output */ 55 56 static struct nesting_state nesting; /* \n changed to ; */ 57 58 static int reject; /* token(cf) gets symbol again */ 59 static int symbol; /* yylex value */ 60 61 #define REJECT (reject = 1) 62 #define ACCEPT (reject = 0) 63 #define token(cf) \ 64 ((reject) ? (ACCEPT, symbol) : (symbol = yylex(cf))) 65 #define tpeek(cf) \ 66 ((reject) ? (symbol) : (REJECT, symbol = yylex(cf))) 67 68 static void 69 yyparse() 70 { 71 int c; 72 73 ACCEPT; 74 75 outtree = c_list(source->type == SSTRING); 76 c = tpeek(0); 77 if (c == 0 && !outtree) 78 outtree = newtp(TEOF); 79 else if (c != '\n' && c != 0) 80 syntaxerr((char *) 0); 81 } 82 83 static struct op * 84 pipeline(cf) 85 int cf; 86 { 87 register struct op *t, *p, *tl = NULL; 88 89 t = get_command(cf); 90 if (t != NULL) { 91 while (token(0) == '|') { 92 if ((p = get_command(CONTIN)) == NULL) 93 syntaxerr((char *) 0); 94 if (tl == NULL) 95 t = tl = block(TPIPE, t, p, NOWORDS); 96 else 97 tl = tl->right = block(TPIPE, tl->right, p, NOWORDS); 98 } 99 REJECT; 100 } 101 return (t); 102 } 103 104 static struct op * 105 andor() 106 { 107 register struct op *t, *p; 108 register int c; 109 110 t = pipeline(0); 111 if (t != NULL) { 112 while ((c = token(0)) == LOGAND || c == LOGOR) { 113 if ((p = pipeline(CONTIN)) == NULL) 114 syntaxerr((char *) 0); 115 t = block(c == LOGAND? TAND: TOR, t, p, NOWORDS); 116 } 117 REJECT; 118 } 119 return (t); 120 } 121 122 static struct op * 123 c_list(multi) 124 int multi; 125 { 126 register struct op *t = NULL, *p, *tl = NULL; 127 register int c; 128 int have_sep; 129 130 while (1) { 131 p = andor(); 132 /* Token has always been read/rejected at this point, so 133 * we don't worry about what flags to pass token() 134 */ 135 c = token(0); 136 have_sep = 1; 137 if (c == '\n' && (multi || inalias(source))) { 138 if (!p) /* ignore blank lines */ 139 continue; 140 } else if (!p) 141 break; 142 else if (c == '&' || c == COPROC) 143 p = block(c == '&' ? TASYNC : TCOPROC, 144 p, NOBLOCK, NOWORDS); 145 else if (c != ';') 146 have_sep = 0; 147 if (!t) 148 t = p; 149 else if (!tl) 150 t = tl = block(TLIST, t, p, NOWORDS); 151 else 152 tl = tl->right = block(TLIST, tl->right, p, NOWORDS); 153 if (!have_sep) 154 break; 155 } 156 REJECT; 157 return t; 158 } 159 160 static struct ioword * 161 synio(cf) 162 int cf; 163 { 164 register struct ioword *iop; 165 int ishere; 166 167 if (tpeek(cf) != REDIR) 168 return NULL; 169 ACCEPT; 170 iop = yylval.iop; 171 ishere = (iop->flag&IOTYPE) == IOHERE; 172 musthave(LWORD, ishere ? HEREDELIM : 0); 173 if (ishere) { 174 iop->delim = yylval.cp; 175 if (*ident != 0) /* unquoted */ 176 iop->flag |= IOEVAL; 177 if (herep >= &heres[HERES]) 178 yyerror("too many <<'s\n"); 179 *herep++ = iop; 180 } else 181 iop->name = yylval.cp; 182 return iop; 183 } 184 185 static void 186 musthave(c, cf) 187 int c, cf; 188 { 189 if ((token(cf)) != c) 190 syntaxerr((char *) 0); 191 } 192 193 static struct op * 194 nested(type, smark, emark) 195 int type, smark, emark; 196 { 197 register struct op *t; 198 struct nesting_state old_nesting; 199 200 nesting_push(&old_nesting, smark); 201 t = c_list(TRUE); 202 musthave(emark, KEYWORD|ALIAS); 203 nesting_pop(&old_nesting); 204 return (block(type, t, NOBLOCK, NOWORDS)); 205 } 206 207 static struct op * 208 get_command(cf) 209 int cf; 210 { 211 register struct op *t; 212 register int c, iopn = 0, syniocf; 213 struct ioword *iop, **iops; 214 XPtrV args, vars; 215 struct nesting_state old_nesting; 216 217 iops = (struct ioword **) alloc(sizeofN(struct ioword *, NUFILE+1), 218 ATEMP); 219 XPinit(args, 16); 220 XPinit(vars, 16); 221 222 syniocf = KEYWORD|ALIAS; 223 switch (c = token(cf|KEYWORD|ALIAS|VARASN)) { 224 default: 225 REJECT; 226 afree((void*) iops, ATEMP); 227 XPfree(args); 228 XPfree(vars); 229 return NULL; /* empty line */ 230 231 case LWORD: 232 case REDIR: 233 REJECT; 234 syniocf &= ~(KEYWORD|ALIAS); 235 t = newtp(TCOM); 236 t->lineno = source->line; 237 while (1) { 238 cf = (t->u.evalflags ? ARRAYVAR : 0) 239 | (XPsize(args) == 0 ? ALIAS|VARASN : CMDWORD); 240 switch (tpeek(cf)) { 241 case REDIR: 242 if (iopn >= NUFILE) 243 yyerror("too many redirections\n"); 244 iops[iopn++] = synio(cf); 245 break; 246 247 case LWORD: 248 ACCEPT; 249 /* the iopn == 0 and XPsize(vars) == 0 are 250 * dubious but at&t ksh acts this way 251 */ 252 if (iopn == 0 && XPsize(vars) == 0 253 && XPsize(args) == 0 254 && assign_command(ident)) 255 t->u.evalflags = DOVACHECK; 256 if ((XPsize(args) == 0 || Flag(FKEYWORD)) 257 && is_wdvarassign(yylval.cp)) 258 XPput(vars, yylval.cp); 259 else 260 XPput(args, yylval.cp); 261 break; 262 263 case '(': 264 /* Check for "> foo (echo hi)", which at&t ksh 265 * allows (not POSIX, but not disallowed) 266 */ 267 afree(t, ATEMP); 268 if (XPsize(args) == 0 && XPsize(vars) == 0) { 269 ACCEPT; 270 goto Subshell; 271 } 272 /* Must be a function */ 273 if (iopn != 0 || XPsize(args) != 1 274 || XPsize(vars) != 0) 275 syntaxerr((char *) 0); 276 ACCEPT; 277 /*(*/ 278 musthave(')', 0); 279 t = function_body(XPptrv(args)[0], FALSE); 280 goto Leave; 281 282 default: 283 goto Leave; 284 } 285 } 286 Leave: 287 break; 288 289 Subshell: 290 case '(': 291 t = nested(TPAREN, '(', ')'); 292 break; 293 294 case '{': /*}*/ 295 t = nested(TBRACE, '{', '}'); 296 break; 297 298 #ifdef KSH 299 case MDPAREN: 300 { 301 static const char let_cmd[] = { CHAR, 'l', CHAR, 'e', 302 CHAR, 't', EOS }; 303 /* Leave KEYWORD in syniocf (allow if (( 1 )) then ...) */ 304 t = newtp(TCOM); 305 t->lineno = source->line; 306 ACCEPT; 307 XPput(args, wdcopy(let_cmd, ATEMP)); 308 musthave(LWORD,LETEXPR); 309 XPput(args, yylval.cp); 310 break; 311 } 312 #endif /* KSH */ 313 314 #ifdef KSH 315 case DBRACKET: /* [[ .. ]] */ 316 /* Leave KEYWORD in syniocf (allow if [[ -n 1 ]] then ...) */ 317 t = newtp(TDBRACKET); 318 ACCEPT; 319 { 320 Test_env te; 321 322 te.flags = TEF_DBRACKET; 323 te.pos.av = &args; 324 te.isa = dbtestp_isa; 325 te.getopnd = dbtestp_getopnd; 326 te.eval = dbtestp_eval; 327 te.error = dbtestp_error; 328 329 test_parse(&te); 330 } 331 break; 332 #endif /* KSH */ 333 334 case FOR: 335 case SELECT: 336 t = newtp((c == FOR) ? TFOR : TSELECT); 337 musthave(LWORD, ARRAYVAR); 338 if (!is_wdvarname(yylval.cp, TRUE)) 339 yyerror("%s: bad identifier\n", 340 c == FOR ? "for" : "select"); 341 t->str = str_save(ident, ATEMP); 342 nesting_push(&old_nesting, c); 343 t->vars = wordlist(); 344 t->left = dogroup(); 345 nesting_pop(&old_nesting); 346 break; 347 348 case WHILE: 349 case UNTIL: 350 nesting_push(&old_nesting, c); 351 t = newtp((c == WHILE) ? TWHILE : TUNTIL); 352 t->left = c_list(TRUE); 353 t->right = dogroup(); 354 nesting_pop(&old_nesting); 355 break; 356 357 case CASE: 358 t = newtp(TCASE); 359 musthave(LWORD, 0); 360 t->str = yylval.cp; 361 nesting_push(&old_nesting, c); 362 t->left = caselist(); 363 nesting_pop(&old_nesting); 364 break; 365 366 case IF: 367 nesting_push(&old_nesting, c); 368 t = newtp(TIF); 369 t->left = c_list(TRUE); 370 t->right = thenpart(); 371 musthave(FI, KEYWORD|ALIAS); 372 nesting_pop(&old_nesting); 373 break; 374 375 case BANG: 376 syniocf &= ~(KEYWORD|ALIAS); 377 t = pipeline(0); 378 if (t == (struct op *) 0) 379 syntaxerr((char *) 0); 380 t = block(TBANG, NOBLOCK, t, NOWORDS); 381 break; 382 383 case TIME: 384 syniocf &= ~(KEYWORD|ALIAS); 385 t = pipeline(0); 386 t = block(TTIME, t, NOBLOCK, NOWORDS); 387 break; 388 389 case FUNCTION: 390 musthave(LWORD, 0); 391 t = function_body(yylval.cp, TRUE); 392 break; 393 } 394 395 while ((iop = synio(syniocf)) != NULL) { 396 if (iopn >= NUFILE) 397 yyerror("too many redirections\n"); 398 iops[iopn++] = iop; 399 } 400 401 if (iopn == 0) { 402 afree((void*) iops, ATEMP); 403 t->ioact = NULL; 404 } else { 405 iops[iopn++] = NULL; 406 iops = (struct ioword **) aresize((void*) iops, 407 sizeofN(struct ioword *, iopn), ATEMP); 408 t->ioact = iops; 409 } 410 411 if (t->type == TCOM || t->type == TDBRACKET) { 412 XPput(args, NULL); 413 t->args = (char **) XPclose(args); 414 XPput(vars, NULL); 415 t->vars = (char **) XPclose(vars); 416 } else { 417 XPfree(args); 418 XPfree(vars); 419 } 420 421 return t; 422 } 423 424 static struct op * 425 dogroup() 426 { 427 register int c; 428 register struct op *list; 429 430 c = token(CONTIN|KEYWORD|ALIAS); 431 /* A {...} can be used instead of do...done for for/select loops 432 * but not for while/until loops - we don't need to check if it 433 * is a while loop because it would have been parsed as part of 434 * the conditional command list... 435 */ 436 if (c == DO) 437 c = DONE; 438 else if (c == '{') 439 c = '}'; 440 else 441 syntaxerr((char *) 0); 442 list = c_list(TRUE); 443 musthave(c, KEYWORD|ALIAS); 444 return list; 445 } 446 447 static struct op * 448 thenpart() 449 { 450 register struct op *t; 451 452 musthave(THEN, KEYWORD|ALIAS); 453 t = newtp(0); 454 t->left = c_list(TRUE); 455 if (t->left == NULL) 456 syntaxerr((char *) 0); 457 t->right = elsepart(); 458 return (t); 459 } 460 461 static struct op * 462 elsepart() 463 { 464 register struct op *t; 465 466 switch (token(KEYWORD|ALIAS|VARASN)) { 467 case ELSE: 468 if ((t = c_list(TRUE)) == NULL) 469 syntaxerr((char *) 0); 470 return (t); 471 472 case ELIF: 473 t = newtp(TELIF); 474 t->left = c_list(TRUE); 475 t->right = thenpart(); 476 return (t); 477 478 default: 479 REJECT; 480 } 481 return NULL; 482 } 483 484 static struct op * 485 caselist() 486 { 487 register struct op *t, *tl; 488 int c; 489 490 c = token(CONTIN|KEYWORD|ALIAS); 491 /* A {...} can be used instead of in...esac for case statements */ 492 if (c == IN) 493 c = ESAC; 494 else if (c == '{') 495 c = '}'; 496 else 497 syntaxerr((char *) 0); 498 t = tl = NULL; 499 while ((tpeek(CONTIN|KEYWORD|ESACONLY)) != c) { /* no ALIAS here */ 500 struct op *tc = casepart(c); 501 if (tl == NULL) 502 t = tl = tc, tl->right = NULL; 503 else 504 tl->right = tc, tl = tc; 505 } 506 musthave(c, KEYWORD|ALIAS); 507 return (t); 508 } 509 510 static struct op * 511 casepart(endtok) 512 int endtok; 513 { 514 register struct op *t; 515 register int c; 516 XPtrV ptns; 517 518 XPinit(ptns, 16); 519 t = newtp(TPAT); 520 c = token(CONTIN|KEYWORD); /* no ALIAS here */ 521 if (c != '(') 522 REJECT; 523 do { 524 musthave(LWORD, 0); 525 XPput(ptns, yylval.cp); 526 } while ((c = token(0)) == '|'); 527 REJECT; 528 XPput(ptns, NULL); 529 t->vars = (char **) XPclose(ptns); 530 musthave(')', 0); 531 532 t->left = c_list(TRUE); 533 /* Note: Posix requires the ;; */ 534 if ((tpeek(CONTIN|KEYWORD|ALIAS)) != endtok) 535 musthave(BREAK, CONTIN|KEYWORD|ALIAS); 536 return (t); 537 } 538 539 static struct op * 540 function_body(name, ksh_func) 541 char *name; 542 int ksh_func; /* function foo { ... } vs foo() { .. } */ 543 { 544 char *sname, *p; 545 struct op *t; 546 int old_func_parse; 547 548 sname = wdstrip(name); 549 /* Check for valid characters in name. posix and ksh93 say only 550 * allow [a-zA-Z_0-9] but this allows more as old pdksh's have 551 * allowed more (the following were never allowed: 552 * nul space nl tab $ ' " \ ` ( ) & | ; = < > 553 * C_QUOTE covers all but = and adds # [ ? *) 554 */ 555 for (p = sname; *p; p++) 556 if (ctype(*p, C_QUOTE) || *p == '=') 557 yyerror("%s: invalid function name\n", sname); 558 559 t = newtp(TFUNCT); 560 t->str = sname; 561 t->u.ksh_func = ksh_func; 562 t->lineno = source->line; 563 564 /* Note that POSIX allows only compound statements after foo(), sh and 565 * at&t ksh allow any command, go with the later since it shouldn't 566 * break anything. However, for function foo, at&t ksh only accepts 567 * an open-brace. 568 */ 569 if (ksh_func) { 570 musthave('{', CONTIN|KEYWORD|ALIAS); /* } */ 571 REJECT; 572 } 573 574 old_func_parse = e->flags & EF_FUNC_PARSE; 575 e->flags |= EF_FUNC_PARSE; 576 if ((t->left = get_command(CONTIN)) == (struct op *) 0) { 577 /* 578 * Probably something like foo() followed by eof or ;. 579 * This is accepted by sh and ksh88. 580 * To make "typeset -f foo" work reliably (so its output can 581 * be used as input), we pretend there is a colon here. 582 */ 583 t->left = newtp(TCOM); 584 t->left->args = (char **) alloc(sizeof(char *) * 2, ATEMP); 585 t->left->args[0] = alloc(sizeof(char) * 3, ATEMP); 586 t->left->args[0][0] = CHAR; 587 t->left->args[0][1] = ':'; 588 t->left->args[0][2] = EOS; 589 t->left->args[1] = (char *) 0; 590 t->left->vars = (char **) alloc(sizeof(char *), ATEMP); 591 t->left->vars[0] = (char *) 0; 592 t->left->lineno = 1; 593 } 594 if (!old_func_parse) 595 e->flags &= ~EF_FUNC_PARSE; 596 597 return t; 598 } 599 600 static char ** 601 wordlist() 602 { 603 register int c; 604 XPtrV args; 605 606 XPinit(args, 16); 607 /* Posix does not do alias expansion here... */ 608 if ((c = token(CONTIN|KEYWORD|ALIAS)) != IN) { 609 if (c != ';') /* non-POSIX, but at&t ksh accepts a ; here */ 610 REJECT; 611 return NULL; 612 } 613 while ((c = token(0)) == LWORD) 614 XPput(args, yylval.cp); 615 if (c != '\n' && c != ';') 616 syntaxerr((char *) 0); 617 if (XPsize(args) == 0) { 618 XPfree(args); 619 return NULL; 620 } else { 621 XPput(args, NULL); 622 return (char **) XPclose(args); 623 } 624 } 625 626 /* 627 * supporting functions 628 */ 629 630 static struct op * 631 block(type, t1, t2, wp) 632 int type; 633 struct op *t1, *t2; 634 char **wp; 635 { 636 register struct op *t; 637 638 t = newtp(type); 639 t->left = t1; 640 t->right = t2; 641 t->vars = wp; 642 return (t); 643 } 644 645 const struct tokeninfo { 646 const char *name; 647 short val; 648 short reserved; 649 } tokentab[] = { 650 /* Reserved words */ 651 { "if", IF, TRUE }, 652 { "then", THEN, TRUE }, 653 { "else", ELSE, TRUE }, 654 { "elif", ELIF, TRUE }, 655 { "fi", FI, TRUE }, 656 { "case", CASE, TRUE }, 657 { "esac", ESAC, TRUE }, 658 { "for", FOR, TRUE }, 659 #ifdef KSH 660 { "select", SELECT, TRUE }, 661 #endif /* KSH */ 662 { "while", WHILE, TRUE }, 663 { "until", UNTIL, TRUE }, 664 { "do", DO, TRUE }, 665 { "done", DONE, TRUE }, 666 { "in", IN, TRUE }, 667 { "function", FUNCTION, TRUE }, 668 { "time", TIME, TRUE }, 669 { "{", '{', TRUE }, 670 { "}", '}', TRUE }, 671 { "!", BANG, TRUE }, 672 #ifdef KSH 673 { "[[", DBRACKET, TRUE }, 674 #endif /* KSH */ 675 /* Lexical tokens (0[EOF], LWORD and REDIR handled specially) */ 676 { "&&", LOGAND, FALSE }, 677 { "||", LOGOR, FALSE }, 678 { ";;", BREAK, FALSE }, 679 #ifdef KSH 680 { "((", MDPAREN, FALSE }, 681 { "|&", COPROC, FALSE }, 682 #endif /* KSH */ 683 /* and some special cases... */ 684 { "newline", '\n', FALSE }, 685 { 0 } 686 }; 687 688 void 689 initkeywords() 690 { 691 register struct tokeninfo const *tt; 692 register struct tbl *p; 693 694 tinit(&keywords, APERM, 32); /* must be 2^n (currently 20 keywords) */ 695 for (tt = tokentab; tt->name; tt++) { 696 if (tt->reserved) { 697 p = tenter(&keywords, tt->name, hash(tt->name)); 698 p->flag |= DEFINED|ISSET; 699 p->type = CKEYWD; 700 p->val.i = tt->val; 701 } 702 } 703 } 704 705 static void 706 syntaxerr(what) 707 const char *what; 708 { 709 char redir[6]; /* 2<<- is the longest redirection, I think */ 710 const char *s; 711 struct tokeninfo const *tt; 712 int c; 713 714 if (!what) 715 what = "unexpected"; 716 REJECT; 717 c = token(0); 718 Again: 719 switch (c) { 720 case 0: 721 if (nesting.start_token) { 722 c = nesting.start_token; 723 source->errline = nesting.start_line; 724 what = "unmatched"; 725 goto Again; 726 } 727 /* don't quote the EOF */ 728 yyerror("syntax error: unexpected EOF\n"); 729 /*NOTREACHED*/ 730 731 case LWORD: 732 s = snptreef((char *) 0, 32, "%S", yylval.cp); 733 break; 734 735 case REDIR: 736 s = snptreef(redir, sizeof(redir), "%R", yylval.iop); 737 break; 738 739 default: 740 for (tt = tokentab; tt->name; tt++) 741 if (tt->val == c) 742 break; 743 if (tt->name) 744 s = tt->name; 745 else { 746 if (c > 0 && c < 256) { 747 redir[0] = c; 748 redir[1] = '\0'; 749 } else 750 shf_snprintf(redir, sizeof(redir), 751 "?%d", c); 752 s = redir; 753 } 754 } 755 yyerror("syntax error: `%s' %s\n", s, what); 756 } 757 758 static void 759 nesting_push(save, tok) 760 struct nesting_state *save; 761 int tok; 762 { 763 *save = nesting; 764 nesting.start_token = tok; 765 nesting.start_line = source->line; 766 } 767 768 static void 769 nesting_pop(saved) 770 struct nesting_state *saved; 771 { 772 nesting = *saved; 773 } 774 775 static struct op * 776 newtp(type) 777 int type; 778 { 779 register struct op *t; 780 781 t = (struct op *) alloc(sizeof(*t), ATEMP); 782 t->type = type; 783 t->u.evalflags = 0; 784 t->args = t->vars = NULL; 785 t->ioact = NULL; 786 t->left = t->right = NULL; 787 t->str = NULL; 788 return (t); 789 } 790 791 struct op * 792 compile(s) 793 Source *s; 794 { 795 nesting.start_token = 0; 796 nesting.start_line = 0; 797 herep = heres; 798 source = s; 799 yyparse(); 800 return outtree; 801 } 802 803 /* This kludge exists to take care of sh/at&t ksh oddity in which 804 * the arguments of alias/export/readonly/typeset have no field 805 * splitting, file globbing, or (normal) tilde expansion done. 806 * at&t ksh seems to do something similar to this since 807 * $ touch a=a; typeset a=[ab]; echo "$a" 808 * a=[ab] 809 * $ x=typeset; $x a=[ab]; echo "$a" 810 * a=a 811 * $ 812 */ 813 static int 814 assign_command(s) 815 char *s; 816 { 817 char c = *s; 818 819 if (Flag(FPOSIX) || !*s) 820 return 0; 821 return (c == 'a' && strcmp(s, "alias") == 0) 822 || (c == 'e' && strcmp(s, "export") == 0) 823 || (c == 'r' && strcmp(s, "readonly") == 0) 824 || (c == 't' && strcmp(s, "typeset") == 0); 825 } 826 827 /* Check if we are in the middle of reading an alias */ 828 static int 829 inalias(s) 830 struct source *s; 831 { 832 for (; s && s->type == SALIAS; s = s->next) 833 if (!(s->flags & SF_ALIASEND)) 834 return 1; 835 return 0; 836 } 837 838 839 #ifdef KSH 840 /* Order important - indexed by Test_meta values 841 * Note that ||, &&, ( and ) can't appear in as unquoted strings 842 * in normal shell input, so these can be interpreted unambiguously 843 * in the evaluation pass. 844 */ 845 static const char dbtest_or[] = { CHAR, '|', CHAR, '|', EOS }; 846 static const char dbtest_and[] = { CHAR, '&', CHAR, '&', EOS }; 847 static const char dbtest_not[] = { CHAR, '!', EOS }; 848 static const char dbtest_oparen[] = { CHAR, '(', EOS }; 849 static const char dbtest_cparen[] = { CHAR, ')', EOS }; 850 const char *const dbtest_tokens[] = { 851 dbtest_or, dbtest_and, dbtest_not, 852 dbtest_oparen, dbtest_cparen 853 }; 854 const char db_close[] = { CHAR, ']', CHAR, ']', EOS }; 855 const char db_lthan[] = { CHAR, '<', EOS }; 856 const char db_gthan[] = { CHAR, '>', EOS }; 857 858 /* Test if the current token is a whatever. Accepts the current token if 859 * it is. Returns 0 if it is not, non-zero if it is (in the case of 860 * TM_UNOP and TM_BINOP, the returned value is a Test_op). 861 */ 862 static int 863 dbtestp_isa(te, meta) 864 Test_env *te; 865 Test_meta meta; 866 { 867 int c = tpeek(ARRAYVAR | (meta == TM_BINOP ? 0 : CONTIN)); 868 int uqword = 0; 869 char *save = (char *) 0; 870 int ret = 0; 871 872 /* unquoted word? */ 873 uqword = c == LWORD && *ident; 874 875 if (meta == TM_OR) 876 ret = c == LOGOR; 877 else if (meta == TM_AND) 878 ret = c == LOGAND; 879 else if (meta == TM_NOT) 880 ret = uqword && strcmp(yylval.cp, dbtest_tokens[(int) TM_NOT]) == 0; 881 else if (meta == TM_OPAREN) 882 ret = c == '(' /*)*/; 883 else if (meta == TM_CPAREN) 884 ret = c == /*(*/ ')'; 885 else if (meta == TM_UNOP || meta == TM_BINOP) { 886 if (meta == TM_BINOP && c == REDIR 887 && (yylval.iop->flag == IOREAD 888 || yylval.iop->flag == IOWRITE)) 889 { 890 ret = 1; 891 save = wdcopy(yylval.iop->flag == IOREAD ? 892 db_lthan : db_gthan, ATEMP); 893 } else if (uqword && (ret = (int) test_isop(te, meta, ident))) 894 save = yylval.cp; 895 } else /* meta == TM_END */ 896 ret = uqword && strcmp(yylval.cp, db_close) == 0; 897 if (ret) { 898 ACCEPT; 899 if (meta != TM_END) { 900 if (!save) 901 save = wdcopy(dbtest_tokens[(int) meta], ATEMP); 902 XPput(*te->pos.av, save); 903 } 904 } 905 return ret; 906 } 907 908 static const char * 909 dbtestp_getopnd(te, op, do_eval) 910 Test_env *te; 911 Test_op op; 912 int do_eval; 913 { 914 int c = tpeek(ARRAYVAR); 915 916 if (c != LWORD) 917 return (const char *) 0; 918 919 ACCEPT; 920 XPput(*te->pos.av, yylval.cp); 921 922 return null; 923 } 924 925 static int 926 dbtestp_eval(te, op, opnd1, opnd2, do_eval) 927 Test_env *te; 928 Test_op op; 929 const char *opnd1; 930 const char *opnd2; 931 int do_eval; 932 { 933 return 1; 934 } 935 936 static void 937 dbtestp_error(te, offset, msg) 938 Test_env *te; 939 int offset; 940 const char *msg; 941 { 942 te->flags |= TEF_ERROR; 943 944 if (offset < 0) { 945 REJECT; 946 /* Kludgy to say the least... */ 947 symbol = LWORD; 948 yylval.cp = *(XPptrv(*te->pos.av) + XPsize(*te->pos.av) 949 + offset); 950 } 951 syntaxerr(msg); 952 } 953 #endif /* KSH */ 954