3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
45 #include <sys/resource.h>
46 #include <sys/wait.h> /* For WIFSIGNALED(status) */
73 #include "myhistedit.h"
77 int evalskip; /* set if we are skipping commands */
78 int skipcount; /* number of levels to skip */
79 MKINIT int loopnest; /* current loop nesting level */
80 int funcnest; /* depth of function calls */
81 static int builtin_flags; /* evalcommand flags for builtins */
85 struct strlist *cmdenviron;
86 int exitstatus; /* exit status of last command */
87 int oexitstatus; /* saved exit status */
90 static void evalloop(union node *, int);
91 static void evalfor(union node *, int);
92 static union node *evalcase(union node *);
93 static void evalsubshell(union node *, int);
94 static void evalredir(union node *, int);
95 static void 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.
123 evalcmd(int argc, char **argv)
132 STARTSTACKSTR(concat);
136 if ((p = *ap++) == NULL)
140 STPUTC('\0', concat);
141 p = grabstackstr(concat);
143 evalstring(p, builtin_flags);
151 * Execute a command or commands contained in a string.
155 evalstring(char *s, int flags)
158 struct stackmark smark;
162 flags_exit = flags & EV_EXIT;
165 setstackmark(&smark);
166 setinputstring(s, 1);
167 while ((n = parsecmd(0)) != NEOF) {
168 if (n != NULL && !nflag) {
169 if (flags_exit && preadateof())
170 evaltree(n, flags | EV_EXIT);
175 popstackmark(&smark);
178 popstackmark(&smark);
187 * Evaluate a parse tree. The value is left in the global variable
192 evaltree(union node *n, int flags)
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);
278 exitstatus = !exitstatus;
283 do_etest = !(flags & EV_TESTED);
286 evalcommand(n, flags, (struct backcmd *)NULL);
287 do_etest = !(flags & EV_TESTED);
290 out1fmt("Node type = %d\n", n->type);
299 if (eflag && exitstatus != 0 && do_etest)
300 exitshell(exitstatus);
307 evalloop(union node *n, int flags)
314 evaltree(n->nbinary.ch1, EV_TESTED);
316 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
320 if (evalskip == SKIPBREAK && --skipcount <= 0)
322 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
326 if (n->type == NWHILE) {
333 evaltree(n->nbinary.ch2, flags);
345 evalfor(union node *n, int flags)
347 struct arglist arglist;
350 struct stackmark smark;
353 setstackmark(&smark);
354 arglist.lastp = &arglist.list;
355 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
356 oexitstatus = exitstatus;
357 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
359 *arglist.lastp = NULL;
363 for (sp = arglist.list ; sp ; sp = sp->next) {
364 setvar(n->nfor.var, sp->text, 0);
365 evaltree(n->nfor.body, flags);
368 if (evalskip == SKIPCONT && --skipcount <= 0) {
372 if (evalskip == SKIPBREAK && --skipcount <= 0)
378 popstackmark(&smark);
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;
395 struct stackmark smark;
397 setstackmark(&smark);
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 popstackmark(&smark);
405 while (cp->nclist.next &&
406 cp->type == NCLISTFALLTHRU &&
407 cp->nclist.body == NULL)
408 cp = cp->nclist.next;
409 if (cp->nclist.next &&
410 cp->type == NCLISTFALLTHRU)
412 if (cp->nclist.body == NULL)
414 return (cp->nclist.body);
418 popstackmark(&smark);
426 * Kick off a subshell to evaluate a tree.
430 evalsubshell(union node *n, int flags)
433 int backgnd = (n->type == NBACKGND);
435 oexitstatus = exitstatus;
436 expredir(n->nredir.redirect);
437 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
438 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
441 redirect(n->nredir.redirect, 0);
442 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
443 } else if (! backgnd) {
445 exitstatus = waitforjob(jp, (int *)NULL);
453 * Evaluate a redirected compound command.
457 evalredir(union node *n, int flags)
459 struct jmploc jmploc;
460 struct jmploc *savehandler;
461 volatile int in_redirect = 1;
463 oexitstatus = exitstatus;
464 expredir(n->nredir.redirect);
465 savehandler = handler;
466 if (setjmp(jmploc.loc)) {
469 handler = savehandler;
472 if (e == EXERROR || e == EXEXEC) {
478 longjmp(handler->loc, 1);
482 redirect(n->nredir.redirect, REDIR_PUSH);
485 evaltree(n->nredir.n, flags);
488 handler = savehandler;
495 * Compute the names of the files in a redirection list.
499 expredir(union node *n)
503 for (redir = n ; redir ; redir = redir->nfile.next) {
506 switch (redir->type) {
512 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
513 redir->nfile.expfname = fn.list->text;
517 if (redir->ndup.vname) {
518 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
519 fixredir(redir, fn.list->text, 1);
529 * Evaluate a pipeline. All the processes in the pipeline are children
530 * of the process creating the pipeline. (This differs from some versions
531 * of the shell, which make the last process in a pipeline the parent
536 evalpipe(union node *n)
544 TRACE(("evalpipe(%p) called\n", (void *)n));
546 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
549 jp = makejob(n, pipelen);
551 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
557 error("Pipe call failed: %s", strerror(errno));
560 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
567 if (!(prevfd >= 0 && pip[0] == 0))
574 evaltree(lp->n, EV_EXIT);
583 if (n->npipe.backgnd == 0) {
585 exitstatus = waitforjob(jp, (int *)NULL);
586 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
595 is_valid_fast_cmdsubst(union node *n)
598 return (n->type == NCMD);
602 * Execute a command inside back quotes. If it's a builtin command, we
603 * want to save its output in a block obtained from malloc. Otherwise
604 * we fork off a subprocess and get the output of the command via a pipe.
605 * Should be called with interrupts off.
609 evalbackcmd(union node *n, struct backcmd *result)
613 struct stackmark smark; /* unnecessary */
614 struct jmploc jmploc;
615 struct jmploc *savehandler;
616 struct localvar *savelocalvars;
618 setstackmark(&smark);
627 if (is_valid_fast_cmdsubst(n)) {
628 exitstatus = oexitstatus;
629 savelocalvars = localvars;
632 savehandler = handler;
633 if (setjmp(jmploc.loc)) {
634 if (exception == EXERROR || exception == EXEXEC)
636 else if (exception != 0) {
637 handler = savehandler;
640 localvars = savelocalvars;
641 longjmp(handler->loc, 1);
645 evalcommand(n, EV_BACKCMD, result);
647 handler = savehandler;
650 localvars = savelocalvars;
654 error("Pipe call failed: %s", strerror(errno));
656 if (forkshell(jp, n, FORK_NOJOB) == 0) {
663 evaltree(n, EV_EXIT);
670 popstackmark(&smark);
671 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
672 result->fd, result->buf, result->nleft, result->jp));
676 mustexpandto(const char *argtext, const char *mask)
679 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
683 if (*argtext == CTLESC)
685 else if (BASESYNTAX[(int)*argtext] == CCTL)
687 if (*argtext != *mask)
689 if (*argtext == '\0')
697 isdeclarationcmd(struct narg *arg)
699 int have_command = 0;
703 while (mustexpandto(arg->text, "command")) {
705 arg = &arg->next->narg;
709 * To also allow "command -p" and "command --" as part of
710 * a declaration command, add code here.
711 * We do not do this, as ksh does not do it either and it
712 * is not required by POSIX.
715 return (mustexpandto(arg->text, "export") ||
716 mustexpandto(arg->text, "readonly") ||
717 (mustexpandto(arg->text, "local") &&
718 (have_command || !isfunc("local"))));
722 * Check if a builtin can safely be executed in the same process,
723 * even though it should be in a subshell (command substitution).
724 * Note that jobid, jobs, times and trap can show information not
725 * available in a child process; this is deliberate.
726 * The arguments should already have been expanded.
729 safe_builtin(int idx, int argc, char **argv)
731 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
732 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
733 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
734 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
737 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
739 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
741 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
742 argv[1][0] == '+') && argv[1][1] == 'o' &&
743 argv[1][2] == '\0'));
748 * Execute a simple command.
749 * Note: This may or may not return if (flags & EV_EXIT).
753 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
755 struct stackmark smark;
757 struct arglist arglist;
758 struct arglist varlist;
766 struct cmdentry cmdentry;
768 struct jmploc jmploc;
769 struct jmploc *savehandler;
771 struct shparam saveparam;
772 struct localvar *savelocalvars;
773 struct parsefile *savetopfile;
777 int do_clearcmdentry;
778 const char *path = pathval();
780 /* First expand the arguments. */
781 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
782 setstackmark(&smark);
783 arglist.lastp = &arglist.list;
784 varlist.lastp = &varlist.list;
787 do_clearcmdentry = 0;
788 oexitstatus = exitstatus;
790 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
791 if (varflag && isassignment(argp->narg.text)) {
792 expandarg(argp, varflag == 1 ? &varlist : &arglist,
795 } else if (varflag == 1)
796 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
797 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
799 *arglist.lastp = NULL;
800 *varlist.lastp = NULL;
801 expredir(cmd->ncmd.redirect);
803 for (sp = arglist.list ; sp ; sp = sp->next)
805 /* Add one slot at the beginning for tryexec(). */
806 argv = stalloc(sizeof (char *) * (argc + 2));
809 for (sp = arglist.list ; sp ; sp = sp->next) {
810 TRACE(("evalcommand arg: %s\n", sp->text));
815 if (iflag && funcnest == 0 && argc > 0)
819 /* Print the command if xflag is set. */
823 ps4 = expandstr(ps4val());
824 out2str(ps4 != NULL ? ps4 : ps4val());
825 for (sp = varlist.list ; sp ; sp = sp->next) {
828 p = strchr(sp->text, '=');
831 outbin(sp->text, p - sp->text, out2);
837 for (sp = arglist.list ; sp ; sp = sp->next) {
840 /* Disambiguate command looking like assignment. */
841 if (sp == arglist.list &&
842 strchr(sp->text, '=') != NULL &&
843 strchr(sp->text, '\'') == NULL) {
855 /* Now locate the command. */
857 /* Variable assignment(s) without command */
858 cmdentry.cmdtype = CMDBUILTIN;
859 cmdentry.u.index = BLTINCMD;
860 cmdentry.special = 0;
862 static const char PATH[] = "PATH=";
863 int cmd_flags = 0, bltinonly = 0;
866 * Modify the command lookup path, if a PATH= assignment
869 for (sp = varlist.list ; sp ; sp = sp->next)
870 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
871 path = sp->text + sizeof(PATH) - 1;
873 * On `PATH=... command`, we need to make
874 * sure that the command isn't using the
875 * non-updated hash table of the outer PATH
876 * setting and we need to make sure that
877 * the hash table isn't filled with items
878 * from the temporary setting.
880 * It would be better to forbit using and
881 * updating the table while this command
882 * runs, by the command finding mechanism
883 * is heavily integrated with hash handling,
884 * so we just delete the hash before and after
885 * the command runs. Partly deleting like
886 * changepatch() does doesn't seem worth the
887 * bookinging effort, since most such runs add
888 * directories in front of the new PATH.
891 do_clearcmdentry = 1;
896 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
897 if (cmdentry.u.index < 0) {
898 cmdentry.u.index = BLTINCMD;
904 find_command(argv[0], &cmdentry, cmd_flags, path);
905 /* implement the bltin and command builtins here */
906 if (cmdentry.cmdtype != CMDBUILTIN)
908 if (cmdentry.u.index == BLTINCMD) {
914 } else if (cmdentry.u.index == COMMANDCMD) {
917 if (!strcmp(argv[1], "-p")) {
920 if (argv[2][0] == '-') {
921 if (strcmp(argv[2], "--"))
931 path = _PATH_STDPATH;
933 do_clearcmdentry = 1;
934 } else if (!strcmp(argv[1], "--")) {
939 } else if (argv[1][0] == '-')
945 cmd_flags |= DO_NOFUNC;
951 * Special builtins lose their special properties when
952 * called via 'command'.
954 if (cmd_flags & DO_NOFUNC)
955 cmdentry.special = 0;
958 /* Fork off a child process if necessary. */
959 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
960 && ((flags & EV_EXIT) == 0 || have_traps()))
961 || ((flags & EV_BACKCMD) != 0
962 && (cmdentry.cmdtype != CMDBUILTIN ||
963 !safe_builtin(cmdentry.u.index, argc, argv)))) {
964 jp = makejob(cmd, 1);
966 if (flags & EV_BACKCMD) {
969 error("Pipe call failed: %s", strerror(errno));
971 if (cmdentry.cmdtype == CMDNORMAL &&
972 cmd->ncmd.redirect == NULL &&
973 varlist.list == NULL &&
974 (mode == FORK_FG || mode == FORK_NOJOB) &&
975 !disvforkset() && !iflag && !mflag) {
976 vforkexecshell(jp, argv, environment(), path,
977 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
980 if (forkshell(jp, cmd, mode) != 0)
981 goto parent; /* at end of routine */
982 if (flags & EV_BACKCMD) {
989 flags &= ~EV_BACKCMD;
994 /* This is the child process if a fork occurred. */
995 /* Execute the command. */
996 if (cmdentry.cmdtype == CMDFUNCTION) {
998 trputs("Shell function: "); trargs(argv);
1000 saveparam = shellparam;
1001 shellparam.malloc = 0;
1002 shellparam.reset = 1;
1003 shellparam.nparam = argc - 1;
1004 shellparam.p = argv + 1;
1005 shellparam.optnext = NULL;
1007 savelocalvars = localvars;
1009 reffunc(cmdentry.u.func);
1010 savehandler = handler;
1011 if (setjmp(jmploc.loc)) {
1012 freeparam(&shellparam);
1013 shellparam = saveparam;
1015 unreffunc(cmdentry.u.func);
1017 localvars = savelocalvars;
1019 handler = savehandler;
1020 longjmp(handler->loc, 1);
1024 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1026 for (sp = varlist.list ; sp ; sp = sp->next)
1028 exitstatus = oexitstatus;
1029 evaltree(getfuncnode(cmdentry.u.func),
1030 flags & (EV_TESTED | EV_EXIT));
1032 unreffunc(cmdentry.u.func);
1034 localvars = savelocalvars;
1035 freeparam(&shellparam);
1036 shellparam = saveparam;
1037 handler = savehandler;
1041 if (evalskip == SKIPFUNC) {
1046 exitshell(exitstatus);
1047 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1049 trputs("builtin command: "); trargs(argv);
1051 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1052 if (flags == EV_BACKCMD) {
1054 memout.nextc = memout.buf;
1055 memout.bufsize = 64;
1056 mode |= REDIR_BACKQ;
1058 savecmdname = commandname;
1059 savetopfile = getcurrentfile();
1060 cmdenviron = varlist.list;
1062 savehandler = handler;
1063 if (setjmp(jmploc.loc)) {
1066 exitstatus = SIGINT+128;
1067 else if (e != EXEXIT)
1072 redirect(cmd->ncmd.redirect, mode);
1074 * If there is no command word, redirection errors should
1075 * not be fatal but assignment errors should.
1078 cmdentry.special = 1;
1079 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1082 commandname = argv[0];
1084 nextopt_optptr = NULL; /* initialize nextopt */
1085 builtin_flags = flags;
1086 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1095 handler = savehandler;
1096 commandname = savecmdname;
1098 exitshell(exitstatus);
1099 if (flags == EV_BACKCMD) {
1100 backcmd->buf = memout.buf;
1101 backcmd->nleft = memout.nextc - memout.buf;
1104 if (cmdentry.u.index != EXECCMD)
1107 if ((e != EXERROR && e != EXEXEC)
1108 || cmdentry.special)
1110 popfilesupto(savetopfile);
1111 if (flags != EV_BACKCMD)
1116 trputs("normal command: "); trargs(argv);
1118 redirect(cmd->ncmd.redirect, 0);
1119 for (sp = varlist.list ; sp ; sp = sp->next)
1120 setvareq(sp->text, VEXPORT|VSTACK);
1121 envp = environment();
1122 shellexec(argv, envp, path, cmdentry.u.index);
1127 parent: /* parent process gets here (if we forked) */
1128 if (mode == FORK_FG) { /* argument to fork */
1130 exitstatus = waitforjob(jp, &realstatus);
1132 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1133 evalskip = SKIPBREAK;
1134 skipcount = loopnest;
1136 } else if (mode == FORK_NOJOB) {
1137 backcmd->fd = pip[0];
1144 setvar("_", lastarg, 0);
1145 if (do_clearcmdentry)
1147 popstackmark(&smark);
1153 * Search for a command. This is called before we fork so that the
1154 * location of the command will be available in the parent as well as
1155 * the child. The check for "goodname" is an overly conservative
1156 * check that the name will not be subject to expansion.
1160 prehash(union node *n)
1162 struct cmdentry entry;
1164 if (n && n->type == NCMD && n->ncmd.args)
1165 if (goodname(n->ncmd.args->narg.text))
1166 find_command(n->ncmd.args->narg.text, &entry, 0,
1173 * Builtin commands. Builtin commands whose functions are closely
1174 * tied to evaluation are implemented here.
1178 * No command given, a bltin command with no arguments, or a bltin command
1179 * with an invalid name.
1183 bltincmd(int argc, char **argv)
1186 out2fmt_flush("%s: not found\n", argv[1]);
1190 * Preserve exitstatus of a previous possible redirection
1198 * Handle break and continue commands. Break, continue, and return are
1199 * all handled by setting the evalskip flag. The evaluation routines
1200 * above all check this flag, and if it is set they start skipping
1201 * commands rather than executing them. The variable skipcount is
1202 * the number of loops to break/continue, or the number of function
1203 * levels to return. (The latter is always 1.) It should probably
1204 * be an error to break out of more loops than exist, but it isn't
1205 * in the standard shell so we don't make it one here.
1209 breakcmd(int argc, char **argv)
1211 int n = argc > 1 ? number(argv[1]) : 1;
1216 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1223 * The `command' command.
1226 commandcmd(int argc __unused, char **argv __unused)
1232 path = bltinlookup("PATH", 1);
1234 while ((ch = nextopt("pvV")) != '\0') {
1237 path = _PATH_STDPATH;
1240 cmd = TYPECMD_SMALLV;
1249 if (*argptr == NULL || argptr[1] != NULL)
1250 error("wrong number of arguments");
1251 return typecmd_impl(2, argptr - 1, cmd, path);
1253 if (*argptr != NULL)
1254 error("commandcmd bad call");
1257 * Do nothing successfully if no command was specified;
1258 * ksh also does this.
1265 * The return command.
1269 returncmd(int argc, char **argv)
1271 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1274 evalskip = SKIPFUNC;
1277 /* skip the rest of the file */
1278 evalskip = SKIPFILE;
1286 falsecmd(int argc __unused, char **argv __unused)
1293 truecmd(int argc __unused, char **argv __unused)
1300 execcmd(int argc, char **argv)
1303 * Because we have historically not supported any options,
1304 * only treat "--" specially.
1306 if (argc > 1 && strcmp(argv[1], "--") == 0)
1311 iflag = 0; /* exit on error */
1314 for (sp = cmdenviron; sp ; sp = sp->next)
1315 setvareq(sp->text, VEXPORT|VSTACK);
1316 shellexec(argv + 1, environment(), pathval(), 0);
1324 timescmd(int argc __unused, char **argv __unused)
1327 long shumins, shsmins, chumins, chsmins;
1328 double shusecs, shssecs, chusecs, chssecs;
1330 if (getrusage(RUSAGE_SELF, &ru) < 0)
1332 shumins = ru.ru_utime.tv_sec / 60;
1333 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1334 shsmins = ru.ru_stime.tv_sec / 60;
1335 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1336 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1338 chumins = ru.ru_utime.tv_sec / 60;
1339 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1340 chsmins = ru.ru_stime.tv_sec / 60;
1341 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1342 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1343 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);