1 use crate::fd::AsFd;
2 #[cfg(not(target_os = "espidf"))]
3 use crate::termios::{Action, OptionalActions, QueueSelector, Termios, Winsize};
4 use crate::{backend, io};
5
6 pub 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")]
tcgetattr<Fd: AsFd>(fd: Fd) -> io::Result<Termios>25 pub 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")]
tcgetwinsize<Fd: AsFd>(fd: Fd) -> io::Result<Winsize>40 pub 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")]
tcgetpgrp<Fd: AsFd>(fd: Fd) -> io::Result<Pid>62 pub 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")]
tcsetpgrp<Fd: AsFd>(fd: Fd, pid: Pid) -> io::Result<()>79 pub 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")]
tcsetattr<Fd: AsFd>( fd: Fd, optional_actions: OptionalActions, termios: &Termios, ) -> io::Result<()>100 pub 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 /// Also known as the `TCSBRK` operation with `ioctl`, with a duration of 0.
111 ///
112 /// This function always uses an effective duration parameter of zero. For the
113 /// equivalent of a `tcsendbreak` with a non-zero duration parameter, use
114 /// `tcdrain`.
115 ///
116 /// # References
117 /// - [POSIX `tcsendbreak`]
118 /// - [Linux `ioctl_tty`]
119 /// - [Linux `termios`]
120 ///
121 /// [POSIX `tcsendbreak`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcsendbreak.html
122 /// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
123 /// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
124 #[inline]
125 #[doc(alias = "TCSBRK")]
tcsendbreak<Fd: AsFd>(fd: Fd) -> io::Result<()>126 pub fn tcsendbreak<Fd: AsFd>(fd: Fd) -> io::Result<()> {
127 backend::termios::syscalls::tcsendbreak(fd.as_fd())
128 }
129
130 /// `tcdrain(fd, duration)`—Wait until all pending output has been written.
131 ///
132 /// # References
133 /// - [POSIX `tcdrain`]
134 /// - [Linux `ioctl_tty`]
135 /// - [Linux `termios`]
136 ///
137 /// [POSIX `tcsetattr`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcdrain.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 #[cfg(not(target_os = "espidf"))]
141 #[inline]
tcdrain<Fd: AsFd>(fd: Fd) -> io::Result<()>142 pub fn tcdrain<Fd: AsFd>(fd: Fd) -> io::Result<()> {
143 backend::termios::syscalls::tcdrain(fd.as_fd())
144 }
145
146 /// `tcflush(fd, queue_selector)`—Wait until all pending output has been
147 /// written.
148 ///
149 /// # References
150 /// - [POSIX `tcflush`]
151 /// - [Linux `ioctl_tty`]
152 /// - [Linux `termios`]
153 ///
154 /// [POSIX `tcflush`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcflush.html
155 /// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
156 /// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
157 #[cfg(not(target_os = "espidf"))]
158 #[inline]
159 #[doc(alias = "TCFLSH")]
tcflush<Fd: AsFd>(fd: Fd, queue_selector: QueueSelector) -> io::Result<()>160 pub fn tcflush<Fd: AsFd>(fd: Fd, queue_selector: QueueSelector) -> io::Result<()> {
161 backend::termios::syscalls::tcflush(fd.as_fd(), queue_selector)
162 }
163
164 /// `tcflow(fd, action)`—Suspend or resume transmission or reception.
165 ///
166 /// # References
167 /// - [POSIX `tcflow`]
168 /// - [Linux `ioctl_tty`]
169 /// - [Linux `termios`]
170 ///
171 /// [POSIX `tcflow`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcflow.html
172 /// [Linux `ioctl_tty`]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
173 /// [Linux `termios`]: https://man7.org/linux/man-pages/man3/termios.3.html
174 #[cfg(not(target_os = "espidf"))]
175 #[inline]
176 #[doc(alias = "TCXONC")]
tcflow<Fd: AsFd>(fd: Fd, action: Action) -> io::Result<()>177 pub fn tcflow<Fd: AsFd>(fd: Fd, action: Action) -> io::Result<()> {
178 backend::termios::syscalls::tcflow(fd.as_fd(), action)
179 }
180
181 /// `tcgetsid(fd)`—Return the session ID of the current session with `fd` as
182 /// its controlling terminal.
183 ///
184 /// # References
185 /// - [POSIX]
186 /// - [Linux]
187 ///
188 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetsid.html
189 /// [Linux]: https://man7.org/linux/man-pages/man3/tcgetsid.3.html
190 #[inline]
191 #[doc(alias = "TIOCGSID")]
tcgetsid<Fd: AsFd>(fd: Fd) -> io::Result<Pid>192 pub fn tcgetsid<Fd: AsFd>(fd: Fd) -> io::Result<Pid> {
193 backend::termios::syscalls::tcgetsid(fd.as_fd())
194 }
195
196 /// `tcsetwinsize(fd)`—Set the current terminal window size.
197 ///
198 /// Also known as the `TIOCSWINSZ` operation with `ioctl`.
199 ///
200 /// # References
201 /// - [Linux]
202 ///
203 /// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
204 #[cfg(not(target_os = "espidf"))]
205 #[inline]
206 #[doc(alias = "TIOCSWINSZ")]
tcsetwinsize<Fd: AsFd>(fd: Fd, winsize: Winsize) -> io::Result<()>207 pub fn tcsetwinsize<Fd: AsFd>(fd: Fd, winsize: Winsize) -> io::Result<()> {
208 backend::termios::syscalls::tcsetwinsize(fd.as_fd(), winsize)
209 }
210