Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: Poll tty before selecting on Solaris



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@xxxxxxx>                  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.
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author