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