parser.c revision 149026
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/parser.c 149026 2005-08-13 15:47:13Z stefanf $"); 401556Srgrimes 4117987Speter#include <stdlib.h> 42149017Sstefanf#include <unistd.h> 4317987Speter 441556Srgrimes#include "shell.h" 451556Srgrimes#include "parser.h" 461556Srgrimes#include "nodes.h" 471556Srgrimes#include "expand.h" /* defines rmescapes() */ 481556Srgrimes#include "syntax.h" 491556Srgrimes#include "options.h" 501556Srgrimes#include "input.h" 511556Srgrimes#include "output.h" 521556Srgrimes#include "var.h" 531556Srgrimes#include "error.h" 541556Srgrimes#include "memalloc.h" 551556Srgrimes#include "mystring.h" 561556Srgrimes#include "alias.h" 5717987Speter#include "show.h" 5859436Scracauer#include "eval.h" 5917987Speter#ifndef NO_HISTORY 601556Srgrimes#include "myhistedit.h" 6117987Speter#endif 621556Srgrimes 631556Srgrimes/* 641556Srgrimes * Shell command parser. 651556Srgrimes */ 661556Srgrimes 67142845Sobrien#define EOFMARKLEN 79 68142845Sobrien#define PROMPTLEN 128 691556Srgrimes 701556Srgrimes/* values returned by readtoken */ 7117987Speter#include "token.h" 721556Srgrimes 731556Srgrimes 741556Srgrimes 751556Srgrimesstruct heredoc { 761556Srgrimes struct heredoc *next; /* next here document in list */ 771556Srgrimes union node *here; /* redirection node */ 781556Srgrimes char *eofmark; /* string indicating end of input */ 791556Srgrimes int striptabs; /* if set, strip leading tabs */ 801556Srgrimes}; 811556Srgrimes 821556Srgrimes 831556Srgrimes 84117261SddsSTATIC struct heredoc *heredoclist; /* list of here documents to read */ 85117261SddsSTATIC int parsebackquote; /* nonzero if we are inside backquotes */ 86117261SddsSTATIC int doprompt; /* if set, prompt the user */ 87117261SddsSTATIC int needprompt; /* true if interactive and at start of line */ 88117261SddsSTATIC int lasttoken; /* last token read */ 891556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 90117261SddsSTATIC char *wordtext; /* text of last word returned by readtoken */ 911556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 92117261SddsSTATIC struct nodelist *backquotelist; 93117261SddsSTATIC union node *redirnode; 94117261SddsSTATIC struct heredoc *heredoc; 95117261SddsSTATIC int quoteflag; /* set if (part of) last token was quoted */ 96117261SddsSTATIC int startlinno; /* line # where last token started */ 971556Srgrimes 9818018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 9918018Speterstatic int noaliases = 0; 1001556Srgrimes 1011556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 1021556Srgrimes#ifdef GDB_HACK 1031556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 1041556Srgrimesstatic const char types[] = "}-+?="; 1051556Srgrimes#endif 1061556Srgrimes 1071556Srgrimes 10890111SimpSTATIC union node *list(int); 10990111SimpSTATIC union node *andor(void); 11090111SimpSTATIC union node *pipeline(void); 11190111SimpSTATIC union node *command(void); 11290111SimpSTATIC union node *simplecmd(union node **, union node *); 11390111SimpSTATIC union node *makename(void); 11490111SimpSTATIC void parsefname(void); 11590111SimpSTATIC void parseheredoc(void); 11690111SimpSTATIC int peektoken(void); 11790111SimpSTATIC int readtoken(void); 11890111SimpSTATIC int xxreadtoken(void); 11990111SimpSTATIC int readtoken1(int, char const *, char *, int); 12090111SimpSTATIC int noexpand(char *); 12190111SimpSTATIC void synexpect(int); 12290111SimpSTATIC void synerror(char *); 12390111SimpSTATIC void setprompt(int); 1241556Srgrimes 12517987Speter 1261556Srgrimes/* 1271556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1281556Srgrimes * valid parse tree indicating a blank line.) 1291556Srgrimes */ 1301556Srgrimes 1311556Srgrimesunion node * 13290111Simpparsecmd(int interact) 13317987Speter{ 1341556Srgrimes int t; 1351556Srgrimes 13660593Scracauer tokpushback = 0; 1371556Srgrimes doprompt = interact; 1381556Srgrimes if (doprompt) 1391556Srgrimes setprompt(1); 1401556Srgrimes else 1411556Srgrimes setprompt(0); 1421556Srgrimes needprompt = 0; 1431556Srgrimes t = readtoken(); 1441556Srgrimes if (t == TEOF) 1451556Srgrimes return NEOF; 1461556Srgrimes if (t == TNL) 1471556Srgrimes return NULL; 1481556Srgrimes tokpushback++; 1491556Srgrimes return list(1); 1501556Srgrimes} 1511556Srgrimes 1521556Srgrimes 1531556SrgrimesSTATIC union node * 15490111Simplist(int nlflag) 15517987Speter{ 1561556Srgrimes union node *n1, *n2, *n3; 15717987Speter int tok; 1581556Srgrimes 1591556Srgrimes checkkwd = 2; 1601556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 1611556Srgrimes return NULL; 16217987Speter n1 = NULL; 1631556Srgrimes for (;;) { 16417987Speter n2 = andor(); 16517987Speter tok = readtoken(); 16617987Speter if (tok == TBACKGND) { 16717987Speter if (n2->type == NCMD || n2->type == NPIPE) { 16817987Speter n2->ncmd.backgnd = 1; 16917987Speter } else if (n2->type == NREDIR) { 17017987Speter n2->type = NBACKGND; 17117987Speter } else { 17217987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 17317987Speter n3->type = NBACKGND; 17417987Speter n3->nredir.n = n2; 17517987Speter n3->nredir.redirect = NULL; 17617987Speter n2 = n3; 17717987Speter } 17817987Speter } 17917987Speter if (n1 == NULL) { 18017987Speter n1 = n2; 18117987Speter } 18217987Speter else { 18317987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 18417987Speter n3->type = NSEMI; 18517987Speter n3->nbinary.ch1 = n1; 18617987Speter n3->nbinary.ch2 = n2; 18717987Speter n1 = n3; 18817987Speter } 18917987Speter switch (tok) { 19013882Sjoerg case TBACKGND: 19117987Speter case TSEMI: 19217987Speter tok = readtoken(); 193102410Scharnier /* FALLTHROUGH */ 1941556Srgrimes case TNL: 19517987Speter if (tok == TNL) { 19617987Speter parseheredoc(); 19717987Speter if (nlflag) 19817987Speter return n1; 19917987Speter } else { 20017987Speter tokpushback++; 20117987Speter } 2021556Srgrimes checkkwd = 2; 2031556Srgrimes if (tokendlist[peektoken()]) 2041556Srgrimes return n1; 2051556Srgrimes break; 2061556Srgrimes case TEOF: 2071556Srgrimes if (heredoclist) 2081556Srgrimes parseheredoc(); 2091556Srgrimes else 2101556Srgrimes pungetc(); /* push back EOF on input */ 2111556Srgrimes return n1; 2121556Srgrimes default: 2131556Srgrimes if (nlflag) 2141556Srgrimes synexpect(-1); 2151556Srgrimes tokpushback++; 2161556Srgrimes return n1; 2171556Srgrimes } 2181556Srgrimes } 2191556Srgrimes} 2201556Srgrimes 2211556Srgrimes 2221556Srgrimes 2231556SrgrimesSTATIC union node * 22490111Simpandor(void) 22590111Simp{ 2261556Srgrimes union node *n1, *n2, *n3; 2271556Srgrimes int t; 2281556Srgrimes 2291556Srgrimes n1 = pipeline(); 2301556Srgrimes for (;;) { 2311556Srgrimes if ((t = readtoken()) == TAND) { 2321556Srgrimes t = NAND; 2331556Srgrimes } else if (t == TOR) { 2341556Srgrimes t = NOR; 2351556Srgrimes } else { 2361556Srgrimes tokpushback++; 2371556Srgrimes return n1; 2381556Srgrimes } 2391556Srgrimes n2 = pipeline(); 2401556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2411556Srgrimes n3->type = t; 2421556Srgrimes n3->nbinary.ch1 = n1; 2431556Srgrimes n3->nbinary.ch2 = n2; 2441556Srgrimes n1 = n3; 2451556Srgrimes } 2461556Srgrimes} 2471556Srgrimes 2481556Srgrimes 2491556Srgrimes 2501556SrgrimesSTATIC union node * 25190111Simppipeline(void) 25290111Simp{ 25375336Sbrian union node *n1, *n2, *pipenode; 2541556Srgrimes struct nodelist *lp, *prev; 25575336Sbrian int negate; 2561556Srgrimes 25775336Sbrian negate = 0; 2581556Srgrimes TRACE(("pipeline: entered\n")); 25975336Sbrian while (readtoken() == TNOT) 26075336Sbrian negate = !negate; 26175336Sbrian tokpushback++; 2621556Srgrimes n1 = command(); 2631556Srgrimes if (readtoken() == TPIPE) { 2641556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 2651556Srgrimes pipenode->type = NPIPE; 2661556Srgrimes pipenode->npipe.backgnd = 0; 2671556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2681556Srgrimes pipenode->npipe.cmdlist = lp; 2691556Srgrimes lp->n = n1; 2701556Srgrimes do { 2711556Srgrimes prev = lp; 2721556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2731556Srgrimes lp->n = command(); 2741556Srgrimes prev->next = lp; 2751556Srgrimes } while (readtoken() == TPIPE); 2761556Srgrimes lp->next = NULL; 2771556Srgrimes n1 = pipenode; 2781556Srgrimes } 2791556Srgrimes tokpushback++; 28075336Sbrian if (negate) { 28175336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 28275336Sbrian n2->type = NNOT; 28375336Sbrian n2->nnot.com = n1; 28475336Sbrian return n2; 28575336Sbrian } else 28675336Sbrian return n1; 2871556Srgrimes} 2881556Srgrimes 2891556Srgrimes 2901556Srgrimes 2911556SrgrimesSTATIC union node * 29290111Simpcommand(void) 29390111Simp{ 2941556Srgrimes union node *n1, *n2; 2951556Srgrimes union node *ap, **app; 2961556Srgrimes union node *cp, **cpp; 2971556Srgrimes union node *redir, **rpp; 29875160Sbrian int t, negate = 0; 2991556Srgrimes 3001556Srgrimes checkkwd = 2; 30117987Speter redir = NULL; 30217987Speter n1 = NULL; 3031556Srgrimes rpp = &redir; 30420425Ssteve 3051556Srgrimes /* Check for redirection which may precede command */ 3061556Srgrimes while (readtoken() == TREDIR) { 3071556Srgrimes *rpp = n2 = redirnode; 3081556Srgrimes rpp = &n2->nfile.next; 3091556Srgrimes parsefname(); 3101556Srgrimes } 3111556Srgrimes tokpushback++; 3121556Srgrimes 31375160Sbrian while (readtoken() == TNOT) { 31475160Sbrian TRACE(("command: TNOT recognized\n")); 31575160Sbrian negate = !negate; 31675160Sbrian } 31775160Sbrian tokpushback++; 31875160Sbrian 3191556Srgrimes switch (readtoken()) { 3201556Srgrimes case TIF: 3211556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3221556Srgrimes n1->type = NIF; 323104554Stjr if ((n1->nif.test = list(0)) == NULL) 324104554Stjr synexpect(-1); 3251556Srgrimes if (readtoken() != TTHEN) 3261556Srgrimes synexpect(TTHEN); 3271556Srgrimes n1->nif.ifpart = list(0); 3281556Srgrimes n2 = n1; 3291556Srgrimes while (readtoken() == TELIF) { 3301556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3311556Srgrimes n2 = n2->nif.elsepart; 3321556Srgrimes n2->type = NIF; 333104554Stjr if ((n2->nif.test = list(0)) == NULL) 334104554Stjr synexpect(-1); 3351556Srgrimes if (readtoken() != TTHEN) 3361556Srgrimes synexpect(TTHEN); 3371556Srgrimes n2->nif.ifpart = list(0); 3381556Srgrimes } 3391556Srgrimes if (lasttoken == TELSE) 3401556Srgrimes n2->nif.elsepart = list(0); 3411556Srgrimes else { 3421556Srgrimes n2->nif.elsepart = NULL; 3431556Srgrimes tokpushback++; 3441556Srgrimes } 3451556Srgrimes if (readtoken() != TFI) 3461556Srgrimes synexpect(TFI); 3471556Srgrimes checkkwd = 1; 3481556Srgrimes break; 3491556Srgrimes case TWHILE: 3501556Srgrimes case TUNTIL: { 3511556Srgrimes int got; 3521556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3531556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 354104554Stjr if ((n1->nbinary.ch1 = list(0)) == NULL) 355104554Stjr synexpect(-1); 3561556Srgrimes if ((got=readtoken()) != TDO) { 3571556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3581556Srgrimes synexpect(TDO); 3591556Srgrimes } 3601556Srgrimes n1->nbinary.ch2 = list(0); 3611556Srgrimes if (readtoken() != TDONE) 3621556Srgrimes synexpect(TDONE); 3631556Srgrimes checkkwd = 1; 3641556Srgrimes break; 3651556Srgrimes } 3661556Srgrimes case TFOR: 3671556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3681556Srgrimes synerror("Bad for loop variable"); 3691556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3701556Srgrimes n1->type = NFOR; 3711556Srgrimes n1->nfor.var = wordtext; 3721556Srgrimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3731556Srgrimes app = ≈ 3741556Srgrimes while (readtoken() == TWORD) { 3751556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3761556Srgrimes n2->type = NARG; 3771556Srgrimes n2->narg.text = wordtext; 3781556Srgrimes n2->narg.backquote = backquotelist; 3791556Srgrimes *app = n2; 3801556Srgrimes app = &n2->narg.next; 3811556Srgrimes } 3821556Srgrimes *app = NULL; 3831556Srgrimes n1->nfor.args = ap; 3841556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3851556Srgrimes synexpect(-1); 3861556Srgrimes } else { 3871556Srgrimes#ifndef GDB_HACK 3881556Srgrimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3891556Srgrimes '@', '=', '\0'}; 3901556Srgrimes#endif 3911556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3921556Srgrimes n2->type = NARG; 3931556Srgrimes n2->narg.text = (char *)argvars; 3941556Srgrimes n2->narg.backquote = NULL; 3951556Srgrimes n2->narg.next = NULL; 3961556Srgrimes n1->nfor.args = n2; 3971556Srgrimes /* 3981556Srgrimes * Newline or semicolon here is optional (but note 3991556Srgrimes * that the original Bourne shell only allowed NL). 4001556Srgrimes */ 4011556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4021556Srgrimes tokpushback++; 4031556Srgrimes } 4041556Srgrimes checkkwd = 2; 4051556Srgrimes if ((t = readtoken()) == TDO) 4061556Srgrimes t = TDONE; 4071556Srgrimes else if (t == TBEGIN) 4081556Srgrimes t = TEND; 4091556Srgrimes else 4101556Srgrimes synexpect(-1); 4111556Srgrimes n1->nfor.body = list(0); 4121556Srgrimes if (readtoken() != t) 4131556Srgrimes synexpect(t); 4141556Srgrimes checkkwd = 1; 4151556Srgrimes break; 4161556Srgrimes case TCASE: 4171556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4181556Srgrimes n1->type = NCASE; 4191556Srgrimes if (readtoken() != TWORD) 4201556Srgrimes synexpect(TWORD); 4211556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4221556Srgrimes n2->type = NARG; 4231556Srgrimes n2->narg.text = wordtext; 4241556Srgrimes n2->narg.backquote = backquotelist; 4251556Srgrimes n2->narg.next = NULL; 4261556Srgrimes while (readtoken() == TNL); 4271556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4281556Srgrimes synerror("expecting \"in\""); 4291556Srgrimes cpp = &n1->ncase.cases; 43018018Speter noaliases = 1; /* turn off alias expansion */ 4312760Ssef checkkwd = 2, readtoken(); 432104202Stjr while (lasttoken != TESAC) { 4331556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4341556Srgrimes cp->type = NCLIST; 4351556Srgrimes app = &cp->nclist.pattern; 436104207Stjr if (lasttoken == TLP) 437104207Stjr readtoken(); 4381556Srgrimes for (;;) { 4391556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4401556Srgrimes ap->type = NARG; 4411556Srgrimes ap->narg.text = wordtext; 4421556Srgrimes ap->narg.backquote = backquotelist; 4432760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4441556Srgrimes break; 4451556Srgrimes app = &ap->narg.next; 4462760Ssef readtoken(); 4471556Srgrimes } 4481556Srgrimes ap->narg.next = NULL; 4491556Srgrimes if (lasttoken != TRP) 45018018Speter noaliases = 0, synexpect(TRP); 4511556Srgrimes cp->nclist.body = list(0); 4522760Ssef 4532760Ssef checkkwd = 2; 4542760Ssef if ((t = readtoken()) != TESAC) { 4552760Ssef if (t != TENDCASE) 45618018Speter noaliases = 0, synexpect(TENDCASE); 4572760Ssef else 4582760Ssef checkkwd = 2, readtoken(); 4592760Ssef } 4601556Srgrimes cpp = &cp->nclist.next; 461104202Stjr } 46218018Speter noaliases = 0; /* reset alias expansion */ 4631556Srgrimes *cpp = NULL; 4641556Srgrimes checkkwd = 1; 4651556Srgrimes break; 4661556Srgrimes case TLP: 4671556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4681556Srgrimes n1->type = NSUBSHELL; 4691556Srgrimes n1->nredir.n = list(0); 4701556Srgrimes n1->nredir.redirect = NULL; 4711556Srgrimes if (readtoken() != TRP) 4721556Srgrimes synexpect(TRP); 4731556Srgrimes checkkwd = 1; 4741556Srgrimes break; 4751556Srgrimes case TBEGIN: 4761556Srgrimes n1 = list(0); 4771556Srgrimes if (readtoken() != TEND) 4781556Srgrimes synexpect(TEND); 4791556Srgrimes checkkwd = 1; 4801556Srgrimes break; 4811556Srgrimes /* Handle an empty command like other simple commands. */ 48217987Speter case TSEMI: 483101662Stjr case TAND: 484101662Stjr case TOR: 48517987Speter /* 48617987Speter * An empty command before a ; doesn't make much sense, and 48717987Speter * should certainly be disallowed in the case of `if ;'. 48817987Speter */ 48917987Speter if (!redir) 49017987Speter synexpect(-1); 4911556Srgrimes case TNL: 49210399Sjoerg case TEOF: 4931556Srgrimes case TWORD: 49417987Speter case TRP: 4951556Srgrimes tokpushback++; 49675160Sbrian n1 = simplecmd(rpp, redir); 49775160Sbrian goto checkneg; 4981556Srgrimes default: 4991556Srgrimes synexpect(-1); 5001556Srgrimes } 5011556Srgrimes 5021556Srgrimes /* Now check for redirection which may follow command */ 5031556Srgrimes while (readtoken() == TREDIR) { 5041556Srgrimes *rpp = n2 = redirnode; 5051556Srgrimes rpp = &n2->nfile.next; 5061556Srgrimes parsefname(); 5071556Srgrimes } 5081556Srgrimes tokpushback++; 5091556Srgrimes *rpp = NULL; 5101556Srgrimes if (redir) { 5111556Srgrimes if (n1->type != NSUBSHELL) { 5121556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5131556Srgrimes n2->type = NREDIR; 5141556Srgrimes n2->nredir.n = n1; 5151556Srgrimes n1 = n2; 5161556Srgrimes } 5171556Srgrimes n1->nredir.redirect = redir; 5181556Srgrimes } 51975160Sbrian 52075160Sbriancheckneg: 52175160Sbrian if (negate) { 52275160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 52375160Sbrian n2->type = NNOT; 52475160Sbrian n2->nnot.com = n1; 52575160Sbrian return n2; 52675160Sbrian } 52775160Sbrian else 52875160Sbrian return n1; 5291556Srgrimes} 5301556Srgrimes 5311556Srgrimes 5321556SrgrimesSTATIC union node * 53390111Simpsimplecmd(union node **rpp, union node *redir) 53490111Simp{ 5351556Srgrimes union node *args, **app; 5361556Srgrimes union node **orig_rpp = rpp; 53775160Sbrian union node *n = NULL, *n2; 53875160Sbrian int negate = 0; 5391556Srgrimes 5401556Srgrimes /* If we don't have any redirections already, then we must reset */ 5411556Srgrimes /* rpp to be the address of the local redir variable. */ 5421556Srgrimes if (redir == 0) 5431556Srgrimes rpp = &redir; 5441556Srgrimes 5451556Srgrimes args = NULL; 5461556Srgrimes app = &args; 5478855Srgrimes /* 5481556Srgrimes * We save the incoming value, because we need this for shell 5491556Srgrimes * functions. There can not be a redirect or an argument between 5508855Srgrimes * the function name and the open parenthesis. 5511556Srgrimes */ 5521556Srgrimes orig_rpp = rpp; 5531556Srgrimes 55475160Sbrian while (readtoken() == TNOT) { 55575160Sbrian TRACE(("command: TNOT recognized\n")); 55675160Sbrian negate = !negate; 55775160Sbrian } 55875160Sbrian tokpushback++; 55975160Sbrian 5601556Srgrimes for (;;) { 5611556Srgrimes if (readtoken() == TWORD) { 5621556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5631556Srgrimes n->type = NARG; 5641556Srgrimes n->narg.text = wordtext; 5651556Srgrimes n->narg.backquote = backquotelist; 5661556Srgrimes *app = n; 5671556Srgrimes app = &n->narg.next; 5681556Srgrimes } else if (lasttoken == TREDIR) { 5691556Srgrimes *rpp = n = redirnode; 5701556Srgrimes rpp = &n->nfile.next; 5711556Srgrimes parsefname(); /* read name of redirection file */ 5721556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5731556Srgrimes && rpp == orig_rpp) { 5741556Srgrimes /* We have a function */ 5751556Srgrimes if (readtoken() != TRP) 5761556Srgrimes synexpect(TRP); 5771556Srgrimes#ifdef notdef 5781556Srgrimes if (! goodname(n->narg.text)) 5791556Srgrimes synerror("Bad function name"); 5801556Srgrimes#endif 5811556Srgrimes n->type = NDEFUN; 5821556Srgrimes n->narg.next = command(); 58375160Sbrian goto checkneg; 5841556Srgrimes } else { 5851556Srgrimes tokpushback++; 5861556Srgrimes break; 5871556Srgrimes } 5881556Srgrimes } 5891556Srgrimes *app = NULL; 5901556Srgrimes *rpp = NULL; 5911556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5921556Srgrimes n->type = NCMD; 5931556Srgrimes n->ncmd.backgnd = 0; 5941556Srgrimes n->ncmd.args = args; 5951556Srgrimes n->ncmd.redirect = redir; 59675160Sbrian 59775160Sbriancheckneg: 59875160Sbrian if (negate) { 59975160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 60075160Sbrian n2->type = NNOT; 60175160Sbrian n2->nnot.com = n; 60275160Sbrian return n2; 60375160Sbrian } 60475160Sbrian else 60575160Sbrian return n; 6061556Srgrimes} 6071556Srgrimes 60817987SpeterSTATIC union node * 60990111Simpmakename(void) 61090111Simp{ 61117987Speter union node *n; 6121556Srgrimes 61317987Speter n = (union node *)stalloc(sizeof (struct narg)); 61417987Speter n->type = NARG; 61517987Speter n->narg.next = NULL; 61617987Speter n->narg.text = wordtext; 61717987Speter n->narg.backquote = backquotelist; 61817987Speter return n; 61917987Speter} 62017987Speter 62190111Simpvoid fixredir(union node *n, const char *text, int err) 62290111Simp{ 62317987Speter TRACE(("Fix redir %s %d\n", text, err)); 62417987Speter if (!err) 62517987Speter n->ndup.vname = NULL; 62617987Speter 62717987Speter if (is_digit(text[0]) && text[1] == '\0') 62817987Speter n->ndup.dupfd = digit_val(text[0]); 62917987Speter else if (text[0] == '-' && text[1] == '\0') 63017987Speter n->ndup.dupfd = -1; 63117987Speter else { 63220425Ssteve 63317987Speter if (err) 63417987Speter synerror("Bad fd number"); 63517987Speter else 63617987Speter n->ndup.vname = makename(); 63717987Speter } 63817987Speter} 63917987Speter 64017987Speter 6411556SrgrimesSTATIC void 64290111Simpparsefname(void) 64390111Simp{ 6441556Srgrimes union node *n = redirnode; 6451556Srgrimes 6461556Srgrimes if (readtoken() != TWORD) 6471556Srgrimes synexpect(-1); 6481556Srgrimes if (n->type == NHERE) { 6491556Srgrimes struct heredoc *here = heredoc; 6501556Srgrimes struct heredoc *p; 6511556Srgrimes int i; 6521556Srgrimes 6531556Srgrimes if (quoteflag == 0) 6541556Srgrimes n->type = NXHERE; 6551556Srgrimes TRACE(("Here document %d\n", n->type)); 6561556Srgrimes if (here->striptabs) { 6571556Srgrimes while (*wordtext == '\t') 6581556Srgrimes wordtext++; 6591556Srgrimes } 6601556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6611556Srgrimes synerror("Illegal eof marker for << redirection"); 6621556Srgrimes rmescapes(wordtext); 6631556Srgrimes here->eofmark = wordtext; 6641556Srgrimes here->next = NULL; 6651556Srgrimes if (heredoclist == NULL) 6661556Srgrimes heredoclist = here; 6671556Srgrimes else { 6681556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6691556Srgrimes p->next = here; 6701556Srgrimes } 6711556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 67217987Speter fixredir(n, wordtext, 0); 6731556Srgrimes } else { 67417987Speter n->nfile.fname = makename(); 6751556Srgrimes } 6761556Srgrimes} 6771556Srgrimes 6781556Srgrimes 6791556Srgrimes/* 6801556Srgrimes * Input any here documents. 6811556Srgrimes */ 6821556Srgrimes 6831556SrgrimesSTATIC void 68490111Simpparseheredoc(void) 68590111Simp{ 6861556Srgrimes struct heredoc *here; 6871556Srgrimes union node *n; 6881556Srgrimes 6891556Srgrimes while (heredoclist) { 6901556Srgrimes here = heredoclist; 6911556Srgrimes heredoclist = here->next; 6921556Srgrimes if (needprompt) { 6931556Srgrimes setprompt(2); 6941556Srgrimes needprompt = 0; 6951556Srgrimes } 6961556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6971556Srgrimes here->eofmark, here->striptabs); 6981556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6991556Srgrimes n->narg.type = NARG; 7001556Srgrimes n->narg.next = NULL; 7011556Srgrimes n->narg.text = wordtext; 7021556Srgrimes n->narg.backquote = backquotelist; 7031556Srgrimes here->here->nhere.doc = n; 7041556Srgrimes } 7051556Srgrimes} 7061556Srgrimes 7071556SrgrimesSTATIC int 70890111Simppeektoken(void) 70990111Simp{ 7101556Srgrimes int t; 7111556Srgrimes 7121556Srgrimes t = readtoken(); 7131556Srgrimes tokpushback++; 7141556Srgrimes return (t); 7151556Srgrimes} 7161556Srgrimes 7171556SrgrimesSTATIC int 71890111Simpreadtoken(void) 71990111Simp{ 7201556Srgrimes int t; 7211556Srgrimes int savecheckkwd = checkkwd; 7221556Srgrimes struct alias *ap; 7231556Srgrimes#ifdef DEBUG 7241556Srgrimes int alreadyseen = tokpushback; 7251556Srgrimes#endif 7268855Srgrimes 7271556Srgrimes top: 7281556Srgrimes t = xxreadtoken(); 7291556Srgrimes 7301556Srgrimes if (checkkwd) { 7311556Srgrimes /* 7321556Srgrimes * eat newlines 7331556Srgrimes */ 7341556Srgrimes if (checkkwd == 2) { 7351556Srgrimes checkkwd = 0; 7361556Srgrimes while (t == TNL) { 7371556Srgrimes parseheredoc(); 7381556Srgrimes t = xxreadtoken(); 7391556Srgrimes } 7401556Srgrimes } else 7411556Srgrimes checkkwd = 0; 7421556Srgrimes /* 7431556Srgrimes * check for keywords and aliases 7441556Srgrimes */ 74520425Ssteve if (t == TWORD && !quoteflag) 74617987Speter { 74798463Sjmallett const char * const *pp; 7481556Srgrimes 74998463Sjmallett for (pp = parsekwd; *pp; pp++) { 75020425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 75117987Speter { 7521556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7531556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7541556Srgrimes goto out; 7551556Srgrimes } 7561556Srgrimes } 75718018Speter if (noaliases == 0 && 75818018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7591556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7601556Srgrimes checkkwd = savecheckkwd; 7611556Srgrimes goto top; 7621556Srgrimes } 7631556Srgrimes } 7641556Srgrimesout: 76575160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 7661556Srgrimes } 7671556Srgrimes#ifdef DEBUG 7681556Srgrimes if (!alreadyseen) 7691556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7701556Srgrimes else 7711556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7721556Srgrimes#endif 7731556Srgrimes return (t); 7741556Srgrimes} 7751556Srgrimes 7761556Srgrimes 7771556Srgrimes/* 7781556Srgrimes * Read the next input token. 7791556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7801556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7811556Srgrimes * quoted. 7821556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7831556Srgrimes * the redirection. 7841556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7851556Srgrimes * on which the token starts. 7861556Srgrimes * 7871556Srgrimes * [Change comment: here documents and internal procedures] 7881556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7891556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7901556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7911556Srgrimes * We could also make parseoperator in essence the main routine, and 7921556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7931556Srgrimes */ 7941556Srgrimes 7951556Srgrimes#define RETURN(token) return lasttoken = token 7961556Srgrimes 7971556SrgrimesSTATIC int 79890111Simpxxreadtoken(void) 79990111Simp{ 80025230Ssteve int c; 8011556Srgrimes 8021556Srgrimes if (tokpushback) { 8031556Srgrimes tokpushback = 0; 8041556Srgrimes return lasttoken; 8051556Srgrimes } 8061556Srgrimes if (needprompt) { 8071556Srgrimes setprompt(2); 8081556Srgrimes needprompt = 0; 8091556Srgrimes } 8101556Srgrimes startlinno = plinno; 8111556Srgrimes for (;;) { /* until token or start of word found */ 8121556Srgrimes c = pgetc_macro(); 8131556Srgrimes if (c == ' ' || c == '\t') 8141556Srgrimes continue; /* quick check for white space first */ 8151556Srgrimes switch (c) { 8161556Srgrimes case ' ': case '\t': 8171556Srgrimes continue; 8181556Srgrimes case '#': 8191556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8201556Srgrimes pungetc(); 8211556Srgrimes continue; 8221556Srgrimes case '\\': 8231556Srgrimes if (pgetc() == '\n') { 8241556Srgrimes startlinno = ++plinno; 8251556Srgrimes if (doprompt) 8261556Srgrimes setprompt(2); 8271556Srgrimes else 8281556Srgrimes setprompt(0); 8291556Srgrimes continue; 8301556Srgrimes } 8311556Srgrimes pungetc(); 8321556Srgrimes goto breakloop; 8331556Srgrimes case '\n': 8341556Srgrimes plinno++; 8351556Srgrimes needprompt = doprompt; 8361556Srgrimes RETURN(TNL); 8371556Srgrimes case PEOF: 8381556Srgrimes RETURN(TEOF); 8391556Srgrimes case '&': 8401556Srgrimes if (pgetc() == '&') 8411556Srgrimes RETURN(TAND); 8421556Srgrimes pungetc(); 8431556Srgrimes RETURN(TBACKGND); 8441556Srgrimes case '|': 8451556Srgrimes if (pgetc() == '|') 8461556Srgrimes RETURN(TOR); 8471556Srgrimes pungetc(); 8481556Srgrimes RETURN(TPIPE); 8491556Srgrimes case ';': 8501556Srgrimes if (pgetc() == ';') 8511556Srgrimes RETURN(TENDCASE); 8521556Srgrimes pungetc(); 8531556Srgrimes RETURN(TSEMI); 8541556Srgrimes case '(': 8551556Srgrimes RETURN(TLP); 8561556Srgrimes case ')': 8571556Srgrimes RETURN(TRP); 8581556Srgrimes default: 8591556Srgrimes goto breakloop; 8601556Srgrimes } 8611556Srgrimes } 8621556Srgrimesbreakloop: 8631556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8641556Srgrimes#undef RETURN 8651556Srgrimes} 8661556Srgrimes 8671556Srgrimes 8681556Srgrimes 8691556Srgrimes/* 8701556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8711556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8721556Srgrimes * word which marks the end of the document and striptabs is true if 8731556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8741556Srgrimes * is the first character of the input token or document. 8751556Srgrimes * 8761556Srgrimes * Because C does not have internal subroutines, I have simulated them 8771556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8781556Srgrimes * will run code that appears at the end of readtoken1. 8791556Srgrimes */ 8801556Srgrimes 8811556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8821556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8831556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8841556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8851556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8861556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8871556Srgrimes 8881556SrgrimesSTATIC int 88990111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) 89090111Simp{ 89117987Speter int c = firstc; 89217987Speter char *out; 8931556Srgrimes int len; 8941556Srgrimes char line[EOFMARKLEN + 1]; 8951556Srgrimes struct nodelist *bqlist; 8961556Srgrimes int quotef; 8971556Srgrimes int dblquote; 8981556Srgrimes int varnest; /* levels of variables expansion */ 8991556Srgrimes int arinest; /* levels of arithmetic expansion */ 9001556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 9011556Srgrimes int oldstyle; 9021556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 90354679Scracauer int synentry; 90417987Speter#if __GNUC__ 90517987Speter /* Avoid longjmp clobbering */ 90617987Speter (void) &out; 90717987Speter (void) "ef; 90817987Speter (void) &dblquote; 90917987Speter (void) &varnest; 91017987Speter (void) &arinest; 91117987Speter (void) &parenlevel; 91217987Speter (void) &oldstyle; 91317987Speter (void) &prevsyntax; 91417987Speter (void) &syntax; 91554679Scracauer (void) &synentry; 91617987Speter#endif 9171556Srgrimes 9181556Srgrimes startlinno = plinno; 9191556Srgrimes dblquote = 0; 9201556Srgrimes if (syntax == DQSYNTAX) 9211556Srgrimes dblquote = 1; 9221556Srgrimes quotef = 0; 9231556Srgrimes bqlist = NULL; 9241556Srgrimes varnest = 0; 9251556Srgrimes arinest = 0; 9261556Srgrimes parenlevel = 0; 9271556Srgrimes 9281556Srgrimes STARTSTACKSTR(out); 9291556Srgrimes loop: { /* for each line, until end of word */ 9301556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9311556Srgrimes for (;;) { /* until end of line or end of word */ 9321556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 93354679Scracauer 93464705Scracauer synentry = syntax[c]; 93554679Scracauer 93654679Scracauer switch(synentry) { 9371556Srgrimes case CNL: /* '\n' */ 9381556Srgrimes if (syntax == BASESYNTAX) 9391556Srgrimes goto endword; /* exit outer loop */ 9401556Srgrimes USTPUTC(c, out); 9411556Srgrimes plinno++; 9421556Srgrimes if (doprompt) 9431556Srgrimes setprompt(2); 9441556Srgrimes else 9451556Srgrimes setprompt(0); 9461556Srgrimes c = pgetc(); 9471556Srgrimes goto loop; /* continue outer loop */ 9481556Srgrimes case CWORD: 9491556Srgrimes USTPUTC(c, out); 9501556Srgrimes break; 9511556Srgrimes case CCTL: 9521556Srgrimes if (eofmark == NULL || dblquote) 9531556Srgrimes USTPUTC(CTLESC, out); 9541556Srgrimes USTPUTC(c, out); 9551556Srgrimes break; 9561556Srgrimes case CBACK: /* backslash */ 9571556Srgrimes c = pgetc(); 9581556Srgrimes if (c == PEOF) { 9591556Srgrimes USTPUTC('\\', out); 9601556Srgrimes pungetc(); 9611556Srgrimes } else if (c == '\n') { 9621556Srgrimes if (doprompt) 9631556Srgrimes setprompt(2); 9641556Srgrimes else 9651556Srgrimes setprompt(0); 9661556Srgrimes } else { 96754631Scracauer if (dblquote && c != '\\' && 96854631Scracauer c != '`' && c != '$' && 96954631Scracauer (c != '"' || eofmark != NULL)) 9701556Srgrimes USTPUTC('\\', out); 97183675Stegge if (SQSYNTAX[c] == CCTL) 9721556Srgrimes USTPUTC(CTLESC, out); 97339137Stegge else if (eofmark == NULL) 97438887Stegge USTPUTC(CTLQUOTEMARK, out); 9751556Srgrimes USTPUTC(c, out); 9761556Srgrimes quotef++; 9771556Srgrimes } 9781556Srgrimes break; 9791556Srgrimes case CSQUOTE: 98039137Stegge if (eofmark == NULL) 98139137Stegge USTPUTC(CTLQUOTEMARK, out); 9821556Srgrimes syntax = SQSYNTAX; 9831556Srgrimes break; 9841556Srgrimes case CDQUOTE: 98539137Stegge if (eofmark == NULL) 98639137Stegge USTPUTC(CTLQUOTEMARK, out); 9871556Srgrimes syntax = DQSYNTAX; 9881556Srgrimes dblquote = 1; 9891556Srgrimes break; 9901556Srgrimes case CENDQUOTE: 99139137Stegge if (eofmark != NULL && arinest == 0 && 99239137Stegge varnest == 0) { 9931556Srgrimes USTPUTC(c, out); 9941556Srgrimes } else { 99539137Stegge if (arinest) { 9961556Srgrimes syntax = ARISYNTAX; 99739137Stegge dblquote = 0; 99839137Stegge } else if (eofmark == NULL) { 9991556Srgrimes syntax = BASESYNTAX; 100039137Stegge dblquote = 0; 100139137Stegge } 10021556Srgrimes quotef++; 10031556Srgrimes } 10041556Srgrimes break; 10051556Srgrimes case CVAR: /* '$' */ 10061556Srgrimes PARSESUB(); /* parse substitution */ 10071556Srgrimes break; 10081556Srgrimes case CENDVAR: /* '}' */ 10091556Srgrimes if (varnest > 0) { 10101556Srgrimes varnest--; 10111556Srgrimes USTPUTC(CTLENDVAR, out); 10121556Srgrimes } else { 10131556Srgrimes USTPUTC(c, out); 10141556Srgrimes } 10151556Srgrimes break; 10161556Srgrimes case CLP: /* '(' in arithmetic */ 10171556Srgrimes parenlevel++; 10181556Srgrimes USTPUTC(c, out); 10191556Srgrimes break; 10201556Srgrimes case CRP: /* ')' in arithmetic */ 10211556Srgrimes if (parenlevel > 0) { 10221556Srgrimes USTPUTC(c, out); 10231556Srgrimes --parenlevel; 10241556Srgrimes } else { 10251556Srgrimes if (pgetc() == ')') { 10261556Srgrimes if (--arinest == 0) { 10271556Srgrimes USTPUTC(CTLENDARI, out); 10281556Srgrimes syntax = prevsyntax; 102939137Stegge if (syntax == DQSYNTAX) 103039137Stegge dblquote = 1; 103139137Stegge else 103239137Stegge dblquote = 0; 10331556Srgrimes } else 10341556Srgrimes USTPUTC(')', out); 10351556Srgrimes } else { 10368855Srgrimes /* 10371556Srgrimes * unbalanced parens 10381556Srgrimes * (don't 2nd guess - no error) 10391556Srgrimes */ 10401556Srgrimes pungetc(); 10411556Srgrimes USTPUTC(')', out); 10421556Srgrimes } 10431556Srgrimes } 10441556Srgrimes break; 10451556Srgrimes case CBQUOTE: /* '`' */ 10461556Srgrimes PARSEBACKQOLD(); 10471556Srgrimes break; 10481556Srgrimes case CEOF: 10491556Srgrimes goto endword; /* exit outer loop */ 10501556Srgrimes default: 10511556Srgrimes if (varnest == 0) 10521556Srgrimes goto endword; /* exit outer loop */ 10531556Srgrimes USTPUTC(c, out); 10541556Srgrimes } 10551556Srgrimes c = pgetc_macro(); 10561556Srgrimes } 10571556Srgrimes } 10581556Srgrimesendword: 10591556Srgrimes if (syntax == ARISYNTAX) 10601556Srgrimes synerror("Missing '))'"); 10611556Srgrimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10621556Srgrimes synerror("Unterminated quoted string"); 10631556Srgrimes if (varnest != 0) { 10641556Srgrimes startlinno = plinno; 10651556Srgrimes synerror("Missing '}'"); 10661556Srgrimes } 10671556Srgrimes USTPUTC('\0', out); 10681556Srgrimes len = out - stackblock(); 10691556Srgrimes out = stackblock(); 10701556Srgrimes if (eofmark == NULL) { 10711556Srgrimes if ((c == '>' || c == '<') 10721556Srgrimes && quotef == 0 10731556Srgrimes && len <= 2 10741556Srgrimes && (*out == '\0' || is_digit(*out))) { 10751556Srgrimes PARSEREDIR(); 10761556Srgrimes return lasttoken = TREDIR; 10771556Srgrimes } else { 10781556Srgrimes pungetc(); 10791556Srgrimes } 10801556Srgrimes } 10811556Srgrimes quoteflag = quotef; 10821556Srgrimes backquotelist = bqlist; 10831556Srgrimes grabstackblock(len); 10841556Srgrimes wordtext = out; 10851556Srgrimes return lasttoken = TWORD; 10861556Srgrimes/* end of readtoken routine */ 10871556Srgrimes 10881556Srgrimes 10891556Srgrimes 10901556Srgrimes/* 10911556Srgrimes * Check to see whether we are at the end of the here document. When this 10921556Srgrimes * is called, c is set to the first character of the next input line. If 10931556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 10941556Srgrimes */ 10951556Srgrimes 10961556Srgrimescheckend: { 10971556Srgrimes if (eofmark) { 10981556Srgrimes if (striptabs) { 10991556Srgrimes while (c == '\t') 11001556Srgrimes c = pgetc(); 11011556Srgrimes } 11021556Srgrimes if (c == *eofmark) { 11031556Srgrimes if (pfgets(line, sizeof line) != NULL) { 110425230Ssteve char *p, *q; 11051556Srgrimes 11061556Srgrimes p = line; 11071556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 11081556Srgrimes if (*p == '\n' && *q == '\0') { 11091556Srgrimes c = PEOF; 11101556Srgrimes plinno++; 11111556Srgrimes needprompt = doprompt; 11121556Srgrimes } else { 11131556Srgrimes pushstring(line, strlen(line), NULL); 11141556Srgrimes } 11151556Srgrimes } 11161556Srgrimes } 11171556Srgrimes } 11181556Srgrimes goto checkend_return; 11191556Srgrimes} 11201556Srgrimes 11211556Srgrimes 11221556Srgrimes/* 11231556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11241556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11251556Srgrimes * first character of the redirection operator. 11261556Srgrimes */ 11271556Srgrimes 11281556Srgrimesparseredir: { 11291556Srgrimes char fd = *out; 11301556Srgrimes union node *np; 11311556Srgrimes 11321556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11331556Srgrimes if (c == '>') { 11341556Srgrimes np->nfile.fd = 1; 11351556Srgrimes c = pgetc(); 11361556Srgrimes if (c == '>') 11371556Srgrimes np->type = NAPPEND; 11381556Srgrimes else if (c == '&') 11391556Srgrimes np->type = NTOFD; 114096922Stjr else if (c == '|') 114196922Stjr np->type = NCLOBBER; 11421556Srgrimes else { 11431556Srgrimes np->type = NTO; 11441556Srgrimes pungetc(); 11451556Srgrimes } 11461556Srgrimes } else { /* c == '<' */ 11471556Srgrimes np->nfile.fd = 0; 11481556Srgrimes c = pgetc(); 11491556Srgrimes if (c == '<') { 11501556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11511556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11521556Srgrimes np->nfile.fd = 0; 11531556Srgrimes } 11541556Srgrimes np->type = NHERE; 11551556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11561556Srgrimes heredoc->here = np; 11571556Srgrimes if ((c = pgetc()) == '-') { 11581556Srgrimes heredoc->striptabs = 1; 11591556Srgrimes } else { 11601556Srgrimes heredoc->striptabs = 0; 11611556Srgrimes pungetc(); 11621556Srgrimes } 11631556Srgrimes } else if (c == '&') 11641556Srgrimes np->type = NFROMFD; 116566612Sbrian else if (c == '>') 116666612Sbrian np->type = NFROMTO; 11671556Srgrimes else { 11681556Srgrimes np->type = NFROM; 11691556Srgrimes pungetc(); 11701556Srgrimes } 11711556Srgrimes } 11721556Srgrimes if (fd != '\0') 11731556Srgrimes np->nfile.fd = digit_val(fd); 11741556Srgrimes redirnode = np; 11751556Srgrimes goto parseredir_return; 11761556Srgrimes} 11771556Srgrimes 11781556Srgrimes 11791556Srgrimes/* 11801556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11811556Srgrimes * and nothing else. 11821556Srgrimes */ 11831556Srgrimes 11841556Srgrimesparsesub: { 11851556Srgrimes int subtype; 11861556Srgrimes int typeloc; 11871556Srgrimes int flags; 11881556Srgrimes char *p; 11891556Srgrimes#ifndef GDB_HACK 11901556Srgrimes static const char types[] = "}-+?="; 11911556Srgrimes#endif 119218202Speter int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11931556Srgrimes 11941556Srgrimes c = pgetc(); 1195149026Sstefanf if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && 1196149026Sstefanf !is_special(c)) { 11971556Srgrimes USTPUTC('$', out); 11981556Srgrimes pungetc(); 11991556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 12001556Srgrimes if (pgetc() == '(') { 12011556Srgrimes PARSEARITH(); 12021556Srgrimes } else { 12031556Srgrimes pungetc(); 12041556Srgrimes PARSEBACKQNEW(); 12051556Srgrimes } 12061556Srgrimes } else { 12071556Srgrimes USTPUTC(CTLVAR, out); 12081556Srgrimes typeloc = out - stackblock(); 12091556Srgrimes USTPUTC(VSNORMAL, out); 12101556Srgrimes subtype = VSNORMAL; 12111556Srgrimes if (c == '{') { 121218202Speter bracketed_name = 1; 12131556Srgrimes c = pgetc(); 121417987Speter if (c == '#') { 121517987Speter if ((c = pgetc()) == '}') 121617987Speter c = '#'; 121717987Speter else 121817987Speter subtype = VSLENGTH; 121917987Speter } 122017987Speter else 122117987Speter subtype = 0; 12221556Srgrimes } 1223149026Sstefanf if (!is_eof(c) && is_name(c)) { 12241556Srgrimes do { 12251556Srgrimes STPUTC(c, out); 12261556Srgrimes c = pgetc(); 1227149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 122818202Speter } else if (is_digit(c)) { 122918202Speter if (bracketed_name) { 123018202Speter do { 123118202Speter STPUTC(c, out); 123218202Speter c = pgetc(); 123318202Speter } while (is_digit(c)); 123418202Speter } else { 123518202Speter STPUTC(c, out); 123618202Speter c = pgetc(); 123718202Speter } 12381556Srgrimes } else { 12391556Srgrimes if (! is_special(c)) 12401556Srgrimesbadsub: synerror("Bad substitution"); 12411556Srgrimes USTPUTC(c, out); 12421556Srgrimes c = pgetc(); 12431556Srgrimes } 12441556Srgrimes STPUTC('=', out); 12451556Srgrimes flags = 0; 12461556Srgrimes if (subtype == 0) { 124717987Speter switch (c) { 124817987Speter case ':': 12491556Srgrimes flags = VSNUL; 12501556Srgrimes c = pgetc(); 125117987Speter /*FALLTHROUGH*/ 125217987Speter default: 125317987Speter p = strchr(types, c); 125417987Speter if (p == NULL) 125517987Speter goto badsub; 125617987Speter subtype = p - types + VSNORMAL; 125717987Speter break; 125817987Speter case '%': 125920425Ssteve case '#': 126017987Speter { 126117987Speter int cc = c; 126217987Speter subtype = c == '#' ? VSTRIMLEFT : 126317987Speter VSTRIMRIGHT; 126417987Speter c = pgetc(); 126517987Speter if (c == cc) 126617987Speter subtype++; 126717987Speter else 126817987Speter pungetc(); 126917987Speter break; 127017987Speter } 12711556Srgrimes } 12721556Srgrimes } else { 12731556Srgrimes pungetc(); 12741556Srgrimes } 127557225Scracauer if (subtype != VSLENGTH && (dblquote || arinest)) 12761556Srgrimes flags |= VSQUOTE; 12771556Srgrimes *(stackblock() + typeloc) = subtype | flags; 12781556Srgrimes if (subtype != VSNORMAL) 12791556Srgrimes varnest++; 12801556Srgrimes } 12811556Srgrimes goto parsesub_return; 12821556Srgrimes} 12831556Srgrimes 12841556Srgrimes 12851556Srgrimes/* 12861556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 12871556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12881556Srgrimes * list of commands (passed by reference), and savelen is the number of 12891556Srgrimes * characters on the top of the stack which must be preserved. 12901556Srgrimes */ 12911556Srgrimes 12921556Srgrimesparsebackq: { 12931556Srgrimes struct nodelist **nlpp; 12941556Srgrimes int savepbq; 12951556Srgrimes union node *n; 12961556Srgrimes char *volatile str; 12971556Srgrimes struct jmploc jmploc; 12981556Srgrimes struct jmploc *volatile savehandler; 12991556Srgrimes int savelen; 130020425Ssteve int saveprompt; 130120425Ssteve#if __GNUC__ 130220425Ssteve /* Avoid longjmp clobbering */ 130320425Ssteve (void) &saveprompt; 130420425Ssteve#endif 13051556Srgrimes 13061556Srgrimes savepbq = parsebackquote; 13071556Srgrimes if (setjmp(jmploc.loc)) { 13081556Srgrimes if (str) 13091556Srgrimes ckfree(str); 13101556Srgrimes parsebackquote = 0; 13111556Srgrimes handler = savehandler; 13121556Srgrimes longjmp(handler->loc, 1); 13131556Srgrimes } 13141556Srgrimes INTOFF; 13151556Srgrimes str = NULL; 13161556Srgrimes savelen = out - stackblock(); 13171556Srgrimes if (savelen > 0) { 13181556Srgrimes str = ckmalloc(savelen); 131917987Speter memcpy(str, stackblock(), savelen); 13201556Srgrimes } 13211556Srgrimes savehandler = handler; 13221556Srgrimes handler = &jmploc; 13231556Srgrimes INTON; 13241556Srgrimes if (oldstyle) { 13251556Srgrimes /* We must read until the closing backquote, giving special 13261556Srgrimes treatment to some slashes, and then push the string and 13271556Srgrimes reread it as input, interpreting it normally. */ 132825230Ssteve char *out; 132925230Ssteve int c; 13301556Srgrimes int savelen; 13311556Srgrimes char *str; 13328855Srgrimes 133320425Ssteve 13341556Srgrimes STARTSTACKSTR(out); 133520425Ssteve for (;;) { 133620425Ssteve if (needprompt) { 133720425Ssteve setprompt(2); 133820425Ssteve needprompt = 0; 133920425Ssteve } 134020425Ssteve switch (c = pgetc()) { 134120425Ssteve case '`': 134220425Ssteve goto done; 134320425Ssteve 134420425Ssteve case '\\': 134520425Ssteve if ((c = pgetc()) == '\n') { 134620425Ssteve plinno++; 134720425Ssteve if (doprompt) 134820425Ssteve setprompt(2); 134920425Ssteve else 135020425Ssteve setprompt(0); 135120425Ssteve /* 135220425Ssteve * If eating a newline, avoid putting 135320425Ssteve * the newline into the new character 135420425Ssteve * stream (via the STPUTC after the 135520425Ssteve * switch). 135620425Ssteve */ 135720425Ssteve continue; 135820425Ssteve } 135917987Speter if (c != '\\' && c != '`' && c != '$' 13601556Srgrimes && (!dblquote || c != '"')) 13611556Srgrimes STPUTC('\\', out); 136220425Ssteve break; 136320425Ssteve 136420425Ssteve case '\n': 136520425Ssteve plinno++; 136620425Ssteve needprompt = doprompt; 136720425Ssteve break; 136820425Ssteve 136920425Ssteve case PEOF: 137020425Ssteve startlinno = plinno; 137120425Ssteve synerror("EOF in backquote substitution"); 137220425Ssteve break; 137320425Ssteve 137420425Ssteve default: 137520425Ssteve break; 137620425Ssteve } 137720425Ssteve STPUTC(c, out); 13781556Srgrimes } 137920425Sstevedone: 13801556Srgrimes STPUTC('\0', out); 13811556Srgrimes savelen = out - stackblock(); 13821556Srgrimes if (savelen > 0) { 13831556Srgrimes str = ckmalloc(savelen); 138417987Speter memcpy(str, stackblock(), savelen); 138517987Speter setinputstring(str, 1); 13861556Srgrimes } 13871556Srgrimes } 13881556Srgrimes nlpp = &bqlist; 13891556Srgrimes while (*nlpp) 13901556Srgrimes nlpp = &(*nlpp)->next; 13911556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13921556Srgrimes (*nlpp)->next = NULL; 13931556Srgrimes parsebackquote = oldstyle; 139420425Ssteve 139520425Ssteve if (oldstyle) { 139620425Ssteve saveprompt = doprompt; 139720425Ssteve doprompt = 0; 139820425Ssteve } 139920425Ssteve 14001556Srgrimes n = list(0); 140120425Ssteve 140220425Ssteve if (oldstyle) 140320425Ssteve doprompt = saveprompt; 140420425Ssteve else { 140520425Ssteve if (readtoken() != TRP) 140620425Ssteve synexpect(TRP); 140720425Ssteve } 140820425Ssteve 14091556Srgrimes (*nlpp)->n = n; 141020425Ssteve if (oldstyle) { 141120425Ssteve /* 141220425Ssteve * Start reading from old file again, ignoring any pushed back 141320425Ssteve * tokens left from the backquote parsing 141420425Ssteve */ 14151556Srgrimes popfile(); 141620425Ssteve tokpushback = 0; 141720425Ssteve } 14181556Srgrimes while (stackblocksize() <= savelen) 14191556Srgrimes growstackblock(); 14201556Srgrimes STARTSTACKSTR(out); 14211556Srgrimes if (str) { 142217987Speter memcpy(out, str, savelen); 14231556Srgrimes STADJUST(savelen, out); 14241556Srgrimes INTOFF; 14251556Srgrimes ckfree(str); 14261556Srgrimes str = NULL; 14271556Srgrimes INTON; 14281556Srgrimes } 14291556Srgrimes parsebackquote = savepbq; 14301556Srgrimes handler = savehandler; 14311556Srgrimes if (arinest || dblquote) 14321556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14331556Srgrimes else 14341556Srgrimes USTPUTC(CTLBACKQ, out); 14351556Srgrimes if (oldstyle) 14361556Srgrimes goto parsebackq_oldreturn; 14371556Srgrimes else 14381556Srgrimes goto parsebackq_newreturn; 14391556Srgrimes} 14401556Srgrimes 14411556Srgrimes/* 14421556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14431556Srgrimes */ 14441556Srgrimesparsearith: { 14451556Srgrimes 14461556Srgrimes if (++arinest == 1) { 14471556Srgrimes prevsyntax = syntax; 14481556Srgrimes syntax = ARISYNTAX; 14491556Srgrimes USTPUTC(CTLARI, out); 145038887Stegge if (dblquote) 145138887Stegge USTPUTC('"',out); 145238887Stegge else 145338887Stegge USTPUTC(' ',out); 14541556Srgrimes } else { 14551556Srgrimes /* 14561556Srgrimes * we collapse embedded arithmetic expansion to 14571556Srgrimes * parenthesis, which should be equivalent 14581556Srgrimes */ 14591556Srgrimes USTPUTC('(', out); 14601556Srgrimes } 14611556Srgrimes goto parsearith_return; 14621556Srgrimes} 14631556Srgrimes 14641556Srgrimes} /* end of readtoken */ 14651556Srgrimes 14661556Srgrimes 14671556Srgrimes 14681556Srgrimes#ifdef mkinit 14691556SrgrimesRESET { 14701556Srgrimes tokpushback = 0; 14711556Srgrimes checkkwd = 0; 14721556Srgrimes} 14731556Srgrimes#endif 14741556Srgrimes 14751556Srgrimes/* 14761556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 14771556Srgrimes * or backquotes). 14781556Srgrimes */ 14791556Srgrimes 14801556SrgrimesSTATIC int 148190111Simpnoexpand(char *text) 148290111Simp{ 148325230Ssteve char *p; 148425230Ssteve char c; 14851556Srgrimes 14861556Srgrimes p = text; 14871556Srgrimes while ((c = *p++) != '\0') { 148839137Stegge if ( c == CTLQUOTEMARK) 148939137Stegge continue; 14901556Srgrimes if (c == CTLESC) 14911556Srgrimes p++; 149283675Stegge else if (BASESYNTAX[(int)c] == CCTL) 14931556Srgrimes return 0; 14941556Srgrimes } 14951556Srgrimes return 1; 14961556Srgrimes} 14971556Srgrimes 14981556Srgrimes 14991556Srgrimes/* 15001556Srgrimes * Return true if the argument is a legal variable name (a letter or 15011556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 15021556Srgrimes */ 15031556Srgrimes 15041556Srgrimesint 150590111Simpgoodname(char *name) 150690111Simp{ 150725230Ssteve char *p; 15081556Srgrimes 15091556Srgrimes p = name; 15101556Srgrimes if (! is_name(*p)) 15111556Srgrimes return 0; 15121556Srgrimes while (*++p) { 15131556Srgrimes if (! is_in_name(*p)) 15141556Srgrimes return 0; 15151556Srgrimes } 15161556Srgrimes return 1; 15171556Srgrimes} 15181556Srgrimes 15191556Srgrimes 15201556Srgrimes/* 15211556Srgrimes * Called when an unexpected token is read during the parse. The argument 15221556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15231556Srgrimes * occur at this point. 15241556Srgrimes */ 15251556Srgrimes 15261556SrgrimesSTATIC void 152790111Simpsynexpect(int token) 152817987Speter{ 15291556Srgrimes char msg[64]; 15301556Srgrimes 15311556Srgrimes if (token >= 0) { 15321556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15331556Srgrimes tokname[lasttoken], tokname[token]); 15341556Srgrimes } else { 15351556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15361556Srgrimes } 15371556Srgrimes synerror(msg); 15381556Srgrimes} 15391556Srgrimes 15401556Srgrimes 15411556SrgrimesSTATIC void 154290111Simpsynerror(char *msg) 154390111Simp{ 15441556Srgrimes if (commandname) 15451556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15461556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15471556Srgrimes error((char *)NULL); 15481556Srgrimes} 15491556Srgrimes 15501556SrgrimesSTATIC void 155190111Simpsetprompt(int which) 155290111Simp{ 15531556Srgrimes whichprompt = which; 15541556Srgrimes 155517987Speter#ifndef NO_HISTORY 15561556Srgrimes if (!el) 155717987Speter#endif 15581556Srgrimes out2str(getprompt(NULL)); 15591556Srgrimes} 15601556Srgrimes 15611556Srgrimes/* 15621556Srgrimes * called by editline -- any expansions to the prompt 15631556Srgrimes * should be added here. 15641556Srgrimes */ 15651556Srgrimeschar * 156690111Simpgetprompt(void *unused __unused) 156725905Ssteve{ 1568142845Sobrien static char ps[PROMPTLEN]; 1569142845Sobrien char *fmt; 1570142845Sobrien int i, j, trim; 1571142845Sobrien 1572142845Sobrien /* 1573142845Sobrien * Select prompt format. 1574142845Sobrien */ 15751556Srgrimes switch (whichprompt) { 15761556Srgrimes case 0: 1577142845Sobrien fmt = ""; 1578142845Sobrien break; 15791556Srgrimes case 1: 1580142845Sobrien fmt = ps1val(); 1581142845Sobrien break; 15821556Srgrimes case 2: 1583142845Sobrien fmt = ps2val(); 1584142845Sobrien break; 15851556Srgrimes default: 15861556Srgrimes return "<internal prompt error>"; 15871556Srgrimes } 1588142845Sobrien 1589142845Sobrien /* 1590142845Sobrien * Format prompt string. 1591142845Sobrien */ 1592142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1593142845Sobrien if (*fmt == '\\') 1594142845Sobrien switch (*++fmt) { 1595142845Sobrien 1596142845Sobrien /* 1597142845Sobrien * Hostname. 1598142845Sobrien * 1599142845Sobrien * \h specifies just the local hostname, 1600142845Sobrien * \H specifies fully-qualified hostname. 1601142845Sobrien */ 1602142845Sobrien case 'h': 1603142845Sobrien case 'H': 1604149024Sstefanf ps[i] = '\0'; 1605142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1606142845Sobrien /* Skip to end of hostname. */ 1607142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1608142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1609142845Sobrien i++; 1610142845Sobrien break; 1611142845Sobrien 1612142845Sobrien /* 1613142845Sobrien * Working directory. 1614142845Sobrien * 1615142845Sobrien * \W specifies just the final component, 1616142845Sobrien * \w specifies the entire path. 1617142845Sobrien */ 1618142845Sobrien case 'W': 1619142845Sobrien case 'w': 1620149024Sstefanf ps[i] = '\0'; 1621142845Sobrien getcwd(&ps[i], PROMPTLEN - i); 1622142845Sobrien if (*fmt == 'W') { 1623142845Sobrien /* Final path component only. */ 1624142845Sobrien trim = 1; 1625142845Sobrien for (j = i; ps[j] != '\0'; j++) 1626142845Sobrien if (ps[j] == '/') 1627142845Sobrien trim = j + 1; 1628142845Sobrien memmove(&ps[i], &ps[trim], 1629142845Sobrien j - trim + 1); 1630142845Sobrien } 1631142845Sobrien /* Skip to end of path. */ 1632142845Sobrien while (ps[i + 1] != '\0') 1633142845Sobrien i++; 1634142845Sobrien break; 1635142845Sobrien 1636142845Sobrien /* 1637142845Sobrien * Superuser status. 1638142845Sobrien * 1639142845Sobrien * '$' for normal users, '#' for root. 1640142845Sobrien */ 1641142845Sobrien case '$': 1642142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1643142845Sobrien break; 1644142845Sobrien 1645142845Sobrien /* 1646142845Sobrien * A literal \. 1647142845Sobrien */ 1648142845Sobrien case '\\': 1649142845Sobrien ps[i] = '\\'; 1650142845Sobrien break; 1651142845Sobrien 1652142845Sobrien /* 1653142845Sobrien * Emit unrecognized formats verbatim. 1654142845Sobrien */ 1655142845Sobrien default: 1656142845Sobrien ps[i++] = '\\'; 1657142845Sobrien ps[i] = *fmt; 1658142845Sobrien break; 1659142845Sobrien } 1660142845Sobrien else 1661142845Sobrien ps[i] = *fmt; 1662142845Sobrien ps[i] = '\0'; 1663142845Sobrien return (ps); 16641556Srgrimes} 1665