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>
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(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);
172 popstackmark(&smark);
173 setstackmark(&smark);
176 popstackmark(&smark);
185 * Evaluate a parse tree. The value is left in the global variable
190 evaltree(union node *n, int flags)
194 struct stackmark smark;
196 setstackmark(&smark);
199 TRACE(("evaltree(NULL) called\n"));
206 displayhist = 1; /* show history substitutions done with fc */
208 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
211 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
214 next = n->nbinary.ch2;
217 evaltree(n->nbinary.ch1, EV_TESTED);
218 if (evalskip || exitstatus != 0) {
221 next = n->nbinary.ch2;
224 evaltree(n->nbinary.ch1, EV_TESTED);
225 if (evalskip || exitstatus == 0)
227 next = n->nbinary.ch2;
233 evalsubshell(n, flags);
234 do_etest = !(flags & EV_TESTED);
237 evalsubshell(n, flags);
240 evaltree(n->nif.test, EV_TESTED);
244 next = n->nif.ifpart;
245 else if (n->nif.elsepart)
246 next = n->nif.elsepart;
253 evalloop(n, flags & ~EV_EXIT);
256 evalfor(n, flags & ~EV_EXIT);
262 next = n->nclist.body;
265 if (n->nclist.body) {
266 evaltree(n->nclist.body, flags & ~EV_EXIT);
270 next = n->nclist.next;
273 defun(n->narg.text, n->narg.next);
277 evaltree(n->nnot.com, EV_TESTED);
280 exitstatus = !exitstatus;
285 do_etest = !(flags & EV_TESTED);
288 evalcommand(n, flags, (struct backcmd *)NULL);
289 do_etest = !(flags & EV_TESTED);
292 out1fmt("Node type = %d\n", n->type);
297 popstackmark(&smark);
298 setstackmark(&smark);
301 popstackmark(&smark);
304 if (eflag && exitstatus != 0 && do_etest)
305 exitshell(exitstatus);
312 evalloop(union node *n, int flags)
320 evaltree(n->nbinary.ch1, EV_TESTED);
322 if (evalskip == SKIPCONT && --skipcount <= 0) {
326 if (evalskip == SKIPBREAK && --skipcount <= 0)
328 if (evalskip == SKIPRETURN)
332 if (n->type == NWHILE) {
339 evaltree(n->nbinary.ch2, flags);
349 evalfor(union node *n, int flags)
351 struct arglist arglist;
356 arglist.lastp = &arglist.list;
357 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
358 oexitstatus = exitstatus;
359 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
361 *arglist.lastp = NULL;
365 for (sp = arglist.list ; sp ; sp = sp->next) {
366 setvar(n->nfor.var, sp->text, 0);
367 evaltree(n->nfor.body, flags);
370 if (evalskip == SKIPCONT && --skipcount <= 0) {
374 if (evalskip == SKIPBREAK && --skipcount <= 0)
385 * Evaluate a case statement, returning the selected tree.
387 * The exit status needs care to get right.
391 evalcase(union node *n)
395 struct arglist arglist;
397 arglist.lastp = &arglist.list;
398 oexitstatus = exitstatus;
399 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
400 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
401 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
402 if (casematch(patp, arglist.list->text)) {
403 while (cp->nclist.next &&
404 cp->type == NCLISTFALLTHRU &&
405 cp->nclist.body == NULL)
406 cp = cp->nclist.next;
407 if (cp->nclist.next &&
408 cp->type == NCLISTFALLTHRU)
410 if (cp->nclist.body == NULL)
412 return (cp->nclist.body);
423 * Kick off a subshell to evaluate a tree.
427 evalsubshell(union node *n, int flags)
430 int backgnd = (n->type == NBACKGND);
432 oexitstatus = exitstatus;
433 expredir(n->nredir.redirect);
434 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
435 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
438 redirect(n->nredir.redirect, 0);
439 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
440 } else if (! backgnd) {
442 exitstatus = waitforjob(jp, (int *)NULL);
450 * Evaluate a redirected compound command.
454 evalredir(union node *n, int flags)
456 struct jmploc jmploc;
457 struct jmploc *savehandler;
458 volatile int in_redirect = 1;
460 oexitstatus = exitstatus;
461 expredir(n->nredir.redirect);
462 savehandler = handler;
463 if (setjmp(jmploc.loc)) {
466 handler = savehandler;
469 if (e == EXERROR || e == EXEXEC) {
475 longjmp(handler->loc, 1);
479 redirect(n->nredir.redirect, REDIR_PUSH);
482 evaltree(n->nredir.n, flags);
485 handler = savehandler;
492 exphere(union node *redir, struct arglist *fn)
494 struct jmploc jmploc;
495 struct jmploc *savehandler;
496 struct localvar *savelocalvars;
497 int need_longjmp = 0;
499 redir->nhere.expdoc = nullstr;
500 savelocalvars = localvars;
503 savehandler = handler;
504 if (setjmp(jmploc.loc))
505 need_longjmp = exception != EXERROR && exception != EXEXEC;
508 expandarg(redir->nhere.doc, fn, 0);
509 redir->nhere.expdoc = fn->list->text;
512 handler = savehandler;
515 localvars = savelocalvars;
517 longjmp(handler->loc, 1);
523 * Compute the names of the files in a redirection list.
527 expredir(union node *n)
531 for (redir = n ; redir ; redir = redir->nfile.next) {
534 switch (redir->type) {
540 expandarg(redir->nfile.fname, &fn, EXP_TILDE);
541 redir->nfile.expfname = fn.list->text;
545 if (redir->ndup.vname) {
546 expandarg(redir->ndup.vname, &fn, EXP_TILDE);
547 fixredir(redir, fn.list->text, 1);
560 * Evaluate a pipeline. All the processes in the pipeline are children
561 * of the process creating the pipeline. (This differs from some versions
562 * of the shell, which make the last process in a pipeline the parent
567 evalpipe(union node *n)
575 TRACE(("evalpipe(%p) called\n", (void *)n));
577 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
580 jp = makejob(n, pipelen);
582 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
589 error("Pipe call failed: %s", strerror(errno));
592 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
599 if (!(prevfd >= 0 && pip[0] == 0))
606 evaltree(lp->n, EV_EXIT);
615 if (n->npipe.backgnd == 0) {
617 exitstatus = waitforjob(jp, (int *)NULL);
618 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
627 is_valid_fast_cmdsubst(union node *n)
630 return (n->type == NCMD);
634 * Execute a command inside back quotes. If it's a builtin command, we
635 * want to save its output in a block obtained from malloc. Otherwise
636 * we fork off a subprocess and get the output of the command via a pipe.
637 * Should be called with interrupts off.
641 evalbackcmd(union node *n, struct backcmd *result)
645 struct stackmark smark;
646 struct jmploc jmploc;
647 struct jmploc *savehandler;
648 struct localvar *savelocalvars;
658 setstackmark(&smark);
659 exitstatus = oexitstatus;
660 if (is_valid_fast_cmdsubst(n)) {
661 savelocalvars = localvars;
664 savehandler = handler;
665 if (setjmp(jmploc.loc)) {
666 if (exception == EXERROR || exception == EXEXEC)
668 else if (exception != 0) {
669 handler = savehandler;
672 localvars = savelocalvars;
673 longjmp(handler->loc, 1);
677 evalcommand(n, EV_BACKCMD, result);
679 handler = savehandler;
682 localvars = savelocalvars;
685 error("Pipe call failed: %s", strerror(errno));
687 if (forkshell(jp, n, FORK_NOJOB) == 0) {
694 evaltree(n, EV_EXIT);
700 popstackmark(&smark);
701 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
702 result->fd, result->buf, result->nleft, result->jp));
706 mustexpandto(const char *argtext, const char *mask)
709 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
713 if (*argtext == CTLESC)
715 else if (BASESYNTAX[(int)*argtext] == CCTL)
717 if (*argtext != *mask)
719 if (*argtext == '\0')
727 isdeclarationcmd(struct narg *arg)
729 int have_command = 0;
733 while (mustexpandto(arg->text, "command")) {
735 arg = &arg->next->narg;
739 * To also allow "command -p" and "command --" as part of
740 * a declaration command, add code here.
741 * We do not do this, as ksh does not do it either and it
742 * is not required by POSIX.
745 return (mustexpandto(arg->text, "export") ||
746 mustexpandto(arg->text, "readonly") ||
747 (mustexpandto(arg->text, "local") &&
748 (have_command || !isfunc("local"))));
752 xtracecommand(struct arglist *varlist, struct arglist *arglist)
758 ps4 = expandstr(ps4val());
759 out2str(ps4 != NULL ? ps4 : ps4val());
760 for (sp = varlist->list ; sp ; sp = sp->next) {
763 p = strchr(sp->text, '=');
766 outbin(sp->text, p - sp->text, out2);
772 for (sp = arglist->list ; sp ; sp = sp->next) {
775 /* Disambiguate command looking like assignment. */
776 if (sp == arglist->list &&
777 strchr(sp->text, '=') != NULL &&
778 strchr(sp->text, '\'') == NULL) {
791 * Check if a builtin can safely be executed in the same process,
792 * even though it should be in a subshell (command substitution).
793 * Note that jobid, jobs, times and trap can show information not
794 * available in a child process; this is deliberate.
795 * The arguments should already have been expanded.
798 safe_builtin(int idx, int argc, char **argv)
800 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
801 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
802 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
803 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
806 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
808 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
810 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
811 argv[1][0] == '+') && argv[1][1] == 'o' &&
812 argv[1][2] == '\0'));
817 * Execute a simple command.
818 * Note: This may or may not return if (flags & EV_EXIT).
822 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
825 struct arglist arglist;
826 struct arglist varlist;
834 struct cmdentry cmdentry;
836 struct jmploc jmploc;
837 struct jmploc *savehandler;
839 struct shparam saveparam;
840 struct localvar *savelocalvars;
841 struct parsefile *savetopfile;
845 int do_clearcmdentry;
846 const char *path = pathval();
848 /* First expand the arguments. */
849 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
850 arglist.lastp = &arglist.list;
851 varlist.lastp = &varlist.list;
854 do_clearcmdentry = 0;
855 oexitstatus = exitstatus;
857 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
858 if (varflag && isassignment(argp->narg.text)) {
859 expandarg(argp, varflag == 1 ? &varlist : &arglist,
862 } else if (varflag == 1)
863 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
864 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
866 *arglist.lastp = NULL;
867 *varlist.lastp = NULL;
868 expredir(cmd->ncmd.redirect);
870 for (sp = arglist.list ; sp ; sp = sp->next)
872 /* Add one slot at the beginning for tryexec(). */
873 argv = stalloc(sizeof (char *) * (argc + 2));
876 for (sp = arglist.list ; sp ; sp = sp->next) {
877 TRACE(("evalcommand arg: %s\n", sp->text));
882 if (iflag && funcnest == 0 && argc > 0)
886 /* Print the command if xflag is set. */
888 xtracecommand(&varlist, &arglist);
890 /* Now locate the command. */
892 /* Variable assignment(s) without command */
893 cmdentry.cmdtype = CMDBUILTIN;
894 cmdentry.u.index = BLTINCMD;
895 cmdentry.special = 0;
897 static const char PATH[] = "PATH=";
898 int cmd_flags = 0, bltinonly = 0;
901 * Modify the command lookup path, if a PATH= assignment
904 for (sp = varlist.list ; sp ; sp = sp->next)
905 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
906 path = sp->text + sizeof(PATH) - 1;
908 * On `PATH=... command`, we need to make
909 * sure that the command isn't using the
910 * non-updated hash table of the outer PATH
911 * setting and we need to make sure that
912 * the hash table isn't filled with items
913 * from the temporary setting.
915 * It would be better to forbit using and
916 * updating the table while this command
917 * runs, by the command finding mechanism
918 * is heavily integrated with hash handling,
919 * so we just delete the hash before and after
920 * the command runs. Partly deleting like
921 * changepatch() does doesn't seem worth the
922 * bookinging effort, since most such runs add
923 * directories in front of the new PATH.
926 do_clearcmdentry = 1;
931 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
932 if (cmdentry.u.index < 0) {
933 cmdentry.u.index = BLTINCMD;
939 find_command(argv[0], &cmdentry, cmd_flags, path);
940 /* implement the bltin and command builtins here */
941 if (cmdentry.cmdtype != CMDBUILTIN)
943 if (cmdentry.u.index == BLTINCMD) {
949 } else if (cmdentry.u.index == COMMANDCMD) {
952 if (!strcmp(argv[1], "-p")) {
955 if (argv[2][0] == '-') {
956 if (strcmp(argv[2], "--"))
966 path = _PATH_STDPATH;
968 do_clearcmdentry = 1;
969 } else if (!strcmp(argv[1], "--")) {
974 } else if (argv[1][0] == '-')
980 cmd_flags |= DO_NOFUNC;
986 * Special builtins lose their special properties when
987 * called via 'command'.
989 if (cmd_flags & DO_NOFUNC)
990 cmdentry.special = 0;
993 /* Fork off a child process if necessary. */
994 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
995 && ((flags & EV_EXIT) == 0 || have_traps()))
996 || ((flags & EV_BACKCMD) != 0
997 && (cmdentry.cmdtype != CMDBUILTIN ||
998 !safe_builtin(cmdentry.u.index, argc, argv)))) {
999 jp = makejob(cmd, 1);
1001 if (flags & EV_BACKCMD) {
1004 error("Pipe call failed: %s", strerror(errno));
1006 if (cmdentry.cmdtype == CMDNORMAL &&
1007 cmd->ncmd.redirect == NULL &&
1008 varlist.list == NULL &&
1009 (mode == FORK_FG || mode == FORK_NOJOB) &&
1010 !disvforkset() && !iflag && !mflag) {
1011 vforkexecshell(jp, argv, environment(), path,
1012 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1015 if (forkshell(jp, cmd, mode) != 0)
1016 goto parent; /* at end of routine */
1017 if (flags & EV_BACKCMD) {
1024 flags &= ~EV_BACKCMD;
1029 /* This is the child process if a fork occurred. */
1030 /* Execute the command. */
1031 if (cmdentry.cmdtype == CMDFUNCTION) {
1033 trputs("Shell function: "); trargs(argv);
1035 saveparam = shellparam;
1036 shellparam.malloc = 0;
1037 shellparam.reset = 1;
1038 shellparam.nparam = argc - 1;
1039 shellparam.p = argv + 1;
1040 shellparam.optnext = NULL;
1042 savelocalvars = localvars;
1044 reffunc(cmdentry.u.func);
1045 savehandler = handler;
1046 if (setjmp(jmploc.loc)) {
1047 freeparam(&shellparam);
1048 shellparam = saveparam;
1050 unreffunc(cmdentry.u.func);
1052 localvars = savelocalvars;
1054 handler = savehandler;
1055 longjmp(handler->loc, 1);
1059 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1061 for (sp = varlist.list ; sp ; sp = sp->next)
1063 exitstatus = oexitstatus;
1064 evaltree(getfuncnode(cmdentry.u.func),
1065 flags & (EV_TESTED | EV_EXIT));
1067 unreffunc(cmdentry.u.func);
1069 localvars = savelocalvars;
1070 freeparam(&shellparam);
1071 shellparam = saveparam;
1072 handler = savehandler;
1076 if (evalskip == SKIPRETURN) {
1081 exitshell(exitstatus);
1082 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1084 trputs("builtin command: "); trargs(argv);
1086 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1087 if (flags == EV_BACKCMD) {
1089 memout.nextc = memout.buf;
1090 memout.bufsize = 64;
1091 mode |= REDIR_BACKQ;
1093 savecmdname = commandname;
1094 savetopfile = getcurrentfile();
1095 cmdenviron = varlist.list;
1097 savehandler = handler;
1098 if (setjmp(jmploc.loc)) {
1101 exitstatus = SIGINT+128;
1102 else if (e != EXEXIT)
1107 redirect(cmd->ncmd.redirect, mode);
1108 outclearerror(out1);
1110 * If there is no command word, redirection errors should
1111 * not be fatal but assignment errors should.
1114 cmdentry.special = 1;
1115 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1118 commandname = argv[0];
1120 nextopt_optptr = NULL; /* initialize nextopt */
1121 builtin_flags = flags;
1122 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1124 if (outiserror(out1)) {
1125 warning("write error on stdout");
1126 if (exitstatus == 0 || exitstatus == 1)
1136 handler = savehandler;
1137 commandname = savecmdname;
1139 exitshell(exitstatus);
1140 if (flags == EV_BACKCMD) {
1141 backcmd->buf = memout.buf;
1142 backcmd->nleft = memout.nextc - memout.buf;
1145 if (cmdentry.u.index != EXECCMD)
1148 if ((e != EXERROR && e != EXEXEC)
1149 || cmdentry.special)
1151 popfilesupto(savetopfile);
1152 if (flags != EV_BACKCMD)
1157 trputs("normal command: "); trargs(argv);
1159 redirect(cmd->ncmd.redirect, 0);
1160 for (sp = varlist.list ; sp ; sp = sp->next)
1161 setvareq(sp->text, VEXPORT|VSTACK);
1162 envp = environment();
1163 shellexec(argv, envp, path, cmdentry.u.index);
1168 parent: /* parent process gets here (if we forked) */
1169 if (mode == FORK_FG) { /* argument to fork */
1171 exitstatus = waitforjob(jp, &realstatus);
1173 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1174 evalskip = SKIPBREAK;
1175 skipcount = loopnest;
1177 } else if (mode == FORK_NOJOB) {
1178 backcmd->fd = pip[0];
1185 setvar("_", lastarg, 0);
1186 if (do_clearcmdentry)
1193 * Search for a command. This is called before we fork so that the
1194 * location of the command will be available in the parent as well as
1195 * the child. The check for "goodname" is an overly conservative
1196 * check that the name will not be subject to expansion.
1200 prehash(union node *n)
1202 struct cmdentry entry;
1204 if (n && n->type == NCMD && n->ncmd.args)
1205 if (goodname(n->ncmd.args->narg.text))
1206 find_command(n->ncmd.args->narg.text, &entry, 0,
1213 * Builtin commands. Builtin commands whose functions are closely
1214 * tied to evaluation are implemented here.
1218 * No command given, a bltin command with no arguments, or a bltin command
1219 * with an invalid name.
1223 bltincmd(int argc, char **argv)
1226 out2fmt_flush("%s: not found\n", argv[1]);
1230 * Preserve exitstatus of a previous possible redirection
1238 * Handle break and continue commands. Break, continue, and return are
1239 * all handled by setting the evalskip flag. The evaluation routines
1240 * above all check this flag, and if it is set they start skipping
1241 * commands rather than executing them. The variable skipcount is
1242 * the number of loops to break/continue, or the number of function
1243 * levels to return. (The latter is always 1.) It should probably
1244 * be an error to break out of more loops than exist, but it isn't
1245 * in the standard shell so we don't make it one here.
1249 breakcmd(int argc, char **argv)
1251 int n = argc > 1 ? number(argv[1]) : 1;
1256 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1263 * The `command' command.
1266 commandcmd(int argc __unused, char **argv __unused)
1272 path = bltinlookup("PATH", 1);
1274 while ((ch = nextopt("pvV")) != '\0') {
1277 path = _PATH_STDPATH;
1280 cmd = TYPECMD_SMALLV;
1289 if (*argptr == NULL || argptr[1] != NULL)
1290 error("wrong number of arguments");
1291 return typecmd_impl(2, argptr - 1, cmd, path);
1293 if (*argptr != NULL)
1294 error("commandcmd bad call");
1297 * Do nothing successfully if no command was specified;
1298 * ksh also does this.
1305 * The return command.
1309 returncmd(int argc, char **argv)
1311 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1313 evalskip = SKIPRETURN;
1320 falsecmd(int argc __unused, char **argv __unused)
1327 truecmd(int argc __unused, char **argv __unused)
1334 execcmd(int argc, char **argv)
1337 * Because we have historically not supported any options,
1338 * only treat "--" specially.
1340 if (argc > 1 && strcmp(argv[1], "--") == 0)
1345 iflag = 0; /* exit on error */
1348 for (sp = cmdenviron; sp ; sp = sp->next)
1349 setvareq(sp->text, VEXPORT|VSTACK);
1350 shellexec(argv + 1, environment(), pathval(), 0);
1358 timescmd(int argc __unused, char **argv __unused)
1361 long shumins, shsmins, chumins, chsmins;
1362 double shusecs, shssecs, chusecs, chssecs;
1364 if (getrusage(RUSAGE_SELF, &ru) < 0)
1366 shumins = ru.ru_utime.tv_sec / 60;
1367 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1368 shsmins = ru.ru_stime.tv_sec / 60;
1369 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1370 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1372 chumins = ru.ru_utime.tv_sec / 60;
1373 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1374 chsmins = ru.ru_stime.tv_sec / 60;
1375 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1376 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1377 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);