diff options
author | Mike Frysinger <vapier@gentoo.org> | 2011-12-16 23:38:41 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2011-12-16 23:38:41 +0000 |
commit | 3a86c8d85d318aee421ccd8b11702765dd0582e8 (patch) | |
tree | e62a8ab77ffc5d0027971e3feb7ae9332186a002 /eclass | |
parent | Bump polkit dep. (diff) | |
download | gentoo-2-3a86c8d85d318aee421ccd8b11702765dd0582e8.tar.gz gentoo-2-3a86c8d85d318aee421ccd8b11702765dd0582e8.tar.bz2 gentoo-2-3a86c8d85d318aee421ccd8b11702765dd0582e8.zip |
add new generic stack helpers estack_{push,pop}, rebase eshopts_{push,pop} on top of those, and then add new helpers for messing with the umask sanely
Diffstat (limited to 'eclass')
-rw-r--r-- | eclass/eutils.eclass | 92 |
1 files changed, 78 insertions, 14 deletions
diff --git a/eclass/eutils.eclass b/eclass/eutils.eclass index 4f243478af76..8435fd35f73c 100644 --- a/eclass/eutils.eclass +++ b/eclass/eutils.eclass @@ -1,6 +1,6 @@ # Copyright 1999-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.372 2011/12/14 17:36:18 vapier Exp $ +# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.373 2011/12/16 23:38:41 vapier Exp $ # @ECLASS: eutils.eclass # @MAINTAINER: @@ -100,6 +100,54 @@ esvn_clean() { find "$@" -type d -name '.svn' -prune -print0 | xargs -0 rm -rf } +# @FUNCTION: estack_push +# @USAGE: <stack> [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="__ESTACK_$1__" ; shift + eval ${stack_name}+=\( \"\$@\" \) +} + +# @FUNCTION: estack_pop +# @USAGE: <stack> [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 arguments" + + # 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="__ESTACK_$1__" ; shift + local __estack_retvar=$1 ; shift + eval local __estack_i=\${#${__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}=\"\${${__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 - local i=${#__ESHOPTS_SAVE__[@]} if [[ $1 == -[su] ]] ; then - __ESHOPTS_SAVE__[$i]=$(shopt -p) + estack_push eshopts "$(shopt -p)" [[ $# -eq 0 ]] && return 0 - shopt "$@" || die "eshopts_push: bad options to shopt: $*" + shopt "$@" || die "${FUNCNAME}: bad options to shopt: $*" else - __ESHOPTS_SAVE__[$i]=$- + estack_push eshopts $- [[ $# -eq 0 ]] && return 0 - set "$@" || die "eshopts_push: bad options to set: $*" + set "$@" || die "${FUNCNAME}: bad options to set: $*" fi } @@ -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() { - [[ $# -ne 0 ]] && die "eshopts_pop takes no arguments" - local i=$(( ${#__ESHOPTS_SAVE__[@]} - 1 )) - [[ ${i} -eq -1 ]] && die "eshopts_{push,pop}: unbalanced pair" - local s=${__ESHOPTS_SAVE__[$i]} - unset __ESHOPTS_SAVE__[$i] + local s + estack_pop eshopts s || die "${FUNCNAME}: unbalanced push" if [[ ${s} == "shopt -"* ]] ; then - eval "${s}" || die "eshopts_pop: sanity: invalid shopt options: ${s}" + eval "${s}" || die "${FUNCNAME}: 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 settings: ${s}" + set +$- || die "${FUNCNAME}: sanity: invalid shell settings: $-" + set -${s} || die "${FUNCNAME}: sanity: unable to restore saved shell settings: ${s}" fi } +# @FUNCTION: eumask_push +# @USAGE: <new umask> +# @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. |