summaryrefslogtreecommitdiffstats
path: root/third_party/rust/termion/src/sys/redox
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/termion/src/sys/redox')
-rw-r--r--third_party/rust/termion/src/sys/redox/attr.rs33
-rw-r--r--third_party/rust/termion/src/sys/redox/mod.rs15
-rw-r--r--third_party/rust/termion/src/sys/redox/size.rs18
-rw-r--r--third_party/rust/termion/src/sys/redox/tty.rs22
4 files changed, 88 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)
+}