Skip to content

Commit

Permalink
refactor: rename { => _comp_compgen}_known_hosts{_real => }
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 17, 2023
1 parent 4d87457 commit d311921
Show file tree
Hide file tree
Showing 82 changed files with 165 additions and 158 deletions.
58 changes: 32 additions & 26 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,7 @@ _user_at_host()
_comp_initialize -n : -- "$@" || return

if [[ $cur == *@* ]]; then
_known_hosts_real "$cur"
_comp_compgen_known_hosts "$cur"
else
_comp_compgen -- -u -S @
compopt -o nospace
Expand All @@ -2360,20 +2360,21 @@ _user_at_host()
shopt -u hostcomplete && complete -F _user_at_host talk ytalk finger

# NOTE: Using this function as a helper function is deprecated. Use
# `_known_hosts_real' instead.
# `_comp_compgen_known_hosts' instead.
# TODO:API: rename per conventions
_known_hosts()
{
local cur prev words cword comp_args
_comp_initialize -n : -- "$@" || return

# NOTE: Using `_known_hosts' as a helper function and passing options
# to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
# NOTE: Using `_known_hosts' as a helper function and passing options to
# `_known_hosts' is deprecated: Use `_comp_compgen_known_hosts'
# instead.
local -a options=()
[[ ${1-} == -a || ${2-} == -a ]] && options+=(-a)
[[ ${1-} == -c || ${2-} == -c ]] && options+=(-c)
local IFS=$' \t\n' # Workaround for connected ${v+"$@"} in bash < 4.4
_known_hosts_real ${options[@]+"${options[@]}"} -- "$cur"
_comp_compgen_known_hosts ${options[@]+"${options[@]}"} -- "$cur"
} # _known_hosts()

