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