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 $
48 #include "expand.h" /* defines rmescapes() */
61 #include "myhistedit.h"
65 * Shell command parser.
71 /* values returned by readtoken */
77 struct heredoc *next; /* next here document in list */
78 union node *here; /* redirection node */
79 char *eofmark; /* string indicating end of input */
80 int striptabs; /* if set, strip leading tabs */
85 STATIC struct heredoc *heredoclist; /* list of here documents to read */
86 STATIC int parsebackquote; /* nonzero if we are inside backquotes */
87 STATIC int doprompt; /* if set, prompt the user */
88 STATIC int needprompt; /* true if interactive and at start of line */
89 STATIC int lasttoken; /* last token read */
90 MKINIT int tokpushback; /* last token pushed back */
91 STATIC char *wordtext; /* text of last word returned by readtoken */
94 * 2 == also eat newlines
98 STATIC struct nodelist *backquotelist;
99 STATIC union node *redirnode;
100 STATIC struct heredoc *heredoc;
101 STATIC int quoteflag; /* set if (part of) last token was quoted */
102 STATIC int startlinno; /* line # where last token started */
103 STATIC int funclinno; /* line # where the current function started */
105 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
106 static int noaliases = 0;
109 STATIC union node *list(int);
110 STATIC union node *andor(void);
111 STATIC union node *pipeline(void);
112 STATIC union node *command(void);
113 STATIC union node *simplecmd(union node **, union node *);
114 STATIC union node *makename(void);
115 STATIC void parsefname(void);
116 STATIC void parseheredoc(void);
117 STATIC int peektoken(void);
118 STATIC int readtoken(void);
119 STATIC int xxreadtoken(void);
120 STATIC int readtoken1(int, char const *, char *, int);
121 STATIC int noexpand(char *);
122 STATIC void synexpect(int);
123 STATIC void synerror(const char *);
124 STATIC void setprompt(int);
128 * Read and parse a command. Returns NEOF on end of file. (NULL is a
129 * valid parse tree indicating a blank line.)
133 parsecmd(int interact)
157 union node *n1, *n2, *n3;
161 if (nlflag == 0 && tokendlist[peektoken()])
167 if (tok == TBACKGND) {
168 if (n2->type == NCMD || n2->type == NPIPE) {
169 n2->ncmd.backgnd = 1;
170 } else if (n2->type == NREDIR) {
173 n3 = (union node *)stalloc(sizeof (struct nredir));
176 n3->nredir.redirect = NULL;
184 n3 = (union node *)stalloc(sizeof (struct nbinary));
186 n3->nbinary.ch1 = n1;
187 n3->nbinary.ch2 = n2;
204 if (tokendlist[peektoken()])
211 pungetc(); /* push back EOF on input */
227 union node *n1, *n2, *n3;
232 if ((t = readtoken()) == TAND) {
234 } else if (t == TOR) {
241 n3 = (union node *)stalloc(sizeof (struct nbinary));
243 n3->nbinary.ch1 = n1;
244 n3->nbinary.ch2 = n2;
254 union node *n1, *n2, *pipenode;
255 struct nodelist *lp, *prev;
259 TRACE(("pipeline: entered\n"));
262 while (readtoken() == TNOT)
267 if (readtoken() == TPIPE) {
268 pipenode = (union node *)stalloc(sizeof (struct npipe));
269 pipenode->type = NPIPE;
270 pipenode->npipe.backgnd = 0;
271 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
272 pipenode->npipe.cmdlist = lp;
278 while (readtoken() == TNOT)
279 innernegate = !innernegate;
283 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
288 n2 = (union node *)stalloc(sizeof (struct nnot));
290 n2->nnot.com = lp->n;
293 } while (readtoken() == TPIPE);
299 n2 = (union node *)stalloc(sizeof (struct nnot));
313 union node *ap, **app;
314 union node *cp, **cpp;
315 union node *redir, **rpp;
323 /* Check for redirection which may precede command */
324 while (readtoken() == TREDIR) {
325 *rpp = n2 = redirnode;
326 rpp = &n2->nfile.next;
331 switch (readtoken()) {
333 n1 = (union node *)stalloc(sizeof (struct nif));
335 if ((n1->nif.test = list(0)) == NULL)
337 if (readtoken() != TTHEN)
339 n1->nif.ifpart = list(0);
341 while (readtoken() == TELIF) {
342 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
343 n2 = n2->nif.elsepart;
345 if ((n2->nif.test = list(0)) == NULL)
347 if (readtoken() != TTHEN)
349 n2->nif.ifpart = list(0);
351 if (lasttoken == TELSE)
352 n2->nif.elsepart = list(0);
354 n2->nif.elsepart = NULL;
357 if (readtoken() != TFI)
364 n1 = (union node *)stalloc(sizeof (struct nbinary));
365 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
366 if ((n1->nbinary.ch1 = list(0)) == NULL)
368 if ((got=readtoken()) != TDO) {
369 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
372 n1->nbinary.ch2 = list(0);
373 if (readtoken() != TDONE)
379 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
380 synerror("Bad for loop variable");
381 n1 = (union node *)stalloc(sizeof (struct nfor));
383 n1->nfor.var = wordtext;
384 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
386 while (readtoken() == TWORD) {
387 n2 = (union node *)stalloc(sizeof (struct narg));
389 n2->narg.text = wordtext;
390 n2->narg.backquote = backquotelist;
392 app = &n2->narg.next;
396 if (lasttoken != TNL && lasttoken != TSEMI)
399 static char argvars[5] = {
400 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
402 n2 = (union node *)stalloc(sizeof (struct narg));
404 n2->narg.text = argvars;
405 n2->narg.backquote = NULL;
406 n2->narg.next = NULL;
409 * Newline or semicolon here is optional (but note
410 * that the original Bourne shell only allowed NL).
412 if (lasttoken != TNL && lasttoken != TSEMI)
416 if ((t = readtoken()) == TDO)
418 else if (t == TBEGIN)
422 n1->nfor.body = list(0);
423 if (readtoken() != t)
428 n1 = (union node *)stalloc(sizeof (struct ncase));
430 if (readtoken() != TWORD)
432 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
434 n2->narg.text = wordtext;
435 n2->narg.backquote = backquotelist;
436 n2->narg.next = NULL;
437 while (readtoken() == TNL);
438 if (lasttoken != TWORD || ! equal(wordtext, "in"))
439 synerror("expecting \"in\"");
440 cpp = &n1->ncase.cases;
441 noaliases = 1; /* turn off alias expansion */
442 checkkwd = 2, readtoken();
443 while (lasttoken != TESAC) {
444 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
446 app = &cp->nclist.pattern;
447 if (lasttoken == TLP)
450 *app = ap = (union node *)stalloc(sizeof (struct narg));
452 ap->narg.text = wordtext;
453 ap->narg.backquote = backquotelist;
454 if (checkkwd = 2, readtoken() != TPIPE)
456 app = &ap->narg.next;
459 ap->narg.next = NULL;
460 if (lasttoken != TRP)
461 noaliases = 0, synexpect(TRP);
462 cp->nclist.body = list(0);
465 if ((t = readtoken()) != TESAC) {
467 noaliases = 0, synexpect(TENDCASE);
469 checkkwd = 2, readtoken();
471 cpp = &cp->nclist.next;
473 noaliases = 0; /* reset alias expansion */
478 n1 = (union node *)stalloc(sizeof (struct nredir));
479 n1->type = NSUBSHELL;
480 n1->nredir.n = list(0);
481 n1->nredir.redirect = NULL;
482 if (readtoken() != TRP)
488 if (readtoken() != TEND)
492 /* Handle an empty command like other simple commands. */
497 * An empty command before a ; doesn't make much sense, and
498 * should certainly be disallowed in the case of `if ;'.
507 return simplecmd(rpp, redir);
512 /* Now check for redirection which may follow command */
513 while (readtoken() == TREDIR) {
514 *rpp = n2 = redirnode;
515 rpp = &n2->nfile.next;
521 if (n1->type != NSUBSHELL) {
522 n2 = (union node *)stalloc(sizeof (struct nredir));
527 n1->nredir.redirect = redir;
534 simplecmd(union node **rpp, union node *redir)
536 union node *args, **app;
537 union node **orig_rpp = rpp;
538 union node *n = NULL;
540 /* If we don't have any redirections already, then we must reset */
541 /* rpp to be the address of the local redir variable. */
548 * We save the incoming value, because we need this for shell
549 * functions. There can not be a redirect or an argument between
550 * the function name and the open parenthesis.
555 if (readtoken() == TWORD) {
556 n = (union node *)stalloc(sizeof (struct narg));
558 n->narg.text = wordtext;
559 n->narg.backquote = backquotelist;
562 } else if (lasttoken == TREDIR) {
563 *rpp = n = redirnode;
564 rpp = &n->nfile.next;
565 parsefname(); /* read name of redirection file */
566 } else if (lasttoken == TLP && app == &args->narg.next
567 && rpp == orig_rpp) {
568 /* We have a function */
569 if (readtoken() != TRP)
573 if (! goodname(n->narg.text))
574 synerror("Bad function name");
577 n->narg.next = command();
587 n = (union node *)stalloc(sizeof (struct ncmd));
591 n->ncmd.redirect = redir;
600 n = (union node *)stalloc(sizeof (struct narg));
603 n->narg.text = wordtext;
604 n->narg.backquote = backquotelist;
609 fixredir(union node *n, const char *text, int err)
611 TRACE(("Fix redir %s %d\n", text, err));
613 n->ndup.vname = NULL;
615 if (is_digit(text[0]) && text[1] == '\0')
616 n->ndup.dupfd = digit_val(text[0]);
617 else if (text[0] == '-' && text[1] == '\0')
622 synerror("Bad fd number");
624 n->ndup.vname = makename();
632 union node *n = redirnode;
634 if (readtoken() != TWORD)
636 if (n->type == NHERE) {
637 struct heredoc *here = heredoc;
643 TRACE(("Here document %d\n", n->type));
644 if (here->striptabs) {
645 while (*wordtext == '\t')
648 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
649 synerror("Illegal eof marker for << redirection");
651 here->eofmark = wordtext;
653 if (heredoclist == NULL)
656 for (p = heredoclist ; p->next ; p = p->next);
659 } else if (n->type == NTOFD || n->type == NFROMFD) {
660 fixredir(n, wordtext, 0);
662 n->nfile.fname = makename();
668 * Input any here documents.
674 struct heredoc *here;
677 while (heredoclist) {
679 heredoclist = here->next;
684 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
685 here->eofmark, here->striptabs);
686 n = (union node *)stalloc(sizeof (struct narg));
689 n->narg.text = wordtext;
690 n->narg.backquote = backquotelist;
691 here->here->nhere.doc = n;
709 int savecheckkwd = checkkwd;
712 int alreadyseen = tokpushback;
731 * check for keywords and aliases
733 if (t == TWORD && !quoteflag)
735 const char * const *pp;
737 for (pp = parsekwd; *pp; pp++) {
738 if (**pp == *wordtext && equal(*pp, wordtext))
740 lasttoken = t = pp - parsekwd + KWDOFFSET;
741 TRACE(("keyword %s recognized\n", tokname[t]));
745 if (noaliases == 0 &&
746 (ap = lookupalias(wordtext, 1)) != NULL) {
747 pushstring(ap->val, strlen(ap->val), ap);
748 checkkwd = savecheckkwd;
757 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
759 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
766 * Read the next input token.
767 * If the token is a word, we set backquotelist to the list of cmds in
768 * backquotes. We set quoteflag to true if any part of the word was
770 * If the token is TREDIR, then we set redirnode to a structure containing
772 * In all cases, the variable startlinno is set to the number of the line
773 * on which the token starts.
775 * [Change comment: here documents and internal procedures]
776 * [Readtoken shouldn't have any arguments. Perhaps we should make the
777 * word parsing code into a separate routine. In this case, readtoken
778 * doesn't need to have any internal procedures, but parseword does.
779 * We could also make parseoperator in essence the main routine, and
780 * have parseword (readtoken1?) handle both words and redirection.]
783 #define RETURN(token) return lasttoken = token
799 for (;;) { /* until token or start of word found */
801 if (c == ' ' || c == '\t')
802 continue; /* quick check for white space first */
807 while ((c = pgetc()) != '\n' && c != PEOF);
811 if (pgetc() == '\n') {
812 startlinno = ++plinno;
823 needprompt = doprompt;
849 /* else FALLTHROUGH */
855 return readtoken1(c, BASESYNTAX, NULL, 0);
862 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
863 * is not NULL, read a here document. In the latter case, eofmark is the
864 * word which marks the end of the document and striptabs is true if
865 * leading tabs should be stripped from the document. The argument firstc
866 * is the first character of the input token or document.
868 * Because C does not have internal subroutines, I have simulated them
869 * using goto's to implement the subroutine linkage. The following macros
870 * will run code that appears at the end of readtoken1.
873 #define CHECKEND() {goto checkend; checkend_return:;}
874 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
875 #define PARSESUB() {goto parsesub; parsesub_return:;}
876 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
877 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
878 #define PARSEARITH() {goto parsearith; parsearith_return:;}
881 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
884 char const * volatile syntax = syn;
887 char line[EOFMARKLEN + 1];
888 struct nodelist *bqlist;
890 volatile int dblquote;
891 volatile int varnest; /* levels of variables expansion */
892 volatile int arinest; /* levels of arithmetic expansion */
893 volatile int parenlevel; /* levels of parens in arithmetic */
894 volatile int oldstyle;
895 char const * volatile prevsyntax = NULL; /* syntax before arithmetic */
900 if (syntax == DQSYNTAX)
909 loop: { /* for each line, until end of word */
910 CHECKEND(); /* set c to PEOF if at end of here document */
911 for (;;) { /* until end of line or end of word */
912 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
914 synentry = syntax[c];
918 if (syntax == BASESYNTAX)
919 goto endword; /* exit outer loop */
927 goto loop; /* continue outer loop */
932 if (eofmark == NULL || dblquote)
933 USTPUTC(CTLESC, out);
936 case CBACK: /* backslash */
941 } else if (c == '\n') {
948 if (dblquote && c != '\\' &&
949 c != '`' && c != '$' &&
950 (c != '"' || eofmark != NULL))
952 if (SQSYNTAX[c] == CCTL)
953 USTPUTC(CTLESC, out);
954 else if (eofmark == NULL)
955 USTPUTC(CTLQUOTEMARK, out);
962 USTPUTC(CTLQUOTEMARK, out);
967 USTPUTC(CTLQUOTEMARK, out);
972 if (eofmark != NULL && arinest == 0 &&
979 } else if (eofmark == NULL) {
987 PARSESUB(); /* parse substitution */
989 case CENDVAR: /* '}' */
992 USTPUTC(CTLENDVAR, out);
997 case CLP: /* '(' in arithmetic */
1001 case CRP: /* ')' in arithmetic */
1002 if (parenlevel > 0) {
1006 if (pgetc() == ')') {
1007 if (--arinest == 0) {
1008 USTPUTC(CTLENDARI, out);
1009 syntax = prevsyntax;
1010 if (syntax == DQSYNTAX)
1019 * (don't 2nd guess - no error)
1026 case CBQUOTE: /* '`' */
1030 goto endword; /* exit outer loop */
1033 goto endword; /* exit outer loop */
1040 if (syntax == ARISYNTAX)
1041 synerror("Missing '))'");
1042 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1043 synerror("Unterminated quoted string");
1045 startlinno = plinno;
1046 synerror("Missing '}'");
1049 len = out - stackblock();
1051 if (eofmark == NULL) {
1052 if ((c == '>' || c == '<')
1055 && (*out == '\0' || is_digit(*out))) {
1057 return lasttoken = TREDIR;
1063 backquotelist = bqlist;
1064 grabstackblock(len);
1066 return lasttoken = TWORD;
1067 /* end of readtoken routine */
1072 * Check to see whether we are at the end of the here document. When this
1073 * is called, c is set to the first character of the next input line. If
1074 * we are at the end of the here document, this routine sets the c to PEOF.
1083 if (c == *eofmark) {
1084 if (pfgets(line, sizeof line) != NULL) {
1088 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1089 if (*p == '\n' && *q == '\0') {
1092 needprompt = doprompt;
1094 pushstring(line, strlen(line), NULL);
1099 goto checkend_return;
1104 * Parse a redirection operator. The variable "out" points to a string
1105 * specifying the fd to be redirected. The variable "c" contains the
1106 * first character of the redirection operator.
1113 np = (union node *)stalloc(sizeof (struct nfile));
1122 np->type = NCLOBBER;
1127 } else { /* c == '<' */
1131 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1132 np = (union node *)stalloc(sizeof (struct nhere));
1136 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1138 if ((c = pgetc()) == '-') {
1139 heredoc->striptabs = 1;
1141 heredoc->striptabs = 0;
1144 } else if (c == '&')
1154 np->nfile.fd = digit_val(fd);
1156 goto parseredir_return;
1161 * Parse a substitution. At this point, we have read the dollar sign
1171 static const char types[] = "}-+?=";
1172 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1178 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1182 } else if (c == '(') { /* $(command) or $((arith)) */
1183 if (pgetc() == '(') {
1190 USTPUTC(CTLVAR, out);
1191 typeloc = out - stackblock();
1192 USTPUTC(VSNORMAL, out);
1199 if ((c = pgetc()) == '}')
1207 if (!is_eof(c) && is_name(c)) {
1213 } while (!is_eof(c) && is_in_name(c));
1215 strncmp(out - length, "LINENO", length) == 0) {
1216 /* Replace the variable name with the
1217 * current line number. */
1220 linno -= funclinno - 1;
1221 snprintf(buf, sizeof(buf), "%d", linno);
1223 for (i = 0; buf[i] != '\0'; i++)
1224 STPUTC(buf[i], out);
1227 } else if (is_digit(c)) {
1228 if (bracketed_name) {
1232 } while (is_digit(c));
1238 if (! is_special(c)) {
1256 p = strchr(types, c);
1263 subtype = p - types + VSNORMAL;
1269 subtype = c == '#' ? VSTRIMLEFT :
1279 } else if (subtype != VSERROR) {
1283 if (subtype != VSLENGTH && (dblquote || arinest))
1285 *(stackblock() + typeloc) = subtype | flags;
1286 if (subtype != VSNORMAL)
1289 goto parsesub_return;
1294 * Called to parse command substitutions. Newstyle is set if the command
1295 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1296 * list of commands (passed by reference), and savelen is the number of
1297 * characters on the top of the stack which must be preserved.
1301 struct nodelist **nlpp;
1305 struct jmploc jmploc;
1306 struct jmploc *volatile savehandler;
1308 volatile int saveprompt;
1310 savepbq = parsebackquote;
1311 if (setjmp(jmploc.loc)) {
1315 handler = savehandler;
1316 longjmp(handler->loc, 1);
1320 savelen = out - stackblock();
1322 str = ckmalloc(savelen);
1323 memcpy(str, stackblock(), savelen);
1325 savehandler = handler;
1329 /* We must read until the closing backquote, giving special
1330 treatment to some slashes, and then push the string and
1331 reread it as input, interpreting it normally. */
1338 STARTSTACKSTR(pout);
1344 switch (pc = pgetc()) {
1349 if ((pc = pgetc()) == '\n') {
1356 * If eating a newline, avoid putting
1357 * the newline into the new character
1358 * stream (via the STPUTC after the
1363 if (pc != '\\' && pc != '`' && pc != '$'
1364 && (!dblquote || pc != '"'))
1370 needprompt = doprompt;
1374 startlinno = plinno;
1375 synerror("EOF in backquote substitution");
1385 psavelen = pout - stackblock();
1387 pstr = ckmalloc(psavelen);
1388 memcpy(pstr, stackblock(), psavelen);
1389 setinputstring(pstr, 1);
1394 nlpp = &(*nlpp)->next;
1395 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1396 (*nlpp)->next = NULL;
1397 parsebackquote = oldstyle;
1400 saveprompt = doprompt;
1407 doprompt = saveprompt;
1409 if (readtoken() != TRP)
1416 * Start reading from old file again, ignoring any pushed back
1417 * tokens left from the backquote parsing
1422 while (stackblocksize() <= savelen)
1426 memcpy(out, str, savelen);
1427 STADJUST(savelen, out);
1433 parsebackquote = savepbq;
1434 handler = savehandler;
1435 if (arinest || dblquote)
1436 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1438 USTPUTC(CTLBACKQ, out);
1440 goto parsebackq_oldreturn;
1442 goto parsebackq_newreturn;
1446 * Parse an arithmetic expansion (indicate start of one and set state)
1450 if (++arinest == 1) {
1451 prevsyntax = syntax;
1453 USTPUTC(CTLARI, out);
1460 * we collapse embedded arithmetic expansion to
1461 * parenthesis, which should be equivalent
1465 goto parsearith_return;
1468 } /* end of readtoken */
1480 * Returns true if the text contains nothing to expand (no dollar signs
1485 noexpand(char *text)
1491 while ((c = *p++) != '\0') {
1492 if ( c == CTLQUOTEMARK)
1496 else if (BASESYNTAX[(int)c] == CCTL)
1504 * Return true if the argument is a legal variable name (a letter or
1505 * underscore followed by zero or more letters, underscores, and digits).
1509 goodname(char *name)
1517 if (! is_in_name(*p))
1525 * Called when an unexpected token is read during the parse. The argument
1526 * is the token that is expected, or -1 if more than one type of token can
1527 * occur at this point.
1531 synexpect(int token)
1536 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1537 tokname[lasttoken], tokname[token]);
1539 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1546 synerror(const char *msg)
1549 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1550 outfmt(&errout, "Syntax error: %s\n", msg);
1555 setprompt(int which)
1557 whichprompt = which;
1562 out2str(getprompt(NULL));
1566 * called by editline -- any expansions to the prompt
1567 * should be added here.
1570 getprompt(void *unused __unused)
1572 static char ps[PROMPTLEN];
1577 * Select prompt format.
1579 switch (whichprompt) {
1590 return "<internal prompt error>";
1594 * Format prompt string.
1596 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1603 * \h specifies just the local hostname,
1604 * \H specifies fully-qualified hostname.
1609 gethostname(&ps[i], PROMPTLEN - i);
1610 /* Skip to end of hostname. */
1611 trim = (*fmt == 'h') ? '.' : '\0';
1612 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1617 * Working directory.
1619 * \W specifies just the final component,
1620 * \w specifies the entire path.
1625 getcwd(&ps[i], PROMPTLEN - i);
1627 /* Final path component only. */
1629 for (j = i; ps[j] != '\0'; j++)
1632 memmove(&ps[i], &ps[trim],
1635 /* Skip to end of path. */
1636 while (ps[i + 1] != '\0')
1643 * '$' for normal users, '#' for root.
1646 ps[i] = (geteuid() != 0) ? '$' : '#';
1657 * Emit unrecognized formats verbatim.