parser.c revision 199282
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 199282 2009-11-14 22:08:32Z jilles $"); 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 doprompt; /* if set, prompt the user */ 86117261SddsSTATIC int needprompt; /* true if interactive and at start of line */ 87117261SddsSTATIC int lasttoken; /* last token read */ 881556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 89117261SddsSTATIC char *wordtext; /* text of last word returned by readtoken */ 901556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 91117261SddsSTATIC struct nodelist *backquotelist; 92117261SddsSTATIC union node *redirnode; 93117261SddsSTATIC struct heredoc *heredoc; 94117261SddsSTATIC int quoteflag; /* set if (part of) last token was quoted */ 95117261SddsSTATIC int startlinno; /* line # where last token started */ 96179022SstefanfSTATIC int funclinno; /* line # where the current function started */ 971556Srgrimes 9818018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 9918018Speterstatic int noaliases = 0; 1001556Srgrimes 1011556Srgrimes 10290111SimpSTATIC union node *list(int); 10390111SimpSTATIC union node *andor(void); 10490111SimpSTATIC union node *pipeline(void); 10590111SimpSTATIC union node *command(void); 10690111SimpSTATIC union node *simplecmd(union node **, union node *); 10790111SimpSTATIC union node *makename(void); 10890111SimpSTATIC void parsefname(void); 10990111SimpSTATIC void parseheredoc(void); 11090111SimpSTATIC int peektoken(void); 11190111SimpSTATIC int readtoken(void); 11290111SimpSTATIC int xxreadtoken(void); 11390111SimpSTATIC int readtoken1(int, char const *, char *, int); 11490111SimpSTATIC int noexpand(char *); 11590111SimpSTATIC void synexpect(int); 11690111SimpSTATIC void synerror(char *); 11790111SimpSTATIC void setprompt(int); 1181556Srgrimes 11917987Speter 1201556Srgrimes/* 1211556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1221556Srgrimes * valid parse tree indicating a blank line.) 1231556Srgrimes */ 1241556Srgrimes 1251556Srgrimesunion node * 12690111Simpparsecmd(int interact) 12717987Speter{ 1281556Srgrimes int t; 1291556Srgrimes 13060593Scracauer tokpushback = 0; 1311556Srgrimes doprompt = interact; 1321556Srgrimes if (doprompt) 1331556Srgrimes setprompt(1); 1341556Srgrimes else 1351556Srgrimes setprompt(0); 1361556Srgrimes needprompt = 0; 1371556Srgrimes t = readtoken(); 1381556Srgrimes if (t == TEOF) 1391556Srgrimes return NEOF; 1401556Srgrimes if (t == TNL) 1411556Srgrimes return NULL; 1421556Srgrimes tokpushback++; 1431556Srgrimes return list(1); 1441556Srgrimes} 1451556Srgrimes 1461556Srgrimes 1471556SrgrimesSTATIC union node * 14890111Simplist(int nlflag) 14917987Speter{ 1501556Srgrimes union node *n1, *n2, *n3; 15117987Speter int tok; 1521556Srgrimes 1531556Srgrimes checkkwd = 2; 1541556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 1551556Srgrimes return NULL; 15617987Speter n1 = NULL; 1571556Srgrimes for (;;) { 15817987Speter n2 = andor(); 15917987Speter tok = readtoken(); 16017987Speter if (tok == TBACKGND) { 16117987Speter if (n2->type == NCMD || n2->type == NPIPE) { 16217987Speter n2->ncmd.backgnd = 1; 16317987Speter } else if (n2->type == NREDIR) { 16417987Speter n2->type = NBACKGND; 16517987Speter } else { 16617987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 16717987Speter n3->type = NBACKGND; 16817987Speter n3->nredir.n = n2; 16917987Speter n3->nredir.redirect = NULL; 17017987Speter n2 = n3; 17117987Speter } 17217987Speter } 17317987Speter if (n1 == NULL) { 17417987Speter n1 = n2; 17517987Speter } 17617987Speter else { 17717987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 17817987Speter n3->type = NSEMI; 17917987Speter n3->nbinary.ch1 = n1; 18017987Speter n3->nbinary.ch2 = n2; 18117987Speter n1 = n3; 18217987Speter } 18317987Speter switch (tok) { 18413882Sjoerg case TBACKGND: 18517987Speter case TSEMI: 18617987Speter tok = readtoken(); 187102410Scharnier /* FALLTHROUGH */ 1881556Srgrimes case TNL: 18917987Speter if (tok == TNL) { 19017987Speter parseheredoc(); 19117987Speter if (nlflag) 19217987Speter return n1; 19317987Speter } else { 19417987Speter tokpushback++; 19517987Speter } 1961556Srgrimes checkkwd = 2; 1971556Srgrimes if (tokendlist[peektoken()]) 1981556Srgrimes return n1; 1991556Srgrimes break; 2001556Srgrimes case TEOF: 2011556Srgrimes if (heredoclist) 2021556Srgrimes parseheredoc(); 2031556Srgrimes else 2041556Srgrimes pungetc(); /* push back EOF on input */ 2051556Srgrimes return n1; 2061556Srgrimes default: 2071556Srgrimes if (nlflag) 2081556Srgrimes synexpect(-1); 2091556Srgrimes tokpushback++; 2101556Srgrimes return n1; 2111556Srgrimes } 2121556Srgrimes } 2131556Srgrimes} 2141556Srgrimes 2151556Srgrimes 2161556Srgrimes 2171556SrgrimesSTATIC union node * 21890111Simpandor(void) 21990111Simp{ 2201556Srgrimes union node *n1, *n2, *n3; 2211556Srgrimes int t; 2221556Srgrimes 2231556Srgrimes n1 = pipeline(); 2241556Srgrimes for (;;) { 2251556Srgrimes if ((t = readtoken()) == TAND) { 2261556Srgrimes t = NAND; 2271556Srgrimes } else if (t == TOR) { 2281556Srgrimes t = NOR; 2291556Srgrimes } else { 2301556Srgrimes tokpushback++; 2311556Srgrimes return n1; 2321556Srgrimes } 2331556Srgrimes n2 = pipeline(); 2341556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2351556Srgrimes n3->type = t; 2361556Srgrimes n3->nbinary.ch1 = n1; 2371556Srgrimes n3->nbinary.ch2 = n2; 2381556Srgrimes n1 = n3; 2391556Srgrimes } 2401556Srgrimes} 2411556Srgrimes 2421556Srgrimes 2431556Srgrimes 2441556SrgrimesSTATIC union node * 24590111Simppipeline(void) 24690111Simp{ 24775336Sbrian union node *n1, *n2, *pipenode; 2481556Srgrimes struct nodelist *lp, *prev; 24975336Sbrian int negate; 2501556Srgrimes 25175336Sbrian negate = 0; 252191009Sstefanf checkkwd = 2; 2531556Srgrimes TRACE(("pipeline: entered\n")); 25475336Sbrian while (readtoken() == TNOT) 25575336Sbrian negate = !negate; 25675336Sbrian tokpushback++; 2571556Srgrimes n1 = command(); 2581556Srgrimes if (readtoken() == TPIPE) { 2591556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 2601556Srgrimes pipenode->type = NPIPE; 2611556Srgrimes pipenode->npipe.backgnd = 0; 2621556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2631556Srgrimes pipenode->npipe.cmdlist = lp; 2641556Srgrimes lp->n = n1; 2651556Srgrimes do { 2661556Srgrimes prev = lp; 2671556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2681556Srgrimes lp->n = command(); 2691556Srgrimes prev->next = lp; 2701556Srgrimes } while (readtoken() == TPIPE); 2711556Srgrimes lp->next = NULL; 2721556Srgrimes n1 = pipenode; 2731556Srgrimes } 2741556Srgrimes tokpushback++; 27575336Sbrian if (negate) { 27675336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 27775336Sbrian n2->type = NNOT; 27875336Sbrian n2->nnot.com = n1; 27975336Sbrian return n2; 28075336Sbrian } else 28175336Sbrian return n1; 2821556Srgrimes} 2831556Srgrimes 2841556Srgrimes 2851556Srgrimes 2861556SrgrimesSTATIC union node * 28790111Simpcommand(void) 28890111Simp{ 2891556Srgrimes union node *n1, *n2; 2901556Srgrimes union node *ap, **app; 2911556Srgrimes union node *cp, **cpp; 2921556Srgrimes union node *redir, **rpp; 29375160Sbrian int t, negate = 0; 2941556Srgrimes 2951556Srgrimes checkkwd = 2; 29617987Speter redir = NULL; 29717987Speter n1 = NULL; 2981556Srgrimes rpp = &redir; 29920425Ssteve 3001556Srgrimes /* Check for redirection which may precede command */ 3011556Srgrimes while (readtoken() == TREDIR) { 3021556Srgrimes *rpp = n2 = redirnode; 3031556Srgrimes rpp = &n2->nfile.next; 3041556Srgrimes parsefname(); 3051556Srgrimes } 3061556Srgrimes tokpushback++; 3071556Srgrimes 30875160Sbrian while (readtoken() == TNOT) { 30975160Sbrian TRACE(("command: TNOT recognized\n")); 31075160Sbrian negate = !negate; 31175160Sbrian } 31275160Sbrian tokpushback++; 31375160Sbrian 3141556Srgrimes switch (readtoken()) { 3151556Srgrimes case TIF: 3161556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3171556Srgrimes n1->type = NIF; 318104554Stjr if ((n1->nif.test = list(0)) == NULL) 319104554Stjr synexpect(-1); 3201556Srgrimes if (readtoken() != TTHEN) 3211556Srgrimes synexpect(TTHEN); 3221556Srgrimes n1->nif.ifpart = list(0); 3231556Srgrimes n2 = n1; 3241556Srgrimes while (readtoken() == TELIF) { 3251556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3261556Srgrimes n2 = n2->nif.elsepart; 3271556Srgrimes n2->type = NIF; 328104554Stjr if ((n2->nif.test = list(0)) == NULL) 329104554Stjr synexpect(-1); 3301556Srgrimes if (readtoken() != TTHEN) 3311556Srgrimes synexpect(TTHEN); 3321556Srgrimes n2->nif.ifpart = list(0); 3331556Srgrimes } 3341556Srgrimes if (lasttoken == TELSE) 3351556Srgrimes n2->nif.elsepart = list(0); 3361556Srgrimes else { 3371556Srgrimes n2->nif.elsepart = NULL; 3381556Srgrimes tokpushback++; 3391556Srgrimes } 3401556Srgrimes if (readtoken() != TFI) 3411556Srgrimes synexpect(TFI); 3421556Srgrimes checkkwd = 1; 3431556Srgrimes break; 3441556Srgrimes case TWHILE: 3451556Srgrimes case TUNTIL: { 3461556Srgrimes int got; 3471556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3481556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 349104554Stjr if ((n1->nbinary.ch1 = list(0)) == NULL) 350104554Stjr synexpect(-1); 3511556Srgrimes if ((got=readtoken()) != TDO) { 3521556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3531556Srgrimes synexpect(TDO); 3541556Srgrimes } 3551556Srgrimes n1->nbinary.ch2 = list(0); 3561556Srgrimes if (readtoken() != TDONE) 3571556Srgrimes synexpect(TDONE); 3581556Srgrimes checkkwd = 1; 3591556Srgrimes break; 3601556Srgrimes } 3611556Srgrimes case TFOR: 3621556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3631556Srgrimes synerror("Bad for loop variable"); 3641556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3651556Srgrimes n1->type = NFOR; 3661556Srgrimes n1->nfor.var = wordtext; 367199282Sjilles while (readtoken() == TNL) 368199282Sjilles ; 369199282Sjilles if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 3701556Srgrimes app = ≈ 3711556Srgrimes while (readtoken() == TWORD) { 3721556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3731556Srgrimes n2->type = NARG; 3741556Srgrimes n2->narg.text = wordtext; 3751556Srgrimes n2->narg.backquote = backquotelist; 3761556Srgrimes *app = n2; 3771556Srgrimes app = &n2->narg.next; 3781556Srgrimes } 3791556Srgrimes *app = NULL; 3801556Srgrimes n1->nfor.args = ap; 3811556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3821556Srgrimes synexpect(-1); 3831556Srgrimes } else { 384149096Sstefanf static char argvars[5] = { 385149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 386149096Sstefanf }; 3871556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3881556Srgrimes n2->type = NARG; 389149096Sstefanf n2->narg.text = argvars; 3901556Srgrimes n2->narg.backquote = NULL; 3911556Srgrimes n2->narg.next = NULL; 3921556Srgrimes n1->nfor.args = n2; 3931556Srgrimes /* 3941556Srgrimes * Newline or semicolon here is optional (but note 3951556Srgrimes * that the original Bourne shell only allowed NL). 3961556Srgrimes */ 3971556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3981556Srgrimes tokpushback++; 3991556Srgrimes } 4001556Srgrimes checkkwd = 2; 4011556Srgrimes if ((t = readtoken()) == TDO) 4021556Srgrimes t = TDONE; 4031556Srgrimes else if (t == TBEGIN) 4041556Srgrimes t = TEND; 4051556Srgrimes else 4061556Srgrimes synexpect(-1); 4071556Srgrimes n1->nfor.body = list(0); 4081556Srgrimes if (readtoken() != t) 4091556Srgrimes synexpect(t); 4101556Srgrimes checkkwd = 1; 4111556Srgrimes break; 4121556Srgrimes case TCASE: 4131556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4141556Srgrimes n1->type = NCASE; 4151556Srgrimes if (readtoken() != TWORD) 4161556Srgrimes synexpect(TWORD); 4171556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4181556Srgrimes n2->type = NARG; 4191556Srgrimes n2->narg.text = wordtext; 4201556Srgrimes n2->narg.backquote = backquotelist; 4211556Srgrimes n2->narg.next = NULL; 4221556Srgrimes while (readtoken() == TNL); 4231556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4241556Srgrimes synerror("expecting \"in\""); 4251556Srgrimes cpp = &n1->ncase.cases; 42618018Speter noaliases = 1; /* turn off alias expansion */ 4272760Ssef checkkwd = 2, readtoken(); 428104202Stjr while (lasttoken != TESAC) { 4291556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4301556Srgrimes cp->type = NCLIST; 4311556Srgrimes app = &cp->nclist.pattern; 432104207Stjr if (lasttoken == TLP) 433104207Stjr readtoken(); 4341556Srgrimes for (;;) { 4351556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4361556Srgrimes ap->type = NARG; 4371556Srgrimes ap->narg.text = wordtext; 4381556Srgrimes ap->narg.backquote = backquotelist; 4392760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4401556Srgrimes break; 4411556Srgrimes app = &ap->narg.next; 4422760Ssef readtoken(); 4431556Srgrimes } 4441556Srgrimes ap->narg.next = NULL; 4451556Srgrimes if (lasttoken != TRP) 44618018Speter noaliases = 0, synexpect(TRP); 4471556Srgrimes cp->nclist.body = list(0); 4482760Ssef 4492760Ssef checkkwd = 2; 4502760Ssef if ((t = readtoken()) != TESAC) { 4512760Ssef if (t != TENDCASE) 45218018Speter noaliases = 0, synexpect(TENDCASE); 4532760Ssef else 4542760Ssef checkkwd = 2, readtoken(); 4552760Ssef } 4561556Srgrimes cpp = &cp->nclist.next; 457104202Stjr } 45818018Speter noaliases = 0; /* reset alias expansion */ 4591556Srgrimes *cpp = NULL; 4601556Srgrimes checkkwd = 1; 4611556Srgrimes break; 4621556Srgrimes case TLP: 4631556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4641556Srgrimes n1->type = NSUBSHELL; 4651556Srgrimes n1->nredir.n = list(0); 4661556Srgrimes n1->nredir.redirect = NULL; 4671556Srgrimes if (readtoken() != TRP) 4681556Srgrimes synexpect(TRP); 4691556Srgrimes checkkwd = 1; 4701556Srgrimes break; 4711556Srgrimes case TBEGIN: 4721556Srgrimes n1 = list(0); 4731556Srgrimes if (readtoken() != TEND) 4741556Srgrimes synexpect(TEND); 4751556Srgrimes checkkwd = 1; 4761556Srgrimes break; 4771556Srgrimes /* Handle an empty command like other simple commands. */ 47817987Speter case TSEMI: 479101662Stjr case TAND: 480101662Stjr case TOR: 48117987Speter /* 48217987Speter * An empty command before a ; doesn't make much sense, and 48317987Speter * should certainly be disallowed in the case of `if ;'. 48417987Speter */ 48517987Speter if (!redir) 48617987Speter synexpect(-1); 4871556Srgrimes case TNL: 48810399Sjoerg case TEOF: 4891556Srgrimes case TWORD: 49017987Speter case TRP: 4911556Srgrimes tokpushback++; 49275160Sbrian n1 = simplecmd(rpp, redir); 49375160Sbrian goto checkneg; 4941556Srgrimes default: 4951556Srgrimes synexpect(-1); 4961556Srgrimes } 4971556Srgrimes 4981556Srgrimes /* Now check for redirection which may follow command */ 4991556Srgrimes while (readtoken() == TREDIR) { 5001556Srgrimes *rpp = n2 = redirnode; 5011556Srgrimes rpp = &n2->nfile.next; 5021556Srgrimes parsefname(); 5031556Srgrimes } 5041556Srgrimes tokpushback++; 5051556Srgrimes *rpp = NULL; 5061556Srgrimes if (redir) { 5071556Srgrimes if (n1->type != NSUBSHELL) { 5081556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5091556Srgrimes n2->type = NREDIR; 5101556Srgrimes n2->nredir.n = n1; 5111556Srgrimes n1 = n2; 5121556Srgrimes } 5131556Srgrimes n1->nredir.redirect = redir; 5141556Srgrimes } 51575160Sbrian 51675160Sbriancheckneg: 51775160Sbrian if (negate) { 51875160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 51975160Sbrian n2->type = NNOT; 52075160Sbrian n2->nnot.com = n1; 52175160Sbrian return n2; 52275160Sbrian } 52375160Sbrian else 52475160Sbrian return n1; 5251556Srgrimes} 5261556Srgrimes 5271556Srgrimes 5281556SrgrimesSTATIC union node * 52990111Simpsimplecmd(union node **rpp, union node *redir) 53090111Simp{ 5311556Srgrimes union node *args, **app; 5321556Srgrimes union node **orig_rpp = rpp; 53375160Sbrian union node *n = NULL, *n2; 53475160Sbrian int negate = 0; 5351556Srgrimes 5361556Srgrimes /* If we don't have any redirections already, then we must reset */ 5371556Srgrimes /* rpp to be the address of the local redir variable. */ 5381556Srgrimes if (redir == 0) 5391556Srgrimes rpp = &redir; 5401556Srgrimes 5411556Srgrimes args = NULL; 5421556Srgrimes app = &args; 5438855Srgrimes /* 5441556Srgrimes * We save the incoming value, because we need this for shell 5451556Srgrimes * functions. There can not be a redirect or an argument between 5468855Srgrimes * the function name and the open parenthesis. 5471556Srgrimes */ 5481556Srgrimes orig_rpp = rpp; 5491556Srgrimes 55075160Sbrian while (readtoken() == TNOT) { 55175160Sbrian TRACE(("command: TNOT recognized\n")); 55275160Sbrian negate = !negate; 55375160Sbrian } 55475160Sbrian tokpushback++; 55575160Sbrian 5561556Srgrimes for (;;) { 5571556Srgrimes if (readtoken() == TWORD) { 5581556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5591556Srgrimes n->type = NARG; 5601556Srgrimes n->narg.text = wordtext; 5611556Srgrimes n->narg.backquote = backquotelist; 5621556Srgrimes *app = n; 5631556Srgrimes app = &n->narg.next; 5641556Srgrimes } else if (lasttoken == TREDIR) { 5651556Srgrimes *rpp = n = redirnode; 5661556Srgrimes rpp = &n->nfile.next; 5671556Srgrimes parsefname(); /* read name of redirection file */ 5681556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5691556Srgrimes && rpp == orig_rpp) { 5701556Srgrimes /* We have a function */ 5711556Srgrimes if (readtoken() != TRP) 5721556Srgrimes synexpect(TRP); 573179022Sstefanf funclinno = plinno; 5741556Srgrimes#ifdef notdef 5751556Srgrimes if (! goodname(n->narg.text)) 5761556Srgrimes synerror("Bad function name"); 5771556Srgrimes#endif 5781556Srgrimes n->type = NDEFUN; 5791556Srgrimes n->narg.next = command(); 580179022Sstefanf funclinno = 0; 58175160Sbrian goto checkneg; 5821556Srgrimes } else { 5831556Srgrimes tokpushback++; 5841556Srgrimes break; 5851556Srgrimes } 5861556Srgrimes } 5871556Srgrimes *app = NULL; 5881556Srgrimes *rpp = NULL; 5891556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5901556Srgrimes n->type = NCMD; 5911556Srgrimes n->ncmd.backgnd = 0; 5921556Srgrimes n->ncmd.args = args; 5931556Srgrimes n->ncmd.redirect = redir; 59475160Sbrian 59575160Sbriancheckneg: 59675160Sbrian if (negate) { 59775160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 59875160Sbrian n2->type = NNOT; 59975160Sbrian n2->nnot.com = n; 60075160Sbrian return n2; 60175160Sbrian } 60275160Sbrian else 60375160Sbrian return n; 6041556Srgrimes} 6051556Srgrimes 60617987SpeterSTATIC union node * 60790111Simpmakename(void) 60890111Simp{ 60917987Speter union node *n; 6101556Srgrimes 61117987Speter n = (union node *)stalloc(sizeof (struct narg)); 61217987Speter n->type = NARG; 61317987Speter n->narg.next = NULL; 61417987Speter n->narg.text = wordtext; 61517987Speter n->narg.backquote = backquotelist; 61617987Speter return n; 61717987Speter} 61817987Speter 61990111Simpvoid fixredir(union node *n, const char *text, int err) 62090111Simp{ 62117987Speter TRACE(("Fix redir %s %d\n", text, err)); 62217987Speter if (!err) 62317987Speter n->ndup.vname = NULL; 62417987Speter 62517987Speter if (is_digit(text[0]) && text[1] == '\0') 62617987Speter n->ndup.dupfd = digit_val(text[0]); 62717987Speter else if (text[0] == '-' && text[1] == '\0') 62817987Speter n->ndup.dupfd = -1; 62917987Speter else { 63020425Ssteve 63117987Speter if (err) 63217987Speter synerror("Bad fd number"); 63317987Speter else 63417987Speter n->ndup.vname = makename(); 63517987Speter } 63617987Speter} 63717987Speter 63817987Speter 6391556SrgrimesSTATIC void 64090111Simpparsefname(void) 64190111Simp{ 6421556Srgrimes union node *n = redirnode; 6431556Srgrimes 6441556Srgrimes if (readtoken() != TWORD) 6451556Srgrimes synexpect(-1); 6461556Srgrimes if (n->type == NHERE) { 6471556Srgrimes struct heredoc *here = heredoc; 6481556Srgrimes struct heredoc *p; 6491556Srgrimes int i; 6501556Srgrimes 6511556Srgrimes if (quoteflag == 0) 6521556Srgrimes n->type = NXHERE; 6531556Srgrimes TRACE(("Here document %d\n", n->type)); 6541556Srgrimes if (here->striptabs) { 6551556Srgrimes while (*wordtext == '\t') 6561556Srgrimes wordtext++; 6571556Srgrimes } 6581556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6591556Srgrimes synerror("Illegal eof marker for << redirection"); 6601556Srgrimes rmescapes(wordtext); 6611556Srgrimes here->eofmark = wordtext; 6621556Srgrimes here->next = NULL; 6631556Srgrimes if (heredoclist == NULL) 6641556Srgrimes heredoclist = here; 6651556Srgrimes else { 6661556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6671556Srgrimes p->next = here; 6681556Srgrimes } 6691556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 67017987Speter fixredir(n, wordtext, 0); 6711556Srgrimes } else { 67217987Speter n->nfile.fname = makename(); 6731556Srgrimes } 6741556Srgrimes} 6751556Srgrimes 6761556Srgrimes 6771556Srgrimes/* 6781556Srgrimes * Input any here documents. 6791556Srgrimes */ 6801556Srgrimes 6811556SrgrimesSTATIC void 68290111Simpparseheredoc(void) 68390111Simp{ 6841556Srgrimes struct heredoc *here; 6851556Srgrimes union node *n; 6861556Srgrimes 6871556Srgrimes while (heredoclist) { 6881556Srgrimes here = heredoclist; 6891556Srgrimes heredoclist = here->next; 6901556Srgrimes if (needprompt) { 6911556Srgrimes setprompt(2); 6921556Srgrimes needprompt = 0; 6931556Srgrimes } 6941556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6951556Srgrimes here->eofmark, here->striptabs); 6961556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6971556Srgrimes n->narg.type = NARG; 6981556Srgrimes n->narg.next = NULL; 6991556Srgrimes n->narg.text = wordtext; 7001556Srgrimes n->narg.backquote = backquotelist; 7011556Srgrimes here->here->nhere.doc = n; 7021556Srgrimes } 7031556Srgrimes} 7041556Srgrimes 7051556SrgrimesSTATIC int 70690111Simppeektoken(void) 70790111Simp{ 7081556Srgrimes int t; 7091556Srgrimes 7101556Srgrimes t = readtoken(); 7111556Srgrimes tokpushback++; 7121556Srgrimes return (t); 7131556Srgrimes} 7141556Srgrimes 7151556SrgrimesSTATIC int 71690111Simpreadtoken(void) 71790111Simp{ 7181556Srgrimes int t; 7191556Srgrimes int savecheckkwd = checkkwd; 7201556Srgrimes struct alias *ap; 7211556Srgrimes#ifdef DEBUG 7221556Srgrimes int alreadyseen = tokpushback; 7231556Srgrimes#endif 7248855Srgrimes 7251556Srgrimes top: 7261556Srgrimes t = xxreadtoken(); 7271556Srgrimes 7281556Srgrimes if (checkkwd) { 7291556Srgrimes /* 7301556Srgrimes * eat newlines 7311556Srgrimes */ 7321556Srgrimes if (checkkwd == 2) { 7331556Srgrimes checkkwd = 0; 7341556Srgrimes while (t == TNL) { 7351556Srgrimes parseheredoc(); 7361556Srgrimes t = xxreadtoken(); 7371556Srgrimes } 7381556Srgrimes } else 7391556Srgrimes checkkwd = 0; 7401556Srgrimes /* 7411556Srgrimes * check for keywords and aliases 7421556Srgrimes */ 74320425Ssteve if (t == TWORD && !quoteflag) 74417987Speter { 74598463Sjmallett const char * const *pp; 7461556Srgrimes 74798463Sjmallett for (pp = parsekwd; *pp; pp++) { 74820425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 74917987Speter { 7501556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7511556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7521556Srgrimes goto out; 7531556Srgrimes } 7541556Srgrimes } 75518018Speter if (noaliases == 0 && 75618018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7571556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7581556Srgrimes checkkwd = savecheckkwd; 7591556Srgrimes goto top; 7601556Srgrimes } 7611556Srgrimes } 7621556Srgrimesout: 76375160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 7641556Srgrimes } 7651556Srgrimes#ifdef DEBUG 7661556Srgrimes if (!alreadyseen) 7671556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7681556Srgrimes else 7691556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7701556Srgrimes#endif 7711556Srgrimes return (t); 7721556Srgrimes} 7731556Srgrimes 7741556Srgrimes 7751556Srgrimes/* 7761556Srgrimes * Read the next input token. 7771556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7781556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7791556Srgrimes * quoted. 7801556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7811556Srgrimes * the redirection. 7821556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7831556Srgrimes * on which the token starts. 7841556Srgrimes * 7851556Srgrimes * [Change comment: here documents and internal procedures] 7861556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7871556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7881556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7891556Srgrimes * We could also make parseoperator in essence the main routine, and 7901556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7911556Srgrimes */ 7921556Srgrimes 7931556Srgrimes#define RETURN(token) return lasttoken = token 7941556Srgrimes 7951556SrgrimesSTATIC int 79690111Simpxxreadtoken(void) 79790111Simp{ 79825230Ssteve int c; 7991556Srgrimes 8001556Srgrimes if (tokpushback) { 8011556Srgrimes tokpushback = 0; 8021556Srgrimes return lasttoken; 8031556Srgrimes } 8041556Srgrimes if (needprompt) { 8051556Srgrimes setprompt(2); 8061556Srgrimes needprompt = 0; 8071556Srgrimes } 8081556Srgrimes startlinno = plinno; 8091556Srgrimes for (;;) { /* until token or start of word found */ 8101556Srgrimes c = pgetc_macro(); 8111556Srgrimes if (c == ' ' || c == '\t') 8121556Srgrimes continue; /* quick check for white space first */ 8131556Srgrimes switch (c) { 8141556Srgrimes case ' ': case '\t': 8151556Srgrimes continue; 8161556Srgrimes case '#': 8171556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8181556Srgrimes pungetc(); 8191556Srgrimes continue; 8201556Srgrimes case '\\': 8211556Srgrimes if (pgetc() == '\n') { 8221556Srgrimes startlinno = ++plinno; 8231556Srgrimes if (doprompt) 8241556Srgrimes setprompt(2); 8251556Srgrimes else 8261556Srgrimes setprompt(0); 8271556Srgrimes continue; 8281556Srgrimes } 8291556Srgrimes pungetc(); 8301556Srgrimes goto breakloop; 8311556Srgrimes case '\n': 8321556Srgrimes plinno++; 8331556Srgrimes needprompt = doprompt; 8341556Srgrimes RETURN(TNL); 8351556Srgrimes case PEOF: 8361556Srgrimes RETURN(TEOF); 8371556Srgrimes case '&': 8381556Srgrimes if (pgetc() == '&') 8391556Srgrimes RETURN(TAND); 8401556Srgrimes pungetc(); 8411556Srgrimes RETURN(TBACKGND); 8421556Srgrimes case '|': 8431556Srgrimes if (pgetc() == '|') 8441556Srgrimes RETURN(TOR); 8451556Srgrimes pungetc(); 8461556Srgrimes RETURN(TPIPE); 8471556Srgrimes case ';': 8481556Srgrimes if (pgetc() == ';') 8491556Srgrimes RETURN(TENDCASE); 8501556Srgrimes pungetc(); 8511556Srgrimes RETURN(TSEMI); 8521556Srgrimes case '(': 8531556Srgrimes RETURN(TLP); 8541556Srgrimes case ')': 8551556Srgrimes RETURN(TRP); 8561556Srgrimes default: 8571556Srgrimes goto breakloop; 8581556Srgrimes } 8591556Srgrimes } 8601556Srgrimesbreakloop: 8611556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8621556Srgrimes#undef RETURN 8631556Srgrimes} 8641556Srgrimes 8651556Srgrimes 8661556Srgrimes 8671556Srgrimes/* 8681556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8691556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8701556Srgrimes * word which marks the end of the document and striptabs is true if 8711556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8721556Srgrimes * is the first character of the input token or document. 8731556Srgrimes * 8741556Srgrimes * Because C does not have internal subroutines, I have simulated them 8751556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8761556Srgrimes * will run code that appears at the end of readtoken1. 8771556Srgrimes */ 8781556Srgrimes 8791556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8801556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8811556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8821556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8831556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8841556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8851556Srgrimes 8861556SrgrimesSTATIC int 88790111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) 88890111Simp{ 88917987Speter int c = firstc; 89017987Speter char *out; 8911556Srgrimes int len; 8921556Srgrimes char line[EOFMARKLEN + 1]; 8931556Srgrimes struct nodelist *bqlist; 8941556Srgrimes int quotef; 8951556Srgrimes int dblquote; 8961556Srgrimes int varnest; /* levels of variables expansion */ 8971556Srgrimes int arinest; /* levels of arithmetic expansion */ 8981556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 8991556Srgrimes int oldstyle; 9001556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 90154679Scracauer int synentry; 9021556Srgrimes 9031556Srgrimes startlinno = plinno; 9041556Srgrimes dblquote = 0; 9051556Srgrimes if (syntax == DQSYNTAX) 9061556Srgrimes dblquote = 1; 9071556Srgrimes quotef = 0; 9081556Srgrimes bqlist = NULL; 9091556Srgrimes varnest = 0; 9101556Srgrimes arinest = 0; 9111556Srgrimes parenlevel = 0; 9121556Srgrimes 9131556Srgrimes STARTSTACKSTR(out); 9141556Srgrimes loop: { /* for each line, until end of word */ 9151556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9161556Srgrimes for (;;) { /* until end of line or end of word */ 9171556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 91854679Scracauer 91964705Scracauer synentry = syntax[c]; 92054679Scracauer 92154679Scracauer switch(synentry) { 9221556Srgrimes case CNL: /* '\n' */ 9231556Srgrimes if (syntax == BASESYNTAX) 9241556Srgrimes goto endword; /* exit outer loop */ 9251556Srgrimes USTPUTC(c, out); 9261556Srgrimes plinno++; 9271556Srgrimes if (doprompt) 9281556Srgrimes setprompt(2); 9291556Srgrimes else 9301556Srgrimes setprompt(0); 9311556Srgrimes c = pgetc(); 9321556Srgrimes goto loop; /* continue outer loop */ 9331556Srgrimes case CWORD: 9341556Srgrimes USTPUTC(c, out); 9351556Srgrimes break; 9361556Srgrimes case CCTL: 9371556Srgrimes if (eofmark == NULL || dblquote) 9381556Srgrimes USTPUTC(CTLESC, out); 9391556Srgrimes USTPUTC(c, out); 9401556Srgrimes break; 9411556Srgrimes case CBACK: /* backslash */ 9421556Srgrimes c = pgetc(); 9431556Srgrimes if (c == PEOF) { 9441556Srgrimes USTPUTC('\\', out); 9451556Srgrimes pungetc(); 9461556Srgrimes } else if (c == '\n') { 947160849Syar plinno++; 9481556Srgrimes if (doprompt) 9491556Srgrimes setprompt(2); 9501556Srgrimes else 9511556Srgrimes setprompt(0); 9521556Srgrimes } else { 95354631Scracauer if (dblquote && c != '\\' && 95454631Scracauer c != '`' && c != '$' && 95554631Scracauer (c != '"' || eofmark != NULL)) 9561556Srgrimes USTPUTC('\\', out); 95783675Stegge if (SQSYNTAX[c] == CCTL) 9581556Srgrimes USTPUTC(CTLESC, out); 95939137Stegge else if (eofmark == NULL) 96038887Stegge USTPUTC(CTLQUOTEMARK, out); 9611556Srgrimes USTPUTC(c, out); 9621556Srgrimes quotef++; 9631556Srgrimes } 9641556Srgrimes break; 9651556Srgrimes case CSQUOTE: 96639137Stegge if (eofmark == NULL) 96739137Stegge USTPUTC(CTLQUOTEMARK, out); 9681556Srgrimes syntax = SQSYNTAX; 9691556Srgrimes break; 9701556Srgrimes case CDQUOTE: 97139137Stegge if (eofmark == NULL) 97239137Stegge USTPUTC(CTLQUOTEMARK, out); 9731556Srgrimes syntax = DQSYNTAX; 9741556Srgrimes dblquote = 1; 9751556Srgrimes break; 9761556Srgrimes case CENDQUOTE: 97739137Stegge if (eofmark != NULL && arinest == 0 && 97839137Stegge varnest == 0) { 9791556Srgrimes USTPUTC(c, out); 9801556Srgrimes } else { 98139137Stegge if (arinest) { 9821556Srgrimes syntax = ARISYNTAX; 98339137Stegge dblquote = 0; 98439137Stegge } else if (eofmark == NULL) { 9851556Srgrimes syntax = BASESYNTAX; 98639137Stegge dblquote = 0; 98739137Stegge } 9881556Srgrimes quotef++; 9891556Srgrimes } 9901556Srgrimes break; 9911556Srgrimes case CVAR: /* '$' */ 9921556Srgrimes PARSESUB(); /* parse substitution */ 9931556Srgrimes break; 9941556Srgrimes case CENDVAR: /* '}' */ 9951556Srgrimes if (varnest > 0) { 9961556Srgrimes varnest--; 9971556Srgrimes USTPUTC(CTLENDVAR, out); 9981556Srgrimes } else { 9991556Srgrimes USTPUTC(c, out); 10001556Srgrimes } 10011556Srgrimes break; 10021556Srgrimes case CLP: /* '(' in arithmetic */ 10031556Srgrimes parenlevel++; 10041556Srgrimes USTPUTC(c, out); 10051556Srgrimes break; 10061556Srgrimes case CRP: /* ')' in arithmetic */ 10071556Srgrimes if (parenlevel > 0) { 10081556Srgrimes USTPUTC(c, out); 10091556Srgrimes --parenlevel; 10101556Srgrimes } else { 10111556Srgrimes if (pgetc() == ')') { 10121556Srgrimes if (--arinest == 0) { 10131556Srgrimes USTPUTC(CTLENDARI, out); 10141556Srgrimes syntax = prevsyntax; 101539137Stegge if (syntax == DQSYNTAX) 101639137Stegge dblquote = 1; 101739137Stegge else 101839137Stegge dblquote = 0; 10191556Srgrimes } else 10201556Srgrimes USTPUTC(')', out); 10211556Srgrimes } else { 10228855Srgrimes /* 10231556Srgrimes * unbalanced parens 10241556Srgrimes * (don't 2nd guess - no error) 10251556Srgrimes */ 10261556Srgrimes pungetc(); 10271556Srgrimes USTPUTC(')', out); 10281556Srgrimes } 10291556Srgrimes } 10301556Srgrimes break; 10311556Srgrimes case CBQUOTE: /* '`' */ 10321556Srgrimes PARSEBACKQOLD(); 10331556Srgrimes break; 10341556Srgrimes case CEOF: 10351556Srgrimes goto endword; /* exit outer loop */ 10361556Srgrimes default: 10371556Srgrimes if (varnest == 0) 10381556Srgrimes goto endword; /* exit outer loop */ 10391556Srgrimes USTPUTC(c, out); 10401556Srgrimes } 10411556Srgrimes c = pgetc_macro(); 10421556Srgrimes } 10431556Srgrimes } 10441556Srgrimesendword: 10451556Srgrimes if (syntax == ARISYNTAX) 10461556Srgrimes synerror("Missing '))'"); 1047197691Sjilles if (syntax != BASESYNTAX && eofmark == NULL) 10481556Srgrimes synerror("Unterminated quoted string"); 10491556Srgrimes if (varnest != 0) { 10501556Srgrimes startlinno = plinno; 10511556Srgrimes synerror("Missing '}'"); 10521556Srgrimes } 10531556Srgrimes USTPUTC('\0', out); 10541556Srgrimes len = out - stackblock(); 10551556Srgrimes out = stackblock(); 10561556Srgrimes if (eofmark == NULL) { 10571556Srgrimes if ((c == '>' || c == '<') 10581556Srgrimes && quotef == 0 10591556Srgrimes && len <= 2 10601556Srgrimes && (*out == '\0' || is_digit(*out))) { 10611556Srgrimes PARSEREDIR(); 10621556Srgrimes return lasttoken = TREDIR; 10631556Srgrimes } else { 10641556Srgrimes pungetc(); 10651556Srgrimes } 10661556Srgrimes } 10671556Srgrimes quoteflag = quotef; 10681556Srgrimes backquotelist = bqlist; 10691556Srgrimes grabstackblock(len); 10701556Srgrimes wordtext = out; 10711556Srgrimes return lasttoken = TWORD; 10721556Srgrimes/* end of readtoken routine */ 10731556Srgrimes 10741556Srgrimes 10751556Srgrimes 10761556Srgrimes/* 10771556Srgrimes * Check to see whether we are at the end of the here document. When this 10781556Srgrimes * is called, c is set to the first character of the next input line. If 10791556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 10801556Srgrimes */ 10811556Srgrimes 10821556Srgrimescheckend: { 10831556Srgrimes if (eofmark) { 10841556Srgrimes if (striptabs) { 10851556Srgrimes while (c == '\t') 10861556Srgrimes c = pgetc(); 10871556Srgrimes } 10881556Srgrimes if (c == *eofmark) { 10891556Srgrimes if (pfgets(line, sizeof line) != NULL) { 109025230Ssteve char *p, *q; 10911556Srgrimes 10921556Srgrimes p = line; 10931556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 10941556Srgrimes if (*p == '\n' && *q == '\0') { 10951556Srgrimes c = PEOF; 10961556Srgrimes plinno++; 10971556Srgrimes needprompt = doprompt; 10981556Srgrimes } else { 10991556Srgrimes pushstring(line, strlen(line), NULL); 11001556Srgrimes } 11011556Srgrimes } 11021556Srgrimes } 11031556Srgrimes } 11041556Srgrimes goto checkend_return; 11051556Srgrimes} 11061556Srgrimes 11071556Srgrimes 11081556Srgrimes/* 11091556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11101556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11111556Srgrimes * first character of the redirection operator. 11121556Srgrimes */ 11131556Srgrimes 11141556Srgrimesparseredir: { 11151556Srgrimes char fd = *out; 11161556Srgrimes union node *np; 11171556Srgrimes 11181556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11191556Srgrimes if (c == '>') { 11201556Srgrimes np->nfile.fd = 1; 11211556Srgrimes c = pgetc(); 11221556Srgrimes if (c == '>') 11231556Srgrimes np->type = NAPPEND; 11241556Srgrimes else if (c == '&') 11251556Srgrimes np->type = NTOFD; 112696922Stjr else if (c == '|') 112796922Stjr np->type = NCLOBBER; 11281556Srgrimes else { 11291556Srgrimes np->type = NTO; 11301556Srgrimes pungetc(); 11311556Srgrimes } 11321556Srgrimes } else { /* c == '<' */ 11331556Srgrimes np->nfile.fd = 0; 11341556Srgrimes c = pgetc(); 11351556Srgrimes if (c == '<') { 11361556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11371556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11381556Srgrimes np->nfile.fd = 0; 11391556Srgrimes } 11401556Srgrimes np->type = NHERE; 11411556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11421556Srgrimes heredoc->here = np; 11431556Srgrimes if ((c = pgetc()) == '-') { 11441556Srgrimes heredoc->striptabs = 1; 11451556Srgrimes } else { 11461556Srgrimes heredoc->striptabs = 0; 11471556Srgrimes pungetc(); 11481556Srgrimes } 11491556Srgrimes } else if (c == '&') 11501556Srgrimes np->type = NFROMFD; 115166612Sbrian else if (c == '>') 115266612Sbrian np->type = NFROMTO; 11531556Srgrimes else { 11541556Srgrimes np->type = NFROM; 11551556Srgrimes pungetc(); 11561556Srgrimes } 11571556Srgrimes } 11581556Srgrimes if (fd != '\0') 11591556Srgrimes np->nfile.fd = digit_val(fd); 11601556Srgrimes redirnode = np; 11611556Srgrimes goto parseredir_return; 11621556Srgrimes} 11631556Srgrimes 11641556Srgrimes 11651556Srgrimes/* 11661556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11671556Srgrimes * and nothing else. 11681556Srgrimes */ 11691556Srgrimes 11701556Srgrimesparsesub: { 1171179022Sstefanf char buf[10]; 11721556Srgrimes int subtype; 11731556Srgrimes int typeloc; 11741556Srgrimes int flags; 11751556Srgrimes char *p; 11761556Srgrimes static const char types[] = "}-+?="; 1177179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1178179022Sstefanf int i; 1179179022Sstefanf int linno; 1180179387Sstefanf int length; 11811556Srgrimes 11821556Srgrimes c = pgetc(); 1183149026Sstefanf if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && 1184149026Sstefanf !is_special(c)) { 11851556Srgrimes USTPUTC('$', out); 11861556Srgrimes pungetc(); 11871556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 11881556Srgrimes if (pgetc() == '(') { 11891556Srgrimes PARSEARITH(); 11901556Srgrimes } else { 11911556Srgrimes pungetc(); 11921556Srgrimes PARSEBACKQNEW(); 11931556Srgrimes } 11941556Srgrimes } else { 11951556Srgrimes USTPUTC(CTLVAR, out); 11961556Srgrimes typeloc = out - stackblock(); 11971556Srgrimes USTPUTC(VSNORMAL, out); 11981556Srgrimes subtype = VSNORMAL; 1199179022Sstefanf flags = 0; 12001556Srgrimes if (c == '{') { 120118202Speter bracketed_name = 1; 12021556Srgrimes c = pgetc(); 120317987Speter if (c == '#') { 120417987Speter if ((c = pgetc()) == '}') 120517987Speter c = '#'; 120617987Speter else 120717987Speter subtype = VSLENGTH; 120817987Speter } 120917987Speter else 121017987Speter subtype = 0; 12111556Srgrimes } 1212149026Sstefanf if (!is_eof(c) && is_name(c)) { 1213179387Sstefanf length = 0; 12141556Srgrimes do { 12151556Srgrimes STPUTC(c, out); 12161556Srgrimes c = pgetc(); 1217179387Sstefanf length++; 1218149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1219179387Sstefanf if (length == 6 && 1220179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1221179022Sstefanf /* Replace the variable name with the 1222179022Sstefanf * current line number. */ 1223179022Sstefanf linno = plinno; 1224179022Sstefanf if (funclinno != 0) 1225179022Sstefanf linno -= funclinno - 1; 1226179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1227179022Sstefanf STADJUST(-6, out); 1228179022Sstefanf for (i = 0; buf[i] != '\0'; i++) 1229179022Sstefanf STPUTC(buf[i], out); 1230179022Sstefanf flags |= VSLINENO; 1231179022Sstefanf } 123218202Speter } else if (is_digit(c)) { 123318202Speter if (bracketed_name) { 123418202Speter do { 123518202Speter STPUTC(c, out); 123618202Speter c = pgetc(); 123718202Speter } while (is_digit(c)); 123818202Speter } else { 123918202Speter STPUTC(c, out); 124018202Speter c = pgetc(); 124118202Speter } 12421556Srgrimes } else { 1243164003Sstefanf if (! is_special(c)) { 1244164003Sstefanf subtype = VSERROR; 1245164003Sstefanf if (c == '}') 1246164003Sstefanf pungetc(); 1247164003Sstefanf else 1248164003Sstefanf USTPUTC(c, out); 1249164003Sstefanf } else { 1250164003Sstefanf USTPUTC(c, out); 1251164003Sstefanf c = pgetc(); 1252164003Sstefanf } 12531556Srgrimes } 12541556Srgrimes if (subtype == 0) { 125517987Speter switch (c) { 125617987Speter case ':': 1257179022Sstefanf flags |= VSNUL; 12581556Srgrimes c = pgetc(); 125917987Speter /*FALLTHROUGH*/ 126017987Speter default: 126117987Speter p = strchr(types, c); 1262164003Sstefanf if (p == NULL) { 1263164003Sstefanf if (flags == VSNUL) 1264164003Sstefanf STPUTC(':', out); 1265164003Sstefanf STPUTC(c, out); 1266164003Sstefanf subtype = VSERROR; 1267164003Sstefanf } else 1268164003Sstefanf subtype = p - types + VSNORMAL; 126917987Speter break; 127017987Speter case '%': 127120425Ssteve case '#': 127217987Speter { 127317987Speter int cc = c; 127417987Speter subtype = c == '#' ? VSTRIMLEFT : 127517987Speter VSTRIMRIGHT; 127617987Speter c = pgetc(); 127717987Speter if (c == cc) 127817987Speter subtype++; 127917987Speter else 128017987Speter pungetc(); 128117987Speter break; 128217987Speter } 12831556Srgrimes } 1284164003Sstefanf } else if (subtype != VSERROR) { 12851556Srgrimes pungetc(); 12861556Srgrimes } 1287164003Sstefanf STPUTC('=', out); 128857225Scracauer if (subtype != VSLENGTH && (dblquote || arinest)) 12891556Srgrimes flags |= VSQUOTE; 12901556Srgrimes *(stackblock() + typeloc) = subtype | flags; 12911556Srgrimes if (subtype != VSNORMAL) 12921556Srgrimes varnest++; 12931556Srgrimes } 12941556Srgrimes goto parsesub_return; 12951556Srgrimes} 12961556Srgrimes 12971556Srgrimes 12981556Srgrimes/* 12991556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 13001556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 13011556Srgrimes * list of commands (passed by reference), and savelen is the number of 13021556Srgrimes * characters on the top of the stack which must be preserved. 13031556Srgrimes */ 13041556Srgrimes 13051556Srgrimesparsebackq: { 13061556Srgrimes struct nodelist **nlpp; 13071556Srgrimes union node *n; 13081556Srgrimes char *volatile str; 13091556Srgrimes struct jmploc jmploc; 1310194765Sjilles struct jmploc *const savehandler = handler; 13111556Srgrimes int savelen; 131220425Ssteve int saveprompt; 1313198173Sjilles const int bq_startlinno = plinno; 13141556Srgrimes 13151556Srgrimes if (setjmp(jmploc.loc)) { 13161556Srgrimes if (str) 13171556Srgrimes ckfree(str); 13181556Srgrimes handler = savehandler; 1319198173Sjilles if (exception == EXERROR) { 1320198173Sjilles startlinno = bq_startlinno; 1321198173Sjilles synerror("Error in command substitution"); 1322198173Sjilles } 13231556Srgrimes longjmp(handler->loc, 1); 13241556Srgrimes } 13251556Srgrimes INTOFF; 13261556Srgrimes str = NULL; 13271556Srgrimes savelen = out - stackblock(); 13281556Srgrimes if (savelen > 0) { 13291556Srgrimes str = ckmalloc(savelen); 133017987Speter memcpy(str, stackblock(), savelen); 13311556Srgrimes } 13321556Srgrimes handler = &jmploc; 13331556Srgrimes INTON; 13341556Srgrimes if (oldstyle) { 13351556Srgrimes /* We must read until the closing backquote, giving special 13361556Srgrimes treatment to some slashes, and then push the string and 13371556Srgrimes reread it as input, interpreting it normally. */ 133825230Ssteve char *out; 133925230Ssteve int c; 13401556Srgrimes int savelen; 13411556Srgrimes char *str; 13428855Srgrimes 134320425Ssteve 13441556Srgrimes STARTSTACKSTR(out); 134520425Ssteve for (;;) { 134620425Ssteve if (needprompt) { 134720425Ssteve setprompt(2); 134820425Ssteve needprompt = 0; 134920425Ssteve } 135020425Ssteve switch (c = pgetc()) { 135120425Ssteve case '`': 135220425Ssteve goto done; 135320425Ssteve 135420425Ssteve case '\\': 135520425Ssteve if ((c = pgetc()) == '\n') { 135620425Ssteve plinno++; 135720425Ssteve if (doprompt) 135820425Ssteve setprompt(2); 135920425Ssteve else 136020425Ssteve setprompt(0); 136120425Ssteve /* 136220425Ssteve * If eating a newline, avoid putting 136320425Ssteve * the newline into the new character 136420425Ssteve * stream (via the STPUTC after the 136520425Ssteve * switch). 136620425Ssteve */ 136720425Ssteve continue; 136820425Ssteve } 136917987Speter if (c != '\\' && c != '`' && c != '$' 13701556Srgrimes && (!dblquote || c != '"')) 13711556Srgrimes STPUTC('\\', out); 137220425Ssteve break; 137320425Ssteve 137420425Ssteve case '\n': 137520425Ssteve plinno++; 137620425Ssteve needprompt = doprompt; 137720425Ssteve break; 137820425Ssteve 137920425Ssteve case PEOF: 138020425Ssteve startlinno = plinno; 138120425Ssteve synerror("EOF in backquote substitution"); 138220425Ssteve break; 138320425Ssteve 138420425Ssteve default: 138520425Ssteve break; 138620425Ssteve } 138720425Ssteve STPUTC(c, out); 13881556Srgrimes } 138920425Sstevedone: 13901556Srgrimes STPUTC('\0', out); 13911556Srgrimes savelen = out - stackblock(); 13921556Srgrimes if (savelen > 0) { 13931556Srgrimes str = ckmalloc(savelen); 139417987Speter memcpy(str, stackblock(), savelen); 139517987Speter setinputstring(str, 1); 13961556Srgrimes } 13971556Srgrimes } 13981556Srgrimes nlpp = &bqlist; 13991556Srgrimes while (*nlpp) 14001556Srgrimes nlpp = &(*nlpp)->next; 14011556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 14021556Srgrimes (*nlpp)->next = NULL; 140320425Ssteve 140420425Ssteve if (oldstyle) { 140520425Ssteve saveprompt = doprompt; 140620425Ssteve doprompt = 0; 140720425Ssteve } 140820425Ssteve 14091556Srgrimes n = list(0); 141020425Ssteve 141120425Ssteve if (oldstyle) 141220425Ssteve doprompt = saveprompt; 141320425Ssteve else { 141420425Ssteve if (readtoken() != TRP) 141520425Ssteve synexpect(TRP); 141620425Ssteve } 141720425Ssteve 14181556Srgrimes (*nlpp)->n = n; 141920425Ssteve if (oldstyle) { 142020425Ssteve /* 142120425Ssteve * Start reading from old file again, ignoring any pushed back 142220425Ssteve * tokens left from the backquote parsing 142320425Ssteve */ 14241556Srgrimes popfile(); 142520425Ssteve tokpushback = 0; 142620425Ssteve } 14271556Srgrimes while (stackblocksize() <= savelen) 14281556Srgrimes growstackblock(); 14291556Srgrimes STARTSTACKSTR(out); 14301556Srgrimes if (str) { 143117987Speter memcpy(out, str, savelen); 14321556Srgrimes STADJUST(savelen, out); 14331556Srgrimes INTOFF; 14341556Srgrimes ckfree(str); 14351556Srgrimes str = NULL; 14361556Srgrimes INTON; 14371556Srgrimes } 14381556Srgrimes handler = savehandler; 14391556Srgrimes if (arinest || dblquote) 14401556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14411556Srgrimes else 14421556Srgrimes USTPUTC(CTLBACKQ, out); 14431556Srgrimes if (oldstyle) 14441556Srgrimes goto parsebackq_oldreturn; 14451556Srgrimes else 14461556Srgrimes goto parsebackq_newreturn; 14471556Srgrimes} 14481556Srgrimes 14491556Srgrimes/* 14501556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14511556Srgrimes */ 14521556Srgrimesparsearith: { 14531556Srgrimes 14541556Srgrimes if (++arinest == 1) { 14551556Srgrimes prevsyntax = syntax; 14561556Srgrimes syntax = ARISYNTAX; 14571556Srgrimes USTPUTC(CTLARI, out); 145838887Stegge if (dblquote) 145938887Stegge USTPUTC('"',out); 146038887Stegge else 146138887Stegge USTPUTC(' ',out); 14621556Srgrimes } else { 14631556Srgrimes /* 14641556Srgrimes * we collapse embedded arithmetic expansion to 14651556Srgrimes * parenthesis, which should be equivalent 14661556Srgrimes */ 14671556Srgrimes USTPUTC('(', out); 14681556Srgrimes } 14691556Srgrimes goto parsearith_return; 14701556Srgrimes} 14711556Srgrimes 14721556Srgrimes} /* end of readtoken */ 14731556Srgrimes 14741556Srgrimes 14751556Srgrimes 14761556Srgrimes#ifdef mkinit 14771556SrgrimesRESET { 14781556Srgrimes tokpushback = 0; 14791556Srgrimes checkkwd = 0; 14801556Srgrimes} 14811556Srgrimes#endif 14821556Srgrimes 14831556Srgrimes/* 14841556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 14851556Srgrimes * or backquotes). 14861556Srgrimes */ 14871556Srgrimes 14881556SrgrimesSTATIC int 148990111Simpnoexpand(char *text) 149090111Simp{ 149125230Ssteve char *p; 149225230Ssteve char c; 14931556Srgrimes 14941556Srgrimes p = text; 14951556Srgrimes while ((c = *p++) != '\0') { 149639137Stegge if ( c == CTLQUOTEMARK) 149739137Stegge continue; 14981556Srgrimes if (c == CTLESC) 14991556Srgrimes p++; 150083675Stegge else if (BASESYNTAX[(int)c] == CCTL) 15011556Srgrimes return 0; 15021556Srgrimes } 15031556Srgrimes return 1; 15041556Srgrimes} 15051556Srgrimes 15061556Srgrimes 15071556Srgrimes/* 15081556Srgrimes * Return true if the argument is a legal variable name (a letter or 15091556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 15101556Srgrimes */ 15111556Srgrimes 15121556Srgrimesint 151390111Simpgoodname(char *name) 151490111Simp{ 151525230Ssteve char *p; 15161556Srgrimes 15171556Srgrimes p = name; 15181556Srgrimes if (! is_name(*p)) 15191556Srgrimes return 0; 15201556Srgrimes while (*++p) { 15211556Srgrimes if (! is_in_name(*p)) 15221556Srgrimes return 0; 15231556Srgrimes } 15241556Srgrimes return 1; 15251556Srgrimes} 15261556Srgrimes 15271556Srgrimes 15281556Srgrimes/* 15291556Srgrimes * Called when an unexpected token is read during the parse. The argument 15301556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15311556Srgrimes * occur at this point. 15321556Srgrimes */ 15331556Srgrimes 15341556SrgrimesSTATIC void 153590111Simpsynexpect(int token) 153617987Speter{ 15371556Srgrimes char msg[64]; 15381556Srgrimes 15391556Srgrimes if (token >= 0) { 15401556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15411556Srgrimes tokname[lasttoken], tokname[token]); 15421556Srgrimes } else { 15431556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15441556Srgrimes } 15451556Srgrimes synerror(msg); 15461556Srgrimes} 15471556Srgrimes 15481556Srgrimes 15491556SrgrimesSTATIC void 155090111Simpsynerror(char *msg) 155190111Simp{ 15521556Srgrimes if (commandname) 15531556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15541556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15551556Srgrimes error((char *)NULL); 15561556Srgrimes} 15571556Srgrimes 15581556SrgrimesSTATIC void 155990111Simpsetprompt(int which) 156090111Simp{ 15611556Srgrimes whichprompt = which; 15621556Srgrimes 156317987Speter#ifndef NO_HISTORY 15641556Srgrimes if (!el) 156517987Speter#endif 15661556Srgrimes out2str(getprompt(NULL)); 15671556Srgrimes} 15681556Srgrimes 15691556Srgrimes/* 15701556Srgrimes * called by editline -- any expansions to the prompt 15711556Srgrimes * should be added here. 15721556Srgrimes */ 15731556Srgrimeschar * 157490111Simpgetprompt(void *unused __unused) 157525905Ssteve{ 1576142845Sobrien static char ps[PROMPTLEN]; 1577142845Sobrien char *fmt; 1578142845Sobrien int i, j, trim; 1579142845Sobrien 1580142845Sobrien /* 1581142845Sobrien * Select prompt format. 1582142845Sobrien */ 15831556Srgrimes switch (whichprompt) { 15841556Srgrimes case 0: 1585142845Sobrien fmt = ""; 1586142845Sobrien break; 15871556Srgrimes case 1: 1588142845Sobrien fmt = ps1val(); 1589142845Sobrien break; 15901556Srgrimes case 2: 1591142845Sobrien fmt = ps2val(); 1592142845Sobrien break; 15931556Srgrimes default: 15941556Srgrimes return "<internal prompt error>"; 15951556Srgrimes } 1596142845Sobrien 1597142845Sobrien /* 1598142845Sobrien * Format prompt string. 1599142845Sobrien */ 1600142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1601142845Sobrien if (*fmt == '\\') 1602142845Sobrien switch (*++fmt) { 1603142845Sobrien 1604142845Sobrien /* 1605142845Sobrien * Hostname. 1606142845Sobrien * 1607142845Sobrien * \h specifies just the local hostname, 1608142845Sobrien * \H specifies fully-qualified hostname. 1609142845Sobrien */ 1610142845Sobrien case 'h': 1611142845Sobrien case 'H': 1612149024Sstefanf ps[i] = '\0'; 1613142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1614142845Sobrien /* Skip to end of hostname. */ 1615142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1616142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1617142845Sobrien i++; 1618142845Sobrien break; 1619142845Sobrien 1620142845Sobrien /* 1621142845Sobrien * Working directory. 1622142845Sobrien * 1623142845Sobrien * \W specifies just the final component, 1624142845Sobrien * \w specifies the entire path. 1625142845Sobrien */ 1626142845Sobrien case 'W': 1627142845Sobrien case 'w': 1628149024Sstefanf ps[i] = '\0'; 1629142845Sobrien getcwd(&ps[i], PROMPTLEN - i); 1630142845Sobrien if (*fmt == 'W') { 1631142845Sobrien /* Final path component only. */ 1632142845Sobrien trim = 1; 1633142845Sobrien for (j = i; ps[j] != '\0'; j++) 1634142845Sobrien if (ps[j] == '/') 1635142845Sobrien trim = j + 1; 1636142845Sobrien memmove(&ps[i], &ps[trim], 1637142845Sobrien j - trim + 1); 1638142845Sobrien } 1639142845Sobrien /* Skip to end of path. */ 1640142845Sobrien while (ps[i + 1] != '\0') 1641142845Sobrien i++; 1642142845Sobrien break; 1643142845Sobrien 1644142845Sobrien /* 1645142845Sobrien * Superuser status. 1646142845Sobrien * 1647142845Sobrien * '$' for normal users, '#' for root. 1648142845Sobrien */ 1649142845Sobrien case '$': 1650142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1651142845Sobrien break; 1652142845Sobrien 1653142845Sobrien /* 1654142845Sobrien * A literal \. 1655142845Sobrien */ 1656142845Sobrien case '\\': 1657142845Sobrien ps[i] = '\\'; 1658142845Sobrien break; 1659142845Sobrien 1660142845Sobrien /* 1661142845Sobrien * Emit unrecognized formats verbatim. 1662142845Sobrien */ 1663142845Sobrien default: 1664142845Sobrien ps[i++] = '\\'; 1665142845Sobrien ps[i] = *fmt; 1666142845Sobrien break; 1667142845Sobrien } 1668142845Sobrien else 1669142845Sobrien ps[i] = *fmt; 1670142845Sobrien ps[i] = '\0'; 1671142845Sobrien return (ps); 16721556Srgrimes} 1673