[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-24 Thread Sam James
commit: 6f545efa90041e0743086f8f99ebe8f711b1fbe9
Author: Sam James  gentoo  org>
AuthorDate: Fri May 24 06:04:51 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 24 06:04:51 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6f545efa

meson.build: prepare for gentoo-functions-1.6

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 624f5c0..6590240 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.5',
+  version: '1.6',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-23 Thread Sam James
commit: 2143fdb2adf0131e9baa4f308b8125cfe5e92a47
Author: Kerin Millar  plushkava  net>
AuthorDate: Wed May 22 16:38:23 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Wed May 22 16:38:23 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=2143fdb2

Correct the wrapping of a comment

In accordance with the presumption of a tab being as wide as 8 spaces.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/functions.sh b/functions.sh
index f965534..48b4c5a 100644
--- a/functions.sh
+++ b/functions.sh
@@ -389,9 +389,9 @@ is_older_than()
fi
shift
 
-   # Check whether GNU find is installed by the name of "gfind". So as to 
avoid
-   # repeated PATH lookups, run the hash builtin in the present shell, 
prior to
-   # forking.
+   # Check whether GNU find is installed by the name of "gfind". So as to
+   # avoid repeated PATH lookups, run the hash builtin in the present
+   # shell, prior to forking.
hash gfind 2>/dev/null; has_gfind=$(( $? == 0 ))
 
for path; do



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-23 Thread Sam James
commit: b5feaf2444ae625f5756c7566042d28f81a2cda1
Author: Kerin Millar  plushkava  net>
AuthorDate: Wed May 22 05:54:12 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Wed May 22 16:27:00 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=b5feaf24

Micro-optimise the eqatag() argument parsing loop

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 10 ++
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/functions.sh b/functions.sh
index 98e7a6f..f965534 100644
--- a/functions.sh
+++ b/functions.sh
@@ -187,7 +187,7 @@ eoutdent()
 #
 eqatag()
 {
-   local arg argc json positional tag
+   local arg i json positional tag
 
case ${genfun_has_jq} in
0)
@@ -211,11 +211,14 @@ eqatag()
if [ "$#" -eq 0 ]; then
die "eqatag: no tag specified"
fi
+   positional=0
tag=$1
shift
-   argc=$#
-   positional=0
+   i=0
for arg; do
+   if [ "$(( i += 1 ))" -eq 1 ]; then
+   set --
+   fi
case ${arg} in
[!=/]*=?*)
if [ "${positional}" -eq 1 ]; then
@@ -234,7 +237,6 @@ eqatag()
_throw_invalid_args eqatag "${arg}"
esac
done
-   shift "${argc}"
json=$(
jq -cn '{
eqatag: {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 3de0f4f80587a58d93eb4d67dc0d536035cbd566
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 20 07:37:15 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 09:28:19 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=3de0f4f8

Render argument checking more stringent; standardise diagnostics

Have esyslog(), is_older_than(), veend(), vewend() and yesno() die for
invalid arguments rather than issue a warning.

Have yesno() die if given no arguments, just as several other functions
already do.

Introduce a helper function named _throw_invalid_args() to assist. It
calls upon _print_args() to safely display any offending argument(s)
while also helping to standardise the diagnostics. To that end, the
diagnostic messages concerning wrong argument counts have been adjusted
to follow suit. Below are some examples.

test-functions: is_older_than: too few arguments (got 0, expected at least 2)
test-functions: is_older_than: too few arguments (got 1, expected at least 2)
test-functions: esyslog: too few arguments (got 0, expected at least 2)
test-functions: esyslog: too few arguments (got 1, expected at least 2)
test-functions: yesno: invalid argument: 'not-a-valid-nameref'
test-functions: yesno: invalid argument: '_"; set -- yes # code injection'

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh   | 45 +++--
 test-functions | 11 +--
 2 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/functions.sh b/functions.sh
index 100bd30..c82c229 100644
--- a/functions.sh
+++ b/functions.sh
@@ -216,7 +216,7 @@ eqatag() {
case ${arg} in
[!=/]*=?*)
if [ "${positional}" -eq 1 ]; then
-   die "eqatag: invalid argument in 
positional context -- ${arg}"
+   _throw_invalid_args eqatag "${arg}"
fi
set -- "$@" --arg "${arg%%=*}" "${arg#*=}"
;;
@@ -228,7 +228,7 @@ eqatag() {
set -- "$@" "${arg}"
;;
*)
-   die "eqatag: invalid argument -- ${arg}"
+   _throw_invalid_args eqatag "${arg}"
esac
done
shift "${argc}"
@@ -263,8 +263,7 @@ esyslog()
local pri tag msg
 
if [ "$#" -lt 2 ]; then
-   ewarn "Too few arguments for esyslog (got $#, expected at least 
2)"
-   return 1
+   die "esyslog: too few arguments (got $#, expected at least 2)"
elif yesno "${EINFO_LOG}" && hash logger 2>/dev/null; then
pri=$1
tag=$2
@@ -376,8 +375,7 @@ is_older_than()
local ref has_gfind
 
if [ "$#" -lt 2 ]; then
-   ewarn "Too few arguments for is_older_than (got $#, expected at 
least 2)"
-   return 1
+   die "is_older_than: too few arguments (got $#, expected at 
least 2)"
elif [ -e "$1" ]; then
ref=$1
else
@@ -424,7 +422,7 @@ 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)"
+   _throw_invalid_args veend "$1"
else
return "$1"
fi
@@ -435,7 +433,7 @@ 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)"
+   _throw_invalid_args vewend "$1"
else
return "$1"
fi
@@ -449,8 +447,14 @@ vewend()
 #
 yesno()
 {
+   local arg
+
+   if [ "$#" -eq 0 ]; then
+   die "yesno: too few arguments (got $#, expected 1)"
+   fi
+   arg=$1
for _ in 1 2; do
-   case $1 in
+   case ${arg} in
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0|'')
return 1
;;
@@ -462,9 +466,9 @@ yesno()
else
# The value appears to be a legal variable name. Treat
# it as a name reference and try again, once only.
-   eval "set -- \"\$$1\""
+   eval "arg=\$$1"
fi
-   done || vewarn "Invalid argument given to yesno (expected a 
boolean-like or a legal name)"
+   done || _throw_invalid_args yesno "$1"
return 1
 }
 
@@ -481,9 +485,7 @@ _eend()
if [ "$#" -eq 0 ]; then
  

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 5c82cfc08a05e994dbb719040a8ed6c81f6b6ec6
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 20 07:45:22 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 09:28:20 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=5c82cfc0

Adhere to the Allman style (again)

For sh, not awk.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 18 --
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/functions.sh b/functions.sh
index c82c229..8c0d1c1 100644
--- a/functions.sh
+++ b/functions.sh
@@ -92,7 +92,8 @@ ebegin()
 # Should the command fail, a diagnostic message shall be printed and the shell
 # be made to exit by calling the die function.
 #
