1d6fabe6eSbostic /*-
2548b48c4Sbostic * Copyright (c) 1980, 1991, 1993
3548b48c4Sbostic * The Regents of the University of California. All rights reserved.
4d6fabe6eSbostic *
5d6fabe6eSbostic * %sccs.include.redist.c%
6c1549ed3Sdist */
7c1549ed3Sdist
843fabe3fSedward #ifndef lint
9*fb2be964Schristos static char sccsid[] = "@(#)sem.c 8.3 (Berkeley) 04/29/95";
10d6fabe6eSbostic #endif /* not lint */
1113082428Sbill
129374f148Sbostic #include <sys/param.h>
139374f148Sbostic #include <sys/ioctl.h>
149374f148Sbostic #include <sys/stat.h>
159374f148Sbostic #include <errno.h>
169374f148Sbostic #include <fcntl.h>
179374f148Sbostic #include <stdlib.h>
189374f148Sbostic #include <string.h>
199374f148Sbostic #include <unistd.h>
20c1ce954aSchristos #if __STDC__
21c1ce954aSchristos # include <stdarg.h>
22c1ce954aSchristos #else
23c1ce954aSchristos # include <varargs.h>
24c1ce954aSchristos #endif
25c1ce954aSchristos
26afc71901Sbostic #include "csh.h"
27afc71901Sbostic #include "proc.h"
28afc71901Sbostic #include "extern.h"
29f3328da7Sbostic
3023ceb1ffSchristos static void vffree __P((int));
312ff0ec22Schristos static Char *splicepipe __P((struct command *t, Char *));
3223ceb1ffSchristos static void doio __P((struct command *t, int *, int *));
33e78a8ca9Schristos static void chkclob __P((char *));
3413082428Sbill
35f3328da7Sbostic void
execute(t,wanttty,pipein,pipeout)3613082428Sbill execute(t, wanttty, pipein, pipeout)
3713082428Sbill register struct command *t;
3813082428Sbill int wanttty, *pipein, *pipeout;
3913082428Sbill {
4013082428Sbill bool forked = 0;
4113082428Sbill struct biltins *bifunc;
4213082428Sbill int pid = 0;
4313082428Sbill int pv[2];
445fad6ed4Schristos sigset_t sigset;
4513082428Sbill
465fad6ed4Schristos static sigset_t csigset;
47f3328da7Sbostic
485fad6ed4Schristos static sigset_t ocsigset;
49f3328da7Sbostic static int onosigchld = 0;
50f3328da7Sbostic static int nosigchld = 0;
51f3328da7Sbostic
5250cee248Schristos UNREGISTER(forked);
5350cee248Schristos UNREGISTER(bifunc);
5450cee248Schristos UNREGISTER(wanttty);
5550cee248Schristos
5613082428Sbill if (t == 0)
5713082428Sbill return;
58f3328da7Sbostic
59f3328da7Sbostic if (t->t_dflg & F_AMPERSAND)
6013082428Sbill wanttty = 0;
6113082428Sbill switch (t->t_dtyp) {
6213082428Sbill
635eb3278cSbostic case NODE_COMMAND:
6413082428Sbill if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
65f3328da7Sbostic (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
665eb3278cSbostic if ((t->t_dflg & F_REPEAT) == 0)
6713082428Sbill Dfix(t); /* $ " ' \ */
6813082428Sbill if (t->t_dcom[0] == 0)
6913082428Sbill return;
7013082428Sbill /* fall into... */
7113082428Sbill
725eb3278cSbostic case NODE_PAREN:
735eb3278cSbostic if (t->t_dflg & F_PIPEOUT)
7413082428Sbill mypipe(pipeout);
7513082428Sbill /*
76f3328da7Sbostic * Must do << early so parent will know where input pointer should be.
7713082428Sbill * If noexec then this is all we do.
7813082428Sbill */
795eb3278cSbostic if (t->t_dflg & F_READ) {
8043fabe3fSedward (void) close(0);
8113082428Sbill heredoc(t->t_dlef);
8213082428Sbill if (noexec)
8343fabe3fSedward (void) close(0);
8413082428Sbill }
8513082428Sbill
86f3328da7Sbostic set(STRstatus, Strsave(STR0));
8713082428Sbill
8813082428Sbill /*
89f3328da7Sbostic * This mess is the necessary kludge to handle the prefix builtins:
90f3328da7Sbostic * nice, nohup, time. These commands can also be used by themselves,
91f3328da7Sbostic * and this is not handled here. This will also work when loops are
92f3328da7Sbostic * parsed.
9313082428Sbill */
945eb3278cSbostic while (t->t_dtyp == NODE_COMMAND)
95f3328da7Sbostic if (eq(t->t_dcom[0], STRnice))
9613082428Sbill if (t->t_dcom[1])
97f3328da7Sbostic if (strchr("+-", t->t_dcom[1][0]))
9813082428Sbill if (t->t_dcom[2]) {
9913082428Sbill setname("nice");
100f3328da7Sbostic t->t_nice =
101f3328da7Sbostic getn(t->t_dcom[1]);
10213082428Sbill lshift(t->t_dcom, 2);
1035eb3278cSbostic t->t_dflg |= F_NICE;
104f3328da7Sbostic }
105f3328da7Sbostic else
10613082428Sbill break;
10713082428Sbill else {
10813082428Sbill t->t_nice = 4;
10913082428Sbill lshift(t->t_dcom, 1);
1105eb3278cSbostic t->t_dflg |= F_NICE;
11113082428Sbill }
11213082428Sbill else
11313082428Sbill break;
114f3328da7Sbostic else if (eq(t->t_dcom[0], STRnohup))
11513082428Sbill if (t->t_dcom[1]) {
1165eb3278cSbostic t->t_dflg |= F_NOHUP;
11713082428Sbill lshift(t->t_dcom, 1);
118f3328da7Sbostic }
119f3328da7Sbostic else
12013082428Sbill break;
121f3328da7Sbostic else if (eq(t->t_dcom[0], STRtime))
12213082428Sbill if (t->t_dcom[1]) {
1235eb3278cSbostic t->t_dflg |= F_TIME;
12413082428Sbill lshift(t->t_dcom, 1);
125f3328da7Sbostic }
126f3328da7Sbostic else
12713082428Sbill break;
12813082428Sbill else
12913082428Sbill break;
130f3328da7Sbostic
131336c1b24Schristos /* is it a command */
132f3328da7Sbostic if (t->t_dtyp == NODE_COMMAND) {
13313082428Sbill /*
13413082428Sbill * Check if we have a builtin function and remember which one.
13513082428Sbill */
136f3328da7Sbostic bifunc = isbfunc(t);
137336c1b24Schristos if (noexec) {
138336c1b24Schristos /*
139336c1b24Schristos * Continue for builtins that are part of the scripting language
140336c1b24Schristos */
141336c1b24Schristos if (bifunc->bfunct != dobreak && bifunc->bfunct != docontin &&
142336c1b24Schristos bifunc->bfunct != doelse && bifunc->bfunct != doend &&
143336c1b24Schristos bifunc->bfunct != doforeach && bifunc->bfunct != dogoto &&
144336c1b24Schristos bifunc->bfunct != doif && bifunc->bfunct != dorepeat &&
145336c1b24Schristos bifunc->bfunct != doswbrk && bifunc->bfunct != doswitch &&
146336c1b24Schristos bifunc->bfunct != dowhile && bifunc->bfunct != dozip)
147336c1b24Schristos break;
148336c1b24Schristos }
149f3328da7Sbostic }
150f3328da7Sbostic else { /* not a command */
1519374f148Sbostic bifunc = NULL;
152336c1b24Schristos if (noexec)
153336c1b24Schristos break;
154f3328da7Sbostic }
15513082428Sbill
15613082428Sbill /*
157f3328da7Sbostic * We fork only if we are timed, or are not the end of a parenthesized
158f3328da7Sbostic * list and not a simple builtin function. Simple meaning one that is
159f3328da7Sbostic * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
160f3328da7Sbostic * fork in some of these cases.
16113082428Sbill */
162f3328da7Sbostic /*
163f3328da7Sbostic * Prevent forking cd, pushd, popd, chdir cause this will cause the
164f3328da7Sbostic * shell not to change dir!
165f3328da7Sbostic */
166f3328da7Sbostic if (bifunc && (bifunc->bfunct == dochngd ||
167f3328da7Sbostic bifunc->bfunct == dopushd ||
168f3328da7Sbostic bifunc->bfunct == dopopd))
169f3328da7Sbostic t->t_dflg &= ~(F_NICE);
17050cee248Schristos if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
171f3328da7Sbostic (!bifunc || t->t_dflg &
17250cee248Schristos (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
173f3328da7Sbostic /*
174f3328da7Sbostic * We have to fork for eval too.
175f3328da7Sbostic */
176cce51c35Schristos (bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
177f3328da7Sbostic bifunc->bfunct == doeval))
1785eb3278cSbostic if (t->t_dtyp == NODE_PAREN ||
179afc71901Sbostic t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
180f3328da7Sbostic forked++;
181f3328da7Sbostic /*
182f3328da7Sbostic * We need to block SIGCHLD here, so that if the process does
183f3328da7Sbostic * not die before we can set the process group
184f3328da7Sbostic */
1855e5c7fccSmarc if (wanttty >= 0 && !nosigchld) {
1865fad6ed4Schristos sigemptyset(&sigset);
1875fad6ed4Schristos sigaddset(&sigset, SIGCHLD);
1885fad6ed4Schristos sigprocmask(SIG_BLOCK, &sigset, &csigset);
1895e5c7fccSmarc nosigchld = 1;
1905e5c7fccSmarc }
1915e5c7fccSmarc
1925e5c7fccSmarc pid = pfork(t, wanttty);
1935e5c7fccSmarc if (pid == 0 && nosigchld) {
1945fad6ed4Schristos sigprocmask(SIG_SETMASK, &csigset, NULL);
1955e5c7fccSmarc nosigchld = 0;
1965e5c7fccSmarc }
1972ff0ec22Schristos else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
1982ff0ec22Schristos backpid = pid;
1992ff0ec22Schristos
2005e5c7fccSmarc }
20113082428Sbill else {
20243fabe3fSedward int ochild, osetintr, ohaderr, odidfds;
203cce51c35Schristos int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
2045fad6ed4Schristos sigset_t osigset;
20513082428Sbill
20615d9e91aSlepreau /*
207f3328da7Sbostic * Prepare for the vfork by saving everything that the child
208f3328da7Sbostic * corrupts before it exec's. Note that in some signal
209f3328da7Sbostic * implementations which keep the signal info in user space
210f3328da7Sbostic * (e.g. Sun's) it will also be necessary to save and restore
2115fad6ed4Schristos * the current sigaction's for the signals the child touches
212f3328da7Sbostic * before it exec's.
21315d9e91aSlepreau */
2145e5c7fccSmarc if (wanttty >= 0 && !nosigchld && !noexec) {
2155fad6ed4Schristos sigemptyset(&sigset);
2165fad6ed4Schristos sigaddset(&sigset, SIGCHLD);
2175fad6ed4Schristos sigprocmask(SIG_BLOCK, &sigset, &csigset);
2185e5c7fccSmarc nosigchld = 1;
2195e5c7fccSmarc }
2205fad6ed4Schristos sigemptyset(&sigset);
2215fad6ed4Schristos sigaddset(&sigset, SIGCHLD);
2225fad6ed4Schristos sigaddset(&sigset, SIGINT);
2235fad6ed4Schristos sigprocmask(SIG_BLOCK, &sigset, &osigset);
224f3328da7Sbostic ochild = child;
225f3328da7Sbostic osetintr = setintr;
226f3328da7Sbostic ohaderr = haderr;
227f3328da7Sbostic odidfds = didfds;
228f3328da7Sbostic oSHIN = SHIN;
229f3328da7Sbostic oSHOUT = SHOUT;
230cce51c35Schristos oSHERR = SHERR;
231f3328da7Sbostic oOLDSTD = OLDSTD;
232f3328da7Sbostic otpgrp = tpgrp;
2335fad6ed4Schristos ocsigset = csigset;
234f3328da7Sbostic onosigchld = nosigchld;
235f3328da7Sbostic Vsav = Vdp = 0;
236f3328da7Sbostic Vexpath = 0;
237f3328da7Sbostic Vt = 0;
23813082428Sbill pid = vfork();
239f3328da7Sbostic
24013082428Sbill if (pid < 0) {
2415fad6ed4Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
242f3328da7Sbostic stderror(ERR_NOPROC);
24313082428Sbill }
24413082428Sbill forked++;
24515d9e91aSlepreau if (pid) { /* parent */
246f3328da7Sbostic child = ochild;
247f3328da7Sbostic setintr = osetintr;
248f3328da7Sbostic haderr = ohaderr;
249f3328da7Sbostic didfds = odidfds;
25043fabe3fSedward SHIN = oSHIN;
251f3328da7Sbostic SHOUT = oSHOUT;
252cce51c35Schristos SHERR = oSHERR;
253f3328da7Sbostic OLDSTD = oOLDSTD;
254f3328da7Sbostic tpgrp = otpgrp;
2555fad6ed4Schristos csigset = ocsigset;
256f3328da7Sbostic nosigchld = onosigchld;
257f3328da7Sbostic
258f3328da7Sbostic xfree((ptr_t) Vsav);
259f3328da7Sbostic Vsav = 0;
260f3328da7Sbostic xfree((ptr_t) Vdp);
261f3328da7Sbostic Vdp = 0;
262f3328da7Sbostic xfree((ptr_t) Vexpath);
263f3328da7Sbostic Vexpath = 0;
264f3328da7Sbostic blkfree((Char **) Vt);
265f3328da7Sbostic Vt = 0;
26613082428Sbill /* this is from pfork() */
26713082428Sbill palloc(pid, t);
2685fad6ed4Schristos sigprocmask(SIG_SETMASK, &osigset, NULL);
269f3328da7Sbostic }
270f3328da7Sbostic else { /* child */
27113082428Sbill /* this is from pfork() */
27213082428Sbill int pgrp;
27313082428Sbill bool ignint = 0;
27413082428Sbill
2755e5c7fccSmarc if (nosigchld) {
2765fad6ed4Schristos sigprocmask(SIG_SETMASK, &csigset, NULL);
2775e5c7fccSmarc nosigchld = 0;
2785e5c7fccSmarc }
279f3328da7Sbostic
28013082428Sbill if (setintr)
281f3328da7Sbostic ignint =
282f3328da7Sbostic (tpgrp == -1 &&
283f3328da7Sbostic (t->t_dflg & F_NOINTERRUPT))
28450cee248Schristos || (gointr && eq(gointr, STRminus));
28513082428Sbill pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
28613082428Sbill child++;
28713082428Sbill if (setintr) {
28813082428Sbill setintr = 0;
289f3328da7Sbostic if (ignint) {
290f3328da7Sbostic (void) signal(SIGINT, SIG_IGN);
291f3328da7Sbostic (void) signal(SIGQUIT, SIG_IGN);
292f3328da7Sbostic }
293f3328da7Sbostic else {
294f3328da7Sbostic (void) signal(SIGINT, vffree);
295f3328da7Sbostic (void) signal(SIGQUIT, SIG_DFL);
296f3328da7Sbostic }
297f3328da7Sbostic
29813082428Sbill if (wanttty >= 0) {
29943fabe3fSedward (void) signal(SIGTSTP, SIG_DFL);
30043fabe3fSedward (void) signal(SIGTTIN, SIG_DFL);
30143fabe3fSedward (void) signal(SIGTTOU, SIG_DFL);
30213082428Sbill }
303f3328da7Sbostic
30443fabe3fSedward (void) signal(SIGTERM, parterm);
305f3328da7Sbostic }
306f3328da7Sbostic else if (tpgrp == -1 &&
3075eb3278cSbostic (t->t_dflg & F_NOINTERRUPT)) {
30843fabe3fSedward (void) signal(SIGINT, SIG_IGN);
30943fabe3fSedward (void) signal(SIGQUIT, SIG_IGN);
31013082428Sbill }
311f3328da7Sbostic
312f3328da7Sbostic pgetty(wanttty, pgrp);
3135eb3278cSbostic if (t->t_dflg & F_NOHUP)
31443fabe3fSedward (void) signal(SIGHUP, SIG_IGN);
3155eb3278cSbostic if (t->t_dflg & F_NICE)
316f3328da7Sbostic (void) setpriority(PRIO_PROCESS, 0, t->t_nice);
31713082428Sbill }
31813082428Sbill
31913082428Sbill }
32013082428Sbill if (pid != 0) {
32113082428Sbill /*
322f3328da7Sbostic * It would be better if we could wait for the whole job when we
323f3328da7Sbostic * knew the last process had been started. Pwait, in fact, does
324f3328da7Sbostic * wait for the whole job anyway, but this test doesn't really
325f3328da7Sbostic * express our intentions.
32613082428Sbill */
3275eb3278cSbostic if (didfds == 0 && t->t_dflg & F_PIPEIN) {
32843fabe3fSedward (void) close(pipein[0]);
32943fabe3fSedward (void) close(pipein[1]);
33043fabe3fSedward }
3315eb3278cSbostic if ((t->t_dflg & F_PIPEOUT) == 0) {
3325e5c7fccSmarc if (nosigchld) {
3335fad6ed4Schristos sigprocmask(SIG_SETMASK, &csigset, NULL);
3345e5c7fccSmarc nosigchld = 0;
3355e5c7fccSmarc }
3365eb3278cSbostic if ((t->t_dflg & F_AMPERSAND) == 0)
33713082428Sbill pwait();
3385e5c7fccSmarc }
33913082428Sbill break;
34013082428Sbill }
34113082428Sbill doio(t, pipein, pipeout);
3425eb3278cSbostic if (t->t_dflg & F_PIPEOUT) {
34343fabe3fSedward (void) close(pipeout[0]);
34443fabe3fSedward (void) close(pipeout[1]);
34543fabe3fSedward }
34613082428Sbill /*
347f3328da7Sbostic * Perform a builtin function. If we are not forked, arrange for
348f3328da7Sbostic * possible stopping
34913082428Sbill */
35013082428Sbill if (bifunc) {
35113082428Sbill func(t, bifunc);
35213082428Sbill if (forked)
35313082428Sbill exitstat();
35413082428Sbill break;
35513082428Sbill }
3565eb3278cSbostic if (t->t_dtyp != NODE_PAREN) {
357cce51c35Schristos doexec(NULL, t);
35813082428Sbill /* NOTREACHED */
35913082428Sbill }
36013082428Sbill /*
36113082428Sbill * For () commands must put new 0,1,2 in FSH* and recurse
36213082428Sbill */
36313082428Sbill OLDSTD = dcopy(0, FOLDSTD);
36413082428Sbill SHOUT = dcopy(1, FSHOUT);
365cce51c35Schristos SHERR = dcopy(2, FSHERR);
36643fabe3fSedward (void) close(SHIN);
36743fabe3fSedward SHIN = -1;
36843fabe3fSedward didfds = 0;
36913082428Sbill wanttty = -1;
3705eb3278cSbostic t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
371f3328da7Sbostic execute(t->t_dspr, wanttty, NULL, NULL);
37213082428Sbill exitstat();
37313082428Sbill
3745eb3278cSbostic case NODE_PIPE:
3755eb3278cSbostic t->t_dcar->t_dflg |= F_PIPEOUT |
3765eb3278cSbostic (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
37713082428Sbill execute(t->t_dcar, wanttty, pipein, pv);
378f3328da7Sbostic t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
3795eb3278cSbostic (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
38013082428Sbill if (wanttty > 0)
38113082428Sbill wanttty = 0; /* got tty already */
38213082428Sbill execute(t->t_dcdr, wanttty, pv, pipeout);
38313082428Sbill break;
38413082428Sbill
3855eb3278cSbostic case NODE_LIST:
38613082428Sbill if (t->t_dcar) {
3875eb3278cSbostic t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
388f3328da7Sbostic execute(t->t_dcar, wanttty, NULL, NULL);
38913082428Sbill /*
39013082428Sbill * In strange case of A&B make a new job after A
39113082428Sbill */
3925eb3278cSbostic if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
3935eb3278cSbostic (t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
39413082428Sbill pendjob();
39513082428Sbill }
39613082428Sbill if (t->t_dcdr) {
397f3328da7Sbostic t->t_dcdr->t_dflg |= t->t_dflg &
398f3328da7Sbostic (F_NOFORK | F_NOINTERRUPT);
399f3328da7Sbostic execute(t->t_dcdr, wanttty, NULL, NULL);
40013082428Sbill }
40113082428Sbill break;
40213082428Sbill
4035eb3278cSbostic case NODE_OR:
4045eb3278cSbostic case NODE_AND:
40513082428Sbill if (t->t_dcar) {
4065eb3278cSbostic t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
407f3328da7Sbostic execute(t->t_dcar, wanttty, NULL, NULL);
408f3328da7Sbostic if ((getn(value(STRstatus)) == 0) !=
4095eb3278cSbostic (t->t_dtyp == NODE_AND))
41013082428Sbill return;
41113082428Sbill }
41213082428Sbill if (t->t_dcdr) {
413f3328da7Sbostic t->t_dcdr->t_dflg |= t->t_dflg &
414f3328da7Sbostic (F_NOFORK | F_NOINTERRUPT);
415f3328da7Sbostic execute(t->t_dcdr, wanttty, NULL, NULL);
41613082428Sbill }
41713082428Sbill break;
41813082428Sbill }
41913082428Sbill /*
420f3328da7Sbostic * Fall through for all breaks from switch
42113082428Sbill *
422f3328da7Sbostic * If there will be no more executions of this command, flush all file
423f3328da7Sbostic * descriptors. Places that turn on the F_REPEAT bit are responsible for
424f3328da7Sbostic * doing donefds after the last re-execution
42513082428Sbill */
4265eb3278cSbostic if (didfds && !(t->t_dflg & F_REPEAT))
42713082428Sbill donefds();
42813082428Sbill }
42913082428Sbill
430f3328da7Sbostic static void
vffree(i)43123ceb1ffSchristos vffree(i)
43223ceb1ffSchristos int i;
43313082428Sbill {
434f3328da7Sbostic register Char **v;
43513082428Sbill
43650cee248Schristos if ((v = gargv) != NULL) {
437f3328da7Sbostic gargv = 0;
438f3328da7Sbostic xfree((ptr_t) v);
439f3328da7Sbostic }
44050cee248Schristos if ((v = pargv) != NULL) {
441f3328da7Sbostic pargv = 0;
442f3328da7Sbostic xfree((ptr_t) v);
443f3328da7Sbostic }
44423ceb1ffSchristos _exit(i);
44513082428Sbill }
446f3328da7Sbostic
44713082428Sbill /*
4482ff0ec22Schristos * Expand and glob the words after an i/o redirection.
4492ff0ec22Schristos * If more than one word is generated, then update the command vector.
4502ff0ec22Schristos *
4512ff0ec22Schristos * This is done differently in all the shells:
4522ff0ec22Schristos * 1. in the bourne shell and ksh globbing is not performed
4532ff0ec22Schristos * 2. Bash/csh say ambiguous
4542ff0ec22Schristos * 3. zsh does i/o to/from all the files
4552ff0ec22Schristos * 4. itcsh concatenates the words.
4562ff0ec22Schristos *
4572ff0ec22Schristos * I don't know what is best to do. I think that Ambiguous is better
4582ff0ec22Schristos * than restructuring the command vector, because the user can get
4592ff0ec22Schristos * unexpected results. In any case, the command vector restructuring
4602ff0ec22Schristos * code is present and the user can choose it by setting noambiguous
4612ff0ec22Schristos */
4622ff0ec22Schristos static Char *
splicepipe(t,cp)4632ff0ec22Schristos splicepipe(t, cp)
4642ff0ec22Schristos register struct command *t;
4652ff0ec22Schristos Char *cp; /* word after < or > */
4662ff0ec22Schristos {
4672ff0ec22Schristos Char *blk[2];
4682ff0ec22Schristos
4692ff0ec22Schristos if (adrof(STRnoambiguous)) {
4702ff0ec22Schristos Char **pv;
4712ff0ec22Schristos
4722ff0ec22Schristos blk[0] = Dfix1(cp); /* expand $ */
4732ff0ec22Schristos blk[1] = NULL;
4742ff0ec22Schristos
4752ff0ec22Schristos gflag = 0, tglob(blk);
4762ff0ec22Schristos if (gflag) {
4772ff0ec22Schristos pv = globall(blk);
4782ff0ec22Schristos if (pv == NULL) {
4798d2f70fbSchristos setname(vis_str(blk[0]));
4802ff0ec22Schristos xfree((ptr_t) blk[0]);
4812ff0ec22Schristos stderror(ERR_NAME | ERR_NOMATCH);
4822ff0ec22Schristos }
4832ff0ec22Schristos gargv = NULL;
4842ff0ec22Schristos if (pv[1] != NULL) { /* we need to fix the command vector */
4852ff0ec22Schristos Char **av = blkspl(t->t_dcom, &pv[1]);
4862ff0ec22Schristos xfree((ptr_t) t->t_dcom);
4872ff0ec22Schristos t->t_dcom = av;
4882ff0ec22Schristos }
4892ff0ec22Schristos xfree((ptr_t) blk[0]);
4902ff0ec22Schristos blk[0] = pv[0];
4912ff0ec22Schristos xfree((ptr_t) pv);
4922ff0ec22Schristos }
4932ff0ec22Schristos }
4942ff0ec22Schristos else {
4952ff0ec22Schristos blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR);
4962ff0ec22Schristos xfree((ptr_t) blk[1]);
4972ff0ec22Schristos }
4982ff0ec22Schristos return(blk[0]);
4992ff0ec22Schristos }
5002ff0ec22Schristos
5012ff0ec22Schristos /*
50213082428Sbill * Perform io redirection.
50313082428Sbill * We may or maynot be forked here.
50413082428Sbill */
505f3328da7Sbostic static void
doio(t,pipein,pipeout)50613082428Sbill doio(t, pipein, pipeout)
50713082428Sbill register struct command *t;
50813082428Sbill int *pipein, *pipeout;
50913082428Sbill {
510f3328da7Sbostic register int fd;
5112ff0ec22Schristos register Char *cp;
51213082428Sbill register int flags = t->t_dflg;
51313082428Sbill
5145eb3278cSbostic if (didfds || (flags & F_REPEAT))
51513082428Sbill return;
5165eb3278cSbostic if ((flags & F_READ) == 0) {/* F_READ already done */
5172ff0ec22Schristos if (t->t_dlef) {
518e78a8ca9Schristos char tmp[MAXPATHLEN+1];
519f3328da7Sbostic
520f3328da7Sbostic /*
521f3328da7Sbostic * so < /dev/std{in,out,err} work
522f3328da7Sbostic */
523f3328da7Sbostic (void) dcopy(SHIN, 0);
524f3328da7Sbostic (void) dcopy(SHOUT, 1);
525cce51c35Schristos (void) dcopy(SHERR, 2);
5262ff0ec22Schristos cp = splicepipe(t, t->t_dlef);
527f3328da7Sbostic (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
528e78a8ca9Schristos tmp[MAXPATHLEN] = '\0';
529f3328da7Sbostic xfree((ptr_t) cp);
530f3328da7Sbostic if ((fd = open(tmp, O_RDONLY)) < 0)
531f3328da7Sbostic stderror(ERR_SYSTEM, tmp, strerror(errno));
532f3328da7Sbostic (void) dmove(fd, 0);
533f3328da7Sbostic }
534f3328da7Sbostic else if (flags & F_PIPEIN) {
535f3328da7Sbostic (void) close(0);
53643fabe3fSedward (void) dup(pipein[0]);
53743fabe3fSedward (void) close(pipein[0]);
53843fabe3fSedward (void) close(pipein[1]);
539f3328da7Sbostic }
540f3328da7Sbostic else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
54143fabe3fSedward (void) close(0);
542f3328da7Sbostic (void) open(_PATH_DEVNULL, O_RDONLY);
543f3328da7Sbostic }
544f3328da7Sbostic else {
545f3328da7Sbostic (void) close(0);
546cde959b4Sedward (void) dup(OLDSTD);
5479374f148Sbostic (void) ioctl(0, FIONCLEX, NULL);
54813082428Sbill }
549f3328da7Sbostic }
5502ff0ec22Schristos if (t->t_drit) {
551e78a8ca9Schristos char tmp[MAXPATHLEN+1];
552f3328da7Sbostic
5532ff0ec22Schristos cp = splicepipe(t, t->t_drit);
554e78a8ca9Schristos (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
555e78a8ca9Schristos tmp[MAXPATHLEN] = '\0';
556f3328da7Sbostic xfree((ptr_t) cp);
557f3328da7Sbostic /*
558f3328da7Sbostic * so > /dev/std{out,err} work
559f3328da7Sbostic */
560f3328da7Sbostic (void) dcopy(SHOUT, 1);
561cce51c35Schristos (void) dcopy(SHERR, 2);
562f3328da7Sbostic if ((flags & F_APPEND) &&
563f3328da7Sbostic #ifdef O_APPEND
564e78a8ca9Schristos (fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
565f3328da7Sbostic #else
566e78a8ca9Schristos (fd = open(tmp, O_WRONLY)) >= 0)
567f3328da7Sbostic (void) lseek(1, (off_t) 0, L_XTND);
568f3328da7Sbostic #endif
569f3328da7Sbostic else {
570f3328da7Sbostic if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
5715eb3278cSbostic if (flags & F_APPEND)
572e78a8ca9Schristos stderror(ERR_SYSTEM, tmp, strerror(errno));
573f3328da7Sbostic chkclob(tmp);
57413082428Sbill }
575*fb2be964Schristos if ((fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
576e78a8ca9Schristos stderror(ERR_SYSTEM, tmp, strerror(errno));
57713082428Sbill }
578f3328da7Sbostic (void) dmove(fd, 1);
579f3328da7Sbostic }
580f3328da7Sbostic else if (flags & F_PIPEOUT) {
581f3328da7Sbostic (void) close(1);
58243fabe3fSedward (void) dup(pipeout[1]);
583f3328da7Sbostic }
584f3328da7Sbostic else {
585f3328da7Sbostic (void) close(1);
586cde959b4Sedward (void) dup(SHOUT);
5879374f148Sbostic (void) ioctl(1, FIONCLEX, NULL);
588f3328da7Sbostic }
58913082428Sbill
59043fabe3fSedward (void) close(2);
591f3328da7Sbostic if (flags & F_STDERR) {
59243fabe3fSedward (void) dup(1);
593f3328da7Sbostic }
594f3328da7Sbostic else {
595cce51c35Schristos (void) dup(SHERR);
5969374f148Sbostic (void) ioctl(2, FIONCLEX, NULL);
597f3328da7Sbostic }
59813082428Sbill didfds = 1;
59913082428Sbill }
60013082428Sbill
601f3328da7Sbostic void
mypipe(pv)60213082428Sbill mypipe(pv)
60313082428Sbill register int *pv;
60413082428Sbill {
60513082428Sbill
60613082428Sbill if (pipe(pv) < 0)
60713082428Sbill goto oops;
60813082428Sbill pv[0] = dmove(pv[0], -1);
60913082428Sbill pv[1] = dmove(pv[1], -1);
61013082428Sbill if (pv[0] >= 0 && pv[1] >= 0)
61113082428Sbill return;
61213082428Sbill oops:
613f3328da7Sbostic stderror(ERR_PIPE);
61413082428Sbill }
61513082428Sbill
616f3328da7Sbostic static void
chkclob(cp)61713082428Sbill chkclob(cp)
618e78a8ca9Schristos register char *cp;
61913082428Sbill {
62013082428Sbill struct stat stb;
62113082428Sbill
622e78a8ca9Schristos if (stat(cp, &stb) < 0)
62313082428Sbill return;
6242ff0ec22Schristos if (S_ISCHR(stb.st_mode))
62513082428Sbill return;
626e78a8ca9Schristos stderror(ERR_EXISTS, cp);
62713082428Sbill }
628