Skip to content

Commit ba95f05

Browse files
authored
Merge pull request #891 from akinomyoga/localvar_inherit
fix: add fixes for `localvar_inherit`
2 parents cb78194 + ace9cd6 commit ba95f05

38 files changed

+155
-127
lines changed

bash_completion

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ _comp_split()
346346
{
347347
local _assign='=' IFS=$' \t\n'
348348

349-
local OPTIND=1 OPTARG='' OPTERR=0 _opt
349+
local OPTIND=1 OPTARG="" OPTERR=0 _opt
350350
while getopts ':alF:' _opt "$@"; do
351351
case $_opt in
352352
a) _assign='+=' ;;
@@ -396,7 +396,7 @@ _comp_looks_like_path()
396396
#
397397
__reassemble_comp_words_by_ref()
398398
{
399-
local exclude i j line ref
399+
local exclude="" i j line ref
400400
# Exclude word separator characters?
401401
if [[ $1 ]]; then
402402
# Yes, exclude word separator characters;
@@ -407,7 +407,7 @@ __reassemble_comp_words_by_ref()
407407
# Default to cword unchanged
408408
printf -v "$3" %s "$COMP_CWORD"
409409
# Are characters excluded which were former included?
410-
if [[ -v exclude ]]; then
410+
if [[ $exclude ]]; then
411411
# Yes, list of word completion separators has shrunk;
412412
line=$COMP_LINE
413413
# Re-assemble words to complete
@@ -531,13 +531,18 @@ __get_cword_at_cursor_by_ref()
531531
#
532532
_get_comp_words_by_ref()
533533
{
534-
local exclude flag i OPTIND=1
534+
local exclude="" flag i OPTIND=1
535535
local cur cword words=()
536-
local upargs=() upvars=() vcur vcword vprev vwords
537-
unset -v vcur vcword vprev vwords # workaround for localvar_inherit
536+
local upargs=() upvars=() vcur="" vcword="" vprev="" vwords=""
538537

539538
while getopts "c:i:n:p:w:" flag "$@"; do
540539
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+
;;&
541546
c) vcur=$OPTARG ;;
542547
i) vcword=$OPTARG ;;
543548
n) exclude=$OPTARG ;;
@@ -566,19 +571,19 @@ _get_comp_words_by_ref()
566571

567572
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
568573

569-
[[ -v vcur ]] && {
574+
[[ $vcur ]] && {
570575
upvars+=("$vcur")
571576
upargs+=(-v $vcur "$cur")
572577
}
573-
[[ -v vcword ]] && {
578+
[[ $vcword ]] && {
574579
upvars+=("$vcword")
575580
upargs+=(-v $vcword "$cword")
576581
}
577-
[[ -v vprev && $cword -ge 1 ]] && {
582+
[[ $vprev && $cword -ge 1 ]] && {
578583
upvars+=("$vprev")
579584
upargs+=(-v $vprev "${words[cword - 1]}")
580585
}
581-
[[ -v vwords ]] && {
586+
[[ $vwords ]] && {
582587
upvars+=("$vwords")
583588
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
584589
}
@@ -935,7 +940,7 @@ _comp_initialize()
935940
{
936941
local exclude="" outx errx inx
937942

938-
local flag OPTIND=1 OPTARG='' OPTERR=0
943+
local flag OPTIND=1 OPTARG="" OPTERR=0
939944
while getopts "n:e:o:i:s" flag "$@"; do
940945
case $flag in
941946
n) exclude+=$OPTARG ;;
@@ -1815,7 +1820,7 @@ _known_hosts()
18151820

18161821
# NOTE: Using `_known_hosts' as a helper function and passing options
18171822
# to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
1818-
local options
1823+
local options=""
18191824
[[ ${1-} == -a || ${2-} == -a ]] && options=-a
18201825
[[ ${1-} == -c || ${2-} == -c ]] && options+=" -c"
18211826
# shellcheck disable=SC2086
@@ -1882,8 +1887,8 @@ _included_ssh_config_files()
18821887
# @return Completions, starting with CWORD, are added to COMPREPLY[]
18831888
_known_hosts_real()
18841889
{
1885-
local configfile flag prefix=""
1886-
local cur suffix="" aliases i host ipv4 ipv6
1890+
local configfile="" flag prefix=""
1891+
local cur suffix="" aliases="" i host ipv4="" ipv6=""
18871892
local -a kh tmpkh=() khd=() config=()
18881893

18891894
# TODO remove trailing %foo from entries
@@ -1893,7 +1898,13 @@ _known_hosts_real()
18931898
case $flag in
18941899
a) aliases='yes' ;;
18951900
c) suffix=':' ;;
1896-
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+
;;
18971908
p) prefix=$OPTARG ;;
18981909
4) ipv4=1 ;;
18991910
6) ipv6=1 ;;
@@ -1922,7 +1933,7 @@ _known_hosts_real()
19221933
kh=()
19231934

