xref: /freebsd/lib/libc/gen/tcsetattr.3 (revision 952d112864d8008aa87278a30a539d888a8493cd)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)tcsetattr.3	8.3 (Berkeley) 1/2/94
33.\"
34.Dd "January 2, 1994"
35.Dt TCSETATTR 3
36.Os
37.Sh NAME
38.Nm cfgetispeed ,
39.Nm cfsetispeed ,
40.Nm cfgetospeed ,
41.Nm cfsetospeed ,
42.Nm cfsetspeed ,
43.Nm cfmakeraw ,
44.Nm tcgetattr ,
45.Nm tcsetattr
46.Nd manipulating the termios structure
47.Sh SYNOPSIS
48.Fd #include <termios.h>
49.Ft speed_t
50.Fn cfgetispeed "const struct termios *t"
51.Ft int
52.Fn cfsetispeed "struct termios *t" "speed_t speed"
53.Ft speed_t
54.Fn cfgetospeed "const struct termios *t"
55.Ft int
56.Fn cfsetospeed "struct termios *t" "speed_t speed"
57.Ft int
58.Fn cfsetspeed "struct termios *t" "speed_t speed"
59.Ft void
60.Fn cfmakeraw "struct termios *t"
61.Ft int
62.Fn tcgetattr "int fd" "struct termios *t"
63.Ft int
64.Fn tcsetattr "int fd" "int action" "const struct termios *t"
65.Sh DESCRIPTION
66The
67.Fn cfmakeraw ,
68.Fn tcgetattr
69and
70.Fn tcsetattr
71functions are provided for getting and setting the termios structure.
72.Pp
73The
74.Fn cfgetispeed ,
75.Fn cfsetispeed ,
76.Fn cfgetospeed ,
77.Fn cfsetospeed
78and
79.Fn cfsetspeed
80functions are provided for getting and setting the baud rate values in
81the termios structure.
82The effects of the functions on the terminal as described below
83do not become effective, nor are all errors detected, until the
84.Fn tcsetattr
85function is called.
86Certain values for baud rates set in the termios structure and passed to
87.Fn tcsetattr
88have special meanings.
89These are discussed in the portion of the manual page that describes the
90.Fn tcsetattr
91function.
92.Sh GETTING AND SETTING THE BAUD RATE
93The input and output baud rates are found in the termios structure.
94The unsigned integer
95.Li speed_t
96is typdef'd in the include file
97.Aq Pa termios.h .
98The value of the integer corresponds directly to the baud rate being
99represented, however, the following symbolic values are defined.
100.Bd -literal
101#define B0	0
102#define B50	50
103#define B75	75
104#define B110	110
105#define B134	134
106#define B150	150
107#define B200	200
108#define B300	300
109#define B600	600
110#define B1200	1200
111#define	B1800	1800
112#define B2400	2400
113#define B4800	4800
114#define B9600	9600
115#define B19200	19200
116#define B38400	38400
117#ifndef _POSIX_SOURCE
118#define EXTA	19200
119#define EXTB	38400
120#endif  /*_POSIX_SOURCE */
121.Ed
122.Pp
123The
124.Fn cfgetispeed
125function returns the input baud rate in the termios structure referenced by
126.Fa tp .
127.Pp
128The
129.Fn cfsetispeed
130function sets the input baud rate in the termios structure referenced by
131.Fa tp
132to
133.Fa speed .
134.Pp
135The
136.Fn cfgetospeed
137function returns the output baud rate in the termios structure referenced by
138.Fa tp .
139.Pp
140The
141.Fn cfsetospeed
142function sets the output baud rate in the termios structure referenced by
143.Fa tp
144to
145.Fa speed .
146.Pp
147The
148.Fn cfsetspeed
149function sets both the input and output baud rate in the termios structure
150referenced by
151.Fa tp
152to
153.Fa speed .
154.Pp
155Upon successful completion, the functions
156.Fn cfsetispeed ,
157.Fn cfsetospeed ,
158and
159.Fn cfsetspeed
160return a value of 0.
161Otherwise, a value of -1 is returned and the global variable
162.Va errno
163is set to indicate the error.
164.Sh GETTING AND SETTING THE TERMIOS STATE
165This section describes the functions that are used to control the general
166terminal interface.
167Unless otherwise noted for a specific command, these functions are restricted
168from use by background processes.
169Attempts to perform these operations shall cause the process group to be sent
170a SIGTTOU signal.
171If the calling process is blocking or ignoring SIGTTOU signals, the process
172is allowed to perform the operation and the SIGTTOU signal is not sent.
173.Pp
174In all the functions, although
175.Fa fd
176is an open file descriptor, the functions affect the underlying terminal
177file, not just the open file description associated with the particular
178file descriptor.
179.Pp
180The
181.Fn cfmakeraw
182function sets the flags stored in the termios structure to a state disabling
183all input and output processing, giving a
184.Dq raw I/O path.
185It should be noted that there is no function to reverse this effect.
186This is because there are a variety of processing options that could be
187re-enabled and the correct method is for an application to snapshot the
188current terminal state using the function
189.Fn tcgetattr ,
190setting raw mode with
191.Fn cfmakeraw
192and the subsequent
193.Fn tcsetattr ,
194and then using another
195.Fn tcsetattr
196with the saved state to revert to the previous terminal state.
197.Pp
198The
199.Fn tcgetattr
200function copies the parameters associated with the terminal referenced
201by
202.Fa fd
203in the termios structure referenced by
204.Fa tp .
205This function is allowed from a background process, however, the terminal
206attributes may be subsequently changed by a foreground process.
207.Pp
208The
209.Fn tcsetattr
210function sets the parameters associated with the terminal from the
211termios structure referenced by
212.Fa tp .
213The
214.Fa action
215field is created by
216.Em or Ns 'ing
217the following values, as specified in the include file
218.Aq Pa termios.h .
219.Bl -tag -width "TCSADRAIN"
220.It Fa TCSANOW
221The change occurs immediately.
222.It Fa TCSADRAIN
223The change occurs after all output written to
224.Fa fd
225has been transmitted to the terminal.
226This value of
227.Fa action
228should be used when changing parameters that affect output.
229.It Fa TCSAFLUSH
230The change occurs after all output written to
231.Fa fd
232has been transmitted to the terminal.
233Additionally, any input that has been received but not read is discarded.
234.It Fa TCSASOFT
235If this value is
236.Em or Ns 'ed
237into the
238.Fa action
239value, the values of the
240.Em c_cflag ,
241.Em c_ispeed ,
242and
243.Em c_ospeed
244fields are ignored.
245.El
246.Pp
247The 0 baud rate is used to terminate the connection.
248If 0 is specified as the output speed to the function
249.Fn tcsetattr ,
250modem control will no longer be asserted on the terminal, disconnecting
251the terminal.
252.Pp
253If zero is specified as the input speed to the function
254.Fn tcsetattr ,
255the input baud rate will be set to the same value as that specified by
256the output baud rate.
257.Pp
258If
259.Fn tcsetattr
260is unable to make any of the requested changes, it returns -1 and
261sets errno.
262Otherwise, it makes all of the requested changes it can.
263If the specified input and output baud rates differ and are a combination
264that is not supported, neither baud rate is changed.
265.Pp
266Upon successful completion, the functions
267.Fn tcgetattr
268and
269.Fn tcsetattr
270return a value of 0.
271Otherwise, they
272return -1 and the global variable
273.Va errno
274is set to indicate the error, as follows:
275.Bl -tag -width Er
276.It Bq Er EBADF
277The
278.Fa fd
279argument to
280.Fn tcgetattr
281or
282.Fn tcsetattr
283was not a valid file descriptor.
284.It Bq Er EINTR
285The
286.Fn tcsetattr
287function was interrupted by a signal.
288.It Bq Er EINVAL
289The
290.Fa action
291argument to the
292.Fn tcsetattr
293function was not valid, or an attempt was made to change an attribute
294represented in the termios structure to an unsupported value.
295.It Bq Er ENOTTY
296The file associated with the
297.Fa fd
298argument to
299.Fn tcgetattr
300or
301.Fn tcsetattr
302is not a terminal.
303.El
304.Sh SEE ALSO
305.Xr tcsendbreak 3 ,
306.Xr termios 4
307.Sh STANDARDS
308The
309.Fn cfgetispeed ,
310.Fn cfsetispeed ,
311.Fn cfgetospeed ,
312.Fn cfsetospeed ,
313.Fn tcgetattr
314and
315.Fn tcsetattr
316functions are expected to be compliant with the
317.St -p1003.1-88
318specification.
319The
320.Fn cfmakeraw
321and
322.Fn cfsetspeed
323functions,
324as well as the
325.Li TCSASOFT
326option to the
327.Fn tcsetattr
328function are extensions to the
329.St -p1003.1-88
330specification.
331