Skip to content

Commit

Permalink
util (ble/builtin/trap): unify handling of "DEBUG" and the other traps
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 9, 2022
1 parent 5c28387 commit a22c25b
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 208 deletions.
8 changes: 6 additions & 2 deletions docs/ChangeLog.md
Expand Up @@ -180,12 +180,18 @@
- 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
- util (`ble/builtin/trap`): run EXIT trap in subshells `#D1862` 5b351e8
- util (`ble/builtin/trap`): fix the RETURN trap `#D1863` 793dfad
- 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
- trap: preserve positional parameters for user trap handlers `#D1865` XXXXXXX
- trap: suppress `INT` processing with user traps `#D1866` XXXXXXX
- trap: unify handling of `DEBUG` and the other traps `#D1867` XXXXXXX
- trap: work around possible interferences by recursive traps `#D1867` XXXXXXX
- trap: ignore `RETURN` for `ble/builtin/trap/.handler` `#D1867` XXXXXXX
- trap: fix a bug that `DEBUG` for internal commands clears `$?` `#D1867` XXXXXXX

## Fixes

Expand Down Expand Up @@ -295,8 +301,6 @@
- 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` 5b351e8
- util (`ble/builtin/trap`): fix the RETURN trap `#D1863` 793dfad

## Documentation

Expand Down
1 change: 1 addition & 0 deletions make_command.sh
Expand Up @@ -1354,6 +1354,7 @@ function sub:scan {
\Ztrap_command=["'\'']trap -- Zd
\Zlocal trap$Zd
\Z"trap -- '\''"Zd
\Z\('\'' trap '\''\*Zd
\Z\(trap \| ble/builtin/trap\) .*;;Zd
\Zble/function#trace trap Zd
\Z# EXIT trapZd
Expand Down
82 changes: 82 additions & 0 deletions memo/D1867.recursiveTrapWA-stub.patch
@@ -0,0 +1,82 @@
From d644e30a2024f729494cdff00891797576f03801 Mon Sep 17 00:00:00 2001
From: Koichi Murase <myoga.murase@gmail.com>
Date: Mon, 29 Aug 2022 11:34:53 +0900
Subject: [PATCH] [stub] recursive trap wa: (implA) array postproc

---
src/util.hook.sh | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/util.hook.sh b/src/util.hook.sh
index b88d004..be80b29 100644
--- a/src/util.hook.sh
+++ b/src/util.hook.sh
@@ -293,7 +293,6 @@ function blehook/eval-after-load {
# blehook

_ble_builtin_trap_inside= # ble/builtin/trap 処理中かどうか
-_ble_builtin_trap_processing= # ble/buitlin/trap/.handler 実行中かどうか

## @fn ble/builtin/trap/.read-arguments args...
## @var[out] flags
@@ -815,6 +814,15 @@ function ble/builtin/trap/invoke {
return 0
} 3>&2 2>/dev/null # set -x 対策 #D0930

+_ble_builtin_trap_processing= # ble/buitlin/trap/.handler 実行中かどうか
+_ble_builtin_trap_depth=0
+_ble_builtin_trap_postproc=()
+_ble_builtin_trap_lastarg=()
+function ble/builtin/trap/install-hook/.compose-trap_command {
+ local sig=$1 name=${_ble_builtin_trap_sig_name[$1]}
+ local handler='ble/builtin/trap/.handler SIGNUM "$BASH_COMMAND" "$@"; builtin eval -- "${_ble_builtin_trap_xpostproc[_ble_builtin_trap_depth]}" \# "${_ble_builtin_trap_xlastarg[_ble_builtin_trap_depth]}"'
+ trap_command="trap -- '${handler/SIGNUM/$sig}' $name"
+}
## @fn ble/builtin/trap/.handler sig bash_command params...
## @param[in] sig
## Specifies the signal number
@@ -822,8 +830,9 @@ function ble/builtin/trap/invoke {
## Specifies the value of BASH_COMMAND in the original context
## @param[in] params...
## Specifies the positional parameters in the original context
-## @var[out] _ble_builtin_trap_postproc
-## @var[out] _ble_builtin_trap_lastarg
+## @var[in] _ble_builtin_trap_depth
+## @var[out] _ble_builtin_trap_xlastarg[_ble_builtin_trap_depth]
+## @var[out] _ble_builtin_trap_xpostproc[_ble_builtin_trap_depth]
function ble/builtin/trap/.handler {
local _ble_trap_lastexit=$? _ble_trap_lastarg=$_ FUNCNEST= IFS=$_ble_term_IFS
local set shopt; ble/base/.adjust-bash-options set shopt
@@ -845,11 +854,8 @@ function ble/builtin/trap/.handler {
shift 2

local _ble_builtin_trap_processing=$_ble_trap_sig
-
- # 透過 _ble_builtin_trap_postproc を設定
- local _ble_local_q=\' _ble_local_Q="'\''"
- _ble_builtin_trap_lastarg=$_ble_trap_lastarg
- _ble_builtin_trap_postproc="ble/util/setexit $_ble_trap_lastexit"
+ local _ble_builtin_trap_lastarg=$_ble_trap_lastarg
+ local _ble_builtin_trap_postproc="ble/util/setexit $_ble_trap_lastexit"

# Note #D1782: ble/builtin/exit で "builtin exit ... &>/dev/null" と
# したリダイレクションを元に戻す。元々 builtin exit が出力するエラー
@@ -915,12 +921,9 @@ function ble/builtin/trap/.handler {
fi

ble/base/.restore-bash-options set shopt
-}
-
-function ble/builtin/trap/install-hook/.compose-trap_command {
- local sig=$1 name=${_ble_builtin_trap_sig_name[$1]}
- local handler="ble/builtin/trap/.handler $sig \"\$BASH_COMMAND\" \"\$@\"; builtin eval -- \"\$_ble_builtin_trap_postproc\" \\# \"\${_ble_builtin_trap_lastarg%%\$_ble_term_nl*}\""
- trap_command="trap -- '$handler' $name"
+ local _ble_builtin_trap_depth=$((_ble_builtin_trap_depth+1))
+ _ble_builtin_trap_xlastarg[_ble_builtin_trap_depth-1]=$_ble_builtin_trap_lastarg
+ _ble_builtin_trap_xpostproc[_ble_builtin_trap_depth-1]=${_ble_builtin_trap_postproc%%$_ble_term_nl*}
}

## @fn ble/builtin/trap/install-hook sig [opts]
--
2.37.2

0 comments on commit a22c25b

Please sign in to comment.