S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / manual / terminal.texi
blob0c5fdd1a765d7d0c8ca81151f9488b9af1bf28bd
1 @node Low-Level Terminal Interface, Syslog, Sockets, Top
2 @c %MENU% How to change the characteristics of a terminal device
3 @chapter Low-Level Terminal Interface
5 This chapter describes functions that are specific to terminal devices.
6 You can use these functions to do things like turn off input echoing;
7 set serial line characteristics such as line speed and flow control; and
8 change which characters are used for end-of-file, command-line editing,
9 sending signals, and similar control functions.
11 Most of the functions in this chapter operate on file descriptors.
12 @xref{Low-Level I/O}, for more information about what a file
13 descriptor is and how to open a file descriptor for a terminal device.
15 @menu
16 * Is It a Terminal::            How to determine if a file is a terminal
17                                  device, and what its name is.
18 * I/O Queues::                  About flow control and typeahead.
19 * Canonical or Not::            Two basic styles of input processing.
20 * Terminal Modes::              How to examine and modify flags controlling
21                                  details of terminal I/O: echoing,
22                                  signals, editing.  Posix.
23 * BSD Terminal Modes::          BSD compatible terminal mode setting
24 * Line Control::                Sending break sequences, clearing
25                                  terminal buffers @dots{}
26 * Noncanon Example::            How to read single characters without echo.
27 * Pseudo-Terminals::            How to open a pseudo-terminal.
28 @end menu
30 @node Is It a Terminal
31 @section Identifying Terminals
32 @cindex terminal identification
33 @cindex identifying terminals
35 The functions described in this chapter only work on files that
36 correspond to terminal devices.  You can find out whether a file
37 descriptor is associated with a terminal by using the @code{isatty}
38 function.
40 @pindex unistd.h
41 Prototypes for the functions in this section are declared in the header
42 file @file{unistd.h}.
44 @comment unistd.h
45 @comment POSIX.1
46 @deftypefun int isatty (int @var{filedes})
47 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
48 @c isatty ok
49 @c  tcgetattr dup ok
50 This function returns @code{1} if @var{filedes} is a file descriptor
51 associated with an open terminal device, and @math{0} otherwise.
52 @end deftypefun
54 If a file descriptor is associated with a terminal, you can get its
55 associated file name using the @code{ttyname} function.  See also the
56 @code{ctermid} function, described in @ref{Identifying the Terminal}.
58 @comment unistd.h
59 @comment POSIX.1
60 @deftypefun {char *} ttyname (int @var{filedes})
61 @safety{@prelim{}@mtunsafe{@mtasurace{:ttyname}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
62 @c ttyname @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
63 @c  isatty dup ok
64 @c  fstat dup ok
65 @c  memcpy dup ok
66 @c  getttyname @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
67 @c   opendir @ascuheap @acsmem @acsfd
68 @c   readdir ok [protected by exclusive access]
69 @c   strcmp dup ok
70 @c   free dup @asulock @aculock @acsfd @acsmem
71 @c   malloc dup @asulock @aculock @acsfd @acsmem
72 @c   closedir @ascuheap @acsmem @acsfd
73 @c   mempcpy dup ok
74 @c   stat dup ok
75 If the file descriptor @var{filedes} is associated with a terminal
76 device, the @code{ttyname} function returns a pointer to a
77 statically-allocated, null-terminated string containing the file name of
78 the terminal file.  The value is a null pointer if the file descriptor
79 isn't associated with a terminal, or the file name cannot be determined.
80 @end deftypefun
82 @comment unistd.h
83 @comment POSIX.1
84 @deftypefun int ttyname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
85 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
86 @c ttyname_r @ascuheap @acsmem @acsfd
87 @c  isatty dup ok
88 @c  fstat dup ok
89 @c  memcpy dup ok
90 @c  getttyname_r @ascuheap @acsmem @acsfd
91 @c   opendir @ascuheap @acsmem @acsfd
92 @c   readdir ok [protected by exclusive access]
93 @c   strcmp dup ok
94 @c   closedir @ascuheap @acsmem @acsfd
95 @c   stpncpy dup ok
96 @c   stat dup ok
97 The @code{ttyname_r} function is similar to the @code{ttyname} function
98 except that it places its result into the user-specified buffer starting
99 at @var{buf} with length @var{len}.
101 The normal return value from @code{ttyname_r} is @math{0}.  Otherwise an
102 error number is returned to indicate the error.  The following
103 @code{errno} error conditions are defined for this function:
105 @table @code
106 @item EBADF
107 The @var{filedes} argument is not a valid file descriptor.
109 @item ENOTTY
110 The @var{filedes} is not associated with a terminal.
112 @item ERANGE
113 The buffer length @var{len} is too small to store the string to be
114 returned.
115 @end table
116 @end deftypefun
118 @node I/O Queues
119 @section I/O Queues
121 Many of the remaining functions in this section refer to the input and
122 output queues of a terminal device.  These queues implement a form of
123 buffering @emph{within the kernel} independent of the buffering
124 implemented by I/O streams (@pxref{I/O on Streams}).
126 @cindex terminal input queue
127 @cindex typeahead buffer
128 The @dfn{terminal input queue} is also sometimes referred to as its
129 @dfn{typeahead buffer}.  It holds the characters that have been received
130 from the terminal but not yet read by any process.
132 The size of the input queue is described by the @code{MAX_INPUT} and
133 @w{@code{_POSIX_MAX_INPUT}} parameters; see @ref{Limits for Files}.  You
134 are guaranteed a queue size of at least @code{MAX_INPUT}, but the queue
135 might be larger, and might even dynamically change size.  If input flow
136 control is enabled by setting the @code{IXOFF} input mode bit
137 (@pxref{Input Modes}), the terminal driver transmits STOP and START
138 characters to the terminal when necessary to prevent the queue from
139 overflowing.  Otherwise, input may be lost if it comes in too fast from
140 the terminal.  In canonical mode, all input stays in the queue until a
141 newline character is received, so the terminal input queue can fill up
142 when you type a very long line.  @xref{Canonical or Not}.
144 @cindex terminal output queue
145 The @dfn{terminal output queue} is like the input queue, but for output;
146 it contains characters that have been written by processes, but not yet
147 transmitted to the terminal.  If output flow control is enabled by
148 setting the @code{IXON} input mode bit (@pxref{Input Modes}), the
149 terminal driver obeys START and STOP characters sent by the terminal to
150 stop and restart transmission of output.
152 @dfn{Clearing} the terminal input queue means discarding any characters
153 that have been received but not yet read.  Similarly, clearing the
154 terminal output queue means discarding any characters that have been
155 written but not yet transmitted.
157 @node Canonical or Not
158 @section Two Styles of Input: Canonical or Not
160 POSIX systems support two basic modes of input: canonical and
161 noncanonical.
163 @cindex canonical input processing
164 In @dfn{canonical input processing} mode, terminal input is processed in
165 lines terminated by newline (@code{'\n'}), EOF, or EOL characters.  No
166 input can be read until an entire line has been typed by the user, and
167 the @code{read} function (@pxref{I/O Primitives}) returns at most a
168 single line of input, no matter how many bytes are requested.
170 In canonical input mode, the operating system provides input editing
171 facilities: some characters are interpreted specially to perform editing
172 operations within the current line of text, such as ERASE and KILL.
173 @xref{Editing Characters}.
175 The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterize
176 the maximum number of bytes which may appear in a single line of
177 canonical input.  @xref{Limits for Files}.  You are guaranteed a maximum
178 line length of at least @code{MAX_CANON} bytes, but the maximum might be
179 larger, and might even dynamically change size.
181 @cindex noncanonical input processing
182 In @dfn{noncanonical input processing} mode, characters are not grouped
183 into lines, and ERASE and KILL processing is not performed.  The
184 granularity with which bytes are read in noncanonical input mode is
185 controlled by the MIN and TIME settings.  @xref{Noncanonical Input}.
187 Most programs use canonical input mode, because this gives the user a
188 way to edit input line by line.  The usual reason to use noncanonical
189 mode is when the program accepts single-character commands or provides
190 its own editing facilities.
192 The choice of canonical or noncanonical input is controlled by the
193 @code{ICANON} flag in the @code{c_lflag} member of @code{struct termios}.
194 @xref{Local Modes}.
196 @node Terminal Modes
197 @section Terminal Modes
199 @pindex termios.h
200 This section describes the various terminal attributes that control how
201 input and output are done.  The functions, data structures, and symbolic
202 constants are all declared in the header file @file{termios.h}.
204 Don't confuse terminal attributes with file attributes.  A device special
205 file which is associated with a terminal has file attributes as described
206 in @ref{File Attributes}.  These are unrelated to the attributes of the
207 terminal device itself, which are discussed in this section.
209 @menu
210 * Mode Data Types::             The data type @code{struct termios} and
211                                  related types.
212 * Mode Functions::              Functions to read and set the terminal
213                                  attributes.
214 * Setting Modes::               The right way to set terminal attributes
215                                  reliably.
216 * Input Modes::                 Flags controlling low-level input handling.
217 * Output Modes::                Flags controlling low-level output handling.
218 * Control Modes::               Flags controlling serial port behavior.
219 * Local Modes::                 Flags controlling high-level input handling.
220 * Line Speed::                  How to read and set the terminal line speed.
221 * Special Characters::          Characters that have special effects,
222                                  and how to change them.
223 * Noncanonical Input::          Controlling how long to wait for input.
224 @end menu
226 @node Mode Data Types
227 @subsection Terminal Mode Data Types
228 @cindex terminal mode data types
230 The entire collection of attributes of a terminal is stored in a
231 structure of type @code{struct termios}.  This structure is used
232 with the functions @code{tcgetattr} and @code{tcsetattr} to read
233 and set the attributes.
235 @comment termios.h
236 @comment POSIX.1
237 @deftp {Data Type} {struct termios}
238 A @code{struct termios} records all the I/O attributes of a terminal.  The
239 structure includes at least the following members:
241 @table @code
242 @item tcflag_t c_iflag
243 A bit mask specifying flags for input modes; see @ref{Input Modes}.
245 @item tcflag_t c_oflag
246 A bit mask specifying flags for output modes; see @ref{Output Modes}.
248 @item tcflag_t c_cflag
249 A bit mask specifying flags for control modes; see @ref{Control Modes}.
251 @item tcflag_t c_lflag
252 A bit mask specifying flags for local modes; see @ref{Local Modes}.
254 @item cc_t c_cc[NCCS]
255 An array specifying which characters are associated with various
256 control functions; see @ref{Special Characters}.
257 @end table
259 The @code{struct termios} structure also contains members which
260 encode input and output transmission speeds, but the representation is
261 not specified.  @xref{Line Speed}, for how to examine and store the
262 speed values.
263 @end deftp
265 The following sections describe the details of the members of the
266 @code{struct termios} structure.
268 @comment termios.h
269 @comment POSIX.1
270 @deftp {Data Type} tcflag_t
271 This is an unsigned integer type used to represent the various
272 bit masks for terminal flags.
273 @end deftp
275 @comment termios.h
276 @comment POSIX.1
277 @deftp {Data Type} cc_t
278 This is an unsigned integer type used to represent characters associated
279 with various terminal control functions.
280 @end deftp
282 @comment termios.h
283 @comment POSIX.1
284 @deftypevr Macro int NCCS
285 The value of this macro is the number of elements in the @code{c_cc}
286 array.
287 @end deftypevr
289 @node Mode Functions
290 @subsection Terminal Mode Functions
291 @cindex terminal mode functions
293 @comment termios.h
294 @comment POSIX.1
295 @deftypefun int tcgetattr (int @var{filedes}, struct termios *@var{termios-p})
296 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
297 @c Converting the kernel-returned termios data structure to the userland
298 @c format does not ensure atomic or consistent writing.
299 This function is used to examine the attributes of the terminal
300 device with file descriptor @var{filedes}.  The attributes are returned
301 in the structure that @var{termios-p} points to.
303 If successful, @code{tcgetattr} returns @math{0}.  A return value of @math{-1}
304 indicates an error.  The following @code{errno} error conditions are
305 defined for this function:
307 @table @code
308 @item EBADF
309 The @var{filedes} argument is not a valid file descriptor.
311 @item ENOTTY
312 The @var{filedes} is not associated with a terminal.
313 @end table
314 @end deftypefun
316 @comment termios.h
317 @comment POSIX.1
318 @deftypefun int tcsetattr (int @var{filedes}, int @var{when}, const struct termios *@var{termios-p})
319 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
320 @c Converting the incoming termios data structure to the kernel format
321 @c does not ensure atomic or consistent reading.
322 This function sets the attributes of the terminal device with file
323 descriptor @var{filedes}.  The new attributes are taken from the
324 structure that @var{termios-p} points to.
326 The @var{when} argument specifies how to deal with input and output
327 already queued.  It can be one of the following values:
329 @vtable @code
330 @comment termios.h
331 @comment POSIX.1
332 @item TCSANOW
333 Make the change immediately.
335 @comment termios.h
336 @comment POSIX.1
337 @item TCSADRAIN
338 Make the change after waiting until all queued output has been written.
339 You should usually use this option when changing parameters that affect
340 output.
342 @comment termios.h
343 @comment POSIX.1
344 @item TCSAFLUSH
345 This is like @code{TCSADRAIN}, but also discards any queued input.
347 @comment termios.h
348 @comment BSD
349 @item TCSASOFT
350 This is a flag bit that you can add to any of the above alternatives.
351 Its meaning is to inhibit alteration of the state of the terminal
352 hardware.  It is a BSD extension; it is only supported on BSD systems
353 and @gnuhurdsystems{}.
355 Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}
356 bit in the @code{c_cflag} member of the structure @var{termios-p} points
357 to.  @xref{Control Modes}, for a description of @code{CIGNORE}.
358 @end vtable
360 If this function is called from a background process on its controlling
361 terminal, normally all processes in the process group are sent a
362 @code{SIGTTOU} signal, in the same way as if the process were trying to
363 write to the terminal.  The exception is if the calling process itself
364 is ignoring or blocking @code{SIGTTOU} signals, in which case the
365 operation is performed and no signal is sent.  @xref{Job Control}.
367 If successful, @code{tcsetattr} returns @math{0}.  A return value of
368 @math{-1} indicates an error.  The following @code{errno} error
369 conditions are defined for this function:
371 @table @code
372 @item EBADF
373 The @var{filedes} argument is not a valid file descriptor.
375 @item ENOTTY
376 The @var{filedes} is not associated with a terminal.
378 @item EINVAL
379 Either the value of the @code{when} argument is not valid, or there is
380 something wrong with the data in the @var{termios-p} argument.
381 @end table
382 @end deftypefun
384 Although @code{tcgetattr} and @code{tcsetattr} specify the terminal
385 device with a file descriptor, the attributes are those of the terminal
386 device itself and not of the file descriptor.  This means that the
387 effects of changing terminal attributes are persistent; if another
388 process opens the terminal file later on, it will see the changed
389 attributes even though it doesn't have anything to do with the open file
390 descriptor you originally specified in changing the attributes.
392 Similarly, if a single process has multiple or duplicated file
393 descriptors for the same terminal device, changing the terminal
394 attributes affects input and output to all of these file
395 descriptors.  This means, for example, that you can't open one file
396 descriptor or stream to read from a terminal in the normal
397 line-buffered, echoed mode; and simultaneously have another file
398 descriptor for the same terminal that you use to read from it in
399 single-character, non-echoed mode.  Instead, you have to explicitly
400 switch the terminal back and forth between the two modes.
402 @node Setting Modes
403 @subsection Setting Terminal Modes Properly
405 When you set terminal modes, you should call @code{tcgetattr} first to
406 get the current modes of the particular terminal device, modify only
407 those modes that you are really interested in, and store the result with
408 @code{tcsetattr}.
410 It's a bad idea to simply initialize a @code{struct termios} structure
411 to a chosen set of attributes and pass it directly to @code{tcsetattr}.
412 Your program may be run years from now, on systems that support members
413 not documented in this manual.  The way to avoid setting these members
414 to unreasonable values is to avoid changing them.
416 What's more, different terminal devices may require different mode
417 settings in order to function properly.  So you should avoid blindly
418 copying attributes from one terminal device to another.
420 When a member contains a collection of independent flags, as the
421 @code{c_iflag}, @code{c_oflag} and @code{c_cflag} members do, even
422 setting the entire member is a bad idea, because particular operating
423 systems have their own flags.  Instead, you should start with the
424 current value of the member and alter only the flags whose values matter
425 in your program, leaving any other flags unchanged.
427 Here is an example of how to set one flag (@code{ISTRIP}) in the
428 @code{struct termios} structure while properly preserving all the other
429 data in the structure:
431 @smallexample
432 @group
434 set_istrip (int desc, int value)
436   struct termios settings;
437   int result;
438 @end group
440 @group
441   result = tcgetattr (desc, &settings);
442   if (result < 0)
443     @{
444       perror ("error in tcgetattr");
445       return 0;
446     @}
447 @end group
448 @group
449   settings.c_iflag &= ~ISTRIP;
450   if (value)
451     settings.c_iflag |= ISTRIP;
452 @end group
453 @group
454   result = tcsetattr (desc, TCSANOW, &settings);
455   if (result < 0)
456     @{
457       perror ("error in tcsetattr");
458       return 0;
459    @}
460   return 1;
462 @end group
463 @end smallexample
465 @node Input Modes
466 @subsection Input Modes
468 This section describes the terminal attribute flags that control
469 fairly low-level aspects of input processing: handling of parity errors,
470 break signals, flow control, and @key{RET} and @key{LFD} characters.
472 All of these flags are bits in the @code{c_iflag} member of the
473 @code{struct termios} structure.  The member is an integer, and you
474 change flags using the operators @code{&}, @code{|} and @code{^}.  Don't
475 try to specify the entire value for @code{c_iflag}---instead, change
476 only specific flags and leave the rest untouched (@pxref{Setting
477 Modes}).
479 @comment termios.h
480 @comment POSIX.1
481 @deftypevr Macro tcflag_t INPCK
482 @cindex parity checking
483 If this bit is set, input parity checking is enabled.  If it is not set,
484 no checking at all is done for parity errors on input; the
485 characters are simply passed through to the application.
487 Parity checking on input processing is independent of whether parity
488 detection and generation on the underlying terminal hardware is enabled;
489 see @ref{Control Modes}.  For example, you could clear the @code{INPCK}
490 input mode flag and set the @code{PARENB} control mode flag to ignore
491 parity errors on input, but still generate parity on output.
493 If this bit is set, what happens when a parity error is detected depends
494 on whether the @code{IGNPAR} or @code{PARMRK} bits are set.  If neither
495 of these bits are set, a byte with a parity error is passed to the
496 application as a @code{'\0'} character.
497 @end deftypevr
499 @comment termios.h
500 @comment POSIX.1
501 @deftypevr Macro tcflag_t IGNPAR
502 If this bit is set, any byte with a framing or parity error is ignored.
503 This is only useful if @code{INPCK} is also set.
504 @end deftypevr
506 @comment termios.h
507 @comment POSIX.1
508 @deftypevr Macro tcflag_t PARMRK
509 If this bit is set, input bytes with parity or framing errors are marked
510 when passed to the program.  This bit is meaningful only when
511 @code{INPCK} is set and @code{IGNPAR} is not set.
513 The way erroneous bytes are marked is with two preceding bytes,
514 @code{377} and @code{0}.  Thus, the program actually reads three bytes
515 for one erroneous byte received from the terminal.
517 If a valid byte has the value @code{0377}, and @code{ISTRIP} (see below)
518 is not set, the program might confuse it with the prefix that marks a
519 parity error.  So a valid byte @code{0377} is passed to the program as
520 two bytes, @code{0377} @code{0377}, in this case.
521 @end deftypevr
523 @comment termios.h
524 @comment POSIX.1
525 @deftypevr Macro tcflag_t ISTRIP
526 If this bit is set, valid input bytes are stripped to seven bits;
527 otherwise, all eight bits are available for programs to read.
528 @end deftypevr
530 @comment termios.h
531 @comment POSIX.1
532 @deftypevr Macro tcflag_t IGNBRK
533 If this bit is set, break conditions are ignored.
535 @cindex break condition, detecting
536 A @dfn{break condition} is defined in the context of asynchronous
537 serial data transmission as a series of zero-value bits longer than a
538 single byte.
539 @end deftypevr
541 @comment termios.h
542 @comment POSIX.1
543 @deftypevr Macro tcflag_t BRKINT
544 If this bit is set and @code{IGNBRK} is not set, a break condition
545 clears the terminal input and output queues and raises a @code{SIGINT}
546 signal for the foreground process group associated with the terminal.
548 If neither @code{BRKINT} nor @code{IGNBRK} are set, a break condition is
549 passed to the application as a single @code{'\0'} character if
550 @code{PARMRK} is not set, or otherwise as a three-character sequence
551 @code{'\377'}, @code{'\0'}, @code{'\0'}.
552 @end deftypevr
554 @comment termios.h
555 @comment POSIX.1
556 @deftypevr Macro tcflag_t IGNCR
557 If this bit is set, carriage return characters (@code{'\r'}) are
558 discarded on input.  Discarding carriage return may be useful on
559 terminals that send both carriage return and linefeed when you type the
560 @key{RET} key.
561 @end deftypevr
563 @comment termios.h
564 @comment POSIX.1
565 @deftypevr Macro tcflag_t ICRNL
566 If this bit is set and @code{IGNCR} is not set, carriage return characters
567 (@code{'\r'}) received as input are passed to the application as newline
568 characters (@code{'\n'}).
569 @end deftypevr
571 @comment termios.h
572 @comment POSIX.1
573 @deftypevr Macro tcflag_t INLCR
574 If this bit is set, newline characters (@code{'\n'}) received as input
575 are passed to the application as carriage return characters (@code{'\r'}).
576 @end deftypevr
578 @comment termios.h
579 @comment POSIX.1
580 @deftypevr Macro tcflag_t IXOFF
581 If this bit is set, start/stop control on input is enabled.  In other
582 words, the computer sends STOP and START characters as necessary to
583 prevent input from coming in faster than programs are reading it.  The
584 idea is that the actual terminal hardware that is generating the input
585 data responds to a STOP character by suspending transmission, and to a
586 START character by resuming transmission.  @xref{Start/Stop Characters}.
587 @end deftypevr
589 @comment termios.h
590 @comment POSIX.1
591 @deftypevr Macro tcflag_t IXON
592 If this bit is set, start/stop control on output is enabled.  In other
593 words, if the computer receives a STOP character, it suspends output
594 until a START character is received.  In this case, the STOP and START
595 characters are never passed to the application program.  If this bit is
596 not set, then START and STOP can be read as ordinary characters.
597 @xref{Start/Stop Characters}.
598 @c !!! mention this interferes with using C-s and C-q for programs like emacs
599 @end deftypevr
601 @comment termios.h
602 @comment BSD
603 @deftypevr Macro tcflag_t IXANY
604 If this bit is set, any input character restarts output when output has
605 been suspended with the STOP character.  Otherwise, only the START
606 character restarts output.
608 This is a BSD extension; it exists only on BSD systems and
609 @gnulinuxhurdsystems{}.
610 @end deftypevr
612 @comment termios.h
613 @comment BSD
614 @deftypevr Macro tcflag_t IMAXBEL
615 If this bit is set, then filling up the terminal input buffer sends a
616 BEL character (code @code{007}) to the terminal to ring the bell.
618 This is a BSD extension.
619 @end deftypevr
621 @node Output Modes
622 @subsection Output Modes
624 This section describes the terminal flags and fields that control how
625 output characters are translated and padded for display.  All of these
626 are contained in the @code{c_oflag} member of the @w{@code{struct termios}}
627 structure.
629 The @code{c_oflag} member itself is an integer, and you change the flags
630 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
631 try to specify the entire value for @code{c_oflag}---instead, change
632 only specific flags and leave the rest untouched (@pxref{Setting
633 Modes}).
635 @comment termios.h
636 @comment POSIX.1
637 @deftypevr Macro tcflag_t OPOST
638 If this bit is set, output data is processed in some unspecified way so
639 that it is displayed appropriately on the terminal device.  This
640 typically includes mapping newline characters (@code{'\n'}) onto
641 carriage return and linefeed pairs.
643 If this bit isn't set, the characters are transmitted as-is.
644 @end deftypevr
646 The following three bits are effective only if @code{OPOST} is set.
648 @comment termios.h
649 @comment POSIX.1
650 @deftypevr Macro tcflag_t ONLCR
651 If this bit is set, convert the newline character on output into a pair
652 of characters, carriage return followed by linefeed.
653 @end deftypevr
655 @comment termios.h (optional)
656 @comment BSD
657 @deftypevr Macro tcflag_t OXTABS
658 If this bit is set, convert tab characters on output into the appropriate
659 number of spaces to emulate a tab stop every eight columns.  This bit
660 exists only on BSD systems and @gnuhurdsystems{}; on
661 @gnulinuxsystems{} it is available as @code{XTABS}.
662 @end deftypevr
664 @comment termios.h (optional)
665 @comment BSD
666 @deftypevr Macro tcflag_t ONOEOT
667 If this bit is set, discard @kbd{C-d} characters (code @code{004}) on
668 output.  These characters cause many dial-up terminals to disconnect.
669 This bit exists only on BSD systems and @gnuhurdsystems{}.
670 @end deftypevr
672 @node Control Modes
673 @subsection Control Modes
675 This section describes the terminal flags and fields that control
676 parameters usually associated with asynchronous serial data
677 transmission.  These flags may not make sense for other kinds of
678 terminal ports (such as a network connection pseudo-terminal).  All of
679 these are contained in the @code{c_cflag} member of the @code{struct
680 termios} structure.
682 The @code{c_cflag} member itself is an integer, and you change the flags
683 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
684 try to specify the entire value for @code{c_cflag}---instead, change
685 only specific flags and leave the rest untouched (@pxref{Setting
686 Modes}).
688 @comment termios.h
689 @comment POSIX.1
690 @deftypevr Macro tcflag_t CLOCAL
691 If this bit is set, it indicates that the terminal is connected
692 ``locally'' and that the modem status lines (such as carrier detect)
693 should be ignored.
694 @cindex modem status lines
695 @cindex carrier detect
697 On many systems if this bit is not set and you call @code{open} without
698 the @code{O_NONBLOCK} flag set, @code{open} blocks until a modem
699 connection is established.
701 If this bit is not set and a modem disconnect is detected, a
702 @code{SIGHUP} signal is sent to the controlling process group for the
703 terminal (if it has one).  Normally, this causes the process to exit;
704 see @ref{Signal Handling}.  Reading from the terminal after a disconnect
705 causes an end-of-file condition, and writing causes an @code{EIO} error
706 to be returned.  The terminal device must be closed and reopened to
707 clear the condition.
708 @cindex modem disconnect
709 @end deftypevr
711 @comment termios.h
712 @comment POSIX.1
713 @deftypevr Macro tcflag_t HUPCL
714 If this bit is set, a modem disconnect is generated when all processes
715 that have the terminal device open have either closed the file or exited.
716 @end deftypevr
718 @comment termios.h
719 @comment POSIX.1
720 @deftypevr Macro tcflag_t CREAD
721 If this bit is set, input can be read from the terminal.  Otherwise,
722 input is discarded when it arrives.
723 @end deftypevr
725 @comment termios.h
726 @comment POSIX.1
727 @deftypevr Macro tcflag_t CSTOPB
728 If this bit is set, two stop bits are used.  Otherwise, only one stop bit
729 is used.
730 @end deftypevr
732 @comment termios.h
733 @comment POSIX.1
734 @deftypevr Macro tcflag_t PARENB
735 If this bit is set, generation and detection of a parity bit are enabled.
736 @xref{Input Modes}, for information on how input parity errors are handled.
738 If this bit is not set, no parity bit is added to output characters, and
739 input characters are not checked for correct parity.
740 @end deftypevr
742 @comment termios.h
743 @comment POSIX.1
744 @deftypevr Macro tcflag_t PARODD
745 This bit is only useful if @code{PARENB} is set.  If @code{PARODD} is set,
746 odd parity is used, otherwise even parity is used.
747 @end deftypevr
749 The control mode flags also includes a field for the number of bits per
750 character.  You can use the @code{CSIZE} macro as a mask to extract the
751 value, like this: @code{settings.c_cflag & CSIZE}.
753 @comment termios.h
754 @comment POSIX.1
755 @deftypevr Macro tcflag_t CSIZE
756 This is a mask for the number of bits per character.
757 @end deftypevr
759 @comment termios.h
760 @comment POSIX.1
761 @deftypevr Macro tcflag_t CS5
762 This specifies five bits per byte.
763 @end deftypevr
765 @comment termios.h
766 @comment POSIX.1
767 @deftypevr Macro tcflag_t CS6
768 This specifies six bits per byte.
769 @end deftypevr
771 @comment termios.h
772 @comment POSIX.1
773 @deftypevr Macro tcflag_t CS7
774 This specifies seven bits per byte.
775 @end deftypevr
777 @comment termios.h
778 @comment POSIX.1
779 @deftypevr Macro tcflag_t CS8
780 This specifies eight bits per byte.
781 @end deftypevr
783 The following four bits are BSD extensions; these exist only on BSD
784 systems and @gnuhurdsystems{}.
786 @comment termios.h
787 @comment BSD
788 @deftypevr Macro tcflag_t CCTS_OFLOW
789 If this bit is set, enable flow control of output based on the CTS wire
790 (RS232 protocol).
791 @end deftypevr
793 @comment termios.h
794 @comment BSD
795 @deftypevr Macro tcflag_t CRTS_IFLOW
796 If this bit is set, enable flow control of input based on the RTS wire
797 (RS232 protocol).
798 @end deftypevr
800 @comment termios.h
801 @comment BSD
802 @deftypevr Macro tcflag_t MDMBUF
803 If this bit is set, enable carrier-based flow control of output.
804 @end deftypevr
806 @comment termios.h
807 @comment BSD
808 @deftypevr Macro tcflag_t CIGNORE
809 If this bit is set, it says to ignore the control modes and line speed
810 values entirely.  This is only meaningful in a call to @code{tcsetattr}.
812 The @code{c_cflag} member and the line speed values returned by
813 @code{cfgetispeed} and @code{cfgetospeed} will be unaffected by the
814 call.  @code{CIGNORE} is useful if you want to set all the software
815 modes in the other members, but leave the hardware details in
816 @code{c_cflag} unchanged.  (This is how the @code{TCSASOFT} flag to
817 @code{tcsettattr} works.)
819 This bit is never set in the structure filled in by @code{tcgetattr}.
820 @end deftypevr
822 @node Local Modes
823 @subsection Local Modes
825 This section describes the flags for the @code{c_lflag} member of the
826 @code{struct termios} structure.  These flags generally control
827 higher-level aspects of input processing than the input modes flags
828 described in @ref{Input Modes}, such as echoing, signals, and the choice
829 of canonical or noncanonical input.
831 The @code{c_lflag} member itself is an integer, and you change the flags
832 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
833 try to specify the entire value for @code{c_lflag}---instead, change
834 only specific flags and leave the rest untouched (@pxref{Setting
835 Modes}).
837 @comment termios.h
838 @comment POSIX.1
839 @deftypevr Macro tcflag_t ICANON
840 This bit, if set, enables canonical input processing mode.  Otherwise,
841 input is processed in noncanonical mode.  @xref{Canonical or Not}.
842 @end deftypevr
844 @comment termios.h
845 @comment POSIX.1
846 @deftypevr Macro tcflag_t ECHO
847 If this bit is set, echoing of input characters back to the terminal
848 is enabled.
849 @cindex echo of terminal input
850 @end deftypevr
852 @comment termios.h
853 @comment POSIX.1
854 @deftypevr Macro tcflag_t ECHOE
855 If this bit is set, echoing indicates erasure of input with the ERASE
856 character by erasing the last character in the current line from the
857 screen.  Otherwise, the character erased is re-echoed to show what has
858 happened (suitable for a printing terminal).
860 This bit only controls the display behavior; the @code{ICANON} bit by
861 itself controls actual recognition of the ERASE character and erasure of
862 input, without which @code{ECHOE} is simply irrelevant.
863 @end deftypevr
865 @comment termios.h
866 @comment BSD
867 @deftypevr Macro tcflag_t ECHOPRT
868 This bit, like @code{ECHOE}, enables display of the ERASE character in
869 a way that is geared to a hardcopy terminal.  When you type the ERASE
870 character, a @samp{\} character is printed followed by the first
871 character erased.  Typing the ERASE character again just prints the next
872 character erased.  Then, the next time you type a normal character, a
873 @samp{/} character is printed before the character echoes.
875 This is a BSD extension, and exists only in BSD systems and
876 @gnulinuxhurdsystems{}.
877 @end deftypevr
879 @comment termios.h
880 @comment POSIX.1
881 @deftypevr Macro tcflag_t ECHOK
882 This bit enables special display of the KILL character by moving to a
883 new line after echoing the KILL character normally.  The behavior of
884 @code{ECHOKE} (below) is nicer to look at.
886 If this bit is not set, the KILL character echoes just as it would if it
887 were not the KILL character.  Then it is up to the user to remember that
888 the KILL character has erased the preceding input; there is no
889 indication of this on the screen.
891 This bit only controls the display behavior; the @code{ICANON} bit by
892 itself controls actual recognition of the KILL character and erasure of
893 input, without which @code{ECHOK} is simply irrelevant.
894 @end deftypevr
896 @comment termios.h
897 @comment BSD
898 @deftypevr Macro tcflag_t ECHOKE
899 This bit is similar to @code{ECHOK}.  It enables special display of the
900 KILL character by erasing on the screen the entire line that has been
901 killed.  This is a BSD extension, and exists only in BSD systems and
902 @gnulinuxhurdsystems{}.
903 @end deftypevr
905 @comment termios.h
906 @comment POSIX.1
907 @deftypevr Macro tcflag_t ECHONL
908 If this bit is set and the @code{ICANON} bit is also set, then the
909 newline (@code{'\n'}) character is echoed even if the @code{ECHO} bit
910 is not set.
911 @end deftypevr
913 @comment termios.h
914 @comment BSD
915 @deftypevr Macro tcflag_t ECHOCTL
916 If this bit is set and the @code{ECHO} bit is also set, echo control
917 characters with @samp{^} followed by the corresponding text character.
918 Thus, control-A echoes as @samp{^A}.  This is usually the preferred mode
919 for interactive input, because echoing a control character back to the
920 terminal could have some undesired effect on the terminal.
922 This is a BSD extension, and exists only in BSD systems and
923 @gnulinuxhurdsystems{}.
924 @end deftypevr
926 @comment termios.h
927 @comment POSIX.1
928 @deftypevr Macro tcflag_t ISIG
929 This bit controls whether the INTR, QUIT, and SUSP characters are
930 recognized.  The functions associated with these characters are performed
931 if and only if this bit is set.  Being in canonical or noncanonical
932 input mode has no effect on the interpretation of these characters.
934 You should use caution when disabling recognition of these characters.
935 Programs that cannot be interrupted interactively are very
936 user-unfriendly.  If you clear this bit, your program should provide
937 some alternate interface that allows the user to interactively send the
938 signals associated with these characters, or to escape from the program.
939 @cindex interactive signals, from terminal
941 @xref{Signal Characters}.
942 @end deftypevr
944 @comment termios.h
945 @comment POSIX.1
946 @deftypevr Macro tcflag_t IEXTEN
947 POSIX.1 gives @code{IEXTEN} implementation-defined meaning,
948 so you cannot rely on this interpretation on all systems.
950 On BSD systems and @gnulinuxhurdsystems{}, it enables the LNEXT and
951 DISCARD characters.
952 @xref{Other Special}.
953 @end deftypevr
955 @comment termios.h
956 @comment POSIX.1
957 @deftypevr Macro tcflag_t NOFLSH
958 Normally, the INTR, QUIT, and SUSP characters cause input and output
959 queues for the terminal to be cleared.  If this bit is set, the queues
960 are not cleared.
961 @end deftypevr
963 @comment termios.h
964 @comment POSIX.1
965 @deftypevr Macro tcflag_t TOSTOP
966 If this bit is set and the system supports job control, then
967 @code{SIGTTOU} signals are generated by background processes that
968 attempt to write to the terminal.  @xref{Access to the Terminal}.
969 @end deftypevr
971 The following bits are BSD extensions; they exist only on BSD systems
972 and @gnuhurdsystems{}.
974 @comment termios.h
975 @comment BSD
976 @deftypevr Macro tcflag_t ALTWERASE
977 This bit determines how far the WERASE character should erase.  The
978 WERASE character erases back to the beginning of a word; the question
979 is, where do words begin?
981 If this bit is clear, then the beginning of a word is a nonwhitespace
982 character following a whitespace character.  If the bit is set, then the
983 beginning of a word is an alphanumeric character or underscore following
984 a character which is none of those.
986 @xref{Editing Characters}, for more information about the WERASE character.
987 @end deftypevr
989 @comment termios.h
990 @comment BSD
991 @deftypevr Macro tcflag_t FLUSHO
992 This is the bit that toggles when the user types the DISCARD character.
993 While this bit is set, all output is discarded.  @xref{Other Special}.
994 @end deftypevr
996 @comment termios.h (optional)
997 @comment BSD
998 @deftypevr Macro tcflag_t NOKERNINFO
999 Setting this bit disables handling of the STATUS character.
1000 @xref{Other Special}.
1001 @end deftypevr
1003 @comment termios.h
1004 @comment BSD
1005 @deftypevr Macro tcflag_t PENDIN
1006 If this bit is set, it indicates that there is a line of input that
1007 needs to be reprinted.  Typing the REPRINT character sets this bit; the
1008 bit remains set until reprinting is finished.  @xref{Editing Characters}.
1009 @end deftypevr
1011 @c EXTPROC is too obscure to document now.  --roland
1013 @node Line Speed
1014 @subsection Line Speed
1015 @cindex line speed
1016 @cindex baud rate
1017 @cindex terminal line speed
1018 @cindex terminal line speed
1020 The terminal line speed tells the computer how fast to read and write
1021 data on the terminal.
1023 If the terminal is connected to a real serial line, the terminal speed
1024 you specify actually controls the line---if it doesn't match the
1025 terminal's own idea of the speed, communication does not work.  Real
1026 serial ports accept only certain standard speeds.  Also, particular
1027 hardware may not support even all the standard speeds.  Specifying a
1028 speed of zero hangs up a dialup connection and turns off modem control
1029 signals.
1031 If the terminal is not a real serial line (for example, if it is a
1032 network connection), then the line speed won't really affect data
1033 transmission speed, but some programs will use it to determine the
1034 amount of padding needed.  It's best to specify a line speed value that
1035 matches the actual speed of the actual terminal, but you can safely
1036 experiment with different values to vary the amount of padding.
1038 There are actually two line speeds for each terminal, one for input and
1039 one for output.  You can set them independently, but most often
1040 terminals use the same speed for both directions.
1042 The speed values are stored in the @code{struct termios} structure, but
1043 don't try to access them in the @code{struct termios} structure
1044 directly.  Instead, you should use the following functions to read and
1045 store them:
1047 @comment termios.h
1048 @comment POSIX.1
1049 @deftypefun speed_t cfgetospeed (const struct termios *@var{termios-p})
1050 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1051 @c Direct access to a single termios field, except on Linux, where
1052 @c multiple accesses may take place.  No worries either way, callers
1053 @c must ensure mutual exclusion on such non-opaque types.
1054 This function returns the output line speed stored in the structure
1055 @code{*@var{termios-p}}.
1056 @end deftypefun
1058 @comment termios.h
1059 @comment POSIX.1
1060 @deftypefun speed_t cfgetispeed (const struct termios *@var{termios-p})
1061 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1062 This function returns the input line speed stored in the structure
1063 @code{*@var{termios-p}}.
1064 @end deftypefun
1066 @comment termios.h
1067 @comment POSIX.1
1068 @deftypefun int cfsetospeed (struct termios *@var{termios-p}, speed_t @var{speed})
1069 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1070 This function stores @var{speed} in @code{*@var{termios-p}} as the output
1071 speed.  The normal return value is @math{0}; a value of @math{-1}
1072 indicates an error.  If @var{speed} is not a speed, @code{cfsetospeed}
1073 returns @math{-1}.
1074 @end deftypefun
1076 @comment termios.h
1077 @comment POSIX.1
1078 @deftypefun int cfsetispeed (struct termios *@var{termios-p}, speed_t @var{speed})
1079 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1080 This function stores @var{speed} in @code{*@var{termios-p}} as the input
1081 speed.  The normal return value is @math{0}; a value of @math{-1}
1082 indicates an error.  If @var{speed} is not a speed, @code{cfsetospeed}
1083 returns @math{-1}.
1084 @end deftypefun
1086 @comment termios.h
1087 @comment BSD
1088 @deftypefun int cfsetspeed (struct termios *@var{termios-p}, speed_t @var{speed})
1089 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1090 @c There's no guarantee that the two calls are atomic, but since this is
1091 @c not an opaque type, callers ought to ensure mutual exclusion to the
1092 @c termios object.
1094 @c cfsetspeed ok
1095 @c  cfsetispeed ok
1096 @c  cfsetospeed ok
1097 This function stores @var{speed} in @code{*@var{termios-p}} as both the
1098 input and output speeds.  The normal return value is @math{0}; a value
1099 of @math{-1} indicates an error.  If @var{speed} is not a speed,
1100 @code{cfsetspeed} returns @math{-1}.  This function is an extension in
1101 4.4 BSD.
1102 @end deftypefun
1104 @comment termios.h
1105 @comment POSIX.1
1106 @deftp {Data Type} speed_t
1107 The @code{speed_t} type is an unsigned integer data type used to
1108 represent line speeds.
1109 @end deftp
1111 The functions @code{cfsetospeed} and @code{cfsetispeed} report errors
1112 only for speed values that the system simply cannot handle.  If you
1113 specify a speed value that is basically acceptable, then those functions
1114 will succeed.  But they do not check that a particular hardware device
1115 can actually support the specified speeds---in fact, they don't know
1116 which device you plan to set the speed for.  If you use @code{tcsetattr}
1117 to set the speed of a particular device to a value that it cannot
1118 handle, @code{tcsetattr} returns @math{-1}.
1120 @strong{Portability note:} In @theglibc{}, the functions above
1121 accept speeds measured in bits per second as input, and return speed
1122 values measured in bits per second.  Other libraries require speeds to
1123 be indicated by special codes.  For POSIX.1 portability, you must use
1124 one of the following symbols to represent the speed; their precise
1125 numeric values are system-dependent, but each name has a fixed meaning:
1126 @code{B110} stands for 110 bps, @code{B300} for 300 bps, and so on.
1127 There is no portable way to represent any speed but these, but these are
1128 the only speeds that typical serial lines can support.
1130 @comment termios.h
1131 @comment POSIX.1
1132 @vindex B0
1133 @comment termios.h
1134 @comment POSIX.1
1135 @vindex B50
1136 @comment termios.h
1137 @comment POSIX.1
1138 @vindex B75
1139 @comment termios.h
1140 @comment POSIX.1
1141 @vindex B110
1142 @comment termios.h
1143 @comment POSIX.1
1144 @vindex B134
1145 @comment termios.h
1146 @comment POSIX.1
1147 @vindex B150
1148 @comment termios.h
1149 @comment POSIX.1
1150 @vindex B200
1151 @comment termios.h
1152 @comment POSIX.1
1153 @vindex B300
1154 @comment termios.h
1155 @comment POSIX.1
1156 @vindex B600
1157 @comment termios.h
1158 @comment POSIX.1
1159 @vindex B1200
1160 @comment termios.h
1161 @comment POSIX.1
1162 @vindex B1800
1163 @comment termios.h
1164 @comment POSIX.1
1165 @vindex B2400
1166 @comment termios.h
1167 @comment POSIX.1
1168 @vindex B4800
1169 @comment termios.h
1170 @comment POSIX.1
1171 @vindex B9600
1172 @comment termios.h
1173 @comment POSIX.1
1174 @vindex B19200
1175 @comment termios.h
1176 @comment POSIX.1
1177 @vindex B38400
1178 @comment termios.h
1179 @comment GNU
1180 @vindex B57600
1181 @comment termios.h
1182 @comment GNU
1183 @vindex B115200
1184 @comment termios.h
1185 @comment GNU
1186 @vindex B230400
1187 @comment termios.h
1188 @comment GNU
1189 @vindex B460800
1190 @smallexample
1191 B0  B50  B75  B110  B134  B150  B200
1192 B300  B600  B1200  B1800  B2400  B4800
1193 B9600  B19200  B38400  B57600  B115200
1194 B230400  B460800
1195 @end smallexample
1197 @vindex EXTA
1198 @vindex EXTB
1199 BSD defines two additional speed symbols as aliases: @code{EXTA} is an
1200 alias for @code{B19200} and @code{EXTB} is an alias for @code{B38400}.
1201 These aliases are obsolete.
1203 @node Special Characters
1204 @subsection Special Characters
1206 In canonical input, the terminal driver recognizes a number of special
1207 characters which perform various control functions.  These include the
1208 ERASE character (usually @key{DEL}) for editing input, and other editing
1209 characters.  The INTR character (normally @kbd{C-c}) for sending a
1210 @code{SIGINT} signal, and other signal-raising characters, may be
1211 available in either canonical or noncanonical input mode.  All these
1212 characters are described in this section.
1214 The particular characters used are specified in the @code{c_cc} member
1215 of the @code{struct termios} structure.  This member is an array; each
1216 element specifies the character for a particular role.  Each element has
1217 a symbolic constant that stands for the index of that element---for
1218 example, @code{VINTR} is the index of the element that specifies the INTR
1219 character, so storing @code{'='} in @code{@var{termios}.c_cc[VINTR]}
1220 specifies @samp{=} as the INTR character.
1222 @vindex _POSIX_VDISABLE
1223 On some systems, you can disable a particular special character function
1224 by specifying the value @code{_POSIX_VDISABLE} for that role.  This
1225 value is unequal to any possible character code.  @xref{Options for
1226 Files}, for more information about how to tell whether the operating
1227 system you are using supports @code{_POSIX_VDISABLE}.
1229 @menu
1230 * Editing Characters::          Special characters that terminate lines and
1231                                   delete text, and other editing functions.
1232 * Signal Characters::           Special characters that send or raise signals
1233                                   to or for certain classes of processes.
1234 * Start/Stop Characters::       Special characters that suspend or resume
1235                                   suspended output.
1236 * Other Special::               Other special characters for BSD systems:
1237                                   they can discard output, and print status.
1238 @end menu
1240 @node Editing Characters
1241 @subsubsection Characters for Input Editing
1243 These special characters are active only in canonical input mode.
1244 @xref{Canonical or Not}.
1246 @comment termios.h
1247 @comment POSIX.1
1248 @deftypevr Macro int VEOF
1249 @cindex EOF character
1250 This is the subscript for the EOF character in the special control
1251 character array.  @code{@var{termios}.c_cc[VEOF]} holds the character
1252 itself.
1254 The EOF character is recognized only in canonical input mode.  It acts
1255 as a line terminator in the same way as a newline character, but if the
1256 EOF character is typed at the beginning of a line it causes @code{read}
1257 to return a byte count of zero, indicating end-of-file.  The EOF
1258 character itself is discarded.
1260 Usually, the EOF character is @kbd{C-d}.
1261 @end deftypevr
1263 @comment termios.h
1264 @comment POSIX.1
1265 @deftypevr Macro int VEOL
1266 @cindex EOL character
1267 This is the subscript for the EOL character in the special control
1268 character array.  @code{@var{termios}.c_cc[VEOL]} holds the character
1269 itself.
1271 The EOL character is recognized only in canonical input mode.  It acts
1272 as a line terminator, just like a newline character.  The EOL character
1273 is not discarded; it is read as the last character in the input line.
1275 @c !!! example: this is set to ESC by 4.3 csh with "set filec" so it can
1276 @c complete partial lines without using cbreak or raw mode.
1278 You don't need to use the EOL character to make @key{RET} end a line.
1279 Just set the ICRNL flag.  In fact, this is the default state of
1280 affairs.
1281 @end deftypevr
1283 @comment termios.h
1284 @comment BSD
1285 @deftypevr Macro int VEOL2
1286 @cindex EOL2 character
1287 This is the subscript for the EOL2 character in the special control
1288 character array.  @code{@var{termios}.c_cc[VEOL2]} holds the character
1289 itself.
1291 The EOL2 character works just like the EOL character (see above), but it
1292 can be a different character.  Thus, you can specify two characters to
1293 terminate an input line, by setting EOL to one of them and EOL2 to the
1294 other.
1296 The EOL2 character is a BSD extension; it exists only on BSD systems
1297 and @gnulinuxhurdsystems{}.
1298 @end deftypevr
1300 @comment termios.h
1301 @comment POSIX.1
1302 @deftypevr Macro int VERASE
1303 @cindex ERASE character
1304 This is the subscript for the ERASE character in the special control
1305 character array.  @code{@var{termios}.c_cc[VERASE]} holds the
1306 character itself.
1308 The ERASE character is recognized only in canonical input mode.  When
1309 the user types the erase character, the previous character typed is
1310 discarded.  (If the terminal generates multibyte character sequences,
1311 this may cause more than one byte of input to be discarded.)  This
1312 cannot be used to erase past the beginning of the current line of text.
1313 The ERASE character itself is discarded.
1314 @c !!! mention ECHOE here
1316 Usually, the ERASE character is @key{DEL}.
1317 @end deftypevr
1319 @comment termios.h
1320 @comment BSD
1321 @deftypevr Macro int VWERASE
1322 @cindex WERASE character
1323 This is the subscript for the WERASE character in the special control
1324 character array.  @code{@var{termios}.c_cc[VWERASE]} holds the character
1325 itself.
1327 The WERASE character is recognized only in canonical mode.  It erases an
1328 entire word of prior input, and any whitespace after it; whitespace
1329 characters before the word are not erased.
1331 The definition of a ``word'' depends on the setting of the
1332 @code{ALTWERASE} mode; @pxref{Local Modes}.
1334 If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
1335 of any characters except space or tab.
1337 If the @code{ALTWERASE} mode is set, a word is defined as a sequence of
1338 characters containing only letters, numbers, and underscores, optionally
1339 followed by one character that is not a letter, number, or underscore.
1341 The WERASE character is usually @kbd{C-w}.
1343 This is a BSD extension.
1344 @end deftypevr
1346 @comment termios.h
1347 @comment POSIX.1
1348 @deftypevr Macro int VKILL
1349 @cindex KILL character
1350 This is the subscript for the KILL character in the special control
1351 character array.  @code{@var{termios}.c_cc[VKILL]} holds the character
1352 itself.
1354 The KILL character is recognized only in canonical input mode.  When the
1355 user types the kill character, the entire contents of the current line
1356 of input are discarded.  The kill character itself is discarded too.
1358 The KILL character is usually @kbd{C-u}.
1359 @end deftypevr
1361 @comment termios.h
1362 @comment BSD
1363 @deftypevr Macro int VREPRINT
1364 @cindex REPRINT character
1365 This is the subscript for the REPRINT character in the special control
1366 character array.  @code{@var{termios}.c_cc[VREPRINT]} holds the character
1367 itself.
1369 The REPRINT character is recognized only in canonical mode.  It reprints
1370 the current input line.  If some asynchronous output has come while you
1371 are typing, this lets you see the line you are typing clearly again.
1373 The REPRINT character is usually @kbd{C-r}.
1375 This is a BSD extension.
1376 @end deftypevr
1378 @node Signal Characters
1379 @subsubsection Characters that Cause Signals
1381 These special characters may be active in either canonical or noncanonical
1382 input mode, but only when the @code{ISIG} flag is set (@pxref{Local
1383 Modes}).
1385 @comment termios.h
1386 @comment POSIX.1
1387 @deftypevr Macro int VINTR
1388 @cindex INTR character
1389 @cindex interrupt character
1390 This is the subscript for the INTR character in the special control
1391 character array.  @code{@var{termios}.c_cc[VINTR]} holds the character
1392 itself.
1394 The INTR (interrupt) character raises a @code{SIGINT} signal for all
1395 processes in the foreground job associated with the terminal.  The INTR
1396 character itself is then discarded.  @xref{Signal Handling}, for more
1397 information about signals.
1399 Typically, the INTR character is @kbd{C-c}.
1400 @end deftypevr
1402 @comment termios.h
1403 @comment POSIX.1
1404 @deftypevr Macro int VQUIT
1405 @cindex QUIT character
1406 This is the subscript for the QUIT character in the special control
1407 character array.  @code{@var{termios}.c_cc[VQUIT]} holds the character
1408 itself.
1410 The QUIT character raises a @code{SIGQUIT} signal for all processes in
1411 the foreground job associated with the terminal.  The QUIT character
1412 itself is then discarded.  @xref{Signal Handling}, for more information
1413 about signals.
1415 Typically, the QUIT character is @kbd{C-\}.
1416 @end deftypevr
1418 @comment termios.h
1419 @comment POSIX.1
1420 @deftypevr Macro int VSUSP
1421 @cindex SUSP character
1422 @cindex suspend character
1423 This is the subscript for the SUSP character in the special control
1424 character array.  @code{@var{termios}.c_cc[VSUSP]} holds the character
1425 itself.
1427 The SUSP (suspend) character is recognized only if the implementation
1428 supports job control (@pxref{Job Control}).  It causes a @code{SIGTSTP}
1429 signal to be sent to all processes in the foreground job associated with
1430 the terminal.  The SUSP character itself is then discarded.
1431 @xref{Signal Handling}, for more information about signals.
1433 Typically, the SUSP character is @kbd{C-z}.
1434 @end deftypevr
1436 Few applications disable the normal interpretation of the SUSP
1437 character.  If your program does this, it should provide some other
1438 mechanism for the user to stop the job.  When the user invokes this
1439 mechanism, the program should send a @code{SIGTSTP} signal to the
1440 process group of the process, not just to the process itself.
1441 @xref{Signaling Another Process}.
1443 @comment termios.h
1444 @comment BSD
1445 @deftypevr Macro int VDSUSP
1446 @cindex DSUSP character
1447 @cindex delayed suspend character
1448 This is the subscript for the DSUSP character in the special control
1449 character array.  @code{@var{termios}.c_cc[VDSUSP]} holds the character
1450 itself.
1452 The DSUSP (suspend) character is recognized only if the implementation
1453 supports job control (@pxref{Job Control}).  It sends a @code{SIGTSTP}
1454 signal, like the SUSP character, but not right away---only when the
1455 program tries to read it as input.  Not all systems with job control
1456 support DSUSP; only BSD-compatible systems do (including @gnuhurdsystems{}).
1458 @xref{Signal Handling}, for more information about signals.
1460 Typically, the DSUSP character is @kbd{C-y}.
1461 @end deftypevr
1463 @node Start/Stop Characters
1464 @subsubsection Special Characters for Flow Control
1466 These special characters may be active in either canonical or noncanonical
1467 input mode, but their use is controlled by the flags @code{IXON} and
1468 @code{IXOFF} (@pxref{Input Modes}).
1470 @comment termios.h
1471 @comment POSIX.1
1472 @deftypevr Macro int VSTART
1473 @cindex START character
1474 This is the subscript for the START character in the special control
1475 character array.  @code{@var{termios}.c_cc[VSTART]} holds the
1476 character itself.
1478 The START character is used to support the @code{IXON} and @code{IXOFF}
1479 input modes.  If @code{IXON} is set, receiving a START character resumes
1480 suspended output; the START character itself is discarded.  If
1481 @code{IXANY} is set, receiving any character at all resumes suspended
1482 output; the resuming character is not discarded unless it is the START
1483 character.  If @code{IXOFF} is set, the system may also transmit START
1484 characters to the terminal.
1486 The usual value for the START character is @kbd{C-q}.  You may not be
1487 able to change this value---the hardware may insist on using @kbd{C-q}
1488 regardless of what you specify.
1489 @end deftypevr
1491 @comment termios.h
1492 @comment POSIX.1
1493 @deftypevr Macro int VSTOP
1494 @cindex STOP character
1495 This is the subscript for the STOP character in the special control
1496 character array.  @code{@var{termios}.c_cc[VSTOP]} holds the character
1497 itself.
1499 The STOP character is used to support the @code{IXON} and @code{IXOFF}
1500 input modes.  If @code{IXON} is set, receiving a STOP character causes
1501 output to be suspended; the STOP character itself is discarded.  If
1502 @code{IXOFF} is set, the system may also transmit STOP characters to the
1503 terminal, to prevent the input queue from overflowing.
1505 The usual value for the STOP character is @kbd{C-s}.  You may not be
1506 able to change this value---the hardware may insist on using @kbd{C-s}
1507 regardless of what you specify.
1508 @end deftypevr
1510 @node Other Special
1511 @subsubsection Other Special Characters
1513 @comment termios.h
1514 @comment BSD
1515 @deftypevr Macro int VLNEXT
1516 @cindex LNEXT character
1517 This is the subscript for the LNEXT character in the special control
1518 character array.  @code{@var{termios}.c_cc[VLNEXT]} holds the character
1519 itself.
1521 The LNEXT character is recognized only when @code{IEXTEN} is set, but in
1522 both canonical and noncanonical mode.  It disables any special
1523 significance of the next character the user types.  Even if the
1524 character would normally perform some editing function or generate a
1525 signal, it is read as a plain character.  This is the analogue of the
1526 @kbd{C-q} command in Emacs.  ``LNEXT'' stands for ``literal next.''
1528 The LNEXT character is usually @kbd{C-v}.
1530 This character is available on BSD systems and @gnulinuxhurdsystems{}.
1531 @end deftypevr
1533 @comment termios.h
1534 @comment BSD
1535 @deftypevr Macro int VDISCARD
1536 @cindex DISCARD character
1537 This is the subscript for the DISCARD character in the special control
1538 character array.  @code{@var{termios}.c_cc[VDISCARD]} holds the character
1539 itself.
1541 The DISCARD character is recognized only when @code{IEXTEN} is set, but
1542 in both canonical and noncanonical mode.  Its effect is to toggle the
1543 discard-output flag.  When this flag is set, all program output is
1544 discarded.  Setting the flag also discards all output currently in the
1545 output buffer.  Typing any other character resets the flag.
1547 This character is available on BSD systems and @gnulinuxhurdsystems{}.
1548 @end deftypevr
1550 @comment termios.h
1551 @comment BSD
1552 @deftypevr Macro int VSTATUS
1553 @cindex STATUS character
1554 This is the subscript for the STATUS character in the special control
1555 character array.  @code{@var{termios}.c_cc[VSTATUS]} holds the character
1556 itself.
1558 The STATUS character's effect is to print out a status message about how
1559 the current process is running.
1561 The STATUS character is recognized only in canonical mode, and only if
1562 @code{NOKERNINFO} is not set.
1564 This character is available only on BSD systems and @gnuhurdsystems{}.
1565 @end deftypevr
1567 @node Noncanonical Input
1568 @subsection Noncanonical Input
1570 In noncanonical input mode, the special editing characters such as
1571 ERASE and KILL are ignored.  The system facilities for the user to edit
1572 input are disabled in noncanonical mode, so that all input characters
1573 (unless they are special for signal or flow-control purposes) are passed
1574 to the application program exactly as typed.  It is up to the
1575 application program to give the user ways to edit the input, if
1576 appropriate.
1578 Noncanonical mode offers special parameters called MIN and TIME for
1579 controlling whether and how long to wait for input to be available.  You
1580 can even use them to avoid ever waiting---to return immediately with
1581 whatever input is available, or with no input.
1583 The MIN and TIME are stored in elements of the @code{c_cc} array, which
1584 is a member of the @w{@code{struct termios}} structure.  Each element of
1585 this array has a particular role, and each element has a symbolic
1586 constant that stands for the index of that element.  @code{VMIN} and
1587 @code{VTIME} are the names for the indices in the array of the MIN and
1588 TIME slots.
1590 @comment termios.h
1591 @comment POSIX.1
1592 @deftypevr Macro int VMIN
1593 @cindex MIN termios slot
1594 This is the subscript for the MIN slot in the @code{c_cc} array.  Thus,
1595 @code{@var{termios}.c_cc[VMIN]} is the value itself.
1597 The MIN slot is only meaningful in noncanonical input mode; it
1598 specifies the minimum number of bytes that must be available in the
1599 input queue in order for @code{read} to return.
1600 @end deftypevr
1602 @comment termios.h
1603 @comment POSIX.1
1604 @deftypevr Macro int VTIME
1605 @cindex TIME termios slot
1606 This is the subscript for the TIME slot in the @code{c_cc} array.  Thus,
1607 @code{@var{termios}.c_cc[VTIME]} is the value itself.
1609 The TIME slot is only meaningful in noncanonical input mode; it
1610 specifies how long to wait for input before returning, in units of 0.1
1611 seconds.
1612 @end deftypevr
1614 The MIN and TIME values interact to determine the criterion for when
1615 @code{read} should return; their precise meanings depend on which of
1616 them are nonzero.  There are four possible cases:
1618 @itemize @bullet
1619 @item
1620 Both TIME and MIN are nonzero.
1622 In this case, TIME specifies how long to wait after each input character
1623 to see if more input arrives.  After the first character received,
1624 @code{read} keeps waiting until either MIN bytes have arrived in all, or
1625 TIME elapses with no further input.
1627 @code{read} always blocks until the first character arrives, even if
1628 TIME elapses first.  @code{read} can return more than MIN characters if
1629 more than MIN happen to be in the queue.
1631 @item
1632 Both MIN and TIME are zero.
1634 In this case, @code{read} always returns immediately with as many
1635 characters as are available in the queue, up to the number requested.
1636 If no input is immediately available, @code{read} returns a value of
1637 zero.
1639 @item
1640 MIN is zero but TIME has a nonzero value.
1642 In this case, @code{read} waits for time TIME for input to become
1643 available; the availability of a single byte is enough to satisfy the
1644 read request and cause @code{read} to return.  When it returns, it
1645 returns as many characters as are available, up to the number requested.
1646 If no input is available before the timer expires, @code{read} returns a
1647 value of zero.
1649 @item
1650 TIME is zero but MIN has a nonzero value.
1652 In this case, @code{read} waits until at least MIN bytes are available
1653 in the queue.  At that time, @code{read} returns as many characters as
1654 are available, up to the number requested.  @code{read} can return more
1655 than MIN characters if more than MIN happen to be in the queue.
1656 @end itemize
1658 What happens if MIN is 50 and you ask to read just 10 bytes?
1659 Normally, @code{read} waits until there are 50 bytes in the buffer (or,
1660 more generally, the wait condition described above is satisfied), and
1661 then reads 10 of them, leaving the other 40 buffered in the operating
1662 system for a subsequent call to @code{read}.
1664 @strong{Portability note:} On some systems, the MIN and TIME slots are
1665 actually the same as the EOF and EOL slots.  This causes no serious
1666 problem because the MIN and TIME slots are used only in noncanonical
1667 input and the EOF and EOL slots are used only in canonical input, but it
1668 isn't very clean.  @Theglibc{} allocates separate slots for these
1669 uses.
1671 @comment termios.h
1672 @comment BSD
1673 @deftypefun void cfmakeraw (struct termios *@var{termios-p})
1674 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1675 @c There's no guarantee the changes are atomic, but since this is not an
1676 @c opaque type, callers ought to ensure mutual exclusion to the termios
1677 @c object.
1678 This function provides an easy way to set up @code{*@var{termios-p}} for
1679 what has traditionally been called ``raw mode'' in BSD.  This uses
1680 noncanonical input, and turns off most processing to give an unmodified
1681 channel to the terminal.
1683 It does exactly this:
1684 @smallexample
1685   @var{termios-p}->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1686                                 |INLCR|IGNCR|ICRNL|IXON);
1687   @var{termios-p}->c_oflag &= ~OPOST;
1688   @var{termios-p}->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1689   @var{termios-p}->c_cflag &= ~(CSIZE|PARENB);
1690   @var{termios-p}->c_cflag |= CS8;
1691 @end smallexample
1692 @end deftypefun
1695 @node BSD Terminal Modes
1696 @section BSD Terminal Modes
1697 @cindex terminal modes, BSD
1699 The usual way to get and set terminal modes is with the functions described
1700 in @ref{Terminal Modes}.  However, on some systems you can use the
1701 BSD-derived functions in this section to do some of the same things.  On
1702 many systems, these functions do not exist.  Even with @theglibc{},
1703 the functions simply fail with @code{errno} = @code{ENOSYS} with many
1704 kernels, including Linux.
1706 The symbols used in this section are declared in @file{sgtty.h}.
1708 @comment termios.h
1709 @comment BSD
1710 @deftp {Data Type} {struct sgttyb}
1711 This structure is an input or output parameter list for @code{gtty} and
1712 @code{stty}.
1714 @table @code
1715 @item char sg_ispeed
1716 Line speed for input
1717 @item char sg_ospeed
1718 Line speed for output
1719 @item char sg_erase
1720 Erase character
1721 @item char sg_kill
1722 Kill character
1723 @item int sg_flags
1724 Various flags
1725 @end table
1726 @end deftp
1728 @comment sgtty.h
1729 @comment BSD
1730 @deftypefun int gtty (int @var{filedes}, struct sgttyb *@var{attributes})
1731 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1732 @c Direct ioctl, BSD only.
1733 This function gets the attributes of a terminal.
1735 @code{gtty} sets *@var{attributes} to describe the terminal attributes
1736 of the terminal which is open with file descriptor @var{filedes}.
1737 @end deftypefun
1739 @comment sgtty.h
1740 @comment BSD
1741 @deftypefun int stty (int @var{filedes}, const struct sgttyb *@var{attributes})
1742 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1743 @c Direct ioctl, BSD only.
1745 This function sets the attributes of a terminal.
1747 @code{stty} sets the terminal attributes of the terminal which is open with
1748 file descriptor @var{filedes} to those described by *@var{attributes}.
1749 @end deftypefun
1751 @node Line Control
1752 @section Line Control Functions
1753 @cindex terminal line control functions
1755 These functions perform miscellaneous control actions on terminal
1756 devices.  As regards terminal access, they are treated like doing
1757 output: if any of these functions is used by a background process on its
1758 controlling terminal, normally all processes in the process group are
1759 sent a @code{SIGTTOU} signal.  The exception is if the calling process
1760 itself is ignoring or blocking @code{SIGTTOU} signals, in which case the
1761 operation is performed and no signal is sent.  @xref{Job Control}.
1763 @cindex break condition, generating
1764 @comment termios.h
1765 @comment POSIX.1
1766 @deftypefun int tcsendbreak (int @var{filedes}, int @var{duration})
1767 @safety{@prelim{}@mtunsafe{@mtasurace{:tcattr(filedes)/bsd}}@asunsafe{}@acunsafe{@acucorrupt{/bsd}}}
1768 @c On Linux, this calls just one out of two ioctls; on BSD, it's two
1769 @c ioctls with a select (for the delay only) in between, the first
1770 @c setting and the latter clearing the break status.  The BSD
1771 @c implementation may leave the break enabled if cancelled, and threads
1772 @c and signals may cause the break to be interrupted before requested.
1773 This function generates a break condition by transmitting a stream of
1774 zero bits on the terminal associated with the file descriptor
1775 @var{filedes}.  The duration of the break is controlled by the
1776 @var{duration} argument.  If zero, the duration is between 0.25 and 0.5
1777 seconds.  The meaning of a nonzero value depends on the operating system.
1779 This function does nothing if the terminal is not an asynchronous serial
1780 data port.
1782 The return value is normally zero.  In the event of an error, a value
1783 of @math{-1} is returned.  The following @code{errno} error conditions
1784 are defined for this function:
1786 @table @code
1787 @item EBADF
1788 The @var{filedes} is not a valid file descriptor.
1790 @item ENOTTY
1791 The @var{filedes} is not associated with a terminal device.
1792 @end table
1793 @end deftypefun
1796 @cindex flushing terminal output queue
1797 @cindex terminal output queue, flushing
1798 @comment termios.h
1799 @comment POSIX.1
1800 @deftypefun int tcdrain (int @var{filedes})
1801 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1802 @c Direct ioctl.
1803 The @code{tcdrain} function waits until all queued
1804 output to the terminal @var{filedes} has been transmitted.
1806 This function is a cancellation point in multi-threaded programs.  This
1807 is a problem if the thread allocates some resources (like memory, file
1808 descriptors, semaphores or whatever) at the time @code{tcdrain} is
1809 called.  If the thread gets canceled these resources stay allocated
1810 until the program ends.  To avoid this calls to @code{tcdrain} should be
1811 protected using cancellation handlers.
1812 @c ref pthread_cleanup_push / pthread_cleanup_pop
1814 The return value is normally zero.  In the event of an error, a value
1815 of @math{-1} is returned.  The following @code{errno} error conditions
1816 are defined for this function:
1818 @table @code
1819 @item EBADF
1820 The @var{filedes} is not a valid file descriptor.
1822 @item ENOTTY
1823 The @var{filedes} is not associated with a terminal device.
1825 @item EINTR
1826 The operation was interrupted by delivery of a signal.
1827 @xref{Interrupted Primitives}.
1828 @end table
1829 @end deftypefun
1832 @cindex clearing terminal input queue
1833 @cindex terminal input queue, clearing
1834 @comment termios.h
1835 @comment POSIX.1
1836 @deftypefun int tcflush (int @var{filedes}, int @var{queue})
1837 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1838 @c Direct ioctl.
1839 The @code{tcflush} function is used to clear the input and/or output
1840 queues associated with the terminal file @var{filedes}.  The @var{queue}
1841 argument specifies which queue(s) to clear, and can be one of the
1842 following values:
1844 @c Extra blank lines here make it look better.
1845 @vtable @code
1846 @item TCIFLUSH
1848 Clear any input data received, but not yet read.
1850 @item TCOFLUSH
1852 Clear any output data written, but not yet transmitted.
1854 @item TCIOFLUSH
1856 Clear both queued input and output.
1857 @end vtable
1859 The return value is normally zero.  In the event of an error, a value
1860 of @math{-1} is returned.  The following @code{errno} error conditions
1861 are defined for this function:
1863 @table @code
1864 @item EBADF
1865 The @var{filedes} is not a valid file descriptor.
1867 @item ENOTTY
1868 The @var{filedes} is not associated with a terminal device.
1870 @item EINVAL
1871 A bad value was supplied as the @var{queue} argument.
1872 @end table
1874 It is unfortunate that this function is named @code{tcflush}, because
1875 the term ``flush'' is normally used for quite another operation---waiting
1876 until all output is transmitted---and using it for discarding input or
1877 output would be confusing.  Unfortunately, the name @code{tcflush} comes
1878 from POSIX and we cannot change it.
1879 @end deftypefun
1881 @cindex flow control, terminal
1882 @cindex terminal flow control
1883 @comment termios.h
1884 @comment POSIX.1
1885 @deftypefun int tcflow (int @var{filedes}, int @var{action})
1886 @safety{@prelim{}@mtunsafe{@mtasurace{:tcattr(filedes)/bsd}}@asunsafe{}@acsafe{}}
1887 @c Direct ioctl on Linux.  On BSD, the TCO* actions are a single ioctl,
1888 @c whereas the TCI actions first call tcgetattr and then write to the fd
1889 @c the c_cc character corresponding to the action; there's a window for
1890 @c another thread to change the xon/xoff characters.
1891 The @code{tcflow} function is used to perform operations relating to
1892 XON/XOFF flow control on the terminal file specified by @var{filedes}.
1894 The @var{action} argument specifies what operation to perform, and can
1895 be one of the following values:
1897 @vtable @code
1898 @item TCOOFF
1899 Suspend transmission of output.
1901 @item TCOON
1902 Restart transmission of output.
1904 @item TCIOFF
1905 Transmit a STOP character.
1907 @item TCION
1908 Transmit a START character.
1909 @end vtable
1911 For more information about the STOP and START characters, see @ref{Special
1912 Characters}.
1914 The return value is normally zero.  In the event of an error, a value
1915 of @math{-1} is returned.  The following @code{errno} error conditions
1916 are defined for this function:
1918 @table @code
1919 @vindex EBADF
1920 @item EBADF
1921 The @var{filedes} is not a valid file descriptor.
1923 @vindex ENOTTY
1924 @item ENOTTY
1925 The @var{filedes} is not associated with a terminal device.
1927 @vindex EINVAL
1928 @item EINVAL
1929 A bad value was supplied as the @var{action} argument.
1930 @end table
1931 @end deftypefun
1933 @node Noncanon Example
1934 @section Noncanonical Mode Example
1936 Here is an example program that shows how you can set up a terminal
1937 device to read single characters in noncanonical input mode, without
1938 echo.
1940 @smallexample
1941 @include termios.c.texi
1942 @end smallexample
1944 This program is careful to restore the original terminal modes before
1945 exiting or terminating with a signal.  It uses the @code{atexit}
1946 function (@pxref{Cleanups on Exit}) to make sure this is done
1947 by @code{exit}.
1949 @ignore
1950 @c !!!! the example doesn't handle any signals!
1951 The signals handled in the example are the ones that typically occur due
1952 to actions of the user.  It might be desirable to handle other signals
1953 such as SIGSEGV that can result from bugs in the program.
1954 @end ignore
1956 The shell is supposed to take care of resetting the terminal modes when
1957 a process is stopped or continued; see @ref{Job Control}.  But some
1958 existing shells do not actually do this, so you may wish to establish
1959 handlers for job control signals that reset terminal modes.  The above
1960 example does so.
1963 @node Pseudo-Terminals
1964 @section Pseudo-Terminals
1965 @cindex pseudo-terminals
1967 A @dfn{pseudo-terminal} is a special interprocess communication channel
1968 that acts like a terminal.  One end of the channel is called the
1969 @dfn{master} side or @dfn{master pseudo-terminal device}, the other side
1970 is called the @dfn{slave} side.  Data written to the master side is
1971 received by the slave side as if it was the result of a user typing at
1972 an ordinary terminal, and data written to the slave side is sent to the
1973 master side as if it was written on an ordinary terminal.
1975 Pseudo terminals are the way programs like @code{xterm} and @code{emacs}
1976 implement their terminal emulation functionality.
1978 @menu
1979 * Allocation::             Allocating a pseudo terminal.
1980 * Pseudo-Terminal Pairs::  How to open both sides of a
1981                             pseudo-terminal in a single operation.
1982 @end menu
1984 @node Allocation
1985 @subsection Allocating Pseudo-Terminals
1986 @cindex allocating pseudo-terminals
1988 @pindex stdlib.h
1989 This subsection describes functions for allocating a pseudo-terminal,
1990 and for making this pseudo-terminal available for actual use.  These
1991 functions are declared in the header file @file{stdlib.h}.
1993 @comment stdlib.h
1994 @comment GNU
1995 @deftypefun int getpt (void)
1996 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
1997 @c On BSD, tries to open multiple potential pty names, returning on the
1998 @c first success.  On Linux, try posix_openpt first, then fallback to
1999 @c the BSD implementation.  The posix implementation opens the ptmx
2000 @c device, checks with statfs that /dev/pts is a devpts or that /dev is
2001 @c a devfs, and returns the fd; static variables devpts_mounted and
2002 @c have_no_dev_ptmx are safely initialized so as to avoid repeated
2003 @c tests.
2004 The @code{getpt} function returns a new file descriptor for the next
2005 available master pseudo-terminal.  The normal return value from
2006 @code{getpt} is a non-negative integer file descriptor.  In the case of
2007 an error, a value of @math{-1} is returned instead.  The following
2008 @code{errno} conditions are defined for this function:
2010 @table @code
2011 @item ENOENT
2012 There are no free master pseudo-terminals available.
2013 @end table
2015 This function is a GNU extension.
2016 @end deftypefun
2018 @comment stdlib.h
2019 @comment SVID, XPG4.2
2020 @deftypefun int grantpt (int @var{filedes})
2021 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2022 @c grantpt @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2023 @c  unix/grantpt:pts_name @acsuheap @acsmem
2024 @c   ptsname_internal dup ok (but this is Linux-only!)
2025 @c   memchr dup ok
2026 @c   realloc dup @acsuheap @acsmem
2027 @c   malloc dup @acsuheap @acsmem
2028 @c   free dup @acsuheap @acsmem
2029 @c  fcntl dup ok
2030 @c  getuid dup ok
2031 @c  chown dup ok
2032 @c  sysconf(_SC_GETGR_R_SIZE_MAX) ok
2033 @c  getgrnam_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2034 @c  getgid dup ok
2035 @c  chmod dup ok
2036 @c  fork dup @aculock
2037 @c  [child]
2038 @c   setrlimit
2039 @c   dup2
2040 @c   CLOSE_ALL_FDS
2041 @c   execle
2042 @c   _exit
2043 @c  waitpid dup ok
2044 @c  WIFEXITED dup ok
2045 @c  WEXITSTATUS dup ok
2046 @c  free dup @ascuheap @acsmem
2047 The @code{grantpt} function changes the ownership and access permission
2048 of the slave pseudo-terminal device corresponding to the master
2049 pseudo-terminal device associated with the file descriptor
2050 @var{filedes}.  The owner is set from the real user ID of the calling
2051 process (@pxref{Process Persona}), and the group is set to a special
2052 group (typically @dfn{tty}) or from the real group ID of the calling
2053 process.  The access permission is set such that the file is both
2054 readable and writable by the owner and only writable by the group.
2056 On some systems this function is implemented by invoking a special
2057 @code{setuid} root program (@pxref{How Change Persona}).  As a
2058 consequence, installing a signal handler for the @code{SIGCHLD} signal
2059 (@pxref{Job Control Signals}) may interfere with a call to
2060 @code{grantpt}.
2062 The normal return value from @code{grantpt} is @math{0}; a value of
2063 @math{-1} is returned in case of failure.  The following @code{errno}
2064 error conditions are defined for this function:
2066 @table @code
2067 @item EBADF
2068 The @var{filedes} argument is not a valid file descriptor.
2070 @item EINVAL
2071 The @var{filedes} argument is not associated with a master pseudo-terminal
2072 device.
2074 @item EACCES
2075 The slave pseudo-terminal device corresponding to the master associated
2076 with @var{filedes} could not be accessed.
2077 @end table
2079 @end deftypefun
2081 @comment stdlib.h
2082 @comment SVID, XPG4.2
2083 @deftypefun int unlockpt (int @var{filedes})
2084 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
2085 @c unlockpt @ascuheap/bsd @acsmem @acsfd
2086 @c /bsd
2087 @c  ptsname_r dup @ascuheap @acsmem @acsfd
2088 @c  revoke ok (syscall)
2089 @c /linux
2090 @c  ioctl dup ok
2091 The @code{unlockpt} function unlocks the slave pseudo-terminal device
2092 corresponding to the master pseudo-terminal device associated with the
2093 file descriptor @var{filedes}.  On many systems, the slave can only be
2094 opened after unlocking, so portable applications should always call
2095 @code{unlockpt} before trying to open the slave.
2097 The normal return value from @code{unlockpt} is @math{0}; a value of
2098 @math{-1} is returned in case of failure.  The following @code{errno}
2099 error conditions are defined for this function:
2101 @table @code
2102 @item EBADF
2103 The @var{filedes} argument is not a valid file descriptor.
2105 @item EINVAL
2106 The @var{filedes} argument is not associated with a master pseudo-terminal
2107 device.
2108 @end table
2109 @end deftypefun
2111 @comment stdlib.h
2112 @comment SVID, XPG4.2
2113 @deftypefun {char *} ptsname (int @var{filedes})
2114 @safety{@prelim{}@mtunsafe{@mtasurace{:ptsname}}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
2115 @c ptsname @mtasurace:ptsname @ascuheap/bsd @acsmem @acsfd
2116 @c  ptsname_r dup @ascuheap/bsd @acsmem @acsfd
2117 If the file descriptor @var{filedes} is associated with a
2118 master pseudo-terminal device, the @code{ptsname} function returns a
2119 pointer to a statically-allocated, null-terminated string containing the
2120 file name of the associated slave pseudo-terminal file.  This string
2121 might be overwritten by subsequent calls to @code{ptsname}.
2122 @end deftypefun
2124 @comment stdlib.h
2125 @comment GNU
2126 @deftypefun int ptsname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
2127 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
2128 @c ptsname_r @ascuheap/bsd @acsmem @acsfd
2129 @c /hurd
2130 @c  term_get_peername ok
2131 @c  strlen dup ok
2132 @c  memcpy dup ok
2133 @c /bsd
2134 @c  isatty dup ok
2135 @c  strlen dup ok
2136 @c  ttyname_r dup @ascuheap @acsmem @acsfd
2137 @c  stat dup ok
2138 @c /linux
2139 @c  ptsname_internal ok
2140 @c   isatty dup ok
2141 @c   ioctl dup ok
2142 @c   strlen dup ok
2143 @c   itoa_word dup ok
2144 @c   stpcpy dup ok
2145 @c   memcpy dup ok
2146 @c   fxstat64 dup ok
2147 @c   MASTER_P ok
2148 @c    major ok
2149 @c     gnu_dev_major ok
2150 @c    minor ok
2151 @c     gnu_dev_minor ok
2152 @c   minor dup ok
2153 @c   xstat64 dup ok
2154 @c   S_ISCHR dup ok
2155 @c   SLAVE_P ok
2156 @c    major dup ok
2157 @c    minor dup ok
2158 The @code{ptsname_r} function is similar to the @code{ptsname} function
2159 except that it places its result into the user-specified buffer starting
2160 at @var{buf} with length @var{len}.
2162 This function is a GNU extension.
2163 @end deftypefun
2165 @strong{Portability Note:} On @w{System V} derived systems, the file
2166 returned by the @code{ptsname} and @code{ptsname_r} functions may be
2167 STREAMS-based, and therefore require additional processing after opening
2168 before it actually behaves as a pseudo terminal.
2169 @c FIXME: xref STREAMS
2171 Typical usage of these functions is illustrated by the following example:
2172 @smallexample
2174 open_pty_pair (int *amaster, int *aslave)
2176   int master, slave;
2177   char *name;
2179   master = getpt ();
2180   if (master < 0)
2181     return 0;
2183   if (grantpt (master) < 0 || unlockpt (master) < 0)
2184     goto close_master;
2185   name = ptsname (master);
2186   if (name == NULL)
2187     goto close_master;
2189   slave = open (name, O_RDWR);
2190   if (slave == -1)
2191     goto close_master;
2193   if (isastream (slave))
2194     @{
2195       if (ioctl (slave, I_PUSH, "ptem") < 0
2196           || ioctl (slave, I_PUSH, "ldterm") < 0)
2197         goto close_slave;
2198     @}
2200   *amaster = master;
2201   *aslave = slave;
2202   return 1;
2204 close_slave:
2205   close (slave);
2207 close_master:
2208   close (master);
2209   return 0;
2211 @end smallexample
2213 @node Pseudo-Terminal Pairs
2214 @subsection Opening a Pseudo-Terminal Pair
2215 @cindex opening a pseudo-terminal pair
2217 These functions, derived from BSD, are available in the separate
2218 @file{libutil} library, and declared in @file{pty.h}.
2220 @comment pty.h
2221 @comment BSD
2222 @deftypefun int openpty (int *@var{amaster}, int *@var{aslave}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
2223 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2224 @c openpty @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2225 @c  getpt @acsfd
2226 @c  grantpt @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2227 @c  unlockpt dup @ascuheap/bsd @acsmem @acsfd
2228 @c  openpty:pts_name @acsuheap @acsmem @acsfd
2229 @c   ptsname_r dup @ascuheap/bsd @acsmem @acsfd
2230 @c   realloc dup @acsuheap @acsmem
2231 @c   malloc dup @acsuheap @acsmem
2232 @c   free dup @acsuheap @acsmem
2233 @c  open dup @acsfd
2234 @c  free dup @acsuheap @acsmem
2235 @c  tcsetattr dup ok
2236 @c  ioctl dup ok
2237 @c  strcpy dup ok
2238 @c  close dup @acsfd
2239 This function allocates and opens a pseudo-terminal pair, returning the
2240 file descriptor for the master in @var{*amaster}, and the file
2241 descriptor for the slave in @var{*aslave}.  If the argument @var{name}
2242 is not a null pointer, the file name of the slave pseudo-terminal
2243 device is stored in @code{*name}.  If @var{termp} is not a null pointer,
2244 the terminal attributes of the slave are set to the ones specified in
2245 the structure that @var{termp} points to (@pxref{Terminal Modes}).
2246 Likewise, if @var{winp} is not a null pointer, the screen size of
2247 the slave is set to the values specified in the structure that
2248 @var{winp} points to.
2250 The normal return value from @code{openpty} is @math{0}; a value of
2251 @math{-1} is returned in case of failure.  The following @code{errno}
2252 conditions are defined for this function:
2254 @table @code
2255 @item ENOENT
2256 There are no free pseudo-terminal pairs available.
2257 @end table
2259 @strong{Warning:} Using the @code{openpty} function with @var{name} not
2260 set to @code{NULL} is @strong{very dangerous} because it provides no
2261 protection against overflowing the string @var{name}.  You should use
2262 the @code{ttyname} function on the file descriptor returned in
2263 @var{*slave} to find out the file name of the slave pseudo-terminal
2264 device instead.
2265 @end deftypefun
2267 @comment pty.h
2268 @comment BSD
2269 @deftypefun int forkpty (int *@var{amaster}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
2270 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2271 @c forkpty @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2272 @c  openpty dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2273 @c  fork dup @aculock
2274 @c  close dup @acsfd
2275 @c  /child
2276 @c   close dup @acsfd
2277 @c   login_tty dup @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
2278 @c   _exit dup ok
2279 @c  close dup @acsfd
2280 This function is similar to the @code{openpty} function, but in
2281 addition, forks a new process (@pxref{Creating a Process}) and makes the
2282 newly opened slave pseudo-terminal device the controlling terminal
2283 (@pxref{Controlling Terminal}) for the child process.
2285 If the operation is successful, there are then both parent and child
2286 processes and both see @code{forkpty} return, but with different values:
2287 it returns a value of @math{0} in the child process and returns the child's
2288 process ID in the parent process.
2290 If the allocation of a pseudo-terminal pair or the process creation
2291 failed, @code{forkpty} returns a value of @math{-1} in the parent
2292 process.
2294 @strong{Warning:} The @code{forkpty} function has the same problems with
2295 respect to the @var{name} argument as @code{openpty}.
2296 @end deftypefun