Home | History | Annotate | Line # | Download | only in sh
eval.c revision 1.153.2.5
      1  1.153.2.5  pgoyette /*	$NetBSD: eval.c,v 1.153.2.5 2018/11/26 01:49:54 pgoyette 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.153.2.5  pgoyette __RCSID("$NetBSD: eval.c,v 1.153.2.5 2018/11/26 01:49:54 pgoyette 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.116  christos #include <string.h>
     49      1.100  christos #include <errno.h>
     50      1.105       dsl #include <limits.h>
     51       1.21  christos #include <unistd.h>
     52       1.76       dsl #include <sys/fcntl.h>
     53      1.129  christos #include <sys/stat.h>
     54       1.68  christos #include <sys/times.h>
     55       1.71     rafal #include <sys/param.h>
     56       1.61  christos #include <sys/types.h>
     57       1.61  christos #include <sys/wait.h>
     58       1.70       dsl #include <sys/sysctl.h>
     59       1.61  christos 
     60        1.1       cgd /*
     61        1.1       cgd  * Evaluate a command.
     62        1.1       cgd  */
     63        1.1       cgd 
     64        1.1       cgd #include "shell.h"
     65        1.1       cgd #include "nodes.h"
     66        1.1       cgd #include "syntax.h"
     67        1.1       cgd #include "expand.h"
     68        1.1       cgd #include "parser.h"
     69        1.1       cgd #include "jobs.h"
     70        1.1       cgd #include "eval.h"
     71        1.1       cgd #include "builtins.h"
     72        1.1       cgd #include "options.h"
     73        1.1       cgd #include "exec.h"
     74        1.1       cgd #include "redir.h"
     75        1.1       cgd #include "input.h"
     76        1.1       cgd #include "output.h"
     77        1.1       cgd #include "trap.h"
     78        1.1       cgd #include "var.h"
     79        1.1       cgd #include "memalloc.h"
     80        1.1       cgd #include "error.h"
     81       1.21  christos #include "show.h"
     82        1.1       cgd #include "mystring.h"
     83       1.64  christos #include "main.h"
     84       1.35  christos #ifndef SMALL
     85      1.114  christos #include "nodenames.h"
     86        1.7       jtc #include "myhistedit.h"
     87       1.11       cgd #endif
     88        1.1       cgd 
     89        1.1       cgd 
     90      1.109  christos STATIC enum skipstate evalskip;	/* != SKIPNONE if we are skipping commands */
     91        1.1       cgd STATIC int skipcount;		/* number of levels to skip */
     92      1.109  christos STATIC int loopnest;		/* current loop nesting level */
     93      1.109  christos STATIC int funcnest;		/* depth of function calls */
     94      1.103  christos STATIC int builtin_flags;	/* evalcommand flags for builtins */
     95      1.109  christos /*
     96      1.109  christos  * Base function nesting level inside a dot command.  Set to 0 initially
     97      1.109  christos  * and to (funcnest + 1) before every dot command to enable
     98      1.109  christos  *   1) detection of being in a file sourced by a dot command and
     99      1.109  christos  *   2) counting of function nesting in that file for the implementation
    100      1.109  christos  *      of the return command.
    101      1.109  christos  * The value is reset to its previous value after the dot command.
    102      1.109  christos  */
    103      1.109  christos STATIC int dot_funcnest;
    104        1.1       cgd 
    105        1.1       cgd 
    106       1.89      matt const char *commandname;
    107        1.1       cgd struct strlist *cmdenviron;
    108        1.1       cgd int exitstatus;			/* exit status of last command */
    109       1.68  christos int back_exitstatus;		/* exit status of backquoted command */
    110        1.1       cgd 
    111        1.1       cgd 
    112       1.68  christos STATIC void evalloop(union node *, int);
    113       1.68  christos STATIC void evalfor(union node *, int);
    114       1.68  christos STATIC void evalcase(union node *, int);
    115       1.68  christos STATIC void evalsubshell(union node *, int);
    116       1.68  christos STATIC void expredir(union node *);
    117  1.153.2.3  pgoyette STATIC void evalredir(union node *, int);
    118       1.68  christos STATIC void evalpipe(union node *);
    119       1.68  christos STATIC void evalcommand(union node *, int, struct backcmd *);
    120       1.68  christos STATIC void prehash(union node *);
    121        1.1       cgd 
    122      1.109  christos STATIC char *find_dot_file(char *);
    123        1.1       cgd 
    124        1.1       cgd /*
    125        1.1       cgd  * Called to reset things after an exception.
    126        1.1       cgd  */
    127        1.1       cgd 
    128        1.1       cgd #ifdef mkinit
    129        1.1       cgd INCLUDE "eval.h"
    130        1.1       cgd 
    131        1.1       cgd RESET {
    132      1.109  christos 	reset_eval();
    133        1.1       cgd }
    134        1.1       cgd 
    135        1.1       cgd SHELLPROC {
    136        1.1       cgd 	exitstatus = 0;
    137        1.1       cgd }
    138        1.1       cgd #endif
    139        1.1       cgd 
    140      1.109  christos void
    141      1.109  christos reset_eval(void)
    142      1.109  christos {
    143      1.109  christos 	evalskip = SKIPNONE;
    144      1.109  christos 	dot_funcnest = 0;
    145      1.109  christos 	loopnest = 0;
    146      1.109  christos 	funcnest = 0;
    147      1.109  christos }
    148      1.109  christos 
    149       1.76       dsl static int
    150       1.76       dsl sh_pipe(int fds[2])
    151       1.76       dsl {
    152       1.76       dsl 	int nfd;
    153       1.76       dsl 
    154       1.76       dsl 	if (pipe(fds))
    155       1.76       dsl 		return -1;
    156       1.76       dsl 
    157       1.76       dsl 	if (fds[0] < 3) {
    158       1.76       dsl 		nfd = fcntl(fds[0], F_DUPFD, 3);
    159       1.76       dsl 		if (nfd != -1) {
    160       1.76       dsl 			close(fds[0]);
    161       1.76       dsl 			fds[0] = nfd;
    162       1.76       dsl 		}
    163       1.76       dsl 	}
    164       1.76       dsl 
    165       1.76       dsl 	if (fds[1] < 3) {
    166       1.76       dsl 		nfd = fcntl(fds[1], F_DUPFD, 3);
    167       1.76       dsl 		if (nfd != -1) {
    168       1.76       dsl 			close(fds[1]);
    169       1.76       dsl 			fds[1] = nfd;
    170       1.76       dsl 		}
    171       1.76       dsl 	}
    172       1.76       dsl 	return 0;
    173       1.76       dsl }
    174        1.1       cgd 
    175        1.1       cgd 
    176        1.1       cgd /*
    177        1.1       cgd  * The eval commmand.
    178        1.1       cgd  */
    179        1.1       cgd 
    180       1.16       cgd int
    181       1.68  christos evalcmd(int argc, char **argv)
    182        1.1       cgd {
    183      1.148       kre 	char *p;
    184      1.148       kre 	char *concat;
    185      1.148       kre 	char **ap;
    186      1.148       kre 
    187      1.148       kre 	if (argc > 1) {
    188      1.148       kre 		p = argv[1];
    189      1.148       kre 		if (argc > 2) {
    190      1.148       kre 			STARTSTACKSTR(concat);
    191      1.148       kre 			ap = argv + 2;
    192      1.148       kre 			for (;;) {
    193      1.148       kre 				while (*p)
    194      1.148       kre 					STPUTC(*p++, concat);
    195      1.148       kre 				if ((p = *ap++) == NULL)
    196      1.148       kre 					break;
    197      1.148       kre 				STPUTC(' ', concat);
    198      1.148       kre 			}
    199      1.148       kre 			STPUTC('\0', concat);
    200      1.148       kre 			p = grabstackstr(concat);
    201      1.148       kre 		}
    202      1.148       kre 		evalstring(p, builtin_flags & EV_TESTED);
    203      1.148       kre 	} else
    204      1.133       kre 		exitstatus = 0;
    205      1.148       kre 	return exitstatus;
    206        1.1       cgd }
    207        1.1       cgd 
    208        1.1       cgd 
    209        1.1       cgd /*
    210        1.1       cgd  * Execute a command or commands contained in a string.
    211        1.1       cgd  */
    212        1.1       cgd 
    213        1.1       cgd void
    214       1.68  christos evalstring(char *s, int flag)
    215       1.68  christos {
    216        1.1       cgd 	union node *n;
    217        1.1       cgd 	struct stackmark smark;
    218  1.153.2.3  pgoyette 	int last;
    219  1.153.2.3  pgoyette 	int any;
    220  1.153.2.3  pgoyette 
    221  1.153.2.3  pgoyette 	last = flag & EV_EXIT;
    222  1.153.2.3  pgoyette 	flag &= ~EV_EXIT;
    223        1.1       cgd 
    224        1.1       cgd 	setstackmark(&smark);
    225      1.143       kre 	setinputstring(s, 1, line_number);
    226       1.64  christos 
    227  1.153.2.3  pgoyette 	any = 0;	/* to determine if exitstatus will have been set */
    228        1.1       cgd 	while ((n = parsecmd(0)) != NEOF) {
    229      1.148       kre 		XTRACE(DBG_EVAL, ("evalstring: "), showtree(n));
    230  1.153.2.3  pgoyette 		if (n && nflag == 0) {
    231  1.153.2.3  pgoyette 			if (last && at_eof())
    232  1.153.2.3  pgoyette 				evaltree(n, flag | EV_EXIT);
    233  1.153.2.3  pgoyette 			else
    234  1.153.2.3  pgoyette 				evaltree(n, flag);
    235  1.153.2.3  pgoyette 			any = 1;
    236  1.153.2.5  pgoyette 			if (evalskip)
    237  1.153.2.5  pgoyette 				break;
    238  1.153.2.3  pgoyette 		}
    239  1.153.2.3  pgoyette 		rststackmark(&smark);
    240        1.1       cgd 	}
    241        1.1       cgd 	popfile();
    242        1.1       cgd 	popstackmark(&smark);
    243  1.153.2.3  pgoyette 	if (!any)
    244  1.153.2.3  pgoyette 		exitstatus = 0;
    245  1.153.2.3  pgoyette 	if (last)
    246  1.153.2.3  pgoyette 		exraise(EXEXIT);
    247        1.1       cgd }
    248        1.1       cgd 
    249        1.1       cgd 
    250        1.1       cgd 
    251        1.1       cgd /*
    252        1.1       cgd  * Evaluate a parse tree.  The value is left in the global variable
    253        1.1       cgd  * exitstatus.
    254        1.1       cgd  */
    255        1.1       cgd 
    256        1.1       cgd void
    257       1.68  christos evaltree(union node *n, int flags)
    258       1.17       cgd {
    259       1.90      tron 	bool do_etest;
    260      1.136       kre 	int sflags = flags & ~EV_EXIT;
    261  1.153.2.3  pgoyette 	union node *next;
    262  1.153.2.3  pgoyette 	struct stackmark smark;
    263       1.90      tron 
    264       1.90      tron 	do_etest = false;
    265      1.115  christos 	if (n == NULL || nflag) {
    266      1.148       kre 		VTRACE(DBG_EVAL, ("evaltree(%s) called\n",
    267      1.148       kre 		    n == NULL ? "NULL" : "-n"));
    268      1.115  christos 		if (nflag == 0)
    269      1.115  christos 			exitstatus = 0;
    270  1.153.2.3  pgoyette 		goto out2;
    271        1.1       cgd 	}
    272  1.153.2.3  pgoyette 
    273  1.153.2.3  pgoyette 	setstackmark(&smark);
    274  1.153.2.3  pgoyette 	do {
    275       1.35  christos #ifndef SMALL
    276  1.153.2.3  pgoyette 		displayhist = 1; /* show history substitutions done with fc */
    277       1.10       cgd #endif
    278  1.153.2.3  pgoyette 		next = NULL;
    279  1.153.2.3  pgoyette 		CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n",
    280  1.153.2.3  pgoyette 		    getpid(), n, NODETYPENAME(n->type), n->type, flags));
    281  1.153.2.3  pgoyette 		switch (n->type) {
    282  1.153.2.3  pgoyette 		case NSEMI:
    283  1.153.2.3  pgoyette 			evaltree(n->nbinary.ch1, sflags);
    284  1.153.2.3  pgoyette 			if (nflag || evalskip)
    285  1.153.2.3  pgoyette 				goto out1;
    286  1.153.2.3  pgoyette 			next = n->nbinary.ch2;
    287  1.153.2.3  pgoyette 			break;
    288  1.153.2.3  pgoyette 		case NAND:
    289  1.153.2.3  pgoyette 			evaltree(n->nbinary.ch1, EV_TESTED);
    290  1.153.2.3  pgoyette 			if (nflag || evalskip || exitstatus != 0)
    291  1.153.2.3  pgoyette 				goto out1;
    292  1.153.2.3  pgoyette 			next = n->nbinary.ch2;
    293  1.153.2.3  pgoyette 			break;
    294  1.153.2.3  pgoyette 		case NOR:
    295  1.153.2.3  pgoyette 			evaltree(n->nbinary.ch1, EV_TESTED);
    296  1.153.2.3  pgoyette 			if (nflag || evalskip || exitstatus == 0)
    297  1.153.2.3  pgoyette 				goto out1;
    298  1.153.2.3  pgoyette 			next = n->nbinary.ch2;
    299  1.153.2.3  pgoyette 			break;
    300  1.153.2.3  pgoyette 		case NREDIR:
    301  1.153.2.3  pgoyette 			evalredir(n, flags);
    302  1.153.2.3  pgoyette 			break;
    303  1.153.2.3  pgoyette 		case NSUBSHELL:
    304  1.153.2.3  pgoyette 			evalsubshell(n, flags);
    305  1.153.2.3  pgoyette 			do_etest = !(flags & EV_TESTED);
    306  1.153.2.3  pgoyette 			break;
    307  1.153.2.3  pgoyette 		case NBACKGND:
    308  1.153.2.3  pgoyette 			evalsubshell(n, flags);
    309  1.153.2.3  pgoyette 			break;
    310  1.153.2.3  pgoyette 		case NIF: {
    311  1.153.2.3  pgoyette 			evaltree(n->nif.test, EV_TESTED);
    312  1.153.2.3  pgoyette 			if (nflag || evalskip)
    313  1.153.2.3  pgoyette 				goto out1;
    314  1.153.2.3  pgoyette 			if (exitstatus == 0)
    315  1.153.2.3  pgoyette 				next = n->nif.ifpart;
    316  1.153.2.3  pgoyette 			else if (n->nif.elsepart)
    317  1.153.2.3  pgoyette 				next = n->nif.elsepart;
    318  1.153.2.3  pgoyette 			else
    319  1.153.2.3  pgoyette 				exitstatus = 0;
    320  1.153.2.3  pgoyette 			break;
    321      1.151       kre 		}
    322  1.153.2.3  pgoyette 		case NWHILE:
    323  1.153.2.3  pgoyette 		case NUNTIL:
    324  1.153.2.3  pgoyette 			evalloop(n, sflags);
    325  1.153.2.3  pgoyette 			break;
    326  1.153.2.3  pgoyette 		case NFOR:
    327  1.153.2.3  pgoyette 			evalfor(n, sflags);
    328  1.153.2.3  pgoyette 			break;
    329  1.153.2.3  pgoyette 		case NCASE:
    330  1.153.2.3  pgoyette 			evalcase(n, sflags);
    331  1.153.2.3  pgoyette 			break;
    332  1.153.2.3  pgoyette 		case NDEFUN:
    333  1.153.2.3  pgoyette 			CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n",
    334  1.153.2.3  pgoyette 			    n->narg.text, n->narg.lineno,
    335  1.153.2.3  pgoyette 			    fnline1 ? " LINENO=1" : ""));
    336  1.153.2.3  pgoyette 			defun(n->narg.text, n->narg.next, n->narg.lineno);
    337       1.29        pk 			exitstatus = 0;
    338  1.153.2.3  pgoyette 			break;
    339  1.153.2.3  pgoyette 		case NNOT:
    340  1.153.2.3  pgoyette 			evaltree(n->nnot.com, EV_TESTED);
    341  1.153.2.3  pgoyette 			exitstatus = !exitstatus;
    342  1.153.2.3  pgoyette 			break;
    343  1.153.2.3  pgoyette 		case NDNOT:
    344  1.153.2.3  pgoyette 			evaltree(n->nnot.com, EV_TESTED);
    345  1.153.2.3  pgoyette 			if (exitstatus != 0)
    346  1.153.2.3  pgoyette 				exitstatus = 1;
    347  1.153.2.3  pgoyette 			break;
    348  1.153.2.3  pgoyette 		case NPIPE:
    349  1.153.2.3  pgoyette 			evalpipe(n);
    350  1.153.2.3  pgoyette 			do_etest = !(flags & EV_TESTED);
    351  1.153.2.3  pgoyette 			break;
    352  1.153.2.3  pgoyette 		case NCMD:
    353  1.153.2.3  pgoyette 			evalcommand(n, flags, NULL);
    354  1.153.2.3  pgoyette 			do_etest = !(flags & EV_TESTED);
    355  1.153.2.3  pgoyette 			break;
    356  1.153.2.3  pgoyette 		default:
    357      1.115  christos #ifdef NODETYPENAME
    358  1.153.2.3  pgoyette 			out1fmt("Node type = %d(%s)\n",
    359  1.153.2.3  pgoyette 				n->type, NODETYPENAME(n->type));
    360      1.115  christos #else
    361  1.153.2.3  pgoyette 			out1fmt("Node type = %d\n", n->type);
    362      1.115  christos #endif
    363  1.153.2.3  pgoyette 			flushout(&output);
    364  1.153.2.3  pgoyette 			break;
    365  1.153.2.3  pgoyette 		}
    366  1.153.2.3  pgoyette 		n = next;
    367  1.153.2.3  pgoyette 		rststackmark(&smark);
    368  1.153.2.3  pgoyette 	} while(n != NULL);
    369  1.153.2.3  pgoyette  out1:
    370  1.153.2.3  pgoyette 	popstackmark(&smark);
    371  1.153.2.3  pgoyette  out2:
    372        1.1       cgd 	if (pendingsigs)
    373        1.1       cgd 		dotrap();
    374  1.153.2.3  pgoyette 	if (eflag && exitstatus != 0 && do_etest)
    375        1.1       cgd 		exitshell(exitstatus);
    376  1.153.2.3  pgoyette 	if (flags & EV_EXIT)
    377  1.153.2.3  pgoyette 		exraise(EXEXIT);
    378        1.1       cgd }
    379        1.1       cgd 
    380        1.1       cgd 
    381        1.1       cgd STATIC void
    382       1.68  christos evalloop(union node *n, int flags)
    383       1.22  christos {
    384        1.1       cgd 	int status;
    385        1.1       cgd 
    386        1.1       cgd 	loopnest++;
    387        1.1       cgd 	status = 0;
    388      1.114  christos 
    389      1.148       kre 	CTRACE(DBG_EVAL,  ("evalloop %s:", NODETYPENAME(n->type)));
    390      1.148       kre 	VXTRACE(DBG_EVAL, (" "), showtree(n->nbinary.ch1));
    391      1.148       kre 	VXTRACE(DBG_EVAL, ("evalloop    do: "), showtree(n->nbinary.ch2));
    392      1.148       kre 	VTRACE(DBG_EVAL,  ("evalloop  done\n"));
    393      1.148       kre 	CTRACE(DBG_EVAL,  ("\n"));
    394      1.114  christos 
    395        1.1       cgd 	for (;;) {
    396  1.153.2.3  pgoyette 		evaltree(n->nbinary.ch1, EV_TESTED);
    397      1.115  christos 		if (nflag)
    398      1.115  christos 			break;
    399        1.1       cgd 		if (evalskip) {
    400      1.137       kre  skipping:		if (evalskip == SKIPCONT && --skipcount <= 0) {
    401      1.109  christos 				evalskip = SKIPNONE;
    402        1.1       cgd 				continue;
    403        1.1       cgd 			}
    404        1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    405      1.109  christos 				evalskip = SKIPNONE;
    406        1.1       cgd 			break;
    407        1.1       cgd 		}
    408        1.1       cgd 		if (n->type == NWHILE) {
    409        1.1       cgd 			if (exitstatus != 0)
    410        1.1       cgd 				break;
    411        1.1       cgd 		} else {
    412        1.1       cgd 			if (exitstatus == 0)
    413        1.1       cgd 				break;
    414        1.1       cgd 		}
    415  1.153.2.3  pgoyette 		evaltree(n->nbinary.ch2, flags & EV_TESTED);
    416        1.1       cgd 		status = exitstatus;
    417        1.1       cgd 		if (evalskip)
    418        1.1       cgd 			goto skipping;
    419        1.1       cgd 	}
    420        1.1       cgd 	loopnest--;
    421        1.1       cgd 	exitstatus = status;
    422        1.1       cgd }
    423        1.1       cgd 
    424        1.1       cgd 
    425        1.1       cgd 
    426        1.1       cgd STATIC void
    427       1.68  christos evalfor(union node *n, int flags)
    428       1.22  christos {
    429        1.1       cgd 	struct arglist arglist;
    430        1.1       cgd 	union node *argp;
    431        1.1       cgd 	struct strlist *sp;
    432        1.1       cgd 	struct stackmark smark;
    433      1.115  christos 	int status;
    434      1.115  christos 
    435      1.115  christos 	status = nflag ? exitstatus : 0;
    436        1.1       cgd 
    437        1.1       cgd 	setstackmark(&smark);
    438        1.1       cgd 	arglist.lastp = &arglist.list;
    439        1.1       cgd 	for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
    440       1.78       dsl 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    441        1.1       cgd 		if (evalskip)
    442        1.1       cgd 			goto out;
    443        1.1       cgd 	}
    444        1.1       cgd 	*arglist.lastp = NULL;
    445        1.1       cgd 
    446        1.1       cgd 	loopnest++;
    447        1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next) {
    448      1.131       kre 		if (xflag) {
    449      1.153       kre 			outxstr(expandstr(ps4val(), line_number));
    450      1.153       kre 			outxstr("for ");
    451      1.153       kre 			outxstr(n->nfor.var);
    452      1.153       kre 			outxc('=');
    453      1.153       kre 			outxshstr(sp->text);
    454      1.153       kre 			outxc('\n');
    455      1.153       kre 			flushout(outx);
    456      1.131       kre 		}
    457      1.131       kre 
    458        1.1       cgd 		setvar(n->nfor.var, sp->text, 0);
    459  1.153.2.3  pgoyette 		evaltree(n->nfor.body, flags & EV_TESTED);
    460       1.68  christos 		status = exitstatus;
    461      1.115  christos 		if (nflag)
    462      1.115  christos 			break;
    463        1.1       cgd 		if (evalskip) {
    464        1.1       cgd 			if (evalskip == SKIPCONT && --skipcount <= 0) {
    465      1.109  christos 				evalskip = SKIPNONE;
    466        1.1       cgd 				continue;
    467        1.1       cgd 			}
    468        1.1       cgd 			if (evalskip == SKIPBREAK && --skipcount <= 0)
    469      1.109  christos 				evalskip = SKIPNONE;
    470        1.1       cgd 			break;
    471        1.1       cgd 		}
    472        1.1       cgd 	}
    473        1.1       cgd 	loopnest--;
    474       1.68  christos 	exitstatus = status;
    475      1.148       kre  out:
    476        1.1       cgd 	popstackmark(&smark);
    477        1.1       cgd }
    478        1.1       cgd 
    479        1.1       cgd 
    480        1.1       cgd 
    481        1.1       cgd STATIC void
    482       1.68  christos evalcase(union node *n, int flags)
    483       1.16       cgd {
    484      1.134       kre 	union node *cp, *ncp;
    485        1.1       cgd 	union node *patp;
    486        1.1       cgd 	struct arglist arglist;
    487        1.1       cgd 	struct stackmark smark;
    488       1.68  christos 	int status = 0;
    489        1.1       cgd 
    490        1.1       cgd 	setstackmark(&smark);
    491        1.1       cgd 	arglist.lastp = &arglist.list;
    492      1.143       kre 	line_number = n->ncase.lineno;
    493        1.7       jtc 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
    494      1.134       kre 	for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
    495      1.134       kre 		for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
    496      1.143       kre 			line_number = patp->narg.lineno;
    497        1.1       cgd 			if (casematch(patp, arglist.list->text)) {
    498      1.134       kre 				while (cp != NULL && evalskip == 0 &&
    499      1.134       kre 				    nflag == 0) {
    500      1.134       kre 					if (cp->type == NCLISTCONT)
    501      1.134       kre 						ncp = cp->nclist.next;
    502      1.134       kre 					else
    503      1.134       kre 						ncp = NULL;
    504      1.143       kre 					line_number = cp->nclist.lineno;
    505  1.153.2.3  pgoyette 					evaltree(cp->nclist.body, flags);
    506       1.68  christos 					status = exitstatus;
    507      1.134       kre 					cp = ncp;
    508        1.1       cgd 				}
    509        1.1       cgd 				goto out;
    510        1.1       cgd 			}
    511        1.1       cgd 		}
    512        1.1       cgd 	}
    513      1.134       kre  out:
    514       1.68  christos 	exitstatus = status;
    515        1.1       cgd 	popstackmark(&smark);
    516        1.1       cgd }
    517        1.1       cgd 
    518        1.1       cgd 
    519        1.1       cgd 
    520        1.1       cgd /*
    521        1.1       cgd  * Kick off a subshell to evaluate a tree.
    522        1.1       cgd  */
    523        1.1       cgd 
    524        1.1       cgd STATIC void
    525       1.68  christos evalsubshell(union node *n, int flags)
    526       1.16       cgd {
    527  1.153.2.3  pgoyette 	struct job *jp= NULL;
    528        1.1       cgd 	int backgnd = (n->type == NBACKGND);
    529        1.1       cgd 
    530        1.1       cgd 	expredir(n->nredir.redirect);
    531      1.151       kre 	if (xflag && n->nredir.redirect) {
    532      1.151       kre 		union node *rn;
    533      1.151       kre 
    534      1.153       kre 		outxstr(expandstr(ps4val(), line_number));
    535      1.153       kre 		outxstr("using redirections:");
    536      1.151       kre 		for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
    537      1.153       kre 			(void) outredir(outx, rn, ' ');
    538  1.153.2.3  pgoyette 		outxstr(" do subshell ("/*)*/);
    539  1.153.2.3  pgoyette 		if (backgnd)
    540  1.153.2.3  pgoyette 			outxstr(/*(*/") &");
    541  1.153.2.3  pgoyette 		outxc('\n');
    542      1.153       kre 		flushout(outx);
    543      1.151       kre 	}
    544  1.153.2.3  pgoyette 	if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
    545  1.153.2.3  pgoyette 	    forkshell(jp = makejob(n, 1), n, backgnd?FORK_BG:FORK_FG) == 0) {
    546       1.63   mycroft 		INTON;
    547        1.1       cgd 		if (backgnd)
    548        1.1       cgd 			flags &=~ EV_TESTED;
    549      1.119  christos 		redirect(n->nredir.redirect, REDIR_KEEP);
    550  1.153.2.3  pgoyette 		evaltree(n->nredir.n, flags | EV_EXIT);   /* never returns */
    551  1.153.2.3  pgoyette 	} else if (!backgnd) {
    552  1.153.2.3  pgoyette 		INTOFF;
    553  1.153.2.3  pgoyette 		exitstatus = waitforjob(jp);
    554  1.153.2.3  pgoyette 		INTON;
    555  1.153.2.3  pgoyette 	} else
    556  1.153.2.3  pgoyette 		exitstatus = 0;
    557  1.153.2.3  pgoyette 
    558      1.151       kre 	if (!backgnd && xflag && n->nredir.redirect) {
    559      1.153       kre 		outxstr(expandstr(ps4val(), line_number));
    560  1.153.2.3  pgoyette 		outxstr(/*(*/") done subshell\n");
    561      1.153       kre 		flushout(outx);
    562      1.151       kre 	}
    563        1.1       cgd }
    564        1.1       cgd 
    565        1.1       cgd 
    566        1.1       cgd 
    567        1.1       cgd /*
    568        1.1       cgd  * Compute the names of the files in a redirection list.
    569        1.1       cgd  */
    570        1.1       cgd 
    571        1.1       cgd STATIC void
    572       1.68  christos expredir(union node *n)
    573       1.22  christos {
    574       1.34       tls 	union node *redir;
    575        1.1       cgd 
    576        1.1       cgd 	for (redir = n ; redir ; redir = redir->nfile.next) {
    577       1.14       jtc 		struct arglist fn;
    578      1.120  christos 
    579       1.14       jtc 		fn.lastp = &fn.list;
    580       1.14       jtc 		switch (redir->type) {
    581       1.45  christos 		case NFROMTO:
    582       1.14       jtc 		case NFROM:
    583       1.14       jtc 		case NTO:
    584       1.59  christos 		case NCLOBBER:
    585       1.14       jtc 		case NAPPEND:
    586        1.7       jtc 			expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
    587        1.1       cgd 			redir->nfile.expfname = fn.list->text;
    588       1.14       jtc 			break;
    589       1.14       jtc 		case NFROMFD:
    590       1.14       jtc 		case NTOFD:
    591       1.14       jtc 			if (redir->ndup.vname) {
    592      1.149       kre 				expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
    593       1.14       jtc 				fixredir(redir, fn.list->text, 1);
    594       1.14       jtc 			}
    595       1.14       jtc 			break;
    596        1.1       cgd 		}
    597        1.1       cgd 	}
    598        1.1       cgd }
    599        1.1       cgd 
    600  1.153.2.3  pgoyette /*
    601  1.153.2.3  pgoyette  * Perform redirections for a compound command, and then do it (and restore)
    602  1.153.2.3  pgoyette  */
    603  1.153.2.3  pgoyette STATIC void
    604  1.153.2.3  pgoyette evalredir(union node *n, int flags)
    605  1.153.2.3  pgoyette {
    606  1.153.2.3  pgoyette 	struct jmploc jmploc;
    607  1.153.2.3  pgoyette 	struct jmploc * const savehandler = handler;
    608  1.153.2.3  pgoyette 	volatile int in_redirect = 1;
    609  1.153.2.3  pgoyette 	const char * volatile PS4 = NULL;
    610  1.153.2.3  pgoyette 
    611  1.153.2.3  pgoyette 	expredir(n->nredir.redirect);
    612  1.153.2.3  pgoyette 
    613  1.153.2.3  pgoyette 	if (xflag && n->nredir.redirect) {
    614  1.153.2.3  pgoyette 		union node *rn;
    615  1.153.2.3  pgoyette 
    616  1.153.2.3  pgoyette 		outxstr(PS4 = expandstr(ps4val(), line_number));
    617  1.153.2.3  pgoyette 		outxstr("using redirections:");
    618  1.153.2.3  pgoyette 		for (rn = n->nredir.redirect; rn != NULL; rn = rn->nfile.next)
    619  1.153.2.3  pgoyette 			(void) outredir(outx, rn, ' ');
    620  1.153.2.3  pgoyette 		outxstr(" do {\n");	/* } */
    621  1.153.2.3  pgoyette 		flushout(outx);
    622  1.153.2.3  pgoyette 	}
    623  1.153.2.3  pgoyette 
    624  1.153.2.3  pgoyette 	if (setjmp(jmploc.loc)) {
    625  1.153.2.3  pgoyette 		int e;
    626  1.153.2.3  pgoyette 
    627  1.153.2.3  pgoyette 		handler = savehandler;
    628  1.153.2.3  pgoyette 		e = exception;
    629  1.153.2.3  pgoyette 		popredir();
    630  1.153.2.4  pgoyette 		if (PS4 != NULL) {
    631  1.153.2.3  pgoyette 			outxstr(PS4);
    632  1.153.2.3  pgoyette 			/* { */ outxstr("} failed\n");
    633  1.153.2.3  pgoyette 			flushout(outx);
    634  1.153.2.3  pgoyette 		}
    635  1.153.2.3  pgoyette 		if (e == EXERROR || e == EXEXEC) {
    636  1.153.2.3  pgoyette 			if (in_redirect) {
    637  1.153.2.3  pgoyette 				exitstatus = 2;
    638  1.153.2.3  pgoyette 				return;
    639  1.153.2.3  pgoyette 			}
    640  1.153.2.3  pgoyette 		}
    641  1.153.2.3  pgoyette 		longjmp(handler->loc, 1);
    642  1.153.2.3  pgoyette 	} else {
    643  1.153.2.3  pgoyette 		INTOFF;
    644  1.153.2.3  pgoyette 		handler = &jmploc;
    645  1.153.2.3  pgoyette 		redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
    646  1.153.2.3  pgoyette 		in_redirect = 0;
    647  1.153.2.3  pgoyette 		INTON;
    648  1.153.2.3  pgoyette 		evaltree(n->nredir.n, flags);
    649  1.153.2.3  pgoyette 	}
    650  1.153.2.3  pgoyette 	INTOFF;
    651  1.153.2.3  pgoyette 	handler = savehandler;
    652  1.153.2.3  pgoyette 	popredir();
    653  1.153.2.3  pgoyette 	INTON;
    654  1.153.2.3  pgoyette 
    655  1.153.2.4  pgoyette 	if (PS4 != NULL) {
    656  1.153.2.3  pgoyette 		outxstr(PS4);
    657  1.153.2.3  pgoyette 		/* { */ outxstr("} done\n");
    658  1.153.2.3  pgoyette 		flushout(outx);
    659  1.153.2.3  pgoyette 	}
    660  1.153.2.3  pgoyette }
    661        1.1       cgd 
    662        1.1       cgd 
    663        1.1       cgd /*
    664        1.1       cgd  * Evaluate a pipeline.  All the processes in the pipeline are children
    665        1.1       cgd  * of the process creating the pipeline.  (This differs from some versions
    666        1.1       cgd  * of the shell, which make the last process in a pipeline the parent
    667        1.1       cgd  * of all the rest.)
    668        1.1       cgd  */
    669        1.1       cgd 
    670        1.1       cgd STATIC void
    671       1.68  christos evalpipe(union node *n)
    672       1.22  christos {
    673        1.1       cgd 	struct job *jp;
    674        1.1       cgd 	struct nodelist *lp;
    675        1.1       cgd 	int pipelen;
    676        1.1       cgd 	int prevfd;
    677        1.1       cgd 	int pip[2];
    678        1.1       cgd 
    679      1.148       kre 	CTRACE(DBG_EVAL, ("evalpipe(%p) called\n", n));
    680        1.1       cgd 	pipelen = 0;
    681        1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
    682        1.1       cgd 		pipelen++;
    683        1.1       cgd 	INTOFF;
    684        1.1       cgd 	jp = makejob(n, pipelen);
    685        1.1       cgd 	prevfd = -1;
    686        1.1       cgd 	for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    687        1.1       cgd 		prehash(lp->n);
    688        1.1       cgd 		pip[1] = -1;
    689        1.1       cgd 		if (lp->next) {
    690       1.76       dsl 			if (sh_pipe(pip) < 0) {
    691       1.87  christos 				if (prevfd >= 0)
    692       1.87  christos 					close(prevfd);
    693  1.153.2.3  pgoyette 				error("Pipe call failed: %s", strerror(errno));
    694        1.1       cgd 			}
    695        1.1       cgd 		}
    696  1.153.2.3  pgoyette 		if (forkshell(jp, lp->n,
    697  1.153.2.3  pgoyette 		    n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
    698        1.1       cgd 			INTON;
    699      1.120  christos 			if (prevfd > 0)
    700      1.120  christos 				movefd(prevfd, 0);
    701        1.1       cgd 			if (pip[1] >= 0) {
    702        1.1       cgd 				close(pip[0]);
    703      1.120  christos 				movefd(pip[1], 1);
    704        1.1       cgd 			}
    705       1.65  christos 			evaltree(lp->n, EV_EXIT);
    706        1.1       cgd 		}
    707        1.1       cgd 		if (prevfd >= 0)
    708        1.1       cgd 			close(prevfd);
    709        1.1       cgd 		prevfd = pip[0];
    710        1.1       cgd 		close(pip[1]);
    711        1.1       cgd 	}
    712        1.1       cgd 	if (n->npipe.backgnd == 0) {
    713  1.153.2.3  pgoyette 		INTOFF;
    714        1.1       cgd 		exitstatus = waitforjob(jp);
    715      1.148       kre 		CTRACE(DBG_EVAL, ("evalpipe:  job done exit status %d\n",
    716      1.148       kre 		    exitstatus));
    717  1.153.2.3  pgoyette 		INTON;
    718      1.115  christos 	} else
    719      1.115  christos 		exitstatus = 0;
    720       1.60   mycroft 	INTON;
    721        1.1       cgd }
    722        1.1       cgd 
    723        1.1       cgd 
    724        1.1       cgd 
    725        1.1       cgd /*
    726        1.1       cgd  * Execute a command inside back quotes.  If it's a builtin command, we
    727        1.1       cgd  * want to save its output in a block obtained from malloc.  Otherwise
    728        1.1       cgd  * we fork off a subprocess and get the output of the command via a pipe.
    729        1.1       cgd  * Should be called with interrupts off.
    730        1.1       cgd  */
    731        1.1       cgd 
    732        1.1       cgd void
    733       1.68  christos evalbackcmd(union node *n, struct backcmd *result)
    734       1.22  christos {
    735        1.1       cgd 	int pip[2];
    736        1.1       cgd 	struct job *jp;
    737  1.153.2.3  pgoyette 	struct stackmark smark;		/* unnecessary (because we fork) */
    738        1.1       cgd 
    739        1.1       cgd 	result->fd = -1;
    740        1.1       cgd 	result->buf = NULL;
    741        1.1       cgd 	result->nleft = 0;
    742        1.1       cgd 	result->jp = NULL;
    743  1.153.2.3  pgoyette 
    744  1.153.2.3  pgoyette 	if (nflag || n == NULL)
    745        1.7       jtc 		goto out;
    746  1.153.2.3  pgoyette 
    747  1.153.2.3  pgoyette 	setstackmark(&smark);
    748  1.153.2.3  pgoyette 
    749       1.46  christos #ifdef notyet
    750       1.46  christos 	/*
    751       1.46  christos 	 * For now we disable executing builtins in the same
    752       1.46  christos 	 * context as the shell, because we are not keeping
    753       1.46  christos 	 * enough state to recover from changes that are
    754       1.46  christos 	 * supposed only to affect subshells. eg. echo "`cd /`"
    755       1.46  christos 	 */
    756        1.7       jtc 	if (n->type == NCMD) {
    757  1.153.2.3  pgoyette 		exitstatus = oexitstatus;	/* XXX o... no longer exists */
    758       1.65  christos 		evalcommand(n, EV_BACKCMD, result);
    759       1.46  christos 	} else
    760       1.46  christos #endif
    761       1.46  christos 	{
    762       1.63   mycroft 		INTOFF;
    763       1.76       dsl 		if (sh_pipe(pip) < 0)
    764        1.1       cgd 			error("Pipe call failed");
    765        1.1       cgd 		jp = makejob(n, 1);
    766       1.65  christos 		if (forkshell(jp, n, FORK_NOJOB) == 0) {
    767        1.1       cgd 			FORCEINTON;
    768        1.1       cgd 			close(pip[0]);
    769      1.120  christos 			movefd(pip[1], 1);
    770       1.50  christos 			eflag = 0;
    771       1.65  christos 			evaltree(n, EV_EXIT);
    772       1.68  christos 			/* NOTREACHED */
    773        1.1       cgd 		}
    774        1.1       cgd 		close(pip[1]);
    775        1.1       cgd 		result->fd = pip[0];
    776        1.1       cgd 		result->jp = jp;
    777       1.63   mycroft 		INTON;
    778        1.1       cgd 	}
    779        1.1       cgd 	popstackmark(&smark);
    780  1.153.2.3  pgoyette  out:
    781      1.148       kre 	CTRACE(DBG_EVAL, ("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
    782        1.1       cgd 		result->fd, result->buf, result->nleft, result->jp));
    783        1.1       cgd }
    784        1.1       cgd 
    785  1.153.2.2  pgoyette const char *
    786       1.70       dsl syspath(void)
    787       1.70       dsl {
    788       1.70       dsl 	static char *sys_path = NULL;
    789       1.70       dsl 	static int mib[] = {CTL_USER, USER_CS_PATH};
    790       1.80  christos 	static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
    791       1.72       agc 	size_t len;
    792       1.70       dsl 
    793       1.70       dsl 	if (sys_path == NULL) {
    794       1.70       dsl 		if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
    795       1.70       dsl 		    (sys_path = ckmalloc(len + 5)) != NULL &&
    796       1.70       dsl 		    sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
    797       1.70       dsl 			memcpy(sys_path, "PATH=", 5);
    798       1.70       dsl 		} else {
    799       1.70       dsl 			ckfree(sys_path);
    800       1.70       dsl 			/* something to keep things happy */
    801       1.80  christos 			sys_path = def_path;
    802       1.70       dsl 		}
    803       1.70       dsl 	}
    804       1.70       dsl 	return sys_path;
    805       1.70       dsl }
    806       1.70       dsl 
    807       1.70       dsl static int
    808       1.70       dsl parse_command_args(int argc, char **argv, int *use_syspath)
    809       1.70       dsl {
    810       1.70       dsl 	int sv_argc = argc;
    811       1.70       dsl 	char *cp, c;
    812       1.70       dsl 
    813       1.70       dsl 	*use_syspath = 0;
    814       1.70       dsl 
    815       1.70       dsl 	for (;;) {
    816       1.70       dsl 		argv++;
    817       1.70       dsl 		if (--argc == 0)
    818       1.70       dsl 			break;
    819       1.70       dsl 		cp = *argv;
    820       1.70       dsl 		if (*cp++ != '-')
    821       1.70       dsl 			break;
    822       1.70       dsl 		if (*cp == '-' && cp[1] == 0) {
    823       1.70       dsl 			argv++;
    824       1.70       dsl 			argc--;
    825       1.70       dsl 			break;
    826       1.70       dsl 		}
    827       1.70       dsl 		while ((c = *cp++)) {
    828       1.70       dsl 			switch (c) {
    829       1.70       dsl 			case 'p':
    830       1.70       dsl 				*use_syspath = 1;
    831       1.70       dsl 				break;
    832       1.70       dsl 			default:
    833       1.70       dsl 				/* run 'typecmd' for other options */
    834       1.70       dsl 				return 0;
    835       1.70       dsl 			}
    836       1.70       dsl 		}
    837       1.70       dsl 	}
    838       1.70       dsl 	return sv_argc - argc;
    839       1.70       dsl }
    840        1.1       cgd 
    841       1.61  christos int vforked = 0;
    842       1.97  christos extern char *trap[];
    843        1.1       cgd 
    844        1.1       cgd /*
    845        1.1       cgd  * Execute a simple command.
    846        1.1       cgd  */
    847        1.1       cgd 
    848        1.1       cgd STATIC void
    849       1.88  christos evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
    850       1.16       cgd {
    851        1.1       cgd 	struct stackmark smark;
    852        1.1       cgd 	union node *argp;
    853        1.1       cgd 	struct arglist arglist;
    854        1.1       cgd 	struct arglist varlist;
    855       1.88  christos 	volatile int flags = flgs;
    856       1.88  christos 	char ** volatile argv;
    857       1.88  christos 	volatile int argc;
    858        1.1       cgd 	char **envp;
    859        1.1       cgd 	int varflag;
    860        1.1       cgd 	struct strlist *sp;
    861       1.88  christos 	volatile int mode;
    862        1.1       cgd 	int pip[2];
    863        1.1       cgd 	struct cmdentry cmdentry;
    864       1.88  christos 	struct job * volatile jp;
    865        1.1       cgd 	struct jmploc jmploc;
    866       1.85  christos 	struct jmploc *volatile savehandler = NULL;
    867       1.89      matt 	const char *volatile savecmdname;
    868        1.1       cgd 	volatile struct shparam saveparam;
    869        1.1       cgd 	struct localvar *volatile savelocalvars;
    870        1.1       cgd 	volatile int e;
    871       1.88  christos 	char * volatile lastarg;
    872       1.88  christos 	const char * volatile path = pathval();
    873       1.82     lukem 	volatile int temp_path;
    874      1.143       kre 	const int savefuncline = funclinebase;
    875      1.143       kre 	const int savefuncabs = funclineabs;
    876        1.1       cgd 
    877       1.61  christos 	vforked = 0;
    878        1.1       cgd 	/* First expand the arguments. */
    879      1.153       kre 	CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called [%s]\n", cmd, flags,
    880      1.153       kre 	    cmd->ncmd.args ? cmd->ncmd.args->narg.text : ""));
    881        1.1       cgd 	setstackmark(&smark);
    882       1.68  christos 	back_exitstatus = 0;
    883       1.68  christos 
    884      1.151       kre 	line_number = cmd->ncmd.lineno;
    885      1.142       kre 
    886        1.1       cgd 	arglist.lastp = &arglist.list;
    887        1.1       cgd 	varflag = 1;
    888       1.78       dsl 	/* Expand arguments, ignoring the initial 'name=value' ones */
    889        1.1       cgd 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    890       1.20  christos 		char *p = argp->narg.text;
    891      1.143       kre 
    892      1.143       kre 		line_number = argp->narg.lineno;
    893        1.1       cgd 		if (varflag && is_name(*p)) {
    894        1.1       cgd 			do {
    895        1.1       cgd 				p++;
    896        1.1       cgd 			} while (is_in_name(*p));
    897       1.68  christos 			if (*p == '=')
    898        1.1       cgd 				continue;
    899        1.1       cgd 		}
    900        1.7       jtc 		expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
    901        1.1       cgd 		varflag = 0;
    902        1.1       cgd 	}
    903        1.1       cgd 	*arglist.lastp = NULL;
    904       1.68  christos 
    905       1.68  christos 	expredir(cmd->ncmd.redirect);
    906       1.68  christos 
    907       1.78       dsl 	/* Now do the initial 'name=value' ones we skipped above */
    908       1.68  christos 	varlist.lastp = &varlist.list;
    909       1.68  christos 	for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
    910       1.68  christos 		char *p = argp->narg.text;
    911      1.143       kre 
    912      1.143       kre 		line_number = argp->narg.lineno;
    913       1.68  christos 		if (!is_name(*p))
    914       1.68  christos 			break;
    915       1.68  christos 		do
    916       1.68  christos 			p++;
    917       1.68  christos 		while (is_in_name(*p));
    918       1.68  christos 		if (*p != '=')
    919       1.68  christos 			break;
    920       1.68  christos 		expandarg(argp, &varlist, EXP_VARTILDE);
    921       1.68  christos 	}
    922        1.1       cgd 	*varlist.lastp = NULL;
    923       1.68  christos 
    924        1.1       cgd 	argc = 0;
    925        1.1       cgd 	for (sp = arglist.list ; sp ; sp = sp->next)
    926        1.1       cgd 		argc++;
    927        1.1       cgd 	argv = stalloc(sizeof (char *) * (argc + 1));
    928        1.7       jtc 
    929        1.7       jtc 	for (sp = arglist.list ; sp ; sp = sp->next) {
    930      1.148       kre 		VTRACE(DBG_EVAL, ("evalcommand arg: %s\n", sp->text));
    931        1.1       cgd 		*argv++ = sp->text;
    932        1.7       jtc 	}
    933        1.1       cgd 	*argv = NULL;
    934        1.1       cgd 	lastarg = NULL;
    935        1.1       cgd 	if (iflag && funcnest == 0 && argc > 0)
    936        1.1       cgd 		lastarg = argv[-1];
    937        1.1       cgd 	argv -= argc;
    938        1.1       cgd 
    939        1.1       cgd 	/* Print the command if xflag is set. */
    940        1.1       cgd 	if (xflag) {
    941       1.70       dsl 		char sep = 0;
    942      1.151       kre 		union node *rn;
    943      1.151       kre 
    944      1.153       kre 		outxstr(expandstr(ps4val(), line_number));
    945        1.1       cgd 		for (sp = varlist.list ; sp ; sp = sp->next) {
    946      1.116  christos 			char *p;
    947      1.116  christos 
    948       1.70       dsl 			if (sep != 0)
    949      1.153       kre 				outxc(sep);
    950      1.116  christos 
    951      1.116  christos 			/*
    952      1.116  christos 			 * The "var=" part should not be quoted, regardless
    953      1.116  christos 			 * of the value, or it would not represent an
    954      1.116  christos 			 * assignment, but rather a command
    955      1.116  christos 			 */
    956      1.116  christos 			p = strchr(sp->text, '=');
    957      1.116  christos 			if (p != NULL) {
    958      1.116  christos 				*p = '\0';	/*XXX*/
    959      1.153       kre 				outxshstr(sp->text);
    960      1.153       kre 				outxc('=');
    961      1.116  christos 				*p++ = '=';	/*XXX*/
    962      1.116  christos 			} else
    963      1.116  christos 				p = sp->text;
    964      1.153       kre 			outxshstr(p);
    965       1.70       dsl 			sep = ' ';
    966        1.1       cgd 		}
    967        1.1       cgd 		for (sp = arglist.list ; sp ; sp = sp->next) {
    968       1.70       dsl 			if (sep != 0)
    969      1.153       kre 				outxc(sep);
    970      1.153       kre 			outxshstr(sp->text);
    971       1.70       dsl 			sep = ' ';
    972        1.1       cgd 		}
    973      1.151       kre 		for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next)
    974      1.153       kre 			if (outredir(outx, rn, sep))
    975      1.151       kre 				sep = ' ';
    976      1.153       kre 		outxc('\n');
    977      1.153       kre 		flushout(outx);
    978        1.1       cgd 	}
    979        1.1       cgd 
    980        1.1       cgd 	/* Now locate the command. */
    981        1.1       cgd 	if (argc == 0) {
    982       1.70       dsl 		cmdentry.cmdtype = CMDSPLBLTIN;
    983       1.68  christos 		cmdentry.u.bltin = bltincmd;
    984        1.1       cgd 	} else {
    985       1.26  christos 		static const char PATH[] = "PATH=";
    986  1.153.2.3  pgoyette 		int cmd_flags = 0;
    987       1.26  christos 
    988       1.26  christos 		/*
    989       1.26  christos 		 * Modify the command lookup path, if a PATH= assignment
    990       1.26  christos 		 * is present
    991       1.26  christos 		 */
    992       1.70       dsl 		for (sp = varlist.list; sp; sp = sp->next)
    993       1.26  christos 			if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
    994       1.26  christos 				path = sp->text + sizeof(PATH) - 1;
    995       1.26  christos 
    996       1.70       dsl 		do {
    997       1.70       dsl 			int argsused, use_syspath;
    998      1.143       kre 
    999       1.70       dsl 			find_command(argv[0], &cmdentry, cmd_flags, path);
   1000  1.153.2.3  pgoyette #if 0
   1001  1.153.2.3  pgoyette 			/*
   1002  1.153.2.3  pgoyette 			 * This short circuits all of the processing that
   1003  1.153.2.3  pgoyette 			 * should be done (including processing the
   1004  1.153.2.3  pgoyette 			 * redirects), so just don't ...
   1005  1.153.2.3  pgoyette 			 *
   1006  1.153.2.3  pgoyette 			 * (eventually this whole #if'd block will vanish)
   1007  1.153.2.3  pgoyette 			 */
   1008       1.70       dsl 			if (cmdentry.cmdtype == CMDUNKNOWN) {
   1009       1.68  christos 				exitstatus = 127;
   1010       1.68  christos 				flushout(&errout);
   1011       1.70       dsl 				goto out;
   1012        1.1       cgd 			}
   1013  1.153.2.3  pgoyette #endif
   1014       1.70       dsl 
   1015       1.70       dsl 			/* implement the 'command' builtin here */
   1016       1.70       dsl 			if (cmdentry.cmdtype != CMDBUILTIN ||
   1017       1.70       dsl 			    cmdentry.u.bltin != bltincmd)
   1018       1.70       dsl 				break;
   1019       1.70       dsl 			cmd_flags |= DO_NOFUNC;
   1020       1.70       dsl 			argsused = parse_command_args(argc, argv, &use_syspath);
   1021       1.70       dsl 			if (argsused == 0) {
   1022  1.153.2.1  pgoyette 				/* use 'type' builtin to display info */
   1023       1.70       dsl 				cmdentry.u.bltin = typecmd;
   1024       1.70       dsl 				break;
   1025       1.70       dsl 			}
   1026       1.70       dsl 			argc -= argsused;
   1027       1.70       dsl 			argv += argsused;
   1028       1.70       dsl 			if (use_syspath)
   1029       1.70       dsl 				path = syspath() + 5;
   1030       1.70       dsl 		} while (argc != 0);
   1031       1.70       dsl 		if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
   1032       1.70       dsl 			/* posix mandates that 'command <splbltin>' act as if
   1033       1.70       dsl 			   <splbltin> was a normal builtin */
   1034       1.70       dsl 			cmdentry.cmdtype = CMDBUILTIN;
   1035        1.1       cgd 	}
   1036        1.1       cgd 
   1037        1.1       cgd 	/* Fork off a child process if necessary. */
   1038       1.98  christos 	if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0)
   1039  1.153.2.3  pgoyette 	 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
   1040  1.153.2.3  pgoyette 	     && (flags & EV_EXIT) == 0)
   1041  1.153.2.3  pgoyette 	 || ((flags & EV_BACKCMD) != 0 &&
   1042  1.153.2.3  pgoyette 	    ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
   1043       1.68  christos 		 || cmdentry.u.bltin == dotcmd
   1044       1.68  christos 		 || cmdentry.u.bltin == evalcmd))) {
   1045       1.62  christos 		INTOFF;
   1046        1.1       cgd 		jp = makejob(cmd, 1);
   1047        1.1       cgd 		mode = cmd->ncmd.backgnd;
   1048        1.1       cgd 		if (flags & EV_BACKCMD) {
   1049        1.1       cgd 			mode = FORK_NOJOB;
   1050       1.76       dsl 			if (sh_pipe(pip) < 0)
   1051        1.1       cgd 				error("Pipe call failed");
   1052        1.1       cgd 		}
   1053       1.61  christos #ifdef DO_SHAREDVFORK
   1054       1.61  christos 		/* It is essential that if DO_SHAREDVFORK is defined that the
   1055       1.61  christos 		 * child's address space is actually shared with the parent as
   1056       1.61  christos 		 * we rely on this.
   1057       1.61  christos 		 */
   1058      1.110  christos 		if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) {
   1059       1.61  christos 			pid_t	pid;
   1060      1.108  christos 			int serrno;
   1061       1.61  christos 
   1062       1.61  christos 			savelocalvars = localvars;
   1063       1.61  christos 			localvars = NULL;
   1064       1.61  christos 			vforked = 1;
   1065      1.140       kre 	VFORK_BLOCK
   1066       1.61  christos 			switch (pid = vfork()) {
   1067       1.61  christos 			case -1:
   1068      1.108  christos 				serrno = errno;
   1069      1.148       kre 				VTRACE(DBG_EVAL, ("vfork() failed, errno=%d\n",
   1070      1.148       kre 				    serrno));
   1071       1.61  christos 				INTON;
   1072      1.108  christos 				error("Cannot vfork (%s)", strerror(serrno));
   1073       1.61  christos 				break;
   1074       1.61  christos 			case 0:
   1075       1.61  christos 				/* Make sure that exceptions only unwind to
   1076       1.61  christos 				 * after the vfork(2)
   1077       1.61  christos 				 */
   1078      1.140       kre 				SHELL_FORKED();
   1079       1.61  christos 				if (setjmp(jmploc.loc)) {
   1080       1.61  christos 					if (exception == EXSHELLPROC) {
   1081      1.148       kre 						/*
   1082      1.148       kre 						 * We can't progress with the
   1083      1.148       kre 						 * vfork, so, set vforked = 2
   1084      1.148       kre 						 * so the parent knows,
   1085      1.148       kre 						 * and _exit();
   1086       1.61  christos 						 */
   1087       1.61  christos 						vforked = 2;
   1088       1.61  christos 						_exit(0);
   1089       1.61  christos 					} else {
   1090  1.153.2.3  pgoyette 						_exit(exception == EXEXIT ?
   1091  1.153.2.3  pgoyette 						    exitstatus : exerrno);
   1092       1.61  christos 					}
   1093       1.61  christos 				}
   1094       1.61  christos 				savehandler = handler;
   1095       1.61  christos 				handler = &jmploc;
   1096       1.70       dsl 				listmklocal(varlist.list, VEXPORT | VNOFUNC);
   1097       1.65  christos 				forkchild(jp, cmd, mode, vforked);
   1098       1.61  christos 				break;
   1099       1.61  christos 			default:
   1100      1.140       kre 				VFORK_UNDO();
   1101      1.148       kre 						/* restore from vfork(2) */
   1102      1.148       kre 				handler = savehandler;
   1103       1.61  christos 				poplocalvars();
   1104       1.61  christos 				localvars = savelocalvars;
   1105       1.61  christos 				if (vforked == 2) {
   1106       1.61  christos 					vforked = 0;
   1107       1.61  christos 
   1108       1.61  christos 					(void)waitpid(pid, NULL, 0);
   1109      1.148       kre 					/*
   1110      1.148       kre 					 * We need to progress in a
   1111      1.148       kre 					 * normal fork fashion
   1112      1.148       kre 					 */
   1113       1.61  christos 					goto normal_fork;
   1114       1.61  christos 				}
   1115      1.148       kre 				/*
   1116      1.148       kre 				 * Here the child has left home,
   1117      1.148       kre 				 * getting on with its life, so
   1118      1.148       kre 				 * so must we...
   1119      1.148       kre 				 */
   1120       1.61  christos 				vforked = 0;
   1121       1.65  christos 				forkparent(jp, cmd, mode, pid);
   1122       1.61  christos 				goto parent;
   1123       1.61  christos 			}
   1124      1.140       kre 	VFORK_END
   1125       1.61  christos 		} else {
   1126      1.148       kre  normal_fork:
   1127       1.61  christos #endif
   1128       1.65  christos 			if (forkshell(jp, cmd, mode) != 0)
   1129       1.61  christos 				goto parent;	/* at end of routine */
   1130  1.153.2.3  pgoyette 			flags |= EV_EXIT;
   1131       1.66  christos 			FORCEINTON;
   1132       1.61  christos #ifdef DO_SHAREDVFORK
   1133       1.61  christos 		}
   1134       1.61  christos #endif
   1135        1.1       cgd 		if (flags & EV_BACKCMD) {
   1136       1.61  christos 			if (!vforked) {
   1137       1.61  christos 				FORCEINTON;
   1138       1.61  christos 			}
   1139        1.1       cgd 			close(pip[0]);
   1140      1.120  christos 			movefd(pip[1], 1);
   1141        1.1       cgd 		}
   1142        1.1       cgd 		flags |= EV_EXIT;
   1143        1.1       cgd 	}
   1144        1.1       cgd 
   1145        1.1       cgd 	/* This is the child process if a fork occurred. */
   1146        1.1       cgd 	/* Execute the command. */
   1147       1.70       dsl 	switch (cmdentry.cmdtype) {
   1148  1.153.2.3  pgoyette 		volatile int saved;
   1149  1.153.2.3  pgoyette 
   1150       1.70       dsl 	case CMDFUNCTION:
   1151      1.152       kre 		VXTRACE(DBG_EVAL, ("Shell function%s:  ",vforked?" VF":""),
   1152      1.152       kre 		    trargs(argv));
   1153  1.153.2.3  pgoyette 		redirect(cmd->ncmd.redirect, saved =
   1154  1.153.2.3  pgoyette 			!(flags & EV_EXIT) || have_traps() ? REDIR_PUSH : 0);
   1155        1.1       cgd 		saveparam = shellparam;
   1156        1.1       cgd 		shellparam.malloc = 0;
   1157       1.32  christos 		shellparam.reset = 1;
   1158        1.1       cgd 		shellparam.nparam = argc - 1;
   1159        1.1       cgd 		shellparam.p = argv + 1;
   1160        1.1       cgd 		shellparam.optnext = NULL;
   1161        1.1       cgd 		INTOFF;
   1162        1.1       cgd 		savelocalvars = localvars;
   1163        1.1       cgd 		localvars = NULL;
   1164  1.153.2.1  pgoyette 		reffunc(cmdentry.u.func);
   1165        1.1       cgd 		INTON;
   1166        1.1       cgd 		if (setjmp(jmploc.loc)) {
   1167       1.47  christos 			if (exception == EXSHELLPROC) {
   1168       1.47  christos 				freeparam((volatile struct shparam *)
   1169       1.47  christos 				    &saveparam);
   1170       1.47  christos 			} else {
   1171        1.1       cgd 				freeparam(&shellparam);
   1172        1.1       cgd 				shellparam = saveparam;
   1173        1.1       cgd 			}
   1174  1.153.2.3  pgoyette 			if (saved)
   1175  1.153.2.3  pgoyette 				popredir();;
   1176  1.153.2.1  pgoyette 			unreffunc(cmdentry.u.func);
   1177        1.1       cgd 			poplocalvars();
   1178        1.1       cgd 			localvars = savelocalvars;
   1179      1.143       kre 			funclinebase = savefuncline;
   1180      1.143       kre 			funclineabs = savefuncabs;
   1181        1.1       cgd 			handler = savehandler;
   1182        1.1       cgd 			longjmp(handler->loc, 1);
   1183        1.1       cgd 		}
   1184        1.1       cgd 		savehandler = handler;
   1185        1.1       cgd 		handler = &jmploc;
   1186      1.143       kre 		if (cmdentry.u.func) {
   1187      1.143       kre 			if (cmdentry.lno_frel)
   1188      1.143       kre 				funclinebase = cmdentry.lineno - 1;
   1189      1.143       kre 			else
   1190      1.143       kre 				funclinebase = 0;
   1191      1.143       kre 			funclineabs = cmdentry.lineno;
   1192      1.143       kre 
   1193      1.143       kre 			VTRACE(DBG_EVAL,
   1194      1.143       kre 			  ("function: node: %d '%s' # %d%s; funclinebase=%d\n",
   1195  1.153.2.1  pgoyette 			    getfuncnode(cmdentry.u.func)->type,
   1196  1.153.2.1  pgoyette 			    NODETYPENAME(getfuncnode(cmdentry.u.func)->type),
   1197      1.143       kre 			    cmdentry.lineno, cmdentry.lno_frel?" (=1)":"",
   1198      1.143       kre 			    funclinebase));
   1199      1.143       kre 		}
   1200      1.106  christos 		listmklocal(varlist.list, VEXPORT);
   1201       1.70       dsl 		/* stop shell blowing its stack */
   1202       1.70       dsl 		if (++funcnest > 1000)
   1203       1.70       dsl 			error("too many nested function calls");
   1204  1.153.2.3  pgoyette 		evaltree(getfuncnode(cmdentry.u.func),
   1205  1.153.2.3  pgoyette 		    flags & (EV_TESTED|EV_EXIT));
   1206        1.1       cgd 		funcnest--;
   1207        1.1       cgd 		INTOFF;
   1208  1.153.2.1  pgoyette 		unreffunc(cmdentry.u.func);
   1209        1.1       cgd 		poplocalvars();
   1210        1.1       cgd 		localvars = savelocalvars;
   1211      1.143       kre 		funclinebase = savefuncline;
   1212      1.143       kre 		funclineabs = savefuncabs;
   1213        1.1       cgd 		freeparam(&shellparam);
   1214        1.1       cgd 		shellparam = saveparam;
   1215        1.1       cgd 		handler = savehandler;
   1216  1.153.2.3  pgoyette 		if (saved)
   1217      1.125       kre 			popredir();
   1218        1.1       cgd 		INTON;
   1219        1.1       cgd 		if (evalskip == SKIPFUNC) {
   1220      1.109  christos 			evalskip = SKIPNONE;
   1221        1.1       cgd 			skipcount = 0;
   1222        1.1       cgd 		}
   1223        1.1       cgd 		if (flags & EV_EXIT)
   1224        1.1       cgd 			exitshell(exitstatus);
   1225       1.70       dsl 		break;
   1226       1.70       dsl 
   1227       1.70       dsl 	case CMDBUILTIN:
   1228       1.70       dsl 	case CMDSPLBLTIN:
   1229      1.152       kre 		VXTRACE(DBG_EVAL, ("builtin command%s:  ",vforked?" VF":""), trargs(argv));
   1230       1.68  christos 		mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
   1231        1.1       cgd 		if (flags == EV_BACKCMD) {
   1232        1.1       cgd 			memout.nleft = 0;
   1233        1.1       cgd 			memout.nextc = memout.buf;
   1234        1.1       cgd 			memout.bufsize = 64;
   1235        1.1       cgd 			mode |= REDIR_BACKQ;
   1236        1.1       cgd 		}
   1237       1.70       dsl 		e = -1;
   1238       1.68  christos 		savehandler = handler;
   1239       1.81       dsl 		savecmdname = commandname;
   1240       1.68  christos 		handler = &jmploc;
   1241      1.104     joerg 		temp_path = 0;
   1242       1.68  christos 		if (!setjmp(jmploc.loc)) {
   1243      1.148       kre 			/*
   1244      1.148       kre 			 * We need to ensure the command hash table isn't
   1245      1.130  christos 			 * corrupted by temporary PATH assignments.
   1246       1.70       dsl 			 * However we must ensure the 'local' command works!
   1247       1.70       dsl 			 */
   1248       1.70       dsl 			if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
   1249       1.70       dsl 			    cmdentry.u.bltin == typecmd)) {
   1250       1.70       dsl 				savelocalvars = localvars;
   1251       1.70       dsl 				localvars = 0;
   1252      1.104     joerg 				temp_path = 1;
   1253       1.70       dsl 				mklocal(path - 5 /* PATH= */, 0);
   1254      1.104     joerg 			}
   1255       1.68  christos 			redirect(cmd->ncmd.redirect, mode);
   1256       1.68  christos 
   1257       1.68  christos 			/* exec is a special builtin, but needs this list... */
   1258       1.68  christos 			cmdenviron = varlist.list;
   1259       1.68  christos 			/* we must check 'readonly' flag for all builtins */
   1260       1.68  christos 			listsetvar(varlist.list,
   1261       1.68  christos 				cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
   1262       1.68  christos 			commandname = argv[0];
   1263       1.68  christos 			/* initialize nextopt */
   1264       1.68  christos 			argptr = argv + 1;
   1265       1.68  christos 			optptr = NULL;
   1266       1.68  christos 			/* and getopt */
   1267       1.68  christos 			optreset = 1;
   1268       1.68  christos 			optind = 1;
   1269      1.103  christos 			builtin_flags = flags;
   1270       1.68  christos 			exitstatus = cmdentry.u.bltin(argc, argv);
   1271       1.68  christos 		} else {
   1272        1.1       cgd 			e = exception;
   1273  1.153.2.3  pgoyette 			if (e == EXINT)
   1274  1.153.2.3  pgoyette 				exitstatus = SIGINT + 128;
   1275  1.153.2.3  pgoyette 			else if (e == EXEXEC)
   1276  1.153.2.3  pgoyette 				exitstatus = exerrno;
   1277  1.153.2.3  pgoyette 			else if (e != EXEXIT)
   1278  1.153.2.3  pgoyette 				exitstatus = 2;
   1279        1.1       cgd 		}
   1280       1.70       dsl 		handler = savehandler;
   1281        1.1       cgd 		flushall();
   1282        1.1       cgd 		out1 = &output;
   1283        1.1       cgd 		out2 = &errout;
   1284        1.1       cgd 		freestdout();
   1285       1.70       dsl 		if (temp_path) {
   1286       1.70       dsl 			poplocalvars();
   1287       1.70       dsl 			localvars = savelocalvars;
   1288       1.70       dsl 		}
   1289       1.39   thorpej 		cmdenviron = NULL;
   1290        1.1       cgd 		if (e != EXSHELLPROC) {
   1291        1.1       cgd 			commandname = savecmdname;
   1292       1.44   mycroft 			if (flags & EV_EXIT)
   1293        1.1       cgd 				exitshell(exitstatus);
   1294        1.1       cgd 		}
   1295        1.1       cgd 		if (e != -1) {
   1296       1.31  christos 			if ((e != EXERROR && e != EXEXEC)
   1297       1.70       dsl 			    || cmdentry.cmdtype == CMDSPLBLTIN)
   1298        1.1       cgd 				exraise(e);
   1299        1.1       cgd 			FORCEINTON;
   1300        1.1       cgd 		}
   1301       1.68  christos 		if (cmdentry.u.bltin != execcmd)
   1302        1.1       cgd 			popredir();
   1303        1.1       cgd 		if (flags == EV_BACKCMD) {
   1304        1.1       cgd 			backcmd->buf = memout.buf;
   1305        1.1       cgd 			backcmd->nleft = memout.nextc - memout.buf;
   1306        1.1       cgd 			memout.buf = NULL;
   1307        1.1       cgd 		}
   1308       1.70       dsl 		break;
   1309       1.70       dsl 
   1310       1.70       dsl 	default:
   1311      1.152       kre 		VXTRACE(DBG_EVAL, ("normal command%s:  ", vforked?" VF":""),
   1312      1.152       kre 		    trargs(argv));
   1313      1.117  christos 		redirect(cmd->ncmd.redirect,
   1314      1.117  christos 		    (vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
   1315       1.61  christos 		if (!vforked)
   1316       1.61  christos 			for (sp = varlist.list ; sp ; sp = sp->next)
   1317       1.61  christos 				setvareq(sp->text, VEXPORT|VSTACK);
   1318        1.1       cgd 		envp = environment();
   1319       1.70       dsl 		shellexec(argv, envp, path, cmdentry.u.index, vforked);
   1320       1.70       dsl 		break;
   1321        1.1       cgd 	}
   1322        1.1       cgd 	goto out;
   1323        1.1       cgd 
   1324      1.148       kre  parent:			/* parent process gets here (if we forked) */
   1325      1.148       kre 
   1326      1.113  christos 	exitstatus = 0;		/* if not altered just below */
   1327       1.68  christos 	if (mode == FORK_FG) {	/* argument to fork */
   1328        1.1       cgd 		exitstatus = waitforjob(jp);
   1329       1.68  christos 	} else if (mode == FORK_NOJOB) {
   1330        1.1       cgd 		backcmd->fd = pip[0];
   1331        1.1       cgd 		close(pip[1]);
   1332        1.1       cgd 		backcmd->jp = jp;
   1333        1.1       cgd 	}
   1334       1.66  christos 	FORCEINTON;
   1335        1.1       cgd 
   1336      1.148       kre  out:
   1337        1.1       cgd 	if (lastarg)
   1338      1.132       kre 		/* implement $_ for whatever use that really is */
   1339      1.147       kre 		(void) setvarsafe("_", lastarg, VNOERROR);
   1340        1.1       cgd 	popstackmark(&smark);
   1341        1.1       cgd }
   1342        1.1       cgd 
   1343        1.1       cgd 
   1344        1.1       cgd /*
   1345        1.1       cgd  * Search for a command.  This is called before we fork so that the
   1346        1.1       cgd  * location of the command will be available in the parent as well as
   1347        1.1       cgd  * the child.  The check for "goodname" is an overly conservative
   1348        1.1       cgd  * check that the name will not be subject to expansion.
   1349        1.1       cgd  */
   1350        1.1       cgd 
   1351        1.1       cgd STATIC void
   1352       1.68  christos prehash(union node *n)
   1353       1.22  christos {
   1354        1.1       cgd 	struct cmdentry entry;
   1355        1.1       cgd 
   1356       1.86  christos 	if (n && n->type == NCMD && n->ncmd.args)
   1357       1.15   mycroft 		if (goodname(n->ncmd.args->narg.text))
   1358       1.26  christos 			find_command(n->ncmd.args->narg.text, &entry, 0,
   1359       1.26  christos 				     pathval());
   1360        1.1       cgd }
   1361        1.1       cgd 
   1362      1.122       kre int
   1363      1.109  christos in_function(void)
   1364      1.109  christos {
   1365      1.109  christos 	return funcnest;
   1366      1.109  christos }
   1367        1.1       cgd 
   1368      1.122       kre enum skipstate
   1369      1.109  christos current_skipstate(void)
   1370      1.109  christos {
   1371      1.109  christos 	return evalskip;
   1372      1.109  christos }
   1373      1.109  christos 
   1374      1.122       kre void
   1375      1.109  christos stop_skipping(void)
   1376      1.109  christos {
   1377      1.109  christos 	evalskip = SKIPNONE;
   1378      1.109  christos 	skipcount = 0;
   1379      1.109  christos }
   1380        1.1       cgd 
   1381        1.1       cgd /*
   1382        1.1       cgd  * Builtin commands.  Builtin commands whose functions are closely
   1383        1.1       cgd  * tied to evaluation are implemented here.
   1384        1.1       cgd  */
   1385        1.1       cgd 
   1386        1.1       cgd /*
   1387       1.70       dsl  * No command given.
   1388        1.1       cgd  */
   1389        1.1       cgd 
   1390       1.16       cgd int
   1391       1.68  christos bltincmd(int argc, char **argv)
   1392       1.16       cgd {
   1393       1.31  christos 	/*
   1394       1.22  christos 	 * Preserve exitstatus of a previous possible redirection
   1395       1.31  christos 	 * as POSIX mandates
   1396       1.22  christos 	 */
   1397       1.68  christos 	return back_exitstatus;
   1398        1.1       cgd }
   1399        1.1       cgd 
   1400        1.1       cgd 
   1401        1.1       cgd /*
   1402        1.1       cgd  * Handle break and continue commands.  Break, continue, and return are
   1403        1.1       cgd  * all handled by setting the evalskip flag.  The evaluation routines
   1404        1.1       cgd  * above all check this flag, and if it is set they start skipping
   1405        1.1       cgd  * commands rather than executing them.  The variable skipcount is
   1406        1.1       cgd  * the number of loops to break/continue, or the number of function
   1407        1.1       cgd  * levels to return.  (The latter is always 1.)  It should probably
   1408        1.1       cgd  * be an error to break out of more loops than exist, but it isn't
   1409        1.1       cgd  * in the standard shell so we don't make it one here.
   1410        1.1       cgd  */
   1411        1.1       cgd 
   1412       1.16       cgd int
   1413       1.68  christos breakcmd(int argc, char **argv)
   1414       1.16       cgd {
   1415       1.30  christos 	int n = argc > 1 ? number(argv[1]) : 1;
   1416        1.1       cgd 
   1417      1.135       kre 	if (n <= 0)
   1418      1.135       kre 		error("invalid count: %d", n);
   1419        1.1       cgd 	if (n > loopnest)
   1420        1.1       cgd 		n = loopnest;
   1421        1.1       cgd 	if (n > 0) {
   1422        1.1       cgd 		evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
   1423        1.1       cgd 		skipcount = n;
   1424        1.1       cgd 	}
   1425        1.1       cgd 	return 0;
   1426        1.1       cgd }
   1427        1.1       cgd 
   1428      1.109  christos int
   1429      1.109  christos dotcmd(int argc, char **argv)
   1430      1.109  christos {
   1431      1.109  christos 	exitstatus = 0;
   1432      1.109  christos 
   1433      1.109  christos 	if (argc >= 2) {		/* That's what SVR2 does */
   1434      1.109  christos 		char *fullname;
   1435      1.109  christos 		/*
   1436      1.109  christos 		 * dot_funcnest needs to be 0 when not in a dotcmd, so it
   1437      1.109  christos 		 * cannot be restored with (funcnest + 1).
   1438      1.109  christos 		 */
   1439      1.109  christos 		int dot_funcnest_old;
   1440      1.109  christos 		struct stackmark smark;
   1441      1.109  christos 
   1442      1.109  christos 		setstackmark(&smark);
   1443      1.109  christos 		fullname = find_dot_file(argv[1]);
   1444      1.109  christos 		setinputfile(fullname, 1);
   1445      1.109  christos 		commandname = fullname;
   1446      1.109  christos 		dot_funcnest_old = dot_funcnest;
   1447      1.109  christos 		dot_funcnest = funcnest + 1;
   1448      1.109  christos 		cmdloop(0);
   1449      1.109  christos 		dot_funcnest = dot_funcnest_old;
   1450      1.109  christos 		popfile();
   1451      1.109  christos 		popstackmark(&smark);
   1452      1.109  christos 	}
   1453      1.109  christos 	return exitstatus;
   1454      1.109  christos }
   1455      1.109  christos 
   1456      1.109  christos /*
   1457      1.109  christos  * Take commands from a file.  To be compatible we should do a path
   1458      1.109  christos  * search for the file, which is necessary to find sub-commands.
   1459      1.109  christos  */
   1460      1.109  christos 
   1461      1.109  christos STATIC char *
   1462      1.109  christos find_dot_file(char *basename)
   1463      1.109  christos {
   1464      1.109  christos 	char *fullname;
   1465      1.109  christos 	const char *path = pathval();
   1466      1.109  christos 	struct stat statb;
   1467      1.109  christos 
   1468      1.109  christos 	/* don't try this for absolute or relative paths */
   1469      1.121       kre 	if (strchr(basename, '/')) {
   1470      1.121       kre 		if (stat(basename, &statb) == 0) {
   1471      1.128       kre 			if (S_ISDIR(statb.st_mode))
   1472      1.128       kre 				error("%s: is a directory", basename);
   1473      1.128       kre 			if (S_ISBLK(statb.st_mode))
   1474      1.128       kre 				error("%s: is a block device", basename);
   1475      1.128       kre 			return basename;
   1476      1.121       kre 		}
   1477      1.141       kre 	} else while ((fullname = padvance(&path, basename, 1)) != NULL) {
   1478      1.128       kre 		if ((stat(fullname, &statb) == 0)) {
   1479      1.128       kre 			/* weird format is to ease future code... */
   1480      1.128       kre 			if (S_ISDIR(statb.st_mode) || S_ISBLK(statb.st_mode))
   1481      1.128       kre 				;
   1482      1.128       kre #if notyet
   1483      1.128       kre 			else if (unreadable()) {
   1484      1.128       kre 				/*
   1485      1.128       kre 				 * testing this via st_mode is ugly to get
   1486      1.128       kre 				 * correct (and would ignore ACLs).
   1487      1.128       kre 				 * better way is just to open the file.
   1488      1.128       kre 				 * But doing that here would (currently)
   1489      1.128       kre 				 * mean opening the file twice, which
   1490      1.128       kre 				 * might not be safe.  So, defer this
   1491      1.128       kre 				 * test until code is restructures so
   1492      1.128       kre 				 * we can return a fd.   Then we also
   1493      1.128       kre 				 * get to fix the mem leak just below...
   1494      1.128       kre 				 */
   1495      1.128       kre 			}
   1496      1.128       kre #endif
   1497      1.128       kre 			else {
   1498      1.128       kre 				/*
   1499      1.128       kre 				 * Don't bother freeing here, since
   1500      1.128       kre 				 * it will be freed by the caller.
   1501      1.128       kre 				 * XXX no it won't - a bug for later.
   1502      1.128       kre 				 */
   1503      1.128       kre 				return fullname;
   1504      1.128       kre 			}
   1505      1.109  christos 		}
   1506      1.109  christos 		stunalloc(fullname);
   1507      1.109  christos 	}
   1508      1.109  christos 
   1509      1.109  christos 	/* not found in the PATH */
   1510      1.109  christos 	error("%s: not found", basename);
   1511      1.109  christos 	/* NOTREACHED */
   1512      1.109  christos }
   1513      1.109  christos 
   1514      1.109  christos 
   1515        1.1       cgd 
   1516        1.1       cgd /*
   1517        1.1       cgd  * The return command.
   1518      1.109  christos  *
   1519      1.109  christos  * Quoth the POSIX standard:
   1520      1.109  christos  *   The return utility shall cause the shell to stop executing the current
   1521      1.109  christos  *   function or dot script. If the shell is not currently executing
   1522      1.109  christos  *   a function or dot script, the results are unspecified.
   1523      1.109  christos  *
   1524      1.109  christos  * As for the unspecified part, there seems to be no de-facto standard: bash
   1525      1.109  christos  * ignores the return with a warning, zsh ignores the return in interactive
   1526      1.109  christos  * mode but seems to liken it to exit in a script.  (checked May 2014)
   1527      1.109  christos  *
   1528      1.109  christos  * We choose to silently ignore the return.  Older versions of this shell
   1529      1.109  christos  * set evalskip to SKIPFILE causing the shell to (indirectly) exit.  This
   1530      1.109  christos  * had at least the problem of circumventing the check for stopped jobs,
   1531      1.109  christos  * which would occur for exit or ^D.
   1532        1.1       cgd  */
   1533        1.1       cgd 
   1534       1.16       cgd int
   1535       1.68  christos returncmd(int argc, char **argv)
   1536       1.16       cgd {
   1537       1.68  christos 	int ret = argc > 1 ? number(argv[1]) : exitstatus;
   1538        1.1       cgd 
   1539      1.109  christos 	if ((dot_funcnest == 0 && funcnest)
   1540      1.109  christos 	    || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) {
   1541        1.1       cgd 		evalskip = SKIPFUNC;
   1542        1.1       cgd 		skipcount = 1;
   1543      1.109  christos 	} else if (dot_funcnest > 0) {
   1544       1.27  christos 		evalskip = SKIPFILE;
   1545       1.27  christos 		skipcount = 1;
   1546      1.109  christos 	} else {
   1547      1.109  christos 		/* XXX: should a warning be issued? */
   1548      1.109  christos 		ret = 0;
   1549        1.1       cgd 	}
   1550      1.109  christos 
   1551      1.109  christos 	return ret;
   1552        1.1       cgd }
   1553        1.1       cgd 
   1554        1.1       cgd 
   1555       1.16       cgd int
   1556       1.68  christos falsecmd(int argc, char **argv)
   1557       1.16       cgd {
   1558        1.8       jtc 	return 1;
   1559        1.8       jtc }
   1560        1.8       jtc 
   1561        1.8       jtc 
   1562       1.16       cgd int
   1563       1.68  christos truecmd(int argc, char **argv)
   1564       1.16       cgd {
   1565        1.1       cgd 	return 0;
   1566        1.1       cgd }
   1567        1.1       cgd 
   1568        1.1       cgd 
   1569       1.16       cgd int
   1570       1.68  christos execcmd(int argc, char **argv)
   1571       1.16       cgd {
   1572        1.1       cgd 	if (argc > 1) {
   1573       1.20  christos 		struct strlist *sp;
   1574       1.20  christos 
   1575        1.1       cgd 		iflag = 0;		/* exit on error */
   1576        1.7       jtc 		mflag = 0;
   1577        1.7       jtc 		optschanged();
   1578       1.70       dsl 		for (sp = cmdenviron; sp; sp = sp->next)
   1579       1.20  christos 			setvareq(sp->text, VEXPORT|VSTACK);
   1580       1.61  christos 		shellexec(argv + 1, environment(), pathval(), 0, 0);
   1581        1.1       cgd 	}
   1582       1.68  christos 	return 0;
   1583       1.68  christos }
   1584       1.68  christos 
   1585       1.68  christos static int
   1586       1.73    itojun conv_time(clock_t ticks, char *seconds, size_t l)
   1587       1.68  christos {
   1588       1.68  christos 	static clock_t tpm = 0;
   1589       1.68  christos 	clock_t mins;
   1590       1.68  christos 	int i;
   1591       1.68  christos 
   1592       1.68  christos 	if (!tpm)
   1593       1.68  christos 		tpm = sysconf(_SC_CLK_TCK) * 60;
   1594       1.68  christos 
   1595       1.68  christos 	mins = ticks / tpm;
   1596       1.73    itojun 	snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
   1597       1.68  christos 
   1598       1.68  christos 	if (seconds[0] == '6' && seconds[1] == '0') {
   1599       1.68  christos 		/* 59.99995 got rounded up... */
   1600       1.68  christos 		mins++;
   1601       1.73    itojun 		strlcpy(seconds, "0.0", l);
   1602       1.68  christos 		return mins;
   1603       1.68  christos 	}
   1604       1.68  christos 
   1605       1.68  christos 	/* suppress trailing zeros */
   1606       1.68  christos 	i = strlen(seconds) - 1;
   1607       1.68  christos 	for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
   1608       1.68  christos 		seconds[i] = 0;
   1609       1.68  christos 	return mins;
   1610       1.68  christos }
   1611       1.68  christos 
   1612       1.68  christos int
   1613       1.68  christos timescmd(int argc, char **argv)
   1614       1.68  christos {
   1615       1.68  christos 	struct tms tms;
   1616       1.68  christos 	int u, s, cu, cs;
   1617       1.68  christos 	char us[8], ss[8], cus[8], css[8];
   1618       1.68  christos 
   1619       1.68  christos 	nextopt("");
   1620       1.68  christos 
   1621       1.68  christos 	times(&tms);
   1622       1.68  christos 
   1623       1.73    itojun 	u = conv_time(tms.tms_utime, us, sizeof(us));
   1624       1.73    itojun 	s = conv_time(tms.tms_stime, ss, sizeof(ss));
   1625       1.73    itojun 	cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
   1626       1.73    itojun 	cs = conv_time(tms.tms_cstime, css, sizeof(css));
   1627       1.68  christos 
   1628       1.68  christos 	outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
   1629       1.68  christos 		u, us, s, ss, cu, cus, cs, css);
   1630       1.68  christos 
   1631        1.1       cgd 	return 0;
   1632        1.1       cgd }
   1633