Skip to content

Commit 99b2894

Browse files
committed
Initial implementation of _reply_compgen_array
1 parent 6663709 commit 99b2894

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

contrib/mount

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77
have mount &&
88
{
99

10+
# Just like COMPREPLY=(`compgen -W "${COMPREPLY[*]}" -- "$cur"`), only better!
11+
# This will correctly escape special characters in COMPREPLY.
12+
_reply_compgen_array()
13+
{
14+
# Create the argument for compgen -W by escaping twice.
15+
#
16+
# One round of escape is because we want to reply with escaped arguments. A
17+
# second round is required because compgen -W will helpfully expand it's
18+
# argument.
19+
local wlist
20+
for i in ${!COMPREPLY[*]}; do
21+
local q=`printf %q "${COMPREPLY[$i]}"`
22+
wlist+=$(quote "$q")$'\n'
23+
done
24+
25+
# We also have to add another round of escaping to $cur.
26+
local ecur="$cur"
27+
ecur="${ecur//\\/\\\\}"
28+
ecur="${ecur/#$\'/\$\'}"
29+
30+
# Actually generate completions.
31+
IFS=$'\n' eval 'COMPREPLY=(`compgen -W "$wlist" -- "${ecur}"`)'
32+
33+
# Strip starting $' in reply if present in cur.
34+
# This is necesarry because readline interprets everything after ' as a
35+
# separate word for completion.
36+
if [[ $cur == $\'* ]]; then
37+
COMPREPLY=( "${COMPREPLY[@]/#$\'}" )
38+
fi
39+
}
40+
1041
_mount()
1142
{
1243
local cur sm host prev
@@ -48,7 +79,14 @@ _mount()
4879
elif [ $prev = -U ]; then
4980
COMPREPLY=( $( compgen -W '$(sed -ne "s/^[[:space:]]*UUID=\([^[:space:]]*\).*/\1/p" /etc/fstab )' -- "$cur" ) )
5081
else
51-
COMPREPLY=( $( compgen -W "$( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' /etc/fstab )" -- "$cur" ) )
82+
COMPREPLY=()
83+
while read -r fs_spec fs_file fs_other; do
84+
if [[ $fs_spec = [#]* ]]; then continue; fi
85+
[[ $fs_spec = */* ]] && { IFS=$'\0' eval "COMPRELPY+=( $'$fs_spec' )"; }
86+
[[ $fs_file = */* ]] && { IFS=$'\0' eval "COMPREPLY+=( $'$fs_file' )"; }
87+
done < /etc/fstab
88+
89+
_reply_compgen_array
5290
fi
5391
fi
5492

0 commit comments

Comments
 (0)