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

Re: [PATCH] _virsh (Was: Re: zsh virsh completion)



On 4 Sep, Daniel Shahaf wrote:
> > > > It'd perhaps be useful if
> > > > the -p option to _call_program also caused it to add something to
> > > > $curcontext when looking up the command style but I'm not sure where
> > > > that could be done as we already have the tag and argument fields
> > > > filled. Any ideas?

> > Perhaps f/sudo? / can't appear in the command name.
> +1, sounds good.

This particular feature was missed before but is added with the
patch below. It is only added for the command and gain-privileges
styles, i.e. those checked by _call_program.

I also noticed that $words[1] was being updated by _arguments before the
code that used it so the patch fixes _sudo. I still need to test this
properly together with a real use such as virsh but I don't have virsh
or sudo installed on my normal system.

> How do we handle the case that >1 precommand wants to set _comp_priv_prefix?

At the moment, the last one sets it and overwrites it. Trying to
chain them is really going too far. Perhaps _sudo should just not
bother at all if _comp_priv_prefix is already set.

Oliver

diff --git a/Completion/Base/Utility/_call_program b/Completion/Base/Utility/_call_program
index 95c761e..9a44f2d 100644
--- a/Completion/Base/Utility/_call_program
+++ b/Completion/Base/Utility/_call_program
@@ -1,12 +1,15 @@
 #autoload +X
 
-local tmp err_fd=-1
+local curcontext="${curcontext}" tmp err_fd=-1
 local -a prefix
 
 if [[ "$1" = -p ]]; then
   shift
-  zstyle -t ":completion:${curcontext}:${1}" gain-privileges &&
-      prefix=( $_comp_priv_prefix )
+  if (( $#_comp_priv_prefix )); then
+    curcontext="${curcontext%:*}/${${(@M)_comp_priv_prefix:#^*[^\\]=*}[1]}:"
+    zstyle -t ":completion:${curcontext}:${1}" gain-privileges &&
+	prefix=( $_comp_priv_prefix )
+  fi
 fi
 
 if (( ${debug_fd:--1} > 2 )) || [[ ! -t 2 ]]
diff --git a/Completion/Solaris/Command/_pfexec b/Completion/Solaris/Command/_pfexec
index 3f1f3e7..2afaf31 100644
--- a/Completion/Solaris/Command/_pfexec
+++ b/Completion/Solaris/Command/_pfexec
@@ -25,7 +25,7 @@ _pfexec() {
  	_arguments \
 		'-P[privileges to acquire]:privspec:_privset' \
  		'(-):command name: _command_names -e' \
-		'*::arguments:{ _comp_priv_prefix=( $words[1] ${(kv)opt_args[-P]} ) ; _normal }'
+		'*::arguments:{ _comp_priv_prefix=( pfexec ${(kv)opt_args[-P]} ) ; _normal }'
 }
 
 _pfexec "$@"
diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo
index 21b1ef4..0a212b7 100644
--- a/Completion/Unix/Command/_sudo
+++ b/Completion/Unix/Command/_sudo
@@ -2,7 +2,7 @@
 
 setopt localoptions extended_glob
 
-local environ e
+local environ e cmd
 local -a args
 
 zstyle -a ":completion:${curcontext}:" environ environ
@@ -39,6 +39,7 @@ args=(
 if [[ $service = sudoedit ]] || (( $words[(i)-e] < $words[(i)^(*sudo|-[^-]*)] ))  ; then
   args=( -A "-*" $args '!(-V --version -h --help)-e' '*:file:_files' )
 else
+  cmd="$words[1]"
   args+=(
     '(-e --edit 1 *)'{-e,--edit}'[edit files instead of running a command]' \
     '(-s --shell)'{-s,--shell}'[run shell as the target user; a command may also be specified]' \
@@ -48,7 +49,7 @@ else
     '(-H --set-home -i --login -s --shell -e --edit)'{-H,--set-home}"[set HOME variable to target user's home dir]" \
     '(-P --preserve-groups -i -login -s --shell -e --edit)'{-P,--preserve-groups}"[preserve group vector instead of setting to target's]" \
     '(-)1:command: _command_names -e'
-    '*::arguments:{ _comp_priv_prefix=( $words[1] -n ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} ) ; _normal }'
+    '*::arguments:{ _comp_priv_prefix=( $cmd -n ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} ) ; _normal }'
   )
 fi
 
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index cab665b..260ace4 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -4191,7 +4191,9 @@ output is influenced by the permissions it is run with. If the
 tt(gain-privileges) style is set to true, tt(_call_program) will make
 use of commands such as tt(sudo), if present on the command-line, to
 match the permissions to whatever the final command is likely to run
-under.
+under. When looking up the tt(gain-privileges) and tt(command) styles,
+the command component of the zstyle context will end with a slash
+(`tt(/)') followed by the command that would be used to gain privileges.
 )
 findex(_combination)
 item(tt(_combination) [ tt(-s) var(pattern) ] var(tag) var(style) var(spec) ... var(field) var(opts) ...)(



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