-edo() {
+edo()
+{
genfun_cmd=$(_print_args "$@")
einfo "Executing: ${genfun_cmd}"
"$@" || die "Failed to execute command: ${genfun_cmd}"
@@ -117,7 +118,8 @@ eend()
 #
 for _ in eerror einfo ewarn; do
eval "
-   $_ () {
+   $_ ()
+   {
${_}n \"\${*}\${genfun_newline}\"
}
"
@@ -183,7 +185,8 @@ eoutdent()
 # parameters the value of a "files" key. The resulting object shall be rendered
 # as JSON by jq(1) before being logged by the logger(1) utility.
 #
-eqatag() {
+eqatag()
+{
local arg argc json positional tag
 
case ${genfun_has_jq} in
@@ -249,7 +252,8 @@ eqatag() {
 # the message shall also be conveyed to the esyslog function. For now, this is
 # implemented merely as an ewarn wrapper.
 #
-eqawarn() {
+eqawarn()
+{
ewarn "$@"
 }
 
@@ -409,7 +413,8 @@ is_older_than()
 #
 for _ in vebegin veerror veindent veinfo veinfon veoutdent vewarn; do
eval "
-   $_ () {
+   $_ ()
+   {
if yesno \"\${EINFO_VERBOSE}\"; then
${_#v} \"\$@\"
fi
@@ -605,7 +610,8 @@ _is_visible()
 # of the ${*@Q} expansion in bash. The output shall be POSIX sh compatible as 
of
 # Issue 8. This should probably be made to exist as a standalone awk script.
 #
-_print_args() {
+_print_args()
+{
awk -v q=\' -f - -- "$@" <<-'EOF'
BEGIN {
argc = ARGC



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: cac89f9c95f04128a7cd119ac9acd1127f0d3e64
Author: Kerin Millar  plushkava  net>
AuthorDate: Tue May 21 10:45:54 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 21 11:03:07 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=cac89f9c

test-functions: Render tests somewhat fast again

Do so by having the tests for is_older_than() and yesno() employ
subshells only where necessary.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 17 +
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/test-functions b/test-functions
index 40beded..6f755db 100755
--- a/test-functions
+++ b/test-functions
@@ -196,7 +196,11 @@ test_is_older_than() {
callback() {
shift
test_description="is_older_than $(_print_args "$@")"
-   ( is_older_than "$@" )
+   if [ "$#" -lt 2 ]; then
+   ( is_older_than "$@" )
+   else
+   is_older_than "$@"
+   fi
}
 
iterate_tests 4 "$@"
@@ -371,6 +375,7 @@ test_yesno() {
eq  0  ON \
eq  0  On \
eq  0  1 \
+   eq  0  truthful_nameref \
ge  1  no \
ge  1  NO \
ge  1  No \
@@ -383,16 +388,20 @@ test_yesno() {
ge  1  0 \
ge  1  not_a_nameref \
ge  1  not-a-valid-nameref \
-   ge  1  '_"; set -- yes # code injection' \
-   eq  0  truthful_nameref
+   ge  1  '_"; set -- yes # code injection'
 
# shellcheck disable=2034
truthful_nameref=yes
+   row=0
 
callback() {
shift
test_description="yesno $(_print_args "$@")"
-   ( yesno "$@" )
+   if [ "$(( row += 1 ))" -ge 22 ]; then
+   ( yesno "$@" )
+   else
+   yesno "$@"
+   fi
}
 
iterate_tests 3 "$@"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 1515b614f83bc30fe1ed63d6a76bf4ede0b5c2f8
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 22:03:53 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 07:50:05 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1515b614

Have _print_args() produce octal sequences for DEL

With this change, octal sequences are rendered for all non-printable
characters in the US-ASCII character set.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/functions.sh b/functions.sh
index 94d0f98..bbbd114 100644
--- a/functions.sh
+++ b/functions.sh
@@ -603,7 +603,6 @@ _is_visible()
 # of the ${*@Q} expansion in bash. The output shall be POSIX sh compatible as 
of
 # Issue 8. This should probably be made to exist as a standalone awk script.
 #
-#
 _print_args() {
awk -v q=\' -f - -- "$@" <<-'EOF'
BEGIN {
@@ -611,7 +610,7 @@ _print_args() {
ARGC = 1
for (arg_idx = 1; arg_idx < argc; arg_idx++) {
arg = ARGV[arg_idx]
-   if (arg !~ /[\001-\037]/) {
+   if (arg !~ /[\001-\037\177]/) {
gsub(q, q "\\" q q, arg)
word = q arg q
} else {
@@ -621,6 +620,7 @@ _print_args() {
char = sprintf("%c", i)
ord_by[char] = i
}
+   ord_by["\177"] = 127
}
word = "$'"
for (i = 1; i <= length(arg); i++) {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 8a8dc3b63c8c639117c12a22960b03102dc00942
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 17:41:23 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 07:49:53 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=8a8dc3b6

Add an eqatag() function

This differs from the isolated-functions implementation in that it will
render a line of compact JSON then use the logger(1) utility to log it.
It requires jq in order to do so. Should jq be unavailable, a warning
will be displayed, though no more than once.

Signed-off-by: Kerin Millar  plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh | 72 
 1 file changed, 72 insertions(+)

diff --git a/functions.sh b/functions.sh
index f2e5bb5..94d0f98 100644
--- a/functions.sh
+++ b/functions.sh
@@ -172,6 +172,78 @@ eoutdent()
_esetdent "$(( ${#genfun_indent} - $1 ))"
 }
 
+#
+# This is based on the eqatag function defined by isolated-functions.sh in
+# portage. If the first parameter is the -v option, it shall be disregarded.
+# Discounting said option, at least one parameter is required, which shall be
+# taken as a tag name. Thereafter, zero or more parameters shall be accepted in
+# the form of "key=val", followed by zero or more parameters beginning with a
+# . An object shall be composed in which the tag is the value of a "tag"
+# key, the key/value pairs the value of a "data" key, and the -prefixed
+# parameters the value of a "files" key. The resulting object shall be rendered
+# as JSON by jq(1) before being logged by the logger(1) utility.
+#
+eqatag() {
+   local arg argc json positional tag
+
+   case ${genfun_has_jq} in
+   0)
+   return 1
+   ;;
+   1)
+   ;;
+   *)
+   if command -v jq >/dev/null; then
+   genfun_has_jq=1
+   else
+   ewarn "The eqatag() function requires that jq 
be installed"
+   genfun_has_jq=0
+   return 1
+   fi
+   esac
+   # Acknowledge the -v option for isolated-functions API compatibility.
+   if [ "$1" = "-v" ]; then
+   shift
+   fi
+   if [ "$#" -eq 0 ]; then
+   die "eqatag: no tag specified"
+   fi
+   tag=$1
+   shift
+   argc=$#
+   positional=0
+   for arg; do
+   case ${arg} in
+   [!=/]*=?*)
+   if [ "${positional}" -eq 1 ]; then
+   die "eqatag: invalid argument in 
positional context -- ${arg}"
+   fi
+   set -- "$@" --arg "${arg%%=*}" "${arg#*=}"
+   ;;
+   /*)
+   if [ "${positional}" -eq 0 ]; then
+   set -- "$@" --args --
+   positional=1
+   fi
+   set -- "$@" "${arg}"
+   ;;
+   *)
+   die "eqatag: invalid argument -- ${arg}"
+   esac
+   done
+   shift "${argc}"
+   json=$(
+   jq -cn '{
+   eqatag: {
+   tag:   $ARGS.named["=tag"],
+   data:  $ARGS.named | with_entries(select(.key | 
startswith("=") | not)),
+   files: $ARGS.positional
+   }
+   }' --arg "=tag" "${tag}" "$@"
+   ) \
+   && logger -p user.debug -t "${0##*/}" -- "${json}"
+}
+
 #
 # Prints a QA warning message, provided that EINFO_QUIET is false. If printed,
 # the message shall also be conveyed to the esyslog function. For now, this is



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 7b63d3d64275c32676a8cad3377294ab59b54486
Author: Kerin Millar  plushkava  net>
AuthorDate: Tue May 21 10:48:37 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 21 12:51:48 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=7b63d3d6

test-functions: Minor ebegin() test refactoring

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/test-functions b/test-functions
index 6f755db..d65a3a2 100755
--- a/test-functions
+++ b/test-functions
@@ -95,19 +95,19 @@ test_ebegin() {
_eprint() {
shift
_ends_with_newline "$*"
-   ok=$(( $? == 0 ))
}
 
ok=0
set -- "message"
ebegin "$1"
+   retval=$?
 
-   if [ "${ok}" -eq 0 ]; then
+   if [ "${retval}" -ne 0 ]; then
printf 'not '
fi
printf 'ok %d - ebegin %s (expecting terminating newline)\n' 
"$((testnum + 1))" "$1"
 
-   return "$(( ok ? 0 : 1 ))"
+   return "${retval}"
 }
 
 test_edo() {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: 85e364c40ed8ec473888b28d71a759365cc78aa5
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 19:45:30 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 07:49:54 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=85e364c4

test-functions: Include /opt/pkg/bin in PATH

This helps where testing on platforms having utilities that were
installed by pkgsrc. In particular, gfind.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test-functions b/test-functions
index 1fe07ad..7eb5981 100755
--- a/test-functions
+++ b/test-functions
@@ -435,7 +435,7 @@ printf 'TAP version 13\n'
 unset -v dir
 
 # PATH is redefined to prevent ebuild-helpers such as die from interfering.
-export PATH=/sbin:/bin:/usr/sbin:/usr/bin
+export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/pkg/bin
 export TEST_GENFUNCS=1
 export TZ=UTC
 testnum=0



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-21 Thread Sam James
commit: e1b0fb2a8eb93814ff1d895964ad03d5166175e3
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 20 05:55:55 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 20 07:50:07 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=e1b0fb2a

Correct an awk else clause in _print_args()

The routine was working correctly anyway, but through fortune. Stick to using
curly braces to avoid any mishaps further down the line.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 16 ++--
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/functions.sh b/functions.sh
index bbbd114..100bd30 100644
--- a/functions.sh
+++ b/functions.sh
@@ -625,21 +625,25 @@ _print_args() {
word = "$'"
for (i = 1; i <= length(arg); i++) {
char = substr(arg, i, 1)
-   if (char == "\\")
+   if (char == "\\") {
word = word ""
-   else if (char == q)
+   } else if (char == q) {
word = word "\\'"
-   else
+   } else {
ord = ord_by[char]
-   if (ord != "")
+   if (ord != "") {
word = word 
"\\" sprintf("%03o", ord)
-   else
+   } else {
word = word char
+   }
+   }
}
word = word q
}
line = line word
-   if (arg_idx < argc - 1) line = line " "
+   if (arg_idx < argc - 1) {
+   line = line " "
+   }
}
print line
}



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-19 Thread Sam James
commit: 774be3a959c128a8cac1d641edb2e208421cc796
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 14:51:16 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sun May 19 14:51:16 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=774be3a9

Have edo() clarify that something is being executed

Because that is what happens.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/functions.sh b/functions.sh
index 890fa7a..0f0aa44 100644
--- a/functions.sh
+++ b/functions.sh
@@ -94,8 +94,8 @@ ebegin()
 #
 edo() {
genfun_cmd=$(_print_args "$@")
-   einfo "${genfun_cmd}"
-   "$@" || die "Failed to run command: ${genfun_cmd}"
+   einfo "Executing: ${genfun_cmd}"
+   "$@" || die "Failed to execute command: ${genfun_cmd}"
 }
 
 #



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-19 Thread Sam James
commit: 1b936a7427ec2100b782dd2e19b47b7f54886be3
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 14:33:54 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sun May 19 14:48:33 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1b936a74

Significantly improve _print_args() behaviour

It will now employ $'' style quoting where useful, per Issue 8:

https://austingroupbugs.net/view.php?id=249

Characters whose ordinal values are lower than 0x20 will be converted to
octal sequences. It should be considered in due course whether this
ought to exist as a standalone awk script.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 42 +++---
 1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/functions.sh b/functions.sh
index 462db97..890fa7a 100644
--- a/functions.sh
+++ b/functions.sh
@@ -94,8 +94,8 @@ ebegin()
 #
 edo() {
genfun_cmd=$(_print_args "$@")
-   einfo "${genfun_cmd% }"
-   "$@" || die "Failed to run command: ${genfun_cmd% }"
+   einfo "${genfun_cmd}"
+   "$@" || die "Failed to run command: ${genfun_cmd}"
 }
 
 #
@@ -528,18 +528,46 @@ _is_visible()
 
 #
 # Prints the positional parameters in a manner that approximates the behaviour
-# of the ${*@Q} expansion in bash.
+# of the ${*@Q} expansion in bash. The output shall be POSIX sh compatible as 
of
+# Issue 8. This should probably be made to exist as a standalone awk script.
+#
 #
 _print_args() {
awk -v q=\' -f - -- "$@" <<-'EOF'
BEGIN {
+   for (i = 1; i < 32; i++) {
+   char = sprintf("%c", i)
+   ord_by[char] = i
+   }
argc = ARGC
ARGC = 1
-   for (i = 1; i < argc; i++) {
-   arg = ARGV[i]
-   gsub(q, q "\\" q q, arg)
-   printf("'%s' ", arg)
+   for (arg_idx = 1; arg_idx < argc; arg_idx++) {
+   arg = ARGV[arg_idx]
+   if (arg !~ /[\001-\037]/) {
+   gsub(q, q "\\" q q, arg)
+   word = q arg q
+   } else {
+   # Use $'' quoting per Issue 8
+   word = "$'"
+   for (i = 1; i <= length(arg); i++) {
+   char = substr(arg, i, 1)
+   if (char == "\\")
+   word = word ""
+   else if (char == q)
+   word = word "\\'"
+   else
+   ord = ord_by[char]
+   if (ord != "")
+   word = word 
"\\" sprintf("%03o", ord)
+   else
+   word = word char
+   }
+   word = word q
+   }
+   line = line word
+   if (arg_idx < argc - 1) line = line " "
}
+   print line
}
EOF
 }



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-19 Thread Sam James
commit: 0744f81fd5109779a86026db2d1c42ee0230a38d
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 14:31:06 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sun May 19 14:31:06 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=0744f81f

test-functions: Actually remove print_args()

This was supposed to be staged for the last commit that touched
test-functions but was harmlessly omitted.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 17 -
 1 file changed, 17 deletions(-)

diff --git a/test-functions b/test-functions
index 24a7527..1fe07ad 100755
--- a/test-functions
+++ b/test-functions
@@ -430,23 +430,6 @@ iterate_tests() {
return "$(( passed < total ))"
 }
 
-print_args() {
-   i=0
-   for arg; do
-   if [ "$((i += 1))" -eq 1 ]; then
-   set --
-   fi
-   case ${arg} in
-   ''|*[[:space:]]*)
-   set -- "$@" "'${arg}'"
-   ;;
-   *)
-   set -- "$@" "${arg}"
-   esac
-   done
-   printf '%s\n' "$*"
-}
-
 printf 'TAP version 13\n'
 
 unset -v dir



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-19 Thread Sam James
commit: 0fbe3a4835e538969c5818ca6bece91de933a8de
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun May 19 14:56:18 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sun May 19 14:56:18 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=0fbe3a48

Micro-optimise _print_args()

Optimise _print_args() by initialising the ord_by array lazily.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 10 ++
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/functions.sh b/functions.sh
index 0f0aa44..f2e5bb5 100644
--- a/functions.sh
+++ b/functions.sh
@@ -535,10 +535,6 @@ _is_visible()
 _print_args() {
awk -v q=\' -f - -- "$@" <<-'EOF'
BEGIN {
-   for (i = 1; i < 32; i++) {
-   char = sprintf("%c", i)
-   ord_by[char] = i
-   }
argc = ARGC
ARGC = 1
for (arg_idx = 1; arg_idx < argc; arg_idx++) {
@@ -548,6 +544,12 @@ _print_args() {
word = q arg q
} else {
# Use $'' quoting per Issue 8
+   if (ord_by["\001"] == "") {
+   for (i = 1; i < 32; i++) {
+   char = sprintf("%c", i)
+   ord_by[char] = i
+   }
+   }
word = "$'"
for (i = 1; i <= length(arg); i++) {
char = substr(arg, i, 1)



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 69c7011e4fc200c569afdd289b447efa0e27b856
Author: Sam James  gentoo  org>
AuthorDate: Sat May 18 16:06:59 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 16:06:59 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=69c7011e

meson.build: prepare for gentoo-functions-1.5

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 7490294..624f5c0 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.4',
+  version: '1.5',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: c0868e07f874b47c9f353c47b3b9ad34e0b5f828
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 15:51:23 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 16:06:45 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=c0868e07

Signify end of options to awk in edo()

This is a fairly important fix because awk doesn't stop looking for
options upon encountering the first non-option argument.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh b/functions.sh
index fdba0c1..db3314a 100644
--- a/functions.sh
+++ b/functions.sh
@@ -95,7 +95,7 @@ ebegin()
 edo() {
# Approximate the effect of ${param@Q} bash expansion.
genfun_cmd=$(
-   awk -v q=\' -f - "$@" <<-'EOF'
+   awk -v q=\' -f - -- "$@" <<-'EOF'
BEGIN {
argc = ARGC
ARGC = 1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 18e702eb8d32695dc503fc272d23e21cad7a6b12
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 16:03:46 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 16:06:45 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=18e702eb

Separate some edo() code into a _print_args() helper function

Further, make test-functions use _print_args() and jettison the inferior
print_args() function that it was previously using.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh   | 33 +++--
 test-functions | 18 +-
 2 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/functions.sh b/functions.sh
index db3314a..462db97 100644
--- a/functions.sh
+++ b/functions.sh
@@ -93,20 +93,7 @@ ebegin()
 # be made to exit by calling the die function.
 #
 edo() {
-   # Approximate the effect of ${param@Q} bash expansion.
-   genfun_cmd=$(
-   awk -v q=\' -f - -- "$@" <<-'EOF'
-   BEGIN {
-   argc = ARGC
-   ARGC = 1
-   for (i = 1; i < argc; i++) {
-   arg = ARGV[i]
-   gsub(q, q "\\" q q, arg)
-   printf("'%s' ", arg)
-   }
-   }
-   EOF
-   )
+   genfun_cmd=$(_print_args "$@")
einfo "${genfun_cmd% }"
"$@" || die "Failed to run command: ${genfun_cmd% }"
 }
@@ -539,6 +526,24 @@ _is_visible()
! case $1 in *[[:graph:]]*) false ;; esac
 }
 
+#
+# Prints the positional parameters in a manner that approximates the behaviour
+# of the ${*@Q} expansion in bash.
+#
+_print_args() {
+   awk -v q=\' -f - -- "$@" <<-'EOF'
+   BEGIN {
+   argc = ARGC
+   ARGC = 1
+   for (i = 1; i < argc; i++) {
+   arg = ARGV[i]
+   gsub(q, q "\\" q q, arg)
+   printf("'%s' ", arg)
+   }
+   }
+   EOF
+}
+
 #
 # Determines whether the terminal on STDIN is able to report its dimensions.
 # Upon success, the number of columns shall be stored in genfun_cols.

diff --git a/test-functions b/test-functions
index 0c0bb45..24a7527 100755
--- a/test-functions
+++ b/test-functions
@@ -41,7 +41,7 @@ test_chdir() {
 
callback() {
shift
-   test_description="chdir $(print_args "$@")"
+   test_description="chdir $(_print_args "$@")"
if [ "$BASH" ]; then
# shellcheck disable=3044
shopt -s cdable_vars
@@ -60,7 +60,7 @@ test_chdir_noop() {
 
callback() {
shift
-   test_description="chdir $(print_args "$@")"
+   test_description="chdir $(_print_args "$@")"
chdir "$@" \
&& test "$PWD" = "$OLDPWD" \
|| { cd - >/dev/null; false; }
@@ -195,7 +195,7 @@ test_is_older_than() {
 
callback() {
shift
-   test_description="is_older_than $(print_args "$@")"
+   test_description="is_older_than $(_print_args "$@")"
is_older_than "$@"
}
 
@@ -221,7 +221,7 @@ test_get_bootparam() {
callback() {
cmdline=$2
shift 2
-   test_description="get_bootparam $(print_args "$@")"
+   test_description="get_bootparam $(_print_args "$@")"
printf '%s\n' "${cmdline}" | get_bootparam "$@"
}
 
@@ -246,7 +246,7 @@ test_esyslog() {
should_log=$2
shift 2
logged=0
-   test_description="esyslog $(print_args "$@")"
+   test_description="esyslog $(_print_args "$@")"
EINFO_LOG=1 esyslog "$@" 2>/dev/null
case $? in
0)
@@ -308,7 +308,7 @@ test_is_identifier() {
 
callback() {
shift
-   test_description="is_identifier $(print_args "$@")"
+   test_description="is_identifier $(_print_args "$@")"
is_identifier "$@"
}
 
@@ -333,7 +333,7 @@ test_is_int() {
 
callback() {
shift
-   test_description="is_int $(print_args "$@")"
+   test_description="is_int $(_print_args "$@")"
is_int "$@"
}
 
@@ -353,7 +353,7 @@ test_is_visible() {
 
callback() {
shift
-   test_description="_is_visible $(print_args "$@")"
+   test_description="_is_visible 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: d35f48614dcdae664b3d20103ba25c747e117990
Author: Sam James  gentoo  org>
AuthorDate: Sat May 18 15:33:52 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 15:34:01 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=d35f4861

meson.build: prepare for gentoo-functions-1.4

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index d868613..7490294 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.3',
+  version: '1.4',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 66c893e7e073006d9b437ed1b5d24644d4a384af
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 15:16:54 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 15:32:16 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=66c893e7

test-functions: Fix spurious edo() test failures

Portage amends PATH in such a way that "ebuild-helper" utilities can be
found. One of those is a die utility. What purpose that can serve is
presently unclear to me. In any case, it was impeding the declaration of
the die() function provided by gentoo-functions. Address it by setting
PATH to a sane, generic value prior to sourcing the functions for
testing.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 test-functions | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/test-functions b/test-functions
index 0dea9f3..0c0bb45 100755
--- a/test-functions
+++ b/test-functions
@@ -451,17 +451,19 @@ printf 'TAP version 13\n'
 
 unset -v dir
 
+# PATH is redefined to prevent ebuild-helpers such as die from interfering.
+export PATH=/sbin:/bin:/usr/sbin:/usr/bin
+export TEST_GENFUNCS=1
+export TZ=UTC
+testnum=0
+rc=0
+
 if ! . ./functions.sh; then
bailout "Couldn't source ./functions.sh"
 fi
 
 assign_tmpdir
 
-export TEST_GENFUNCS=1
-export TZ=UTC
-testnum=0
-rc=0
-
 test_chdir || rc=1
 test_chdir_noop || rc=1
 ( test_ebegin ) || rc=1; testnum=$((testnum + 1))



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 11043c2d2f4efc455877e68380b711464d4b0d59
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 14:46:26 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 15:32:14 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=11043c2d

test-functions: Fix invalid TAP output

Quite rightly, meson complains about the output of the edo tests constituting
invalid TAP. Since only the exit status matters, mute STDOUT for these
particular tests.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 test-functions | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test-functions b/test-functions
index 9414136..0dea9f3 100755
--- a/test-functions
+++ b/test-functions
@@ -118,7 +118,7 @@ test_edo() {
callback() {
shift
test_description="edo $1"
-   ( edo "$1" )
+   ( edo "$1" >/dev/null )
}
 
iterate_tests 3 "$@"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 225875f2041902344e6e9744af17836b1fdfe67c
Author: Sam James  gentoo  org>
AuthorDate: Sat May 18 14:04:17 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 14:04:30 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=225875f2

meson.build: prepare for gentoo-functions-1.3

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 0c564b3..d868613 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.2',
+  version: '1.3',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 61e6f15aab5524331946d5172fcd73d30c3c16f0
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 10:26:00 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 11:28:43 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=61e6f15a

Add an eqawarn() function

In portage's isolated-functions, eqawarn() prints a warning with the
asterisk in dark yellow as opposed to bright yellow. To my mind, this
does not merit yet another cookier-cutter function in gentoo-functions.
So, for now, implement it as an ewarn() wrapper.

Signed-off-by: Kerin Millar  plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh | 9 +
 1 file changed, 9 insertions(+)

diff --git a/functions.sh b/functions.sh
index dd5bb76..2efcf1f 100644
--- a/functions.sh
+++ b/functions.sh
@@ -160,6 +160,15 @@ eoutdent()
_esetdent "$(( ${#genfun_indent} - $1 ))"
 }
 
+#
+# Prints a QA warning message, provided that EINFO_QUIET is false. If printed,
+# the message shall also be conveyed to the esyslog function. For now, this is
+# implemented merely as an ewarn wrapper.
+#
+eqawarn() {
+   ewarn "$@"
+}
+
 #
 # Invokes the logger(1) utility, provided that EINFO_LOG is true. The first
 # parameter shall be taken as a priority level, the second as the message tag,



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: dea9bf08dd93895101331a3d4e307199d6962891
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 13:13:37 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 13:27:54 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=dea9bf08

Add an edo() function

Its implementation is similar to that of the edo eclass.

Signed-off-by: Kerin Millar  plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh   | 25 +
 test-functions | 15 +++
 2 files changed, 40 insertions(+)

diff --git a/functions.sh b/functions.sh
index 2efcf1f..6301c40 100644
--- a/functions.sh
+++ b/functions.sh
@@ -86,6 +86,31 @@ ebegin()
fi
 }
 
+#
+# Takes the positional parameters as the definition of a simple command then
+# prints the command as an informational message with einfo before executing 
it.
+# Should the command fail, a diagnostic message shall be printed and the shell
+# be made to exit by calling the die function.
+#
+edo() {
+   # Approximate the effect of ${param@Q} bash expansion.
+   genfun_cmd=$(
+   awk -v q=\' -f - "$@" <<-'EOF'
+   BEGIN {
+   argc = ARGC
+   ARGC = 1
+   for (i = 1; i < argc; i++) {
+   arg = ARGV[i]
+   gsub(q, q "\\" q q, arg)
+   printf("'%s' ", arg)
+   }
+   }
+   EOF
+   )
+   einfo "${genfun_cmd% }"
+   "$@" || die "Failed to run command: ${genfun_cmd% }"
+}
+
 #
 # Prints an indicator to convey the completion of a given process, provided 
that
 # EINFO_QUIET is false. It is expected that it be paired with an earlier call 
to

diff --git a/test-functions b/test-functions
index d46546a..9414136 100755
--- a/test-functions
+++ b/test-functions
@@ -110,6 +110,20 @@ test_ebegin() {
return "$(( ok ? 0 : 1 ))"
 }
 
+test_edo() {
+   set -- \
+   eq  1  false \
+   eq  0  true
+
+   callback() {
+   shift
+   test_description="edo $1"
+   ( edo "$1" )
+   }
+
+   iterate_tests 3 "$@"
+}
+
 test_is_older_than() {
set -- \
ge  1  N/A   N/A \
@@ -459,6 +473,7 @@ test_is_int || rc=1
 test_is_visible || rc=1
 test_yesno || rc=1
 test_die || rc=1
+test_edo || rc=1
 
 cleanup_tmpdir
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 895bd87dc035ace26aefb460ee0a192da6c9521e
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 10:20:06 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 10:20:06 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=895bd87d

test-functions: Use test rather than [ to placate shellcheck

Also, fix an accidental - though ultimately harmless - case of SC2027.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-functions b/test-functions
index 4360eb2..d46546a 100755
--- a/test-functions
+++ b/test-functions
@@ -77,7 +77,7 @@ test_die() {
eq  255  255
 
callback() {
-   test_description="( exit "$2" ); die"
+   test_description="( exit $2 ); die"
( exit "$2" )
stderr=$(die "$2" 2>&1)
retval=$?
@@ -404,7 +404,7 @@ iterate_tests() {
done
eval "${code}"
retval=$?
-   if [ "${retval}" -"$1" "$2" ]; then
+   if test "${retval}" -"$1" "$2"; then
passed=$((passed + 1))
else
printf 'not '



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 973fe5e1968e4ee6bcffa78b59807eb4f81e5823
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 13:15:36 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 13:28:00 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=973fe5e1

Reposition the declaration of _has_dumb_terminal()

So as to maintain an alphabetical order.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/functions.sh b/functions.sh
index 6301c40..fdba0c1 100644
--- a/functions.sh
+++ b/functions.sh
@@ -505,14 +505,6 @@ _esetdent()
genfun_indent=$(printf "%${1}s" '')
 }
 
-#
-# Determines whether the terminal is a dumb one.
-#
-_has_dumb_terminal()
-{
-   ! case ${TERM} in *dumb*) false ;; esac
-}
-
 #
 # Tries to determine whether the terminal supports ECMA-48 SGR color sequences.
 #
@@ -531,6 +523,14 @@ _has_color_terminal()
fi
 }
 
+#
+# Determines whether the terminal is a dumb one.
+#
+_has_dumb_terminal()
+{
+   ! case ${TERM} in *dumb*) false ;; esac
+}
+
 #
 # Determines whether the first parameter contains any visible characters.
 #



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-18 Thread Sam James
commit: 730f6a6388342cdbe2a80fe78cfce30b8c952cab
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat May 18 10:04:13 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Sat May 18 10:14:34 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=730f6a63

test-functions: Fix a spurious test failure

The second-to-last commit added a test case for the die() function but
also made it so that the exit status of each test is always compared to
the expected value with the -eq operator. This turns out be
insufficiently inflexible. For example, cd may return 2 in bash but 1 in
another shell.

Address this by requiring for each operand slice defined by a callback
function to begin with an operator name, and by having the
iterate_tests() function use the operator at the time of invoking the
test utility.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 373 +
 1 file changed, 188 insertions(+), 185 deletions(-)

diff --git a/test-functions b/test-functions
index e087a3c..4360eb2 100755
--- a/test-functions
+++ b/test-functions
@@ -26,14 +26,14 @@ cleanup_tmpdir() {
 
 test_chdir() {
set -- \
-   1  grandchild  \
-   1 var  \
-   0  -L  \
-   0  -p  \
-   0  -e  \
-   0  -@  \
-   0   -  \
-   0   child
+   ge  1  grandchild  \
+   ge  1 var  \
+   eq  0  -L  \
+   eq  0  -p  \
+   eq  0  -e  \
+   eq  0  -@  \
+   eq  0   -  \
+   eq  0   child
 
if ! mkdir -p -- -L -p -e -@ - child child/grandchild; then
bailout "Couldn't set up all test directories"
@@ -51,11 +51,12 @@ test_chdir() {
&& cd - >/dev/null
}
 
-   iterate_tests 2 "$@"
+   iterate_tests 3 "$@"
 }
 
 test_chdir_noop() {
-   set -- 0 ''
+   set -- \
+   eq  0  ''
 
callback() {
shift
@@ -65,15 +66,15 @@ test_chdir_noop() {
|| { cd - >/dev/null; false; }
}
 
-   iterate_tests 2 "$@"
+   iterate_tests 3 "$@"
 }
 
 test_die() {
set -- \
-   1 0 \
-   2 2 \
-   126 126 \
-   255 255
+   eq10 \
+   eq22 \
+   eq  126  126 \
+   eq  255  255
 
callback() {
test_description="( exit "$2" ); die"
@@ -87,7 +88,7 @@ test_die() {
fi
}
 
-   iterate_tests 2 "$@"
+   iterate_tests 3 "$@"
 }
 
 test_ebegin() {
@@ -111,60 +112,60 @@ test_ebegin() {
 
 test_is_older_than() {
set -- \
-   1  N/A   N/A \
-   1  newer N/A \
-   1  newer-empty   N/A \
-   1  newer/fileN/A \
-   1  non-existent  N/A \
-   0  newer newer \
-   1  newer newer-empty \
-   0  newer newer/file \
-   1  newer non-existent \
-   1  newer older \
-   1  newer older-empty \
-   1  newer older/file \
-   0  newer-empty   newer \
-   1  newer-empty   newer-empty \
-   0  newer-empty   newer/file \
-   1  newer-empty   non-existent \
-   1  newer-empty   older \
-   1  newer-empty   older-empty \
-   1  newer-empty   older/file \
-   1  newer/filenewer \
-   1  newer/filenewer-empty \
-   1  newer/filenewer/file \
-   1  newer/filenon-existent \
-   1  newer/fileolder \
-   1  newer/fileolder-empty \
-   1  newer/fileolder/file \
-   0  non-existent  newer \
-   0  non-existent  newer-empty \
-   0  non-existent  newer/file \
-   1  non-existent  non-existent \
-   0  non-existent  older \
-   0  non-existent  older-empty \
-   0  non-existent  older/file \
-   0  older newer \
-   0  older newer-empty \
-   0  older newer/file \
-   1  older non-existent \
-   0  older older \
-   1  older older-empty \
-   0  older older/file \
-   0  older-empty   newer \
-   0  older-empty   newer-empty \
-   0  older-empty   newer/file \
-   1  older-empty   non-existent \
-   0  older-empty   older \
-   1  older-empty   older-empty \
-   0  older-empty   older/file \
-

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 57f643e81c8bd8ab5d428aa5770c3a8097a456a2
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri May 17 01:21:15 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 02:17:13 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=57f643e8

Document variables influencing runtime behaviour

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 20 +++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/functions.sh b/functions.sh
index b0170f9..dd5bb76 100644
--- a/functions.sh
+++ b/functions.sh
@@ -3,12 +3,30 @@
 # shellcheck shell=sh disable=3043
 
 # This file contains a series of function declarations followed by some
-# initialization code. Functions intended for internal use shall be prefixed
+# initialisation code. Functions intended for internal use shall be prefixed
 # with an  and shall not be considered as being a part of the 
public
 # API. With the exception of those declared by the local builtin, all variables
 # intended for internal use shall be prefixed with "genfun_" to indicate so,
 # and to reduce the probability of name space conflicts.
 
+# The following variables affect initialisation and/or function behaviour.
+
+# BASH  : whether bash-specific features may be employed
+# BASHPID   : potentially used by _update_columns() to detect subshells
+# COLUMNS   : potentially used by _update_columns() to get the column count
+# EERROR_QUIET  : whether error printing functions should be silenced
+# EINFO_LOG : whether printing functions should call esyslog()
+# EINFO_QUIET   : whether info message printing functions should be silenced
+# EINFO_VERBOSE : whether v-prefixed functions should do anything
+# IFS   : multiple message operands are joined by its first character
+# INSIDE_EMACS  : whether to work around an emacs-specific bug in _eend()
+# NO_COLOR  : whether colored output should be suppressed
+# RC_NOCOLOR: like NO_COLOR but deprecated
+# TEST_GENFUNCS : used for testing the behaviour of get_bootparam()
+# TERM  : may influence message formatting and whether color is used
+
+
+
 #
 # A safe wrapper for the cd builtin. To run cd "$dir" is problematic because:
 #



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: d3e39fba9cafdc78a92363702bcc117bf6c30a9e
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 02:32:10 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 02:36:54 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=d3e39fba

Declare eerror(), einfo() and ewarn() dynamically

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 30 +-
 1 file changed, 9 insertions(+), 21 deletions(-)

diff --git a/functions.sh b/functions.sh
index dfbbe31..60fa2fa 100644
--- a/functions.sh
+++ b/functions.sh
@@ -56,12 +56,16 @@ eend()
 }
 
 #
-#show an error message (with a newline) and log it
+#   Declare the eerror, einfo and ewarn functions. These wrap errorn, einfon 
and
+#   ewarnn respectively, the difference being that a newline is appended.
 #
-eerror()
-{
-   eerrorn "${*}${genfun_newline}"
-}
+for _ in eerror einfo ewarn; do
+   eval "
+   $_ () {
+   ${_}n \"\${*}\${genfun_newline}\"
+   }
+   "
+done
 
 #
 #show an error message (without a newline) and log it
@@ -86,14 +90,6 @@ eindent()
_esetdent "$(( ${#genfun_indent} + $1 ))"
 }
 
-#
-#show an informative message (with a newline)
-#
-einfo()
-{
-   einfon "${*}${genfun_newline}"
-}
-
 #
 #show an informative message (without a newline)
 #
@@ -138,14 +134,6 @@ esyslog()
fi
 }
 
-#
-#show a warning message (with a newline) and log it
-#
-ewarn()
-{
-   ewarnn "${*}${genfun_newline}"
-}
-
 #
 #show a warning message (without a newline) and log it
 #



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 0dd8364c03c6f8737150ee4f146ddeeec57efee9
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 05:11:16 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 05:11:16 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=0dd8364c

Have esyslog() check that EINFO_LOG is true instead of non-empty

It makes no sense for EINFO_LOG to be exceptional.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/functions.sh b/functions.sh
index 050a1d8..bd97f7e 100644
--- a/functions.sh
+++ b/functions.sh
@@ -122,8 +122,7 @@ eoutdent()
 }
 
 #
-# Invokes logger(1) to log the given message, provided that EINFO_LOG is set as
-# a non-empty value.
+# Invokes logger(1) to log the given message, provided that EINFO_LOG is true.
 #
 esyslog()
 {
@@ -132,7 +131,7 @@ esyslog()
if [ "$#" -lt 2 ]; then
ewarn "Too few arguments for esyslog (got $#, expected at least 
2)"
return 1
-   elif [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
+   elif yesno "${EINFO_LOG}" && hash logger 2>/dev/null; then
pri=$1
tag=$2
shift 2



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 2ba6c4f1f86ff15cf11fdb515a22989a3d16f5ab
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 12:32:50 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 01:40:20 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=2ba6c4f1

Rename _has_monochrome_terminal() to _has_color_terminal()

This also inverts the sense of its exit status but it feels a little
more natural this way. Moreover, the behaviour now matches the
description of the function.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/functions.sh b/functions.sh
index e2af8cf..e494765 100644
--- a/functions.sh
+++ b/functions.sh
@@ -444,18 +444,18 @@ _has_dumb_terminal()
 #
 # Tries to determine whether the terminal supports ECMA-48 SGR color sequences.
 #
-_has_monochrome_terminal()
+_has_color_terminal()
 {
local colors
 
# The tput(1) invocation is not portable, though ncurses suffices. In
# this day and age, it is exceedingly unlikely that it will be needed.
if _has_dumb_terminal; then
-   true
+   false
elif colors=$(tput colors 2>/dev/null) && is_int "${colors}"; then
-   test "${colors}" -eq -1
+   test "${colors}" -gt 0
else
-   false
+   true
fi
 }
 
@@ -545,7 +545,7 @@ else
done
 fi
 
-if _has_monochrome_terminal || yesno "${RC_NOCOLOR}"; then
+if ! _has_color_terminal || yesno "${RC_NOCOLOR}"; then
unset -v BAD BRACKET GOOD HILITE NORMAL WARN
 else
# Define some ECMA-48 SGR sequences for color support. These variables



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 13e1d297905a7be552af2f4407da188c242e1efe
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 06:56:30 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 01:40:20 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=13e1d297

Tweak the comment style for yesno()

Following William's style because it remains the prevailing one.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/functions.sh b/functions.sh
index 1f547d0..9424660 100644
--- a/functions.sh
+++ b/functions.sh
@@ -307,6 +307,7 @@ vewend()
fi
 }
 
+#
 # Determines whether the first parameter is truthy. The values taken to be true
 # are "yes", "true", "on" and "1", whereas their opposites are taken to be
 # false. The empty string is also taken to be false. All pattern matching is



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 83a380418dd6b669e633074def33edc370978b01
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 02:15:15 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 02:34:10 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=83a38041

Remove some superfluous boolean variable declarations

Remove the superfluous declarations of EINFO_QUIET, EINFO_VERBOSE,
RC_NOCOLOR and genfun_indent. No attempt has been made to appease the
nounset cultists because it was already the case that gentoo-functions
be incompatible with it. Should push come to shove, nounset could be
accommodated but such a requirement ought then to be attended to in its
own right.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 14 +++---
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/functions.sh b/functions.sh
index 6647c67..dfbbe31 100644
--- a/functions.sh
+++ b/functions.sh
@@ -488,14 +488,7 @@ _update_tty_level()
 
 # This is the main script, please add all functions above this point!
 # shellcheck disable=2034
-RC_GOT_FUNCTIONS="yes"
-
-# Dont output to stdout?
-EINFO_QUIET="${EINFO_QUIET:-no}"
-EINFO_VERBOSE="${EINFO_VERBOSE:-no}"
-
-# Set the initial value for e-message indentation.
-genfun_indent=
+RC_GOT_FUNCTIONS=yes
 
 # Assign the LF ('\n') character for later expansion. POSIX Issue 8 permits
 # $'\n' but it may take years for it to be commonly implemented.
@@ -513,13 +506,12 @@ fi
 # Should we use color?
 if [ -n "${NO_COLOR}" ]; then
# See https://no-color.org/.
-   RC_NOCOLOR="yes"
+   RC_NOCOLOR=yes
 else
-   RC_NOCOLOR="${RC_NOCOLOR:-no}"
for _ in "$@"; do
case $_ in
--nocolor|--nocolour|-C)
-   RC_NOCOLOR="yes"
+   RC_NOCOLOR=yes
break
esac
done



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: e3f08cc9af0f8310ada80af54fbea9e69f70e39c
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 05:14:03 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 05:14:03 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=e3f08cc9

Don't call is_int() from _esetdent()

There is no need.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh b/functions.sh
index bd97f7e..57f6aa2 100644
--- a/functions.sh
+++ b/functions.sh
@@ -423,7 +423,7 @@ _eprint()
 #
 _esetdent()
 {
-   if ! is_int "$1" || [ "$1" -lt 0 ]; then
+   if [ "$1" -lt 0 ]; then
set -- 0
fi
genfun_indent=$(printf "%${1}s" '')



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 36d3b72507a6142d9453a55dbcc6ca038768f3f5
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 12:22:51 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 01:40:20 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=36d3b725

More comment improvements

Render the descriptions of eerrorn(), esyslog(), _update_columns() and
the v-prefixed functions more accurate.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 13 -
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/functions.sh b/functions.sh
index 9424660..e2af8cf 100644
--- a/functions.sh
+++ b/functions.sh
@@ -74,8 +74,9 @@ for _ in eerror einfo ewarn; do
 done
 
 #
-# Prints an error message without appending a newline, while also conveying it
-# to the esyslog function.
+# Prints an error message without appending a newline, provided that
+# EERROR_QUIET is false. If printed, the message shall also be conveyed to the
+# esyslog function.
 #
 eerrorn()
 {
@@ -122,7 +123,9 @@ eoutdent()
 }
 
 #
-# Invokes logger(1) to log the given message, provided that EINFO_LOG is true.
+# Invokes the logger(1) utility, provided that EINFO_LOG is true. The first
+# parameter shall be taken as a priority level, the second as the message tag,
+# and the remaining parameters as the message to be logged.
 #
 esyslog()
 {
@@ -273,7 +276,7 @@ is_older_than()
 #
 # Declare the vebegin, veerror, veindent, veinfo, veinfon, veoutdent and vewarn
 # functions. These differ from their non-v-prefixed counterparts in that they
-# only print where EINFO_VERBOSE is true.
+# only have an effect where EINFO_VERBOSE is true.
 #
 for _ in vebegin veerror veindent veinfo veinfon veoutdent vewarn; do
eval "
@@ -466,7 +469,7 @@ _is_visible()
 
 #
 # Determines whether the terminal on STDIN is able to report its dimensions.
-# Upon success, the number of columns shall be stored.
+# Upon success, the number of columns shall be stored in genfun_cols.
 #
 _update_columns()
 {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 6761163b5c8667a1a111683f5bf72e5dff4e604e
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 12:55:08 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 01:40:20 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6761163b

Add a die() function

For now, it will only be defined if no utility, function or alias
already exists by that name. Like the exit builtin, it is able to
preserve the value of $? but will never exit with a status of 0. For
example, the following will preserve the exit status of failing_command.

failing_command || die "gadzooks"

Whereas the following will ignore the value of $? - being that it is 0 -
and instead exit with a status of 1.

if prevailing_command; then
die "fiddlesticks"
fi

Signed-off-by: Kerin Millar  plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh   | 20 
 test-functions | 39 +--
 2 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/functions.sh b/functions.sh
index e494765..b0170f9 100644
--- a/functions.sh
+++ b/functions.sh
@@ -30,6 +30,26 @@ chdir()
CDPATH= cd -- "$@"
 }
 
+#
+# Prints a diagnostic message prefixed with the basename of the running script
+# before exiting. It shall preserve the value of $? as it was at the time of
+# invocation unless its value was 0, in which case the exit status shall be 1.
+#
+if ! command -v die >/dev/null; then
+   die()
+   {
+   case $? in
+   0)
+   genfun_status=1
+   ;;
+   *)
+   genfun_status=$?
+   esac
+   printf '%s: %s\n' "${0##*/}" "$*" >&2
+   exit "${genfun_status}"
+   }
+fi
+
 #
 # Prints a message indicating the onset of a given process, provided that
 # EINFO_QUIET is false. It is expected that eend eventually be called, so as to

diff --git a/test-functions b/test-functions
index a2d15b4..e087a3c 100755
--- a/test-functions
+++ b/test-functions
@@ -68,6 +68,28 @@ test_chdir_noop() {
iterate_tests 2 "$@"
 }
 
+test_die() {
+   set -- \
+   1 0 \
+   2 2 \
+   126 126 \
+   255 255
+
+   callback() {
+   test_description="( exit "$2" ); die"
+   ( exit "$2" )
+   stderr=$(die "$2" 2>&1)
+   retval=$?
+   if [ "${stderr}" = "test-functions: $2" ]; then
+   return "${retval}"
+   else
+   return 1
+   fi
+   }
+
+   iterate_tests 2 "$@"
+}
+
 test_ebegin() {
_eprint() {
shift
@@ -380,24 +402,12 @@ iterate_tests() {
fi
done
eval "${code}"
-   case $? in
-   0)
-   test "$?" -eq "$1"
-   ;;
-   *)
-   test "$?" -ge "$1"
-   esac
-   if [ "$?" -eq 0 ]; then
+   if [ "$?" -eq "$1" ]; then
passed=$((passed + 1))
else
printf 'not '
fi
-   if [ "$1" -eq 0 ]; then
-   expected=$1
-   else
-   expected=">=$1"
-   fi
-   printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" 
"${test_description}" "${expected}"
+   printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" 
"${test_description}" "$1"
shift "${slice_width}"
done
return "$(( passed < total ))"
@@ -445,6 +455,7 @@ test_is_identifier || rc=1
 test_is_int || rc=1
 test_is_visible || rc=1
 test_yesno || rc=1
+test_die || rc=1
 
 cleanup_tmpdir
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 1bd941e3de386fd04e23b23407d213868920dbd3
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 06:20:02 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri May 17 01:40:19 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1bd941e3

Make _eend() much faster in bash

This is accomplished by instead invoking the true binary and taking a
reading of the COLUMNS variable. However, this is only done in
situations where it can be expected to be reliable. Even as of
bash 5.3-alpha, the checkwinsize feature does not work reliably in a
subshell.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 28 
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/functions.sh b/functions.sh
index 57f6aa2..1f547d0 100644
--- a/functions.sh
+++ b/functions.sh
@@ -469,14 +469,20 @@ _is_visible()
 #
 _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
+   # Command substitutions are rather slow in bash. Using the COLUMNS
+   # variable helps but checkwinsize won't work properly in subshells.
+   # shellcheck disable=3028,3044
+   if [ "$$" = "${BASHPID}" ] && shopt -q checkwinsize; then
+   "${genfun_bin_true}"
+   set -- 0 "${COLUMNS}"
+   else
+   # The following use of stty(1) is portable as of POSIX Issue 8.
+   genfun_ifs=${IFS}
+   IFS=' '
+   # shellcheck disable=2046
+   set -- $(stty size 2>/dev/null)
+   IFS=${genfun_ifs}
+   fi
[ "$#" -eq 2 ] && is_int "$2" && [ "$2" -gt 0 ] && genfun_cols=$2
 }
 
@@ -515,6 +521,12 @@ else
genfun_offset=0
 fi
 
+# Store the path to the true binary. It is potentially used by _update_columns.
+if [ "${BASH}" ]; then
+   # shellcheck disable=3045
+   genfun_bin_true=$(type -P true)
+fi
+
 # Determine whether the use of color is to be wilfully avoided.
 if [ -n "${NO_COLOR}" ]; then
# See https://no-color.org/.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 4f6bbc44f0a0264e0a1d1d034dad863a69ce1f07
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 05:04:32 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 05:08:50 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=4f6bbc44

Improve the comments

Also, add comments for several functions that did not have any.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 125 +++
 1 file changed, 75 insertions(+), 50 deletions(-)

diff --git a/functions.sh b/functions.sh
index 60fa2fa..050a1d8 100644
--- a/functions.sh
+++ b/functions.sh
@@ -10,12 +10,12 @@
 # and to reduce the probability of name space conflicts.
 
 #
-#   A safe wrapper for the cd builtin. To run cd "$dir" is problematic because:
+# 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
+# - it may consider its operand as an option
+# - it will search CDPATH for an operand not beginning with ./, ../ or /
+# - it will switch to OLDPWD if the operand is -
+# - cdable_vars causes bash to treat the operand as a potential variable name
 #
 chdir()
 {
@@ -31,7 +31,9 @@ chdir()
 }
 
 #
-#show a message indicating the start of a process
+# Prints a message indicating the onset of a given process, provided that
+# EINFO_QUIET is false. It is expected that eend eventually be called, so as to
+# indicate whether the process completed successfully or not.
 #
 ebegin()
 {
@@ -47,8 +49,12 @@ ebegin()
 }
 
 #
-#indicate the completion of process
-#if error, show errstr via eerror
+# Prints an indicator to convey the completion of a given process, provided 
that
+# EINFO_QUIET is false. It is expected that it be paired with an earlier call 
to
+# ebegin. The first parameter shall be taken as an exit status value, making it
+# possible to distinguish between success and failure. If unspecified, it shall
+# default to 0. The remaining parameters, if any, shall be taken as a 
diagnostic
+# message to convey as an error where the exit status is not 0.
 #
 eend()
 {
@@ -56,8 +62,8 @@ eend()
 }
 
 #
-#   Declare the eerror, einfo and ewarn functions. These wrap errorn, einfon 
and
-#   ewarnn respectively, the difference being that a newline is appended.
+# Declare the eerror, einfo and ewarn functions. These wrap errorn, einfon and
+# ewarnn respectively, the difference being that a newline is appended.
 #
 for _ in eerror einfo ewarn; do
eval "
@@ -68,7 +74,8 @@ for _ in eerror einfo ewarn; do
 done
 
 #
-#show an error message (without a newline) and log it
+# Prints an error message without appending a newline, while also conveying it
+# to the esyslog function.
 #
 eerrorn()
 {
@@ -80,7 +87,8 @@ eerrorn()
 }
 
 #
-#increase the indent used for e-commands.
+# Decreases the level of indentation used by various printing functions. If no
+# numerical parameter is given, or if it is negative, increase by 2 spaces.
 #
 eindent()
 {
@@ -91,7 +99,8 @@ eindent()
 }
 
 #
-#show an informative message (without a newline)
+# Prints an informational message without appending a newline, provided that
+# EINFO_QUIET is false.
 #
 einfon()
 {
@@ -101,7 +110,8 @@ einfon()
 }
 
 #
-#decrease the indent used for e-commands.
+# Decreases the level of indentation used by various printing functions. If no
+# numerical parameter is given, or if it is negative, decrease by 2 spaces.
 #
 eoutdent()
 {
@@ -112,7 +122,8 @@ eoutdent()
 }
 
 #
-#use the system logger to log a message
+# Invokes logger(1) to log the given message, provided that EINFO_LOG is set as
+# a non-empty value.
 #
 esyslog()
 {
@@ -135,7 +146,9 @@ esyslog()
 }
 
 #
-#show a warning message (without a newline) and log it
+# Prints a warning message without appending a newline, provided that
+# EINFO_QUIET is false. If printed, the message shall also be conveyed to the
+# esyslog function.
 #
 ewarnn()
 {
@@ -146,8 +159,8 @@ ewarnn()
 }
 
 #
-#indicate the completion of process
-#if error, show errstr via ewarn
+# This behaves as the eend function does, except that the given diagnostic
+# message shall be presented as a warning rather than an error.
 #
 ewend()
 {
@@ -155,9 +168,8 @@ ewend()
 }
 
 #
-#   return 0 if gentoo=param was passed to the kernel
-#
-#   EXAMPLE:  if get_bootparam "nodevfs" ; then 
+# Determines whether the kernel cmdline contains the specified parameter as a
+# component of a comma-separated list specified in the format of gentoo=.
 #
 get_bootparam()
 (
@@ -191,7 +203,7 @@ get_bootparam()
 )
 
 #
-#   Determine whether the first operand is a valid identifier (variable name).
+# Determines whether the first parameter is 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: 50ce35639679a07286dff5a409df6d44a6d9166c
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 01:38:31 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 01:38:31 2024 +
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 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-16 Thread Sam James
commit: e4eab7040c9b36f5ebfc9eb0880cfd39735e4a85
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu May 16 01:51:13 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Thu May 16 01:53:05 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=e4eab704

Declare some of the v-prefixed functions dynamically

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh | 62 ++--
 1 file changed, 14 insertions(+), 48 deletions(-)

diff --git a/functions.sh b/functions.sh
index 8561eb3..6647c67 100644
--- a/functions.sh
+++ b/functions.sh
@@ -272,12 +272,20 @@ is_older_than()
read -r _
 }
 
-vebegin()
-{
-   if yesno "${EINFO_VERBOSE}"; then
-   ebegin "$@"
-   fi
-}
+#
+#   Declare the vebegin, veerror, veindent, veinfo, veinfon, veoutdent and
+#   vewarn functions. These differ from their non-v-prefixed counterparts in
+#   that they only have an effect where EINFO_VERBOSE is set as a truthy value.
+#
+for _ in vebegin veerror veindent veinfo veinfon veoutdent vewarn; do
+   eval "
+   $_ () {
+   if yesno \"\${EINFO_VERBOSE}\"; then
+   ${_#v} \"\$@\"
+   fi
+   }
+   "
+done
 
 veend()
 {
@@ -290,48 +298,6 @@ veend()
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



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-15 Thread Sam James
commit: 68c3ed9035165e4daa902421fa9de4589d631b3b
Author: Sam James  gentoo  org>
AuthorDate: Wed May 15 10:28:15 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Wed May 15 10:28:31 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=68c3ed90

meson.build: prepare for gentoo-functions-1.2

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index de4abf0..0c564b3 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.1',
+  version: '1.2',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-15 Thread Sam James
commit: f3e254127ebad7c8774081e3834653effe1966d0
Author: Kerin Millar  plushkava  net>
AuthorDate: Tue May 14 08:33:09 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Wed May 15 10:28:30 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f3e25412

Have _eend() remember the cursor position before printing

The act of printing a LF character after the indicator moves the cursor
back to the row beneath - as intended - but also results in the cursor
being positioned at the first column, even though it may have been
elsewhere initially. Address this by using the DECSC sequence to save
the cursor position prior to printing the indicator, then the DECRC
sequence to restore it afterwards. My testing shows no measurable
performance impact.

Consider the following script as a test case.

#!/bin/sh
. ./functions.sh
ebegin Testing
einfon more output
eend 0

Below is a depicted invocation which clearly demonstrates the beneficial
effect of this change.

$ /.testcase; printf done
 * Testing ...[ ok ]
 * more outputdone$ â–‰

Whereas, previously, the outcome would have been as shown below.

$ /.testcase; printf done
 * Testing ...[ ok ]
done$ â–‰ output

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/functions.sh b/functions.sh
index 6d77f59..7bd4b11 100644
--- a/functions.sh
+++ b/functions.sh
@@ -227,10 +227,10 @@ _eend()
fi
 
if [ "${genfun_tty}" -eq 2 ]; then
-   # Move the cursor up by one line with CUU before positioning it
-   # horizontally with CHA. Both are formal ECMA-48 CSI sequences.
-   # Print the indicator afterwards.
-   printf '\033[1A\033[%dG %s\n' "$(( genfun_cols - 6 + 
genfun_offset ))" "${msg}"
+   # Save the cursor position with DECSC, move it up by one line
+   # with CUU, position it horizontally with CHA, print the
+   # indicator, then restore the cursor position with DECRC.
+   printf '\0337\033[1A\033[%dG %s\0338' "$(( genfun_cols - 6 + 
genfun_offset ))" "${msg}"
else
# The standard output does not refer to a sufficiently capable
# terminal. Print only the indicator.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: c75ed22e9360f44e202581527269f830f8585d7b
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:18:06 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:18:06 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=c75ed22e

meson.build: prepare for gentoo-functions-1.1

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index b89d1be..de4abf0 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '1.0',
+  version: '1.1',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 12d78db029707bc459f8ddded827dc46078fb8ad
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:15:24 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:15:24 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=12d78db0

meson.build: fix tests

Fixes: ea4fb2a1e34900635d0db7bd1d8e5e1bea06ced0
Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 05dba85..b89d1be 100644
--- a/meson.build
+++ b/meson.build
@@ -29,7 +29,7 @@ do_tests = get_option('tests')
 if do_tests
   test(
 'test-functions', files('test-functions'),
-workdir : meson.current_build_dir(),
+workdir : meson.current_source_dir(),
 protocol : 'tap',
 verbose : true
   )



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: ea4fb2a1e34900635d0db7bd1d8e5e1bea06ced0
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:10:24 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:10:45 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=ea4fb2a1

Cleanup functions.sh.in template

No longer needed since bfd5710de9b14712c4a621259b951bd10489d0c0.

Signed-off-by: Sam James  gentoo.org>

 functions.sh.in => functions.sh |  0
 meson.build | 14 ++
 2 files changed, 2 insertions(+), 12 deletions(-)

diff --git a/functions.sh.in b/functions.sh
similarity index 100%
rename from functions.sh.in
rename to functions.sh

diff --git a/meson.build b/meson.build
index 6846d40..f18c5f8 100644
--- a/meson.build
+++ b/meson.build
@@ -8,18 +8,8 @@ project(
   ]
 )
 
-conf_data = configuration_data()
-conf_data.set('version', meson.project_version())
-# For now, we can't really use libexec, given everyone hardcodes /lib/gentoo.
-# We might be able to install some symlinks to get around this though?
-conf_data.set('GENTOO_LIBEXEC_DIR', get_option('prefix') / 'lib' / 'gentoo')
-
-# TODO: Cleanup now that functions.sh no longer uses ecma48-cpr
-configure_file(
-  input: 'functions.sh.in',
-  output: 'functions.sh',
-  configuration: conf_data,
-  install: true,
+install_data(
+  'functions.sh',
   install_dir: 'lib/gentoo'
 )
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: f1dd9a109f67ff77ce8a312cb737ab68a3f3a3fb
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:11:53 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:12:25 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f1dd9a10

Drop ecma48-cpr

Obsolete as of bfd5710de9b14712c4a621259b951bd10489d0c0. A neat tool, but
it ended up not being a good fit for the reasons described in that commit.

Signed-off-by: Sam James  gentoo.org>

 ecma48-cpr.c | 239 ---
 meson.build  |   7 --
 2 files changed, 246 deletions(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
deleted file mode 100644
index d910f6f..000
--- a/ecma48-cpr.c
+++ /dev/null
@@ -1,239 +0,0 @@
-/*
- * ecma48-cpr.c
- * Treat STDIN as a tty and report the cursor position using the CPR sequence.
- *
- * Originally distributed as wsize.c by Stephen J. Friedl .
- * Repurposed for gentoo-functions by Kerin F. Millar .
- * This software is in the public domain.
- */
-
-#define _POSIX_C_SOURCE 200809L
-
-#define PROGRAM "ecma48-cpr"
-#define READ_TIMEOUT_NS 25000
-#define BUFSIZE 100
-#define MAX_LOOPS 20
-
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-
-static struct termios save_tty;
-static bool is_timed_out = false;
-static bool is_tty_saved = false;
-
-static void cleanup(void);
-static void die(char const * const errmsg);
-static void on_signal(int const signo);
-
-#ifndef __APPLE__
-static timer_t init_timer(void);
-#endif
-
-int
-main(void) {
-   /*
-* Establish that STDIN is a terminal.
-*/
-   if (! isatty(STDIN_FILENO)) {
-   die("cannot determine the cursor position because stdin is not 
a tty");
-   }
-
-   /*
-* Duplicate STDIN to a new file descriptor before reopening it as a
-* writeable stream.
-*/
-   int fd = dup(STDIN_FILENO);
-   FILE *tty;
-   if (fd < 0) {
-   die("failed to dup stdin");
-   } else {
-   tty = fdopen(fd, "w");
-   if (tty == NULL) {
-   die("failed to re-open the tty for writing");
-   }
-   }
-
-   /*
-* Save the current terminal settings.
-*/
-   if (tcgetattr(STDIN_FILENO, _tty) != 0) {
-   die("failed to obtain the current terminal settings");
-   } else {
-   is_tty_saved = true;
-   }
-
-   /*
-* Duplicate the current terminal settings for modification.
-*/
-   struct termios new_tty = save_tty;
-
-   /*
-* Turn off ECHO, so that the response from the terminal isn't printed.
-* Also, the terminal must be operating in its noncanonical mode,
-* thereby ensuring that its input is always immediately available,
-* with no processing having been performed.
-*/
-   new_tty.c_lflag &= ~(ECHO | ICANON | IXON);
-
-   /*
-* Set an interbyte timeout of 1 decisecond. The timer is started only
-* after the first byte is read, so read(2) will block until then.
-*/
-   new_tty.c_cc[VMIN]  = 1;
-   new_tty.c_cc[VTIME] = 1;
-
-   /*
-* Prepare to catch our signals. We treat both an interrupt and a
-* depleted timer as essentially the same thing: fatal errors.
-*/
-   struct sigaction act = {
-   .sa_handler = on_signal,
-   .sa_flags = 0
-   };
-   sigemptyset(_mask);
-   sigaction(SIGALRM, , NULL);
-
-
-   /*
-* Ignore the signals most likely to interrupt the process from hereon.
-*/
-   signal(SIGINT, SIG_IGN);
-   signal(SIGTERM, SIG_IGN);
-   signal(SIGTSTP, SIG_IGN);
-
-   /*
-* Try to apply the new terminal settings.
-*/
-   if (tcsetattr(STDIN_FILENO, TCSAFLUSH, _tty) != 0) {
-   die("failed to modify the terminal settings");
-   } else if (fprintf(tty, "\033[6n") != 4) {
-   die("failed to write the CPR sequence to the terminal");
-   } else if (fclose(tty) != 0) {
-   die("failed to flush the stream after writing the CPR 
sequence");
-   }
-
-   /*
-* A timeout is required, just in case read(2) proves unable to read an
-* initial byte, otherwise causing the program to hang.
-*/
-#ifdef __APPLE__
-   alarm(1);
-#else
-   timer_t timerid = init_timer();
-#endif
-
-   /*
-* Read up to (sizeof ibuf - 1) bytes of input in total. Upon each
-* successful read, scan the input buffer for a valid ECMA4-8 CPR
-* response. Abort if no such response is found within MAX_LOOPS
-* iterations.
-*/
-   char ibuf[BUFSIZE];
-   char const * const imax = ibuf + sizeof ibuf - 1;
-   char *iptr = ibuf;
-   int maxloops = MAX_LOOPS;
-   int row = -1;
-   int col = -1;
-   ssize_t nr;
-

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 7c0ade278efd23419813824453284d525a544e28
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:07:06 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:07:40 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=7c0ade27

meson.build: add TODO wrt cleaning up functions.sh template

Signed-off-by: Sam James  gentoo.org>

 meson.build | 1 +
 1 file changed, 1 insertion(+)

diff --git a/meson.build b/meson.build
index 5346aee..6846d40 100644
--- a/meson.build
+++ b/meson.build
@@ -14,6 +14,7 @@ conf_data.set('version', meson.project_version())
 # We might be able to install some symlinks to get around this though?
 conf_data.set('GENTOO_LIBEXEC_DIR', get_option('prefix') / 'lib' / 'gentoo')
 
+# TODO: Cleanup now that functions.sh no longer uses ecma48-cpr
 configure_file(
   input: 'functions.sh.in',
   output: 'functions.sh',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 6f8204800db6d66cc006cea972c6936c6675d8cc
Author: Sam James  gentoo  org>
AuthorDate: Tue May 14 00:07:53 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Tue May 14 00:08:25 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6f820480

functions.sh.in: fix typo

Kerin fixed this in a later push but I'd already pulled it in by that
point.

Signed-off-by: Sam James  gentoo.org>

 functions.sh.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh.in b/functions.sh.in
index e40f1ff..6d77f59 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -499,7 +499,7 @@ _update_tty_level()
# terminal is detected, the value shall be 0. If a dumb terminal is
# detected, the value shall be 1. If a smart terminal is detected, the
# value shall be 2. For a terminal to be considered as smart, it must be
-   # able to successfuly reports its dimensions.
+   # able to successfully report its dimensions.
if [ ! -t 0 ]; then
genfun_tty=0
elif _has_dumb_terminal || ! _update_columns; then



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: bfd5710de9b14712c4a621259b951bd10489d0c0
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 13 23:18:02 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 13 23:18:02 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=bfd5710d

Simplify _eprint() and _eend() behaviour markedly

Following almost a year of usage and testing, I now consider my
experiments in smart terminal handling to be both a success and a
failure by equal measure. I deem them to be a successful in so far as
the code functions as intended, and that there have been several
positive reports from those intrepid enough to unmask
gentoo-functions-1.0. However, I also deem them to be a failure in so
far as there is a noticeable impact upon performance. In particular,
repeated calls to ebegin() and eend() are handled rather slowly. As
such, this commit greatly simplifies the code whilst retaining the most
compelling traits of the work that has been performed. The exact changes
are described herewith.

The _eprint() function shall now only check whether STDOUT refers to a
tty. In the event that it does, the message may be printed in a
colourised fashion. Otherwise, it shall be printed plainly. No longer
will the terminal be asked to remember the position of the cursor (using
DECSC), nor shall any variables be used for the purpose of keeping
state.

The _eend() function shall no longer attempt to restore the prior
position of the cursor (using DECRC), nor attempt to determine the
present position of the cursor (using ecma48-cpr). Instead, the
behaviour is reduced to that of checking whether STDOUT refers to a
smart terminal. In the event that it does, the success/failure
identifier shall always be printed on the preceding line, right-aligned.
Otherwise, it shall be printed plainly on the present line. It remains
the case that the terminal will be asked to report its dimensions, so
that _eend() can adapt to any adjustments made to the width of the
terminal emulator.

As a consequence of these changes, the _update_cursor_coords() and
_ecma48_cpr() functions are rendered obsolete and have been removed.
Additionally, the "genfun_cols", "genfun_y" and "genfun_is_pending_lf"
variables are rendered obsolete and have been removed. Finally, the
_update_winsize() function has been renamed to _update_columns().

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 194 +++-
 test-functions  |  34 --
 2 files changed, 22 insertions(+), 206 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index c97219b..e40f1ff 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -14,81 +14,15 @@
 #
 _eprint()
 {
-   local color msg
+   local color
+
color=$1
shift
 
-   # Check whether STDOUT is a terminal, and how capable it is.
-   _update_tty_level <&1
-
-   if [ "${genfun_tty}" -eq 2 ]; then
-   # If the cursor is not situated on column 1, print a LF
-   # character. The assumption here is that the last call may have
-   # been via ebegin, or any of the other printing functions that
-   # pass a message without a trailing LF.
-   if [ "${genfun_x}" -ne 1 ]; then
-   printf '\n'
-   fi
-   elif [ "${genfun_is_pending_lf}" -eq 1 ]; then
-   # We are about to print to a dumb terminal or something other
-   # than a terminal. Print a LF character because the last printed
-   # message did not end with one. This technique is not ideal.
-   # For one thing, it can be thwarted by having called a printing
-   # function from a subshell or a shell launched by a subprocess,
-   # because the change to the flag variable would be lost. For
-   # another, it's possible for the user of the library to be
-   # directing STDOUT/STDERR to different places between calls.
-   # Such weaknesses cannot be addressed without some form of IPC.
-   printf '\n'
-   fi
-
-   msg=$*
-   if [ "${genfun_tty}" -lt 2 ]; then
-   if [ "${genfun_tty}" -eq 1 ]; then
-   # Print but do not attempt to save the cursor position.
-   printf ' %s*%s %s%s' "${color}" "${NORMAL}" 
"${genfun_indent}" "${msg}"
-   else
-   printf ' * %s%s' "${genfun_indent}" "${msg}"
-   fi
-   if _ends_with_newline "${msg}"; then
-   genfun_is_pending_lf=0
-   else
-   # Record the fact that a LF character is pending.
-   genfun_is_pending_lf=1
-   fi
-   elif ! _ends_with_newline "${msg}"; then
-   # Print the message before saving the cursor position with the
-   # DECSC sequence. This is a private 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 1fd9f819fbb2493c47c2166cd234246c1f8f85a7
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 13 22:14:05 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 13 22:19:16 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1fd9f819

Don't check for Emacs every time _eend() is called

For it is an edge case and it is not clear that it is worth it. Instead,
check at the time of sourcing. The underlying bug really ought to be
addressed upstream.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 21 ++---
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 555d1d1..c97219b 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -336,24 +336,15 @@ _eend()
# Calculate the column at which the indicator may be printed.
indent=$(( genfun_cols - genfun_x - 6 ))
 
-   # In Emacs, M-x term opens an "eterm-color" terminal, whose
-   # implementation of the CHA (ECMA-48 CSI) sequence suffers from
-   # an off-by-one error.
-   if [ "${INSIDE_EMACS}" ] && [ "${TERM}" = "eterm-color" ]; then
-   offset=-1
-   else
-   offset=0
-   fi
-
# Determine whether the cursor needs to be repositioned.
if [ "${indent}" -gt 0 ]; then
# Use CHA (ECMA-48 CSI) to move the cursor to the right.
-   printf '\033[%dG' "$(( genfun_x + indent + offset ))"
+   printf '\033[%dG' "$(( genfun_x + indent + 
genfun_offset ))"
elif [ "${indent}" -lt 0 ]; then
# The indent is negative, meaning that there is not
# enough room. Arrange for the indicator to be printed
# on the next line instead.
-   printf '\n\033[%dG' "$(( genfun_cols - 6 + offset ))"
+   printf '\n\033[%dG' "$(( genfun_cols - 6 + 
genfun_offset ))"
fi
 
# Finally, print the indicator.
@@ -697,6 +688,14 @@ genfun_newline='
 # Whether the last printed message is pending a concluding LF character.
 genfun_is_pending_lf=0
 
+# In Emacs, M-x term opens an "eterm-color" terminal, whose implementation of
+# the CHA (ECMA-48 CSI) sequence suffers from an off-by-one error.
+if [ "${INSIDE_EMACS}" ] && [ "${TERM}" = "eterm-color" ]; then
+   genfun_offset=-1
+else
+   genfun_offset=0
+fi
+
 # Should we use color?
 if [ -n "${NO_COLOR}" ]; then
# See https://no-color.org/.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 520388fda047e61a04802faf86a3d8d850cc58dc
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 13 21:56:09 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 13 22:19:14 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=520388fd

Render _update_winsize() immune to the prevailing value of IFS

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 5 +
 1 file changed, 5 insertions(+)

diff --git a/functions.sh.in b/functions.sh.in
index d1df518..555d1d1 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -638,12 +638,17 @@ _update_tty_level()
 
 _update_winsize()
 {
+   local ifs
+
# The following use of stty(1) is portable as of POSIX Issue 8. It would
# be beneficial to leverage the checkwinsize option in bash but the
# implementation is buggy. Given that Chet has agreed to investigate,
# it may eventually become possible to support it.
+   ifs=$IFS
+   IFS=' '
# shellcheck disable=2046
set -- $(stty size 2>/dev/null)
+   IFS=$ifs
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2" && [ "$1" -gt 0 ] && [ 
"$2" -gt 0 ]; then
genfun_rows=$1
genfun_cols=$2



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: bc8ced13c5a987a9f1b0ac6d425d00d580d09519
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 13 21:34:22 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 13 21:34:22 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=bc8ced13

ecma48-cpr: Use designated initialisers for legibility

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 27 +++
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 5a0f936..d910f6f 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -92,10 +92,11 @@ main(void) {
 * Prepare to catch our signals. We treat both an interrupt and a
 * depleted timer as essentially the same thing: fatal errors.
 */
-   struct sigaction act;
-   act.sa_handler = on_signal;
+   struct sigaction act = {
+   .sa_handler = on_signal,
+   .sa_flags = 0
+   };
sigemptyset(_mask);
-   act.sa_flags = 0;
sigaction(SIGALRM, , NULL);
 
 
@@ -190,19 +191,21 @@ main(void) {
 #ifndef __APPLE__
 static timer_t
 init_timer(void) {
-   struct itimerspec timer;
-   struct sigevent event;
timer_t timerid;
-   event.sigev_notify = SIGEV_SIGNAL;
-   event.sigev_signo = SIGALRM;
-   event.sigev_value.sival_ptr = 
+   struct sigevent event = {
+   .sigev_value.sival_ptr = ,
+   .sigev_notify = SIGEV_SIGNAL,
+   .sigev_signo = SIGALRM
+   };
if (timer_create(CLOCK_REALTIME, , ) == -1) {
die("failed to create a per-process timer");
} else {
-   timer.it_value.tv_sec = 0;
-   timer.it_value.tv_nsec = READ_TIMEOUT_NS;
-   timer.it_interval.tv_sec = 0;
-   timer.it_interval.tv_nsec = 0;
+   struct itimerspec timer = {
+   .it_value.tv_nsec = READ_TIMEOUT_NS,
+   .it_interval.tv_nsec = 0,
+   .it_interval.tv_sec = 0,
+   .it_value.tv_sec = 0
+   };
if (timer_settime(timerid, 0, , NULL) == -1) {
die("failed to configure the per-process timer");
}



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-05-13 Thread Sam James
commit: 7fcc1c137a79587b26db03079ee659bcbac8c533
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon May 13 21:37:02 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Mon May 13 21:52:45 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=7fcc1c13

Unify the brace style

I tend to define functions in accordance with the K brace style. In
deference to the traditions of the project, employ the Allman style
instead.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 33 ++---
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index d2baf5d..d1df518 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -12,7 +12,8 @@
 #
 #Called by ebegin, eerrorn, einfon, and ewarnn.
 #
-_eprint() {
+_eprint()
+{
local color msg
color=$1
shift
@@ -532,7 +533,8 @@ is_older_than()
 #   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() {
+is_int()
+{
set -- "${1#-}"
case $1 in
''|*[!0123456789]*)
@@ -554,7 +556,8 @@ is_int() {
 #   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() {
+chdir()
+{
if [ "$BASH" ]; then
# shellcheck disable=3044
shopt -u cdable_vars
@@ -569,7 +572,8 @@ chdir() {
 #
 #   Determine whether the first operand contains any visible characters.
 #
-_is_visible() {
+_is_visible()
+{
! case $1 in *[[:graph:]]*) false ;; esac
 }
 
@@ -584,11 +588,13 @@ is_identifier()
esac
 )
 
-_has_dumb_terminal() {
+_has_dumb_terminal()
+{
! case ${TERM} in *dumb*) false ;; esac
 }
 
-_has_monochrome_terminal() {
+_has_monochrome_terminal()
+{
local colors
 
# The tput(1) invocation is not portable, though ncurses suffices. In
@@ -602,12 +608,14 @@ _has_monochrome_terminal() {
fi
 }
 
-_ends_with_newline() {
+_ends_with_newline()
+{
test "${genfun_newline}" \
&& ! case $1 in *"${genfun_newline}") false ;; esac
 }
 
-_update_tty_level() {
+_update_tty_level()
+{
# Grade the capability of the terminal attached to STDIN (if any) on a
# scale of 0 to 2, assigning the resulting value to genfun_tty. If no
# terminal is detected, the value shall be 0. If a dumb terminal is
@@ -628,7 +636,8 @@ _update_tty_level() {
fi
 }
 
-_update_winsize() {
+_update_winsize()
+{
# The following use of stty(1) is portable as of POSIX Issue 8. It would
# be beneficial to leverage the checkwinsize option in bash but the
# implementation is buggy. Given that Chet has agreed to investigate,
@@ -645,7 +654,8 @@ _update_winsize() {
fi
 }
 
-_update_cursor_coords() {
+_update_cursor_coords()
+{
# shellcheck disable=2046
set -- $(_ecma48_cpr)
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
@@ -658,7 +668,8 @@ _update_cursor_coords() {
fi
 }
 
-_ecma48_cpr() {
+_ecma48_cpr()
+{
@GENTOO_LIBEXEC_DIR@/ecma48-cpr
 }
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2024-02-16 Thread Sam James
commit: a72102fe08752fefc8243855285affd93e5ff015
Author: Sam James  gentoo  org>
AuthorDate: Fri Feb 16 21:35:32 2024 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Feb 16 21:35:32 2024 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=a72102fe

meson.build: crank version to 1.0

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 2557168..5346aee 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 project(
   'gentoo-functions', 'c',
-  version: '0.20',
+  version: '1.0',
   license: 'GPL-2.0-only',
   default_options : [
 'warning_level=2',



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: a930fa7a97375463d02cde833f6f31c5aef479e9
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun Jun 11 08:49:19 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 08:51:42 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=a930fa7a

ecma48-cpr: Ignore SIGINT, SIGTERM and SIGTSTP

There seems little sense in honouring these signals at the point that
the terminal settings are about to be altered and the CPR sequence
written.

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 7d32c63..5a0f936 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -99,6 +99,13 @@ main(void) {
sigaction(SIGALRM, , NULL);
 
 
+   /*
+* Ignore the signals most likely to interrupt the process from hereon.
+*/
+   signal(SIGINT, SIG_IGN);
+   signal(SIGTERM, SIG_IGN);
+   signal(SIGTSTP, SIG_IGN);
+
/*
 * Try to apply the new terminal settings.
 */



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: 075761b394201951e2948be8730f8cb8e7fc8122
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun Jun 11 11:26:16 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 11:36:13 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=075761b3

Account for the buggy CHA implementation in Emacs

In Emacs, M-x term opens an "eterm-color" terminal, whose
implementation of the CHA (ECMA-48 CSI) sequence suffers from an
off-by-one error. Work around it. Note that Eterm does not have this
same bug.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 6f1ee75..d2baf5d 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -335,15 +335,24 @@ _eend()
# Calculate the column at which the indicator may be printed.
indent=$(( genfun_cols - genfun_x - 6 ))
 
+   # In Emacs, M-x term opens an "eterm-color" terminal, whose
+   # implementation of the CHA (ECMA-48 CSI) sequence suffers from
+   # an off-by-one error.
+   if [ "${INSIDE_EMACS}" ] && [ "${TERM}" = "eterm-color" ]; then
+   offset=-1
+   else
+   offset=0
+   fi
+
# Determine whether the cursor needs to be repositioned.
if [ "${indent}" -gt 0 ]; then
# Use CHA (ECMA-48 CSI) to move the cursor to the right.
-   printf '\033[%dG' "$(( genfun_x + indent ))"
+   printf '\033[%dG' "$(( genfun_x + indent + offset ))"
elif [ "${indent}" -lt 0 ]; then
# The indent is negative, meaning that there is not
# enough room. Arrange for the indicator to be printed
# on the next line instead.
-   printf '\n\033[%dG' "$(( genfun_cols - 6 ))"
+   printf '\n\033[%dG' "$(( genfun_cols - 6 + offset ))"
fi
 
# Finally, print the indicator.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: 6f6440c538efb2edbcdfe298521252b6510a1a80
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun Jun 11 08:57:47 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 09:18:32 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6f6440c5

Detect PTYs that aren't sufficiently functional to be considered as smart

In this case, the offender is the PTY that is set up by the
_create_pty_or_pipe() utility function in portage, which uses python's
pty module. I transpires that its behaviour is not exactly equivalent to
whichever controlling terminal is in effect at the time that emerge(1)
is executed, if any. As a consequence, the ecma48-cpr utility times out
while waiting for a response to the CPR (ECMA-48 CSI) sequence.

I can discern no option other than to refrain from considering the
terminal as being "smart" in this case. This is accomplished by
recognising that stty(1) reports "0 0" when asked to check its size.
These are nonsensical readings that would never be produced for a
properly functioning, non-dumb terminal. In fact, this check used to be
in place but was removed, simply because I forgot how important it is.

Processes running under portage that call into gentoo-functions will
still be able to produce coloured messages, but all the advantages of
the smart terminal handling path will be obviated. This is unfortunate,
but I have no intention of returning to the original, broken behaviour
of gentoo-functions, which was to assume that the characteristics of the
terminal found at the time of sourcing hold forever, and that STDOUT
and STDERR will never refer to anything other than that same terminal.

I would add that script(1) does not exhibit the same issue.

$ tty
/dev/pts/0
$ script -c 'tty; stty size <&1; /lib/gentoo/ecma48-cpr <&1' /dev/null
Script started, output log file is '/dev/null'.
/dev/pts/11
34 144
34 1
Script done.

In other words, it is portage that is broken and which should be fixed.

Signed-off-by: Kerin Millar  plushkava.net>
Reported-by: Sam James  gentoo.org>

 functions.sh.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh.in b/functions.sh.in
index 9be9978..6f1ee75 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -626,7 +626,7 @@ _update_winsize() {
# it may eventually become possible to support it.
# shellcheck disable=2046
set -- $(stty size 2>/dev/null)
-   if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
+   if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2" && [ "$1" -gt 0 ] && [ 
"$2" -gt 0 ]; then
genfun_rows=$1
genfun_cols=$2
else



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: 8d257696be7383ff8533e50b893f93365bea9756
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 23:22:57 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 08:32:19 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=8d257696

ecma48-cpr: Don't assign to new_tty twice

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 813621e..4710b39 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -72,7 +72,6 @@ main(void) {
 * Duplicate the current terminal settings for modification.
 */
struct termios new_tty = save_tty;
-   new_tty = save_tty;
 
/*
 * Turn off ECHO, so that the response from the terminal isn't printed.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: a608aa2b90299f74dbced4a31822aff7e4dc30ff
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 23:21:58 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 08:32:16 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=a608aa2b

ecma48-cpr: Flush the input queue with tcsetattr(3) instead

Doing so saves an additional syscall and waits for the output queue to
be drained, which is no bad thing.

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 144c4ea..813621e 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -93,10 +93,8 @@ main(void) {
/*
 * Try to apply the new terminal settings.
 */
-   if (tcsetattr(STDIN_FILENO, TCSANOW, _tty) != 0) {
+   if (tcsetattr(STDIN_FILENO, TCSAFLUSH, _tty) != 0) {
die("failed to modify the terminal settings");
-   } else if (tcflush(STDIN_FILENO, TCIFLUSH) != 0) {
-   die("failed to flush the terminal's input queue");
} else if (fprintf(tty, "\033[6n") != 4) {
die("failed to write the CPR sequence to the terminal");
} else if (fclose(tty) != 0) {
@@ -214,7 +212,7 @@ static void
 cleanup(void) {
bool const is_saved = is_tty_saved;
if (is_saved) {
-   tcsetattr(STDIN_FILENO, TCSANOW, _tty);
+   tcsetattr(STDIN_FILENO, TCSAFLUSH, _tty);
is_tty_saved = false;
}
 }



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: 87ee7f2a56d712246eb8d630e3282295ab92bbbe
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 23:36:15 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 08:32:22 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=87ee7f2a

ecma48-cpr: Set up the signal handler before writing to the terminal

The idea being to narrow the window of time during which the terminal
is in its raw mode as much as is possible.

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 21 +++--
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 4710b39..f397ab7 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -89,6 +89,17 @@ main(void) {
new_tty.c_cc[VMIN]  = 1;
new_tty.c_cc[VTIME] = 1;
 
+   /*
+* Prepare to catch our signals. We treat both an interrupt and a
+* depleted timer as essentially the same thing: fatal errors.
+*/
+   struct sigaction act;
+   act.sa_handler = on_signal;
+   sigemptyset(_mask);
+   act.sa_flags = 0;
+   sigaction(SIGALRM, , NULL);
+
+
/*
 * Try to apply the new terminal settings.
 */
@@ -100,16 +111,6 @@ main(void) {
die("failed to flush the stream after writing the CPR 
sequence");
}
 
-   /*
-* Prepare to catch our signals. We treat both an interrupt and a
-* depleted timer as essentially the same thing: fatal errors.
-*/
-   struct sigaction act;
-   act.sa_handler = on_signal;
-   sigemptyset(_mask);
-   act.sa_flags = 0;
-   sigaction(SIGALRM, , NULL);
-
/*
 * A timeout is required, just in case read(2) proves unable to read an
 * initial byte, otherwise causing the program to hang.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: b6fe181d26b6819ca0086a2fc9b664ee3f84a555
Author: Kerin Millar  plushkava  net>
AuthorDate: Sun Jun 11 08:43:35 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Jun 11 08:43:35 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=b6fe181d

ecma48-cpr: Disable flow control in the terminal

While at it, don't bother disabling ECHOE, ECHOK and ECHONL, for they do
nothing in the case that ICANON has been disabled.

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index f397ab7..7d32c63 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -79,8 +79,7 @@ main(void) {
 * thereby ensuring that its input is always immediately available,
 * with no processing having been performed.
 */
-   new_tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
-   new_tty.c_lflag &= ~(ICANON);
+   new_tty.c_lflag &= ~(ECHO | ICANON | IXON);
 
/*
 * Set an interbyte timeout of 1 decisecond. The timer is started only



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-11 Thread Sam James
commit: 7f27dfa0ca0969837a9a40df49281ec7a2dae035
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 07:40:16 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 07:42:07 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=7f27dfa0

test-functions: Add a test for the ebegin() function

The test determines whether ebegin() appends at least one 
character to the message (following the ASCII ellipsis).

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 20 
 1 file changed, 20 insertions(+)

diff --git a/test-functions b/test-functions
index aed6620..513deb3 100755
--- a/test-functions
+++ b/test-functions
@@ -68,6 +68,25 @@ test_chdir_noop() {
iterate_tests 2 "$@"
 }
 
+test_ebegin() {
+   _eprint() {
+   shift
+   _ends_with_newline "$*"
+   ok=$(( $? == 0 ))
+   }
+
+   ok=0
+   set -- "message"
+   ebegin "$1"
+
+   if [ "${ok}" -eq 0 ]; then
+   printf 'not '
+   fi
+   printf 'ok %d - ebegin %s (expecting terminating newline)\n' 
"$((testnum + 1))" "$1"
+
+   return "$(( ok ? 0 : 1 ))"
+}
+
 test_is_older_than() {
set -- \
1  N/A   N/A \
@@ -451,6 +470,7 @@ rc=0
 
 test_chdir || rc=1
 test_chdir_noop || rc=1
+( test_ebegin ) || rc=1; testnum=$((testnum + 1))
 test_is_older_than || rc=1
 test_get_bootparam || rc=1
 test_esyslog || rc=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-10 Thread Sam James
commit: c3c6ba532055f01cde10882b67aecda00abd01e1
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 07:10:48 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 07:15:56 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=c3c6ba53

Fix the order of the arguments used to compose the CUP sequence

The CUP (ECMA-48 CSI) sequence expects for the row to come first, and
the column second. I could have just swapped the arguments but, instead,
I have adjusted the surrounding code so as to strictly adhere to this
convention (one that is also observed by stty size). This should
eliminate the possibility of any such mistake being made in the future.

Signed-off-by: Kerin Millar  plushkava.net>
Fixes: 20bc15b5b1009149c4a3d531911d3c219dc55f3a

 functions.sh.in | 12 ++--
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 0c3e7ba..9be9978 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -270,7 +270,7 @@ _eend()
fi
 
# Stash the last known terminal dimensions, if any.
-   set -- "${genfun_cols}" "${genfun_rows}"
+   set -- "${genfun_rows}" "${genfun_cols}"
 
# Check whether STDOUT is a terminal, and how capable it is.
_update_tty_level <&1
@@ -304,9 +304,9 @@ _eend()
# Provided that the terminal has not since been resized, it may
# be possible to write the indicator on the same row as the
# last printed message, even if it were LF-terminated.
-   if [ "${genfun_cols}" -eq "$1" ] && [ "${genfun_rows}" -eq "$2" 
]; then
+   if [ "${genfun_rows}" -eq "$1" ] && [ "${genfun_cols}" -eq "$2" 
]; then
# Stash the current position of the cursor.
-   set -- "${genfun_x}" "${genfun_y}"
+   set -- "${genfun_y}" "${genfun_x}"
 
# Using the DECRC sequence, restore the cursor position
# to wherever it was just after the last message was
@@ -324,11 +324,11 @@ _eend()
# preceding row. If it did, assume that scrolling has
# occurred since printing the last message and move the
# cursor back to where it was with CUP (ECMA-48 CSI).
-   offset=$(( $2 - genfun_y ))
+   offset=$(( $1 - genfun_y ))
if [ "${offset}" -lt 0 ] || [ "${offset}" -gt 1 ]; then
printf '\033[%d;%dH' "$1" "$2"
-   genfun_x=$1
-   genfun_y=$2
+   genfun_y=$1
+   genfun_x=$2
fi
fi
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-10 Thread Sam James
commit: 3f62d58dbb7fcb4ddaf14f7216110eebe9bd0b3e
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 07:03:07 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 07:15:49 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=3f62d58d

Ensure that ebegin() appends a newline to the final message

The ebegin() function was recently modified to strip the trailing ,
if any, from the given message. Later, it was modified to strip all trailing
 characters. The purpose of doing so is to ensure that there is
no accidental line breaking betwixt the message and the ASCII ellipsis
that is appended. However, the function should then have been appending a
single  to the composed message before handing it off to _eprint().
Make it so.

Signed-off-by: Kerin Millar  plushkava.net>
Fixes: 1947f0ed81f3b95a7e10a8a5a707776948f8a487

 functions.sh.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh.in b/functions.sh.in
index 76f1ad4..0c3e7ba 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -243,7 +243,7 @@ ebegin()
while _ends_with_newline "${msg}"; do
msg=${msg%"${genfun_newline}"}
done
-   _eprint "${GOOD}" "${msg} ..."
+   _eprint "${GOOD}" "${msg} ...${genfun_newline}"
fi
 }
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-10 Thread Sam James
commit: 02d2824b4daeedd4084da53ebf3633a8b5508a92
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 05:40:35 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 05:40:35 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=02d2824b

test-functions: Really fix non-conforming TAP output

The previous "fix" was an experimental patch that was not yet intended
for inclusion. This supplemental commit does as little as is necessary
to render the test suite properly functional under meson, given that
meson.build has been configured to expect the use of the TAP protocol.
It also configures meson to be verbose while executing the test suite.

As far as I can gather, the TAP support in meson is weak. Subtests do
not appear to be supported. For that reason, I have dropped the TAP
version from 14 to 13. While meson recognises "# SKIP" as a directive,
it does not report the reason that may follow the text. Another issue is
that no controlling terminal is present under meson. Therefore,
test_update_cursor_coords() is rendered useless, for it will always be
skipped. I have left the test in for the time being, in the hope that
there may yet be a solution.

In any case, this should be enough to render the - ebuild both
testable and usable.

Signed-off-by: Kerin Millar  plushkava.net>

 meson.build|  3 ++-
 test-functions | 21 +++--
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/meson.build b/meson.build
index aa56907..2557168 100644
--- a/meson.build
+++ b/meson.build
@@ -46,6 +46,7 @@ if do_tests
   test(
 'test-functions', files('test-functions'),
 workdir : meson.current_build_dir(),
-protocol : 'tap'
+protocol : 'tap',
+verbose : true
   )
 endif

diff --git a/test-functions b/test-functions
index 5e04c5f..aed6620 100755
--- a/test-functions
+++ b/test-functions
@@ -305,9 +305,8 @@ test_is_visible() {
 }
 
 test_update_cursor_coords() {
-   printf '%d..%d\n' "${tap_i}" "${tap_i}"
skip() {
-   print 'ok %d # SKIP\n' "${tap_i}"
+   printf 'ok %d - _update_cursor_coords # SKIP\n' "$((testnum += 
1))"
}
if _has_dumb_terminal; then
skip
@@ -323,10 +322,10 @@ test_update_cursor_coords() {
# The terminal isn't smart
skip
elif ! _update_cursor_coords <>/dev/tty; then
-   printf 'not ok %d - _update_cursor_coords\n' "${tap_i}"
+   printf 'not ok %d - _update_cursor_coords\n' "$((testnum += 1))"
false
else
-   printf 'ok %d - _update_cursor_coords (x = %d, y = %d)\n' 
"${tap_i}" "${genfun_x}" "${genfun_y}"
+   printf 'ok %d - _update_cursor_coords (x = %d, y = %d)\n' 
"$((testnum += 1))" "${genfun_x}" "${genfun_y}"
fi
 }
 
@@ -374,7 +373,6 @@ iterate_tests() {
shift
 
total=$(( $# / slice_width ))
-   printf '%d..%d\n' "${tap_i}" "$((tap_i + total - 1))"
passed=0
i=0
while [ "$((i += 1))" -le "${total}" ]; do
@@ -405,9 +403,8 @@ iterate_tests() {
else
expected=">=$1"
fi
-   printf 'ok %d - %s (expecting %s)\n' "${tap_i}" 
"${test_description}" "${expected}"
+   printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" 
"${test_description}" "${expected}"
shift "${slice_width}"
-   tap_i=$((tap_i + 1))
done
return "$(( passed < total ))"
 }
@@ -429,7 +426,7 @@ print_args() {
printf '%s\n' "$*"
 }
 
-printf 'TAP version 14\n'
+printf 'TAP version 13\n'
 
 unset -v dir
 
@@ -449,9 +446,9 @@ assign_tmpdir
 
 export TEST_GENFUNCS=1
 export TZ=UTC
-tap_i=1
-
+testnum=0
 rc=0
+
 test_chdir || rc=1
 test_chdir_noop || rc=1
 test_is_older_than || rc=1
@@ -462,5 +459,9 @@ test_is_int || rc=1
 test_is_visible || rc=1
 test_update_cursor_coords || rc=1
 test_yesno || rc=1
+
 cleanup_tmpdir
+
+printf '1..%d\n' "${testnum}"
+
 exit "${rc}"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 6c59a01b2e2454cd286cc9d701cb6648d428106e
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 21:38:13 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 21:38:13 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6c59a01b

test-functions: Do not silence ewarn() while testing is_older_than()

I changed my mind about this. It's probable that more diagnostic
messages will be added in the future and it may then be useful to
observe them in the case that the test suite fails.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test-functions b/test-functions
index 0d7b1ab..0fd998d 100755
--- a/test-functions
+++ b/test-functions
@@ -424,7 +424,7 @@ export TZ=UTC
 rc=0
 test_chdir || rc=1
 test_chdir_noop || rc=1
-( ewarn() { true; }; test_is_older_than ) || rc=1
+test_is_older_than || rc=1
 test_get_bootparam || rc=1
 test_esyslog || rc=1
 test_is_identifier || rc=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 3f455919da23065ac6cbff9fa86791ae67798654
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 01:50:15 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 01:50:15 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=3f455919

meson.build: Use the TAP protocol for running the tests

Signed-off-by: Kerin Millar  plushkava.net>

 meson.build | 1 +
 1 file changed, 1 insertion(+)

diff --git a/meson.build b/meson.build
index 8996d95..aa56907 100644
--- a/meson.build
+++ b/meson.build
@@ -46,5 +46,6 @@ if do_tests
   test(
 'test-functions', files('test-functions'),
 workdir : meson.current_build_dir(),
+protocol : 'tap'
   )
 endif



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 6dea8b6b960b3370461a1b42c05f101bf42c89a4
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 22:44:10 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 22:47:49 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6dea8b6b

Address a slew of shellcheck warnings

False-positives galore. It's getting out of hand for test-functions, so
I disabled several more tests in its global scope. It was correct to
point out that "${PWD}" should be quoted, however. Bash doesn't mind but
other sh implementations might.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in |  4 +++-
 test-functions  | 18 ++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index d4becf8..76f1ad4 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -547,11 +547,13 @@ is_int() {
 #
 chdir() {
if [ "$BASH" ]; then
+   # shellcheck disable=3044
shopt -u cdable_vars
fi
if [ "$1" = - ]; then
set -- ./-
fi
+   # shellcheck disable=1007,2164
CDPATH= cd -- "$@"
 }
 
@@ -648,7 +650,7 @@ _update_cursor_coords() {
 }
 
 _ecma48_cpr() {
-   @GENTOO_LIBEXEC_DIR@/ecma48-cpr "$@"
+   @GENTOO_LIBEXEC_DIR@/ecma48-cpr
 }
 
 # This is the main script, please add all functions above this point!

diff --git a/test-functions b/test-functions
index 9f87e98..34dc8b8 100755
--- a/test-functions
+++ b/test-functions
@@ -1,5 +1,5 @@
 #!/bin/sh
-# shellcheck disable=2015
+# shellcheck disable=2015,2154,2164,2181,2317
 
 # Requires mktemp(1), which is not a standard utility, but is commonly
 # available. The implementations provided by GNU coreutils, busybox and toybox
@@ -43,9 +43,10 @@ test_chdir() {
shift
test_description="chdir $(print_args "$@")"
if [ "$BASH" ]; then
+   # shellcheck disable=3044
shopt -s cdable_vars
fi
-   CDPATH=child var=$CDPATH chdir "$@" \
+   CDPATH=child var=child chdir "$@" \
&& test "$PWD" != "$OLDPWD" \
&& cd - >/dev/null
}
@@ -221,8 +222,8 @@ test_is_identifier() {
1   [Z   \
1  '`a'  \
1  '`Z'  \
-   1   {a   \
-   1   {Z   \
+   1  '{a'  \
+   1  '{Z'  \
1  '|a'  \
1  '|Z'  \
1   a/   \
@@ -233,8 +234,8 @@ test_is_identifier() {
1   Z[   \
1  'a`'  \
1  'Z`'  \
-   1   a{   \
-   1   Z{   \
+   1  'a{'  \
+   1  'Z{'  \
1  'a|'  \
1  'Z|'  \
0a   \
@@ -351,6 +352,7 @@ test_yesno() {
1  '_"; set -- yes # code injection' \
0  truthful_nameref
 
+   # shellcheck disable=2034
truthful_nameref=yes
 
callback() {
@@ -433,8 +435,8 @@ fi
 # ecma48-cpr utility will not yet have been installed. Account for that by
 # redeclaring its shim function.
 if [ "${EBUILD_PHASE}" = test ]; then
-   export BUILD_DIR=${PWD}
-   _ecma48_cpr() { "${BUILD_DIR}"/ecma48-cpr "$@"; }
+   export BUILD_DIR="${PWD}"
+   _ecma48_cpr() { "${BUILD_DIR}"/ecma48-cpr; }
 fi
 
 assign_tmpdir



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: be49b00f58db2c62e1a908507e80d11a1bc64611
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Jun 10 01:45:16 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sat Jun 10 01:48:42 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=be49b00f

test-functions: Fix non-conforming TAP 14 output

Given that there are no subtests, it isn't permissible to duplicate any
of the test numbers. Also, the test_update_cursor_coords() function was
not correctly conveying the case where the test is skipped.

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 29 ++---
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/test-functions b/test-functions
index 34dc8b8..5e04c5f 100755
--- a/test-functions
+++ b/test-functions
@@ -305,23 +305,28 @@ test_is_visible() {
 }
 
 test_update_cursor_coords() {
+   printf '%d..%d\n' "${tap_i}" "${tap_i}"
+   skip() {
+   print 'ok %d # SKIP\n' "${tap_i}"
+   }
if _has_dumb_terminal; then
-   printf '1..0 # TERM defines the terminal as being dumb\n'
+   skip
elif ! ctty=$(ps -p "$$" -otty= 2>/dev/null) || [ -z "${ctty}" ]; then
-   printf '1..0 # the ps(1) implementation is defective or 
non-compliant\n'
+   # The ps(1) implementation is defective or non-compliant
+   skip
elif [ "${ctty}" = "?" ]; then
-   printf '1..0 # no controlling terminal is available\n'
+   # No controlling terminal is available
+   skip
elif [ ! -e /dev/tty ]; then
-   printf '1..0 # the /dev/tty character device is missing\n'
+   skip
elif ! _update_winsize /dev/tty; then
-   printf '1..1\n'
-   printf 'not ok 1 - ecm48-cpr failed\n'
+   printf 'not ok %d - _update_cursor_coords\n' "${tap_i}"
false
else
-   printf '1..1\n'
-   printf 'ok 1 - ecm48-cpr succeeded (x = %d, y = %d)\n' 
"${genfun_x}" "${genfun_y}"
+   printf 'ok %d - _update_cursor_coords (x = %d, y = %d)\n' 
"${tap_i}" "${genfun_x}" "${genfun_y}"
fi
 }
 
@@ -369,7 +374,7 @@ iterate_tests() {
shift
 
total=$(( $# / slice_width ))
-   printf '1..%d\n' "${total}"
+   printf '%d..%d\n' "${tap_i}" "$((tap_i + total - 1))"
passed=0
i=0
while [ "$((i += 1))" -le "${total}" ]; do
@@ -400,8 +405,9 @@ iterate_tests() {
else
expected=">=$1"
fi
-   printf 'ok %d - %s (expecting %s)\n' "${i}" 
"${test_description}" "${expected}"
+   printf 'ok %d - %s (expecting %s)\n' "${tap_i}" 
"${test_description}" "${expected}"
shift "${slice_width}"
+   tap_i=$((tap_i + 1))
done
return "$(( passed < total ))"
 }
@@ -443,6 +449,7 @@ assign_tmpdir
 
 export TEST_GENFUNCS=1
 export TZ=UTC
+tap_i=1
 
 rc=0
 test_chdir || rc=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: bf484caa1884d1045df7fcd4a5e40c0e0cd717fb
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 21:34:45 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 21:34:45 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=bf484caa

Restore genfun_y at the same time as genfun_x in _eend()

That this was not happening did not yet amount to a bug but let's ensure
that it cannot be one in the future.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 1 +
 1 file changed, 1 insertion(+)

diff --git a/functions.sh.in b/functions.sh.in
index d13be7d..d4becf8 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -328,6 +328,7 @@ _eend()
if [ "${offset}" -lt 0 ] || [ "${offset}" -gt 1 ]; then
printf '\033[%d;%dH' "$1" "$2"
genfun_x=$1
+   genfun_y=$2
fi
fi
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 347c3975525e15f52a537ef4ecccf8f3b33087bf
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 21:26:16 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 21:26:16 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=347c3975

Remove the vim modeline

I am fed up with the indentation behaviour being wrecked by the
imablance between ts and sw and I aleady assume that a tabstop is
equivalent to 8 spaces. Enough.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 2 --
 1 file changed, 2 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 5141670..d13be7d 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -698,5 +698,3 @@ else
NORMAL=$(printf '\033[0m')
WARN=$(printf '\033[33;01m')
 fi
-
-# vim:ts=4



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: de41ef45b33e645d04756c3a7dba2230f847eb0f
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 22:10:04 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 22:25:47 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=de41ef45

test-functions: Add a test for the _update_cursor_coords() function

Signed-off-by: Kerin Millar  plushkava.net>

 test-functions | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/test-functions b/test-functions
index 0fd998d..9f87e98 100755
--- a/test-functions
+++ b/test-functions
@@ -303,6 +303,27 @@ test_is_visible() {
iterate_tests 2 "$@"
 }
 
+test_update_cursor_coords() {
+   if _has_dumb_terminal; then
+   printf '1..0 # TERM defines the terminal as being dumb\n'
+   elif ! ctty=$(ps -p "$$" -otty= 2>/dev/null) || [ -z "${ctty}" ]; then
+   printf '1..0 # the ps(1) implementation is defective or 
non-compliant\n'
+   elif [ "${ctty}" = "?" ]; then
+   printf '1..0 # no controlling terminal is available\n'
+   elif [ ! -e /dev/tty ]; then
+   printf '1..0 # the /dev/tty character device is missing\n'
+   elif ! _update_winsize /dev/tty; then
+   printf '1..1\n'
+   printf 'not ok 1 - ecm48-cpr failed\n'
+   false
+   else
+   printf '1..1\n'
+   printf 'ok 1 - ecm48-cpr succeeded (x = %d, y = %d)\n' 
"${genfun_x}" "${genfun_y}"
+   fi
+}
+
 test_yesno() {
set -- \
0  yes \
@@ -430,6 +451,7 @@ test_esyslog || rc=1
 test_is_identifier || rc=1
 test_is_int || rc=1
 test_is_visible || rc=1
+test_update_cursor_coords || rc=1
 test_yesno || rc=1
 cleanup_tmpdir
 exit "${rc}"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 721c22bcd300b9a5d995e3dfbe66e46ca944e08d
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 21:21:35 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 21:21:51 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=721c22bc

Right-trim messages given to ebegin() and _eprint() where appropriate

Have ebegin() strip all trailing newlines rather than just one at most.
Likewise for _eprint() in the case that a smart terminal is found.

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 16 
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/functions.sh.in b/functions.sh.in
index 673be7d..5141670 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -62,8 +62,13 @@ _eprint() {
# VT100 and can be considered as a de-facto standard.
printf ' %s*%s %s%s\0337' "${color}" "${NORMAL}" 
"${genfun_indent}" "${msg}"
else
-   # Print the message without its trailing LF character.
-   msg=${msg%"${genfun_newline}"}
+   # Strip all trailing LF characters before printing the message.
+   while true; do
+   msg=${msg%"${genfun_newline}"}
+   if ! _ends_with_newline "${msg}"; then
+   break
+   fi
+   done
printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" 
"${msg}"
 
# Determine the current position of the cursor
@@ -235,7 +240,9 @@ ebegin()
 
if ! yesno "${EINFO_QUIET}"; then
msg=$*
-   msg=${msg%"${genfun_newline}"}
+   while _ends_with_newline "${msg}"; do
+   msg=${msg%"${genfun_newline}"}
+   done
_eprint "${GOOD}" "${msg} ..."
fi
 }
@@ -584,7 +591,8 @@ _has_monochrome_terminal() {
 }
 
 _ends_with_newline() {
-   ! case $1 in *"${genfun_newline}") false ;; esac
+   test "${genfun_newline}" \
+   && ! case $1 in *"${genfun_newline}") false ;; esac
 }
 
 _update_tty_level() {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: be8077d728a39ae606dc6ace51cb18fe45955f78
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 20:30:57 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 20:45:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=be8077d7

test-functions: Ensure that ecma48-cpr can be resolved during src_test()

The test phase is run prior to the install phase. Compensate for this by
wrapping ecma48-cpr with a shim function and redeclaring said function
where the value of EBUILD_PHASE is equal to "test".

Signed-off-by: Kerin Millar  plushkava.net>

 functions.sh.in | 6 +-
 test-functions  | 8 
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/functions.sh.in b/functions.sh.in
index 3053162..673be7d 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -627,7 +627,7 @@ _update_winsize() {
 
 _update_cursor_coords() {
# shellcheck disable=2046
-   set -- $(@GENTOO_LIBEXEC_DIR@/ecma48-cpr)
+   set -- $(_ecma48_cpr)
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
genfun_y=$1
genfun_x=$2
@@ -638,6 +638,10 @@ _update_cursor_coords() {
fi
 }
 
+_ecma48_cpr() {
+   @GENTOO_LIBEXEC_DIR@/ecma48-cpr "$@"
+}
+
 # This is the main script, please add all functions above this point!
 # shellcheck disable=2034
 RC_GOT_FUNCTIONS="yes"

diff --git a/test-functions b/test-functions
index 5a6b23b..0d7b1ab 100755
--- a/test-functions
+++ b/test-functions
@@ -408,6 +408,14 @@ if ! . ./functions.sh; then
bailout "Couldn't source ./functions.sh"
 fi
 
+# Since the test suite is normally executed during the src_test phase, the
+# ecma48-cpr utility will not yet have been installed. Account for that by
+# redeclaring its shim function.
+if [ "${EBUILD_PHASE}" = test ]; then
+   export BUILD_DIR=${PWD}
+   _ecma48_cpr() { "${BUILD_DIR}"/ecma48-cpr "$@"; }
+fi
+
 assign_tmpdir
 
 export TEST_GENFUNCS=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 9ed12f2dbbd47a621839a4ba3183669f8ec5806c
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Jun  9 20:49:49 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 20:54:07 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9ed12f2d

Have ecma48-cpr flush the terminal's input queue

There is no guarantee that the input queue is empty at the time that
ecma48-cpr is executed. If the terminal has input waiting, not only
will ecma48-cpr potentially fail but it will pollute the input queue.
Work around this by flushing the input queue entirely. I'm not
especially pleased by this approach but I can see no other option,
short of giving up on the idea of using the CPR sequence at all.

Signed-off-by: Kerin Millar  plushkava.net>

 ecma48-cpr.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
index 9aef182..144c4ea 100644
--- a/ecma48-cpr.c
+++ b/ecma48-cpr.c
@@ -95,6 +95,8 @@ main(void) {
 */
if (tcsetattr(STDIN_FILENO, TCSANOW, _tty) != 0) {
die("failed to modify the terminal settings");
+   } else if (tcflush(STDIN_FILENO, TCIFLUSH) != 0) {
+   die("failed to flush the terminal's input queue");
} else if (fprintf(tty, "\033[6n") != 4) {
die("failed to write the CPR sequence to the terminal");
} else if (fclose(tty) != 0) {



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: d06bd4a6b6dac170724c17cabd282fee4eb8f91e
Author: Sam James  gentoo  org>
AuthorDate: Fri Jun  9 11:16:11 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 11:16:11 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=d06bd4a6

meson.build: drop libexecdir for now

Right now, I don't think there's much benefit for putting the ecma tool
in libexecdir given it's in /usr and everything assumes functions.sh is at
/lib/gentoo/functions.sh - i.e. it unnecessarily breaks split-usr.

Maybe we can just add some symlinks later though.

Signed-off-by: Sam James  gentoo.org>

 meson.build | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/meson.build b/meson.build
index f86edd5..8996d95 100644
--- a/meson.build
+++ b/meson.build
@@ -10,7 +10,9 @@ project(
 
 conf_data = configuration_data()
 conf_data.set('version', meson.project_version())
-conf_data.set('GENTOO_LIBEXEC_DIR', get_option('prefix') / 
get_option('libexecdir') / 'gentoo')
+# For now, we can't really use libexec, given everyone hardcodes /lib/gentoo.
+# We might be able to install some symlinks to get around this though?
+conf_data.set('GENTOO_LIBEXEC_DIR', get_option('prefix') / 'lib' / 'gentoo')
 
 configure_file(
   input: 'functions.sh.in',
@@ -36,7 +38,7 @@ executable(
   'ecma48-cpr',
   'ecma48-cpr.c',
   install: true,
-  install_dir: get_option('prefix') / get_option('libexecdir') / 'gentoo'
+  install_dir: get_option('prefix') / 'lib' / 'gentoo'
 )
 
 do_tests = get_option('tests')



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 33e23d38b0235978641e29ea367a89614b7bc999
Author: Sam James  gentoo  org>
AuthorDate: Fri Jun  9 11:11:22 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 11:11:22 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=33e23d38

meson.build: fix running tests out of source

Signed-off-by: Sam James  gentoo.org>

 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 049812d..f86edd5 100644
--- a/meson.build
+++ b/meson.build
@@ -43,6 +43,6 @@ do_tests = get_option('tests')
 if do_tests
   test(
 'test-functions', files('test-functions'),
-workdir : meson.current_source_dir(),
+workdir : meson.current_build_dir(),
   )
 endif



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 20bc15b5b1009149c4a3d531911d3c219dc55f3a
Author: Kerin Millar  plushkava  net>
AuthorDate: Tue Jun  6 04:07:13 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 06:57:07 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=20bc15b5

Overhaul _eprint() and _eend() so as to better leverage smart terminals

This commit introduces a function named _update_tty_level(), whose
reponsibility is to grade the capability of the terminal, if any, on a
scale of 0 to 2. A value of 0 indicates that no terminal has been
detected, 1 that a dumb terminal has been detected, while 2 indicates
that a smart terminal has been detected. For the terminal to be
considered as smart, all of the following conditions must hold true.

- the TERM variable must not contain "dumb" as a substring
- the stty(1) utility must be able to report the terminal's dimensions
- the cursor's position must be reported for the ECMA-48 CPR sequence

Both the _eprint() and _eend() functions have been adjusted, so as to
evaluate the terminal's capabilites on each occasion that they are
called, with the overall intention of better exploiting the capabilities
of modern terminals, thus providing a better experience for a majority
of Gentoo users. These adjustments are described herewith.

Common to both functions is that the mechanism of tracking whether a
 character is pending for the last printed message is no longer
employed for smart terminals.

As concerns _eprint(), it now begins by not only determining whether a
terminal is attached to STDOUT, but whether it is a smart terminal. In
the case that it is smart, it shall be determined whether the cursor is
situated at the first column. Should it not be, a  character
shall be emitted, on the basis that the last call may have been to a
function that printed a message without a trailing .

Next, the message shall be checked, so as to determine whether it
contains a trailing  character. Should it not, the message
shall be printed, along with a trailing DECSC sequence. The effect of
this sequence is to instruct the terminal emulator to save the current
position of the cursor.

If, on the other hand, the message is found to contain a trailing
 character, it shall be stripped prior to printing the message.
The terminal shall then be asked to report the new position of the
cursor. Should the cursor turn out to be situated at the very last row
of the terminal, it shall be assumed that vertical scrolling is about to
occur and the cursor shall be moved up by one row, prior to its position
being saved by way of the DECSC sequence. Thereafter, two 
characters shall be printed, so as to advance to the next line (relative
to the message). Otherwise, should the cursor be found to be situated at
a row that precedes the last row of the terminal, the DECSC sequence
shall be emitted, followed by a single  character.

Note that the overall intention is to stash the exact position of the
cursor at the point that it is situated at the end of the printed
message, but just before a  is printed (if any).

As concerns _eend(), and as with _eprint(), it now begins by not only
determining whether a terminal is attached to STDOUT, but whether it is
a smart terminal, though not before saving the last-known dimensions
of the terminal. Next, in the case of a dumb terminal - or no terminal
at all - the message shall be printed with a single preceding space.
That is, no attempt shall be made to right-align the success/failure
indicator. If, on the other hand, the terminal is found to be smart, the
behaviour shall be as described by the next paragraph.

The current dimensions of the terminal shall be compared against the
last-known dimensions of the terminal, as they were prior to the
terminal grading procedure. The purpose of doing so is to determine
whether the terminal has been recently resized. For the terminal not to
have been resized implies that it may yet be possible to write the
success/failure indicator on the very same row as the last printed
message, even if it were -terminated. With that in mind, the
current position of the cursor shall be saved. Next, the DECRC sequence
shall be emitted to the terminal. The effect of this sequence is to
instruct the terminal emulator to restore the position of the cursor,
as was saved by the DECSC sequence. In other words, the cursor position
shall be moved to wherever it was just after the last message was
printed, albeit before the trailing  character (if any). Next,
the terminal shall be asked to report the new position of the cursor. In
the event that the cursor is found to have moved to any other row than
the immediately preceding one, it shall be assumed that scrolling has
occurred since printing the last message, in which case the cursor shall
be moved back to its prior position. Otherwise, the cursor shall be left
at its present position.

With that out of the way, the degree of indentation required to
right-align 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 16f347ffdff4da72d9a262a82f32777229c413b2
Author: Sam James  gentoo  org>
AuthorDate: Fri Jun  9 00:38:23 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 06:57:10 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=16f347ff

Port to Meson

... yielding
```
/tmp/destdir
└── usr
└── local
├── bin
│   └── consoletype
├── lib
│   └── gentoo
│   └── functions.sh
├── libexec
│   └── gentoo
│   └── ecma48-cpr
└── share
└── man
└── man1
└── consoletype.1
11 directories, 4 files
```

Signed-off-by: Sam James  gentoo.org>

 Makefile| 39 -
 functions.sh => functions.sh.in |  7 +-
 meson.build | 48 +
 meson_options.txt   |  3 +++
 4 files changed, 52 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile
deleted file mode 100644
index 3fa8d85..000
--- a/Makefile
+++ /dev/null
@@ -1,39 +0,0 @@
-# gentoo-functions Makefile
-# Copyright 2014-2023 Gentoo Authors
-# Distributed under the terms of the GNU General Public License v2
-
-VERSION = 0.20
-GITREF ?= $(VERSION)
-PKG ?= gentoo-functions-$(VERSION)
-
-ROOTPREFIX ?=
-ROOTSBINDIR ?= $(ROOTPREFIX)/sbin
-ROOTLIBEXECDIR ?= $(ROOTPREFIX)/lib/gentoo
-
-PREFIX ?= /usr
-MANDIR ?= $(PREFIX)/share/man
-
-PROGRAMS = consoletype
-
-all: $(PROGRAMS)
-
-check: all
-   ./test-functions
-
-install: all
-   install -m 0755 -d $(DESTDIR)$(ROOTSBINDIR)
-   install -m 0755 consoletype $(DESTDIR)$(ROOTSBINDIR)
-   install -m 0755 -d $(DESTDIR)$(ROOTLIBEXECDIR)
-   install -m 0644 functions.sh $(DESTDIR)$(ROOTLIBEXECDIR)
-   install -m 0755 -d $(DESTDIR)$(MANDIR)/man1
-   install -m 0644 consoletype.1 $(DESTDIR)$(MANDIR)/man1
-
-clean:
-   rm -rf $(PROGRAMS)
-
-dist:
-   git archive --prefix=$(PKG)/ $(GITREF) | xz > $(PKG).tar.xz
-
-consoletype: consoletype.c
-
-# vim: set ts=4 :

diff --git a/functions.sh b/functions.sh.in
similarity index 98%
rename from functions.sh
rename to functions.sh.in
index 154c8a4..3053162 100644
--- a/functions.sh
+++ b/functions.sh.in
@@ -9,11 +9,6 @@
 # intended for internal use shall be prefixed with "genfun_" to indicate so,
 # and to reduce the probability of name space conflicts.
 
-# FIXME. There is just one invocation of ./ecma48-cpr, which can be found in
-# the _update_cursor_coords function. If you think it should be named sometime
-# else, I'm open to suggestions. Its behaviour now resembles that of stty size,
-# except that it reports the cursor coordinates (obviously).
-
 #
 #Called by ebegin, eerrorn, einfon, and ewarnn.
 #
@@ -632,7 +627,7 @@ _update_winsize() {
 
 _update_cursor_coords() {
# shellcheck disable=2046
-   set -- $(./ecma48-cpr)
+   set -- $(@GENTOO_LIBEXEC_DIR@/ecma48-cpr)
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
genfun_y=$1
genfun_x=$2

diff --git a/meson.build b/meson.build
new file mode 100644
index 000..049812d
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,48 @@
+project(
+  'gentoo-functions', 'c',
+  version: '0.20',
+  license: 'GPL-2.0-only',
+  default_options : [
+'warning_level=2',
+'c_std=gnu11',
+  ]
+)
+
+conf_data = configuration_data()
+conf_data.set('version', meson.project_version())
+conf_data.set('GENTOO_LIBEXEC_DIR', get_option('prefix') / 
get_option('libexecdir') / 'gentoo')
+
+configure_file(
+  input: 'functions.sh.in',
+  output: 'functions.sh',
+  configuration: conf_data,
+  install: true,
+  install_dir: 'lib/gentoo'
+)
+
+cc = meson.get_compiler('c')
+
+executable(
+  'consoletype',
+  'consoletype.c',
+  install: true
+)
+
+install_man(
+  'consoletype.1',
+)
+
+executable(
+  'ecma48-cpr',
+  'ecma48-cpr.c',
+  install: true,
+  install_dir: get_option('prefix') / get_option('libexecdir') / 'gentoo'
+)
+
+do_tests = get_option('tests')
+if do_tests
+  test(
+'test-functions', files('test-functions'),
+workdir : meson.current_source_dir(),
+  )
+endif

diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 000..7fbab2d
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,3 @@
+option('tests', type : 'boolean', value : true,
+  description : 'Build tests'
+)



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: 2a78a15de6e5d90da0657bf74929993b1b51a337
Author: Kerin Millar  plushkava  net>
AuthorDate: Thu Jun  8 05:48:02 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Jun  9 06:57:10 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=2a78a15d

Add a chdir() function to act as a safer alternative to 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 varname

This commit introduces a chdir() function that addresses all of these
pitfalls.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh   | 18 +
 test-functions | 63 +++---
 2 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/functions.sh b/functions.sh
index 7838c90..154c8a4 100644
--- a/functions.sh
+++ b/functions.sh
@@ -534,6 +534,24 @@ is_int() {
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
+#
+chdir() {
+   if [ "$BASH" ]; then
+   shopt -u cdable_vars
+   fi
+   if [ "$1" = - ]; then
+   set -- ./-
+   fi
+   CDPATH= cd -- "$@"
+}
+
 #
 #   Determine whether the first operand contains any visible characters.
 #

diff --git a/test-functions b/test-functions
index b80587b..5a6b23b 100755
--- a/test-functions
+++ b/test-functions
@@ -14,7 +14,7 @@ bailout() {
 assign_tmpdir() {
# shellcheck disable=1007
dir=$(mktemp -d) \
-   && CDPATH= cd -- "${dir}" \
+   && chdir "${dir}" \
|| bailout "Couldn't create or change to the temp dir"
 }
 
@@ -24,6 +24,49 @@ cleanup_tmpdir() {
fi
 }
 
+test_chdir() {
+   set -- \
+   1  grandchild  \
+   1 var  \
+   0  -L  \
+   0  -p  \
+   0  -e  \
+   0  -@  \
+   0   -  \
+   0   child
+
+   if ! mkdir -p -- -L -p -e -@ - child child/grandchild; then
+   bailout "Couldn't set up all test directories"
+   fi
+
+   callback() {
+   shift
+   test_description="chdir $(print_args "$@")"
+   if [ "$BASH" ]; then
+   shopt -s cdable_vars
+   fi
+   CDPATH=child var=$CDPATH chdir "$@" \
+   && test "$PWD" != "$OLDPWD" \
+   && cd - >/dev/null
+   }
+
+   iterate_tests 2 "$@"
+}
+
+test_chdir_noop() {
+   set -- 0 ''
+
+   callback() {
+   shift
+   test_description="chdir $(print_args "$@")"
+   chdir "$@" \
+   && test "$PWD" = "$OLDPWD" \
+   || { cd - >/dev/null; false; }
+   }
+
+   iterate_tests 2 "$@"
+}
+
 test_is_older_than() {
set -- \
1  N/A   N/A \
@@ -317,12 +360,24 @@ iterate_tests() {
fi
done
eval "${code}"
-   if [ "$?" -eq "$1" ]; then
+   case $? in
+   0)
+   test "$?" -eq "$1"
+   ;;
+   *)
+   test "$?" -ge "$1"
+   esac
+   if [ "$?" -eq 0 ]; then
passed=$((passed + 1))
else
printf 'not '
fi
-   printf 'ok %d - %s (expecting %d)\n' "${i}" 
"${test_description}" "$1"
+   if [ "$1" -eq 0 ]; then
+   expected=$1
+   else
+   expected=">=$1"
+   fi
+   printf 'ok %d - %s (expecting %s)\n' "${i}" 
"${test_description}" "${expected}"
shift "${slice_width}"
done
return "$(( passed < total ))"
@@ -359,6 +414,8 @@ export TEST_GENFUNCS=1
 export TZ=UTC
 
 rc=0
+test_chdir || rc=1
+test_chdir_noop || rc=1
 ( ewarn() { true; }; test_is_older_than ) || rc=1
 test_get_bootparam || rc=1
 test_esyslog || rc=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-09 Thread Sam James
commit: e08b541e2c875d7718d4452bbd8bb1228bd1a53b
Author: Kerin Millar  plushkava  net>
AuthorDate: Tue Jun  6 03:54:50 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:14:24 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=e08b541e

Introduce a utility to obtain the cursor coordinates via ECMA-48 CPR

The ECMA-48 specification defines the CPR (Cursor Position Report)
sequence which, as its name implies, instructs the terminal to divulge
the present coordinates of the cursor. Though it is theoretically
possible to make use of this sequence by using the shell and standard
utilities, it would be tremendously costly and somewhat unreliable to
do so. This commit introduces a utility - written in C - that does the
job.

Its behaviour is quite simple. If STDIN is found to be a terminal, an
attempt will be made to initiate - and read - a Cursor Position Report.
Upon success, the present row and column of the cursor shall be printed
as two space-separated decimal integers. Otherwise, a diagnostic
message shall be printed to STDERR, and the utility shall exit with
a non-zero status.

This utility will be used by the impending overhaul of the _eprint()
and _eend() functions.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 ecma48-cpr.c | 230 +++
 1 file changed, 230 insertions(+)

diff --git a/ecma48-cpr.c b/ecma48-cpr.c
new file mode 100644
index 000..9aef182
--- /dev/null
+++ b/ecma48-cpr.c
@@ -0,0 +1,230 @@
+/*
+ * ecma48-cpr.c
+ * Treat STDIN as a tty and report the cursor position using the CPR sequence.
+ *
+ * Originally distributed as wsize.c by Stephen J. Friedl .
+ * Repurposed for gentoo-functions by Kerin F. Millar .
+ * This software is in the public domain.
+ */
+
+#define _POSIX_C_SOURCE 200809L
+
+#define PROGRAM "ecma48-cpr"
+#define READ_TIMEOUT_NS 25000
+#define BUFSIZE 100
+#define MAX_LOOPS 20
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static struct termios save_tty;
+static bool is_timed_out = false;
+static bool is_tty_saved = false;
+
+static void cleanup(void);
+static void die(char const * const errmsg);
+static void on_signal(int const signo);
+
+#ifndef __APPLE__
+static timer_t init_timer(void);
+#endif
+
+int
+main(void) {
+   /*
+* Establish that STDIN is a terminal.
+*/
+   if (! isatty(STDIN_FILENO)) {
+   die("cannot determine the cursor position because stdin is not 
a tty");
+   }
+
+   /*
+* Duplicate STDIN to a new file descriptor before reopening it as a
+* writeable stream.
+*/
+   int fd = dup(STDIN_FILENO);
+   FILE *tty;
+   if (fd < 0) {
+   die("failed to dup stdin");
+   } else {
+   tty = fdopen(fd, "w");
+   if (tty == NULL) {
+   die("failed to re-open the tty for writing");
+   }
+   }
+
+   /*
+* Save the current terminal settings.
+*/
+   if (tcgetattr(STDIN_FILENO, _tty) != 0) {
+   die("failed to obtain the current terminal settings");
+   } else {
+   is_tty_saved = true;
+   }
+
+   /*
+* Duplicate the current terminal settings for modification.
+*/
+   struct termios new_tty = save_tty;
+   new_tty = save_tty;
+
+   /*
+* Turn off ECHO, so that the response from the terminal isn't printed.
+* Also, the terminal must be operating in its noncanonical mode,
+* thereby ensuring that its input is always immediately available,
+* with no processing having been performed.
+*/
+   new_tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
+   new_tty.c_lflag &= ~(ICANON);
+
+   /*
+* Set an interbyte timeout of 1 decisecond. The timer is started only
+* after the first byte is read, so read(2) will block until then.
+*/
+   new_tty.c_cc[VMIN]  = 1;
+   new_tty.c_cc[VTIME] = 1;
+
+   /*
+* Try to apply the new terminal settings.
+*/
+   if (tcsetattr(STDIN_FILENO, TCSANOW, _tty) != 0) {
+   die("failed to modify the terminal settings");
+   } else if (fprintf(tty, "\033[6n") != 4) {
+   die("failed to write the CPR sequence to the terminal");
+   } else if (fclose(tty) != 0) {
+   die("failed to flush the stream after writing the CPR 
sequence");
+   }
+
+   /*
+* Prepare to catch our signals. We treat both an interrupt and a
+* depleted timer as essentially the same thing: fatal errors.
+*/
+   struct sigaction act;
+   act.sa_handler = on_signal;
+   sigemptyset(_mask);
+   act.sa_flags = 0;
+   sigaction(SIGALRM, , NULL);
+
+   /*
+* A timeout is required, just in case read(2) proves unable to read an
+

[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: 12eedb30ca60f0a7ae781b845d7bc22cc1b3267f
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon Jun  5 03:01:43 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:53 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=12eedb30

test-functions: Add a test for the _is_visible() function

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 test-functions | 21 +
 1 file changed, 21 insertions(+)

diff --git a/test-functions b/test-functions
index 72b5cae..b80587b 100755
--- a/test-functions
+++ b/test-functions
@@ -240,6 +240,26 @@ test_is_int() {
iterate_tests 2 "$@"
 }
 
+test_is_visible() {
+   set -- \
+   1  '' \
+   1  ' ' \
+   1  "$(printf '\t')" \
+   1  "$(printf '\a')" \
+   0  . \
+   0  ' . ' \
+   0  "$(printf '\t.\t')" \
+   0  "$(printf '\a.\a')"
+
+   callback() {
+   shift
+   test_description="_is_visible $(print_args "$@")"
+   _is_visible "$@"
+   }
+
+   iterate_tests 2 "$@"
+}
+
 test_yesno() {
set -- \
0  yes \
@@ -344,6 +364,7 @@ test_get_bootparam || rc=1
 test_esyslog || rc=1
 test_is_identifier || rc=1
 test_is_int || rc=1
+test_is_visible || rc=1
 test_yesno || rc=1
 cleanup_tmpdir
 exit "${rc}"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: b7245f936e1cd16aad6d9c7481fd0b52b2371703
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 25 00:55:20 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=b7245f93

Add and integrate the _update_winsize() function

This new function assumes responsibility for updating the genfun_cols
variable. Additionally, it assumes responsibility for updating the newly
introduced genfun_rows variable.

As before, stty(1) is used to determine the dimensions of the terminal.
The approach has been slightly altered so as to incur just one subshell
in the course of doing so. The reason for this is that changes will soon
be made to the _eprint() and _eend() functions that require for the
function to be called repeatedly, not merely at the time of sourcing
functions.sh.

Should stty(1) fail - or produce nonsensical output - it will no longer
be assumed that there are 80 available columns. The requirement for the
"[ ok ]" and "[ !! ]" indicators to always be indented was recently
eliminated, so there is no longer any need to falsify a value for
genfun_cols.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 42 ++
 1 file changed, 18 insertions(+), 24 deletions(-)

diff --git a/functions.sh b/functions.sh
index 44a6dce..c326af4 100644
--- a/functions.sh
+++ b/functions.sh
@@ -477,6 +477,23 @@ _ends_with_newline() {
! case $1 in *"${genfun_newline}") false ;; esac
 }
 
+_update_winsize() {
+   # The following use of stty(1) is portable as of POSIX Issue 8. It would
+   # be beneficial to leverage the checkwinsize option in bash but the
+   # implementation is buggy. Given that Chet has agreed to investigate,
+   # it may eventually become possible to support it.
+   # shellcheck disable=2046
+   set -- $(stty size 2>/dev/null)
+   if is_int "$1" && is_int "$2" && [ "$1" -gt 0 ] && [ "$2" -gt 0 ]; then
+   genfun_rows=$1
+   genfun_cols=$2
+   else
+   genfun_rows=
+   genfun_cols=
+   false
+   fi
+}
+
 # This is the main script, please add all functions above this point!
 # shellcheck disable=2034
 RC_GOT_FUNCTIONS="yes"
@@ -511,30 +528,7 @@ else
done
 fi
 
-# Try to determine the number of available columns in the terminal.
-for _ in 1 2; do
-   case $_ in
-   1)
-   # This use of stty(1) is portable as of POSIX Issue 8.
-   genfun_cols=$(
-   stty size 2>/dev/null | {
-   if IFS=' ' read -r _ cols; then
-   printf '%s\n' "${cols}"
-   fi
-   }
-   )
-   ;;
-   2)
-   # Give up and assume 80 available columns.
-   genfun_cols=80
-   break
-   esac
-   if is_int "${genfun_cols}" && [ "${genfun_cols}" -gt 0 ]; then
-   break
-   fi
-done
-
-if _has_dumb_terminal; then
+if _has_dumb_terminal || ! _update_winsize; then
unset -v genfun_endcol
 else
# Set some ECMA-48 CSI sequences (CUU and CUF) for cursor positioning.



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: cc75239fd896bb528e4faf9e6b54a900fb657f2e
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Feb 24 06:15:57 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=cc75239f

Jettison the genfun_lastbegun_strlen and genfun_lastcall variables

The genfun_lastbegun_strlen variable was previously used by _eend() to
indent the "[ ok ]" and "[ !! ]" indicators in the case that STDOUT is
found not to be a tty. Dispense with this variable. Instead, refrain
from indenting the indicator at all. After all, the width of the
controlling terminal is immaterial unless one is actually intending to
print to it.

$ { ebegin "Testing"; eend 0; } | cat
 * Testing ... [ ok ]

Apart from simplifying the code, this change brings the overall
behaviour of the printing functions closer to that of their counterparts
in OpenRC.

The genfun_lastcall variable was previously used by the _eprint()
function for the sole purpose of printing a LF (newline) character in
the case that the last message was printed as a consequence of calling
the ebegin() function. It would do so in anticipation of the _eend()
function later emitting the CUU (ECMA-48 CSI) sequence to move the
cursor up by one line, just prior to printing the "[ ok ]" and "[ !! ]"
indicators. Additionally, it was used by the ebegin() function to
determine whether a terminating LF character should follow the printed
message. Specifically, it would elect to print a LF character in the
case that ECMA-48 CSI sequences had been disabled at the time of
functions.sh being sourced. Finally, the value of genfun_lastcall would
influence the (now defunct) method by which the _eend() function would
calculate the degree of indentation required for the indicator in the
case of STDOUT not being a tty.

This variable has been dispensed with and replaced by a variable named
genfun_is_pending_lf. As its name suggests, its purpose is to track
whether the last printed message happened to contain a terminating LF
character. Now, whenever the _eprint() function is called, it consults
the variable so as to determine whether a LF character should be printed
for the purpose of terminating the last message, just before proceeding
to print the next one. Once the next one has been printed, the value of
the variable is updated accordingly. Similarly, the _eend() function
consults the variable so as to determine whether a LF character should
be printed to a terminal, just prior to the CUU (ECMA-48 CSI) sequence.

Ultimately, ebegin() will no longer be sloppily treated as a special
case. Rather, any printing function that inhibits the addition of a
terminating LF character (ebegin, einfon, ewarn, errorn etc) will have
its message be terminated upon calling any printing function thereafter.

In addition to cleaning up the code a little, these changes should
render the impending overhaul of the _eprint() and _eend() functions
easier to digest, once it lands.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 94 
 1 file changed, 44 insertions(+), 50 deletions(-)

diff --git a/functions.sh b/functions.sh
index 31d1b43..97b0e6b 100644
--- a/functions.sh
+++ b/functions.sh
@@ -13,17 +13,21 @@
 #Called by ebegin, eerrorn, einfon, and ewarnn.
 #
 _eprint() {
-   local color
+   local color msg
color=$1
shift
 
-   if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; 
then
-   printf '\n'
-   fi
+   msg=$*
if [ -t 1 ]; then
-   printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" 
"$*"
+   printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" 
"${msg}"
else
-   printf ' * %s%s' "${genfun_indent}" "$*"
+   printf ' * %s%s' "${genfun_indent}" "${msg}"
+   fi
+
+   if _ends_with_newline "${msg}"; then
+   genfun_is_pending_lf=0
+   else
+   genfun_is_pending_lf=1
fi
 }
 
@@ -117,7 +121,6 @@ einfon()
 {
if ! yesno "${EINFO_QUIET}"; then
_eprint "${GOOD}" "$@"
-   genfun_lastcall="einfon"
fi
 }
 
@@ -126,9 +129,7 @@ einfon()
 #
 einfo()
 {
-   einfon "$*
-"
-   genfun_lastcall="einfo"
+   einfon "${*}${genfun_newline}"
 }
 
 #
@@ -139,7 +140,6 @@ ewarnn()
if ! yesno "${EINFO_QUIET}"; then
_eprint "${WARN}" "$@" >&2
esyslog "daemon.warning" "${0##*/}" "$@"
-   genfun_lastcall="ewarnn"
fi
 }
 
@@ -148,9 +148,7 @@ ewarnn()
 #
 ewarn()
 {
-   ewarnn "$*
-"
-   genfun_lastcall="ewarn"
+   ewarnn "${*}${genfun_newline}"
 }
 
 #
@@ -161,7 +159,6 @@ eerrorn()
if ! yesno "${EERROR_QUIET}"; then
_eprint "${BAD}" "$@" >&2
esyslog "daemon.err" 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: 7e6ee7b371f5cc56dcf0e5918c6ec93ea0f7fb1b
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon Jun  5 02:44:30 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:53 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=7e6ee7b3

test-functions: Silence ewarn() while testing is_older_than()

The is_older_than() function now calls ewarn() if given invalid
arguments. For the purposes of the test suite, the resulting diagnostic
messages aren't particularly interesting.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 test-functions | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test-functions b/test-functions
index 892a255..72b5cae 100755
--- a/test-functions
+++ b/test-functions
@@ -339,7 +339,7 @@ export TEST_GENFUNCS=1
 export TZ=UTC
 
 rc=0
-test_is_older_than || rc=1
+( ewarn() { true; }; test_is_older_than ) || rc=1
 test_get_bootparam || rc=1
 test_esyslog || rc=1
 test_is_identifier || rc=1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: dd7522f3df09b8a92a303cc72690363979ecff3d
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon Jun  5 04:08:39 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:53 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=dd7522f3

Don't assume a monochrome terminal in the case that tput colors fails

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/functions.sh b/functions.sh
index c326af4..34bb266 100644
--- a/functions.sh
+++ b/functions.sh
@@ -470,6 +470,8 @@ _has_monochrome_terminal() {
true
elif colors=$(tput colors 2>/dev/null) && is_int "${colors}"; then
test "${colors}" -eq -1
+   else
+   false
fi
 }
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: 17aa01321a250b99fa487e8be430d71a459e75e6
Author: Kerin Millar  plushkava  net>
AuthorDate: Mon Jun  5 02:42:26 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:53 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=17aa0132

test-functions: Add a test for the is_identifier() function

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 test-functions | 56 
 1 file changed, 56 insertions(+)

diff --git a/test-functions b/test-functions
index 2c1fa67..892a255 100755
--- a/test-functions
+++ b/test-functions
@@ -160,6 +160,61 @@ test_esyslog() {
iterate_tests 5 "$@"
 }
 
+test_is_identifier() {
+   set -- \
+   1   ''   \
+   1_   \
+   10   \
+   1   0a   \
+   1   0Z   \
+   19   \
+   1   9a   \
+   1   9Z   \
+   1   /a   \
+   1   /Z   \
+   1   .a   \
+   1   .Z   \
+   1   [a   \
+   1   [Z   \
+   1  '`a'  \
+   1  '`Z'  \
+   1   {a   \
+   1   {Z   \
+   1  '|a'  \
+   1  '|Z'  \
+   1   a/   \
+   1   Z/   \
+   1   a.   \
+   1   Z.   \
+   1   a[   \
+   1   Z[   \
+   1  'a`'  \
+   1  'Z`'  \
+   1   a{   \
+   1   Z{   \
+   1  'a|'  \
+   1  'Z|'  \
+   0a   \
+   0Z   \
+   0   __   \
+   0   _a   \
+   0   _Z   \
+   0   a_   \
+   0   Z_   \
+   0  a_a   \
+   0  a_Z   \
+   0  Z_a   \
+   0  Z_Z
+
+   callback() {
+   shift
+   test_description="is_identifier $(print_args "$@")"
+   is_identifier "$@"
+   }
+
+   iterate_tests 2 "$@"
+}
+
 test_is_int() {
set -- \
1  N/A \
@@ -287,6 +342,7 @@ rc=0
 test_is_older_than || rc=1
 test_get_bootparam || rc=1
 test_esyslog || rc=1
+test_is_identifier || rc=1
 test_is_int || rc=1
 test_yesno || rc=1
 cleanup_tmpdir



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: f8260d8494ac1fc243f9543a6b6836e69d7054d0
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Feb 24 02:24:23 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f8260d84

Use ewarn() to print the recently added diagnostic messages

Both the _eprint() and _eend() functions will be overhauled in the near
future, at which point conveying diagnostic messages (indirectly) to
_eprint() will become beneficial. This commit affects the following
functions.

  - esyslog
  - is_older_than
  - veend
  - vewend

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/functions.sh b/functions.sh
index 53f3205..31d1b43 100644
--- a/functions.sh
+++ b/functions.sh
@@ -95,7 +95,7 @@ esyslog()
local pri tag msg
 
if [ "$#" -lt 2 ]; then
-   printf 'Too few arguments for esyslog (got %d, expected at 
least 2)\n' "$#" >&2
+   ewarn "Too few arguments for esyslog (got $#, expected at least 
2)"
return 1
elif [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
pri=$1
@@ -208,7 +208,7 @@ _eend()
if [ "$#" -eq 0 ]; then
retval=0
elif ! is_int "$1" || [ "$1" -lt 0 ]; then
-   printf 'Invalid argument given to %s (the exit status code must 
be an integer >= 0)\n' "${CALLER}" >&2
+   ewarn "Invalid argument given to ${CALLER} (the exit status 
code must be an integer >= 0)"
retval=0
shift
else
@@ -331,7 +331,7 @@ veend()
if yesno "${EINFO_VERBOSE}"; then
CALLER=veend eend "$@"
elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then
-   printf 'Invalid argument given to veend (the exit status code 
must be an integer >= 0)\n' >&2
+   ewarn "Invalid argument given to veend (the exit status code 
must be an integer >= 0)"
else
return "$1"
fi
@@ -342,7 +342,7 @@ vewend()
if yesno "${EINFO_VERBOSE}"; then
CALLER=vewend ewend "$@"
elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then
-   printf 'Invalid argument given to vewend (the exit status code 
must be an integer >= 0)\n' >&2
+   ewarn "Invalid argument given to vewend (the exit status code 
must be an integer >= 0)"
else
return "$1"
fi
@@ -408,7 +408,7 @@ is_older_than()
local ref has_gfind
 
if [ "$#" -lt 2 ]; then
-   printf 'Too few arguments for is_older_than (got %d, expected 
at least 2)\n' "$#" >&2
+   ewarn "Too few arguments for is_older_than (got $#, expected at 
least 2)"
return 1
elif [ -e "$1" ]; then
ref=$1



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: 1947f0ed81f3b95a7e10a8a5a707776948f8a487
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Feb 24 18:49:33 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1947f0ed

Have ebegin() strip the trailing LF of the message it is given, if any

This is a precautionary measure, given that ebegin() appends " ..." to
the message.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/functions.sh b/functions.sh
index 97b0e6b..78317b3 100644
--- a/functions.sh
+++ b/functions.sh
@@ -179,8 +179,9 @@ ebegin()
local msg
 
if ! yesno "${EINFO_QUIET}"; then
-   msg="$* ...${genfun_newline}"
-   GENFUN_CALLER=ebegin _eprint "${GOOD}" "${msg}"
+   msg=$*
+   msg=${msg%"${genfun_newline}"}
+   _eprint "${GOOD}" "${msg} ...${genfun_newline}"
fi
 }
 



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-06-07 Thread Sam James
commit: bf2c509abfe1bcd2835531861425d2f33c06e13e
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Feb 24 19:27:32 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Wed Jun  7 11:12:18 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=bf2c509a

Drop support for the checkwinsize feature of bash

While there is nothing wrong with the way in which it is presently
being used, the feature is sufficiently bug-ridden that it will not work
correctly with the impending overhaul of the _eprint() and _eend()
functions. As such, drop support for it in advance. Chet has pledged to
treat my bug report as a feature request, so it may yet be possible to
re-introduce support for it in the future.

Signed-off-by: Kerin Millar  plushkava.net>
Bug: https://lists.gnu.org/archive/html/bug-bash/2023-02/msg00142.html
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 14 ++
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/functions.sh b/functions.sh
index 78317b3..44a6dce 100644
--- a/functions.sh
+++ b/functions.sh
@@ -512,19 +512,9 @@ else
 fi
 
 # Try to determine the number of available columns in the terminal.
-for _ in 1 2 3; do
+for _ in 1 2; do
case $_ in
1)
-   # Running an external command causes bash >=4.3 to set
-   # the COLUMNS variable, provided that the checkwinsize
-   # shopt is enabled. As of 5.0, it's enabled by default.
-   # shellcheck disable=3044
-   if [ -n "${BASH}" ] && shopt -s checkwinsize 
2>/dev/null; then
-   /bin/true
-   fi
-   genfun_cols=${COLUMNS}
-   ;;
-   2)
# This use of stty(1) is portable as of POSIX Issue 8.
genfun_cols=$(
stty size 2>/dev/null | {
@@ -534,7 +524,7 @@ for _ in 1 2 3; do
}
)
;;
-   3)
+   2)
# Give up and assume 80 available columns.
genfun_cols=80
break



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 60775e89a1411c772def465ef9b901b3fe78e450
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 16:55:17 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:19 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=60775e89

Simplify ECMA-48 sequence definition and dial back on the use of tput(1)

Up until very recently, functions.sh would not attempt to determine
whether a given message was being printed to a terminal. Over the years,
this has caused some deleterious effects, such as printing ECMA-48 CSI
and SGR sequences to file descriptors that are not necessarily attached
to a terminal. Another issue used to be that these sequences would be
emitted to terminals identifying themselves as "dumb".

Rather than tackle the underlying problems, the historical response has
tended towards further entrenching the use of the ncurses implementation
of tput(1). While it has its uses, the way in which it is employed is in
no way portable. POSIX only guarantees the availability of the clear,
init and reset operands. Presently, the colors operand is used as a
heuristic to determine whether the ncurses implementation is available
before proceeding to run a series of commands that will usually output
ECMA-48 SGR sequences that are standard for all but ancient and/or
obscure video terminals.

Another present use of tput(1) is in generating the CUU1 and CUF
sequences. In the overwhelming majority of cases, it will generate these
as standard ECMA-48 CSI sequences. To put this in perspective, the DEC
VT100, a video terminal released almost 45 years ago, supports these
sequences. The ECMA-48 specification, itself, is almost as old.

Unfortunately, reasoning with such matters tends towards a dichotomy.
On the one hand, there is a camp that considers it a cardinal sin to
attempt to emit an ANSI escape sequence without first having consulted a
specific implementation of tput(1). On the other, there is the camp that
points out that ECMA-48 sequences are highly portable and that video
terminals from the 1970s and 1980s are largely obsolete. Here are two
articles that present opposing points of view.

  https://mywiki.wooledge.org/BashFAQ/037
  https://xn--rpa.cc/irl/term.html

Of late, I find the arguments presented by the latter camp to be
increasingly convincing. As such, the goal of this commit is twofold.

Firstly, to implement an independent method for detecting a dumb
terminal. To that end, a _has_dumb_terminal() function has been added,
which simply checks the value of TERM. This, alone, determines whether
the CUU1 and CUF sequences should be assigned, with tput(1) no longer
being used to generate them.

Secondly, to further dial back on the use of tput(1) by not using it to
generate the SGR (colour) sequences. While I believe that nobody would
notice if we were to do away with the use of tput(1) altogether, I have
taken a nuanced approach by continuing to use it for detecting the
number of available colours. To that end, a _has_monochrome_terminal()
function has been implemented. This, alone, determines whether the SGR
sequences should be assigned. The function works by first checking
whether the terminal is dumb. If not, it tries to run "tput colors",
before checking whether -1 colours are reported, for that is how the
ncurses implementation reports the absence of colour support.

The resulting code is simpler and more pleasing to read, with tput being
run once, at most. Also, because there is no longer a requirement to
handle the CSI sequences in a raw form, they are now declared as strings
containing backslash-escape sequences, with printf %b being used to
decode them. This has the advantage of not emitting the raw codes to
STDERR while using xtrace for debugging.

In the future, I think that it may well be possible to drop the function
that detects a monochrome terminal and rely on dumb terminal detection
instead but let's see how it goes.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 46 --
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/functions.sh b/functions.sh
index c287b24..53f3205 100644
--- a/functions.sh
+++ b/functions.sh
@@ -253,7 +253,7 @@ _eend()
fi
 
if [ "${is_tty}" -eq 1 ] && [ -n "${genfun_endcol}" ]; then
-   printf '%s %s\n' "${genfun_endcol}" "${msg}"
+   printf '%b %s\n' "${genfun_endcol}" "${msg}"
else
[ "${genfun_lastcall}" = ebegin ] || genfun_lastbegun_strlen=0
printf "%$(( cols - genfun_lastbegun_strlen - 7 ))s %s\n" '' 
"${msg}"
@@ -474,6 +474,22 @@ is_identifier()
esac
 )
 
+_has_dumb_terminal() {
+   ! case ${TERM} in *dumb*) false ;; esac
+}
+
+_has_monochrome_terminal() {
+   local colors
+
+   # The tput(1) invocation is not portable, though ncurses suffices. In
+   # this day and age, it is 

[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 47a622418d5745d15d6b47ec2c63ab060e39dd25
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 11:00:09 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:07 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=47a62241

Precede end columns with a single space, not two, in _eend()

A beneficial effect of doing so, apart from being able to show one more
character, is that the the associated arithmetic expansions uniformly
work with the genuine length of the end column, which is 7.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/functions.sh b/functions.sh
index beaef03..c287b24 100644
--- a/functions.sh
+++ b/functions.sh
@@ -253,10 +253,10 @@ _eend()
fi
 
if [ "${is_tty}" -eq 1 ] && [ -n "${genfun_endcol}" ]; then
-   printf '%s  %s\n' "${genfun_endcol}" "${msg}"
+   printf '%s %s\n' "${genfun_endcol}" "${msg}"
else
[ "${genfun_lastcall}" = ebegin ] || genfun_lastbegun_strlen=0
-   printf "%$(( cols - genfun_lastbegun_strlen - 6 ))s%s\n" '' 
"${msg}"
+   printf "%$(( cols - genfun_lastbegun_strlen - 7 ))s %s\n" '' 
"${msg}"
fi
 
return "${retval}"
@@ -536,8 +536,8 @@ done
 # Set an ECMA-48 CSI sequence, allowing for eend to line up the [ ok ] string.
 {
genfun_endcol="$(tput cuu1)" \
-   && genfun_endcol="${genfun_endcol}$(tput cuf -- "$(( genfun_cols - 8 
))")" \
-   || genfun_endcol="$(printf '\033[A\033[%dC' "$(( genfun_cols - 8 ))")"
+   && genfun_endcol="${genfun_endcol}$(tput cuf -- "$(( genfun_cols - 7 
))")" \
+   || genfun_endcol="$(printf '\033[A\033[%dC' "$(( genfun_cols - 7 ))")"
 } 2>/dev/null
 
 # Setup the colors so our messages all look pretty



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 4927d7758cc4885680d1613ba818ee119738eac1
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 07:34:30 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=4927d775

Prevent shellcheck from complaining that HILITE is unused

Despite the fact that HILITE has never been expanded internally,
shellcheck only began to complain about it recently. Regardless, it is
a superfluous warning.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/functions.sh b/functions.sh
index e818049..7246179 100644
--- a/functions.sh
+++ b/functions.sh
@@ -553,6 +553,7 @@ else
BAD=$(printf '\033[31;01m')
BRACKET=$(printf '\033[34;01m')
GOOD=$(printf '\033[32;01m')
+   # shellcheck disable=2034
HILITE=$(printf '\033[36;01m')
NORMAL=$(printf '\033[0m')
WARN=$(printf '\033[33;01m')



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 3bd65f0f5598fc4bf93deb46b60775f19ba7c3e2
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 07:00:38 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=3bd65f0f

Avoid a needless test where the number of TTY columns can't be detected

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/functions.sh b/functions.sh
index d068627..e818049 100644
--- a/functions.sh
+++ b/functions.sh
@@ -523,6 +523,7 @@ for _ in 1 2 3; do
3)
# Give up and assume 80 available columns.
genfun_cols=80
+   break
esac
if is_int "${genfun_cols}" && [ "${genfun_cols}" -gt 0 ]; then
break



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: c517951d98b6ae3c9ca0c838c029641287bd4d8d
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 06:55:02 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=c517951d

Support NO_COLOR as a means of suppressing coloured output

Per https://no-color.org/, NO_COLOR is a de-facto standard for allowing
the user to express their preference for not seeing coloured output.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 25 ++---
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/functions.sh b/functions.sh
index fbc0812..d068627 100644
--- a/functions.sh
+++ b/functions.sh
@@ -479,20 +479,23 @@ RC_GOT_FUNCTIONS="yes"
 EINFO_QUIET="${EINFO_QUIET:-no}"
 EINFO_VERBOSE="${EINFO_VERBOSE:-no}"
 
-# Should we use color?
-RC_NOCOLOR="${RC_NOCOLOR:-no}"
-
 # Set the initial value for e-message indentation.
 genfun_indent=
 
-for _ in "$@"; do
-   case $_ in
-   # Check whether the user specifed an argument to disable color.
-   --nocolor|--nocolour|-C)
-   RC_NOCOLOR="yes"
-   break
-   esac
-done
+# Should we use color?
+if [ -n "${NO_COLOR}" ]; then
+   # See https://no-color.org/.
+   RC_NOCOLOR="yes"
+else
+   RC_NOCOLOR="${RC_NOCOLOR:-no}"
+   for _ in "$@"; do
+   case $_ in
+   --nocolor|--nocolour|-C)
+   RC_NOCOLOR="yes"
+   break
+   esac
+   done
+fi
 
 # Try to determine the number of available columns in the terminal.
 for _ in 1 2 3; do



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: de7a2ae7c416efe0fa75443f9174b05b1c6bbc5b
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 06:11:53 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=de7a2ae7

Explain the composition and naming conventions of gentoo-functions.sh

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 16 +---
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/functions.sh b/functions.sh
index 2918558..3b56b1d 100644
--- a/functions.sh
+++ b/functions.sh
@@ -2,8 +2,15 @@
 # Distributed under the terms of the GNU General Public License v2
 # shellcheck shell=sh disable=3043
 
+# This file contains a series of function declarations followed by some
+# initialization code. Functions intended for internal use shall be prefixed
+# with an  and shall not be considered as being a part of the 
public
+# API. With the exception of those declared by the local builtin, all variables
+# intended for internal use shall be prefixed with "genfun_" to indicate so,
+# and to reduce the probability of name space conflicts.
+
 #
-#This is a private function, called by ebegin, eerrorn, einfon, and ewarnn.
+#Called by ebegin, eerrorn, einfon, and ewarnn.
 #
 _eprint() {
local color
@@ -23,7 +30,6 @@ _eprint() {
 #
 #hard set the indent used for e-commands.
 #num defaults to 0
-# This is a private function.
 #
 _esetdent()
 {
@@ -201,9 +207,6 @@ ebegin()
 #indicate the completion of process, called from eend/ewend
 #if error, show errstr via efunc
 #
-#This function is private to functions.sh.  Do not call it from a
-#script.
-#
 _eend()
 {
local cols efunc is_tty msg retval
@@ -462,8 +465,7 @@ is_int() {
 }
 
 #
-#   Determine whether the first operand contains any visible characters. This
-#   is intended to be a private function.
+#   Determine whether the first operand contains any visible characters.
 #
 _is_visible() {
! case $1 in *[[:graph:]]*) false ;; esac



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 9bbf95a8fc7c280eb6cf323dc88b89e67293316a
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 08:03:14 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9bbf95a8

Add and integrate an is_identifier() function

Break out the routine to check for a valid identifier (variable name)
from yesno() into its own function. It is designated as a public
function because a great many scripts exist that use eval in an
exceedingly dangerous fashion, essentially performing code injection.
This function could well benefit scripts of such a calibre.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 25 ++---
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/functions.sh b/functions.sh
index 7246179..beaef03 100644
--- a/functions.sh
+++ b/functions.sh
@@ -76,19 +76,11 @@ yesno()
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
return 0
esac
-   if [ "$_" -gt 1 ]; then
+   if [ "$_" -ne 1 ] || ! is_identifier "$1"; then
! break
else
-   # Using eval can be very dangerous. Check whether the
-   # value is a legitimate variable name before proceeding
-   # to treat it as one.
-   (
-   LC_ALL=C
-   case $1 in
-   ''|_|[[:digit:]]*|*[!_[:alnum:]]*) exit 
1
-   esac
-   ) || ! break
-   # Treat the value as a nameref then try again.
+   # 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)"
@@ -471,6 +463,17 @@ _is_visible() {
! case $1 in *[[:graph:]]*) false ;; esac
 }
 
+#
+#   Determine whether the first operand is a valid identifier (variable name).
+#
+is_identifier()
+(
+   LC_ALL=C
+   case $1 in
+   ''|_|[[:digit:]]*|*[!_[:alnum:]]*) false
+   esac
+)
+
 # This is the main script, please add all functions above this point!
 # shellcheck disable=2034
 RC_GOT_FUNCTIONS="yes"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: 9e0d9b555da6866d366bfcb8cb40f3e4c4e79a2b
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 06:46:12 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:06 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9e0d9b55

Drop -nc as a supported argument for disabling color

https://bugs.gentoo.org/599792#c1 went unnoted at the time, but the
commenter was quite right. It is just silly. Get rid of it.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/functions.sh b/functions.sh
index 3b56b1d..fbc0812 100644
--- a/functions.sh
+++ b/functions.sh
@@ -488,7 +488,7 @@ genfun_indent=
 for _ in "$@"; do
case $_ in
# Check whether the user specifed an argument to disable color.
-   --nocolor|--nocolour|-nc|-C)
+   --nocolor|--nocolour|-C)
RC_NOCOLOR="yes"
break
esac



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-19 Thread Sam James
commit: efdf0a4d0a889eb1549163b32a3a1c68521e4c42
Author: Kerin Millar  plushkava  net>
AuthorDate: Sat Feb 18 05:53:23 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Sun Feb 19 16:14:05 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=efdf0a4d

Declare RC_GOT_FUNCTIONS after having declared all functions, not before

There is no sense in claiming that the functions have been gotten until
such time as the shell has successfully evaluated their declarations.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/functions.sh b/functions.sh
index 01f2972..2918558 100644
--- a/functions.sh
+++ b/functions.sh
@@ -2,9 +2,6 @@
 # Distributed under the terms of the GNU General Public License v2
 # shellcheck shell=sh disable=3043
 
-# shellcheck disable=2034
-RC_GOT_FUNCTIONS="yes"
-
 #
 #This is a private function, called by ebegin, eerrorn, einfon, and ewarnn.
 #
@@ -473,6 +470,8 @@ _is_visible() {
 }
 
 # This is the main script, please add all functions above this point!
+# shellcheck disable=2034
+RC_GOT_FUNCTIONS="yes"
 
 # Dont output to stdout?
 EINFO_QUIET="${EINFO_QUIET:-no}"



[gentoo-commits] proj/gentoo-functions:master commit in: /

2023-02-16 Thread Sam James
commit: 9b955a2693fe679b03005128867525f4b8363a22
Author: Kerin Millar  plushkava  net>
AuthorDate: Fri Feb 17 04:45:13 2023 +
Commit: Sam James  gentoo  org>
CommitDate: Fri Feb 17 07:38:33 2023 +
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9b955a26

Only join the positional parameters where necessary in _eend()

Also, don't join them twice.

Signed-off-by: Kerin Millar  plushkava.net>
Signed-off-by: Sam James  gentoo.org>

 functions.sh | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/functions.sh b/functions.sh
index 816acf4..01f2972 100644
--- a/functions.sh
+++ b/functions.sh
@@ -237,8 +237,11 @@ _eend()
 
if [ "${retval}" -ne 0 ]; then
# If a message was given, print it with the specified function.
-   if _is_visible "$*"; then
-   "${efunc}" "$*"
+   if [ "$#" -gt 0 ]; then
+   msg=$*
+   if _is_visible "${msg}"; then
+   "${efunc}" "${msg}"
+   fi
fi
# Generate an indicator for ebegin's unsuccessful conclusion.
if [ "${is_tty}" -eq 0 ]; then



  1   2   >