BSD 3 development
[unix-history] / usr / man / man4 / tty.4
CommitLineData
0ee5c557
BJ
1.TH TTY 4
2.SH NAME
3tty \- general terminal interface
4.SH DESCRIPTION
5This section describes
6both a particular special file,
7and the general nature of the terminal interface.
8.PP
9The file
10.I /dev/tty
11is, in each process, a synonym
12for the control terminal associated with that process.
13It is useful for programs that wish to
14be sure of writing messages on the terminal
15no matter how output has been redirected.
16It can also be used for programs that demand a file name
17for output, when typed output is desired
18and it is tiresome to find out which terminal
19is currently in use.
20.PP
21As for terminals in general: all of the low-speed asynchronous
22communications ports use the
23same general interface, no matter what
24hardware is involved.
25The remainder of this section discusses
26the common features of the interface.
27.PP
28When a terminal file is opened, it causes
29the process to wait until a connection is established.
30In practice user's programs seldom open these
31files; they are opened by
32.I init
33and become a user's
34input and output file.
35The very first terminal file open in a process becomes
36the
37.I "control terminal"
38for that process.
39The control terminal plays a special
40role in handling quit or interrupt signals, as discussed
41below.
42The control terminal is inherited by a child process during a
43.IR fork ,
44even if the control terminal is closed.
45The set of processes that thus share a control terminal
46is called a
47.I process
48.IR group ;
49all members of a process group receive certain signals
50together, see DEL below and
51.IR kill (2).
52.PP
53A terminal associated with one of these files ordinarily
54operates in full-duplex mode.
55Characters may be typed at any time,
56even while output is occurring, and are only lost when the
57system's character input buffers become completely
58choked, which is rare,
59or when the user has accumulated the maximum allowed number of
60input characters that have not yet been read by some program.
61Currently this limit is 256 characters.
62When the input limit is reached all the
63saved characters are thrown away without notice.
64.PP
65Normally, terminal input is processed in units of lines.
66This means that a program attempting
67to read will be suspended until an entire line has been
68typed.
69Also, no matter how many characters are requested
70in the read call, at most one line will be returned.
71It is not however necessary to read a whole line at
72once; any number of characters may be
73requested in a read, even one, without losing information.
74There are special modes, discussed below,
75that permit the program to read each character as typed
76without waiting for a full line.
77.PP
78During input, erase and kill processing is normally
79done.
80By default, the character `#' erases the
81last character typed, except that it will not erase
82beyond the beginning of a line or an EOT.
83By default, the character `@' kills the entire
84line up to the point where it was typed, but not beyond an EOT.
85Both these
86characters operate on a keystroke basis independently
87of any backspacing or tabbing that may have been done.
88Either `@' or `#' may be entered literally by preceding
89it by `\\';
90the erase or kill character remains, but the
91`\\' disappears.
92These two characters may be changed to others.
93.PP
94When desired,
95all upper-case letters are mapped into
96the corresponding lower-case letter.
97The upper-case letter may be generated by preceding
98it by `\\'.
99In addition, the following escape sequences can be generated
100on output and accepted on input:
101.PP
102.nf
103for use
104\` \e\'
105| \\!
106~ \e^
107{ \e(
108} \e)
109.fi
110.PP
111Certain ASCII control characters have special meaning.
112These characters are not passed to a reading program
113except in raw mode where they lose their special character.
114Also, it is possible to change these characters from the default;
115see below.
116.TP
117EOT
118(Control-D) may be used to generate an end of file
119from a terminal.
120When an EOT is received, all the characters
121waiting to be read are immediately passed to
122the program, without waiting for a new-line,
123and the EOT is discarded.
124Thus if there are no characters waiting, which
125is to say the EOT occurred at the beginning of a line,
126zero characters will be passed back, and this is
127the standard end-of-file indication.
128.TP
129DEL
130(Rubout)
131is not passed to a program but generates
132an
133.I interrupt
134signal
135which is sent to all processes with the associated control terminal.
136Normally each such process is forced to terminate,
137but arrangements may be made either to
138ignore the signal or to receive a
139trap to an agreed-upon location.
140See
141.IR signal (2).
142.TP
143FS
144(Control-\\ or control-shift-L)
145generates the
146.I quit
147signal.
148Its treatment is identical to the interrupt signal
149except that unless a receiving process has
150made other arrangements it will not only be terminated
151but a core image file will be generated.
152.TP
153DC3
154(Control-S) delays all printing on the terminal
155until something is typed in.
156.TP
157DC1
158(Control-Q) restarts printing after DC3 without generating
159any input to a program.
160.PP
161When the carrier signal from the dataset drops (usually
162because the user has hung up his terminal)
163a
164.I hangup
165signal is sent to all processes with the terminal
166as control terminal.
167Unless other arrangements have been made,
168this signal causes the processes to terminate.
169If the hangup signal is ignored, any read
170returns with an end-of-file indication.
171Thus programs that read a terminal and test for
172end-of-file on their input
173can terminate appropriately when
174hung up on.
175.PP
176When one or more
177characters are written, they are actually transmitted
178to the terminal as soon as previously-written characters
179have finished typing.
180Input characters are echoed by putting them in the output queue
181as they arrive.
182When a process produces characters more rapidly than they can be typed,
183it will be suspended when its output queue exceeds some limit.
184When the queue has drained down to some threshold
185the program is resumed.
186Even parity is always generated on output.
187The EOT character is not transmitted
188(except in raw mode)
189to prevent terminals
190that respond to it from hanging up.
191.PP
192Several
193.IR ioctl (2)
194calls apply to terminals.
195Most of them use the following structure,
196defined in
197.IR <sgtty.h> :
198.PP
199.nf
200.ft 3
201struct sgttyb {
202 char sg_ispeed;
203 char sg_ospeed;
204 char sg_erase;
205 char sg_kill;
206 int sg_flags;
207};
208.ft R
209.fi
210.PP
211The
212.I sg_ispeed
213and
214.I sg_ospeed
215fields describe the input and output speeds of the
216device according to the following table,
217which corresponds to the DEC DH-11 interface.
218If other hardware is used,
219impossible speed changes are ignored.
220Symbolic values in the table are as defined in
221.IR <sgtty.h> .
222.PP
223.nf
224.ta \w'B9600 'u +5n
225B0 0 (hang up dataphone)
226B50 1 50 baud
227B75 2 75 baud
228B110 3 110 baud
229B134 4 134.5 baud
230B150 5 150 baud
231B200 6 200 baud
232B300 7 300 baud
233B600 8 600 baud
234B1200 9 1200 baud
235B1800 10 1800 baud
236B2400 11 2400 baud
237B4800 12 4800 baud
238B9600 13 9600 baud
239EXTA 14 External A
240EXTB 15 External B
241.fi
242.DT
243.PP
244In the current configuration,
245only 110, 150, 300 and 1200 baud are really supported on dial-up lines.
246Code conversion and line control required for
247IBM 2741's (134.5 baud)
248must be implemented by the user's
249program.
250The half-duplex line discipline
251required for the 202 dataset (1200 baud)
252is not supplied; full-duplex 212 datasets work fine.
253.PP
254The
255.I sg_erase
256and
257.I sg_kill
258fields of the argument structure
259specify the erase and kill characters respectively.
260(Defaults are # and @.)
261.PP
262The
263.I sg_flags
264field of the argument structure
265contains several bits that determine the
266system's treatment of the terminal:
267.PP
268.ta \w'ALLDELAY 'u +\w'0100000 'u
269.nf
270ALLDELAY 0177400 Delay algorithm selection
271BSDELAY 0100000 Select backspace delays (not implemented):
272BS0 0
273BS1 0100000
274VTDELAY 0040000 Select form-feed and vertical-tab delays:
275FF0 0
276FF1 0100000
277CRDELAY 0030000 Select carriage-return delays:
278CR0 0
279CR1 0010000
280CR2 0020000
281CR3 0030000
282TBDELAY 0006000 Select tab delays:
283TAB0 0
284TAB1 0001000
285TAB2 0004000
286XTABS 0006000
287NLDELAY 0001400 Select new-line delays:
288NL0 0
289NL1 0000400
290NL2 0001000
291NL3 0001400
292EVENP 0000200 Even parity allowed on input (most terminals)
293ODDP 0000100 Odd parity allowed on input
294RAW 0000040 Raw mode: wake up on all characters, 8-bit interface
295CRMOD 0000020 Map CR into LF; echo LF or CR as CR-LF
296ECHO 0000010 Echo (full duplex)
297LCASE 0000004 Map upper case to lower on input
298CBREAK 0000002 Return each character as soon as typed
299TANDEM 0000001 Automatic flow control
300.DT
301.fi
302.PP
303The delay bits specify how long
304transmission stops to allow for mechanical or other movement
305when certain characters are sent to the terminal.
306In all cases a value of 0 indicates no delay.
307.PP
308Backspace delays are currently ignored but might
309be used for Terminet 300's.
310.PP
311If a form-feed/vertical tab delay is specified,
312it lasts for about 2 seconds.
313.PP
314Carriage-return delay type 1 lasts about .08 seconds
315and is suitable for the Terminet 300.
316Delay type 2 lasts about .16 seconds and is suitable
317for the VT05 and the TI 700.
318Delay type 3 is unimplemented and is 0.
319.PP
320New-line delay type 1 is dependent on the current column
321and is tuned for Teletype model 37's.
322Type 2 is useful for the VT05 and is about .10 seconds.
323Type 3 is unimplemented and is 0.
324.PP
325Tab delay type 1 is dependent on the amount of movement
326and is tuned to the Teletype model
32737.
328Type 3, called XTABS,
329is not a delay at all but causes tabs to be replaced
330by the appropriate number of spaces on output.
331.PP
332Characters with the wrong parity, as determined by bits 200 and
333100, are ignored.
334.PP
335In raw mode, every character is passed immediately
336to the program without waiting until a full line has been typed.
337No erase or kill processing is done;
338the end-of-file indicator (EOT), the interrupt character
339(DEL) and the quit character (FS) are not treated specially.
340There are no delays and no echoing, and no replacement of
341one character for another;
342characters are a full 8 bits for both input and output
343(parity is up to the program).
344.PP
345Mode 020 causes input carriage returns to be turned into
346new-lines;
347input of either CR or LF causes LF-CR both to
348be echoed
349(for terminals with a new-line function).
350.PP
351CBREAK is a sort of half-cooked (rare?) mode.
352Programs can read each character as soon as typed, instead
353of waiting for a full line,
354but quit and interrupt work, and output delays, case-translation,
355CRMOD, XTABS, ECHO, and parity work normally.
356On the other hand there is no erase or kill,
357and no special treatment of \e or EOT.
358.PP
359TANDEM mode causes the system to produce
360a stop character (default DC3) whenever the input
361queue is in danger of overflowing, and a start character
362(default DC1)
363when the input queue has drained sufficiently.
364It is useful for flow control when the `terminal'
365is actually another machine that obeys the conventions.
366.PP
367Several
368.I ioctl
369calls have the form:
370.PP
371.B #include <sgtty.h>
372.PP
373.B ioctl(fildes, code, arg)
374.br
375.B struct sgttyb *arg;
376.PP
377The applicable codes are:
378.TP
379TIOCGETP
380Fetch the parameters associated with the terminal, and store
381in the pointed-to structure.
382.TP
383TIOCSETP
384Set the parameters according to the pointed-to structure.
385The interface delays until output is quiescent,
386then throws away any unread characters,
387before changing the modes.
388.TP
389TIOCSETN
390Set the parameters but do not delay or flush input.
391Switching out of RAW or CBREAK mode
392may cause some garbage input.
393.PP
394With the following codes the
395.I arg
396is ignored.
397.TP
398TIOCEXCL
399Set ``exclusive-use'' mode:
400no further opens are permitted until the file has been closed.
401.TP
402TIOCNXCL
403Turn off ``exclusive-use'' mode.
404.TP
405TIOCHPCL
406When the file is closed for the last time,
407hang up the terminal.
408This is useful when the line is associated
409with an ACU used to place outgoing calls.
410.TP
411TIOCFLUSH
412All characters waiting in input or output queues are flushed.
413.PP
414The following codes affect characters that are special
415to the terminal interface.
416The argument is a pointer to the following structure,
417defined in
418.IR <sgtty.h> :
419.PP
420.nf
421.ft 3
422struct tchars {
423 char t_intrc; /* interrupt */
424 char t_quitc; /* quit */
425 char t_startc; /* start output */
426 char t_stopc; /* stop output */
427 char t_eofc; /* end-of-file */
428 char t_brkc; /* input delimiter (like nl) */
429};
430.fi
431.ft R
432.PP
433The default values for these characters are
434DEL, FS, DC1, DC3, EOT, and \-1.
435A character value of \-1
436eliminates the effect of that character.
437The
438.I t_brkc
439character, by default \-1,
440acts like a new-line in that it terminates a `line,'
441is echoed, and is passed to the program.
442The `stop' and `start' characters may be the same,
443to produce a toggle effect.
444It is probably counterproductive to make
445other special characters (including erase and kill)
446identical.
447.PP
448The calls are:
449.TP
450TIOCSETC
451Change the various special characters to those given in the structure.
452.TP
453TIOCSETP
454Set the special characters to those given in the structure.
455.SH FILES
456/dev/tty
457.br
458/dev/tty*
459.br
460/dev/console
461.SH SEE ALSO
462getty(8), stty (1), signal(2), ioctl(2)
463.SH BUGS
464Half-duplex terminals are not supported.
465.PP
466The terminal handler has clearly entered the
467race for ever-greater complexity and generality.
468It's still not complex and general enough for
469TENEX fans.