eval.c revision 19683
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 *	$Id: eval.c,v 1.7 1996/10/22 03:02:07 steve Exp $
37 */
38
39#ifndef lint
40static char 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
79
80/* reasons for skipping commands (see comment on breakcmd routine) */
81#define SKIPBREAK 1
82#define SKIPCONT 2
83#define SKIPFUNC 3
84
85MKINIT int evalskip;		/* set if we are skipping commands */
86STATIC int skipcount;		/* number of levels to skip */
87MKINIT int loopnest;		/* current loop nesting level */
88int funcnest;			/* depth of function calls */
89
90
91char *commandname;
92struct strlist *cmdenviron;
93int exitstatus;			/* exit status of last command */
94int oexitstatus;		/* saved exit status */
95
96
97STATIC void evalloop __P((union node *));
98STATIC void evalfor __P((union node *));
99STATIC void evalcase __P((union node *, int));
100STATIC void evalsubshell __P((union node *, int));
101STATIC void expredir __P((union node *));
102STATIC void evalpipe __P((union node *));
103STATIC void evalcommand __P((union node *, int, struct backcmd *));
104STATIC void prehash __P((union node *));
105
106
107/*
108 * Called to reset things after an exception.
109 */
110
111#ifdef mkinit
112INCLUDE "eval.h"
113
114RESET {
115	evalskip = 0;
116	loopnest = 0;
117	funcnest = 0;
118}
119
120SHELLPROC {
121	exitstatus = 0;
122}
123#endif
124
125
126
127/*
128 * The eval commmand.
129 */
130
131int
132evalcmd(argc, argv)
133	int argc;
134	char **argv;
135{
136        char *p;
137        char *concat;
138        char **ap;
139
140        if (argc > 1) {
141                p = argv[1];
142                if (argc > 2) {
143                        STARTSTACKSTR(concat);
144                        ap = argv + 2;
145                        for (;;) {
146                                while (*p)
147                                        STPUTC(*p++, concat);
148                                if ((p = *ap++) == NULL)
149                                        break;
150                                STPUTC(' ', concat);
151                        }
152                        STPUTC('\0', concat);
153                        p = grabstackstr(concat);
154                }
155                evalstring(p);
156        }
157        return exitstatus;
158}
159
160
161/*
162 * Execute a command or commands contained in a string.
163 */
164
165void
166evalstring(s)
167	char *s;
168	{
169	union node *n;
170	struct stackmark smark;
171
172	setstackmark(&smark);
173	setinputstring(s, 1);
174	while ((n = parsecmd(0)) != NEOF) {
175		evaltree(n, 0);
176		popstackmark(&smark);
177	}
178	popfile();
179	popstackmark(&smark);
180}
181
182
183
184/*
185 * Evaluate a parse tree.  The value is left in the global variable
186 * exitstatus.
187 */
188
189void
190evaltree(n, flags)
191	union node *n;
192	int flags;
193{
194	if (n == NULL) {
195		TRACE(("evaltree(NULL) called\n"));
196		exitstatus = 0;
197		goto out;
198	}
199#ifndef NO_HISTORY
200	displayhist = 1;	/* show history substitutions done with fc */
201#endif
202	TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
203	switch (n->type) {
204	case NSEMI:
205		evaltree(n->nbinary.ch1, 0);
206		if (evalskip)
207			goto out;
208		evaltree(n->nbinary.ch2, flags);
209		break;
210	case NAND:
211		evaltree(n->nbinary.ch1, EV_TESTED);
212		if (evalskip || exitstatus != 0) {
213			flags |= EV_TESTED;
214			goto out;
215		}
216		evaltree(n->nbinary.ch2, flags);
217		break;
218	case NOR:
219		evaltree(n->nbinary.ch1, EV_TESTED);
220		if (evalskip || exitstatus == 0)
221			goto out;
222		evaltree(n->nbinary.ch2, flags);
223		break;
224	case NREDIR:
225		expredir(n->nredir.redirect);
226		redirect(n->nredir.redirect, REDIR_PUSH);
227		evaltree(n->nredir.n, flags);
228		popredir();
229		break;
230	case NSUBSHELL:
231		evalsubshell(n, flags);
232		break;
233	case NBACKGND:
234		evalsubshell(n, flags);
235		break;
236	case NIF: {
237		int status;
238
239		evaltree(n->nif.test, EV_TESTED);
240		status = exitstatus;
241		exitstatus = 0;
242		if (evalskip)
243			goto out;
244		if (status == 0)
245			evaltree(n->nif.ifpart, flags);
246		else if (n->nif.elsepart)
247			evaltree(n->nif.elsepart, flags);
248		break;
249	}
250	case NWHILE:
251	case NUNTIL:
252		evalloop(n);
253		break;
254	case NFOR:
255		evalfor(n);
256		break;
257	case NCASE:
258		evalcase(n, flags);
259		break;
260	case NDEFUN:
261		defun(n->narg.text, n->narg.next);
262		exitstatus = 0;
263		break;
264	case NNOT:
265		evaltree(n->nnot.com, EV_TESTED);
266		exitstatus = !exitstatus;
267		break;
268
269	case NPIPE:
270		evalpipe(n);
271		break;
272	case NCMD:
273		evalcommand(n, flags, (struct backcmd *)NULL);
274		break;
275	default:
276		out1fmt("Node type = %d\n", n->type);
277		flushout(&output);
278		break;
279	}
280out:
281	if (pendingsigs)
282		dotrap();
283	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
284		exitshell(exitstatus);
285}
286
287
288STATIC void
289evalloop(n)
290	union node *n;
291{
292	int status;
293
294	loopnest++;
295	status = 0;
296	for (;;) {
297		evaltree(n->nbinary.ch1, EV_TESTED);
298		if (evalskip) {
299skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
300				evalskip = 0;
301				continue;
302			}
303			if (evalskip == SKIPBREAK && --skipcount <= 0)
304				evalskip = 0;
305			break;
306		}
307		if (n->type == NWHILE) {
308			if (exitstatus != 0)
309				break;
310		} else {
311			if (exitstatus == 0)
312				break;
313		}
314		evaltree(n->nbinary.ch2, 0);
315		status = exitstatus;
316		if (evalskip)
317			goto skipping;
318	}
319	loopnest--;
320	exitstatus = status;
321}
322
323
324
325STATIC void
326evalfor(n)
327    union node *n;
328{
329	struct arglist arglist;
330	union node *argp;
331	struct strlist *sp;
332	struct stackmark smark;
333
334	setstackmark(&smark);
335	arglist.lastp = &arglist.list;
336	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
337		oexitstatus = exitstatus;
338		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
339		if (evalskip)
340			goto out;
341	}
342	*arglist.lastp = NULL;
343
344	exitstatus = 0;
345	loopnest++;
346	for (sp = arglist.list ; sp ; sp = sp->next) {
347		setvar(n->nfor.var, sp->text, 0);
348		evaltree(n->nfor.body, 0);
349		if (evalskip) {
350			if (evalskip == SKIPCONT && --skipcount <= 0) {
351				evalskip = 0;
352				continue;
353			}
354			if (evalskip == SKIPBREAK && --skipcount <= 0)
355				evalskip = 0;
356			break;
357		}
358	}
359	loopnest--;
360out:
361	popstackmark(&smark);
362}
363
364
365
366STATIC void
367evalcase(n, flags)
368	union node *n;
369	int flags;
370{
371	union node *cp;
372	union node *patp;
373	struct arglist arglist;
374	struct stackmark smark;
375
376	setstackmark(&smark);
377	arglist.lastp = &arglist.list;
378	oexitstatus = exitstatus;
379	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
380	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
381		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
382			if (casematch(patp, arglist.list->text)) {
383				if (evalskip == 0) {
384					evaltree(cp->nclist.body, flags);
385				}
386				goto out;
387			}
388		}
389	}
390out:
391	popstackmark(&smark);
392}
393
394
395
396/*
397 * Kick off a subshell to evaluate a tree.
398 */
399
400STATIC void
401evalsubshell(n, flags)
402	union node *n;
403	int flags;
404{
405	struct job *jp;
406	int backgnd = (n->type == NBACKGND);
407
408	expredir(n->nredir.redirect);
409	jp = makejob(n, 1);
410	if (forkshell(jp, n, backgnd) == 0) {
411		if (backgnd)
412			flags &=~ EV_TESTED;
413		redirect(n->nredir.redirect, 0);
414		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
415	}
416	if (! backgnd) {
417		INTOFF;
418		exitstatus = waitforjob(jp);
419		INTON;
420	}
421}
422
423
424
425/*
426 * Compute the names of the files in a redirection list.
427 */
428
429STATIC void
430expredir(n)
431	union node *n;
432{
433	register union node *redir;
434
435	for (redir = n ; redir ; redir = redir->nfile.next) {
436		struct arglist fn;
437		fn.lastp = &fn.list;
438		oexitstatus = exitstatus;
439		switch (redir->type) {
440		case NFROM:
441		case NTO:
442		case NAPPEND:
443			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
444			redir->nfile.expfname = fn.list->text;
445			break;
446		case NFROMFD:
447		case NTOFD:
448			if (redir->ndup.vname) {
449				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
450				fixredir(redir, fn.list->text, 1);
451			}
452			break;
453		}
454	}
455}
456
457
458
459/*
460 * Evaluate a pipeline.  All the processes in the pipeline are children
461 * of the process creating the pipeline.  (This differs from some versions
462 * of the shell, which make the last process in a pipeline the parent
463 * of all the rest.)
464 */
465
466STATIC void
467evalpipe(n)
468	union node *n;
469{
470	struct job *jp;
471	struct nodelist *lp;
472	int pipelen;
473	int prevfd;
474	int pip[2];
475
476	TRACE(("evalpipe(0x%lx) called\n", (long)n));
477	pipelen = 0;
478	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
479		pipelen++;
480	INTOFF;
481	jp = makejob(n, pipelen);
482	prevfd = -1;
483	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
484		prehash(lp->n);
485		pip[1] = -1;
486		if (lp->next) {
487			if (pipe(pip) < 0) {
488				close(prevfd);
489				error("Pipe call failed");
490			}
491		}
492		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
493			INTON;
494			if (prevfd > 0) {
495				close(0);
496				copyfd(prevfd, 0);
497				close(prevfd);
498			}
499			if (pip[1] >= 0) {
500				close(pip[0]);
501				if (pip[1] != 1) {
502					close(1);
503					copyfd(pip[1], 1);
504					close(pip[1]);
505				}
506			}
507			evaltree(lp->n, EV_EXIT);
508		}
509		if (prevfd >= 0)
510			close(prevfd);
511		prevfd = pip[0];
512		close(pip[1]);
513	}
514	INTON;
515	if (n->npipe.backgnd == 0) {
516		INTOFF;
517		exitstatus = waitforjob(jp);
518		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
519		INTON;
520	}
521}
522
523
524
525/*
526 * Execute a command inside back quotes.  If it's a builtin command, we
527 * want to save its output in a block obtained from malloc.  Otherwise
528 * we fork off a subprocess and get the output of the command via a pipe.
529 * Should be called with interrupts off.
530 */
531
532void
533evalbackcmd(n, result)
534	union node *n;
535	struct backcmd *result;
536{
537	int pip[2];
538	struct job *jp;
539	struct stackmark smark;		/* unnecessary */
540
541	setstackmark(&smark);
542	result->fd = -1;
543	result->buf = NULL;
544	result->nleft = 0;
545	result->jp = NULL;
546	if (n == NULL) {
547		exitstatus = 0;
548		goto out;
549	}
550	if (n->type == NCMD) {
551		exitstatus = oexitstatus;
552		evalcommand(n, EV_BACKCMD, result);
553	} else {
554		exitstatus = 0;
555		if (pipe(pip) < 0)
556			error("Pipe call failed");
557		jp = makejob(n, 1);
558		if (forkshell(jp, n, FORK_NOJOB) == 0) {
559			FORCEINTON;
560			close(pip[0]);
561			if (pip[1] != 1) {
562				close(1);
563				copyfd(pip[1], 1);
564				close(pip[1]);
565			}
566			evaltree(n, EV_EXIT);
567		}
568		close(pip[1]);
569		result->fd = pip[0];
570		result->jp = jp;
571	}
572out:
573	popstackmark(&smark);
574	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
575		result->fd, result->buf, result->nleft, result->jp));
576}
577
578
579
580/*
581 * Execute a simple command.
582 */
583
584STATIC void
585evalcommand(cmd, flags, backcmd)
586	union node *cmd;
587	int flags;
588	struct backcmd *backcmd;
589{
590	struct stackmark smark;
591	union node *argp;
592	struct arglist arglist;
593	struct arglist varlist;
594	char **argv;
595	int argc;
596	char **envp;
597	int varflag;
598	struct strlist *sp;
599	int mode;
600	int pip[2];
601	struct cmdentry cmdentry;
602	struct job *jp;
603	struct jmploc jmploc;
604	struct jmploc *volatile savehandler;
605	char *volatile savecmdname;
606	volatile struct shparam saveparam;
607	struct localvar *volatile savelocalvars;
608	volatile int e;
609	char *lastarg;
610#if __GNUC__
611	/* Avoid longjmp clobbering */
612	(void) &argv;
613	(void) &argc;
614	(void) &lastarg;
615	(void) &flags;
616#endif
617
618	/* First expand the arguments. */
619	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
620	setstackmark(&smark);
621	arglist.lastp = &arglist.list;
622	varlist.lastp = &varlist.list;
623	varflag = 1;
624	oexitstatus = exitstatus;
625	exitstatus = 0;
626	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
627		char *p = argp->narg.text;
628		if (varflag && is_name(*p)) {
629			do {
630				p++;
631			} while (is_in_name(*p));
632			if (*p == '=') {
633				expandarg(argp, &varlist, EXP_VARTILDE);
634				continue;
635			}
636		}
637		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
638		varflag = 0;
639	}
640	*arglist.lastp = NULL;
641	*varlist.lastp = NULL;
642	expredir(cmd->ncmd.redirect);
643	argc = 0;
644	for (sp = arglist.list ; sp ; sp = sp->next)
645		argc++;
646	argv = stalloc(sizeof (char *) * (argc + 1));
647
648	for (sp = arglist.list ; sp ; sp = sp->next) {
649		TRACE(("evalcommand arg: %s\n", sp->text));
650		*argv++ = sp->text;
651	}
652	*argv = NULL;
653	lastarg = NULL;
654	if (iflag && funcnest == 0 && argc > 0)
655		lastarg = argv[-1];
656	argv -= argc;
657
658	/* Print the command if xflag is set. */
659	if (xflag) {
660		outc('+', &errout);
661		for (sp = varlist.list ; sp ; sp = sp->next) {
662			outc(' ', &errout);
663			out2str(sp->text);
664		}
665		for (sp = arglist.list ; sp ; sp = sp->next) {
666			outc(' ', &errout);
667			out2str(sp->text);
668		}
669		outc('\n', &errout);
670		flushout(&errout);
671	}
672
673	/* Now locate the command. */
674	if (argc == 0) {
675		cmdentry.cmdtype = CMDBUILTIN;
676		cmdentry.u.index = BLTINCMD;
677	} else {
678		static const char PATH[] = "PATH=";
679		char *path = pathval();
680
681		/*
682		 * Modify the command lookup path, if a PATH= assignment
683		 * is present
684		 */
685		for (sp = varlist.list ; sp ; sp = sp->next)
686			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
687				path = sp->text + sizeof(PATH) - 1;
688
689		find_command(argv[0], &cmdentry, 1, path);
690		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
691			exitstatus = 1;
692			flushout(&errout);
693			return;
694		}
695		/* implement the bltin builtin here */
696		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
697			for (;;) {
698				argv++;
699				if (--argc == 0)
700					break;
701				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
702					outfmt(&errout, "%s: not found\n", *argv);
703					exitstatus = 1;
704					flushout(&errout);
705					return;
706				}
707				if (cmdentry.u.index != BLTINCMD)
708					break;
709			}
710		}
711	}
712
713	/* Fork off a child process if necessary. */
714	if (cmd->ncmd.backgnd
715	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
716	 || ((flags & EV_BACKCMD) != 0
717	    && (cmdentry.cmdtype != CMDBUILTIN
718		 || cmdentry.u.index == DOTCMD
719		 || cmdentry.u.index == EVALCMD))) {
720		jp = makejob(cmd, 1);
721		mode = cmd->ncmd.backgnd;
722		if (flags & EV_BACKCMD) {
723			mode = FORK_NOJOB;
724			if (pipe(pip) < 0)
725				error("Pipe call failed");
726		}
727		if (forkshell(jp, cmd, mode) != 0)
728			goto parent;	/* at end of routine */
729		if (flags & EV_BACKCMD) {
730			FORCEINTON;
731			close(pip[0]);
732			if (pip[1] != 1) {
733				close(1);
734				copyfd(pip[1], 1);
735				close(pip[1]);
736			}
737		}
738		flags |= EV_EXIT;
739	}
740
741	/* This is the child process if a fork occurred. */
742	/* Execute the command. */
743	if (cmdentry.cmdtype == CMDFUNCTION) {
744		trputs("Shell function:  ");  trargs(argv);
745		redirect(cmd->ncmd.redirect, REDIR_PUSH);
746		saveparam = shellparam;
747		shellparam.malloc = 0;
748		shellparam.nparam = argc - 1;
749		shellparam.p = argv + 1;
750		shellparam.optnext = NULL;
751		INTOFF;
752		savelocalvars = localvars;
753		localvars = NULL;
754		INTON;
755		if (setjmp(jmploc.loc)) {
756			if (exception == EXSHELLPROC)
757				freeparam((struct shparam *)&saveparam);
758			else {
759				freeparam(&shellparam);
760				shellparam = saveparam;
761			}
762			poplocalvars();
763			localvars = savelocalvars;
764			handler = savehandler;
765			longjmp(handler->loc, 1);
766		}
767		savehandler = handler;
768		handler = &jmploc;
769		for (sp = varlist.list ; sp ; sp = sp->next)
770			mklocal(sp->text);
771		funcnest++;
772		evaltree(cmdentry.u.func, 0);
773		funcnest--;
774		INTOFF;
775		poplocalvars();
776		localvars = savelocalvars;
777		freeparam(&shellparam);
778		shellparam = saveparam;
779		handler = savehandler;
780		popredir();
781		INTON;
782		if (evalskip == SKIPFUNC) {
783			evalskip = 0;
784			skipcount = 0;
785		}
786		if (flags & EV_EXIT)
787			exitshell(exitstatus);
788	} else if (cmdentry.cmdtype == CMDBUILTIN) {
789		trputs("builtin command:  ");  trargs(argv);
790		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
791		if (flags == EV_BACKCMD) {
792			memout.nleft = 0;
793			memout.nextc = memout.buf;
794			memout.bufsize = 64;
795			mode |= REDIR_BACKQ;
796		}
797		redirect(cmd->ncmd.redirect, mode);
798		savecmdname = commandname;
799		cmdenviron = varlist.list;
800		e = -1;
801		if (setjmp(jmploc.loc)) {
802			e = exception;
803			exitstatus = (e == EXINT)? SIGINT+128 : 2;
804			goto cmddone;
805		}
806		savehandler = handler;
807		handler = &jmploc;
808		commandname = argv[0];
809		argptr = argv + 1;
810		optptr = NULL;			/* initialize nextopt */
811		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
812		flushall();
813cmddone:
814		out1 = &output;
815		out2 = &errout;
816		freestdout();
817		if (e != EXSHELLPROC) {
818			commandname = savecmdname;
819			if (flags & EV_EXIT) {
820				exitshell(exitstatus);
821			}
822		}
823		handler = savehandler;
824		if (e != -1) {
825			if (e != EXERROR || cmdentry.u.index == BLTINCMD
826					       || cmdentry.u.index == DOTCMD
827					       || cmdentry.u.index == EVALCMD
828#ifndef NO_HISTORY
829					       || cmdentry.u.index == HISTCMD
830#endif
831					       || cmdentry.u.index == EXECCMD)
832				exraise(e);
833			FORCEINTON;
834		}
835		if (cmdentry.u.index != EXECCMD)
836			popredir();
837		if (flags == EV_BACKCMD) {
838			backcmd->buf = memout.buf;
839			backcmd->nleft = memout.nextc - memout.buf;
840			memout.buf = NULL;
841		}
842	} else {
843		trputs("normal command:  ");  trargs(argv);
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;
935
936	n = 1;
937	if (argc > 1)
938		n = number(argv[1]);
939	if (n > loopnest)
940		n = loopnest;
941	if (n > 0) {
942		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
943		skipcount = n;
944	}
945	return 0;
946}
947
948
949/*
950 * The return command.
951 */
952
953int
954returncmd(argc, argv)
955	int argc;
956	char **argv;
957{
958	int ret;
959
960	ret = exitstatus;
961	if (argc > 1)
962		ret = number(argv[1]);
963	if (funcnest) {
964		evalskip = SKIPFUNC;
965		skipcount = 1;
966	}
967	return ret;
968}
969
970
971int
972falsecmd(argc, argv)
973	int argc;
974	char **argv;
975{
976	return 1;
977}
978
979
980int
981truecmd(argc, argv)
982	int argc;
983	char **argv;
984{
985	return 0;
986}
987
988
989int
990execcmd(argc, argv)
991	int argc;
992	char **argv;
993{
994	if (argc > 1) {
995		struct strlist *sp;
996
997		iflag = 0;		/* exit on error */
998		mflag = 0;
999		optschanged();
1000		for (sp = cmdenviron; sp ; sp = sp->next)
1001			setvareq(sp->text, VEXPORT|VSTACK);
1002		shellexec(argv + 1, environment(), pathval(), 0);
1003
1004	}
1005	return 0;
1006}
1007