From cca66b9ec4e494c1d919bff0f71a820d8afab1fa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:24:48 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- packaging/macos/jhb/usr/src/bash_d/README.md | 9 ++ packaging/macos/jhb/usr/src/bash_d/ansi.sh | 76 +++++++++++ packaging/macos/jhb/usr/src/bash_d/assert.sh | 42 ++++++ packaging/macos/jhb/usr/src/bash_d/bash_d.sh | 98 +++++++++++++ packaging/macos/jhb/usr/src/bash_d/echo.sh | 52 +++++++ packaging/macos/jhb/usr/src/bash_d/error.sh | 45 ++++++ packaging/macos/jhb/usr/src/bash_d/lib.sh | 174 ++++++++++++++++++++++++ packaging/macos/jhb/usr/src/bash_d/readlinkf.sh | 27 ++++ 8 files changed, 523 insertions(+) create mode 100644 packaging/macos/jhb/usr/src/bash_d/README.md create mode 100644 packaging/macos/jhb/usr/src/bash_d/ansi.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/assert.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/bash_d.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/echo.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/error.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/lib.sh create mode 100644 packaging/macos/jhb/usr/src/bash_d/readlinkf.sh (limited to 'packaging/macos/jhb/usr/src') diff --git a/packaging/macos/jhb/usr/src/bash_d/README.md b/packaging/macos/jhb/usr/src/bash_d/README.md new file mode 100644 index 0000000..fc97cc4 --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/README.md @@ -0,0 +1,9 @@ +# `bash_d` + +Conceived as part of my (unpublished) dotfiles on macOS, this collection of functions outgrew its originally limited scope and I began developing this into the spiritual successor of [bashlib](https://github.com/dehesselle/bashlib). + +This is far from finished and comes with no support and without documentation at this point, but I need to put this online now so I can start using this in other projects. + +## License + +[GPL](LICENSE) \ No newline at end of file diff --git a/packaging/macos/jhb/usr/src/bash_d/ansi.sh b/packaging/macos/jhb/usr/src/bash_d/ansi.sh new file mode 100644 index 0000000..5ecc5ff --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/ansi.sh @@ -0,0 +1,76 @@ +# SPDX-FileCopyrightText: 2021 René de Hesselle +# +# SPDX-License-Identifier: GPL-2.0-or-later + +### description ################################################################ + +# This file contains ANSI control codes to control terminal output. + +### shellcheck ################################################################# + +# shellcheck shell=bash # no shebang as this file is intended to be sourced +# shellcheck disable=SC2034 # there are a lot of unexported vars here + +### includes ################################################################### + +# Nothing here. + +### variables ################################################################## + +ANSI_ENABLED=${ANSI_ENABLED:-true} # allow ANSI escape sequences... +ANSI_TERM_ONLY=${ANSI_TERM_ONLY:-true} # ...but only when running in a terminal + +ANSI_CURSOR_LEFT="\033[1D" + +# 0: normal +# 1: bold +# 2: faint +# | +# \033[1;32m +# | +# color +ANSI_FG_BLACK="\033[0;30m" +ANSI_FG_BLACK_BOLD="\033[1;30m" +ANSI_FG_BLACK_BRIGHT="\033[0;90m" +ANSI_FG_BLUE="\033[0;34m" +ANSI_FG_BLUE_BOLD="\033[1;34m" +ANSI_FG_BLUE_BRIGHT="\033[0;94m" +ANSI_FG_CYAN="\033[0;36m" +ANSI_FG_CYAN_BOLD="\033[1;36m" +ANSI_FG_CYAN_BRIGHT="\033[0;96m" +ANSI_FG_GREEN="\033[0;32m" +ANSI_FG_GREEN_BOLD="\033[1;32m" +ANSI_FG_GREEN_BRIGHT="\033[0;92m" +ANSI_FG_MAGENTA="\033[0;35m" +ANSI_FG_MAGENTA_BOLD="\033[1;35m" +ANSI_FG_MAGENTA_BRIGHT="\033[0;95m" +ANSI_FG_RED="\033[0;31m" +ANSI_FG_RED_BOLD="\033[1;31m" +ANSI_FG_RED_BRIGHT="\033[0;91m" +ANSI_FG_RESET="\033[0;0m" +ANSI_FG_WHITE="\033[0;37m" +ANSI_FG_WHITE_BOLD="\033[1;37m" +ANSI_FG_WHITE_BRIGHT="\033[0;97m" +ANSI_FG_YELLOW="\033[0;33m" +ANSI_FG_YELLOW_BOLD="\033[1;33m" +ANSI_FG_YELLOW_BRIGHT="\033[0;93m" + +### functions ################################################################## + +function ansi_test_colors +{ + for color in ${!ANSI_FG_*}; do + echo -e "${!color}This is $color" + done +} + +### aliases #################################################################### + +# This performs the following check: +# - usage of ANSI is generally enabled AND +# - we are either running in a terminal OR we don't care at all +alias ansi_is_usable='$ANSI_ENABLED && ([ -t 1 ] || ! $ANSI_TERM_ONLY)' + +### main ####################################################################### + +# Nothing here. diff --git a/packaging/macos/jhb/usr/src/bash_d/assert.sh b/packaging/macos/jhb/usr/src/bash_d/assert.sh new file mode 100644 index 0000000..6b65138 --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/assert.sh @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: 2021 René de Hesselle +# +# SPDX-License-Identifier: GPL-2.0-or-later + +### description ################################################################ + +# This file provides a mechanism to restrict sourcing a script to specific +# platforms. + +### shellcheck ################################################################# + +# shellcheck shell=bash # no shebang as this file is intended to be sourced + +### dependencies ############################################################### + +bash_d_include echo + +### variables ################################################################## + +ASSERT_RC=0 + +### functions ################################################################## + +# Nothing here. + +### aliases #################################################################### + +# Bash version +alias assert_bash4_or_above='[ ${BASH_VERSINFO[0]} -lt 4 ] && echo_e "$(basename ${BASH_SOURCE[0]}) will be unavailable (depends on bash >= 4)" && return $LINENO || true' + +# Git +alias assert_git='[ ! -x "$(command -v git)" ] && echo_e "$(basename ${BASH_SOURCE[0]}) will be unavailable (depends on git)" && return $LINENO || true' + +# Unix platforms +alias assert_darwin='[ "$(uname)" != "Darwin" ] && echo_e "Darwin required" && return $LINENO || true' +alias assert_linux='[ "$(uname)" != "Linux" ] && echo_e "Linux required" && return $LINENO || true' + +alias assert_ok='ASSERT_RC=$?; [ $ASSERT_RC -ne 0 ] && echo_e "assert_ok rc=$ASSERT_RC" && return $LINENO' + +### main ####################################################################### + +# Nothing here. diff --git a/packaging/macos/jhb/usr/src/bash_d/bash_d.sh b/packaging/macos/jhb/usr/src/bash_d/bash_d.sh new file mode 100644 index 0000000..e0715b4 --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/bash_d.sh @@ -0,0 +1,98 @@ +# SPDX-FileCopyrightText: 2021 René de Hesselle +# +# SPDX-License-Identifier: GPL-2.0-or-later + +### description ################################################################ + +# Provide an include guard to be used in every script to protect them from +# being sourced multiple times. + +### shellcheck ################################################################# + +# shellcheck shell=bash # no shebang as this file is intended to be sourced + +### dependencies ############################################################### + +# All of these need to be "or true" because they fail on the first run. + +bash_d_include echo 2>/dev/null || true +bash_d_include assert 2>/dev/null || true + +### variables ################################################################## + +BASH_D_DIR=${BASH_D_DIR:-$(dirname "${BASH_SOURCE[0]}")} + +if [ "$BASH_D_FAIL_ON_INCLUDE_ERROR" != "false" ]; then + BASH_D_FAIL_ON_INCLUDE_ERROR=true +fi + +### functions ################################################################## + +function bash_d_include_all +{ + local includes + includes=$(mktemp "$TMP/${FUNCNAME[0]}".XXXXXX) + + echo "BASH_D_FAIL_ON_INCLUDE_ERROR=false" >> "$includes" + for file in "$BASH_D_DIR"/*.sh; do + echo "bash_d_include $(basename "$file")" >> "$includes" + done + echo "BASH_D_FAIL_ON_INCLUDE_ERROR=true" >> "$includes" + + # shellcheck disable=SC1090 # dynamic sourcing on purpose + source "$includes" + rm "$includes" +} + +function bash_d_is_included +{ + local file=$1 + + #shellcheck disable=SC2076 # we want literal match, not regex + if [[ " ${BASH_D_INCLUDE_FILES[*]} " =~ \ + " $BASH_D_DIR/$(basename -s .sh "$file").sh " ]]; then + return 0 + else + return 1 + fi +} + +### aliases #################################################################### + +# shellcheck disable=SC2142 # too much trickery for shellcheck to digest +alias bash_d_include=\ +'declare -a BASH_D_INCLUDE_FILES; '\ +'BASH_D_INCLUDE_FILE=$(sed -n ${LINENO}p ${BASH_SOURCE[0]} | awk '"'"'{ print $2 }'"'"'); '\ +'BASH_D_INCLUDE_FILE=$BASH_D_DIR/$(basename -s .sh $BASH_D_INCLUDE_FILE).sh; '\ +'if [[ " ${BASH_D_INCLUDE_FILES[@]} " =~ " ${BASH_D_INCLUDE_FILE} " ]]; then '\ +' : ; '\ +'else '\ +' if [ "$BASH_D_INCLUDE_FILE" = "$BASH_D_DIR/bash_d.sh" ] && '\ +' [ ${#BASH_D_INCLUDE_FILES[@]} -gt 0 ] || '\ +' source $BASH_D_INCLUDE_FILE; then '\ +' BASH_D_INCLUDE_FILE=$(sed -n ${LINENO}p ${BASH_SOURCE[0]} | awk '"'"'{ print $2 }'"'"'); '\ +' BASH_D_INCLUDE_FILE=$BASH_D_DIR/$(basename -s .sh $BASH_D_INCLUDE_FILE).sh; '\ +' BASH_D_INCLUDE_FILES+=("$BASH_D_INCLUDE_FILE"); '\ +' else '\ +' if alias | grep "echo_e" >/dev/null; then '\ +' echo_e "$BASH_D_INCLUDE_FILE will be unavailable"; '\ +' else '\ +' >&2 echo "error: $BASH_D_INCLUDE_FILE will be unavailable"; '\ +' fi; '\ +' if [ "$BASH_D_INCLUDE_FILE" != "$BASH_D_DIR/bash_d.sh" ] && $BASH_D_FAIL_ON_INCLUDE_ERROR; then '\ +' exit 1; '\ +' fi '\ +' fi '\ +'fi '\ +'# ' + +### main ####################################################################### + +if [ ! -d "$BASH_D_DIR" ]; then + echo "error: BASH_D_DIR=$BASH_D_DIR is invalid [${BASH_SOURCE[0]}]" + exit 1 +fi + +shopt -s expand_aliases + +bash_d_include bash_d # Need to rerun so complete setting us up. diff --git a/packaging/macos/jhb/usr/src/bash_d/echo.sh b/packaging/macos/jhb/usr/src/bash_d/echo.sh new file mode 100644 index 0000000..d160db0 --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/echo.sh @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: 2021 René de Hesselle +# +# SPDX-License-Identifier: GPL-2.0-or-later + +### description ################################################################ + +# Provide colorful convenience functions for echo. + +### shellcheck ################################################################# + +# shellcheck shell=bash # no shebang as this file is intended to be sourced + +### includes ################################################################### + +bash_d_include ansi + +### variables ################################################################## + +# Nothing here. + +### functions ################################################################## + +function echo_message__ +{ + local funcname=$1 # empty if outside function + local filename=$2 + local type=$3 + local color=$4 + local args=${*:5} + + if [ -z "$funcname" ] || [ "$funcname" = "source" ]; then + funcname=$(basename "$filename") + fi + + if ansi_is_usable; then + echo -e "${color}$type:$ANSI_FG_RESET $args ${ANSI_FG_BLACK_BRIGHT}[$funcname]$ANSI_FG_RESET" + else + echo "$type: $args [$funcname]" + fi +} + +### aliases #################################################################### + +alias echo_d='>&2 echo_message__ "$FUNCNAME" "${BASH_SOURCE[0]}" "debug" "$ANSI_FG_BLUE_BOLD"' +alias echo_e='>&2 echo_message__ "$FUNCNAME" "${BASH_SOURCE[0]}" "error" "$ANSI_FG_RED_BRIGHT"' +alias echo_i='>&2 echo_message__ "$FUNCNAME" "${BASH_SOURCE[0]}" "info" "$ANSI_FG_BLUE_BRIGHT"' +alias echo_o='>&2 echo_message__ "$FUNCNAME" "${BASH_SOURCE[0]}" "ok" "$ANSI_FG_GREEN_BRIGHT"' +alias echo_w='>&2 echo_message__ "$FUNCNAME" "${BASH_SOURCE[0]}" "warning" "$ANSI_FG_YELLOW_BRIGHT"' + +### main ####################################################################### + +# Nothing here. diff --git a/packaging/macos/jhb/usr/src/bash_d/error.sh b/packaging/macos/jhb/usr/src/bash_d/error.sh new file mode 100644 index 0000000..69ec0be --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/error.sh @@ -0,0 +1,45 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# https://github.com/dehesselle/bash_d + +### includes ################################################################### + +bash_d_include ansi.sh +bash_d_include echo.sh + +### variables ################################################################## + +# Nothing here. + +### functions ################################################################## + +function error_catch +{ + local rc=$1 + + local index=0 + local output + + while output=$(caller $index); do + if [ $index -eq 0 ]; then + if ansi_is_usable; then + echo_e "rc=$rc $ANSI_FG_YELLOW_BRIGHT$BASH_COMMAND$ANSI_FG_RESET" + else + echo "rc=$rc $BASH_COMMAND" + fi + fi + + echo_e $output + ((index+=1)) + done + + exit $rc +} + +### aliases #################################################################### + +alias error_trace_enable='set -o errtrace; trap '\''error_catch ${?}'\'' ERR' +alias error_trace_disable='trap - ERR' + +### main ####################################################################### + +# Nothing here. \ No newline at end of file diff --git a/packaging/macos/jhb/usr/src/bash_d/lib.sh b/packaging/macos/jhb/usr/src/bash_d/lib.sh new file mode 100644 index 0000000..c1f385e --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/lib.sh @@ -0,0 +1,174 @@ +# SPDX-FileCopyrightText: 2021 René de Hesselle +# +# SPDX-License-Identifier: GPL-2.0-or-later + +### description ################################################################ + +# Provide convenience wrappers for install_name_tool. + +### shellcheck ################################################################# + +# shellcheck shell=bash # no shebang as this file is intended to be sourced + +### dependencies ############################################################### + +assert_darwin +bash_d_include echo +bash_d_include readlinkf + +### variables ################################################################## + +LIB_RESET_ID=keep # options: basename, canonical, keep + +### functions ################################################################## + +function lib_change_path +{ + # Compared to install_name_tool, this function + # - requires less arguments as 'source' can be deducted from 'target' + # - can apply the requested changes to multiple binaries at once + + local target=$1 # new path to dynamically linked library + local binaries=${*:2} # binaries to modify + + local source_lib=${target##*/} # get library filename from target location + + for binary in $binaries; do # won't work if spaces in paths + if [[ $binary == *.so ]] || + [[ $binary == *.dylib ]] || + [ $(file $binary | grep "shared library" | wc -l) -eq 1 ]; then + lib_reset_id $binary + fi + + local source=$(otool -L $binary | grep "$source_lib " | awk '{ print $1 }') + if [ -z $source ]; then + echo_w "no $source_lib in $binary" + else + # Reconstructing 'target' as it might have been specified as regex. + target=$(dirname $target)/$(basename $source) + + install_name_tool -change $source $target $binary + fi + done +} + +function lib_change_paths +{ + # This is a wrapper ontop lib_change_path: given a directory 'lib_dir' that + # contains the libraries, all (matching) libraries linked in 'binary' can be + # changed at once to a specified 'target' path. + + local target=$1 # new path to dynamically linked library + local lib_dir=$2 + local binaries=${*:3} + + for binary in $binaries; do + for linked_lib in $(otool -L $binary | tail -n +2 | awk '{ print $1 }'); do + if [ "$(basename $binary)" != "$(basename $linked_lib)" ] && + [ -f $lib_dir/$(basename $linked_lib) ]; then + lib_change_path $target/$(basename $linked_lib) $binary + fi + done + done +} + +function lib_change_siblings +{ + # This is a wrapper ontop lib_change_path: all libraries inside a given + # 'lib_dir' that are linked to libraries located in that same 'lib_dir' can + # be automatically adjusted. + + local lib_dir=$1 + + for lib in $lib_dir/*.dylib; do + lib_reset_id $lib + for linked_lib in $(otool -L $lib | tail -n +2 | awk '{ print $1 }'); do + if [ "$(basename $lib)" != "$(basename $linked_lib)" ] && + [ -f $lib_dir/$(basename $linked_lib) ]; then + lib_change_path @loader_path/$(basename $linked_lib) $lib + fi + done + done +} + +function lib_reset_id +{ + local lib=$1 + + case "$LIB_RESET_ID" in + basename) + install_name_tool -id $(basename $lib) $lib + ;; + canonical) + install_name_tool -id $(readlinkf $lib) $lib + ;; + keep) + : # don't do anything + ;; + *) + echo_e "invalid value for LIB_RESET_ID: $LIB_RESET_ID" + ;; + esac +} + +function lib_add_rpath +{ + local rpath=$1 + local binary=$2 + + install_name_tool -add_rpath "$rpath" "$binary" +} + +function lib_clear_rpath +{ + local binary=$1 + + for rpath in $(otool -l $binary | grep -A2 LC_RPATH | grep -E "^[ ]+path" | awk '{ print $2 }'); do + install_name_tool -delete_rpath $rpath $binary + done +} + +function lib_replace_path +{ + local source=$1 + local target=$2 + local binary=$3 + + for lib in $(lib_get_linked $binary); do + if [[ $lib =~ $source ]]; then + lib_change_path @rpath/$(basename $lib) $binary + fi + done +} + +function lib_get_linked +{ + local binary=$1 # can be executable or library + + #echo_d "binary: $binary" + + local filter # we need to distinguish between executable and library + + local file_type + file_type=$(file "$binary") + if [[ $file_type = *"shared library"* ]]; then + filter="-v $(otool -D "$binary" | tail -n 1)" # exclude library id + elif [[ $file_type = *"executable"* ]]; then + filter="-E [.]+" # include everything + else + echo_w "neither shared library nor executable: $binary" + return 1 + fi + + # since we're not echoing this, output will be newline-separated + # shellcheck disable=SC2086 # need word splitting for arguments + otool -L "$binary" | grep " " | grep $filter | awk '{ print $1 }' +} + +### aliases #################################################################### + +# Nothing here. + +### main ####################################################################### + +# Nothing here. diff --git a/packaging/macos/jhb/usr/src/bash_d/readlinkf.sh b/packaging/macos/jhb/usr/src/bash_d/readlinkf.sh new file mode 100644 index 0000000..135a886 --- /dev/null +++ b/packaging/macos/jhb/usr/src/bash_d/readlinkf.sh @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# https://github.com/dehesselle/bash_d + +### description ################################################################ + +# This is a replacement for GNU's '-f' extension to 'readlink' which is not +# part of the BSD version. + +### includes ################################################################### + +# Nothing here. + +### variables ################################################################## + +# Nothing here. + +### functions ################################################################## + +# Nothing here. + +### aliases #################################################################### + +alias readlinkf='perl -e '"'"'use Cwd "abs_path"; print abs_path(@ARGV[0])'"'"' --' + +### main ####################################################################### + +# Nothing here. -- cgit v1.2.3