19241935
# ssh config files
1925-
if [[ -v configfile ]]; then
1936+
if [[ $configfile ]]; then
19261937
[[ -r $configfile && ! -d $configfile ]] && config+=("$configfile")
19271938
else
19281939
for i in /etc/ssh/ssh_config ~/.ssh/config ~/.ssh2/config; do
@@ -1968,7 +1979,7 @@ _known_hosts_real()
19681979
done
19691980
fi
19701981

1971-
if [[ ! -v configfile ]]; then
1982+
if [[ ! $configfile ]]; then
19721983
# Global and user known_hosts files
19731984
for i in /etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2 \
19741985
/etc/known_hosts /etc/known_hosts2 ~/.ssh/known_hosts \
@@ -2033,7 +2044,7 @@ _known_hosts_real()
20332044
fi
20342045

20352046
# append any available aliases from ssh config files
2036-
if [[ ${#config[@]} -gt 0 && -v aliases ]]; then
2047+
if [[ ${#config[@]} -gt 0 && $aliases ]]; then
20372048
local -a hosts=($(command sed -ne 's/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' "${config[@]}"))
20382049
if ((${#hosts[@]} != 0)); then
20392050
COMPREPLY+=($(compgen -P "$prefix" \
@@ -2069,13 +2080,13 @@ _known_hosts_real()
20692080
$reset
20702081

20712082
if ((${#COMPREPLY[@]})); then
2072-
if [[ -v ipv4 ]]; then
2083+
if [[ $ipv4 ]]; then
20732084
COMPREPLY=("${COMPREPLY[@]/*:*$suffix/}")
20742085
fi
2075-
if [[ -v ipv6 ]]; then
2086+
if [[ $ipv6 ]]; then
20762087
COMPREPLY=("${COMPREPLY[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
20772088
fi
2078-
if [[ -v ipv4 || -v ipv6 ]]; then
2089+
if [[ $ipv4 || $ipv6 ]]; then
20792090
for i in "${!COMPREPLY[@]}"; do
20802091
[[ ${COMPREPLY[i]} ]] || unset -v 'COMPREPLY[i]'
20812092
done
@@ -2113,7 +2124,7 @@ _cd()
21132124
return
21142125
fi
21152126

2116-
local mark_dirs='' mark_symdirs=''
2127+
local mark_dirs="" mark_symdirs=""
21172128
_comp_readline_variable_on mark-directories && mark_dirs=y
21182129
_comp_readline_variable_on mark-symlinked-directories && mark_symdirs=y
21192130

completions/_adb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ _comp_cmd_adb()
2525
;;
2626
esac
2727

28-
local cmd i
28+
local cmd has_cmd=false i
2929
for ((i = 1; i < cword; i++)); do
3030
if [[ ${words[i]} != -* && ${words[i - 1]} != -[sp] ]]; then
3131
cmd="${words[i]}"
32+
has_cmd=true
3233
break
3334
fi
3435
done
3536

36-
if [[ ! -v cmd ]]; then
37+
if ! "$has_cmd"; then
3738
local tmp=()
3839
if [[ ! $cur || $cur == -* ]]; then
3940
tmp+=($(compgen -W '$(_parse_help "$1" help)' -- "$cur"))

completions/_chsh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ _comp_cmd_chsh()
88
local cur prev words cword comp_args
99
_comp_initialize -- "$@" || return
1010

11-
local word chroot
11+
local word chroot="" has_chroot=false
1212
for word in "${words[@]}"; do
13-
if [[ -v chroot ]]; then
13+
if "$has_chroot"; then
1414
chroot=$word
1515
break
1616
fi
17-
[[ $word != -@(R|-root) ]] || chroot=
17+
[[ $word != -@(R|-root) ]] || has_chroot=true
1818
done
1919

2020
case $prev in

completions/_op

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ _comp_cmd_op()
3030
local cur prev words cword split comp_args
3131
_comp_initialize -s -- "$@" || return
3232

33-
local command i
33+
local command has_command=false i
3434
for ((i = 1; i < cword; i++)); do
3535
case ${words[i]} in
3636
--help | --version) return ;;
3737
-*) ;;
3838
*)
3939
command=${words[i]}
40+
has_command=true
4041
break
4142
;;
4243
esac
4344
done
4445

45-
if [[ ! -v command && $cur == -* ]]; then
46+
if ! "$has_command" && [[ $cur == -* ]]; then
4647
COMPREPLY=($(compgen -W '$(_parse_usage "$1" --help)' -- "$cur"))
4748
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
4849
return
4950
fi
5051

51-
[[ -v command ]] && _comp_cmd_op__command_options "$1" && return
52+
"$has_command" && _comp_cmd_op__command_options "$1" && return
5253

53-
if [[ ! -v command || $command == "$prev" ]]; then
54+
if ! "$has_command" || [[ $command == "$prev" ]]; then
5455
COMPREPLY=($(compgen -W '$(_op_commands "$1" ${command-})' -- "$cur"))
5556
[[ ${COMPREPLY-} ]] && return
5657
fi

completions/_udevadm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ _comp_cmd_udevadm()
88
local cur prev words cword split comp_args
99
_comp_initialize -s -- "$@" || return
1010

11-
local i udevcmd
11+
local i udevcmd has_udevcmd=false
1212
for ((i = 1; i < cword; i++)); do
1313
if [[ ${words[i]} != -* ]]; then
1414
udevcmd=${words[i]}
15+
has_udevcmd=true
1516
break
1617
fi
1718
done
@@ -53,7 +54,7 @@ _comp_cmd_udevadm()
5354

5455
$split && return
5556

56-
if [[ ! -v udevcmd ]]; then
57+
if ! "$has_udevcmd"; then
5758
case $cur in
5859
-*)
5960
COMPREPLY=($(compgen -W '--help --version --debug' -- "$cur"))

completions/_yum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ _comp_cmd_yum()
4242
local cur prev words cword split comp_args
4343
_comp_initialize -s -- "$@" || return
4444

45-
local special i
45+
local special="" i
4646
for ((i = 1; i < ${#words[@]} - 1; i++)); do
4747
if [[ ${words[i]} == @(install|update|upgrade|remove|erase|deplist|info) ]]; then
4848
special=${words[i]}
4949
break
5050
fi
5151
done
5252

53-
if [[ -v special ]]; then
53+
if [[ $special ]]; then
5454
# TODO: install|update|upgrade should not match *src.rpm
5555
if [[ $cur == @(*/|[.~])* &&
5656
$special == @(deplist|install|update|upgrade) ]]; then

completions/apt-build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ _apt_build()
55
local cur prev words cword comp_args
66
_comp_initialize -- "$@" || return
77

8-
local special i
8+
local special="" i
99
for ((i = 1; i < ${#words[@]} - 1; i++)); do
1010
if [[ ${words[i]} == @(install|remove|source|info|clean) ]]; then
1111
special=${words[i]}
1212
break
1313
fi
1414
done
1515

16-
if [[ -v special ]]; then
16+
if [[ $special ]]; then
1717
case $special in
1818
install | source | info)
1919
COMPREPLY=($(_comp_xfunc apt-cache packages))

completions/apt-cache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _apt_cache()
2828
local cur prev words cword comp_args
2929
_comp_initialize -- "$@" || return
3030

31-
local special ispecial
31+
local special="" ispecial
3232
if [[ $cur != show ]]; then
3333
for ((ispecial = 1; ispecial < ${#words[@]} - 1; ispecial++)); do
3434
if [[ ${words[ispecial]} == @(add|depends|dotty|madison|policy|rdepends|show?(pkg|src|)) ]]; then
@@ -38,7 +38,7 @@ _apt_cache()
3838
done
3939
fi
4040

41-
if [[ -v special && $ispecial -lt $cword ]]; then
41+
if [[ $special && $ispecial -lt $cword ]]; then
4242
case $special in
4343
add)
4444
_filedir
@@ -84,7 +84,7 @@ _apt_cache()
8484
--full --all-versions --no-all-versions --generate --no-generate
8585
--names-only --all-names --recurse --installed --with-source --help
8686
--version --config-file --option' -- "$cur"))
87-
elif [[ ! -v special ]]; then
87+
elif [[ ! $special ]]; then
8888
COMPREPLY=($(compgen -W 'gencaches showpkg stats showsrc dump
8989
dumpavail unmet show search depends rdepends pkgnames dotty xvcg
9090
policy madison' -- "$cur"))

completions/apt-get

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ _apt_get()
2020
local cur prev words cword comp_args package
2121
_comp_initialize -n ':=' -- "$@" || return
2222

23-
local special i
23+
local special="" i
2424
for ((i = 1; i < ${#words[@]} - 1; i++)); do
2525
if [[ ${words[i]} == @(install|remove|auto?(-)remove|purge|source|build-dep|download|changelog) ]]; then
2626
special=${words[i]}
2727
break
2828
fi
2929
done
3030

31-
if [[ -v special ]]; then
31+
if [[ $special ]]; then
3232
case $special in
3333
remove | auto?(-)remove | purge)
3434
_comp_xfunc_apt_get_installed_packages

completions/apt-mark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ _comp_cmd_apt_mark()
55
local cur prev words cword split comp_args
66
_comp_initialize -s -- "$@" || return
77

8-
local special i
8+
local special="" i
99
for ((i = 1; i < ${#words[@]} - 1; i++)); do
1010
if [[ ${words[i]} == @(auto|manual|minimize-manual|showauto|showmanual|hold|unhold|showhold|install|remove|deinstall|purge|showinstall|showremove|showpurge) ]]; then
1111
special=${words[i]}
1212
break
1313
fi
1414
done
1515

16-
if [[ -v special ]]; then
16+
if [[ $special ]]; then
1717
case $special in
1818
auto | manual | unhold)
1919
local -A showcmds=([auto]=manual [manual]=auto [unhold]=hold)

0 commit comments

Comments
 (0)