eval.c revision 20425
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.
353044Sdg *
3620425Ssteve *	$Id: eval.c,v 1.8 1996/11/12 18:35:06 peter Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4020425Sstevestatic char const sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
411556Srgrimes#endif /* not lint */
421556Srgrimes
4317987Speter#include <signal.h>
4417987Speter#include <unistd.h>
4517987Speter
461556Srgrimes/*
471556Srgrimes * Evaluate a command.
481556Srgrimes */
491556Srgrimes
501556Srgrimes#include "shell.h"
511556Srgrimes#include "nodes.h"
521556Srgrimes#include "syntax.h"
531556Srgrimes#include "expand.h"
541556Srgrimes#include "parser.h"
551556Srgrimes#include "jobs.h"
561556Srgrimes#include "eval.h"
571556Srgrimes#include "builtins.h"
581556Srgrimes#include "options.h"
591556Srgrimes#include "exec.h"
601556Srgrimes#include "redir.h"
611556Srgrimes#include "input.h"
621556Srgrimes#include "output.h"
631556Srgrimes#include "trap.h"
641556Srgrimes#include "var.h"
651556Srgrimes#include "memalloc.h"
661556Srgrimes#include "error.h"
6717987Speter#include "show.h"
681556Srgrimes#include "mystring.h"
6917987Speter#ifndef NO_HISTORY
701556Srgrimes#include "myhistedit.h"
7117987Speter#endif
721556Srgrimes
731556Srgrimes
741556Srgrimes/* flags in argument to evaltree */
751556Srgrimes#define EV_EXIT 01		/* exit after evaluating tree */
761556Srgrimes#define EV_TESTED 02		/* exit status is checked; ignore -e flag */
771556Srgrimes#define EV_BACKCMD 04		/* command executing within back quotes */
781556Srgrimes
791556SrgrimesMKINIT int evalskip;		/* set if we are skipping commands */
801556SrgrimesSTATIC int skipcount;		/* number of levels to skip */
811556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
821556Srgrimesint funcnest;			/* depth of function calls */
831556Srgrimes
841556Srgrimes
851556Srgrimeschar *commandname;
861556Srgrimesstruct strlist *cmdenviron;
871556Srgrimesint exitstatus;			/* exit status of last command */
8817987Speterint oexitstatus;		/* saved exit status */
891556Srgrimes
901556Srgrimes
9117987SpeterSTATIC void evalloop __P((union node *));
9217987SpeterSTATIC void evalfor __P((union node *));
9317987SpeterSTATIC void evalcase __P((union node *, int));
9417987SpeterSTATIC void evalsubshell __P((union node *, int));
9517987SpeterSTATIC void expredir __P((union node *));
9617987SpeterSTATIC void evalpipe __P((union node *));
9717987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *));
9817987SpeterSTATIC void prehash __P((union node *));
991556Srgrimes
1001556Srgrimes
1011556Srgrimes/*
1021556Srgrimes * Called to reset things after an exception.
1031556Srgrimes */
1041556Srgrimes
1051556Srgrimes#ifdef mkinit
1061556SrgrimesINCLUDE "eval.h"
1071556Srgrimes
1081556SrgrimesRESET {
1091556Srgrimes	evalskip = 0;
1101556Srgrimes	loopnest = 0;
1111556Srgrimes	funcnest = 0;
1121556Srgrimes}
1131556Srgrimes
1141556SrgrimesSHELLPROC {
1151556Srgrimes	exitstatus = 0;
1161556Srgrimes}
1171556Srgrimes#endif
1181556Srgrimes
1191556Srgrimes
1201556Srgrimes
1211556Srgrimes/*
1221556Srgrimes * The eval commmand.
1231556Srgrimes */
1241556Srgrimes
12517987Speterint
12620425Ssteveevalcmd(argc, argv)
12717987Speter	int argc;
12820425Ssteve	char **argv;
1291556Srgrimes{
1301556Srgrimes        char *p;
1311556Srgrimes        char *concat;
1321556Srgrimes        char **ap;
1331556Srgrimes
1341556Srgrimes        if (argc > 1) {
1351556Srgrimes                p = argv[1];
1361556Srgrimes                if (argc > 2) {
1371556Srgrimes                        STARTSTACKSTR(concat);
1381556Srgrimes                        ap = argv + 2;
1391556Srgrimes                        for (;;) {
1401556Srgrimes                                while (*p)
1411556Srgrimes                                        STPUTC(*p++, concat);
1421556Srgrimes                                if ((p = *ap++) == NULL)
1431556Srgrimes                                        break;
1441556Srgrimes                                STPUTC(' ', concat);
1451556Srgrimes                        }
1461556Srgrimes                        STPUTC('\0', concat);
1471556Srgrimes                        p = grabstackstr(concat);
1481556Srgrimes                }
1491556Srgrimes                evalstring(p);
1501556Srgrimes        }
1511556Srgrimes        return exitstatus;
1521556Srgrimes}
1531556Srgrimes
1541556Srgrimes
1551556Srgrimes/*
1561556Srgrimes * Execute a command or commands contained in a string.
1571556Srgrimes */
1581556Srgrimes
1591556Srgrimesvoid
1601556Srgrimesevalstring(s)
1611556Srgrimes	char *s;
1621556Srgrimes	{
1631556Srgrimes	union node *n;
1641556Srgrimes	struct stackmark smark;
1651556Srgrimes
1661556Srgrimes	setstackmark(&smark);
1671556Srgrimes	setinputstring(s, 1);
1681556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
1691556Srgrimes		evaltree(n, 0);
1701556Srgrimes		popstackmark(&smark);
1711556Srgrimes	}
1721556Srgrimes	popfile();
1731556Srgrimes	popstackmark(&smark);
1741556Srgrimes}
1751556Srgrimes
1761556Srgrimes
1771556Srgrimes
1781556Srgrimes/*
1791556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1801556Srgrimes * exitstatus.
1811556Srgrimes */
1821556Srgrimes
1831556Srgrimesvoid
1841556Srgrimesevaltree(n, flags)
1851556Srgrimes	union node *n;
18617987Speter	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);
2481556Srgrimes		break;
2491556Srgrimes	case NCASE:
2501556Srgrimes		evalcase(n, flags);
2511556Srgrimes		break;
2521556Srgrimes	case NDEFUN:
2531556Srgrimes		defun(n->narg.text, n->narg.next);
2541556Srgrimes		exitstatus = 0;
2551556Srgrimes		break;
2561556Srgrimes	case NNOT:
2571556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2581556Srgrimes		exitstatus = !exitstatus;
2591556Srgrimes		break;
2601556Srgrimes
2611556Srgrimes	case NPIPE:
2621556Srgrimes		evalpipe(n);
2631556Srgrimes		break;
2641556Srgrimes	case NCMD:
2651556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
2661556Srgrimes		break;
2671556Srgrimes	default:
2681556Srgrimes		out1fmt("Node type = %d\n", n->type);
2691556Srgrimes		flushout(&output);
2701556Srgrimes		break;
2711556Srgrimes	}
2721556Srgrimesout:
2731556Srgrimes	if (pendingsigs)
2741556Srgrimes		dotrap();
2751556Srgrimes	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
2761556Srgrimes		exitshell(exitstatus);
2771556Srgrimes}
2781556Srgrimes
2791556Srgrimes
2801556SrgrimesSTATIC void
2811556Srgrimesevalloop(n)
2821556Srgrimes	union node *n;
28317987Speter{
2841556Srgrimes	int status;
2851556Srgrimes
2861556Srgrimes	loopnest++;
2871556Srgrimes	status = 0;
2881556Srgrimes	for (;;) {
2891556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2901556Srgrimes		if (evalskip) {
2911556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
2921556Srgrimes				evalskip = 0;
2931556Srgrimes				continue;
2941556Srgrimes			}
2951556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
2961556Srgrimes				evalskip = 0;
2971556Srgrimes			break;
2981556Srgrimes		}
2991556Srgrimes		if (n->type == NWHILE) {
3001556Srgrimes			if (exitstatus != 0)
3011556Srgrimes				break;
3021556Srgrimes		} else {
3031556Srgrimes			if (exitstatus == 0)
3041556Srgrimes				break;
3051556Srgrimes		}
3061556Srgrimes		evaltree(n->nbinary.ch2, 0);
3071556Srgrimes		status = exitstatus;
3081556Srgrimes		if (evalskip)
3091556Srgrimes			goto skipping;
3101556Srgrimes	}
3111556Srgrimes	loopnest--;
3121556Srgrimes	exitstatus = status;
3131556Srgrimes}
3141556Srgrimes
3151556Srgrimes
3161556Srgrimes
3171556SrgrimesSTATIC void
3181556Srgrimesevalfor(n)
31917987Speter    union node *n;
32017987Speter{
3211556Srgrimes	struct arglist arglist;
3221556Srgrimes	union node *argp;
3231556Srgrimes	struct strlist *sp;
3241556Srgrimes	struct stackmark smark;
3251556Srgrimes
3261556Srgrimes	setstackmark(&smark);
3271556Srgrimes	arglist.lastp = &arglist.list;
3281556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
32917987Speter		oexitstatus = exitstatus;
3301556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3311556Srgrimes		if (evalskip)
3321556Srgrimes			goto out;
3331556Srgrimes	}
3341556Srgrimes	*arglist.lastp = NULL;
3351556Srgrimes
3361556Srgrimes	exitstatus = 0;
3371556Srgrimes	loopnest++;
3381556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3391556Srgrimes		setvar(n->nfor.var, sp->text, 0);
3401556Srgrimes		evaltree(n->nfor.body, 0);
3411556Srgrimes		if (evalskip) {
3421556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3431556Srgrimes				evalskip = 0;
3441556Srgrimes				continue;
3451556Srgrimes			}
3461556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3471556Srgrimes				evalskip = 0;
3481556Srgrimes			break;
3491556Srgrimes		}
3501556Srgrimes	}
3511556Srgrimes	loopnest--;
3521556Srgrimesout:
3531556Srgrimes	popstackmark(&smark);
3541556Srgrimes}
3551556Srgrimes
3561556Srgrimes
3571556Srgrimes
3581556SrgrimesSTATIC void
3591556Srgrimesevalcase(n, flags)
3601556Srgrimes	union node *n;
36117987Speter	int flags;
36217987Speter{
3631556Srgrimes	union node *cp;
3641556Srgrimes	union node *patp;
3651556Srgrimes	struct arglist arglist;
3661556Srgrimes	struct stackmark smark;
3671556Srgrimes
3681556Srgrimes	setstackmark(&smark);
3691556Srgrimes	arglist.lastp = &arglist.list;
37017987Speter	oexitstatus = exitstatus;
3711556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3721556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3731556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3741556Srgrimes			if (casematch(patp, arglist.list->text)) {
3751556Srgrimes				if (evalskip == 0) {
3761556Srgrimes					evaltree(cp->nclist.body, flags);
3771556Srgrimes				}
3781556Srgrimes				goto out;
3791556Srgrimes			}
3801556Srgrimes		}
3811556Srgrimes	}
3821556Srgrimesout:
3831556Srgrimes	popstackmark(&smark);
3841556Srgrimes}
3851556Srgrimes
3861556Srgrimes
3871556Srgrimes
3881556Srgrimes/*
3891556Srgrimes * Kick off a subshell to evaluate a tree.
3901556Srgrimes */
3911556Srgrimes
3921556SrgrimesSTATIC void
3931556Srgrimesevalsubshell(n, flags)
3941556Srgrimes	union node *n;
39517987Speter	int flags;
39617987Speter{
3971556Srgrimes	struct job *jp;
3981556Srgrimes	int backgnd = (n->type == NBACKGND);
3991556Srgrimes
4001556Srgrimes	expredir(n->nredir.redirect);
4011556Srgrimes	jp = makejob(n, 1);
4021556Srgrimes	if (forkshell(jp, n, backgnd) == 0) {
4031556Srgrimes		if (backgnd)
4041556Srgrimes			flags &=~ EV_TESTED;
4051556Srgrimes		redirect(n->nredir.redirect, 0);
4061556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
4071556Srgrimes	}
4081556Srgrimes	if (! backgnd) {
4091556Srgrimes		INTOFF;
4101556Srgrimes		exitstatus = waitforjob(jp);
4111556Srgrimes		INTON;
4121556Srgrimes	}
4131556Srgrimes}
4141556Srgrimes
4151556Srgrimes
4161556Srgrimes
4171556Srgrimes/*
4181556Srgrimes * Compute the names of the files in a redirection list.
4191556Srgrimes */
4201556Srgrimes
4211556SrgrimesSTATIC void
4221556Srgrimesexpredir(n)
4231556Srgrimes	union node *n;
42417987Speter{
4251556Srgrimes	register union node *redir;
4261556Srgrimes
4271556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
42817987Speter		struct arglist fn;
42917987Speter		fn.lastp = &fn.list;
43017987Speter		oexitstatus = exitstatus;
43117987Speter		switch (redir->type) {
43217987Speter		case NFROM:
43317987Speter		case NTO:
43417987Speter		case NAPPEND:
4351556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4361556Srgrimes			redir->nfile.expfname = fn.list->text;
43717987Speter			break;
43817987Speter		case NFROMFD:
43917987Speter		case NTOFD:
44017987Speter			if (redir->ndup.vname) {
44117987Speter				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
44217987Speter				fixredir(redir, fn.list->text, 1);
44317987Speter			}
44417987Speter			break;
4451556Srgrimes		}
4461556Srgrimes	}
4471556Srgrimes}
4481556Srgrimes
4491556Srgrimes
4501556Srgrimes
4511556Srgrimes/*
4521556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
4531556Srgrimes * of the process creating the pipeline.  (This differs from some versions
4541556Srgrimes * of the shell, which make the last process in a pipeline the parent
4551556Srgrimes * of all the rest.)
4561556Srgrimes */
4571556Srgrimes
4581556SrgrimesSTATIC void
4591556Srgrimesevalpipe(n)
4601556Srgrimes	union node *n;
46117987Speter{
4621556Srgrimes	struct job *jp;
4631556Srgrimes	struct nodelist *lp;
4641556Srgrimes	int pipelen;
4651556Srgrimes	int prevfd;
4661556Srgrimes	int pip[2];
4671556Srgrimes
46817987Speter	TRACE(("evalpipe(0x%lx) called\n", (long)n));
4691556Srgrimes	pipelen = 0;
4701556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
4711556Srgrimes		pipelen++;
4721556Srgrimes	INTOFF;
4731556Srgrimes	jp = makejob(n, pipelen);
4741556Srgrimes	prevfd = -1;
4751556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
4761556Srgrimes		prehash(lp->n);
4771556Srgrimes		pip[1] = -1;
4781556Srgrimes		if (lp->next) {
4791556Srgrimes			if (pipe(pip) < 0) {
4801556Srgrimes				close(prevfd);
4811556Srgrimes				error("Pipe call failed");
4821556Srgrimes			}
4831556Srgrimes		}
4841556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
4851556Srgrimes			INTON;
4861556Srgrimes			if (prevfd > 0) {
4871556Srgrimes				close(0);
4881556Srgrimes				copyfd(prevfd, 0);
4891556Srgrimes				close(prevfd);
4901556Srgrimes			}
4911556Srgrimes			if (pip[1] >= 0) {
4921556Srgrimes				close(pip[0]);
4931556Srgrimes				if (pip[1] != 1) {
4941556Srgrimes					close(1);
4951556Srgrimes					copyfd(pip[1], 1);
4961556Srgrimes					close(pip[1]);
4971556Srgrimes				}
4981556Srgrimes			}
4991556Srgrimes			evaltree(lp->n, EV_EXIT);
5001556Srgrimes		}
5011556Srgrimes		if (prevfd >= 0)
5021556Srgrimes			close(prevfd);
5031556Srgrimes		prevfd = pip[0];
5041556Srgrimes		close(pip[1]);
5051556Srgrimes	}
5061556Srgrimes	INTON;
5071556Srgrimes	if (n->npipe.backgnd == 0) {
5081556Srgrimes		INTOFF;
5091556Srgrimes		exitstatus = waitforjob(jp);
5101556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5111556Srgrimes		INTON;
5121556Srgrimes	}
5131556Srgrimes}
5141556Srgrimes
5151556Srgrimes
5161556Srgrimes
5171556Srgrimes/*
5181556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5191556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5201556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5211556Srgrimes * Should be called with interrupts off.
5221556Srgrimes */
5231556Srgrimes
5241556Srgrimesvoid
5251556Srgrimesevalbackcmd(n, result)
5261556Srgrimes	union node *n;
5271556Srgrimes	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)
5481556Srgrimes			error("Pipe call failed");
5491556Srgrimes		jp = makejob(n, 1);
5501556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
5511556Srgrimes			FORCEINTON;
5521556Srgrimes			close(pip[0]);
5531556Srgrimes			if (pip[1] != 1) {
5541556Srgrimes				close(1);
5551556Srgrimes				copyfd(pip[1], 1);
5561556Srgrimes				close(pip[1]);
5571556Srgrimes			}
5581556Srgrimes			evaltree(n, EV_EXIT);
5591556Srgrimes		}
5601556Srgrimes		close(pip[1]);
5611556Srgrimes		result->fd = pip[0];
5621556Srgrimes		result->jp = jp;
5631556Srgrimes	}
5641556Srgrimesout:
5651556Srgrimes	popstackmark(&smark);
5661556Srgrimes	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
5671556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
5681556Srgrimes}
5691556Srgrimes
5701556Srgrimes
5711556Srgrimes
5721556Srgrimes/*
5731556Srgrimes * Execute a simple command.
5741556Srgrimes */
5751556Srgrimes
5761556SrgrimesSTATIC void
5771556Srgrimesevalcommand(cmd, flags, backcmd)
5781556Srgrimes	union node *cmd;
57917987Speter	int flags;
5801556Srgrimes	struct backcmd *backcmd;
58117987Speter{
5821556Srgrimes	struct stackmark smark;
5831556Srgrimes	union node *argp;
5841556Srgrimes	struct arglist arglist;
5851556Srgrimes	struct arglist varlist;
5861556Srgrimes	char **argv;
5871556Srgrimes	int argc;
5881556Srgrimes	char **envp;
5891556Srgrimes	int varflag;
5901556Srgrimes	struct strlist *sp;
5911556Srgrimes	int mode;
5921556Srgrimes	int pip[2];
5931556Srgrimes	struct cmdentry cmdentry;
5941556Srgrimes	struct job *jp;
5951556Srgrimes	struct jmploc jmploc;
5961556Srgrimes	struct jmploc *volatile savehandler;
5971556Srgrimes	char *volatile savecmdname;
5981556Srgrimes	volatile struct shparam saveparam;
5991556Srgrimes	struct localvar *volatile savelocalvars;
6001556Srgrimes	volatile int e;
6011556Srgrimes	char *lastarg;
60217987Speter#if __GNUC__
60317987Speter	/* Avoid longjmp clobbering */
60417987Speter	(void) &argv;
60517987Speter	(void) &argc;
60617987Speter	(void) &lastarg;
60717987Speter	(void) &flags;
60817987Speter#endif
6091556Srgrimes
6101556Srgrimes	/* First expand the arguments. */
61117987Speter	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
6121556Srgrimes	setstackmark(&smark);
6131556Srgrimes	arglist.lastp = &arglist.list;
6141556Srgrimes	varlist.lastp = &varlist.list;
6151556Srgrimes	varflag = 1;
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) {
6521556Srgrimes		outc('+', &errout);
6531556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
6541556Srgrimes			outc(' ', &errout);
6551556Srgrimes			out2str(sp->text);
6561556Srgrimes		}
6571556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
6581556Srgrimes			outc(' ', &errout);
6591556Srgrimes			out2str(sp->text);
6601556Srgrimes		}
6611556Srgrimes		outc('\n', &errout);
6621556Srgrimes		flushout(&errout);
6631556Srgrimes	}
6641556Srgrimes
6651556Srgrimes	/* Now locate the command. */
6661556Srgrimes	if (argc == 0) {
6671556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6681556Srgrimes		cmdentry.u.index = BLTINCMD;
6691556Srgrimes	} else {
67017987Speter		static const char PATH[] = "PATH=";
67117987Speter		char *path = pathval();
67217987Speter
67317987Speter		/*
67417987Speter		 * Modify the command lookup path, if a PATH= assignment
67517987Speter		 * is present
67617987Speter		 */
67717987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
67817987Speter			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
67917987Speter				path = sp->text + sizeof(PATH) - 1;
68017987Speter
68117987Speter		find_command(argv[0], &cmdentry, 1, path);
6821556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
68320425Ssteve			exitstatus = 127;
6841556Srgrimes			flushout(&errout);
6851556Srgrimes			return;
6861556Srgrimes		}
6871556Srgrimes		/* implement the bltin builtin here */
6881556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
6891556Srgrimes			for (;;) {
6901556Srgrimes				argv++;
6911556Srgrimes				if (--argc == 0)
6921556Srgrimes					break;
6931556Srgrimes				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
6941556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
69520425Ssteve					exitstatus = 127;
6961556Srgrimes					flushout(&errout);
6971556Srgrimes					return;
6981556Srgrimes				}
6991556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7001556Srgrimes					break;
7011556Srgrimes			}
7021556Srgrimes		}
7031556Srgrimes	}
7041556Srgrimes
7051556Srgrimes	/* Fork off a child process if necessary. */
7061556Srgrimes	if (cmd->ncmd.backgnd
70717987Speter	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
70817987Speter	 || ((flags & EV_BACKCMD) != 0
7091556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
7101556Srgrimes		 || cmdentry.u.index == DOTCMD
71117987Speter		 || cmdentry.u.index == EVALCMD))) {
7121556Srgrimes		jp = makejob(cmd, 1);
7131556Srgrimes		mode = cmd->ncmd.backgnd;
7141556Srgrimes		if (flags & EV_BACKCMD) {
7151556Srgrimes			mode = FORK_NOJOB;
7161556Srgrimes			if (pipe(pip) < 0)
7171556Srgrimes				error("Pipe call failed");
7181556Srgrimes		}
7191556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7201556Srgrimes			goto parent;	/* at end of routine */
7211556Srgrimes		if (flags & EV_BACKCMD) {
7221556Srgrimes			FORCEINTON;
7231556Srgrimes			close(pip[0]);
7241556Srgrimes			if (pip[1] != 1) {
7251556Srgrimes				close(1);
7261556Srgrimes				copyfd(pip[1], 1);
7271556Srgrimes				close(pip[1]);
7281556Srgrimes			}
7291556Srgrimes		}
7301556Srgrimes		flags |= EV_EXIT;
7311556Srgrimes	}
7321556Srgrimes
7331556Srgrimes	/* This is the child process if a fork occurred. */
7341556Srgrimes	/* Execute the command. */
7351556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
73620425Ssteve#ifdef DEBUG
7371556Srgrimes		trputs("Shell function:  ");  trargs(argv);
73820425Ssteve#endif
7391556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7401556Srgrimes		saveparam = shellparam;
7411556Srgrimes		shellparam.malloc = 0;
74220425Ssteve		shellparam.reset = 1;
7431556Srgrimes		shellparam.nparam = argc - 1;
7441556Srgrimes		shellparam.p = argv + 1;
7451556Srgrimes		shellparam.optnext = NULL;
7461556Srgrimes		INTOFF;
7471556Srgrimes		savelocalvars = localvars;
7481556Srgrimes		localvars = NULL;
7491556Srgrimes		INTON;
7501556Srgrimes		if (setjmp(jmploc.loc)) {
7511556Srgrimes			if (exception == EXSHELLPROC)
7521556Srgrimes				freeparam((struct shparam *)&saveparam);
7531556Srgrimes			else {
7541556Srgrimes				freeparam(&shellparam);
7551556Srgrimes				shellparam = saveparam;
7561556Srgrimes			}
7571556Srgrimes			poplocalvars();
7581556Srgrimes			localvars = savelocalvars;
7591556Srgrimes			handler = savehandler;
7601556Srgrimes			longjmp(handler->loc, 1);
7611556Srgrimes		}
7621556Srgrimes		savehandler = handler;
7631556Srgrimes		handler = &jmploc;
7641556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
7651556Srgrimes			mklocal(sp->text);
7661556Srgrimes		funcnest++;
7671556Srgrimes		evaltree(cmdentry.u.func, 0);
7681556Srgrimes		funcnest--;
7691556Srgrimes		INTOFF;
7701556Srgrimes		poplocalvars();
7711556Srgrimes		localvars = savelocalvars;
7721556Srgrimes		freeparam(&shellparam);
7731556Srgrimes		shellparam = saveparam;
7741556Srgrimes		handler = savehandler;
7751556Srgrimes		popredir();
7761556Srgrimes		INTON;
7771556Srgrimes		if (evalskip == SKIPFUNC) {
7781556Srgrimes			evalskip = 0;
7791556Srgrimes			skipcount = 0;
7801556Srgrimes		}
7811556Srgrimes		if (flags & EV_EXIT)
7821556Srgrimes			exitshell(exitstatus);
7831556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
78420425Ssteve#ifdef DEBUG
7851556Srgrimes		trputs("builtin command:  ");  trargs(argv);
78620425Ssteve#endif
7871556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
7881556Srgrimes		if (flags == EV_BACKCMD) {
7891556Srgrimes			memout.nleft = 0;
7901556Srgrimes			memout.nextc = memout.buf;
7911556Srgrimes			memout.bufsize = 64;
7921556Srgrimes			mode |= REDIR_BACKQ;
7931556Srgrimes		}
7941556Srgrimes		redirect(cmd->ncmd.redirect, mode);
7951556Srgrimes		savecmdname = commandname;
7961556Srgrimes		cmdenviron = varlist.list;
7971556Srgrimes		e = -1;
7981556Srgrimes		if (setjmp(jmploc.loc)) {
7991556Srgrimes			e = exception;
8001556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8011556Srgrimes			goto cmddone;
8021556Srgrimes		}
8031556Srgrimes		savehandler = handler;
8041556Srgrimes		handler = &jmploc;
8051556Srgrimes		commandname = argv[0];
8061556Srgrimes		argptr = argv + 1;
8071556Srgrimes		optptr = NULL;			/* initialize nextopt */
8081556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8091556Srgrimes		flushall();
8101556Srgrimescmddone:
8111556Srgrimes		out1 = &output;
8121556Srgrimes		out2 = &errout;
8131556Srgrimes		freestdout();
8141556Srgrimes		if (e != EXSHELLPROC) {
8151556Srgrimes			commandname = savecmdname;
8161556Srgrimes			if (flags & EV_EXIT) {
8171556Srgrimes				exitshell(exitstatus);
8181556Srgrimes			}
8191556Srgrimes		}
8201556Srgrimes		handler = savehandler;
8211556Srgrimes		if (e != -1) {
82220425Ssteve			if ((e != EXERROR && e != EXEXEC)
82320425Ssteve			   || cmdentry.u.index == BLTINCMD
82420425Ssteve			   || cmdentry.u.index == DOTCMD
82520425Ssteve			   || cmdentry.u.index == EVALCMD
82617987Speter#ifndef NO_HISTORY
82720425Ssteve			   || cmdentry.u.index == HISTCMD
82817987Speter#endif
82920425Ssteve			   || cmdentry.u.index == EXECCMD)
8301556Srgrimes				exraise(e);
8311556Srgrimes			FORCEINTON;
8321556Srgrimes		}
8331556Srgrimes		if (cmdentry.u.index != EXECCMD)
8341556Srgrimes			popredir();
8351556Srgrimes		if (flags == EV_BACKCMD) {
8361556Srgrimes			backcmd->buf = memout.buf;
8371556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8381556Srgrimes			memout.buf = NULL;
8391556Srgrimes		}
8401556Srgrimes	} else {
84120425Ssteve#ifdef DEBUG
8421556Srgrimes		trputs("normal command:  ");  trargs(argv);
84320425Ssteve#endif
8441556Srgrimes		clearredir();
8451556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8461556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8471556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8481556Srgrimes		envp = environment();
84917987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8501556Srgrimes		/*NOTREACHED*/
8511556Srgrimes	}
8521556Srgrimes	goto out;
8531556Srgrimes
8541556Srgrimesparent:	/* parent process gets here (if we forked) */
8551556Srgrimes	if (mode == 0) {	/* argument to fork */
8561556Srgrimes		INTOFF;
85719683Speter		exitstatus = waitforjob(jp);
8581556Srgrimes		INTON;
8591556Srgrimes	} else if (mode == 2) {
8601556Srgrimes		backcmd->fd = pip[0];
8611556Srgrimes		close(pip[1]);
8621556Srgrimes		backcmd->jp = jp;
8631556Srgrimes	}
8641556Srgrimes
8651556Srgrimesout:
8661556Srgrimes	if (lastarg)
8671556Srgrimes		setvar("_", lastarg, 0);
8681556Srgrimes	popstackmark(&smark);
8691556Srgrimes}
8701556Srgrimes
8711556Srgrimes
8721556Srgrimes
8731556Srgrimes/*
8741556Srgrimes * Search for a command.  This is called before we fork so that the
8751556Srgrimes * location of the command will be available in the parent as well as
8761556Srgrimes * the child.  The check for "goodname" is an overly conservative
8771556Srgrimes * check that the name will not be subject to expansion.
8781556Srgrimes */
8791556Srgrimes
8801556SrgrimesSTATIC void
8811556Srgrimesprehash(n)
8821556Srgrimes	union node *n;
88317987Speter{
8841556Srgrimes	struct cmdentry entry;
8851556Srgrimes
88617987Speter	if (n->type == NCMD && n->ncmd.args)
88717987Speter		if (goodname(n->ncmd.args->narg.text))
88817987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
88917987Speter				     pathval());
8901556Srgrimes}
8911556Srgrimes
8921556Srgrimes
8931556Srgrimes
8941556Srgrimes/*
8951556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
8961556Srgrimes * tied to evaluation are implemented here.
8971556Srgrimes */
8981556Srgrimes
8991556Srgrimes/*
9001556Srgrimes * No command given, or a bltin command with no arguments.  Set the
9011556Srgrimes * specified variables.
9021556Srgrimes */
9031556Srgrimes
90417987Speterint
90517987Speterbltincmd(argc, argv)
90617987Speter	int argc;
90720425Ssteve	char **argv;
90817987Speter{
9091556Srgrimes	listsetvar(cmdenviron);
91020425Ssteve	/*
91117987Speter	 * Preserve exitstatus of a previous possible redirection
91220425Ssteve	 * as POSIX mandates
91317987Speter	 */
9141556Srgrimes	return exitstatus;
9151556Srgrimes}
9161556Srgrimes
9171556Srgrimes
9181556Srgrimes/*
9191556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9201556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9211556Srgrimes * above all check this flag, and if it is set they start skipping
9221556Srgrimes * commands rather than executing them.  The variable skipcount is
9231556Srgrimes * the number of loops to break/continue, or the number of function
9241556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9251556Srgrimes * be an error to break out of more loops than exist, but it isn't
9261556Srgrimes * in the standard shell so we don't make it one here.
9271556Srgrimes */
9281556Srgrimes
92917987Speterint
93017987Speterbreakcmd(argc, argv)
93117987Speter	int argc;
93220425Ssteve	char **argv;
93317987Speter{
93420425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
9351556Srgrimes
9361556Srgrimes	if (n > loopnest)
9371556Srgrimes		n = loopnest;
9381556Srgrimes	if (n > 0) {
9391556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9401556Srgrimes		skipcount = n;
9411556Srgrimes	}
9421556Srgrimes	return 0;
9431556Srgrimes}
9441556Srgrimes
9451556Srgrimes
9461556Srgrimes/*
9471556Srgrimes * The return command.
9481556Srgrimes */
9491556Srgrimes
95017987Speterint
95120425Sstevereturncmd(argc, argv)
95217987Speter	int argc;
95320425Ssteve	char **argv;
95417987Speter{
95520425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
9561556Srgrimes
9571556Srgrimes	if (funcnest) {
9581556Srgrimes		evalskip = SKIPFUNC;
9591556Srgrimes		skipcount = 1;
96020425Ssteve	} else {
96120425Ssteve		/* skip the rest of the file */
96220425Ssteve		evalskip = SKIPFILE;
96320425Ssteve		skipcount = 1;
9641556Srgrimes	}
9651556Srgrimes	return ret;
9661556Srgrimes}
9671556Srgrimes
9681556Srgrimes
96917987Speterint
97020425Sstevefalsecmd(argc, argv)
97117987Speter	int argc;
97220425Ssteve	char **argv;
97317987Speter{
97417987Speter	return 1;
97517987Speter}
97617987Speter
97717987Speter
97817987Speterint
97920425Sstevetruecmd(argc, argv)
98017987Speter	int argc;
98120425Ssteve	char **argv;
98217987Speter{
9831556Srgrimes	return 0;
9841556Srgrimes}
9851556Srgrimes
9861556Srgrimes
98717987Speterint
98820425Ssteveexeccmd(argc, argv)
98917987Speter	int argc;
99020425Ssteve	char **argv;
99117987Speter{
9921556Srgrimes	if (argc > 1) {
99317987Speter		struct strlist *sp;
99417987Speter
9951556Srgrimes		iflag = 0;		/* exit on error */
9961556Srgrimes		mflag = 0;
9971556Srgrimes		optschanged();
99817987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
99917987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10001556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10011556Srgrimes
10021556Srgrimes	}
10031556Srgrimes	return 0;
10041556Srgrimes}
1005