about summary refs log tree commit diff
path: root/Completion/Unix/Command/_sudo
blob: c334c6765be48c65fa89a5b1e2af6009028123f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#compdef sudo sudoedit

local curcontext="$curcontext" environ e cmd cpp ret=1
local -a context state line args _comp_priv_prefix
local -A opt_args

zstyle -a ":completion:${curcontext}:" environ environ

for e in "${environ[@]}"
do local -x "$e"
done

args=(
  '(-A --askpass)'{-A,--askpass}'[use a helper program for password prompting]'
  '(-B --bell)'{-B,--bell}'[ring bell when prompting]'
  '(-C --close-from)'{-C+,--close-from=}'[close file descriptors]:lowest fd to close'
  '(-D --chdir)'{-D+,--chdir=}'[change the working directory before running command]:directory:_directories'
  '(-g --group)'{-g+,--group=}'[run command as the specified group name or ID]:group:_groups'
  '(-)'{-h,--help}'[display help message and exit]'
  '(-h --host)'{-h+,--host=}'[run command on host]:host:_hosts'
  '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-K,--remove-timestamp}'[remove timestamp file completely]'
  '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-k,--reset-timestamp}'[invalidate timestamp file]'
  \*{-l,--list}"[list user's privileges or check a specific command]"
  '(-n --non-interactive)'{-n,--non-interactive}'[non-interactive mode, no prompts are used]'
  '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-N,--no-update}"[don't update user's cached credentials]"
  '(-p --prompt)'{-p+,--prompt=}'[use the specified password prompt]:prompt'
  '(-R --chroot)'{-R+,--chroot=}'[change the root directory before running command]:directory:_directories'
  '(-r --role)'{-r+,--role=}'[create SELinux security context with specified role]: :_selinux_roles'
  '(-S --stdin)'{-S,--stdin}'[read password from standard input]'
  '(-t --type)'{-t+,--type=}'[create SELinux security context with specified type]: :_selinux_types'
  '(-T --command-timeout)'{-T+,--command-timeout=}'[terminate command after specified time limit]:timeout'
  '(-U --other-user -v --validate)'{-U+,--other-user=}'[in list mode, display privileges for user]:user:_users'
  '(-u --user)'{-u+,--user=}'[run command (or edit file) as specified user]:user:_users'
  '(-)'{-V,--version}'[display version information and exit]'
  '(-v --validate -U --other-user *)'{-v,--validate}"[update user's timestamp without running a command]"
)

# Does -e appears before the first word that doesn't begin with a hyphen?
# The way (i) works in subscripts, the test will always be true if all the
# words begin with a hyphen.
# 
# TODO: use _arguments' $opt_args to detect the cases '-u jrandom -e' and '-Ae'
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]' \
    '(-i --login)'{-i,--login}'[run login shell as the target user; a command may also be specified]' \
    '(-b --background -i --login -s --shell -e --edit)'{-b,--background}'[run command in the background]' \
    '(--preserve-env -i --login -s --shell -e --edit)-E[preserve user environment when running command]' \
    '(-E -i --login -s --shell -e --edit)--preserve-env=-[preserve user environment when running command]::environment variable:_sequence _parameters -g "*export*"' \
    '(-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]" \
    '*:: :->normal'
  )
fi

_arguments -s -S $args && ret=0

if [[ $state = normal ]]; then
  _comp_priv_prefix=(
    $cmd -n
    ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]}
  )
  if (( $+opt_args[-l] || $+opt_args[--list] )); then
    _normal -p $service
    return
  fi
  while [[ $words[1] = *=* ]]; do
    if (( CURRENT == 1 )); then
      compstate[parameter]="${PREFIX%%\=*}"
      compset -P 1 '*='
      _value && ret=0
      return ret
    fi
    shift words
    (( CURRENT-- ))
  done
  if (( CURRENT == 1 )); then
    curcontext="${curcontext%:*:*}:-command-:"
    _alternative \
      'commands:: _command_names -e' \
      'options:option:(-s --shell -l --login)' \
      'parameters: :_parameters -g "*export*~*readonly*" -qS=' && ret=0
  else
    _normal
  fi
fi

return ret