Skip to content

Commit 8d5d742

Browse files
authored
Merge pull request #990 from scop/refactor/api-and-naming
refactor: more API naming work
2 parents 9f7931a + f475b2e commit 8d5d742

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+143
-127
lines changed

bash_completion

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -932,12 +932,17 @@ _comp_get_words()
932932
((${#upvars[@]})) && local "${upvars[@]}" && _comp_upvars "${upargs[@]}"
933933
}
934934

935-
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
936-
# word-to-complete.
937-
# With a colon in COMP_WORDBREAKS, words containing
938-
# colons are always completed as entire words if the word to complete contains
939-
# a colon. This function fixes this, by removing the colon-containing-prefix
940-
# from COMPREPLY items.
935+
# Generate the specified items after left-trimming with the word-to-complete
936+
# containing a colon (:). If the word-to-complete does not contain a colon,
937+
# this generates the specified items without modifications.
938+
# @param $@ items to generate
939+
# @var[in] cur current word to complete
940+
#
941+
# @remarks In Bash, with a colon in COMP_WORDBREAKS, words containing colons
942+
# are always completed as entire words if the word to complete contains a
943+
# colon. This function fixes this behavior by removing the
944+
# colon-containing-prefix from the items.
945+
#
941946
# The preferred solution is to remove the colon (:) from COMP_WORDBREAKS in
942947
# your .bashrc:
943948
#
@@ -946,22 +951,32 @@ _comp_get_words()
946951
#
947952
# See also: Bash FAQ - E13) Why does filename completion misbehave if a colon
948953
# appears in the filename? - https://tiswww.case.edu/php/chet/bash/FAQ
954+
#
955+
# @since 2.12
956+
_comp_compgen_ltrim_colon()
957+
{
958+
(($#)) || return 0
959+
local -a tmp
960+
tmp=("$@")
961+
if [[ $cur == *:* && $COMP_WORDBREAKS == *:* ]]; then
962+
# Remove colon-word prefix from items
963+
local colon_word=${cur%"${cur##*:}"}
964+
tmp=("${tmp[@]#"$colon_word"}")
965+
fi
966+
_comp_compgen -R -- -W '"${tmp[@]}"'
967+
}
968+
969+
# If the word-to-complete contains a colon (:), left-trim COMPREPLY items with
970+
# word-to-complete.
971+
#
949972
# @param $1 current word to complete (cur)
950-
# @modifies global array $COMPREPLY
973+
# @var[in,out] COMPREPLY
951974
#
952975
# @since 2.12
953976
_comp_ltrim_colon_completions()
954977
{
955-
local i=${#COMPREPLY[*]}
956-
((i == 0)) && return 0
957-
if [[ $1 == *:* && $COMP_WORDBREAKS == *:* ]]; then
958-
# Remove colon-word prefix from COMPREPLY items
959-
local colon_word=${1%"${1##*:}"}
960-
COMPREPLY=("${COMPREPLY[@]}")
961-
while ((i-- > 0)); do
962-
COMPREPLY[i]=${COMPREPLY[i]#"$colon_word"}
963-
done
964-
fi
978+
((${#COMPREPLY[@]})) || return 0
979+
_comp_compgen -c "$1" ltrim_colon "${COMPREPLY[@]}"
965980
} # _comp_ltrim_colon_completions()
966981

967982
# This function quotes the argument in a way so that readline dequoting
@@ -1512,27 +1527,26 @@ _comp_compgen_usage()
15121527

15131528
# This function completes on signal names (minus the SIG prefix)
15141529
# @param $1 prefix
1515-
# TODO:API: rename per conventions
1516-
_signals()
1530+
_comp_compgen_signals()
15171531
{
15181532
local -a sigs
15191533
_comp_compgen -v sigs -c "SIG${cur#"${1-}"}" -- -P "${1-}" -A signal &&
1520-
COMPREPLY+=("${sigs[@]/#${1-}SIG/${1-}}")
1534+
_comp_compgen_set "${sigs[@]/#${1-}SIG/${1-}}"
15211535
}
15221536

15231537
# This function completes on known mac addresses
15241538
#
1525-
# TODO:API: rename per conventions
1526-
_mac_addresses()
1539+
_comp_compgen_mac_addresses()
15271540
{
15281541
local re='\([A-Fa-f0-9]\{2\}:\)\{5\}[A-Fa-f0-9]\{2\}'
15291542
local PATH="$PATH:/sbin:/usr/sbin"
1543+
local -a addresses
15301544

15311545
# Local interfaces
15321546
# - ifconfig on Linux: HWaddr or ether
15331547
# - ifconfig on FreeBSD: ether
15341548
# - ip link: link/ether
1535-
_comp_split -a COMPREPLY "$(
1549+
_comp_compgen -v addresses split -- "$(
15361550
{
15371551
LC_ALL=C ifconfig -a || ip -c=never link show || ip link show
15381552
} 2>/dev/null | command sed -ne \
@@ -1543,7 +1557,7 @@ _mac_addresses()
15431557
)"
15441558

15451559
# ARP cache
1546-
_comp_split -a COMPREPLY "$(
1560+
_comp_compgen -av addresses split -- "$(
15471561
{
15481562
arp -an || ip -c=never neigh show || ip neigh show
15491563
} 2>/dev/null | command sed -ne \
@@ -1552,18 +1566,15 @@ _mac_addresses()
15521566
)"
15531567

15541568
# /etc/ethers
1555-
_comp_split -a COMPREPLY "$(command sed -ne \
1569+
_comp_compgen -av addresses split -- "$(command sed -ne \
15561570
"s/^[[:space:]]*\($re\)[[:space:]].*/\1/p" /etc/ethers 2>/dev/null)"
15571571

1558-
((${#COMPREPLY[@]})) &&
1559-
_comp_compgen -- -W '"${COMPREPLY[@]}"'
1560-
_comp_ltrim_colon_completions "$cur"
1572+
_comp_compgen_ltrim_colon "${addresses[@]}"
15611573
}
15621574

15631575
# This function completes on configured network interfaces
15641576
#
1565-
# TODO:API: rename per conventions
1566-
_configured_interfaces()
1577+
_comp_compgen_configured_interfaces()
15671578
{
15681579
local -a files
15691580
if [[ -f /etc/debian_version ]]; then
@@ -1598,8 +1609,7 @@ _configured_interfaces()
15981609
# -6: IPv6 addresses only
15991610
# -a: All addresses
16001611
#
1601-
# TODO:API: rename per conventions
1602-
_ip_addresses()
1612+
_comp_compgen_ip_addresses()
16031613
{
16041614
local n
16051615
case ${1-} in
@@ -1608,13 +1618,19 @@ _ip_addresses()
16081618
*) n= ;;
16091619
esac
16101620
local PATH=$PATH:/sbin
1611-
local addrs=$({
1621+
local addrs
1622+
_comp_compgen -v addrs split -- "$({
16121623
LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show
16131624
} 2>/dev/null |
16141625
command sed -e 's/[[:space:]]addr:/ /' -ne \
1615-
"s|.*inet${n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")
1616-
_comp_compgen -a -- -W "$addrs"
1617-
[[ ! $n ]] || _comp_ltrim_colon_completions "$cur"
1626+
"s|.*inet${n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")" ||
1627+
return
1628+
1629+
if [[ ! $n ]]; then
1630+
_comp_compgen -R -- -W '"${addrs[@]}"'
1631+
else
1632+
_comp_compgen_ltrim_colon "${addrs[@]}"
1633+
fi
16181634
}
16191635

16201636
# This function completes on available kernels
@@ -1629,11 +1645,11 @@ _kernel_versions()
16291645
# -a: restrict to active interfaces only
16301646
# -w: restrict to wireless interfaces only
16311647
#
1632-
# TODO:API: rename per conventions
1633-
_available_interfaces()
1648+
_comp_compgen_available_interfaces()
16341649
{
16351650
local PATH=$PATH:/sbin
1636-
local generated=$({
1651+
local generated
1652+
_comp_compgen -v generated split -- "$({
16371653
if [[ ${1-} == -w ]]; then
16381654
iwconfig
16391655
elif [[ ${1-} == -a ]]; then
@@ -1642,9 +1658,8 @@ _available_interfaces()
16421658
ifconfig -a || ip -c=never link show || ip link show
16431659
fi
16441660
} 2>/dev/null | awk \
1645-
'/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')
1646-
_comp_split -l COMPREPLY "$generated" &&
1647-
_comp_compgen -- -W '"${COMPREPLY[@]/%[[:punct:]]/}"'
1661+
'/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" &&
1662+
_comp_compgen -R -- -W '"${generated[@]/%[[:punct:]]/}"'
16481663
}
16491664

16501665
# Echo number of CPUs, falling back to 1 on failure.

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ _comp_deprecate_func 2.12 _command _comp_command
99
_comp_deprecate_func 2.12 _root_command _comp_root_command
1010
_comp_deprecate_func 2.12 _xfunc _comp_xfunc
1111
_comp_deprecate_func 2.12 _upvars _comp_upvars
12-
_comp_deprecate_func 2.12 __reassemble_comp_words_by_ref _comp__reassemble_words
13-
_comp_deprecate_func 2.12 __get_cword_at_cursor_by_ref \
14-
_comp__get_cword_at_cursor
1512
_comp_deprecate_func 2.12 _get_comp_words_by_ref _comp_get_words
1613
_comp_deprecate_func 2.12 _longopt _comp_longopt
17-
_comp_deprecate_func 2.12 _split_longopt _comp__split_longopt
1814
_comp_deprecate_func 2.12 __ltrim_colon_completions _comp_ltrim_colon_completions
15+
_comp_deprecate_func 2.12 _signals _comp_compgen_signals
16+
_comp_deprecate_func 2.12 _mac_addresses _comp_compgen_mac_addresses
17+
_comp_deprecate_func 2.12 _available_interfaces _comp_compgen_available_interfaces
18+
_comp_deprecate_func 2.12 _configured_interfaces _comp_compgen_configured_interfaces
19+
_comp_deprecate_func 2.12 _ip_addresses _comp_compgen_ip_addresses
1920

2021
# Backwards compatibility for compat completions that use have().
2122
# @deprecated 1.90 should no longer be used; generally not needed with

completions/_nmcli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ _comp_cmd_nmcli()
5454
return
5555
;;
5656
iface)
57-
_available_interfaces
57+
_comp_compgen_available_interfaces
5858
return
5959
;;
6060
bssid)

completions/arp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _comp_cmd_arp()
99
# shellcheck disable=SC2254
1010
case $prev in
1111
--device | -${noargopts}i)
12-
_available_interfaces -a
12+
_comp_compgen_available_interfaces -a
1313
return
1414
;;
1515
--protocol | -${noargopts}[Ap])

completions/arping

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ _comp_cmd_arping()
1010
return
1111
;;
1212
-*I)
13-
_available_interfaces -a
13+
_comp_compgen_available_interfaces -a
1414
return
1515
;;
1616
-*s)
17-
_ip_addresses
17+
_comp_compgen_ip_addresses
1818
return
1919
;;
2020
esac

completions/arpspoof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _comp_cmd_arpspoof()
77

88
case $prev in
99
-i)
10-
_available_interfaces -a
10+
_comp_compgen_available_interfaces -a
1111
return
1212
;;
1313
-t)

