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.8 1995/08/27 20:26:42 joerg 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 TAND: 446 case TOR: 447 case TNL: 448 case TSEMI: 449 /* Handle EOF as an empty command, too */ 450 case TEOF: 451 case TWORD: 452 tokpushback++; 453 return simplecmd(rpp, redir); 454 default: 455 synexpect(-1); 456 } 457 458 /* Now check for redirection which may follow command */ 459 while (readtoken() == TREDIR) { 460 *rpp = n2 = redirnode; 461 rpp = &n2->nfile.next; 462 parsefname(); 463 } 464 tokpushback++; 465 *rpp = NULL; 466 if (redir) { 467 if (n1->type != NSUBSHELL) { 468 n2 = (union node *)stalloc(sizeof (struct nredir)); 469 n2->type = NREDIR; 470 n2->nredir.n = n1; 471 n1 = n2; 472 } 473 n1->nredir.redirect = redir; 474 } 475 return n1; 476 } 477 478 479 STATIC union node * 480 simplecmd(rpp, redir) 481 union node **rpp, *redir; 482 { 483 union node *args, **app; 484 union node **orig_rpp = rpp; 485 union node *n; 486 487 /* If we don't have any redirections already, then we must reset */ 488 /* rpp to be the address of the local redir variable. */ 489 if (redir == 0) 490 rpp = &redir; 491 492 args = NULL; 493 app = &args; 494 /* 495 * We save the incoming value, because we need this for shell 496 * functions. There can not be a redirect or an argument between 497 * the function name and the open parenthesis. 498 */ 499 orig_rpp = rpp; 500 501 for (;;) { 502 if (readtoken() == TWORD) { 503 n = (union node *)stalloc(sizeof (struct narg)); 504 n->type = NARG; 505 n->narg.text = wordtext; 506 n->narg.backquote = backquotelist; 507 *app = n; 508 app = &n->narg.next; 509 } else if (lasttoken == TREDIR) { 510 *rpp = n = redirnode; 511 rpp = &n->nfile.next; 512 parsefname(); /* read name of redirection file */ 513 } else if (lasttoken == TLP && app == &args->narg.next 514 && rpp == orig_rpp) { 515 /* We have a function */ 516 if (readtoken() != TRP) 517 synexpect(TRP); 518 #ifdef notdef 519 if (! goodname(n->narg.text)) 520 synerror("Bad function name"); 521 #endif 522 n->type = NDEFUN; 523 n->narg.next = command(); 524 return n; 525 } else { 526 tokpushback++; 527 break; 528 } 529 } 530 *app = NULL; 531 *rpp = NULL; 532 n = (union node *)stalloc(sizeof (struct ncmd)); 533 n->type = NCMD; 534 n->ncmd.backgnd = 0; 535 n->ncmd.args = args; 536 n->ncmd.redirect = redir; 537 return n; 538 } 539 540 541 STATIC void 542 parsefname() { 543 union node *n = redirnode; 544 545 if (readtoken() != TWORD) 546 synexpect(-1); 547 if (n->type == NHERE) { 548 struct heredoc *here = heredoc; 549 struct heredoc *p; 550 int i; 551 552 if (quoteflag == 0) 553 n->type = NXHERE; 554 TRACE(("Here document %d\n", n->type)); 555 if (here->striptabs) { 556 while (*wordtext == '\t') 557 wordtext++; 558 } 559 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 560 synerror("Illegal eof marker for << redirection"); 561 rmescapes(wordtext); 562 here->eofmark = wordtext; 563 here->next = NULL; 564 if (heredoclist == NULL) 565 heredoclist = here; 566 else { 567 for (p = heredoclist ; p->next ; p = p->next); 568 p->next = here; 569 } 570 } else if (n->type == NTOFD || n->type == NFROMFD) { 571 if (is_digit(wordtext[0])) 572 n->ndup.dupfd = digit_val(wordtext[0]); 573 else if (wordtext[0] == '-') 574 n->ndup.dupfd = -1; 575 else 576 goto bad; 577 if (wordtext[1] != '\0') { 578 bad: 579 synerror("Bad fd number"); 580 } 581 } else { 582 n->nfile.fname = (union node *)stalloc(sizeof (struct narg)); 583 n = n->nfile.fname; 584 n->type = NARG; 585 n->narg.next = NULL; 586 n->narg.text = wordtext; 587 n->narg.backquote = backquotelist; 588 } 589 } 590 591 592 /* 593 * Input any here documents. 594 */ 595 596 STATIC void 597 parseheredoc() { 598 struct heredoc *here; 599 union node *n; 600 601 while (heredoclist) { 602 here = heredoclist; 603 heredoclist = here->next; 604 if (needprompt) { 605 setprompt(2); 606 needprompt = 0; 607 } 608 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 609 here->eofmark, here->striptabs); 610 n = (union node *)stalloc(sizeof (struct narg)); 611 n->narg.type = NARG; 612 n->narg.next = NULL; 613 n->narg.text = wordtext; 614 n->narg.backquote = backquotelist; 615 here->here->nhere.doc = n; 616 } 617 } 618 619 STATIC int 620 peektoken() { 621 int t; 622 623 t = readtoken(); 624 tokpushback++; 625 return (t); 626 } 627 628 STATIC int xxreadtoken(); 629 630 STATIC int 631 readtoken() { 632 int t; 633 int savecheckkwd = checkkwd; 634 struct alias *ap; 635 #ifdef DEBUG 636 int alreadyseen = tokpushback; 637 #endif 638 639 top: 640 t = xxreadtoken(); 641 642 if (checkkwd) { 643 /* 644 * eat newlines 645 */ 646 if (checkkwd == 2) { 647 checkkwd = 0; 648 while (t == TNL) { 649 parseheredoc(); 650 t = xxreadtoken(); 651 } 652 } else 653 checkkwd = 0; 654 /* 655 * check for keywords and aliases 656 */ 657 if (t == TWORD && !quoteflag) { 658 register char * const *pp, *s; 659 660 for (pp = (char **)parsekwd; *pp; pp++) { 661 if (**pp == *wordtext && equal(*pp, wordtext)) { 662 lasttoken = t = pp - parsekwd + KWDOFFSET; 663 TRACE(("keyword %s recognized\n", tokname[t])); 664 goto out; 665 } 666 } 667 if (ap = lookupalias(wordtext, 1)) { 668 pushstring(ap->val, strlen(ap->val), ap); 669 checkkwd = savecheckkwd; 670 goto top; 671 } 672 } 673 out: 674 checkkwd = 0; 675 } 676 #ifdef DEBUG 677 if (!alreadyseen) 678 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 679 else 680 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 681 #endif 682 return (t); 683 } 684 685 686 /* 687 * Read the next input token. 688 * If the token is a word, we set backquotelist to the list of cmds in 689 * backquotes. We set quoteflag to true if any part of the word was 690 * quoted. 691 * If the token is TREDIR, then we set redirnode to a structure containing 692 * the redirection. 693 * In all cases, the variable startlinno is set to the number of the line 694 * on which the token starts. 695 * 696 * [Change comment: here documents and internal procedures] 697 * [Readtoken shouldn't have any arguments. Perhaps we should make the 698 * word parsing code into a separate routine. In this case, readtoken 699 * doesn't need to have any internal procedures, but parseword does. 700 * We could also make parseoperator in essence the main routine, and 701 * have parseword (readtoken1?) handle both words and redirection.] 702 */ 703 704 #define RETURN(token) return lasttoken = token 705 706 STATIC int 707 xxreadtoken() { 708 register c; 709 710 if (tokpushback) { 711 tokpushback = 0; 712 return lasttoken; 713 } 714 if (needprompt) { 715 setprompt(2); 716 needprompt = 0; 717 } 718 startlinno = plinno; 719 for (;;) { /* until token or start of word found */ 720 c = pgetc_macro(); 721 if (c == ' ' || c == '\t') 722 continue; /* quick check for white space first */ 723 switch (c) { 724 case ' ': case '\t': 725 continue; 726 case '#': 727 while ((c = pgetc()) != '\n' && c != PEOF); 728 pungetc(); 729 continue; 730 case '\\': 731 if (pgetc() == '\n') { 732 startlinno = ++plinno; 733 if (doprompt) 734 setprompt(2); 735 else 736 setprompt(0); 737 continue; 738 } 739 pungetc(); 740 goto breakloop; 741 case '\n': 742 plinno++; 743 needprompt = doprompt; 744 RETURN(TNL); 745 case PEOF: 746 RETURN(TEOF); 747 case '&': 748 if (pgetc() == '&') 749 RETURN(TAND); 750 pungetc(); 751 RETURN(TBACKGND); 752 case '|': 753 if (pgetc() == '|') 754 RETURN(TOR); 755 pungetc(); 756 RETURN(TPIPE); 757 case ';': 758 if (pgetc() == ';') 759 RETURN(TENDCASE); 760 pungetc(); 761 RETURN(TSEMI); 762 case '(': 763 RETURN(TLP); 764 case ')': 765 RETURN(TRP); 766 default: 767 goto breakloop; 768 } 769 } 770 breakloop: 771 return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 772 #undef RETURN 773 } 774 775 776 777 /* 778 * If eofmark is NULL, read a word or a redirection symbol. If eofmark 779 * is not NULL, read a here document. In the latter case, eofmark is the 780 * word which marks the end of the document and striptabs is true if 781 * leading tabs should be stripped from the document. The argument firstc 782 * is the first character of the input token or document. 783 * 784 * Because C does not have internal subroutines, I have simulated them 785 * using goto's to implement the subroutine linkage. The following macros 786 * will run code that appears at the end of readtoken1. 787 */ 788 789 #define CHECKEND() {goto checkend; checkend_return:;} 790 #define PARSEREDIR() {goto parseredir; parseredir_return:;} 791 #define PARSESUB() {goto parsesub; parsesub_return:;} 792 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 793 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 794 #define PARSEARITH() {goto parsearith; parsearith_return:;} 795 796 STATIC int 797 readtoken1(firstc, syntax, eofmark, striptabs) 798 int firstc; 799 char const *syntax; 800 char *eofmark; 801 int striptabs; 802 { 803 register c = firstc; 804 register char *out; 805 int len; 806 char line[EOFMARKLEN + 1]; 807 struct nodelist *bqlist; 808 int quotef; 809 int dblquote; 810 int varnest; /* levels of variables expansion */ 811 int arinest; /* levels of arithmetic expansion */ 812 int parenlevel; /* levels of parens in arithmetic */ 813 int oldstyle; 814 char const *prevsyntax; /* syntax before arithmetic */ 815 816 startlinno = plinno; 817 dblquote = 0; 818 if (syntax == DQSYNTAX) 819 dblquote = 1; 820 quotef = 0; 821 bqlist = NULL; 822 varnest = 0; 823 arinest = 0; 824 parenlevel = 0; 825 826 STARTSTACKSTR(out); 827 loop: { /* for each line, until end of word */ 828 #if ATTY 829 if (c == '\034' && doprompt 830 && attyset() && ! equal(termval(), "emacs")) { 831 attyline(); 832 if (syntax == BASESYNTAX) 833 return readtoken(); 834 c = pgetc(); 835 goto loop; 836 } 837 #endif 838 CHECKEND(); /* set c to PEOF if at end of here document */ 839 for (;;) { /* until end of line or end of word */ 840 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 841 if (parsebackquote && c == '\\') { 842 c = pgetc(); /* XXX - compat with old /bin/sh */ 843 if (/*c != '\\' && */c != '`' && c != '$') { 844 pungetc(); 845 c = '\\'; 846 } 847 } 848 switch(syntax[c]) { 849 case CNL: /* '\n' */ 850 if (syntax == BASESYNTAX) 851 goto endword; /* exit outer loop */ 852 USTPUTC(c, out); 853 plinno++; 854 if (doprompt) 855 setprompt(2); 856 else 857 setprompt(0); 858 c = pgetc(); 859 goto loop; /* continue outer loop */ 860 case CWORD: 861 USTPUTC(c, out); 862 break; 863 case CCTL: 864 if (eofmark == NULL || dblquote) 865 USTPUTC(CTLESC, out); 866 USTPUTC(c, out); 867 break; 868 case CBACK: /* backslash */ 869 c = pgetc(); 870 if (c == PEOF) { 871 USTPUTC('\\', out); 872 pungetc(); 873 } else if (c == '\n') { 874 if (doprompt) 875 setprompt(2); 876 else 877 setprompt(0); 878 } else { 879 if (dblquote && c != '\\' && c != '`' && c != '$' 880 && (c != '"' || eofmark != NULL)) 881 USTPUTC('\\', out); 882 if (SQSYNTAX[c] == CCTL) 883 USTPUTC(CTLESC, out); 884 USTPUTC(c, out); 885 quotef++; 886 } 887 break; 888 case CSQUOTE: 889 syntax = SQSYNTAX; 890 break; 891 case CDQUOTE: 892 syntax = DQSYNTAX; 893 dblquote = 1; 894 break; 895 case CENDQUOTE: 896 if (eofmark) { 897 USTPUTC(c, out); 898 } else { 899 if (arinest) 900 syntax = ARISYNTAX; 901 else 902 syntax = BASESYNTAX; 903 quotef++; 904 dblquote = 0; 905 } 906 break; 907 case CVAR: /* '$' */ 908 PARSESUB(); /* parse substitution */ 909 break; 910 case CENDVAR: /* '}' */ 911 if (varnest > 0) { 912 varnest--; 913 USTPUTC(CTLENDVAR, out); 914 } else { 915 USTPUTC(c, out); 916 } 917 break; 918 case CLP: /* '(' in arithmetic */ 919 parenlevel++; 920 USTPUTC(c, out); 921 break; 922 case CRP: /* ')' in arithmetic */ 923 if (parenlevel > 0) { 924 USTPUTC(c, out); 925 --parenlevel; 926 } else { 927 if (pgetc() == ')') { 928 if (--arinest == 0) { 929 USTPUTC(CTLENDARI, out); 930 syntax = prevsyntax; 931 } else 932 USTPUTC(')', out); 933 } else { 934 /* 935 * unbalanced parens 936 * (don't 2nd guess - no error) 937 */ 938 pungetc(); 939 USTPUTC(')', out); 940 } 941 } 942 break; 943 case CBQUOTE: /* '`' */ 944 PARSEBACKQOLD(); 945 break; 946 case CEOF: 947 goto endword; /* exit outer loop */ 948 default: 949 if (varnest == 0) 950 goto endword; /* exit outer loop */ 951 USTPUTC(c, out); 952 } 953 c = pgetc_macro(); 954 } 955 } 956 endword: 957 if (syntax == ARISYNTAX) 958 synerror("Missing '))'"); 959 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 960 synerror("Unterminated quoted string"); 961 if (varnest != 0) { 962 startlinno = plinno; 963 synerror("Missing '}'"); 964 } 965 USTPUTC('\0', out); 966 len = out - stackblock(); 967 out = stackblock(); 968 if (eofmark == NULL) { 969 if ((c == '>' || c == '<') 970 && quotef == 0 971 && len <= 2 972 && (*out == '\0' || is_digit(*out))) { 973 PARSEREDIR(); 974 return lasttoken = TREDIR; 975 } else { 976 pungetc(); 977 } 978 } 979 quoteflag = quotef; 980 backquotelist = bqlist; 981 grabstackblock(len); 982 wordtext = out; 983 return lasttoken = TWORD; 984 /* end of readtoken routine */ 985 986 987 988 /* 989 * Check to see whether we are at the end of the here document. When this 990 * is called, c is set to the first character of the next input line. If 991 * we are at the end of the here document, this routine sets the c to PEOF. 992 */ 993 994 checkend: { 995 if (eofmark) { 996 if (striptabs) { 997 while (c == '\t') 998 c = pgetc(); 999 } 1000 if (c == *eofmark) { 1001 if (pfgets(line, sizeof line) != NULL) { 1002 register char *p, *q; 1003 1004 p = line; 1005 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1006 if (*p == '\n' && *q == '\0') { 1007 c = PEOF; 1008 plinno++; 1009 needprompt = doprompt; 1010 } else { 1011 pushstring(line, strlen(line), NULL); 1012 } 1013 } 1014 } 1015 } 1016 goto checkend_return; 1017 } 1018 1019 1020 /* 1021 * Parse a redirection operator. The variable "out" points to a string 1022 * specifying the fd to be redirected. The variable "c" contains the 1023 * first character of the redirection operator. 1024 */ 1025 1026 parseredir: { 1027 char fd = *out; 1028 union node *np; 1029 1030 np = (union node *)stalloc(sizeof (struct nfile)); 1031 if (c == '>') { 1032 np->nfile.fd = 1; 1033 c = pgetc(); 1034 if (c == '>') 1035 np->type = NAPPEND; 1036 else if (c == '&') 1037 np->type = NTOFD; 1038 else { 1039 np->type = NTO; 1040 pungetc(); 1041 } 1042 } else { /* c == '<' */ 1043 np->nfile.fd = 0; 1044 c = pgetc(); 1045 if (c == '<') { 1046 if (sizeof (struct nfile) != sizeof (struct nhere)) { 1047 np = (union node *)stalloc(sizeof (struct nhere)); 1048 np->nfile.fd = 0; 1049 } 1050 np->type = NHERE; 1051 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 1052 heredoc->here = np; 1053 if ((c = pgetc()) == '-') { 1054 heredoc->striptabs = 1; 1055 } else { 1056 heredoc->striptabs = 0; 1057 pungetc(); 1058 } 1059 } else if (c == '&') 1060 np->type = NFROMFD; 1061 else { 1062 np->type = NFROM; 1063 pungetc(); 1064 } 1065 } 1066 if (fd != '\0') 1067 np->nfile.fd = digit_val(fd); 1068 redirnode = np; 1069 goto parseredir_return; 1070 } 1071 1072 1073 /* 1074 * Parse a substitution. At this point, we have read the dollar sign 1075 * and nothing else. 1076 */ 1077 1078 parsesub: { 1079 int subtype; 1080 int typeloc; 1081 int flags; 1082 char *p; 1083 #ifndef GDB_HACK 1084 static const char types[] = "}-+?="; 1085 #endif 1086 1087 c = pgetc(); 1088 if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 1089 USTPUTC('$', out); 1090 pungetc(); 1091 } else if (c == '(') { /* $(command) or $((arith)) */ 1092 if (pgetc() == '(') { 1093 PARSEARITH(); 1094 } else { 1095 pungetc(); 1096 PARSEBACKQNEW(); 1097 } 1098 } else { 1099 USTPUTC(CTLVAR, out); 1100 typeloc = out - stackblock(); 1101 USTPUTC(VSNORMAL, out); 1102 subtype = VSNORMAL; 1103 if (c == '{') { 1104 c = pgetc(); 1105 subtype = 0; 1106 } 1107 if (is_name(c)) { 1108 do { 1109 STPUTC(c, out); 1110 c = pgetc(); 1111 } while (is_in_name(c)); 1112 } else { 1113 if (! is_special(c)) 1114 badsub: synerror("Bad substitution"); 1115 USTPUTC(c, out); 1116 c = pgetc(); 1117 } 1118 STPUTC('=', out); 1119 flags = 0; 1120 if (subtype == 0) { 1121 if (c == ':') { 1122 flags = VSNUL; 1123 c = pgetc(); 1124 } 1125 p = strchr(types, c); 1126 if (p == NULL) 1127 goto badsub; 1128 subtype = p - types + VSNORMAL; 1129 } else { 1130 pungetc(); 1131 } 1132 if (dblquote || arinest) 1133 flags |= VSQUOTE; 1134 *(stackblock() + typeloc) = subtype | flags; 1135 if (subtype != VSNORMAL) 1136 varnest++; 1137 } 1138 goto parsesub_return; 1139 } 1140 1141 1142 /* 1143 * Called to parse command substitutions. Newstyle is set if the command 1144 * is enclosed inside $(...); nlpp is a pointer to the head of the linked 1145 * list of commands (passed by reference), and savelen is the number of 1146 * characters on the top of the stack which must be preserved. 1147 */ 1148 1149 parsebackq: { 1150 struct nodelist **nlpp; 1151 int savepbq; 1152 union node *n; 1153 char *volatile str; 1154 struct jmploc jmploc; 1155 struct jmploc *volatile savehandler; 1156 int savelen; 1157 1158 savepbq = parsebackquote; 1159 if (setjmp(jmploc.loc)) { 1160 if (str) 1161 ckfree(str); 1162 parsebackquote = 0; 1163 handler = savehandler; 1164 longjmp(handler->loc, 1); 1165 } 1166 INTOFF; 1167 str = NULL; 1168 savelen = out - stackblock(); 1169 if (savelen > 0) { 1170 str = ckmalloc(savelen); 1171 bcopy(stackblock(), str, savelen); 1172 } 1173 savehandler = handler; 1174 handler = &jmploc; 1175 INTON; 1176 if (oldstyle) { 1177 /* We must read until the closing backquote, giving special 1178 treatment to some slashes, and then push the string and 1179 reread it as input, interpreting it normally. */ 1180 register char *out; 1181 register c; 1182 int savelen; 1183 char *str; 1184 1185 STARTSTACKSTR(out); 1186 while ((c = pgetc ()) != '`') { 1187 if (c == PEOF) { 1188 startlinno = plinno; 1189 synerror("EOF in backquote substitution"); 1190 } 1191 if (c == '\\') { 1192 c = pgetc (); 1193 if ( c != '\\' && c != '`' && c != '$' 1194 && (!dblquote || c != '"')) 1195 STPUTC('\\', out); 1196 } 1197 STPUTC(c, out); 1198 } 1199 STPUTC('\0', out); 1200 savelen = out - stackblock(); 1201 if (savelen > 0) { 1202 str = ckmalloc(savelen); 1203 bcopy(stackblock(), str, savelen); 1204 } 1205 setinputstring(str, 1); 1206 } 1207 nlpp = &bqlist; 1208 while (*nlpp) 1209 nlpp = &(*nlpp)->next; 1210 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1211 (*nlpp)->next = NULL; 1212 parsebackquote = oldstyle; 1213 n = list(0); 1214 if (!oldstyle && (readtoken() != TRP)) 1215 synexpect(TRP); 1216 (*nlpp)->n = n; 1217 /* Start reading from old file again. */ 1218 if (oldstyle) 1219 popfile(); 1220 while (stackblocksize() <= savelen) 1221 growstackblock(); 1222 STARTSTACKSTR(out); 1223 if (str) { 1224 bcopy(str, out, savelen); 1225 STADJUST(savelen, out); 1226 INTOFF; 1227 ckfree(str); 1228 str = NULL; 1229 INTON; 1230 } 1231 parsebackquote = savepbq; 1232 handler = savehandler; 1233 if (arinest || dblquote) 1234 USTPUTC(CTLBACKQ | CTLQUOTE, out); 1235 else 1236 USTPUTC(CTLBACKQ, out); 1237 if (oldstyle) 1238 goto parsebackq_oldreturn; 1239 else 1240 goto parsebackq_newreturn; 1241 } 1242 1243 /* 1244 * Parse an arithmetic expansion (indicate start of one and set state) 1245 */ 1246 parsearith: { 1247 1248 if (++arinest == 1) { 1249 prevsyntax = syntax; 1250 syntax = ARISYNTAX; 1251 USTPUTC(CTLARI, out); 1252 } else { 1253 /* 1254 * we collapse embedded arithmetic expansion to 1255 * parenthesis, which should be equivalent 1256 */ 1257 USTPUTC('(', out); 1258 } 1259 goto parsearith_return; 1260 } 1261 1262 } /* end of readtoken */ 1263 1264 1265 1266 #ifdef mkinit 1267 RESET { 1268 tokpushback = 0; 1269 checkkwd = 0; 1270 } 1271 #endif 1272 1273 /* 1274 * Returns true if the text contains nothing to expand (no dollar signs 1275 * or backquotes). 1276 */ 1277 1278 STATIC int 1279 noexpand(text) 1280 char *text; 1281 { 1282 register char *p; 1283 register char c; 1284 1285 p = text; 1286 while ((c = *p++) != '\0') { 1287 if (c == CTLESC) 1288 p++; 1289 else if (BASESYNTAX[c] == CCTL) 1290 return 0; 1291 } 1292 return 1; 1293 } 1294 1295 1296 /* 1297 * Return true if the argument is a legal variable name (a letter or 1298 * underscore followed by zero or more letters, underscores, and digits). 1299 */ 1300 1301 int 1302 goodname(name) 1303 char *name; 1304 { 1305 register char *p; 1306 1307 p = name; 1308 if (! is_name(*p)) 1309 return 0; 1310 while (*++p) { 1311 if (! is_in_name(*p)) 1312 return 0; 1313 } 1314 return 1; 1315 } 1316 1317 1318 /* 1319 * Called when an unexpected token is read during the parse. The argument 1320 * is the token that is expected, or -1 if more than one type of token can 1321 * occur at this point. 1322 */ 1323 1324 STATIC void 1325 synexpect(token) { 1326 char msg[64]; 1327 1328 if (token >= 0) { 1329 fmtstr(msg, 64, "%s unexpected (expecting %s)", 1330 tokname[lasttoken], tokname[token]); 1331 } else { 1332 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 1333 } 1334 synerror(msg); 1335 } 1336 1337 1338 STATIC void 1339 synerror(msg) 1340 char *msg; 1341 { 1342 if (commandname) 1343 outfmt(&errout, "%s: %d: ", commandname, startlinno); 1344 outfmt(&errout, "Syntax error: %s\n", msg); 1345 error((char *)NULL); 1346 } 1347 1348 STATIC void 1349 setprompt(which) 1350 int which; 1351 { 1352 whichprompt = which; 1353 1354 if (!el) 1355 out2str(getprompt(NULL)); 1356 } 1357 1358 /* 1359 * called by editline -- any expansions to the prompt 1360 * should be added here. 1361 */ 1362 char * 1363 getprompt(unused) 1364 void *unused; 1365 { 1366 switch (whichprompt) { 1367 case 0: 1368 return ""; 1369 case 1: 1370 return ps1val(); 1371 case 2: 1372 return ps2val(); 1373 default: 1374 return "<internal prompt error>"; 1375 } 1376 } 1377