From 99b289474ead02cc8c629ceec1192ce9f0b4f84e Mon Sep 17 00:00:00 2001 From: Crestez Dan Leonard Date: Thu, 4 Feb 2010 16:31:51 +0200 Subject: Initial implementation of _reply_compgen_array --- contrib/mount | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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 -- cgit v1.2.1