@@ -932,12 +932,17 @@ _comp_get_words()
932
932
(( ${# upvars[@]} )) && local " ${upvars[@]} " && _comp_upvars " ${upargs[@]} "
933
933
}
934
934
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
+ #
941
946
# The preferred solution is to remove the colon (:) from COMP_WORDBREAKS in
942
947
# your .bashrc:
943
948
#
@@ -946,22 +951,32 @@ _comp_get_words()
946
951
#
947
952
# See also: Bash FAQ - E13) Why does filename completion misbehave if a colon
948
953
# 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
+ #
949
972
# @param $1 current word to complete (cur)
950
- # @modifies global array $ COMPREPLY
973
+ # @var[in,out] COMPREPLY
951
974
#
952
975
# @since 2.12
953
976
_comp_ltrim_colon_completions ()
954
977
{
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[@]} "
965
980
} # _comp_ltrim_colon_completions()
966
981
967
982
# This function quotes the argument in a way so that readline dequoting
@@ -1512,27 +1527,26 @@ _comp_compgen_usage()
1512
1527
1513
1528
# This function completes on signal names (minus the SIG prefix)
1514
1529
# @param $1 prefix
1515
- # TODO:API: rename per conventions
1516
- _signals ()
1530
+ _comp_compgen_signals ()
1517
1531
{
1518
1532
local -a sigs
1519
1533
_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-} } "
1521
1535
}
1522
1536
1523
1537
# This function completes on known mac addresses
1524
1538
#
1525
- # TODO:API: rename per conventions
1526
- _mac_addresses ()
1539
+ _comp_compgen_mac_addresses ()
1527
1540
{
1528
1541
local re=' \([A-Fa-f0-9]\{2\}:\)\{5\}[A-Fa-f0-9]\{2\}'
1529
1542
local PATH=" $PATH :/sbin:/usr/sbin"
1543
+ local -a addresses
1530
1544
1531
1545
# Local interfaces
1532
1546
# - ifconfig on Linux: HWaddr or ether
1533
1547
# - ifconfig on FreeBSD: ether
1534
1548
# - ip link: link/ether
1535
- _comp_split -a COMPREPLY " $(
1549
+ _comp_compgen -v addresses split -- " $(
1536
1550
{
1537
1551
LC_ALL=C ifconfig -a || ip -c=never link show || ip link show
1538
1552
} 2> /dev/null | command sed -ne \
@@ -1543,7 +1557,7 @@ _mac_addresses()
1543
1557
) "
1544
1558
1545
1559
# ARP cache
1546
- _comp_split -a COMPREPLY " $(
1560
+ _comp_compgen -av addresses split -- " $(
1547
1561
{
1548
1562
arp -an || ip -c=never neigh show || ip neigh show
1549
1563
} 2> /dev/null | command sed -ne \
@@ -1552,18 +1566,15 @@ _mac_addresses()
1552
1566
) "
1553
1567
1554
1568
# /etc/ethers
1555
- _comp_split -a COMPREPLY " $( command sed -ne \
1569
+ _comp_compgen -av addresses split -- " $( command sed -ne \
1556
1570
" s/^[[:space:]]*\($re \)[[:space:]].*/\1/p" /etc/ethers 2> /dev/null) "
1557
1571
1558
- (( ${# COMPREPLY[@]} )) &&
1559
- _comp_compgen -- -W ' "${COMPREPLY[@]}"'
1560
- _comp_ltrim_colon_completions " $cur "
1572
+ _comp_compgen_ltrim_colon " ${addresses[@]} "
1561
1573
}
1562
1574
1563
1575
# This function completes on configured network interfaces
1564
1576
#
1565
- # TODO:API: rename per conventions
1566
- _configured_interfaces ()
1577
+ _comp_compgen_configured_interfaces ()
1567
1578
{
1568
1579
local -a files
1569
1580
if [[ -f /etc/debian_version ]]; then
@@ -1598,8 +1609,7 @@ _configured_interfaces()
1598
1609
# -6: IPv6 addresses only
1599
1610
# -a: All addresses
1600
1611
#
1601
- # TODO:API: rename per conventions
1602
- _ip_addresses ()
1612
+ _comp_compgen_ip_addresses ()
1603
1613
{
1604
1614
local n
1605
1615
case ${1-} in
@@ -1608,13 +1618,19 @@ _ip_addresses()
1608
1618
* ) n= ;;
1609
1619
esac
1610
1620
local PATH=$PATH :/sbin
1611
- local addrs=$( {
1621
+ local addrs
1622
+ _comp_compgen -v addrs split -- " $( {
1612
1623
LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show
1613
1624
} 2> /dev/null |
1614
1625
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
1618
1634
}
1619
1635
1620
1636
# This function completes on available kernels
@@ -1629,11 +1645,11 @@ _kernel_versions()
1629
1645
# -a: restrict to active interfaces only
1630
1646
# -w: restrict to wireless interfaces only
1631
1647
#
1632
- # TODO:API: rename per conventions
1633
- _available_interfaces ()
1648
+ _comp_compgen_available_interfaces ()
1634
1649
{
1635
1650
local PATH=$PATH :/sbin
1636
- local generated=$( {
1651
+ local generated
1652
+ _comp_compgen -v generated split -- " $( {
1637
1653
if [[ ${1-} == -w ]]; then
1638
1654
iwconfig
1639
1655
elif [[ ${1-} == -a ]]; then
@@ -1642,9 +1658,8 @@ _available_interfaces()
1642
1658
ifconfig -a || ip -c=never link show || ip link show
1643
1659
fi
1644
1660
} 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:]]/}"'
1648
1663
}
1649
1664
1650
1665
# Echo number of CPUs, falling back to 1 on failure.
0 commit comments