From cac06e55066e4779b417ecb648c68c8ffe6131bb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 25 Mar 2019 11:52:43 -0700 Subject: [PATCH] rename pty traits, make selectable via enum --- src/frontend/guicommon/localtab.rs | 8 ++--- src/frontend/guicommon/window.rs | 4 +-- src/main.rs | 4 +-- src/pty/mod.rs | 51 ++++++++++++++++++++++++------ src/pty/unix.rs | 24 +++++++------- src/pty/win/conpty.rs | 24 +++++++------- src/pty/win/mod.rs | 22 +++---------- src/pty/win/winpty/mod.rs | 26 +++++++-------- 8 files changed, 90 insertions(+), 73 deletions(-) diff --git a/src/frontend/guicommon/localtab.rs b/src/frontend/guicommon/localtab.rs index 46757d4f1..33ffc9190 100644 --- a/src/frontend/guicommon/localtab.rs +++ b/src/frontend/guicommon/localtab.rs @@ -1,6 +1,6 @@ use crate::mux::renderable::Renderable; use crate::mux::tab::{alloc_tab_id, Tab, TabId}; -use crate::pty::{ChildTrait, MasterPtyTrait, PtySize}; +use crate::pty::{Child, MasterPty, PtySize}; use failure::Error; use std::cell::{RefCell, RefMut}; use term::{KeyCode, KeyModifiers, MouseEvent, Terminal, TerminalHost}; @@ -8,8 +8,8 @@ use term::{KeyCode, KeyModifiers, MouseEvent, Terminal, TerminalHost}; pub struct LocalTab { tab_id: TabId, terminal: RefCell, - process: RefCell>, - pty: RefCell>, + process: RefCell>, + pty: RefCell>, } impl Tab for LocalTab { @@ -83,7 +83,7 @@ impl Tab for LocalTab { } impl LocalTab { - pub fn new(terminal: Terminal, process: Box, pty: Box) -> Self { + pub fn new(terminal: Terminal, process: Box, pty: Box) -> Self { let tab_id = alloc_tab_id(); Self { tab_id, diff --git a/src/frontend/guicommon/window.rs b/src/frontend/guicommon/window.rs index f176bd51c..381c3c8b5 100644 --- a/src/frontend/guicommon/window.rs +++ b/src/frontend/guicommon/window.rs @@ -6,7 +6,7 @@ use crate::mux::window::WindowId; use crate::mux::Mux; use crate::opengl::render::Renderer; use crate::opengl::textureatlas::OutOfTextureSpace; -use crate::pty::{PtySize, PtySystem, ThePtySystem}; +use crate::pty::{PtySize, PtySystemSelection}; use failure::Error; use glium; use std::rc::Rc; @@ -162,7 +162,7 @@ pub trait TerminalWindow { let rows = (dims.height as usize + 1) / dims.cell_height; let cols = (dims.width as usize + 1) / dims.cell_width; - let pty_sys = ThePtySystem {}; + let pty_sys = PtySystemSelection::default().get()?; let (pty, slave) = pty_sys.openpty(PtySize { rows: rows as u16, cols: cols as u16, diff --git a/src/main.rs b/src/main.rs index d835ff8d2..52dd29eaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ mod font; use crate::font::{FontConfiguration, FontSystemSelection}; mod pty; -use pty::{PtySize, PtySystem, ThePtySystem}; +use pty::{PtySize, PtySystemSelection}; use std::env; /// Determine which shell to run. @@ -200,7 +200,7 @@ fn spawn_tab( let initial_pixel_width = 0; let initial_pixel_height = 0; - let pty_sys = ThePtySystem {}; + let pty_sys = PtySystemSelection::default().get()?; let (master, slave) = pty_sys.openpty(PtySize { rows: initial_rows, cols: initial_cols, diff --git a/src/pty/mod.rs b/src/pty/mod.rs index 945ff3902..9d73af008 100644 --- a/src/pty/mod.rs +++ b/src/pty/mod.rs @@ -17,7 +17,7 @@ pub struct PtySize { pub pixel_height: u16, } -pub trait MasterPtyTrait: std::io::Write { +pub trait MasterPty: std::io::Write { /// Inform the kernel and thus the child process that the window resized. /// It will update the winsize information maintained by the kernel, /// and generate a signal for the child to notice and update its state. @@ -26,14 +26,14 @@ pub trait MasterPtyTrait: std::io::Write { fn try_clone_reader(&self) -> Result, Error>; } -pub trait ChildTrait: std::fmt::Debug { +pub trait Child: std::fmt::Debug { fn try_wait(&mut self) -> IoResult>; fn kill(&mut self) -> IoResult<()>; fn wait(&mut self) -> IoResult; } -pub trait SlavePtyTrait { - fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error>; +pub trait SlavePty { + fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error>; } #[derive(Debug)] @@ -61,10 +61,10 @@ pub trait PtySystem { /// Create a new Pty instance with the window size set to the specified /// dimensions. Returns a (master, slave) Pty pair. The master side /// is used to drive the slave side. - fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error>; + fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error>; } -impl ChildTrait for std::process::Child { +impl Child for std::process::Child { fn try_wait(&mut self) -> IoResult> { std::process::Child::try_wait(self).map(|s| match s { Some(s) => Some(s.into()), @@ -81,7 +81,38 @@ impl ChildTrait for std::process::Child { } } -#[cfg(unix)] -pub use self::unix::UnixPtySystem as ThePtySystem; -#[cfg(windows)] -pub use self::win::conpty::ConPtySystem as ThePtySystem; +#[allow(dead_code)] +pub enum PtySystemSelection { + Unix, + ConPty, + WinPty, +} + +impl PtySystemSelection { + #[cfg(unix)] + pub fn get(&self) -> Result, Error> { + match self { + PtySystemSelection::Unix => Ok(Box::new(unix::UnixPtySystem {})), + _ => bail!("{:?} not available on unix"), + } + } + #[cfg(windows)] + pub fn get(&self) -> Result, Error> { + match self { + PtySystemSelection::ConPty => Ok(Box::new(win::conpty::ConPtySystem {})), + PtySystemSelection::WinPty => Ok(Box::new(win::winpty::WinPtySystem {})), + _ => bail!("{:?} not available on Windows"), + } + } +} + +impl Default for PtySystemSelection { + fn default() -> PtySystemSelection { + #[cfg(unix)] + return PtySystemSelection::Unix; + #[cfg(all(windows, not(feature = "use-winpty")))] + return PtySystemSelection::ConPty; + #[cfg(all(windows, feature = "use-winpty"))] + return PtySystemSelection::WinPty; + } +} diff --git a/src/pty/unix.rs b/src/pty/unix.rs index 6421e4164..73d0b4903 100644 --- a/src/pty/unix.rs +++ b/src/pty/unix.rs @@ -1,6 +1,6 @@ //! Working with pseudo-terminals -use crate::pty::{ChildTrait, CommandBuilder, MasterPtyTrait, PtySize, PtySystem, SlavePtyTrait}; +use crate::pty::{Child, CommandBuilder, MasterPty, PtySize, PtySystem, SlavePty}; use failure::Error; use libc::{self, winsize}; use std::io; @@ -10,11 +10,9 @@ use std::os::unix::process::CommandExt; use std::process::Stdio; use std::ptr; -pub use std::process::{Child, Command, ExitStatus}; - pub struct UnixPtySystem {} impl PtySystem for UnixPtySystem { - fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { + fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { let mut master: RawFd = -1; let mut slave: RawFd = -1; @@ -41,10 +39,10 @@ impl PtySystem for UnixPtySystem { bail!("failed to openpty: {:?}", io::Error::last_os_error()); } - let master = MasterPty { + let master = UnixMasterPty { fd: OwnedFd { fd: master }, }; - let slave = SlavePty { + let slave = UnixSlavePty { fd: OwnedFd { fd: slave }, }; @@ -135,13 +133,13 @@ impl io::Write for OwnedFd { /// Represents the master end of a pty. /// The file descriptor will be closed when the Pty is dropped. -pub struct MasterPty { +pub struct UnixMasterPty { fd: OwnedFd, } /// Represents the slave end of a pty. /// The file descriptor will be closed when the Pty is dropped. -pub struct SlavePty { +pub struct UnixSlavePty { fd: OwnedFd, } @@ -202,8 +200,8 @@ fn set_nonblocking(fd: RawFd) -> Result<(), Error> { Ok(()) } -impl SlavePtyTrait for SlavePty { - fn spawn_command(&self, builder: CommandBuilder) -> Result, Error> { +impl SlavePty for UnixSlavePty { + fn spawn_command(&self, builder: CommandBuilder) -> Result, Error> { let mut cmd = builder.as_command(); cmd.stdin(self.as_stdio()?) @@ -263,7 +261,7 @@ impl SlavePtyTrait for SlavePty { } } -impl SlavePty { +impl UnixSlavePty { /// Helper for setting up a Command instance fn as_stdio(&self) -> Result { let dup = self.fd.try_clone()?; @@ -271,7 +269,7 @@ impl SlavePty { } } -impl MasterPtyTrait for MasterPty { +impl MasterPty for UnixMasterPty { fn resize(&self, size: PtySize) -> Result<(), Error> { let ws_size = winsize { ws_row: size.rows, @@ -313,7 +311,7 @@ impl MasterPtyTrait for MasterPty { } } -impl io::Write for MasterPty { +impl io::Write for UnixMasterPty { fn write(&mut self, buf: &[u8]) -> Result { self.fd.write(buf) } diff --git a/src/pty/win/conpty.rs b/src/pty/win/conpty.rs index 0b1e4ead4..906ae70fa 100644 --- a/src/pty/win/conpty.rs +++ b/src/pty/win/conpty.rs @@ -1,7 +1,7 @@ use super::ownedhandle::OwnedHandle; -use super::Child; +use super::WinChild; use crate::pty::cmdbuilder::CommandBuilder; -use crate::pty::{ChildTrait, MasterPtyTrait, PtySize, PtySystem, SlavePtyTrait}; +use crate::pty::{Child, MasterPty, PtySize, PtySystem, SlavePty}; use failure::Error; use lazy_static::lazy_static; use shared_library::shared_library; @@ -26,7 +26,7 @@ const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE: usize = 0x00020016; pub struct ConPtySystem {} impl PtySystem for ConPtySystem { - fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { + fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { let (stdin_read, stdin_write) = pipe()?; let (stdout_read, stdout_write) = pipe()?; @@ -39,7 +39,7 @@ impl PtySystem for ConPtySystem { &stdout_write, )?; - let master = MasterPty { + let master = ConPtyMasterPty { inner: Arc::new(Mutex::new(Inner { con, readable: stdout_read, @@ -48,7 +48,7 @@ impl PtySystem for ConPtySystem { })), }; - let slave = SlavePty { + let slave = ConPtySlavePty { inner: master.inner.clone(), }; @@ -205,15 +205,15 @@ impl Inner { } #[derive(Clone)] -pub struct MasterPty { +pub struct ConPtyMasterPty { inner: Arc>, } -pub struct SlavePty { +pub struct ConPtySlavePty { inner: Arc>, } -impl MasterPtyTrait for MasterPty { +impl MasterPty for ConPtyMasterPty { fn resize(&self, size: PtySize) -> Result<(), Error> { let mut inner = self.inner.lock().unwrap(); inner.resize(size.rows, size.cols, size.pixel_width, size.pixel_height) @@ -229,7 +229,7 @@ impl MasterPtyTrait for MasterPty { } } -impl io::Write for MasterPty { +impl io::Write for ConPtyMasterPty { fn write(&mut self, buf: &[u8]) -> Result { self.inner.lock().unwrap().writable.write(buf) } @@ -238,8 +238,8 @@ impl io::Write for MasterPty { } } -impl SlavePtyTrait for SlavePty { - fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error> { +impl SlavePty for ConPtySlavePty { + fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error> { let inner = self.inner.lock().unwrap(); let mut si: STARTUPINFOEXW = unsafe { mem::zeroed() }; @@ -282,7 +282,7 @@ impl SlavePtyTrait for SlavePty { let _main_thread = OwnedHandle::new(pi.hThread); let proc = OwnedHandle::new(pi.hProcess); - Ok(Box::new(Child { proc })) + Ok(Box::new(WinChild { proc })) } } diff --git a/src/pty/win/mod.rs b/src/pty/win/mod.rs index 66d332c0f..e6f4b7518 100644 --- a/src/pty/win/mod.rs +++ b/src/pty/win/mod.rs @@ -1,4 +1,4 @@ -use crate::pty::{ChildTrait, ExitStatus}; +use crate::pty::{Child, ExitStatus}; use std::io::{Error as IoError, Result as IoResult}; use winapi::shared::minwindef::DWORD; use winapi::um::minwinbase::STILL_ACTIVE; @@ -14,24 +14,12 @@ pub mod ownedhandle; use ownedhandle::OwnedHandle; #[derive(Debug)] -pub struct Child { +pub struct WinChild { proc: OwnedHandle, } -impl ChildTrait for Child { +impl Child for WinChild { fn try_wait(&mut self) -> IoResult> { - Child::try_wait(self) - } - fn kill(&mut self) -> IoResult<()> { - Child::kill(self) - } - fn wait(&mut self) -> IoResult { - Child::wait(self) - } -} - -impl Child { - pub fn try_wait(&mut self) -> IoResult> { let mut status: DWORD = 0; let res = unsafe { GetExitCodeProcess(self.proc.handle, &mut status) }; if res != 0 { @@ -45,7 +33,7 @@ impl Child { } } - pub fn kill(&mut self) -> IoResult<()> { + fn kill(&mut self) -> IoResult<()> { unsafe { TerminateProcess(self.proc.handle, 1); } @@ -53,7 +41,7 @@ impl Child { Ok(()) } - pub fn wait(&mut self) -> IoResult { + fn wait(&mut self) -> IoResult { if let Ok(Some(status)) = self.try_wait() { return Ok(status); } diff --git a/src/pty/win/winpty/mod.rs b/src/pty/win/winpty/mod.rs index 86954a5c3..0b7f2b5a5 100644 --- a/src/pty/win/winpty/mod.rs +++ b/src/pty/win/winpty/mod.rs @@ -1,7 +1,7 @@ use super::ownedhandle::OwnedHandle; -use super::Child; +use super::WinChild; use crate::pty::cmdbuilder::CommandBuilder; -use crate::pty::{ChildTrait, MasterPtyTrait, PtySize, PtySystem, SlavePtyTrait}; +use crate::pty::{Child, MasterPty, PtySize, PtySystem, SlavePty}; use failure::Error; use safe::{AgentFlags, MouseMode, SpawnConfig, SpawnFlags, Timeout, WinPty, WinPtyConfig}; use std::ffi::OsString; @@ -20,22 +20,22 @@ struct Inner { } #[derive(Clone)] -pub struct MasterPty { +pub struct WinPtyMasterPty { inner: Arc>, } -pub struct SlavePty { +pub struct WinPtySlavePty { inner: Arc>, } -impl MasterPtyTrait for MasterPty { +impl MasterPty for WinPtyMasterPty { fn resize(&self, size: PtySize) -> Result<(), Error> { let mut inner = self.inner.lock().unwrap(); if inner.pty.set_size(size.cols as i32, size.rows as i32)? { inner.size = size; Ok(()) } else { - bail!("MasterPty::resize returned false"); + bail!("WinPtyMasterPty::resize returned false"); } } @@ -48,7 +48,7 @@ impl MasterPtyTrait for MasterPty { } } -impl std::io::Write for MasterPty { +impl std::io::Write for WinPtyMasterPty { fn write(&mut self, buf: &[u8]) -> Result { self.inner.lock().unwrap().writer.write(buf) } @@ -57,8 +57,8 @@ impl std::io::Write for MasterPty { } } -impl SlavePtyTrait for SlavePty { - fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error> { +impl SlavePty for WinPtySlavePty { + fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error> { let (exe, cmdline) = cmd.cmdline()?; let cmd_os = OsString::from_wide(&cmdline); eprintln!( @@ -78,7 +78,7 @@ impl SlavePtyTrait for SlavePty { let mut inner = self.inner.lock().unwrap(); let spawned = inner.pty.spawn(&spawn_config)?; - let child = Child { + let child = WinChild { proc: spawned.process_handle, }; @@ -88,7 +88,7 @@ impl SlavePtyTrait for SlavePty { pub struct WinPtySystem {} impl PtySystem for WinPtySystem { - fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { + fn openpty(&self, size: PtySize) -> Result<(Box, Box), Error> { let mut config = WinPtyConfig::new(AgentFlags::empty())?; config.set_initial_size(size.cols as i32, size.rows as i32); @@ -107,10 +107,10 @@ impl PtySystem for WinPtySystem { size, })); - let master = MasterPty { + let master = WinPtyMasterPty { inner: Arc::clone(&inner), }; - let slave = SlavePty { inner }; + let slave = WinPtySlavePty { inner }; Ok((Box::new(master), Box::new(slave))) }