diff --git a/Cargo.lock b/Cargo.lock index ff885e628..4ad84f438 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,9 +328,11 @@ name = "fish-common" version = "0.0.0" dependencies = [ "bitflags", + "fish-build-helper", "fish-widestring", "libc", "nix", + "rsconf", ] [[package]] diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 4b599fb9b..dd2a956e5 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -12,5 +12,9 @@ fish-widestring.workspace = true libc.workspace = true nix.workspace = true +[build-dependencies] +fish-build-helper.workspace = true +rsconf.workspace = true + [lints] workspace = true diff --git a/crates/common/build.rs b/crates/common/build.rs new file mode 100644 index 000000000..5bb46d694 --- /dev/null +++ b/crates/common/build.rs @@ -0,0 +1,5 @@ +use fish_build_helper::target_os_is_apple; + +fn main() { + rsconf::declare_cfg("apple", target_os_is_apple()); +} diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 459c74423..fc8c6c966 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -133,34 +133,30 @@ pub struct UnescapeFlags: u32 { /// most common operating systems do not use them. The value is cached for the duration of the fish /// session. We err on the side of assuming it's not a console session. This approach isn't /// bullet-proof and that's OK. -#[cfg(not(any(target_os = "ios", target_os = "macos")))] pub fn is_console_session() -> bool { static IS_CONSOLE_SESSION: OnceLock = OnceLock::new(); // TODO(terminal-workaround) *IS_CONSOLE_SESSION.get_or_init(|| { - nix::unistd::ttyname(unsafe { std::os::fd::BorrowedFd::borrow_raw(STDIN_FILENO) }) - .is_ok_and(|buf| { - // Check if the tty matches /dev/(console|dcons|tty[uv\d]) - let is_console_tty = match buf.as_os_str().as_bytes() { - b"/dev/console" => true, - b"/dev/dcons" => true, - bytes => bytes.strip_prefix(b"/dev/tty").is_some_and(|rest| { - matches!(rest.first(), Some(b'u' | b'v' | b'0'..=b'9')) - }), - }; + // No console session on Apple, and ttyname may hang (#12506). + !cfg!(apple) + && nix::unistd::ttyname(unsafe { std::os::fd::BorrowedFd::borrow_raw(STDIN_FILENO) }) + .is_ok_and(|buf| { + // Check if the tty matches /dev/(console|dcons|tty[uv\d]) + let is_console_tty = match buf.as_os_str().as_bytes() { + b"/dev/console" => true, + b"/dev/dcons" => true, + bytes => bytes.strip_prefix(b"/dev/tty").is_some_and(|rest| { + matches!(rest.first(), Some(b'u' | b'v' | b'0'..=b'9')) + }), + }; - // and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` or `sun-color`. - is_console_tty && env::var_os("TERM").is_none_or(|t| !t.as_bytes().contains(&b'-')) - }) + // and that $TERM is simple, e.g. `xterm` or `vt100`, not `xterm-something` or `sun-color`. + is_console_tty + && env::var_os("TERM").is_none_or(|t| !t.as_bytes().contains(&b'-')) + }) }) } -#[cfg(any(target_os = "ios", target_os = "macos"))] -pub fn is_console_session() -> bool { - // No console session on Apple, and ttyname may hang (#12506). - false -} - /// Exits without invoking destructors (via _exit), useful for code after fork. pub fn exit_without_destructors(code: libc::c_int) -> ! { unsafe { libc::_exit(code) };