From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Rayah-0008Jc-Fz for garchives@archives.gentoo.org; Wed, 14 Dec 2011 23:50:27 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 07E6F21C118; Wed, 14 Dec 2011 23:50:18 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 05AB621C064 for ; Wed, 14 Dec 2011 23:49:49 +0000 (UTC) Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 6D8B41B402C for ; Wed, 14 Dec 2011 23:49:49 +0000 (UTC) From: Mike Frysinger Organization: wh0rd.org To: gentoo-dev@lists.gentoo.org Subject: Re: [gentoo-dev] estack_{push,pop}: cool new helpers or over engineering? Date: Wed, 14 Dec 2011 18:49:49 -0500 User-Agent: KMail/1.13.7 (Linux/3.1.0-atsc; KDE/4.6.5; x86_64; ; ) References: <201112141725.22148.vapier@gentoo.org> In-Reply-To: <201112141725.22148.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="nextPart1572439.mLreQqAl5e"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <201112141849.50101.vapier@gentoo.org> X-Archives-Salt: 0874b9cb-0d16-4ead-9f2e-8efadc0a6b66 X-Archives-Hash: 193bc7db5f69702a92087d0c09f797db --nextPart1572439.mLreQqAl5e Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable some people pointed out typos/bugs, so here's v2 =2Dmike =2D-- eutils.eclass 14 Dec 2011 17:36:18 -0000 1.372 +++ eutils.eclass 14 Dec 2011 23:46:37 -0000 @@ -100,6 +100,54 @@ esvn_clean() { find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf } =20 +# @FUNCTION: estack_push +# @USAGE: [items to push] +# @DESCRIPTION: +# Push any number of items onto the specified stack. Pick a name that +# is a valid variable (i.e. stick to alphanumerics), and push as many +# items as you like onto the stack at once. +# +# The following code snippet will echo 5, then 4, then 3, then ... +# @CODE +# estack_push mystack 1 2 3 4 5 +# while estack_pop mystack i ; do +# echo ${i} +# done +# @CODE +estack_push() { + [[ $# -eq 0 ]] && die "estack_push: incorrect # of arguments" + local stack_name=3D"__ESTACK_$1__" ; shift + eval ${stack_name}+=3D\( \"\$@\" \) +} + +# @FUNCTION: estack_pop +# @USAGE: [variable] +# @DESCRIPTION: +# Pop a single item off the specified stack. If a variable is specified, +# the popped item is stored there. If no more items are available, return +# 1, else return 0. See estack_push for more info. +estack_pop() { + ( [[ $# -eq 0 ]] || [[ $# -gt 2 ]] ) && die "estack_pop: incorrect # of a= rguments" + + # We use the fugly __estack_xxx var names to avoid collision with + # passing back the return value. If we used "local i" and the + # caller ran `estack_pop ... i`, we'd end up setting the local + # copy of "i" rather than the caller's copy. The __estack_xxx + # garbage is preferable to using $1/$2 everywhere as that is a + # bit harder to read. + local __estack_name=3D"__ESTACK_$1__" ; shift + local __estack_retvar=3D$1 ; shift + eval local __estack_i=3D\${#${__estack_name}[@]} + # Don't warn -- let the caller interpret this as a failure + # or as normal behavior (akin to `shift`) + [[ $(( --__estack_i )) -eq -1 ]] && return 1 + + if [[ -n ${__estack_retvar} ]] ; then + eval ${__estack_retvar}=3D\"\${${__estack_name}[${__estack_i}]}\" + fi + eval unset ${__estack_name}[${__estack_i}] +} + # @FUNCTION: eshopts_push # @USAGE: [options to `set` or `shopt`] # @DESCRIPTION: @@ -126,15 +174,14 @@ esvn_clean() { eshopts_push() { # have to assume __ESHOPTS_SAVE__ isn't screwed with # as a `declare -a` here will reset its value =2D local i=3D${#__ESHOPTS_SAVE__[@]} if [[ $1 =3D=3D -[su] ]] ; then =2D __ESHOPTS_SAVE__[$i]=3D$(shopt -p) + estack_push eshopts "$(shopt -p)" [[ $# -eq 0 ]] && return 0 shopt "$@" || die "eshopts_push: bad options to shopt: $*" else =2D __ESHOPTS_SAVE__[$i]=3D$- + estack_push eshopts $- [[ $# -eq 0 ]] && return 0 set "$@" || die "eshopts_push: bad options to set: $*" fi } =20 @@ -144,19 +191,36 @@ eshopts_push() { # Restore the shell options to the state saved with the corresponding # eshopts_push call. See that function for more details. eshopts_pop() { =2D [[ $# -ne 0 ]] && die "eshopts_pop takes no arguments" =2D local i=3D$(( ${#__ESHOPTS_SAVE__[@]} - 1 )) =2D [[ ${i} -eq -1 ]] && die "eshopts_{push,pop}: unbalanced pair" =2D local s=3D${__ESHOPTS_SAVE__[$i]} =2D unset __ESHOPTS_SAVE__[$i] + local s + estack_pop eshopts s || die "${FUNCNAME}: unbalanced push" if [[ ${s} =3D=3D "shopt -"* ]] ; then eval "${s}" || die "eshopts_pop: sanity: invalid shopt options: ${s}" else set +$- || die "eshopts_pop: sanity: invalid shell settings: $-" set -${s} || die "eshopts_pop: sanity: unable to restore saved shell s= ettings: ${s}" fi } =20 +# @FUNCTION: eumask_push +# @USAGE: +# @DESCRIPTION: +# Set the umask to the new value specified while saving the previous +# value onto a stack. Useful for temporarily changing the umask. +eumask_push() { + estack_push eumask "$(umask)" + umask "$@" || die "${FUNCNAME}: bad options to umask: $*" +} + +# @FUNCTION: eumask_pop +# @USAGE: +# @DESCRIPTION: +# Restore the previous umask state. +eumask_pop() { + local s + estack_pop eumask s || die "${FUNCNAME}: unbalanced push" + umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}" +} + # @VARIABLE: EPATCH_SOURCE # @DESCRIPTION: # Default directory to search for patches. --nextPart1572439.mLreQqAl5e 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) iQIcBAABAgAGBQJO6TYeAAoJEEFjO5/oN/WBSvcP+wSFkPpe8naBWjvcpzx8rEyz Uzd7a5k0Hhowi69tVmVCqMv0O8pSMmvE11Fg850dVRX+/bowdCK70B4QhZl9dRZW /V1fkD4sDWMdEYhmMxaALwr0If4013JQ+8AoGJF5uRmTlhvof0ZPosXqsIA+kp67 Yd6woq5YiPV2Wa/mA9QD91TmeWKDuMvDY85JD9pbTRVn4DIDVAKpDg0foE7TY7LR hpI5yqxf6rvoQGsTj9WDb+et+4cxUBpN07fgwZLn9tIdEtKVKt33zL1KOJDwwdUb ggDD+TVHwMnVuCJpImDDg191FMwpvEt91L2HMhKsYC70WSrW04RXdu5fB9NKxPc/ 55lRAUXKrC5Ext0EJIsmf+Q2jEkRFN7Enl7+iylk+C1xSNXgyrXlLV11A5Rpvn+P CdycCU9JTmQS5S4VQa3YLYHAnkWUcExaq5p7HTpYLD5pkKB5/nbbPwa8qLbOvwnY 20ZD+KZOzMwfT6+CrXcM9zLRzETcRcCOjO6YOXxhA5Q1Y/ZI0vE1U/8n6o0w3DVx 7N8+IbJeIDSQgz7ozSgc1Xlx3n+U8LA6fFUzuJgZCAra3NS7IEVZdKUupF1b/jZm 9OXQ45lAbiqdIi5D+8JtH23+x+Mo0MK9BB0hoIFctgzIzA8Rp16mwOtnMnLtdo27 a7EXwrZhqkBSRxP7kdSP =BL/9 -----END PGP SIGNATURE----- --nextPart1572439.mLreQqAl5e--