Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.19
      1 /*	$NetBSD: eval.c,v 1.19 1995/03/21 09:08:54 cgd 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.19 1995/03/21 09:08:54 cgd 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 	register char *p;
    608 	int mode;
    609 	int pip[2];
    610 	struct cmdentry cmdentry;
    611 	struct job *jp;
    612 	struct jmploc jmploc;
    613 	struct jmploc *volatile savehandler;
    614 	char *volatile savecmdname;
    615 	volatile struct shparam saveparam;
    616 	struct localvar *volatile savelocalvars;
    617 	volatile int e;
    618 	char *lastarg;
    619 
    620 	/* First expand the arguments. */
    621 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
    622 	setstackmark(&smark);
    623 	arglist.lastp = &arglist.list;
    624 	varlist.lastp = &varlist.list;
    625 	varflag = 1;
    626 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    627 		p = argp->narg.text;
    628 		if (varflag && is_name(*p)) {
    629 			do {
    630 				p++;
    631 			} while (is_in_name(*p));
    632 			if (*p == '=') {
    633 				expandarg(argp, &varlist, EXP_VARTILDE);
    634 				continue;
    635 			}
    636 		}
    637 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    638 		varflag = 0;
    639 	}
    640 	*arglist.lastp = NULL;
    641 	*varlist.lastp = NULL;
    642 	expredir(cmd->ncmd.redirect);
    643 	argc = 0;
    644 	for (sp = arglist.list ; sp ; sp = sp->next)
    645 		argc++;
    646 	argv = stalloc(sizeof (char *) * (argc + 1));
    647 
    648 	for (sp = arglist.list ; sp ; sp = sp->next) {
    649 		TRACE(("evalcommand arg: %s\n", sp->text));
    650 		*argv++ = sp->text;
    651 	}
    652 	*argv = NULL;
    653 	lastarg = NULL;
    654 	if (iflag && funcnest == 0 && argc > 0)
    655 		lastarg = argv[-1];
    656 	argv -= argc;
    657 
    658 	/* Print the command if xflag is set. */
    659 	if (xflag) {
    660 		outc('+', &errout);
    661 		for (sp = varlist.list ; sp ; sp = sp->next) {
    662 			outc(' ', &errout);
    663 			out2str(sp->text);
    664 		}
    665 		for (sp = arglist.list ; sp ; sp = sp->next) {
    666 			outc(' ', &errout);
    667 			out2str(sp->text);
    668 		}
    669 		outc('\n', &errout);
    670 		flushout(&errout);
    671 	}
    672 
    673 	/* Now locate the command. */
    674 	if (argc == 0) {
    675 		cmdentry.cmdtype = CMDBUILTIN;
    676 		cmdentry.u.index = BLTINCMD;
    677 	} else {
    678 		find_command(argv[0], &cmdentry, 1);
    679 		if (cmdentry.cmdtype == CMDUNKNOWN) {	/* command not found */
    680 			exitstatus = 2;
    681 			flushout(&errout);
    682 			return;
    683 		}
    684 		/* implement the bltin builtin here */
    685 		if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
    686 			for (;;) {
    687 				argv++;
    688 				if (--argc == 0)
    689 					break;
    690 				if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
    691 					outfmt(&errout, "%s: not found\n", *argv);
    692 					exitstatus = 2;
    693 					flushout(&errout);
    694 					return;
    695 				}
    696 				if (cmdentry.u.index != BLTINCMD)
    697 					break;
    698 			}
    699 		}
    700 	}
    701 
    702 	/* Fork off a child process if necessary. */
    703 	if (cmd->ncmd.backgnd
    704 	 || cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0
    705 	 || (flags & EV_BACKCMD) != 0
    706 	    && (cmdentry.cmdtype != CMDBUILTIN
    707 		 || cmdentry.u.index == DOTCMD
    708 		 || cmdentry.u.index == EVALCMD)) {
    709 		jp = makejob(cmd, 1);
    710 		mode = cmd->ncmd.backgnd;
    711 		if (flags & EV_BACKCMD) {
    712 			mode = FORK_NOJOB;
    713 			if (pipe(pip) < 0)
    714 				error("Pipe call failed");
    715 		}
    716 		if (forkshell(jp, cmd, mode) != 0)
    717 			goto parent;	/* at end of routine */
    718 		if (flags & EV_BACKCMD) {
    719 			FORCEINTON;
    720 			close(pip[0]);
    721 			if (pip[1] != 1) {
    722 				close(1);
    723 				copyfd(pip[1], 1);
    724 				close(pip[1]);
    725 			}
    726 		}
    727 		flags |= EV_EXIT;
    728 	}
    729 
    730 	/* This is the child process if a fork occurred. */
    731 	/* Execute the command. */
    732 	if (cmdentry.cmdtype == CMDFUNCTION) {
    733 		trputs("Shell function:  ");  trargs(argv);
    734 		redirect(cmd->ncmd.redirect, REDIR_PUSH);
    735 		saveparam = shellparam;
    736 		shellparam.malloc = 0;
    737 		shellparam.nparam = argc - 1;
    738 		shellparam.p = argv + 1;
    739 		shellparam.optnext = NULL;
    740 		INTOFF;
    741 		savelocalvars = localvars;
    742 		localvars = NULL;
    743 		INTON;
    744 		if (setjmp(jmploc.loc)) {
    745 			if (exception == EXSHELLPROC)
    746 				freeparam((struct shparam *)&saveparam);
    747 			else {
    748 				freeparam(&shellparam);
    749 				shellparam = saveparam;
    750 			}
    751 			poplocalvars();
    752 			localvars = savelocalvars;
    753 			handler = savehandler;
    754 			longjmp(handler->loc, 1);
    755 		}
    756 		savehandler = handler;
    757 		handler = &jmploc;
    758 		for (sp = varlist.list ; sp ; sp = sp->next)
    759 			mklocal(sp->text);
    760 		funcnest++;
    761 		evaltree(cmdentry.u.func, 0);
    762 		funcnest--;
    763 		INTOFF;
    764 		poplocalvars();
    765 		localvars = savelocalvars;
    766 		freeparam(&shellparam);
    767 		shellparam = saveparam;
    768 		handler = savehandler;
    769 		popredir();
    770 		INTON;
    771 		if (evalskip == SKIPFUNC) {
    772 			evalskip = 0;
    773 			skipcount = 0;
    774 		}
    775 		if (flags & EV_EXIT)
    776 			exitshell(exitstatus);
    777 	} else if (cmdentry.cmdtype == CMDBUILTIN) {
    778 		trputs("builtin command:  ");  trargs(argv);
    779 		mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
    780 		if (flags == EV_BACKCMD) {
    781 			memout.nleft = 0;
    782 			memout.nextc = memout.buf;
    783 			memout.bufsize = 64;
    784 			mode |= REDIR_BACKQ;
    785 		}
    786 		redirect(cmd->ncmd.redirect, mode);
    787 		savecmdname = commandname;
    788 		cmdenviron = varlist.list;
    789 		e = -1;
    790 		if (setjmp(jmploc.loc)) {
    791 			e = exception;
    792 			exitstatus = (e == EXINT)? SIGINT+128 : 2;
    793 			goto cmddone;
    794 		}
    795 		savehandler = handler;
    796 		handler = &jmploc;
    797 		commandname = argv[0];
    798 		argptr = argv + 1;
    799 		optptr = NULL;			/* initialize nextopt */
    800 		exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
    801 		flushall();
    802 cmddone:
    803 		out1 = &output;
    804 		out2 = &errout;
    805 		freestdout();
    806 		if (e != EXSHELLPROC) {
    807 			commandname = savecmdname;
    808 			if (flags & EV_EXIT) {
    809 				exitshell(exitstatus);
    810 			}
    811 		}
    812 		handler = savehandler;
    813 		if (e != -1) {
    814 			if (e != EXERROR || cmdentry.u.index == BLTINCMD
    815 					       || cmdentry.u.index == DOTCMD
    816 					       || cmdentry.u.index == EVALCMD
    817 #ifndef NO_HISTORY
    818 					       || cmdentry.u.index == HISTCMD
    819 #endif
    820 					       || cmdentry.u.index == EXECCMD)
    821 				exraise(e);
    822 			FORCEINTON;
    823 		}
    824 		if (cmdentry.u.index != EXECCMD)
    825 			popredir();
    826 		if (flags == EV_BACKCMD) {
    827 			backcmd->buf = memout.buf;
    828 			backcmd->nleft = memout.nextc - memout.buf;
    829 			memout.buf = NULL;
    830 		}
    831 	} else {
    832 		trputs("normal command:  ");  trargs(argv);
    833 		clearredir();
    834 		redirect(cmd->ncmd.redirect, 0);
    835 		if (varlist.list) {
    836 			p = stalloc(strlen(pathval()) + 1);
    837 			scopy(pathval(), p);
    838 		} else {
    839 			p = pathval();
    840 		}
    841 		for (sp = varlist.list ; sp ; sp = sp->next)
    842 			setvareq(sp->text, VEXPORT|VSTACK);
    843 		envp = environment();
    844 		shellexec(argv, envp, p, cmdentry.u.index);
    845 		/*NOTREACHED*/
    846 	}
    847 	goto out;
    848 
    849 parent:	/* parent process gets here (if we forked) */
    850 	if (mode == 0) {	/* argument to fork */
    851 		INTOFF;
    852 		exitstatus = waitforjob(jp);
    853 		INTON;
    854 	} else if (mode == 2) {
    855 		backcmd->fd = pip[0];
    856 		close(pip[1]);
    857 		backcmd->jp = jp;
    858 	}
    859 
    860 out:
    861 	if (lastarg)
    862 		setvar("_", lastarg, 0);
    863 	popstackmark(&smark);
    864 }
    865 
    866 
    867 
    868 /*
    869  * Search for a command.  This is called before we fork so that the
    870  * location of the command will be available in the parent as well as
    871  * the child.  The check for "goodname" is an overly conservative
    872  * check that the name will not be subject to expansion.
    873  */
    874 
    875 STATIC void
    876 prehash(n)
    877 	union node *n;
    878 	{
    879 	struct cmdentry entry;
    880 
    881 	if (n->type == NCMD && n->ncmd.args)
    882 		if (goodname(n->ncmd.args->narg.text))
    883 			find_command(n->ncmd.args->narg.text, &entry, 0);
    884 }
    885 
    886 
    887 
    888 /*
    889  * Builtin commands.  Builtin commands whose functions are closely
    890  * tied to evaluation are implemented here.
    891  */
    892 
    893 /*
    894  * No command given, or a bltin command with no arguments.  Set the
    895  * specified variables.
    896  */
    897 
    898 int
    899 bltincmd(argc, argv)
    900 	int argc;
    901 	char **argv;
    902 {
    903 	listsetvar(cmdenviron);
    904 	return 0;
    905 }
    906 
    907 
    908 /*
    909  * Handle break and continue commands.  Break, continue, and return are
    910  * all handled by setting the evalskip flag.  The evaluation routines
    911  * above all check this flag, and if it is set they start skipping
    912  * commands rather than executing them.  The variable skipcount is
    913  * the number of loops to break/continue, or the number of function
    914  * levels to return.  (The latter is always 1.)  It should probably
    915  * be an error to break out of more loops than exist, but it isn't
    916  * in the standard shell so we don't make it one here.
    917  */
    918 
    919 int
    920 breakcmd(argc, argv)
    921 	int argc;
    922 	char **argv;
    923 {
    924 	int n;
    925 
    926 	n = 1;
    927 	if (argc > 1)
    928 		n = number(argv[1]);
    929 	if (n > loopnest)
    930 		n = loopnest;
    931 	if (n > 0) {
    932 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
    933 		skipcount = n;
    934 	}
    935 	return 0;
    936 }
    937 
    938 
    939 /*
    940  * The return command.
    941  */
    942 
    943 int
    944 returncmd(argc, argv)
    945 	int argc;
    946 	char **argv;
    947 {
    948 	int ret;
    949 
    950 	ret = exitstatus;
    951 	if (argc > 1)
    952 		ret = number(argv[1]);
    953 	if (funcnest) {
    954 		evalskip = SKIPFUNC;
    955 		skipcount = 1;
    956 	}
    957 	return ret;
    958 }
    959 
    960 
    961 int
    962 falsecmd(argc, argv)
    963 	int argc;
    964 	char **argv;
    965 {
    966 	return 1;
    967 }
    968 
    969 
    970 int
    971 truecmd(argc, argv)
    972 	int argc;
    973 	char **argv;
    974 {
    975 	return 0;
    976 }
    977 
    978 
    979 int
    980 execcmd(argc, argv)
    981 	int argc;
    982 	char **argv;
    983 {
    984 	if (argc > 1) {
    985 		iflag = 0;		/* exit on error */
    986 		mflag = 0;
    987 		optschanged();
    988 		shellexec(argv + 1, environment(), pathval(), 0);
    989 
    990 	}
    991 	return 0;
    992 }
    993