rename pty traits, make selectable via enum
This commit is contained in:
1 parent
10165fac32
commit
cac06e5506
8 files changed
+90
-73
No files matched your search
@@ -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<Terminal>,
|
||||
process: RefCell<Box<ChildTrait>>,
|
||||
pty: RefCell<Box<MasterPtyTrait>>,
|
||||
process: RefCell<Box<Child>>,
|
||||
pty: RefCell<Box<MasterPty>>,
|
||||
}
|
||||
|
||||
impl Tab for LocalTab {
|
||||
@@ -83,7 +83,7 @@ impl Tab for LocalTab {
|
||||
}
|
||||
|
||||
impl LocalTab {
|
||||
pub fn new(terminal: Terminal, process: Box<ChildTrait>, pty: Box<MasterPtyTrait>) -> Self {
|
||||
pub fn new(terminal: Terminal, process: Box<Child>, pty: Box<MasterPty>) -> Self {
|
||||
let tab_id = alloc_tab_id();
|
||||
Self {
|
||||
tab_id,
|
||||
|
||||
@@ -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,
|
||||
|
||||
+2
-2
@@ -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,
|
||||
|
||||
+41
-10
@@ -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<Box<std::io::Read + Send>, Error>;
|
||||
}
|
||||
|
||||
pub trait ChildTrait: std::fmt::Debug {
|
||||
pub trait Child: std::fmt::Debug {
|
||||
fn try_wait(&mut self) -> IoResult<Option<ExitStatus>>;
|
||||
fn kill(&mut self) -> IoResult<()>;
|
||||
fn wait(&mut self) -> IoResult<ExitStatus>;
|
||||
}
|
||||
|
||||
pub trait SlavePtyTrait {
|
||||
fn spawn_command(&self, cmd: CommandBuilder) -> Result<Box<ChildTrait>, Error>;
|
||||
pub trait SlavePty {
|
||||
fn spawn_command(&self, cmd: CommandBuilder) -> Result<Box<Child>, 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<MasterPtyTrait>, Box<SlavePtyTrait>), Error>;
|
||||
fn openpty(&self, size: PtySize) -> Result<(Box<MasterPty>, Box<SlavePty>), Error>;
|
||||
}
|
||||
|
||||
impl ChildTrait for std::process::Child {
|
||||
impl Child for std::process::Child {
|
||||
fn try_wait(&mut self) -> IoResult<Option<ExitStatus>> {
|
||||
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<Box<PtySystem>, Error> {
|
||||
match self {
|
||||
PtySystemSelection::Unix => Ok(Box::new(unix::UnixPtySystem {})),
|
||||
_ => bail!("{:?} not available on unix"),
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
pub fn get(&self) -> Result<Box<PtySystem>, 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;
|
||||
}
|
||||
}
|
||||
+11
-13
@@ -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<MasterPtyTrait>, Box<SlavePtyTrait>), Error> {
|
||||
fn openpty(&self, size: PtySize) -> Result<(Box<MasterPty>, Box<SlavePty>), 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<Box<ChildTrait>, Error> {
|
||||
impl SlavePty for UnixSlavePty {
|
||||
fn spawn_command(&self, builder: CommandBuilder) -> Result<Box<Child>, 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<Stdio, Error> {
|
||||
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<usize, io::Error> {
|
||||
self.fd.write(buf)
|
||||
}
|
||||
|
||||
+12
-12
@@ -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<MasterPtyTrait>, Box<SlavePtyTrait>), Error> {
|
||||
fn openpty(&self, size: PtySize) -> Result<(Box<MasterPty>, Box<SlavePty>), 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<Mutex<Inner>>,
|
||||
}
|
||||
|
||||
pub struct SlavePty {
|
||||
pub struct ConPtySlavePty {
|
||||
inner: Arc<Mutex<Inner>>,
|
||||
}
|
||||
|
||||
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<usize, io::Error> {
|
||||
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<Box<ChildTrait>, Error> {
|
||||
impl SlavePty for ConPtySlavePty {
|
||||
fn spawn_command(&self, cmd: CommandBuilder) -> Result<Box<Child>, 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 }))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-17
@@ -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<Option<ExitStatus>> {
|
||||
Child::try_wait(self)
|
||||
}
|
||||
fn kill(&mut self) -> IoResult<()> {
|
||||
Child::kill(self)
|
||||
}
|
||||
fn wait(&mut self) -> IoResult<ExitStatus> {
|
||||
Child::wait(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Child {
|
||||
pub fn try_wait(&mut self) -> IoResult<Option<ExitStatus>> {
|
||||
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<ExitStatus> {
|
||||
fn wait(&mut self) -> IoResult<ExitStatus> {
|
||||
if let Ok(Some(status)) = self.try_wait() {
|
||||
return Ok(status);
|
||||
}
|
||||
|
||||
+13
-13
@@ -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<Mutex<Inner>>,
|
||||
}
|
||||
|
||||
pub struct SlavePty {
|
||||
pub struct WinPtySlavePty {
|
||||
inner: Arc<Mutex<Inner>>,
|
||||
}
|
||||
|
||||
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<usize, std::io::Error> {
|
||||
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<Box<ChildTrait>, Error> {
|
||||
impl SlavePty for WinPtySlavePty {
|
||||
fn spawn_command(&self, cmd: CommandBuilder) -> Result<Box<Child>, 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<MasterPtyTrait>, Box<SlavePtyTrait>), Error> {
|
||||
fn openpty(&self, size: PtySize) -> Result<(Box<MasterPty>, Box<SlavePty>), 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)))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user