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