summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCrestez Dan Leonard <cdleonard@gmail.com>2010-02-04 16:31:51 +0200
committerCrestez Dan Leonard <cdleonard@gmail.com>2010-02-04 16:31:51 +0200
commit99b289474ead02cc8c629ceec1192ce9f0b4f84e (patch)
tree148b4ee66bd8b7ca555432684fdd9553ba69df42
parent6663709d7679cfbc770665dc01175ac65b6f5c6f (diff)
downloadbash-completion-99b289474ead02cc8c629ceec1192ce9f0b4f84e.tar.gz
Initial implementation of _reply_compgen_array
-rw-r--r--contrib/mount40
1 files changed, 39 insertions, 1 deletions
diff --git a/contrib/mount b/contrib/mount
index d8ba81bc..c15f798f 100644
--- a/contrib/mount
+++ b/contrib/mount
@@ -7,6 +7,37 @@
have mount &&
{
+# Just like COMPREPLY=(`compgen -W "${COMPREPLY[*]}" -- "$cur"`), only better!
+# This will correctly escape special characters in COMPREPLY.
+_reply_compgen_array()
+{
+ # Create the argument for compgen -W by escaping twice.
+ #
+ # One round of escape is because we want to reply with escaped arguments. A
+ # second round is required because compgen -W will helpfully expand it's
+ # argument.
+ local wlist
+ for i in ${!COMPREPLY[*]}; do
+ local q=`printf %q "${COMPREPLY[$i]}"`
+ wlist+=$(quote "$q")$'\n'
+ done
+
+ # We also have to add another round of escaping to $cur.
+ local ecur="$cur"
+ ecur="${ecur//\\/\\\\}"
+ ecur="${ecur/#$\'/\$\'}"
+
+ # Actually generate completions.
+ IFS=$'\n' eval 'COMPREPLY=(`compgen -W "$wlist" -- "${ecur}"`)'
+
+ # Strip starting $' in reply if present in cur.
+ # This is necesarry because readline interprets everything after ' as a
+ # separate word for completion.
+ if [[ $cur == $\'* ]]; then
+ COMPREPLY=( "${COMPREPLY[@]/#$\'}" )
+ fi
+}
+
_mount()
{
local cur sm host prev
@@ -48,7 +79,14 @@ _mount()
elif [ $prev = -U ]; then
COMPREPLY=( $( compgen -W '$(sed -ne "s/^[[:space:]]*UUID=\([^[:space:]]*\).*/\1/p" /etc/fstab )' -- "$cur" ) )
else
- COMPREPLY=( $( compgen -W "$( awk '! /^[ \t]*#/ {if ($2 ~ /\//) print $2}' /etc/fstab )" -- "$cur" ) )
+ COMPREPLY=()
+ while read -r fs_spec fs_file fs_other; do
+ if [[ $fs_spec = [#]* ]]; then continue; fi
+ [[ $fs_spec = */* ]] && { IFS=$'\0' eval "COMPRELPY+=( $'$fs_spec' )"; }
+ [[ $fs_file = */* ]] && { IFS=$'\0' eval "COMPREPLY+=( $'$fs_file' )"; }
+ done < /etc/fstab
+
+ _reply_compgen_array
fi
fi