public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Subject: Re: [gentoo-dev] evar_push/pop helpers
Date: Mon, 17 Jun 2013 01:46:02 -0400	[thread overview]
Message-ID: <201306170146.03611.vapier@gentoo.org> (raw)
In-Reply-To: <201306012303.21261.vapier@gentoo.org>

[-- Attachment #1: Type: Text/Plain, Size: 3499 bytes --]

here's v2
-mike

--- eutils.eclass	22 May 2013 05:10:29 -0000	1.421
+++ eutils.eclass	17 Jun 2013 05:41:58 -0000
@@ -146,6 +146,79 @@ estack_pop() {
 	eval unset ${__estack_name}\[${__estack_i}\]
 }
 
+# @FUNCTION: evar_push
+# @USAGE: <variable to save> [more vars to save]
+# @DESCRIPTION:
+# This let's you temporarily modify a variable and then restore it (including
+# set vs unset semantics).  Arrays are not supported at this time.
+#
+# This is meant for variables where using `local` does not work (such as
+# exported variables, or only temporarily changing things in a func).
+#
+# For example:
+# @CODE
+#		evar_push LC_ALL
+#		export LC_ALL=C
+#		... do some stuff that needs LC_ALL=C 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} == "set" ]] \
+			&& val=${!var} \
+			|| val="${___ECLASS_ONCE_EUTILS}"
+		estack_push evar "${var}" "${val}"
+	done
+}
+
+# @FUNCTION: evar_push_set
+# @USAGE: <variable to save> [new value to store]
+# @DESCRIPTION:
+# This is a handy shortcut to save and temporarily set a variable.  If a value
+# is not specified, the var will be unset.
+evar_push_set() {
+	local var=$1
+	evar_push ${var}
+	case $# in
+	1) unset ${var} ;;
+	# Can't use `printf -v` as that does not set $var when $2 is "".
+	2) eval ${var}=\$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=${1:-bad}
+	case $# in
+	0) cnt=1 ;;
+	1) isdigit "${cnt}" || 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"
+		# Can't use `printf -v` as that does not set $var when $2 is "".
+		[[ ${val} == "${___ECLASS_ONCE_EUTILS}" ]] \
+			&& unset ${var} \
+			|| eval ${var}=\${val}
+	done
+}
+
 # @FUNCTION: eshopts_push
 # @USAGE: [options to `set` or `shopt`]
 # @DESCRIPTION:
@@ -218,6 +291,18 @@ eumask_pop() {
 	umask ${s} || die "${FUNCNAME}: sanity: could not restore umask: ${s}"
 }
 
+# @FUNCTION: isdigit
+# @USAGE: <number> [more numbers]
+# @DESCRIPTION:
+# Return true if all arguments are numbers.
+isdigit() {
+	local d
+	for d ; do
+		[[ ${d:-bad} == *[!0-9]* ]] && return 1
+	done
+	return 0
+}
+
 # @VARIABLE: EPATCH_SOURCE
 # @DESCRIPTION:
 # Default directory to search for patches.
@@ -344,8 +429,11 @@ epatch() {
 		local EPATCH_SUFFIX=$1
 
 	elif [[ -d $1 ]] ; then
-		# 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 consistent #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
 
 	elif [[ -f ${EPATCH_SOURCE}/$1 ]] ; then
 		# Re-use EPATCH_SOURCE as a search dir

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

  parent reply	other threads:[~2013-06-17  5:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-02  3:03 [gentoo-dev] evar_push/pop helpers Mike Frysinger
2013-06-02  6:51 ` Michał Górny
2013-06-02  7:09   ` Mike Frysinger
2013-06-02  7:16     ` Michał Górny
2013-06-02  7:29       ` Mike Frysinger
2013-06-02  7:48         ` Tom Wijsman
2013-06-17  5:45           ` Mike Frysinger
2013-06-02  8:39         ` Michał Górny
2013-06-02 15:40           ` Mike Frysinger
2013-06-02 15:57             ` Andreas K. Huettel
2013-06-02  7:33     ` Tom Wijsman
2013-06-02 17:38 ` [gentoo-dev] " Steven J. Long
2013-06-17  5:42   ` Mike Frysinger
2013-06-17 16:06     ` Mike Frysinger
2013-06-17  5:46 ` Mike Frysinger [this message]
2013-06-17 17:51   ` [gentoo-dev] " Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201306170146.03611.vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox