Home | History | Annotate | Line # | Download | only in csh
sem.c revision 1.24
      1  1.24  christos /* $NetBSD: sem.c,v 1.24 2004/09/28 16:07:01 christos Exp $ */
      2   1.7       cgd 
      3   1.1       cgd /*-
      4   1.5   mycroft  * Copyright (c) 1980, 1991, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.23       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.11  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.7       cgd #if 0
     35   1.7       cgd static char sccsid[] = "@(#)sem.c	8.1 (Berkeley) 5/31/93";
     36   1.7       cgd #else
     37  1.24  christos __RCSID("$NetBSD: sem.c,v 1.24 2004/09/28 16:07:01 christos Exp $");
     38   1.7       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41  1.19       wiz #include <sys/ioctl.h>
     42   1.1       cgd #include <sys/param.h>
     43   1.1       cgd #include <sys/stat.h>
     44  1.19       wiz 
     45   1.1       cgd #include <errno.h>
     46   1.1       cgd #include <fcntl.h>
     47  1.20       wiz #include <stdarg.h>
     48   1.1       cgd #include <stdlib.h>
     49   1.1       cgd #include <string.h>
     50   1.1       cgd #include <unistd.h>
     51   1.1       cgd 
     52   1.1       cgd #include "csh.h"
     53  1.19       wiz #include "extern.h"
     54   1.1       cgd #include "proc.h"
     55   1.1       cgd 
     56  1.19       wiz static void vffree(int);
     57  1.19       wiz static Char *splicepipe(struct command *t, Char *);
     58  1.19       wiz static void doio(struct command *t, int *, int *);
     59  1.19       wiz static void chkclob(char *);
     60   1.1       cgd 
     61   1.1       cgd void
     62  1.19       wiz execute(struct command *t, int wanttty, int *pipein, int *pipeout)
     63   1.1       cgd {
     64  1.19       wiz     static sigset_t csigset, ocsigset;
     65  1.19       wiz     static int nosigchld = 0, onosigchld = 0;
     66   1.1       cgd     struct biltins *bifunc;
     67  1.19       wiz     int pv[2], pid;
     68  1.21    kleink     sigset_t nsigset;
     69  1.19       wiz     bool forked;
     70   1.1       cgd 
     71   1.5   mycroft     UNREGISTER(forked);
     72   1.5   mycroft     UNREGISTER(bifunc);
     73   1.5   mycroft     UNREGISTER(wanttty);
     74   1.5   mycroft 
     75  1.19       wiz     forked = 0;
     76  1.19       wiz     pid = 0;
     77  1.19       wiz 
     78   1.1       cgd     if (t == 0)
     79   1.1       cgd 	return;
     80   1.1       cgd 
     81   1.1       cgd     if (t->t_dflg & F_AMPERSAND)
     82   1.1       cgd 	wanttty = 0;
     83   1.1       cgd     switch (t->t_dtyp) {
     84   1.1       cgd     case NODE_COMMAND:
     85   1.1       cgd 	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
     86  1.19       wiz 	    (void)Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
     87   1.1       cgd 	if ((t->t_dflg & F_REPEAT) == 0)
     88   1.1       cgd 	    Dfix(t);		/* $ " ' \ */
     89   1.1       cgd 	if (t->t_dcom[0] == 0)
     90   1.1       cgd 	    return;
     91  1.12   mycroft 	/* FALLTHROUGH */
     92   1.1       cgd     case NODE_PAREN:
     93   1.1       cgd 	if (t->t_dflg & F_PIPEOUT)
     94   1.1       cgd 	    mypipe(pipeout);
     95   1.1       cgd 	/*
     96   1.1       cgd 	 * Must do << early so parent will know where input pointer should be.
     97   1.1       cgd 	 * If noexec then this is all we do.
     98   1.1       cgd 	 */
     99   1.1       cgd 	if (t->t_dflg & F_READ) {
    100  1.19       wiz 	    (void)close(0);
    101   1.1       cgd 	    heredoc(t->t_dlef);
    102   1.1       cgd 	    if (noexec)
    103  1.19       wiz 		(void)close(0);
    104   1.1       cgd 	}
    105   1.1       cgd 
    106   1.1       cgd 	set(STRstatus, Strsave(STR0));
    107   1.1       cgd 
    108   1.1       cgd 	/*
    109   1.1       cgd 	 * This mess is the necessary kludge to handle the prefix builtins:
    110   1.1       cgd 	 * nice, nohup, time.  These commands can also be used by themselves,
    111   1.1       cgd 	 * and this is not handled here. This will also work when loops are
    112   1.1       cgd 	 * parsed.
    113   1.1       cgd 	 */
    114   1.1       cgd 	while (t->t_dtyp == NODE_COMMAND)
    115  1.17  christos 	    if (eq(t->t_dcom[0], STRnice)) {
    116  1.17  christos 		if (t->t_dcom[1]) {
    117  1.17  christos 		    if (strchr("+-", t->t_dcom[1][0])) {
    118   1.1       cgd 			if (t->t_dcom[2]) {
    119   1.1       cgd 			    setname("nice");
    120   1.1       cgd 			    t->t_nice =
    121   1.1       cgd 				getn(t->t_dcom[1]);
    122   1.1       cgd 			    lshift(t->t_dcom, 2);
    123   1.1       cgd 			    t->t_dflg |= F_NICE;
    124   1.1       cgd 			}
    125   1.1       cgd 			else
    126   1.1       cgd 			    break;
    127  1.17  christos 		    } else {
    128   1.1       cgd 			t->t_nice = 4;
    129   1.1       cgd 			lshift(t->t_dcom, 1);
    130   1.1       cgd 			t->t_dflg |= F_NICE;
    131   1.1       cgd 		    }
    132  1.17  christos 		} else
    133   1.1       cgd 		    break;
    134  1.17  christos 	    } else if (eq(t->t_dcom[0], STRnohup)) {
    135   1.1       cgd 		if (t->t_dcom[1]) {
    136   1.1       cgd 		    t->t_dflg |= F_NOHUP;
    137   1.1       cgd 		    lshift(t->t_dcom, 1);
    138   1.1       cgd 		}
    139   1.1       cgd 		else
    140   1.1       cgd 		    break;
    141  1.17  christos 	    } else if (eq(t->t_dcom[0], STRtime)) {
    142   1.1       cgd 		if (t->t_dcom[1]) {
    143   1.1       cgd 		    t->t_dflg |= F_TIME;
    144   1.1       cgd 		    lshift(t->t_dcom, 1);
    145   1.1       cgd 		}
    146   1.1       cgd 		else
    147   1.1       cgd 		    break;
    148  1.17  christos 	    } else
    149   1.1       cgd 		break;
    150   1.1       cgd 
    151   1.5   mycroft 	/* is it a command */
    152   1.1       cgd 	if (t->t_dtyp == NODE_COMMAND) {
    153   1.1       cgd 	    /*
    154   1.1       cgd 	     * Check if we have a builtin function and remember which one.
    155   1.1       cgd 	     */
    156   1.1       cgd 	    bifunc = isbfunc(t);
    157  1.18      tron  	    if (noexec && bifunc != NULL) {
    158   1.5   mycroft 		/*
    159   1.5   mycroft 		 * Continue for builtins that are part of the scripting language
    160   1.5   mycroft 		 */
    161   1.5   mycroft 		if (bifunc->bfunct != dobreak   && bifunc->bfunct != docontin &&
    162   1.5   mycroft 		    bifunc->bfunct != doelse    && bifunc->bfunct != doend    &&
    163   1.5   mycroft 		    bifunc->bfunct != doforeach && bifunc->bfunct != dogoto   &&
    164   1.5   mycroft 		    bifunc->bfunct != doif      && bifunc->bfunct != dorepeat &&
    165   1.5   mycroft 		    bifunc->bfunct != doswbrk   && bifunc->bfunct != doswitch &&
    166   1.5   mycroft 		    bifunc->bfunct != dowhile   && bifunc->bfunct != dozip)
    167   1.5   mycroft 		    break;
    168   1.5   mycroft 	    }
    169   1.1       cgd 	}
    170   1.1       cgd 	else {			/* not a command */
    171   1.1       cgd 	    bifunc = NULL;
    172   1.5   mycroft 	    if (noexec)
    173   1.5   mycroft 		break;
    174   1.1       cgd 	}
    175   1.1       cgd 
    176   1.1       cgd 	/*
    177   1.1       cgd 	 * We fork only if we are timed, or are not the end of a parenthesized
    178   1.1       cgd 	 * list and not a simple builtin function. Simple meaning one that is
    179   1.1       cgd 	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
    180   1.1       cgd 	 * fork in some of these cases.
    181   1.1       cgd 	 */
    182   1.1       cgd 	/*
    183   1.1       cgd 	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
    184   1.1       cgd 	 * shell not to change dir!
    185   1.1       cgd 	 */
    186   1.1       cgd 	if (bifunc && (bifunc->bfunct == dochngd ||
    187   1.1       cgd 		       bifunc->bfunct == dopushd ||
    188   1.1       cgd 		       bifunc->bfunct == dopopd))
    189   1.1       cgd 	    t->t_dflg &= ~(F_NICE);
    190   1.5   mycroft 	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
    191   1.1       cgd 	     (!bifunc || t->t_dflg &
    192   1.5   mycroft 	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
    193   1.1       cgd 	/*
    194   1.1       cgd 	 * We have to fork for eval too.
    195   1.1       cgd 	 */
    196   1.5   mycroft 	    (bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
    197  1.16   thorpej 	     bifunc->bfunct == doeval)) {
    198   1.1       cgd 	    if (t->t_dtyp == NODE_PAREN ||
    199   1.1       cgd 		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
    200   1.1       cgd 		forked++;
    201   1.1       cgd 		/*
    202   1.1       cgd 		 * We need to block SIGCHLD here, so that if the process does
    203   1.1       cgd 		 * not die before we can set the process group
    204   1.1       cgd 		 */
    205   1.1       cgd 		if (wanttty >= 0 && !nosigchld) {
    206  1.21    kleink 		    sigemptyset(&nsigset);
    207  1.21    kleink 		    (void)sigaddset(&nsigset, SIGCHLD);
    208  1.21    kleink 		    (void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
    209   1.1       cgd 		    nosigchld = 1;
    210   1.1       cgd 		}
    211   1.1       cgd 
    212   1.1       cgd 		pid = pfork(t, wanttty);
    213   1.1       cgd 		if (pid == 0 && nosigchld) {
    214  1.19       wiz 		    (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
    215   1.1       cgd 		    nosigchld = 0;
    216   1.1       cgd 		}
    217   1.5   mycroft 		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
    218   1.5   mycroft 		    backpid = pid;
    219   1.5   mycroft 
    220   1.1       cgd 	    }
    221   1.1       cgd 	    else {
    222  1.19       wiz 		int ochild, osetintr, ohaderr, odidfds;
    223  1.19       wiz 		int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
    224   1.8   mycroft 		sigset_t osigset;
    225   1.1       cgd 
    226   1.1       cgd 		/*
    227   1.1       cgd 		 * Prepare for the vfork by saving everything that the child
    228   1.1       cgd 		 * corrupts before it exec's. Note that in some signal
    229   1.1       cgd 		 * implementations which keep the signal info in user space
    230   1.1       cgd 		 * (e.g. Sun's) it will also be necessary to save and restore
    231   1.8   mycroft 		 * the current sigaction's for the signals the child touches
    232   1.1       cgd 		 * before it exec's.
    233   1.1       cgd 		 */
    234   1.1       cgd 		if (wanttty >= 0 && !nosigchld && !noexec) {
    235  1.21    kleink 		    sigemptyset(&nsigset);
    236  1.21    kleink 		    (void)sigaddset(&nsigset, SIGCHLD);
    237  1.21    kleink 		    (void)sigprocmask(SIG_BLOCK, &nsigset, &csigset);
    238   1.1       cgd 		    nosigchld = 1;
    239   1.1       cgd 		}
    240  1.21    kleink 		sigemptyset(&nsigset);
    241  1.21    kleink 		(void)sigaddset(&nsigset, SIGCHLD);
    242  1.21    kleink 		(void)sigaddset(&nsigset, SIGINT);
    243  1.21    kleink 		(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
    244   1.1       cgd 		ochild = child;
    245   1.1       cgd 		osetintr = setintr;
    246   1.1       cgd 		ohaderr = haderr;
    247   1.1       cgd 		odidfds = didfds;
    248   1.1       cgd 		oSHIN = SHIN;
    249   1.1       cgd 		oSHOUT = SHOUT;
    250   1.5   mycroft 		oSHERR = SHERR;
    251   1.1       cgd 		oOLDSTD = OLDSTD;
    252   1.1       cgd 		otpgrp = tpgrp;
    253   1.8   mycroft 		ocsigset = csigset;
    254   1.1       cgd 		onosigchld = nosigchld;
    255   1.1       cgd 		Vsav = Vdp = 0;
    256   1.1       cgd 		Vexpath = 0;
    257   1.1       cgd 		Vt = 0;
    258   1.1       cgd 		pid = vfork();
    259   1.1       cgd 
    260   1.1       cgd 		if (pid < 0) {
    261  1.19       wiz 		    (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
    262   1.1       cgd 		    stderror(ERR_NOPROC);
    263   1.1       cgd 		}
    264   1.1       cgd 		forked++;
    265   1.1       cgd 		if (pid) {	/* parent */
    266   1.1       cgd 		    child = ochild;
    267   1.1       cgd 		    setintr = osetintr;
    268   1.1       cgd 		    haderr = ohaderr;
    269   1.1       cgd 		    didfds = odidfds;
    270   1.1       cgd 		    SHIN = oSHIN;
    271   1.1       cgd 		    SHOUT = oSHOUT;
    272   1.5   mycroft 		    SHERR = oSHERR;
    273   1.1       cgd 		    OLDSTD = oOLDSTD;
    274   1.1       cgd 		    tpgrp = otpgrp;
    275   1.8   mycroft 		    csigset = ocsigset;
    276   1.1       cgd 		    nosigchld = onosigchld;
    277   1.1       cgd 
    278   1.1       cgd 		    xfree((ptr_t) Vsav);
    279   1.1       cgd 		    Vsav = 0;
    280   1.1       cgd 		    xfree((ptr_t) Vdp);
    281   1.1       cgd 		    Vdp = 0;
    282   1.1       cgd 		    xfree((ptr_t) Vexpath);
    283   1.1       cgd 		    Vexpath = 0;
    284   1.1       cgd 		    blkfree((Char **) Vt);
    285   1.1       cgd 		    Vt = 0;
    286   1.1       cgd 		    /* this is from pfork() */
    287   1.1       cgd 		    palloc(pid, t);
    288  1.19       wiz 		    (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
    289   1.1       cgd 		}
    290   1.1       cgd 		else {		/* child */
    291   1.1       cgd 		    /* this is from pfork() */
    292  1.19       wiz 		    int pgrp;
    293  1.19       wiz 		    bool ignint = 0;
    294   1.1       cgd 
    295   1.1       cgd 		    if (nosigchld) {
    296  1.19       wiz 		        (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
    297   1.1       cgd 			nosigchld = 0;
    298   1.1       cgd 		    }
    299   1.1       cgd 
    300   1.1       cgd 		    if (setintr)
    301   1.1       cgd 			ignint =
    302   1.1       cgd 			    (tpgrp == -1 &&
    303   1.1       cgd 			     (t->t_dflg & F_NOINTERRUPT))
    304   1.5   mycroft 			    || (gointr && eq(gointr, STRminus));
    305   1.1       cgd 		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
    306   1.1       cgd 		    child++;
    307   1.1       cgd 		    if (setintr) {
    308   1.1       cgd 			setintr = 0;
    309   1.1       cgd 			if (ignint) {
    310  1.19       wiz 			    (void)signal(SIGINT, SIG_IGN);
    311  1.19       wiz 			    (void)signal(SIGQUIT, SIG_IGN);
    312   1.1       cgd 			}
    313   1.1       cgd 			else {
    314  1.19       wiz 			    (void)signal(SIGINT, vffree);
    315  1.19       wiz 			    (void)signal(SIGQUIT, SIG_DFL);
    316   1.1       cgd 			}
    317   1.1       cgd 
    318   1.1       cgd 			if (wanttty >= 0) {
    319  1.19       wiz 			    (void)signal(SIGTSTP, SIG_DFL);
    320  1.19       wiz 			    (void)signal(SIGTTIN, SIG_DFL);
    321  1.19       wiz 			    (void)signal(SIGTTOU, SIG_DFL);
    322   1.1       cgd 			}
    323   1.1       cgd 
    324  1.19       wiz 			(void)signal(SIGTERM, parterm);
    325   1.1       cgd 		    }
    326   1.1       cgd 		    else if (tpgrp == -1 &&
    327   1.1       cgd 			     (t->t_dflg & F_NOINTERRUPT)) {
    328  1.19       wiz 			(void)signal(SIGINT, SIG_IGN);
    329  1.19       wiz 			(void)signal(SIGQUIT, SIG_IGN);
    330   1.1       cgd 		    }
    331   1.1       cgd 
    332   1.1       cgd 		    pgetty(wanttty, pgrp);
    333   1.1       cgd 		    if (t->t_dflg & F_NOHUP)
    334  1.19       wiz 			(void)signal(SIGHUP, SIG_IGN);
    335   1.1       cgd 		    if (t->t_dflg & F_NICE)
    336  1.19       wiz 			(void)setpriority(PRIO_PROCESS, 0, t->t_nice);
    337   1.1       cgd 		}
    338   1.1       cgd 
    339   1.1       cgd 	    }
    340  1.16   thorpej 	}
    341   1.1       cgd 	if (pid != 0) {
    342   1.1       cgd 	    /*
    343   1.1       cgd 	     * It would be better if we could wait for the whole job when we
    344   1.1       cgd 	     * knew the last process had been started.  Pwait, in fact, does
    345   1.1       cgd 	     * wait for the whole job anyway, but this test doesn't really
    346   1.1       cgd 	     * express our intentions.
    347   1.1       cgd 	     */
    348   1.1       cgd 	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
    349  1.19       wiz 		(void)close(pipein[0]);
    350  1.19       wiz 		(void)close(pipein[1]);
    351   1.1       cgd 	    }
    352   1.1       cgd 	    if ((t->t_dflg & F_PIPEOUT) == 0) {
    353   1.1       cgd 		if (nosigchld) {
    354  1.19       wiz 		    (void)sigprocmask(SIG_SETMASK, &csigset, NULL);
    355   1.1       cgd 		    nosigchld = 0;
    356   1.1       cgd 		}
    357   1.1       cgd 		if ((t->t_dflg & F_AMPERSAND) == 0)
    358   1.1       cgd 		    pwait();
    359   1.1       cgd 	    }
    360   1.1       cgd 	    break;
    361   1.1       cgd 	}
    362   1.1       cgd 	doio(t, pipein, pipeout);
    363   1.1       cgd 	if (t->t_dflg & F_PIPEOUT) {
    364  1.19       wiz 	    (void)close(pipeout[0]);
    365  1.19       wiz 	    (void)close(pipeout[1]);
    366   1.1       cgd 	}
    367   1.1       cgd 	/*
    368   1.1       cgd 	 * Perform a builtin function. If we are not forked, arrange for
    369   1.1       cgd 	 * possible stopping
    370   1.1       cgd 	 */
    371   1.1       cgd 	if (bifunc) {
    372   1.1       cgd 	    func(t, bifunc);
    373  1.15   mycroft 	    if (forked)
    374   1.1       cgd 		exitstat();
    375   1.1       cgd 	    break;
    376   1.1       cgd 	}
    377  1.15   mycroft 	if (t->t_dtyp != NODE_PAREN)
    378   1.5   mycroft 	    doexec(NULL, t);
    379   1.1       cgd 	/*
    380   1.1       cgd 	 * For () commands must put new 0,1,2 in FSH* and recurse
    381   1.1       cgd 	 */
    382  1.24  christos 	(void) ioctl(OLDSTD = dcopy(0, FOLDSTD), FIOCLEX, NULL);
    383  1.24  christos 	(void) ioctl(SHOUT = dcopy(1, FSHOUT), FIOCLEX, NULL);
    384  1.24  christos 	(void) ioctl(SHERR = dcopy(2, FSHERR), FIOCLEX, NULL);
    385   1.1       cgd 	(void) close(SHIN);
    386  1.24  christos 
    387   1.1       cgd 	SHIN = -1;
    388   1.1       cgd 	didfds = 0;
    389   1.1       cgd 	wanttty = -1;
    390   1.1       cgd 	t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    391   1.1       cgd 	execute(t->t_dspr, wanttty, NULL, NULL);
    392   1.1       cgd 	exitstat();
    393  1.12   mycroft 	/* NOTREACHED */
    394   1.1       cgd     case NODE_PIPE:
    395   1.1       cgd 	t->t_dcar->t_dflg |= F_PIPEOUT |
    396   1.1       cgd 	    (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
    397   1.1       cgd 	execute(t->t_dcar, wanttty, pipein, pv);
    398   1.1       cgd 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
    399   1.1       cgd 			(F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
    400   1.1       cgd 	if (wanttty > 0)
    401   1.1       cgd 	    wanttty = 0;	/* got tty already */
    402   1.1       cgd 	execute(t->t_dcdr, wanttty, pv, pipeout);
    403   1.1       cgd 	break;
    404   1.1       cgd     case NODE_LIST:
    405   1.1       cgd 	if (t->t_dcar) {
    406   1.1       cgd 	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    407   1.1       cgd 	    execute(t->t_dcar, wanttty, NULL, NULL);
    408   1.1       cgd 	    /*
    409   1.1       cgd 	     * In strange case of A&B make a new job after A
    410   1.1       cgd 	     */
    411   1.1       cgd 	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
    412   1.1       cgd 		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
    413   1.1       cgd 		pendjob();
    414   1.1       cgd 	}
    415   1.1       cgd 	if (t->t_dcdr) {
    416   1.1       cgd 	    t->t_dcdr->t_dflg |= t->t_dflg &
    417   1.1       cgd 		(F_NOFORK | F_NOINTERRUPT);
    418   1.1       cgd 	    execute(t->t_dcdr, wanttty, NULL, NULL);
    419   1.1       cgd 	}
    420   1.1       cgd 	break;
    421   1.1       cgd     case NODE_OR:
    422   1.1       cgd     case NODE_AND:
    423   1.1       cgd 	if (t->t_dcar) {
    424   1.1       cgd 	    t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
    425   1.1       cgd 	    execute(t->t_dcar, wanttty, NULL, NULL);
    426   1.1       cgd 	    if ((getn(value(STRstatus)) == 0) !=
    427   1.1       cgd 		(t->t_dtyp == NODE_AND))
    428   1.1       cgd 		return;
    429   1.1       cgd 	}
    430   1.1       cgd 	if (t->t_dcdr) {
    431   1.1       cgd 	    t->t_dcdr->t_dflg |= t->t_dflg &
    432   1.1       cgd 		(F_NOFORK | F_NOINTERRUPT);
    433   1.1       cgd 	    execute(t->t_dcdr, wanttty, NULL, NULL);
    434   1.1       cgd 	}
    435   1.1       cgd 	break;
    436   1.1       cgd     }
    437   1.1       cgd     /*
    438   1.1       cgd      * Fall through for all breaks from switch
    439   1.1       cgd      *
    440   1.1       cgd      * If there will be no more executions of this command, flush all file
    441   1.1       cgd      * descriptors. Places that turn on the F_REPEAT bit are responsible for
    442   1.1       cgd      * doing donefds after the last re-execution
    443   1.1       cgd      */
    444   1.1       cgd     if (didfds && !(t->t_dflg & F_REPEAT))
    445   1.1       cgd 	donefds();
    446   1.1       cgd }
    447   1.1       cgd 
    448   1.1       cgd static void
    449  1.19       wiz vffree(int i)
    450   1.1       cgd {
    451  1.10       tls     Char **v;
    452   1.1       cgd 
    453   1.5   mycroft     if ((v = gargv) != NULL) {
    454   1.1       cgd 	gargv = 0;
    455   1.1       cgd 	xfree((ptr_t) v);
    456   1.1       cgd     }
    457   1.5   mycroft     if ((v = pargv) != NULL) {
    458   1.1       cgd 	pargv = 0;
    459   1.1       cgd 	xfree((ptr_t) v);
    460   1.1       cgd     }
    461   1.1       cgd     _exit(i);
    462  1.14   mycroft     /* NOTREACHED */
    463   1.1       cgd }
    464   1.1       cgd 
    465   1.1       cgd /*
    466   1.5   mycroft  * Expand and glob the words after an i/o redirection.
    467   1.5   mycroft  * If more than one word is generated, then update the command vector.
    468   1.5   mycroft  *
    469   1.5   mycroft  * This is done differently in all the shells:
    470   1.5   mycroft  * 1. in the bourne shell and ksh globbing is not performed
    471   1.5   mycroft  * 2. Bash/csh say ambiguous
    472   1.5   mycroft  * 3. zsh does i/o to/from all the files
    473   1.5   mycroft  * 4. itcsh concatenates the words.
    474   1.5   mycroft  *
    475   1.5   mycroft  * I don't know what is best to do. I think that Ambiguous is better
    476   1.5   mycroft  * than restructuring the command vector, because the user can get
    477   1.5   mycroft  * unexpected results. In any case, the command vector restructuring
    478   1.5   mycroft  * code is present and the user can choose it by setting noambiguous
    479   1.5   mycroft  */
    480   1.5   mycroft static Char *
    481  1.19       wiz splicepipe(struct command *t, Char *cp /* word after < or > */)
    482   1.5   mycroft {
    483   1.5   mycroft     Char *blk[2];
    484   1.5   mycroft 
    485   1.5   mycroft     if (adrof(STRnoambiguous)) {
    486   1.5   mycroft 	Char **pv;
    487   1.5   mycroft 
    488   1.5   mycroft 	blk[0] = Dfix1(cp); /* expand $ */
    489   1.5   mycroft 	blk[1] = NULL;
    490   1.5   mycroft 
    491   1.5   mycroft 	gflag = 0, tglob(blk);
    492   1.5   mycroft 	if (gflag) {
    493   1.5   mycroft 	    pv = globall(blk);
    494   1.5   mycroft 	    if (pv == NULL) {
    495   1.5   mycroft 		setname(vis_str(blk[0]));
    496   1.5   mycroft 		xfree((ptr_t) blk[0]);
    497   1.5   mycroft 		stderror(ERR_NAME | ERR_NOMATCH);
    498  1.12   mycroft 		/* NOTREACHED */
    499   1.5   mycroft 	    }
    500   1.5   mycroft 	    gargv = NULL;
    501   1.5   mycroft 	    if (pv[1] != NULL) { /* we need to fix the command vector */
    502   1.5   mycroft 		Char **av = blkspl(t->t_dcom, &pv[1]);
    503   1.5   mycroft 		xfree((ptr_t) t->t_dcom);
    504   1.5   mycroft 		t->t_dcom = av;
    505   1.5   mycroft 	    }
    506   1.5   mycroft 	    xfree((ptr_t) blk[0]);
    507   1.5   mycroft 	    blk[0] = pv[0];
    508   1.5   mycroft 	    xfree((ptr_t) pv);
    509   1.5   mycroft 	}
    510   1.5   mycroft     }
    511   1.5   mycroft     else {
    512   1.5   mycroft 	blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR);
    513   1.5   mycroft 	xfree((ptr_t) blk[1]);
    514   1.5   mycroft     }
    515   1.5   mycroft     return(blk[0]);
    516   1.5   mycroft }
    517   1.5   mycroft 
    518   1.5   mycroft /*
    519   1.1       cgd  * Perform io redirection.
    520   1.1       cgd  * We may or maynot be forked here.
    521   1.1       cgd  */
    522   1.1       cgd static void
    523  1.19       wiz doio(struct command *t, int *pipein, int *pipeout)
    524   1.1       cgd {
    525  1.10       tls     Char *cp;
    526  1.19       wiz     int fd, flags;
    527   1.1       cgd 
    528  1.19       wiz     flags = t->t_dflg;
    529   1.1       cgd     if (didfds || (flags & F_REPEAT))
    530   1.1       cgd 	return;
    531   1.1       cgd     if ((flags & F_READ) == 0) {/* F_READ already done */
    532   1.5   mycroft 	if (t->t_dlef) {
    533  1.19       wiz 	    char tmp[MAXPATHLEN+1];
    534   1.1       cgd 
    535   1.1       cgd 	    /*
    536   1.1       cgd 	     * so < /dev/std{in,out,err} work
    537   1.1       cgd 	     */
    538  1.19       wiz 	    (void)dcopy(SHIN, 0);
    539  1.19       wiz 	    (void)dcopy(SHOUT, 1);
    540  1.19       wiz 	    (void)dcopy(SHERR, 2);
    541   1.5   mycroft 	    cp = splicepipe(t, t->t_dlef);
    542  1.22    itojun 	    (void)strlcpy(tmp, short2str(cp), sizeof(tmp));
    543   1.1       cgd 	    xfree((ptr_t) cp);
    544  1.12   mycroft 	    if ((fd = open(tmp, O_RDONLY)) < 0) {
    545   1.1       cgd 		stderror(ERR_SYSTEM, tmp, strerror(errno));
    546  1.12   mycroft 		/* NOTREACHED */
    547  1.12   mycroft 	    }
    548  1.19       wiz 	    (void)dmove(fd, 0);
    549   1.1       cgd 	}
    550   1.1       cgd 	else if (flags & F_PIPEIN) {
    551  1.19       wiz 	    (void)close(0);
    552  1.19       wiz 	    (void)dup(pipein[0]);
    553  1.19       wiz 	    (void)close(pipein[0]);
    554  1.19       wiz 	    (void)close(pipein[1]);
    555   1.1       cgd 	}
    556   1.1       cgd 	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
    557  1.19       wiz 	    (void)close(0);
    558  1.19       wiz 	    (void)open(_PATH_DEVNULL, O_RDONLY);
    559   1.1       cgd 	}
    560   1.1       cgd 	else {
    561  1.19       wiz 	    (void)close(0);
    562  1.19       wiz 	    (void)dup(OLDSTD);
    563  1.19       wiz 	    (void)ioctl(0, FIONCLEX, NULL);
    564   1.1       cgd 	}
    565   1.1       cgd     }
    566   1.5   mycroft     if (t->t_drit) {
    567   1.1       cgd 	char    tmp[MAXPATHLEN+1];
    568   1.1       cgd 
    569   1.5   mycroft 	cp = splicepipe(t, t->t_drit);
    570  1.22    itojun 	(void)strlcpy(tmp, short2str(cp), sizeof(tmp));
    571   1.1       cgd 	xfree((ptr_t) cp);
    572   1.1       cgd 	/*
    573   1.1       cgd 	 * so > /dev/std{out,err} work
    574   1.1       cgd 	 */
    575  1.19       wiz 	(void)dcopy(SHOUT, 1);
    576  1.19       wiz 	(void)dcopy(SHERR, 2);
    577   1.1       cgd 	if ((flags & F_APPEND) &&
    578   1.1       cgd #ifdef O_APPEND
    579   1.1       cgd 	    (fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
    580   1.1       cgd #else
    581   1.1       cgd 	    (fd = open(tmp, O_WRONLY)) >= 0)
    582  1.19       wiz 	    (void)lseek(1, (off_t) 0, SEEK_END);
    583   1.1       cgd #endif
    584   1.1       cgd 	else {
    585   1.1       cgd 	    if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
    586  1.12   mycroft 		if (flags & F_APPEND) {
    587   1.1       cgd 		    stderror(ERR_SYSTEM, tmp, strerror(errno));
    588  1.12   mycroft 		    /* NOTREACHED */
    589  1.12   mycroft 		}
    590   1.1       cgd 		chkclob(tmp);
    591   1.1       cgd 	    }
    592  1.12   mycroft 	    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
    593   1.1       cgd 		stderror(ERR_SYSTEM, tmp, strerror(errno));
    594  1.12   mycroft 		/* NOTREACHED */
    595  1.12   mycroft 	    }
    596   1.1       cgd 	}
    597  1.19       wiz 	(void)dmove(fd, 1);
    598   1.1       cgd     }
    599   1.1       cgd     else if (flags & F_PIPEOUT) {
    600  1.19       wiz 	(void)close(1);
    601  1.19       wiz 	(void)dup(pipeout[1]);
    602   1.1       cgd     }
    603   1.1       cgd     else {
    604  1.19       wiz 	(void)close(1);
    605  1.19       wiz 	(void)dup(SHOUT);
    606  1.19       wiz 	(void)ioctl(1, FIONCLEX, NULL);
    607   1.1       cgd     }
    608   1.1       cgd 
    609  1.19       wiz     (void)close(2);
    610   1.1       cgd     if (flags & F_STDERR) {
    611  1.19       wiz 	(void)dup(1);
    612   1.1       cgd     }
    613   1.1       cgd     else {
    614  1.19       wiz 	(void)dup(SHERR);
    615  1.19       wiz 	(void)ioctl(2, FIONCLEX, NULL);
    616   1.1       cgd     }
    617   1.1       cgd     didfds = 1;
    618   1.1       cgd }
    619   1.1       cgd 
    620   1.1       cgd void
    621  1.19       wiz mypipe(int *pv)
    622   1.1       cgd {
    623   1.1       cgd     if (pipe(pv) < 0)
    624   1.1       cgd 	goto oops;
    625   1.1       cgd     pv[0] = dmove(pv[0], -1);
    626   1.1       cgd     pv[1] = dmove(pv[1], -1);
    627   1.1       cgd     if (pv[0] >= 0 && pv[1] >= 0)
    628   1.1       cgd 	return;
    629   1.1       cgd oops:
    630   1.1       cgd     stderror(ERR_PIPE);
    631  1.12   mycroft     /* NOTREACHED */
    632   1.1       cgd }
    633   1.1       cgd 
    634   1.1       cgd static void
    635  1.19       wiz chkclob(char *cp)
    636   1.1       cgd {
    637   1.1       cgd     struct stat stb;
    638   1.1       cgd 
    639   1.1       cgd     if (stat(cp, &stb) < 0)
    640   1.1       cgd 	return;
    641   1.5   mycroft     if (S_ISCHR(stb.st_mode))
    642   1.1       cgd 	return;
    643   1.1       cgd     stderror(ERR_EXISTS, cp);
    644  1.12   mycroft     /* NOTREACHED */
    645   1.1       cgd }
    646