[comp.os.os9] ttyfly

jov@stc06.ctd.ornl.gov (JONES J P) (08/30/90)

For simple interactive programs, I usually like to write a function
(char ttyfly() -- get one on the fly) which returns a character from
the keyboard if one is available, and 0 otherwise (non-blocking).
Since this is non-standard, it usually requires kicking around the OS a
little. Under some systems this is easy, and on others (e.g. VMS) it is
horrid.

Does anyone have a ttyfly() or something similar for OS9? If not,
can you point me in the right direction? If anyone cares, I can
trade my VMS C ttyfly() (which took over 200 lines of code).

Judd Jones -- Oak Ridge National Lab -- jonesjp@ornl.gov
(Now permanently IMHO brain-damaged former VMS user)

stp@ethz.UUCP (Stephan Paschedag) (08/31/90)

In article <1990Aug30.152825.21217@cs.utk.edu> jonesjp@ornl.gov (JONES J P) writes:
>
>For simple interactive programs, I usually like to write a function
>(char ttyfly() -- get one on the fly) which returns a character from
>the keyboard if one is available, and 0 otherwise (non-blocking).


Here it is :


char ttyfly()
{
  char ch;

  if (_gs_rdy(0) < 0)
    return(0);
  read(0,&ch,1);   /* or ch=getchar() if stdin is in unbuffered mode */
  return(ch);
}



==============================================================================
OS/2 & PS/2 : half an operating system for half a computer

Stephan Paschedag                                         stp@ethz.UUCP
MPL AG, Zelgweg 12, CH-5405 Baden-Daettwil     ..!mcvax!cernvax!chx400!ethz!stp
______________________________________________________________________________

jejones@mcrware.UUCP (James Jones) (08/31/90)

What you need to check out is the GS_RDY getstat call, which tells you how
many characters are waiting to be read.  (For historical reasons, it tech-
nically thinks not having any characters waiting is an "error," but that
doesn't get in the way.)

For efficiency's sake, I'd seriously suggest buffer the waiting characters,
so in C, you'd have something like

static char	mybuf[MYBUFSIZE];	/* or whatever */
static char *next;
static int	chleft;

ttyfly()
{
	if (--chleft < 0) {
		if ((chleft = _gs_rdy(0)) < 0)
			return 0;
		if (chleft > MYBUFSIZE)
			chleft = MYBUFSIZE;				/* Code defensively! */
		chleft = read(0, mybuf, chleft);
		if (chleft <= 0)
			return 0;
		next = mybuf;
	}
	return *next++;
}

	James Jones