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