Skip to content

Commit aa08658

Browse files
committed
Enable rustix::termios::isatty on wasm32-wasi.
1 parent 059f872 commit aa08658

File tree

7 files changed

+167
-50
lines changed

7 files changed

+167
-50
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ serial_test = "0.6"
7878
memoffset = "0.7.1"
7979
flate2 = "1.0"
8080

81-
[target.'cfg(not(target_os = "emscripten"))'.dev-dependencies]
81+
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi")))'.dev-dependencies]
8282
criterion = "0.4"
8383

8484
[target.'cfg(windows)'.dev-dependencies]

examples/stdio.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
3838
if isatty(fd) {
3939
#[cfg(feature = "procfs")]
4040
println!(" - ttyname: {}", ttyname(fd, Vec::new())?.to_string_lossy());
41+
42+
#[cfg(target_os = "wasi")]
43+
println!(" - is a tty");
44+
45+
#[cfg(not(target_os = "wasi"))]
4146
println!(" - process group: {:?}", rustix::termios::tcgetpgrp(fd)?);
47+
48+
#[cfg(not(target_os = "wasi"))]
4249
println!(" - winsize: {:?}", rustix::termios::tcgetwinsize(fd)?);
4350

51+
#[cfg(not(target_os = "wasi"))]
4452
{
4553
use rustix::termios::*;
4654
let term = tcgetattr(fd)?;

src/backend/libc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) mod process;
8181
#[cfg(not(windows))]
8282
#[cfg(feature = "rand")]
8383
pub(crate) mod rand;
84-
#[cfg(not(any(windows, target_os = "wasi")))]
84+
#[cfg(not(windows))]
8585
#[cfg(feature = "termios")]
8686
pub(crate) mod termios;
8787
#[cfg(not(windows))]

src/backend/libc/termios/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub(crate) mod syscalls;
2+
#[cfg(not(target_os = "wasi"))]
23
pub(crate) mod types;

src/backend/libc/termios/syscalls.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ use crate::fd::BorrowedFd;
1111
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
1212
use crate::ffi::CStr;
1313
use crate::io;
14+
#[cfg(not(target_os = "wasi"))]
1415
use crate::process::{Pid, RawNonZeroPid};
16+
#[cfg(not(target_os = "wasi"))]
1517
use crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize};
1618
use core::mem::MaybeUninit;
1719
use libc_errno::errno;
1820

21+
#[cfg(not(target_os = "wasi"))]
1922
pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
2023
let mut result = MaybeUninit::<Termios>::uninit();
2124
unsafe {
@@ -24,6 +27,7 @@ pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
2427
}
2528
}
2629

