Add Book to My BookshelfPurchase This Book Online

Chapter 12 - Terminals

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Overview of Terminal I/O
Terminal input and output is processed in one of two modes, canonical mode or non-canonical mode.
In canonical mode, terminal input is processed in units of lines. A line is delimited by a newline (ASCII LF), an end-of-file character (ASCII EOT), or an end-of-line character (user defined). This means that a program attempting to read from the terminal will be suspended until an entire line has been typed. Furthermore, no matter how many characters are requested in the read call, at most one line will be returned. It is, of course, not necessary to read an entire line all at once; one or a few characters may be read at a time, and the operating system will satisfy the reads from the buffered input line. But it is important to understand that the first read request, regardless of its size, will not be satisfied until an entire line has been typed.
When in canonical mode, certain keyboard characters enable special processing. The erase character allows one character at a time to be deleted from the input, to correct typing mistakes. The kill character allows the entire input line typed to this point to be discarded. Other keyboard characters provide advanced editing features; these are discussed below. Because input is processed a line at a time, the erase and kill processing is done before a program reading from the terminal sees the input; therefore, the average program does not have to deal with these issues.
Canonical mode input processing also allows certain keyboard sequences to generate signals that are sent to the processes in the terminal's process group. These keyboard sequences can cause a program to terminate, with or without a core dump, and, on systems that support job control, can cause a program to stop execution.
Finally, canonical mode enables certain output processing features such as the generation of delays after the output of certain characters such as newlines, tabs, and form feeds, the expansion of tabs to spaces, and the conversion of lowercase letters to uppercase (for very old, uppercase-only terminals).
In non-canonical mode, input characters are not assembled into lines, and erase and kill processing does not occur. Signal generation and output processing are still performed, although they may be disabled.
When in non-canonical mode, input characters are returned to a reading process based on either a minimum input threshold (reads return after some minimum number of characters has been typed), a maximum time (reads return after a timer expires), or some combination of these.
Version 7 and BSD UNIX use the terms cooked instead of canonical and cbreak instead of non-canonical. These terms are still in general use today, even when describing systems on which they do not apply. They are presented below.
Cooked Mode
Cooked mode corresponds to canonical mode, above. Input is processed a line at a time, and input editing and signal generation is enabled. Output processing is also performed.
Cbreak Mode
Cbreak mode is a sort of “half-cooked” mode in which input editing is disabled, and reads are satisfied one character at a time (input is not buffered). When in cbreak mode, signal generation and output processing are still performed.
Raw Mode
In raw mode, all input and output processing is disabled, as is all signal generation. Read requests are satisfied one character at a time. raw mode corresponds to non-canonical mode, above, with the addition of disabling keyboard signals and output processing.
Special Characters
Several characters have special meaning in canonical mode. Version 7 provided only a basic set of these characters; most of those in the list below were added by Berkeley, and then later adopted by POSIX and SVR4. Almost all of these characters can be changed under program control; the default values are shown in parentheses.
CR (Carriage Return)
This character cannot be changed. This character is recognized in canonical input mode. Usually, the CR character is translated to NL (newline) and has the same effect as an NL character. This character is returned to the reading process (perhaps after being translated to NL).
DISCARD (CTRL-O)
This character causes all subsequent output to be discarded, until another DISCARD character is entered or the discard condition is cleared. This character is discarded by the terminal driver when processed; it is not returned to the reading process. This character is not specified in the POSIX standard, nor is it available in HP-UX 10.x.
DSUSP (CTRL-Y)
This is the delayed-suspend character; it is recoginized in canonical and basic non-canonical modes if job control is in effect. Like the SUSP character, this character sends the SIGTSTP signal to all processes in the foreground process group. However, the signal is not delivered when the character is typed, but when a process reads from the controlling terminal. This character is discarded by the terminal driver when processed; it is not returned to the reading process. This character is not specified in the POSIX standard.
EOF (CTRL-D)
This character is recognized on input in canonical mode. When this character is entered, all bytes remaining to be read are immediately passed to the reading process. If there are no bytes remaining, a count of zero is returned to the read. Entering an EOF character at the beginning of a line is the usual way to indicate an end-of-file to a program. This character is discarded by the terminal driver when processed; it is not returned to the reading process.
Some operating systems, such as MS-DOS, use a character to mark the end of a file (MS-DOS uses CTRL-Z). This character, when encountered during reading, indicates the end of the file. UNIX, on the other hand, signifies the end-of-file condition by causing read to return zero. The presence of an EOF character in the input stream does not indicate the end of a file. Its only purpose is to tell the terminal driver to generate the end-of-file condition for the reading process.
EOL (No default)
In POSIX, this character functions as an additional end-of-line delimiter when in canonical mode. It is not normally used. This character is returned to the reading process.
EOL2 (No default)
In SVR4, this character functions as still another end-of-line delimiter when in canonical mode. It is not normally used. This character is returned to the reading process.
ERASE (DEL or CTRL-H)
This character is recognized in canonical mode, and causes the previous character in the line to be erased. It is not possible to erase beyond the beginning of the line. This character is discarded by the terminal driver; it is not returned to the reading process.
INTR (CTRL-C or DEL)
This character is recognized in canonical and basic non-canonical mode. When received, it causes a SIGINT signal to be delivered to all processes in the foreground process group. This character is discarded by the terminal driver; it is not returned to the reading process.
KILL (CTRL-U)
This character is recognized in canonical mode, and erases the entire input line. It is discarded by the terminal driver; it is not returned to the reading process.
LNEXT (CTRL-V)
This character is recognized in canonical mode and causes the special meaning of the next character to be typed to be ignored (“LNEXT” stands for “literal next”). This allows the user to type any of the characters in this section to a program. This character is discarded when processed by the terminal driver, but the next character typed is passed to the reading process. This character is not specified by the POSIX standard.
NL (Newline)
This character is recognized in canonical mode and serves as the end-of-line delimiter. This character cannot be changed. This character is returned to the reading process.
QUIT (CTRL-\)
This character is recognized in canonical and basic non-canonical mode. It causes the SIGQUIT signal to be delivered to all processes in the foreground process group. This character is discarded when processed by the terminal driver; it is not returned to the reading process.
REPRINT (CTRL-R)
This character is recognized in canonical mode. It causes all unread input (the line as typed so far) to be reprinted. This character is discarded when processed; it is not returned to the reading process. This character is not specified by the POSIX standard, nor is it available in HP-UX 10.x.
START (CTRL-Q)
This character is recognized in canonical and basic non-canonical mode if flow control is enabled. When received, it causes output that has been suspended with a STOP character to start again. This character is discarded when processed; it is not returned to the reading process.
STOP (CTRL-S)
This character is recognized in canonical and basic non-canonical mode if flow control is enabled. When received, it causes output to be suspended (but not discarded) until a START character is received. This character is not returned to the reading process.
SUSP (CTRL-Z)
This character is recognized in canonical and basic non-canonical mode when job control is enabled. It causes a SIGTSTP signal to be delivered to all processes in the foreground process group. This character is discarded by the terminal driver; it is not returned to the reading process.
WERASE (CTRL-W)
This character is recognized in canonical mode. It causes the previous word to be erased. A “word” is delimited by whitespace. This character is not returned to the reading process.
BREAK
BREAK is not really a character but, rather, a condition that can be generated by the terminal hardware. Usually, BREAK is interpreted as a synonym for the INTR character, although this is not required.
Terminal Characteristics
Table 12-1 summarizes all the terminal characteristics that can be controlled on POSIX, System V, and BSD systems. Several vendors have added additional characteristics to this list; those additions are not discussed in this book. For each characteristic, the table gives a brief description and indicates the flag and option that controls this characteristic in each of the three versions. The flags and options are described in detail in the remaining sections of the chapter.
Table 12-1:  Terminal Characteristics
Characteristic
POSIX
System V
BSD
Generate SIGINT on BREAK
BRKINT
BRKINT
(cooked, cbreak)
Ignore BREAK condition
IGNBRK
IGNBRK
(raw)
Map NL to CR on input
INLCR
INLCR
—
Map CR to NL on input
ICRNL
ICRNL
CRMOD
Ignore CR
IGNCR
IGNCR
—
Enable input parity checking
INPCK
INPCK
EVENP, ODDP
Ignore characters with parity errors
IGNPAR
IGNPAR
(cooked, cbreak)
Mark characters with parity errors
PARMRK
PARMRK
—
Strip eighth bit off input characters
ISTRIP
ISTRIP
LPASS8
Enable start/stop input flow control
IXOFF
IXOFF
TANDEM
Enable start/stop output flow control
IXON
IXON
(cooked, cbreak)
Enable any character to restart output
—
IXANY
LDECCTQ
Map uppercase to lowercase on input
—
IUCLC
LCASE
Ring terminal bell on input queue full
—
IMAXBEL
NTTYDISC
Perform output processing
OPOST
OPOST
LLITOUT
Backspace delay mask
—
BSDLY
BSDELAY
Carriage return delay mask
—
CRDLY
CRDELAY
Form feed delay mask
—
FFDLY
VTDELAY
Horizontal tab delay mask
—
TABDLY
TBDELAY
Newline delay mask
—
NLDLY
NLDELAY
Vertical tab delay mask
—
VTDLY
VTDELAY
Use fill character for delay
—
OFILL
—
Fill character is DEL, else NUL
—
OFDEL
—
Map CR to NL on output
—
OCRNL
—
Map NL to CR-NL on output
—
ONLCR
CRMOD
NL performs CR function
—
ONLRET
—
No CR output at column zero
—
ONOCR
—
Map lowercase to uppercase on output
—
OLCUC
LCASE
Expand tabs to spaces
—
XTABS
XTABS
Baud rate
B0...B38400
B0...B38400
B0...B9600
Character size mask
CSIZE
CSIZE
—
Send two stop bits, else one
CSTOPB
CSTOPB
—
Enable parity
PARENB
PARENB
—
Odd parity, else even
PARODD
PARODD
ODDP, EVENP
Extended parity (mark and space)
—
PAREXT
—
Ignore modem status lines
CLOCAL
CLOCAL
—
No hangup when carrier drops
—
—
LNOHANG
Hangup on last close
HUPCL
HUPCL
TIOCHPCL
Flow control via carrier drops
—
—
LMDMBUF
Enable receiver
CREAD
CREAD
—
Convert ~ to ` on output (Hazeltine)
—
—
LTILDE
Canonical input
ICANON
ICANON
(cooked)
Enable extended input processing
IEXTEN
IEXTEN
NTTYDISC
Enable tty-generated signals
ISIG
ISIG
(cooked, cbreak)
Enable character echo
ECHO
ECHO
ECHO
Backspace on erase
—
—
LCRTBS
Visually erase with backspace-space-backspace
ECHOE
ECHOE
LCRTERA
Echo newline after kill
ECHOK
ECHOK
default
Visually kill with backspace-space-backspace
ECHOKE
—
LCRTKIL
Visual erase/kill for hardcopy terminals
—
ECHOPRT
LPRTERA
Echo control characters as ^X
—
ECHOCTL
LCTLECH
Echo NL even if ECHO is off
ECHONL
ECHONL
—
Output is being flushed
—
FLUSHO
LFLUSHO
Disable flush after interrupt/quit
NOFLSH
NOFLSH
—
Retype pending input on next character
—
PENDIN
LPENDIN
Send SIGTTOU on output from background
TOSTOP
—
LTOSTOP
Canonical uppercase/lowercase presentation
—
XCASE
LCASE

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback