Gentoo Archives: gentoo-dev

From: Mike Frysinger <vapier@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] evar_push/pop helpers
Date: Sun, 02 Jun 2013 03:03:28
Message-Id: 201306012303.21261.vapier@gentoo.org
1 simple set of helpers to save/restore a variable in a limited section of code
2
3 you can see an example of it in action at the end of the file where i need to
4 tweak epatch (and no, doing `LC_COLLATE=C set -- ....` does not work).
5 -mike
6
7 --- eutils.eclass 22 May 2013 05:10:29 -0000 1.421
8 +++ eutils.eclass 2 Jun 2013 03:00:46 -0000
9 @@ -146,6 +146,77 @@ estack_pop() {
10 eval unset ${__estack_name}\[${__estack_i}\]
11 }
12
13 +# @FUNCTION: evar_push
14 +# @USAGE: <variable to save> [more vars to save]
15 +# @DESCRIPTION:
16 +# This let's you temporarily modify a variable and then restore it (including
17 +# set vs unset semantics). Arrays are not supported at this time.
18 +#
19 +# For example:
20 +# @CODE
21 +# evar_push LC_ALL
22 +# export LC_ALL=C
23 +# ... do some stuff that needs LC_ALL=C set ...
24 +# evar_pop
25 +#
26 +# # You can also save/restore more than one var at a time
27 +# evar_push BUTTERFLY IN THE SKY
28 +# ... do stuff with the vars ...
29 +# evar_pop # This restores just one var, SKY
30 +# ... do more stuff ...
31 +# evar_pop 3 # This pops the remaining 3 vars
32 +# @CODE
33 +evar_push() {
34 + local var val
35 + for var ; do
36 + [[ ${!var+set} == "set" ]] \
37 + && val=${!var} \
38 + || val="${___ECLASS_ONCE_EUTILS}"
39 + estack_push evar "${var}" "${val}"
40 + done
41 +}
42 +
43 +# @FUNCTION: evar_push_set
44 +# @USAGE: <variable to save> [new value to store]
45 +# @DESCRIPTION:
46 +# This is a handy shortcut to save and temporarily set a variable. If a value
47 +# is not specified, the var will be unset.
48 +evar_push_set() {
49 + local var=$1
50 + evar_push ${var}
51 + case $# in
52 + 1) unset ${var} ;;
53 + 2) eval ${var}=\$2 ;;
54 + *) die "${FUNCNAME}: incorrect # of args: $*" ;;
55 + esac
56 +}
57 +
58 +# @FUNCTION: evar_pop
59 +# @USAGE: [number of vars to restore]
60 +# @DESCRIPTION:
61 +# Restore the variables to the state saved with the corresponding
62 +# evar_push call. See that function for more details.
63 +evar_pop() {
64 + local cnt=$1
65 + case $# in
66 + 0) cnt=1 ;;
67 + 1)
68 + : ${cnt:=bad}
69 + [[ -n ${cnt//[0-9]} ]] && die "${FUNCNAME}: first arg must be a number: $*"
70 + ;;
71 + *) die "${FUNCNAME}: only accepts one arg: $*" ;;
72 + esac
73 +
74 + local var val
75 + while (( cnt-- )) ; do
76 + estack_pop evar val || die "${FUNCNAME}: unbalanced push"
77 + estack_pop evar var || die "${FUNCNAME}: unbalanced push"
78 + [[ ${val} == "${___ECLASS_ONCE_EUTILS}" ]] \
79 + && unset ${var} \
80 + || eval ${var}=\${val}
81 + done
82 +}
83 +
84 # @FUNCTION: eshopts_push
85 # @USAGE: [options to `set` or `shopt`]
86 # @DESCRIPTION:
87 @@ -344,8 +415,11 @@ epatch() {
88 local EPATCH_SUFFIX=$1
89
90 elif [[ -d $1 ]] ; then
91 - # Some people like to make dirs of patches w/out suffixes (vim)
92 + # We have to force sorting to C so that the wildcard expansion is consistent #471666.
93 + evar_push_set LC_COLLATE C
94 + # Some people like to make dirs of patches w/out suffixes (vim).
95 set -- "$1"/*${EPATCH_SUFFIX:+."${EPATCH_SUFFIX}"}
96 + evar_pop
97
98 elif [[ -f ${EPATCH_SOURCE}/$1 ]] ; then
99 # Re-use EPATCH_SOURCE as a search dir

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
Re: [gentoo-dev] evar_push/pop helpers "Michał Górny" <mgorny@g.o>
[gentoo-dev] Re: evar_push/pop helpers "Steven J. Long" <slong@××××××××××××××××××.uk>
Re: [gentoo-dev] evar_push/pop helpers Mike Frysinger <vapier@g.o>