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