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)
319 evaltree(n->nbinary.ch1, EV_TESTED);
321 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
325 if (evalskip == SKIPBREAK && --skipcount <= 0)
327 if (evalskip == SKIPRETURN)
331 if (n->type == NWHILE) {
338 evaltree(n->nbinary.ch2, flags);
350 evalfor(union node *n, int flags)
352 struct arglist arglist;
357 arglist.lastp = &arglist.list;
358 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
359 oexitstatus = exitstatus;
360 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
362 *arglist.lastp = NULL;
366 for (sp = arglist.list ; sp ; sp = sp->next) {
367 setvar(n->nfor.var, sp->text, 0);
368 evaltree(n->nfor.body, flags);
371 if (evalskip == SKIPCONT && --skipcount <= 0) {
375 if (evalskip == SKIPBREAK && --skipcount <= 0)
386 * Evaluate a case statement, returning the selected tree.
388 * The exit status needs care to get right.
392 evalcase(union node *n)
396 struct arglist arglist;
398 arglist.lastp = &arglist.list;
399 oexitstatus = exitstatus;
400 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
401 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
402 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
403 if (casematch(patp, arglist.list->text)) {
404 while (cp->nclist.next &&
405 cp->type == NCLISTFALLTHRU &&
406 cp->nclist.body == NULL)
407 cp = cp->nclist.next;
408 if (cp->nclist.next &&
409 cp->type == NCLISTFALLTHRU)
411 if (cp->nclist.body == NULL)
413 return (cp->nclist.body);
424 * Kick off a subshell to evaluate a tree.
428 evalsubshell(union node *n, int flags)
431 int backgnd = (n->type == NBACKGND);
433 oexitstatus = exitstatus;
434 expredir(n->nredir.redirect);
435 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
436 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
439 redirect(n->nredir.redirect, 0);
440 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
441 } else if (! backgnd) {
443 exitstatus = waitforjob(jp, (int *)NULL);
451 * Evaluate a redirected compound command.
455 evalredir(union node *n, int flags)
457 struct jmploc jmploc;
458 struct jmploc *savehandler;
459 volatile int in_redirect = 1;
461 oexitstatus = exitstatus;
462 expredir(n->nredir.redirect);
463 savehandler = handler;
464 if (setjmp(jmploc.loc)) {
467 handler = savehandler;
470 if (e == EXERROR || e == EXEXEC) {
476 longjmp(handler->loc, 1);
480 redirect(n->nredir.redirect, REDIR_PUSH);
483 evaltree(n->nredir.n, flags);
486 handler = savehandler;
493 exphere(union node *redir, struct arglist *fn)
495 struct jmploc jmploc;
496 struct jmploc *savehandler;
497 struct localvar *savelocalvars;
498 int need_longjmp = 0;
500 redir->nhere.expdoc = nullstr;
501 savelocalvars = localvars;
504 savehandler = handler;
505 if (setjmp(jmploc.loc))
506 need_longjmp = exception != EXERROR && exception != EXEXEC;
509 expandarg(redir->nhere.doc, fn, 0);
510 redir->nhere.expdoc = fn->list->text;
513 handler = savehandler;
516 localvars = savelocalvars;
518 longjmp(handler->loc, 1);
524 * Compute the names of the files in a redirection list.
528 expredir(union node *n)
532 for (redir = n ; redir ; redir = redir->nfile.next) {
535 switch (redir->type) {
541 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
542 redir->nfile.expfname = fn.list->text;
546 if (redir->ndup.vname) {
547 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
548 fixredir(redir, fn.list->text, 1);
561 * Evaluate a pipeline. All the processes in the pipeline are children
562 * of the process creating the pipeline. (This differs from some versions
563 * of the shell, which make the last process in a pipeline the parent
568 evalpipe(union node *n)
576 TRACE(("evalpipe(%p) called\n", (void *)n));
578 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
581 jp = makejob(n, pipelen);
583 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
590 error("Pipe call failed: %s", strerror(errno));
593 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
600 if (!(prevfd >= 0 && pip[0] == 0))
607 evaltree(lp->n, EV_EXIT);
616 if (n->npipe.backgnd == 0) {
618 exitstatus = waitforjob(jp, (int *)NULL);
619 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
628 is_valid_fast_cmdsubst(union node *n)
631 return (n->type == NCMD);
635 * Execute a command inside back quotes. If it's a builtin command, we
636 * want to save its output in a block obtained from malloc. Otherwise
637 * we fork off a subprocess and get the output of the command via a pipe.
638 * Should be called with interrupts off.
642 evalbackcmd(union node *n, struct backcmd *result)
646 struct stackmark smark;
647 struct jmploc jmploc;
648 struct jmploc *savehandler;
649 struct localvar *savelocalvars;
651 setstackmark(&smark);
660 exitstatus = oexitstatus;
661 if (is_valid_fast_cmdsubst(n)) {
662 savelocalvars = localvars;
665 savehandler = handler;
666 if (setjmp(jmploc.loc)) {
667 if (exception == EXERROR || exception == EXEXEC)
669 else if (exception != 0) {
670 handler = savehandler;
673 localvars = savelocalvars;
674 longjmp(handler->loc, 1);
678 evalcommand(n, EV_BACKCMD, result);
680 handler = savehandler;
683 localvars = savelocalvars;
686 error("Pipe call failed: %s", strerror(errno));
688 if (forkshell(jp, n, FORK_NOJOB) == 0) {
695 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 * Check if a builtin can safely be executed in the same process,
755 * even though it should be in a subshell (command substitution).
756 * Note that jobid, jobs, times and trap can show information not
757 * available in a child process; this is deliberate.
758 * The arguments should already have been expanded.
761 safe_builtin(int idx, int argc, char **argv)
763 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
764 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
765 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
766 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
769 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
771 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
773 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
774 argv[1][0] == '+') && argv[1][1] == 'o' &&
775 argv[1][2] == '\0'));
780 * Execute a simple command.
781 * Note: This may or may not return if (flags & EV_EXIT).
785 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
788 struct arglist arglist;
789 struct arglist varlist;
797 struct cmdentry cmdentry;
799 struct jmploc jmploc;
800 struct jmploc *savehandler;
802 struct shparam saveparam;
803 struct localvar *savelocalvars;
804 struct parsefile *savetopfile;
808 int do_clearcmdentry;
809 const char *path = pathval();
811 /* First expand the arguments. */
812 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
813 arglist.lastp = &arglist.list;
814 varlist.lastp = &varlist.list;
817 do_clearcmdentry = 0;
818 oexitstatus = exitstatus;
820 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
821 if (varflag && isassignment(argp->narg.text)) {
822 expandarg(argp, varflag == 1 ? &varlist : &arglist,
825 } else if (varflag == 1)
826 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
827 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
829 *arglist.lastp = NULL;
830 *varlist.lastp = NULL;
831 expredir(cmd->ncmd.redirect);
833 for (sp = arglist.list ; sp ; sp = sp->next)
835 /* Add one slot at the beginning for tryexec(). */
836 argv = stalloc(sizeof (char *) * (argc + 2));
839 for (sp = arglist.list ; sp ; sp = sp->next) {
840 TRACE(("evalcommand arg: %s\n", sp->text));
845 if (iflag && funcnest == 0 && argc > 0)
849 /* Print the command if xflag is set. */
853 ps4 = expandstr(ps4val());
854 out2str(ps4 != NULL ? ps4 : ps4val());
855 for (sp = varlist.list ; sp ; sp = sp->next) {
858 p = strchr(sp->text, '=');
861 outbin(sp->text, p - sp->text, out2);
867 for (sp = arglist.list ; sp ; sp = sp->next) {
870 /* Disambiguate command looking like assignment. */
871 if (sp == arglist.list &&
872 strchr(sp->text, '=') != NULL &&
873 strchr(sp->text, '\'') == NULL) {
885 /* Now locate the command. */
887 /* Variable assignment(s) without command */
888 cmdentry.cmdtype = CMDBUILTIN;
889 cmdentry.u.index = BLTINCMD;
890 cmdentry.special = 0;
892 static const char PATH[] = "PATH=";
893 int cmd_flags = 0, bltinonly = 0;
896 * Modify the command lookup path, if a PATH= assignment
899 for (sp = varlist.list ; sp ; sp = sp->next)
900 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
901 path = sp->text + sizeof(PATH) - 1;
903 * On `PATH=... command`, we need to make
904 * sure that the command isn't using the
905 * non-updated hash table of the outer PATH
906 * setting and we need to make sure that
907 * the hash table isn't filled with items
908 * from the temporary setting.
910 * It would be better to forbit using and
911 * updating the table while this command
912 * runs, by the command finding mechanism
913 * is heavily integrated with hash handling,
914 * so we just delete the hash before and after
915 * the command runs. Partly deleting like
916 * changepatch() does doesn't seem worth the
917 * bookinging effort, since most such runs add
918 * directories in front of the new PATH.
921 do_clearcmdentry = 1;
926 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
927 if (cmdentry.u.index < 0) {
928 cmdentry.u.index = BLTINCMD;
934 find_command(argv[0], &cmdentry, cmd_flags, path);
935 /* implement the bltin and command builtins here */
936 if (cmdentry.cmdtype != CMDBUILTIN)
938 if (cmdentry.u.index == BLTINCMD) {
944 } else if (cmdentry.u.index == COMMANDCMD) {
947 if (!strcmp(argv[1], "-p")) {
950 if (argv[2][0] == '-') {
951 if (strcmp(argv[2], "--"))
961 path = _PATH_STDPATH;
963 do_clearcmdentry = 1;
964 } else if (!strcmp(argv[1], "--")) {
969 } else if (argv[1][0] == '-')
975 cmd_flags |= DO_NOFUNC;
981 * Special builtins lose their special properties when
982 * called via 'command'.
984 if (cmd_flags & DO_NOFUNC)
985 cmdentry.special = 0;
988 /* Fork off a child process if necessary. */
989 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
990 && ((flags & EV_EXIT) == 0 || have_traps()))
991 || ((flags & EV_BACKCMD) != 0
992 && (cmdentry.cmdtype != CMDBUILTIN ||
993 !safe_builtin(cmdentry.u.index, argc, argv)))) {
994 jp = makejob(cmd, 1);
996 if (flags & EV_BACKCMD) {
999 error("Pipe call failed: %s", strerror(errno));
1001 if (cmdentry.cmdtype == CMDNORMAL &&
1002 cmd->ncmd.redirect == NULL &&
1003 varlist.list == NULL &&
1004 (mode == FORK_FG || mode == FORK_NOJOB) &&
1005 !disvforkset() && !iflag && !mflag) {
1006 vforkexecshell(jp, argv, environment(), path,
1007 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1010 if (forkshell(jp, cmd, mode) != 0)
1011 goto parent; /* at end of routine */
1012 if (flags & EV_BACKCMD) {
1019 flags &= ~EV_BACKCMD;
1024 /* This is the child process if a fork occurred. */
1025 /* Execute the command. */
1026 if (cmdentry.cmdtype == CMDFUNCTION) {
1028 trputs("Shell function: "); trargs(argv);
1030 saveparam = shellparam;
1031 shellparam.malloc = 0;
1032 shellparam.reset = 1;
1033 shellparam.nparam = argc - 1;
1034 shellparam.p = argv + 1;
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)
1246 int n = argc > 1 ? number(argv[1]) : 1;
1251 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1258 * The `command' command.
1261 commandcmd(int argc __unused, char **argv __unused)
1267 path = bltinlookup("PATH", 1);
1269 while ((ch = nextopt("pvV")) != '\0') {
1272 path = _PATH_STDPATH;
1275 cmd = TYPECMD_SMALLV;
1284 if (*argptr == NULL || argptr[1] != NULL)
1285 error("wrong number of arguments");
1286 return typecmd_impl(2, argptr - 1, cmd, path);
1288 if (*argptr != NULL)
1289 error("commandcmd bad call");
1292 * Do nothing successfully if no command was specified;
1293 * ksh also does this.
1300 * The return command.
1304 returncmd(int argc, char **argv)
1306 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1308 evalskip = SKIPRETURN;
1315 falsecmd(int argc __unused, char **argv __unused)
1322 truecmd(int argc __unused, char **argv __unused)
1329 execcmd(int argc, char **argv)
1332 * Because we have historically not supported any options,
1333 * only treat "--" specially.
1335 if (argc > 1 && strcmp(argv[1], "--") == 0)
1340 iflag = 0; /* exit on error */
1343 for (sp = cmdenviron; sp ; sp = sp->next)
1344 setvareq(sp->text, VEXPORT|VSTACK);
1345 shellexec(argv + 1, environment(), pathval(), 0);
1353 timescmd(int argc __unused, char **argv __unused)
1356 long shumins, shsmins, chumins, chsmins;
1357 double shusecs, shssecs, chusecs, chssecs;
1359 if (getrusage(RUSAGE_SELF, &ru) < 0)
1361 shumins = ru.ru_utime.tv_sec / 60;
1362 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1363 shsmins = ru.ru_stime.tv_sec / 60;
1364 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1365 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1367 chumins = ru.ru_utime.tv_sec / 60;
1368 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1369 chsmins = ru.ru_stime.tv_sec / 60;
1370 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1371 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1372 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);