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