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