eval.c revision 18754
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.5 1996/09/01 10:19:57 peter 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			/* don't bomb out on "set -e; false && true" */
214			flags |= EV_TESTED;
215			goto out;
216		}
217		evaltree(n->nbinary.ch2, flags);
218		break;
219	case NOR:
220		evaltree(n->nbinary.ch1, EV_TESTED);
221		if (evalskip || exitstatus == 0)
222			goto out;
223		evaltree(n->nbinary.ch2, flags);
224		break;
225	case NREDIR:
226		expredir(n->nredir.redirect);
227		redirect(n->nredir.redirect, REDIR_PUSH);
228		evaltree(n->nredir.n, flags);
229		popredir();
230		break;
231	case NSUBSHELL:
232		evalsubshell(n, flags);
233		break;
234	case NBACKGND:
235		evalsubshell(n, flags);
236		break;
237	case NIF: {
238		int status;
239
240		evaltree(n->nif.test, EV_TESTED);
241		status = exitstatus;
242		exitstatus = 0;
243		if (evalskip)
244			goto out;
245		if (status == 0)
246			evaltree(n->nif.ifpart, flags);
247		else if (n->nif.elsepart)
248			evaltree(n->nif.elsepart, flags);
249		break;
250	}
251	case NWHILE:
252	case NUNTIL:
253		evalloop(n);
254		break;
255	case NFOR:
256		evalfor(n);
257		break;
258	case NCASE:
259		evalcase(n, flags);
260		break;
261	case NDEFUN:
262		defun(n->narg.text, n->narg.next);
263		exitstatus = 0;
264		break;
265	case NNOT:
266		evaltree(n->nnot.com, EV_TESTED);
267		exitstatus = !exitstatus;
268		break;
269
270	case NPIPE:
271		evalpipe(n);
272		break;
273	case NCMD:
274		evalcommand(n, flags, (struct backcmd *)NULL);
275		break;
276	default:
277		out1fmt("Node type = %d\n", n->type);
278		flushout(&output);
279		break;
280	}
281out:
282	if (pendingsigs)
283		dotrap();
284	if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
285		exitshell(exitstatus);
286}
287
288
289STATIC void
290evalloop(n)
291	union node *n;
292{
293	int status;
294
295	loopnest++;
296	status = 0;
297	for (;;) {
298		evaltree(n->nbinary.ch1, EV_TESTED);
299		if (evalskip) {
300skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
301				evalskip = 0;
302				continue;
303			}
304			if (evalskip == SKIPBREAK && --skipcount <= 0)
305				evalskip = 0;
306			break;
307		}
308		if (n->type == NWHILE) {
309			if (exitstatus != 0)
310				break;
311		} else {
312			if (exitstatus == 0)
313				break;
314		}
315		evaltree(n->nbinary.ch2, 0);
316		status = exitstatus;
317		if (evalskip)
318			goto skipping;
319	}
320	loopnest--;
321	exitstatus = status;
322}
323
324
325
326STATIC void
327evalfor(n)
328    union node *n;
329{
330	struct arglist arglist;
331	union node *argp;
332	struct strlist *sp;
333	struct stackmark smark;
334
335	setstackmark(&smark);
336	arglist.lastp = &arglist.list;
337	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
338		oexitstatus = exitstatus;
339		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
340		if (evalskip)
341			goto out;
342	}
343	*arglist.lastp = NULL;
344
345	exitstatus = 0;
346	loopnest++;
347	for (sp = arglist.list ; sp ; sp = sp->next) {
348		setvar(n->nfor.var, sp->text, 0);
349		evaltree(n->nfor.body, 0);
350		if (evalskip) {
351			if (evalskip == SKIPCONT && --skipcount <= 0) {
352				evalskip = 0;
353				continue;
354			}
355			if (evalskip == SKIPBREAK && --skipcount <= 0)
356				evalskip = 0;
357			break;
358		}
359	}
360	loopnest--;
361out:
362	popstackmark(&smark);
363}
364
365
366
367STATIC void
368evalcase(n, flags)
369	union node *n;
370	int flags;
371{
372	union node *cp;
373	union node *patp;
374	struct arglist arglist;
375	struct stackmark smark;
376
377	setstackmark(&smark);
378	arglist.lastp = &arglist.list;
379	oexitstatus = exitstatus;
380	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
381	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
382		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
383			if (casematch(patp, arglist.list->text)) {
384				if (evalskip == 0) {
385					evaltree(cp->nclist.body, flags);
386				}
387				goto out;
388			}
389		}
390	}
391out:
392	popstackmark(&smark);
393}
394
395
396
397/*
398 * Kick off a subshell to evaluate a tree.
399 */
400
401STATIC void
402evalsubshell(n, flags)
403	union node *n;
404	int flags;
405{
406	struct job *jp;
407	int backgnd = (n->type == NBACKGND);
408
409	expredir(n->nredir.redirect);
410	jp = makejob(n, 1);
411	if (forkshell(jp, n, backgnd) == 0) {
412		if (backgnd)
413			flags &=~ EV_TESTED;
414		redirect(n->nredir.redirect, 0);
415		evaltree(n->nredir.n, flags | EV_EXIT);	/* never returns */
416	}
417	if (! backgnd) {
418		INTOFF;
419		exitstatus = waitforjob(jp);
420		INTON;
421	}
422}
423
424
425
426/*
427 * Compute the names of the files in a redirection list.
428 */
429
430STATIC void
431expredir(n)
432	union node *n;
433{
434	register union node *redir;
435
436	for (redir = n ; redir ; redir = redir->nfile.next) {
437		struct arglist fn;
438		fn.lastp = &fn.list;
439		oexitstatus = exitstatus;
440		switch (redir->type) {
441		case NFROM:
442		case NTO:
443		case NAPPEND:
444			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
445			redir->nfile.expfname = fn.list->text;
446			break;
447		case NFROMFD:
448		case NTOFD:
449			if (redir->ndup.vname) {
450				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
451				fixredir(redir, fn.list->text, 1);
452			}
453			break;
454		}
455	}
456}
457
458
459
460/*
461 * Evaluate a pipeline.  All the processes in the pipeline are children
462 * of the process creating the pipeline.  (This differs from some versions
463 * of the shell, which make the last process in a pipeline the parent
464 * of all the rest.)
465 */
466
467STATIC void
468evalpipe(n)
469	union node *n;
470{
471	struct job *jp;
472	struct nodelist *lp;
473	int pipelen;
474	int prevfd;
475	int pip[2];
476
477	TRACE(("evalpipe(0x%lx) called\n", (long)n));
478	pipelen = 0;
479	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
480		pipelen++;
481	INTOFF;
482	jp = makejob(n, pipelen);
483	prevfd = -1;
484	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
485		prehash(lp->n);
486		pip[1] = -1;
487		if (lp->next) {
488			if (pipe(pip) < 0) {
489				close(prevfd);
490				error("Pipe call failed");
491			}
492		}
493		if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
494			INTON;
495			if (prevfd > 0) {
496				close(0);
497				copyfd(prevfd, 0);
498				close(prevfd);
499			}
500			if (pip[1] >= 0) {
501				close(pip[0]);
502				if (pip[1] != 1) {
503					close(1);
504					copyfd(pip[1], 1);
505					close(pip[1]);
506				}
507			}
508			evaltree(lp->n, EV_EXIT);
509		}
510		if (prevfd >= 0)
511			close(prevfd);
512		prevfd = pip[0];
513		close(pip[1]);
514	}
515	INTON;
516	if (n->npipe.backgnd == 0) {
517		INTOFF;
518		exitstatus = waitforjob(jp);
519		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
520		INTON;
521	}
522}
523
524
525
526/*
527 * Execute a command inside back quotes.  If it's a builtin command, we
528 * want to save its output in a block obtained from malloc.  Otherwise
529 * we fork off a subprocess and get the output of the command via a pipe.
530 * Should be called with interrupts off.
531 */
532
533void
534evalbackcmd(n, result)
535	union node *n;
536	struct backcmd *result;
537{
538	int pip[2];
539	struct job *jp;
540	struct stackmark smark;		/* unnecessary */
541
542	setstackmark(&smark);
543	result->fd = -1;
544	result->buf = NULL;
545	result->nleft = 0;
546	result->jp = NULL;
547	if (n == NULL) {
548		exitstatus = 0;
549		goto out;
550	}
551	if (n->type == NCMD) {
552		exitstatus = oexitstatus;
553		evalcommand(n, EV_BACKCMD, result);
554	} else {
555		exitstatus = 0;
556		if (pipe(pip) < 0)
557			error("Pipe call failed");
558		jp = makejob(n, 1);
559		if (forkshell(jp, n, FORK_NOJOB) == 0) {
560			FORCEINTON;
561			close(pip[0]);
562			if (pip[1] != 1) {
563				close(1);
564				copyfd(pip[1], 1);
565				close(pip[1]);
566			}
567			evaltree(n, EV_EXIT);
568		}
569		close(pip[1]);
570		result->fd = pip[0];
571		result->jp = jp;
572	}
573out:
574	popstackmark(&smark);
575	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
576		result->fd, result->buf, result->nleft, result->jp));
577}
578
579
580
581/*
582 * Execute a simple command.
583 */
584
585STATIC void
586evalcommand(cmd, flags, backcmd)
587	union node *cmd;
588	int flags;
589	struct backcmd *backcmd;
590{
591	struct stackmark smark;
592	union node *argp;
593	struct arglist arglist;
594	struct arglist varlist;
595	char **argv;
596	int argc;
597	char **envp;
598	int varflag;
599	struct strlist *sp;
600	int mode;
601	int pip[2];
602	struct cmdentry cmdentry;
603	struct job *jp;
604	struct jmploc jmploc;
605	struct jmploc *volatile savehandler;
606	char *volatile savecmdname;
607	volatile struct shparam saveparam;
608	struct localvar *volatile savelocalvars;
609	volatile int e;
610	char *lastarg;
611#if __GNUC__
612	/* Avoid longjmp clobbering */
613	(void) &argv;
614	(void) &argc;
615	(void) &lastarg;
616	(void) &flags;
617#endif
618
619	/* First expand the arguments. */
620	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
621	setstackmark(&smark);
622	arglist.lastp = &arglist.list;
623	varlist.lastp = &varlist.list;
624	varflag = 1;
625	oexitstatus = exitstatus;
626	exitstatus = 0;
627	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
628		char *p = argp->narg.text;
629		if (varflag && is_name(*p)) {
630			do {
631				p++;
632			} while (is_in_name(*p));
633			if (*p == '=') {
634				expandarg(argp, &varlist, EXP_VARTILDE);
635				continue;
636			}
637		}
638		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
639		varflag = 0;
640	}
641	*arglist.lastp = NULL;
642	*varlist.lastp = NULL;
643	expredir(cmd->ncmd.redirect);
644	argc = 0;
645	for (sp = arglist.list ; sp ; sp = sp->next)
646		argc++;
647	argv = stalloc(sizeof (char *) * (argc + 1));
648
649	for (sp = arglist.list ; sp ; sp = sp->next) {
650		TRACE(("evalcommand arg: %s\n", sp->text));
651		*argv++ = sp->text;
652	}
653	*argv = NULL;
654	lastarg = NULL;
655	if (iflag && funcnest == 0 && argc > 0)
656		lastarg = argv[-1];
657	argv -= argc;
658
659	/* Print the command if xflag is set. */
660	if (xflag) {
661		outc('+', &errout);
662		for (sp = varlist.list ; sp ; sp = sp->next) {
663			outc(' ', &errout);
664			out2str(sp->text);
665		}
666		for (sp = arglist.list ; sp ; sp = sp->next) {
667			outc(' ', &errout);
668			out2str(sp->text);
669		}
670		outc('\n', &errout);
671		flushout(&errout);
672	}
673
674	/* Now locate the command. */
675	if (argc == 0) {
676		cmdentry.cmdtype = CMDBUILTIN;
677		cmdentry.u.index = BLTINCMD;
678	} else {
679		static const char PATH[] = "PATH=";
680		char *path = pathval();
681
682		/*
683		 * Modify the command lookup path, if a PATH= assignment
684		 * is present
685		 */
686		for (sp = varlist.list ; sp ; sp = sp->next)
687			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
688				path = sp->text + sizeof(PATH) - 1;
689
690		find_command(argv[0], &cmdentry, 1, path);
691		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
692			exitstatus = 1;
693			flushout(&errout);
694			return;
695		}
696		/* implement the bltin builtin here */
697		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
698			for (;;) {
699				argv++;
700				if (--argc == 0)
701					break;
702				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
703					outfmt(&errout, "%s: not found\n", *argv);
704					exitstatus = 1;
705					flushout(&errout);
706					return;
707				}
708				if (cmdentry.u.index != BLTINCMD)
709					break;
710			}
711		}
712	}
713
714	/* Fork off a child process if necessary. */
715	if (cmd->ncmd.backgnd
716	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
717	 || ((flags & EV_BACKCMD) != 0
718	    && (cmdentry.cmdtype != CMDBUILTIN
719		 || cmdentry.u.index == DOTCMD
720		 || cmdentry.u.index == EVALCMD))) {
721		jp = makejob(cmd, 1);
722		mode = cmd->ncmd.backgnd;
723		if (flags & EV_BACKCMD) {
724			mode = FORK_NOJOB;
725			if (pipe(pip) < 0)
726				error("Pipe call failed");
727		}
728		if (forkshell(jp, cmd, mode) != 0)
729			goto parent;	/* at end of routine */
730		if (flags & EV_BACKCMD) {
731			FORCEINTON;
732			close(pip[0]);
733			if (pip[1] != 1) {
734				close(1);
735				copyfd(pip[1], 1);
736				close(pip[1]);
737			}
738		}
739		flags |= EV_EXIT;
740	}
741
742	/* This is the child process if a fork occurred. */
743	/* Execute the command. */
744	if (cmdentry.cmdtype == CMDFUNCTION) {
745		trputs("Shell function:  ");  trargs(argv);
746		redirect(cmd->ncmd.redirect, REDIR_PUSH);
747		saveparam = shellparam;
748		shellparam.malloc = 0;
749		shellparam.nparam = argc - 1;
750		shellparam.p = argv + 1;
751		shellparam.optnext = NULL;
752		INTOFF;
753		savelocalvars = localvars;
754		localvars = NULL;
755		INTON;
756		if (setjmp(jmploc.loc)) {
757			if (exception == EXSHELLPROC)
758				freeparam((struct shparam *)&saveparam);
759			else {
760				freeparam(&shellparam);
761				shellparam = saveparam;
762			}
763			poplocalvars();
764			localvars = savelocalvars;
765			handler = savehandler;
766			longjmp(handler->loc, 1);
767		}
768		savehandler = handler;
769		handler = &jmploc;
770		for (sp = varlist.list ; sp ; sp = sp->next)
771			mklocal(sp->text);
772		funcnest++;
773		evaltree(cmdentry.u.func, 0);
774		funcnest--;
775		INTOFF;
776		poplocalvars();
777		localvars = savelocalvars;
778		freeparam(&shellparam);
779		shellparam = saveparam;
780		handler = savehandler;
781		popredir();
782		INTON;
783		if (evalskip == SKIPFUNC) {
784			evalskip = 0;
785			skipcount = 0;
786		}
787		if (flags & EV_EXIT)
788			exitshell(exitstatus);
789	} else if (cmdentry.cmdtype == CMDBUILTIN) {
790		trputs("builtin command:  ");  trargs(argv);
791		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
792		if (flags == EV_BACKCMD) {
793			memout.nleft = 0;
794			memout.nextc = memout.buf;
795			memout.bufsize = 64;
796			mode |= REDIR_BACKQ;
797		}
798		redirect(cmd->ncmd.redirect, mode);
799		savecmdname = commandname;
800		cmdenviron = varlist.list;
801		e = -1;
802		if (setjmp(jmploc.loc)) {
803			e = exception;
804			exitstatus = (e == EXINT)? SIGINT+128 : 2;
805			goto cmddone;
806		}
807		savehandler = handler;
808		handler = &jmploc;
809		commandname = argv[0];
810		argptr = argv + 1;
811		optptr = NULL;			/* initialize nextopt */
812		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
813		flushall();
814cmddone:
815		out1 = &output;
816		out2 = &errout;
817		freestdout();
818		if (e != EXSHELLPROC) {
819			commandname = savecmdname;
820			if (flags & EV_EXIT) {
821				exitshell(exitstatus);
822			}
823		}
824		handler = savehandler;
825		if (e != -1) {
826			if (e != EXERROR || cmdentry.u.index == BLTINCMD
827					       || cmdentry.u.index == DOTCMD
828					       || cmdentry.u.index == EVALCMD
829#ifndef NO_HISTORY
830					       || cmdentry.u.index == HISTCMD
831#endif
832					       || cmdentry.u.index == EXECCMD)
833				exraise(e);
834			FORCEINTON;
835		}
836		if (cmdentry.u.index != EXECCMD)
837			popredir();
838		if (flags == EV_BACKCMD) {
839			backcmd->buf = memout.buf;
840			backcmd->nleft = memout.nextc - memout.buf;
841			memout.buf = NULL;
842		}
843	} else {
844		trputs("normal command:  ");  trargs(argv);
845		clearredir();
846		redirect(cmd->ncmd.redirect, 0);
847		for (sp = varlist.list ; sp ; sp = sp->next)
848			setvareq(sp->text, VEXPORT|VSTACK);
849		envp = environment();
850		shellexec(argv, envp, pathval(), cmdentry.u.index);
851		/*NOTREACHED*/
852	}
853	goto out;
854
855parent:	/* parent process gets here (if we forked) */
856	if (mode == 0) {	/* argument to fork */
857		INTOFF;
858		exitstatus = waitforjob(jp);
859		INTON;
860	} else if (mode == 2) {
861		backcmd->fd = pip[0];
862		close(pip[1]);
863		backcmd->jp = jp;
864	}
865
866out:
867	if (lastarg)
868		setvar("_", lastarg, 0);
869	popstackmark(&smark);
870}
871
872
873
874/*
875 * Search for a command.  This is called before we fork so that the
876 * location of the command will be available in the parent as well as
877 * the child.  The check for "goodname" is an overly conservative
878 * check that the name will not be subject to expansion.
879 */
880
881STATIC void
882prehash(n)
883	union node *n;
884{
885	struct cmdentry entry;
886
887	if (n->type == NCMD && n->ncmd.args)
888		if (goodname(n->ncmd.args->narg.text))
889			find_command(n->ncmd.args->narg.text, &entry, 0,
890				     pathval());
891}
892
893
894
895/*
896 * Builtin commands.  Builtin commands whose functions are closely
897 * tied to evaluation are implemented here.
898 */
899
900/*
901 * No command given, or a bltin command with no arguments.  Set the
902 * specified variables.
903 */
904
905int
906bltincmd(argc, argv)
907	int argc;
908	char **argv;
909{
910	listsetvar(cmdenviron);
911	/*
912	 * Preserve exitstatus of a previous possible redirection
913	 * as POSIX mandates
914	 */
915	return exitstatus;
916}
917
918
919/*
920 * Handle break and continue commands.  Break, continue, and return are
921 * all handled by setting the evalskip flag.  The evaluation routines
922 * above all check this flag, and if it is set they start skipping
923 * commands rather than executing them.  The variable skipcount is
924 * the number of loops to break/continue, or the number of function
925 * levels to return.  (The latter is always 1.)  It should probably
926 * be an error to break out of more loops than exist, but it isn't
927 * in the standard shell so we don't make it one here.
928 */
929
930int
931breakcmd(argc, argv)
932	int argc;
933	char **argv;
934{
935	int n;
936
937	n = 1;
938	if (argc > 1)
939		n = number(argv[1]);
940	if (n > loopnest)
941		n = loopnest;
942	if (n > 0) {
943		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
944		skipcount = n;
945	}
946	return 0;
947}
948
949
950/*
951 * The return command.
952 */
953
954int
955returncmd(argc, argv)
956	int argc;
957	char **argv;
958{
959	int ret;
960
961	ret = exitstatus;
962	if (argc > 1)
963		ret = number(argv[1]);
964	if (funcnest) {
965		evalskip = SKIPFUNC;
966		skipcount = 1;
967	}
968	return ret;
969}
970
971
972int
973falsecmd(argc, argv)
974	int argc;
975	char **argv;
976{
977	return 1;
978}
979
980
981int
982truecmd(argc, argv)
983	int argc;
984	char **argv;
985{
986	return 0;
987}
988
989
990int
991execcmd(argc, argv)
992	int argc;
993	char **argv;
994{
995	if (argc > 1) {
996		struct strlist *sp;
997
998		iflag = 0;		/* exit on error */
999		mflag = 0;
1000		optschanged();
1001		for (sp = cmdenviron; sp ; sp = sp->next)
1002			setvareq(sp->text, VEXPORT|VSTACK);
1003		shellexec(argv + 1, environment(), pathval(), 0);
1004
1005	}
1006	return 0;
1007}
1008