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