Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

708 Te rminal I/O Chapter 18


void
tty_atexit(void) /* can be set up by atexit(tty_atexit) */
{
if (ttysavefd >= 0)
tty_reset(ttysavefd);
}

struct termios *
tty_termios(void) /* let caller see original tty state */
{
return(&save_termios);
}

Figure 18.20Set terminal mode to cbreak or raw

Our definition of cbreak mode is the following:

•Noncanonical mode. As we mentioned at the beginning of this section, this
mode turns offsome input character processing. It does not turn offsignal
handling, so the user can always type one of the characters that triggers a
terminal-generated signal. Be awarethat the caller should catch these signals;
otherwise, there’s a chance that the signal will terminate the program, and the
terminal will be left in cbreak mode.
As a general rule, whenever we write a program that changes the terminal
mode, we should catch most signals. This allows us to reset the terminal mode
beforeterminating.
•Echo off.
•One byte at a time input.To dothis, we set MIN to 1 and TIME to 0. This is case
BfromFigure18.19. Areadwon’t return until at least one byte is available.

We define raw mode as follows:

•Noncanonical mode. We also turn offprocessing of the signal-generating
characters (ISIG)and the extended input character processing (IEXTEN).
Additionally, we disable a BREAK character from generating a signal, by turning
offBRKINT.
•Echo off.
•Wedisable the CR-to-NL mapping on input (ICRNL), input parity detection
(INPCK), the stripping of the eighth bit on input (ISTRIP), and output flow
control (IXON).
•Eight-bit characters (CS8), and parity checking is disabled (PARENB).
•All output processing is disabled (OPOST).
•One byte at a time input (MIN = 1, TIME = 0).

The program in Figure18.21 tests raw and cbreak modes.
Free download pdf