Skip to main content

rustpython_host_env/
termios.rs

1pub type Termios = ::termios::Termios;
2
3#[cfg(any(target_os = "illumos", target_os = "solaris"))]
4pub use libc::{CSTART, CSTOP, CSWTCH};
5
6#[cfg(any(
7    target_os = "dragonfly",
8    target_os = "freebsd",
9    target_os = "macos",
10    target_os = "netbsd",
11    target_os = "openbsd"
12))]
13pub use libc::{FIOASYNC, TIOCGETD, TIOCSETD};
14
15pub use libc::{FIOCLEX, FIONBIO, TIOCGWINSZ, TIOCSWINSZ};
16
17#[cfg(any(
18    target_os = "android",
19    target_os = "dragonfly",
20    target_os = "freebsd",
21    target_os = "linux",
22    target_os = "macos",
23    target_os = "netbsd",
24    target_os = "openbsd"
25))]
26pub use libc::{
27    FIONCLEX, FIONREAD, TIOCEXCL, TIOCM_CAR, TIOCM_CD, TIOCM_CTS, TIOCM_DSR, TIOCM_DTR, TIOCM_LE,
28    TIOCM_RI, TIOCM_RNG, TIOCM_RTS, TIOCM_SR, TIOCM_ST, TIOCMBIC, TIOCMBIS, TIOCMGET, TIOCMSET,
29    TIOCNXCL, TIOCSCTTY,
30};
31
32#[cfg(any(target_os = "android", target_os = "linux"))]
33pub use libc::{
34    IBSHIFT, TCFLSH, TCGETA, TCGETS, TCSBRK, TCSETA, TCSETAF, TCSETAW, TCSETS, TCSETSF, TCSETSW,
35    TCXONC, TIOCGSERIAL, TIOCGSOFTCAR, TIOCINQ, TIOCLINUX, TIOCSSOFTCAR, XTABS,
36};
37
38#[cfg(any(
39    target_os = "android",
40    target_os = "dragonfly",
41    target_os = "freebsd",
42    target_os = "linux",
43    target_os = "macos"
44))]
45pub use libc::{TIOCCONS, TIOCGPGRP, TIOCOUTQ, TIOCSPGRP, TIOCSTI};
46
47#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "macos"))]
48pub use libc::{
49    TIOCNOTTY, TIOCPKT, TIOCPKT_DATA, TIOCPKT_DOSTOP, TIOCPKT_FLUSHREAD, TIOCPKT_FLUSHWRITE,
50    TIOCPKT_NOSTOP, TIOCPKT_START, TIOCPKT_STOP,
51};
52
53#[cfg(target_os = "macos")]
54pub use libc::{
55    _POSIX_VDISABLE, ALTWERASE, B7200, B14400, B28800, B76800, CIGNORE, EXTPROC, IUTF8, MDMBUF,
56    NOKERNINFO, ONOEOT, OXTABS, VDSUSP, VSTATUS,
57};
58
59#[cfg(any(
60    target_os = "android",
61    target_os = "freebsd",
62    target_os = "illumos",
63    target_os = "linux",
64    target_os = "macos",
65    target_os = "openbsd",
66    target_os = "solaris"
67))]
68pub use ::termios::os::target::TAB3;
69#[cfg(any(
70    target_os = "dragonfly",
71    target_os = "freebsd",
72    target_os = "macos",
73    target_os = "netbsd",
74    target_os = "openbsd"
75))]
76pub use ::termios::os::target::TCSASOFT;
77#[cfg(any(
78    target_os = "android",
79    target_os = "freebsd",
80    target_os = "illumos",
81    target_os = "linux",
82    target_os = "netbsd",
83    target_os = "solaris"
84))]
85pub use ::termios::os::target::{B460800, B921600};
86#[cfg(any(target_os = "android", target_os = "linux"))]
87pub use ::termios::os::target::{
88    B500000, B576000, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000,
89    B4000000, CBAUDEX,
90};
91#[cfg(any(
92    target_os = "android",
93    target_os = "illumos",
94    target_os = "linux",
95    target_os = "macos",
96    target_os = "solaris"
97))]
98pub use ::termios::os::target::{
99    BS0, BS1, BSDLY, CR0, CR1, CR2, CR3, CRDLY, FF0, FF1, FFDLY, NL0, NL1, NLDLY, OFDEL, OFILL,
100    TAB1, TAB2, VT0, VT1, VTDLY,
101};
102#[cfg(any(
103    target_os = "android",
104    target_os = "illumos",
105    target_os = "linux",
106    target_os = "solaris"
107))]
108pub use ::termios::os::target::{CBAUD, CIBAUD, IUCLC, OLCUC, XCASE};
109#[cfg(any(
110    target_os = "android",
111    target_os = "freebsd",
112    target_os = "illumos",
113    target_os = "linux",
114    target_os = "macos",
115    target_os = "solaris"
116))]
117pub use ::termios::os::target::{TAB0, TABDLY};
118#[cfg(any(target_os = "android", target_os = "linux"))]
119pub use ::termios::os::target::{VSWTC, VSWTC as VSWTCH};
120#[cfg(any(target_os = "illumos", target_os = "solaris"))]
121pub use ::termios::os::target::{VSWTCH, VSWTCH as VSWTC};
122pub use ::termios::{
123    B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200,
124    B38400, BRKINT, CLOCAL, CREAD, CS5, CS6, CS7, CS8, CSIZE, CSTOPB, ECHO, ECHOE, ECHOK, ECHONL,
125    HUPCL, ICANON, ICRNL, IEXTEN, IGNBRK, IGNCR, IGNPAR, INLCR, INPCK, ISIG, ISTRIP, IXANY, IXOFF,
126    IXON, NOFLSH, OCRNL, ONLCR, ONLRET, ONOCR, OPOST, PARENB, PARMRK, PARODD, TCIFLUSH, TCIOFF,
127    TCIOFLUSH, TCION, TCOFLUSH, TCOOFF, TCOON, TCSADRAIN, TCSAFLUSH, TCSANOW, TOSTOP, VEOF, VEOL,
128    VERASE, VINTR, VKILL, VMIN, VQUIT, VSTART, VSTOP, VSUSP, VTIME,
129    os::target::{
130        B57600, B115200, B230400, CRTSCTS, ECHOCTL, ECHOKE, ECHOPRT, EXTA, EXTB, FLUSHO, IMAXBEL,
131        NCCS, PENDIN, VDISCARD, VEOL2, VLNEXT, VREPRINT, VWERASE,
132    },
133};
134
135pub fn tcgetattr(fd: i32) -> std::io::Result<Termios> {
136    Termios::from_fd(fd)
137}
138
139pub fn tcsetattr(fd: i32, when: i32, termios: &Termios) -> std::io::Result<()> {
140    ::termios::tcsetattr(fd, when, termios)
141}
142
143pub fn cfgetispeed(termios: &Termios) -> libc::speed_t {
144    ::termios::cfgetispeed(termios)
145}
146
147pub fn cfgetospeed(termios: &Termios) -> libc::speed_t {
148    ::termios::cfgetospeed(termios)
149}
150
151pub fn cfsetispeed(termios: &mut Termios, speed: libc::speed_t) -> std::io::Result<()> {
152    ::termios::cfsetispeed(termios, speed)
153}
154
155pub fn cfsetospeed(termios: &mut Termios, speed: libc::speed_t) -> std::io::Result<()> {
156    ::termios::cfsetospeed(termios, speed)
157}
158
159pub fn tcsendbreak(fd: i32, duration: i32) -> std::io::Result<()> {
160    ::termios::tcsendbreak(fd, duration)
161}
162
163pub fn tcdrain(fd: i32) -> std::io::Result<()> {
164    ::termios::tcdrain(fd)
165}
166
167pub fn tcflush(fd: i32, queue: i32) -> std::io::Result<()> {
168    ::termios::tcflush(fd, queue)
169}
170
171pub fn tcflow(fd: i32, action: i32) -> std::io::Result<()> {
172    ::termios::tcflow(fd, action)
173}
174
175pub fn tcgetwinsize(fd: i32) -> std::io::Result<(u16, u16)> {
176    let mut size: libc::winsize = unsafe { core::mem::zeroed() };
177    let ret = unsafe { libc::ioctl(fd, TIOCGWINSZ as _, &mut size) };
178    if ret < 0 {
179        return Err(std::io::Error::last_os_error());
180    }
181    Ok((size.ws_row, size.ws_col))
182}
183
184pub fn tcsetwinsize(fd: i32, row: u16, col: u16) -> std::io::Result<()> {
185    let mut size: libc::winsize = unsafe { core::mem::zeroed() };
186    let ret = unsafe { libc::ioctl(fd, TIOCGWINSZ as _, &mut size) };
187    if ret < 0 {
188        return Err(std::io::Error::last_os_error());
189    }
190    size.ws_row = row;
191    size.ws_col = col;
192    let ret = unsafe { libc::ioctl(fd, TIOCSWINSZ as _, &size) };
193    if ret < 0 {
194        return Err(std::io::Error::last_os_error());
195    }
196    Ok(())
197}