2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid
[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/parser.c,v 1.51.2.1 2005/03/03 03:43:20 obrien Exp $");
49 #include "expand.h" /* defines rmescapes() */
61 #if !defined(NO_HISTORY) && !defined(EDITLINE)
62 #include "myhistedit.h"
66 * Shell command parser.
72 /* values returned by readtoken */
78 struct heredoc
*next
; /* next here document in list */
79 union node
*here
; /* redirection node */
80 char *eofmark
; /* string indicating end of input */
81 int striptabs
; /* if set, strip leading tabs */
86 STATIC
struct heredoc
*heredoclist
; /* list of here documents to read */
87 STATIC
int parsebackquote
; /* nonzero if we are inside backquotes */
88 STATIC
int doprompt
; /* if set, prompt the user */
89 STATIC
int needprompt
; /* true if interactive and at start of line */
90 STATIC
int lasttoken
; /* last token read */
91 MKINIT
int tokpushback
; /* last token pushed back */
92 STATIC
char *wordtext
; /* text of last word returned by readtoken */
93 MKINIT
int checkkwd
; /* 1 == check for kwds, 2 == also eat newlines */
94 STATIC
struct nodelist
*backquotelist
;
95 STATIC
union node
*redirnode
;
96 STATIC
struct heredoc
*heredoc
;
97 STATIC
int quoteflag
; /* set if (part of) last token was quoted */
98 STATIC
int startlinno
; /* line # where last token started */
100 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
101 static int noaliases
= 0;
103 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
105 static const char argvars
[5] = {CTLVAR
, VSNORMAL
|VSQUOTE
, '@', '=', '\0'};
106 static const char types
[] = "}-+?=";
110 STATIC
union node
*list(int);
111 STATIC
union node
*andor(void);
112 STATIC
union node
*pipeline(void);
113 STATIC
union node
*command(void);
114 STATIC
union node
*simplecmd(union node
**, union node
*);
115 STATIC
union node
*makename(void);
116 STATIC
void parsefname(void);
117 STATIC
void parseheredoc(void);
118 STATIC
int peektoken(void);
119 STATIC
int readtoken(void);
120 STATIC
int xxreadtoken(void);
121 STATIC
int readtoken1(int, char const *, char *, int);
122 STATIC
int noexpand(char *);
123 STATIC
void synexpect(int);
124 STATIC
void synerror(char *);
125 STATIC
void setprompt(int);
129 * Read and parse a command. Returns NEOF on end of file. (NULL is a
130 * valid parse tree indicating a blank line.)
134 parsecmd(int interact
)
137 extern int exitstatus
;
142 setprompt(exitstatus
== 0 || (vpse
.flags
& VUNSET
)
160 union node
*n1
, *n2
, *n3
;
164 if (nlflag
== 0 && tokendlist
[peektoken()])
170 if (tok
== TBACKGND
) {
171 if (n2
->type
== NCMD
|| n2
->type
== NPIPE
) {
172 n2
->ncmd
.backgnd
= 1;
173 } else if (n2
->type
== NREDIR
) {
176 n3
= (union node
*)stalloc(sizeof (struct nredir
));
179 n3
->nredir
.redirect
= NULL
;
187 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
189 n3
->nbinary
.ch1
= n1
;
190 n3
->nbinary
.ch2
= n2
;
207 if (tokendlist
[peektoken()])
214 pungetc(); /* push back EOF on input */
230 union node
*n1
, *n2
, *n3
;
235 if ((t
= readtoken()) == TAND
) {
237 } else if (t
== TOR
) {
244 n3
= (union node
*)stalloc(sizeof (struct nbinary
));
246 n3
->nbinary
.ch1
= n1
;
247 n3
->nbinary
.ch2
= n2
;
257 union node
*n1
, *n2
, *pipenode
;
258 struct nodelist
*lp
, *prev
;
262 TRACE(("pipeline: entered\n"));
263 while (readtoken() == TNOT
)
267 if (readtoken() == TPIPE
) {
268 pipenode
= (union node
*)stalloc(sizeof (struct npipe
));
269 pipenode
->type
= NPIPE
;
270 pipenode
->npipe
.backgnd
= 0;
271 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
272 pipenode
->npipe
.cmdlist
= lp
;
276 lp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
279 } while (readtoken() == TPIPE
);
285 n2
= (union node
*)stalloc(sizeof (struct nnot
));
299 union node
*ap
, **app
;
300 union node
*cp
, **cpp
;
301 union node
*redir
, **rpp
;
309 /* Check for redirection which may precede command */
310 while (readtoken() == TREDIR
) {
311 *rpp
= n2
= redirnode
;
312 rpp
= &n2
->nfile
.next
;
317 while (readtoken() == TNOT
) {
318 TRACE(("command: TNOT recognized\n"));
323 switch (readtoken()) {
325 n1
= (union node
*)stalloc(sizeof (struct nif
));
327 if ((n1
->nif
.test
= list(0)) == NULL
)
329 if (readtoken() != TTHEN
)
331 n1
->nif
.ifpart
= list(0);
333 while (readtoken() == TELIF
) {
334 n2
->nif
.elsepart
= (union node
*)stalloc(sizeof (struct nif
));
335 n2
= n2
->nif
.elsepart
;
337 if ((n2
->nif
.test
= list(0)) == NULL
)
339 if (readtoken() != TTHEN
)
341 n2
->nif
.ifpart
= list(0);
343 if (lasttoken
== TELSE
)
344 n2
->nif
.elsepart
= list(0);
346 n2
->nif
.elsepart
= NULL
;
349 if (readtoken() != TFI
)
356 n1
= (union node
*)stalloc(sizeof (struct nbinary
));
357 n1
->type
= (lasttoken
== TWHILE
)? NWHILE
: NUNTIL
;
358 if ((n1
->nbinary
.ch1
= list(0)) == NULL
)
360 if ((got
=readtoken()) != TDO
) {
361 TRACE(("expecting DO got %s %s\n", tokname
[got
], got
== TWORD
? wordtext
: ""));
364 n1
->nbinary
.ch2
= list(0);
365 if (readtoken() != TDONE
)
371 if (readtoken() != TWORD
|| quoteflag
|| ! goodname(wordtext
))
372 synerror("Bad for loop variable");
373 n1
= (union node
*)stalloc(sizeof (struct nfor
));
375 n1
->nfor
.var
= wordtext
;
376 if (readtoken() == TWORD
&& ! quoteflag
&& equal(wordtext
, "in")) {
378 while (readtoken() == TWORD
) {
379 n2
= (union node
*)stalloc(sizeof (struct narg
));
381 n2
->narg
.text
= wordtext
;
382 n2
->narg
.backquote
= backquotelist
;
384 app
= &n2
->narg
.next
;
388 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
392 static const char argvars
[5] = {CTLVAR
, VSNORMAL
|VSQUOTE
,
395 n2
= (union node
*)stalloc(sizeof (struct narg
));
397 n2
->narg
.text
= (char *)argvars
;
398 n2
->narg
.backquote
= NULL
;
399 n2
->narg
.next
= NULL
;
402 * Newline or semicolon here is optional (but note
403 * that the original Bourne shell only allowed NL).
405 if (lasttoken
!= TNL
&& lasttoken
!= TSEMI
)
409 if ((t
= readtoken()) == TDO
)
411 else if (t
== TBEGIN
)
415 n1
->nfor
.body
= list(0);
416 if (readtoken() != t
)
421 n1
= (union node
*)stalloc(sizeof (struct ncase
));
423 if (readtoken() != TWORD
)
425 n1
->ncase
.expr
= n2
= (union node
*)stalloc(sizeof (struct narg
));
427 n2
->narg
.text
= wordtext
;
428 n2
->narg
.backquote
= backquotelist
;
429 n2
->narg
.next
= NULL
;
430 while (readtoken() == TNL
);
431 if (lasttoken
!= TWORD
|| ! equal(wordtext
, "in"))
432 synerror("expecting \"in\"");
433 cpp
= &n1
->ncase
.cases
;
434 noaliases
= 1; /* turn off alias expansion */
435 checkkwd
= 2, readtoken();
436 while (lasttoken
!= TESAC
) {
437 *cpp
= cp
= (union node
*)stalloc(sizeof (struct nclist
));
439 app
= &cp
->nclist
.pattern
;
440 if (lasttoken
== TLP
)
443 *app
= ap
= (union node
*)stalloc(sizeof (struct narg
));
445 ap
->narg
.text
= wordtext
;
446 ap
->narg
.backquote
= backquotelist
;
447 if (checkkwd
= 2, readtoken() != TPIPE
)
449 app
= &ap
->narg
.next
;
452 ap
->narg
.next
= NULL
;
453 if (lasttoken
!= TRP
)
454 noaliases
= 0, synexpect(TRP
);
455 cp
->nclist
.body
= list(0);
458 if ((t
= readtoken()) != TESAC
) {
460 noaliases
= 0, synexpect(TENDCASE
);
462 checkkwd
= 2, readtoken();
464 cpp
= &cp
->nclist
.next
;
466 noaliases
= 0; /* reset alias expansion */
471 n1
= (union node
*)stalloc(sizeof (struct nredir
));
472 n1
->type
= NSUBSHELL
;
473 n1
->nredir
.n
= list(0);
474 n1
->nredir
.redirect
= NULL
;
475 if (readtoken() != TRP
)
481 if (readtoken() != TEND
)
485 /* 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 ;'.
500 n1
= simplecmd(rpp
, redir
);
506 /* Now check for redirection which may follow command */
507 while (readtoken() == TREDIR
) {
508 *rpp
= n2
= redirnode
;
509 rpp
= &n2
->nfile
.next
;
515 if (n1
->type
!= NSUBSHELL
) {
516 n2
= (union node
*)stalloc(sizeof (struct nredir
));
521 n1
->nredir
.redirect
= redir
;
526 n2
= (union node
*)stalloc(sizeof (struct nnot
));
537 simplecmd(union node
**rpp
, union node
*redir
)
539 union node
*args
, **app
;
540 union node
**orig_rpp
= rpp
;
541 union node
*n
= NULL
, *n2
;
544 /* If we don't have any redirections already, then we must reset */
545 /* rpp to be the address of the local redir variable. */
552 * We save the incoming value, because we need this for shell
553 * functions. There can not be a redirect or an argument between
554 * the function name and the open parenthesis.
558 while (readtoken() == TNOT
) {
559 TRACE(("command: TNOT recognized\n"));
565 if (readtoken() == TWORD
) {
566 n
= (union node
*)stalloc(sizeof (struct narg
));
568 n
->narg
.text
= wordtext
;
569 n
->narg
.backquote
= backquotelist
;
572 } else if (lasttoken
== TREDIR
) {
573 *rpp
= n
= redirnode
;
574 rpp
= &n
->nfile
.next
;
575 parsefname(); /* read name of redirection file */
576 } else if (lasttoken
== TLP
&& app
== &args
->narg
.next
577 && rpp
== orig_rpp
) {
578 /* We have a function */
579 if (readtoken() != TRP
)
582 if (! goodname(n
->narg
.text
))
583 synerror("Bad function name");
586 n
->narg
.next
= command();
595 n
= (union node
*)stalloc(sizeof (struct ncmd
));
599 n
->ncmd
.redirect
= redir
;
603 n2
= (union node
*)stalloc(sizeof (struct nnot
));
617 n
= (union node
*)stalloc(sizeof (struct narg
));
620 n
->narg
.text
= wordtext
;
621 n
->narg
.backquote
= backquotelist
;
625 void fixredir(union node
*n
, const char *text
, int err
)
627 TRACE(("Fix redir %s %d\n", text
, err
));
629 n
->ndup
.vname
= NULL
;
631 if (is_digit(text
[0]) && text
[1] == '\0')
632 n
->ndup
.dupfd
= digit_val(text
[0]);
633 else if (text
[0] == '-' && text
[1] == '\0')
638 synerror("Bad fd number");
640 n
->ndup
.vname
= makename();
648 union node
*n
= redirnode
;
650 if (readtoken() != TWORD
)
652 if (n
->type
== NHERE
) {
653 struct heredoc
*here
= heredoc
;
659 TRACE(("Here document %d\n", n
->type
));
660 if (here
->striptabs
) {
661 while (*wordtext
== '\t')
664 if (! noexpand(wordtext
) || (i
= strlen(wordtext
)) == 0 || i
> EOFMARKLEN
)
665 synerror("Illegal eof marker for << redirection");
667 here
->eofmark
= wordtext
;
669 if (heredoclist
== NULL
)
672 for (p
= heredoclist
; p
->next
; p
= p
->next
);
675 } else if (n
->type
== NTOFD
|| n
->type
== NFROMFD
) {
676 fixredir(n
, wordtext
, 0);
678 n
->nfile
.fname
= makename();
684 * Input any here documents.
690 struct heredoc
*here
;
693 while (heredoclist
) {
695 heredoclist
= here
->next
;
700 readtoken1(pgetc(), here
->here
->type
== NHERE
? SQSYNTAX
: DQSYNTAX
,
701 here
->eofmark
, here
->striptabs
);
702 n
= (union node
*)stalloc(sizeof (struct narg
));
705 n
->narg
.text
= wordtext
;
706 n
->narg
.backquote
= backquotelist
;
707 here
->here
->nhere
.doc
= n
;
725 int savecheckkwd
= checkkwd
;
728 int alreadyseen
= tokpushback
;
747 * check for keywords and aliases
749 if (t
== TWORD
&& !quoteflag
)
751 const char * const *pp
;
753 for (pp
= parsekwd
; *pp
; pp
++) {
754 if (**pp
== *wordtext
&& equal(*pp
, wordtext
))
756 lasttoken
= t
= pp
- parsekwd
+ KWDOFFSET
;
757 TRACE(("keyword %s recognized\n", tokname
[t
]));
761 if (noaliases
== 0 &&
762 (ap
= lookupalias(wordtext
, 1)) != NULL
) {
763 pushstring(ap
->val
, strlen(ap
->val
), ap
);
764 checkkwd
= savecheckkwd
;
769 checkkwd
= (t
== TNOT
) ? savecheckkwd
: 0;
773 TRACE(("token %s %s\n", tokname
[t
], t
== TWORD
? wordtext
: ""));
775 TRACE(("reread token %s %s\n", 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 readtoken1(int firstc
, char const *syntax
, char *eofmark
, int striptabs
)
898 char line
[EOFMARKLEN
+ 1];
899 struct nodelist
*bqlist
;
902 int varnest
; /* levels of variables expansion */
903 int arinest
; /* levels of arithmetic expansion */
904 int parenlevel
; /* levels of parens in arithmetic */
906 char const *prevsyntax
; /* syntax before arithmetic */
909 /* Avoid longjmp clobbering */
924 if (syntax
== DQSYNTAX
)
933 loop
: { /* for each line, until end of word */
934 CHECKEND(); /* set c to PEOF if at end of here document */
935 for (;;) { /* until end of line or end of word */
936 CHECKSTRSPACE(3, out
); /* permit 3 calls to USTPUTC */
938 synentry
= syntax
[c
];
942 if (syntax
== BASESYNTAX
)
943 goto endword
; /* exit outer loop */
951 goto loop
; /* continue outer loop */
956 if (eofmark
== NULL
|| dblquote
)
957 USTPUTC(CTLESC
, out
);
960 case CBACK
: /* backslash */
965 } else if (c
== '\n') {
971 if (dblquote
&& c
!= '\\' &&
972 c
!= '`' && c
!= '$' &&
973 (c
!= '"' || eofmark
!= NULL
))
975 if (SQSYNTAX
[c
] == CCTL
)
976 USTPUTC(CTLESC
, out
);
977 else if (eofmark
== NULL
)
978 USTPUTC(CTLQUOTEMARK
, out
);
985 USTPUTC(CTLQUOTEMARK
, out
);
990 USTPUTC(CTLQUOTEMARK
, out
);
995 if (eofmark
!= NULL
&& arinest
== 0 &&
1002 } else if (eofmark
== NULL
) {
1003 syntax
= BASESYNTAX
;
1009 case CVAR
: /* '$' */
1010 PARSESUB(); /* parse substitution */
1012 case CENDVAR
: /* '}' */
1015 USTPUTC(CTLENDVAR
, out
);
1020 case CLP
: /* '(' in arithmetic */
1024 case CRP
: /* ')' in arithmetic */
1025 if (parenlevel
> 0) {
1029 if (pgetc() == ')') {
1030 if (--arinest
== 0) {
1031 USTPUTC(CTLENDARI
, out
);
1032 syntax
= prevsyntax
;
1033 if (syntax
== DQSYNTAX
)
1042 * (don't 2nd guess - no error)
1049 case CBQUOTE
: /* '`' */
1053 goto endword
; /* exit outer loop */
1056 goto endword
; /* exit outer loop */
1063 if (syntax
== ARISYNTAX
)
1064 synerror("Missing '))'");
1065 if (syntax
!= BASESYNTAX
&& ! parsebackquote
&& eofmark
== NULL
)
1066 synerror("Unterminated quoted string");
1068 startlinno
= plinno
;
1069 synerror("Missing '}'");
1072 len
= out
- stackblock();
1074 if (eofmark
== NULL
) {
1075 if ((c
== '>' || c
== '<')
1078 && (*out
== '\0' || is_digit(*out
))) {
1080 return lasttoken
= TREDIR
;
1086 backquotelist
= bqlist
;
1087 grabstackblock(len
);
1089 return lasttoken
= TWORD
;
1090 /* end of readtoken routine */
1095 * Check to see whether we are at the end of the here document. When this
1096 * is called, c is set to the first character of the next input line. If
1097 * we are at the end of the here document, this routine sets the c to PEOF.
1106 if (c
== *eofmark
) {
1107 if (pfgets(line
, sizeof line
) != NULL
) {
1111 for (q
= eofmark
+ 1 ; *q
&& *p
== *q
; p
++, q
++);
1112 if (*p
== '\n' && *q
== '\0') {
1115 needprompt
= doprompt
;
1117 pushstring(line
, strlen(line
), NULL
);
1122 goto checkend_return
;
1127 * Parse a redirection operator. The variable "out" points to a string
1128 * specifying the fd to be redirected. The variable "c" contains the
1129 * first character of the redirection operator.
1136 np
= (union node
*)stalloc(sizeof (struct nfile
));
1145 np
->type
= NCLOBBER
;
1150 } else { /* c == '<' */
1154 if (sizeof (struct nfile
) != sizeof (struct nhere
)) {
1155 np
= (union node
*)stalloc(sizeof (struct nhere
));
1159 heredoc
= (struct heredoc
*)stalloc(sizeof (struct heredoc
));
1161 if ((c
= pgetc()) == '-') {
1162 heredoc
->striptabs
= 1;
1164 heredoc
->striptabs
= 0;
1167 } else if (c
== '&')
1177 np
->nfile
.fd
= digit_val(fd
);
1179 goto parseredir_return
;
1184 * Parse a substitution. At this point, we have read the dollar sign
1194 static const char types
[] = "}-+?=";
1196 int bracketed_name
= 0; /* used to handle ${[0-9]*} variables */
1199 if (c
!= '(' && c
!= '{' && !is_name(c
) && !is_special(c
)) {
1202 } else if (c
== '(') { /* $(command) or $((arith)) */
1203 if (pgetc() == '(') {
1210 USTPUTC(CTLVAR
, out
);
1211 typeloc
= out
- stackblock();
1212 USTPUTC(VSNORMAL
, out
);
1218 if ((c
= pgetc()) == '}')
1230 } while (is_in_name(c
));
1231 } else if (is_digit(c
)) {
1232 if (bracketed_name
) {
1236 } while (is_digit(c
));
1242 if (! is_special(c
))
1243 badsub
: synerror("Bad substitution");
1256 p
= strchr(types
, c
);
1259 subtype
= p
- types
+ VSNORMAL
;
1265 subtype
= c
== '#' ? VSTRIMLEFT
:
1278 if (subtype
!= VSLENGTH
&& (dblquote
|| arinest
))
1280 *(stackblock() + typeloc
) = subtype
| flags
;
1281 if (subtype
!= VSNORMAL
)
1284 goto parsesub_return
;
1289 * Called to parse command substitutions. Newstyle is set if the command
1290 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1291 * list of commands (passed by reference), and savelen is the number of
1292 * characters on the top of the stack which must be preserved.
1296 struct nodelist
**nlpp
;
1300 struct jmploc jmploc
;
1301 struct jmploc
*volatile savehandler
;
1305 /* Avoid longjmp clobbering */
1309 savepbq
= parsebackquote
;
1310 if (setjmp(jmploc
.loc
)) {
1314 handler
= savehandler
;
1315 longjmp(handler
->loc
, 1);
1319 savelen
= out
- stackblock();
1321 str
= ckmalloc(savelen
);
1322 memcpy(str
, stackblock(), savelen
);
1324 savehandler
= handler
;
1328 /* We must read until the closing backquote, giving special
1329 treatment to some slashes, and then push the string and
1330 reread it as input, interpreting it normally. */
1343 switch (c
= pgetc()) {
1348 if ((c
= pgetc()) == '\n') {
1355 * If eating a newline, avoid putting
1356 * the newline into the new character
1357 * stream (via the STPUTC after the
1362 if (c
!= '\\' && c
!= '`' && c
!= '$'
1363 && (!dblquote
|| c
!= '"'))
1369 needprompt
= doprompt
;
1373 startlinno
= plinno
;
1374 synerror("EOF in backquote substitution");
1384 savelen
= out
- stackblock();
1386 str
= ckmalloc(savelen
);
1387 memcpy(str
, stackblock(), savelen
);
1388 setinputstring(str
, 1);
1393 nlpp
= &(*nlpp
)->next
;
1394 *nlpp
= (struct nodelist
*)stalloc(sizeof (struct nodelist
));
1395 (*nlpp
)->next
= NULL
;
1396 parsebackquote
= oldstyle
;
1399 saveprompt
= doprompt
;
1406 doprompt
= saveprompt
;
1408 if (readtoken() != TRP
)
1415 * Start reading from old file again, ignoring any pushed back
1416 * tokens left from the backquote parsing
1421 while (stackblocksize() <= savelen
)
1425 memcpy(out
, str
, savelen
);
1426 STADJUST(savelen
, out
);
1432 parsebackquote
= savepbq
;
1433 handler
= savehandler
;
1434 if (arinest
|| dblquote
)
1435 USTPUTC(CTLBACKQ
| CTLQUOTE
, out
);
1437 USTPUTC(CTLBACKQ
, out
);
1439 goto parsebackq_oldreturn
;
1441 goto parsebackq_newreturn
;
1445 * Parse an arithmetic expansion (indicate start of one and set state)
1449 if (++arinest
== 1) {
1450 prevsyntax
= syntax
;
1452 USTPUTC(CTLARI
, out
);
1459 * we collapse embedded arithmetic expansion to
1460 * parenthesis, which should be equivalent
1464 goto parsearith_return
;
1467 } /* end of readtoken */
1479 * Returns true if the text contains nothing to expand (no dollar signs
1484 noexpand(char *text
)
1490 while ((c
= *p
++) != '\0') {
1491 if ( c
== CTLQUOTEMARK
)
1495 else if (BASESYNTAX
[(int)c
] == CCTL
)
1503 * Return true if the argument is a legal variable name (a letter or
1504 * underscore followed by zero or more letters, underscores, and digits).
1508 goodname(char *name
)
1516 if (! is_in_name(*p
))
1524 * Called when an unexpected token is read during the parse. The argument
1525 * is the token that is expected, or -1 if more than one type of token can
1526 * occur at this point.
1530 synexpect(int token
)
1535 fmtstr(msg
, 64, "%s unexpected (expecting %s)",
1536 tokname
[lasttoken
], tokname
[token
]);
1538 fmtstr(msg
, 64, "%s unexpected", tokname
[lasttoken
]);
1548 outfmt(&errout
, "%s: %d: ", commandname
, startlinno
);
1549 outfmt(&errout
, "Syntax error: %s\n", msg
);
1550 error((char *)NULL
);
1554 setprompt(int which
)
1556 whichprompt
= which
;
1563 #endif /* EDITLINE */
1564 #endif /* !NO_HISTORY */
1565 out2str(getprompt(NULL
));
1569 * called by editline -- any expansions to the prompt
1570 * should be added here.
1573 getprompt(void *unused __unused
)
1575 static char ps
[PROMPTLEN
];
1580 * Select prompt format.
1582 switch (whichprompt
) {
1596 return "<internal prompt error>";
1600 * Format prompt string.
1602 for (i
= 0; (i
< 127) && (*fmt
!= '\0'); i
++, fmt
++)
1609 * \h specifies just the local hostname,
1610 * \H specifies fully-qualified hostname.
1615 gethostname(&ps
[i
], PROMPTLEN
- i
);
1616 /* Skip to end of hostname. */
1617 trim
= (*fmt
== 'h') ? '.' : '\0';
1618 while ((ps
[i
+1] != '\0') && (ps
[i
+1] != trim
))
1623 * Working directory.
1625 * \W specifies just the final component,
1626 * \w specifies the entire path.
1631 getcwd(&ps
[i
], PROMPTLEN
- i
);
1633 /* Final path component only. */
1635 for (j
= i
; ps
[j
] != '\0'; j
++)
1638 memmove(&ps
[i
], &ps
[trim
],
1641 /* Skip to end of path. */
1642 while (ps
[i
+ 1] != '\0')
1649 * '$' for normal users, '#' for root.
1652 ps
[i
] = (geteuid() != 0) ? '$' : '#';
1663 * Emit unrecognized formats verbatim.
1677 * $PchId: parser.c,v 1.6 2006/05/29 13:08:11 philip Exp $