changeset 563:0ab71eddcfd7

farray.sh: change the quoting responsibilities: the decoration is now provided by the called function also
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 12 Sep 2024 00:01:46 +0200
parents 3fb59bd978c0
children b379e27cc583
files share/local-bsdtools/farray.sh
diffstat 1 files changed, 27 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Wed Sep 11 22:03:46 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Thu Sep 12 00:01:46 2024 +0200
@@ -102,11 +102,16 @@
 
 
 #:
-#: Quote the given input to be safely used in evals with "Dollar-Single Quotes"
+#: Quote the given input using "Dollar-Single-Quotes" to be safely used in
+#: evals.
 #:
 #: Args:
 #:   $1: The value to be quoted.
 #:
+#: Output (stdout):
+#:   The properly quoted string including surrounding "Dollar-Single-Quotes"
+#:   (e.g. $'...').
+#:
 #:
 #: From FreeBSD's :manpage:`sh(1)`:
 #:
@@ -125,6 +130,18 @@
 #:                Literal single-quote
 #:
 _farr_quote_for_eval_dsq() {
+    printf "\$'%s'" "$(_farr_inner_quote_for_dsq "${1}")"
+}
+
+
+#:
+#: Helper to quote for "Dollar-Single-Quotes".
+#:
+#: This function handles just the quoting mechanics. It does not surround
+#: the result with any other string decoration.
+#: See also `_farr_quote_for_eval_dsq`.
+#:
+_farr_inner_quote_for_dsq() {
     printf "%s" "${1}" \
         | LC_ALL=C /usr/bin/sed -e $'s/\\\\/\\\\\\\\/g' -e $'s/\'/\\\\\'/g'
     #                              escape a backslash      escape a single quote
@@ -312,7 +329,7 @@
         # Set value Escape properly: use $' ' and escape any
         # backslashes and single quotes.
         #
-        eval ${__farr_gvrname}_${__farr_len_1}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_newval}")"\'
+        eval ${__farr_gvrname}_${__farr_len_1}="$(_farr_quote_for_eval_dsq "${__farr_newval}")"
         # the implementation below line does not escape properly
         #   eval ${__farr_gvrname}_${__farr_len_1}="\"${__farr_value}\""
 
@@ -353,12 +370,12 @@
     # For proper quoting: see farray_append
     if [ \( ${__farr_index} -ge 1 \) -a \( ${__farr_index} -le ${__farr_len} \) ]; then
         # replace a value at an existing index
-        eval ${__farr_gvrname}_${__farr_index}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
+        eval ${__farr_gvrname}_${__farr_index}="$(_farr_quote_for_eval_dsq "${__farr_value}")"
     else
         __farr_len_1=$((__farr_len + 1))
         if [ ${__farr_index} -eq ${__farr_len_1} ]; then
             # append value
-            eval ${__farr_gvrname}_${__farr_len_1}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
+            eval ${__farr_gvrname}_${__farr_len_1}="$(_farr_quote_for_eval_dsq "${__farr_value}")"
             # and set new length
             eval ${__farr_gvrname}__=${__farr_len_1}
         else
@@ -470,7 +487,7 @@
     while [ ${__farr_idx} -lt ${__farr_len} ]; do
         # copy the following value to the current index
         eval __farr_value=\"\$\{${__farr_gvrname}_${__farr_idx_1}\}\"
-        eval ${__farr_gvrname}_${__farr_idx}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
+        eval ${__farr_gvrname}_${__farr_idx}="$(_farr_quote_for_eval_dsq "${__farr_value}")"
         __farr_idx=$((__farr_idx + 1))
         __farr_idx_1=$((__farr_idx + 1))
     done
@@ -695,7 +712,7 @@
     __farr_join_idx=1
     while [ ${__farr_join_idx} -le ${__farr_len} ]; do
 	eval __farr_current_value=\"\$\{${__farr_gvrname}_${__farr_join_idx}\}\"
-	__farr_command="${__farr_command}${__farr_real_separator}\$'$(_farr_quote_for_eval_dsq "${__farr_current_value}")'"
+	__farr_command="${__farr_command}${__farr_real_separator}$(_farr_quote_for_eval_dsq "${__farr_current_value}")"
 	__farr_real_separator=' '
 	__farr_join_idx=$((__farr_join_idx + 1))
     done
@@ -731,7 +748,7 @@
         if [ ${__farr_join_idx} -gt 1 ]; then
             printf "%s" " "
         fi
-        printf "%s" "\$'$(_farr_quote_for_eval_dsq "${__farr_current_value}")'"
+        printf "%s" "$(_farr_quote_for_eval_dsq "${__farr_current_value}")"
 	__farr_join_idx=$((__farr_join_idx + 1))
     done
 }
@@ -1095,7 +1112,7 @@
         if [ -n "${__farr_elkey}" ]; then
             eval __farr_elkey=\"\$\{${__farr_keyname}_${__farr_idx}\}\"
             if [ "${__farr_elkey}" = "${__farr_key}" ]; then
-                eval ${__farr_valname}_${__farr_idx}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
+                eval ${__farr_valname}_${__farr_idx}="$(_farr_quote_for_eval_dsq "${__farr_value}")"
                 return 0
             fi
         else
@@ -1110,8 +1127,8 @@
     #
     __farr_len=${__farr_idx}
     #   ... the key/value pairs to storage
-    eval ${__farr_keyname}_${__farr_len}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_key}")"\'
-    eval ${__farr_valname}_${__farr_len}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
+    eval ${__farr_keyname}_${__farr_len}="$(_farr_quote_for_eval_dsq "${__farr_key}")"
+    eval ${__farr_valname}_${__farr_len}="$(_farr_quote_for_eval_dsq "${__farr_value}")"
     #   ... the new length
     eval ${__farr_objname}__=${__farr_len}
     return 0