|
|
|
@ -8,15 +8,11 @@ readonly MIAOU_BASH_ERROR MIAOU_BASH_DEBUG |
|
|
|
|
|
|
|
# FUNCTIONS |
|
|
|
|
|
|
|
function on_return { |
|
|
|
# regex does not work here unfortunately! |
|
|
|
if [[ "${BASH_COMMAND}" == 'return '* ]]; then |
|
|
|
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) |
|
|
|
if [[ -n "$code" ]] && [[ "$code" -ne 0 ]]; then |
|
|
|
trap - DEBUG |
|
|
|
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}" |
|
|
|
fi |
|
|
|
fi |
|
|
|
# overridden function which permits backtracing of original `return` statement |
|
|
|
function false { |
|
|
|
>&2 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} false" |
|
|
|
debug "overridden false" |
|
|
|
builtin false |
|
|
|
} |
|
|
|
|
|
|
|
# overridden function which permits backtracing of original `exit` statement |
|
|
|
@ -28,6 +24,17 @@ function exit { |
|
|
|
builtin exit "$code" |
|
|
|
} |
|
|
|
|
|
|
|
function on_return { |
|
|
|
# regex does not work here unfortunately! |
|
|
|
if [[ "${BASH_COMMAND}" == 'return '* ]]; then |
|
|
|
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) |
|
|
|
if [[ -n "$code" ]] && [[ "$code" -ne 0 ]]; then |
|
|
|
trap - DEBUG |
|
|
|
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}" |
|
|
|
fi |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
|
function on_exit { |
|
|
|
code=${1:-0} |
|
|
|
|
|
|
|
@ -48,8 +55,8 @@ function on_exit { |
|
|
|
unset 'stack_sources[0]' |
|
|
|
unset 'stack_sources[-1]' |
|
|
|
|
|
|
|
if [[ "${#stack_functions[@]}" -gt 1 && "${stack_functions[1]}" == exit && "${stack_sources[1]}" == "${BASH_SOURCE[0]}" ]]; then |
|
|
|
# the exit case scenario |
|
|
|
if [[ "${#stack_functions[@]}" -gt 1 && "${stack_functions[1]}" =~ exit|false && "${stack_sources[1]}" == "${BASH_SOURCE[0]}" ]]; then |
|
|
|
# the exit or false case scenario |
|
|
|
unset 'stack_functions[1]' |
|
|
|
unset 'stack_sources[1]' |
|
|
|
unset 'stack_lines[0]' |
|
|
|
@ -70,6 +77,7 @@ function on_exit { |
|
|
|
last_source=${last_source//./"\."} # replace '.' with '\.' |
|
|
|
regex1="^$last_source: line ([0-9]+): (.*)\$" |
|
|
|
regex2="^$last_source:([0-9]+) (.*)\$" |
|
|
|
# debug "last_error=[$last_error] <=> ${last_source}" |
|
|
|
if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then |
|
|
|
line=${BASH_REMATCH[1]} |
|
|
|
message=${BASH_REMATCH[2]} |
|
|
|
@ -80,7 +88,9 @@ function on_exit { |
|
|
|
failure_payload="${BASH_REMATCH[1]}" |
|
|
|
stack_lines[0]=$line |
|
|
|
else |
|
|
|
|
|
|
|
if [[ "$message" == 'false' ]]; then |
|
|
|
failure_type='FALSE' |
|
|
|
else |
|
|
|
regex="^return ([0-9]*) from (.*)\$" |
|
|
|
if [[ "$message" =~ $regex ]]; then |
|
|
|
# return_code="${BASH_REMATCH[1]}" |
|
|
|
@ -132,6 +142,7 @@ function on_exit { |
|
|
|
fi |
|
|
|
fi |
|
|
|
fi |
|
|
|
fi |
|
|
|
else |
|
|
|
failure_type='UNDEFINED' |
|
|
|
debug "last error=${last_error}" |
|
|
|
@ -167,7 +178,7 @@ function on_exit { |
|
|
|
} |
|
|
|
|
|
|
|
function debug { |
|
|
|
$MIAOU_BASH_DEBUG && >/dev/tty echo "*********** DEBUG: $*" |
|
|
|
[[ "${MIAOU_BASH_DEBUG}" == true ]] && >/dev/tty echo "*********** DEBUG: $*" || |
|
|
|
true |
|
|
|
} |
|
|
|
|
|
|
|
@ -227,7 +238,7 @@ BASH_ARGV0="$1" && shift # to pretend script runs by itself |
|
|
|
# magic FD + delayed tee => /dev/fd/3 means delayed stderr, /dev/fd/4 remains original stderr |
|
|
|
exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr |
|
|
|
|
|
|
|
$MIAOU_BASH_DEBUG && >/dev/tty echo '---stdout---' |
|
|
|
debug '---stdout---' |
|
|
|
# shellcheck source=/dev/null |
|
|
|
source "$BASH_ARGV0" 2>&3 |
|
|
|
dump_success |