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

PATCH: clobbers typeahead saga, latest installment



I played around some more with my previous typeahead patch.  There were a
couple of problems.

1. As I'd half guessed, kungetbuf could get filled up in the wrong order
because the stuff read always got stuck in to be read first.  I had a patch
for this (longer than I might have wished).

2. However, I played around some more on IRIX over a slowish line, which
actually helped show up the problems in this case.  The patch I sent set
up the terminal for non-canonical mode unconditionally.  However, there was
no guarantee the shell had finished processing all the lines of typeahead
at that point.  That meant the terminal could be for a short time in
non-canonical mode even though the keys being read were typeahead from
kungetbuf.  Because of the difficulty of mixing the two modes on the
systems we're talking about, that non-canonical mode typeahead sometimes got
inserted at the wrong place.  This is a pretty fundamental problem with
what I was doing, and may be why zsetterm() simply refused to do anything
when there was typeahead before.

A little thought showed that the old typeahead strategy could be made more
robust without too much work by adding another FIONREAD test at the point
the key was about to be read.  This is only used if zsetterm() has declined
to alter the terminal mode; if there is still typeahead, fine, just read
a character; if there isn't, fine, we can now set up the terminal for
non-canonical mode processing with no worries.  This guarantees the very
next character read will always be read in the correct mode with no
argy-bargy.  So I am much happier with this patch.  (I have checked on the
IRIX system that was giving trouble, of course.)

One minor sophistication still possible is to make the first FIONREAD store
the value it returned; then you wouldn't have to call the ioctl() again
until those had been used up.  But I don't think that's really necessary.

I send a diff against my previous patch, since the hunks that removed the
CLOBBERS_TYPEAHEAD configuration test are still appropriate.

--- Src/Zle/zle_main.c.df	Mon May  3 17:12:17 1999
+++ Src/Zle/zle_main.c	Thu May  6 11:30:27 1999
@@ -111,6 +111,10 @@
 /**/
 int feepflag;
 
+#ifdef FIONREAD
+static int delayzsetterm;
+#endif
+
 /* set up terminal */
 
 /**/
@@ -124,11 +128,25 @@
 
     ioctl(SHTTY, FIONREAD, (char *)&val);
     if (val) {
-	char *tmpline = (char *)zalloc(val);
-	read(SHTTY, tmpline, val);
-	ungetkeys(tmpline, val);
-	zfree(tmpline, val);
-    }
+	/*
+	 * Problems can occur on some systems when switching from
+	 * canonical to non-canonical input.  The former is usually
+	 * set while running programmes, but the latter is necessary
+	 * for zle.  If there is input in canonical mode, then we
+	 * need to read it without setting up the terminal.  Furthermore,
+	 * while that input gets processed there may be more input
+	 * being typed (i.e. further typeahead).  This means that
+	 * we can't set up the terminal for zle *at all* until
+	 * we are sure there is no more typeahead to come.  So
+	 * if there is typeahead, we set the flag delayzsetterm.
+	 * Then getkey() performs another FIONREAD call; if that is
+	 * 0, we have finally used up all the typeahead, and it is
+	 * safe to alter the terminal, which we do at that point.
+	 */
+	delayzsetterm = 1;
+	return;
+    } else
+	delayzsetterm = 0;
 #endif
 
 /* sanitize the tty */
@@ -302,7 +320,19 @@
     if (kungetct)
 	ret = STOUC(kungetbuf[--kungetct]);
     else {
-	if (keytmout) {
+#ifdef FIONREAD
+	if (delayzsetterm) {
+	    int val;
+	    ioctl(SHTTY, FIONREAD, (char *)&val);
+	    if (!val)
+		zsetterm();
+	}
+#endif
+	if (keytmout
+#ifdef FIONREAD
+	    && ! delayzsetterm
+#endif
+	    ) {
 	    if (keytimeout > 500)
 		exp100ths = 500;
 	    else if (keytimeout > 0)

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy



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