eval.c revision 100437
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 * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
4036150Scharnier#endif
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/eval.c 100437 2002-07-21 06:49:14Z tjr $");
441556Srgrimes
45100437Stjr#include <paths.h>
4617987Speter#include <signal.h>
4717987Speter#include <unistd.h>
4845266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */
4953891Scracauer#include <errno.h>
5017987Speter
511556Srgrimes/*
521556Srgrimes * Evaluate a command.
531556Srgrimes */
541556Srgrimes
551556Srgrimes#include "shell.h"
561556Srgrimes#include "nodes.h"
571556Srgrimes#include "syntax.h"
581556Srgrimes#include "expand.h"
591556Srgrimes#include "parser.h"
601556Srgrimes#include "jobs.h"
611556Srgrimes#include "eval.h"
621556Srgrimes#include "builtins.h"
631556Srgrimes#include "options.h"
641556Srgrimes#include "exec.h"
651556Srgrimes#include "redir.h"
661556Srgrimes#include "input.h"
671556Srgrimes#include "output.h"
681556Srgrimes#include "trap.h"
691556Srgrimes#include "var.h"
701556Srgrimes#include "memalloc.h"
711556Srgrimes#include "error.h"
7217987Speter#include "show.h"
731556Srgrimes#include "mystring.h"
7417987Speter#ifndef NO_HISTORY
751556Srgrimes#include "myhistedit.h"
7617987Speter#endif
771556Srgrimes
781556Srgrimes
791556Srgrimes/* flags in argument to evaltree */
801556Srgrimes#define EV_EXIT 01		/* exit after evaluating tree */
811556Srgrimes#define EV_TESTED 02		/* exit status is checked; ignore -e flag */
821556Srgrimes#define EV_BACKCMD 04		/* command executing within back quotes */
831556Srgrimes
841556SrgrimesMKINIT int evalskip;		/* set if we are skipping commands */
851556SrgrimesSTATIC int skipcount;		/* number of levels to skip */
861556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
871556Srgrimesint funcnest;			/* depth of function calls */
881556Srgrimes
891556Srgrimes
901556Srgrimeschar *commandname;
911556Srgrimesstruct strlist *cmdenviron;
921556Srgrimesint exitstatus;			/* exit status of last command */
9317987Speterint oexitstatus;		/* saved exit status */
941556Srgrimes
951556Srgrimes
9690111SimpSTATIC void evalloop(union node *);
9790111SimpSTATIC void evalfor(union node *);
9890111SimpSTATIC void evalcase(union node *, int);
9990111SimpSTATIC void evalsubshell(union node *, int);
10090111SimpSTATIC void expredir(union node *);
10190111SimpSTATIC void evalpipe(union node *);
10290111SimpSTATIC void evalcommand(union node *, int, struct backcmd *);
10390111SimpSTATIC void prehash(union node *);
1041556Srgrimes
1051556Srgrimes
1061556Srgrimes/*
1071556Srgrimes * Called to reset things after an exception.
1081556Srgrimes */
1091556Srgrimes
1101556Srgrimes#ifdef mkinit
1111556SrgrimesINCLUDE "eval.h"
1121556Srgrimes
1131556SrgrimesRESET {
1141556Srgrimes	evalskip = 0;
1151556Srgrimes	loopnest = 0;
1161556Srgrimes	funcnest = 0;
1171556Srgrimes}
1181556Srgrimes
1191556SrgrimesSHELLPROC {
1201556Srgrimes	exitstatus = 0;
1211556Srgrimes}
1221556Srgrimes#endif
1231556Srgrimes
1241556Srgrimes
1251556Srgrimes
1261556Srgrimes/*
12746684Skris * The eval command.
1281556Srgrimes */
1291556Srgrimes
13017987Speterint
13190111Simpevalcmd(int argc, char **argv)
1321556Srgrimes{
1331556Srgrimes        char *p;
1341556Srgrimes        char *concat;
1351556Srgrimes        char **ap;
1361556Srgrimes
1371556Srgrimes        if (argc > 1) {
1381556Srgrimes                p = argv[1];
1391556Srgrimes                if (argc > 2) {
1401556Srgrimes                        STARTSTACKSTR(concat);
1411556Srgrimes                        ap = argv + 2;
1421556Srgrimes                        for (;;) {
1431556Srgrimes                                while (*p)
1441556Srgrimes                                        STPUTC(*p++, concat);
1451556Srgrimes                                if ((p = *ap++) == NULL)
1461556Srgrimes                                        break;
1471556Srgrimes                                STPUTC(' ', concat);
1481556Srgrimes                        }
1491556Srgrimes                        STPUTC('\0', concat);
1501556Srgrimes                        p = grabstackstr(concat);
1511556Srgrimes                }
1521556Srgrimes                evalstring(p);
1531556Srgrimes        }
1541556Srgrimes        return exitstatus;
1551556Srgrimes}
1561556Srgrimes
1571556Srgrimes
1581556Srgrimes/*
1591556Srgrimes * Execute a command or commands contained in a string.
1601556Srgrimes */
1611556Srgrimes
1621556Srgrimesvoid
16390111Simpevalstring(char *s)
16490111Simp{
1651556Srgrimes	union node *n;
1661556Srgrimes	struct stackmark smark;
1671556Srgrimes
1681556Srgrimes	setstackmark(&smark);
1691556Srgrimes	setinputstring(s, 1);
1701556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
1711556Srgrimes		evaltree(n, 0);
1721556Srgrimes		popstackmark(&smark);
1731556Srgrimes	}
1741556Srgrimes	popfile();
1751556Srgrimes	popstackmark(&smark);
1761556Srgrimes}
1771556Srgrimes
1781556Srgrimes
1791556Srgrimes
1801556Srgrimes/*
1811556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1821556Srgrimes * exitstatus.
1831556Srgrimes */
1841556Srgrimes
1851556Srgrimesvoid
18690111Simpevaltree(union node *n, int flags)
18717987Speter{
1881556Srgrimes	if (n == NULL) {
1891556Srgrimes		TRACE(("evaltree(NULL) called\n"));
1901556Srgrimes		exitstatus = 0;
1911556Srgrimes		goto out;
1921556Srgrimes	}
19317987Speter#ifndef NO_HISTORY
1941556Srgrimes	displayhist = 1;	/* show history substitutions done with fc */
19517987Speter#endif
19617987Speter	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
1971556Srgrimes	switch (n->type) {
1981556Srgrimes	case NSEMI:
1991556Srgrimes		evaltree(n->nbinary.ch1, 0);
2001556Srgrimes		if (evalskip)
2011556Srgrimes			goto out;
2021556Srgrimes		evaltree(n->nbinary.ch2, flags);
2031556Srgrimes		break;
2041556Srgrimes	case NAND:
2051556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
20618754Ssteve		if (evalskip || exitstatus != 0) {
20718754Ssteve			flags |= EV_TESTED;
2081556Srgrimes			goto out;
20918754Ssteve		}
2101556Srgrimes		evaltree(n->nbinary.ch2, flags);
2111556Srgrimes		break;
2121556Srgrimes	case NOR:
2131556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2141556Srgrimes		if (evalskip || exitstatus == 0)
2151556Srgrimes			goto out;
2161556Srgrimes		evaltree(n->nbinary.ch2, flags);
2171556Srgrimes		break;
2181556Srgrimes	case NREDIR:
2191556Srgrimes		expredir(n->nredir.redirect);
2201556Srgrimes		redirect(n->nredir.redirect, REDIR_PUSH);
2211556Srgrimes		evaltree(n->nredir.n, flags);
2221556Srgrimes		popredir();
2231556Srgrimes		break;
2241556Srgrimes	case NSUBSHELL:
2251556Srgrimes		evalsubshell(n, flags);
2261556Srgrimes		break;
2271556Srgrimes	case NBACKGND:
2281556Srgrimes		evalsubshell(n, flags);
2291556Srgrimes		break;
2301556Srgrimes	case NIF: {
2311556Srgrimes		evaltree(n->nif.test, EV_TESTED);
2321556Srgrimes		if (evalskip)
2331556Srgrimes			goto out;
23420425Ssteve		if (exitstatus == 0)
2351556Srgrimes			evaltree(n->nif.ifpart, flags);
23617987Speter		else if (n->nif.elsepart)
2371556Srgrimes			evaltree(n->nif.elsepart, flags);
23820425Ssteve		else
23920425Ssteve			exitstatus = 0;
2401556Srgrimes		break;
2411556Srgrimes	}
2421556Srgrimes	case NWHILE:
2431556Srgrimes	case NUNTIL:
2441556Srgrimes		evalloop(n);
2451556Srgrimes		break;
2461556Srgrimes	case NFOR:
2471556Srgrimes		evalfor(n);
24877557Sgad		/*
24977557Sgad		 * The 'for' command does not set exitstatus, so the value
25077557Sgad		 * now in exitstatus is from the last command executed in
25177557Sgad		 * the 'for' loop.  That exit value had been tested (wrt
25277557Sgad		 * 'sh -e' checking) while processing that command, and
25377557Sgad		 * it should not be re-tested here.
25477557Sgad		 */
25577557Sgad		flags |= EV_TESTED;
2561556Srgrimes		break;
2571556Srgrimes	case NCASE:
2581556Srgrimes		evalcase(n, flags);
25977557Sgad		/*
26077557Sgad		 * The 'case' command does not set exitstatus, so the value
26177557Sgad		 * now in exitstatus is from the last command executed in
26277557Sgad		 * the 'case' block.  That exit value had been tested (wrt
26377557Sgad		 * 'sh -e' checking) while processing that command, and
26477557Sgad		 * it should not be re-tested here.
26577557Sgad		 */
26677557Sgad		flags |= EV_TESTED;
2671556Srgrimes		break;
2681556Srgrimes	case NDEFUN:
2691556Srgrimes		defun(n->narg.text, n->narg.next);
2701556Srgrimes		exitstatus = 0;
2711556Srgrimes		break;
2721556Srgrimes	case NNOT:
2731556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2741556Srgrimes		exitstatus = !exitstatus;
2751556Srgrimes		break;
2761556Srgrimes
2771556Srgrimes	case NPIPE:
2781556Srgrimes		evalpipe(n);
2791556Srgrimes		break;
2801556Srgrimes	case NCMD:
2811556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
2821556Srgrimes		break;
2831556Srgrimes	default:
2841556Srgrimes		out1fmt("Node type = %d\n", n->type);
2851556Srgrimes		flushout(&output);
2861556Srgrimes		break;
2871556Srgrimes	}
2881556Srgrimesout:
2891556Srgrimes	if (pendingsigs)
2901556Srgrimes		dotrap();
29152526Scracauer	/*
29252526Scracauer	 * XXX - Like "!(n->type == NSEMI)", more types will probably
29352526Scracauer	 * need to be excluded from this test. It's probably better
29452526Scracauer	 * to set or unset EV_TESTED in the loop above than to bloat
29552526Scracauer	 * the conditional here.
29652526Scracauer	 */
29752526Scracauer	if ((flags & EV_EXIT) || (eflag && exitstatus
29852526Scracauer	    && !(flags & EV_TESTED) && !(n->type == NSEMI)))
2991556Srgrimes		exitshell(exitstatus);
3001556Srgrimes}
3011556Srgrimes
3021556Srgrimes
3031556SrgrimesSTATIC void
30490111Simpevalloop(union node *n)
30517987Speter{
3061556Srgrimes	int status;
3071556Srgrimes
3081556Srgrimes	loopnest++;
3091556Srgrimes	status = 0;
3101556Srgrimes	for (;;) {
3111556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
3121556Srgrimes		if (evalskip) {
3131556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
3141556Srgrimes				evalskip = 0;
3151556Srgrimes				continue;
3161556Srgrimes			}
3171556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3181556Srgrimes				evalskip = 0;
3191556Srgrimes			break;
3201556Srgrimes		}
3211556Srgrimes		if (n->type == NWHILE) {
3221556Srgrimes			if (exitstatus != 0)
3231556Srgrimes				break;
3241556Srgrimes		} else {
3251556Srgrimes			if (exitstatus == 0)
3261556Srgrimes				break;
3271556Srgrimes		}
3281556Srgrimes		evaltree(n->nbinary.ch2, 0);
3291556Srgrimes		status = exitstatus;
3301556Srgrimes		if (evalskip)
3311556Srgrimes			goto skipping;
3321556Srgrimes	}
3331556Srgrimes	loopnest--;
3341556Srgrimes	exitstatus = status;
3351556Srgrimes}
3361556Srgrimes
3371556Srgrimes
3381556Srgrimes
3391556SrgrimesSTATIC void
34090111Simpevalfor(union node *n)
34117987Speter{
3421556Srgrimes	struct arglist arglist;
3431556Srgrimes	union node *argp;
3441556Srgrimes	struct strlist *sp;
3451556Srgrimes	struct stackmark smark;
3461556Srgrimes
3471556Srgrimes	setstackmark(&smark);
3481556Srgrimes	arglist.lastp = &arglist.list;
3491556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
35017987Speter		oexitstatus = exitstatus;
3511556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3521556Srgrimes		if (evalskip)
3531556Srgrimes			goto out;
3541556Srgrimes	}
3551556Srgrimes	*arglist.lastp = NULL;
3561556Srgrimes
3571556Srgrimes	exitstatus = 0;
3581556Srgrimes	loopnest++;
3591556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3601556Srgrimes		setvar(n->nfor.var, sp->text, 0);
3611556Srgrimes		evaltree(n->nfor.body, 0);
3621556Srgrimes		if (evalskip) {
3631556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3641556Srgrimes				evalskip = 0;
3651556Srgrimes				continue;
3661556Srgrimes			}
3671556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3681556Srgrimes				evalskip = 0;
3691556Srgrimes			break;
3701556Srgrimes		}
3711556Srgrimes	}
3721556Srgrimes	loopnest--;
3731556Srgrimesout:
3741556Srgrimes	popstackmark(&smark);
3751556Srgrimes}
3761556Srgrimes
3771556Srgrimes
3781556Srgrimes
3791556SrgrimesSTATIC void
38090111Simpevalcase(union node *n, int flags)
38117987Speter{
3821556Srgrimes	union node *cp;
3831556Srgrimes	union node *patp;
3841556Srgrimes	struct arglist arglist;
3851556Srgrimes	struct stackmark smark;
3861556Srgrimes
3871556Srgrimes	setstackmark(&smark);
3881556Srgrimes	arglist.lastp = &arglist.list;
38917987Speter	oexitstatus = exitstatus;
3901556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3911556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3921556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3931556Srgrimes			if (casematch(patp, arglist.list->text)) {
3941556Srgrimes				if (evalskip == 0) {
3951556Srgrimes					evaltree(cp->nclist.body, flags);
3961556Srgrimes				}
3971556Srgrimes				goto out;
3981556Srgrimes			}
3991556Srgrimes		}
4001556Srgrimes	}
4011556Srgrimesout:
4021556Srgrimes	popstackmark(&smark);
4031556Srgrimes}
4041556Srgrimes
4051556Srgrimes
4061556Srgrimes
4071556Srgrimes/*
4081556Srgrimes * Kick off a subshell to evaluate a tree.
4091556Srgrimes */
4101556Srgrimes
4111556SrgrimesSTATIC void
41290111Simpevalsubshell(union node *n, int flags)
41317987Speter{
4141556Srgrimes	struct job *jp;
4151556Srgrimes	int backgnd = (n->type == NBACKGND);
4161556Srgrimes
4171556Srgrimes	expredir(n->nredir.redirect);
4181556Srgrimes	jp = makejob(n, 1);
4191556Srgrimes	if (forkshell(jp, n, backgnd) == 0) {
4201556Srgrimes		if (backgnd)
4211556Srgrimes			flags &=~ EV_TESTED;
4221556Srgrimes		redirect(n->nredir.redirect, 0);
4231556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
4241556Srgrimes	}
4251556Srgrimes	if (! backgnd) {
4261556Srgrimes		INTOFF;
42745916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
4281556Srgrimes		INTON;
4291556Srgrimes	}
4301556Srgrimes}
4311556Srgrimes
4321556Srgrimes
4331556Srgrimes
4341556Srgrimes/*
4351556Srgrimes * Compute the names of the files in a redirection list.
4361556Srgrimes */
4371556Srgrimes
4381556SrgrimesSTATIC void
43990111Simpexpredir(union node *n)
44017987Speter{
44125222Ssteve	union node *redir;
4421556Srgrimes
4431556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
44417987Speter		struct arglist fn;
44517987Speter		fn.lastp = &fn.list;
44617987Speter		oexitstatus = exitstatus;
44717987Speter		switch (redir->type) {
44817987Speter		case NFROM:
44917987Speter		case NTO:
45066612Sbrian		case NFROMTO:
45117987Speter		case NAPPEND:
45296922Stjr		case NCLOBBER:
4531556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4541556Srgrimes			redir->nfile.expfname = fn.list->text;
45517987Speter			break;
45617987Speter		case NFROMFD:
45717987Speter		case NTOFD:
45817987Speter			if (redir->ndup.vname) {
45917987Speter				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
46017987Speter				fixredir(redir, fn.list->text, 1);
46117987Speter			}
46217987Speter			break;
4631556Srgrimes		}
4641556Srgrimes	}
4651556Srgrimes}
4661556Srgrimes
4671556Srgrimes
4681556Srgrimes
4691556Srgrimes/*
4701556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
4711556Srgrimes * of the process creating the pipeline.  (This differs from some versions
4721556Srgrimes * of the shell, which make the last process in a pipeline the parent
4731556Srgrimes * of all the rest.)
4741556Srgrimes */
4751556Srgrimes
4761556SrgrimesSTATIC void
47790111Simpevalpipe(union node *n)
47817987Speter{
4791556Srgrimes	struct job *jp;
4801556Srgrimes	struct nodelist *lp;
4811556Srgrimes	int pipelen;
4821556Srgrimes	int prevfd;
4831556Srgrimes	int pip[2];
4841556Srgrimes
48517987Speter	TRACE(("evalpipe(0x%lx) called\n", (long)n));
4861556Srgrimes	pipelen = 0;
4871556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
4881556Srgrimes		pipelen++;
4891556Srgrimes	INTOFF;
4901556Srgrimes	jp = makejob(n, pipelen);
4911556Srgrimes	prevfd = -1;
4921556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
4931556Srgrimes		prehash(lp->n);
4941556Srgrimes		pip[1] = -1;
4951556Srgrimes		if (lp->next) {
4961556Srgrimes			if (pipe(pip) < 0) {
4971556Srgrimes				close(prevfd);
49853891Scracauer				error("Pipe call failed: %s", strerror(errno));
4991556Srgrimes			}
5001556Srgrimes		}
5011556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
5021556Srgrimes			INTON;
5031556Srgrimes			if (prevfd > 0) {
5041556Srgrimes				close(0);
5051556Srgrimes				copyfd(prevfd, 0);
5061556Srgrimes				close(prevfd);
5071556Srgrimes			}
5081556Srgrimes			if (pip[1] >= 0) {
50953282Scracauer				if (!(prevfd >= 0 && pip[0] == 0))
51052900Scracauer					close(pip[0]);
5111556Srgrimes				if (pip[1] != 1) {
5121556Srgrimes					close(1);
5131556Srgrimes					copyfd(pip[1], 1);
5141556Srgrimes					close(pip[1]);
5151556Srgrimes				}
5161556Srgrimes			}
5171556Srgrimes			evaltree(lp->n, EV_EXIT);
5181556Srgrimes		}
5191556Srgrimes		if (prevfd >= 0)
5201556Srgrimes			close(prevfd);
5211556Srgrimes		prevfd = pip[0];
5221556Srgrimes		close(pip[1]);
5231556Srgrimes	}
5241556Srgrimes	INTON;
5251556Srgrimes	if (n->npipe.backgnd == 0) {
5261556Srgrimes		INTOFF;
52745916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
5281556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5291556Srgrimes		INTON;
5301556Srgrimes	}
5311556Srgrimes}
5321556Srgrimes
5331556Srgrimes
5341556Srgrimes
5351556Srgrimes/*
5361556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5371556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5381556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5391556Srgrimes * Should be called with interrupts off.
5401556Srgrimes */
5411556Srgrimes
5421556Srgrimesvoid
54390111Simpevalbackcmd(union node *n, struct backcmd *result)
54417987Speter{
5451556Srgrimes	int pip[2];
5461556Srgrimes	struct job *jp;
5471556Srgrimes	struct stackmark smark;		/* unnecessary */
5481556Srgrimes
5491556Srgrimes	setstackmark(&smark);
5501556Srgrimes	result->fd = -1;
5511556Srgrimes	result->buf = NULL;
5521556Srgrimes	result->nleft = 0;
5531556Srgrimes	result->jp = NULL;
55417987Speter	if (n == NULL) {
55517987Speter		exitstatus = 0;
5561556Srgrimes		goto out;
55717987Speter	}
5581556Srgrimes	if (n->type == NCMD) {
55917987Speter		exitstatus = oexitstatus;
5601556Srgrimes		evalcommand(n, EV_BACKCMD, result);
5611556Srgrimes	} else {
56217987Speter		exitstatus = 0;
5631556Srgrimes		if (pipe(pip) < 0)
56453891Scracauer			error("Pipe call failed: %s", strerror(errno));
5651556Srgrimes		jp = makejob(n, 1);
5661556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
5671556Srgrimes			FORCEINTON;
5681556Srgrimes			close(pip[0]);
5691556Srgrimes			if (pip[1] != 1) {
5701556Srgrimes				close(1);
5711556Srgrimes				copyfd(pip[1], 1);
5721556Srgrimes				close(pip[1]);
5731556Srgrimes			}
5741556Srgrimes			evaltree(n, EV_EXIT);
5751556Srgrimes		}
5761556Srgrimes		close(pip[1]);
5771556Srgrimes		result->fd = pip[0];
5781556Srgrimes		result->jp = jp;
5791556Srgrimes	}
5801556Srgrimesout:
5811556Srgrimes	popstackmark(&smark);
5821556Srgrimes	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
5831556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
5841556Srgrimes}
5851556Srgrimes
5861556Srgrimes
5871556Srgrimes
5881556Srgrimes/*
5891556Srgrimes * Execute a simple command.
5901556Srgrimes */
5911556Srgrimes
5921556SrgrimesSTATIC void
59390111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd)
59417987Speter{
5951556Srgrimes	struct stackmark smark;
5961556Srgrimes	union node *argp;
5971556Srgrimes	struct arglist arglist;
5981556Srgrimes	struct arglist varlist;
5991556Srgrimes	char **argv;
6001556Srgrimes	int argc;
6011556Srgrimes	char **envp;
6021556Srgrimes	int varflag;
6031556Srgrimes	struct strlist *sp;
6041556Srgrimes	int mode;
6051556Srgrimes	int pip[2];
6061556Srgrimes	struct cmdentry cmdentry;
6071556Srgrimes	struct job *jp;
6081556Srgrimes	struct jmploc jmploc;
6091556Srgrimes	struct jmploc *volatile savehandler;
6101556Srgrimes	char *volatile savecmdname;
6111556Srgrimes	volatile struct shparam saveparam;
6121556Srgrimes	struct localvar *volatile savelocalvars;
6131556Srgrimes	volatile int e;
6141556Srgrimes	char *lastarg;
61545916Scracauer	int realstatus;
61654884Scracauer	int do_clearcmdentry;
61717987Speter#if __GNUC__
61817987Speter	/* Avoid longjmp clobbering */
61917987Speter	(void) &argv;
62017987Speter	(void) &argc;
62117987Speter	(void) &lastarg;
62217987Speter	(void) &flags;
62354884Scracauer	(void) &do_clearcmdentry;
62417987Speter#endif
6251556Srgrimes
6261556Srgrimes	/* First expand the arguments. */
62717987Speter	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
6281556Srgrimes	setstackmark(&smark);
6291556Srgrimes	arglist.lastp = &arglist.list;
6301556Srgrimes	varlist.lastp = &varlist.list;
6311556Srgrimes	varflag = 1;
63254884Scracauer	do_clearcmdentry = 0;
63317987Speter	oexitstatus = exitstatus;
63417987Speter	exitstatus = 0;
6351556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
63617987Speter		char *p = argp->narg.text;
6371556Srgrimes		if (varflag && is_name(*p)) {
6381556Srgrimes			do {
6391556Srgrimes				p++;
6401556Srgrimes			} while (is_in_name(*p));
6411556Srgrimes			if (*p == '=') {
6421556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6431556Srgrimes				continue;
6441556Srgrimes			}
6451556Srgrimes		}
6461556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6471556Srgrimes		varflag = 0;
6481556Srgrimes	}
6491556Srgrimes	*arglist.lastp = NULL;
6501556Srgrimes	*varlist.lastp = NULL;
6511556Srgrimes	expredir(cmd->ncmd.redirect);
6521556Srgrimes	argc = 0;
6531556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6541556Srgrimes		argc++;
6551556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6561556Srgrimes
6571556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6581556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6591556Srgrimes		*argv++ = sp->text;
6601556Srgrimes	}
6611556Srgrimes	*argv = NULL;
6621556Srgrimes	lastarg = NULL;
6631556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6641556Srgrimes		lastarg = argv[-1];
6651556Srgrimes	argv -= argc;
6661556Srgrimes
6671556Srgrimes	/* Print the command if xflag is set. */
6681556Srgrimes	if (xflag) {
6691556Srgrimes		outc('+', &errout);
6701556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
6711556Srgrimes			outc(' ', &errout);
6721556Srgrimes			out2str(sp->text);
6731556Srgrimes		}
6741556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
6751556Srgrimes			outc(' ', &errout);
6761556Srgrimes			out2str(sp->text);
6771556Srgrimes		}
6781556Srgrimes		outc('\n', &errout);
6791556Srgrimes		flushout(&errout);
6801556Srgrimes	}
6811556Srgrimes
6821556Srgrimes	/* Now locate the command. */
6831556Srgrimes	if (argc == 0) {
6841556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6851556Srgrimes		cmdentry.u.index = BLTINCMD;
6861556Srgrimes	} else {
68717987Speter		static const char PATH[] = "PATH=";
68817987Speter		char *path = pathval();
68917987Speter
69017987Speter		/*
69117987Speter		 * Modify the command lookup path, if a PATH= assignment
69217987Speter		 * is present
69317987Speter		 */
69417987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
69554884Scracauer			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
69617987Speter				path = sp->text + sizeof(PATH) - 1;
69754884Scracauer				/*
69854884Scracauer				 * On `PATH=... command`, we need to make
69954884Scracauer				 * sure that the command isn't using the
70054884Scracauer				 * non-updated hash table of the outer PATH
70154884Scracauer				 * setting and we need to make sure that
70254884Scracauer				 * the hash table isn't filled with items
70354884Scracauer				 * from the temporary setting.
70454884Scracauer				 *
70554884Scracauer				 * It would be better to forbit using and
70654884Scracauer				 * updating the table while this command
70754884Scracauer				 * runs, by the command finding mechanism
70854884Scracauer				 * is heavily integrated with hash handling,
70954884Scracauer				 * so we just delete the hash before and after
71054884Scracauer				 * the command runs. Partly deleting like
71154884Scracauer				 * changepatch() does doesn't seem worth the
71254884Scracauer				 * bookinging effort, since most such runs add
71354884Scracauer				 * diretories in front of the new PATH.
71454884Scracauer				 */
71554884Scracauer				clearcmdentry(0);
71654884Scracauer				do_clearcmdentry = 1;
71754884Scracauer			}
71817987Speter
71917987Speter		find_command(argv[0], &cmdentry, 1, path);
7201556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
72120425Ssteve			exitstatus = 127;
7221556Srgrimes			flushout(&errout);
7231556Srgrimes			return;
7241556Srgrimes		}
7251556Srgrimes		/* implement the bltin builtin here */
7261556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
7271556Srgrimes			for (;;) {
7281556Srgrimes				argv++;
7291556Srgrimes				if (--argc == 0)
7301556Srgrimes					break;
7311556Srgrimes				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
7321556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
73320425Ssteve					exitstatus = 127;
7341556Srgrimes					flushout(&errout);
7351556Srgrimes					return;
7361556Srgrimes				}
7371556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7381556Srgrimes					break;
7391556Srgrimes			}
7401556Srgrimes		}
7411556Srgrimes	}
7421556Srgrimes
7431556Srgrimes	/* Fork off a child process if necessary. */
7441556Srgrimes	if (cmd->ncmd.backgnd
74545221Scracauer	 || (cmdentry.cmdtype == CMDNORMAL
74645221Scracauer	    && ((flags & EV_EXIT) == 0 || Tflag))
74717987Speter	 || ((flags & EV_BACKCMD) != 0
7481556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
74948896Ssheldonh		 || cmdentry.u.index == CDCMD
7501556Srgrimes		 || cmdentry.u.index == DOTCMD
751100437Stjr		 || cmdentry.u.index == EVALCMD))
752100437Stjr	 || (cmdentry.cmdtype == CMDBUILTIN &&
753100437Stjr	    cmdentry.u.index == COMMANDCMD)) {
7541556Srgrimes		jp = makejob(cmd, 1);
7551556Srgrimes		mode = cmd->ncmd.backgnd;
7561556Srgrimes		if (flags & EV_BACKCMD) {
7571556Srgrimes			mode = FORK_NOJOB;
7581556Srgrimes			if (pipe(pip) < 0)
75953891Scracauer				error("Pipe call failed: %s", strerror(errno));
7601556Srgrimes		}
7611556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7621556Srgrimes			goto parent;	/* at end of routine */
7631556Srgrimes		if (flags & EV_BACKCMD) {
7641556Srgrimes			FORCEINTON;
7651556Srgrimes			close(pip[0]);
7661556Srgrimes			if (pip[1] != 1) {
7671556Srgrimes				close(1);
7681556Srgrimes				copyfd(pip[1], 1);
7691556Srgrimes				close(pip[1]);
7701556Srgrimes			}
7711556Srgrimes		}
7721556Srgrimes		flags |= EV_EXIT;
7731556Srgrimes	}
7741556Srgrimes
7751556Srgrimes	/* This is the child process if a fork occurred. */
7761556Srgrimes	/* Execute the command. */
7771556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
77820425Ssteve#ifdef DEBUG
7791556Srgrimes		trputs("Shell function:  ");  trargs(argv);
78020425Ssteve#endif
7811556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7821556Srgrimes		saveparam = shellparam;
7831556Srgrimes		shellparam.malloc = 0;
78420425Ssteve		shellparam.reset = 1;
7851556Srgrimes		shellparam.nparam = argc - 1;
7861556Srgrimes		shellparam.p = argv + 1;
7871556Srgrimes		shellparam.optnext = NULL;
7881556Srgrimes		INTOFF;
7891556Srgrimes		savelocalvars = localvars;
7901556Srgrimes		localvars = NULL;
7911556Srgrimes		INTON;
7921556Srgrimes		if (setjmp(jmploc.loc)) {
7931556Srgrimes			if (exception == EXSHELLPROC)
7941556Srgrimes				freeparam((struct shparam *)&saveparam);
7951556Srgrimes			else {
7961556Srgrimes				freeparam(&shellparam);
7971556Srgrimes				shellparam = saveparam;
7981556Srgrimes			}
7991556Srgrimes			poplocalvars();
8001556Srgrimes			localvars = savelocalvars;
8011556Srgrimes			handler = savehandler;
8021556Srgrimes			longjmp(handler->loc, 1);
8031556Srgrimes		}
8041556Srgrimes		savehandler = handler;
8051556Srgrimes		handler = &jmploc;
8061556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8071556Srgrimes			mklocal(sp->text);
8081556Srgrimes		funcnest++;
80935675Scracauer		if (flags & EV_TESTED)
81035675Scracauer			evaltree(cmdentry.u.func, EV_TESTED);
81135675Scracauer		else
81235675Scracauer			evaltree(cmdentry.u.func, 0);
8131556Srgrimes		funcnest--;
8141556Srgrimes		INTOFF;
8151556Srgrimes		poplocalvars();
8161556Srgrimes		localvars = savelocalvars;
8171556Srgrimes		freeparam(&shellparam);
8181556Srgrimes		shellparam = saveparam;
8191556Srgrimes		handler = savehandler;
8201556Srgrimes		popredir();
8211556Srgrimes		INTON;
8221556Srgrimes		if (evalskip == SKIPFUNC) {
8231556Srgrimes			evalskip = 0;
8241556Srgrimes			skipcount = 0;
8251556Srgrimes		}
8261556Srgrimes		if (flags & EV_EXIT)
8271556Srgrimes			exitshell(exitstatus);
8281556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
82920425Ssteve#ifdef DEBUG
8301556Srgrimes		trputs("builtin command:  ");  trargs(argv);
83120425Ssteve#endif
8321556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
8331556Srgrimes		if (flags == EV_BACKCMD) {
8341556Srgrimes			memout.nleft = 0;
8351556Srgrimes			memout.nextc = memout.buf;
8361556Srgrimes			memout.bufsize = 64;
8371556Srgrimes			mode |= REDIR_BACKQ;
8381556Srgrimes		}
8391556Srgrimes		redirect(cmd->ncmd.redirect, mode);
8401556Srgrimes		savecmdname = commandname;
8411556Srgrimes		cmdenviron = varlist.list;
8421556Srgrimes		e = -1;
8431556Srgrimes		if (setjmp(jmploc.loc)) {
8441556Srgrimes			e = exception;
8451556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8461556Srgrimes			goto cmddone;
8471556Srgrimes		}
8481556Srgrimes		savehandler = handler;
8491556Srgrimes		handler = &jmploc;
8501556Srgrimes		commandname = argv[0];
8511556Srgrimes		argptr = argv + 1;
8521556Srgrimes		optptr = NULL;			/* initialize nextopt */
8531556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8541556Srgrimes		flushall();
8551556Srgrimescmddone:
85660592Scracauer		cmdenviron = NULL;
8571556Srgrimes		out1 = &output;
8581556Srgrimes		out2 = &errout;
8591556Srgrimes		freestdout();
8601556Srgrimes		if (e != EXSHELLPROC) {
8611556Srgrimes			commandname = savecmdname;
8621556Srgrimes			if (flags & EV_EXIT) {
8631556Srgrimes				exitshell(exitstatus);
8641556Srgrimes			}
8651556Srgrimes		}
8661556Srgrimes		handler = savehandler;
8671556Srgrimes		if (e != -1) {
86820425Ssteve			if ((e != EXERROR && e != EXEXEC)
86920425Ssteve			   || cmdentry.u.index == BLTINCMD
87020425Ssteve			   || cmdentry.u.index == DOTCMD
87120425Ssteve			   || cmdentry.u.index == EVALCMD
87217987Speter#ifndef NO_HISTORY
87320425Ssteve			   || cmdentry.u.index == HISTCMD
87417987Speter#endif
875100437Stjr			   || cmdentry.u.index == EXECCMD
876100437Stjr			   || cmdentry.u.index == COMMANDCMD)
8771556Srgrimes				exraise(e);
8781556Srgrimes			FORCEINTON;
8791556Srgrimes		}
8801556Srgrimes		if (cmdentry.u.index != EXECCMD)
8811556Srgrimes			popredir();
8821556Srgrimes		if (flags == EV_BACKCMD) {
8831556Srgrimes			backcmd->buf = memout.buf;
8841556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8851556Srgrimes			memout.buf = NULL;
8861556Srgrimes		}
8871556Srgrimes	} else {
88820425Ssteve#ifdef DEBUG
8891556Srgrimes		trputs("normal command:  ");  trargs(argv);
89020425Ssteve#endif
8911556Srgrimes		clearredir();
8921556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8931556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8941556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8951556Srgrimes		envp = environment();
89617987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8971556Srgrimes		/*NOTREACHED*/
8981556Srgrimes	}
8991556Srgrimes	goto out;
9001556Srgrimes
9011556Srgrimesparent:	/* parent process gets here (if we forked) */
9021556Srgrimes	if (mode == 0) {	/* argument to fork */
9031556Srgrimes		INTOFF;
90445916Scracauer		exitstatus = waitforjob(jp, &realstatus);
9051556Srgrimes		INTON;
90645916Scracauer		if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
90745266Scracauer			evalskip = SKIPBREAK;
90845266Scracauer			skipcount = loopnest;
90945266Scracauer		}
9101556Srgrimes	} else if (mode == 2) {
9111556Srgrimes		backcmd->fd = pip[0];
9121556Srgrimes		close(pip[1]);
9131556Srgrimes		backcmd->jp = jp;
9141556Srgrimes	}
9151556Srgrimes
9161556Srgrimesout:
9171556Srgrimes	if (lastarg)
9181556Srgrimes		setvar("_", lastarg, 0);
91954884Scracauer	if (do_clearcmdentry)
92054884Scracauer		clearcmdentry(0);
9211556Srgrimes	popstackmark(&smark);
9221556Srgrimes}
9231556Srgrimes
9241556Srgrimes
9251556Srgrimes
9261556Srgrimes/*
9271556Srgrimes * Search for a command.  This is called before we fork so that the
9281556Srgrimes * location of the command will be available in the parent as well as
9291556Srgrimes * the child.  The check for "goodname" is an overly conservative
9301556Srgrimes * check that the name will not be subject to expansion.
9311556Srgrimes */
9321556Srgrimes
9331556SrgrimesSTATIC void
93490111Simpprehash(union node *n)
93517987Speter{
9361556Srgrimes	struct cmdentry entry;
9371556Srgrimes
93817987Speter	if (n->type == NCMD && n->ncmd.args)
93917987Speter		if (goodname(n->ncmd.args->narg.text))
94017987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
94117987Speter				     pathval());
9421556Srgrimes}
9431556Srgrimes
9441556Srgrimes
9451556Srgrimes
9461556Srgrimes/*
9471556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
9481556Srgrimes * tied to evaluation are implemented here.
9491556Srgrimes */
9501556Srgrimes
9511556Srgrimes/*
9521556Srgrimes * No command given, or a bltin command with no arguments.  Set the
9531556Srgrimes * specified variables.
9541556Srgrimes */
9551556Srgrimes
95617987Speterint
95790111Simpbltincmd(int argc __unused, char **argv __unused)
95817987Speter{
9591556Srgrimes	listsetvar(cmdenviron);
96020425Ssteve	/*
96117987Speter	 * Preserve exitstatus of a previous possible redirection
96220425Ssteve	 * as POSIX mandates
96317987Speter	 */
9641556Srgrimes	return exitstatus;
9651556Srgrimes}
9661556Srgrimes
9671556Srgrimes
9681556Srgrimes/*
9691556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9701556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9711556Srgrimes * above all check this flag, and if it is set they start skipping
9721556Srgrimes * commands rather than executing them.  The variable skipcount is
9731556Srgrimes * the number of loops to break/continue, or the number of function
9741556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9751556Srgrimes * be an error to break out of more loops than exist, but it isn't
9761556Srgrimes * in the standard shell so we don't make it one here.
9771556Srgrimes */
9781556Srgrimes
97917987Speterint
98090111Simpbreakcmd(int argc, char **argv)
98117987Speter{
98220425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
9831556Srgrimes
9841556Srgrimes	if (n > loopnest)
9851556Srgrimes		n = loopnest;
9861556Srgrimes	if (n > 0) {
9871556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9881556Srgrimes		skipcount = n;
9891556Srgrimes	}
9901556Srgrimes	return 0;
9911556Srgrimes}
9921556Srgrimes
993100437Stjr/*
994100437Stjr * The `command' command.
995100437Stjr */
996100437Stjrint
997100437Stjrcommandcmd(int argc, char **argv)
998100437Stjr{
999100437Stjr	static char stdpath[] = _PATH_STDPATH;
1000100437Stjr	struct jmploc loc, *old;
1001100437Stjr	struct strlist *sp;
1002100437Stjr	char *path;
1003100437Stjr	int ch;
10041556Srgrimes
1005100437Stjr	for (sp = cmdenviron; sp ; sp = sp->next)
1006100437Stjr		setvareq(sp->text, VEXPORT|VSTACK);
1007100437Stjr	path = pathval();
1008100437Stjr
1009100437Stjr	optind = optreset = 1;
1010100437Stjr	while ((ch = getopt(argc, argv, "p")) != -1) {
1011100437Stjr		switch (ch) {
1012100437Stjr		case 'p':
1013100437Stjr			path = stdpath;
1014100437Stjr			break;
1015100437Stjr		case '?':
1016100437Stjr		default:
1017100437Stjr			error("unknown option: -%c", optopt);
1018100437Stjr		}
1019100437Stjr	}
1020100437Stjr	argc -= optind;
1021100437Stjr	argv += optind;
1022100437Stjr
1023100437Stjr	if (argc != 0) {
1024100437Stjr		old = handler;
1025100437Stjr		handler = &loc;
1026100437Stjr		if (setjmp(handler->loc) == 0)
1027100437Stjr			shellexec(argv, environment(), path, 0);
1028100437Stjr		handler = old;
1029100437Stjr		if (exception == EXEXEC)
1030100437Stjr			exit(exerrno);
1031100437Stjr		exraise(exception);
1032100437Stjr	}
1033100437Stjr
1034100437Stjr	/*
1035100437Stjr	 * Do nothing successfully if no command was specified;
1036100437Stjr	 * ksh also does this.
1037100437Stjr	 */
1038100437Stjr	exit(0);
1039100437Stjr}
1040100437Stjr
1041100437Stjr
10421556Srgrimes/*
10431556Srgrimes * The return command.
10441556Srgrimes */
10451556Srgrimes
104617987Speterint
104790111Simpreturncmd(int argc, char **argv)
104817987Speter{
104920425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
10501556Srgrimes
10511556Srgrimes	if (funcnest) {
10521556Srgrimes		evalskip = SKIPFUNC;
10531556Srgrimes		skipcount = 1;
105420425Ssteve	} else {
105520425Ssteve		/* skip the rest of the file */
105620425Ssteve		evalskip = SKIPFILE;
105720425Ssteve		skipcount = 1;
10581556Srgrimes	}
10591556Srgrimes	return ret;
10601556Srgrimes}
10611556Srgrimes
10621556Srgrimes
106317987Speterint
106490111Simpfalsecmd(int argc __unused, char **argv __unused)
106517987Speter{
106617987Speter	return 1;
106717987Speter}
106817987Speter
106917987Speter
107017987Speterint
107190111Simptruecmd(int argc __unused, char **argv __unused)
107217987Speter{
10731556Srgrimes	return 0;
10741556Srgrimes}
10751556Srgrimes
10761556Srgrimes
107717987Speterint
107890111Simpexeccmd(int argc, char **argv)
107917987Speter{
10801556Srgrimes	if (argc > 1) {
108117987Speter		struct strlist *sp;
108217987Speter
10831556Srgrimes		iflag = 0;		/* exit on error */
10841556Srgrimes		mflag = 0;
10851556Srgrimes		optschanged();
108617987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
108717987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10881556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10891556Srgrimes
10901556Srgrimes	}
10911556Srgrimes	return 0;
10921556Srgrimes}
1093