2
* Copyright (c) 1991, 1993
3
* The Regents of the University of California. All rights reserved.
4
* Copyright (c) 1997-2005
5
* Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41
#include "expand.h" /* defines rmescapes() */
42
#include "redir.h" /* defines copyfd() */
43
#include "exec.h" /* defines find_builtin() */
56
#include "myhistedit.h"
60
* Shell command parser.
65
/* values returned by readtoken */
71
struct heredoc *next; /* next here document in list */
72
union node *here; /* redirection node */
73
char *eofmark; /* string indicating end of input */
74
int striptabs; /* if set, strip leading tabs */
79
struct heredoc *heredoclist; /* list of here documents to read */
80
int doprompt; /* if set, prompt the user */
81
int needprompt; /* true if interactive and at start of line */
82
int lasttoken; /* last token read */
83
MKINIT int tokpushback; /* last token pushed back */
84
char *wordtext; /* text of last word returned by readtoken */
86
struct nodelist *backquotelist;
87
union node *redirnode;
88
struct heredoc *heredoc;
89
int quoteflag; /* set if (part of) last token was quoted */
90
int startlinno; /* line # where last token started */
93
STATIC union node *list(int);
94
STATIC union node *andor(void);
95
STATIC union node *pipeline(void);
96
STATIC union node *command(void);
97
STATIC union node *simplecmd(void);
98
STATIC union node *makename(void);
99
STATIC void parsefname(void);
100
STATIC void parseheredoc(void);
101
STATIC int peektoken(void);
102
STATIC int readtoken(void);
103
STATIC int xxreadtoken(void);
104
STATIC int readtoken1(int, char const *, char *, int);
105
STATIC int noexpand(char *);
106
STATIC void synexpect(int) __attribute__((__noreturn__));
107
STATIC void synerror(const char *) __attribute__((__noreturn__));
108
STATIC void setprompt(int);
112
isassignment(const char *p)
114
const char *q = endofname(p);
122
* Read and parse a command. Returns NEOF on end of file. (NULL is a
123
* valid parse tree indicating a blank line.)
127
parsecmd(int interact)
149
union node *n1, *n2, *n3;
152
checkkwd = CHKNL | CHKKWD | CHKALIAS;
153
if (nlflag == 2 && tokendlist[peektoken()])
159
if (tok == TBACKGND) {
160
if (n2->type == NPIPE) {
161
n2->npipe.backgnd = 1;
163
if (n2->type != NREDIR) {
164
n3 = stalloc(sizeof(struct nredir));
166
n3->nredir.redirect = NULL;
176
n3 = (union node *)stalloc(sizeof (struct nbinary));
178
n3->nbinary.ch1 = n1;
179
n3->nbinary.ch2 = n2;
195
checkkwd = CHKNL | CHKKWD | CHKALIAS;
196
if (tokendlist[peektoken()])
203
pungetc(); /* push back EOF on input */
219
union node *n1, *n2, *n3;
224
if ((t = readtoken()) == TAND) {
226
} else if (t == TOR) {
232
checkkwd = CHKNL | CHKKWD | CHKALIAS;
234
n3 = (union node *)stalloc(sizeof (struct nbinary));
236
n3->nbinary.ch1 = n1;
237
n3->nbinary.ch2 = n2;
247
union node *n1, *n2, *pipenode;
248
struct nodelist *lp, *prev;
252
TRACE(("pipeline: entered\n"));
253
if (readtoken() == TNOT) {
255
checkkwd = CHKKWD | CHKALIAS;
259
if (readtoken() == TPIPE) {
260
pipenode = (union node *)stalloc(sizeof (struct npipe));
261
pipenode->type = NPIPE;
262
pipenode->npipe.backgnd = 0;
263
lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
264
pipenode->npipe.cmdlist = lp;
268
lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
269
checkkwd = CHKNL | CHKKWD | CHKALIAS;
272
} while (readtoken() == TPIPE);
278
n2 = (union node *)stalloc(sizeof (struct nnot));
292
union node *ap, **app;
293
union node *cp, **cpp;
294
union node *redir, **rpp;
301
switch (readtoken()) {
306
n1 = (union node *)stalloc(sizeof (struct nif));
308
n1->nif.test = list(0);
309
if (readtoken() != TTHEN)
311
n1->nif.ifpart = list(0);
313
while (readtoken() == TELIF) {
314
n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
315
n2 = n2->nif.elsepart;
317
n2->nif.test = list(0);
318
if (readtoken() != TTHEN)
320
n2->nif.ifpart = list(0);
322
if (lasttoken == TELSE)
323
n2->nif.elsepart = list(0);
325
n2->nif.elsepart = NULL;
333
n1 = (union node *)stalloc(sizeof (struct nbinary));
334
n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
335
n1->nbinary.ch1 = list(0);
336
if ((got=readtoken()) != TDO) {
337
TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
340
n1->nbinary.ch2 = list(0);
345
if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
346
synerror("Bad for loop variable");
347
n1 = (union node *)stalloc(sizeof (struct nfor));
349
n1->nfor.var = wordtext;
350
checkkwd = CHKKWD | CHKALIAS;
351
if (readtoken() == TIN) {
353
while (readtoken() == TWORD) {
354
n2 = (union node *)stalloc(sizeof (struct narg));
356
n2->narg.text = wordtext;
357
n2->narg.backquote = backquotelist;
359
app = &n2->narg.next;
363
if (lasttoken != TNL && lasttoken != TSEMI)
366
n2 = (union node *)stalloc(sizeof (struct narg));
368
n2->narg.text = (char *)dolatstr;
369
n2->narg.backquote = NULL;
370
n2->narg.next = NULL;
373
* Newline or semicolon here is optional (but note
374
* that the original Bourne shell only allowed NL).
376
if (lasttoken != TNL && lasttoken != TSEMI)
379
checkkwd = CHKNL | CHKKWD | CHKALIAS;
380
if (readtoken() != TDO)
382
n1->nfor.body = list(0);
386
n1 = (union node *)stalloc(sizeof (struct ncase));
388
if (readtoken() != TWORD)
390
n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
392
n2->narg.text = wordtext;
393
n2->narg.backquote = backquotelist;
394
n2->narg.next = NULL;
396
checkkwd = CHKKWD | CHKALIAS;
397
} while (readtoken() == TNL);
398
if (lasttoken != TIN)
400
cpp = &n1->ncase.cases;
402
checkkwd = CHKNL | CHKKWD;
405
if (lasttoken == TLP)
407
*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
409
app = &cp->nclist.pattern;
411
*app = ap = (union node *)stalloc(sizeof (struct narg));
413
ap->narg.text = wordtext;
414
ap->narg.backquote = backquotelist;
415
if (readtoken() != TPIPE)
417
app = &ap->narg.next;
420
ap->narg.next = NULL;
421
if (lasttoken != TRP)
423
cp->nclist.body = list(2);
425
cpp = &cp->nclist.next;
427
checkkwd = CHKNL | CHKKWD;
428
if ((t = readtoken()) != TESAC) {
438
n1 = (union node *)stalloc(sizeof (struct nredir));
439
n1->type = NSUBSHELL;
440
n1->nredir.n = list(0);
441
n1->nredir.redirect = NULL;
454
if (readtoken() != t)
458
/* Now check for redirection which may follow command */
459
checkkwd = CHKKWD | CHKALIAS;
461
while (readtoken() == TREDIR) {
462
*rpp = n2 = redirnode;
463
rpp = &n2->nfile.next;
469
if (n1->type != NSUBSHELL) {
470
n2 = (union node *)stalloc(sizeof (struct nredir));
475
n1->nredir.redirect = redir;
484
union node *args, **app;
485
union node *n = NULL;
486
union node *vars, **vpp;
487
union node **rpp, *redir;
497
savecheckkwd = CHKALIAS;
499
checkkwd = savecheckkwd;
500
switch (readtoken()) {
502
n = (union node *)stalloc(sizeof (struct narg));
504
n->narg.text = wordtext;
505
n->narg.backquote = backquotelist;
506
if (savecheckkwd && isassignment(wordtext)) {
516
*rpp = n = redirnode;
517
rpp = &n->nfile.next;
518
parsefname(); /* read name of redirection file */
522
args && app == &args->narg.next &&
525
struct builtincmd *bcmd;
528
/* We have a function */
529
if (readtoken() != TRP)
534
(bcmd = find_builtin(name)) &&
535
bcmd->flags & BUILTIN_SPECIAL
538
synerror("Bad function name");
540
checkkwd = CHKNL | CHKKWD | CHKALIAS;
541
n->narg.next = command();
554
n = (union node *)stalloc(sizeof (struct ncmd));
557
n->ncmd.assign = vars;
558
n->ncmd.redirect = redir;
567
n = (union node *)stalloc(sizeof (struct narg));
570
n->narg.text = wordtext;
571
n->narg.backquote = backquotelist;
575
void fixredir(union node *n, const char *text, int err)
577
TRACE(("Fix redir %s %d\n", text, err));
579
n->ndup.vname = NULL;
581
if (is_digit(text[0]) && text[1] == '\0')
582
n->ndup.dupfd = digit_val(text[0]);
583
else if (text[0] == '-' && text[1] == '\0')
588
synerror("Bad fd number");
590
n->ndup.vname = makename();
598
union node *n = redirnode;
600
if (readtoken() != TWORD)
602
if (n->type == NHERE) {
603
struct heredoc *here = heredoc;
609
TRACE(("Here document %d\n", n->type));
610
if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
611
synerror("Illegal eof marker for << redirection");
613
here->eofmark = wordtext;
615
if (heredoclist == NULL)
618
for (p = heredoclist ; p->next ; p = p->next);
621
} else if (n->type == NTOFD || n->type == NFROMFD) {
622
fixredir(n, wordtext, 0);
624
n->nfile.fname = makename();
630
* Input any here documents.
636
struct heredoc *here;
646
readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
647
here->eofmark, here->striptabs);
648
n = (union node *)stalloc(sizeof (struct narg));
651
n->narg.text = wordtext;
652
n->narg.backquote = backquotelist;
653
here->here->nhere.doc = n;
673
int alreadyseen = tokpushback;
682
if (checkkwd & CHKNL) {
689
if (t != TWORD || quoteflag) {
696
if (checkkwd & CHKKWD) {
697
const char *const *pp;
699
if ((pp = findkwd(wordtext))) {
700
lasttoken = t = pp - parsekwd + KWDOFFSET;
701
TRACE(("keyword %s recognized\n", tokname[t]));
706
if (checkkwd & CHKALIAS) {
708
if ((ap = lookupalias(wordtext, 1)) != NULL) {
710
pushstring(ap->val, ap);
719
TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
721
TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
728
* Read the next input token.
729
* If the token is a word, we set backquotelist to the list of cmds in
730
* backquotes. We set quoteflag to true if any part of the word was
732
* If the token is TREDIR, then we set redirnode to a structure containing
734
* In all cases, the variable startlinno is set to the number of the line
735
* on which the token starts.
737
* [Change comment: here documents and internal procedures]
738
* [Readtoken shouldn't have any arguments. Perhaps we should make the
739
* word parsing code into a separate routine. In this case, readtoken
740
* doesn't need to have any internal procedures, but parseword does.
741
* We could also make parseoperator in essence the main routine, and
742
* have parseword (readtoken1?) handle both words and redirection.]
745
#define RETURN(token) return lasttoken = token
760
for (;;) { /* until token or start of word found */
767
while ((c = pgetc()) != '\n' && c != PEOF);
771
if (pgetc() == '\n') {
772
startlinno = ++plinno;
781
needprompt = doprompt;
809
return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
816
* If eofmark is NULL, read a word or a redirection symbol. If eofmark
817
* is not NULL, read a here document. In the latter case, eofmark is the
818
* word which marks the end of the document and striptabs is true if
819
* leading tabs should be stripped from the document. The argument firstc
820
* is the first character of the input token or document.
822
* Because C does not have internal subroutines, I have simulated them
823
* using goto's to implement the subroutine linkage. The following macros
824
* will run code that appears at the end of readtoken1.
827
#define CHECKEND() {goto checkend; checkend_return:;}
828
#define PARSEREDIR() {goto parseredir; parseredir_return:;}
829
#define PARSESUB() {goto parsesub; parsesub_return:;}
830
#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
831
#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
832
#define PARSEARITH() {goto parsearith; parsearith_return:;}
835
readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
840
char line[EOFMARKLEN + 1];
841
struct nodelist *bqlist;
844
int varnest; /* levels of variables expansion */
845
int arinest; /* levels of arithmetic expansion */
846
int parenlevel; /* levels of parens in arithmetic */
847
int dqvarnest; /* levels of variables expansion within double quotes */
849
char const *prevsyntax = NULL; /* syntax before arithmetic */
853
if (syntax == DQSYNTAX)
863
loop: { /* for each line, until end of word */
865
if (c == '\034' && doprompt
866
&& attyset() && ! equal(termval(), "emacs")) {
868
if (syntax == BASESYNTAX)
874
CHECKEND(); /* set c to PEOF if at end of here document */
875
for (;;) { /* until end of line or end of word */
876
CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
879
if (syntax == BASESYNTAX)
880
goto endword; /* exit outer loop */
886
goto loop; /* continue outer loop */
891
if (eofmark == NULL || dblquote)
892
USTPUTC(CTLESC, out);
895
case CBACK: /* backslash */
898
USTPUTC(CTLESC, out);
901
} else if (c == '\n') {
907
c != '\\' && c != '`' &&
913
USTPUTC(CTLESC, out);
916
if (SQSYNTAX[c] == CCTL)
917
USTPUTC(CTLESC, out);
925
if (eofmark == NULL) {
926
USTPUTC(CTLQUOTEMARK, out);
934
if (eofmark != NULL && arinest == 0 &&
938
if (dqvarnest == 0) {
947
PARSESUB(); /* parse substitution */
949
case CENDVAR: /* '}' */
955
USTPUTC(CTLENDVAR, out);
960
case CLP: /* '(' in arithmetic */
964
case CRP: /* ')' in arithmetic */
965
if (parenlevel > 0) {
969
if (pgetc() == ')') {
970
if (--arinest == 0) {
971
USTPUTC(CTLENDARI, out);
973
if (syntax == DQSYNTAX)
982
* (don't 2nd guess - no error)
989
case CBQUOTE: /* '`' */
993
goto endword; /* exit outer loop */
998
goto endword; /* exit outer loop */
1007
if (syntax == ARISYNTAX)
1008
synerror("Missing '))'");
1009
if (syntax != BASESYNTAX && eofmark == NULL)
1010
synerror("Unterminated quoted string");
1012
startlinno = plinno;
1014
synerror("Missing '}'");
1017
len = out - (char *)stackblock();
1019
if (eofmark == NULL) {
1020
if ((c == '>' || c == '<')
1023
&& (*out == '\0' || is_digit(*out))) {
1025
return lasttoken = TREDIR;
1031
backquotelist = bqlist;
1032
grabstackblock(len);
1034
return lasttoken = TWORD;
1035
/* end of readtoken routine */
1040
* Check to see whether we are at the end of the here document. When this
1041
* is called, c is set to the first character of the next input line. If
1042
* we are at the end of the here document, this routine sets the c to PEOF.
1055
if (c == *eofmark) {
1056
if (pfgets(line, sizeof line) != NULL) {
1060
for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1061
if (*p == '\n' && *q == '\0') {
1064
needprompt = doprompt;
1066
pushstring(line, NULL);
1071
goto checkend_return;
1076
* Parse a redirection operator. The variable "out" points to a string
1077
* specifying the fd to be redirected. The variable "c" contains the
1078
* first character of the redirection operator.
1085
np = (union node *)stalloc(sizeof (struct nfile));
1092
np->type = NCLOBBER;
1099
} else { /* c == '<' */
1101
switch (c = pgetc()) {
1103
if (sizeof (struct nfile) != sizeof (struct nhere)) {
1104
np = (union node *)stalloc(sizeof (struct nhere));
1108
heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1110
if ((c = pgetc()) == '-') {
1111
heredoc->striptabs = 1;
1113
heredoc->striptabs = 0;
1133
np->nfile.fd = digit_val(fd);
1135
goto parseredir_return;
1140
* Parse a substitution. At this point, we have read the dollar sign
1149
static const char types[] = "}-+?=";
1154
(c != '(' && c != '{' && !is_name(c) && !is_special(c))
1158
} else if (c == '(') { /* $(command) or $((arith)) */
1159
if (pgetc() == '(') {
1166
USTPUTC(CTLVAR, out);
1167
typeloc = out - (char *)stackblock();
1168
USTPUTC(VSNORMAL, out);
1173
if ((c = pgetc()) == '}')
1181
if (c > PEOA && is_name(c)) {
1185
} while (c > PEOA && is_in_name(c));
1186
} else if (is_digit(c)) {
1190
} while (is_digit(c));
1192
else if (is_special(c)) {
1197
badsub: synerror("Bad substitution");
1208
p = strchr(types, c);
1211
subtype = p - types + VSNORMAL;
1217
subtype = c == '#' ? VSTRIMLEFT :
1230
if (dblquote || arinest)
1232
*((char *)stackblock() + typeloc) = subtype | flags;
1233
if (subtype != VSNORMAL) {
1235
if (dblquote || arinest) {
1240
goto parsesub_return;
1245
* Called to parse command substitutions. Newstyle is set if the command
1246
* is enclosed inside $(...); nlpp is a pointer to the head of the linked
1247
* list of commands (passed by reference), and savelen is the number of
1248
* characters on the top of the stack which must be preserved.
1252
struct nodelist **nlpp;
1259
savelen = out - (char *)stackblock();
1261
str = alloca(savelen);
1262
memcpy(str, stackblock(), savelen);
1265
/* We must read until the closing backquote, giving special
1266
treatment to some slashes, and then push the string and
1267
reread it as input, interpreting it normally. */
1274
STARTSTACKSTR(pout);
1279
switch (pc = pgetc()) {
1284
if ((pc = pgetc()) == '\n') {
1289
* If eating a newline, avoid putting
1290
* the newline into the new character
1291
* stream (via the STPUTC after the
1296
if (pc != '\\' && pc != '`' && pc != '$'
1297
&& (!dblquote || pc != '"'))
1306
startlinno = plinno;
1307
synerror("EOF in backquote substitution");
1311
needprompt = doprompt;
1321
psavelen = pout - (char *)stackblock();
1323
pstr = grabstackstr(pout);
1324
setinputstring(pstr);
1329
nlpp = &(*nlpp)->next;
1330
*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1331
(*nlpp)->next = NULL;
1334
saveprompt = doprompt;
1341
doprompt = saveprompt;
1343
if (readtoken() != TRP)
1350
* Start reading from old file again, ignoring any pushed back
1351
* tokens left from the backquote parsing
1356
while (stackblocksize() <= savelen)
1360
memcpy(out, str, savelen);
1361
STADJUST(savelen, out);
1363
if (arinest || dblquote)
1364
USTPUTC(CTLBACKQ | CTLQUOTE, out);
1366
USTPUTC(CTLBACKQ, out);
1368
goto parsebackq_oldreturn;
1370
goto parsebackq_newreturn;
1374
* Parse an arithmetic expansion (indicate start of one and set state)
1378
if (++arinest == 1) {
1379
prevsyntax = syntax;
1381
USTPUTC(CTLARI, out);
1388
* we collapse embedded arithmetic expansion to
1389
* parenthesis, which should be equivalent
1393
goto parsearith_return;
1396
} /* end of readtoken */
1409
* Returns true if the text contains nothing to expand (no dollar signs
1414
noexpand(char *text)
1420
while ((c = *p++) != '\0') {
1421
if (c == CTLQUOTEMARK)
1425
else if (BASESYNTAX[(int)c] == CCTL)
1433
* Return of a legal variable name (a letter or underscore followed by zero or
1434
* more letters, underscores, and digits).
1438
endofname(const char *name)
1446
if (! is_in_name(*p))
1454
* Called when an unexpected token is read during the parse. The argument
1455
* is the token that is expected, or -1 if more than one type of token can
1456
* occur at this point.
1460
synexpect(int token)
1465
fmtstr(msg, 64, "%s unexpected (expecting %s)",
1466
tokname[lasttoken], tokname[token]);
1468
fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1476
synerror(const char *msg)
1478
sh_error("Syntax error: %s", msg);
1483
setprompt(int which)
1485
struct stackmark smark;
1489
whichprompt = which;
1497
setstackmark(&smark);
1498
stalloc(stackblocksize());
1499
out2str(getprompt(NULL));
1500
popstackmark(&smark);
1505
expandstr(const char *ps)
1509
/* XXX Fix (char *) cast. */
1510
setinputstring((char *)ps);
1511
readtoken1(pgetc(), DQSYNTAX, nullstr, 0);
1516
n.narg.text = wordtext;
1517
n.narg.backquote = backquotelist;
1519
expandarg(&n, NULL, 0);
1520
return stackblock();
1524
* called by editline -- any expansions to the prompt
1525
* should be added here.
1528
getprompt(void *unused)
1532
switch (whichprompt) {
1535
return "<internal prompt error>";
1547
return expandstr(prompt);
1551
findkwd(const char *s)
1554
s, parsekwd, sizeof(parsekwd) / sizeof(const char *)