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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)parser.c 8.7 (Berkeley) 5/16/95
37 * $FreeBSD: src/bin/sh/parser.c,v 1.29.2.9 2002/10/18 11:24:04 tjr Exp $
38 * $DragonFly: src/bin/sh/parser.c,v 1.6 2005/11/06 11:44:02 swildner Exp $
46 #include "expand.h" /* defines rmescapes() */
47 #include "redir.h" /* defines copyfd() */
60 #include "myhistedit.h"
64 * Shell command parser.
69 /* values returned by readtoken */
75 struct heredoc *next; /* next here document in list */
76 union node *here; /* redirection node */
77 char *eofmark; /* string indicating end of input */
78 int striptabs; /* if set, strip leading tabs */
83 STATIC struct heredoc *heredoclist; /* list of here documents to read */
84 STATIC int parsebackquote; /* nonzero if we are inside backquotes */
85 STATIC int doprompt; /* if set, prompt the user */
86 STATIC int needprompt; /* true if interactive and at start of line */
87 STATIC int lasttoken; /* last token read */
88 MKINIT int tokpushback; /* last token pushed back */
89 STATIC char *wordtext; /* text of last word returned by readtoken */
90 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
91 STATIC struct nodelist *backquotelist;
92 STATIC union node *redirnode;
93 STATIC struct heredoc *heredoc;
94 STATIC int quoteflag; /* set if (part of) last token was quoted */
95 STATIC int startlinno; /* line # where last token started */
97 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
98 static int noaliases = 0;
100 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
102 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
103 static const char types[] = "}-+?=";
107 STATIC union node *list(int);
108 STATIC union node *andor(void);
109 STATIC union node *pipeline(void);
110 STATIC union node *command(void);
111 STATIC union node *simplecmd(union node **, union node *);
112 STATIC union node *makename(void);
113 STATIC void parsefname(void);
114 STATIC void parseheredoc(void);
115 STATIC int peektoken(void);
116 STATIC int readtoken(void);
117 STATIC int xxreadtoken(void);
118 STATIC int readtoken1(int, char const *, char *, int);
119 STATIC int noexpand(char *);
120 STATIC void synexpect(int);
121 STATIC void synerror(const char *);
122 STATIC void setprompt(int);
126 * Read and parse a command. Returns NEOF on end of file. (NULL is a
127 * valid parse tree indicating a blank line.)
131 parsecmd(int interact)
155 union node *n1, *n2, *n3;
159 if (nlflag == 0 && tokendlist[peektoken()])
165 if (tok == TBACKGND) {
166 if (n2->type == NCMD || n2->type == NPIPE) {
167 n2->ncmd.backgnd = 1;
168 } else if (n2->type == NREDIR) {
171 n3 = (union node *)stalloc(sizeof (struct nredir));
174 n3->nredir.redirect = NULL;
182 n3 = (union node *)stalloc(sizeof (struct nbinary));
184 n3->nbinary.ch1 = n1;
185 n3->nbinary.ch2 = n2;
202 if (tokendlist[peektoken()])
209 pungetc(); /* push back EOF on input */
225 union node *n1, *n2, *n3;
230 if ((t = readtoken()) == TAND) {
232 } else if (t == TOR) {
239 n3 = (union node *)stalloc(sizeof (struct nbinary));
241 n3->nbinary.ch1 = n1;
242 n3->nbinary.ch2 = n2;
252 union node *n1, *n2, *pipenode;
253 struct nodelist *lp, *prev;
257 TRACE(("pipeline: entered\n"));
258 while (readtoken() == TNOT)
262 if (readtoken() == TPIPE) {
263 pipenode = (union node *)stalloc(sizeof (struct npipe));
264 pipenode->type = NPIPE;
265 pipenode->npipe.backgnd = 0;
266 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
267 pipenode->npipe.cmdlist = lp;
271 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
274 } while (readtoken() == TPIPE);
280 n2 = (union node *)stalloc(sizeof (struct nnot));
294 union node *ap, **app;
295 union node *cp, **cpp;
296 union node *redir, **rpp;
304 /* Check for redirection which may precede command */
305 while (readtoken() == TREDIR) {
306 *rpp = n2 = redirnode;
307 rpp = &n2->nfile.next;
312 while (readtoken() == TNOT) {
313 TRACE(("command: TNOT recognized\n"));
318 switch (readtoken()) {
320 n1 = (union node *)stalloc(sizeof (struct nif));
322 n1->nif.test = list(0);
323 if (readtoken() != TTHEN)
325 n1->nif.ifpart = list(0);
327 while (readtoken() == TELIF) {
328 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
329 n2 = n2->nif.elsepart;
331 n2->nif.test = list(0);
332 if (readtoken() != TTHEN)
334 n2->nif.ifpart = list(0);
336 if (lasttoken == TELSE)
337 n2->nif.elsepart = list(0);
339 n2->nif.elsepart = NULL;
342 if (readtoken() != TFI)
349 n1 = (union node *)stalloc(sizeof (struct nbinary));
350 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
351 n1->nbinary.ch1 = list(0);
352 if ((got=readtoken()) != TDO) {
353 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
356 n1->nbinary.ch2 = list(0);
357 if (readtoken() != TDONE)
363 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
364 synerror("Bad for loop variable");
365 n1 = (union node *)stalloc(sizeof (struct nfor));
367 n1->nfor.var = wordtext;
368 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
370 while (readtoken() == TWORD) {
371 n2 = (union node *)stalloc(sizeof (struct narg));
373 n2->narg.text = wordtext;
374 n2->narg.backquote = backquotelist;
376 app = &n2->narg.next;
380 if (lasttoken != TNL && lasttoken != TSEMI)
384 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
387 n2 = (union node *)stalloc(sizeof (struct narg));
389 n2->narg.text = (char *)argvars;
390 n2->narg.backquote = NULL;
391 n2->narg.next = NULL;
394 * Newline or semicolon here is optional (but note
395 * that the original Bourne shell only allowed NL).
397 if (lasttoken != TNL && lasttoken != TSEMI)
401 if ((t = readtoken()) == TDO)
403 else if (t == TBEGIN)
407 n1->nfor.body = list(0);
408 if (readtoken() != t)
413 n1 = (union node *)stalloc(sizeof (struct ncase));
415 if (readtoken() != TWORD)
417 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
419 n2->narg.text = wordtext;
420 n2->narg.backquote = backquotelist;
421 n2->narg.next = NULL;
422 while (readtoken() == TNL);
423 if (lasttoken != TWORD || ! equal(wordtext, "in"))
424 synerror("expecting \"in\"");
425 cpp = &n1->ncase.cases;
426 noaliases = 1; /* turn off alias expansion */
427 checkkwd = 2, readtoken();
428 while (lasttoken != TESAC) {
429 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
431 app = &cp->nclist.pattern;
432 if (lasttoken == TLP)
435 *app = ap = (union node *)stalloc(sizeof (struct narg));
437 ap->narg.text = wordtext;
438 ap->narg.backquote = backquotelist;
439 if (checkkwd = 2, readtoken() != TPIPE)
441 app = &ap->narg.next;
444 ap->narg.next = NULL;
445 if (lasttoken != TRP)
446 noaliases = 0, synexpect(TRP);
447 cp->nclist.body = list(0);
450 if ((t = readtoken()) != TESAC) {
452 noaliases = 0, synexpect(TENDCASE);
454 checkkwd = 2, readtoken();
456 cpp = &cp->nclist.next;
458 noaliases = 0; /* reset alias expansion */
463 n1 = (union node *)stalloc(sizeof (struct nredir));
464 n1->type = NSUBSHELL;
465 n1->nredir.n = list(0);
466 n1->nredir.redirect = NULL;
467 if (readtoken() != TRP)
473 if (readtoken() != TEND)
477 /* Handle an empty command like other simple commands. */
482 * An empty command before a ; doesn't make much sense, and
483 * should certainly be disallowed in the case of `if ;'.
492 n1 = simplecmd(rpp, redir);
498 /* Now check for redirection which may follow command */
499 while (readtoken() == TREDIR) {
500 *rpp = n2 = redirnode;
501 rpp = &n2->nfile.next;
507 if (n1->type != NSUBSHELL) {
508 n2 = (union node *)stalloc(sizeof (struct nredir));
513 n1->nredir.redirect = redir;
518 n2 = (union node *)stalloc(sizeof (struct nnot));
529 simplecmd(union node **rpp, union node *redir)
531 union node *args, **app;
532 union node **orig_rpp = rpp;
533 union node *n = NULL, *n2;
536 /* If we don't have any redirections already, then we must reset */
537 /* rpp to be the address of the local redir variable. */
544 * We save the incoming value, because we need this for shell
545 * functions. There can not be a redirect or an argument between
546 * the function name and the open parenthesis.
550 while (readtoken() == TNOT) {
551 TRACE(("command: TNOT recognized\n"));
557 if (readtoken() == TWORD) {
558 n = (union node *)stalloc(sizeof (struct narg));
560 n->narg.text = wordtext;
561 n->narg.backquote = backquotelist;
564 } else if (lasttoken == TREDIR) {
565 *rpp = n = redirnode;
566 rpp = &n->nfile.next;
567 parsefname(); /* read name of redirection file */
568 } else if (lasttoken == TLP && app == &args->narg.next
569 && rpp == orig_rpp) {
570 /* We have a function */
571 if (readtoken() != TRP)
574 if (! goodname(n->narg.text))
575 synerror("Bad function name");
578 n->narg.next = command();
587 n = (union node *)stalloc(sizeof (struct ncmd));
591 n->ncmd.redirect = redir;
595 n2 = (union node *)stalloc(sizeof (struct nnot));
609 n = (union node *)stalloc(sizeof (struct narg));
612 n->narg.text = wordtext;
613 n->narg.backquote = backquotelist;
618 fixredir(union node *n, const char *text, int err)
620 TRACE(("Fix redir %s %d\n", text, err));
622 n->ndup.vname = NULL;
624 if (is_digit(text[0]) && text[1] == '\0')
625 n->ndup.dupfd = digit_val(text[0]);
626 else if (text[0] == '-' && text[1] == '\0')
631 synerror("Bad fd number");
633 n->ndup.vname = makename();
641 union node *n = redirnode;
643 if (readtoken() != TWORD)
645 if (n->type == NHERE) {
646 struct heredoc *here = heredoc;
652 TRACE(("Here document %d\n", n->type));
653 if (here->striptabs) {
654 while (*wordtext == '\t')
657 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
658 synerror("Illegal eof marker for << redirection");
660 here->eofmark = wordtext;
662 if (heredoclist == NULL)
665 for (p = heredoclist ; p->next ; p = p->next);
668 } else if (n->type == NTOFD || n->type == NFROMFD) {
669 fixredir(n, wordtext, 0);
671 n->nfile.fname = makename();
677 * Input any here documents.
683 struct heredoc *here;
686 while (heredoclist) {
688 heredoclist = here->next;
693 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
694 here->eofmark, here->striptabs);
695 n = (union node *)stalloc(sizeof (struct narg));
698 n->narg.text = wordtext;
699 n->narg.backquote = backquotelist;
700 here->here->nhere.doc = n;
718 int savecheckkwd = checkkwd;
721 int alreadyseen = tokpushback;
740 * check for keywords and aliases
742 if (t == TWORD && !quoteflag)
744 const char * const *pp;
746 for (pp = parsekwd; *pp; pp++) {
747 if (**pp == *wordtext && equal(*pp, wordtext))
749 lasttoken = t = pp - parsekwd + KWDOFFSET;
750 TRACE(("keyword %s recognized\n", tokname[t]));
754 if (noaliases == 0 &&
755 (ap = lookupalias(wordtext, 1)) != NULL) {
756 pushstring(ap->val, strlen(ap->val), ap);
757 checkkwd = savecheckkwd;
762 checkkwd = (t == TNOT) ? savecheckkwd : 0;
766 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
768 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
775 * Read the next input token.
776 * If the token is a word, we set backquotelist to the list of cmds in
777 * backquotes. We set quoteflag to true if any part of the word was
779 * If the token is TREDIR, then we set redirnode to a structure containing
781 * In all cases, the variable startlinno is set to the number of the line
782 * on which the token starts.
784 * [Change comment: here documents and internal procedures]
785 * [Readtoken shouldn't have any arguments. Perhaps we should make the
786 * word parsing code into a separate routine. In this case, readtoken
787 * doesn't need to have any internal procedures, but parseword does.
788 * We could also make parseoperator in essence the main routine, and
789 * have parseword (readtoken1?) handle both words and redirection.]
792 #define RETURN(token) return lasttoken = token
808 for (;;) { /* until token or start of word found */
810 if (c == ' ' || c == '\t')
811 continue; /* quick check for white space first */
816 while ((c = pgetc()) != '\n' && c != PEOF);
820 if (pgetc() == '\n') {
821 startlinno = ++plinno;
832 needprompt = doprompt;
860 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
867 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
868 * is not NULL, read a here document. In the latter case, eofmark is the
869 * word which marks the end of the document and striptabs is true if
870 * leading tabs should be stripped from the document. The argument firstc
871 * is the first character of the input token or document.
873 * Because C does not have internal subroutines, I have simulated them
874 * using goto's to implement the subroutine linkage. The following macros
875 * will run code that appears at the end of readtoken1.
878 #define CHECKEND() {goto checkend; checkend_return:;}
879 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
880 #define PARSESUB() {goto parsesub; parsesub_return:;}
881 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
882 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
883 #define PARSEARITH() {goto parsearith; parsearith_return:;}
886 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
891 char line[EOFMARKLEN + 1];
892 struct nodelist *bqlist;
895 int varnest; /* levels of variables expansion */
896 int arinest; /* levels of arithmetic expansion */
897 int parenlevel; /* levels of parens in arithmetic */
899 char const *prevsyntax; /* syntax before arithmetic */
902 /* Avoid longjmp clobbering */
917 if (syntax == DQSYNTAX)
926 loop: { /* for each line, until end of word */
927 CHECKEND(); /* set c to PEOF if at end of here document */
928 for (;;) { /* until end of line or end of word */
929 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
931 synentry = syntax[c];
935 if (syntax == BASESYNTAX)
936 goto endword; /* exit outer loop */
944 goto loop; /* continue outer loop */
949 if (eofmark == NULL || dblquote)
950 USTPUTC(CTLESC, out);
953 case CBACK: /* backslash */
958 } else if (c == '\n') {
964 if (dblquote && c != '\\' &&
965 c != '`' && c != '$' &&
966 (c != '"' || eofmark != NULL))
968 if (SQSYNTAX[c] == CCTL)
969 USTPUTC(CTLESC, out);
970 else if (eofmark == NULL)
971 USTPUTC(CTLQUOTEMARK, out);
978 USTPUTC(CTLQUOTEMARK, out);
983 USTPUTC(CTLQUOTEMARK, out);
988 if (eofmark != NULL && arinest == 0 &&
995 } else if (eofmark == NULL) {
1002 case CVAR: /* '$' */
1003 PARSESUB(); /* parse substitution */
1005 case CENDVAR: /* '}' */
1008 USTPUTC(CTLENDVAR, out);
1013 case CLP: /* '(' in arithmetic */
1017 case CRP: /* ')' in arithmetic */
1018 if (parenlevel > 0) {
1022 if (pgetc() == ')') {
1023 if (--arinest == 0) {
1024 USTPUTC(CTLENDARI, out);
1025 syntax = prevsyntax;
1026 if (syntax == DQSYNTAX)
1035 * (don't 2nd guess - no error)
1042 case CBQUOTE: /* '`' */
1046 goto endword; /* exit outer loop */
1049 goto endword; /* exit outer loop */
1056 if (syntax == ARISYNTAX)
1057 synerror("Missing '))'");
1058 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1059 synerror("Unterminated quoted string");
1061 startlinno = plinno;
1062 synerror("Missing '}'");
1065 len = out - stackblock();
1067 if (eofmark == NULL) {
1068 if ((c == '>' || c == '<')
1071 && (*out == '\0' || is_digit(*out))) {
1073 return lasttoken = TREDIR;
1079 backquotelist = bqlist;
1080 grabstackblock(len);
1082 return lasttoken = TWORD;
1083 /* end of readtoken routine */
1088 * Check to see whether we are at the end of the here document. When this
1089 * is called, c is set to the first character of the next input line. If
1090 * we are at the end of the here document, this routine sets the c to PEOF.
1099 if (c == *eofmark) {
1100 if (pfgets(line, sizeof line) != NULL) {
1104 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1105 if (*p == '\n' && *q == '\0') {
1108 needprompt = doprompt;
1110 pushstring(line, strlen(line), NULL);
1115 goto checkend_return;
1120 * Parse a redirection operator. The variable "out" points to a string
1121 * specifying the fd to be redirected. The variable "c" contains the
1122 * first character of the redirection operator.
1129 np = (union node *)stalloc(sizeof (struct nfile));
1138 np->type = NCLOBBER;
1143 } else { /* c == '<' */
1147 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1148 np = (union node *)stalloc(sizeof (struct nhere));
1152 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1154 if ((c = pgetc()) == '-') {
1155 heredoc->striptabs = 1;
1157 heredoc->striptabs = 0;
1160 } else if (c == '&')
1170 np->nfile.fd = digit_val(fd);
1172 goto parseredir_return;
1177 * Parse a substitution. At this point, we have read the dollar sign
1187 static const char types[] = "}-+?=";
1189 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1192 if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1195 } else if (c == '(') { /* $(command) or $((arith)) */
1196 if (pgetc() == '(') {
1203 USTPUTC(CTLVAR, out);
1204 typeloc = out - stackblock();
1205 USTPUTC(VSNORMAL, out);
1211 if ((c = pgetc()) == '}')
1223 } while (is_in_name(c));
1224 } else if (is_digit(c)) {
1225 if (bracketed_name) {
1229 } while (is_digit(c));
1235 if (! is_special(c))
1236 badsub: synerror("Bad substitution");
1249 p = strchr(types, c);
1252 subtype = p - types + VSNORMAL;
1258 subtype = c == '#' ? VSTRIMLEFT :
1271 if (subtype != VSLENGTH && (dblquote || arinest))
1273 *(stackblock() + typeloc) = subtype | flags;
1274 if (subtype != VSNORMAL)
1277 goto parsesub_return;
1282 * Called to parse command substitutions. Newstyle is set if the command
1283 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1284 * list of commands (passed by reference), and savelen is the number of
1285 * characters on the top of the stack which must be preserved.
1289 struct nodelist **nlpp;
1293 struct jmploc jmploc;
1294 struct jmploc *volatile savehandler;
1298 /* Avoid longjmp clobbering */
1302 savepbq = parsebackquote;
1303 if (setjmp(jmploc.loc)) {
1307 handler = savehandler;
1308 longjmp(handler->loc, 1);
1312 savelen = out - stackblock();
1314 str = ckmalloc(savelen);
1315 memcpy(str, stackblock(), savelen);
1317 savehandler = handler;
1321 /* We must read until the closing backquote, giving special
1322 treatment to some slashes, and then push the string and
1323 reread it as input, interpreting it normally. */
1336 switch (c = pgetc()) {
1341 if ((c = pgetc()) == '\n') {
1348 * If eating a newline, avoid putting
1349 * the newline into the new character
1350 * stream (via the STPUTC after the
1355 if (c != '\\' && c != '`' && c != '$'
1356 && (!dblquote || c != '"'))
1362 needprompt = doprompt;
1366 startlinno = plinno;
1367 synerror("EOF in backquote substitution");
1377 savelen = out - stackblock();
1379 str = ckmalloc(savelen);
1380 memcpy(str, stackblock(), savelen);
1381 setinputstring(str, 1);
1386 nlpp = &(*nlpp)->next;
1387 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1388 (*nlpp)->next = NULL;
1389 parsebackquote = oldstyle;
1392 saveprompt = doprompt;
1399 doprompt = saveprompt;
1401 if (readtoken() != TRP)
1408 * Start reading from old file again, ignoring any pushed back
1409 * tokens left from the backquote parsing
1414 while (stackblocksize() <= savelen)
1418 memcpy(out, str, savelen);
1419 STADJUST(savelen, out);
1425 parsebackquote = savepbq;
1426 handler = savehandler;
1427 if (arinest || dblquote)
1428 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1430 USTPUTC(CTLBACKQ, out);
1432 goto parsebackq_oldreturn;
1434 goto parsebackq_newreturn;
1438 * Parse an arithmetic expansion (indicate start of one and set state)
1442 if (++arinest == 1) {
1443 prevsyntax = syntax;
1445 USTPUTC(CTLARI, out);
1452 * we collapse embedded arithmetic expansion to
1453 * parenthesis, which should be equivalent
1457 goto parsearith_return;
1460 } /* end of readtoken */
1472 * Returns true if the text contains nothing to expand (no dollar signs
1477 noexpand(char *text)
1483 while ((c = *p++) != '\0') {
1484 if ( c == CTLQUOTEMARK)
1488 else if (BASESYNTAX[(int)c] == CCTL)
1496 * Return true if the argument is a legal variable name (a letter or
1497 * underscore followed by zero or more letters, underscores, and digits).
1501 goodname(char *name)
1509 if (! is_in_name(*p))
1517 * Called when an unexpected token is read during the parse. The argument
1518 * is the token that is expected, or -1 if more than one type of token can
1519 * occur at this point.
1523 synexpect(int token)
1528 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1529 tokname[lasttoken], tokname[token]);
1531 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1538 synerror(const char *msg)
1541 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1542 outfmt(&errout, "Syntax error: %s\n", msg);
1543 error((char *)NULL);
1547 setprompt(int which)
1549 whichprompt = which;
1554 out2str(getprompt(NULL));
1558 * called by editline -- any expansions to the prompt
1559 * should be added here.
1562 getprompt(void *unused __unused)
1564 switch (whichprompt) {
1572 return "<internal prompt error>";