parser.c revision 179387
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 179387 2008-05-28 21:44:32Z 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 */ 97179022SstefanfSTATIC int funclinno; /* line # where the current function started */ 981556Srgrimes 9918018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 10018018Speterstatic int noaliases = 0; 1011556Srgrimes 1021556Srgrimes 10390111SimpSTATIC union node *list(int); 10490111SimpSTATIC union node *andor(void); 10590111SimpSTATIC union node *pipeline(void); 10690111SimpSTATIC union node *command(void); 10790111SimpSTATIC union node *simplecmd(union node **, union node *); 10890111SimpSTATIC union node *makename(void); 10990111SimpSTATIC void parsefname(void); 11090111SimpSTATIC void parseheredoc(void); 11190111SimpSTATIC int peektoken(void); 11290111SimpSTATIC int readtoken(void); 11390111SimpSTATIC int xxreadtoken(void); 11490111SimpSTATIC int readtoken1(int, char const *, char *, int); 11590111SimpSTATIC int noexpand(char *); 11690111SimpSTATIC void synexpect(int); 11790111SimpSTATIC void synerror(char *); 11890111SimpSTATIC void setprompt(int); 1191556Srgrimes 12017987Speter 1211556Srgrimes/* 1221556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1231556Srgrimes * valid parse tree indicating a blank line.) 1241556Srgrimes */ 1251556Srgrimes 1261556Srgrimesunion node * 12790111Simpparsecmd(int interact) 12817987Speter{ 1291556Srgrimes int t; 1301556Srgrimes 13160593Scracauer tokpushback = 0; 1321556Srgrimes doprompt = interact; 1331556Srgrimes if (doprompt) 1341556Srgrimes setprompt(1); 1351556Srgrimes else 1361556Srgrimes setprompt(0); 1371556Srgrimes needprompt = 0; 1381556Srgrimes t = readtoken(); 1391556Srgrimes if (t == TEOF) 1401556Srgrimes return NEOF; 1411556Srgrimes if (t == TNL) 1421556Srgrimes return NULL; 1431556Srgrimes tokpushback++; 1441556Srgrimes return list(1); 1451556Srgrimes} 1461556Srgrimes 1471556Srgrimes 1481556SrgrimesSTATIC union node * 14990111Simplist(int nlflag) 15017987Speter{ 1511556Srgrimes union node *n1, *n2, *n3; 15217987Speter int tok; 1531556Srgrimes 1541556Srgrimes checkkwd = 2; 1551556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 1561556Srgrimes return NULL; 15717987Speter n1 = NULL; 1581556Srgrimes for (;;) { 15917987Speter n2 = andor(); 16017987Speter tok = readtoken(); 16117987Speter if (tok == TBACKGND) { 16217987Speter if (n2->type == NCMD || n2->type == NPIPE) { 16317987Speter n2->ncmd.backgnd = 1; 16417987Speter } else if (n2->type == NREDIR) { 16517987Speter n2->type = NBACKGND; 16617987Speter } else { 16717987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 16817987Speter n3->type = NBACKGND; 16917987Speter n3->nredir.n = n2; 17017987Speter n3->nredir.redirect = NULL; 17117987Speter n2 = n3; 17217987Speter } 17317987Speter } 17417987Speter if (n1 == NULL) { 17517987Speter n1 = n2; 17617987Speter } 17717987Speter else { 17817987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 17917987Speter n3->type = NSEMI; 18017987Speter n3->nbinary.ch1 = n1; 18117987Speter n3->nbinary.ch2 = n2; 18217987Speter n1 = n3; 18317987Speter } 18417987Speter switch (tok) { 18513882Sjoerg case TBACKGND: 18617987Speter case TSEMI: 18717987Speter tok = readtoken(); 188102410Scharnier /* FALLTHROUGH */ 1891556Srgrimes case TNL: 19017987Speter if (tok == TNL) { 19117987Speter parseheredoc(); 19217987Speter if (nlflag) 19317987Speter return n1; 19417987Speter } else { 19517987Speter tokpushback++; 19617987Speter } 1971556Srgrimes checkkwd = 2; 1981556Srgrimes if (tokendlist[peektoken()]) 1991556Srgrimes return n1; 2001556Srgrimes break; 2011556Srgrimes case TEOF: 2021556Srgrimes if (heredoclist) 2031556Srgrimes parseheredoc(); 2041556Srgrimes else 2051556Srgrimes pungetc(); /* push back EOF on input */ 2061556Srgrimes return n1; 2071556Srgrimes default: 2081556Srgrimes if (nlflag) 2091556Srgrimes synexpect(-1); 2101556Srgrimes tokpushback++; 2111556Srgrimes return n1; 2121556Srgrimes } 2131556Srgrimes } 2141556Srgrimes} 2151556Srgrimes 2161556Srgrimes 2171556Srgrimes 2181556SrgrimesSTATIC union node * 21990111Simpandor(void) 22090111Simp{ 2211556Srgrimes union node *n1, *n2, *n3; 2221556Srgrimes int t; 2231556Srgrimes 2241556Srgrimes n1 = pipeline(); 2251556Srgrimes for (;;) { 2261556Srgrimes if ((t = readtoken()) == TAND) { 2271556Srgrimes t = NAND; 2281556Srgrimes } else if (t == TOR) { 2291556Srgrimes t = NOR; 2301556Srgrimes } else { 2311556Srgrimes tokpushback++; 2321556Srgrimes return n1; 2331556Srgrimes } 2341556Srgrimes n2 = pipeline(); 2351556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2361556Srgrimes n3->type = t; 2371556Srgrimes n3->nbinary.ch1 = n1; 2381556Srgrimes n3->nbinary.ch2 = n2; 2391556Srgrimes n1 = n3; 2401556Srgrimes } 2411556Srgrimes} 2421556Srgrimes 2431556Srgrimes 2441556Srgrimes 2451556SrgrimesSTATIC union node * 24690111Simppipeline(void) 24790111Simp{ 24875336Sbrian union node *n1, *n2, *pipenode; 2491556Srgrimes struct nodelist *lp, *prev; 25075336Sbrian int negate; 2511556Srgrimes 25275336Sbrian negate = 0; 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; 3671556Srgrimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3681556Srgrimes app = ≈ 3691556Srgrimes while (readtoken() == TWORD) { 3701556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3711556Srgrimes n2->type = NARG; 3721556Srgrimes n2->narg.text = wordtext; 3731556Srgrimes n2->narg.backquote = backquotelist; 3741556Srgrimes *app = n2; 3751556Srgrimes app = &n2->narg.next; 3761556Srgrimes } 3771556Srgrimes *app = NULL; 3781556Srgrimes n1->nfor.args = ap; 3791556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3801556Srgrimes synexpect(-1); 3811556Srgrimes } else { 382149096Sstefanf static char argvars[5] = { 383149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 384149096Sstefanf }; 3851556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3861556Srgrimes n2->type = NARG; 387149096Sstefanf n2->narg.text = argvars; 3881556Srgrimes n2->narg.backquote = NULL; 3891556Srgrimes n2->narg.next = NULL; 3901556Srgrimes n1->nfor.args = n2; 3911556Srgrimes /* 3921556Srgrimes * Newline or semicolon here is optional (but note 3931556Srgrimes * that the original Bourne shell only allowed NL). 3941556Srgrimes */ 3951556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3961556Srgrimes tokpushback++; 3971556Srgrimes } 3981556Srgrimes checkkwd = 2; 3991556Srgrimes if ((t = readtoken()) == TDO) 4001556Srgrimes t = TDONE; 4011556Srgrimes else if (t == TBEGIN) 4021556Srgrimes t = TEND; 4031556Srgrimes else 4041556Srgrimes synexpect(-1); 4051556Srgrimes n1->nfor.body = list(0); 4061556Srgrimes if (readtoken() != t) 4071556Srgrimes synexpect(t); 4081556Srgrimes checkkwd = 1; 4091556Srgrimes break; 4101556Srgrimes case TCASE: 4111556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4121556Srgrimes n1->type = NCASE; 4131556Srgrimes if (readtoken() != TWORD) 4141556Srgrimes synexpect(TWORD); 4151556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4161556Srgrimes n2->type = NARG; 4171556Srgrimes n2->narg.text = wordtext; 4181556Srgrimes n2->narg.backquote = backquotelist; 4191556Srgrimes n2->narg.next = NULL; 4201556Srgrimes while (readtoken() == TNL); 4211556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4221556Srgrimes synerror("expecting \"in\""); 4231556Srgrimes cpp = &n1->ncase.cases; 42418018Speter noaliases = 1; /* turn off alias expansion */ 4252760Ssef checkkwd = 2, readtoken(); 426104202Stjr while (lasttoken != TESAC) { 4271556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4281556Srgrimes cp->type = NCLIST; 4291556Srgrimes app = &cp->nclist.pattern; 430104207Stjr if (lasttoken == TLP) 431104207Stjr readtoken(); 4321556Srgrimes for (;;) { 4331556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4341556Srgrimes ap->type = NARG; 4351556Srgrimes ap->narg.text = wordtext; 4361556Srgrimes ap->narg.backquote = backquotelist; 4372760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4381556Srgrimes break; 4391556Srgrimes app = &ap->narg.next; 4402760Ssef readtoken(); 4411556Srgrimes } 4421556Srgrimes ap->narg.next = NULL; 4431556Srgrimes if (lasttoken != TRP) 44418018Speter noaliases = 0, synexpect(TRP); 4451556Srgrimes cp->nclist.body = list(0); 4462760Ssef 4472760Ssef checkkwd = 2; 4482760Ssef if ((t = readtoken()) != TESAC) { 4492760Ssef if (t != TENDCASE) 45018018Speter noaliases = 0, synexpect(TENDCASE); 4512760Ssef else 4522760Ssef checkkwd = 2, readtoken(); 4532760Ssef } 4541556Srgrimes cpp = &cp->nclist.next; 455104202Stjr } 45618018Speter noaliases = 0; /* reset alias expansion */ 4571556Srgrimes *cpp = NULL; 4581556Srgrimes checkkwd = 1; 4591556Srgrimes break; 4601556Srgrimes case TLP: 4611556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4621556Srgrimes n1->type = NSUBSHELL; 4631556Srgrimes n1->nredir.n = list(0); 4641556Srgrimes n1->nredir.redirect = NULL; 4651556Srgrimes if (readtoken() != TRP) 4661556Srgrimes synexpect(TRP); 4671556Srgrimes checkkwd = 1; 4681556Srgrimes break; 4691556Srgrimes case TBEGIN: 4701556Srgrimes n1 = list(0); 4711556Srgrimes if (readtoken() != TEND) 4721556Srgrimes synexpect(TEND); 4731556Srgrimes checkkwd = 1; 4741556Srgrimes break; 4751556Srgrimes /* Handle an empty command like other simple commands. */ 47617987Speter case TSEMI: 477101662Stjr case TAND: 478101662Stjr case TOR: 47917987Speter /* 48017987Speter * An empty command before a ; doesn't make much sense, and 48117987Speter * should certainly be disallowed in the case of `if ;'. 48217987Speter */ 48317987Speter if (!redir) 48417987Speter synexpect(-1); 4851556Srgrimes case TNL: 48610399Sjoerg case TEOF: 4871556Srgrimes case TWORD: 48817987Speter case TRP: 4891556Srgrimes tokpushback++; 49075160Sbrian n1 = simplecmd(rpp, redir); 49175160Sbrian goto checkneg; 4921556Srgrimes default: 4931556Srgrimes synexpect(-1); 4941556Srgrimes } 4951556Srgrimes 4961556Srgrimes /* Now check for redirection which may follow command */ 4971556Srgrimes while (readtoken() == TREDIR) { 4981556Srgrimes *rpp = n2 = redirnode; 4991556Srgrimes rpp = &n2->nfile.next; 5001556Srgrimes parsefname(); 5011556Srgrimes } 5021556Srgrimes tokpushback++; 5031556Srgrimes *rpp = NULL; 5041556Srgrimes if (redir) { 5051556Srgrimes if (n1->type != NSUBSHELL) { 5061556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5071556Srgrimes n2->type = NREDIR; 5081556Srgrimes n2->nredir.n = n1; 5091556Srgrimes n1 = n2; 5101556Srgrimes } 5111556Srgrimes n1->nredir.redirect = redir; 5121556Srgrimes } 51375160Sbrian 51475160Sbriancheckneg: 51575160Sbrian if (negate) { 51675160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 51775160Sbrian n2->type = NNOT; 51875160Sbrian n2->nnot.com = n1; 51975160Sbrian return n2; 52075160Sbrian } 52175160Sbrian else 52275160Sbrian return n1; 5231556Srgrimes} 5241556Srgrimes 5251556Srgrimes 5261556SrgrimesSTATIC union node * 52790111Simpsimplecmd(union node **rpp, union node *redir) 52890111Simp{ 5291556Srgrimes union node *args, **app; 5301556Srgrimes union node **orig_rpp = rpp; 53175160Sbrian union node *n = NULL, *n2; 53275160Sbrian int negate = 0; 5331556Srgrimes 5341556Srgrimes /* If we don't have any redirections already, then we must reset */ 5351556Srgrimes /* rpp to be the address of the local redir variable. */ 5361556Srgrimes if (redir == 0) 5371556Srgrimes rpp = &redir; 5381556Srgrimes 5391556Srgrimes args = NULL; 5401556Srgrimes app = &args; 5418855Srgrimes /* 5421556Srgrimes * We save the incoming value, because we need this for shell 5431556Srgrimes * functions. There can not be a redirect or an argument between 5448855Srgrimes * the function name and the open parenthesis. 5451556Srgrimes */ 5461556Srgrimes orig_rpp = rpp; 5471556Srgrimes 54875160Sbrian while (readtoken() == TNOT) { 54975160Sbrian TRACE(("command: TNOT recognized\n")); 55075160Sbrian negate = !negate; 55175160Sbrian } 55275160Sbrian tokpushback++; 55375160Sbrian 5541556Srgrimes for (;;) { 5551556Srgrimes if (readtoken() == TWORD) { 5561556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5571556Srgrimes n->type = NARG; 5581556Srgrimes n->narg.text = wordtext; 5591556Srgrimes n->narg.backquote = backquotelist; 5601556Srgrimes *app = n; 5611556Srgrimes app = &n->narg.next; 5621556Srgrimes } else if (lasttoken == TREDIR) { 5631556Srgrimes *rpp = n = redirnode; 5641556Srgrimes rpp = &n->nfile.next; 5651556Srgrimes parsefname(); /* read name of redirection file */ 5661556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5671556Srgrimes && rpp == orig_rpp) { 5681556Srgrimes /* We have a function */ 5691556Srgrimes if (readtoken() != TRP) 5701556Srgrimes synexpect(TRP); 571179022Sstefanf funclinno = plinno; 5721556Srgrimes#ifdef notdef 5731556Srgrimes if (! goodname(n->narg.text)) 5741556Srgrimes synerror("Bad function name"); 5751556Srgrimes#endif 5761556Srgrimes n->type = NDEFUN; 5771556Srgrimes n->narg.next = command(); 578179022Sstefanf funclinno = 0; 57975160Sbrian goto checkneg; 5801556Srgrimes } else { 5811556Srgrimes tokpushback++; 5821556Srgrimes break; 5831556Srgrimes } 5841556Srgrimes } 5851556Srgrimes *app = NULL; 5861556Srgrimes *rpp = NULL; 5871556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5881556Srgrimes n->type = NCMD; 5891556Srgrimes n->ncmd.backgnd = 0; 5901556Srgrimes n->ncmd.args = args; 5911556Srgrimes n->ncmd.redirect = redir; 59275160Sbrian 59375160Sbriancheckneg: 59475160Sbrian if (negate) { 59575160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 59675160Sbrian n2->type = NNOT; 59775160Sbrian n2->nnot.com = n; 59875160Sbrian return n2; 59975160Sbrian } 60075160Sbrian else 60175160Sbrian return n; 6021556Srgrimes} 6031556Srgrimes 60417987SpeterSTATIC union node * 60590111Simpmakename(void) 60690111Simp{ 60717987Speter union node *n; 6081556Srgrimes 60917987Speter n = (union node *)stalloc(sizeof (struct narg)); 61017987Speter n->type = NARG; 61117987Speter n->narg.next = NULL; 61217987Speter n->narg.text = wordtext; 61317987Speter n->narg.backquote = backquotelist; 61417987Speter return n; 61517987Speter} 61617987Speter 61790111Simpvoid fixredir(union node *n, const char *text, int err) 61890111Simp{ 61917987Speter TRACE(("Fix redir %s %d\n", text, err)); 62017987Speter if (!err) 62117987Speter n->ndup.vname = NULL; 62217987Speter 62317987Speter if (is_digit(text[0]) && text[1] == '\0') 62417987Speter n->ndup.dupfd = digit_val(text[0]); 62517987Speter else if (text[0] == '-' && text[1] == '\0') 62617987Speter n->ndup.dupfd = -1; 62717987Speter else { 62820425Ssteve 62917987Speter if (err) 63017987Speter synerror("Bad fd number"); 63117987Speter else 63217987Speter n->ndup.vname = makename(); 63317987Speter } 63417987Speter} 63517987Speter 63617987Speter 6371556SrgrimesSTATIC void 63890111Simpparsefname(void) 63990111Simp{ 6401556Srgrimes union node *n = redirnode; 6411556Srgrimes 6421556Srgrimes if (readtoken() != TWORD) 6431556Srgrimes synexpect(-1); 6441556Srgrimes if (n->type == NHERE) { 6451556Srgrimes struct heredoc *here = heredoc; 6461556Srgrimes struct heredoc *p; 6471556Srgrimes int i; 6481556Srgrimes 6491556Srgrimes if (quoteflag == 0) 6501556Srgrimes n->type = NXHERE; 6511556Srgrimes TRACE(("Here document %d\n", n->type)); 6521556Srgrimes if (here->striptabs) { 6531556Srgrimes while (*wordtext == '\t') 6541556Srgrimes wordtext++; 6551556Srgrimes } 6561556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6571556Srgrimes synerror("Illegal eof marker for << redirection"); 6581556Srgrimes rmescapes(wordtext); 6591556Srgrimes here->eofmark = wordtext; 6601556Srgrimes here->next = NULL; 6611556Srgrimes if (heredoclist == NULL) 6621556Srgrimes heredoclist = here; 6631556Srgrimes else { 6641556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6651556Srgrimes p->next = here; 6661556Srgrimes } 6671556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 66817987Speter fixredir(n, wordtext, 0); 6691556Srgrimes } else { 67017987Speter n->nfile.fname = makename(); 6711556Srgrimes } 6721556Srgrimes} 6731556Srgrimes 6741556Srgrimes 6751556Srgrimes/* 6761556Srgrimes * Input any here documents. 6771556Srgrimes */ 6781556Srgrimes 6791556SrgrimesSTATIC void 68090111Simpparseheredoc(void) 68190111Simp{ 6821556Srgrimes struct heredoc *here; 6831556Srgrimes union node *n; 6841556Srgrimes 6851556Srgrimes while (heredoclist) { 6861556Srgrimes here = heredoclist; 6871556Srgrimes heredoclist = here->next; 6881556Srgrimes if (needprompt) { 6891556Srgrimes setprompt(2); 6901556Srgrimes needprompt = 0; 6911556Srgrimes } 6921556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6931556Srgrimes here->eofmark, here->striptabs); 6941556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6951556Srgrimes n->narg.type = NARG; 6961556Srgrimes n->narg.next = NULL; 6971556Srgrimes n->narg.text = wordtext; 6981556Srgrimes n->narg.backquote = backquotelist; 6991556Srgrimes here->here->nhere.doc = n; 7001556Srgrimes } 7011556Srgrimes} 7021556Srgrimes 7031556SrgrimesSTATIC int 70490111Simppeektoken(void) 70590111Simp{ 7061556Srgrimes int t; 7071556Srgrimes 7081556Srgrimes t = readtoken(); 7091556Srgrimes tokpushback++; 7101556Srgrimes return (t); 7111556Srgrimes} 7121556Srgrimes 7131556SrgrimesSTATIC int 71490111Simpreadtoken(void) 71590111Simp{ 7161556Srgrimes int t; 7171556Srgrimes int savecheckkwd = checkkwd; 7181556Srgrimes struct alias *ap; 7191556Srgrimes#ifdef DEBUG 7201556Srgrimes int alreadyseen = tokpushback; 7211556Srgrimes#endif 7228855Srgrimes 7231556Srgrimes top: 7241556Srgrimes t = xxreadtoken(); 7251556Srgrimes 7261556Srgrimes if (checkkwd) { 7271556Srgrimes /* 7281556Srgrimes * eat newlines 7291556Srgrimes */ 7301556Srgrimes if (checkkwd == 2) { 7311556Srgrimes checkkwd = 0; 7321556Srgrimes while (t == TNL) { 7331556Srgrimes parseheredoc(); 7341556Srgrimes t = xxreadtoken(); 7351556Srgrimes } 7361556Srgrimes } else 7371556Srgrimes checkkwd = 0; 7381556Srgrimes /* 7391556Srgrimes * check for keywords and aliases 7401556Srgrimes */ 74120425Ssteve if (t == TWORD && !quoteflag) 74217987Speter { 74398463Sjmallett const char * const *pp; 7441556Srgrimes 74598463Sjmallett for (pp = parsekwd; *pp; pp++) { 74620425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 74717987Speter { 7481556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7491556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7501556Srgrimes goto out; 7511556Srgrimes } 7521556Srgrimes } 75318018Speter if (noaliases == 0 && 75418018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7551556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7561556Srgrimes checkkwd = savecheckkwd; 7571556Srgrimes goto top; 7581556Srgrimes } 7591556Srgrimes } 7601556Srgrimesout: 76175160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 7621556Srgrimes } 7631556Srgrimes#ifdef DEBUG 7641556Srgrimes if (!alreadyseen) 7651556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7661556Srgrimes else 7671556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7681556Srgrimes#endif 7691556Srgrimes return (t); 7701556Srgrimes} 7711556Srgrimes 7721556Srgrimes 7731556Srgrimes/* 7741556Srgrimes * Read the next input token. 7751556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7761556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7771556Srgrimes * quoted. 7781556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7791556Srgrimes * the redirection. 7801556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7811556Srgrimes * on which the token starts. 7821556Srgrimes * 7831556Srgrimes * [Change comment: here documents and internal procedures] 7841556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7851556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7861556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7871556Srgrimes * We could also make parseoperator in essence the main routine, and 7881556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7891556Srgrimes */ 7901556Srgrimes 7911556Srgrimes#define RETURN(token) return lasttoken = token 7921556Srgrimes 7931556SrgrimesSTATIC int 79490111Simpxxreadtoken(void) 79590111Simp{ 79625230Ssteve int c; 7971556Srgrimes 7981556Srgrimes if (tokpushback) { 7991556Srgrimes tokpushback = 0; 8001556Srgrimes return lasttoken; 8011556Srgrimes } 8021556Srgrimes if (needprompt) { 8031556Srgrimes setprompt(2); 8041556Srgrimes needprompt = 0; 8051556Srgrimes } 8061556Srgrimes startlinno = plinno; 8071556Srgrimes for (;;) { /* until token or start of word found */ 8081556Srgrimes c = pgetc_macro(); 8091556Srgrimes if (c == ' ' || c == '\t') 8101556Srgrimes continue; /* quick check for white space first */ 8111556Srgrimes switch (c) { 8121556Srgrimes case ' ': case '\t': 8131556Srgrimes continue; 8141556Srgrimes case '#': 8151556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8161556Srgrimes pungetc(); 8171556Srgrimes continue; 8181556Srgrimes case '\\': 8191556Srgrimes if (pgetc() == '\n') { 8201556Srgrimes startlinno = ++plinno; 8211556Srgrimes if (doprompt) 8221556Srgrimes setprompt(2); 8231556Srgrimes else 8241556Srgrimes setprompt(0); 8251556Srgrimes continue; 8261556Srgrimes } 8271556Srgrimes pungetc(); 8281556Srgrimes goto breakloop; 8291556Srgrimes case '\n': 8301556Srgrimes plinno++; 8311556Srgrimes needprompt = doprompt; 8321556Srgrimes RETURN(TNL); 8331556Srgrimes case PEOF: 8341556Srgrimes RETURN(TEOF); 8351556Srgrimes case '&': 8361556Srgrimes if (pgetc() == '&') 8371556Srgrimes RETURN(TAND); 8381556Srgrimes pungetc(); 8391556Srgrimes RETURN(TBACKGND); 8401556Srgrimes case '|': 8411556Srgrimes if (pgetc() == '|') 8421556Srgrimes RETURN(TOR); 8431556Srgrimes pungetc(); 8441556Srgrimes RETURN(TPIPE); 8451556Srgrimes case ';': 8461556Srgrimes if (pgetc() == ';') 8471556Srgrimes RETURN(TENDCASE); 8481556Srgrimes pungetc(); 8491556Srgrimes RETURN(TSEMI); 8501556Srgrimes case '(': 8511556Srgrimes RETURN(TLP); 8521556Srgrimes case ')': 8531556Srgrimes RETURN(TRP); 8541556Srgrimes default: 8551556Srgrimes goto breakloop; 8561556Srgrimes } 8571556Srgrimes } 8581556Srgrimesbreakloop: 8591556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8601556Srgrimes#undef RETURN 8611556Srgrimes} 8621556Srgrimes 8631556Srgrimes 8641556Srgrimes 8651556Srgrimes/* 8661556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8671556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8681556Srgrimes * word which marks the end of the document and striptabs is true if 8691556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8701556Srgrimes * is the first character of the input token or document. 8711556Srgrimes * 8721556Srgrimes * Because C does not have internal subroutines, I have simulated them 8731556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8741556Srgrimes * will run code that appears at the end of readtoken1. 8751556Srgrimes */ 8761556Srgrimes 8771556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8781556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8791556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8801556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8811556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8821556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8831556Srgrimes 8841556SrgrimesSTATIC int 88590111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) 88690111Simp{ 88717987Speter int c = firstc; 88817987Speter char *out; 8891556Srgrimes int len; 8901556Srgrimes char line[EOFMARKLEN + 1]; 8911556Srgrimes struct nodelist *bqlist; 8921556Srgrimes int quotef; 8931556Srgrimes int dblquote; 8941556Srgrimes int varnest; /* levels of variables expansion */ 8951556Srgrimes int arinest; /* levels of arithmetic expansion */ 8961556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 8971556Srgrimes int oldstyle; 8981556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 89954679Scracauer int synentry; 90017987Speter#if __GNUC__ 90117987Speter /* Avoid longjmp clobbering */ 90217987Speter (void) &out; 90317987Speter (void) "ef; 90417987Speter (void) &dblquote; 90517987Speter (void) &varnest; 90617987Speter (void) &arinest; 90717987Speter (void) &parenlevel; 90817987Speter (void) &oldstyle; 90917987Speter (void) &prevsyntax; 91017987Speter (void) &syntax; 91154679Scracauer (void) &synentry; 91217987Speter#endif 9131556Srgrimes 9141556Srgrimes startlinno = plinno; 9151556Srgrimes dblquote = 0; 9161556Srgrimes if (syntax == DQSYNTAX) 9171556Srgrimes dblquote = 1; 9181556Srgrimes quotef = 0; 9191556Srgrimes bqlist = NULL; 9201556Srgrimes varnest = 0; 9211556Srgrimes arinest = 0; 9221556Srgrimes parenlevel = 0; 9231556Srgrimes 9241556Srgrimes STARTSTACKSTR(out); 9251556Srgrimes loop: { /* for each line, until end of word */ 9261556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9271556Srgrimes for (;;) { /* until end of line or end of word */ 9281556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 92954679Scracauer 93064705Scracauer synentry = syntax[c]; 93154679Scracauer 93254679Scracauer switch(synentry) { 9331556Srgrimes case CNL: /* '\n' */ 9341556Srgrimes if (syntax == BASESYNTAX) 9351556Srgrimes goto endword; /* exit outer loop */ 9361556Srgrimes USTPUTC(c, out); 9371556Srgrimes plinno++; 9381556Srgrimes if (doprompt) 9391556Srgrimes setprompt(2); 9401556Srgrimes else 9411556Srgrimes setprompt(0); 9421556Srgrimes c = pgetc(); 9431556Srgrimes goto loop; /* continue outer loop */ 9441556Srgrimes case CWORD: 9451556Srgrimes USTPUTC(c, out); 9461556Srgrimes break; 9471556Srgrimes case CCTL: 9481556Srgrimes if (eofmark == NULL || dblquote) 9491556Srgrimes USTPUTC(CTLESC, out); 9501556Srgrimes USTPUTC(c, out); 9511556Srgrimes break; 9521556Srgrimes case CBACK: /* backslash */ 9531556Srgrimes c = pgetc(); 9541556Srgrimes if (c == PEOF) { 9551556Srgrimes USTPUTC('\\', out); 9561556Srgrimes pungetc(); 9571556Srgrimes } else if (c == '\n') { 958160849Syar plinno++; 9591556Srgrimes if (doprompt) 9601556Srgrimes setprompt(2); 9611556Srgrimes else 9621556Srgrimes setprompt(0); 9631556Srgrimes } else { 96454631Scracauer if (dblquote && c != '\\' && 96554631Scracauer c != '`' && c != '$' && 96654631Scracauer (c != '"' || eofmark != NULL)) 9671556Srgrimes USTPUTC('\\', out); 96883675Stegge if (SQSYNTAX[c] == CCTL) 9691556Srgrimes USTPUTC(CTLESC, out); 97039137Stegge else if (eofmark == NULL) 97138887Stegge USTPUTC(CTLQUOTEMARK, out); 9721556Srgrimes USTPUTC(c, out); 9731556Srgrimes quotef++; 9741556Srgrimes } 9751556Srgrimes break; 9761556Srgrimes case CSQUOTE: 97739137Stegge if (eofmark == NULL) 97839137Stegge USTPUTC(CTLQUOTEMARK, out); 9791556Srgrimes syntax = SQSYNTAX; 9801556Srgrimes break; 9811556Srgrimes case CDQUOTE: 98239137Stegge if (eofmark == NULL) 98339137Stegge USTPUTC(CTLQUOTEMARK, out); 9841556Srgrimes syntax = DQSYNTAX; 9851556Srgrimes dblquote = 1; 9861556Srgrimes break; 9871556Srgrimes case CENDQUOTE: 98839137Stegge if (eofmark != NULL && arinest == 0 && 98939137Stegge varnest == 0) { 9901556Srgrimes USTPUTC(c, out); 9911556Srgrimes } else { 99239137Stegge if (arinest) { 9931556Srgrimes syntax = ARISYNTAX; 99439137Stegge dblquote = 0; 99539137Stegge } else if (eofmark == NULL) { 9961556Srgrimes syntax = BASESYNTAX; 99739137Stegge dblquote = 0; 99839137Stegge } 9991556Srgrimes quotef++; 10001556Srgrimes } 10011556Srgrimes break; 10021556Srgrimes case CVAR: /* '$' */ 10031556Srgrimes PARSESUB(); /* parse substitution */ 10041556Srgrimes break; 10051556Srgrimes case CENDVAR: /* '}' */ 10061556Srgrimes if (varnest > 0) { 10071556Srgrimes varnest--; 10081556Srgrimes USTPUTC(CTLENDVAR, out); 10091556Srgrimes } else { 10101556Srgrimes USTPUTC(c, out); 10111556Srgrimes } 10121556Srgrimes break; 10131556Srgrimes case CLP: /* '(' in arithmetic */ 10141556Srgrimes parenlevel++; 10151556Srgrimes USTPUTC(c, out); 10161556Srgrimes break; 10171556Srgrimes case CRP: /* ')' in arithmetic */ 10181556Srgrimes if (parenlevel > 0) { 10191556Srgrimes USTPUTC(c, out); 10201556Srgrimes --parenlevel; 10211556Srgrimes } else { 10221556Srgrimes if (pgetc() == ')') { 10231556Srgrimes if (--arinest == 0) { 10241556Srgrimes USTPUTC(CTLENDARI, out); 10251556Srgrimes syntax = prevsyntax; 102639137Stegge if (syntax == DQSYNTAX) 102739137Stegge dblquote = 1; 102839137Stegge else 102939137Stegge dblquote = 0; 10301556Srgrimes } else 10311556Srgrimes USTPUTC(')', out); 10321556Srgrimes } else { 10338855Srgrimes /* 10341556Srgrimes * unbalanced parens 10351556Srgrimes * (don't 2nd guess - no error) 10361556Srgrimes */ 10371556Srgrimes pungetc(); 10381556Srgrimes USTPUTC(')', out); 10391556Srgrimes } 10401556Srgrimes } 10411556Srgrimes break; 10421556Srgrimes case CBQUOTE: /* '`' */ 10431556Srgrimes PARSEBACKQOLD(); 10441556Srgrimes break; 10451556Srgrimes case CEOF: 10461556Srgrimes goto endword; /* exit outer loop */ 10471556Srgrimes default: 10481556Srgrimes if (varnest == 0) 10491556Srgrimes goto endword; /* exit outer loop */ 10501556Srgrimes USTPUTC(c, out); 10511556Srgrimes } 10521556Srgrimes c = pgetc_macro(); 10531556Srgrimes } 10541556Srgrimes } 10551556Srgrimesendword: 10561556Srgrimes if (syntax == ARISYNTAX) 10571556Srgrimes synerror("Missing '))'"); 10581556Srgrimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10591556Srgrimes synerror("Unterminated quoted string"); 10601556Srgrimes if (varnest != 0) { 10611556Srgrimes startlinno = plinno; 10621556Srgrimes synerror("Missing '}'"); 10631556Srgrimes } 10641556Srgrimes USTPUTC('\0', out); 10651556Srgrimes len = out - stackblock(); 10661556Srgrimes out = stackblock(); 10671556Srgrimes if (eofmark == NULL) { 10681556Srgrimes if ((c == '>' || c == '<') 10691556Srgrimes && quotef == 0 10701556Srgrimes && len <= 2 10711556Srgrimes && (*out == '\0' || is_digit(*out))) { 10721556Srgrimes PARSEREDIR(); 10731556Srgrimes return lasttoken = TREDIR; 10741556Srgrimes } else { 10751556Srgrimes pungetc(); 10761556Srgrimes } 10771556Srgrimes } 10781556Srgrimes quoteflag = quotef; 10791556Srgrimes backquotelist = bqlist; 10801556Srgrimes grabstackblock(len); 10811556Srgrimes wordtext = out; 10821556Srgrimes return lasttoken = TWORD; 10831556Srgrimes/* end of readtoken routine */ 10841556Srgrimes 10851556Srgrimes 10861556Srgrimes 10871556Srgrimes/* 10881556Srgrimes * Check to see whether we are at the end of the here document. When this 10891556Srgrimes * is called, c is set to the first character of the next input line. If 10901556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 10911556Srgrimes */ 10921556Srgrimes 10931556Srgrimescheckend: { 10941556Srgrimes if (eofmark) { 10951556Srgrimes if (striptabs) { 10961556Srgrimes while (c == '\t') 10971556Srgrimes c = pgetc(); 10981556Srgrimes } 10991556Srgrimes if (c == *eofmark) { 11001556Srgrimes if (pfgets(line, sizeof line) != NULL) { 110125230Ssteve char *p, *q; 11021556Srgrimes 11031556Srgrimes p = line; 11041556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 11051556Srgrimes if (*p == '\n' && *q == '\0') { 11061556Srgrimes c = PEOF; 11071556Srgrimes plinno++; 11081556Srgrimes needprompt = doprompt; 11091556Srgrimes } else { 11101556Srgrimes pushstring(line, strlen(line), NULL); 11111556Srgrimes } 11121556Srgrimes } 11131556Srgrimes } 11141556Srgrimes } 11151556Srgrimes goto checkend_return; 11161556Srgrimes} 11171556Srgrimes 11181556Srgrimes 11191556Srgrimes/* 11201556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11211556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11221556Srgrimes * first character of the redirection operator. 11231556Srgrimes */ 11241556Srgrimes 11251556Srgrimesparseredir: { 11261556Srgrimes char fd = *out; 11271556Srgrimes union node *np; 11281556Srgrimes 11291556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11301556Srgrimes if (c == '>') { 11311556Srgrimes np->nfile.fd = 1; 11321556Srgrimes c = pgetc(); 11331556Srgrimes if (c == '>') 11341556Srgrimes np->type = NAPPEND; 11351556Srgrimes else if (c == '&') 11361556Srgrimes np->type = NTOFD; 113796922Stjr else if (c == '|') 113896922Stjr np->type = NCLOBBER; 11391556Srgrimes else { 11401556Srgrimes np->type = NTO; 11411556Srgrimes pungetc(); 11421556Srgrimes } 11431556Srgrimes } else { /* c == '<' */ 11441556Srgrimes np->nfile.fd = 0; 11451556Srgrimes c = pgetc(); 11461556Srgrimes if (c == '<') { 11471556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11481556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11491556Srgrimes np->nfile.fd = 0; 11501556Srgrimes } 11511556Srgrimes np->type = NHERE; 11521556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11531556Srgrimes heredoc->here = np; 11541556Srgrimes if ((c = pgetc()) == '-') { 11551556Srgrimes heredoc->striptabs = 1; 11561556Srgrimes } else { 11571556Srgrimes heredoc->striptabs = 0; 11581556Srgrimes pungetc(); 11591556Srgrimes } 11601556Srgrimes } else if (c == '&') 11611556Srgrimes np->type = NFROMFD; 116266612Sbrian else if (c == '>') 116366612Sbrian np->type = NFROMTO; 11641556Srgrimes else { 11651556Srgrimes np->type = NFROM; 11661556Srgrimes pungetc(); 11671556Srgrimes } 11681556Srgrimes } 11691556Srgrimes if (fd != '\0') 11701556Srgrimes np->nfile.fd = digit_val(fd); 11711556Srgrimes redirnode = np; 11721556Srgrimes goto parseredir_return; 11731556Srgrimes} 11741556Srgrimes 11751556Srgrimes 11761556Srgrimes/* 11771556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11781556Srgrimes * and nothing else. 11791556Srgrimes */ 11801556Srgrimes 11811556Srgrimesparsesub: { 1182179022Sstefanf char buf[10]; 11831556Srgrimes int subtype; 11841556Srgrimes int typeloc; 11851556Srgrimes int flags; 11861556Srgrimes char *p; 11871556Srgrimes static const char types[] = "}-+?="; 1188179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1189179022Sstefanf int i; 1190179022Sstefanf int linno; 1191179387Sstefanf int length; 11921556Srgrimes 11931556Srgrimes c = pgetc(); 1194149026Sstefanf if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && 1195149026Sstefanf !is_special(c)) { 11961556Srgrimes USTPUTC('$', out); 11971556Srgrimes pungetc(); 11981556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 11991556Srgrimes if (pgetc() == '(') { 12001556Srgrimes PARSEARITH(); 12011556Srgrimes } else { 12021556Srgrimes pungetc(); 12031556Srgrimes PARSEBACKQNEW(); 12041556Srgrimes } 12051556Srgrimes } else { 12061556Srgrimes USTPUTC(CTLVAR, out); 12071556Srgrimes typeloc = out - stackblock(); 12081556Srgrimes USTPUTC(VSNORMAL, out); 12091556Srgrimes subtype = VSNORMAL; 1210179022Sstefanf flags = 0; 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)) { 1224179387Sstefanf length = 0; 12251556Srgrimes do { 12261556Srgrimes STPUTC(c, out); 12271556Srgrimes c = pgetc(); 1228179387Sstefanf length++; 1229149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1230179387Sstefanf if (length == 6 && 1231179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1232179022Sstefanf /* Replace the variable name with the 1233179022Sstefanf * current line number. */ 1234179022Sstefanf linno = plinno; 1235179022Sstefanf if (funclinno != 0) 1236179022Sstefanf linno -= funclinno - 1; 1237179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1238179022Sstefanf STADJUST(-6, out); 1239179022Sstefanf for (i = 0; buf[i] != '\0'; i++) 1240179022Sstefanf STPUTC(buf[i], out); 1241179022Sstefanf flags |= VSLINENO; 1242179022Sstefanf } 124318202Speter } else if (is_digit(c)) { 124418202Speter if (bracketed_name) { 124518202Speter do { 124618202Speter STPUTC(c, out); 124718202Speter c = pgetc(); 124818202Speter } while (is_digit(c)); 124918202Speter } else { 125018202Speter STPUTC(c, out); 125118202Speter c = pgetc(); 125218202Speter } 12531556Srgrimes } else { 1254164003Sstefanf if (! is_special(c)) { 1255164003Sstefanf subtype = VSERROR; 1256164003Sstefanf if (c == '}') 1257164003Sstefanf pungetc(); 1258164003Sstefanf else 1259164003Sstefanf USTPUTC(c, out); 1260164003Sstefanf } else { 1261164003Sstefanf USTPUTC(c, out); 1262164003Sstefanf c = pgetc(); 1263164003Sstefanf } 12641556Srgrimes } 12651556Srgrimes if (subtype == 0) { 126617987Speter switch (c) { 126717987Speter case ':': 1268179022Sstefanf flags |= VSNUL; 12691556Srgrimes c = pgetc(); 127017987Speter /*FALLTHROUGH*/ 127117987Speter default: 127217987Speter p = strchr(types, c); 1273164003Sstefanf if (p == NULL) { 1274164003Sstefanf if (flags == VSNUL) 1275164003Sstefanf STPUTC(':', out); 1276164003Sstefanf STPUTC(c, out); 1277164003Sstefanf subtype = VSERROR; 1278164003Sstefanf } else 1279164003Sstefanf subtype = p - types + VSNORMAL; 128017987Speter break; 128117987Speter case '%': 128220425Ssteve case '#': 128317987Speter { 128417987Speter int cc = c; 128517987Speter subtype = c == '#' ? VSTRIMLEFT : 128617987Speter VSTRIMRIGHT; 128717987Speter c = pgetc(); 128817987Speter if (c == cc) 128917987Speter subtype++; 129017987Speter else 129117987Speter pungetc(); 129217987Speter break; 129317987Speter } 12941556Srgrimes } 1295164003Sstefanf } else if (subtype != VSERROR) { 12961556Srgrimes pungetc(); 12971556Srgrimes } 1298164003Sstefanf STPUTC('=', out); 129957225Scracauer if (subtype != VSLENGTH && (dblquote || arinest)) 13001556Srgrimes flags |= VSQUOTE; 13011556Srgrimes *(stackblock() + typeloc) = subtype | flags; 13021556Srgrimes if (subtype != VSNORMAL) 13031556Srgrimes varnest++; 13041556Srgrimes } 13051556Srgrimes goto parsesub_return; 13061556Srgrimes} 13071556Srgrimes 13081556Srgrimes 13091556Srgrimes/* 13101556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 13111556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 13121556Srgrimes * list of commands (passed by reference), and savelen is the number of 13131556Srgrimes * characters on the top of the stack which must be preserved. 13141556Srgrimes */ 13151556Srgrimes 13161556Srgrimesparsebackq: { 13171556Srgrimes struct nodelist **nlpp; 13181556Srgrimes int savepbq; 13191556Srgrimes union node *n; 13201556Srgrimes char *volatile str; 13211556Srgrimes struct jmploc jmploc; 13221556Srgrimes struct jmploc *volatile savehandler; 13231556Srgrimes int savelen; 132420425Ssteve int saveprompt; 132520425Ssteve#if __GNUC__ 132620425Ssteve /* Avoid longjmp clobbering */ 132720425Ssteve (void) &saveprompt; 132820425Ssteve#endif 13291556Srgrimes 13301556Srgrimes savepbq = parsebackquote; 13311556Srgrimes if (setjmp(jmploc.loc)) { 13321556Srgrimes if (str) 13331556Srgrimes ckfree(str); 13341556Srgrimes parsebackquote = 0; 13351556Srgrimes handler = savehandler; 13361556Srgrimes longjmp(handler->loc, 1); 13371556Srgrimes } 13381556Srgrimes INTOFF; 13391556Srgrimes str = NULL; 13401556Srgrimes savelen = out - stackblock(); 13411556Srgrimes if (savelen > 0) { 13421556Srgrimes str = ckmalloc(savelen); 134317987Speter memcpy(str, stackblock(), savelen); 13441556Srgrimes } 13451556Srgrimes savehandler = handler; 13461556Srgrimes handler = &jmploc; 13471556Srgrimes INTON; 13481556Srgrimes if (oldstyle) { 13491556Srgrimes /* We must read until the closing backquote, giving special 13501556Srgrimes treatment to some slashes, and then push the string and 13511556Srgrimes reread it as input, interpreting it normally. */ 135225230Ssteve char *out; 135325230Ssteve int c; 13541556Srgrimes int savelen; 13551556Srgrimes char *str; 13568855Srgrimes 135720425Ssteve 13581556Srgrimes STARTSTACKSTR(out); 135920425Ssteve for (;;) { 136020425Ssteve if (needprompt) { 136120425Ssteve setprompt(2); 136220425Ssteve needprompt = 0; 136320425Ssteve } 136420425Ssteve switch (c = pgetc()) { 136520425Ssteve case '`': 136620425Ssteve goto done; 136720425Ssteve 136820425Ssteve case '\\': 136920425Ssteve if ((c = pgetc()) == '\n') { 137020425Ssteve plinno++; 137120425Ssteve if (doprompt) 137220425Ssteve setprompt(2); 137320425Ssteve else 137420425Ssteve setprompt(0); 137520425Ssteve /* 137620425Ssteve * If eating a newline, avoid putting 137720425Ssteve * the newline into the new character 137820425Ssteve * stream (via the STPUTC after the 137920425Ssteve * switch). 138020425Ssteve */ 138120425Ssteve continue; 138220425Ssteve } 138317987Speter if (c != '\\' && c != '`' && c != '$' 13841556Srgrimes && (!dblquote || c != '"')) 13851556Srgrimes STPUTC('\\', out); 138620425Ssteve break; 138720425Ssteve 138820425Ssteve case '\n': 138920425Ssteve plinno++; 139020425Ssteve needprompt = doprompt; 139120425Ssteve break; 139220425Ssteve 139320425Ssteve case PEOF: 139420425Ssteve startlinno = plinno; 139520425Ssteve synerror("EOF in backquote substitution"); 139620425Ssteve break; 139720425Ssteve 139820425Ssteve default: 139920425Ssteve break; 140020425Ssteve } 140120425Ssteve STPUTC(c, out); 14021556Srgrimes } 140320425Sstevedone: 14041556Srgrimes STPUTC('\0', out); 14051556Srgrimes savelen = out - stackblock(); 14061556Srgrimes if (savelen > 0) { 14071556Srgrimes str = ckmalloc(savelen); 140817987Speter memcpy(str, stackblock(), savelen); 140917987Speter setinputstring(str, 1); 14101556Srgrimes } 14111556Srgrimes } 14121556Srgrimes nlpp = &bqlist; 14131556Srgrimes while (*nlpp) 14141556Srgrimes nlpp = &(*nlpp)->next; 14151556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 14161556Srgrimes (*nlpp)->next = NULL; 14171556Srgrimes parsebackquote = oldstyle; 141820425Ssteve 141920425Ssteve if (oldstyle) { 142020425Ssteve saveprompt = doprompt; 142120425Ssteve doprompt = 0; 142220425Ssteve } 142320425Ssteve 14241556Srgrimes n = list(0); 142520425Ssteve 142620425Ssteve if (oldstyle) 142720425Ssteve doprompt = saveprompt; 142820425Ssteve else { 142920425Ssteve if (readtoken() != TRP) 143020425Ssteve synexpect(TRP); 143120425Ssteve } 143220425Ssteve 14331556Srgrimes (*nlpp)->n = n; 143420425Ssteve if (oldstyle) { 143520425Ssteve /* 143620425Ssteve * Start reading from old file again, ignoring any pushed back 143720425Ssteve * tokens left from the backquote parsing 143820425Ssteve */ 14391556Srgrimes popfile(); 144020425Ssteve tokpushback = 0; 144120425Ssteve } 14421556Srgrimes while (stackblocksize() <= savelen) 14431556Srgrimes growstackblock(); 14441556Srgrimes STARTSTACKSTR(out); 14451556Srgrimes if (str) { 144617987Speter memcpy(out, str, savelen); 14471556Srgrimes STADJUST(savelen, out); 14481556Srgrimes INTOFF; 14491556Srgrimes ckfree(str); 14501556Srgrimes str = NULL; 14511556Srgrimes INTON; 14521556Srgrimes } 14531556Srgrimes parsebackquote = savepbq; 14541556Srgrimes handler = savehandler; 14551556Srgrimes if (arinest || dblquote) 14561556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14571556Srgrimes else 14581556Srgrimes USTPUTC(CTLBACKQ, out); 14591556Srgrimes if (oldstyle) 14601556Srgrimes goto parsebackq_oldreturn; 14611556Srgrimes else 14621556Srgrimes goto parsebackq_newreturn; 14631556Srgrimes} 14641556Srgrimes 14651556Srgrimes/* 14661556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14671556Srgrimes */ 14681556Srgrimesparsearith: { 14691556Srgrimes 14701556Srgrimes if (++arinest == 1) { 14711556Srgrimes prevsyntax = syntax; 14721556Srgrimes syntax = ARISYNTAX; 14731556Srgrimes USTPUTC(CTLARI, out); 147438887Stegge if (dblquote) 147538887Stegge USTPUTC('"',out); 147638887Stegge else 147738887Stegge USTPUTC(' ',out); 14781556Srgrimes } else { 14791556Srgrimes /* 14801556Srgrimes * we collapse embedded arithmetic expansion to 14811556Srgrimes * parenthesis, which should be equivalent 14821556Srgrimes */ 14831556Srgrimes USTPUTC('(', out); 14841556Srgrimes } 14851556Srgrimes goto parsearith_return; 14861556Srgrimes} 14871556Srgrimes 14881556Srgrimes} /* end of readtoken */ 14891556Srgrimes 14901556Srgrimes 14911556Srgrimes 14921556Srgrimes#ifdef mkinit 14931556SrgrimesRESET { 14941556Srgrimes tokpushback = 0; 14951556Srgrimes checkkwd = 0; 14961556Srgrimes} 14971556Srgrimes#endif 14981556Srgrimes 14991556Srgrimes/* 15001556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 15011556Srgrimes * or backquotes). 15021556Srgrimes */ 15031556Srgrimes 15041556SrgrimesSTATIC int 150590111Simpnoexpand(char *text) 150690111Simp{ 150725230Ssteve char *p; 150825230Ssteve char c; 15091556Srgrimes 15101556Srgrimes p = text; 15111556Srgrimes while ((c = *p++) != '\0') { 151239137Stegge if ( c == CTLQUOTEMARK) 151339137Stegge continue; 15141556Srgrimes if (c == CTLESC) 15151556Srgrimes p++; 151683675Stegge else if (BASESYNTAX[(int)c] == CCTL) 15171556Srgrimes return 0; 15181556Srgrimes } 15191556Srgrimes return 1; 15201556Srgrimes} 15211556Srgrimes 15221556Srgrimes 15231556Srgrimes/* 15241556Srgrimes * Return true if the argument is a legal variable name (a letter or 15251556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 15261556Srgrimes */ 15271556Srgrimes 15281556Srgrimesint 152990111Simpgoodname(char *name) 153090111Simp{ 153125230Ssteve char *p; 15321556Srgrimes 15331556Srgrimes p = name; 15341556Srgrimes if (! is_name(*p)) 15351556Srgrimes return 0; 15361556Srgrimes while (*++p) { 15371556Srgrimes if (! is_in_name(*p)) 15381556Srgrimes return 0; 15391556Srgrimes } 15401556Srgrimes return 1; 15411556Srgrimes} 15421556Srgrimes 15431556Srgrimes 15441556Srgrimes/* 15451556Srgrimes * Called when an unexpected token is read during the parse. The argument 15461556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15471556Srgrimes * occur at this point. 15481556Srgrimes */ 15491556Srgrimes 15501556SrgrimesSTATIC void 155190111Simpsynexpect(int token) 155217987Speter{ 15531556Srgrimes char msg[64]; 15541556Srgrimes 15551556Srgrimes if (token >= 0) { 15561556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15571556Srgrimes tokname[lasttoken], tokname[token]); 15581556Srgrimes } else { 15591556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15601556Srgrimes } 15611556Srgrimes synerror(msg); 15621556Srgrimes} 15631556Srgrimes 15641556Srgrimes 15651556SrgrimesSTATIC void 156690111Simpsynerror(char *msg) 156790111Simp{ 15681556Srgrimes if (commandname) 15691556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15701556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15711556Srgrimes error((char *)NULL); 15721556Srgrimes} 15731556Srgrimes 15741556SrgrimesSTATIC void 157590111Simpsetprompt(int which) 157690111Simp{ 15771556Srgrimes whichprompt = which; 15781556Srgrimes 157917987Speter#ifndef NO_HISTORY 15801556Srgrimes if (!el) 158117987Speter#endif 15821556Srgrimes out2str(getprompt(NULL)); 15831556Srgrimes} 15841556Srgrimes 15851556Srgrimes/* 15861556Srgrimes * called by editline -- any expansions to the prompt 15871556Srgrimes * should be added here. 15881556Srgrimes */ 15891556Srgrimeschar * 159090111Simpgetprompt(void *unused __unused) 159125905Ssteve{ 1592142845Sobrien static char ps[PROMPTLEN]; 1593142845Sobrien char *fmt; 1594142845Sobrien int i, j, trim; 1595142845Sobrien 1596142845Sobrien /* 1597142845Sobrien * Select prompt format. 1598142845Sobrien */ 15991556Srgrimes switch (whichprompt) { 16001556Srgrimes case 0: 1601142845Sobrien fmt = ""; 1602142845Sobrien break; 16031556Srgrimes case 1: 1604142845Sobrien fmt = ps1val(); 1605142845Sobrien break; 16061556Srgrimes case 2: 1607142845Sobrien fmt = ps2val(); 1608142845Sobrien break; 16091556Srgrimes default: 16101556Srgrimes return "<internal prompt error>"; 16111556Srgrimes } 1612142845Sobrien 1613142845Sobrien /* 1614142845Sobrien * Format prompt string. 1615142845Sobrien */ 1616142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1617142845Sobrien if (*fmt == '\\') 1618142845Sobrien switch (*++fmt) { 1619142845Sobrien 1620142845Sobrien /* 1621142845Sobrien * Hostname. 1622142845Sobrien * 1623142845Sobrien * \h specifies just the local hostname, 1624142845Sobrien * \H specifies fully-qualified hostname. 1625142845Sobrien */ 1626142845Sobrien case 'h': 1627142845Sobrien case 'H': 1628149024Sstefanf ps[i] = '\0'; 1629142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1630142845Sobrien /* Skip to end of hostname. */ 1631142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1632142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1633142845Sobrien i++; 1634142845Sobrien break; 1635142845Sobrien 1636142845Sobrien /* 1637142845Sobrien * Working directory. 1638142845Sobrien * 1639142845Sobrien * \W specifies just the final component, 1640142845Sobrien * \w specifies the entire path. 1641142845Sobrien */ 1642142845Sobrien case 'W': 1643142845Sobrien case 'w': 1644149024Sstefanf ps[i] = '\0'; 1645142845Sobrien getcwd(&ps[i], PROMPTLEN - i); 1646142845Sobrien if (*fmt == 'W') { 1647142845Sobrien /* Final path component only. */ 1648142845Sobrien trim = 1; 1649142845Sobrien for (j = i; ps[j] != '\0'; j++) 1650142845Sobrien if (ps[j] == '/') 1651142845Sobrien trim = j + 1; 1652142845Sobrien memmove(&ps[i], &ps[trim], 1653142845Sobrien j - trim + 1); 1654142845Sobrien } 1655142845Sobrien /* Skip to end of path. */ 1656142845Sobrien while (ps[i + 1] != '\0') 1657142845Sobrien i++; 1658142845Sobrien break; 1659142845Sobrien 1660142845Sobrien /* 1661142845Sobrien * Superuser status. 1662142845Sobrien * 1663142845Sobrien * '$' for normal users, '#' for root. 1664142845Sobrien */ 1665142845Sobrien case '$': 1666142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1667142845Sobrien break; 1668142845Sobrien 1669142845Sobrien /* 1670142845Sobrien * A literal \. 1671142845Sobrien */ 1672142845Sobrien case '\\': 1673142845Sobrien ps[i] = '\\'; 1674142845Sobrien break; 1675142845Sobrien 1676142845Sobrien /* 1677142845Sobrien * Emit unrecognized formats verbatim. 1678142845Sobrien */ 1679142845Sobrien default: 1680142845Sobrien ps[i++] = '\\'; 1681142845Sobrien ps[i] = *fmt; 1682142845Sobrien break; 1683142845Sobrien } 1684142845Sobrien else 1685142845Sobrien ps[i] = *fmt; 1686142845Sobrien ps[i] = '\0'; 1687142845Sobrien return (ps); 16881556Srgrimes} 1689