From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 878AA158011 for ; Mon, 13 Feb 2023 21:37:29 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id CB55EE07EF; Mon, 13 Feb 2023 21:37:28 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A9451E07EF for ; Mon, 13 Feb 2023 21:37:28 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C5CE2340B10 for ; Mon, 13 Feb 2023 21:37:27 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id B74998B3 for ; Mon, 13 Feb 2023 21:37:24 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1676324154.f97465ca0bf8134f811c3a89af25693a2802c31b.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: f97465ca0bf8134f811c3a89af25693a2802c31b X-VCS-Branch: master Date: Mon, 13 Feb 2023 21:37:24 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 2cf682ae-edc3-47df-ac35-3737564da8c9 X-Archives-Hash: a59e48ee1539efa030a7207778854c1e commit: f97465ca0bf8134f811c3a89af25693a2802c31b Author: Kerin Millar plushkava net> AuthorDate: Mon Feb 13 05:20:25 2023 +0000 Commit: Sam James gentoo 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 plushkava.net> Signed-off-by: Sam James 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}" }