Skip to content

Commit 6b0a466

Browse files
committed
feat(_comp_expand_glob): fail when no paths are generated
1 parent 848aa41 commit 6b0a466

File tree

20 files changed

+40
-68
lines changed

20 files changed

+40
-68
lines changed

bash_completion

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ _comp_upvars()
325325
# parameter expansions, command substitutions, and other expansions will be
326326
# processed. The user-provided strings should not be directly specified to
327327
# this argument.
328+
# @return 0 if at least one path is generated, 1 if no path is generated, or 2
329+
# if the usage is incorrect.
328330
# @since 2.12
329331
_comp_expand_glob()
330332
{
@@ -361,7 +363,7 @@ _comp_expand_glob()
361363
[[ :$_original_opts: == *:nullglob:* ]] || shopt -u nullglob
362364
[[ :$_original_opts: == *:failglob:* ]] && shopt -s failglob
363365
[[ :$_original_opts: == *:noglob:* ]] && set -o noglob
364-
return 0
366+
eval "((\${#$1[@]}))"
365367
}
366368

367369
# Split a string and assign to an array. This function basically performs
@@ -1652,14 +1654,12 @@ _comp_compgen_configured_interfaces()
16521654
local -a files
16531655
if [[ -f /etc/debian_version ]]; then
16541656
# Debian system
1655-
_comp_expand_glob files '/etc/network/interfaces /etc/network/interfaces.d/*'
1656-
((${#files[@]})) || return 0
1657+
_comp_expand_glob files '/etc/network/interfaces /etc/network/interfaces.d/*' || return 0
16571658
_comp_compgen -U files split -- "$(command sed -ne \
16581659
's|^iface \([^ ]\{1,\}\).*$|\1|p' "${files[@]}" 2>/dev/null)"
16591660
elif [[ -f /etc/SuSE-release ]]; then
16601661
# SuSE system
1661-
_comp_expand_glob files '/etc/sysconfig/network/ifcfg-*'
1662-
((${#files[@]})) || return 0
1662+
_comp_expand_glob files '/etc/sysconfig/network/ifcfg-*' || return 0
16631663
_comp_compgen -U files split -- "$(printf '%s\n' "${files[@]}" |
16641664
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
16651665
elif [[ -f /etc/pld-release ]]; then
@@ -1668,8 +1668,7 @@ _comp_compgen_configured_interfaces()
16681668
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
16691669
else
16701670
# Assume Red Hat
1671-
_comp_expand_glob files '/etc/sysconfig/network-scripts/ifcfg-*'
1672-
((${#files[@]})) || return 0
1671+
_comp_expand_glob files '/etc/sysconfig/network-scripts/ifcfg-*' || return 0
16731672
_comp_compgen -U files split -- "$(printf '%s\n' "${files[@]}" |
16741673
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
16751674
fi
@@ -1951,8 +1950,7 @@ _comp_compgen_xinetd_services()
19511950
local xinetddir=${_comp__test_xinetd_dir:-/etc/xinetd.d}
19521951
if [[ -d $xinetddir ]]; then
19531952
local -a svcs
1954-
_comp_expand_glob svcs '$xinetddir/!($_comp_backup_glob)'
1955-
if ((${#svcs[@]})); then
1953+
if _comp_expand_glob svcs '$xinetddir/!($_comp_backup_glob)'; then
19561954
_comp_compgen -U svcs -U xinetddir -- -W '"${svcs[@]#$xinetddir/}"'
19571955
fi
19581956
fi
@@ -2018,10 +2016,11 @@ _comp__init_set_up_service_completions()
20182016
local sysvdirs svc svcdir svcs
20192017
_comp_sysvdirs &&
20202018
for svcdir in "${sysvdirs[@]}"; do
2021-
_comp_expand_glob svcs '"$svcdir"/!($_comp_backup_glob)'
2022-
for svc in "${svcs[@]}"; do
2023-
[[ -x $svc ]] && complete -F _comp_complete_service "$svc"
2024-
done
2019+
if _comp_expand_glob svcs '"$svcdir"/!($_comp_backup_glob)'; then
2020+
for svc in "${svcs[@]}"; do
2021+
[[ -x $svc ]] && complete -F _comp_complete_service "$svc"
2022+
done
2023+
fi
20252024
done
20262025
unset -f "$FUNCNAME"
20272026
}
@@ -2355,8 +2354,7 @@ _comp_compgen_terms()
23552354
{
23562355
toe -a || toe
23572356
} | _comp_awk '{ print $1 }'
2358-
_comp_expand_glob dirs '/{etc,lib,usr/lib,usr/share}/terminfo/?'
2359-
((${#dirs[@]})) &&
2357+
_comp_expand_glob dirs '/{etc,lib,usr/lib,usr/share}/terminfo/?' &&
23602358
find "${dirs[@]}" -type f -maxdepth 1 |
23612359
_comp_awk -F / '{ print $NF }'
23622360
} 2>/dev/null)"
@@ -2455,9 +2453,8 @@ _comp__included_ssh_config_files()
24552453
i="${relative_include_base}/${i}"
24562454
fi
24572455
_comp_expand_tilde "$i"
2458-
_comp_expand_glob files '$REPLY'
2459-
# In case the expanded variable contains multiple paths
2460-
if ((${#files[@]})); then
2456+
if _comp_expand_glob files '$REPLY'; then
2457+
# In case the expanded variable contains multiple paths
24612458
for f in "${files[@]}"; do
24622459
if [[ -r $f && ! -d $f ]]; then
24632460
config+=("$f")
@@ -2592,8 +2589,7 @@ _comp_compgen_known_hosts__impl()
25922589
done
25932590
for i in /etc/ssh2/knownhosts ~/.ssh2/hostkeys; do
25942591
[[ -d $i ]] || continue
2595-
_comp_expand_glob tmpkh '"$i"/*.pub'
2596-
((${#tmpkh[@]})) && khd+=("${tmpkh[@]}")
2592+
_comp_expand_glob tmpkh '"$i"/*.pub' && khd+=("${tmpkh[@]}")
25972593
done
25982594
fi
25992595

completions/_rtcwake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ _comp_cmd_rtcwake()
1717
return
1818
;;
1919
--device | -d)
20-
_comp_expand_glob COMPREPLY '/dev/rtc?*'
21-
((${#COMPREPLY[@]})) &&
20+
_comp_expand_glob COMPREPLY '/dev/rtc?*' &&
2221
_comp_compgen -- -W '"${COMPREPLY[@]#/dev/}"'
2322
return
2423
;;

completions/_yum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ _comp_cmd_yum__compgen_repolist()
3333
_comp_cmd_yum__compgen_plugins()
3434
{
3535
local -a files
36-
_comp_expand_glob files '/usr/lib/yum-plugins/*.py{,c,o}'
37-
((${#files[@]})) || return
36+
_comp_expand_glob files '/usr/lib/yum-plugins/*.py{,c,o}' || return
3837
_comp_compgen -U files split -- "$(
3938
printf '%s\n' "${files[@]}" |
4039
command sed -ne 's|.*/\([^./]*\)\.py[co]\{0,1\}$|\1|p' | sort -u

completions/dpkg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ _comp_cmd_dpkg_reconfigure()
157157
# shellcheck disable=SC2254
158158
case $prev in
159159
--frontend | -${noargopts}f)
160-
_comp_expand_glob opt '/usr/share/perl5/Debconf/FrontEnd/*'
161-
if ((${#opt[@]})); then
160+
if _comp_expand_glob opt '/usr/share/perl5/Debconf/FrontEnd/*'; then
162161
opt=("${opt[@]##*/}")
163162
opt=("${opt[@]%.pm}")
164163
_comp_compgen -- -W '"${opt[@]}"'

completions/hcitool

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ _comp_cmd_hciattach()
337337
_comp_count_args
338338
case $REPLY in
339339
1)
340-
_comp_expand_glob COMPREPLY '/dev/tty*'
341-
((${#COMPREPLY[@]})) &&
340+
_comp_expand_glob COMPREPLY '/dev/tty*' &&
342341
_comp_compgen -- -W '"${COMPREPLY[@]}"
343342
"${COMPREPLY[@]#/dev/}"'
344343
;;

completions/hunspell

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ _comp_cmd_hunspell()
1111
;;
1212
-d)
1313
local -a dicts
14-
_comp_expand_glob dicts '/usr/share/hunspell/*.dic /usr/local/share/hunspell/*.dic'
15-
if ((${#dicts[@]})); then
14+
if _comp_expand_glob dicts '/usr/share/hunspell/*.dic /usr/local/share/hunspell/*.dic'; then
1615
dicts=("${dicts[@]##*/}")
1716
dicts=("${dicts[@]%.dic}")
1817
_comp_compgen -- -W '"${dicts[@]}"'

completions/info

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ _comp_cmd_info()
5353
if ((${#infopath[@]})); then
5454
_comp_compgen -Rv infopath -- -S "/$cur*" -W '"${infopath[@]}"'
5555
local IFS=
56-
_comp_expand_glob COMPREPLY '${infopath[@]}'
57-
_comp_unlocal IFS
58-
59-
if ((${#COMPREPLY[@]})); then
56+
if _comp_expand_glob COMPREPLY '${infopath[@]}'; then
6057
# weed out directory path names and paths to info pages (empty
6158
# elements will be removed by the later `compgen -X ''`)
6259
COMPREPLY=("${COMPREPLY[@]##*/?(:)}")
@@ -65,6 +62,7 @@ _comp_cmd_info()
6562
# weed out info dir file with -X 'dir'
6663
_comp_compgen -c "${cur//\\\\/}" -- -W '"${COMPREPLY[@]%.*}"' -X '@(|dir)'
6764
fi
65+
_comp_unlocal IFS
6866
fi
6967
} &&
7068
complete -F _comp_cmd_info info pinfo

completions/ipmitool

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ _comp_cmd_ipmitool()
1717
;;
1818
-*d)
1919
local -a files
20-
_comp_expand_glob files '/dev/ipmi* /dev/ipmi/* /dev/ipmidev/*'
21-
((${#files[@]})) &&
20+
_comp_expand_glob files '/dev/ipmi* /dev/ipmi/* /dev/ipmidev/*' &&
2221
_comp_compgen -- -W '"${files[@]##*([^0-9])}"' -X '![0-9]*'
2322
return
2423
;;

completions/java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ _comp_cmd_java__packages()
116116
# parse each sourcepath element for packages
117117
for i in "${sourcepaths[@]}"; do
118118
if [[ -d $i ]]; then
119-
_comp_expand_glob files '"$i/$cur"*'
120-
((${#files[@]})) || continue
119+
_comp_expand_glob files '"$i/$cur"*' || continue
121120
_comp_split -la COMPREPLY "$(
122121
command ls -F -d "${files[@]}" 2>/dev/null |
123122
command sed -e 's|^'"$i"'/||'

completions/lintian

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
_comp_cmd_lintian__tags()
44
{
55
local search tags check_files
6-
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc'
7-
((${#check_files[@]})) || return 0
6+
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc' || return 0
87

98
tags=$(_comp_awk '/^Tag/ { print $2 }' "${check_files[@]}")
109
if [[ $cur == *, ]]; then
@@ -23,8 +22,7 @@ _comp_cmd_lintian__tags()
2322
_comp_cmd_lintian__checks()
2423
{
2524
local match search todisable checks check_files
26-
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc'
27-
((${#check_files[@]})) || return 0
25+
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc' || return 0
2826

2927
checks=$(_comp_awk '/^(Check-Script|Abbrev)/ { print $2 }' \
3028
"${check_files[@]}")
@@ -49,8 +47,7 @@ _comp_cmd_lintian__checks()
4947
_comp_cmd_lintian__infos()
5048
{
5149
local search infos collection_files
52-
_comp_expand_glob collection_files '/usr/share/lintian/collection/*.desc'
53-
((${#collection_files[@]})) || return 0
50+
_comp_expand_glob collection_files '/usr/share/lintian/collection/*.desc' || return 0
5451

5552
infos=$(_comp_awk '/^Collector/ { print $2 }' \
5653
"${collection_files[@]}")

0 commit comments

Comments
 (0)