Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

672 Te rminal I/O Chapter 18


If we don’t do anything special, canonical mode is the default. For example, if the shell
redirects standardinput to the terminal and we usereadandwriteto copy standard
input to standardoutput, the terminal is in canonical mode, and eachreadreturns at
most one line. Programs that manipulate the entirescreen, such as thevieditor,use
noncanonical mode, since the commands may be single characters and arenot
terminated by newlines. Also, this editor doesn’t want processing by the system of the
special characters, since they may overlap with the editor commands. For example, the
Control-Dcharacter is often the end-of-file character for the terminal, but it’s also avi
command to scroll down one-half screen.

The Version 7 and older BSD-style terminal drivers supported three modes for terminal input:
(a) cooked mode (the input is collected into lines, and the special characters areprocessed), (b)
raw mode (the input is not assembled into lines, and there is no processing of special
characters), and (c) cbreak mode (the input is not assembled into lines, but some of the special
characters areprocessed). Figure18.20 shows a POSIX.1 function that places a terminal in
cbreak or raw mode.

POSIX.1 defines 11special input characters, 9 of which we can change.We’ve been
using some of these throughout the text—the end-of-file character (usually Control-D)
and the suspend character (usually Control-Z), for example. Section 18.3 describes each
of these characters.
We can think of a terminal device as being controlled by a terminal driver,usually
within the kernel. Each terminal device has an input queue and an output queue,
shown in Figure18.1.

output queue if echo enabled input queue

next character to
transmit to device

next character
written by process

next character
read from device

next character
read by process

MAX_INPUT

Figure 18.1 Logical picture of input and output queues for a terminal device

Thereare several points to consider from this picture.

•Ifechoing is enabled, there is an implied link between the input queue and the
output queue.
•The size of the input queue,MAX_INPUT(see Figure2.12), is finite. When the
input queue for a particular device fills, the system behavior is implementation
dependent. Most UNIX systems echo the bell character when this happens.
Free download pdf