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