eval.c revision 194128
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/eval.c 194128 2009-06-13 21:17:45Z jilles $");
401556Srgrimes
41100437Stjr#include <paths.h>
4217987Speter#include <signal.h>
43102576Skeramida#include <stdlib.h>
4417987Speter#include <unistd.h>
45153091Sstefanf#include <sys/resource.h>
4645266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */
4753891Scracauer#include <errno.h>
4817987Speter
491556Srgrimes/*
501556Srgrimes * Evaluate a command.
511556Srgrimes */
521556Srgrimes
531556Srgrimes#include "shell.h"
541556Srgrimes#include "nodes.h"
551556Srgrimes#include "syntax.h"
561556Srgrimes#include "expand.h"
571556Srgrimes#include "parser.h"
581556Srgrimes#include "jobs.h"
591556Srgrimes#include "eval.h"
601556Srgrimes#include "builtins.h"
611556Srgrimes#include "options.h"
621556Srgrimes#include "exec.h"
631556Srgrimes#include "redir.h"
641556Srgrimes#include "input.h"
651556Srgrimes#include "output.h"
661556Srgrimes#include "trap.h"
671556Srgrimes#include "var.h"
681556Srgrimes#include "memalloc.h"
691556Srgrimes#include "error.h"
7017987Speter#include "show.h"
711556Srgrimes#include "mystring.h"
7217987Speter#ifndef NO_HISTORY
731556Srgrimes#include "myhistedit.h"
7417987Speter#endif
751556Srgrimes
761556Srgrimes
771556SrgrimesMKINIT int evalskip;		/* set if we are skipping commands */
781556SrgrimesSTATIC int skipcount;		/* number of levels to skip */
791556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
801556Srgrimesint funcnest;			/* depth of function calls */
81193169SstefanfSTATIC int builtin_flags;	/* evalcommand flags for builtins */
821556Srgrimes
831556Srgrimes
841556Srgrimeschar *commandname;
851556Srgrimesstruct strlist *cmdenviron;
861556Srgrimesint exitstatus;			/* exit status of last command */
8717987Speterint oexitstatus;		/* saved exit status */
881556Srgrimes
891556Srgrimes
90149933SstefanfSTATIC void evalloop(union node *, int);
91149933SstefanfSTATIC void evalfor(union node *, int);
9290111SimpSTATIC void evalcase(union node *, int);
9390111SimpSTATIC void evalsubshell(union node *, int);
9490111SimpSTATIC void expredir(union node *);
9590111SimpSTATIC void evalpipe(union node *);
9690111SimpSTATIC void evalcommand(union node *, int, struct backcmd *);
9790111SimpSTATIC void prehash(union node *);
981556Srgrimes
991556Srgrimes
1001556Srgrimes/*
1011556Srgrimes * Called to reset things after an exception.
1021556Srgrimes */
1031556Srgrimes
1041556Srgrimes#ifdef mkinit
1051556SrgrimesINCLUDE "eval.h"
1061556Srgrimes
1071556SrgrimesRESET {
1081556Srgrimes	evalskip = 0;
1091556Srgrimes	loopnest = 0;
1101556Srgrimes	funcnest = 0;
1111556Srgrimes}
1121556Srgrimes
1131556SrgrimesSHELLPROC {
1141556Srgrimes	exitstatus = 0;
1151556Srgrimes}
1161556Srgrimes#endif
1171556Srgrimes
1181556Srgrimes
1191556Srgrimes
1201556Srgrimes/*
12146684Skris * The eval command.
1221556Srgrimes */
1231556Srgrimes
12417987Speterint
12590111Simpevalcmd(int argc, char **argv)
1261556Srgrimes{
1271556Srgrimes        char *p;
1281556Srgrimes        char *concat;
1291556Srgrimes        char **ap;
1301556Srgrimes
1311556Srgrimes        if (argc > 1) {
1321556Srgrimes                p = argv[1];
1331556Srgrimes                if (argc > 2) {
1341556Srgrimes                        STARTSTACKSTR(concat);
1351556Srgrimes                        ap = argv + 2;
1361556Srgrimes                        for (;;) {
1371556Srgrimes                                while (*p)
1381556Srgrimes                                        STPUTC(*p++, concat);
1391556Srgrimes                                if ((p = *ap++) == NULL)
1401556Srgrimes                                        break;
1411556Srgrimes                                STPUTC(' ', concat);
1421556Srgrimes                        }
1431556Srgrimes                        STPUTC('\0', concat);
1441556Srgrimes                        p = grabstackstr(concat);
1451556Srgrimes                }
146193169Sstefanf                evalstring(p, builtin_flags & EV_TESTED);
1471556Srgrimes        }
1481556Srgrimes        return exitstatus;
1491556Srgrimes}
1501556Srgrimes
1511556Srgrimes
1521556Srgrimes/*
1531556Srgrimes * Execute a command or commands contained in a string.
1541556Srgrimes */
1551556Srgrimes
1561556Srgrimesvoid
157193169Sstefanfevalstring(char *s, int flags)
15890111Simp{
1591556Srgrimes	union node *n;
1601556Srgrimes	struct stackmark smark;
161194128Sjilles	int flags_exit;
1621556Srgrimes
163194128Sjilles	flags_exit = flags & EV_EXIT;
164194128Sjilles	flags &= ~EV_EXIT;
1651556Srgrimes	setstackmark(&smark);
1661556Srgrimes	setinputstring(s, 1);
1671556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
168194128Sjilles		if (n != NULL) {
169194128Sjilles			if (flags_exit && preadateof())
170194128Sjilles				evaltree(n, flags | EV_EXIT);
171194128Sjilles			else
172194128Sjilles				evaltree(n, flags);
173194128Sjilles		}
1741556Srgrimes		popstackmark(&smark);
1751556Srgrimes	}
1761556Srgrimes	popfile();
1771556Srgrimes	popstackmark(&smark);
178194128Sjilles	if (flags_exit)
179194128Sjilles		exitshell(exitstatus);
1801556Srgrimes}
1811556Srgrimes
1821556Srgrimes
1831556Srgrimes/*
1841556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1851556Srgrimes * exitstatus.
1861556Srgrimes */
1871556Srgrimes
1881556Srgrimesvoid
18990111Simpevaltree(union node *n, int flags)
19017987Speter{
191149927Sstefanf	int do_etest;
192149927Sstefanf
193149927Sstefanf	do_etest = 0;
1941556Srgrimes	if (n == NULL) {
1951556Srgrimes		TRACE(("evaltree(NULL) called\n"));
1961556Srgrimes		exitstatus = 0;
1971556Srgrimes		goto out;
1981556Srgrimes	}
19917987Speter#ifndef NO_HISTORY
2001556Srgrimes	displayhist = 1;	/* show history substitutions done with fc */
20117987Speter#endif
202149802Sstefanf	TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
2031556Srgrimes	switch (n->type) {
2041556Srgrimes	case NSEMI:
205149932Sstefanf		evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
2061556Srgrimes		if (evalskip)
2071556Srgrimes			goto out;
2081556Srgrimes		evaltree(n->nbinary.ch2, flags);
2091556Srgrimes		break;
2101556Srgrimes	case NAND:
2111556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
21218754Ssteve		if (evalskip || exitstatus != 0) {
2131556Srgrimes			goto out;
21418754Ssteve		}
2151556Srgrimes		evaltree(n->nbinary.ch2, flags);
2161556Srgrimes		break;
2171556Srgrimes	case NOR:
2181556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2191556Srgrimes		if (evalskip || exitstatus == 0)
2201556Srgrimes			goto out;
2211556Srgrimes		evaltree(n->nbinary.ch2, flags);
2221556Srgrimes		break;
2231556Srgrimes	case NREDIR:
2241556Srgrimes		expredir(n->nredir.redirect);
2251556Srgrimes		redirect(n->nredir.redirect, REDIR_PUSH);
2261556Srgrimes		evaltree(n->nredir.n, flags);
2271556Srgrimes		popredir();
2281556Srgrimes		break;
2291556Srgrimes	case NSUBSHELL:
2301556Srgrimes		evalsubshell(n, flags);
231149927Sstefanf		do_etest = !(flags & EV_TESTED);
2321556Srgrimes		break;
2331556Srgrimes	case NBACKGND:
2341556Srgrimes		evalsubshell(n, flags);
2351556Srgrimes		break;
2361556Srgrimes	case NIF: {
2371556Srgrimes		evaltree(n->nif.test, EV_TESTED);
2381556Srgrimes		if (evalskip)
2391556Srgrimes			goto out;
24020425Ssteve		if (exitstatus == 0)
2411556Srgrimes			evaltree(n->nif.ifpart, flags);
24217987Speter		else if (n->nif.elsepart)
2431556Srgrimes			evaltree(n->nif.elsepart, flags);
24420425Ssteve		else
24520425Ssteve			exitstatus = 0;
2461556Srgrimes		break;
2471556Srgrimes	}
2481556Srgrimes	case NWHILE:
2491556Srgrimes	case NUNTIL:
250149933Sstefanf		evalloop(n, flags & ~EV_EXIT);
2511556Srgrimes		break;
2521556Srgrimes	case NFOR:
253149933Sstefanf		evalfor(n, flags & ~EV_EXIT);
2541556Srgrimes		break;
2551556Srgrimes	case NCASE:
2561556Srgrimes		evalcase(n, flags);
2571556Srgrimes		break;
2581556Srgrimes	case NDEFUN:
2591556Srgrimes		defun(n->narg.text, n->narg.next);
2601556Srgrimes		exitstatus = 0;
2611556Srgrimes		break;
2621556Srgrimes	case NNOT:
2631556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2641556Srgrimes		exitstatus = !exitstatus;
2651556Srgrimes		break;
2661556Srgrimes
2671556Srgrimes	case NPIPE:
2681556Srgrimes		evalpipe(n);
269149927Sstefanf		do_etest = !(flags & EV_TESTED);
2701556Srgrimes		break;
2711556Srgrimes	case NCMD:
2721556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
273149927Sstefanf		do_etest = !(flags & EV_TESTED);
2741556Srgrimes		break;
2751556Srgrimes	default:
2761556Srgrimes		out1fmt("Node type = %d\n", n->type);
2771556Srgrimes		flushout(&output);
2781556Srgrimes		break;
2791556Srgrimes	}
2801556Srgrimesout:
2811556Srgrimes	if (pendingsigs)
2821556Srgrimes		dotrap();
283149927Sstefanf	if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest))
2841556Srgrimes		exitshell(exitstatus);
2851556Srgrimes}
2861556Srgrimes
2871556Srgrimes
2881556SrgrimesSTATIC void
289149933Sstefanfevalloop(union node *n, int flags)
29017987Speter{
2911556Srgrimes	int status;
2921556Srgrimes
2931556Srgrimes	loopnest++;
2941556Srgrimes	status = 0;
2951556Srgrimes	for (;;) {
2961556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2971556Srgrimes		if (evalskip) {
2981556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
2991556Srgrimes				evalskip = 0;
3001556Srgrimes				continue;
3011556Srgrimes			}
3021556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3031556Srgrimes				evalskip = 0;
3041556Srgrimes			break;
3051556Srgrimes		}
3061556Srgrimes		if (n->type == NWHILE) {
3071556Srgrimes			if (exitstatus != 0)
3081556Srgrimes				break;
3091556Srgrimes		} else {
3101556Srgrimes			if (exitstatus == 0)
3111556Srgrimes				break;
3121556Srgrimes		}
313149933Sstefanf		evaltree(n->nbinary.ch2, flags);
3141556Srgrimes		status = exitstatus;
3151556Srgrimes		if (evalskip)
3161556Srgrimes			goto skipping;
3171556Srgrimes	}
3181556Srgrimes	loopnest--;
3191556Srgrimes	exitstatus = status;
3201556Srgrimes}
3211556Srgrimes
3221556Srgrimes
3231556Srgrimes
3241556SrgrimesSTATIC void
325149933Sstefanfevalfor(union node *n, int flags)
32617987Speter{
3271556Srgrimes	struct arglist arglist;
3281556Srgrimes	union node *argp;
3291556Srgrimes	struct strlist *sp;
3301556Srgrimes	struct stackmark smark;
3311556Srgrimes
3321556Srgrimes	setstackmark(&smark);
3331556Srgrimes	arglist.lastp = &arglist.list;
3341556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
33517987Speter		oexitstatus = exitstatus;
3361556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3371556Srgrimes		if (evalskip)
3381556Srgrimes			goto out;
3391556Srgrimes	}
3401556Srgrimes	*arglist.lastp = NULL;
3411556Srgrimes
3421556Srgrimes	exitstatus = 0;
3431556Srgrimes	loopnest++;
3441556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3451556Srgrimes		setvar(n->nfor.var, sp->text, 0);
346149933Sstefanf		evaltree(n->nfor.body, flags);
3471556Srgrimes		if (evalskip) {
3481556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3491556Srgrimes				evalskip = 0;
3501556Srgrimes				continue;
3511556Srgrimes			}
3521556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3531556Srgrimes				evalskip = 0;
3541556Srgrimes			break;
3551556Srgrimes		}
3561556Srgrimes	}
3571556Srgrimes	loopnest--;
3581556Srgrimesout:
3591556Srgrimes	popstackmark(&smark);
3601556Srgrimes}
3611556Srgrimes
3621556Srgrimes
3631556Srgrimes
3641556SrgrimesSTATIC void
36590111Simpevalcase(union node *n, int flags)
36617987Speter{
3671556Srgrimes	union node *cp;
3681556Srgrimes	union node *patp;
3691556Srgrimes	struct arglist arglist;
3701556Srgrimes	struct stackmark smark;
3711556Srgrimes
3721556Srgrimes	setstackmark(&smark);
3731556Srgrimes	arglist.lastp = &arglist.list;
37417987Speter	oexitstatus = exitstatus;
375172440Sstefanf	exitstatus = 0;
3761556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3771556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3781556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3791556Srgrimes			if (casematch(patp, arglist.list->text)) {
3801556Srgrimes				if (evalskip == 0) {
3811556Srgrimes					evaltree(cp->nclist.body, flags);
3821556Srgrimes				}
3831556Srgrimes				goto out;
3841556Srgrimes			}
3851556Srgrimes		}
3861556Srgrimes	}
3871556Srgrimesout:
3881556Srgrimes	popstackmark(&smark);
3891556Srgrimes}
3901556Srgrimes
3911556Srgrimes
3921556Srgrimes
3931556Srgrimes/*
3941556Srgrimes * Kick off a subshell to evaluate a tree.
3951556Srgrimes */
3961556Srgrimes
3971556SrgrimesSTATIC void
39890111Simpevalsubshell(union node *n, int flags)
39917987Speter{
4001556Srgrimes	struct job *jp;
4011556Srgrimes	int backgnd = (n->type == NBACKGND);
4021556Srgrimes
4031556Srgrimes	expredir(n->nredir.redirect);
4041556Srgrimes	jp = makejob(n, 1);
4051556Srgrimes	if (forkshell(jp, n, backgnd) == 0) {
4061556Srgrimes		if (backgnd)
4071556Srgrimes			flags &=~ EV_TESTED;
4081556Srgrimes		redirect(n->nredir.redirect, 0);
4091556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
4101556Srgrimes	}
4111556Srgrimes	if (! backgnd) {
4121556Srgrimes		INTOFF;
41345916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
4141556Srgrimes		INTON;
4151556Srgrimes	}
4161556Srgrimes}
4171556Srgrimes
4181556Srgrimes
4191556Srgrimes
4201556Srgrimes/*
4211556Srgrimes * Compute the names of the files in a redirection list.
4221556Srgrimes */
4231556Srgrimes
4241556SrgrimesSTATIC void
42590111Simpexpredir(union node *n)
42617987Speter{
42725222Ssteve	union node *redir;
4281556Srgrimes
4291556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
43017987Speter		struct arglist fn;
43117987Speter		fn.lastp = &fn.list;
43217987Speter		oexitstatus = exitstatus;
43317987Speter		switch (redir->type) {
43417987Speter		case NFROM:
43517987Speter		case NTO:
43666612Sbrian		case NFROMTO:
43717987Speter		case NAPPEND:
43896922Stjr		case NCLOBBER:
4391556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4401556Srgrimes			redir->nfile.expfname = fn.list->text;
44117987Speter			break;
44217987Speter		case NFROMFD:
44317987Speter		case NTOFD:
44417987Speter			if (redir->ndup.vname) {
445181017Sstefanf				expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
44617987Speter				fixredir(redir, fn.list->text, 1);
44717987Speter			}
44817987Speter			break;
4491556Srgrimes		}
4501556Srgrimes	}
4511556Srgrimes}
4521556Srgrimes
4531556Srgrimes
4541556Srgrimes
4551556Srgrimes/*
4561556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
4571556Srgrimes * of the process creating the pipeline.  (This differs from some versions
4581556Srgrimes * of the shell, which make the last process in a pipeline the parent
4591556Srgrimes * of all the rest.)
4601556Srgrimes */
4611556Srgrimes
4621556SrgrimesSTATIC void
46390111Simpevalpipe(union node *n)
46417987Speter{
4651556Srgrimes	struct job *jp;
4661556Srgrimes	struct nodelist *lp;
4671556Srgrimes	int pipelen;
4681556Srgrimes	int prevfd;
4691556Srgrimes	int pip[2];
4701556Srgrimes
471149802Sstefanf	TRACE(("evalpipe(%p) called\n", (void *)n));
4721556Srgrimes	pipelen = 0;
4731556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
4741556Srgrimes		pipelen++;
4751556Srgrimes	INTOFF;
4761556Srgrimes	jp = makejob(n, pipelen);
4771556Srgrimes	prevfd = -1;
4781556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
4791556Srgrimes		prehash(lp->n);
4801556Srgrimes		pip[1] = -1;
4811556Srgrimes		if (lp->next) {
4821556Srgrimes			if (pipe(pip) < 0) {
4831556Srgrimes				close(prevfd);
48453891Scracauer				error("Pipe call failed: %s", strerror(errno));
4851556Srgrimes			}
4861556Srgrimes		}
4871556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
4881556Srgrimes			INTON;
4891556Srgrimes			if (prevfd > 0) {
490124780Sdes				dup2(prevfd, 0);
4911556Srgrimes				close(prevfd);
4921556Srgrimes			}
4931556Srgrimes			if (pip[1] >= 0) {
49453282Scracauer				if (!(prevfd >= 0 && pip[0] == 0))
49552900Scracauer					close(pip[0]);
4961556Srgrimes				if (pip[1] != 1) {
497124780Sdes					dup2(pip[1], 1);
4981556Srgrimes					close(pip[1]);
4991556Srgrimes				}
5001556Srgrimes			}
5011556Srgrimes			evaltree(lp->n, EV_EXIT);
5021556Srgrimes		}
5031556Srgrimes		if (prevfd >= 0)
5041556Srgrimes			close(prevfd);
5051556Srgrimes		prevfd = pip[0];
5061556Srgrimes		close(pip[1]);
5071556Srgrimes	}
5081556Srgrimes	INTON;
5091556Srgrimes	if (n->npipe.backgnd == 0) {
5101556Srgrimes		INTOFF;
51145916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
5121556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5131556Srgrimes		INTON;
5141556Srgrimes	}
5151556Srgrimes}
5161556Srgrimes
5171556Srgrimes
5181556Srgrimes
5191556Srgrimes/*
5201556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5211556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5221556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5231556Srgrimes * Should be called with interrupts off.
5241556Srgrimes */
5251556Srgrimes
5261556Srgrimesvoid
52790111Simpevalbackcmd(union node *n, struct backcmd *result)
52817987Speter{
5291556Srgrimes	int pip[2];
5301556Srgrimes	struct job *jp;
5311556Srgrimes	struct stackmark smark;		/* unnecessary */
5321556Srgrimes
5331556Srgrimes	setstackmark(&smark);
5341556Srgrimes	result->fd = -1;
5351556Srgrimes	result->buf = NULL;
5361556Srgrimes	result->nleft = 0;
5371556Srgrimes	result->jp = NULL;
53817987Speter	if (n == NULL) {
53917987Speter		exitstatus = 0;
5401556Srgrimes		goto out;
54117987Speter	}
5421556Srgrimes	if (n->type == NCMD) {
54317987Speter		exitstatus = oexitstatus;
5441556Srgrimes		evalcommand(n, EV_BACKCMD, result);
5451556Srgrimes	} else {
54617987Speter		exitstatus = 0;
5471556Srgrimes		if (pipe(pip) < 0)
54853891Scracauer			error("Pipe call failed: %s", strerror(errno));
5491556Srgrimes		jp = makejob(n, 1);
5501556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
5511556Srgrimes			FORCEINTON;
5521556Srgrimes			close(pip[0]);
5531556Srgrimes			if (pip[1] != 1) {
554124780Sdes				dup2(pip[1], 1);
5551556Srgrimes				close(pip[1]);
5561556Srgrimes			}
5571556Srgrimes			evaltree(n, EV_EXIT);
5581556Srgrimes		}
5591556Srgrimes		close(pip[1]);
5601556Srgrimes		result->fd = pip[0];
5611556Srgrimes		result->jp = jp;
5621556Srgrimes	}
5631556Srgrimesout:
5641556Srgrimes	popstackmark(&smark);
565109627Stjr	TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
5661556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
5671556Srgrimes}
5681556Srgrimes
5691556Srgrimes
5701556Srgrimes
5711556Srgrimes/*
5721556Srgrimes * Execute a simple command.
5731556Srgrimes */
5741556Srgrimes
5751556SrgrimesSTATIC void
57690111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd)
57717987Speter{
5781556Srgrimes	struct stackmark smark;
5791556Srgrimes	union node *argp;
5801556Srgrimes	struct arglist arglist;
5811556Srgrimes	struct arglist varlist;
5821556Srgrimes	char **argv;
5831556Srgrimes	int argc;
5841556Srgrimes	char **envp;
5851556Srgrimes	int varflag;
5861556Srgrimes	struct strlist *sp;
5871556Srgrimes	int mode;
5881556Srgrimes	int pip[2];
5891556Srgrimes	struct cmdentry cmdentry;
5901556Srgrimes	struct job *jp;
5911556Srgrimes	struct jmploc jmploc;
5921556Srgrimes	struct jmploc *volatile savehandler;
5931556Srgrimes	char *volatile savecmdname;
5941556Srgrimes	volatile struct shparam saveparam;
5951556Srgrimes	struct localvar *volatile savelocalvars;
5961556Srgrimes	volatile int e;
5971556Srgrimes	char *lastarg;
59845916Scracauer	int realstatus;
59954884Scracauer	int do_clearcmdentry;
600193222Srse#ifdef __GNUC__
60117987Speter	/* Avoid longjmp clobbering */
60217987Speter	(void) &argv;
60317987Speter	(void) &argc;
60417987Speter	(void) &lastarg;
60517987Speter	(void) &flags;
60654884Scracauer	(void) &do_clearcmdentry;
60717987Speter#endif
6081556Srgrimes
6091556Srgrimes	/* First expand the arguments. */
610149802Sstefanf	TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
6111556Srgrimes	setstackmark(&smark);
6121556Srgrimes	arglist.lastp = &arglist.list;
6131556Srgrimes	varlist.lastp = &varlist.list;
6141556Srgrimes	varflag = 1;
61554884Scracauer	do_clearcmdentry = 0;
61617987Speter	oexitstatus = exitstatus;
61717987Speter	exitstatus = 0;
6181556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
61917987Speter		char *p = argp->narg.text;
6201556Srgrimes		if (varflag && is_name(*p)) {
6211556Srgrimes			do {
6221556Srgrimes				p++;
6231556Srgrimes			} while (is_in_name(*p));
6241556Srgrimes			if (*p == '=') {
6251556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6261556Srgrimes				continue;
6271556Srgrimes			}
6281556Srgrimes		}
6291556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6301556Srgrimes		varflag = 0;
6311556Srgrimes	}
6321556Srgrimes	*arglist.lastp = NULL;
6331556Srgrimes	*varlist.lastp = NULL;
6341556Srgrimes	expredir(cmd->ncmd.redirect);
6351556Srgrimes	argc = 0;
6361556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6371556Srgrimes		argc++;
6381556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6391556Srgrimes
6401556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6411556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6421556Srgrimes		*argv++ = sp->text;
6431556Srgrimes	}
6441556Srgrimes	*argv = NULL;
6451556Srgrimes	lastarg = NULL;
6461556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6471556Srgrimes		lastarg = argv[-1];
6481556Srgrimes	argv -= argc;
6491556Srgrimes
6501556Srgrimes	/* Print the command if xflag is set. */
6511556Srgrimes	if (xflag) {
652159632Sstefanf		char sep = 0;
653159632Sstefanf		out2str(ps4val());
6541556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
655159632Sstefanf			if (sep != 0)
656159632Sstefanf				outc(' ', &errout);
6571556Srgrimes			out2str(sp->text);
658159632Sstefanf			sep = ' ';
6591556Srgrimes		}
6601556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
661159632Sstefanf			if (sep != 0)
662159632Sstefanf				outc(' ', &errout);
6631556Srgrimes			out2str(sp->text);
664159632Sstefanf			sep = ' ';
6651556Srgrimes		}
6661556Srgrimes		outc('\n', &errout);
6671556Srgrimes		flushout(&errout);
6681556Srgrimes	}
6691556Srgrimes
6701556Srgrimes	/* Now locate the command. */
6711556Srgrimes	if (argc == 0) {
672157601Sstefanf		/* Variable assignment(s) without command */
6731556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6741556Srgrimes		cmdentry.u.index = BLTINCMD;
675157601Sstefanf		cmdentry.special = 1;
6761556Srgrimes	} else {
67717987Speter		static const char PATH[] = "PATH=";
67817987Speter		char *path = pathval();
67917987Speter
68017987Speter		/*
68117987Speter		 * Modify the command lookup path, if a PATH= assignment
68217987Speter		 * is present
68317987Speter		 */
68417987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
68554884Scracauer			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
68617987Speter				path = sp->text + sizeof(PATH) - 1;
687155301Sschweikh				/*
68854884Scracauer				 * On `PATH=... command`, we need to make
68954884Scracauer				 * sure that the command isn't using the
69054884Scracauer				 * non-updated hash table of the outer PATH
691155301Sschweikh				 * setting and we need to make sure that
69254884Scracauer				 * the hash table isn't filled with items
69354884Scracauer				 * from the temporary setting.
69454884Scracauer				 *
695155301Sschweikh				 * It would be better to forbit using and
69654884Scracauer				 * updating the table while this command
69754884Scracauer				 * runs, by the command finding mechanism
69854884Scracauer				 * is heavily integrated with hash handling,
69954884Scracauer				 * so we just delete the hash before and after
70054884Scracauer				 * the command runs. Partly deleting like
70154884Scracauer				 * changepatch() does doesn't seem worth the
70254884Scracauer				 * bookinging effort, since most such runs add
703123996Smaxim				 * directories in front of the new PATH.
70454884Scracauer				 */
70554884Scracauer				clearcmdentry(0);
70654884Scracauer				do_clearcmdentry = 1;
70754884Scracauer			}
70817987Speter
70917987Speter		find_command(argv[0], &cmdentry, 1, path);
7101556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
71120425Ssteve			exitstatus = 127;
7121556Srgrimes			flushout(&errout);
7131556Srgrimes			return;
7141556Srgrimes		}
7151556Srgrimes		/* implement the bltin builtin here */
7161556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
7171556Srgrimes			for (;;) {
7181556Srgrimes				argv++;
7191556Srgrimes				if (--argc == 0)
7201556Srgrimes					break;
721157601Sstefanf				if ((cmdentry.u.index = find_builtin(*argv,
722157601Sstefanf				    &cmdentry.special)) < 0) {
7231556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
72420425Ssteve					exitstatus = 127;
7251556Srgrimes					flushout(&errout);
7261556Srgrimes					return;
7271556Srgrimes				}
7281556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7291556Srgrimes					break;
7301556Srgrimes			}
7311556Srgrimes		}
7321556Srgrimes	}
7331556Srgrimes
7341556Srgrimes	/* Fork off a child process if necessary. */
7351556Srgrimes	if (cmd->ncmd.backgnd
73645221Scracauer	 || (cmdentry.cmdtype == CMDNORMAL
737194127Sjilles	    && ((flags & EV_EXIT) == 0 || have_traps()))
73817987Speter	 || ((flags & EV_BACKCMD) != 0
7391556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
74048896Ssheldonh		 || cmdentry.u.index == CDCMD
7411556Srgrimes		 || cmdentry.u.index == DOTCMD
742100437Stjr		 || cmdentry.u.index == EVALCMD))
743100437Stjr	 || (cmdentry.cmdtype == CMDBUILTIN &&
744100437Stjr	    cmdentry.u.index == COMMANDCMD)) {
7451556Srgrimes		jp = makejob(cmd, 1);
7461556Srgrimes		mode = cmd->ncmd.backgnd;
7471556Srgrimes		if (flags & EV_BACKCMD) {
7481556Srgrimes			mode = FORK_NOJOB;
7491556Srgrimes			if (pipe(pip) < 0)
75053891Scracauer				error("Pipe call failed: %s", strerror(errno));
7511556Srgrimes		}
7521556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7531556Srgrimes			goto parent;	/* at end of routine */
7541556Srgrimes		if (flags & EV_BACKCMD) {
7551556Srgrimes			FORCEINTON;
7561556Srgrimes			close(pip[0]);
7571556Srgrimes			if (pip[1] != 1) {
758124780Sdes				dup2(pip[1], 1);
7591556Srgrimes				close(pip[1]);
7601556Srgrimes			}
7611556Srgrimes		}
7621556Srgrimes		flags |= EV_EXIT;
7631556Srgrimes	}
7641556Srgrimes
7651556Srgrimes	/* This is the child process if a fork occurred. */
7661556Srgrimes	/* Execute the command. */
7671556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
76820425Ssteve#ifdef DEBUG
7691556Srgrimes		trputs("Shell function:  ");  trargs(argv);
77020425Ssteve#endif
7711556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7721556Srgrimes		saveparam = shellparam;
7731556Srgrimes		shellparam.malloc = 0;
77420425Ssteve		shellparam.reset = 1;
7751556Srgrimes		shellparam.nparam = argc - 1;
7761556Srgrimes		shellparam.p = argv + 1;
7771556Srgrimes		shellparam.optnext = NULL;
7781556Srgrimes		INTOFF;
7791556Srgrimes		savelocalvars = localvars;
7801556Srgrimes		localvars = NULL;
7811556Srgrimes		INTON;
7821556Srgrimes		if (setjmp(jmploc.loc)) {
7831556Srgrimes			if (exception == EXSHELLPROC)
7841556Srgrimes				freeparam((struct shparam *)&saveparam);
7851556Srgrimes			else {
7861556Srgrimes				freeparam(&shellparam);
7871556Srgrimes				shellparam = saveparam;
7881556Srgrimes			}
7891556Srgrimes			poplocalvars();
7901556Srgrimes			localvars = savelocalvars;
7911556Srgrimes			handler = savehandler;
7921556Srgrimes			longjmp(handler->loc, 1);
7931556Srgrimes		}
7941556Srgrimes		savehandler = handler;
7951556Srgrimes		handler = &jmploc;
7961556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
7971556Srgrimes			mklocal(sp->text);
7981556Srgrimes		funcnest++;
799185231Sstefanf		exitstatus = oexitstatus;
80035675Scracauer		if (flags & EV_TESTED)
80135675Scracauer			evaltree(cmdentry.u.func, EV_TESTED);
80235675Scracauer		else
80335675Scracauer			evaltree(cmdentry.u.func, 0);
8041556Srgrimes		funcnest--;
8051556Srgrimes		INTOFF;
8061556Srgrimes		poplocalvars();
8071556Srgrimes		localvars = savelocalvars;
8081556Srgrimes		freeparam(&shellparam);
8091556Srgrimes		shellparam = saveparam;
8101556Srgrimes		handler = savehandler;
8111556Srgrimes		popredir();
8121556Srgrimes		INTON;
8131556Srgrimes		if (evalskip == SKIPFUNC) {
8141556Srgrimes			evalskip = 0;
8151556Srgrimes			skipcount = 0;
8161556Srgrimes		}
8171556Srgrimes		if (flags & EV_EXIT)
8181556Srgrimes			exitshell(exitstatus);
8191556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
82020425Ssteve#ifdef DEBUG
8211556Srgrimes		trputs("builtin command:  ");  trargs(argv);
82220425Ssteve#endif
8231556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
8241556Srgrimes		if (flags == EV_BACKCMD) {
8251556Srgrimes			memout.nleft = 0;
8261556Srgrimes			memout.nextc = memout.buf;
8271556Srgrimes			memout.bufsize = 64;
8281556Srgrimes			mode |= REDIR_BACKQ;
8291556Srgrimes		}
8301556Srgrimes		savecmdname = commandname;
8311556Srgrimes		cmdenviron = varlist.list;
8321556Srgrimes		e = -1;
8331556Srgrimes		if (setjmp(jmploc.loc)) {
8341556Srgrimes			e = exception;
8351556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8361556Srgrimes			goto cmddone;
8371556Srgrimes		}
8381556Srgrimes		savehandler = handler;
8391556Srgrimes		handler = &jmploc;
840157601Sstefanf		redirect(cmd->ncmd.redirect, mode);
841157601Sstefanf		if (cmdentry.special)
842157601Sstefanf			listsetvar(cmdenviron);
8431556Srgrimes		commandname = argv[0];
8441556Srgrimes		argptr = argv + 1;
8451556Srgrimes		optptr = NULL;			/* initialize nextopt */
846193169Sstefanf		builtin_flags = flags;
8471556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8481556Srgrimes		flushall();
8491556Srgrimescmddone:
85060592Scracauer		cmdenviron = NULL;
8511556Srgrimes		out1 = &output;
8521556Srgrimes		out2 = &errout;
8531556Srgrimes		freestdout();
8541556Srgrimes		if (e != EXSHELLPROC) {
8551556Srgrimes			commandname = savecmdname;
8561556Srgrimes			if (flags & EV_EXIT) {
8571556Srgrimes				exitshell(exitstatus);
8581556Srgrimes			}
8591556Srgrimes		}
8601556Srgrimes		handler = savehandler;
8611556Srgrimes		if (e != -1) {
86220425Ssteve			if ((e != EXERROR && e != EXEXEC)
863157601Sstefanf			    || cmdentry.special)
8641556Srgrimes				exraise(e);
8651556Srgrimes			FORCEINTON;
8661556Srgrimes		}
8671556Srgrimes		if (cmdentry.u.index != EXECCMD)
8681556Srgrimes			popredir();
8691556Srgrimes		if (flags == EV_BACKCMD) {
8701556Srgrimes			backcmd->buf = memout.buf;
8711556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8721556Srgrimes			memout.buf = NULL;
8731556Srgrimes		}
8741556Srgrimes	} else {
87520425Ssteve#ifdef DEBUG
8761556Srgrimes		trputs("normal command:  ");  trargs(argv);
87720425Ssteve#endif
8781556Srgrimes		clearredir();
8791556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8801556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8811556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8821556Srgrimes		envp = environment();
88317987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8841556Srgrimes		/*NOTREACHED*/
8851556Srgrimes	}
8861556Srgrimes	goto out;
8871556Srgrimes
8881556Srgrimesparent:	/* parent process gets here (if we forked) */
8891556Srgrimes	if (mode == 0) {	/* argument to fork */
8901556Srgrimes		INTOFF;
89145916Scracauer		exitstatus = waitforjob(jp, &realstatus);
8921556Srgrimes		INTON;
89345916Scracauer		if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
89445266Scracauer			evalskip = SKIPBREAK;
89545266Scracauer			skipcount = loopnest;
89645266Scracauer		}
8971556Srgrimes	} else if (mode == 2) {
8981556Srgrimes		backcmd->fd = pip[0];
8991556Srgrimes		close(pip[1]);
9001556Srgrimes		backcmd->jp = jp;
9011556Srgrimes	}
9021556Srgrimes
9031556Srgrimesout:
9041556Srgrimes	if (lastarg)
9051556Srgrimes		setvar("_", lastarg, 0);
90654884Scracauer	if (do_clearcmdentry)
90754884Scracauer		clearcmdentry(0);
9081556Srgrimes	popstackmark(&smark);
9091556Srgrimes}
9101556Srgrimes
9111556Srgrimes
9121556Srgrimes
9131556Srgrimes/*
9141556Srgrimes * Search for a command.  This is called before we fork so that the
9151556Srgrimes * location of the command will be available in the parent as well as
9161556Srgrimes * the child.  The check for "goodname" is an overly conservative
9171556Srgrimes * check that the name will not be subject to expansion.
9181556Srgrimes */
9191556Srgrimes
9201556SrgrimesSTATIC void
92190111Simpprehash(union node *n)
92217987Speter{
9231556Srgrimes	struct cmdentry entry;
9241556Srgrimes
925159633Sstefanf	if (n && n->type == NCMD && n->ncmd.args)
92617987Speter		if (goodname(n->ncmd.args->narg.text))
92717987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
92817987Speter				     pathval());
9291556Srgrimes}
9301556Srgrimes
9311556Srgrimes
9321556Srgrimes
9331556Srgrimes/*
9341556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
9351556Srgrimes * tied to evaluation are implemented here.
9361556Srgrimes */
9371556Srgrimes
9381556Srgrimes/*
939157601Sstefanf * No command given, or a bltin command with no arguments.
9401556Srgrimes */
9411556Srgrimes
94217987Speterint
94390111Simpbltincmd(int argc __unused, char **argv __unused)
94417987Speter{
94520425Ssteve	/*
94617987Speter	 * Preserve exitstatus of a previous possible redirection
94720425Ssteve	 * as POSIX mandates
94817987Speter	 */
9491556Srgrimes	return exitstatus;
9501556Srgrimes}
9511556Srgrimes
9521556Srgrimes
9531556Srgrimes/*
9541556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9551556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9561556Srgrimes * above all check this flag, and if it is set they start skipping
9571556Srgrimes * commands rather than executing them.  The variable skipcount is
9581556Srgrimes * the number of loops to break/continue, or the number of function
9591556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9601556Srgrimes * be an error to break out of more loops than exist, but it isn't
9611556Srgrimes * in the standard shell so we don't make it one here.
9621556Srgrimes */
9631556Srgrimes
96417987Speterint
96590111Simpbreakcmd(int argc, char **argv)
96617987Speter{
96720425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
9681556Srgrimes
9691556Srgrimes	if (n > loopnest)
9701556Srgrimes		n = loopnest;
9711556Srgrimes	if (n > 0) {
9721556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9731556Srgrimes		skipcount = n;
9741556Srgrimes	}
9751556Srgrimes	return 0;
9761556Srgrimes}
9771556Srgrimes
978100437Stjr/*
979100437Stjr * The `command' command.
980100437Stjr */
981100437Stjrint
982100437Stjrcommandcmd(int argc, char **argv)
983100437Stjr{
984100437Stjr	static char stdpath[] = _PATH_STDPATH;
985100437Stjr	struct jmploc loc, *old;
986100437Stjr	struct strlist *sp;
987100437Stjr	char *path;
988100437Stjr	int ch;
989151810Sstefanf	int cmd = -1;
9901556Srgrimes
991100437Stjr	for (sp = cmdenviron; sp ; sp = sp->next)
992100437Stjr		setvareq(sp->text, VEXPORT|VSTACK);
993100437Stjr	path = pathval();
994100437Stjr
995100437Stjr	optind = optreset = 1;
996100663Stjr	opterr = 0;
997151810Sstefanf	while ((ch = getopt(argc, argv, "pvV")) != -1) {
998100437Stjr		switch (ch) {
999100437Stjr		case 'p':
1000100437Stjr			path = stdpath;
1001100437Stjr			break;
1002151810Sstefanf		case 'v':
1003151810Sstefanf			cmd = TYPECMD_SMALLV;
1004151810Sstefanf			break;
1005151810Sstefanf		case 'V':
1006151810Sstefanf			cmd = TYPECMD_BIGV;
1007151810Sstefanf			break;
1008100437Stjr		case '?':
1009100437Stjr		default:
1010100437Stjr			error("unknown option: -%c", optopt);
1011100437Stjr		}
1012100437Stjr	}
1013100437Stjr	argc -= optind;
1014100437Stjr	argv += optind;
1015100437Stjr
1016151810Sstefanf	if (cmd != -1) {
1017151810Sstefanf		if (argc != 1)
1018151810Sstefanf			error("wrong number of arguments");
1019151810Sstefanf		return typecmd_impl(2, argv - 1, cmd);
1020151810Sstefanf	}
1021100437Stjr	if (argc != 0) {
1022100437Stjr		old = handler;
1023100437Stjr		handler = &loc;
1024100437Stjr		if (setjmp(handler->loc) == 0)
1025100437Stjr			shellexec(argv, environment(), path, 0);
1026100437Stjr		handler = old;
1027100437Stjr		if (exception == EXEXEC)
1028100437Stjr			exit(exerrno);
1029100437Stjr		exraise(exception);
1030100437Stjr	}
1031100437Stjr
1032100437Stjr	/*
1033100437Stjr	 * Do nothing successfully if no command was specified;
1034100437Stjr	 * ksh also does this.
1035100437Stjr	 */
1036100437Stjr	exit(0);
1037100437Stjr}
1038100437Stjr
1039100437Stjr
10401556Srgrimes/*
10411556Srgrimes * The return command.
10421556Srgrimes */
10431556Srgrimes
104417987Speterint
104590111Simpreturncmd(int argc, char **argv)
104617987Speter{
104720425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
10481556Srgrimes
10491556Srgrimes	if (funcnest) {
10501556Srgrimes		evalskip = SKIPFUNC;
10511556Srgrimes		skipcount = 1;
105220425Ssteve	} else {
105320425Ssteve		/* skip the rest of the file */
105420425Ssteve		evalskip = SKIPFILE;
105520425Ssteve		skipcount = 1;
10561556Srgrimes	}
10571556Srgrimes	return ret;
10581556Srgrimes}
10591556Srgrimes
10601556Srgrimes
106117987Speterint
106290111Simpfalsecmd(int argc __unused, char **argv __unused)
106317987Speter{
106417987Speter	return 1;
106517987Speter}
106617987Speter
106717987Speter
106817987Speterint
106990111Simptruecmd(int argc __unused, char **argv __unused)
107017987Speter{
10711556Srgrimes	return 0;
10721556Srgrimes}
10731556Srgrimes
10741556Srgrimes
107517987Speterint
107690111Simpexeccmd(int argc, char **argv)
107717987Speter{
10781556Srgrimes	if (argc > 1) {
107917987Speter		struct strlist *sp;
108017987Speter
10811556Srgrimes		iflag = 0;		/* exit on error */
10821556Srgrimes		mflag = 0;
10831556Srgrimes		optschanged();
108417987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
108517987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10861556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10871556Srgrimes
10881556Srgrimes	}
10891556Srgrimes	return 0;
10901556Srgrimes}
1091153091Sstefanf
1092153091Sstefanf
1093153091Sstefanfint
1094153091Sstefanftimescmd(int argc __unused, char **argv __unused)
1095153091Sstefanf{
1096153091Sstefanf	struct rusage ru;
1097153091Sstefanf	long shumins, shsmins, chumins, chsmins;
1098153091Sstefanf	double shusecs, shssecs, chusecs, chssecs;
1099153091Sstefanf
1100153091Sstefanf	if (getrusage(RUSAGE_SELF, &ru) < 0)
1101153091Sstefanf		return 1;
1102153091Sstefanf	shumins = ru.ru_utime.tv_sec / 60;
1103153091Sstefanf	shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1104153091Sstefanf	shsmins = ru.ru_stime.tv_sec / 60;
1105153091Sstefanf	shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1106153091Sstefanf	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1107153091Sstefanf		return 1;
1108153091Sstefanf	chumins = ru.ru_utime.tv_sec / 60;
1109153091Sstefanf	chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1110153091Sstefanf	chsmins = ru.ru_stime.tv_sec / 60;
1111153091Sstefanf	chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1112153091Sstefanf	out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1113153091Sstefanf	    shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);
1114153091Sstefanf	return 0;
1115153091Sstefanf}
1116