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) {
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;
499 unsigned char saveoptreset;
501 redir->nhere.expdoc = "";
502 savelocalvars = localvars;
504 saveoptreset = shellparam.reset;
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->args[0];
515 handler = savehandler;
518 localvars = savelocalvars;
519 shellparam.reset = saveoptreset;
521 longjmp(handler->loc, 1);
527 * Compute the names of the files in a redirection list.
531 expredir(union node *n)
535 for (redir = n ; redir ; redir = redir->nfile.next) {
538 switch (redir->type) {
544 expandarg(redir->nfile.fname, &fn, EXP_TILDE);
545 redir->nfile.expfname = fn.args[0];
549 if (redir->ndup.vname) {
550 expandarg(redir->ndup.vname, &fn, EXP_TILDE);
551 fixredir(redir, fn.args[0], 1);
564 * Evaluate a pipeline. All the processes in the pipeline are children
565 * of the process creating the pipeline. (This differs from some versions
566 * of the shell, which make the last process in a pipeline the parent
571 evalpipe(union node *n)
579 TRACE(("evalpipe(%p) called\n", (void *)n));
581 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
584 jp = makejob(n, pipelen);
586 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
593 error("Pipe call failed: %s", strerror(errno));
596 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
603 if (!(prevfd >= 0 && pip[0] == 0))
610 evaltree(lp->n, EV_EXIT);
619 if (n->npipe.backgnd == 0) {
621 exitstatus = waitforjob(jp, (int *)NULL);
622 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
631 is_valid_fast_cmdsubst(union node *n)
634 return (n->type == NCMD);
638 * Execute a command inside back quotes. If it's a builtin command, we
639 * want to save its output in a block obtained from malloc. Otherwise
640 * we fork off a subprocess and get the output of the command via a pipe.
641 * Should be called with interrupts off.
645 evalbackcmd(union node *n, struct backcmd *result)
649 struct stackmark smark;
650 struct jmploc jmploc;
651 struct jmploc *savehandler;
652 struct localvar *savelocalvars;
653 unsigned char saveoptreset;
663 setstackmark(&smark);
664 exitstatus = oexitstatus;
665 if (is_valid_fast_cmdsubst(n)) {
666 savelocalvars = localvars;
668 saveoptreset = shellparam.reset;
670 savehandler = handler;
671 if (setjmp(jmploc.loc)) {
672 if (exception == EXERROR || exception == EXEXEC)
674 else if (exception != 0) {
675 handler = savehandler;
678 localvars = savelocalvars;
679 shellparam.reset = saveoptreset;
680 longjmp(handler->loc, 1);
684 evalcommand(n, EV_BACKCMD, result);
686 handler = savehandler;
689 localvars = savelocalvars;
690 shellparam.reset = saveoptreset;
693 error("Pipe call failed: %s", strerror(errno));
695 if (forkshell(jp, n, FORK_NOJOB) == 0) {
702 evaltree(n, EV_EXIT);
708 popstackmark(&smark);
709 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
710 result->fd, result->buf, result->nleft, result->jp));
714 mustexpandto(const char *argtext, const char *mask)
717 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
721 if (*argtext == CTLESC)
723 else if (BASESYNTAX[(int)*argtext] == CCTL)
725 if (*argtext != *mask)
727 if (*argtext == '\0')
735 isdeclarationcmd(struct narg *arg)
737 int have_command = 0;
741 while (mustexpandto(arg->text, "command")) {
743 arg = &arg->next->narg;
747 * To also allow "command -p" and "command --" as part of
748 * a declaration command, add code here.
749 * We do not do this, as ksh does not do it either and it
750 * is not required by POSIX.
753 return (mustexpandto(arg->text, "export") ||
754 mustexpandto(arg->text, "readonly") ||
755 (mustexpandto(arg->text, "local") &&
756 (have_command || !isfunc("local"))));
760 xtracecommand(struct arglist *varlist, int argc, char **argv)
763 const char *text, *p, *ps4;
766 ps4 = expandstr(ps4val());
767 out2str(ps4 != NULL ? ps4 : ps4val());
768 for (i = 0; i < varlist->count; i++) {
769 text = varlist->args[i];
772 p = strchr(text, '=');
775 outbin(text, p - text, out2);
781 for (i = 0; i < argc; i++) {
793 * Check if a builtin can safely be executed in the same process,
794 * even though it should be in a subshell (command substitution).
795 * Note that jobid, jobs, times and trap can show information not
796 * available in a child process; this is deliberate.
797 * The arguments should already have been expanded.
800 safe_builtin(int idx, int argc, char **argv)
802 /* Generated from builtins.def. */
803 if (safe_builtin_always(idx))
805 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
807 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
809 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
810 argv[1][0] == '+') && argv[1][1] == 'o' &&
811 argv[1][2] == '\0'));
816 * Execute a simple command.
817 * Note: This may or may not return if (flags & EV_EXIT).
821 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
824 struct arglist arglist;
825 struct arglist varlist;
832 struct cmdentry cmdentry;
834 struct jmploc jmploc;
835 struct jmploc *savehandler;
837 struct shparam saveparam;
838 struct localvar *savelocalvars;
839 struct parsefile *savetopfile;
843 int do_clearcmdentry;
844 const char *path = pathval();
847 /* First expand the arguments. */
848 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
849 emptyarglist(&arglist);
850 emptyarglist(&varlist);
853 do_clearcmdentry = 0;
854 oexitstatus = exitstatus;
856 /* Add one slot at the beginning for tryexec(). */
857 appendarglist(&arglist, nullstr);
858 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
859 if (varflag && isassignment(argp->narg.text)) {
860 expandarg(argp, varflag == 1 ? &varlist : &arglist,
863 } else if (varflag == 1)
864 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
865 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
867 appendarglist(&arglist, nullstr);
868 expredir(cmd->ncmd.redirect);
869 argc = arglist.count - 2;
870 argv = &arglist.args[1];
874 if (iflag && funcnest == 0 && argc > 0)
875 lastarg = argv[argc - 1];
877 /* Print the command if xflag is set. */
879 xtracecommand(&varlist, argc, argv);
881 /* Now locate the command. */
883 /* Variable assignment(s) without command */
884 cmdentry.cmdtype = CMDBUILTIN;
885 cmdentry.u.index = BLTINCMD;
886 cmdentry.special = 0;
888 static const char PATH[] = "PATH=";
889 int cmd_flags = 0, bltinonly = 0;
892 * Modify the command lookup path, if a PATH= assignment
895 for (i = 0; i < varlist.count; i++)
896 if (strncmp(varlist.args[i], PATH, sizeof(PATH) - 1) == 0) {
897 path = varlist.args[i] + sizeof(PATH) - 1;
899 * On `PATH=... command`, we need to make
900 * sure that the command isn't using the
901 * non-updated hash table of the outer PATH
902 * setting and we need to make sure that
903 * the hash table isn't filled with items
904 * from the temporary setting.
906 * It would be better to forbit using and
907 * updating the table while this command
908 * runs, by the command finding mechanism
909 * is heavily integrated with hash handling,
910 * so we just delete the hash before and after
911 * the command runs. Partly deleting like
912 * changepatch() does doesn't seem worth the
913 * bookinging effort, since most such runs add
914 * directories in front of the new PATH.
917 do_clearcmdentry = 1;
922 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
923 if (cmdentry.u.index < 0) {
924 cmdentry.u.index = BLTINCMD;
930 find_command(argv[0], &cmdentry, cmd_flags, path);
931 /* implement the bltin and command builtins here */
932 if (cmdentry.cmdtype != CMDBUILTIN)
934 if (cmdentry.u.index == BLTINCMD) {
940 } else if (cmdentry.u.index == COMMANDCMD) {
943 if (!strcmp(argv[1], "-p")) {
946 if (argv[2][0] == '-') {
947 if (strcmp(argv[2], "--"))
957 path = _PATH_STDPATH;
959 do_clearcmdentry = 1;
960 } else if (!strcmp(argv[1], "--")) {
965 } else if (argv[1][0] == '-')
971 cmd_flags |= DO_NOFUNC;
977 * Special builtins lose their special properties when
978 * called via 'command'.
980 if (cmd_flags & DO_NOFUNC)
981 cmdentry.special = 0;
984 /* Fork off a child process if necessary. */
985 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
986 && ((flags & EV_EXIT) == 0 || have_traps()))
987 || ((flags & EV_BACKCMD) != 0
988 && (cmdentry.cmdtype != CMDBUILTIN ||
989 !safe_builtin(cmdentry.u.index, argc, argv)))) {
990 jp = makejob(cmd, 1);
992 if (flags & EV_BACKCMD) {
995 error("Pipe call failed: %s", strerror(errno));
997 if (cmdentry.cmdtype == CMDNORMAL &&
998 cmd->ncmd.redirect == NULL &&
999 varlist.count == 0 &&
1000 (mode == FORK_FG || mode == FORK_NOJOB) &&
1001 !disvforkset() && !iflag && !mflag) {
1002 vforkexecshell(jp, argv, environment(), path,
1003 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1006 if (forkshell(jp, cmd, mode) != 0)
1007 goto parent; /* at end of routine */
1008 if (flags & EV_BACKCMD) {
1015 flags &= ~EV_BACKCMD;
1020 /* This is the child process if a fork occurred. */
1021 /* Execute the command. */
1022 if (cmdentry.cmdtype == CMDFUNCTION) {
1024 trputs("Shell function: "); trargs(argv);
1026 saveparam = shellparam;
1027 shellparam.malloc = 0;
1028 shellparam.reset = 1;
1029 shellparam.nparam = argc - 1;
1030 shellparam.p = argv + 1;
1031 shellparam.optp = NULL;
1032 shellparam.optnext = NULL;
1034 savelocalvars = localvars;
1036 reffunc(cmdentry.u.func);
1037 savehandler = handler;
1038 if (setjmp(jmploc.loc)) {
1040 unreffunc(cmdentry.u.func);
1042 localvars = savelocalvars;
1043 freeparam(&shellparam);
1044 shellparam = saveparam;
1046 handler = savehandler;
1047 longjmp(handler->loc, 1);
1051 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1053 for (i = 0; i < varlist.count; i++)
1054 mklocal(varlist.args[i]);
1055 exitstatus = oexitstatus;
1056 evaltree(getfuncnode(cmdentry.u.func),
1057 flags & (EV_TESTED | EV_EXIT));
1059 unreffunc(cmdentry.u.func);
1061 localvars = savelocalvars;
1062 freeparam(&shellparam);
1063 shellparam = saveparam;
1064 handler = savehandler;
1068 if (evalskip == SKIPRETURN) {
1073 exitshell(exitstatus);
1074 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1076 trputs("builtin command: "); trargs(argv);
1078 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1079 if (flags == EV_BACKCMD) {
1080 memout.nextc = memout.buf;
1081 mode |= REDIR_BACKQ;
1083 savecmdname = commandname;
1084 savetopfile = getcurrentfile();
1085 cmdenviron = &varlist;
1087 savehandler = handler;
1088 if (setjmp(jmploc.loc)) {
1091 exitstatus = SIGINT+128;
1092 else if (e != EXEXIT)
1097 redirect(cmd->ncmd.redirect, mode);
1098 outclearerror(out1);
1100 * If there is no command word, redirection errors should
1101 * not be fatal but assignment errors should.
1104 cmdentry.special = 1;
1105 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1108 commandname = argv[0];
1110 nextopt_optptr = NULL; /* initialize nextopt */
1111 builtin_flags = flags;
1112 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1114 if (outiserror(out1)) {
1115 warning("write error on stdout");
1116 if (exitstatus == 0 || exitstatus == 1)
1126 handler = savehandler;
1127 commandname = savecmdname;
1129 exitshell(exitstatus);
1130 if (flags == EV_BACKCMD) {
1131 backcmd->buf = memout.buf;
1132 backcmd->nleft = memout.buf != NULL ?
1133 memout.nextc - memout.buf : 0;
1135 memout.nextc = NULL;
1136 memout.bufend = NULL;
1137 memout.bufsize = 64;
1139 if (cmdentry.u.index != EXECCMD)
1142 if ((e != EXERROR && e != EXEXEC)
1143 || cmdentry.special)
1145 popfilesupto(savetopfile);
1146 if (flags != EV_BACKCMD)
1151 trputs("normal command: "); trargs(argv);
1153 redirect(cmd->ncmd.redirect, 0);
1154 for (i = 0; i < varlist.count; i++)
1155 setvareq(varlist.args[i], VEXPORT|VSTACK);
1156 envp = environment();
1157 shellexec(argv, envp, path, cmdentry.u.index);
1162 parent: /* parent process gets here (if we forked) */
1163 if (mode == FORK_FG) { /* argument to fork */
1165 exitstatus = waitforjob(jp, &signaled);
1167 if (iflag && loopnest > 0 && signaled) {
1168 evalskip = SKIPBREAK;
1169 skipcount = loopnest;
1171 } else if (mode == FORK_NOJOB) {
1172 backcmd->fd = pip[0];
1179 setvar("_", lastarg, 0);
1180 if (do_clearcmdentry)
1187 * Search for a command. This is called before we fork so that the
1188 * location of the command will be available in the parent as well as
1189 * the child. The check for "goodname" is an overly conservative
1190 * check that the name will not be subject to expansion.
1194 prehash(union node *n)
1196 struct cmdentry entry;
1198 if (n && n->type == NCMD && n->ncmd.args)
1199 if (goodname(n->ncmd.args->narg.text))
1200 find_command(n->ncmd.args->narg.text, &entry, 0,
1207 * Builtin commands. Builtin commands whose functions are closely
1208 * tied to evaluation are implemented here.
1212 * No command given, a bltin command with no arguments, or a bltin command
1213 * with an invalid name.
1217 bltincmd(int argc, char **argv)
1220 out2fmt_flush("%s: not found\n", argv[1]);
1224 * Preserve exitstatus of a previous possible command substitution
1232 * Handle break and continue commands. Break, continue, and return are
1233 * all handled by setting the evalskip flag. The evaluation routines
1234 * above all check this flag, and if it is set they start skipping
1235 * commands rather than executing them. The variable skipcount is
1236 * the number of loops to break/continue, or the number of function
1237 * levels to return. (The latter is always 1.) It should probably
1238 * be an error to break out of more loops than exist, but it isn't
1239 * in the standard shell so we don't make it one here.
1243 breakcmd(int argc, char **argv)
1249 /* Allow arbitrarily large numbers. */
1250 n = strtol(argv[1], &end, 10);
1251 if (!is_digit(argv[1][0]) || *end != '\0')
1252 error("Illegal number: %s", argv[1]);
1258 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1265 * The `command' command.
1268 commandcmd(int argc __unused, char **argv __unused)
1274 path = bltinlookup("PATH", 1);
1276 while ((ch = nextopt("pvV")) != '\0') {
1279 path = _PATH_STDPATH;
1282 cmd = TYPECMD_SMALLV;
1291 if (*argptr == NULL || argptr[1] != NULL)
1292 error("wrong number of arguments");
1293 return typecmd_impl(2, argptr - 1, cmd, path);
1295 if (*argptr != NULL)
1296 error("commandcmd bad call");
1299 * Do nothing successfully if no command was specified;
1300 * ksh also does this.
1307 * The return command.
1311 returncmd(int argc, char **argv)
1313 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1315 evalskip = SKIPRETURN;
1322 falsecmd(int argc __unused, char **argv __unused)
1329 truecmd(int argc __unused, char **argv __unused)
1336 execcmd(int argc, char **argv)
1341 * Because we have historically not supported any options,
1342 * only treat "--" specially.
1344 if (argc > 1 && strcmp(argv[1], "--") == 0)
1347 iflag = 0; /* exit on error */
1350 for (i = 0; i < cmdenviron->count; i++)
1351 setvareq(cmdenviron->args[i], 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);