Commit dec4642c by Eric Coissac

- Create the signal handler only on purpose.

- Deal correctly with the pseudo signals EXIT and ERR generated by bash
parent 2fb5dd62
...@@ -19,7 +19,7 @@ __LECABASH__trap_signal_list__() { ...@@ -19,7 +19,7 @@ __LECABASH__trap_signal_list__() {
| sed -E $'s/[ \t]+/@/g' \ | sed -E $'s/[ \t]+/@/g' \
| tr '@' '\n' \ | tr '@' '\n' \
| grep '^SIG' \ | grep '^SIG' \
| sed 's/SIG//') | sed 's/SIG//') EXIT ERR
} }
__LECABASH__trap_signal_code__() { __LECABASH__trap_signal_code__() {
...@@ -29,7 +29,8 @@ __LECABASH__trap_signal_code__() { ...@@ -29,7 +29,8 @@ __LECABASH__trap_signal_code__() {
| sed -E 's/\)@/ /g' \ | sed -E 's/\)@/ /g' \
| tr '@' '\n' \ | tr '@' '\n' \
| grep "SIG${signal}" \ | grep "SIG${signal}" \
| awk '{print $1}') | awk '{print $1}') \
|| echo 0
} }
...@@ -41,6 +42,7 @@ __LECABASH__signal_trap_stack_name__() { ...@@ -41,6 +42,7 @@ __LECABASH__signal_trap_stack_name__() {
} }
__LECABASH__trap_function__() { __LECABASH__trap_function__() {
local exitcode="$?"
local signal="$1" local signal="$1"
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})" local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
local code="$(__LECABASH__trap_signal_code__ $signal)" local code="$(__LECABASH__trap_signal_code__ $signal)"
...@@ -54,7 +56,11 @@ __LECABASH__trap_function__() { ...@@ -54,7 +56,11 @@ __LECABASH__trap_function__() {
loginfo "Commands registered on signal $signal run." loginfo "Commands registered on signal $signal run."
fi fi
exit "$((128 + code))" if [[ "$signal" != "EXIT" ]]; then
exit "$((128 + code))"
else
exit "$exitcode"
fi
} }
__LECABASH__contains__() { __LECABASH__contains__() {
...@@ -68,30 +74,47 @@ __LECABASH__contains__() { ...@@ -68,30 +74,47 @@ __LECABASH__contains__() {
return 1 return 1
} }
# Create a trap for all signals __LECABASH__istrapregistred__() {
local signal="$1"
__LECABASH__register_trap_manager__() { local registred=$(trap -p "$signal" \
local signals=( $(__LECABASH__trap_signal_list__) ) | grep '__LECABASH__trap_function__' \
local used_signal=( $(trap -p | awk '{print $NF}') ) | wc -l)
for signal in "${signals[@]}"; do if (( registred == 0 )) ; then
if ! __LECABASH__contains__ "$s" "${used_signal[@]}"; then return 1
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})" else
newstack $stackname return 0
logdebug "Register trap for signal $signal" fi
trap "__LECABASH__trap_function__ $signal" "$signal"
fi
done
} }
__LECABASH__registered_trap__() {
local signal="$1"
local command=$(trap -p "$signal" \
| awk '{$NF="";\
split($0,parts,"--");\
print parts[2]}')
eval "echo $command"
}
function registertrap() { function registertrap() {
local signal=$1 local signal=$1
shift shift
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})" local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
if ! __LECABASH__istrapregistred__ "$signal" ; then
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
newstack $stackname
local oldcmd=$(__LECABASH__registered_trap__ "$signal")
if [[ oldcmd != "" ]] ; then
pushvalue "$stackname" "${oldcmd}"
fi
logdebug "Register trap for signal $signal"
trap "__LECABASH__trap_function__ $signal" "$signal"
fi
loginfo "Register new command on $signal : ${*}" loginfo "Register new command on $signal : ${*}"
pushvalue "$stackname" "${*}" pushvalue "$stackname" "${*}"
} }
__LECABASH__register_trap_manager__
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment