1 /* $NetBSD: parser.c,v 1.73 2008/11/08 00:14:05 christos Exp $ */
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
40 __RCSID("$NetBSD: parser.c,v 1.73 2008/11/08 00:14:05 christos Exp $");
49 #include "expand.h" /* defines rmescapes() */
50 #include "eval.h" /* defines commandname */
51 #include "redir.h" /* defines copyfd() */
63 #include "myhistedit.h"
67 * Shell command parser.
72 /* values returned by readtoken */
76 #define CLOSEBRACE '}'
80 struct heredoc
*next
; /* next here document in list */
81 union node
*here
; /* redirection node */
82 char *eofmark
; /* string indicating end of input */
83 int striptabs
; /* if set, strip leading tabs */
88 static int noalias
= 0; /* when set, don't handle aliases */
89 struct heredoc
*heredoclist
; /* list of here documents to read */
90 int parsebackquote
; /* nonzero if we are inside backquotes */
91 int doprompt
; /* if set, prompt the user */
92 int needprompt
; /* true if interactive and at start of line */
93 int lasttoken
; /* last token read */
94 MKINIT
int tokpushback
; /* last token pushed back */
95 char *wordtext
; /* text of last word returned by readtoken */
96 MKINIT
int checkkwd
; /* 1 == check for kwds, 2 == also eat newlines */
97 struct nodelist
*backquotelist
;
98 union node
*redirnode
;
99 struct heredoc
*heredoc
;
100 int quoteflag
; /* set if (part of) last token was quoted */
101 int startlinno
; /* line # where last token started */
104 STATIC
union node
*list(int, 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) __dead
;
118 STATIC
void synerror(const char *) __dead
;
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
)
150 list(int nlflag
, int erflag
)
152 union node
*n1
, *n2
, *n3
;
154 TRACE(("list: entered\n"));
157 if (nlflag
== 0 && tokendlist
[peektoken()])
163 if (tok
== TBACKGND
) {
164 if (n2
->type
== NCMD
|| n2
->type
== NPIPE
) {
165 n2
->ncmd
.backgnd
= 1;
166 } else if (n2
->type
== NREDIR
) {
169 n3
= (union node
*)stalloc(sizeof (struct nredir
));
172 n3
->nredir
.redirect
= NULL
;
180 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
182 n3
->nbinary
.ch1
= n1
;
183 n3
->nbinary
.ch2
= n2
;
200 if (tokendlist
[peektoken()])
207 pungetc(); /* push back EOF on input */
210 if (nlflag
|| erflag
)
223 union node
*n1
, *n2
, *n3
;
226 TRACE(("andor: entered\n"));
229 if ((t
= readtoken()) == TAND
) {
231 } else if (t
== TOR
) {
238 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
240 n3
->nbinary
.ch1
= n1
;
241 n3
->nbinary
.ch2
= n2
;
251 union node
*n1
, *n2
, *pipenode
;
252 struct nodelist
*lp
, *prev
;
255 TRACE(("pipeline: entered\n"));
259 while (readtoken() == TNOT
) {
260 TRACE(("pipeline: TNOT recognized\n"));
265 if (readtoken() == TPIPE
) {
266 pipenode
= (union node
*)stalloc(sizeof (struct npipe
));
267 pipenode
->type
= NPIPE
;
268 pipenode
->npipe
.backgnd
= 0;
269 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
270 pipenode
->npipe
.cmdlist
= lp
;
274 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
277 } while (readtoken() == TPIPE
);
283 TRACE(("negate pipeline\n"));
284 n2
= (union node
*)stalloc(sizeof (struct nnot
));
298 union node
*ap
, **app
;
299 union node
*cp
, **cpp
;
300 union node
*redir
, **rpp
;
303 TRACE(("command: entered\n"));
310 /* Check for redirection which may precede command */
311 while (readtoken() == TREDIR
) {
312 *rpp
= n2
= redirnode
;
313 rpp
= &n2
->nfile
.next
;
318 while (readtoken() == TNOT
) {
319 TRACE(("command: TNOT recognized\n"));
324 switch (readtoken()) {
326 n1
= (union node
*)stalloc(sizeof (struct nif
));
328 n1
->nif
.test
= list(0, 0);
329 if (readtoken() != TTHEN
)
331 n1
->nif
.ifpart
= list(0, 0);
333 while (readtoken() == TELIF
) {
334 n2
->nif
.elsepart
= (union node
*)stalloc(sizeof (struct nif
));
335 n2
= n2
->nif
.elsepart
;
337 n2
->nif
.test
= list(0, 0);
338 if (readtoken() != TTHEN
)
340 n2
->nif
.ifpart
= list(0, 0);
342 if (lasttoken
== TELSE
)
343 n2
->nif
.elsepart
= list(0, 0);
345 n2
->nif
.elsepart
= NULL
;
348 if (readtoken() != TFI
)
355 n1
= (union node
*)stalloc(sizeof (struct nbinary
));
356 n1
->type
= (lasttoken
== TWHILE
)? NWHILE
: NUNTIL
;
357 n1
->nbinary
.ch1
= list(0, 0);
358 if ((got
=readtoken()) != TDO
) {
359 TRACE(("expecting DO got %s %s\n", tokname
[got
], got
== TWORD
? wordtext
: ""));
362 n1
->nbinary
.ch2
= list(0, 0);
363 if (readtoken() != TDONE
)
369 if (readtoken() != TWORD
|| quoteflag
|| ! goodname(wordtext
))
370 synerror("Bad for loop variable");
371 n1
= (union node
*)stalloc(sizeof (struct nfor
));
373 n1
->nfor
.var
= wordtext
;
374 if (readtoken() == TWORD
&& ! quoteflag
&& equal(wordtext
, "in")) {
376 while (readtoken() == TWORD
) {
377 n2
= (union node
*)stalloc(sizeof (struct narg
));
379 n2
->narg
.text
= wordtext
;
380 n2
->narg
.backquote
= backquotelist
;
382 app
= &n2
->narg
.next
;
386 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
389 static char argvars
[5] = {CTLVAR
, VSNORMAL
|VSQUOTE
,
391 n2
= (union node
*)stalloc(sizeof (struct narg
));
393 n2
->narg
.text
= argvars
;
394 n2
->narg
.backquote
= NULL
;
395 n2
->narg
.next
= NULL
;
398 * Newline or semicolon here is optional (but note
399 * that the original Bourne shell only allowed NL).
401 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
405 if ((t
= readtoken()) == TDO
)
407 else if (t
== TBEGIN
)
411 n1
->nfor
.body
= list(0, 0);
412 if (readtoken() != t
)
417 n1
= (union node
*)stalloc(sizeof (struct ncase
));
419 if (readtoken() != TWORD
)
421 n1
->ncase
.expr
= n2
= (union node
*)stalloc(sizeof (struct narg
));
423 n2
->narg
.text
= wordtext
;
424 n2
->narg
.backquote
= backquotelist
;
425 n2
->narg
.next
= NULL
;
426 while (readtoken() == TNL
);
427 if (lasttoken
!= TWORD
|| ! equal(wordtext
, "in"))
428 synerror("expecting \"in\"");
429 cpp
= &n1
->ncase
.cases
;
431 checkkwd
= 2, readtoken();
433 *cpp
= cp
= (union node
*)stalloc(sizeof (struct nclist
));
434 if (lasttoken
== TLP
)
437 app
= &cp
->nclist
.pattern
;
439 *app
= ap
= (union node
*)stalloc(sizeof (struct narg
));
441 ap
->narg
.text
= wordtext
;
442 ap
->narg
.backquote
= backquotelist
;
443 if (checkkwd
= 2, readtoken() != TPIPE
)
445 app
= &ap
->narg
.next
;
448 ap
->narg
.next
= NULL
;
450 if (lasttoken
!= TRP
) {
453 cp
->nclist
.body
= list(0, 0);
456 if ((t
= readtoken()) != TESAC
) {
466 cpp
= &cp
->nclist
.next
;
467 } while(lasttoken
!= TESAC
);
473 n1
= (union node
*)stalloc(sizeof (struct nredir
));
474 n1
->type
= NSUBSHELL
;
475 n1
->nredir
.n
= list(0, 0);
476 n1
->nredir
.redirect
= NULL
;
477 if (readtoken() != TRP
)
483 if (readtoken() != TEND
)
487 /* Handle an empty command like other simple commands. */
490 * An empty command before a ; doesn't make much sense, and
491 * should certainly be disallowed in the case of `if ;'.
502 n1
= simplecmd(rpp
, redir
);
509 /* Now check for redirection which may follow command */
510 while (readtoken() == TREDIR
) {
511 *rpp
= n2
= redirnode
;
512 rpp
= &n2
->nfile
.next
;
518 if (n1
->type
!= NSUBSHELL
) {
519 n2
= (union node
*)stalloc(sizeof (struct nredir
));
524 n1
->nredir
.redirect
= redir
;
529 TRACE(("negate command\n"));
530 n2
= (union node
*)stalloc(sizeof (struct nnot
));
541 simplecmd(union node
**rpp
, union node
*redir
)
543 union node
*args
, **app
;
544 union node
**orig_rpp
= rpp
;
545 union node
*n
= NULL
, *n2
;
548 /* If we don't have any redirections already, then we must reset */
549 /* rpp to be the address of the local redir variable. */
556 * We save the incoming value, because we need this for shell
557 * functions. There can not be a redirect or an argument between
558 * the function name and the open parenthesis.
562 while (readtoken() == TNOT
) {
563 TRACE(("simplcmd: TNOT recognized\n"));
569 if (readtoken() == TWORD
) {
570 n
= (union node
*)stalloc(sizeof (struct narg
));
572 n
->narg
.text
= wordtext
;
573 n
->narg
.backquote
= backquotelist
;
576 } else if (lasttoken
== TREDIR
) {
577 *rpp
= n
= redirnode
;
578 rpp
= &n
->nfile
.next
;
579 parsefname(); /* read name of redirection file */
580 } else if (lasttoken
== TLP
&& app
== &args
->narg
.next
581 && rpp
== orig_rpp
) {
582 /* We have a function */
583 if (readtoken() != TRP
)
585 rmescapes(n
->narg
.text
);
586 if (!goodname(n
->narg
.text
))
587 synerror("Bad function name");
589 n
->narg
.next
= command();
598 n
= (union node
*)stalloc(sizeof (struct ncmd
));
602 n
->ncmd
.redirect
= redir
;
606 TRACE(("negate simplecmd\n"));
607 n2
= (union node
*)stalloc(sizeof (struct nnot
));
621 n
= (union node
*)stalloc(sizeof (struct narg
));
624 n
->narg
.text
= wordtext
;
625 n
->narg
.backquote
= backquotelist
;
629 void fixredir(union node
*n
, const char *text
, int err
)
631 TRACE(("Fix redir %s %d\n", text
, err
));
633 n
->ndup
.vname
= NULL
;
635 if (is_digit(text
[0]) && text
[1] == '\0')
636 n
->ndup
.dupfd
= digit_val(text
[0]);
637 else if (text
[0] == '-' && text
[1] == '\0')
642 synerror("Bad fd number");
644 n
->ndup
.vname
= makename();
652 union node
*n
= redirnode
;
654 if (readtoken() != TWORD
)
656 if (n
->type
== NHERE
) {
657 struct heredoc
*here
= heredoc
;
663 TRACE(("Here document %d\n", n
->type
));
664 if (here
->striptabs
) {
665 while (*wordtext
== '\t')
668 if (! noexpand(wordtext
) || (i
= strlen(wordtext
)) == 0 || i
> EOFMARKLEN
)
669 synerror("Illegal eof marker for << redirection");
671 here
->eofmark
= wordtext
;
673 if (heredoclist
== NULL
)
676 for (p
= heredoclist
; p
->next
; p
= p
->next
);
679 } else if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
680 fixredir(n
, wordtext
, 0);
682 n
->nfile
.fname
= makename();
688 * Input any here documents.
694 struct heredoc
*here
;
697 while (heredoclist
) {
699 heredoclist
= here
->next
;
704 readtoken1(pgetc(), here
->here
->type
== NHERE
? SQSYNTAX
: DQSYNTAX
,
705 here
->eofmark
, here
->striptabs
);
706 n
= (union node
*)stalloc(sizeof (struct narg
));
709 n
->narg
.text
= wordtext
;
710 n
->narg
.backquote
= backquotelist
;
711 here
->here
->nhere
.doc
= n
;
729 int savecheckkwd
= checkkwd
;
731 int alreadyseen
= tokpushback
;
751 * check for keywords and aliases
753 if (t
== TWORD
&& !quoteflag
)
755 const char *const *pp
;
757 for (pp
= parsekwd
; *pp
; pp
++) {
758 if (**pp
== *wordtext
&& equal(*pp
, wordtext
))
761 parsekwd
+ KWDOFFSET
;
762 TRACE(("keyword %s recognized\n", tokname
[t
]));
767 (ap
= lookupalias(wordtext
, 1)) != NULL
) {
768 pushstring(ap
->val
, strlen(ap
->val
), ap
);
769 checkkwd
= savecheckkwd
;
774 checkkwd
= (t
== TNOT
) ? savecheckkwd
: 0;
776 TRACE(("%stoken %s %s\n", alreadyseen
? "reread " : "", tokname
[t
], t
== TWORD
? wordtext
: ""));
782 * Read the next input token.
783 * If the token is a word, we set backquotelist to the list of cmds in
784 * backquotes. We set quoteflag to true if any part of the word was
786 * If the token is TREDIR, then we set redirnode to a structure containing
788 * In all cases, the variable startlinno is set to the number of the line
789 * on which the token starts.
791 * [Change comment: here documents and internal procedures]
792 * [Readtoken shouldn't have any arguments. Perhaps we should make the
793 * word parsing code into a separate routine. In this case, readtoken
794 * doesn't need to have any internal procedures, but parseword does.
795 * We could also make parseoperator in essence the main routine, and
796 * have parseword (readtoken1?) handle both words and redirection.]
799 #define RETURN(token) return lasttoken = token
815 for (;;) { /* until token or start of word found */
817 if (c
== ' ' || c
== '\t')
818 continue; /* quick check for white space first */
823 while ((c
= pgetc()) != '\n' && c
!= PEOF
);
827 if (pgetc() == '\n') {
828 startlinno
= ++plinno
;
839 needprompt
= doprompt
;
867 return readtoken1(c
, BASESYNTAX
, (char *)NULL
, 0);
874 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
875 * is not NULL, read a here document. In the latter case, eofmark is the
876 * word which marks the end of the document and striptabs is true if
877 * leading tabs should be stripped from the document. The argument firstc
878 * is the first character of the input token or document.
880 * Because C does not have internal subroutines, I have simulated them
881 * using goto's to implement the subroutine linkage. The following macros
882 * will run code that appears at the end of readtoken1.
885 #define CHECKEND() {goto checkend; checkend_return:;}
886 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
887 #define PARSESUB() {goto parsesub; parsesub_return:;}
888 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
889 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
890 #define PARSEARITH() {goto parsearith; parsearith_return:;}
893 * Keep track of nested doublequotes in dblquote and doublequotep.
894 * We use dblquote for the first 32 levels, and we expand to a malloc'ed
895 * region for levels above that. Usually we never need to malloc.
896 * This code assumes that an int is 32 bits. We don't use uint32_t,
897 * because the rest of the code does not.
899 #define ISDBLQUOTE() ((varnest < 32) ? (dblquote & (1 << varnest)) : \
900 (dblquotep[(varnest / 32) - 1] & (1 << (varnest % 32))))
902 #define SETDBLQUOTE() \
904 dblquote |= (1 << varnest); \
906 dblquotep[(varnest / 32) - 1] |= (1 << (varnest % 32))
908 #define CLRDBLQUOTE() \
910 dblquote &= ~(1 << varnest); \
912 dblquotep[(varnest / 32) - 1] &= ~(1 << (varnest % 32))
915 readtoken1(int firstc
, char const *syn
, char *eofmark
, int striptabs
)
917 char const * volatile syntax
= syn
;
921 char line
[EOFMARKLEN
+ 1];
922 struct nodelist
*bqlist
;
924 int * volatile dblquotep
= NULL
;
925 volatile size_t maxnest
= 32;
926 volatile int dblquote
;
927 volatile size_t varnest
; /* levels of variables expansion */
928 volatile int arinest
; /* levels of arithmetic expansion */
929 volatile int parenlevel
; /* levels of parens in arithmetic */
930 volatile int oldstyle
;
931 char const * volatile prevsyntax
; /* syntax before arithmetic */
933 prevsyntax
= NULL
; /* XXX gcc4 */
939 if (syntax
== DQSYNTAX
) {
948 loop
: { /* for each line, until end of word */
950 if (c
== '\034' && doprompt
951 && attyset() && ! equal(termval(), "emacs")) {
953 if (syntax
== BASESYNTAX
)
959 CHECKEND(); /* set c to PEOF if at end of here document */
960 for (;;) { /* until end of line or end of word */
961 CHECKSTRSPACE(4, out
); /* permit 4 calls to USTPUTC */
964 if (syntax
== BASESYNTAX
)
965 goto endword
; /* exit outer loop */
973 goto loop
; /* continue outer loop */
978 if (eofmark
== NULL
|| ISDBLQUOTE())
979 USTPUTC(CTLESC
, out
);
982 case CBACK
: /* backslash */
997 if (ISDBLQUOTE() && c
!= '\\' &&
998 c
!= '`' && c
!= '$' &&
999 (c
!= '"' || eofmark
!= NULL
))
1001 if (SQSYNTAX
[c
] == CCTL
)
1002 USTPUTC(CTLESC
, out
);
1003 else if (eofmark
== NULL
) {
1004 USTPUTC(CTLQUOTEMARK
, out
);
1007 USTPUTC(CTLQUOTEEND
, out
);
1013 if (syntax
!= SQSYNTAX
) {
1014 if (eofmark
== NULL
)
1015 USTPUTC(CTLQUOTEMARK
, out
);
1020 if (eofmark
!= NULL
&& arinest
== 0 &&
1022 /* Ignore inside quoted here document */
1026 /* End of single quotes... */
1030 syntax
= BASESYNTAX
;
1032 USTPUTC(CTLQUOTEEND
, out
);
1036 if (eofmark
!= NULL
&& arinest
== 0 &&
1038 /* Ignore inside here document */
1050 USTPUTC(CTLQUOTEMARK
, out
);
1054 if (eofmark
!= NULL
)
1058 USTPUTC(CTLQUOTEEND
, out
);
1059 syntax
= BASESYNTAX
;
1064 USTPUTC(CTLQUOTEMARK
, out
);
1067 case CVAR
: /* '$' */
1068 PARSESUB(); /* parse substitution */
1070 case CENDVAR
: /* CLOSEBRACE */
1071 if (varnest
> 0 && !ISDBLQUOTE()) {
1073 USTPUTC(CTLENDVAR
, out
);
1078 case CLP
: /* '(' in arithmetic */
1082 case CRP
: /* ')' in arithmetic */
1083 if (parenlevel
> 0) {
1087 if (pgetc() == ')') {
1088 if (--arinest
== 0) {
1089 USTPUTC(CTLENDARI
, out
);
1090 syntax
= prevsyntax
;
1091 if (syntax
== DQSYNTAX
)
1100 * (don't 2nd guess - no error)
1107 case CBQUOTE
: /* '`' */
1111 goto endword
; /* exit outer loop */
1114 goto endword
; /* exit outer loop */
1121 if (syntax
== ARISYNTAX
)
1122 synerror("Missing '))'");
1123 if (syntax
!= BASESYNTAX
&& /* ! parsebackquote && */ eofmark
== NULL
)
1124 synerror("Unterminated quoted string");
1126 startlinno
= plinno
;
1128 synerror("Missing '}'");
1131 len
= out
- stackblock();
1133 if (eofmark
== NULL
) {
1134 if ((c
== '>' || c
== '<')
1137 && (*out
== '\0' || is_digit(*out
))) {
1139 return lasttoken
= TREDIR
;
1145 backquotelist
= bqlist
;
1146 grabstackblock(len
);
1148 if (dblquotep
!= NULL
)
1150 return lasttoken
= TWORD
;
1151 /* end of readtoken routine */
1156 * Check to see whether we are at the end of the here document. When this
1157 * is called, c is set to the first character of the next input line. If
1158 * we are at the end of the here document, this routine sets the c to PEOF.
1167 if (c
== *eofmark
) {
1168 if (pfgets(line
, sizeof line
) != NULL
) {
1172 for (q
= eofmark
+ 1 ; *q
&& *p
== *q
; p
++, q
++);
1173 if ((*p
== '\0' || *p
== '\n') && *q
== '\0') {
1176 needprompt
= doprompt
;
1178 pushstring(line
, strlen(line
), NULL
);
1183 goto checkend_return
;
1188 * Parse a redirection operator. The variable "out" points to a string
1189 * specifying the fd to be redirected. The variable "c" contains the
1190 * first character of the redirection operator.
1197 np
= (union node
*)stalloc(sizeof (struct nfile
));
1204 np
->type
= NCLOBBER
;
1211 } else { /* c == '<' */
1213 switch (c
= pgetc()) {
1215 if (sizeof (struct nfile
) != sizeof (struct nhere
)) {
1216 np
= (union node
*)stalloc(sizeof (struct nhere
));
1220 heredoc
= (struct heredoc
*)stalloc(sizeof (struct heredoc
));
1222 if ((c
= pgetc()) == '-') {
1223 heredoc
->striptabs
= 1;
1225 heredoc
->striptabs
= 0;
1245 np
->nfile
.fd
= digit_val(fd
);
1247 goto parseredir_return
;
1252 * Parse a substitution. At this point, we have read the dollar sign
1261 static const char types
[] = "}-+?=";
1264 if (c
!= '(' && c
!= OPENBRACE
&& !is_name(c
) && !is_special(c
)) {
1267 } else if (c
== '(') { /* $(command) or $((arith)) */
1268 if (pgetc() == '(') {
1275 USTPUTC(CTLVAR
, out
);
1276 typeloc
= out
- stackblock();
1277 USTPUTC(VSNORMAL
, out
);
1279 if (c
== OPENBRACE
) {
1282 if ((c
= pgetc()) == CLOSEBRACE
)
1294 } while (is_in_name(c
));
1295 } else if (is_digit(c
)) {
1299 } while (is_digit(c
));
1301 else if (is_special(c
)) {
1306 badsub
: synerror("Bad substitution");
1317 p
= strchr(types
, c
);
1320 subtype
= p
- types
+ VSNORMAL
;
1326 subtype
= c
== '#' ? VSTRIMLEFT
:
1339 if (ISDBLQUOTE() || arinest
)
1341 *(stackblock() + typeloc
) = subtype
| flags
;
1342 if (subtype
!= VSNORMAL
) {
1344 if (varnest
>= maxnest
) {
1345 dblquotep
= ckrealloc(dblquotep
, maxnest
/ 8);
1346 dblquotep
[(maxnest
/ 32) - 1] = 0;
1351 goto parsesub_return
;
1356 * Called to parse command substitutions. Newstyle is set if the command
1357 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1358 * list of commands (passed by reference), and savelen is the number of
1359 * characters on the top of the stack which must be preserved.
1363 struct nodelist
**nlpp
;
1367 struct jmploc jmploc
;
1368 struct jmploc
*volatile savehandler
;
1372 savepbq
= parsebackquote
;
1373 if (setjmp(jmploc
.loc
)) {
1377 handler
= savehandler
;
1378 longjmp(handler
->loc
, 1);
1382 savelen
= out
- stackblock();
1384 str
= ckmalloc(savelen
);
1385 memcpy(str
, stackblock(), savelen
);
1387 savehandler
= handler
;
1391 /* We must read until the closing backquote, giving special
1392 treatment to some slashes, and then push the string and
1393 reread it as input, interpreting it normally. */
1400 STARTSTACKSTR(pout
);
1406 switch (pc
= pgetc()) {
1411 if ((pc
= pgetc()) == '\n') {
1418 * If eating a newline, avoid putting
1419 * the newline into the new character
1420 * stream (via the STPUTC after the
1425 if (pc
!= '\\' && pc
!= '`' && pc
!= '$'
1426 && (!ISDBLQUOTE() || pc
!= '"'))
1432 needprompt
= doprompt
;
1436 startlinno
= plinno
;
1437 synerror("EOF in backquote substitution");
1447 psavelen
= pout
- stackblock();
1449 pstr
= grabstackstr(pout
);
1450 setinputstring(pstr
, 1);
1455 nlpp
= &(*nlpp
)->next
;
1456 *nlpp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
1457 (*nlpp
)->next
= NULL
;
1458 parsebackquote
= oldstyle
;
1461 saveprompt
= doprompt
;
1466 n
= list(0, oldstyle
);
1469 doprompt
= saveprompt
;
1471 if (readtoken() != TRP
)
1478 * Start reading from old file again, ignoring any pushed back
1479 * tokens left from the backquote parsing
1484 while (stackblocksize() <= savelen
)
1488 memcpy(out
, str
, savelen
);
1489 STADJUST(savelen
, out
);
1495 parsebackquote
= savepbq
;
1496 handler
= savehandler
;
1497 if (arinest
|| ISDBLQUOTE())
1498 USTPUTC(CTLBACKQ
| CTLQUOTE
, out
);
1500 USTPUTC(CTLBACKQ
, out
);
1502 goto parsebackq_oldreturn
;
1504 goto parsebackq_newreturn
;
1508 * Parse an arithmetic expansion (indicate start of one and set state)
1512 if (++arinest
== 1) {
1513 prevsyntax
= syntax
;
1515 USTPUTC(CTLARI
, out
);
1522 * we collapse embedded arithmetic expansion to
1523 * parenthesis, which should be equivalent
1527 goto parsearith_return
;
1530 } /* end of readtoken */
1542 * Returns true if the text contains nothing to expand (no dollar signs
1547 noexpand(char *text
)
1553 while ((c
= *p
++) != '\0') {
1554 if (c
== CTLQUOTEMARK
)
1558 else if (BASESYNTAX
[(int)c
] == CCTL
)
1566 * Return true if the argument is a legal variable name (a letter or
1567 * underscore followed by zero or more letters, underscores, and digits).
1571 goodname(char *name
)
1579 if (! is_in_name(*p
))
1587 * Called when an unexpected token is read during the parse. The argument
1588 * is the token that is expected, or -1 if more than one type of token can
1589 * occur at this point.
1593 synexpect(int token
)
1598 fmtstr(msg
, 64, "%s unexpected (expecting %s)",
1599 tokname
[lasttoken
], tokname
[token
]);
1601 fmtstr(msg
, 64, "%s unexpected", tokname
[lasttoken
]);
1609 synerror(const char *msg
)
1612 outfmt(&errout
, "%s: %d: ", commandname
, startlinno
);
1614 outfmt(&errout
, "%s: ", getprogname());
1615 outfmt(&errout
, "Syntax error: %s\n", msg
);
1616 error((char *)NULL
);
1621 setprompt(int which
)
1623 whichprompt
= which
;
1628 out2str(getprompt(NULL
));
1632 * called by editline -- any expansions to the prompt
1633 * should be added here.
1636 getprompt(void *unused
)
1638 switch (whichprompt
) {
1646 return "<internal prompt error>";