Skip to content

Commit

Permalink
util (ble/builtin/trap): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 9, 2022
1 parent 007eef8 commit 55a182b
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 115 deletions.
2 changes: 1 addition & 1 deletion contrib
Submodule contrib updated 1 files
+1 −1 config/execmark.bash
30 changes: 17 additions & 13 deletions docs/ChangeLog.md
Expand Up @@ -56,7 +56,7 @@
- util (`bleopt`): do no select obsoleted options by wildcards `#D1595` f4312df
- util (`bleopt`): fix error messages for unknown options `#D1610` 66df3e2
- util (`bleopt`, `bind`): fix error message and exit status, respectively `#D1640` b663cee
- util (`blehook`): support wildcards `#D1861` XXXXXXX
- util (`blehook`): support wildcards `#D1861` 480b7b3
- progcomp: support quoted commands and better `progcomp_alias` `#D1581` `#D1583` dbe87c3
- progcomp: fix a bug that command names may stray into completer function names `#D1611` 1f2d45f
- syntax: highlight quotes of the `\?` form `#D1584` 5076a03
Expand Down Expand Up @@ -106,7 +106,7 @@
- util: suppress false warnings of `bind` inside non-interactive shells (reported by wukuan405) `#D1823` 1e19a67
- history: support `bleopt history_erasedups_limit` (motivated by SuperSandro2000) `#D1822` e4afb5a 3110967
- prompt: support `bleopt prompt_{emacs,vi}_mode_indicator` (motivated by ferdinandyb) `#D1843` 2b905f8
- util (`ble-import`): support option `-q` `#D1859` XXXXXXX
- util (`ble-import`): support option `-q` `#D1859` 1ca87a9

## Changes

Expand Down Expand Up @@ -134,7 +134,7 @@
- prompt: update `PS0` between multiple commands (motivated by tycho-kirchner) `#D1560` 8f29203
- edit (`widget:display-shell-version`): include `ble.sh` version `#D1545` 750ca38
- edit (`display-shell-version`): detect configurations and print details `#D1781` 5015cb56
- edit (`display-shell-version`): show information of the OS distribution and properly handle saved locales `#D1854` 066ec63 XXXXXXX
- edit (`display-shell-version`): show information of the OS distribution and properly handle saved locales `#D1854` 066ec63 bdb7dd6
- complete (`ble-sabbrev`): support colored output `#D1546` 750ca38
- decode (`ble-bind`): support colored output `#D1547` 750ca38
- decode (`ble-bind`): output bindings of the specified keymaps with `ble-bind -m KEYMAP` (fixup 750ca38) `#D1559` 6e0245a
Expand Down Expand Up @@ -174,12 +174,16 @@
- complete: do not show option descriptions for the empty-word completion (requested by geekscrapy) `#D1846` 1c7f7a1
- syntax (`extract-command`): extract unexpected command names as commands `#D1848` 5b63459
- main (`ble-reload`): preserve the original initialization options `#D1852` d8c92cc
- blehook: print reusable code to restore the user hooks `#D1857` XXXXXXX
- blehook: separate internal and user hooks `#D1856` XXXXXXX
- trap,hook: refactor `ERR` `#D1858` XXXXXXX
- blehook: rename `ERR{ => EXEC}` and separate from the `ERR` trap
- trap: remove the support for the shell function `TRAPERR`
- trap: preserve `BASH_COMMAND` in trap handlers
- blehook: print reusable code to restore the user hooks `#D1857` b763677
- blehook: separate internal and user hooks `#D1856` b763677
- trap: refactor
- trap,blehook: rename `ERR{ => EXEC}` and separate from the `ERR` trap `#D1858` 94d1371
- trap: remove the support for the shell function `TRAPERR` `#D1858` 94d1371
- trap: preserve `BASH_COMMAND` in trap handlers `#D1858` 94d1371
- trap,blehook: move to a new file `util.hook.sh` `#D1864` XXXXXXX
- trap (`trap -p`): fix unprinted existing user traps `#D1864` XXXXXXX
- trap (`ble/builtin/trap/finalize`): fix a failure of restoring the original trap `#D1864` XXXXXXX
- trap (`trap -p`): print also custom traps `#D1864` XXXXXXX

## Fixes

Expand Down Expand Up @@ -289,8 +293,8 @@
- complete: fix wrong `COMP_POINT` with `progcomp_alias` `#D1841` 369f7c0
- main (`ble-update`): fix error message with system-wide installation of `ble.sh` (fixed by tars0x9752) 1d2a9c1 a450775
- main. util: fix problems of readlink etc. found by test in macOS (reported by aiotter) `#D1849` fa955c1 `#D1855` a22e145
- util (`ble/builtin/trap`): run EXIT trap in subshells `#D1862` XXXXXXX
- util (`ble/builtin/trap`): fix the RETURN trap `#D1863` XXXXXXX
- util (`ble/builtin/trap`): run EXIT trap in subshells `#D1862` 5b351e8
- util (`ble/builtin/trap`): fix the RETURN trap `#D1863` 793dfad

## Documentation

Expand Down Expand Up @@ -430,8 +434,8 @@
- util (`fd#alloc`): limit the search range of free fds `#D1813` 43be0e4 4c90072
- github/workflows: define an action for the nightly builds (contributed by uyha) `#D1814` a3082a0
- global: quote numbers for unexpected `IFS` `#D1835` 0179afc
- history: refactor hooks `history_{{delete,clear,insert} => change}` `#D1860` XXXXXXX
- history: rename the hook `history_{on => }leave` `#D1860` XXXXXXX
- history: refactor hooks `history_{{delete,clear,insert} => change}` `#D1860` c393c93
- history: rename the hook `history_{on => }leave` `#D1860` c393c93

