zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: PATCH: Fix _file_descriptors
Date: Sun, 26 Feb 2012 19:37:13 +0100	[thread overview]
Message-ID: <1330281433-23949-1-git-send-email-mikachu@gmail.com> (raw)

I noticed file descriptor completion didn't work, with the verbose style set
because when this is run,
  fds=( /dev/fd/<0-9>(N:t) )
the /dev/fd dir is open while the glob is performed, which results in
a spurious entry in the result, which then cannot be dereferenced. The
result is that the list array is not aligned to the fds array (and also
an error message is output), and an fd that doesn't exist is completed.

If the verbose style is not set, the spurious fd is not filtered.

---
 Completion/Zsh/Type/_file_descriptors |   34 ++++++++++++++++++--------------
 1 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/Completion/Zsh/Type/_file_descriptors b/Completion/Zsh/Type/_file_descriptors
index 3e251b7..38e2bf5 100644
--- a/Completion/Zsh/Type/_file_descriptors
+++ b/Completion/Zsh/Type/_file_descriptors
@@ -1,31 +1,35 @@
 #autoload
 
-local i fds expl list link sep
+local i expl link sep cmd
+local -a fds list
 
-fds=( /dev/fd/<0-9>(N:t) )
-
-if zstyle -T ":completion:${curcontext}:" verbose && [[ -h /proc/$$/fd/$fds[1] ]]; then
+if zstyle -T ":completion:${curcontext}:" verbose && [ -h /proc/$$/fd/<->([1]) ]; then
   zstyle -s ":completion:${curcontext}:" list-separator sep || sep=--
 
   if zmodload -F zsh/stat b:zstat; then
-    for i in "${fds[@]}"; do
-      zstat +link -A link /proc/$$/fd/$i
-      list+=( "$i $sep ${link[1]}" )
-    done
+    cmd='zstat +link -A link $REPLY; link=$link[1]'
   elif (( $+commands[readlink] )); then
-    for i in "${fds[@]}"; do
-      list+=( "$i $sep $(readlink /proc/$$/fd/$i)" )
-    done
+    cmd='link=$(readlink $REPLY)'
   else
-    for i in "${fds[@]}"; do
-      list+=( "$i $sep $(ls -l /proc/$$/fd/$i|sed 's/.*-> //' )" )
-    done
+    cmd='link=$(ls -l $REPLY|sed "s/.*-> //" )'
   fi
 
-  if (( $list[(I)* $sep ?*] )); then
+  # Filter out the fd for the dir opened during the glob (/proc/$$/fd)
+  : /proc/$$/fd/<->(e,$cmd'
+                       if [[ $link == /proc/$$/fd ]]; then
+                         false
+                       else
+                         fds+=( $REPLY:t )
+                         list+=( "$REPLY:t $sep $link" )
+                       fi
+                      ',)
+
+  if (( $list[(I)<-> $sep ?*] )); then
     _wanted file-descriptors expl 'file descriptor' compadd "$@" -d list -a - fds
     return
   fi
+else
+  fds=( /dev/fd/<->(N:t) )
 fi
 
 _wanted file-descriptors expl 'file descriptor' compadd -a "$@" - fds
-- 
1.7.5.4


             reply	other threads:[~2012-02-26 18:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-26 18:37 Mikael Magnusson [this message]
2012-02-27 16:08 ` Oliver Kiddle
2012-02-27 16:36   ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1330281433-23949-1-git-send-email-mikachu@gmail.com \
    --to=mikachu@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).