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