diff options
author | David Hewitt <1939362+davidhewitt@users.noreply.github.com> | 2020-01-02 11:49:27 +0000 |
---|---|---|
committer | Christian Duerr <contact@christianduerr.com> | 2020-01-02 11:49:27 +0000 |
commit | f632919134414e31ffd3ae9b5732d673deb0adf5 (patch) | |
tree | 59e74ceabba1d08a5d5966502dda113b11143fc2 /winpty | |
parent | 77acb26d7328f678b26c8aff2ee5706d78949c44 (diff) | |
download | alacritty-f632919134414e31ffd3ae9b5732d673deb0adf5.tar.gz alacritty-f632919134414e31ffd3ae9b5732d673deb0adf5.zip |
Clean up Windows PTY string handling
Removes widestring and dunce dependencies, reduces some code duplication
and corrects a few typos.
Diffstat (limited to 'winpty')
-rw-r--r-- | winpty/Cargo.toml | 1 | ||||
-rw-r--r-- | winpty/src/lib.rs | 8 | ||||
-rw-r--r-- | winpty/src/windows.rs | 30 |
3 files changed, 18 insertions, 21 deletions
diff --git a/winpty/Cargo.toml b/winpty/Cargo.toml index 9d1b2d27..02cc21c7 100644 --- a/winpty/Cargo.toml +++ b/winpty/Cargo.toml @@ -9,7 +9,6 @@ edition = "2018" [target.'cfg(windows)'.dependencies] winpty-sys = "0.4.3" bitflags = "1.0" -widestring = "0.4.0" [target.'cfg(windows)'.dev-dependencies] named_pipe = "0.4.1" diff --git a/winpty/src/lib.rs b/winpty/src/lib.rs index 117ec16d..f3b7aff9 100644 --- a/winpty/src/lib.rs +++ b/winpty/src/lib.rs @@ -1,13 +1,5 @@ #![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use, clippy::wrong_pub_self_convention)] -#[macro_use] -#[cfg(windows)] -extern crate bitflags; -#[cfg(windows)] -extern crate widestring; -#[cfg(windows)] -extern crate winpty_sys; - #[cfg(windows)] pub mod windows; diff --git a/winpty/src/windows.rs b/winpty/src/windows.rs index 20cb3dc3..25bf9d3e 100644 --- a/winpty/src/windows.rs +++ b/winpty/src/windows.rs @@ -1,12 +1,15 @@ +use std::ffi::{OsStr, OsString}; use std::fmt::{self, Display, Formatter}; +use std::iter::once; +use std::os::windows::ffi::{OsStrExt, OsStringExt}; use std::os::windows::io::RawHandle; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::ptr::{null, null_mut}; use std::result::Result; -use winpty_sys::*; +use bitflags::bitflags; -use widestring::WideCString; +use winpty_sys::*; #[derive(Copy, Clone, Debug)] pub enum ErrorCode { @@ -167,7 +170,7 @@ impl Winpty { pub fn conin_name(&mut self) -> PathBuf { unsafe { let raw = winpty_conin_name(self.0); - PathBuf::from(&String::from_utf16_lossy(std::slice::from_raw_parts(raw, wcslen(raw)))) + OsString::from_wide(std::slice::from_raw_parts(raw, wcslen(raw))).into() } } @@ -176,7 +179,7 @@ impl Winpty { pub fn conout_name(&mut self) -> PathBuf { unsafe { let raw = winpty_conout_name(self.0); - PathBuf::from(&String::from_utf16_lossy(std::slice::from_raw_parts(raw, wcslen(raw)))) + OsString::from_wide(std::slice::from_raw_parts(raw, wcslen(raw))).into() } } @@ -186,7 +189,7 @@ impl Winpty { pub fn conerr_name(&mut self) -> PathBuf { unsafe { let raw = winpty_conerr_name(self.0); - PathBuf::from(&String::from_utf16_lossy(std::slice::from_raw_parts(raw, wcslen(raw)))) + OsString::from_wide(std::slice::from_raw_parts(raw, wcslen(raw))).into() } } @@ -283,25 +286,28 @@ impl SpawnConfig { spawnflags: SpawnFlags, appname: Option<&str>, cmdline: Option<&str>, - cwd: Option<&str>, - end: Option<&str>, + cwd: Option<&Path>, + env: Option<&str>, ) -> Result<Self, Error> { let mut err = null_mut() as *mut winpty_error_t; - let to_wstring = |s| WideCString::from_str(s).unwrap(); + fn to_wstring<S: AsRef<OsStr> + ?Sized>(s: &S) -> Vec<u16> { + OsStr::new(s).encode_wide().chain(once(0)).collect() + } + let appname = appname.map(to_wstring); let cmdline = cmdline.map(to_wstring); let cwd = cwd.map(to_wstring); - let end = end.map(to_wstring); + let env = env.map(to_wstring); - let wstring_ptr = |opt: &Option<WideCString>| opt.as_ref().map_or(null(), |ws| ws.as_ptr()); + let wstring_ptr = |opt: &Option<Vec<u16>>| opt.as_ref().map_or(null(), |ws| ws.as_ptr()); let spawn_config = unsafe { winpty_spawn_config_new( spawnflags.bits(), wstring_ptr(&appname), wstring_ptr(&cmdline), wstring_ptr(&cwd), - wstring_ptr(&end), + wstring_ptr(&env), &mut err, ) }; |