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