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