Skip to content

Commit

Permalink
util: preserve original traps and restore them on unload
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Feb 16, 2022
1 parent 9b96578 commit 398e404
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 44 deletions.
12 changes: 9 additions & 3 deletions ble.pp
Expand Up @@ -1564,12 +1564,13 @@ function ble/base/unload {
ble/term/stty/TRAPEXIT
ble/term/leave
ble/util/buffer.flush >&2
ble/fd#finalize
ble/util/import/finalize
blehook/invoke unload
ble/decode/keymap#unload
ble-edit/bind/clear-keymap-definition-loader
ble/builtin/trap/finalize
ble/util/import/finalize
ble/fd#finalize
ble/bin/rm -rf "$_ble_base_run/$$".* 2>/dev/null
blehook/invoke unload
return 0
}
blehook EXIT+=ble/base/unload
Expand Down Expand Up @@ -1765,6 +1766,11 @@ function ble/base/sub:clear-cache {
ble/function#trace ble/dispatch
ble/function#trace ble/base/attach-from-PROMPT_COMMAND

# Note #D1775: 以下は ble/base/unload 時に元の trap または ble.sh 有効
# 時にユーザーが設定した trap を復元する為に用いる物。
ble/function#trace ble/base/unload
ble/function#trace ble/builtin/trap/finalize

ble-import -f lib/_package
if [[ $_ble_init_command ]]; then
ble/base/sub:"$_ble_init_command"; _ble_init_exit=$?
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -91,6 +91,7 @@
- prompt: support `bleopt prompt_command_changes_layout` `#D1750` e199bee
- exec: measure execution times `#D1756` 2b28bec
- edit: work around a bash-4.4..5.1 bug of `exit` outputting time to stderr of exit context `#D1765` 3de751e 0000000
- util: preserve original traps and restore them on unload `#D1775` `#D1776` `#D1777` 0000000

## Changes

Expand Down
86 changes: 50 additions & 36 deletions note.txt
Expand Up @@ -1768,6 +1768,39 @@ bash_tips
* return without arguments in trap handlers. これは結局強い理由がないと変更さ
れない雰囲気になっている。

2020-04-25

* starship コマンド実行時間の計測
preexec と precmd を使っている?
https://github.com/starship/starship/blob/master/src/init/starship.bash

追記 2022-02-17 これは ble.sh でも類似の物を実装した。

* pipexec という物があるそうだ。と思ったが調べたら C で書かれている。
https://github.com/flonatel/pipexec

* zsh のテーマである powerlevel10k は実は結構複雑な処理を実装している。
ごちゃごちゃとした雑多の設定の寄せ集めではない。
ble.sh 程ではないが単にプロンプトと呼べるレベルを超えている。
* https://github.com/romkatv/powerlevel10k
* https://github.com/Powerlevel9k/powerlevel9k
p9k と initial commit が同じなので再実装というよりは fork の気がする。

https://github.com/aristocratos/bashtop
これは最近現れた物で pure bash で色々な UI を実装している。
オプション引数はなく設定は直接編集する様になっている。
背景が明るい時の配色に対応していない。256色要求。

* 実用性よりも見た目重視。これはツールの性格による。
ble.sh 自体は他のプログラムを呼び出す為の物なので主張は控え目。
然し、宣伝の為には見た目を派手にした物も必要なのかもしれない。
* 何故か現れたばかりなのに 6.3k も集まっているし、
一体何が起こるとこのように話題になるのだろうか。不思議である。
HN にも reddit にも対して人気になった物は見られない。
何処から広まってどう人気になったのか不明である。
* freebsd, aur, debian, fedora/centos にまでパッケージが在る。


@todo
*******************************************************************************
ToDo
Expand All @@ -1778,9 +1811,6 @@ bash_tips
* ble/builtin/trap DEBUG は関数の入れ子や trace 属性などは考慮していない。こ
の事によって問題が起こる可能性もある。

* DEBUG trap 以外についても元からあった trap を拾う様にしたい
Ref #D1772

* 関数の定義位置を DEBUG trap 等を使って抽出する事が可能なのではないか?

2022-02-12
Expand Down Expand Up @@ -3957,11 +3987,6 @@ bash_tips
"${_histcmd@P}" が使える (bash 4.4)。HISTCMD が unset されているか
どうかは HISTCMD=A して値が変化するかどうかで判定できる。

2020-09-01

* trap: ble.sh で上書きする時に元々存在していた trap はどうなっていたか。
* trap: ble.sh を unload する時に復元する仕組みがあっても良いのではないか。

2020-08-27

* 真面目に宣伝など考えるべきなのかもしれない。
Expand Down Expand Up @@ -4263,34 +4288,6 @@ bash_tips

2020-04-25

* starship コマンド実行時間の計測
preexec と precmd を使っている?
https://github.com/starship/starship/blob/master/src/init/starship.bash

pipexec という物があるそうだ。と思ったが調べたら C で書かれている。
https://github.com/flonatel/pipexec

zsh のテーマである powerlevel10k は実は結構複雑な処理を実装している。
ごちゃごちゃとした雑多の設定の寄せ集めではない。
ble.sh 程ではないが単にプロンプトと呼べるレベルを超えている。
* https://github.com/romkatv/powerlevel10k
* https://github.com/Powerlevel9k/powerlevel9k
p9k と initial commit が同じなので再実装というよりは fork の気がする。

https://github.com/aristocratos/bashtop
これは最近現れた物で pure bash で色々な UI を実装している。
オプション引数はなく設定は直接編集する様になっている。
背景が明るい時の配色に対応していない。256色要求。

* 実用性よりも見た目重視。これはツールの性格による。
ble.sh 自体は他のプログラムを呼び出す為の物なので主張は控え目。
然し、宣伝の為には見た目を派手にした物も必要なのかもしれない。
* 何故か現れたばかりなのに 6.3k も集まっているし、
一体何が起こるとこのように話題になるのだろうか。不思議である。
HN にも reddit にも対して人気になった物は見られない。
何処から広まってどう人気になったのか不明である。
* freebsd, aur, debian, fedora/centos にまでパッケージが在る。

* trap: DEBUG trap を用いて DEBUG trap を再現できるか? [#T0003]
参考: #M0016

Expand Down Expand Up @@ -6118,6 +6115,23 @@ bash_tips

2022-02-17

* 2020-09-01 trap: ble.sh を unload する時に復元する仕組みがあっても良いのではないか [#D1777]
#D1775 と同時に対応した。

* 2020-09-01 desolved: trap: ble.sh で上書きする時に元々存在していた trap はどうなっていたか [#D1776]
Ref #D1775

* util: DEBUG trap 以外についても元からあった trap を拾う様にしたい [#D1775]
Ref #D1772

RETURN と ERR 以外については中から普通に trap を見ることができる筈なので、
そのまま中から値を抽出するというので良い。現在使用しているのは EXIT, WINCH,
INT である。他に bash3 等で USR1 も使っていた筈。

実際に trap を設定しているのは ble/builtin/trap/install-hook の一箇所のみで
ある。ここで trap を読み取って ble/builtin/trap の枠組みで再設定すれば良い。
対応した。

* bash-3.0 でコマンドを実行するとプロンプトの前に変な文字列 '' が出力される [#D1774]

うーん。家に帰ったら再現しなくなった。と思ったら再現の仕方が変化した。C-c
Expand Down
41 changes: 36 additions & 5 deletions src/util.sh
Expand Up @@ -1981,6 +1981,19 @@ function ble/builtin/trap/reserve {
ble/builtin/trap/.get-sig-index "$1" || return 1
_ble_builtin_trap_reserved[ret]=1
}
function ble/builtin/trap/finalize {
local sig
for sig in "${!_ble_builtin_trap_reserved[@]}"; do
local name=${_ble_builtin_trap_signames[index]}
[[ $name && ${_ble_builtin_trap_reserved[sig]} ]] || continue
if [[ ${_ble_builtin_trap_handlers[sig]+set} ]]; then
builtin trap -- "${_ble_builtin_trap_handlers[sig]+set}" "$name"
else
builtin trap -- - "$name"
fi
done
}

function ble/builtin/trap {
local set shopt; ble/base/.adjust-bash-options set shopt
local flags command sigspecs
Expand Down Expand Up @@ -2187,6 +2200,8 @@ function ble/builtin/trap/install-hook {

local handler="ble/builtin/trap/.handler $sig ${name#SIG}; builtin eval -- \"\$_ble_builtin_trap_postproc\" \\# \"\$_ble_builtin_trap_lastarg\""
local trap_command="trap -- '$handler' $name"
local trap_string; ble/util/assign trap_string "builtin trap -p $name"

if [[ $opts == *:readline:* ]] && ! ble/util/is-running-in-subshell; then
# Note #D1345: ble.sh の内部で "builtin trap -- WINCH" 等とすると
# readline の処理が行われなくなってしまう (COLUMNS, LINES が更新さ
Expand All @@ -2207,12 +2222,28 @@ function ble/builtin/trap/install-hook {
# - INT は bind -x 内だと改めて設定しないと有効にならない(?)様なの
# で既に登録されていても、builtin trap は省略できない。
#
local trap
ble/util/assign trap "builtin trap -p $name"
[[ $trap_command == "$trap" ]] && return 0
fi
[[ $trap_command == "$trap_string" ]] && return 0
fi

builtin eval "builtin $trap_command"; local ext=$?

case $trap_string in
("trap -- 'ble/builtin/trap/"*) ;; # ble-0.4
("trap -- 'ble/base/unload"*|"trap -- 'ble-edit/"*) ;; # bash-0.3 以前
("trap -- '"*)
# Note: 既存の handler がない時のみ設定を読み取る。既存の設定がある時は
# ble.sh をロードしてから trap が実行された事を意味する。一方で、ble.sh が
# ロードされて以降に builtin trap の設定がユーザーによって直接変更される事
# は想定していないので、builtin trap から読み取った結果は ble.sh ロード前と
# 想定して良い。
[[ ! ${_ble_builtin_trap_handlers[sig]+set} ]] &&
# Note: 1000 以上はデバグ用の trap (DEBUG, RETURN, EXIT) で既定では trap
# が関数呼び出しで継承されないので、trap_string の内容は信用できない。
((sig<1000)) &&
builtin eval -- "ble/builtin/$trap_string" ;;
esac

builtin eval "builtin $trap_command"
return "$ext"
}

#------------------------------------------------------------------------------
Expand Down

0 comments on commit 398e404

Please sign in to comment.