[comp.unix.questions] Problem with nodelay

drk@athena.mit.edu (David R Kohr) (06/08/90)

I'm having a problem with the System V curses(3v) provided with
Sun/OS 4.0.3.  In particular, the nodelay() function, when given
the argument TRUE to turn off waiting for a keypress (so that
mvgetch() becomes non-blocking), seems simply not to work as
advertised.

The following program is supposed to read a character from the
terminal, then display the character, number of repetitions through
the loop, and number of times no character was present to be read.
It stops when 'q' is pressed.

The program works fine when the call "nodelay(stdscr, FALSE);" is
used: mvgetch() waits for a keypress, as it should.  But the call
"nodelay(stdscr, TRUE);" causes funny things to happen: a lot of
the repetitions through the loop aren't displayed, and when you
first press a character, the program "hangs" (no more displays
at all), but it's really still going through the loop, since if
you stop the process with ^Z, you get an updated display showing
a higher number of repetitions than before.  Also, even if you
do press 'q', the program can't be stopped unless you kill it
with ^C.

The halfdelay() function also doesn't work properly: if I substitute
"halfdelay(n);" for the nodelay() call, where n is anything between
1 and 255, mvgetch() still blocks (just like with 
"nodelay(stdscr, TRUE);"), rather than using a timeout like it's
supposed to.

I compiled the program with /usr/5bin/cc, so I think all the right
headers and libraries were used.

Here's the program:
---------------
#include <curses.h>


/*
Main function: repeatedly read character, displaying character read,
iteration number, and number of instances of no character present, at
end of each iteration.  If character is 'q', end loop.
*/

void main()
{
	int lastc = 0,	/* Last char read from keyboard */
	    reps = 0,	/* Repetitions of loop */
	    errs = 0;	/* Occurences of ERR */

	initscr();
	cbreak();
	noecho();
	nodelay(stdscr, TRUE);	/* Here's the call. */

	while (lastc != 'q') {
		if ((lastc = mvgetch(1, 1)) == ERR)
			errs++;
		mvprintw(2, 1, "Last character: %x ('%c')", lastc,
			 lastc & 0xFF);
		mvprintw(3, 1, "Repetition number: %d", ++reps);
		mvprintw(4, 1, "Occurrences of ERR: %d", errs);
		refresh();
	}
	endwin();
}
---------------
If anyone can show me how to get this to work, I'd be much obliged.
Otherwise, if someone could explain to me how to do a non-blocking
test for a keypress (i.e., mvgetch() in nodelay(stdscr, TRUE) mode)
using the Berkeley curses(3x) package supplied with Sun/OS, that
would probably solve my problem also.

Thanks in advance,

--
David R. Kohr     M.I.T. Lincoln Laboratory      Group 45 ("Radars 'R' Us")
    email:    DRK@ATHENA.MIT.EDU (preferred)  or  KOHR@LL.LL.MIT.EDU
    phone:    (617)981-0775 (work)	      or  (617)527-3908 (home)

drk@athena.mit.edu (David R Kohr) (06/09/90)

In article <1990Jun8.002108.11536@athena.mit.edu> drk@athena.mit.edu (David R Kohr) writes:
>I'm having a problem with the System V curses(3v) provided with
>Sun/OS 4.0.3.

I got the following reply from Carl Smith at Sun in response to a
cross-posting of this article to comp.sys.sun:
-------
From: cs@Eng.Sun.COM (Carl Smith)
Message-Id: <9006080158.AA21762@sparky.Eng.Sun.COM>
Received: by kanawha.Eng.Sun.COM (4.1/SMI-4.1)
	id AA00474; Thu, 7 Jun 90 18:58:45 PDT
To: drk@ATHENA.MIT.EDU (David R Kohr)
Subject: Re: Problem with nodelay() in curses(3v) in Sun/OS 4.0.3.

	I discovered the same thing myself about a year ago and filed a bug
on it.  It's bug #1012523, and it's fixed in SunOS 4.1.  Here's the public
summary from that bug report:

	The curses library function wgetch fails when the window is put in
	no delay mode (via nodelay(win, TRUE)).  Characters in the tty input
	queue are not read, and the return value of the function is incorrect.

			Carl
-------
So I now know that the Sun/OS 4.0.3 nodelay() and halfdelay() System V
curses(3v) routines definitely do *not* work properly.

Now if someone could tell me how to achieve the same functionality
as nodelay() by some other means (perhaps a non-blocking select(2) on
the terminal device), that would completely solve my problem.

--
David R. Kohr     M.I.T. Lincoln Laboratory      Group 45 ("Radars 'R' Us")
    email:    DRK@ATHENA.MIT.EDU (preferred)  or  KOHR@LL.LL.MIT.EDU
    phone:    (617)981-0775 (work)	      or  (617)527-3908 (home)