Skip to content

Commit

Permalink
main: work around sourcing "ble.sh" inside subshells
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 28, 2022
1 parent 4b63b16 commit 8eb493a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
11 changes: 7 additions & 4 deletions ble-core.sh
Expand Up @@ -129,6 +129,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"'
Expand Down Expand Up @@ -658,19 +661,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:]_]*)='\''\(.*\)'\''$/) {
Expand Down
8 changes: 4 additions & 4 deletions ble-edit.sh
Expand Up @@ -5361,7 +5361,7 @@ else
print "_ble_edit_history=("
}
# ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。
#% # ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。
/^ *[0-9]+\*? +(__ble_ext__|\?\?)/ {
if (n != "") {
n = "";
Expand All @@ -5378,11 +5378,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;
Expand Down Expand Up @@ -5537,7 +5537,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]}
Expand Down
5 changes: 5 additions & 0 deletions ble.pp
Expand Up @@ -62,6 +62,11 @@
return 1 2>/dev/null || exit 1
fi

if ((BASH_SUBSHELL)); then
builtin echo "ble.sh: ble.sh cannot be loaded into a subshell." >&2
return 1 2>/dev/null || builtin exit 1
fi

if [[ -o posix ]]; then
unset _ble_bash
echo "ble.sh: ble.sh is not intended to be used in bash POSIX modes (--posix)." >&2
Expand Down
1 change: 1 addition & 0 deletions test/benchmark/benchmark.sh
Expand Up @@ -68,6 +68,7 @@ _ble_measure_threshold=100000 # 一回の計測が threshold [usec] 以上にな

## @var[out] ret nsec
function ble-measure {
local TIMEFORMAT=
if [[ ! $_ble_measure_base ]]; then
_ble_measure_base=0 nsec=0
# : よりも a=1 の方が速い様だ
Expand Down

0 comments on commit 8eb493a

Please sign in to comment.