Skip to content

fix: add fixes for localvar_inherit #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 5, 2023
Merged
55 changes: 33 additions & 22 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ _comp_split()
{
local _assign='=' IFS=$' \t\n'

local OPTIND=1 OPTARG='' OPTERR=0 _opt
local OPTIND=1 OPTARG="" OPTERR=0 _opt
while getopts ':alF:' _opt "$@"; do
case $_opt in
a) _assign='+=' ;;
Expand Down Expand Up @@ -396,7 +396,7 @@ _comp_looks_like_path()
#
__reassemble_comp_words_by_ref()
{
local exclude i j line ref
local exclude="" i j line ref
# Exclude word separator characters?
if [[ $1 ]]; then
# Yes, exclude word separator characters;
Expand All @@ -407,7 +407,7 @@ __reassemble_comp_words_by_ref()
# Default to cword unchanged
printf -v "$3" %s "$COMP_CWORD"
# Are characters excluded which were former included?
if [[ -v exclude ]]; then
if [[ $exclude ]]; then
# Yes, list of word completion separators has shrunk;
line=$COMP_LINE
# Re-assemble words to complete
Expand Down Expand Up @@ -531,13 +531,18 @@ __get_cword_at_cursor_by_ref()
#
_get_comp_words_by_ref()
{
local exclude flag i OPTIND=1
local exclude="" flag i OPTIND=1
local cur cword words=()
local upargs=() upvars=() vcur vcword vprev vwords
unset -v vcur vcword vprev vwords # workaround for localvar_inherit
local upargs=() upvars=() vcur="" vcword="" vprev="" vwords=""

while getopts "c:i:n:p:w:" flag "$@"; do
case $flag in
[cipw])
if [[ $OPTARG != [a-zA-Z_]*([a-zA-Z_0-9])?(\[*\]) ]]; then
echo "bash_completion: $FUNCNAME: -$flag: invalid variable name \`$OPTARG'" >&2
return 1
fi
;;&
c) vcur=$OPTARG ;;
i) vcword=$OPTARG ;;
n) exclude=$OPTARG ;;
Expand Down Expand Up @@ -566,19 +571,19 @@ _get_comp_words_by_ref()

__get_cword_at_cursor_by_ref "${exclude-}" words cword cur

[[ -v vcur ]] && {
[[ $vcur ]] && {
upvars+=("$vcur")
upargs+=(-v $vcur "$cur")
}
[[ -v vcword ]] && {
[[ $vcword ]] && {
upvars+=("$vcword")
upargs+=(-v $vcword "$cword")
}
[[ -v vprev && $cword -ge 1 ]] && {
[[ $vprev && $cword -ge 1 ]] && {
upvars+=("$vprev")
upargs+=(-v $vprev "${words[cword - 1]}")
}
[[ -v vwords ]] && {
[[ $vwords ]] && {
upvars+=("$vwords")
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
}
Expand Down Expand Up @@ -935,7 +940,7 @@ _comp_initialize()
{
local exclude="" outx errx inx

local flag OPTIND=1 OPTARG='' OPTERR=0
local flag OPTIND=1 OPTARG="" OPTERR=0
while getopts "n:e:o:i:s" flag "$@"; do
case $flag in
n) exclude+=$OPTARG ;;
Expand Down Expand Up @@ -1815,7 +1820,7 @@ _known_hosts()

# NOTE: Using `_known_hosts' as a helper function and passing options
# to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
local options
local options=""
[[ ${1-} == -a || ${2-} == -a ]] && options=-a
[[ ${1-} == -c || ${2-} == -c ]] && options+=" -c"
# shellcheck disable=SC2086
Expand Down Expand Up @@ -1882,8 +1887,8 @@ _included_ssh_config_files()
# @return Completions, starting with CWORD, are added to COMPREPLY[]
_known_hosts_real()
{
local configfile flag prefix=""
local cur suffix="" aliases i host ipv4 ipv6
local configfile="" flag prefix=""
local cur suffix="" aliases="" i host ipv4="" ipv6=""
local -a kh tmpkh=() khd=() config=()

# TODO remove trailing %foo from entries
Expand All @@ -1893,7 +1898,13 @@ _known_hosts_real()
case $flag in
a) aliases='yes' ;;
c) suffix=':' ;;
F) configfile=$OPTARG ;;
F)
if [[ ! $OPTARG ]]; then
echo "bash_completion: $FUNCNAME: -F: an empty filename is specified" >&2
return 1
fi
configfile=$OPTARG
;;
p) prefix=$OPTARG ;;
4) ipv4=1 ;;
6) ipv6=1 ;;
Expand Down Expand Up @@ -1922,7 +1933,7 @@ _known_hosts_real()
kh=()

# ssh config files
if [[ -v configfile ]]; then
if [[ $configfile ]]; then
[[ -r $configfile && ! -d $configfile ]] && config+=("$configfile")
else
for i in /etc/ssh/ssh_config ~/.ssh/config ~/.ssh2/config; do
Expand Down Expand Up @@ -1968,7 +1979,7 @@ _known_hosts_real()
done
fi

if [[ ! -v configfile ]]; then
if [[ ! $configfile ]]; then
# Global and user known_hosts files
for i in /etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2 \
/etc/known_hosts /etc/known_hosts2 ~/.ssh/known_hosts \
Expand Down Expand Up @@ -2033,7 +2044,7 @@ _known_hosts_real()
fi

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

