rustix/termios/tc.rs
1use crate::fd::AsFd;
2#[cfg(not(target_os = "espidf"))]
3use crate::termios::{Action, OptionalActions, QueueSelector, Termios, Winsize};
4use crate::{backend, io};
5
6pub use crate::pid::Pid;
7
8/// `tcgetattr(fd)`—Get terminal attributes.
9///
10/// Also known as the `TCGETS` (or `TCGETS2` on Linux) operation with `ioctl`.
11///
12/// On Linux, this uses `TCGETS2`. If that fails in a way that indicates that
13/// the host doesn't support it, this falls back to the old `TCGETS`, manually
14/// initializes the fields that `TCGETS` doesn't initialize, and fails with
15/// `io::Errno::RANGE` if the input or output speeds cannot be supported.
16///
17/// # References
18/// - [POSIX `tcgetattr`]
19/// - [Linux `ioctl_tty`]
20/// - [Linux `termios`]
21///
22/// [POSIX `tcgetattr`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcgetattr.html
23/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
24/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
25#[cfg(not(any(windows, target_os = "espidf", target_os = "wasi")))]
26#[inline]
27#[doc(alias = "TCGETS")]
28#[doc(alias = "TCGETS2")]
29#[doc(alias = "tcgetattr2")]
30pub fn tcgetattr<Fd: AsFd>(fd: Fd) -> io::Result<Termios> {
31 backend::termios::syscalls::tcgetattr(fd.as_fd())
32}
33
34/// `tcgetwinsize(fd)`—Get the current terminal window size.
35///
36/// Also known as the `TIOCGWINSZ` operation with `ioctl`.
37///
38/// # References
39/// - [Linux]
40///
41/// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
42#[cfg(not(any(
43 windows,
44 target_os = "horizon",
45 target_os = "espidf",
46 target_os = "wasi"
47)))]
48#[inline]
49#[doc(alias = "TIOCGWINSZ")]
50pub fn tcgetwinsize<Fd: AsFd>(fd: Fd) -> io::Result<Winsize> {
51 backend::termios::syscalls::tcgetwinsize(fd.as_fd())
52}
53
54/// `tcgetpgrp(fd)`—Get the terminal foreground process group.
55///
56/// Also known as the `TIOCGPGRP` operation with `ioctl`.
57///
58/// On Linux, if `fd` is a pseudo-terminal, the underlying system call here can
59/// return a pid of 0, which rustix's `Pid` type doesn't support. So rustix
60/// instead handles this case by failing with [`io::Errno::OPNOTSUPP`] if the
61/// pid is 0.
62///
63/// # References
64/// - [POSIX]
65/// - [Linux]
66///
67/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcgetpgrp.html
68/// [Linux]: https://man7.org/linux/man-pages/man3/tcgetpgrp.3.html
69#[cfg(not(any(windows, target_os = "wasi")))]
70#[inline]
71#[doc(alias = "TIOCGPGRP")]
72pub fn tcgetpgrp<Fd: AsFd>(fd: Fd) -> io::Result<Pid> {
73 backend::termios::syscalls::tcgetpgrp(fd.as_fd())
74}
75
76/// `tcsetpgrp(fd, pid)`—Set the terminal foreground process group.
77///
78/// Also known as the `TIOCSPGRP` operation with `ioctl`.
79///
80/// # References
81/// - [POSIX]
82/// - [Linux]
83///
84/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcsetpgrp.html
85/// [Linux]: https://man7.org/linux/man-pages/man3/tcsetpgrp.3.html
86#[cfg(not(any(windows, target_os = "wasi")))]
87#[inline]
88#[doc(alias = "TIOCSPGRP")]
89pub fn tcsetpgrp<Fd: AsFd>(fd: Fd, pid: Pid) -> io::Result<()> {
90 backend::termios::syscalls::tcsetpgrp(fd.as_fd(), pid)
91}
92
93/// `tcsetattr(fd)`—Set terminal attributes.
94///
95/// Also known as the `TCSETS` (or `TCSETS2` on Linux) operation with `ioctl`.
96///
97/// On Linux, this uses `TCSETS2`. If that fails in a way that indicates that
98/// the host doesn't support it, this falls back to the old `TCSETS`, and fails
99/// with `io::Errno::RANGE` if the input or output speeds cannot be supported.
100///
101/// # References
102/// - [POSIX `tcsetattr`]
103/// - [Linux `ioctl_tty`]
104/// - [Linux `termios`]
105///
106/// [POSIX `tcsetattr`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcsetattr.html
107/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
108/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
109#[cfg(not(target_os = "espidf"))]
110#[inline]
111#[doc(alias = "TCSETS")]
112#[doc(alias = "TCSETS2")]
113#[doc(alias = "tcsetattr2")]
114pub fn tcsetattr<Fd: AsFd>(
115 fd: Fd,
116 optional_actions: OptionalActions,
117 termios: &Termios,
118) -> io::Result<()> {
119 backend::termios::syscalls::tcsetattr(fd.as_fd(), optional_actions, termios)
120}
121
122/// `tcsendbreak(fd, 0)`—Transmit zero-valued bits.
123///
124/// This transmits zero-valued bits for at least 0.25 seconds.
125///
126/// This function does not have a `duration` parameter, and always uses the
127/// implementation-defined value, which transmits for at least 0.25 seconds.
128///
129/// Also known as the `TCSBRK` operation with `ioctl`, with a duration
130/// parameter of 0.
131///
132/// # References
133/// - [POSIX `tcsendbreak`]
134/// - [Linux `ioctl_tty`]
135/// - [Linux `termios`]
136///
137/// [POSIX `tcsendbreak`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcsendbreak.html
138/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
139/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
140#[inline]
141#[doc(alias = "TCSBRK")]
142pub fn tcsendbreak<Fd: AsFd>(fd: Fd) -> io::Result<()> {
143 backend::termios::syscalls::tcsendbreak(fd.as_fd())
144}
145
146/// `tcdrain(fd, duration)`—Wait until all pending output has been written.
147///
148/// # References
149/// - [POSIX `tcdrain`]
150/// - [Linux `ioctl_tty`]
151/// - [Linux `termios`]
152///
153/// [POSIX `tcsetattr`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcdrain.html
154/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
155/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
156#[cfg(not(target_os = "espidf"))]
157#[inline]
158pub fn tcdrain<Fd: AsFd>(fd: Fd) -> io::Result<()> {
159 backend::termios::syscalls::tcdrain(fd.as_fd())
160}
161
162/// `tcflush(fd, queue_selector)`—Wait until all pending output has been
163/// written.
164///
165/// # References
166/// - [POSIX `tcflush`]
167/// - [Linux `ioctl_tty`]
168/// - [Linux `termios`]
169///
170/// [POSIX `tcflush`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcflush.html
171/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
172/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
173#[cfg(not(target_os = "espidf"))]
174#[inline]
175#[doc(alias = "TCFLSH")]
176pub fn tcflush<Fd: AsFd>(fd: Fd, queue_selector: QueueSelector) -> io::Result<()> {
177 backend::termios::syscalls::tcflush(fd.as_fd(), queue_selector)
178}
179
180/// `tcflow(fd, action)`—Suspend or resume transmission or reception.
181///
182/// # References
183/// - [POSIX `tcflow`]
184/// - [Linux `ioctl_tty`]
185/// - [Linux `termios`]
186///
187/// [POSIX `tcflow`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcflow.html
188/// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
189/// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
190#[cfg(not(target_os = "espidf"))]
191#[inline]
192#[doc(alias = "TCXONC")]
193pub fn tcflow<Fd: AsFd>(fd: Fd, action: Action) -> io::Result<()> {
194 backend::termios::syscalls::tcflow(fd.as_fd(), action)
195}
196
197/// `tcgetsid(fd)`—Return the session ID of the current session with `fd` as
198/// its controlling terminal.
199///
200/// # References
201/// - [POSIX]
202/// - [Linux]
203///
204/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/tcgetsid.html
205/// [Linux]: https://man7.org/linux/man-pages/man3/tcgetsid.3.html
206#[inline]
207#[doc(alias = "TIOCGSID")]
208pub fn tcgetsid<Fd: AsFd>(fd: Fd) -> io::Result<Pid> {
209 backend::termios::syscalls::tcgetsid(fd.as_fd())
210}
211
212/// `tcsetwinsize(fd)`—Set the current terminal window size.
213///
214/// Also known as the `TIOCSWINSZ` operation with `ioctl`.
215///
216/// # References
217/// - [Linux]
218///
219/// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
220#[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
221#[inline]
222#[doc(alias = "TIOCSWINSZ")]
223pub fn tcsetwinsize<Fd: AsFd>(fd: Fd, winsize: Winsize) -> io::Result<()> {
224 backend::termios::syscalls::tcsetwinsize(fd.as_fd(), winsize)
225}