[pacman-dev] [PATCH] libmakepkg: increase robustness of the detection of array variables

Allan McRae allan at archlinux.org
Sun Feb 7 11:30:42 UTC 2016


Extract array detection into its own utility function that ensures
extglob is enabled.

Suggested-by: Dave Reisner <dreisner at archlinux.org>
Signed-off-by: Allan McRae <allan at archlinux.org>
---
 scripts/libmakepkg/lint_pkgbuild/variable.sh.in |  6 +++---
 scripts/libmakepkg/util/util.sh                 | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/scripts/libmakepkg/lint_pkgbuild/variable.sh.in b/scripts/libmakepkg/lint_pkgbuild/variable.sh.in
index 183e740..30c9909 100644
--- a/scripts/libmakepkg/lint_pkgbuild/variable.sh.in
+++ b/scripts/libmakepkg/lint_pkgbuild/variable.sh.in
@@ -43,7 +43,7 @@ lint_variable() {
 	for i in ${array[@]} ${arch_array[@]}; do
 		eval "keys=(\"\${!$i[@]}\")"
 		if (( ${#keys[*]} > 0 )); then
-				if [[ "$(declare -p $i)" != "declare -a "* ]]; then
+			if ! is_array $i; then
 				error "$(gettext "%s should be an array")" "$i"
 				ret=1
 			fi
@@ -57,7 +57,7 @@ lint_variable() {
 			v="${i}_${a}"
 			eval "keys=(\"\${!${v}[@]}\")"
 			if (( ${#keys[*]} > 0 )); then
-				if [[ "$(declare -p $v)" != "declare -a "* ]]; then
+				if ! is_array $v; then
 					error "$(gettext "%s_%s should be an array")" "$i" "$a"
 					ret=1
 				fi
@@ -68,7 +68,7 @@ lint_variable() {
 	for i in ${string[@]}; do
 		eval "keys=(\"\${!$i[@]}\")"
 		if (( ${#keys[*]} > 0 )); then
-			if [[ "$(declare -p $i)" == "declare -a "* ]]; then
+			if is_array $i; then
 				error "$(gettext "%s should not be an array")" "$i"
 				ret=1
 			fi
diff --git a/scripts/libmakepkg/util/util.sh b/scripts/libmakepkg/util/util.sh
index 3c996da..675e75d 100644
--- a/scripts/libmakepkg/util/util.sh
+++ b/scripts/libmakepkg/util/util.sh
@@ -37,6 +37,23 @@ in_array() {
 	return 1 # Not Found
 }
 
+# tests if a variable is an array
+is_array() {
+	local v=$1
+	local ret=1
+
+	# this function requires extglob - save current options to restore later
+	local shellopts=$(shopt -p)
+	shopt -s extglob
+
+	if [[ $(declare -p "$i") == declare\ -*([[:alnum:]])a*([[:alnum:]])\ * ]]; then
+		ret=0
+	fi
+
+	eval "$shellopts"
+	return $ret
+}
+
 # Canonicalize a directory path if it exists
 canonicalize_path() {
 	local path="$1";
-- 
2.7.0


More information about the pacman-dev mailing list