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
32 * @(#)eval.c 8.9 (Berkeley) 6/8/95
33 * $FreeBSD: head/bin/sh/eval.c 247206 2013-02-23 22:50:57Z 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 MKINIT 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.
121 evalcmd(int argc, char **argv)
130 STARTSTACKSTR(concat);
134 if ((p = *ap++) == NULL)
138 STPUTC('\0', concat);
139 p = grabstackstr(concat);
141 evalstring(p, builtin_flags);
149 * Execute a command or commands contained in a string.
153 evalstring(char *s, int flags)
156 struct stackmark smark;
160 flags_exit = flags & EV_EXIT;
163 setstackmark(&smark);
164 setinputstring(s, 1);
165 while ((n = parsecmd(0)) != NEOF) {
166 if (n != NULL && !nflag) {
167 if (flags_exit && preadateof())
168 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);
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);
296 popstackmark(&smark);
297 setstackmark(&smark);
300 popstackmark(&smark);
303 if (eflag && exitstatus != 0 && do_etest)
304 exitshell(exitstatus);
311 evalloop(union node *n, int flags)
318 evaltree(n->nbinary.ch1, EV_TESTED);
320 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
324 if (evalskip == SKIPBREAK && --skipcount <= 0)
326 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
330 if (n->type == NWHILE) {
337 evaltree(n->nbinary.ch2, flags);
349 evalfor(union node *n, int flags)
351 struct arglist arglist;
356 arglist.lastp = &arglist.list;
357 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
358 oexitstatus = exitstatus;
359 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
361 *arglist.lastp = NULL;
365 for (sp = arglist.list ; sp ; sp = sp->next) {
366 setvar(n->nfor.var, sp->text, 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 arglist.lastp = &arglist.list;
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.list->text)) {
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, 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) {
475 longjmp(handler->loc, 1);
479 redirect(n->nredir.redirect, REDIR_PUSH);
482 evaltree(n->nredir.n, flags);
485 handler = savehandler;
492 exphere(union node *redir, struct arglist *fn)
494 struct jmploc jmploc;
495 struct jmploc *savehandler;
496 struct localvar *savelocalvars;
497 int need_longjmp = 0;
499 redir->nhere.expdoc = nullstr;
500 savelocalvars = localvars;
503 savehandler = handler;
504 if (setjmp(jmploc.loc))
505 need_longjmp = exception != EXERROR && exception != EXEXEC;
508 expandarg(redir->nhere.doc, fn, 0);
509 redir->nhere.expdoc = fn->list->text;
512 handler = savehandler;
515 localvars = savelocalvars;
517 longjmp(handler->loc, 1);
523 * Compute the names of the files in a redirection list.
527 expredir(union node *n)
531 for (redir = n ; redir ; redir = redir->nfile.next) {
534 switch (redir->type) {
540 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
541 redir->nfile.expfname = fn.list->text;
545 if (redir->ndup.vname) {
546 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
547 fixredir(redir, fn.list->text, 1);
560 * Evaluate a pipeline. All the processes in the pipeline are children
561 * of the process creating the pipeline. (This differs from some versions
562 * of the shell, which make the last process in a pipeline the parent
567 evalpipe(union node *n)
575 TRACE(("evalpipe(%p) called\n", (void *)n));
577 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
580 jp = makejob(n, pipelen);
582 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
589 error("Pipe call failed: %s", strerror(errno));
592 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
599 if (!(prevfd >= 0 && pip[0] == 0))
606 evaltree(lp->n, EV_EXIT);
615 if (n->npipe.backgnd == 0) {
617 exitstatus = waitforjob(jp, NULL);
618 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
627 is_valid_fast_cmdsubst(union node *n)
630 return (n->type == NCMD);
634 * Execute a command inside back quotes. If it's a builtin command, we
635 * want to save its output in a block obtained from malloc. Otherwise
636 * we fork off a subprocess and get the output of the command via a pipe.
637 * Should be called with interrupts off.
641 evalbackcmd(union node *n, struct backcmd *result)
645 struct stackmark smark;
646 struct jmploc jmploc;
647 struct jmploc *savehandler;
648 struct localvar *savelocalvars;
650 setstackmark(&smark);
659 exitstatus = oexitstatus;
660 if (is_valid_fast_cmdsubst(n)) {
661 savelocalvars = localvars;
664 savehandler = handler;
665 if (setjmp(jmploc.loc)) {
666 if (exception == EXERROR || exception == EXEXEC)
668 else if (exception != 0) {
669 handler = savehandler;
672 localvars = savelocalvars;
673 longjmp(handler->loc, 1);
677 evalcommand(n, EV_BACKCMD, result);
679 handler = savehandler;
682 localvars = savelocalvars;
685 error("Pipe call failed: %s", strerror(errno));
687 if (forkshell(jp, n, FORK_NOJOB) == 0) {
694 evaltree(n, EV_EXIT);
701 popstackmark(&smark);
702 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
703 result->fd, result->buf, result->nleft, result->jp));
707 mustexpandto(const char *argtext, const char *mask)
710 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
714 if (*argtext == CTLESC)
716 else if (BASESYNTAX[(int)*argtext] == CCTL)
718 if (*argtext != *mask)
720 if (*argtext == '\0')
728 isdeclarationcmd(struct narg *arg)
730 int have_command = 0;
734 while (mustexpandto(arg->text, "command")) {
736 arg = &arg->next->narg;
740 * To also allow "command -p" and "command --" as part of
741 * a declaration command, add code here.
742 * We do not do this, as ksh does not do it either and it
743 * is not required by POSIX.
746 return (mustexpandto(arg->text, "export") ||
747 mustexpandto(arg->text, "readonly") ||
748 (mustexpandto(arg->text, "local") &&
749 (have_command || !isfunc("local"))));
753 * Check if a builtin can safely be executed in the same process,
754 * even though it should be in a subshell (command substitution).
755 * Note that jobid, jobs, times and trap can show information not
756 * available in a child process; this is deliberate.
757 * The arguments should already have been expanded.
760 safe_builtin(int idx, int argc, char **argv)
762 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
763 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
764 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
765 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
768 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
770 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
772 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
773 argv[1][0] == '+') && argv[1][1] == 'o' &&
774 argv[1][2] == '\0'));
779 * Execute a simple command.
780 * Note: This may or may not return if (flags & EV_EXIT).
784 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
787 struct arglist arglist;
788 struct arglist varlist;
789 volatile int flags = flgs;
790 char **volatile argv;
797 struct cmdentry cmdentry;
798 struct job *volatile jp;
799 struct jmploc jmploc;
800 struct jmploc *savehandler;
801 const char *savecmdname;
802 struct shparam saveparam;
803 struct localvar *savelocalvars;
804 struct parsefile *savetopfile;
806 char *volatile lastarg;
808 volatile int do_clearcmdentry;
809 const char *path = pathval();
811 /* First expand the arguments. */
812 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
813 arglist.lastp = &arglist.list;
814 varlist.lastp = &varlist.list;
817 do_clearcmdentry = 0;
818 oexitstatus = exitstatus;
820 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
821 if (varflag && isassignment(argp->narg.text)) {
822 expandarg(argp, varflag == 1 ? &varlist : &arglist,
825 } else if (varflag == 1)
826 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
827 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
829 *arglist.lastp = NULL;
830 *varlist.lastp = NULL;
831 expredir(cmd->ncmd.redirect);
833 for (sp = arglist.list ; sp ; sp = sp->next)
835 /* Add one slot at the beginning for tryexec(). */
836 argv = stalloc(sizeof (char *) * (argc + 2));
839 for (sp = arglist.list ; sp ; sp = sp->next) {
840 TRACE(("evalcommand arg: %s\n", sp->text));
845 if (iflag && funcnest == 0 && argc > 0)
849 /* Print the command if xflag is set. */
853 ps4 = expandstr(ps4val());
854 out2str(ps4 != NULL ? ps4 : ps4val());
855 for (sp = varlist.list ; sp ; sp = sp->next) {
858 p = strchr(sp->text, '=');
861 outbin(sp->text, p - sp->text, out2);
867 for (sp = arglist.list ; sp ; sp = sp->next) {
870 /* Disambiguate command looking like assignment. */
871 if (sp == arglist.list &&
872 strchr(sp->text, '=') != NULL &&
873 strchr(sp->text, '\'') == NULL) {
885 /* Now locate the command. */
887 /* Variable assignment(s) without command */
888 cmdentry.cmdtype = CMDBUILTIN;
889 cmdentry.u.index = BLTINCMD;
890 cmdentry.special = 0;
892 static const char PATH[] = "PATH=";
893 int cmd_flags = 0, bltinonly = 0;
896 * Modify the command lookup path, if a PATH= assignment
899 for (sp = varlist.list ; sp ; sp = sp->next)
900 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
901 path = sp->text + sizeof(PATH) - 1;
903 * On `PATH=... command`, we need to make
904 * sure that the command isn't using the
905 * non-updated hash table of the outer PATH
906 * setting and we need to make sure that
907 * the hash table isn't filled with items
908 * from the temporary setting.
910 * It would be better to forbit using and
911 * updating the table while this command
912 * runs, by the command finding mechanism
913 * is heavily integrated with hash handling,
914 * so we just delete the hash before and after
915 * the command runs. Partly deleting like
916 * changepatch() does doesn't seem worth the
917 * bookinging effort, since most such runs add
918 * directories in front of the new PATH.
921 do_clearcmdentry = 1;
926 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
927 if (cmdentry.u.index < 0) {
928 cmdentry.u.index = BLTINCMD;
934 find_command(argv[0], &cmdentry, cmd_flags, path);
935 /* implement the bltin and command builtins here */
936 if (cmdentry.cmdtype != CMDBUILTIN)
938 if (cmdentry.u.index == BLTINCMD) {
944 } else if (cmdentry.u.index == COMMANDCMD) {
947 if (!strcmp(argv[1], "-p")) {
950 if (argv[2][0] == '-') {
951 if (strcmp(argv[2], "--"))
961 path = _PATH_STDPATH;
963 do_clearcmdentry = 1;
964 } else if (!strcmp(argv[1], "--")) {
969 } else if (argv[1][0] == '-')
975 cmd_flags |= DO_NOFUNC;
981 * Special builtins lose their special properties when
982 * called via 'command'.
984 if (cmd_flags & DO_NOFUNC)
985 cmdentry.special = 0;
988 /* Fork off a child process if necessary. */
989 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
990 && ((flags & EV_EXIT) == 0 || have_traps()))
991 || ((flags & EV_BACKCMD) != 0
992 && (cmdentry.cmdtype != CMDBUILTIN ||
993 !safe_builtin(cmdentry.u.index, argc, argv)))) {
994 jp = makejob(cmd, 1);
996 if (flags & EV_BACKCMD) {
999 error("Pipe call failed: %s", strerror(errno));
1001 if (cmdentry.cmdtype == CMDNORMAL &&
1002 cmd->ncmd.redirect == NULL &&
1003 varlist.list == NULL &&
1004 (mode == FORK_FG || mode == FORK_NOJOB) &&
1005 !disvforkset() && !iflag && !mflag) {
1006 vforkexecshell(jp, argv, environment(), path,
1007 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1010 if (forkshell(jp, cmd, mode) != 0)
1011 goto parent; /* at end of routine */
1012 if (flags & EV_BACKCMD) {
1019 flags &= ~EV_BACKCMD;
1024 /* This is the child process if a fork occurred. */
1025 /* Execute the command. */
1026 if (cmdentry.cmdtype == CMDFUNCTION) {
1028 trputs("Shell function: "); trargs(argv);
1030 saveparam = shellparam;
1031 shellparam.malloc = 0;
1032 shellparam.reset = 1;
1033 shellparam.nparam = argc - 1;
1034 shellparam.p = argv + 1;
1035 shellparam.optnext = NULL;
1037 savelocalvars = localvars;
1039 reffunc(cmdentry.u.func);
1040 savehandler = handler;
1041 if (setjmp(jmploc.loc)) {
1042 freeparam(&shellparam);
1043 shellparam = saveparam;
1045 unreffunc(cmdentry.u.func);
1047 localvars = savelocalvars;
1049 handler = savehandler;
1050 longjmp(handler->loc, 1);
1054 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1056 for (sp = varlist.list ; sp ; sp = sp->next)
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 == SKIPFUNC) {
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) {
1084 memout.nextc = memout.buf;
1085 memout.bufsize = 64;
1086 mode |= REDIR_BACKQ;
1088 savecmdname = commandname;
1089 savetopfile = getcurrentfile();
1090 cmdenviron = varlist.list;
1092 savehandler = handler;
1093 if (setjmp(jmploc.loc)) {
1096 exitstatus = SIGINT+128;
1097 else if (e != EXEXIT)
1102 redirect(cmd->ncmd.redirect, mode);
1103 outclearerror(out1);
1105 * If there is no command word, redirection errors should
1106 * not be fatal but assignment errors should.
1109 cmdentry.special = 1;
1110 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1113 commandname = argv[0];
1115 nextopt_optptr = NULL; /* initialize nextopt */
1116 builtin_flags = flags;
1117 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1119 if (outiserror(out1)) {
1120 warning("write error on stdout");
1121 if (exitstatus == 0 || exitstatus == 1)
1131 handler = savehandler;
1132 commandname = savecmdname;
1134 exitshell(exitstatus);
1135 if (flags == EV_BACKCMD) {
1136 backcmd->buf = memout.buf;
1137 backcmd->nleft = memout.nextc - memout.buf;
1140 if (cmdentry.u.index != EXECCMD)
1143 if ((e != EXERROR && e != EXEXEC)
1144 || cmdentry.special)
1146 popfilesupto(savetopfile);
1147 if (flags != EV_BACKCMD)
1152 trputs("normal command: "); trargs(argv);
1154 redirect(cmd->ncmd.redirect, 0);
1155 for (sp = varlist.list ; sp ; sp = sp->next)
1156 setvareq(sp->text, VEXPORT|VSTACK);
1157 envp = environment();
1158 shellexec(argv, envp, path, cmdentry.u.index);
1163 parent: /* parent process gets here (if we forked) */
1164 if (mode == FORK_FG) { /* argument to fork */
1166 exitstatus = waitforjob(jp, &realstatus);
1168 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1169 evalskip = SKIPBREAK;
1170 skipcount = loopnest;
1172 } else if (mode == FORK_NOJOB) {
1173 backcmd->fd = pip[0];
1180 setvar("_", lastarg, 0);
1181 if (do_clearcmdentry)
1188 * Search for a command. This is called before we fork so that the
1189 * location of the command will be available in the parent as well as
1190 * the child. The check for "goodname" is an overly conservative
1191 * check that the name will not be subject to expansion.
1195 prehash(union node *n)
1197 struct cmdentry entry;
1199 if (n && n->type == NCMD && n->ncmd.args)
1200 if (goodname(n->ncmd.args->narg.text))
1201 find_command(n->ncmd.args->narg.text, &entry, 0,
1208 * Builtin commands. Builtin commands whose functions are closely
1209 * tied to evaluation are implemented here.
1213 * No command given, a bltin command with no arguments, or a bltin command
1214 * with an invalid name.
1218 bltincmd(int argc, char **argv)
1221 out2fmt_flush("%s: not found\n", argv[1]);
1225 * Preserve exitstatus of a previous possible redirection
1233 * Handle break and continue commands. Break, continue, and return are
1234 * all handled by setting the evalskip flag. The evaluation routines
1235 * above all check this flag, and if it is set they start skipping
1236 * commands rather than executing them. The variable skipcount is
1237 * the number of loops to break/continue, or the number of function
1238 * levels to return. (The latter is always 1.) It should probably
1239 * be an error to break out of more loops than exist, but it isn't
1240 * in the standard shell so we don't make it one here.
1244 breakcmd(int argc, char **argv)
1246 int n = argc > 1 ? number(argv[1]) : 1;
1251 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1258 * The `command' command.
1261 commandcmd(int argc __unused, char **argv __unused)
1267 path = bltinlookup("PATH", 1);
1269 while ((ch = nextopt("pvV")) != '\0') {
1272 path = _PATH_STDPATH;
1275 cmd = TYPECMD_SMALLV;
1284 if (*argptr == NULL || argptr[1] != NULL)
1285 error("wrong number of arguments");
1286 return typecmd_impl(2, argptr - 1, cmd, path);
1288 if (*argptr != NULL)
1289 error("commandcmd bad call");
1292 * Do nothing successfully if no command was specified;
1293 * ksh also does this.
1300 * The return command.
1304 returncmd(int argc, char **argv)
1306 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1309 evalskip = SKIPFUNC;
1312 /* skip the rest of the file */
1313 evalskip = SKIPFILE;
1321 falsecmd(int argc __unused, char **argv __unused)
1328 truecmd(int argc __unused, char **argv __unused)
1335 execcmd(int argc, char **argv)
1338 * Because we have historically not supported any options,
1339 * only treat "--" specially.
1341 if (argc > 1 && strcmp(argv[1], "--") == 0)
1346 iflag = 0; /* exit on error */
1349 for (sp = cmdenviron; sp ; sp = sp->next)
1350 setvareq(sp->text, VEXPORT|VSTACK);
1351 shellexec(argv + 1, environment(), pathval(), 0);
1359 timescmd(int argc __unused, char **argv __unused)
1362 long shumins, shsmins, chumins, chsmins;
1363 double shusecs, shssecs, chusecs, chssecs;
1365 if (getrusage(RUSAGE_SELF, &ru) < 0)
1367 shumins = ru.ru_utime.tv_sec / 60;
1368 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1369 shsmins = ru.ru_stime.tv_sec / 60;
1370 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1371 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1373 chumins = ru.ru_utime.tv_sec / 60;
1374 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1375 chsmins = ru.ru_stime.tv_sec / 60;
1376 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1377 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1378 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);