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