From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 77F591381F3 for ; Mon, 17 Jun 2013 05:46:10 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 58F7AE08EA; Mon, 17 Jun 2013 05:45:59 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id E714AE0856 for ; Mon, 17 Jun 2013 05:45:57 +0000 (UTC) Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 1D33033BDD3 for ; Mon, 17 Jun 2013 05:45:57 +0000 (UTC) From: Mike Frysinger Organization: wh0rd.org To: gentoo-dev@lists.gentoo.org Subject: Re: [gentoo-dev] evar_push/pop helpers Date: Mon, 17 Jun 2013 01:46:02 -0400 User-Agent: KMail/1.13.7 (Linux/3.8.3; KDE/4.6.5; x86_64; ; ) References: <201306012303.21261.vapier@gentoo.org> In-Reply-To: <201306012303.21261.vapier@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart9208213.if76TKVWPY"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201306170146.03611.vapier@gentoo.org> X-Archives-Salt: 0e04822e-16f0-4a32-a099-e048096567ce X-Archives-Hash: 0849929a4d88e74edb8f7153907b33f4 --nextPart9208213.if76TKVWPY Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable here's v2 =2Dmike =2D-- eutils.eclass 22 May 2013 05:10:29 -0000 1.421 +++ eutils.eclass 17 Jun 2013 05:41:58 -0000 @@ -146,6 +146,79 @@ estack_pop() { eval unset ${__estack_name}\[${__estack_i}\] } =20 +# @FUNCTION: evar_push +# @USAGE: [more vars to save] +# @DESCRIPTION: +# This let's you temporarily modify a variable and then restore it (includ= ing +# set vs unset semantics). Arrays are not supported at this time. +# +# This is meant for variables where using `local` does not work (such as +# exported variables, or only temporarily changing things in a func). +# +# For example: +# @CODE +# evar_push LC_ALL +# export LC_ALL=3DC +# ... do some stuff that needs LC_ALL=3DC set ... +# evar_pop +# +# # You can also save/restore more than one var at a time +# evar_push BUTTERFLY IN THE SKY +# ... do stuff with the vars ... +# evar_pop # This restores just one var, SKY +# ... do more stuff ... +# evar_pop 3 # This pops the remaining 3 vars +# @CODE +evar_push() { + local var val + for var ; do + [[ ${!var+set} =3D=3D "set" ]] \ + && val=3D${!var} \ + || val=3D"${___ECLASS_ONCE_EUTILS}" + estack_push evar "${var}" "${val}" + done +} + +# @FUNCTION: evar_push_set +# @USAGE: [new value to store] +# @DESCRIPTION: +# This is a handy shortcut to save and temporarily set a variable. If a v= alue +# is not specified, the var will be unset. +evar_push_set() { + local var=3D$1 + evar_push ${var} + case $# in + 1) unset ${var} ;; + # Can't use `printf -v` as that does not set $var when $2 is "". + 2) eval ${var}=3D\$2 ;; + *) die "${FUNCNAME}: incorrect # of args: $*" ;; + esac +} + +# @FUNCTION: evar_pop +# @USAGE: [number of vars to restore] +# @DESCRIPTION: +# Restore the variables to the state saved with the corresponding +# evar_push call. See that function for more details. +evar_pop() { + local cnt=3D${1:-bad} + case $# in + 0) cnt=3D1 ;; + 1) isdigit "${cnt}" || die "${FUNCNAME}: first arg must be a number: $*" = ;; + *) die "${FUNCNAME}: only accepts one arg: $*" ;; + esac + + local var val + while (( cnt-- )) ; do + estack_pop evar val || die "${FUNCNAME}: unbalanced push" + estack_pop evar var || die "${FUNCNAME}: unbalanced push" + # Can't use `printf -v` as that does not set $var when $2 is "". + [[ ${val} =3D=3D "${___ECLASS_ONCE_EUTILS}" ]] \ + && unset ${var} \ + || eval ${var}=3D\${val} + done +} + # @FUNCTION: eshopts_push # @USAGE: [options to `set` or `shopt`] # @DESCRIPTION: @@ -218,6 +291,18 @@ eumask_pop() { umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}" } =20 +# @FUNCTION: isdigit +# @USAGE: [more numbers] +# @DESCRIPTION: +# Return true if all arguments are numbers. +isdigit() { + local d + for d ; do + [[ ${d:-bad} =3D=3D *[!0-9]* ]] && return 1 + done + return 0 +} + # @VARIABLE: EPATCH_SOURCE # @DESCRIPTION: # Default directory to search for patches. @@ -344,8 +429,11 @@ epatch() { local EPATCH_SUFFIX=3D$1 =20 elif [[ -d $1 ]] ; then =2D # Some people like to make dirs of patches w/out suffixes (vim) + # We have to force sorting to C so that the wildcard expansion is consis= tent #471666. + evar_push_set LC_COLLATE C + # Some people like to make dirs of patches w/out suffixes (vim). set -- "$1"/*${EPATCH_SUFFIX:+."${EPATCH_SUFFIX}"} + evar_pop =20 elif [[ -f ${EPATCH_SOURCE}/$1 ]] ; then # Re-use EPATCH_SOURCE as a search dir --nextPart9208213.if76TKVWPY Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQIcBAABAgAGBQJRvqKbAAoJEEFjO5/oN/WBB+4QALwzBwNTzsJVQLVuITUzdLLr 42Yaw+mUpSmAul28CZkcN2KRlBcmXQtuYbxxfG96v1jHT27L7/ZqciM7K+iS9LPz MDUG8H31b9oRZgOgJG4+H+M50q4yRRoF6mRWrRm+rwm1LX2r/po0JHGXRFabxuMh +3VLbWDFk3UXi17yVamu/JZIfdL60LlQBCqKQvLlYSYZYdFbZERLcy9vuklopz0X rAZM4TppM1lARTbH2aW4djAbp/EtO+wy7Z5hxdihIe2YCxN1YcrASnsg3kmgxbZy TxgCmv8YmoN3mU4Yg8RZNomWUSie/4VgxPmzoC5wbMtOvGeYrPFNJJSJ9v0IrB8r Q1eyRVHBNoDKDg5OQ9Utg4oU2M9d5HW5YSivly9yAmq9MaAiJzT6NFOaJA26qILi C/+1sxbBSdTXcZN7k77pxj6lRCtWiqvsmuWal83Zr5fWgOLV8ftquJozB6bwEeGo w9lJZlfksPhwqiYZAU+FOV/RlFdhag/MELM0YeWceI/ML4zjwNaMUe5n88M845cG ZPVz/LEkcDz4Sm7eBJKP55P5UUcFLCYNaDd46O3s47Lng98cjcug5p6nBOz+ie6u QBm+JNuHuwMP4aBTgb6UtOfrJPnl8nCeBbD6RfL+ku7yonsB0WC/dGDqCgvjgvFl gNedS2kXT+Kypq+sjoKd =j6Tb -----END PGP SIGNATURE----- --nextPart9208213.if76TKVWPY--