summaryrefslogtreecommitdiff
path: root/src/lib/libremessages
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libremessages')
-rwxr-xr-xsrc/lib/libremessages81
1 files changed, 58 insertions, 23 deletions
diff --git a/src/lib/libremessages b/src/lib/libremessages
index c6d08e2..b89e2bc 100755
--- a/src/lib/libremessages
+++ b/src/lib/libremessages
@@ -2,28 +2,33 @@
# This may be included with or without `set -euE`
# When run directly, it does `set -euE`
-# Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
-# Copyright (c) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>
-# Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
-# Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
-# Copyright (c) 2006 by Alex Smith <alex@alex-smith.me.uk>
-# Copyright (c) 2006 by Andras Voroskoi <voroskoi@frugalware.org>
-# Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
-# Copyright (c) 2011 by Joshua Haase <hahj87@gmail.com>
-# Copyright (c) 2012-2013 by Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2011 Joshua Ismael Haase Hernández (xihh) <hahj87@gmail.com>
+# Copyright (C) 2012 Nicolás Reynolds <fauno@parabola.nu>
+# Copyright (C) 2012-2014 Luke Shumaker <lukeshu@sbcglobal.net>
+
+# For just the setup_traps() function:
+# Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>
+# Copyright (C) 2006-2010 Pacman Development Team <pacman-dev@archlinux.org>
+# Copyright (C) 2005 Aurelien Foret <orelien@chez.com>
+# Copyright (C) 2005 Christian Hamar <krics@linuxforum.hu>
+# Copyright (C) 2006 Alex Smith <alex@alex-smith.me.uk>
+# Copyright (C) 2006 Andras Voroskoi <voroskoi@frugalware.org>
+# Copyright (C) 2006 Miklos Vajna <vmiklos@frugalware.org>
#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
+# License: GNU GPLv2+
#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
# Inherit most functions from devtools #
@@ -36,7 +41,7 @@
################################################################################
panic() {
- echo "$(_ 'panic: malformed call to internal function')" >&2
+ echo "$(_l _ 'panic: malformed call to internal function')" >&2
exit 1
}
@@ -88,8 +93,6 @@ bullet() {
# bullet.
flag() {
[[ $# == 2 ]] || panic
- local n='
-'
local flag=$1
local desc="$(_ "$(_html_whitespace_collapse <<<"$2")")"
@@ -99,7 +102,7 @@ flag() {
done
local lines
- IFS=$n lines=($(fmt -u -w $((73-indent)) <<<"$desc"))
+ IFS=$'\n' lines=($(fmt -u -w $((73-indent)) <<<"$desc"))
local line
for line in "${lines[@]}"; do
printf " %-${indent}s %s\n" "$flag" "$line"
@@ -119,6 +122,38 @@ term_title() {
printf "$fmt" "$*"
}
+# Usage: setup_traps [handler]
+# Sets up traps on TERM, HUP, QUIT and INT signals, as well as the ERR event,
+# 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() {
+ [[ $# -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
+ done
+ trap '_libremessages_trap_exit INT "Aborted by user! Exiting..."' INT
+ trap '_libremessages_trap_exit USR1 "An unknown error has occurred. Exiting..."' ERR
+}
+
################################################################################
# Run one of the defined functions if invoked directly #
################################################################################