1 @node Low-Level Terminal Interface
2 @chapter Low-Level Terminal Interface
4 This chapter describes functions that are specific to terminal devices.
5 You can use these functions to do things like turn off input echoing;
6 set serial line characteristics such as line speed and flow control; and
7 change which characters are used for end-of-file, command-line editing,
8 sending signals, and similar control functions.
10 Most of the functions in this chapter operate on file descriptors.
11 @xref{Low-Level I/O}, for more information about what a file
12 descriptor is and how to open a file descriptor for a terminal device.
15 * Is It a Terminal:: How to determine if a file is a terminal
16 device, and what its name is.
17 * I/O Queues:: About flow control and typeahead.
18 * Canonical or Not:: Two basic styles of input processing.
19 * Terminal Modes:: How to examine and modify flags controlling
20 details of terminal I/O: echoing,
22 * Line Control:: Sending break sequences, clearing
23 terminal buffers @dots{}
24 * Noncanon Example:: How to read single characters without echo.
27 @node Is It a Terminal
28 @section Identifying Terminals
29 @cindex terminal identification
30 @cindex identifying terminals
32 The functions described in this chapter only work on files that
33 correspond to terminal devices. You can find out whether a file
34 descriptor is associated with a terminal by using the @code{isatty}
38 Prototypes for both @code{isatty} and @code{ttyname} are declared in
39 the header file @file{unistd.h}.
43 @deftypefun int isatty (int @var{filedes})
44 This function returns @code{1} if @var{filedes} is a file descriptor
45 associated with an open terminal device, and @code{0} otherwise.
48 If a file descriptor is associated with a terminal, you can get its
49 associated file name using the @code{ttyname} function. See also the
50 @code{ctermid} function, described in @ref{Identifying the Terminal}.
54 @deftypefun {char *} ttyname (int @var{filedes})
55 If the file descriptor @var{filedes} is associated with a terminal
56 device, the @code{ttyname} function returns a pointer to a
57 statically-allocated, null-terminated string containing the file name of
58 the terminal file. The value is a null pointer if the file descriptor
59 isn't associated with a terminal, or the file name cannot be determined.
65 Many of the remaining functions in this section refer to the input and
66 output queues of a terminal device. These queues implement a form of
67 buffering @emph{within the kernel} independent of the buffering
68 implemented by I/O streams (@pxref{I/O on Streams}).
70 @cindex terminal input queue
71 @cindex typeahead buffer
72 The @dfn{terminal input queue} is also sometimes referred to as its
73 @dfn{typeahead buffer}. It holds the characters that have been received
74 from the terminal but not yet read by any process.
76 The size of the input queue is described by the @code{MAX_INPUT} and
77 @w{@code{_POSIX_MAX_INPUT}} parameters; see @ref{Limits for Files}. You
78 are guaranteed a queue size of at least @code{MAX_INPUT}, but the queue
79 might be larger, and might even dynamically change size. If input flow
80 control is enabled by setting the @code{IXOFF} input mode bit
81 (@pxref{Input Modes}), the terminal driver transmits STOP and START
82 characters to the terminal when necessary to prevent the queue from
83 overflowing. Otherwise, input may be lost if it comes in too fast from
84 the terminal. In canonical mode, all input stays in the queue until a
85 newline character is received, so the terminal input queue can fill up
86 when you type a very long line. @xref{Canonical or Not}.
88 @cindex terminal output queue
89 The @dfn{terminal output queue} is like the input queue, but for output;
90 it contains characters that have been written by processes, but not yet
91 transmitted to the terminal. If output flow control is enabled by
92 setting the @code{IXON} input mode bit (@pxref{Input Modes}), the
93 terminal driver obeys STOP and STOP characters sent by the terminal to
94 stop and restart transmission of output.
96 @dfn{Clearing} the terminal input queue means discarding any characters
97 that have been received but not yet read. Similarly, clearing the
98 terminal output queue means discarding any characters that have been
99 written but not yet transmitted.
101 @node Canonical or Not
102 @section Two Styles of Input: Canonical or Not
104 POSIX systems support two basic modes of input: canonical and
107 @cindex canonical input processing
108 In @dfn{canonical input processing} mode, terminal input is processed in
109 lines terminated by newline (@code{'\n'}), EOF, or EOL characters. No
110 input can be read until an entire line has been typed by the user, and
111 the @code{read} function (@pxref{I/O Primitives}) returns at most a
112 single line of input, no matter how many bytes are requested.
114 In canonical input mode, the operating system provides input editing
115 facilities: some characters are interpreted specially to perform editing
116 operations within the current line of text, such as ERASE and KILL.
117 @xref{Editing Characters}.
119 The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterize
120 the maximum number of bytes which may appear in a single line of
121 canonical input. @xref{Limits for Files}. You are guaranteed a maximum
122 line length of at least @code{MAX_CANON} bytes, but the maximum might be
123 larger, and might even dynamically change size.
125 @cindex noncanonical input processing
126 In @dfn{noncanonical input processing} mode, characters are not grouped
127 into lines, and ERASE and KILL processing is not performed. The
128 granularity with which bytes are read in noncanonical input mode is
129 controlled by the MIN and TIME settings. @xref{Noncanonical Input}.
131 Most programs use canonical input mode, because this gives the user a
132 way to edit input line by line. The usual reason to use noncanonical
133 mode is when the program accepts single-character commands or provides
134 its own editing facilities.
136 The choice of canonical or noncanonical input is controlled by the
137 @code{ICANON} flag in the @code{c_lflag} member of @code{struct termios}.
141 @section Terminal Modes
144 This section describes the various terminal attributes that control how
145 input and output are done. The functions, data structures, and symbolic
146 constants are all declared in the header file @file{termios.h}.
147 @c !!! should mention terminal attributes are distinct from file attributes
150 * Mode Data Types:: The data type @code{struct termios} and
152 * Mode Functions:: Functions to read and set the terminal
154 * Setting Modes:: The right way to set terminal attributes
156 * Input Modes:: Flags controlling low-level input handling.
157 * Output Modes:: Flags controlling low-level output handling.
158 * Control Modes:: Flags controlling serial port behavior.
159 * Local Modes:: Flags controlling high-level input handling.
160 * Line Speed:: How to read and set the terminal line speed.
161 * Special Characters:: Characters that have special effects,
162 and how to change them.
163 * Noncanonical Input:: Controlling how long to wait for input.
166 @node Mode Data Types
167 @subsection Terminal Mode Data Types
168 @cindex terminal mode data types
170 The entire collection of attributes of a terminal is stored in a
171 structure of type @code{struct termios}. This structure is used
172 with the functions @code{tcgetattr} and @code{tcsetattr} to read
173 and set the attributes.
177 @deftp {Data Type} {struct termios}
178 Structure that records all the I/O attributes of a terminal. The
179 structure includes at least the following members:
182 @item tcflag_t c_iflag
183 A bit mask specifying flags for input modes; see @ref{Input Modes}.
185 @item tcflag_t c_oflag
186 A bit mask specifying flags for output modes; see @ref{Output Modes}.
188 @item tcflag_t c_cflag
189 A bit mask specifying flags for control modes; see @ref{Control Modes}.
191 @item tcflag_t c_lflag
192 A bit mask specifying flags for local modes; see @ref{Local Modes}.
194 @item cc_t c_cc[NCCS]
195 An array specifying which characters are associated with various
196 control functions; see @ref{Special Characters}.
199 The @code{struct termios} structure also contains members which
200 encode input and output transmission speeds, but the representation is
201 not specified. @xref{Line Speed}, for how to examine and store the
205 The following sections describe the details of the members of the
206 @code{struct termios} structure.
210 @deftp {Data Type} tcflag_t
211 This is an unsigned integer type used to represent the various
212 bit masks for terminal flags.
217 @deftp {Data Type} cc_t
218 This is an unsigned integer type used to represent characters associated
219 with various terminal control functions.
224 @deftypevr Macro int NCCS
225 The value of this macro is the number of elements in the @code{c_cc}
230 @subsection Terminal Mode Functions
231 @cindex terminal mode functions
235 @deftypefun int tcgetattr (int @var{filedes}, struct termios *@var{termios-p})
236 This function is used to examine the attributes of the terminal
237 device with file descriptor @var{filedes}. The attributes are returned
238 in the structure that @var{termios-p} points to.
240 If successful, @code{tcgetattr} returns @code{0}. A return value of @code{-1}
241 indicates an error. The following @code{errno} error conditions are
242 defined for this function:
246 The @var{filedes} argument is not a valid file descriptor.
249 The @var{filedes} is not associated with a terminal.
255 @deftypefun int tcsetattr (int @var{filedes}, int @var{when}, const struct termios *@var{termios-p})
256 This function sets the attributes of the terminal device with file
257 descriptor @var{filedes}. The new attributes are taken from the
258 structure that @var{termios-p} points to.
260 The @var{when} argument specifies how to deal with input and output
261 already queued. It can be one of the following values:
268 Make the change immediately.
274 Make the change after waiting until all queued output has been written.
275 You should usually use this option when changing parameters that affect
282 This is like @code{TCSADRAIN}, but also discards any queued input.
288 This is a flag bit that you can add to any of the above alternatives.
289 Its meaning is to inhibit alteration of the state of the terminal
290 hardware. It is a BSD extension; it is only supported on BSD systems
293 Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}
294 bit in the @code{c_cflag} member of the structure @var{termios-p} points
295 to. @xref{Control Modes}, for a description of @code{CIGNORE}.
298 If this function is called from a background process on its controlling
299 terminal, normally all processes in the process group are sent a
300 @code{SIGTTOU} signal, in the same way as if the process were trying to
301 write to the terminal. The exception is if the calling process itself
302 is ignoring or blocking @code{SIGTTOU} signals, in which case the
303 operation is performed and no signal is sent. @xref{Job Control}.
305 If successful, @code{tcsetattr} returns @code{0}. A return value of
306 @code{-1} indicates an error. The following @code{errno} error
307 conditions are defined for this function:
311 The @var{filedes} argument is not a valid file descriptor.
314 The @var{filedes} is not associated with a terminal.
317 Either the value of the @code{when} argument is not valid, or there is
318 something wrong with the data in the @var{termios-p} argument.
322 Although @code{tcgetattr} and @code{tcsetattr} specify the terminal
323 device with a file descriptor, the attributes are those of the terminal
324 device itself and not of the file descriptor. This means that the
325 effects of changing terminal attributes are persistent; if another
326 process opens the terminal file later on, it will see the changed
327 attributes even though it doesn't have anything to do with the open file
328 descriptor you originally specified in changing the attributes.
330 Similarly, if a single process has multiple or duplicated file
331 descriptors for the same terminal device, changing the terminal
332 attributes affects input and output to all of these file
333 descriptors. This means, for example, that you can't open one file
334 descriptor or stream to read from a terminal in the normal
335 line-buffered, echoed mode; and simultaneously have another file
336 descriptor for the same terminal that you use to read from it in
337 single-character, non-echoed mode. Instead, you have to explicitly
338 switch the terminal back and forth between the two modes.
341 @subsection Setting Terminal Modes Properly
343 When you set terminal modes, you should call @code{tcgetattr} first to
344 get the current modes of the particular terminal device, modify only
345 those modes that you are really interested in, and store the result with
348 It's a bad idea to simply initialize a @code{struct termios} structure
349 to a chosen set of attributes and pass it directly to @code{tcsetattr}.
350 Your program may be run years from now, on systems that support members
351 not documented in this manual. The way to avoid setting these members
352 to unreasonable values is to avoid changing them.
354 What's more, different terminal devices may require different mode
355 settings in order to function properly. So you should avoid blindly
356 copying attributes from one terminal device to another.
358 When a member contains a collection of independent flags, as the
359 @code{c_iflag}, @code{c_oflag} and @code{c_cflag} members do, even
360 setting the entire member is a bad idea, because particular operating
361 systems have their own flags. Instead, you should start with the
362 current value of the member and alter only the flags whose values matter
363 in your program, leaving any other flags unchanged.
365 Here is an example of how to set one flag (@code{ISTRIP}) in the
366 @code{struct termios} structure while properly preserving all the other
367 data in the structure:
372 set_istrip (int desc, int value)
374 struct termios settings;
379 result = tcgetattr (desc, &settings);
382 perror ("error in tcgetattr");
387 settings.c_iflag &= ~ISTRIP;
389 settings.c_iflag |= ISTRIP;
392 result = tcsetattr (desc, TCSANOW, &settings);
395 perror ("error in tcgetattr");
404 @subsection Input Modes
406 This section describes the terminal attribute flags that control
407 fairly low-level aspects of input processing: handling of parity errors,
408 break signals, flow control, and @key{RET} and @key{LFD} characters.
410 All of these flags are bits in the @code{c_iflag} member of the
411 @code{struct termios} structure. The member is an integer, and you
412 change flags using the operators @code{&}, @code{|} and @code{^}. Don't
413 try to specify the entire value for @code{c_iflag}---instead, change
414 only specific flags and leave the rest untouched (@pxref{Setting
419 @deftypevr Macro tcflag_t INPCK
420 @cindex parity checking
421 If this bit is set, input parity checking is enabled. If it is not set,
422 no checking at all is done for parity errors on input; the
423 characters are simply passed through to the application.
425 Parity checking on input processing is independent of whether parity
426 detection and generation on the underlying terminal hardware is enabled;
427 see @ref{Control Modes}. For example, you could clear the @code{INPCK}
428 input mode flag and set the @code{PARENB} control mode flag to ignore
429 parity errors on input, but still generate parity on output.
431 If this bit is set, what happens when a parity error is detected depends
432 on whether the @code{IGNPAR} or @code{PARMRK} bits are set. If neither
433 of these bits are set, a byte with a parity error is passed to the
434 application as a @code{'\0'} character.
439 @deftypevr Macro tcflag_t IGNPAR
440 If this bit is set, any byte with a framing or parity error is ignored.
441 This is only useful if @code{INPCK} is also set.
446 @deftypevr Macro tcflag_t PARMRK
447 If this bit is set, input bytes with parity or framing errors are marked
448 when passed to the program. This bit is meaningful only when
449 @code{INPCK} is set and @code{IGNPAR} is not set.
451 The way erroneous bytes are marked is with two preceding bytes,
452 @code{377} and @code{0}. Thus, the program actually reads three bytes
453 for one erroneous byte received from the terminal.
455 If a valid byte has the value @code{0377}, and @code{ISTRIP} (see below)
456 is not set, the program might confuse it with the prefix that marks a
457 parity error. So a valid byte @code{0377} is passed to the program as
458 two bytes, @code{0377} @code{0377}, in this case.
463 @deftypevr Macro tcflag_t ISTRIP
464 If this bit is set, valid input bytes are stripped to seven bits;
465 otherwise, all eight bits are available for programs to read.
470 @deftypevr Macro tcflag_t IGNBRK
471 If this bit is set, break conditions are ignored.
473 @cindex break condition, detecting
474 A @dfn{break condition} is defined in the context of asynchronous
475 serial data transmission as a series of zero-value bits longer than a
481 @deftypevr Macro tcflag_t BRKINT
482 If this bit is set and @code{IGNBRK} is not set, a break condition
483 clears the terminal input and output queues and raises a @code{SIGINT}
484 signal for the foreground process group associated with the terminal.
486 If neither @code{BRKINT} nor @code{IGNBRK} are set, a break condition is
487 passed to the application as a single @code{'\0'} character if
488 @code{PARMRK} is not set, or otherwise as a three-character sequence
489 @code{'\377'}, @code{'\0'}, @code{'\0'}.
494 @deftypevr Macro tcflag_t IGNCR
495 If this bit is set, carriage return characters (@code{'\r'}) are
496 discarded on input. Discarding carriage return may be useful on
497 terminals that send both carriage return and linefeed when you type the
503 @deftypevr Macro tcflag_t ICRNL
504 If this bit is set and @code{IGNCR} is not set, carriage return characters
505 (@code{'\r'}) received as input are passed to the application as newline
506 characters (@code{'\n'}).
511 @deftypevr Macro tcflag_t INLCR
512 If this bit is set, newline characters (@code{'\n'}) received as input
513 are passed to the application as carriage return characters (@code{'\r'}).
518 @deftypevr Macro tcflag_t IXOFF
519 If this bit is set, start/stop control on input is enabled. In other
520 words, the computer sends STOP and START characters as necessary to
521 prevent input from coming in faster than programs are reading it. The
522 idea is that the actual terminal hardware that is generating the input
523 data responds to a STOP character by suspending transmission, and to a
524 START character by resuming transmission. @xref{Start/Stop Characters}.
529 @deftypevr Macro tcflag_t IXON
530 If this bit is set, start/stop control on output is enabled. In other
531 words, if the computer receives a STOP character, it suspends output
532 until a START character is received. In this case, the STOP and START
533 characters are never passed to the application program. If this bit is
534 not set, then START and STOP can be read as ordinary characters.
535 @xref{Start/Stop Characters}.
536 @c !!! mention this interferes with using C-s and C-q for programs like emacs
541 @deftypevr Macro tcflag_t IXANY
542 If this bit is set, any input character restarts output when output has
543 been suspended with the STOP character. Otherwise, only the START
544 character restarts output.
546 This is a BSD extension; it exists only on BSD systems and the GNU system.
551 @deftypevr Macro tcflag_t IMAXBEL
552 If this bit is set, then filling up the terminal input buffer sends a
553 BEL character (code @code{007}) to the terminal to ring the bell.
555 This is a BSD extension.
559 @subsection Output Modes
561 This section describes the terminal flags and fields that control how
562 output characters are translated and padded for display. All of these
563 are contained in the @code{c_oflag} member of the @w{@code{struct termios}}
566 The @code{c_oflag} member itself is an integer, and you change the flags
567 and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
568 try to specify the entire value for @code{c_oflag}---instead, change
569 only specific flags and leave the rest untouched (@pxref{Setting
574 @deftypevr Macro tcflag_t OPOST
575 If this bit is set, output data is processed in some unspecified way so
576 that it is displayed appropriately on the terminal device. This
577 typically includes mapping newline characters (@code{'\n'}) onto
578 carriage return and linefeed pairs.
580 If this bit isn't set, the characters are transmitted as-is.
583 The following three bits are BSD features, and they exist only BSD
584 systems and the GNU system. They are effective only if @code{OPOST} is
589 @deftypevr Macro tcflag_t ONLCR
590 If this bit is set, convert the newline character on output into a pair
591 of characters, carriage return followed by linefeed.
596 @deftypevr Macro tcflag_t OXTABS
597 If this bit is set, convert tab characters on output into the appropriate
598 number of spaces to emulate a tab stop every eight columns.
603 @deftypevr Macro tcflag_t ONOEOT
604 If this bit is set, discard @kbd{C-d} characters (code @code{004}) on
605 output. These characters cause many dial-up terminals to disconnect.
609 @subsection Control Modes
611 This section describes the terminal flags and fields that control
612 parameters usually associated with asynchronous serial data
613 transmission. These flags may not make sense for other kinds of
614 terminal ports (such as a network connection pseudo-terminal). All of
615 these are contained in the @code{c_cflag} member of the @code{struct
618 The @code{c_cflag} member itself is an integer, and you change the flags
619 and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
620 try to specify the entire value for @code{c_cflag}---instead, change
621 only specific flags and leave the rest untouched (@pxref{Setting
626 @deftypevr Macro tcflag_t CLOCAL
627 If this bit is set, it indicates that the terminal is connected
628 ``locally'' and that the modem status lines (such as carrier detect)
630 @cindex modem status lines
631 @cindex carrier detect
633 On many systems if this bit is not set and you call @code{open} without
634 the @code{O_NONBLOCK} flag set, @code{open} blocks until a modem
635 connection is established.
637 If this bit is not set and a modem disconnect is detected, a
638 @code{SIGHUP} signal is sent to the controlling process group for the
639 terminal (if it has one). Normally, this causes the process to exit;
640 see @ref{Signal Handling}. Reading from the terminal after a disconnect
641 causes an end-of-file condition, and writing causes an @code{EIO} error
642 to be returned. The terminal device must be closed and reopened to
644 @cindex modem disconnect
649 @deftypevr Macro tcflag_t HUPCL
650 If this bit is set, a modem disconnect is generated when all processes
651 that have the terminal device open have either closed the file or exited.
656 @deftypevr Macro tcflag_t CREAD
657 If this bit is set, input can be read from the terminal. Otherwise,
658 input is discarded when it arrives.
663 @deftypevr Macro tcflag_t CSTOPB
664 If this bit is set, two stop bits are used. Otherwise, only one stop bit
670 @deftypevr Macro tcflag_t PARENB
671 If this bit is set, generation and detection of a parity bit are enabled.
672 @xref{Input Modes}, for information on how input parity errors are handled.
674 If this bit is not set, no parity bit is added to output characters, and
675 input characters are not checked for correct parity.
680 @deftypevr Macro tcflag_t PARODD
681 This bit is only useful if @code{PARENB} is set. If @code{PARODD} is set,
682 odd parity is used, otherwise even parity is used.
685 The control mode flags also includes a field for the number of bits per
686 character. You can use the @code{CSIZE} macro as a mask to extract the
687 value, like this: @code{settings.c_cflag & CSIZE}.
691 @deftypevr Macro tcflag_t CSIZE
692 This is a mask for the number of bits per character.
697 @deftypevr Macro tcflag_t CS5
698 This specifies five bits per byte.
703 @deftypevr Macro tcflag_t CS6
704 This specifies six bits per byte.
709 @deftypevr Macro tcflag_t CS7
710 This specifies seven bits per byte.
715 @deftypevr Macro tcflag_t CS8
716 This specifies eight bits per byte.
719 The following four bits are BSD extensions; this exist only on BSD
720 systems and the GNU system.
724 @deftypevr Macro tcflag_t CCTS_OFLOW
725 If this bit is set, enable flow control of output based on the CTS wire
731 @deftypevr Macro tcflag_t CRTS_IFLOW
732 If this bit is set, enable flow control of input based on the RTS wire
738 @deftypevr Macro tcflag_t MDMBUF
739 If this bit is set, enable carrier-based flow control of output.
744 @deftypevr Macro tcflag_t CIGNORE
745 If this bit is set, it says to ignore the control modes and line speed
746 values entirely. This is only meaningful in a call to @code{tcsetattr}.
748 The @code{c_cflag} member and the line speed values returned by
749 @code{cfgetispeed} and @code{cfgetospeed} will be unaffected by the
750 call. @code{CIGNORE} is useful if you want to set all the software
751 modes in the other members, but leave the hardware details in
752 @code{c_cflag} unchanged. (This is how the @code{TCSASOFT} flag to
753 @code{tcsettattr} works.)
755 This bit is never set in the structure filled in by @code{tcgetattr}.
759 @subsection Local Modes
761 This section describes the flags for the @code{c_lflag} member of the
762 @code{struct termios} structure. These flags generally control
763 higher-level aspects of input processing than the input modes flags
764 described in @ref{Input Modes}, such as echoing, signals, and the choice
765 of canonical or noncanonical input.
767 The @code{c_lflag} member itself is an integer, and you change the flags
768 and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
769 try to specify the entire value for @code{c_lflag}---instead, change
770 only specific flags and leave the rest untouched (@pxref{Setting
775 @deftypevr Macro tcflag_t ICANON
776 This bit, if set, enables canonical input processing mode. Otherwise,
777 input is processed in noncanonical mode. @xref{Canonical or Not}.
782 @deftypevr Macro tcflag_t ECHO
783 If this bit is set, echoing of input characters back to the terminal
785 @cindex echo of terminal input
790 @deftypevr Macro tcflag_t ECHOE
791 If this bit is set, echoing indicates erasure of input with the ERASE
792 character by erasing the last character in the current line from the
793 screen. Otherwise, the character erased is re-echoed to show what has
794 happened (suitable for a printing terminal).
796 This bit only controls the display behavior; the @code{ICANON} bit by
797 itself controls actual recognition of the ERASE character and erasure of
798 input, without which @code{ECHOE} is simply irrelevant.
803 @deftypevr Macro tcflag_t ECHOPRT
804 This bit is like @code{ECHOE}, enables display of the ERASE character in
805 a way that is geared to a hardcopy terminal. When you type the ERASE
806 character, a @samp{\} character is printed followed by the first
807 character erased. Typing the ERASE character again just prints the next
808 character erased. Then, the next time you type a normal character, a
809 @samp{/} character is printed before the character echoes.
811 This is a BSD extension, and exists only in BSD systems and the
817 @deftypevr Macro tcflag_t ECHOK
818 This bit enables special display of the KILL character by moving to a
819 new line after echoing the KILL character normally. The behavior of
820 @code{ECHOKE} (below) is nicer to look at.
822 If this bit is not set, the KILL character echoes just as it would if it
823 were not the KILL character. Then it is up to the user to remember that
824 the KILL character has erased the preceding input; there is no
825 indication of this on the screen.
827 This bit only controls the display behavior; the @code{ICANON} bit by
828 itself controls actual recognition of the KILL character and erasure of
829 input, without which @code{ECHOK} is simply irrelevant.
834 @deftypevr Macro tcflag_t ECHOKE
835 This bit is similar to @code{ECHOK}. It enables special display of the
836 KILL character by erasing on the screen the entire line that has been
837 killed. This is a BSD extension, and exists only in BSD systems and the
843 @deftypevr Macro tcflag_t ECHONL
844 If this bit is set and the @code{ICANON} bit is also set, then the
845 newline (@code{'\n'}) character is echoed even if the @code{ECHO} bit
851 @deftypevr Macro tcflag_t ECHOCTL
852 If this bit is set and the @code{ECHO} bit is also set, echo control
853 characters with @samp{^} followed by the corresponding text character.
854 Thus, control-A echoes as @samp{^A}. This is usually the preferred mode
855 for interactive input, because echoing a control character back to the
856 terminal could have some undesired effect on the terminal.
858 This is a BSD extension, and exists only in BSD systems and the
864 @deftypevr Macro tcflag_t ISIG
865 This bit controls whether the INTR, QUIT, and SUSP characters are
866 recognized. The functions associated with these characters are performed
867 if and only if this bit is set. Being in canonical or noncanonical
868 input mode has no affect on the interpretation of these characters.
870 You should use caution when disabling recognition of these characters.
871 Programs that cannot be interrupted interactively are very
872 user-unfriendly. If you clear this bit, your program should provide
873 some alternate interface that allows the user to interactively send the
874 signals associated with these characters, or to escape from the program.
875 @cindex interactive signals, from terminal
877 @xref{Signal Characters}.
882 @deftypevr Macro tcflag_t IEXTEN
883 POSIX.1 gives @code{IEXTEN} implementation-defined meaning,
884 so you cannot rely on this interpretation on all systems.
886 On BSD systems and the GNU system, it enables the LNEXT and DISCARD characters.
887 @xref{Other Special}.
892 @deftypevr Macro tcflag_t NOFLSH
893 Normally, the INTR, QUIT, and SUSP characters cause input and output
894 queues for the terminal to be cleared. If this bit is set, the queues
900 @deftypevr Macro tcflag_t TOSTOP
901 If this bit is set and the system supports job control, then
902 @code{SIGTTOU} signals are generated by background processes that
903 attempt to write to the terminal. @xref{Access to the Terminal}.
906 The following bits are BSD extensions; they exist only in BSD systems
911 @deftypevr Macro tcflag_t ALTWERASE
912 This bit determines how far the WERASE character should erase. The
913 WERASE character erases back to the beginning of a word; the question
914 is, where do words begin?
916 If this bit is clear, then the beginning of a word is a nonwhitespace
917 character following a whitespace character. If the bit is set, then the
918 beginning of a word is an alphanumeric character or underscore following
919 a character which is none of those.
921 @xref{Editing Characters}, for more information about the WERASE character.
926 @deftypevr Macro tcflag_t FLUSHO
927 This is the bit that toggles when the user types the DISCARD character.
928 While this bit is set, all output is discarded. @xref{Other Special}.
933 @deftypevr Macro tcflag_t NOKERNINFO
934 Setting this bit disables handling of the STATUS character.
935 @xref{Other Special}.
940 @deftypevr Macro tcflag_t PENDIN
941 If this bit is set, it indicates that there is a line of input that
942 needs to be reprinted. Typing the REPRINT character sets this bit; the
943 bit remains set until reprinting is finished. @xref{Editing Characters}.
946 @c EXTPROC is too obscure to document now. --roland
949 @subsection Line Speed
952 @cindex terminal line speed
953 @cindex terminal line speed
955 The terminal line speed tells the computer how fast to read and write
956 data on the terminal.
958 If the terminal is connected to a real serial line, the terminal speed
959 you specify actually controls the line---if it doesn't match the
960 terminal's own idea of the speed, communication does not work. Real
961 serial ports accept only certain standard speeds. Also, particular
962 hardware may not support even all the standard speeds. Specifying a
963 speed of zero hangs up a dialup connection and turns off modem control
966 If the terminal is not a real serial line (for example, if it is a
967 network connection), then the line speed won't really affect data
968 transmission speed, but some programs will use it to determine the
969 amount of padding needed. It's best to specify a line speed value that
970 matches the actual speed of the actual terminal, but you can safely
971 experiment with different values to vary the amount of padding.
973 There are actually two line speeds for each terminal, one for input and
974 one for output. You can set them independently, but most often
975 terminals use the same speed for both directions.
977 The speed values are stored in the @code{struct termios} structure, but
978 don't try to access them in the @code{struct termios} structure
979 directly. Instead, you should use the following functions to read and
984 @deftypefun speed_t cfgetospeed (const struct termios *@var{termios-p})
985 This function returns the output line speed stored in the structure
986 @code{*@var{termios-p}}.
991 @deftypefun speed_t cfgetispeed (const struct termios *@var{termios-p})
992 This function returns the input line speed stored in the structure
993 @code{*@var{termios-p}}.
998 @deftypefun int cfsetospeed (struct termios *@var{termios-p}, speed_t @var{speed})
999 This function stores @var{speed} in @code{*@var{termios-p}} as the output
1000 speed. The normal return value is @code{0}; a value of @code{-1}
1001 indicates an error. If @var{speed} is not a speed, @code{cfsetospeed}
1007 @deftypefun int cfsetispeed (struct termios *@var{termios-p}, speed_t @var{speed})
1008 This function stores @var{speed} in @code{*@var{termios-p}} as the input
1009 speed. The normal return value is @code{0}; a value of @code{-1}
1010 indicates an error. If @var{speed} is not a speed, @code{cfsetospeed}
1016 @deftypefun int cfsetspeed (struct termios *@var{termios-p}, speed_t @var{speed})
1017 This function stores @var{speed} in @code{*@var{termios-p}} as both the
1018 input and output speeds. The normal return value is @code{0}; a value
1019 of @code{-1} indicates an error. If @var{speed} is not a speed,
1020 @code{cfsetspeed} returns @code{-1}. This function is an extension in
1026 @deftp {Data Type} speed_t
1027 The @code{speed_t} type is an unsigned integer data type used to
1028 represent line speeds.
1031 The functions @code{cfsetospeed} and @code{cfsetispeed} report errors
1032 only for speed values that the system simply cannot handle. If you
1033 specify a speed value that is basically acceptable, then those functions
1034 will succeed. But they do not check that a particular hardware device
1035 can actually support the specified speeds---in fact, they don't know
1036 which device you plan to set the speed for. If you use @code{tcsetattr}
1037 to set the speed of a particular device to a value that it cannot
1038 handle, @code{tcsetattr} returns @code{-1}.
1040 @strong{Portability note:} In the GNU library, the functions above
1041 accept speeds measured in bits per second as input, and return speed
1042 values measured in bits per second. Other libraries require speeds to
1043 be indicated by special codes. For POSIX.1 portability, you must use
1044 one of the following symbols to represent the speed; their precise
1045 numeric values are system-dependent, but each name has a fixed meaning:
1046 @code{B110} stands for 110 bps, @code{B300} for 300 bps, and so on.
1047 There is no portable way to represent any speed but these, but these are
1048 the only speeds that typical serial lines can support.
1111 B0 B50 B75 B110 B134 B150 B200
1112 B300 B600 B1200 B1800 B2400 B4800
1113 B9600 B19200 B38400 B57600 B115200
1119 BSD defines two additional speed symbols as aliases: @code{EXTA} is an
1120 alias for @code{B19200} and @code{EXTB} is an alias for @code{B38400}.
1121 These aliases are obsolete.
1123 @node Special Characters
1124 @subsection Special Characters
1126 In canonical input, the terminal driver recognizes a number of special
1127 characters which perform various control functions. These include the
1128 ERASE character (usually @key{DEL}) for editing input, and other editing
1129 characters. The INTR character (normally @kbd{C-c}) for sending a
1130 @code{SIGINT} signal, and other signal-raising characters, may be
1131 available in either canonical or noncanonical input mode. All these
1132 characters are described in this section.
1134 The particular characters used are specified in the @code{c_cc} member
1135 of the @code{struct termios} structure. This member is an array; each
1136 element specifies the character for a particular role. Each element has
1137 a symbolic constant that stands for the index of that element---for
1138 example, @code{INTR} is the index of the element that specifies the INTR
1139 character, so storing @code{'='} in @code{@var{termios}.c_cc[INTR]}
1140 specifies @samp{=} as the INTR character.
1142 @vindex _POSIX_VDISABLE
1143 On some systems, you can disable a particular special character function
1144 by specifying the value @code{_POSIX_VDISABLE} for that role. This
1145 value is unequal to any possible character code. @xref{Options for
1146 Files}, for more information about how to tell whether the operating
1147 system you are using supports @code{_POSIX_VDISABLE}.
1150 * Editing Characters:: Special characters that terminate lines and
1151 delete text, and other editing functions.
1152 * Signal Characters:: Special characters that send or raise signals
1153 to or for certain classes of processes.
1154 * Start/Stop Characters:: Special characters that suspend or resume
1156 * Other Special:: Other special characters for BSD systems:
1157 they can discard output, and print status.
1160 @node Editing Characters
1161 @subsubsection Characters for Input Editing
1163 These special characters are active only in canonical input mode.
1164 @xref{Canonical or Not}.
1168 @deftypevr Macro int VEOF
1169 @cindex EOF character
1170 This is the subscript for the EOF character in the special control
1171 character array. @code{@var{termios}.c_cc[VEOF]} holds the character
1174 The EOF character is recognized only in canonical input mode. It acts
1175 as a line terminator in the same way as a newline character, but if the
1176 EOF character is typed at the beginning of a line it causes @code{read}
1177 to return a byte count of zero, indicating end-of-file. The EOF
1178 character itself is discarded.
1180 Usually, the EOF character is @kbd{C-d}.
1185 @deftypevr Macro int VEOL
1186 @cindex EOL character
1187 This is the subscript for the EOL character in the special control
1188 character array. @code{@var{termios}.c_cc[VEOL]} holds the character
1191 The EOL character is recognized only in canonical input mode. It acts
1192 as a line terminator, just like a newline character. The EOL character
1193 is not discarded; it is read as the last character in the input line.
1195 @c !!! example: this is set to ESC by 4.3 csh with "set filec" so it can
1196 @c complete partial lines without using cbreak or raw mode.
1198 You don't need to use the EOL character to make @key{RET} end a line.
1199 Just set the ICRNL flag. In fact, this is the default state of
1205 @deftypevr Macro int VEOL2
1206 @cindex EOL2 character
1207 This is the subscript for the EOL2 character in the special control
1208 character array. @code{@var{termios}.c_cc[VEOL2]} holds the character
1211 The EOL2 character works just like the EOL character (see above), but it
1212 can be a different character. Thus, you can specify two characters to
1213 terminate an input line, by setting EOL to one of them and EOL2 to the
1216 The EOL2 character is a BSD extension; it exists only on BSD systems
1222 @deftypevr Macro int VERASE
1223 @cindex ERASE character
1224 This is the subscript for the ERASE character in the special control
1225 character array. @code{@var{termios}.c_cc[VERASE]} holds the
1228 The ERASE character is recognized only in canonical input mode. When
1229 the user types the erase character, the previous character typed is
1230 discarded. (If the terminal generates multibyte character sequences,
1231 this may cause more than one byte of input to be discarded.) This
1232 cannot be used to erase past the beginning of the current line of text.
1233 The ERASE character itself is discarded.
1234 @c !!! mention ECHOE here
1236 Usually, the ERASE character is @key{DEL}.
1241 @deftypevr Macro int VWERASE
1242 @cindex WERASE character
1243 This is the subscript for the WERASE character in the special control
1244 character array. @code{@var{termios}.c_cc[VWERASE]} holds the character
1247 The WERASE character is recognized only in canonical mode. It erases an
1248 entire word of prior input, and any whitespace after it; whitespace
1249 characters before the word are not erased.
1251 The definition of a ``word'' depends on the setting of the
1252 @code{ALTWERASE} mode; @pxref{Local Modes}.
1254 If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
1255 of any characters except space or tab.
1257 If the @code{ALTWERASE} mode is set, a word is defined as a sequence of
1258 characters containing only letters, numbers, and underscores, optionally
1259 followed by one character that is not a letter, number, or underscore.
1261 The WERASE character is usually @kbd{C-w}.
1263 This is a BSD extension.
1268 @deftypevr Macro int VKILL
1269 @cindex KILL character
1270 This is the subscript for the KILL character in the special control
1271 character array. @code{@var{termios}.c_cc[VKILL]} holds the character
1274 The KILL character is recognized only in canonical input mode. When the
1275 user types the kill character, the entire contents of the current line
1276 of input are discarded. The kill character itself is discarded too.
1278 The KILL character is usually @kbd{C-u}.
1283 @deftypevr Macro int VREPRINT
1284 @cindex REPRINT character
1285 This is the subscript for the REPRINT character in the special control
1286 character array. @code{@var{termios}.c_cc[VREPRINT]} holds the character
1289 The REPRINT character is recognized only in canonical mode. It reprints
1290 the current input line. If some asynchronous output has come while you
1291 are typing, this lets you see the line you are typing clearly again.
1293 The REPRINT character is usually @kbd{C-r}.
1295 This is a BSD extension.
1298 @node Signal Characters
1299 @subsubsection Characters that Cause Signals
1301 These special characters may be active in either canonical or noncanonical
1302 input mode, but only when the @code{ISIG} flag is set (@pxref{Local
1307 @deftypevr Macro int VINTR
1308 @cindex INTR character
1309 @cindex interrupt character
1310 This is the subscript for the INTR character in the special control
1311 character array. @code{@var{termios}.c_cc[VINTR]} holds the character
1314 The INTR (interrupt) character raises a @code{SIGINT} signal for all
1315 processes in the foreground job associated with the terminal. The INTR
1316 character itself is then discarded. @xref{Signal Handling}, for more
1317 information about signals.
1319 Typically, the INTR character is @kbd{C-c}.
1324 @deftypevr Macro int VQUIT
1325 @cindex QUIT character
1326 This is the subscript for the QUIT character in the special control
1327 character array. @code{@var{termios}.c_cc[VQUIT]} holds the character
1330 The QUIT character raises a @code{SIGQUIT} signal for all processes in
1331 the foreground job associated with the terminal. The QUIT character
1332 itself is then discarded. @xref{Signal Handling}, for more information
1335 Typically, the QUIT character is @kbd{C-\}.
1340 @deftypevr Macro int VSUSP
1341 @cindex SUSP character
1342 @cindex suspend character
1343 This is the subscript for the SUSP character in the special control
1344 character array. @code{@var{termios}.c_cc[VSUSP]} holds the character
1347 The SUSP (suspend) character is recognized only if the implementation
1348 supports job control (@pxref{Job Control}). It causes a @code{SIGTSTP}
1349 signal to be sent to all processes in the foreground job associated with
1350 the terminal. The SUSP character itself is then discarded.
1351 @xref{Signal Handling}, for more information about signals.
1353 Typically, the SUSP character is @kbd{C-z}.
1356 Few applications disable the normal interpretation of the SUSP
1357 character. If your program does this, it should provide some other
1358 mechanism for the user to stop the job. When the user invokes this
1359 mechanism, the program should send a @code{SIGTSTP} signal to the
1360 process group of the process, not just to the process itself.
1361 @xref{Signaling Another Process}.
1365 @deftypevr Macro int VDSUSP
1366 @cindex DSUSP character
1367 @cindex delayed suspend character
1368 This is the subscript for the DSUSP character in the special control
1369 character array. @code{@var{termios}.c_cc[VDSUSP]} holds the character
1372 The DSUSP (suspend) character is recognized only if the implementation
1373 supports job control (@pxref{Job Control}). It sends a @code{SIGTSTP}
1374 signal, like the SUSP character, but not right away---only when the
1375 program tries to read it as input. Not all systems with job control
1376 support DSUSP; only BSD-compatible systems (including the GNU system).
1378 @xref{Signal Handling}, for more information about signals.
1380 Typically, the DSUSP character is @kbd{C-y}.
1383 @node Start/Stop Characters
1384 @subsubsection Special Characters for Flow Control
1386 These special characters may be active in either canonical or noncanonical
1387 input mode, but their use is controlled by the flags @code{IXON} and
1388 @code{IXOFF} (@pxref{Input Modes}).
1392 @deftypevr Macro int VSTART
1393 @cindex START character
1394 This is the subscript for the START character in the special control
1395 character array. @code{@var{termios}.c_cc[VSTART]} holds the
1398 The START character is used to support the @code{IXON} and @code{IXOFF}
1399 input modes. If @code{IXON} is set, receiving a START character resumes
1400 suspended output; the START character itself is discarded. If
1401 @code{IXANY} is set, receiving any character at all resumes suspended
1402 output; the resuming character is not discarded unless it is the START
1403 character. @code{IXOFF} is set, the system may also transmit START
1404 characters to the terminal.
1406 The usual value for the START character is @kbd{C-q}. You may not be
1407 able to change this value---the hardware may insist on using @kbd{C-q}
1408 regardless of what you specify.
1413 @deftypevr Macro int VSTOP
1414 @cindex STOP character
1415 This is the subscript for the STOP character in the special control
1416 character array. @code{@var{termios}.c_cc[VSTOP]} holds the character
1419 The STOP character is used to support the @code{IXON} and @code{IXOFF}
1420 input modes. If @code{IXON} is set, receiving a STOP character causes
1421 output to be suspended; the STOP character itself is discarded. If
1422 @code{IXOFF} is set, the system may also transmit STOP characters to the
1423 terminal, to prevent the input queue from overflowing.
1425 The usual value for the STOP character is @kbd{C-s}. You may not be
1426 able to change this value---the hardware may insist on using @kbd{C-s}
1427 regardless of what you specify.
1431 @subsubsection Other Special Characters
1433 These special characters exist only in BSD systems and the GNU system.
1437 @deftypevr Macro int VLNEXT
1438 @cindex LNEXT character
1439 This is the subscript for the LNEXT character in the special control
1440 character array. @code{@var{termios}.c_cc[VLNEXT]} holds the character
1443 The LNEXT character is recognized only when @code{IEXTEN} is set, but in
1444 both canonical and noncanonical mode. It disables any special
1445 significance of the next character the user types. Even if the
1446 character would normally perform some editing function or generate a
1447 signal, it is read as a plain character. This is the analogue of the
1448 @kbd{C-q} command in Emacs. ``LNEXT'' stands for ``literal next.''
1450 The LNEXT character is usually @kbd{C-v}.
1455 @deftypevr Macro int VDISCARD
1456 @cindex DISCARD character
1457 This is the subscript for the DISCARD character in the special control
1458 character array. @code{@var{termios}.c_cc[VDISCARD]} holds the character
1461 The DISCARD character is recognized only when @code{IEXTEN} is set, but
1462 in both canonical and noncanonical mode. Its effect is to toggle the
1463 discard-output flag. When this flag is set, all program output is
1464 discarded. Setting the flag also discards all output currently in the
1465 output buffer. Typing any other character resets the flag.
1470 @deftypevr Macro int VSTATUS
1471 @cindex STATUS character
1472 This is the subscript for the STATUS character in the special control
1473 character array. @code{@var{termios}.c_cc[VSTATUS]} holds the character
1476 The STATUS character's effect is to print out a status message about how
1477 the current process is running.
1479 The STATUS character is recognized only in canonical mode, and only if
1480 @code{NOKERNINFO} is not set.
1483 @node Noncanonical Input
1484 @subsection Noncanonical Input
1486 In noncanonical input mode, the special editing characters such as
1487 ERASE and KILL are ignored. The system facilities for the user to edit
1488 input are disabled in noncanonical mode, so that all input characters
1489 (unless they are special for signal or flow-control purposes) are passed
1490 to the application program exactly as typed. It is up to the
1491 application program to give the user ways to edit the input, if
1494 Noncanonical mode offers special parameters called MIN and TIME for
1495 controlling whether and how long to wait for input to be available. You
1496 can even use them to avoid ever waiting---to return immediately with
1497 whatever input is available, or with no input.
1499 The MIN and TIME are stored in elements of the @code{c_cc} array, which
1500 is a member of the @w{@code{struct termios}} structure. Each element of
1501 this array has a particular role, and each element has a symbolic
1502 constant that stands for the index of that element. @code{VMIN} and
1503 @code{VMAX} are the names for the indices in the array of the MIN and
1508 @deftypevr Macro int VMIN
1509 @cindex MIN termios slot
1510 This is the subscript for the MIN slot in the @code{c_cc} array. Thus,
1511 @code{@var{termios}.c_cc[VMIN]} is the value itself.
1513 The MIN slot is only meaningful in noncanonical input mode; it
1514 specifies the minimum number of bytes that must be available in the
1515 input queue in order for @code{read} to return.
1520 @deftypevr Macro int VTIME
1521 @cindex TIME termios slot
1522 This is the subscript for the TIME slot in the @code{c_cc} array. Thus,
1523 @code{@var{termios}.c_cc[VTIME]} is the value itself.
1525 The TIME slot is only meaningful in noncanonical input mode; it
1526 specifies how long to wait for input before returning, in units of 0.1
1530 The MIN and TIME values interact to determine the criterion for when
1531 @code{read} should return; their precise meanings depend on which of
1532 them are nonzero. There are four possible cases:
1536 Both TIME and MIN are nonzero.
1538 In this case, TIME specifies how long to wait after each input character
1539 to see if more input arrives. After the first character received,
1540 @code{read} keeps waiting until either MIN bytes have arrived in all, or
1541 TIME elapses with no further input.
1543 @code{read} always blocks until the first character arrives, even if
1544 TIME elapses first. @code{read} can return more than MIN characters if
1545 more than MIN happen to be in the queue.
1548 Both MIN and TIME are zero.
1550 In this case, @code{read} always returns immediately with as many
1551 characters as are available in the queue, up to the number requested.
1552 If no input is immediately available, @code{read} returns a value of
1556 MIN is zero but TIME has a nonzero value.
1558 In this case, @code{read} waits for time TIME for input to become
1559 available; the availability of a single byte is enough to satisfy the
1560 read request and cause @code{read} to return. When it returns, it
1561 returns as many characters as are available, up to the number requested.
1562 If no input is available before the timer expires, @code{read} returns a
1566 TIME is zero but MIN has a nonzero value.
1568 In this case, @code{read} waits until at least MIN bytes are available
1569 in the queue. At that time, @code{read} returns as many characters as
1570 are available, up to the number requested. @code{read} can return more
1571 than MIN characters if more than MIN happen to be in the queue.
1574 What happens if MIN is 50 and you ask to read just 10 bytes?
1575 Normally, @code{read} waits until there are 50 bytes in the buffer (or,
1576 more generally, the wait condition described above is satisfied), and
1577 then reads 10 of them, leaving the other 40 buffered in the operating
1578 system for a subsequent call to @code{read}.
1580 @strong{Portability note:} On some systems, the MIN and TIME slots are
1581 actually the same as the EOF and EOL slots. This causes no serious
1582 problem because the MIN and TIME slots are used only in noncanonical
1583 input and the EOF and EOL slots are used only in canonical input, but it
1584 isn't very clean. The GNU library allocates separate slots for these
1589 @deftypefun int cfmakeraw (struct termios *@var{termios-p})
1590 This function provides an easy way to set up @code{*@var{termios-p}} for
1591 what has traditionally been called ``raw mode'' in BSD. This uses
1592 noncanonical input, and turns off most processing to give an unmodified
1593 channel to the terminal.
1595 It does exactly this:
1597 @var{termios-p}->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1598 |INLCR|IGNCR|ICRNL|IXON);
1599 @var{termios-p}->c_oflag &= ~OPOST;
1600 @var{termios-p}->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1601 @var{termios-p}->c_cflag &= ~(CSIZE|PARENB);
1602 @var{termios-p}->c_cflag |= CS8;
1607 @section Line Control Functions
1608 @cindex terminal line control functions
1610 These functions perform miscellaneous control actions on terminal
1611 devices. As regards terminal access, they are treated like doing
1612 output: if any of these functions is used by a background process on its
1613 controlling terminal, normally all processes in the process group are
1614 sent a @code{SIGTTOU} signal. The exception is if the calling process
1615 itself is ignoring or blocking @code{SIGTTOU} signals, in which case the
1616 operation is performed and no signal is sent. @xref{Job Control}.
1618 @cindex break condition, generating
1621 @deftypefun int tcsendbreak (int @var{filedes}, int @var{duration})
1622 This function generates a break condition by transmitting a stream of
1623 zero bits on the terminal associated with the file descriptor
1624 @var{filedes}. The duration of the break is controlled by the
1625 @var{duration} argument. If zero, the duration is between 0.25 and 0.5
1626 seconds. The meaning of a nonzero value depends on the operating system.
1628 This function does nothing if the terminal is not an asynchronous serial
1631 The return value is normally zero. In the event of an error, a value
1632 of @code{-1} is returned. The following @code{errno} error conditions
1633 are defined for this function:
1637 The @var{filedes} is not a valid file descriptor.
1640 The @var{filedes} is not associated with a terminal device.
1645 @cindex flushing terminal output queue
1646 @cindex terminal output queue, flushing
1649 @deftypefun int tcdrain (int @var{filedes})
1650 The @code{tcdrain} function waits until all queued
1651 output to the terminal @var{filedes} has been transmitted.
1653 This function is a cancelation point in multi-threaded programs. This
1654 is a problem if the thread allocates some resources (like memory, file
1655 descriptors, semaphores or whatever) at the time @code{tcdrain} is
1656 called. If the thread gets canceled these resources stay allocated
1657 until the program ends. To avoid this calls to @code{tcdrain} should be
1658 protected using cancelation handlers.
1659 @c ref pthread_cleanup_push / pthread_cleanup_pop
1661 The return value is normally zero. In the event of an error, a value
1662 of @code{-1} is returned. The following @code{errno} error conditions
1663 are defined for this function:
1667 The @var{filedes} is not a valid file descriptor.
1670 The @var{filedes} is not associated with a terminal device.
1673 The operation was interrupted by delivery of a signal.
1674 @xref{Interrupted Primitives}.
1679 @cindex clearing terminal input queue
1680 @cindex terminal input queue, clearing
1683 @deftypefun int tcflush (int @var{filedes}, int @var{queue})
1684 The @code{tcflush} function is used to clear the input and/or output
1685 queues associated with the terminal file @var{filedes}. The @var{queue}
1686 argument specifies which queue(s) to clear, and can be one of the
1689 @c Extra blank lines here make it look better.
1694 Clear any input data received, but not yet read.
1699 Clear any output data written, but not yet transmitted.
1704 Clear both queued input and output.
1707 The return value is normally zero. In the event of an error, a value
1708 of @code{-1} is returned. The following @code{errno} error conditions
1709 are defined for this function:
1713 The @var{filedes} is not a valid file descriptor.
1716 The @var{filedes} is not associated with a terminal device.
1719 A bad value was supplied as the @var{queue} argument.
1722 It is unfortunate that this function is named @code{tcflush}, because
1723 the term ``flush'' is normally used for quite another operation---waiting
1724 until all output is transmitted---and using it for discarding input or
1725 output would be confusing. Unfortunately, the name @code{tcflush} comes
1726 from POSIX and we cannot change it.
1729 @cindex flow control, terminal
1730 @cindex terminal flow control
1733 @deftypefun int tcflow (int @var{filedes}, int @var{action})
1734 The @code{tcflow} function is used to perform operations relating to
1735 XON/XOFF flow control on the terminal file specified by @var{filedes}.
1737 The @var{action} argument specifies what operation to perform, and can
1738 be one of the following values:
1743 Suspend transmission of output.
1747 Restart transmission of output.
1751 Transmit a STOP character.
1755 Transmit a START character.
1758 For more information about the STOP and START characters, see @ref{Special
1761 The return value is normally zero. In the event of an error, a value
1762 of @code{-1} is returned. The following @code{errno} error conditions
1763 are defined for this function:
1768 The @var{filedes} is not a valid file descriptor.
1772 The @var{filedes} is not associated with a terminal device.
1776 A bad value was supplied as the @var{action} argument.
1780 @node Noncanon Example
1781 @section Noncanonical Mode Example
1783 Here is an example program that shows how you can set up a terminal
1784 device to read single characters in noncanonical input mode, without
1788 @include termios.c.texi
1791 This program is careful to restore the original terminal modes before
1792 exiting or terminating with a signal. It uses the @code{atexit}
1793 function (@pxref{Cleanups on Exit}) to make sure this is done
1797 @c !!!! the example doesn't handle any signals!
1798 The signals handled in the example are the ones that typically occur due
1799 to actions of the user. It might be desirable to handle other signals
1800 such as SIGSEGV that can result from bugs in the program.
1803 The shell is supposed to take care of resetting the terminal modes when
1804 a process is stopped or continued; see @ref{Job Control}. But some
1805 existing shells do not actually do this, so you may wish to establish
1806 handlers for job control signals that reset terminal modes. The above