## Contrib

Expand Down
3 changes: 1 addition & 2 deletions lib/test-util.sh
Expand Up @@ -1158,8 +1158,7 @@ function is-global() (readonly "$1"; ! local "$1" 2>/dev/null)
ble/test 'ble/builtin/trap/invoke HUP' stdout=

# 9999 / CUSTOM (custom trap)
ble/builtin/trap/.register 9999 CUSTOM
ble/builtin/trap/reserve CUSTOM
ble/builtin/trap/sig#new CUSTOM
ble/builtin/trap 'echo custom trap' CUSTOM
ble/test 'ble/builtin/trap/invoke CUSTOM' stdout='custom trap'
function ble/builtin/trap:CUSTOM { echo "__set_handler__ ($2) $1"; }
Expand Down
3 changes: 1 addition & 2 deletions make_command.sh
Expand Up @@ -1353,8 +1353,7 @@ function sub:scan {
\Zline = "bind"Zd
\Ztrap_command=["'\'']trap -- Zd
\Zlocal trap$Zd
\Z\$\{content#"trap -- '\''"\}Zd
\Z\("trap -- '\''.*"\*\)Zd
\Z"trap -- '\''"Zd
\Z\(trap \| ble/builtin/trap\) .*;;Zd
\Zble/function#trace trap Zd
\Z# EXIT trapZd
Expand Down
39 changes: 38 additions & 1 deletion note.txt
Expand Up @@ -6634,9 +6634,46 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2022-08-25

* trap: refactor and fix ble/builtin/trap [#D1864]

* ble/builtin/trap 及び blehook 関連のコードが util.sh の中で肥大化して、他
の util 内の関数に対する依存性が問題になってきている。より後ろで定義され
ている関数に依存しているので初期化を遅延させるなどしなければならず不都合
である。コードをもっと後ろに移動する代わりに、ファイルを分離する事にした。

* fixed: 元からあった trap は出力されるのか

これは実装を確認してみると出力されない気がする。実際に試してみるとやはり
出力されない。修正した。

* fixed: そもそも ble/base/unload した時に元の trap を復元するべきなのでは
ないかという気がする。と思って確認してみた所、元から復元する様になってい
た。然しちゃんと動いていない。

* fixed: 然し、trap - INT という設定が外側に伝播しない様だ。EXIT はちゃん
と解除されるが INT と WINCH は外側へ行かない。

と思ったら単に $sig を参照する所を存在しない変数 $index を参照して trap
に渡すシグナル名を決めていたのが原因だった。修正した。

* fixed: というか復元するコードでカスタムシグナルに対しても勝手に trap が
呼び出されてしまう。これは修正するべき。特に install-hook 等を通して設
定された物だけを復元する。

* ok: ble-reload した時に元々ある _ble_builtin_trap_handlers を保持するべき
なのではないか? 現在は上書きしてしまっているので install-hook 経由で設定
されている hook 等は消滅してしまう気がする。

→これは ble/base/unload の際にちゃんと builtin trap を復元する様にしたら
動く様になったので良しとする。

* done: trap CUSTOM なども trap -p で出力

2022-08-24

* RETURN trap はちゃんと動くのだろうか [#D1863]
* trap: RETURN trap はちゃんと動くのだろうか [#D1863]

現在の trap の実装で RETURN はちゃんと設定できるのだろうか? → 試してみた所、
ちゃんと意図した箇所で RETURN trap が呼び出されているが、関係ない場所でも
Expand Down
4 changes: 2 additions & 2 deletions src/edit.sh
Expand Up @@ -6116,7 +6116,7 @@ _ble_edit_exec_TRAPDEBUG_EXIT=
_ble_edit_exec_inside_begin=
_ble_edit_exec_inside_prologue=
_ble_edit_exec_inside_userspace=
ble/builtin/trap/reserve DEBUG
ble/builtin/trap/sig#reserve DEBUG override-builtin-signal

## @fn ble-edit/exec:gexec/.TRAPDEBUG/trap [opts]
## @param[in] opts
Expand Down Expand Up @@ -6155,7 +6155,7 @@ ble/function#trace _ble_edit_exec_gexec__TRAPDEBUG_adjust
function ble-edit/exec:gexec/.TRAPDEBUG/restore {
_ble_edit_exec_TRAPDEBUG_adjusted=
local opts=$1
ble/builtin/trap/.initialize
ble/builtin/trap/sig#init
if [[ ${_ble_builtin_trap_handlers[_ble_builtin_trap_DEBUG]} ]]; then
ble-edit/exec:gexec/.TRAPDEBUG/trap "$opts"
fi
Expand Down
4 changes: 2 additions & 2 deletions src/history.sh
Expand Up @@ -56,7 +56,7 @@ else
((stat)) || return 0

local ret=11
ble/builtin/trap/.get-sig-index SIGSEGV
ble/builtin/trap/sig#resolve SIGSEGV
((stat==128+ret)) || return 0

local msg="bash: SIGSEGV: suspicious timestamp in HISTFILE"
Expand Down Expand Up @@ -102,7 +102,7 @@ if ((_ble_bash<40000)); then
if ((ext==142)); then
printf 'ble.sh: timeout: builtin history %s' "$*" >/dev/tty
local ret=11
ble/builtin/trap/.get-sig-index SIGSEGV
ble/builtin/trap/sig#resolve SIGSEGV
((ext=128+ret))
fi
ble/builtin/history/.check-timestamp-sigsegv "$ext"
Expand Down

0 comments on commit 55a182b

Please sign in to comment.