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 */
98 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
99 static int noaliases = 0;
102 STATIC union node *list(int);
103 STATIC union node *andor(void);
104 STATIC union node *pipeline(void);
105 STATIC union node *command(void);
106 STATIC union node *simplecmd(union node **, union node *);
107 STATIC union node *makename(void);
108 STATIC void parsefname(void);
109 STATIC void parseheredoc(void);
110 STATIC int peektoken(void);
111 STATIC int readtoken(void);
112 STATIC int xxreadtoken(void);
113 STATIC int readtoken1(int, char const *, char *, int);
114 STATIC int noexpand(char *);
115 STATIC void synexpect(int);
116 STATIC void synerror(char *);
117 STATIC void setprompt(int);
121 * Read and parse a command. Returns NEOF on end of file. (NULL is a
122 * valid parse tree indicating a blank line.)
126 parsecmd(int interact)
150 union node *n1, *n2, *n3;
154 if (nlflag == 0 && tokendlist[peektoken()])
160 if (tok == TBACKGND) {
161 if (n2->type == NCMD || n2->type == NPIPE) {
162 n2->ncmd.backgnd = 1;
163 } else if (n2->type == NREDIR) {
166 n3 = (union node *)stalloc(sizeof (struct nredir));
169 n3->nredir.redirect = NULL;
177 n3 = (union node *)stalloc(sizeof (struct nbinary));
179 n3->nbinary.ch1 = n1;
180 n3->nbinary.ch2 = n2;
197 if (tokendlist[peektoken()])
204 pungetc(); /* push back EOF on input */
220 union node *n1, *n2, *n3;
225 if ((t = readtoken()) == TAND) {
227 } else if (t == TOR) {
234 n3 = (union node *)stalloc(sizeof (struct nbinary));
236 n3->nbinary.ch1 = n1;
237 n3->nbinary.ch2 = n2;
247 union node *n1, *n2, *pipenode;
248 struct nodelist *lp, *prev;
252 TRACE(("pipeline: entered\n"));
253 while (readtoken() == TNOT)
257 if (readtoken() == TPIPE) {
258 pipenode = (union node *)stalloc(sizeof (struct npipe));
259 pipenode->type = NPIPE;
260 pipenode->npipe.backgnd = 0;
261 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
262 pipenode->npipe.cmdlist = lp;
266 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
269 } while (readtoken() == TPIPE);
275 n2 = (union node *)stalloc(sizeof (struct nnot));
289 union node *ap, **app;
290 union node *cp, **cpp;
291 union node *redir, **rpp;
299 /* Check for redirection which may precede command */
300 while (readtoken() == TREDIR) {
301 *rpp = n2 = redirnode;
302 rpp = &n2->nfile.next;
307 while (readtoken() == TNOT) {
308 TRACE(("command: TNOT recognized\n"));
313 switch (readtoken()) {
315 n1 = (union node *)stalloc(sizeof (struct nif));
317 if ((n1->nif.test = list(0)) == NULL)
319 if (readtoken() != TTHEN)
321 n1->nif.ifpart = list(0);
323 while (readtoken() == TELIF) {
324 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
325 n2 = n2->nif.elsepart;
327 if ((n2->nif.test = list(0)) == NULL)
329 if (readtoken() != TTHEN)
331 n2->nif.ifpart = list(0);
333 if (lasttoken == TELSE)
334 n2->nif.elsepart = list(0);
336 n2->nif.elsepart = NULL;
339 if (readtoken() != TFI)
346 n1 = (union node *)stalloc(sizeof (struct nbinary));
347 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
348 if ((n1->nbinary.ch1 = list(0)) == NULL)
350 if ((got=readtoken()) != TDO) {
351 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
354 n1->nbinary.ch2 = list(0);
355 if (readtoken() != TDONE)
361 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
362 synerror("Bad for loop variable");
363 n1 = (union node *)stalloc(sizeof (struct nfor));
365 n1->nfor.var = wordtext;
366 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
368 while (readtoken() == TWORD) {
369 n2 = (union node *)stalloc(sizeof (struct narg));
371 n2->narg.text = wordtext;
372 n2->narg.backquote = backquotelist;
374 app = &n2->narg.next;
378 if (lasttoken != TNL && lasttoken != TSEMI)
381 static char argvars[5] = {
382 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
384 n2 = (union node *)stalloc(sizeof (struct narg));
386 n2->narg.text = argvars;
387 n2->narg.backquote = NULL;
388 n2->narg.next = NULL;
391 * Newline or semicolon here is optional (but note
392 * that the original Bourne shell only allowed NL).
394 if (lasttoken != TNL && lasttoken != TSEMI)
398 if ((t = readtoken()) == TDO)
400 else if (t == TBEGIN)
404 n1->nfor.body = list(0);
405 if (readtoken() != t)
410 n1 = (union node *)stalloc(sizeof (struct ncase));
412 if (readtoken() != TWORD)
414 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
416 n2->narg.text = wordtext;
417 n2->narg.backquote = backquotelist;
418 n2->narg.next = NULL;
419 while (readtoken() == TNL);
420 if (lasttoken != TWORD || ! equal(wordtext, "in"))
421 synerror("expecting \"in\"");
422 cpp = &n1->ncase.cases;
423 noaliases = 1; /* turn off alias expansion */
424 checkkwd = 2, readtoken();
425 while (lasttoken != TESAC) {
426 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
428 app = &cp->nclist.pattern;
429 if (lasttoken == TLP)
432 *app = ap = (union node *)stalloc(sizeof (struct narg));
434 ap->narg.text = wordtext;
435 ap->narg.backquote = backquotelist;
436 if (checkkwd = 2, readtoken() != TPIPE)
438 app = &ap->narg.next;
441 ap->narg.next = NULL;
442 if (lasttoken != TRP)
443 noaliases = 0, synexpect(TRP);
444 cp->nclist.body = list(0);
447 if ((t = readtoken()) != TESAC) {
449 noaliases = 0, synexpect(TENDCASE);
451 checkkwd = 2, readtoken();
453 cpp = &cp->nclist.next;
455 noaliases = 0; /* reset alias expansion */
460 n1 = (union node *)stalloc(sizeof (struct nredir));
461 n1->type = NSUBSHELL;
462 n1->nredir.n = list(0);
463 n1->nredir.redirect = NULL;
464 if (readtoken() != TRP)
470 if (readtoken() != TEND)
474 /* Handle an empty command like other simple commands. */
479 * An empty command before a ; doesn't make much sense, and
480 * should certainly be disallowed in the case of `if ;'.
489 n1 = simplecmd(rpp, redir);
495 /* Now check for redirection which may follow command */
496 while (readtoken() == TREDIR) {
497 *rpp = n2 = redirnode;
498 rpp = &n2->nfile.next;
504 if (n1->type != NSUBSHELL) {
505 n2 = (union node *)stalloc(sizeof (struct nredir));
510 n1->nredir.redirect = redir;
515 n2 = (union node *)stalloc(sizeof (struct nnot));
526 simplecmd(union node **rpp, union node *redir)
528 union node *args, **app;
529 union node **orig_rpp = rpp;
530 union node *n = NULL, *n2;
533 /* If we don't have any redirections already, then we must reset */
534 /* rpp to be the address of the local redir variable. */
541 * We save the incoming value, because we need this for shell
542 * functions. There can not be a redirect or an argument between
543 * the function name and the open parenthesis.
547 while (readtoken() == TNOT) {
548 TRACE(("command: TNOT recognized\n"));
554 if (readtoken() == TWORD) {
555 n = (union node *)stalloc(sizeof (struct narg));
557 n->narg.text = wordtext;
558 n->narg.backquote = backquotelist;
561 } else if (lasttoken == TREDIR) {
562 *rpp = n = redirnode;
563 rpp = &n->nfile.next;
564 parsefname(); /* read name of redirection file */
565 } else if (lasttoken == TLP && app == &args->narg.next
566 && rpp == orig_rpp) {
567 /* We have a function */
568 if (readtoken() != TRP)
571 if (! goodname(n->narg.text))
572 synerror("Bad function name");
575 n->narg.next = command();
584 n = (union node *)stalloc(sizeof (struct ncmd));
588 n->ncmd.redirect = redir;
592 n2 = (union node *)stalloc(sizeof (struct nnot));
606 n = (union node *)stalloc(sizeof (struct narg));
609 n->narg.text = wordtext;
610 n->narg.backquote = backquotelist;
614 void fixredir(union node *n, const char *text, int err)
616 TRACE(("Fix redir %s %d\n", text, err));
618 n->ndup.vname = NULL;
620 if (is_digit(text[0]) && text[1] == '\0')
621 n->ndup.dupfd = digit_val(text[0]);
622 else if (text[0] == '-' && text[1] == '\0')
627 synerror("Bad fd number");
629 n->ndup.vname = makename();
637 union node *n = redirnode;
639 if (readtoken() != TWORD)
641 if (n->type == NHERE) {
642 struct heredoc *here = heredoc;
648 TRACE(("Here document %d\n", n->type));
649 if (here->striptabs) {
650 while (*wordtext == '\t')
653 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
654 synerror("Illegal eof marker for << redirection");
656 here->eofmark = wordtext;
658 if (heredoclist == NULL)
661 for (p = heredoclist ; p->next ; p = p->next);
664 } else if (n->type == NTOFD || n->type == NFROMFD) {
665 fixredir(n, wordtext, 0);
667 n->nfile.fname = makename();
673 * Input any here documents.
679 struct heredoc *here;
682 while (heredoclist) {
684 heredoclist = here->next;
689 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
690 here->eofmark, here->striptabs);
691 n = (union node *)stalloc(sizeof (struct narg));
694 n->narg.text = wordtext;
695 n->narg.backquote = backquotelist;
696 here->here->nhere.doc = n;
714 int savecheckkwd = checkkwd;
717 int alreadyseen = tokpushback;
736 * check for keywords and aliases
738 if (t == TWORD && !quoteflag)
740 const char * const *pp;
742 for (pp = parsekwd; *pp; pp++) {
743 if (**pp == *wordtext && equal(*pp, wordtext))
745 lasttoken = t = pp - parsekwd + KWDOFFSET;
746 TRACE(("keyword %s recognized\n", tokname[t]));
750 if (noaliases == 0 &&
751 (ap = lookupalias(wordtext, 1)) != NULL) {
752 pushstring(ap->val, strlen(ap->val), ap);
753 checkkwd = savecheckkwd;
758 checkkwd = (t == TNOT) ? savecheckkwd : 0;
762 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
764 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
771 * Read the next input token.
772 * If the token is a word, we set backquotelist to the list of cmds in
773 * backquotes. We set quoteflag to true if any part of the word was
775 * If the token is TREDIR, then we set redirnode to a structure containing
777 * In all cases, the variable startlinno is set to the number of the line
778 * on which the token starts.
780 * [Change comment: here documents and internal procedures]
781 * [Readtoken shouldn't have any arguments. Perhaps we should make the
782 * word parsing code into a separate routine. In this case, readtoken
783 * doesn't need to have any internal procedures, but parseword does.
784 * We could also make parseoperator in essence the main routine, and
785 * have parseword (readtoken1?) handle both words and redirection.]
788 #define RETURN(token) return lasttoken = token
804 for (;;) { /* until token or start of word found */
806 if (c == ' ' || c == '\t')
807 continue; /* quick check for white space first */
812 while ((c = pgetc()) != '\n' && c != PEOF);
816 if (pgetc() == '\n') {
817 startlinno = ++plinno;
828 needprompt = doprompt;
856 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
863 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
864 * is not NULL, read a here document. In the latter case, eofmark is the
865 * word which marks the end of the document and striptabs is true if
866 * leading tabs should be stripped from the document. The argument firstc
867 * is the first character of the input token or document.
869 * Because C does not have internal subroutines, I have simulated them
870 * using goto's to implement the subroutine linkage. The following macros
871 * will run code that appears at the end of readtoken1.
874 #define CHECKEND() {goto checkend; checkend_return:;}
875 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
876 #define PARSESUB() {goto parsesub; parsesub_return:;}
877 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
878 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
879 #define PARSEARITH() {goto parsearith; parsearith_return:;}
882 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
887 char line[EOFMARKLEN + 1];
888 struct nodelist *bqlist;
891 int varnest; /* levels of variables expansion */
892 int arinest; /* levels of arithmetic expansion */
893 int parenlevel; /* levels of parens in arithmetic */
895 char const *prevsyntax; /* syntax before arithmetic */
898 /* Avoid longjmp clobbering */
913 if (syntax == DQSYNTAX)
922 loop: { /* for each line, until end of word */
923 CHECKEND(); /* set c to PEOF if at end of here document */
924 for (;;) { /* until end of line or end of word */
925 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
927 synentry = syntax[c];
931 if (syntax == BASESYNTAX)
932 goto endword; /* exit outer loop */
940 goto loop; /* continue outer loop */
945 if (eofmark == NULL || dblquote)
946 USTPUTC(CTLESC, out);
949 case CBACK: /* backslash */
954 } else if (c == '\n') {
961 if (dblquote && c != '\\' &&
962 c != '`' && c != '$' &&
963 (c != '"' || eofmark != NULL))
965 if (SQSYNTAX[c] == CCTL)
966 USTPUTC(CTLESC, out);
967 else if (eofmark == NULL)
968 USTPUTC(CTLQUOTEMARK, out);
975 USTPUTC(CTLQUOTEMARK, out);
980 USTPUTC(CTLQUOTEMARK, out);
985 if (eofmark != NULL && arinest == 0 &&
992 } else if (eofmark == NULL) {
1000 PARSESUB(); /* parse substitution */
1002 case CENDVAR: /* '}' */
1005 USTPUTC(CTLENDVAR, out);
1010 case CLP: /* '(' in arithmetic */
1014 case CRP: /* ')' in arithmetic */
1015 if (parenlevel > 0) {
1019 if (pgetc() == ')') {
1020 if (--arinest == 0) {
1021 USTPUTC(CTLENDARI, out);
1022 syntax = prevsyntax;
1023 if (syntax == DQSYNTAX)
1032 * (don't 2nd guess - no error)
1039 case CBQUOTE: /* '`' */
1043 goto endword; /* exit outer loop */
1046 goto endword; /* exit outer loop */
1053 if (syntax == ARISYNTAX)
1054 synerror("Missing '))'");
1055 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1056 synerror("Unterminated quoted string");
1058 startlinno = plinno;
1059 synerror("Missing '}'");
1062 len = out - stackblock();
1064 if (eofmark == NULL) {
1065 if ((c == '>' || c == '<')
1068 && (*out == '\0' || is_digit(*out))) {
1070 return lasttoken = TREDIR;
1076 backquotelist = bqlist;
1077 grabstackblock(len);
1079 return lasttoken = TWORD;
1080 /* end of readtoken routine */
1085 * Check to see whether we are at the end of the here document. When this
1086 * is called, c is set to the first character of the next input line. If
1087 * we are at the end of the here document, this routine sets the c to PEOF.
1096 if (c == *eofmark) {
1097 if (pfgets(line, sizeof line) != NULL) {
1101 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1102 if (*p == '\n' && *q == '\0') {
1105 needprompt = doprompt;
1107 pushstring(line, strlen(line), NULL);
1112 goto checkend_return;
1117 * Parse a redirection operator. The variable "out" points to a string
1118 * specifying the fd to be redirected. The variable "c" contains the
1119 * first character of the redirection operator.
1126 np = (union node *)stalloc(sizeof (struct nfile));
1135 np->type = NCLOBBER;
1140 } else { /* c == '<' */
1144 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1145 np = (union node *)stalloc(sizeof (struct nhere));
1149 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1151 if ((c = pgetc()) == '-') {
1152 heredoc->striptabs = 1;
1154 heredoc->striptabs = 0;
1157 } else if (c == '&')
1167 np->nfile.fd = digit_val(fd);
1169 goto parseredir_return;
1174 * Parse a substitution. At this point, we have read the dollar sign
1183 static const char types[] = "}-+?=";
1184 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);
1207 if ((c = pgetc()) == '}')
1215 if (!is_eof(c) && is_name(c)) {
1219 } while (!is_eof(c) && is_in_name(c));
1220 } else if (is_digit(c)) {
1221 if (bracketed_name) {
1225 } while (is_digit(c));
1231 if (! is_special(c)) {
1250 p = strchr(types, c);
1257 subtype = p - types + VSNORMAL;
1263 subtype = c == '#' ? VSTRIMLEFT :
1273 } else if (subtype != VSERROR) {
1277 if (subtype != VSLENGTH && (dblquote || arinest))
1279 *(stackblock() + typeloc) = subtype | flags;
1280 if (subtype != VSNORMAL)
1283 goto parsesub_return;
1288 * Called to parse command substitutions. Newstyle is set if the command
1289 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1290 * list of commands (passed by reference), and savelen is the number of
1291 * characters on the top of the stack which must be preserved.
1295 struct nodelist **nlpp;
1299 struct jmploc jmploc;
1300 struct jmploc *volatile savehandler;
1304 /* Avoid longjmp clobbering */
1308 savepbq = parsebackquote;
1309 if (setjmp(jmploc.loc)) {
1313 handler = savehandler;
1314 longjmp(handler->loc, 1);
1318 savelen = out - stackblock();
1320 str = ckmalloc(savelen);
1321 memcpy(str, stackblock(), savelen);
1323 savehandler = handler;
1327 /* We must read until the closing backquote, giving special
1328 treatment to some slashes, and then push the string and
1329 reread it as input, interpreting it normally. */
1342 switch (c = pgetc()) {
1347 if ((c = pgetc()) == '\n') {
1354 * If eating a newline, avoid putting
1355 * the newline into the new character
1356 * stream (via the STPUTC after the
1361 if (c != '\\' && c != '`' && c != '$'
1362 && (!dblquote || c != '"'))
1368 needprompt = doprompt;
1372 startlinno = plinno;
1373 synerror("EOF in backquote substitution");
1383 savelen = out - stackblock();
1385 str = ckmalloc(savelen);
1386 memcpy(str, stackblock(), savelen);
1387 setinputstring(str, 1);
1392 nlpp = &(*nlpp)->next;
1393 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1394 (*nlpp)->next = NULL;
1395 parsebackquote = oldstyle;
1398 saveprompt = doprompt;
1405 doprompt = saveprompt;
1407 if (readtoken() != TRP)
1414 * Start reading from old file again, ignoring any pushed back
1415 * tokens left from the backquote parsing
1420 while (stackblocksize() <= savelen)
1424 memcpy(out, str, savelen);
1425 STADJUST(savelen, out);
1431 parsebackquote = savepbq;
1432 handler = savehandler;
1433 if (arinest || dblquote)
1434 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1436 USTPUTC(CTLBACKQ, out);
1438 goto parsebackq_oldreturn;
1440 goto parsebackq_newreturn;
1444 * Parse an arithmetic expansion (indicate start of one and set state)
1448 if (++arinest == 1) {
1449 prevsyntax = syntax;
1451 USTPUTC(CTLARI, out);
1458 * we collapse embedded arithmetic expansion to
1459 * parenthesis, which should be equivalent
1463 goto parsearith_return;
1466 } /* end of readtoken */
1478 * Returns true if the text contains nothing to expand (no dollar signs
1483 noexpand(char *text)
1489 while ((c = *p++) != '\0') {
1490 if ( c == CTLQUOTEMARK)
1494 else if (BASESYNTAX[(int)c] == CCTL)
1502 * Return true if the argument is a legal variable name (a letter or
1503 * underscore followed by zero or more letters, underscores, and digits).
1507 goodname(char *name)
1515 if (! is_in_name(*p))
1523 * Called when an unexpected token is read during the parse. The argument
1524 * is the token that is expected, or -1 if more than one type of token can
1525 * occur at this point.
1529 synexpect(int token)
1534 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1535 tokname[lasttoken], tokname[token]);
1537 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1547 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1548 outfmt(&errout, "Syntax error: %s\n", msg);
1549 error((char *)NULL);
1553 setprompt(int which)
1555 whichprompt = which;
1560 out2str(getprompt(NULL));
1564 * called by editline -- any expansions to the prompt
1565 * should be added here.
1568 getprompt(void *unused __unused)
1570 static char ps[PROMPTLEN];
1575 * Select prompt format.
1577 switch (whichprompt) {
1588 return "<internal prompt error>";
1592 * Format prompt string.
1594 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1601 * \h specifies just the local hostname,
1602 * \H specifies fully-qualified hostname.
1607 gethostname(&ps[i], PROMPTLEN - i);
1608 /* Skip to end of hostname. */
1609 trim = (*fmt == 'h') ? '.' : '\0';
1610 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1615 * Working directory.
1617 * \W specifies just the final component,
1618 * \w specifies the entire path.
1623 getcwd(&ps[i], PROMPTLEN - i);
1625 /* Final path component only. */
1627 for (j = i; ps[j] != '\0'; j++)
1630 memmove(&ps[i], &ps[trim],
1633 /* Skip to end of path. */
1634 while (ps[i + 1] != '\0')
1641 * '$' for normal users, '#' for root.
1644 ps[i] = (geteuid() != 0) ? '$' : '#';
1655 * Emit unrecognized formats verbatim.