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