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