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 C24AA1381F3 for ; Sun, 2 Jun 2013 03:03:28 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A6C65E085B; Sun, 2 Jun 2013 03:03:22 +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 AAF6FE07F9 for ; Sun, 2 Jun 2013 03:03:21 +0000 (UTC) Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id D7BB833E0D0 for ; Sun, 2 Jun 2013 03:03:20 +0000 (UTC) From: Mike Frysinger Organization: wh0rd.org To: gentoo-dev@lists.gentoo.org Subject: [gentoo-dev] evar_push/pop helpers Date: Sat, 1 Jun 2013 23:03:20 -0400 User-Agent: KMail/1.13.7 (Linux/3.8.3; KDE/4.6.5; x86_64; ; ) 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="nextPart1483890.gPKc5sJPY9"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201306012303.21261.vapier@gentoo.org> X-Archives-Salt: 9f3ea7f1-5df8-4fc5-93d0-8f195f038dfa X-Archives-Hash: a34842bc76cd6b80d8eaa3813498936b --nextPart1483890.gPKc5sJPY9 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable simple set of helpers to save/restore a variable in a limited section of co= de you can see an example of it in action at the end of the file where i need = to tweak epatch (and no, doing `LC_COLLATE=3DC set -- ....` does not work). =2Dmike =2D-- eutils.eclass 22 May 2013 05:10:29 -0000 1.421 +++ eutils.eclass 2 Jun 2013 03:00:46 -0000 @@ -146,6 +146,77 @@ 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. +# +# 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} ;; + 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 + case $# in + 0) cnt=3D1 ;; + 1) + : ${cnt:=3Dbad} + [[ -n ${cnt//[0-9]} ]] && 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" + [[ ${val} =3D=3D "${___ECLASS_ONCE_EUTILS}" ]] \ + && unset ${var} \ + || eval ${var}=3D\${val} + done +} + # @FUNCTION: eshopts_push # @USAGE: [options to `set` or `shopt`] # @DESCRIPTION: @@ -344,8 +415,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 --nextPart1483890.gPKc5sJPY9 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) iQIcBAABAgAGBQJRqrX5AAoJEEFjO5/oN/WBr40QAMu9gljbkmFmRzSGt1SDfLNz A1ZRivulLvrie9IQO5cOZDw6W4f0P23m1Lw8fSj2X2oC7AUDC1TmahwAYC9PQbFx VnFXqLc8MwnWiIlG3mh4DtMqsbe2xvLn69dFt8ANXCT+koq2tAYGVn2dekXJlnVg Lq0Yqe78LUSVKkfHnNExZvUmDGfQVA0ZmYp7nt5fJIqOayHBiEWpRDqjZFBroim1 hrmO76u5V2y9MNTTh/QjQXNO5ugOdnEDIvR1B7UdkSmSeyJu51EsKiGeixw31Pff Wk421VVXxObvwNap6lBKzZ3PDa1SBv7mynhkr88fkeSH5frnzY72Fkn4n0M55AzM 3B7ZPS7Guuv4P1b4gsoTOWgAUsmdmW00MyacWTEakUZ0c5m0CnIsmn06dYtdDv9o MP4/VUX76yf6pZ+Z+LX7paMv+NAVo62F6Z3oEZYmqB89keEW7XfEmzKe9gTZWpnj DjdCh08dRFKMoq8vGYPz8DmSh0hTrnTQdMI+qxci/8B8Ghwnbu0w0qotXOSvPTgJ Q92WDxDDt4iOt6evT08LHsKHuVU4lUYD/IcHGh2vvSh/GSNOrNvl92mHpOfxYTrx utUYbwLvlFFdT3NT3YZ8xN/biFtPY5sD5vcowwph1wEAdJR3J/WQB6+Ct8eyp7c4 mfZt8bOtLhliv86ne1h2 =Dn0H -----END PGP SIGNATURE----- --nextPart1483890.gPKc5sJPY9--