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 MKINIT 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.
124 evalcmd(int argc, char **argv)
133 STARTSTACKSTR(concat);
137 if ((p = *ap++) == NULL)
141 STPUTC('\0', concat);
142 p = grabstackstr(concat);
144 evalstring(p, builtin_flags);
152 * Execute a command or commands contained in a string.
156 evalstring(char *s, int flags)
159 struct stackmark smark;
163 flags_exit = flags & EV_EXIT;
166 setstackmark(&smark);
167 setinputstring(s, 1);
168 while ((n = parsecmd(0)) != NEOF) {
169 if (n != NULL && !nflag) {
170 if (flags_exit && preadateof())
171 evaltree(n, flags | EV_EXIT);
176 popstackmark(&smark);
177 setstackmark(&smark);
180 popstackmark(&smark);
189 * Evaluate a parse tree. The value is left in the global variable
194 evaltree(union node *n, int flags)
198 struct stackmark smark;
200 setstackmark(&smark);
203 TRACE(("evaltree(NULL) called\n"));
210 displayhist = 1; /* show history substitutions done with fc */
212 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
215 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
218 next = n->nbinary.ch2;
221 evaltree(n->nbinary.ch1, EV_TESTED);
222 if (evalskip || exitstatus != 0) {
225 next = n->nbinary.ch2;
228 evaltree(n->nbinary.ch1, EV_TESTED);
229 if (evalskip || exitstatus == 0)
231 next = n->nbinary.ch2;
237 evalsubshell(n, flags);
238 do_etest = !(flags & EV_TESTED);
241 evalsubshell(n, flags);
244 evaltree(n->nif.test, EV_TESTED);
248 next = n->nif.ifpart;
249 else if (n->nif.elsepart)
250 next = n->nif.elsepart;
257 evalloop(n, flags & ~EV_EXIT);
260 evalfor(n, flags & ~EV_EXIT);
266 next = n->nclist.body;
269 if (n->nclist.body) {
270 evaltree(n->nclist.body, flags & ~EV_EXIT);
274 next = n->nclist.next;
277 defun(n->narg.text, n->narg.next);
281 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)
321 evaltree(n->nbinary.ch1, EV_TESTED);
323 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
327 if (evalskip == SKIPBREAK && --skipcount <= 0)
329 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
333 if (n->type == NWHILE) {
340 evaltree(n->nbinary.ch2, flags);
352 evalfor(union node *n, int flags)
354 struct arglist arglist;
359 arglist.lastp = &arglist.list;
360 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
361 oexitstatus = exitstatus;
362 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
364 *arglist.lastp = NULL;
368 for (sp = arglist.list ; sp ; sp = sp->next) {
369 setvar(n->nfor.var, sp->text, 0);
370 evaltree(n->nfor.body, flags);
373 if (evalskip == SKIPCONT && --skipcount <= 0) {
377 if (evalskip == SKIPBREAK && --skipcount <= 0)
388 * Evaluate a case statement, returning the selected tree.
390 * The exit status needs care to get right.
394 evalcase(union node *n)
398 struct arglist arglist;
400 arglist.lastp = &arglist.list;
401 oexitstatus = exitstatus;
402 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
403 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
404 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
405 if (casematch(patp, arglist.list->text)) {
406 while (cp->nclist.next &&
407 cp->type == NCLISTFALLTHRU &&
408 cp->nclist.body == NULL)
409 cp = cp->nclist.next;
410 if (cp->nclist.next &&
411 cp->type == NCLISTFALLTHRU)
413 if (cp->nclist.body == NULL)
415 return (cp->nclist.body);
426 * Kick off a subshell to evaluate a tree.
430 evalsubshell(union node *n, int flags)
433 int backgnd = (n->type == NBACKGND);
435 oexitstatus = exitstatus;
436 expredir(n->nredir.redirect);
437 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
438 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
441 redirect(n->nredir.redirect, 0);
442 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
443 } else if (! backgnd) {
445 exitstatus = waitforjob(jp, (int *)NULL);
453 * Evaluate a redirected compound command.
457 evalredir(union node *n, int flags)
459 struct jmploc jmploc;
460 struct jmploc *savehandler;
461 volatile int in_redirect = 1;
463 oexitstatus = exitstatus;
464 expredir(n->nredir.redirect);
465 savehandler = handler;
466 if (setjmp(jmploc.loc)) {
469 handler = savehandler;
472 if (e == EXERROR || e == EXEXEC) {
478 longjmp(handler->loc, 1);
482 redirect(n->nredir.redirect, REDIR_PUSH);
485 evaltree(n->nredir.n, flags);
488 handler = savehandler;
495 exphere(union node *redir, struct arglist *fn)
497 struct jmploc jmploc;
498 struct jmploc *savehandler;
499 struct localvar *savelocalvars;
500 int need_longjmp = 0;
502 redir->nhere.expdoc = nullstr;
503 savelocalvars = localvars;
506 savehandler = handler;
507 if (setjmp(jmploc.loc))
508 need_longjmp = exception != EXERROR && exception != EXEXEC;
511 expandarg(redir->nhere.doc, fn, 0);
512 redir->nhere.expdoc = fn->list->text;
515 handler = savehandler;
518 localvars = savelocalvars;
520 longjmp(handler->loc, 1);
526 * Compute the names of the files in a redirection list.
530 expredir(union node *n)
534 for (redir = n ; redir ; redir = redir->nfile.next) {
537 switch (redir->type) {
543 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
544 redir->nfile.expfname = fn.list->text;
548 if (redir->ndup.vname) {
549 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
550 fixredir(redir, fn.list->text, 1);
563 * Evaluate a pipeline. All the processes in the pipeline are children
564 * of the process creating the pipeline. (This differs from some versions
565 * of the shell, which make the last process in a pipeline the parent
570 evalpipe(union node *n)
578 TRACE(("evalpipe(%p) called\n", (void *)n));
580 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
583 jp = makejob(n, pipelen);
585 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;
652 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);
703 popstackmark(&smark);
704 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
705 result->fd, result->buf, result->nleft, result->jp));
709 mustexpandto(const char *argtext, const char *mask)
712 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
716 if (*argtext == CTLESC)
718 else if (BASESYNTAX[(int)*argtext] == CCTL)
720 if (*argtext != *mask)
722 if (*argtext == '\0')
730 isdeclarationcmd(struct narg *arg)
732 int have_command = 0;
736 while (mustexpandto(arg->text, "command")) {
738 arg = &arg->next->narg;
742 * To also allow "command -p" and "command --" as part of
743 * a declaration command, add code here.
744 * We do not do this, as ksh does not do it either and it
745 * is not required by POSIX.
748 return (mustexpandto(arg->text, "export") ||
749 mustexpandto(arg->text, "readonly") ||
750 (mustexpandto(arg->text, "local") &&
751 (have_command || !isfunc("local"))));
755 * Check if a builtin can safely be executed in the same process,
756 * even though it should be in a subshell (command substitution).
757 * Note that jobid, jobs, times and trap can show information not
758 * available in a child process; this is deliberate.
759 * The arguments should already have been expanded.
762 safe_builtin(int idx, int argc, char **argv)
764 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
765 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
766 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
767 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
770 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
772 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
774 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
775 argv[1][0] == '+') && argv[1][1] == 'o' &&
776 argv[1][2] == '\0'));
781 * Execute a simple command.
782 * Note: This may or may not return if (flags & EV_EXIT).
786 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
789 struct arglist arglist;
790 struct arglist varlist;
798 struct cmdentry cmdentry;
800 struct jmploc jmploc;
801 struct jmploc *savehandler;
803 struct shparam saveparam;
804 struct localvar *savelocalvars;
805 struct parsefile *savetopfile;
809 int do_clearcmdentry;
810 const char *path = pathval();
812 /* First expand the arguments. */
813 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
814 arglist.lastp = &arglist.list;
815 varlist.lastp = &varlist.list;
818 do_clearcmdentry = 0;
819 oexitstatus = exitstatus;
821 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
822 if (varflag && isassignment(argp->narg.text)) {
823 expandarg(argp, varflag == 1 ? &varlist : &arglist,
826 } else if (varflag == 1)
827 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
828 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
830 *arglist.lastp = NULL;
831 *varlist.lastp = NULL;
832 expredir(cmd->ncmd.redirect);
834 for (sp = arglist.list ; sp ; sp = sp->next)
836 /* Add one slot at the beginning for tryexec(). */
837 argv = stalloc(sizeof (char *) * (argc + 2));
840 for (sp = arglist.list ; sp ; sp = sp->next) {
841 TRACE(("evalcommand arg: %s\n", sp->text));
846 if (iflag && funcnest == 0 && argc > 0)
850 /* Print the command if xflag is set. */
854 ps4 = expandstr(ps4val());
855 out2str(ps4 != NULL ? ps4 : ps4val());
856 for (sp = varlist.list ; sp ; sp = sp->next) {
859 p = strchr(sp->text, '=');
862 outbin(sp->text, p - sp->text, out2);
868 for (sp = arglist.list ; sp ; sp = sp->next) {
871 /* Disambiguate command looking like assignment. */
872 if (sp == arglist.list &&
873 strchr(sp->text, '=') != NULL &&
874 strchr(sp->text, '\'') == NULL) {
886 /* Now locate the command. */
888 /* Variable assignment(s) without command */
889 cmdentry.cmdtype = CMDBUILTIN;
890 cmdentry.u.index = BLTINCMD;
891 cmdentry.special = 0;
893 static const char PATH[] = "PATH=";
894 int cmd_flags = 0, bltinonly = 0;
897 * Modify the command lookup path, if a PATH= assignment
900 for (sp = varlist.list ; sp ; sp = sp->next)
901 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
902 path = sp->text + sizeof(PATH) - 1;
904 * On `PATH=... command`, we need to make
905 * sure that the command isn't using the
906 * non-updated hash table of the outer PATH
907 * setting and we need to make sure that
908 * the hash table isn't filled with items
909 * from the temporary setting.
911 * It would be better to forbit using and
912 * updating the table while this command
913 * runs, by the command finding mechanism
914 * is heavily integrated with hash handling,
915 * so we just delete the hash before and after
916 * the command runs. Partly deleting like
917 * changepatch() does doesn't seem worth the
918 * bookinging effort, since most such runs add
919 * directories in front of the new PATH.
922 do_clearcmdentry = 1;
927 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
928 if (cmdentry.u.index < 0) {
929 cmdentry.u.index = BLTINCMD;
935 find_command(argv[0], &cmdentry, cmd_flags, path);
936 /* implement the bltin and command builtins here */
937 if (cmdentry.cmdtype != CMDBUILTIN)
939 if (cmdentry.u.index == BLTINCMD) {
945 } else if (cmdentry.u.index == COMMANDCMD) {
948 if (!strcmp(argv[1], "-p")) {
951 if (argv[2][0] == '-') {
952 if (strcmp(argv[2], "--"))
962 path = _PATH_STDPATH;
964 do_clearcmdentry = 1;
965 } else if (!strcmp(argv[1], "--")) {
970 } else if (argv[1][0] == '-')
976 cmd_flags |= DO_NOFUNC;
982 * Special builtins lose their special properties when
983 * called via 'command'.
985 if (cmd_flags & DO_NOFUNC)
986 cmdentry.special = 0;
989 /* Fork off a child process if necessary. */
990 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
991 && ((flags & EV_EXIT) == 0 || have_traps()))
992 || ((flags & EV_BACKCMD) != 0
993 && (cmdentry.cmdtype != CMDBUILTIN ||
994 !safe_builtin(cmdentry.u.index, argc, argv)))) {
995 jp = makejob(cmd, 1);
997 if (flags & EV_BACKCMD) {
1000 error("Pipe call failed: %s", strerror(errno));
1002 if (cmdentry.cmdtype == CMDNORMAL &&
1003 cmd->ncmd.redirect == NULL &&
1004 varlist.list == NULL &&
1005 (mode == FORK_FG || mode == FORK_NOJOB) &&
1006 !disvforkset() && !iflag && !mflag) {
1007 vforkexecshell(jp, argv, environment(), path,
1008 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1011 if (forkshell(jp, cmd, mode) != 0)
1012 goto parent; /* at end of routine */
1013 if (flags & EV_BACKCMD) {
1020 flags &= ~EV_BACKCMD;
1025 /* This is the child process if a fork occurred. */
1026 /* Execute the command. */
1027 if (cmdentry.cmdtype == CMDFUNCTION) {
1029 trputs("Shell function: "); trargs(argv);
1031 saveparam = shellparam;
1032 shellparam.malloc = 0;
1033 shellparam.reset = 1;
1034 shellparam.nparam = argc - 1;
1035 shellparam.p = argv + 1;
1036 shellparam.optnext = NULL;
1038 savelocalvars = localvars;
1040 reffunc(cmdentry.u.func);
1041 savehandler = handler;
1042 if (setjmp(jmploc.loc)) {
1043 freeparam(&shellparam);
1044 shellparam = saveparam;
1046 unreffunc(cmdentry.u.func);
1048 localvars = savelocalvars;
1050 handler = savehandler;
1051 longjmp(handler->loc, 1);
1055 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1057 for (sp = varlist.list ; sp ; sp = sp->next)
1059 exitstatus = oexitstatus;
1060 evaltree(getfuncnode(cmdentry.u.func),
1061 flags & (EV_TESTED | EV_EXIT));
1063 unreffunc(cmdentry.u.func);
1065 localvars = savelocalvars;
1066 freeparam(&shellparam);
1067 shellparam = saveparam;
1068 handler = savehandler;
1072 if (evalskip == SKIPFUNC) {
1077 exitshell(exitstatus);
1078 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1080 trputs("builtin command: "); trargs(argv);
1082 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1083 if (flags == EV_BACKCMD) {
1085 memout.nextc = memout.buf;
1086 memout.bufsize = 64;
1087 mode |= REDIR_BACKQ;
1089 savecmdname = commandname;
1090 savetopfile = getcurrentfile();
1091 cmdenviron = varlist.list;
1093 savehandler = handler;
1094 if (setjmp(jmploc.loc)) {
1097 exitstatus = SIGINT+128;
1098 else if (e != EXEXIT)
1103 redirect(cmd->ncmd.redirect, mode);
1104 outclearerror(out1);
1106 * If there is no command word, redirection errors should
1107 * not be fatal but assignment errors should.
1110 cmdentry.special = 1;
1111 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1114 commandname = argv[0];
1116 nextopt_optptr = NULL; /* initialize nextopt */
1117 builtin_flags = flags;
1118 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1120 if (outiserror(out1)) {
1121 warning("write error on stdout");
1122 if (exitstatus == 0 || exitstatus == 1)
1132 handler = savehandler;
1133 commandname = savecmdname;
1135 exitshell(exitstatus);
1136 if (flags == EV_BACKCMD) {
1137 backcmd->buf = memout.buf;
1138 backcmd->nleft = memout.nextc - memout.buf;
1141 if (cmdentry.u.index != EXECCMD)
1144 if ((e != EXERROR && e != EXEXEC)
1145 || cmdentry.special)
1147 popfilesupto(savetopfile);
1148 if (flags != EV_BACKCMD)
1153 trputs("normal command: "); trargs(argv);
1155 redirect(cmd->ncmd.redirect, 0);
1156 for (sp = varlist.list ; sp ; sp = sp->next)
1157 setvareq(sp->text, VEXPORT|VSTACK);
1158 envp = environment();
1159 shellexec(argv, envp, path, cmdentry.u.index);
1164 parent: /* parent process gets here (if we forked) */
1165 if (mode == FORK_FG) { /* argument to fork */
1167 exitstatus = waitforjob(jp, &realstatus);
1169 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1170 evalskip = SKIPBREAK;
1171 skipcount = loopnest;
1173 } else if (mode == FORK_NOJOB) {
1174 backcmd->fd = pip[0];
1181 setvar("_", lastarg, 0);
1182 if (do_clearcmdentry)
1189 * Search for a command. This is called before we fork so that the
1190 * location of the command will be available in the parent as well as
1191 * the child. The check for "goodname" is an overly conservative
1192 * check that the name will not be subject to expansion.
1196 prehash(union node *n)
1198 struct cmdentry entry;
1200 if (n && n->type == NCMD && n->ncmd.args)
1201 if (goodname(n->ncmd.args->narg.text))
1202 find_command(n->ncmd.args->narg.text, &entry, 0,
1209 * Builtin commands. Builtin commands whose functions are closely
1210 * tied to evaluation are implemented here.
1214 * No command given, a bltin command with no arguments, or a bltin command
1215 * with an invalid name.
1219 bltincmd(int argc, char **argv)
1222 out2fmt_flush("%s: not found\n", argv[1]);
1226 * Preserve exitstatus of a previous possible redirection
1234 * Handle break and continue commands. Break, continue, and return are
1235 * all handled by setting the evalskip flag. The evaluation routines
1236 * above all check this flag, and if it is set they start skipping
1237 * commands rather than executing them. The variable skipcount is
1238 * the number of loops to break/continue, or the number of function
1239 * levels to return. (The latter is always 1.) It should probably
1240 * be an error to break out of more loops than exist, but it isn't
1241 * in the standard shell so we don't make it one here.
1245 breakcmd(int argc, char **argv)
1247 int n = argc > 1 ? number(argv[1]) : 1;
1252 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1259 * The `command' command.
1262 commandcmd(int argc __unused, char **argv __unused)
1268 path = bltinlookup("PATH", 1);
1270 while ((ch = nextopt("pvV")) != '\0') {
1273 path = _PATH_STDPATH;
1276 cmd = TYPECMD_SMALLV;
1285 if (*argptr == NULL || argptr[1] != NULL)
1286 error("wrong number of arguments");
1287 return typecmd_impl(2, argptr - 1, cmd, path);
1289 if (*argptr != NULL)
1290 error("commandcmd bad call");
1293 * Do nothing successfully if no command was specified;
1294 * ksh also does this.
1301 * The return command.
1305 returncmd(int argc, char **argv)
1307 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1310 evalskip = SKIPFUNC;
1313 /* skip the rest of the file */
1314 evalskip = SKIPFILE;
1322 falsecmd(int argc __unused, char **argv __unused)
1329 truecmd(int argc __unused, char **argv __unused)
1336 execcmd(int argc, char **argv)
1339 * Because we have historically not supported any options,
1340 * only treat "--" specially.
1342 if (argc > 1 && strcmp(argv[1], "--") == 0)
1347 iflag = 0; /* exit on error */
1350 for (sp = cmdenviron; sp ; sp = sp->next)
1351 setvareq(sp->text, VEXPORT|VSTACK);
1352 shellexec(argv + 1, environment(), pathval(), 0);
1360 timescmd(int argc __unused, char **argv __unused)
1363 long shumins, shsmins, chumins, chsmins;
1364 double shusecs, shssecs, chusecs, chssecs;
1366 if (getrusage(RUSAGE_SELF, &ru) < 0)
1368 shumins = ru.ru_utime.tv_sec / 60;
1369 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1370 shsmins = ru.ru_stime.tv_sec / 60;
1371 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1372 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1374 chumins = ru.ru_utime.tv_sec / 60;
1375 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1376 chsmins = ru.ru_stime.tv_sec / 60;
1377 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1378 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1379 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);