30+
#[cfg(not(target_os = "wasi"))]
2731
pub(crate) fn tcgetpgrp(fd: BorrowedFd<'_>) -> io::Result<Pid> {
2832
unsafe {
2933
let pid = ret_pid_t(c::tcgetpgrp(borrowed_fd(fd)))?;
@@ -32,10 +36,12 @@ pub(crate) fn tcgetpgrp(fd: BorrowedFd<'_>) -> io::Result<Pid> {
3236
}
3337
}
3438

39+
#[cfg(not(target_os = "wasi"))]
3540
pub(crate) fn tcsetpgrp(fd: BorrowedFd<'_>, pid: Pid) -> io::Result<()> {
3641
unsafe { ret(c::tcsetpgrp(borrowed_fd(fd), pid.as_raw_nonzero().get())) }
3742
}
3843

44+
#[cfg(not(target_os = "wasi"))]
3945
pub(crate) fn tcsetattr(
4046
fd: BorrowedFd,
4147
optional_actions: OptionalActions,
@@ -50,22 +56,27 @@ pub(crate) fn tcsetattr(
5056
}
5157
}
5258

59+
#[cfg(not(target_os = "wasi"))]
5360
pub(crate) fn tcsendbreak(fd: BorrowedFd) -> io::Result<()> {
5461
unsafe { ret(c::tcsendbreak(borrowed_fd(fd), 0)) }
5562
}
5663

64+
#[cfg(not(target_os = "wasi"))]
5765
pub(crate) fn tcdrain(fd: BorrowedFd) -> io::Result<()> {
5866
unsafe { ret(c::tcdrain(borrowed_fd(fd))) }
5967
}
6068

69+
#[cfg(not(target_os = "wasi"))]
6170
pub(crate) fn tcflush(fd: BorrowedFd, queue_selector: QueueSelector) -> io::Result<()> {
6271
unsafe { ret(c::tcflush(borrowed_fd(fd), queue_selector as _)) }
6372
}
6473

74+
#[cfg(not(target_os = "wasi"))]
6575
pub(crate) fn tcflow(fd: BorrowedFd, action: Action) -> io::Result<()> {
6676
unsafe { ret(c::tcflow(borrowed_fd(fd), action as _)) }
6777
}
6878

79+
#[cfg(not(target_os = "wasi"))]
6980
pub(crate) fn tcgetsid(fd: BorrowedFd) -> io::Result<Pid> {
7081
unsafe {
7182
let pid = ret_pid_t(c::tcgetsid(borrowed_fd(fd)))?;
@@ -74,10 +85,12 @@ pub(crate) fn tcgetsid(fd: BorrowedFd) -> io::Result<Pid> {
7485
}
7586
}
7687

88+
#[cfg(not(target_os = "wasi"))]
7789
pub(crate) fn tcsetwinsize(fd: BorrowedFd, winsize: Winsize) -> io::Result<()> {
7890
unsafe { ret(c::ioctl(borrowed_fd(fd), c::TIOCSWINSZ, &winsize)) }
7991
}
8092

93+
#[cfg(not(target_os = "wasi"))]
8194
pub(crate) fn tcgetwinsize(fd: BorrowedFd) -> io::Result<Winsize> {
8295
unsafe {
8396
let mut buf = MaybeUninit::<Winsize>::uninit();
@@ -90,33 +103,39 @@ pub(crate) fn tcgetwinsize(fd: BorrowedFd) -> io::Result<Winsize> {
90103
}
91104
}
92105

106+
#[cfg(not(target_os = "wasi"))]
93107
#[inline]
94108
#[must_use]
95109
pub(crate) fn cfgetospeed(termios: &Termios) -> Speed {
96110
unsafe { c::cfgetospeed(termios) }
97111
}
98112

113+
#[cfg(not(target_os = "wasi"))]
99114
#[inline]
100115
#[must_use]
101116
pub(crate) fn cfgetispeed(termios: &Termios) -> Speed {
102117
unsafe { c::cfgetispeed(termios) }
103118
}
104119

120+
#[cfg(not(target_os = "wasi"))]
105121
#[inline]
106122
pub(crate) fn cfmakeraw(termios: &mut Termios) {
107123
unsafe { c::cfmakeraw(termios) }
108124
}
109125

126+
#[cfg(not(target_os = "wasi"))]
110127
#[inline]
111128
pub(crate) fn cfsetospeed(termios: &mut Termios, speed: Speed) -> io::Result<()> {
112129
unsafe { ret(c::cfsetospeed(termios, speed)) }
113130
}
114131

132+
#[cfg(not(target_os = "wasi"))]
115133
#[inline]
116134
pub(crate) fn cfsetispeed(termios: &mut Termios, speed: Speed) -> io::Result<()> {
117135
unsafe { ret(c::cfsetispeed(termios, speed)) }
118136
}
119137

138+
#[cfg(not(target_os = "wasi"))]
120139
#[inline]
121140
pub(crate) fn cfsetspeed(termios: &mut Termios, speed: Speed) -> io::Result<()> {
122141
unsafe { ret(c::cfsetspeed(termios, speed)) }

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub mod process;
188188
#[cfg(feature = "rand")]
189189
#[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))]
190190
pub mod rand;
191-
#[cfg(not(any(windows, target_os = "wasi")))]
191+
#[cfg(not(windows))]
192192
#[cfg(feature = "termios")]
193193
#[cfg_attr(doc_cfg, doc(cfg(feature = "termios")))]
194194
pub mod termios;

0 commit comments

Comments
 (0)