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

Re: typeahead problem



On Dec 8,  9:38pm, Andrej Borsenkow wrote:
> Subject: RE: typeahead problem
> 
> > I think the choice to pass purge==0 was partly to get consistent script
> > behavior regardless of the availablilty of FIONREAD.
> 
> Unfortunately, it's not consistent. Consistent would be to read the first
> character of typeahead.
> 
> About why it works and does not work.
> 
> getquery() changes tty modes, and on some systems that flushes unread input
> ("clobber typeahead") Looks, like my system is so fast, that even if I type
> several characters, the first one is read by _first_ read -q, then the
> second is read by the _second_ read -q and the others are simply lost.

Your machine executes "sleep 10" so quickly that you can't type before it
finishes?

> In other words, if system does have FIONREAD but clobbers typeahead, zsh
> always reads the first character and forgets the rest.

That shouldn't be how it works.  If the system clobbers typeahead in the
way described in Src/Zle/zle_main.c, then the typeahead should get lost
before getquery() has a chance to read even the first character.

However, it is the case that zsetterm() and getquery() are inconsistent
about their use of FIONREAD.  zsetterm() calls ioctl(FIONREAD) _before_
attaching to the TTY, whereas getquery() attaches first and then calls
ioctl().  If the zsetterm() code doesn't cause a problem with spurious
SIGTTIN and SIGTTOU signals, it should be safe to move the FIONREAD code
in getquery() to above the attachtty() and setcbreak().

> CLOBBER_TYPEAHED is used in zsetterm() but not in getquery() ...

Yes, but that doesn't make any difference [except for the inconsistency
I just noted], because getquery() already performs an equivalent test on
the value obtained from ioctl(FIONREAD).

> ... Well, do we need to change modes (setcbreak()) _before_ doing read?

Yes, but apparently not before doing ioctl(FIONREAD).

On Dec 8,  7:38pm, Bernd Eggink wrote:
> Subject: Re: typeahead problem
> Bart Schaefer wrote:
>  
> > The question is which of those two cases `read -q` should emulate.  The
> > decision made was to treat it like spell checking, leaving the typeahead
> > alone when possible so that other commands may consume it.  
> 
> IMHO this decision was wrong, but it's probabely too late now.

Actually, I don't get the impression that `read -q` is all that widely
used, and as it's already inconsistent depending on FIONREAD, this might
be a reasonable change.  We should probably float it on zsh-users first,
though.

> > get the "always consume one character" behavior is to use
> > 
> >         read -k1 "REPLY?Yes or no: " && do_something
> 
> Hm, no. You have to write something like
> 
> 	typeset -u REPLY
> 	read -k1 "REPLY?Yes or no: "
> 	[[ $REPLY == Y ]] && do_something

Well, yes, but you should still test for success of read.  I assumed you
were already testing $REPLY inside "do_something" ... if you don't need
the answer captured in $REPLY, you can just do

	read -q "?Yes or no: " && ...



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