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