]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/parser.c
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid
[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/parser.c 326025 2017-11-20 19:49:47Z pfg $");
50 #include "expand.h" /* defines rmescapes() */
62 #include "exec.h" /* to check for special builtins */
64 #include "myhistedit.h"
68 * Shell command parser.
73 /* values of checkkwd variable */
78 /* values returned by readtoken */
84 struct heredoc
*next
; /* next here document in list */
85 union node
*here
; /* redirection node */
86 char *eofmark
; /* string indicating end of input */
87 int striptabs
; /* if set, strip leading tabs */
91 struct parser_temp
*next
;
96 static struct heredoc
*heredoclist
; /* list of here documents to read */
97 static int doprompt
; /* if set, prompt the user */
98 static int needprompt
; /* true if interactive and at start of line */
99 static int lasttoken
; /* last token read */
100 static int tokpushback
; /* last token pushed back */
101 static char *wordtext
; /* text of last word returned by readtoken */
103 static struct nodelist
*backquotelist
;
104 static union node
*redirnode
;
105 static struct heredoc
*heredoc
;
106 static int quoteflag
; /* set if (part of) last token was quoted */
107 static int startlinno
; /* line # where last token started */
108 static int funclinno
; /* line # where the current function started */
109 static struct parser_temp
*parser_temp
;
111 #define NOEOFMARK ((const char *)&heredoclist)
114 static union node
*list(int);
115 static union node
*andor(void);
116 static union node
*pipeline(void);
117 static union node
*command(void);
118 static union node
*simplecmd(union node
**, union node
*);
119 static union node
*makename(void);
120 static union node
*makebinary(int type
, union node
*n1
, union node
*n2
);
121 static void parsefname(void);
122 static void parseheredoc(void);
123 static int peektoken(void);
124 static int readtoken(void);
125 static int xxreadtoken(void);
126 static int readtoken1(int, const char *, const char *, int);
127 static int noexpand(char *);
128 static void consumetoken(int);
129 static void synexpect(int) __dead2
;
130 static void synerror(const char *) __dead2
;
131 static void setprompt(int);
132 static int pgetc_linecont(void);
136 parser_temp_alloc(size_t len
)
138 struct parser_temp
*t
;
141 t
= ckmalloc(sizeof(*t
));
143 t
->next
= parser_temp
;
145 t
->data
= ckmalloc(len
);
152 parser_temp_realloc(void *ptr
, size_t len
)
154 struct parser_temp
*t
;
159 error("bug: parser_temp_realloc misused");
160 t
->data
= ckrealloc(t
->data
, len
);
167 parser_temp_free_upto(void *ptr
)
169 struct parser_temp
*t
;
173 while (parser_temp
!= NULL
&& !done
) {
175 parser_temp
= t
->next
;
176 done
= t
->data
== ptr
;
182 error("bug: parser_temp_free_upto misused");
187 parser_temp_free_all(void)
189 struct parser_temp
*t
;
192 while (parser_temp
!= NULL
) {
194 parser_temp
= t
->next
;
203 * Read and parse a command. Returns NEOF on end of file. (NULL is a
204 * valid parse tree indicating a blank line.)
208 parsecmd(int interact
)
212 /* This assumes the parser is not re-entered,
213 * which could happen if we add command substitution on PS1/PS2.
215 parser_temp_free_all();
237 * Read and parse words for wordexp.
238 * Returns a list of NARG nodes; NULL if there are no words.
243 union node
*n
, *first
= NULL
, **pnext
;
246 /* This assumes the parser is not re-entered,
247 * which could happen if we add command substitution on PS1/PS2.
249 parser_temp_free_all();
258 while ((t
= readtoken()) != TEOF
) {
263 pnext
= &n
->narg
.next
;
272 union node
*ntop
, *n1
, *n2
, *n3
;
275 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
276 if (!nlflag
&& tokendlist
[peektoken()])
282 if (tok
== TBACKGND
) {
283 if (n2
!= NULL
&& n2
->type
== NPIPE
) {
284 n2
->npipe
.backgnd
= 1;
285 } else if (n2
!= NULL
&& n2
->type
== NREDIR
) {
288 n3
= (union node
*)stalloc(sizeof (struct nredir
));
291 n3
->nredir
.redirect
= NULL
;
297 else if (n1
== NULL
) {
298 n1
= makebinary(NSEMI
, ntop
, n2
);
302 n3
= makebinary(NSEMI
, n1
->nbinary
.ch2
, n2
);
303 n1
->nbinary
.ch2
= n3
;
316 } else if (tok
== TEOF
&& nlflag
) {
322 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
323 if (!nlflag
&& tokendlist
[peektoken()])
330 pungetc(); /* push back EOF on input */
351 if ((t
= readtoken()) == TAND
) {
353 } else if (t
== TOR
) {
359 n
= makebinary(t
, n
, pipeline());
368 union node
*n1
, *n2
, *pipenode
;
369 struct nodelist
*lp
, *prev
;
373 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
374 TRACE(("pipeline: entered\n"));
375 while (readtoken() == TNOT
)
379 if (readtoken() == TPIPE
) {
380 pipenode
= (union node
*)stalloc(sizeof (struct npipe
));
381 pipenode
->type
= NPIPE
;
382 pipenode
->npipe
.backgnd
= 0;
383 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
384 pipenode
->npipe
.cmdlist
= lp
;
388 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
389 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
397 } while (readtoken() == TPIPE
);
403 n2
= (union node
*)stalloc(sizeof (struct nnot
));
417 union node
*ap
, **app
;
418 union node
*cp
, **cpp
;
419 union node
*redir
, **rpp
;
423 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
429 /* Check for redirection which may precede command */
430 while (readtoken() == TREDIR
) {
431 *rpp
= n2
= redirnode
;
432 rpp
= &n2
->nfile
.next
;
437 switch (readtoken()) {
439 n1
= (union node
*)stalloc(sizeof (struct nif
));
441 if ((n1
->nif
.test
= list(0)) == NULL
)
444 n1
->nif
.ifpart
= list(0);
446 while (readtoken() == TELIF
) {
447 n2
->nif
.elsepart
= (union node
*)stalloc(sizeof (struct nif
));
448 n2
= n2
->nif
.elsepart
;
450 if ((n2
->nif
.test
= list(0)) == NULL
)
453 n2
->nif
.ifpart
= list(0);
455 if (lasttoken
== TELSE
)
456 n2
->nif
.elsepart
= list(0);
458 n2
->nif
.elsepart
= NULL
;
462 checkkwd
= CHKKWD
| CHKALIAS
;
467 if ((n1
= list(0)) == NULL
)
470 n1
= makebinary((t
== TWHILE
)? NWHILE
: NUNTIL
, n1
, list(0));
472 checkkwd
= CHKKWD
| CHKALIAS
;
475 if (readtoken() != TWORD
|| quoteflag
|| ! goodname(wordtext
))
476 synerror("Bad for loop variable");
477 n1
= (union node
*)stalloc(sizeof (struct nfor
));
479 n1
->nfor
.var
= wordtext
;
480 while (readtoken() == TNL
)
482 if (lasttoken
== TWORD
&& ! quoteflag
&& equal(wordtext
, "in")) {
484 while (readtoken() == TWORD
) {
487 app
= &n2
->narg
.next
;
491 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
494 static char argvars
[5] = {
495 CTLVAR
, VSNORMAL
|VSQUOTE
, '@', '=', '\0'
497 n2
= (union node
*)stalloc(sizeof (struct narg
));
499 n2
->narg
.text
= argvars
;
500 n2
->narg
.backquote
= NULL
;
501 n2
->narg
.next
= NULL
;
504 * Newline or semicolon here is optional (but note
505 * that the original Bourne shell only allowed NL).
507 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
510 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
511 if ((t
= readtoken()) == TDO
)
513 else if (t
== TBEGIN
)
517 n1
->nfor
.body
= list(0);
519 checkkwd
= CHKKWD
| CHKALIAS
;
522 n1
= (union node
*)stalloc(sizeof (struct ncase
));
525 n1
->ncase
.expr
= makename();
526 while (readtoken() == TNL
);
527 if (lasttoken
!= TWORD
|| ! equal(wordtext
, "in"))
528 synerror("expecting \"in\"");
529 cpp
= &n1
->ncase
.cases
;
530 checkkwd
= CHKNL
| CHKKWD
, readtoken();
531 while (lasttoken
!= TESAC
) {
532 *cpp
= cp
= (union node
*)stalloc(sizeof (struct nclist
));
534 app
= &cp
->nclist
.pattern
;
535 if (lasttoken
== TLP
)
538 *app
= ap
= makename();
539 checkkwd
= CHKNL
| CHKKWD
;
540 if (readtoken() != TPIPE
)
542 app
= &ap
->narg
.next
;
545 ap
->narg
.next
= NULL
;
546 if (lasttoken
!= TRP
)
548 cp
->nclist
.body
= list(0);
550 checkkwd
= CHKNL
| CHKKWD
| CHKALIAS
;
551 if ((t
= readtoken()) != TESAC
) {
554 else if (t
== TFALLTHRU
)
555 cp
->type
= NCLISTFALLTHRU
;
558 checkkwd
= CHKNL
| CHKKWD
, readtoken();
560 cpp
= &cp
->nclist
.next
;
563 checkkwd
= CHKKWD
| CHKALIAS
;
566 n1
= (union node
*)stalloc(sizeof (struct nredir
));
567 n1
->type
= NSUBSHELL
;
568 n1
->nredir
.n
= list(0);
569 n1
->nredir
.redirect
= NULL
;
571 checkkwd
= CHKKWD
| CHKALIAS
;
577 checkkwd
= CHKKWD
| CHKALIAS
;
579 /* A simple command must have at least one redirection or word. */
594 n1
= simplecmd(rpp
, redir
);
600 /* Now check for redirection which may follow command */
601 while (readtoken() == TREDIR
) {
602 *rpp
= n2
= redirnode
;
603 rpp
= &n2
->nfile
.next
;
610 n2
= (union node
*)stalloc(sizeof (struct nredir
));
615 n1
->nredir
.redirect
= redir
;
623 simplecmd(union node
**rpp
, union node
*redir
)
625 union node
*args
, **app
;
626 union node
**orig_rpp
= rpp
;
627 union node
*n
= NULL
;
631 /* If we don't have any redirections already, then we must reset */
632 /* rpp to be the address of the local redir variable. */
639 * We save the incoming value, because we need this for shell
640 * functions. There can not be a redirect or an argument between
641 * the function name and the open parenthesis.
645 savecheckkwd
= CHKALIAS
;
648 checkkwd
= savecheckkwd
;
649 if (readtoken() == TWORD
) {
653 if (savecheckkwd
!= 0 && !isassignment(wordtext
))
655 } else if (lasttoken
== TREDIR
) {
656 *rpp
= n
= redirnode
;
657 rpp
= &n
->nfile
.next
;
658 parsefname(); /* read name of redirection file */
659 } else if (lasttoken
== TLP
&& app
== &args
->narg
.next
660 && rpp
== orig_rpp
) {
661 /* We have a function */
665 * - Require plain text.
666 * - Functions with '/' cannot be called.
668 * - Reject ksh extended glob patterns.
670 if (!noexpand(n
->narg
.text
) || quoteflag
||
671 strchr(n
->narg
.text
, '/') ||
673 n
->narg
.text
[strlen(n
->narg
.text
) - 1]))
674 synerror("Bad function name");
675 rmescapes(n
->narg
.text
);
676 if (find_builtin(n
->narg
.text
, &special
) >= 0 &&
678 synerror("Cannot override a special builtin with a function");
680 n
->narg
.next
= command();
690 n
= (union node
*)stalloc(sizeof (struct ncmd
));
693 n
->ncmd
.redirect
= redir
;
702 n
= (union node
*)stalloc(sizeof (struct narg
));
705 n
->narg
.text
= wordtext
;
706 n
->narg
.backquote
= backquotelist
;
711 makebinary(int type
, union node
*n1
, union node
*n2
)
715 n
= (union node
*)stalloc(sizeof (struct nbinary
));
725 checkkwd
|= CHKALIAS
;
729 fixredir(union node
*n
, const char *text
, int err
)
731 TRACE(("Fix redir %s %d\n", text
, err
));
733 n
->ndup
.vname
= NULL
;
735 if (is_digit(text
[0]) && text
[1] == '\0')
736 n
->ndup
.dupfd
= digit_val(text
[0]);
737 else if (text
[0] == '-' && text
[1] == '\0')
742 synerror("Bad fd number");
744 n
->ndup
.vname
= makename();
752 union node
*n
= redirnode
;
755 if (n
->type
== NHERE
) {
756 struct heredoc
*here
= heredoc
;
761 TRACE(("Here document %d\n", n
->type
));
762 if (here
->striptabs
) {
763 while (*wordtext
== '\t')
766 if (! noexpand(wordtext
))
767 synerror("Illegal eof marker for << redirection");
769 here
->eofmark
= wordtext
;
771 if (heredoclist
== NULL
)
774 for (p
= heredoclist
; p
->next
; p
= p
->next
);
777 } else if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
778 fixredir(n
, wordtext
, 0);
780 n
->nfile
.fname
= makename();
786 * Input any here documents.
792 struct heredoc
*here
;
795 while (heredoclist
) {
797 heredoclist
= here
->next
;
802 readtoken1(pgetc(), here
->here
->type
== NHERE
? SQSYNTAX
: DQSYNTAX
,
803 here
->eofmark
, here
->striptabs
);
805 here
->here
->nhere
.doc
= n
;
825 int alreadyseen
= tokpushback
;
834 if (checkkwd
& CHKNL
) {
842 * check for keywords and aliases
844 if (t
== TWORD
&& !quoteflag
)
846 const char * const *pp
;
848 if (checkkwd
& CHKKWD
)
849 for (pp
= parsekwd
; *pp
; pp
++) {
850 if (**pp
== *wordtext
&& equal(*pp
, wordtext
))
852 lasttoken
= t
= pp
- parsekwd
+ KWDOFFSET
;
853 TRACE(("keyword %s recognized\n", tokname
[t
]));
857 if (checkkwd
& CHKALIAS
&&
858 (ap
= lookupalias(wordtext
, 1)) != NULL
) {
859 pushstring(ap
->val
, strlen(ap
->val
), ap
);
869 TRACE(("token %s %s\n", tokname
[t
], t
== TWORD
? wordtext
: ""));
871 TRACE(("reread token %s %s\n", tokname
[t
], t
== TWORD
? wordtext
: ""));
878 * Read the next input token.
879 * If the token is a word, we set backquotelist to the list of cmds in
880 * backquotes. We set quoteflag to true if any part of the word was
882 * If the token is TREDIR, then we set redirnode to a structure containing
884 * In all cases, the variable startlinno is set to the number of the line
885 * on which the token starts.
887 * [Change comment: here documents and internal procedures]
888 * [Readtoken shouldn't have any arguments. Perhaps we should make the
889 * word parsing code into a separate routine. In this case, readtoken
890 * doesn't need to have any internal procedures, but parseword does.
891 * We could also make parseoperator in essence the main routine, and
892 * have parseword (readtoken1?) handle both words and redirection.]
895 #define RETURN(token) return lasttoken = token
911 for (;;) { /* until token or start of word found */
917 while ((c
= pgetc()) != '\n' && c
!= PEOF
);
921 if (pgetc() == '\n') {
922 startlinno
= ++plinno
;
932 return readtoken1(c
, BASESYNTAX
, (char *)NULL
, 0);
935 needprompt
= doprompt
;
940 if (pgetc_linecont() == '&')
945 if (pgetc_linecont() == '|')
950 c
= pgetc_linecont();
967 #define MAXNEST_static 8
970 const char *syntax
; /* *SYNTAX */
971 int parenlevel
; /* levels of parentheses in arithmetic */
972 enum tokenstate_category
975 TSTATE_VAR_OLD
, /* ${var+-=?}, inherits dquotes */
976 TSTATE_VAR_NEW
, /* other ${var...}, own dquote state */
983 * Check to see whether we are at the end of the here document. When this
984 * is called, c is set to the first character of the next input line. If
985 * we are at the end of the here document, this routine sets the c to PEOF.
986 * The new value of c is returned.
990 checkend(int c
, const char *eofmark
, int striptabs
)
1000 for (q
= eofmark
+ 1; c2
= pgetc(), *q
!= '\0' && c2
== *q
; q
++)
1002 if ((c2
== PEOF
|| c2
== '\n') && *q
== '\0') {
1006 needprompt
= doprompt
;
1010 pushstring(eofmark
+ 1, q
- (eofmark
+ 1), NULL
);
1012 } else if (c
== '\n' && *eofmark
== '\0') {
1015 needprompt
= doprompt
;
1022 * Parse a redirection operator. The variable "out" points to a string
1023 * specifying the fd to be redirected. The variable "c" contains the
1024 * first character of the redirection operator.
1028 parseredir(char *out
, int c
)
1033 np
= (union node
*)stalloc(sizeof (struct nfile
));
1036 c
= pgetc_linecont();
1042 np
->type
= NCLOBBER
;
1047 } else { /* c == '<' */
1049 c
= pgetc_linecont();
1051 if (sizeof (struct nfile
) != sizeof (struct nhere
)) {
1052 np
= (union node
*)stalloc(sizeof (struct nhere
));
1056 heredoc
= (struct heredoc
*)stalloc(sizeof (struct heredoc
));
1058 if ((c
= pgetc_linecont()) == '-') {
1059 heredoc
->striptabs
= 1;
1061 heredoc
->striptabs
= 0;
1064 } else if (c
== '&')
1074 np
->nfile
.fd
= digit_val(fd
);
1079 * Called to parse command substitutions.
1083 parsebackq(char *out
, struct nodelist
**pbqlist
,
1084 int oldstyle
, int dblquote
, int quoted
)
1086 struct nodelist
**nlpp
;
1089 struct jmploc jmploc
;
1090 struct jmploc
*const savehandler
= handler
;
1093 const int bq_startlinno
= plinno
;
1094 char *volatile ostr
= NULL
;
1095 struct parsefile
*const savetopfile
= getcurrentfile();
1096 struct heredoc
*const saveheredoclist
= heredoclist
;
1097 struct heredoc
*here
;
1100 if (setjmp(jmploc
.loc
)) {
1101 popfilesupto(savetopfile
);
1106 heredoclist
= saveheredoclist
;
1107 handler
= savehandler
;
1108 if (exception
== EXERROR
) {
1109 startlinno
= bq_startlinno
;
1110 synerror("Error in command substitution");
1112 longjmp(handler
->loc
, 1);
1115 savelen
= out
- stackblock();
1117 str
= ckmalloc(savelen
);
1118 memcpy(str
, stackblock(), savelen
);
1124 /* We must read until the closing backquote, giving special
1125 treatment to some slashes, and then push the string and
1126 reread it as input, interpreting it normally. */
1132 STARTSTACKSTR(oout
);
1138 CHECKSTRSPACE(2, oout
);
1139 c
= pgetc_linecont();
1145 if (c
!= '\\' && c
!= '`' && c
!= '$'
1146 && (!dblquote
|| c
!= '"'))
1147 USTPUTC('\\', oout
);
1152 needprompt
= doprompt
;
1156 startlinno
= plinno
;
1157 synerror("EOF in backquote substitution");
1165 USTPUTC('\0', oout
);
1166 olen
= oout
- stackblock();
1168 ostr
= ckmalloc(olen
);
1169 memcpy(ostr
, stackblock(), olen
);
1170 setinputstring(ostr
, 1);
1175 nlpp
= &(*nlpp
)->next
;
1176 *nlpp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
1177 (*nlpp
)->next
= NULL
;
1180 saveprompt
= doprompt
;
1187 if (peektoken() != TEOF
)
1189 doprompt
= saveprompt
;
1196 * Start reading from old file again, ignoring any pushed back
1197 * tokens left from the backquote parsing
1203 CHECKSTRSPACE(savelen
+ 1, out
);
1206 memcpy(out
, str
, savelen
);
1207 STADJUST(savelen
, out
);
1215 here
= saveheredoclist
;
1217 while (here
->next
!= NULL
)
1219 here
->next
= heredoclist
;
1220 heredoclist
= saveheredoclist
;
1222 handler
= savehandler
;
1225 USTPUTC(CTLBACKQ
| CTLQUOTE
, out
);
1227 USTPUTC(CTLBACKQ
, out
);
1233 * Called to parse a backslash escape sequence inside $'...'.
1234 * The backslash has already been read.
1237 readcstyleesc(char *out
)
1245 synerror("Unterminated quoted string");
1258 case 'a': v
= '\a'; break;
1259 case 'b': v
= '\b'; break;
1260 case 'e': v
= '\033'; break;
1261 case 'f': v
= '\f'; break;
1262 case 'n': v
= '\n'; break;
1263 case 'r': v
= '\r'; break;
1264 case 't': v
= '\t'; break;
1265 case 'v': v
= '\v'; break;
1270 if (c
>= '0' && c
<= '9')
1271 v
= (v
<< 4) + c
- '0';
1272 else if (c
>= 'A' && c
<= 'F')
1273 v
= (v
<< 4) + c
- 'A' + 10;
1274 else if (c
>= 'a' && c
<= 'f')
1275 v
= (v
<< 4) + c
- 'a' + 10;
1281 case '0': case '1': case '2': case '3':
1282 case '4': case '5': case '6': case '7':
1285 if (c
>= '0' && c
<= '7') {
1289 if (c
>= '0' && c
<= '7') {
1299 if (c
< 0x3f || c
> 0x7a || c
== 0x60)
1300 synerror("Bad escape sequence");
1301 if (c
== '\\' && pgetc() != '\\')
1302 synerror("Bad escape sequence");
1310 n
= c
== 'U' ? 8 : 4;
1312 for (i
= 0; i
< n
; i
++) {
1314 if (c
>= '0' && c
<= '9')
1315 v
= (v
<< 4) + c
- '0';
1316 else if (c
>= 'A' && c
<= 'F')
1317 v
= (v
<< 4) + c
- 'A' + 10;
1318 else if (c
>= 'a' && c
<= 'f')
1319 v
= (v
<< 4) + c
- 'a' + 10;
1321 synerror("Bad escape sequence");
1323 if (v
== 0 || (v
>= 0xd800 && v
<= 0xdfff))
1324 synerror("Bad escape sequence");
1325 /* We really need iconv here. */
1326 if (initial_localeisutf8
&& v
> 127) {
1327 CHECKSTRSPACE(4, out
);
1329 * We cannot use wctomb() as the locale may have
1333 USTPUTC(0xc0 | v
>> 6, out
);
1334 USTPUTC(0x80 | (v
& 0x3f), out
);
1336 } else if (v
<= 0xffff) {
1337 USTPUTC(0xe0 | v
>> 12, out
);
1338 USTPUTC(0x80 | ((v
>> 6) & 0x3f), out
);
1339 USTPUTC(0x80 | (v
& 0x3f), out
);
1341 } else if (v
<= 0x10ffff) {
1342 USTPUTC(0xf0 | v
>> 18, out
);
1343 USTPUTC(0x80 | ((v
>> 12) & 0x3f), out
);
1344 USTPUTC(0x80 | ((v
>> 6) & 0x3f), out
);
1345 USTPUTC(0x80 | (v
& 0x3f), out
);
1353 synerror("Bad escape sequence");
1357 * We can't handle NUL bytes.
1358 * POSIX says we should skip till the closing quote.
1361 while ((c
= pgetc()) != '\'') {
1365 synerror("Unterminated quoted string");
1377 if (SQSYNTAX
[vc
] == CCTL
)
1378 USTPUTC(CTLESC
, out
);
1385 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
1386 * is not NULL, read a here document. In the latter case, eofmark is the
1387 * word which marks the end of the document and striptabs is true if
1388 * leading tabs should be stripped from the document. The argument firstc
1389 * is the first character of the input token or document.
1391 * Because C does not have internal subroutines, I have simulated them
1392 * using goto's to implement the subroutine linkage. The following macros
1393 * will run code that appears at the end of readtoken1.
1396 #define PARSESUB() {goto parsesub; parsesub_return:;}
1397 #define PARSEARITH() {goto parsearith; parsearith_return:;}
1400 readtoken1(int firstc
, char const *initialsyntax
, const char *eofmark
,
1406 struct nodelist
*bqlist
;
1411 struct tokenstate state_static
[MAXNEST_static
];
1412 int maxnest
= MAXNEST_static
;
1413 struct tokenstate
*state
= state_static
;
1416 startlinno
= plinno
;
1421 state
[level
].syntax
= initialsyntax
;
1422 state
[level
].parenlevel
= 0;
1423 state
[level
].category
= TSTATE_TOP
;
1426 loop
: { /* for each line, until end of word */
1427 if (eofmark
&& eofmark
!= NOEOFMARK
)
1428 /* set c to PEOF if at end of here document */
1429 c
= checkend(c
, eofmark
, striptabs
);
1430 for (;;) { /* until end of line or end of word */
1431 CHECKSTRSPACE(4, out
); /* permit 4 calls to USTPUTC */
1433 synentry
= state
[level
].syntax
[c
];
1436 case CNL
: /* '\n' */
1437 if (state
[level
].syntax
== BASESYNTAX
)
1438 goto endword
; /* exit outer loop */
1446 goto loop
; /* continue outer loop */
1449 out
= readcstyleesc(out
);
1457 if (eofmark
== NULL
|| initialsyntax
!= SQSYNTAX
)
1458 USTPUTC(CTLESC
, out
);
1461 case CBACK
: /* backslash */
1466 } else if (c
== '\n') {
1473 if (state
[level
].syntax
== DQSYNTAX
&&
1474 c
!= '\\' && c
!= '`' && c
!= '$' &&
1475 (c
!= '"' || (eofmark
!= NULL
&&
1476 newvarnest
== 0)) &&
1477 (c
!= '}' || state
[level
].category
!= TSTATE_VAR_OLD
))
1479 if ((eofmark
== NULL
||
1481 state
[level
].syntax
== BASESYNTAX
)
1482 USTPUTC(CTLQUOTEMARK
, out
);
1483 if (SQSYNTAX
[c
] == CCTL
)
1484 USTPUTC(CTLESC
, out
);
1486 if ((eofmark
== NULL
||
1488 state
[level
].syntax
== BASESYNTAX
&&
1489 state
[level
].category
== TSTATE_VAR_OLD
)
1490 USTPUTC(CTLQUOTEEND
, out
);
1495 USTPUTC(CTLQUOTEMARK
, out
);
1496 state
[level
].syntax
= SQSYNTAX
;
1500 USTPUTC(CTLQUOTEMARK
, out
);
1501 state
[level
].syntax
= DQSYNTAX
;
1504 if (eofmark
!= NULL
&& newvarnest
== 0)
1507 if (state
[level
].category
== TSTATE_VAR_OLD
)
1508 USTPUTC(CTLQUOTEEND
, out
);
1509 state
[level
].syntax
= BASESYNTAX
;
1513 case CVAR
: /* '$' */
1514 PARSESUB(); /* parse substitution */
1516 case CENDVAR
: /* '}' */
1518 ((state
[level
].category
== TSTATE_VAR_OLD
&&
1519 state
[level
].syntax
==
1520 state
[level
- 1].syntax
) ||
1521 (state
[level
].category
== TSTATE_VAR_NEW
&&
1522 state
[level
].syntax
== BASESYNTAX
))) {
1523 if (state
[level
].category
== TSTATE_VAR_NEW
)
1526 USTPUTC(CTLENDVAR
, out
);
1531 case CLP
: /* '(' in arithmetic */
1532 state
[level
].parenlevel
++;
1535 case CRP
: /* ')' in arithmetic */
1536 if (state
[level
].parenlevel
> 0) {
1538 --state
[level
].parenlevel
;
1540 if (pgetc_linecont() == ')') {
1542 state
[level
].category
== TSTATE_ARITH
) {
1544 USTPUTC(CTLENDARI
, out
);
1550 * (don't 2nd guess - no error)
1557 case CBQUOTE
: /* '`' */
1558 out
= parsebackq(out
, &bqlist
, 1,
1559 state
[level
].syntax
== DQSYNTAX
&&
1560 (eofmark
== NULL
|| newvarnest
> 0),
1561 state
[level
].syntax
== DQSYNTAX
|| state
[level
].syntax
== ARISYNTAX
);
1564 goto endword
; /* exit outer loop */
1569 goto endword
; /* exit outer loop */
1576 if (state
[level
].syntax
== ARISYNTAX
)
1577 synerror("Missing '))'");
1578 if (state
[level
].syntax
!= BASESYNTAX
&& eofmark
== NULL
)
1579 synerror("Unterminated quoted string");
1580 if (state
[level
].category
== TSTATE_VAR_OLD
||
1581 state
[level
].category
== TSTATE_VAR_NEW
) {
1582 startlinno
= plinno
;
1583 synerror("Missing '}'");
1585 if (state
!= state_static
)
1586 parser_temp_free_upto(state
);
1588 len
= out
- stackblock();
1590 if (eofmark
== NULL
) {
1591 if ((c
== '>' || c
== '<')
1594 && (*out
== '\0' || is_digit(*out
))) {
1596 return lasttoken
= TREDIR
;
1602 backquotelist
= bqlist
;
1603 grabstackblock(len
);
1605 return lasttoken
= TWORD
;
1606 /* end of readtoken routine */
1610 * Parse a substitution. At this point, we have read the dollar sign
1619 static const char types
[] = "}-+?=";
1624 c
= pgetc_linecont();
1625 if (c
== '(') { /* $(command) or $((arith)) */
1626 if (pgetc_linecont() == '(') {
1630 out
= parsebackq(out
, &bqlist
, 0,
1631 state
[level
].syntax
== DQSYNTAX
&&
1632 (eofmark
== NULL
|| newvarnest
> 0),
1633 state
[level
].syntax
== DQSYNTAX
||
1634 state
[level
].syntax
== ARISYNTAX
);
1636 } else if (c
== '{' || is_name(c
) || is_special(c
)) {
1637 USTPUTC(CTLVAR
, out
);
1638 typeloc
= out
- stackblock();
1639 USTPUTC(VSNORMAL
, out
);
1643 c
= pgetc_linecont();
1647 if (!is_eof(c
) && is_name(c
)) {
1651 c
= pgetc_linecont();
1653 } while (!is_eof(c
) && is_in_name(c
));
1655 strncmp(out
- length
, "LINENO", length
) == 0) {
1656 /* Replace the variable name with the
1657 * current line number. */
1659 CHECKSTRSPACE(11, out
);
1662 linno
-= funclinno
- 1;
1663 length
= snprintf(out
, 11, "%d", linno
);
1669 } else if (is_digit(c
)) {
1670 if (subtype
!= VSNORMAL
) {
1673 c
= pgetc_linecont();
1674 } while (is_digit(c
));
1677 c
= pgetc_linecont();
1679 } else if (is_special(c
)) {
1681 c
= pgetc_linecont();
1682 if (subtype
== 0 && c1
== '#') {
1684 if (strchr(types
, c
) == NULL
&& c
!= ':' &&
1685 c
!= '#' && c
!= '%')
1688 c
= pgetc_linecont();
1689 if (c1
!= '}' && c
== '}') {
1704 else if (c
== '\n' || c
== PEOF
)
1705 synerror("Unexpected end of line in substitution");
1706 else if (BASESYNTAX
[c
] != CCTL
)
1713 c
= pgetc_linecont();
1716 p
= strchr(types
, c
);
1718 if (c
== '\n' || c
== PEOF
)
1719 synerror("Unexpected end of line in substitution");
1722 if (BASESYNTAX
[c
] != CCTL
)
1726 subtype
= p
- types
+ VSNORMAL
;
1732 subtype
= c
== '#' ? VSTRIMLEFT
:
1734 c
= pgetc_linecont();
1742 } else if (subtype
!= VSERROR
) {
1743 if (subtype
== VSLENGTH
&& c
!= '}')
1748 if (state
[level
].syntax
== DQSYNTAX
||
1749 state
[level
].syntax
== ARISYNTAX
)
1751 *(stackblock() + typeloc
) = subtype
| flags
;
1752 if (subtype
!= VSNORMAL
) {
1753 if (level
+ 1 >= maxnest
) {
1755 if (state
== state_static
) {
1756 state
= parser_temp_alloc(
1757 maxnest
* sizeof(*state
));
1758 memcpy(state
, state_static
,
1759 MAXNEST_static
* sizeof(*state
));
1761 state
= parser_temp_realloc(state
,
1762 maxnest
* sizeof(*state
));
1765 state
[level
].parenlevel
= 0;
1766 if (subtype
== VSMINUS
|| subtype
== VSPLUS
||
1767 subtype
== VSQUESTION
|| subtype
== VSASSIGN
) {
1769 * For operators that were in the Bourne shell,
1770 * inherit the double-quote state.
1772 state
[level
].syntax
= state
[level
- 1].syntax
;
1773 state
[level
].category
= TSTATE_VAR_OLD
;
1776 * The other operators take a pattern,
1777 * so go to BASESYNTAX.
1778 * Also, ' and " are now special, even
1779 * in here documents.
1781 state
[level
].syntax
= BASESYNTAX
;
1782 state
[level
].category
= TSTATE_VAR_NEW
;
1786 } else if (c
== '\'' && state
[level
].syntax
== BASESYNTAX
) {
1787 /* $'cstylequotes' */
1788 USTPUTC(CTLQUOTEMARK
, out
);
1789 state
[level
].syntax
= SQSYNTAX
;
1795 goto parsesub_return
;
1800 * Parse an arithmetic expansion (indicate start of one and set state)
1804 if (level
+ 1 >= maxnest
) {
1806 if (state
== state_static
) {
1807 state
= parser_temp_alloc(
1808 maxnest
* sizeof(*state
));
1809 memcpy(state
, state_static
,
1810 MAXNEST_static
* sizeof(*state
));
1812 state
= parser_temp_realloc(state
,
1813 maxnest
* sizeof(*state
));
1816 state
[level
].syntax
= ARISYNTAX
;
1817 state
[level
].parenlevel
= 0;
1818 state
[level
].category
= TSTATE_ARITH
;
1819 USTPUTC(CTLARI
, out
);
1820 if (state
[level
- 1].syntax
== DQSYNTAX
)
1824 goto parsearith_return
;
1827 } /* end of readtoken */
1831 * Returns true if the text contains nothing to expand (no dollar signs
1836 noexpand(char *text
)
1842 while ((c
= *p
++) != '\0') {
1843 if ( c
== CTLQUOTEMARK
)
1847 else if (BASESYNTAX
[(int)c
] == CCTL
)
1855 * Return true if the argument is a legal variable name (a letter or
1856 * underscore followed by zero or more letters, underscores, and digits).
1860 goodname(const char *name
)
1868 if (! is_in_name(*p
))
1876 isassignment(const char *p
)
1884 else if (!is_in_name(*p
))
1892 consumetoken(int token
)
1894 if (readtoken() != token
)
1900 * Called when an unexpected token is read during the parse. The argument
1901 * is the token that is expected, or -1 if more than one type of token can
1902 * occur at this point.
1906 synexpect(int token
)
1911 fmtstr(msg
, 64, "%s unexpected (expecting %s)",
1912 tokname
[lasttoken
], tokname
[token
]);
1914 fmtstr(msg
, 64, "%s unexpected", tokname
[lasttoken
]);
1921 synerror(const char *msg
)
1924 outfmt(out2
, "%s: %d: ", commandname
, startlinno
);
1926 outfmt(out2
, "%s: ", arg0
);
1927 outfmt(out2
, "Syntax error: %s\n", msg
);
1928 error((char *)NULL
);
1932 setprompt(int which
)
1934 whichprompt
= which
;
1942 out2str(getprompt(NULL
));
1948 pgetc_linecont(void)
1952 while ((c
= pgetc_macro()) == '\\') {
1962 /* Allow the backslash to be pushed back. */
1963 pushstring("\\", 1, NULL
);
1971 * called by editline -- any expansions to the prompt
1972 * should be added here.
1975 getprompt(void *unused __unused
)
1977 static char ps
[PROMPTLEN
];
1981 static char internal_error
[] = "??";
1984 * Select prompt format.
1986 switch (whichprompt
) {
1997 return internal_error
;
2001 * Format prompt string.
2003 for (i
= 0; (i
< PROMPTLEN
- 1) && (*fmt
!= '\0'); i
++, fmt
++)
2010 * \h specifies just the local hostname,
2011 * \H specifies fully-qualified hostname.
2016 gethostname(&ps
[i
], PROMPTLEN
- i
- 1);
2017 ps
[PROMPTLEN
- 1] = '\0';
2018 /* Skip to end of hostname. */
2019 trim
= (*fmt
== 'h') ? '.' : '\0';
2020 while ((ps
[i
] != '\0') && (ps
[i
] != trim
))
2026 * Working directory.
2028 * \W specifies just the final component,
2029 * \w specifies the entire path.
2033 pwd
= lookupvar("PWD");
2034 if (pwd
== NULL
|| *pwd
== '\0')
2037 *pwd
== '/' && pwd
[1] != '\0')
2038 strlcpy(&ps
[i
], strrchr(pwd
, '/') + 1,
2041 strlcpy(&ps
[i
], pwd
, PROMPTLEN
- i
);
2042 /* Skip to end of path. */
2043 while (ps
[i
+ 1] != '\0')
2050 * '$' for normal users, '#' for root.
2053 ps
[i
] = (geteuid() != 0) ? '$' : '#';
2064 * Emit unrecognized formats verbatim.
2068 if (i
< PROMPTLEN
- 2)
2080 expandstr(const char *ps
)
2083 struct jmploc jmploc
;
2084 struct jmploc
*const savehandler
= handler
;
2085 const int saveprompt
= doprompt
;
2086 struct parsefile
*const savetopfile
= getcurrentfile();
2087 struct parser_temp
*const saveparser_temp
= parser_temp
;
2088 const char *result
= NULL
;
2090 if (!setjmp(jmploc
.loc
)) {
2093 setinputstring(ps
, 1);
2095 readtoken1(pgetc(), DQSYNTAX
, NOEOFMARK
, 0);
2096 if (backquotelist
!= NULL
)
2097 error("Command substitution not allowed here");
2101 n
.narg
.text
= wordtext
;
2102 n
.narg
.backquote
= backquotelist
;
2104 expandarg(&n
, NULL
, 0);
2105 result
= stackblock();
2108 handler
= savehandler
;
2109 doprompt
= saveprompt
;
2110 popfilesupto(savetopfile
);
2111 if (parser_temp
!= saveparser_temp
) {
2112 parser_temp_free_all();
2113 parser_temp
= saveparser_temp
;
2115 if (result
!= NULL
) {
2117 } else if (exception
== EXINT
)