Scott M. Mcdermott

UNIX Systems & Network Administrator
available for contract or salaried positions

bomb

#
# error message to stderr, with optional termination
# depending on invocation name:
#
#       warn:  do not exit
#       bomb:  exit
#       error: exit
#

require get_program_name

__bomb_internal ()
{
    local message="$@"
    local progname=`get_program_name`
    local stackdepth=$((${#FUNCNAME[@]} - 2))
    local i callstack

    for ((i = stackdepth; i > 1; i--)); do
        callstack="${callstack}${FUNCNAME[i]}: "
    done

    (echo "${progname}: ${callstack}${message}" | fmt -tw 78 >&2)

    ((LIBSH_ERROR_EXIT)) && exit 1
}

warn  () { LIBSH_ERROR_EXIT=0; __bomb_internal "$@"; } # do not exit
bomb  () { LIBSH_ERROR_EXIT=1; __bomb_internal "$@"; } # exit 1
error () { LIBSH_ERROR_EXIT=1; __bomb_internal "$@"; } # identical to bomb()
# vim:syn=sh:ft=sh