Gentoo Archives: gentoo-dev

From: Mike Frysinger <vapier@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] Re: evar_push/pop helpers
Date: Mon, 17 Jun 2013 05:42:16
Message-Id: 201306170142.15784.vapier@gentoo.org
In Reply to: [gentoo-dev] Re: evar_push/pop helpers by "Steven J. Long"
1 On Sunday 02 June 2013 13:38:04 Steven J. Long wrote:
2 > On Sat, Jun 01, 2013 at 11:03:20PM -0400, Mike Frysinger wrote:
3 > > --- eutils.eclass 22 May 2013 05:10:29 -0000 1.421
4 > > +++ eutils.eclass 2 Jun 2013 03:00:46 -0000
5 > > @@ -146,6 +146,77 @@ estack_pop() {
6 > > eval unset ${__estack_name}\[${__estack_i}\]
7 > > }
8 >
9 > Just in passing, one of the places you don't want nullglob messing things
10 > up.
11
12 not sure what you mean. that escapes the [] so that bash doesn't accidentally
13 glob things. when the code actually executes, you don't need to escape
14 things.
15
16 touch f i d x
17 unset arr
18 declare -A arr
19 arr[f]=1234
20 arr[idx]=4321
21 echo "${arr[f]}" "${arr[idx]}"
22 __estack_name=arr
23 __estack_i=f
24 eval unset ${__estack_name}\[${__estack_i}\]
25 echo ${#arr[@]}
26 __estack_i=idx
27 eval unset ${__estack_name}\[${__estack_i}\]
28 echo ${#arr[@]}
29
30 seems to work
31
32 > > +# is not specified, the var will be unset.
33 > > +evar_push_set() {
34 > > + local var=$1
35 > > + evar_push ${var}
36 > > + case $# in
37 > > + 1) unset ${var} ;;
38 > > + 2) eval ${var}=\$2 ;;
39 >
40 > I wish you wouldn't use eval for this. I know it's technically okay here,
41 > or would be if you verified the parameter, but bash has printf -v for this
42 > purpose:
43
44 interesting, i hadn't seen that before ... looks new to bash-3.1. /me tucks
45 that into his tool belt.
46
47 although it doesn't quite work in the edge case where the value is an empty
48 string. consider:
49 unset x
50 printf -v x ''
51 echo ${x+set}
52
53 that should show "set", but it does not. i'll have to keep `eval ${var}=`
54 when the value we're setting is empty. or just keep the eval code since i
55 have to do eval anyways at that point.
56
57 i'll report it upstream to the bash guys.
58
59 > printf -v "$1" '%s' "$2" 2>/dev/null || die "unable to set: '$1' to: '$2'"
60 >
61 > Note you should verify the variable name, ime, irrespective of a die on the
62 > printf (or eval in sh.) It's much better feedback to the developer using
63 > the routine.
64
65 i don't think it's worth it. if you screw up the name, it tends to be a one-
66 time cost, and the error shown is pretty clear as to where it's coming from.
67
68 > printf -v also works with array members btw, if you do decide to extend in
69 > that direction:
70
71 i don't think i will, but i probably can convert the other core stack code to
72 use this ...
73
74 > > + : ${cnt:=bad}
75 > > + [[ -n ${cnt//[0-9]} ]] && die "${FUNCNAME}: first arg must be a
76 > > number: $*"
77 > > + ;;
78 >
79 > Though a generic is_int function comes in much handier, ime.
80
81 yeah, and we have other code that wants this (a simple grep for 0-9 in eclass/
82 shows a bunch of hits)
83
84 > > - # Some people like to make dirs of patches w/out suffixes (vim)
85 > > + # We have to force sorting to C so that the wildcard expansion
86 > > # is consistent #471666.
87 > > + evar_push_set LC_COLLATE C
88 > > + # Some people like to make dirs of patches w/out suffixes (vim).
89 > > set -- "$1"/*${EPATCH_SUFFIX:+."${EPATCH_SUFFIX}"}
90 > > + evar_pop
91 >
92 > Have to say I'd just do this in a function adding to a locally-scoped
93 > array, if it were me, eg local args; foo "$1" "$EPATCH_SUFFIX"; set --
94 > "${args[@]}"; unset args
95 > foo() {
96 > local LC_COLLATE=C
97 > args=("$1"/*${2:+."$2"})
98 > }
99
100 since i plan on using these funcs in other places, using the existing api
101 keeps things simple
102
103 > though I'd prefer it if EPATCH_SUFFIX were allowed to be a list,
104 > personally.
105
106 wouldn't really make this any easier
107 -mike

Attachments

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

Replies

Subject Author
Re: [gentoo-dev] Re: evar_push/pop helpers Mike Frysinger <vapier@g.o>