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