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