Blame SOURCES/ksh-20120801-macro.patch

503c26
diff -up ksh-20120801/src/cmd/ksh93/include/io.h.macro ksh-20120801/src/cmd/ksh93/include/io.h
503c26
--- ksh-20120801/src/cmd/ksh93/include/io.h.macro	2012-07-18 16:12:38.000000000 +0200
503c26
+++ ksh-20120801/src/cmd/ksh93/include/io.h	2013-07-04 16:14:05.809595966 +0200
503c26
@@ -81,6 +81,7 @@ extern void 	sh_iosave(Shell_t *, int,in
503c26
 extern int 	sh_iovalidfd(Shell_t*, int);
503c26
 extern int 	sh_inuse(Shell_t*, int);
503c26
 extern void 	sh_iounsave(Shell_t*);
503c26
+extern void	iounpipe(Shell_t*);
503c26
 extern int	sh_chkopen(const char*);
503c26
 extern int	sh_ioaccess(int,int);
503c26
 extern int	sh_devtofd(const char*);
503c26
diff -up ksh-20120801/src/cmd/ksh93/sh/subshell.c.macro ksh-20120801/src/cmd/ksh93/sh/subshell.c
503c26
--- ksh-20120801/src/cmd/ksh93/sh/subshell.c.macro	2013-07-04 16:14:05.783595751 +0200
503c26
+++ ksh-20120801/src/cmd/ksh93/sh/subshell.c	2013-07-04 16:15:46.673432991 +0200
503c26
@@ -171,7 +171,7 @@ void sh_subfork(void)
503c26
 {
503c26
 	register struct subshell *sp = subshell_data;
503c26
 	Shell_t	*shp = sp->shp;
503c26
-	int	curenv = shp->curenv;
503c26
+	int	curenv = shp->curenv, comsub=shp->comsub;
503c26
 	pid_t pid;
503c26
 	char *trap = shp->st.trapcom[0];
503c26
 	if(trap)
503c26
@@ -204,7 +204,7 @@ void sh_subfork(void)
503c26
 		shp->comsub = 0;
503c26
 		SH_SUBSHELLNOD->nvalue.s = 0;
503c26
 		sp->subpid=0;
503c26
-		shp->st.trapcom[0] = trap;
503c26
+		shp->st.trapcom[0] = (comsub==2?NULL:trap);
503c26
 		shp->savesig = 0;
503c26
 	}
503c26
 }
503c26
@@ -743,7 +743,6 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
503c26
 		fchdir(shp->pwdfd);
503c26
 	}
503c26
 	shp->subshare = sp->subshare;
503c26
-	shp->comsub = sp->comsub;
503c26
 	shp->subdup = sp->subdup;
503c26
 #if SHOPT_COSHELL
503c26
 	shp->coshell = sp->coshell;
503c26
@@ -773,7 +772,12 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_
503c26
 	if(nsig>0)
503c26
 		kill(getpid(),nsig);
503c26
 	if(sp->subpid)
503c26
+	{
503c26
 		job_wait(sp->subpid);
503c26
+		if(comsub>1)
503c26
+			iounpipe(shp);
503c26
+	}
503c26
+	shp->comsub = sp->comsub;
503c26
 	if(comsub && iop && sp->pipefd<0)
503c26
 		sfseek(iop,(off_t)0,SEEK_SET);
503c26
 	if(shp->trapnote)
503c26
diff -up ksh-20120801/src/cmd/ksh93/sh/xec.c.macro ksh-20120801/src/cmd/ksh93/sh/xec.c
503c26
--- ksh-20120801/src/cmd/ksh93/sh/xec.c.macro	2013-07-04 16:14:05.800595891 +0200
503c26
+++ ksh-20120801/src/cmd/ksh93/sh/xec.c	2013-07-04 16:14:05.810595975 +0200
503c26
@@ -102,11 +102,11 @@ struct funenv
503c26
  * temp file.
503c26
  */
503c26
 static int      subpipe[3],subdup,tsetio,usepipe;
503c26
-static void iounpipe(Shell_t*);
503c26
+void iounpipe(Shell_t*);
503c26
 
