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