commit:     f97465ca0bf8134f811c3a89af25693a2802c31b
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Mon Feb 13 05:20:25 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Feb 13 21:35:54 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f97465ca

Improve the __end(), eend() and ewend() functions

The _eend() function has a poor calling convention because, while both
eend() and ewend() always pass a function name, _eend() expects for it
to be the second argument. A better design would be for mandatory
arguments to precede optional ones. Make it so.

Also, implement a parameter validation routine for the optionally
specified exit status code. It determines whether the parameter is in
the form of an integer and whether its value is greater than or equal
to 0. Should the parameter be considered invalid, a diagnostic message
will be printed to STDERR and its value will be taken as if it were 0.
Note that negative values are not necessarily tolerated by builtins such
as return and exit. As is shown below, they may cause sh(1) to exit.

$ dash -c 'return -1; echo done'
dash: 1: return: Illegal number: -1

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

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

diff --git a/functions.sh b/functions.sh
index 38c49fd..26a4d7a 100644
--- a/functions.sh
+++ b/functions.sh
@@ -242,10 +242,22 @@ ebegin()
 #
 _eend()
 {
-       local retval="${1:-0}" efunc="${2:-eerror}" msg
-       shift 2
+       local efunc msg retval
+
+       efunc=$1
+       shift
+       if [ "$#" -eq 0 ]; then
+               retval=0
+       elif ! is_int "$1" || [ "$1" -lt 0 ]; then
+               printf 'Invalid argument given to _eend (the exit status code 
must be an integer >= 0)\n' >&2
+               retval=0
+               shift
+       else
+               retval=$1
+               shift
+       fi
 
-       if [ "${retval}" != "0" ]; then
+       if [ "${retval}" -ne 0 ]; then
                if _is_visible "$*"; then
                        "${efunc}" "$*"
                fi
@@ -272,11 +284,10 @@ _eend()
 #
 eend()
 {
-       local retval="${1:-0}"
-       [ $# -eq 0 ] || shift
-
-       _eend "${retval}" eerror "$*"
+       local retval
 
+       _eend error "$@"
+       retval=$?
        LAST_E_CMD="eend"
        return "${retval}"
 }
@@ -287,11 +298,10 @@ eend()
 #
 ewend()
 {
-       local retval="${1:-0}"
-       [ $# -eq 0 ] || shift
-
-       _eend "${retval}" ewarn "$*"
+       local retval
 
+       _eend ewarn "$@"
+       retval=$?
        LAST_E_CMD="ewend"
        return "${retval}"
 }

Reply via email to