# Helper function to locate ssh included files in configs
Expand Down Expand Up @@ -2436,7 +2437,7 @@ _comp__included_ssh_config_files()
# BASH_COMPLETION_KNOWN_HOSTS_WITH_AVAHI is set to a non-empty value.
# Also hosts from HOSTFILE (compgen -A hostname) are added, unless
# BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE is set to an empty value.
# Usage: _known_hosts_real [OPTIONS] CWORD
# Usage: _comp_compgen_known_hosts [OPTIONS] CWORD
# Options:
# -a Use aliases from ssh config files
# -c Use `:' suffix
Expand All @@ -2445,9 +2446,17 @@ _comp__included_ssh_config_files()
# -4 Filter IPv6 addresses from results
# -6 Filter IPv4 addresses from results
# @return Completions, starting with CWORD, are added to COMPREPLY[]
# TODO:API: rename per conventions
_known_hosts_real()
# @since 2.12
_comp_compgen_known_hosts()
{
local known_hosts
_comp_compgen_known_hosts__impl "$@" || return "$?"
_comp_compgen -U known_hosts set "${known_hosts[@]}"
}
_comp_compgen_known_hosts__impl()
{
known_hosts=()

local configfile="" flag prefix=""
local cur suffix="" aliases="" i host ipv4="" ipv6=""
local -a kh tmpkh=() khd=() config=()
Expand Down Expand Up @@ -2550,8 +2559,6 @@ _known_hosts_real()

# If we have known_hosts files to use
if ((${#kh[@]} + ${#khd[@]} > 0)); then
local -a tmp=()

if ((${#kh[@]} > 0)); then
# https://man.openbsd.org/sshd.8#SSH_KNOWN_HOSTS_FILE_FORMAT
for i in "${kh[@]}"; do
Expand All @@ -2573,7 +2580,7 @@ _known_hosts_real()
# Remove trailing ] + optional :port
host=${host%]?(:+([0-9]))}
# Add host to candidates
[[ $host ]] && tmp+=("$host")
[[ $host ]] && known_hosts+=("$host")
done
fi
done <"$i"
Expand All @@ -2588,21 +2595,21 @@ _known_hosts_real()
if [[ $i == *key_22_*.pub && -r $i ]]; then
host=${i/#*key_22_/}
host=${host/%.pub/}
[[ $host ]] && tmp+=("$host")
[[ $host ]] && known_hosts+=("$host")
fi
done
fi

# apply suffix and prefix
((${#tmp[@]})) &&
_comp_compgen -- -W '"${tmp[@]}"' -P "$prefix" -S "$suffix"
((${#known_hosts[@]})) &&
_comp_compgen -v known_hosts -- -W '"${known_hosts[@]}"' -P "$prefix" -S "$suffix"
fi

# append any available aliases from ssh config files
if [[ ${#config[@]} -gt 0 && $aliases ]]; then
local -a hosts
if _comp_split hosts "$(command sed -ne 's/^[[:blank:]]*[Hh][Oo][Ss][Tt][[:blank:]=]\{1,\}\(.*\)$/\1/p' "${config[@]}")"; then
_comp_compgen -a -- -P "$prefix" \
_comp_compgen -av known_hosts -- -P "$prefix" \
-S "$suffix" -W '"${hosts[@]%%[*?%]*}"' -X '@(\!*|)'
fi
fi
Expand All @@ -2615,38 +2622,37 @@ _known_hosts_real()
local generated=$(avahi-browse -cprak 2>/dev/null | awk -F';' \
'/^=/ && $5 ~ /^_(ssh|workstation)\._tcp$/ { print $7 }' |
sort -u)
_comp_compgen -a -- -P "$prefix" -S "$suffix" -W '$generated'
_comp_compgen -av known_hosts -- -P "$prefix" -S "$suffix" -W '$generated'
fi

# Add hosts reported by ruptime.
if type ruptime &>/dev/null; then
local generated=$(ruptime 2>/dev/null | awk '!/^ruptime:/ { print $1 }')
_comp_compgen -a -- -W '$generated'
_comp_compgen -av known_hosts -- -W '$generated'
fi

# Add results of normal hostname completion, unless
# `BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE' is set to an empty value.
if [[ ${BASH_COMPLETION_KNOWN_HOSTS_WITH_HOSTFILE-${COMP_KNOWN_HOSTS_WITH_HOSTFILE-1}} ]]; then
_comp_compgen -a -- -A hostname -P "$prefix" -S "$suffix"
_comp_compgen -av known_hosts -- -A hostname -P "$prefix" -S "$suffix"
fi

if ((${#COMPREPLY[@]})); then
if ((${#known_hosts[@]})); then
if [[ $ipv4 ]]; then
COMPREPLY=("${COMPREPLY[@]/*:*$suffix/}")
known_hosts=("${known_hosts[@]/*:*$suffix/}")
fi
if [[ $ipv6 ]]; then
COMPREPLY=("${COMPREPLY[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
known_hosts=("${known_hosts[@]/+([0-9]).+([0-9]).+([0-9]).+([0-9])$suffix/}")
fi
if [[ $ipv4 || $ipv6 ]]; then
for i in "${!COMPREPLY[@]}"; do
[[ ${COMPREPLY[i]} ]] || unset -v 'COMPREPLY[i]'
for i in "${!known_hosts[@]}"; do
[[ ${known_hosts[i]} ]] || unset -v 'known_hosts[i]'
done
fi
_comp_compgen -v known_hosts -c "$prefix$cur" ltrim_colon "${known_hosts[@]}"
fi

_comp_ltrim_colon_completions "$prefix$cur"

} # _known_hosts_real()
} # _comp_compgen_known_hosts__impl()
complete -F _known_hosts traceroute traceroute6 \
fping fping6 telnet rsh rlogin ftp dig drill mtr ssh-installkeys showmount

Expand Down
1 change: 1 addition & 0 deletions bash_completion.d/000_bash_completion_compat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _comp_deprecate_func 2.12 _root_command _comp_root_command
_comp_deprecate_func 2.12 _xfunc _comp_xfunc
_comp_deprecate_func 2.12 _upvars _comp_upvars
_comp_deprecate_func 2.12 _get_comp_words_by_ref _comp_get_words
_comp_deprecate_func 2.12 _known_hosts_real _comp_compgen_known_hosts
_comp_deprecate_func 2.12 _longopt _comp_longopt
_comp_deprecate_func 2.12 __ltrim_colon_completions _comp_ltrim_colon_completions
_comp_deprecate_func 2.12 _signals _comp_compgen_signals
Expand Down
2 changes: 1 addition & 1 deletion completions/_xm
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ _comp_cmd_xm()
_comp_cmd_xm__domain_names
;;
3)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
;;
esac
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/arping
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _comp_cmd_arping()
return
fi

_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
} &&
complete -F _comp_cmd_arping arping

Expand Down
4 changes: 2 additions & 2 deletions completions/arpspoof
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ _comp_cmd_arpspoof()
return
;;
-t)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
esac

if [[ $cur == -* ]]; then
_comp_compgen_usage
else
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
fi

} &&
Expand Down
2 changes: 1 addition & 1 deletion completions/cancel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _comp_cmd_cancel()

case $prev in
-h)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
-U)
Expand Down
2 changes: 1 addition & 1 deletion completions/chromium-browser
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _comp_cmd_chromium_browser()
case $cur in
*://*)
local prefix="${cur%%://*}://"
_known_hosts_real -- "${cur#*://}"
_comp_compgen_known_hosts -- "${cur#*://}"
COMPREPLY=("${COMPREPLY[@]/#/$prefix}")
_comp_ltrim_colon_completions "$cur"
;;
Expand Down
6 changes: 3 additions & 3 deletions completions/chronyc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _comp_cmd_chronyc__command_args()
_comp_split args "$("$1" help 2>/dev/null |
awk '/^'"$prev"'\s[^ []/ { gsub("\\|", " ", $2); print $2 }')"
case $args in
\<address\>) _known_hosts_real -- "$cur" ;;
\<address\>) _comp_compgen_known_hosts -- "$cur" ;;
\<*) ;;
*) ((${#args[@]})) &&
_comp_compgen -a -- -W '"${args[@]}"' ;;
Expand All @@ -23,7 +23,7 @@ _comp_cmd_chronyc()
return
;;
-*h)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
esac
Expand Down Expand Up @@ -53,7 +53,7 @@ _comp_cmd_chronyc()
fi
;;
2)
[[ $prev == @(peer|server) ]] && _known_hosts_real -- "$cur"
[[ $prev == @(peer|server) ]] && _comp_compgen_known_hosts -- "$cur"
;;
esac
} &&
Expand Down
6 changes: 3 additions & 3 deletions completions/curl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ _comp_cmd_curl()
return
;;
--dns-servers | --noproxy)
_known_hosts_real -- "${cur##*,}"
_comp_compgen_known_hosts -- "${cur##*,}"
((${#COMPREPLY[@]})) &&
_comp_delimited , -W '"${COMPREPLY[@]}"'
return
Expand All @@ -86,7 +86,7 @@ _comp_cmd_curl()
;;
--ftp-port | -${noargopts}P)
_comp_compgen_available_interfaces -a
_known_hosts_real -- "$cur"
_comp_compgen -a known_hosts -- "$cur"
_comp_compgen -a ip_addresses -a
return
;;
Expand Down Expand Up @@ -126,7 +126,7 @@ _comp_cmd_curl()
;;
--preproxy | --proxy | --socks4 | --socks4a | --socks5 | \
--socks5-hostname | -${noargopts}x)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
--pubkey)
Expand Down
2 changes: 1 addition & 1 deletion completions/dhclient
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _comp_cmd_dhclient()
return
;;
-s)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion completions/fio
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ _comp_cmd_fio()
return
;;
--client)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
--remote-config)
Expand Down
2 changes: 1 addition & 1 deletion completions/freeciv
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ _comp_cmd_freeciv()
return
;;
--Meta | --server | -[Ms])
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
--Plugin | -P)
Expand Down
2 changes: 1 addition & 1 deletion completions/geoiplookup
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ _comp_cmd_geoiplookup()

local ipvx
[[ $1 == *6 ]] && ipvx=-6 || ipvx=-4
_known_hosts_real $ipvx -- "$cur"
_comp_compgen_known_hosts $ipvx -- "$cur"
} &&
complete -F _comp_cmd_geoiplookup geoiplookup geoiplookup6

Expand Down
2 changes: 1 addition & 1 deletion completions/gkrellm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _comp_cmd_gkrellm()
return
;;
-s | --server)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
-l | --logfile)
Expand Down
4 changes: 2 additions & 2 deletions completions/hping2
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _comp_cmd_hping2()
return
;;
--spoof | -${noargopts}a)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
--tos | -${noargopts}o)
Expand All @@ -30,7 +30,7 @@ _comp_cmd_hping2()
if [[ $cur == -* ]]; then
_comp_compgen_help
else
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
fi
} &&
complete -F _comp_cmd_hping2 hping hping2 hping3
Expand Down
2 changes: 1 addition & 1 deletion completions/ifstat
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _comp_cmd_ifstat()
"$1" --help 2>&1 || :
} |
command grep -q -- '-s.*--noupdate'; then
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
fi
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/influx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _comp_cmd_influx()
return
;;
-host)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
-format | -precision | -consistency)
Expand Down
2 changes: 1 addition & 1 deletion completions/iperf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ _comp_cmd_iperf()
return
;;
--client | -${noargopts}c)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
--reportexclude | -${noargopts}x)
Expand Down
2 changes: 1 addition & 1 deletion completions/ipmitool
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _comp_cmd_ipmitool()
return
;;
-*H)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
-*[fSO])
Expand Down
2 changes: 1 addition & 1 deletion completions/jps
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _comp_cmd_jps()
_comp_compgen -- -W "-q -m -l -v -V -J -help"
[[ ${COMPREPLY-} == -J* ]] && compopt -o nospace
else
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
fi
} &&
complete -F _comp_cmd_jps jps
Expand Down
2 changes: 1 addition & 1 deletion completions/ktutil
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _comp_cmd_ktutil()
return
;;
-a | --admin-server)
_known_hosts_real -- "$cur"
_comp_compgen_known_hosts -- "$cur"
return
;;
-r | --realm)
Expand Down
Loading

0 comments on commit d311921

Please sign in to comment.