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

Re: PATCH: update completions for some common Unix commands



Daniel Shahaf wrote:
> Maybe use 'jls jid name' so as to not rely on 'jid' and 'name' being
> consecutive in the output of 'jls -n'?

Yes.

> Should jail names be offered as completions, rather than just as
> descriptions?  I think most jail-related commands commands accept either
> jname or jid to identify a jail.

ps and pgrep both seem to require IDs but allow 0 for processes that are
not part of a jail. Do jail-related commands accepting names have a name
for the host system? I was thinking it'd be good to complete also
non-running jails but that needs to parse /etc/jail.conf and other
potential sources such as ezjail so I'll leave that out for now.

Also, Eric Cook had some good suggestions (off-list) for completing
other properties such as the jail path. So the following allows _jails
to take a -0 option to complete jid 0 and -o for selecting the jail
parameter to complete such as name or path. There's probably commands
somewhere that can take either jids or names (jls being one example) but
that's not yet handled.

Oliver

diff --git a/Completion/BSD/Type/_jails b/Completion/BSD/Type/_jails
index 36bffec..5fb8bde 100644
--- a/Completion/BSD/Type/_jails
+++ b/Completion/BSD/Type/_jails
@@ -1,5 +1,33 @@
 #autoload
 
-jails=( ${${${${(f)"$(_call_program jails jls -n)"}##*jid=}/ name=/:}%% *} )
+# Options:
+#
+# -0        include jid 0 as a match for the host system
+# -o param  jail parameter to complete instead of jid -
+#              e.g. name, path, ip4.addr, host.hostname
 
-_describe -t jails jail jails "$@"
+local addhost host param desc=1
+local -a args expl
+zparseopts -D -K -E 0=addhost o:=param
+param=${param[2]:-jid}
+
+jails=( ${${(f)"$(_call_program jails jls $param name)"}/ /:} )
+case $param in
+  jid) host=0 ;;
+  name)
+    host=
+    desc=0
+  ;;
+  path)
+    host=/
+    args=( -M 'r:|/=* r:|=*' )
+  ;;
+  ip4.addr) args=( -M 'r:|.=* r:|=*' ) ;;
+esac
+[[ -n $addhost && -n $host ]] && jails+=( "$host:$HOST" )
+
+if (( desc )); then
+  _describe -t jails jail jails "$@" "$args[@]"
+else
+  _wanted jails expl jail compadd "$@" "$args[@]" - ${jails%:*}
+fi
diff --git a/Completion/Unix/Command/_pgrep b/Completion/Unix/Command/_pgrep
index 3af55a3..b0a0d2f 100644
--- a/Completion/Unix/Command/_pgrep
+++ b/Completion/Unix/Command/_pgrep
@@ -8,7 +8,7 @@ arguments=('-P[parent process id]:parent process id:->ppid'
      '-F[match only in process in pidfile]:files:_files'
      '-g[match only in process group ids]:group:->pgid'
      '-G[match only real group id]:group:_groups'
-     '-j[match only in processes inside jails]:jail id:->jid'
+     '-j[match only in processes inside jails]:jail id:_sequence _jails -0'
      '-J[match only in project ids]:project id:->projid'
      '-M[extract the name list from the specified core]:files:_files'
      '-N[extract the name list from the specified system]:files:_files'
@@ -93,16 +93,6 @@ case $state in
     _wanted sid expl 'session id' compadd -S ',' -q -F used $sid
     ;;
 
-  (jid)
-    compset -P '*,'
-
-    local -a used jid
-    used=(${(s:,:)IPREFIX})
-    jid=(${(uon)$(ps -ax -o jid=)})
-
-    _wanted jid expl 'jail id' compadd -S ',' -q -F used $jid
-    ;;
-
   (ppid)
     compset -P '*,'
 
diff --git a/Completion/Unix/Command/_ps b/Completion/Unix/Command/_ps
index 925f483..4106759 100644
--- a/Completion/Unix/Command/_ps
+++ b/Completion/Unix/Command/_ps
@@ -160,7 +160,7 @@ if (( CURRENT > 1 )) && [[ $OSTYPE != solaris* || ( $OSTYPE = linux-gnu && $word
       _wanted -C option-k-1 properties expl 'property' _sequence -s , _ps_props -s - && return
     ;;
     *G) _sequence -s , _groups && return ;;
-    *J) _sequence -s , _jails && return ;;
+    *J) _sequence -s , _jails -0 && return ;;
     *[MNW]) _files && return ;;
     *t)
       _wanted -C option-t-1 ttys expl tty _sequence -s , \



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