|
|
@ -1,4 +1,5 @@ |
|
|
#!/usr/bin/env bash |
|
|
|
|
|
|
|
|
#!/usr/bin/env -S -- bash |
|
|
|
|
|
#!/usr/bin/env -S -- bash -x |
|
|
|
|
|
|
|
|
# constants |
|
|
# constants |
|
|
|
|
|
|
|
|
@ -47,7 +48,16 @@ function is_true { |
|
|
# HINT: define an alias to bypass ARG evaluation when unnecessary! |
|
|
# HINT: define an alias to bypass ARG evaluation when unnecessary! |
|
|
# example: alias toto="bash -c \"[[ \\\$MIAOU_DEBUG == 'true' ]] && echo yes \\\$* || echo nope\"" |
|
|
# example: alias toto="bash -c \"[[ \\\$MIAOU_DEBUG == 'true' ]] && echo yes \\\$* || echo nope\"" |
|
|
function miaou_debug { |
|
|
function miaou_debug { |
|
|
is_true "${MIAOU_DEBUG}" && >&4 builtin echo -e "\e[90m <== DEBUG ==> \e[33m$*\e[0m" || true |
|
|
|
|
|
|
|
|
if is_true "${MIAOU_DEBUG}"; then |
|
|
|
|
|
[[ ! -v miaou_debug_hierarchy ]] && miaou_debug_hierarchy=0 |
|
|
|
|
|
local i message="$*" padding='' |
|
|
|
|
|
regex='^</' # starts with </ |
|
|
|
|
|
[[ $miaou_debug_hierarchy -gt 0 ]] && [[ "${message}" =~ $regex ]] && ((--miaou_debug_hierarchy)) |
|
|
|
|
|
for i in $(seq 1 "$miaou_debug_hierarchy"); do padding+=' '; done |
|
|
|
|
|
>&4 builtin echo -e "\e[90m <== DEBUG ==> ${padding}\e[33m${message}\e[0m" |
|
|
|
|
|
regex='^<[^/].*[^/]>$' # markup starting |
|
|
|
|
|
[[ "${message}" =~ $regex ]] && ((++miaou_debug_hierarchy)) || true |
|
|
|
|
|
fi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function on_return { |
|
|
function on_return { |
|
|
@ -59,6 +69,7 @@ function on_return { |
|
|
print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" "${failure_type}" "${failure_payload}" |
|
|
print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" "${failure_type}" "${failure_payload}" |
|
|
miaou_debug "<on_return code=$failure_code payload=$failure_payload />" |
|
|
miaou_debug "<on_return code=$failure_code payload=$failure_payload />" |
|
|
fi |
|
|
fi |
|
|
|
|
|
true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
# overridden function which permits backtracing of the original `exit` statement |
|
|
# overridden function which permits backtracing of the original `exit` statement |
|
|
@ -105,7 +116,7 @@ function on_error { |
|
|
failure_code="${1:-200}" |
|
|
failure_code="${1:-200}" |
|
|
>&4 printf '\r' # TODO: force carriage return explicitly, is it really useful? |
|
|
>&4 printf '\r' # TODO: force carriage return explicitly, is it really useful? |
|
|
|
|
|
|
|
|
miaou_debug "<on_error code=${failure_code} command=[${BASH_COMMAND}]>" |
|
|
|
|
|
|
|
|
miaou_debug "<on_error code=${failure_code} command={${BASH_COMMAND}}>" |
|
|
|
|
|
|
|
|
if [[ $$ == "$BASHPID" ]]; then |
|
|
if [[ $$ == "$BASHPID" ]]; then |
|
|
if [[ ! -v failure_type ]]; then |
|
|
if [[ ! -v failure_type ]]; then |
|
|
@ -129,14 +140,14 @@ function on_error { |
|
|
error_funcname=("${error_funcname[@]}") |
|
|
error_funcname=("${error_funcname[@]}") |
|
|
|
|
|
|
|
|
if [[ -v failure_payload ]]; then |
|
|
if [[ -v failure_payload ]]; then |
|
|
miaou_debug "</on_error exit=$failure_code type=${failure_type} payload=${failure_payload}>" |
|
|
|
|
|
|
|
|
miaou_debug "</on_error exit=$failure_code type=${failure_type} payload={${failure_payload}} >" |
|
|
else |
|
|
else |
|
|
miaou_debug "</on_error exit=$failure_code type=${failure_type} >" |
|
|
miaou_debug "</on_error exit=$failure_code type=${failure_type} >" |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
builtin exit "$failure_code" |
|
|
builtin exit "$failure_code" |
|
|
else |
|
|
else |
|
|
miaou_debug "</on_error pipe=$failure_code sub_pid=$BASHPID pid=$$ command='${BASH_COMMAND}'>" |
|
|
|
|
|
|
|
|
miaou_debug "</on_error pipe=$failure_code sub_pid=$BASHPID pid=$$ command={${BASH_COMMAND}}>" |
|
|
print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" 'SUBSHELL_ERROR' "${BASH_COMMAND}" |
|
|
print_miaou_error "${BASH_SOURCE[1]}" "${BASH_LINENO[0]}" "${failure_code}" 'SUBSHELL_ERROR' "${BASH_COMMAND}" |
|
|
kill -PIPE $$ |
|
|
kill -PIPE $$ |
|
|
fi |
|
|
fi |
|
|
@ -169,7 +180,7 @@ function on_pipe { |
|
|
failure_code=141 |
|
|
failure_code=141 |
|
|
failure_type="PIPE_ERROR" |
|
|
failure_type="PIPE_ERROR" |
|
|
fi |
|
|
fi |
|
|
miaou_debug "<on_pipe exit=$failure_code sub_pid=$BASHPID pid=$$/>" |
|
|
|
|
|
|
|
|
miaou_debug "<on_pipe code=$failure_code sub_pid=$BASHPID pid=$$ />" |
|
|
builtin exit "$failure_code" |
|
|
builtin exit "$failure_code" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -237,7 +248,7 @@ function on_exit { |
|
|
last_error="${tmp_error[-1]}" |
|
|
last_error="${tmp_error[-1]}" |
|
|
regex='^(.*): line ([0-9]+): miaou_error: RETURN [0-9]+' |
|
|
regex='^(.*): line ([0-9]+): miaou_error: RETURN [0-9]+' |
|
|
if [[ "$last_error" =~ $regex ]]; then |
|
|
if [[ "$last_error" =~ $regex ]]; then |
|
|
miaou_debug "<return exit=$code />" |
|
|
|
|
|
|
|
|
miaou_debug "<return code=$code />" |
|
|
local source line |
|
|
local source line |
|
|
source="${BASH_REMATCH[1]}" |
|
|
source="${BASH_REMATCH[1]}" |
|
|
line="${BASH_REMATCH[2]}" |
|
|
line="${BASH_REMATCH[2]}" |
|
|
@ -251,7 +262,7 @@ function on_exit { |
|
|
else |
|
|
else |
|
|
if [[ $code -gt 0 && ${#tmp_error[@]} -gt 0 ]]; then |
|
|
if [[ $code -gt 0 && ${#tmp_error[@]} -gt 0 ]]; then |
|
|
last_error="${tmp_error[-1]}" |
|
|
last_error="${tmp_error[-1]}" |
|
|
miaou_debug "<on_exit_tmp_error last='$last_error'>" |
|
|
|
|
|
|
|
|
miaou_debug "<on_exit_tmp_error last={$last_error} >" |
|
|
if [[ $code -eq 1 ]]; then |
|
|
if [[ $code -eq 1 ]]; then |
|
|
regex='^(.*): line ([0-9]+): (.*): unbound variable$' |
|
|
regex='^(.*): line ([0-9]+): (.*): unbound variable$' |
|
|
if [[ "$last_error" =~ $regex ]]; then |
|
|
if [[ "$last_error" =~ $regex ]]; then |
|
|
@ -259,7 +270,7 @@ function on_exit { |
|
|
source="${BASH_REMATCH[1]}" |
|
|
source="${BASH_REMATCH[1]}" |
|
|
line="${BASH_REMATCH[2]}" |
|
|
line="${BASH_REMATCH[2]}" |
|
|
payload="${BASH_REMATCH[3]}" |
|
|
payload="${BASH_REMATCH[3]}" |
|
|
miaou_debug "<unbound_variable source=${source} line=${line} payload=${payload}>" |
|
|
|
|
|
|
|
|
miaou_debug "<unbound_variable source=${source} line=${line} payload={${payload}} >" |
|
|
failure_code=$code |
|
|
failure_code=$code |
|
|
failure_type='UNBOUND_VARIABLE' |
|
|
failure_type='UNBOUND_VARIABLE' |
|
|
failure_payload="${BASH_REMATCH[3]}" |
|
|
failure_payload="${BASH_REMATCH[3]}" |
|
|
|