#!/usr/bin/env bash # This may be included with or without `set -euE` # When run directly, it does `set -euE` # Copyright (C) 2011 Joshua Ismael Haase Hernández (xihh) # Copyright (C) 2012 Nicolás Reynolds # Copyright (C) 2012-2014 Luke Shumaker # For just the setup_traps() function: # Copyright (C) 2002-2006 Judd Vinet # Copyright (C) 2006-2010 Pacman Development Team # Copyright (C) 2005 Aurelien Foret # Copyright (C) 2005 Christian Hamar # Copyright (C) 2006 Alex Smith # Copyright (C) 2006 Andras Voroskoi # Copyright (C) 2006 Miklos Vajna # # License: GNU GPLv2+ # # 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. # # 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 . ################################################################################ # Inherit most functions from devtools # ################################################################################ . "$(librelib common.sh)" ################################################################################ # Own functions # ################################################################################ # Usage: panic # # For programming errors, bails immediately with little fanfare. panic() { echo "$(_l _ 'panic: malformed call to internal function')" >&2 exit 1 } # Usage: print MESG [ARGS...] # # Like printf, but gettext-aware, and prints a trailing newline print() { [[ $# -ge 1 ]] || panic local mesg="$(_ "$1")" shift printf -- "$mesg\n" "$@" } # Usage: _libremessages_html_whitespace_collapse <<' 'Use this file instead of pacman.conf' # # The description is fed through gettext, the flag is not, so if part # of the flag needs to be translated, you must do that yourself: # # ex: flag "-C <$(_ FILE)>" 'Use this file instead of pacman.conf' # # If you want to line-break the description in the source, so it isn't # crazy-long, feel free, it is reflowed/wrapped the same way as prose # and bullet. flag() { [[ $# == 2 ]] || panic local flag=$1 local desc="$(_ "$(_libremessages_html_whitespace_collapse <<<"$2")")" # Unless the $flag is extra-wide, the $desc should start at # column 16 (that is two literal-tabs). If $flag is wide, # this should be increased in increments of 8 (that is, a # literal-tab). Everything should be wrapped to 75 columns. # The printf-format we use has 3 spaces built into it (two at # the beginning, and a seperator). Therefore, the default # indent is 16-3=13 columns. And the width left for $desc # is (75-3)-indent = 72-indent. declare -i indent=13 while [[ $indent -le ${#flag} ]]; do indent=$((indent+8)) done local lines IFS=$'\n' lines=($(fmt -u -w $((72-indent)) <<<"$desc")) local line for line in "${lines[@]}"; do printf " %-${indent}s %s\n" "$flag" "$line" flag='' done } # Usage: term_title MESG [ARGS...] # # Sets the terminal title. term_title() { [[ $# -ge 1 ]] || panic local fmt='' case "$TERM" in screen|tmux) fmt='\ek%s\e\\';; xterm*|rxvt*) fmt='\e]0;%s\a';; esac printf "$fmt" "$(printf -- "$@")" } # 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. # # This function is based on code from pacman:makepkg 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 # ################################################################################ if [[ "${0##*/}" == libremessages ]]; then set -euE "$@" fi