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