zsh-workers
 help / color / mirror / code / Atom feed
* Re: read_poll RE: PATCH: Zpty cleanup (merge 13061 with 13116)
@ 2000-11-08 14:09 Sven Wischnowsky
  2000-11-08 14:46 ` Andrej Borsenkow
  0 siblings, 1 reply; 14+ messages in thread
From: Sven Wischnowsky @ 2000-11-08 14:09 UTC (permalink / raw)
  To: zsh-workers


Andrej Borsenkow wrote:

> Unless I completely miss something:
> 
> read_poll tries to read from fd and stores the character it has read in
> readchar; that is cmd->read when called from checkptycmd. But this value does
> not seem to be used anywhere in read loop that looks like read_poll simply
> eats up this readahead char ...

See zpty.c:474 and following lines.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: PATCH: read_poll Re: PATCH: Zpty cleanup (merge 13061 with 13116)
@ 2000-11-09  8:12 Sven Wischnowsky
  0 siblings, 0 replies; 14+ messages in thread
From: Sven Wischnowsky @ 2000-11-09  8:12 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> On Nov 8,  3:58pm, Bart Schaefer wrote:
> } Subject: PATCH: read_poll Re: PATCH: Zpty cleanup (merge 13061 with 13116)
> }
> } You're correct.  The code Sven referenced at line 474 needs to be repeated
> } after line 484.
> 
> Sven's apparently also sent a patch for this which hasn't propagated
> through the mailing list yet -- he committed the change.

And it still hasn't. Hm, that's weird, makes me wonder what happened
with the other mails I sent yesterday afternoon (not to the list).

I'll just change the `?????' in the ChangeLog to `unposted' (and
change it again if the mail re-appears).

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: PATCH: read_poll Re: PATCH: Zpty cleanup (merge 13061 with 13116)
@ 2000-11-09  8:09 Sven Wischnowsky
  0 siblings, 0 replies; 14+ messages in thread
From: Sven Wischnowsky @ 2000-11-09  8:09 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> On Nov 8, 11:20am, Sven Wischnowsky wrote:
> }
> } Bart Schaefer wrote:
> } 
> } > However, that does not mean that the rest of the tests should be skipped
> } > when select() returns 0.  A return of 0 means the select() timed out,
> } > which (apparently) might happen under Cygwin even if there actually are
> } > characters available to be read.  Peter/Andrej, is that the case?
> } 
> } I don't know about Cygwin, but that blocking read (line 1381) is
> } exactly the test I wanted to avoid.
> 
> The read on line 1381 is *non-*blocking.  Since polltty is now always zero
> when called from zpty, the setblock_fd() call is made before the read() is
> attempted.  That was the patch I first committed which started me down the
> road of merging in your changes.

Ouch. Right, hadn't thought about the change to polltty. Sorry.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: PATCH: Zpty cleanup (merge 13061 with 13116)
@ 2000-11-08 10:20 Sven Wischnowsky
  2000-11-08 10:42 ` Andrej Borsenkow
  0 siblings, 1 reply; 14+ messages in thread
From: Sven Wischnowsky @ 2000-11-08 10:20 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> ...
> 
> > There were some things in [read_poll()] I think were really wrong,
> > like testing `select() > 1' and like trying all possible tests if the
> > first tests worked but returned zero.
> 
> ...
> 
> However, that does not mean that the rest of the tests should be skipped
> when select() returns 0.  A return of 0 means the select() timed out,
> which (apparently) might happen under Cygwin even if there actually are
> characters available to be read.  Peter/Andrej, is that the case?

