1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)parser.c 5.10 (Berkeley) 04/20/93"; 13 #endif /* not lint */ 14 15 #include "shell.h" 16 #include "parser.h" 17 #include "nodes.h" 18 #include "expand.h" /* defines rmescapes() */ 19 #include "redir.h" /* defines copyfd() */ 20 #include "syntax.h" 21 #include "options.h" 22 #include "input.h" 23 #include "output.h" 24 #include "var.h" 25 #include "error.h" 26 #include "memalloc.h" 27 #include "mystring.h" 28 #include "alias.h" 29 #include "myhistedit.h" 30 31 32 /* 33 * Shell command parser. 34 */ 35 36 #define EOFMARKLEN 79 37 38 /* values returned by readtoken */ 39 #include "token.def" 40 41 42 43 struct heredoc { 44 struct heredoc *next; /* next here document in list */ 45 union node *here; /* redirection node */ 46 char *eofmark; /* string indicating end of input */ 47 int striptabs; /* if set, strip leading tabs */ 48 }; 49 50 51 52 struct heredoc *heredoclist; /* list of here documents to read */ 53 int parsebackquote; /* nonzero if we are inside backquotes */ 54 int doprompt; /* if set, prompt the user */ 55 int needprompt; /* true if interactive and at start of line */ 56 int lasttoken; /* last token read */ 57 MKINIT int tokpushback; /* last token pushed back */ 58 char *wordtext; /* text of last word returned by readtoken */ 59 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 60 struct nodelist *backquotelist; 61 union node *redirnode; 62 struct heredoc *heredoc; 63 int quoteflag; /* set if (part of) last token was quoted */ 64 int startlinno; /* line # where last token started */ 65 66 67 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 68 #ifdef GDB_HACK 69 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 70 static const char types[] = "}-+?="; 71 #endif 72 73 74 STATIC union node *list __P((int)); 75 STATIC union node *andor __P((void)); 76 STATIC union node *pipeline __P((void)); 77 STATIC union node *command __P((void)); 78 STATIC union node *simplecmd __P((void)); 79 STATIC void parsefname __P((void)); 80 STATIC void parseheredoc __P((void)); 81 STATIC int readtoken __P((void)); 82 STATIC int readtoken1 __P((int, char const *, char *, int)); 83 STATIC void attyline __P((void)); 84 STATIC int noexpand __P((char *)); 85 STATIC void synexpect __P((int)); 86 STATIC void synerror __P((char *)); 87 STATIC void setprompt __P((int)); 88 89 /* 90 * Read and parse a command. Returns NEOF on end of file. (NULL is a 91 * valid parse tree indicating a blank line.) 92 */ 93 94 union node * 95 parsecmd(interact) { 96 int t; 97 98 doprompt = interact; 99 if (doprompt) 100 setprompt(1); 101 else 102 setprompt(0); 103 needprompt = 0; 104 t = readtoken(); 105 if (t == TEOF) 106 return NEOF; 107 if (t == TNL) 108 return NULL; 109 tokpushback++; 110 return list(1); 111 } 112 113 114 STATIC union node * 115 list(nlflag) { 116 union node *n1, *n2, *n3; 117 118 checkkwd = 2; 119 if (nlflag == 0 && tokendlist[peektoken()]) 120 return NULL; 121 n1 = andor(); 122 for (;;) { 123 switch (readtoken()) { 124 case TBACKGND: 125 if (n1->type == NCMD || n1->type == NPIPE) { 126 n1->ncmd.backgnd = 1; 127 } else if (n1->type == NREDIR) { 128 n1->type = NBACKGND; 129 } else { 130 n3 = (union node *)stalloc(sizeof (struct nredir)); 131 n3->type = NBACKGND; 132 n3->nredir.n = n1; 133 n3->nredir.redirect = NULL; 134 n1 = n3; 135 } 136 goto tsemi; 137 case TNL: 138 tokpushback++; 139 /* fall through */ 140 tsemi: case TSEMI: 141 if (readtoken() == TNL) { 142 parseheredoc(); 143 if (nlflag) 144 return n1; 145 } else { 146 tokpushback++; 147 } 148 checkkwd = 2; 149 if (tokendlist[peektoken()]) 150 return n1; 151 n2 = andor(); 152 n3 = (union node *)stalloc(sizeof (struct nbinary)); 153 n3->type = NSEMI; 154 n3->nbinary.ch1 = n1; 155 n3->nbinary.ch2 = n2; 156 n1 = n3; 157 break; 158 case TEOF: 159 if (heredoclist) 160 parseheredoc(); 161 else 162 pungetc(); /* push back EOF on input */ 163 return n1; 164 default: 165 if (nlflag) 166 synexpect(-1); 167 tokpushback++; 168 return n1; 169 } 170 } 171 } 172 173 174 175 STATIC union node * 176 andor() { 177 union node *n1, *n2, *n3; 178 int t; 179 180 n1 = pipeline(); 181 for (;;) { 182 if ((t = readtoken()) == TAND) { 183 t = NAND; 184 } else if (t == TOR) { 185 t = NOR; 186 } else { 187 tokpushback++; 188 return n1; 189 } 190 n2 = pipeline(); 191 n3 = (union node *)stalloc(sizeof (struct nbinary)); 192 n3->type = t; 193 n3->nbinary.ch1 = n1; 194 n3->nbinary.ch2 = n2; 195 n1 = n3; 196 } 197 } 198 199 200 201 STATIC union node * 202 pipeline() { 203 union node *n1, *pipenode, *notnode; 204 struct nodelist *lp, *prev; 205 int negate = 0; 206 207 TRACE(("pipeline: entered\n")); 208 while (readtoken() == TNOT) { 209 TRACE(("pipeline: TNOT recognized\n")); 210 negate = !negate; 211 } 212 tokpushback++; 213 n1 = command(); 214 if (readtoken() == TPIPE) { 215 pipenode = (union node *)stalloc(sizeof (struct npipe)); 216 pipenode->type = NPIPE; 217 pipenode->npipe.backgnd = 0; 218 lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 219 pipenode->npipe.cmdlist = lp; 220 lp->n = n1; 221 do { 222 prev = lp; 223 lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 224 lp->n = command(); 225 prev->next = lp; 226 } while (readtoken() == TPIPE); 227 lp->next = NULL; 228 n1 = pipenode; 229 } 230 tokpushback++; 231 if (negate) { 232 notnode = (union node *)stalloc(sizeof (struct nnot)); 233 notnode->type = NNOT; 234 notnode->nnot.com = n1; 235 n1 = notnode; 236 } 237 return n1; 238 } 239 240 241 242 STATIC union node * 243 command() { 244 union node *n1, *n2; 245 union node *ap, **app; 246 union node *cp, **cpp; 247 union node *redir, **rpp; 248 int t; 249 250 checkkwd = 2; 251 switch (readtoken()) { 252 case TIF: 253 n1 = (union node *)stalloc(sizeof (struct nif)); 254 n1->type = NIF; 255 n1->nif.test = list(0); 256 if (readtoken() != TTHEN) 257 synexpect(TTHEN); 258 n1->nif.ifpart = list(0); 259 n2 = n1; 260 while (readtoken() == TELIF) { 261 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 262 n2 = n2->nif.elsepart; 263 n2->type = NIF; 264 n2->nif.test = list(0); 265 if (readtoken() != TTHEN) 266 synexpect(TTHEN); 267 n2->nif.ifpart = list(0); 268 } 269 if (lasttoken == TELSE) 270 n2->nif.elsepart = list(0); 271 else { 272 n2->nif.elsepart = NULL; 273 tokpushback++; 274 } 275 if (readtoken() != TFI) 276 synexpect(TFI); 277 checkkwd = 1; 278 break; 279 case TWHILE: 280 case TUNTIL: { 281 int got; 282 n1 = (union node *)stalloc(sizeof (struct nbinary)); 283 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 284 n1->nbinary.ch1 = list(0); 285 if ((got=readtoken()) != TDO) { 286 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 287 synexpect(TDO); 288 } 289 n1->nbinary.ch2 = list(0); 290 if (readtoken() != TDONE) 291 synexpect(TDONE); 292 checkkwd = 1; 293 break; 294 } 295 case TFOR: 296 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 297 synerror("Bad for loop variable"); 298 n1 = (union node *)stalloc(sizeof (struct nfor)); 299 n1->type = NFOR; 300 n1->nfor.var = wordtext; 301 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 302 app = ≈ 303 while (readtoken() == TWORD) { 304 n2 = (union node *)stalloc(sizeof (struct narg)); 305 n2->type = NARG; 306 n2->narg.text = wordtext; 307 n2->narg.backquote = backquotelist; 308 *app = n2; 309 app = &n2->narg.next; 310 } 311 *app = NULL; 312 n1->nfor.args = ap; 313 if (lasttoken != TNL && lasttoken != TSEMI) 314 synexpect(-1); 315 } else { 316 #ifndef GDB_HACK 317 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 318 '@', '=', '\0'}; 319 #endif 320 n2 = (union node *)stalloc(sizeof (struct narg)); 321 n2->type = NARG; 322 n2->narg.text = (char *)argvars; 323 n2->narg.backquote = NULL; 324 n2->narg.next = NULL; 325 n1->nfor.args = n2; 326 /* 327 * Newline or semicolon here is optional (but note 328 * that the original Bourne shell only allowed NL). 329 */ 330 if (lasttoken != TNL && lasttoken != TSEMI) 331 tokpushback++; 332 } 333 checkkwd = 2; 334 if ((t = readtoken()) == TDO) 335 t = TDONE; 336 else if (t == TBEGIN) 337 t = TEND; 338 else 339 synexpect(-1); 340 n1->nfor.body = list(0); 341 if (readtoken() != t) 342 synexpect(t); 343 checkkwd = 1; 344 break; 345 case TCASE: 346 n1 = (union node *)stalloc(sizeof (struct ncase)); 347 n1->type = NCASE; 348 if (readtoken() != TWORD) 349 synexpect(TWORD); 350 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 351 n2->type = NARG; 352 n2->narg.text = wordtext; 353 n2->narg.backquote = backquotelist; 354 n2->narg.next = NULL; 355 while (readtoken() == TNL); 356 if (lasttoken != TWORD || ! equal(wordtext, "in")) 357 synerror("expecting \"in\""); 358 cpp = &n1->ncase.cases; 359 while (checkkwd = 2, readtoken() == TWORD) { 360 *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 361 cp->type = NCLIST; 362 app = &cp->nclist.pattern; 363 for (;;) { 364 *app = ap = (union node *)stalloc(sizeof (struct narg)); 365 ap->type = NARG; 366 ap->narg.text = wordtext; 367 ap->narg.backquote = backquotelist; 368 if (readtoken() != TPIPE) 369 break; 370 app = &ap->narg.next; 371 if (readtoken() != TWORD) 372 synexpect(TWORD); 373 } 374 ap->narg.next = NULL; 375 if (lasttoken != TRP) 376 synexpect(TRP); 377 cp->nclist.body = list(0); 378 if ((t = readtoken()) == TESAC) 379 tokpushback++; 380 else if (t != TENDCASE) 381 synexpect(TENDCASE); 382 cpp = &cp->nclist.next; 383 } 384 *cpp = NULL; 385 if (lasttoken != TESAC) 386 synexpect(TESAC); 387 checkkwd = 1; 388 break; 389 case TLP: 390 n1 = (union node *)stalloc(sizeof (struct nredir)); 391 n1->type = NSUBSHELL; 392 n1->nredir.n = list(0); 393 n1->nredir.redirect = NULL; 394 if (readtoken() != TRP) 395 synexpect(TRP); 396 checkkwd = 1; 397 break; 398 case TBEGIN: 399 n1 = list(0); 400 if (readtoken() != TEND) 401 synexpect(TEND); 402 checkkwd = 1; 403 break; 404 case TWORD: 405 case TREDIR: 406 tokpushback++; 407 return simplecmd(); 408 default: 409 synexpect(-1); 410 } 411 412 /* Now check for redirection which may follow command */ 413 rpp = &redir; 414 while (readtoken() == TREDIR) { 415 *rpp = n2 = redirnode; 416 rpp = &n2->nfile.next; 417 parsefname(); 418 } 419 tokpushback++; 420 *rpp = NULL; 421 if (redir) { 422 if (n1->type != NSUBSHELL) { 423 n2 = (union node *)stalloc(sizeof (struct nredir)); 424 n2->type = NREDIR; 425 n2->nredir.n = n1; 426 n1 = n2; 427 } 428 n1->nredir.redirect = redir; 429 } 430 return n1; 431 } 432 433 434 STATIC union node * 435 simplecmd() { 436 union node *args, **app; 437 union node *redir, **rpp; 438 union node *n; 439 440 args = NULL; 441 app = &args; 442 rpp = &redir; 443 for (;;) { 444 if (readtoken() == TWORD) { 445 n = (union node *)stalloc(sizeof (struct narg)); 446 n->type = NARG; 447 n->narg.text = wordtext; 448 n->narg.backquote = backquotelist; 449 *app = n; 450 app = &n->narg.next; 451 } else if (lasttoken == TREDIR) { 452 *rpp = n = redirnode; 453 rpp = &n->nfile.next; 454 parsefname(); /* read name of redirection file */ 455 } else if (lasttoken == TLP && app == &args->narg.next 456 && rpp == &redir) { 457 /* We have a function */ 458 if (readtoken() != TRP) 459 synexpect(TRP); 460 #ifdef notdef 461 if (! goodname(n->narg.text)) 462 synerror("Bad function name"); 463 #endif 464 n->type = NDEFUN; 465 n->narg.next = command(); 466 return n; 467 } else { 468 tokpushback++; 469 break; 470 } 471 } 472 *app = NULL; 473 *rpp = NULL; 474 n = (union node *)stalloc(sizeof (struct ncmd)); 475 n->type = NCMD; 476 n->ncmd.backgnd = 0; 477 n->ncmd.args = args; 478 n->ncmd.redirect = redir; 479 return n; 480 } 481 482 483 STATIC void 484 parsefname() { 485 union node *n = redirnode; 486 487 if (readtoken() != TWORD) 488 synexpect(-1); 489 if (n->type == NHERE) { 490 struct heredoc *here = heredoc; 491 struct heredoc *p; 492 int i; 493 494 if (quoteflag == 0) 495 n->type = NXHERE; 496 TRACE(("Here document %d\n", n->type)); 497 if (here->striptabs) { 498 while (*wordtext == '\t') 499 wordtext++; 500 } 501 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 502 synerror("Illegal eof marker for << redirection"); 503 rmescapes(wordtext); 504 here->eofmark = wordtext; 505 here->next = NULL; 506 if (heredoclist == NULL) 507 heredoclist = here; 508 else { 509 for (p = heredoclist ; p->next ; p = p->next); 510 p->next = here; 511 } 512 } else if (n->type == NTOFD || n->type == NFROMFD) { 513 if (is_digit(wordtext[0])) 514 n->ndup.dupfd = digit_val(wordtext[0]); 515 else if (wordtext[0] == '-') 516 n->ndup.dupfd = -1; 517 else 518 goto bad; 519 if (wordtext[1] != '\0') { 520 bad: 521 synerror("Bad fd number"); 522 } 523 } else { 524 n->nfile.fname = (union node *)stalloc(sizeof (struct narg)); 525 n = n->nfile.fname; 526 n->type = NARG; 527 n->narg.next = NULL; 528 n->narg.text = wordtext; 529 n->narg.backquote = backquotelist; 530 } 531 } 532 533 534 /* 535 * Input any here documents. 536 */ 537 538 STATIC void 539 parseheredoc() { 540 struct heredoc *here; 541 union node *n; 542 543 while (heredoclist) { 544 here = heredoclist; 545 heredoclist = here->next; 546 if (needprompt) { 547 setprompt(2); 548 needprompt = 0; 549 } 550 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 551 here->eofmark, here->striptabs); 552 n = (union node *)stalloc(sizeof (struct narg)); 553 n->narg.type = NARG; 554 n->narg.next = NULL; 555 n->narg.text = wordtext; 556 n->narg.backquote = backquotelist; 557 here->here->nhere.doc = n; 558 } 559 } 560 561 STATIC int 562 peektoken() { 563 int t; 564 565 t = readtoken(); 566 tokpushback++; 567 return (t); 568 } 569 570 STATIC int xxreadtoken(); 571 572 STATIC int 573 readtoken() { 574 int t; 575 int savecheckkwd = checkkwd; 576 struct alias *ap; 577 #ifdef DEBUG 578 int alreadyseen = tokpushback; 579 #endif 580 581 top: 582 t = xxreadtoken(); 583 584 if (checkkwd) { 585 /* 586 * eat newlines 587 */ 588 if (checkkwd == 2) { 589 checkkwd = 0; 590 while (t == TNL) { 591 parseheredoc(); 592 t = xxreadtoken(); 593 } 594 } else 595 checkkwd = 0; 596 /* 597 * check for keywords and aliases 598 */ 599 if (t == TWORD && !quoteflag) { 600 register char * const *pp, *s; 601 602 for (pp = parsekwd; *pp; pp++) { 603 if (**pp == *wordtext && equal(*pp, wordtext)) { 604 lasttoken = t = pp - parsekwd + KWDOFFSET; 605 TRACE(("keyword %s recognized\n", tokname[t])); 606 goto out; 607 } 608 } 609 if (ap = lookupalias(wordtext, 1)) { 610 pushstring(ap->val, strlen(ap->val), ap); 611 checkkwd = savecheckkwd; 612 goto top; 613 } 614 } 615 out: 616 checkkwd = 0; 617 } 618 #ifdef DEBUG 619 if (!alreadyseen) 620 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 621 else 622 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 623 #endif 624 return (t); 625 } 626 627 628 /* 629 * Read the next input token. 630 * If the token is a word, we set backquotelist to the list of cmds in 631 * backquotes. We set quoteflag to true if any part of the word was 632 * quoted. 633 * If the token is TREDIR, then we set redirnode to a structure containing 634 * the redirection. 635 * In all cases, the variable startlinno is set to the number of the line 636 * on which the token starts. 637 * 638 * [Change comment: here documents and internal procedures] 639 * [Readtoken shouldn't have any arguments. Perhaps we should make the 640 * word parsing code into a separate routine. In this case, readtoken 641 * doesn't need to have any internal procedures, but parseword does. 642 * We could also make parseoperator in essence the main routine, and 643 * have parseword (readtoken1?) handle both words and redirection.] 644 */ 645 646 #define RETURN(token) return lasttoken = token 647 648 STATIC int 649 xxreadtoken() { 650 register c; 651 652 if (tokpushback) { 653 tokpushback = 0; 654 return lasttoken; 655 } 656 if (needprompt) { 657 setprompt(2); 658 needprompt = 0; 659 } 660 startlinno = plinno; 661 for (;;) { /* until token or start of word found */ 662 c = pgetc_macro(); 663 if (c == ' ' || c == '\t') 664 continue; /* quick check for white space first */ 665 switch (c) { 666 case ' ': case '\t': 667 continue; 668 case '#': 669 while ((c = pgetc()) != '\n' && c != PEOF); 670 pungetc(); 671 continue; 672 case '\\': 673 if (pgetc() == '\n') { 674 startlinno = ++plinno; 675 if (doprompt) 676 setprompt(2); 677 else 678 setprompt(0); 679 continue; 680 } 681 pungetc(); 682 goto breakloop; 683 case '\n': 684 plinno++; 685 needprompt = doprompt; 686 RETURN(TNL); 687 case PEOF: 688 RETURN(TEOF); 689 case '&': 690 if (pgetc() == '&') 691 RETURN(TAND); 692 pungetc(); 693 RETURN(TBACKGND); 694 case '|': 695 if (pgetc() == '|') 696 RETURN(TOR); 697 pungetc(); 698 RETURN(TPIPE); 699 case ';': 700 if (pgetc() == ';') 701 RETURN(TENDCASE); 702 pungetc(); 703 RETURN(TSEMI); 704 case '(': 705 RETURN(TLP); 706 case ')': 707 RETURN(TRP); 708 default: 709 goto breakloop; 710 } 711 } 712 breakloop: 713 return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 714 #undef RETURN 715 } 716 717 718 719 /* 720 * If eofmark is NULL, read a word or a redirection symbol. If eofmark 721 * is not NULL, read a here document. In the latter case, eofmark is the 722 * word which marks the end of the document and striptabs is true if 723 * leading tabs should be stripped from the document. The argument firstc 724 * is the first character of the input token or document. 725 * 726 * Because C does not have internal subroutines, I have simulated them 727 * using goto's to implement the subroutine linkage. The following macros 728 * will run code that appears at the end of readtoken1. 729 */ 730 731 #define CHECKEND() {goto checkend; checkend_return:;} 732 #define PARSEREDIR() {goto parseredir; parseredir_return:;} 733 #define PARSESUB() {goto parsesub; parsesub_return:;} 734 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 735 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 736 #define PARSEARITH() {goto parsearith; parsearith_return:;} 737 738 STATIC int 739 readtoken1(firstc, syntax, eofmark, striptabs) 740 int firstc; 741 char const *syntax; 742 char *eofmark; 743 int striptabs; 744 { 745 register c = firstc; 746 register char *out; 747 int len; 748 char line[EOFMARKLEN + 1]; 749 struct nodelist *bqlist; 750 int quotef; 751 int dblquote; 752 int varnest; /* levels of variables expansion */ 753 int arinest; /* levels of arithmetic expansion */ 754 int parenlevel; /* levels of parens in arithmetic */ 755 int oldstyle; 756 char const *prevsyntax; /* syntax before arithmetic */ 757 758 startlinno = plinno; 759 dblquote = 0; 760 if (syntax == DQSYNTAX) 761 dblquote = 1; 762 quotef = 0; 763 bqlist = NULL; 764 varnest = 0; 765 arinest = 0; 766 parenlevel = 0; 767 768 STARTSTACKSTR(out); 769 loop: { /* for each line, until end of word */ 770 #if ATTY 771 if (c == '\034' && doprompt 772 && attyset() && ! equal(termval(), "emacs")) { 773 attyline(); 774 if (syntax == BASESYNTAX) 775 return readtoken(); 776 c = pgetc(); 777 goto loop; 778 } 779 #endif 780 CHECKEND(); /* set c to PEOF if at end of here document */ 781 for (;;) { /* until end of line or end of word */ 782 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 783 if (parsebackquote && c == '\\') { 784 c = pgetc(); /* XXX - compat with old /bin/sh */ 785 if (c != '\\' && c != '`' && c != '$') { 786 pungetc(); 787 c = '\\'; 788 } 789 } 790 switch(syntax[c]) { 791 case CNL: /* '\n' */ 792 if (syntax == BASESYNTAX) 793 goto endword; /* exit outer loop */ 794 USTPUTC(c, out); 795 plinno++; 796 if (doprompt) 797 setprompt(2); 798 else 799 setprompt(0); 800 c = pgetc(); 801 goto loop; /* continue outer loop */ 802 case CWORD: 803 USTPUTC(c, out); 804 break; 805 case CCTL: 806 if (eofmark == NULL || dblquote) 807 USTPUTC(CTLESC, out); 808 USTPUTC(c, out); 809 break; 810 case CBACK: /* backslash */ 811 c = pgetc(); 812 if (c == PEOF) { 813 USTPUTC('\\', out); 814 pungetc(); 815 } else if (c == '\n') { 816 if (doprompt) 817 setprompt(2); 818 else 819 setprompt(0); 820 } else { 821 if (dblquote && c != '\\' && c != '`' && c != '$' 822 && (c != '"' || eofmark != NULL)) 823 USTPUTC('\\', out); 824 if (SQSYNTAX[c] == CCTL) 825 USTPUTC(CTLESC, out); 826 USTPUTC(c, out); 827 quotef++; 828 } 829 break; 830 case CSQUOTE: 831 syntax = SQSYNTAX; 832 break; 833 case CDQUOTE: 834 syntax = DQSYNTAX; 835 dblquote = 1; 836 break; 837 case CENDQUOTE: 838 if (eofmark) { 839 USTPUTC(c, out); 840 } else { 841 if (arinest) 842 syntax = ARISYNTAX; 843 else 844 syntax = BASESYNTAX; 845 quotef++; 846 dblquote = 0; 847 } 848 break; 849 case CVAR: /* '$' */ 850 PARSESUB(); /* parse substitution */ 851 break; 852 case CENDVAR: /* '}' */ 853 if (varnest > 0) { 854 varnest--; 855 USTPUTC(CTLENDVAR, out); 856 } else { 857 USTPUTC(c, out); 858 } 859 break; 860 case CLP: /* '(' in arithmetic */ 861 parenlevel++; 862 USTPUTC(c, out); 863 break; 864 case CRP: /* ')' in arithmetic */ 865 if (parenlevel > 0) { 866 USTPUTC(c, out); 867 --parenlevel; 868 } else { 869 if (pgetc() == ')') { 870 if (--arinest == 0) { 871 USTPUTC(CTLENDARI, out); 872 syntax = prevsyntax; 873 } else 874 USTPUTC(')', out); 875 } else { 876 /* 877 * unbalanced parens 878 * (don't 2nd guess - no error) 879 */ 880 pungetc(); 881 USTPUTC(')', out); 882 } 883 } 884 break; 885 case CBQUOTE: /* '`' */ 886 if (parsebackquote && syntax == BASESYNTAX) { 887 if (out == stackblock()) 888 return lasttoken = TENDBQUOTE; 889 else 890 goto endword; /* exit outer loop */ 891 } 892 PARSEBACKQOLD(); 893 break; 894 case CEOF: 895 goto endword; /* exit outer loop */ 896 default: 897 if (varnest == 0) 898 goto endword; /* exit outer loop */ 899 USTPUTC(c, out); 900 } 901 c = pgetc_macro(); 902 } 903 } 904 endword: 905 if (syntax == ARISYNTAX) 906 synerror("Missing '))'"); 907 if (syntax != BASESYNTAX && eofmark == NULL) 908 synerror("Unterminated quoted string"); 909 if (varnest != 0) { 910 startlinno = plinno; 911 synerror("Missing '}'"); 912 } 913 USTPUTC('\0', out); 914 len = out - stackblock(); 915 out = stackblock(); 916 if (eofmark == NULL) { 917 if ((c == '>' || c == '<') 918 && quotef == 0 919 && len <= 2 920 && (*out == '\0' || is_digit(*out))) { 921 PARSEREDIR(); 922 return lasttoken = TREDIR; 923 } else { 924 pungetc(); 925 } 926 } 927 quoteflag = quotef; 928 backquotelist = bqlist; 929 grabstackblock(len); 930 wordtext = out; 931 return lasttoken = TWORD; 932 /* end of readtoken routine */ 933 934 935 936 /* 937 * Check to see whether we are at the end of the here document. When this 938 * is called, c is set to the first character of the next input line. If 939 * we are at the end of the here document, this routine sets the c to PEOF. 940 */ 941 942 checkend: { 943 if (eofmark) { 944 if (striptabs) { 945 while (c == '\t') 946 c = pgetc(); 947 } 948 if (c == *eofmark) { 949 if (pfgets(line, sizeof line) != NULL) { 950 register char *p, *q; 951 952 p = line; 953 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 954 if (*p == '\n' && *q == '\0') { 955 c = PEOF; 956 plinno++; 957 needprompt = doprompt; 958 } else { 959 pushstring(line, strlen(line), NULL); 960 } 961 } 962 } 963 } 964 goto checkend_return; 965 } 966 967 968 /* 969 * Parse a redirection operator. The variable "out" points to a string 970 * specifying the fd to be redirected. The variable "c" contains the 971 * first character of the redirection operator. 972 */ 973 974 parseredir: { 975 char fd = *out; 976 union node *np; 977 978 np = (union node *)stalloc(sizeof (struct nfile)); 979 if (c == '>') { 980 np->nfile.fd = 1; 981 c = pgetc(); 982 if (c == '>') 983 np->type = NAPPEND; 984 else if (c == '&') 985 np->type = NTOFD; 986 else { 987 np->type = NTO; 988 pungetc(); 989 } 990 } else { /* c == '<' */ 991 np->nfile.fd = 0; 992 c = pgetc(); 993 if (c == '<') { 994 if (sizeof (struct nfile) != sizeof (struct nhere)) { 995 np = (union node *)stalloc(sizeof (struct nhere)); 996 np->nfile.fd = 0; 997 } 998 np->type = NHERE; 999 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 1000 heredoc->here = np; 1001 if ((c = pgetc()) == '-') { 1002 heredoc->striptabs = 1; 1003 } else { 1004 heredoc->striptabs = 0; 1005 pungetc(); 1006 } 1007 } else if (c == '&') 1008 np->type = NFROMFD; 1009 else { 1010 np->type = NFROM; 1011 pungetc(); 1012 } 1013 } 1014 if (fd != '\0') 1015 np->nfile.fd = digit_val(fd); 1016 redirnode = np; 1017 goto parseredir_return; 1018 } 1019 1020 1021 /* 1022 * Parse a substitution. At this point, we have read the dollar sign 1023 * and nothing else. 1024 */ 1025 1026 parsesub: { 1027 int subtype; 1028 int typeloc; 1029 int flags; 1030 char *p; 1031 #ifndef GDB_HACK 1032 static const char types[] = "}-+?="; 1033 #endif 1034 1035 c = pgetc(); 1036 if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 1037 USTPUTC('$', out); 1038 pungetc(); 1039 } else if (c == '(') { /* $(command) or $((arith)) */ 1040 if (pgetc() == '(') { 1041 PARSEARITH(); 1042 } else { 1043 pungetc(); 1044 PARSEBACKQNEW(); 1045 } 1046 } else { 1047 USTPUTC(CTLVAR, out); 1048 typeloc = out - stackblock(); 1049 USTPUTC(VSNORMAL, out); 1050 subtype = VSNORMAL; 1051 if (c == '{') { 1052 c = pgetc(); 1053 subtype = 0; 1054 } 1055 if (is_name(c)) { 1056 do { 1057 STPUTC(c, out); 1058 c = pgetc(); 1059 } while (is_in_name(c)); 1060 } else { 1061 if (! is_special(c)) 1062 badsub: synerror("Bad substitution"); 1063 USTPUTC(c, out); 1064 c = pgetc(); 1065 } 1066 STPUTC('=', out); 1067 flags = 0; 1068 if (subtype == 0) { 1069 if (c == ':') { 1070 flags = VSNUL; 1071 c = pgetc(); 1072 } 1073 p = strchr(types, c); 1074 if (p == NULL) 1075 goto badsub; 1076 subtype = p - types + VSNORMAL; 1077 } else { 1078 pungetc(); 1079 } 1080 if (dblquote || arinest) 1081 flags |= VSQUOTE; 1082 *(stackblock() + typeloc) = subtype | flags; 1083 if (subtype != VSNORMAL) 1084 varnest++; 1085 } 1086 goto parsesub_return; 1087 } 1088 1089 1090 /* 1091 * Called to parse command substitutions. Newstyle is set if the command 1092 * is enclosed inside $(...); nlpp is a pointer to the head of the linked 1093 * list of commands (passed by reference), and savelen is the number of 1094 * characters on the top of the stack which must be preserved. 1095 */ 1096 1097 parsebackq: { 1098 struct nodelist **nlpp; 1099 int savepbq; 1100 union node *n; 1101 char *volatile str; 1102 struct jmploc jmploc; 1103 struct jmploc *volatile savehandler; 1104 int savelen; 1105 int t; 1106 1107 savepbq = parsebackquote; 1108 if (setjmp(jmploc.loc)) { 1109 if (str) 1110 ckfree(str); 1111 parsebackquote = 0; 1112 handler = savehandler; 1113 longjmp(handler->loc, 1); 1114 } 1115 INTOFF; 1116 str = NULL; 1117 savelen = out - stackblock(); 1118 if (savelen > 0) { 1119 str = ckmalloc(savelen); 1120 bcopy(stackblock(), str, savelen); 1121 } 1122 savehandler = handler; 1123 handler = &jmploc; 1124 INTON; 1125 nlpp = &bqlist; 1126 while (*nlpp) 1127 nlpp = &(*nlpp)->next; 1128 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1129 (*nlpp)->next = NULL; 1130 parsebackquote = oldstyle; 1131 n = list(0); 1132 t = oldstyle? TENDBQUOTE : TRP; 1133 if (readtoken() != t) 1134 synexpect(t); 1135 (*nlpp)->n = n; 1136 while (stackblocksize() <= savelen) 1137 growstackblock(); 1138 STARTSTACKSTR(out); 1139 if (str) { 1140 bcopy(str, out, savelen); 1141 STADJUST(savelen, out); 1142 INTOFF; 1143 ckfree(str); 1144 str = NULL; 1145 INTON; 1146 } 1147 parsebackquote = savepbq; 1148 handler = savehandler; 1149 if (arinest || dblquote) 1150 USTPUTC(CTLBACKQ | CTLQUOTE, out); 1151 else 1152 USTPUTC(CTLBACKQ, out); 1153 if (oldstyle) 1154 goto parsebackq_oldreturn; 1155 else 1156 goto parsebackq_newreturn; 1157 } 1158 1159 /* 1160 * Parse an arithmetic expansion (indicate start of one and set state) 1161 */ 1162 parsearith: { 1163 1164 if (++arinest == 1) { 1165 prevsyntax = syntax; 1166 syntax = ARISYNTAX; 1167 USTPUTC(CTLARI, out); 1168 } else { 1169 /* 1170 * we collapse embedded arithmetic expansion to 1171 * parenthesis, which should be equivalent 1172 */ 1173 USTPUTC('(', out); 1174 } 1175 goto parsearith_return; 1176 } 1177 1178 } /* end of readtoken */ 1179 1180 1181 1182 #ifdef mkinit 1183 RESET { 1184 tokpushback = 0; 1185 checkkwd = 0; 1186 } 1187 #endif 1188 1189 /* 1190 * Returns true if the text contains nothing to expand (no dollar signs 1191 * or backquotes). 1192 */ 1193 1194 STATIC int 1195 noexpand(text) 1196 char *text; 1197 { 1198 register char *p; 1199 register char c; 1200 1201 p = text; 1202 while ((c = *p++) != '\0') { 1203 if (c == CTLESC) 1204 p++; 1205 else if (BASESYNTAX[c] == CCTL) 1206 return 0; 1207 } 1208 return 1; 1209 } 1210 1211 1212 /* 1213 * Return true if the argument is a legal variable name (a letter or 1214 * underscore followed by zero or more letters, underscores, and digits). 1215 */ 1216 1217 int 1218 goodname(name) 1219 char *name; 1220 { 1221 register char *p; 1222 1223 p = name; 1224 if (! is_name(*p)) 1225 return 0; 1226 while (*++p) { 1227 if (! is_in_name(*p)) 1228 return 0; 1229 } 1230 return 1; 1231 } 1232 1233 1234 /* 1235 * Called when an unexpected token is read during the parse. The argument 1236 * is the token that is expected, or -1 if more than one type of token can 1237 * occur at this point. 1238 */ 1239 1240 STATIC void 1241 synexpect(token) { 1242 char msg[64]; 1243 1244 if (token >= 0) { 1245 fmtstr(msg, 64, "%s unexpected (expecting %s)", 1246 tokname[lasttoken], tokname[token]); 1247 } else { 1248 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 1249 } 1250 synerror(msg); 1251 } 1252 1253 1254 STATIC void 1255 synerror(msg) 1256 char *msg; 1257 { 1258 if (commandname) 1259 outfmt(&errout, "%s: %d: ", commandname, startlinno); 1260 outfmt(&errout, "Syntax error: %s\n", msg); 1261 error((char *)NULL); 1262 } 1263 1264 STATIC void 1265 setprompt(which) 1266 int which; 1267 { 1268 whichprompt = which; 1269 1270 if (!el) 1271 out2str(getprompt(NULL)); 1272 } 1273 1274 /* 1275 * called by editline -- any expansions to the prompt 1276 * should be added here. 1277 */ 1278 char * 1279 getprompt(unused) 1280 void *unused; 1281 { 1282 switch (whichprompt) { 1283 case 0: 1284 return ""; 1285 case 1: 1286 return ps1val(); 1287 case 2: 1288 return ps2val(); 1289 default: 1290 return "<internal prompt error>"; 1291 } 1292 } 1293