eval.c revision 53891
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 53891 1999-11-29 19:11:01Z 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;
61517987Speter#if __GNUC__
61617987Speter	/* Avoid longjmp clobbering */
61717987Speter	(void) &argv;
61817987Speter	(void) &argc;
61917987Speter	(void) &lastarg;
62017987Speter	(void) &flags;
62117987Speter#endif
6221556Srgrimes
6231556Srgrimes	/* First expand the arguments. */
62417987Speter	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
6251556Srgrimes	setstackmark(&smark);
6261556Srgrimes	arglist.lastp = &arglist.list;
6271556Srgrimes	varlist.lastp = &varlist.list;
6281556Srgrimes	varflag = 1;
62917987Speter	oexitstatus = exitstatus;
63017987Speter	exitstatus = 0;
6311556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
63217987Speter		char *p = argp->narg.text;
6331556Srgrimes		if (varflag && is_name(*p)) {
6341556Srgrimes			do {
6351556Srgrimes				p++;
6361556Srgrimes			} while (is_in_name(*p));
6371556Srgrimes			if (*p == '=') {
6381556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6391556Srgrimes				continue;
6401556Srgrimes			}
6411556Srgrimes		}
6421556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6431556Srgrimes		varflag = 0;
6441556Srgrimes	}
6451556Srgrimes	*arglist.lastp = NULL;
6461556Srgrimes	*varlist.lastp = NULL;
6471556Srgrimes	expredir(cmd->ncmd.redirect);
6481556Srgrimes	argc = 0;
6491556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6501556Srgrimes		argc++;
6511556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6521556Srgrimes
6531556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6541556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6551556Srgrimes		*argv++ = sp->text;
6561556Srgrimes	}
6571556Srgrimes	*argv = NULL;
6581556Srgrimes	lastarg = NULL;
6591556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6601556Srgrimes		lastarg = argv[-1];
6611556Srgrimes	argv -= argc;
6621556Srgrimes
6631556Srgrimes	/* Print the command if xflag is set. */
6641556Srgrimes	if (xflag) {
6651556Srgrimes		outc('+', &errout);
6661556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
6671556Srgrimes			outc(' ', &errout);
6681556Srgrimes			out2str(sp->text);
6691556Srgrimes		}
6701556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
6711556Srgrimes			outc(' ', &errout);
6721556Srgrimes			out2str(sp->text);
6731556Srgrimes		}
6741556Srgrimes		outc('\n', &errout);
6751556Srgrimes		flushout(&errout);
6761556Srgrimes	}
6771556Srgrimes
6781556Srgrimes	/* Now locate the command. */
6791556Srgrimes	if (argc == 0) {
6801556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
6811556Srgrimes		cmdentry.u.index = BLTINCMD;
6821556Srgrimes	} else {
68317987Speter		static const char PATH[] = "PATH=";
68417987Speter		char *path = pathval();
68517987Speter
68617987Speter		/*
68717987Speter		 * Modify the command lookup path, if a PATH= assignment
68817987Speter		 * is present
68917987Speter		 */
69017987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
69117987Speter			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
69217987Speter				path = sp->text + sizeof(PATH) - 1;
69317987Speter
69417987Speter		find_command(argv[0], &cmdentry, 1, path);
6951556Srgrimes		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
69620425Ssteve			exitstatus = 127;
6971556Srgrimes			flushout(&errout);
6981556Srgrimes			return;
6991556Srgrimes		}
7001556Srgrimes		/* implement the bltin builtin here */
7011556Srgrimes		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
7021556Srgrimes			for (;;) {
7031556Srgrimes				argv++;
7041556Srgrimes				if (--argc == 0)
7051556Srgrimes					break;
7061556Srgrimes				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
7071556Srgrimes					outfmt(&errout, "%s: not found\n", *argv);
70820425Ssteve					exitstatus = 127;
7091556Srgrimes					flushout(&errout);
7101556Srgrimes					return;
7111556Srgrimes				}
7121556Srgrimes				if (cmdentry.u.index != BLTINCMD)
7131556Srgrimes					break;
7141556Srgrimes			}
7151556Srgrimes		}
7161556Srgrimes	}
7171556Srgrimes
7181556Srgrimes	/* Fork off a child process if necessary. */
7191556Srgrimes	if (cmd->ncmd.backgnd
72045221Scracauer	 || (cmdentry.cmdtype == CMDNORMAL
72145221Scracauer	    && ((flags & EV_EXIT) == 0 || Tflag))
72217987Speter	 || ((flags & EV_BACKCMD) != 0
7231556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
72448896Ssheldonh		 || cmdentry.u.index == CDCMD
7251556Srgrimes		 || cmdentry.u.index == DOTCMD
72617987Speter		 || cmdentry.u.index == EVALCMD))) {
7271556Srgrimes		jp = makejob(cmd, 1);
7281556Srgrimes		mode = cmd->ncmd.backgnd;
7291556Srgrimes		if (flags & EV_BACKCMD) {
7301556Srgrimes			mode = FORK_NOJOB;
7311556Srgrimes			if (pipe(pip) < 0)
73253891Scracauer				error("Pipe call failed: %s", strerror(errno));
7331556Srgrimes		}
7341556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
7351556Srgrimes			goto parent;	/* at end of routine */
7361556Srgrimes		if (flags & EV_BACKCMD) {
7371556Srgrimes			FORCEINTON;
7381556Srgrimes			close(pip[0]);
7391556Srgrimes			if (pip[1] != 1) {
7401556Srgrimes				close(1);
7411556Srgrimes				copyfd(pip[1], 1);
7421556Srgrimes				close(pip[1]);
7431556Srgrimes			}
7441556Srgrimes		}
7451556Srgrimes		flags |= EV_EXIT;
7461556Srgrimes	}
7471556Srgrimes
7481556Srgrimes	/* This is the child process if a fork occurred. */
7491556Srgrimes	/* Execute the command. */
7501556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
75120425Ssteve#ifdef DEBUG
7521556Srgrimes		trputs("Shell function:  ");  trargs(argv);
75320425Ssteve#endif
7541556Srgrimes		redirect(cmd->ncmd.redirect, REDIR_PUSH);
7551556Srgrimes		saveparam = shellparam;
7561556Srgrimes		shellparam.malloc = 0;
75720425Ssteve		shellparam.reset = 1;
7581556Srgrimes		shellparam.nparam = argc - 1;
7591556Srgrimes		shellparam.p = argv + 1;
7601556Srgrimes		shellparam.optnext = NULL;
7611556Srgrimes		INTOFF;
7621556Srgrimes		savelocalvars = localvars;
7631556Srgrimes		localvars = NULL;
7641556Srgrimes		INTON;
7651556Srgrimes		if (setjmp(jmploc.loc)) {
7661556Srgrimes			if (exception == EXSHELLPROC)
7671556Srgrimes				freeparam((struct shparam *)&saveparam);
7681556Srgrimes			else {
7691556Srgrimes				freeparam(&shellparam);
7701556Srgrimes				shellparam = saveparam;
7711556Srgrimes			}
7721556Srgrimes			poplocalvars();
7731556Srgrimes			localvars = savelocalvars;
7741556Srgrimes			handler = savehandler;
7751556Srgrimes			longjmp(handler->loc, 1);
7761556Srgrimes		}
7771556Srgrimes		savehandler = handler;
7781556Srgrimes		handler = &jmploc;
7791556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
7801556Srgrimes			mklocal(sp->text);
7811556Srgrimes		funcnest++;
78235675Scracauer		if (flags & EV_TESTED)
78335675Scracauer			evaltree(cmdentry.u.func, EV_TESTED);
78435675Scracauer		else
78535675Scracauer			evaltree(cmdentry.u.func, 0);
7861556Srgrimes		funcnest--;
7871556Srgrimes		INTOFF;
7881556Srgrimes		poplocalvars();
7891556Srgrimes		localvars = savelocalvars;
7901556Srgrimes		freeparam(&shellparam);
7911556Srgrimes		shellparam = saveparam;
7921556Srgrimes		handler = savehandler;
7931556Srgrimes		popredir();
7941556Srgrimes		INTON;
7951556Srgrimes		if (evalskip == SKIPFUNC) {
7961556Srgrimes			evalskip = 0;
7971556Srgrimes			skipcount = 0;
7981556Srgrimes		}
7991556Srgrimes		if (flags & EV_EXIT)
8001556Srgrimes			exitshell(exitstatus);
8011556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
80220425Ssteve#ifdef DEBUG
8031556Srgrimes		trputs("builtin command:  ");  trargs(argv);
80420425Ssteve#endif
8051556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
8061556Srgrimes		if (flags == EV_BACKCMD) {
8071556Srgrimes			memout.nleft = 0;
8081556Srgrimes			memout.nextc = memout.buf;
8091556Srgrimes			memout.bufsize = 64;
8101556Srgrimes			mode |= REDIR_BACKQ;
8111556Srgrimes		}
8121556Srgrimes		redirect(cmd->ncmd.redirect, mode);
8131556Srgrimes		savecmdname = commandname;
8141556Srgrimes		cmdenviron = varlist.list;
8151556Srgrimes		e = -1;
8161556Srgrimes		if (setjmp(jmploc.loc)) {
8171556Srgrimes			e = exception;
8181556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
8191556Srgrimes			goto cmddone;
8201556Srgrimes		}
8211556Srgrimes		savehandler = handler;
8221556Srgrimes		handler = &jmploc;
8231556Srgrimes		commandname = argv[0];
8241556Srgrimes		argptr = argv + 1;
8251556Srgrimes		optptr = NULL;			/* initialize nextopt */
8261556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
8271556Srgrimes		flushall();
8281556Srgrimescmddone:
8291556Srgrimes		out1 = &output;
8301556Srgrimes		out2 = &errout;
8311556Srgrimes		freestdout();
8321556Srgrimes		if (e != EXSHELLPROC) {
8331556Srgrimes			commandname = savecmdname;
8341556Srgrimes			if (flags & EV_EXIT) {
8351556Srgrimes				exitshell(exitstatus);
8361556Srgrimes			}
8371556Srgrimes		}
8381556Srgrimes		handler = savehandler;
8391556Srgrimes		if (e != -1) {
84020425Ssteve			if ((e != EXERROR && e != EXEXEC)
84120425Ssteve			   || cmdentry.u.index == BLTINCMD
84220425Ssteve			   || cmdentry.u.index == DOTCMD
84320425Ssteve			   || cmdentry.u.index == EVALCMD
84417987Speter#ifndef NO_HISTORY
84520425Ssteve			   || cmdentry.u.index == HISTCMD
84617987Speter#endif
84720425Ssteve			   || cmdentry.u.index == EXECCMD)
8481556Srgrimes				exraise(e);
8491556Srgrimes			FORCEINTON;
8501556Srgrimes		}
8511556Srgrimes		if (cmdentry.u.index != EXECCMD)
8521556Srgrimes			popredir();
8531556Srgrimes		if (flags == EV_BACKCMD) {
8541556Srgrimes			backcmd->buf = memout.buf;
8551556Srgrimes			backcmd->nleft = memout.nextc - memout.buf;
8561556Srgrimes			memout.buf = NULL;
8571556Srgrimes		}
8581556Srgrimes	} else {
85920425Ssteve#ifdef DEBUG
8601556Srgrimes		trputs("normal command:  ");  trargs(argv);
86120425Ssteve#endif
8621556Srgrimes		clearredir();
8631556Srgrimes		redirect(cmd->ncmd.redirect, 0);
8641556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8651556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
8661556Srgrimes		envp = environment();
86717987Speter		shellexec(argv, envp, pathval(), cmdentry.u.index);
8681556Srgrimes		/*NOTREACHED*/
8691556Srgrimes	}
8701556Srgrimes	goto out;
8711556Srgrimes
8721556Srgrimesparent:	/* parent process gets here (if we forked) */
8731556Srgrimes	if (mode == 0) {	/* argument to fork */
8741556Srgrimes		INTOFF;
87545916Scracauer		exitstatus = waitforjob(jp, &realstatus);
8761556Srgrimes		INTON;
87745916Scracauer		if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
87845266Scracauer			evalskip = SKIPBREAK;
87945266Scracauer			skipcount = loopnest;
88045266Scracauer		}
8811556Srgrimes	} else if (mode == 2) {
8821556Srgrimes		backcmd->fd = pip[0];
8831556Srgrimes		close(pip[1]);
8841556Srgrimes		backcmd->jp = jp;
8851556Srgrimes	}
8861556Srgrimes
8871556Srgrimesout:
8881556Srgrimes	if (lastarg)
8891556Srgrimes		setvar("_", lastarg, 0);
8901556Srgrimes	popstackmark(&smark);
8911556Srgrimes}
8921556Srgrimes
8931556Srgrimes
8941556Srgrimes
8951556Srgrimes/*
8961556Srgrimes * Search for a command.  This is called before we fork so that the
8971556Srgrimes * location of the command will be available in the parent as well as
8981556Srgrimes * the child.  The check for "goodname" is an overly conservative
8991556Srgrimes * check that the name will not be subject to expansion.
9001556Srgrimes */
9011556Srgrimes
9021556SrgrimesSTATIC void
9031556Srgrimesprehash(n)
9041556Srgrimes	union node *n;
90517987Speter{
9061556Srgrimes	struct cmdentry entry;
9071556Srgrimes
90817987Speter	if (n->type == NCMD && n->ncmd.args)
90917987Speter		if (goodname(n->ncmd.args->narg.text))
91017987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
91117987Speter				     pathval());
9121556Srgrimes}
9131556Srgrimes
9141556Srgrimes
9151556Srgrimes
9161556Srgrimes/*
9171556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
9181556Srgrimes * tied to evaluation are implemented here.
9191556Srgrimes */
9201556Srgrimes
9211556Srgrimes/*
9221556Srgrimes * No command given, or a bltin command with no arguments.  Set the
9231556Srgrimes * specified variables.
9241556Srgrimes */
9251556Srgrimes
92617987Speterint
92717987Speterbltincmd(argc, argv)
92825905Ssteve	int argc __unused;
92925905Ssteve	char **argv __unused;
93017987Speter{
9311556Srgrimes	listsetvar(cmdenviron);
93220425Ssteve	/*
93317987Speter	 * Preserve exitstatus of a previous possible redirection
93420425Ssteve	 * as POSIX mandates
93517987Speter	 */
9361556Srgrimes	return exitstatus;
9371556Srgrimes}
9381556Srgrimes
9391556Srgrimes
9401556Srgrimes/*
9411556Srgrimes * Handle break and continue commands.  Break, continue, and return are
9421556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
9431556Srgrimes * above all check this flag, and if it is set they start skipping
9441556Srgrimes * commands rather than executing them.  The variable skipcount is
9451556Srgrimes * the number of loops to break/continue, or the number of function
9461556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
9471556Srgrimes * be an error to break out of more loops than exist, but it isn't
9481556Srgrimes * in the standard shell so we don't make it one here.
9491556Srgrimes */
9501556Srgrimes
95117987Speterint
95217987Speterbreakcmd(argc, argv)
95317987Speter	int argc;
95420425Ssteve	char **argv;
95517987Speter{
95620425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
9571556Srgrimes
9581556Srgrimes	if (n > loopnest)
9591556Srgrimes		n = loopnest;
9601556Srgrimes	if (n > 0) {
9611556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
9621556Srgrimes		skipcount = n;
9631556Srgrimes	}
9641556Srgrimes	return 0;
9651556Srgrimes}
9661556Srgrimes
9671556Srgrimes
9681556Srgrimes/*
9691556Srgrimes * The return command.
9701556Srgrimes */
9711556Srgrimes
97217987Speterint
97320425Sstevereturncmd(argc, argv)
97417987Speter	int argc;
97520425Ssteve	char **argv;
97617987Speter{
97720425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
9781556Srgrimes
9791556Srgrimes	if (funcnest) {
9801556Srgrimes		evalskip = SKIPFUNC;
9811556Srgrimes		skipcount = 1;
98220425Ssteve	} else {
98320425Ssteve		/* skip the rest of the file */
98420425Ssteve		evalskip = SKIPFILE;
98520425Ssteve		skipcount = 1;
9861556Srgrimes	}
9871556Srgrimes	return ret;
9881556Srgrimes}
9891556Srgrimes
9901556Srgrimes
99117987Speterint
99220425Sstevefalsecmd(argc, argv)
99325905Ssteve	int argc __unused;
99425905Ssteve	char **argv __unused;
99517987Speter{
99617987Speter	return 1;
99717987Speter}
99817987Speter
99917987Speter
100017987Speterint
100120425Sstevetruecmd(argc, argv)
100225905Ssteve	int argc __unused;
100325905Ssteve	char **argv __unused;
100417987Speter{
10051556Srgrimes	return 0;
10061556Srgrimes}
10071556Srgrimes
10081556Srgrimes
100917987Speterint
101020425Ssteveexeccmd(argc, argv)
101117987Speter	int argc;
101220425Ssteve	char **argv;
101317987Speter{
10141556Srgrimes	if (argc > 1) {
101517987Speter		struct strlist *sp;
101617987Speter
10171556Srgrimes		iflag = 0;		/* exit on error */
10181556Srgrimes		mflag = 0;
10191556Srgrimes		optschanged();
102017987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
102117987Speter			setvareq(sp->text, VEXPORT|VSTACK);
10221556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
10231556Srgrimes
10241556Srgrimes	}
10251556Srgrimes	return 0;
10261556Srgrimes}
1027