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.10 2007/01/14 05:39:22 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 *syntax, char *eofmark, int striptabs)
888 char line[EOFMARKLEN + 1];
889 struct nodelist *bqlist;
892 int varnest; /* levels of variables expansion */
893 int arinest; /* levels of arithmetic expansion */
894 int parenlevel; /* levels of parens in arithmetic */
896 char const *prevsyntax; /* syntax before arithmetic */
899 /* Avoid longjmp clobbering */
914 if (syntax == DQSYNTAX)
923 loop: { /* for each line, until end of word */
924 CHECKEND(); /* set c to PEOF if at end of here document */
925 for (;;) { /* until end of line or end of word */
926 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
928 synentry = syntax[c];
932 if (syntax == BASESYNTAX)
933 goto endword; /* exit outer loop */
941 goto loop; /* continue outer loop */
946 if (eofmark == NULL || dblquote)
947 USTPUTC(CTLESC, out);
950 case CBACK: /* backslash */
955 } else if (c == '\n') {
962 if (dblquote && c != '\\' &&
963 c != '`' && c != '$' &&
964 (c != '"' || eofmark != NULL))
966 if (SQSYNTAX[c] == CCTL)
967 USTPUTC(CTLESC, out);
968 else if (eofmark == NULL)
969 USTPUTC(CTLQUOTEMARK, out);
976 USTPUTC(CTLQUOTEMARK, out);
981 USTPUTC(CTLQUOTEMARK, out);
986 if (eofmark != NULL && arinest == 0 &&
993 } else if (eofmark == NULL) {
1000 case CVAR: /* '$' */
1001 PARSESUB(); /* parse substitution */
1003 case CENDVAR: /* '}' */
1006 USTPUTC(CTLENDVAR, out);
1011 case CLP: /* '(' in arithmetic */
1015 case CRP: /* ')' in arithmetic */
1016 if (parenlevel > 0) {
1020 if (pgetc() == ')') {
1021 if (--arinest == 0) {
1022 USTPUTC(CTLENDARI, out);
1023 syntax = prevsyntax;
1024 if (syntax == DQSYNTAX)
1033 * (don't 2nd guess - no error)
1040 case CBQUOTE: /* '`' */
1044 goto endword; /* exit outer loop */
1047 goto endword; /* exit outer loop */
1054 if (syntax == ARISYNTAX)
1055 synerror("Missing '))'");
1056 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1057 synerror("Unterminated quoted string");
1059 startlinno = plinno;
1060 synerror("Missing '}'");
1063 len = out - stackblock();
1065 if (eofmark == NULL) {
1066 if ((c == '>' || c == '<')
1069 && (*out == '\0' || is_digit(*out))) {
1071 return lasttoken = TREDIR;
1077 backquotelist = bqlist;
1078 grabstackblock(len);
1080 return lasttoken = TWORD;
1081 /* end of readtoken routine */
1086 * Check to see whether we are at the end of the here document. When this
1087 * is called, c is set to the first character of the next input line. If
1088 * we are at the end of the here document, this routine sets the c to PEOF.
1097 if (c == *eofmark) {
1098 if (pfgets(line, sizeof line) != NULL) {
1102 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1103 if (*p == '\n' && *q == '\0') {
1106 needprompt = doprompt;
1108 pushstring(line, strlen(line), NULL);
1113 goto checkend_return;
1118 * Parse a redirection operator. The variable "out" points to a string
1119 * specifying the fd to be redirected. The variable "c" contains the
1120 * first character of the redirection operator.
1127 np = (union node *)stalloc(sizeof (struct nfile));
1136 np->type = NCLOBBER;
1141 } else { /* c == '<' */
1145 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1146 np = (union node *)stalloc(sizeof (struct nhere));
1150 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1152 if ((c = pgetc()) == '-') {
1153 heredoc->striptabs = 1;
1155 heredoc->striptabs = 0;
1158 } else if (c == '&')
1168 np->nfile.fd = digit_val(fd);
1170 goto parseredir_return;
1175 * Parse a substitution. At this point, we have read the dollar sign
1184 static const char types[] = "}-+?=";
1185 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1188 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1192 } else if (c == '(') { /* $(command) or $((arith)) */
1193 if (pgetc() == '(') {
1200 USTPUTC(CTLVAR, out);
1201 typeloc = out - stackblock();
1202 USTPUTC(VSNORMAL, out);
1208 if ((c = pgetc()) == '}')
1216 if (!is_eof(c) && is_name(c)) {
1220 } while (!is_eof(c) && is_in_name(c));
1221 } else if (is_digit(c)) {
1222 if (bracketed_name) {
1226 } while (is_digit(c));
1232 if (! is_special(c)) {
1251 p = strchr(types, c);
1258 subtype = p - types + VSNORMAL;
1264 subtype = c == '#' ? VSTRIMLEFT :
1274 } else if (subtype != VSERROR) {
1278 if (subtype != VSLENGTH && (dblquote || arinest))
1280 *(stackblock() + typeloc) = subtype | flags;
1281 if (subtype != VSNORMAL)
1284 goto parsesub_return;
1289 * Called to parse command substitutions. Newstyle is set if the command
1290 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1291 * list of commands (passed by reference), and savelen is the number of
1292 * characters on the top of the stack which must be preserved.
1296 struct nodelist **nlpp;
1300 struct jmploc jmploc;
1301 struct jmploc *volatile savehandler;
1305 /* Avoid longjmp clobbering */
1309 savepbq = parsebackquote;
1310 if (setjmp(jmploc.loc)) {
1314 handler = savehandler;
1315 longjmp(handler->loc, 1);
1319 savelen = out - stackblock();
1321 str = ckmalloc(savelen);
1322 memcpy(str, stackblock(), savelen);
1324 savehandler = handler;
1328 /* We must read until the closing backquote, giving special
1329 treatment to some slashes, and then push the string and
1330 reread it as input, interpreting it normally. */
1337 STARTSTACKSTR(pout);
1343 switch (pc = pgetc()) {
1348 if ((pc = pgetc()) == '\n') {
1355 * If eating a newline, avoid putting
1356 * the newline into the new character
1357 * stream (via the STPUTC after the
1362 if (pc != '\\' && pc != '`' && pc != '$'
1363 && (!dblquote || pc != '"'))
1369 needprompt = doprompt;
1373 startlinno = plinno;
1374 synerror("EOF in backquote substitution");
1384 psavelen = pout - stackblock();
1386 pstr = ckmalloc(psavelen);
1387 memcpy(pstr, stackblock(), psavelen);
1388 setinputstring(pstr, 1);
1393 nlpp = &(*nlpp)->next;
1394 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1395 (*nlpp)->next = NULL;
1396 parsebackquote = oldstyle;
1399 saveprompt = doprompt;
1406 doprompt = saveprompt;
1408 if (readtoken() != TRP)
1415 * Start reading from old file again, ignoring any pushed back
1416 * tokens left from the backquote parsing
1421 while (stackblocksize() <= savelen)
1425 memcpy(out, str, savelen);
1426 STADJUST(savelen, out);
1432 parsebackquote = savepbq;
1433 handler = savehandler;
1434 if (arinest || dblquote)
1435 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1437 USTPUTC(CTLBACKQ, out);
1439 goto parsebackq_oldreturn;
1441 goto parsebackq_newreturn;
1445 * Parse an arithmetic expansion (indicate start of one and set state)
1449 if (++arinest == 1) {
1450 prevsyntax = syntax;
1452 USTPUTC(CTLARI, out);
1459 * we collapse embedded arithmetic expansion to
1460 * parenthesis, which should be equivalent
1464 goto parsearith_return;
1467 } /* end of readtoken */
1479 * Returns true if the text contains nothing to expand (no dollar signs
1484 noexpand(char *text)
1490 while ((c = *p++) != '\0') {
1491 if ( c == CTLQUOTEMARK)
1495 else if (BASESYNTAX[(int)c] == CCTL)
1503 * Return true if the argument is a legal variable name (a letter or
1504 * underscore followed by zero or more letters, underscores, and digits).
1508 goodname(char *name)
1516 if (! is_in_name(*p))
1524 * Called when an unexpected token is read during the parse. The argument
1525 * is the token that is expected, or -1 if more than one type of token can
1526 * occur at this point.
1530 synexpect(int token)
1535 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1536 tokname[lasttoken], tokname[token]);
1538 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1545 synerror(const char *msg)
1548 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1549 outfmt(&errout, "Syntax error: %s\n", msg);
1550 error((char *)NULL);
1554 setprompt(int which)
1556 whichprompt = which;
1561 out2str(getprompt(NULL));
1565 * called by editline -- any expansions to the prompt
1566 * should be added here.
1569 getprompt(void *unused __unused)
1571 static char ps[PROMPTLEN];
1576 * Select prompt format.
1578 switch (whichprompt) {
1589 return "<internal prompt error>";
1593 * Format prompt string.
1595 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1602 * \h specifies just the local hostname,
1603 * \H specifies fully-qualified hostname.
1608 gethostname(&ps[i], PROMPTLEN - i);
1609 /* Skip to end of hostname. */
1610 trim = (*fmt == 'h') ? '.' : '\0';
1611 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1616 * Working directory.
1618 * \W specifies just the final component,
1619 * \w specifies the entire path.
1624 getcwd(&ps[i], PROMPTLEN - i);
1626 /* Final path component only. */
1628 for (j = i; ps[j] != '\0'; j++)
1631 memmove(&ps[i], &ps[trim],
1634 /* Skip to end of path. */
1635 while (ps[i + 1] != '\0')
1642 * '$' for normal users, '#' for root.
1645 ps[i] = (geteuid() != 0) ? '$' : '#';
1656 * Emit unrecognized formats verbatim.