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. 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>
39 __FBSDID("$FreeBSD$");
45 #include <sys/resource.h>
46 #include <sys/wait.h> /* For WIFSIGNALED(status) */
73 #include "myhistedit.h"
77 int evalskip
; /* set if we are skipping commands */
78 int skipcount
; /* number of levels to skip */
79 static int loopnest
; /* current loop nesting level */
80 int funcnest
; /* depth of function calls */
81 static int builtin_flags
; /* evalcommand flags for builtins */
85 struct strlist
*cmdenviron
;
86 int exitstatus
; /* exit status of last command */
87 int oexitstatus
; /* saved exit status */
90 static void evalloop(union node
*, int);
91 static void evalfor(union node
*, int);
92 static union node
*evalcase(union node
*);
93 static void evalsubshell(union node
*, int);
94 static void evalredir(union node
*, int);
95 static void exphere(union node
*, struct arglist
*);
96 static void expredir(union node
*);
97 static void evalpipe(union node
*);
98 static int is_valid_fast_cmdsubst(union node
*n
);
99 static void evalcommand(union node
*, int, struct backcmd
*);
100 static void prehash(union node
*);
104 * Called to reset things after an exception.
120 evalcmd(int argc
, char **argv
)
129 STARTSTACKSTR(concat
);
133 if ((p
= *ap
++) == NULL
)
137 STPUTC('\0', concat
);
138 p
= grabstackstr(concat
);
140 evalstring(p
, builtin_flags
);
148 * Execute a command or commands contained in a string.
152 evalstring(const char *s
, int flags
)
155 struct stackmark smark
;
159 flags_exit
= flags
& EV_EXIT
;
162 setstackmark(&smark
);
163 setinputstring(s
, 1);
164 while ((n
= parsecmd(0)) != NEOF
) {
165 if (n
!= NULL
&& !nflag
) {
166 if (flags_exit
&& preadateof())
167 evaltree(n
, flags
| EV_EXIT
);
174 popstackmark(&smark
);
175 setstackmark(&smark
);
178 popstackmark(&smark
);
187 * Evaluate a parse tree. The value is left in the global variable
192 evaltree(union node
*n
, int flags
)
196 struct stackmark smark
;
198 setstackmark(&smark
);
201 TRACE(("evaltree(NULL) called\n"));
208 displayhist
= 1; /* show history substitutions done with fc */
210 TRACE(("evaltree(%p: %d) called\n", (void *)n
, n
->type
));
213 evaltree(n
->nbinary
.ch1
, flags
& ~EV_EXIT
);
216 next
= n
->nbinary
.ch2
;
219 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
220 if (evalskip
|| exitstatus
!= 0) {
223 next
= n
->nbinary
.ch2
;
226 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
227 if (evalskip
|| exitstatus
== 0)
229 next
= n
->nbinary
.ch2
;
235 evalsubshell(n
, flags
);
236 do_etest
= !(flags
& EV_TESTED
);
239 evalsubshell(n
, flags
);
242 evaltree(n
->nif
.test
, EV_TESTED
);
246 next
= n
->nif
.ifpart
;
247 else if (n
->nif
.elsepart
)
248 next
= n
->nif
.elsepart
;
255 evalloop(n
, flags
& ~EV_EXIT
);
258 evalfor(n
, flags
& ~EV_EXIT
);
264 next
= n
->nclist
.body
;
267 if (n
->nclist
.body
) {
268 evaltree(n
->nclist
.body
, flags
& ~EV_EXIT
);
272 next
= n
->nclist
.next
;
275 defun(n
->narg
.text
, n
->narg
.next
);
279 evaltree(n
->nnot
.com
, EV_TESTED
);
282 exitstatus
= !exitstatus
;
287 do_etest
= !(flags
& EV_TESTED
);
290 evalcommand(n
, flags
, (struct backcmd
*)NULL
);
291 do_etest
= !(flags
& EV_TESTED
);
294 out1fmt("Node type = %d\n", n
->type
);
299 popstackmark(&smark
);
300 setstackmark(&smark
);
303 popstackmark(&smark
);
306 if (eflag
&& exitstatus
!= 0 && do_etest
)
307 exitshell(exitstatus
);
314 evalloop(union node
*n
, int flags
)
322 evaltree(n
->nbinary
.ch1
, EV_TESTED
);
324 if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
328 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
330 if (evalskip
== SKIPRETURN
)
334 if (n
->type
== NWHILE
) {
341 evaltree(n
->nbinary
.ch2
, flags
);
351 evalfor(union node
*n
, int flags
)
353 struct arglist arglist
;
358 arglist
.lastp
= &arglist
.list
;
359 for (argp
= n
->nfor
.args
; argp
; argp
= argp
->narg
.next
) {
360 oexitstatus
= exitstatus
;
361 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
363 *arglist
.lastp
= NULL
;
367 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
368 setvar(n
->nfor
.var
, sp
->text
, 0);
369 evaltree(n
->nfor
.body
, flags
);
372 if (evalskip
== SKIPCONT
&& --skipcount
<= 0) {
376 if (evalskip
== SKIPBREAK
&& --skipcount
<= 0)
387 * Evaluate a case statement, returning the selected tree.
389 * The exit status needs care to get right.
393 evalcase(union node
*n
)
397 struct arglist arglist
;
399 arglist
.lastp
= &arglist
.list
;
400 oexitstatus
= exitstatus
;
401 expandarg(n
->ncase
.expr
, &arglist
, EXP_TILDE
);
402 for (cp
= n
->ncase
.cases
; cp
; cp
= cp
->nclist
.next
) {
403 for (patp
= cp
->nclist
.pattern
; patp
; patp
= patp
->narg
.next
) {
404 if (casematch(patp
, arglist
.list
->text
)) {
405 while (cp
->nclist
.next
&&
406 cp
->type
== NCLISTFALLTHRU
&&
407 cp
->nclist
.body
== NULL
)
408 cp
= cp
->nclist
.next
;
409 if (cp
->nclist
.next
&&
410 cp
->type
== NCLISTFALLTHRU
)
412 if (cp
->nclist
.body
== NULL
)
414 return (cp
->nclist
.body
);
425 * Kick off a subshell to evaluate a tree.
429 evalsubshell(union node
*n
, int flags
)
432 int backgnd
= (n
->type
== NBACKGND
);
434 oexitstatus
= exitstatus
;
435 expredir(n
->nredir
.redirect
);
436 if ((!backgnd
&& flags
& EV_EXIT
&& !have_traps()) ||
437 forkshell(jp
= makejob(n
, 1), n
, backgnd
) == 0) {
440 redirect(n
->nredir
.redirect
, 0);
441 evaltree(n
->nredir
.n
, flags
| EV_EXIT
); /* never returns */
442 } else if (!backgnd
) {
444 exitstatus
= waitforjob(jp
, (int *)NULL
);
452 * Evaluate a redirected compound command.
456 evalredir(union node
*n
, int flags
)
458 struct jmploc jmploc
;
459 struct jmploc
*savehandler
;
460 volatile int in_redirect
= 1;
462 oexitstatus
= exitstatus
;
463 expredir(n
->nredir
.redirect
);
464 savehandler
= handler
;
465 if (setjmp(jmploc
.loc
)) {
468 handler
= savehandler
;
471 if (e
== EXERROR
|| e
== EXEXEC
) {
477 longjmp(handler
->loc
, 1);
481 redirect(n
->nredir
.redirect
, REDIR_PUSH
);
484 evaltree(n
->nredir
.n
, flags
);
487 handler
= savehandler
;
494 exphere(union node
*redir
, struct arglist
*fn
)
496 struct jmploc jmploc
;
497 struct jmploc
*savehandler
;
498 struct localvar
*savelocalvars
;
499 int need_longjmp
= 0;
501 redir
->nhere
.expdoc
= "";
502 savelocalvars
= localvars
;
505 savehandler
= handler
;
506 if (setjmp(jmploc
.loc
))
507 need_longjmp
= exception
!= EXERROR
&& exception
!= EXEXEC
;
510 expandarg(redir
->nhere
.doc
, fn
, 0);
511 redir
->nhere
.expdoc
= fn
->list
->text
;
514 handler
= savehandler
;
517 localvars
= savelocalvars
;
519 longjmp(handler
->loc
, 1);
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
);
543 redir
->nfile
.expfname
= fn
.list
->text
;
547 if (redir
->ndup
.vname
) {
548 expandarg(redir
->ndup
.vname
, &fn
, EXP_TILDE
);
549 fixredir(redir
, fn
.list
->text
, 1);
562 * Evaluate a pipeline. All the processes in the pipeline are children
563 * of the process creating the pipeline. (This differs from some versions
564 * of the shell, which make the last process in a pipeline the parent
569 evalpipe(union node
*n
)
577 TRACE(("evalpipe(%p) called\n", (void *)n
));
579 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
)
582 jp
= makejob(n
, pipelen
);
584 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
591 error("Pipe call failed: %s", strerror(errno
));
594 if (forkshell(jp
, lp
->n
, n
->npipe
.backgnd
) == 0) {
601 if (!(prevfd
>= 0 && pip
[0] == 0))
608 evaltree(lp
->n
, EV_EXIT
);
617 if (n
->npipe
.backgnd
== 0) {
619 exitstatus
= waitforjob(jp
, (int *)NULL
);
620 TRACE(("evalpipe: job done exit status %d\n", exitstatus
));
629 is_valid_fast_cmdsubst(union node
*n
)
632 return (n
->type
== NCMD
);
636 * Execute a command inside back quotes. If it's a builtin command, we
637 * want to save its output in a block obtained from malloc. Otherwise
638 * we fork off a subprocess and get the output of the command via a pipe.
639 * Should be called with interrupts off.
643 evalbackcmd(union node
*n
, struct backcmd
*result
)
647 struct stackmark smark
;
648 struct jmploc jmploc
;
649 struct jmploc
*savehandler
;
650 struct localvar
*savelocalvars
;
660 setstackmark(&smark
);
661 exitstatus
= oexitstatus
;
662 if (is_valid_fast_cmdsubst(n
)) {
663 savelocalvars
= localvars
;
666 savehandler
= handler
;
667 if (setjmp(jmploc
.loc
)) {
668 if (exception
== EXERROR
|| exception
== EXEXEC
)
670 else if (exception
!= 0) {
671 handler
= savehandler
;
674 localvars
= savelocalvars
;
675 longjmp(handler
->loc
, 1);
679 evalcommand(n
, EV_BACKCMD
, result
);
681 handler
= savehandler
;
684 localvars
= savelocalvars
;
687 error("Pipe call failed: %s", strerror(errno
));
689 if (forkshell(jp
, n
, FORK_NOJOB
) == 0) {
696 evaltree(n
, EV_EXIT
);
702 popstackmark(&smark
);
703 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
704 result
->fd
, result
->buf
, result
->nleft
, result
->jp
));
708 mustexpandto(const char *argtext
, const char *mask
)
711 if (*argtext
== CTLQUOTEMARK
|| *argtext
== CTLQUOTEEND
) {
715 if (*argtext
== CTLESC
)
717 else if (BASESYNTAX
[(int)*argtext
] == CCTL
)
719 if (*argtext
!= *mask
)
721 if (*argtext
== '\0')
729 isdeclarationcmd(struct narg
*arg
)
731 int have_command
= 0;
735 while (mustexpandto(arg
->text
, "command")) {
737 arg
= &arg
->next
->narg
;
741 * To also allow "command -p" and "command --" as part of
742 * a declaration command, add code here.
743 * We do not do this, as ksh does not do it either and it
744 * is not required by POSIX.
747 return (mustexpandto(arg
->text
, "export") ||
748 mustexpandto(arg
->text
, "readonly") ||
749 (mustexpandto(arg
->text
, "local") &&
750 (have_command
|| !isfunc("local"))));
754 xtracecommand(struct arglist
*varlist
, struct arglist
*arglist
)
760 ps4
= expandstr(ps4val());
761 out2str(ps4
!= NULL
? ps4
: ps4val());
762 for (sp
= varlist
->list
; sp
; sp
= sp
->next
) {
765 p
= strchr(sp
->text
, '=');
768 outbin(sp
->text
, p
- sp
->text
, out2
);
774 for (sp
= arglist
->list
; sp
; sp
= sp
->next
) {
785 * Check if a builtin can safely be executed in the same process,
786 * even though it should be in a subshell (command substitution).
787 * Note that jobid, jobs, times and trap can show information not
788 * available in a child process; this is deliberate.
789 * The arguments should already have been expanded.
792 safe_builtin(int idx
, int argc
, char **argv
)
794 if (idx
== BLTINCMD
|| idx
== COMMANDCMD
|| idx
== ECHOCMD
||
795 idx
== FALSECMD
|| idx
== JOBIDCMD
|| idx
== JOBSCMD
||
796 idx
== KILLCMD
|| idx
== PRINTFCMD
|| idx
== PWDCMD
||
797 idx
== TESTCMD
|| idx
== TIMESCMD
|| idx
== TRUECMD
||
800 if (idx
== EXPORTCMD
|| idx
== TRAPCMD
|| idx
== ULIMITCMD
||
802 return (argc
<= 1 || (argc
== 2 && argv
[1][0] == '-'));
804 return (argc
<= 1 || (argc
== 2 && (argv
[1][0] == '-' ||
805 argv
[1][0] == '+') && argv
[1][1] == 'o' &&
806 argv
[1][2] == '\0'));
811 * Execute a simple command.
812 * Note: This may or may not return if (flags & EV_EXIT).
816 evalcommand(union node
*cmd
, int flags
, struct backcmd
*backcmd
)
819 struct arglist arglist
;
820 struct arglist varlist
;
828 struct cmdentry cmdentry
;
830 struct jmploc jmploc
;
831 struct jmploc
*savehandler
;
833 struct shparam saveparam
;
834 struct localvar
*savelocalvars
;
835 struct parsefile
*savetopfile
;
839 int do_clearcmdentry
;
840 const char *path
= pathval();
842 /* First expand the arguments. */
843 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd
, flags
));
844 arglist
.lastp
= &arglist
.list
;
845 varlist
.lastp
= &varlist
.list
;
848 do_clearcmdentry
= 0;
849 oexitstatus
= exitstatus
;
851 for (argp
= cmd
->ncmd
.args
; argp
; argp
= argp
->narg
.next
) {
852 if (varflag
&& isassignment(argp
->narg
.text
)) {
853 expandarg(argp
, varflag
== 1 ? &varlist
: &arglist
,
856 } else if (varflag
== 1)
857 varflag
= isdeclarationcmd(&argp
->narg
) ? 2 : 0;
858 expandarg(argp
, &arglist
, EXP_FULL
| EXP_TILDE
);
860 *arglist
.lastp
= NULL
;
861 *varlist
.lastp
= NULL
;
862 expredir(cmd
->ncmd
.redirect
);
864 for (sp
= arglist
.list
; sp
; sp
= sp
->next
)
866 /* Add one slot at the beginning for tryexec(). */
867 argv
= stalloc(sizeof (char *) * (argc
+ 2));
870 for (sp
= arglist
.list
; sp
; sp
= sp
->next
) {
871 TRACE(("evalcommand arg: %s\n", sp
->text
));
876 if (iflag
&& funcnest
== 0 && argc
> 0)
880 /* Print the command if xflag is set. */
882 xtracecommand(&varlist
, &arglist
);
884 /* Now locate the command. */
886 /* Variable assignment(s) without command */
887 cmdentry
.cmdtype
= CMDBUILTIN
;
888 cmdentry
.u
.index
= BLTINCMD
;
889 cmdentry
.special
= 0;
891 static const char PATH
[] = "PATH=";
892 int cmd_flags
= 0, bltinonly
= 0;
895 * Modify the command lookup path, if a PATH= assignment
898 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
899 if (strncmp(sp
->text
, PATH
, sizeof(PATH
) - 1) == 0) {
900 path
= sp
->text
+ sizeof(PATH
) - 1;
902 * On `PATH=... command`, we need to make
903 * sure that the command isn't using the
904 * non-updated hash table of the outer PATH
905 * setting and we need to make sure that
906 * the hash table isn't filled with items
907 * from the temporary setting.
909 * It would be better to forbit using and
910 * updating the table while this command
911 * runs, by the command finding mechanism
912 * is heavily integrated with hash handling,
913 * so we just delete the hash before and after
914 * the command runs. Partly deleting like
915 * changepatch() does doesn't seem worth the
916 * bookinging effort, since most such runs add
917 * directories in front of the new PATH.
920 do_clearcmdentry
= 1;
925 cmdentry
.u
.index
= find_builtin(*argv
, &cmdentry
.special
);
926 if (cmdentry
.u
.index
< 0) {
927 cmdentry
.u
.index
= BLTINCMD
;
933 find_command(argv
[0], &cmdentry
, cmd_flags
, path
);
934 /* implement the bltin and command builtins here */
935 if (cmdentry
.cmdtype
!= CMDBUILTIN
)
937 if (cmdentry
.u
.index
== BLTINCMD
) {
943 } else if (cmdentry
.u
.index
== COMMANDCMD
) {
946 if (!strcmp(argv
[1], "-p")) {
949 if (argv
[2][0] == '-') {
950 if (strcmp(argv
[2], "--"))
960 path
= _PATH_STDPATH
;
962 do_clearcmdentry
= 1;
963 } else if (!strcmp(argv
[1], "--")) {
968 } else if (argv
[1][0] == '-')
974 cmd_flags
|= DO_NOFUNC
;
980 * Special builtins lose their special properties when
981 * called via 'command'.
983 if (cmd_flags
& DO_NOFUNC
)
984 cmdentry
.special
= 0;
987 /* Fork off a child process if necessary. */
988 if (((cmdentry
.cmdtype
== CMDNORMAL
|| cmdentry
.cmdtype
== CMDUNKNOWN
)
989 && ((flags
& EV_EXIT
) == 0 || have_traps()))
990 || ((flags
& EV_BACKCMD
) != 0
991 && (cmdentry
.cmdtype
!= CMDBUILTIN
||
992 !safe_builtin(cmdentry
.u
.index
, argc
, argv
)))) {
993 jp
= makejob(cmd
, 1);
995 if (flags
& EV_BACKCMD
) {
998 error("Pipe call failed: %s", strerror(errno
));
1000 if (cmdentry
.cmdtype
== CMDNORMAL
&&
1001 cmd
->ncmd
.redirect
== NULL
&&
1002 varlist
.list
== NULL
&&
1003 (mode
== FORK_FG
|| mode
== FORK_NOJOB
) &&
1004 !disvforkset() && !iflag
&& !mflag
) {
1005 vforkexecshell(jp
, argv
, environment(), path
,
1006 cmdentry
.u
.index
, flags
& EV_BACKCMD
? pip
: NULL
);
1009 if (forkshell(jp
, cmd
, mode
) != 0)
1010 goto parent
; /* at end of routine */
1011 if (flags
& EV_BACKCMD
) {
1018 flags
&= ~EV_BACKCMD
;
1023 /* This is the child process if a fork occurred. */
1024 /* Execute the command. */
1025 if (cmdentry
.cmdtype
== CMDFUNCTION
) {
1027 trputs("Shell function: "); trargs(argv
);
1029 saveparam
= shellparam
;
1030 shellparam
.malloc
= 0;
1031 shellparam
.reset
= 1;
1032 shellparam
.nparam
= argc
- 1;
1033 shellparam
.p
= argv
+ 1;
1034 shellparam
.optp
= NULL
;
1035 shellparam
.optnext
= NULL
;
1037 savelocalvars
= localvars
;
1039 reffunc(cmdentry
.u
.func
);
1040 savehandler
= handler
;
1041 if (setjmp(jmploc
.loc
)) {
1042 freeparam(&shellparam
);
1043 shellparam
= saveparam
;
1045 unreffunc(cmdentry
.u
.func
);
1047 localvars
= savelocalvars
;
1049 handler
= savehandler
;
1050 longjmp(handler
->loc
, 1);
1054 redirect(cmd
->ncmd
.redirect
, REDIR_PUSH
);
1056 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
1058 exitstatus
= oexitstatus
;
1059 evaltree(getfuncnode(cmdentry
.u
.func
),
1060 flags
& (EV_TESTED
| EV_EXIT
));
1062 unreffunc(cmdentry
.u
.func
);
1064 localvars
= savelocalvars
;
1065 freeparam(&shellparam
);
1066 shellparam
= saveparam
;
1067 handler
= savehandler
;
1071 if (evalskip
== SKIPRETURN
) {
1076 exitshell(exitstatus
);
1077 } else if (cmdentry
.cmdtype
== CMDBUILTIN
) {
1079 trputs("builtin command: "); trargs(argv
);
1081 mode
= (cmdentry
.u
.index
== EXECCMD
)? 0 : REDIR_PUSH
;
1082 if (flags
== EV_BACKCMD
) {
1084 memout
.nextc
= memout
.buf
;
1085 memout
.bufsize
= 64;
1086 mode
|= REDIR_BACKQ
;
1088 savecmdname
= commandname
;
1089 savetopfile
= getcurrentfile();
1090 cmdenviron
= varlist
.list
;
1092 savehandler
= handler
;
1093 if (setjmp(jmploc
.loc
)) {
1096 exitstatus
= SIGINT
+128;
1097 else if (e
!= EXEXIT
)
1102 redirect(cmd
->ncmd
.redirect
, mode
);
1103 outclearerror(out1
);
1105 * If there is no command word, redirection errors should
1106 * not be fatal but assignment errors should.
1109 cmdentry
.special
= 1;
1110 listsetvar(cmdenviron
, cmdentry
.special
? 0 : VNOSET
);
1113 commandname
= argv
[0];
1115 nextopt_optptr
= NULL
; /* initialize nextopt */
1116 builtin_flags
= flags
;
1117 exitstatus
= (*builtinfunc
[cmdentry
.u
.index
])(argc
, argv
);
1119 if (outiserror(out1
)) {
1120 warning("write error on stdout");
1121 if (exitstatus
== 0 || exitstatus
== 1)
1131 handler
= savehandler
;
1132 commandname
= savecmdname
;
1134 exitshell(exitstatus
);
1135 if (flags
== EV_BACKCMD
) {
1136 backcmd
->buf
= memout
.buf
;
1137 backcmd
->nleft
= memout
.nextc
- memout
.buf
;
1140 if (cmdentry
.u
.index
!= EXECCMD
)
1143 if ((e
!= EXERROR
&& e
!= EXEXEC
)
1144 || cmdentry
.special
)
1146 popfilesupto(savetopfile
);
1147 if (flags
!= EV_BACKCMD
)
1152 trputs("normal command: "); trargs(argv
);
1154 redirect(cmd
->ncmd
.redirect
, 0);
1155 for (sp
= varlist
.list
; sp
; sp
= sp
->next
)
1156 setvareq(sp
->text
, VEXPORT
|VSTACK
);
1157 envp
= environment();
1158 shellexec(argv
, envp
, path
, cmdentry
.u
.index
);
1163 parent
: /* parent process gets here (if we forked) */
1164 if (mode
== FORK_FG
) { /* argument to fork */
1166 exitstatus
= waitforjob(jp
, &realstatus
);
1168 if (iflag
&& loopnest
> 0 && WIFSIGNALED(realstatus
)) {
1169 evalskip
= SKIPBREAK
;
1170 skipcount
= loopnest
;
1172 } else if (mode
== FORK_NOJOB
) {
1173 backcmd
->fd
= pip
[0];
1180 setvar("_", lastarg
, 0);
1181 if (do_clearcmdentry
)
1188 * Search for a command. This is called before we fork so that the
1189 * location of the command will be available in the parent as well as
1190 * the child. The check for "goodname" is an overly conservative
1191 * check that the name will not be subject to expansion.
1195 prehash(union node
*n
)
1197 struct cmdentry entry
;
1199 if (n
&& n
->type
== NCMD
&& n
->ncmd
.args
)
1200 if (goodname(n
->ncmd
.args
->narg
.text
))
1201 find_command(n
->ncmd
.args
->narg
.text
, &entry
, 0,
1208 * Builtin commands. Builtin commands whose functions are closely
1209 * tied to evaluation are implemented here.
1213 * No command given, a bltin command with no arguments, or a bltin command
1214 * with an invalid name.
1218 bltincmd(int argc
, char **argv
)
1221 out2fmt_flush("%s: not found\n", argv
[1]);
1225 * Preserve exitstatus of a previous possible redirection
1233 * Handle break and continue commands. Break, continue, and return are
1234 * all handled by setting the evalskip flag. The evaluation routines
1235 * above all check this flag, and if it is set they start skipping
1236 * commands rather than executing them. The variable skipcount is
1237 * the number of loops to break/continue, or the number of function
1238 * levels to return. (The latter is always 1.) It should probably
1239 * be an error to break out of more loops than exist, but it isn't
1240 * in the standard shell so we don't make it one here.
1244 breakcmd(int argc
, char **argv
)
1250 /* Allow arbitrarily large numbers. */
1251 n
= strtol(argv
[1], &end
, 10);
1252 if (!is_digit(argv
[1][0]) || *end
!= '\0')
1253 error("Illegal number: %s", argv
[1]);
1259 evalskip
= (**argv
== 'c')? SKIPCONT
: SKIPBREAK
;
1266 * The `command' command.
1269 commandcmd(int argc __unused
, char **argv __unused
)
1275 path
= bltinlookup("PATH", 1);
1277 while ((ch
= nextopt("pvV")) != '\0') {
1280 path
= _PATH_STDPATH
;
1283 cmd
= TYPECMD_SMALLV
;
1292 if (*argptr
== NULL
|| argptr
[1] != NULL
)
1293 error("wrong number of arguments");
1294 return typecmd_impl(2, argptr
- 1, cmd
, path
);
1296 if (*argptr
!= NULL
)
1297 error("commandcmd bad call");
1300 * Do nothing successfully if no command was specified;
1301 * ksh also does this.
1308 * The return command.
1312 returncmd(int argc
, char **argv
)
1314 int ret
= argc
> 1 ? number(argv
[1]) : oexitstatus
;
1316 evalskip
= SKIPRETURN
;
1323 falsecmd(int argc __unused
, char **argv __unused
)
1330 truecmd(int argc __unused
, char **argv __unused
)
1337 execcmd(int argc
, char **argv
)
1340 * Because we have historically not supported any options,
1341 * only treat "--" specially.
1343 if (argc
> 1 && strcmp(argv
[1], "--") == 0)
1348 iflag
= 0; /* exit on error */
1351 for (sp
= cmdenviron
; sp
; sp
= sp
->next
)
1352 setvareq(sp
->text
, VEXPORT
|VSTACK
);
1353 shellexec(argv
+ 1, environment(), pathval(), 0);
1361 timescmd(int argc __unused
, char **argv __unused
)
1364 long shumins
, shsmins
, chumins
, chsmins
;
1365 double shusecs
, shssecs
, chusecs
, chssecs
;
1367 if (getrusage(RUSAGE_SELF
, &ru
) < 0)
1369 shumins
= ru
.ru_utime
.tv_sec
/ 60;
1370 shusecs
= ru
.ru_utime
.tv_sec
% 60 + ru
.ru_utime
.tv_usec
/ 1000000.;
1371 shsmins
= ru
.ru_stime
.tv_sec
/ 60;
1372 shssecs
= ru
.ru_stime
.tv_sec
% 60 + ru
.ru_stime
.tv_usec
/ 1000000.;
1373 if (getrusage(RUSAGE_CHILDREN
, &ru
) < 0)
1375 chumins
= ru
.ru_utime
.tv_sec
/ 60;
1376 chusecs
= ru
.ru_utime
.tv_sec
% 60 + ru
.ru_utime
.tv_usec
/ 1000000.;
1377 chsmins
= ru
.ru_stime
.tv_sec
/ 60;
1378 chssecs
= ru
.ru_stime
.tv_sec
% 60 + ru
.ru_stime
.tv_usec
/ 1000000.;
1379 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins
,
1380 shusecs
, shsmins
, shssecs
, chumins
, chusecs
, chsmins
, chssecs
);