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