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
32 * @(#)eval.c 8.9 (Berkeley) 6/8/95
33 * $FreeBSD: head/bin/sh/eval.c 253658 2013-07-25 19:48:15Z jilles $
37 #include <sys/resource.h>
38 #include <sys/wait.h> /* For WIFSIGNALED(status) */
70 #include "myhistedit.h"
74 int evalskip; /* set if we are skipping commands */
75 int skipcount; /* number of levels to skip */
76 static int loopnest; /* current loop nesting level */
77 int funcnest; /* depth of function calls */
78 static int builtin_flags; /* evalcommand flags for builtins */
81 const char *commandname;
82 struct strlist *cmdenviron;
83 int exitstatus; /* exit status of last command */
84 int oexitstatus; /* saved exit status */
87 static void evalloop(union node *, int);
88 static void evalfor(union node *, int);
89 static union node *evalcase(union node *);
90 static void evalsubshell(union node *, int);
91 static void evalredir(union node *, int);
92 static void exphere(union node *, struct arglist *);
93 static void expredir(union node *);
94 static void evalpipe(union node *);
95 static int is_valid_fast_cmdsubst(union node *n);
96 static void evalcommand(union node *, int, struct backcmd *);
97 static void prehash(union node *);
101 * Called to reset things after an exception.
118 evalcmd(int argc, char **argv)
127 STARTSTACKSTR(concat);
131 if ((p = *ap++) == NULL)
135 STPUTC('\0', concat);
136 p = grabstackstr(concat);
138 evalstring(p, builtin_flags);
146 * Execute a command or commands contained in a string.
150 evalstring(char *s, int flags)
153 struct stackmark smark;
157 flags_exit = flags & EV_EXIT;
160 setstackmark(&smark);
161 setinputstring(s, 1);
162 while ((n = parsecmd(0)) != NEOF) {
163 if (n != NULL && !nflag) {
164 if (flags_exit && preadateof())
165 evaltree(n, flags | EV_EXIT);
170 popstackmark(&smark);
171 setstackmark(&smark);
174 popstackmark(&smark);
183 * Evaluate a parse tree. The value is left in the global variable
188 evaltree(union node *n, int flags)
192 struct stackmark smark;
194 setstackmark(&smark);
197 TRACE(("evaltree(NULL) called\n"));
204 displayhist = 1; /* show history substitutions done with fc */
206 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
209 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
212 next = n->nbinary.ch2;
215 evaltree(n->nbinary.ch1, EV_TESTED);
216 if (evalskip || exitstatus != 0) {
219 next = n->nbinary.ch2;
222 evaltree(n->nbinary.ch1, EV_TESTED);
223 if (evalskip || exitstatus == 0)
225 next = n->nbinary.ch2;
231 evalsubshell(n, flags);
232 do_etest = !(flags & EV_TESTED);
235 evalsubshell(n, flags);
238 evaltree(n->nif.test, EV_TESTED);
242 next = n->nif.ifpart;
243 else if (n->nif.elsepart)
244 next = n->nif.elsepart;
251 evalloop(n, flags & ~EV_EXIT);
254 evalfor(n, flags & ~EV_EXIT);
260 next = n->nclist.body;
263 if (n->nclist.body) {
264 evaltree(n->nclist.body, flags & ~EV_EXIT);
268 next = n->nclist.next;
271 defun(n->narg.text, n->narg.next);
275 evaltree(n->nnot.com, EV_TESTED);
278 exitstatus = !exitstatus;
283 do_etest = !(flags & EV_TESTED);
286 evalcommand(n, flags, NULL);
287 do_etest = !(flags & EV_TESTED);
290 out1fmt("Node type = %d\n", n->type);
295 popstackmark(&smark);
296 setstackmark(&smark);
299 popstackmark(&smark);
302 if (eflag && exitstatus != 0 && do_etest)
303 exitshell(exitstatus);
310 evalloop(union node *n, int flags)
317 evaltree(n->nbinary.ch1, EV_TESTED);
319 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
323 if (evalskip == SKIPBREAK && --skipcount <= 0)
325 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
329 if (n->type == NWHILE) {
336 evaltree(n->nbinary.ch2, flags);
348 evalfor(union node *n, int flags)
350 struct arglist arglist;
355 arglist.lastp = &arglist.list;
356 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
357 oexitstatus = exitstatus;
358 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
360 *arglist.lastp = NULL;
364 for (sp = arglist.list ; sp ; sp = sp->next) {
365 setvar(n->nfor.var, sp->text, 0);
366 evaltree(n->nfor.body, flags);
369 if (evalskip == SKIPCONT && --skipcount <= 0) {
373 if (evalskip == SKIPBREAK && --skipcount <= 0)
384 * Evaluate a case statement, returning the selected tree.
386 * The exit status needs care to get right.
390 evalcase(union node *n)
394 struct arglist arglist;
396 arglist.lastp = &arglist.list;
397 oexitstatus = exitstatus;
398 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
399 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
400 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
401 if (casematch(patp, arglist.list->text)) {
402 while (cp->nclist.next &&
403 cp->type == NCLISTFALLTHRU &&
404 cp->nclist.body == NULL)
405 cp = cp->nclist.next;
406 if (cp->nclist.next &&
407 cp->type == NCLISTFALLTHRU)
409 if (cp->nclist.body == NULL)
411 return (cp->nclist.body);
422 * Kick off a subshell to evaluate a tree.
426 evalsubshell(union node *n, int flags)
429 int backgnd = (n->type == NBACKGND);
431 oexitstatus = exitstatus;
432 expredir(n->nredir.redirect);
433 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
434 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
437 redirect(n->nredir.redirect, 0);
438 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
439 } else if (!backgnd) {
441 exitstatus = waitforjob(jp, NULL);
449 * Evaluate a redirected compound command.
453 evalredir(union node *n, int flags)
455 struct jmploc jmploc;
456 struct jmploc *savehandler;
457 volatile int in_redirect = 1;
459 oexitstatus = exitstatus;
460 expredir(n->nredir.redirect);
461 savehandler = handler;
462 if (setjmp(jmploc.loc)) {
465 handler = savehandler;
468 if (e == EXERROR || e == EXEXEC) {
474 longjmp(handler->loc, 1);
478 redirect(n->nredir.redirect, REDIR_PUSH);
481 evaltree(n->nredir.n, flags);
484 handler = savehandler;
491 exphere(union node *redir, struct arglist *fn)
493 struct jmploc jmploc;
494 struct jmploc *savehandler;
495 struct localvar *savelocalvars;
496 int need_longjmp = 0;
498 redir->nhere.expdoc = nullstr;
499 savelocalvars = localvars;
502 savehandler = handler;
503 if (setjmp(jmploc.loc))
504 need_longjmp = exception != EXERROR && exception != EXEXEC;
507 expandarg(redir->nhere.doc, fn, 0);
508 redir->nhere.expdoc = fn->list->text;
511 handler = savehandler;
514 localvars = savelocalvars;
516 longjmp(handler->loc, 1);
522 * Compute the names of the files in a redirection list.
526 expredir(union node *n)
530 for (redir = n ; redir ; redir = redir->nfile.next) {
533 switch (redir->type) {
539 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
540 redir->nfile.expfname = fn.list->text;
544 if (redir->ndup.vname) {
545 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
546 fixredir(redir, fn.list->text, 1);
559 * Evaluate a pipeline. All the processes in the pipeline are children
560 * of the process creating the pipeline. (This differs from some versions
561 * of the shell, which make the last process in a pipeline the parent
566 evalpipe(union node *n)
574 TRACE(("evalpipe(%p) called\n", (void *)n));
576 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
579 jp = makejob(n, pipelen);
581 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
588 error("Pipe call failed: %s", strerror(errno));
591 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
598 if (!(prevfd >= 0 && pip[0] == 0))
605 evaltree(lp->n, EV_EXIT);
614 if (n->npipe.backgnd == 0) {
616 exitstatus = waitforjob(jp, NULL);
617 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
626 is_valid_fast_cmdsubst(union node *n)
629 return (n->type == NCMD);
633 * Execute a command inside back quotes. If it's a builtin command, we
634 * want to save its output in a block obtained from malloc. Otherwise
635 * we fork off a subprocess and get the output of the command via a pipe.
636 * Should be called with interrupts off.
640 evalbackcmd(union node *n, struct backcmd *result)
644 struct stackmark smark;
645 struct jmploc jmploc;
646 struct jmploc *savehandler;
647 struct localvar *savelocalvars;
649 setstackmark(&smark);
658 exitstatus = oexitstatus;
659 if (is_valid_fast_cmdsubst(n)) {
660 savelocalvars = localvars;
663 savehandler = handler;
664 if (setjmp(jmploc.loc)) {
665 if (exception == EXERROR || exception == EXEXEC)
667 else if (exception != 0) {
668 handler = savehandler;
671 localvars = savelocalvars;
672 longjmp(handler->loc, 1);
676 evalcommand(n, EV_BACKCMD, result);
678 handler = savehandler;
681 localvars = savelocalvars;
684 error("Pipe call failed: %s", strerror(errno));
686 if (forkshell(jp, n, FORK_NOJOB) == 0) {
693 evaltree(n, EV_EXIT);
700 popstackmark(&smark);
701 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
702 result->fd, result->buf, result->nleft, result->jp));
706 mustexpandto(const char *argtext, const char *mask)
709 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
713 if (*argtext == CTLESC)
715 else if (BASESYNTAX[(int)*argtext] == CCTL)
717 if (*argtext != *mask)
719 if (*argtext == '\0')
727 isdeclarationcmd(struct narg *arg)
729 int have_command = 0;
733 while (mustexpandto(arg->text, "command")) {
735 arg = &arg->next->narg;
739 * To also allow "command -p" and "command --" as part of
740 * a declaration command, add code here.
741 * We do not do this, as ksh does not do it either and it
742 * is not required by POSIX.
745 return (mustexpandto(arg->text, "export") ||
746 mustexpandto(arg->text, "readonly") ||
747 (mustexpandto(arg->text, "local") &&
748 (have_command || !isfunc("local"))));
752 * Check if a builtin can safely be executed in the same process,
753 * even though it should be in a subshell (command substitution).
754 * Note that jobid, jobs, times and trap can show information not
755 * available in a child process; this is deliberate.
756 * The arguments should already have been expanded.
759 safe_builtin(int idx, int argc, char **argv)
761 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
762 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
763 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
764 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
767 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
769 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
771 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
772 argv[1][0] == '+') && argv[1][1] == 'o' &&
773 argv[1][2] == '\0'));
778 * Execute a simple command.
779 * Note: This may or may not return if (flags & EV_EXIT).
783 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
786 struct arglist arglist;
787 struct arglist varlist;
788 volatile int flags = flgs;
789 char **volatile argv;
796 struct cmdentry cmdentry;
797 struct job *volatile jp;
798 struct jmploc jmploc;
799 struct jmploc *savehandler;
800 const char *savecmdname;
801 struct shparam saveparam;
802 struct localvar *savelocalvars;
803 struct parsefile *savetopfile;
805 char *volatile lastarg;
807 volatile int do_clearcmdentry;
808 const char *path = pathval();
810 /* First expand the arguments. */
811 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
812 arglist.lastp = &arglist.list;
813 varlist.lastp = &varlist.list;
816 do_clearcmdentry = 0;
817 oexitstatus = exitstatus;
819 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
820 if (varflag && isassignment(argp->narg.text)) {
821 expandarg(argp, varflag == 1 ? &varlist : &arglist,
824 } else if (varflag == 1)
825 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
826 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
828 *arglist.lastp = NULL;
829 *varlist.lastp = NULL;
830 expredir(cmd->ncmd.redirect);
832 for (sp = arglist.list ; sp ; sp = sp->next)
834 /* Add one slot at the beginning for tryexec(). */
835 argv = stalloc(sizeof (char *) * (argc + 2));
838 for (sp = arglist.list ; sp ; sp = sp->next) {
839 TRACE(("evalcommand arg: %s\n", sp->text));
844 if (iflag && funcnest == 0 && argc > 0)
848 /* Print the command if xflag is set. */
852 ps4 = expandstr(ps4val());
853 out2str(ps4 != NULL ? ps4 : ps4val());
854 for (sp = varlist.list ; sp ; sp = sp->next) {
857 p = strchr(sp->text, '=');
860 outbin(sp->text, p - sp->text, out2);
866 for (sp = arglist.list ; sp ; sp = sp->next) {
869 /* Disambiguate command looking like assignment. */
870 if (sp == arglist.list &&
871 strchr(sp->text, '=') != NULL &&
872 strchr(sp->text, '\'') == NULL) {
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 (sp = varlist.list ; sp ; sp = sp->next)
899 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
900 path = sp->text + 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.list == NULL &&
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.optnext = NULL;
1036 savelocalvars = localvars;
1038 reffunc(cmdentry.u.func);
1039 savehandler = handler;
1040 if (setjmp(jmploc.loc)) {
1041 freeparam(&shellparam);
1042 shellparam = saveparam;
1044 unreffunc(cmdentry.u.func);
1046 localvars = savelocalvars;
1048 handler = savehandler;
1049 longjmp(handler->loc, 1);
1053 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1055 for (sp = varlist.list ; sp ; sp = sp->next)
1057 exitstatus = oexitstatus;
1058 evaltree(getfuncnode(cmdentry.u.func),
1059 flags & (EV_TESTED | EV_EXIT));
1061 unreffunc(cmdentry.u.func);
1063 localvars = savelocalvars;
1064 freeparam(&shellparam);
1065 shellparam = saveparam;
1066 handler = savehandler;
1070 if (evalskip == SKIPFUNC) {
1075 exitshell(exitstatus);
1076 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1078 trputs("builtin command: "); trargs(argv);
1080 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1081 if (flags == EV_BACKCMD) {
1083 memout.nextc = memout.buf;
1084 memout.bufsize = 64;
1085 mode |= REDIR_BACKQ;
1087 savecmdname = commandname;
1088 savetopfile = getcurrentfile();
1089 cmdenviron = varlist.list;
1091 savehandler = handler;
1092 if (setjmp(jmploc.loc)) {
1095 exitstatus = SIGINT+128;
1096 else if (e != EXEXIT)
1101 redirect(cmd->ncmd.redirect, mode);
1102 outclearerror(out1);
1104 * If there is no command word, redirection errors should
1105 * not be fatal but assignment errors should.
1108 cmdentry.special = 1;
1109 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1112 commandname = argv[0];
1114 nextopt_optptr = NULL; /* initialize nextopt */
1115 builtin_flags = flags;
1116 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1118 if (outiserror(out1)) {
1119 warning("write error on stdout");
1120 if (exitstatus == 0 || exitstatus == 1)
1130 handler = savehandler;
1131 commandname = savecmdname;
1133 exitshell(exitstatus);
1134 if (flags == EV_BACKCMD) {
1135 backcmd->buf = memout.buf;
1136 backcmd->nleft = memout.nextc - memout.buf;
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 (sp = varlist.list ; sp ; sp = sp->next)
1155 setvareq(sp->text, 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, &realstatus);
1167 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
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 redirection
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)
1245 int n = argc > 1 ? number(argv[1]) : 1;
1250 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1257 * The `command' command.
1260 commandcmd(int argc __unused, char **argv __unused)
1266 path = bltinlookup("PATH", 1);
1268 while ((ch = nextopt("pvV")) != '\0') {
1271 path = _PATH_STDPATH;
1274 cmd = TYPECMD_SMALLV;
1283 if (*argptr == NULL || argptr[1] != NULL)
1284 error("wrong number of arguments");
1285 return typecmd_impl(2, argptr - 1, cmd, path);
1287 if (*argptr != NULL)
1288 error("commandcmd bad call");
1291 * Do nothing successfully if no command was specified;
1292 * ksh also does this.
1299 * The return command.
1303 returncmd(int argc, char **argv)
1305 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1308 evalskip = SKIPFUNC;
1311 /* skip the rest of the file */
1312 evalskip = SKIPFILE;
1320 falsecmd(int argc __unused, char **argv __unused)
1327 truecmd(int argc __unused, char **argv __unused)
1334 execcmd(int argc, char **argv)
1337 * Because we have historically not supported any options,
1338 * only treat "--" specially.
1340 if (argc > 1 && strcmp(argv[1], "--") == 0)
1345 iflag = 0; /* exit on error */
1348 for (sp = cmdenviron; sp ; sp = sp->next)
1349 setvareq(sp->text, VEXPORT|VSTACK);
1350 shellexec(argv + 1, environment(), pathval(), 0);
1358 timescmd(int argc __unused, char **argv __unused)
1361 long shumins, shsmins, chumins, chsmins;
1362 double shusecs, shssecs, chusecs, chssecs;
1364 if (getrusage(RUSAGE_SELF, &ru) < 0)
1366 shumins = ru.ru_utime.tv_sec / 60;
1367 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1368 shsmins = ru.ru_stime.tv_sec / 60;
1369 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1370 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1372 chumins = ru.ru_utime.tv_sec / 60;
1373 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1374 chsmins = ru.ru_stime.tv_sec / 60;
1375 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1376 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1377 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);