eval.c revision 214599
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 * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/eval.c 214599 2010-10-31 12:06:02Z jilles $");
401556Srgrimes
41100437Stjr#include <paths.h>
4217987Speter#include <signal.h>
43102576Skeramida#include <stdlib.h>
4417987Speter#include <unistd.h>
45153091Sstefanf#include <sys/resource.h>
4645266Scracauer#include <sys/wait.h> /* For WIFSIGNALED(status) */
4753891Scracauer#include <errno.h>
4817987Speter
491556Srgrimes/*
501556Srgrimes * Evaluate a command.
511556Srgrimes */
521556Srgrimes
531556Srgrimes#include "shell.h"
541556Srgrimes#include "nodes.h"
551556Srgrimes#include "syntax.h"
561556Srgrimes#include "expand.h"
571556Srgrimes#include "parser.h"
581556Srgrimes#include "jobs.h"
591556Srgrimes#include "eval.h"
601556Srgrimes#include "builtins.h"
611556Srgrimes#include "options.h"
621556Srgrimes#include "exec.h"
631556Srgrimes#include "redir.h"
641556Srgrimes#include "input.h"
651556Srgrimes#include "output.h"
661556Srgrimes#include "trap.h"
671556Srgrimes#include "var.h"
681556Srgrimes#include "memalloc.h"
691556Srgrimes#include "error.h"
7017987Speter#include "show.h"
711556Srgrimes#include "mystring.h"
7217987Speter#ifndef NO_HISTORY
731556Srgrimes#include "myhistedit.h"
7417987Speter#endif
751556Srgrimes
761556Srgrimes
77201053Sjillesint evalskip;			/* set if we are skipping commands */
78213760Sobrienstatic int skipcount;		/* number of levels to skip */
791556SrgrimesMKINIT int loopnest;		/* current loop nesting level */
801556Srgrimesint funcnest;			/* depth of function calls */
81213760Sobrienstatic int builtin_flags;	/* evalcommand flags for builtins */
821556Srgrimes
831556Srgrimes
841556Srgrimeschar *commandname;
851556Srgrimesstruct strlist *cmdenviron;
861556Srgrimesint exitstatus;			/* exit status of last command */
8717987Speterint oexitstatus;		/* saved exit status */
881556Srgrimes
891556Srgrimes
90213811Sobrienstatic void evalloop(union node *, int);
91213811Sobrienstatic void evalfor(union node *, int);
92213811Sobrienstatic void evalcase(union node *, int);
93213811Sobrienstatic void evalsubshell(union node *, int);
94213811Sobrienstatic void evalredir(union node *, int);
95213811Sobrienstatic void expredir(union node *);
96213811Sobrienstatic void evalpipe(union node *);
97213811Sobrienstatic void evalcommand(union node *, int, struct backcmd *);
98213811Sobrienstatic void prehash(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/*
12246684Skris * The eval command.
1231556Srgrimes */
1241556Srgrimes
12517987Speterint
12690111Simpevalcmd(int argc, char **argv)
1271556Srgrimes{
1281556Srgrimes        char *p;
1291556Srgrimes        char *concat;
1301556Srgrimes        char **ap;
1311556Srgrimes
1321556Srgrimes        if (argc > 1) {
1331556Srgrimes                p = argv[1];
1341556Srgrimes                if (argc > 2) {
1351556Srgrimes                        STARTSTACKSTR(concat);
1361556Srgrimes                        ap = argv + 2;
1371556Srgrimes                        for (;;) {
1381556Srgrimes                                while (*p)
1391556Srgrimes                                        STPUTC(*p++, concat);
1401556Srgrimes                                if ((p = *ap++) == NULL)
1411556Srgrimes                                        break;
1421556Srgrimes                                STPUTC(' ', concat);
1431556Srgrimes                        }
1441556Srgrimes                        STPUTC('\0', concat);
1451556Srgrimes                        p = grabstackstr(concat);
1461556Srgrimes                }
147193169Sstefanf                evalstring(p, builtin_flags & EV_TESTED);
148210829Sjilles        } else
149210829Sjilles                exitstatus = 0;
1501556Srgrimes        return exitstatus;
1511556Srgrimes}
1521556Srgrimes
1531556Srgrimes
1541556Srgrimes/*
1551556Srgrimes * Execute a command or commands contained in a string.
1561556Srgrimes */
1571556Srgrimes
1581556Srgrimesvoid
159193169Sstefanfevalstring(char *s, int flags)
16090111Simp{
1611556Srgrimes	union node *n;
1621556Srgrimes	struct stackmark smark;
163194128Sjilles	int flags_exit;
164210829Sjilles	int any;
1651556Srgrimes
166194128Sjilles	flags_exit = flags & EV_EXIT;
167194128Sjilles	flags &= ~EV_EXIT;
168210829Sjilles	any = 0;
1691556Srgrimes	setstackmark(&smark);
1701556Srgrimes	setinputstring(s, 1);
1711556Srgrimes	while ((n = parsecmd(0)) != NEOF) {
172194128Sjilles		if (n != NULL) {
173194128Sjilles			if (flags_exit && preadateof())
174194128Sjilles				evaltree(n, flags | EV_EXIT);
175194128Sjilles			else
176194128Sjilles				evaltree(n, flags);
177210829Sjilles			any = 1;
178194128Sjilles		}
1791556Srgrimes		popstackmark(&smark);
1801556Srgrimes	}
1811556Srgrimes	popfile();
1821556Srgrimes	popstackmark(&smark);
183210829Sjilles	if (!any)
184210829Sjilles		exitstatus = 0;
185194128Sjilles	if (flags_exit)
186194128Sjilles		exitshell(exitstatus);
1871556Srgrimes}
1881556Srgrimes
1891556Srgrimes
1901556Srgrimes/*
1911556Srgrimes * Evaluate a parse tree.  The value is left in the global variable
1921556Srgrimes * exitstatus.
1931556Srgrimes */
1941556Srgrimes
1951556Srgrimesvoid
19690111Simpevaltree(union node *n, int flags)
19717987Speter{
198149927Sstefanf	int do_etest;
199214599Sjilles	union node *next;
200149927Sstefanf
201149927Sstefanf	do_etest = 0;
2021556Srgrimes	if (n == NULL) {
2031556Srgrimes		TRACE(("evaltree(NULL) called\n"));
2041556Srgrimes		exitstatus = 0;
2051556Srgrimes		goto out;
2061556Srgrimes	}
207214599Sjilles	do {
208214599Sjilles	next = NULL;
20917987Speter#ifndef NO_HISTORY
2101556Srgrimes	displayhist = 1;	/* show history substitutions done with fc */
21117987Speter#endif
212149802Sstefanf	TRACE(("evaltree(%p: %d) called\n", (void *)n, n->type));
2131556Srgrimes	switch (n->type) {
2141556Srgrimes	case NSEMI:
215149932Sstefanf		evaltree(n->nbinary.ch1, flags & ~EV_EXIT);
2161556Srgrimes		if (evalskip)
2171556Srgrimes			goto out;
218214599Sjilles		next = n->nbinary.ch2;
2191556Srgrimes		break;
2201556Srgrimes	case NAND:
2211556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
22218754Ssteve		if (evalskip || exitstatus != 0) {
2231556Srgrimes			goto out;
22418754Ssteve		}
225214599Sjilles		next = n->nbinary.ch2;
2261556Srgrimes		break;
2271556Srgrimes	case NOR:
2281556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
2291556Srgrimes		if (evalskip || exitstatus == 0)
2301556Srgrimes			goto out;
231214599Sjilles		next = n->nbinary.ch2;
2321556Srgrimes		break;
2331556Srgrimes	case NREDIR:
234205154Sjilles		evalredir(n, flags);
2351556Srgrimes		break;
2361556Srgrimes	case NSUBSHELL:
2371556Srgrimes		evalsubshell(n, flags);
238149927Sstefanf		do_etest = !(flags & EV_TESTED);
2391556Srgrimes		break;
2401556Srgrimes	case NBACKGND:
2411556Srgrimes		evalsubshell(n, flags);
2421556Srgrimes		break;
2431556Srgrimes	case NIF: {
2441556Srgrimes		evaltree(n->nif.test, EV_TESTED);
2451556Srgrimes		if (evalskip)
2461556Srgrimes			goto out;
24720425Ssteve		if (exitstatus == 0)
248214599Sjilles			next = n->nif.ifpart;
24917987Speter		else if (n->nif.elsepart)
250214599Sjilles			next = n->nif.elsepart;
25120425Ssteve		else
25220425Ssteve			exitstatus = 0;
2531556Srgrimes		break;
2541556Srgrimes	}
2551556Srgrimes	case NWHILE:
2561556Srgrimes	case NUNTIL:
257149933Sstefanf		evalloop(n, flags & ~EV_EXIT);
2581556Srgrimes		break;
2591556Srgrimes	case NFOR:
260149933Sstefanf		evalfor(n, flags & ~EV_EXIT);
2611556Srgrimes		break;
2621556Srgrimes	case NCASE:
2631556Srgrimes		evalcase(n, flags);
2641556Srgrimes		break;
2651556Srgrimes	case NDEFUN:
2661556Srgrimes		defun(n->narg.text, n->narg.next);
2671556Srgrimes		exitstatus = 0;
2681556Srgrimes		break;
2691556Srgrimes	case NNOT:
2701556Srgrimes		evaltree(n->nnot.com, EV_TESTED);
2711556Srgrimes		exitstatus = !exitstatus;
2721556Srgrimes		break;
2731556Srgrimes
2741556Srgrimes	case NPIPE:
2751556Srgrimes		evalpipe(n);
276149927Sstefanf		do_etest = !(flags & EV_TESTED);
2771556Srgrimes		break;
2781556Srgrimes	case NCMD:
2791556Srgrimes		evalcommand(n, flags, (struct backcmd *)NULL);
280149927Sstefanf		do_etest = !(flags & EV_TESTED);
2811556Srgrimes		break;
2821556Srgrimes	default:
2831556Srgrimes		out1fmt("Node type = %d\n", n->type);
2841556Srgrimes		flushout(&output);
2851556Srgrimes		break;
2861556Srgrimes	}
287214599Sjilles	n = next;
288214599Sjilles	} while (n != NULL);
2891556Srgrimesout:
2901556Srgrimes	if (pendingsigs)
2911556Srgrimes		dotrap();
292149927Sstefanf	if ((flags & EV_EXIT) || (eflag && exitstatus != 0 && do_etest))
2931556Srgrimes		exitshell(exitstatus);
2941556Srgrimes}
2951556Srgrimes
2961556Srgrimes
297213811Sobrienstatic void
298149933Sstefanfevalloop(union node *n, int flags)
29917987Speter{
3001556Srgrimes	int status;
3011556Srgrimes
3021556Srgrimes	loopnest++;
3031556Srgrimes	status = 0;
3041556Srgrimes	for (;;) {
3051556Srgrimes		evaltree(n->nbinary.ch1, EV_TESTED);
3061556Srgrimes		if (evalskip) {
3071556Srgrimesskipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
3081556Srgrimes				evalskip = 0;
3091556Srgrimes				continue;
3101556Srgrimes			}
3111556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3121556Srgrimes				evalskip = 0;
313212475Sjilles			if (evalskip == SKIPFUNC || evalskip == SKIPFILE)
314212475Sjilles				status = exitstatus;
3151556Srgrimes			break;
3161556Srgrimes		}
3171556Srgrimes		if (n->type == NWHILE) {
3181556Srgrimes			if (exitstatus != 0)
3191556Srgrimes				break;
3201556Srgrimes		} else {
3211556Srgrimes			if (exitstatus == 0)
3221556Srgrimes				break;
3231556Srgrimes		}
324149933Sstefanf		evaltree(n->nbinary.ch2, flags);
3251556Srgrimes		status = exitstatus;
3261556Srgrimes		if (evalskip)
3271556Srgrimes			goto skipping;
3281556Srgrimes	}
3291556Srgrimes	loopnest--;
3301556Srgrimes	exitstatus = status;
3311556Srgrimes}
3321556Srgrimes
3331556Srgrimes
3341556Srgrimes
335213811Sobrienstatic void
336149933Sstefanfevalfor(union node *n, int flags)
33717987Speter{
3381556Srgrimes	struct arglist arglist;
3391556Srgrimes	union node *argp;
3401556Srgrimes	struct strlist *sp;
3411556Srgrimes	struct stackmark smark;
3421556Srgrimes
3431556Srgrimes	setstackmark(&smark);
3441556Srgrimes	arglist.lastp = &arglist.list;
3451556Srgrimes	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
34617987Speter		oexitstatus = exitstatus;
3471556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
3481556Srgrimes		if (evalskip)
3491556Srgrimes			goto out;
3501556Srgrimes	}
3511556Srgrimes	*arglist.lastp = NULL;
3521556Srgrimes
3531556Srgrimes	exitstatus = 0;
3541556Srgrimes	loopnest++;
3551556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
3561556Srgrimes		setvar(n->nfor.var, sp->text, 0);
357149933Sstefanf		evaltree(n->nfor.body, flags);
3581556Srgrimes		if (evalskip) {
3591556Srgrimes			if (evalskip == SKIPCONT && --skipcount <= 0) {
3601556Srgrimes				evalskip = 0;
3611556Srgrimes				continue;
3621556Srgrimes			}
3631556Srgrimes			if (evalskip == SKIPBREAK && --skipcount <= 0)
3641556Srgrimes				evalskip = 0;
3651556Srgrimes			break;
3661556Srgrimes		}
3671556Srgrimes	}
3681556Srgrimes	loopnest--;
3691556Srgrimesout:
3701556Srgrimes	popstackmark(&smark);
3711556Srgrimes}
3721556Srgrimes
3731556Srgrimes
3741556Srgrimes
375213811Sobrienstatic void
37690111Simpevalcase(union node *n, int flags)
37717987Speter{
3781556Srgrimes	union node *cp;
3791556Srgrimes	union node *patp;
3801556Srgrimes	struct arglist arglist;
3811556Srgrimes	struct stackmark smark;
3821556Srgrimes
3831556Srgrimes	setstackmark(&smark);
3841556Srgrimes	arglist.lastp = &arglist.list;
38517987Speter	oexitstatus = exitstatus;
386172440Sstefanf	exitstatus = 0;
3871556Srgrimes	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
3881556Srgrimes	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
3891556Srgrimes		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
3901556Srgrimes			if (casematch(patp, arglist.list->text)) {
3911556Srgrimes				if (evalskip == 0) {
3921556Srgrimes					evaltree(cp->nclist.body, flags);
3931556Srgrimes				}
3941556Srgrimes				goto out;
3951556Srgrimes			}
3961556Srgrimes		}
3971556Srgrimes	}
3981556Srgrimesout:
3991556Srgrimes	popstackmark(&smark);
4001556Srgrimes}
4011556Srgrimes
4021556Srgrimes
4031556Srgrimes
4041556Srgrimes/*
4051556Srgrimes * Kick off a subshell to evaluate a tree.
4061556Srgrimes */
4071556Srgrimes
408213811Sobrienstatic void
40990111Simpevalsubshell(union node *n, int flags)
41017987Speter{
4111556Srgrimes	struct job *jp;
4121556Srgrimes	int backgnd = (n->type == NBACKGND);
4131556Srgrimes
4141556Srgrimes	expredir(n->nredir.redirect);
415194774Sjilles	if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
416194774Sjilles			forkshell(jp = makejob(n, 1), n, backgnd) == 0) {
4171556Srgrimes		if (backgnd)
4181556Srgrimes			flags &=~ EV_TESTED;
4191556Srgrimes		redirect(n->nredir.redirect, 0);
4201556Srgrimes		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
421201053Sjilles	} else if (! backgnd) {
4221556Srgrimes		INTOFF;
42345916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
4241556Srgrimes		INTON;
4251556Srgrimes	}
4261556Srgrimes}
4271556Srgrimes
4281556Srgrimes
429205154Sjilles/*
430205154Sjilles * Evaluate a redirected compound command.
431205154Sjilles */
4321556Srgrimes
433213811Sobrienstatic void
434205154Sjillesevalredir(union node *n, int flags)
435205154Sjilles{
436205154Sjilles	struct jmploc jmploc;
437205154Sjilles	struct jmploc *savehandler;
438205154Sjilles	volatile int in_redirect = 1;
439205154Sjilles
440205154Sjilles	expredir(n->nredir.redirect);
441205154Sjilles	savehandler = handler;
442205154Sjilles	if (setjmp(jmploc.loc)) {
443205154Sjilles		int e;
444205154Sjilles
445205154Sjilles		handler = savehandler;
446205154Sjilles		e = exception;
447205154Sjilles		if (e == EXERROR || e == EXEXEC) {
448205154Sjilles			popredir();
449205154Sjilles			if (in_redirect) {
450205154Sjilles				exitstatus = 2;
451205154Sjilles				return;
452205154Sjilles			}
453205154Sjilles		}
454205154Sjilles		longjmp(handler->loc, 1);
455205154Sjilles	} else {
456205154Sjilles		INTOFF;
457205154Sjilles		handler = &jmploc;
458205154Sjilles		redirect(n->nredir.redirect, REDIR_PUSH);
459205154Sjilles		in_redirect = 0;
460205154Sjilles		INTON;
461205154Sjilles		evaltree(n->nredir.n, flags);
462205154Sjilles	}
463205154Sjilles	INTOFF;
464205154Sjilles	handler = savehandler;
465205154Sjilles	popredir();
466205154Sjilles	INTON;
467205154Sjilles}
468205154Sjilles
469205154Sjilles
4701556Srgrimes/*
4711556Srgrimes * Compute the names of the files in a redirection list.
4721556Srgrimes */
4731556Srgrimes
474213811Sobrienstatic void
47590111Simpexpredir(union node *n)
47617987Speter{
47725222Ssteve	union node *redir;
4781556Srgrimes
4791556Srgrimes	for (redir = n ; redir ; redir = redir->nfile.next) {
48017987Speter		struct arglist fn;
48117987Speter		fn.lastp = &fn.list;
48217987Speter		oexitstatus = exitstatus;
48317987Speter		switch (redir->type) {
48417987Speter		case NFROM:
48517987Speter		case NTO:
48666612Sbrian		case NFROMTO:
48717987Speter		case NAPPEND:
48896922Stjr		case NCLOBBER:
4891556Srgrimes			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
4901556Srgrimes			redir->nfile.expfname = fn.list->text;
49117987Speter			break;
49217987Speter		case NFROMFD:
49317987Speter		case NTOFD:
49417987Speter			if (redir->ndup.vname) {
495181017Sstefanf				expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
49617987Speter				fixredir(redir, fn.list->text, 1);
49717987Speter			}
49817987Speter			break;
4991556Srgrimes		}
5001556Srgrimes	}
5011556Srgrimes}
5021556Srgrimes
5031556Srgrimes
5041556Srgrimes
5051556Srgrimes/*
5061556Srgrimes * Evaluate a pipeline.  All the processes in the pipeline are children
5071556Srgrimes * of the process creating the pipeline.  (This differs from some versions
5081556Srgrimes * of the shell, which make the last process in a pipeline the parent
5091556Srgrimes * of all the rest.)
5101556Srgrimes */
5111556Srgrimes
512213811Sobrienstatic void
51390111Simpevalpipe(union node *n)
51417987Speter{
5151556Srgrimes	struct job *jp;
5161556Srgrimes	struct nodelist *lp;
5171556Srgrimes	int pipelen;
5181556Srgrimes	int prevfd;
5191556Srgrimes	int pip[2];
5201556Srgrimes
521149802Sstefanf	TRACE(("evalpipe(%p) called\n", (void *)n));
5221556Srgrimes	pipelen = 0;
5231556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
5241556Srgrimes		pipelen++;
5251556Srgrimes	INTOFF;
5261556Srgrimes	jp = makejob(n, pipelen);
5271556Srgrimes	prevfd = -1;
5281556Srgrimes	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
5291556Srgrimes		prehash(lp->n);
5301556Srgrimes		pip[1] = -1;
5311556Srgrimes		if (lp->next) {
5321556Srgrimes			if (pipe(pip) < 0) {
5331556Srgrimes				close(prevfd);
53453891Scracauer				error("Pipe call failed: %s", strerror(errno));
5351556Srgrimes			}
5361556Srgrimes		}
5371556Srgrimes		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
5381556Srgrimes			INTON;
5391556Srgrimes			if (prevfd > 0) {
540124780Sdes				dup2(prevfd, 0);
5411556Srgrimes				close(prevfd);
5421556Srgrimes			}
5431556Srgrimes			if (pip[1] >= 0) {
54453282Scracauer				if (!(prevfd >= 0 && pip[0] == 0))
54552900Scracauer					close(pip[0]);
5461556Srgrimes				if (pip[1] != 1) {
547124780Sdes					dup2(pip[1], 1);
5481556Srgrimes					close(pip[1]);
5491556Srgrimes				}
5501556Srgrimes			}
5511556Srgrimes			evaltree(lp->n, EV_EXIT);
5521556Srgrimes		}
5531556Srgrimes		if (prevfd >= 0)
5541556Srgrimes			close(prevfd);
5551556Srgrimes		prevfd = pip[0];
5561556Srgrimes		close(pip[1]);
5571556Srgrimes	}
5581556Srgrimes	INTON;
5591556Srgrimes	if (n->npipe.backgnd == 0) {
5601556Srgrimes		INTOFF;
56145916Scracauer		exitstatus = waitforjob(jp, (int *)NULL);
5621556Srgrimes		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
5631556Srgrimes		INTON;
5641556Srgrimes	}
5651556Srgrimes}
5661556Srgrimes
5671556Srgrimes
5681556Srgrimes
5691556Srgrimes/*
5701556Srgrimes * Execute a command inside back quotes.  If it's a builtin command, we
5711556Srgrimes * want to save its output in a block obtained from malloc.  Otherwise
5721556Srgrimes * we fork off a subprocess and get the output of the command via a pipe.
5731556Srgrimes * Should be called with interrupts off.
5741556Srgrimes */
5751556Srgrimes
5761556Srgrimesvoid
57790111Simpevalbackcmd(union node *n, struct backcmd *result)
57817987Speter{
5791556Srgrimes	int pip[2];
5801556Srgrimes	struct job *jp;
5811556Srgrimes	struct stackmark smark;		/* unnecessary */
5821556Srgrimes
5831556Srgrimes	setstackmark(&smark);
5841556Srgrimes	result->fd = -1;
5851556Srgrimes	result->buf = NULL;
5861556Srgrimes	result->nleft = 0;
5871556Srgrimes	result->jp = NULL;
58817987Speter	if (n == NULL) {
58917987Speter		exitstatus = 0;
5901556Srgrimes		goto out;
59117987Speter	}
5921556Srgrimes	if (n->type == NCMD) {
59317987Speter		exitstatus = oexitstatus;
5941556Srgrimes		evalcommand(n, EV_BACKCMD, result);
5951556Srgrimes	} else {
59617987Speter		exitstatus = 0;
5971556Srgrimes		if (pipe(pip) < 0)
59853891Scracauer			error("Pipe call failed: %s", strerror(errno));
5991556Srgrimes		jp = makejob(n, 1);
6001556Srgrimes		if (forkshell(jp, n, FORK_NOJOB) == 0) {
6011556Srgrimes			FORCEINTON;
6021556Srgrimes			close(pip[0]);
6031556Srgrimes			if (pip[1] != 1) {
604124780Sdes				dup2(pip[1], 1);
6051556Srgrimes				close(pip[1]);
6061556Srgrimes			}
6071556Srgrimes			evaltree(n, EV_EXIT);
6081556Srgrimes		}
6091556Srgrimes		close(pip[1]);
6101556Srgrimes		result->fd = pip[0];
6111556Srgrimes		result->jp = jp;
6121556Srgrimes	}
6131556Srgrimesout:
6141556Srgrimes	popstackmark(&smark);
615109627Stjr	TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
6161556Srgrimes		result->fd, result->buf, result->nleft, result->jp));
6171556Srgrimes}
6181556Srgrimes
6191556Srgrimes
6201556Srgrimes
6211556Srgrimes/*
6221556Srgrimes * Execute a simple command.
6231556Srgrimes */
6241556Srgrimes
625213811Sobrienstatic void
62690111Simpevalcommand(union node *cmd, int flags, struct backcmd *backcmd)
62717987Speter{
6281556Srgrimes	struct stackmark smark;
6291556Srgrimes	union node *argp;
6301556Srgrimes	struct arglist arglist;
6311556Srgrimes	struct arglist varlist;
6321556Srgrimes	char **argv;
6331556Srgrimes	int argc;
6341556Srgrimes	char **envp;
6351556Srgrimes	int varflag;
6361556Srgrimes	struct strlist *sp;
6371556Srgrimes	int mode;
6381556Srgrimes	int pip[2];
6391556Srgrimes	struct cmdentry cmdentry;
6401556Srgrimes	struct job *jp;
6411556Srgrimes	struct jmploc jmploc;
642194765Sjilles	struct jmploc *savehandler;
643194765Sjilles	char *savecmdname;
644194765Sjilles	struct shparam saveparam;
645194765Sjilles	struct localvar *savelocalvars;
646199647Sjilles	struct parsefile *savetopfile;
6471556Srgrimes	volatile int e;
6481556Srgrimes	char *lastarg;
64945916Scracauer	int realstatus;
65054884Scracauer	int do_clearcmdentry;
651211287Sjilles	const char *path = pathval();
6521556Srgrimes
6531556Srgrimes	/* First expand the arguments. */
654149802Sstefanf	TRACE(("evalcommand(%p, %d) called\n", (void *)cmd, flags));
6551556Srgrimes	setstackmark(&smark);
6561556Srgrimes	arglist.lastp = &arglist.list;
6571556Srgrimes	varlist.lastp = &varlist.list;
6581556Srgrimes	varflag = 1;
65954884Scracauer	do_clearcmdentry = 0;
66017987Speter	oexitstatus = exitstatus;
66117987Speter	exitstatus = 0;
6621556Srgrimes	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
66317987Speter		char *p = argp->narg.text;
6641556Srgrimes		if (varflag && is_name(*p)) {
6651556Srgrimes			do {
6661556Srgrimes				p++;
6671556Srgrimes			} while (is_in_name(*p));
6681556Srgrimes			if (*p == '=') {
6691556Srgrimes				expandarg(argp, &varlist, EXP_VARTILDE);
6701556Srgrimes				continue;
6711556Srgrimes			}
6721556Srgrimes		}
6731556Srgrimes		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
6741556Srgrimes		varflag = 0;
6751556Srgrimes	}
6761556Srgrimes	*arglist.lastp = NULL;
6771556Srgrimes	*varlist.lastp = NULL;
6781556Srgrimes	expredir(cmd->ncmd.redirect);
6791556Srgrimes	argc = 0;
6801556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next)
6811556Srgrimes		argc++;
6821556Srgrimes	argv = stalloc(sizeof (char *) * (argc + 1));
6831556Srgrimes
6841556Srgrimes	for (sp = arglist.list ; sp ; sp = sp->next) {
6851556Srgrimes		TRACE(("evalcommand arg: %s\n", sp->text));
6861556Srgrimes		*argv++ = sp->text;
6871556Srgrimes	}
6881556Srgrimes	*argv = NULL;
6891556Srgrimes	lastarg = NULL;
6901556Srgrimes	if (iflag && funcnest == 0 && argc > 0)
6911556Srgrimes		lastarg = argv[-1];
6921556Srgrimes	argv -= argc;
6931556Srgrimes
6941556Srgrimes	/* Print the command if xflag is set. */
6951556Srgrimes	if (xflag) {
696159632Sstefanf		char sep = 0;
697194786Sjilles		const char *p;
698159632Sstefanf		out2str(ps4val());
6991556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next) {
700159632Sstefanf			if (sep != 0)
701201366Sjilles				out2c(' ');
702194786Sjilles			p = sp->text;
703194786Sjilles			while (*p != '=' && *p != '\0')
704194786Sjilles				out2c(*p++);
705194786Sjilles			if (*p != '\0') {
706194786Sjilles				out2c(*p++);
707194786Sjilles				out2qstr(p);
708194786Sjilles			}
709159632Sstefanf			sep = ' ';
7101556Srgrimes		}
7111556Srgrimes		for (sp = arglist.list ; sp ; sp = sp->next) {
712159632Sstefanf			if (sep != 0)
713201366Sjilles				out2c(' ');
714194786Sjilles			/* Disambiguate command looking like assignment. */
715194786Sjilles			if (sp == arglist.list &&
716194786Sjilles					strchr(sp->text, '=') != NULL &&
717194786Sjilles					strchr(sp->text, '\'') == NULL) {
718194786Sjilles				out2c('\'');
719194786Sjilles				out2str(sp->text);
720194786Sjilles				out2c('\'');
721194786Sjilles			} else
722194786Sjilles				out2qstr(sp->text);
723159632Sstefanf			sep = ' ';
7241556Srgrimes		}
725201366Sjilles		out2c('\n');
7261556Srgrimes		flushout(&errout);
7271556Srgrimes	}
7281556Srgrimes
7291556Srgrimes	/* Now locate the command. */
7301556Srgrimes	if (argc == 0) {
731157601Sstefanf		/* Variable assignment(s) without command */
7321556Srgrimes		cmdentry.cmdtype = CMDBUILTIN;
7331556Srgrimes		cmdentry.u.index = BLTINCMD;
734205138Sjilles		cmdentry.special = 0;
7351556Srgrimes	} else {
73617987Speter		static const char PATH[] = "PATH=";
737204800Sjilles		int cmd_flags = 0, bltinonly = 0;
73817987Speter
73917987Speter		/*
74017987Speter		 * Modify the command lookup path, if a PATH= assignment
74117987Speter		 * is present
74217987Speter		 */
74317987Speter		for (sp = varlist.list ; sp ; sp = sp->next)
74454884Scracauer			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
74517987Speter				path = sp->text + sizeof(PATH) - 1;
746155301Sschweikh				/*
74754884Scracauer				 * On `PATH=... command`, we need to make
74854884Scracauer				 * sure that the command isn't using the
74954884Scracauer				 * non-updated hash table of the outer PATH
750155301Sschweikh				 * setting and we need to make sure that
75154884Scracauer				 * the hash table isn't filled with items
75254884Scracauer				 * from the temporary setting.
75354884Scracauer				 *
754155301Sschweikh				 * It would be better to forbit using and
75554884Scracauer				 * updating the table while this command
75654884Scracauer				 * runs, by the command finding mechanism
75754884Scracauer				 * is heavily integrated with hash handling,
75854884Scracauer				 * so we just delete the hash before and after
75954884Scracauer				 * the command runs. Partly deleting like
76054884Scracauer				 * changepatch() does doesn't seem worth the
76154884Scracauer				 * bookinging effort, since most such runs add
762123996Smaxim				 * directories in front of the new PATH.
76354884Scracauer				 */
76454884Scracauer				clearcmdentry(0);
76554884Scracauer				do_clearcmdentry = 1;
76654884Scracauer			}
76717987Speter
768204800Sjilles		for (;;) {
769204800Sjilles			if (bltinonly) {
770204800Sjilles				cmdentry.u.index = find_builtin(*argv, &cmdentry.special);
771204800Sjilles				if (cmdentry.u.index < 0) {
772201431Sjilles					cmdentry.u.index = BLTINCMD;
773201431Sjilles					argv--;
774201431Sjilles					argc++;
775201431Sjilles					break;
7761556Srgrimes				}
777204800Sjilles			} else
778204800Sjilles				find_command(argv[0], &cmdentry, cmd_flags, path);
779204800Sjilles			/* implement the bltin and command builtins here */
780204800Sjilles			if (cmdentry.cmdtype != CMDBUILTIN)
781204800Sjilles				break;
782204800Sjilles			if (cmdentry.u.index == BLTINCMD) {
783204800Sjilles				if (argc == 1)
7841556Srgrimes					break;
785204800Sjilles				argv++;
786204800Sjilles				argc--;
787204800Sjilles				bltinonly = 1;
788204800Sjilles			} else if (cmdentry.u.index == COMMANDCMD) {
789204800Sjilles				if (argc == 1)
790204800Sjilles					break;
791204800Sjilles				if (!strcmp(argv[1], "-p")) {
792204800Sjilles					if (argc == 2)
793204800Sjilles						break;
794204800Sjilles					if (argv[2][0] == '-') {
795204800Sjilles						if (strcmp(argv[2], "--"))
796204800Sjilles							break;
797204800Sjilles						if (argc == 3)
798204800Sjilles							break;
799204800Sjilles						argv += 3;
800204800Sjilles						argc -= 3;
801204800Sjilles					} else {
802204800Sjilles						argv += 2;
803204800Sjilles						argc -= 2;
804204800Sjilles					}
805204800Sjilles					path = _PATH_STDPATH;
806204800Sjilles					clearcmdentry(0);
807204800Sjilles					do_clearcmdentry = 1;
808204800Sjilles				} else if (!strcmp(argv[1], "--")) {
809204800Sjilles					if (argc == 2)
810204800Sjilles						break;
811204800Sjilles					argv += 2;
812204800Sjilles					argc -= 2;
813204800Sjilles				} else if (argv[1][0] == '-')
814204800Sjilles					break;
815204800Sjilles				else {
816204800Sjilles					argv++;
817204800Sjilles					argc--;
818204800Sjilles				}
819204800Sjilles				cmd_flags |= DO_NOFUNC;
820204800Sjilles				bltinonly = 0;
821204800Sjilles			} else
822204800Sjilles				break;
8231556Srgrimes		}
824204800Sjilles		/*
825204800Sjilles		 * Special builtins lose their special properties when
826204800Sjilles		 * called via 'command'.
827204800Sjilles		 */
828204800Sjilles		if (cmd_flags & DO_NOFUNC)
829204800Sjilles			cmdentry.special = 0;
8301556Srgrimes	}
8311556Srgrimes
8321556Srgrimes	/* Fork off a child process if necessary. */
8331556Srgrimes	if (cmd->ncmd.backgnd
834197820Sjilles	 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
835194127Sjilles	    && ((flags & EV_EXIT) == 0 || have_traps()))
83617987Speter	 || ((flags & EV_BACKCMD) != 0
8371556Srgrimes	    && (cmdentry.cmdtype != CMDBUILTIN
83848896Ssheldonh		 || cmdentry.u.index == CDCMD
8391556Srgrimes		 || cmdentry.u.index == DOTCMD
840204800Sjilles		 || cmdentry.u.index == EVALCMD))) {
8411556Srgrimes		jp = makejob(cmd, 1);
8421556Srgrimes		mode = cmd->ncmd.backgnd;
8431556Srgrimes		if (flags & EV_BACKCMD) {
8441556Srgrimes			mode = FORK_NOJOB;
8451556Srgrimes			if (pipe(pip) < 0)
84653891Scracauer				error("Pipe call failed: %s", strerror(errno));
8471556Srgrimes		}
8481556Srgrimes		if (forkshell(jp, cmd, mode) != 0)
8491556Srgrimes			goto parent;	/* at end of routine */
8501556Srgrimes		if (flags & EV_BACKCMD) {
8511556Srgrimes			FORCEINTON;
8521556Srgrimes			close(pip[0]);
8531556Srgrimes			if (pip[1] != 1) {
854124780Sdes				dup2(pip[1], 1);
8551556Srgrimes				close(pip[1]);
8561556Srgrimes			}
8571556Srgrimes		}
8581556Srgrimes		flags |= EV_EXIT;
8591556Srgrimes	}
8601556Srgrimes
8611556Srgrimes	/* This is the child process if a fork occurred. */
8621556Srgrimes	/* Execute the command. */
8631556Srgrimes	if (cmdentry.cmdtype == CMDFUNCTION) {
86420425Ssteve#ifdef DEBUG
8651556Srgrimes		trputs("Shell function:  ");  trargs(argv);
86620425Ssteve#endif
8671556Srgrimes		saveparam = shellparam;
8681556Srgrimes		shellparam.malloc = 0;
86920425Ssteve		shellparam.reset = 1;
8701556Srgrimes		shellparam.nparam = argc - 1;
8711556Srgrimes		shellparam.p = argv + 1;
8721556Srgrimes		shellparam.optnext = NULL;
8731556Srgrimes		INTOFF;
8741556Srgrimes		savelocalvars = localvars;
8751556Srgrimes		localvars = NULL;
876196483Sjilles		reffunc(cmdentry.u.func);
877194765Sjilles		savehandler = handler;
8781556Srgrimes		if (setjmp(jmploc.loc)) {
8791556Srgrimes			if (exception == EXSHELLPROC)
880194765Sjilles				freeparam(&saveparam);
8811556Srgrimes			else {
8821556Srgrimes				freeparam(&shellparam);
8831556Srgrimes				shellparam = saveparam;
884204802Sjilles				if (exception == EXERROR || exception == EXEXEC)
885204802Sjilles					popredir();
8861556Srgrimes			}
887196483Sjilles			unreffunc(cmdentry.u.func);
8881556Srgrimes			poplocalvars();
8891556Srgrimes			localvars = savelocalvars;
890201283Sjilles			funcnest--;
8911556Srgrimes			handler = savehandler;
8921556Srgrimes			longjmp(handler->loc, 1);
8931556Srgrimes		}
8941556Srgrimes		handler = &jmploc;
895201283Sjilles		funcnest++;
896204802Sjilles		redirect(cmd->ncmd.redirect, REDIR_PUSH);
897199660Sjilles		INTON;
8981556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
8991556Srgrimes			mklocal(sp->text);
900185231Sstefanf		exitstatus = oexitstatus;
90135675Scracauer		if (flags & EV_TESTED)
902196634Sjilles			evaltree(getfuncnode(cmdentry.u.func), EV_TESTED);
90335675Scracauer		else
904196634Sjilles			evaltree(getfuncnode(cmdentry.u.func), 0);
9051556Srgrimes		INTOFF;
906196483Sjilles		unreffunc(cmdentry.u.func);
9071556Srgrimes		poplocalvars();
9081556Srgrimes		localvars = savelocalvars;
9091556Srgrimes		freeparam(&shellparam);
9101556Srgrimes		shellparam = saveparam;
9111556Srgrimes		handler = savehandler;
912201283Sjilles		funcnest--;
9131556Srgrimes		popredir();
9141556Srgrimes		INTON;
9151556Srgrimes		if (evalskip == SKIPFUNC) {
9161556Srgrimes			evalskip = 0;
9171556Srgrimes			skipcount = 0;
9181556Srgrimes		}
9191556Srgrimes		if (flags & EV_EXIT)
9201556Srgrimes			exitshell(exitstatus);
9211556Srgrimes	} else if (cmdentry.cmdtype == CMDBUILTIN) {
92220425Ssteve#ifdef DEBUG
9231556Srgrimes		trputs("builtin command:  ");  trargs(argv);
92420425Ssteve#endif
9251556Srgrimes		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
9261556Srgrimes		if (flags == EV_BACKCMD) {
9271556Srgrimes			memout.nleft = 0;
9281556Srgrimes			memout.nextc = memout.buf;
9291556Srgrimes			memout.bufsize = 64;
9301556Srgrimes			mode |= REDIR_BACKQ;
931201366Sjilles			cmdentry.special = 0;
9321556Srgrimes		}
9331556Srgrimes		savecmdname = commandname;
934199647Sjilles		savetopfile = getcurrentfile();
9351556Srgrimes		cmdenviron = varlist.list;
9361556Srgrimes		e = -1;
937194765Sjilles		savehandler = handler;
9381556Srgrimes		if (setjmp(jmploc.loc)) {
9391556Srgrimes			e = exception;
9401556Srgrimes			exitstatus = (e == EXINT)? SIGINT+128 : 2;
9411556Srgrimes			goto cmddone;
9421556Srgrimes		}
9431556Srgrimes		handler = &jmploc;
944157601Sstefanf		redirect(cmd->ncmd.redirect, mode);
945205138Sjilles		/*
946205138Sjilles		 * If there is no command word, redirection errors should
947205138Sjilles		 * not be fatal but assignment errors should.
948205138Sjilles		 */
949205138Sjilles		if (argc == 0 && !(flags & EV_BACKCMD))
950205138Sjilles			cmdentry.special = 1;
951157601Sstefanf		if (cmdentry.special)
952157601Sstefanf			listsetvar(cmdenviron);
953207678Sjilles		if (argc > 0)
954207678Sjilles			bltinsetlocale();
9551556Srgrimes		commandname = argv[0];
9561556Srgrimes		argptr = argv + 1;
957201053Sjilles		nextopt_optptr = NULL;		/* initialize nextopt */
958193169Sstefanf		builtin_flags = flags;
9591556Srgrimes		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
9601556Srgrimes		flushall();
9611556Srgrimescmddone:
962207678Sjilles		if (argc > 0)
963207678Sjilles			bltinunsetlocale();
96460592Scracauer		cmdenviron = NULL;
9651556Srgrimes		out1 = &output;
9661556Srgrimes		out2 = &errout;
9671556Srgrimes		freestdout();
9681556Srgrimes		if (e != EXSHELLPROC) {
9691556Srgrimes			commandname = savecmdname;
9701556Srgrimes			if (flags & EV_EXIT) {
9711556Srgrimes				exitshell(exitstatus);
9721556Srgrimes			}
9731556Srgrimes		}
9741556Srgrimes		handler = savehandler;
975201366Sjilles		if (flags == EV_BACKCMD) {
976201366Sjilles			backcmd->buf = memout.buf;
977201366Sjilles			backcmd->nleft = memout.nextc - memout.buf;
978201366Sjilles			memout.buf = NULL;
979201366Sjilles		}
980204801Sjilles		if (cmdentry.u.index != EXECCMD &&
981204801Sjilles				(e == -1 || e == EXERROR || e == EXEXEC))
982204801Sjilles			popredir();
9831556Srgrimes		if (e != -1) {
98420425Ssteve			if ((e != EXERROR && e != EXEXEC)
985157601Sstefanf			    || cmdentry.special)
9861556Srgrimes				exraise(e);
987199647Sjilles			popfilesupto(savetopfile);
988201366Sjilles			if (flags != EV_BACKCMD)
989201366Sjilles				FORCEINTON;
9901556Srgrimes		}
9911556Srgrimes	} else {
99220425Ssteve#ifdef DEBUG
9931556Srgrimes		trputs("normal command:  ");  trargs(argv);
99420425Ssteve#endif
9951556Srgrimes		redirect(cmd->ncmd.redirect, 0);
9961556Srgrimes		for (sp = varlist.list ; sp ; sp = sp->next)
9971556Srgrimes			setvareq(sp->text, VEXPORT|VSTACK);
9981556Srgrimes		envp = environment();
999204800Sjilles		shellexec(argv, envp, path, cmdentry.u.index);
10001556Srgrimes		/*NOTREACHED*/
10011556Srgrimes	}
10021556Srgrimes	goto out;
10031556Srgrimes
10041556Srgrimesparent:	/* parent process gets here (if we forked) */
1005212214Sjilles	if (mode == FORK_FG) {	/* argument to fork */
10061556Srgrimes		INTOFF;
100745916Scracauer		exitstatus = waitforjob(jp, &realstatus);
10081556Srgrimes		INTON;
100945916Scracauer		if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
101045266Scracauer			evalskip = SKIPBREAK;
101145266Scracauer			skipcount = loopnest;
101245266Scracauer		}
1013212214Sjilles	} else if (mode == FORK_NOJOB) {
10141556Srgrimes		backcmd->fd = pip[0];
10151556Srgrimes		close(pip[1]);
10161556Srgrimes		backcmd->jp = jp;
10171556Srgrimes	}
10181556Srgrimes
10191556Srgrimesout:
10201556Srgrimes	if (lastarg)
10211556Srgrimes		setvar("_", lastarg, 0);
102254884Scracauer	if (do_clearcmdentry)
102354884Scracauer		clearcmdentry(0);
10241556Srgrimes	popstackmark(&smark);
10251556Srgrimes}
10261556Srgrimes
10271556Srgrimes
10281556Srgrimes
10291556Srgrimes/*
10301556Srgrimes * Search for a command.  This is called before we fork so that the
10311556Srgrimes * location of the command will be available in the parent as well as
10321556Srgrimes * the child.  The check for "goodname" is an overly conservative
10331556Srgrimes * check that the name will not be subject to expansion.
10341556Srgrimes */
10351556Srgrimes
1036213811Sobrienstatic void
103790111Simpprehash(union node *n)
103817987Speter{
10391556Srgrimes	struct cmdentry entry;
10401556Srgrimes
1041159633Sstefanf	if (n && n->type == NCMD && n->ncmd.args)
104217987Speter		if (goodname(n->ncmd.args->narg.text))
104317987Speter			find_command(n->ncmd.args->narg.text, &entry, 0,
104417987Speter				     pathval());
10451556Srgrimes}
10461556Srgrimes
10471556Srgrimes
10481556Srgrimes
10491556Srgrimes/*
10501556Srgrimes * Builtin commands.  Builtin commands whose functions are closely
10511556Srgrimes * tied to evaluation are implemented here.
10521556Srgrimes */
10531556Srgrimes
10541556Srgrimes/*
1055201431Sjilles * No command given, a bltin command with no arguments, or a bltin command
1056201431Sjilles * with an invalid name.
10571556Srgrimes */
10581556Srgrimes
105917987Speterint
1060201431Sjillesbltincmd(int argc, char **argv)
106117987Speter{
1062201431Sjilles	if (argc > 1) {
1063201431Sjilles		out2fmt_flush("%s: not found\n", argv[1]);
1064201431Sjilles		return 127;
1065201431Sjilles	}
106620425Ssteve	/*
106717987Speter	 * Preserve exitstatus of a previous possible redirection
106820425Ssteve	 * as POSIX mandates
106917987Speter	 */
10701556Srgrimes	return exitstatus;
10711556Srgrimes}
10721556Srgrimes
10731556Srgrimes
10741556Srgrimes/*
10751556Srgrimes * Handle break and continue commands.  Break, continue, and return are
10761556Srgrimes * all handled by setting the evalskip flag.  The evaluation routines
10771556Srgrimes * above all check this flag, and if it is set they start skipping
10781556Srgrimes * commands rather than executing them.  The variable skipcount is
10791556Srgrimes * the number of loops to break/continue, or the number of function
10801556Srgrimes * levels to return.  (The latter is always 1.)  It should probably
10811556Srgrimes * be an error to break out of more loops than exist, but it isn't
10821556Srgrimes * in the standard shell so we don't make it one here.
10831556Srgrimes */
10841556Srgrimes
108517987Speterint
108690111Simpbreakcmd(int argc, char **argv)
108717987Speter{
108820425Ssteve	int n = argc > 1 ? number(argv[1]) : 1;
10891556Srgrimes
10901556Srgrimes	if (n > loopnest)
10911556Srgrimes		n = loopnest;
10921556Srgrimes	if (n > 0) {
10931556Srgrimes		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
10941556Srgrimes		skipcount = n;
10951556Srgrimes	}
10961556Srgrimes	return 0;
10971556Srgrimes}
10981556Srgrimes
1099100437Stjr/*
1100100437Stjr * The `command' command.
1101100437Stjr */
1102100437Stjrint
1103100437Stjrcommandcmd(int argc, char **argv)
1104100437Stjr{
1105207783Sjilles	const char *path;
1106100437Stjr	int ch;
1107151810Sstefanf	int cmd = -1;
11081556Srgrimes
1109204800Sjilles	path = bltinlookup("PATH", 1);
1110100437Stjr
1111100437Stjr	optind = optreset = 1;
1112100663Stjr	opterr = 0;
1113151810Sstefanf	while ((ch = getopt(argc, argv, "pvV")) != -1) {
1114100437Stjr		switch (ch) {
1115100437Stjr		case 'p':
1116207783Sjilles			path = _PATH_STDPATH;
1117100437Stjr			break;
1118151810Sstefanf		case 'v':
1119151810Sstefanf			cmd = TYPECMD_SMALLV;
1120151810Sstefanf			break;
1121151810Sstefanf		case 'V':
1122151810Sstefanf			cmd = TYPECMD_BIGV;
1123151810Sstefanf			break;
1124100437Stjr		case '?':
1125100437Stjr		default:
1126100437Stjr			error("unknown option: -%c", optopt);
1127100437Stjr		}
1128100437Stjr	}
1129100437Stjr	argc -= optind;
1130100437Stjr	argv += optind;
1131100437Stjr
1132151810Sstefanf	if (cmd != -1) {
1133151810Sstefanf		if (argc != 1)
1134151810Sstefanf			error("wrong number of arguments");
1135201343Sjilles		return typecmd_impl(2, argv - 1, cmd, path);
1136151810Sstefanf	}
1137204800Sjilles	if (argc != 0)
1138214538Sjilles		error("commandcmd bad call");
1139100437Stjr
1140100437Stjr	/*
1141100437Stjr	 * Do nothing successfully if no command was specified;
1142100437Stjr	 * ksh also does this.
1143100437Stjr	 */
1144204800Sjilles	return 0;
1145100437Stjr}
1146100437Stjr
1147100437Stjr
11481556Srgrimes/*
11491556Srgrimes * The return command.
11501556Srgrimes */
11511556Srgrimes
115217987Speterint
115390111Simpreturncmd(int argc, char **argv)
115417987Speter{
115520425Ssteve	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
11561556Srgrimes
11571556Srgrimes	if (funcnest) {
11581556Srgrimes		evalskip = SKIPFUNC;
11591556Srgrimes		skipcount = 1;
116020425Ssteve	} else {
116120425Ssteve		/* skip the rest of the file */
116220425Ssteve		evalskip = SKIPFILE;
116320425Ssteve		skipcount = 1;
11641556Srgrimes	}
11651556Srgrimes	return ret;
11661556Srgrimes}
11671556Srgrimes
11681556Srgrimes
116917987Speterint
117090111Simpfalsecmd(int argc __unused, char **argv __unused)
117117987Speter{
117217987Speter	return 1;
117317987Speter}
117417987Speter
117517987Speter
117617987Speterint
117790111Simptruecmd(int argc __unused, char **argv __unused)
117817987Speter{
11791556Srgrimes	return 0;
11801556Srgrimes}
11811556Srgrimes
11821556Srgrimes
118317987Speterint
118490111Simpexeccmd(int argc, char **argv)
118517987Speter{
1186208630Sjilles	/*
1187208630Sjilles	 * Because we have historically not supported any options,
1188208630Sjilles	 * only treat "--" specially.
1189208630Sjilles	 */
1190208630Sjilles	if (argc > 1 && strcmp(argv[1], "--") == 0)
1191208630Sjilles		argc--, argv++;
11921556Srgrimes	if (argc > 1) {
119317987Speter		struct strlist *sp;
119417987Speter
11951556Srgrimes		iflag = 0;		/* exit on error */
11961556Srgrimes		mflag = 0;
11971556Srgrimes		optschanged();
119817987Speter		for (sp = cmdenviron; sp ; sp = sp->next)
119917987Speter			setvareq(sp->text, VEXPORT|VSTACK);
12001556Srgrimes		shellexec(argv + 1, environment(), pathval(), 0);
12011556Srgrimes
12021556Srgrimes	}
12031556Srgrimes	return 0;
12041556Srgrimes}
1205153091Sstefanf
1206153091Sstefanf
1207153091Sstefanfint
1208153091Sstefanftimescmd(int argc __unused, char **argv __unused)
1209153091Sstefanf{
1210153091Sstefanf	struct rusage ru;
1211153091Sstefanf	long shumins, shsmins, chumins, chsmins;
1212153091Sstefanf	double shusecs, shssecs, chusecs, chssecs;
1213153091Sstefanf
1214153091Sstefanf	if (getrusage(RUSAGE_SELF, &ru) < 0)
1215153091Sstefanf		return 1;
1216153091Sstefanf	shumins = ru.ru_utime.tv_sec / 60;
1217153091Sstefanf	shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1218153091Sstefanf	shsmins = ru.ru_stime.tv_sec / 60;
1219153091Sstefanf	shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1220153091Sstefanf	if (getrusage(RUSAGE_CHILDREN, &ru) < 0)
1221153091Sstefanf		return 1;
1222153091Sstefanf	chumins = ru.ru_utime.tv_sec / 60;
1223153091Sstefanf	chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.;
1224153091Sstefanf	chsmins = ru.ru_stime.tv_sec / 60;
1225153091Sstefanf	chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.;
1226153091Sstefanf	out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins,
1227153091Sstefanf	    shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs);
1228153091Sstefanf	return 0;
1229153091Sstefanf}
1230