| 1 | use std::fs::File; |
| 2 | use std::io::{Read, Write}; |
| 3 | use std::mem::ManuallyDrop; |
| 4 | use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd}; |
| 5 | |
| 6 | /// Unix handle to an open terminal. |
| 7 | pub enum Terminal { |
| 8 | /// Non-owning file for one of the standard I/O streams. |
| 9 | Stdio(ManuallyDrop<File>), |
| 10 | |
| 11 | /// Owned file for `/dev/tty`. |
| 12 | File(File), |
| 13 | } |
| 14 | |
| 15 | #[derive(Copy, Clone)] |
| 16 | pub struct TerminalMode { |
| 17 | termios: libc::termios, |
| 18 | } |
| 19 | |
| 20 | impl Terminal { |
| 21 | pub fn open() -> std::io::Result<Self> { |
| 22 | if let Some(terminal) = open_fd_terminal(2) { |
| 23 | Ok(terminal) |
| 24 | } else if let Some(terminal) = open_fd_terminal(0) { |
| 25 | Ok(terminal) |
| 26 | } else if let Some(terminal) = open_fd_terminal(1) { |
| 27 | Ok(terminal) |
| 28 | } else { |
| 29 | let file = std::fs::OpenOptions::new() |
| 30 | .read(true) |
| 31 | .write(true) |
| 32 | .open("/dev/tty")?; |
| 33 | if is_terminal(file.as_fd()) { |
| 34 | Ok(Self::File(file)) |
| 35 | } else { |
| 36 | Err(std::io::Error::from_raw_os_error(libc::ENOTTY)) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub fn get_terminal_mode(&self) -> std::io::Result<TerminalMode> { |
| 42 | unsafe { |
| 43 | let mut termios = std::mem::zeroed(); |
| 44 | check_ret(libc::tcgetattr(self.as_fd().as_raw_fd(), &mut termios))?; |
| 45 | Ok(TerminalMode { termios }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn set_terminal_mode(&self, mode: &TerminalMode) -> std::io::Result<()> { |
| 50 | unsafe { |
| 51 | check_ret(libc::tcsetattr( |
| 52 | self.as_fd().as_raw_fd(), |
| 53 | libc::TCSANOW, |
| 54 | &mode.termios, |
| 55 | ))?; |
| 56 | Ok(()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn as_file(&self) -> &File { |
| 61 | match self { |
| 62 | Self::Stdio(io) => io, |
| 63 | Self::File(io) => io, |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn open_fd_terminal(fd: RawFd) -> Option<Terminal> { |
| 69 | let file = unsafe { ManuallyDrop::new(File::from_raw_fd(fd)) }; |
| 70 | if is_terminal(file.as_fd()) { |
| 71 | Some(Terminal::Stdio(file)) |
| 72 | } else { |
| 73 | None |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl TerminalMode { |
| 78 | pub fn enable_line_editing(&mut self) { |
| 79 | self.termios.c_lflag |= libc::ICANON; |
| 80 | } |
| 81 | |
| 82 | pub fn disable_echo(&mut self) { |
| 83 | self.termios.c_lflag &= !libc::ECHO; |
| 84 | self.termios.c_lflag &= !libc::ICANON; |
| 85 | } |
| 86 | |
| 87 | pub fn enable_echo(&mut self) { |
| 88 | self.termios.c_lflag |= libc::ECHO; |
| 89 | self.termios.c_lflag |= !libc::ICANON; |
| 90 | } |
| 91 | |
| 92 | pub fn is_echo_enabled(&self) -> bool { |
| 93 | self.termios.c_lflag & libc::ECHO != 0 |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn is_terminal(fd: BorrowedFd) -> bool { |
| 98 | unsafe { |
| 99 | libc::isatty(fd.as_raw_fd()) == 1 |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn check_ret(input: i32) -> std::io::Result<()> { |
| 104 | if input == 0 { |
| 105 | Ok(()) |
| 106 | } else { |
| 107 | Err(std::io::Error::last_os_error()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl AsFd for Terminal { |
| 112 | fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { |
| 113 | match self { |
| 114 | Self::Stdio(stdin) => stdin.as_fd(), |
| 115 | Self::File(file) => file.as_fd(), |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl Read for Terminal { |
| 121 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 122 | self.as_file().read(buf) |
| 123 | } |
| 124 | |
| 125 | fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> { |
| 126 | self.as_file().read_vectored(bufs) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl Write for Terminal { |
| 131 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 132 | self.as_file().write(buf) |
| 133 | } |
| 134 | |
| 135 | fn flush(&mut self) -> std::io::Result<()> { |
| 136 | self.as_file().flush() |
| 137 | } |
| 138 | |
| 139 | fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> { |
| 140 | self.as_file().write_vectored(bufs) |
| 141 | } |
| 142 | } |