diff options
Diffstat (limited to 'src/tty')
-rw-r--r-- | src/tty/mod.rs | 10 | ||||
-rw-r--r-- | src/tty/unix.rs | 91 | ||||
-rw-r--r-- | src/tty/windows/conpty.rs | 15 | ||||
-rw-r--r-- | src/tty/windows/mod.rs | 66 | ||||
-rw-r--r-- | src/tty/windows/winpty.rs | 42 |
5 files changed, 70 insertions, 154 deletions
diff --git a/src/tty/mod.rs b/src/tty/mod.rs index ea2dd0c4..ec175ee6 100644 --- a/src/tty/mod.rs +++ b/src/tty/mod.rs @@ -57,7 +57,7 @@ pub trait EventedReadWrite { #[derive(PartialEq)] pub enum ChildEvent { /// Indicates the child has exited - Exited + Exited, } /// A pseudoterminal (or PTY) @@ -65,7 +65,7 @@ pub enum ChildEvent { /// This is a refinement of EventedReadWrite that also provides a channel through which we can be /// notified if the PTY child process does something we care about (other than writing to the TTY). /// In particular, this allows for race-free child exit notification on UNIX (cf. `SIGCHLD`). -pub trait EventedPty : EventedReadWrite { +pub trait EventedPty: EventedReadWrite { #[cfg(unix)] fn child_event_token(&self) -> mio::Token; @@ -83,11 +83,7 @@ pub fn setup_env(config: &Config) { // below. env::set_var( "TERM", - if Database::from_name("alacritty").is_ok() { - "alacritty" - } else { - "xterm-256color" - }, + if Database::from_name("alacritty").is_ok() { "alacritty" } else { "xterm-256color" }, ); // Advertise 24-bit color support diff --git a/src/tty/unix.rs b/src/tty/unix.rs index d8392dcd..0e3dc2fd 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -15,24 +15,27 @@ //! tty related functionality //! -use crate::tty::{EventedReadWrite, EventedPty, ChildEvent}; -use crate::term::SizeInfo; -use crate::display::OnResize; -use crate::config::{Config, Shell}; use crate::cli::Options; +use crate::config::{Config, Shell}; +use crate::display::OnResize; +use crate::term::SizeInfo; +use crate::tty::{ChildEvent, EventedPty, EventedReadWrite}; use mio; use libc::{self, c_int, pid_t, winsize, TIOCSCTTY}; use nix::pty::openpty; use signal_hook::{self as sighook, iterator::Signals}; -use std::os::unix::{process::CommandExt, io::{FromRawFd, AsRawFd, RawFd}}; -use std::fs::File; -use std::process::{Command, Stdio, Child}; -use std::ffi::CStr; -use std::ptr; use mio::unix::EventedFd; +use std::ffi::CStr; +use std::fs::File; use std::io; +use std::os::unix::{ + io::{AsRawFd, FromRawFd, RawFd}, + process::CommandExt, +}; +use std::process::{Child, Command, Stdio}; +use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; /// Process ID of child process @@ -55,8 +58,7 @@ fn make_pty(size: winsize) -> (RawFd, RawFd) { win_size.ws_xpixel = 0; win_size.ws_ypixel = 0; - let ends = openpty(Some(&win_size), None) - .expect("openpty failed"); + let ends = openpty(Some(&win_size), None).expect("openpty failed"); (ends.master, ends.slave) } @@ -134,7 +136,6 @@ pub struct Pty { token: mio::Token, signals: Signals, signals_token: mio::Token, - } impl Pty { @@ -145,9 +146,7 @@ impl Pty { pub fn resize<T: ToWinsize>(&self, size: &T) { let win = size.to_winsize(); - let res = unsafe { - libc::ioctl(self.fd.as_raw_fd(), libc::TIOCSWINSZ, &win as *const _) - }; + let res = unsafe { libc::ioctl(self.fd.as_raw_fd(), libc::TIOCSWINSZ, &win as *const _) }; if res < 0 { die!("ioctl TIOCSWINSZ failed: {}", errno()); @@ -170,10 +169,7 @@ pub fn new<T: ToWinsize>( let default_shell = if cfg!(target_os = "macos") { let shell_name = pw.shell.rsplit('/').next().unwrap(); - let argv = vec![ - String::from("-c"), - format!("exec -a -{} {}", shell_name, pw.shell), - ]; + let argv = vec![String::from("-c"), format!("exec -a -{} {}", shell_name, pw.shell)]; Shell::new_with_args("/bin/bash", argv) } else { @@ -265,7 +261,7 @@ pub fn new<T: ToWinsize>( }, Err(err) => { die!("Failed to spawn command: {}", err); - } + }, } } @@ -282,19 +278,14 @@ impl EventedReadWrite for Pty { poll_opts: mio::PollOpt, ) -> io::Result<()> { self.token = token.next().unwrap(); - poll.register( - &EventedFd(&self.fd.as_raw_fd()), - self.token, - interest, - poll_opts - )?; + poll.register(&EventedFd(&self.fd.as_raw_fd()), self.token, interest, poll_opts)?; self.signals_token = token.next().unwrap(); poll.register( &self.signals, self.signals_token, mio::Ready::readable(), - mio::PollOpt::level() + mio::PollOpt::level(), ) } @@ -303,20 +294,15 @@ impl EventedReadWrite for Pty { &mut self, poll: &mio::Poll, interest: mio::Ready, - poll_opts: mio::PollOpt + poll_opts: mio::PollOpt, ) -> io::Result<()> { - poll.reregister( - &EventedFd(&self.fd.as_raw_fd()), - self.token, - interest, - poll_opts - )?; + poll.reregister(&EventedFd(&self.fd.as_raw_fd()), self.token, interest, poll_opts)?; poll.reregister( &self.signals, self.signals_token, mio::Ready::readable(), - mio::PollOpt::level() + mio::PollOpt::level(), ) } @@ -350,23 +336,20 @@ impl EventedReadWrite for Pty { impl EventedPty for Pty { #[inline] fn next_child_event(&mut self) -> Option<ChildEvent> { - self.signals - .pending() - .next() - .and_then(|signal| { - if signal != sighook::SIGCHLD { - return None; - } - - match self.child.try_wait() { - Err(e) => { - error!("Error checking child process termination: {}", e); - None - }, - Ok(None) => None, - Ok(_) => Some(ChildEvent::Exited), - } - }) + self.signals.pending().next().and_then(|signal| { + if signal != sighook::SIGCHLD { + return None; + } + + match self.child.try_wait() { + Err(e) => { + error!("Error checking child process termination: {}", e); + None + }, + Ok(None) => None, + Ok(_) => Some(ChildEvent::Exited), + } + }) } #[inline] @@ -400,9 +383,7 @@ impl OnResize for i32 { fn on_resize(&mut self, size: &SizeInfo) { let win = size.to_winsize(); - let res = unsafe { - libc::ioctl(*self, libc::TIOCSWINSZ, &win as *const _) - }; + let res = unsafe { libc::ioctl(*self, libc::TIOCSWINSZ, &win as *const _) }; if res < 0 { die!("ioctl TIOCSWINSZ failed: {}", errno()); diff --git a/src/tty/windows/conpty.rs b/src/tty/windows/conpty.rs index 6c74df7f..7383118f 100644 --- a/src/tty/windows/conpty.rs +++ b/src/tty/windows/conpty.rs @@ -214,10 +214,7 @@ pub fn new<'a>( cmdline.insert(0, initial_command.program().into()); // Warning, here be borrow hell - let cwd = options - .working_dir - .as_ref() - .map(|dir| canonicalize(dir).unwrap()); + let cwd = options.working_dir.as_ref().map(|dir| canonicalize(dir).unwrap()); let cwd = cwd.as_ref().map(|dir| dir.to_str().unwrap()); // Create the client application, using startup info containing ConPTY info @@ -250,10 +247,7 @@ pub fn new<'a>( let conin = EventedAnonWrite::new(conin); let conout = EventedAnonRead::new(conout); - let agent = Conpty { - handle: pty_handle, - api, - }; + let agent = Conpty { handle: pty_handle, api }; Some(Pty { handle: super::PtyHandle::Conpty(ConptyHandle::new(agent)), @@ -279,10 +273,7 @@ fn coord_from_sizeinfo(sizeinfo: &SizeInfo) -> Option<COORD> { let lines = sizeinfo.lines().0; if cols <= i16::MAX as usize && lines <= i16::MAX as usize { - Some(COORD { - X: sizeinfo.cols().0 as i16, - Y: sizeinfo.lines().0 as i16, - }) + Some(COORD { X: sizeinfo.cols().0 as i16, Y: sizeinfo.lines().0 as i16 }) } else { None } diff --git a/src/tty/windows/mod.rs b/src/tty/windows/mod.rs index 2f5caa93..c87c5257 100644 --- a/src/tty/windows/mod.rs +++ b/src/tty/windows/mod.rs @@ -28,7 +28,7 @@ use crate::cli::Options; use crate::config::Config; use crate::display::OnResize; use crate::term::SizeInfo; -use crate::tty::{EventedReadWrite, EventedPty}; +use crate::tty::{EventedPty, EventedReadWrite}; mod conpty; mod winpty; @@ -44,14 +44,14 @@ pub fn process_should_exit() -> bool { WAIT_OBJECT_0 => { info!("wait_object_0"); true - } + }, // Reached timeout of 0, process has not exited WAIT_TIMEOUT => false, // Error checking process, winpty gave us a bad agent handle? _ => { info!("Bad exit: {}", ::std::io::Error::last_os_error()); true - } + }, } } } @@ -219,7 +219,7 @@ impl<'a> OnResize for PtyHandle<'a> { PtyHandle::Conpty(c) => { let mut handle = c.clone(); handle.on_resize(sizeinfo) - } + }, } } } @@ -240,34 +240,14 @@ impl<'a> EventedReadWrite for Pty<'a> { self.write_token = token.next().unwrap(); if interest.is_readable() { - poll.register( - &self.conout, - self.read_token, - mio::Ready::readable(), - poll_opts, - )? + poll.register(&self.conout, self.read_token, mio::Ready::readable(), poll_opts)? } else { - poll.register( - &self.conout, - self.read_token, - mio::Ready::empty(), - poll_opts, - )? + poll.register(&self.conout, self.read_token, mio::Ready::empty(), poll_opts)? } if interest.is_writable() { - poll.register( - &self.conin, - self.write_token, - mio::Ready::writable(), - poll_opts, - )? + poll.register(&self.conin, self.write_token, mio::Ready::writable(), poll_opts)? } else { - poll.register( - &self.conin, - self.write_token, - mio::Ready::empty(), - poll_opts, - )? + poll.register(&self.conin, self.write_token, mio::Ready::empty(), poll_opts)? } Ok(()) } @@ -280,34 +260,14 @@ impl<'a> EventedReadWrite for Pty<'a> { poll_opts: mio::PollOpt, ) -> io::Result<()> { if interest.is_readable() { - poll.reregister( - &self.conout, - self.read_token, - mio::Ready::readable(), - poll_opts, - )?; + poll.reregister(&self.conout, self.read_token, mio::Ready::readable(), poll_opts)?; } else { - poll.reregister( - &self.conout, - self.read_token, - mio::Ready::empty(), - poll_opts, - )?; + poll.reregister(&self.conout, self.read_token, mio::Ready::empty(), poll_opts)?; } if interest.is_writable() { - poll.reregister( - &self.conin, - self.write_token, - mio::Ready::writable(), - poll_opts, - )?; + poll.reregister(&self.conin, self.write_token, mio::Ready::writable(), poll_opts)?; } else { - poll.reregister( - &self.conin, - self.write_token, - mio::Ready::empty(), - poll_opts, - )?; + poll.reregister(&self.conin, self.write_token, mio::Ready::empty(), poll_opts)?; } Ok(()) } @@ -340,4 +300,4 @@ impl<'a> EventedReadWrite for Pty<'a> { } } -impl<'a> EventedPty for Pty<'a> { } +impl<'a> EventedPty for Pty<'a> {} diff --git a/src/tty/windows/winpty.rs b/src/tty/windows/winpty.rs index 26536eee..10bd9d01 100644 --- a/src/tty/windows/winpty.rs +++ b/src/tty/windows/winpty.rs @@ -14,29 +14,29 @@ use super::{Pty, HANDLE}; -use std::io; use std::fs::OpenOptions; -use std::os::windows::io::{FromRawHandle, IntoRawHandle}; +use std::io; use std::os::windows::fs::OpenOptionsExt; +use std::os::windows::io::{FromRawHandle, IntoRawHandle}; use std::sync::Arc; use std::u16; use dunce::canonicalize; use mio_named_pipes::NamedPipe; use winapi::um::winbase::FILE_FLAG_OVERLAPPED; -use winpty::{ConfigFlags, MouseMode, SpawnConfig, SpawnFlags, Winpty}; use winpty::Config as WinptyConfig; +use winpty::{ConfigFlags, MouseMode, SpawnConfig, SpawnFlags, Winpty}; +use crate::cli::Options; use crate::config::{Config, Shell}; use crate::display::OnResize; -use crate::cli::Options; use crate::term::SizeInfo; // We store a raw pointer because we need mutable access to call // on_resize from a separate thread. Winpty internally uses a mutex // so this is safe, despite outwards appearance. pub struct Agent<'a> { - winpty: *mut Winpty<'a> + winpty: *mut Winpty<'a>, } /// Handle can be cloned freely and moved between threads. @@ -48,9 +48,7 @@ unsafe impl<'a> Sync for Agent<'a> {} impl<'a> Agent<'a> { pub fn new(winpty: Winpty<'a>) -> Self { - Self { - winpty: Box::into_raw(Box::new(winpty)) - } + Self { winpty: Box::into_raw(Box::new(winpty)) } } /// Get immutable access to Winpty. @@ -68,11 +66,12 @@ impl<'a> Agent<'a> { impl<'a> Drop for Agent<'a> { fn drop(&mut self) { - unsafe { Box::from_raw(self.winpty); } + unsafe { + Box::from_raw(self.winpty); + } } } - /// How long the winpty agent should wait for any RPC request /// This is a placeholder value until we see how often long responses happen const AGENT_TIMEOUT: u32 = 10000; @@ -112,30 +111,19 @@ pub fn new<'a>( Some(&cmdline.join(" ")), cwd, None, // Env - ).unwrap(); + ) + .unwrap(); let default_opts = &mut OpenOptions::new(); - default_opts - .share_mode(0) - .custom_flags(FILE_FLAG_OVERLAPPED); + default_opts.share_mode(0).custom_flags(FILE_FLAG_OVERLAPPED); let (conout_pipe, conin_pipe); unsafe { conout_pipe = NamedPipe::from_raw_handle( - default_opts - .clone() - .read(true) - .open(conout) - .unwrap() - .into_raw_handle(), + default_opts.clone().read(true).open(conout).unwrap().into_raw_handle(), ); conin_pipe = NamedPipe::from_raw_handle( - default_opts - .clone() - .write(true) - .open(conin) - .unwrap() - .into_raw_handle(), + default_opts.clone().write(true).open(conin).unwrap().into_raw_handle(), ); }; @@ -166,7 +154,7 @@ pub fn new<'a>( conout: super::EventedReadablePipe::Named(conout_pipe), conin: super::EventedWritablePipe::Named(conin_pipe), read_token: 0.into(), - write_token: 0.into() + write_token: 0.into(), } } |