Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.102.2.1
      1  1.102.2.1      yamt /*	$NetBSD: eval.c,v 1.102.2.1 2012/04/17 00:01:38 yamt Exp $	*/
      2       1.19       cgd 
      3        1.1       cgd /*-
      4        1.7       jtc  * Copyright (c) 1993
      5        1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * This code is derived from software contributed to Berkeley by
      8        1.1       cgd  * Kenneth Almquist.
      9        1.1       cgd  *
     10        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11        1.1       cgd  * modification, are permitted provided that the following conditions
     12        1.1       cgd  * are met:
     13        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.74       agc  * 3. Neither the name of the University nor the names of its contributors
     19        1.1       cgd  *    may be used to endorse or promote products derived from this software
     20        1.1       cgd  *    without specific prior written permission.
     21        1.1       cgd  *
     22        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32        1.1       cgd  * SUCH DAMAGE.
     33        1.1       cgd  */
     34        1.1       cgd 
     35       1.36  christos #include <sys/cdefs.h>
     36        1.1       cgd #ifndef lint
     37       1.19       cgd #if 0
     38       1.26  christos static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
     39       1.19       cgd #else
     40  1.102.2.1      yamt __RCSID("$NetBSD: eval.c,v 1.102.2.1 2012/04/17 00:01:38 yamt Exp $");
     41       1.24       cgd #endif
     42        1.1       cgd #endif /* not lint */
     43        1.1       cgd 
     44       1.90      tron #include <stdbool.h>
     45       1.70       dsl #include <stdlib.h>
     46       1.21  christos #include <signal.h>
     47       1.69       agc #include <stdio.h>
     48      1.100  christos #include <errno.h>
     49       1.21  christos #include <unistd.h>
     50       1.76       dsl #include <sys/fcntl.h>
     51       1.68  christos #include <sys/times.h>
     52       1.71     rafal #include <sys/param.h>
     53       1.61  christos #include <sys/types.h>
     54       1.61  christos #include <sys/wait.h>
     55       1.70       dsl #include <sys/sysctl.h>
     56       1.61  christos 
     57        1.1       cgd /*
     58        1.1       cgd  * Evaluate a command.
     59        1.1       cgd  */
     60        1.1       cgd 
     61        1.1       cgd #include "shell.h"
     62        1.1       cgd #include "nodes.h"
     63        1.1       cgd #include "syntax.h"
     64        1.1       cgd #include "expand.h"
     65        1.1       cgd #include "parser.h"
     66        1.1       cgd #include "jobs.h"
     67        1.1       cgd #include "eval.h"
     68        1.1       cgd #include "builtins.h"
     69        1.1       cgd #include "options.h"
     70        1.1       cgd #include "exec.h"
     71        1.1       cgd #include "redir.h"
     72        1.1       cgd #include "input.h"
     73        1.1       cgd #include "output.h"
     74        1.1       cgd #include "trap.h"
     75        1.1       cgd #include "var.h"
     76        1.1       cgd #include "memalloc.h"
     77        1.1       cgd #include "error.h"
     78       1.21  christos #include "show.h"
     79        1.1       cgd #include "mystring.h"
     80       1.64  christos #include "main.h"
     81       1.35  christos #ifndef SMALL
     82        1.7       jtc #include "myhistedit.h"
     83       1.11       cgd #endif
     84        1.1       cgd 
     85        1.1       cgd 
     86        1.1       cgd /* flags in argument to evaltree */
     87        1.1       cgd #define EV_EXIT 01		/* exit after evaluating tree */
     88        1.1       cgd #define EV_TESTED 02		/* exit status is checked; ignore -e flag */
     89        1.1       cgd #define EV_BACKCMD 04		/* command executing within back quotes */
     90        1.1       cgd 
     91       1.57  christos int evalskip;			/* set if we are skipping commands */
     92        1.1       cgd STATIC int skipcount;		/* number of levels to skip */
     93        1.1       cgd MKINIT int loopnest;		/* current loop nesting level */
     94        1.1       cgd int funcnest;			/* depth of function calls */
     95  1.102.2.1      yamt STATIC int builtin_flags;	/* evalcommand flags for builtins */
     96        1.1       cgd 
     97        1.1       cgd 
     98       1.89      matt const char *commandname;
     99        1.1       cgd struct strlist *cmdenviron;
    100        1.1       cgd int exitstatus;			/* exit status of last command */
    101       1.68  christos int back_exitstatus;		/* exit status of backquoted command */
    102        1.1       cgd 
    103        1.1       cgd 
    104       1.68  christos STATIC void evalloop(union node *, int);
    105       1.68  christos STATIC void evalfor(union node *, int);
    106       1.68  christos STATIC void evalcase(union node *, int);
    107       1.68  christos STATIC void evalsubshell(union node *, int);
    108       1.68  christos STATIC void expredir(union node *);
    109       1.68  christos STATIC void evalpipe(union node *);
    110       1.68  christos STATIC void evalcommand(union node *, int, struct backcmd *);
    111       1.68  christos STATIC void prehash(union node *);
    112        1.1       cgd 
    113        1.1       cgd 
    114        1.1       cgd /*
    115        1.1       cgd  * Called to reset things after an exception.
    116        1.1       cgd  */
    117        1.1       cgd 
    118        1.1       cgd #ifdef mkinit
    119        1.1       cgd INCLUDE "eval.h"
    120        1.1       cgd 
    121        1.1       cgd RESET {
    122        1.1       cgd 	evalskip = 0;
    123        1.1       cgd 	loopnest = 0;
    124        1.1       cgd 	funcnest = 0;
    125        1.1       cgd }
    126        1.1       cgd 
    127        1.1       cgd SHELLPROC {
    128        1.1       cgd 	exitstatus = 0;
    129        1.1       cgd }
    130        1.1       cgd #endif
    131        1.1       cgd 
    132       1.76       dsl static int
    133       1.76       dsl sh_pipe(int fds[2])
    134       1.76       dsl {
    135       1.76       dsl 	int nfd;
    136       1.76       dsl 
    137       1.76       dsl 	if (pipe(fds))
    138       1.76       dsl 		return -1;
    139       1.76       dsl 
    140       1.76       dsl 	if (fds[0] < 3) {
    141       1.76       dsl 		nfd = fcntl(fds[0], F_DUPFD, 3);
    142       1.76       dsl 		if (nfd != -1) {
    143       1.76       dsl 			close(fds[0]);
    144       1.76       dsl 			fds[0] = nfd;
    145       1.76       dsl 		}
    146       1.76       dsl 	}
    147       1.76       dsl 
    148       1.76       dsl 	if (fds[1] < 3) {
    149       1.76       dsl 		nfd = fcntl(fds[1], F_DUPFD, 3);
    150       1.76       dsl 		if (nfd != -1) {
    151       1.76       dsl 			close(fds[1]);
    152       1.76       dsl 			fds[1] = nfd;
    153       1.76       dsl 		}
    154       1.76       dsl 	}
    155       1.76       dsl 	return 0;
    156       1.76       dsl }
    157        1.1       cgd 
    158        1.1       cgd 
    159        1.1       cgd /*
    160        1.1       cgd  * The eval commmand.
    161        1.1       cgd  */
    162        1.1       cgd 
    163       1.16       cgd int
    164       1.68  christos evalcmd(int argc, char **argv)
    165        1.1       cgd {
    166        1.1       cgd         char *p;
    167        1.1       cgd         char *concat;
    168        1.1       cgd         char **ap;
    169        1.1       cgd 
    170        1.1       cgd         if (argc > 1) {
    171        1.1       cgd                 p = argv[1];
    172        1.1       cgd                 if (argc > 2) {
    173        1.1       cgd                         STARTSTACKSTR(concat);
    174        1.1       cgd                         ap = argv + 2;
    175        1.1       cgd                         for (;;) {
    176        1.1       cgd                                 while (*p)
    177        1.1       cgd                                         STPUTC(*p++, concat);
    178        1.1       cgd                                 if ((p = *ap++) == NULL)
    179        1.1       cgd                                         break;
    180        1.1       cgd                                 STPUTC(' ', concat);
    181        1.1       cgd                         }
    182        1.1       cgd                         STPUTC('\0', concat);
    183        1.1       cgd                         p = grabstackstr(concat);
    184        1.1       cgd                 }
    185  1.102.2.1      yamt                 evalstring(p, builtin_flags & EV_TESTED);
    186        1.1       cgd         }
    187        1.1       cgd         return exitstatus;
    188        1.1       cgd }
    189        1.1       cgd 
    190        1.1       cgd 
    191        1.1       cgd /*
    192        1.1       cgd  * Execute a command or commands contained in a string.
    193        1.1       cgd  */
    194        1.1       cgd 
    195        1.1       cgd void
    196       1.68  christos evalstring(char *s, int flag)
    197       1.68  christos {
    198        1.1       cgd 	union node *n;
    199        1.1       cgd 	struct stackmark smark;
    200        1.1       cgd 
    201        1.1       cgd 	setstackmark(&smark);
    202        1.1       cgd 	setinputstring(s, 1);
    203       1.64  christos 
    204        1.1       cgd 	while ((n = parsecmd(0)) != NEOF) {
    205       1.65  christos 		evaltree(n, flag);
    206        1.1       cgd 		popstackmark(&smark);
    207        1.1       cgd 	}
    208        1.1       cgd 	popfile();
    209        1.1       cgd 	popstackmark(&smark);
    210        1.1       cgd }
    211        1.1       cgd 
    212        1.1       cgd 
    213        1.1       cgd 
    214        1.1       cgd /*
    215        1.1       cgd  * Evaluate a parse tree.  The value is left in the global variable
    216        1.1       cgd  * exitstatus.
    217        1.1       cgd  */
    218        1.1       cgd 
    219        1.1       cgd void
    220       1.68  christos evaltree(union node *n, int flags)
    221       1.17       cgd {
    222       1.90      tron 	bool do_etest;
    223       1.90      tron 
    224       1.90      tron 	do_etest = false;
    225        1.1       cgd 	if (n == NULL) {
    226        1.1       cgd 		TRACE(("evaltree(NULL) called\n"));
    227        1.7       jtc 		exitstatus = 0;
    228        1.7       jtc 		goto out;
    229        1.1       cgd 	}
    230       1.35  christos #ifndef SMALL
    231        1.7       jtc 	displayhist = 1;	/* show history substitutions done with fc */
    232       1.10       cgd #endif
    233       1.70       dsl 	TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
    234       1.70       dsl 	    getpid(), n, n->type, flags));
    235        1.1       cgd 	switch (n->type) {
    236        1.1       cgd 	case NSEMI:
    237       1.65  christos 		evaltree(n->nbinary.ch1, flags & EV_TESTED);
    238        1.1       cgd 		if (evalskip)
    239        1.1       cgd 			goto out;
    240       1.65  christos 		evaltree(n->nbinary.ch2, flags);
    241        1.1       cgd 		break;
    242        1.1       cgd 	case NAND:
    243       1.65  christos 		evaltree(n->nbinary.ch1, EV_TESTED);
    244       1.70       dsl 		if (evalskip || exitstatus != 0)
    245        1.1       cgd 			goto out;
    246       1.79   mycroft 		evaltree(n->nbinary.ch2, flags);
    247        1.1       cgd 		break;
    248        1.1       cgd 	case NOR:
    249       1.65  christos 		evaltree(n->nbinary.ch1, EV_TESTED);
    250        1.1       cgd 		if (evalskip || exitstatus == 0)
    251        1.1       cgd 			goto out;
    252       1.79   mycroft 		evaltree(n->nbinary.ch2, flags);
    253        1.1       cgd 		break;
    254        1.1       cgd 	case NREDIR:
    255        1.1       cgd 		expredir(n->nredir.redirect);
    256        1.1       cgd 		redirect(n->nredir.redirect, REDIR_PUSH);
    257       1.65  christos 		evaltree(n->nredir.n, flags);
    258        1.1       cgd 		popredir();
    259        1.1       cgd 		break;
    260        1.1       cgd 	case NSUBSHELL:
    261       1.65  christos 		evalsubshell(n, flags);
    262       1.93      tron 		do_etest = !(flags & EV_TESTED);
    263        1.1       cgd 		break;
    264        1.1       cgd 	case NBACKGND:
    265       1.65  christos 		evalsubshell(n, flags);
    266        1.1       cgd 		break;
    267        1.1       cgd 	case NIF: {
    268       1.65  christos 		evaltree(n->nif.test, EV_TESTED);
    269        1.1       cgd 		if (evalskip)
    270        1.1       cgd 			goto out;
    271       1.28  christos 		if (exitstatus == 0)
    272       1.65  christos 			evaltree(n->nif.ifpart, flags);
    273       1.22  christos 		else if (n->nif.elsepart)
    274       1.65  christos 			evaltree(n->nif.elsepart, flags);
    275       1.29        pk 		else
    276       1.29        pk 			exitstatus = 0;
    277        1.1       cgd 		break;
    278        1.1       cgd 	}
    279        1.1       cgd 	case NWHILE:
    280        1.1       cgd 	case NUNTIL:
    281       1.65  christos 		evalloop(n, flags);
    282        1.1       cgd 		break;
    283        1.1       cgd 	case NFOR:
    284       1.65  christos 		evalfor(n, flags);
    285        1.1       cgd 		break;
    286        1.1       cgd 	case NCASE:
    287       1.65  christos 		evalcase(n, flags);
    288        1.1       cgd 		break;
    289        1.1       cgd 	case NDEFUN:
    290        1.1       cgd 		defun(n->narg.text, n->narg.next);
    291        1.1       cgd 		exitstatus = 0;
    292        1.1       cgd 		break;
    293        1.7       jtc 	case NNOT:
    294       1.65  christos 		evaltree(n->nnot.com, EV_TESTED);
    295        1.7       jtc 		exitstatus = !exitstatus;
    296        1.7       jtc 		break;
    297        1.1       cgd 	case NPIPE:
    298       1.65  christos 		evalpipe(n);
    299       1.93      tron 		do_etest = !(flags & EV_TESTED);
    300        1.1       cgd 		break;
    301        1.1       cgd 	case NCMD:
    302      1.102    plunky 		evalcommand(n, flags, NULL);
    303       1.93      tron 		do_etest = !(flags & EV_TESTED);
    304        1.1       cgd 		break;
    305        1.1       cgd 	default:
    306        1.1       cgd 		out1fmt("Node type = %d\n", n->type);
    307        1.1       cgd 		flushout(&output);
    308        1.1       cgd 		break;
    309        1.1       cgd 	}
    310        1.1       cgd out:
    311        1.1       cgd 	if (pendingsigs)
    312        1.1       cgd 		dotrap();
    313       1.93      tron 	if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest))
    314        1.1       cgd 		exitshell(exitstatus);
    315        1.1       cgd }
    316        1.1       cgd 
    317        1.1       cgd 
    318        1.1       cgd STATIC void
    319       1.68  christos evalloop(union node *n, int flags)
    320       1.22  christos {
    321        1.1       cgd 	int status;
    322        1.1       cgd 
    323        1.1       cgd 	loopnest++;
    324        1.1       cgd 	status = 0;
    325        1.1       cgd 	for (;;) {
    326       1.65  christos 		evaltree(n->nbinary.ch1, EV_TESTED);
    327        1.1       cgd 		if (evalskip) {
    328        1.1       cgd skipping:	  if (evalskip == SKIPCONT && --skipcount <= 0) {
    329        1.1       cgd 				evalskip = 0;
    330        1.1       cgd 				continue;
    331        1.1       cgd 			}
    332        1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    333        1.1       cgd 				evalskip = 0;
    334        1.1       cgd 			break;
    335        1.1       cgd 		}
    336        1.1       cgd 		if (n->type == NWHILE) {
    337        1.1       cgd 			if (exitstatus != 0)
    338        1.1       cgd 				break;
    339        1.1       cgd 		} else {
    340        1.1       cgd 			if (exitstatus == 0)
    341        1.1       cgd 				break;
    342        1.1       cgd 		}
    343       1.65  christos 		evaltree(n->nbinary.ch2, flags & EV_TESTED);
    344        1.1       cgd 		status = exitstatus;
    345        1.1       cgd 		if (evalskip)
    346        1.1       cgd 			goto skipping;
    347        1.1       cgd 	}
    348        1.1       cgd 	loopnest--;
    349        1.1       cgd 	exitstatus = status;
    350        1.1       cgd }
    351        1.1       cgd 
    352        1.1       cgd 
    353        1.1       cgd 
    354        1.1       cgd STATIC void
    355       1.68  christos evalfor(union node *n, int flags)
    356       1.22  christos {
    357        1.1       cgd 	struct arglist arglist;
    358        1.1       cgd 	union node *argp;
    359        1.1       cgd 	struct strlist *sp;
    360        1.1       cgd 	struct stackmark smark;
    361       1.68  christos 	int status = 0;
    362        1.1       cgd 
    363        1.1       cgd 	setstackmark(&smark);
    364        1.1       cgd 	arglist.lastp = &arglist.list;
    365        1.1       cgd 	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
    366       1.78       dsl 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    367        1.1       cgd 		if (evalskip)
    368        1.1       cgd 			goto out;
    369        1.1       cgd 	}
    370        1.1       cgd 	*arglist.lastp = NULL;
    371        1.1       cgd 
    372        1.1       cgd 	loopnest++;
    373        1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next) {
    374        1.1       cgd 		setvar(n->nfor.var, sp->text, 0);
    375       1.65  christos 		evaltree(n->nfor.body, flags & EV_TESTED);
    376       1.68  christos 		status = exitstatus;
    377        1.1       cgd 		if (evalskip) {
    378        1.1       cgd 			if (evalskip == SKIPCONT && --skipcount <= 0) {
    379        1.1       cgd 				evalskip = 0;
    380        1.1       cgd 				continue;
    381        1.1       cgd 			}
    382        1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    383        1.1       cgd 				evalskip = 0;
    384        1.1       cgd 			break;
    385        1.1       cgd 		}
    386        1.1       cgd 	}
    387        1.1       cgd 	loopnest--;
    388       1.68  christos 	exitstatus = status;
    389        1.1       cgd out:
    390        1.1       cgd 	popstackmark(&smark);
    391        1.1       cgd }
    392        1.1       cgd 
    393        1.1       cgd 
    394        1.1       cgd 
    395        1.1       cgd STATIC void
    396       1.68  christos evalcase(union node *n, int flags)
    397       1.16       cgd {
    398        1.1       cgd 	union node *cp;
    399        1.1       cgd 	union node *patp;
    400        1.1       cgd 	struct arglist arglist;
    401        1.1       cgd 	struct stackmark smark;
    402       1.68  christos 	int status = 0;
    403        1.1       cgd 
    404        1.1       cgd 	setstackmark(&smark);
    405        1.1       cgd 	arglist.lastp = &arglist.list;
    406        1.7       jtc 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
    407        1.1       cgd 	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
    408        1.1       cgd 		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
    409        1.1       cgd 			if (casematch(patp, arglist.list->text)) {
    410        1.1       cgd 				if (evalskip == 0) {
    411       1.65  christos 					evaltree(cp->nclist.body, flags);
    412       1.68  christos 					status = exitstatus;
    413        1.1       cgd 				}
    414        1.1       cgd 				goto out;
    415        1.1       cgd 			}
    416        1.1       cgd 		}
    417        1.1       cgd 	}
    418        1.1       cgd out:
    419       1.68  christos 	exitstatus = status;
    420        1.1       cgd 	popstackmark(&smark);
    421        1.1       cgd }
    422        1.1       cgd 
    423        1.1       cgd 
    424        1.1       cgd 
    425        1.1       cgd /*
    426        1.1       cgd  * Kick off a subshell to evaluate a tree.
    427        1.1       cgd  */
    428        1.1       cgd 
    429        1.1       cgd STATIC void
    430       1.68  christos evalsubshell(union node *n, int flags)
    431       1.16       cgd {
    432        1.1       cgd 	struct job *jp;
    433        1.1       cgd 	int backgnd = (n->type == NBACKGND);
    434        1.1       cgd 
    435        1.1       cgd 	expredir(n->nredir.redirect);
    436       1.63   mycroft 	INTOFF;
    437        1.1       cgd 	jp = makejob(n, 1);
    438       1.83  christos 	if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
    439       1.63   mycroft 		INTON;
    440        1.1       cgd 		if (backgnd)
    441        1.1       cgd 			flags &=~ EV_TESTED;
    442        1.1       cgd 		redirect(n->nredir.redirect, 0);
    443       1.64  christos 		/* never returns */
    444       1.65  christos 		evaltree(n->nredir.n, flags | EV_EXIT);
    445        1.1       cgd 	}
    446       1.84  christos 	if (! backgnd)
    447        1.1       cgd 		exitstatus = waitforjob(jp);
    448       1.63   mycroft 	INTON;
    449        1.1       cgd }
    450        1.1       cgd 
    451        1.1       cgd 
    452        1.1       cgd 
    453        1.1       cgd /*
    454        1.1       cgd  * Compute the names of the files in a redirection list.
    455        1.1       cgd  */
    456        1.1       cgd 
    457        1.1       cgd STATIC void
    458       1.68  christos expredir(union node *n)
    459       1.22  christos {
    460       1.34       tls 	union node *redir;
    461        1.1       cgd 
    462        1.1       cgd 	for (redir = n ; redir ; redir = redir->nfile.next) {
    463       1.14       jtc 		struct arglist fn;
    464       1.14       jtc 		fn.lastp = &fn.list;
    465       1.14       jtc 		switch (redir->type) {
    466       1.45  christos 		case NFROMTO:
    467       1.14       jtc 		case NFROM:
    468       1.14       jtc 		case NTO:
    469       1.59  christos 		case NCLOBBER:
    470       1.14       jtc 		case NAPPEND:
    471        1.7       jtc 			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
    472        1.1       cgd 			redir->nfile.expfname = fn.list->text;
    473       1.14       jtc 			break;
    474       1.14       jtc 		case NFROMFD:
    475       1.14       jtc 		case NTOFD:
    476       1.14       jtc 			if (redir->ndup.vname) {
    477       1.14       jtc 				expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
    478       1.14       jtc 				fixredir(redir, fn.list->text, 1);
    479       1.14       jtc 			}
    480       1.14       jtc 			break;
    481        1.1       cgd 		}
    482        1.1       cgd 	}
    483        1.1       cgd }
    484        1.1       cgd 
    485        1.1       cgd 
    486        1.1       cgd 
    487        1.1       cgd /*
    488        1.1       cgd  * Evaluate a pipeline.  All the processes in the pipeline are children
    489        1.1       cgd  * of the process creating the pipeline.  (This differs from some versions
    490        1.1       cgd  * of the shell, which make the last process in a pipeline the parent
    491        1.1       cgd  * of all the rest.)
    492        1.1       cgd  */
    493        1.1       cgd 
    494        1.1       cgd STATIC void
    495       1.68  christos evalpipe(union node *n)
    496       1.22  christos {
    497        1.1       cgd 	struct job *jp;
    498        1.1       cgd 	struct nodelist *lp;
    499        1.1       cgd 	int pipelen;
    500        1.1       cgd 	int prevfd;
    501        1.1       cgd 	int pip[2];
    502        1.1       cgd 
    503       1.18       cgd 	TRACE(("evalpipe(0x%lx) called\n", (long)n));
    504        1.1       cgd 	pipelen = 0;
    505        1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
    506        1.1       cgd 		pipelen++;
    507        1.1       cgd 	INTOFF;
    508        1.1       cgd 	jp = makejob(n, pipelen);
    509        1.1       cgd 	prevfd = -1;
    510        1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    511        1.1       cgd 		prehash(lp->n);
    512        1.1       cgd 		pip[1] = -1;
    513        1.1       cgd 		if (lp->next) {
    514       1.76       dsl 			if (sh_pipe(pip) < 0) {
    515       1.87  christos 				if (prevfd >= 0)
    516       1.87  christos 					close(prevfd);
    517        1.1       cgd 				error("Pipe call failed");
    518        1.1       cgd 			}
    519        1.1       cgd 		}
    520       1.83  christos 		if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
    521        1.1       cgd 			INTON;
    522        1.1       cgd 			if (prevfd > 0) {
    523        1.1       cgd 				close(0);
    524      1.101     pooka 				copyfd(prevfd, 0, 1);
    525        1.1       cgd 				close(prevfd);
    526        1.1       cgd 			}
    527        1.1       cgd 			if (pip[1] >= 0) {
    528        1.1       cgd 				close(pip[0]);
    529        1.1       cgd 				if (pip[1] != 1) {
    530        1.1       cgd 					close(1);
    531      1.101     pooka 					copyfd(pip[1], 1, 1);
    532        1.1       cgd 					close(pip[1]);
    533        1.1       cgd 				}
    534        1.1       cgd 			}
    535       1.65  christos 			evaltree(lp->n, EV_EXIT);
    536        1.1       cgd 		}
    537        1.1       cgd 		if (prevfd >= 0)
    538        1.1       cgd 			close(prevfd);
    539        1.1       cgd 		prevfd = pip[0];
    540        1.1       cgd 		close(pip[1]);
    541        1.1       cgd 	}
    542        1.1       cgd 	if (n->npipe.backgnd == 0) {
    543        1.1       cgd 		exitstatus = waitforjob(jp);
    544        1.1       cgd 		TRACE(("evalpipe:  job done exit status %d\n", exitstatus));
    545        1.1       cgd 	}
    546       1.60   mycroft 	INTON;
    547        1.1       cgd }
    548        1.1       cgd 
    549        1.1       cgd 
    550        1.1       cgd 
    551        1.1       cgd /*
    552        1.1       cgd  * Execute a command inside back quotes.  If it's a builtin command, we
    553        1.1       cgd  * want to save its output in a block obtained from malloc.  Otherwise
    554        1.1       cgd  * we fork off a subprocess and get the output of the command via a pipe.
    555        1.1       cgd  * Should be called with interrupts off.
    556        1.1       cgd  */
    557        1.1       cgd 
    558        1.1       cgd void
    559       1.68  christos evalbackcmd(union node *n, struct backcmd *result)
    560       1.22  christos {
    561        1.1       cgd 	int pip[2];
    562        1.1       cgd 	struct job *jp;
    563        1.1       cgd 	struct stackmark smark;		/* unnecessary */
    564        1.1       cgd 
    565        1.1       cgd 	setstackmark(&smark);
    566        1.1       cgd 	result->fd = -1;
    567        1.1       cgd 	result->buf = NULL;
    568        1.1       cgd 	result->nleft = 0;
    569        1.1       cgd 	result->jp = NULL;
    570       1.23  christos 	if (n == NULL) {
    571        1.7       jtc 		goto out;
    572       1.23  christos 	}
    573       1.46  christos #ifdef notyet
    574       1.46  christos 	/*
    575       1.46  christos 	 * For now we disable executing builtins in the same
    576       1.46  christos 	 * context as the shell, because we are not keeping
    577       1.46  christos 	 * enough state to recover from changes that are
    578       1.46  christos 	 * supposed only to affect subshells. eg. echo "`cd /`"
    579       1.46  christos 	 */
    580        1.7       jtc 	if (n->type == NCMD) {
    581       1.23  christos 		exitstatus = oexitstatus;
    582       1.65  christos 		evalcommand(n, EV_BACKCMD, result);
    583       1.46  christos 	} else
    584       1.46  christos #endif
    585       1.46  christos 	{
    586       1.63   mycroft 		INTOFF;
    587       1.76       dsl 		if (sh_pipe(pip) < 0)
    588        1.1       cgd 			error("Pipe call failed");
    589        1.1       cgd 		jp = makejob(n, 1);
    590       1.65  christos 		if (forkshell(jp, n, FORK_NOJOB) == 0) {
    591        1.1       cgd 			FORCEINTON;
    592        1.1       cgd 			close(pip[0]);
    593        1.1       cgd 			if (pip[1] != 1) {
    594        1.1       cgd 				close(1);
    595      1.101     pooka 				copyfd(pip[1], 1, 1);
    596        1.1       cgd 				close(pip[1]);
    597        1.1       cgd 			}
    598       1.50  christos 			eflag = 0;
    599       1.65  christos 			evaltree(n, EV_EXIT);
    600       1.68  christos 			/* NOTREACHED */
    601        1.1       cgd 		}
    602        1.1       cgd 		close(pip[1]);
    603        1.1       cgd 		result->fd = pip[0];
    604        1.1       cgd 		result->jp = jp;
    605       1.63   mycroft 		INTON;
    606        1.1       cgd 	}
    607        1.7       jtc out:
    608        1.1       cgd 	popstackmark(&smark);
    609        1.1       cgd 	TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
    610        1.1       cgd 		result->fd, result->buf, result->nleft, result->jp));
    611        1.1       cgd }
    612        1.1       cgd 
    613       1.70       dsl static const char *
    614       1.70       dsl syspath(void)
    615       1.70       dsl {
    616       1.70       dsl 	static char *sys_path = NULL;
    617       1.70       dsl 	static int mib[] = {CTL_USER, USER_CS_PATH};
    618       1.80  christos 	static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
    619       1.72       agc 	size_t len;
    620       1.70       dsl 
    621       1.70       dsl 	if (sys_path == NULL) {
    622       1.70       dsl 		if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
    623       1.70       dsl 		    (sys_path = ckmalloc(len + 5)) != NULL &&
    624       1.70       dsl 		    sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
    625       1.70       dsl 			memcpy(sys_path, "PATH=", 5);
    626       1.70       dsl 		} else {
    627       1.70       dsl 			ckfree(sys_path);
    628       1.70       dsl 			/* something to keep things happy */
    629       1.80  christos 			sys_path = def_path;
    630       1.70       dsl 		}
    631       1.70       dsl 	}
    632       1.70       dsl 	return sys_path;
    633       1.70       dsl }
    634       1.70       dsl 
    635       1.70       dsl static int
    636       1.70       dsl parse_command_args(int argc, char **argv, int *use_syspath)
    637       1.70       dsl {
    638       1.70       dsl 	int sv_argc = argc;
    639       1.70       dsl 	char *cp, c;
    640       1.70       dsl 
    641       1.70       dsl 	*use_syspath = 0;
    642       1.70       dsl 
    643       1.70       dsl 	for (;;) {
    644       1.70       dsl 		argv++;
    645       1.70       dsl 		if (--argc == 0)
    646       1.70       dsl 			break;
    647       1.70       dsl 		cp = *argv;
    648       1.70       dsl 		if (*cp++ != '-')
    649       1.70       dsl 			break;
    650       1.70       dsl 		if (*cp == '-' && cp[1] == 0) {
    651       1.70       dsl 			argv++;
    652       1.70       dsl 			argc--;
    653       1.70       dsl 			break;
    654       1.70       dsl 		}
    655       1.70       dsl 		while ((c = *cp++)) {
    656       1.70       dsl 			switch (c) {
    657       1.70       dsl 			case 'p':
    658       1.70       dsl 				*use_syspath = 1;
    659       1.70       dsl 				break;
    660       1.70       dsl 			default:
    661       1.70       dsl 				/* run 'typecmd' for other options */
    662       1.70       dsl 				return 0;
    663       1.70       dsl 			}
    664       1.70       dsl 		}
    665       1.70       dsl 	}
    666       1.70       dsl 	return sv_argc - argc;
    667       1.70       dsl }
    668        1.1       cgd 
    669       1.61  christos int vforked = 0;
    670       1.97  christos extern char *trap[];
    671        1.1       cgd 
    672        1.1       cgd /*
    673        1.1       cgd  * Execute a simple command.
    674        1.1       cgd  */
    675        1.1       cgd 
    676        1.1       cgd STATIC void
    677       1.88  christos evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
    678       1.16       cgd {
    679        1.1       cgd 	struct stackmark smark;
    680        1.1       cgd 	union node *argp;
    681        1.1       cgd 	struct arglist arglist;
    682        1.1       cgd 	struct arglist varlist;
    683       1.88  christos 	volatile int flags = flgs;
    684       1.88  christos 	char ** volatile argv;
    685       1.88  christos 	volatile int argc;
    686        1.1       cgd 	char **envp;
    687        1.1       cgd 	int varflag;
    688        1.1       cgd 	struct strlist *sp;
    689       1.88  christos 	volatile int mode;
    690        1.1       cgd 	int pip[2];
    691        1.1       cgd 	struct cmdentry cmdentry;
    692       1.88  christos 	struct job * volatile jp;
    693        1.1       cgd 	struct jmploc jmploc;
    694       1.85  christos 	struct jmploc *volatile savehandler = NULL;
    695       1.89      matt 	const char *volatile savecmdname;
    696        1.1       cgd 	volatile struct shparam saveparam;
    697        1.1       cgd 	struct localvar *volatile savelocalvars;
    698        1.1       cgd 	volatile int e;
    699       1.88  christos 	char * volatile lastarg;
    700       1.88  christos 	const char * volatile path = pathval();
    701       1.82     lukem 	volatile int temp_path;
    702        1.1       cgd 
    703       1.61  christos 	vforked = 0;
    704        1.1       cgd 	/* First expand the arguments. */
    705       1.65  christos 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
    706        1.1       cgd 	setstackmark(&smark);
    707       1.68  christos 	back_exitstatus = 0;
    708       1.68  christos 
    709        1.1       cgd 	arglist.lastp = &arglist.list;
    710        1.1       cgd 	varflag = 1;
    711       1.78       dsl 	/* Expand arguments, ignoring the initial 'name=value' ones */
    712        1.1       cgd 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    713       1.20  christos 		char *p = argp->narg.text;
    714        1.1       cgd 		if (varflag && is_name(*p)) {
    715        1.1       cgd 			do {
    716        1.1       cgd 				p++;
    717        1.1       cgd 			} while (is_in_name(*p));
    718       1.68  christos 			if (*p == '=')
    719        1.1       cgd 				continue;
    720        1.1       cgd 		}
    721        1.7       jtc 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    722        1.1       cgd 		varflag = 0;
    723        1.1       cgd 	}
    724        1.1       cgd 	*arglist.lastp = NULL;
    725       1.68  christos 
    726       1.68  christos 	expredir(cmd->ncmd.redirect);
    727       1.68  christos 
    728       1.78       dsl 	/* Now do the initial 'name=value' ones we skipped above */
    729       1.68  christos 	varlist.lastp = &varlist.list;
    730       1.68  christos 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    731       1.68  christos 		char *p = argp->narg.text;
    732       1.68  christos 		if (!is_name(*p))
    733       1.68  christos 			break;
    734       1.68  christos 		do
    735       1.68  christos 			p++;
    736       1.68  christos 		while (is_in_name(*p));
    737       1.68  christos 		if (*p != '=')
    738       1.68  christos 			break;
    739       1.68  christos 		expandarg(argp, &varlist, EXP_VARTILDE);
    740       1.68  christos 	}
    741        1.1       cgd 	*varlist.lastp = NULL;
    742       1.68  christos 
    743        1.1       cgd 	argc = 0;
    744        1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next)
    745        1.1       cgd 		argc++;
    746        1.1       cgd 	argv = stalloc(sizeof (char *) * (argc + 1));
    747        1.7       jtc 
    748        1.7       jtc 	for (sp = arglist.list ; sp ; sp = sp->next) {
    749        1.7       jtc 		TRACE(("evalcommand arg: %s\n", sp->text));
    750        1.1       cgd 		*argv++ = sp->text;
    751        1.7       jtc 	}
    752        1.1       cgd 	*argv = NULL;
    753        1.1       cgd 	lastarg = NULL;
    754        1.1       cgd 	if (iflag && funcnest == 0 && argc > 0)
    755        1.1       cgd 		lastarg = argv[-1];
    756        1.1       cgd 	argv -= argc;
    757        1.1       cgd 
    758        1.1       cgd 	/* Print the command if xflag is set. */
    759        1.1       cgd 	if (xflag) {
    760       1.70       dsl 		char sep = 0;
    761       1.70       dsl 		out2str(ps4val());
    762        1.1       cgd 		for (sp = varlist.list ; sp ; sp = sp->next) {
    763       1.70       dsl 			if (sep != 0)
    764       1.70       dsl 				outc(sep, &errout);
    765       1.94  christos 			out2shstr(sp->text);
    766       1.70       dsl 			sep = ' ';
    767        1.1       cgd 		}
    768        1.1       cgd 		for (sp = arglist.list ; sp ; sp = sp->next) {
    769       1.70       dsl 			if (sep != 0)
    770       1.70       dsl 				outc(sep, &errout);
    771       1.94  christos 			out2shstr(sp->text);
    772       1.70       dsl 			sep = ' ';
    773        1.1       cgd 		}
    774        1.1       cgd 		outc('\n', &errout);
    775        1.1       cgd 		flushout(&errout);
    776        1.1       cgd 	}
    777        1.1       cgd 
    778        1.1       cgd 	/* Now locate the command. */
    779        1.1       cgd 	if (argc == 0) {
    780       1.70       dsl 		cmdentry.cmdtype = CMDSPLBLTIN;
    781       1.68  christos 		cmdentry.u.bltin = bltincmd;
    782        1.1       cgd 	} else {
    783       1.26  christos 		static const char PATH[] = "PATH=";
    784       1.70       dsl 		int cmd_flags = DO_ERR;
    785       1.26  christos 
    786       1.26  christos 		/*
    787       1.26  christos 		 * Modify the command lookup path, if a PATH= assignment
    788       1.26  christos 		 * is present
    789       1.26  christos 		 */
    790       1.70       dsl 		for (sp = varlist.list; sp; sp = sp->next)
    791       1.26  christos 			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
    792       1.26  christos 				path = sp->text + sizeof(PATH) - 1;
    793       1.26  christos 
    794       1.70       dsl 		do {
    795       1.70       dsl 			int argsused, use_syspath;
    796       1.70       dsl 			find_command(argv[0], &cmdentry, cmd_flags, path);
    797       1.70       dsl 			if (cmdentry.cmdtype == CMDUNKNOWN) {
    798       1.68  christos 				exitstatus = 127;
    799       1.68  christos 				flushout(&errout);
    800       1.70       dsl 				goto out;
    801        1.1       cgd 			}
    802       1.70       dsl 
    803       1.70       dsl 			/* implement the 'command' builtin here */
    804       1.70       dsl 			if (cmdentry.cmdtype != CMDBUILTIN ||
    805       1.70       dsl 			    cmdentry.u.bltin != bltincmd)
    806       1.70       dsl 				break;
    807       1.70       dsl 			cmd_flags |= DO_NOFUNC;
    808       1.70       dsl 			argsused = parse_command_args(argc, argv, &use_syspath);
    809       1.70       dsl 			if (argsused == 0) {
    810       1.70       dsl 				/* use 'type' builting to display info */
    811       1.70       dsl 				cmdentry.u.bltin = typecmd;
    812       1.70       dsl 				break;
    813       1.70       dsl 			}
    814       1.70       dsl 			argc -= argsused;
    815       1.70       dsl 			argv += argsused;
    816       1.70       dsl 			if (use_syspath)
    817       1.70       dsl 				path = syspath() + 5;
    818       1.70       dsl 		} while (argc != 0);
    819       1.70       dsl 		if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
    820       1.70       dsl 			/* posix mandates that 'command <splbltin>' act as if
    821       1.70       dsl 			   <splbltin> was a normal builtin */
    822       1.70       dsl 			cmdentry.cmdtype = CMDBUILTIN;
    823        1.1       cgd 	}
    824        1.1       cgd 
    825        1.1       cgd 	/* Fork off a child process if necessary. */
    826       1.98  christos 	if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0)
    827       1.96  christos 	 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
    828       1.21  christos 	 || ((flags & EV_BACKCMD) != 0
    829       1.68  christos 	    && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
    830       1.68  christos 		 || cmdentry.u.bltin == dotcmd
    831       1.68  christos 		 || cmdentry.u.bltin == evalcmd))) {
    832       1.62  christos 		INTOFF;
    833        1.1       cgd 		jp = makejob(cmd, 1);
    834        1.1       cgd 		mode = cmd->ncmd.backgnd;
    835        1.1       cgd 		if (flags & EV_BACKCMD) {
    836        1.1       cgd 			mode = FORK_NOJOB;
    837       1.76       dsl 			if (sh_pipe(pip) < 0)
    838        1.1       cgd 				error("Pipe call failed");
    839        1.1       cgd 		}
    840       1.61  christos #ifdef DO_SHAREDVFORK
    841       1.61  christos 		/* It is essential that if DO_SHAREDVFORK is defined that the
    842       1.61  christos 		 * child's address space is actually shared with the parent as
    843       1.61  christos 		 * we rely on this.
    844       1.61  christos 		 */
    845       1.61  christos 		if (cmdentry.cmdtype == CMDNORMAL) {
    846       1.61  christos 			pid_t	pid;
    847       1.61  christos 
    848       1.61  christos 			savelocalvars = localvars;
    849       1.61  christos 			localvars = NULL;
    850       1.61  christos 			vforked = 1;
    851       1.61  christos 			switch (pid = vfork()) {
    852       1.61  christos 			case -1:
    853       1.75       dsl 				TRACE(("Vfork failed, errno=%d\n", errno));
    854       1.61  christos 				INTON;
    855       1.61  christos 				error("Cannot vfork");
    856       1.61  christos 				break;
    857       1.61  christos 			case 0:
    858       1.61  christos 				/* Make sure that exceptions only unwind to
    859       1.61  christos 				 * after the vfork(2)
    860       1.61  christos 				 */
    861       1.61  christos 				if (setjmp(jmploc.loc)) {
    862       1.61  christos 					if (exception == EXSHELLPROC) {
    863       1.61  christos 						/* We can't progress with the vfork,
    864       1.61  christos 						 * so, set vforked = 2 so the parent
    865       1.61  christos 						 * knows, and _exit();
    866       1.61  christos 						 */
    867       1.61  christos 						vforked = 2;
    868       1.61  christos 						_exit(0);
    869       1.61  christos 					} else {
    870       1.61  christos 						_exit(exerrno);
    871       1.61  christos 					}
    872       1.61  christos 				}
    873       1.61  christos 				savehandler = handler;
    874       1.61  christos 				handler = &jmploc;
    875       1.70       dsl 				listmklocal(varlist.list, VEXPORT | VNOFUNC);
    876       1.65  christos 				forkchild(jp, cmd, mode, vforked);
    877       1.61  christos 				break;
    878       1.61  christos 			default:
    879       1.61  christos 				handler = savehandler;	/* restore from vfork(2) */
    880       1.61  christos 				poplocalvars();
    881       1.61  christos 				localvars = savelocalvars;
    882       1.61  christos 				if (vforked == 2) {
    883       1.61  christos 					vforked = 0;
    884       1.61  christos 
    885       1.61  christos 					(void)waitpid(pid, NULL, 0);
    886       1.61  christos 					/* We need to progress in a normal fork fashion */
    887       1.61  christos 					goto normal_fork;
    888       1.61  christos 				}
    889       1.61  christos 				vforked = 0;
    890       1.65  christos 				forkparent(jp, cmd, mode, pid);
    891       1.61  christos 				goto parent;
    892       1.61  christos 			}
    893       1.61  christos 		} else {
    894       1.61  christos normal_fork:
    895       1.61  christos #endif
    896       1.65  christos 			if (forkshell(jp, cmd, mode) != 0)
    897       1.61  christos 				goto parent;	/* at end of routine */
    898       1.66  christos 			FORCEINTON;
    899       1.61  christos #ifdef DO_SHAREDVFORK
    900       1.61  christos 		}
    901       1.61  christos #endif
    902        1.1       cgd 		if (flags & EV_BACKCMD) {
    903       1.61  christos 			if (!vforked) {
    904       1.61  christos 				FORCEINTON;
    905       1.61  christos 			}
    906        1.1       cgd 			close(pip[0]);
    907        1.1       cgd 			if (pip[1] != 1) {
    908        1.1       cgd 				close(1);
    909      1.101     pooka 				copyfd(pip[1], 1, 1);
    910        1.1       cgd 				close(pip[1]);
    911        1.1       cgd 			}
    912        1.1       cgd 		}
    913        1.1       cgd 		flags |= EV_EXIT;
    914        1.1       cgd 	}
    915        1.1       cgd 
    916        1.1       cgd 	/* This is the child process if a fork occurred. */
    917        1.1       cgd 	/* Execute the command. */
    918       1.70       dsl 	switch (cmdentry.cmdtype) {
    919       1.70       dsl 	case CMDFUNCTION:
    920       1.31  christos #ifdef DEBUG
    921        1.1       cgd 		trputs("Shell function:  ");  trargs(argv);
    922       1.31  christos #endif
    923        1.1       cgd 		redirect(cmd->ncmd.redirect, REDIR_PUSH);
    924        1.1       cgd 		saveparam = shellparam;
    925        1.1       cgd 		shellparam.malloc = 0;
    926       1.32  christos 		shellparam.reset = 1;
    927        1.1       cgd 		shellparam.nparam = argc - 1;
    928        1.1       cgd 		shellparam.p = argv + 1;
    929        1.1       cgd 		shellparam.optnext = NULL;
    930        1.1       cgd 		INTOFF;
    931        1.1       cgd 		savelocalvars = localvars;
    932        1.1       cgd 		localvars = NULL;
    933        1.1       cgd 		INTON;
    934        1.1       cgd 		if (setjmp(jmploc.loc)) {
    935       1.47  christos 			if (exception == EXSHELLPROC) {
    936       1.47  christos 				freeparam((volatile struct shparam *)
    937       1.47  christos 				    &saveparam);
    938       1.47  christos 			} else {
    939        1.1       cgd 				freeparam(&shellparam);
    940        1.1       cgd 				shellparam = saveparam;
    941        1.1       cgd 			}
    942        1.1       cgd 			poplocalvars();
    943        1.1       cgd 			localvars = savelocalvars;
    944        1.1       cgd 			handler = savehandler;
    945        1.1       cgd 			longjmp(handler->loc, 1);
    946        1.1       cgd 		}
    947        1.1       cgd 		savehandler = handler;
    948        1.1       cgd 		handler = &jmploc;
    949       1.70       dsl 		listmklocal(varlist.list, 0);
    950       1.70       dsl 		/* stop shell blowing its stack */
    951       1.70       dsl 		if (++funcnest > 1000)
    952       1.70       dsl 			error("too many nested function calls");
    953       1.65  christos 		evaltree(cmdentry.u.func, flags & EV_TESTED);
    954        1.1       cgd 		funcnest--;
    955        1.1       cgd 		INTOFF;
    956        1.1       cgd 		poplocalvars();
    957        1.1       cgd 		localvars = savelocalvars;
    958        1.1       cgd 		freeparam(&shellparam);
    959        1.1       cgd 		shellparam = saveparam;
    960        1.1       cgd 		handler = savehandler;
    961        1.1       cgd 		popredir();
    962        1.1       cgd 		INTON;
    963        1.1       cgd 		if (evalskip == SKIPFUNC) {
    964        1.1       cgd 			evalskip = 0;
    965        1.1       cgd 			skipcount = 0;
    966        1.1       cgd 		}
    967        1.1       cgd 		if (flags & EV_EXIT)
    968        1.1       cgd 			exitshell(exitstatus);
    969       1.70       dsl 		break;
    970       1.70       dsl 
    971       1.70       dsl 	case CMDBUILTIN:
    972       1.70       dsl 	case CMDSPLBLTIN:
    973       1.31  christos #ifdef DEBUG
    974        1.1       cgd 		trputs("builtin command:  ");  trargs(argv);
    975       1.31  christos #endif
    976       1.68  christos 		mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
    977        1.1       cgd 		if (flags == EV_BACKCMD) {
    978        1.1       cgd 			memout.nleft = 0;
    979        1.1       cgd 			memout.nextc = memout.buf;
    980        1.1       cgd 			memout.bufsize = 64;
    981        1.1       cgd 			mode |= REDIR_BACKQ;
    982        1.1       cgd 		}
    983       1.70       dsl 		e = -1;
    984       1.68  christos 		savehandler = handler;
    985       1.81       dsl 		savecmdname = commandname;
    986       1.68  christos 		handler = &jmploc;
    987       1.68  christos 		if (!setjmp(jmploc.loc)) {
    988       1.70       dsl 			/* We need to ensure the command hash table isn't
    989       1.70       dsl 			 * corruped by temporary PATH assignments.
    990       1.70       dsl 			 * However we must ensure the 'local' command works!
    991       1.70       dsl 			 */
    992       1.70       dsl 			if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
    993       1.70       dsl 			    cmdentry.u.bltin == typecmd)) {
    994       1.70       dsl 				savelocalvars = localvars;
    995       1.70       dsl 				localvars = 0;
    996       1.70       dsl 				mklocal(path - 5 /* PATH= */, 0);
    997       1.70       dsl 				temp_path = 1;
    998       1.70       dsl 			} else
    999       1.70       dsl 				temp_path = 0;
   1000       1.68  christos 			redirect(cmd->ncmd.redirect, mode);
   1001       1.68  christos 
   1002       1.68  christos 			/* exec is a special builtin, but needs this list... */
   1003       1.68  christos 			cmdenviron = varlist.list;
   1004       1.68  christos 			/* we must check 'readonly' flag for all builtins */
   1005       1.68  christos 			listsetvar(varlist.list,
   1006       1.68  christos 				cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
   1007       1.68  christos 			commandname = argv[0];
   1008       1.68  christos 			/* initialize nextopt */
   1009       1.68  christos 			argptr = argv + 1;
   1010       1.68  christos 			optptr = NULL;
   1011       1.68  christos 			/* and getopt */
   1012       1.68  christos 			optreset = 1;
   1013       1.68  christos 			optind = 1;
   1014  1.102.2.1      yamt 			builtin_flags = flags;
   1015       1.68  christos 			exitstatus = cmdentry.u.bltin(argc, argv);
   1016       1.68  christos 		} else {
   1017        1.1       cgd 			e = exception;
   1018       1.70       dsl 			exitstatus = e == EXINT ? SIGINT + 128 :
   1019       1.68  christos 					e == EXEXEC ? exerrno : 2;
   1020        1.1       cgd 		}
   1021       1.70       dsl 		handler = savehandler;
   1022        1.1       cgd 		flushall();
   1023        1.1       cgd 		out1 = &output;
   1024        1.1       cgd 		out2 = &errout;
   1025        1.1       cgd 		freestdout();
   1026       1.70       dsl 		if (temp_path) {
   1027       1.70       dsl 			poplocalvars();
   1028       1.70       dsl 			localvars = savelocalvars;
   1029       1.70       dsl 		}
   1030       1.39   thorpej 		cmdenviron = NULL;
   1031        1.1       cgd 		if (e != EXSHELLPROC) {
   1032        1.1       cgd 			commandname = savecmdname;
   1033       1.44   mycroft 			if (flags & EV_EXIT)
   1034        1.1       cgd 				exitshell(exitstatus);
   1035        1.1       cgd 		}
   1036        1.1       cgd 		if (e != -1) {
   1037       1.31  christos 			if ((e != EXERROR && e != EXEXEC)
   1038       1.70       dsl 			    || cmdentry.cmdtype == CMDSPLBLTIN)
   1039        1.1       cgd 				exraise(e);
   1040        1.1       cgd 			FORCEINTON;
   1041        1.1       cgd 		}
   1042       1.68  christos 		if (cmdentry.u.bltin != execcmd)
   1043        1.1       cgd 			popredir();
   1044        1.1       cgd 		if (flags == EV_BACKCMD) {
   1045        1.1       cgd 			backcmd->buf = memout.buf;
   1046        1.1       cgd 			backcmd->nleft = memout.nextc - memout.buf;
   1047        1.1       cgd 			memout.buf = NULL;
   1048        1.1       cgd 		}
   1049       1.70       dsl 		break;
   1050       1.70       dsl 
   1051       1.70       dsl 	default:
   1052       1.31  christos #ifdef DEBUG
   1053        1.1       cgd 		trputs("normal command:  ");  trargs(argv);
   1054       1.31  christos #endif
   1055       1.61  christos 		clearredir(vforked);
   1056       1.61  christos 		redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
   1057       1.61  christos 		if (!vforked)
   1058       1.61  christos 			for (sp = varlist.list ; sp ; sp = sp->next)
   1059       1.61  christos 				setvareq(sp->text, VEXPORT|VSTACK);
   1060        1.1       cgd 		envp = environment();
   1061       1.70       dsl 		shellexec(argv, envp, path, cmdentry.u.index, vforked);
   1062       1.70       dsl 		break;
   1063        1.1       cgd 	}
   1064        1.1       cgd 	goto out;
   1065        1.1       cgd 
   1066        1.1       cgd parent:	/* parent process gets here (if we forked) */
   1067       1.68  christos 	if (mode == FORK_FG) {	/* argument to fork */
   1068        1.1       cgd 		exitstatus = waitforjob(jp);
   1069       1.68  christos 	} else if (mode == FORK_NOJOB) {
   1070        1.1       cgd 		backcmd->fd = pip[0];
   1071        1.1       cgd 		close(pip[1]);
   1072        1.1       cgd 		backcmd->jp = jp;
   1073        1.1       cgd 	}
   1074       1.66  christos 	FORCEINTON;
   1075        1.1       cgd 
   1076        1.1       cgd out:
   1077        1.1       cgd 	if (lastarg)
   1078       1.70       dsl 		/* dsl: I think this is intended to be used to support
   1079       1.70       dsl 		 * '_' in 'vi' command mode during line editing...
   1080       1.70       dsl 		 * However I implemented that within libedit itself.
   1081       1.70       dsl 		 */
   1082        1.1       cgd 		setvar("_", lastarg, 0);
   1083        1.1       cgd 	popstackmark(&smark);
   1084        1.1       cgd }
   1085        1.1       cgd 
   1086        1.1       cgd 
   1087        1.1       cgd /*
   1088        1.1       cgd  * Search for a command.  This is called before we fork so that the
   1089        1.1       cgd  * location of the command will be available in the parent as well as
   1090        1.1       cgd  * the child.  The check for "goodname" is an overly conservative
   1091        1.1       cgd  * check that the name will not be subject to expansion.
   1092        1.1       cgd  */
   1093        1.1       cgd 
   1094        1.1       cgd STATIC void
   1095       1.68  christos prehash(union node *n)
   1096       1.22  christos {
   1097        1.1       cgd 	struct cmdentry entry;
   1098        1.1       cgd 
   1099       1.86  christos 	if (n && n->type == NCMD && n->ncmd.args)
   1100       1.15   mycroft 		if (goodname(n->ncmd.args->narg.text))
   1101       1.26  christos 			find_command(n->ncmd.args->narg.text, &entry, 0,
   1102       1.26  christos 				     pathval());
   1103        1.1       cgd }
   1104        1.1       cgd 
   1105        1.1       cgd 
   1106        1.1       cgd 
   1107        1.1       cgd /*
   1108        1.1       cgd  * Builtin commands.  Builtin commands whose functions are closely
   1109        1.1       cgd  * tied to evaluation are implemented here.
   1110        1.1       cgd  */
   1111        1.1       cgd 
   1112        1.1       cgd /*
   1113       1.70       dsl  * No command given.
   1114        1.1       cgd  */
   1115        1.1       cgd 
   1116       1.16       cgd int
   1117       1.68  christos bltincmd(int argc, char **argv)
   1118       1.16       cgd {
   1119       1.31  christos 	/*
   1120       1.22  christos 	 * Preserve exitstatus of a previous possible redirection
   1121       1.31  christos 	 * as POSIX mandates
   1122       1.22  christos 	 */
   1123       1.68  christos 	return back_exitstatus;
   1124        1.1       cgd }
   1125        1.1       cgd 
   1126        1.1       cgd 
   1127        1.1       cgd /*
   1128        1.1       cgd  * Handle break and continue commands.  Break, continue, and return are
   1129        1.1       cgd  * all handled by setting the evalskip flag.  The evaluation routines
   1130        1.1       cgd  * above all check this flag, and if it is set they start skipping
   1131        1.1       cgd  * commands rather than executing them.  The variable skipcount is
   1132        1.1       cgd  * the number of loops to break/continue, or the number of function
   1133        1.1       cgd  * levels to return.  (The latter is always 1.)  It should probably
   1134        1.1       cgd  * be an error to break out of more loops than exist, but it isn't
   1135        1.1       cgd  * in the standard shell so we don't make it one here.
   1136        1.1       cgd  */
   1137        1.1       cgd 
   1138       1.16       cgd int
   1139       1.68  christos breakcmd(int argc, char **argv)
   1140       1.16       cgd {
   1141       1.30  christos 	int n = argc > 1 ? number(argv[1]) : 1;
   1142        1.1       cgd 
   1143        1.1       cgd 	if (n > loopnest)
   1144        1.1       cgd 		n = loopnest;
   1145        1.1       cgd 	if (n > 0) {
   1146        1.1       cgd 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
   1147        1.1       cgd 		skipcount = n;
   1148        1.1       cgd 	}
   1149        1.1       cgd 	return 0;
   1150        1.1       cgd }
   1151        1.1       cgd 
   1152        1.1       cgd 
   1153        1.1       cgd /*
   1154        1.1       cgd  * The return command.
   1155        1.1       cgd  */
   1156        1.1       cgd 
   1157       1.16       cgd int
   1158       1.68  christos returncmd(int argc, char **argv)
   1159       1.16       cgd {
   1160       1.68  christos 	int ret = argc > 1 ? number(argv[1]) : exitstatus;
   1161        1.1       cgd 
   1162        1.1       cgd 	if (funcnest) {
   1163        1.1       cgd 		evalskip = SKIPFUNC;
   1164        1.1       cgd 		skipcount = 1;
   1165       1.27  christos 		return ret;
   1166       1.27  christos 	}
   1167       1.27  christos 	else {
   1168       1.27  christos 		/* Do what ksh does; skip the rest of the file */
   1169       1.27  christos 		evalskip = SKIPFILE;
   1170       1.27  christos 		skipcount = 1;
   1171       1.27  christos 		return ret;
   1172        1.1       cgd 	}
   1173        1.1       cgd }
   1174        1.1       cgd 
   1175        1.1       cgd 
   1176       1.16       cgd int
   1177       1.68  christos falsecmd(int argc, char **argv)
   1178       1.16       cgd {
   1179        1.8       jtc 	return 1;
   1180        1.8       jtc }
   1181        1.8       jtc 
   1182        1.8       jtc 
   1183       1.16       cgd int
   1184       1.68  christos truecmd(int argc, char **argv)
   1185       1.16       cgd {
   1186        1.1       cgd 	return 0;
   1187        1.1       cgd }
   1188        1.1       cgd 
   1189        1.1       cgd 
   1190       1.16       cgd int
   1191       1.68  christos execcmd(int argc, char **argv)
   1192       1.16       cgd {
   1193        1.1       cgd 	if (argc > 1) {
   1194       1.20  christos 		struct strlist *sp;
   1195       1.20  christos 
   1196        1.1       cgd 		iflag = 0;		/* exit on error */
   1197        1.7       jtc 		mflag = 0;
   1198        1.7       jtc 		optschanged();
   1199       1.70       dsl 		for (sp = cmdenviron; sp; sp = sp->next)
   1200       1.20  christos 			setvareq(sp->text, VEXPORT|VSTACK);
   1201       1.61  christos 		shellexec(argv + 1, environment(), pathval(), 0, 0);
   1202        1.1       cgd 	}
   1203       1.68  christos 	return 0;
   1204       1.68  christos }
   1205       1.68  christos 
   1206       1.68  christos static int
   1207       1.73    itojun conv_time(clock_t ticks, char *seconds, size_t l)
   1208       1.68  christos {
   1209       1.68  christos 	static clock_t tpm = 0;
   1210       1.68  christos 	clock_t mins;
   1211       1.68  christos 	int i;
   1212       1.68  christos 
   1213       1.68  christos 	if (!tpm)
   1214       1.68  christos 		tpm = sysconf(_SC_CLK_TCK) * 60;
   1215       1.68  christos 
   1216       1.68  christos 	mins = ticks / tpm;
   1217       1.73    itojun 	snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
   1218       1.68  christos 
   1219       1.68  christos 	if (seconds[0] == '6' && seconds[1] == '0') {
   1220       1.68  christos 		/* 59.99995 got rounded up... */
   1221       1.68  christos 		mins++;
   1222       1.73    itojun 		strlcpy(seconds, "0.0", l);
   1223       1.68  christos 		return mins;
   1224       1.68  christos 	}
   1225       1.68  christos 
   1226       1.68  christos 	/* suppress trailing zeros */
   1227       1.68  christos 	i = strlen(seconds) - 1;
   1228       1.68  christos 	for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
   1229       1.68  christos 		seconds[i] = 0;
   1230       1.68  christos 	return mins;
   1231       1.68  christos }
   1232       1.68  christos 
   1233       1.68  christos int
   1234       1.68  christos timescmd(int argc, char **argv)
   1235       1.68  christos {
   1236       1.68  christos 	struct tms tms;
   1237       1.68  christos 	int u, s, cu, cs;
   1238       1.68  christos 	char us[8], ss[8], cus[8], css[8];
   1239       1.68  christos 
   1240       1.68  christos 	nextopt("");
   1241       1.68  christos 
   1242       1.68  christos 	times(&tms);
   1243       1.68  christos 
   1244       1.73    itojun 	u = conv_time(tms.tms_utime, us, sizeof(us));
   1245       1.73    itojun 	s = conv_time(tms.tms_stime, ss, sizeof(ss));
   1246       1.73    itojun 	cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
   1247       1.73    itojun 	cs = conv_time(tms.tms_cstime, css, sizeof(css));
   1248       1.68  christos 
   1249       1.68  christos 	outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
   1250       1.68  christos 		u, us, s, ss, cu, cus, cs, css);
   1251       1.68  christos 
   1252        1.1       cgd 	return 0;
   1253        1.1       cgd }
   1254