diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/termion/src/sys | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/termion/src/sys')
-rw-r--r-- | third_party/rust/termion/src/sys/redox/attr.rs | 33 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/redox/mod.rs | 15 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/redox/size.rs | 18 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/redox/tty.rs | 22 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/unix/attr.rs | 29 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/unix/mod.rs | 33 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/unix/size.rs | 48 | ||||
-rw-r--r-- | third_party/rust/termion/src/sys/unix/tty.rs | 17 |
8 files changed, 215 insertions, 0 deletions
diff --git a/third_party/rust/termion/src/sys/redox/attr.rs b/third_party/rust/termion/src/sys/redox/attr.rs new file mode 100644 index 0000000000..c6489a510c --- /dev/null +++ b/third_party/rust/termion/src/sys/redox/attr.rs @@ -0,0 +1,33 @@ +use std::io; + +use super::{cvt, syscall, Termios}; + +pub fn get_terminal_attr() -> io::Result<Termios> { + let mut termios = Termios::default(); + + let fd = cvt(syscall::dup(0, b"termios"))?; + let res = cvt(syscall::read(fd, &mut termios)); + let _ = syscall::close(fd); + + if res? == termios.len() { + Ok(termios) + } else { + Err(io::Error::new(io::ErrorKind::Other, "Unable to get the terminal attributes.")) + } +} + +pub fn set_terminal_attr(termios: &Termios) -> io::Result<()> { + let fd = cvt(syscall::dup(0, b"termios"))?; + let res = cvt(syscall::write(fd, termios)); + let _ = syscall::close(fd); + + if res? == termios.len() { + Ok(()) + } else { + Err(io::Error::new(io::ErrorKind::Other, "Unable to set the terminal attributes.")) + } +} + +pub fn raw_terminal_attr(ios: &mut Termios) { + ios.make_raw() +} diff --git a/third_party/rust/termion/src/sys/redox/mod.rs b/third_party/rust/termion/src/sys/redox/mod.rs new file mode 100644 index 0000000000..2a9b875e32 --- /dev/null +++ b/third_party/rust/termion/src/sys/redox/mod.rs @@ -0,0 +1,15 @@ +extern crate redox_termios; +extern crate syscall; + +use std::io; + +pub use self::redox_termios::Termios; + +pub mod attr; +pub mod size; +pub mod tty; + +// Support function for converting syscall error to io error +fn cvt(result: Result<usize, syscall::Error>) -> io::Result<usize> { + result.map_err(|err| io::Error::from_raw_os_error(err.errno)) +} diff --git a/third_party/rust/termion/src/sys/redox/size.rs b/third_party/rust/termion/src/sys/redox/size.rs new file mode 100644 index 0000000000..07f64a2437 --- /dev/null +++ b/third_party/rust/termion/src/sys/redox/size.rs @@ -0,0 +1,18 @@ +use std::io; + +use super::{cvt, redox_termios, syscall}; + +/// Get the size of the terminal. +pub fn terminal_size() -> io::Result<(u16, u16)> { + let mut winsize = redox_termios::Winsize::default(); + + let fd = cvt(syscall::dup(1, b"winsize"))?; + let res = cvt(syscall::read(fd, &mut winsize)); + let _ = syscall::close(fd); + + if res? == winsize.len() { + Ok((winsize.ws_col, winsize.ws_row)) + } else { + Err(io::Error::new(io::ErrorKind::Other, "Unable to get the terminal size.")) + } +} diff --git a/third_party/rust/termion/src/sys/redox/tty.rs b/third_party/rust/termion/src/sys/redox/tty.rs new file mode 100644 index 0000000000..9179b39625 --- /dev/null +++ b/third_party/rust/termion/src/sys/redox/tty.rs @@ -0,0 +1,22 @@ +use std::{env, fs, io}; +use std::os::unix::io::AsRawFd; + +use super::syscall; + +/// Is this stream a TTY? +pub fn is_tty<T: AsRawFd>(stream: &T) -> bool { + if let Ok(fd) = syscall::dup(stream.as_raw_fd(), b"termios") { + let _ = syscall::close(fd); + true + } else { + false + } +} + +/// Get the TTY device. +/// +/// This allows for getting stdio representing _only_ the TTY, and not other streams. +pub fn get_tty() -> io::Result<fs::File> { + let tty = try!(env::var("TTY").map_err(|x| io::Error::new(io::ErrorKind::NotFound, x))); + fs::OpenOptions::new().read(true).write(true).open(tty) +} diff --git a/third_party/rust/termion/src/sys/unix/attr.rs b/third_party/rust/termion/src/sys/unix/attr.rs new file mode 100644 index 0000000000..5e21fbac8e --- /dev/null +++ b/third_party/rust/termion/src/sys/unix/attr.rs @@ -0,0 +1,29 @@ +use std::{io, mem}; + +use super::{cvt, Termios}; +use super::libc::c_int; + +pub fn get_terminal_attr() -> io::Result<Termios> { + extern "C" { + pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int; + } + unsafe { + let mut termios = mem::zeroed(); + cvt(tcgetattr(0, &mut termios))?; + Ok(termios) + } +} + +pub fn set_terminal_attr(termios: &Termios) -> io::Result<()> { + extern "C" { + pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int; + } + cvt(unsafe { tcsetattr(0, 0, termios) }).and(Ok(())) +} + +pub fn raw_terminal_attr(termios: &mut Termios) { + extern "C" { + pub fn cfmakeraw(termptr: *mut Termios); + } + unsafe { cfmakeraw(termios) } +} diff --git a/third_party/rust/termion/src/sys/unix/mod.rs b/third_party/rust/termion/src/sys/unix/mod.rs new file mode 100644 index 0000000000..08d73feb12 --- /dev/null +++ b/third_party/rust/termion/src/sys/unix/mod.rs @@ -0,0 +1,33 @@ +extern crate libc; + +use std::io; + +pub use self::libc::termios as Termios; + +pub mod attr; +pub mod size; +pub mod tty; + +// Support functions for converting libc return values to io errors { +trait IsMinusOne { + fn is_minus_one(&self) -> bool; +} + +macro_rules! impl_is_minus_one { + ($($t:ident)*) => ($(impl IsMinusOne for $t { + fn is_minus_one(&self) -> bool { + *self == -1 + } + })*) + } + +impl_is_minus_one! { i8 i16 i32 i64 isize } + +fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> { + if t.is_minus_one() { + Err(io::Error::last_os_error()) + } else { + Ok(t) + } +} +// } End of support functions diff --git a/third_party/rust/termion/src/sys/unix/size.rs b/third_party/rust/termion/src/sys/unix/size.rs new file mode 100644 index 0000000000..9c2aaf1a8a --- /dev/null +++ b/third_party/rust/termion/src/sys/unix/size.rs @@ -0,0 +1,48 @@ +use std::{io, mem}; + +use super::cvt; +use super::libc::{c_ushort, ioctl, STDOUT_FILENO}; + +#[repr(C)] +struct TermSize { + row: c_ushort, + col: c_ushort, + _x: c_ushort, + _y: c_ushort, +} + +#[cfg(target_os = "linux")] +pub const TIOCGWINSZ: usize = 0x00005413; + +#[cfg(not(target_os = "linux"))] +pub const TIOCGWINSZ: usize = 0x40087468; + +// Since attributes on non-item statements is not stable yet, we use a function. +#[cfg(not(target_os = "android"))] +#[cfg(not(target_os = "redox"))] +#[cfg(target_pointer_width = "64")] +#[cfg(not(target_env = "musl"))] +fn tiocgwinsz() -> u64 { + TIOCGWINSZ as u64 +} +#[cfg(not(target_os = "android"))] +#[cfg(not(target_os = "redox"))] +#[cfg(target_pointer_width = "32")] +#[cfg(not(target_env = "musl"))] +fn tiocgwinsz() -> u32 { + TIOCGWINSZ as u32 +} + +#[cfg(any(target_env = "musl", target_os = "android"))] +fn tiocgwinsz() -> i32 { + TIOCGWINSZ as i32 +} + +/// Get the size of the terminal. +pub fn terminal_size() -> io::Result<(u16, u16)> { + unsafe { + let mut size: TermSize = mem::zeroed(); + cvt(ioctl(STDOUT_FILENO, tiocgwinsz(), &mut size as *mut _))?; + Ok((size.col as u16, size.row as u16)) + } +} diff --git a/third_party/rust/termion/src/sys/unix/tty.rs b/third_party/rust/termion/src/sys/unix/tty.rs new file mode 100644 index 0000000000..2be9363470 --- /dev/null +++ b/third_party/rust/termion/src/sys/unix/tty.rs @@ -0,0 +1,17 @@ +use std::{fs, io}; +use std::os::unix::io::AsRawFd; + +use super::libc; + + +/// Is this stream a TTY? +pub fn is_tty<T: AsRawFd>(stream: &T) -> bool { + unsafe { libc::isatty(stream.as_raw_fd()) == 1 } +} + +/// Get the TTY device. +/// +/// This allows for getting stdio representing _only_ the TTY, and not other streams. +pub fn get_tty() -> io::Result<fs::File> { + fs::OpenOptions::new().read(true).write(true).open("/dev/tty") +} |