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>
64 #include "myhistedit.h"
68 /* flags in argument to evaltree */
69 #define EV_EXIT 01 /* exit after evaluating tree */
70 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
71 #define EV_BACKCMD 04 /* command executing within back quotes */
73 int evalskip
; /* set if we are skipping commands */
74 STATIC
int skipcount
; /* number of levels to skip */
75 MKINIT
int loopnest
; /* current loop nesting level */
76 static int funcnest
; /* depth of function calls */
80 int exitstatus
; /* exit status of last command */
81 int back_exitstatus
; /* exit status of backquoted command */
84 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
87 void evaltreenr(union node
*, int) __attribute__ ((__noreturn__
));
88 STATIC
void evalloop(union node
*, int);
89 STATIC
void evalfor(union node
*, int);
90 STATIC
void evalcase(union node
*, int);
91 STATIC
void evalsubshell(union node
*, int);
92 STATIC
void expredir(union node
*);
93 STATIC
void evalpipe(union node
*, int);
95 STATIC
void evalcommand(union node
*, int, struct backcmd
*);
97 STATIC
void evalcommand(union node
*, int);
99 STATIC
int evalbltin(const struct builtincmd
*, int, char **, int);
100 STATIC
int evalfun(struct funcnode
*, int, char **, int);
101 STATIC
void prehash(union node
*);
102 STATIC
int eprintlist(struct output
*, struct strlist
*, int);
103 STATIC
int bltincmd(int, char **);
106 STATIC
const struct builtincmd bltin
= {
113 * Called to reset things after an exception.
131 static int evalcmd(int argc
, char **argv
, int flags
)
140 STARTSTACKSTR(concat
);
143 concat
= stputs(p
, concat
);
144 if ((p
= *ap
++) == NULL
)
148 STPUTC('\0', concat
);
149 p
= grabstackstr(concat
);
151 return evalstring(p
, flags
& EV_TESTED
);
158 * Execute a command or commands contained in a string.
162 evalstring(char *s
, int flags
)
165 struct stackmark smark
;
169 setstackmark(&smark
);
172 while ((n
= parsecmd(0)) != NEOF
) {
175 popstackmark(&smark
);
187 * Evaluate a parse tree. The value is left in the global variable
192 evaltree(union node
*n
, int flags
)
195 void (*evalfn
)(union node
*, int);
199 TRACE(("evaltree(NULL) called\n"));
203 displayhist
= 1; /* show history substitutions done with fc */
205 TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
206 getpid(), n
, n
->type
, flags
));
210 out1fmt("Node type = %d\n", n
->type
);
211 #ifndef USE_GLIBC_STDIO
217 evaltree(n
->nnot
.com
, EV_TESTED
);
218 status
= !exitstatus
;
221 expredir(n
->nredir
.redirect
);
222 status
= redirectsafe(n
->nredir
.redirect
, REDIR_PUSH
);
224 evaltree(n
->nredir
.n
, flags
& EV_TESTED
);
231 if (eflag
&& !(flags
& EV_TESTED
))
233 evalcommand(n
, flags
, (struct backcmd
*)NULL
);
236 evalfn
= evalcommand
;
238 if (eflag
&& !(flags
& EV_TESTED
))
251 evalfn
= evalsubshell
;
256 if (eflag
&& !(flags
& EV_TESTED
))
269 #error NAND + 1 != NOR
272 #error NOR + 1 != NSEMI
274 isor
= n
->type
- NAND
;
277 (flags
| ((isor
>> 1) - 1)) & EV_TESTED
279 if (!exitstatus
== isor
)
291 evaltree(n
->nif
.test
, EV_TESTED
);
294 if (exitstatus
== 0) {
297 } else if (n
->nif
.elsepart
) {
303 defun(n
->narg
.text
, n
->narg
.next
);
311 if ((checkexit
& exitstatus
) ||
312 (pendingsigs
&& dotrap()) ||
318 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
321 void evaltreenr(union node
*n
, int flags
)
322 #ifdef HAVE_ATTRIBUTE_ALIAS
323 __attribute__ ((alias("evaltree")));
333 evalloop(union node
*n
, int flags
)
343 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
345 skipping
: if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
349 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
354 if (n
->type
!= NWHILE
)
358 evaltree(n
->nbinary
.ch2
, flags
);
370 evalfor(union node
*n
, int flags
)
372 struct arglist arglist
;
375 struct stackmark smark
;
377 setstackmark(&smark
);
378 arglist
.lastp
= &arglist
.list
;
379 for (argp
= n
->nfor
.args
; argp
; argp
= argp
->narg
.next
) {
380 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
385 *arglist
.lastp
= NULL
;
390 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
391 setvar(n
->nfor
.var
, sp
->text
, 0);
392 evaltree(n
->nfor
.body
, flags
);
394 if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
398 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
405 popstackmark(&smark
);
411 evalcase(union node
*n
, int flags
)
415 struct arglist arglist
;
416 struct stackmark smark
;
418 setstackmark(&smark
);
419 arglist
.lastp
= &arglist
.list
;
420 expandarg(n
->ncase
.expr
, &arglist
, EXP_TILDE
);
422 for (cp
= n
->ncase
.cases
; cp
&& evalskip
== 0 ; cp
= cp
->nclist
.next
) {
423 for (patp
= cp
->nclist
.pattern
; patp
; patp
= patp
->narg
.next
) {
424 if (casematch(patp
, arglist
.list
->text
)) {
426 evaltree(cp
->nclist
.body
, flags
);
433 popstackmark(&smark
);
439 * Kick off a subshell to evaluate a tree.
443 evalsubshell(union node
*n
, int flags
)
446 int backgnd
= (n
->type
== NBACKGND
);
449 expredir(n
->nredir
.redirect
);
450 if (!backgnd
&& flags
& EV_EXIT
&& !trap
[0])
454 if (forkshell(jp
, n
, backgnd
) == 0) {
460 redirect(n
->nredir
.redirect
, 0);
461 evaltreenr(n
->nredir
.n
, flags
);
466 status
= waitforjob(jp
);
474 * Compute the names of the files in a redirection list.
478 expredir(union node
*n
)
482 for (redir
= n
; redir
; redir
= redir
->nfile
.next
) {
485 switch (redir
->type
) {
491 expandarg(redir
->nfile
.fname
, &fn
, EXP_TILDE
| EXP_REDIR
);
492 redir
->nfile
.expfname
= fn
.list
->text
;
496 if (redir
->ndup
.vname
) {
497 expandarg(redir
->ndup
.vname
, &fn
, EXP_FULL
| EXP_TILDE
);
498 fixredir(redir
, fn
.list
->text
, 1);
508 * Evaluate a pipeline. All the processes in the pipeline are children
509 * of the process creating the pipeline. (This differs from some versions
510 * of the shell, which make the last process in a pipeline the parent
515 evalpipe(union node
*n
, int flags
)
523 TRACE(("evalpipe(0x%lx) called\n", (long)n
));
525 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
)
529 jp
= makejob(n
, pipelen
);
531 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
537 sh_error("Pipe call failed");
540 if (forkshell(jp
, lp
->n
, n
->npipe
.backgnd
) == 0) {
553 evaltreenr(lp
->n
, flags
);
561 if (n
->npipe
.backgnd
== 0) {
562 exitstatus
= waitforjob(jp
);
563 TRACE(("evalpipe: job done exit status %d\n", exitstatus
));
571 * Execute a command inside back quotes. If it's a builtin command, we
572 * want to save its output in a block obtained from malloc. Otherwise
573 * we fork off a subprocess and get the output of the command via a pipe.
574 * Should be called with interrupts off.
578 evalbackcmd(union node
*n
, struct backcmd
*result
)
590 * For now we disable executing builtins in the same
591 * context as the shell, because we are not keeping
592 * enough state to recover from changes that are
593 * supposed only to affect subshells. eg. echo "`cd /`"
595 if (n
->type
== NCMD
) {
596 struct ifsregion saveifs
;
597 struct ifsregion
*savelastp
;
598 struct nodelist
*saveargbackq
;
601 savelastp
= ifslastp
;
602 saveargbackq
= argbackq
;
604 exitstatus
= oexitstatus
;
605 evalcommand(n
, EV_BACKCMD
, result
);
608 ifslastp
= savelastp
;
609 argbackq
= saveargbackq
;
617 sh_error("Pipe call failed");
619 if (forkshell(jp
, n
, FORK_NOJOB
) == 0) {
627 evaltreenr(n
, EV_EXIT
);
635 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
636 result
->fd
, result
->buf
, result
->nleft
, result
->jp
));
640 parse_command_args(char **argv
, const char **path
)
652 if (c
== '-' && !*cp
) {
663 /* run 'typecmd' for other options */
666 } while ((c
= *cp
++));
674 * Execute a simple command.
679 evalcommand(union node
*cmd
, int flags
, struct backcmd
*backcmd
)
681 evalcommand(union node
*cmd
, int flags
)
684 struct localvar_list
*localvar_stop
;
685 struct stackmark smark
;
687 struct arglist arglist
;
688 struct arglist varlist
;
695 struct cmdentry cmdentry
;
704 /* First expand the arguments. */
705 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd
, flags
));
706 setstackmark(&smark
);
707 localvar_stop
= pushlocalvars();
710 cmdentry
.cmdtype
= CMDBUILTIN
;
711 cmdentry
.u
.cmd
= &bltin
;
712 varlist
.lastp
= &varlist
.list
;
713 *varlist
.lastp
= NULL
;
714 arglist
.lastp
= &arglist
.list
;
715 *arglist
.lastp
= NULL
;
718 for (argp
= cmd
->ncmd
.args
; argp
; argp
= argp
->narg
.next
) {
719 struct strlist
**spp
;
722 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
723 for (sp
= *spp
; sp
; sp
= sp
->next
)
727 /* Reserve one extra spot at the front for shellexec. */
728 nargv
= stalloc(sizeof (char *) * (argc
+ 2));
730 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
731 TRACE(("evalcommand arg: %s\n", sp
->text
));
737 if (iflag
&& funcnest
== 0 && argc
> 0)
741 expredir(cmd
->ncmd
.redirect
);
742 status
= redirectsafe(cmd
->ncmd
.redirect
, REDIR_PUSH
|REDIR_SAVEFD2
);
745 for (argp
= cmd
->ncmd
.assign
; argp
; argp
= argp
->narg
.next
) {
746 struct strlist
**spp
;
750 expandarg(argp
, &varlist
, EXP_VARTILDE
);
752 mklocal((*spp
)->text
);
755 * Modify the command lookup path, if a PATH= assignment
759 if (varequal(p
, path
))
763 /* Print the command if xflag is set. */
769 outstr(expandstr(ps4val()), out
);
771 sep
= eprintlist(out
, varlist
.list
, sep
);
772 eprintlist(out
, arglist
.list
, sep
);
782 /* Now locate the command. */
785 int cmd_flag
= DO_ERR
;
790 find_command(argv
[0], &cmdentry
, cmd_flag
, path
);
791 if (cmdentry
.cmdtype
== CMDUNKNOWN
) {
799 /* implement bltin and command here */
800 if (cmdentry
.cmdtype
!= CMDBUILTIN
)
804 cmdentry
.u
.cmd
->flags
&
807 if (cmdentry
.u
.cmd
== EXECCMD
)
809 if (cmdentry
.u
.cmd
!= COMMANDCMD
)
813 nargv
= parse_command_args(argv
, &path
);
816 argc
-= nargv
- argv
;
818 cmd_flag
|= DO_NOFUNC
;
823 /* We have a redirection error. */
831 /* Execute the command. */
832 switch (cmdentry
.cmdtype
) {
834 /* Fork off a child process if necessary. */
835 if (!(flags
& EV_EXIT
) || trap
[0]) {
837 jp
= makejob(cmd
, 1);
838 if (forkshell(jp
, cmd
, FORK_FG
) != 0) {
839 exitstatus
= waitforjob(jp
);
845 listsetvar(varlist
.list
, VEXPORT
|VSTACK
);
846 shellexec(argv
, path
, cmdentry
.u
.index
);
850 if (spclbltin
> 0 || argc
== 0) {
852 if (execcmd
&& argc
> 1)
853 listsetvar(varlist
.list
, VEXPORT
);
855 if (evalbltin(cmdentry
.u
.cmd
, argc
, argv
, flags
)) {
863 status
= (i
== EXINT
) ? SIGINT
+ 128 : 2;
866 if (i
== EXINT
|| spclbltin
> 0) {
868 longjmp(handler
->loc
, 1);
876 if (evalfun(cmdentry
.u
.func
, argc
, argv
, flags
))
883 unwindlocalvars(localvar_stop
);
885 /* dsl: I think this is intended to be used to support
886 * '_' in 'vi' command mode during line editing...
887 * However I implemented that within libedit itself.
889 setvar("_", lastarg
, 0);
890 popstackmark(&smark
);
894 evalbltin(const struct builtincmd
*cmd
, int argc
, char **argv
, int flags
)
896 char *volatile savecmdname
;
897 struct jmploc
*volatile savehandler
;
898 struct jmploc jmploc
;
902 savecmdname
= commandname
;
903 if ((i
= setjmp(jmploc
.loc
)))
905 savehandler
= handler
;
907 commandname
= argv
[0];
909 optptr
= NULL
; /* initialize nextopt */
911 status
= evalcmd(argc
, argv
, flags
);
913 status
= (*cmd
->builtin
)(argc
, argv
);
915 status
|= outerr(out1
);
919 commandname
= savecmdname
;
920 handler
= savehandler
;
926 evalfun(struct funcnode
*func
, int argc
, char **argv
, int flags
)
928 volatile struct shparam saveparam
;
929 struct jmploc
*volatile savehandler
;
930 struct jmploc jmploc
;
933 saveparam
= shellparam
;
934 if ((e
= setjmp(jmploc
.loc
))) {
938 savehandler
= handler
;
940 shellparam
.malloc
= 0;
944 shellparam
.nparam
= argc
- 1;
945 shellparam
.p
= argv
+ 1;
946 shellparam
.optind
= 1;
947 shellparam
.optoff
= -1;
949 evaltree(&func
->n
, flags
& EV_TESTED
);
955 freeparam(&shellparam
);
956 shellparam
= saveparam
;
957 handler
= savehandler
;
959 evalskip
&= ~SKIPFUNC
;
965 * Search for a command. This is called before we fork so that the
966 * location of the command will be available in the parent as well as
967 * the child. The check for "goodname" is an overly conservative
968 * check that the name will not be subject to expansion.
972 prehash(union node
*n
)
974 struct cmdentry entry
;
976 if (n
->type
== NCMD
&& n
->ncmd
.args
)
977 if (goodname(n
->ncmd
.args
->narg
.text
))
978 find_command(n
->ncmd
.args
->narg
.text
, &entry
, 0,
985 * Builtin commands. Builtin commands whose functions are closely
986 * tied to evaluation are implemented here.
994 bltincmd(int argc
, char **argv
)
997 * Preserve exitstatus of a previous possible redirection
1000 return back_exitstatus
;
1005 * Handle break and continue commands. Break, continue, and return are
1006 * all handled by setting the evalskip flag. The evaluation routines
1007 * above all check this flag, and if it is set they start skipping
1008 * commands rather than executing them. The variable skipcount is
1009 * the number of loops to break/continue, or the number of function
1010 * levels to return. (The latter is always 1.) It should probably
1011 * be an error to break out of more loops than exist, but it isn't
1012 * in the standard shell so we don't make it one here.
1016 breakcmd(int argc
, char **argv
)
1018 int n
= argc
> 1 ? number(argv
[1]) : 1;
1025 evalskip
= (**argv
== 'c')? SKIPCONT
: SKIPBREAK
;
1033 * The return command.
1037 returncmd(int argc
, char **argv
)
1040 * If called outside a function, do what ksh does;
1041 * skip the rest of the file.
1043 evalskip
= funcnest
? SKIPFUNC
: SKIPFILE
;
1044 return argv
[1] ? number(argv
[1]) : exitstatus
;
1049 falsecmd(int argc
, char **argv
)
1056 truecmd(int argc
, char **argv
)
1063 execcmd(int argc
, char **argv
)
1066 iflag
= 0; /* exit on error */
1069 shellexec(argv
+ 1, pathval(), 0);
1076 eprintlist(struct output
*out
, struct strlist
*sp
, int sep
)
1081 p
= " %s" + (1 - sep
);
1083 outfmt(out
, p
, sp
->text
);