1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 22:33:52 +03:00

windows: extract Child

This commit is contained in:
Wez Furlong 2019-03-25 07:25:43 -07:00
parent 7875bc4e1a
commit 416311892b
3 changed files with 62 additions and 51 deletions

View File

@ -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};

View File

@ -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<Option<ExitStatus>> {
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<ExitStatus> {
unsafe {
TerminateProcess(self.proc.handle, 1);
}
self.wait()
}
pub fn wait(&mut self) -> IoResult<ExitStatus> {
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,

View File

@ -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<Option<ExitStatus>> {
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<ExitStatus> {
unsafe {
TerminateProcess(self.proc.handle, 1);
}
self.wait()
}
pub fn wait(&mut self) -> IoResult<ExitStatus> {
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,
}