Skip to content

Commit

Permalink
Update calls to use the latest Windows API
Browse files Browse the repository at this point in the history
  • Loading branch information
andfoy committed Sep 16, 2022
1 parent 522b023 commit 90d2801
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
50 changes: 30 additions & 20 deletions src/pty/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,16 @@ fn read(mut length: u32, blocking: bool, stream: HANDLE, using_pipes: bool) -> R
if !blocking {
if using_pipes {
let mut bytes_u = MaybeUninit::<u32>::uninit();
let bytes_ptr = bytes_u.as_mut_ptr();

//let mut available_bytes = Box::<>::new_uninit();
//let bytes_ptr: *mut u32 = &mut *available_bytes;
unsafe {
let bytes_ptr = ptr::addr_of_mut!(*bytes_u.as_mut_ptr());
let bytes_ref = bytes_ptr.as_mut().unwrap();

result =
if PeekNamedPipe(stream, ptr::null_mut::<c_void>(), 0,
ptr::null_mut::<u32>(), bytes_ptr, ptr::null_mut::<u32>()).as_bool() {
if PeekNamedPipe(stream, None,
None, Some(bytes_ref), None).as_bool() {
S_OK
} else {
Error::from_win32().into()
Expand All @@ -158,7 +161,9 @@ fn read(mut length: u32, blocking: bool, stream: HANDLE, using_pipes: bool) -> R
// let size_ptr: *mut i64 = ptr::null_mut();
unsafe {
let size_ptr = ptr::addr_of_mut!(*size.as_mut_ptr());
result = if GetFileSizeEx(stream, size_ptr).as_bool() { S_OK } else { Error::from_win32().into() };
let size_ref = size_ptr.as_mut().unwrap();
// let size_ref = *size.as_mut_ptr();
result = if GetFileSizeEx(stream, size_ref).as_bool() { S_OK } else { Error::from_win32().into() };

if result.is_err() {
let result_msg = result.message();
Expand Down Expand Up @@ -190,10 +195,12 @@ fn read(mut length: u32, blocking: bool, stream: HANDLE, using_pipes: bool) -> R
_ => {
let buf_ptr = buf_vec.as_mut_ptr();
let buf_void = buf_ptr as *mut c_void;
// let chars_read_ptr = chars_read.as_mut_ptr();
let chars_read_ptr = ptr::addr_of_mut!(*chars_read.as_mut_ptr());
let chars_read_mut = chars_read_ptr.as_mut();
// println!("Blocked here");
result =
if ReadFile(stream, buf_void, length, chars_read_ptr, null_overlapped).as_bool() {
if ReadFile(stream, Some(&mut buf_vec[..]), chars_read_mut, None).as_bool() {
S_OK
} else {
Error::from_win32().into()
Expand All @@ -218,7 +225,8 @@ fn read(mut length: u32, blocking: bool, stream: HANDLE, using_pipes: bool) -> R

unsafe {
MultiByteToWideChar(
CP_UTF8, MULTI_BYTE_TO_WIDE_CHAR_FLAGS(0), &buf_vec[..], &mut vec_buf[..]);
CP_UTF8, MULTI_BYTE_TO_WIDE_CHAR_FLAGS(0), &buf_vec[..],
Some(&mut vec_buf[..]));
}

// let non_zeros: Vec<u16> = vec_buf.split(|elem| *elem == 0 as u16).collect();
Expand All @@ -239,10 +247,10 @@ fn read(mut length: u32, blocking: bool, stream: HANDLE, using_pipes: bool) -> R
fn is_alive(process: HANDLE) -> Result<bool, OsString> {
unsafe {
let is_timeout = WaitForSingleObject(process, 0);
let succ = is_timeout != WAIT_FAILED.0;
let succ = is_timeout != WAIT_FAILED;

if succ {
let alive = is_timeout == WAIT_TIMEOUT.0;
let alive = is_timeout == WAIT_TIMEOUT;
Ok(alive)
} else {
let err: HRESULT = Error::from_win32().into();
Expand All @@ -258,7 +266,8 @@ fn get_exitstatus(process: HANDLE) -> Result<Option<u32>, OsString> {
let mut exit = MaybeUninit::<u32>::uninit();
unsafe {
let exit_ptr: *mut u32 = ptr::addr_of_mut!(*exit.as_mut_ptr());
let succ = GetExitCodeProcess(process, exit_ptr).as_bool();
let exit_ref = exit_ptr.as_mut().unwrap();
let succ = GetExitCodeProcess(process, exit_ref).as_bool();

if succ {
let actual_exit = *exit_ptr;
Expand All @@ -283,9 +292,9 @@ fn is_eof(process: HANDLE, stream: HANDLE) -> Result<bool, OsString> {
let mut bytes = MaybeUninit::<u32>::uninit();
unsafe {
let bytes_ptr: *mut u32 = ptr::addr_of_mut!(*bytes.as_mut_ptr());
let bytes_ref = bytes_ptr.as_mut();
let succ = PeekNamedPipe(
stream, ptr::null_mut::<c_void>(), 0,
ptr::null_mut::<u32>(), bytes_ptr, ptr::null_mut::<u32>()).as_bool();
stream, None, None, bytes_ref, None).as_bool();

let total_bytes = bytes.assume_init();
if succ {
Expand Down Expand Up @@ -486,23 +495,24 @@ impl PTYProcess {

unsafe {
let required_size = WideCharToMultiByte(
CP_UTF8, 0, &vec_buf[..], PSTR(ptr::null_mut::<u8>()),
0, PCSTR(ptr::null_mut::<u8>()), ptr::null_mut::<i32>());
CP_UTF8, 0, &vec_buf[..], None,
PCSTR(ptr::null_mut::<u8>()), None);

let mut bytes_buf: Vec<u8> = std::iter::repeat(0).take((required_size) as usize).collect();
let bytes_buf_ptr = bytes_buf.as_mut_ptr();
let pstr = PSTR(bytes_buf_ptr);

WideCharToMultiByte(
CP_UTF8, 0, &vec_buf[..], pstr,
required_size, PCSTR(ptr::null_mut::<u8>()),
ptr::null_mut::<i32>());
CP_UTF8, 0, &vec_buf[..], Some(&mut bytes_buf[..]),
PCSTR(ptr::null_mut::<u8>()),
None);

let mut written_bytes = MaybeUninit::<u32>::uninit();
let bytes_ptr: *mut u32 = ptr::addr_of_mut!(*written_bytes.as_mut_ptr());
let bytes_ref = bytes_ptr.as_mut();

result =
if WriteFile(self.conin, bytes_buf[..].as_ptr() as *const c_void, bytes_buf.len() as u32, bytes_ptr, null_overlapped).as_bool() {
if WriteFile(self.conin, Some(&bytes_buf[..]), bytes_ref, None).as_bool() {
S_OK
} else {
Error::from_win32().into()
Expand Down Expand Up @@ -531,9 +541,9 @@ impl PTYProcess {
let mut bytes = MaybeUninit::<u32>::uninit();
unsafe {
let bytes_ptr: *mut u32 = ptr::addr_of_mut!(*bytes.as_mut_ptr());
let bytes_ref = bytes_ptr.as_mut();
let mut succ = PeekNamedPipe(
self.conout, ptr::null_mut::<c_void>(), 0,
ptr::null_mut::<u32>(), bytes_ptr, ptr::null_mut::<u32>()).as_bool();
self.conout, None, None, bytes_ref, None).as_bool();

let total_bytes = bytes.assume_init();

Expand Down Expand Up @@ -624,7 +634,7 @@ impl Drop for PTYProcess {
if self.reader_process_out.send(None).is_ok() { }

// Cancel all pending IO operations on conout
CancelIoEx(self.conout, ptr::null());
CancelIoEx(self.conout, None);

// Send instruction to thread to finish
if self.reader_alive.send(false).is_ok() { }
Expand Down
27 changes: 15 additions & 12 deletions src/pty/conpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl PTYImpl for ConPTY {
let h_console_res = CreateFileW(
conout_pwstr, FILE_GENERIC_READ | FILE_GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ptr::null(), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, HANDLE(0));
None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, HANDLE(0));

if let Err(err) = h_console_res {
let result_msg = err.message();
Expand All @@ -89,7 +89,7 @@ impl PTYImpl for ConPTY {
let h_in_res = CreateFileW(
conin_pwstr,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
FILE_SHARE_READ, ptr::null(),
FILE_SHARE_READ, None,
OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES(0),
HANDLE(0));

Expand All @@ -103,10 +103,10 @@ impl PTYImpl for ConPTY {
let h_in = h_in_res.unwrap();

let mut console_mode_un = MaybeUninit::<CONSOLE_MODE>::uninit();
let console_mode_ptr = console_mode_un.as_mut_ptr();
let console_mode_ref = console_mode_un.as_mut_ptr();

result =
if GetConsoleMode(h_console, console_mode_ptr).as_bool() {
if GetConsoleMode(h_console, console_mode_ref.as_mut().unwrap()).as_bool() {
S_OK
} else {
Error::from_win32().into()
Expand Down Expand Up @@ -175,15 +175,15 @@ impl PTYImpl for ConPTY {
// Setup PTY size
let size = COORD {X: args.cols as i16, Y: args.rows as i16};

if !CreatePipe(&mut input_read_side, &mut input_write_side, ptr::null(), 0).as_bool() {
if !CreatePipe(&mut input_read_side, &mut input_write_side, None, 0).as_bool() {
result = Error::from_win32().into();
let result_msg = result.message();
let err_msg: &[u16] = result_msg.as_wide();
let string = OsString::from_wide(err_msg);
return Err(string);
}

if !CreatePipe(&mut output_read_side, &mut output_write_side, ptr::null(), 0).as_bool() {
if !CreatePipe(&mut output_read_side, &mut output_write_side, None, 0).as_bool() {
result = Error::from_win32().into();
let result_msg = result.message();
let err_msg: &[u16] = result_msg.as_wide();
Expand Down Expand Up @@ -255,7 +255,8 @@ impl PTYImpl for ConPTY {
let mut required_bytes_u = MaybeUninit::<usize>::uninit();
let required_bytes_ptr = required_bytes_u.as_mut_ptr();
InitializeProcThreadAttributeList(
LPPROC_THREAD_ATTRIBUTE_LIST(ptr::null_mut()), 1, 0, required_bytes_ptr);
LPPROC_THREAD_ATTRIBUTE_LIST(ptr::null_mut()), 1, 0,
required_bytes_ptr.as_mut().unwrap());

// Allocate memory to represent the list
let mut required_bytes = required_bytes_u.assume_init();
Expand Down Expand Up @@ -285,7 +286,7 @@ impl PTYImpl for ConPTY {
if !UpdateProcThreadAttribute(
start_info.lpAttributeList, 0, 0x00020016,
self.handle.0 as _, mem::size_of::<HPCON>(),
ptr::null_mut(), ptr::null_mut()).as_bool() {
ptr::null_mut(), None).as_bool() {
result = Error::from_win32().into();
let result_msg = result.message();
let err_msg: &[u16] = result_msg.as_wide();
Expand All @@ -296,18 +297,20 @@ impl PTYImpl for ConPTY {
self.startup_info = start_info;
let pi_ptr = &mut self.process_info as *mut _;
let si_ptr = &start_info as *const STARTUPINFOEXW;
let si_ptr_addr = si_ptr as usize;
let si_w_ptr = si_ptr_addr as *const STARTUPINFOW;

let succ = CreateProcessW(
PCWSTR(ptr::null_mut()),
PWSTR(cmd),
ptr::null_mut(),
ptr::null_mut(),
None,
None,
false,
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT,
environ as _,
PCWSTR(working_dir),
si_ptr as *const _,
pi_ptr
si_w_ptr.as_ref().unwrap(),
&mut self.process_info
).as_bool();

if !succ {
Expand Down
4 changes: 2 additions & 2 deletions src/pty/winpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl PTYImpl for WinPTY {

let empty_handle = HANDLE(0);
let conin_res = CreateFileW(
PCWSTR(conin_name as *const u16), FILE_GENERIC_WRITE, FILE_SHARE_NONE, ptr::null(),
PCWSTR(conin_name as *const u16), FILE_GENERIC_WRITE, FILE_SHARE_NONE, None,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, empty_handle
);

Expand All @@ -182,7 +182,7 @@ impl PTYImpl for WinPTY {
}

let conout_res = CreateFileW(
PCWSTR(conout_name as *mut u16), FILE_GENERIC_READ, FILE_SHARE_NONE, ptr::null(),
PCWSTR(conout_name as *mut u16), FILE_GENERIC_READ, FILE_SHARE_NONE, None,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, empty_handle
);

Expand Down

0 comments on commit 90d2801

Please sign in to comment.