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