From 416311892bae87e2a82ccbf9ec7620d9f4caab4b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 25 Mar 2019 07:25:43 -0700 Subject: [PATCH] windows: extract Child --- src/pty/mod.rs | 4 ++- src/pty/win/conpty.rs | 51 +------------------------------------ src/pty/win/mod.rs | 58 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/pty/mod.rs b/src/pty/mod.rs index 5b5bec2dd..ce674fd7c 100644 --- a/src/pty/mod.rs +++ b/src/pty/mod.rs @@ -6,4 +6,6 @@ pub mod win; #[cfg(unix)] pub use self::unix::{openpty, Child, Command, ExitStatus, MasterPty, SlavePty}; #[cfg(windows)] -pub use self::win::conpty::{openpty, Child, Command, ExitStatus, MasterPty, SlavePty}; +pub use self::win::conpty::{openpty, Command, MasterPty, SlavePty}; +#[cfg(windows)] +pub use self::win::{Child, ExitStatus}; diff --git a/src/pty/win/conpty.rs b/src/pty/win/conpty.rs index eb177496b..c32785786 100644 --- a/src/pty/win/conpty.rs +++ b/src/pty/win/conpty.rs @@ -1,6 +1,7 @@ use super::cmdline::CommandBuilder; use super::ownedhandle::OwnedHandle; use super::winsize; +use super::{Child, ExitStatus}; use failure::Error; use lazy_static::lazy_static; use shared_library::shared_library; @@ -17,7 +18,6 @@ use winapi::shared::minwindef::DWORD; use winapi::shared::winerror::{HRESULT, S_OK}; use winapi::um::fileapi::WriteFile; use winapi::um::handleapi::*; -use winapi::um::minwinbase::STILL_ACTIVE; use winapi::um::namedpipeapi::CreatePipe; use winapi::um::processthreadsapi::*; use winapi::um::synchapi::WaitForSingleObject; @@ -187,55 +187,6 @@ impl Drop for ProcThreadAttributeList { } } -#[derive(Debug)] -pub struct Child { - proc: OwnedHandle, -} - -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 { - if status == STILL_ACTIVE { - Ok(None) - } else { - Ok(Some(ExitStatus { status })) - } - } else { - Ok(None) - } - } - - pub fn kill(&mut self) -> IoResult { - unsafe { - TerminateProcess(self.proc.handle, 1); - } - self.wait() - } - - pub fn wait(&mut self) -> IoResult { - if let Ok(Some(status)) = self.try_wait() { - return Ok(status); - } - unsafe { - WaitForSingleObject(self.proc.handle, INFINITE); - } - let mut status: DWORD = 0; - let res = unsafe { GetExitCodeProcess(self.proc.handle, &mut status) }; - if res != 0 { - Ok(ExitStatus { status }) - } else { - Err(IoError::last_os_error()) - } - } -} - -#[derive(Debug)] -pub struct ExitStatus { - status: DWORD, -} - type HPCON = HANDLE; shared_library!(ConPtyFuncs, diff --git a/src/pty/win/mod.rs b/src/pty/win/mod.rs index 24a707bb6..35f419dac 100644 --- a/src/pty/win/mod.rs +++ b/src/pty/win/mod.rs @@ -1,8 +1,17 @@ +use std::io::{self, Error as IoError, Result as IoResult}; +use winapi::shared::minwindef::DWORD; +use winapi::um::minwinbase::STILL_ACTIVE; +use winapi::um::processthreadsapi::*; +use winapi::um::synchapi::WaitForSingleObject; +use winapi::um::winbase::INFINITE; + pub mod cmdline; pub mod conpty; pub mod ownedhandle; pub mod winpty; +use ownedhandle::OwnedHandle; + #[derive(Debug, Clone, Copy)] #[allow(non_camel_case_types)] pub struct winsize { @@ -11,3 +20,52 @@ pub struct winsize { pub ws_xpixel: u16, pub ws_ypixel: u16, } + +#[derive(Debug)] +pub struct Child { + proc: OwnedHandle, +} + +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 { + if status == STILL_ACTIVE { + Ok(None) + } else { + Ok(Some(ExitStatus { status })) + } + } else { + Ok(None) + } + } + + pub fn kill(&mut self) -> IoResult { + unsafe { + TerminateProcess(self.proc.handle, 1); + } + self.wait() + } + + pub fn wait(&mut self) -> IoResult { + if let Ok(Some(status)) = self.try_wait() { + return Ok(status); + } + unsafe { + WaitForSingleObject(self.proc.handle, INFINITE); + } + let mut status: DWORD = 0; + let res = unsafe { GetExitCodeProcess(self.proc.handle, &mut status) }; + if res != 0 { + Ok(ExitStatus { status }) + } else { + Err(IoError::last_os_error()) + } + } +} + +#[derive(Debug)] +pub struct ExitStatus { + status: DWORD, +}