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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 */
97 STATIC int funclinno; /* line # where the current function started */
99 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
100 static int noaliases = 0;
103 STATIC union node *list(int);
104 STATIC union node *andor(void);
105 STATIC union node *pipeline(void);
106 STATIC union node *command(void);
107 STATIC union node *simplecmd(union node **, union node *);
108 STATIC union node *makename(void);
109 STATIC void parsefname(void);
110 STATIC void parseheredoc(void);
111 STATIC int peektoken(void);
112 STATIC int readtoken(void);
113 STATIC int xxreadtoken(void);
114 STATIC int readtoken1(int, char const *, char *, int);
115 STATIC int noexpand(char *);
116 STATIC void synexpect(int);
117 STATIC void synerror(char *);
118 STATIC void setprompt(int);
122 * Read and parse a command. Returns NEOF on end of file. (NULL is a
123 * valid parse tree indicating a blank line.)
127 parsecmd(int interact)
151 union node *n1, *n2, *n3;
155 if (nlflag == 0 && tokendlist[peektoken()])
161 if (tok == TBACKGND) {
162 if (n2->type == NCMD || n2->type == NPIPE) {
163 n2->ncmd.backgnd = 1;
164 } else if (n2->type == NREDIR) {
167 n3 = (union node *)stalloc(sizeof (struct nredir));
170 n3->nredir.redirect = NULL;
178 n3 = (union node *)stalloc(sizeof (struct nbinary));
180 n3->nbinary.ch1 = n1;
181 n3->nbinary.ch2 = n2;
198 if (tokendlist[peektoken()])
205 pungetc(); /* push back EOF on input */
221 union node *n1, *n2, *n3;
226 if ((t = readtoken()) == TAND) {
228 } else if (t == TOR) {
235 n3 = (union node *)stalloc(sizeof (struct nbinary));
237 n3->nbinary.ch1 = n1;
238 n3->nbinary.ch2 = n2;
248 union node *n1, *n2, *pipenode;
249 struct nodelist *lp, *prev;
253 TRACE(("pipeline: entered\n"));
254 while (readtoken() == TNOT)
258 if (readtoken() == TPIPE) {
259 pipenode = (union node *)stalloc(sizeof (struct npipe));
260 pipenode->type = NPIPE;
261 pipenode->npipe.backgnd = 0;
262 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
263 pipenode->npipe.cmdlist = lp;
267 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
270 } while (readtoken() == TPIPE);
276 n2 = (union node *)stalloc(sizeof (struct nnot));
290 union node *ap, **app;
291 union node *cp, **cpp;
292 union node *redir, **rpp;
300 /* Check for redirection which may precede command */
301 while (readtoken() == TREDIR) {
302 *rpp = n2 = redirnode;
303 rpp = &n2->nfile.next;
308 while (readtoken() == TNOT) {
309 TRACE(("command: TNOT recognized\n"));
314 switch (readtoken()) {
316 n1 = (union node *)stalloc(sizeof (struct nif));
318 if ((n1->nif.test = list(0)) == NULL)
320 if (readtoken() != TTHEN)
322 n1->nif.ifpart = list(0);
324 while (readtoken() == TELIF) {
325 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
326 n2 = n2->nif.elsepart;
328 if ((n2->nif.test = list(0)) == NULL)
330 if (readtoken() != TTHEN)
332 n2->nif.ifpart = list(0);
334 if (lasttoken == TELSE)
335 n2->nif.elsepart = list(0);
337 n2->nif.elsepart = NULL;
340 if (readtoken() != TFI)
347 n1 = (union node *)stalloc(sizeof (struct nbinary));
348 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
349 if ((n1->nbinary.ch1 = list(0)) == NULL)
351 if ((got=readtoken()) != TDO) {
352 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
355 n1->nbinary.ch2 = list(0);
356 if (readtoken() != TDONE)
362 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
363 synerror("Bad for loop variable");
364 n1 = (union node *)stalloc(sizeof (struct nfor));
366 n1->nfor.var = wordtext;
367 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
369 while (readtoken() == TWORD) {
370 n2 = (union node *)stalloc(sizeof (struct narg));
372 n2->narg.text = wordtext;
373 n2->narg.backquote = backquotelist;
375 app = &n2->narg.next;
379 if (lasttoken != TNL && lasttoken != TSEMI)
382 static char argvars[5] = {
383 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
385 n2 = (union node *)stalloc(sizeof (struct narg));
387 n2->narg.text = argvars;
388 n2->narg.backquote = NULL;
389 n2->narg.next = NULL;
392 * Newline or semicolon here is optional (but note
393 * that the original Bourne shell only allowed NL).
395 if (lasttoken != TNL && lasttoken != TSEMI)
399 if ((t = readtoken()) == TDO)
401 else if (t == TBEGIN)
405 n1->nfor.body = list(0);
406 if (readtoken() != t)
411 n1 = (union node *)stalloc(sizeof (struct ncase));
413 if (readtoken() != TWORD)
415 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
417 n2->narg.text = wordtext;
418 n2->narg.backquote = backquotelist;
419 n2->narg.next = NULL;
420 while (readtoken() == TNL);
421 if (lasttoken != TWORD || ! equal(wordtext, "in"))
422 synerror("expecting \"in\"");
423 cpp = &n1->ncase.cases;
424 noaliases = 1; /* turn off alias expansion */
425 checkkwd = 2, readtoken();
426 while (lasttoken != TESAC) {
427 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
429 app = &cp->nclist.pattern;
430 if (lasttoken == TLP)
433 *app = ap = (union node *)stalloc(sizeof (struct narg));
435 ap->narg.text = wordtext;
436 ap->narg.backquote = backquotelist;
437 if (checkkwd = 2, readtoken() != TPIPE)
439 app = &ap->narg.next;
442 ap->narg.next = NULL;
443 if (lasttoken != TRP)
444 noaliases = 0, synexpect(TRP);
445 cp->nclist.body = list(0);
448 if ((t = readtoken()) != TESAC) {
450 noaliases = 0, synexpect(TENDCASE);
452 checkkwd = 2, readtoken();
454 cpp = &cp->nclist.next;
456 noaliases = 0; /* reset alias expansion */
461 n1 = (union node *)stalloc(sizeof (struct nredir));
462 n1->type = NSUBSHELL;
463 n1->nredir.n = list(0);
464 n1->nredir.redirect = NULL;
465 if (readtoken() != TRP)
471 if (readtoken() != TEND)
475 /* Handle an empty command like other simple commands. */
480 * An empty command before a ; doesn't make much sense, and
481 * should certainly be disallowed in the case of `if ;'.
490 n1 = simplecmd(rpp, redir);
496 /* Now check for redirection which may follow command */
497 while (readtoken() == TREDIR) {
498 *rpp = n2 = redirnode;
499 rpp = &n2->nfile.next;
505 if (n1->type != NSUBSHELL) {
506 n2 = (union node *)stalloc(sizeof (struct nredir));
511 n1->nredir.redirect = redir;
516 n2 = (union node *)stalloc(sizeof (struct nnot));
527 simplecmd(union node **rpp, union node *redir)
529 union node *args, **app;
530 union node **orig_rpp = rpp;
531 union node *n = NULL, *n2;
534 /* If we don't have any redirections already, then we must reset */
535 /* rpp to be the address of the local redir variable. */
542 * We save the incoming value, because we need this for shell
543 * functions. There can not be a redirect or an argument between
544 * the function name and the open parenthesis.
548 while (readtoken() == TNOT) {
549 TRACE(("command: TNOT recognized\n"));
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;
595 n2 = (union node *)stalloc(sizeof (struct nnot));
609 n = (union node *)stalloc(sizeof (struct narg));
612 n->narg.text = wordtext;
613 n->narg.backquote = backquotelist;
617 void fixredir(union node *n, const char *text, int err)
619 TRACE(("Fix redir %s %d\n", text, err));
621 n->ndup.vname = NULL;
623 if (is_digit(text[0]) && text[1] == '\0')
624 n->ndup.dupfd = digit_val(text[0]);
625 else if (text[0] == '-' && text[1] == '\0')
630 synerror("Bad fd number");
632 n->ndup.vname = makename();
640 union node *n = redirnode;
642 if (readtoken() != TWORD)
644 if (n->type == NHERE) {
645 struct heredoc *here = heredoc;
651 TRACE(("Here document %d\n", n->type));
652 if (here->striptabs) {
653 while (*wordtext == '\t')
656 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
657 synerror("Illegal eof marker for << redirection");
659 here->eofmark = wordtext;
661 if (heredoclist == NULL)
664 for (p = heredoclist ; p->next ; p = p->next);
667 } else if (n->type == NTOFD || n->type == NFROMFD) {
668 fixredir(n, wordtext, 0);
670 n->nfile.fname = makename();
676 * Input any here documents.
682 struct heredoc *here;
685 while (heredoclist) {
687 heredoclist = here->next;
692 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
693 here->eofmark, here->striptabs);
694 n = (union node *)stalloc(sizeof (struct narg));
697 n->narg.text = wordtext;
698 n->narg.backquote = backquotelist;
699 here->here->nhere.doc = n;
717 int savecheckkwd = checkkwd;
720 int alreadyseen = tokpushback;
739 * check for keywords and aliases
741 if (t == TWORD && !quoteflag)
743 const char * const *pp;
745 for (pp = parsekwd; *pp; pp++) {
746 if (**pp == *wordtext && equal(*pp, wordtext))
748 lasttoken = t = pp - parsekwd + KWDOFFSET;
749 TRACE(("keyword %s recognized\n", tokname[t]));
753 if (noaliases == 0 &&
754 (ap = lookupalias(wordtext, 1)) != NULL) {
755 pushstring(ap->val, strlen(ap->val), ap);
756 checkkwd = savecheckkwd;
761 checkkwd = (t == TNOT) ? savecheckkwd : 0;
765 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
767 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
774 * Read the next input token.
775 * If the token is a word, we set backquotelist to the list of cmds in
776 * backquotes. We set quoteflag to true if any part of the word was
778 * If the token is TREDIR, then we set redirnode to a structure containing
780 * In all cases, the variable startlinno is set to the number of the line
781 * on which the token starts.
783 * [Change comment: here documents and internal procedures]
784 * [Readtoken shouldn't have any arguments. Perhaps we should make the
785 * word parsing code into a separate routine. In this case, readtoken
786 * doesn't need to have any internal procedures, but parseword does.
787 * We could also make parseoperator in essence the main routine, and
788 * have parseword (readtoken1?) handle both words and redirection.]
791 #define RETURN(token) return lasttoken = token
807 for (;;) { /* until token or start of word found */
809 if (c == ' ' || c == '\t')
810 continue; /* quick check for white space first */
815 while ((c = pgetc()) != '\n' && c != PEOF);
819 if (pgetc() == '\n') {
820 startlinno = ++plinno;
831 needprompt = doprompt;
859 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
866 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
867 * is not NULL, read a here document. In the latter case, eofmark is the
868 * word which marks the end of the document and striptabs is true if
869 * leading tabs should be stripped from the document. The argument firstc
870 * is the first character of the input token or document.
872 * Because C does not have internal subroutines, I have simulated them
873 * using goto's to implement the subroutine linkage. The following macros
874 * will run code that appears at the end of readtoken1.
877 #define CHECKEND() {goto checkend; checkend_return:;}
878 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
879 #define PARSESUB() {goto parsesub; parsesub_return:;}
880 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
881 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
882 #define PARSEARITH() {goto parsearith; parsearith_return:;}
885 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
890 char line[EOFMARKLEN + 1];
891 struct nodelist *bqlist;
894 int varnest; /* levels of variables expansion */
895 int arinest; /* levels of arithmetic expansion */
896 int parenlevel; /* levels of parens in arithmetic */
898 char const *prevsyntax; /* syntax before arithmetic */
901 /* Avoid longjmp clobbering */
916 if (syntax == DQSYNTAX)
925 loop: { /* for each line, until end of word */
926 CHECKEND(); /* set c to PEOF if at end of here document */
927 for (;;) { /* until end of line or end of word */
928 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
930 synentry = syntax[c];
934 if (syntax == BASESYNTAX)
935 goto endword; /* exit outer loop */
943 goto loop; /* continue outer loop */
948 if (eofmark == NULL || dblquote)
949 USTPUTC(CTLESC, out);
952 case CBACK: /* backslash */
957 } else if (c == '\n') {
964 if (dblquote && c != '\\' &&
965 c != '`' && c != '$' &&
966 (c != '"' || eofmark != NULL))
968 if (SQSYNTAX[c] == CCTL)
969 USTPUTC(CTLESC, out);
970 else if (eofmark == NULL)
971 USTPUTC(CTLQUOTEMARK, out);
978 USTPUTC(CTLQUOTEMARK, out);
983 USTPUTC(CTLQUOTEMARK, out);
988 if (eofmark != NULL && arinest == 0 &&
995 } else if (eofmark == NULL) {
1002 case CVAR: /* '$' */
1003 PARSESUB(); /* parse substitution */
1005 case CENDVAR: /* '}' */
1008 USTPUTC(CTLENDVAR, out);
1013 case CLP: /* '(' in arithmetic */
1017 case CRP: /* ')' in arithmetic */
1018 if (parenlevel > 0) {
1022 if (pgetc() == ')') {
1023 if (--arinest == 0) {
1024 USTPUTC(CTLENDARI, out);
1025 syntax = prevsyntax;
1026 if (syntax == DQSYNTAX)
1035 * (don't 2nd guess - no error)
1042 case CBQUOTE: /* '`' */
1046 goto endword; /* exit outer loop */
1049 goto endword; /* exit outer loop */
1056 if (syntax == ARISYNTAX)
1057 synerror("Missing '))'");
1058 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1059 synerror("Unterminated quoted string");
1061 startlinno = plinno;
1062 synerror("Missing '}'");
1065 len = out - stackblock();
1067 if (eofmark == NULL) {
1068 if ((c == '>' || c == '<')
1071 && (*out == '\0' || is_digit(*out))) {
1073 return lasttoken = TREDIR;
1079 backquotelist = bqlist;
1080 grabstackblock(len);
1082 return lasttoken = TWORD;
1083 /* end of readtoken routine */
1088 * Check to see whether we are at the end of the here document. When this
1089 * is called, c is set to the first character of the next input line. If
1090 * we are at the end of the here document, this routine sets the c to PEOF.
1099 if (c == *eofmark) {
1100 if (pfgets(line, sizeof line) != NULL) {
1104 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1105 if (*p == '\n' && *q == '\0') {
1108 needprompt = doprompt;
1110 pushstring(line, strlen(line), NULL);
1115 goto checkend_return;
1120 * Parse a redirection operator. The variable "out" points to a string
1121 * specifying the fd to be redirected. The variable "c" contains the
1122 * first character of the redirection operator.
1129 np = (union node *)stalloc(sizeof (struct nfile));
1138 np->type = NCLOBBER;
1143 } else { /* c == '<' */
1147 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1148 np = (union node *)stalloc(sizeof (struct nhere));
1152 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1154 if ((c = pgetc()) == '-') {
1155 heredoc->striptabs = 1;
1157 heredoc->striptabs = 0;
1160 } else if (c == '&')
1170 np->nfile.fd = digit_val(fd);
1172 goto parseredir_return;
1177 * Parse a substitution. At this point, we have read the dollar sign
1187 static const char types[] = "}-+?=";
1188 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1194 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1198 } else if (c == '(') { /* $(command) or $((arith)) */
1199 if (pgetc() == '(') {
1206 USTPUTC(CTLVAR, out);
1207 typeloc = out - stackblock();
1208 USTPUTC(VSNORMAL, out);
1215 if ((c = pgetc()) == '}')
1223 if (!is_eof(c) && is_name(c)) {
1229 } while (!is_eof(c) && is_in_name(c));
1231 strncmp(out - length, "LINENO", length) == 0) {
1232 /* Replace the variable name with the
1233 * current line number. */
1236 linno -= funclinno - 1;
1237 snprintf(buf, sizeof(buf), "%d", linno);
1239 for (i = 0; buf[i] != '\0'; i++)
1240 STPUTC(buf[i], out);
1243 } else if (is_digit(c)) {
1244 if (bracketed_name) {
1248 } while (is_digit(c));
1254 if (! is_special(c)) {
1272 p = strchr(types, c);
1279 subtype = p - types + VSNORMAL;
1285 subtype = c == '#' ? VSTRIMLEFT :
1295 } else if (subtype != VSERROR) {
1299 if (subtype != VSLENGTH && (dblquote || arinest))
1301 *(stackblock() + typeloc) = subtype | flags;
1302 if (subtype != VSNORMAL)
1305 goto parsesub_return;
1310 * Called to parse command substitutions. Newstyle is set if the command
1311 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1312 * list of commands (passed by reference), and savelen is the number of
1313 * characters on the top of the stack which must be preserved.
1317 struct nodelist **nlpp;
1321 struct jmploc jmploc;
1322 struct jmploc *volatile savehandler;
1326 /* Avoid longjmp clobbering */
1330 savepbq = parsebackquote;
1331 if (setjmp(jmploc.loc)) {
1335 handler = savehandler;
1336 longjmp(handler->loc, 1);
1340 savelen = out - stackblock();
1342 str = ckmalloc(savelen);
1343 memcpy(str, stackblock(), savelen);
1345 savehandler = handler;
1349 /* We must read until the closing backquote, giving special
1350 treatment to some slashes, and then push the string and
1351 reread it as input, interpreting it normally. */
1364 switch (c = pgetc()) {
1369 if ((c = pgetc()) == '\n') {
1376 * If eating a newline, avoid putting
1377 * the newline into the new character
1378 * stream (via the STPUTC after the
1383 if (c != '\\' && c != '`' && c != '$'
1384 && (!dblquote || c != '"'))
1390 needprompt = doprompt;
1394 startlinno = plinno;
1395 synerror("EOF in backquote substitution");
1405 savelen = out - stackblock();
1407 str = ckmalloc(savelen);
1408 memcpy(str, stackblock(), savelen);
1409 setinputstring(str, 1);
1414 nlpp = &(*nlpp)->next;
1415 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1416 (*nlpp)->next = NULL;
1417 parsebackquote = oldstyle;
1420 saveprompt = doprompt;
1427 doprompt = saveprompt;
1429 if (readtoken() != TRP)
1436 * Start reading from old file again, ignoring any pushed back
1437 * tokens left from the backquote parsing
1442 while (stackblocksize() <= savelen)
1446 memcpy(out, str, savelen);
1447 STADJUST(savelen, out);
1453 parsebackquote = savepbq;
1454 handler = savehandler;
1455 if (arinest || dblquote)
1456 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1458 USTPUTC(CTLBACKQ, out);
1460 goto parsebackq_oldreturn;
1462 goto parsebackq_newreturn;
1466 * Parse an arithmetic expansion (indicate start of one and set state)
1470 if (++arinest == 1) {
1471 prevsyntax = syntax;
1473 USTPUTC(CTLARI, out);
1480 * we collapse embedded arithmetic expansion to
1481 * parenthesis, which should be equivalent
1485 goto parsearith_return;
1488 } /* end of readtoken */
1500 * Returns true if the text contains nothing to expand (no dollar signs
1505 noexpand(char *text)
1511 while ((c = *p++) != '\0') {
1512 if ( c == CTLQUOTEMARK)
1516 else if (BASESYNTAX[(int)c] == CCTL)
1524 * Return true if the argument is a legal variable name (a letter or
1525 * underscore followed by zero or more letters, underscores, and digits).
1529 goodname(char *name)
1537 if (! is_in_name(*p))
1545 * Called when an unexpected token is read during the parse. The argument
1546 * is the token that is expected, or -1 if more than one type of token can
1547 * occur at this point.
1551 synexpect(int token)
1556 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1557 tokname[lasttoken], tokname[token]);
1559 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1569 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1570 outfmt(&errout, "Syntax error: %s\n", msg);
1571 error((char *)NULL);
1575 setprompt(int which)
1577 whichprompt = which;
1582 out2str(getprompt(NULL));
1586 * called by editline -- any expansions to the prompt
1587 * should be added here.
1590 getprompt(void *unused __unused)
1592 static char ps[PROMPTLEN];
1597 * Select prompt format.
1599 switch (whichprompt) {
1610 return "<internal prompt error>";
1614 * Format prompt string.
1616 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1623 * \h specifies just the local hostname,
1624 * \H specifies fully-qualified hostname.
1629 gethostname(&ps[i], PROMPTLEN - i);
1630 /* Skip to end of hostname. */
1631 trim = (*fmt == 'h') ? '.' : '\0';
1632 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1637 * Working directory.
1639 * \W specifies just the final component,
1640 * \w specifies the entire path.
1645 getcwd(&ps[i], PROMPTLEN - i);
1647 /* Final path component only. */
1649 for (j = i; ps[j] != '\0'; j++)
1652 memmove(&ps[i], &ps[trim],
1655 /* Skip to end of path. */
1656 while (ps[i + 1] != '\0')
1663 * '$' for normal users, '#' for root.
1666 ps[i] = (geteuid() != 0) ? '$' : '#';
1677 * Emit unrecognized formats verbatim.