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