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