]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/zsh/_systemctl.in
networkd: fix route table from unsigned char to uint32_t (#6083)
[thirdparty/systemd.git] / shell-completion / zsh / _systemctl.in
CommitLineData
ff7a0685
WG
1#compdef systemctl
2
3(( $+functions[_systemctl_command] )) || _systemctl_command()
4{
5 local -a _systemctl_cmds
6 _systemctl_cmds=(
d808ca64 7 "list-sockets:List sockets"
d1811159 8 "list-timers:List timers"
ff7a0685
WG
9 "list-units:List units"
10 "start:Start (activate) one or more units"
11 "stop:Stop (deactivate) one or more units"
12 "reload:Reload one or more units"
13 "restart:Start or restart one or more units"
14 "condrestart:Restart one or more units if active"
15 "try-restart:Restart one or more units if active"
16 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
17 "force-reload:Reload one or more units if possible, otherwise restart if active"
18 "hibernate:Hibernate the system"
19 "hybrid-sleep:Hibernate and suspend the system"
aabf5d42 20 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
ff7a0685
WG
21 "isolate:Start one unit and stop all others"
22 "kill:Send signal to processes of a unit"
23 "is-active:Check whether units are active"
24 "is-failed:Check whether units are failed"
25 "status:Show runtime status of one or more units"
26 "show:Show properties of one or more units/jobs or the manager"
6da49b8b 27 "cat:Show the source unit files and drop-ins"
ff7a0685 28 "reset-failed:Reset failed state for all, one, or more units"
ff7a0685
WG
29 "list-unit-files:List installed unit files"
30 "enable:Enable one or more unit files"
31 "disable:Disable one or more unit files"
32 "reenable:Reenable one or more unit files"
33 "preset:Enable/disable one or more unit files based on preset configuration"
1cf3c30c
ZJS
34 "set-default:Set the default target"
35 "get-default:Query the default target"
409886c4 36 "edit:Edit one or more unit files"
1cf3c30c 37 "is-system-running:Query overall status of the system"
ff7a0685
WG
38 "help:Show documentation for specified units"
39 "list-dependencies:Show unit dependency tree"
40 "mask:Mask one or more units"
41 "unmask:Unmask one or more units"
42 "link:Link one or more units files into the search path"
43 "is-enabled:Check whether unit files are enabled"
44 "list-jobs:List jobs"
45 "cancel:Cancel all, one, or more jobs"
ff7a0685
WG
46 "show-environment:Dump environment"
47 "set-environment:Set one or more environment variables"
48 "unset-environment:Unset one or more environment variables"
49 "daemon-reload:Reload systemd manager configuration"
50 "daemon-reexec:Reexecute systemd manager"
51 "default:Enter system default mode"
52 "rescue:Enter system rescue mode"
53 "emergency:Enter system emergency mode"
54 "halt:Shut down and halt the system"
55 "suspend:Suspend the system"
56 "poweroff:Shut down and power-off the system"
57 "reboot:Shut down and reboot the system"
58 "kexec:Shut down and reboot the system with kexec"
59 "exit:Ask for user instance termination"
7b742b31 60 "switch-root:Change root directory"
0f8158bd 61 "revert:Revert unit files to their vendor versions"
ff7a0685
WG
62 )
63
64 if (( CURRENT == 1 )); then
65 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
66 else
d34b7c11 67 local curcontext="$curcontext" expl
ff7a0685
WG
68
69 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
70 # Deal with any aliases
71 case $cmd in
72 condrestart) cmd="try-restart";;
aabf5d42 73 force-reload) cmd="try-reload-or-restart";;
ff7a0685
WG
74 esac
75
76 if (( $#cmd )); then
77 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
78
79 local update_policy
80 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
81 if [[ -z "$update_policy" ]]; then
82 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
83 fi
84
85 _call_function ret _systemctl_$cmd || _message 'no more arguments'
86 else
87 _message "unknown systemctl command: $words[1]"
88 fi
89 return ret
90 fi
91}
92
93__systemctl()
94{
99171d2f 95 systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null
ff7a0685
WG
96}
97
98
99# Fills the unit list
100_systemctl_all_units()
101{
88e4dbd5
АТ
102 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
103 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
ff7a0685 104 then
2103d29d 105 _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
88e4dbd5 106 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
ff7a0685
WG
107 fi
108}
109
110# Fills the unit list including all file units
111_systemctl_really_all_units()
112{
113 local -a all_unit_files;
114 local -a really_all_units;
88e4dbd5
АТ
115 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
116 ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
ff7a0685 117 then
2103d29d 118 all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
ff7a0685
WG
119 _systemctl_all_units
120 really_all_units=($_sys_all_units $all_unit_files)
121 _sys_really_all_units=(${(u)really_all_units})
88e4dbd5 122 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
ff7a0685
WG
123 fi
124}
125
126_filter_units_by_property() {
6c9414a7
EC
127 local property=$1 value=$2; shift 2
128 local -a units; units=("${(q-)@}")
129 local -A props
130 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
131 echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}"
ff7a0685
WG
132}
133
e4e868f3 134_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
e9a19bd8 135
f29c77bc 136
2103d29d 137_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
81333ecf
ZJS
138
139_systemctl_startable_units(){
67afa931 140 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
81333ecf
ZJS
141 _filter_units_by_property CanStart yes $(
142 __systemctl $mode list-unit-files --state enabled,disabled,static | \
143 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
144 __systemctl $mode list-units --state inactive,failed | \
67afa931 145 { while read -r a b; do echo -E - " $a"; done; } )) ) )
81333ecf
ZJS
146}
147
148_systemctl_restartable_units(){
67afa931 149 _sys_restartable_units=( $(_filter_units_by_property CanStart yes $(
81333ecf
ZJS
150 __systemctl $mode list-unit-files --state enabled,disabled,static | \
151 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
152 __systemctl $mode list-units | \
67afa931 153 { while read -r a b; do echo -E - " $a"; done; } )) )
81333ecf
ZJS
154}
155
ed119049 156_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) }
fb869ca1 157_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) }
ff7a0685 158
67afa931 159local fun
ff7a0685 160# Completion functions for ALL_UNITS
0f8158bd 161for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit revert ; do
ff7a0685
WG
162 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
163 {
164 _systemctl_really_all_units
d34b7c11
EC
165 _wanted systemd-units expl unit \
166 compadd "$@" -a - _sys_really_all_units
ff7a0685
WG
167 }
168done
169
170# Completion functions for ENABLED_UNITS
e9a19bd8
ZJS
171(( $+functions[_systemctl_disable] )) || _systemctl_disable()
172{
fb869ca1 173 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
174 _wanted systemd-units expl 'enabled unit' \
175 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
e9a19bd8
ZJS
176}
177
178(( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
179{
fb869ca1 180 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
181 _wanted systemd-units expl 'enabled/disabled unit' \
182 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
e9a19bd8 183}
ff7a0685
WG
184
185# Completion functions for DISABLED_UNITS
186(( $+functions[_systemctl_enable] )) || _systemctl_enable()
187{
fb869ca1 188 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
189 _wanted systemd-units expl 'disabled unit' \
190 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
ff7a0685
WG
191}
192
193# Completion functions for FAILED_UNITS
194(( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
195{
463985a9 196 local _sys_failed_units; _systemctl_failed_units
d34b7c11
EC
197 _wanted systemd-units expl 'failed unit' \
198 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
ff7a0685
WG
199}
200
201# Completion functions for STARTABLE_UNITS
202(( $+functions[_systemctl_start] )) || _systemctl_start()
203{
bf8864c2 204 local _sys_startable_units; _systemctl_startable_units
d34b7c11
EC
205 _wanted systemd-units expl 'startable unit' \
206 compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
ff7a0685
WG
207}
208
209# Completion functions for STOPPABLE_UNITS
210for fun in stop kill try-restart condrestart ; do
211 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
212 {
bf8864c2 213 local _sys_active_units; _systemctl_active_units
d34b7c11
EC
214 _wanted systemd-units expl 'stoppable unit' \
215 compadd "$@" - $( _filter_units_by_property CanStop yes \
216 ${_sys_active_units[*]} )
ff7a0685
WG
217 }
218done
219
220# Completion functions for ISOLATABLE_UNITS
221(( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
222{
223 _systemctl_all_units
d34b7c11
EC
224 _wanted systemd-units expl 'isolatable unit' \
225 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
226 ${_sys_all_units[*]} )
ff7a0685
WG
227}
228
229# Completion functions for RELOADABLE_UNITS
aabf5d42 230for fun in reload try-reload-or-restart force-reload ; do
ff7a0685
WG
231 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
232 {
bf8864c2 233 local _sys_active_units; _systemctl_active_units
d34b7c11
EC
234 _wanted systemd-units expl 'reloadable unit' \
235 compadd "$@" - $( _filter_units_by_property CanReload yes \
236 ${_sys_active_units[*]} )
ff7a0685
WG
237 }
238done
239
240# Completion functions for RESTARTABLE_UNITS
241for fun in restart reload-or-restart ; do
242 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
243 {
bf8864c2 244 local _sys_restartable_units; _systemctl_restartable_units
d34b7c11
EC
245 _wanted systemd-units expl 'restartable unit' \
246 compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
ff7a0685
WG
247 }
248done
249
250# Completion functions for MASKED_UNITS
251(( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
252{
fb869ca1 253 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
254 _wanted systemd-units expl 'masked unit' \
255 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
ff7a0685
WG
256}
257
258# Completion functions for JOBS
259(( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
260{
d34b7c11
EC
261 _wanted systemd-jobs expl job \
262 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
263 _message "no jobs found"
ff7a0685
WG
264}
265
1cf3c30c
ZJS
266# Completion functions for TARGETS
267(( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
268{
d34b7c11
EC
269 _wanted systemd-targets expl target \
270 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
271 _message "no targets found"
ff7a0685
WG
272}
273
274# Completion functions for ENVS
275for fun in set-environment unset-environment ; do
276 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
277 {
278 local fun=$0 ; fun=${fun##_systemctl_}
279 local suf
280 if [[ "${fun}" = "set-environment" ]]; then
281 suf='-S='
282 fi
d34b7c11
EC
283 _wanted systemd-environment expl 'environment variable' \
284 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
ff7a0685
WG
285 }
286done
287
2c12a402
ZJS
288(( $+functions[_systemctl_link] )) || _systemctl_link() {
289 _sd_unit_files
290}
ff7a0685 291
7b742b31
ZJS
292(( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
293 _files
294}
295
ff7a0685 296# no systemctl completion for:
299c397c 297# [STANDALONE]='daemon-reexec daemon-reload default
ff7a0685
WG
298# emergency exit halt kexec list-jobs list-units
299# list-unit-files poweroff reboot rescue show-environment'
ff7a0685
WG
300
301_systemctl_caching_policy()
302{
303 local _sysunits
304 local -a oldcache
305
306 # rebuild if cache is more than a day old
307 oldcache=( "$1"(mh+1) )
308 (( $#oldcache )) && return 0
309
2103d29d 310 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
ff7a0685
WG
311
312 if (( $#_sysunits )); then
313 for unit in $_sysunits; do
314 [[ "$unit" -nt "$1" ]] && return 0
315 done
316 fi
317
318 return 1
319}
320
298b9e23
WG
321_unit_states() {
322 local -a _states
840b2c0e 323 _states=("${(fo)$(__systemctl --state=help)}")
298b9e23
WG
324 _values -s , "${_states[@]}"
325}
326
327_unit_types() {
328 local -a _types
840b2c0e 329 _types=("${(fo)$(__systemctl -t help)}")
298b9e23
WG
330 _values -s , "${_types[@]}"
331}
332
c0a67aef 333_unit_properties() {
88e4dbd5
АТ
334 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
335 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
c0a67aef 336 then
2103d29d
EC
337 _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all;
338 @rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*}
339 )
88e4dbd5 340 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
c0a67aef
ZJS
341 fi
342 _values -s , "${_sys_all_properties[@]}"
343}
344
903e7c37
ZJS
345_job_modes() {
346 local -a _modes
347 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
348 _values -s , "${_modes[@]}"
349}
350
e09d0d46 351# Build arguments for "systemctl" to be used in completion.
68c4f6d4 352local -a _modes; _modes=("--user" "--system")
e09d0d46
DH
353# Use the last mode (they are exclusive and the last one is used).
354local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
ff7a0685
WG
355_arguments -s \
356 {-h,--help}'[Show help]' \
357 '--version[Show package version]' \
862f4963 358 {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
903e7c37
ZJS
359 '--state=[Display units in the specified state]:unit state:_unit_states' \
360 '--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \
c0a67aef 361 {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \
ff7a0685
WG
362 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
363 '--reverse[Show reverse dependencies]' \
364 '--after[Show units ordered after]' \
365 '--before[Show units ordered before]' \
ff7a0685 366 {-l,--full}"[Don't ellipsize unit names on output]" \
ff7a0685 367 '--show-types[When showing sockets, show socket type]' \
ff7a0685
WG
368 {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
369 {-q,--quiet}'[Suppress output]' \
370 '--no-block[Do not wait until operation finished]' \
371 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
372 '--no-pager[Do not pipe output into a pager]' \
373 '--system[Connect to system manager]' \
374 '--user[Connect to user service manager]' \
375 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
376 '--global[Enable/disable unit files globally]' \
377 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
378 '--no-ask-password[Do not ask for system passwords]' \
379 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
862f4963 380 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
ff7a0685
WG
381 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
382 '--root=[Enable unit files in the specified root directory]:directory:_directories' \
383 '--runtime[Enable unit files only temporarily until next reboot]' \
6da49b8b 384 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
ff7a0685 385 {-P,--privileged}'[Acquire privileges before execution]' \
862f4963 386 {-n+,--lines=}'[Journal entries to show]:number of entries' \
a02c5fe7 387 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
5bdf2243 388 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
ff7a0685 389 '--plain[When used with list-dependencies, print output as a list]' \
bef19548 390 '--failed[Show failed units]' \
ff7a0685 391 '*::systemctl command:_systemctl_command'