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