I don't know about Cygwin, but that blocking read (line 1381) is
exactly the test I wanted to avoid. When building the patch I had a
case where zpty was waiting in checkptycmd() because of that (select()
correctly had returned zero, saying that there wasn't anything to be
read, the `timeout' being zero after all means that the select should
just do a poll, not wait).

I don't know if I can reproduce it, maybe I'll try at the weekend.

> ...
> 
> [Late-breaking note:  *Somebody* should have noticed that read_poll()
> calls gettyinfo() and settyinfo(), which of course affects SHTTY, and
> has nothing whatever to do with the pty.  So polltty=1 is just a very
> expensive no-op for zpty, and it really must pass 0.]

Ahem. Oops.

Hmhm. I wasn't too happy with calling read_poll() in zpty.c
anyway. The problem I wanted to solve is, of course, that a zpty
command should be marked as `finished' if there is still input to be
read even though the OS tells us that the process has exited (which
can happen and is very ugly).

I don't have any particular comments or patches for the other things. Yet.


Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 14+ messages in thread
* PATCH: Misc. zpty tweaks, plus commentary
@ 2000-11-04 23:29 Bart Schaefer
  2000-11-05  1:59 ` Bart Schaefer
  2000-11-05  4:35 ` Bart Schaefer
  0 siblings, 2 replies; 14+ messages in thread
From: Bart Schaefer @ 2000-11-04 23:29 UTC (permalink / raw)
  To: zsh-workers

The first hunk below simply adds strerror output to a couple of warning
messages, so one has a better idea of why things went wrong.

The second hunk passes (!cmd->block) as the final argument of read_poll().
Without this, `zpty -b foo bar; zpty -t foo' would hang until `bar' produced
output (which it might never do).  With it, I suspect that on systems that
don't HAVE_SELECT you can get a nonzero return from zpty -t even when some
output is still available; but I can't test that and I don't know another
fix for the blocking problem.

The third through sixth hunks change `zpty -r' to stream to stdout when
there is no parameter name into which the output is to be captured, rather
than buffering all the output in memory.

The fifth and seventh hunks remove some code that's been #if 0 for a while.

The motivation for all this is to be able to use a program running under
zpty as a filter, much like a coproc.  `zpty -w' already was able to stream
into the pty (even though that's not documented); e.g. `zpty -w foo < file'
writes the entire contents of the file to the pty (or tries to -- it might
fail if `zpty -b' was not used to create the pty).  Now `zpty -r foo' can
stream the output as well.  Unfortunately, it's still not possible to do
both at once.

The problem is that whenever zsh forks a builtin, it closes all descriptors
numbered higher than 10, which includes the master pty descriptor.  So if
you try to use `zpty -r' or `zpty -w' in a pipeline, or try to put them in
the background, they'll get "invalid file descriptor" when attempting to
access the pty.  I've played with "hiding" the master fd by zeroing its slot
in fdtable[], and that does make zpty work correctly, but it also means that
the master descriptor is still open when running other builtins, which is
not quite so desirable.

Any suggestions for the right approach to fixing this remaining problem?

Index: Src/Modules/zpty.c
===================================================================
@@ -275,12 +275,12 @@
 	return 1;
     }
     if (get_pty(1, &master)) {
-	zwarnnam(nam, "can't open pseudo terminal", NULL, 0);
+	zwarnnam(nam, "can't open pseudo terminal: %e", NULL, errno);
 	return 1;
     }
     if ((pid = fork()) == -1) {
+	zwarnnam(nam, "can't create pty command %s: %e", pname, errno);
 	close(master);
-	zwarnnam(nam, "couldn't create pty command: %s", pname, 0);
 	return 1;
     } else if (!pid) {
 
@@ -438,7 +438,8 @@
 {
     if (cmd->read != -1)
 	return;
-    if (!read_poll(cmd->fd, &cmd->read, 1) && kill(cmd->pid, 0) < 0) {
+    if (!read_poll(cmd->fd, &cmd->read, !cmd->block) &&
+	kill(cmd->pid, 0) < 0) {
 	cmd->fin = 1;
 	zclose(cmd->fd);
     }
@@ -447,7 +448,7 @@
 static int
 ptyread(char *nam, Ptycmd cmd, char **args)
 {
-    int blen = 256, used = 0, ret = 1;
+    int blen = 256, used = 0, seen = 0, ret = 1;
     char *buf = (char *) zhalloc((blen = 256) + 1);
     Patprog prog = NULL;
 
@@ -465,7 +466,9 @@
 	    zwarnnam(nam, "bad pattern: %s", args[1], 0);
 	    return 1;
 	}
-    }
+    } else
+	fflush(stdout);
+
     if (cmd->read != -1) {
 	buf[0] = (char) cmd->read;
 	buf[1] = '\0';
@@ -479,30 +482,19 @@
 		break;
 	}
 	if ((ret = read(cmd->fd, buf + used, 1)) == 1) {
+	    seen = 1;
 	    if (++used == blen) {
-		buf = hrealloc(buf, blen, blen << 1);
-		blen <<= 1;
+		if (!*args) {
+		    write(1, buf, used);
+		    used = 0;
+		} else {
+		    buf = hrealloc(buf, blen, blen << 1);
+		    blen <<= 1;
+		}
 	    }
 	}
 	buf[used] = '\0';
 
-#if 0
-	/* This once used the following test, to make sure to return
-	 * non-zero if there are no characters to read.  That looks
-	 * like a thinko now, because it disables non-blocking ptys. */
-
-	if (ret < 0 && (cmd->block
-#ifdef EWOULDBLOCK
-			|| errno != EWOULDBLOCK
-#else
-#ifdef EAGAIN
-			|| errno != EAGAIN
-#endif
-#endif
-			))
-	    break;
-#endif
-
 	if (!prog && ret <= 0)
 	    break;
     } while (!errflag && !breaks && !retflag && !contflag &&
@@ -511,11 +503,10 @@
 
     if (*args)
 	setsparam(*args, ztrdup(metafy(buf, used, META_HREALLOC)));
-    else {
-	fflush(stdout);
+    else
 	write(1, buf, used);
-    }
-    return !used;
+
+    return !seen;
 }
 
 static int
@@ -524,21 +515,7 @@
     int written;
 
     for (; len; len -= written, s += written) {
-	if ((written = write(cmd->fd, s, len)) < 0
-#if 0
-	    /* Same as above. */
-	    &&
-	    (cmd->block
-#ifdef EWOULDBLOCK
-			|| errno != EWOULDBLOCK
-#else
-#ifdef EAGAIN
-			|| errno != EAGAIN
-#endif
-#endif
-	     )
-#endif
-	    )
+	if ((written = write(cmd->fd, s, len)) < 0)
 	    return 1;
 	if (written < 0) {
 	    checkptycmd(cmd);

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2000-11-09  8:12 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-08 14:09 read_poll RE: PATCH: Zpty cleanup (merge 13061 with 13116) Sven Wischnowsky
2000-11-08 14:46 ` Andrej Borsenkow
2000-11-08 15:58   ` PATCH: read_poll " Bart Schaefer
2000-11-08 17:00     ` Bart Schaefer
2000-11-08 19:10     ` Bug in cygwin with select and master pty Andrej Borsenkow
  -- strict thread matches above, loose matches on Subject: below --
2000-11-09  8:12 PATCH: read_poll Re: PATCH: Zpty cleanup (merge 13061 with 13116) Sven Wischnowsky
2000-11-09  8:09 Sven Wischnowsky
2000-11-08 10:20 Sven Wischnowsky
2000-11-08 10:42 ` Andrej Borsenkow
2000-11-04 23:29 PATCH: Misc. zpty tweaks, plus commentary Bart Schaefer
2000-11-05  1:59 ` Bart Schaefer
2000-11-05  4:35 ` Bart Schaefer
2000-11-05  9:12   ` PATCH: Zpty cleanup (merge 13061 with 13116) Bart Schaefer
2000-11-08 14:03     ` read_poll " Andrej Borsenkow

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).