reading chars in raw mode with Ioctl .. How ?

Guy Harris guy at auspex.auspex.com
Fri Oct 19 04:52:44 AEST 1990


>This should do BSD or SV single character input.

It does, but it may be overkill.  The problem is that the term "raw
mode" is used to refer both to the mode that, in the older V7-based tty
drivers such as the one in BSD, is actually called RAW mode, *and* to
any mode that gives you characters as they're typed.

RAW mode does a *lot* more than just give you characters as they're
typed.  It also:

	puts the line in 8 bits, no parity, mode;

	turns off all output processing;

	turns off special handling of all input characters, including
	the interrupt, quit, suspend (if you have job control), ^S, and
	^Q characters;

etc..

Not all applications that want to read characters one at a time
necessarily want to do all that.

In the older tty driver, you can turn CBREAK mode on, which will only
put the driver in "character at a time" mode and, as such, disable the
line-editing characters; it won't turn off the signal-generating
characters nor ^S nor ^Q, nor will it affect output processing or change
the parity mode of the line.  In the newer tty driver, you can turn
ICANON mode off, which has the same effect.

This does, of course, mean that you now have to *catch* signals such as
SIGINT and, if you have job control, SIGTSTP, and handle them
appropriately; otherwise, if you abort the program by typing your
interrupt character, or stop it by typing the suspend character if you
have job control, the tty will still be in character-at-a-time mode.

It also means you can't use ^S, ^Q, etc.  as input characters; you'd
have to disable those as well.  Individual control characters can
sort-of be disabled in the older driver by setting them to '\377', as
the older driver usually strips input characters to 7 bits before
comparing them with those special characters.  (As of 4.3BSD, though, if
you turn PASS8 on, it won't strip it, so if somebody happens to have a
key that generates, say, the ISO Latin 1 "y with a diaresis" character,
whose character code happens to *be* '\377', you may be in for a
surprise if you set one of those characters to '\377'.  You might try
'\200' instead, as it's a less-likely character to get as input.)

The newer driver lets you disable the signal-generating characters
(interrupt, quit, suspend if you have job control) by turning ISIG off,
and disable ^S and ^Q by turning IXON off. 

If you want 8-bit input - e.g., if your terminal has a META key that
turns the 8th bit on, and you want to be able to use it - PASS8 mode
works with the 4.3BSD and later versions of the older driver; it puts
the tty port into 8 bits, no parity mode, and prevents characters from
being stripped to 7 bits on input.  In the newer driver, you have to
turn the ISTRIP bit off (to prevent characters from being stripped to 7
bits on input), and set the "c_cflag" field to include CS8 and turn off
PARENB (for 8 bits, no parity).



More information about the Comp.unix.programmer mailing list