From 500915fdc7805c6fcc4ea2cb744917f57b8f387a Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Wed, 19 May 2021 11:25:18 +0900 Subject: [PATCH] main: work around sourcing "ble.sh" inside subshells --- ble.pp | 5 ++++- src/edit.sh | 8 ++++---- src/util.sh | 11 +++++++---- test/benchmark/benchmark.sh | 1 + 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ble.pp b/ble.pp index d77ec6bb..438114a0 100644 --- a/ble.pp +++ b/ble.pp @@ -56,7 +56,10 @@ return 1 2>/dev/null || exit 1 fi 3>&2 >/dev/null 2>&1 # set -x 対策 #D0930 -if [[ $- != *i* ]]; then +if ((BASH_SUBSHELL)); then + builtin echo "ble.sh: ble.sh cannot be loaded into a subshell." >&3 + return 1 2>/dev/null || builtin exit 1 +elif [[ $- != *i* ]]; then { ((${#BASH_SOURCE[@]})) && [[ ${BASH_SOURCE[${#BASH_SOURCE[@]}-1]} == *bashrc ]]; } || builtin echo "ble.sh: This is not an interactive session." >&3 return 1 2>/dev/null || builtin exit 1 diff --git a/src/edit.sh b/src/edit.sh index ad0574e0..82ee9255 100644 --- a/src/edit.sh +++ b/src/edit.sh @@ -4771,7 +4771,7 @@ else print "_ble_edit_history=(" } - # ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。 +#% # ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。 /^ *[0-9]+\*? +(__ble_ext__|\?\?)/ { if (n != "") { n = ""; @@ -4788,11 +4788,11 @@ else else gsub(apos, apos "\\" apos apos, line); - # 対策 #D1239: bash-3.2 以前では ^A, ^? が ^A^A, ^A^? に化ける +#% # 対策 #D1239: bash-3.2 以前では ^A, ^? が ^A^A, ^A^? に化ける gsub(/\001/, "'$apos'${_ble_term_SOH}'$apos'", line); gsub(/\177/, "'$apos'${_ble_term_DEL}'$apos'", line); - # 対策 #D1270: MSYS2 で ^M を代入すると消える +#% # 対策 #D1270: MSYS2 で ^M を代入すると消える gsub(/\015/, "'$apos'${_ble_term_CR}'$apos'", line); t = t != "" ? t "\n" line : line; @@ -4947,7 +4947,7 @@ function ble-edit/history/add { local command=$1 if [[ $_ble_edit_history_prefix ]]; then local code=' - # PREFIX_history_edit を未編集状態に戻す +#% # PREFIX_history_edit を未編集状態に戻す local index for index in "${!PREFIX_history_dirt[@]}"; do PREFIX_history_edit[index]=${PREFIX_history[index]} diff --git a/src/util.sh b/src/util.sh index 7a3e3706..c2b86752 100644 --- a/src/util.sh +++ b/src/util.sh @@ -1180,6 +1180,9 @@ if ((_ble_bash>=40000)); then function ble/util/is-running-in-subshell { [[ $$ != $BASHPID ]]; } else function ble/util/is-running-in-subshell { + # Note: bash-4.3 以下では BASH_SUBSHELL はパイプやプロセス置換で増えないの + # で信頼性が低いらしい。唯、関数内で実行している限りは大丈夫なのかもしれ + # ない。 ((BASH_SUBSHELL)) && return 0 local bashpid= command='echo $PPID' ble/util/assign bashpid 'ble/bin/sh -c "$command"' @@ -1242,19 +1245,19 @@ function ble/util/declare-print-definitions { if (decl) { isArray = (decl ~ /declare +-[fFgilrtux]*[aA]/); - # bash-3.0 の declare -p は改行について誤った出力をする。 +#% # bash-3.0 の declare -p は改行について誤った出力をする。 if (_ble_bash < 30100) gsub(/\\\n/, "\n", decl); if (_ble_bash < 40000) { - # #D1238 bash-3.2 以前の declare -p は ^A, ^? を - # ^A^A, ^A^? と出力してしまうので補正する。 +#% # #D1238 bash-3.2 以前の declare -p は ^A, ^? を +#% # ^A^A, ^A^? と出力してしまうので補正する。 gsub(/\001\001/, "${_ble_term_SOH}", decl); gsub(/\001\177/, "${_ble_term_DEL}", decl); } if (flag_escape_cr) gsub(/\015/, "${_ble_term_CR}", decl); - # declare 除去 +#% # declare 除去 sub(/^declare +(-[-aAfFgilrtux]+ +)?(-- +)?/, "", decl); if (isArray) { if (decl ~ /^([[:alpha:]_][[:alnum:]_]*)='\''\(.*\)'\''$/) { diff --git a/test/benchmark/benchmark.sh b/test/benchmark/benchmark.sh index 9ee67596..369233f5 100755 --- a/test/benchmark/benchmark.sh +++ b/test/benchmark/benchmark.sh @@ -93,6 +93,7 @@ _ble_measure_threshold=100000 # 一回の計測が threshold [usec] 以上にな ## @var[out] nsec ## 実行時間を nsec 単位で返します。 function ble-measure { + local TIMEFORMAT= if [[ ! $_ble_measure_base ]]; then _ble_measure_base=0 nsec=0 # : よりも a=1 の方が速い様だ