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 MKINIT int tokpushback; /* last token pushed back */
100 static char *wordtext; /* text of last word returned by readtoken */
101 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
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, 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 void parsefname(void);
118 static void parseheredoc(void);
119 static int peektoken(void);
120 static int readtoken(void);
121 static int xxreadtoken(void);
122 static int readtoken1(int, char const *, char *, int);
123 static int noexpand(char *);
124 static void synexpect(int) __dead2;
125 static void synerror(const char *) __dead2;
126 static void setprompt(int);
130 parser_temp_alloc(size_t len)
132 struct parser_temp *t;
135 t = ckmalloc(sizeof(*t));
137 t->next = parser_temp;
139 t->data = ckmalloc(len);
146 parser_temp_realloc(void *ptr, size_t len)
148 struct parser_temp *t;
153 error("bug: parser_temp_realloc misused");
154 t->data = ckrealloc(t->data, len);
161 parser_temp_free_upto(void *ptr)
163 struct parser_temp *t;
167 while (parser_temp != NULL && !done) {
169 parser_temp = t->next;
170 done = t->data == ptr;
176 error("bug: parser_temp_free_upto misused");
181 parser_temp_free_all(void)
183 struct parser_temp *t;
186 while (parser_temp != NULL) {
188 parser_temp = t->next;
197 * Read and parse a command. Returns NEOF on end of file. (NULL is a
198 * valid parse tree indicating a blank line.)
202 parsecmd(int interact)
206 /* This assumes the parser is not re-entered,
207 * which could happen if we add command substitution on PS1/PS2.
209 parser_temp_free_all();
230 list(int nlflag, int erflag)
232 union node *ntop, *n1, *n2, *n3;
235 checkkwd = CHKNL | CHKKWD | CHKALIAS;
236 if (!nlflag && !erflag && tokendlist[peektoken()])
242 if (tok == TBACKGND) {
243 if (n2->type == NPIPE) {
244 n2->npipe.backgnd = 1;
245 } else if (n2->type == NREDIR) {
248 n3 = (union node *)stalloc(sizeof (struct nredir));
251 n3->nredir.redirect = NULL;
257 else if (n1 == NULL) {
258 n1 = (union node *)stalloc(sizeof (struct nbinary));
260 n1->nbinary.ch1 = ntop;
261 n1->nbinary.ch2 = n2;
265 n3 = (union node *)stalloc(sizeof (struct nbinary));
267 n3->nbinary.ch1 = n1->nbinary.ch2;
268 n3->nbinary.ch2 = n2;
269 n1->nbinary.ch2 = n3;
282 } else if (tok == TEOF && nlflag) {
288 checkkwd = CHKNL | CHKKWD | CHKALIAS;
289 if (!nlflag && !erflag && tokendlist[peektoken()])
296 pungetc(); /* push back EOF on input */
299 if (nlflag || erflag)
312 union node *n1, *n2, *n3;
317 if ((t = readtoken()) == TAND) {
319 } else if (t == TOR) {
326 n3 = (union node *)stalloc(sizeof (struct nbinary));
328 n3->nbinary.ch1 = n1;
329 n3->nbinary.ch2 = n2;
339 union node *n1, *n2, *pipenode;
340 struct nodelist *lp, *prev;
344 checkkwd = CHKNL | CHKKWD | CHKALIAS;
345 TRACE(("pipeline: entered\n"));
346 while (readtoken() == TNOT)
350 if (readtoken() == TPIPE) {
351 pipenode = (union node *)stalloc(sizeof (struct npipe));
352 pipenode->type = NPIPE;
353 pipenode->npipe.backgnd = 0;
354 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
355 pipenode->npipe.cmdlist = lp;
359 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
360 checkkwd = CHKNL | CHKKWD | CHKALIAS;
368 } while (readtoken() == TPIPE);
374 n2 = (union node *)stalloc(sizeof (struct nnot));
388 union node *ap, **app;
389 union node *cp, **cpp;
390 union node *redir, **rpp;
394 checkkwd = CHKNL | CHKKWD | CHKALIAS;
400 /* Check for redirection which may precede command */
401 while (readtoken() == TREDIR) {
402 *rpp = n2 = redirnode;
403 rpp = &n2->nfile.next;
408 switch (readtoken()) {
410 n1 = (union node *)stalloc(sizeof (struct nif));
412 if ((n1->nif.test = list(0, 0)) == NULL)
414 if (readtoken() != TTHEN)
416 n1->nif.ifpart = list(0, 0);
418 while (readtoken() == TELIF) {
419 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
420 n2 = n2->nif.elsepart;
422 if ((n2->nif.test = list(0, 0)) == NULL)
424 if (readtoken() != TTHEN)
426 n2->nif.ifpart = list(0, 0);
428 if (lasttoken == TELSE)
429 n2->nif.elsepart = list(0, 0);
431 n2->nif.elsepart = NULL;
434 if (readtoken() != TFI)
436 checkkwd = CHKKWD | CHKALIAS;
441 n1 = (union node *)stalloc(sizeof (struct nbinary));
442 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
443 if ((n1->nbinary.ch1 = list(0, 0)) == NULL)
445 if ((got=readtoken()) != TDO) {
446 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
449 n1->nbinary.ch2 = list(0, 0);
450 if (readtoken() != TDONE)
452 checkkwd = CHKKWD | CHKALIAS;
456 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
457 synerror("Bad for loop variable");
458 n1 = (union node *)stalloc(sizeof (struct nfor));
460 n1->nfor.var = wordtext;
461 while (readtoken() == TNL)
463 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
465 while (readtoken() == TWORD) {
466 n2 = (union node *)stalloc(sizeof (struct narg));
468 n2->narg.text = wordtext;
469 n2->narg.backquote = backquotelist;
471 app = &n2->narg.next;
475 if (lasttoken != TNL && lasttoken != TSEMI)
478 static char argvars[5] = {
479 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
481 n2 = (union node *)stalloc(sizeof (struct narg));
483 n2->narg.text = argvars;
484 n2->narg.backquote = NULL;
485 n2->narg.next = NULL;
488 * Newline or semicolon here is optional (but note
489 * that the original Bourne shell only allowed NL).
491 if (lasttoken != TNL && lasttoken != TSEMI)
494 checkkwd = CHKNL | CHKKWD | CHKALIAS;
495 if ((t = readtoken()) == TDO)
497 else if (t == TBEGIN)
501 n1->nfor.body = list(0, 0);
502 if (readtoken() != t)
504 checkkwd = CHKKWD | CHKALIAS;
507 n1 = (union node *)stalloc(sizeof (struct ncase));
509 if (readtoken() != TWORD)
511 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
513 n2->narg.text = wordtext;
514 n2->narg.backquote = backquotelist;
515 n2->narg.next = NULL;
516 while (readtoken() == TNL);
517 if (lasttoken != TWORD || ! equal(wordtext, "in"))
518 synerror("expecting \"in\"");
519 cpp = &n1->ncase.cases;
520 checkkwd = CHKNL | CHKKWD, readtoken();
521 while (lasttoken != TESAC) {
522 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
524 app = &cp->nclist.pattern;
525 if (lasttoken == TLP)
528 *app = ap = (union node *)stalloc(sizeof (struct narg));
530 ap->narg.text = wordtext;
531 ap->narg.backquote = backquotelist;
532 checkkwd = CHKNL | CHKKWD;
533 if (readtoken() != TPIPE)
535 app = &ap->narg.next;
538 ap->narg.next = NULL;
539 if (lasttoken != TRP)
541 cp->nclist.body = list(0, 0);
543 checkkwd = CHKNL | CHKKWD | CHKALIAS;
544 if ((t = readtoken()) != TESAC) {
547 else if (t == TFALLTHRU)
548 cp->type = NCLISTFALLTHRU;
551 checkkwd = CHKNL | CHKKWD, readtoken();
553 cpp = &cp->nclist.next;
556 checkkwd = CHKKWD | CHKALIAS;
559 n1 = (union node *)stalloc(sizeof (struct nredir));
560 n1->type = NSUBSHELL;
561 n1->nredir.n = list(0, 0);
562 n1->nredir.redirect = NULL;
563 if (readtoken() != TRP)
565 checkkwd = CHKKWD | CHKALIAS;
570 if (readtoken() != TEND)
572 checkkwd = CHKKWD | CHKALIAS;
574 /* Handle an empty command like other simple commands. */
580 * An empty command before a ; doesn't make much sense, and
581 * should certainly be disallowed in the case of `if ;'.
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) {
646 n = (union node *)stalloc(sizeof (struct narg));
648 n->narg.text = wordtext;
649 n->narg.backquote = backquotelist;
652 if (savecheckkwd != 0 && !isassignment(wordtext))
654 } else if (lasttoken == TREDIR) {
655 *rpp = n = redirnode;
656 rpp = &n->nfile.next;
657 parsefname(); /* read name of redirection file */
658 } else if (lasttoken == TLP && app == &args->narg.next
659 && rpp == orig_rpp) {
660 /* We have a function */
661 if (readtoken() != TRP)
665 * - Require plain text.
666 * - Functions with '/' cannot be called.
668 * - Reject ksh extended glob patterns.
670 if (!noexpand(n->narg.text) || quoteflag ||
671 strchr(n->narg.text, '/') ||
673 n->narg.text[strlen(n->narg.text) - 1]))
674 synerror("Bad function name");
675 rmescapes(n->narg.text);
676 if (find_builtin(n->narg.text, &special) >= 0 &&
678 synerror("Cannot override a special builtin with a function");
680 n->narg.next = command();
690 n = (union node *)stalloc(sizeof (struct ncmd));
693 n->ncmd.redirect = redir;
702 n = (union node *)stalloc(sizeof (struct narg));
705 n->narg.text = wordtext;
706 n->narg.backquote = backquotelist;
711 fixredir(union node *n, const char *text, int err)
713 TRACE(("Fix redir %s %d\n", text, err));
715 n->ndup.vname = NULL;
717 if (is_digit(text[0]) && text[1] == '\0')
718 n->ndup.dupfd = digit_val(text[0]);
719 else if (text[0] == '-' && text[1] == '\0')
724 synerror("Bad fd number");
726 n->ndup.vname = makename();
734 union node *n = redirnode;
736 if (readtoken() != TWORD)
738 if (n->type == NHERE) {
739 struct heredoc *here = heredoc;
745 TRACE(("Here document %d\n", n->type));
746 if (here->striptabs) {
747 while (*wordtext == '\t')
750 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
751 synerror("Illegal eof marker for << redirection");
753 here->eofmark = wordtext;
755 if (heredoclist == NULL)
758 for (p = heredoclist ; p->next ; p = p->next);
761 } else if (n->type == NTOFD || n->type == NFROMFD) {
762 fixredir(n, wordtext, 0);
764 n->nfile.fname = makename();
770 * Input any here documents.
776 struct heredoc *here;
779 while (heredoclist) {
781 heredoclist = here->next;
786 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
787 here->eofmark, here->striptabs);
788 n = (union node *)stalloc(sizeof (struct narg));
791 n->narg.text = wordtext;
792 n->narg.backquote = backquotelist;
793 here->here->nhere.doc = n;
813 int alreadyseen = tokpushback;
822 if (checkkwd & CHKNL) {
830 * check for keywords and aliases
832 if (t == TWORD && !quoteflag)
834 const char * const *pp;
836 if (checkkwd & CHKKWD)
837 for (pp = parsekwd; *pp; pp++) {
838 if (**pp == *wordtext && equal(*pp, wordtext))
840 lasttoken = t = pp - parsekwd + KWDOFFSET;
841 TRACE(("keyword %s recognized\n", tokname[t]));
845 if (checkkwd & CHKALIAS &&
846 (ap = lookupalias(wordtext, 1)) != NULL) {
847 pushstring(ap->val, strlen(ap->val), ap);
857 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
859 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
866 * Read the next input token.
867 * If the token is a word, we set backquotelist to the list of cmds in
868 * backquotes. We set quoteflag to true if any part of the word was
870 * If the token is TREDIR, then we set redirnode to a structure containing
872 * In all cases, the variable startlinno is set to the number of the line
873 * on which the token starts.
875 * [Change comment: here documents and internal procedures]
876 * [Readtoken shouldn't have any arguments. Perhaps we should make the
877 * word parsing code into a separate routine. In this case, readtoken
878 * doesn't need to have any internal procedures, but parseword does.
879 * We could also make parseoperator in essence the main routine, and
880 * have parseword (readtoken1?) handle both words and redirection.]
883 #define RETURN(token) return lasttoken = token
899 for (;;) { /* until token or start of word found */
905 while ((c = pgetc()) != '\n' && c != PEOF);
909 if (pgetc() == '\n') {
910 startlinno = ++plinno;
921 needprompt = doprompt;
952 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
957 #define MAXNEST_static 8
960 const char *syntax; /* *SYNTAX */
961 int parenlevel; /* levels of parentheses in arithmetic */
962 enum tokenstate_category
965 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
966 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
973 * Called to parse command substitutions.
977 parsebackq(char *out, struct nodelist **pbqlist,
978 int oldstyle, int dblquote, int quoted)
980 struct nodelist **nlpp;
983 struct jmploc jmploc;
984 struct jmploc *const savehandler = handler;
987 const int bq_startlinno = plinno;
988 char *volatile ostr = NULL;
989 struct parsefile *const savetopfile = getcurrentfile();
990 struct heredoc *const saveheredoclist = heredoclist;
991 struct heredoc *here;
994 if (setjmp(jmploc.loc)) {
995 popfilesupto(savetopfile);
1000 heredoclist = saveheredoclist;
1001 handler = savehandler;
1002 if (exception == EXERROR) {
1003 startlinno = bq_startlinno;
1004 synerror("Error in command substitution");
1006 longjmp(handler->loc, 1);
1009 savelen = out - stackblock();
1011 str = ckmalloc(savelen);
1012 memcpy(str, stackblock(), savelen);
1018 /* We must read until the closing backquote, giving special
1019 treatment to some slashes, and then push the string and
1020 reread it as input, interpreting it normally. */
1026 STARTSTACKSTR(oout);
1032 CHECKSTRSPACE(2, oout);
1033 switch (c = pgetc()) {
1038 if ((c = pgetc()) == '\n') {
1045 * If eating a newline, avoid putting
1046 * the newline into the new character
1047 * stream (via the USTPUTC after the
1052 if (c != '\\' && c != '`' && c != '$'
1053 && (!dblquote || c != '"'))
1054 USTPUTC('\\', oout);
1059 needprompt = doprompt;
1063 startlinno = plinno;
1064 synerror("EOF in backquote substitution");
1073 USTPUTC('\0', oout);
1074 olen = oout - stackblock();
1076 ostr = ckmalloc(olen);
1077 memcpy(ostr, stackblock(), olen);
1078 setinputstring(ostr, 1);
1083 nlpp = &(*nlpp)->next;
1084 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1085 (*nlpp)->next = NULL;
1088 saveprompt = doprompt;
1092 n = list(0, oldstyle);
1095 doprompt = saveprompt;
1097 if (readtoken() != TRP)
1104 * Start reading from old file again, ignoring any pushed back
1105 * tokens left from the backquote parsing
1111 CHECKSTRSPACE(savelen + 1, out);
1114 memcpy(out, str, savelen);
1115 STADJUST(savelen, out);
1123 here = saveheredoclist;
1125 while (here->next != NULL)
1127 here->next = heredoclist;
1128 heredoclist = saveheredoclist;
1130 handler = savehandler;
1133 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1135 USTPUTC(CTLBACKQ, out);
1141 * Called to parse a backslash escape sequence inside $'...'.
1142 * The backslash has already been read.
1145 readcstyleesc(char *out)
1152 synerror("Unterminated quoted string");
1165 case 'a': v = '\a'; break;
1166 case 'b': v = '\b'; break;
1167 case 'e': v = '\033'; break;
1168 case 'f': v = '\f'; break;
1169 case 'n': v = '\n'; break;
1170 case 'r': v = '\r'; break;
1171 case 't': v = '\t'; break;
1172 case 'v': v = '\v'; break;
1177 if (c >= '0' && c <= '9')
1178 v = (v << 4) + c - '0';
1179 else if (c >= 'A' && c <= 'F')
1180 v = (v << 4) + c - 'A' + 10;
1181 else if (c >= 'a' && c <= 'f')
1182 v = (v << 4) + c - 'a' + 10;
1188 case '0': case '1': case '2': case '3':
1189 case '4': case '5': case '6': case '7':
1192 if (c >= '0' && c <= '7') {
1196 if (c >= '0' && c <= '7') {
1206 if (c < 0x3f || c > 0x7a || c == 0x60)
1207 synerror("Bad escape sequence");
1208 if (c == '\\' && pgetc() != '\\')
1209 synerror("Bad escape sequence");
1217 n = c == 'U' ? 8 : 4;
1219 for (i = 0; i < n; i++) {
1221 if (c >= '0' && c <= '9')
1222 v = (v << 4) + c - '0';
1223 else if (c >= 'A' && c <= 'F')
1224 v = (v << 4) + c - 'A' + 10;
1225 else if (c >= 'a' && c <= 'f')
1226 v = (v << 4) + c - 'a' + 10;
1228 synerror("Bad escape sequence");
1230 if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1231 synerror("Bad escape sequence");
1232 /* We really need iconv here. */
1233 if (initial_localeisutf8 && v > 127) {
1234 CHECKSTRSPACE(4, out);
1236 * We cannot use wctomb() as the locale may have
1240 USTPUTC(0xc0 | v >> 6, out);
1241 USTPUTC(0x80 | (v & 0x3f), out);
1243 } else if (v <= 0xffff) {
1244 USTPUTC(0xe0 | v >> 12, out);
1245 USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1246 USTPUTC(0x80 | (v & 0x3f), out);
1248 } else if (v <= 0x10ffff) {
1249 USTPUTC(0xf0 | v >> 18, out);
1250 USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1251 USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1252 USTPUTC(0x80 | (v & 0x3f), out);
1260 synerror("Bad escape sequence");
1264 * We can't handle NUL bytes.
1265 * POSIX says we should skip till the closing quote.
1268 while ((c = pgetc()) != '\'') {
1272 synerror("Unterminated quoted string");
1277 if (SQSYNTAX[v] == CCTL)
1278 USTPUTC(CTLESC, out);
1285 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
1286 * is not NULL, read a here document. In the latter case, eofmark is the
1287 * word which marks the end of the document and striptabs is true if
1288 * leading tabs should be stripped from the document. The argument firstc
1289 * is the first character of the input token or document.
1291 * Because C does not have internal subroutines, I have simulated them
1292 * using goto's to implement the subroutine linkage. The following macros
1293 * will run code that appears at the end of readtoken1.
1296 #define CHECKEND() {goto checkend; checkend_return:;}
1297 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
1298 #define PARSESUB() {goto parsesub; parsesub_return:;}
1299 #define PARSEARITH() {goto parsearith; parsearith_return:;}
1302 readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1307 char line[EOFMARKLEN + 1];
1308 struct nodelist *bqlist;
1313 struct tokenstate state_static[MAXNEST_static];
1314 int maxnest = MAXNEST_static;
1315 struct tokenstate *state = state_static;
1318 startlinno = plinno;
1323 state[level].syntax = initialsyntax;
1324 state[level].parenlevel = 0;
1325 state[level].category = TSTATE_TOP;
1328 loop: { /* for each line, until end of word */
1329 CHECKEND(); /* set c to PEOF if at end of here document */
1330 for (;;) { /* until end of line or end of word */
1331 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
1333 synentry = state[level].syntax[c];
1336 case CNL: /* '\n' */
1337 if (state[level].syntax == BASESYNTAX)
1338 goto endword; /* exit outer loop */
1346 goto loop; /* continue outer loop */
1349 out = readcstyleesc(out);
1357 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1358 USTPUTC(CTLESC, out);
1361 case CBACK: /* backslash */
1366 } else if (c == '\n') {
1373 if (state[level].syntax == DQSYNTAX &&
1374 c != '\\' && c != '`' && c != '$' &&
1375 (c != '"' || (eofmark != NULL &&
1376 newvarnest == 0)) &&
1377 (c != '}' || state[level].category != TSTATE_VAR_OLD))
1379 if ((eofmark == NULL ||
1381 state[level].syntax == BASESYNTAX)
1382 USTPUTC(CTLQUOTEMARK, out);
1383 if (SQSYNTAX[c] == CCTL)
1384 USTPUTC(CTLESC, out);
1386 if ((eofmark == NULL ||
1388 state[level].syntax == BASESYNTAX &&
1389 state[level].category == TSTATE_VAR_OLD)
1390 USTPUTC(CTLQUOTEEND, out);
1395 USTPUTC(CTLQUOTEMARK, out);
1396 state[level].syntax = SQSYNTAX;
1400 USTPUTC(CTLQUOTEMARK, out);
1401 state[level].syntax = DQSYNTAX;
1404 if (eofmark != NULL && newvarnest == 0)
1407 if (state[level].category == TSTATE_VAR_OLD)
1408 USTPUTC(CTLQUOTEEND, out);
1409 state[level].syntax = BASESYNTAX;
1413 case CVAR: /* '$' */
1414 PARSESUB(); /* parse substitution */
1416 case CENDVAR: /* '}' */
1418 ((state[level].category == TSTATE_VAR_OLD &&
1419 state[level].syntax ==
1420 state[level - 1].syntax) ||
1421 (state[level].category == TSTATE_VAR_NEW &&
1422 state[level].syntax == BASESYNTAX))) {
1423 if (state[level].category == TSTATE_VAR_NEW)
1426 USTPUTC(CTLENDVAR, out);
1431 case CLP: /* '(' in arithmetic */
1432 state[level].parenlevel++;
1435 case CRP: /* ')' in arithmetic */
1436 if (state[level].parenlevel > 0) {
1438 --state[level].parenlevel;
1440 if (pgetc() == ')') {
1442 state[level].category == TSTATE_ARITH) {
1444 USTPUTC(CTLENDARI, out);
1450 * (don't 2nd guess - no error)
1457 case CBQUOTE: /* '`' */
1458 out = parsebackq(out, &bqlist, 1,
1459 state[level].syntax == DQSYNTAX &&
1460 (eofmark == NULL || newvarnest > 0),
1461 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1464 goto endword; /* exit outer loop */
1469 goto endword; /* exit outer loop */
1476 if (state[level].syntax == ARISYNTAX)
1477 synerror("Missing '))'");
1478 if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1479 synerror("Unterminated quoted string");
1480 if (state[level].category == TSTATE_VAR_OLD ||
1481 state[level].category == TSTATE_VAR_NEW) {
1482 startlinno = plinno;
1483 synerror("Missing '}'");
1485 if (state != state_static)
1486 parser_temp_free_upto(state);
1488 len = out - stackblock();
1490 if (eofmark == NULL) {
1491 if ((c == '>' || c == '<')
1494 && (*out == '\0' || is_digit(*out))) {
1496 return lasttoken = TREDIR;
1502 backquotelist = bqlist;
1503 grabstackblock(len);
1505 return lasttoken = TWORD;
1506 /* end of readtoken routine */
1510 * Check to see whether we are at the end of the here document. When this
1511 * is called, c is set to the first character of the next input line. If
1512 * we are at the end of the here document, this routine sets the c to PEOF.
1521 if (c == *eofmark) {
1522 if (pfgets(line, sizeof line) != NULL) {
1526 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1527 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1531 needprompt = doprompt;
1534 pushstring(line, strlen(line), NULL);
1539 goto checkend_return;
1544 * Parse a redirection operator. The variable "out" points to a string
1545 * specifying the fd to be redirected. The variable "c" contains the
1546 * first character of the redirection operator.
1553 np = (union node *)stalloc(sizeof (struct nfile));
1562 np->type = NCLOBBER;
1567 } else { /* c == '<' */
1571 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1572 np = (union node *)stalloc(sizeof (struct nhere));
1576 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1578 if ((c = pgetc()) == '-') {
1579 heredoc->striptabs = 1;
1581 heredoc->striptabs = 0;
1584 } else if (c == '&')
1594 np->nfile.fd = digit_val(fd);
1596 goto parseredir_return;
1601 * Parse a substitution. At this point, we have read the dollar sign
1611 static const char types[] = "}-+?=";
1612 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1618 if (c == '(') { /* $(command) or $((arith)) */
1619 if (pgetc() == '(') {
1623 out = parsebackq(out, &bqlist, 0,
1624 state[level].syntax == DQSYNTAX &&
1625 (eofmark == NULL || newvarnest > 0),
1626 state[level].syntax == DQSYNTAX ||
1627 state[level].syntax == ARISYNTAX);
1629 } else if (c == '{' || is_name(c) || is_special(c)) {
1630 USTPUTC(CTLVAR, out);
1631 typeloc = out - stackblock();
1632 USTPUTC(VSNORMAL, out);
1641 if (!is_eof(c) && is_name(c)) {
1647 } while (!is_eof(c) && is_in_name(c));
1649 strncmp(out - length, "LINENO", length) == 0) {
1650 /* Replace the variable name with the
1651 * current line number. */
1654 linno -= funclinno - 1;
1655 snprintf(buf, sizeof(buf), "%d", linno);
1660 } else if (is_digit(c)) {
1661 if (bracketed_name) {
1665 } while (is_digit(c));
1670 } else if (is_special(c)) {
1673 if (subtype == 0 && c1 == '#') {
1675 if (strchr(types, c) == NULL && c != ':' &&
1676 c != '#' && c != '%')
1680 if (c1 != '}' && c == '}') {
1695 else if (c == '\n' || c == PEOF)
1696 synerror("Unexpected end of line in substitution");
1707 p = strchr(types, c);
1709 if (c == '\n' || c == PEOF)
1710 synerror("Unexpected end of line in substitution");
1716 subtype = p - types + VSNORMAL;
1722 subtype = c == '#' ? VSTRIMLEFT :
1732 } else if (subtype != VSERROR) {
1733 if (subtype == VSLENGTH && c != '}')
1738 if (state[level].syntax == DQSYNTAX ||
1739 state[level].syntax == ARISYNTAX)
1741 *(stackblock() + typeloc) = subtype | flags;
1742 if (subtype != VSNORMAL) {
1743 if (level + 1 >= maxnest) {
1745 if (state == state_static) {
1746 state = parser_temp_alloc(
1747 maxnest * sizeof(*state));
1748 memcpy(state, state_static,
1749 MAXNEST_static * sizeof(*state));
1751 state = parser_temp_realloc(state,
1752 maxnest * sizeof(*state));
1755 state[level].parenlevel = 0;
1756 if (subtype == VSMINUS || subtype == VSPLUS ||
1757 subtype == VSQUESTION || subtype == VSASSIGN) {
1759 * For operators that were in the Bourne shell,
1760 * inherit the double-quote state.
1762 state[level].syntax = state[level - 1].syntax;
1763 state[level].category = TSTATE_VAR_OLD;
1766 * The other operators take a pattern,
1767 * so go to BASESYNTAX.
1768 * Also, ' and " are now special, even
1769 * in here documents.
1771 state[level].syntax = BASESYNTAX;
1772 state[level].category = TSTATE_VAR_NEW;
1776 } else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1777 /* $'cstylequotes' */
1778 USTPUTC(CTLQUOTEMARK, out);
1779 state[level].syntax = SQSYNTAX;
1785 goto parsesub_return;
1790 * Parse an arithmetic expansion (indicate start of one and set state)
1794 if (level + 1 >= maxnest) {
1796 if (state == state_static) {
1797 state = parser_temp_alloc(
1798 maxnest * sizeof(*state));
1799 memcpy(state, state_static,
1800 MAXNEST_static * sizeof(*state));
1802 state = parser_temp_realloc(state,
1803 maxnest * sizeof(*state));
1806 state[level].syntax = ARISYNTAX;
1807 state[level].parenlevel = 0;
1808 state[level].category = TSTATE_ARITH;
1809 USTPUTC(CTLARI, out);
1810 if (state[level - 1].syntax == DQSYNTAX)
1814 goto parsearith_return;
1817 } /* end of readtoken */
1829 * Returns true if the text contains nothing to expand (no dollar signs
1834 noexpand(char *text)
1840 while ((c = *p++) != '\0') {
1841 if ( c == CTLQUOTEMARK)
1845 else if (BASESYNTAX[(int)c] == CCTL)
1853 * Return true if the argument is a legal variable name (a letter or
1854 * underscore followed by zero or more letters, underscores, and digits).
1858 goodname(const char *name)
1866 if (! is_in_name(*p))
1874 isassignment(const char *p)
1882 else if (!is_in_name(*p))
1890 * Called when an unexpected token is read during the parse. The argument
1891 * is the token that is expected, or -1 if more than one type of token can
1892 * occur at this point.
1896 synexpect(int token)
1901 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1902 tokname[lasttoken], tokname[token]);
1904 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1911 synerror(const char *msg)
1914 outfmt(out2, "%s: %d: ", commandname, startlinno);
1915 outfmt(out2, "Syntax error: %s\n", msg);
1916 error((char *)NULL);
1920 setprompt(int which)
1922 whichprompt = which;
1928 out2str(getprompt(NULL));
1934 * called by editline -- any expansions to the prompt
1935 * should be added here.
1938 getprompt(void *unused __unused)
1940 static char ps[PROMPTLEN];
1944 static char internal_error[] = "??";
1947 * Select prompt format.
1949 switch (whichprompt) {
1960 return internal_error;
1964 * Format prompt string.
1966 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1973 * \h specifies just the local hostname,
1974 * \H specifies fully-qualified hostname.
1979 gethostname(&ps[i], PROMPTLEN - i);
1980 /* Skip to end of hostname. */
1981 trim = (*fmt == 'h') ? '.' : '\0';
1982 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1987 * Working directory.
1989 * \W specifies just the final component,
1990 * \w specifies the entire path.
1994 pwd = lookupvar("PWD");
1998 *pwd == '/' && pwd[1] != '\0')
1999 strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2002 strlcpy(&ps[i], pwd, PROMPTLEN - i);
2003 /* Skip to end of path. */
2004 while (ps[i + 1] != '\0')
2011 * '$' for normal users, '#' for root.
2014 ps[i] = (geteuid() != 0) ? '$' : '#';
2025 * Emit unrecognized formats verbatim.
2043 struct jmploc jmploc;
2044 struct jmploc *const savehandler = handler;
2045 const int saveprompt = doprompt;
2046 struct parsefile *const savetopfile = getcurrentfile();
2047 struct parser_temp *const saveparser_temp = parser_temp;
2048 const char *result = NULL;
2050 if (!setjmp(jmploc.loc)) {
2053 setinputstring(ps, 1);
2055 readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2056 if (backquotelist != NULL)
2057 error("Command substitution not allowed here");
2061 n.narg.text = wordtext;
2062 n.narg.backquote = backquotelist;
2064 expandarg(&n, NULL, 0);
2065 result = stackblock();
2068 handler = savehandler;
2069 doprompt = saveprompt;
2070 popfilesupto(savetopfile);
2071 if (parser_temp != saveparser_temp) {
2072 parser_temp_free_all();
2073 parser_temp = saveparser_temp;
2075 if (result != NULL) {
2077 } else if (exception == EXINT)