]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/terminal.texi
4e4be880ff6676570a3177140f05bd5f1c4cdd68
[thirdparty/glibc.git] / manual / terminal.texi
1 @node Low-Level Terminal Interface
2 @chapter Low-Level Terminal Interface
3
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.
9
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.
13
14 @menu
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,
21 signals, editing.
22 * Line Control:: Sending break sequences, clearing
23 terminal buffers @dots{}
24 * Noncanon Example:: How to read single characters without echo.
25 @end menu
26
27 @node Is It a Terminal
28 @section Identifying Terminals
29 @cindex terminal identification
30 @cindex identifying terminals
31
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}
35 function.
36
37 @pindex unistd.h
38 Prototypes for both @code{isatty} and @code{ttyname} are declared in
39 the header file @file{unistd.h}.
40
41 @comment unistd.h
42 @comment POSIX.1
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.
46 @end deftypefun
47
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}.
51
52 @comment unistd.h
53 @comment POSIX.1
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.
60 @end deftypefun
61
62 @node I/O Queues
63 @section I/O Queues
64
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}).
69
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.
75
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}.
87
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 START and STOP characters sent by the terminal to
94 stop and restart transmission of output.
95
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.
100
101 @node Canonical or Not
102 @section Two Styles of Input: Canonical or Not
103
104 POSIX systems support two basic modes of input: canonical and
105 noncanonical.
106
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.
113
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}.
118
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.
124
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}.
130
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.
135
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}.
138 @xref{Local Modes}.
139
140 @node Terminal Modes
141 @section Terminal Modes
142
143 @pindex termios.h
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
148
149 @menu
150 * Mode Data Types:: The data type @code{struct termios} and
151 related types.
152 * Mode Functions:: Functions to read and set the terminal
153 attributes.
154 * Setting Modes:: The right way to set terminal attributes
155 reliably.
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.
164 @end menu
165
166 @node Mode Data Types
167 @subsection Terminal Mode Data Types
168 @cindex terminal mode data types
169
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.
174
175 @comment termios.h
176 @comment POSIX.1
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:
180
181 @table @code
182 @item tcflag_t c_iflag
183 A bit mask specifying flags for input modes; see @ref{Input Modes}.
184
185 @item tcflag_t c_oflag
186 A bit mask specifying flags for output modes; see @ref{Output Modes}.
187
188 @item tcflag_t c_cflag
189 A bit mask specifying flags for control modes; see @ref{Control Modes}.
190
191 @item tcflag_t c_lflag
192 A bit mask specifying flags for local modes; see @ref{Local Modes}.
193
194 @item cc_t c_cc[NCCS]
195 An array specifying which characters are associated with various
196 control functions; see @ref{Special Characters}.
197 @end table
198
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
202 speed values.
203 @end deftp
204
205 The following sections describe the details of the members of the
206 @code{struct termios} structure.
207
208 @comment termios.h
209 @comment POSIX.1
210 @deftp {Data Type} tcflag_t
211 This is an unsigned integer type used to represent the various
212 bit masks for terminal flags.
213 @end deftp
214
215 @comment termios.h
216 @comment POSIX.1
217 @deftp {Data Type} cc_t
218 This is an unsigned integer type used to represent characters associated
219 with various terminal control functions.
220 @end deftp
221
222 @comment termios.h
223 @comment POSIX.1
224 @deftypevr Macro int NCCS
225 The value of this macro is the number of elements in the @code{c_cc}
226 array.
227 @end deftypevr
228
229 @node Mode Functions
230 @subsection Terminal Mode Functions
231 @cindex terminal mode functions
232
233 @comment termios.h
234 @comment POSIX.1
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.
239
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:
243
244 @table @code
245 @item EBADF
246 The @var{filedes} argument is not a valid file descriptor.
247
248 @item ENOTTY
249 The @var{filedes} is not associated with a terminal.
250 @end table
251 @end deftypefun
252
253 @comment termios.h
254 @comment POSIX.1
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.
259
260 The @var{when} argument specifies how to deal with input and output
261 already queued. It can be one of the following values:
262
263 @table @code
264 @comment termios.h
265 @comment POSIX.1
266 @item TCSANOW
267 @vindex TCSANOW
268 Make the change immediately.
269
270 @comment termios.h
271 @comment POSIX.1
272 @item TCSADRAIN
273 @vindex TCSADRAIN
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
276 output.
277
278 @comment termios.h
279 @comment POSIX.1
280 @item TCSAFLUSH
281 @vindex TCSAFLUSH
282 This is like @code{TCSADRAIN}, but also discards any queued input.
283
284 @comment termios.h
285 @comment BSD
286 @item TCSASOFT
287 @vindex TCSASOFT
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
291 and the GNU system.
292
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}.
296 @end table
297
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}.
304
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:
308
309 @table @code
310 @item EBADF
311 The @var{filedes} argument is not a valid file descriptor.
312
313 @item ENOTTY
314 The @var{filedes} is not associated with a terminal.
315
316 @item EINVAL
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.
319 @end table
320 @end deftypefun
321
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.
329
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.
339
340 @node Setting Modes
341 @subsection Setting Terminal Modes Properly
342
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
346 @code{tcsetattr}.
347
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.
353
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.
357
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.
364
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:
368
369 @smallexample
370 @group
371 int
372 set_istrip (int desc, int value)
373 @{
374 struct termios settings;
375 int result;
376 @end group
377
378 @group
379 result = tcgetattr (desc, &settings);
380 if (result < 0)
381 @{
382 perror ("error in tcgetattr");
383 return 0;
384 @}
385 @end group
386 @group
387 settings.c_iflag &= ~ISTRIP;
388 if (value)
389 settings.c_iflag |= ISTRIP;
390 @end group
391 @group
392 result = tcsetattr (desc, TCSANOW, &settings);
393 if (result < 0)
394 @{
395 perror ("error in tcgetattr");
396 return;
397 @}
398 return 1;
399 @}
400 @end group
401 @end smallexample
402
403 @node Input Modes
404 @subsection Input Modes
405
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.
409
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
415 Modes}).
416
417 @comment termios.h
418 @comment POSIX.1
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.
424
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.
430
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.
435 @end deftypevr
436
437 @comment termios.h
438 @comment POSIX.1
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.
442 @end deftypevr
443
444 @comment termios.h
445 @comment POSIX.1
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.
450
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.
454
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.
459 @end deftypevr
460
461 @comment termios.h
462 @comment POSIX.1
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.
466 @end deftypevr
467
468 @comment termios.h
469 @comment POSIX.1
470 @deftypevr Macro tcflag_t IGNBRK
471 If this bit is set, break conditions are ignored.
472
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
476 single byte.
477 @end deftypevr
478
479 @comment termios.h
480 @comment POSIX.1
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.
485
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'}.
490 @end deftypevr
491
492 @comment termios.h
493 @comment POSIX.1
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
498 @key{RET} key.
499 @end deftypevr
500
501 @comment termios.h
502 @comment POSIX.1
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'}).
507 @end deftypevr
508
509 @comment termios.h
510 @comment POSIX.1
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'}).
514 @end deftypevr
515
516 @comment termios.h
517 @comment POSIX.1
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}.
525 @end deftypevr
526
527 @comment termios.h
528 @comment POSIX.1
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
537 @end deftypevr
538
539 @comment termios.h
540 @comment BSD
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.
545
546 This is a BSD extension; it exists only on BSD systems and the GNU system.
547 @end deftypevr
548
549 @comment termios.h
550 @comment BSD
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.
554
555 This is a BSD extension.
556 @end deftypevr
557
558 @node Output Modes
559 @subsection Output Modes
560
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}}
564 structure.
565
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
570 Modes}).
571
572 @comment termios.h
573 @comment POSIX.1
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.
579
580 If this bit isn't set, the characters are transmitted as-is.
581 @end deftypevr
582
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
585 set.
586
587 @comment termios.h
588 @comment BSD
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.
592 @end deftypevr
593
594 @comment termios.h
595 @comment BSD
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.
599 @end deftypevr
600
601 @comment termios.h
602 @comment BSD
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.
606 @end deftypevr
607
608 @node Control Modes
609 @subsection Control Modes
610
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
616 termios} structure.
617
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
622 Modes}).
623
624 @comment termios.h
625 @comment POSIX.1
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)
629 should be ignored.
630 @cindex modem status lines
631 @cindex carrier detect
632
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.
636
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
643 clear the condition.
644 @cindex modem disconnect
645 @end deftypevr
646
647 @comment termios.h
648 @comment POSIX.1
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.
652 @end deftypevr
653
654 @comment termios.h
655 @comment POSIX.1
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.
659 @end deftypevr
660
661 @comment termios.h
662 @comment POSIX.1
663 @deftypevr Macro tcflag_t CSTOPB
664 If this bit is set, two stop bits are used. Otherwise, only one stop bit
665 is used.
666 @end deftypevr
667
668 @comment termios.h
669 @comment POSIX.1
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.
673
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.
676 @end deftypevr
677
678 @comment termios.h
679 @comment POSIX.1
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.
683 @end deftypevr
684
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}.
688
689 @comment termios.h
690 @comment POSIX.1
691 @deftypevr Macro tcflag_t CSIZE
692 This is a mask for the number of bits per character.
693 @end deftypevr
694
695 @comment termios.h
696 @comment POSIX.1
697 @deftypevr Macro tcflag_t CS5
698 This specifies five bits per byte.
699 @end deftypevr
700
701 @comment termios.h
702 @comment POSIX.1
703 @deftypevr Macro tcflag_t CS6
704 This specifies six bits per byte.
705 @end deftypevr
706
707 @comment termios.h
708 @comment POSIX.1
709 @deftypevr Macro tcflag_t CS7
710 This specifies seven bits per byte.
711 @end deftypevr
712
713 @comment termios.h
714 @comment POSIX.1
715 @deftypevr Macro tcflag_t CS8
716 This specifies eight bits per byte.
717 @end deftypevr
718
719 The following four bits are BSD extensions; this exist only on BSD
720 systems and the GNU system.
721
722 @comment termios.h
723 @comment BSD
724 @deftypevr Macro tcflag_t CCTS_OFLOW
725 If this bit is set, enable flow control of output based on the CTS wire
726 (RS232 protocol).
727 @end deftypevr
728
729 @comment termios.h
730 @comment BSD
731 @deftypevr Macro tcflag_t CRTS_IFLOW
732 If this bit is set, enable flow control of input based on the RTS wire
733 (RS232 protocol).
734 @end deftypevr
735
736 @comment termios.h
737 @comment BSD
738 @deftypevr Macro tcflag_t MDMBUF
739 If this bit is set, enable carrier-based flow control of output.
740 @end deftypevr
741
742 @comment termios.h
743 @comment BSD
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}.
747
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.)
754
755 This bit is never set in the structure filled in by @code{tcgetattr}.
756 @end deftypevr
757
758 @node Local Modes
759 @subsection Local Modes
760
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.
766
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
771 Modes}).
772
773 @comment termios.h
774 @comment POSIX.1
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}.
778 @end deftypevr
779
780 @comment termios.h
781 @comment POSIX.1
782 @deftypevr Macro tcflag_t ECHO
783 If this bit is set, echoing of input characters back to the terminal
784 is enabled.
785 @cindex echo of terminal input
786 @end deftypevr
787
788 @comment termios.h
789 @comment POSIX.1
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).
795
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.
799 @end deftypevr
800
801 @comment termios.h
802 @comment BSD
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.
810
811 This is a BSD extension, and exists only in BSD systems and the
812 GNU system.
813 @end deftypevr
814
815 @comment termios.h
816 @comment POSIX.1
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.
821
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.
826
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.
830 @end deftypevr
831
832 @comment termios.h
833 @comment BSD
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
838 GNU system.
839 @end deftypevr
840
841 @comment termios.h
842 @comment POSIX.1
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
846 is not set.
847 @end deftypevr
848
849 @comment termios.h
850 @comment BSD
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.
857
858 This is a BSD extension, and exists only in BSD systems and the
859 GNU system.
860 @end deftypevr
861
862 @comment termios.h
863 @comment POSIX.1
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.
869
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
876
877 @xref{Signal Characters}.
878 @end deftypevr
879
880 @comment termios.h
881 @comment POSIX.1
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.
885
886 On BSD systems and the GNU system, it enables the LNEXT and DISCARD characters.
887 @xref{Other Special}.
888 @end deftypevr
889
890 @comment termios.h
891 @comment POSIX.1
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
895 are not cleared.
896 @end deftypevr
897
898 @comment termios.h
899 @comment POSIX.1
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}.
904 @end deftypevr
905
906 The following bits are BSD extensions; they exist only in BSD systems
907 and the GNU system.
908
909 @comment termios.h
910 @comment BSD
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?
915
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.
920
921 @xref{Editing Characters}, for more information about the WERASE character.
922 @end deftypevr
923
924 @comment termios.h
925 @comment BSD
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}.
929 @end deftypevr
930
931 @comment termios.h
932 @comment BSD
933 @deftypevr Macro tcflag_t NOKERNINFO
934 Setting this bit disables handling of the STATUS character.
935 @xref{Other Special}.
936 @end deftypevr
937
938 @comment termios.h
939 @comment BSD
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}.
944 @end deftypevr
945
946 @c EXTPROC is too obscure to document now. --roland
947
948 @node Line Speed
949 @subsection Line Speed
950 @cindex line speed
951 @cindex baud rate
952 @cindex terminal line speed
953 @cindex terminal line speed
954
955 The terminal line speed tells the computer how fast to read and write
956 data on the terminal.
957
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
964 signals.
965
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.
972
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.
976
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
980 store them:
981
982 @comment termios.h
983 @comment POSIX.1
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}}.
987 @end deftypefun
988
989 @comment termios.h
990 @comment POSIX.1
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}}.
994 @end deftypefun
995
996 @comment termios.h
997 @comment POSIX.1
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}
1002 returns @code{-1}.
1003 @end deftypefun
1004
1005 @comment termios.h
1006 @comment POSIX.1
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}
1011 returns @code{-1}.
1012 @end deftypefun
1013
1014 @comment termios.h
1015 @comment BSD
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
1021 4.4 BSD.
1022 @end deftypefun
1023
1024 @comment termios.h
1025 @comment POSIX.1
1026 @deftp {Data Type} speed_t
1027 The @code{speed_t} type is an unsigned integer data type used to
1028 represent line speeds.
1029 @end deftp
1030
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}.
1039
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.
1049
1050 @comment termios.h
1051 @comment POSIX.1
1052 @vindex B0
1053 @comment termios.h
1054 @comment POSIX.1
1055 @vindex B50
1056 @comment termios.h
1057 @comment POSIX.1
1058 @vindex B75
1059 @comment termios.h
1060 @comment POSIX.1
1061 @vindex B110
1062 @comment termios.h
1063 @comment POSIX.1
1064 @vindex B134
1065 @comment termios.h
1066 @comment POSIX.1
1067 @vindex B150
1068 @comment termios.h
1069 @comment POSIX.1
1070 @vindex B200
1071 @comment termios.h
1072 @comment POSIX.1
1073 @vindex B300
1074 @comment termios.h
1075 @comment POSIX.1
1076 @vindex B600
1077 @comment termios.h
1078 @comment POSIX.1
1079 @vindex B1200
1080 @comment termios.h
1081 @comment POSIX.1
1082 @vindex B1800
1083 @comment termios.h
1084 @comment POSIX.1
1085 @vindex B2400
1086 @comment termios.h
1087 @comment POSIX.1
1088 @vindex B4800
1089 @comment termios.h
1090 @comment POSIX.1
1091 @vindex B9600
1092 @comment termios.h
1093 @comment POSIX.1
1094 @vindex B19200
1095 @comment termios.h
1096 @comment POSIX.1
1097 @vindex B38400
1098 @comment termios.h
1099 @comment GNU
1100 @vindex B57600
1101 @comment termios.h
1102 @comment GNU
1103 @vindex B115200
1104 @comment termios.h
1105 @comment GNU
1106 @vindex B230400
1107 @comment termios.h
1108 @comment GNU
1109 @vindex B460800
1110 @smallexample
1111 B0 B50 B75 B110 B134 B150 B200
1112 B300 B600 B1200 B1800 B2400 B4800
1113 B9600 B19200 B38400 B57600 B115200
1114 B230400 B460800
1115 @end smallexample
1116
1117 @vindex EXTA
1118 @vindex EXTB
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.
1122
1123 @node Special Characters
1124 @subsection Special Characters
1125
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.
1133
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{VINTR} is the index of the element that specifies the INTR
1139 character, so storing @code{'='} in @code{@var{termios}.c_cc[VINTR]}
1140 specifies @samp{=} as the INTR character.
1141
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}.
1148
1149 @menu
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
1155 suspended output.
1156 * Other Special:: Other special characters for BSD systems:
1157 they can discard output, and print status.
1158 @end menu
1159
1160 @node Editing Characters
1161 @subsubsection Characters for Input Editing
1162
1163 These special characters are active only in canonical input mode.
1164 @xref{Canonical or Not}.
1165
1166 @comment termios.h
1167 @comment POSIX.1
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
1172 itself.
1173
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.
1179
1180 Usually, the EOF character is @kbd{C-d}.
1181 @end deftypevr
1182
1183 @comment termios.h
1184 @comment POSIX.1
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
1189 itself.
1190
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.
1194
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.
1197
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
1200 affairs.
1201 @end deftypevr
1202
1203 @comment termios.h
1204 @comment BSD
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
1209 itself.
1210
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
1214 other.
1215
1216 The EOL2 character is a BSD extension; it exists only on BSD systems
1217 and the GNU system.
1218 @end deftypevr
1219
1220 @comment termios.h
1221 @comment POSIX.1
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
1226 character itself.
1227
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
1235
1236 Usually, the ERASE character is @key{DEL}.
1237 @end deftypevr
1238
1239 @comment termios.h
1240 @comment BSD
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
1245 itself.
1246
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.
1250
1251 The definition of a ``word'' depends on the setting of the
1252 @code{ALTWERASE} mode; @pxref{Local Modes}.
1253
1254 If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
1255 of any characters except space or tab.
1256
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.
1260
1261 The WERASE character is usually @kbd{C-w}.
1262
1263 This is a BSD extension.
1264 @end deftypevr
1265
1266 @comment termios.h
1267 @comment POSIX.1
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
1272 itself.
1273
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.
1277
1278 The KILL character is usually @kbd{C-u}.
1279 @end deftypevr
1280
1281 @comment termios.h
1282 @comment BSD
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
1287 itself.
1288
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.
1292
1293 The REPRINT character is usually @kbd{C-r}.
1294
1295 This is a BSD extension.
1296 @end deftypevr
1297
1298 @node Signal Characters
1299 @subsubsection Characters that Cause Signals
1300
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
1303 Modes}).
1304
1305 @comment termios.h
1306 @comment POSIX.1
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
1312 itself.
1313
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.
1318
1319 Typically, the INTR character is @kbd{C-c}.
1320 @end deftypevr
1321
1322 @comment termios.h
1323 @comment POSIX.1
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
1328 itself.
1329
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
1333 about signals.
1334
1335 Typically, the QUIT character is @kbd{C-\}.
1336 @end deftypevr
1337
1338 @comment termios.h
1339 @comment POSIX.1
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
1345 itself.
1346
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.
1352
1353 Typically, the SUSP character is @kbd{C-z}.
1354 @end deftypevr
1355
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}.
1362
1363 @comment termios.h
1364 @comment BSD
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
1370 itself.
1371
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).
1377
1378 @xref{Signal Handling}, for more information about signals.
1379
1380 Typically, the DSUSP character is @kbd{C-y}.
1381 @end deftypevr
1382
1383 @node Start/Stop Characters
1384 @subsubsection Special Characters for Flow Control
1385
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}).
1389
1390 @comment termios.h
1391 @comment POSIX.1
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
1396 character itself.
1397
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.
1405
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.
1409 @end deftypevr
1410
1411 @comment termios.h
1412 @comment POSIX.1
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
1417 itself.
1418
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.
1424
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.
1428 @end deftypevr
1429
1430 @node Other Special
1431 @subsubsection Other Special Characters
1432
1433 These special characters exist only in BSD systems and the GNU system.
1434
1435 @comment termios.h
1436 @comment BSD
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
1441 itself.
1442
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.''
1449
1450 The LNEXT character is usually @kbd{C-v}.
1451 @end deftypevr
1452
1453 @comment termios.h
1454 @comment BSD
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
1459 itself.
1460
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.
1466 @end deftypevr
1467
1468 @comment termios.h
1469 @comment BSD
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
1474 itself.
1475
1476 The STATUS character's effect is to print out a status message about how
1477 the current process is running.
1478
1479 The STATUS character is recognized only in canonical mode, and only if
1480 @code{NOKERNINFO} is not set.
1481 @end deftypevr
1482
1483 @node Noncanonical Input
1484 @subsection Noncanonical Input
1485
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
1492 appropriate.
1493
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.
1498
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
1504 TIME slots.
1505
1506 @comment termios.h
1507 @comment POSIX.1
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.
1512
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.
1516 @end deftypevr
1517
1518 @comment termios.h
1519 @comment POSIX.1
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.
1524
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
1527 seconds.
1528 @end deftypevr
1529
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:
1533
1534 @itemize @bullet
1535 @item
1536 Both TIME and MIN are nonzero.
1537
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.
1542
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.
1546
1547 @item
1548 Both MIN and TIME are zero.
1549
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
1553 zero.
1554
1555 @item
1556 MIN is zero but TIME has a nonzero value.
1557
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
1563 value of zero.
1564
1565 @item
1566 TIME is zero but MIN has a nonzero value.
1567
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.
1572 @end itemize
1573
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}.
1579
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
1585 uses.
1586
1587 @comment termios.h
1588 @comment BSD
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.
1594
1595 It does exactly this:
1596 @smallexample
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;
1603 @end smallexample
1604 @end deftypefun
1605
1606 @node Line Control
1607 @section Line Control Functions
1608 @cindex terminal line control functions
1609
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}.
1617
1618 @cindex break condition, generating
1619 @comment termios.h
1620 @comment POSIX.1
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.
1627
1628 This function does nothing if the terminal is not an asynchronous serial
1629 data port.
1630
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:
1634
1635 @table @code
1636 @item EBADF
1637 The @var{filedes} is not a valid file descriptor.
1638
1639 @item ENOTTY
1640 The @var{filedes} is not associated with a terminal device.
1641 @end table
1642 @end deftypefun
1643
1644
1645 @cindex flushing terminal output queue
1646 @cindex terminal output queue, flushing
1647 @comment termios.h
1648 @comment POSIX.1
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.
1652
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
1660
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:
1664
1665 @table @code
1666 @item EBADF
1667 The @var{filedes} is not a valid file descriptor.
1668
1669 @item ENOTTY
1670 The @var{filedes} is not associated with a terminal device.
1671
1672 @item EINTR
1673 The operation was interrupted by delivery of a signal.
1674 @xref{Interrupted Primitives}.
1675 @end table
1676 @end deftypefun
1677
1678
1679 @cindex clearing terminal input queue
1680 @cindex terminal input queue, clearing
1681 @comment termios.h
1682 @comment POSIX.1
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
1687 following values:
1688
1689 @c Extra blank lines here make it look better.
1690 @table @code
1691 @vindex TCIFLUSH
1692 @item TCIFLUSH
1693
1694 Clear any input data received, but not yet read.
1695
1696 @vindex TCOFLUSH
1697 @item TCOFLUSH
1698
1699 Clear any output data written, but not yet transmitted.
1700
1701 @vindex TCIOFLUSH
1702 @item TCIOFLUSH
1703
1704 Clear both queued input and output.
1705 @end table
1706
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:
1710
1711 @table @code
1712 @item EBADF
1713 The @var{filedes} is not a valid file descriptor.
1714
1715 @item ENOTTY
1716 The @var{filedes} is not associated with a terminal device.
1717
1718 @item EINVAL
1719 A bad value was supplied as the @var{queue} argument.
1720 @end table
1721
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.
1727 @end deftypefun
1728
1729 @cindex flow control, terminal
1730 @cindex terminal flow control
1731 @comment termios.h
1732 @comment POSIX.1
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}.
1736
1737 The @var{action} argument specifies what operation to perform, and can
1738 be one of the following values:
1739
1740 @table @code
1741 @vindex TCOOFF
1742 @item TCOOFF
1743 Suspend transmission of output.
1744
1745 @vindex TCOON
1746 @item TCOON
1747 Restart transmission of output.
1748
1749 @vindex TCIOFF
1750 @item TCIOFF
1751 Transmit a STOP character.
1752
1753 @vindex TCION
1754 @item TCION
1755 Transmit a START character.
1756 @end table
1757
1758 For more information about the STOP and START characters, see @ref{Special
1759 Characters}.
1760
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:
1764
1765 @table @code
1766 @vindex EBADF
1767 @item EBADF
1768 The @var{filedes} is not a valid file descriptor.
1769
1770 @vindex ENOTTY
1771 @item ENOTTY
1772 The @var{filedes} is not associated with a terminal device.
1773
1774 @vindex EINVAL
1775 @item EINVAL
1776 A bad value was supplied as the @var{action} argument.
1777 @end table
1778 @end deftypefun
1779
1780 @node Noncanon Example
1781 @section Noncanonical Mode Example
1782
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
1785 echo.
1786
1787 @smallexample
1788 @include termios.c.texi
1789 @end smallexample
1790
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
1794 by @code{exit}.
1795
1796 @ignore
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.
1801 @end ignore
1802
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
1807 example does so.