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