summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/substvars.sh41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/substvars.sh b/src/substvars.sh
index 60d1402..36ec308 100644
--- a/src/substvars.sh
+++ b/src/substvars.sh
@@ -1,6 +1,6 @@
# Functions for setting and substituting variables
#
-# Copyright (C) 2012, 2014 Patrick "P. J." McDermott
+# Copyright (C) 2012, 2014, 2019 Patrick McDermott
#
# This file is part of the ProteanOS Development Kit.
#
@@ -19,36 +19,33 @@
# <http://www.gnu.org/licenses/>.
SUBSTVARS_MAX_DEPTH=50
+SUBSTVAR_TRIM_SED='
+ H; # Store each input line in the hold space.
+ ${ # At the last line of input:
+ g; # restore the hold space into the pattern space,
+ s/^[\n]*//; # remove leading newline characters,
+ s/[\n]*$//; # remove trailing newline characters, and
+ p; # print the results.
+ };
+'
set_substvar()
{
local name="${1}"
local value="${2}"
- # Convert variable name to lower case and validate.
- name="$(printf '%s\n' "${name}" | tr 'A-Z-' 'a-z_')"
- case "${name:- }" in
- *[!a-z0-9_]*)
- warn "$(get_msg 'substvar_invalid')" "${name}"
- return 1
- ;;
+ # Validate variable name and convert to lower case.
+ case "${name}" in *[!A-Za-z0-9-]* | '')
+ warn "$(get_msg 'substvar_invalid')" "${name}"
+ return 1
+ ;;
esac
+ name="$(printf '%s' "${name}" | tr 'A-Z-' 'a-z_')"
- # Trim leading and trailing newline characters from value.
- value="$(printf '%s\n' "${value}" | sed -n '
- H; # Store each input line in the hold space.
- ${ # At the last line of input:
- g; # Restore the hold space into the pattern space.
- s/^[\n]*//; # Remove leading newline characters.
- s/[\n]*$//; # Remove trailing newline characters.
- p; # Print the results.
- };
- ')"
-
- # Escape single quotes in value.
- value="$(printf '%s\n' "${value}" | sed "s/'/'\\\\''/g")"
+ # Trim leading and trailing whitespace from value.
+ value="$(printf '%s' "${value}" | sed -n "${SUBSTVAR_TRIM_SED}")"
- eval "substvar_${name}='${value}'"
+ eval "substvar_${name}=\"\${value}\""
return 0
}