1 /* $NetBSD: parser.c,v 1.93 2014/08/29 09:35:19 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.93 2014/08/29 09:35:19 christos Exp $");
51 #include "expand.h" /* defines rmescapes() */
52 #include "eval.h" /* defines commandname */
53 #include "redir.h" /* defines copyfd() */
65 #include "myhistedit.h"
69 * Shell command parser.
74 /* values returned by readtoken */
78 #define CLOSEBRACE '}'
82 struct heredoc
*next
; /* next here document in list */
83 union node
*here
; /* redirection node */
84 char *eofmark
; /* string indicating end of input */
85 int striptabs
; /* if set, strip leading tabs */
90 static int noalias
= 0; /* when set, don't handle aliases */
91 struct heredoc
*heredoclist
; /* list of here documents to read */
92 int parsebackquote
; /* nonzero if we are inside backquotes */
93 int doprompt
; /* if set, prompt the user */
94 int needprompt
; /* true if interactive and at start of line */
95 int lasttoken
; /* last token read */
96 MKINIT
int tokpushback
; /* last token pushed back */
97 char *wordtext
; /* text of last word returned by readtoken */
98 MKINIT
int checkkwd
; /* 1 == check for kwds, 2 == also eat newlines */
99 struct nodelist
*backquotelist
;
100 union node
*redirnode
;
101 struct heredoc
*heredoc
;
102 int quoteflag
; /* set if (part of) last token was quoted */
103 int startlinno
; /* line # where last token started */
104 int funclinno
; /* line # where the current function started */
107 STATIC
union node
*list(int, int);
108 STATIC
union node
*andor(void);
109 STATIC
union node
*pipeline(void);
110 STATIC
union node
*command(void);
111 STATIC
union node
*simplecmd(union node
**, union node
*);
112 STATIC
union node
*makename(void);
113 STATIC
void parsefname(void);
114 STATIC
void parseheredoc(void);
115 STATIC
int peektoken(void);
116 STATIC
int readtoken(void);
117 STATIC
int xxreadtoken(void);
118 STATIC
int readtoken1(int, char const *, char *, int);
119 STATIC
int noexpand(char *);
120 STATIC
void synexpect(int) __dead
;
121 STATIC
void synerror(const char *) __dead
;
122 STATIC
void setprompt(int);
126 * Read and parse a command. Returns NEOF on end of file. (NULL is a
127 * valid parse tree indicating a blank line.)
131 parsecmd(int interact
)
153 list(int nlflag
, int erflag
)
155 union node
*n1
, *n2
, *n3
;
157 TRACE(("list: entered\n"));
160 if (nlflag
== 0 && tokendlist
[peektoken()])
166 if (tok
== TBACKGND
) {
167 if (n2
->type
== NCMD
|| n2
->type
== NPIPE
) {
168 n2
->ncmd
.backgnd
= 1;
169 } else if (n2
->type
== NREDIR
) {
172 n3
= (union node
*)stalloc(sizeof (struct nredir
));
175 n3
->nredir
.redirect
= NULL
;
183 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
185 n3
->nbinary
.ch1
= n1
;
186 n3
->nbinary
.ch2
= n2
;
203 if (tokendlist
[peektoken()])
210 pungetc(); /* push back EOF on input */
213 if (nlflag
|| erflag
)
226 union node
*n1
, *n2
, *n3
;
229 TRACE(("andor: entered\n"));
232 if ((t
= readtoken()) == TAND
) {
234 } else if (t
== TOR
) {
241 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
243 n3
->nbinary
.ch1
= n1
;
244 n3
->nbinary
.ch2
= n2
;
254 union node
*n1
, *n2
, *pipenode
;
255 struct nodelist
*lp
, *prev
;
258 TRACE(("pipeline: entered\n"));
262 while (readtoken() == TNOT
) {
263 TRACE(("pipeline: TNOT recognized\n"));
268 if (readtoken() == TPIPE
) {
269 pipenode
= (union node
*)stalloc(sizeof (struct npipe
));
270 pipenode
->type
= NPIPE
;
271 pipenode
->npipe
.backgnd
= 0;
272 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
273 pipenode
->npipe
.cmdlist
= lp
;
277 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
280 } while (readtoken() == TPIPE
);
286 TRACE(("negate pipeline\n"));
287 n2
= (union node
*)stalloc(sizeof (struct nnot
));
301 union node
*ap
, **app
;
302 union node
*cp
, **cpp
;
303 union node
*redir
, **rpp
;
306 TRACE(("command: entered\n"));
313 /* Check for redirection which may precede command */
314 while (readtoken() == TREDIR
) {
315 *rpp
= n2
= redirnode
;
316 rpp
= &n2
->nfile
.next
;
321 while (readtoken() == TNOT
) {
322 TRACE(("command: TNOT recognized\n"));
327 switch (readtoken()) {
329 n1
= (union node
*)stalloc(sizeof (struct nif
));
331 n1
->nif
.test
= list(0, 0);
332 if (readtoken() != TTHEN
)
334 n1
->nif
.ifpart
= list(0, 0);
336 while (readtoken() == TELIF
) {
337 n2
->nif
.elsepart
= (union node
*)stalloc(sizeof (struct nif
));
338 n2
= n2
->nif
.elsepart
;
340 n2
->nif
.test
= list(0, 0);
341 if (readtoken() != TTHEN
)
343 n2
->nif
.ifpart
= list(0, 0);
345 if (lasttoken
== TELSE
)
346 n2
->nif
.elsepart
= list(0, 0);
348 n2
->nif
.elsepart
= NULL
;
351 if (readtoken() != TFI
)
358 n1
= (union node
*)stalloc(sizeof (struct nbinary
));
359 n1
->type
= (lasttoken
== TWHILE
)? NWHILE
: NUNTIL
;
360 n1
->nbinary
.ch1
= list(0, 0);
361 if ((got
=readtoken()) != TDO
) {
362 TRACE(("expecting DO got %s %s\n", tokname
[got
], got
== TWORD
? wordtext
: ""));
365 n1
->nbinary
.ch2
= list(0, 0);
366 if (readtoken() != TDONE
)
372 if (readtoken() != TWORD
|| quoteflag
|| ! goodname(wordtext
))
373 synerror("Bad for loop variable");
374 n1
= (union node
*)stalloc(sizeof (struct nfor
));
376 n1
->nfor
.var
= wordtext
;
377 if (readtoken() == TWORD
&& ! quoteflag
&& equal(wordtext
, "in")) {
379 while (readtoken() == TWORD
) {
380 n2
= (union node
*)stalloc(sizeof (struct narg
));
382 n2
->narg
.text
= wordtext
;
383 n2
->narg
.backquote
= backquotelist
;
385 app
= &n2
->narg
.next
;
389 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
392 static char argvars
[5] = {CTLVAR
, VSNORMAL
|VSQUOTE
,
394 n2
= (union node
*)stalloc(sizeof (struct narg
));
396 n2
->narg
.text
= argvars
;
397 n2
->narg
.backquote
= NULL
;
398 n2
->narg
.next
= NULL
;
401 * Newline or semicolon here is optional (but note
402 * that the original Bourne shell only allowed NL).
404 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
408 if ((t
= readtoken()) == TDO
)
410 else if (t
== TBEGIN
)
414 n1
->nfor
.body
= list(0, 0);
415 if (readtoken() != t
)
420 n1
= (union node
*)stalloc(sizeof (struct ncase
));
422 if (readtoken() != TWORD
)
424 n1
->ncase
.expr
= n2
= (union node
*)stalloc(sizeof (struct narg
));
426 n2
->narg
.text
= wordtext
;
427 n2
->narg
.backquote
= backquotelist
;
428 n2
->narg
.next
= NULL
;
429 while (readtoken() == TNL
);
430 if (lasttoken
!= TWORD
|| ! equal(wordtext
, "in"))
431 synerror("expecting \"in\"");
432 cpp
= &n1
->ncase
.cases
;
434 checkkwd
= 2, readtoken();
436 * Both ksh and bash accept 'case x in esac'
437 * so configure scripts started taking advantage of this.
438 * The page: http://pubs.opengroup.org/onlinepubs/\
439 * 009695399/utilities/xcu_chap02.html contradicts itself,
440 * as to if this is legal; the "Case Conditional Format"
441 * paragraph shows one case is required, but the "Grammar"
442 * section shows a grammar that explicitly allows the no
445 while (lasttoken
!= TESAC
) {
446 *cpp
= cp
= (union node
*)stalloc(sizeof (struct nclist
));
447 if (lasttoken
== TLP
)
450 app
= &cp
->nclist
.pattern
;
452 *app
= ap
= (union node
*)stalloc(sizeof (struct narg
));
454 ap
->narg
.text
= wordtext
;
455 ap
->narg
.backquote
= backquotelist
;
456 if (checkkwd
= 2, readtoken() != TPIPE
)
458 app
= &ap
->narg
.next
;
461 ap
->narg
.next
= NULL
;
463 if (lasttoken
!= TRP
) {
466 cp
->nclist
.body
= list(0, 0);
469 if ((t
= readtoken()) != TESAC
) {
479 cpp
= &cp
->nclist
.next
;
486 n1
= (union node
*)stalloc(sizeof (struct nredir
));
487 n1
->type
= NSUBSHELL
;
488 n1
->nredir
.n
= list(0, 0);
489 n1
->nredir
.redirect
= NULL
;
490 if (readtoken() != TRP
)
496 if (readtoken() != TEND
)
500 /* Handle an empty command like other simple commands. */
503 * An empty command before a ; doesn't make much sense, and
504 * should certainly be disallowed in the case of `if ;'.
515 n1
= simplecmd(rpp
, redir
);
522 /* Now check for redirection which may follow command */
523 while (readtoken() == TREDIR
) {
524 *rpp
= n2
= redirnode
;
525 rpp
= &n2
->nfile
.next
;
531 if (n1
->type
!= NSUBSHELL
) {
532 n2
= (union node
*)stalloc(sizeof (struct nredir
));
537 n1
->nredir
.redirect
= redir
;
542 TRACE(("negate command\n"));
543 n2
= (union node
*)stalloc(sizeof (struct nnot
));
554 simplecmd(union node
**rpp
, union node
*redir
)
556 union node
*args
, **app
;
557 union node
**orig_rpp
= rpp
;
558 union node
*n
= NULL
, *n2
;
561 /* If we don't have any redirections already, then we must reset */
562 /* rpp to be the address of the local redir variable. */
569 * We save the incoming value, because we need this for shell
570 * functions. There can not be a redirect or an argument between
571 * the function name and the open parenthesis.
575 while (readtoken() == TNOT
) {
576 TRACE(("simplcmd: TNOT recognized\n"));
582 if (readtoken() == TWORD
) {
583 n
= (union node
*)stalloc(sizeof (struct narg
));
585 n
->narg
.text
= wordtext
;
586 n
->narg
.backquote
= backquotelist
;
589 } else if (lasttoken
== TREDIR
) {
590 *rpp
= n
= redirnode
;
591 rpp
= &n
->nfile
.next
;
592 parsefname(); /* read name of redirection file */
593 } else if (lasttoken
== TLP
&& app
== &args
->narg
.next
594 && rpp
== orig_rpp
) {
595 /* We have a function */
596 if (readtoken() != TRP
)
599 rmescapes(n
->narg
.text
);
600 if (!goodname(n
->narg
.text
))
601 synerror("Bad function name");
603 n
->narg
.next
= command();
613 n
= (union node
*)stalloc(sizeof (struct ncmd
));
617 n
->ncmd
.redirect
= redir
;
621 TRACE(("negate simplecmd\n"));
622 n2
= (union node
*)stalloc(sizeof (struct nnot
));
636 n
= (union node
*)stalloc(sizeof (struct narg
));
639 n
->narg
.text
= wordtext
;
640 n
->narg
.backquote
= backquotelist
;
644 void fixredir(union node
*n
, const char *text
, int err
)
646 TRACE(("Fix redir %s %d\n", text
, err
));
648 n
->ndup
.vname
= NULL
;
651 n
->ndup
.dupfd
= number(text
);
652 else if (text
[0] == '-' && text
[1] == '\0')
657 synerror("Bad fd number");
659 n
->ndup
.vname
= makename();
667 union node
*n
= redirnode
;
669 if (readtoken() != TWORD
)
671 if (n
->type
== NHERE
) {
672 struct heredoc
*here
= heredoc
;
678 TRACE(("Here document %d\n", n
->type
));
679 if (here
->striptabs
) {
680 while (*wordtext
== '\t')
683 if (! noexpand(wordtext
) || (i
= strlen(wordtext
)) == 0 || i
> EOFMARKLEN
)
684 synerror("Illegal eof marker for << redirection");
686 here
->eofmark
= wordtext
;
688 if (heredoclist
== NULL
)
691 for (p
= heredoclist
; p
->next
; p
= p
->next
)
695 } else if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
696 fixredir(n
, wordtext
, 0);
698 n
->nfile
.fname
= makename();
704 * Input any here documents.
710 struct heredoc
*here
;
713 while (heredoclist
) {
715 heredoclist
= here
->next
;
720 readtoken1(pgetc(), here
->here
->type
== NHERE
? SQSYNTAX
: DQSYNTAX
,
721 here
->eofmark
, here
->striptabs
);
722 n
= (union node
*)stalloc(sizeof (struct narg
));
725 n
->narg
.text
= wordtext
;
726 n
->narg
.backquote
= backquotelist
;
727 here
->here
->nhere
.doc
= n
;
745 int savecheckkwd
= checkkwd
;
747 int alreadyseen
= tokpushback
;
767 * check for keywords and aliases
769 if (t
== TWORD
&& !quoteflag
)
771 const char *const *pp
;
773 for (pp
= parsekwd
; *pp
; pp
++) {
774 if (**pp
== *wordtext
&& equal(*pp
, wordtext
))
777 parsekwd
+ KWDOFFSET
;
778 TRACE(("keyword %s recognized\n", tokname
[t
]));
783 (ap
= lookupalias(wordtext
, 1)) != NULL
) {
784 pushstring(ap
->val
, strlen(ap
->val
), ap
);
785 checkkwd
= savecheckkwd
;
790 checkkwd
= (t
== TNOT
) ? savecheckkwd
: 0;
792 TRACE(("%stoken %s %s\n", alreadyseen
? "reread " : "", tokname
[t
], t
== TWORD
? wordtext
: ""));
798 * Read the next input token.
799 * If the token is a word, we set backquotelist to the list of cmds in
800 * backquotes. We set quoteflag to true if any part of the word was
802 * If the token is TREDIR, then we set redirnode to a structure containing
804 * In all cases, the variable startlinno is set to the number of the line
805 * on which the token starts.
807 * [Change comment: here documents and internal procedures]
808 * [Readtoken shouldn't have any arguments. Perhaps we should make the
809 * word parsing code into a separate routine. In this case, readtoken
810 * doesn't need to have any internal procedures, but parseword does.
811 * We could also make parseoperator in essence the main routine, and
812 * have parseword (readtoken1?) handle both words and redirection.]
815 #define RETURN(token) return lasttoken = token
831 for (;;) { /* until token or start of word found */
837 while ((c
= pgetc()) != '\n' && c
!= PEOF
)
844 startlinno
= ++plinno
;
859 needprompt
= doprompt
;
887 return readtoken1(c
, BASESYNTAX
, NULL
, 0);
894 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
895 * is not NULL, read a here document. In the latter case, eofmark is the
896 * word which marks the end of the document and striptabs is true if
897 * leading tabs should be stripped from the document. The argument firstc
898 * is the first character of the input token or document.
900 * Because C does not have internal subroutines, I have simulated them
901 * using goto's to implement the subroutine linkage. The following macros
902 * will run code that appears at the end of readtoken1.
905 #define CHECKEND() {goto checkend; checkend_return:;}
906 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
907 #define PARSESUB() {goto parsesub; parsesub_return:;}
908 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
909 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
910 #define PARSEARITH() {goto parsearith; parsearith_return:;}
913 * Keep track of nested doublequotes in dblquote and doublequotep.
914 * We use dblquote for the first 32 levels, and we expand to a malloc'ed
915 * region for levels above that. Usually we never need to malloc.
916 * This code assumes that an int is 32 bits. We don't use uint32_t,
917 * because the rest of the code does not.
919 #define ISDBLQUOTE() ((varnest < 32) ? (dblquote & (1 << varnest)) : \
920 (dblquotep[(varnest / 32) - 1] & (1 << (varnest % 32))))
922 #define SETDBLQUOTE() \
924 dblquote |= (1 << varnest); \
926 dblquotep[(varnest / 32) - 1] |= (1 << (varnest % 32))
928 #define CLRDBLQUOTE() \
930 dblquote &= ~(1 << varnest); \
932 dblquotep[(varnest / 32) - 1] &= ~(1 << (varnest % 32))
935 readtoken1(int firstc
, char const *syn
, char *eofmark
, int striptabs
)
937 char const * volatile syntax
= syn
;
941 char line
[EOFMARKLEN
+ 1];
942 struct nodelist
*bqlist
;
944 int * volatile dblquotep
= NULL
;
945 volatile size_t maxnest
= 32;
946 volatile int dblquote
;
947 volatile size_t varnest
; /* levels of variables expansion */
948 volatile int arinest
; /* levels of arithmetic expansion */
949 volatile int parenlevel
; /* levels of parens in arithmetic */
950 volatile int oldstyle
;
951 char const * volatile prevsyntax
; /* syntax before arithmetic */
953 prevsyntax
= NULL
; /* XXX gcc4 */
959 if (syntax
== DQSYNTAX
) {
968 loop
: { /* for each line, until end of word */
970 if (c
== '\034' && doprompt
971 && attyset() && ! equal(termval(), "emacs")) {
973 if (syntax
== BASESYNTAX
)
979 CHECKEND(); /* set c to PEOF if at end of here document */
980 for (;;) { /* until end of line or end of word */
981 CHECKSTRSPACE(4, out
); /* permit 4 calls to USTPUTC */
984 if (syntax
== BASESYNTAX
)
985 goto endword
; /* exit outer loop */
993 goto loop
; /* continue outer loop */
998 if (eofmark
== NULL
|| ISDBLQUOTE())
999 USTPUTC(CTLESC
, out
);
1002 case CBACK
: /* backslash */
1018 if (ISDBLQUOTE() && c
!= '\\' &&
1019 c
!= '`' && c
!= '$' &&
1020 (c
!= '"' || eofmark
!= NULL
))
1022 if (SQSYNTAX
[c
] == CCTL
)
1023 USTPUTC(CTLESC
, out
);
1024 else if (eofmark
== NULL
) {
1025 USTPUTC(CTLQUOTEMARK
, out
);
1028 USTPUTC(CTLQUOTEEND
, out
);
1034 if (syntax
!= SQSYNTAX
) {
1035 if (eofmark
== NULL
)
1036 USTPUTC(CTLQUOTEMARK
, out
);
1041 if (eofmark
!= NULL
&& arinest
== 0 &&
1043 /* Ignore inside quoted here document */
1047 /* End of single quotes... */
1051 syntax
= BASESYNTAX
;
1053 USTPUTC(CTLQUOTEEND
, out
);
1057 if (eofmark
!= NULL
&& arinest
== 0 &&
1059 /* Ignore inside here document */
1071 USTPUTC(CTLQUOTEMARK
, out
);
1075 if (eofmark
!= NULL
)
1079 USTPUTC(CTLQUOTEEND
, out
);
1080 syntax
= BASESYNTAX
;
1085 USTPUTC(CTLQUOTEMARK
, out
);
1088 case CVAR
: /* '$' */
1089 PARSESUB(); /* parse substitution */
1091 case CENDVAR
: /* CLOSEBRACE */
1092 if (varnest
> 0 && !ISDBLQUOTE()) {
1094 USTPUTC(CTLENDVAR
, out
);
1099 case CLP
: /* '(' in arithmetic */
1103 case CRP
: /* ')' in arithmetic */
1104 if (parenlevel
> 0) {
1108 if (pgetc() == ')') {
1109 if (--arinest
== 0) {
1110 USTPUTC(CTLENDARI
, out
);
1111 syntax
= prevsyntax
;
1112 if (syntax
== DQSYNTAX
)
1121 * (don't 2nd guess - no error)
1128 case CBQUOTE
: /* '`' */
1132 goto endword
; /* exit outer loop */
1134 if (varnest
== 0 && !ISDBLQUOTE())
1135 goto endword
; /* exit outer loop */
1142 if (syntax
== ARISYNTAX
)
1143 synerror("Missing '))'");
1144 if (syntax
!= BASESYNTAX
&& /* ! parsebackquote && */ eofmark
== NULL
)
1145 synerror("Unterminated quoted string");
1147 startlinno
= plinno
;
1149 synerror("Missing '}'");
1152 len
= out
- stackblock();
1154 if (eofmark
== NULL
) {
1155 if ((c
== '>' || c
== '<')
1157 && (*out
== '\0' || is_number(out
))) {
1159 return lasttoken
= TREDIR
;
1165 backquotelist
= bqlist
;
1166 grabstackblock(len
);
1168 if (dblquotep
!= NULL
)
1170 return lasttoken
= TWORD
;
1171 /* end of readtoken routine */
1176 * Check to see whether we are at the end of the here document. When this
1177 * is called, c is set to the first character of the next input line. If
1178 * we are at the end of the here document, this routine sets the c to PEOF.
1187 if (c
== *eofmark
) {
1188 if (pfgets(line
, sizeof line
) != NULL
) {
1192 for (q
= eofmark
+ 1 ; *q
&& *p
== *q
; p
++, q
++)
1194 if ((*p
== '\0' || *p
== '\n') && *q
== '\0') {
1197 needprompt
= doprompt
;
1199 pushstring(line
, strlen(line
), NULL
);
1204 goto checkend_return
;
1209 * Parse a redirection operator. The variable "out" points to a string
1210 * specifying the fd to be redirected. The variable "c" contains the
1211 * first character of the redirection operator.
1217 strlcpy(fd
, out
, sizeof(fd
));
1219 np
= (union node
*)stalloc(sizeof (struct nfile
));
1226 np
->type
= NCLOBBER
;
1233 } else { /* c == '<' */
1235 switch (c
= pgetc()) {
1237 if (sizeof (struct nfile
) != sizeof (struct nhere
)) {
1238 np
= (union node
*)stalloc(sizeof (struct nhere
));
1242 heredoc
= (struct heredoc
*)stalloc(sizeof (struct heredoc
));
1244 if ((c
= pgetc()) == '-') {
1245 heredoc
->striptabs
= 1;
1247 heredoc
->striptabs
= 0;
1267 np
->nfile
.fd
= number(fd
);
1269 goto parseredir_return
;
1274 * Parse a substitution. At this point, we have read the dollar sign
1284 static const char types
[] = "}-+?=";
1289 if (c
!= '(' && c
!= OPENBRACE
&& !is_name(c
) && !is_special(c
)) {
1292 } else if (c
== '(') { /* $(command) or $((arith)) */
1293 if (pgetc() == '(') {
1300 USTPUTC(CTLVAR
, out
);
1301 typeloc
= out
- stackblock();
1302 USTPUTC(VSNORMAL
, out
);
1305 if (c
== OPENBRACE
) {
1308 if ((c
= pgetc()) == CLOSEBRACE
)
1321 } while (is_in_name(c
));
1322 if (out
- p
== 6 && strncmp(p
, "LINENO", 6) == 0) {
1323 /* Replace the variable name with the
1324 * current line number. */
1327 linno
-= funclinno
- 1;
1328 snprintf(buf
, sizeof(buf
), "%d", linno
);
1330 for (i
= 0; buf
[i
] != '\0'; i
++)
1331 STPUTC(buf
[i
], out
);
1334 } else if (is_digit(c
)) {
1338 } while (is_digit(c
));
1340 else if (is_special(c
)) {
1345 badsub
: synerror("Bad substitution");
1355 p
= strchr(types
, c
);
1358 subtype
= p
- types
+ VSNORMAL
;
1364 subtype
= c
== '#' ? VSTRIMLEFT
:
1377 if (ISDBLQUOTE() || arinest
)
1379 *(stackblock() + typeloc
) = subtype
| flags
;
1380 if (subtype
!= VSNORMAL
) {
1382 if (varnest
>= maxnest
) {
1383 dblquotep
= ckrealloc(dblquotep
, maxnest
/ 8);
1384 dblquotep
[(maxnest
/ 32) - 1] = 0;
1389 goto parsesub_return
;
1394 * Called to parse command substitutions. Newstyle is set if the command
1395 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1396 * list of commands (passed by reference), and savelen is the number of
1397 * characters on the top of the stack which must be preserved.
1401 struct nodelist
**nlpp
;
1404 char *volatile str
= NULL
;
1405 struct jmploc jmploc
;
1406 struct jmploc
*volatile savehandler
= NULL
;
1410 savepbq
= parsebackquote
;
1411 if (setjmp(jmploc
.loc
)) {
1415 handler
= savehandler
;
1416 longjmp(handler
->loc
, 1);
1420 savelen
= out
- stackblock();
1422 str
= ckmalloc(savelen
);
1423 memcpy(str
, stackblock(), savelen
);
1425 savehandler
= handler
;
1429 /* We must read until the closing backquote, giving special
1430 treatment to some slashes, and then push the string and
1431 reread it as input, interpreting it normally. */
1438 STARTSTACKSTR(pout
);
1444 switch (pc
= pgetc()) {
1449 if ((pc
= pgetc()) == '\n') {
1456 * If eating a newline, avoid putting
1457 * the newline into the new character
1458 * stream (via the STPUTC after the
1463 if (pc
!= '\\' && pc
!= '`' && pc
!= '$'
1464 && (!ISDBLQUOTE() || pc
!= '"'))
1470 needprompt
= doprompt
;
1474 startlinno
= plinno
;
1475 synerror("EOF in backquote substitution");
1485 psavelen
= pout
- stackblock();
1487 pstr
= grabstackstr(pout
);
1488 setinputstring(pstr
, 1);
1493 nlpp
= &(*nlpp
)->next
;
1494 *nlpp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
1495 (*nlpp
)->next
= NULL
;
1496 parsebackquote
= oldstyle
;
1499 saveprompt
= doprompt
;
1504 n
= list(0, oldstyle
);
1507 doprompt
= saveprompt
;
1509 if (readtoken() != TRP
)
1516 * Start reading from old file again, ignoring any pushed back
1517 * tokens left from the backquote parsing
1522 while (stackblocksize() <= savelen
)
1526 memcpy(out
, str
, savelen
);
1527 STADJUST(savelen
, out
);
1533 parsebackquote
= savepbq
;
1534 handler
= savehandler
;
1535 if (arinest
|| ISDBLQUOTE())
1536 USTPUTC(CTLBACKQ
| CTLQUOTE
, out
);
1538 USTPUTC(CTLBACKQ
, out
);
1540 goto parsebackq_oldreturn
;
1542 goto parsebackq_newreturn
;
1546 * Parse an arithmetic expansion (indicate start of one and set state)
1550 if (++arinest
== 1) {
1551 prevsyntax
= syntax
;
1553 USTPUTC(CTLARI
, out
);
1560 * we collapse embedded arithmetic expansion to
1561 * parenthesis, which should be equivalent
1565 goto parsearith_return
;
1568 } /* end of readtoken */
1580 * Returns true if the text contains nothing to expand (no dollar signs
1585 noexpand(char *text
)
1591 while ((c
= *p
++) != '\0') {
1592 if (c
== CTLQUOTEMARK
)
1596 else if (BASESYNTAX
[(int)c
] == CCTL
)
1604 * Return true if the argument is a legal variable name (a letter or
1605 * underscore followed by zero or more letters, underscores, and digits).
1609 goodname(char *name
)
1617 if (! is_in_name(*p
))
1625 * Called when an unexpected token is read during the parse. The argument
1626 * is the token that is expected, or -1 if more than one type of token can
1627 * occur at this point.
1631 synexpect(int token
)
1636 fmtstr(msg
, 64, "%s unexpected (expecting %s)",
1637 tokname
[lasttoken
], tokname
[token
]);
1639 fmtstr(msg
, 64, "%s unexpected", tokname
[lasttoken
]);
1647 synerror(const char *msg
)
1650 outfmt(&errout
, "%s: %d: ", commandname
, startlinno
);
1652 outfmt(&errout
, "%s: ", getprogname());
1653 outfmt(&errout
, "Syntax error: %s\n", msg
);
1659 setprompt(int which
)
1661 whichprompt
= which
;
1666 out2str(getprompt(NULL
));
1670 * called by editline -- any expansions to the prompt
1671 * should be added here.
1674 getprompt(void *unused
)
1676 switch (whichprompt
) {
1684 return "<internal prompt error>";