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