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