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