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