summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Makefile44
-rw-r--r--src/lib/blacklist.sh96
-rw-r--r--src/lib/common.sh.head (renamed from src/lib/common.sh.top)0
-rw-r--r--src/lib/common.sh.tail (renamed from src/lib/common.sh.bottom)0
-rwxr-xr-xsrc/lib/libreblacklist141
-rw-r--r--src/lib/librelib.7.ronn3
-rwxr-xr-xsrc/lib/libremessages207
-rw-r--r--src/lib/messages.sh208
8 files changed, 373 insertions, 326 deletions
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 '$(<D)/$*.head' && \
echo && \
sed -r \
-e '/encoding problem/d;/LANG=/d' \
@@ -19,21 +19,27 @@ common.sh: %: %.in %.top Makefile
-e 's/gettext /_l _ /g' \
-e "s/^(\s+)(msg|error) '/\1_l \2 '/" \
-e 's|lock\(\)\s*\{|lock()\n{|' \
- '$*.in' && \
+ '$(<D)/$*.in' && \
echo && \
- cat '$*.bottom' && \
+ cat '$(<D)/$*.tail' && \
:; } > '$@'
# 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#: $<:&/' && \
+ $(<D)/librexgettext --simple=_l:2 $< && \
+ :; } | $(pofmt) > $@
+$(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 <lukeshu@sbcglobal.net>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+# 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.top b/src/lib/common.sh.head
index 23bfeb8..23bfeb8 100644
--- a/src/lib/common.sh.top
+++ b/src/lib/common.sh.head
diff --git a/src/lib/common.sh.bottom b/src/lib/common.sh.tail
index e133fad..e133fad 100644
--- a/src/lib/common.sh.bottom
+++ b/src/lib/common.sh.tail
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 <lukeshu@sbcglobal.net>
+# Copyright (C) 2013-2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# 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 <http://www.gnu.org/licenses/>.
-# 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) <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>
+# Copyright (C) 2012-2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# 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 <http://www.gnu.org/licenses/>.
-################################################################################
-# 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 <<<STRING
-#
-# Collapses whitespace on stadard I/O, similar to HTML whitespace
-# collapsing, with the exception that it puts two spaces between
-# sentences. It considers newline, tab, and space to be whitespace.
-whitespace_collapse() {
- [[ $# == 0 ]] || panic
-
- tr '\n' '\r' | sed -r \
- -e 's/\r/ /g' -e 's/\t/ /g' \
- -e 's/(^|[^.!? ]) +/\1 /g' -e 's/([.!?]) +/\1 /g'
-}
-
-
-# Usage: prose MESG [ARGS...]
-#
-# Do HTML-style whitespace collapsing on the first argument, translate
-# it (gettext), then word-wrap it to 75 columns. This is useful for
-# printing a paragraph of prose in --help text.
-prose() {
- [[ $# -ge 1 ]] || panic
- local mesg="$(_ "$(whitespace_collapse <<<"$1")")"; shift
- printf -- "$mesg" "$@" | fmt -u
-}
-
-# Usage: bullet MESG [ARGS...]
-# Like prose, but print a bullet "-" before the first line, and indent the
-# remaining lines.
-bullet() {
- [[ $# -ge 1 ]] || panic
- local mesg="$(_ "$(whitespace_collapse <<<"$1")")"; shift
- # Wrap the text to 71 columns; 75 (the default) minus a 4 column indent
- printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/ - /' -e '2,$s/^/ /'
-}
-
-# Usage: flag FLAG DESCRIPTION [FLAG2 DESCRIPTION2...]
-#
-# Print a flag and description formatted for --help text.
-#
-# ex: flag '-C <FILE>' '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) <hahj87@gmail.com>
+# Copyright (C) 2012 Nicolás Reynolds <fauno@parabola.nu>
+# Copyright (C) 2012-2014, 2016 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>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+################################################################################
+# 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 <<<STRING
+#
+# Collapses whitespace on stadard I/O, similar to HTML whitespace
+# collapsing, with the exception that it puts two spaces between
+# sentences. It considers newline, tab, and space to be whitespace.
+whitespace_collapse() {
+ [[ $# == 0 ]] || panic
+
+ tr '\n' '\r' | sed -r \
+ -e 's/\r/ /g' -e 's/\t/ /g' \
+ -e 's/(^|[^.!? ]) +/\1 /g' -e 's/([.!?]) +/\1 /g'
+}
+
+
+# Usage: prose MESG [ARGS...]
+#
+# Do HTML-style whitespace collapsing on the first argument, translate
+# it (gettext), then word-wrap it to 75 columns. This is useful for
+# printing a paragraph of prose in --help text.
+prose() {
+ [[ $# -ge 1 ]] || panic
+ local mesg="$(_ "$(whitespace_collapse <<<"$1")")"; shift
+ printf -- "$mesg" "$@" | fmt -u
+}
+
+# Usage: bullet MESG [ARGS...]
+# Like prose, but print a bullet "-" before the first line, and indent the
+# remaining lines.
+bullet() {
+ [[ $# -ge 1 ]] || panic
+ local mesg="$(_ "$(whitespace_collapse <<<"$1")")"; shift
+ # Wrap the text to 71 columns; 75 (the default) minus a 4 column indent
+ printf -- "$mesg" "$@" | fmt -u -w 71 | sed -e '1s/^/ - /' -e '2,$s/^/ /'
+}
+
+# Usage: flag FLAG DESCRIPTION [FLAG2 DESCRIPTION2...]
+#
+# Print a flag and description formatted for --help text.
+#
+# ex: flag '-C <FILE>' '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
+}