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$");
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 */
92 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
93 static struct nodelist *backquotelist;
94 static union node *redirnode;
95 static struct heredoc *heredoc;
96 static int quoteflag; /* set if (part of) last token was quoted */
97 static int startlinno; /* line # where last token started */
98 static int funclinno; /* line # where the current function started */
100 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
101 static int noaliases = 0;
104 static union node *list(int);
105 static union node *andor(void);
106 static union node *pipeline(void);
107 static union node *command(void);
108 static union node *simplecmd(union node **, union node *);
109 static union node *makename(void);
110 static void parsefname(void);
111 static void parseheredoc(void);
112 static int peektoken(void);
113 static int readtoken(void);
114 static int xxreadtoken(void);
115 static int readtoken1(int, char const *, char *, int);
116 static int noexpand(char *);
117 static void synexpect(int);
118 static void synerror(const char *);
119 static void setprompt(int);
123 * Read and parse a command. Returns NEOF on end of file. (NULL is a
124 * valid parse tree indicating a blank line.)
128 parsecmd(int interact)
152 union node *n1, *n2, *n3;
156 if (nlflag == 0 && tokendlist[peektoken()])
162 if (tok == TBACKGND) {
163 if (n2->type == NCMD || n2->type == NPIPE) {
164 n2->ncmd.backgnd = 1;
165 } else if (n2->type == NREDIR) {
168 n3 = (union node *)stalloc(sizeof (struct nredir));
171 n3->nredir.redirect = NULL;
179 n3 = (union node *)stalloc(sizeof (struct nbinary));
181 n3->nbinary.ch1 = n1;
182 n3->nbinary.ch2 = n2;
199 if (tokendlist[peektoken()])
206 pungetc(); /* push back EOF on input */
222 union node *n1, *n2, *n3;
227 if ((t = readtoken()) == TAND) {
229 } else if (t == TOR) {
236 n3 = (union node *)stalloc(sizeof (struct nbinary));
238 n3->nbinary.ch1 = n1;
239 n3->nbinary.ch2 = n2;
249 union node *n1, *n2, *pipenode;
250 struct nodelist *lp, *prev;
255 TRACE(("pipeline: entered\n"));
256 while (readtoken() == TNOT)
260 if (readtoken() == TPIPE) {
261 pipenode = (union node *)stalloc(sizeof (struct npipe));
262 pipenode->type = NPIPE;
263 pipenode->npipe.backgnd = 0;
264 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
265 pipenode->npipe.cmdlist = lp;
269 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
272 } while (readtoken() == TPIPE);
278 n2 = (union node *)stalloc(sizeof (struct nnot));
292 union node *ap, **app;
293 union node *cp, **cpp;
294 union node *redir, **rpp;
302 /* Check for redirection which may precede command */
303 while (readtoken() == TREDIR) {
304 *rpp = n2 = redirnode;
305 rpp = &n2->nfile.next;
310 while (readtoken() == TNOT) {
311 TRACE(("command: TNOT recognized\n"));
316 switch (readtoken()) {
318 n1 = (union node *)stalloc(sizeof (struct nif));
320 if ((n1->nif.test = list(0)) == NULL)
322 if (readtoken() != TTHEN)
324 n1->nif.ifpart = list(0);
326 while (readtoken() == TELIF) {
327 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
328 n2 = n2->nif.elsepart;
330 if ((n2->nif.test = list(0)) == NULL)
332 if (readtoken() != TTHEN)
334 n2->nif.ifpart = list(0);
336 if (lasttoken == TELSE)
337 n2->nif.elsepart = list(0);
339 n2->nif.elsepart = NULL;
342 if (readtoken() != TFI)
349 n1 = (union node *)stalloc(sizeof (struct nbinary));
350 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
351 if ((n1->nbinary.ch1 = list(0)) == NULL)
353 if ((got=readtoken()) != TDO) {
354 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
357 n1->nbinary.ch2 = list(0);
358 if (readtoken() != TDONE)
364 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
365 synerror("Bad for loop variable");
366 n1 = (union node *)stalloc(sizeof (struct nfor));
368 n1->nfor.var = wordtext;
369 while (readtoken() == TNL)
371 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
373 while (readtoken() == TWORD) {
374 n2 = (union node *)stalloc(sizeof (struct narg));
376 n2->narg.text = wordtext;
377 n2->narg.backquote = backquotelist;
379 app = &n2->narg.next;
383 if (lasttoken != TNL && lasttoken != TSEMI)
386 static char argvars[5] = {
387 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
389 n2 = (union node *)stalloc(sizeof (struct narg));
391 n2->narg.text = argvars;
392 n2->narg.backquote = NULL;
393 n2->narg.next = NULL;
396 * Newline or semicolon here is optional (but note
397 * that the original Bourne shell only allowed NL).
399 if (lasttoken != TNL && lasttoken != TSEMI)
403 if ((t = readtoken()) == TDO)
405 else if (t == TBEGIN)
409 n1->nfor.body = list(0);
410 if (readtoken() != t)
415 n1 = (union node *)stalloc(sizeof (struct ncase));
417 if (readtoken() != TWORD)
419 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
421 n2->narg.text = wordtext;
422 n2->narg.backquote = backquotelist;
423 n2->narg.next = NULL;
424 while (readtoken() == TNL);
425 if (lasttoken != TWORD || ! equal(wordtext, "in"))
426 synerror("expecting \"in\"");
427 cpp = &n1->ncase.cases;
428 noaliases = 1; /* turn off alias expansion */
429 checkkwd = 2, readtoken();
430 while (lasttoken != TESAC) {
431 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
433 app = &cp->nclist.pattern;
434 if (lasttoken == TLP)
437 *app = ap = (union node *)stalloc(sizeof (struct narg));
439 ap->narg.text = wordtext;
440 ap->narg.backquote = backquotelist;
441 if (checkkwd = 2, readtoken() != TPIPE)
443 app = &ap->narg.next;
446 ap->narg.next = NULL;
447 if (lasttoken != TRP)
448 noaliases = 0, synexpect(TRP);
449 cp->nclist.body = list(0);
452 if ((t = readtoken()) != TESAC) {
454 noaliases = 0, synexpect(TENDCASE);
456 checkkwd = 2, readtoken();
458 cpp = &cp->nclist.next;
460 noaliases = 0; /* reset alias expansion */
465 n1 = (union node *)stalloc(sizeof (struct nredir));
466 n1->type = NSUBSHELL;
467 n1->nredir.n = list(0);
468 n1->nredir.redirect = NULL;
469 if (readtoken() != TRP)
475 if (readtoken() != TEND)
479 /* Handle an empty command like other simple commands. */
485 * An empty command before a ; doesn't make much sense, and
486 * should certainly be disallowed in the case of `if ;'.
495 n1 = simplecmd(rpp, redir);
501 /* Now check for redirection which may follow command */
502 while (readtoken() == TREDIR) {
503 *rpp = n2 = redirnode;
504 rpp = &n2->nfile.next;
510 if (n1->type != NSUBSHELL) {
511 n2 = (union node *)stalloc(sizeof (struct nredir));
516 n1->nredir.redirect = redir;
521 n2 = (union node *)stalloc(sizeof (struct nnot));
532 simplecmd(union node **rpp, union node *redir)
534 union node *args, **app;
535 union node **orig_rpp = rpp;
536 union node *n = NULL, *n2;
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.
553 while (readtoken() == TNOT) {
554 TRACE(("command: TNOT recognized\n"));
560 if (readtoken() == TWORD) {
561 n = (union node *)stalloc(sizeof (struct narg));
563 n->narg.text = wordtext;
564 n->narg.backquote = backquotelist;
567 } else if (lasttoken == TREDIR) {
568 *rpp = n = redirnode;
569 rpp = &n->nfile.next;
570 parsefname(); /* read name of redirection file */
571 } else if (lasttoken == TLP && app == &args->narg.next
572 && rpp == orig_rpp) {
573 /* We have a function */
574 if (readtoken() != TRP)
578 if (! goodname(n->narg.text))
579 synerror("Bad function name");
582 n->narg.next = command();
592 n = (union node *)stalloc(sizeof (struct ncmd));
596 n->ncmd.redirect = redir;
600 n2 = (union node *)stalloc(sizeof (struct nnot));
614 n = (union node *)stalloc(sizeof (struct narg));
617 n->narg.text = wordtext;
618 n->narg.backquote = backquotelist;
623 fixredir(union node *n, const char *text, int err)
625 TRACE(("Fix redir %s %d\n", text, err));
627 n->ndup.vname = NULL;
629 if (is_digit(text[0]) && text[1] == '\0')
630 n->ndup.dupfd = digit_val(text[0]);
631 else if (text[0] == '-' && text[1] == '\0')
636 synerror("Bad fd number");
638 n->ndup.vname = makename();
646 union node *n = redirnode;
648 if (readtoken() != TWORD)
650 if (n->type == NHERE) {
651 struct heredoc *here = heredoc;
657 TRACE(("Here document %d\n", n->type));
658 if (here->striptabs) {
659 while (*wordtext == '\t')
662 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
663 synerror("Illegal eof marker for << redirection");
665 here->eofmark = wordtext;
667 if (heredoclist == NULL)
670 for (p = heredoclist ; p->next ; p = p->next);
673 } else if (n->type == NTOFD || n->type == NFROMFD) {
674 fixredir(n, wordtext, 0);
676 n->nfile.fname = makename();
682 * Input any here documents.
688 struct heredoc *here;
691 while (heredoclist) {
693 heredoclist = here->next;
698 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
699 here->eofmark, here->striptabs);
700 n = (union node *)stalloc(sizeof (struct narg));
703 n->narg.text = wordtext;
704 n->narg.backquote = backquotelist;
705 here->here->nhere.doc = n;
723 int savecheckkwd = checkkwd;
726 int alreadyseen = tokpushback;
745 * check for keywords and aliases
747 if (t == TWORD && !quoteflag)
749 const char * const *pp;
751 for (pp = parsekwd; *pp; pp++) {
752 if (**pp == *wordtext && equal(*pp, wordtext))
754 lasttoken = t = pp - parsekwd + KWDOFFSET;
755 TRACE(("keyword %s recognized\n", tokname[t]));
759 if (noaliases == 0 &&
760 (ap = lookupalias(wordtext, 1)) != NULL) {
761 pushstring(ap->val, strlen(ap->val), ap);
762 checkkwd = savecheckkwd;
767 checkkwd = (t == TNOT) ? savecheckkwd : 0;
771 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
773 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
780 * Read the next input token.
781 * If the token is a word, we set backquotelist to the list of cmds in
782 * backquotes. We set quoteflag to true if any part of the word was
784 * If the token is TREDIR, then we set redirnode to a structure containing
786 * In all cases, the variable startlinno is set to the number of the line
787 * on which the token starts.
789 * [Change comment: here documents and internal procedures]
790 * [Readtoken shouldn't have any arguments. Perhaps we should make the
791 * word parsing code into a separate routine. In this case, readtoken
792 * doesn't need to have any internal procedures, but parseword does.
793 * We could also make parseoperator in essence the main routine, and
794 * have parseword (readtoken1?) handle both words and redirection.]
797 #define RETURN(token) return lasttoken = token
813 for (;;) { /* until token or start of word found */
815 if (c == ' ' || c == '\t')
816 continue; /* quick check for white space first */
821 while ((c = pgetc()) != '\n' && c != PEOF);
825 if (pgetc() == '\n') {
826 startlinno = ++plinno;
837 needprompt = doprompt;
865 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
872 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
873 * is not NULL, read a here document. In the latter case, eofmark is the
874 * word which marks the end of the document and striptabs is true if
875 * leading tabs should be stripped from the document. The argument firstc
876 * is the first character of the input token or document.
878 * Because C does not have internal subroutines, I have simulated them
879 * using goto's to implement the subroutine linkage. The following macros
880 * will run code that appears at the end of readtoken1.
883 #define CHECKEND() {goto checkend; checkend_return:;}
884 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
885 #define PARSESUB() {goto parsesub; parsesub_return:;}
886 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
887 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
888 #define PARSEARITH() {goto parsearith; parsearith_return:;}
891 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
896 char line[EOFMARKLEN + 1];
897 struct nodelist *bqlist;
900 int varnest; /* levels of variables expansion */
901 int arinest; /* levels of arithmetic expansion */
902 int parenlevel; /* levels of parens in arithmetic */
904 char const *prevsyntax; /* syntax before arithmetic */
909 if (syntax == DQSYNTAX)
918 loop: { /* for each line, until end of word */
919 CHECKEND(); /* set c to PEOF if at end of here document */
920 for (;;) { /* until end of line or end of word */
921 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
923 synentry = syntax[c];
927 if (syntax == BASESYNTAX)
928 goto endword; /* exit outer loop */
936 goto loop; /* continue outer loop */
941 if (eofmark == NULL || dblquote)
942 USTPUTC(CTLESC, out);
945 case CBACK: /* backslash */
950 } else if (c == '\n') {
957 if (dblquote && c != '\\' &&
958 c != '`' && c != '$' &&
959 (c != '"' || eofmark != NULL))
961 if (SQSYNTAX[c] == CCTL)
962 USTPUTC(CTLESC, out);
963 else if (eofmark == NULL)
964 USTPUTC(CTLQUOTEMARK, out);
971 USTPUTC(CTLQUOTEMARK, out);
976 USTPUTC(CTLQUOTEMARK, out);
981 if (eofmark != NULL && arinest == 0 &&
988 } else if (eofmark == NULL) {
996 PARSESUB(); /* parse substitution */
998 case CENDVAR: /* '}' */
1001 USTPUTC(CTLENDVAR, out);
1006 case CLP: /* '(' in arithmetic */
1010 case CRP: /* ')' in arithmetic */
1011 if (parenlevel > 0) {
1015 if (pgetc() == ')') {
1016 if (--arinest == 0) {
1017 USTPUTC(CTLENDARI, out);
1018 syntax = prevsyntax;
1019 if (syntax == DQSYNTAX)
1028 * (don't 2nd guess - no error)
1035 case CBQUOTE: /* '`' */
1039 goto endword; /* exit outer loop */
1042 goto endword; /* exit outer loop */
1049 if (syntax == ARISYNTAX)
1050 synerror("Missing '))'");
1051 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1052 synerror("Unterminated quoted string");
1054 startlinno = plinno;
1055 synerror("Missing '}'");
1058 len = out - stackblock();
1060 if (eofmark == NULL) {
1061 if ((c == '>' || c == '<')
1064 && (*out == '\0' || is_digit(*out))) {
1066 return lasttoken = TREDIR;
1072 backquotelist = bqlist;
1073 grabstackblock(len);
1075 return lasttoken = TWORD;
1076 /* end of readtoken routine */
1081 * Check to see whether we are at the end of the here document. When this
1082 * is called, c is set to the first character of the next input line. If
1083 * we are at the end of the here document, this routine sets the c to PEOF.
1092 if (c == *eofmark) {
1093 if (pfgets(line, sizeof line) != NULL) {
1097 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1098 if (*p == '\n' && *q == '\0') {
1101 needprompt = doprompt;
1103 pushstring(line, strlen(line), NULL);
1108 goto checkend_return;
1113 * Parse a redirection operator. The variable "out" points to a string
1114 * specifying the fd to be redirected. The variable "c" contains the
1115 * first character of the redirection operator.
1122 np = (union node *)stalloc(sizeof (struct nfile));
1131 np->type = NCLOBBER;
1136 } else { /* c == '<' */
1140 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1141 np = (union node *)stalloc(sizeof (struct nhere));
1145 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1147 if ((c = pgetc()) == '-') {
1148 heredoc->striptabs = 1;
1150 heredoc->striptabs = 0;
1153 } else if (c == '&')
1163 np->nfile.fd = digit_val(fd);
1165 goto parseredir_return;
1170 * Parse a substitution. At this point, we have read the dollar sign
1180 static const char types[] = "}-+?=";
1181 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1187 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1191 } else if (c == '(') { /* $(command) or $((arith)) */
1192 if (pgetc() == '(') {
1199 USTPUTC(CTLVAR, out);
1200 typeloc = out - stackblock();
1201 USTPUTC(VSNORMAL, out);
1208 if ((c = pgetc()) == '}')
1216 if (!is_eof(c) && is_name(c)) {
1222 } while (!is_eof(c) && is_in_name(c));
1224 strncmp(out - length, "LINENO", length) == 0) {
1225 /* Replace the variable name with the
1226 * current line number. */
1229 linno -= funclinno - 1;
1230 snprintf(buf, sizeof(buf), "%d", linno);
1232 for (i = 0; buf[i] != '\0'; i++)
1233 STPUTC(buf[i], out);
1236 } else if (is_digit(c)) {
1237 if (bracketed_name) {
1241 } while (is_digit(c));
1247 if (! is_special(c)) {
1265 p = strchr(types, c);
1272 subtype = p - types + VSNORMAL;
1278 subtype = c == '#' ? VSTRIMLEFT :
1288 } else if (subtype != VSERROR) {
1292 if (subtype != VSLENGTH && (dblquote || arinest))
1294 *(stackblock() + typeloc) = subtype | flags;
1295 if (subtype != VSNORMAL)
1298 goto parsesub_return;
1303 * Called to parse command substitutions. Newstyle is set if the command
1304 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1305 * list of commands (passed by reference), and savelen is the number of
1306 * characters on the top of the stack which must be preserved.
1310 struct nodelist **nlpp;
1314 struct jmploc jmploc;
1315 struct jmploc *const savehandler = handler;
1318 const int bq_startlinno = plinno;
1320 savepbq = parsebackquote;
1321 if (setjmp(jmploc.loc)) {
1325 handler = savehandler;
1326 if (exception == EXERROR) {
1327 startlinno = bq_startlinno;
1328 synerror("Error in command substitution");
1330 longjmp(handler->loc, 1);
1334 savelen = out - stackblock();
1336 str = ckmalloc(savelen);
1337 memcpy(str, stackblock(), savelen);
1342 /* We must read until the closing backquote, giving special
1343 treatment to some slashes, and then push the string and
1344 reread it as input, interpreting it normally. */
1357 switch (c = pgetc()) {
1362 if ((c = pgetc()) == '\n') {
1369 * If eating a newline, avoid putting
1370 * the newline into the new character
1371 * stream (via the STPUTC after the
1376 if (c != '\\' && c != '`' && c != '$'
1377 && (!dblquote || c != '"'))
1383 needprompt = doprompt;
1387 startlinno = plinno;
1388 synerror("EOF in backquote substitution");
1398 savelen = out - stackblock();
1400 str = ckmalloc(savelen);
1401 memcpy(str, stackblock(), savelen);
1402 setinputstring(str, 1);
1407 nlpp = &(*nlpp)->next;
1408 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1409 (*nlpp)->next = NULL;
1410 parsebackquote = oldstyle;
1413 saveprompt = doprompt;
1420 doprompt = saveprompt;
1422 if (readtoken() != TRP)
1429 * Start reading from old file again, ignoring any pushed back
1430 * tokens left from the backquote parsing
1435 while (stackblocksize() <= savelen)
1439 memcpy(out, str, savelen);
1440 STADJUST(savelen, out);
1446 parsebackquote = savepbq;
1447 handler = savehandler;
1448 if (arinest || dblquote)
1449 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1451 USTPUTC(CTLBACKQ, out);
1453 goto parsebackq_oldreturn;
1455 goto parsebackq_newreturn;
1459 * Parse an arithmetic expansion (indicate start of one and set state)
1463 if (++arinest == 1) {
1464 prevsyntax = syntax;
1466 USTPUTC(CTLARI, out);
1473 * we collapse embedded arithmetic expansion to
1474 * parenthesis, which should be equivalent
1478 goto parsearith_return;
1481 } /* end of readtoken */
1493 * Returns true if the text contains nothing to expand (no dollar signs
1498 noexpand(char *text)
1504 while ((c = *p++) != '\0') {
1505 if ( c == CTLQUOTEMARK)
1509 else if (BASESYNTAX[(int)c] == CCTL)
1517 * Return true if the argument is a legal variable name (a letter or
1518 * underscore followed by zero or more letters, underscores, and digits).
1522 goodname(const char *name)
1530 if (! is_in_name(*p))
1538 * Called when an unexpected token is read during the parse. The argument
1539 * is the token that is expected, or -1 if more than one type of token can
1540 * occur at this point.
1544 synexpect(int token)
1549 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1550 tokname[lasttoken], tokname[token]);
1552 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1559 synerror(const char *msg)
1562 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1563 outfmt(&errout, "Syntax error: %s\n", msg);
1564 error((char *)NULL);
1568 setprompt(int which)
1570 whichprompt = which;
1575 out2str(getprompt(NULL));
1579 * called by editline -- any expansions to the prompt
1580 * should be added here.
1583 getprompt(void *unused __unused)
1585 static char ps[PROMPTLEN];
1589 static char internal_error[] = "<internal prompt error>";
1592 * Select prompt format.
1594 switch (whichprompt) {
1605 return internal_error;
1609 * Format prompt string.
1611 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1618 * \h specifies just the local hostname,
1619 * \H specifies fully-qualified hostname.
1624 gethostname(&ps[i], PROMPTLEN - i);
1625 /* Skip to end of hostname. */
1626 trim = (*fmt == 'h') ? '.' : '\0';
1627 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1632 * Working directory.
1634 * \W specifies just the final component,
1635 * \w specifies the entire path.
1639 pwd = lookupvar("PWD");
1643 *pwd == '/' && pwd[1] != '\0')
1644 strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1647 strlcpy(&ps[i], pwd, PROMPTLEN - i);
1648 /* Skip to end of path. */
1649 while (ps[i + 1] != '\0')
1656 * '$' for normal users, '#' for root.
1659 ps[i] = (geteuid() != 0) ? '$' : '#';
1670 * Emit unrecognized formats verbatim.