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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <stdio.h> 44 45 #include "shell.h" 46 #include "parser.h" 47 #include "nodes.h" 48 #include "expand.h" /* defines rmescapes() */ 49 #include "syntax.h" 50 #include "options.h" 51 #include "input.h" 52 #include "output.h" 53 #include "var.h" 54 #include "error.h" 55 #include "memalloc.h" 56 #include "mystring.h" 57 #include "alias.h" 58 #include "show.h" 59 #include "eval.h" 60 #include "exec.h" /* to check for special builtins */ 61 #ifndef NO_HISTORY 62 #include "myhistedit.h" 63 #endif 64 65 /* 66 * Shell command parser. 67 */ 68 69 #define EOFMARKLEN 79 70 #define PROMPTLEN 128 71 72 /* values of checkkwd variable */ 73 #define CHKALIAS 0x1 74 #define CHKKWD 0x2 75 #define CHKNL 0x4 76 77 /* values returned by readtoken */ 78 #include "token.h" 79 80 81 82 struct heredoc { 83 struct heredoc *next; /* next here document in list */ 84 union node *here; /* redirection node */ 85 char *eofmark; /* string indicating end of input */ 86 int striptabs; /* if set, strip leading tabs */ 87 }; 88 89 struct parser_temp { 90 struct parser_temp *next; 91 void *data; 92 }; 93 94 95 static struct heredoc *heredoclist; /* list of here documents to read */ 96 static int doprompt; /* if set, prompt the user */ 97 static int needprompt; /* true if interactive and at start of line */ 98 static int lasttoken; /* last token read */ 99 int tokpushback; /* last token pushed back */ 100 static char *wordtext; /* text of last word returned by readtoken */ 101 static int checkkwd; 102 static struct nodelist *backquotelist; 103 static union node *redirnode; 104 static struct heredoc *heredoc; 105 static int quoteflag; /* set if (part of) last token was quoted */ 106 static int startlinno; /* line # where last token started */ 107 static int funclinno; /* line # where the current function started */ 108 static struct parser_temp *parser_temp; 109 110 111 static union node *list(int, int); 112 static union node *andor(void); 113 static union node *pipeline(void); 114 static union node *command(void); 115 static union node *simplecmd(union node **, union node *); 116 static union node *makename(void); 117 static void parsefname(void); 118 static void parseheredoc(void); 119 static int peektoken(void); 120 static int readtoken(void); 121 static int xxreadtoken(void); 122 static int readtoken1(int, const char *, const char *, int); 123 static int noexpand(char *); 124 static void synexpect(int) __dead2; 125 static void synerror(const char *) __dead2; 126 static void setprompt(int); 127 128 129 static void * 130 parser_temp_alloc(size_t len) 131 { 132 struct parser_temp *t; 133 134 INTOFF; 135 t = ckmalloc(sizeof(*t)); 136 t->data = NULL; 137 t->next = parser_temp; 138 parser_temp = t; 139 t->data = ckmalloc(len); 140 INTON; 141 return t->data; 142 } 143 144 145 static void * 146 parser_temp_realloc(void *ptr, size_t len) 147 { 148 struct parser_temp *t; 149 150 INTOFF; 151 t = parser_temp; 152 if (ptr != t->data) 153 error("bug: parser_temp_realloc misused"); 154 t->data = ckrealloc(t->data, len); 155 INTON; 156 return t->data; 157 } 158 159 160 static void 161 parser_temp_free_upto(void *ptr) 162 { 163 struct parser_temp *t; 164 int done = 0; 165 166 INTOFF; 167 while (parser_temp != NULL && !done) { 168 t = parser_temp; 169 parser_temp = t->next; 170 done = t->data == ptr; 171 ckfree(t->data); 172 ckfree(t); 173 } 174 INTON; 175 if (!done) 176 error("bug: parser_temp_free_upto misused"); 177 } 178 179 180 static void 181 parser_temp_free_all(void) 182 { 183 struct parser_temp *t; 184 185 INTOFF; 186 while (parser_temp != NULL) { 187 t = parser_temp; 188 parser_temp = t->next; 189 ckfree(t->data); 190 ckfree(t); 191 } 192 INTON; 193 } 194 195 196 /* 197 * Read and parse a command. Returns NEOF on end of file. (NULL is a 198 * valid parse tree indicating a blank line.) 199 */ 200 201 union node * 202 parsecmd(int interact) 203 { 204 int t; 205 206 /* This assumes the parser is not re-entered, 207 * which could happen if we add command substitution on PS1/PS2. 208 */ 209 parser_temp_free_all(); 210 heredoclist = NULL; 211 212 tokpushback = 0; 213 checkkwd = 0; 214 doprompt = interact; 215 if (doprompt) 216 setprompt(1); 217 else 218 setprompt(0); 219 needprompt = 0; 220 t = readtoken(); 221 if (t == TEOF) 222 return NEOF; 223 if (t == TNL) 224 return NULL; 225 tokpushback++; 226 return list(1, 1); 227 } 228 229 230 static union node * 231 list(int nlflag, int erflag) 232 { 233 union node *ntop, *n1, *n2, *n3; 234 int tok; 235 236 checkkwd = CHKNL | CHKKWD | CHKALIAS; 237 if (!nlflag && !erflag && tokendlist[peektoken()]) 238 return NULL; 239 ntop = n1 = NULL; 240 for (;;) { 241 n2 = andor(); 242 tok = readtoken(); 243 if (tok == TBACKGND) { 244 if (n2 != NULL && n2->type == NPIPE) { 245 n2->npipe.backgnd = 1; 246 } else if (n2 != NULL && n2->type == NREDIR) { 247 n2->type = NBACKGND; 248 } else { 249 n3 = (union node *)stalloc(sizeof (struct nredir)); 250 n3->type = NBACKGND; 251 n3->nredir.n = n2; 252 n3->nredir.redirect = NULL; 253 n2 = n3; 254 } 255 } 256 if (ntop == NULL) 257 ntop = n2; 258 else if (n1 == NULL) { 259 n1 = (union node *)stalloc(sizeof (struct nbinary)); 260 n1->type = NSEMI; 261 n1->nbinary.ch1 = ntop; 262 n1->nbinary.ch2 = n2; 263 ntop = n1; 264 } 265 else { 266 n3 = (union node *)stalloc(sizeof (struct nbinary)); 267 n3->type = NSEMI; 268 n3->nbinary.ch1 = n1->nbinary.ch2; 269 n3->nbinary.ch2 = n2; 270 n1->nbinary.ch2 = n3; 271 n1 = n3; 272 } 273 switch (tok) { 274 case TBACKGND: 275 case TSEMI: 276 tok = readtoken(); 277 /* FALLTHROUGH */ 278 case TNL: 279 if (tok == TNL) { 280 parseheredoc(); 281 if (nlflag) 282 return ntop; 283 } else if (tok == TEOF && nlflag) { 284 parseheredoc(); 285 return ntop; 286 } else { 287 tokpushback++; 288 } 289 checkkwd = CHKNL | CHKKWD | CHKALIAS; 290 if (!nlflag && (erflag ? peektoken() == TEOF : 291 tokendlist[peektoken()])) 292 return ntop; 293 break; 294 case TEOF: 295 if (heredoclist) 296 parseheredoc(); 297 else 298 pungetc(); /* push back EOF on input */ 299 return ntop; 300 default: 301 if (nlflag || erflag) 302 synexpect(-1); 303 tokpushback++; 304 return ntop; 305 } 306 } 307 } 308 309 310 311 static union node * 312 andor(void) 313 { 314 union node *n1, *n2, *n3; 315 int t; 316 317 n1 = pipeline(); 318 for (;;) { 319 if ((t = readtoken()) == TAND) { 320 t = NAND; 321 } else if (t == TOR) { 322 t = NOR; 323 } else { 324 tokpushback++; 325 return n1; 326 } 327 n2 = pipeline(); 328 n3 = (union node *)stalloc(sizeof (struct nbinary)); 329 n3->type = t; 330 n3->nbinary.ch1 = n1; 331 n3->nbinary.ch2 = n2; 332 n1 = n3; 333 } 334 } 335 336 337 338 static union node * 339 pipeline(void) 340 { 341 union node *n1, *n2, *pipenode; 342 struct nodelist *lp, *prev; 343 int negate, t; 344 345 negate = 0; 346 checkkwd = CHKNL | CHKKWD | CHKALIAS; 347 TRACE(("pipeline: entered\n")); 348 while (readtoken() == TNOT) 349 negate = !negate; 350 tokpushback++; 351 n1 = command(); 352 if (readtoken() == TPIPE) { 353 pipenode = (union node *)stalloc(sizeof (struct npipe)); 354 pipenode->type = NPIPE; 355 pipenode->npipe.backgnd = 0; 356 lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 357 pipenode->npipe.cmdlist = lp; 358 lp->n = n1; 359 do { 360 prev = lp; 361 lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 362 checkkwd = CHKNL | CHKKWD | CHKALIAS; 363 t = readtoken(); 364 tokpushback++; 365 if (t == TNOT) 366 lp->n = pipeline(); 367 else 368 lp->n = command(); 369 prev->next = lp; 370 } while (readtoken() == TPIPE); 371 lp->next = NULL; 372 n1 = pipenode; 373 } 374 tokpushback++; 375 if (negate) { 376 n2 = (union node *)stalloc(sizeof (struct nnot)); 377 n2->type = NNOT; 378 n2->nnot.com = n1; 379 return n2; 380 } else 381 return n1; 382 } 383 384 385 386 static union node * 387 command(void) 388 { 389 union node *n1, *n2; 390 union node *ap, **app; 391 union node *cp, **cpp; 392 union node *redir, **rpp; 393 int t; 394 int is_subshell; 395 396 checkkwd = CHKNL | CHKKWD | CHKALIAS; 397 is_subshell = 0; 398 redir = NULL; 399 n1 = NULL; 400 rpp = &redir; 401 402 /* Check for redirection which may precede command */ 403 while (readtoken() == TREDIR) { 404 *rpp = n2 = redirnode; 405 rpp = &n2->nfile.next; 406 parsefname(); 407 } 408 tokpushback++; 409 410 switch (readtoken()) { 411 case TIF: 412 n1 = (union node *)stalloc(sizeof (struct nif)); 413 n1->type = NIF; 414 if ((n1->nif.test = list(0, 0)) == NULL) 415 synexpect(-1); 416 if (readtoken() != TTHEN) 417 synexpect(TTHEN); 418 n1->nif.ifpart = list(0, 0); 419 n2 = n1; 420 while (readtoken() == TELIF) { 421 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 422 n2 = n2->nif.elsepart; 423 n2->type = NIF; 424 if ((n2->nif.test = list(0, 0)) == NULL) 425 synexpect(-1); 426 if (readtoken() != TTHEN) 427 synexpect(TTHEN); 428 n2->nif.ifpart = list(0, 0); 429 } 430 if (lasttoken == TELSE) 431 n2->nif.elsepart = list(0, 0); 432 else { 433 n2->nif.elsepart = NULL; 434 tokpushback++; 435 } 436 if (readtoken() != TFI) 437 synexpect(TFI); 438 checkkwd = CHKKWD | CHKALIAS; 439 break; 440 case TWHILE: 441 case TUNTIL: { 442 int got; 443 n1 = (union node *)stalloc(sizeof (struct nbinary)); 444 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 445 if ((n1->nbinary.ch1 = list(0, 0)) == NULL) 446 synexpect(-1); 447 if ((got=readtoken()) != TDO) { 448 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 449 synexpect(TDO); 450 } 451 n1->nbinary.ch2 = list(0, 0); 452 if (readtoken() != TDONE) 453 synexpect(TDONE); 454 checkkwd = CHKKWD | CHKALIAS; 455 break; 456 } 457 case TFOR: 458 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 459 synerror("Bad for loop variable"); 460 n1 = (union node *)stalloc(sizeof (struct nfor)); 461 n1->type = NFOR; 462 n1->nfor.var = wordtext; 463 while (readtoken() == TNL) 464 ; 465 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 466 app = ≈ 467 while (readtoken() == TWORD) { 468 n2 = (union node *)stalloc(sizeof (struct narg)); 469 n2->type = NARG; 470 n2->narg.text = wordtext; 471 n2->narg.backquote = backquotelist; 472 *app = n2; 473 app = &n2->narg.next; 474 } 475 *app = NULL; 476 n1->nfor.args = ap; 477 if (lasttoken != TNL && lasttoken != TSEMI) 478 synexpect(-1); 479 } else { 480 static char argvars[5] = { 481 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 482 }; 483 n2 = (union node *)stalloc(sizeof (struct narg)); 484 n2->type = NARG; 485 n2->narg.text = argvars; 486 n2->narg.backquote = NULL; 487 n2->narg.next = NULL; 488 n1->nfor.args = n2; 489 /* 490 * Newline or semicolon here is optional (but note 491 * that the original Bourne shell only allowed NL). 492 */ 493 if (lasttoken != TNL && lasttoken != TSEMI) 494 tokpushback++; 495 } 496 checkkwd = CHKNL | CHKKWD | CHKALIAS; 497 if ((t = readtoken()) == TDO) 498 t = TDONE; 499 else if (t == TBEGIN) 500 t = TEND; 501 else 502 synexpect(-1); 503 n1->nfor.body = list(0, 0); 504 if (readtoken() != t) 505 synexpect(t); 506 checkkwd = CHKKWD | CHKALIAS; 507 break; 508 case TCASE: 509 n1 = (union node *)stalloc(sizeof (struct ncase)); 510 n1->type = NCASE; 511 if (readtoken() != TWORD) 512 synexpect(TWORD); 513 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 514 n2->type = NARG; 515 n2->narg.text = wordtext; 516 n2->narg.backquote = backquotelist; 517 n2->narg.next = NULL; 518 while (readtoken() == TNL); 519 if (lasttoken != TWORD || ! equal(wordtext, "in")) 520 synerror("expecting \"in\""); 521 cpp = &n1->ncase.cases; 522 checkkwd = CHKNL | CHKKWD, readtoken(); 523 while (lasttoken != TESAC) { 524 *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 525 cp->type = NCLIST; 526 app = &cp->nclist.pattern; 527 if (lasttoken == TLP) 528 readtoken(); 529 for (;;) { 530 *app = ap = (union node *)stalloc(sizeof (struct narg)); 531 ap->type = NARG; 532 ap->narg.text = wordtext; 533 ap->narg.backquote = backquotelist; 534 checkkwd = CHKNL | CHKKWD; 535 if (readtoken() != TPIPE) 536 break; 537 app = &ap->narg.next; 538 readtoken(); 539 } 540 ap->narg.next = NULL; 541 if (lasttoken != TRP) 542 synexpect(TRP); 543 cp->nclist.body = list(0, 0); 544 545 checkkwd = CHKNL | CHKKWD | CHKALIAS; 546 if ((t = readtoken()) != TESAC) { 547 if (t == TENDCASE) 548 ; 549 else if (t == TFALLTHRU) 550 cp->type = NCLISTFALLTHRU; 551 else 552 synexpect(TENDCASE); 553 checkkwd = CHKNL | CHKKWD, readtoken(); 554 } 555 cpp = &cp->nclist.next; 556 } 557 *cpp = NULL; 558 checkkwd = CHKKWD | CHKALIAS; 559 break; 560 case TLP: 561 n1 = (union node *)stalloc(sizeof (struct nredir)); 562 n1->type = NSUBSHELL; 563 n1->nredir.n = list(0, 0); 564 n1->nredir.redirect = NULL; 565 if (readtoken() != TRP) 566 synexpect(TRP); 567 checkkwd = CHKKWD | CHKALIAS; 568 is_subshell = 1; 569 break; 570 case TBEGIN: 571 n1 = list(0, 0); 572 if (readtoken() != TEND) 573 synexpect(TEND); 574 checkkwd = CHKKWD | CHKALIAS; 575 break; 576 /* Handle an empty command like other simple commands. */ 577 case TBACKGND: 578 case TSEMI: 579 case TAND: 580 case TOR: 581 case TPIPE: 582 case TENDCASE: 583 case TFALLTHRU: 584 /* 585 * An empty command before a ; doesn't make much sense, and 586 * should certainly be disallowed in the case of `if ;'. 587 */ 588 if (!redir) 589 synexpect(-1); 590 case TNL: 591 case TEOF: 592 case TWORD: 593 case TRP: 594 tokpushback++; 595 n1 = simplecmd(rpp, redir); 596 return n1; 597 default: 598 synexpect(-1); 599 } 600 601 /* Now check for redirection which may follow command */ 602 while (readtoken() == TREDIR) { 603 *rpp = n2 = redirnode; 604 rpp = &n2->nfile.next; 605 parsefname(); 606 } 607 tokpushback++; 608 *rpp = NULL; 609 if (redir) { 610 if (!is_subshell) { 611 n2 = (union node *)stalloc(sizeof (struct nredir)); 612 n2->type = NREDIR; 613 n2->nredir.n = n1; 614 n1 = n2; 615 } 616 n1->nredir.redirect = redir; 617 } 618 619 return n1; 620 } 621 622 623 static union node * 624 simplecmd(union node **rpp, union node *redir) 625 { 626 union node *args, **app; 627 union node **orig_rpp = rpp; 628 union node *n = NULL; 629 int special; 630 int savecheckkwd; 631 632 /* If we don't have any redirections already, then we must reset */ 633 /* rpp to be the address of the local redir variable. */ 634 if (redir == 0) 635 rpp = &redir; 636 637 args = NULL; 638 app = &args; 639 /* 640 * We save the incoming value, because we need this for shell 641 * functions. There can not be a redirect or an argument between 642 * the function name and the open parenthesis. 643 */ 644 orig_rpp = rpp; 645 646 savecheckkwd = CHKALIAS; 647 648 for (;;) { 649 checkkwd = savecheckkwd; 650 if (readtoken() == TWORD) { 651 n = (union node *)stalloc(sizeof (struct narg)); 652 n->type = NARG; 653 n->narg.text = wordtext; 654 n->narg.backquote = backquotelist; 655 *app = n; 656 app = &n->narg.next; 657 if (savecheckkwd != 0 && !isassignment(wordtext)) 658 savecheckkwd = 0; 659 } else if (lasttoken == TREDIR) { 660 *rpp = n = redirnode; 661 rpp = &n->nfile.next; 662 parsefname(); /* read name of redirection file */ 663 } else if (lasttoken == TLP && app == &args->narg.next 664 && rpp == orig_rpp) { 665 /* We have a function */ 666 if (readtoken() != TRP) 667 synexpect(TRP); 668 funclinno = plinno; 669 /* 670 * - Require plain text. 671 * - Functions with '/' cannot be called. 672 * - Reject name=(). 673 * - Reject ksh extended glob patterns. 674 */ 675 if (!noexpand(n->narg.text) || quoteflag || 676 strchr(n->narg.text, '/') || 677 strchr("!%*+-=?@}~", 678 n->narg.text[strlen(n->narg.text) - 1])) 679 synerror("Bad function name"); 680 rmescapes(n->narg.text); 681 if (find_builtin(n->narg.text, &special) >= 0 && 682 special) 683 synerror("Cannot override a special builtin with a function"); 684 n->type = NDEFUN; 685 n->narg.next = command(); 686 funclinno = 0; 687 return n; 688 } else { 689 tokpushback++; 690 break; 691 } 692 } 693 *app = NULL; 694 *rpp = NULL; 695 n = (union node *)stalloc(sizeof (struct ncmd)); 696 n->type = NCMD; 697 n->ncmd.args = args; 698 n->ncmd.redirect = redir; 699 return n; 700 } 701 702 static union node * 703 makename(void) 704 { 705 union node *n; 706 707 n = (union node *)stalloc(sizeof (struct narg)); 708 n->type = NARG; 709 n->narg.next = NULL; 710 n->narg.text = wordtext; 711 n->narg.backquote = backquotelist; 712 return n; 713 } 714 715 void 716 fixredir(union node *n, const char *text, int err) 717 { 718 TRACE(("Fix redir %s %d\n", text, err)); 719 if (!err) 720 n->ndup.vname = NULL; 721 722 if (is_digit(text[0]) && text[1] == '\0') 723 n->ndup.dupfd = digit_val(text[0]); 724 else if (text[0] == '-' && text[1] == '\0') 725 n->ndup.dupfd = -1; 726 else { 727 728 if (err) 729 synerror("Bad fd number"); 730 else 731 n->ndup.vname = makename(); 732 } 733 } 734 735 736 static void 737 parsefname(void) 738 { 739 union node *n = redirnode; 740 741 if (readtoken() != TWORD) 742 synexpect(-1); 743 if (n->type == NHERE) { 744 struct heredoc *here = heredoc; 745 struct heredoc *p; 746 int i; 747 748 if (quoteflag == 0) 749 n->type = NXHERE; 750 TRACE(("Here document %d\n", n->type)); 751 if (here->striptabs) { 752 while (*wordtext == '\t') 753 wordtext++; 754 } 755 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 756 synerror("Illegal eof marker for << redirection"); 757 rmescapes(wordtext); 758 here->eofmark = wordtext; 759 here->next = NULL; 760 if (heredoclist == NULL) 761 heredoclist = here; 762 else { 763 for (p = heredoclist ; p->next ; p = p->next); 764 p->next = here; 765 } 766 } else if (n->type == NTOFD || n->type == NFROMFD) { 767 fixredir(n, wordtext, 0); 768 } else { 769 n->nfile.fname = makename(); 770 } 771 } 772 773 774 /* 775 * Input any here documents. 776 */ 777 778 static void 779 parseheredoc(void) 780 { 781 struct heredoc *here; 782 union node *n; 783 784 while (heredoclist) { 785 here = heredoclist; 786 heredoclist = here->next; 787 if (needprompt) { 788 setprompt(2); 789 needprompt = 0; 790 } 791 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 792 here->eofmark, here->striptabs); 793 n = (union node *)stalloc(sizeof (struct narg)); 794 n->narg.type = NARG; 795 n->narg.next = NULL; 796 n->narg.text = wordtext; 797 n->narg.backquote = backquotelist; 798 here->here->nhere.doc = n; 799 } 800 } 801 802 static int 803 peektoken(void) 804 { 805 int t; 806 807 t = readtoken(); 808 tokpushback++; 809 return (t); 810 } 811 812 static int 813 readtoken(void) 814 { 815 int t; 816 struct alias *ap; 817 #ifdef DEBUG 818 int alreadyseen = tokpushback; 819 #endif 820 821 top: 822 t = xxreadtoken(); 823 824 /* 825 * eat newlines 826 */ 827 if (checkkwd & CHKNL) { 828 while (t == TNL) { 829 parseheredoc(); 830 t = xxreadtoken(); 831 } 832 } 833 834 /* 835 * check for keywords and aliases 836 */ 837 if (t == TWORD && !quoteflag) 838 { 839 const char * const *pp; 840 841 if (checkkwd & CHKKWD) 842 for (pp = parsekwd; *pp; pp++) { 843 if (**pp == *wordtext && equal(*pp, wordtext)) 844 { 845 lasttoken = t = pp - parsekwd + KWDOFFSET; 846 TRACE(("keyword %s recognized\n", tokname[t])); 847 goto out; 848 } 849 } 850 if (checkkwd & CHKALIAS && 851 (ap = lookupalias(wordtext, 1)) != NULL) { 852 pushstring(ap->val, strlen(ap->val), ap); 853 goto top; 854 } 855 } 856 out: 857 if (t != TNOT) 858 checkkwd = 0; 859 860 #ifdef DEBUG 861 if (!alreadyseen) 862 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 863 else 864 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 865 #endif 866 return (t); 867 } 868 869 870 /* 871 * Read the next input token. 872 * If the token is a word, we set backquotelist to the list of cmds in 873 * backquotes. We set quoteflag to true if any part of the word was 874 * quoted. 875 * If the token is TREDIR, then we set redirnode to a structure containing 876 * the redirection. 877 * In all cases, the variable startlinno is set to the number of the line 878 * on which the token starts. 879 * 880 * [Change comment: here documents and internal procedures] 881 * [Readtoken shouldn't have any arguments. Perhaps we should make the 882 * word parsing code into a separate routine. In this case, readtoken 883 * doesn't need to have any internal procedures, but parseword does. 884 * We could also make parseoperator in essence the main routine, and 885 * have parseword (readtoken1?) handle both words and redirection.] 886 */ 887 888 #define RETURN(token) return lasttoken = token 889 890 static int 891 xxreadtoken(void) 892 { 893 int c; 894 895 if (tokpushback) { 896 tokpushback = 0; 897 return lasttoken; 898 } 899 if (needprompt) { 900 setprompt(2); 901 needprompt = 0; 902 } 903 startlinno = plinno; 904 for (;;) { /* until token or start of word found */ 905 c = pgetc_macro(); 906 switch (c) { 907 case ' ': case '\t': 908 continue; 909 case '#': 910 while ((c = pgetc()) != '\n' && c != PEOF); 911 pungetc(); 912 continue; 913 case '\\': 914 if (pgetc() == '\n') { 915 startlinno = ++plinno; 916 if (doprompt) 917 setprompt(2); 918 else 919 setprompt(0); 920 continue; 921 } 922 pungetc(); 923 goto breakloop; 924 case '\n': 925 plinno++; 926 needprompt = doprompt; 927 RETURN(TNL); 928 case PEOF: 929 RETURN(TEOF); 930 case '&': 931 if (pgetc() == '&') 932 RETURN(TAND); 933 pungetc(); 934 RETURN(TBACKGND); 935 case '|': 936 if (pgetc() == '|') 937 RETURN(TOR); 938 pungetc(); 939 RETURN(TPIPE); 940 case ';': 941 c = pgetc(); 942 if (c == ';') 943 RETURN(TENDCASE); 944 else if (c == '&') 945 RETURN(TFALLTHRU); 946 pungetc(); 947 RETURN(TSEMI); 948 case '(': 949 RETURN(TLP); 950 case ')': 951 RETURN(TRP); 952 default: 953 goto breakloop; 954 } 955 } 956 breakloop: 957 return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 958 #undef RETURN 959 } 960 961 962 #define MAXNEST_static 8 963 struct tokenstate 964 { 965 const char *syntax; /* *SYNTAX */ 966 int parenlevel; /* levels of parentheses in arithmetic */ 967 enum tokenstate_category 968 { 969 TSTATE_TOP, 970 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 971 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 972 TSTATE_ARITH 973 } category; 974 }; 975 976 977 /* 978 * Called to parse command substitutions. 979 */ 980 981 static char * 982 parsebackq(char *out, struct nodelist **pbqlist, 983 int oldstyle, int dblquote, int quoted) 984 { 985 struct nodelist **nlpp; 986 union node *n; 987 char *volatile str; 988 struct jmploc jmploc; 989 struct jmploc *const savehandler = handler; 990 size_t savelen; 991 int saveprompt; 992 const int bq_startlinno = plinno; 993 char *volatile ostr = NULL; 994 struct parsefile *const savetopfile = getcurrentfile(); 995 struct heredoc *const saveheredoclist = heredoclist; 996 struct heredoc *here; 997 998 str = NULL; 999 if (setjmp(jmploc.loc)) { 1000 popfilesupto(savetopfile); 1001 if (str) 1002 ckfree(str); 1003 if (ostr) 1004 ckfree(ostr); 1005 heredoclist = saveheredoclist; 1006 handler = savehandler; 1007 if (exception == EXERROR) { 1008 startlinno = bq_startlinno; 1009 synerror("Error in command substitution"); 1010 } 1011 longjmp(handler->loc, 1); 1012 } 1013 INTOFF; 1014 savelen = out - stackblock(); 1015 if (savelen > 0) { 1016 str = ckmalloc(savelen); 1017 memcpy(str, stackblock(), savelen); 1018 } 1019 handler = &jmploc; 1020 heredoclist = NULL; 1021 INTON; 1022 if (oldstyle) { 1023 /* We must read until the closing backquote, giving special 1024 treatment to some slashes, and then push the string and 1025 reread it as input, interpreting it normally. */ 1026 char *oout; 1027 int c; 1028 int olen; 1029 1030 1031 STARTSTACKSTR(oout); 1032 for (;;) { 1033 if (needprompt) { 1034 setprompt(2); 1035 needprompt = 0; 1036 } 1037 CHECKSTRSPACE(2, oout); 1038 switch (c = pgetc()) { 1039 case '`': 1040 goto done; 1041 1042 case '\\': 1043 if ((c = pgetc()) == '\n') { 1044 plinno++; 1045 if (doprompt) 1046 setprompt(2); 1047 else 1048 setprompt(0); 1049 /* 1050 * If eating a newline, avoid putting 1051 * the newline into the new character 1052 * stream (via the USTPUTC after the 1053 * switch). 1054 */ 1055 continue; 1056 } 1057 if (c != '\\' && c != '`' && c != '$' 1058 && (!dblquote || c != '"')) 1059 USTPUTC('\\', oout); 1060 break; 1061 1062 case '\n': 1063 plinno++; 1064 needprompt = doprompt; 1065 break; 1066 1067 case PEOF: 1068 startlinno = plinno; 1069 synerror("EOF in backquote substitution"); 1070 break; 1071 1072 default: 1073 break; 1074 } 1075 USTPUTC(c, oout); 1076 } 1077 done: 1078 USTPUTC('\0', oout); 1079 olen = oout - stackblock(); 1080 INTOFF; 1081 ostr = ckmalloc(olen); 1082 memcpy(ostr, stackblock(), olen); 1083 setinputstring(ostr, 1); 1084 INTON; 1085 } 1086 nlpp = pbqlist; 1087 while (*nlpp) 1088 nlpp = &(*nlpp)->next; 1089 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1090 (*nlpp)->next = NULL; 1091 1092 if (oldstyle) { 1093 saveprompt = doprompt; 1094 doprompt = 0; 1095 } 1096 1097 n = list(0, oldstyle); 1098 1099 if (oldstyle) 1100 doprompt = saveprompt; 1101 else { 1102 if (readtoken() != TRP) 1103 synexpect(TRP); 1104 } 1105 1106 (*nlpp)->n = n; 1107 if (oldstyle) { 1108 /* 1109 * Start reading from old file again, ignoring any pushed back 1110 * tokens left from the backquote parsing 1111 */ 1112 popfile(); 1113 tokpushback = 0; 1114 } 1115 STARTSTACKSTR(out); 1116 CHECKSTRSPACE(savelen + 1, out); 1117 INTOFF; 1118 if (str) { 1119 memcpy(out, str, savelen); 1120 STADJUST(savelen, out); 1121 ckfree(str); 1122 str = NULL; 1123 } 1124 if (ostr) { 1125 ckfree(ostr); 1126 ostr = NULL; 1127 } 1128 here = saveheredoclist; 1129 if (here != NULL) { 1130 while (here->next != NULL) 1131 here = here->next; 1132 here->next = heredoclist; 1133 heredoclist = saveheredoclist; 1134 } 1135 handler = savehandler; 1136 INTON; 1137 if (quoted) 1138 USTPUTC(CTLBACKQ | CTLQUOTE, out); 1139 else 1140 USTPUTC(CTLBACKQ, out); 1141 return out; 1142 } 1143 1144 1145 /* 1146 * Called to parse a backslash escape sequence inside $'...'. 1147 * The backslash has already been read. 1148 */ 1149 static char * 1150 readcstyleesc(char *out) 1151 { 1152 int c, v, i, n; 1153 1154 c = pgetc(); 1155 switch (c) { 1156 case '\0': 1157 synerror("Unterminated quoted string"); 1158 case '\n': 1159 plinno++; 1160 if (doprompt) 1161 setprompt(2); 1162 else 1163 setprompt(0); 1164 return out; 1165 case '\\': 1166 case '\'': 1167 case '"': 1168 v = c; 1169 break; 1170 case 'a': v = '\a'; break; 1171 case 'b': v = '\b'; break; 1172 case 'e': v = '\033'; break; 1173 case 'f': v = '\f'; break; 1174 case 'n': v = '\n'; break; 1175 case 'r': v = '\r'; break; 1176 case 't': v = '\t'; break; 1177 case 'v': v = '\v'; break; 1178 case 'x': 1179 v = 0; 1180 for (;;) { 1181 c = pgetc(); 1182 if (c >= '0' && c <= '9') 1183 v = (v << 4) + c - '0'; 1184 else if (c >= 'A' && c <= 'F') 1185 v = (v << 4) + c - 'A' + 10; 1186 else if (c >= 'a' && c <= 'f') 1187 v = (v << 4) + c - 'a' + 10; 1188 else 1189 break; 1190 } 1191 pungetc(); 1192 break; 1193 case '0': case '1': case '2': case '3': 1194 case '4': case '5': case '6': case '7': 1195 v = c - '0'; 1196 c = pgetc(); 1197 if (c >= '0' && c <= '7') { 1198 v <<= 3; 1199 v += c - '0'; 1200 c = pgetc(); 1201 if (c >= '0' && c <= '7') { 1202 v <<= 3; 1203 v += c - '0'; 1204 } else 1205 pungetc(); 1206 } else 1207 pungetc(); 1208 break; 1209 case 'c': 1210 c = pgetc(); 1211 if (c < 0x3f || c > 0x7a || c == 0x60) 1212 synerror("Bad escape sequence"); 1213 if (c == '\\' && pgetc() != '\\') 1214 synerror("Bad escape sequence"); 1215 if (c == '?') 1216 v = 127; 1217 else 1218 v = c & 0x1f; 1219 break; 1220 case 'u': 1221 case 'U': 1222 n = c == 'U' ? 8 : 4; 1223 v = 0; 1224 for (i = 0; i < n; i++) { 1225 c = pgetc(); 1226 if (c >= '0' && c <= '9') 1227 v = (v << 4) + c - '0'; 1228 else if (c >= 'A' && c <= 'F') 1229 v = (v << 4) + c - 'A' + 10; 1230 else if (c >= 'a' && c <= 'f') 1231 v = (v << 4) + c - 'a' + 10; 1232 else 1233 synerror("Bad escape sequence"); 1234 } 1235 if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1236 synerror("Bad escape sequence"); 1237 /* We really need iconv here. */ 1238 if (initial_localeisutf8 && v > 127) { 1239 CHECKSTRSPACE(4, out); 1240 /* 1241 * We cannot use wctomb() as the locale may have 1242 * changed. 1243 */ 1244 if (v <= 0x7ff) { 1245 USTPUTC(0xc0 | v >> 6, out); 1246 USTPUTC(0x80 | (v & 0x3f), out); 1247 return out; 1248 } else if (v <= 0xffff) { 1249 USTPUTC(0xe0 | v >> 12, out); 1250 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1251 USTPUTC(0x80 | (v & 0x3f), out); 1252 return out; 1253 } else if (v <= 0x10ffff) { 1254 USTPUTC(0xf0 | v >> 18, out); 1255 USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1256 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1257 USTPUTC(0x80 | (v & 0x3f), out); 1258 return out; 1259 } 1260 } 1261 if (v > 127) 1262 v = '?'; 1263 break; 1264 default: 1265 synerror("Bad escape sequence"); 1266 } 1267 v = (char)v; 1268 /* 1269 * We can't handle NUL bytes. 1270 * POSIX says we should skip till the closing quote. 1271 */ 1272 if (v == '\0') { 1273 while ((c = pgetc()) != '\'') { 1274 if (c == '\\') 1275 c = pgetc(); 1276 if (c == PEOF) 1277 synerror("Unterminated quoted string"); 1278 } 1279 pungetc(); 1280 return out; 1281 } 1282 if (SQSYNTAX[v] == CCTL) 1283 USTPUTC(CTLESC, out); 1284 USTPUTC(v, out); 1285 return out; 1286 } 1287 1288 1289 /* 1290 * If eofmark is NULL, read a word or a redirection symbol. If eofmark 1291 * is not NULL, read a here document. In the latter case, eofmark is the 1292 * word which marks the end of the document and striptabs is true if 1293 * leading tabs should be stripped from the document. The argument firstc 1294 * is the first character of the input token or document. 1295 * 1296 * Because C does not have internal subroutines, I have simulated them 1297 * using goto's to implement the subroutine linkage. The following macros 1298 * will run code that appears at the end of readtoken1. 1299 */ 1300 1301 #define CHECKEND() {goto checkend; checkend_return:;} 1302 #define PARSEREDIR() {goto parseredir; parseredir_return:;} 1303 #define PARSESUB() {goto parsesub; parsesub_return:;} 1304 #define PARSEARITH() {goto parsearith; parsearith_return:;} 1305 1306 static int 1307 readtoken1(int firstc, char const *initialsyntax, const char *eofmark, 1308 int striptabs) 1309 { 1310 int c = firstc; 1311 char *out; 1312 int len; 1313 char line[EOFMARKLEN + 1]; 1314 struct nodelist *bqlist; 1315 int quotef; 1316 int newvarnest; 1317 int level; 1318 int synentry; 1319 struct tokenstate state_static[MAXNEST_static]; 1320 int maxnest = MAXNEST_static; 1321 struct tokenstate *state = state_static; 1322 int sqiscstyle = 0; 1323 1324 startlinno = plinno; 1325 quotef = 0; 1326 bqlist = NULL; 1327 newvarnest = 0; 1328 level = 0; 1329 state[level].syntax = initialsyntax; 1330 state[level].parenlevel = 0; 1331 state[level].category = TSTATE_TOP; 1332 1333 STARTSTACKSTR(out); 1334 loop: { /* for each line, until end of word */ 1335 CHECKEND(); /* set c to PEOF if at end of here document */ 1336 for (;;) { /* until end of line or end of word */ 1337 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 1338 1339 synentry = state[level].syntax[c]; 1340 1341 switch(synentry) { 1342 case CNL: /* '\n' */ 1343 if (state[level].syntax == BASESYNTAX) 1344 goto endword; /* exit outer loop */ 1345 USTPUTC(c, out); 1346 plinno++; 1347 if (doprompt) 1348 setprompt(2); 1349 else 1350 setprompt(0); 1351 c = pgetc(); 1352 goto loop; /* continue outer loop */ 1353 case CSBACK: 1354 if (sqiscstyle) { 1355 out = readcstyleesc(out); 1356 break; 1357 } 1358 /* FALLTHROUGH */ 1359 case CWORD: 1360 USTPUTC(c, out); 1361 break; 1362 case CCTL: 1363 if (eofmark == NULL || initialsyntax != SQSYNTAX) 1364 USTPUTC(CTLESC, out); 1365 USTPUTC(c, out); 1366 break; 1367 case CBACK: /* backslash */ 1368 c = pgetc(); 1369 if (c == PEOF) { 1370 USTPUTC('\\', out); 1371 pungetc(); 1372 } else if (c == '\n') { 1373 plinno++; 1374 if (doprompt) 1375 setprompt(2); 1376 else 1377 setprompt(0); 1378 } else { 1379 if (state[level].syntax == DQSYNTAX && 1380 c != '\\' && c != '`' && c != '$' && 1381 (c != '"' || (eofmark != NULL && 1382 newvarnest == 0)) && 1383 (c != '}' || state[level].category != TSTATE_VAR_OLD)) 1384 USTPUTC('\\', out); 1385 if ((eofmark == NULL || 1386 newvarnest > 0) && 1387 state[level].syntax == BASESYNTAX) 1388 USTPUTC(CTLQUOTEMARK, out); 1389 if (SQSYNTAX[c] == CCTL) 1390 USTPUTC(CTLESC, out); 1391 USTPUTC(c, out); 1392 if ((eofmark == NULL || 1393 newvarnest > 0) && 1394 state[level].syntax == BASESYNTAX && 1395 state[level].category == TSTATE_VAR_OLD) 1396 USTPUTC(CTLQUOTEEND, out); 1397 quotef++; 1398 } 1399 break; 1400 case CSQUOTE: 1401 USTPUTC(CTLQUOTEMARK, out); 1402 state[level].syntax = SQSYNTAX; 1403 sqiscstyle = 0; 1404 break; 1405 case CDQUOTE: 1406 USTPUTC(CTLQUOTEMARK, out); 1407 state[level].syntax = DQSYNTAX; 1408 break; 1409 case CENDQUOTE: 1410 if (eofmark != NULL && newvarnest == 0) 1411 USTPUTC(c, out); 1412 else { 1413 if (state[level].category == TSTATE_VAR_OLD) 1414 USTPUTC(CTLQUOTEEND, out); 1415 state[level].syntax = BASESYNTAX; 1416 quotef++; 1417 } 1418 break; 1419 case CVAR: /* '$' */ 1420 PARSESUB(); /* parse substitution */ 1421 break; 1422 case CENDVAR: /* '}' */ 1423 if (level > 0 && 1424 ((state[level].category == TSTATE_VAR_OLD && 1425 state[level].syntax == 1426 state[level - 1].syntax) || 1427 (state[level].category == TSTATE_VAR_NEW && 1428 state[level].syntax == BASESYNTAX))) { 1429 if (state[level].category == TSTATE_VAR_NEW) 1430 newvarnest--; 1431 level--; 1432 USTPUTC(CTLENDVAR, out); 1433 } else { 1434 USTPUTC(c, out); 1435 } 1436 break; 1437 case CLP: /* '(' in arithmetic */ 1438 state[level].parenlevel++; 1439 USTPUTC(c, out); 1440 break; 1441 case CRP: /* ')' in arithmetic */ 1442 if (state[level].parenlevel > 0) { 1443 USTPUTC(c, out); 1444 --state[level].parenlevel; 1445 } else { 1446 if (pgetc() == ')') { 1447 if (level > 0 && 1448 state[level].category == TSTATE_ARITH) { 1449 level--; 1450 USTPUTC(CTLENDARI, out); 1451 } else 1452 USTPUTC(')', out); 1453 } else { 1454 /* 1455 * unbalanced parens 1456 * (don't 2nd guess - no error) 1457 */ 1458 pungetc(); 1459 USTPUTC(')', out); 1460 } 1461 } 1462 break; 1463 case CBQUOTE: /* '`' */ 1464 out = parsebackq(out, &bqlist, 1, 1465 state[level].syntax == DQSYNTAX && 1466 (eofmark == NULL || newvarnest > 0), 1467 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 1468 break; 1469 case CEOF: 1470 goto endword; /* exit outer loop */ 1471 case CIGN: 1472 break; 1473 default: 1474 if (level == 0) 1475 goto endword; /* exit outer loop */ 1476 USTPUTC(c, out); 1477 } 1478 c = pgetc_macro(); 1479 } 1480 } 1481 endword: 1482 if (state[level].syntax == ARISYNTAX) 1483 synerror("Missing '))'"); 1484 if (state[level].syntax != BASESYNTAX && eofmark == NULL) 1485 synerror("Unterminated quoted string"); 1486 if (state[level].category == TSTATE_VAR_OLD || 1487 state[level].category == TSTATE_VAR_NEW) { 1488 startlinno = plinno; 1489 synerror("Missing '}'"); 1490 } 1491 if (state != state_static) 1492 parser_temp_free_upto(state); 1493 USTPUTC('\0', out); 1494 len = out - stackblock(); 1495 out = stackblock(); 1496 if (eofmark == NULL) { 1497 if ((c == '>' || c == '<') 1498 && quotef == 0 1499 && len <= 2 1500 && (*out == '\0' || is_digit(*out))) { 1501 PARSEREDIR(); 1502 return lasttoken = TREDIR; 1503 } else { 1504 pungetc(); 1505 } 1506 } 1507 quoteflag = quotef; 1508 backquotelist = bqlist; 1509 grabstackblock(len); 1510 wordtext = out; 1511 return lasttoken = TWORD; 1512 /* end of readtoken routine */ 1513 1514 1515 /* 1516 * Check to see whether we are at the end of the here document. When this 1517 * is called, c is set to the first character of the next input line. If 1518 * we are at the end of the here document, this routine sets the c to PEOF. 1519 */ 1520 1521 checkend: { 1522 if (eofmark) { 1523 if (striptabs) { 1524 while (c == '\t') 1525 c = pgetc(); 1526 } 1527 if (c == *eofmark) { 1528 if (pfgets(line, sizeof line) != NULL) { 1529 const char *p, *q; 1530 1531 p = line; 1532 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1533 if ((*p == '\0' || *p == '\n') && *q == '\0') { 1534 c = PEOF; 1535 if (*p == '\n') { 1536 plinno++; 1537 needprompt = doprompt; 1538 } 1539 } else { 1540 pushstring(line, strlen(line), NULL); 1541 } 1542 } 1543 } 1544 } 1545 goto checkend_return; 1546 } 1547 1548 1549 /* 1550 * Parse a redirection operator. The variable "out" points to a string 1551 * specifying the fd to be redirected. The variable "c" contains the 1552 * first character of the redirection operator. 1553 */ 1554 1555 parseredir: { 1556 char fd = *out; 1557 union node *np; 1558 1559 np = (union node *)stalloc(sizeof (struct nfile)); 1560 if (c == '>') { 1561 np->nfile.fd = 1; 1562 c = pgetc(); 1563 if (c == '>') 1564 np->type = NAPPEND; 1565 else if (c == '&') 1566 np->type = NTOFD; 1567 else if (c == '|') 1568 np->type = NCLOBBER; 1569 else { 1570 np->type = NTO; 1571 pungetc(); 1572 } 1573 } else { /* c == '<' */ 1574 np->nfile.fd = 0; 1575 c = pgetc(); 1576 if (c == '<') { 1577 if (sizeof (struct nfile) != sizeof (struct nhere)) { 1578 np = (union node *)stalloc(sizeof (struct nhere)); 1579 np->nfile.fd = 0; 1580 } 1581 np->type = NHERE; 1582 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 1583 heredoc->here = np; 1584 if ((c = pgetc()) == '-') { 1585 heredoc->striptabs = 1; 1586 } else { 1587 heredoc->striptabs = 0; 1588 pungetc(); 1589 } 1590 } else if (c == '&') 1591 np->type = NFROMFD; 1592 else if (c == '>') 1593 np->type = NFROMTO; 1594 else { 1595 np->type = NFROM; 1596 pungetc(); 1597 } 1598 } 1599 if (fd != '\0') 1600 np->nfile.fd = digit_val(fd); 1601 redirnode = np; 1602 goto parseredir_return; 1603 } 1604 1605 1606 /* 1607 * Parse a substitution. At this point, we have read the dollar sign 1608 * and nothing else. 1609 */ 1610 1611 parsesub: { 1612 char buf[10]; 1613 int subtype; 1614 int typeloc; 1615 int flags; 1616 char *p; 1617 static const char types[] = "}-+?="; 1618 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1619 int linno; 1620 int length; 1621 int c1; 1622 1623 c = pgetc(); 1624 if (c == '(') { /* $(command) or $((arith)) */ 1625 if (pgetc() == '(') { 1626 PARSEARITH(); 1627 } else { 1628 pungetc(); 1629 out = parsebackq(out, &bqlist, 0, 1630 state[level].syntax == DQSYNTAX && 1631 (eofmark == NULL || newvarnest > 0), 1632 state[level].syntax == DQSYNTAX || 1633 state[level].syntax == ARISYNTAX); 1634 } 1635 } else if (c == '{' || is_name(c) || is_special(c)) { 1636 USTPUTC(CTLVAR, out); 1637 typeloc = out - stackblock(); 1638 USTPUTC(VSNORMAL, out); 1639 subtype = VSNORMAL; 1640 flags = 0; 1641 if (c == '{') { 1642 bracketed_name = 1; 1643 c = pgetc(); 1644 subtype = 0; 1645 } 1646 varname: 1647 if (!is_eof(c) && is_name(c)) { 1648 length = 0; 1649 do { 1650 STPUTC(c, out); 1651 c = pgetc(); 1652 length++; 1653 } while (!is_eof(c) && is_in_name(c)); 1654 if (length == 6 && 1655 strncmp(out - length, "LINENO", length) == 0) { 1656 /* Replace the variable name with the 1657 * current line number. */ 1658 linno = plinno; 1659 if (funclinno != 0) 1660 linno -= funclinno - 1; 1661 snprintf(buf, sizeof(buf), "%d", linno); 1662 STADJUST(-6, out); 1663 STPUTS(buf, out); 1664 flags |= VSLINENO; 1665 } 1666 } else if (is_digit(c)) { 1667 if (bracketed_name) { 1668 do { 1669 STPUTC(c, out); 1670 c = pgetc(); 1671 } while (is_digit(c)); 1672 } else { 1673 STPUTC(c, out); 1674 c = pgetc(); 1675 } 1676 } else if (is_special(c)) { 1677 c1 = c; 1678 c = pgetc(); 1679 if (subtype == 0 && c1 == '#') { 1680 subtype = VSLENGTH; 1681 if (strchr(types, c) == NULL && c != ':' && 1682 c != '#' && c != '%') 1683 goto varname; 1684 c1 = c; 1685 c = pgetc(); 1686 if (c1 != '}' && c == '}') { 1687 pungetc(); 1688 c = c1; 1689 goto varname; 1690 } 1691 pungetc(); 1692 c = c1; 1693 c1 = '#'; 1694 subtype = 0; 1695 } 1696 USTPUTC(c1, out); 1697 } else { 1698 subtype = VSERROR; 1699 if (c == '}') 1700 pungetc(); 1701 else if (c == '\n' || c == PEOF) 1702 synerror("Unexpected end of line in substitution"); 1703 else 1704 USTPUTC(c, out); 1705 } 1706 if (subtype == 0) { 1707 switch (c) { 1708 case ':': 1709 flags |= VSNUL; 1710 c = pgetc(); 1711 /*FALLTHROUGH*/ 1712 default: 1713 p = strchr(types, c); 1714 if (p == NULL) { 1715 if (c == '\n' || c == PEOF) 1716 synerror("Unexpected end of line in substitution"); 1717 if (flags == VSNUL) 1718 STPUTC(':', out); 1719 STPUTC(c, out); 1720 subtype = VSERROR; 1721 } else 1722 subtype = p - types + VSNORMAL; 1723 break; 1724 case '%': 1725 case '#': 1726 { 1727 int cc = c; 1728 subtype = c == '#' ? VSTRIMLEFT : 1729 VSTRIMRIGHT; 1730 c = pgetc(); 1731 if (c == cc) 1732 subtype++; 1733 else 1734 pungetc(); 1735 break; 1736 } 1737 } 1738 } else if (subtype != VSERROR) { 1739 if (subtype == VSLENGTH && c != '}') 1740 subtype = VSERROR; 1741 pungetc(); 1742 } 1743 STPUTC('=', out); 1744 if (state[level].syntax == DQSYNTAX || 1745 state[level].syntax == ARISYNTAX) 1746 flags |= VSQUOTE; 1747 *(stackblock() + typeloc) = subtype | flags; 1748 if (subtype != VSNORMAL) { 1749 if (level + 1 >= maxnest) { 1750 maxnest *= 2; 1751 if (state == state_static) { 1752 state = parser_temp_alloc( 1753 maxnest * sizeof(*state)); 1754 memcpy(state, state_static, 1755 MAXNEST_static * sizeof(*state)); 1756 } else 1757 state = parser_temp_realloc(state, 1758 maxnest * sizeof(*state)); 1759 } 1760 level++; 1761 state[level].parenlevel = 0; 1762 if (subtype == VSMINUS || subtype == VSPLUS || 1763 subtype == VSQUESTION || subtype == VSASSIGN) { 1764 /* 1765 * For operators that were in the Bourne shell, 1766 * inherit the double-quote state. 1767 */ 1768 state[level].syntax = state[level - 1].syntax; 1769 state[level].category = TSTATE_VAR_OLD; 1770 } else { 1771 /* 1772 * The other operators take a pattern, 1773 * so go to BASESYNTAX. 1774 * Also, ' and " are now special, even 1775 * in here documents. 1776 */ 1777 state[level].syntax = BASESYNTAX; 1778 state[level].category = TSTATE_VAR_NEW; 1779 newvarnest++; 1780 } 1781 } 1782 } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1783 /* $'cstylequotes' */ 1784 USTPUTC(CTLQUOTEMARK, out); 1785 state[level].syntax = SQSYNTAX; 1786 sqiscstyle = 1; 1787 } else { 1788 USTPUTC('$', out); 1789 pungetc(); 1790 } 1791 goto parsesub_return; 1792 } 1793 1794 1795 /* 1796 * Parse an arithmetic expansion (indicate start of one and set state) 1797 */ 1798 parsearith: { 1799 1800 if (level + 1 >= maxnest) { 1801 maxnest *= 2; 1802 if (state == state_static) { 1803 state = parser_temp_alloc( 1804 maxnest * sizeof(*state)); 1805 memcpy(state, state_static, 1806 MAXNEST_static * sizeof(*state)); 1807 } else 1808 state = parser_temp_realloc(state, 1809 maxnest * sizeof(*state)); 1810 } 1811 level++; 1812 state[level].syntax = ARISYNTAX; 1813 state[level].parenlevel = 0; 1814 state[level].category = TSTATE_ARITH; 1815 USTPUTC(CTLARI, out); 1816 if (state[level - 1].syntax == DQSYNTAX) 1817 USTPUTC('"',out); 1818 else 1819 USTPUTC(' ',out); 1820 goto parsearith_return; 1821 } 1822 1823 } /* end of readtoken */ 1824 1825 1826 /* 1827 * Returns true if the text contains nothing to expand (no dollar signs 1828 * or backquotes). 1829 */ 1830 1831 static int 1832 noexpand(char *text) 1833 { 1834 char *p; 1835 char c; 1836 1837 p = text; 1838 while ((c = *p++) != '\0') { 1839 if ( c == CTLQUOTEMARK) 1840 continue; 1841 if (c == CTLESC) 1842 p++; 1843 else if (BASESYNTAX[(int)c] == CCTL) 1844 return 0; 1845 } 1846 return 1; 1847 } 1848 1849 1850 /* 1851 * Return true if the argument is a legal variable name (a letter or 1852 * underscore followed by zero or more letters, underscores, and digits). 1853 */ 1854 1855 int 1856 goodname(const char *name) 1857 { 1858 const char *p; 1859 1860 p = name; 1861 if (! is_name(*p)) 1862 return 0; 1863 while (*++p) { 1864 if (! is_in_name(*p)) 1865 return 0; 1866 } 1867 return 1; 1868 } 1869 1870 1871 int 1872 isassignment(const char *p) 1873 { 1874 if (!is_name(*p)) 1875 return 0; 1876 p++; 1877 for (;;) { 1878 if (*p == '=') 1879 return 1; 1880 else if (!is_in_name(*p)) 1881 return 0; 1882 p++; 1883 } 1884 } 1885 1886 1887 /* 1888 * Called when an unexpected token is read during the parse. The argument 1889 * is the token that is expected, or -1 if more than one type of token can 1890 * occur at this point. 1891 */ 1892 1893 static void 1894 synexpect(int token) 1895 { 1896 char msg[64]; 1897 1898 if (token >= 0) { 1899 fmtstr(msg, 64, "%s unexpected (expecting %s)", 1900 tokname[lasttoken], tokname[token]); 1901 } else { 1902 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 1903 } 1904 synerror(msg); 1905 } 1906 1907 1908 static void 1909 synerror(const char *msg) 1910 { 1911 if (commandname) 1912 outfmt(out2, "%s: %d: ", commandname, startlinno); 1913 outfmt(out2, "Syntax error: %s\n", msg); 1914 error((char *)NULL); 1915 } 1916 1917 static void 1918 setprompt(int which) 1919 { 1920 whichprompt = which; 1921 1922 #ifndef NO_HISTORY 1923 if (!el) 1924 #endif 1925 { 1926 out2str(getprompt(NULL)); 1927 flushout(out2); 1928 } 1929 } 1930 1931 /* 1932 * called by editline -- any expansions to the prompt 1933 * should be added here. 1934 */ 1935 char * 1936 getprompt(void *unused __unused) 1937 { 1938 static char ps[PROMPTLEN]; 1939 char *fmt; 1940 const char *pwd; 1941 int i, trim; 1942 static char internal_error[] = "??"; 1943 1944 /* 1945 * Select prompt format. 1946 */ 1947 switch (whichprompt) { 1948 case 0: 1949 fmt = nullstr; 1950 break; 1951 case 1: 1952 fmt = ps1val(); 1953 break; 1954 case 2: 1955 fmt = ps2val(); 1956 break; 1957 default: 1958 return internal_error; 1959 } 1960 1961 /* 1962 * Format prompt string. 1963 */ 1964 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1965 if (*fmt == '\\') 1966 switch (*++fmt) { 1967 1968 /* 1969 * Hostname. 1970 * 1971 * \h specifies just the local hostname, 1972 * \H specifies fully-qualified hostname. 1973 */ 1974 case 'h': 1975 case 'H': 1976 ps[i] = '\0'; 1977 gethostname(&ps[i], PROMPTLEN - i); 1978 /* Skip to end of hostname. */ 1979 trim = (*fmt == 'h') ? '.' : '\0'; 1980 while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1981 i++; 1982 break; 1983 1984 /* 1985 * Working directory. 1986 * 1987 * \W specifies just the final component, 1988 * \w specifies the entire path. 1989 */ 1990 case 'W': 1991 case 'w': 1992 pwd = lookupvar("PWD"); 1993 if (pwd == NULL) 1994 pwd = "?"; 1995 if (*fmt == 'W' && 1996 *pwd == '/' && pwd[1] != '\0') 1997 strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1998 PROMPTLEN - i); 1999 else 2000 strlcpy(&ps[i], pwd, PROMPTLEN - i); 2001 /* Skip to end of path. */ 2002 while (ps[i + 1] != '\0') 2003 i++; 2004 break; 2005 2006 /* 2007 * Superuser status. 2008 * 2009 * '$' for normal users, '#' for root. 2010 */ 2011 case '$': 2012 ps[i] = (geteuid() != 0) ? '$' : '#'; 2013 break; 2014 2015 /* 2016 * A literal \. 2017 */ 2018 case '\\': 2019 ps[i] = '\\'; 2020 break; 2021 2022 /* 2023 * Emit unrecognized formats verbatim. 2024 */ 2025 default: 2026 ps[i++] = '\\'; 2027 ps[i] = *fmt; 2028 break; 2029 } 2030 else 2031 ps[i] = *fmt; 2032 ps[i] = '\0'; 2033 return (ps); 2034 } 2035 2036 2037 const char * 2038 expandstr(const char *ps) 2039 { 2040 union node n; 2041 struct jmploc jmploc; 2042 struct jmploc *const savehandler = handler; 2043 const int saveprompt = doprompt; 2044 struct parsefile *const savetopfile = getcurrentfile(); 2045 struct parser_temp *const saveparser_temp = parser_temp; 2046 const char *result = NULL; 2047 2048 if (!setjmp(jmploc.loc)) { 2049 handler = &jmploc; 2050 parser_temp = NULL; 2051 setinputstring(ps, 1); 2052 doprompt = 0; 2053 readtoken1(pgetc(), DQSYNTAX, "\n\n", 0); 2054 if (backquotelist != NULL) 2055 error("Command substitution not allowed here"); 2056 2057 n.narg.type = NARG; 2058 n.narg.next = NULL; 2059 n.narg.text = wordtext; 2060 n.narg.backquote = backquotelist; 2061 2062 expandarg(&n, NULL, 0); 2063 result = stackblock(); 2064 INTOFF; 2065 } 2066 handler = savehandler; 2067 doprompt = saveprompt; 2068 popfilesupto(savetopfile); 2069 if (parser_temp != saveparser_temp) { 2070 parser_temp_free_all(); 2071 parser_temp = saveparser_temp; 2072 } 2073 if (result != NULL) { 2074 INTON; 2075 } else if (exception == EXINT) 2076 raise(SIGINT); 2077 return result; 2078 } 2079