From 4b5a4582aba04a34cb50d77e03f6b03e51108596 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 6 Jan 2020 16:43:17 +0800 Subject: [PATCH] global: fix subshell detection in Bash 3.2 --- ble.pp | 4 ++-- src/edit.sh | 2 +- src/util.sh | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ble.pp b/ble.pp index ba03f944..1ef6e5bb 100644 --- a/ble.pp +++ b/ble.pp @@ -187,7 +187,7 @@ function ble/bin/.freeze-utility-path { # POSIX utilities -_ble_init_posix_command_list=(sed date rm mkdir mkfifo sleep stty sort awk chmod grep man cat wc mv) +_ble_init_posix_command_list=(sed date rm mkdir mkfifo sleep stty sort awk chmod grep man cat wc mv sh) function ble/.check-environment { if ! type "${_ble_init_posix_command_list[@]}" &>/dev/null; then local cmd commandMissing= @@ -651,7 +651,7 @@ function ble/base/unload-for-reload { return 0 } function ble/base/unload { - [[ $$ == $BASHPID ]] || return + ble/util/is-running-in-subshell && return 1 local IFS=$' \t\n' ble/term/stty/TRAPEXIT ble/term/leave diff --git a/src/edit.sh b/src/edit.sh index f1baa328..ca05a422 100644 --- a/src/edit.sh +++ b/src/edit.sh @@ -3519,7 +3519,7 @@ function ble-edit/exec/restore-BASH_REMATCH { function ble/builtin/exit { local ext=${1-$?} - if ((BASHPID!=$$)) || [[ $_ble_decode_bind_state == none ]]; then + if ble/util/is-running-in-subshell || [[ $_ble_decode_bind_state == none ]]; then builtin exit "$ext" return fi diff --git a/src/util.sh b/src/util.sh index 4680de43..0888063e 100644 --- a/src/util.sh +++ b/src/util.sh @@ -895,6 +895,18 @@ else function ble/util/is-stdin-ready { false; } fi +# Note: BASHPID は Bash-4.0 以上 +if ((_ble_bash>=40000)); then + function ble/util/is-running-in-subshell { [[ $$ != $BASHPID ]]; } +else + function ble/util/is-running-in-subshell { + ((BASH_SUBSHELL)) && return 0 + local bashpid= command='echo $PPID' + ble/util/assign bashpid 'ble/bin/sh -c "$command"' + [[ $$ != $bashpid ]] + } +fi + ## 関数 ble/util/openat fdvar redirect ## "exec {fdvar}>foo" に該当する操作を実行します。 ## @param[out] fdvar