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