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