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>
72 #include "myhistedit.h"
76 int evalskip; /* set if we are skipping commands */
77 int skipcount; /* number of levels to skip */
78 static int loopnest; /* current loop nesting level */
79 int funcnest; /* depth of function calls */
80 static int builtin_flags; /* evalcommand flags for builtins */
84 struct arglist *cmdenviron;
85 int exitstatus; /* exit status of last command */
86 int oexitstatus; /* saved exit status */
89 static void evalloop(union node *, int);
90 static void evalfor(union node *, int);
91 static union node *evalcase(union node *);
92 static void evalsubshell(union node *, int);
93 static void evalredir(union node *, int);
94 static void exphere(union node *, struct arglist *);
95 static void expredir(union node *);
96 static void evalpipe(union node *);
97 static int is_valid_fast_cmdsubst(union node *n);
98 static void evalcommand(union node *, int, struct backcmd *);
99 static void prehash(union node *);
103 * Called to reset things after an exception.
119 evalcmd(int argc, char **argv)
128 STARTSTACKSTR(concat);
132 if ((p = *ap++) == NULL)
136 STPUTC('\0', concat);
137 p = grabstackstr(concat);
139 evalstring(p, builtin_flags);
147 * Execute a command or commands contained in a string.
151 evalstring(const char *s, int flags)
154 struct stackmark smark;
158 flags_exit = flags & EV_EXIT;
161 setstackmark(&smark);
162 setinputstring(s, 1);
163 while ((n = parsecmd(0)) != NEOF) {
164 if (n != NULL && !nflag) {
165 if (flags_exit && preadateof())
166 evaltree(n, flags | EV_EXIT);
173 popstackmark(&smark);
174 setstackmark(&smark);
177 popstackmark(&smark);
186 * Evaluate a parse tree. The value is left in the global variable
191 evaltree(union node *n, int flags)
195 struct stackmark smark;
197 setstackmark(&smark);
200 TRACE(("evaltree(NULL) called\n"));
207 displayhist = 1; /* show history substitutions done with fc */
209 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
212 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
215 next = n->nbinary.ch2;
218 evaltree(n->nbinary.ch1, EV_TESTED);
219 if (evalskip || exitstatus != 0) {
222 next = n->nbinary.ch2;
225 evaltree(n->nbinary.ch1, EV_TESTED);
226 if (evalskip || exitstatus == 0)
228 next = n->nbinary.ch2;
234 evalsubshell(n, flags);
235 do_etest = !(flags & EV_TESTED);
238 evalsubshell(n, flags);
241 evaltree(n->nif.test, EV_TESTED);
245 next = n->nif.ifpart;
246 else if (n->nif.elsepart)
247 next = n->nif.elsepart;
254 evalloop(n, flags & ~EV_EXIT);
257 evalfor(n, flags & ~EV_EXIT);
263 next = n->nclist.body;
266 if (n->nclist.body) {
267 evaltree(n->nclist.body, flags & ~EV_EXIT);
271 next = n->nclist.next;
274 defun(n->narg.text, n->narg.next);
278 evaltree(n->nnot.com, EV_TESTED);
281 exitstatus = !exitstatus;
286 do_etest = !(flags & EV_TESTED);
289 evalcommand(n, flags, (struct backcmd *)NULL);
290 do_etest = !(flags & EV_TESTED);
293 out1fmt("Node type = %d\n", n->type);
298 popstackmark(&smark);
299 setstackmark(&smark);
302 popstackmark(&smark);
305 if (eflag && exitstatus != 0 && do_etest)
306 exitshell(exitstatus);
313 evalloop(union node *n, int flags)
321 evaltree(n->nbinary.ch1, EV_TESTED);
323 if (evalskip == SKIPCONT && --skipcount <= 0) {
327 if (evalskip == SKIPBREAK && --skipcount <= 0)
329 if (evalskip == SKIPRETURN)
333 if (n->type == NWHILE) {
340 evaltree(n->nbinary.ch2, flags);
350 evalfor(union node *n, int flags)
352 struct arglist arglist;
357 emptyarglist(&arglist);
358 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
359 oexitstatus = exitstatus;
360 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
365 for (i = 0; i < arglist.count; i++) {
366 setvar(n->nfor.var, arglist.args[i], 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 emptyarglist(&arglist);
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.args[0])) {
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) {
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;
500 unsigned char saveoptreset;
502 redir->nhere.expdoc = "";
503 savelocalvars = localvars;
505 saveoptreset = shellparam.reset;
507 savehandler = handler;
508 if (setjmp(jmploc.loc))
509 need_longjmp = exception != EXERROR && exception != EXEXEC;
512 expandarg(redir->nhere.doc, fn, 0);
513 redir->nhere.expdoc = fn->args[0];
516 handler = savehandler;
519 localvars = savelocalvars;
520 shellparam.reset = saveoptreset;
522 longjmp(handler->loc, 1);
528 * Compute the names of the files in a redirection list.
532 expredir(union node *n)
536 for (redir = n ; redir ; redir = redir->nfile.next) {
539 switch (redir->type) {
545 expandarg(redir->nfile.fname, &fn, EXP_TILDE);
546 redir->nfile.expfname = fn.args[0];
550 if (redir->ndup.vname) {
551 expandarg(redir->ndup.vname, &fn, EXP_TILDE);
552 fixredir(redir, fn.args[0], 1);
565 * Evaluate a pipeline. All the processes in the pipeline are children
566 * of the process creating the pipeline. (This differs from some versions
567 * of the shell, which make the last process in a pipeline the parent
572 evalpipe(union node *n)
580 TRACE(("evalpipe(%p) called\n", (void *)n));
582 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
585 jp = makejob(n, pipelen);
587 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
594 error("Pipe call failed: %s", strerror(errno));
597 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
604 if (!(prevfd >= 0 && pip[0] == 0))
611 evaltree(lp->n, EV_EXIT);
620 if (n->npipe.backgnd == 0) {
622 exitstatus = waitforjob(jp, (int *)NULL);
623 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
632 is_valid_fast_cmdsubst(union node *n)
635 return (n->type == NCMD);
639 * Execute a command inside back quotes. If it's a builtin command, we
640 * want to save its output in a block obtained from malloc. Otherwise
641 * we fork off a subprocess and get the output of the command via a pipe.
642 * Should be called with interrupts off.
646 evalbackcmd(union node *n, struct backcmd *result)
650 struct stackmark smark;
651 struct jmploc jmploc;
652 struct jmploc *savehandler;
653 struct localvar *savelocalvars;
654 unsigned char saveoptreset;
664 setstackmark(&smark);
665 exitstatus = oexitstatus;
666 if (is_valid_fast_cmdsubst(n)) {
667 savelocalvars = localvars;
669 saveoptreset = shellparam.reset;
671 savehandler = handler;
672 if (setjmp(jmploc.loc)) {
673 if (exception == EXERROR)
675 else if (exception == EXEXEC)
677 else if (exception != 0) {
678 handler = savehandler;
681 localvars = savelocalvars;
682 shellparam.reset = saveoptreset;
683 longjmp(handler->loc, 1);
687 evalcommand(n, EV_BACKCMD, result);
689 handler = savehandler;
692 localvars = savelocalvars;
693 shellparam.reset = saveoptreset;
696 error("Pipe call failed: %s", strerror(errno));
698 if (forkshell(jp, n, FORK_NOJOB) == 0) {
705 evaltree(n, EV_EXIT);
711 popstackmark(&smark);
712 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
713 result->fd, result->buf, result->nleft, result->jp));
717 mustexpandto(const char *argtext, const char *mask)
720 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
724 if (*argtext == CTLESC)
726 else if (BASESYNTAX[(int)*argtext] == CCTL)
728 if (*argtext != *mask)
730 if (*argtext == '\0')
738 isdeclarationcmd(struct narg *arg)
740 int have_command = 0;
744 while (mustexpandto(arg->text, "command")) {
746 arg = &arg->next->narg;
750 * To also allow "command -p" and "command --" as part of
751 * a declaration command, add code here.
752 * We do not do this, as ksh does not do it either and it
753 * is not required by POSIX.
756 return (mustexpandto(arg->text, "export") ||
757 mustexpandto(arg->text, "readonly") ||
758 (mustexpandto(arg->text, "local") &&
759 (have_command || !isfunc("local"))));
763 xtracecommand(struct arglist *varlist, int argc, char **argv)
766 const char *text, *p, *ps4;
769 ps4 = expandstr(ps4val());
770 out2str(ps4 != NULL ? ps4 : ps4val());
771 for (i = 0; i < varlist->count; i++) {
772 text = varlist->args[i];
775 p = strchr(text, '=');
778 outbin(text, p - text, out2);
784 for (i = 0; i < argc; i++) {
796 * Check if a builtin can safely be executed in the same process,
797 * even though it should be in a subshell (command substitution).
798 * Note that jobid, jobs, times and trap can show information not
799 * available in a child process; this is deliberate.
800 * The arguments should already have been expanded.
803 safe_builtin(int idx, int argc, char **argv)
805 /* Generated from builtins.def. */
806 if (safe_builtin_always(idx))
808 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
810 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
812 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
813 argv[1][0] == '+') && argv[1][1] == 'o' &&
814 argv[1][2] == '\0'));
819 * Execute a simple command.
820 * Note: This may or may not return if (flags & EV_EXIT).
824 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
827 struct arglist arglist;
828 struct arglist varlist;
835 struct cmdentry cmdentry;
837 struct jmploc jmploc;
838 struct jmploc *savehandler;
840 struct shparam saveparam;
841 struct localvar *savelocalvars;
842 struct parsefile *savetopfile;
846 int do_clearcmdentry;
847 const char *path = pathval();
850 /* First expand the arguments. */
851 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
852 emptyarglist(&arglist);
853 emptyarglist(&varlist);
856 do_clearcmdentry = 0;
857 oexitstatus = exitstatus;
859 /* Add one slot at the beginning for tryexec(). */
860 appendarglist(&arglist, nullstr);
861 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
862 if (varflag && isassignment(argp->narg.text)) {
863 expandarg(argp, varflag == 1 ? &varlist : &arglist,
866 } else if (varflag == 1)
867 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
868 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
870 appendarglist(&arglist, nullstr);
871 expredir(cmd->ncmd.redirect);
872 argc = arglist.count - 2;
873 argv = &arglist.args[1];
877 if (iflag && funcnest == 0 && argc > 0)
878 lastarg = argv[argc - 1];
880 /* Print the command if xflag is set. */
882 xtracecommand(&varlist, argc, argv);
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 (i = 0; i < varlist.count; i++)
899 if (strncmp(varlist.args[i], PATH, sizeof(PATH) - 1) == 0) {
900 path = varlist.args[i] + 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.count == 0 &&
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)) {
1043 unreffunc(cmdentry.u.func);
1045 localvars = savelocalvars;
1046 freeparam(&shellparam);
1047 shellparam = saveparam;
1049 handler = savehandler;
1050 longjmp(handler->loc, 1);
1054 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1056 for (i = 0; i < varlist.count; i++)
1057 mklocal(varlist.args[i]);
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) {
1083 memout.nextc = memout.buf;
1084 mode |= REDIR_BACKQ;
1086 savecmdname = commandname;
1087 savetopfile = getcurrentfile();
1088 cmdenviron = &varlist;
1090 savehandler = handler;
1091 if (setjmp(jmploc.loc)) {
1094 exitstatus = SIGINT+128;
1095 else if (e != EXEXEC && e != EXEXIT)
1100 redirect(cmd->ncmd.redirect, mode);
1101 outclearerror(out1);
1103 * If there is no command word, redirection errors should
1104 * not be fatal but assignment errors should.
1107 cmdentry.special = 1;
1108 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1111 commandname = argv[0];
1113 nextopt_optptr = NULL; /* initialize nextopt */
1114 builtin_flags = flags;
1115 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1117 if (outiserror(out1)) {
1118 warning("write error on stdout");
1119 if (exitstatus == 0 || exitstatus == 1)
1129 handler = savehandler;
1130 commandname = savecmdname;
1132 exitshell(exitstatus);
1133 if (flags == EV_BACKCMD) {
1134 backcmd->buf = memout.buf;
1135 backcmd->nleft = memout.buf != NULL ?
1136 memout.nextc - memout.buf : 0;
1138 memout.nextc = NULL;
1139 memout.bufend = NULL;
1140 memout.bufsize = 64;
1142 if (cmdentry.u.index != EXECCMD)
1145 if ((e != EXERROR && e != EXEXEC)
1146 || cmdentry.special)
1148 popfilesupto(savetopfile);
1149 if (flags != EV_BACKCMD)
1154 trputs("normal command: "); trargs(argv);
1156 redirect(cmd->ncmd.redirect, 0);
1157 for (i = 0; i < varlist.count; i++)
1158 setvareq(varlist.args[i], VEXPORT|VSTACK);
1159 envp = environment();
1160 shellexec(argv, envp, path, cmdentry.u.index);
1165 parent: /* parent process gets here (if we forked) */
1166 if (mode == FORK_FG) { /* argument to fork */
1168 exitstatus = waitforjob(jp, &signaled);
1170 if (iflag && loopnest > 0 && signaled) {
1171 evalskip = SKIPBREAK;
1172 skipcount = loopnest;
1174 } else if (mode == FORK_NOJOB) {
1175 backcmd->fd = pip[0];
1182 setvar("_", lastarg, 0);
1183 if (do_clearcmdentry)
1190 * Search for a command. This is called before we fork so that the
1191 * location of the command will be available in the parent as well as
1192 * the child. The check for "goodname" is an overly conservative
1193 * check that the name will not be subject to expansion.
1197 prehash(union node *n)
1199 struct cmdentry entry;
1201 if (n && n->type == NCMD && n->ncmd.args)
1202 if (goodname(n->ncmd.args->narg.text))
1203 find_command(n->ncmd.args->narg.text, &entry, 0,
1210 * Builtin commands. Builtin commands whose functions are closely
1211 * tied to evaluation are implemented here.
1215 * No command given, a bltin command with no arguments, or a bltin command
1216 * with an invalid name.
1220 bltincmd(int argc, char **argv)
1223 out2fmt_flush("%s: not found\n", argv[1]);
1227 * Preserve exitstatus of a previous possible command substitution
1235 * Handle break and continue commands. Break, continue, and return are
1236 * all handled by setting the evalskip flag. The evaluation routines
1237 * above all check this flag, and if it is set they start skipping
1238 * commands rather than executing them. The variable skipcount is
1239 * the number of loops to break/continue, or the number of function
1240 * levels to return. (The latter is always 1.) It should probably
1241 * be an error to break out of more loops than exist, but it isn't
1242 * in the standard shell so we don't make it one here.
1246 breakcmd(int argc, char **argv)
1252 /* Allow arbitrarily large numbers. */
1253 n = strtol(argv[1], &end, 10);
1254 if (!is_digit(argv[1][0]) || *end != '\0')
1255 error("Illegal number: %s", argv[1]);
1261 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1268 * The `command' command.
1271 commandcmd(int argc __unused, char **argv __unused)
1277 path = bltinlookup("PATH", 1);
1279 while ((ch = nextopt("pvV")) != '\0') {
1282 path = _PATH_STDPATH;
1285 cmd = TYPECMD_SMALLV;
1294 if (*argptr == NULL || argptr[1] != NULL)
1295 error("wrong number of arguments");
1296 return typecmd_impl(2, argptr - 1, cmd, path);
1298 if (*argptr != NULL)
1299 error("commandcmd bad call");
1302 * Do nothing successfully if no command was specified;
1303 * ksh also does this.
1310 * The return command.
1314 returncmd(int argc, char **argv)
1316 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1318 evalskip = SKIPRETURN;
1325 falsecmd(int argc __unused, char **argv __unused)
1332 truecmd(int argc __unused, char **argv __unused)
1339 execcmd(int argc, char **argv)
1344 * Because we have historically not supported any options,
1345 * only treat "--" specially.
1347 if (argc > 1 && strcmp(argv[1], "--") == 0)
1350 iflag = 0; /* exit on error */
1353 for (i = 0; i < cmdenviron->count; i++)
1354 setvareq(cmdenviron->args[i], VEXPORT|VSTACK);
1355 shellexec(argv + 1, environment(), pathval(), 0);
1363 timescmd(int argc __unused, char **argv __unused)
1366 long shumins, shsmins, chumins, chsmins;
1367 double shusecs, shssecs, chusecs, chssecs;
1369 if (getrusage(RUSAGE_SELF, &ru) < 0)
1371 shumins = ru.ru_utime.tv_sec / 60;
1372 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1373 shsmins = ru.ru_stime.tv_sec / 60;
1374 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1375 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1377 chumins = ru.ru_utime.tv_sec / 60;
1378 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1379 chsmins = ru.ru_stime.tv_sec / 60;
1380 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1381 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1382 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);