From f632919134414e31ffd3ae9b5732d673deb0adf5 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:49:27 +0000 Subject: Clean up Windows PTY string handling Removes widestring and dunce dependencies, reduces some code duplication and corrects a few typos. --- alacritty_terminal/src/tty/windows/conpty.rs | 27 ++++++++------------------- alacritty_terminal/src/tty/windows/mod.rs | 21 ++++++++++++++++++++- alacritty_terminal/src/tty/windows/winpty.rs | 20 ++++++-------------- 3 files changed, 34 insertions(+), 34 deletions(-) (limited to 'alacritty_terminal/src/tty/windows') diff --git a/alacritty_terminal/src/tty/windows/conpty.rs b/alacritty_terminal/src/tty/windows/conpty.rs index 44b3662f..28cdf4c4 100644 --- a/alacritty_terminal/src/tty/windows/conpty.rs +++ b/alacritty_terminal/src/tty/windows/conpty.rs @@ -18,10 +18,8 @@ use std::mem; use std::os::windows::io::IntoRawHandle; use std::ptr; -use dunce::canonicalize; use mio_anonymous_pipes::{EventedAnonRead, EventedAnonWrite}; use miow; -use widestring::U16CString; use winapi::shared::basetsd::{PSIZE_T, SIZE_T}; use winapi::shared::minwindef::{BYTE, DWORD}; use winapi::shared::ntdef::{HANDLE, HRESULT, LPWSTR}; @@ -34,11 +32,11 @@ use winapi::um::processthreadsapi::{ use winapi::um::winbase::{EXTENDED_STARTUPINFO_PRESENT, STARTF_USESTDHANDLES, STARTUPINFOEXW}; use winapi::um::wincontypes::{COORD, HPCON}; -use crate::config::{Config, Shell}; +use crate::config::Config; use crate::event::OnResize; use crate::term::SizeInfo; use crate::tty::windows::child::ChildExitWatcher; -use crate::tty::windows::Pty; +use crate::tty::windows::{cmdline, win32_string, Pty}; // TODO: Replace with winapi's implementation. This cannot be // done until a safety net is in place for versions of Windows @@ -141,8 +139,7 @@ pub fn new(config: &Config, size: &SizeInfo, _window_id: Option) -> let mut startup_info_ex: STARTUPINFOEXW = Default::default(); - let title = config.window.title.clone(); - let title = U16CString::from_str(title).unwrap(); + let title = win32_string(&config.window.title); startup_info_ex.StartupInfo.lpTitle = title.as_ptr() as LPWSTR; startup_info_ex.StartupInfo.cb = mem::size_of::() as u32; @@ -205,19 +202,11 @@ pub fn new(config: &Config, size: &SizeInfo, _window_id: Option) -> } } - // Get process commandline - let default_shell = &Shell::new("powershell"); - let shell = config.shell.as_ref().unwrap_or(default_shell); - let mut cmdline = shell.args.clone(); - cmdline.insert(0, shell.program.to_string()); - - // Warning, here be borrow hell - let cwd = config.working_directory().as_ref().map(|dir| canonicalize(dir).unwrap()); - let cwd = cwd.as_ref().map(|dir| dir.to_str().unwrap()); - - // Create the client application, using startup info containing ConPTY info - let cmdline = U16CString::from_str(&cmdline.join(" ")).unwrap(); - let cwd = cwd.map(|s| U16CString::from_str(&s).unwrap()); + let cmdline = win32_string(&cmdline(&config)); + let cwd = config + .working_directory() + .map(|dir| dir.canonicalize().unwrap()) + .map(|path| win32_string(&path)); let mut proc_info: PROCESS_INFORMATION = Default::default(); unsafe { diff --git a/alacritty_terminal/src/tty/windows/mod.rs b/alacritty_terminal/src/tty/windows/mod.rs index f5e0e61f..03e2a3cd 100644 --- a/alacritty_terminal/src/tty/windows/mod.rs +++ b/alacritty_terminal/src/tty/windows/mod.rs @@ -12,7 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::ffi::OsStr; use std::io::{self, Read, Write}; +use std::iter::once; +use std::os::windows::ffi::OsStrExt; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::TryRecvError; @@ -22,7 +25,7 @@ use mio_named_pipes::NamedPipe; use log::info; -use crate::config::Config; +use crate::config::{Config, Shell}; use crate::event::OnResize; use crate::term::SizeInfo; use crate::tty::windows::child::ChildExitWatcher; @@ -298,3 +301,19 @@ impl OnResize for Pty { } } } + +fn cmdline(config: &Config) -> String { + let default_shell = Shell::new("powershell"); + let shell = config.shell.as_ref().unwrap_or(&default_shell); + + once(shell.program.as_ref()) + .chain(shell.args.iter().map(|a| a.as_ref())) + .collect::>() + .join(" ") +} + +/// Converts the string slice into a Windows-standard representation for "W"- +/// suffixed function variants, which accept UTF-16 encoded string values. +pub fn win32_string + ?Sized>(value: &S) -> Vec { + OsStr::new(value).encode_wide().chain(once(0)).collect() +} diff --git a/alacritty_terminal/src/tty/windows/winpty.rs b/alacritty_terminal/src/tty/windows/winpty.rs index 0d2421b0..c56ad8f6 100644 --- a/alacritty_terminal/src/tty/windows/winpty.rs +++ b/alacritty_terminal/src/tty/windows/winpty.rs @@ -17,17 +17,16 @@ use std::os::windows::fs::OpenOptionsExt; use std::os::windows::io::{FromRawHandle, IntoRawHandle}; use std::u16; -use dunce::canonicalize; use log::info; use mio_named_pipes::NamedPipe; use winapi::um::winbase::FILE_FLAG_OVERLAPPED; use winpty::{Config as WinptyConfig, ConfigFlags, MouseMode, SpawnConfig, SpawnFlags, Winpty}; -use crate::config::{Config, Shell}; +use crate::config::Config; use crate::event::OnResize; use crate::term::SizeInfo; use crate::tty::windows::child::ChildExitWatcher; -use crate::tty::windows::Pty; +use crate::tty::windows::{cmdline, Pty}; pub use winpty::Winpty as Agent; @@ -42,22 +41,15 @@ pub fn new(config: &Config, size: &SizeInfo, _window_id: Option) -> let mut agent = Winpty::open(&wconfig).unwrap(); let (conin, conout) = (agent.conin_name(), agent.conout_name()); - // Get process commandline - let default_shell = &Shell::new("powershell"); - let shell = config.shell.as_ref().unwrap_or(default_shell); - let mut cmdline = shell.args.clone(); - cmdline.insert(0, shell.program.to_string()); - - // Warning, here be borrow hell - let cwd = config.working_directory().as_ref().map(|dir| canonicalize(dir).unwrap()); - let cwd = cwd.as_ref().map(|dir| dir.to_str().unwrap()); + let cmdline = cmdline(&config); + let cwd = config.working_directory().map(|dir| dir.canonicalize().unwrap()); // Spawn process let spawnconfig = SpawnConfig::new( SpawnFlags::AUTO_SHUTDOWN | SpawnFlags::EXIT_AFTER_SHUTDOWN, None, // appname - Some(&cmdline.join(" ")), - cwd, + Some(&cmdline), + cwd.as_ref().map(|p| p.as_ref()), None, // Env ) .unwrap(); -- cgit v1.2.3-54-g00ecf