From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 7E55A1581D3 for ; Fri, 17 May 2024 04:03:52 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B86D4E2A74; Fri, 17 May 2024 04:03:51 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 966FAE2A74 for ; Fri, 17 May 2024 04:03:51 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 9BDAF342F94 for ; Fri, 17 May 2024 04:03:49 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id E8CD0132F for ; Fri, 17 May 2024 04:03:47 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1715823511.50ce35639679a07286dff5a409df6d44a6d9166c.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 50ce35639679a07286dff5a409df6d44a6d9166c X-VCS-Branch: master Date: Fri, 17 May 2024 04:03:47 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: c90552f8-1e39-4372-ac7f-c3e9c3d1ba09 X-Archives-Hash: 092b2c73c5fd10783b674655b22a22a7 commit: 50ce35639679a07286dff5a409df6d44a6d9166c Author: Kerin Millar plushkava net> AuthorDate: Thu May 16 01:38:31 2024 +0000 Commit: Sam James gentoo org> CommitDate: Thu May 16 01:38:31 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=50ce3563 Reorganise the order in which functions are declared Sort by alphabetical order, grouped by public then private scope. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 598 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 298 insertions(+), 300 deletions(-) diff --git a/functions.sh b/functions.sh index 7bd4b11..8561eb3 100644 --- a/functions.sh +++ b/functions.sh @@ -10,32 +10,69 @@ # and to reduce the probability of name space conflicts. # -# Called by ebegin, eerrorn, einfon, and ewarnn. +# A safe wrapper for the cd builtin. To run cd "$dir" is problematic because: # -_eprint() +# 1) it may consider its operand as an option +# 2) it will search CDPATH for an operand not beginning with ./, ../ or / +# 3) it will switch to OLDPWD if the operand is - +# 4) cdable_vars causes bash to treat the operand as a potential variable name +# +chdir() { - local color + if [ "$BASH" ]; then + # shellcheck disable=3044 + shopt -u cdable_vars + fi + if [ "$1" = - ]; then + set -- ./- + fi + # shellcheck disable=1007,2164 + CDPATH= cd -- "$@" +} - color=$1 - shift +# +# show a message indicating the start of a process +# +ebegin() +{ + local msg - if [ -t 1 ]; then - printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "$*" - else - printf ' * %s%s' "${genfun_indent}" "$*" + if ! yesno "${EINFO_QUIET}"; then + msg=$* + while _ends_with_newline "${msg}"; do + msg=${msg%"${genfun_newline}"} + done + _eprint "${GOOD}" "${msg} ...${genfun_newline}" fi } # -# hard set the indent used for e-commands. -# num defaults to 0 +# indicate the completion of process +# if error, show errstr via eerror # -_esetdent() +eend() { - if ! is_int "$1" || [ "$1" -lt 0 ]; then - set -- 0 + GENFUN_CALLER=${GENFUN_CALLER:-eend} _eend eerror "$@" +} + +# +# show an error message (with a newline) and log it +# +eerror() +{ + eerrorn "${*}${genfun_newline}" +} + +# +# show an error message (without a newline) and log it +# +eerrorn() +{ + if ! yesno "${EERROR_QUIET}"; then + _eprint "${BAD}" "$@" >&2 + esyslog "daemon.err" "${0##*/}" "$@" fi - genfun_indent=$(printf "%${1}s" '') + return 1 } # @@ -50,40 +87,32 @@ eindent() } # -# decrease the indent used for e-commands. +# show an informative message (with a newline) # -eoutdent() +einfo() { - if ! is_int "$1" || [ "$1" -le 0 ]; then - set -- 2 + einfon "${*}${genfun_newline}" +} + +# +# show an informative message (without a newline) +# +einfon() +{ + if ! yesno "${EINFO_QUIET}"; then + _eprint "${GOOD}" "$@" fi - _esetdent "$(( ${#genfun_indent} - $1 ))" } # -# this function was lifted from OpenRC. It returns 0 if the argument or -# the value of the argument is "yes", "true", "on", or "1" or 1 -# otherwise. +# decrease the indent used for e-commands. # -yesno() +eoutdent() { - for _ in 1 2; do - case $1 in - [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0|'') - return 1 - ;; - [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) - return 0 - esac - if [ "$_" -ne 1 ] || ! is_identifier "$1"; then - ! break - else - # The value appears to be a legal variable name. Treat - # it as a name reference and try again, once only. - eval "set -- \"\$$1\"" - fi - done || vewarn "Invalid argument given to yesno (expected a boolean-like or a legal name)" - return 1 + if ! is_int "$1" || [ "$1" -le 0 ]; then + set -- 2 + fi + _esetdent "$(( ${#genfun_indent} - $1 ))" } # @@ -110,21 +139,11 @@ esyslog() } # -# show an informative message (without a newline) -# -einfon() -{ - if ! yesno "${EINFO_QUIET}"; then - _eprint "${GOOD}" "$@" - fi -} - -# -# show an informative message (with a newline) +# show a warning message (with a newline) and log it # -einfo() +ewarn() { - einfon "${*}${genfun_newline}" + ewarnn "${*}${genfun_newline}" } # @@ -139,49 +158,217 @@ ewarnn() } # -# show a warning message (with a newline) and log it +# indicate the completion of process +# if error, show errstr via ewarn # -ewarn() +ewend() { - ewarnn "${*}${genfun_newline}" + GENFUN_CALLER=${GENFUN_CALLER:-ewend} _eend ewarn "$@" } # -# show an error message (without a newline) and log it +# return 0 if gentoo=param was passed to the kernel # -eerrorn() -{ - if ! yesno "${EERROR_QUIET}"; then - _eprint "${BAD}" "$@" >&2 - esyslog "daemon.err" "${0##*/}" "$@" - fi +# EXAMPLE: if get_bootparam "nodevfs" ; then .... +# +get_bootparam() +( + # Gentoo cmdline parameters are comma-delimited, so a search + # string containing a comma must not be allowed to match. + # Similarly, the empty string must not be allowed to match. + case $1 in ''|*,*) return 1 ;; esac + + # Reset the value of IFS because there is no telling what it may be. + IFS=$(printf ' \n\t') + + if [ "${TEST_GENFUNCS}" = 1 ]; then + read -r cmdline + else + read -r cmdline < /proc/cmdline + fi || return + + # Disable pathname expansion. The definition of this function + # is a compound command that incurs a subshell. Therefore, the + # prior state of the option does not need to be recalled. + set -f + for opt in ${cmdline}; do + gentoo_opt=${opt#gentoo=} + if [ "${opt}" != "${gentoo_opt}" ]; then + case ,${gentoo_opt}, in + *,"$1",*) return 0 + esac + fi + done return 1 -} +) # -# show an error message (with a newline) and log it +# Determine whether the first operand is a valid identifier (variable name). # -eerror() +is_identifier() +( + LC_ALL=C + case $1 in + ''|_|[[:digit:]]*|*[!_[:alnum:]]*) false + esac +) + +# +# Determine whether the first operand is in the form of an integer. A leading +# shall be permitted. Thereafter, leading zeroes shall not be +# permitted because the string might later be considered to be octal in an +# arithmetic context, causing the shell to exit if the number be invalid. +# +is_int() { - eerrorn "${*}${genfun_newline}" + set -- "${1#-}" + case $1 in + ''|*[!0123456789]*) + false + ;; + 0) + true + ;; + *) + test "$1" = "${1#0}" + esac } # -# show a message indicating the start of a process +# return 0 if any of the files/dirs are newer than +# the reference file # -ebegin() +# EXAMPLE: if is_older_than a.out *.o ; then ... +is_older_than() { - local msg + local ref has_gfind - if ! yesno "${EINFO_QUIET}"; then - msg=$* - while _ends_with_newline "${msg}"; do - msg=${msg%"${genfun_newline}"} - done - _eprint "${GOOD}" "${msg} ...${genfun_newline}" + if [ "$#" -lt 2 ]; then + ewarn "Too few arguments for is_older_than (got $#, expected at least 2)" + return 1 + elif [ -e "$1" ]; then + ref=$1 + else + ref= + fi + shift + + # Consult the hash table in the present shell, prior to forking. + hash gfind 2>/dev/null; has_gfind=$(( $? == 0 )) + + for path; do + if [ -e "${path}" ]; then + printf '%s\0' "${path}" + fi + done | + { + set -- -L -files0-from - ${ref:+-newermm} ${ref:+"${ref}"} -printf '\n' -quit + if [ "${has_gfind}" -eq 1 ]; then + gfind "$@" + else + find "$@" + fi + } | + read -r _ +} + +vebegin() +{ + if yesno "${EINFO_VERBOSE}"; then + ebegin "$@" + fi +} + +veend() +{ + if yesno "${EINFO_VERBOSE}"; then + GENFUN_CALLER=veend eend "$@" + elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then + ewarn "Invalid argument given to veend (the exit status code must be an integer >= 0)" + else + return "$1" + fi +} + +veerror() +{ + if yesno "${EINFO_VERBOSE}"; then + eerror "$@" fi } +veindent() +{ + if yesno "${EINFO_VERBOSE}"; then + eindent "$@" + fi +} + +veinfo() +{ + if yesno "${EINFO_VERBOSE}"; then + einfo "$@" + fi +} + +veinfon() +{ + if yesno "${EINFO_VERBOSE}"; then + einfon "$@" + fi +} + +veoutdent() +{ + if yesno "${EINFO_VERBOSE}"; then + eoutdent "$@" + fi +} + +vewarn() +{ + if yesno "${EINFO_VERBOSE}"; then + ewarn "$@" + fi +} + +vewend() +{ + if yesno "${EINFO_VERBOSE}"; then + GENFUN_CALLER=vewend ewend "$@" + elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then + ewarn "Invalid argument given to vewend (the exit status code must be an integer >= 0)" + else + return "$1" + fi +} + +# +# this function was lifted from OpenRC. It returns 0 if the argument or +# the value of the argument is "yes", "true", "on", or "1" or 1 +# otherwise. +# +yesno() +{ + for _ in 1 2; do + case $1 in + [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0|'') + return 1 + ;; + [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) + return 0 + esac + if [ "$_" -ne 1 ] || ! is_identifier "$1"; then + ! break + else + # The value appears to be a legal variable name. Treat + # it as a name reference and try again, once only. + eval "set -- \"\$$1\"" + fi + done || vewarn "Invalid argument given to yesno (expected a boolean-like or a legal name)" + return 1 +} + # # indicate the completion of process, called from eend/ewend # if error, show errstr via efunc @@ -240,231 +427,41 @@ _eend() return "${retval}" } -# -# indicate the completion of process -# if error, show errstr via eerror -# -eend() +_ends_with_newline() { - GENFUN_CALLER=${GENFUN_CALLER:-eend} _eend eerror "$@" + test "${genfun_newline}" \ + && ! case $1 in *"${genfun_newline}") false ;; esac } # -# indicate the completion of process -# if error, show errstr via ewarn +# Called by ebegin, eerrorn, einfon, and ewarnn. # -ewend() -{ - GENFUN_CALLER=${GENFUN_CALLER:-ewend} _eend ewarn "$@" -} - -# v-e-commands honor EINFO_VERBOSE which defaults to no. -veinfo() -{ - if yesno "${EINFO_VERBOSE}"; then - einfo "$@" - fi -} - -veinfon() -{ - if yesno "${EINFO_VERBOSE}"; then - einfon "$@" - fi -} - -vewarn() -{ - if yesno "${EINFO_VERBOSE}"; then - ewarn "$@" - fi -} - -veerror() -{ - if yesno "${EINFO_VERBOSE}"; then - eerror "$@" - fi -} - -vebegin() -{ - if yesno "${EINFO_VERBOSE}"; then - ebegin "$@" - fi -} - -veend() -{ - if yesno "${EINFO_VERBOSE}"; then - GENFUN_CALLER=veend eend "$@" - elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then - ewarn "Invalid argument given to veend (the exit status code must be an integer >= 0)" - else - return "$1" - fi -} - -vewend() -{ - if yesno "${EINFO_VERBOSE}"; then - GENFUN_CALLER=vewend ewend "$@" - elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then - ewarn "Invalid argument given to vewend (the exit status code must be an integer >= 0)" - else - return "$1" - fi -} - -veindent() -{ - if yesno "${EINFO_VERBOSE}"; then - eindent "$@" - fi -} - -veoutdent() +_eprint() { - if yesno "${EINFO_VERBOSE}"; then - eoutdent "$@" - fi -} - -# -# return 0 if gentoo=param was passed to the kernel -# -# EXAMPLE: if get_bootparam "nodevfs" ; then .... -# -get_bootparam() -( - # Gentoo cmdline parameters are comma-delimited, so a search - # string containing a comma must not be allowed to match. - # Similarly, the empty string must not be allowed to match. - case $1 in ''|*,*) return 1 ;; esac - - # Reset the value of IFS because there is no telling what it may be. - IFS=$(printf ' \n\t') - - if [ "${TEST_GENFUNCS}" = 1 ]; then - read -r cmdline - else - read -r cmdline < /proc/cmdline - fi || return - - # Disable pathname expansion. The definition of this function - # is a compound command that incurs a subshell. Therefore, the - # prior state of the option does not need to be recalled. - set -f - for opt in ${cmdline}; do - gentoo_opt=${opt#gentoo=} - if [ "${opt}" != "${gentoo_opt}" ]; then - case ,${gentoo_opt}, in - *,"$1",*) return 0 - esac - fi - done - return 1 -) + local color -# -# return 0 if any of the files/dirs are newer than -# the reference file -# -# EXAMPLE: if is_older_than a.out *.o ; then ... -is_older_than() -{ - local ref has_gfind + color=$1 + shift - if [ "$#" -lt 2 ]; then - ewarn "Too few arguments for is_older_than (got $#, expected at least 2)" - return 1 - elif [ -e "$1" ]; then - ref=$1 + if [ -t 1 ]; then + printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "$*" else - ref= + printf ' * %s%s' "${genfun_indent}" "$*" fi - shift - - # Consult the hash table in the present shell, prior to forking. - hash gfind 2>/dev/null; has_gfind=$(( $? == 0 )) - - for path; do - if [ -e "${path}" ]; then - printf '%s\0' "${path}" - fi - done | - { - set -- -L -files0-from - ${ref:+-newermm} ${ref:+"${ref}"} -printf '\n' -quit - if [ "${has_gfind}" -eq 1 ]; then - gfind "$@" - else - find "$@" - fi - } | - read -r _ -} - -# -# Determine whether the first operand is in the form of an integer. A leading -# shall be permitted. Thereafter, leading zeroes shall not be -# permitted because the string might later be considered to be octal in an -# arithmetic context, causing the shell to exit if the number be invalid. -# -is_int() -{ - set -- "${1#-}" - case $1 in - ''|*[!0123456789]*) - false - ;; - 0) - true - ;; - *) - test "$1" = "${1#0}" - esac } # -# A safe wrapper for the cd builtin. To run cd "$dir" is problematic because: -# -# 1) it may consider its operand as an option -# 2) it will search CDPATH for an operand not beginning with ./, ../ or / -# 3) it will switch to OLDPWD if the operand is - -# 4) cdable_vars causes bash to treat the operand as a potential variable name +# hard set the indent used for e-commands. +# num defaults to 0 # -chdir() +_esetdent() { - if [ "$BASH" ]; then - # shellcheck disable=3044 - shopt -u cdable_vars - fi - if [ "$1" = - ]; then - set -- ./- + if ! is_int "$1" || [ "$1" -lt 0 ]; then + set -- 0 fi - # shellcheck disable=1007,2164 - CDPATH= cd -- "$@" -} - -# -# Determine whether the first operand contains any visible characters. -# -_is_visible() -{ - ! case $1 in *[[:graph:]]*) false ;; esac + genfun_indent=$(printf "%${1}s" '') } -# -# Determine whether the first operand is a valid identifier (variable name). -# -is_identifier() -( - LC_ALL=C - case $1 in - ''|_|[[:digit:]]*|*[!_[:alnum:]]*) false - esac -) - _has_dumb_terminal() { ! case ${TERM} in *dumb*) false ;; esac @@ -485,12 +482,26 @@ _has_monochrome_terminal() fi } -_ends_with_newline() +# +# Determine whether the first operand contains any visible characters. +# +_is_visible() { - test "${genfun_newline}" \ - && ! case $1 in *"${genfun_newline}") false ;; esac + ! case $1 in *[[:graph:]]*) false ;; esac } +_update_columns() +{ + local ifs + + # The following use of stty(1) is portable as of POSIX Issue 8. + ifs=$IFS + IFS=' ' + # shellcheck disable=2046 + set -- $(stty size 2>/dev/null) + IFS=$ifs + [ "$#" -eq 2 ] && is_int "$2" && [ "$2" -gt 0 ] && genfun_cols=$2 +} _update_tty_level() { @@ -509,19 +520,6 @@ _update_tty_level() fi } -_update_columns() -{ - local ifs - - # The following use of stty(1) is portable as of POSIX Issue 8. - ifs=$IFS - IFS=' ' - # shellcheck disable=2046 - set -- $(stty size 2>/dev/null) - IFS=$ifs - [ "$#" -eq 2 ] && is_int "$2" && [ "$2" -gt 0 ] && genfun_cols=$2 -} - # This is the main script, please add all functions above this point! # shellcheck disable=2034 RC_GOT_FUNCTIONS="yes"