Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: improve lsof completion



This handles completion of hostnames etc after the -i option to lsof and
also the TCP states after -s. This just takes the basic states listed in
the man page. A correct list might apparently be system specific.

Options are all still up-to-date as of lsof 4.91.

Oliver

diff --git a/Completion/Unix/Command/_lsof b/Completion/Unix/Command/_lsof
index bbb6eab59..86115a4ef 100644
--- a/Completion/Unix/Command/_lsof
+++ b/Completion/Unix/Command/_lsof
@@ -1,6 +1,6 @@
 #compdef lsof
 
-local curcontext="$curcontext" state line expl fields args
+local curcontext="$curcontext" state line expl fields args alts suf hsuf pref ret=1
 
 case $OSTYPE in
   linux*) args=( '-X[skip reporting of info on network connections]' ) ;;
@@ -28,8 +28,8 @@ _arguments -C -s -S $args \
   '+f[enable listing of kernel file structure info]::info type:(c f g G n)' \
   '-F[select output fields]:fields:->fields' \
   '-g[select by process group id]::process group id' \
-  '*-i[select internet files]:address' \
-  '-K[select listing of tasks of processes]' \
+  '(*)*-i[select internet files]::address:->addresses' \
+  '-K+[select listing of tasks of processes]::value:((i\:ignore\ tasks))' \
   '-k[specify kernel name list file]:kernel file:_files' \
   '-l[inhibit conversion of UIDs to user names]' \
   '-L[list no link counts]' \
@@ -46,7 +46,7 @@ _arguments -C -s -S $args \
   '-r[repeat listing endlessly]::delay (seconds)' \
   '+r[repeat listing until no files listed]::delay (seconds)' \
   '-R[list parent PID]' \
-  '(-o)-s[list file size]' \
+  '(-o)-s+[list file size or exclude TCP/UDP files by state]::state:->states' \
   '-S[specify timeout for kernel functions that might deadlock]:timeout (seconds)' \
   '-T[select reporting of TCP/TPI info]::info type:((q\:queue\ length\ reporting s\:state\ reporting w\:window\ size\ reporting))' \
   '(+w -w)-t[terse output]' \
@@ -57,18 +57,52 @@ _arguments -C -s -S $args \
   '(-t)+w[suppress warnings]' \
   '(-t)-w[enable warnings]' \
   '-x[cross filesystems/traverse symlinks with +d/+D]::type:((f\:filesystems s\:symlinks))' \
-  '*:file:_files' && return
+  '*:file:_files' && ret=0
 
 case $state in
+  addresses)
+    if compset -P 1 '*:'; then
+      _ports && ret=0
+    else
+      if compset -P 1 '*@'; then
+        pref=()
+      else
+        pref=( -P @ )
+        suf=( -qS @ )
+        [[ $PREFIX = (#i)(4|6|tcp|udp)* ]] || alts=( 'ip-versions:IP version:(4 6)' )
+        compset -P '[46]'
+        alts+=( 'protocols:protocol:compadd $suf -M 'm:{a-z}={A-Z}' TCP UDP' )
+      fi
+      if compset -S '@*'; then
+        suf=()
+      else
+        hsuf=( -qS : )
+        compset -S ':*' && hsuf=()
+        alts+=( "hosts:host:_hosts $pref $hsuf" )
+      fi
+
+      _alternative $alts && ret=0
+    fi
+  ;;
   fields)
     fields=( ${${${${(f)"$($words[1] -F \? 2>&1)"}[2,-1]#??}//  ##/:}:#(#s)[${PREFIX:-:}]:*} )
     compset -P '*'
-    _describe -t fields "field" fields -S '' && return
+    _describe -t fields "field" fields -S '' && ret=0
   ;;
   filedes)
     _sequence -s , _wanted -x file-descriptors expl "file descriptor" compadd - \
-        cwd err jld ltz mem mmap pd rtd tr txt v86 && return
+        cwd err jld ltz mem mmap pd rtd tr txt v86 && ret=0
+  ;;
+  states)
+    if compset -P 1 '*:'; then
+      _sequence _wanted states expl state compadd - -M 'm:{a-z}={A-Z}' \
+          CLOSED IDLE BOUND LISTEN ESTABLISHED SYN_SENT SYN_RCDV ESTABLISHED \
+          CLOSE_WAIT FIN_WAIT1 CLOSING LAST_ACK FIN_WAIT_2 TIME_WAIT && ret=0
+    else
+      compset -S ':*' || suf=( -qS : )
+      _wanted protocols expl protocol compadd $suf -M 'm:{a-z}={A-Z}' TCP UDP && ret=0
+    fi
   ;;
 esac
 
-return 1
+return ret



Messages sorted by: Reverse Date, Date, Thread, Author