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