1322 Chapter 62
62.11 Summary
On early UNIX systems, terminals were real hardware devices connected to a
computer via serial lines. Early terminals were not standardized, meaning that different
escape sequences were required to program the terminals produced by different
vendors. On modern workstations, such terminals have been superseded by bit-
mapped monitors running the X Window System. However, the ability to program
terminals is still required when dealing with virtual devices, such as virtual consoles
and terminal emulators (which employ pseudoterminals), and real devices connected
via serial lines.
Terminal settings (with the exception of the terminal window size) are main-
tained in a structure of type termios, which contains four bit-mask fields that control
various terminal settings and an array that defines the various special characters
interpreted by the terminal driver. The tcgetattr() and tcsetattr() functions allow a
program to retrieve and modify the terminal settings.
When performing input, the terminal driver can operate in two different
modes. In canonical mode, input is gathered into lines (terminated by one of the
line-delimiter characters) and line editing is enabled. By contrast, noncanonical
mode allows an application to read terminal input a character at a time, without need-
ing to wait for the user to type a line-delimiter character. Line editing is disabled in
noncanonical mode. Completion of a noncanonical mode read is controlled by the
MIN and TIME fields of the termios structure, which determine the minimum number
of characters to be read and a timeout to be applied to the read operation. We
described four distinct cases for the operation of noncanonical reads.
Historically, the Seventh Edition and BSD terminal drivers provided three
input modes—cooked, cbreak, and raw—which performed varying degrees of pro-
cessing of terminal input and output. Cbreak and raw modes can be emulated by
changing various fields within the termios structure.
A range of functions perform various other terminal operations. These include
changing the terminal line speed and performing line-control operations (generat-
ing a break condition, pausing until output has been transmitted, flushing terminal
input and output queues, and suspending or resuming transmission of data in either
direction between the terminal and the computer). Other functions allow us to check if
a given file descriptor refers to a terminal and to obtain the name of that terminal. The
ioctl() system call can be used to retrieve and modify the terminal window size recorded
by the kernel, and to perform a range of other terminal-related operations.
Further information
[Stevens, 1992] also describes terminal programming and goes into much more
detail on programming serial ports. Several good online resources discuss terminal
programming. Hosted at the LDP web site (http://www.tldp.org) are the Serial
HOWTO and the Text-terminal HOWTO, both by David S. Lawyer. Another useful
source of information is the Serial Programming Guide for POSIX Operating Systems
by Michael R. Sweet, available online at http://www.easysw.com/~mike/serial/.