Skip to content

Commit 7a4dcfe

Browse files
committed
refactor(bash_completion): avoid [[ -v var ]] for localvar_inherit
1 parent a03809b commit 7a4dcfe

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

bash_completion

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ _comp_looks_like_path()
458458
#
459459
__reassemble_comp_words_by_ref()
460460
{
461-
local exclude i j line ref
462-
unset -v exclude # workaround for localvar_inherit
461+
local exclude="" i j line ref
463462
# Exclude word separator characters?
464463
if [[ $1 ]]; then
465464
# Yes, exclude word separator characters;
@@ -470,7 +469,7 @@ __reassemble_comp_words_by_ref()
470469
# Default to cword unchanged
471470
printf -v "$3" %s "$COMP_CWORD"
472471
# Are characters excluded which were former included?
473-
if [[ -v exclude ]]; then
472+
if [[ $exclude ]]; then
474473
# Yes, list of word completion separators has shrunk;
475474
line=$COMP_LINE
476475
# Re-assemble words to complete
@@ -594,13 +593,18 @@ __get_cword_at_cursor_by_ref()
594593
#
595594
_get_comp_words_by_ref()
596595
{
597-
local exclude flag i OPTIND=1
596+
local exclude="" flag i OPTIND=1
598597
local cur cword words=()
599-
local upargs=() upvars=() vcur vcword vprev vwords
600-
unset -v vcur vcword vprev vwords # workaround for localvar_inherit
598+
local upargs=() upvars=() vcur="" vcword="" vprev="" vwords=""
601599

602600
while getopts "c:i:n:p:w:" flag "$@"; do
603601
case $flag in
602+
[cipw])
603+
if [[ $OPTARG != [a-zA-Z_]*([a-zA-Z_0-9])?(\[*\]) ]]; then
604+
echo "bash_completion: $FUNCNAME: -$flag: invalid variable name \`$OPTARG'" >&2
605+
return 1
606+
fi
607+
;;&
604608
c) vcur=$OPTARG ;;
605609
i) vcword=$OPTARG ;;
606610
n) exclude=$OPTARG ;;
@@ -629,19 +633,19 @@ _get_comp_words_by_ref()
629633

630634
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
631635

632-
[[ -v vcur ]] && {
636+
[[ $vcur ]] && {
633637
upvars+=("$vcur")
634638
upargs+=(-v $vcur "$cur")
635639
}
636-
[[ -v vcword ]] && {
640+
[[ $vcword ]] && {
637641
upvars+=("$vcword")
638642
upargs+=(-v $vcword "$cword")
639643
}
640-
[[ -v vprev && $cword -ge 1 ]] && {
644+
[[ $vprev && $cword -ge 1 ]] && {
641645
upvars+=("$vprev")
642646
upargs+=(-v $vprev "${words[cword - 1]}")
643647
}
644-
[[ -v vwords ]] && {
648+
[[ $vwords ]] && {
645649
upvars+=("$vwords")
646650
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
647651
}
@@ -2010,10 +2014,9 @@ _included_ssh_config_files()
20102014
# @return Completions, starting with CWORD, are added to COMPREPLY[]
20112015
_known_hosts_real()
20122016
{
2013-
local configfile flag prefix=""
2014-
local cur suffix="" aliases i host ipv4 ipv6
2017+
local configfile="" flag prefix=""
2018+
local cur suffix="" aliases="" i host ipv4="" ipv6=""
20152019
local -a kh tmpkh=() khd=() config=()
2016-
unset -v configfile aliases ipv4 ipv6 # workaround for localvar_inherit
20172020

20182021
# TODO remove trailing %foo from entries
20192022

@@ -2022,7 +2025,13 @@ _known_hosts_real()
20222025
case $flag in
20232026
a) aliases='yes' ;;
20242027
c) suffix=':' ;;
2025-
F) configfile=$OPTARG ;;
2028+
F)
2029+
if [[ ! $OPTARG ]]; then
2030+
echo "bash_completion: $FUNCNAME: -F: an empty filename is specified" >&2
2031+
return 1
2032+
fi
2033+
configfile=$OPTARG
2034+
;;
20262035
p) prefix=$OPTARG ;;
20272036
4) ipv4=1 ;;
20282037
6) ipv6=1 ;;
@@ -2051,7 +2060,7 @@ _known_hosts_real()
20512060
kh=()
20522061

20532062
# ssh config files
2054-
if [[ -v configfile ]]; then
2063+
if [[ $configfile ]]; then
20552064
[[ -r $configfile && ! -d $configfile ]] && config+=("$configfile")
20562065
else
20572066
for i in /etc/ssh/ssh_config ~/.ssh/config ~/.ssh2/config; do
@@ -2097,7 +2106,7 @@ _known_hosts_real()
20972106
done
20982107
fi
20992108

2100-
if [[ ! -v configfile ]]; then
2109+
if [[ ! $configfile ]]; then
21012110
# Global and user known_hosts files
21022111
for i in /etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2 \
21032112
/etc/known_hosts /etc/known_hosts2 ~/.ssh/known_hosts \
@@ -2162,7 +2171,7 @@ _known_hosts_real()
21622171
fi
21632172

21642173
# append any available aliases from ssh config files
2165-
if [[ ${#config[@]} -gt 0 && -v aliases ]]; then
2174+
if [[ ${#config[@]} -gt 0 && $aliases ]]; then
21662175
local -a hosts=($(command sed -ne 's/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' "${config[@]}"))
21672176
if ((${#hosts[@]} != 0)); then
21682177
COMPREPLY+=($(compgen -P "$prefix" \
@@ -2198,13 +2207,13 @@ _known_hosts_real()
21982207
$reset
21992208

22002209
if ((${#COMPREPLY[@]})); then
2201-
if [[ -v ipv4 ]]; then
2210+
if [[ $ipv4 ]]; then
22022211
COMPREPLY=("${COMPREPLY[@]/*:*$suffix/}")
22032212
fi
2204-
if [[ -v ipv6 ]]; then
2213+
if [[ $ipv6 ]]; then
22052214
COMPREPLY=("${COMPREPLY[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
22062215
fi
2207-
if [[ -v ipv4 || -v ipv6 ]]; then
2216+
if [[ $ipv4 || $ipv6 ]]; then
22082217
for i in "${!COMPREPLY[@]}"; do
22092218
[[ ${COMPREPLY[i]} ]] || unset -v 'COMPREPLY[i]'
22102219
done

0 commit comments

Comments
 (0)