diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/.gitignore | 3 | ||||
-rw-r--r-- | src/lib/Makefile | 30 | ||||
-rw-r--r-- | src/lib/common.sh.bottom | 1 | ||||
-rw-r--r-- | src/lib/common.sh.top | 26 | ||||
-rw-r--r-- | src/lib/conf.sh | 190 | ||||
-rwxr-xr-x | src/lib/libreblacklist | 148 | ||||
-rwxr-xr-x | src/lib/librelib | 79 | ||||
-rwxr-xr-x | src/lib/libremessages | 131 |
8 files changed, 608 insertions, 0 deletions
diff --git a/src/lib/.gitignore b/src/lib/.gitignore new file mode 100644 index 0000000..9a0c402 --- /dev/null +++ b/src/lib/.gitignore @@ -0,0 +1,3 @@ +common.sh +common.sh.in +common.sh.top diff --git a/src/lib/Makefile b/src/lib/Makefile new file mode 100644 index 0000000..45fd330 --- /dev/null +++ b/src/lib/Makefile @@ -0,0 +1,30 @@ +copy_files = common.sh.in +libexecs = $(filter-out librelib,$(wildcard libre*)) +# include common.sh in libs explicitly, because it might not exist yet +# when the wildcard is performed +libs = $(sort $(wildcard *.sh) common.sh) +include ../../common.mk + +# Build ############################################################## + +common.sh: %: %.in %.top Makefile + @echo "GEN $@" + @{ \ + cat '$*.top' && \ + echo && \ + sed -r -e '/encoding problem/d;/LANG=/d' -e 's/mesg=\$$(.)/mesg="$$(_ "$$\1")"/' '$*.in' && \ + echo && \ + cat '$*.bottom' && \ + :; } > '$@' + +# Translate ########################################################## + +pot: libreblacklist.pot common.sh.pot librelib.pot + +libreblacklist.pot: libreblacklist + { \ + sed -n '/^# Usage:/,/()/{ /^#/ { =; p; } }' $< | sed -r -e 's/^# (.*)/msgid "\1"/' -e 's/^[0-9]*$$/#. embedded usage text\n#: $<:&/'; \ + sed -rn '/print /{ =; s/\s*print "([^"]*)".*/msgid "\1"/p; }' $< | sed 's/^[0-9]*$$/#. print\n#: $<:&/' ; \ + } | sed 's/^msgid .*/&\nmsgstr ""\n/' > $@ +common.sh.pot: common.sh + xgettext --omit-header -i --from-code=UTF-8 -L shell -o $@ $< diff --git a/src/lib/common.sh.bottom b/src/lib/common.sh.bottom new file mode 100644 index 0000000..e133fad --- /dev/null +++ b/src/lib/common.sh.bottom @@ -0,0 +1 @@ +fi diff --git a/src/lib/common.sh.top b/src/lib/common.sh.top new file mode 100644 index 0000000..054301b --- /dev/null +++ b/src/lib/common.sh.top @@ -0,0 +1,26 @@ +#!/bin/bash # non-executable, but put this there as a hint to text editors +# This may be included with or without `set -euE` + +# This file is included by libremessages. +# You should probably use libremessages instead of this. + +# 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; version 2 of the License. +# +# 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. + +if [[ -z ${_INCLUDE_COMMON_SH:-} ]]; then +_INCLUDE_COMMON_SH=true + +export TEXTDOMAIN='libretools' +export TEXTDOMAINDIR='/usr/share/locale' + +if type gettext &>/dev/null; then + _() { gettext "$@"; } +else + _() { echo "$@"; } +fi diff --git a/src/lib/conf.sh b/src/lib/conf.sh new file mode 100644 index 0000000..f96af26 --- /dev/null +++ b/src/lib/conf.sh @@ -0,0 +1,190 @@ +#!/bin/bash # non-executable, but put this there as a hint to text editors +# This may be included with or without `set -euE` + +# Copyright (c) 2012-2013 by Luke Shumaker <lukeshu@sbcglobal.net> +# +# 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. +# +# 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/>. + +LIBREUSER="${SUDO_USER:-$USER}" +if [[ $LIBREUSER == $USER ]]; then + LIBREHOME=$HOME +else + LIBREHOME="$(eval echo ~$LIBREUSER)" +fi +if [[ -z ${XDG_CONFIG_HOME:-} ]]; then + export XDG_CONFIG_HOME="${LIBREHOME}/.config" +fi +if [[ -z ${XDG_CACHE_HOME:-} ]]; then + export XDG_CACHE_HOME="${LIBREHOME}/.cache" +fi + +# Generic functions ############################################################ + +# Usage: list_files $slug +# Lists the configuration files to be considered for $slug. +# Later files should take precedence over earlier files. +list_files() { + local slug=$1 + case $slug in + abs) + echo /etc/$slug.conf + echo "$LIBREHOME/.$slug.conf" + ;; + makepkg) + if [[ ${MAKEPKG_CONF:-} != /etc/$slug.conf && -r ${MAKEPKG_CONF:-} ]]; then + echo "$MAKEPKG_CONF" + else + echo /etc/$slug.conf + echo "$LIBREHOME/.$slug.conf" + fi + ;; + libretools) + echo /etc/$slug.conf + echo "$XDG_CONFIG_HOME/libretools/$slug.conf" + ;; + *) + echo /etc/libretools.d/$slug.conf + echo "$XDG_CONFIG_HOME/libretools/$slug.conf" + ;; + esac +} + +# Usage: list_envvars $slug +# Lists the environmental variables that take precidence over the configuration +# files for $slug. +list_envvars() { + local slug=$1 + case $slug in + makepkg) + printf '%s\n' \ + PKGDEST SRCDEST SRCPKGDEST LOGDEST \ + BUILDDIR \ + PKGEXT SRCEXT \ + GPGKEY PACKAGER + ;; + *) :;; + esac +} + +# Usage: load_files $slug +# Loads the configuration files for $slug in the proper order. +load_files() { + local slug=$1 + local var + local file + + # Save the existing versions at _VARNAME + for var in $(list_envvars $slug); do + [[ -n ${!var:-} ]] && eval "_$var=\${$var}" + done + + # Load the files + for file in $(list_files $slug); do + if [[ -r $file ]]; then + . "$file" || return 1 + fi + done + + # Restore the _SAVED versions + for var in $(list_envvars $slug); do + eval "$var=\${_$var:-\${$var:-}}" + done +} + +# Usage: check_vars $slug VAR1 VAR2... +# Check whether the variables listed are properly set. +# If not, it prints a message saying to set them in the configuration file(s) +# for $slug. +check_vars() { + local slug=$1; shift + + local ret=0 + + local VAR + for VAR in "$@"; do + if [[ -z ${!VAR:-} ]]; then + type print &>/dev/null || . libremessages + if [[ $(list_files $slug|wc -l) -gt 1 ]]; then + print "Configure '%s' in one of:" "$VAR" + list_files $slug | sed 's/./ -> &/' + else + print "Configure '%s' in '%s'" "$VAR" "$(list_files $slug)" + fi + ret=1 + fi + done >&2 + + if [[ $ret != 0 ]]; then + return 1 + fi +} + +# makepkg configuration ######################################################## + +# Usage: get_conf_makepkg <var_name> <default_value> +get_conf_makepkg() ( + set +euE + local setting=$1 + local default=$2 + load_files makepkg + printf '%s\n' "${!setting:-${default}}" +) + +set_conf_makepkg() { + local key=$1 + local val=$2 + local file + for file in $(list_files makepkg|tac); do + if [[ -w $file ]]; then + sed -i "/^\s*$key=/d" "$file" + printf '%s=%q\n' "$key" "$val" >> "$file" + return 0 + fi + done + return 1 +} + +# PKGBUILD (not configuration, per se) ######################################### + +unset_PKGBUILD() { + # This routine is based primarily off of the PKGBUILD(5) man-page, + # version 4.1.2, dated 2013-06-18. + + # From the "OPTIONS AND DIRECTIVES" section (in order of mention) + unset -v pkgname pkgver + unset -f pkgver + unset -v pkgrel pkgdesc epoch url license install changelog source + unset -v noextract md5sums sha{1,256,384,512}sums groups arch backup + unset -v depends makedepends checkdepends optdepends conflicts provides + unset -v replaces options + + # From the "PACKAGING FUNCTIONS" section (in order of mention) + unset -f package prepare build check + + # From the "PACKAGE SPLITTING" section + unset -f $(declare -f|sed -n 's/^\(package_\S*\) ()\s*$/\1/p') + unset -v pkgbase + + # These are used by the `librefetch` program + unset -v mksource mknoextract mkmd5sums mksha{1,256,384,512}sums + unset -v mkdepends + unset -f mksource +} + +load_PKGBUILD() { + local file=${1:-./PKGBUILD} + unset_PKGBUILD + CARCH="$(get_conf_makepkg CARCH "`uname -m`")" + . "$file" +} diff --git a/src/lib/libreblacklist b/src/lib/libreblacklist new file mode 100755 index 0000000..5db1a3f --- /dev/null +++ b/src/lib/libreblacklist @@ -0,0 +1,148 @@ +#!/usr/bin/env bash +# This may be included with or without `set -euE` +# When run directly, it does `set -euE` + +# Copyright (c) 2013 by Luke Shumaker <lukeshu@sbcglobal.net> +# +# 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. +# +# 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 '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" + + 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 + warning "Using local copy of blacklist" + else + 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- +} + +if [[ "${0##*/}" == libreblacklist ]]; then + set -euE + usage-outside() { + sed -n '/^# Usage:/,/()/p' "$0" | + tr '\n' '\r' | sed 's/\s*()\s*[{(]/\n/g' + } + # The output format of this is: + # - The first line is "Usage:" + # - The second line is a brief description + # - The last line is the command name (prefixed with "blacklist-") + # - The in-between lines are the extended description. + usage-inside() { + sed 's/\r/\n/g'<<<"$1"|sed -e '/^$/d' -e 's/^# //' + } + usage() { + . $(librelib messages) + if [[ $# -eq 0 ]]; then + print "Usage: %s [-h] COMMAND [ARGUMENTS]" "${0##*/}" + print "Tool for working with the nonfree software blacklist" + 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##*/} }" + 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 + done + fi + } + + if [[ $# -eq 0 ]]; then + usage >/dev/stderr + exit 1 + fi + _blacklist_cmd=$1 + shift + if [[ $_blacklist_cmd == -h ]]; then + usage "$@" + else + "blacklist-$_blacklist_cmd" "$@" + fi +fi diff --git a/src/lib/librelib b/src/lib/librelib new file mode 100755 index 0000000..2dc9314 --- /dev/null +++ b/src/lib/librelib @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# Copyright (c) 2013 by Luke Shumaker <lukeshu@sbcglobal.net> +# +# 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. +# +# 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/>. + +default_libdir=/usr/lib/libretools + +if ! type gettext &>/dev/null; then + gettext() { echo "$@"; } +fi + +print() { + mesg=$1 + shift + printf -- "$(gettext "$mesg")\n" "$@" +} + +prose() { + print "$@" | fmt -su +} + +cmd=${0##*/} +usage() { + . libremessages + print 'Usage: . $(%s LIBRARY)' "$cmd" + print "Finds a shell library file" + echo + prose "While some libraries can be sourced just by their name because + they are installed in PATH (like libremessages), some are not + installed there (like conf.sh), so a path must be given. + Hardcoding that path is the way of the dark side." + echo + prose 'By default, it looks for the files in `%s`, but this can be + changed with the environmental variable LIBRETOOLS_LIBDIR.' "$default_libdir" + echo + print "Example usage:" + printf ' . $(%s conf.sh)\n' "$cmd" +} + +main() { + if [[ $# != 1 ]]; then + usage >&2 + return 2 + fi + if [[ $1 == '-h' ]]; then + usage + return 0; + fi + + if [[ -z $LIBRETOOLS_LIBDIR ]]; then + export LIBRETOOLS_LIBDIR=$default_libdir + fi + + lib=$1 + lib=${lib#libre} + lib=${lib%.sh} + + for file in ${lib} libre${lib} ${lib}.sh libre${lib}.sh; do + if [[ -f "$LIBRETOOLS_LIBDIR/$file" ]]; then + printf '%s\n' "$LIBRETOOLS_LIBDIR/$file" + return 0; + fi + done + print '%s: could not find library: %s' "$cmd" "$lib" >> /dev/stderr + return 1 +} + +main "$@" diff --git a/src/lib/libremessages b/src/lib/libremessages new file mode 100755 index 0000000..c6d08e2 --- /dev/null +++ b/src/lib/libremessages @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +# 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> +# +# 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. +# +# 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 # +################################################################################ + +panic() { + echo "$(_ 'panic: malformed call to internal function')" >&2 + exit 1 +} + +# Usage: print MESG ARG1 ARG2... +# Like printf, but gettext-aware, and prints a trailing newline +print() { + [[ $# -ge 1 ]] || panic + local mesg="$(_ "$1")" + shift + printf -- "$mesg\n" "$@" +} + +# Do HTML-style whitespace collapsing on standard IO. It considers newline, +# tab, and space to be whitespace. +_html_whitespace_collapse() { + [[ $# == 0 ]] || panic + tr '\n' ' ' | sed -r -e 's/\t/ /g' -e 's/ +/ /g' +} + + +# Usage: prose MESG +# 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="$(_ "$(_html_whitespace_collapse <<<"$1")")"; shift + printf -- "$mesg" "$@" | fmt -u +} + +# Usage: bullet MESG +# Like prose, but print a bullet "-" before the first line, and indent the +# remaining lines. +bullet() { + [[ $# -ge 1 ]] || panic + local mesg="$(_ "$(_html_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 +# 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. +flag() { + [[ $# == 2 ]] || panic + local n=' +' + local flag=$1 + local desc="$(_ "$(_html_whitespace_collapse <<<"$2")")" + + declare -i indent=13 + while [[ $indent -le ${#flag} ]]; do + indent=$((indent+8)) + done + + local lines + IFS=$n lines=($(fmt -u -w $((73-indent)) <<<"$desc")) + local line + for line in "${lines[@]}"; do + printf " %-${indent}s %s\n" "$flag" "$line" + flag='' + done +} + +# Usage: term_title This will be the term title +# 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" "$*" +} + +################################################################################ +# Run one of the defined functions if invoked directly # +################################################################################ + +if [[ "${0##*/}" == libremessages ]]; then + set -euE + _libremessages_cmd=$1 + shift + "$_libremessages_cmd" "$@" +fi |