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;
254 TRACE(("pipeline: entered\n"));
255 while (readtoken() == TNOT)
259 if (readtoken() == TPIPE) {
260 pipenode = (union node *)stalloc(sizeof (struct npipe));
261 pipenode->type = NPIPE;
262 pipenode->npipe.backgnd = 0;
263 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
264 pipenode->npipe.cmdlist = lp;
268 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
271 } while (readtoken() == TPIPE);
277 n2 = (union node *)stalloc(sizeof (struct nnot));
291 union node *ap, **app;
292 union node *cp, **cpp;
293 union node *redir, **rpp;
301 /* Check for redirection which may precede command */
302 while (readtoken() == TREDIR) {
303 *rpp = n2 = redirnode;
304 rpp = &n2->nfile.next;
309 while (readtoken() == TNOT) {
310 TRACE(("command: TNOT recognized\n"));
315 switch (readtoken()) {
317 n1 = (union node *)stalloc(sizeof (struct nif));
319 if ((n1->nif.test = list(0)) == NULL)
321 if (readtoken() != TTHEN)
323 n1->nif.ifpart = list(0);
325 while (readtoken() == TELIF) {
326 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
327 n2 = n2->nif.elsepart;
329 if ((n2->nif.test = list(0)) == NULL)
331 if (readtoken() != TTHEN)
333 n2->nif.ifpart = list(0);
335 if (lasttoken == TELSE)
336 n2->nif.elsepart = list(0);
338 n2->nif.elsepart = NULL;
341 if (readtoken() != TFI)
348 n1 = (union node *)stalloc(sizeof (struct nbinary));
349 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
350 if ((n1->nbinary.ch1 = list(0)) == NULL)
352 if ((got=readtoken()) != TDO) {
353 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
356 n1->nbinary.ch2 = list(0);
357 if (readtoken() != TDONE)
363 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
364 synerror("Bad for loop variable");
365 n1 = (union node *)stalloc(sizeof (struct nfor));
367 n1->nfor.var = wordtext;
368 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
370 while (readtoken() == TWORD) {
371 n2 = (union node *)stalloc(sizeof (struct narg));
373 n2->narg.text = wordtext;
374 n2->narg.backquote = backquotelist;
376 app = &n2->narg.next;
380 if (lasttoken != TNL && lasttoken != TSEMI)
383 static char argvars[5] = {
384 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
386 n2 = (union node *)stalloc(sizeof (struct narg));
388 n2->narg.text = argvars;
389 n2->narg.backquote = NULL;
390 n2->narg.next = NULL;
393 * Newline or semicolon here is optional (but note
394 * that the original Bourne shell only allowed NL).
396 if (lasttoken != TNL && lasttoken != TSEMI)
400 if ((t = readtoken()) == TDO)
402 else if (t == TBEGIN)
406 n1->nfor.body = list(0);
407 if (readtoken() != t)
412 n1 = (union node *)stalloc(sizeof (struct ncase));
414 if (readtoken() != TWORD)
416 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
418 n2->narg.text = wordtext;
419 n2->narg.backquote = backquotelist;
420 n2->narg.next = NULL;
421 while (readtoken() == TNL);
422 if (lasttoken != TWORD || ! equal(wordtext, "in"))
423 synerror("expecting \"in\"");
424 cpp = &n1->ncase.cases;
425 noaliases = 1; /* turn off alias expansion */
426 checkkwd = 2, readtoken();
427 while (lasttoken != TESAC) {
428 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
430 app = &cp->nclist.pattern;
431 if (lasttoken == TLP)
434 *app = ap = (union node *)stalloc(sizeof (struct narg));
436 ap->narg.text = wordtext;
437 ap->narg.backquote = backquotelist;
438 if (checkkwd = 2, readtoken() != TPIPE)
440 app = &ap->narg.next;
443 ap->narg.next = NULL;
444 if (lasttoken != TRP)
445 noaliases = 0, synexpect(TRP);
446 cp->nclist.body = list(0);
449 if ((t = readtoken()) != TESAC) {
451 noaliases = 0, synexpect(TENDCASE);
453 checkkwd = 2, readtoken();
455 cpp = &cp->nclist.next;
457 noaliases = 0; /* reset alias expansion */
462 n1 = (union node *)stalloc(sizeof (struct nredir));
463 n1->type = NSUBSHELL;
464 n1->nredir.n = list(0);
465 n1->nredir.redirect = NULL;
466 if (readtoken() != TRP)
472 if (readtoken() != TEND)
476 /* Handle an empty command like other simple commands. */
481 * An empty command before a ; doesn't make much sense, and
482 * should certainly be disallowed in the case of `if ;'.
491 n1 = simplecmd(rpp, redir);
497 /* Now check for redirection which may follow command */
498 while (readtoken() == TREDIR) {
499 *rpp = n2 = redirnode;
500 rpp = &n2->nfile.next;
506 if (n1->type != NSUBSHELL) {
507 n2 = (union node *)stalloc(sizeof (struct nredir));
512 n1->nredir.redirect = redir;
517 n2 = (union node *)stalloc(sizeof (struct nnot));
528 simplecmd(union node **rpp, union node *redir)
530 union node *args, **app;
531 union node **orig_rpp = rpp;
532 union node *n = NULL, *n2;
535 /* If we don't have any redirections already, then we must reset */
536 /* rpp to be the address of the local redir variable. */
543 * We save the incoming value, because we need this for shell
544 * functions. There can not be a redirect or an argument between
545 * the function name and the open parenthesis.
549 while (readtoken() == TNOT) {
550 TRACE(("command: TNOT recognized\n"));
556 if (readtoken() == TWORD) {
557 n = (union node *)stalloc(sizeof (struct narg));
559 n->narg.text = wordtext;
560 n->narg.backquote = backquotelist;
563 } else if (lasttoken == TREDIR) {
564 *rpp = n = redirnode;
565 rpp = &n->nfile.next;
566 parsefname(); /* read name of redirection file */
567 } else if (lasttoken == TLP && app == &args->narg.next
568 && rpp == orig_rpp) {
569 /* We have a function */
570 if (readtoken() != TRP)
574 if (! goodname(n->narg.text))
575 synerror("Bad function name");
578 n->narg.next = command();
588 n = (union node *)stalloc(sizeof (struct ncmd));
592 n->ncmd.redirect = redir;
596 n2 = (union node *)stalloc(sizeof (struct nnot));
610 n = (union node *)stalloc(sizeof (struct narg));
613 n->narg.text = wordtext;
614 n->narg.backquote = backquotelist;
618 void fixredir(union node *n, const char *text, int err)
620 TRACE(("Fix redir %s %d\n", text, err));
622 n->ndup.vname = NULL;
624 if (is_digit(text[0]) && text[1] == '\0')
625 n->ndup.dupfd = digit_val(text[0]);
626 else if (text[0] == '-' && text[1] == '\0')
631 synerror("Bad fd number");
633 n->ndup.vname = makename();
641 union node *n = redirnode;
643 if (readtoken() != TWORD)
645 if (n->type == NHERE) {
646 struct heredoc *here = heredoc;
652 TRACE(("Here document %d\n", n->type));
653 if (here->striptabs) {
654 while (*wordtext == '\t')
657 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
658 synerror("Illegal eof marker for << redirection");
660 here->eofmark = wordtext;
662 if (heredoclist == NULL)
665 for (p = heredoclist ; p->next ; p = p->next);
668 } else if (n->type == NTOFD || n->type == NFROMFD) {
669 fixredir(n, wordtext, 0);
671 n->nfile.fname = makename();
677 * Input any here documents.
683 struct heredoc *here;
686 while (heredoclist) {
688 heredoclist = here->next;
693 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
694 here->eofmark, here->striptabs);
695 n = (union node *)stalloc(sizeof (struct narg));
698 n->narg.text = wordtext;
699 n->narg.backquote = backquotelist;
700 here->here->nhere.doc = n;
718 int savecheckkwd = checkkwd;
721 int alreadyseen = tokpushback;
740 * check for keywords and aliases
742 if (t == TWORD && !quoteflag)
744 const char * const *pp;
746 for (pp = parsekwd; *pp; pp++) {
747 if (**pp == *wordtext && equal(*pp, wordtext))
749 lasttoken = t = pp - parsekwd + KWDOFFSET;
750 TRACE(("keyword %s recognized\n", tokname[t]));
754 if (noaliases == 0 &&
755 (ap = lookupalias(wordtext, 1)) != NULL) {
756 pushstring(ap->val, strlen(ap->val), ap);
757 checkkwd = savecheckkwd;
762 checkkwd = (t == TNOT) ? savecheckkwd : 0;
766 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
768 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
775 * Read the next input token.
776 * If the token is a word, we set backquotelist to the list of cmds in
777 * backquotes. We set quoteflag to true if any part of the word was
779 * If the token is TREDIR, then we set redirnode to a structure containing
781 * In all cases, the variable startlinno is set to the number of the line
782 * on which the token starts.
784 * [Change comment: here documents and internal procedures]
785 * [Readtoken shouldn't have any arguments. Perhaps we should make the
786 * word parsing code into a separate routine. In this case, readtoken
787 * doesn't need to have any internal procedures, but parseword does.
788 * We could also make parseoperator in essence the main routine, and
789 * have parseword (readtoken1?) handle both words and redirection.]
792 #define RETURN(token) return lasttoken = token
808 for (;;) { /* until token or start of word found */
810 if (c == ' ' || c == '\t')
811 continue; /* quick check for white space first */
816 while ((c = pgetc()) != '\n' && c != PEOF);
820 if (pgetc() == '\n') {
821 startlinno = ++plinno;
832 needprompt = doprompt;
860 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
867 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
868 * is not NULL, read a here document. In the latter case, eofmark is the
869 * word which marks the end of the document and striptabs is true if
870 * leading tabs should be stripped from the document. The argument firstc
871 * is the first character of the input token or document.
873 * Because C does not have internal subroutines, I have simulated them
874 * using goto's to implement the subroutine linkage. The following macros
875 * will run code that appears at the end of readtoken1.
878 #define CHECKEND() {goto checkend; checkend_return:;}
879 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
880 #define PARSESUB() {goto parsesub; parsesub_return:;}
881 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
882 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
883 #define PARSEARITH() {goto parsearith; parsearith_return:;}
886 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
891 char line[EOFMARKLEN + 1];
892 struct nodelist *bqlist;
895 int varnest; /* levels of variables expansion */
896 int arinest; /* levels of arithmetic expansion */
897 int parenlevel; /* levels of parens in arithmetic */
899 char const *prevsyntax; /* syntax before arithmetic */
904 if (syntax == DQSYNTAX)
913 loop: { /* for each line, until end of word */
914 CHECKEND(); /* set c to PEOF if at end of here document */
915 for (;;) { /* until end of line or end of word */
916 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
918 synentry = syntax[c];
922 if (syntax == BASESYNTAX)
923 goto endword; /* exit outer loop */
931 goto loop; /* continue outer loop */
936 if (eofmark == NULL || dblquote)
937 USTPUTC(CTLESC, out);
940 case CBACK: /* backslash */
945 } else if (c == '\n') {
952 if (dblquote && c != '\\' &&
953 c != '`' && c != '$' &&
954 (c != '"' || eofmark != NULL))
956 if (SQSYNTAX[c] == CCTL)
957 USTPUTC(CTLESC, out);
958 else if (eofmark == NULL)
959 USTPUTC(CTLQUOTEMARK, out);
966 USTPUTC(CTLQUOTEMARK, out);
971 USTPUTC(CTLQUOTEMARK, out);
976 if (eofmark != NULL && arinest == 0 &&
983 } else if (eofmark == NULL) {
991 PARSESUB(); /* parse substitution */
993 case CENDVAR: /* '}' */
996 USTPUTC(CTLENDVAR, out);
1001 case CLP: /* '(' in arithmetic */
1005 case CRP: /* ')' in arithmetic */
1006 if (parenlevel > 0) {
1010 if (pgetc() == ')') {
1011 if (--arinest == 0) {
1012 USTPUTC(CTLENDARI, out);
1013 syntax = prevsyntax;
1014 if (syntax == DQSYNTAX)
1023 * (don't 2nd guess - no error)
1030 case CBQUOTE: /* '`' */
1034 goto endword; /* exit outer loop */
1037 goto endword; /* exit outer loop */
1044 if (syntax == ARISYNTAX)
1045 synerror("Missing '))'");
1046 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1047 synerror("Unterminated quoted string");
1049 startlinno = plinno;
1050 synerror("Missing '}'");
1053 len = out - stackblock();
1055 if (eofmark == NULL) {
1056 if ((c == '>' || c == '<')
1059 && (*out == '\0' || is_digit(*out))) {
1061 return lasttoken = TREDIR;
1067 backquotelist = bqlist;
1068 grabstackblock(len);
1070 return lasttoken = TWORD;
1071 /* end of readtoken routine */
1076 * Check to see whether we are at the end of the here document. When this
1077 * is called, c is set to the first character of the next input line. If
1078 * we are at the end of the here document, this routine sets the c to PEOF.
1087 if (c == *eofmark) {
1088 if (pfgets(line, sizeof line) != NULL) {
1092 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1093 if (*p == '\n' && *q == '\0') {
1096 needprompt = doprompt;
1098 pushstring(line, strlen(line), NULL);
1103 goto checkend_return;
1108 * Parse a redirection operator. The variable "out" points to a string
1109 * specifying the fd to be redirected. The variable "c" contains the
1110 * first character of the redirection operator.
1117 np = (union node *)stalloc(sizeof (struct nfile));
1126 np->type = NCLOBBER;
1131 } else { /* c == '<' */
1135 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1136 np = (union node *)stalloc(sizeof (struct nhere));
1140 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1142 if ((c = pgetc()) == '-') {
1143 heredoc->striptabs = 1;
1145 heredoc->striptabs = 0;
1148 } else if (c == '&')
1158 np->nfile.fd = digit_val(fd);
1160 goto parseredir_return;
1165 * Parse a substitution. At this point, we have read the dollar sign
1175 static const char types[] = "}-+?=";
1176 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1182 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1186 } else if (c == '(') { /* $(command) or $((arith)) */
1187 if (pgetc() == '(') {
1194 USTPUTC(CTLVAR, out);
1195 typeloc = out - stackblock();
1196 USTPUTC(VSNORMAL, out);
1203 if ((c = pgetc()) == '}')
1211 if (!is_eof(c) && is_name(c)) {
1217 } while (!is_eof(c) && is_in_name(c));
1219 strncmp(out - length, "LINENO", length) == 0) {
1220 /* Replace the variable name with the
1221 * current line number. */
1224 linno -= funclinno - 1;
1225 snprintf(buf, sizeof(buf), "%d", linno);
1227 for (i = 0; buf[i] != '\0'; i++)
1228 STPUTC(buf[i], out);
1231 } else if (is_digit(c)) {
1232 if (bracketed_name) {
1236 } while (is_digit(c));
1242 if (! is_special(c)) {
1260 p = strchr(types, c);
1267 subtype = p - types + VSNORMAL;
1273 subtype = c == '#' ? VSTRIMLEFT :
1283 } else if (subtype != VSERROR) {
1287 if (subtype != VSLENGTH && (dblquote || arinest))
1289 *(stackblock() + typeloc) = subtype | flags;
1290 if (subtype != VSNORMAL)
1293 goto parsesub_return;
1298 * Called to parse command substitutions. Newstyle is set if the command
1299 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1300 * list of commands (passed by reference), and savelen is the number of
1301 * characters on the top of the stack which must be preserved.
1305 struct nodelist **nlpp;
1309 struct jmploc jmploc;
1310 struct jmploc *const savehandler = handler;
1314 savepbq = parsebackquote;
1315 if (setjmp(jmploc.loc)) {
1319 handler = savehandler;
1320 longjmp(handler->loc, 1);
1324 savelen = out - stackblock();
1326 str = ckmalloc(savelen);
1327 memcpy(str, stackblock(), savelen);
1332 /* We must read until the closing backquote, giving special
1333 treatment to some slashes, and then push the string and
1334 reread it as input, interpreting it normally. */
1347 switch (c = pgetc()) {
1352 if ((c = pgetc()) == '\n') {
1359 * If eating a newline, avoid putting
1360 * the newline into the new character
1361 * stream (via the STPUTC after the
1366 if (c != '\\' && c != '`' && c != '$'
1367 && (!dblquote || c != '"'))
1373 needprompt = doprompt;
1377 startlinno = plinno;
1378 synerror("EOF in backquote substitution");
1388 savelen = out - stackblock();
1390 str = ckmalloc(savelen);
1391 memcpy(str, stackblock(), savelen);
1392 setinputstring(str, 1);
1397 nlpp = &(*nlpp)->next;
1398 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1399 (*nlpp)->next = NULL;
1400 parsebackquote = oldstyle;
1403 saveprompt = doprompt;
1410 doprompt = saveprompt;
1412 if (readtoken() != TRP)
1419 * Start reading from old file again, ignoring any pushed back
1420 * tokens left from the backquote parsing
1425 while (stackblocksize() <= savelen)
1429 memcpy(out, str, savelen);
1430 STADJUST(savelen, out);
1436 parsebackquote = savepbq;
1437 handler = savehandler;
1438 if (arinest || dblquote)
1439 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1441 USTPUTC(CTLBACKQ, out);
1443 goto parsebackq_oldreturn;
1445 goto parsebackq_newreturn;
1449 * Parse an arithmetic expansion (indicate start of one and set state)
1453 if (++arinest == 1) {
1454 prevsyntax = syntax;
1456 USTPUTC(CTLARI, out);
1463 * we collapse embedded arithmetic expansion to
1464 * parenthesis, which should be equivalent
1468 goto parsearith_return;
1471 } /* end of readtoken */
1483 * Returns true if the text contains nothing to expand (no dollar signs
1488 noexpand(char *text)
1494 while ((c = *p++) != '\0') {
1495 if ( c == CTLQUOTEMARK)
1499 else if (BASESYNTAX[(int)c] == CCTL)
1507 * Return true if the argument is a legal variable name (a letter or
1508 * underscore followed by zero or more letters, underscores, and digits).
1512 goodname(char *name)
1520 if (! is_in_name(*p))
1528 * Called when an unexpected token is read during the parse. The argument
1529 * is the token that is expected, or -1 if more than one type of token can
1530 * occur at this point.
1534 synexpect(int token)
1539 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1540 tokname[lasttoken], tokname[token]);
1542 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1552 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1553 outfmt(&errout, "Syntax error: %s\n", msg);
1554 error((char *)NULL);
1558 setprompt(int which)
1560 whichprompt = which;
1565 out2str(getprompt(NULL));
1569 * called by editline -- any expansions to the prompt
1570 * should be added here.
1573 getprompt(void *unused __unused)
1575 static char ps[PROMPTLEN];
1580 * Select prompt format.
1582 switch (whichprompt) {
1593 return "<internal prompt error>";
1597 * Format prompt string.
1599 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1606 * \h specifies just the local hostname,
1607 * \H specifies fully-qualified hostname.
1612 gethostname(&ps[i], PROMPTLEN - i);
1613 /* Skip to end of hostname. */
1614 trim = (*fmt == 'h') ? '.' : '\0';
1615 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1620 * Working directory.
1622 * \W specifies just the final component,
1623 * \w specifies the entire path.
1628 getcwd(&ps[i], PROMPTLEN - i);
1630 /* Final path component only. */
1632 for (j = i; ps[j] != '\0'; j++)
1635 memmove(&ps[i], &ps[trim],
1638 /* Skip to end of path. */
1639 while (ps[i + 1] != '\0')
1646 * '$' for normal users, '#' for root.
1649 ps[i] = (geteuid() != 0) ? '$' : '#';
1660 * Emit unrecognized formats verbatim.