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
[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/eval.c,v 1.42 2004/04/06 20:06:51 markm Exp $");
49 #include <sys/wait.h> /* For WIFSIGNALED(status) */
75 #if !defined(NO_HISTORY) && !defined(EDITLINE)
76 #include "myhistedit.h"
80 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin:"
83 /* flags in argument to evaltree */
84 #define EV_EXIT 01 /* exit after evaluating tree */
85 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
86 #define EV_BACKCMD 04 /* command executing within back quotes */
88 MKINIT
int evalskip
; /* set if we are skipping commands */
89 STATIC
int skipcount
; /* number of levels to skip */
90 MKINIT
int loopnest
; /* current loop nesting level */
91 int funcnest
; /* depth of function calls */
95 struct strlist
*cmdenviron
;
96 int exitstatus
; /* exit status of last command */
97 int oexitstatus
; /* saved exit status */
100 STATIC
void evalloop(union node
*);
101 STATIC
void evalfor(union node
*);
102 STATIC
void evalcase(union node
*, int);
103 STATIC
void evalsubshell(union node
*, int);
104 STATIC
void expredir(union node
*);
105 STATIC
void evalpipe(union node
*);
106 STATIC
void evalcommand(union node
*, int, struct backcmd
*);
107 STATIC
void prehash(union node
*);
111 * Called to reset things after an exception.
135 evalcmd(int argc
, char **argv
)
144 STARTSTACKSTR(concat
);
148 STPUTC(*p
++, concat
);
149 if ((p
= *ap
++) == NULL
)
153 STPUTC('\0', concat
);
154 p
= grabstackstr(concat
);
163 * Execute a command or commands contained in a string.
170 struct stackmark smark
;
172 setstackmark(&smark
);
173 setinputstring(s
, 1);
174 while ((n
= parsecmd(0)) != NEOF
) {
176 popstackmark(&smark
);
179 popstackmark(&smark
);
185 * Evaluate a parse tree. The value is left in the global variable
190 evaltree(union node
*n
, int flags
)
193 TRACE(("evaltree(NULL) called\n"));
197 #if !defined(NO_HISTORY) && !defined(EDITLINE)
198 displayhist
= 1; /* show history substitutions done with fc */
200 TRACE(("evaltree(0x%lx: %d) called\n", (long)n
, n
->type
));
203 evaltree(n
->nbinary
.ch1
, 0);
206 evaltree(n
->nbinary
.ch2
, flags
);
209 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
210 if (evalskip
|| exitstatus
!= 0) {
213 evaltree(n
->nbinary
.ch2
, flags
);
216 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
217 if (evalskip
|| exitstatus
== 0)
219 evaltree(n
->nbinary
.ch2
, flags
);
222 expredir(n
->nredir
.redirect
);
223 redirect(n
->nredir
.redirect
, REDIR_PUSH
);
224 evaltree(n
->nredir
.n
, flags
);
228 evalsubshell(n
, flags
);
231 evalsubshell(n
, flags
);
234 evaltree(n
->nif
.test
, EV_TESTED
);
238 evaltree(n
->nif
.ifpart
, flags
);
239 else if (n
->nif
.elsepart
)
240 evaltree(n
->nif
.elsepart
, flags
);
256 defun(n
->narg
.text
, n
->narg
.next
);
260 evaltree(n
->nnot
.com
, EV_TESTED
);
261 exitstatus
= !exitstatus
;
268 evalcommand(n
, flags
, (struct backcmd
*)NULL
);
271 out1fmt("Node type = %d\n", n
->type
);
278 if ((flags
& EV_EXIT
) || (eflag
&& exitstatus
279 && !(flags
& EV_TESTED
) && (n
->type
== NCMD
||
280 n
->type
== NSUBSHELL
)))
281 exitshell(exitstatus
);
286 evalloop(union node
*n
)
293 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
295 skipping
: if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
299 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
303 if (n
->type
== NWHILE
) {
310 evaltree(n
->nbinary
.ch2
, 0);
322 evalfor(union node
*n
)
324 struct arglist arglist
;
327 struct stackmark smark
;
329 setstackmark(&smark
);
330 arglist
.lastp
= &arglist
.list
;
331 for (argp
= n
->nfor
.args
; argp
; argp
= argp
->narg
.next
) {
332 oexitstatus
= exitstatus
;
333 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
337 *arglist
.lastp
= NULL
;
341 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
342 setvar(n
->nfor
.var
, sp
->text
, 0);
343 evaltree(n
->nfor
.body
, 0);
345 if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
349 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
356 popstackmark(&smark
);
362 evalcase(union node
*n
, int flags
)
366 struct arglist arglist
;
367 struct stackmark smark
;
369 setstackmark(&smark
);
370 arglist
.lastp
= &arglist
.list
;
371 oexitstatus
= exitstatus
;
372 expandarg(n
->ncase
.expr
, &arglist
, EXP_TILDE
);
373 for (cp
= n
->ncase
.cases
; cp
&& evalskip
== 0 ; cp
= cp
->nclist
.next
) {
374 for (patp
= cp
->nclist
.pattern
; patp
; patp
= patp
->narg
.next
) {
375 if (casematch(patp
, arglist
.list
->text
)) {
377 evaltree(cp
->nclist
.body
, flags
);
384 popstackmark(&smark
);
390 * Kick off a subshell to evaluate a tree.
394 evalsubshell(union node
*n
, int flags
)
397 int backgnd
= (n
->type
== NBACKGND
);
399 expredir(n
->nredir
.redirect
);
401 if (forkshell(jp
, n
, backgnd
) == 0) {
404 redirect(n
->nredir
.redirect
, 0);
405 evaltree(n
->nredir
.n
, flags
| EV_EXIT
); /* never returns */
409 exitstatus
= waitforjob(jp
, (int *)NULL
);
417 * Compute the names of the files in a redirection list.
421 expredir(union node
*n
)
425 for (redir
= n
; redir
; redir
= redir
->nfile
.next
) {
428 oexitstatus
= exitstatus
;
429 switch (redir
->type
) {
435 expandarg(redir
->nfile
.fname
, &fn
, EXP_TILDE
| EXP_REDIR
);
436 redir
->nfile
.expfname
= fn
.list
->text
;
440 if (redir
->ndup
.vname
) {
441 expandarg(redir
->ndup
.vname
, &fn
, EXP_FULL
| EXP_TILDE
);
442 fixredir(redir
, fn
.list
->text
, 1);
452 * Evaluate a pipeline. All the processes in the pipeline are children
453 * of the process creating the pipeline. (This differs from some versions
454 * of the shell, which make the last process in a pipeline the parent
459 evalpipe(union node
*n
)
467 TRACE(("evalpipe(0x%lx) called\n", (long)n
));
469 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
)
472 jp
= makejob(n
, pipelen
);
474 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
480 error("Pipe call failed: %s", strerror(errno
));
483 if (forkshell(jp
, lp
->n
, n
->npipe
.backgnd
) == 0) {
490 if (!(prevfd
>= 0 && pip
[0] == 0))
497 evaltree(lp
->n
, EV_EXIT
);
505 if (n
->npipe
.backgnd
== 0) {
507 exitstatus
= waitforjob(jp
, (int *)NULL
);
508 TRACE(("evalpipe: job done exit status %d\n", exitstatus
));
516 * Execute a command inside back quotes. If it's a builtin command, we
517 * want to save its output in a block obtained from malloc. Otherwise
518 * we fork off a subprocess and get the output of the command via a pipe.
519 * Should be called with interrupts off.
523 evalbackcmd(union node
*n
, struct backcmd
*result
)
527 struct stackmark smark
; /* unnecessary */
529 setstackmark(&smark
);
538 if (n
->type
== NCMD
) {
539 exitstatus
= oexitstatus
;
540 evalcommand(n
, EV_BACKCMD
, result
);
544 error("Pipe call failed: %s", strerror(errno
));
546 if (forkshell(jp
, n
, FORK_NOJOB
) == 0) {
553 evaltree(n
, EV_EXIT
);
560 popstackmark(&smark
);
561 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
562 result
->fd
, result
->buf
, result
->nleft
, result
->jp
));
568 * Execute a simple command.
572 evalcommand(union node
*cmd
, int flags
, struct backcmd
*backcmd
)
574 struct stackmark smark
;
576 struct arglist arglist
;
577 struct arglist varlist
;
585 struct cmdentry cmdentry
;
587 struct jmploc jmploc
;
588 struct jmploc
*volatile savehandler
;
589 char *volatile savecmdname
;
590 volatile struct shparam saveparam
;
591 struct localvar
*volatile savelocalvars
;
595 int do_clearcmdentry
;
597 /* Avoid longjmp clobbering */
602 (void) &do_clearcmdentry
;
605 /* First expand the arguments. */
606 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd
, flags
));
607 setstackmark(&smark
);
608 arglist
.lastp
= &arglist
.list
;
609 varlist
.lastp
= &varlist
.list
;
611 do_clearcmdentry
= 0;
612 oexitstatus
= exitstatus
;
614 for (argp
= cmd
->ncmd
.args
; argp
; argp
= argp
->narg
.next
) {
615 char *p
= argp
->narg
.text
;
616 if (varflag
&& is_name(*p
)) {
619 } while (is_in_name(*p
));
621 expandarg(argp
, &varlist
, EXP_VARTILDE
);
625 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
628 *arglist
.lastp
= NULL
;
629 *varlist
.lastp
= NULL
;
630 expredir(cmd
->ncmd
.redirect
);
632 for (sp
= arglist
.list
; sp
; sp
= sp
->next
)
634 argv
= stalloc(sizeof (char *) * (argc
+ 1));
636 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
637 TRACE(("evalcommand arg: %s\n", sp
->text
));
642 if (iflag
&& funcnest
== 0 && argc
> 0)
646 /* Print the command if xflag is set. */
649 for (sp
= varlist
.list
; sp
; sp
= sp
->next
) {
653 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
661 /* Now locate the command. */
663 cmdentry
.cmdtype
= CMDBUILTIN
;
664 cmdentry
.u
.index
= BLTINCMD
;
666 static const char PATH
[] = "PATH=";
667 char *path
= pathval();
670 * Modify the command lookup path, if a PATH= assignment
673 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
674 if (strncmp(sp
->text
, PATH
, sizeof(PATH
) - 1) == 0) {
675 path
= sp
->text
+ sizeof(PATH
) - 1;
677 * On `PATH=... command`, we need to make
678 * sure that the command isn't using the
679 * non-updated hash table of the outer PATH
680 * setting and we need to make sure that
681 * the hash table isn't filled with items
682 * from the temporary setting.
684 * It would be better to forbit using and
685 * updating the table while this command
686 * runs, by the command finding mechanism
687 * is heavily integrated with hash handling,
688 * so we just delete the hash before and after
689 * the command runs. Partly deleting like
690 * changepatch() does doesn't seem worth the
691 * bookinging effort, since most such runs add
692 * directories in front of the new PATH.
695 do_clearcmdentry
= 1;
698 find_command(argv
[0], &cmdentry
, 1, path
);
699 if (cmdentry
.cmdtype
== CMDUNKNOWN
) { /* command not found */
704 /* implement the bltin builtin here */
705 if (cmdentry
.cmdtype
== CMDBUILTIN
&& cmdentry
.u
.index
== BLTINCMD
) {
710 if ((cmdentry
.u
.index
= find_builtin(*argv
)) < 0) {
711 outfmt(&errout
, "%s: not found\n", *argv
);
716 if (cmdentry
.u
.index
!= BLTINCMD
)
722 /* Fork off a child process if necessary. */
723 if (cmd
->ncmd
.backgnd
724 || (cmdentry
.cmdtype
== CMDNORMAL
725 && ((flags
& EV_EXIT
) == 0 || Tflag
))
726 || ((flags
& EV_BACKCMD
) != 0
727 && (cmdentry
.cmdtype
!= CMDBUILTIN
728 || cmdentry
.u
.index
== CDCMD
729 || cmdentry
.u
.index
== DOTCMD
730 || cmdentry
.u
.index
== EVALCMD
))
731 || (cmdentry
.cmdtype
== CMDBUILTIN
&&
732 cmdentry
.u
.index
== COMMANDCMD
)) {
733 jp
= makejob(cmd
, 1);
734 mode
= cmd
->ncmd
.backgnd
;
735 if (flags
& EV_BACKCMD
) {
738 error("Pipe call failed: %s", strerror(errno
));
740 if (forkshell(jp
, cmd
, mode
) != 0)
741 goto parent
; /* at end of routine */
742 if (flags
& EV_BACKCMD
) {
753 /* This is the child process if a fork occurred. */
754 /* Execute the command. */
755 if (cmdentry
.cmdtype
== CMDFUNCTION
) {
757 trputs("Shell function: "); trargs(argv
);
759 redirect(cmd
->ncmd
.redirect
, REDIR_PUSH
);
760 saveparam
= shellparam
;
761 shellparam
.malloc
= 0;
762 shellparam
.reset
= 1;
763 shellparam
.nparam
= argc
- 1;
764 shellparam
.p
= argv
+ 1;
765 shellparam
.optnext
= NULL
;
767 savelocalvars
= localvars
;
770 if (setjmp(jmploc
.loc
)) {
771 if (exception
== EXSHELLPROC
)
772 freeparam((struct shparam
*)&saveparam
);
774 freeparam(&shellparam
);
775 shellparam
= saveparam
;
778 localvars
= savelocalvars
;
779 handler
= savehandler
;
780 longjmp(handler
->loc
, 1);
782 savehandler
= handler
;
784 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
787 if (flags
& EV_TESTED
)
788 evaltree(cmdentry
.u
.func
, EV_TESTED
);
790 evaltree(cmdentry
.u
.func
, 0);
794 localvars
= savelocalvars
;
795 freeparam(&shellparam
);
796 shellparam
= saveparam
;
797 handler
= savehandler
;
800 if (evalskip
== SKIPFUNC
) {
805 exitshell(exitstatus
);
806 } else if (cmdentry
.cmdtype
== CMDBUILTIN
) {
808 trputs("builtin command: "); trargs(argv
);
810 mode
= (cmdentry
.u
.index
== EXECCMD
)? 0 : REDIR_PUSH
;
811 if (flags
== EV_BACKCMD
) {
813 memout
.nextc
= memout
.buf
;
817 redirect(cmd
->ncmd
.redirect
, mode
);
818 savecmdname
= commandname
;
819 cmdenviron
= varlist
.list
;
821 if (setjmp(jmploc
.loc
)) {
823 exitstatus
= (e
== EXINT
)? SIGINT
+128 : 2;
826 savehandler
= handler
;
828 commandname
= argv
[0];
830 optptr
= NULL
; /* initialize nextopt */
831 exitstatus
= (*builtinfunc
[cmdentry
.u
.index
])(argc
, argv
);
838 if (e
!= EXSHELLPROC
) {
839 commandname
= savecmdname
;
840 if (flags
& EV_EXIT
) {
841 exitshell(exitstatus
);
844 handler
= savehandler
;
846 if ((e
!= EXERROR
&& e
!= EXEXEC
)
847 || cmdentry
.u
.index
== BLTINCMD
848 || cmdentry
.u
.index
== DOTCMD
849 || cmdentry
.u
.index
== EVALCMD
851 || cmdentry
.u
.index
== HISTCMD
853 || cmdentry
.u
.index
== EXECCMD
854 || cmdentry
.u
.index
== COMMANDCMD
)
858 if (cmdentry
.u
.index
!= EXECCMD
)
860 if (flags
== EV_BACKCMD
) {
861 backcmd
->buf
= memout
.buf
;
862 backcmd
->nleft
= memout
.nextc
- memout
.buf
;
867 trputs("normal command: "); trargs(argv
);
870 redirect(cmd
->ncmd
.redirect
, 0);
871 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
872 setvareq(sp
->text
, VEXPORT
|VSTACK
);
873 envp
= environment();
874 shellexec(argv
, envp
, pathval(), cmdentry
.u
.index
);
879 parent
: /* parent process gets here (if we forked) */
880 if (mode
== 0) { /* argument to fork */
882 exitstatus
= waitforjob(jp
, &realstatus
);
884 if (iflag
&& loopnest
> 0 && WIFSIGNALED(realstatus
)) {
885 evalskip
= SKIPBREAK
;
886 skipcount
= loopnest
;
888 } else if (mode
== 2) {
889 backcmd
->fd
= pip
[0];
896 setvar("_", lastarg
, 0);
897 if (do_clearcmdentry
)
899 popstackmark(&smark
);
905 * Search for a command. This is called before we fork so that the
906 * location of the command will be available in the parent as well as
907 * the child. The check for "goodname" is an overly conservative
908 * check that the name will not be subject to expansion.
912 prehash(union node
*n
)
914 struct cmdentry entry
;
916 if (n
->type
== NCMD
&& n
->ncmd
.args
)
917 if (goodname(n
->ncmd
.args
->narg
.text
))
918 find_command(n
->ncmd
.args
->narg
.text
, &entry
, 0,
925 * Builtin commands. Builtin commands whose functions are closely
926 * tied to evaluation are implemented here.
930 * No command given, or a bltin command with no arguments. Set the
931 * specified variables.
935 bltincmd(int argc __unused
, char **argv __unused
)
937 listsetvar(cmdenviron
);
939 * Preserve exitstatus of a previous possible redirection
947 * Handle break and continue commands. Break, continue, and return are
948 * all handled by setting the evalskip flag. The evaluation routines
949 * above all check this flag, and if it is set they start skipping
950 * commands rather than executing them. The variable skipcount is
951 * the number of loops to break/continue, or the number of function
952 * levels to return. (The latter is always 1.) It should probably
953 * be an error to break out of more loops than exist, but it isn't
954 * in the standard shell so we don't make it one here.
958 breakcmd(int argc
, char **argv
)
960 int n
= argc
> 1 ? number(argv
[1]) : 1;
965 evalskip
= (**argv
== 'c')? SKIPCONT
: SKIPBREAK
;
972 * The `command' command.
975 commandcmd(int argc
, char **argv
)
977 static char stdpath
[] = _PATH_STDPATH
;
978 struct jmploc loc
, *old
;
983 for (sp
= cmdenviron
; sp
; sp
= sp
->next
)
984 setvareq(sp
->text
, VEXPORT
|VSTACK
);
987 optind
= optreset
= 1;
989 while ((ch
= getopt(argc
, argv
, "p")) != -1) {
996 error("unknown option: -%c", optopt
);
1005 if (setjmp(handler
->loc
) == 0)
1006 shellexec(argv
, environment(), path
, 0);
1008 if (exception
== EXEXEC
)
1014 * Do nothing successfully if no command was specified;
1015 * ksh also does this.
1022 * The return command.
1026 returncmd(int argc
, char **argv
)
1028 int ret
= argc
> 1 ? number(argv
[1]) : oexitstatus
;
1031 evalskip
= SKIPFUNC
;
1034 /* skip the rest of the file */
1035 evalskip
= SKIPFILE
;
1043 falsecmd(int argc __unused
, char **argv __unused
)
1050 truecmd(int argc __unused
, char **argv __unused
)
1057 execcmd(int argc
, char **argv
)
1062 iflag
= 0; /* exit on error */
1065 for (sp
= cmdenviron
; sp
; sp
= sp
->next
)
1066 setvareq(sp
->text
, VEXPORT
|VSTACK
);
1067 shellexec(argv
+ 1, environment(), pathval(), 0);
1074 * $PchId: eval.c,v 1.7 2006/04/10 14:46:14 philip Exp $