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