Skip to content

Commit

Permalink
main: fix adjustments of bash options
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 1, 2022
1 parent cdf74c2 commit 138c476
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ble.pp
Expand Up @@ -715,7 +715,7 @@ function ble/init/clean-up {
# 状態復元
ble/base/restore-BASH_REMATCH
ble/init/restore-IFS
if [[ :$opts: != *:check-attach:* && ! $_ble_attached ]]; then
if [[ :$opts: != *:check-attach:* || ! $_ble_attached ]]; then
ble/base/restore-bash-options
ble/base/restore-POSIXLY_CORRECT
ble/base/restore-builtin-wrappers
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -316,6 +316,7 @@
- progcomp: fix a bug that `COMP_WORDBREAKS` is ignored `#D1872` 4d2dd35
- global: quote `return $?` `#D1884` 801d14a
- canvas (`ble/canvas/trace`): fix text justification for empty lines (reported by rashil2000) `#D1894` xxxxxxx
- main: fix adjustments of bash options (reported by rashil2000) `#D1895` xxxxxxx

## Documentation

Expand Down
25 changes: 25 additions & 0 deletions memo/D1895.bashrc
@@ -0,0 +1,25 @@
# debug

print_posix() {
local tag=$1
if [[ -o posix ]]; then
echo "${tag:+$tag: }posix [POSIXLY_CORRECT=${POSIXLY_CORRECT-(unset)}]"
else
echo "${tag:+$tag: }not posix [POSIXLY_CORRECT=${POSIXLY_CORRECT-(unset)}]"
fi >/dev/pts/3
}
print_posix profile1
set +o posix
print_posix profile2

# Test 1 (prompt attach)
#source ~/.mwg/src/ble.sh/out/ble.sh --attach=prompt
#PROMPT_COMMAND+=('print_posix PROMPT_COMMAND; unset POSIXLY_CORRECT')

# Test 2 (direct attach)
#source ~/.mwg/src/ble.sh/out/ble.sh --attach=attach

# Test 3 (manual attach)
source ~/.mwg/src/ble.sh/out/ble.sh --attach=none
ble-attach
#echo ATTACHED >/dev/tty
70 changes: 70 additions & 0 deletions note.txt
Expand Up @@ -6627,6 +6627,76 @@ bash_tips

2022-11-28

* posix: sh --login at MinGW で沢山エラーメッセージが出て segfault (reported by rashil2000) [#D1895]
https://github.com/akinomyoga/ble.sh/issues/237

% どうも set +p posix してもその効果が次の実行時に消滅するのが原因の様だ。
% Linux などで sh --login をしても再現しない。Cygwin では再現した。Cygwin
% と MinGW だけで今のところ再現している。

取り敢えず set +ev している箇所で +o posix も指定したら問題は発生しなくなる
様である。と思ったが MinGW では問題は起こらなくなるものの Cygwin では依然と
して駄目だった。manual attach でも prompt attach でも Cygwin では動かない。

→何と sh --login で起動した場合は起動時には non posix だがその後で posix
に変化する様だ。

* 一方で Linux で問題が発生していなかったのは単に bashrc が読み込まれていな
かった為である。

また、PROMPT_COMMAND の中から set +o posix した時には問題が生じていない様
にも見える。然し実際に prompt attach を実行してみると問題が生じる。追加で
PROMPT_COMMAND で set +o posix を設定すると問題は生じなくなる。
PROMPT_COMMAND における set +o posix の位置を attach よりも後に変更してみ
ると問題が発生する。

うーん。何らかの瞬間に adjust bash options の制御が変になっている可能性?
そもそも adjust-POSIXLY_CORRECT の呼び出しにまで至っていない気がする。こ
れはユーザーコマンドを実行する瞬間に呼び出される。

1 うーん。元々 ble-attach でもちゃんと adjust は呼び出しているが、そもそ
も既に adjusted の状態になっている為にわざわざ adjust を実行していない
という事の様だ。

そして source ble.sh の時点で調整に入っているのでその後での変更に対して
ちゃんと確認しなくても良いという判断である。というかこれだとユーザーが
自分で set -o posix を設定した時にも問題になる。代わりに他の
bash-options と同じタイミングで restore/adjust するべきなのではないか。

→と思って確認してみた所ちゃんと restore している気がする。と思ったがど
うやら条件判定に失敗して restore がちゃんとされていなかった様だ。これを
直したらちゃんと動く様になった。

% 2 また、何故かこの時点で既に posix モードではなくなっている。或いは関数
% の中でだけ posix モードが自動で消えるなどの仕組みがあるのだろうか。と
% 思ったら単に確認用のスクリプトが間違っていた。

* ble-attach を明示的に呼び出した時にも現在の対策だけで動くのだろうか。或い
は +ev と同様に改めて設定する必要があるだろうか。

% うーん。そもそも +ev と同じ箇所で再調整をしたとしても失敗している。
% segfault する。.hook とは別の場所で調整する必要がある? 取り敢えず
% ble-attach は無事に完了している事は確認した。問題は何処で一番最初に
% ble.sh の処理に突入するかという事である。自然に考えると .hook の筈であ
% る。

→と思ったら単に +ev している箇所が二箇所あったというだけであった。修正し
たら segfault も直った。つまり、何れにしてもこの処理は必要になるという事
である。

* もしこれが問題なのだとすると以前の修正で +ev としていたのは不要だったので
はないか? と思ったが +ev の設定に関しては source ble.sh してから変わると
も思えないし、関係ない? 或いは+ev が必要になったのはユーザーが自分で -ev
を指定した時?

51113237 #D1832 の説明を読むと、これは寧ろ ble-attach を明示的に指定した
時の問題であって、今回の restore し忘れの問題は関係ない。何れにしても
restore はしないのだ。bash -e で起動すると bashrc の読み込み後に設定が変
わるという事の様に見える。なのでやはり必要な気がする。

何れにしても上の項目で +o posix は実行する必要があると判明したので +ev も
保持するのは確定である。

* 2022-10-13 ble/canvas/trace: rps1 の位置の問題 (reported by rashil2000) [#D1894]
https://github.com/akinomyoga/ble.sh/issues/239

Expand Down
4 changes: 2 additions & 2 deletions src/decode.sh
Expand Up @@ -645,7 +645,7 @@ function ble-decode/.hook {

local buflen=${#_ble_decode_input_buffer[@]}
if ((buflen%257==0&&buflen>=2000)); then
[[ ! $_ble_bash_options_adjusted ]] || set +ev
[[ ! $_ble_bash_options_adjusted ]] || set +ev +o posix

local IFS=$_ble_term_IFS
local _ble_decode_hook_Processing=prologue
Expand Down Expand Up @@ -680,7 +680,7 @@ function ble-decode/.hook {

# Note: bind -x 内の set +v は揮発性なのでできるだけ先頭で set +v しておく。
# (PROLOGUE 内から呼ばれる) stdout.on より前であれば大丈夫 #D0930
[[ ! $_ble_bash_options_adjusted ]] || set +ev
[[ ! $_ble_bash_options_adjusted ]] || set +ev +o posix

local IFS=$_ble_term_IFS
local _ble_decode_hook_Processing=prologue
Expand Down

0 comments on commit 138c476

Please sign in to comment.