Skip to content

Commit b63d25a

Browse files
authored
Merge pull request #1085 from akinomyoga/_comp_split-2
fix: replace unquoted array assignments with `_comp_{split,compgen_split,expand_glob}`
2 parents 597f62f + a1dd4d0 commit b63d25a

Some content is hidden

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

46 files changed

+149
-217
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/_adb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ _comp_cmd_adb()
3939
_comp_compgen -av tmp help -- help
4040
fi
4141
if [[ ! $cur || $cur != -* ]]; then
42-
tmp+=($("$1" help 2>&1 | _comp_awk '$1 == "adb" { print $2 }'))
42+
_comp_split -a tmp "$("$1" help 2>&1 | _comp_awk '$1 == "adb" { print $2 }')"
4343
tmp+=(devices connect disconnect sideload)
4444
fi
4545
((${#tmp[@]})) &&

completions/_mock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _comp_cmd_mock()
2828
;;
2929
-r | --root)
3030
_comp_compgen_split -- "$(command ls "$cfgdir")" &&
31-
COMPREPLY=(${COMPREPLY[@]/%.cfg/})
31+
COMPREPLY=("${COMPREPLY[@]/%.cfg/}")
3232
return
3333
;;
3434
--configdir | --resultdir)

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/aspell

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _comp_cmd_aspell__dictionary()
1111
COMPREPLY=("${COMPREPLY[@]#$datadir/}")
1212
fi
1313
# Then, add the canonical dicts
14-
COMPREPLY+=($("$aspell" dicts 2>/dev/null))
14+
_comp_split -a COMPREPLY "$("$aspell" dicts 2>/dev/null)"
1515
((${#COMPREPLY[@]})) &&
1616
_comp_compgen -- -X '\*' -W '"${COMPREPLY[@]}"'
1717
}

completions/complete

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ _comp_cmd_complete()
2929
return
3030
;;
3131
-*p | -*r)
32-
COMPREPLY=($(complete -p | command sed -e 's|.* ||'))
33-
((${#COMPREPLY[@]})) &&
34-
_comp_compgen -- -W '"${COMPREPLY[@]}"'
32+
_comp_compgen_split -l -- "$(complete -p | command sed -e 's|.* ||')"
3533
return
3634
;;
3735
esac

completions/curl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ _comp_cmd_curl()
103103
return
104104
;;
105105
--help | -${noargopts}h)
106-
local x categories=(
107-
$("$1" --help non-existent-category 2>&1 |
108-
_comp_awk '/^[ \t]/ {print $1}')
109-
)
110-
if ((${#categories[@]})); then
106+
local x categories
107+
if _comp_split categories "$("$1" --help non-existent-category 2>&1 |
108+
_comp_awk '/^[ \t]/ {print $1}')"; then
111109
for x in "${categories[@]}"; do
112110
# Looks like an option? Likely no --help category support
113111
[[ $x != -* ]] || return

completions/cvs

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
_comp_deprecate_var 2.12 \
44
COMP_CVS_REMOTE BASH_COMPLETION_CMD_CVS_REMOTE
55

6-
_comp_cmd_cvs__entries()
6+
# Usage: _comp_cmd_cvs__compgen_entries [base_path]
7+
# @param[opt] $1
8+
# shellcheck disable=SC2120
9+
_comp_cmd_cvs__compgen_entries()
710
{
8-
local prefix=${cur%/*}/ IFS=$'\n'
9-
[[ -e ${prefix-}CVS/Entries ]] || prefix=""
10-
entries=($(cut -d/ -f2 -s "${prefix-}CVS/Entries" 2>/dev/null))
11-
if ((${#entries[@]})); then
12-
_comp_compgen -Rv entries -- -P "${prefix-}" -W '"${entries[@]}"'
11+
local base_path=${1-$cur}
12+
local _prefix=${base_path%/*}/
13+
[[ -e ${_prefix-}CVS/Entries ]] || _prefix=""
14+
_comp_compgen -c "${cur#"$_prefix"}" split -lP "$_prefix" -- "$(cut -d/ -f2 -s "${_prefix-}CVS/Entries" 2>/dev/null)" &&
1315
compopt -o filenames
14-
fi
1516
}
1617

1718
_comp_cmd_cvs__modules()
1819
{
19-
COMPREPLY=($(command ls -d "${cvsroot}${prefix:+/$prefix}"/!(CVSROOT)))
20+
_comp_expand_glob COMPREPLY '"$cvsroot${prefix:+/$prefix}"/!(CVSROOT)'
2021
}
2122

2223
_comp_cmd_cvs__compgen_commands()
@@ -41,7 +42,7 @@ _comp_xfunc_cvs_compgen_roots()
4142
{
4243
local -a cvsroots=()
4344
[[ -v CVSROOT ]] && cvsroots=("$CVSROOT")
44-
[[ -r ~/.cvspass ]] && cvsroots+=($(_comp_awk '{ print $2 }' ~/.cvspass))
45+
[[ -r ~/.cvspass ]] && _comp_split -a cvsroots "$(_comp_awk '{ print $2 }' ~/.cvspass)"
4546
[[ -r CVS/Root ]] && mapfile -tO "${#cvsroots[@]}" cvsroots <CVS/Root
4647
((${#cvsroots[@]})) &&
4748
_comp_compgen -U cvsroots -- -W '"${cvsroots[@]}"'
@@ -56,7 +57,7 @@ _comp_cmd_cvs()
5657
_comp_initialize -n : -- "$@" || return
5758

5859
local count mode="" i cvsroot="" has_cvsroot="" pwd
59-
local -a flags files entries changed newremoved
60+
local -a flags files entries
6061

6162
local noargopts='!(-*|*[d]*)'
6263
count=0
@@ -140,7 +141,7 @@ _comp_cmd_cvs()
140141
;;
141142
esac
142143
elif [[ $i == -* ]]; then
143-
flags+=($i)
144+
flags+=("$i")
144145
fi
145146
((count++))
146147
done
@@ -159,9 +160,12 @@ _comp_cmd_cvs()
159160
esac
160161

161162
if [[ $cur != -* ]]; then
162-
_comp_cmd_cvs__entries
163-
[[ ! $cur ]] && files=(!(CVS)) ||
164-
files=($(command ls -d "${cur}"* 2>/dev/null))
163+
_comp_compgen -Rv entries -i cvs entries "${cur-}"
164+
if [[ ! $cur ]]; then
165+
_comp_expand_glob files '!(CVS)'
166+
else
167+
_comp_expand_glob files '"${cur}"*'
168+
fi
165169
local f
166170
for i in "${!files[@]}"; do
167171
if [[ ${files[i]} == ?(*/)CVS ]]; then
@@ -201,9 +205,7 @@ _comp_cmd_cvs()
201205
if [[ $cur == -* ]]; then
202206
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
203207
else
204-
_comp_cmd_cvs__entries
205-
((${#entries[@]})) &&
206-
_comp_compgen -- -W '"${entries[@]}"'
208+
_comp_cmd_cvs__compgen_entries
207209
fi
208210
;;
209211
annotate)
@@ -212,9 +214,7 @@ _comp_cmd_cvs()
212214
if [[ $cur == -* ]]; then
213215
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
214216
else
215-
_comp_cmd_cvs__entries
216-
((${#entries[@]})) &&
217-
_comp_compgen -- -W '"${entries[@]}"'
217+
_comp_cmd_cvs__compgen_entries
218218
fi
219219
;;
220220
checkout)
@@ -235,10 +235,8 @@ _comp_cmd_cvs()
235235

236236
if [[ $cur != -* ]]; then
237237
[[ ! $has_cvsroot ]] && cvsroot=${CVSROOT-}
238-
COMPREPLY=($(cvs -d "$cvsroot" co -c 2>/dev/null |
239-
_comp_awk '{print $1}'))
240-
((${#COMPREPLY[@]})) &&
241-
_comp_compgen -- -W '"${COMPREPLY[@]}"'
238+
_comp_compgen_split -- "$(cvs -d "$cvsroot" co -c 2>/dev/null |
239+
_comp_awk '{print $1}')"
242240
else
243241
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
244242
fi
@@ -262,19 +260,16 @@ _comp_cmd_cvs()
262260
if [[ ${BASH_COMPLETION_CMD_CVS_REMOTE-} ]]; then
263261
# this is the least computationally intensive way found so
264262
# far, but other changes (something other than
265-
# changed/removed/new) may be missing
266-
changed=($(cvs -q diff --brief 2>&1 |
267-
command sed -ne 's/^Files [^ ]* and \([^ ]*\) differ$/\1/p'))
268-
newremoved=($(cvs -q diff --brief 2>&1 |
269-
command sed -ne 's/^cvs diff: \([^ ]*\) .*, no comparison available$/\1/p'))
270-
((${#changed[@]})) && COMPREPLY+=("${changed[@]}")
271-
((${#newremoved[@]})) && COMPREPLY+=("${newremoved[@]}")
272-
((${#COMPREPLY[@]})) &&
273-
_comp_compgen -- -W '"${COMPREPLY[@]}"'
263+
# changed/removed/new) may be missing.
264+
_comp_compgen -a split -- "$(cvs -q diff --brief 2>&1 |
265+
command sed -ne '
266+
# changed
267+
s/^Files [^ ]* and \([^ ]*\) differ$/\1/p
268+
# new/removed
269+
s/^cvs diff: \([^ ]*\) .*, no comparison available$/\1/p
270+
')"
274271
else
275-
_comp_cmd_cvs__entries
276-
((${#entries[@]})) &&
277-
_comp_compgen -- -W '"${entries[@]}"'
272+
_comp_cmd_cvs__compgen_entries
278273
fi
279274
else
280275
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
@@ -288,18 +283,14 @@ _comp_cmd_cvs()
288283
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
289284
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
290285
else
291-
_comp_cmd_cvs__entries
292-
((${#entries[@]})) &&
293-
_comp_compgen -- -W '"${entries[@]}"'
286+
_comp_cmd_cvs__compgen_entries
294287
fi
295288
;;
296289
editors | watchers)
297290
if [[ $cur == -* ]]; then
298291
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
299292
else
300-
_comp_cmd_cvs__entries
301-
((${#entries[@]})) &&
302-
_comp_compgen -- -W '"${entries[@]}"'
293+
_comp_cmd_cvs__compgen_entries
303294
fi
304295
;;
305296
export)
@@ -320,9 +311,8 @@ _comp_cmd_cvs()
320311

321312
if [[ $cur != -* ]]; then
322313
[[ ! $has_cvsroot ]] && cvsroot=${CVSROOT-}
323-
COMPREPLY=($(cvs -d "$cvsroot" co -c | _comp_awk '{print $1}'))
324-
((${#COMPREPLY[@]})) &&
325-
_comp_compgen -- -W '"${COMPREPLY[@]}"'
314+
_comp_compgen_split -- "$(cvs -d "$cvsroot" co -c |
315+
_comp_awk '{print $1}')"
326316
else
327317
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
328318
fi
@@ -345,8 +335,8 @@ _comp_cmd_cvs()
345335
local prefix=${cur%/*}
346336
if [[ -r ${cvsroot}/${prefix} ]]; then
347337
_comp_cmd_cvs__modules
348-
COMPREPLY=(${COMPREPLY[@]#"$cvsroot"})
349-
COMPREPLY=(${COMPREPLY[@]#\/})
338+
COMPREPLY=("${COMPREPLY[@]#"$cvsroot"}")
339+
COMPREPLY=("${COMPREPLY[@]#\/}")
350340
fi
351341
pwd=$(pwd)
352342
pwd=${pwd##*/}
@@ -359,7 +349,7 @@ _comp_cmd_cvs()
359349
;;
360350
remove)
361351
if [[ $cur != -* ]]; then
362-
_comp_cmd_cvs__entries
352+
_comp_compgen -Rv entries -i cvs entries "${cur-}"
363353
if [[ $prev != -f ]]; then
364354
# find out what files are missing
365355
for i in "${!entries[@]}"; do
@@ -387,9 +377,7 @@ _comp_cmd_cvs()
387377
if [[ $cur == -* ]]; then
388378
_comp_cmd_cvs__compgen_command_options "$1" "$mode"
389379
else
390-
_comp_cmd_cvs__entries
391-
((${#entries[@]})) &&
392-
_comp_compgen -- -W '"${entries[@]}"'
380+
_comp_cmd_cvs__compgen_entries
393381
fi
394382
;;
395383
"")

completions/declare

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _comp_cmd_declare()
1010
_comp_compgen -Rv opts usage -c help -s "$1"
1111
# Most options also have a '+' form.
1212
# We'll exclude the ones that don't with compgen.
13-
opts+=(${opts[*]/-/+})
13+
opts+=("${opts[@]/-/+}")
1414
_comp_compgen -- -W "${opts[*]}" -X '+[Ffgp]'
1515
return
1616
fi

0 commit comments

Comments
 (0)