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
struct strlist *cmdenviron;
81
int exitstatus; /* exit status of last command */
82
int back_exitstatus; /* exit status of backquoted command */
85
#if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
88
void evaltreenr(union node *, int) __attribute__ ((__noreturn__));
89
STATIC void evalloop(union node *, int);
90
STATIC void evalfor(union node *, int);
91
STATIC void evalcase(union node *, int);
92
STATIC void evalsubshell(union node *, int);
93
STATIC void expredir(union node *);
94
STATIC void evalpipe(union node *, int);
96
STATIC void evalcommand(union node *, int, struct backcmd *);
98
STATIC void evalcommand(union node *, int);
100
STATIC int evalbltin(const struct builtincmd *, int, char **);
101
STATIC int evalfun(struct funcnode *, int, char **, int);
102
STATIC void prehash(union node *);
103
STATIC int eprintlist(struct output *, struct strlist *, int);
104
STATIC int bltincmd(int, char **);
107
STATIC const struct builtincmd bltin = {
114
* Called to reset things after an exception.
133
evalcmd(int argc, char **argv)
142
STARTSTACKSTR(concat);
145
concat = stputs(p, concat);
146
if ((p = *ap++) == NULL)
150
STPUTC('\0', concat);
151
p = grabstackstr(concat);
153
evalstring(p, ~SKIPEVAL);
161
* Execute a command or commands contained in a string.
165
evalstring(char *s, int mask)
168
struct stackmark smark;
172
setstackmark(&smark);
175
while ((n = parsecmd(0)) != NEOF) {
177
popstackmark(&smark);
192
* Evaluate a parse tree. The value is left in the global variable
197
evaltree(union node *n, int flags)
200
void (*evalfn)(union node *, int);
204
TRACE(("evaltree(NULL) called\n"));
208
displayhist = 1; /* show history substitutions done with fc */
210
TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
211
getpid(), n, n->type, flags));
215
out1fmt("Node type = %d\n", n->type);
216
#ifndef USE_GLIBC_STDIO
222
evaltree(n->nnot.com, EV_TESTED);
223
status = !exitstatus;
226
expredir(n->nredir.redirect);
227
status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
229
evaltree(n->nredir.n, flags & EV_TESTED);
236
if (eflag && !(flags & EV_TESTED))
238
evalcommand(n, flags, (struct backcmd *)NULL);
241
evalfn = evalcommand;
243
if (eflag && !(flags & EV_TESTED))
256
evalfn = evalsubshell;
261
if (eflag && !(flags & EV_TESTED))
274
#error NAND + 1 != NOR
277
#error NOR + 1 != NSEMI
279
isor = n->type - NAND;
282
(flags | ((isor >> 1) - 1)) & EV_TESTED
284
if (!exitstatus == isor)
296
evaltree(n->nif.test, EV_TESTED);
299
if (exitstatus == 0) {
302
} else if (n->nif.elsepart) {
308
defun(n->narg.text, n->narg.next);
316
if ((checkexit & exitstatus))
317
evalskip |= SKIPEVAL;
318
else if (pendingsigs && dotrap())
321
if (flags & EV_EXIT) {
328
#if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
331
void evaltreenr(union node *, int) __attribute__ ((alias("evaltree")));
335
evalloop(union node *n, int flags)
345
evaltree(n->nbinary.ch1, EV_TESTED);
347
skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
351
if (evalskip == SKIPBREAK && --skipcount <= 0)
356
if (n->type != NWHILE)
360
evaltree(n->nbinary.ch2, flags);
372
evalfor(union node *n, int flags)
374
struct arglist arglist;
377
struct stackmark smark;
379
setstackmark(&smark);
380
arglist.lastp = &arglist.list;
381
for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
382
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
387
*arglist.lastp = NULL;
392
for (sp = arglist.list ; sp ; sp = sp->next) {
393
setvar(n->nfor.var, sp->text, 0);
394
evaltree(n->nfor.body, flags);
396
if (evalskip == SKIPCONT && --skipcount <= 0) {
400
if (evalskip == SKIPBREAK && --skipcount <= 0)
407
popstackmark(&smark);
413
evalcase(union node *n, int flags)
417
struct arglist arglist;
418
struct stackmark smark;
420
setstackmark(&smark);
421
arglist.lastp = &arglist.list;
422
expandarg(n->ncase.expr, &arglist, EXP_TILDE);
424
for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
425
for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
426
if (casematch(patp, arglist.list->text)) {
428
evaltree(cp->nclist.body, flags);
435
popstackmark(&smark);
441
* Kick off a subshell to evaluate a tree.
445
evalsubshell(union node *n, int flags)
448
int backgnd = (n->type == NBACKGND);
451
expredir(n->nredir.redirect);
452
if (!backgnd && flags & EV_EXIT && !trap[0])
456
if (forkshell(jp, n, backgnd) == 0) {
462
redirect(n->nredir.redirect, 0);
463
evaltreenr(n->nredir.n, flags);
468
status = waitforjob(jp);
476
* Compute the names of the files in a redirection list.
480
expredir(union node *n)
484
for (redir = n ; redir ; redir = redir->nfile.next) {
487
switch (redir->type) {
493
expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
494
redir->nfile.expfname = fn.list->text;
498
if (redir->ndup.vname) {
499
expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
500
fixredir(redir, fn.list->text, 1);
510
* Evaluate a pipeline. All the processes in the pipeline are children
511
* of the process creating the pipeline. (This differs from some versions
512
* of the shell, which make the last process in a pipeline the parent
517
evalpipe(union node *n, int flags)
525
TRACE(("evalpipe(0x%lx) called\n", (long)n));
527
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
531
jp = makejob(n, pipelen);
533
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
539
sh_error("Pipe call failed");
542
if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
555
evaltreenr(lp->n, flags);
563
if (n->npipe.backgnd == 0) {
564
exitstatus = waitforjob(jp);
565
TRACE(("evalpipe: job done exit status %d\n", exitstatus));
573
* Execute a command inside back quotes. If it's a builtin command, we
574
* want to save its output in a block obtained from malloc. Otherwise
575
* we fork off a subprocess and get the output of the command via a pipe.
576
* Should be called with interrupts off.
580
evalbackcmd(union node *n, struct backcmd *result)
597
* For now we disable executing builtins in the same
598
* context as the shell, because we are not keeping
599
* enough state to recover from changes that are
600
* supposed only to affect subshells. eg. echo "`cd /`"
602
if (n->type == NCMD) {
603
struct ifsregion saveifs;
604
struct ifsregion *savelastp;
605
struct nodelist *saveargbackq;
608
savelastp = ifslastp;
609
saveargbackq = argbackq;
611
exitstatus = oexitstatus;
612
evalcommand(n, EV_BACKCMD, result);
615
ifslastp = savelastp;
616
argbackq = saveargbackq;
624
sh_error("Pipe call failed");
626
if (forkshell(jp, n, FORK_NOJOB) == 0) {
635
evaltreenr(n, EV_EXIT);
644
TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
645
result->fd, result->buf, result->nleft, result->jp));
649
parse_command_args(char **argv, const char **path)
661
if (c == '-' && !*cp) {
671
/* run 'typecmd' for other options */
674
} while ((c = *cp++));
682
* Execute a simple command.
687
evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
689
evalcommand(union node *cmd, int flags)
692
struct stackmark smark;
694
struct arglist arglist;
695
struct arglist varlist;
702
struct cmdentry cmdentry;
711
/* First expand the arguments. */
712
TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
713
setstackmark(&smark);
716
cmdentry.cmdtype = CMDBUILTIN;
717
cmdentry.u.cmd = &bltin;
718
varlist.lastp = &varlist.list;
719
*varlist.lastp = NULL;
720
arglist.lastp = &arglist.list;
721
*arglist.lastp = NULL;
724
for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) {
725
struct strlist **spp;
728
expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
729
for (sp = *spp; sp; sp = sp->next)
733
argv = nargv = stalloc(sizeof (char *) * (argc + 1));
734
for (sp = arglist.list ; sp ; sp = sp->next) {
735
TRACE(("evalcommand arg: %s\n", sp->text));
741
if (iflag && funcnest == 0 && argc > 0)
745
expredir(cmd->ncmd.redirect);
746
status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH|REDIR_SAVEFD2);
749
for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
750
struct strlist **spp;
754
expandarg(argp, &varlist, EXP_VARTILDE);
757
* Modify the command lookup path, if a PATH= assignment
761
if (varequal(p, path))
765
/* Print the command if xflag is set. */
771
outstr(expandstr(ps4val()), out);
773
sep = eprintlist(out, varlist.list, sep);
774
eprintlist(out, arglist.list, sep);
784
/* Now locate the command. */
787
int cmd_flag = DO_ERR;
792
find_command(argv[0], &cmdentry, cmd_flag, path);
793
if (cmdentry.cmdtype == CMDUNKNOWN) {
801
/* implement bltin and command here */
802
if (cmdentry.cmdtype != CMDBUILTIN)
806
cmdentry.u.cmd->flags &
809
if (cmdentry.u.cmd == EXECCMD)
811
if (cmdentry.u.cmd != COMMANDCMD)
815
nargv = parse_command_args(argv, &path);
818
argc -= nargv - argv;
820
cmd_flag |= DO_NOFUNC;
825
/* We have a redirection error. */
833
/* Execute the command. */
834
switch (cmdentry.cmdtype) {
836
/* Fork off a child process if necessary. */
837
if (!(flags & EV_EXIT) || trap[0]) {
839
jp = makejob(cmd, 1);
840
if (forkshell(jp, cmd, FORK_FG) != 0) {
841
exitstatus = waitforjob(jp);
847
listsetvar(varlist.list, VEXPORT|VSTACK);
848
shellexec(argv, path, cmdentry.u.index);
852
cmdenviron = varlist.list;
854
struct strlist *list = cmdenviron;
856
if (spclbltin > 0 || argc == 0) {
858
if (execcmd && argc > 1)
863
if (evalbltin(cmdentry.u.cmd, argc, argv)) {
881
if (i == EXINT || spclbltin > 0) {
883
longjmp(handler->loc, 1);
890
listsetvar(varlist.list, 0);
891
if (evalfun(cmdentry.u.func, argc, argv, flags))
899
/* dsl: I think this is intended to be used to support
900
* '_' in 'vi' command mode during line editing...
901
* However I implemented that within libedit itself.
903
setvar("_", lastarg, 0);
904
popstackmark(&smark);
908
evalbltin(const struct builtincmd *cmd, int argc, char **argv) {
909
char *volatile savecmdname;
910
struct jmploc *volatile savehandler;
911
struct jmploc jmploc;
914
savecmdname = commandname;
915
if ((i = setjmp(jmploc.loc)))
917
savehandler = handler;
919
commandname = argv[0];
921
optptr = NULL; /* initialize nextopt */
922
exitstatus = (*cmd->builtin)(argc, argv);
925
exitstatus |= outerr(out1);
927
commandname = savecmdname;
929
handler = savehandler;
935
evalfun(struct funcnode *func, int argc, char **argv, int flags)
937
volatile struct shparam saveparam;
938
struct localvar *volatile savelocalvars;
939
struct jmploc *volatile savehandler;
940
struct jmploc jmploc;
943
saveparam = shellparam;
944
savelocalvars = localvars;
945
if ((e = setjmp(jmploc.loc))) {
949
savehandler = handler;
952
shellparam.malloc = 0;
956
shellparam.nparam = argc - 1;
957
shellparam.p = argv + 1;
958
shellparam.optind = 1;
959
shellparam.optoff = -1;
960
evaltree(&func->n, flags & EV_TESTED);
966
localvars = savelocalvars;
967
freeparam(&shellparam);
968
shellparam = saveparam;
969
handler = savehandler;
971
evalskip &= ~SKIPFUNC;
977
* Search for a command. This is called before we fork so that the
978
* location of the command will be available in the parent as well as
979
* the child. The check for "goodname" is an overly conservative
980
* check that the name will not be subject to expansion.
984
prehash(union node *n)
986
struct cmdentry entry;
988
if (n->type == NCMD && n->ncmd.args)
989
if (goodname(n->ncmd.args->narg.text))
990
find_command(n->ncmd.args->narg.text, &entry, 0,
997
* Builtin commands. Builtin commands whose functions are closely
998
* tied to evaluation are implemented here.
1006
bltincmd(int argc, char **argv)
1009
* Preserve exitstatus of a previous possible redirection
1012
return back_exitstatus;
1017
* Handle break and continue commands. Break, continue, and return are
1018
* all handled by setting the evalskip flag. The evaluation routines
1019
* above all check this flag, and if it is set they start skipping
1020
* commands rather than executing them. The variable skipcount is
1021
* the number of loops to break/continue, or the number of function
1022
* levels to return. (The latter is always 1.) It should probably
1023
* be an error to break out of more loops than exist, but it isn't
1024
* in the standard shell so we don't make it one here.
1028
breakcmd(int argc, char **argv)
1030
int n = argc > 1 ? number(argv[1]) : 1;
1033
sh_error(illnum, argv[1]);
1037
evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1045
* The return command.
1049
returncmd(int argc, char **argv)
1052
* If called outside a function, do what ksh does;
1053
* skip the rest of the file.
1055
evalskip = funcnest ? SKIPFUNC : SKIPFILE;
1056
return argv[1] ? number(argv[1]) : exitstatus;
1061
falsecmd(int argc, char **argv)
1068
truecmd(int argc, char **argv)
1075
execcmd(int argc, char **argv)
1078
iflag = 0; /* exit on error */
1081
shellexec(argv + 1, pathval(), 0);
1088
eprintlist(struct output *out, struct strlist *sp, int sep)
1093
p = " %s" + (1 - sep);
1095
outfmt(out, p, sp->text);