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__() {
| sed -E $'s/[ \t]+/@/g' \
| tr '@' '\n' \
| grep '^SIG' \
| sed 's/SIG//')
| sed 's/SIG//') EXIT ERR
}
__LECABASH__trap_signal_code__() {
......@@ -29,7 +29,8 @@ __LECABASH__trap_signal_code__() {
| sed -E 's/\)@/ /g' \
| tr '@' '\n' \
| grep "SIG${signal}" \
| awk '{print $1}')
| awk '{print $1}') \
|| echo 0
}
......@@ -41,6 +42,7 @@ __LECABASH__signal_trap_stack_name__() {
}
__LECABASH__trap_function__() {
local exitcode="$?"
local signal="$1"
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
local code="$(__LECABASH__trap_signal_code__ $signal)"
......@@ -54,7 +56,11 @@ __LECABASH__trap_function__() {
loginfo "Commands registered on signal $signal run."
fi
exit "$((128 + code))"
if [[ "$signal" != "EXIT" ]]; then
exit "$((128 + code))"
else
exit "$exitcode"
fi
}
__LECABASH__contains__() {
......@@ -68,30 +74,47 @@ __LECABASH__contains__() {
return 1
}
# Create a trap for all signals
__LECABASH__register_trap_manager__() {
local signals=( $(__LECABASH__trap_signal_list__) )
local used_signal=( $(trap -p | awk '{print $NF}') )
for signal in "${signals[@]}"; do
if ! __LECABASH__contains__ "$s" "${used_signal[@]}"; then
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
newstack $stackname
logdebug "Register trap for signal $signal"
trap "__LECABASH__trap_function__ $signal" "$signal"
fi
done
__LECABASH__istrapregistred__() {
local signal="$1"
local registred=$(trap -p "$signal" \
| grep '__LECABASH__trap_function__' \
| wc -l)
if (( registred == 0 )) ; then
return 1
else
return 0
fi
}
__LECABASH__registered_trap__() {
local signal="$1"
local command=$(trap -p "$signal" \
| awk '{$NF="";\
split($0,parts,"--");\
print parts[2]}')
eval "echo $command"
}
function registertrap() {
local signal=$1
shift
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 : ${*}"
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