makeWrapper: quote paths

Fixes https://github.com/NixOS/nixpkgs/pull/22962#commitcomment-21144939

Also run ShellCheck.
This commit is contained in:
Nikolay Amiantov 2017-03-05 13:56:30 +03:00 committed by Robin Gloster
parent da6619cffe
commit 7ff6eec5fd

View File

@ -19,98 +19,100 @@
# are read first and used as VALS # are read first and used as VALS
# --suffix-contents # --suffix-contents
makeWrapper() { makeWrapper() {
local original=$1 local original="$1"
local wrapper=$2 local wrapper="$2"
local params varName value command separator n fileNames local params varName value command separator n fileNames
local argv0 flagsBefore flags extraFlagsArray local argv0 flagsBefore flags
mkdir -p "$(dirname $wrapper)" mkdir -p "$(dirname "$wrapper")"
echo "#! $SHELL -e" > $wrapper echo "#! $SHELL -e" > "$wrapper"
params=("$@") params=("$@")
for ((n = 2; n < ${#params[*]}; n += 1)); do for ((n = 2; n < ${#params[*]}; n += 1)); do
p=${params[$n]} p="${params[$n]}"
if test "$p" = "--set"; then if test "$p" = "--set"; then
varName=${params[$((n + 1))]} varName="${params[$((n + 1))]}"
value=${params[$((n + 2))]} value="${params[$((n + 2))]}"
n=$((n + 2)) n=$((n + 2))
echo "export $varName=\"$value\"" >> $wrapper echo "export $varName=\"$value\"" >> "$wrapper"
fi fi
if test "$p" = "--unset"; then if test "$p" = "--unset"; then
varName=${params[$((n + 1))]} varName="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
echo "unset $varName" >> "$wrapper" echo "unset $varName" >> "$wrapper"
fi fi
if test "$p" = "--run"; then if test "$p" = "--run"; then
command=${params[$((n + 1))]} command="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
echo "$command" >> $wrapper echo "$command" >> "$wrapper"
fi fi
if test "$p" = "--suffix" -o "$p" = "--prefix"; then if test "$p" = "--suffix" -o "$p" = "--prefix"; then
varName=${params[$((n + 1))]} varName="${params[$((n + 1))]}"
separator=${params[$((n + 2))]} separator="${params[$((n + 2))]}"
value=${params[$((n + 3))]} value="${params[$((n + 3))]}"
n=$((n + 3)) n=$((n + 3))
if test -n "$value"; then if test -n "$value"; then
if test "$p" = "--suffix"; then if test "$p" = "--suffix"; then
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> $wrapper echo "export $varName=\$$varName\${$varName:+$separator}$value" >> "$wrapper"
else else
echo "export $varName=$value\${$varName:+$separator}\$$varName" >> $wrapper echo "export $varName=$value\${$varName:+$separator}\$$varName" >> "$wrapper"
fi fi
fi fi
fi fi
if test "$p" = "--suffix-each"; then if test "$p" = "--suffix-each"; then
varName=${params[$((n + 1))]} varName="${params[$((n + 1))]}"
separator=${params[$((n + 2))]} separator="${params[$((n + 2))]}"
values=${params[$((n + 3))]} values="${params[$((n + 3))]}"
n=$((n + 3)) n=$((n + 3))
for value in $values; do for value in $values; do
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> $wrapper echo "export $varName=\$$varName\${$varName:+$separator}$value" >> "$wrapper"
done done
fi fi
if test "$p" = "--suffix-contents" -o "$p" = "--prefix-contents"; then if test "$p" = "--suffix-contents" -o "$p" = "--prefix-contents"; then
varName=${params[$((n + 1))]} varName="${params[$((n + 1))]}"
separator=${params[$((n + 2))]} separator="${params[$((n + 2))]}"
fileNames=${params[$((n + 3))]} fileNames="${params[$((n + 3))]}"
n=$((n + 3)) n=$((n + 3))
for fileName in $fileNames; do for fileName in $fileNames; do
if test "$p" = "--suffix-contents"; then if test "$p" = "--suffix-contents"; then
echo "export $varName=\$$varName\${$varName:+$separator}$(cat $fileName)" >> $wrapper echo "export $varName=\$$varName\${$varName:+$separator}$(cat "$fileName")" >> "$wrapper"
else else
echo "export $varName=$(cat $fileName)\${$varName:+$separator}\$$varName" >> $wrapper echo "export $varName=$(cat "$fileName")\${$varName:+$separator}\$$varName" >> "$wrapper"
fi fi
done done
fi fi
if test "$p" = "--add-flags"; then if test "$p" = "--add-flags"; then
flags=${params[$((n + 1))]} flags="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
flagsBefore="$flagsBefore $flags" flagsBefore="$flagsBefore $flags"
fi fi
if test "$p" = "--argv0"; then if test "$p" = "--argv0"; then
argv0=${params[$((n + 1))]} argv0="${params[$((n + 1))]}"
n=$((n + 1)) n=$((n + 1))
fi fi
done done
# Note: extraFlagsArray is an array containing additional flags # Note: extraFlagsArray is an array containing additional flags
# that may be set by --run actions. # that may be set by --run actions.
echo exec ${argv0:+-a $argv0} "$original" \ # Silence warning about unexpanded extraFlagsArray:
$flagsBefore '"${extraFlagsArray[@]}"' '"$@"' >> $wrapper # shellcheck disable=SC2016
echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \
"$flagsBefore" '"${extraFlagsArray[@]}"' '"$@"' >> "$wrapper"
chmod +x $wrapper chmod +x "$wrapper"
} }
addSuffix() { addSuffix() {
suffix=$1 suffix="$1"
shift shift
for name in "$@"; do for name in "$@"; do
echo "$name$suffix" echo "$name$suffix"
@ -128,7 +130,10 @@ filterExisting() {
# Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...> # Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...>
wrapProgram() { wrapProgram() {
local prog="$1" local prog="$1"
local hidden="$(dirname "$prog")/.$(basename "$prog")"-wrapped local hidden
mv $prog $hidden hidden="$(dirname "$prog")/.$(basename "$prog")"-wrapped
makeWrapper $hidden $prog --argv0 '"$0"' "$@" mv "$prog" "$hidden"
# Silence warning about unexpanded $0:
# shellcheck disable=SC2016
makeWrapper "$hidden" "$prog" --argv0 '$0' "$@"
} }