Skip to content

Commit

Permalink
feat(_comp_expand_glob): fail when no paths are generated
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 14, 2024
1 parent 848aa41 commit 6b0a466
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 68 deletions.
36 changes: 16 additions & 20 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ _comp_upvars()
# parameter expansions, command substitutions, and other expansions will be
# processed. The user-provided strings should not be directly specified to
# this argument.
# @return 0 if at least one path is generated, 1 if no path is generated, or 2
# if the usage is incorrect.
# @since 2.12
_comp_expand_glob()
{
Expand Down Expand Up @@ -361,7 +363,7 @@ _comp_expand_glob()
[[ :$_original_opts: == *:nullglob:* ]] || shopt -u nullglob
[[ :$_original_opts: == *:failglob:* ]] && shopt -s failglob
[[ :$_original_opts: == *:noglob:* ]] && set -o noglob
return 0
eval "((\${#$1[@]}))"
}

# Split a string and assign to an array. This function basically performs
Expand Down Expand Up @@ -1652,14 +1654,12 @@ _comp_compgen_configured_interfaces()
local -a files
if [[ -f /etc/debian_version ]]; then
# Debian system
_comp_expand_glob files '/etc/network/interfaces /etc/network/interfaces.d/*'
((${#files[@]})) || return 0
_comp_expand_glob files '/etc/network/interfaces /etc/network/interfaces.d/*' || return 0
_comp_compgen -U files split -- "$(command sed -ne \
's|^iface \([^ ]\{1,\}\).*$|\1|p' "${files[@]}" 2>/dev/null)"
elif [[ -f /etc/SuSE-release ]]; then
# SuSE system
_comp_expand_glob files '/etc/sysconfig/network/ifcfg-*'
((${#files[@]})) || return 0
_comp_expand_glob files '/etc/sysconfig/network/ifcfg-*' || return 0
_comp_compgen -U files split -- "$(printf '%s\n' "${files[@]}" |
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
elif [[ -f /etc/pld-release ]]; then
Expand All @@ -1668,8 +1668,7 @@ _comp_compgen_configured_interfaces()
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
else
# Assume Red Hat
_comp_expand_glob files '/etc/sysconfig/network-scripts/ifcfg-*'
((${#files[@]})) || return 0
_comp_expand_glob files '/etc/sysconfig/network-scripts/ifcfg-*' || return 0
_comp_compgen -U files split -- "$(printf '%s\n' "${files[@]}" |
command sed -ne 's|.*ifcfg-\([^*].*\)$|\1|p')"
fi
Expand Down Expand Up @@ -1951,8 +1950,7 @@ _comp_compgen_xinetd_services()
local xinetddir=${_comp__test_xinetd_dir:-/etc/xinetd.d}
if [[ -d $xinetddir ]]; then
local -a svcs
_comp_expand_glob svcs '$xinetddir/!($_comp_backup_glob)'
if ((${#svcs[@]})); then
if _comp_expand_glob svcs '$xinetddir/!($_comp_backup_glob)'; then
_comp_compgen -U svcs -U xinetddir -- -W '"${svcs[@]#$xinetddir/}"'
fi
fi
Expand Down Expand Up @@ -2018,10 +2016,11 @@ _comp__init_set_up_service_completions()
local sysvdirs svc svcdir svcs
_comp_sysvdirs &&
for svcdir in "${sysvdirs[@]}"; do
_comp_expand_glob svcs '"$svcdir"/!($_comp_backup_glob)'
for svc in "${svcs[@]}"; do
[[ -x $svc ]] && complete -F _comp_complete_service "$svc"
done
if _comp_expand_glob svcs '"$svcdir"/!($_comp_backup_glob)'; then
for svc in "${svcs[@]}"; do
[[ -x $svc ]] && complete -F _comp_complete_service "$svc"
done
fi
done
unset -f "$FUNCNAME"
}
Expand Down Expand Up @@ -2355,8 +2354,7 @@ _comp_compgen_terms()
{
toe -a || toe
} | _comp_awk '{ print $1 }'
_comp_expand_glob dirs '/{etc,lib,usr/lib,usr/share}/terminfo/?'
((${#dirs[@]})) &&
_comp_expand_glob dirs '/{etc,lib,usr/lib,usr/share}/terminfo/?' &&
find "${dirs[@]}" -type f -maxdepth 1 |
_comp_awk -F / '{ print $NF }'
} 2>/dev/null)"
Expand Down Expand Up @@ -2455,9 +2453,8 @@ _comp__included_ssh_config_files()
i="${relative_include_base}/${i}"
fi
_comp_expand_tilde "$i"
_comp_expand_glob files '$REPLY'
# In case the expanded variable contains multiple paths
if ((${#files[@]})); then
if _comp_expand_glob files '$REPLY'; then
# In case the expanded variable contains multiple paths
for f in "${files[@]}"; do
if [[ -r $f && ! -d $f ]]; then
config+=("$f")
Expand Down Expand Up @@ -2592,8 +2589,7 @@ _comp_compgen_known_hosts__impl()
done
for i in /etc/ssh2/knownhosts ~/.ssh2/hostkeys; do
[[ -d $i ]] || continue
_comp_expand_glob tmpkh '"$i"/*.pub'
((${#tmpkh[@]})) && khd+=("${tmpkh[@]}")
_comp_expand_glob tmpkh '"$i"/*.pub' && khd+=("${tmpkh[@]}")
done
fi

Expand Down
3 changes: 1 addition & 2 deletions completions/_rtcwake
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ _comp_cmd_rtcwake()
return
;;
--device | -d)
_comp_expand_glob COMPREPLY '/dev/rtc?*'
((${#COMPREPLY[@]})) &&
_comp_expand_glob COMPREPLY '/dev/rtc?*' &&
_comp_compgen -- -W '"${COMPREPLY[@]#/dev/}"'
return
;;
Expand Down
3 changes: 1 addition & 2 deletions completions/_yum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ _comp_cmd_yum__compgen_repolist()
_comp_cmd_yum__compgen_plugins()
{
local -a files
_comp_expand_glob files '/usr/lib/yum-plugins/*.py{,c,o}'
((${#files[@]})) || return
_comp_expand_glob files '/usr/lib/yum-plugins/*.py{,c,o}' || return
_comp_compgen -U files split -- "$(
printf '%s\n' "${files[@]}" |
command sed -ne 's|.*/\([^./]*\)\.py[co]\{0,1\}$|\1|p' | sort -u
Expand Down
3 changes: 1 addition & 2 deletions completions/dpkg
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ _comp_cmd_dpkg_reconfigure()
# shellcheck disable=SC2254
case $prev in
--frontend | -${noargopts}f)
_comp_expand_glob opt '/usr/share/perl5/Debconf/FrontEnd/*'
if ((${#opt[@]})); then
if _comp_expand_glob opt '/usr/share/perl5/Debconf/FrontEnd/*'; then
opt=("${opt[@]##*/}")
opt=("${opt[@]%.pm}")
_comp_compgen -- -W '"${opt[@]}"'
Expand Down
3 changes: 1 addition & 2 deletions completions/hcitool
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,7 @@ _comp_cmd_hciattach()
_comp_count_args
case $REPLY in
1)
_comp_expand_glob COMPREPLY '/dev/tty*'
((${#COMPREPLY[@]})) &&
_comp_expand_glob COMPREPLY '/dev/tty*' &&
_comp_compgen -- -W '"${COMPREPLY[@]}"
"${COMPREPLY[@]#/dev/}"'
;;
Expand Down
3 changes: 1 addition & 2 deletions completions/hunspell
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ _comp_cmd_hunspell()
;;
-d)
local -a dicts
_comp_expand_glob dicts '/usr/share/hunspell/*.dic /usr/local/share/hunspell/*.dic'
if ((${#dicts[@]})); then
if _comp_expand_glob dicts '/usr/share/hunspell/*.dic /usr/local/share/hunspell/*.dic'; then
dicts=("${dicts[@]##*/}")
dicts=("${dicts[@]%.dic}")
_comp_compgen -- -W '"${dicts[@]}"'
Expand Down
6 changes: 2 additions & 4 deletions completions/info
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ _comp_cmd_info()
if ((${#infopath[@]})); then
_comp_compgen -Rv infopath -- -S "/$cur*" -W '"${infopath[@]}"'
local IFS=
_comp_expand_glob COMPREPLY '${infopath[@]}'
_comp_unlocal IFS

if ((${#COMPREPLY[@]})); then
if _comp_expand_glob COMPREPLY '${infopath[@]}'; then
# weed out directory path names and paths to info pages (empty
# elements will be removed by the later `compgen -X ''`)
COMPREPLY=("${COMPREPLY[@]##*/?(:)}")
Expand All @@ -65,6 +62,7 @@ _comp_cmd_info()
# weed out info dir file with -X 'dir'
_comp_compgen -c "${cur//\\\\/}" -- -W '"${COMPREPLY[@]%.*}"' -X '@(|dir)'
fi
_comp_unlocal IFS
fi
} &&
complete -F _comp_cmd_info info pinfo
Expand Down
3 changes: 1 addition & 2 deletions completions/ipmitool
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ _comp_cmd_ipmitool()
;;
-*d)
local -a files
_comp_expand_glob files '/dev/ipmi* /dev/ipmi/* /dev/ipmidev/*'
((${#files[@]})) &&
_comp_expand_glob files '/dev/ipmi* /dev/ipmi/* /dev/ipmidev/*' &&
_comp_compgen -- -W '"${files[@]##*([^0-9])}"' -X '![0-9]*'
return
;;
Expand Down
3 changes: 1 addition & 2 deletions completions/java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ _comp_cmd_java__packages()
# parse each sourcepath element for packages
for i in "${sourcepaths[@]}"; do
if [[ -d $i ]]; then
_comp_expand_glob files '"$i/$cur"*'
((${#files[@]})) || continue
_comp_expand_glob files '"$i/$cur"*' || continue
_comp_split -la COMPREPLY "$(
command ls -F -d "${files[@]}" 2>/dev/null |
command sed -e 's|^'"$i"'/||'
Expand Down
9 changes: 3 additions & 6 deletions completions/lintian
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
_comp_cmd_lintian__tags()
{
local search tags check_files
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc'
((${#check_files[@]})) || return 0
_comp_expand_glob check_files '/usr/share/lintian/checks/*.desc' || return 0

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

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

infos=$(_comp_awk '/^Collector/ { print $2 }' \
"${collection_files[@]}")
Expand Down
6 changes: 2 additions & 4 deletions completions/man
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,15 @@ _comp_cmd_man()
_comp_compgen -aRv manfiles -- -S "/*cat$sect/$cur*" -W '"${manpath[@]}"'

local IFS=
_comp_expand_glob COMPREPLY '${manfiles[@]}'
_comp_unlocal IFS

if ((${#COMPREPLY[@]} != 0)); then
if _comp_expand_glob COMPREPLY '${manfiles[@]}'; then
# weed out directory path names and paths to man pages (empty
# elements will be removed by the later `compgen -X ''`)
COMPREPLY=("${COMPREPLY[@]##*/?(:)}")
# strip suffix from man pages
COMPREPLY=("${COMPREPLY[@]%$comprsuffix}")
_comp_compgen -c "${cur//\\\\/}" -- -W '"${COMPREPLY[@]%.*}"' -X ''
fi
_comp_unlocal IFS
fi

# shellcheck disable=SC2053
Expand Down
6 changes: 2 additions & 4 deletions completions/minicom
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ _comp_cmd_minicom()
return
;;
--ptty | -${noargopts}p)
_comp_expand_glob COMPREPLY '/dev/tty*'
((${#COMPREPLY[@]})) &&
_comp_expand_glob COMPREPLY '/dev/tty*' &&
_comp_compgen -- -W '"${COMPREPLY[@]}" "${COMPREPLY[@]#/dev/}"'
return
;;
Expand All @@ -33,8 +32,7 @@ _comp_cmd_minicom()
fi

local -a files
_comp_expand_glob files '{/etc/,/etc/minicom/,~/.}minirc.?*'
((${#files[@]})) &&
_comp_expand_glob files '{/etc/,/etc/minicom/,~/.}minirc.?*' &&
_comp_compgen -- -W '"${files[@]##*minirc.}"'
} &&
complete -F _comp_cmd_minicom -o default minicom
Expand Down
3 changes: 1 addition & 2 deletions completions/pkgadd
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ _comp_cmd_pkgadd()
else
local -a pkginst_list
if [[ -d $device ]]; then
_comp_expand_glob pkginst_list '"$device"/*/pkginfo'
if ((${#pkginst_list[@]})); then
if _comp_expand_glob pkginst_list '"$device"/*/pkginfo'; then
pkginst_list=("${pkginst_list[@]#"$device/"}")
pkginst_list=("${pkginst_list[@]%/pkginfo}")
fi
Expand Down
3 changes: 1 addition & 2 deletions completions/ps
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ _comp_cmd_ps()
return
;;
?(-)t | [^-]*t | --tty)
_comp_expand_glob COMPREPLY '/dev/tty*'
((${#COMPREPLY[@]})) &&
_comp_expand_glob COMPREPLY '/dev/tty*' &&
_comp_compgen -- -W '"${COMPREPLY[@]}" "${COMPREPLY[@]#/dev/}"'
return
;;
Expand Down
3 changes: 1 addition & 2 deletions completions/qemu
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ _comp_cmd_qemu()
;;
-k)
local -a keymaps
_comp_expand_glob keymaps '/usr/{local/,}share/qemu/keymaps/!(common|modifiers)'
((${#keymaps[@]})) &&
_comp_expand_glob keymaps '/usr/{local/,}share/qemu/keymaps/!(common|modifiers)' &&
_comp_compgen -- -W '"${keymaps[@]##*/}"'
return
;;
Expand Down
3 changes: 1 addition & 2 deletions completions/screen
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ _comp_cmd_screen__sessions()

if ((cword == 1)); then
if [[ $cur == /dev* ]]; then
_comp_expand_glob COMPREPLY '/dev/serial/*/* /dev/ttyUSB* /dev/ttyACM*'
((${#COMPREPLY[@]})) &&
_comp_expand_glob COMPREPLY '/dev/serial/*/* /dev/ttyUSB* /dev/ttyACM*' &&
_comp_compgen -- -W '"${COMPREPLY[@]}"'
return
fi
Expand Down
3 changes: 1 addition & 2 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,7 @@ _comp_xfunc_scp_compgen_local_files()
fi

local files
_comp_expand_glob files '"$cur"*'
((${#files[@]})) || return 0
_comp_expand_glob files '"$cur"*' || return 0
if [[ $_dirsonly ]]; then
_comp_compgen -U files split -l -- "$(
command ls -aF1dL "${files[@]}" 2>/dev/null |
Expand Down
3 changes: 1 addition & 2 deletions completions/valgrind
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ _comp_cmd_valgrind()
# Tools seem to be named e.g. like memcheck-amd64-linux from which
# we want to grab memcheck.
local -a files
_comp_expand_glob files '/usr{,/local}/lib{,64,exec}{/*-linux-gnu,}/valgrind/*'
if ((${#files[@]})); then
if _comp_expand_glob files '/usr{,/local}/lib{,64,exec}{/*-linux-gnu,}/valgrind/*'; then
_comp_compgen_split -- "$(
for f in "${files[@]}"; do
[[ $f != *.so && -x $f && $f =~ ^.*/(.*)-[^-]+-[^-]+ ]] &&
Expand Down
3 changes: 1 addition & 2 deletions completions/vpnc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ _comp_cmd_vpnc()
else
# config name, /etc/vpnc/<name>.conf
local -a configs
_comp_expand_glob configs '/etc/vpnc/*.conf'
if ((${#configs[@]})); then
if _comp_expand_glob configs '/etc/vpnc/*.conf'; then
configs=("${configs[@]##*/}")
configs=("${configs[@]%.conf}")
compopt -o filenames
Expand Down
3 changes: 1 addition & 2 deletions completions/xdg-mime
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ _comp_cmd_xdg_mime()
default)
if ((args == 2)); then
local -a desktops
_comp_expand_glob desktops '/usr/share/applications/*.desktop'
if ((${#desktops[@]})); then
if _comp_expand_glob desktops '/usr/share/applications/*.desktop'; then
desktops=("${desktops[@]##*/}")
_comp_compgen -- -W '"${desktops[@]}"'
fi
Expand Down

0 comments on commit 6b0a466

Please sign in to comment.