Skip to content

Commit

Permalink
Merge pull request #861 from scop/fix/variable-quoting
Browse files Browse the repository at this point in the history
fix: variable quoting fixes
  • Loading branch information
scop authored Dec 26, 2022
2 parents d460505 + 4407656 commit 16729d0
Show file tree
Hide file tree
Showing 112 changed files with 334 additions and 317 deletions.
1 change: 0 additions & 1 deletion .shellcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ disable=SC2207 # suggested alternatives fail in posix mode or use temp files
# These disables are to be investigated and decided

disable=SC2016
disable=SC2086
disable=SC2155
70 changes: 39 additions & 31 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ _comp_have_command()
{
# Completions for system administrator commands are installed as well in
# case completion is attempted via `sudo command ...'.
PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type $1 &>/dev/null
PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type "$1" &>/dev/null
}

_comp_deprecate_func _have _comp_have_command
Expand All @@ -132,7 +132,7 @@ _comp_deprecate_func _have _comp_have_command
have()
{
unset -v have
_comp_have_command $1 && have=yes
_comp_have_command "$1" && have=yes
}

# This function checks whether a given readline variable
Expand Down Expand Up @@ -274,10 +274,11 @@ _upvar()
echo "bash_completion: $FUNCNAME: deprecated function," \
"use _upvars instead" >&2
if unset -v "$1"; then # Unset & validate varname
# shellcheck disable=SC2140 # TODO
if (($# == 2)); then
eval $1=\"\$2\" # Return single value
eval "$1"=\"\$2\" # Return single value
else
eval $1=\(\"\$"{@:2}"\"\) # Return array
eval "$1"=\(\"\$"{@:2}"\"\) # Return array
fi
fi
}
Expand Down Expand Up @@ -312,8 +313,8 @@ _upvars()
return 1
}
# Assign array of -aN elements
# shellcheck disable=SC2015 # TODO
[[ $2 ]] && unset -v "$2" && eval $2=\(\"\$"{@:3:${1#-a}}"\"\) &&
# shellcheck disable=SC2015,SC2140 # TODO
[[ $2 ]] && unset -v "$2" && eval "$2"=\(\"\$"{@:3:${1#-a}}"\"\) &&
shift $((${1#-a} + 2)) || {
echo bash_completion: \
"$FUNCNAME: \`$1${2+ }$2': missing argument(s)" \
Expand All @@ -324,7 +325,7 @@ _upvars()
-v)
# Assign single value
# shellcheck disable=SC2015 # TODO
[[ $2 ]] && unset -v "$2" && eval $2=\"\$3\" &&
[[ $2 ]] && unset -v "$2" && eval "$2"=\"\$3\" &&
shift 3 || {
echo "bash_completion: $FUNCNAME: $1:" \
"missing argument(s)" >&2
Expand Down Expand Up @@ -559,8 +560,8 @@ __get_cword_at_cursor_by_ref()
((index < 0)) && index=0
fi

local "$2" "$3" "$4" && _upvars -a${#words[@]} $2 ${words+"${words[@]}"} \
-v $3 "$cword" -v $4 "${cur:0:index}"
local "$2" "$3" "$4" && _upvars -a${#words[@]} "$2" ${words+"${words[@]}"} \
-v "$3" "$cword" -v "$4" "${cur:0:index}"
}

# Get the word to complete and optional previous words.
Expand Down Expand Up @@ -771,9 +772,9 @@ _quote_readline_by_ref()
{
if [[ $1 == \'* ]]; then
# Leave out first character
printf -v $2 %s "${1:1}"
printf -v "$2" %s "${1:1}"
else
printf -v $2 %q "$1"
printf -v "$2" %q "$1"

# If result becomes quoted like this: $'string', re-evaluate in order
# to drop the additional quoting. See also:
Expand Down Expand Up @@ -831,7 +832,7 @@ _filedir()

reset=$(shopt -po noglob)
set -o noglob
toks+=($(compgen "${opts[@]}" -- $quoted))
toks+=($(compgen "${opts[@]}" -- "$quoted"))
IFS=' '
$reset
IFS=$'\n'
Expand All @@ -841,7 +842,7 @@ _filedir()
$arg && ${#toks[@]} -lt 1 ]] && {
reset=$(shopt -po noglob)
set -o noglob
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- $quoted))
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- "$quoted"))
IFS=' '
$reset
IFS=$'\n'
Expand Down Expand Up @@ -883,8 +884,8 @@ _variables()
# Completing $var / ${var / ${!var / ${#var
if [[ $cur == '${'* ]]; then
local arrs vars
vars=($(compgen -A variable -P ${BASH_REMATCH[1]} -S '}' -- ${BASH_REMATCH[3]}))
arrs=($(compgen -A arrayvar -P ${BASH_REMATCH[1]} -S '[' -- ${BASH_REMATCH[3]}))
vars=($(compgen -A variable -P "${BASH_REMATCH[1]}" -S '}' -- "${BASH_REMATCH[3]}"))
arrs=($(compgen -A arrayvar -P "${BASH_REMATCH[1]}" -S '[' -- "${BASH_REMATCH[3]}"))
if ((${#vars[@]} == 1 && ${#arrs[@]} != 0)); then
# Complete ${arr with ${array[ if there is only one match, and that match is an array variable
compopt -o nospace
Expand All @@ -902,7 +903,7 @@ _variables()
# Complete ${array[i with ${array[idx]}
local reset=$(shopt -po noglob) IFS=$'\n'
set -o noglob
COMPREPLY+=($(compgen -W '$(printf %s\\n "${!'${BASH_REMATCH[2]}'[@]}")' \
COMPREPLY+=($(compgen -W '$(printf %s\\n "${!'"${BASH_REMATCH[2]}"'[@]}")' \
-P "${BASH_REMATCH[1]}${BASH_REMATCH[2]}[" -S ']}' -- "${BASH_REMATCH[3]}"))
IFS=$' \t\n'
$reset
Expand Down Expand Up @@ -1101,7 +1102,7 @@ _comp_initialize()
;;
esac
cur=${cur##"$redir"}
_filedir $xspec
_filedir "$xspec"
return 1
fi

Expand Down Expand Up @@ -1187,7 +1188,10 @@ _parse_help()
(
case $cmd in
-) exec cat ;;
*) _comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---help} 2>&1 ;;
*)
# shellcheck disable=SC2086
_comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---help} 2>&1
;;
esac
) |
while read -r line; do
Expand Down Expand Up @@ -1224,7 +1228,10 @@ _parse_usage()
(
case $cmd in
-) exec cat ;;
*) _comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---usage} 2>&1 ;;
*)
# shellcheck disable=SC2086
_comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---usage} 2>&1
;;
esac
) |
while read -r line; do
Expand All @@ -1237,7 +1244,7 @@ _parse_usage()
# Treat as bundled short options
for ((i = 1; i < ${#option}; i++)); do
char=${option:i:1}
[[ $char != '[' ]] && printf '%s\n' -$char && rc=0
[[ $char != '[' ]] && printf '%s\n' -"$char" && rc=0
done
;;
*)
Expand Down Expand Up @@ -1392,7 +1399,7 @@ _ncpus()
local var=NPROCESSORS_ONLN
[[ $OSTYPE == *@(linux|msys|cygwin)* ]] && var=_$var
local n=$(getconf $var 2>/dev/null)
printf %s ${n:-1}
printf %s "${n:-1}"
}

# Perform tilde (~) completion
Expand All @@ -1409,7 +1416,7 @@ _tilde()
# 2>/dev/null for direct invocation, e.g. in the _tilde unit test
((result > 0)) && compopt -o filenames 2>/dev/null
fi
return $result
return "$result"
}

# Expand variable starting with tilde (~)
Expand Down Expand Up @@ -1437,7 +1444,7 @@ _tilde()
__expand_tilde_by_ref()
{
if [[ ${!1-} == \~* ]]; then
eval $1="$(printf ~%q "${!1#\~}")"
eval "$1"="$(printf ~%q "${!1#\~}")"
fi
} # __expand_tilde_by_ref()

Expand Down Expand Up @@ -1662,7 +1669,7 @@ _modules()
{
local modpath
modpath=/lib/modules/$1
COMPREPLY=($(compgen -W "$(command ls -RL $modpath 2>/dev/null |
COMPREPLY=($(compgen -W "$(command ls -RL "$modpath" 2>/dev/null |
command sed -ne 's/^\(.*\)\.k\{0,1\}o\(\.[gx]z\)\{0,1\}$/\1/p' \
-e 's/^\(.*\)\.ko\.zst$/\1/p')" -- "$cur"))
}
Expand Down Expand Up @@ -1938,6 +1945,7 @@ _known_hosts()
local options
[[ ${1-} == -a || ${2-} == -a ]] && options=-a
[[ ${1-} == -c || ${2-} == -c ]] && options+=" -c"
# shellcheck disable=SC2086
_known_hosts_real ${options-} -- "$cur"
} # _known_hosts()

Expand Down Expand Up @@ -1977,7 +1985,7 @@ _included_ssh_config_files()
if [[ -r $f && ! -d $f ]]; then
config+=("$f")
# The Included file is processed to look for Included files in itself
_included_ssh_config_files $f
_included_ssh_config_files "$f"
fi
done
fi
Expand Down Expand Up @@ -2240,7 +2248,7 @@ _cd()
for i in ${CDPATH//:/$'\n'}; do
# create an array of matched subdirs
k=${#COMPREPLY[@]}
for j in $(compgen -d -- $i/$cur); do
for j in $(compgen -d -- "$i/$cur"); do
if [[ ($mark_symdirs && -L $j || $mark_dirs && ! -L $j) && ! -d ${j#"$i/"} ]]; then
j+="/"
fi
Expand Down Expand Up @@ -2365,7 +2373,7 @@ _comp_command_offset()
# FIXME: should we take "+o opt" into account?
cspec=${cspec#*-o }
opt=${cspec%% *}
compopt -o $opt
compopt -o "$opt"
cspec=${cspec#"$opt"}
done
else
Expand Down Expand Up @@ -2457,7 +2465,7 @@ _longopt()
COMPREPLY=($(compgen -W "$(LC_ALL=C $1 --help 2>&1 |
while read -r line; do
[[ $line =~ --[A-Za-z0-9]+([-_][A-Za-z0-9]+)*=? ]] &&
printf '%s\n' ${BASH_REMATCH[0]}
printf '%s\n' "${BASH_REMATCH[0]}"
done)" -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
elif [[ $1 == *@(rmdir|chroot) ]]; then
Expand Down Expand Up @@ -2490,7 +2498,7 @@ _filedir_xspec()
toks=($(
compgen -d -- "$(quote_readline "$cur")" | {
while read -r tmp; do
printf '%s\n' $tmp
printf '%s\n' "$tmp"
done
}
))
Expand All @@ -2509,7 +2517,7 @@ _filedir_xspec()
toks+=($(
eval compgen -f -X "'!$xspec'" -- '$(quote_readline "$cur")' | {
while read -r tmp; do
[[ $tmp ]] && printf '%s\n' $tmp
[[ $tmp ]] && printf '%s\n' "$tmp"
done
}
))
Expand Down Expand Up @@ -2750,7 +2758,7 @@ unset -v _comp__init_compat_dir _comp__init_file
# garbages of suppressed command outputs.
_comp__init_user_file=${BASH_COMPLETION_USER_FILE:-~/.bash_completion}
[[ $_comp__init_user_file != "${BASH_SOURCE[0]}" && $_comp__init_user_file != /dev/null && -r $_comp__init_user_file && -f $_comp__init_user_file ]] &&
. $_comp__init_user_file
. "$_comp__init_user_file"
unset -v _comp__init_user_file
unset -f have
Expand Down
2 changes: 1 addition & 1 deletion completions/2to3
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _2to3()
;;
-f | --fix | -x | --nofix)
COMPREPLY=($(compgen -W \
"$($1 --list-fixes 2>/dev/null | command sed -e 1d)" -- "$cur"))
"$("$1" --list-fixes 2>/dev/null | command sed -e 1d)" -- "$cur"))
return
;;
-j | --processes)
Expand Down
18 changes: 9 additions & 9 deletions completions/7z
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _7z()

case $cur in
-ao*)
COMPREPLY=($(compgen -P${cur:0:3} -W 'a s t u' -- "${cur:3}"))
COMPREPLY=($(compgen -P"${cur:0:3}" -W 'a s t u' -- "${cur:3}"))
return
;;
-?(a)[ix]*)
Expand All @@ -26,7 +26,7 @@ _7z()
opt=${cur:0:2} cur=${cur:2}
fi
if [[ $cur != *[@\!]* ]]; then
COMPREPLY=($(compgen -P$opt -W '@ ! r@ r-@ r0@ r! r-! r0!' \
COMPREPLY=($(compgen -P"$opt" -W '@ ! r@ r-@ r0@ r! r-! r0!' \
-- "$cur"))
elif [[ $cur == ?(r@(-|0|))@* ]]; then
local IFS=$' \t\n' reset=$(shopt -po noglob)
Expand All @@ -50,31 +50,31 @@ _7z()
set -o noglob
compopt -o filenames
local IFS=$'\n'
COMPREPLY=($(compgen -d -P${cur:0:2} -S/ -- "${cur:2}"))
COMPREPLY=($(compgen -d -P"${cur:0:2}" -S/ -- "${cur:2}"))
_comp_unlocal IFS
$reset
compopt -o nospace
return
;;
-r?*)
COMPREPLY=($(compgen -P${cur:0:2} -W '- 0' -- "${cur:2}"))
COMPREPLY=($(compgen -P"${cur:0:2}" -W '- 0' -- "${cur:2}"))
return
;;
-scs*)
COMPREPLY=($(compgen -P${cur:0:4} -W 'UTF-8 WIN DOS' \
COMPREPLY=($(compgen -P"${cur:0:4}" -W 'UTF-8 WIN DOS' \
-- "${cur:4}"))
return
;;
-ssc?*)
COMPREPLY=($(compgen -P${cur:0:4} -W '-' -- "${cur:4}"))
COMPREPLY=($(compgen -P"${cur:0:4}" -W '-' -- "${cur:4}"))
return
;;
-t*)
if [[ $mode == w ]]; then
COMPREPLY=($(compgen -P${cur:0:2} -W '7z bzip2 gzip swfc
COMPREPLY=($(compgen -P"${cur:0:2}" -W '7z bzip2 gzip swfc
tar wim xz zip' -- "${cur:2}"))
else
COMPREPLY=($(compgen -P${cur:0:2} -W '7z apm arj bzip2 cab
COMPREPLY=($(compgen -P"${cur:0:2}" -W '7z apm arj bzip2 cab
chm cpio cramfs deb dmg elf fat flv gzip hfs iso lzh lzma
lzma86 macho mbr mslz mub nsis ntfs pe ppmd rar rpm
squashfs swf swfc tar udf vhd wim xar xz z zip' \
Expand Down Expand Up @@ -114,7 +114,7 @@ _7z()
else
if [[ ${words[1]} == d ]]; then
local IFS=$'\n'
COMPREPLY=($(compgen -W "$(printf '%s\n' "$($1 l ${words[2]} \
COMPREPLY=($(compgen -W "$(printf '%s\n' "$("$1" l "${words[2]}" \
-slt 2>/dev/null | command sed -n '/^Path =/s/^Path = \(.*\)$/\1/p' \
2>/dev/null | tail -n+2)")" -- "$cur"))
compopt -o filenames
Expand Down
4 changes: 2 additions & 2 deletions completions/_adb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _adb()
tmp+=($(compgen -W '$(_parse_help "$1" help)' -- "$cur"))
fi
if [[ ! $cur || $cur != -* ]]; then
tmp+=($($1 help 2>&1 | awk '$1 == "adb" { print $2 }'))
tmp+=($("$1" help 2>&1 | awk '$1 == "adb" { print $2 }'))
tmp+=(devices connect disconnect sideload)
fi
((${#tmp[@]})) &&
Expand All @@ -49,7 +49,7 @@ _adb()

# TODO: more and better command completions

_adb_command_usage "$1" $cmd
_adb_command_usage "$1" "$cmd"

case $cmd in
push | restore | sideload)
Expand Down
2 changes: 1 addition & 1 deletion completions/_mock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _mock()
return
;;
-r | --root)
COMPREPLY=($(compgen -W "$(command ls $cfgdir)" -- "$cur"))
COMPREPLY=($(compgen -W "$(command ls "$cfgdir")" -- "$cur"))
COMPREPLY=(${COMPREPLY[@]/%.cfg/})
return
;;
Expand Down
10 changes: 5 additions & 5 deletions completions/_modules
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

_module_list()
{
local modules="$(command sed 's/:/ /g' <<<$LOADEDMODULES | sort)"
compgen -W "$modules" -- $1
local modules="$(command sed 's/:/ /g' <<<"$LOADEDMODULES" | sort)"
compgen -W "$modules" -- "$1"
}

_module_path()
{
local modules="$(command sed 's/:/ /g' <<<$MODULEPATH | sort)"
compgen -W "$modules" -- $1
local modules="$(command sed 's/:/ /g' <<<"$MODULEPATH" | sort)"
compgen -W "$modules" -- "$1"
}

_module_avail()
Expand All @@ -41,7 +41,7 @@ _module_avail()
xargs printf '%s\n' | command sed -e 's/(default)//g' | sort
)"

compgen -W "$modules" -- $1
compgen -W "$modules" -- "$1"
}

# A completion function for the module alias
Expand Down
Loading

0 comments on commit 16729d0

Please sign in to comment.