eval.c revision 21673
1/*-
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	$FreeBSD: head/bin/sh/eval.c 21673 1997-01-14 07:20:47Z jkh $
37 */
38
39#ifndef lint
40static char const sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
41#endif /* not lint */
42
43#include <signal.h>
44#include <unistd.h>
45
46/*
47 * Evaluate a command.
48 */
49
50#include "shell.h"
51#include "nodes.h"
52#include "syntax.h"
53#include "expand.h"
54#include "parser.h"
55#include "jobs.h"
56#include "eval.h"
57#include "builtins.h"
58#include "options.h"
59#include "exec.h"
60#include "redir.h"
61#include "input.h"
62#include "output.h"
63#include "trap.h"
64#include "var.h"
65#include "memalloc.h"
66#include "error.h"
67#include "show.h"
68#include "mystring.h"
69#ifndef NO_HISTORY
70#include "myhistedit.h"
71#endif
72
73
74/* flags in argument to evaltree */
75#define EV_EXIT 01		/* exit after evaluating tree */
76#define EV_TESTED 02		/* exit status is checked; ignore -e flag */
77#define EV_BACKCMD 04		/* command executing within back quotes */
78
79MKINIT int evalskip;		/* set if we are skipping commands */
80STATIC int skipcount;		/* number of levels to skip */
81MKINIT int loopnest;		/* current loop nesting level */
82int funcnest;			/* depth of function calls */
83
84
85char *commandname;
86struct strlist *cmdenviron;
87int exitstatus;			/* exit status of last command */
88int oexitstatus;		/* saved exit status */
89
90
91STATIC void evalloop __P((union node *));
92STATIC void evalfor __P((union node *));
93STATIC void evalcase __P((union node *, int));
94STATIC void evalsubshell __P((union node *, int));
95STATIC void expredir __P((union node *));
96STATIC void evalpipe __P((union node *));
97STATIC void evalcommand __P((union node *, int, struct backcmd *));
98STATIC void prehash __P((union node *));
99
100
101/*
102 * Called to reset things after an exception.
103 */
104
105#ifdef mkinit
106INCLUDE "eval.h"
107
108RESET {
109	evalskip = 0;
110	loopnest = 0;
111	funcnest = 0;
112}
113
114SHELLPROC {
115	exitstatus = 0;
116}
117#endif
118
119
120
121/*
122 * The eval commmand.
123 */
124
125int
126evalcmd(argc, argv)
127	int argc;
128	char **argv;
129{
130        char *p;
131        char *concat;
132        char **ap;
133
134        if (argc > 1) {
135                p = argv[1];
136                if (argc > 2) {
137                        STARTSTACKSTR(concat);
138                        ap = argv + 2;
139                        for (;;) {
140                                while (*p)
141                                        STPUTC(*p++, concat);
142                                if ((p = *ap++) == NULL)
143                                        break;
144                                STPUTC(' ', concat);
145                        }
146                        STPUTC('\0', concat);
147                        p = grabstackstr(concat);
148                }
149                evalstring(p);
150        }
151        return exitstatus;
152}
153
154
155/*
156 * Execute a command or commands contained in a string.
157 */
158
159void
160evalstring(s)
161	char *s;
162	{
163	union node *n;
164	struct stackmark smark;
165
166	setstackmark(&smark);
167	setinputstring(s, 1);
168	while ((n = parsecmd(0)) != NEOF) {
169		evaltree(n, 0);
170		popstackmark(&smark);
171	}
172	popfile();
173	popstackmark(&smark);
174}
175
176
177
178/*
179 * Evaluate a parse tree.  The value is left in the global variable
180 * exitstatus.
181 */
182
183void
184evaltree(n, flags)
185	union node *n;
186	int flags;
187{
188	if (n == NULL) {
189		TRACE(("evaltree(NULL) called\n"));
190		exitstatus = 0;
191		goto out;
192	}
193#ifndef NO_HISTORY
194	displayhist = 1;	/* show history substitutions done with fc */
195#endif
196	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
197	switch (n->type) {
198	case NSEMI:
199		evaltree(n->nbinary.ch1, 0);
200		if (evalskip)
201			goto out;
202		evaltree(n->nbinary.ch2, flags);
203		break;
204	case NAND:
205		evaltree(n->nbinary.ch1, EV_TESTED);
206		if (evalskip || exitstatus != 0) {
207			flags |= EV_TESTED;
208			goto out;
209		}
210		evaltree(n->nbinary.ch2, flags);
211		break;
212	case NOR:
213		evaltree(n->nbinary.ch1, EV_TESTED);
214		if (evalskip || exitstatus == 0)
215			goto out;
216		evaltree(n->nbinary.ch2, flags);
217		break;
218	case NREDIR:
219		expredir(n->nredir.redirect);
220		redirect(n->nredir.redirect, REDIR_PUSH);
221		evaltree(n->nredir.n, flags);
222		popredir();
223		break;
224	case NSUBSHELL:
225		evalsubshell(n, flags);
226		break;
227	case NBACKGND:
228		evalsubshell(n, flags);
229		break;
230	case NIF: {
231		evaltree(n->nif.test, EV_TESTED);
232		if (evalskip)
233			goto out;
234		if (exitstatus == 0)
235			evaltree(n->nif.ifpart, flags);
236		else if (n->nif.elsepart)
237			evaltree(n->nif.elsepart, flags);
238		else
239			exitstatus = 0;
240		break;
241	}
242	case NWHILE:
243	case NUNTIL:
244		evalloop(n);
245		break;
246	case NFOR:
247		evalfor(n);
248		break;
249	case NCASE:
250		evalcase(n, flags);
251		break;
252	case NDEFUN:
253		defun(n->narg.text, n->narg.next);
254		exitstatus = 0;
255		break;
256	case NNOT:
257		evaltree(n->nnot.com, EV_TESTED);
258		exitstatus = !exitstatus;
259		break;
260
261	case NPIPE:
262		evalpipe(n);
263		break;
264	case NCMD:
265		evalcommand(n, flags, (struct backcmd *)NULL);
266		break;
267	default:
268		out1fmt("Node type = %d\n", n->type);
269		flushout(&output);
270		break;
271	}
272out:
273	if (pendingsigs)
274		dotrap();
275	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
276		exitshell(exitstatus);
277}
278
279
280STATIC void
281evalloop(n)
282	union node *n;
283{
284	int status;
285
286	loopnest++;
287	status = 0;
288	for (;;) {
289		evaltree(n->nbinary.ch1, EV_TESTED);
290		if (evalskip) {
291skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
292				evalskip = 0;
293				continue;
294			}
295			if (evalskip == SKIPBREAK && --skipcount <= 0)
296				evalskip = 0;
297			break;
298		}
299		if (n->type == NWHILE) {
300			if (exitstatus != 0)
301				break;
302		} else {
303			if (exitstatus == 0)
304				break;
305		}
306		evaltree(n->nbinary.ch2, 0);
307		status = exitstatus;
308		if (evalskip)
309			goto skipping;
310	}
311	loopnest--;
312	exitstatus = status;
313}
314
315
316
317STATIC void
318evalfor(n)
319    union node *n;
320{
321	struct arglist arglist;
322	union node *argp;
323	struct strlist *sp;
324	struct stackmark smark;
325
326	setstackmark(&smark);
327	arglist.lastp = &arglist.list;
328	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
329		oexitstatus = exitstatus;
330		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
331		if (evalskip)
332			goto out;
333	}
334	*arglist.lastp = NULL;
335
336	exitstatus = 0;
337	loopnest++;
338	for (sp = arglist.list ; sp ; sp = sp->next) {
339		setvar(n->nfor.var, sp->text, 0);
340		evaltree(n->nfor.body, 0);
341		if (evalskip) {
342			if (evalskip == SKIPCONT && --skipcount <= 0) {
343				evalskip = 0;
344				continue;
345			}
346			if (evalskip == SKIPBREAK && --skipcount <= 0)
347				evalskip = 0;
348			break;
349		}
350	}
351	loopnest--;
352out:
353	popstackmark(&smark);
354}
355
356
357
358STATIC void
359evalcase(n, flags)
360	union node *n;
361	int flags;
362{
363	union node *cp;
364	union node *patp;
365	struct arglist arglist;
366	struct stackmark smark;
367
368	setstackmark(&smark);
369	arglist.lastp = &arglist.list;
370	oexitstatus = exitstatus;
371	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
372	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
373		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
374			if (casematch(patp, arglist.list->text)) {
375				if (evalskip == 0) {
376					evaltree(cp->nclist.body, flags);
377				}
378				goto out;
379			}
380		}
381	}
382out:
383	popstackmark(&smark);
384}
385
386
387
388/*
389 * Kick off a subshell to evaluate a tree.
390 */
391
392STATIC void
393evalsubshell(n, flags)
394	union node *n;
395	int flags;
396{
397	struct job *jp;
398	int backgnd = (n->type == NBACKGND);
399
400	expredir(n->nredir.redirect);
401	jp = makejob(n, 1);
402	if (forkshell(jp, n, backgnd) == 0) {
403		if (backgnd)
404			flags &=~ EV_TESTED;
405		redirect(n->nredir.redirect, 0);
406		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
407	}
408	if (! backgnd) {
409		INTOFF;
410		exitstatus = waitforjob(jp);
411		INTON;
412	}
413}
414
415
416
417/*
418 * Compute the names of the files in a redirection list.
419 */
420
421STATIC void
422expredir(n)
423	union node *n;
424{
425	register union node *redir;
426
427	for (redir = n ; redir ; redir = redir->nfile.next) {
428		struct arglist fn;
429		fn.lastp = &fn.list;
430		oexitstatus = exitstatus;
431		switch (redir->type) {
432		case NFROM:
433		case NTO:
434		case NAPPEND:
435			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
436			redir->nfile.expfname = fn.list->text;
437			break;
438		case NFROMFD:
439		case NTOFD:
440			if (redir->ndup.vname) {
441				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
442				fixredir(redir, fn.list->text, 1);
443			}
444			break;
445		}
446	}
447}
448
449
450
451/*
452 * Evaluate a pipeline.  All the processes in the pipeline are children
453 * of the process creating the pipeline.  (This differs from some versions
454 * of the shell, which make the last process in a pipeline the parent
455 * of all the rest.)
456 */
457
458STATIC void
459evalpipe(n)
460	union node *n;
461{
462	struct job *jp;
463	struct nodelist *lp;
464	int pipelen;
465	int prevfd;
466	int pip[2];
467
468	TRACE(("evalpipe(0x%lx) called\n", (long)n));
469	pipelen = 0;
470	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
471		pipelen++;
472	INTOFF;
473	jp = makejob(n, pipelen);
474	prevfd = -1;
475	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
476		prehash(lp->n);
477		pip[1] = -1;
478		if (lp->next) {
479			if (pipe(pip) < 0) {
480				close(prevfd);
481				error("Pipe call failed");
482			}
483		}
484		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
485			INTON;
486			if (prevfd > 0) {
487				close(0);
488				copyfd(prevfd, 0);
489				close(prevfd);
490			}
491			if (pip[1] >= 0) {
492				close(pip[0]);
493				if (pip[1] != 1) {
494					close(1);
495					copyfd(pip[1], 1);
496					close(pip[1]);
497				}
498			}
499			evaltree(lp->n, EV_EXIT);
500		}
501		if (prevfd >= 0)
502			close(prevfd);
503		prevfd = pip[0];
504		close(pip[1]);
505	}
506	INTON;
507	if (n->npipe.backgnd == 0) {
508		INTOFF;
509		exitstatus = waitforjob(jp);
510		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
511		INTON;
512	}
513}
514
515
516
517/*
518 * Execute a command inside back quotes.  If it's a builtin command, we
519 * want to save its output in a block obtained from malloc.  Otherwise
520 * we fork off a subprocess and get the output of the command via a pipe.
521 * Should be called with interrupts off.
522 */
523
524void
525evalbackcmd(n, result)
526	union node *n;
527	struct backcmd *result;
528{
529	int pip[2];
530	struct job *jp;
531	struct stackmark smark;		/* unnecessary */
532
533	setstackmark(&smark);
534	result->fd = -1;
535	result->buf = NULL;
536	result->nleft = 0;
537	result->jp = NULL;
538	if (n == NULL) {
539		exitstatus = 0;
540		goto out;
541	}
542	if (n->type == NCMD) {
543		exitstatus = oexitstatus;
544		evalcommand(n, EV_BACKCMD, result);
545	} else {
546		exitstatus = 0;
547		if (pipe(pip) < 0)
548			error("Pipe call failed");
549		jp = makejob(n, 1);
550		if (forkshell(jp, n, FORK_NOJOB) == 0) {
551			FORCEINTON;
552			close(pip[0]);
553			if (pip[1] != 1) {
554				close(1);
555				copyfd(pip[1], 1);
556				close(pip[1]);
557			}
558			evaltree(n, EV_EXIT);
559		}
560		close(pip[1]);
561		result->fd = pip[0];
562		result->jp = jp;
563	}
564out:
565	popstackmark(&smark);
566	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
567		result->fd, result->buf, result->nleft, result->jp));
568}
569
570
571
572/*
573 * Execute a simple command.
574 */
575
576STATIC void
577evalcommand(cmd, flags, backcmd)
578	union node *cmd;
579	int flags;
580	struct backcmd *backcmd;
581{
582	struct stackmark smark;
583	union node *argp;
584	struct arglist arglist;
585	struct arglist varlist;
586	char **argv;
587	int argc;
588	char **envp;
589	int varflag;
590	struct strlist *sp;
591	int mode;
592	int pip[2];
593	struct cmdentry cmdentry;
594	struct job *jp;
595	struct jmploc jmploc;
596	struct jmploc *volatile savehandler;
597	char *volatile savecmdname;
598	volatile struct shparam saveparam;
599	struct localvar *volatile savelocalvars;
600	volatile int e;
601	char *lastarg;
602#if __GNUC__
603	/* Avoid longjmp clobbering */
604	(void) &argv;
605	(void) &argc;
606	(void) &lastarg;
607	(void) &flags;
608#endif
609
610	/* First expand the arguments. */
611	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
612	setstackmark(&smark);
613	arglist.lastp = &arglist.list;
614	varlist.lastp = &varlist.list;
615	varflag = 1;
616	oexitstatus = exitstatus;
617	exitstatus = 0;
618	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
619		char *p = argp->narg.text;
620		if (varflag && is_name(*p)) {
621			do {
622				p++;
623			} while (is_in_name(*p));
624			if (*p == '=') {
625				expandarg(argp, &varlist, EXP_VARTILDE);
626				continue;
627			}
628		}
629		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
630		varflag = 0;
631	}
632	*arglist.lastp = NULL;
633	*varlist.lastp = NULL;
634	expredir(cmd->ncmd.redirect);
635	argc = 0;
636	for (sp = arglist.list ; sp ; sp = sp->next)
637		argc++;
638	argv = stalloc(sizeof (char *) * (argc + 1));
639
640	for (sp = arglist.list ; sp ; sp = sp->next) {
641		TRACE(("evalcommand arg: %s\n", sp->text));
642		*argv++ = sp->text;
643	}
644	*argv = NULL;
645	lastarg = NULL;
646	if (iflag && funcnest == 0 && argc > 0)
647		lastarg = argv[-1];
648	argv -= argc;
649
650	/* Print the command if xflag is set. */
651	if (xflag) {
652		outc('+', &errout);
653		for (sp = varlist.list ; sp ; sp = sp->next) {
654			outc(' ', &errout);
655			out2str(sp->text);
656		}
657		for (sp = arglist.list ; sp ; sp = sp->next) {
658			outc(' ', &errout);
659			out2str(sp->text);
660		}
661		outc('\n', &errout);
662		flushout(&errout);
663	}
664
665	/* Now locate the command. */
666	if (argc == 0) {
667		cmdentry.cmdtype = CMDBUILTIN;
668		cmdentry.u.index = BLTINCMD;
669	} else {
670		static const char PATH[] = "PATH=";
671		char *path = pathval();
672
673		/*
674		 * Modify the command lookup path, if a PATH= assignment
675		 * is present
676		 */
677		for (sp = varlist.list ; sp ; sp = sp->next)
678			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
679				path = sp->text + sizeof(PATH) - 1;
680
681		find_command(argv[0], &cmdentry, 1, path);
682		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
683			exitstatus = 127;
684			flushout(&errout);
685			return;
686		}
687		/* implement the bltin builtin here */
688		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
689			for (;;) {
690				argv++;
691				if (--argc == 0)
692					break;
693				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
694					outfmt(&errout, "%s: not found\n", *argv);
695					exitstatus = 127;
696					flushout(&errout);
697					return;
698				}
699				if (cmdentry.u.index != BLTINCMD)
700					break;
701			}
702		}
703	}
704
705	/* Fork off a child process if necessary. */
706	if (cmd->ncmd.backgnd
707	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
708	 || ((flags & EV_BACKCMD) != 0
709	    && (cmdentry.cmdtype != CMDBUILTIN
710		 || cmdentry.u.index == DOTCMD
711		 || cmdentry.u.index == EVALCMD))) {
712		jp = makejob(cmd, 1);
713		mode = cmd->ncmd.backgnd;
714		if (flags & EV_BACKCMD) {
715			mode = FORK_NOJOB;
716			if (pipe(pip) < 0)
717				error("Pipe call failed");
718		}
719		if (forkshell(jp, cmd, mode) != 0)
720			goto parent;	/* at end of routine */
721		if (flags & EV_BACKCMD) {
722			FORCEINTON;
723			close(pip[0]);
724			if (pip[1] != 1) {
725				close(1);
726				copyfd(pip[1], 1);
727				close(pip[1]);
728			}
729		}
730		flags |= EV_EXIT;
731	}
732
733	/* This is the child process if a fork occurred. */
734	/* Execute the command. */
735	if (cmdentry.cmdtype == CMDFUNCTION) {
736#ifdef DEBUG
737		trputs("Shell function:  ");  trargs(argv);
738#endif
739		redirect(cmd->ncmd.redirect, REDIR_PUSH);
740		saveparam = shellparam;
741		shellparam.malloc = 0;
742		shellparam.reset = 1;
743		shellparam.nparam = argc - 1;
744		shellparam.p = argv + 1;
745		shellparam.optnext = NULL;
746		INTOFF;
747		savelocalvars = localvars;
748		localvars = NULL;
749		INTON;
750		if (setjmp(jmploc.loc)) {
751			if (exception == EXSHELLPROC)
752				freeparam((struct shparam *)&saveparam);
753			else {
754				freeparam(&shellparam);
755				shellparam = saveparam;
756			}
757			poplocalvars();
758			localvars = savelocalvars;
759			handler = savehandler;
760			longjmp(handler->loc, 1);
761		}
762		savehandler = handler;
763		handler = &jmploc;
764		for (sp = varlist.list ; sp ; sp = sp->next)
765			mklocal(sp->text);
766		funcnest++;
767		evaltree(cmdentry.u.func, 0);
768		funcnest--;
769		INTOFF;
770		poplocalvars();
771		localvars = savelocalvars;
772		freeparam(&shellparam);
773		shellparam = saveparam;
774		handler = savehandler;
775		popredir();
776		INTON;
777		if (evalskip == SKIPFUNC) {
778			evalskip = 0;
779			skipcount = 0;
780		}
781		if (flags & EV_EXIT)
782			exitshell(exitstatus);
783	} else if (cmdentry.cmdtype == CMDBUILTIN) {
784#ifdef DEBUG
785		trputs("builtin command:  ");  trargs(argv);
786#endif
787		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
788		if (flags == EV_BACKCMD) {
789			memout.nleft = 0;
790			memout.nextc = memout.buf;
791			memout.bufsize = 64;
792			mode |= REDIR_BACKQ;
793		}
794		redirect(cmd->ncmd.redirect, mode);
795		savecmdname = commandname;
796		cmdenviron = varlist.list;
797		e = -1;
798		if (setjmp(jmploc.loc)) {
799			e = exception;
800			exitstatus = (e == EXINT)? SIGINT+128 : 2;
801			goto cmddone;
802		}
803		savehandler = handler;
804		handler = &jmploc;
805		commandname = argv[0];
806		argptr = argv + 1;
807		optptr = NULL;			/* initialize nextopt */
808		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
809		flushall();
810cmddone:
811		out1 = &output;
812		out2 = &errout;
813		freestdout();
814		if (e != EXSHELLPROC) {
815			commandname = savecmdname;
816			if (flags & EV_EXIT) {
817				exitshell(exitstatus);
818			}
819		}
820		handler = savehandler;
821		if (e != -1) {
822			if ((e != EXERROR && e != EXEXEC)
823			   || cmdentry.u.index == BLTINCMD
824			   || cmdentry.u.index == DOTCMD
825			   || cmdentry.u.index == EVALCMD
826#ifndef NO_HISTORY
827			   || cmdentry.u.index == HISTCMD
828#endif
829			   || cmdentry.u.index == EXECCMD)
830				exraise(e);
831			FORCEINTON;
832		}
833		if (cmdentry.u.index != EXECCMD)
834			popredir();
835		if (flags == EV_BACKCMD) {
836			backcmd->buf = memout.buf;
837			backcmd->nleft = memout.nextc - memout.buf;
838			memout.buf = NULL;
839		}
840	} else {
841#ifdef DEBUG
842		trputs("normal command:  ");  trargs(argv);
843#endif
844		clearredir();
845		redirect(cmd->ncmd.redirect, 0);
846		for (sp = varlist.list ; sp ; sp = sp->next)
847			setvareq(sp->text, VEXPORT|VSTACK);
848		envp = environment();
849		shellexec(argv, envp, pathval(), cmdentry.u.index);
850		/*NOTREACHED*/
851	}
852	goto out;
853
854parent:	/* parent process gets here (if we forked) */
855	if (mode == 0) {	/* argument to fork */
856		INTOFF;
857		exitstatus = waitforjob(jp);
858		INTON;
859	} else if (mode == 2) {
860		backcmd->fd = pip[0];
861		close(pip[1]);
862		backcmd->jp = jp;
863	}
864
865out:
866	if (lastarg)
867		setvar("_", lastarg, 0);
868	popstackmark(&smark);
869}
870
871
872
873/*
874 * Search for a command.  This is called before we fork so that the
875 * location of the command will be available in the parent as well as
876 * the child.  The check for "goodname" is an overly conservative
877 * check that the name will not be subject to expansion.
878 */
879
880STATIC void
881prehash(n)
882	union node *n;
883{
884	struct cmdentry entry;
885
886	if (n->type == NCMD && n->ncmd.args)
887		if (goodname(n->ncmd.args->narg.text))
888			find_command(n->ncmd.args->narg.text, &entry, 0,
889				     pathval());
890}
891
892
893
894/*
895 * Builtin commands.  Builtin commands whose functions are closely
896 * tied to evaluation are implemented here.
897 */
898
899/*
900 * No command given, or a bltin command with no arguments.  Set the
901 * specified variables.
902 */
903
904int
905bltincmd(argc, argv)
906	int argc;
907	char **argv;
908{
909	listsetvar(cmdenviron);
910	/*
911	 * Preserve exitstatus of a previous possible redirection
912	 * as POSIX mandates
913	 */
914	return exitstatus;
915}
916
917
918/*
919 * Handle break and continue commands.  Break, continue, and return are
920 * all handled by setting the evalskip flag.  The evaluation routines
921 * above all check this flag, and if it is set they start skipping
922 * commands rather than executing them.  The variable skipcount is
923 * the number of loops to break/continue, or the number of function
924 * levels to return.  (The latter is always 1.)  It should probably
925 * be an error to break out of more loops than exist, but it isn't
926 * in the standard shell so we don't make it one here.
927 */
928
929int
930breakcmd(argc, argv)
931	int argc;
932	char **argv;
933{
934	int n = argc > 1 ? number(argv[1]) : 1;
935
936	if (n > loopnest)
937		n = loopnest;
938	if (n > 0) {
939		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
940		skipcount = n;
941	}
942	return 0;
943}
944
945
946/*
947 * The return command.
948 */
949
950int
951returncmd(argc, argv)
952	int argc;
953	char **argv;
954{
955	int ret = argc > 1 ? number(argv[1]) : oexitstatus;
956
957	if (funcnest) {
958		evalskip = SKIPFUNC;
959		skipcount = 1;
960	} else {
961		/* skip the rest of the file */
962		evalskip = SKIPFILE;
963		skipcount = 1;
964	}
965	return ret;
966}
967
968
969int
970falsecmd(argc, argv)
971	int argc;
972	char **argv;
973{
974	return 1;
975}
976
977
978int
979truecmd(argc, argv)
980	int argc;
981	char **argv;
982{
983	return 0;
984}
985
986
987int
988execcmd(argc, argv)
989	int argc;
990	char **argv;
991{
992	if (argc > 1) {
993		struct strlist *sp;
994
995		iflag = 0;		/* exit on error */
996		mflag = 0;
997		optschanged();
998		for (sp = cmdenviron; sp ; sp = sp->next)
999			setvareq(sp->text, VEXPORT|VSTACK);
1000		shellexec(argv + 1, environment(), pathval(), 0);
1001
1002	}
1003	return 0;
1004}
1005