Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getty(8)

signal(2)

ioctl(2)

TTY(4)  —  UNIX Programmer’s Manual

NAME

tty − general terminal interface

DESCRIPTION

This section describes both a particular special file, and the general nature of the terminal interface. 

When a terminal file is opened, it causes the process to wait until a connection is established.  In practice user’s programs seldom open these files; they are opened by init(8) and become a user’s standard input and standard output device.  The very first terminal file open in a process becomes the control terminal for that process.  The control terminal plays a special role in handling quit or interrupt signals, as discussed below.  The control terminal is inherited by a child process during a fork, even if the control terminal is closed. The set of processes that thus share a control terminal is called a process group; all members of a process group receive certain signals together, see DEL below and kill(2).

The file /dev/tty is, in each process, a synonym for the control terminal associated with that process.  The above-mentioned /dev/tty file is useful for programs that wish to be sure of writing messages on the terminal no matter how output has been redirected.  It can also be used for programs that demand a file name for output, when typed output is desired and it is tiresome to find out which terminal is currently in use.  [The terminals associated with various processes can, if needed, be discovered using ps(1)].

A terminal associated with one of these files ordinarily operates in full-duplex mode.  Characters may be typed at any time, even while output is occurring, and are only lost when the system’s character input buffers become completely choked, which is rare, or when the user has accumulated the maximum allowed number of input characters that have not yet been read by some program.  Currently this limit is 256 characters.  When the input limit is reached all the saved characters are thrown away without notice. 

Normally, terminal input is processed in units of lines.  This means that a program attempting to read will be suspended until an entire line has been typed.  Also, no matter how many characters are requested in the read call, at most one line will be returned.  It is not however necessary to read a whole line at once; any number of characters may be requested in a read, even one, without losing information.  There are special modes, discussed below, that permit the program to read each character as typed without waiting for a full line.  Certain ASCII control characters have special meaning.  These characters are not passed to a reading program except in "raw" mode where they lose their special character.  Also, it is possible to change these characters from the default; see below. 

EOT (Control-D) may be used to generate an end of file from a terminal.  When an EOT is received, all the characters waiting to be read are immediately passed to the program, without waiting for a new-line, and the EOT is discarded.  Thus if there are no characters waiting, which is to say the EOT occurred at the beginning of a line, zero characters will be passed back, and this is the standard end-of-file indication. 

DEL (Rubout) is not passed to a program but generates an interrupt signal which is sent to all processes with the associated control terminal.  Normally each such process is forced to terminate, but arrangements may be made either to ignore the signal or to receive a trap to an agreed-upon location.  See signal(2).

FS (Control-\ or control-shift-L) generates the quit signal.  Its treatment is identical to the interrupt signal except that unless a receiving process has made other arrangements it will not only be terminated but a core image file will be generated. 

DC3 (Control-S) delays all printing on the terminal until something is typed in. 

DC1 (Control-Q) restarts  printing after DC3 without generating any input to a program. 

During input, erase and kill processing is normally done.  By default, the character "^-h" (control-h) erases the last character typed, except that it will not erase beyond the beginning of a line or an EOT.  By default, the character ’@’ kills the entire line up to the point where it was typed, but not beyond an EOT.  Both these characters operate on a keystroke basis independently of any backspacing or tabbing that may have been done.  Either "@" or "^h" may be entered literally by preceding it by a backslash ’\’; the erase or kill character remains, but the "\" disappears.  These two characters may be changed to others. 

When desired, all upper-case letters are mapped into the corresponding lower-case letter.  The upper-case letter may be generated by preceding it by ’\’.  In addition, the following escape sequences can be generated on output and accepted on input:

foruse
´\´
|\!
~\^
{\(
}\)

When one or more characters are sent by the system to a user, they are actually transmitted to the terminal as soon as previously-written characters have finished typing.  Input characters are echoed by putting them in the output queue as they arrive.  When a process produces characters more rapidly than they can be typed, it will be suspended when its output queue exceeds some limit.  When the queue has drained down to some threshold the program is resumed.  Even parity is usually generated on output.  The EOT character is not transmitted (except in raw mode) to prevent terminals that respond to it from hanging up. 

Several ioctl(2) calls apply to terminals. Most of them use the following structure, defined in <sgtty.h>:

struct sgttyb {
charsg_ispeed;
charsg_ospeed;
charsg_erase;
charsg_kill;
intsg_flags;
};

The "sg_ispeed" and "sg_ospeed" fields describe the input and output speeds of the device according to the following table, which corresponds to the DEC DH-11 interface.  If other hardware is used, impossible speed changes are ignored.  Symbolic values in the table are as defined in <sgtty.h>.

B00(hang up dataphone)
B50150 baud
B75275 baud
B1103110 baud
B1344134.5 baud
B1505150 baud
B2006200 baud
B3007300 baud
B6008600 baud
B120091200 baud
B1800101800 baud
B2400112400 baud
B4800124800 baud
B9600139600 baud
B19200  14      19200 baud
EXTA    14      External A
EXTB15 External B

The "sg_erase" and "sg_kill" fields of the argument structure specify the erase and kill characters respectively.  (Defaults are Control-h (backspace)and @.) 

