From d3abd5d600dc45f92c82222c9f64defb4e73ebcb Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 2 May 2023 14:53:10 -0500 Subject: [PATCH] Fix inverted is_console_session() logic The $TERM matching logic was inverted. --- fish-rust/src/common.rs | 58 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 597d949b5..bbccf8fec 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -1891,38 +1891,34 @@ pub const fn assert_sync() {} /// session. We err on the side of assuming it's not a console session. This approach isn't /// bullet-proof and that's OK. pub fn is_console_session() -> bool { - *CONSOLE_SESSION -} + static IS_CONSOLE_SESSION: Lazy = Lazy::new(|| { + use std::os::unix::ffi::OsStrExt; -static CONSOLE_SESSION: Lazy = Lazy::new(|| { - const path_max: usize = libc::PATH_MAX as _; - let mut tty_name: [u8; path_max] = [0; path_max]; - if unsafe { - libc::ttyname_r( - STDIN_FILENO, - std::ptr::addr_of_mut!(tty_name).cast(), - path_max, - ) - } != 0 - { - return false; - } - // Test that the tty matches /dev/(console|dcons|tty[uv\d]) - let len = "/dev/tty".len(); - ( - ( - tty_name.starts_with(b"/dev/tty") && - ([b'u', b'v'].contains(&tty_name[len]) || tty_name[len].is_ascii_digit()) - ) || - tty_name.starts_with(b"/dev/dcons\0") || - tty_name.starts_with(b"/dev/console\0")) - // and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` - && match env::var("TERM") { - Ok(term) => ["-", "sun-color"].contains(&term.as_str()), - Err(env::VarError::NotPresent) => true, - Err(_) => false, - } -}); + const PATH_MAX: usize = libc::PATH_MAX as usize; + let mut tty_name = [0u8; PATH_MAX]; + unsafe { + if libc::ttyname_r(STDIN_FILENO, tty_name.as_mut_ptr().cast(), tty_name.len()) != 0 { + return false; + } + } + // Check if the tty matches /dev/(console|dcons|tty[uv\d]) + const LEN: usize = b"/dev/tty".len(); + ( + ( + tty_name.starts_with(b"/dev/tty") && + ([b'u', b'v'].contains(&tty_name[LEN]) || tty_name[LEN].is_ascii_digit()) + ) || + tty_name.starts_with(b"/dev/dcons\0") || + tty_name.starts_with(b"/dev/console\0")) + // and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` or `sun-color`. + && match env::var_os("TERM") { + Some(term) => !term.as_bytes().contains(&b'-'), + None => true, + } + }); + + *IS_CONSOLE_SESSION +} /// Asserts that a slice is alphabetically sorted by a [`&wstr`] `name` field. ///