eval.c revision 18754
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 *
3618754Ssteve *	$Id: eval.c,v 1.5 1996/09/01 10:19:57 peter Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4017987Speterstatic char 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
791556Srgrimes
801556Srgrimes/* reasons for skipping commands (see comment on breakcmd routine) */
811556Srgrimes#define SKIPBREAK 1
821556Srgrimes#define SKIPCONT 2
831556Srgrimes#define SKIPFUNC 3
841556Srgrimes
851556SrgrimesMKINIT int evalskip;		/* set if we are skipping commands */
861556SrgrimesSTATIC int skipcount;		/* number of levels to skip */
871556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
881556Srgrimesint funcnest;			/* depth of function calls */
891556Srgrimes
901556Srgrimes
911556Srgrimeschar *commandname;
921556Srgrimesstruct strlist *cmdenviron;
931556Srgrimesint exitstatus;			/* exit status of last command */
9417987Speterint oexitstatus;		/* saved exit status */
951556Srgrimes
961556Srgrimes
9717987SpeterSTATIC void evalloop __P((union node *));
9817987SpeterSTATIC void evalfor __P((union node *));
9917987SpeterSTATIC void evalcase __P((union node *, int));
10017987SpeterSTATIC void evalsubshell __P((union node *, int));
10117987SpeterSTATIC void expredir __P((union node *));
10217987SpeterSTATIC void evalpipe __P((union node *));
10317987SpeterSTATIC void evalcommand __P((union node *, int, struct backcmd *));
10417987SpeterSTATIC void prehash __P((union node *));
1051556Srgrimes
1061556Srgrimes
1071556Srgrimes/*
1081556Srgrimes * Called to reset things after an exception.
1091556Srgrimes */
1101556Srgrimes
1111556Srgrimes#ifdef mkinit
1121556SrgrimesINCLUDE "eval.h"
1131556Srgrimes
1141556SrgrimesRESET {
1151556Srgrimes	evalskip = 0;
1161556Srgrimes	loopnest = 0;
1171556Srgrimes	funcnest = 0;
1181556Srgrimes}
1191556Srgrimes
1201556SrgrimesSHELLPROC {
1211556Srgrimes	exitstatus = 0;
1221556Srgrimes}
1231556Srgrimes#endif
1241556Srgrimes
1251556Srgrimes
1261556Srgrimes
1271556Srgrimes/*
1281556Srgrimes * The eval commmand.
1291556Srgrimes */
1301556Srgrimes
13117987Speterint
13217987Speterevalcmd(argc, argv)
13317987Speter	int argc;
13417987Speter	char **argv;
1351556Srgrimes{
1361556Srgrimes        char *p;
1371556Srgrimes        char *concat;
1381556Srgrimes        char **ap;
1391556Srgrimes
1401556Srgrimes        if (argc > 1) {
1411556Srgrimes                p = argv[1];
1421556Srgrimes                if (argc > 2) {
1431556Srgrimes                        STARTSTACKSTR(concat);
1441556Srgrimes                        ap = argv + 2;
1451556Srgrimes                        for (;;) {
1461556Srgrimes                                while (*p)
1471556Srgrimes                                        STPUTC(*p++, concat);
1481556Srgrimes                                if ((p = *ap++) == NULL)
1491556Srgrimes                                        break;
1501556Srgrimes                                STPUTC(' ', concat);
1511556Srgrimes                        }
1521556Srgrimes                        STPUTC('\0', concat);
1531556Srgrimes                        p = grabstackstr(concat);
1541556Srgrimes                }
1551556Srgrimes                evalstring(p);
1561556Srgrimes        }
1571556Srgrimes        return exitstatus;
1581556Srgrimes}
1591556Srgrimes
1601556Srgrimes
1611556Srgrimes/*
1621556Srgrimes * Execute a command or commands contained in a string.
1631556Srgrimes */
1641556Srgrimes
1651556Srgrimesvoid
1661556Srgrimesevalstring(s)
1671556Srgrimes	char *s;
1681556Srgrimes	{
1691556Srgrimes	union node *n;
1701556Srgrimes	struct stackmark smark;
1711556Srgrimes
1721556Srgrimes	setstackmark(&smark);
1731556Srgrimes	setinputstring(s, 1);
1741556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
1751556Srgrimes		evaltree(n, 0);
1761556Srgrimes		popstackmark(&smark);
1771556Srgrimes	}
1781556Srgrimes	popfile();
1791556Srgrimes	popstackmark(&smark);
1801556Srgrimes}
1811556Srgrimes
1821556Srgrimes
1831556Srgrimes
1841556Srgrimes/*
1851556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1861556Srgrimes * exitstatus.
1871556Srgrimes */
1881556Srgrimes
1891556Srgrimesvoid
1901556Srgrimesevaltree(n, flags)
1911556Srgrimes	union node *n;
19217987Speter	int flags;
19317987Speter{
1941556Srgrimes	if (n == NULL) {
1951556Srgrimes		TRACE(("evaltree(NULL) called\n"));
1961556Srgrimes		exitstatus = 0;
1971556Srgrimes		goto out;
1981556Srgrimes	}
19917987Speter#ifndef NO_HISTORY
2001556Srgrimes	displayhist = 1;	/* show history substitutions done with fc */
20117987Speter#endif
20217987Speter	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
2031556Srgrimes	switch (n->type) {
2041556Srgrimes	case NSEMI:
2051556Srgrimes		evaltree(n->nbinary.ch1, 0);
2061556Srgrimes		if (evalskip)
2071556Srgrimes			goto out;
2081556Srgrimes		evaltree(n->nbinary.ch2, flags);
2091556Srgrimes		break;
2101556Srgrimes	case NAND:
2111556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
21218754Ssteve		if (evalskip || exitstatus != 0) {
21318754Ssteve			/* don't bomb out on "set -e; false && true" */
21418754Ssteve			flags |= EV_TESTED;
2151556Srgrimes			goto out;
21618754Ssteve		}
2171556Srgrimes		evaltree(n->nbinary.ch2, flags);
2181556Srgrimes		break;
2191556Srgrimes	case NOR:
2201556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2211556Srgrimes		if (evalskip || exitstatus == 0)
2221556Srgrimes			goto out;
2231556Srgrimes		evaltree(n->nbinary.ch2, flags);
2241556Srgrimes		break;
2251556Srgrimes	case NREDIR:
2261556Srgrimes		expredir(n->nredir.redirect);
2271556Srgrimes		redirect(n->nredir.redirect, REDIR_PUSH);
2281556Srgrimes		evaltree(n->nredir.n, flags);
2291556Srgrimes		popredir();
2301556Srgrimes		break;
2311556Srgrimes	case NSUBSHELL:
2321556Srgrimes		evalsubshell(n, flags);
2331556Srgrimes		break;
2341556Srgrimes	case NBACKGND:
2351556Srgrimes		evalsubshell(n, flags);
2361556Srgrimes		break;
2371556Srgrimes	case NIF: {
23817987Speter		int status;
2391556Srgrimes
2401556Srgrimes		evaltree(n->nif.test, EV_TESTED);
24117987Speter		status = exitstatus;
24217987Speter		exitstatus = 0;
2431556Srgrimes		if (evalskip)
2441556Srgrimes			goto out;
24517987Speter		if (status == 0)
2461556Srgrimes			evaltree(n->nif.ifpart, flags);
24717987Speter		else if (n->nif.elsepart)
2481556Srgrimes			evaltree(n->nif.elsepart, flags);
2491556Srgrimes		break;
2501556Srgrimes	}
2511556Srgrimes	case NWHILE:
2521556Srgrimes	case NUNTIL:
2531556Srgrimes		evalloop(n);
2541556Srgrimes		break;
2551556Srgrimes	case NFOR:
2561556Srgrimes		evalfor(n);
2571556Srgrimes		break;
2581556Srgrimes	case NCASE:
2591556Srgrimes		evalcase(n, flags);
2601556Srgrimes		break;
2611556Srgrimes	case NDEFUN:
2621556Srgrimes		defun(n->narg.text, n->narg.next);
2631556Srgrimes		exitstatus = 0;
2641556Srgrimes		break;
2651556Srgrimes	case NNOT:
2661556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2671556Srgrimes		exitstatus = !exitstatus;
2681556Srgrimes		break;
2691556Srgrimes
2701556Srgrimes	case NPIPE:
2711556Srgrimes		evalpipe(n);
2721556Srgrimes		break;
2731556Srgrimes	case NCMD:
2741556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
2751556Srgrimes		break;
2761556Srgrimes	default:
2771556Srgrimes		out1fmt("Node type = %d\n", n->type);
2781556Srgrimes		flushout(&output);
2791556Srgrimes		break;
2801556Srgrimes	}
2811556Srgrimesout:
2821556Srgrimes	if (pendingsigs)
2831556Srgrimes		dotrap();
2841556Srgrimes	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
2851556Srgrimes		exitshell(exitstatus);
2861556Srgrimes}
2871556Srgrimes
2881556Srgrimes
2891556SrgrimesSTATIC void
2901556Srgrimesevalloop(n)
2911556Srgrimes	union node *n;
29217987Speter{
2931556Srgrimes	int status;
2941556Srgrimes
2951556Srgrimes	loopnest++;
2961556Srgrimes	status = 0;
2971556Srgrimes	for (;;) {
2981556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2991556Srgrimes		if (evalskip) {
3001556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
3011556Srgrimes				evalskip = 0;
3021556Srgrimes				continue;
3031556Srgrimes			}
3041556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3051556Srgrimes				evalskip = 0;
3061556Srgrimes			break;
3071556Srgrimes		}
3081556Srgrimes		if (n->type == NWHILE) {
3091556Srgrimes			if (exitstatus != 0)
3101556Srgrimes				break;
3111556Srgrimes		} else {
3121556Srgrimes			if (exitstatus == 0)
3131556Srgrimes				break;
3141556Srgrimes		}
3151556Srgrimes		evaltree(n->nbinary.ch2, 0);
3161556Srgrimes		status = exitstatus;
3171556Srgrimes		if (evalskip)
3181556Srgrimes			goto skipping;
3191556Srgrimes	}
3201556Srgrimes	loopnest--;
3211556Srgrimes	exitstatus = status;
3221556Srgrimes}
3231556Srgrimes
3241556Srgrimes
3251556Srgrimes
3261556SrgrimesSTATIC void
3271556Srgrimesevalfor(n)
32817987Speter    union node *n;
32917987Speter{
3301556Srgrimes	struct arglist arglist;
3311556Srgrimes	union node *argp;
3321556Srgrimes	struct strlist *sp;
3331556Srgrimes	struct stackmark smark;
3341556Srgrimes
3351556Srgrimes	setstackmark(&smark);
3361556Srgrimes	arglist.lastp = &arglist.list;
3371556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
33817987Speter		oexitstatus = exitstatus;
3391556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3401556Srgrimes		if (evalskip)
3411556Srgrimes			goto out;
3421556Srgrimes	}
3431556Srgrimes	*arglist.lastp = NULL;
3441556Srgrimes
3451556Srgrimes	exitstatus = 0;
3461556Srgrimes	loopnest++;
3471556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3481556Srgrimes		setvar(n->nfor.var, sp->text, 0);
3491556Srgrimes		evaltree(n->nfor.body, 0);
3501556Srgrimes		if (evalskip) {
3511556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3521556Srgrimes				evalskip = 0;
3531556Srgrimes				continue;
3541556Srgrimes			}
3551556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3561556Srgrimes				evalskip = 0;
3571556Srgrimes			break;
3581556Srgrimes		}
3591556Srgrimes	}
3601556Srgrimes	loopnest--;
3611556Srgrimesout:
3621556Srgrimes	popstackmark(&smark);
3631556Srgrimes}
3641556Srgrimes
3651556Srgrimes
3661556Srgrimes
3671556SrgrimesSTATIC void
3681556Srgrimesevalcase(n, flags)
3691556Srgrimes	union node *n;
37017987Speter	int flags;
37117987Speter{
3721556Srgrimes	union node *cp;
3731556Srgrimes	union node *patp;
3741556Srgrimes	struct arglist arglist;
3751556Srgrimes	struct stackmark smark;
3761556Srgrimes
3771556Srgrimes	setstackmark(&smark);
3781556Srgrimes	arglist.lastp = &arglist.list;
37917987Speter	oexitstatus = exitstatus;
3801556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3811556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3821556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3831556Srgrimes			if (casematch(patp, arglist.list->text)) {
3841556Srgrimes				if (evalskip == 0) {
3851556Srgrimes					evaltree(cp->nclist.body, flags);
3861556Srgrimes				}
3871556Srgrimes				goto out;
3881556Srgrimes			}
3891556Srgrimes		}
3901556Srgrimes	}
3911556Srgrimesout:
3921556Srgrimes	popstackmark(&smark);
3931556Srgrimes}
3941556Srgrimes
3951556Srgrimes
3961556Srgrimes
3971556Srgrimes/*
3981556Srgrimes * Kick off a subshell to evaluate a tree.
3991556Srgrimes */
4001556Srgrimes
4011556SrgrimesSTATIC void
4021556Srgrimesevalsubshell(n, flags)
4031556Srgrimes	union node *n;
40417987Speter	int flags;
40517987Speter{
4061556Srgrimes	struct job *jp;
4071556Srgrimes	int backgnd = (n->type == NBACKGND);
4081556Srgrimes
4091556Srgrimes	expredir(n->nredir.redirect);
4101556Srgrimes	jp = makejob(n, 1);
4111556Srgrimes	if (forkshell(jp, n, backgnd) == 0) {
4121556Srgrimes		if (backgnd)
4131556Srgrimes			flags &=~ EV_TESTED;
4141556Srgrimes		redirect(n->nredir.redirect, 0);
4151556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
4161556Srgrimes	}
4171556Srgrimes	if (! backgnd) {
4181556Srgrimes		INTOFF;
4191556Srgrimes		exitstatus = waitforjob(jp);
4201556Srgrimes		INTON;
4211556Srgrimes	}
4221556Srgrimes}
4231556Srgrimes
4241556Srgrimes
4251556Srgrimes
4261556Srgrimes/*
4271556Srgrimes * Compute the names of the files in a redirection list.
4281556Srgrimes */
4291556Srgrimes
4301556SrgrimesSTATIC void
4311556Srgrimesexpredir(n)
4321556Srgrimes	union node *n;
43317987Speter{
4341556Srgrimes	register union node *redir;
4351556Srgrimes
4361556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
43717987Speter		struct arglist fn;
43817987Speter		fn.lastp = &fn.list;
43917987Speter		oexitstatus = exitstatus;
44017987Speter		switch (redir->type) {
44117987Speter		case NFROM:
44217987Speter		case NTO:
44317987Speter		case NAPPEND:
4441556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4451556Srgrimes			redir->nfile.expfname = fn.list->text;
44617987Speter			break;
44717987Speter		case NFROMFD:
44817987Speter		case NTOFD:
44917987Speter			if (redir->ndup.vname) {
45017987Speter				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
45117987Speter				fixredir(redir, fn.list->text, 1);
45217987Speter			}
45317987Speter			break;
4541556Srgrimes		}
4551556Srgrimes	}
4561556Srgrimes}
4571556Srgrimes
4581556Srgrimes
4591556Srgrimes
4601556Srgrimes/*
4611556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
4621556Srgrimes * of the process creating the pipeline.  (This differs from some versions
4631556Srgrimes * of the shell, which make the last process in a pipeline the parent
4641556Srgrimes * of all the rest.)
4651556Srgrimes */
4661556Srgrimes
4671556SrgrimesSTATIC void
4681556Srgrimesevalpipe(n)
4691556Srgrimes	union node *n;
47017987Speter{
4711556Srgrimes	struct job *jp;
4721556Srgrimes	struct nodelist *lp;
4731556Srgrimes	int pipelen;
4741556Srgrimes	int prevfd;
4751556Srgrimes	int pip[2];
4761556Srgrimes
47717987Speter	TRACE(("evalpipe(0x%lx) called\n", (long)n));
4781556Srgrimes	pipelen = 0;
4791556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
4801556Srgrimes		pipelen++;
4811556Srgrimes	INTOFF;
4821556Srgrimes	jp = makejob(n, pipelen);
4831556Srgrimes	prevfd = -1;
4841556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
4851556Srgrimes		prehash(lp->n);
4861556Srgrimes		pip[1] = -1;
4871556Srgrimes		if (lp->next) {
4881556Srgrimes			if (pipe(pip) < 0) {
4891556Srgrimes				close(prevfd);
4901556Srgrimes				error("Pipe call failed");
4911556Srgrimes			}
4921556Srgrimes		}
4931556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
4941556Srgrimes			INTON;
4951556Srgrimes			if (prevfd > 0) {
4961556Srgrimes				close(0);
4971556Srgrimes				copyfd(prevfd, 0);
4981556Srgrimes				close(prevfd);
4991556Srgrimes			}
5001556Srgrimes			if (pip[1] >= 0) {
5011556Srgrimes				close(pip[0]);
5021556Srgrimes				if (pip[1] != 1) {
5031556Srgrimes					close(1);
5041556Srgrimes					copyfd(pip[1], 1);
5051556Srgrimes					close(pip[1]);
5061556Srgrimes				}
5071556Srgrimes			}
5081556Srgrimes			evaltree(lp->n, EV_EXIT);
5091556Srgrimes		}
5101556Srgrimes		if (prevfd >= 0)
5111556Srgrimes			close(prevfd);
5121556Srgrimes		prevfd = pip[0];
5131556Srgrimes		close(pip[1]);
5141556Srgrimes	}
5151556Srgrimes	INTON;
5161556Srgrimes	if (n->npipe.backgnd == 0) {
5171556Srgrimes		INTOFF;
5181556Srgrimes		exitstatus = waitforjob(jp);
5191556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5201556Srgrimes		INTON;
5211556Srgrimes	}
5221556Srgrimes}
5231556Srgrimes
5241556Srgrimes
5251556Srgrimes
5261556Srgrimes/*
5271556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5281556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5291556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5301556Srgrimes * Should be called with interrupts off.
5311556Srgrimes */
5321556Srgrimes
5331556Srgrimesvoid
5341556Srgrimesevalbackcmd(n, result)
5351556Srgrimes	union node *n;
5361556Srgrimes	struct backcmd *result;
53717987Speter{
5381556Srgrimes	int pip[2];
5391556Srgrimes	struct job *jp;
5401556Srgrimes	struct stackmark smark;		/* unnecessary */
5411556Srgrimes
5421556Srgrimes	setstackmark(&smark);
5431556Srgrimes	result->fd = -1;
5441556Srgrimes	result->buf = NULL;
5451556Srgrimes	result->nleft = 0;
5461556Srgrimes	result->jp = NULL;
54717987Speter	if (n == NULL) {
54817987Speter		exitstatus = 0;
5491556Srgrimes		goto out;
55017987Speter	}
5511556Srgrimes	if (n->type == NCMD) {
55217987Speter		exitstatus = oexitstatus;
5531556Srgrimes		evalcommand(n, EV_BACKCMD, result);
5541556Srgrimes	} else {
55517987Speter		exitstatus = 0;
5561556Srgrimes		if (pipe(pip) < 0)
5571556Srgrimes			error("Pipe call failed");
5581556Srgrimes		jp = makejob(n, 1);
5591556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
5601556Srgrimes			FORCEINTON;
5611556Srgrimes			close(pip[0]);
5621556Srgrimes			if (pip[1] != 1) {
5631556Srgrimes				close(1);
5641556Srgrimes				copyfd(pip[1], 1);
5651556Srgrimes				close(pip[1]);
5661556Srgrimes			}
5671556Srgrimes			evaltree(n, EV_EXIT);
5681556Srgrimes		}
5691556Srgrimes		close(pip[1]);
5701556Srgrimes		result->fd = pip[0];
5711556Srgrimes		result->jp = jp;
5721556Srgrimes	}
5731556Srgrimesout:
5741556Srgrimes	popstackmark(&smark);
5751556Srgrimes	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
5761556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
5771556Srgrimes}
5781556Srgrimes
5791556Srgrimes
5801556Srgrimes
5811556Srgrimes/*
5821556Srgrimes * Execute a simple command.
5831556Srgrimes */
5841556Srgrimes
5851556SrgrimesSTATIC void
5861556Srgrimesevalcommand(cmd, flags, backcmd)
5871556Srgrimes	union node *cmd;
58817987Speter	int flags;
5891556Srgrimes	struct backcmd *backcmd;
59017987Speter{
5911556Srgrimes	struct stackmark smark;
5921556Srgrimes	union node *argp;
5931556Srgrimes	struct arglist arglist;
5941556Srgrimes	struct arglist varlist;
5951556Srgrimes	char **argv;
5961556Srgrimes	int argc;
5971556Srgrimes	char **envp;
5981556Srgrimes	int varflag;
5991556Srgrimes	struct strlist *sp;
6001556Srgrimes	int mode;
6011556Srgrimes	int pip[2];
6021556Srgrimes	struct cmdentry cmdentry;
6031556Srgrimes	struct job *jp;
6041556Srgrimes	struct jmploc jmploc;
6051556Srgrimes	struct jmploc *volatile savehandler;
6061556Srgrimes	char *volatile savecmdname;
6071556Srgrimes	volatile struct shparam saveparam;
6081556Srgrimes	struct localvar *volatile savelocalvars;
6091556Srgrimes	volatile int e;
6101556Srgrimes	char *lastarg;
61117987Speter#if __GNUC__
61217987Speter	/* Avoid longjmp clobbering */
61317987Speter	(void) &argv;
61417987Speter	(void) &argc;
61517987Speter	(void) &lastarg;
61617987Speter	(void) &flags;
61717987Speter#endif
6181556Srgrimes
6191556Srgrimes	/* First expand the arguments. */
62017987Speter	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
6211556Srgrimes	setstackmark(&smark);
6221556Srgrimes	arglist.lastp = &arglist.list;
6231556Srgrimes	varlist.lastp = &varlist.list;
6241556Srgrimes	varflag = 1;
62517987Speter	oexitstatus = exitstatus;
62617987Speter	exitstatus = 0;
6271556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
62817987Speter		char *p = argp->narg.text;
6291556Srgrimes		if (varflag && is_name(*p)) {
6301556Srgrimes			do {
6311556Srgrimes				p++;
6321556Srgrimes			} while (is_in_name(*p));
6331556Srgrimes			if (*p == '=') {
6341556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6351556Srgrimes				continue;
6361556Srgrimes			}
6371556Srgrimes		}
6381556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6391556Srgrimes		varflag = 0;
6401556Srgrimes	}
6411556Srgrimes	*arglist.lastp = NULL;
6421556Srgrimes	*varlist.lastp = NULL;
6431556Srgrimes	expredir(cmd->ncmd.redirect);
6441556Srgrimes	argc = 0;
6451556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6461556Srgrimes		argc++;
6471556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6481556Srgrimes
6491556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6501556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6511556Srgrimes		*argv++ = sp->text;
6521556Srgrimes	}
6531556Srgrimes	*argv = NULL;
6541556Srgrimes	lastarg = NULL;
6551556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6561556Srgrimes		lastarg = argv[-1];
6571556Srgrimes	argv -= argc;
6581556Srgrimes
6591556Srgrimes	/* Print the command if xflag is set. */
6601556Srgrimes	if (xflag) {
6611556Srgrimes		outc('+', &errout);
6621556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
6631556Srgrimes			outc(' ', &errout);
6641556Srgrimes			out2str(sp->text);
6651556Srgrimes		}
6661556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
6671556Srgrimes			outc(' ', &errout);
6681556Srgrimes			out2str(sp->text);
6691556Srgrimes		}
6701556Srgrimes		outc('\n', &errout);
6711556Srgrimes		flushout(&errout);
6721556Srgrimes	}
6731556Srgrimes
6741556Srgrimes	/* Now locate the command. */
6751556Srgrimes	if (argc == 0) {
6761556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6771556Srgrimes		cmdentry.u.index = BLTINCMD;
6781556Srgrimes	} else {
67917987Speter		static const char PATH[] = "PATH=";
68017987Speter		char *path = pathval();
68117987Speter
68217987Speter		/*
68317987Speter		 * Modify the command lookup path, if a PATH= assignment
68417987Speter		 * is present
68517987Speter		 */
68617987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
68717987Speter			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
68817987Speter				path = sp->text + sizeof(PATH) - 1;
68917987Speter
69017987Speter		find_command(argv[0], &cmdentry, 1, path);
6911556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
69217987Speter			exitstatus = 1;
6931556Srgrimes			flushout(&errout);
6941556Srgrimes			return;
6951556Srgrimes		}
6961556Srgrimes		/* implement the bltin builtin here */
6971556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
6981556Srgrimes			for (;;) {
6991556Srgrimes				argv++;
7001556Srgrimes				if (--argc == 0)
7011556Srgrimes					break;
7021556Srgrimes				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
7031556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
70417987Speter					exitstatus = 1;
7051556Srgrimes					flushout(&errout);
7061556Srgrimes					return;
7071556Srgrimes				}
7081556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7091556Srgrimes					break;
7101556Srgrimes			}
7111556Srgrimes		}
7121556Srgrimes	}
7131556Srgrimes
7141556Srgrimes	/* Fork off a child process if necessary. */
7151556Srgrimes	if (cmd->ncmd.backgnd
71617987Speter	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
71717987Speter	 || ((flags & EV_BACKCMD) != 0
7181556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
7191556Srgrimes		 || cmdentry.u.index == DOTCMD
72017987Speter		 || cmdentry.u.index == EVALCMD))) {
7211556Srgrimes		jp = makejob(cmd, 1);
7221556Srgrimes		mode = cmd->ncmd.backgnd;
7231556Srgrimes		if (flags & EV_BACKCMD) {
7241556Srgrimes			mode = FORK_NOJOB;
7251556Srgrimes			if (pipe(pip) < 0)
7261556Srgrimes				error("Pipe call failed");
7271556Srgrimes		}
7281556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7291556Srgrimes			goto parent;	/* at end of routine */
7301556Srgrimes		if (flags & EV_BACKCMD) {
7311556Srgrimes			FORCEINTON;
7321556Srgrimes			close(pip[0]);
7331556Srgrimes			if (pip[1] != 1) {
7341556Srgrimes				close(1);
7351556Srgrimes				copyfd(pip[1], 1);
7361556Srgrimes				close(pip[1]);
7371556Srgrimes			}
7381556Srgrimes		}
7391556Srgrimes		flags |= EV_EXIT;
7401556Srgrimes	}
7411556Srgrimes
7421556Srgrimes	/* This is the child process if a fork occurred. */
7431556Srgrimes	/* Execute the command. */
7441556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
7451556Srgrimes		trputs("Shell function:  ");  trargs(argv);
7461556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7471556Srgrimes		saveparam = shellparam;
7481556Srgrimes		shellparam.malloc = 0;
7491556Srgrimes		shellparam.nparam = argc - 1;
7501556Srgrimes		shellparam.p = argv + 1;
7511556Srgrimes		shellparam.optnext = NULL;
7521556Srgrimes		INTOFF;
7531556Srgrimes		savelocalvars = localvars;
7541556Srgrimes		localvars = NULL;
7551556Srgrimes		INTON;
7561556Srgrimes		if (setjmp(jmploc.loc)) {
7571556Srgrimes			if (exception == EXSHELLPROC)
7581556Srgrimes				freeparam((struct shparam *)&saveparam);
7591556Srgrimes			else {
7601556Srgrimes				freeparam(&shellparam);
7611556Srgrimes				shellparam = saveparam;
7621556Srgrimes			}
7631556Srgrimes			poplocalvars();
7641556Srgrimes			localvars = savelocalvars;
7651556Srgrimes			handler = savehandler;
7661556Srgrimes			longjmp(handler->loc, 1);
7671556Srgrimes		}
7681556Srgrimes		savehandler = handler;
7691556Srgrimes		handler = &jmploc;
7701556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
7711556Srgrimes			mklocal(sp->text);
7721556Srgrimes		funcnest++;
7731556Srgrimes		evaltree(cmdentry.u.func, 0);
7741556Srgrimes		funcnest--;
7751556Srgrimes		INTOFF;
7761556Srgrimes		poplocalvars();
7771556Srgrimes		localvars = savelocalvars;
7781556Srgrimes		freeparam(&shellparam);
7791556Srgrimes		shellparam = saveparam;
7801556Srgrimes		handler = savehandler;
7811556Srgrimes		popredir();
7821556Srgrimes		INTON;
7831556Srgrimes		if (evalskip == SKIPFUNC) {
7841556Srgrimes			evalskip = 0;
7851556Srgrimes			skipcount = 0;
7861556Srgrimes		}
7871556Srgrimes		if (flags & EV_EXIT)
7881556Srgrimes			exitshell(exitstatus);
7891556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
7901556Srgrimes		trputs("builtin command:  ");  trargs(argv);
7911556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
7921556Srgrimes		if (flags == EV_BACKCMD) {
7931556Srgrimes			memout.nleft = 0;
7941556Srgrimes			memout.nextc = memout.buf;
7951556Srgrimes			memout.bufsize = 64;
7961556Srgrimes			mode |= REDIR_BACKQ;
7971556Srgrimes		}
7981556Srgrimes		redirect(cmd->ncmd.redirect, mode);
7991556Srgrimes		savecmdname = commandname;
8001556Srgrimes		cmdenviron = varlist.list;
8011556Srgrimes		e = -1;
8021556Srgrimes		if (setjmp(jmploc.loc)) {
8031556Srgrimes			e = exception;
8041556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8051556Srgrimes			goto cmddone;
8061556Srgrimes		}
8071556Srgrimes		savehandler = handler;
8081556Srgrimes		handler = &jmploc;
8091556Srgrimes		commandname = argv[0];
8101556Srgrimes		argptr = argv + 1;
8111556Srgrimes		optptr = NULL;			/* initialize nextopt */
8121556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8131556Srgrimes		flushall();
8141556Srgrimescmddone:
8151556Srgrimes		out1 = &output;
8161556Srgrimes		out2 = &errout;
8171556Srgrimes		freestdout();
8181556Srgrimes		if (e != EXSHELLPROC) {
8191556Srgrimes			commandname = savecmdname;
8201556Srgrimes			if (flags & EV_EXIT) {
8211556Srgrimes				exitshell(exitstatus);
8221556Srgrimes			}
8231556Srgrimes		}
8241556Srgrimes		handler = savehandler;
8251556Srgrimes		if (e != -1) {
8261556Srgrimes			if (e != EXERROR || cmdentry.u.index == BLTINCMD
8271556Srgrimes					       || cmdentry.u.index == DOTCMD
8281556Srgrimes					       || cmdentry.u.index == EVALCMD
82917987Speter#ifndef NO_HISTORY
8301556Srgrimes					       || cmdentry.u.index == HISTCMD
83117987Speter#endif
8321556Srgrimes					       || cmdentry.u.index == EXECCMD)
8331556Srgrimes				exraise(e);
8341556Srgrimes			FORCEINTON;
8351556Srgrimes		}
8361556Srgrimes		if (cmdentry.u.index != EXECCMD)
8371556Srgrimes			popredir();
8381556Srgrimes		if (flags == EV_BACKCMD) {
8391556Srgrimes			backcmd->buf = memout.buf;
8401556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8411556Srgrimes			memout.buf = NULL;
8421556Srgrimes		}
8431556Srgrimes	} else {
8441556Srgrimes		trputs("normal command:  ");  trargs(argv);
8451556Srgrimes		clearredir();
8461556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8471556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8481556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8491556Srgrimes		envp = environment();
85017987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8511556Srgrimes		/*NOTREACHED*/
8521556Srgrimes	}
8531556Srgrimes	goto out;
8541556Srgrimes
8551556Srgrimesparent:	/* parent process gets here (if we forked) */
8561556Srgrimes	if (mode == 0) {	/* argument to fork */
8571556Srgrimes		INTOFF;
8581556Srgrimes		exitstatus = waitforjob(jp);
8591556Srgrimes		INTON;
8601556Srgrimes	} else if (mode == 2) {
8611556Srgrimes		backcmd->fd = pip[0];
8621556Srgrimes		close(pip[1]);
8631556Srgrimes		backcmd->jp = jp;
8641556Srgrimes	}
8651556Srgrimes
8661556Srgrimesout:
8671556Srgrimes	if (lastarg)
8681556Srgrimes		setvar("_", lastarg, 0);
8691556Srgrimes	popstackmark(&smark);
8701556Srgrimes}
8711556Srgrimes
8721556Srgrimes
8731556Srgrimes
8741556Srgrimes/*
8751556Srgrimes * Search for a command.  This is called before we fork so that the
8761556Srgrimes * location of the command will be available in the parent as well as
8771556Srgrimes * the child.  The check for "goodname" is an overly conservative
8781556Srgrimes * check that the name will not be subject to expansion.
8791556Srgrimes */
8801556Srgrimes
8811556SrgrimesSTATIC void
8821556Srgrimesprehash(n)
8831556Srgrimes	union node *n;
88417987Speter{
8851556Srgrimes	struct cmdentry entry;
8861556Srgrimes
88717987Speter	if (n->type == NCMD && n->ncmd.args)
88817987Speter		if (goodname(n->ncmd.args->narg.text))
88917987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
89017987Speter				     pathval());
8911556Srgrimes}
8921556Srgrimes
8931556Srgrimes
8941556Srgrimes
8951556Srgrimes/*
8961556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
8971556Srgrimes * tied to evaluation are implemented here.
8981556Srgrimes */
8991556Srgrimes
9001556Srgrimes/*
9011556Srgrimes * No command given, or a bltin command with no arguments.  Set the
9021556Srgrimes * specified variables.
9031556Srgrimes */
9041556Srgrimes
90517987Speterint
90617987Speterbltincmd(argc, argv)
90717987Speter	int argc;
90817987Speter	char **argv;
90917987Speter{
9101556Srgrimes	listsetvar(cmdenviron);
91117987Speter	/*
91217987Speter	 * Preserve exitstatus of a previous possible redirection
91317987Speter	 * as POSIX mandates
91417987Speter	 */
9151556Srgrimes	return exitstatus;
9161556Srgrimes}
9171556Srgrimes
9181556Srgrimes
9191556Srgrimes/*
9201556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9211556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9221556Srgrimes * above all check this flag, and if it is set they start skipping
9231556Srgrimes * commands rather than executing them.  The variable skipcount is
9241556Srgrimes * the number of loops to break/continue, or the number of function
9251556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9261556Srgrimes * be an error to break out of more loops than exist, but it isn't
9271556Srgrimes * in the standard shell so we don't make it one here.
9281556Srgrimes */
9291556Srgrimes
93017987Speterint
93117987Speterbreakcmd(argc, argv)
93217987Speter	int argc;
93317987Speter	char **argv;
93417987Speter{
9351556Srgrimes	int n;
9361556Srgrimes
9371556Srgrimes	n = 1;
9381556Srgrimes	if (argc > 1)
9391556Srgrimes		n = number(argv[1]);
9401556Srgrimes	if (n > loopnest)
9411556Srgrimes		n = loopnest;
9421556Srgrimes	if (n > 0) {
9431556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9441556Srgrimes		skipcount = n;
9451556Srgrimes	}
9461556Srgrimes	return 0;
9471556Srgrimes}
9481556Srgrimes
9491556Srgrimes
9501556Srgrimes/*
9511556Srgrimes * The return command.
9521556Srgrimes */
9531556Srgrimes
95417987Speterint
95517987Speterreturncmd(argc, argv)
95617987Speter	int argc;
95717987Speter	char **argv;
95817987Speter{
9591556Srgrimes	int ret;
9601556Srgrimes
9611556Srgrimes	ret = exitstatus;
9621556Srgrimes	if (argc > 1)
9631556Srgrimes		ret = number(argv[1]);
9641556Srgrimes	if (funcnest) {
9651556Srgrimes		evalskip = SKIPFUNC;
9661556Srgrimes		skipcount = 1;
9671556Srgrimes	}
9681556Srgrimes	return ret;
9691556Srgrimes}
9701556Srgrimes
9711556Srgrimes
97217987Speterint
97317987Speterfalsecmd(argc, argv)
97417987Speter	int argc;
97517987Speter	char **argv;
97617987Speter{
97717987Speter	return 1;
97817987Speter}
97917987Speter
98017987Speter
98117987Speterint
98217987Spetertruecmd(argc, argv)
98317987Speter	int argc;
98417987Speter	char **argv;
98517987Speter{
9861556Srgrimes	return 0;
9871556Srgrimes}
9881556Srgrimes
9891556Srgrimes
99017987Speterint
99117987Speterexeccmd(argc, argv)
99217987Speter	int argc;
99317987Speter	char **argv;
99417987Speter{
9951556Srgrimes	if (argc > 1) {
99617987Speter		struct strlist *sp;
99717987Speter
9981556Srgrimes		iflag = 0;		/* exit on error */
9991556Srgrimes		mflag = 0;
10001556Srgrimes		optschanged();
100117987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
100217987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10031556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10041556Srgrimes
10051556Srgrimes	}
10061556Srgrimes	return 0;
10071556Srgrimes}
1008