File ksh93-pipe.dif of Package ksh

--- src/cmd/ksh93/include/defs.h
+++ src/cmd/ksh93/include/defs.h	2011-06-10 17:37:22.852426230 +0200
@@ -154,7 +154,7 @@ struct limits
 	pid_t		bckpid;		/* background process id */ \
 	pid_t		cpid; \
 	pid_t		pipepid; \
-	int32_t		ppid;		/* parent process id of shell */ \
+	pid_t		ppid;		/* parent process id of shell */ \
 	int		topfd; \
 	int		sigmax;		/* maximum number of signals */ \
 	int		savesig; \
--- src/cmd/ksh93/sh/io.c
+++ src/cmd/ksh93/sh/io.c	2011-06-10 18:13:02.511926423 +0200
@@ -1076,15 +1076,18 @@ int	sh_redirect(Shell_t *shp,struct iono
 						message = e_file;
 						goto fail;
 					}
-					if(shp->subshell && dupfd==1 && (sfset(sfstdout,0,0)&SF_STRING))
+					if(shp->subshell && dupfd==1)
 					{
 						if(sfset(sfstdout,0,0)&SF_STRING)
 							sh_subtmpfile(shp);
-						shp->subdup |= 1<< fn;
+						if(shp->comsub==1)
+							shp->subdup |= 1<< fn;
 						dupfd = sffileno(sfstdout);
 					}
 					else if(shp->sftable[dupfd])
 						sfsync(shp->sftable[dupfd]);
