Team BBL
Previous Page Next Page

18.2. Overview

Terminal I/O has two modes:

  1. Canonical mode input processing. In this mode, terminal input is processed as lines. The terminal driver returns at most one line per read request.

  2. Noncanonical mode input processing. The input characters are not assembled into lines.

If we don't do anything special, canonical mode is the default. For example, if the shell redirects standard input to the terminal and we use read and write to copy standard input to standard output, the terminal is in canonical mode, and each read returns at most one line. Programs that manipulate the entire screen, such as the vi editor, use noncanonical mode, since the commands may be single characters and are not 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-D character is often the end-of-file character for the terminal, but it's also a vi 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 are processed), (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 are processed). Figure 18.20 shows a POSIX.1 function that places a terminal in cbreak or raw mode.

POSIX.1 defines 11 special 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 Figure 18.1.

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


There are several points to consider from this picture.

  • If echoing is enabled, there is an implied link between the input queue and the output queue.

  • The size of the input queue, MAX_INPUT (see Figure 2.11), 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.

  • There is another input limit, MAX_CANON, that we don't show here. This limit is the maximum number of bytes in a canonical input line.

  • Although the size of the output queue is finite, no constants defining that size are accessible to the program, because when the output queue starts to fill up, the kernel simply puts the writing process to sleep until room is available.

  • We'll see how the tcflush flush function allows us to flush either the input queue or the output queue. Similarly, when we describe the tcsetattr function, we'll see how we can tell the system to change the attributes of a terminal device only after the output queue is empty. (We want to do this, for example, if we're changing the output attributes.) We can also tell the system to discard everything in the input queue when changing the terminal attributes. (We want to do this if we're changing the input attributes or changing between canonical and noncanonical modes, so that previously entered characters aren't interpreted in the wrong mode.)

Most UNIX systems implement all the canonical processing in a module called the terminal line discipline. We can think of this module as a box that sits between the kernel's generic read and write functions and the actual device driver (see Figure 18.2).

Figure 18.2. Terminal line discipline


Note the similarity of this picture and the diagram of a stream shown in Figure 14.14. We'll return to this picture in Chapter 19, when we discuss pseudo terminals.

All the terminal device characteristics that we can examine and change are contained in a termios structure. This structure is defined in the header <termios.h>, which we use throughout this chapter:

   struct termios {
     tcflag_t  c_iflag;    /* input flags */
     tcflag_t  c_oflag;    /* output flags */
     tcflag_t  c_cflag;    /* control flags */
     tcflag_t  c_lflag;    /* local flags */
     cc_t      c_cc[NCCS]; /* control characters */
   };

Roughly speaking, the input flags control the input of characters by the terminal device driver (strip eighth bit on input, enable input parity checking, etc.), the output flags control the driver output (perform output processing, map newline to CR/LF, etc.), the control flags affect the RS-232 serial lines (ignore modem status lines, one or two stop bits per character, etc.), and the local flags affect the interface between the driver and the user (echo on or off, visually erase characters, enable terminal-generated signals, job control stop signal for background output, etc.).

The type tcflag_t is big enough to hold each of the flag values and is often defined as an unsigned int or an unsigned long. The c_cc array contains all the special characters that we can change. NCCS is the number of elements in this array and is typically between 15 and 20 (since most implementations of the UNIX System support more than the 11 POSIX-defined special characters). The cc_t type is large enough to hold each special character and is typically an unsigned char.

Versions of System V that predated the POSIX standard had a header named <termio.h> and a structure named termio. POSIX.1 added an s to the names, to differentiate them from their predecessors.

Figures 18.3 through 18.6 list all the terminal flags that we can change to affect the characteristics of a terminal device. Note that even though the Single UNIX Specification defines a common subset that all platforms start from, all the implementations have their own additions. Most of these additions come from the historical differences between the systems. We'll discuss each of these flag values in detail in Section 18.5.

Figure 18.3. c_cflag terminal flags

Flag

Description

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

CBAUDEXT

extended baud rate

    

CCAR_OFLOW

DCD flow control of output

 

 

 

CCTS_OFLOW

CTS flow control of output

 

 

CDSR_OFLOW

DSR flow control of output

 

 

 

CDTR_IFLOW

DTR flow control of input

 

 

 

CIBAUDEXT

extended input baud rate

    

CIGNORE

ignore control flags

 

 

 

CLOCAL

ignore modem status lines

CREAD

enable receiver

CRTSCTS

enable hardware flow control

 

CRTS_IFLOW

RTS flow control of input

 

 

CRTSXOFF

enable input hardware flow control

    

CSIZE

character size mask

CSTOPB

send two stop bits, else one

HUPCL

hang up on last close

MDMBUF

same as CCAR_OFLOW

 

 

 

PARENB

parity enable

PAREXT

mark or space parity

    

PARODD

odd parity, else even


Figure 18.4. c_iflag terminal flags

Flag

Description

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

BRKINT

generate SIGINT on BREAK

ICRNL

map CR to NL on input

IGNBRK

ignore BREAK condition

IGNCR

ignore CR

IGNPAR

ignore characters with parity errors

IMAXBEL

ring bell on input queue full

 

INLCR

map NL to CR on input

INPCK

enable input parity checking

ISTRIP

strip eighth bit off input characters

IUCLC

map uppercase to lowercase on input

  

 

IXANY

enable any characters to restart output

XSI

IXOFF

enable start/stop input flow control

IXON

enable start/stop output flow control

PARMRK

mark parity errors


Figure 18.5. c_lflag terminal flags

Flag

Description

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

ALTWERASE

use alternate WERASE algorithm

 

 

 

ECHO

enable echo

ECHOCTL

echo control chars as ^(Char)

 

ECHOE

visually erase chars

ECHOK

echo kill

ECHOKE

visual erase for kill

 

ECHONL

echo NL

ECHOPRT

visual erase mode for hard copy

 

EXTPROC

external character processing

 

 

 

FLUSHO

output being flushed

 

ICANON

canonical input

IEXTEN

enable extended input char processing

ISIG

enable terminal-generated signals

NOFLSH

disable flush after interrupt or quit

NOKERNINFO

no kernel output from STATUS

 

 

 

PENDIN

retype pending input

 

TOSTOP

send SIGTTOU for background output

XCASE

canonical upper/lower presentation

  

 


Figure 18.6. c_oflag terminal flags

Flag

Description

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

BSDLY

backspace delay mask

XSI

 

 

CMSPAR

mark or space parity

  

  

CRDLY

CR delay mask

XSI

 

 

FFDLY

form feed delay mask

XSI

 

 

NLDLY

NL delay mask

XSI

 

 

OCRNL

map CR to NL on output

XSI

 

OFDEL

fill is DEL, else NUL

XSI

 

 

OFILL

use fill character for delay

XSI

 

 

OLCUC

map lowercase to uppercase on output

  

 

ONLCR

map NL to CR-NL

XSI

ONLRET

NL performs CR function

XSI

 

ONOCR

no CR output at column 0

XSI

 

ONOEOT

discard EOTs (^D) on output

 

 

 

OPOST

perform output processing

OXTABS

expand tabs to spaces

 

 

 

TABDLY

horizontal tab delay mask

XSI

 

 

VtdLY

vertical tab delay mask

XSI

 

 


Given all the options available, how do we examine and change these characteristics of a terminal device? Figure 18.7 summarizes the various functions defined by the Single UNIX Specification that operate on terminal devices. (All the functions listed are part of the base POSIX specification, except for tcgetsid, which is an XSI extension. We described tcgetpgrp, tcgetsid, and tcsetpgrp in Section 9.7.)

Figure 18.7. Summary of terminal I/O functions

Function

Description

tcgetattr

fetch attributes (termios structure)

tcsetattr

set attributes (termios structure)

cfgetispeed

get input speed

cfgetospeed

get output speed

cfsetispeed

set input speed

cfsetospeed

set output speed

tcdrain

wait for all output to be transmitted

tcflow

suspend transmit or receive

tcflush

flush pending input and/or output

tcsendbreak

send BREAK character

tcgetpgrp

get foreground process group ID

tcsetpgrp

set foreground process group ID

tcgetsid

get process group ID of session leader for controlling
TTY (XSI extension)


Note that the Single UNIX Specification doesn't use the classic ioctl on terminal devices. Instead, it uses the 13 functions shown in Figure 18.7. The reason is that the ioctl function for terminal devices uses a different data type for its final argument, which depends on the action being performed. This makes type checking of the arguments impossible.

Although only 13 functions operate on terminal devices, the first two functions in Figure 18.7 (tcgetattr and tcsetattr) manipulate almost 70 different flags (see Figures 18.3 through 18.6). The handling of terminal devices is complicated by the large number of options available for terminal devices and trying to determine which options are required for a particular device (be it a terminal, modem, printer, or whatever).

The relationships among the 13 functions shown in Figure 18.7 are shown in Figure 18.8.

Figure 18.8. Relationships among the terminal-related functions


POSIX.1 doesn't specify where in the termios structure the baud rate information is stored; that is an implementation detail. Some systems, such as Linux and Solaris, store this information in the c_cflag field. BSD-derived systems, such as FreeBSD and Mac OS X, have two separate fields in the structure: one for the input speed and one for the output speed.

    Team BBL
    Previous Page Next Page