Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from winapi to windows-sys #49

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions projects/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/rpassword/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.60"
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"] }
windows-sys = { version = "0.42.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_Storage_FileSystem", "Win32_Security", "Win32_System_SystemServices"] }

[dependencies]
rtoolbox = { path = "../rtoolbox", version = "0.0" }
27 changes: 14 additions & 13 deletions projects/rpassword/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ mod unix {

#[cfg(target_family = "windows")]
mod windows {
use std::io::{self, BufReader};
use std::io::BufRead;
use std::io::{self, BufReader};
use std::os::windows::io::FromRawHandle;
use winapi::shared::minwindef::LPDWORD;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::wincon::{ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT};
use winapi::um::winnt::{
FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE, HANDLE,
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::Storage::FileSystem::{
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::Console::{
GetConsoleMode, SetConsoleMode, CONSOLE_MODE, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
};
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};

struct HiddenInput {
mode: u32,
Expand All @@ -162,7 +163,7 @@ mod windows {
let mut mode = 0;

// Get the old mode so we can reset back to it when we are done
if unsafe { GetConsoleMode(handle, &mut mode as LPDWORD) } == 0 {
if unsafe { GetConsoleMode(handle, &mut mode as *mut CONSOLE_MODE) } == 0 {
return Err(std::io::Error::last_os_error());
}

Expand All @@ -189,21 +190,21 @@ mod windows {
pub fn read_password() -> std::io::Result<String> {
let handle = unsafe {
CreateFileA(
b"CONIN$\x00".as_ptr() as *const i8,
b"CONIN$\x00".as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null_mut(),
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
INVALID_HANDLE_VALUE,
)
};

if handle == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error());
}

let mut stream = BufReader::new(unsafe { std::fs::File::from_raw_handle(handle) });
let mut stream = BufReader::new(unsafe { std::fs::File::from_raw_handle(handle as _) });
read_password_from_handle_with_hidden_input(&mut stream, handle)
}

Expand Down
7 changes: 2 additions & 5 deletions projects/rpassword/tests/no-terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ use std::io::Cursor;

use rpassword::read_password_from_bufread;

#[cfg(unix)]
#[cfg(unix)]
fn close_stdin() {
unsafe {
libc::close(libc::STDIN_FILENO);
}
}

#[cfg(windows)]
#[cfg(windows)]
fn close_stdin() {
use winapi::um::handleapi::CloseHandle;
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::STD_INPUT_HANDLE;
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Console::{GetStdHandle, STD_INPUT_HANDLE};

unsafe {
CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
Expand Down
6 changes: 3 additions & 3 deletions projects/rprompt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ edition = "2018"
[dependencies]
rtoolbox = { path = "../rtoolbox", version = "0.0" }

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"]
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.42"
features = ["Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Console", "Win32_System_SystemServices"]
18 changes: 10 additions & 8 deletions projects/rprompt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ fn get_tty_reader() -> std::io::Result<impl BufRead> {
#[cfg(windows)]
fn get_tty_reader() -> std::io::Result<impl BufRead> {
use std::os::windows::io::FromRawHandle;
use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::Storage::FileSystem::{
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};

let handle = unsafe {
CreateFileA(
b"CONIN$\x00".as_ptr() as *const i8,
b"CONIN$\x00".as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null_mut(),
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
INVALID_HANDLE_VALUE,
)
};

Expand All @@ -86,7 +89,6 @@ fn get_tty_reader() -> std::io::Result<impl BufRead> {
}

Ok(BufReader::new(unsafe {
std::fs::File::from_raw_handle(handle)
std::fs::File::from_raw_handle(handle as _)
}))
}

6 changes: 3 additions & 3 deletions projects/rtoolbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ serde_json = { version = "1.0", optional = true }
[target.'cfg(unix)'.dependencies.libc]
version = "0.2"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"]
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.42"
features = ["Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Console", "Win32_System_SystemServices"]
28 changes: 13 additions & 15 deletions projects/rtoolbox/src/atty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// included in all copies or substantial portions of the Software.

#[cfg(windows)]
use winapi::shared::minwindef::DWORD;
use windows_sys::Win32::System::Console::STD_HANDLE;

#[cfg(windows)]
use winapi::shared::ntdef::WCHAR;
type WCHAR = u16;

/// possible stream sources
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -49,7 +50,7 @@ pub fn is(stream: Stream) -> bool {
/// returns true if this is a tty
#[cfg(windows)]
pub fn is(stream: Stream) -> bool {
use winapi::um::winbase::{
use windows_sys::Win32::System::Console::{
STD_ERROR_HANDLE as STD_ERROR, STD_INPUT_HANDLE as STD_INPUT,
STD_OUTPUT_HANDLE as STD_OUTPUT,
};
Expand Down Expand Up @@ -85,8 +86,8 @@ pub fn isnt(stream: Stream) -> bool {

/// Returns true if any of the given fds are on a console.
#[cfg(windows)]
unsafe fn console_on_any(fds: &[DWORD]) -> bool {
use winapi::um::{consoleapi::GetConsoleMode, processenv::GetStdHandle};
unsafe fn console_on_any(fds: &[STD_HANDLE]) -> bool {
use windows_sys::Win32::System::Console::{GetConsoleMode, GetStdHandle};

for &fd in fds {
let mut out = 0;
Expand All @@ -100,20 +101,17 @@ unsafe fn console_on_any(fds: &[DWORD]) -> bool {

/// Returns true if there is an MSYS tty on the given handle.
#[cfg(windows)]
unsafe fn msys_tty_on(fd: DWORD) -> bool {
unsafe fn msys_tty_on(fd: STD_HANDLE) -> bool {
use std::os::raw::c_void;
use std::{mem, slice};

use winapi::{
ctypes::c_void,
shared::minwindef::MAX_PATH,
um::{
fileapi::FILE_NAME_INFO, minwinbase::FileNameInfo, processenv::GetStdHandle,
winbase::GetFileInformationByHandleEx,
},
};
use windows_sys::Win32::Foundation::MAX_PATH;
use windows_sys::Win32::Storage::FileSystem::GetFileInformationByHandleEx;
use windows_sys::Win32::Storage::FileSystem::{FileNameInfo, FILE_NAME_INFO};
use windows_sys::Win32::System::Console::GetStdHandle;

let size = mem::size_of::<FILE_NAME_INFO>();
let mut name_info_bytes = vec![0u8; size + MAX_PATH * mem::size_of::<WCHAR>()];
let mut name_info_bytes = vec![0u8; size + MAX_PATH as usize * mem::size_of::<WCHAR>()];
let res = GetFileInformationByHandleEx(
GetStdHandle(fd),
FileNameInfo,
Expand Down
17 changes: 10 additions & 7 deletions projects/rtoolbox/src/print_tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,31 @@ mod unix {
mod windows {
use std::io::Write;
use std::os::windows::io::FromRawHandle;
use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE};
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::Storage::FileSystem::{
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};

/// Displays a message on the TTY
pub fn print_tty(prompt: impl ToString) -> std::io::Result<()> {
let handle = unsafe {
CreateFileA(
b"CONOUT$\x00".as_ptr() as *const i8,
b"CONOUT$\x00".as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null_mut(),
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
INVALID_HANDLE_VALUE,
)
};
if handle == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error());
}

let mut stream = unsafe { std::fs::File::from_raw_handle(handle) };
let mut stream = unsafe { std::fs::File::from_raw_handle(handle as _) };

stream
.write_all(prompt.to_string().as_str().as_bytes())
Expand Down