On Sun, 25 Jan 2015 23:54:02 +0300 Alexander Galanin wrote:
>
> bash completion for pmount hangs. The problem is in grepping stdin in
> line 62 of /etc/bash_completion.d/pmount:
> 
>     devices="$( command ls $(grep -v '^[[:space:]]*#' /etc/pmount.allow ) 
> $(grep 1 /sys/block/*/removable | sed -e 
> 's,/sys/block/,/dev/,;s,/removable:1,*,') 2>/dev/null | sort -u | sed -e 
> 's,\(^/dev/\)\(.*\),\1\2 \2,' ; grep $mdir /proc/mounts | sed -e 
> 's,.*\($mdir/[^ ]*\).*,\1,' )"

It seems the problem was introduced in the commit 539bf17b for the bug 678713
"pumount tab completion doesn't support mounted paths" that was made in
the _pmount() function (line 62) instead of _pumount(), line 87.
See changelog entry:

> pmount (0.9.23-3) unstable; urgency=low
...
>   * Add bash completion for mounted paths in pumount (closes: #678713) .
>     Thanks to Aaron Small <@> for the patch.
> 
>  -- Vincent Fourmond <fourm...@debian.org>  Sun, 18 May 2014 10:03:13 +0200

Unfortunately original line 87 and the fix suggested in the bug 678713
has variable expansion $mdir prevented by single quotes

87c87
<       devices="$( grep $mdir /proc/mounts | sed -e 's,.*/$mdir/,,;s,\ 
.*,,;s,\(.*\),\1\n/dev/\1,;s,/dev//dev,/dev,' )"
---
>       devices="$( grep $mdir /proc/mounts | sed -e 's,.*/$mdir/,,;s,\ 
> .*,,;s,\(.*\),\1\n/dev/\1,;s,/dev//dev,/dev,';grep $mdir /proc/mounts | sed 
> -e 's,.*\($mdir/[^ ]*\).*,\1,' )"

Notice s,/dev//dev,/dev, that is necessary due to $mdir is not expanded to 
/media.
Moreover $mdir already contains leading slash so extra one leads to pattern
matching failure.

As I see the intention is provide /media completion option in addition
to /dev and directory name shortcut. The following variant works for me
unless I explicitly specify mount directory with e.g. spaces (otherwise
two of three completion options contain 040 sequence). I have found
tedious to get 3 variants for pumount when just one device is attached
that is why I have added one more "if" for immediate completion.

At least the _pmount() function should be reverted (first chunk),
_pumount() does its work even in the current state.

--- pmount.orig 2014-05-18 15:13:54.000000000 +0700
+++ pmount      2018-04-30 11:20:21.680935069 +0700
@@ -59,7 +59,7 @@
        if [[ "$cur" == -* ]]; then
                COMPREPLY=( $( compgen -W "$options" -- $cur ) )
        else
-    devices="$( command ls $(grep -v '^[[:space:]]*#' /etc/pmount.allow ) 
$(grep 1 /sys/block/*/removable | sed -e 
's,/sys/block/,/dev/,;s,/removable:1,*,') 2>/dev/null | sort -u | sed -e 
's,\(^/dev/\)\(.*\),\1\2 \2,' ; grep $mdir /proc/mounts | sed -e 's,.*\($mdir/[^
]*\).*,\1,' )"
+    devices="$( command ls $(grep -v '^[[:space:]]*#' /etc/pmount.allow ) 
$(grep 1 /sys/block/*/removable | sed -e 
's,/sys/block/,/dev/,;s,/removable:1,*,') 2>/dev/null | sort -u | sed -e 
's,\(^/dev/\)\(.*\),\1\2 \2,' )"
                COMPREPLY=( $( compgen -W "$devices" -- $cur ) )
        fi

@@ -84,7 +84,13 @@
    if [[ "$cur" == -* ]]; then
       COMPREPLY=( $( compgen -W "$options" -- $cur ) )
    else
-      devices="$( grep $mdir /proc/mounts | sed -e 's,.*/$mdir/,,;s,\ 
.*,,;s,\(.*\),\1\n/dev/\1,;s,/dev//dev,/dev,' )"
+      if [ -z "$cur" -a 1 = "$(grep -c " $mdir" /proc/mounts)" ]; then
+         # Do not show variants if just one device is mounted, use /dev/sdXY.
+         devices="$(sed -n -e "s,^\([^ ]*\) \($mdir/\([^ ]*\)\) .*$,\1,p" 
/proc/mounts)"
+      else
+         # Add /dev, /media, and just shortcut entries. Caveat: only /dev 
works after pmount /dev/sdc1 "my usb"
+         devices="$(sed -n -e "s,^\([^ ]*\) \($mdir/\([^ ]*\)\) 
.*$,\1\n\2\n\3,p" /proc/mounts)"
+      fi
       COMPREPLY=( $( compgen -W "$devices" -- $cur ) )
    fi

Reply via email to