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.58 2006/11/05 18:36:05 stefanf Exp $
38 * $DragonFly: src/bin/sh/parser.c,v 1.11 2007/01/14 17:29:58 pavalos Exp $
47 #include "expand.h" /* defines rmescapes() */
60 #include "myhistedit.h"
64 * Shell command parser.
70 /* values returned by readtoken */
76 struct heredoc *next; /* next here document in list */
77 union node *here; /* redirection node */
78 char *eofmark; /* string indicating end of input */
79 int striptabs; /* if set, strip leading tabs */
84 STATIC struct heredoc *heredoclist; /* list of here documents to read */
85 STATIC int parsebackquote; /* nonzero if we are inside backquotes */
86 STATIC int doprompt; /* if set, prompt the user */
87 STATIC int needprompt; /* true if interactive and at start of line */
88 STATIC int lasttoken; /* last token read */
89 MKINIT int tokpushback; /* last token pushed back */
90 STATIC char *wordtext; /* text of last word returned by readtoken */
91 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
92 STATIC struct nodelist *backquotelist;
93 STATIC union node *redirnode;
94 STATIC struct heredoc *heredoc;
95 STATIC int quoteflag; /* set if (part of) last token was quoted */
96 STATIC int startlinno; /* line # where last token started */
98 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
99 static int noaliases = 0;
102 STATIC union node *list(int);
103 STATIC union node *andor(void);
104 STATIC union node *pipeline(void);
105 STATIC union node *command(void);
106 STATIC union node *simplecmd(union node **, union node *);
107 STATIC union node *makename(void);
108 STATIC void parsefname(void);
109 STATIC void parseheredoc(void);
110 STATIC int peektoken(void);
111 STATIC int readtoken(void);
112 STATIC int xxreadtoken(void);
113 STATIC int readtoken1(int, char const *, char *, int);
114 STATIC int noexpand(char *);
115 STATIC void synexpect(int);
116 STATIC void synerror(const char *);
117 STATIC void setprompt(int);
121 * Read and parse a command. Returns NEOF on end of file. (NULL is a
122 * valid parse tree indicating a blank line.)
126 parsecmd(int interact)
150 union node *n1, *n2, *n3;
154 if (nlflag == 0 && tokendlist[peektoken()])
160 if (tok == TBACKGND) {
161 if (n2->type == NCMD || n2->type == NPIPE) {
162 n2->ncmd.backgnd = 1;
163 } else if (n2->type == NREDIR) {
166 n3 = (union node *)stalloc(sizeof (struct nredir));
169 n3->nredir.redirect = NULL;
177 n3 = (union node *)stalloc(sizeof (struct nbinary));
179 n3->nbinary.ch1 = n1;
180 n3->nbinary.ch2 = n2;
197 if (tokendlist[peektoken()])
204 pungetc(); /* push back EOF on input */
220 union node *n1, *n2, *n3;
225 if ((t = readtoken()) == TAND) {
227 } else if (t == TOR) {
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 while (readtoken() == TNOT)
257 if (readtoken() == TPIPE) {
258 pipenode = (union node *)stalloc(sizeof (struct npipe));
259 pipenode->type = NPIPE;
260 pipenode->npipe.backgnd = 0;
261 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
262 pipenode->npipe.cmdlist = lp;
266 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
269 } while (readtoken() == TPIPE);
275 n2 = (union node *)stalloc(sizeof (struct nnot));
289 union node *ap, **app;
290 union node *cp, **cpp;
291 union node *redir, **rpp;
299 /* Check for redirection which may precede command */
300 while (readtoken() == TREDIR) {
301 *rpp = n2 = redirnode;
302 rpp = &n2->nfile.next;
307 while (readtoken() == TNOT) {
308 TRACE(("command: TNOT recognized\n"));
313 switch (readtoken()) {
315 n1 = (union node *)stalloc(sizeof (struct nif));
317 if ((n1->nif.test = list(0)) == NULL)
319 if (readtoken() != TTHEN)
321 n1->nif.ifpart = list(0);
323 while (readtoken() == TELIF) {
324 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
325 n2 = n2->nif.elsepart;
327 if ((n2->nif.test = list(0)) == NULL)
329 if (readtoken() != TTHEN)
331 n2->nif.ifpart = list(0);
333 if (lasttoken == TELSE)
334 n2->nif.elsepart = list(0);
336 n2->nif.elsepart = NULL;
339 if (readtoken() != TFI)
346 n1 = (union node *)stalloc(sizeof (struct nbinary));
347 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
348 if ((n1->nbinary.ch1 = list(0)) == NULL)
350 if ((got=readtoken()) != TDO) {
351 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
354 n1->nbinary.ch2 = list(0);
355 if (readtoken() != TDONE)
361 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
362 synerror("Bad for loop variable");
363 n1 = (union node *)stalloc(sizeof (struct nfor));
365 n1->nfor.var = wordtext;
366 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
368 while (readtoken() == TWORD) {
369 n2 = (union node *)stalloc(sizeof (struct narg));
371 n2->narg.text = wordtext;
372 n2->narg.backquote = backquotelist;
374 app = &n2->narg.next;
378 if (lasttoken != TNL && lasttoken != TSEMI)
381 static char argvars[5] = {
382 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
384 n2 = (union node *)stalloc(sizeof (struct narg));
386 n2->narg.text = argvars;
387 n2->narg.backquote = NULL;
388 n2->narg.next = NULL;
391 * Newline or semicolon here is optional (but note
392 * that the original Bourne shell only allowed NL).
394 if (lasttoken != TNL && lasttoken != TSEMI)
398 if ((t = readtoken()) == TDO)
400 else if (t == TBEGIN)
404 n1->nfor.body = list(0);
405 if (readtoken() != t)
410 n1 = (union node *)stalloc(sizeof (struct ncase));
412 if (readtoken() != TWORD)
414 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
416 n2->narg.text = wordtext;
417 n2->narg.backquote = backquotelist;
418 n2->narg.next = NULL;
419 while (readtoken() == TNL);
420 if (lasttoken != TWORD || ! equal(wordtext, "in"))
421 synerror("expecting \"in\"");
422 cpp = &n1->ncase.cases;
423 noaliases = 1; /* turn off alias expansion */
424 checkkwd = 2, readtoken();
425 while (lasttoken != TESAC) {
426 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
428 app = &cp->nclist.pattern;
429 if (lasttoken == TLP)
432 *app = ap = (union node *)stalloc(sizeof (struct narg));
434 ap->narg.text = wordtext;
435 ap->narg.backquote = backquotelist;
436 if (checkkwd = 2, readtoken() != TPIPE)
438 app = &ap->narg.next;
441 ap->narg.next = NULL;
442 if (lasttoken != TRP)
443 noaliases = 0, synexpect(TRP);
444 cp->nclist.body = list(0);
447 if ((t = readtoken()) != TESAC) {
449 noaliases = 0, synexpect(TENDCASE);
451 checkkwd = 2, readtoken();
453 cpp = &cp->nclist.next;
455 noaliases = 0; /* reset alias expansion */
460 n1 = (union node *)stalloc(sizeof (struct nredir));
461 n1->type = NSUBSHELL;
462 n1->nredir.n = list(0);
463 n1->nredir.redirect = NULL;
464 if (readtoken() != TRP)
470 if (readtoken() != TEND)
474 /* Handle an empty command like other simple commands. */
479 * An empty command before a ; doesn't make much sense, and
480 * should certainly be disallowed in the case of `if ;'.
489 n1 = simplecmd(rpp, redir);
495 /* Now check for redirection which may follow command */
496 while (readtoken() == TREDIR) {
497 *rpp = n2 = redirnode;
498 rpp = &n2->nfile.next;
504 if (n1->type != NSUBSHELL) {
505 n2 = (union node *)stalloc(sizeof (struct nredir));
510 n1->nredir.redirect = redir;
515 n2 = (union node *)stalloc(sizeof (struct nnot));
526 simplecmd(union node **rpp, union node *redir)
528 union node *args, **app;
529 union node **orig_rpp = rpp;
530 union node *n = NULL, *n2;
533 /* If we don't have any redirections already, then we must reset */
534 /* rpp to be the address of the local redir variable. */
541 * We save the incoming value, because we need this for shell
542 * functions. There can not be a redirect or an argument between
543 * the function name and the open parenthesis.
547 while (readtoken() == TNOT) {
548 TRACE(("command: TNOT recognized\n"));
554 if (readtoken() == TWORD) {
555 n = (union node *)stalloc(sizeof (struct narg));
557 n->narg.text = wordtext;
558 n->narg.backquote = backquotelist;
561 } else if (lasttoken == TREDIR) {
562 *rpp = n = redirnode;
563 rpp = &n->nfile.next;
564 parsefname(); /* read name of redirection file */
565 } else if (lasttoken == TLP && app == &args->narg.next
566 && rpp == orig_rpp) {
567 /* We have a function */
568 if (readtoken() != TRP)
571 if (! goodname(n->narg.text))
572 synerror("Bad function name");
575 n->narg.next = command();
584 n = (union node *)stalloc(sizeof (struct ncmd));
588 n->ncmd.redirect = redir;
592 n2 = (union node *)stalloc(sizeof (struct nnot));
606 n = (union node *)stalloc(sizeof (struct narg));
609 n->narg.text = wordtext;
610 n->narg.backquote = backquotelist;
615 fixredir(union node *n, const char *text, int err)
617 TRACE(("Fix redir %s %d\n", text, err));
619 n->ndup.vname = NULL;
621 if (is_digit(text[0]) && text[1] == '\0')
622 n->ndup.dupfd = digit_val(text[0]);
623 else if (text[0] == '-' && text[1] == '\0')
628 synerror("Bad fd number");
630 n->ndup.vname = makename();
638 union node *n = redirnode;
640 if (readtoken() != TWORD)
642 if (n->type == NHERE) {
643 struct heredoc *here = heredoc;
649 TRACE(("Here document %d\n", n->type));
650 if (here->striptabs) {
651 while (*wordtext == '\t')
654 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
655 synerror("Illegal eof marker for << redirection");
657 here->eofmark = wordtext;
659 if (heredoclist == NULL)
662 for (p = heredoclist ; p->next ; p = p->next);
665 } else if (n->type == NTOFD || n->type == NFROMFD) {
666 fixredir(n, wordtext, 0);
668 n->nfile.fname = makename();
674 * Input any here documents.
680 struct heredoc *here;
683 while (heredoclist) {
685 heredoclist = here->next;
690 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
691 here->eofmark, here->striptabs);
692 n = (union node *)stalloc(sizeof (struct narg));
695 n->narg.text = wordtext;
696 n->narg.backquote = backquotelist;
697 here->here->nhere.doc = n;
715 int savecheckkwd = checkkwd;
718 int alreadyseen = tokpushback;
737 * check for keywords and aliases
739 if (t == TWORD && !quoteflag)
741 const char * const *pp;
743 for (pp = parsekwd; *pp; pp++) {
744 if (**pp == *wordtext && equal(*pp, wordtext))
746 lasttoken = t = pp - parsekwd + KWDOFFSET;
747 TRACE(("keyword %s recognized\n", tokname[t]));
751 if (noaliases == 0 &&
752 (ap = lookupalias(wordtext, 1)) != NULL) {
753 pushstring(ap->val, strlen(ap->val), ap);
754 checkkwd = savecheckkwd;
759 checkkwd = (t == TNOT) ? savecheckkwd : 0;
763 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
765 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
772 * Read the next input token.
773 * If the token is a word, we set backquotelist to the list of cmds in
774 * backquotes. We set quoteflag to true if any part of the word was
776 * If the token is TREDIR, then we set redirnode to a structure containing
778 * In all cases, the variable startlinno is set to the number of the line
779 * on which the token starts.
781 * [Change comment: here documents and internal procedures]
782 * [Readtoken shouldn't have any arguments. Perhaps we should make the
783 * word parsing code into a separate routine. In this case, readtoken
784 * doesn't need to have any internal procedures, but parseword does.
785 * We could also make parseoperator in essence the main routine, and
786 * have parseword (readtoken1?) handle both words and redirection.]
789 #define RETURN(token) return lasttoken = token
805 for (;;) { /* until token or start of word found */
807 if (c == ' ' || c == '\t')
808 continue; /* quick check for white space first */
813 while ((c = pgetc()) != '\n' && c != PEOF);
817 if (pgetc() == '\n') {
818 startlinno = ++plinno;
829 needprompt = doprompt;
857 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
864 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
865 * is not NULL, read a here document. In the latter case, eofmark is the
866 * word which marks the end of the document and striptabs is true if
867 * leading tabs should be stripped from the document. The argument firstc
868 * is the first character of the input token or document.
870 * Because C does not have internal subroutines, I have simulated them
871 * using goto's to implement the subroutine linkage. The following macros
872 * will run code that appears at the end of readtoken1.
875 #define CHECKEND() {goto checkend; checkend_return:;}
876 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
877 #define PARSESUB() {goto parsesub; parsesub_return:;}
878 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
879 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
880 #define PARSEARITH() {goto parsearith; parsearith_return:;}
883 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
886 char const * volatile syntax = syn;
889 char line[EOFMARKLEN + 1];
890 struct nodelist *bqlist;
892 volatile int dblquote;
893 volatile int varnest; /* levels of variables expansion */
894 volatile int arinest; /* levels of arithmetic expansion */
895 volatile int parenlevel; /* levels of parens in arithmetic */
896 volatile int oldstyle;
897 char const * volatile prevsyntax = NULL; /* syntax before arithmetic */
902 if (syntax == DQSYNTAX)
911 loop: { /* for each line, until end of word */
912 CHECKEND(); /* set c to PEOF if at end of here document */
913 for (;;) { /* until end of line or end of word */
914 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
916 synentry = syntax[c];
920 if (syntax == BASESYNTAX)
921 goto endword; /* exit outer loop */
929 goto loop; /* continue outer loop */
934 if (eofmark == NULL || dblquote)
935 USTPUTC(CTLESC, out);
938 case CBACK: /* backslash */
943 } else if (c == '\n') {
950 if (dblquote && c != '\\' &&
951 c != '`' && c != '$' &&
952 (c != '"' || eofmark != NULL))
954 if (SQSYNTAX[c] == CCTL)
955 USTPUTC(CTLESC, out);
956 else if (eofmark == NULL)
957 USTPUTC(CTLQUOTEMARK, out);
964 USTPUTC(CTLQUOTEMARK, out);
969 USTPUTC(CTLQUOTEMARK, out);
974 if (eofmark != NULL && arinest == 0 &&
981 } else if (eofmark == NULL) {
989 PARSESUB(); /* parse substitution */
991 case CENDVAR: /* '}' */
994 USTPUTC(CTLENDVAR, out);
999 case CLP: /* '(' in arithmetic */
1003 case CRP: /* ')' in arithmetic */
1004 if (parenlevel > 0) {
1008 if (pgetc() == ')') {
1009 if (--arinest == 0) {
1010 USTPUTC(CTLENDARI, out);
1011 syntax = prevsyntax;
1012 if (syntax == DQSYNTAX)
1021 * (don't 2nd guess - no error)
1028 case CBQUOTE: /* '`' */
1032 goto endword; /* exit outer loop */
1035 goto endword; /* exit outer loop */
1042 if (syntax == ARISYNTAX)
1043 synerror("Missing '))'");
1044 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1045 synerror("Unterminated quoted string");
1047 startlinno = plinno;
1048 synerror("Missing '}'");
1051 len = out - stackblock();
1053 if (eofmark == NULL) {
1054 if ((c == '>' || c == '<')
1057 && (*out == '\0' || is_digit(*out))) {
1059 return lasttoken = TREDIR;
1065 backquotelist = bqlist;
1066 grabstackblock(len);
1068 return lasttoken = TWORD;
1069 /* end of readtoken routine */
1074 * Check to see whether we are at the end of the here document. When this
1075 * is called, c is set to the first character of the next input line. If
1076 * we are at the end of the here document, this routine sets the c to PEOF.
1085 if (c == *eofmark) {
1086 if (pfgets(line, sizeof line) != NULL) {
1090 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1091 if (*p == '\n' && *q == '\0') {
1094 needprompt = doprompt;
1096 pushstring(line, strlen(line), NULL);
1101 goto checkend_return;
1106 * Parse a redirection operator. The variable "out" points to a string
1107 * specifying the fd to be redirected. The variable "c" contains the
1108 * first character of the redirection operator.
1115 np = (union node *)stalloc(sizeof (struct nfile));
1124 np->type = NCLOBBER;
1129 } else { /* c == '<' */
1133 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1134 np = (union node *)stalloc(sizeof (struct nhere));
1138 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1140 if ((c = pgetc()) == '-') {
1141 heredoc->striptabs = 1;
1143 heredoc->striptabs = 0;
1146 } else if (c == '&')
1156 np->nfile.fd = digit_val(fd);
1158 goto parseredir_return;
1163 * Parse a substitution. At this point, we have read the dollar sign
1172 static const char types[] = "}-+?=";
1173 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1176 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1180 } else if (c == '(') { /* $(command) or $((arith)) */
1181 if (pgetc() == '(') {
1188 USTPUTC(CTLVAR, out);
1189 typeloc = out - stackblock();
1190 USTPUTC(VSNORMAL, out);
1196 if ((c = pgetc()) == '}')
1204 if (!is_eof(c) && is_name(c)) {
1208 } while (!is_eof(c) && is_in_name(c));
1209 } else if (is_digit(c)) {
1210 if (bracketed_name) {
1214 } while (is_digit(c));
1220 if (! is_special(c)) {
1239 p = strchr(types, c);
1246 subtype = p - types + VSNORMAL;
1252 subtype = c == '#' ? VSTRIMLEFT :
1262 } else if (subtype != VSERROR) {
1266 if (subtype != VSLENGTH && (dblquote || arinest))
1268 *(stackblock() + typeloc) = subtype | flags;
1269 if (subtype != VSNORMAL)
1272 goto parsesub_return;
1277 * Called to parse command substitutions. Newstyle is set if the command
1278 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1279 * list of commands (passed by reference), and savelen is the number of
1280 * characters on the top of the stack which must be preserved.
1284 struct nodelist **nlpp;
1288 struct jmploc jmploc;
1289 struct jmploc *volatile savehandler;
1291 volatile int saveprompt;
1293 savepbq = parsebackquote;
1294 if (setjmp(jmploc.loc)) {
1298 handler = savehandler;
1299 longjmp(handler->loc, 1);
1303 savelen = out - stackblock();
1305 str = ckmalloc(savelen);
1306 memcpy(str, stackblock(), savelen);
1308 savehandler = handler;
1312 /* We must read until the closing backquote, giving special
1313 treatment to some slashes, and then push the string and
1314 reread it as input, interpreting it normally. */
1321 STARTSTACKSTR(pout);
1327 switch (pc = pgetc()) {
1332 if ((pc = pgetc()) == '\n') {
1339 * If eating a newline, avoid putting
1340 * the newline into the new character
1341 * stream (via the STPUTC after the
1346 if (pc != '\\' && pc != '`' && pc != '$'
1347 && (!dblquote || pc != '"'))
1353 needprompt = doprompt;
1357 startlinno = plinno;
1358 synerror("EOF in backquote substitution");
1368 psavelen = pout - stackblock();
1370 pstr = ckmalloc(psavelen);
1371 memcpy(pstr, stackblock(), psavelen);
1372 setinputstring(pstr, 1);
1377 nlpp = &(*nlpp)->next;
1378 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1379 (*nlpp)->next = NULL;
1380 parsebackquote = oldstyle;
1383 saveprompt = doprompt;
1390 doprompt = saveprompt;
1392 if (readtoken() != TRP)
1399 * Start reading from old file again, ignoring any pushed back
1400 * tokens left from the backquote parsing
1405 while (stackblocksize() <= savelen)
1409 memcpy(out, str, savelen);
1410 STADJUST(savelen, out);
1416 parsebackquote = savepbq;
1417 handler = savehandler;
1418 if (arinest || dblquote)
1419 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1421 USTPUTC(CTLBACKQ, out);
1423 goto parsebackq_oldreturn;
1425 goto parsebackq_newreturn;
1429 * Parse an arithmetic expansion (indicate start of one and set state)
1433 if (++arinest == 1) {
1434 prevsyntax = syntax;
1436 USTPUTC(CTLARI, out);
1443 * we collapse embedded arithmetic expansion to
1444 * parenthesis, which should be equivalent
1448 goto parsearith_return;
1451 } /* end of readtoken */
1463 * Returns true if the text contains nothing to expand (no dollar signs
1468 noexpand(char *text)
1474 while ((c = *p++) != '\0') {
1475 if ( c == CTLQUOTEMARK)
1479 else if (BASESYNTAX[(int)c] == CCTL)
1487 * Return true if the argument is a legal variable name (a letter or
1488 * underscore followed by zero or more letters, underscores, and digits).
1492 goodname(char *name)
1500 if (! is_in_name(*p))
1508 * Called when an unexpected token is read during the parse. The argument
1509 * is the token that is expected, or -1 if more than one type of token can
1510 * occur at this point.
1514 synexpect(int token)
1519 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1520 tokname[lasttoken], tokname[token]);
1522 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1529 synerror(const char *msg)
1532 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1533 outfmt(&errout, "Syntax error: %s\n", msg);
1534 error((char *)NULL);
1538 setprompt(int which)
1540 whichprompt = which;
1545 out2str(getprompt(NULL));
1549 * called by editline -- any expansions to the prompt
1550 * should be added here.
1553 getprompt(void *unused __unused)
1555 static char ps[PROMPTLEN];
1560 * Select prompt format.
1562 switch (whichprompt) {
1573 return "<internal prompt error>";
1577 * Format prompt string.
1579 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1586 * \h specifies just the local hostname,
1587 * \H specifies fully-qualified hostname.
1592 gethostname(&ps[i], PROMPTLEN - i);
1593 /* Skip to end of hostname. */
1594 trim = (*fmt == 'h') ? '.' : '\0';
1595 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1600 * Working directory.
1602 * \W specifies just the final component,
1603 * \w specifies the entire path.
1608 getcwd(&ps[i], PROMPTLEN - i);
1610 /* Final path component only. */
1612 for (j = i; ps[j] != '\0'; j++)
1615 memmove(&ps[i], &ps[trim],
1618 /* Skip to end of path. */
1619 while (ps[i + 1] != '\0')
1626 * '$' for normal users, '#' for root.
1629 ps[i] = (geteuid() != 0) ? '$' : '#';
1640 * Emit unrecognized formats verbatim.