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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)parser.c 8.7 (Berkeley) 5/16/95
37 * $FreeBSD: src/bin/sh/parser.c,v 1.29.2.9 2002/10/18 11:24:04 tjr Exp $
38 * $DragonFly: src/bin/sh/parser.c,v 1.8 2006/09/28 22:29:44 pavalos Exp $
46 #include "expand.h" /* defines rmescapes() */
47 #include "redir.h" /* defines copyfd() */
60 #include "myhistedit.h"
64 * Shell command parser.
69 /* values returned by readtoken */
75 struct heredoc
*next
; /* next here document in list */
76 union node
*here
; /* redirection node */
77 char *eofmark
; /* string indicating end of input */
78 int striptabs
; /* if set, strip leading tabs */
83 STATIC
struct heredoc
*heredoclist
; /* list of here documents to read */
84 STATIC
int parsebackquote
; /* nonzero if we are inside backquotes */
85 STATIC
int doprompt
; /* if set, prompt the user */
86 STATIC
int needprompt
; /* true if interactive and at start of line */
87 STATIC
int lasttoken
; /* last token read */
88 MKINIT
int tokpushback
; /* last token pushed back */
89 STATIC
char *wordtext
; /* text of last word returned by readtoken */
90 MKINIT
int checkkwd
; /* 1 == check for kwds, 2 == also eat newlines */
91 STATIC
struct nodelist
*backquotelist
;
92 STATIC
union node
*redirnode
;
93 STATIC
struct heredoc
*heredoc
;
94 STATIC
int quoteflag
; /* set if (part of) last token was quoted */
95 STATIC
int startlinno
; /* line # where last token started */
97 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
98 static int noaliases
= 0;
100 STATIC
union node
*list(int);
101 STATIC
union node
*andor(void);
102 STATIC
union node
*pipeline(void);
103 STATIC
union node
*command(void);
104 STATIC
union node
*simplecmd(union node
**, union node
*);
105 STATIC
union node
*makename(void);
106 STATIC
void parsefname(void);
107 STATIC
void parseheredoc(void);
108 STATIC
int peektoken(void);
109 STATIC
int readtoken(void);
110 STATIC
int xxreadtoken(void);
111 STATIC
int readtoken1(int, char const *, char *, int);
112 STATIC
int noexpand(char *);
113 STATIC
void synexpect(int);
114 STATIC
void synerror(const char *);
115 STATIC
void setprompt(int);
119 * Read and parse a command. Returns NEOF on end of file. (NULL is a
120 * valid parse tree indicating a blank line.)
124 parsecmd(int interact
)
148 union node
*n1
, *n2
, *n3
;
152 if (nlflag
== 0 && tokendlist
[peektoken()])
158 if (tok
== TBACKGND
) {
159 if (n2
->type
== NCMD
|| n2
->type
== NPIPE
) {
160 n2
->ncmd
.backgnd
= 1;
161 } else if (n2
->type
== NREDIR
) {
164 n3
= (union node
*)stalloc(sizeof (struct nredir
));
167 n3
->nredir
.redirect
= NULL
;
175 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
177 n3
->nbinary
.ch1
= n1
;
178 n3
->nbinary
.ch2
= n2
;
195 if (tokendlist
[peektoken()])
202 pungetc(); /* push back EOF on input */
218 union node
*n1
, *n2
, *n3
;
223 if ((t
= readtoken()) == TAND
) {
225 } else if (t
== TOR
) {
232 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
234 n3
->nbinary
.ch1
= n1
;
235 n3
->nbinary
.ch2
= n2
;
245 union node
*n1
, *n2
, *pipenode
;
246 struct nodelist
*lp
, *prev
;
250 TRACE(("pipeline: entered\n"));
251 while (readtoken() == TNOT
)
255 if (readtoken() == TPIPE
) {
256 pipenode
= (union node
*)stalloc(sizeof (struct npipe
));
257 pipenode
->type
= NPIPE
;
258 pipenode
->npipe
.backgnd
= 0;
259 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
260 pipenode
->npipe
.cmdlist
= lp
;
264 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
267 } while (readtoken() == TPIPE
);
273 n2
= (union node
*)stalloc(sizeof (struct nnot
));
287 union node
*ap
, **app
;
288 union node
*cp
, **cpp
;
289 union node
*redir
, **rpp
;
297 /* Check for redirection which may precede command */
298 while (readtoken() == TREDIR
) {
299 *rpp
= n2
= redirnode
;
300 rpp
= &n2
->nfile
.next
;
305 while (readtoken() == TNOT
) {
306 TRACE(("command: TNOT recognized\n"));
311 switch (readtoken()) {
313 n1
= (union node
*)stalloc(sizeof (struct nif
));
315 n1
->nif
.test
= list(0);
316 if (readtoken() != TTHEN
)
318 n1
->nif
.ifpart
= list(0);
320 while (readtoken() == TELIF
) {
321 n2
->nif
.elsepart
= (union node
*)stalloc(sizeof (struct nif
));
322 n2
= n2
->nif
.elsepart
;
324 n2
->nif
.test
= list(0);
325 if (readtoken() != TTHEN
)
327 n2
->nif
.ifpart
= list(0);
329 if (lasttoken
== TELSE
)
330 n2
->nif
.elsepart
= list(0);
332 n2
->nif
.elsepart
= NULL
;
335 if (readtoken() != TFI
)
342 n1
= (union node
*)stalloc(sizeof (struct nbinary
));
343 n1
->type
= (lasttoken
== TWHILE
)? NWHILE
: NUNTIL
;
344 n1
->nbinary
.ch1
= list(0);
345 if ((got
=readtoken()) != TDO
) {
346 TRACE(("expecting DO got %s %s\n", tokname
[got
], got
== TWORD
? wordtext
: ""));
349 n1
->nbinary
.ch2
= list(0);
350 if (readtoken() != TDONE
)
356 if (readtoken() != TWORD
|| quoteflag
|| ! goodname(wordtext
))
357 synerror("Bad for loop variable");
358 n1
= (union node
*)stalloc(sizeof (struct nfor
));
360 n1
->nfor
.var
= wordtext
;
361 if (readtoken() == TWORD
&& ! quoteflag
&& equal(wordtext
, "in")) {
363 while (readtoken() == TWORD
) {
364 n2
= (union node
*)stalloc(sizeof (struct narg
));
366 n2
->narg
.text
= wordtext
;
367 n2
->narg
.backquote
= backquotelist
;
369 app
= &n2
->narg
.next
;
373 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
376 static char argvars
[5] = {CTLVAR
, VSNORMAL
|VSQUOTE
, '@', '=', '\0'};
377 n2
= (union node
*)stalloc(sizeof (struct narg
));
379 n2
->narg
.text
= argvars
;
380 n2
->narg
.backquote
= NULL
;
381 n2
->narg
.next
= NULL
;
384 * Newline or semicolon here is optional (but note
385 * that the original Bourne shell only allowed NL).
387 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
391 if ((t
= readtoken()) == TDO
)
393 else if (t
== TBEGIN
)
397 n1
->nfor
.body
= list(0);
398 if (readtoken() != t
)
403 n1
= (union node
*)stalloc(sizeof (struct ncase
));
405 if (readtoken() != TWORD
)
407 n1
->ncase
.expr
= n2
= (union node
*)stalloc(sizeof (struct narg
));
409 n2
->narg
.text
= wordtext
;
410 n2
->narg
.backquote
= backquotelist
;
411 n2
->narg
.next
= NULL
;
412 while (readtoken() == TNL
);
413 if (lasttoken
!= TWORD
|| ! equal(wordtext
, "in"))
414 synerror("expecting \"in\"");
415 cpp
= &n1
->ncase
.cases
;
416 noaliases
= 1; /* turn off alias expansion */
417 checkkwd
= 2, readtoken();
418 while (lasttoken
!= TESAC
) {
419 *cpp
= cp
= (union node
*)stalloc(sizeof (struct nclist
));
421 app
= &cp
->nclist
.pattern
;
422 if (lasttoken
== TLP
)
425 *app
= ap
= (union node
*)stalloc(sizeof (struct narg
));
427 ap
->narg
.text
= wordtext
;
428 ap
->narg
.backquote
= backquotelist
;
429 if (checkkwd
= 2, readtoken() != TPIPE
)
431 app
= &ap
->narg
.next
;
434 ap
->narg
.next
= NULL
;
435 if (lasttoken
!= TRP
)
436 noaliases
= 0, synexpect(TRP
);
437 cp
->nclist
.body
= list(0);
440 if ((t
= readtoken()) != TESAC
) {
442 noaliases
= 0, synexpect(TENDCASE
);
444 checkkwd
= 2, readtoken();
446 cpp
= &cp
->nclist
.next
;
448 noaliases
= 0; /* reset alias expansion */
453 n1
= (union node
*)stalloc(sizeof (struct nredir
));
454 n1
->type
= NSUBSHELL
;
455 n1
->nredir
.n
= list(0);
456 n1
->nredir
.redirect
= NULL
;
457 if (readtoken() != TRP
)
463 if (readtoken() != TEND
)
467 /* Handle an empty command like other simple commands. */
472 * An empty command before a ; doesn't make much sense, and
473 * should certainly be disallowed in the case of `if ;'.
482 n1
= simplecmd(rpp
, redir
);
488 /* Now check for redirection which may follow command */
489 while (readtoken() == TREDIR
) {
490 *rpp
= n2
= redirnode
;
491 rpp
= &n2
->nfile
.next
;
497 if (n1
->type
!= NSUBSHELL
) {
498 n2
= (union node
*)stalloc(sizeof (struct nredir
));
503 n1
->nredir
.redirect
= redir
;
508 n2
= (union node
*)stalloc(sizeof (struct nnot
));
519 simplecmd(union node
**rpp
, union node
*redir
)
521 union node
*args
, **app
;
522 union node
**orig_rpp
= rpp
;
523 union node
*n
= NULL
, *n2
;
526 /* If we don't have any redirections already, then we must reset */
527 /* rpp to be the address of the local redir variable. */
534 * We save the incoming value, because we need this for shell
535 * functions. There can not be a redirect or an argument between
536 * the function name and the open parenthesis.
540 while (readtoken() == TNOT
) {
541 TRACE(("command: TNOT recognized\n"));
547 if (readtoken() == TWORD
) {
548 n
= (union node
*)stalloc(sizeof (struct narg
));
550 n
->narg
.text
= wordtext
;
551 n
->narg
.backquote
= backquotelist
;
554 } else if (lasttoken
== TREDIR
) {
555 *rpp
= n
= redirnode
;
556 rpp
= &n
->nfile
.next
;
557 parsefname(); /* read name of redirection file */
558 } else if (lasttoken
== TLP
&& app
== &args
->narg
.next
559 && rpp
== orig_rpp
) {
560 /* We have a function */
561 if (readtoken() != TRP
)
564 if (! goodname(n
->narg
.text
))
565 synerror("Bad function name");
568 n
->narg
.next
= command();
577 n
= (union node
*)stalloc(sizeof (struct ncmd
));
581 n
->ncmd
.redirect
= redir
;
585 n2
= (union node
*)stalloc(sizeof (struct nnot
));
599 n
= (union node
*)stalloc(sizeof (struct narg
));
602 n
->narg
.text
= wordtext
;
603 n
->narg
.backquote
= backquotelist
;
608 fixredir(union node
*n
, const char *text
, int err
)
610 TRACE(("Fix redir %s %d\n", text
, err
));
612 n
->ndup
.vname
= NULL
;
614 if (is_digit(text
[0]) && text
[1] == '\0')
615 n
->ndup
.dupfd
= digit_val(text
[0]);
616 else if (text
[0] == '-' && text
[1] == '\0')
621 synerror("Bad fd number");
623 n
->ndup
.vname
= makename();
631 union node
*n
= redirnode
;
633 if (readtoken() != TWORD
)
635 if (n
->type
== NHERE
) {
636 struct heredoc
*here
= heredoc
;
642 TRACE(("Here document %d\n", n
->type
));
643 if (here
->striptabs
) {
644 while (*wordtext
== '\t')
647 if (! noexpand(wordtext
) || (i
= strlen(wordtext
)) == 0 || i
> EOFMARKLEN
)
648 synerror("Illegal eof marker for << redirection");
650 here
->eofmark
= wordtext
;
652 if (heredoclist
== NULL
)
655 for (p
= heredoclist
; p
->next
; p
= p
->next
);
658 } else if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
659 fixredir(n
, wordtext
, 0);
661 n
->nfile
.fname
= makename();
667 * Input any here documents.
673 struct heredoc
*here
;
676 while (heredoclist
) {
678 heredoclist
= here
->next
;
683 readtoken1(pgetc(), here
->here
->type
== NHERE
? SQSYNTAX
: DQSYNTAX
,
684 here
->eofmark
, here
->striptabs
);
685 n
= (union node
*)stalloc(sizeof (struct narg
));
688 n
->narg
.text
= wordtext
;
689 n
->narg
.backquote
= backquotelist
;
690 here
->here
->nhere
.doc
= n
;
708 int savecheckkwd
= checkkwd
;
711 int alreadyseen
= tokpushback
;
730 * check for keywords and aliases
732 if (t
== TWORD
&& !quoteflag
)
734 const char * const *pp
;
736 for (pp
= parsekwd
; *pp
; pp
++) {
737 if (**pp
== *wordtext
&& equal(*pp
, wordtext
))
739 lasttoken
= t
= pp
- parsekwd
+ KWDOFFSET
;
740 TRACE(("keyword %s recognized\n", tokname
[t
]));
744 if (noaliases
== 0 &&
745 (ap
= lookupalias(wordtext
, 1)) != NULL
) {
746 pushstring(ap
->val
, strlen(ap
->val
), ap
);
747 checkkwd
= savecheckkwd
;
752 checkkwd
= (t
== TNOT
) ? savecheckkwd
: 0;
756 TRACE(("token %s %s\n", tokname
[t
], t
== TWORD
? wordtext
: ""));
758 TRACE(("reread token %s %s\n", tokname
[t
], t
== TWORD
? wordtext
: ""));
765 * Read the next input token.
766 * If the token is a word, we set backquotelist to the list of cmds in
767 * backquotes. We set quoteflag to true if any part of the word was
769 * If the token is TREDIR, then we set redirnode to a structure containing
771 * In all cases, the variable startlinno is set to the number of the line
772 * on which the token starts.
774 * [Change comment: here documents and internal procedures]
775 * [Readtoken shouldn't have any arguments. Perhaps we should make the
776 * word parsing code into a separate routine. In this case, readtoken
777 * doesn't need to have any internal procedures, but parseword does.
778 * We could also make parseoperator in essence the main routine, and
779 * have parseword (readtoken1?) handle both words and redirection.]
782 #define RETURN(token) return lasttoken = token
798 for (;;) { /* until token or start of word found */
800 if (c
== ' ' || c
== '\t')
801 continue; /* quick check for white space first */
806 while ((c
= pgetc()) != '\n' && c
!= PEOF
);
810 if (pgetc() == '\n') {
811 startlinno
= ++plinno
;
822 needprompt
= doprompt
;
850 return readtoken1(c
, BASESYNTAX
, (char *)NULL
, 0);
857 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
858 * is not NULL, read a here document. In the latter case, eofmark is the
859 * word which marks the end of the document and striptabs is true if
860 * leading tabs should be stripped from the document. The argument firstc
861 * is the first character of the input token or document.
863 * Because C does not have internal subroutines, I have simulated them
864 * using goto's to implement the subroutine linkage. The following macros
865 * will run code that appears at the end of readtoken1.
868 #define CHECKEND() {goto checkend; checkend_return:;}
869 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
870 #define PARSESUB() {goto parsesub; parsesub_return:;}
871 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
872 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
873 #define PARSEARITH() {goto parsearith; parsearith_return:;}
876 readtoken1(int firstc
, char const *syntax
, char *eofmark
, int striptabs
)
881 char line
[EOFMARKLEN
+ 1];
882 struct nodelist
*bqlist
;
885 int varnest
; /* levels of variables expansion */
886 int arinest
; /* levels of arithmetic expansion */
887 int parenlevel
; /* levels of parens in arithmetic */
889 char const *prevsyntax
; /* syntax before arithmetic */
892 /* Avoid longjmp clobbering */
907 if (syntax
== DQSYNTAX
)
916 loop
: { /* for each line, until end of word */
917 CHECKEND(); /* set c to PEOF if at end of here document */
918 for (;;) { /* until end of line or end of word */
919 CHECKSTRSPACE(3, out
); /* permit 3 calls to USTPUTC */
921 synentry
= syntax
[c
];
925 if (syntax
== BASESYNTAX
)
926 goto endword
; /* exit outer loop */
934 goto loop
; /* continue outer loop */
939 if (eofmark
== NULL
|| dblquote
)
940 USTPUTC(CTLESC
, out
);
943 case CBACK
: /* backslash */
948 } else if (c
== '\n') {
954 if (dblquote
&& c
!= '\\' &&
955 c
!= '`' && c
!= '$' &&
956 (c
!= '"' || eofmark
!= NULL
))
958 if (SQSYNTAX
[c
] == CCTL
)
959 USTPUTC(CTLESC
, out
);
960 else if (eofmark
== NULL
)
961 USTPUTC(CTLQUOTEMARK
, out
);
968 USTPUTC(CTLQUOTEMARK
, out
);
973 USTPUTC(CTLQUOTEMARK
, out
);
978 if (eofmark
!= NULL
&& arinest
== 0 &&
985 } else if (eofmark
== NULL
) {
993 PARSESUB(); /* parse substitution */
995 case CENDVAR
: /* '}' */
998 USTPUTC(CTLENDVAR
, out
);
1003 case CLP
: /* '(' in arithmetic */
1007 case CRP
: /* ')' in arithmetic */
1008 if (parenlevel
> 0) {
1012 if (pgetc() == ')') {
1013 if (--arinest
== 0) {
1014 USTPUTC(CTLENDARI
, out
);
1015 syntax
= prevsyntax
;
1016 if (syntax
== DQSYNTAX
)
1025 * (don't 2nd guess - no error)
1032 case CBQUOTE
: /* '`' */
1036 goto endword
; /* exit outer loop */
1039 goto endword
; /* exit outer loop */
1046 if (syntax
== ARISYNTAX
)
1047 synerror("Missing '))'");
1048 if (syntax
!= BASESYNTAX
&& ! parsebackquote
&& eofmark
== NULL
)
1049 synerror("Unterminated quoted string");
1051 startlinno
= plinno
;
1052 synerror("Missing '}'");
1055 len
= out
- stackblock();
1057 if (eofmark
== NULL
) {
1058 if ((c
== '>' || c
== '<')
1061 && (*out
== '\0' || is_digit(*out
))) {
1063 return lasttoken
= TREDIR
;
1069 backquotelist
= bqlist
;
1070 grabstackblock(len
);
1072 return lasttoken
= TWORD
;
1073 /* end of readtoken routine */
1078 * Check to see whether we are at the end of the here document. When this
1079 * is called, c is set to the first character of the next input line. If
1080 * we are at the end of the here document, this routine sets the c to PEOF.
1089 if (c
== *eofmark
) {
1090 if (pfgets(line
, sizeof line
) != NULL
) {
1094 for (q
= eofmark
+ 1 ; *q
&& *p
== *q
; p
++, q
++);
1095 if (*p
== '\n' && *q
== '\0') {
1098 needprompt
= doprompt
;
1100 pushstring(line
, strlen(line
), NULL
);
1105 goto checkend_return
;
1110 * Parse a redirection operator. The variable "out" points to a string
1111 * specifying the fd to be redirected. The variable "c" contains the
1112 * first character of the redirection operator.
1119 np
= (union node
*)stalloc(sizeof (struct nfile
));
1128 np
->type
= NCLOBBER
;
1133 } else { /* c == '<' */
1137 if (sizeof (struct nfile
) != sizeof (struct nhere
)) {
1138 np
= (union node
*)stalloc(sizeof (struct nhere
));
1142 heredoc
= (struct heredoc
*)stalloc(sizeof (struct heredoc
));
1144 if ((c
= pgetc()) == '-') {
1145 heredoc
->striptabs
= 1;
1147 heredoc
->striptabs
= 0;
1150 } else if (c
== '&')
1160 np
->nfile
.fd
= digit_val(fd
);
1162 goto parseredir_return
;
1167 * Parse a substitution. At this point, we have read the dollar sign
1176 static const char types
[] = "}-+?=";
1177 int bracketed_name
= 0; /* used to handle ${[0-9]*} variables */
1180 if (c
!= '(' && c
!= '{' && (is_eof(c
) || !is_name(c
)) &&
1184 } else if (c
== '(') { /* $(command) or $((arith)) */
1185 if (pgetc() == '(') {
1192 USTPUTC(CTLVAR
, out
);
1193 typeloc
= out
- stackblock();
1194 USTPUTC(VSNORMAL
, out
);
1200 if ((c
= pgetc()) == '}')
1208 if (!is_eof(c
) && is_name(c
)) {
1212 } while (!is_eof(c
) && is_in_name(c
));
1213 } else if (is_digit(c
)) {
1214 if (bracketed_name
) {
1218 } while (is_digit(c
));
1224 if (! is_special(c
))
1225 badsub
: synerror("Bad substitution");
1238 p
= strchr(types
, c
);
1241 subtype
= p
- types
+ VSNORMAL
;
1247 subtype
= c
== '#' ? VSTRIMLEFT
:
1260 if (subtype
!= VSLENGTH
&& (dblquote
|| arinest
))
1262 *(stackblock() + typeloc
) = subtype
| flags
;
1263 if (subtype
!= VSNORMAL
)
1266 goto parsesub_return
;
1271 * Called to parse command substitutions. Newstyle is set if the command
1272 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1273 * list of commands (passed by reference), and savelen is the number of
1274 * characters on the top of the stack which must be preserved.
1278 struct nodelist
**nlpp
;
1282 struct jmploc jmploc
;
1283 struct jmploc
*volatile savehandler
;
1287 /* Avoid longjmp clobbering */
1291 savepbq
= parsebackquote
;
1292 if (setjmp(jmploc
.loc
)) {
1296 handler
= savehandler
;
1297 longjmp(handler
->loc
, 1);
1301 savelen
= out
- stackblock();
1303 str
= ckmalloc(savelen
);
1304 memcpy(str
, stackblock(), savelen
);
1306 savehandler
= handler
;
1310 /* We must read until the closing backquote, giving special
1311 treatment to some slashes, and then push the string and
1312 reread it as input, interpreting it normally. */
1319 STARTSTACKSTR(pout
);
1325 switch (pc
= pgetc()) {
1330 if ((pc
= pgetc()) == '\n') {
1337 * If eating a newline, avoid putting
1338 * the newline into the new character
1339 * stream (via the STPUTC after the
1344 if (pc
!= '\\' && pc
!= '`' && pc
!= '$'
1345 && (!dblquote
|| pc
!= '"'))
1351 needprompt
= doprompt
;
1355 startlinno
= plinno
;
1356 synerror("EOF in backquote substitution");
1366 psavelen
= pout
- stackblock();
1368 pstr
= ckmalloc(psavelen
);
1369 memcpy(pstr
, stackblock(), psavelen
);
1370 setinputstring(pstr
, 1);
1375 nlpp
= &(*nlpp
)->next
;
1376 *nlpp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
1377 (*nlpp
)->next
= NULL
;
1378 parsebackquote
= oldstyle
;
1381 saveprompt
= doprompt
;
1388 doprompt
= saveprompt
;
1390 if (readtoken() != TRP
)
1397 * Start reading from old file again, ignoring any pushed back
1398 * tokens left from the backquote parsing
1403 while (stackblocksize() <= savelen
)
1407 memcpy(out
, str
, savelen
);
1408 STADJUST(savelen
, out
);
1414 parsebackquote
= savepbq
;
1415 handler
= savehandler
;
1416 if (arinest
|| dblquote
)
1417 USTPUTC(CTLBACKQ
| CTLQUOTE
, out
);
1419 USTPUTC(CTLBACKQ
, out
);
1421 goto parsebackq_oldreturn
;
1423 goto parsebackq_newreturn
;
1427 * Parse an arithmetic expansion (indicate start of one and set state)
1431 if (++arinest
== 1) {
1432 prevsyntax
= syntax
;
1434 USTPUTC(CTLARI
, out
);
1441 * we collapse embedded arithmetic expansion to
1442 * parenthesis, which should be equivalent
1446 goto parsearith_return
;
1449 } /* end of readtoken */
1461 * Returns true if the text contains nothing to expand (no dollar signs
1466 noexpand(char *text
)
1472 while ((c
= *p
++) != '\0') {
1473 if ( c
== CTLQUOTEMARK
)
1477 else if (BASESYNTAX
[(int)c
] == CCTL
)
1485 * Return true if the argument is a legal variable name (a letter or
1486 * underscore followed by zero or more letters, underscores, and digits).
1490 goodname(char *name
)
1498 if (! is_in_name(*p
))
1506 * Called when an unexpected token is read during the parse. The argument
1507 * is the token that is expected, or -1 if more than one type of token can
1508 * occur at this point.
1512 synexpect(int token
)
1517 fmtstr(msg
, 64, "%s unexpected (expecting %s)",
1518 tokname
[lasttoken
], tokname
[token
]);
1520 fmtstr(msg
, 64, "%s unexpected", tokname
[lasttoken
]);
1527 synerror(const char *msg
)
1530 outfmt(&errout
, "%s: %d: ", commandname
, startlinno
);
1531 outfmt(&errout
, "Syntax error: %s\n", msg
);
1532 error((char *)NULL
);
1536 setprompt(int which
)
1538 whichprompt
= which
;
1543 out2str(getprompt(NULL
));
1547 * called by editline -- any expansions to the prompt
1548 * should be added here.
1551 getprompt(void *unused __unused
)
1553 switch (whichprompt
) {
1561 return "<internal prompt error>";