Skip to content

Commit 064cca7

Browse files
committed
Remove windows-sys dependency
Use the windows api directly with raw-dylib
1 parent 21a47e6 commit 064cca7

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "passterm"
3-
version = "2.0.2"
3+
version = "2.0.3"
44
edition = "2021"
55
authors = [
66
"Kyle Schreiber <[email protected]>",
@@ -17,9 +17,6 @@ categories = ["command-line-interface"]
1717
[dependencies]
1818
zeroize = { version = "1.7", optional = true }
1919

20-
[target.'cfg(windows)'.dependencies]
21-
windows-sys = { version = "0.52.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_Storage_FileSystem", "Win32_Security"] }
22-
2320
[target.'cfg(unix)'.dependencies]
2421
libc = "0.2"
2522

src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
1414
mod tty;
1515

16+
#[cfg(target_family = "windows")]
17+
mod win32;
18+
1619
pub use crate::tty::Stream;
1720
use std::error::Error;
1821
use std::io::Read;
@@ -186,14 +189,15 @@ fn find_lf(input: &[u8]) -> Option<usize> {
186189
#[cfg(target_family = "windows")]
187190
mod windows {
188191
use crate::{find_crlf, print_stream, strip_newline, PromptError, Stream};
189-
190-
use windows_sys::Win32::Foundation::{
191-
CloseHandle, BOOL, FALSE, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE,
192+
use crate::win32::{
193+
CreateFileA, GetFileType, CloseHandle,
194+
GetConsoleMode, GetStdHandle, ReadConsoleW,
195+
SetConsoleMode, WriteConsoleW
192196
};
193-
use windows_sys::Win32::Storage::FileSystem::{CreateFileA, GetFileType, OPEN_EXISTING};
194-
use windows_sys::Win32::System::Console::{
195-
GetConsoleMode, GetStdHandle, ReadConsoleW, SetConsoleMode, WriteConsoleW, CONSOLE_MODE,
196-
ENABLE_ECHO_INPUT, STD_INPUT_HANDLE,
197+
use crate::win32::{
198+
HANDLE, BOOL, FALSE, OPEN_EXISTING,
199+
GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE,
200+
ENABLE_ECHO_INPUT, STD_INPUT_HANDLE
197201
};
198202

199203
struct HandleCloser(HANDLE);
@@ -205,9 +209,9 @@ mod windows {
205209
}
206210

207211
fn set_echo(echo: bool, handle: HANDLE) -> Result<(), PromptError> {
208-
let mut mode: CONSOLE_MODE = 0;
212+
let mut mode: u32 = 0;
209213
unsafe {
210-
if GetConsoleMode(handle, &mut mode as *mut CONSOLE_MODE) == FALSE {
214+
if GetConsoleMode(handle, &mut mode) == FALSE {
211215
return Err(PromptError::IOError(std::io::Error::last_os_error()));
212216
}
213217
}
@@ -272,7 +276,7 @@ mod windows {
272276
// the file type comes back as FILE_TYPE_PIPE 0x03. This means
273277
// that we can't tell if we're in a pipe or a console, so echo
274278
// won't be disabled at all.
275-
GetFileType(handle) == windows_sys::Win32::Storage::FileSystem::FILE_TYPE_CHAR
279+
GetFileType(handle) == crate::win32::FILE_TYPE_CHAR
276280
};
277281

278282
// Disable terminal echo if we're in a console, if we're not,

src/tty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ pub use crate::tty::unix::isatty;
1818
#[cfg(target_family = "windows")]
1919
mod windows {
2020
use crate::tty::Stream;
21-
use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
22-
use windows_sys::Win32::Storage::FileSystem::GetFileType;
23-
use windows_sys::Win32::System::Console::{
24-
GetStdHandle, STD_ERROR_HANDLE, STD_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
21+
use crate::win32::{
22+
HANDLE, INVALID_HANDLE_VALUE, STD_ERROR_HANDLE,
23+
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE
2524
};
25+
use crate::win32::{GetFileType, GetStdHandle};
2626

2727
/// Returns true if the given stream is a tty.
2828
#[allow(clippy::let_and_return)]
@@ -46,13 +46,13 @@ mod windows {
4646

4747
let is_atty = unsafe {
4848
// Consoles will show as FILE_TYPE_CHAR (0x02)
49-
GetFileType(handle) == windows_sys::Win32::Storage::FileSystem::FILE_TYPE_CHAR
49+
GetFileType(handle) == crate::win32::FILE_TYPE_CHAR
5050
};
5151

5252
is_atty
5353
}
5454

55-
unsafe fn get_handle(input_handle: STD_HANDLE) -> Result<HANDLE, ()> {
55+
unsafe fn get_handle(input_handle: u32) -> Result<HANDLE, ()> {
5656
let handle = GetStdHandle(input_handle);
5757
if handle == INVALID_HANDLE_VALUE {
5858
return Err(());

src/win32.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![allow(non_snake_case, dead_code)]
2+
3+
use std::ffi::c_void;
4+
5+
pub type HANDLE = isize;
6+
pub type PCSTR = *const u8;
7+
pub type BOOL = i32;
8+
9+
pub const FALSE: BOOL = 0i32;
10+
pub const TRUE: BOOL = 1i32;
11+
pub const OPEN_EXISTING: u32 = 3;
12+
pub const GENERIC_READ: u32 = 0x80000000;
13+
pub const GENERIC_WRITE: u32 = 0x40000000;
14+
pub const INVALID_HANDLE_VALUE: HANDLE = -1isize;
15+
pub const ENABLE_ECHO_INPUT: u32 = 4;
16+
pub const STD_ERROR_HANDLE: u32 = 0xfffffff4;
17+
pub const STD_INPUT_HANDLE: u32 = 0xfffffff6;
18+
pub const STD_OUTPUT_HANDLE: u32 = 0xfffffff5;
19+
pub const FILE_TYPE_CHAR: u32 = 2u32;
20+
21+
#[link(name = "kernel32", kind = "raw-dylib")]
22+
extern "system" {
23+
pub fn CloseHandle(hobject: HANDLE) -> BOOL;
24+
pub fn CreateFileA(lpfilename: PCSTR, dwdesiredaccess: u32, dwsharemode: u32, lpsecurityattributes: *const c_void, dwcreationdisposition: u32, dwflagsandattributes: u32, htemplatefile: HANDLE) -> HANDLE;
25+
pub fn GetFileType(hfile: HANDLE) -> u32;
26+
pub fn GetConsoleMode(hconsolehandle: HANDLE, lpmode: *mut u32) -> BOOL;
27+
pub fn GetStdHandle(nstdhandle: u32) -> HANDLE;
28+
pub fn ReadConsoleW(hconsoleinput: HANDLE, lpbuffer: *mut c_void, nnumberofcharstoread: u32, lpnumberofcharsread: *mut u32, pinputcontrol: *const c_void) -> BOOL;
29+
pub fn SetConsoleMode(hconsolehandle: HANDLE, dwmode: u32) -> BOOL;
30+
pub fn WriteConsoleW(hconsoleoutput: HANDLE, lpbuffer: *const c_void, nnumberofcharstowrite: u32, lpnumberofcharswritten: *mut u32, lpreserved: *const c_void) -> BOOL;
31+
}

0 commit comments

Comments
 (0)