Skip to content

Commit

Permalink
Overhaul the termios API.
Browse files Browse the repository at this point in the history
Instead of defining `Termios` as an alias for the libc type, define our
own type with a libc-compatible layout, so that we can use `bitflags`
flags types, and have better overall ergonomics.
  • Loading branch information
sunfishcode committed Apr 20, 2023
1 parent 9ad2cbf commit e018796
Show file tree
Hide file tree
Showing 9 changed files with 1,071 additions and 1,517 deletions.
2 changes: 0 additions & 2 deletions src/backend/libc/termios/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub(crate) mod syscalls;
#[cfg(not(target_os = "wasi"))]
pub(crate) mod types;
100 changes: 59 additions & 41 deletions src/backend/libc/termios/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,40 @@ use core::mem::MaybeUninit;
#[cfg(not(target_os = "wasi"))]
pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
let mut result = MaybeUninit::<Termios>::uninit();

#[cfg(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
))]
unsafe {
ret(c::tcgetattr(borrowed_fd(fd), result.as_mut_ptr()))?;
ret(c::ioctl(borrowed_fd(fd), c::TCGETS2, result.as_mut_ptr()))?;
Ok(result.assume_init())
}
}

#[cfg(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
))]
pub(crate) fn tcgetattr2(fd: BorrowedFd<'_>) -> io::Result<crate::termios::Termios2> {
let mut result = MaybeUninit::<crate::termios::Termios2>::uninit();
#[cfg(not(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
)))]
unsafe {
ret(c::ioctl(borrowed_fd(fd), c::TCGETS2, result.as_mut_ptr()))?;
ret(c::tcgetattr(borrowed_fd(fd), result.as_mut_ptr()))?;
Ok(result.assume_init())
}
}
Expand All @@ -68,37 +79,44 @@ pub(crate) fn tcsetattr(
optional_actions: OptionalActions,
termios: &Termios,
) -> io::Result<()> {
#[cfg(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
))]
unsafe {
ret(c::tcsetattr(
ret(c::ioctl(
borrowed_fd(fd),
optional_actions as _,
(c::TCSETS2 as u32 + optional_actions as u32) as _,
termios,
))
}
}

#[cfg(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
))]
pub(crate) fn tcsetattr2(
fd: BorrowedFd,
optional_actions: OptionalActions,
termios: &crate::termios::Termios2,
) -> io::Result<()> {
#[cfg(not(all(
any(target_os = "android", target_os = "linux"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "x32",
target_arch = "riscv64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
)
)))]
unsafe {
ret(c::ioctl(
ret(c::tcsetattr(
borrowed_fd(fd),
(c::TCSETS2 as u32 + optional_actions as u32) as _,
optional_actions as _,
termios,
))
}
Expand Down
Loading

0 comments on commit e018796

Please sign in to comment.