about summary refs log tree commit diff
path: root/Completion/Zsh
diff options
context:
space:
mode:
authordana <dana@dana.is>2020-03-11 16:26:41 -0500
committerdana <dana@dana.is>2020-03-11 16:26:41 -0500
commit359e0dae2bded75eebf7fe3b3cf919e1dd44c333 (patch)
treee177397b30d61ffbb61658c894e7d5a4c821ee72 /Completion/Zsh
parent0d7f888945bd487d6458807684883b22dc3b31b8 (diff)
downloadzsh-359e0dae2bded75eebf7fe3b3cf919e1dd44c333.tar.gz
zsh-359e0dae2bded75eebf7fe3b3cf919e1dd44c333.tar.xz
zsh-359e0dae2bded75eebf7fe3b3cf919e1dd44c333.zip
45424: completion: Add **/sbin to PATH when completing commands like sudo
* Update _command_names to add **/sbin to PATH when not otherwise overridden
  and _comp_priv_prefix is set. This ensures that, on systems where
  unprivileged users don't have **/sbin in their PATHs, we can still complete
  those commands when they try to run them with e.g. sudo

* Update functions for privilege-gaining commands to use the aforementioned
  mechanism

* Fix some other minor issues in _pfexec and _doas

Changes from the posted diff:

* Fix a minor redundancy in _doas

* Add _c_p_p=( '' ) hack to _su
Diffstat (limited to 'Completion/Zsh')
-rw-r--r--Completion/Zsh/Type/_command_names19
1 files changed, 16 insertions, 3 deletions
diff --git a/Completion/Zsh/Type/_command_names b/Completion/Zsh/Type/_command_names
index cd630b7a4..b1c35f013 100644
--- a/Completion/Zsh/Type/_command_names
+++ b/Completion/Zsh/Type/_command_names
@@ -41,11 +41,24 @@ fi
 args=( "$@" )
 
 local -a cmdpath
-if zstyle -a ":completion:${curcontext}" command-path cmdpath &&
-   [[ $#cmdpath -gt 0 ]]
-then
+
+zstyle -a ":completion:${curcontext}" command-path cmdpath
+
+# Using the current PATH doesn't necessarily make sense when completing commands
+# to tools like sudo, which might set a different one. A common issue is that
+# /**/sbin appear in the PATH used by the tool, but not in the one used by the
+# unprivileged user who calls it. To do the right thing in the most common
+# cases, we'll simply ensure that the sbin variants always appear here when not
+# otherwise overridden (bash-completion's _sudo does something similar)
+if (( ! $#cmdpath && $#_comp_priv_prefix )); then
+  cmdpath=( $path ${path/%\/bin//sbin} )
+  cmdpath=( ${(u)^cmdpath}(/-N) )
+fi
+
+if (( $#cmdpath )); then
   local -a +h path
   local -A +h commands
   path=( $cmdpath )
 fi
+
 _alternative -O args "$defs[@]"