stdenv-setup: Clean up 'substitute()' for style and error handling

It now blows up on null byte in file (rather than silently truncating),
and invalid arguments (rather than silently skipping).
This commit is contained in:
John Ericson 2017-07-12 11:56:43 -04:00
parent 5d4efb2c81
commit 5d693c84d2

View File

@ -421,50 +421,55 @@ fi
substitute() { substitute() {
local input="$1" local input=$1
local output="$2" local output=$2
shift 2
if [ ! -f "$input" ]; then if [ ! -f "$input" ]; then
echo "substitute(): file '$input' does not exist" echo "${FUNCNAME[0]}(): ERROR: file '$input' does not exist" >&2
return 1 return 1
fi fi
local -a params=("$@") local content
# read returns non-0 on EOF, so we want read to fail
local n p pattern replacement varName content if IFS='' read -r -N 0 content < "$input"; then
echo "${FUNCNAME[0]}(): ERROR: File \"$input\" has null bytes, won't process" >&2
# a slightly hacky way to keep newline at the end return 1
content="$(cat "$input"; printf "%s" X)"
content="${content%X}"
for ((n = 2; n < ${#params[*]}; n += 1)); do
p="${params[$n]}"
if [ "$p" = --replace ]; then
pattern="${params[$n + 1]}"
replacement="${params[$n + 2]}"
let n+=2
fi fi
if [ "$p" = --subst-var ]; then while (( "$#" )); do
varName="${params[$n + 1]}" case "$1" in
let n+=1 --replace)
pattern=$2
replacement=$3
shift 3
;;
--subst-var)
local varName=$2
shift 2
# check if the used nix attribute name is a valid bash name # check if the used nix attribute name is a valid bash name
if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
echo "WARNING: substitution variables should be valid bash names," echo "${FUNCNAME[0]}(): WARNING: substitution variables should be valid bash names," >&2
echo " \"$varName\" isn't and therefore was skipped; it might be caused" echo " \"$varName\" isn't and therefore was skipped; it might be caused" >&2
echo " by multi-line phases in variables - see #14907 for details." echo " by multi-line phases in variables - see #14907 for details." >&2
continue continue
fi fi
pattern="@$varName@" pattern=@$varName@
replacement="${!varName}" replacement=${!varName}
fi ;;
if [ "$p" = --subst-var-by ]; then --subst-var-by)
pattern="@${params[$n + 1]}@" pattern=@$2@
replacement="${params[$n + 2]}" replacement=$3
let n+=2 shift 3
fi ;;
*)
echo "${FUNCNAME[0]}(): ERROR: Invalid command line argument: $1" >&2
return 1
;;
esac
content="${content//"$pattern"/$replacement}" content="${content//"$pattern"/$replacement}"
done done