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