[comp.emacs] bug in MicroEmacs 3.9e

kjartan@rhi.hi.is (Kjartan R. Gudmundsson) (10/28/88)

Keyboard input in MicroEmacs 3.9e on Unix system V is implemented 
with busy wait.

In the file termio.c we have the functions "typahead()" and "ttgetc()" as
follows (the comments are mine): 

ttgetc()
{
... some code for other operating systems left out ....
#if	USG 
	if( kbdqp )
		kbdqp = FALSE;
	else
	{
		if( kbdpoll && fcntl( 0, F_SETFL, kbdflgs ) < 0 )
			return FALSE;
		kbdpoll = FALSE;
/*the next sentence might be OK on pc runing XINIX with 1 user
  but when 10 users on a bigger UNIX machine are using MicroEmacs
  this one sentence consumes a lot of the CPU: */
		while (read(0, &kbdq, 1) != 1)  
			;  /* !!!!!! ????*/
	}
	return ( kbdq & 127 ); /* americans don't like 8 bit characters*/
#endif
#if	TYPEAH & (~ST520)
/* typahead:	Check to see if any characters are already in the
		keyboard buffer
*/

typahead()

{

#if	USG
	if( !kbdqp )
	{
		if( !kbdpoll && fcntl( 0, F_SETFL, kbdflgs | O_NDELAY ) < 0 )
			return(FALSE);
		kbdqp = (1 == read( 0, &kbdq, 1 ));
	}
	return ( kbdqp );
#endif
	return(FALSE);
}
#endif
---------------------------------------------------------
/* improved code is: */
#if	USG
	if( kbdqp )
		kbdqp = FALSE;
	else
	{	/* we desperatly seek a character so we turn off
		   the NO_DELAY flag and simply wait for the bastard*/
		if( fcntl( 0, F_SETFL, kbdflgs & ~NO_DELAY ) < 0 )
			return FALSE;
		read(0, &kbdq, 1);
	}
	return ( kbdq & 0xFF ); /* 8 bit characters returned */
#endif
}

#if	TYPEAH & (~ST520)
/* typahead:	Check to see if any characters are already in the
		keyboard buffer
*/

typahead()

{

#if	USG
	if( !kbdqp )
	{	/* we want to check to see if a character is ready
		   if it is, we read the character and set the 
		   character_ready flag otherwise the character_ready
		   flag is turned off*/
		/* it would of course be possible to misuse this function
		   for ex. like this 
		       while (!typahead())
			    ;
		   but every tool can be misused.*/

		if( fcntl( 0, F_SETFL, kbdflgs | O_NDELAY ) < 0 )
			return(FALSE);
		kbdqp = (1 == read( 0, &kbdq, 1 ));
	}
	return ( kbdqp );
#endif
	return(FALSE);
}
#endif