+					if(dupfd!=1 && fn < 10)
+						shp->subdup &= ~(1<<fn);
 				}
 				else if(fd=='-' && fname[1]==0)
 				{
--- src/cmd/ksh93/sh/xec.c
+++ src/cmd/ksh93/sh/xec.c	2011-06-10 18:09:32.396425650 +0200
@@ -86,10 +86,11 @@ struct funenv
  * temp file.
  */
 static int	subpipe[3] = {-1};
-static int	subdup;
+static int	subdup,tsetio,usepipe;
 static void iousepipe(Shell_t *shp)
 {
 	int i;
+	usepipe++;
 	fcntl(subpipe[0],F_SETFD,FD_CLOEXEC);
 	subpipe[2] = fcntl(1,F_DUPFD,10);
 	shp->fdstatus[subpipe[2]] = shp->fdstatus[1];
@@ -112,6 +113,7 @@ static void iounpipe(Shell_t *shp)
 {
 	int n;
 	char buff[SF_BUFSIZE];
+	usepipe = 0;
 	close(1);
 	fcntl(subpipe[2], F_DUPFD, 1);
 	shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
@@ -135,6 +137,7 @@ static void iounpipe(Shell_t *shp)
 	}
 	sh_close(subpipe[0]);
 	subpipe[0] = -1;
+	tsetio = 0;
 }
 
 /*
@@ -533,8 +536,10 @@ int sh_eval(register Sfio_t *iop, int mo
 	static Sfio_t *io_save;
 	volatile int traceon=0, lineno=0;
 	int binscript=shp->binscript;
+	char comsub = shp->comsub;
 	io_save = iop; /* preserve correct value across longjmp */
 	shp->binscript = 0;
+	shp->comsub = 0;
 #define SH_TOPFUN	0x8000	/* this is a temporary tksh hack */
 	if (mode & SH_TOPFUN)
 	{
@@ -573,6 +578,7 @@ int sh_eval(register Sfio_t *iop, int mo
 			break;
 	}
 	sh_popcontext(&buff);
+	shp->comsub = comsub;
 	shp->binscript = binscript;
 	if(traceon)
 		sh_onoption(SH_XTRACE);
@@ -1245,13 +1251,16 @@ int sh_exec(register const Shnode_t *t,
 		    {
 			register pid_t parent;
 			int no_fork,jobid;
-			int pipes[2];
+			int pipes[3];
 			if(shp->subshell)
 			{
 				sh_subtmpfile(shp);
-				subpipe[0] = -1;
-				if(shp->comsub==1 && !(shp->fdstatus[1]&IONOSEEK) && sh_pipe(subpipe)>=0)
-					iousepipe(shp);
+				if(!usepipe)
+				{
+					subpipe[0] = -1;
+					if(shp->comsub==1 && !(shp->fdstatus[1]&IONOSEEK) && sh_pipe(subpipe)>=0)
+						iousepipe(shp);
+				}
 				if((type&(FAMP|TFORK))==(FAMP|TFORK))
 					sh_subfork();
 			}
@@ -1291,7 +1300,10 @@ int sh_exec(register const Shnode_t *t,
 #endif /* SHOPT_BGX */
 				nv_getval(RANDNOD);
 				if(type&FCOOP)
+				{
+					pipes[2] = 0;
 					coproc_init(shp,pipes);
+				}
 #if SHOPT_AMP
 				if((type&(FAMP|FINT)) == (FAMP|FINT))
 					parent = sh_ntfork(shp,t,com,&jobid,ntflag);
@@ -1353,6 +1365,8 @@ int sh_exec(register const Shnode_t *t,
 						shp->pipepid = parent;
 					else
 						job_wait(parent);
+					if(usepipe && tsetio && subdup)
+						iounpipe(shp);
 					if(!sh_isoption(SH_MONITOR))
 					{
 						shp->trapnote &= ~SH_SIGIGNORE;
@@ -1496,6 +1510,8 @@ int sh_exec(register const Shnode_t *t,
 			jmpval = sigsetjmp(buff.buff,0);
 			if(jmpval==0)
 			{
+				if(shp->comsub==1)
+					tsetio = 1;
 				sh_redirect(shp,t->fork.forkio,execflg);
 				(t->fork.forktre)->tre.tretyp |= t->tre.tretyp&FSHOWME;
 				sh_exec(t->fork.forktre,flags&~simple);
@@ -1523,6 +1539,8 @@ int sh_exec(register const Shnode_t *t,
 					if(type || !sh_isoption(SH_PIPEFAIL))
 						shp->exitval = type;
 				}
+				if(shp->comsub==1 && subpipe[0]>=0)
+					iounpipe(shp);
 				shp->pipepid = 0;
 				shp->st.ioset = 0;
 				if(simple && was_errexit)
@@ -1580,14 +1598,22 @@ int sh_exec(register const Shnode_t *t,
 		     * All elements of the pipe are started by the parent.
 		     * The last element executes in current environment
 		     */
-			int	pvo[2];	/* old pipe for multi-stage */
-			int	pvn[2];	/* current set up pipe */
+			int	pvo[3];	/* old pipe for multi-stage */
+			int	pvn[3];	/* current set up pipe */
 			int	savepipe = pipejob;
 			int	showme = t->tre.tretyp&FSHOWME;
 			pid_t	savepgid = job.curpgid;
 			job.curpgid = 0;
 			if(shp->subshell)
+			{
 				sh_subtmpfile(shp);
+				if(!usepipe)
+				{
+					subpipe[0] = -1;
+					if(shp->comsub==1 && !(shp->fdstatus[1]&IONOSEEK) && sh_pipe(subpipe)>=0)
+						iousepipe(shp);
+				}
+			}
 			shp->inpipe = pvo;
 			shp->outpipe = pvn;
 			pvo[1] = -1;
@@ -2543,7 +2569,10 @@ pid_t _sh_fork(register pid_t parent,int
 		if(jobid)
 			*jobid = myjob;
 		if(shp->comsub==1 && subpipe[0]>=0)
-			iounpipe(shp);
+		{
+			if(!tsetio || !subdup)
+				iounpipe(shp);
+		}
 		return(parent);
 	}
 #if !_std_malloc
@@ -2602,6 +2631,7 @@ pid_t _sh_fork(register pid_t parent,int
 	if(sig>0)
 		sh_fault(sig);
 	sh_sigcheck();
+	usepipe=0;
 	return(0);
 }
 
openSUSE Build Service is sponsored by