Skip to content

Commit

Permalink
global: workaround Bash 3.2 bug of array initialization with SOH/DEL
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 24, 2020
1 parent 327661f commit 1720ec0
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 34 deletions.
8 changes: 2 additions & 6 deletions lib/init-term.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ function ble/init:term/initialize {
ble/init:term/define-cap.2 _ble_term_ind $'\eD' ind:sf
ble/init:term/define-cap _ble_term_ri $'\eM' ri:sr
ble/init:term/define-cap _ble_term_cr $'\r' cr:cr
_ble_term_nl=$'\n'
ble/init:term/register-varname _ble_term_nl
_ble_term_IFS=$' \t\n'
ble/init:term/register-varname _ble_term_IFS
_ble_term_fs=$'\034'
ble/init:term/register-varname _ble_term_fs

# CUU/CUD/CUF/CUB
ble/init:term/define-cap _ble_term_cuu $'\e[%dA' cuu:UP 123
Expand Down Expand Up @@ -241,3 +235,5 @@ function ble/init:term/initialize {
ble/util/put "ble/term.sh: updating tput cache for TERM=$TERM... " >&2
ble/init:term/initialize
ble/util/print $'\r'"ble/term.sh: updating tput cache for TERM=$TERM... done" >&2

return 0
74 changes: 49 additions & 25 deletions memo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ bash 配列の宣言に関する仕様・バグと注意点
- 同じスコープの場合でも unset a してから local -A a すれば大丈夫。
- グローバルでは起こらない。

* BUG bash-3.0..3.2: ^? や ^A の値が declare -p で ^A^? や ^A^A に変換されてしまう。

bash_features

* printf -v var %s value
Expand Down Expand Up @@ -912,29 +914,6 @@ bash_tips
ToDo
-------------------------------------------------------------------------------

2020-01-24

* Bash 3.2 ^A, ^? を含む配列に対する対策 [#T0003]

更に履歴に格納されている値も変化している。
history -s で登録されている値は合っている。
何処でずれるのだろうか。。。

よく考えたらこれは可也広範に亘るのではないだろうか。
そもそも配列の複製 arr2=("${arr1[@]}") を安全に実行できるのだろうか。
これは別項目を立てて対策を考えるべきである。

どういう操作が安全でどういう操作が駄目なのか。
対策する事は可能だろうか。

* arr2=("${arr1[@]}") これは安全の様だ
* arr1=("$del" "$soh") これも安全の様だ

ble.sh の中で特に問題になりそうなのは何処か。

* binding のキャッシュ?
* vi のマクロの記録?

2020-01-23

* 前々から発生していたが曖昧補完などを実行すると時々ごみが残る。
Expand Down Expand Up @@ -2582,7 +2561,7 @@ bash_tips
- decode (`ble/builtin/bind`): fix widget mapping for `default_keymap=safe` `#D1234` 750a9f5
- main (ble-update): fix a bug that the check of `make` does not work in Bash 3.2 `#D1236` 08ced81
- stty: workaround Bash 3.2 bug of array initialization with SOH/DEL `#D1238` defdbd4
- syntax: fix a infinite loop for variable assignments and parameter expansions `#D1239` 0000000
- syntax: fix a infinite loop for variable assignments and parameter expansions `#D1239` 327661f

Internal changes and fixes
- internal: merge subdir `test` into `memo` `#D1230` f0c38b6
Expand Down Expand Up @@ -3725,6 +3704,51 @@ bash_tips

2020-01-24

* Bash 3.2 ^A, ^? を含む配列に対する対策 [#D1241]

更に履歴に格納されている値も変化している。
history -s で登録されている値は合っている。
何処でずれるのだろうか。。。

よく考えたらこれは可也広範に亘るのではないだろうか。
そもそも配列の複製 arr2=("${arr1[@]}") を安全に実行できるのだろうか。
これは別項目を立てて対策を考えるべきである。

どういう操作が安全でどういう操作が駄目なのか。
対策する事は可能だろうか。

* arr2=("${arr1[@]}") これは安全の様だ
* arr1=("$del" "$soh") これも安全の様だ

取り敢えず _ble_term_del 等に入れれば良い?
これで declare-print-definitions に関しては大丈夫な気がする。

ble.sh の中で特に問題になりそうなのは何処か。

* history の読み取り。
これはスクリプトを構築してそれを eval している。
うーん。対策を入れてみたが効いていない気がする。
これはまた別の所で問題になっているのだろうか。
或いは置換が効いていない?
→これは簡単なミスだった。修正したら動く様になった。

* binding のキャッシュ?
→これは declare-print-definitions の方を対策した。
恐らく大丈夫だろう。直接 declare -p を呼び出している箇所を全て塞げばOK

* vi のマクロの記録?
これは eval の様な事はしていないので大丈夫の筈。

他に "eval -- ..." となっている箇所を探してみたが恐らく大丈夫。
基本的に直接 ^A や ^? の文字が arr=(...) の形式の中に現れていなければ大丈夫なのだ。

* BASH_REMATCH は local と宣言してから使うべきなのではないか [#D1240]
或いは、local と宣言する事ができない可能性はあるだろうか?
これは試してみる価値はある。と思って試してみたら、
local と宣言する事ができなかった。エラーが表示される。
更に、無視して実行するとグローバルの BASH_REMATCH が書き換えられてしまう。
従って、この方向性に基づく BASH_REMATCH 書き換え対策はできない。

* Bash 3.2 で存在する変数名を入力すると無限ループになる [#D1239]
ble-0.3 では発生していない。

Expand All @@ -3750,7 +3774,7 @@ bash_tips
更に調べると declare -p は ^? を勝手に ^A^? に変換するらしい。面倒だ。
というか配列の要素の中に ^? が含まれている場合、
これを正しく補正して arr=() の形式にする事ができない。うーん。
取り敢えず別項目を立てて処理する事にする #T0003
取り敢えず別項目を立てて処理する事にする #D1241

* ble/syntax/parse の遅延が動いていない [#D1237]
update-syntax で is-function でチェックしているが、
Expand Down
15 changes: 15 additions & 0 deletions memo/D1240.BASH_REMATCH.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# bash source

rex='1..4'; [[ 1444 =~ $rex ]]
declare -p BASH_REMATCH

function fun1 {
local BASH_REMATCH
declare -p BASH_REMATCH
local rex='1..4'
[[ 1234 =~ $rex ]]
declare -p BASH_REMATCH
}
fun1

declare -p BASH_REMATCH
39 changes: 39 additions & 0 deletions memo/D1241.array-SOH-DEL.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# bash -*- mode: sh; mode: sh-bash -*-

if ((!_ble_bash)); then
echo 'This script should be sourced into a ble.sh session' >&2
return 1
elif ((_ble_bash>=40000)); then
echo 'This source script is for bash 3.X' >&2
return 1
fi

echo '# Check arr2=("${arr1[@]}")'
arr1[0]=
arr1[1]=
arr2=("${arr1[@]}")
ble/util/declare-print-definitions arr1 arr2 | cat -A
echo

echo '# Check arr1=(1 "$del" "$soh")'
_ble_term_del=
_ble_term_soh=
arr1=(1 "$_ble_term_del" "$_ble_term_soh")
ble/util/declare-print-definitions arr1 | cat -A
echo

echo '# Check arr1=(2 $del $soh)'
_ble_term_del=
_ble_term_soh=
arr1=(2 $_ble_term_del $_ble_term_soh)
arr2=(2 ''$_ble_term_del'' ''$_ble_term_soh'')
ble/util/declare-print-definitions arr1 arr2 | cat -A
echo

# 以下は全部駄目。値が変化してしまう。
echo "# Check arr1=(3 \$'\\001' \$'\\177')"
arr1=(3 $'\001' $'\177')
arr2=(3  )
arr3=(3 '' '')
ble/util/declare-print-definitions arr1 arr2 arr3 | cat -A
echo
4 changes: 4 additions & 0 deletions src/history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ else
else
gsub(apos, apos "\\" apos apos, line);
# 対策 #D1239 bash-3.2 以前では ^A, ^? が ^A^A, ^A^? に化ける
gsub(/\001/, "'$apos'${_ble_term_soh}'$apos'", line);
gsub(/\177/, "'$apos'${_ble_term_del}'$apos'", line);
t = t != "" ? t "\n" line : line;
}
END {
Expand Down
12 changes: 9 additions & 3 deletions src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,8 @@ function ble/util/declare-print-definitions {
if (_ble_bash < 40000) {
# #D1238 bash-3.2 以前の declare -p は ^A, ^? を
# ^A^A, ^A^? と出力してしまうので補正する。
gsub(/\001\001/, "\001\002", decl);
gsub(/\001\177/, "\177", decl);
gsub(/\001\002/, "\001", decl);
gsub(/\001\001/, "${_ble_term_soh}", decl);
gsub(/\001\177/, "${_ble_term_del}", decl);
}
# declare 除去
Expand Down Expand Up @@ -2907,6 +2906,13 @@ function ble/term:cygwin/initialize.hook {
}

function ble/term/.initialize {
# Constants (init-term.sh に失敗すると大変なので此処に書く)
_ble_term_nl=$'\n'
_ble_term_fs=$'\034'
_ble_term_soh=$'\001'
_ble_term_del=$'\177'
_ble_term_IFS=$' \t\n'

if [[ $_ble_base/lib/init-term.sh -nt $_ble_base_cache/$TERM.term ]]; then
source "$_ble_base/lib/init-term.sh"
else
Expand Down

0 comments on commit 1720ec0

Please sign in to comment.