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