Skip to content

Commit 901e2eb

Browse files
committed
refactor(bash_completion): avoid [[ -v var ]] for localvar_inherit
1 parent 128b975 commit 901e2eb

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
@@ -396,8 +396,7 @@ _comp_looks_like_path()
396396
#
397397
__reassemble_comp_words_by_ref()
398398
{
399-
local exclude i j line ref
400-
unset -v exclude # workaround for localvar_inherit
399+
local exclude="" i j line ref
401400
# Exclude word separator characters?
402401
if [[ $1 ]]; then
403402
# Yes, exclude word separator characters;
@@ -408,7 +407,7 @@ __reassemble_comp_words_by_ref()
408407
# Default to cword unchanged
409408
printf -v "$3" %s "$COMP_CWORD"
410409
# Are characters excluded which were former included?
411-
if [[ -v exclude ]]; then
410+
if [[ $exclude ]]; then
412411
# Yes, list of word completion separators has shrunk;
413412
line=$COMP_LINE
414413
# Re-assemble words to complete
@@ -532,13 +531,18 @@ __get_cword_at_cursor_by_ref()
532531
#
533532
_get_comp_words_by_ref()
534533
{
535-
local exclude flag i OPTIND=1
534+
local exclude="" flag i OPTIND=1
536535
local cur cword words=()
537-
local upargs=() upvars=() vcur vcword vprev vwords
538-
unset -v vcur vcword vprev vwords # workaround for localvar_inherit
536+
local upargs=() upvars=() vcur="" vcword="" vprev="" vwords=""
539537

540538
while getopts "c:i:n:p:w:" flag "$@"; do
541539
case $flag in
540+
[cipw])
541+
if [[ $OPTARG != [a-zA-Z_]*([a-zA-Z_0-9])?(\[*\]) ]]; then
542+
echo "bash_completion: $FUNCNAME: -$flag: invalid variable name \`$OPTARG'" >&2
543+
return 1
544+
fi
545+
;;&
542546
c) vcur=$OPTARG ;;
543547
i) vcword=$OPTARG ;;
544548
n) exclude=$OPTARG ;;
@@ -567,19 +571,19 @@ _get_comp_words_by_ref()
567571

568572
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
569573

570-
[[ -v vcur ]] && {
574+
[[ $vcur ]] && {
571575
upvars+=("$vcur")
572576
upargs+=(-v $vcur "$cur")
573577
}
574-
[[ -v vcword ]] && {
578+
[[ $vcword ]] && {
575579
upvars+=("$vcword")
576580
upargs+=(-v $vcword "$cword")
577581
}
578-
[[ -v vprev && $cword -ge 1 ]] && {
582+
[[ $vprev && $cword -ge 1 ]] && {
579583
upvars+=("$vprev")
580584
upargs+=(-v $vprev "${words[cword - 1]}")
581585
}
582-
[[ -v vwords ]] && {
586+
[[ $vwords ]] && {
583587
upvars+=("$vwords")
584588
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
585589
}
@@ -1883,10 +1887,9 @@ _included_ssh_config_files()
18831887
# @return Completions, starting with CWORD, are added to COMPREPLY[]
18841888
_known_hosts_real()
18851889
{
1886-
local configfile flag prefix=""
1887-
local cur suffix="" aliases i host ipv4 ipv6
1890+
local configfile="" flag prefix=""
1891+
local cur suffix="" aliases="" i host ipv4="" ipv6=""
18881892
local -a kh tmpkh=() khd=() config=()
1889-
unset -v configfile aliases ipv4 ipv6 # workaround for localvar_inherit
18901893

18911894
# TODO remove trailing %foo from entries
18921895

@@ -1895,7 +1898,13 @@ _known_hosts_real()
18951898
case $flag in
18961899
a) aliases='yes' ;;
18971900
c) suffix=':' ;;
1898-
F) configfile=$OPTARG ;;
1901+
F)
1902+
if [[ ! $OPTARG ]]; then
1903+
echo "bash_completion: $FUNCNAME: -F: an empty filename is specified" >&2
1904+
return 1
1905+
fi
1906+
configfile=$OPTARG
1907+
;;
18991908
p) prefix=$OPTARG ;;
19001909
4) ipv4=1 ;;
19011910
6) ipv6=1 ;;
@@ -1924,7 +1933,7 @@ _known_hosts_real()
19241933
kh=()
19251934

19261935
# ssh config files
1927-
if [[ -v configfile ]]; then
1936+
if [[ $configfile ]]; then
19281937
[[ -r $configfile && ! -d $configfile ]] && config+=("$configfile")
19291938
else
19301939
for i in /etc/ssh/ssh_config ~/.ssh/config ~/.ssh2/config; do
@@ -1970,7 +1979,7 @@ _known_hosts_real()
19701979
done
19711980
fi
19721981

1973-
if [[ ! -v configfile ]]; then
1982+
if [[ ! $configfile ]]; then
19741983
# Global and user known_hosts files
19751984
for i in /etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2 \
19761985
/etc/known_hosts /etc/known_hosts2 ~/.ssh/known_hosts \
@@ -2035,7 +2044,7 @@ _known_hosts_real()
20352044
fi
20362045

20372046
# append any available aliases from ssh config files
2038-
if [[ ${#config[@]} -gt 0 && -v aliases ]]; then
2047+
if [[ ${#config[@]} -gt 0 && $aliases ]]; then
20392048
local -a hosts=($(command sed -ne 's/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' "${config[@]}"))
20402049
if ((${#hosts[@]} != 0)); then
20412050
COMPREPLY+=($(compgen -P "$prefix" \
@@ -2071,13 +2080,13 @@ _known_hosts_real()
20712080
$reset
20722081

20732082
if ((${#COMPREPLY[@]})); then
2074-
if [[ -v ipv4 ]]; then
2083+
if [[ $ipv4 ]]; then
20752084
COMPREPLY=("${COMPREPLY[@]/*:*$suffix/}")
20762085
fi
2077-
if [[ -v ipv6 ]]; then
2086+
if [[ $ipv6 ]]; then
20782087
COMPREPLY=("${COMPREPLY[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
20792088
fi
2080-
if [[ -v ipv4 || -v ipv6 ]]; then
2089+
if [[ $ipv4 || $ipv6 ]]; then
20812090
for i in "${!COMPREPLY[@]}"; do
20822091
[[ ${COMPREPLY[i]} ]] || unset -v 'COMPREPLY[i]'
20832092
done

0 commit comments

Comments
 (0)