From 7ef055e48c966fc1171cd60f3e9f34a18d47f91d Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Tue, 23 Apr 2019 12:59:54 -0400 Subject: set_substvar(): Sync with opkbuild --- 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 @@ # . 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 } -- cgit v0.9.1