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