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