xref: /dragonfly/contrib/tcsh-6/sh.sem.c (revision 6e5c5008)
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.sem.c,v 3.88 2014/09/08 18:52:15 christos Exp $ */
2 /*
3  * sh.sem.c: I/O redirections and job forking. A touchy issue!
4  *	     Most stuff with builtins is incorrect
5  */
6 /*-
7  * Copyright (c) 1980, 1991 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include "sh.h"
35 
36 RCSID("$tcsh: sh.sem.c,v 3.88 2014/09/08 18:52:15 christos Exp $")
37 
38 #include "tc.h"
39 #include "tw.h"
40 #ifdef WINNT_NATIVE
41 #include "nt.const.h"
42 #endif /*WINNT_NATIVE*/
43 
44 #ifdef CLOSE_ON_EXEC
45 # ifndef SUNOS4
46 #  ifndef CLEX_DUPS
47 #   define CLEX_DUPS
48 #  endif /* CLEX_DUPS */
49 # endif /* !SUNOS4 */
50 #endif /* CLOSE_ON_EXEC */
51 
52 #if defined(__sparc__) || defined(sparc)
53 # if !defined(MACH) && SYSVREL == 0 && !defined(Lynx) && !defined(BSD4_4) && !defined(__linux__) && !defined(__GNU__) && !defined(__GLIBC__)
54 #  include <vfork.h>
55 # endif /* !MACH && SYSVREL == 0 && !Lynx && !BSD4_4 && !glibc */
56 #endif /* __sparc__ || sparc */
57 
58 #ifdef VFORK
59 static	void		vffree		(int);
60 #endif
61 static	Char		*splicepipe	(struct command *, Char *);
62 static	void		 doio		(struct command *, int *, int *);
63 static	void		 chkclob	(const char *);
64 
65 /*
66  * C shell
67  */
68 
69 /*
70  * For SVR4, there are problems with pipelines having the first process as
71  * the group leader.  The problem occurs when the first process exits before
72  * the others have a chance to setpgid().  This is because in SVR4 you can't
73  * have a zombie as a group leader.  The solution I have used is to reverse
74  * the order in which pipelines are started, making the last process the
75  * group leader.  (Note I am not using 'pipeline' in the generic sense -- I
76  * mean processes connected by '|'.)  I don't know yet if this causes other
77  * problems.
78  *
79  * All the changes for this are in execute(), and are enclosed in
80  * '#ifdef BACKPIPE'
81  *
82  * David Dawes (dawes@physics.su.oz.au) Oct 1991
83  */
84 
85 /*VARARGS 1*/
86 void
87 execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
88     int do_glob)
89 {
90     int    forked = 0;
91     const struct biltins * volatile bifunc;
92     pid_t pid = 0;
93     int     pv[2];
94     sigset_t set;
95     static sigset_t csigset;
96 #ifdef VFORK
97     static int onosigchld = 0;
98 #endif /* VFORK */
99     static int nosigchld = 0;
100 
101     (void) &wanttty;
102     (void) &forked;
103     (void) &bifunc;
104 
105     if (t == 0)
106 	return;
107 
108 #ifdef WINNT_NATIVE
109     {
110         if ((varval(STRNTslowexec) == STRNULL) &&
111             !t->t_dcdr && !t->t_dcar && !t->t_dflg && !didfds &&
112             (intty || intact) && (t->t_dtyp == NODE_COMMAND) &&
113 	    !isbfunc(t)) {
114 	    if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
115 		(void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
116 	    Dfix(t);
117             if (nt_try_fast_exec(t) == 0)
118                 return;
119         }
120     }
121 #endif /* WINNT_NATIVE */
122 
123     /*
124      * Ed hutchins@sgi.com & Dominic dbg@sgi.com
125      * Sat Feb 25 03:13:11 PST 1995
126      * try implicit cd if we have a 1 word command
127      */
128     if (implicit_cd && (intty || intact) && t->t_dcom && t->t_dcom[0] &&
129 	 t->t_dcom[0][0] && (blklen(t->t_dcom) == 1) && !noexec) {
130 	Char *sCName;
131 	struct stat stbuf;
132 	char *pathname;
133 
134 	sCName = dollar(t->t_dcom[0]);
135 	if (sCName != NULL && sCName[0] == '~') {
136 	    struct Strbuf buf = Strbuf_INIT;
137 	    const Char *name_end;
138 
139 	    for (name_end = sCName + 1; *name_end != '\0' && *name_end != '/';
140 		 name_end++)
141 		continue;
142 	    if (name_end != sCName + 1) {
143 		Char *name, *home;
144 
145 		name = Strnsave(sCName + 1, name_end - (sCName + 1));
146 		home = gethdir(name);
147 		if (home != NULL) {
148 		    Strbuf_append(&buf, home);
149 		    xfree(home);
150 		} else
151 		    Strbuf_append(&buf, name);
152 		xfree(name);
153 	    } else
154 		Strbuf_append(&buf, varval(STRhome));
155 	    Strbuf_append(&buf, name_end);
156 	    xfree(sCName);
157 	    sCName = Strbuf_finish(&buf);
158 	}
159 
160 	pathname = short2str(sCName);
161 	xfree(sCName);
162 	/* if this is a dir, tack a "cd" on as the first arg */
163 	if (pathname != NULL &&
164 	    ((stat(pathname, &stbuf) != -1 && S_ISDIR(stbuf.st_mode))
165 #ifdef WINNT_NATIVE
166 	     || (pathname[0] && pathname[1] == ':' && pathname[2] == '\0')
167 #endif /* WINNT_NATIVE */
168 	     )) {
169 	    Char *vCD[2];
170 	    Char **ot_dcom = t->t_dcom;
171 
172 	    vCD[0] = Strsave(STRcd);
173 	    vCD[1] = NULL;
174 	    t->t_dcom = blkspl(vCD, ot_dcom);
175 	    xfree(ot_dcom);
176 	    if (implicit_cd > 1) {
177 		blkpr(t->t_dcom);
178 		xputchar( '\n' );
179 	    }
180 	}
181     }
182 
183     /*
184      * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
185      * Don't check for wantty > 0...
186      */
187     if (t->t_dflg & F_AMPERSAND)
188 	wanttty = 0;
189     switch (t->t_dtyp) {
190 
191     case NODE_COMMAND:
192 	if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
193 	    memmove(t->t_dcom[0], t->t_dcom[0] + 1,
194 		    (Strlen(t->t_dcom[0] + 1) + 1) * sizeof (*t->t_dcom[0]));
195 	if ((t->t_dflg & F_REPEAT) == 0)
196 	    Dfix(t);		/* $ " ' \ */
197 	if (t->t_dcom[0] == 0) {
198 	    return;
199 	}
200 	/*FALLTHROUGH*/
201 
202     case NODE_PAREN:
203 #ifdef BACKPIPE
204 	if (t->t_dflg & F_PIPEIN)
205 	    mypipe(pipein);
206 #else /* !BACKPIPE */
207 	if (t->t_dflg & F_PIPEOUT)
208 	    mypipe(pipeout);
209 #endif /* BACKPIPE */
210 	/*
211 	 * Must do << early so parent will know where input pointer should be.
212 	 * If noexec then this is all we do.
213 	 */
214 	if (t->t_dflg & F_READ) {
215 	    xclose(0);
216 	    heredoc(t->t_dlef);
217 	    if (noexec)
218 		xclose(0);
219 	}
220 
221 	setcopy(STRstatus, STR0, VAR_READWRITE);
222 
223 	/*
224 	 * This mess is the necessary kludge to handle the prefix builtins:
225 	 * nice, nohup, time.  These commands can also be used by themselves,
226 	 * and this is not handled here. This will also work when loops are
227 	 * parsed.
228 	 */
229 	while (t->t_dtyp == NODE_COMMAND)
230 	    if (eq(t->t_dcom[0], STRnice)) {
231 		if (t->t_dcom[1]) {
232 		    if (strchr("+-", t->t_dcom[1][0])) {
233 			if (t->t_dcom[2]) {
234 			    setname("nice");
235 			    t->t_nice = (unsigned char)getn(t->t_dcom[1]);
236 			    lshift(t->t_dcom, 2);
237 			    t->t_dflg |= F_NICE;
238 			}
239 			else
240 			    break;
241 		    }
242 		    else {
243 			t->t_nice = 4;
244 			lshift(t->t_dcom, 1);
245 			t->t_dflg |= F_NICE;
246 		    }
247 		}
248 		else
249 		    break;
250 	    }
251 	    else if (eq(t->t_dcom[0], STRnohup)) {
252 		if (t->t_dcom[1]) {
253 		    t->t_dflg |= F_NOHUP;
254 		    lshift(t->t_dcom, 1);
255 		}
256 		else
257 		    break;
258 	    }
259 	    else if (eq(t->t_dcom[0], STRhup)) {
260 		if (t->t_dcom[1]) {
261 		    t->t_dflg |= F_HUP;
262 		    lshift(t->t_dcom, 1);
263 		}
264 		else
265 		    break;
266 	    }
267 	    else if (eq(t->t_dcom[0], STRtime)) {
268 		if (t->t_dcom[1]) {
269 		    t->t_dflg |= F_TIME;
270 		    lshift(t->t_dcom, 1);
271 		}
272 		else
273 		    break;
274 	    }
275 #ifdef F_VER
276 	    else if (eq(t->t_dcom[0], STRver))
277 		if (t->t_dcom[1] && t->t_dcom[2]) {
278 		    setname("ver");
279 		    t->t_systype = getv(t->t_dcom[1]);
280 		    lshift(t->t_dcom, 2);
281 		    t->t_dflg |= F_VER;
282 		}
283 		else
284 		    break;
285 #endif  /* F_VER */
286 	    else
287 		break;
288 
289 	/* is it a command */
290 	if (t->t_dtyp == NODE_COMMAND) {
291 	    /*
292 	     * Check if we have a builtin function and remember which one.
293 	     */
294 	    bifunc = isbfunc(t);
295  	    if (noexec) {
296 		/*
297 		 * Continue for builtins that are part of the scripting language
298 		 */
299 		if (bifunc == NULL)
300 		    break;
301 		if (bifunc->bfunct != (bfunc_t)dobreak	&&
302 		    bifunc->bfunct != (bfunc_t)docontin	&&
303 		    bifunc->bfunct != (bfunc_t)doelse	&&
304 		    bifunc->bfunct != (bfunc_t)doend	&&
305 		    bifunc->bfunct != (bfunc_t)doforeach&&
306 		    bifunc->bfunct != (bfunc_t)dogoto	&&
307 		    bifunc->bfunct != (bfunc_t)doif	&&
308 		    bifunc->bfunct != (bfunc_t)dorepeat	&&
309 		    bifunc->bfunct != (bfunc_t)doswbrk	&&
310 		    bifunc->bfunct != (bfunc_t)doswitch	&&
311 		    bifunc->bfunct != (bfunc_t)dowhile	&&
312 		    bifunc->bfunct != (bfunc_t)dozip)
313 		    break;
314 	    }
315 	}
316 	else {			/* not a command */
317 	    bifunc = NULL;
318 	    if (noexec)
319 		break;
320 	}
321 
322 	/*
323 	 * GrP Executing a command - run jobcmd hook
324 	 * Don't run for builtins
325 	 * Don't run if we're not in a tty
326 	 * Don't run if we're not really executing
327 	 */
328 	/*
329 	 * CR  -  Charles Ross Aug 2005
330 	 * added "isoutatty".
331 	 * The new behavior is that the jobcmd won't be executed
332 	 * if stdout (SHOUT) isnt attached to a tty.. IE when
333 	 * redirecting, or using backquotes etc..
334 	 */
335 	if (t->t_dtyp == NODE_COMMAND && !bifunc && !noexec && intty && isoutatty) {
336 	    Char *cmd = unparse(t);
337 
338 	    cleanup_push(cmd, xfree);
339 	    job_cmd(cmd);
340 	    cleanup_until(cmd);
341 	}
342 
343 	/*
344 	 * We fork only if we are timed, or are not the end of a parenthesized
345 	 * list and not a simple builtin function. Simple meaning one that is
346 	 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
347 	 * fork in some of these cases.
348 	 */
349 #ifdef BACKPIPE
350 	/*
351 	 * Can't have NOFORK for the tail of a pipe - because it is not the
352 	 * last command spawned (even if it is at the end of a parenthesised
353 	 * list).
354 	 */
355 	if (t->t_dflg & F_PIPEIN)
356 	    t->t_dflg &= ~(F_NOFORK);
357 #else
358 	/*
359 	 * "command | builtin" may cause major misbehaviour as noted in
360 	 * in the BUGS file entry
361 	 * Subject: Redirected input to built-in functions misbehaves badly
362 	 * forking when the builtin is the end of the pipe corrects the
363 	 * problem.
364 	 */
365 	if (bifunc && (t->t_dflg & F_PIPEIN))
366 	    t->t_dflg &= ~(F_NOFORK);
367 #endif /* BACKPIPE */
368 	/*
369 	 * Prevent forking cd, pushd, popd, chdir cause this will cause the
370 	 * shell not to change dir! (XXX: but only for nice?)
371 	 */
372 	if (bifunc && (bifunc->bfunct == (bfunc_t)dochngd ||
373 		       bifunc->bfunct == (bfunc_t)dopushd ||
374 		       bifunc->bfunct == (bfunc_t)dopopd))
375 	    t->t_dflg &= ~(F_NICE);
376 
377 	if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
378 	     (!bifunc || t->t_dflg &
379 	      (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP | F_HUP)))) ||
380 	/*
381 	 * We have to fork for eval too.
382 	 */
383 	    (bifunc && (t->t_dflg & F_PIPEIN) != 0 &&
384 	     bifunc->bfunct == (bfunc_t)doeval)) {
385 #ifdef VFORK
386 	    if (t->t_dtyp == NODE_PAREN ||
387 		t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc)
388 #endif /* VFORK */
389 	    {
390 		forked++;
391 		/*
392 		 * We need to block SIGCHLD here, so that if the process does
393 		 * not die before we can set the process group
394 		 */
395 		if (wanttty >= 0 && !nosigchld) {
396 		    sigemptyset(&set);
397 		    sigaddset(&set, SIGCHLD);
398 		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
399 
400 		    nosigchld = 1;
401 		}
402 
403 		pid = pfork(t, wanttty);
404 		if (pid == 0 && nosigchld) {
405 		    sigprocmask(SIG_SETMASK, &csigset, NULL);
406 		    nosigchld = 0;
407 		}
408 		else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
409 		    backpid = pid;
410 	    }
411 
412 #ifdef VFORK
413 	    else {
414 		int     ochild, osetintr, ohaderr, odidfds;
415 		int     oSHIN, oSHOUT, oSHDIAG, oOLDSTD, otpgrp;
416 		int     oisoutatty, oisdiagatty;
417 		sigset_t oset, ocsigset;
418 # ifndef CLOSE_ON_EXEC
419 		int     odidcch;
420 # endif  /* !CLOSE_ON_EXEC */
421 
422 		/*
423 		 * Prepare for the vfork by saving everything that the child
424 		 * corrupts before it exec's. Note that in some signal
425 		 * implementations which keep the signal info in user space
426 		 * (e.g. Sun's) it will also be necessary to save and restore
427 		 * the current sigvec's for the signals the child touches
428 		 * before it exec's.
429 		 */
430 
431 		/*
432 		 * Sooooo true... If this is a Sun, save the sigvec's. (Skip
433 		 * Gilbrech - 11/22/87)
434 		 */
435 # ifdef SAVESIGVEC
436 		struct sigaction savesv[NSIGSAVED];
437 		sigset_t savesm;
438 
439 # endif /* SAVESIGVEC */
440 		if (wanttty >= 0 && !nosigchld && !noexec) {
441 		    sigemptyset(&set);
442 		    sigaddset(&set, SIGCHLD);
443 		    (void)sigprocmask(SIG_BLOCK, &set, &csigset);
444 		    nosigchld = 1;
445 		}
446 		sigemptyset(&set);
447 		sigaddset(&set, SIGCHLD);
448 		sigaddset(&set, SIGINT);
449 		(void)sigprocmask(SIG_BLOCK, &set, &oset);
450 		ochild = child;
451 		osetintr = setintr;
452 		ohaderr = haderr;
453 		odidfds = didfds;
454 # ifndef CLOSE_ON_EXEC
455 		odidcch = didcch;
456 # endif /* !CLOSE_ON_EXEC */
457 		oSHIN = SHIN;
458 		oSHOUT = SHOUT;
459 		oSHDIAG = SHDIAG;
460 		oOLDSTD = OLDSTD;
461 		otpgrp = tpgrp;
462 		oisoutatty = isoutatty;
463 		oisdiagatty = isdiagatty;
464 		ocsigset = csigset;
465 		onosigchld = nosigchld;
466 		Vsav = Vdp = 0;
467 		Vexpath = 0;
468 		Vt = 0;
469 # ifdef SAVESIGVEC
470 		savesigvec(savesv, savesm);
471 # endif /* SAVESIGVEC */
472 		if (use_fork)
473 		    pid = fork();
474 		else
475 		    pid = vfork();
476 
477 		if (pid < 0) {
478 # ifdef SAVESIGVEC
479 		    restoresigvec(savesv, savesm);
480 # endif /* SAVESIGVEC */
481 		    sigprocmask(SIG_SETMASK, &oset, NULL);
482 		    stderror(ERR_NOPROC);
483 		}
484 		forked++;
485 		if (pid) {	/* parent */
486 # ifdef SAVESIGVEC
487 		    restoresigvec(savesv, savesm);
488 # endif /* SAVESIGVEC */
489 		    child = ochild;
490 		    setintr = osetintr;
491 		    haderr = ohaderr;
492 		    didfds = odidfds;
493 		    SHIN = oSHIN;
494 # ifndef CLOSE_ON_EXEC
495 		    didcch = odidcch;
496 # endif /* !CLOSE_ON_EXEC */
497 		    SHOUT = oSHOUT;
498 		    SHDIAG = oSHDIAG;
499 		    OLDSTD = oOLDSTD;
500 		    tpgrp = otpgrp;
501 		    isoutatty = oisoutatty;
502 		    isdiagatty = oisdiagatty;
503 		    csigset = ocsigset;
504 		    nosigchld = onosigchld;
505 
506 		    xfree(Vsav);
507 		    Vsav = 0;
508 		    xfree(Vdp);
509 		    Vdp = 0;
510 		    xfree(Vexpath);
511 		    Vexpath = 0;
512 		    blk_cleanup(Vt);
513 		    Vt = 0;
514 		    /* this is from pfork() */
515 		    palloc(pid, t);
516 		    sigprocmask(SIG_SETMASK, &oset, NULL);
517 		}
518 		else {		/* child */
519 		    /* this is from pfork() */
520 		    pid_t pgrp;
521 		    int    ignint = 0;
522 		    if (nosigchld) {
523 			sigprocmask(SIG_SETMASK, &csigset, NULL);
524 			nosigchld = 0;
525 		    }
526 
527 		    if (setintr)
528 			ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
529 				|| (gointr && eq(gointr, STRminus));
530 		    pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
531 		    child++;
532 		    if (setintr) {
533 			setintr = 0;
534 /*
535  * casts made right for SunOS 4.0 by Douglas C. Schmidt
536  * <schmidt%sunshine.ics.uci.edu@ROME.ICS.UCI.EDU>
537  * (thanks! -- PWP)
538  *
539  * ignint ifs cleaned by Johan Widen <mcvax!osiris.sics.se!jw@uunet.UU.NET>
540  * (thanks again)
541  */
542 			if (ignint) {
543 			    (void) signal(SIGINT, SIG_IGN);
544 			    (void) signal(SIGQUIT, SIG_IGN);
545 			}
546 			else {
547 			    (void) signal(SIGINT, vffree);
548 			    (void) signal(SIGQUIT, SIG_DFL);
549 			}
550 # ifdef BSDJOBS
551 			if (wanttty >= 0) {
552 			    (void) signal(SIGTSTP, SIG_DFL);
553 			    (void) signal(SIGTTIN, SIG_DFL);
554 			    (void) signal(SIGTTOU, SIG_DFL);
555 			}
556 # endif /* BSDJOBS */
557 
558 			sigaction(SIGTERM, &parterm, NULL);
559 		    }
560 		    else if (tpgrp == -1 &&
561 			     (t->t_dflg & F_NOINTERRUPT)) {
562 			(void) signal(SIGINT, SIG_IGN);
563 			(void) signal(SIGQUIT, SIG_IGN);
564 		    }
565 
566 		    pgetty(wanttty, pgrp);
567 
568 		    if (t->t_dflg & F_NOHUP)
569 			(void) signal(SIGHUP, SIG_IGN);
570 		    if (t->t_dflg & F_HUP)
571 			(void) signal(SIGHUP, SIG_DFL);
572 		    if (t->t_dflg & F_NICE) {
573 			int nval = SIGN_EXTEND_CHAR(t->t_nice);
574 # if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
575 			if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
576 				stderror(ERR_SYSTEM, "setpriority",
577 				    strerror(errno));
578 # else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
579 			(void) nice(nval);
580 # endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
581 		    }
582 # ifdef F_VER
583 		    if (t->t_dflg & F_VER) {
584 			tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
585 			dohash(NULL, NULL);
586 		    }
587 # endif /* F_VER */
588 		}
589 
590 	    }
591 #endif /* VFORK */
592 	}
593 	if (pid != 0) {
594 	    /*
595 	     * It would be better if we could wait for the whole job when we
596 	     * knew the last process had been started.  Pwait, in fact, does
597 	     * wait for the whole job anyway, but this test doesn't really
598 	     * express our intentions.
599 	     */
600 #ifdef BACKPIPE
601 	    if (didfds == 0 && t->t_dflg & F_PIPEOUT) {
602 		xclose(pipeout[0]);
603 		xclose(pipeout[1]);
604 	    }
605 	    if ((t->t_dflg & F_PIPEIN) != 0)
606 		break;
607 #else /* !BACKPIPE */
608 	    if (didfds == 0 && t->t_dflg & F_PIPEIN) {
609 		xclose(pipein[0]);
610 		xclose(pipein[1]);
611 	    }
612 	    if ((t->t_dflg & F_PIPEOUT) != 0)
613 		break;
614 #endif /* BACKPIPE */
615 
616 	    if (nosigchld) {
617 		sigprocmask(SIG_SETMASK, &csigset, NULL);
618 		nosigchld = 0;
619 	    }
620 	    if ((t->t_dflg & F_AMPERSAND) == 0)
621 		pwait();
622 	    break;
623 	}
624 
625 	doio(t, pipein, pipeout);
626 #ifdef BACKPIPE
627 	if (t->t_dflg & F_PIPEIN) {
628 	    xclose(pipein[0]);
629 	    xclose(pipein[1]);
630 	}
631 #else /* !BACKPIPE */
632 	if (t->t_dflg & F_PIPEOUT) {
633 	    xclose(pipeout[0]);
634 	    xclose(pipeout[1]);
635 	}
636 #endif /* BACKPIPE */
637 	/*
638 	 * Perform a builtin function. If we are not forked, arrange for
639 	 * possible stopping
640 	 */
641 	if (bifunc) {
642 	    if (forked) {
643 		func(t, bifunc);
644 		exitstat();
645 	    } else {
646 		jmp_buf_t oldexit;
647 		int ohaderr = haderr;
648 
649 		getexit(oldexit);
650 		if (setexit() == 0)
651 		    func(t, bifunc);
652 		resexit(oldexit);
653 		haderr = ohaderr;
654 
655 		if (adrof(STRprintexitvalue)) {
656 		    int rv = getn(varval(STRstatus));
657 		    if (rv != 0)
658 			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
659 		}
660 	    }
661 	    break;
662 	}
663 	if (t->t_dtyp != NODE_PAREN) {
664 	    doexec(t, do_glob);
665 	    /* NOTREACHED */
666 	}
667 	/*
668 	 * For () commands must put new 0,1,2 in FSH* and recurse
669 	 */
670 	if ((OLDSTD = dcopy(0, FOLDSTD)) >= 0)
671 	    (void)close_on_exec(OLDSTD, 1);
672 	if ((SHOUT = dcopy(1, FSHOUT)) >= 0) {
673 	    (void)close_on_exec(SHOUT, 1);
674 	    isoutatty = isatty(SHOUT);
675 	}
676 	if ((SHDIAG = dcopy(2, FSHDIAG)) >= 0) {
677 	    (void)close_on_exec(SHDIAG, 1);
678 	    isdiagatty = isatty(SHDIAG);
679     	}
680 	xclose(SHIN);
681 	SHIN = -1;
682 #ifndef CLOSE_ON_EXEC
683 	didcch = 0;
684 #else
685 	(void) close_on_exec(FSHOUT, 1);
686 	(void) close_on_exec(FSHDIAG, 1);
687 	(void) close_on_exec(FOLDSTD, 1);
688 #endif /* !CLOSE_ON_EXEC */
689 	didfds = 0;
690 	wanttty = -1;
691 	t->t_dspr->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
692 	execute(t->t_dspr, wanttty, NULL, NULL, do_glob);
693 	exitstat();
694 
695     case NODE_PIPE:
696 #ifdef BACKPIPE
697 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
698 	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
699 	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
700 	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
701 	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
702 	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
703 #else /* !BACKPIPE */
704 	t->t_dcar->t_dflg |= F_PIPEOUT | (t->t_dflg &
705 	    (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT | F_BACKQ));
706 	execute(t->t_dcar, wanttty, pipein, pv, do_glob);
707 	t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
708 	    (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT | F_BACKQ));
709 	execute(t->t_dcdr, wanttty, pv, pipeout, do_glob);
710 #endif /* BACKPIPE */
711 	break;
712 
713     case NODE_LIST:
714 	if (t->t_dcar) {
715 	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
716 	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
717 	    /*
718 	     * In strange case of A&B make a new job after A
719 	     */
720 	    if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
721 		(t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
722 		pendjob();
723 	}
724 	if (t->t_dcdr) {
725 	    t->t_dcdr->t_dflg |= t->t_dflg &
726 		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
727 	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
728 	}
729 	break;
730 
731     case NODE_OR:
732     case NODE_AND:
733 	if (t->t_dcar) {
734 	    t->t_dcar->t_dflg |= t->t_dflg & (F_NOINTERRUPT | F_BACKQ);
735 	    execute(t->t_dcar, wanttty, NULL, NULL, do_glob);
736 	    if ((getn(varval(STRstatus)) == 0) !=
737 		(t->t_dtyp == NODE_AND)) {
738 		return;
739 	    }
740 	}
741 	if (t->t_dcdr) {
742 	    t->t_dcdr->t_dflg |= t->t_dflg &
743 		(F_NOFORK | F_NOINTERRUPT | F_BACKQ);
744 	    execute(t->t_dcdr, wanttty, NULL, NULL, do_glob);
745 	}
746 	break;
747 
748     default:
749 	break;
750     }
751     /*
752      * Fall through for all breaks from switch
753      *
754      * If there will be no more executions of this command, flush all file
755      * descriptors. Places that turn on the F_REPEAT bit are responsible for
756      * doing donefds after the last re-execution
757      */
758     if (didfds && !(t->t_dflg & F_REPEAT))
759 	donefds();
760 }
761 
762 #ifdef VFORK
763 static void
764 /*ARGSUSED*/
765 vffree(int snum)
766 {
767     USE(snum);
768 
769     _exit(1);
770 }
771 #endif /* VFORK */
772 
773 /*
774  * Expand and glob the words after an i/o redirection.
775  * If more than one word is generated, then update the command vector.
776  *
777  * This is done differently in all the shells:
778  * 1. in the bourne shell and ksh globbing is not performed
779  * 2. Bash/csh say ambiguous
780  * 3. zsh does i/o to/from all the files
781  * 4. itcsh concatenates the words.
782  *
783  * I don't know what is best to do. I think that Ambiguous is better
784  * than restructuring the command vector, because the user can get
785  * unexpected results. In any case, the command vector restructuring
786  * code is present and the user can choose it by setting noambiguous
787  */
788 static Char *
789 splicepipe(struct command *t, Char *cp)
790 {
791     Char *blk[2];
792 
793     if (adrof(STRnoambiguous)) {
794 	Char **pv;
795 	int gflag;
796 
797 	blk[0] = Dfix1(cp); /* expand $ */
798 	blk[1] = NULL;
799 
800 	gflag = tglob(blk);
801 	if (gflag) {
802 	    pv = globall(blk, gflag);
803 	    if (pv == NULL) {
804 		setname(short2str(blk[0]));
805 		xfree(blk[0]);
806 		stderror(ERR_NAME | ERR_NOMATCH);
807 	    }
808 	    if (pv[1] != NULL) { /* we need to fix the command vector */
809 		Char **av = blkspl(t->t_dcom, &pv[1]);
810 		xfree(t->t_dcom);
811 		t->t_dcom = av;
812 	    }
813 	    xfree(blk[0]);
814 	    blk[0] = pv[0];
815 	    xfree(pv);
816 	}
817     }
818     else {
819 	Char *buf;
820 
821 	buf = Dfix1(cp);
822 	cleanup_push(buf, xfree);
823 	blk[0] = globone(buf, G_ERROR);
824 	cleanup_until(buf);
825     }
826     return(blk[0]);
827 }
828 
829 /*
830  * Perform io redirection.
831  * We may or maynot be forked here.
832  */
833 static void
834 doio(struct command *t, int *pipein, int *pipeout)
835 {
836     int fd;
837     Char *cp;
838     unsigned long flags = t->t_dflg;
839 
840     if (didfds || (flags & F_REPEAT))
841 	return;
842     if ((flags & F_READ) == 0) {/* F_READ already done */
843 	if (t->t_dlef) {
844 	    char *tmp;
845 
846 	    /*
847 	     * so < /dev/std{in,out,err} work
848 	     */
849 	    (void) dcopy(SHIN, 0);
850 	    (void) dcopy(SHOUT, 1);
851 	    (void) dcopy(SHDIAG, 2);
852 	    cp = splicepipe(t, t->t_dlef);
853 	    tmp = strsave(short2str(cp));
854 	    xfree(cp);
855 	    cleanup_push(tmp, xfree);
856 	    if ((fd = xopen(tmp, O_RDONLY|O_LARGEFILE)) < 0)
857 		stderror(ERR_SYSTEM, tmp, strerror(errno));
858 	    cleanup_until(tmp);
859 	    /* allow input files larger than 2Gb  */
860 #ifndef WINNT_NATIVE
861 	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
862 #endif /*!WINNT_NATIVE*/
863 	    (void) dmove(fd, 0);
864 	}
865 	else if (flags & F_PIPEIN) {
866 	    xclose(0);
867 	    TCSH_IGNORE(dup(pipein[0]));
868 	    xclose(pipein[0]);
869 	    xclose(pipein[1]);
870 	}
871 	else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
872 	    xclose(0);
873 	    (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
874 	}
875 	else {
876 	    xclose(0);
877 	    TCSH_IGNORE(dup(OLDSTD));
878 #if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
879 	    /*
880 	     * PWP: Unlike Bezerkeley 4.3, FIONCLEX for Pyramid is preserved
881 	     * across dup()s, so we have to UNSET it here or else we get a
882 	     * command with NO stdin, stdout, or stderr at all (a bad thing
883 	     * indeed)
884 	     */
885 	    (void) close_on_exec(0, 0);
886 #endif /* CLOSE_ON_EXEC && CLEX_DUPS */
887 	}
888     }
889     if (t->t_drit) {
890 	char *tmp;
891 
892 	cp = splicepipe(t, t->t_drit);
893 	tmp = strsave(short2str(cp));
894 	xfree(cp);
895 	cleanup_push(tmp, xfree);
896 	/*
897 	 * so > /dev/std{out,err} work
898 	 */
899 	(void) dcopy(SHOUT, 1);
900 	(void) dcopy(SHDIAG, 2);
901 	if ((flags & F_APPEND) != 0) {
902 #ifdef O_APPEND
903 	    fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
904 #else /* !O_APPEND */
905 	    fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
906 	    (void) lseek(fd, (off_t) 0, L_XTND);
907 #endif /* O_APPEND */
908 	}
909 	else
910 	    fd = 0;
911 	if ((flags & F_APPEND) == 0 || fd == -1) {
912 	    if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
913 		if (flags & F_APPEND)
914 		    stderror(ERR_SYSTEM, tmp, strerror(errno));
915 		chkclob(tmp);
916 	    }
917 	    if ((fd = xcreat(tmp, 0666)) < 0)
918 		stderror(ERR_SYSTEM, tmp, strerror(errno));
919 	    /* allow input files larger than 2Gb  */
920 #ifndef WINNT_NATIVE
921 	    (void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_LARGEFILE);
922 #endif /*!WINNT_NATIVE*/
923 	}
924 	cleanup_until(tmp);
925 	(void) dmove(fd, 1);
926 	is1atty = isatty(1);
927     }
928     else if (flags & F_PIPEOUT) {
929 	xclose(1);
930 	TCSH_IGNORE(dup(pipeout[1]));
931 	is1atty = 0;
932     }
933     else {
934 	xclose(1);
935 	TCSH_IGNORE(dup(SHOUT));
936 	is1atty = isoutatty;
937 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
938 	(void) close_on_exec(1, 0);
939 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
940     }
941 
942     xclose(2);
943     if (flags & F_STDERR) {
944 	TCSH_IGNORE(dup(1));
945 	is2atty = is1atty;
946     }
947     else {
948 	TCSH_IGNORE(dup(SHDIAG));
949 	is2atty = isdiagatty;
950 # if defined(CLOSE_ON_EXEC) && defined(CLEX_DUPS)
951 	(void) close_on_exec(2, 0);
952 # endif /* CLOSE_ON_EXEC && CLEX_DUPS */
953     }
954     didfds = 1;
955 }
956 
957 void
958 mypipe(int *pv)
959 {
960 
961     if (pipe(pv) < 0)
962 	goto oops;
963     (void)close_on_exec(pv[0] = dmove(pv[0], -1), 1);
964     (void)close_on_exec(pv[1] = dmove(pv[1], -1), 1);
965     if (pv[0] >= 0 && pv[1] >= 0)
966 	return;
967     if (pv[0] >= 0)
968 	xclose(pv[0]);
969     if (pv[1] >= 0)
970 	xclose(pv[1]);
971 oops:
972     stderror(ERR_PIPE);
973 }
974 
975 static void
976 chkclob(const char *cp)
977 {
978     struct stat stb;
979 
980     if (stat(cp, &stb) < 0)
981 	return;
982     if (S_ISCHR(stb.st_mode))
983 	return;
984     stderror(ERR_EXISTS, cp);
985 }
986