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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)eval.c 8.9 (Berkeley) 6/8/95
37 * $FreeBSD: src/bin/sh/eval.c,v 1.120 2012/02/04 23:12:14 jilles Exp $
41 #include <sys/resource.h>
42 #include <sys/wait.h> /* For WIFSIGNALED(status) */
74 #include "myhistedit.h"
78 int evalskip; /* set if we are skipping commands */
79 int skipcount; /* number of levels to skip */
80 MKINIT int loopnest; /* current loop nesting level */
81 int funcnest; /* depth of function calls */
82 static int builtin_flags; /* evalcommand flags for builtins */
85 const char *commandname;
86 struct strlist *cmdenviron;
87 int exitstatus; /* exit status of last command */
88 int oexitstatus; /* saved exit status */
91 static void evalloop(union node *, int);
92 static void evalfor(union node *, int);
93 static union node *evalcase(union node *);
94 static void evalsubshell(union node *, int);
95 static void evalredir(union node *, int);
96 static void expredir(union node *);
97 static void evalpipe(union node *);
98 static int is_valid_fast_cmdsubst(union node *n);
99 static void evalcommand(union node *, int, struct backcmd *);
100 static void prehash(union node *);
104 * Called to reset things after an exception.
124 evalcmd(int argc, char **argv)
133 STARTSTACKSTR(concat);
137 if ((p = *ap++) == NULL)
141 STPUTC('\0', concat);
142 p = grabstackstr(concat);
144 evalstring(p, builtin_flags);
152 * Execute a command or commands contained in a string.
156 evalstring(char *s, int flags)
159 struct stackmark smark;
163 flags_exit = flags & EV_EXIT;
166 setstackmark(&smark);
167 setinputstring(s, 1);
168 while ((n = parsecmd(0)) != NEOF) {
169 if (n != NULL && !nflag) {
170 if (flags_exit && preadateof())
171 evaltree(n, flags | EV_EXIT);
176 popstackmark(&smark);
179 popstackmark(&smark);
188 * Evaluate a parse tree. The value is left in the global variable
193 evaltree(union node *n, int flags)
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);
279 exitstatus = !exitstatus;
284 do_etest = !(flags & EV_TESTED);
287 evalcommand(n, flags, NULL);
288 do_etest = !(flags & EV_TESTED);
291 out1fmt("Node type = %d\n", n->type);
300 if (eflag && exitstatus != 0 && do_etest)
301 exitshell(exitstatus);
308 evalloop(union node *n, int flags)
315 evaltree(n->nbinary.ch1, EV_TESTED);
317 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
321 if (evalskip == SKIPBREAK && --skipcount <= 0)
323 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
327 if (n->type == NWHILE) {
334 evaltree(n->nbinary.ch2, flags);
346 evalfor(union node *n, int flags)
348 struct arglist arglist;
351 struct stackmark smark;
354 setstackmark(&smark);
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)
379 popstackmark(&smark);
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;
396 struct stackmark smark;
398 setstackmark(&smark);
399 arglist.lastp = &arglist.list;
400 oexitstatus = exitstatus;
401 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
402 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
403 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
404 if (casematch(patp, arglist.list->text)) {
405 popstackmark(&smark);
406 while (cp->nclist.next &&
407 cp->type == NCLISTFALLTHRU &&
408 cp->nclist.body == NULL)
409 cp = cp->nclist.next;
410 if (cp->nclist.next &&
411 cp->type == NCLISTFALLTHRU)
413 if (cp->nclist.body == NULL)
415 return (cp->nclist.body);
419 popstackmark(&smark);
427 * Kick off a subshell to evaluate a tree.
431 evalsubshell(union node *n, int flags)
434 int backgnd = (n->type == NBACKGND);
436 oexitstatus = exitstatus;
437 expredir(n->nredir.redirect);
438 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
439 forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
442 redirect(n->nredir.redirect, 0);
443 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
444 } else if (!backgnd) {
446 exitstatus = waitforjob(jp, NULL);
454 * Evaluate a redirected compound command.
458 evalredir(union node *n, int flags)
460 struct jmploc jmploc;
461 struct jmploc *savehandler;
462 volatile int in_redirect = 1;
464 oexitstatus = exitstatus;
465 expredir(n->nredir.redirect);
466 savehandler = handler;
467 if (setjmp(jmploc.loc)) {
470 handler = savehandler;
473 if (e == EXERROR || e == EXEXEC) {
479 longjmp(handler->loc, 1);
483 redirect(n->nredir.redirect, REDIR_PUSH);
486 evaltree(n->nredir.n, flags);
489 handler = savehandler;
496 * Compute the names of the files in a redirection list.
500 expredir(union node *n)
504 for (redir = n ; redir ; redir = redir->nfile.next) {
507 switch (redir->type) {
513 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
514 redir->nfile.expfname = fn.list->text;
518 if (redir->ndup.vname) {
519 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
520 fixredir(redir, fn.list->text, 1);
530 * Evaluate a pipeline. All the processes in the pipeline are children
531 * of the process creating the pipeline. (This differs from some versions
532 * of the shell, which make the last process in a pipeline the parent
537 evalpipe(union node *n)
545 TRACE(("evalpipe(%p) called\n", (void *)n));
547 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
550 jp = makejob(n, pipelen);
552 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
559 error("Pipe call failed: %s", strerror(errno));
562 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
569 if (!(prevfd >= 0 && pip[0] == 0))
576 evaltree(lp->n, EV_EXIT);
585 if (n->npipe.backgnd == 0) {
587 exitstatus = waitforjob(jp, NULL);
588 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
597 is_valid_fast_cmdsubst(union node *n)
600 return (n->type == NCMD);
604 * Execute a command inside back quotes. If it's a builtin command, we
605 * want to save its output in a block obtained from malloc. Otherwise
606 * we fork off a subprocess and get the output of the command via a pipe.
607 * Should be called with interrupts off.
611 evalbackcmd(union node *n, struct backcmd *result)
615 struct stackmark smark; /* unnecessary */
616 struct jmploc jmploc;
617 struct jmploc *savehandler;
618 struct localvar *savelocalvars;
620 setstackmark(&smark);
629 if (is_valid_fast_cmdsubst(n)) {
630 exitstatus = oexitstatus;
631 savelocalvars = localvars;
634 savehandler = handler;
635 if (setjmp(jmploc.loc)) {
636 if (exception == EXERROR || exception == EXEXEC)
638 else if (exception != 0) {
639 handler = savehandler;
642 localvars = savelocalvars;
643 longjmp(handler->loc, 1);
647 evalcommand(n, EV_BACKCMD, result);
649 handler = savehandler;
652 localvars = savelocalvars;
656 error("Pipe call failed: %s", strerror(errno));
658 if (forkshell(jp, n, FORK_NOJOB) == 0) {
665 evaltree(n, EV_EXIT);
672 popstackmark(&smark);
673 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
674 result->fd, result->buf, result->nleft, result->jp));
678 * Check if a builtin can safely be executed in the same process,
679 * even though it should be in a subshell (command substitution).
680 * Note that jobid, jobs, times and trap can show information not
681 * available in a child process; this is deliberate.
682 * The arguments should already have been expanded.
685 safe_builtin(int idx, int argc, char **argv)
687 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
688 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
689 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
690 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
693 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
695 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
697 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
698 argv[1][0] == '+') && argv[1][1] == 'o' &&
699 argv[1][2] == '\0'));
704 * Execute a simple command.
705 * Note: This may or may not return if (flags & EV_EXIT).
709 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
711 struct stackmark smark;
713 struct arglist arglist;
714 struct arglist varlist;
715 volatile int flags = flgs;
716 char **volatile argv;
723 struct cmdentry cmdentry;
724 struct job *volatile jp;
725 struct jmploc jmploc;
726 struct jmploc *savehandler;
727 const char *savecmdname;
728 struct shparam saveparam;
729 struct localvar *savelocalvars;
730 struct parsefile *savetopfile;
732 char *volatile lastarg;
734 volatile int do_clearcmdentry;
735 const char *path = pathval();
737 /* First expand the arguments. */
738 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
739 setstackmark(&smark);
740 arglist.lastp = &arglist.list;
741 varlist.lastp = &varlist.list;
744 do_clearcmdentry = 0;
745 oexitstatus = exitstatus;
747 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
748 if (varflag && isassignment(argp->narg.text)) {
749 expandarg(argp, &varlist, EXP_VARTILDE);
752 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
755 *arglist.lastp = NULL;
756 *varlist.lastp = NULL;
757 expredir(cmd->ncmd.redirect);
759 for (sp = arglist.list ; sp ; sp = sp->next)
761 /* Add one slot at the beginning for tryexec(). */
762 argv = stalloc(sizeof (char *) * (argc + 2));
765 for (sp = arglist.list ; sp ; sp = sp->next) {
766 TRACE(("evalcommand arg: %s\n", sp->text));
771 if (iflag && funcnest == 0 && argc > 0)
775 /* Print the command if xflag is set. */
779 ps4 = expandstr(ps4val());
780 out2str(ps4 != NULL ? ps4 : ps4val());
781 for (sp = varlist.list ; sp ; sp = sp->next) {
784 p = strchr(sp->text, '=');
787 outbin(sp->text, p - sp->text, out2);
793 for (sp = arglist.list ; sp ; sp = sp->next) {
796 /* Disambiguate command looking like assignment. */
797 if (sp == arglist.list &&
798 strchr(sp->text, '=') != NULL &&
799 strchr(sp->text, '\'') == NULL) {
811 /* Now locate the command. */
813 /* Variable assignment(s) without command */
814 cmdentry.cmdtype = CMDBUILTIN;
815 cmdentry.u.index = BLTINCMD;
816 cmdentry.special = 0;
818 static const char PATH[] = "PATH=";
819 int cmd_flags = 0, bltinonly = 0;
822 * Modify the command lookup path, if a PATH= assignment
825 for (sp = varlist.list ; sp ; sp = sp->next)
826 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
827 path = sp->text + sizeof(PATH) - 1;
829 * On `PATH=... command`, we need to make
830 * sure that the command isn't using the
831 * non-updated hash table of the outer PATH
832 * setting and we need to make sure that
833 * the hash table isn't filled with items
834 * from the temporary setting.
836 * It would be better to forbit using and
837 * updating the table while this command
838 * runs, by the command finding mechanism
839 * is heavily integrated with hash handling,
840 * so we just delete the hash before and after
841 * the command runs. Partly deleting like
842 * changepatch() does doesn't seem worth the
843 * bookinging effort, since most such runs add
844 * directories in front of the new PATH.
847 do_clearcmdentry = 1;
852 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
853 if (cmdentry.u.index < 0) {
854 cmdentry.u.index = BLTINCMD;
860 find_command(argv[0], &cmdentry, cmd_flags, path);
861 /* implement the bltin and command builtins here */
862 if (cmdentry.cmdtype != CMDBUILTIN)
864 if (cmdentry.u.index == BLTINCMD) {
870 } else if (cmdentry.u.index == COMMANDCMD) {
873 if (!strcmp(argv[1], "-p")) {
876 if (argv[2][0] == '-') {
877 if (strcmp(argv[2], "--"))
887 path = _PATH_STDPATH;
889 do_clearcmdentry = 1;
890 } else if (!strcmp(argv[1], "--")) {
895 } else if (argv[1][0] == '-')
901 cmd_flags |= DO_NOFUNC;
907 * Special builtins lose their special properties when
908 * called via 'command'.
910 if (cmd_flags & DO_NOFUNC)
911 cmdentry.special = 0;
914 /* Fork off a child process if necessary. */
915 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
916 && ((flags & EV_EXIT) == 0 || have_traps()))
917 || ((flags & EV_BACKCMD) != 0
918 && (cmdentry.cmdtype != CMDBUILTIN ||
919 !safe_builtin(cmdentry.u.index, argc, argv)))) {
920 jp = makejob(cmd, 1);
922 if (flags & EV_BACKCMD) {
925 error("Pipe call failed: %s", strerror(errno));
927 if (cmdentry.cmdtype == CMDNORMAL &&
928 cmd->ncmd.redirect == NULL &&
929 varlist.list == NULL &&
930 (mode == FORK_FG || mode == FORK_NOJOB) &&
931 !disvforkset() && !iflag && !mflag) {
932 vforkexecshell(jp, argv, environment(), path,
933 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
936 if (forkshell(jp, cmd, mode) != 0)
937 goto parent; /* at end of routine */
938 if (flags & EV_BACKCMD) {
945 flags &= ~EV_BACKCMD;
950 /* This is the child process if a fork occurred. */
951 /* Execute the command. */
952 if (cmdentry.cmdtype == CMDFUNCTION) {
954 trputs("Shell function: "); trargs(argv);
956 saveparam = shellparam;
957 shellparam.malloc = 0;
958 shellparam.reset = 1;
959 shellparam.nparam = argc - 1;
960 shellparam.p = argv + 1;
961 shellparam.optnext = NULL;
963 savelocalvars = localvars;
965 reffunc(cmdentry.u.func);
966 savehandler = handler;
967 if (setjmp(jmploc.loc)) {
968 freeparam(&shellparam);
969 shellparam = saveparam;
971 unreffunc(cmdentry.u.func);
973 localvars = savelocalvars;
975 handler = savehandler;
976 longjmp(handler->loc, 1);
980 redirect(cmd->ncmd.redirect, REDIR_PUSH);
982 for (sp = varlist.list ; sp ; sp = sp->next)
984 exitstatus = oexitstatus;
985 evaltree(getfuncnode(cmdentry.u.func),
986 flags & (EV_TESTED | EV_EXIT));
988 unreffunc(cmdentry.u.func);
990 localvars = savelocalvars;
991 freeparam(&shellparam);
992 shellparam = saveparam;
993 handler = savehandler;
997 if (evalskip == SKIPFUNC) {
1002 exitshell(exitstatus);
1003 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1005 trputs("builtin command: "); trargs(argv);
1007 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1008 if (flags == EV_BACKCMD) {
1010 memout.nextc = memout.buf;
1011 memout.bufsize = 64;
1012 mode |= REDIR_BACKQ;
1014 savecmdname = commandname;
1015 savetopfile = getcurrentfile();
1016 cmdenviron = varlist.list;
1018 savehandler = handler;
1019 if (setjmp(jmploc.loc)) {
1022 exitstatus = SIGINT+128;
1023 else if (e != EXEXIT)
1028 redirect(cmd->ncmd.redirect, mode);
1030 * If there is no command word, redirection errors should
1031 * not be fatal but assignment errors should.
1034 cmdentry.special = 1;
1035 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1038 commandname = argv[0];
1040 nextopt_optptr = NULL; /* initialize nextopt */
1041 builtin_flags = flags;
1042 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1051 handler = savehandler;
1052 commandname = savecmdname;
1054 exitshell(exitstatus);
1055 if (flags == EV_BACKCMD) {
1056 backcmd->buf = memout.buf;
1057 backcmd->nleft = memout.nextc - memout.buf;
1060 if (cmdentry.u.index != EXECCMD)
1063 if ((e != EXERROR && e != EXEXEC)
1064 || cmdentry.special)
1066 popfilesupto(savetopfile);
1067 if (flags != EV_BACKCMD)
1072 trputs("normal command: "); trargs(argv);
1074 redirect(cmd->ncmd.redirect, 0);
1075 for (sp = varlist.list ; sp ; sp = sp->next)
1076 setvareq(sp->text, VEXPORT|VSTACK);
1077 envp = environment();
1078 shellexec(argv, envp, path, cmdentry.u.index);
1083 parent: /* parent process gets here (if we forked) */
1084 if (mode == FORK_FG) { /* argument to fork */
1086 exitstatus = waitforjob(jp, &realstatus);
1088 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1089 evalskip = SKIPBREAK;
1090 skipcount = loopnest;
1092 } else if (mode == FORK_NOJOB) {
1093 backcmd->fd = pip[0];
1100 setvar("_", lastarg, 0);
1101 if (do_clearcmdentry)
1103 popstackmark(&smark);
1109 * Search for a command. This is called before we fork so that the
1110 * location of the command will be available in the parent as well as
1111 * the child. The check for "goodname" is an overly conservative
1112 * check that the name will not be subject to expansion.
1116 prehash(union node *n)
1118 struct cmdentry entry;
1120 if (n && n->type == NCMD && n->ncmd.args)
1121 if (goodname(n->ncmd.args->narg.text))
1122 find_command(n->ncmd.args->narg.text, &entry, 0,
1129 * Builtin commands. Builtin commands whose functions are closely
1130 * tied to evaluation are implemented here.
1134 * No command given, a bltin command with no arguments, or a bltin command
1135 * with an invalid name.
1139 bltincmd(int argc, char **argv)
1142 out2fmt_flush("%s: not found\n", argv[1]);
1146 * Preserve exitstatus of a previous possible redirection
1154 * Handle break and continue commands. Break, continue, and return are
1155 * all handled by setting the evalskip flag. The evaluation routines
1156 * above all check this flag, and if it is set they start skipping
1157 * commands rather than executing them. The variable skipcount is
1158 * the number of loops to break/continue, or the number of function
1159 * levels to return. (The latter is always 1.) It should probably
1160 * be an error to break out of more loops than exist, but it isn't
1161 * in the standard shell so we don't make it one here.
1165 breakcmd(int argc, char **argv)
1167 int n = argc > 1 ? number(argv[1]) : 1;
1172 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1179 * The `command' command.
1182 commandcmd(int argc, char **argv)
1188 path = bltinlookup("PATH", 1);
1190 optind = optreset = 1;
1192 while ((ch = getopt(argc, argv, "pvV")) != -1) {
1195 path = _PATH_STDPATH;
1198 cmd = TYPECMD_SMALLV;
1205 error("unknown option: -%c", optopt);
1213 error("wrong number of arguments");
1214 return typecmd_impl(2, argv - 1, cmd, path);
1217 error("commandcmd bad call");
1220 * Do nothing successfully if no command was specified;
1221 * ksh also does this.
1228 * The return command.
1232 returncmd(int argc, char **argv)
1234 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1237 evalskip = SKIPFUNC;
1240 /* skip the rest of the file */
1241 evalskip = SKIPFILE;
1249 falsecmd(int argc __unused, char **argv __unused)
1256 truecmd(int argc __unused, char **argv __unused)
1263 execcmd(int argc, char **argv)
1266 * Because we have historically not supported any options,
1267 * only treat "--" specially.
1269 if (argc > 1 && strcmp(argv[1], "--") == 0)
1274 iflag = 0; /* exit on error */
1277 for (sp = cmdenviron; sp ; sp = sp->next)
1278 setvareq(sp->text, VEXPORT|VSTACK);
1279 shellexec(argv + 1, environment(), pathval(), 0);
1287 timescmd(int argc __unused, char **argv __unused)
1290 long shumins, shsmins, chumins, chsmins;
1291 double shusecs, shssecs, chusecs, chssecs;
1293 if (getrusage(RUSAGE_SELF, &ru) < 0)
1295 shumins = ru.ru_utime.tv_sec / 60;
1296 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1297 shsmins = ru.ru_stime.tv_sec / 60;
1298 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1299 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1301 chumins = ru.ru_utime.tv_sec / 60;
1302 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1303 chsmins = ru.ru_stime.tv_sec / 60;
1304 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1305 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1306 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);