blob: 0f0354b1ad162f194abbd1193b7b773929488c1e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001@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
4
5This chapter describes functions that are specific to terminal devices.
6You can use these functions to do things like turn off input echoing;
7set serial line characteristics such as line speed and flow control; and
8change which characters are used for end-of-file, command-line editing,
9sending signals, and similar control functions.
10
11Most of the functions in this chapter operate on file descriptors.
12@xref{Low-Level I/O}, for more information about what a file
13descriptor is and how to open a file descriptor for a terminal device.
14
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
29
30@node Is It a Terminal
31@section Identifying Terminals
32@cindex terminal identification
33@cindex identifying terminals
34
35The functions described in this chapter only work on files that
36correspond to terminal devices. You can find out whether a file
37descriptor is associated with a terminal by using the @code{isatty}
38function.
39
40@pindex unistd.h
41Prototypes for the functions in this section are declared in the header
42file @file{unistd.h}.
43
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
50This function returns @code{1} if @var{filedes} is a file descriptor
51associated with an open terminal device, and @math{0} otherwise.
52@end deftypefun
53
54If a file descriptor is associated with a terminal, you can get its
55associated file name using the @code{ttyname} function. See also the
56@code{ctermid} function, described in @ref{Identifying the Terminal}.
57
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
75If the file descriptor @var{filedes} is associated with a terminal
76device, the @code{ttyname} function returns a pointer to a
77statically-allocated, null-terminated string containing the file name of
78the terminal file. The value is a null pointer if the file descriptor
79isn't associated with a terminal, or the file name cannot be determined.
80@end deftypefun
81
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
97The @code{ttyname_r} function is similar to the @code{ttyname} function
98except that it places its result into the user-specified buffer starting
99at @var{buf} with length @var{len}.
100
101The normal return value from @code{ttyname_r} is @math{0}. Otherwise an
102error number is returned to indicate the error. The following
103@code{errno} error conditions are defined for this function:
104
105@table @code
106@item EBADF
107The @var{filedes} argument is not a valid file descriptor.
108
109@item ENOTTY
110The @var{filedes} is not associated with a terminal.
111
112@item ERANGE
113The buffer length @var{len} is too small to store the string to be
114returned.
115@end table
116@end deftypefun
117
118@node I/O Queues
119@section I/O Queues
120
121Many of the remaining functions in this section refer to the input and
122output queues of a terminal device. These queues implement a form of
123buffering @emph{within the kernel} independent of the buffering
124implemented by I/O streams (@pxref{I/O on Streams}).
125
126@cindex terminal input queue
127@cindex typeahead buffer
128The @dfn{terminal input queue} is also sometimes referred to as its
129@dfn{typeahead buffer}. It holds the characters that have been received
130from the terminal but not yet read by any process.
131
132The 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
134are guaranteed a queue size of at least @code{MAX_INPUT}, but the queue
135might be larger, and might even dynamically change size. If input flow
136control is enabled by setting the @code{IXOFF} input mode bit
137(@pxref{Input Modes}), the terminal driver transmits STOP and START
138characters to the terminal when necessary to prevent the queue from
139overflowing. Otherwise, input may be lost if it comes in too fast from
140the terminal. In canonical mode, all input stays in the queue until a
141newline character is received, so the terminal input queue can fill up
142when you type a very long line. @xref{Canonical or Not}.
143
144@cindex terminal output queue
145The @dfn{terminal output queue} is like the input queue, but for output;
146it contains characters that have been written by processes, but not yet
147transmitted to the terminal. If output flow control is enabled by
148setting the @code{IXON} input mode bit (@pxref{Input Modes}), the
149terminal driver obeys START and STOP characters sent by the terminal to
150stop and restart transmission of output.
151
152@dfn{Clearing} the terminal input queue means discarding any characters
153that have been received but not yet read. Similarly, clearing the
154terminal output queue means discarding any characters that have been
155written but not yet transmitted.
156
157@node Canonical or Not
158@section Two Styles of Input: Canonical or Not
159
160POSIX systems support two basic modes of input: canonical and
161noncanonical.
162
163@cindex canonical input processing
164In @dfn{canonical input processing} mode, terminal input is processed in
165lines terminated by newline (@code{'\n'}), EOF, or EOL characters. No
166input can be read until an entire line has been typed by the user, and
167the @code{read} function (@pxref{I/O Primitives}) returns at most a
168single line of input, no matter how many bytes are requested.
169
170In canonical input mode, the operating system provides input editing
171facilities: some characters are interpreted specially to perform editing
172operations within the current line of text, such as ERASE and KILL.
173@xref{Editing Characters}.
174
175The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterize
176the maximum number of bytes which may appear in a single line of
177canonical input. @xref{Limits for Files}. You are guaranteed a maximum
178line length of at least @code{MAX_CANON} bytes, but the maximum might be
179larger, and might even dynamically change size.
180
181@cindex noncanonical input processing
182In @dfn{noncanonical input processing} mode, characters are not grouped
183into lines, and ERASE and KILL processing is not performed. The
184granularity with which bytes are read in noncanonical input mode is
185controlled by the MIN and TIME settings. @xref{Noncanonical Input}.
186
187Most programs use canonical input mode, because this gives the user a
188way to edit input line by line. The usual reason to use noncanonical
189mode is when the program accepts single-character commands or provides
190its own editing facilities.
191
192The 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}.
195
196@node Terminal Modes
197@section Terminal Modes
198
199@pindex termios.h
200This section describes the various terminal attributes that control how
201input and output are done. The functions, data structures, and symbolic
202constants are all declared in the header file @file{termios.h}.
203
204Don't confuse terminal attributes with file attributes. A device special
205file which is associated with a terminal has file attributes as described
206in @ref{File Attributes}. These are unrelated to the attributes of the
207terminal device itself, which are discussed in this section.
208
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
225
226@node Mode Data Types
227@subsection Terminal Mode Data Types
228@cindex terminal mode data types
229
230The entire collection of attributes of a terminal is stored in a
231structure of type @code{struct termios}. This structure is used
232with the functions @code{tcgetattr} and @code{tcsetattr} to read
233and set the attributes.
234
235@comment termios.h
236@comment POSIX.1
237@deftp {Data Type} {struct termios}
238Structure that records all the I/O attributes of a terminal. The
239structure includes at least the following members:
240
241@table @code
242@item tcflag_t c_iflag
243A bit mask specifying flags for input modes; see @ref{Input Modes}.
244
245@item tcflag_t c_oflag
246A bit mask specifying flags for output modes; see @ref{Output Modes}.
247
248@item tcflag_t c_cflag
249A bit mask specifying flags for control modes; see @ref{Control Modes}.
250
251@item tcflag_t c_lflag
252A bit mask specifying flags for local modes; see @ref{Local Modes}.
253
254@item cc_t c_cc[NCCS]
255An array specifying which characters are associated with various
256control functions; see @ref{Special Characters}.
257@end table
258
259The @code{struct termios} structure also contains members which
260encode input and output transmission speeds, but the representation is
261not specified. @xref{Line Speed}, for how to examine and store the
262speed values.
263@end deftp
264
265The following sections describe the details of the members of the
266@code{struct termios} structure.
267
268@comment termios.h
269@comment POSIX.1
270@deftp {Data Type} tcflag_t
271This is an unsigned integer type used to represent the various
272bit masks for terminal flags.
273@end deftp
274
275@comment termios.h
276@comment POSIX.1
277@deftp {Data Type} cc_t
278This is an unsigned integer type used to represent characters associated
279with various terminal control functions.
280@end deftp
281
282@comment termios.h
283@comment POSIX.1
284@deftypevr Macro int NCCS
285The value of this macro is the number of elements in the @code{c_cc}
286array.
287@end deftypevr
288
289@node Mode Functions
290@subsection Terminal Mode Functions
291@cindex terminal mode functions
292
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.
299This function is used to examine the attributes of the terminal
300device with file descriptor @var{filedes}. The attributes are returned
301in the structure that @var{termios-p} points to.
302
303If successful, @code{tcgetattr} returns @math{0}. A return value of @math{-1}
304indicates an error. The following @code{errno} error conditions are
305defined for this function:
306
307@table @code
308@item EBADF
309The @var{filedes} argument is not a valid file descriptor.
310
311@item ENOTTY
312The @var{filedes} is not associated with a terminal.
313@end table
314@end deftypefun
315
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.
322This function sets the attributes of the terminal device with file
323descriptor @var{filedes}. The new attributes are taken from the
324structure that @var{termios-p} points to.
325
326The @var{when} argument specifies how to deal with input and output
327already queued. It can be one of the following values:
328
329@table @code
330@comment termios.h
331@comment POSIX.1
332@item TCSANOW
333@vindex TCSANOW
334Make the change immediately.
335
336@comment termios.h
337@comment POSIX.1
338@item TCSADRAIN
339@vindex TCSADRAIN
340Make the change after waiting until all queued output has been written.
341You should usually use this option when changing parameters that affect
342output.
343
344@comment termios.h
345@comment POSIX.1
346@item TCSAFLUSH
347@vindex TCSAFLUSH
348This is like @code{TCSADRAIN}, but also discards any queued input.
349
350@comment termios.h
351@comment BSD
352@item TCSASOFT
353@vindex TCSASOFT
354This is a flag bit that you can add to any of the above alternatives.
355Its meaning is to inhibit alteration of the state of the terminal
356hardware. It is a BSD extension; it is only supported on BSD systems
357and @gnuhurdsystems{}.
358
359Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}
360bit in the @code{c_cflag} member of the structure @var{termios-p} points
361to. @xref{Control Modes}, for a description of @code{CIGNORE}.
362@end table
363
364If this function is called from a background process on its controlling
365terminal, 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
367write to the terminal. The exception is if the calling process itself
368is ignoring or blocking @code{SIGTTOU} signals, in which case the
369operation is performed and no signal is sent. @xref{Job Control}.
370
371If successful, @code{tcsetattr} returns @math{0}. A return value of
372@math{-1} indicates an error. The following @code{errno} error
373conditions are defined for this function:
374
375@table @code
376@item EBADF
377The @var{filedes} argument is not a valid file descriptor.
378
379@item ENOTTY
380The @var{filedes} is not associated with a terminal.
381
382@item EINVAL
383Either the value of the @code{when} argument is not valid, or there is
384something wrong with the data in the @var{termios-p} argument.
385@end table
386@end deftypefun
387
388Although @code{tcgetattr} and @code{tcsetattr} specify the terminal
389device with a file descriptor, the attributes are those of the terminal
390device itself and not of the file descriptor. This means that the
391effects of changing terminal attributes are persistent; if another
392process opens the terminal file later on, it will see the changed
393attributes even though it doesn't have anything to do with the open file
394descriptor you originally specified in changing the attributes.
395
396Similarly, if a single process has multiple or duplicated file
397descriptors for the same terminal device, changing the terminal
398attributes affects input and output to all of these file
399descriptors. This means, for example, that you can't open one file
400descriptor or stream to read from a terminal in the normal
401line-buffered, echoed mode; and simultaneously have another file
402descriptor for the same terminal that you use to read from it in
403single-character, non-echoed mode. Instead, you have to explicitly
404switch the terminal back and forth between the two modes.
405
406@node Setting Modes
407@subsection Setting Terminal Modes Properly
408
409When you set terminal modes, you should call @code{tcgetattr} first to
410get the current modes of the particular terminal device, modify only
411those modes that you are really interested in, and store the result with
412@code{tcsetattr}.
413
414It's a bad idea to simply initialize a @code{struct termios} structure
415to a chosen set of attributes and pass it directly to @code{tcsetattr}.
416Your program may be run years from now, on systems that support members
417not documented in this manual. The way to avoid setting these members
418to unreasonable values is to avoid changing them.
419
420What's more, different terminal devices may require different mode
421settings in order to function properly. So you should avoid blindly
422copying attributes from one terminal device to another.
423
424When a member contains a collection of independent flags, as the
425@code{c_iflag}, @code{c_oflag} and @code{c_cflag} members do, even
426setting the entire member is a bad idea, because particular operating
427systems have their own flags. Instead, you should start with the
428current value of the member and alter only the flags whose values matter
429in your program, leaving any other flags unchanged.
430
431Here is an example of how to set one flag (@code{ISTRIP}) in the
432@code{struct termios} structure while properly preserving all the other
433data in the structure:
434
435@smallexample
436@group
437int
438set_istrip (int desc, int value)
439@{
440 struct termios settings;
441 int result;
442@end group
443
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;
465@}
466@end group
467@end smallexample
468
469@node Input Modes
470@subsection Input Modes
471
472This section describes the terminal attribute flags that control
473fairly low-level aspects of input processing: handling of parity errors,
474break signals, flow control, and @key{RET} and @key{LFD} characters.
475
476All 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
478change flags using the operators @code{&}, @code{|} and @code{^}. Don't
479try to specify the entire value for @code{c_iflag}---instead, change
480only specific flags and leave the rest untouched (@pxref{Setting
481Modes}).
482
483@comment termios.h
484@comment POSIX.1
485@deftypevr Macro tcflag_t INPCK
486@cindex parity checking
487If this bit is set, input parity checking is enabled. If it is not set,
488no checking at all is done for parity errors on input; the
489characters are simply passed through to the application.
490
491Parity checking on input processing is independent of whether parity
492detection and generation on the underlying terminal hardware is enabled;
493see @ref{Control Modes}. For example, you could clear the @code{INPCK}
494input mode flag and set the @code{PARENB} control mode flag to ignore
495parity errors on input, but still generate parity on output.
496
497If this bit is set, what happens when a parity error is detected depends
498on whether the @code{IGNPAR} or @code{PARMRK} bits are set. If neither
499of these bits are set, a byte with a parity error is passed to the
500application as a @code{'\0'} character.
501@end deftypevr
502
503@comment termios.h
504@comment POSIX.1
505@deftypevr Macro tcflag_t IGNPAR
506If this bit is set, any byte with a framing or parity error is ignored.
507This is only useful if @code{INPCK} is also set.
508@end deftypevr
509
510@comment termios.h
511@comment POSIX.1
512@deftypevr Macro tcflag_t PARMRK
513If this bit is set, input bytes with parity or framing errors are marked
514when passed to the program. This bit is meaningful only when
515@code{INPCK} is set and @code{IGNPAR} is not set.
516
517The way erroneous bytes are marked is with two preceding bytes,
518@code{377} and @code{0}. Thus, the program actually reads three bytes
519for one erroneous byte received from the terminal.
520
521If a valid byte has the value @code{0377}, and @code{ISTRIP} (see below)
522is not set, the program might confuse it with the prefix that marks a
523parity error. So a valid byte @code{0377} is passed to the program as
524two bytes, @code{0377} @code{0377}, in this case.
525@end deftypevr
526
527@comment termios.h
528@comment POSIX.1
529@deftypevr Macro tcflag_t ISTRIP
530If this bit is set, valid input bytes are stripped to seven bits;
531otherwise, all eight bits are available for programs to read.
532@end deftypevr
533
534@comment termios.h
535@comment POSIX.1
536@deftypevr Macro tcflag_t IGNBRK
537If this bit is set, break conditions are ignored.
538
539@cindex break condition, detecting
540A @dfn{break condition} is defined in the context of asynchronous
541serial data transmission as a series of zero-value bits longer than a
542single byte.
543@end deftypevr
544
545@comment termios.h
546@comment POSIX.1
547@deftypevr Macro tcflag_t BRKINT
548If this bit is set and @code{IGNBRK} is not set, a break condition
549clears the terminal input and output queues and raises a @code{SIGINT}
550signal for the foreground process group associated with the terminal.
551
552If neither @code{BRKINT} nor @code{IGNBRK} are set, a break condition is
553passed 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
557
558@comment termios.h
559@comment POSIX.1
560@deftypevr Macro tcflag_t IGNCR
561If this bit is set, carriage return characters (@code{'\r'}) are
562discarded on input. Discarding carriage return may be useful on
563terminals that send both carriage return and linefeed when you type the
564@key{RET} key.
565@end deftypevr
566
567@comment termios.h
568@comment POSIX.1
569@deftypevr Macro tcflag_t ICRNL
570If 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
572characters (@code{'\n'}).
573@end deftypevr
574
575@comment termios.h
576@comment POSIX.1
577@deftypevr Macro tcflag_t INLCR
578If this bit is set, newline characters (@code{'\n'}) received as input
579are passed to the application as carriage return characters (@code{'\r'}).
580@end deftypevr
581
582@comment termios.h
583@comment POSIX.1
584@deftypevr Macro tcflag_t IXOFF
585If this bit is set, start/stop control on input is enabled. In other
586words, the computer sends STOP and START characters as necessary to
587prevent input from coming in faster than programs are reading it. The
588idea is that the actual terminal hardware that is generating the input
589data responds to a STOP character by suspending transmission, and to a
590START character by resuming transmission. @xref{Start/Stop Characters}.
591@end deftypevr
592
593@comment termios.h
594@comment POSIX.1
595@deftypevr Macro tcflag_t IXON
596If this bit is set, start/stop control on output is enabled. In other
597words, if the computer receives a STOP character, it suspends output
598until a START character is received. In this case, the STOP and START
599characters are never passed to the application program. If this bit is
600not 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
604
605@comment termios.h
606@comment BSD
607@deftypevr Macro tcflag_t IXANY
608If this bit is set, any input character restarts output when output has
609been suspended with the STOP character. Otherwise, only the START
610character restarts output.
611
612This is a BSD extension; it exists only on BSD systems and
613@gnulinuxhurdsystems{}.
614@end deftypevr
615
616@comment termios.h
617@comment BSD
618@deftypevr Macro tcflag_t IMAXBEL
619If this bit is set, then filling up the terminal input buffer sends a
620BEL character (code @code{007}) to the terminal to ring the bell.
621
622This is a BSD extension.
623@end deftypevr
624
625@node Output Modes
626@subsection Output Modes
627
628This section describes the terminal flags and fields that control how
629output characters are translated and padded for display. All of these
630are contained in the @code{c_oflag} member of the @w{@code{struct termios}}
631structure.
632
633The @code{c_oflag} member itself is an integer, and you change the flags
634and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
635try to specify the entire value for @code{c_oflag}---instead, change
636only specific flags and leave the rest untouched (@pxref{Setting
637Modes}).
638
639@comment termios.h
640@comment POSIX.1
641@deftypevr Macro tcflag_t OPOST
642If this bit is set, output data is processed in some unspecified way so
643that it is displayed appropriately on the terminal device. This
644typically includes mapping newline characters (@code{'\n'}) onto
645carriage return and linefeed pairs.
646
647If this bit isn't set, the characters are transmitted as-is.
648@end deftypevr
649
650The following three bits are effective only if @code{OPOST} is set.
651
652@comment termios.h
653@comment POSIX.1
654@deftypevr Macro tcflag_t ONLCR
655If this bit is set, convert the newline character on output into a pair
656of characters, carriage return followed by linefeed.
657@end deftypevr
658
659@comment termios.h (optional)
660@comment BSD
661@deftypevr Macro tcflag_t OXTABS
662If this bit is set, convert tab characters on output into the appropriate
663number of spaces to emulate a tab stop every eight columns. This bit
664exists only on BSD systems and @gnuhurdsystems{}; on
665@gnulinuxsystems{} it is available as @code{XTABS}.
666@end deftypevr
667
668@comment termios.h (optional)
669@comment BSD
670@deftypevr Macro tcflag_t ONOEOT
671If this bit is set, discard @kbd{C-d} characters (code @code{004}) on
672output. These characters cause many dial-up terminals to disconnect.
673This bit exists only on BSD systems and @gnuhurdsystems{}.
674@end deftypevr
675
676@node Control Modes
677@subsection Control Modes
678
679This section describes the terminal flags and fields that control
680parameters usually associated with asynchronous serial data
681transmission. These flags may not make sense for other kinds of
682terminal ports (such as a network connection pseudo-terminal). All of
683these are contained in the @code{c_cflag} member of the @code{struct
684termios} structure.
685
686The @code{c_cflag} member itself is an integer, and you change the flags
687and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
688try to specify the entire value for @code{c_cflag}---instead, change
689only specific flags and leave the rest untouched (@pxref{Setting
690Modes}).
691
692@comment termios.h
693@comment POSIX.1
694@deftypevr Macro tcflag_t CLOCAL
695If this bit is set, it indicates that the terminal is connected
696``locally'' and that the modem status lines (such as carrier detect)
697should be ignored.
698@cindex modem status lines
699@cindex carrier detect
700
701On many systems if this bit is not set and you call @code{open} without
702the @code{O_NONBLOCK} flag set, @code{open} blocks until a modem
703connection is established.
704
705If 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
707terminal (if it has one). Normally, this causes the process to exit;
708see @ref{Signal Handling}. Reading from the terminal after a disconnect
709causes an end-of-file condition, and writing causes an @code{EIO} error
710to be returned. The terminal device must be closed and reopened to
711clear the condition.
712@cindex modem disconnect
713@end deftypevr
714
715@comment termios.h
716@comment POSIX.1
717@deftypevr Macro tcflag_t HUPCL
718If this bit is set, a modem disconnect is generated when all processes
719that have the terminal device open have either closed the file or exited.
720@end deftypevr
721
722@comment termios.h
723@comment POSIX.1
724@deftypevr Macro tcflag_t CREAD
725If this bit is set, input can be read from the terminal. Otherwise,
726input is discarded when it arrives.
727@end deftypevr
728
729@comment termios.h
730@comment POSIX.1
731@deftypevr Macro tcflag_t CSTOPB
732If this bit is set, two stop bits are used. Otherwise, only one stop bit
733is used.
734@end deftypevr
735
736@comment termios.h
737@comment POSIX.1
738@deftypevr Macro tcflag_t PARENB
739If 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.
741
742If this bit is not set, no parity bit is added to output characters, and
743input characters are not checked for correct parity.
744@end deftypevr
745
746@comment termios.h
747@comment POSIX.1
748@deftypevr Macro tcflag_t PARODD
749This bit is only useful if @code{PARENB} is set. If @code{PARODD} is set,
750odd parity is used, otherwise even parity is used.
751@end deftypevr
752
753The control mode flags also includes a field for the number of bits per
754character. You can use the @code{CSIZE} macro as a mask to extract the
755value, like this: @code{settings.c_cflag & CSIZE}.
756
757@comment termios.h
758@comment POSIX.1
759@deftypevr Macro tcflag_t CSIZE
760This is a mask for the number of bits per character.
761@end deftypevr
762
763@comment termios.h
764@comment POSIX.1
765@deftypevr Macro tcflag_t CS5
766This specifies five bits per byte.
767@end deftypevr
768
769@comment termios.h
770@comment POSIX.1
771@deftypevr Macro tcflag_t CS6
772This specifies six bits per byte.
773@end deftypevr
774
775@comment termios.h
776@comment POSIX.1
777@deftypevr Macro tcflag_t CS7
778This specifies seven bits per byte.
779@end deftypevr
780
781@comment termios.h
782@comment POSIX.1
783@deftypevr Macro tcflag_t CS8
784This specifies eight bits per byte.
785@end deftypevr
786
787The following four bits are BSD extensions; these exist only on BSD
788systems and @gnuhurdsystems{}.
789
790@comment termios.h
791@comment BSD
792@deftypevr Macro tcflag_t CCTS_OFLOW
793If this bit is set, enable flow control of output based on the CTS wire
794(RS232 protocol).
795@end deftypevr
796
797@comment termios.h
798@comment BSD
799@deftypevr Macro tcflag_t CRTS_IFLOW
800If this bit is set, enable flow control of input based on the RTS wire
801(RS232 protocol).
802@end deftypevr
803
804@comment termios.h
805@comment BSD
806@deftypevr Macro tcflag_t MDMBUF
807If this bit is set, enable carrier-based flow control of output.
808@end deftypevr
809
810@comment termios.h
811@comment BSD
812@deftypevr Macro tcflag_t CIGNORE
813If this bit is set, it says to ignore the control modes and line speed
814values entirely. This is only meaningful in a call to @code{tcsetattr}.
815
816The @code{c_cflag} member and the line speed values returned by
817@code{cfgetispeed} and @code{cfgetospeed} will be unaffected by the
818call. @code{CIGNORE} is useful if you want to set all the software
819modes 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.)
822
823This bit is never set in the structure filled in by @code{tcgetattr}.
824@end deftypevr
825
826@node Local Modes
827@subsection Local Modes
828
829This section describes the flags for the @code{c_lflag} member of the
830@code{struct termios} structure. These flags generally control
831higher-level aspects of input processing than the input modes flags
832described in @ref{Input Modes}, such as echoing, signals, and the choice
833of canonical or noncanonical input.
834
835The @code{c_lflag} member itself is an integer, and you change the flags
836and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
837try to specify the entire value for @code{c_lflag}---instead, change
838only specific flags and leave the rest untouched (@pxref{Setting
839Modes}).
840
841@comment termios.h
842@comment POSIX.1
843@deftypevr Macro tcflag_t ICANON
844This bit, if set, enables canonical input processing mode. Otherwise,
845input is processed in noncanonical mode. @xref{Canonical or Not}.
846@end deftypevr
847
848@comment termios.h
849@comment POSIX.1
850@deftypevr Macro tcflag_t ECHO
851If this bit is set, echoing of input characters back to the terminal
852is enabled.
853@cindex echo of terminal input
854@end deftypevr
855
856@comment termios.h
857@comment POSIX.1
858@deftypevr Macro tcflag_t ECHOE
859If this bit is set, echoing indicates erasure of input with the ERASE
860character by erasing the last character in the current line from the
861screen. Otherwise, the character erased is re-echoed to show what has
862happened (suitable for a printing terminal).
863
864This bit only controls the display behavior; the @code{ICANON} bit by
865itself controls actual recognition of the ERASE character and erasure of
866input, without which @code{ECHOE} is simply irrelevant.
867@end deftypevr
868
869@comment termios.h
870@comment BSD
871@deftypevr Macro tcflag_t ECHOPRT
872This bit is like @code{ECHOE}, enables display of the ERASE character in
873a way that is geared to a hardcopy terminal. When you type the ERASE
874character, a @samp{\} character is printed followed by the first
875character erased. Typing the ERASE character again just prints the next
876character erased. Then, the next time you type a normal character, a
877@samp{/} character is printed before the character echoes.
878
879This is a BSD extension, and exists only in BSD systems and
880@gnulinuxhurdsystems{}.
881@end deftypevr
882
883@comment termios.h
884@comment POSIX.1
885@deftypevr Macro tcflag_t ECHOK
886This bit enables special display of the KILL character by moving to a
887new line after echoing the KILL character normally. The behavior of
888@code{ECHOKE} (below) is nicer to look at.
889
890If this bit is not set, the KILL character echoes just as it would if it
891were not the KILL character. Then it is up to the user to remember that
892the KILL character has erased the preceding input; there is no
893indication of this on the screen.
894
895This bit only controls the display behavior; the @code{ICANON} bit by
896itself controls actual recognition of the KILL character and erasure of
897input, without which @code{ECHOK} is simply irrelevant.
898@end deftypevr
899
900@comment termios.h
901@comment BSD
902@deftypevr Macro tcflag_t ECHOKE
903This bit is similar to @code{ECHOK}. It enables special display of the
904KILL character by erasing on the screen the entire line that has been
905killed. This is a BSD extension, and exists only in BSD systems and
906@gnulinuxhurdsystems{}.
907@end deftypevr
908
909@comment termios.h
910@comment POSIX.1
911@deftypevr Macro tcflag_t ECHONL
912If this bit is set and the @code{ICANON} bit is also set, then the
913newline (@code{'\n'}) character is echoed even if the @code{ECHO} bit
914is not set.
915@end deftypevr
916
917@comment termios.h
918@comment BSD
919@deftypevr Macro tcflag_t ECHOCTL
920If this bit is set and the @code{ECHO} bit is also set, echo control
921characters with @samp{^} followed by the corresponding text character.
922Thus, control-A echoes as @samp{^A}. This is usually the preferred mode
923for interactive input, because echoing a control character back to the
924terminal could have some undesired effect on the terminal.
925
926This is a BSD extension, and exists only in BSD systems and
927@gnulinuxhurdsystems{}.
928@end deftypevr
929
930@comment termios.h
931@comment POSIX.1
932@deftypevr Macro tcflag_t ISIG
933This bit controls whether the INTR, QUIT, and SUSP characters are
934recognized. The functions associated with these characters are performed
935if and only if this bit is set. Being in canonical or noncanonical
936input mode has no affect on the interpretation of these characters.
937
938You should use caution when disabling recognition of these characters.
939Programs that cannot be interrupted interactively are very
940user-unfriendly. If you clear this bit, your program should provide
941some alternate interface that allows the user to interactively send the
942signals associated with these characters, or to escape from the program.
943@cindex interactive signals, from terminal
944
945@xref{Signal Characters}.
946@end deftypevr
947
948@comment termios.h
949@comment POSIX.1
950@deftypevr Macro tcflag_t IEXTEN
951POSIX.1 gives @code{IEXTEN} implementation-defined meaning,
952so you cannot rely on this interpretation on all systems.
953
954On BSD systems and @gnulinuxhurdsystems{}, it enables the LNEXT and
955DISCARD characters.
956@xref{Other Special}.
957@end deftypevr
958
959@comment termios.h
960@comment POSIX.1
961@deftypevr Macro tcflag_t NOFLSH
962Normally, the INTR, QUIT, and SUSP characters cause input and output
963queues for the terminal to be cleared. If this bit is set, the queues
964are not cleared.
965@end deftypevr
966
967@comment termios.h
968@comment POSIX.1
969@deftypevr Macro tcflag_t TOSTOP
970If this bit is set and the system supports job control, then
971@code{SIGTTOU} signals are generated by background processes that
972attempt to write to the terminal. @xref{Access to the Terminal}.
973@end deftypevr
974
975The following bits are BSD extensions; they exist only on BSD systems
976and @gnuhurdsystems{}.
977
978@comment termios.h
979@comment BSD
980@deftypevr Macro tcflag_t ALTWERASE
981This bit determines how far the WERASE character should erase. The
982WERASE character erases back to the beginning of a word; the question
983is, where do words begin?
984
985If this bit is clear, then the beginning of a word is a nonwhitespace
986character following a whitespace character. If the bit is set, then the
987beginning of a word is an alphanumeric character or underscore following
988a character which is none of those.
989
990@xref{Editing Characters}, for more information about the WERASE character.
991@end deftypevr
992
993@comment termios.h
994@comment BSD
995@deftypevr Macro tcflag_t FLUSHO
996This is the bit that toggles when the user types the DISCARD character.
997While this bit is set, all output is discarded. @xref{Other Special}.
998@end deftypevr
999
1000@comment termios.h (optional)
1001@comment BSD
1002@deftypevr Macro tcflag_t NOKERNINFO
1003Setting this bit disables handling of the STATUS character.
1004@xref{Other Special}.
1005@end deftypevr
1006
1007@comment termios.h
1008@comment BSD
1009@deftypevr Macro tcflag_t PENDIN
1010If this bit is set, it indicates that there is a line of input that
1011needs to be reprinted. Typing the REPRINT character sets this bit; the
1012bit remains set until reprinting is finished. @xref{Editing Characters}.
1013@end deftypevr
1014
1015@c EXTPROC is too obscure to document now. --roland
1016
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
1023
1024The terminal line speed tells the computer how fast to read and write
1025data on the terminal.
1026
1027If the terminal is connected to a real serial line, the terminal speed
1028you specify actually controls the line---if it doesn't match the
1029terminal's own idea of the speed, communication does not work. Real
1030serial ports accept only certain standard speeds. Also, particular
1031hardware may not support even all the standard speeds. Specifying a
1032speed of zero hangs up a dialup connection and turns off modem control
1033signals.
1034
1035If the terminal is not a real serial line (for example, if it is a
1036network connection), then the line speed won't really affect data
1037transmission speed, but some programs will use it to determine the
1038amount of padding needed. It's best to specify a line speed value that
1039matches the actual speed of the actual terminal, but you can safely
1040experiment with different values to vary the amount of padding.
1041
1042There are actually two line speeds for each terminal, one for input and
1043one for output. You can set them independently, but most often
1044terminals use the same speed for both directions.
1045
1046The speed values are stored in the @code{struct termios} structure, but
1047don't try to access them in the @code{struct termios} structure
1048directly. Instead, you should use the following functions to read and
1049store them:
1050
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.
1058This function returns the output line speed stored in the structure
1059@code{*@var{termios-p}}.
1060@end deftypefun
1061
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{}}
1066This function returns the input line speed stored in the structure
1067@code{*@var{termios-p}}.
1068@end deftypefun
1069
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{}}
1074This function stores @var{speed} in @code{*@var{termios-p}} as the output
1075speed. The normal return value is @math{0}; a value of @math{-1}
1076indicates an error. If @var{speed} is not a speed, @code{cfsetospeed}
1077returns @math{-1}.
1078@end deftypefun
1079
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{}}
1084This function stores @var{speed} in @code{*@var{termios-p}} as the input
1085speed. The normal return value is @math{0}; a value of @math{-1}
1086indicates an error. If @var{speed} is not a speed, @code{cfsetospeed}
1087returns @math{-1}.
1088@end deftypefun
1089
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.
1097
1098@c cfsetspeed ok
1099@c cfsetispeed ok
1100@c cfsetospeed ok
1101This function stores @var{speed} in @code{*@var{termios-p}} as both the
1102input and output speeds. The normal return value is @math{0}; a value
1103of @math{-1} indicates an error. If @var{speed} is not a speed,
1104@code{cfsetspeed} returns @math{-1}. This function is an extension in
11054.4 BSD.
1106@end deftypefun
1107
1108@comment termios.h
1109@comment POSIX.1
1110@deftp {Data Type} speed_t
1111The @code{speed_t} type is an unsigned integer data type used to
1112represent line speeds.
1113@end deftp
1114
1115The functions @code{cfsetospeed} and @code{cfsetispeed} report errors
1116only for speed values that the system simply cannot handle. If you
1117specify a speed value that is basically acceptable, then those functions
1118will succeed. But they do not check that a particular hardware device
1119can actually support the specified speeds---in fact, they don't know
1120which device you plan to set the speed for. If you use @code{tcsetattr}
1121to set the speed of a particular device to a value that it cannot
1122handle, @code{tcsetattr} returns @math{-1}.
1123
1124@strong{Portability note:} In @theglibc{}, the functions above
1125accept speeds measured in bits per second as input, and return speed
1126values measured in bits per second. Other libraries require speeds to
1127be indicated by special codes. For POSIX.1 portability, you must use
1128one of the following symbols to represent the speed; their precise
1129numeric 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.
1131There is no portable way to represent any speed but these, but these are
1132the only speeds that typical serial lines can support.
1133
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
1195B0 B50 B75 B110 B134 B150 B200
1196B300 B600 B1200 B1800 B2400 B4800
1197B9600 B19200 B38400 B57600 B115200
1198B230400 B460800
1199@end smallexample
1200
1201@vindex EXTA
1202@vindex EXTB
1203BSD defines two additional speed symbols as aliases: @code{EXTA} is an
1204alias for @code{B19200} and @code{EXTB} is an alias for @code{B38400}.
1205These aliases are obsolete.
1206
1207@node Special Characters
1208@subsection Special Characters
1209
1210In canonical input, the terminal driver recognizes a number of special
1211characters which perform various control functions. These include the
1212ERASE character (usually @key{DEL}) for editing input, and other editing
1213characters. The INTR character (normally @kbd{C-c}) for sending a
1214@code{SIGINT} signal, and other signal-raising characters, may be
1215available in either canonical or noncanonical input mode. All these
1216characters are described in this section.
1217
1218The particular characters used are specified in the @code{c_cc} member
1219of the @code{struct termios} structure. This member is an array; each
1220element specifies the character for a particular role. Each element has
1221a symbolic constant that stands for the index of that element---for
1222example, @code{VINTR} is the index of the element that specifies the INTR
1223character, so storing @code{'='} in @code{@var{termios}.c_cc[VINTR]}
1224specifies @samp{=} as the INTR character.
1225
1226@vindex _POSIX_VDISABLE
1227On some systems, you can disable a particular special character function
1228by specifying the value @code{_POSIX_VDISABLE} for that role. This
1229value is unequal to any possible character code. @xref{Options for
1230Files}, for more information about how to tell whether the operating
1231system you are using supports @code{_POSIX_VDISABLE}.
1232
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
1243
1244@node Editing Characters
1245@subsubsection Characters for Input Editing
1246
1247These special characters are active only in canonical input mode.
1248@xref{Canonical or Not}.
1249
1250@comment termios.h
1251@comment POSIX.1
1252@deftypevr Macro int VEOF
1253@cindex EOF character
1254This is the subscript for the EOF character in the special control
1255character array. @code{@var{termios}.c_cc[VEOF]} holds the character
1256itself.
1257
1258The EOF character is recognized only in canonical input mode. It acts
1259as a line terminator in the same way as a newline character, but if the
1260EOF character is typed at the beginning of a line it causes @code{read}
1261to return a byte count of zero, indicating end-of-file. The EOF
1262character itself is discarded.
1263
1264Usually, the EOF character is @kbd{C-d}.
1265@end deftypevr
1266
1267@comment termios.h
1268@comment POSIX.1
1269@deftypevr Macro int VEOL
1270@cindex EOL character
1271This is the subscript for the EOL character in the special control
1272character array. @code{@var{termios}.c_cc[VEOL]} holds the character
1273itself.
1274
1275The EOL character is recognized only in canonical input mode. It acts
1276as a line terminator, just like a newline character. The EOL character
1277is not discarded; it is read as the last character in the input line.
1278
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.
1281
1282You don't need to use the EOL character to make @key{RET} end a line.
1283Just set the ICRNL flag. In fact, this is the default state of
1284affairs.
1285@end deftypevr
1286
1287@comment termios.h
1288@comment BSD
1289@deftypevr Macro int VEOL2
1290@cindex EOL2 character
1291This is the subscript for the EOL2 character in the special control
1292character array. @code{@var{termios}.c_cc[VEOL2]} holds the character
1293itself.
1294
1295The EOL2 character works just like the EOL character (see above), but it
1296can be a different character. Thus, you can specify two characters to
1297terminate an input line, by setting EOL to one of them and EOL2 to the
1298other.
1299
1300The EOL2 character is a BSD extension; it exists only on BSD systems
1301and @gnulinuxhurdsystems{}.
1302@end deftypevr
1303
1304@comment termios.h
1305@comment POSIX.1
1306@deftypevr Macro int VERASE
1307@cindex ERASE character
1308This is the subscript for the ERASE character in the special control
1309character array. @code{@var{termios}.c_cc[VERASE]} holds the
1310character itself.
1311
1312The ERASE character is recognized only in canonical input mode. When
1313the user types the erase character, the previous character typed is
1314discarded. (If the terminal generates multibyte character sequences,
1315this may cause more than one byte of input to be discarded.) This
1316cannot be used to erase past the beginning of the current line of text.
1317The ERASE character itself is discarded.
1318@c !!! mention ECHOE here
1319
1320Usually, the ERASE character is @key{DEL}.
1321@end deftypevr
1322
1323@comment termios.h
1324@comment BSD
1325@deftypevr Macro int VWERASE
1326@cindex WERASE character
1327This is the subscript for the WERASE character in the special control
1328character array. @code{@var{termios}.c_cc[VWERASE]} holds the character
1329itself.
1330
1331The WERASE character is recognized only in canonical mode. It erases an
1332entire word of prior input, and any whitespace after it; whitespace
1333characters before the word are not erased.
1334
1335The definition of a ``word'' depends on the setting of the
1336@code{ALTWERASE} mode; @pxref{Local Modes}.
1337
1338If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
1339of any characters except space or tab.
1340
1341If the @code{ALTWERASE} mode is set, a word is defined as a sequence of
1342characters containing only letters, numbers, and underscores, optionally
1343followed by one character that is not a letter, number, or underscore.
1344
1345The WERASE character is usually @kbd{C-w}.
1346
1347This is a BSD extension.
1348@end deftypevr
1349
1350@comment termios.h
1351@comment POSIX.1
1352@deftypevr Macro int VKILL
1353@cindex KILL character
1354This is the subscript for the KILL character in the special control
1355character array. @code{@var{termios}.c_cc[VKILL]} holds the character
1356itself.
1357
1358The KILL character is recognized only in canonical input mode. When the
1359user types the kill character, the entire contents of the current line
1360of input are discarded. The kill character itself is discarded too.
1361
1362The KILL character is usually @kbd{C-u}.
1363@end deftypevr
1364
1365@comment termios.h
1366@comment BSD
1367@deftypevr Macro int VREPRINT
1368@cindex REPRINT character
1369This is the subscript for the REPRINT character in the special control
1370character array. @code{@var{termios}.c_cc[VREPRINT]} holds the character
1371itself.
1372
1373The REPRINT character is recognized only in canonical mode. It reprints
1374the current input line. If some asynchronous output has come while you
1375are typing, this lets you see the line you are typing clearly again.
1376
1377The REPRINT character is usually @kbd{C-r}.
1378
1379This is a BSD extension.
1380@end deftypevr
1381
1382@node Signal Characters
1383@subsubsection Characters that Cause Signals
1384
1385These special characters may be active in either canonical or noncanonical
1386input mode, but only when the @code{ISIG} flag is set (@pxref{Local
1387Modes}).
1388
1389@comment termios.h
1390@comment POSIX.1
1391@deftypevr Macro int VINTR
1392@cindex INTR character
1393@cindex interrupt character
1394This is the subscript for the INTR character in the special control
1395character array. @code{@var{termios}.c_cc[VINTR]} holds the character
1396itself.
1397
1398The INTR (interrupt) character raises a @code{SIGINT} signal for all
1399processes in the foreground job associated with the terminal. The INTR
1400character itself is then discarded. @xref{Signal Handling}, for more
1401information about signals.
1402
1403Typically, the INTR character is @kbd{C-c}.
1404@end deftypevr
1405
1406@comment termios.h
1407@comment POSIX.1
1408@deftypevr Macro int VQUIT
1409@cindex QUIT character
1410This is the subscript for the QUIT character in the special control
1411character array. @code{@var{termios}.c_cc[VQUIT]} holds the character
1412itself.
1413
1414The QUIT character raises a @code{SIGQUIT} signal for all processes in
1415the foreground job associated with the terminal. The QUIT character
1416itself is then discarded. @xref{Signal Handling}, for more information
1417about signals.
1418
1419Typically, the QUIT character is @kbd{C-\}.
1420@end deftypevr
1421
1422@comment termios.h
1423@comment POSIX.1
1424@deftypevr Macro int VSUSP
1425@cindex SUSP character
1426@cindex suspend character
1427This is the subscript for the SUSP character in the special control
1428character array. @code{@var{termios}.c_cc[VSUSP]} holds the character
1429itself.
1430
1431The SUSP (suspend) character is recognized only if the implementation
1432supports job control (@pxref{Job Control}). It causes a @code{SIGTSTP}
1433signal to be sent to all processes in the foreground job associated with
1434the terminal. The SUSP character itself is then discarded.
1435@xref{Signal Handling}, for more information about signals.
1436
1437Typically, the SUSP character is @kbd{C-z}.
1438@end deftypevr
1439
1440Few applications disable the normal interpretation of the SUSP
1441character. If your program does this, it should provide some other
1442mechanism for the user to stop the job. When the user invokes this
1443mechanism, the program should send a @code{SIGTSTP} signal to the
1444process group of the process, not just to the process itself.
1445@xref{Signaling Another Process}.
1446
1447@comment termios.h
1448@comment BSD
1449@deftypevr Macro int VDSUSP
1450@cindex DSUSP character
1451@cindex delayed suspend character
1452This is the subscript for the DSUSP character in the special control
1453character array. @code{@var{termios}.c_cc[VDSUSP]} holds the character
1454itself.
1455
1456The DSUSP (suspend) character is recognized only if the implementation
1457supports job control (@pxref{Job Control}). It sends a @code{SIGTSTP}
1458signal, like the SUSP character, but not right away---only when the
1459program tries to read it as input. Not all systems with job control
1460support DSUSP; only BSD-compatible systems (including @gnuhurdsystems{}).
1461
1462@xref{Signal Handling}, for more information about signals.
1463
1464Typically, the DSUSP character is @kbd{C-y}.
1465@end deftypevr
1466
1467@node Start/Stop Characters
1468@subsubsection Special Characters for Flow Control
1469
1470These special characters may be active in either canonical or noncanonical
1471input mode, but their use is controlled by the flags @code{IXON} and
1472@code{IXOFF} (@pxref{Input Modes}).
1473
1474@comment termios.h
1475@comment POSIX.1
1476@deftypevr Macro int VSTART
1477@cindex START character
1478This is the subscript for the START character in the special control
1479character array. @code{@var{termios}.c_cc[VSTART]} holds the
1480character itself.
1481
1482The START character is used to support the @code{IXON} and @code{IXOFF}
1483input modes. If @code{IXON} is set, receiving a START character resumes
1484suspended output; the START character itself is discarded. If
1485@code{IXANY} is set, receiving any character at all resumes suspended
1486output; the resuming character is not discarded unless it is the START
1487character. @code{IXOFF} is set, the system may also transmit START
1488characters to the terminal.
1489
1490The usual value for the START character is @kbd{C-q}. You may not be
1491able to change this value---the hardware may insist on using @kbd{C-q}
1492regardless of what you specify.
1493@end deftypevr
1494
1495@comment termios.h
1496@comment POSIX.1
1497@deftypevr Macro int VSTOP
1498@cindex STOP character
1499This is the subscript for the STOP character in the special control
1500character array. @code{@var{termios}.c_cc[VSTOP]} holds the character
1501itself.
1502
1503The STOP character is used to support the @code{IXON} and @code{IXOFF}
1504input modes. If @code{IXON} is set, receiving a STOP character causes
1505output to be suspended; the STOP character itself is discarded. If
1506@code{IXOFF} is set, the system may also transmit STOP characters to the
1507terminal, to prevent the input queue from overflowing.
1508
1509The usual value for the STOP character is @kbd{C-s}. You may not be
1510able to change this value---the hardware may insist on using @kbd{C-s}
1511regardless of what you specify.
1512@end deftypevr
1513
1514@node Other Special
1515@subsubsection Other Special Characters
1516
1517@comment termios.h
1518@comment BSD
1519@deftypevr Macro int VLNEXT
1520@cindex LNEXT character
1521This is the subscript for the LNEXT character in the special control
1522character array. @code{@var{termios}.c_cc[VLNEXT]} holds the character
1523itself.
1524
1525The LNEXT character is recognized only when @code{IEXTEN} is set, but in
1526both canonical and noncanonical mode. It disables any special
1527significance of the next character the user types. Even if the
1528character would normally perform some editing function or generate a
1529signal, 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.''
1531
1532The LNEXT character is usually @kbd{C-v}.
1533
1534This character is available on BSD systems and @gnulinuxhurdsystems{}.
1535@end deftypevr
1536
1537@comment termios.h
1538@comment BSD
1539@deftypevr Macro int VDISCARD
1540@cindex DISCARD character
1541This is the subscript for the DISCARD character in the special control
1542character array. @code{@var{termios}.c_cc[VDISCARD]} holds the character
1543itself.
1544
1545The DISCARD character is recognized only when @code{IEXTEN} is set, but
1546in both canonical and noncanonical mode. Its effect is to toggle the
1547discard-output flag. When this flag is set, all program output is
1548discarded. Setting the flag also discards all output currently in the
1549output buffer. Typing any other character resets the flag.
1550
1551This character is available on BSD systems and @gnulinuxhurdsystems{}.
1552@end deftypevr
1553
1554@comment termios.h
1555@comment BSD
1556@deftypevr Macro int VSTATUS
1557@cindex STATUS character
1558This is the subscript for the STATUS character in the special control
1559character array. @code{@var{termios}.c_cc[VSTATUS]} holds the character
1560itself.
1561
1562The STATUS character's effect is to print out a status message about how
1563the current process is running.
1564
1565The STATUS character is recognized only in canonical mode, and only if
1566@code{NOKERNINFO} is not set.
1567
1568This character is available only on BSD systems and @gnuhurdsystems{}.
1569@end deftypevr
1570
1571@node Noncanonical Input
1572@subsection Noncanonical Input
1573
1574In noncanonical input mode, the special editing characters such as
1575ERASE and KILL are ignored. The system facilities for the user to edit
1576input are disabled in noncanonical mode, so that all input characters
1577(unless they are special for signal or flow-control purposes) are passed
1578to the application program exactly as typed. It is up to the
1579application program to give the user ways to edit the input, if
1580appropriate.
1581
1582Noncanonical mode offers special parameters called MIN and TIME for
1583controlling whether and how long to wait for input to be available. You
1584can even use them to avoid ever waiting---to return immediately with
1585whatever input is available, or with no input.
1586
1587The MIN and TIME are stored in elements of the @code{c_cc} array, which
1588is a member of the @w{@code{struct termios}} structure. Each element of
1589this array has a particular role, and each element has a symbolic
1590constant 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
1592TIME slots.
1593
1594@comment termios.h
1595@comment POSIX.1
1596@deftypevr Macro int VMIN
1597@cindex MIN termios slot
1598This 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.
1600
1601The MIN slot is only meaningful in noncanonical input mode; it
1602specifies the minimum number of bytes that must be available in the
1603input queue in order for @code{read} to return.
1604@end deftypevr
1605
1606@comment termios.h
1607@comment POSIX.1
1608@deftypevr Macro int VTIME
1609@cindex TIME termios slot
1610This 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.
1612
1613The TIME slot is only meaningful in noncanonical input mode; it
1614specifies how long to wait for input before returning, in units of 0.1
1615seconds.
1616@end deftypevr
1617
1618The MIN and TIME values interact to determine the criterion for when
1619@code{read} should return; their precise meanings depend on which of
1620them are nonzero. There are four possible cases:
1621
1622@itemize @bullet
1623@item
1624Both TIME and MIN are nonzero.
1625
1626In this case, TIME specifies how long to wait after each input character
1627to see if more input arrives. After the first character received,
1628@code{read} keeps waiting until either MIN bytes have arrived in all, or
1629TIME elapses with no further input.
1630
1631@code{read} always blocks until the first character arrives, even if
1632TIME elapses first. @code{read} can return more than MIN characters if
1633more than MIN happen to be in the queue.
1634
1635@item
1636Both MIN and TIME are zero.
1637
1638In this case, @code{read} always returns immediately with as many
1639characters as are available in the queue, up to the number requested.
1640If no input is immediately available, @code{read} returns a value of
1641zero.
1642
1643@item
1644MIN is zero but TIME has a nonzero value.
1645
1646In this case, @code{read} waits for time TIME for input to become
1647available; the availability of a single byte is enough to satisfy the
1648read request and cause @code{read} to return. When it returns, it
1649returns as many characters as are available, up to the number requested.
1650If no input is available before the timer expires, @code{read} returns a
1651value of zero.
1652
1653@item
1654TIME is zero but MIN has a nonzero value.
1655
1656In this case, @code{read} waits until at least MIN bytes are available
1657in the queue. At that time, @code{read} returns as many characters as
1658are available, up to the number requested. @code{read} can return more
1659than MIN characters if more than MIN happen to be in the queue.
1660@end itemize
1661
1662What happens if MIN is 50 and you ask to read just 10 bytes?
1663Normally, @code{read} waits until there are 50 bytes in the buffer (or,
1664more generally, the wait condition described above is satisfied), and
1665then reads 10 of them, leaving the other 40 buffered in the operating
1666system for a subsequent call to @code{read}.
1667
1668@strong{Portability note:} On some systems, the MIN and TIME slots are
1669actually the same as the EOF and EOL slots. This causes no serious
1670problem because the MIN and TIME slots are used only in noncanonical
1671input and the EOF and EOL slots are used only in canonical input, but it
1672isn't very clean. @Theglibc{} allocates separate slots for these
1673uses.
1674
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.
1682This function provides an easy way to set up @code{*@var{termios-p}} for
1683what has traditionally been called ``raw mode'' in BSD. This uses
1684noncanonical input, and turns off most processing to give an unmodified
1685channel to the terminal.
1686
1687It 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
1697
1698
1699@node BSD Terminal Modes
1700@section BSD Terminal Modes
1701@cindex terminal modes, BSD
1702
1703The usual way to get and set terminal modes is with the functions described
1704in @ref{Terminal Modes}. However, on some systems you can use the
1705BSD-derived functions in this section to do some of the same thing. On
1706many systems, these functions do not exist. Even with @theglibc{},
1707the functions simply fail with @code{errno} = @code{ENOSYS} with many
1708kernels, including Linux.
1709
1710The symbols used in this section are declared in @file{sgtty.h}.
1711
1712@comment termios.h
1713@comment BSD
1714@deftp {Data Type} {struct sgttyb}
1715This structure is an input or output parameter list for @code{gtty} and
1716@code{stty}.
1717
1718@table @code
1719@item char sg_ispeed
1720Line speed for input
1721@item char sg_ospeed
1722Line speed for output
1723@item char sg_erase
1724Erase character
1725@item char sg_kill
1726Kill character
1727@item int sg_flags
1728Various flags
1729@end table
1730@end deftp
1731
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.
1737This function gets the attributes of a terminal.
1738
1739@code{gtty} sets *@var{attributes} to describe the terminal attributes
1740of the terminal which is open with file descriptor @var{filedes}.
1741@end deftypefun
1742
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.
1748
1749This function sets the attributes of a terminal.
1750
1751@code{stty} sets the terminal attributes of the terminal which is open with
1752file descriptor @var{filedes} to those described by *@var{filedes}.
1753@end deftypefun
1754
1755@node Line Control
1756@section Line Control Functions
1757@cindex terminal line control functions
1758
1759These functions perform miscellaneous control actions on terminal
1760devices. As regards terminal access, they are treated like doing
1761output: if any of these functions is used by a background process on its
1762controlling terminal, normally all processes in the process group are
1763sent a @code{SIGTTOU} signal. The exception is if the calling process
1764itself is ignoring or blocking @code{SIGTTOU} signals, in which case the
1765operation is performed and no signal is sent. @xref{Job Control}.
1766
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.
1777This function generates a break condition by transmitting a stream of
1778zero 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
1781seconds. The meaning of a nonzero value depends on the operating system.
1782
1783This function does nothing if the terminal is not an asynchronous serial
1784data port.
1785
1786The return value is normally zero. In the event of an error, a value
1787of @math{-1} is returned. The following @code{errno} error conditions
1788are defined for this function:
1789
1790@table @code
1791@item EBADF
1792The @var{filedes} is not a valid file descriptor.
1793
1794@item ENOTTY
1795The @var{filedes} is not associated with a terminal device.
1796@end table
1797@end deftypefun
1798
1799
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.
1807The @code{tcdrain} function waits until all queued
1808output to the terminal @var{filedes} has been transmitted.
1809
1810This function is a cancellation point in multi-threaded programs. This
1811is a problem if the thread allocates some resources (like memory, file
1812descriptors, semaphores or whatever) at the time @code{tcdrain} is
1813called. If the thread gets canceled these resources stay allocated
1814until the program ends. To avoid this calls to @code{tcdrain} should be
1815protected using cancellation handlers.
1816@c ref pthread_cleanup_push / pthread_cleanup_pop
1817
1818The return value is normally zero. In the event of an error, a value
1819of @math{-1} is returned. The following @code{errno} error conditions
1820are defined for this function:
1821
1822@table @code
1823@item EBADF
1824The @var{filedes} is not a valid file descriptor.
1825
1826@item ENOTTY
1827The @var{filedes} is not associated with a terminal device.
1828
1829@item EINTR
1830The operation was interrupted by delivery of a signal.
1831@xref{Interrupted Primitives}.
1832@end table
1833@end deftypefun
1834
1835
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.
1843The @code{tcflush} function is used to clear the input and/or output
1844queues associated with the terminal file @var{filedes}. The @var{queue}
1845argument specifies which queue(s) to clear, and can be one of the
1846following values:
1847
1848@c Extra blank lines here make it look better.
1849@table @code
1850@vindex TCIFLUSH
1851@item TCIFLUSH
1852
1853Clear any input data received, but not yet read.
1854
1855@vindex TCOFLUSH
1856@item TCOFLUSH
1857
1858Clear any output data written, but not yet transmitted.
1859
1860@vindex TCIOFLUSH
1861@item TCIOFLUSH
1862
1863Clear both queued input and output.
1864@end table
1865
1866The return value is normally zero. In the event of an error, a value
1867of @math{-1} is returned. The following @code{errno} error conditions
1868are defined for this function:
1869
1870@table @code
1871@item EBADF
1872The @var{filedes} is not a valid file descriptor.
1873
1874@item ENOTTY
1875The @var{filedes} is not associated with a terminal device.
1876
1877@item EINVAL
1878A bad value was supplied as the @var{queue} argument.
1879@end table
1880
1881It is unfortunate that this function is named @code{tcflush}, because
1882the term ``flush'' is normally used for quite another operation---waiting
1883until all output is transmitted---and using it for discarding input or
1884output would be confusing. Unfortunately, the name @code{tcflush} comes
1885from POSIX and we cannot change it.
1886@end deftypefun
1887
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.
1898The @code{tcflow} function is used to perform operations relating to
1899XON/XOFF flow control on the terminal file specified by @var{filedes}.
1900
1901The @var{action} argument specifies what operation to perform, and can
1902be one of the following values:
1903
1904@table @code
1905@vindex TCOOFF
1906@item TCOOFF
1907Suspend transmission of output.
1908
1909@vindex TCOON
1910@item TCOON
1911Restart transmission of output.
1912
1913@vindex TCIOFF
1914@item TCIOFF
1915Transmit a STOP character.
1916
1917@vindex TCION
1918@item TCION
1919Transmit a START character.
1920@end table
1921
1922For more information about the STOP and START characters, see @ref{Special
1923Characters}.
1924
1925The return value is normally zero. In the event of an error, a value
1926of @math{-1} is returned. The following @code{errno} error conditions
1927are defined for this function:
1928
1929@table @code
1930@vindex EBADF
1931@item EBADF
1932The @var{filedes} is not a valid file descriptor.
1933
1934@vindex ENOTTY
1935@item ENOTTY
1936The @var{filedes} is not associated with a terminal device.
1937
1938@vindex EINVAL
1939@item EINVAL
1940A bad value was supplied as the @var{action} argument.
1941@end table
1942@end deftypefun
1943
1944@node Noncanon Example
1945@section Noncanonical Mode Example
1946
1947Here is an example program that shows how you can set up a terminal
1948device to read single characters in noncanonical input mode, without
1949echo.
1950
1951@smallexample
1952@include termios.c.texi
1953@end smallexample
1954
1955This program is careful to restore the original terminal modes before
1956exiting or terminating with a signal. It uses the @code{atexit}
1957function (@pxref{Cleanups on Exit}) to make sure this is done
1958by @code{exit}.
1959
1960@ignore
1961@c !!!! the example doesn't handle any signals!
1962The signals handled in the example are the ones that typically occur due
1963to actions of the user. It might be desirable to handle other signals
1964such as SIGSEGV that can result from bugs in the program.
1965@end ignore
1966
1967The shell is supposed to take care of resetting the terminal modes when
1968a process is stopped or continued; see @ref{Job Control}. But some
1969existing shells do not actually do this, so you may wish to establish
1970handlers for job control signals that reset terminal modes. The above
1971example does so.
1972
1973
1974@node Pseudo-Terminals
1975@section Pseudo-Terminals
1976@cindex pseudo-terminals
1977
1978A @dfn{pseudo-terminal} is a special interprocess communication channel
1979that 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
1981is called the @dfn{slave} side. Data written to the master side is
1982received by the slave side as if it was the result of a user typing at
1983an ordinary terminal, and data written to the slave side is sent to the
1984master side as if it was written on an ordinary terminal.
1985
1986Pseudo terminals are the way programs like @code{xterm} and @code{emacs}
1987implement their terminal emulation functionality.
1988
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
1994
1995@node Allocation
1996@subsection Allocating Pseudo-Terminals
1997@cindex allocating pseudo-terminals
1998
1999@pindex stdlib.h
2000This subsection describes functions for allocating a pseudo-terminal,
2001and for making this pseudo-terminal available for actual use. These
2002functions are declared in the header file @file{stdlib.h}.
2003
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.
2015The @code{getpt} function returns a new file descriptor for the next
2016available master pseudo-terminal. The normal return value from
2017@code{getpt} is a non-negative integer file descriptor. In the case of
2018an error, a value of @math{-1} is returned instead. The following
2019@code{errno} conditions are defined for this function:
2020
2021@table @code
2022@item ENOENT
2023There are no free master pseudo-terminals available.
2024@end table
2025
2026This function is a GNU extension.
2027@end deftypefun
2028
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
2058The @code{grantpt} function changes the ownership and access permission
2059of the slave pseudo-terminal device corresponding to the master
2060pseudo-terminal device associated with the file descriptor
2061@var{filedes}. The owner is set from the real user ID of the calling
2062process (@pxref{Process Persona}), and the group is set to a special
2063group (typically @dfn{tty}) or from the real group ID of the calling
2064process. The access permission is set such that the file is both
2065readable and writable by the owner and only writable by the group.
2066
2067On some systems this function is implemented by invoking a special
2068@code{setuid} root program (@pxref{How Change Persona}). As a
2069consequence, installing a signal handler for the @code{SIGCHLD} signal
2070(@pxref{Job Control Signals}) may interfere with a call to
2071@code{grantpt}.
2072
2073The 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}
2075error conditions are defined for this function:
2076
2077@table @code
2078@item EBADF
2079The @var{filedes} argument is not a valid file descriptor.
2080
2081@item EINVAL
2082The @var{filedes} argument is not associated with a master pseudo-terminal
2083device.
2084
2085@item EACCES
2086The slave pseudo-terminal device corresponding to the master associated
2087with @var{filedes} could not be accessed.
2088@end table
2089
2090@end deftypefun
2091
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
2102The @code{unlockpt} function unlocks the slave pseudo-terminal device
2103corresponding to the master pseudo-terminal device associated with the
2104file descriptor @var{filedes}. On many systems, the slave can only be
2105opened after unlocking, so portable applications should always call
2106@code{unlockpt} before trying to open the slave.
2107
2108The 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}
2110error conditions are defined for this function:
2111
2112@table @code
2113@item EBADF
2114The @var{filedes} argument is not a valid file descriptor.
2115
2116@item EINVAL
2117The @var{filedes} argument is not associated with a master pseudo-terminal
2118device.
2119@end table
2120@end deftypefun
2121
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
2128If the file descriptor @var{filedes} is associated with a
2129master pseudo-terminal device, the @code{ptsname} function returns a
2130pointer to a statically-allocated, null-terminated string containing the
2131file name of the associated slave pseudo-terminal file. This string
2132might be overwritten by subsequent calls to @code{ptsname}.
2133@end deftypefun
2134
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
2169The @code{ptsname_r} function is similar to the @code{ptsname} function
2170except that it places its result into the user-specified buffer starting
2171at @var{buf} with length @var{len}.
2172
2173This function is a GNU extension.
2174@end deftypefun
2175
2176@strong{Portability Note:} On @w{System V} derived systems, the file
2177returned by the @code{ptsname} and @code{ptsname_r} functions may be
2178STREAMS-based, and therefore require additional processing after opening
2179before it actually behaves as a pseudo terminal.
2180@c FIXME: xref STREAMS
2181
2182Typical usage of these functions is illustrated by the following example:
2183@smallexample
2184int
2185open_pty_pair (int *amaster, int *aslave)
2186@{
2187 int master, slave;
2188 char *name;
2189
2190 master = getpt ();
2191 if (master < 0)
2192 return 0;
2193
2194 if (grantpt (master) < 0 || unlockpt (master) < 0)
2195 goto close_master;
2196 name = ptsname (master);
2197 if (name == NULL)
2198 goto close_master;
2199
2200 slave = open (name, O_RDWR);
2201 if (slave == -1)
2202 goto close_master;
2203
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 @}
2210
2211 *amaster = master;
2212 *aslave = slave;
2213 return 1;
2214
2215close_slave:
2216 close (slave);
2217
2218close_master:
2219 close (master);
2220 return 0;
2221@}
2222@end smallexample
2223
2224@node Pseudo-Terminal Pairs
2225@subsection Opening a Pseudo-Terminal Pair
2226@cindex opening a pseudo-terminal pair
2227
2228These functions, derived from BSD, are available in the separate
2229@file{libutil} library, and declared in @file{pty.h}.
2230
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
2250This function allocates and opens a pseudo-terminal pair, returning the
2251file descriptor for the master in @var{*amaster}, and the file
2252descriptor for the slave in @var{*aslave}. If the argument @var{name}
2253is not a null pointer, the file name of the slave pseudo-terminal
2254device is stored in @code{*name}. If @var{termp} is not a null pointer,
2255the terminal attributes of the slave are set to the ones specified in
2256the structure that @var{termp} points to (@pxref{Terminal Modes}).
2257Likewise, if the @var{winp} is not a null pointer, the screen size of
2258the slave is set to the values specified in the structure that
2259@var{winp} points to.
2260
2261The 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}
2263conditions are defined for this function:
2264
2265@table @code
2266@item ENOENT
2267There are no free pseudo-terminal pairs available.
2268@end table
2269
2270@strong{Warning:} Using the @code{openpty} function with @var{name} not
2271set to @code{NULL} is @strong{very dangerous} because it provides no
2272protection against overflowing the string @var{name}. You should use
2273the @code{ttyname} function on the file descriptor returned in
2274@var{*slave} to find out the file name of the slave pseudo-terminal
2275device instead.
2276@end deftypefun
2277
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
2291This function is similar to the @code{openpty} function, but in
2292addition, forks a new process (@pxref{Creating a Process}) and makes the
2293newly opened slave pseudo-terminal device the controlling terminal
2294(@pxref{Controlling Terminal}) for the child process.
2295
2296If the operation is successful, there are then both parent and child
2297processes and both see @code{forkpty} return, but with different values:
2298it returns a value of @math{0} in the child process and returns the child's
2299process ID in the parent process.
2300
2301If the allocation of a pseudo-terminal pair or the process creation
2302failed, @code{forkpty} returns a value of @math{-1} in the parent
2303process.
2304
2305@strong{Warning:} The @code{forkpty} function has the same problems with
2306respect to the @var{name} argument as @code{openpty}.
2307@end deftypefun