[comp.unix.questions] Is typeahead loss a generic ksh problem?

ado@elsie.UUCP (Arthur David Olson) (05/17/88)

We're using the 02/21/85b version of ksh on a VAX 11/750 running MORE/bsd 4.3.
At times ksh will flush typeahead, as shown by the transcript below:

	Script started on Mon May 16 14:11:13 1988
	$ function q {
	> 	(echo alpha; sleep 5; echo omega) | awk '{print ; exit}'
	> }
	$ q ; echo hello, world
	alpha
	hello, world
	$ q
	alpha
>>>>	echo hello, world
	$ 
	$ exit

	script done on Mon May 16 14:12:29 1988

I (quickly) typed in the ">>>>"d line above before receiving the ksh "$" prompt;
the input was discarded.

Is this a problem with other versions of ksh?  Is there a known fix?
-- 
	  Canada is to spaceflight as the U.S.S.R. is to baseball.
	ado@ncifcrf.gov			ADO is a trademark of Ampex.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (05/18/88)

In article <8062@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
>At times ksh will flush typeahead, as shown by the transcript below:
>Is this a problem with other versions of ksh?  Is there a known fix?

It's really a problem with the BSD V7-based terminal handler, which makes
it difficult to switch modes without losing typeahead.  However, it can
be done, as witness the BRL Bourne shell which doesn't have this problem
(at least, not when linked with our System V emulation library).  We DO
have the problem to some degree with our Cray-2, which relies on TELNET
protocols to have the "front end" system take care of most aspects of
terminal handling instead of doing it right.  On the Cray-2 I added a
small delay before switching modes to give output time to drain before
the OLFCR mapping goes away, but that's not entirely satisfactory either.

I suspect the ksh sources could be tweaked for better operation on BSD
by borrowing some of the tricks used in my System V ioctl() emulation.
Future releases of BSD are supposed to have a POSIX-compatible terminal
handler.

dce@mips.COM (David Elliott) (05/18/88)

(I apologize if this is a repost.  I looked, and it doesn't appear to
 have gone out, but flakiness is flakiness.)

In article <8062@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
>We're using the 02/21/85b version of ksh on a VAX 11/750 running MORE/bsd 4.3.
>At times ksh will flush typeahead, as shown by the transcript below:

Typeahead flush is a problem with most line-editors, which is one of
the reasons it is preferable to implement line-editors in the tty
instead of in the shells.  When you change from standard (cooked) mode
to raw or pseudo-raw mode, you lose the data.

A similar problem exists with the Livermore csh line-editor and is likely
to exist in tcsh.  It does not exist in the old BSD command completion
interface, which uses alternate break characters and TIOCSTI.

What can happen is that either typeahead gets flushed, or it gets
eaten by the editor.  For example, if you type the lines

	sleep 20
	head -1
	echo foo
	echo bar

wait for the prompt, and then type "ls", you might expect the "head -1"
to read and print "echo foo", the word "bar" to be printed, and an "ls"
to be executed.  Instead, the "head -1" will read "ls", and "foo" and
"bar" will be printed.  In other words, the line-editor has eaten
everything in the queue, even though it does not use the data.

You learn to live without typeahead or without a line editor.  I chose
to live without the editor.

-- 
David Elliott		dce@mips.com  or  {ames,prls,pyramid,decwrl}!mips!dce

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (05/19/88)

I've run ksh on a number of machines, BSD and SysV, and that has never
happened to me.  Given the machine you're using, could it be so loaded
that the device driver is dropping stuff?

-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

gwyn@brl-smoke.ARPA (Doug Gwyn ) (05/19/88)

In article <2208@quacky.mips.COM> dce@mips.COM (David Elliott) writes:
>You learn to live without typeahead or without a line editor.  I chose
>to live without the editor.

There is no need for typeahead to be flushed when the terminal handler
mode is changed via ioctl.  That is an artifact of the old, creaky
terminal handler most 4BSD-based systems are still saddled with.  In
fact it isn't even necessary on modern 4BSD systems; I often type
ahead just after invoking our local screen editor, which definitely
changes terminal modes after my first few characters but does not
lost them.

ado@elsie.UUCP (Arthur David Olson) (05/20/88)

Thanks to everyone who replied to the original posting.  The problem turned
out to be that if you run a command such as
	( sleep 5 ; echo hi ) | exit
the "echo hi" is terminated by a SIGPIPE signal.  Ksh (or at least the version
of ksh in use at elsie) waits for all elements of a pipeline to complete
before prompting again.  It noticed that the SIGPIPE death had occurred,
and assumed (in "reset_ttygrp", for those keeping score) that some tty cleanup
should be done.

The fix (in "await") was to change a
			reset_ttygrp(pw,w&LOBYTE);
call to
			if ((w&LOBYTE)==SIGPIPE)
				reset_ttygrp(pw,0);
			else	reset_ttygrp(pw,w&LOBYTE);
-- 
    Canada's program is to spaceflight as the carrier pigeon is to mail.
	ado@ncifcrf.gov			ADO is a trademark of Ampex.

tneff@atpal.UUCP (Tom Neff) (05/20/88)

In article <8062@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
>-- 
>	  Canada is to spaceflight as the U.S.S.R. is to baseball.

I'm forced to disagree, Arthur.  Canada has given the space program one
thing the USSR has never given baseball: a great arm.  :-)

(actually 4 of 'em, 3 still extant)

-- 
Tom Neff			UUCP: ...uunet!pwcmrd!skipnyc!atpal!tneff
	"None of your toys	CIS: 76556,2536		MCI: TNEFF
	 will function..."	GEnie: TOMNEFF		BIX: are you kidding?

leo@philmds.UUCP (Leo de Wit) (05/25/88)

In article <7930@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <2208@quacky.mips.COM> dce@mips.COM (David Elliott) writes:
>>You learn to live without typeahead or without a line editor.  I chose
>>to live without the editor.
>
>There is no need for typeahead to be flushed when the terminal handler
>mode is changed via ioctl.  That is an artifact of the old, creaky
>terminal handler most 4BSD-based systems are still saddled with.  In
>fact it isn't even necessary on modern 4BSD systems; I often type
>ahead just after invoking our local screen editor, which definitely
>changes terminal modes after my first few characters but does not
>lost them.


Although the discussion was kind of closed, I would like to make a remark
about typeahead, flushing etc. because I have seen nobody mentioning this:

From the stty manual page (to give it an 'official' ring *-) :

pendin   Input is pending after a switch from cbreak to cooked and will be
         re-input when a read becomes pending or more input arrives (internal
         state bit).

I also have no problems with typeahead flushing (I use the esh which is a
Bourne sh with csh features like command line editing with cursor keys etc,
foreground/background and other features of the csh). I also use stty new
(I think this also sets pending) and although the shell must run in cbreak
mode no characters are lost (thanks to 'pendin'). I suggest the originator
of the problem uses: stty new; maybe this solves his problem.

        Leo.