2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
48 #include "expand.h" /* defines rmescapes() */
60 #include "exec.h" /* to check for special builtins */
62 #include "myhistedit.h"
66 * Shell command parser.
72 /* values of checkkwd variable */
77 /* values returned by readtoken */
83 struct heredoc *next; /* next here document in list */
84 union node *here; /* redirection node */
85 char *eofmark; /* string indicating end of input */
86 int striptabs; /* if set, strip leading tabs */
90 struct parser_temp *next;
95 static struct heredoc *heredoclist; /* list of here documents to read */
96 static int doprompt; /* if set, prompt the user */
97 static int needprompt; /* true if interactive and at start of line */
98 static int lasttoken; /* last token read */
99 static int tokpushback; /* last token pushed back */
100 static char *wordtext; /* text of last word returned by readtoken */
102 static struct nodelist *backquotelist;
103 static union node *redirnode;
104 static struct heredoc *heredoc;
105 static int quoteflag; /* set if (part of) last token was quoted */
106 static int startlinno; /* line # where last token started */
107 static int funclinno; /* line # where the current function started */
108 static struct parser_temp *parser_temp;
111 static union node *list(int);
112 static union node *andor(void);
113 static union node *pipeline(void);
114 static union node *command(void);
115 static union node *simplecmd(union node **, union node *);
116 static union node *makename(void);
117 static union node *makebinary(int type, union node *n1, union node *n2);
118 static void parsefname(void);
119 static void parseheredoc(void);
120 static int peektoken(void);
121 static int readtoken(void);
122 static int xxreadtoken(void);
123 static int readtoken1(int, const char *, const char *, int);
124 static int noexpand(char *);
125 static void consumetoken(int);
126 static void synexpect(int) __dead2;
127 static void synerror(const char *) __dead2;
128 static void setprompt(int);
132 parser_temp_alloc(size_t len)
134 struct parser_temp *t;
137 t = ckmalloc(sizeof(*t));
139 t->next = parser_temp;
141 t->data = ckmalloc(len);
148 parser_temp_realloc(void *ptr, size_t len)
150 struct parser_temp *t;
155 error("bug: parser_temp_realloc misused");
156 t->data = ckrealloc(t->data, len);
163 parser_temp_free_upto(void *ptr)
165 struct parser_temp *t;
169 while (parser_temp != NULL && !done) {
171 parser_temp = t->next;
172 done = t->data == ptr;
178 error("bug: parser_temp_free_upto misused");
183 parser_temp_free_all(void)
185 struct parser_temp *t;
188 while (parser_temp != NULL) {
190 parser_temp = t->next;
199 * Read and parse a command. Returns NEOF on end of file. (NULL is a
200 * valid parse tree indicating a blank line.)
204 parsecmd(int interact)
208 /* This assumes the parser is not re-entered,
209 * which could happen if we add command substitution on PS1/PS2.
211 parser_temp_free_all();
233 * Read and parse words for wordexp.
234 * Returns a list of NARG nodes; NULL if there are no words.
239 union node *n, *first = NULL, **pnext;
242 /* This assumes the parser is not re-entered,
243 * which could happen if we add command substitution on PS1/PS2.
245 parser_temp_free_all();
254 while ((t = readtoken()) != TEOF) {
259 pnext = &n->narg.next;
268 union node *ntop, *n1, *n2, *n3;
271 checkkwd = CHKNL | CHKKWD | CHKALIAS;
272 if (!nlflag && tokendlist[peektoken()])
278 if (tok == TBACKGND) {
279 if (n2 != NULL && n2->type == NPIPE) {
280 n2->npipe.backgnd = 1;
281 } else if (n2 != NULL && n2->type == NREDIR) {
284 n3 = (union node *)stalloc(sizeof (struct nredir));
287 n3->nredir.redirect = NULL;
293 else if (n1 == NULL) {
294 n1 = makebinary(NSEMI, ntop, n2);
298 n3 = makebinary(NSEMI, n1->nbinary.ch2, n2);
299 n1->nbinary.ch2 = n3;
312 } else if (tok == TEOF && nlflag) {
318 checkkwd = CHKNL | CHKKWD | CHKALIAS;
319 if (!nlflag && tokendlist[peektoken()])
326 pungetc(); /* push back EOF on input */
347 if ((t = readtoken()) == TAND) {
349 } else if (t == TOR) {
355 n = makebinary(t, n, pipeline());
364 union node *n1, *n2, *pipenode;
365 struct nodelist *lp, *prev;
369 checkkwd = CHKNL | CHKKWD | CHKALIAS;
370 TRACE(("pipeline: entered\n"));
371 while (readtoken() == TNOT)
375 if (readtoken() == TPIPE) {
376 pipenode = (union node *)stalloc(sizeof (struct npipe));
377 pipenode->type = NPIPE;
378 pipenode->npipe.backgnd = 0;
379 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
380 pipenode->npipe.cmdlist = lp;
384 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
385 checkkwd = CHKNL | CHKKWD | CHKALIAS;
393 } while (readtoken() == TPIPE);
399 n2 = (union node *)stalloc(sizeof (struct nnot));
413 union node *ap, **app;
414 union node *cp, **cpp;
415 union node *redir, **rpp;
419 checkkwd = CHKNL | CHKKWD | CHKALIAS;
425 /* Check for redirection which may precede command */
426 while (readtoken() == TREDIR) {
427 *rpp = n2 = redirnode;
428 rpp = &n2->nfile.next;
433 switch (readtoken()) {
435 n1 = (union node *)stalloc(sizeof (struct nif));
437 if ((n1->nif.test = list(0)) == NULL)
440 n1->nif.ifpart = list(0);
442 while (readtoken() == TELIF) {
443 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
444 n2 = n2->nif.elsepart;
446 if ((n2->nif.test = list(0)) == NULL)
449 n2->nif.ifpart = list(0);
451 if (lasttoken == TELSE)
452 n2->nif.elsepart = list(0);
454 n2->nif.elsepart = NULL;
458 checkkwd = CHKKWD | CHKALIAS;
463 if ((n1 = list(0)) == NULL)
466 n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0));
468 checkkwd = CHKKWD | CHKALIAS;
471 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
472 synerror("Bad for loop variable");
473 n1 = (union node *)stalloc(sizeof (struct nfor));
475 n1->nfor.var = wordtext;
476 while (readtoken() == TNL)
478 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
480 while (readtoken() == TWORD) {
483 app = &n2->narg.next;
487 if (lasttoken != TNL && lasttoken != TSEMI)
490 static char argvars[5] = {
491 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
493 n2 = (union node *)stalloc(sizeof (struct narg));
495 n2->narg.text = argvars;
496 n2->narg.backquote = NULL;
497 n2->narg.next = NULL;
500 * Newline or semicolon here is optional (but note
501 * that the original Bourne shell only allowed NL).
503 if (lasttoken != TNL && lasttoken != TSEMI)
506 checkkwd = CHKNL | CHKKWD | CHKALIAS;
507 if ((t = readtoken()) == TDO)
509 else if (t == TBEGIN)
513 n1->nfor.body = list(0);
515 checkkwd = CHKKWD | CHKALIAS;
518 n1 = (union node *)stalloc(sizeof (struct ncase));
521 n1->ncase.expr = makename();
522 while (readtoken() == TNL);
523 if (lasttoken != TWORD || ! equal(wordtext, "in"))
524 synerror("expecting \"in\"");
525 cpp = &n1->ncase.cases;
526 checkkwd = CHKNL | CHKKWD, readtoken();
527 while (lasttoken != TESAC) {
528 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
530 app = &cp->nclist.pattern;
531 if (lasttoken == TLP)
534 *app = ap = makename();
535 checkkwd = CHKNL | CHKKWD;
536 if (readtoken() != TPIPE)
538 app = &ap->narg.next;
541 ap->narg.next = NULL;
542 if (lasttoken != TRP)
544 cp->nclist.body = list(0);
546 checkkwd = CHKNL | CHKKWD | CHKALIAS;
547 if ((t = readtoken()) != TESAC) {
550 else if (t == TFALLTHRU)
551 cp->type = NCLISTFALLTHRU;
554 checkkwd = CHKNL | CHKKWD, readtoken();
556 cpp = &cp->nclist.next;
559 checkkwd = CHKKWD | CHKALIAS;
562 n1 = (union node *)stalloc(sizeof (struct nredir));
563 n1->type = NSUBSHELL;
564 n1->nredir.n = list(0);
565 n1->nredir.redirect = NULL;
567 checkkwd = CHKKWD | CHKALIAS;
573 checkkwd = CHKKWD | CHKALIAS;
575 /* A simple command must have at least one redirection or word. */
590 n1 = simplecmd(rpp, redir);
596 /* Now check for redirection which may follow command */
597 while (readtoken() == TREDIR) {
598 *rpp = n2 = redirnode;
599 rpp = &n2->nfile.next;
606 n2 = (union node *)stalloc(sizeof (struct nredir));
611 n1->nredir.redirect = redir;
619 simplecmd(union node **rpp, union node *redir)
621 union node *args, **app;
622 union node **orig_rpp = rpp;
623 union node *n = NULL;
627 /* If we don't have any redirections already, then we must reset */
628 /* rpp to be the address of the local redir variable. */
635 * We save the incoming value, because we need this for shell
636 * functions. There can not be a redirect or an argument between
637 * the function name and the open parenthesis.
641 savecheckkwd = CHKALIAS;
644 checkkwd = savecheckkwd;
645 if (readtoken() == TWORD) {
649 if (savecheckkwd != 0 && !isassignment(wordtext))
651 } else if (lasttoken == TREDIR) {
652 *rpp = n = redirnode;
653 rpp = &n->nfile.next;
654 parsefname(); /* read name of redirection file */
655 } else if (lasttoken == TLP && app == &args->narg.next
656 && rpp == orig_rpp) {
657 /* We have a function */
661 * - Require plain text.
662 * - Functions with '/' cannot be called.
664 * - Reject ksh extended glob patterns.
666 if (!noexpand(n->narg.text) || quoteflag ||
667 strchr(n->narg.text, '/') ||
669 n->narg.text[strlen(n->narg.text) - 1]))
670 synerror("Bad function name");
671 rmescapes(n->narg.text);
672 if (find_builtin(n->narg.text, &special) >= 0 &&
674 synerror("Cannot override a special builtin with a function");
676 n->narg.next = command();
686 n = (union node *)stalloc(sizeof (struct ncmd));
689 n->ncmd.redirect = redir;
698 n = (union node *)stalloc(sizeof (struct narg));
701 n->narg.text = wordtext;
702 n->narg.backquote = backquotelist;
707 makebinary(int type, union node *n1, union node *n2)
711 n = (union node *)stalloc(sizeof (struct nbinary));
721 checkkwd |= CHKALIAS;
725 fixredir(union node *n, const char *text, int err)
727 TRACE(("Fix redir %s %d\n", text, err));
729 n->ndup.vname = NULL;
731 if (is_digit(text[0]) && text[1] == '\0')
732 n->ndup.dupfd = digit_val(text[0]);
733 else if (text[0] == '-' && text[1] == '\0')
738 synerror("Bad fd number");
740 n->ndup.vname = makename();
748 union node *n = redirnode;
751 if (n->type == NHERE) {
752 struct heredoc *here = heredoc;
758 TRACE(("Here document %d\n", n->type));
759 if (here->striptabs) {
760 while (*wordtext == '\t')
763 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
764 synerror("Illegal eof marker for << redirection");
766 here->eofmark = wordtext;
768 if (heredoclist == NULL)
771 for (p = heredoclist ; p->next ; p = p->next);
774 } else if (n->type == NTOFD || n->type == NFROMFD) {
775 fixredir(n, wordtext, 0);
777 n->nfile.fname = makename();
783 * Input any here documents.
789 struct heredoc *here;
792 while (heredoclist) {
794 heredoclist = here->next;
799 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
800 here->eofmark, here->striptabs);
802 here->here->nhere.doc = n;
822 int alreadyseen = tokpushback;
831 if (checkkwd & CHKNL) {
839 * check for keywords and aliases
841 if (t == TWORD && !quoteflag)
843 const char * const *pp;
845 if (checkkwd & CHKKWD)
846 for (pp = parsekwd; *pp; pp++) {
847 if (**pp == *wordtext && equal(*pp, wordtext))
849 lasttoken = t = pp - parsekwd + KWDOFFSET;
850 TRACE(("keyword %s recognized\n", tokname[t]));
854 if (checkkwd & CHKALIAS &&
855 (ap = lookupalias(wordtext, 1)) != NULL) {
856 pushstring(ap->val, strlen(ap->val), ap);
866 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
868 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
875 * Read the next input token.
876 * If the token is a word, we set backquotelist to the list of cmds in
877 * backquotes. We set quoteflag to true if any part of the word was
879 * If the token is TREDIR, then we set redirnode to a structure containing
881 * In all cases, the variable startlinno is set to the number of the line
882 * on which the token starts.
884 * [Change comment: here documents and internal procedures]
885 * [Readtoken shouldn't have any arguments. Perhaps we should make the
886 * word parsing code into a separate routine. In this case, readtoken
887 * doesn't need to have any internal procedures, but parseword does.
888 * We could also make parseoperator in essence the main routine, and
889 * have parseword (readtoken1?) handle both words and redirection.]
892 #define RETURN(token) return lasttoken = token
908 for (;;) { /* until token or start of word found */
914 while ((c = pgetc()) != '\n' && c != PEOF);
918 if (pgetc() == '\n') {
919 startlinno = ++plinno;
930 needprompt = doprompt;
961 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
966 #define MAXNEST_static 8
969 const char *syntax; /* *SYNTAX */
970 int parenlevel; /* levels of parentheses in arithmetic */
971 enum tokenstate_category
974 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
975 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
982 * Called to parse command substitutions.
986 parsebackq(char *out, struct nodelist **pbqlist,
987 int oldstyle, int dblquote, int quoted)
989 struct nodelist **nlpp;
992 struct jmploc jmploc;
993 struct jmploc *const savehandler = handler;
996 const int bq_startlinno = plinno;
997 char *volatile ostr = NULL;
998 struct parsefile *const savetopfile = getcurrentfile();
999 struct heredoc *const saveheredoclist = heredoclist;
1000 struct heredoc *here;
1003 if (setjmp(jmploc.loc)) {
1004 popfilesupto(savetopfile);
1009 heredoclist = saveheredoclist;
1010 handler = savehandler;
1011 if (exception == EXERROR) {
1012 startlinno = bq_startlinno;
1013 synerror("Error in command substitution");
1015 longjmp(handler->loc, 1);
1018 savelen = out - stackblock();
1020 str = ckmalloc(savelen);
1021 memcpy(str, stackblock(), savelen);
1027 /* We must read until the closing backquote, giving special
1028 treatment to some slashes, and then push the string and
1029 reread it as input, interpreting it normally. */
1035 STARTSTACKSTR(oout);
1041 CHECKSTRSPACE(2, oout);
1042 switch (c = pgetc()) {
1047 if ((c = pgetc()) == '\n') {
1054 * If eating a newline, avoid putting
1055 * the newline into the new character
1056 * stream (via the USTPUTC after the
1061 if (c != '\\' && c != '`' && c != '$'
1062 && (!dblquote || c != '"'))
1063 USTPUTC('\\', oout);
1068 needprompt = doprompt;
1072 startlinno = plinno;
1073 synerror("EOF in backquote substitution");
1082 USTPUTC('\0', oout);
1083 olen = oout - stackblock();
1085 ostr = ckmalloc(olen);
1086 memcpy(ostr, stackblock(), olen);
1087 setinputstring(ostr, 1);
1092 nlpp = &(*nlpp)->next;
1093 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1094 (*nlpp)->next = NULL;
1097 saveprompt = doprompt;
1104 if (peektoken() != TEOF)
1106 doprompt = saveprompt;
1113 * Start reading from old file again, ignoring any pushed back
1114 * tokens left from the backquote parsing
1120 CHECKSTRSPACE(savelen + 1, out);
1123 memcpy(out, str, savelen);
1124 STADJUST(savelen, out);
1132 here = saveheredoclist;
1134 while (here->next != NULL)
1136 here->next = heredoclist;
1137 heredoclist = saveheredoclist;
1139 handler = savehandler;
1142 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1144 USTPUTC(CTLBACKQ, out);
1150 * Called to parse a backslash escape sequence inside $'...'.
1151 * The backslash has already been read.
1154 readcstyleesc(char *out)
1161 synerror("Unterminated quoted string");
1174 case 'a': v = '\a'; break;
1175 case 'b': v = '\b'; break;
1176 case 'e': v = '\033'; break;
1177 case 'f': v = '\f'; break;
1178 case 'n': v = '\n'; break;
1179 case 'r': v = '\r'; break;
1180 case 't': v = '\t'; break;
1181 case 'v': v = '\v'; break;
1186 if (c >= '0' && c <= '9')
1187 v = (v << 4) + c - '0';
1188 else if (c >= 'A' && c <= 'F')
1189 v = (v << 4) + c - 'A' + 10;
1190 else if (c >= 'a' && c <= 'f')
1191 v = (v << 4) + c - 'a' + 10;
1197 case '0': case '1': case '2': case '3':
1198 case '4': case '5': case '6': case '7':
1201 if (c >= '0' && c <= '7') {
1205 if (c >= '0' && c <= '7') {
1215 if (c < 0x3f || c > 0x7a || c == 0x60)
1216 synerror("Bad escape sequence");
1217 if (c == '\\' && pgetc() != '\\')
1218 synerror("Bad escape sequence");
1226 n = c == 'U' ? 8 : 4;
1228 for (i = 0; i < n; i++) {
1230 if (c >= '0' && c <= '9')
1231 v = (v << 4) + c - '0';
1232 else if (c >= 'A' && c <= 'F')
1233 v = (v << 4) + c - 'A' + 10;
1234 else if (c >= 'a' && c <= 'f')
1235 v = (v << 4) + c - 'a' + 10;
1237 synerror("Bad escape sequence");
1239 if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1240 synerror("Bad escape sequence");
1241 /* We really need iconv here. */
1242 if (initial_localeisutf8 && v > 127) {
1243 CHECKSTRSPACE(4, out);
1245 * We cannot use wctomb() as the locale may have
1249 USTPUTC(0xc0 | v >> 6, out);
1250 USTPUTC(0x80 | (v & 0x3f), out);
1252 } else if (v <= 0xffff) {
1253 USTPUTC(0xe0 | v >> 12, out);
1254 USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1255 USTPUTC(0x80 | (v & 0x3f), out);
1257 } else if (v <= 0x10ffff) {
1258 USTPUTC(0xf0 | v >> 18, out);
1259 USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1260 USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1261 USTPUTC(0x80 | (v & 0x3f), out);
1269 synerror("Bad escape sequence");
1273 * We can't handle NUL bytes.
1274 * POSIX says we should skip till the closing quote.
1277 while ((c = pgetc()) != '\'') {
1281 synerror("Unterminated quoted string");
1286 if (SQSYNTAX[v] == CCTL)
1287 USTPUTC(CTLESC, out);
1294 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
1295 * is not NULL, read a here document. In the latter case, eofmark is the
1296 * word which marks the end of the document and striptabs is true if
1297 * leading tabs should be stripped from the document. The argument firstc
1298 * is the first character of the input token or document.
1300 * Because C does not have internal subroutines, I have simulated them
1301 * using goto's to implement the subroutine linkage. The following macros
1302 * will run code that appears at the end of readtoken1.
1305 #define CHECKEND() {goto checkend; checkend_return:;}
1306 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
1307 #define PARSESUB() {goto parsesub; parsesub_return:;}
1308 #define PARSEARITH() {goto parsearith; parsearith_return:;}
1311 readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1317 char line[EOFMARKLEN + 1];
1318 struct nodelist *bqlist;
1323 struct tokenstate state_static[MAXNEST_static];
1324 int maxnest = MAXNEST_static;
1325 struct tokenstate *state = state_static;
1328 startlinno = plinno;
1333 state[level].syntax = initialsyntax;
1334 state[level].parenlevel = 0;
1335 state[level].category = TSTATE_TOP;
1338 loop: { /* for each line, until end of word */
1339 CHECKEND(); /* set c to PEOF if at end of here document */
1340 for (;;) { /* until end of line or end of word */
1341 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
1343 synentry = state[level].syntax[c];
1346 case CNL: /* '\n' */
1347 if (state[level].syntax == BASESYNTAX)
1348 goto endword; /* exit outer loop */
1356 goto loop; /* continue outer loop */
1359 out = readcstyleesc(out);
1367 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1368 USTPUTC(CTLESC, out);
1371 case CBACK: /* backslash */
1376 } else if (c == '\n') {
1383 if (state[level].syntax == DQSYNTAX &&
1384 c != '\\' && c != '`' && c != '$' &&
1385 (c != '"' || (eofmark != NULL &&
1386 newvarnest == 0)) &&
1387 (c != '}' || state[level].category != TSTATE_VAR_OLD))
1389 if ((eofmark == NULL ||
1391 state[level].syntax == BASESYNTAX)
1392 USTPUTC(CTLQUOTEMARK, out);
1393 if (SQSYNTAX[c] == CCTL)
1394 USTPUTC(CTLESC, out);
1396 if ((eofmark == NULL ||
1398 state[level].syntax == BASESYNTAX &&
1399 state[level].category == TSTATE_VAR_OLD)
1400 USTPUTC(CTLQUOTEEND, out);
1405 USTPUTC(CTLQUOTEMARK, out);
1406 state[level].syntax = SQSYNTAX;
1410 USTPUTC(CTLQUOTEMARK, out);
1411 state[level].syntax = DQSYNTAX;
1414 if (eofmark != NULL && newvarnest == 0)
1417 if (state[level].category == TSTATE_VAR_OLD)
1418 USTPUTC(CTLQUOTEEND, out);
1419 state[level].syntax = BASESYNTAX;
1423 case CVAR: /* '$' */
1424 PARSESUB(); /* parse substitution */
1426 case CENDVAR: /* '}' */
1428 ((state[level].category == TSTATE_VAR_OLD &&
1429 state[level].syntax ==
1430 state[level - 1].syntax) ||
1431 (state[level].category == TSTATE_VAR_NEW &&
1432 state[level].syntax == BASESYNTAX))) {
1433 if (state[level].category == TSTATE_VAR_NEW)
1436 USTPUTC(CTLENDVAR, out);
1441 case CLP: /* '(' in arithmetic */
1442 state[level].parenlevel++;
1445 case CRP: /* ')' in arithmetic */
1446 if (state[level].parenlevel > 0) {
1448 --state[level].parenlevel;
1450 if (pgetc() == ')') {
1452 state[level].category == TSTATE_ARITH) {
1454 USTPUTC(CTLENDARI, out);
1460 * (don't 2nd guess - no error)
1467 case CBQUOTE: /* '`' */
1468 out = parsebackq(out, &bqlist, 1,
1469 state[level].syntax == DQSYNTAX &&
1470 (eofmark == NULL || newvarnest > 0),
1471 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1474 goto endword; /* exit outer loop */
1479 goto endword; /* exit outer loop */
1486 if (state[level].syntax == ARISYNTAX)
1487 synerror("Missing '))'");
1488 if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1489 synerror("Unterminated quoted string");
1490 if (state[level].category == TSTATE_VAR_OLD ||
1491 state[level].category == TSTATE_VAR_NEW) {
1492 startlinno = plinno;
1493 synerror("Missing '}'");
1495 if (state != state_static)
1496 parser_temp_free_upto(state);
1498 len = out - stackblock();
1500 if (eofmark == NULL) {
1501 if ((c == '>' || c == '<')
1504 && (*out == '\0' || is_digit(*out))) {
1506 return lasttoken = TREDIR;
1512 backquotelist = bqlist;
1513 grabstackblock(len);
1515 return lasttoken = TWORD;
1516 /* end of readtoken routine */
1520 * Check to see whether we are at the end of the here document. When this
1521 * is called, c is set to the first character of the next input line. If
1522 * we are at the end of the here document, this routine sets the c to PEOF.
1531 if (c == *eofmark) {
1532 if (pfgets(line, sizeof line) != NULL) {
1536 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1537 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1541 needprompt = doprompt;
1544 pushstring(line, strlen(line), NULL);
1549 goto checkend_return;
1554 * Parse a redirection operator. The variable "out" points to a string
1555 * specifying the fd to be redirected. The variable "c" contains the
1556 * first character of the redirection operator.
1563 np = (union node *)stalloc(sizeof (struct nfile));
1572 np->type = NCLOBBER;
1577 } else { /* c == '<' */
1581 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1582 np = (union node *)stalloc(sizeof (struct nhere));
1586 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1588 if ((c = pgetc()) == '-') {
1589 heredoc->striptabs = 1;
1591 heredoc->striptabs = 0;
1594 } else if (c == '&')
1604 np->nfile.fd = digit_val(fd);
1606 goto parseredir_return;
1611 * Parse a substitution. At this point, we have read the dollar sign
1621 static const char types[] = "}-+?=";
1622 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1628 if (c == '(') { /* $(command) or $((arith)) */
1629 if (pgetc() == '(') {
1633 out = parsebackq(out, &bqlist, 0,
1634 state[level].syntax == DQSYNTAX &&
1635 (eofmark == NULL || newvarnest > 0),
1636 state[level].syntax == DQSYNTAX ||
1637 state[level].syntax == ARISYNTAX);
1639 } else if (c == '{' || is_name(c) || is_special(c)) {
1640 USTPUTC(CTLVAR, out);
1641 typeloc = out - stackblock();
1642 USTPUTC(VSNORMAL, out);
1651 if (!is_eof(c) && is_name(c)) {
1657 } while (!is_eof(c) && is_in_name(c));
1659 strncmp(out - length, "LINENO", length) == 0) {
1660 /* Replace the variable name with the
1661 * current line number. */
1664 linno -= funclinno - 1;
1665 snprintf(buf, sizeof(buf), "%d", linno);
1670 } else if (is_digit(c)) {
1671 if (bracketed_name) {
1675 } while (is_digit(c));
1680 } else if (is_special(c)) {
1683 if (subtype == 0 && c1 == '#') {
1685 if (strchr(types, c) == NULL && c != ':' &&
1686 c != '#' && c != '%')
1690 if (c1 != '}' && c == '}') {
1705 else if (c == '\n' || c == PEOF)
1706 synerror("Unexpected end of line in substitution");
1707 else if (BASESYNTAX[c] != CCTL)
1717 p = strchr(types, c);
1719 if (c == '\n' || c == PEOF)
1720 synerror("Unexpected end of line in substitution");
1723 if (BASESYNTAX[c] != CCTL)
1727 subtype = p - types + VSNORMAL;
1733 subtype = c == '#' ? VSTRIMLEFT :
1743 } else if (subtype != VSERROR) {
1744 if (subtype == VSLENGTH && c != '}')
1749 if (state[level].syntax == DQSYNTAX ||
1750 state[level].syntax == ARISYNTAX)
1752 *(stackblock() + typeloc) = subtype | flags;
1753 if (subtype != VSNORMAL) {
1754 if (level + 1 >= maxnest) {
1756 if (state == state_static) {
1757 state = parser_temp_alloc(
1758 maxnest * sizeof(*state));
1759 memcpy(state, state_static,
1760 MAXNEST_static * sizeof(*state));
1762 state = parser_temp_realloc(state,
1763 maxnest * sizeof(*state));
1766 state[level].parenlevel = 0;
1767 if (subtype == VSMINUS || subtype == VSPLUS ||
1768 subtype == VSQUESTION || subtype == VSASSIGN) {
1770 * For operators that were in the Bourne shell,
1771 * inherit the double-quote state.
1773 state[level].syntax = state[level - 1].syntax;
1774 state[level].category = TSTATE_VAR_OLD;
1777 * The other operators take a pattern,
1778 * so go to BASESYNTAX.
1779 * Also, ' and " are now special, even
1780 * in here documents.
1782 state[level].syntax = BASESYNTAX;
1783 state[level].category = TSTATE_VAR_NEW;
1787 } else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1788 /* $'cstylequotes' */
1789 USTPUTC(CTLQUOTEMARK, out);
1790 state[level].syntax = SQSYNTAX;
1796 goto parsesub_return;
1801 * Parse an arithmetic expansion (indicate start of one and set state)
1805 if (level + 1 >= maxnest) {
1807 if (state == state_static) {
1808 state = parser_temp_alloc(
1809 maxnest * sizeof(*state));
1810 memcpy(state, state_static,
1811 MAXNEST_static * sizeof(*state));
1813 state = parser_temp_realloc(state,
1814 maxnest * sizeof(*state));
1817 state[level].syntax = ARISYNTAX;
1818 state[level].parenlevel = 0;
1819 state[level].category = TSTATE_ARITH;
1820 USTPUTC(CTLARI, out);
1821 if (state[level - 1].syntax == DQSYNTAX)
1825 goto parsearith_return;
1828 } /* end of readtoken */
1832 * Returns true if the text contains nothing to expand (no dollar signs
1837 noexpand(char *text)
1843 while ((c = *p++) != '\0') {
1844 if ( c == CTLQUOTEMARK)
1848 else if (BASESYNTAX[(int)c] == CCTL)
1856 * Return true if the argument is a legal variable name (a letter or
1857 * underscore followed by zero or more letters, underscores, and digits).
1861 goodname(const char *name)
1869 if (! is_in_name(*p))
1877 isassignment(const char *p)
1885 else if (!is_in_name(*p))
1893 consumetoken(int token)
1895 if (readtoken() != token)
1901 * Called when an unexpected token is read during the parse. The argument
1902 * is the token that is expected, or -1 if more than one type of token can
1903 * occur at this point.
1907 synexpect(int token)
1912 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1913 tokname[lasttoken], tokname[token]);
1915 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1922 synerror(const char *msg)
1925 outfmt(out2, "%s: %d: ", commandname, startlinno);
1926 outfmt(out2, "Syntax error: %s\n", msg);
1927 error((char *)NULL);
1931 setprompt(int which)
1933 whichprompt = which;
1939 out2str(getprompt(NULL));
1945 * called by editline -- any expansions to the prompt
1946 * should be added here.
1949 getprompt(void *unused __unused)
1951 static char ps[PROMPTLEN];
1955 static char internal_error[] = "??";
1958 * Select prompt format.
1960 switch (whichprompt) {
1971 return internal_error;
1975 * Format prompt string.
1977 for (i = 0; (i < PROMPTLEN - 1) && (*fmt != '\0'); i++, fmt++)
1984 * \h specifies just the local hostname,
1985 * \H specifies fully-qualified hostname.
1990 gethostname(&ps[i], PROMPTLEN - i - 1);
1991 ps[PROMPTLEN - 1] = '\0';
1992 /* Skip to end of hostname. */
1993 trim = (*fmt == 'h') ? '.' : '\0';
1994 while ((ps[i] != '\0') && (ps[i] != trim))
2000 * Working directory.
2002 * \W specifies just the final component,
2003 * \w specifies the entire path.
2007 pwd = lookupvar("PWD");
2008 if (pwd == NULL || *pwd == '\0')
2011 *pwd == '/' && pwd[1] != '\0')
2012 strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2015 strlcpy(&ps[i], pwd, PROMPTLEN - i);
2016 /* Skip to end of path. */
2017 while (ps[i + 1] != '\0')
2024 * '$' for normal users, '#' for root.
2027 ps[i] = (geteuid() != 0) ? '$' : '#';
2038 * Emit unrecognized formats verbatim.
2042 if (i < PROMPTLEN - 2)
2054 expandstr(const char *ps)
2057 struct jmploc jmploc;
2058 struct jmploc *const savehandler = handler;
2059 const int saveprompt = doprompt;
2060 struct parsefile *const savetopfile = getcurrentfile();
2061 struct parser_temp *const saveparser_temp = parser_temp;
2062 const char *result = NULL;
2064 if (!setjmp(jmploc.loc)) {
2067 setinputstring(ps, 1);
2069 readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2070 if (backquotelist != NULL)
2071 error("Command substitution not allowed here");
2075 n.narg.text = wordtext;
2076 n.narg.backquote = backquotelist;
2078 expandarg(&n, NULL, 0);
2079 result = stackblock();
2082 handler = savehandler;
2083 doprompt = saveprompt;
2084 popfilesupto(savetopfile);
2085 if (parser_temp != saveparser_temp) {
2086 parser_temp_free_all();
2087 parser_temp = saveparser_temp;
2089 if (result != NULL) {
2091 } else if (exception == EXINT)