Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

710 Te rminal I/O Chapter 18


Running the program in Figure18.21, we can see what happens with these two
terminal modes:
$./a.out
Enter raw mode characters, terminate with DELETE
4
33
133
61
70
176
type DELETE
Enter cbreak mode characters, terminate with SIGINT
1 type Control-A
10 type backspace
signal caught type interrupt key

In raw mode, the characters entered wereControl-D( 04 )and the special function key
F7. On the terminal being used, this function key generated five characters:ESC( 033 ),[
( 0133 ), 1 ( 061 ), 8 ( 070 ),and ̃( 0176 ).Note that with the output processing turned offin
raw mode ( ̃OPOST), we do not get a carriage return output after each character.Also
note that special-character processing is disabled in cbreak mode (so, for example,
Control-D, the end-of-file character,and backspace aren’t handled specially), whereas
the terminal-generated signals arestill processed.

18.12 Terminal WindowSiz e


Most UNIX systems provide a way to keep track of the current terminal window size
and to have the kernel notify the foreground process group when the size changes. The
kernel maintains awinsizestructurefor every terminal and pseudo terminal:

struct winsize {
unsigned short ws_row; /* rows, in characters */
unsigned short ws_col; /* columns, in characters */
unsigned short ws_xpixel; /* horizontal size, pixels (unused) */
unsigned short ws_ypixel; /* vertical size, pixels (unused) */
};

The rules for this structureare asfollows:

•Wecan fetch the current value of this structureusing anioctl(Section 3.15) of
TIOCGWINSZ.
•Wecan storeanew value of this structure in the kernel using anioctlof
TIOCSWINSZ.Ifthis new value differs from the current value stored in the
kernel, aSIGWINCHsignal is sent to the foreground process group. (Note from
Figure10.1 that the default action for this signal is to be ignored.)
Free download pdf