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.12 2007/01/18 17:03:18 corecode 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 */
93 * 2 == also eat newlines
97 STATIC struct nodelist *backquotelist;
98 STATIC union node *redirnode;
99 STATIC struct heredoc *heredoc;
100 STATIC int quoteflag; /* set if (part of) last token was quoted */
101 STATIC int startlinno; /* line # where last token started */
102 STATIC int funclinno; /* line # where the current function started */
104 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
105 static int noaliases = 0;
108 STATIC union node *list(int);
109 STATIC union node *andor(void);
110 STATIC union node *pipeline(void);
111 STATIC union node *command(void);
112 STATIC union node *simplecmd(union node **, union node *);
113 STATIC union node *makename(void);
114 STATIC void parsefname(void);
115 STATIC void parseheredoc(void);
116 STATIC int peektoken(void);
117 STATIC int readtoken(void);
118 STATIC int xxreadtoken(void);
119 STATIC int readtoken1(int, char const *, char *, int);
120 STATIC int noexpand(char *);
121 STATIC void synexpect(int);
122 STATIC void synerror(const char *);
123 STATIC void setprompt(int);
127 * Read and parse a command. Returns NEOF on end of file. (NULL is a
128 * valid parse tree indicating a blank line.)
132 parsecmd(int interact)
156 union node *n1, *n2, *n3;
160 if (nlflag == 0 && tokendlist[peektoken()])
166 if (tok == TBACKGND) {
167 if (n2->type == NCMD || n2->type == NPIPE) {
168 n2->ncmd.backgnd = 1;
169 } else if (n2->type == NREDIR) {
172 n3 = (union node *)stalloc(sizeof (struct nredir));
175 n3->nredir.redirect = NULL;
183 n3 = (union node *)stalloc(sizeof (struct nbinary));
185 n3->nbinary.ch1 = n1;
186 n3->nbinary.ch2 = n2;
203 if (tokendlist[peektoken()])
210 pungetc(); /* push back EOF on input */
226 union node *n1, *n2, *n3;
231 if ((t = readtoken()) == TAND) {
233 } else if (t == TOR) {
240 n3 = (union node *)stalloc(sizeof (struct nbinary));
242 n3->nbinary.ch1 = n1;
243 n3->nbinary.ch2 = n2;
253 union node *n1, *n2, *pipenode;
254 struct nodelist *lp, *prev;
258 TRACE(("pipeline: entered\n"));
261 while (readtoken() == TNOT)
266 if (readtoken() == TPIPE) {
267 pipenode = (union node *)stalloc(sizeof (struct npipe));
268 pipenode->type = NPIPE;
269 pipenode->npipe.backgnd = 0;
270 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
271 pipenode->npipe.cmdlist = lp;
277 while (readtoken() == TNOT)
278 innernegate = !innernegate;
282 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
287 n2 = (union node *)stalloc(sizeof (struct nnot));
289 n2->nnot.com = lp->n;
292 } while (readtoken() == TPIPE);
298 n2 = (union node *)stalloc(sizeof (struct nnot));
312 union node *ap, **app;
313 union node *cp, **cpp;
314 union node *redir, **rpp;
322 /* Check for redirection which may precede command */
323 while (readtoken() == TREDIR) {
324 *rpp = n2 = redirnode;
325 rpp = &n2->nfile.next;
330 switch (readtoken()) {
332 n1 = (union node *)stalloc(sizeof (struct nif));
334 if ((n1->nif.test = list(0)) == NULL)
336 if (readtoken() != TTHEN)
338 n1->nif.ifpart = list(0);
340 while (readtoken() == TELIF) {
341 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
342 n2 = n2->nif.elsepart;
344 if ((n2->nif.test = list(0)) == NULL)
346 if (readtoken() != TTHEN)
348 n2->nif.ifpart = list(0);
350 if (lasttoken == TELSE)
351 n2->nif.elsepart = list(0);
353 n2->nif.elsepart = NULL;
356 if (readtoken() != TFI)
363 n1 = (union node *)stalloc(sizeof (struct nbinary));
364 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
365 if ((n1->nbinary.ch1 = list(0)) == NULL)
367 if ((got=readtoken()) != TDO) {
368 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
371 n1->nbinary.ch2 = list(0);
372 if (readtoken() != TDONE)
378 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
379 synerror("Bad for loop variable");
380 n1 = (union node *)stalloc(sizeof (struct nfor));
382 n1->nfor.var = wordtext;
383 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
385 while (readtoken() == TWORD) {
386 n2 = (union node *)stalloc(sizeof (struct narg));
388 n2->narg.text = wordtext;
389 n2->narg.backquote = backquotelist;
391 app = &n2->narg.next;
395 if (lasttoken != TNL && lasttoken != TSEMI)
398 static char argvars[5] = {
399 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
401 n2 = (union node *)stalloc(sizeof (struct narg));
403 n2->narg.text = argvars;
404 n2->narg.backquote = NULL;
405 n2->narg.next = NULL;
408 * Newline or semicolon here is optional (but note
409 * that the original Bourne shell only allowed NL).
411 if (lasttoken != TNL && lasttoken != TSEMI)
415 if ((t = readtoken()) == TDO)
417 else if (t == TBEGIN)
421 n1->nfor.body = list(0);
422 if (readtoken() != t)
427 n1 = (union node *)stalloc(sizeof (struct ncase));
429 if (readtoken() != TWORD)
431 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
433 n2->narg.text = wordtext;
434 n2->narg.backquote = backquotelist;
435 n2->narg.next = NULL;
436 while (readtoken() == TNL);
437 if (lasttoken != TWORD || ! equal(wordtext, "in"))
438 synerror("expecting \"in\"");
439 cpp = &n1->ncase.cases;
440 noaliases = 1; /* turn off alias expansion */
441 checkkwd = 2, readtoken();
442 while (lasttoken != TESAC) {
443 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
445 app = &cp->nclist.pattern;
446 if (lasttoken == TLP)
449 *app = ap = (union node *)stalloc(sizeof (struct narg));
451 ap->narg.text = wordtext;
452 ap->narg.backquote = backquotelist;
453 if (checkkwd = 2, readtoken() != TPIPE)
455 app = &ap->narg.next;
458 ap->narg.next = NULL;
459 if (lasttoken != TRP)
460 noaliases = 0, synexpect(TRP);
461 cp->nclist.body = list(0);
464 if ((t = readtoken()) != TESAC) {
466 noaliases = 0, synexpect(TENDCASE);
468 checkkwd = 2, readtoken();
470 cpp = &cp->nclist.next;
472 noaliases = 0; /* reset alias expansion */
477 n1 = (union node *)stalloc(sizeof (struct nredir));
478 n1->type = NSUBSHELL;
479 n1->nredir.n = list(0);
480 n1->nredir.redirect = NULL;
481 if (readtoken() != TRP)
487 if (readtoken() != TEND)
491 /* Handle an empty command like other simple commands. */
496 * An empty command before a ; doesn't make much sense, and
497 * should certainly be disallowed in the case of `if ;'.
506 return simplecmd(rpp, redir);
511 /* Now check for redirection which may follow command */
512 while (readtoken() == TREDIR) {
513 *rpp = n2 = redirnode;
514 rpp = &n2->nfile.next;
520 if (n1->type != NSUBSHELL) {
521 n2 = (union node *)stalloc(sizeof (struct nredir));
526 n1->nredir.redirect = redir;
533 simplecmd(union node **rpp, union node *redir)
535 union node *args, **app;
536 union node **orig_rpp = rpp;
537 union node *n = NULL;
539 /* If we don't have any redirections already, then we must reset */
540 /* rpp to be the address of the local redir variable. */
547 * We save the incoming value, because we need this for shell
548 * functions. There can not be a redirect or an argument between
549 * the function name and the open parenthesis.
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)
572 if (! goodname(n->narg.text))
573 synerror("Bad function name");
576 n->narg.next = command();
586 n = (union node *)stalloc(sizeof (struct ncmd));
590 n->ncmd.redirect = redir;
599 n = (union node *)stalloc(sizeof (struct narg));
602 n->narg.text = wordtext;
603 n->narg.backquote = backquotelist;
608 fixredir(union node *n, const char *text, int err)
610 TRACE(("Fix redir %s %d\n", text, err));
612 n->ndup.vname = NULL;
614 if (is_digit(text[0]) && text[1] == '\0')
615 n->ndup.dupfd = digit_val(text[0]);
616 else if (text[0] == '-' && text[1] == '\0')
621 synerror("Bad fd number");
623 n->ndup.vname = makename();
631 union node *n = redirnode;
633 if (readtoken() != TWORD)
635 if (n->type == NHERE) {
636 struct heredoc *here = heredoc;
642 TRACE(("Here document %d\n", n->type));
643 if (here->striptabs) {
644 while (*wordtext == '\t')
647 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
648 synerror("Illegal eof marker for << redirection");
650 here->eofmark = wordtext;
652 if (heredoclist == NULL)
655 for (p = heredoclist ; p->next ; p = p->next);
658 } else if (n->type == NTOFD || n->type == NFROMFD) {
659 fixredir(n, wordtext, 0);
661 n->nfile.fname = makename();
667 * Input any here documents.
673 struct heredoc *here;
676 while (heredoclist) {
678 heredoclist = here->next;
683 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
684 here->eofmark, here->striptabs);
685 n = (union node *)stalloc(sizeof (struct narg));
688 n->narg.text = wordtext;
689 n->narg.backquote = backquotelist;
690 here->here->nhere.doc = n;
708 int savecheckkwd = checkkwd;
711 int alreadyseen = tokpushback;
730 * check for keywords and aliases
732 if (t == TWORD && !quoteflag)
734 const char * const *pp;
736 for (pp = parsekwd; *pp; pp++) {
737 if (**pp == *wordtext && equal(*pp, wordtext))
739 lasttoken = t = pp - parsekwd + KWDOFFSET;
740 TRACE(("keyword %s recognized\n", tokname[t]));
744 if (noaliases == 0 &&
745 (ap = lookupalias(wordtext, 1)) != NULL) {
746 pushstring(ap->val, strlen(ap->val), ap);
747 checkkwd = savecheckkwd;
756 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
758 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
765 * Read the next input token.
766 * If the token is a word, we set backquotelist to the list of cmds in
767 * backquotes. We set quoteflag to true if any part of the word was
769 * If the token is TREDIR, then we set redirnode to a structure containing
771 * In all cases, the variable startlinno is set to the number of the line
772 * on which the token starts.
774 * [Change comment: here documents and internal procedures]
775 * [Readtoken shouldn't have any arguments. Perhaps we should make the
776 * word parsing code into a separate routine. In this case, readtoken
777 * doesn't need to have any internal procedures, but parseword does.
778 * We could also make parseoperator in essence the main routine, and
779 * have parseword (readtoken1?) handle both words and redirection.]
782 #define RETURN(token) return lasttoken = token
798 for (;;) { /* until token or start of word found */
800 if (c == ' ' || c == '\t')
801 continue; /* quick check for white space first */
806 while ((c = pgetc()) != '\n' && c != PEOF);
810 if (pgetc() == '\n') {
811 startlinno = ++plinno;
822 needprompt = doprompt;
848 /* else FALLTHROUGH */
854 return readtoken1(c, BASESYNTAX, NULL, 0);
861 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
862 * is not NULL, read a here document. In the latter case, eofmark is the
863 * word which marks the end of the document and striptabs is true if
864 * leading tabs should be stripped from the document. The argument firstc
865 * is the first character of the input token or document.
867 * Because C does not have internal subroutines, I have simulated them
868 * using goto's to implement the subroutine linkage. The following macros
869 * will run code that appears at the end of readtoken1.
872 #define CHECKEND() {goto checkend; checkend_return:;}
873 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
874 #define PARSESUB() {goto parsesub; parsesub_return:;}
875 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
876 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
877 #define PARSEARITH() {goto parsearith; parsearith_return:;}
880 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
883 char const * volatile syntax = syn;
886 char line[EOFMARKLEN + 1];
887 struct nodelist *bqlist;
889 volatile int dblquote;
890 volatile int varnest; /* levels of variables expansion */
891 volatile int arinest; /* levels of arithmetic expansion */
892 volatile int parenlevel; /* levels of parens in arithmetic */
893 volatile int oldstyle;
894 char const * volatile prevsyntax = NULL; /* syntax before arithmetic */
899 if (syntax == DQSYNTAX)
908 loop: { /* for each line, until end of word */
909 CHECKEND(); /* set c to PEOF if at end of here document */
910 for (;;) { /* until end of line or end of word */
911 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
913 synentry = syntax[c];
917 if (syntax == BASESYNTAX)
918 goto endword; /* exit outer loop */
926 goto loop; /* continue outer loop */
931 if (eofmark == NULL || dblquote)
932 USTPUTC(CTLESC, out);
935 case CBACK: /* backslash */
940 } else if (c == '\n') {
947 if (dblquote && c != '\\' &&
948 c != '`' && c != '$' &&
949 (c != '"' || eofmark != NULL))
951 if (SQSYNTAX[c] == CCTL)
952 USTPUTC(CTLESC, out);
953 else if (eofmark == NULL)
954 USTPUTC(CTLQUOTEMARK, out);
961 USTPUTC(CTLQUOTEMARK, out);
966 USTPUTC(CTLQUOTEMARK, out);
971 if (eofmark != NULL && arinest == 0 &&
978 } else if (eofmark == NULL) {
986 PARSESUB(); /* parse substitution */
988 case CENDVAR: /* '}' */
991 USTPUTC(CTLENDVAR, out);
996 case CLP: /* '(' in arithmetic */
1000 case CRP: /* ')' in arithmetic */
1001 if (parenlevel > 0) {
1005 if (pgetc() == ')') {
1006 if (--arinest == 0) {
1007 USTPUTC(CTLENDARI, out);
1008 syntax = prevsyntax;
1009 if (syntax == DQSYNTAX)
1018 * (don't 2nd guess - no error)
1025 case CBQUOTE: /* '`' */
1029 goto endword; /* exit outer loop */
1032 goto endword; /* exit outer loop */
1039 if (syntax == ARISYNTAX)
1040 synerror("Missing '))'");
1041 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1042 synerror("Unterminated quoted string");
1044 startlinno = plinno;
1045 synerror("Missing '}'");
1048 len = out - stackblock();
1050 if (eofmark == NULL) {
1051 if ((c == '>' || c == '<')
1054 && (*out == '\0' || is_digit(*out))) {
1056 return lasttoken = TREDIR;
1062 backquotelist = bqlist;
1063 grabstackblock(len);
1065 return lasttoken = TWORD;
1066 /* end of readtoken routine */
1071 * Check to see whether we are at the end of the here document. When this
1072 * is called, c is set to the first character of the next input line. If
1073 * we are at the end of the here document, this routine sets the c to PEOF.
1082 if (c == *eofmark) {
1083 if (pfgets(line, sizeof line) != NULL) {
1087 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1088 if (*p == '\n' && *q == '\0') {
1091 needprompt = doprompt;
1093 pushstring(line, strlen(line), NULL);
1098 goto checkend_return;
1103 * Parse a redirection operator. The variable "out" points to a string
1104 * specifying the fd to be redirected. The variable "c" contains the
1105 * first character of the redirection operator.
1112 np = (union node *)stalloc(sizeof (struct nfile));
1121 np->type = NCLOBBER;
1126 } else { /* c == '<' */
1130 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1131 np = (union node *)stalloc(sizeof (struct nhere));
1135 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1137 if ((c = pgetc()) == '-') {
1138 heredoc->striptabs = 1;
1140 heredoc->striptabs = 0;
1143 } else if (c == '&')
1153 np->nfile.fd = digit_val(fd);
1155 goto parseredir_return;
1160 * Parse a substitution. At this point, we have read the dollar sign
1170 static const char types[] = "}-+?=";
1171 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1177 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1181 } else if (c == '(') { /* $(command) or $((arith)) */
1182 if (pgetc() == '(') {
1189 USTPUTC(CTLVAR, out);
1190 typeloc = out - stackblock();
1191 USTPUTC(VSNORMAL, out);
1198 if ((c = pgetc()) == '}')
1206 if (!is_eof(c) && is_name(c)) {
1212 } while (!is_eof(c) && is_in_name(c));
1214 strncmp(out - length, "LINENO", length) == 0) {
1215 /* Replace the variable name with the
1216 * current line number. */
1219 linno -= funclinno - 1;
1220 snprintf(buf, sizeof(buf), "%d", linno);
1222 for (i = 0; buf[i] != '\0'; i++)
1223 STPUTC(buf[i], out);
1226 } else if (is_digit(c)) {
1227 if (bracketed_name) {
1231 } while (is_digit(c));
1237 if (! is_special(c)) {
1255 p = strchr(types, c);
1262 subtype = p - types + VSNORMAL;
1268 subtype = c == '#' ? VSTRIMLEFT :
1278 } else if (subtype != VSERROR) {
1282 if (subtype != VSLENGTH && (dblquote || arinest))
1284 *(stackblock() + typeloc) = subtype | flags;
1285 if (subtype != VSNORMAL)
1288 goto parsesub_return;
1293 * Called to parse command substitutions. Newstyle is set if the command
1294 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1295 * list of commands (passed by reference), and savelen is the number of
1296 * characters on the top of the stack which must be preserved.
1300 struct nodelist **nlpp;
1304 struct jmploc jmploc;
1305 struct jmploc *volatile savehandler;
1307 volatile int saveprompt;
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);
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.