From 74f68d0149f2a94780c860b0775d1e880dbe9225 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 5 Mar 2014 10:57:57 -0500 Subject: teach libremessages:setup_traps to be able to use a custom signal handler --- src/lib/libremessages | 31 ++++++++++++++++++++++--------- src/lib/libremessages.1.ronn | 9 +++++++-- 2 files changed, 29 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/lib/libremessages b/src/lib/libremessages index e5b7157..162c8fb 100755 --- a/src/lib/libremessages +++ b/src/lib/libremessages @@ -117,17 +117,30 @@ term_title() { printf "$fmt" "$*" } -# Usage: setup_traps +# Usage: setup_traps [handler] # Sets up traps on TERM, HUP, QUIT and INT signals, as well as the ERR event, -# similar to makepkg +# similar to makepkg. +# +# If `handler` is specified, instead of using the default handler +# (which is good for most purposes), it will call the command handler +# with the arguments: +# +# ${handler} SIGNAL_NAME MESSAGE_FMT [MESSAGE_PARAMS...] +# +# where MESSAGE_* are printf-like stuff. setup_traps() { - _libremessages_trap_exit() { - local signal=$1; shift - echo - error "$@" - trap -- "$signal" - kill "-$signal" "$$" - } + [[ $# -le 1 ]] || panic + if [[ $# == 1 ]]; then + eval "_libremessages_trap_exit() { $1 \"\$@\"; }" + else + _libremessages_trap_exit() { + local signal=$1; shift + echo + error "$@" + trap -- "$signal" + kill "-$signal" "$$" + } + fi set -E for signal in TERM HUP QUIT; do trap "_libremessages_trap_exit $signal '%s signal caught. Exiting...' $signal" $signal diff --git a/src/lib/libremessages.1.ronn b/src/lib/libremessages.1.ronn index d4c35fc..d39dad0 100644 --- a/src/lib/libremessages.1.ronn +++ b/src/lib/libremessages.1.ronn @@ -69,9 +69,14 @@ Unless otherwise noted, these do not implicitly call `gettext`. Joins all arguments with whitespace, and sets the terminal title to that. - * `setup_traps`: + * `setup_traps` []: Sets traps on TERM, HUP, QUIT and INT signals, as sell as the ERR - event, similar to makepkg. + event, similar to makepkg. If is specified, instead of + using the default handler (which is good for most purposes), it + will call with the arguments + ` [...]`, where + is a `printf`(1)-formatted string, and + are its arguments. ### PROSE ROUTINES -- cgit v1.2.2