Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / man / man4 / termios.4
1 .\" Copyright (c) 1991, 1992, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)termios.4   8.4 (Berkeley) 4/19/94
33 .\" $FreeBSD: src/share/man/man4/termios.4,v 1.12.2.9 2001/12/17 11:30:12 ru Exp $
34 .\"
35 .Dd April 19, 1994
36 .Dt TERMIOS 4
37 .Os
38 .Sh NAME
39 .Nm termios
40 .Nd general terminal line discipline
41 .Sh SYNOPSIS
42 .In termios.h
43 .Sh DESCRIPTION
44 This describes a general terminal line discipline that is
45 supported on tty asynchronous communication ports.
46 .Ss Opening a Terminal Device File
47 When a terminal file is opened, it normally causes the process to wait
48 until a connection is established.  For most hardware, the presence
49 of a connection is indicated by the assertion of the hardware
50 .Dv CARRIER
51 line.
52 If the termios structure associated with the terminal file has the
53 .Dv CLOCAL
54 flag set in the cflag, or if the
55 .Dv O_NONBLOCK
56 flag is set
57 in the
58 .Xr open 2
59 call, then the open will succeed even without
60 a connection being present.
61 In practice, applications
62 seldom open these files; they are opened by special programs, such
63 as
64 .Xr getty 8
65 or
66 .Xr rlogind 8 ,
67 and become
68 an application's standard input, output, and error files.
69 .Ss Job Control in a Nutshell
70 Every process is associated with a particular process group and session.
71 The grouping is hierarchical: every member of a particular process group is a
72 member of the same session.  This structuring is used in managing groups
73 of related processes for purposes of
74 .\" .Gw "job control" ;
75 .Em "job control" ;
76 that is, the
77 ability from the keyboard (or from program control) to simultaneously
78 stop or restart
79 a complex command (a command composed of one or more related
80 processes).  The grouping into process groups allows delivering
81 of signals that stop or start the group as a whole, along with
82 arbitrating which process group has access to the single controlling
83 terminal.  The grouping at a higher layer into sessions is to restrict
84 the job control related signals and system calls to within processes
85 resulting from a particular instance of a "login".  Typically, a session
86 is created when a user logs in, and the login terminal is setup
87 to be the controlling terminal; all processes spawned from that
88 login shell are in the same session, and inherit the controlling
89 terminal.
90 .Pp
91 A job control shell
92 operating interactively (that is, reading commands from a terminal)
93 normally groups related processes together by placing them into the
94 same process group.  A set of processes in the same process group
95 is collectively referred to as a "job". When the foreground process
96 group of the terminal is the same as the process group of a particular
97 job, that job is said to be in the "foreground".  When the process
98 group of the terminal is different from the process group of
99 a job (but is still the controlling terminal), that job is said
100 to be in the "background".  Normally the
101 shell reads a command and starts the job that implements that
102 command.  If the command is to be started in the foreground (typical), it
103 sets the process group of the terminal to the process group
104 of the started job, waits for the job to complete, and then
105 sets the process group of the terminal back to its own process
106 group (it puts itself into the foreground).  If the job is to
107 be started in the background (as denoted by the shell operator "&"),
108 it never changes the process group of the terminal and doesn't
109 wait for the job to complete (that is, it immediately attempts to read the next
110 command).  If the job is started in the foreground, the user may
111 type a key (usually
112 .Ql \&^Z )
113 which generates the terminal stop signal
114 .Pq Dv SIGTSTP
115 and has the effect of stopping the entire job.
116 The shell will notice that the job stopped, and will resume running after
117 placing itself in the foreground.
118 The shell also has commands for placing stopped jobs in the background,
119 and for placing stopped or background jobs into the foreground.
120 .Ss Orphaned Process Groups
121 An orphaned process group is a process group that has no process
122 whose parent is in a different process group, yet is in the same
123 session.  Conceptually it means a process group that doesn't have
124 a parent that could do anything if it were to be stopped.  For example,
125 the initial login shell is typically in an orphaned process group.
126 Orphaned process groups are immune to keyboard generated stop
127 signals and job control signals resulting from reads or writes to the
128 controlling terminal.
129 .Ss The Controlling Terminal
130 A terminal may belong to a process as its controlling terminal.  Each
131 process of a session that has a controlling terminal has the same
132 controlling terminal.  A terminal may be the controlling terminal for at
133 most one session.  The controlling terminal for a session is allocated by
134 the session leader by issuing the
135 .Dv TIOCSCTTY
136 ioctl.  A controlling terminal
137 is never acquired by merely opening a terminal device file.
138 When a controlling terminal becomes
139 associated with a session, its foreground process group is set to
140 the process group of the session leader.
141 .Pp
142 The controlling terminal is inherited by a child process during a
143 .Xr fork 2
144 function call.  A process relinquishes its controlling terminal when it
145 creates a new session with the
146 .Xr setsid 2
147 function; other processes
148 remaining in the old session that had this terminal as their controlling
149 terminal continue to have it.
150 A process does not relinquish its
151 controlling terminal simply by closing all of its file descriptors
152 associated with the controlling terminal if other processes continue to
153 have it open.
154 .Pp
155 When a controlling process terminates, the controlling terminal is
156 disassociated from the current session, allowing it to be acquired by a
157 new session leader.  Subsequent access to the terminal by other processes
158 in the earlier session will be denied, with attempts to access the
159 terminal treated as if modem disconnect had been sensed.
160 .Ss Terminal Access Control
161 If a process is in the foreground process group of its controlling
162 terminal, read operations are allowed.
163 Any attempts by a process
164 in a background process group to read from its controlling terminal
165 causes a
166 .Dv SIGTTIN
167 signal to be sent to
168 the process's group
169 unless one of the
170 following special cases apply:  If the reading process is ignoring or
171 blocking the
172 .Dv SIGTTIN
173 signal, or if the process group of the reading
174 process is orphaned, the
175 .Xr read 2
176 returns -1 with
177 .Va errno set to
178 .Er EIO
179 and no
180 signal is sent.  The default action of the
181 .Dv SIGTTIN
182 signal is to stop the
183 process to which it is sent.
184 .Pp
185 If a process is in the foreground process group of its controlling
186 terminal, write operations are allowed.
187 Attempts by a process in a background process group to write to its
188 controlling terminal will cause the process group to be sent a
189 .Dv SIGTTOU
190 signal unless one of the following special cases apply:  If
191 .Dv TOSTOP
192 is not
193 set, or if
194 .Dv TOSTOP
195 is set and the process is ignoring or blocking the
196 .Dv SIGTTOU
197 signal, the process is allowed to write to the terminal and the
198 .Dv SIGTTOU
199 signal is not sent.  If
200 .Dv TOSTOP
201 is set, and the process group of
202 the writing process is orphaned, and the writing process is not ignoring
203 or blocking
204 .Dv SIGTTOU ,
205 the
206 .Xr write 2
207 returns -1 with
208 errno set to
209 .Er EIO
210 and no signal is sent.
211 .Pp
212 Certain calls that set terminal parameters are treated in the same
213 fashion as write, except that
214 .Dv TOSTOP
215 is ignored; that is, the effect is
216 identical to that of terminal writes when
217 .Dv TOSTOP
218 is set.
219 .Ss Input Processing and Reading Data
220 A terminal device associated with a terminal device file may operate in
221 full-duplex mode, so that data may arrive even while output is occurring.
222 Each terminal device file has associated with it an input queue, into
223 which incoming data is stored by the system before being read by a
224 process.  The system imposes a limit,
225 .Pf \&{ Dv MAX_INPUT Ns \&} ,
226 on the number of
227 bytes that may be stored in the input queue.  The behavior of the system
228 when this limit is exceeded depends on the setting of the
229 .Dv IMAXBEL
230 flag in the termios
231 .Fa c_iflag .
232 If this flag is set, the terminal
233 is sent an
234 .Tn ASCII
235 .Dv BEL
236 character each time a character is received
237 while the input queue is full.  Otherwise, the input queue is flushed
238 upon receiving the character.
239 .Pp
240 Two general kinds of input processing are available, determined by
241 whether the terminal device file is in canonical mode or noncanonical
242 mode.
243 Additionally,
244 input characters are processed according to the
245 .Fa c_iflag
246 and
247 .Fa c_lflag
248 fields.  Such processing can include echoing, which
249 in general means transmitting input characters immediately back to the
250 terminal when they are received from the terminal.  This is useful for
251 terminals that can operate in full-duplex mode.
252 .Pp
253 The manner in which data is provided to a process reading from a terminal
254 device file is dependent on whether the terminal device file is in
255 canonical or noncanonical mode.
256 .Pp
257 Another dependency is whether the
258 .Dv O_NONBLOCK
259 flag is set by
260 .Xr open 2
261 or
262 .Xr fcntl 2 .
263 If the
264 .Dv O_NONBLOCK
265 flag is clear, then the read request is
266 blocked until data is available or a signal has been received.  If the
267 .Dv O_NONBLOCK
268 flag is set, then the read request is completed, without
269 blocking, in one of three ways:
270 .Bl -enum -offset indent
271 .It
272 If there is enough data available to satisfy the entire request,
273 and the read completes successfully the number of
274 bytes read is returned.
275 .It
276 If there is not enough data available to satisfy the entire
277 request, and the read completes successfully, having read as
278 much data as possible, the number of bytes read is returned.
279 .It
280 If there is no data available, the read returns -1, with
281 errno set to
282 .Er EAGAIN .
283 .El
284 .Pp
285 When data is available depends on whether the input processing mode is
286 canonical or noncanonical.
287 .Ss Canonical Mode Input Processing
288 In canonical mode input processing, terminal input is processed in units
289 of lines.  A line is delimited by a newline
290 .Ql \&\en
291 character, an end-of-file
292 .Pq Dv EOF
293 character, or an end-of-line
294 .Pq Dv EOL
295 character.  See the
296 .Sx "Special Characters"
297 section for
298 more information on
299 .Dv EOF
300 and
301 .Dv EOL .
302 This means that a read request will
303 not return until an entire line has been typed, or a signal has been
304 received.  Also, no matter how many bytes are requested in the read call,
305 at most one line is returned.  It is not, however, necessary to
306 read a whole line at once; any number of bytes, even one, may be
307 requested in a read without losing information.
308 .Pp
309 .Pf \&{ Dv MAX_CANON Ns \&}
310 is a limit on the
311 number of bytes in a line.
312 The behavior of the system when this limit is
313 exceeded is the same as when the input queue limit
314 .Pf \&{ Dv MAX_INPUT Ns \&} ,
315 is exceeded.
316 .Pp
317 Erase and kill processing occur when either of two special characters,
318 the
319 .Dv ERASE
320 and
321 .Dv KILL
322 characters (see the
323 .Sx "Special Characters section" ) ,
324 is received.
325 This processing affects data in the input queue that has not yet been
326 delimited by a newline
327 .Dv NL ,
328 .Dv EOF ,
329 or
330 .Dv EOL
331 character.  This un-delimited
332 data makes up the current line.  The
333 .Dv ERASE
334 character deletes the last
335 character in the current line, if there is any.  The
336 .Dv KILL
337 character
338 deletes all data in the current line, if there is any.  The
339 .Dv ERASE
340 and
341 .Dv KILL
342 characters have no effect if there is no data in the current line.
343 The
344 .Dv ERASE
345 and
346 .Dv KILL
347 characters themselves are not placed in the input
348 queue.
349 .Ss Noncanonical Mode Input Processing
350 In noncanonical mode input processing, input bytes are not assembled into
351 lines, and erase and kill processing does not occur.  The values of the
352 .Dv VMIN
353 and
354 .Dv VTIME
355 members of the
356 .Fa c_cc
357 array are used to determine how to
358 process the bytes received.
359 .Pp
360 .Dv MIN
361 represents the minimum number of bytes that should be received when
362 the
363 .Xr read 2
364 function successfully returns.
365 .Dv TIME
366 is a timer of 0.1 second
367 granularity that is used to time out bursty and short term data
368 transmissions.  If
369 .Dv MIN
370 is greater than
371 .Dv \&{ Dv MAX_INPUT Ns \&} ,
372 the response to the
373 request is undefined.  The four possible values for
374 .Dv MIN
375 and
376 .Dv TIME
377 and
378 their interactions are described below.
379 .Ss "Case A: MIN > 0, TIME > 0"
380 In this case
381 .Dv TIME
382 serves as an inter-byte timer and is activated after
383 the first byte is received.  Since it is an inter-byte timer, it is reset
384 after a byte is received.  The interaction between
385 .Dv MIN
386 and
387 .Dv TIME
388 is as
389 follows:  as soon as one byte is received, the inter-byte timer is
390 started.  If
391 .Dv MIN
392 bytes are received before the inter-byte timer expires
393 (remember that the timer is reset upon receipt of each byte), the read is
394 satisfied.  If the timer expires before
395 .Dv MIN
396 bytes are received, the
397 characters received to that point are returned to the user.  Note that if
398 .Dv TIME
399 expires at least one byte is returned because the timer would
400 not have been enabled unless a byte was received.  In this case
401 .Pf \&( Dv MIN
402 > 0,
403 .Dv TIME
404 > 0) the read blocks until the
405 .Dv MIN
406 and
407 .Dv TIME
408 mechanisms are
409 activated by the receipt of the first byte, or a signal is received.  If
410 data is in the buffer at the time of the
411 .Fn read ,
412 the result is as
413 if data had been received immediately after the
414 .Fn read .
415 .Ss "Case B: MIN > 0, TIME = 0"
416 In this case, since the value of
417 .Dv TIME
418 is zero, the timer plays no role
419 and only
420 .Dv MIN
421 is significant.  A pending read is not satisfied until
422 .Dv MIN
423 bytes are received (i.e., the pending read blocks until
424 .Dv MIN
425 bytes
426 are received), or a signal is received.  A program that uses this case to
427 read record-based terminal
428 .Dv I/O
429 may block indefinitely in the read
430 operation.
431 .Ss "Case C: MIN = 0, TIME > 0"
432 In this case, since
433 .Dv MIN
434 = 0,
435 .Dv TIME
436 no longer represents an inter-byte
437 timer.  It now serves as a read timer that is activated as soon as the
438 read function is processed.  A read is satisfied as soon as a single
439 byte is received or the read timer expires.  Note that in this case if
440 the timer expires, no bytes are returned.  If the timer does not
441 expire, the only way the read can be satisfied is if a byte is received.
442 In this case the read will not block indefinitely waiting for a byte; if
443 no byte is received within
444 .Dv TIME Ns *0.1
445 seconds after the read is initiated,
446 the read returns a value of zero, having read no data.  If data is
447 in the buffer at the time of the read, the timer is started as if
448 data had been received immediately after the read.
449 .Ss Case D: MIN = 0, TIME = 0
450 The minimum of either the number of bytes requested or the number of
451 bytes currently available is returned without waiting for more
452 bytes to be input.  If no characters are available, read returns a
453 value of zero, having read no data.
454 .Ss Writing Data and Output Processing
455 When a process writes one or more bytes to a terminal device file, they
456 are processed according to the
457 .Fa c_oflag
458 field (see the
459 .Sx "Output Modes
460 section).  The
461 implementation may provide a buffering mechanism; as such, when a call to
462 .Fn write
463 completes, all of the bytes written have been scheduled for
464 transmission to the device, but the transmission will not necessarily
465 have been completed.
466 .\" See also .Sx "6.4.2" for the effects of
467 .\" .Dv O_NONBLOCK
468 .\" on write.
469 .Ss Special Characters
470 Certain characters have special functions on input or output or both.
471 These functions are summarized as follows:
472 .Bl -tag -width indent
473 .It Dv INTR
474 Special character on input and is recognized if the
475 .Dv ISIG
476 flag (see the
477 .Sx "Local Modes"
478 section) is enabled.  Generates a
479 .Dv SIGINT
480 signal which is sent to all processes in the foreground
481 process group for which the terminal is the controlling
482 terminal.  If
483 .Dv ISIG
484 is set, the
485 .Dv INTR
486 character is
487 discarded when processed.
488 .It Dv QUIT
489 Special character on input and is recognized if the
490 .Dv ISIG
491 flag is enabled.  Generates a
492 .Dv SIGQUIT
493 signal which is
494 sent to all processes in the foreground process group
495 for which the terminal is the controlling terminal.  If
496 .Dv ISIG
497 is set, the
498 .Dv QUIT
499 character is discarded when
500 processed.
501 .It Dv ERASE
502 Special character on input and is recognized if the
503 .Dv ICANON
504 flag is set.  Erases the last character in the
505 current line; see
506 .Sx "Canonical Mode Input Processing" .
507 It does not erase beyond
508 the start of a line, as delimited by an
509 .Dv NL ,
510 .Dv EOF ,
511 or
512 .Dv EOL
513 character.  If
514 .Dv ICANON
515 is set, the
516 .Dv ERASE
517 character is
518 discarded when processed.
519 .It Dv KILL
520 Special character on input and is recognized if the
521 .Dv ICANON
522 flag is set.  Deletes the entire line, as
523 delimited by a
524 .Dv NL ,
525 .Dv EOF ,
526 or
527 .Dv EOL
528 character.  If
529 .Dv ICANON
530 is set, the
531 .Dv KILL
532 character is discarded when processed.
533 .It Dv EOF
534 Special character on input and is recognized if the
535 .Dv ICANON
536 flag is set.  When received, all the bytes
537 waiting to be read are immediately passed to the
538 process, without waiting for a newline, and the
539 .Dv EOF
540 is discarded.  Thus, if there are no bytes waiting (that
541 is, the
542 .Dv EOF
543 occurred at the beginning of a line), a byte
544 count of zero is returned from the
545 .Fn read ,
546 representing an end-of-file indication.  If
547 .Dv ICANON
548 is
549 set, the
550 .Dv EOF
551 character is discarded when processed.
552 .It Dv NL
553 Special character on input and is recognized if the
554 .Dv ICANON
555 flag is set.  It is the line delimiter
556 .Ql \&\en .
557 .It Dv EOL
558 Special character on input and is recognized if the
559 .Dv ICANON
560 flag is set.  Is an additional line delimiter,
561 like
562 .Dv NL .
563 .It Dv SUSP
564 If the
565 .Dv ISIG
566 flag is enabled, receipt of the
567 .Dv SUSP
568 character causes a
569 .Dv SIGTSTP
570 signal to be sent to all processes in the
571 foreground process group for which the terminal is the
572 controlling terminal, and the
573 .Dv SUSP
574 character is
575 discarded when processed.
576 .It Dv STOP
577 Special character on both input and output and is
578 recognized if the
579 .Dv IXON
580 (output control) or
581 .Dv IXOFF
582 (input
583 control) flag is set.  Can be used to temporarily
584 suspend output.  It is useful with fast terminals to
585 prevent output from disappearing before it can be read.
586 If
587 .Dv IXON
588 is set, the
589 .Dv STOP
590 character is discarded when
591 processed.
592 .It Dv START
593 Special character on both input and output and is
594 recognized if the
595 .Dv IXON
596 (output control) or
597 .Dv IXOFF
598 (input
599 control) flag is set.  Can be used to resume output that
600 has been suspended by a
601 .Dv STOP
602 character.  If
603 .Dv IXON
604 is set, the
605 .Dv START
606 character is discarded when processed.
607 .It Dv CR
608 Special character on input and is recognized if the
609 .Dv ICANON
610 flag is set; it is the
611 .Ql \&\er ,
612 as denoted in the
613 .Tn \&C
614 Standard {2}.  When
615 .Dv ICANON
616 and
617 .Dv ICRNL
618 are set and
619 .Dv IGNCR
620 is not set, this character is translated into a
621 .Dv NL ,
622 and
623 has the same effect as a
624 .Dv NL
625 character.
626 .El
627 .Pp
628 The following special characters are extensions defined by this
629 system and are not a part of
630 .St -p1003.1
631 termios.
632 .Bl -tag -width indent
633 .It Dv EOL2
634 Secondary
635 .Dv EOL
636 character.  Same function as
637 .Dv EOL .
638 .It Dv WERASE
639 Special character on input and is recognized if the
640 .Dv ICANON
641 flag is set.  Erases the last word in the current
642 line according to one of two algorithms.  If the
643 .Dv ALTWERASE
644 flag is not set, first any preceding whitespace is
645 erased, and then the maximal sequence of non-whitespace
646 characters.  If
647 .Dv ALTWERASE
648 is set, first any preceding
649 whitespace is erased, and then the maximal sequence
650 of alphabetic/underscores or non alphabetic/underscores.
651 As a special case in this second algorithm, the first previous
652 non-whitespace character is skipped in determining
653 whether the preceding word is a sequence of
654 alphabetic/underscores.  This sounds confusing but turns
655 out to be quite practical.
656 .It Dv REPRINT
657 Special character on input and is recognized if the
658 .Dv ICANON
659 flag is set.  Causes the current input edit line
660 to be retyped.
661 .It Dv DSUSP
662 Has similar actions to the
663 .Dv SUSP
664 character, except that
665 the
666 .Dv SIGTSTP
667 signal is delivered when one of the processes
668 in the foreground process group issues a
669 .Fn read
670 to the
671 controlling terminal.
672 .It Dv LNEXT
673 Special character on input and is recognized if the
674 .Dv IEXTEN
675 flag is set.  Receipt of this character causes the next
676 character to be taken literally.
677 .It Dv DISCARD
678 Special character on input and is recognized if the
679 .Dv IEXTEN
680 flag is set.  Receipt of this character toggles the flushing
681 of terminal output.
682 .It Dv STATUS
683 Special character on input and is recognized if the
684 .Dv ICANON
685 flag is set.  Receipt of this character causes a
686 .Dv SIGINFO
687 signal to be sent to the foreground process group of the
688 terminal.  Also, if the
689 .Dv NOKERNINFO
690 flag is not set, it
691 causes the kernel to write a status message to the terminal
692 that displays the current load average, the name of the
693 command in the foreground, its process ID, the symbolic
694 wait channel, the number of user and system seconds used,
695 the percentage of cpu the process is getting, and the resident
696 set size of the process.
697 .El
698 .Pp
699 The
700 .Dv NL
701 and
702 .Dv CR
703 characters cannot be changed.
704 The values for all the remaining characters can be set and are
705 described later in the document under
706 Special Control Characters.
707 .Pp
708 Special
709 character functions associated with changeable special control characters
710 can be disabled individually by setting their value to
711 .Dv {_POSIX_VDISABLE} ;
712 see
713 .Sx "Special Control Characters" .
714 .Pp
715 If two or more special characters have the same value, the function
716 performed when that character is received is undefined.
717 .Ss Modem Disconnect
718 If a modem disconnect is detected by the terminal interface for a
719 controlling terminal, and if
720 .Dv CLOCAL
721 is not set in the
722 .Fa c_cflag
723 field for
724 the terminal, the
725 .Dv SIGHUP
726 signal is sent to the controlling
727 process associated with the terminal.  Unless other arrangements have
728 been made, this causes the controlling process to terminate.
729 Any subsequent call to the
730 .Fn read
731 function returns the value zero,
732 indicating end of file.  Thus, processes that read a terminal
733 file and test for end-of-file can terminate appropriately after a
734 disconnect.
735 .\" If the
736 .\" .Er EIO
737 .\" condition specified in 6.1.1.4 that applies
738 .\" when the implementation supports job control also exists, it is
739 .\" unspecified whether the
740 .\" .Dv EOF
741 .\" condition or the
742 .\" .Pf [ Dv EIO
743 .\" ] is returned.
744 Any
745 subsequent
746 .Fn write
747 to the terminal device returns -1, with
748 .Va errno
749 set to
750 .Er EIO ,
751 until the device is closed.
752 .Sh General Terminal Interface
753 .Ss Closing a Terminal Device File
754 The last process to close a terminal device file causes any output
755 to be sent to the device and any input to be discarded.  Then, if
756 .Dv HUPCL
757 is set in the control modes, and the communications port supports a
758 disconnect function, the terminal device performs a disconnect.
759 .Ss Parameters That Can Be Set
760 Routines that need to control certain terminal
761 .Tn I/O
762 characteristics
763 do so by using the termios structure as defined in the header
764 .Aq Pa termios.h .
765 This structure contains minimally four scalar elements of bit flags
766 and one array of special characters.  The scalar flag elements are
767 named:
768 .Fa c_iflag ,
769 .Fa c_oflag ,
770 .Fa c_cflag ,
771 and
772 .Fa c_lflag .
773 The character array is named
774 .Fa c_cc ,
775 and its maximum index is
776 .Dv NCCS .
777 .Ss Input Modes
778 Values of the
779 .Fa c_iflag
780 field describe the basic
781 terminal input control, and are composed of
782 following masks:
783 .Pp
784 .Bl -tag -width IMAXBEL -offset indent -compact
785 .It Dv IGNBRK
786 /* ignore BREAK condition */
787 .It Dv BRKINT
788 /* map BREAK to SIGINTR */
789 .It Dv IGNPAR
790 /* ignore (discard) parity errors */
791 .It Dv PARMRK
792 /* mark parity and framing errors */
793 .It Dv INPCK
794 /* enable checking of parity errors */
795 .It Dv ISTRIP
796 /* strip 8th bit off chars */
797 .It Dv INLCR
798 /* map NL into CR */
799 .It Dv IGNCR
800 /* ignore CR */
801 .It Dv ICRNL
802 /* map CR to NL (ala CRMOD) */
803 .It Dv IXON
804 /* enable output flow control */
805 .It Dv IXOFF
806 /* enable input flow control */
807 .It Dv IXANY
808 /* any char will restart after stop */
809 .It Dv IMAXBEL
810 /* ring bell on input queue full */
811 .El
812 .Pp
813 In the context of asynchronous serial data transmission, a break
814 condition is defined as a sequence of zero-valued bits that continues for
815 more than the time to send one byte.  The entire sequence of zero-valued
816 bits is interpreted as a single break condition, even if it continues for
817 a time equivalent to more than one byte.  In contexts other than
818 asynchronous serial data transmission the definition of a break condition
819 is implementation defined.
820 .Pp
821 If
822 .Dv IGNBRK
823 is set, a break condition detected on input is ignored, that
824 is, not put on the input queue and therefore not read by any process.  If
825 .Dv IGNBRK
826 is not set and
827 .Dv BRKINT
828 is set, the break condition flushes the
829 input and output queues and if the terminal is the controlling terminal
830 of a foreground process group, the break condition generates a
831 single
832 .Dv SIGINT
833 signal to that foreground process group.  If neither
834 .Dv IGNBRK
835 nor
836 .Dv BRKINT
837 is set, a break condition is read as a single
838 .Ql \&\e0 ,
839 or if
840 .Dv PARMRK
841 is set, as
842 .Ql \&\e377 ,
843 .Ql \&\e0 ,
844 .Ql \&\e0 .
845 .Pp
846 If
847 .Dv IGNPAR
848 is set, a byte with a framing or parity error (other than
849 break) is ignored.
850 .Pp
851 If
852 .Dv PARMRK
853 is set, and
854 .Dv IGNPAR
855 is not set, a byte with a framing or parity
856 error (other than break) is given to the application as the
857 three-character sequence
858 .Ql \&\e377 ,
859 .Ql \&\e0 ,
860 X, where
861 .Ql \&\e377 ,
862 .Ql \&\e0
863 is a two-character
864 flag preceding each sequence and X is the data of the character received
865 in error.  To avoid ambiguity in this case, if
866 .Dv ISTRIP
867 is not set, a valid
868 character of
869 .Ql \&\e377
870 is given to the application as
871 .Ql \&\e377 ,
872 .Ql \&\e377 .
873 If
874 neither
875 .Dv PARMRK
876 nor
877 .Dv IGNPAR
878 is set, a framing or parity error (other than
879 break) is given to the application as a single character
880 .Ql \&\e0 .
881 .Pp
882 If
883 .Dv INPCK
884 is set, input parity checking is enabled.  If
885 .Dv INPCK
886 is not set,
887 input parity checking is disabled, allowing output parity generation
888 without input parity errors.  Note that whether input parity checking is
889 enabled or disabled is independent of whether parity detection is enabled
890 or disabled (see
891 .Sx "Control Modes" ) .
892 If parity detection is enabled but input
893 parity checking is disabled, the hardware to which the terminal is
894 connected recognizes the parity bit, but the terminal special file
895 does not check whether this bit is set correctly or not.
896 .Pp
897 If
898 .Dv ISTRIP
899 is set, valid input bytes are first stripped to seven bits,
900 otherwise all eight bits are processed.
901 .Pp
902 If
903 .Dv INLCR
904 is set, a received
905 .Dv NL
906 character is translated into a
907 .Dv CR
908 character.  If
909 .Dv IGNCR
910 is set, a received
911 .Dv CR
912 character is ignored (not
913 read).  If
914 .Dv IGNCR
915 is not set and
916 .Dv ICRNL
917 is set, a received
918 .Dv CR
919 character is
920 translated into a
921 .Dv NL
922 character.
923 .Pp
924 If
925 .Dv IXON
926 is set, start/stop output control is enabled.  A received
927 .Dv STOP
928 character suspends output and a received
929 .Dv START
930 character
931 restarts output.
932 If
933 .Dv IXANY
934 is also set, then any character may
935 restart output.
936 When
937 .Dv IXON
938 is set,
939 .Dv START
940 and
941 .Dv STOP
942 characters are not
943 read, but merely perform flow control functions.  When
944 .Dv IXON
945 is not set,
946 the
947 .Dv START
948 and
949 .Dv STOP
950 characters are read.
951 .Pp
952 If
953 .Dv IXOFF
954 is set, start/stop input control is enabled.  The system shall
955 transmit one or more
956 .Dv STOP
957 characters, which are intended to cause the
958 terminal device to stop transmitting data, as needed to prevent the input
959 queue from overflowing and causing the undefined behavior described in
960 .Sx "Input Processing and Reading Data" ,
961 and shall transmit one or more
962 .Dv START
963 characters, which are
964 intended to cause the terminal device to resume transmitting data, as
965 soon as the device can continue transmitting data without risk of
966 overflowing the input queue.  The precise conditions under which
967 .Dv STOP
968 and
969 START
970 characters are transmitted are implementation defined.
971 .Pp
972 If
973 .Dv IMAXBEL
974 is set and the input queue is full, subsequent input shall cause an
975 .Tn ASCII
976 .Dv BEL
977 character to be transmitted to
978 the output queue.
979 .Pp
980 The initial input control value after
981 .Fn open
982 is implementation defined.
983 .Ss Output Modes
984 Values of the
985 .Fa c_oflag
986 field describe the basic terminal output control,
987 and are composed of the following masks:
988 .Pp
989 .Bl -tag -width OXTABS -offset indent -compact
990 .It Dv OPOST
991 /* enable following output processing */
992 .It Dv ONLCR
993 /* map NL to CR-NL (ala
994 .Dv CRMOD )
995 */
996 .It Dv OCRNL
997 /* map CR to NL */
998 .It Dv OXTABS
999 /* expand tabs to spaces */
1000 .It Dv ONOEOT
1001 /* discard
1002 .Dv EOT Ns 's
1003 .Ql \&^D
1004 on output) */
1005 .It Dv ONOCR
1006 /* do not transmit CRs on column 0 */
1007 .It Dv ONLRET
1008 /* on the termianl NL performs the CR function */
1009 .El
1010 .Pp
1011 If
1012 .Dv OPOST
1013 is set, the remaining flag masks are interpreted as follows;
1014 otherwise characters are transmitted without change.
1015 .Pp
1016 If
1017 .Dv ONLCR
1018 is set, newlines are translated to carriage return, linefeeds.
1019 .Pp
1020 If
1021 .Dv OCRNL
1022 is set, carriage returns are translated to newlines.
1023 .Pp
1024 If
1025 .Dv OXTABS
1026 is set, tabs are expanded to the appropriate number of
1027 spaces (assuming 8 column tab stops).
1028 .Pp
1029 If
1030 .Dv ONOEOT
1031 is set,
1032 .Tn ASCII
1033 .Dv EOT Ns 's
1034 are discarded on output.
1035 .Pp
1036 If
1037 .Dv ONOCR
1038 is set, no CR character is transmitted when at column 0 (first position).
1039 .Pp
1040 If
1041 .Dv ONLRET
1042 is set, the NL character is assumed to do the carriage-return function;
1043 the column pointer will be set to 0.
1044 .Ss Control Modes
1045 Values of the
1046 .Fa c_cflag
1047 field describe the basic
1048 terminal hardware control, and are composed of the
1049 following masks.
1050 Not all values
1051 specified are supported by all hardware.
1052 .Pp
1053 .Bl -tag -width CRTSXIFLOW -offset indent -compact
1054 .It Dv CSIZE
1055 /* character size mask */
1056 .It Dv CS5
1057 /* 5 bits (pseudo) */
1058 .It Dv CS6
1059 /* 6 bits */
1060 .It Dv CS7
1061 /* 7 bits */
1062 .It Dv CS8
1063 /* 8 bits */
1064 .It Dv CSTOPB
1065 /* send 2 stop bits */
1066 .It Dv CREAD
1067 /* enable receiver */
1068 .It Dv PARENB
1069 /* parity enable */
1070 .It Dv PARODD
1071 /* odd parity, else even */
1072 .It Dv HUPCL
1073 /* hang up on last close */
1074 .It Dv CLOCAL
1075 /* ignore modem status lines */
1076 .It Dv CCTS_OFLOW
1077 /*
1078 .Dv CTS
1079 flow control of output */
1080 .It Dv CRTSCTS
1081 /* same as
1082 .Dv CCTS_OFLOW
1083 */
1084 .It Dv CRTS_IFLOW
1085 /* RTS flow control of input */
1086 .It Dv MDMBUF
1087 /* flow control output via Carrier */
1088 .El
1089 .Pp
1090 The
1091 .Dv CSIZE
1092 bits specify the byte size in bits for both transmission and
1093 reception.  The
1094 .Fa c_cflag
1095 is masked with
1096 .Dv CSIZE
1097 and compared with the
1098 values
1099 .Dv CS5 ,
1100 .Dv CS6 ,
1101 .Dv CS7 ,
1102 or
1103 .Dv CS8 .
1104 This size does not include the parity bit, if any.  If
1105 .Dv CSTOPB
1106 is set, two stop bits are used, otherwise one stop bit.  For example, at
1107 110 baud, two stop bits are normally used.
1108 .Pp
1109 If
1110 .Dv CREAD
1111 is set, the receiver is enabled.  Otherwise, no character is
1112 received.
1113 Not all hardware supports this bit.  In fact, this flag
1114 is pretty silly and if it were not part of the
1115 .Nm
1116 specification
1117 it would be omitted.
1118 .Pp
1119 If
1120 .Dv PARENB
1121 is set, parity generation and detection are enabled and a parity
1122 bit is added to each character.  If parity is enabled,
1123 .Dv PARODD
1124 specifies
1125 odd parity if set, otherwise even parity is used.
1126 .Pp
1127 If
1128 .Dv HUPCL
1129 is set, the modem control lines for the port are lowered
1130 when the last process with the port open closes the port or the process
1131 terminates.  The modem connection is broken.
1132 .Pp
1133 If
1134 .Dv CLOCAL
1135 is set, a connection does not depend on the state of the modem
1136 status lines.  If
1137 .Dv CLOCAL
1138 is clear, the modem status lines are
1139 monitored.
1140 .Pp
1141 Under normal circumstances, a call to the
1142 .Fn open
1143 function waits for
1144 the modem connection to complete.  However, if the
1145 .Dv O_NONBLOCK
1146 flag is set
1147 or if
1148 .Dv CLOCAL
1149 has been set, the
1150 .Fn open
1151 function returns
1152 immediately without waiting for the connection.
1153 .Pp
1154 The
1155 .Dv CCTS_OFLOW
1156 .Pf ( Dv CRTSCTS )
1157 flag is currently unused.
1158 .Pp
1159 If
1160 .Dv MDMBUF
1161 is set then output flow control is controlled by the state
1162 of Carrier Detect.
1163 .Pp
1164 If the object for which the control modes are set is not an asynchronous
1165 serial connection, some of the modes may be ignored; for example, if an
1166 attempt is made to set the baud rate on a network connection to a
1167 terminal on another host, the baud rate may or may not be set on the
1168 connection between that terminal and the machine it is directly connected
1169 to.
1170 .Ss Local Modes
1171 Values of the
1172 .Fa c_lflag
1173 field describe the control of
1174 various functions, and are composed of the following
1175 masks.
1176 .Pp
1177 .Bl -tag -width NOKERNINFO -offset indent -compact
1178 .It Dv ECHOKE
1179 /* visual erase for line kill */
1180 .It Dv ECHOE
1181 /* visually erase chars */
1182 .It Dv ECHO
1183 /* enable echoing */
1184 .It Dv ECHONL
1185 /* echo
1186 .Dv NL
1187 even if
1188 .Dv ECHO
1189 is off */
1190 .It Dv ECHOPRT
1191 /* visual erase mode for hardcopy */
1192 .It Dv ECHOCTL
1193 /* echo control chars as ^(Char) */
1194 .It Dv ISIG
1195 /* enable signals
1196 .Dv INTR ,
1197 .Dv QUIT ,
1198 .Dv [D]SUSP
1199 */
1200 .It Dv ICANON
1201 /* canonicalize input lines */
1202 .It Dv ALTWERASE
1203 /* use alternate
1204 .Dv WERASE
1205 algorithm */
1206 .It Dv IEXTEN
1207 /* enable
1208 .Dv DISCARD
1209 and
1210 .Dv LNEXT
1211 */
1212 .It Dv EXTPROC
1213 /* external processing */
1214 .It Dv TOSTOP
1215 /* stop background jobs from output */
1216 .It Dv FLUSHO
1217 /* output being flushed (state) */
1218 .It Dv NOKERNINFO
1219 /* no kernel output from
1220 .Dv VSTATUS
1221 */
1222 .It Dv PENDIN
1223 /* XXX retype pending input (state) */
1224 .It Dv NOFLSH
1225 /* don't flush after interrupt */
1226 .El
1227 .Pp
1228 If
1229 .Dv ECHO
1230 is set, input characters are echoed back to the terminal.  If
1231 .Dv ECHO
1232 is not set, input characters are not echoed.
1233 .Pp
1234 If
1235 .Dv ECHOE
1236 and
1237 .Dv ICANON
1238 are set, the
1239 .Dv ERASE
1240 character causes the terminal
1241 to erase the last character in the current line from the display, if
1242 possible.  If there is no character to erase, an implementation may echo
1243 an indication that this was the case or do nothing.
1244 .Pp
1245 If
1246 .Dv ECHOK
1247 and
1248 .Dv ICANON
1249 are set, the
1250 .Dv KILL
1251 character causes
1252 the current line to be discarded and the system echoes the
1253 .Ql \&\en
1254 character after the
1255 .Dv KILL
1256 character.
1257 .Pp
1258 If
1259 .Dv ECHOKE
1260 and
1261 .Dv ICANON
1262 are set, the
1263 .Dv KILL
1264 character causes
1265 the current line to be discarded and the system causes
1266 the terminal
1267 to erase the line from the display.
1268 .Pp
1269 If
1270 .Dv ECHOPRT
1271 and
1272 .Dv ICANON
1273 are set, the system assumes
1274 that the display is a printing device and prints a
1275 backslash and the erased characters when processing
1276 .Dv ERASE
1277 characters, followed by a forward slash.
1278 .Pp
1279 If
1280 .Dv ECHOCTL
1281 is set, the system echoes control characters
1282 in a visible fashion using a caret followed by the control character.
1283 .Pp
1284 If
1285 .Dv ALTWERASE
1286 is set, the system uses an alternative algorithm
1287 for determining what constitutes a word when processing
1288 .Dv WERASE
1289 characters (see
1290 .Dv WERASE ) .
1291 .Pp
1292 If
1293 .Dv ECHONL
1294 and
1295 .Dv ICANON
1296 are set, the
1297 .Ql \&\en
1298 character echoes even if
1299 .Dv ECHO
1300 is not set.
1301 .Pp
1302 If
1303 .Dv ICANON
1304 is set, canonical processing is enabled.  This enables the
1305 erase and kill edit functions, and the assembly of input characters into
1306 lines delimited by
1307 .Dv NL ,
1308 .Dv EOF ,
1309 and
1310 .Dv EOL ,
1311 as described in
1312 .Sx "Canonical Mode Input Processing" .
1313 .Pp
1314 If
1315 .Dv ICANON
1316 is not set, read requests are satisfied directly from the input
1317 queue.  A read is not satisfied until at least
1318 .Dv MIN
1319 bytes have been
1320 received or the timeout value
1321 .Dv TIME
1322 expired between bytes.  The time value
1323 represents tenths of seconds.  See
1324 .Sx "Noncanonical Mode Input Processing"
1325 for more details.
1326 .Pp
1327 If
1328 .Dv ISIG
1329 is set, each input character is checked against the special
1330 control characters
1331 .Dv INTR ,
1332 .Dv QUIT ,
1333 and
1334 .Dv SUSP
1335 (job control only).  If an input
1336 character matches one of these control characters, the function
1337 associated with that character is performed.  If
1338 .Dv ISIG
1339 is not set, no
1340 checking is done.  Thus these special input functions are possible only
1341 if
1342 .Dv ISIG
1343 is set.
1344 .Pp
1345 If
1346 .Dv IEXTEN
1347 is set, implementation-defined functions are recognized
1348 from the input data.  How
1349 .Dv IEXTEN
1350 being set
1351 interacts with
1352 .Dv ICANON ,
1353 .Dv ISIG ,
1354 .Dv IXON ,
1355 or
1356 .Dv IXOFF
1357 is implementation defined.
1358 If
1359 .Dv IEXTEN
1360 is not set, then
1361 implementation-defined functions are not recognized, and the
1362 corresponding input characters are not processed as described for
1363 .Dv ICANON ,
1364 .Dv ISIG ,
1365 .Dv IXON ,
1366 and
1367 .Dv IXOFF .
1368 .Pp
1369 If
1370 .Dv NOFLSH
1371 is set, the normal flush of the input and output queues
1372 associated with the
1373 .Dv INTR ,
1374 .Dv QUIT ,
1375 and
1376 .Dv SUSP
1377 characters
1378 are not be done.
1379 .Pp
1380 If
1381 .Dv TOSTOP
1382 is set, the signal
1383 .Dv SIGTTOU
1384 is sent to the process group of a process that tries to write to
1385 its controlling terminal if it is not in the foreground process group for
1386 that terminal.  This signal, by default, stops the members of the process
1387 group.  Otherwise, the output generated by that process is output to the
1388 current output stream.  Processes that are blocking or ignoring
1389 .Dv SIGTTOU
1390 signals are excepted and allowed to produce output and the
1391 .Dv SIGTTOU
1392 signal
1393 is not sent.
1394 .Pp
1395 If
1396 .Dv NOKERNINFO
1397 is set, the kernel does not produce a status message
1398 when processing
1399 .Dv STATUS
1400 characters (see
1401 .Dv STATUS ) .
1402 .Ss Special Control Characters
1403 The special control characters values are defined by the array
1404 .Fa c_cc .
1405 This table lists the array index, the corresponding special character,
1406 and the system default value.  For an accurate list of
1407 the system defaults, consult the header file
1408 .Aq Pa ttydefaults.h .
1409 .Pp
1410 .Bl -column "Index Name" "Special Character" -offset indent -compact
1411 .It Em "Index Name      Special Character       Default Value"
1412 .It Dv VEOF Ta EOF Ta \&^D
1413 .It Dv VEOL Ta EOL Ta _POSIX_VDISABLE
1414 .It Dv VEOL2 Ta EOL2 Ta _POSIX_VDISABLE
1415 .It Dv VERASE Ta ERASE Ta \&^? Ql \&\e177
1416 .It Dv VWERASE  Ta WERASE Ta \&^W
1417 .It Dv VKILL Ta KILL Ta \&^U
1418 .It Dv VREPRINT Ta REPRINT Ta \&^R
1419 .It Dv VINTR Ta INTR Ta \&^C
1420 .It Dv VQUIT Ta QUIT Ta \&^\e\e Ql \&\e34
1421 .It Dv VSUSP Ta SUSP Ta \&^Z
1422 .It Dv VDSUSP Ta DSUSP Ta \&^Y
1423 .It Dv VSTART Ta START Ta \&^Q
1424 .It Dv VSTOP Ta STOP Ta \&^S
1425 .It Dv VLNEXT Ta LNEXT Ta \&^V
1426 .It Dv VDISCARD Ta DISCARD Ta \&^O
1427 .It Dv VMIN Ta --- Ta \&1
1428 .It Dv VTIME Ta --- Ta \&0
1429 .It Dv VSTATUS Ta STATUS Ta \&^T
1430 .El
1431 .Pp
1432 If the
1433 value of one of the changeable special control characters (see
1434 .Sx "Special Characters" )
1435 is
1436 .Dv {_POSIX_VDISABLE} ,
1437 that function is disabled; that is, no input
1438 data is recognized as the disabled special character.
1439 If
1440 .Dv ICANON
1441 is
1442 not set, the value of
1443 .Dv {_POSIX_VDISABLE}
1444 has no special meaning for the
1445 .Dv VMIN
1446 and
1447 .Dv VTIME
1448 entries of the
1449 .Fa c_cc
1450 array.
1451 .Pp
1452 The initial values of the flags and control characters
1453 after
1454 .Fn open
1455 is set according to
1456 the values in the header
1457 .Aq Pa sys/ttydefaults.h .