completions/brctl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _comp_cmd_brctl()
2626
3)
2727
case $command in
2828
addif | delif)
29-
_configured_interfaces
29+
_comp_compgen_configured_interfaces
3030
;;
3131
stp)
3232
_comp_compgen -- -W 'on off'

completions/curl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _comp_cmd_curl()
6767
return
6868
;;
6969
--dns-ipv[46]-addr)
70-
_ip_addresses -"${prev:9:1}"
70+
_comp_compgen_ip_addresses -"${prev:9:1}"
7171
return
7272
;;
7373
--dns-servers | --noproxy)
@@ -85,9 +85,9 @@ _comp_cmd_curl()
8585
return
8686
;;
8787
--ftp-port | -${noargopts}P)
88-
_available_interfaces -a
88+
_comp_compgen_available_interfaces -a
8989
_known_hosts_real -- "$cur"
90-
_ip_addresses -a
90+
_comp_compgen -a ip_addresses -a
9191
return
9292
;;
9393
--ftp-method)
@@ -99,7 +99,7 @@ _comp_cmd_curl()
9999
return
100100
;;
101101
--interface)
102-
_available_interfaces -a
102+
_comp_compgen_available_interfaces -a
103103
return
104104
;;
105105
--help | -${noargopts}h)

completions/dhclient

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _comp_cmd_dhclient()
2626
if [[ $cur == -* ]]; then
2727
_comp_compgen_usage
2828
else
29-
_available_interfaces
29+
_comp_compgen_available_interfaces
3030
fi
3131
} &&
3232
complete -F _comp_cmd_dhclient dhclient

completions/dnsspoof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _comp_cmd_dnsspoof()
77

88
case $prev in
99
-i)
10-
_available_interfaces -a
10+
_comp_compgen_available_interfaces -a
1111
return
1212
;;
1313
-f)

0 commit comments

Comments
 (0)