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