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 Sep 23, 2021
1 parent 742777e commit 500915f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion ble.pp
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/edit.sh
Expand Up @@ -4771,7 +4771,7 @@ else
print "_ble_edit_history=("
}
# ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。
#% # ※rcfile として読み込むと HISTTIMEFORMAT が ?? に化ける。
/^ *[0-9]+\*? +(__ble_ext__|\?\?)/ {
if (n != "") {
n = "";
Expand All @@ -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;
Expand Down Expand Up @@ -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]}
Expand Down
11 changes: 7 additions & 4 deletions src/util.sh
Expand Up @@ -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"'
Expand Down Expand Up @@ -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:]_]*)='\''\(.*\)'\''$/) {
Expand Down
1 change: 1 addition & 0 deletions test/benchmark/benchmark.sh
Expand Up @@ -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 の方が速い様だ
Expand Down

0 comments on commit 500915f

Please sign in to comment.