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(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;
622 void fixredir(union node *n, const char *text, int err)
624 TRACE(("Fix redir %s %d\n", text, err));
626 n->ndup.vname = NULL;
628 if (is_digit(text[0]) && text[1] == '\0')
629 n->ndup.dupfd = digit_val(text[0]);
630 else if (text[0] == '-' && text[1] == '\0')
635 synerror("Bad fd number");
637 n->ndup.vname = makename();
645 union node *n = redirnode;
647 if (readtoken() != TWORD)
649 if (n->type == NHERE) {
650 struct heredoc *here = heredoc;
656 TRACE(("Here document %d\n", n->type));
657 if (here->striptabs) {
658 while (*wordtext == '\t')
661 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
662 synerror("Illegal eof marker for << redirection");
664 here->eofmark = wordtext;
666 if (heredoclist == NULL)
669 for (p = heredoclist ; p->next ; p = p->next);
672 } else if (n->type == NTOFD || n->type == NFROMFD) {
673 fixredir(n, wordtext, 0);
675 n->nfile.fname = makename();
681 * Input any here documents.
687 struct heredoc *here;
690 while (heredoclist) {
692 heredoclist = here->next;
697 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
698 here->eofmark, here->striptabs);
699 n = (union node *)stalloc(sizeof (struct narg));
702 n->narg.text = wordtext;
703 n->narg.backquote = backquotelist;
704 here->here->nhere.doc = n;
722 int savecheckkwd = checkkwd;
725 int alreadyseen = tokpushback;
744 * check for keywords and aliases
746 if (t == TWORD && !quoteflag)
748 const char * const *pp;
750 for (pp = parsekwd; *pp; pp++) {
751 if (**pp == *wordtext && equal(*pp, wordtext))
753 lasttoken = t = pp - parsekwd + KWDOFFSET;
754 TRACE(("keyword %s recognized\n", tokname[t]));
758 if (noaliases == 0 &&
759 (ap = lookupalias(wordtext, 1)) != NULL) {
760 pushstring(ap->val, strlen(ap->val), ap);
761 checkkwd = savecheckkwd;
766 checkkwd = (t == TNOT) ? savecheckkwd : 0;
770 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
772 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
779 * Read the next input token.
780 * If the token is a word, we set backquotelist to the list of cmds in
781 * backquotes. We set quoteflag to true if any part of the word was
783 * If the token is TREDIR, then we set redirnode to a structure containing
785 * In all cases, the variable startlinno is set to the number of the line
786 * on which the token starts.
788 * [Change comment: here documents and internal procedures]
789 * [Readtoken shouldn't have any arguments. Perhaps we should make the
790 * word parsing code into a separate routine. In this case, readtoken
791 * doesn't need to have any internal procedures, but parseword does.
792 * We could also make parseoperator in essence the main routine, and
793 * have parseword (readtoken1?) handle both words and redirection.]
796 #define RETURN(token) return lasttoken = token
812 for (;;) { /* until token or start of word found */
814 if (c == ' ' || c == '\t')
815 continue; /* quick check for white space first */
820 while ((c = pgetc()) != '\n' && c != PEOF);
824 if (pgetc() == '\n') {
825 startlinno = ++plinno;
836 needprompt = doprompt;
864 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
871 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
872 * is not NULL, read a here document. In the latter case, eofmark is the
873 * word which marks the end of the document and striptabs is true if
874 * leading tabs should be stripped from the document. The argument firstc
875 * is the first character of the input token or document.
877 * Because C does not have internal subroutines, I have simulated them
878 * using goto's to implement the subroutine linkage. The following macros
879 * will run code that appears at the end of readtoken1.
882 #define CHECKEND() {goto checkend; checkend_return:;}
883 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
884 #define PARSESUB() {goto parsesub; parsesub_return:;}
885 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
886 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
887 #define PARSEARITH() {goto parsearith; parsearith_return:;}
890 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
895 char line[EOFMARKLEN + 1];
896 struct nodelist *bqlist;
899 int varnest; /* levels of variables expansion */
900 int arinest; /* levels of arithmetic expansion */
901 int parenlevel; /* levels of parens in arithmetic */
903 char const *prevsyntax; /* syntax before arithmetic */
908 if (syntax == DQSYNTAX)
917 loop: { /* for each line, until end of word */
918 CHECKEND(); /* set c to PEOF if at end of here document */
919 for (;;) { /* until end of line or end of word */
920 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
922 synentry = syntax[c];
926 if (syntax == BASESYNTAX)
927 goto endword; /* exit outer loop */
935 goto loop; /* continue outer loop */
940 if (eofmark == NULL || dblquote)
941 USTPUTC(CTLESC, out);
944 case CBACK: /* backslash */
949 } else if (c == '\n') {
956 if (dblquote && c != '\\' &&
957 c != '`' && c != '$' &&
958 (c != '"' || eofmark != NULL))
960 if (SQSYNTAX[c] == CCTL)
961 USTPUTC(CTLESC, out);
962 else if (eofmark == NULL)
963 USTPUTC(CTLQUOTEMARK, out);
970 USTPUTC(CTLQUOTEMARK, out);
975 USTPUTC(CTLQUOTEMARK, out);
980 if (eofmark != NULL && arinest == 0 &&
987 } else if (eofmark == NULL) {
995 PARSESUB(); /* parse substitution */
997 case CENDVAR: /* '}' */
1000 USTPUTC(CTLENDVAR, out);
1005 case CLP: /* '(' in arithmetic */
1009 case CRP: /* ')' in arithmetic */
1010 if (parenlevel > 0) {
1014 if (pgetc() == ')') {
1015 if (--arinest == 0) {
1016 USTPUTC(CTLENDARI, out);
1017 syntax = prevsyntax;
1018 if (syntax == DQSYNTAX)
1027 * (don't 2nd guess - no error)
1034 case CBQUOTE: /* '`' */
1038 goto endword; /* exit outer loop */
1041 goto endword; /* exit outer loop */
1048 if (syntax == ARISYNTAX)
1049 synerror("Missing '))'");
1050 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1051 synerror("Unterminated quoted string");
1053 startlinno = plinno;
1054 synerror("Missing '}'");
1057 len = out - stackblock();
1059 if (eofmark == NULL) {
1060 if ((c == '>' || c == '<')
1063 && (*out == '\0' || is_digit(*out))) {
1065 return lasttoken = TREDIR;
1071 backquotelist = bqlist;
1072 grabstackblock(len);
1074 return lasttoken = TWORD;
1075 /* end of readtoken routine */
1080 * Check to see whether we are at the end of the here document. When this
1081 * is called, c is set to the first character of the next input line. If
1082 * we are at the end of the here document, this routine sets the c to PEOF.
1091 if (c == *eofmark) {
1092 if (pfgets(line, sizeof line) != NULL) {
1096 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1097 if (*p == '\n' && *q == '\0') {
1100 needprompt = doprompt;
1102 pushstring(line, strlen(line), NULL);
1107 goto checkend_return;
1112 * Parse a redirection operator. The variable "out" points to a string
1113 * specifying the fd to be redirected. The variable "c" contains the
1114 * first character of the redirection operator.
1121 np = (union node *)stalloc(sizeof (struct nfile));
1130 np->type = NCLOBBER;
1135 } else { /* c == '<' */
1139 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1140 np = (union node *)stalloc(sizeof (struct nhere));
1144 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1146 if ((c = pgetc()) == '-') {
1147 heredoc->striptabs = 1;
1149 heredoc->striptabs = 0;
1152 } else if (c == '&')
1162 np->nfile.fd = digit_val(fd);
1164 goto parseredir_return;
1169 * Parse a substitution. At this point, we have read the dollar sign
1179 static const char types[] = "}-+?=";
1180 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1186 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1190 } else if (c == '(') { /* $(command) or $((arith)) */
1191 if (pgetc() == '(') {
1198 USTPUTC(CTLVAR, out);
1199 typeloc = out - stackblock();
1200 USTPUTC(VSNORMAL, out);
1207 if ((c = pgetc()) == '}')
1215 if (!is_eof(c) && is_name(c)) {
1221 } while (!is_eof(c) && is_in_name(c));
1223 strncmp(out - length, "LINENO", length) == 0) {
1224 /* Replace the variable name with the
1225 * current line number. */
1228 linno -= funclinno - 1;
1229 snprintf(buf, sizeof(buf), "%d", linno);
1231 for (i = 0; buf[i] != '\0'; i++)
1232 STPUTC(buf[i], out);
1235 } else if (is_digit(c)) {
1236 if (bracketed_name) {
1240 } while (is_digit(c));
1246 if (! is_special(c)) {
1264 p = strchr(types, c);
1271 subtype = p - types + VSNORMAL;
1277 subtype = c == '#' ? VSTRIMLEFT :
1287 } else if (subtype != VSERROR) {
1291 if (subtype != VSLENGTH && (dblquote || arinest))
1293 *(stackblock() + typeloc) = subtype | flags;
1294 if (subtype != VSNORMAL)
1297 goto parsesub_return;
1302 * Called to parse command substitutions. Newstyle is set if the command
1303 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1304 * list of commands (passed by reference), and savelen is the number of
1305 * characters on the top of the stack which must be preserved.
1309 struct nodelist **nlpp;
1313 struct jmploc jmploc;
1314 struct jmploc *const savehandler = handler;
1317 const int bq_startlinno = plinno;
1319 savepbq = parsebackquote;
1320 if (setjmp(jmploc.loc)) {
1324 handler = savehandler;
1325 if (exception == EXERROR) {
1326 startlinno = bq_startlinno;
1327 synerror("Error in command substitution");
1329 longjmp(handler->loc, 1);
1333 savelen = out - stackblock();
1335 str = ckmalloc(savelen);
1336 memcpy(str, stackblock(), savelen);
1341 /* We must read until the closing backquote, giving special
1342 treatment to some slashes, and then push the string and
1343 reread it as input, interpreting it normally. */
1356 switch (c = pgetc()) {
1361 if ((c = pgetc()) == '\n') {
1368 * If eating a newline, avoid putting
1369 * the newline into the new character
1370 * stream (via the STPUTC after the
1375 if (c != '\\' && c != '`' && c != '$'
1376 && (!dblquote || c != '"'))
1382 needprompt = doprompt;
1386 startlinno = plinno;
1387 synerror("EOF in backquote substitution");
1397 savelen = out - stackblock();
1399 str = ckmalloc(savelen);
1400 memcpy(str, stackblock(), savelen);
1401 setinputstring(str, 1);
1406 nlpp = &(*nlpp)->next;
1407 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1408 (*nlpp)->next = NULL;
1409 parsebackquote = oldstyle;
1412 saveprompt = doprompt;
1419 doprompt = saveprompt;
1421 if (readtoken() != TRP)
1428 * Start reading from old file again, ignoring any pushed back
1429 * tokens left from the backquote parsing
1434 while (stackblocksize() <= savelen)
1438 memcpy(out, str, savelen);
1439 STADJUST(savelen, out);
1445 parsebackquote = savepbq;
1446 handler = savehandler;
1447 if (arinest || dblquote)
1448 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1450 USTPUTC(CTLBACKQ, out);
1452 goto parsebackq_oldreturn;
1454 goto parsebackq_newreturn;
1458 * Parse an arithmetic expansion (indicate start of one and set state)
1462 if (++arinest == 1) {
1463 prevsyntax = syntax;
1465 USTPUTC(CTLARI, out);
1472 * we collapse embedded arithmetic expansion to
1473 * parenthesis, which should be equivalent
1477 goto parsearith_return;
1480 } /* end of readtoken */
1492 * Returns true if the text contains nothing to expand (no dollar signs
1497 noexpand(char *text)
1503 while ((c = *p++) != '\0') {
1504 if ( c == CTLQUOTEMARK)
1508 else if (BASESYNTAX[(int)c] == CCTL)
1516 * Return true if the argument is a legal variable name (a letter or
1517 * underscore followed by zero or more letters, underscores, and digits).
1521 goodname(const char *name)
1529 if (! is_in_name(*p))
1537 * Called when an unexpected token is read during the parse. The argument
1538 * is the token that is expected, or -1 if more than one type of token can
1539 * occur at this point.
1543 synexpect(int token)
1548 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1549 tokname[lasttoken], tokname[token]);
1551 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1561 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1562 outfmt(&errout, "Syntax error: %s\n", msg);
1563 error((char *)NULL);
1567 setprompt(int which)
1569 whichprompt = which;
1574 out2str(getprompt(NULL));
1578 * called by editline -- any expansions to the prompt
1579 * should be added here.
1582 getprompt(void *unused __unused)
1584 static char ps[PROMPTLEN];
1589 * Select prompt format.
1591 switch (whichprompt) {
1602 return "<internal prompt error>";
1606 * Format prompt string.
1608 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1615 * \h specifies just the local hostname,
1616 * \H specifies fully-qualified hostname.
1621 gethostname(&ps[i], PROMPTLEN - i);
1622 /* Skip to end of hostname. */
1623 trim = (*fmt == 'h') ? '.' : '\0';
1624 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1629 * Working directory.
1631 * \W specifies just the final component,
1632 * \w specifies the entire path.
1637 getcwd(&ps[i], PROMPTLEN - i);
1638 if (*fmt == 'W' && ps[i + 1] != '\0') {
1639 /* Final path component only. */
1641 for (j = i; ps[j] != '\0'; j++)
1644 memmove(&ps[i], &ps[trim],
1647 /* Skip to end of path. */
1648 while (ps[i + 1] != '\0')
1655 * '$' for normal users, '#' for root.
1658 ps[i] = (geteuid() != 0) ? '$' : '#';
1669 * Emit unrecognized formats verbatim.