eval.c revision 60592
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
4136150Scharnierstatic const char rcsid[] =
4250471Speter  "$FreeBSD: head/bin/sh/eval.c 60592 2000-05-15 12:33:17Z cracauer $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <signal.h>
4617987Speter#include <unistd.h>
4745266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */
4853891Scracauer#include <errno.h>
4917987Speter
501556Srgrimes/*
511556Srgrimes * Evaluate a command.
521556Srgrimes */
531556Srgrimes
541556Srgrimes#include "shell.h"
551556Srgrimes#include "nodes.h"
561556Srgrimes#include "syntax.h"
571556Srgrimes#include "expand.h"
581556Srgrimes#include "parser.h"
591556Srgrimes#include "jobs.h"
601556Srgrimes#include "eval.h"
611556Srgrimes#include "builtins.h"
621556Srgrimes#include "options.h"
631556Srgrimes#include "exec.h"
641556Srgrimes#include "redir.h"
651556Srgrimes#include "input.h"
661556Srgrimes#include "output.h"
671556Srgrimes#include "trap.h"
681556Srgrimes#include "var.h"
691556Srgrimes#include "memalloc.h"
701556Srgrimes#include "error.h"
7117987Speter#include "show.h"
721556Srgrimes#include "mystring.h"
7317987Speter#ifndef NO_HISTORY
741556Srgrimes#include "myhistedit.h"
7517987Speter#endif
761556Srgrimes
771556Srgrimes
781556Srgrimes/* flags in argument to evaltree */
791556Srgrimes#define EV_EXIT 01		/* exit after evaluating tree */
801556Srgrimes#define EV_TESTED 02		/* exit status is checked; ignore -e flag */
811556Srgrimes#define EV_BACKCMD 04		/* command executing within back quotes */
821556Srgrimes
831556SrgrimesMKINIT int evalskip;		/* set if we are skipping commands */
841556SrgrimesSTATIC int skipcount;		/* number of levels to skip */
851556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
861556Srgrimesint funcnest;			/* depth of function calls */
871556Srgrimes
881556Srgrimes
891556Srgrimeschar *commandname;
901556Srgrimesstruct strlist *cmdenviron;
911556Srgrimesint exitstatus;			/* exit status of last command */
9217987Speterint oexitstatus;		/* saved exit status */
931556Srgrimes
941556Srgrimes
9517987SpeterSTATIC void evalloop __P((union node *));
9617987SpeterSTATIC void evalfor __P((union node *));
9717987SpeterSTATIC void evalcase __P((union node *, int));
9817987SpeterSTATIC void evalsubshell __P((union node *, int));
9917987SpeterSTATIC void expredir __P((union node *));
10017987SpeterSTATIC void evalpipe __P((union node *));
10117987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *));
10217987SpeterSTATIC void prehash __P((union node *));
1031556Srgrimes
1041556Srgrimes
1051556Srgrimes/*
1061556Srgrimes * Called to reset things after an exception.
1071556Srgrimes */
1081556Srgrimes
1091556Srgrimes#ifdef mkinit
1101556SrgrimesINCLUDE "eval.h"
1111556Srgrimes
1121556SrgrimesRESET {
1131556Srgrimes	evalskip = 0;
1141556Srgrimes	loopnest = 0;
1151556Srgrimes	funcnest = 0;
1161556Srgrimes}
1171556Srgrimes
1181556SrgrimesSHELLPROC {
1191556Srgrimes	exitstatus = 0;
1201556Srgrimes}
1211556Srgrimes#endif
1221556Srgrimes
1231556Srgrimes
1241556Srgrimes
1251556Srgrimes/*
12646684Skris * The eval command.
1271556Srgrimes */
1281556Srgrimes
12917987Speterint
13020425Ssteveevalcmd(argc, argv)
13117987Speter	int argc;
13220425Ssteve	char **argv;
1331556Srgrimes{
1341556Srgrimes        char *p;
1351556Srgrimes        char *concat;
1361556Srgrimes        char **ap;
1371556Srgrimes
1381556Srgrimes        if (argc > 1) {
1391556Srgrimes                p = argv[1];
1401556Srgrimes                if (argc > 2) {
1411556Srgrimes                        STARTSTACKSTR(concat);
1421556Srgrimes                        ap = argv + 2;
1431556Srgrimes                        for (;;) {
1441556Srgrimes                                while (*p)
1451556Srgrimes                                        STPUTC(*p++, concat);
1461556Srgrimes                                if ((p = *ap++) == NULL)
1471556Srgrimes                                        break;
1481556Srgrimes                                STPUTC(' ', concat);
1491556Srgrimes                        }
1501556Srgrimes                        STPUTC('\0', concat);
1511556Srgrimes                        p = grabstackstr(concat);
1521556Srgrimes                }
1531556Srgrimes                evalstring(p);
1541556Srgrimes        }
1551556Srgrimes        return exitstatus;
1561556Srgrimes}
1571556Srgrimes
1581556Srgrimes
1591556Srgrimes/*
1601556Srgrimes * Execute a command or commands contained in a string.
1611556Srgrimes */
1621556Srgrimes
1631556Srgrimesvoid
1641556Srgrimesevalstring(s)
1651556Srgrimes	char *s;
1661556Srgrimes	{
1671556Srgrimes	union node *n;
1681556Srgrimes	struct stackmark smark;
1691556Srgrimes
1701556Srgrimes	setstackmark(&smark);
1711556Srgrimes	setinputstring(s, 1);
1721556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
1731556Srgrimes		evaltree(n, 0);
1741556Srgrimes		popstackmark(&smark);
1751556Srgrimes	}
1761556Srgrimes	popfile();
1771556Srgrimes	popstackmark(&smark);
1781556Srgrimes}
1791556Srgrimes
1801556Srgrimes
1811556Srgrimes
1821556Srgrimes/*
1831556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1841556Srgrimes * exitstatus.
1851556Srgrimes */
1861556Srgrimes
1871556Srgrimesvoid
1881556Srgrimesevaltree(n, flags)
1891556Srgrimes	union node *n;
19017987Speter	int flags;
19117987Speter{
1921556Srgrimes	if (n == NULL) {
1931556Srgrimes		TRACE(("evaltree(NULL) called\n"));
1941556Srgrimes		exitstatus = 0;
1951556Srgrimes		goto out;
1961556Srgrimes	}
19717987Speter#ifndef NO_HISTORY
1981556Srgrimes	displayhist = 1;	/* show history substitutions done with fc */
19917987Speter#endif
20017987Speter	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
2011556Srgrimes	switch (n->type) {
2021556Srgrimes	case NSEMI:
2031556Srgrimes		evaltree(n->nbinary.ch1, 0);
2041556Srgrimes		if (evalskip)
2051556Srgrimes			goto out;
2061556Srgrimes		evaltree(n->nbinary.ch2, flags);
2071556Srgrimes		break;
2081556Srgrimes	case NAND:
2091556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
21018754Ssteve		if (evalskip || exitstatus != 0) {
21118754Ssteve			flags |= EV_TESTED;
2121556Srgrimes			goto out;
21318754Ssteve		}
2141556Srgrimes		evaltree(n->nbinary.ch2, flags);
2151556Srgrimes		break;
2161556Srgrimes	case NOR:
2171556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2181556Srgrimes		if (evalskip || exitstatus == 0)
2191556Srgrimes			goto out;
2201556Srgrimes		evaltree(n->nbinary.ch2, flags);
2211556Srgrimes		break;
2221556Srgrimes	case NREDIR:
2231556Srgrimes		expredir(n->nredir.redirect);
2241556Srgrimes		redirect(n->nredir.redirect, REDIR_PUSH);
2251556Srgrimes		evaltree(n->nredir.n, flags);
2261556Srgrimes		popredir();
2271556Srgrimes		break;
2281556Srgrimes	case NSUBSHELL:
2291556Srgrimes		evalsubshell(n, flags);
2301556Srgrimes		break;
2311556Srgrimes	case NBACKGND:
2321556Srgrimes		evalsubshell(n, flags);
2331556Srgrimes		break;
2341556Srgrimes	case NIF: {
2351556Srgrimes		evaltree(n->nif.test, EV_TESTED);
2361556Srgrimes		if (evalskip)
2371556Srgrimes			goto out;
23820425Ssteve		if (exitstatus == 0)
2391556Srgrimes			evaltree(n->nif.ifpart, flags);
24017987Speter		else if (n->nif.elsepart)
2411556Srgrimes			evaltree(n->nif.elsepart, flags);
24220425Ssteve		else
24320425Ssteve			exitstatus = 0;
2441556Srgrimes		break;
2451556Srgrimes	}
2461556Srgrimes	case NWHILE:
2471556Srgrimes	case NUNTIL:
2481556Srgrimes		evalloop(n);
2491556Srgrimes		break;
2501556Srgrimes	case NFOR:
2511556Srgrimes		evalfor(n);
2521556Srgrimes		break;
2531556Srgrimes	case NCASE:
2541556Srgrimes		evalcase(n, flags);
2551556Srgrimes		break;
2561556Srgrimes	case NDEFUN:
2571556Srgrimes		defun(n->narg.text, n->narg.next);
2581556Srgrimes		exitstatus = 0;
2591556Srgrimes		break;
2601556Srgrimes	case NNOT:
2611556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2621556Srgrimes		exitstatus = !exitstatus;
2631556Srgrimes		break;
2641556Srgrimes
2651556Srgrimes	case NPIPE:
2661556Srgrimes		evalpipe(n);
2671556Srgrimes		break;
2681556Srgrimes	case NCMD:
2691556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
2701556Srgrimes		break;
2711556Srgrimes	default:
2721556Srgrimes		out1fmt("Node type = %d\n", n->type);
2731556Srgrimes		flushout(&output);
2741556Srgrimes		break;
2751556Srgrimes	}
2761556Srgrimesout:
2771556Srgrimes	if (pendingsigs)
2781556Srgrimes		dotrap();
27952526Scracauer	/*
28052526Scracauer	 * XXX - Like "!(n->type == NSEMI)", more types will probably
28152526Scracauer	 * need to be excluded from this test. It's probably better
28252526Scracauer	 * to set or unset EV_TESTED in the loop above than to bloat
28352526Scracauer	 * the conditional here.
28452526Scracauer	 */
28552526Scracauer	if ((flags & EV_EXIT) || (eflag && exitstatus
28652526Scracauer	    && !(flags & EV_TESTED) && !(n->type == NSEMI)))
2871556Srgrimes		exitshell(exitstatus);
2881556Srgrimes}
2891556Srgrimes
2901556Srgrimes
2911556SrgrimesSTATIC void
2921556Srgrimesevalloop(n)
2931556Srgrimes	union node *n;
29417987Speter{
2951556Srgrimes	int status;
2961556Srgrimes
2971556Srgrimes	loopnest++;
2981556Srgrimes	status = 0;
2991556Srgrimes	for (;;) {
3001556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
3011556Srgrimes		if (evalskip) {
3021556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
3031556Srgrimes				evalskip = 0;
3041556Srgrimes				continue;
3051556Srgrimes			}
3061556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3071556Srgrimes				evalskip = 0;
3081556Srgrimes			break;
3091556Srgrimes		}
3101556Srgrimes		if (n->type == NWHILE) {
3111556Srgrimes			if (exitstatus != 0)
3121556Srgrimes				break;
3131556Srgrimes		} else {
3141556Srgrimes			if (exitstatus == 0)
3151556Srgrimes				break;
3161556Srgrimes		}
3171556Srgrimes		evaltree(n->nbinary.ch2, 0);
3181556Srgrimes		status = exitstatus;
3191556Srgrimes		if (evalskip)
3201556Srgrimes			goto skipping;
3211556Srgrimes	}
3221556Srgrimes	loopnest--;
3231556Srgrimes	exitstatus = status;
3241556Srgrimes}
3251556Srgrimes
3261556Srgrimes
3271556Srgrimes
3281556SrgrimesSTATIC void
3291556Srgrimesevalfor(n)
33017987Speter    union node *n;
33117987Speter{
3321556Srgrimes	struct arglist arglist;
3331556Srgrimes	union node *argp;
3341556Srgrimes	struct strlist *sp;
3351556Srgrimes	struct stackmark smark;
3361556Srgrimes
3371556Srgrimes	setstackmark(&smark);
3381556Srgrimes	arglist.lastp = &arglist.list;
3391556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
34017987Speter		oexitstatus = exitstatus;
3411556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3421556Srgrimes		if (evalskip)
3431556Srgrimes			goto out;
3441556Srgrimes	}
3451556Srgrimes	*arglist.lastp = NULL;
3461556Srgrimes
3471556Srgrimes	exitstatus = 0;
3481556Srgrimes	loopnest++;
3491556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3501556Srgrimes		setvar(n->nfor.var, sp->text, 0);
3511556Srgrimes		evaltree(n->nfor.body, 0);
3521556Srgrimes		if (evalskip) {
3531556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3541556Srgrimes				evalskip = 0;
3551556Srgrimes				continue;
3561556Srgrimes			}
3571556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3581556Srgrimes				evalskip = 0;
3591556Srgrimes			break;
3601556Srgrimes		}
3611556Srgrimes	}
3621556Srgrimes	loopnest--;
3631556Srgrimesout:
3641556Srgrimes	popstackmark(&smark);
3651556Srgrimes}
3661556Srgrimes
3671556Srgrimes
3681556Srgrimes
3691556SrgrimesSTATIC void
3701556Srgrimesevalcase(n, flags)
3711556Srgrimes	union node *n;
37217987Speter	int flags;
37317987Speter{
3741556Srgrimes	union node *cp;
3751556Srgrimes	union node *patp;
3761556Srgrimes	struct arglist arglist;
3771556Srgrimes	struct stackmark smark;
3781556Srgrimes
3791556Srgrimes	setstackmark(&smark);
3801556Srgrimes	arglist.lastp = &arglist.list;
38117987Speter	oexitstatus = exitstatus;
3821556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3831556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3841556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3851556Srgrimes			if (casematch(patp, arglist.list->text)) {
3861556Srgrimes				if (evalskip == 0) {
3871556Srgrimes					evaltree(cp->nclist.body, flags);
3881556Srgrimes				}
3891556Srgrimes				goto out;
3901556Srgrimes			}
3911556Srgrimes		}
3921556Srgrimes	}
3931556Srgrimesout:
3941556Srgrimes	popstackmark(&smark);
3951556Srgrimes}
3961556Srgrimes
3971556Srgrimes
3981556Srgrimes
3991556Srgrimes/*
4001556Srgrimes * Kick off a subshell to evaluate a tree.
4011556Srgrimes */
4021556Srgrimes
4031556SrgrimesSTATIC void
4041556Srgrimesevalsubshell(n, flags)
4051556Srgrimes	union node *n;
40617987Speter	int flags;
40717987Speter{
4081556Srgrimes	struct job *jp;
4091556Srgrimes	int backgnd = (n->type == NBACKGND);
4101556Srgrimes
4111556Srgrimes	expredir(n->nredir.redirect);
4121556Srgrimes	jp = makejob(n, 1);
4131556Srgrimes	if (forkshell(jp, n, backgnd) == 0) {
4141556Srgrimes		if (backgnd)
4151556Srgrimes			flags &=~ EV_TESTED;
4161556Srgrimes		redirect(n->nredir.redirect, 0);
4171556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
4181556Srgrimes	}
4191556Srgrimes	if (! backgnd) {
4201556Srgrimes		INTOFF;
42145916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
4221556Srgrimes		INTON;
4231556Srgrimes	}
4241556Srgrimes}
4251556Srgrimes
4261556Srgrimes
4271556Srgrimes
4281556Srgrimes/*
4291556Srgrimes * Compute the names of the files in a redirection list.
4301556Srgrimes */
4311556Srgrimes
4321556SrgrimesSTATIC void
4331556Srgrimesexpredir(n)
4341556Srgrimes	union node *n;
43517987Speter{
43625222Ssteve	union node *redir;
4371556Srgrimes
4381556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
43917987Speter		struct arglist fn;
44017987Speter		fn.lastp = &fn.list;
44117987Speter		oexitstatus = exitstatus;
44217987Speter		switch (redir->type) {
44317987Speter		case NFROM:
44417987Speter		case NTO:
44517987Speter		case NAPPEND:
4461556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4471556Srgrimes			redir->nfile.expfname = fn.list->text;
44817987Speter			break;
44917987Speter		case NFROMFD:
45017987Speter		case NTOFD:
45117987Speter			if (redir->ndup.vname) {
45217987Speter				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
45317987Speter				fixredir(redir, fn.list->text, 1);
45417987Speter			}
45517987Speter			break;
4561556Srgrimes		}
4571556Srgrimes	}
4581556Srgrimes}
4591556Srgrimes
4601556Srgrimes
4611556Srgrimes
4621556Srgrimes/*
4631556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
4641556Srgrimes * of the process creating the pipeline.  (This differs from some versions
4651556Srgrimes * of the shell, which make the last process in a pipeline the parent
4661556Srgrimes * of all the rest.)
4671556Srgrimes */
4681556Srgrimes
4691556SrgrimesSTATIC void
4701556Srgrimesevalpipe(n)
4711556Srgrimes	union node *n;
47217987Speter{
4731556Srgrimes	struct job *jp;
4741556Srgrimes	struct nodelist *lp;
4751556Srgrimes	int pipelen;
4761556Srgrimes	int prevfd;
4771556Srgrimes	int pip[2];
4781556Srgrimes
47917987Speter	TRACE(("evalpipe(0x%lx) called\n", (long)n));
4801556Srgrimes	pipelen = 0;
4811556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
4821556Srgrimes		pipelen++;
4831556Srgrimes	INTOFF;
4841556Srgrimes	jp = makejob(n, pipelen);
4851556Srgrimes	prevfd = -1;
4861556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
4871556Srgrimes		prehash(lp->n);
4881556Srgrimes		pip[1] = -1;
4891556Srgrimes		if (lp->next) {
4901556Srgrimes			if (pipe(pip) < 0) {
4911556Srgrimes				close(prevfd);
49253891Scracauer				error("Pipe call failed: %s", strerror(errno));
4931556Srgrimes			}
4941556Srgrimes		}
4951556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
4961556Srgrimes			INTON;
4971556Srgrimes			if (prevfd > 0) {
4981556Srgrimes				close(0);
4991556Srgrimes				copyfd(prevfd, 0);
5001556Srgrimes				close(prevfd);
5011556Srgrimes			}
5021556Srgrimes			if (pip[1] >= 0) {
50353282Scracauer				if (!(prevfd >= 0 && pip[0] == 0))
50452900Scracauer					close(pip[0]);
5051556Srgrimes				if (pip[1] != 1) {
5061556Srgrimes					close(1);
5071556Srgrimes					copyfd(pip[1], 1);
5081556Srgrimes					close(pip[1]);
5091556Srgrimes				}
5101556Srgrimes			}
5111556Srgrimes			evaltree(lp->n, EV_EXIT);
5121556Srgrimes		}
5131556Srgrimes		if (prevfd >= 0)
5141556Srgrimes			close(prevfd);
5151556Srgrimes		prevfd = pip[0];
5161556Srgrimes		close(pip[1]);
5171556Srgrimes	}
5181556Srgrimes	INTON;
5191556Srgrimes	if (n->npipe.backgnd == 0) {
5201556Srgrimes		INTOFF;
52145916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
5221556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5231556Srgrimes		INTON;
5241556Srgrimes	}
5251556Srgrimes}
5261556Srgrimes
5271556Srgrimes
5281556Srgrimes
5291556Srgrimes/*
5301556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5311556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5321556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5331556Srgrimes * Should be called with interrupts off.
5341556Srgrimes */
5351556Srgrimes
5361556Srgrimesvoid
5371556Srgrimesevalbackcmd(n, result)
5381556Srgrimes	union node *n;
5391556Srgrimes	struct backcmd *result;
54017987Speter{
5411556Srgrimes	int pip[2];
5421556Srgrimes	struct job *jp;
5431556Srgrimes	struct stackmark smark;		/* unnecessary */
5441556Srgrimes
5451556Srgrimes	setstackmark(&smark);
5461556Srgrimes	result->fd = -1;
5471556Srgrimes	result->buf = NULL;
5481556Srgrimes	result->nleft = 0;
5491556Srgrimes	result->jp = NULL;
55017987Speter	if (n == NULL) {
55117987Speter		exitstatus = 0;
5521556Srgrimes		goto out;
55317987Speter	}
5541556Srgrimes	if (n->type == NCMD) {
55517987Speter		exitstatus = oexitstatus;
5561556Srgrimes		evalcommand(n, EV_BACKCMD, result);
5571556Srgrimes	} else {
55817987Speter		exitstatus = 0;
5591556Srgrimes		if (pipe(pip) < 0)
56053891Scracauer			error("Pipe call failed: %s", strerror(errno));
5611556Srgrimes		jp = makejob(n, 1);
5621556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
5631556Srgrimes			FORCEINTON;
5641556Srgrimes			close(pip[0]);
5651556Srgrimes			if (pip[1] != 1) {
5661556Srgrimes				close(1);
5671556Srgrimes				copyfd(pip[1], 1);
5681556Srgrimes				close(pip[1]);
5691556Srgrimes			}
5701556Srgrimes			evaltree(n, EV_EXIT);
5711556Srgrimes		}
5721556Srgrimes		close(pip[1]);
5731556Srgrimes		result->fd = pip[0];
5741556Srgrimes		result->jp = jp;
5751556Srgrimes	}
5761556Srgrimesout:
5771556Srgrimes	popstackmark(&smark);
5781556Srgrimes	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
5791556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
5801556Srgrimes}
5811556Srgrimes
5821556Srgrimes
5831556Srgrimes
5841556Srgrimes/*
5851556Srgrimes * Execute a simple command.
5861556Srgrimes */
5871556Srgrimes
5881556SrgrimesSTATIC void
5891556Srgrimesevalcommand(cmd, flags, backcmd)
5901556Srgrimes	union node *cmd;
59117987Speter	int flags;
5921556Srgrimes	struct backcmd *backcmd;
59317987Speter{
5941556Srgrimes	struct stackmark smark;
5951556Srgrimes	union node *argp;
5961556Srgrimes	struct arglist arglist;
5971556Srgrimes	struct arglist varlist;
5981556Srgrimes	char **argv;
5991556Srgrimes	int argc;
6001556Srgrimes	char **envp;
6011556Srgrimes	int varflag;
6021556Srgrimes	struct strlist *sp;
6031556Srgrimes	int mode;
6041556Srgrimes	int pip[2];
6051556Srgrimes	struct cmdentry cmdentry;
6061556Srgrimes	struct job *jp;
6071556Srgrimes	struct jmploc jmploc;
6081556Srgrimes	struct jmploc *volatile savehandler;
6091556Srgrimes	char *volatile savecmdname;
6101556Srgrimes	volatile struct shparam saveparam;
6111556Srgrimes	struct localvar *volatile savelocalvars;
6121556Srgrimes	volatile int e;
6131556Srgrimes	char *lastarg;
61445916Scracauer	int realstatus;
61554884Scracauer	int do_clearcmdentry;
61617987Speter#if __GNUC__
61717987Speter	/* Avoid longjmp clobbering */
61817987Speter	(void) &argv;
61917987Speter	(void) &argc;
62017987Speter	(void) &lastarg;
62117987Speter	(void) &flags;
62254884Scracauer	(void) &do_clearcmdentry;
62317987Speter#endif
6241556Srgrimes
6251556Srgrimes	/* First expand the arguments. */
62617987Speter	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
6271556Srgrimes	setstackmark(&smark);
6281556Srgrimes	arglist.lastp = &arglist.list;
6291556Srgrimes	varlist.lastp = &varlist.list;
6301556Srgrimes	varflag = 1;
63154884Scracauer	do_clearcmdentry = 0;
63217987Speter	oexitstatus = exitstatus;
63317987Speter	exitstatus = 0;
6341556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
63517987Speter		char *p = argp->narg.text;
6361556Srgrimes		if (varflag && is_name(*p)) {
6371556Srgrimes			do {
6381556Srgrimes				p++;
6391556Srgrimes			} while (is_in_name(*p));
6401556Srgrimes			if (*p == '=') {
6411556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6421556Srgrimes				continue;
6431556Srgrimes			}
6441556Srgrimes		}
6451556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6461556Srgrimes		varflag = 0;
6471556Srgrimes	}
6481556Srgrimes	*arglist.lastp = NULL;
6491556Srgrimes	*varlist.lastp = NULL;
6501556Srgrimes	expredir(cmd->ncmd.redirect);
6511556Srgrimes	argc = 0;
6521556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6531556Srgrimes		argc++;
6541556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6551556Srgrimes
6561556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6571556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6581556Srgrimes		*argv++ = sp->text;
6591556Srgrimes	}
6601556Srgrimes	*argv = NULL;
6611556Srgrimes	lastarg = NULL;
6621556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6631556Srgrimes		lastarg = argv[-1];
6641556Srgrimes	argv -= argc;
6651556Srgrimes
6661556Srgrimes	/* Print the command if xflag is set. */
6671556Srgrimes	if (xflag) {
6681556Srgrimes		outc('+', &errout);
6691556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
6701556Srgrimes			outc(' ', &errout);
6711556Srgrimes			out2str(sp->text);
6721556Srgrimes		}
6731556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
6741556Srgrimes			outc(' ', &errout);
6751556Srgrimes			out2str(sp->text);
6761556Srgrimes		}
6771556Srgrimes		outc('\n', &errout);
6781556Srgrimes		flushout(&errout);
6791556Srgrimes	}
6801556Srgrimes
6811556Srgrimes	/* Now locate the command. */
6821556Srgrimes	if (argc == 0) {
6831556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6841556Srgrimes		cmdentry.u.index = BLTINCMD;
6851556Srgrimes	} else {
68617987Speter		static const char PATH[] = "PATH=";
68717987Speter		char *path = pathval();
68817987Speter
68917987Speter		/*
69017987Speter		 * Modify the command lookup path, if a PATH= assignment
69117987Speter		 * is present
69217987Speter		 */
69317987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
69454884Scracauer			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
69517987Speter				path = sp->text + sizeof(PATH) - 1;
69654884Scracauer				/*
69754884Scracauer				 * On `PATH=... command`, we need to make
69854884Scracauer				 * sure that the command isn't using the
69954884Scracauer				 * non-updated hash table of the outer PATH
70054884Scracauer				 * setting and we need to make sure that
70154884Scracauer				 * the hash table isn't filled with items
70254884Scracauer				 * from the temporary setting.
70354884Scracauer				 *
70454884Scracauer				 * It would be better to forbit using and
70554884Scracauer				 * updating the table while this command
70654884Scracauer				 * runs, by the command finding mechanism
70754884Scracauer				 * is heavily integrated with hash handling,
70854884Scracauer				 * so we just delete the hash before and after
70954884Scracauer				 * the command runs. Partly deleting like
71054884Scracauer				 * changepatch() does doesn't seem worth the
71154884Scracauer				 * bookinging effort, since most such runs add
71254884Scracauer				 * diretories in front of the new PATH.
71354884Scracauer				 */
71454884Scracauer				clearcmdentry(0);
71554884Scracauer				do_clearcmdentry = 1;
71654884Scracauer			}
71717987Speter
71817987Speter		find_command(argv[0], &cmdentry, 1, path);
7191556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
72020425Ssteve			exitstatus = 127;
7211556Srgrimes			flushout(&errout);
7221556Srgrimes			return;
7231556Srgrimes		}
7241556Srgrimes		/* implement the bltin builtin here */
7251556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
7261556Srgrimes			for (;;) {
7271556Srgrimes				argv++;
7281556Srgrimes				if (--argc == 0)
7291556Srgrimes					break;
7301556Srgrimes				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
7311556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
73220425Ssteve					exitstatus = 127;
7331556Srgrimes					flushout(&errout);
7341556Srgrimes					return;
7351556Srgrimes				}
7361556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7371556Srgrimes					break;
7381556Srgrimes			}
7391556Srgrimes		}
7401556Srgrimes	}
7411556Srgrimes
7421556Srgrimes	/* Fork off a child process if necessary. */
7431556Srgrimes	if (cmd->ncmd.backgnd
74445221Scracauer	 || (cmdentry.cmdtype == CMDNORMAL
74545221Scracauer	    && ((flags & EV_EXIT) == 0 || Tflag))
74617987Speter	 || ((flags & EV_BACKCMD) != 0
7471556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
74848896Ssheldonh		 || cmdentry.u.index == CDCMD
7491556Srgrimes		 || cmdentry.u.index == DOTCMD
75017987Speter		 || cmdentry.u.index == EVALCMD))) {
7511556Srgrimes		jp = makejob(cmd, 1);
7521556Srgrimes		mode = cmd->ncmd.backgnd;
7531556Srgrimes		if (flags & EV_BACKCMD) {
7541556Srgrimes			mode = FORK_NOJOB;
7551556Srgrimes			if (pipe(pip) < 0)
75653891Scracauer				error("Pipe call failed: %s", strerror(errno));
7571556Srgrimes		}
7581556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7591556Srgrimes			goto parent;	/* at end of routine */
7601556Srgrimes		if (flags & EV_BACKCMD) {
7611556Srgrimes			FORCEINTON;
7621556Srgrimes			close(pip[0]);
7631556Srgrimes			if (pip[1] != 1) {
7641556Srgrimes				close(1);
7651556Srgrimes				copyfd(pip[1], 1);
7661556Srgrimes				close(pip[1]);
7671556Srgrimes			}
7681556Srgrimes		}
7691556Srgrimes		flags |= EV_EXIT;
7701556Srgrimes	}
7711556Srgrimes
7721556Srgrimes	/* This is the child process if a fork occurred. */
7731556Srgrimes	/* Execute the command. */
7741556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
77520425Ssteve#ifdef DEBUG
7761556Srgrimes		trputs("Shell function:  ");  trargs(argv);
77720425Ssteve#endif
7781556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7791556Srgrimes		saveparam = shellparam;
7801556Srgrimes		shellparam.malloc = 0;
78120425Ssteve		shellparam.reset = 1;
7821556Srgrimes		shellparam.nparam = argc - 1;
7831556Srgrimes		shellparam.p = argv + 1;
7841556Srgrimes		shellparam.optnext = NULL;
7851556Srgrimes		INTOFF;
7861556Srgrimes		savelocalvars = localvars;
7871556Srgrimes		localvars = NULL;
7881556Srgrimes		INTON;
7891556Srgrimes		if (setjmp(jmploc.loc)) {
7901556Srgrimes			if (exception == EXSHELLPROC)
7911556Srgrimes				freeparam((struct shparam *)&saveparam);
7921556Srgrimes			else {
7931556Srgrimes				freeparam(&shellparam);
7941556Srgrimes				shellparam = saveparam;
7951556Srgrimes			}
7961556Srgrimes			poplocalvars();
7971556Srgrimes			localvars = savelocalvars;
7981556Srgrimes			handler = savehandler;
7991556Srgrimes			longjmp(handler->loc, 1);
8001556Srgrimes		}
8011556Srgrimes		savehandler = handler;
8021556Srgrimes		handler = &jmploc;
8031556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8041556Srgrimes			mklocal(sp->text);
8051556Srgrimes		funcnest++;
80635675Scracauer		if (flags & EV_TESTED)
80735675Scracauer			evaltree(cmdentry.u.func, EV_TESTED);
80835675Scracauer		else
80935675Scracauer			evaltree(cmdentry.u.func, 0);
8101556Srgrimes		funcnest--;
8111556Srgrimes		INTOFF;
8121556Srgrimes		poplocalvars();
8131556Srgrimes		localvars = savelocalvars;
8141556Srgrimes		freeparam(&shellparam);
8151556Srgrimes		shellparam = saveparam;
8161556Srgrimes		handler = savehandler;
8171556Srgrimes		popredir();
8181556Srgrimes		INTON;
8191556Srgrimes		if (evalskip == SKIPFUNC) {
8201556Srgrimes			evalskip = 0;
8211556Srgrimes			skipcount = 0;
8221556Srgrimes		}
8231556Srgrimes		if (flags & EV_EXIT)
8241556Srgrimes			exitshell(exitstatus);
8251556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
82620425Ssteve#ifdef DEBUG
8271556Srgrimes		trputs("builtin command:  ");  trargs(argv);
82820425Ssteve#endif
8291556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
8301556Srgrimes		if (flags == EV_BACKCMD) {
8311556Srgrimes			memout.nleft = 0;
8321556Srgrimes			memout.nextc = memout.buf;
8331556Srgrimes			memout.bufsize = 64;
8341556Srgrimes			mode |= REDIR_BACKQ;
8351556Srgrimes		}
8361556Srgrimes		redirect(cmd->ncmd.redirect, mode);
8371556Srgrimes		savecmdname = commandname;
8381556Srgrimes		cmdenviron = varlist.list;
8391556Srgrimes		e = -1;
8401556Srgrimes		if (setjmp(jmploc.loc)) {
8411556Srgrimes			e = exception;
8421556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8431556Srgrimes			goto cmddone;
8441556Srgrimes		}
8451556Srgrimes		savehandler = handler;
8461556Srgrimes		handler = &jmploc;
8471556Srgrimes		commandname = argv[0];
8481556Srgrimes		argptr = argv + 1;
8491556Srgrimes		optptr = NULL;			/* initialize nextopt */
8501556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8511556Srgrimes		flushall();
8521556Srgrimescmddone:
85360592Scracauer		cmdenviron = NULL;
8541556Srgrimes		out1 = &output;
8551556Srgrimes		out2 = &errout;
8561556Srgrimes		freestdout();
8571556Srgrimes		if (e != EXSHELLPROC) {
8581556Srgrimes			commandname = savecmdname;
8591556Srgrimes			if (flags & EV_EXIT) {
8601556Srgrimes				exitshell(exitstatus);
8611556Srgrimes			}
8621556Srgrimes		}
8631556Srgrimes		handler = savehandler;
8641556Srgrimes		if (e != -1) {
86520425Ssteve			if ((e != EXERROR && e != EXEXEC)
86620425Ssteve			   || cmdentry.u.index == BLTINCMD
86720425Ssteve			   || cmdentry.u.index == DOTCMD
86820425Ssteve			   || cmdentry.u.index == EVALCMD
86917987Speter#ifndef NO_HISTORY
87020425Ssteve			   || cmdentry.u.index == HISTCMD
87117987Speter#endif
87220425Ssteve			   || cmdentry.u.index == EXECCMD)
8731556Srgrimes				exraise(e);
8741556Srgrimes			FORCEINTON;
8751556Srgrimes		}
8761556Srgrimes		if (cmdentry.u.index != EXECCMD)
8771556Srgrimes			popredir();
8781556Srgrimes		if (flags == EV_BACKCMD) {
8791556Srgrimes			backcmd->buf = memout.buf;
8801556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8811556Srgrimes			memout.buf = NULL;
8821556Srgrimes		}
8831556Srgrimes	} else {
88420425Ssteve#ifdef DEBUG
8851556Srgrimes		trputs("normal command:  ");  trargs(argv);
88620425Ssteve#endif
8871556Srgrimes		clearredir();
8881556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8891556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8901556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8911556Srgrimes		envp = environment();
89217987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8931556Srgrimes		/*NOTREACHED*/
8941556Srgrimes	}
8951556Srgrimes	goto out;
8961556Srgrimes
8971556Srgrimesparent:	/* parent process gets here (if we forked) */
8981556Srgrimes	if (mode == 0) {	/* argument to fork */
8991556Srgrimes		INTOFF;
90045916Scracauer		exitstatus = waitforjob(jp, &realstatus);
9011556Srgrimes		INTON;
90245916Scracauer		if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
90345266Scracauer			evalskip = SKIPBREAK;
90445266Scracauer			skipcount = loopnest;
90545266Scracauer		}
9061556Srgrimes	} else if (mode == 2) {
9071556Srgrimes		backcmd->fd = pip[0];
9081556Srgrimes		close(pip[1]);
9091556Srgrimes		backcmd->jp = jp;
9101556Srgrimes	}
9111556Srgrimes
9121556Srgrimesout:
9131556Srgrimes	if (lastarg)
9141556Srgrimes		setvar("_", lastarg, 0);
91554884Scracauer	if (do_clearcmdentry)
91654884Scracauer		clearcmdentry(0);
9171556Srgrimes	popstackmark(&smark);
9181556Srgrimes}
9191556Srgrimes
9201556Srgrimes
9211556Srgrimes
9221556Srgrimes/*
9231556Srgrimes * Search for a command.  This is called before we fork so that the
9241556Srgrimes * location of the command will be available in the parent as well as
9251556Srgrimes * the child.  The check for "goodname" is an overly conservative
9261556Srgrimes * check that the name will not be subject to expansion.
9271556Srgrimes */
9281556Srgrimes
9291556SrgrimesSTATIC void
9301556Srgrimesprehash(n)
9311556Srgrimes	union node *n;
93217987Speter{
9331556Srgrimes	struct cmdentry entry;
9341556Srgrimes
93517987Speter	if (n->type == NCMD && n->ncmd.args)
93617987Speter		if (goodname(n->ncmd.args->narg.text))
93717987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
93817987Speter				     pathval());
9391556Srgrimes}
9401556Srgrimes
9411556Srgrimes
9421556Srgrimes
9431556Srgrimes/*
9441556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
9451556Srgrimes * tied to evaluation are implemented here.
9461556Srgrimes */
9471556Srgrimes
9481556Srgrimes/*
9491556Srgrimes * No command given, or a bltin command with no arguments.  Set the
9501556Srgrimes * specified variables.
9511556Srgrimes */
9521556Srgrimes
95317987Speterint
95417987Speterbltincmd(argc, argv)
95525905Ssteve	int argc __unused;
95625905Ssteve	char **argv __unused;
95717987Speter{
9581556Srgrimes	listsetvar(cmdenviron);
95920425Ssteve	/*
96017987Speter	 * Preserve exitstatus of a previous possible redirection
96120425Ssteve	 * as POSIX mandates
96217987Speter	 */
9631556Srgrimes	return exitstatus;
9641556Srgrimes}
9651556Srgrimes
9661556Srgrimes
9671556Srgrimes/*
9681556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9691556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9701556Srgrimes * above all check this flag, and if it is set they start skipping
9711556Srgrimes * commands rather than executing them.  The variable skipcount is
9721556Srgrimes * the number of loops to break/continue, or the number of function
9731556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9741556Srgrimes * be an error to break out of more loops than exist, but it isn't
9751556Srgrimes * in the standard shell so we don't make it one here.
9761556Srgrimes */
9771556Srgrimes
97817987Speterint
97917987Speterbreakcmd(argc, argv)
98017987Speter	int argc;
98120425Ssteve	char **argv;
98217987Speter{
98320425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
9841556Srgrimes
9851556Srgrimes	if (n > loopnest)
9861556Srgrimes		n = loopnest;
9871556Srgrimes	if (n > 0) {
9881556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9891556Srgrimes		skipcount = n;
9901556Srgrimes	}
9911556Srgrimes	return 0;
9921556Srgrimes}
9931556Srgrimes
9941556Srgrimes
9951556Srgrimes/*
9961556Srgrimes * The return command.
9971556Srgrimes */
9981556Srgrimes
99917987Speterint
100020425Sstevereturncmd(argc, argv)
100117987Speter	int argc;
100220425Ssteve	char **argv;
100317987Speter{
100420425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
10051556Srgrimes
10061556Srgrimes	if (funcnest) {
10071556Srgrimes		evalskip = SKIPFUNC;
10081556Srgrimes		skipcount = 1;
100920425Ssteve	} else {
101020425Ssteve		/* skip the rest of the file */
101120425Ssteve		evalskip = SKIPFILE;
101220425Ssteve		skipcount = 1;
10131556Srgrimes	}
10141556Srgrimes	return ret;
10151556Srgrimes}
10161556Srgrimes
10171556Srgrimes
101817987Speterint
101920425Sstevefalsecmd(argc, argv)
102025905Ssteve	int argc __unused;
102125905Ssteve	char **argv __unused;
102217987Speter{
102317987Speter	return 1;
102417987Speter}
102517987Speter
102617987Speter
102717987Speterint
102820425Sstevetruecmd(argc, argv)
102925905Ssteve	int argc __unused;
103025905Ssteve	char **argv __unused;
103117987Speter{
10321556Srgrimes	return 0;
10331556Srgrimes}
10341556Srgrimes
10351556Srgrimes
103617987Speterint
103720425Ssteveexeccmd(argc, argv)
103817987Speter	int argc;
103920425Ssteve	char **argv;
104017987Speter{
10411556Srgrimes	if (argc > 1) {
104217987Speter		struct strlist *sp;
104317987Speter
10441556Srgrimes		iflag = 0;		/* exit on error */
10451556Srgrimes		mflag = 0;
10461556Srgrimes		optschanged();
104717987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
104817987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10491556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10501556Srgrimes
10511556Srgrimes	}
10521556Srgrimes	return 0;
10531556Srgrimes}
1054