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: head/bin/sh/eval.c 247206 2013-02-23 22:50:57Z jilles $
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 exphere(union node *, struct arglist *);
97 static void expredir(union node *);
98 static void evalpipe(union node *);
99 static int is_valid_fast_cmdsubst(union node *n);
100 static void evalcommand(union node *, int, struct backcmd *);
101 static void prehash(union node *);
105 * Called to reset things after an exception.
125 evalcmd(int argc, char **argv)
134 STARTSTACKSTR(concat);
138 if ((p = *ap++) == NULL)
142 STPUTC('\0', concat);
143 p = grabstackstr(concat);
145 evalstring(p, builtin_flags);
153 * Execute a command or commands contained in a string.
157 evalstring(char *s, int flags)
160 struct stackmark smark;
164 flags_exit = flags & EV_EXIT;
167 setstackmark(&smark);
168 setinputstring(s, 1);
169 while ((n = parsecmd(0)) != NEOF) {
170 if (n != NULL && !nflag) {
171 if (flags_exit && preadateof())
172 evaltree(n, flags | EV_EXIT);
177 popstackmark(&smark);
178 setstackmark(&smark);
181 popstackmark(&smark);
190 * Evaluate a parse tree. The value is left in the global variable
195 evaltree(union node *n, int flags)
199 struct stackmark smark;
201 setstackmark(&smark);
204 TRACE(("evaltree(NULL) called\n"));
211 displayhist = 1; /* show history substitutions done with fc */
213 TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
216 evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
219 next = n->nbinary.ch2;
222 evaltree(n->nbinary.ch1, EV_TESTED);
223 if (evalskip || exitstatus != 0) {
226 next = n->nbinary.ch2;
229 evaltree(n->nbinary.ch1, EV_TESTED);
230 if (evalskip || exitstatus == 0)
232 next = n->nbinary.ch2;
238 evalsubshell(n, flags);
239 do_etest = !(flags & EV_TESTED);
242 evalsubshell(n, flags);
245 evaltree(n->nif.test, EV_TESTED);
249 next = n->nif.ifpart;
250 else if (n->nif.elsepart)
251 next = n->nif.elsepart;
258 evalloop(n, flags & ~EV_EXIT);
261 evalfor(n, flags & ~EV_EXIT);
267 next = n->nclist.body;
270 if (n->nclist.body) {
271 evaltree(n->nclist.body, flags & ~EV_EXIT);
275 next = n->nclist.next;
278 defun(n->narg.text, n->narg.next);
282 evaltree(n->nnot.com, EV_TESTED);
283 exitstatus = !exitstatus;
288 do_etest = !(flags & EV_TESTED);
291 evalcommand(n, flags, NULL);
292 do_etest = !(flags & EV_TESTED);
295 out1fmt("Node type = %d\n", n->type);
300 popstackmark(&smark);
301 setstackmark(&smark);
304 popstackmark(&smark);
307 if (eflag && exitstatus != 0 && do_etest)
308 exitshell(exitstatus);
315 evalloop(union node *n, int flags)
322 evaltree(n->nbinary.ch1, EV_TESTED);
324 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
328 if (evalskip == SKIPBREAK && --skipcount <= 0)
330 if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
334 if (n->type == NWHILE) {
341 evaltree(n->nbinary.ch2, flags);
353 evalfor(union node *n, int flags)
355 struct arglist arglist;
360 arglist.lastp = &arglist.list;
361 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
362 oexitstatus = exitstatus;
363 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
365 *arglist.lastp = NULL;
369 for (sp = arglist.list ; sp ; sp = sp->next) {
370 setvar(n->nfor.var, sp->text, 0);
371 evaltree(n->nfor.body, flags);
374 if (evalskip == SKIPCONT && --skipcount <= 0) {
378 if (evalskip == SKIPBREAK && --skipcount <= 0)
389 * Evaluate a case statement, returning the selected tree.
391 * The exit status needs care to get right.
395 evalcase(union node *n)
399 struct arglist arglist;
401 arglist.lastp = &arglist.list;
402 oexitstatus = exitstatus;
403 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
404 for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
405 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
406 if (casematch(patp, arglist.list->text)) {
407 while (cp->nclist.next &&
408 cp->type == NCLISTFALLTHRU &&
409 cp->nclist.body == NULL)
410 cp = cp->nclist.next;
411 if (cp->nclist.next &&
412 cp->type == NCLISTFALLTHRU)
414 if (cp->nclist.body == NULL)
416 return (cp->nclist.body);
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 exphere(union node *redir, struct arglist *fn)
498 struct jmploc jmploc;
499 struct jmploc *savehandler;
500 struct localvar *savelocalvars;
501 int need_longjmp = 0;
503 redir->nhere.expdoc = nullstr;
504 savelocalvars = localvars;
507 savehandler = handler;
508 if (setjmp(jmploc.loc))
509 need_longjmp = exception != EXERROR && exception != EXEXEC;
512 expandarg(redir->nhere.doc, fn, 0);
513 redir->nhere.expdoc = fn->list->text;
516 handler = savehandler;
519 localvars = savelocalvars;
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 | EXP_REDIR);
545 redir->nfile.expfname = fn.list->text;
549 if (redir->ndup.vname) {
550 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
551 fixredir(redir, fn.list->text, 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, 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;
654 setstackmark(&smark);
663 exitstatus = oexitstatus;
664 if (is_valid_fast_cmdsubst(n)) {
665 savelocalvars = localvars;
668 savehandler = handler;
669 if (setjmp(jmploc.loc)) {
670 if (exception == EXERROR || exception == EXEXEC)
672 else if (exception != 0) {
673 handler = savehandler;
676 localvars = savelocalvars;
677 longjmp(handler->loc, 1);
681 evalcommand(n, EV_BACKCMD, result);
683 handler = savehandler;
686 localvars = savelocalvars;
689 error("Pipe call failed: %s", strerror(errno));
691 if (forkshell(jp, n, FORK_NOJOB) == 0) {
698 evaltree(n, EV_EXIT);
705 popstackmark(&smark);
706 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
707 result->fd, result->buf, result->nleft, result->jp));
711 mustexpandto(const char *argtext, const char *mask)
714 if (*argtext == CTLQUOTEMARK || *argtext == CTLQUOTEEND) {
718 if (*argtext == CTLESC)
720 else if (BASESYNTAX[(int)*argtext] == CCTL)
722 if (*argtext != *mask)
724 if (*argtext == '\0')
732 isdeclarationcmd(struct narg *arg)
734 int have_command = 0;
738 while (mustexpandto(arg->text, "command")) {
740 arg = &arg->next->narg;
744 * To also allow "command -p" and "command --" as part of
745 * a declaration command, add code here.
746 * We do not do this, as ksh does not do it either and it
747 * is not required by POSIX.
750 return (mustexpandto(arg->text, "export") ||
751 mustexpandto(arg->text, "readonly") ||
752 (mustexpandto(arg->text, "local") &&
753 (have_command || !isfunc("local"))));
757 * Check if a builtin can safely be executed in the same process,
758 * even though it should be in a subshell (command substitution).
759 * Note that jobid, jobs, times and trap can show information not
760 * available in a child process; this is deliberate.
761 * The arguments should already have been expanded.
764 safe_builtin(int idx, int argc, char **argv)
766 if (idx == BLTINCMD || idx == COMMANDCMD || idx == ECHOCMD ||
767 idx == FALSECMD || idx == JOBIDCMD || idx == JOBSCMD ||
768 idx == KILLCMD || idx == PRINTFCMD || idx == PWDCMD ||
769 idx == TESTCMD || idx == TIMESCMD || idx == TRUECMD ||
772 if (idx == EXPORTCMD || idx == TRAPCMD || idx == ULIMITCMD ||
774 return (argc <= 1 || (argc == 2 && argv[1][0] == '-'));
776 return (argc <= 1 || (argc == 2 && (argv[1][0] == '-' ||
777 argv[1][0] == '+') && argv[1][1] == 'o' &&
778 argv[1][2] == '\0'));
783 * Execute a simple command.
784 * Note: This may or may not return if (flags & EV_EXIT).
788 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
791 struct arglist arglist;
792 struct arglist varlist;
793 volatile int flags = flgs;
794 char **volatile argv;
801 struct cmdentry cmdentry;
802 struct job *volatile jp;
803 struct jmploc jmploc;
804 struct jmploc *savehandler;
805 const char *savecmdname;
806 struct shparam saveparam;
807 struct localvar *savelocalvars;
808 struct parsefile *savetopfile;
810 char *volatile lastarg;
812 volatile int do_clearcmdentry;
813 const char *path = pathval();
815 /* First expand the arguments. */
816 TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
817 arglist.lastp = &arglist.list;
818 varlist.lastp = &varlist.list;
821 do_clearcmdentry = 0;
822 oexitstatus = exitstatus;
824 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
825 if (varflag && isassignment(argp->narg.text)) {
826 expandarg(argp, varflag == 1 ? &varlist : &arglist,
829 } else if (varflag == 1)
830 varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
831 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
833 *arglist.lastp = NULL;
834 *varlist.lastp = NULL;
835 expredir(cmd->ncmd.redirect);
837 for (sp = arglist.list ; sp ; sp = sp->next)
839 /* Add one slot at the beginning for tryexec(). */
840 argv = stalloc(sizeof (char *) * (argc + 2));
843 for (sp = arglist.list ; sp ; sp = sp->next) {
844 TRACE(("evalcommand arg: %s\n", sp->text));
849 if (iflag && funcnest == 0 && argc > 0)
853 /* Print the command if xflag is set. */
857 ps4 = expandstr(ps4val());
858 out2str(ps4 != NULL ? ps4 : ps4val());
859 for (sp = varlist.list ; sp ; sp = sp->next) {
862 p = strchr(sp->text, '=');
865 outbin(sp->text, p - sp->text, out2);
871 for (sp = arglist.list ; sp ; sp = sp->next) {
874 /* Disambiguate command looking like assignment. */
875 if (sp == arglist.list &&
876 strchr(sp->text, '=') != NULL &&
877 strchr(sp->text, '\'') == NULL) {
889 /* Now locate the command. */
891 /* Variable assignment(s) without command */
892 cmdentry.cmdtype = CMDBUILTIN;
893 cmdentry.u.index = BLTINCMD;
894 cmdentry.special = 0;
896 static const char PATH[] = "PATH=";
897 int cmd_flags = 0, bltinonly = 0;
900 * Modify the command lookup path, if a PATH= assignment
903 for (sp = varlist.list ; sp ; sp = sp->next)
904 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
905 path = sp->text + sizeof(PATH) - 1;
907 * On `PATH=... command`, we need to make
908 * sure that the command isn't using the
909 * non-updated hash table of the outer PATH
910 * setting and we need to make sure that
911 * the hash table isn't filled with items
912 * from the temporary setting.
914 * It would be better to forbit using and
915 * updating the table while this command
916 * runs, by the command finding mechanism
917 * is heavily integrated with hash handling,
918 * so we just delete the hash before and after
919 * the command runs. Partly deleting like
920 * changepatch() does doesn't seem worth the
921 * bookinging effort, since most such runs add
922 * directories in front of the new PATH.
925 do_clearcmdentry = 1;
930 cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
931 if (cmdentry.u.index < 0) {
932 cmdentry.u.index = BLTINCMD;
938 find_command(argv[0], &cmdentry, cmd_flags, path);
939 /* implement the bltin and command builtins here */
940 if (cmdentry.cmdtype != CMDBUILTIN)
942 if (cmdentry.u.index == BLTINCMD) {
948 } else if (cmdentry.u.index == COMMANDCMD) {
951 if (!strcmp(argv[1], "-p")) {
954 if (argv[2][0] == '-') {
955 if (strcmp(argv[2], "--"))
965 path = _PATH_STDPATH;
967 do_clearcmdentry = 1;
968 } else if (!strcmp(argv[1], "--")) {
973 } else if (argv[1][0] == '-')
979 cmd_flags |= DO_NOFUNC;
985 * Special builtins lose their special properties when
986 * called via 'command'.
988 if (cmd_flags & DO_NOFUNC)
989 cmdentry.special = 0;
992 /* Fork off a child process if necessary. */
993 if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
994 && ((flags & EV_EXIT) == 0 || have_traps()))
995 || ((flags & EV_BACKCMD) != 0
996 && (cmdentry.cmdtype != CMDBUILTIN ||
997 !safe_builtin(cmdentry.u.index, argc, argv)))) {
998 jp = makejob(cmd, 1);
1000 if (flags & EV_BACKCMD) {
1003 error("Pipe call failed: %s", strerror(errno));
1005 if (cmdentry.cmdtype == CMDNORMAL &&
1006 cmd->ncmd.redirect == NULL &&
1007 varlist.list == NULL &&
1008 (mode == FORK_FG || mode == FORK_NOJOB) &&
1009 !disvforkset() && !iflag && !mflag) {
1010 vforkexecshell(jp, argv, environment(), path,
1011 cmdentry.u.index, flags & EV_BACKCMD ? pip : NULL);
1014 if (forkshell(jp, cmd, mode) != 0)
1015 goto parent; /* at end of routine */
1016 if (flags & EV_BACKCMD) {
1023 flags &= ~EV_BACKCMD;
1028 /* This is the child process if a fork occurred. */
1029 /* Execute the command. */
1030 if (cmdentry.cmdtype == CMDFUNCTION) {
1032 trputs("Shell function: "); trargs(argv);
1034 saveparam = shellparam;
1035 shellparam.malloc = 0;
1036 shellparam.reset = 1;
1037 shellparam.nparam = argc - 1;
1038 shellparam.p = argv + 1;
1039 shellparam.optnext = NULL;
1041 savelocalvars = localvars;
1043 reffunc(cmdentry.u.func);
1044 savehandler = handler;
1045 if (setjmp(jmploc.loc)) {
1046 freeparam(&shellparam);
1047 shellparam = saveparam;
1049 unreffunc(cmdentry.u.func);
1051 localvars = savelocalvars;
1053 handler = savehandler;
1054 longjmp(handler->loc, 1);
1058 redirect(cmd->ncmd.redirect, REDIR_PUSH);
1060 for (sp = varlist.list ; sp ; sp = sp->next)
1062 exitstatus = oexitstatus;
1063 evaltree(getfuncnode(cmdentry.u.func),
1064 flags & (EV_TESTED | EV_EXIT));
1066 unreffunc(cmdentry.u.func);
1068 localvars = savelocalvars;
1069 freeparam(&shellparam);
1070 shellparam = saveparam;
1071 handler = savehandler;
1075 if (evalskip == SKIPFUNC) {
1080 exitshell(exitstatus);
1081 } else if (cmdentry.cmdtype == CMDBUILTIN) {
1083 trputs("builtin command: "); trargs(argv);
1085 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
1086 if (flags == EV_BACKCMD) {
1088 memout.nextc = memout.buf;
1089 memout.bufsize = 64;
1090 mode |= REDIR_BACKQ;
1092 savecmdname = commandname;
1093 savetopfile = getcurrentfile();
1094 cmdenviron = varlist.list;
1096 savehandler = handler;
1097 if (setjmp(jmploc.loc)) {
1100 exitstatus = SIGINT+128;
1101 else if (e != EXEXIT)
1106 redirect(cmd->ncmd.redirect, mode);
1107 outclearerror(out1);
1109 * If there is no command word, redirection errors should
1110 * not be fatal but assignment errors should.
1113 cmdentry.special = 1;
1114 listsetvar(cmdenviron, cmdentry.special ? 0 : VNOSET);
1117 commandname = argv[0];
1119 nextopt_optptr = NULL; /* initialize nextopt */
1120 builtin_flags = flags;
1121 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
1123 if (outiserror(out1)) {
1124 warning("write error on stdout");
1125 if (exitstatus == 0 || exitstatus == 1)
1135 handler = savehandler;
1136 commandname = savecmdname;
1138 exitshell(exitstatus);
1139 if (flags == EV_BACKCMD) {
1140 backcmd->buf = memout.buf;
1141 backcmd->nleft = memout.nextc - memout.buf;
1144 if (cmdentry.u.index != EXECCMD)
1147 if ((e != EXERROR && e != EXEXEC)
1148 || cmdentry.special)
1150 popfilesupto(savetopfile);
1151 if (flags != EV_BACKCMD)
1156 trputs("normal command: "); trargs(argv);
1158 redirect(cmd->ncmd.redirect, 0);
1159 for (sp = varlist.list ; sp ; sp = sp->next)
1160 setvareq(sp->text, VEXPORT|VSTACK);
1161 envp = environment();
1162 shellexec(argv, envp, path, cmdentry.u.index);
1167 parent: /* parent process gets here (if we forked) */
1168 if (mode == FORK_FG) { /* argument to fork */
1170 exitstatus = waitforjob(jp, &realstatus);
1172 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
1173 evalskip = SKIPBREAK;
1174 skipcount = loopnest;
1176 } else if (mode == FORK_NOJOB) {
1177 backcmd->fd = pip[0];
1184 setvar("_", lastarg, 0);
1185 if (do_clearcmdentry)
1192 * Search for a command. This is called before we fork so that the
1193 * location of the command will be available in the parent as well as
1194 * the child. The check for "goodname" is an overly conservative
1195 * check that the name will not be subject to expansion.
1199 prehash(union node *n)
1201 struct cmdentry entry;
1203 if (n && n->type == NCMD && n->ncmd.args)
1204 if (goodname(n->ncmd.args->narg.text))
1205 find_command(n->ncmd.args->narg.text, &entry, 0,
1212 * Builtin commands. Builtin commands whose functions are closely
1213 * tied to evaluation are implemented here.
1217 * No command given, a bltin command with no arguments, or a bltin command
1218 * with an invalid name.
1222 bltincmd(int argc, char **argv)
1225 out2fmt_flush("%s: not found\n", argv[1]);
1229 * Preserve exitstatus of a previous possible redirection
1237 * Handle break and continue commands. Break, continue, and return are
1238 * all handled by setting the evalskip flag. The evaluation routines
1239 * above all check this flag, and if it is set they start skipping
1240 * commands rather than executing them. The variable skipcount is
1241 * the number of loops to break/continue, or the number of function
1242 * levels to return. (The latter is always 1.) It should probably
1243 * be an error to break out of more loops than exist, but it isn't
1244 * in the standard shell so we don't make it one here.
1248 breakcmd(int argc, char **argv)
1250 int n = argc > 1 ? number(argv[1]) : 1;
1255 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1262 * The `command' command.
1265 commandcmd(int argc __unused, char **argv __unused)
1271 path = bltinlookup("PATH", 1);
1273 while ((ch = nextopt("pvV")) != '\0') {
1276 path = _PATH_STDPATH;
1279 cmd = TYPECMD_SMALLV;
1288 if (*argptr == NULL || argptr[1] != NULL)
1289 error("wrong number of arguments");
1290 return typecmd_impl(2, argptr - 1, cmd, path);
1292 if (*argptr != NULL)
1293 error("commandcmd bad call");
1296 * Do nothing successfully if no command was specified;
1297 * ksh also does this.
1304 * The return command.
1308 returncmd(int argc, char **argv)
1310 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1313 evalskip = SKIPFUNC;
1316 /* skip the rest of the file */
1317 evalskip = SKIPFILE;
1325 falsecmd(int argc __unused, char **argv __unused)
1332 truecmd(int argc __unused, char **argv __unused)
1339 execcmd(int argc, char **argv)
1342 * Because we have historically not supported any options,
1343 * only treat "--" specially.
1345 if (argc > 1 && strcmp(argv[1], "--") == 0)
1350 iflag = 0; /* exit on error */
1353 for (sp = cmdenviron; sp ; sp = sp->next)
1354 setvareq(sp->text, VEXPORT|VSTACK);
1355 shellexec(argv + 1, environment(), pathval(), 0);
1363 timescmd(int argc __unused, char **argv __unused)
1366 long shumins, shsmins, chumins, chsmins;
1367 double shusecs, shssecs, chusecs, chssecs;
1369 if (getrusage(RUSAGE_SELF, &ru) < 0)
1371 shumins = ru.ru_utime.tv_sec / 60;
1372 shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1373 shsmins = ru.ru_stime.tv_sec / 60;
1374 shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1375 if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1377 chumins = ru.ru_utime.tv_sec / 60;
1378 chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1379 chsmins = ru.ru_stime.tv_sec / 60;
1380 chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1381 out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1382 shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);