zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: Poll tty before selecting on Solaris
@ 2003-02-14 18:57 Peter Stephenson
  2003-02-22 23:50 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2003-02-14 18:57 UTC (permalink / raw)
  To: Zsh hackers list

This extravaganza is necessary on Solaris when you use select() with the
terminal and there is already typeahead --- it doesn't see it, you need
to use a read with the termios poll mechanism.  You wouldn't typically
see this unless you use the zle -F mechanism to wait for other output;
unfortunately I use this all day to talk to BlueCore.

If anyone can tell me what it is I'm doing wrong in order to avoid this,
that would be a relief.

Index: Src/Zle/zle_main.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_main.c,v
retrieving revision 1.29
diff -u -r1.29 zle_main.c
--- Src/Zle/zle_main.c	27 Jan 2003 14:54:53 -0000	1.29
+++ Src/Zle/zle_main.c	14 Feb 2003 18:51:13 -0000
@@ -348,13 +348,12 @@
 {
     long exp100ths;
     int ret;
+#ifdef HAS_TIO
+    struct ttyinfo ti;
+#endif
 #ifndef HAVE_POLL
 # ifdef HAVE_SELECT
     fd_set foofd;
-# else
-#  ifdef HAS_TIO
-    struct ttyinfo ti;
-#  endif
 # endif
 #endif
 
@@ -379,9 +378,40 @@
 	    int i, errtry = 0, selret;
 # ifdef HAVE_POLL
 	    int poll_timeout;
-	    int nfds = keytmout ? 1 : 1 + nwatch;
+	    int nfds;
+	    struct pollfd *fds;
+# else
+	    int fdmax;
+	    struct timeval *tvptr;
+	    struct timeval expire_tv;
+# endif
+# if defined(HAS_TIO) && defined(sun)
+	    /*
+	     * Yes, I know this is complicated.  Yes, I know we
+	     * already have three bits of code to poll the terminal
+	     * down below.  No, I don't want to do this either.
+	     * However, it turns out on certain OSes, specifically
+	     * Solaris, that you can't poll typeahead for love nor
+	     * money without actually trying to read it.  But
+	     * if we are trying to select (and we need to if we
+	     * are watching other fd's) we won't pick that up.
+	     * So we just try and read it without blocking in
+	     * the time-honoured (i.e. absurdly baroque) termios
+	     * fashion.
+	     */
+	    gettyinfo(&ti);
+	    ti.tio.c_cc[VMIN] = 0;
+	    settyinfo(&ti);
+	    ret = read(SHTTY, cptr, 1);
+	    ti.tio.c_cc[VMIN] = 1;
+	    settyinfo(&ti);
+	    if (ret > 0)
+		return 1;
+# endif
+# ifdef HAVE_POLL
+	    nfds = keytmout ? 1 : 1 + nwatch;
 	    /* First pollfd is SHTTY, following are the nwatch fds */
-	    struct pollfd *fds = zalloc(sizeof(struct pollfd) * nfds);
+	    fds = zalloc(sizeof(struct pollfd) * nfds);
 	    if (exp100ths)
 		poll_timeout = exp100ths * 10;
 	    else
@@ -400,9 +430,8 @@
 		}
 	    }
 # else
-	    int fdmax = SHTTY;
-	    struct timeval *tvptr = NULL;
-	    struct timeval expire_tv;
+	    fdmax = SHTTY;
+	    tvptr = NULL;
 	    if (exp100ths) {
 		expire_tv.tv_sec = exp100ths / 100;
 		expire_tv.tv_usec = (exp100ths % 100) * 10000L;

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: PATCH: Poll tty before selecting on Solaris
  2003-02-14 18:57 PATCH: Poll tty before selecting on Solaris Peter Stephenson
@ 2003-02-22 23:50 ` Bart Schaefer
  2003-02-25 13:50   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2003-02-22 23:50 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 14,  6:57pm, Peter Stephenson wrote:
}
} This extravaganza is necessary on Solaris when you use select() with the
} terminal and there is already typeahead --- it doesn't see it, you need
} to use a read with the termios poll mechanism.

It's been quite a while since I worked with Solaris, but I suspect this
has to do with the STREAMS-based tty driver.  Typeahead that has already
been buffered by the stream object is invisible to select().

There might be a tty mode setting to make the input stream unbuffered that
would avoid the need to poll().  Note this isn't related to stdio buffers,
so setbuf() et al. probably isn't going to have any effect on it.


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

* Re: PATCH: Poll tty before selecting on Solaris
  2003-02-22 23:50 ` Bart Schaefer
@ 2003-02-25 13:50   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2003-02-25 13:50 UTC (permalink / raw)
  To: Zsh hackers list

"Bart Schaefer" wrote:
> On Feb 14,  6:57pm, Peter Stephenson wrote:
> } This extravaganza is necessary on Solaris when you use select() with the
> } terminal and there is already typeahead --- it doesn't see it, you need
> } to use a read with the termios poll mechanism.
> 
> It's been quite a while since I worked with Solaris, but I suspect this
> has to do with the STREAMS-based tty driver.

I had a vague feeling it would involve something like `pushing the
ttdwim module atop the STREAMS stack', or something.

The docs on the Sun website imply all the configuration is done by
standard termios; I haven't found anything which has the appropriate
effect.  I tried making the tcsetattr() use TCSANOW instead of
TCSADRAIN, but that didn't seem to help.

I think this is going to have to stay, but I'd be interested to know if
other systems show this behaviour.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

end of thread, other threads:[~2003-02-25 13:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-14 18:57 PATCH: Poll tty before selecting on Solaris Peter Stephenson
2003-02-22 23:50 ` Bart Schaefer
2003-02-25 13:50   ` Peter Stephenson

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).