From 4fc35f6038256078e7030bf95b6260cc5390bb4f Mon Sep 17 00:00:00 2001 From: Nathan Lilienthal Date: Fri, 6 Nov 2020 23:48:48 -0500 Subject: Spawn new alacritty processes in CWD on macOS On macOS we can use 'proc_pidinfo' to determine the working directory of the terminal foreground process. Fixes #1979. --- CHANGELOG.md | 4 +- alacritty/src/event.rs | 14 ++-- alacritty/src/locale.rs | 100 -------------------------- alacritty/src/macos/locale.rs | 100 ++++++++++++++++++++++++++ alacritty/src/macos/mod.rs | 2 + alacritty/src/macos/proc.rs | 160 ++++++++++++++++++++++++++++++++++++++++++ alacritty/src/main.rs | 6 +- 7 files changed, 278 insertions(+), 108 deletions(-) delete mode 100644 alacritty/src/locale.rs create mode 100644 alacritty/src/macos/locale.rs create mode 100644 alacritty/src/macos/mod.rs create mode 100644 alacritty/src/macos/proc.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a350cff1..f8d615b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Cursors are now inverted when their fixed color is similar to the cell's background -- Use working directory of active process instead of shell for SpawnNewInstance action +- Use the working directory of the terminal foreground process, instead of the shell's working + directory, for `SpawnNewInstance` action - Fallback to normal underline for unsupported underline types in `CSI 4 : ? m` escapes - The user's background color is now used as the foreground for the render timer - Use yellow/red from the config for error and warning messages instead of fixed colors @@ -39,6 +40,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Wayland's Client side decorations now use the search bar colors - Reduce memory usage by up to at least 30% with a full scrollback buffer - The number of zerowidth characters per cell is no longer limited to 5 +- `SpawnNewInstance` is now using the working directory of the terminal foreground process on macOS ### Fixed diff --git a/alacritty/src/event.rs b/alacritty/src/event.rs index 0f9c24a5..20f087c3 100644 --- a/alacritty/src/event.rs +++ b/alacritty/src/event.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; use std::cmp::{max, min}; use std::env; use std::fmt::Debug; -#[cfg(unix)] +#[cfg(not(any(target_os = "macos", windows)))] use std::fs; use std::fs::File; use std::io::Write; @@ -45,6 +45,8 @@ use crate::config::Config; use crate::daemon::start_daemon; use crate::display::{Display, DisplayUpdate}; use crate::input::{self, ActionContext as _, FONT_SIZE_STEP}; +#[cfg(target_os = "macos")] +use crate::macos; use crate::message_bar::{Message, MessageBuffer}; use crate::scheduler::{Scheduler, TimerId}; use crate::url::{Url, Urls}; @@ -309,15 +311,17 @@ impl<'a, N: Notify + 'a, T: EventListener> input::ActionContext for ActionCon pid = tty::child_pid(); } - #[cfg(not(target_os = "freebsd"))] + #[cfg(not(any(target_os = "macos", target_os = "freebsd")))] let link_path = format!("/proc/{}/cwd", pid); #[cfg(target_os = "freebsd")] let link_path = format!("/compat/linux/proc/{}/cwd", pid); + #[cfg(not(target_os = "macos"))] + let cwd = fs::read_link(link_path); + #[cfg(target_os = "macos")] + let cwd = macos::proc::cwd(pid); // Add the current working directory as parameter. - fs::read_link(link_path) - .map(|path| vec!["--working-directory".into(), path]) - .unwrap_or_default() + cwd.map(|path| vec!["--working-directory".into(), path]).unwrap_or_default() }; #[cfg(not(unix))] diff --git a/alacritty/src/locale.rs b/alacritty/src/locale.rs deleted file mode 100644 index 6c02c12c..00000000 --- a/alacritty/src/locale.rs +++ /dev/null @@ -1,100 +0,0 @@ -#![allow(clippy::let_unit_value)] - -use std::env; -use std::ffi::{CStr, CString}; -use std::os::raw::c_char; -use std::slice; -use std::str; - -use libc::{setlocale, LC_ALL, LC_CTYPE}; -use log::debug; -use objc::runtime::{Class, Object}; -use objc::{msg_send, sel, sel_impl}; - -const FALLBACK_LOCALE: &str = "UTF-8"; - -pub fn set_locale_environment() { - let env_locale_c = CString::new("").unwrap(); - let env_locale_ptr = unsafe { setlocale(LC_ALL, env_locale_c.as_ptr()) }; - if !env_locale_ptr.is_null() { - let env_locale = unsafe { CStr::from_ptr(env_locale_ptr).to_string_lossy() }; - - // Assume `C` locale means unchanged, since it is the default anyways. - if env_locale != "C" { - debug!("Using environment locale: {}", env_locale); - return; - } - } - - let system_locale = system_locale(); - - // Set locale to system locale. - let system_locale_c = CString::new(system_locale.clone()).expect("nul byte in system locale"); - let lc_all = unsafe { setlocale(LC_ALL, system_locale_c.as_ptr()) }; - - // Check if system locale was valid or not. - if lc_all.is_null() { - // Use fallback locale. - debug!("Using fallback locale: {}", FALLBACK_LOCALE); - - let fallback_locale_c = CString::new(FALLBACK_LOCALE).unwrap(); - unsafe { setlocale(LC_CTYPE, fallback_locale_c.as_ptr()) }; - - env::set_var("LC_CTYPE", FALLBACK_LOCALE); - } else { - // Use system locale. - debug!("Using system locale: {}", system_locale); - - env::set_var("LC_ALL", system_locale); - } -} - -/// Determine system locale based on language and country code. -fn system_locale() -> String { - unsafe { - let locale_class = Class::get("NSLocale").unwrap(); - let locale: *const Object = msg_send![locale_class, currentLocale]; - let _: () = msg_send![locale_class, release]; - - // `localeIdentifier` returns extra metadata with the locale (including currency and - // collator) on newer versions of macOS. This is not a valid locale, so we use - // `languageCode` and `countryCode`, if they're available (macOS 10.12+): - // - // https://developer.apple.com/documentation/foundation/nslocale/1416263-localeidentifier?language=objc - // https://developer.apple.com/documentation/foundation/nslocale/1643060-countrycode?language=objc - // https://developer.apple.com/documentation/foundation/nslocale/1643026-languagecode?language=objc - let is_language_code_supported: bool = - msg_send![locale, respondsToSelector: sel!(languageCode)]; - let is_country_code_supported: bool = - msg_send![locale, respondsToSelector: sel!(countryCode)]; - let locale_id = if is_language_code_supported && is_country_code_supported { - let language_code: *const Object = msg_send![locale, languageCode]; - let language_code_str = nsstring_as_str(language_code).to_owned(); - let _: () = msg_send![language_code, release]; - - let country_code: *const Object = msg_send![locale, countryCode]; - let country_code_str = nsstring_as_str(country_code).to_owned(); - let _: () = msg_send![country_code, release]; - - format!("{}_{}.UTF-8", &language_code_str, &country_code_str) - } else { - let identifier: *const Object = msg_send![locale, localeIdentifier]; - let identifier_str = nsstring_as_str(identifier).to_owned(); - let _: () = msg_send![identifier, release]; - - identifier_str + ".UTF-8" - }; - - let _: () = msg_send![locale, release]; - - locale_id - } -} - -const UTF8_ENCODING: usize = 4; - -unsafe fn nsstring_as_str<'a>(nsstring: *const Object) -> &'a str { - let cstr: *const c_char = msg_send![nsstring, UTF8String]; - let len: usize = msg_send![nsstring, lengthOfBytesUsingEncoding: UTF8_ENCODING]; - str::from_utf8(slice::from_raw_parts(cstr as *const u8, len)).unwrap() -} diff --git a/alacritty/src/macos/locale.rs b/alacritty/src/macos/locale.rs new file mode 100644 index 00000000..6c02c12c --- /dev/null +++ b/alacritty/src/macos/locale.rs @@ -0,0 +1,100 @@ +#![allow(clippy::let_unit_value)] + +use std::env; +use std::ffi::{CStr, CString}; +use std::os::raw::c_char; +use std::slice; +use std::str; + +use libc::{setlocale, LC_ALL, LC_CTYPE}; +use log::debug; +use objc::runtime::{Class, Object}; +use objc::{msg_send, sel, sel_impl}; + +const FALLBACK_LOCALE: &str = "UTF-8"; + +pub fn set_locale_environment() { + let env_locale_c = CString::new("").unwrap(); + let env_locale_ptr = unsafe { setlocale(LC_ALL, env_locale_c.as_ptr()) }; + if !env_locale_ptr.is_null() { + let env_locale = unsafe { CStr::from_ptr(env_locale_ptr).to_string_lossy() }; + + // Assume `C` locale means unchanged, since it is the default anyways. + if env_locale != "C" { + debug!("Using environment locale: {}", env_locale); + return; + } + } + + let system_locale = system_locale(); + + // Set locale to system locale. + let system_locale_c = CString::new(system_locale.clone()).expect("nul byte in system locale"); + let lc_all = unsafe { setlocale(LC_ALL, system_locale_c.as_ptr()) }; + + // Check if system locale was valid or not. + if lc_all.is_null() { + // Use fallback locale. + debug!("Using fallback locale: {}", FALLBACK_LOCALE); + + let fallback_locale_c = CString::new(FALLBACK_LOCALE).unwrap(); + unsafe { setlocale(LC_CTYPE, fallback_locale_c.as_ptr()) }; + + env::set_var("LC_CTYPE", FALLBACK_LOCALE); + } else { + // Use system locale. + debug!("Using system locale: {}", system_locale); + + env::set_var("LC_ALL", system_locale); + } +} + +/// Determine system locale based on language and country code. +fn system_locale() -> String { + unsafe { + let locale_class = Class::get("NSLocale").unwrap(); + let locale: *const Object = msg_send![locale_class, currentLocale]; + let _: () = msg_send![locale_class, release]; + + // `localeIdentifier` returns extra metadata with the locale (including currency and + // collator) on newer versions of macOS. This is not a valid locale, so we use + // `languageCode` and `countryCode`, if they're available (macOS 10.12+): + // + // https://developer.apple.com/documentation/foundation/nslocale/1416263-localeidentifier?language=objc + // https://developer.apple.com/documentation/foundation/nslocale/1643060-countrycode?language=objc + // https://developer.apple.com/documentation/foundation/nslocale/1643026-languagecode?language=objc + let is_language_code_supported: bool = + msg_send![locale, respondsToSelector: sel!(languageCode)]; + let is_country_code_supported: bool = + msg_send![locale, respondsToSelector: sel!(countryCode)]; + let locale_id = if is_language_code_supported && is_country_code_supported { + let language_code: *const Object = msg_send![locale, languageCode]; + let language_code_str = nsstring_as_str(language_code).to_owned(); + let _: () = msg_send![language_code, release]; + + let country_code: *const Object = msg_send![locale, countryCode]; + let country_code_str = nsstring_as_str(country_code).to_owned(); + let _: () = msg_send![country_code, release]; + + format!("{}_{}.UTF-8", &language_code_str, &country_code_str) + } else { + let identifier: *const Object = msg_send![locale, localeIdentifier]; + let identifier_str = nsstring_as_str(identifier).to_owned(); + let _: () = msg_send![identifier, release]; + + identifier_str + ".UTF-8" + }; + + let _: () = msg_send![locale, release]; + + locale_id + } +} + +const UTF8_ENCODING: usize = 4; + +unsafe fn nsstring_as_str<'a>(nsstring: *const Object) -> &'a str { + let cstr: *const c_char = msg_send![nsstring, UTF8String]; + let len: usize = msg_send![nsstring, lengthOfBytesUsingEncoding: UTF8_ENCODING]; + str::from_utf8(slice::from_raw_parts(cstr as *const u8, len)).unwrap() +} diff --git a/alacritty/src/macos/mod.rs b/alacritty/src/macos/mod.rs new file mode 100644 index 00000000..1d630730 --- /dev/null +++ b/alacritty/src/macos/mod.rs @@ -0,0 +1,2 @@ +pub mod locale; +pub mod proc; diff --git a/alacritty/src/macos/proc.rs b/alacritty/src/macos/proc.rs new file mode 100644 index 00000000..be7e4891 --- /dev/null +++ b/alacritty/src/macos/proc.rs @@ -0,0 +1,160 @@ +use std::ffi::{CStr, CString, IntoStringError}; +use std::fmt::{self, Display, Formatter}; +use std::io; +use std::mem::{self, MaybeUninit}; +use std::os::raw::{c_int, c_void}; +use std::path::PathBuf; + +/// Error during working directory retrieval. +#[derive(Debug)] +pub enum Error { + Io(io::Error), + + /// Error converting into utf8 string. + IntoString(IntoStringError), + + /// Expected return size didn't match libproc's. + InvalidSize, +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Error::InvalidSize => None, + Error::Io(err) => err.source(), + Error::IntoString(err) => err.source(), + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Error::InvalidSize => write!(f, "Invalid proc_pidinfo return size"), + Error::Io(err) => write!(f, "Error getting current working directory: {}", err), + Error::IntoString(err) => { + write!(f, "Error when parsing current working directory: {}", err) + }, + } + } +} + +impl From for Error { + fn from(val: io::Error) -> Self { + Error::Io(val) + } +} + +impl From for Error { + fn from(val: IntoStringError) -> Self { + Error::IntoString(val) + } +} + +pub fn cwd(pid: c_int) -> Result { + let mut info = MaybeUninit::::uninit(); + let info_ptr = info.as_mut_ptr() as *mut c_void; + let size = mem::size_of::() as c_int; + + let c_str = unsafe { + let pidinfo_size = sys::proc_pidinfo(pid, sys::PROC_PIDVNODEPATHINFO, 0, info_ptr, size); + match pidinfo_size { + c if c < 0 => return Err(io::Error::last_os_error().into()), + s if s != size => return Err(Error::InvalidSize), + _ => CStr::from_ptr(info.assume_init().pvi_cdir.vip_path.as_ptr()), + } + }; + + Ok(CString::from(c_str).into_string().map(PathBuf::from)?) +} + +/// Bindings for libproc. +#[allow(non_camel_case_types)] +mod sys { + use std::os::raw::{c_char, c_int, c_longlong, c_void}; + + pub const PROC_PIDVNODEPATHINFO: c_int = 9; + + type gid_t = c_int; + type off_t = c_longlong; + type uid_t = c_int; + type fsid_t = fsid; + + #[repr(C)] + #[derive(Debug, Copy, Clone)] + pub struct fsid { + pub val: [i32; 2usize], + } + + #[repr(C)] + #[derive(Debug, Copy, Clone)] + pub struct vinfo_stat { + pub vst_dev: u32, + pub vst_mode: u16, + pub vst_nlink: u16, + pub vst_ino: u64, + pub vst_uid: uid_t, + pub vst_gid: gid_t, + pub vst_atime: i64, + pub vst_atimensec: i64, + pub vst_mtime: i64, + pub vst_mtimensec: i64, + pub vst_ctime: i64, + pub vst_ctimensec: i64, + pub vst_birthtime: i64, + pub vst_birthtimensec: i64, + pub vst_size: off_t, + pub vst_blocks: i64, + pub vst_blksize: i32, + pub vst_flags: u32, + pub vst_gen: u32, + pub vst_rdev: u32, + pub vst_qspare: [i64; 2usize], + } + + #[repr(C)] + #[derive(Debug, Copy, Clone)] + pub struct vnode_info { + pub vi_stat: vinfo_stat, + pub vi_type: c_int, + pub vi_pad: c_int, + pub vi_fsid: fsid_t, + } + + #[repr(C)] + #[derive(Copy, Clone)] + pub struct vnode_info_path { + pub vip_vi: vnode_info, + pub vip_path: [c_char; 1024usize], + } + + #[repr(C)] + #[derive(Copy, Clone)] + pub struct proc_vnodepathinfo { + pub pvi_cdir: vnode_info_path, + pub pvi_rdir: vnode_info_path, + } + + extern "C" { + pub fn proc_pidinfo( + pid: c_int, + flavor: c_int, + arg: u64, + buffer: *mut c_void, + buffersize: c_int, + ) -> c_int; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::env; + use std::process; + + #[test] + fn cwd_matches_current_dir() { + assert_eq!(cwd(process::id() as i32).ok(), env::current_dir().ok()); + } +} diff --git a/alacritty/src/main.rs b/alacritty/src/main.rs index dc316c79..98fe2d05 100644 --- a/alacritty/src/main.rs +++ b/alacritty/src/main.rs @@ -38,9 +38,9 @@ mod daemon; mod display; mod event; mod input; -#[cfg(target_os = "macos")] -mod locale; mod logging; +#[cfg(target_os = "macos")] +mod macos; mod message_bar; mod meter; #[cfg(windows)] @@ -63,6 +63,8 @@ use crate::config::monitor; use crate::config::Config; use crate::display::Display; use crate::event::{Event, EventProxy, Processor}; +#[cfg(target_os = "macos")] +use crate::macos::locale; use crate::message_bar::MessageBuffer; fn main() { -- cgit