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 * @(#)eval.c 8.9 (Berkeley) 6/8/95
37 * $FreeBSD: src/bin/sh/eval.c,v 1.27.2.5 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/eval.c,v 1.5 2006/05/12 01:56:11 y0netan1 Exp $
41 #include <sys/wait.h> /* For WIFSIGNALED(status) */
73 #include "myhistedit.h"
77 /* flags in argument to evaltree */
78 #define EV_EXIT 01 /* exit after evaluating tree */
79 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
80 #define EV_BACKCMD 04 /* command executing within back quotes */
82 MKINIT
int evalskip
; /* set if we are skipping commands */
83 STATIC
int skipcount
; /* number of levels to skip */
84 MKINIT
int loopnest
; /* current loop nesting level */
85 int funcnest
; /* depth of function calls */
88 const char *commandname
;
89 struct strlist
*cmdenviron
;
90 int exitstatus
; /* exit status of last command */
91 int oexitstatus
; /* saved exit status */
94 STATIC
void evalloop(union node
*);
95 STATIC
void evalfor(union node
*);
96 STATIC
void evalcase(union node
*, int);
97 STATIC
void evalsubshell(union node
*, int);
98 STATIC
void expredir(union node
*);
99 STATIC
void evalpipe(union node
*);
100 STATIC
void evalcommand(union node
*, int, struct backcmd
*);
101 STATIC
void prehash(union node
*);
105 * Called to reset things after an exception.
129 evalcmd(int argc
, char **argv
)
138 STARTSTACKSTR(concat
);
142 STPUTC(*p
++, concat
);
143 if ((p
= *ap
++) == NULL
)
147 STPUTC('\0', concat
);
148 p
= grabstackstr(concat
);
157 * Execute a command or commands contained in a string.
164 struct stackmark smark
;
166 setstackmark(&smark
);
167 setinputstring(s
, 1);
168 while ((n
= parsecmd(0)) != NEOF
) {
170 popstackmark(&smark
);
173 popstackmark(&smark
);
179 * Evaluate a parse tree. The value is left in the global variable
184 evaltree(union node
*n
, int flags
)
187 TRACE(("evaltree(NULL) called\n"));
192 displayhist
= 1; /* show history substitutions done with fc */
194 TRACE(("evaltree(0x%lx: %d) called\n", (long)n
, n
->type
));
197 evaltree(n
->nbinary
.ch1
, 0);
200 evaltree(n
->nbinary
.ch2
, flags
);
203 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
204 if (evalskip
|| exitstatus
!= 0) {
207 evaltree(n
->nbinary
.ch2
, flags
);
210 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
211 if (evalskip
|| exitstatus
== 0)
213 evaltree(n
->nbinary
.ch2
, flags
);
216 expredir(n
->nredir
.redirect
);
217 redirect(n
->nredir
.redirect
, REDIR_PUSH
);
218 evaltree(n
->nredir
.n
, flags
);
222 evalsubshell(n
, flags
);
225 evalsubshell(n
, flags
);
228 evaltree(n
->nif
.test
, EV_TESTED
);
232 evaltree(n
->nif
.ifpart
, flags
);
233 else if (n
->nif
.elsepart
)
234 evaltree(n
->nif
.elsepart
, flags
);
250 defun(n
->narg
.text
, n
->narg
.next
);
254 evaltree(n
->nnot
.com
, EV_TESTED
);
255 exitstatus
= !exitstatus
;
262 evalcommand(n
, flags
, (struct backcmd
*)NULL
);
265 out1fmt("Node type = %d\n", n
->type
);
272 if ((flags
& EV_EXIT
) || (eflag
&& exitstatus
273 && !(flags
& EV_TESTED
) && (n
->type
== NCMD
||
274 n
->type
== NSUBSHELL
)))
275 exitshell(exitstatus
);
280 evalloop(union node
*n
)
287 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
289 skipping
: if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
293 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
297 if (n
->type
== NWHILE
) {
304 evaltree(n
->nbinary
.ch2
, 0);
316 evalfor(union node
*n
)
318 struct arglist arglist
;
321 struct stackmark smark
;
323 setstackmark(&smark
);
324 arglist
.lastp
= &arglist
.list
;
325 for (argp
= n
->nfor
.args
; argp
; argp
= argp
->narg
.next
) {
326 oexitstatus
= exitstatus
;
327 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
331 *arglist
.lastp
= NULL
;
335 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
336 setvar(n
->nfor
.var
, sp
->text
, 0);
337 evaltree(n
->nfor
.body
, 0);
339 if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
343 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
350 popstackmark(&smark
);
356 evalcase(union node
*n
, int flags
)
360 struct arglist arglist
;
361 struct stackmark smark
;
363 setstackmark(&smark
);
364 arglist
.lastp
= &arglist
.list
;
365 oexitstatus
= exitstatus
;
366 expandarg(n
->ncase
.expr
, &arglist
, EXP_TILDE
);
367 for (cp
= n
->ncase
.cases
; cp
&& evalskip
== 0 ; cp
= cp
->nclist
.next
) {
368 for (patp
= cp
->nclist
.pattern
; patp
; patp
= patp
->narg
.next
) {
369 if (casematch(patp
, arglist
.list
->text
)) {
371 evaltree(cp
->nclist
.body
, flags
);
378 popstackmark(&smark
);
384 * Kick off a subshell to evaluate a tree.
388 evalsubshell(union node
*n
, int flags
)
391 int backgnd
= (n
->type
== NBACKGND
);
393 expredir(n
->nredir
.redirect
);
395 if (forkshell(jp
, n
, backgnd
) == 0) {
398 redirect(n
->nredir
.redirect
, 0);
399 evaltree(n
->nredir
.n
, flags
| EV_EXIT
); /* never returns */
403 exitstatus
= waitforjob(jp
, (int *)NULL
);
411 * Compute the names of the files in a redirection list.
415 expredir(union node
*n
)
419 for (redir
= n
; redir
; redir
= redir
->nfile
.next
) {
422 oexitstatus
= exitstatus
;
423 switch (redir
->type
) {
429 expandarg(redir
->nfile
.fname
, &fn
, EXP_TILDE
| EXP_REDIR
);
430 redir
->nfile
.expfname
= fn
.list
->text
;
434 if (redir
->ndup
.vname
) {
435 expandarg(redir
->ndup
.vname
, &fn
, EXP_FULL
| EXP_TILDE
);
436 fixredir(redir
, fn
.list
->text
, 1);
446 * Evaluate a pipeline. All the processes in the pipeline are children
447 * of the process creating the pipeline. (This differs from some versions
448 * of the shell, which make the last process in a pipeline the parent
453 evalpipe(union node
*n
)
461 TRACE(("evalpipe(0x%lx) called\n", (long)n
));
463 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
)
466 jp
= makejob(n
, pipelen
);
468 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
474 error("Pipe call failed: %s", strerror(errno
));
477 if (forkshell(jp
, lp
->n
, n
->npipe
.backgnd
) == 0) {
485 if (!(prevfd
>= 0 && pip
[0] == 0))
493 evaltree(lp
->n
, EV_EXIT
);
501 if (n
->npipe
.backgnd
== 0) {
503 exitstatus
= waitforjob(jp
, (int *)NULL
);
504 TRACE(("evalpipe: job done exit status %d\n", exitstatus
));
512 * Execute a command inside back quotes. If it's a builtin command, we
513 * want to save its output in a block obtained from malloc. Otherwise
514 * we fork off a subprocess and get the output of the command via a pipe.
515 * Should be called with interrupts off.
519 evalbackcmd(union node
*n
, struct backcmd
*result
)
523 struct stackmark smark
; /* unnecessary */
525 setstackmark(&smark
);
534 if (n
->type
== NCMD
) {
535 exitstatus
= oexitstatus
;
536 evalcommand(n
, EV_BACKCMD
, result
);
540 error("Pipe call failed: %s", strerror(errno
));
542 if (forkshell(jp
, n
, FORK_NOJOB
) == 0) {
550 evaltree(n
, EV_EXIT
);
557 popstackmark(&smark
);
558 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
559 result
->fd
, result
->buf
, result
->nleft
, result
->jp
));
565 * Execute a simple command.
569 evalcommand(union node
*cmd
, int flags
, struct backcmd
*backcmd
)
571 struct stackmark smark
;
573 struct arglist arglist
;
574 struct arglist varlist
;
582 struct cmdentry cmdentry
;
584 struct jmploc jmploc
;
585 struct jmploc
*volatile savehandler
;
586 const char *volatile savecmdname
;
587 volatile struct shparam saveparam
;
588 struct localvar
*volatile savelocalvars
;
592 int do_clearcmdentry
;
594 /* Avoid longjmp clobbering */
599 (void) &do_clearcmdentry
;
602 /* First expand the arguments. */
603 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd
, flags
));
604 setstackmark(&smark
);
605 arglist
.lastp
= &arglist
.list
;
606 varlist
.lastp
= &varlist
.list
;
608 do_clearcmdentry
= 0;
609 oexitstatus
= exitstatus
;
611 for (argp
= cmd
->ncmd
.args
; argp
; argp
= argp
->narg
.next
) {
612 char *p
= argp
->narg
.text
;
613 if (varflag
&& is_name(*p
)) {
616 } while (is_in_name(*p
));
618 expandarg(argp
, &varlist
, EXP_VARTILDE
);
622 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
625 *arglist
.lastp
= NULL
;
626 *varlist
.lastp
= NULL
;
627 expredir(cmd
->ncmd
.redirect
);
629 for (sp
= arglist
.list
; sp
; sp
= sp
->next
)
631 argv
= stalloc(sizeof (char *) * (argc
+ 1));
633 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
634 TRACE(("evalcommand arg: %s\n", sp
->text
));
639 if (iflag
&& funcnest
== 0 && argc
> 0)
643 /* Print the command if xflag is set. */
646 for (sp
= varlist
.list
; sp
; sp
= sp
->next
) {
650 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
658 /* Now locate the command. */
660 cmdentry
.cmdtype
= CMDBUILTIN
;
661 cmdentry
.u
.index
= BLTINCMD
;
663 static const char PATH
[] = "PATH=";
664 const char *path
= pathval();
667 * Modify the command lookup path, if a PATH= assignment
670 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
671 if (strncmp(sp
->text
, PATH
, sizeof(PATH
) - 1) == 0) {
672 path
= sp
->text
+ sizeof(PATH
) - 1;
674 * On `PATH=... command`, we need to make
675 * sure that the command isn't using the
676 * non-updated hash table of the outer PATH
677 * setting and we need to make sure that
678 * the hash table isn't filled with items
679 * from the temporary setting.
681 * It would be better to forbit using and
682 * updating the table while this command
683 * runs, by the command finding mechanism
684 * is heavily integrated with hash handling,
685 * so we just delete the hash before and after
686 * the command runs. Partly deleting like
687 * changepatch() does doesn't seem worth the
688 * bookinging effort, since most such runs add
689 * diretories in front of the new PATH.
692 do_clearcmdentry
= 1;
695 find_command(argv
[0], &cmdentry
, 1, path
);
696 if (cmdentry
.cmdtype
== CMDUNKNOWN
) { /* command not found */
701 /* implement the bltin builtin here */
702 if (cmdentry
.cmdtype
== CMDBUILTIN
&& cmdentry
.u
.index
== BLTINCMD
) {
707 if ((cmdentry
.u
.index
= find_builtin(*argv
)) < 0) {
708 outfmt(&errout
, "%s: not found\n", *argv
);
713 if (cmdentry
.u
.index
!= BLTINCMD
)
719 /* Fork off a child process if necessary. */
720 if (cmd
->ncmd
.backgnd
721 || (cmdentry
.cmdtype
== CMDNORMAL
722 && ((flags
& EV_EXIT
) == 0 || Tflag
))
723 || ((flags
& EV_BACKCMD
) != 0
724 && (cmdentry
.cmdtype
!= CMDBUILTIN
725 || cmdentry
.u
.index
== CDCMD
726 || cmdentry
.u
.index
== DOTCMD
727 || cmdentry
.u
.index
== EVALCMD
))
728 || (cmdentry
.cmdtype
== CMDBUILTIN
&&
729 cmdentry
.u
.index
== COMMANDCMD
)) {
730 jp
= makejob(cmd
, 1);
731 mode
= cmd
->ncmd
.backgnd
;
732 if (flags
& EV_BACKCMD
) {
735 error("Pipe call failed: %s", strerror(errno
));
737 if (forkshell(jp
, cmd
, mode
) != 0)
738 goto parent
; /* at end of routine */
739 if (flags
& EV_BACKCMD
) {
751 /* This is the child process if a fork occurred. */
752 /* Execute the command. */
753 if (cmdentry
.cmdtype
== CMDFUNCTION
) {
755 trputs("Shell function: "); trargs(argv
);
757 redirect(cmd
->ncmd
.redirect
, REDIR_PUSH
);
758 saveparam
= shellparam
;
759 shellparam
.malloc
= 0;
760 shellparam
.reset
= 1;
761 shellparam
.nparam
= argc
- 1;
762 shellparam
.p
= argv
+ 1;
763 shellparam
.optnext
= NULL
;
765 savelocalvars
= localvars
;
768 if (setjmp(jmploc
.loc
)) {
769 if (exception
== EXSHELLPROC
)
770 freeparam((struct shparam
*)&saveparam
);
772 freeparam(&shellparam
);
773 shellparam
= saveparam
;
776 localvars
= savelocalvars
;
777 handler
= savehandler
;
778 longjmp(handler
->loc
, 1);
780 savehandler
= handler
;
782 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
785 if (flags
& EV_TESTED
)
786 evaltree(cmdentry
.u
.func
, EV_TESTED
);
788 evaltree(cmdentry
.u
.func
, 0);
792 localvars
= savelocalvars
;
793 freeparam(&shellparam
);
794 shellparam
= saveparam
;
795 handler
= savehandler
;
798 if (evalskip
== SKIPFUNC
) {
803 exitshell(exitstatus
);
804 } else if (cmdentry
.cmdtype
== CMDBUILTIN
) {
806 trputs("builtin command: "); trargs(argv
);
808 mode
= (cmdentry
.u
.index
== EXECCMD
)? 0 : REDIR_PUSH
;
809 if (flags
== EV_BACKCMD
) {
811 memout
.nextc
= memout
.buf
;
815 redirect(cmd
->ncmd
.redirect
, mode
);
816 savecmdname
= commandname
;
817 cmdenviron
= varlist
.list
;
819 if (setjmp(jmploc
.loc
)) {
821 exitstatus
= (e
== EXINT
)? SIGINT
+128 : 2;
824 savehandler
= handler
;
826 commandname
= argv
[0];
828 optptr
= NULL
; /* initialize nextopt */
829 exitstatus
= (*builtinfunc
[cmdentry
.u
.index
])(argc
, argv
);
836 if (e
!= EXSHELLPROC
) {
837 commandname
= savecmdname
;
838 if (flags
& EV_EXIT
) {
839 exitshell(exitstatus
);
842 handler
= savehandler
;
844 if ((e
!= EXERROR
&& e
!= EXEXEC
)
845 || cmdentry
.u
.index
== BLTINCMD
846 || cmdentry
.u
.index
== DOTCMD
847 || cmdentry
.u
.index
== EVALCMD
849 || cmdentry
.u
.index
== HISTCMD
851 || cmdentry
.u
.index
== EXECCMD
852 || cmdentry
.u
.index
== COMMANDCMD
)
856 if (cmdentry
.u
.index
!= EXECCMD
)
858 if (flags
== EV_BACKCMD
) {
859 backcmd
->buf
= memout
.buf
;
860 backcmd
->nleft
= memout
.nextc
- memout
.buf
;
865 trputs("normal command: "); trargs(argv
);
868 redirect(cmd
->ncmd
.redirect
, 0);
869 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
870 setvareq(sp
->text
, VEXPORT
|VSTACK
);
871 envp
= environment();
872 shellexec(argv
, envp
, pathval(), cmdentry
.u
.index
);
877 parent
: /* parent process gets here (if we forked) */
878 if (mode
== 0) { /* argument to fork */
880 exitstatus
= waitforjob(jp
, &realstatus
);
882 if (iflag
&& loopnest
> 0 && WIFSIGNALED(realstatus
)) {
883 evalskip
= SKIPBREAK
;
884 skipcount
= loopnest
;
886 } else if (mode
== 2) {
887 backcmd
->fd
= pip
[0];
894 setvar("_", lastarg
, 0);
895 if (do_clearcmdentry
)
897 popstackmark(&smark
);
903 * Search for a command. This is called before we fork so that the
904 * location of the command will be available in the parent as well as
905 * the child. The check for "goodname" is an overly conservative
906 * check that the name will not be subject to expansion.
910 prehash(union node
*n
)
912 struct cmdentry entry
;
914 if (n
->type
== NCMD
&& n
->ncmd
.args
)
915 if (goodname(n
->ncmd
.args
->narg
.text
))
916 find_command(n
->ncmd
.args
->narg
.text
, &entry
, 0,
923 * Builtin commands. Builtin commands whose functions are closely
924 * tied to evaluation are implemented here.
928 * No command given, or a bltin command with no arguments. Set the
929 * specified variables.
933 bltincmd(int argc __unused
, char **argv __unused
)
935 listsetvar(cmdenviron
);
937 * Preserve exitstatus of a previous possible redirection
945 * Handle break and continue commands. Break, continue, and return are
946 * all handled by setting the evalskip flag. The evaluation routines
947 * above all check this flag, and if it is set they start skipping
948 * commands rather than executing them. The variable skipcount is
949 * the number of loops to break/continue, or the number of function
950 * levels to return. (The latter is always 1.) It should probably
951 * be an error to break out of more loops than exist, but it isn't
952 * in the standard shell so we don't make it one here.
956 breakcmd(int argc
, char **argv
)
958 int n
= argc
> 1 ? number(argv
[1]) : 1;
963 evalskip
= (**argv
== 'c')? SKIPCONT
: SKIPBREAK
;
970 * The `command' command.
973 commandcmd(int argc
, char **argv
)
975 static char stdpath
[] = _PATH_STDPATH
;
976 struct jmploc loc
, *old
;
981 for (sp
= cmdenviron
; sp
; sp
= sp
->next
)
982 setvareq(sp
->text
, VEXPORT
|VSTACK
);
985 optind
= optreset
= 1;
987 while ((ch
= getopt(argc
, argv
, "p")) != -1) {
994 error("unknown option: -%c", optopt
);
1003 if (setjmp(handler
->loc
) == 0)
1004 shellexec(argv
, environment(), path
, 0);
1006 if (exception
== EXEXEC
)
1012 * Do nothing successfully if no command was specified;
1013 * ksh also does this.
1020 * The return command.
1024 returncmd(int argc
, char **argv
)
1026 int ret
= argc
> 1 ? number(argv
[1]) : oexitstatus
;
1029 evalskip
= SKIPFUNC
;
1032 /* skip the rest of the file */
1033 evalskip
= SKIPFILE
;
1041 falsecmd(int argc __unused
, char **argv __unused
)
1048 truecmd(int argc __unused
, char **argv __unused
)
1055 execcmd(int argc
, char **argv
)
1060 iflag
= 0; /* exit on error */
1063 for (sp
= cmdenviron
; sp
; sp
= sp
->next
)
1064 setvareq(sp
->text
, VEXPORT
|VSTACK
);
1065 shellexec(argv
+ 1, environment(), pathval(), 0);