sgtty" -- Device Driver" "
General terminal interface
COHERENT uses two method for controlling terminals:
sgtty and termio. To use
sgtty, simply include the statement
#include <sgtty.h> in your sources.
To use termio, include the statement
#include <termio.h>.
The rest of this article discusses the
sgtty method of controlling terminals.
When a terminal file is opened, it normally causes the
process to wait until a connection is established. In
practice, users' programs seldom open these files; they are
opened by the program getty and become a user's
standard input, output, and error files. The very first
terminal file opened by the process group leader of a
terminal file not already associated with a process group
becomes the controlling terminal for that process
group. The controlling terminal plays a special role in
handling quit and
interrupt signals, as discussed below. The
controlling terminal is inherited by a child process during
a call to fork. A process can break this
association by changing its process group using
setpgrp.
A terminal associated with one of these files ordinarily
operates in full-duplex mode. Characters can be typed at
any time, even while output is occurring, and are only lost
when the system's input buffers become completely full,
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, the sytems
throws away all the saved characters without notice.
Normally, terminal input is processed in units of lines. A
line is delimited by a newline character (ASCII LF) or an
end-of-file character (ASCII EOT). Unless otherwise
directed, 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.
During input, the system normally processes
erase and kill characters.
By default, the backspace character erases the last
character typed, except that it will not erase beyond the
beginning of the line. By default, the &&lltt;;ccttrrll--UU&&ggtt;; kills
(deletes) the entire input line, and optionally outputs a
newline character. Both these characters operate on a
keystroke-by-keystroke basis, independently of any
backspacing or tabbing which may have been done. Both the
erase and kill characters may be entered literally by
preceding them with the escape character (\). In this case,
the escape character is not read. You may change the erase
and kill characters via command stty.
Certain characters have special functions on input. These
functions and their default character values are summarized
as follows:
- IINNTTRR
- (&&lltt;;ccttrrll--CC&&ggtt;; or ASCII ETX) generates an
interrupt signal that is sent to all processes
associated with the controlling 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 the Lexicon entry for
signal.
- QQUUIITT
- (Control-\ or ASCII ES) generates a quit
signal. Its treatment is identical to that of the interrupt
signal except that, unless a receiving process has made
other arrangements, it will not only be terminated but a
core image file (called ccoorree) will be created in the current
working directory.
- EERRAASSEE
- (&&lltt;;bbaacckkssppaaccee&&ggtt;; or ASCII BS) erases the preceding
character. It will not erase beyond the start of a line, as
delimited by a newline or EOF character.
- KKIILLLL
- (&&lltt;;ccttrrll--UU&&ggtt;; or ASCII NAK) deletes the entire line,
as delimited by a newline or EOF character.
- EEOOFF
- (&&lltt;;ccttrrll--DD&&ggtt;; or ASCII EOT) generates an end-of-file
character from a terminal. When received, all the
characters waiting to be read are immediately passed to the
program without waiting for a newline, and the
EOF is discarded. Thus, if no characters
are waiting, which is to say the EOF
occurred at the beginning of a line. zero characters will
be passed back, which is the standard end-of-file
indication.
- NNLL
- (ASCII LF) is the normal line delimiter. It cannot be
changed or escaped.
- SSTTOOPP
- (&&lltt;;ccttrrll--SS&&ggtt;; or ASCII DC3) can be used to suspend
output. It is useful with CRT terminals to prevent output
from disappearing before it can be read. While output is
suspended, STOP characters are ignored and not read.
- SSTTAARRTT
- (&&lltt;;ccttrrll--QQ&&ggtt;; or ASCII DC1) resumes output that has
been suspended by a STOP character. While output is not
suspended, START characters are ignored and not read. The
start/stop characters can be changed via command
stty, or via special ioctl()
calls described below.
The character values for INTR, QUIT, ERASE,
EOF, and KILL may be changed to suit
individual tastes. The ERASE, KILL, and
EOF character may be escaped by a preceding
\ character, in which case the system ignores its special
meaning. See the Lexicon article on stty
for information on how to change these settings dynamically.
When using a ``modem control'' serial line, loss of carrier
from the data-set (modem) causes a hangup signal to
be sent to all processes that have this terminal as the
controlling terminal. Unless other arrangements have been
made, this signal causes the process to terminate. If the
hangup signal is ignored, any subsequent read returns with
an end-of-file indication. Thus programs that read a
terminal and test for end-of-file can terminate
appropriately when hung up on.
When one or more characters are written, they are
transmitted to the terminal as soon as previously written
characters have finished typing. Input characters are
echoed by putting them into the output queue as they arrive.
If a process produces characters more rapidly than they can
be printed, it will be suspended when its output queue
exceeds some limit, known as the ``high water mark''. When
the queue has ``drained'' down to some threshold, the
program resumes.
The header file &&lltt;;ssggttttyy..hh&&ggtt;; declares structures and
manifest constants to control the ssggttttyy interface. Of
interest to users are the constants that define baud rates
for terminal ports; these are as follows.
BB5500 50 baud
BB7755 75 baud
BB111100 110 baud
BB113344 134 baud
BB115500 150 baud
BB220000 200 baud
BB330000 300 baud
BB660000 600 baud
BB11220000 1200 baud
BB11880000 1800 baud
BB22440000 2400 baud
BB44880000 4800 baud
BB99660000 9600 baud
BB1199220000 19,200 baud
BB3388440000 38,400 baud
Terminal ioctl() Functions
Header file <sgtty.h> defines the
following data structures used by the various device drivers
to convey terminal specific information. These structures
are used in conjunction with special terminal or device
driver symbolic constants as part of
ioctl() requests.
The sgttyb structure contains information
related to line discipline, such as serial line speed, if
appropriate, the ``erase'' and ``kill'' characters, and a
series of flags which set the mode of the line.
/*
* Structure for TIOCSETP/TIOCGETP
*/
struct sgttyb {
char sg_ispeed;/* Input speed */
char sg_ospeed;/* Output speed */
char sg_erase; /* Character erase */
char sg_kill; /* Line kill character */
int sg_flags; /* Flags */
};
The following symbolic constants are used to access bit
positions of member sg_flags in data
structure sgttyb:
- CCBBRREEAAKK
- Each input character causes wakeup (i.e., forces a
return from a read() system call).
- CCRRMMOODD
- Map the carriage return characters '\r' to the newline
character '\n'.
- CCRRTT
- Use CRT-style character erase.
- EECCHHOO
- Echo input characters.
- EEVVEENNPP
- Select even parity. If used in conjunction with
ODDP, allow either parity.
- LLCCAASSEE
- Lowercase mapping on input.
- OODDDDPP
- Select odd parity. If used in conjunction with
EVENP, allow either parity.
- RRAAWW
- Raw mode. Same as RAWIN plus
RAWOUT.
- RRAAWWIINN
- Input is treated as 8-bit characters and not
interpreted.
- RRAAWWOOUUTT
- Output is treated as 8-bit characters and not
interpreted.
- TTAANNDDEEMM
- Use X-ON/X-OFF flow control protocol to remote device.
- XXTTAABBSS
- Expand tabs to spaces.
Data structure tchars specifies additional
special terminal characters such as the ``interrupt'' and
``quit'' characters, the ``start'' and ``stop'' characters
used for flow control, and the ``end-of-file'' character.
/*
* Structure for TIOCSETC/TIOCGETC
*/
struct tchars {
char t_intrc; /* Interrupt */
char t_quitc; /* Quit */
char t_startc; /* Start output */
char t_stopc; /* Stop output */
char t_eofc; /* End of file */
char t_brkc; /* Input delimiter */
};
The following symbolic constants are used to access various
device functions via ioctl() calls, as
defined in header file <sgtty.h>. Note
that not all functions are appropriate for all classes of
devices.
- TTIIOOCCCCBBRRKK
- Clear a BREAK condition on a serial line (i.e.,
``mark'' the line). This request cancels a previously
issued TIOCSBRK request.
- TTIIOOCCCCDDTTRR
- Clear modem control signal Data Terminal Ready (DTR) on
a serial line.
- TTIIOOCCCCHHPPCCLL
- Do not force a hangup on ``last close'' on a modem
line. The normal mode of operation for serial lines is to
drop modem signal Data Terminal Ready (DTR) when the last
close() operation is performed, thus
requesting the attached modem to drop the connection.
- TTIIOOCCCCRRTTSS
- Clear the Request To Send (RTS) signal on a serial
line. Modem control signal RTS is often used for hardware
flow control.
- TTIIOOCCEEXXCCLL
- Set device access as exclusive use. This request
requires the process to have root
privileges.
- TTIIOOCCFFLLUUSSHH
- Flush the input queue, discarding any pending input
characters, and wait for the output queue to ``drain''.
- TTIIOOCCGGEETTCC
- Get current values of the special terminal characters,
as defined by data structure tchars.
- TTIIOOCCGGEETTFF
- Get current console keyboard function key bindings.
This request is specific to the nkb console
keyboard device driver. See Lexicon article
nkb for further details.
- TTIIOOCCGGEETTKKBBTT
- Get current console keyboard key mapping table. This
request is specific to the nkb console
keyboard device driver. See Lexicon article
nkb for further details.
- TTIIOOCCGGEETTPP
- Get current terminal line settings, as defined by data
structure sgttyb.
- TTIIOOCCGGEETTTTFF
- Get current value of the terminal flags, as defined by
field t_flags in the TTY structure.
- TTIIOOCCHHPPCCLL
- Set hangup on ``last close''. See
TIOCCHPCL for further details.
- TTIIOOCCRRMMSSRR
- Get the current value of the Modem Status Register
(MSR) for the specified serial line. This request is device
driver specific and is currently supported only in the
al device driver. Symbolic constants
MSRCTS, MSRDSR,
MSRRI, and MSRRLSD correspond
to the Clear To Send, Data Set Ready, Ring Indicator and
Receive Line Status Detect (i.e. Carrier Detect) signals,
respectively, in the MSR.
- TTIIOOCCNNXXCCLL
- Set this device or port as non-exclusive use. See
TIOCEXCL for further details.
- TTIIOOCCQQUUEERRYY
- Query the number of characters currently waiting in the
input queue.
- TTIIOOCCSSBBRRKK
- Assert BREAK (i.e., ``space the line'') on the given
serial port. This is often used during login to signal a
remote system to ``hunt'' to the next baud rate in a
sequence. See TIOCCBRK for further
details.
- TTIIOOCCSSDDTTRR
- Assert modem control signal Data Terminal Ready (DTR)
on a serial line.
- TTIIOOCCSSEETTCC
- Wait for output to ``drain'', then set the terminal
control characters for this device, as specified by data
structure tchars.
- TTIIOOCCSSEETTFF
- Set console keyboard function key mapping. This
request is specific to the nkb console
keyboard device driver. See Lexicon article
nkb for further details.
- TTIIOOCCSSEETTKKBBTT
- Set console keyboard key mapping table. This request
is specific to the nkb console keyboard
device driver. See Lexicon article nkb for
further details.
- TTIIOOCCSSEETTNN
- Set terminal line settings, as defined by data
structure sgttyb. Do not flush the input queue
prior to using the new settings.
- TTIIOOCCSSEETTPP
- Same as request TIOCSETN, but also flush
the input queue.
- TTIIOOCCSSRRTTSS
- Assert the Request To Send (RTS) signal on a serial
line. Modem control signal RTS is often used for hardware
flow control.
Examples
The following code fragment gets the current terminal
settings and turns off echo.
#include <sgtty.h>
static struct sgttyb new, orig;
. . .
/*
* Get the existing terminal parameters for the terminal
* device associated with file descriptor 0 (stdin),
* turn off echo, turn on CBREAK (break on every input character)
* and set the new parameters.
*/
ioctl(0, TIOCGETP, &orig);
new = orig;
new.sg_flags &= ~ECHO; /* Turn off echo */
new.sg_flags |= CBREAK; /* Turn on CBREAK mode */
ioctl(0, TIOCSETN, &new);
The following line uses the previously saved terminal mode
to return the terminal mode to its prior state:
ioctl(0, TIOCSETN, &orig);
See Also