@@ -721,20 +721,13 @@ _comp_quote_compgen()
721
721
#
722
722
_filedir ()
723
723
{
724
- local IFS=$' \n '
725
-
726
724
_tilde " ${cur-} " || return
727
725
728
726
local -a toks
729
- local reset arg=${1-}
727
+ local arg=${1-}
730
728
731
729
if [[ $arg == -d ]]; then
732
- reset=$( shopt -po noglob)
733
- set -o noglob
734
- toks=($( compgen -d -- " ${cur-} " ) )
735
- IFS=' '
736
- $reset
737
- IFS=$' \n '
730
+ _comp_compgen toks -d -- " ${cur-} "
738
731
else
739
732
local ret
740
733
_comp_quote_compgen " ${cur-} "
@@ -755,23 +748,12 @@ _filedir()
755
748
! ${plusdirs-} ]] ||
756
749
opts+=(" ${plusdirs[@]} " )
757
750
758
- reset=$( shopt -po noglob)
759
- set -o noglob
760
- toks+=($( compgen " ${opts[@]} " -- " $quoted " ) )
761
- IFS=' '
762
- $reset
763
- IFS=$' \n '
751
+ _comp_compgen toks " ${opts[@]} " -- " $quoted "
764
752
765
753
# Try without filter if it failed to produce anything and configured to
766
754
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-} } &&
767
- $arg && ${# toks[@]} -lt 1 ]] && {
768
- reset=$( shopt -po noglob)
769
- set -o noglob
770
- toks+=($( compgen -f ${plusdirs+" ${plusdirs[@]} " } -- " $quoted " ) )
771
- IFS=' '
772
- $reset
773
- IFS=$' \n '
774
- }
755
+ $arg && ${# toks[@]} -lt 1 ]] &&
756
+ _comp_compgen -a toks -f ${plusdirs+" ${plusdirs[@]} " } -- " $quoted "
775
757
fi
776
758
777
759
if (( ${# toks[@]} != 0 )) ; then
@@ -826,12 +808,8 @@ _variables()
826
808
return 0
827
809
elif [[ $cur =~ ^(\$\{ [#! ]?)([A-Za-z0-9_]*)\[([^]]*)$ ]]; then
828
810
# Complete ${array[i with ${array[idx]}
829
- local reset= $( shopt -po noglob) IFS= $' \n '
830
- set -o noglob
831
- COMPREPLY+= ($( compgen -W ' $(printf %s\\n "${!' " ${BASH_REMATCH[2]} " ' [@]}")' \
832
- -P " ${BASH_REMATCH[1]}${BASH_REMATCH[2]} [" -S ' ]}' -- " ${BASH_REMATCH[3]} " ) )
833
- IFS= $' \t\n '
834
- $reset
811
+ _comp_compgen -a COMPREPLY -W ' "${!' " ${BASH_REMATCH[2]} " ' [@]}"' \
812
+ -P " ${BASH_REMATCH[1]}${BASH_REMATCH[2]} [" -S ' ]}' -- " ${BASH_REMATCH[3]} "
835
813
# Complete ${arr[@ and ${arr[*
836
814
if [[ ${BASH_REMATCH[3]} == [@* ] ]]; then
837
815
COMPREPLY+=(" ${BASH_REMATCH[1]}${BASH_REMATCH[2]} [${BASH_REMATCH[3]} ]}" )
@@ -1057,28 +1035,27 @@ _comp_initialize()
1057
1035
# @return True (0) if an option was found, False (> 0) otherwise
1058
1036
__parse_options ()
1059
1037
{
1060
- local option option2 i IFS= $' \t\n ,/| '
1038
+ local option option2 i
1061
1039
1062
1040
# Take first found long option, or first one (short) if not found.
1063
1041
option=
1064
- local reset=$( shopt -po noglob)
1065
- set -o noglob
1066
- local -a array=($1 )
1067
- $reset
1068
- for i in " ${array[@]} " ; do
1069
- case " $i " in
1070
- ---* ) break ;;
1071
- --?* )
1072
- option=$i
1073
- break
1074
- ;;
1075
- -?* ) [[ $option ]] || option=$i ;;
1076
- * ) break ;;
1077
- esac
1078
- done
1042
+ local -a array
1043
+ if _comp_split -F $' \t\n ,/|' array " $1 " ; then
1044
+ for i in " ${array[@]} " ; do
1045
+ case " $i " in
1046
+ ---* ) break ;;
1047
+ --?* )
1048
+ option=$i
1049
+ break
1050
+ ;;
1051
+ -?* ) [[ $option ]] || option=$i ;;
1052
+ * ) break ;;
1053
+ esac
1054
+ done
1055
+ fi
1079
1056
[[ $option ]] || return 1
1080
1057
1081
- IFS=$' \t\n ' # affects parsing of the regexps below...
1058
+ local IFS=$' \t\n ' # affects parsing of the regexps below...
1082
1059
1083
1060
# Expand --[no]foo to --foo and --nofoo etc
1084
1061
if [[ $option =~ (\[ (( no| dont)-? )\]). ]]; then
@@ -1335,12 +1312,13 @@ _tilde()
1335
1312
local result=0
1336
1313
if [[ ${1-} == \~ * && $1 != * /* ]]; then
1337
1314
# Try generate ~username completions
1338
- COMPREPLY=($( compgen -P ' ~' -u -- " ${1# \~ } " ) )
1339
- result=${# COMPREPLY[@]}
1340
- # 2>/dev/null for direct invocation, e.g. in the _tilde unit test
1341
- (( result > 0 )) && compopt -o filenames 2> /dev/null
1315
+ if _comp_compgen COMPREPLY -P ' ~' -u -- " ${1# \~ } " ; then
1316
+ # 2>/dev/null for direct invocation, e.g. in the _tilde unit test
1317
+ compopt -o filenames 2> /dev/null
1318
+ return 1
1319
+ fi
1342
1320
fi
1343
- return " $(( result > 0 ? 1 : 0 )) "
1321
+ return 0
1344
1322
}
1345
1323
1346
1324
# Expand variable starting with tilde (~)
@@ -1879,12 +1857,10 @@ _included_ssh_config_files()
1879
1857
local configfile i files f
1880
1858
configfile=$1
1881
1859
1882
- local IFS=$' \t\n ' reset=$( shopt -po noglob)
1883
- set -o noglob
1884
- local included=($( command sed -ne ' s/^[[:blank:]]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][[:blank:]]\(.*\)$/\1/p' " ${configfile} " ) )
1885
- $reset
1860
+ local -a included
1861
+ _comp_split included " $( command sed -ne ' s/^[[:blank:]]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee][[:blank:]]\(.*\)$/\1/p' " ${configfile} " ) "
1862
+ (( ${# included[@]} )) || return
1886
1863
1887
- [[ ${included-} ]] || return
1888
1864
for i in " ${included[@]} " ; do
1889
1865
# Check the origin of $configfile to complete relative included paths on included
1890
1866
# files according to ssh_config(5):
@@ -2419,16 +2395,9 @@ _filedir_xspec()
2419
2395
_comp_quote_compgen " $cur "
2420
2396
local quoted=$ret
2421
2397
2422
- local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
2398
+ local xspec=${_xspecs[${1##*/}]} tmp
2423
2399
local -a toks
2424
-
2425
- toks=($(
2426
- compgen -d -- " $quoted " | {
2427
- while read -r tmp; do
2428
- printf ' %s\n' " $tmp "
2429
- done
2430
- }
2431
- ) )
2400
+ _comp_compgen toks -d -- " $quoted "
2432
2401
2433
2402
# Munge xspec to contain uppercase version too
2434
2403
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html
@@ -2441,24 +2410,12 @@ _filedir_xspec()
2441
2410
fi
2442
2411
xspec=" $matchop ($xspec | ${xspec^^} )"
2443
2412
2444
- toks+=($(
2445
- eval compgen -f -X " '!$xspec '" -- ' $quoted' | {
2446
- while read -r tmp; do
2447
- [[ $tmp ]] && printf ' %s\n' " $tmp "
2448
- done
2449
- }
2450
- ) )
2413
+ _comp_compgen -a toks -f -X " @ (|! ($xspec ))" -- " $quoted "
2451
2414
2452
2415
# Try without filter if it failed to produce anything and configured to
2453
2416
[[ ${BASH_COMPLETION_FILEDIR_FALLBACK-${COMP_FILEDIR_FALLBACK-} } &&
2454
- ${# toks[@]} -lt 1 ]] && {
2455
- local reset=$( shopt -po noglob)
2456
- set -o noglob
2457
- toks+=($( compgen -f -- " $quoted " ) )
2458
- IFS=' '
2459
- $reset
2460
- IFS=$'\n'
2461
- }
2417
+ ${# toks[@]} -lt 1 ]] &&
2418
+ _comp_compgen -a toks -f -- " $quoted "
2462
2419
2463
2420
if ((${# toks[@]} != 0)); then
2464
2421
compopt -o filenames
0 commit comments