chiark / gitweb /
a54e9d727ade070a2207c5ddf2f9674adef5435b
[elogind.git] / shell-completion / systemd-zsh-completion.zsh
1 #compdef systemctl loginctl journalctl hostnamectl localectl timedatectl systemd-coredumpctl udevadm systemd-analyze systemd-cat systemd-ask-password systemd-cgls systemd-cgtop systemd-delta systemd-detect-virt systemd-inhibit systemd-machine-id-setup systemd-notify systemd-nspawn systemd-tmpfiles systemd-tty-ask-password-agent
2
3 _ctls()
4 {
5     local curcontext="$curcontext" state lstate line
6     case "$service" in
7         systemctl)
8             # -s for aggregated options like -aP
9             _arguments -s \
10                 {-h,--help}'[Show help]' \
11                 '--version[Show package version]' \
12                 {-t,--type=}'[List only units of a particular type]:unit type:(automount device mount path service snapshot socket swap target timer)' \
13                 \*{-p,--property=}'[Show only properties by specific name]:unit property' \
14                 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
15                 '--failed[Show only failed units]' \
16                 "--full[Don't ellipsize unit names on output]" \
17                 '--fail[When queueing a new job, fail if conflicting jobs are pending]' \
18                 '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \
19                 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
20                 {-s,--signal=}'[Which signal to send]:signal:_signals' \
21                 {-H,--host=}'[Show information for remote host]:userathost:_hosts_or_user_at_host' \
22                 {-P,--privileged}'[Acquire privileges before execution]' \
23                 {-q,--quiet}'[Suppress output]' \
24                 '--no-block[Do not wait until operation finished]' \
25                 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
26                 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
27                 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
28                 '--no-pager[Do not pipe output into a pager]' \
29                 '--no-ask-password[Do not ask for system passwords]' \
30                 '--system[Connect to system manager]' \
31                 '--user[Connect to user service manager]' \
32                 '--global[Enable/disable unit files globally]' \
33                 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
34                 '--root=[Enable unit files in the specified root directory]:directory:_directories' \
35                 '--runtime[Enable unit files only temporarily until next reboot]' \
36                 {-n,--lines=}'[Journal entries to show]:number of entries' \
37                 {-o,--output=}'[Change journal output mode]:modes:_outputmodes' \
38                 '*::systemctl command:_systemctl_command'
39         ;;
40         loginctl)
41             _arguments -s \
42                 {-h,--help}'[Show help]' \
43                 '--version[Show package version]' \
44                 \*{-p,--property=}'[Show only properties by this name]:unit property' \
45                 {-a,--all}'[Show all properties, including empty ones]' \
46                 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
47                 {-s,--signal=}'[Which signal to send]:signal:_signals' \
48                 '--no-ask-password[Do not ask for system passwords]' \
49                 {-H,--host=}'[Show information for remote host]:userathost:_hosts_or_user_at_host' \
50                 {-P,--privileged}'[Acquire privileges before execution]' \
51                 '--no-pager[Do not pipe output into a pager]' \
52                 '*::loginctl command:_loginctl_command'
53         ;;
54
55         hostnamectl)
56             _arguments -s \
57                 {-h,--help}'[Show this help]' \
58                 '--version[Show package version]' \
59                 '--transient[Only set transient hostname]' \
60                 '--static[Only set static hostname]' \
61                 '--pretty[Only set pretty hostname]' \
62                 '--no-ask-password[Do not prompt for password]' \
63                 {-H,--host=}'[Operate on remote host]:userathost:_hosts_or_user_at_host' \
64                 '*::hostnamectl commands:_hostnamectl_command'
65         ;;
66         journalctl)
67             _arguments -s \
68                 '--since=[Start showing entries newer or of the specified date]:YYYY-MM-DD HH\:MM\:SS' \
69                 '--until=[Stop showing entries older or of the specified date]:YYYY-MM-DD HH\:MM\:SS' \
70                 {-c,--cursor=}'[Start showing entries from specified cursor]:cursors:_journal_fields __CURSORS' \
71                 {-b,--this-boot}'[Show data only from current boot]' \
72                 {-u,--unit=}'[Show data only from the specified unit]:units:_journal_fields _SYSTEMD_UNIT' \
73                 '--user-unit[Show data only from the specified user session unit]:units:_journal_fields _SYSTEMD_USER_UNIT' \
74                 {-p,--priority=}'[Show only messages within the specified priority range]:priority:_journal_fields PRIORITY' \
75                 {-f,--follow}'[Follow journal]' \
76                 {-n,--lines=}'[Number of journal entries to show]:integer' \
77                 '--no-tail[Show all lines, even in follow mode]' \
78                 {-o,--output=}'[Change journal output mode]:output modes:_outputmodes' \
79                 '--full[Show long fields in full]' \
80                 {-a,--all}'[Show all fields, including long and unprintable]' \
81                 {-q,--quiet}"[Don't show privilege warning]" \
82                 '--no-pager[Do not pipe output into a pager]' \
83                 {-m,--merge}'[Show entries from all available journals]' \
84                 {-D,--directory=}'[Show journal files from directory]:directories:_directories' \
85                 '--interval=[Time interval for changing the FSS sealing key]:time interval' \
86                 '--verify-key=[Specify FSS verification key]:FSS key' \
87                 {-h,--help}'[Show this help]' \
88                 '--version[Show package version]' \
89                 '--new-id128[Generate a new 128 Bit ID]' \
90                 '--header[Show journal header information]' \
91                 '--disk-usage[Show total disk usage]' \
92                 {-F,--field=}'[List all values a certain field takes]:Fields:_list_fields' \
93                 '--setup-keys[Generate new FSS key pair]' \
94                 '--verify[Verify journal file consistency]' \
95                 '--list-catalog[List messages in catalog]' \
96                 '--update-catalog[Update binary catalog database]' \
97                 '*::default: _journal_none'
98         ;;
99         localectl)
100             _arguments \
101                 {-h,--help}'[Show this help]' \
102                 '--version[Show package version]' \
103                 "--no-convert[Don't convert keyboard mappings]" \
104                 '--no-pager[Do not pipe output into a pager]' \
105                 '--no-ask-password[Do not prompt for password]' \
106                 {-H,--host=}'[Operate on remote host]:userathost:_hosts_or_user_at_host' \
107                 '*::localectl commands:_localectl_command'
108         ;;
109         systemd-coredumpctl)
110             _arguments \
111                 {-o,--output=}'[Write output to FILE]:output file:_files' \
112                 '--no-pager[Do not pipe output into a pager]' \
113                 {-h,--help}'[Show this help]' \
114                 '--version[Show package version]' \
115                 '*::systemd-coredumpctl commands:_systemd-coredumpctl_command'
116
117         ;;
118         timedatectl)
119             _arguments -s \
120                 {-h,--help}'[Show this help]' \
121                 '--version[Show package version]' \
122                 '--adjust-system-clock[Adjust system clock when changing local RTC mode]' \
123                 '--no-pager[Do not pipe output into a pager]' \
124                 '--no-ask-password[Do not prompt for password]' \
125                 {-H,--host=}'[Operate on remote host]:userathost:_hosts_or_user_at_host' \
126                 '*::timedatectl commands:_timedatectl_command'
127         ;;
128         udevadm)
129             _arguments \
130                 '--debug[Print debug messages to stderr]' \
131                 '--version[Print version number]' \
132                 '--help[Print help text]' \
133                 '*::udevadm commands:_udevadm_command'
134         ;;
135         systemd-analyze)
136             _arguments \
137                 {-h,--help}'[Show help text.]' \
138                 '--user[Shows performance data of user sessions instead of the system manager.]' \
139                 '--order[When generating graph for dot, show only order]' \
140                 '--require[When generating graph for dot, show only requirement]' \
141                 '*::systemd-analyze commands:_systemd_analyze_command'
142         ;;
143         systemd-ask-password)
144             _arguments \
145                 {-h,--help}'[Show this help]' \
146                 '--icon=[Icon name]' \
147                 '--timeout=[Timeout in sec]' \
148                 '--no-tty[Ask question via agent even on TTY]' \
149                 '--accept-cached[Accept cached passwords]' \
150                 '--multiple[List multiple passwords if available]'
151         ;;
152         systemd-cat)
153             _arguments \
154                 {-h,--help}'[Show this help]' \
155                 '--version[Show package version.]' \
156                 {-t,--identifier=}'[Set syslog identifier.]' \
157                 {-p,--priority=}'[Set priority value.]:value:({0..7})' \
158                 '--level-prefix=[Control whether level prefix shall be parsed.]:boolean:(1 0)' \
159                 ':Message'
160         ;;
161         systemd-cgls)
162             _arguments \
163                 {-h,--help}'[Show this help]' \
164                 '--version[Show package version]' \
165                 '--no-pager[Do not pipe output into a pager]' \
166                 {-a,--all}'[Show all groups, including empty]' \
167                 '-k[Include kernel threads in output]' \
168                 ':cgroups:(cpuset cpu cpuacct memory devices freezer net_cls blkio)'
169         ;;
170         systemd-cgtop)
171             _arguments \
172                 {-h,--help}'[Show this help]' \
173                 '--version[Print version and exit]' \
174                 '(-c -m -i -t)-p[Order by path]' \
175                 '(-c -p -m -i)-t[Order by number of tasks]' \
176                 '(-m -p -i -t)-c[Order by CPU load]' \
177                 '(-c -p -i -t)-m[Order by memory load]' \
178                 '(-c -m -p -t)-i[Order by IO load]' \
179                 {-d,--delay=}'[Specify delay]' \
180                 {-n,--iterations=}'[Run for N iterations before exiting]' \
181                 {-b,--batch}'[Run in batch mode, accepting no input]' \
182                 '--depth=[Maximum traversal depth]'
183         ;;
184         systemd-delta)
185             _arguments \
186                 {-h,--help}'[Show this help]' \
187                 '--version[Show package version]' \
188                 '--no-pager[Do not pipe output into a pager]' \
189                 '--diff=[Show a diff when overridden files differ]:boolean:(1 0)' \
190                 {-t,--type=}'[Only display a selected set of override types]:types:(masked equivalent redirected overridden unchanged)' \
191                 ':SUFFIX:(tmpfiles.d sysctl.d systemd/system)'
192         ;;
193         systemd-detect-virt)
194             _arguments \
195                 {-h,--help}'[Show this help]' \
196                 '--version[Show package version]' \
197                 {-c,--container}'[Only detect whether we are run in a container]' \
198                 {-v,--vm}'[Only detect whether we are run in a VM]' \
199                 {-q,--quiet}"[Don't output anything, just set return value]"
200         ;;
201         systemd-inhibit)
202             _arguments \
203                 {-h,--help}'[Show this help]' \
204                 '--version[Show package version]' \
205                 '--what=[Operations to inhibit]:options:(shutdown sleep idle handle-power-key handle-suspend-key handle-hibernate-key handle-lid-switch)' \
206                 '--who=[A descriptive string who is inhibiting]' \
207                 '--why=[A descriptive string why is being inhibited]' \
208                 '--mode=[One of block or delay]' \
209                 '--list[List active inhibitors]' \
210                 '*:commands:_systemd_inhibit_command'
211         ;;
212         systemd-machine-id-setup)
213             _arguments \
214                 {-h,--help}'[Show this help]' \
215                 '--version[Show package version]'
216         ;;
217         systemd-notify)
218             _arguments \
219                 {-h,--help}'[Show this help]' \
220                 '--version[Show package version]' \
221                 '--ready[Inform the init system about service start-up completion.]' \
222                 '--pid=[Inform the init system about the main PID of the daemon]' \
223                 '--status=[Send a free-form status string for the daemon to the init systemd]' \
224                 '--booted[Returns 0 if the system was booted up with systemd]' \
225                 '--readahead=[Controls disk read-ahead operations]:arguments:(cancel done noreply)'
226         ;;
227         systemd-nspawn)
228             _arguments \
229                 {-h,--help}'[Show this help]' \
230                 {--directory=,-D}'[Directory to use as file system root for the namespace container. If omitted the current directory will be used.]:directories:_directories' \
231                 {--boot,-b}'[Automatically search for an init binary and invoke it instead of a shell or a user supplied program.]' \
232                 {--user=,-u}'[Run the command under specified user, create home directory and cd into it.]' \
233                 '--uuid=[Set the specified uuid for the container.]' \
234                 {--controllers=,-C}'[Makes the container appear in other hierarchies than the name=systemd:/ one. Takes a comma-separated list of controllers.]' \
235                 '--private-network[Turn off networking in the container. This makes all network interfaces unavailable in the container, with the exception of the loopback device.]' \
236                 '--read-only[Mount the root file system read only for the container.]' \
237                 '--capability=[List one or more additional capabilities to grant the container.]:capabilities:_systemd-nspawn' \
238                 "--link-journal=[Control whether the container's journal shall be made visible to the host system.]:options:(no, host, guest, auto)" \
239                 '-j[Equivalent to --link-journal=guest.]'
240         ;;
241         systemd-tmpfiles)
242             _arguments \
243                 '--create[Create, set ownership/permissions based on the config files.]' \
244                 '--clean[Clean up all files and directories with an age parameter configured.]' \
245                 '--remove[All files and directories marked with r, R in the configuration files are removed.]' \
246                 '--prefix=[Only apply rules that apply to paths with the specified prefix.]' \
247                 '--help[Prints a short help text and exits.]' \
248                 '*::files:_files'
249         ;;
250         systemd-tty-ask-password-agent)
251             _arguments \
252                 {-h,--help}'[Prints a short help text and exits.]' \
253                 '--version[Prints a short version string and exits.]' \
254                 '--list[Lists all currently pending system password requests.]' \
255                 '--query[Process all currently pending system password requests by querying the user on the calling TTY.]' \
256                 '--watch[Continuously process password requests.]' \
257                 '--wall[Forward password requests to wall(1).]' \
258                 '--plymouth[Ask question with plymouth(8).]' \
259                 '--console[Ask question on /dev/console.]'
260         ;;
261         *) _message 'eh?' ;;
262     esac
263 }
264
265 _systemd-nspawn(){
266     local -a _caps
267     _caps=( CAP_CHOWN CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH
268             CAP_FOWNER CAP_FSETID CAP_IPC_OWNER CAP_KILL CAP_LEASE CAP_LINUX_IMMUTABLE
269             CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW CAP_SETGID CAP_SETFCAP CAP_SETPCAP
270             CAP_SETUID CAP_SYS_ADMIN CAP_SYS_CHROOT CAP_SYS_NICE CAP_SYS_PTRACE CAP_SYS_TTY_CONFIG
271             CAP_SYS_RESOURCE CAP_SYS_BOOT )
272     _values -s , 'capabilities' "$_caps[@]"
273 }
274
275 _systemd_inhibit_command(){
276     if (( CURRENT == 1 )); then
277         compset -q
278         _normal
279     else
280         local n=${words[(b:2:i)[^-]*]}
281         if (( n <= CURRENT )); then
282             compset -n $n
283             _alternative \
284                 'files:file:_files' \
285                 'commands:command:_normal' && return 0
286         fi
287         _default
288     fi
289
290 }
291
292 _systemd_analyze_command(){
293     local -a _systemd_analyze_cmds
294     _systemd_analyze_cmds=(
295         'time:Print the time taken to start'
296         'blame:prints a list of all running units, ordered by the time they took to initialize'
297         'plot:prints an SVG graphic detailing which system services have been started at what time'
298         'dot:Dump dependency graph for dot(1)'
299     )
300
301     if (( CURRENT == 1 )); then
302         _describe "options" _systemd_analyze_cmds
303     else
304         _message "no more options"
305     fi
306 }
307
308 _hosts_or_user_at_host()
309 {
310   _alternative \
311     'users-hosts:: _user_at_host' \
312     'hosts:: _hosts'
313 }
314
315 _outputmodes() {
316     local -a _output_opts
317     _output_opts=(short short-monotonic verbose export json json-pretty json-see cat)
318     _describe -t output 'output mode' _output_opts || compadd "$@"
319 }
320
321
322 (( $+functions[_systemctl_command] )) || _systemctl_command()
323 {
324   local -a _systemctl_cmds
325   _systemctl_cmds=(
326     "list-units:List units"
327     "start:Start (activate) one or more units"
328     "stop:Stop (deactivate) one or more units"
329     "reload:Reload one or more units"
330     "restart:Start or restart one or more units"
331     "condrestart:Restart one or more units if active"
332     "try-restart:Restart one or more units if active"
333     "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
334     "force-reload:Reload one or more units if possible, otherwise restart if active"
335     "hibernate:Hibernate the system"
336     "hybrid-sleep:Hibernate and suspend the system"
337     "reload-or-try-restart:Reload one or more units if possible, otherwise restart if active"
338     "isolate:Start one unit and stop all others"
339     "kill:Send signal to processes of a unit"
340     "is-active:Check whether units are active"
341     "is-failed:Check whether units are failed"
342     "status:Show runtime status of one or more units"
343     "show:Show properties of one or more units/jobs or the manager"
344     "reset-failed:Reset failed state for all, one, or more units"
345     "load:Load one or more units"
346     "list-unit-files:List installed unit files"
347     "enable:Enable one or more unit files"
348     "disable:Disable one or more unit files"
349     "reenable:Reenable one or more unit files"
350     "preset:Enable/disable one or more unit files based on preset configuration"
351     "mask:Mask one or more units"
352     "unmask:Unmask one or more units"
353     "link:Link one or more units files into the search path"
354     "is-enabled:Check whether unit files are enabled"
355     "list-jobs:List jobs"
356     "cancel:Cancel all, one, or more jobs"
357     "dump:Dump server status"
358     "snapshot:Create a snapshot"
359     "delete:Remove one or more snapshots"
360     "show-environment:Dump environment"
361     "set-environment:Set one or more environment variables"
362     "unset-environment:Unset one or more environment variables"
363     "daemon-reload:Reload systemd manager configuration"
364     "daemon-reexec:Reexecute systemd manager"
365     "default:Enter system default mode"
366     "rescue:Enter system rescue mode"
367     "emergency:Enter system emergency mode"
368     "halt:Shut down and halt the system"
369     "suspend:Suspend the system"
370     "poweroff:Shut down and power-off the system"
371     "reboot:Shut down and reboot the system"
372     "kexec:Shut down and reboot the system with kexec"
373     "exit:Ask for user instance termination"
374   )
375
376   if (( CURRENT == 1 )); then
377     _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
378   else
379     local curcontext="$curcontext"
380
381     cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
382     # Deal with any aliases
383     case $cmd in
384       condrestart) cmd="try-restart";;
385       force-reload) cmd="reload-or-try-restart";;
386     esac
387
388     if (( $#cmd )); then
389       curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
390
391       local update_policy
392       zstyle -s ":completion:${curcontext}:" cache-policy update_policy
393       if [[ -z "$update_policy" ]]; then
394         zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
395       fi
396
397       _call_function ret _systemctl_$cmd || _message 'no more arguments'
398     else
399       _message "unknown systemctl command: $words[1]"
400     fi
401     return ret
402   fi
403 }
404
405 __systemctl()
406 {
407   local -a _modes
408   _modes=("--user" "--system")
409   systemctl ${words:*_modes} --full --no-legend --no-pager "$@"
410 }
411
412
413 # Fills the unit list
414 _systemctl_all_units()
415 {
416   if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
417     ! _retrieve_cache SYS_ALL_UNITS;
418   then
419     _sys_all_units=( $(__systemctl list-units --all | { while read a b; do echo " $a"; done; }) )
420     _store_cache SYS_ALL_UNITS _sys_all_units
421   fi
422 }
423
424 # Fills the unit list including all file units
425 _systemctl_really_all_units()
426 {
427   local -a all_unit_files;
428   local -a really_all_units;
429   if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) &&
430     ! _retrieve_cache SYS_REALLY_ALL_UNITS;
431   then
432     all_unit_files=( $(__systemctl list-unit-files | { while read a b; do echo " $a"; done; }) )
433     _systemctl_all_units
434     really_all_units=($_sys_all_units $all_unit_files)
435     _sys_really_all_units=(${(u)really_all_units})
436     _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units
437   fi
438 }
439
440 _filter_units_by_property() {
441   local property=$1 value=$2 ; shift ; shift
442   local -a units ; units=($*)
443   local prop unit
444   for ((i=1; $i <= ${#units[*]}; i++)); do
445     # FIXME: "Failed to issue method call: Unknown unit" errors are ignored for
446     # now (related to DBUS_ERROR_UNKNOWN_OBJECT). in the future, we need to
447     # revert to calling 'systemctl show' once for all units, which is way
448     # faster
449     unit=${units[i]}
450     prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"}
451     if [[ "${prop}" = "$property=$value" ]]; then
452       echo " ${unit}"
453     fi
454   done
455 }
456
457 _systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo " $a"; done; }) )}
458 _systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" || $c == "failed" ]] && echo " $a"; done; }) )}
459 _systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo " $a"; done; }) )}
460 _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo " $a"; done; }) )}
461 _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo " $a"; done; }) )}
462 _systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read a b; do [[ $b == "masked" ]] && echo " $a"; done; }) )}
463
464 # Completion functions for ALL_UNITS
465 for fun in is-active is-failed is-enabled status show mask preset ; do
466   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
467   {
468     _systemctl_really_all_units
469     compadd "$@" -a - _sys_really_all_units
470   }
471 done
472
473 # Completion functions for ENABLED_UNITS
474 for fun in disable reenable ; do
475   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
476   {
477     _systemctl_enabled_units
478     _systemctl_disabled_units
479     compadd "$@" -a - _sys_enabled_units _sys_disabled_units
480   }
481 done
482
483 # Completion functions for DISABLED_UNITS
484 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
485 {
486   _systemctl_disabled_units
487   compadd "$@" -a - _sys_disabled_units
488 }
489
490 # Completion functions for FAILED_UNITS
491 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
492 {
493   _systemctl_failed_units
494   compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
495 }
496
497 # Completion functions for STARTABLE_UNITS
498 (( $+functions[_systemctl_start] )) || _systemctl_start()
499 {
500   _systemctl_inactive_units
501   compadd "$@" -a - _sys_inactive_units
502 }
503
504 # Completion functions for STOPPABLE_UNITS
505 for fun in stop kill try-restart condrestart ; do
506   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
507   {
508     _systemctl_active_units
509     compadd "$@" - $( _filter_units_by_property CanStop yes \
510       ${_sys_active_units[*]} )
511   }
512 done
513
514 # Completion functions for ISOLATABLE_UNITS
515 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
516 {
517   _systemctl_all_units
518   compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
519     ${_sys_all_units[*]} )
520 }
521
522 # Completion functions for RELOADABLE_UNITS
523 for fun in reload reload-or-try-restart force-reload ; do
524   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
525   {
526     _systemctl_active_units
527     compadd "$@" - $( _filter_units_by_property CanReload yes \
528       ${_sys_active_units[*]} )
529   }
530 done
531
532 # Completion functions for RESTARTABLE_UNITS
533 for fun in restart reload-or-restart ; do
534   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
535   {
536     _systemctl_all_units
537     compadd "$@" - $( _filter_units_by_property CanStart yes \
538       ${_sys_all_units[*]} | while read line; do \
539       [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
540       done )
541   }
542 done
543
544 # Completion functions for MASKED_UNITS
545 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
546 {
547   _systemctl_masked_units
548   compadd "$@" -a - _sys_masked_units || _message "no masked unit found"
549 }
550
551 # Completion functions for JOBS
552 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
553 {
554   compadd "$@" - $(__systemctl list-jobs \
555     | cut -d' ' -f1  2>/dev/null ) || _message "no job found"
556 }
557
558 # Completion functions for SNAPSHOTS
559 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
560 {
561   compadd "$@" - $(__systemctl list-units --type snapshot --all \
562     | cut -d' ' -f1  2>/dev/null ) || _message "no snapshot found"
563 }
564
565 # Completion functions for ENVS
566 for fun in set-environment unset-environment ; do
567   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
568   {
569     local fun=$0 ; fun=${fun##_systemctl_}
570     local suf
571     if [[ "${fun}" = "set-environment" ]]; then
572       suf='-S='
573     fi
574
575     compadd "$@" ${suf} - $(systemctl show-environment \
576       | while read line; do echo " ${line%%\=}";done )
577   }
578 done
579
580 (( $+functions[_systemctl_link] )) || _systemctl_link() { _files }
581
582 # no systemctl completion for:
583 #    [STANDALONE]='daemon-reexec daemon-reload default dump
584 #                  emergency exit halt kexec list-jobs list-units
585 #                  list-unit-files poweroff reboot rescue show-environment'
586 #         [NAME]='snapshot load'
587
588 _systemctl_caching_policy()
589 {
590   local _sysunits
591   local -a oldcache
592
593   # rebuild if cache is more than a day old
594   oldcache=( "$1"(mh+1) )
595   (( $#oldcache )) && return 0
596
597   _sysunits=($(__systemctl --all | cut -d' ' -f1))
598
599   if (( $#_sysunits )); then
600     for unit in $_sysunits; do
601       [[ "$unit" -nt "$1" ]] && return 0
602     done
603   fi
604
605   return 1
606 }
607
608 _list_fields() {
609     local -a journal_fields
610     journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
611                     ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID}
612                     _{P,U,G}ID _COMM _EXE _CMDLINE
613                     _AUDIT_{SESSION,LOGINUID}
614                     _SYSTEMD_{CGROUP,SESSION,UNIT,OWNER_UID}
615                     _SYSTEMD_USER_UNIT
616                     _SELINUX_CONTEXT _SOURCE_REALTIME_TIMESTAMP
617                     _{BOOT,MACHINE}_ID _HOSTNAME _TRANSPORT
618                     _KERNEL_{DEVICE,SUBSYSTEM}
619                     _UDEV_{SYSNAME,DEVNODE,DEVLINK}
620                     __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP)
621     _describe 'possible fields' journal_fields
622 }
623
624 _journal_none() {
625     local -a _commands _files
626     _commands=( ${(f)"$(_call_program commands "$service" -F _EXE 2>/dev/null)"} )
627     _alternative : \
628         'files:/dev files:_files -W /dev -P /dev/' \
629         "commands:commands:($_commands[@])" \
630         'fields:fields:_list_fields'
631 }
632
633 _journal_fields() {
634     local -a _fields cmd
635     cmd=("journalctl" "-F ${@[-1]}" "2>/dev/null" )
636     _fields=( ${(f)"$(_call_program fields $cmd[@])"} )
637     typeset -U _fields
638     _describe 'possible values' _fields
639 }
640
641
642 _loginctl_all_sessions(){_sys_all_sessions=($(loginctl list-sessions | { while read a b; do echo " $a"; done; }) )}
643 _loginctl_all_users()   {_sys_all_users=(   $(loginctl list-users    | { while read a b; do echo " $a"; done; }) )}
644 _loginctl_all_seats()   {_sys_all_seats=(   $(loginctl list-seats    | { while read a b; do echo " $a"; done; }) )}
645
646 # Completion functions for SESSIONS
647 for fun in session-status show-session activate lock-session unlock-session terminate-session kill-session ; do
648   (( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
649   {
650     _loginctl_all_sessions
651     compadd "$@" -a - _sys_all_sessions
652   }
653 done
654
655 # Completion functions for USERS
656 for fun in user-status show-user enable-linger disable-linger terminate-user kill-user ; do
657   (( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
658   {
659     _loginctl_all_users
660     compadd "$@" -a - _sys_all_users
661   }
662 done
663
664 # Completion functions for SEATS
665 (( $+functions[_loginctl_seats] )) || _loginctl_seats()
666 {
667   _loginctl_all_seats
668   compadd "$@" -a - _sys_all_seats
669 }
670 for fun in seat-status show-seat terminate-seat ; do
671   (( $+functions[_loginctl_$fun] )) || _loginctl_$fun()
672   { _loginctl_seats }
673 done
674
675 # Completion functions for ATTACH
676 (( $+functions[_loginctl_attach] )) || _loginctl_attach()
677 {
678   _loginctl_all_seats
679
680   _arguments -w -C -S -s \
681     ':seat:_loginctl_seats' \
682     '*:device:_files'
683 }
684
685 # no loginctl completion for:
686 # [STANDALONE]='list-sessions list-users list-seats flush-devices'
687
688 (( $+functions[_loginctl_command] )) || _loginctl_command()
689 {
690   local -a _loginctl_cmds
691   _loginctl_cmds=(
692     "list-sessions:List sessions"
693     "session-status:Show session status"
694     "show-session:Show properties of one or more sessions"
695     "activate:Activate a session"
696     "lock-session:Screen lock one or more sessions"
697     "unlock-session:Screen unlock one or more sessions"
698     "terminate-session:Terminate one or more sessions"
699     "kill-session:Send signal to processes of a session"
700     "list-users:List users"
701     "user-status:Show user status"
702     "show-user:Show properties of one or more users"
703     "enable-linger:Enable linger state of one or more users"
704     "disable-linger:Disable linger state of one or more users"
705     "terminate-user:Terminate all sessions of one or more users"
706     "kill-user:Send signal to processes of a user"
707     "list-seats:List seats"
708     "seat-status:Show seat status"
709     "show-seat:Show properties of one or more seats"
710     "attach:Attach one or more devices to a seat"
711     "flush-devices:Flush all device associations"
712     "terminate-seat:Terminate all sessions on one or more seats"
713   )
714
715   if (( CURRENT == 1 )); then
716     _describe -t commands 'loginctl command' _loginctl_cmds || compadd "$@"
717   else
718     local curcontext="$curcontext"
719
720     cmd="${${_loginctl_cmds[(r)$words[1]:*]%%:*}}"
721
722     if (( $#cmd )); then
723       curcontext="${curcontext%:*:*}:loginctl-${cmd}:"
724
725       _call_function ret _loginctl_$cmd || _message 'no more arguments'
726     else
727       _message "unknown loginctl command: $words[1]"
728     fi
729     return ret
730   fi
731 }
732
733 _hostnamectl_command() {
734     local -a _hostnamectl_cmds
735     _hostnamectl_cmds=(
736         "status:Show current hostname settings"
737         "set-hostname:Set system hostname"
738         "set-icon-name:Set icon name for host"
739     )
740     if (( CURRENT == 1 )); then
741         _describe -t commands 'hostnamectl commands' _hostnamectl_cmds || compadd "$@"
742     else
743         local curcontext="$curcontext"
744         cmd="${${_hostnamectl_cmds[(r)$words[1]:*]%%:*}}"
745         if (( $#cmd )); then
746             [[ $cmd == status ]] && msg="no options" || msg="options for $cmd"
747             _message "$msg"
748         else
749             _message "unknown hostnamectl command: $words[1]"
750         fi
751     fi
752 }
753
754 _localectl_set-locale() {
755     local -a _confs _locales
756     local expl suf
757     _locales=( ${(f)"$(_call_program locales "$service" list-locales)"} )
758     _confs=( ${${(f)"$(_call_program confs "locale 2>/dev/null")"}%\=*} )
759     if [[ -prefix 1 *\= ]]; then
760         local conf=${PREFIX%%\=*}
761         compset -P1 '*='
762         _wanted locales expl "locales configs" \
763             _combination localeconfs  confs=$conf locales "$@" -
764     else
765         compadd -S '='  $_confs
766     fi
767 }
768
769 _localectl_set-keymap() {
770     local -a _keymaps
771     _keymaps=( ${(f)"$(_call_program locales "$service" list-keymaps)"} )
772     if (( CURRENT <= 3 )); then
773         _describe keymaps _keymaps
774     else
775         _message "no more options"
776     fi
777 }
778
779 _localectl_set-x11-keymap() {
780     if (( $+commands[pkg-config] )); then
781         local -a _file _layout _model _variant _options
782         local _xorg_lst
783         _xorg_lst=${"$($commands[pkg-config] xkeyboard-config --variable=xkb_base)"}
784         _file=( ${(ps:\n\!:)"$(<$_xorg_lst/rules/xorg.lst)"} )
785         _layout=( ${${${(M)${(f)_file[1]}:#  *}#  }%% *} )
786         _model=( ${${${(M)${(f)_file[2]}:#  *}#  }%% *} )
787         _variant=( ${${${(M)${(f)_file[3]}:#  *}#  }%% *} )
788         _options=( ${${${(M)${(f)_file[4]}:#  *}#  }%% *} )
789         #_layout=( ${(f)"$( echo $_file[1] | awk '/^  / {print $1}' )"} )
790         #_model=( ${(f)"$(echo $_file[2] | awk '/^  / {print $1}')"} )
791         #_variant=( ${(f)"$(echo $_file[3] | awk '/^  / {print $1}')"} )
792         #_options=( ${(f)"$(echo ${_file[4]//:/\\:} | awk '/^  / {print $1}')"} )
793
794         case $CURRENT in
795             2) _describe layouts _layout ;;
796             3) _describe models _model;;
797             4) _describe variants _variant;;
798             5) _describe options _options;;
799             *) _message "no more options"
800         esac
801     fi
802 }
803
804
805 _localectl_command() {
806     local -a _localectl_cmds
807     _localectl_cmds=(
808         'status:Show current locale settings'
809         'set-locale:Set system locale'
810         'list-locales:Show known locales'
811         'set-keymap:Set virtual console keyboard mapping'
812         'list-keymaps:Show known virtual console keyboard mappings'
813         'set-x11-keymap:Set X11 keyboard mapping'
814     )
815     if (( CURRENT == 1 )); then
816         _describe -t commands 'localectl command' _localectl_cmds
817     else
818         local curcontext="$curcontext"
819         cmd="${${_localectl_cmds[(r)$words[1]:*]%%:*}}"
820         if (( $+functions[_localectl_$cmd] )); then
821             _localectl_$cmd
822         else
823             _message "no more options"
824         fi
825     fi
826 }
827
828 _timedatectl_set-timezone(){
829     local -a _timezones
830     _timezones=( ${(f)"$(_call_program timezones "${service}" list-timezones)"} )
831     compadd "$_timezones[@]"
832 }
833
834 _timedatectl_set-time(){
835     _message "YYYY-MM-DD HH:MM:SS"
836 }
837
838 _timedatectl_set-local-rtc(){
839     local -a _options
840     _options=(
841         '0:Maintain RTC in universal time'
842         '1:Maintain RTC in local time'
843     )
844     _describe options _options
845 }
846
847 _timedatectl_set-ntp(){
848     local -a _options
849     _options=(
850         '0:Disable NTP based network time configuration'
851         '1:Enable NTP based network time configuration'
852     )
853     _describe options _options
854 }
855
856 _timedatectl_command(){
857     local -a _timedatectl_cmds
858     _timedatectl_cmds=(
859         'status:Show current time settings'
860         'set-time:Set system time'
861         'set-timezone:Set system timezone'
862         'list-timezones:Show known timezones'
863         'set-local-rtc:Control whether RTC is in local time'
864         'set-ntp:Control whether NTP is enabled'
865     )
866     if (( CURRENT == 1 )); then
867         _describe -t commands 'timedatectl command' _timedatectl_cmds
868     else
869         local curcontext="$curcontext"
870         cmd="${${_timedatectl_cmds[(r)$words[1]:*]%%:*}}"
871         if (( $#cmd )); then
872             if (( $+functions[_timedatectl_$cmd] )); then
873                 _timedatectl_$cmd
874             else
875                 _message "no more options"
876             fi
877         else
878             _message "unknown timedatectl command: $words[1]"
879         fi
880     fi
881 }
882 _systemd-coredumpctl_command(){
883     local -a _systemd_coredumpctl_cmds
884     _systemd_coredumpctl_cmds=(
885             'list:List available coredumps'
886             'dump:Print coredump to std'
887     )
888     if (( CURRENT == 1 )); then
889         _describe -t commands 'systemd-coredumpctl command' _systemd_coredumpctl_cmds
890     else
891         local curcontext="$curcontext"
892         local -a dumps
893         cmd="${${_systemd_coredumpctl_cmds[(r)$words[1]:*]%%:*}}"
894         if (( $#cmd  )); then
895             dumps=( "${(f)$(_call_program dumps "systemd-coredumpctl list 2>/dev/null")}" )
896             if [[ -n "$dumps" ]]; then
897                 compadd "${dumps[@]}"
898             else
899                 _message "no coredumps"
900             fi
901         else
902             _message "no more options"
903         fi
904
905     fi
906
907 }
908
909 _udevadm_info(){
910     _arguments \
911         '--query=[Query the database for specified type of device data. It needs the --path or --name to identify the specified device.]:type:(name symlink path property all)' \
912         '--path=[The devpath of the device to query.]:sys files:_files -P /sys/ -W /sys' \
913         '--name=[The name of the device node or a symlink to query]:device files:_files -P /dev/ -W /dev' \
914         '--root[Print absolute paths in name or symlink query.]' \
915         '--attribute-walk[Print all sysfs properties of the specified device that can be used in udev rules to match the specified device]' \
916         '--export[Print output as key/value pairs.]' \
917         '--export-prefix=[Add a prefix to the key name of exported values.]:prefix' \
918         '--device-id-of-file=[Print major/minor numbers of the underlying device, where the file lives on.]:files:_udevadm_mounts' \
919         '--export-db[Export the content of the udev database.]' \
920         '--cleanup-db[Cleanup the udev database.]'
921 }
922
923 _udevadm_trigger(){
924     _arguments \
925         '--verbose[Print the list of devices which will be triggered.]' \
926         '--dry-run[Do not actually trigger the event.]' \
927         '--type=[Trigger a specific type of devices.]:types:(devices subsystems failed)' \
928         '--action=[Type of event to be triggered.]:actions:(add change remove)' \
929         '--subsystem-match=[Trigger events for devices which belong to a matching subsystem.]' \
930         '--subsystem-nomatch=[Do not trigger events for devices which belong to a matching subsystem.]' \
931         '--attr-match=attribute=[Trigger events for devices with a matching sysfs attribute.]' \
932         '--attr-nomatch=attribute=[Do not trigger events for devices with a matching sysfs attribute.]' \
933         '--property-match=[Trigger events for devices with a matching property value.]' \
934         '--tag-match=property[Trigger events for devices with a matching tag.]' \
935         '--sysname-match=[Trigger events for devices with a matching sys device name.]' \
936         '--parent-match=[Trigger events for all children of a given device.]'
937 }
938
939 _udevadm_settle(){
940     _arguments \
941        '--timeout=[Maximum number of seconds to wait for the event queue to become empty.]' \
942        '--seq-start=[Wait only for events after the given sequence number.]' \
943        '--seq-end=[Wait only for events before the given sequence number.]' \
944        '--exit-if-exists=[Stop waiting if file exists.]:files:_files' \
945        '--quiet[Do not print any output, like the remaining queue entries when reaching the timeout.]' \
946        '--help[Print help text.]'
947 }
948
949 _udevadm_control(){
950     _arguments \
951         '--exit[Signal and wait for systemd-udevd to exit.]' \
952         '--log-priority=[Set the internal log level of systemd-udevd.]:priorities:(err info debug)' \
953         '--stop-exec-queue[Signal systemd-udevd to stop executing new events. Incoming events will be queued.]' \
954         '--start-exec-queue[Signal systemd-udevd to enable the execution of events.]' \
955         '--reload[Signal systemd-udevd to reload the rules files and other databases like the kernel module index.]' \
956         '--property=[Set a global property for all events.]' \
957         '--children-max=[Set the maximum number of events.]' \
958         '--timeout=[The maximum number of seconds to wait for a reply from systemd-udevd.]' \
959         '--help[Print help text.]'
960 }
961
962 _udevadm_monitor(){
963     _arguments \
964         '--kernel[Print the kernel uevents.]' \
965         '--udev[Print the udev event after the rule processing.]' \
966         '--property[Also print the properties of the event.]' \
967         '--subsystem-match=[Filter events by subsystem[/devtype].]' \
968         '--tag-match=[Filter events by property.]' \
969         '--help[Print help text.]'
970 }
971
972 _udevadm_test(){
973     _arguments \
974         '--action=[The action string.]:actions:(add change remove)' \
975         '--subsystem=[The subsystem string.]' \
976         '--help[Print help text.]' \
977         '*::devpath:_files -P /sys/ -W /sys'
978 }
979
980 _udevadm_test-builtin(){
981     if (( CURRENT == 2 )); then
982     _arguments \
983         '--help[Print help text]' \
984         '*::builtins:(blkid btrfs hwdb input_id kmod path_id usb_id uaccess)'
985     elif  (( CURRENT == 3 )); then
986         _arguments \
987             '--help[Print help text]' \
988             '*::syspath:_files -P /sys -W /sys'
989     else
990         _arguments \
991             '--help[Print help text]'
992     fi
993 }
994
995 _udevadm_mounts(){
996   local dev_tmp dpath_tmp mp_tmp mline
997
998     tmp=( "${(@f)$(< /etc/mtab)}" )
999     dev_tmp=( "${(@)${(@)tmp%% *}:#none}" )
1000     mp_tmp=( "${(@)${(@)tmp#* }%% *}" )
1001
1002   local MATCH
1003   mp_tmp=("${(@q)mp_tmp//(#m)\\[0-7](#c3)/${(#)$(( 8#${MATCH[2,-1]} ))}}")
1004   dpath_tmp=( "${(@Mq)dev_tmp:#/*}" )
1005   dev_tmp=( "${(@q)dev_tmp:#/*}" )
1006
1007   _alternative \
1008     'device-paths: device path:compadd -a dpath_tmp' \
1009     'directories:mount point:compadd -a mp_tmp'
1010 }
1011
1012
1013 _udevadm_command(){
1014     local -a _udevadm_cmds
1015     _udevadm_cmds=(
1016         'info:query sysfs or the udev database'
1017         'trigger:request events from the kernel'
1018         'settle:wait for the event queue to finish'
1019         'control:control the udev daemon'
1020         'monitor:listen to kernel and udev events'
1021         'test:test an event run'
1022         'test-builtin:test a built-in command'
1023     )
1024
1025     if ((CURRENT == 1)); then
1026         _describe -t commands 'udevadm commands' _udevadm_cmds
1027     else
1028         local curcontext="$curcontext"
1029         cmd="${${_udevadm_cmds[(r)$words[1]:*]%%:*}}"
1030         if (($#cmd)); then
1031             if (( $+functions[_udevadm_$cmd] )); then
1032                 _udevadm_$cmd
1033             else
1034                 _message "no options for $cmd"
1035             fi
1036         else
1037             _message "no more options"
1038         fi
1039     fi
1040 }
1041
1042 _ctls "$@"
1043
1044 #vim: set ft=zsh sw=4 ts=4 et