From 4322825ac735bc1e5fde8d7458d89b2fadca1863 Mon Sep 17 00:00:00 2001 From: pagedown Date: Wed, 9 Mar 2022 16:12:20 +0800 Subject: [PATCH] Add support for sh that does not support the -l login shell option FreeBSD sh does not support using the -l option to run as a login shell. --- shell-integration/ssh/bootstrap.sh | 43 +++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/shell-integration/ssh/bootstrap.sh b/shell-integration/ssh/bootstrap.sh index 97785e2cd..f48df6c3b 100644 --- a/shell-integration/ssh/bootstrap.sh +++ b/shell-integration/ssh/bootstrap.sh @@ -412,6 +412,36 @@ exec_with_shell_integration() { esac } +execute_sh_with_posix_env() { + if [ "$login_shell" != "/bin/sh" ]; then return 1; fi + if command "$login_shell" -l -c ":" > /dev/null 2> /dev/null; then return 1; fi + if [ -z "$shell_integration_dir" ]; then + if [ -n "$KITTY_SSH_KITTEN_DATA_DIR" ]; then + shell_integration_dir="$HOME/$KITTY_SSH_KITTEN_DATA_DIR/shell-integration" + else + die "The data directory is not defined, ssh kitten cannot function." + fi + fi + sh_dir="$shell_integration_dir/sh" + if [ ! -d "$sh_dir" ]; then mkdir -p "$sh_dir" || die "Creating data directory failed"; fi + # Source /etc/profile, ~/.profile, and then check and source ENV + echo ' +if [ -n "$KITTY_SH_INJECT" ]; then + unset ENV; unset KITTY_SH_INJECT + _ksi_safe_source() { if [ -f "$1" -a -r "$1" ]; then . "$1"; return 0; fi; return 1; } + if [ -n "$KITTY_SH_POSIX_ENV" ]; then export ENV="$KITTY_SH_POSIX_ENV"; fi + unset KITTY_SH_POSIX_ENV + _ksi_safe_source "/etc/profile"; _ksi_safe_source "${HOME-}/.profile" + if [ -n "$ENV" ]; then _ksi_safe_source "$ENV"; fi +fi' > "$sh_dir/login_shell_env.sh" + export KITTY_SH_INJECT=1 + if [ -n "$ENV" ]; then + export KITTY_SH_POSIX_ENV="$ENV" + fi + export ENV="$sh_dir/login_shell_env.sh" + exec "$login_shell" +} + # Used in the tests TEST_SCRIPT @@ -422,10 +452,14 @@ case "$KITTY_SHELL_INTEGRATION" in ;; (*) # not blank - q=$(printf "%s" "$KITTY_SHELL_INTEGRATION" | command grep '\bno-rc\b') - if [ -z "$q" ]; then - exec_with_shell_integration - # exec failed, unset + if [ -n "$shell_integration_dir" ]; then + q=$(printf "%s" "$KITTY_SHELL_INTEGRATION" | command grep '\bno-rc\b') + if [ -z "$q" ]; then + exec_with_shell_integration + # exec failed, unset + unset KITTY_SHELL_INTEGRATION + fi + else unset KITTY_SHELL_INTEGRATION fi ;; @@ -437,4 +471,5 @@ esac [ "$(exec -a echo echo OK 2> /dev/null)" = "OK" ] && exec -a "-$shell_name" $login_shell execute_with_python execute_with_perl +execute_sh_with_posix_env exec $login_shell "-l"