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