raw/cooked single char i/o

guy at rlgvax.UUCP guy at rlgvax.UUCP
Sat Jun 11 14:58:31 AEST 1983


Leave the terminal in CBREAK mode (NOT RAW mode - see previous submission)
all the time; if you want to disable the interrupt and quit characters,
set them to '\0377' using the TIOCGETC and TIOCSETC "ioctl" calls.

What you do is:

#include <whatever you are including>
#include <curses.h>
#include <sgtty.h>

main()
{
	struct tchars oldtchars, newtchars;

	initialization;
	enter "curses";
	crmode();
	ioctl(_tty_ch, TIOCGETC, &oldtchars);
	newtchars = oldtchars;
	newtchars.t_intrc = '\0377';
	newtchars.t_quitc = '\0377';
	ioctl(_tty_ch, TIOCSETC, &newtchars);

	do whatever your program does;

	nocrmode();
	ioctl(_tty_ch, TIOCSETC, &oldtchars);
	leave "curses";
	exit(<your exit code>);
}

The reason typeahead was flushed was that you were going into and out of RAW
mode before each read; going into or out of RAW (or CBREAK) mode flushes all
typeahead.  It also consumes a lot of CPU time; furthermore, it means that
while you aren't waiting for a character your program is vulnerable to
interrupts!  If you are using the job control mechanism, you may want either
to disable their control characters as well (see the manual page TTY(4)) or
catch them.

		Guy Harris
		RLG Corporation
		{seismo,mcnc,we13,brl-bmd,allegra}!rlgvax!guy



More information about the Comp.unix.wizards mailing list