From 7e704d7ac997387341e920e1757c24cac0efe5e9 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 8 Feb 2016 16:36:45 -0500 Subject: Refactor the build system. Avoid recursive make. This looks like a lot, but more things should "just work". We have `make dist` now! --- src/lib/Makefile | 44 +++++----- src/lib/blacklist.sh | 96 ++++++++++++++++++++++ src/lib/common.sh.bottom | 1 - src/lib/common.sh.head | 25 ++++++ src/lib/common.sh.tail | 1 + src/lib/common.sh.top | 25 ------ src/lib/libreblacklist | 141 ++++++++------------------------ src/lib/librelib.7.ronn | 3 +- src/lib/libremessages | 207 ++-------------------------------------------- src/lib/messages.sh | 208 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 399 insertions(+), 352 deletions(-) create mode 100644 src/lib/blacklist.sh delete mode 100644 src/lib/common.sh.bottom create mode 100644 src/lib/common.sh.head create mode 100644 src/lib/common.sh.tail delete mode 100644 src/lib/common.sh.top create mode 100644 src/lib/messages.sh (limited to 'src/lib') diff --git a/src/lib/Makefile b/src/lib/Makefile index 97b30eb..f9ec1e8 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -1,17 +1,17 @@ -copy_files = common.sh.in -libexecs = $(filter-out librelib,$(progs)) -# include common.sh in libs explicitly, because it might not exist yet -# when the wildcard is performed -libs = $(sort $(wildcard *.sh) common.sh conf.sh) +include $(dir $(lastword $(MAKEFILE_LIST)))/../../config.mk +include $(topsrcdir)/automake.head.mk -include ../../common.mk +install-libs += common.sh conf.sh +devtools-files = common.sh.in # Build ############################################################## -common.sh: %: %.in %.top Makefile - @echo "GEN $@" +$(outdir)/conf.sh: $(var)sysconfdir $(var)pkgconfdir + +$(outdir)/common.sh: $(outdir)/%: $(srcdir)/%.in $(srcdir)/%.head $(srcdir)/%.tail $(outdir)/Makefile + @echo "OUT $@" @{ \ - cat '$*.top' && \ + cat '$( '$@' # Translate ########################################################## -LIBREXGETTEXT_FLAGS += --simple=_l:2 - -libreblacklist.pot: libreblacklist librexgettext - { \ +$(outdir)/blacklist.sh.pot: $(srcdir)/blacklist.sh $(srcdir)/librexgettext + @echo "OUT $@" + @{ \ sed -n '/^# Usage:/,/()/{ /^#/ { =; p; } }' $< | \ sed -r -e 's/^# (.*)/msgid "\1"\nmsgstr ""\n/' \ - -e 's/^[0-9]*$$/#. embedded usage text\n#: $<:&/'; \ - ./librexgettext $<; \ - } | $(pofmt) > $@ + -e 's/^[0-9]*$$/#. embedded usage text\n#: $<:&/' && \ + $( $@ +$(outdir)/common.sh.pot : LIBREXGETTEXT_FLAGS += --simple=_l:2 +$(outdir)/conf.sh.pot : LIBREXGETTEXT_FLAGS += --simple=_l:2 +$(outdir)/librelib.pot : LIBREXGETTEXT_FLAGS += --simple=_l:2 +$(outdir)/messages.sh.pot : LIBREXGETTEXT_FLAGS += --simple=_l:2 +$(outdir)/librexgettext.pot: LIBREXGETTEXT_FLAGS += --simple=errusage + +###################################################################### -librexgettext.pot: LIBREXGETTEXT_FLAGS += --simple=errusage +include $(topsrcdir)/automake.tail.mk diff --git a/src/lib/blacklist.sh b/src/lib/blacklist.sh new file mode 100644 index 0000000..0a3cc39 --- /dev/null +++ b/src/lib/blacklist.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# This may be included with or without `set -euE` + +# Copyright (C) 2013-2014, 2016 Luke Shumaker +# +# 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 . + +# make sure XDG_CACHE_HOME is set +. "$(librelib conf)" + +# Usage: blacklist-normalize <$file +# Normalizes the syntax of the blacklist on stdin. +blacklist-normalize() { + sed -e '/^#/d' -e 's/^[^:]*$/&::/' -e 's/^[^:]*:[^:]*$/&:/' +} + +# Usage: blacklist-cat +# Prints the blacklist. +# Uses the cache, but downloads it if it doesn't exist. Also normalizes the blacklist for easier parsing. +blacklist-cat() { + local file="$XDG_CACHE_HOME/libretools/blacklist.txt" + if ! [[ -e $file ]]; then + # exit on failure, whether set -e or not + blacklist-update || return $? + fi + blacklist-normalize < "$file" +} + +# Usage: blacklist-update +# Updates (or creates) the cached copy of the blacklist. +blacklist-update() ( + . libremessages + load_files libretools || return 1 + check_vars libretools BLACKLIST || return 1 + + local remote_blacklist="$BLACKLIST" + local local_blacklist="$XDG_CACHE_HOME/libretools/blacklist.txt" + + _l stat_busy "Downloading blacklist of proprietary software packages" + + mkdir -p "${local_blacklist%/*}" + if wget -N -q -O "${local_blacklist}.part" "$remote_blacklist" 2>/dev/null; then + stat_done + mv -f "${local_blacklist}.part" "$local_blacklist" + else + stat_done + rm "${local_blacklist}.part" + if [[ -e "$local_blacklist" ]]; then + _l warning "Using local copy of blacklist" + else + _l error "Download failed, exiting" + return 1 + fi + + fi +) + +# Usage: blacklist-cat | blacklist-lookup $pkgname +# Filters to obtain the line for $pkgname from the blacklist on stdin. +# Exits successfully whether a line is found or not. +blacklist-lookup() { + local pkg=$1 + # we accept that $pkg contains no regex-nes + blacklist-normalize | grep "^$pkg:" || true +} + +# Usage: blacklist-cat | blacklist-get-pkg +# Prints only the package name field of the blacklist line(s) on stdin. +blacklist-get-pkg() { + blacklist-normalize | cut -d: -f1 +} + +# Usage: blacklist-cat | blacklist-get-rep +# Prints only the replacement package field of the blacklist line(s) on stdin. +blacklist-get-rep() { + blacklist-normalize | cut -d: -f2 +} + +# Usage: blacklist-cat | blacklist-get-reason +# Prints only the reason field of the blacklist line(s) on stdin. +blacklist-get-reason() { + blacklist-normalize | cut -d: -f3- +} diff --git a/src/lib/common.sh.bottom b/src/lib/common.sh.bottom deleted file mode 100644 index e133fad..0000000 --- a/src/lib/common.sh.bottom +++ /dev/null @@ -1 +0,0 @@ -fi diff --git a/src/lib/common.sh.head b/src/lib/common.sh.head new file mode 100644 index 0000000..23bfeb8 --- /dev/null +++ b/src/lib/common.sh.head @@ -0,0 +1,25 @@ +#!/hint/bash +# This may be included with or without `set -euE` + +# This file is included by libremessages. +# You should probably use libremessages instead of this. + +# License: Unspecified + +shopt -s extglob + +if [[ -z ${_INCLUDE_COMMON_SH:-} ]]; then +_INCLUDE_COMMON_SH=true + +[[ -n ${TEXTDOMAIN:-} ]] || export TEXTDOMAIN='libretools' +[[ -n ${TEXTDOMAINDIR:-} ]] || export TEXTDOMAINDIR='/usr/share/locale' + +if type gettext &>/dev/null; then + _() { gettext "$@"; } +else + _() { echo "$@"; } +fi + +_l() { + TEXTDOMAIN='librelib' TEXTDOMAINDIR='/usr/share/locale' "$@" +} diff --git a/src/lib/common.sh.tail b/src/lib/common.sh.tail new file mode 100644 index 0000000..e133fad --- /dev/null +++ b/src/lib/common.sh.tail @@ -0,0 +1 @@ +fi diff --git a/src/lib/common.sh.top b/src/lib/common.sh.top deleted file mode 100644 index 23bfeb8..0000000 --- a/src/lib/common.sh.top +++ /dev/null @@ -1,25 +0,0 @@ -#!/hint/bash -# This may be included with or without `set -euE` - -# This file is included by libremessages. -# You should probably use libremessages instead of this. - -# License: Unspecified - -shopt -s extglob - -if [[ -z ${_INCLUDE_COMMON_SH:-} ]]; then -_INCLUDE_COMMON_SH=true - -[[ -n ${TEXTDOMAIN:-} ]] || export TEXTDOMAIN='libretools' -[[ -n ${TEXTDOMAINDIR:-} ]] || export TEXTDOMAINDIR='/usr/share/locale' - -if type gettext &>/dev/null; then - _() { gettext "$@"; } -else - _() { echo "$@"; } -fi - -_l() { - TEXTDOMAIN='librelib' TEXTDOMAINDIR='/usr/share/locale' "$@" -} diff --git a/src/lib/libreblacklist b/src/lib/libreblacklist index 1e5a467..6c354fe 100755 --- a/src/lib/libreblacklist +++ b/src/lib/libreblacklist @@ -1,8 +1,5 @@ #!/usr/bin/env bash -# This may be included with or without `set -euE` -# When run directly, it does `set -euE` - -# Copyright (C) 2013-2014 Luke Shumaker +# Copyright (C) 2013-2014, 2016 Luke Shumaker # # License: GNU GPLv2+ # @@ -19,88 +16,17 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -# make sure XDG_CACHE_HOME is set -. "$(librelib conf)" - -# Usage: blacklist-normalize <$file -# Normalizes the syntax of the blacklist on stdin. -blacklist-normalize() { - sed -e '/^#/d' -e 's/^[^:]*$/&::/' -e 's/^[^:]*:[^:]*$/&:/' -} - -# Usage: blacklist-cat -# Prints the blacklist. -# Uses the cache, but downloads it if it doesn't exist. Also normalizes the blacklist for easier parsing. -blacklist-cat() { - local file="$XDG_CACHE_HOME/libretools/blacklist.txt" - if ! [[ -e $file ]]; then - # exit on failure, whether set -e or not - blacklist-update || return $? - fi - blacklist-normalize < "$file" -} - -# Usage: blacklist-update -# Updates (or creates) the cached copy of the blacklist. -blacklist-update() ( - . libremessages - load_files libretools || return 1 - check_vars libretools BLACKLIST || return 1 - - local remote_blacklist="$BLACKLIST" - local local_blacklist="$XDG_CACHE_HOME/libretools/blacklist.txt" - - _l stat_busy "Downloading blacklist of proprietary software packages" - - mkdir -p "${local_blacklist%/*}" - if wget -N -q -O "${local_blacklist}.part" "$remote_blacklist" 2>/dev/null; then - stat_done - mv -f "${local_blacklist}.part" "$local_blacklist" - else - stat_done - rm "${local_blacklist}.part" - if [[ -e "$local_blacklist" ]]; then - _l warning "Using local copy of blacklist" - else - _l error "Download failed, exiting" - return 1 - fi - - fi -) - -# Usage: blacklist-cat | blacklist-lookup $pkgname -# Filters to obtain the line for $pkgname from the blacklist on stdin. -# Exits successfully whether a line is found or not. -blacklist-lookup() { - local pkg=$1 - # we accept that $pkg contains no regex-nes - blacklist-normalize | grep "^$pkg:" || true -} - -# Usage: blacklist-cat | blacklist-get-pkg -# Prints only the package name field of the blacklist line(s) on stdin. -blacklist-get-pkg() { - blacklist-normalize | cut -d: -f1 -} - -# Usage: blacklist-cat | blacklist-get-rep -# Prints only the replacement package field of the blacklist line(s) on stdin. -blacklist-get-rep() { - blacklist-normalize | cut -d: -f2 -} +if [[ "${0##*/}" != libreblacklist ]]; then + . "$(librelib blacklist)" +else + set -euE -# Usage: blacklist-cat | blacklist-get-reason -# Prints only the reason field of the blacklist line(s) on stdin. -blacklist-get-reason() { - blacklist-normalize | cut -d: -f3- -} + lib_file="$(librelib blacklist)" + . "$lib_file" -if [[ "${0##*/}" == libreblacklist ]]; then - set -euE usage-outside() { - sed -n '/^# Usage:/,/()/p' "$0" | - tr '\n' '\r' | sed 's/\s*()\s*[{(]/\n/g' + sed -n '/^# Usage:/,/()/p' "$lib_file" | + tr '\n' '\r' | sed 's/\s*()\s*[{(]/\n/g' } # The output format of this is: # - The first line is "Usage:" @@ -110,6 +36,7 @@ if [[ "${0##*/}" == libreblacklist ]]; then usage-inside() { sed 's/\r/\n/g'<<<"$1"|sed -e '/^$/d' -e 's/^# //' } + usage() { export TEXTDOMAIN='librelib' export TEXTDOMAINDIR='/usr/share/locale' @@ -120,33 +47,37 @@ if [[ "${0##*/}" == libreblacklist ]]; then echo print "Commands:" usage-outside | while read -r sec; do sec="$(usage-inside "$sec")" - cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p') - desc="$(_ "$(sed -n 2p <<<"$sec")")" - flag "$cmd" "${desc//blacklist-/${0##*/} }" + cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p') + desc="$(_ "$(sed -n 2p <<<"$sec")")" + flag "$cmd" "${desc//blacklist-/${0##*/} }" done else usage-outside | while read -r sec; do sec="$(usage-inside "$sec")" - cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p') - if [[ "$cmd" == "$1" ]]; then - <<<"$sec" sed '$d' | - while read -r line; do print "$line"; done | - sed "s/blacklist-/${0##*/} /g" | - fmt -us - return 0 - fi + cmd=$(<<<"$sec" sed -n '$s/^blacklist-//p') + if [[ "$cmd" == "$1" ]]; then + <<<"$sec" sed '$d' | + while read -r line; do print "$line"; done | + sed "s/blacklist-/${0##*/} /g" | + fmt -us + return 0 + fi done fi } - if [[ $# -eq 0 ]]; then - usage >&2 - exit 1 - fi - _blacklist_cmd=$1 - shift - if [[ $_blacklist_cmd == -h ]]; then - usage "$@" - else - "blacklist-$_blacklist_cmd" "$@" - fi + main() { + if [[ $# -eq 0 ]]; then + usage >&2 + exit 1 + fi + _blacklist_cmd=$1 + shift + if [[ $_blacklist_cmd == -h ]]; then + usage "$@" + else + "blacklist-$_blacklist_cmd" "$@" + fi + } + + main "$@" fi diff --git a/src/lib/librelib.7.ronn b/src/lib/librelib.7.ronn index 31fb65f..33b0c55 100644 --- a/src/lib/librelib.7.ronn +++ b/src/lib/librelib.7.ronn @@ -11,8 +11,7 @@ There are three parts to librelib: 1. The `librelib`(1) executable. 2. The non-executable libraries installed in `/usr/lib/libretools` - 3. The executable libraries installed in both `/usr/bin` and - `/usr/lib/libretools`. + 3. The executable libraries installed in `/usr/bin` The `librelib` executable isn't very exciting, it just finds the libraries installed in `/usr/lib/libretools`. Think of it as a sort diff --git a/src/lib/libremessages b/src/lib/libremessages index 528d9b6..647204a 100755 --- a/src/lib/libremessages +++ b/src/lib/libremessages @@ -1,19 +1,5 @@ #!/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 +# Copyright (C) 2012-2014, 2016 Luke Shumaker # # License: GNU GPLv2+ # @@ -30,189 +16,10 @@ # 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: 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. If you pass in multiple flag/description pairs at once, -# the descriptions are all alligned together. -flag() { - [[ $# == $(($#/2*2)) ]] || panic - local args=("$@") - - declare -i flaglen=0 - while [[ $# -gt 0 ]]; do - if [[ $1 == *: ]]; then - shift 1 - else - if [[ ${#1} -gt $flaglen ]]; then - flaglen=${#1} - fi - shift 2 - fi - done - set -- "${args[@]}" - - # Unless the $flaglen is extra-wide, the $desc should start at - # column 16 (that is two literal-tabs). If $flaglen 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 4 spaces built into it (two at - # the beginning, and two for a seperator). Therefore, the - # default indent is 16-4=12 columns. And the width left for - # $desc is (75-4)-indent = 71-indent. - - declare -i indent=12 - while [[ $indent -lt $flaglen ]]; do - indent+=8 - done - local fmt2 fmt1 - fmt2=" %-${indent}s %s\n" - printf -v fmt1 " %-${indent}s %%s\n" '' - - while [[ $# -gt 0 ]]; do - if [[ $1 == *: ]]; then - printf -- ' %s\n' "$(_ "$1")" - shift - else - local flag=$1 - local desc="$(_ "$(whitespace_collapse <<<"$2")")" - shift 2 - - local lines - IFS=$'\n' lines=($(fmt -u -w $((71-indent)) <<<"$desc")) - printf -- "$fmt2" "$flag" "${lines[0]}" - [[ ${#lines[@]} -lt 2 ]] || printf -- "$fmt1" "${lines[@]:1}" - fi - 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 - "$@" +if [[ "${0##*/}" != libremessages ]]; then + . "$(librelib messages)" +else + set -euE + . "$(librelib messages)" + "$@" fi diff --git a/src/lib/messages.sh b/src/lib/messages.sh new file mode 100644 index 0000000..4b4897e --- /dev/null +++ b/src/lib/messages.sh @@ -0,0 +1,208 @@ +#!/usr/bin/env bash +# This may be included with or without `set -euE` + +# Copyright (C) 2011 Joshua Ismael Haase Hernández (xihh) +# Copyright (C) 2012 Nicolás Reynolds +# Copyright (C) 2012-2014, 2016 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: 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. If you pass in multiple flag/description pairs at once, +# the descriptions are all alligned together. +flag() { + [[ $# == $(($#/2*2)) ]] || panic + local args=("$@") + + declare -i flaglen=0 + while [[ $# -gt 0 ]]; do + if [[ $1 == *: ]]; then + shift 1 + else + if [[ ${#1} -gt $flaglen ]]; then + flaglen=${#1} + fi + shift 2 + fi + done + set -- "${args[@]}" + + # Unless the $flaglen is extra-wide, the $desc should start at + # column 16 (that is two literal-tabs). If $flaglen 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 4 spaces built into it (two at + # the beginning, and two for a seperator). Therefore, the + # default indent is 16-4=12 columns. And the width left for + # $desc is (75-4)-indent = 71-indent. + + declare -i indent=12 + while [[ $indent -lt $flaglen ]]; do + indent+=8 + done + local fmt2 fmt1 + fmt2=" %-${indent}s %s\n" + printf -v fmt1 " %-${indent}s %%s\n" '' + + while [[ $# -gt 0 ]]; do + if [[ $1 == *: ]]; then + printf -- ' %s\n' "$(_ "$1")" + shift + else + local flag=$1 + local desc="$(_ "$(whitespace_collapse <<<"$2")")" + shift 2 + + local lines + IFS=$'\n' lines=($(fmt -u -w $((71-indent)) <<<"$desc")) + printf -- "$fmt2" "$flag" "${lines[0]}" + [[ ${#lines[@]} -lt 2 ]] || printf -- "$fmt1" "${lines[@]:1}" + fi + 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 +} -- cgit v1.2.3-54-g00ecf