The "sg_flags" field of the argument structure contains several bits that determine the system’s treatment of the terminal:

ALLDELAY0177400Delay algorithm selection
BSDELAY0100000Select backspace delays (not implemented):
BS00
BS10100000
VTDELAY0040000Select form-feed and vertical-tab delays:
FF00
FF10100000
CRDELAY0030000Select carriage-return delays:
CR00
CR10010000
CR20020000
CR30030000
TBDELAY0006000Select tab delays:
TAB00
TAB10001000
TAB20004000
XTABS0006000
NLDELAY0001400Select new-line delays:
NL00
NL10000400
NL20001000
NL30001400
EVENP0000200Even parity allowed on input (most terminals)
ODDP0000100Odd parity allowed on input
RAW0000040Raw mode: wake up on all characters, 8-bit interface
CRMOD0000020Map CR into LF; echo LF or CR as CR-LF
ECHO0000010Echo (full duplex)
LCASE0000004Map upper case to lower on input
CBREAK0000002Return each character as soon as typed
TANDEM0000001Automatic flow control

The delay bits specify how long transmission stops to allow for mechanical or other movement when certain characters are sent to the terminal.  In all cases a value of 0 indicates no delay. 

Backspace delays are currently ignored but might be used for Terminet 300’s. 

If a form-feed/vertical tab delay is specified, it lasts for about 2 seconds. 

Carriage-return delay type 1 lasts about .08 seconds and is suitable for the Terminet 300.  Delay type 2 lasts about .16 seconds and is suitable for the VT05 and the TI 700.  Delay type 3 is unimplemented and is 0. 

New-line delay type 1 is dependent on the current column and is tuned for Teletype model 37’s.  Type 2 is useful for the VT05 and is about .10 seconds.  Type 3 is unimplemented and is 0. 

Tab delay type 1 is dependent on the amount of movement and is tuned to the Teletype model 37.  Type 3, called XTABS, is not a delay at all but causes tabs to be replaced by the appropriate number of spaces on output. 

Characters with the wrong parity, as determined by bits 200 and 100, are ignored. 

In raw mode, every character is passed immediately to the program without waiting until a full line has been typed.  No erase or kill processing is done; the end-of-file indicator (EOT), the interrupt character (DEL) and the quit character (FS) are not treated specially.  There are no delays and no echoing, and no replacement of one character for another; characters are a full 8 bits for both input and output (parity is up to the program). 

Mode 020 causes input carriage returns to be turned into new-lines; input of either CR or LF causes LF-CR both to be echoed (for terminals with a new-line function). 

CBREAK is a sort of half-cooked (rare?) mode.  Programs can read each character as soon as typed, instead of waiting for a full line, but quit and interrupt work, and output delays, case-translation, CRMOD, XTABS, ECHO, and parity work normally.  On the other hand there is no erase or kill, and no special treatment of \ or EOT. 

TANDEM mode causes the system to produce a stop character (default DC3) whenever the input queue is in danger of overflowing, and a start character (default DC1) when the input queue has drained sufficiently.  It is useful for flow control when the ’terminal’ is actually another machine that obeys the conventions. 

Several ioctl calls have the form:

#include <sgtty.h>

ioctl(fildes, code, arg)
int fildes, code;
struct sgttyb *arg;

The applicable codes are:

TIOCGETP
Fetch the parameters associated with the terminal, and store in the pointed-to structure.

TIOCSETP
Set the parameters according to the pointed-to structure. The interface delays until output is quiescent, then throws away any unread characters, before changing the modes.

TIOCSETN
Set the parameters but do not delay or flush input. Switching out of RAW or CBREAK mode may cause some garbage input.

With the following codes the arg is ignored. 

TIOCEXCL
Set "exclusive-use" mode: no further opens are permitted until the file has been closed.

TIOCNXCL
Turn off "exclusive-use" mode.

TIOCHPCL
When the file is closed for the last time, hang up the terminal. This is useful when the line is associated with an ACU used to place outgoing calls.

TIOCFLUSH
All characters waiting in input or output queues are flushed.

The following codes affect characters that are special to the terminal interface.  The argument is a pointer to the following structure, defined in <sgtty.h>:

struct tchars {
chart_intrc;/* interrupt */
chart_quitc;/* quit */
chart_startc;/* start output */
chart_stopc;/* stop output */
chart_eofc;/* end-of-file */
chart_brkc;/* input delimiter (like nl) */
};

The default values for these characters are DEL, FS, DC1, DC3, EOT, and −1.  A character value of −1 eliminates the effect of that character.  The "t_brkc" character, by default −1, acts like a new-line in that it terminates a ’line,’ is echoed, and is passed to the program.  The ’stop’ and ’start’ characters may be the same, to produce a toggle effect.  It is probably counterproductive to make other special characters (including erase and kill) identical. 

The calls are:

TIOCSETC
Change the various special characters to those given in the structure.

TIOCSETP
Set the special characters to those given in the structure.

FIONREAD
Return the number of characters currently in a terminal’s input buffer into the integer pointer rg.

FILES

/dev/tty
/dev/tty*
/dev/console

SEE ALSO

getty(8), stty (1), signal(2), ioctl(2)

7th Edition  —  1/20/82

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026