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