3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. 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
38 #include <sys/types.h>
66 #include "myhistedit.h"
70 int evalskip
; /* set if we are skipping commands */
71 STATIC
int skipcount
; /* number of levels to skip */
72 MKINIT
int loopnest
; /* current loop nesting level */
73 static int funcline
; /* starting line number of current function, or 0 if not in a function */
77 int exitstatus
; /* exit status of last command */
78 int back_exitstatus
; /* exit status of backquoted command */
79 int savestatus
= -1; /* exit status of last command outside traps */
81 /* Prevent PS4 nesting. */
84 MKINIT
int tpip
[2] = { -1 };
86 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
89 void evaltreenr(union node
*, int) __attribute__ ((__noreturn__
));
90 STATIC
int evalloop(union node
*, int);
91 STATIC
int evalfor(union node
*, int);
92 STATIC
int evalcase(union node
*, int);
93 STATIC
int evalsubshell(union node
*, int);
94 STATIC
void expredir(union node
*);
95 STATIC
int evalpipe(union node
*, int);
97 STATIC
int evalcommand(union node
*, int, struct backcmd
*);
99 STATIC
int evalcommand(union node
*, int);
101 STATIC
int evalbltin(const struct builtincmd
*, int, char **, int);
102 STATIC
int evalfun(struct funcnode
*, int, char **, int);
103 STATIC
void prehash(union node
*);
104 STATIC
int eprintlist(struct output
*, struct strlist
*, int);
105 STATIC
int bltincmd(int, char **);
108 STATIC
const struct builtincmd bltin
= {
111 .flags
= BUILTIN_REGULAR
,
116 * Called to reset things after an exception.
123 if (savestatus
>= 0) {
124 if (exception
== EXEXIT
|| evalskip
== SKIPFUNCDEF
)
125 exitstatus
= savestatus
;
145 static int evalcmd(int argc
, char **argv
, int flags
)
154 STARTSTACKSTR(concat
);
157 concat
= stputs(p
, concat
);
158 if ((p
= *ap
++) == NULL
)
162 STPUTC('\0', concat
);
163 p
= grabstackstr(concat
);
165 return evalstring(p
, flags
& EV_TESTED
);
172 * Execute a command or commands contained in a string.
176 evalstring(char *s
, int flags
)
179 struct stackmark smark
;
184 setstackmark(&smark
);
187 for (; (n
= parsecmd(0)) != NEOF
; popstackmark(&smark
)) {
190 i
= evaltree(n
, flags
& ~(parser_eof() ? 0 : EV_EXIT
));
197 popstackmark(&smark
);
207 * Evaluate a parse tree. The value is left in the global variable
212 evaltree(union node
*n
, int flags
)
215 int (*evalfn
)(union node
*, int);
216 struct stackmark smark
;
220 setstackmark(&smark
);
226 TRACE(("evaltree(NULL) called\n"));
233 displayhist
= 1; /* show history substitutions done with fc */
235 TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
236 getpid(), n
, n
->type
, flags
));
240 out1fmt("Node type = %d\n", n
->type
);
241 #ifndef USE_GLIBC_STDIO
247 status
= evaltree(n
->nnot
.com
, EV_TESTED
);
252 errlinno
= lineno
= n
->nredir
.linno
;
254 lineno
-= funcline
- 1;
255 expredir(n
->nredir
.redirect
);
256 pushredir(n
->nredir
.redirect
);
257 status
= redirectsafe(n
->nredir
.redirect
, REDIR_PUSH
);
259 checkexit
= EV_TESTED
;
261 status
= evaltree(n
->nredir
.n
, flags
& EV_TESTED
);
262 if (n
->nredir
.redirect
)
266 evalfn
= evalcommand
;
268 checkexit
= EV_TESTED
;
279 evalfn
= evalsubshell
;
291 #error NAND + 1 != NOR
294 #error NOR + 1 != NSEMI
296 isor
= n
->type
- NAND
;
297 status
= evaltree(n
->nbinary
.ch1
,
298 (flags
| ((isor
>> 1) - 1)) & EV_TESTED
);
299 if ((!status
) == isor
|| evalskip
)
305 status
= evalfn(n
, flags
);
308 status
= evaltree(n
->nif
.test
, EV_TESTED
);
314 } else if (n
->nif
.elsepart
) {
330 if (eflag
&& (~flags
& checkexit
) && status
)
333 if (flags
& EV_EXIT
) {
338 popstackmark(&smark
);
344 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
347 void evaltreenr(union node
*n
, int flags
)
348 #ifdef HAVE_ATTRIBUTE_ALIAS
349 __attribute__ ((alias("evaltree")));
358 static int skiploop(void)
368 if (likely(--skipcount
<= 0)) {
382 evalloop(union node
*n
, int flags
)
393 i
= evaltree(n
->nbinary
.ch1
, EV_TESTED
);
395 if (skip
== SKIPFUNC
)
399 if (n
->type
!= NWHILE
)
403 status
= evaltree(n
->nbinary
.ch2
, flags
);
405 } while (!(skip
& ~SKIPCONT
));
414 evalfor(union node
*n
, int flags
)
416 struct arglist arglist
;
421 errlinno
= lineno
= n
->nfor
.linno
;
423 lineno
-= funcline
- 1;
425 arglist
.lastp
= &arglist
.list
;
426 for (argp
= n
->nfor
.args
; argp
; argp
= argp
->narg
.next
) {
427 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
429 *arglist
.lastp
= NULL
;
434 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
435 setvar(n
->nfor
.var
, sp
->text
, 0);
436 status
= evaltree(n
->nfor
.body
, flags
);
437 if (skiploop() & ~SKIPCONT
)
448 evalcase(union node
*n
, int flags
)
452 struct arglist arglist
;
455 errlinno
= lineno
= n
->ncase
.linno
;
457 lineno
-= funcline
- 1;
459 arglist
.lastp
= &arglist
.list
;
460 expandarg(n
->ncase
.expr
, &arglist
, FNMATCH_IS_ENABLED
? EXP_TILDE
:
461 EXP_TILDE
| EXP_MBCHAR
);
462 for (cp
= n
->ncase
.cases
; cp
&& evalskip
== 0 ; cp
= cp
->nclist
.next
) {
463 for (patp
= cp
->nclist
.pattern
; patp
; patp
= patp
->narg
.next
) {
464 if (casematch(patp
, arglist
.list
->text
)) {
465 /* Ensure body is non-empty as otherwise
466 * EV_EXIT may prevent us from setting the
469 if (evalskip
== 0 && cp
->nclist
.body
) {
470 status
= evaltree(cp
->nclist
.body
,
484 * Kick off a subshell to evaluate a tree.
488 evalsubshell(union node
*n
, int flags
)
491 int backgnd
= (n
->type
== NBACKGND
);
494 errlinno
= lineno
= n
->nredir
.linno
;
496 lineno
-= funcline
- 1;
498 expredir(n
->nredir
.redirect
);
500 if (!backgnd
&& flags
& EV_EXIT
&& !have_traps()) {
505 if (forkshell(jp
, n
->nredir
.n
, backgnd
) == 0) {
511 redirect(n
->nredir
.redirect
, 0);
512 evaltreenr(n
->nredir
.n
, flags
);
517 status
= waitforjob(jp
);
525 * Compute the names of the files in a redirection list.
529 expredir(union node
*n
)
533 for (redir
= n
; redir
; redir
= redir
->nfile
.next
) {
536 switch (redir
->type
) {
542 expandarg(redir
->nfile
.fname
, &fn
, EXP_TILDE
| EXP_REDIR
);
543 redir
->nfile
.expfname
= fn
.list
->text
;
547 if (redir
->ndup
.vname
) {
548 expandarg(redir
->ndup
.vname
, &fn
, EXP_TILDE
| EXP_REDIR
);
549 fixredir(redir
, fn
.list
->text
, 1);
559 * Evaluate a pipeline. All the processes in the pipeline are children
560 * of the process creating the pipeline. (This differs from some versions
561 * of the shell, which make the last process in a pipeline the parent
566 evalpipe(union node
*n
, int flags
)
575 TRACE(("evalpipe(0x%lx) called\n", (long)n
));
577 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
)
581 jp
= makejob(pipelen
);
583 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
589 sh_error("Pipe call failed");
592 if (forkshell(jp
, lp
->n
, n
->npipe
.backgnd
) == 0) {
605 evaltreenr(lp
->n
, flags
);
613 if (n
->npipe
.backgnd
== 0) {
614 status
= waitforjob(jp
);
615 TRACE(("evalpipe: job done exit status %d\n", status
));
625 * Execute a command inside back quotes. If it's a builtin command, we
626 * want to save its output in a block obtained from malloc. Otherwise
627 * we fork off a subprocess and get the output of the command via a pipe.
628 * Should be called with interrupts off.
632 evalbackcmd(union node
*n
, struct backcmd
*result
)
650 pid
= forkshell(jp
, n
, FORK_NOJOB
);
660 evaltreenr(n
, EV_EXIT
);
668 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
669 result
->fd
, result
->buf
, result
->nleft
, result
->jp
));
672 static struct strlist
*fill_arglist(struct arglist
*arglist
,
675 struct strlist
**lastp
= arglist
->lastp
;
678 while ((argp
= *argpp
)) {
679 expandarg(argp
, arglist
, EXP_FULL
| EXP_TILDE
);
680 *argpp
= argp
->narg
.next
;
688 static int parse_command_args(struct arglist
*arglist
, union node
**argpp
,
691 struct strlist
*sp
= arglist
->list
;
695 sp
= unlikely(sp
->next
) ? sp
->next
:
696 fill_arglist(arglist
, argpp
);
704 if (c
== '-' && !*cp
) {
705 if (likely(!sp
->next
) && !fill_arglist(arglist
, argpp
))
716 /* run 'typecmd' for other options */
719 } while ((c
= *cp
++));
727 * Execute a simple command.
732 evalcommand(union node
*cmd
, int flags
, struct backcmd
*backcmd
)
734 evalcommand(union node
*cmd
, int flags
)
737 struct localvar_list
*localvar_stop
;
738 struct parsefile
*file_stop
;
739 struct redirtab
*redir_stop
;
741 struct arglist arglist
;
742 struct arglist varlist
;
750 struct cmdentry cmdentry
;
762 errlinno
= lineno
= cmd
->ncmd
.linno
;
764 lineno
-= funcline
- 1;
766 /* First expand the arguments. */
767 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd
, flags
));
768 file_stop
= parsefile
;
771 cmdentry
.cmdtype
= CMDBUILTIN
;
772 cmdentry
.u
.cmd
= &bltin
;
773 varlist
.lastp
= &varlist
.list
;
774 *varlist
.lastp
= NULL
;
775 arglist
.lastp
= &arglist
.list
;
776 *arglist
.lastp
= NULL
;
786 argp
= cmd
->ncmd
.args
;
787 if ((osp
= fill_arglist(&arglist
, &argp
))) {
788 int pseudovarflag
= 0;
791 find_command(arglist
.list
->text
, &cmdentry
,
792 cmd_flag
| DO_REGBLTIN
, pathval());
796 /* implement bltin and command here */
797 if (cmdentry
.cmdtype
!= CMDBUILTIN
)
800 pseudovarflag
= cmdentry
.u
.cmd
->flags
& BUILTIN_ASSIGN
;
801 if (likely(spclbltin
< 0)) {
803 cmdentry
.u
.cmd
->flags
&
806 vlocal
= spclbltin
^ BUILTIN_SPECIAL
;
808 execcmd
= cmdentry
.u
.cmd
== EXECCMD
;
809 if (likely(cmdentry
.u
.cmd
!= COMMANDCMD
))
812 cmd_flag
= parse_command_args(&arglist
, &argp
, &path
);
817 for (; argp
; argp
= argp
->narg
.next
)
818 expandarg(argp
, &arglist
,
820 isassignment(argp
->narg
.text
) ?
821 EXP_VARTILDE
: EXP_FULL
| EXP_TILDE
);
823 for (sp
= arglist
.list
; sp
; sp
= sp
->next
)
826 if (execcmd
&& argc
> 1)
830 localvar_stop
= pushlocalvars(vlocal
);
832 /* Reserve one extra spot at the front for shellexec. */
833 nargv
= stalloc(sizeof (char *) * (argc
+ 2));
835 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
836 TRACE(("evalcommand arg: %s\n", sp
->text
));
842 if (iflag
&& funcline
== 0 && argc
> 0)
846 expredir(cmd
->ncmd
.redirect
);
847 redir_stop
= pushredir(cmd
->ncmd
.redirect
);
848 status
= redirectsafe(cmd
->ncmd
.redirect
, REDIR_PUSH
|REDIR_SAVEFD2
);
850 if (unlikely(status
)) {
854 /* We have a redirection error. */
861 for (argp
= cmd
->ncmd
.assign
; argp
; argp
= argp
->narg
.next
) {
862 struct strlist
**spp
;
865 expandarg(argp
, &varlist
, EXP_VARTILDE
);
868 mklocal((*spp
)->text
, VEXPORT
);
870 setvareq((*spp
)->text
, vflags
);
873 /* Print the command if xflag is set. */
874 if (xflag
&& !inps4
) {
880 outstr(expandstr(ps4val()), out
);
883 sep
= eprintlist(out
, varlist
.list
, sep
);
884 eprintlist(out
, osp
, sep
);
891 /* Now locate the command. */
892 if (cmdentry
.cmdtype
!= CMDBUILTIN
||
893 !(cmdentry
.u
.cmd
->flags
& BUILTIN_REGULAR
)) {
894 path
= unlikely(path
) ? path
: pathval();
895 find_command(argv
[0], &cmdentry
, cmd_flag
| DO_ERR
, path
);
900 /* Execute the command. */
901 switch (cmdentry
.cmdtype
) {
910 /* Fork off a child process if necessary. */
911 if (!(flags
& EV_EXIT
) || have_traps()) {
913 jp
= vforkexec(cmd
, argv
, path
, cmdentry
.u
.index
);
916 shellexec(argv
, path
, cmdentry
.u
.index
);
920 if (evalbltin(cmdentry
.u
.cmd
, argc
, argv
, flags
) &&
921 !(exception
== EXERROR
&& spclbltin
<= 0)) {
923 longjmp(handler
->loc
, 1);
928 if (evalfun(cmdentry
.u
.func
, argc
, argv
, flags
))
933 status
= waitforjob(jp
);
937 if (cmd
->ncmd
.redirect
)
939 unwindredir(redir_stop
);
940 unwindfiles(file_stop
);
941 unwindlocalvars(localvar_stop
);
943 /* dsl: I think this is intended to be used to support
944 * '_' in 'vi' command mode during line editing...
945 * However I implemented that within libedit itself.
947 setvar("_", lastarg
, 0);
953 evalbltin(const struct builtincmd
*cmd
, int argc
, char **argv
, int flags
)
955 char *volatile savecmdname
;
956 struct jmploc
*volatile savehandler
;
957 struct jmploc jmploc
;
961 savecmdname
= commandname
;
962 savehandler
= handler
;
963 if ((i
= setjmp(jmploc
.loc
)))
966 commandname
= argv
[0];
968 optptr
= NULL
; /* initialize nextopt */
970 status
= evalcmd(argc
, argv
, flags
);
972 status
= (*cmd
->builtin
)(argc
, argv
);
975 sh_warnx("%s: I/O error", commandname
);
976 status
|= outerr(out1
);
980 commandname
= savecmdname
;
981 handler
= savehandler
;
987 evalfun(struct funcnode
*func
, int argc
, char **argv
, int flags
)
989 volatile struct shparam saveparam
;
990 struct jmploc
*volatile savehandler
;
991 struct jmploc jmploc
;
996 saveparam
= shellparam
;
997 savefuncline
= funcline
;
998 saveloopnest
= loopnest
;
999 savehandler
= handler
;
1000 if ((e
= setjmp(jmploc
.loc
))) {
1005 shellparam
.malloc
= 0;
1007 funcline
= func
->n
.ndefun
.linno
;
1010 shellparam
.nparam
= argc
- 1;
1011 shellparam
.p
= argv
+ 1;
1012 shellparam
.optind
= 1;
1013 shellparam
.optoff
= -1;
1014 evaltree(func
->n
.ndefun
.body
, flags
& EV_TESTED
);
1017 loopnest
= saveloopnest
;
1018 funcline
= savefuncline
;
1020 freeparam(&shellparam
);
1021 shellparam
= saveparam
;
1022 handler
= savehandler
;
1024 evalskip
&= ~(SKIPFUNC
| SKIPFUNCDEF
);
1030 * Search for a command. This is called before we fork so that the
1031 * location of the command will be available in the parent as well as
1032 * the child. The check for "goodname" is an overly conservative
1033 * check that the name will not be subject to expansion.
1037 prehash(union node
*n
)
1039 struct cmdentry entry
;
1041 if (n
->type
== NCMD
&& n
->ncmd
.args
)
1042 if (goodname(n
->ncmd
.args
->narg
.text
))
1043 find_command(n
->ncmd
.args
->narg
.text
, &entry
, 0,
1050 * Builtin commands. Builtin commands whose functions are closely
1051 * tied to evaluation are implemented here.
1059 bltincmd(int argc
, char **argv
)
1062 * Preserve exitstatus of a previous possible redirection
1065 return back_exitstatus
;
1070 * Handle break and continue commands. Break, continue, and return are
1071 * all handled by setting the evalskip flag. The evaluation routines
1072 * above all check this flag, and if it is set they start skipping
1073 * commands rather than executing them. The variable skipcount is
1074 * the number of loops to break/continue, or the number of function
1075 * levels to return. (The latter is always 1.) It should probably
1076 * be an error to break out of more loops than exist, but it isn't
1077 * in the standard shell so we don't make it one here.
1081 breakcmd(int argc
, char **argv
)
1083 int n
= argc
> 1 ? number(argv
[1]) : 1;
1090 evalskip
= (**argv
== 'c')? SKIPCONT
: SKIPBREAK
;
1098 * The return command.
1102 returncmd(int argc
, char **argv
)
1108 * If called outside a function, do what ksh does;
1109 * skip the rest of the file.
1113 status
= number(argv
[1]);
1116 status
= exitstatus
;
1125 falsecmd(int argc
, char **argv
)
1132 truecmd(int argc
, char **argv
)
1139 execcmd(int argc
, char **argv
)
1142 iflag
= 0; /* exit on error */
1145 shellexec(argv
+ 1, pathval(), 0);
1152 eprintlist(struct output
*out
, struct strlist
*sp
, int sep
)
1160 outfmt(out
, p
, sp
->text
);