503c26
-static int iousepipe(Shell_t *shp)
503c26
+int iousepipe(Shell_t *shp)
503c26
 {
503c26
-	int i;
503c26
+	int fd=sffileno(sfstdout),i,err=errno;
503c26
 	if(usepipe)
503c26
 	{
503c26
 		usepipe++;
503c26
@@ -115,13 +115,18 @@ static int iousepipe(Shell_t *shp)
503c26
 	if(sh_rpipe(subpipe) < 0)
503c26
 		return(0);
503c26
 	usepipe++;
503c26
-	fcntl(subpipe[0],F_SETFD,FD_CLOEXEC);
503c26
-	subpipe[2] = sh_fcntl(1,F_DUPFD,10);
503c26
-	fcntl(subpipe[2],F_SETFD,FD_CLOEXEC);
503c26
+	if(shp->comsub!=1)
503c26
+	{
503c26
+		subpipe[2] = sh_fcntl(subpipe[1],F_DUPFD,10);
503c26
+		sh_close(subpipe[1]);
503c26
+		return(1);
503c26
+	}
503c26
+	subpipe[2] = sh_fcntl(fd,F_dupfd_cloexec,10);
503c26
 	shp->fdstatus[subpipe[2]] = shp->fdstatus[1];
503c26
-	close(1);
503c26
-	fcntl(subpipe[1],F_DUPFD,1);
503c26
-	shp->fdstatus[1] = shp->fdstatus[subpipe[1]];
503c26
+	while(close(fd)<0 && errno==EINTR)
503c26
+		errno = err;
503c26
+	fcntl(subpipe[1],F_DUPFD,fd);
503c26
+	shp->fdstatus[1] = shp->fdstatus[subpipe[1]]&~IOCLEX;
503c26
 	sh_close(subpipe[1]);
503c26
 	if(subdup=shp->subdup) for(i=0; i < 10; i++)
503c26
 	{
503c26
@@ -135,14 +140,23 @@ static int iousepipe(Shell_t *shp)
503c26
 	return(1);
503c26
 }
503c26
 
503c26
-static void iounpipe(Shell_t *shp)
503c26
+void iounpipe(Shell_t *shp)
503c26
 {
503c26
-	int n;
503c26
+	int fd=sffileno(sfstdout),n,err=errno;
503c26
 	char buff[SF_BUFSIZE];
503c26
-	close(1);
503c26
-	fcntl(subpipe[2], F_DUPFD, 1);
503c26
-	shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
503c26
+	if(!usepipe)
503c26
+		return;
503c26
 	--usepipe;
503c26
+	if(shp->comsub>1)
503c26
+	{
503c26
+		sh_close(subpipe[2]);
503c26
+		while(read(subpipe[0],buff,sizeof(buff))>0);
503c26
+		goto done;
503c26
+	}
503c26
+	while(close(fd)<0 && errno==EINTR)
503c26
+		errno = err;
503c26
+	fcntl(subpipe[2], F_DUPFD, fd);
503c26
+	shp->fdstatus[1] = shp->fdstatus[subpipe[2]];
503c26
 	if(subdup) for(n=0; n < 10; n++)
503c26
 	{
503c26
 		if(subdup&(1<
503c26
@@ -174,6 +188,7 @@ static void iounpipe(Shell_t *shp)
503c26
 		else if(errno!=EINTR)
503c26
 			break;
503c26
 	}
503c26
+done:
503c26
 	sh_close(subpipe[0]);
503c26
 	subpipe[0] = -1;
503c26
 	tsetio = 0;
503c26
@@ -725,7 +740,7 @@ static void unset_instance(Namval_t *nq,
503c26
 }
503c26
 
503c26
 #if SHOPT_COSHELL
503c26
-uintmax_t	coused;
503c26
+static uintmax_t	coused;
503c26
 /*
503c26
  * print out function definition
503c26
  */
503c26
@@ -1619,10 +1634,14 @@ int sh_exec(register const Shnode_t *t,
503c26
 			if(shp->subshell)
503c26
 			{
503c26
 				sh_subtmpfile(shp);
503c26
-				if(shp->comsub==1 && !(shp->fdstatus[1]&IONOSEEK))
503c26
-					unpipe=iousepipe(shp);
503c26
 				if((type&(FAMP|TFORK))==(FAMP|TFORK))
503c26
-					sh_subfork();
503c26
+				{
503c26
+					if(shp->comsub && !(shp->fdstatus[1]&IONOSEEK))
503c26
+					{
503c26
+						unpipe = iousepipe(shp);
503c26
+						sh_subfork();
503c26
+					}
503c26
+				}
503c26
 			}
503c26
 			no_fork = !ntflag && !(type&(FAMP|FPOU)) && !shp->subshell &&
503c26
 			    !(shp->st.trapcom[SIGINT] && *shp->st.trapcom[SIGINT]) &&
503c26
@@ -3495,8 +3514,7 @@ static void sh_funct(Shell_t *shp,Namval
503c26
 	struct funenv fun;
503c26
 	char *fname = nv_getval(SH_FUNNAMENOD);
503c26
 	struct Level	*lp =(struct Level*)(SH_LEVELNOD->nvfun);
503c26
-	int		level, pipepid=shp->pipepid, comsub=shp->comsub;
503c26
-	shp->comsub = 0;
503c26
+	int		level, pipepid=shp->pipepid;
503c26
 	shp->pipepid = 0;
503c26
 	sh_stats(STAT_FUNCT);
503c26
 	if(!lp->hdr.disc)
503c26
@@ -3539,7 +3557,6 @@ static void sh_funct(Shell_t *shp,Namval
503c26
 	lp->maxlevel = level;
503c26
 	SH_LEVELNOD->nvalue.s = lp->maxlevel;
503c26
 	shp->last_root = nv_dict(DOTSHNOD);
503c26
-	shp->comsub = comsub;
503c26
 #if 0
503c26
 	nv_putval(SH_FUNNAMENOD,shp->st.funname,NV_NOFREE);
503c26
 #else