if ((${#COMPREPLY[@]})); then
if [[ -v ipv4 ]]; then
if [[ $ipv4 ]]; then
COMPREPLY=("${COMPREPLY[@]/*:*$suffix/}")
fi
if [[ -v ipv6 ]]; then
if [[ $ipv6 ]]; then
COMPREPLY=("${COMPREPLY[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
fi
if [[ -v ipv4 || -v ipv6 ]]; then
if [[ $ipv4 || $ipv6 ]]; then
for i in "${!COMPREPLY[@]}"; do
[[ ${COMPREPLY[i]} ]] || unset -v 'COMPREPLY[i]'
done
Expand Down Expand Up @@ -2113,7 +2124,7 @@ _cd()
return
fi

local mark_dirs='' mark_symdirs=''
local mark_dirs="" mark_symdirs=""
_comp_readline_variable_on mark-directories && mark_dirs=y
_comp_readline_variable_on mark-symlinked-directories && mark_symdirs=y

Expand Down
5 changes: 3 additions & 2 deletions completions/_adb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ _comp_cmd_adb()
;;
esac

local cmd i
local cmd has_cmd=false i
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* && ${words[i - 1]} != -[sp] ]]; then
cmd="${words[i]}"
has_cmd=true
break
fi
done

if [[ ! -v cmd ]]; then
if ! "$has_cmd"; then
local tmp=()
if [[ ! $cur || $cur == -* ]]; then
tmp+=($(compgen -W '$(_parse_help "$1" help)' -- "$cur"))
Expand Down
6 changes: 3 additions & 3 deletions completions/_chsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ _comp_cmd_chsh()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local word chroot
local word chroot="" has_chroot=false
for word in "${words[@]}"; do
if [[ -v chroot ]]; then
if "$has_chroot"; then
chroot=$word
break
fi
[[ $word != -@(R|-root) ]] || chroot=
[[ $word != -@(R|-root) ]] || has_chroot=true
done

case $prev in
Expand Down
9 changes: 5 additions & 4 deletions completions/_op
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,28 @@ _comp_cmd_op()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local command i
local command has_command=false i
for ((i = 1; i < cword; i++)); do
case ${words[i]} in
--help | --version) return ;;
-*) ;;
*)
command=${words[i]}
has_command=true
break
;;
esac
done

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

[[ -v command ]] && _comp_cmd_op__command_options "$1" && return
"$has_command" && _comp_cmd_op__command_options "$1" && return

if [[ ! -v command || $command == "$prev" ]]; then
if ! "$has_command" || [[ $command == "$prev" ]]; then
COMPREPLY=($(compgen -W '$(_op_commands "$1" ${command-})' -- "$cur"))
[[ ${COMPREPLY-} ]] && return
fi
Expand Down
5 changes: 3 additions & 2 deletions completions/_udevadm
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ _comp_cmd_udevadm()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local i udevcmd
local i udevcmd has_udevcmd=false
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* ]]; then
udevcmd=${words[i]}
has_udevcmd=true
break
fi
done
Expand Down Expand Up @@ -53,7 +54,7 @@ _comp_cmd_udevadm()

$split && return

if [[ ! -v udevcmd ]]; then
if ! "$has_udevcmd"; then
case $cur in
-*)
COMPREPLY=($(compgen -W '--help --version --debug' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_yum
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ _comp_cmd_yum()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

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

if [[ -v special ]]; then
if [[ $special ]]; then
# TODO: install|update|upgrade should not match *src.rpm
if [[ $cur == @(*/|[.~])* &&
$special == @(deplist|install|update|upgrade) ]]; then
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-build
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ _apt_build()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

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

if [[ -v special ]]; then
if [[ $special ]]; then
case $special in
install | source | info)
COMPREPLY=($(_comp_xfunc apt-cache packages))
Expand Down
6 changes: 3 additions & 3 deletions completions/apt-cache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _apt_cache()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

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

if [[ -v special && $ispecial -lt $cword ]]; then
if [[ $special && $ispecial -lt $cword ]]; then
case $special in
add)
_filedir
Expand Down Expand Up @@ -84,7 +84,7 @@ _apt_cache()
--full --all-versions --no-all-versions --generate --no-generate
--names-only --all-names --recurse --installed --with-source --help
--version --config-file --option' -- "$cur"))
elif [[ ! -v special ]]; then
elif [[ ! $special ]]; then
COMPREPLY=($(compgen -W 'gencaches showpkg stats showsrc dump
dumpavail unmet show search depends rdepends pkgnames dotty xvcg
policy madison' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-get
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ _apt_get()
local cur prev words cword comp_args package
_comp_initialize -n ':=' -- "$@" || return

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

if [[ -v special ]]; then
if [[ $special ]]; then
case $special in
remove | auto?(-)remove | purge)
_comp_xfunc_apt_get_installed_packages
Expand Down
4 changes: 2 additions & 2 deletions completions/apt-mark
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ _comp_cmd_apt_mark()
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

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

if [[ -v special ]]; then
if [[ $special ]]; then
case $special in
auto | manual | unhold)
local -A showcmds=([auto]=manual [manual]=auto [unhold]=hold)
Expand Down
4 changes: 2 additions & 2 deletions completions/aptitude
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ _aptitude()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local special i
local special="" i
for ((i = 1; i < ${#words[@]} - 1; i++)); do
if [[ ${words[i]} == @(@(|re)install|@(|un)hold|@(|un)markauto|@(dist|full|safe)-upgrade|download|show|forbid-version|purge|remove|changelog|why@(|-not)|keep@(|-all)|build-dep|@(add|remove)-user-tag|versions) ]]; then
special=${words[i]}
break
fi
done

if [[ -v special ]]; then
if [[ $special ]]; then
case $special in
install | hold | markauto | unmarkauto | dist-upgrade | full-upgrade | \
safe-upgrade | download | show | changelog | why | why-not | build-dep | \
Expand Down
Loading