From patchwork Thu May 31 16:24:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Alexander Steffens X-Patchwork-Id: 566 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id EF26247F878D for ; Thu, 31 May 2018 16:25:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on apollo X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_ADSP_CUSTOM_MED=0.001, DKIM_SIGNED=0.1,FREEMAIL_FROM=0.5,MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,T_DKIM_INVALID=1 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Thu, 31 May 2018 16:25:35 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 1BB08ADE369ED; Thu, 31 May 2018 16:25:31 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [5.9.250.164]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS; Thu, 31 May 2018 16:25:31 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 011192C719; Thu, 31 May 2018 16:25:31 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SMjyff3+ Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 400902BE44 for ; Thu, 31 May 2018 16:25:28 +0000 (UTC) Received: from orion.archlinux.org (orion.archlinux.org [IPv6:2a01:4f8:160:6087::1]) by luna.archlinux.org (Postfix) with ESMTPS for ; Thu, 31 May 2018 16:25:28 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 0BE5FADE369E5 for ; Thu, 31 May 2018 16:25:27 +0000 (UTC) Received: from mail-wm0-x231.google.com (mail-wm0-x231.google.com [IPv6:2a00:1450:400c:c09::231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS for ; Thu, 31 May 2018 16:25:26 +0000 (UTC) Received: by mail-wm0-x231.google.com with SMTP id j15-v6so3611272wme.0 for ; Thu, 31 May 2018 09:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ao/JJze7tNxM1IFnZVDlECxMXIjsKV1RguKvl3UDoIE=; b=SMjyff3+3VCx0QYnvonY2rfV0LsWAmOzlUE0SHKP8qG/fD2PkAnbcTOkr1eQta4ZDH g/leoXhrdVeenhDR+vXqzMjNAEXJ2FSUiM4mxdimxX+PU4LZYKeJjPjCPt7FjeJa3+qH +39Zx2CTYwXTwXH6IpkNortO+I26HqHyQavuRRn2QeDp0aIkGWuUC1XnUlovukXunRrn qnI1LGK0nqXqaDRLl4C4jJcyiDr5G9CkYXZmMMdXEa4FHFy+L8HkiM93JSD5eskFeZEF phTAbdJJRp/kGj3M9v81rI6qnHOKQ9wvyeZMSuVrCWaQS0TwPANgji9qqbpJBXoT8GVE BE8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ao/JJze7tNxM1IFnZVDlECxMXIjsKV1RguKvl3UDoIE=; b=Ty57mZbz0WR8QEO+m21byWFBP0XEuLWnT3kjel46KejGSvsv2yGGjVf/eVIJ1f7S4i BEGBlxkMmDl5DRT+CBhz2umuPDauQxHQvVJOT63lebMbSaSCg4FWuRgWW2l/gVI7pcwL uiAPzFI7GP7H6j+f4QWtmykN+du00TTa9qOpZ0n+GXnt0l3hkKgwzpFDGFT8rQNOGXMS Gw6j6isiW5U1KOt/jnRyvkFFUVx41D8FXeb///hJxrpwTX9/2BEcsq+1yZ7Jb9rsBlw2 vTiKVTECDo/DH/q2j8rOUj4eEGPFyH9UtCuxH7Vc6KT7yqQhrxscBmP7epIVrxUWnK0Z qCGg== X-Gm-Message-State: ALKqPweUAuhrMUGUdIKaYbivVNimKs61tIEpW7DSEoul4mthSF72GBQX lxvdU/WYhRBXYoiocmbdXk3jcu4i X-Google-Smtp-Source: ADUXVKKqbWofUxEGVaV0pITOuxcNS5z37LMo9HtNOUPLwz6sX6gpkCVAmEDuSxrkWzIh0VqLxJGGkA== X-Received: by 2002:a1c:170f:: with SMTP id 15-v6mr395393wmx.90.1527783926415; Thu, 31 May 2018 09:25:26 -0700 (PDT) Received: from philomeena.fritz.box ([2001:470:6d:88a:6bec:9309:97:97c]) by smtp.gmail.com with ESMTPSA id v196-v6sm1685341wmf.36.2018.05.31.09.25.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 31 May 2018 09:25:25 -0700 (PDT) From: "Jan Alexander Steffens (heftig)" To: pacman-dev@archlinux.org Date: Thu, 31 May 2018 18:24:46 +0200 Message-Id: X-Mailer: git-send-email 2.17.0 Subject: [pacman-dev] [PATCH 01/10] libmakepkg/util/option: Refactor checking to reduce code duplication X-BeenThere: pacman-dev@archlinux.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Discussion list for pacman development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Discussion list for pacman development Errors-To: pacman-dev-bounces@archlinux.org Sender: "pacman-dev" Pull out the expected=y/n check into a separate function and make use of the fact we can just concatenate the fallback arrays to get the same result. --- scripts/libmakepkg/util/option.sh.in | 85 +++++++++------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/scripts/libmakepkg/util/option.sh.in b/scripts/libmakepkg/util/option.sh.in index 46e0568d..28357e27 100644 --- a/scripts/libmakepkg/util/option.sh.in +++ b/scripts/libmakepkg/util/option.sh.in @@ -48,95 +48,66 @@ in_opt_array() { } +## +# usage : check_opt_array( $option, $expected_val, $haystack ) +# return : 0 - matches expected +# 1 - does not match expected +# 127 - not found +## +check_opt_array() { + local option=$1 expected=$2; shift 2 + + in_opt_array "$option" "$@" + case $? in + 0) # assert enabled + [[ $expected = y ]] + return ;; + 1) # assert disabled + [[ $expected = n ]] + return ;; + esac + + # not found + return 127 +} + + ## # Checks to see if options are present in makepkg.conf or PKGBUILD; # PKGBUILD options always take precedence. # # usage : check_option( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_option() { - in_opt_array "$1" ${options[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # fall back to makepkg.conf options - in_opt_array "$1" ${OPTIONS[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # not found - return 127 + check_opt_array "$@" "${options[@]}" "${OPTIONS[@]}" } ## # Check if option is present in BUILDENV # # usage : check_buildenv( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_buildenv() { - in_opt_array "$1" ${BUILDENV[@]} - case $? in - 0) # assert enabled - [[ $2 = "y" ]] - return ;; - 1) # assert disabled - [[ $2 = "n" ]] - return ;; - esac - - # not found - return 127 + check_opt_array "$@" "${BUILDENV[@]}" } + ## # Checks to see if options are present in BUILDENV or PKGBUILD; # PKGBUILD options always take precedence. # # usage : check_buildoption( $option, $expected_val ) # return : 0 - matches expected # 1 - does not match expected # 127 - not found ## check_buildoption() { - in_opt_array "$1" ${options[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - in_opt_array "$1" ${BUILDENV[@]} - case $? in - 0) # assert enabled - [[ $2 = y ]] - return ;; - 1) # assert disabled - [[ $2 = n ]] - return - esac - - # not found - return 127 + check_opt_array "$@" "${options[@]}" "${BUILDENV[@]}" }