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