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 */
103 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
104 static int noaliases = 0;
107 STATIC union node *list(int);
108 STATIC union node *andor(void);
109 STATIC union node *pipeline(void);
110 STATIC union node *command(void);
111 STATIC union node *simplecmd(union node **, union node *);
112 STATIC union node *makename(void);
113 STATIC void parsefname(void);
114 STATIC void parseheredoc(void);
115 STATIC int peektoken(void);
116 STATIC int readtoken(void);
117 STATIC int xxreadtoken(void);
118 STATIC int readtoken1(int, char const *, char *, int);
119 STATIC int noexpand(char *);
120 STATIC void synexpect(int);
121 STATIC void synerror(const char *);
122 STATIC void setprompt(int);
126 * Read and parse a command. Returns NEOF on end of file. (NULL is a
127 * valid parse tree indicating a blank line.)
131 parsecmd(int interact)
155 union node *n1, *n2, *n3;
159 if (nlflag == 0 && tokendlist[peektoken()])
165 if (tok == TBACKGND) {
166 if (n2->type == NCMD || n2->type == NPIPE) {
167 n2->ncmd.backgnd = 1;
168 } else if (n2->type == NREDIR) {
171 n3 = (union node *)stalloc(sizeof (struct nredir));
174 n3->nredir.redirect = NULL;
182 n3 = (union node *)stalloc(sizeof (struct nbinary));
184 n3->nbinary.ch1 = n1;
185 n3->nbinary.ch2 = n2;
202 if (tokendlist[peektoken()])
209 pungetc(); /* push back EOF on input */
225 union node *n1, *n2, *n3;
230 if ((t = readtoken()) == TAND) {
232 } else if (t == TOR) {
239 n3 = (union node *)stalloc(sizeof (struct nbinary));
241 n3->nbinary.ch1 = n1;
242 n3->nbinary.ch2 = n2;
252 union node *n1, *n2, *pipenode;
253 struct nodelist *lp, *prev;
257 TRACE(("pipeline: entered\n"));
260 while (readtoken() == TNOT)
265 if (readtoken() == TPIPE) {
266 pipenode = (union node *)stalloc(sizeof (struct npipe));
267 pipenode->type = NPIPE;
268 pipenode->npipe.backgnd = 0;
269 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
270 pipenode->npipe.cmdlist = lp;
276 while (readtoken() == TNOT)
277 innernegate = !innernegate;
281 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
286 n2 = (union node *)stalloc(sizeof (struct nnot));
288 n2->nnot.com = lp->n;
291 } while (readtoken() == TPIPE);
297 n2 = (union node *)stalloc(sizeof (struct nnot));
311 union node *ap, **app;
312 union node *cp, **cpp;
313 union node *redir, **rpp;
321 /* Check for redirection which may precede command */
322 while (readtoken() == TREDIR) {
323 *rpp = n2 = redirnode;
324 rpp = &n2->nfile.next;
329 switch (readtoken()) {
331 n1 = (union node *)stalloc(sizeof (struct nif));
333 if ((n1->nif.test = list(0)) == NULL)
335 if (readtoken() != TTHEN)
337 n1->nif.ifpart = list(0);
339 while (readtoken() == TELIF) {
340 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
341 n2 = n2->nif.elsepart;
343 if ((n2->nif.test = list(0)) == NULL)
345 if (readtoken() != TTHEN)
347 n2->nif.ifpart = list(0);
349 if (lasttoken == TELSE)
350 n2->nif.elsepart = list(0);
352 n2->nif.elsepart = NULL;
355 if (readtoken() != TFI)
362 n1 = (union node *)stalloc(sizeof (struct nbinary));
363 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
364 if ((n1->nbinary.ch1 = list(0)) == NULL)
366 if ((got=readtoken()) != TDO) {
367 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
370 n1->nbinary.ch2 = list(0);
371 if (readtoken() != TDONE)
377 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
378 synerror("Bad for loop variable");
379 n1 = (union node *)stalloc(sizeof (struct nfor));
381 n1->nfor.var = wordtext;
382 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
384 while (readtoken() == TWORD) {
385 n2 = (union node *)stalloc(sizeof (struct narg));
387 n2->narg.text = wordtext;
388 n2->narg.backquote = backquotelist;
390 app = &n2->narg.next;
394 if (lasttoken != TNL && lasttoken != TSEMI)
397 static char argvars[5] = {
398 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
400 n2 = (union node *)stalloc(sizeof (struct narg));
402 n2->narg.text = argvars;
403 n2->narg.backquote = NULL;
404 n2->narg.next = NULL;
407 * Newline or semicolon here is optional (but note
408 * that the original Bourne shell only allowed NL).
410 if (lasttoken != TNL && lasttoken != TSEMI)
414 if ((t = readtoken()) == TDO)
416 else if (t == TBEGIN)
420 n1->nfor.body = list(0);
421 if (readtoken() != t)
426 n1 = (union node *)stalloc(sizeof (struct ncase));
428 if (readtoken() != TWORD)
430 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
432 n2->narg.text = wordtext;
433 n2->narg.backquote = backquotelist;
434 n2->narg.next = NULL;
435 while (readtoken() == TNL);
436 if (lasttoken != TWORD || ! equal(wordtext, "in"))
437 synerror("expecting \"in\"");
438 cpp = &n1->ncase.cases;
439 noaliases = 1; /* turn off alias expansion */
440 checkkwd = 2, readtoken();
441 while (lasttoken != TESAC) {
442 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
444 app = &cp->nclist.pattern;
445 if (lasttoken == TLP)
448 *app = ap = (union node *)stalloc(sizeof (struct narg));
450 ap->narg.text = wordtext;
451 ap->narg.backquote = backquotelist;
452 if (checkkwd = 2, readtoken() != TPIPE)
454 app = &ap->narg.next;
457 ap->narg.next = NULL;
458 if (lasttoken != TRP)
459 noaliases = 0, synexpect(TRP);
460 cp->nclist.body = list(0);
463 if ((t = readtoken()) != TESAC) {
465 noaliases = 0, synexpect(TENDCASE);
467 checkkwd = 2, readtoken();
469 cpp = &cp->nclist.next;
471 noaliases = 0; /* reset alias expansion */
476 n1 = (union node *)stalloc(sizeof (struct nredir));
477 n1->type = NSUBSHELL;
478 n1->nredir.n = list(0);
479 n1->nredir.redirect = NULL;
480 if (readtoken() != TRP)
486 if (readtoken() != TEND)
490 /* Handle an empty command like other simple commands. */
495 * An empty command before a ; doesn't make much sense, and
496 * should certainly be disallowed in the case of `if ;'.
505 return simplecmd(rpp, redir);
510 /* Now check for redirection which may follow command */
511 while (readtoken() == TREDIR) {
512 *rpp = n2 = redirnode;
513 rpp = &n2->nfile.next;
519 if (n1->type != NSUBSHELL) {
520 n2 = (union node *)stalloc(sizeof (struct nredir));
525 n1->nredir.redirect = redir;
532 simplecmd(union node **rpp, union node *redir)
534 union node *args, **app;
535 union node **orig_rpp = rpp;
536 union node *n = NULL;
538 /* If we don't have any redirections already, then we must reset */
539 /* rpp to be the address of the local redir variable. */
546 * We save the incoming value, because we need this for shell
547 * functions. There can not be a redirect or an argument between
548 * the function name and the open parenthesis.
553 if (readtoken() == TWORD) {
554 n = (union node *)stalloc(sizeof (struct narg));
556 n->narg.text = wordtext;
557 n->narg.backquote = backquotelist;
560 } else if (lasttoken == TREDIR) {
561 *rpp = n = redirnode;
562 rpp = &n->nfile.next;
563 parsefname(); /* read name of redirection file */
564 } else if (lasttoken == TLP && app == &args->narg.next
565 && rpp == orig_rpp) {
566 /* We have a function */
567 if (readtoken() != TRP)
570 if (! goodname(n->narg.text))
571 synerror("Bad function name");
574 n->narg.next = command();
583 n = (union node *)stalloc(sizeof (struct ncmd));
587 n->ncmd.redirect = redir;
596 n = (union node *)stalloc(sizeof (struct narg));
599 n->narg.text = wordtext;
600 n->narg.backquote = backquotelist;
605 fixredir(union node *n, const char *text, int err)
607 TRACE(("Fix redir %s %d\n", text, err));
609 n->ndup.vname = NULL;
611 if (is_digit(text[0]) && text[1] == '\0')
612 n->ndup.dupfd = digit_val(text[0]);
613 else if (text[0] == '-' && text[1] == '\0')
618 synerror("Bad fd number");
620 n->ndup.vname = makename();
628 union node *n = redirnode;
630 if (readtoken() != TWORD)
632 if (n->type == NHERE) {
633 struct heredoc *here = heredoc;
639 TRACE(("Here document %d\n", n->type));
640 if (here->striptabs) {
641 while (*wordtext == '\t')
644 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
645 synerror("Illegal eof marker for << redirection");
647 here->eofmark = wordtext;
649 if (heredoclist == NULL)
652 for (p = heredoclist ; p->next ; p = p->next);
655 } else if (n->type == NTOFD || n->type == NFROMFD) {
656 fixredir(n, wordtext, 0);
658 n->nfile.fname = makename();
664 * Input any here documents.
670 struct heredoc *here;
673 while (heredoclist) {
675 heredoclist = here->next;
680 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
681 here->eofmark, here->striptabs);
682 n = (union node *)stalloc(sizeof (struct narg));
685 n->narg.text = wordtext;
686 n->narg.backquote = backquotelist;
687 here->here->nhere.doc = n;
705 int savecheckkwd = checkkwd;
708 int alreadyseen = tokpushback;
727 * check for keywords and aliases
729 if (t == TWORD && !quoteflag)
731 const char * const *pp;
733 for (pp = parsekwd; *pp; pp++) {
734 if (**pp == *wordtext && equal(*pp, wordtext))
736 lasttoken = t = pp - parsekwd + KWDOFFSET;
737 TRACE(("keyword %s recognized\n", tokname[t]));
741 if (noaliases == 0 &&
742 (ap = lookupalias(wordtext, 1)) != NULL) {
743 pushstring(ap->val, strlen(ap->val), ap);
744 checkkwd = savecheckkwd;
753 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
755 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
762 * Read the next input token.
763 * If the token is a word, we set backquotelist to the list of cmds in
764 * backquotes. We set quoteflag to true if any part of the word was
766 * If the token is TREDIR, then we set redirnode to a structure containing
768 * In all cases, the variable startlinno is set to the number of the line
769 * on which the token starts.
771 * [Change comment: here documents and internal procedures]
772 * [Readtoken shouldn't have any arguments. Perhaps we should make the
773 * word parsing code into a separate routine. In this case, readtoken
774 * doesn't need to have any internal procedures, but parseword does.
775 * We could also make parseoperator in essence the main routine, and
776 * have parseword (readtoken1?) handle both words and redirection.]
779 #define RETURN(token) return lasttoken = token
795 for (;;) { /* until token or start of word found */
797 if (c == ' ' || c == '\t')
798 continue; /* quick check for white space first */
803 while ((c = pgetc()) != '\n' && c != PEOF);
807 if (pgetc() == '\n') {
808 startlinno = ++plinno;
819 needprompt = doprompt;
845 /* else FALLTHROUGH */
851 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
858 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
859 * is not NULL, read a here document. In the latter case, eofmark is the
860 * word which marks the end of the document and striptabs is true if
861 * leading tabs should be stripped from the document. The argument firstc
862 * is the first character of the input token or document.
864 * Because C does not have internal subroutines, I have simulated them
865 * using goto's to implement the subroutine linkage. The following macros
866 * will run code that appears at the end of readtoken1.
869 #define CHECKEND() {goto checkend; checkend_return:;}
870 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
871 #define PARSESUB() {goto parsesub; parsesub_return:;}
872 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
873 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
874 #define PARSEARITH() {goto parsearith; parsearith_return:;}
877 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
880 char const * volatile syntax = syn;
883 char line[EOFMARKLEN + 1];
884 struct nodelist *bqlist;
886 volatile int dblquote;
887 volatile int varnest; /* levels of variables expansion */
888 volatile int arinest; /* levels of arithmetic expansion */
889 volatile int parenlevel; /* levels of parens in arithmetic */
890 volatile int oldstyle;
891 char const * volatile prevsyntax = NULL; /* syntax before arithmetic */
896 if (syntax == DQSYNTAX)
905 loop: { /* for each line, until end of word */
906 CHECKEND(); /* set c to PEOF if at end of here document */
907 for (;;) { /* until end of line or end of word */
908 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
910 synentry = syntax[c];
914 if (syntax == BASESYNTAX)
915 goto endword; /* exit outer loop */
923 goto loop; /* continue outer loop */
928 if (eofmark == NULL || dblquote)
929 USTPUTC(CTLESC, out);
932 case CBACK: /* backslash */
937 } else if (c == '\n') {
944 if (dblquote && c != '\\' &&
945 c != '`' && c != '$' &&
946 (c != '"' || eofmark != NULL))
948 if (SQSYNTAX[c] == CCTL)
949 USTPUTC(CTLESC, out);
950 else if (eofmark == NULL)
951 USTPUTC(CTLQUOTEMARK, out);
958 USTPUTC(CTLQUOTEMARK, out);
963 USTPUTC(CTLQUOTEMARK, out);
968 if (eofmark != NULL && arinest == 0 &&
975 } else if (eofmark == NULL) {
983 PARSESUB(); /* parse substitution */
985 case CENDVAR: /* '}' */
988 USTPUTC(CTLENDVAR, out);
993 case CLP: /* '(' in arithmetic */
997 case CRP: /* ')' in arithmetic */
998 if (parenlevel > 0) {
1002 if (pgetc() == ')') {
1003 if (--arinest == 0) {
1004 USTPUTC(CTLENDARI, out);
1005 syntax = prevsyntax;
1006 if (syntax == DQSYNTAX)
1015 * (don't 2nd guess - no error)
1022 case CBQUOTE: /* '`' */
1026 goto endword; /* exit outer loop */
1029 goto endword; /* exit outer loop */
1036 if (syntax == ARISYNTAX)
1037 synerror("Missing '))'");
1038 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1039 synerror("Unterminated quoted string");
1041 startlinno = plinno;
1042 synerror("Missing '}'");
1045 len = out - stackblock();
1047 if (eofmark == NULL) {
1048 if ((c == '>' || c == '<')
1051 && (*out == '\0' || is_digit(*out))) {
1053 return lasttoken = TREDIR;
1059 backquotelist = bqlist;
1060 grabstackblock(len);
1062 return lasttoken = TWORD;
1063 /* end of readtoken routine */
1068 * Check to see whether we are at the end of the here document. When this
1069 * is called, c is set to the first character of the next input line. If
1070 * we are at the end of the here document, this routine sets the c to PEOF.
1079 if (c == *eofmark) {
1080 if (pfgets(line, sizeof line) != NULL) {
1084 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1085 if (*p == '\n' && *q == '\0') {
1088 needprompt = doprompt;
1090 pushstring(line, strlen(line), NULL);
1095 goto checkend_return;
1100 * Parse a redirection operator. The variable "out" points to a string
1101 * specifying the fd to be redirected. The variable "c" contains the
1102 * first character of the redirection operator.
1109 np = (union node *)stalloc(sizeof (struct nfile));
1118 np->type = NCLOBBER;
1123 } else { /* c == '<' */
1127 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1128 np = (union node *)stalloc(sizeof (struct nhere));
1132 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1134 if ((c = pgetc()) == '-') {
1135 heredoc->striptabs = 1;
1137 heredoc->striptabs = 0;
1140 } else if (c == '&')
1150 np->nfile.fd = digit_val(fd);
1152 goto parseredir_return;
1157 * Parse a substitution. At this point, we have read the dollar sign
1166 static const char types[] = "}-+?=";
1167 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1170 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1174 } else if (c == '(') { /* $(command) or $((arith)) */
1175 if (pgetc() == '(') {
1182 USTPUTC(CTLVAR, out);
1183 typeloc = out - stackblock();
1184 USTPUTC(VSNORMAL, out);
1190 if ((c = pgetc()) == '}')
1198 if (!is_eof(c) && is_name(c)) {
1202 } while (!is_eof(c) && is_in_name(c));
1203 } else if (is_digit(c)) {
1204 if (bracketed_name) {
1208 } while (is_digit(c));
1214 if (! is_special(c)) {
1233 p = strchr(types, c);
1240 subtype = p - types + VSNORMAL;
1246 subtype = c == '#' ? VSTRIMLEFT :
1256 } else if (subtype != VSERROR) {
1260 if (subtype != VSLENGTH && (dblquote || arinest))
1262 *(stackblock() + typeloc) = subtype | flags;
1263 if (subtype != VSNORMAL)
1266 goto parsesub_return;
1271 * Called to parse command substitutions. Newstyle is set if the command
1272 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1273 * list of commands (passed by reference), and savelen is the number of
1274 * characters on the top of the stack which must be preserved.
1278 struct nodelist **nlpp;
1282 struct jmploc jmploc;
1283 struct jmploc *volatile savehandler;
1285 volatile int saveprompt;
1287 savepbq = parsebackquote;
1288 if (setjmp(jmploc.loc)) {
1292 handler = savehandler;
1293 longjmp(handler->loc, 1);
1297 savelen = out - stackblock();
1299 str = ckmalloc(savelen);
1300 memcpy(str, stackblock(), savelen);
1302 savehandler = handler;
1306 /* We must read until the closing backquote, giving special
1307 treatment to some slashes, and then push the string and
1308 reread it as input, interpreting it normally. */
1315 STARTSTACKSTR(pout);
1321 switch (pc = pgetc()) {
1326 if ((pc = pgetc()) == '\n') {
1333 * If eating a newline, avoid putting
1334 * the newline into the new character
1335 * stream (via the STPUTC after the
1340 if (pc != '\\' && pc != '`' && pc != '$'
1341 && (!dblquote || pc != '"'))
1347 needprompt = doprompt;
1351 startlinno = plinno;
1352 synerror("EOF in backquote substitution");
1362 psavelen = pout - stackblock();
1364 pstr = ckmalloc(psavelen);
1365 memcpy(pstr, stackblock(), psavelen);
1366 setinputstring(pstr, 1);
1371 nlpp = &(*nlpp)->next;
1372 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1373 (*nlpp)->next = NULL;
1374 parsebackquote = oldstyle;
1377 saveprompt = doprompt;
1384 doprompt = saveprompt;
1386 if (readtoken() != TRP)
1393 * Start reading from old file again, ignoring any pushed back
1394 * tokens left from the backquote parsing
1399 while (stackblocksize() <= savelen)
1403 memcpy(out, str, savelen);
1404 STADJUST(savelen, out);
1410 parsebackquote = savepbq;
1411 handler = savehandler;
1412 if (arinest || dblquote)
1413 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1415 USTPUTC(CTLBACKQ, out);
1417 goto parsebackq_oldreturn;
1419 goto parsebackq_newreturn;
1423 * Parse an arithmetic expansion (indicate start of one and set state)
1427 if (++arinest == 1) {
1428 prevsyntax = syntax;
1430 USTPUTC(CTLARI, out);
1437 * we collapse embedded arithmetic expansion to
1438 * parenthesis, which should be equivalent
1442 goto parsearith_return;
1445 } /* end of readtoken */
1457 * Returns true if the text contains nothing to expand (no dollar signs
1462 noexpand(char *text)
1468 while ((c = *p++) != '\0') {
1469 if ( c == CTLQUOTEMARK)
1473 else if (BASESYNTAX[(int)c] == CCTL)
1481 * Return true if the argument is a legal variable name (a letter or
1482 * underscore followed by zero or more letters, underscores, and digits).
1486 goodname(char *name)
1494 if (! is_in_name(*p))
1502 * Called when an unexpected token is read during the parse. The argument
1503 * is the token that is expected, or -1 if more than one type of token can
1504 * occur at this point.
1508 synexpect(int token)
1513 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1514 tokname[lasttoken], tokname[token]);
1516 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1523 synerror(const char *msg)
1526 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1527 outfmt(&errout, "Syntax error: %s\n", msg);
1528 error((char *)NULL);
1532 setprompt(int which)
1534 whichprompt = which;
1539 out2str(getprompt(NULL));
1543 * called by editline -- any expansions to the prompt
1544 * should be added here.
1547 getprompt(void *unused __unused)
1549 static char ps[PROMPTLEN];
1554 * Select prompt format.
1556 switch (whichprompt) {
1567 return "<internal prompt error>";
1571 * Format prompt string.
1573 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1580 * \h specifies just the local hostname,
1581 * \H specifies fully-qualified hostname.
1586 gethostname(&ps[i], PROMPTLEN - i);
1587 /* Skip to end of hostname. */
1588 trim = (*fmt == 'h') ? '.' : '\0';
1589 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1594 * Working directory.
1596 * \W specifies just the final component,
1597 * \w specifies the entire path.
1602 getcwd(&ps[i], PROMPTLEN - i);
1604 /* Final path component only. */
1606 for (j = i; ps[j] != '\0'; j++)
1609 memmove(&ps[i], &ps[trim],
1612 /* Skip to end of path. */
1613 while (ps[i + 1] != '\0')
1620 * '$' for normal users, '#' for root.
1623 ps[i] = (geteuid() != 0) ? '$' : '#';
1634 * Emit unrecognized formats verbatim.