]>
Commit | Line | Data |
---|---|---|
f8457290 | 1 | # shellcheck shell=bash |
d611dadc | 2 | # systemctl(1) completion -*- shell-script -*- |
05ebcbd5 | 3 | # vi: ft=sh |
db9ecf05 | 4 | # SPDX-License-Identifier: LGPL-2.1-or-later |
d611dadc MB |
5 | # |
6 | # This file is part of systemd. | |
7 | # | |
96b2fb93 | 8 | # Copyright © 2010 Ran Benita |
d611dadc MB |
9 | |
10 | __systemctl() { | |
843cfcb1 | 11 | local mode=$1; shift 1 |
d60bd2ff | 12 | systemctl $mode --full --legend=no --no-pager --plain "$@" 2>/dev/null |
d611dadc MB |
13 | } |
14 | ||
caffaf58 | 15 | __systemd_properties() { |
b0d3095f | 16 | {{LIBEXECDIR}}/systemd --dump-bus-properties |
caffaf58 ZJS |
17 | } |
18 | ||
d611dadc | 19 | __contains_word () { |
843cfcb1 ZJS |
20 | local w word=$1; shift |
21 | for w in "$@"; do | |
22 | [[ $w = "$word" ]] && return | |
23 | done | |
d611dadc MB |
24 | } |
25 | ||
5908656c | 26 | {% raw -%} |
b1bdb649 | 27 | __filter_units_by_properties () { |
843cfcb1 ZJS |
28 | local mode=$1 properties=$2; shift 2 |
29 | local units=("$@") | |
30 | local props i p n | |
31 | local names= count=0 | |
32 | ||
33 | IFS=$',' read -r -a p < <(echo "Names,$properties") | |
34 | n=${#p[*]} | |
35 | readarray -t props < \ | |
36 | <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}") | |
37 | ||
38 | for ((i=0; i < ${#props[*]}; i++)); do | |
39 | if [[ -z ${props[i]} ]]; then | |
40 | if (( count == n )) && [[ -n $names ]]; then | |
f28255e2 | 41 | echo $names |
843cfcb1 ZJS |
42 | fi |
43 | names= | |
44 | count=0 | |
45 | else | |
46 | (( count++ )) | |
47 | if [[ ${props[i]%%=*} == 'Names' ]]; then | |
48 | names=${props[i]#*=} | |
49 | fi | |
f28255e2 | 50 | fi |
843cfcb1 ZJS |
51 | done |
52 | if (( count == n )) && [[ -n $names ]]; then | |
53 | echo $names | |
54 | fi | |
b1bdb649 | 55 | } |
5908656c | 56 | {% endraw %} |
b1bdb649 | 57 | |
98476dc8 | 58 | __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ |
843cfcb1 | 59 | | { while read -r a b; do echo " $a"; done; }; } |
98476dc8 | 60 | __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ |
843cfcb1 | 61 | | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; } |
c839b729 | 62 | __get_template_names () { __systemctl $1 list-unit-files "$2*" \ |
843cfcb1 | 63 | | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; } |
c839b729 | 64 | __get_active_units () { __systemctl $1 list-units "$2*" \ |
843cfcb1 | 65 | | { while read -r a b; do echo " $a"; done; }; } |
c24c63e9 LB |
66 | __get_active_services() { __systemctl $1 list-units "$2*.service" \ |
67 | | { while read -r a b; do echo " $a"; done; }; } | |
f28255e2 YW |
68 | |
69 | __get_not_masked_unit_files() { | |
843cfcb1 ZJS |
70 | # filter out masked, not-found, or template units. |
71 | __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \ | |
72 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } | |
f28255e2 YW |
73 | } |
74 | ||
f29c77bc | 75 | __get_startable_units () { |
843cfcb1 ZJS |
76 | __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $( |
77 | { __get_not_masked_unit_files $1 $2 | |
78 | # get inactive template units | |
79 | __systemctl $1 list-units --state inactive,failed "$2*" | \ | |
80 | { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; } | |
81 | } | sort -u ) | |
9ff8af54 | 82 | } |
f29c77bc | 83 | __get_restartable_units () { |
843cfcb1 ZJS |
84 | # filter out masked and not-found |
85 | __filter_units_by_properties $1 CanStart=yes $( | |
86 | { __get_not_masked_unit_files $1 $2 | |
87 | __get_active_units $1 $2 | |
88 | } | sort -u ) | |
f28255e2 YW |
89 | } |
90 | ||
91 | __get_stoppable_units () { | |
843cfcb1 ZJS |
92 | # filter out masked and not-found |
93 | local units=$( | |
94 | { __get_not_masked_unit_files $1 $2 | |
95 | __get_active_units $1 $2 | |
96 | } | sort -u ) | |
97 | __filter_units_by_properties $1 ActiveState=active,CanStop=yes $units | |
98 | __filter_units_by_properties $1 ActiveState=reloading,CanStop=yes $units | |
99 | __filter_units_by_properties $1 ActiveState=activating,CanStop=yes $units | |
9ff8af54 | 100 | } |
f28255e2 YW |
101 | |
102 | __get_reloadable_units () { | |
843cfcb1 ZJS |
103 | # filter out masked and not-found |
104 | __filter_units_by_properties $1 ActiveState=active,CanReload=yes $( | |
105 | { __get_not_masked_unit_files $1 $2 | |
106 | __get_active_units $1 $2 | |
107 | } | sort -u ) | |
f28255e2 YW |
108 | } |
109 | ||
05ebcbd5 FS |
110 | __get_failed_units() { __systemctl $1 list-units "$2*" \ |
111 | | while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; } | |
112 | __get_enabled_units() { __systemctl $1 list-unit-files "$2*" \ | |
113 | | while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; } | |
114 | __get_disabled_units() { __systemctl $1 list-unit-files "$2*" \ | |
115 | | while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; } | |
116 | __get_masked_units() { __systemctl $1 list-unit-files "$2*" \ | |
117 | | while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; } | |
118 | __get_all_unit_files() { __systemctl $1 list-unit-files "$2*" | while read -r a b; do echo " $a"; done; } | |
d611dadc | 119 | |
3a221b5d | 120 | __get_machines() { |
05ebcbd5 FS |
121 | local a |
122 | ||
123 | while read a _; do | |
124 | echo " $a" | |
125 | done < <(machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null | sort -u; echo ".host") | |
3a221b5d EV |
126 | } |
127 | ||
d611dadc | 128 | _systemctl () { |
843cfcb1 | 129 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
cec82cb9 | 130 | local i verb comps mode cur_orig |
d611dadc | 131 | |
843cfcb1 ZJS |
132 | local -A OPTS=( |
133 | [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global | |
97e27852 FS |
134 | --help -h --no-ask-password --no-block --legend=no --no-pager --no-reload --no-wall --now |
135 | --quiet -q --system --user --version --runtime --recursive -r --firmware-setup | |
136 | --show-types --plain --failed --value --fail --dry-run --wait --no-warn --with-dependencies | |
137 | --show-transaction -T --mkdir --marked --read-only' | |
138 | [ARG]='--host -H --kill-whom --property -p -P --signal -s --type -t --state --job-mode --root | |
139 | --preset-mode -n --lines -o --output -M --machine --message --timestamp --check-inhibitors --what | |
140 | --image --boot-loader-menu --boot-loader-entry --reboot-argument --drop-in' | |
843cfcb1 ZJS |
141 | ) |
142 | ||
143 | if __contains_word "--user" ${COMP_WORDS[*]}; then | |
144 | mode=--user | |
145 | elif __contains_word "--global" ${COMP_WORDS[*]}; then | |
146 | mode=--user | |
147 | else | |
148 | mode=--system | |
149 | fi | |
150 | ||
151 | if __contains_word "$prev" ${OPTS[ARG]}; then | |
152 | case $prev in | |
153 | --signal|-s) | |
154 | _signals | |
155 | return | |
156 | ;; | |
157 | --type|-t) | |
158 | comps=$(__systemctl $mode -t help) | |
159 | ;; | |
160 | --state) | |
161 | comps=$(__systemctl $mode --state=help) | |
162 | ;; | |
163 | --job-mode) | |
164 | comps='fail replace replace-irreversibly isolate | |
903e7c37 | 165 | ignore-dependencies ignore-requirements flush' |
843cfcb1 | 166 | ;; |
4ccde410 | 167 | --kill-whom|--kill-who) |
843cfcb1 ZJS |
168 | comps='all control main' |
169 | ;; | |
170 | --root) | |
171 | comps=$(compgen -A directory -- "$cur" ) | |
172 | compopt -o filenames | |
173 | ;; | |
174 | --host|-H) | |
175 | comps=$(compgen -A hostname) | |
176 | ;; | |
97e27852 | 177 | --property|-p|-P) |
843cfcb1 ZJS |
178 | comps=$(__systemd_properties) |
179 | ;; | |
180 | --preset-mode) | |
181 | comps='full enable-only disable-only' | |
182 | ;; | |
183 | --output|-o) | |
184 | comps=$( systemctl --output=help 2>/dev/null ) | |
185 | ;; | |
186 | --machine|-M) | |
187 | comps=$( __get_machines ) | |
188 | ;; | |
46ad9c53 LB |
189 | --timestamp) |
190 | comps='pretty us µs utc us+utc µs+utc' | |
191 | ;; | |
4327574f FS |
192 | --check-inhibitors) |
193 | comps='auto yes no' | |
194 | ;; | |
97e27852 FS |
195 | --what) |
196 | comps='configuration state cache logs runtime all' | |
197 | ;; | |
198 | --image) | |
199 | comps=$(compgen -A file -- "$cur") | |
200 | compopt -o filenames | |
201 | ;; | |
202 | --boot-loader-entry) | |
203 | comps=$(systemctl --boot-loader-entry=help 2>/dev/null) | |
204 | ;; | |
843cfcb1 ZJS |
205 | esac |
206 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
207 | return 0 | |
208 | fi | |
d611dadc | 209 | |
843cfcb1 ZJS |
210 | if [[ "$cur" = -* ]]; then |
211 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
212 | return 0 | |
213 | fi | |
d611dadc | 214 | |
843cfcb1 ZJS |
215 | local -A VERBS=( |
216 | [ALL_UNITS]='cat mask' | |
aea447c9 | 217 | [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert' |
843cfcb1 ZJS |
218 | [ENABLED_UNITS]='disable' |
219 | [DISABLED_UNITS]='enable' | |
c2e09812 | 220 | [REENABLABLE_UNITS]='reenable' |
843cfcb1 ZJS |
221 | [FAILED_UNITS]='reset-failed' |
222 | [STARTABLE_UNITS]='start' | |
97e27852 | 223 | [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart freeze thaw' |
843cfcb1 ZJS |
224 | [ISOLATABLE_UNITS]='isolate' |
225 | [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload' | |
d611dadc | 226 | [RESTARTABLE_UNITS]='restart reload-or-restart' |
843cfcb1 ZJS |
227 | [TARGET_AND_UNITS]='add-wants add-requires' |
228 | [MASKED_UNITS]='unmask' | |
229 | [JOBS]='cancel' | |
230 | [ENVS]='set-environment unset-environment import-environment' | |
7df82b8d | 231 | [STANDALONE]='daemon-reexec daemon-reload default whoami |
c58493c0 | 232 | emergency exit halt hibernate hybrid-sleep |
1386e34b | 233 | suspend-then-hibernate kexec soft-reboot list-jobs list-sockets |
c58493c0 | 234 | list-timers list-units list-unit-files poweroff |
1cf3c30c | 235 | reboot rescue show-environment suspend get-default |
73369684 | 236 | is-system-running preset-all list-automounts list-paths' |
c24c63e9 | 237 | [FILE]='link switch-root' |
843cfcb1 ZJS |
238 | [TARGETS]='set-default' |
239 | [MACHINES]='list-machines' | |
4171837b YW |
240 | [LOG_LEVEL]='log-level' |
241 | [LOG_TARGET]='log-target' | |
79272d30 LB |
242 | [SERVICE_LOG_LEVEL]='service-log-level' |
243 | [SERVICE_LOG_TARGET]='service-log-target' | |
4171837b | 244 | [SERVICE_WATCHDOGS]='service-watchdogs' |
c24c63e9 | 245 | [MOUNT]='bind mount-image' |
843cfcb1 ZJS |
246 | ) |
247 | ||
248 | for ((i=0; i < COMP_CWORD; i++)); do | |
249 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && | |
250 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
251 | verb=${COMP_WORDS[i]} | |
252 | break | |
253 | fi | |
254 | done | |
d611dadc | 255 | |
72c9177d | 256 | # When trying to match a unit name with certain special characters in its name (i.e |
cec82cb9 FS |
257 | # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible |
258 | # match to fail. | |
259 | # The following condition solves two cases: | |
260 | # 1) We're trying to complete an already escaped unit name part, | |
261 | # i.e foo\\x2dba. In this case we need to unescape the name, so it | |
262 | # gets properly matched with the systemctl output (i.e. foo\x2dba). | |
263 | # However, we need to keep the original escaped name as well for the | |
264 | # final match, as the completion machinery does the unescaping | |
265 | # automagically. | |
266 | # 2) We're trying to complete an unescaped (literal) unit name part, | |
267 | # i.e. foo\x2dba. That means we don't have to do the unescaping | |
268 | # required for correct matching with systemctl's output, however, | |
269 | # we need to escape the name for the final match, where the completion | |
270 | # expects the string to be escaped. | |
271 | cur_orig=$cur | |
272 | if [[ $cur =~ '\\' ]]; then | |
273 | cur="$(echo $cur | xargs echo)" | |
274 | else | |
275 | cur_orig="$(printf '%q' $cur)" | |
276 | fi | |
72c9177d | 277 | |
36ec0268 | 278 | if [[ -z ${verb-} ]]; then |
843cfcb1 | 279 | comps="${VERBS[*]}" |
aea447c9 | 280 | |
843cfcb1 ZJS |
281 | elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then |
282 | comps=$( __get_all_units $mode "$cur" ) | |
283 | compopt -o filenames | |
d611dadc | 284 | |
843cfcb1 ZJS |
285 | elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then |
286 | comps=$( __get_non_template_units $mode "$cur" ) | |
287 | compopt -o filenames | |
d611dadc | 288 | |
843cfcb1 ZJS |
289 | elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then |
290 | comps=$( __get_enabled_units $mode "$cur" ) | |
291 | compopt -o filenames | |
c2e09812 | 292 | |
843cfcb1 ZJS |
293 | elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then |
294 | comps=$( __get_disabled_units $mode "$cur"; | |
295 | __get_template_names $mode "$cur") | |
296 | compopt -o filenames | |
d611dadc | 297 | |
843cfcb1 ZJS |
298 | elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then |
299 | comps=$( __get_disabled_units $mode "$cur"; | |
300 | __get_enabled_units $mode "$cur"; | |
301 | __get_template_names $mode "$cur") | |
302 | compopt -o filenames | |
d611dadc | 303 | |
843cfcb1 ZJS |
304 | elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then |
305 | comps=$( __get_startable_units $mode "$cur" ) | |
306 | compopt -o filenames | |
d611dadc | 307 | |
843cfcb1 ZJS |
308 | elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then |
309 | comps=$( __get_restartable_units $mode "$cur" ) | |
310 | compopt -o filenames | |
d611dadc | 311 | |
843cfcb1 ZJS |
312 | elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then |
313 | comps=$( __get_stoppable_units $mode "$cur" ) | |
314 | compopt -o filenames | |
d611dadc | 315 | |
843cfcb1 ZJS |
316 | elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then |
317 | comps=$( __get_reloadable_units $mode "$cur" ) | |
318 | compopt -o filenames | |
d611dadc | 319 | |
843cfcb1 ZJS |
320 | elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then |
321 | comps=$( __filter_units_by_properties $mode AllowIsolate=yes \ | |
babf4f68 | 322 | $( __get_non_template_units $mode "$cur" ) ) |
843cfcb1 ZJS |
323 | compopt -o filenames |
324 | ||
325 | elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then | |
326 | comps=$( __get_failed_units $mode "$cur" ) | |
327 | compopt -o filenames | |
d611dadc | 328 | |
843cfcb1 ZJS |
329 | elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then |
330 | comps=$( __get_masked_units $mode "$cur" ) | |
331 | compopt -o filenames | |
332 | ||
333 | elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then | |
334 | if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \ | |
8fc5cd71 | 335 | || __contains_word "$prev" ${OPTS[STANDALONE]}; then |
843cfcb1 ZJS |
336 | comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \ |
337 | | { while read -r a b; do echo " $a"; done; } ) | |
338 | else | |
339 | comps=$( __get_all_unit_files $mode "$cur" ) | |
340 | fi | |
341 | compopt -o filenames | |
8fc5cd71 | 342 | |
843cfcb1 ZJS |
343 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
344 | comps='' | |
d611dadc | 345 | |
843cfcb1 ZJS |
346 | elif __contains_word "$verb" ${VERBS[JOBS]}; then |
347 | comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } ) | |
d611dadc | 348 | |
34199208 ZJS |
349 | elif [ "$verb" = 'unset-environment' ]; then |
350 | comps=$( __systemctl $mode show-environment \ | |
351 | | while read -r line; do echo " ${line%%=*}"; done ) | |
352 | compopt -o nospace | |
353 | ||
354 | elif [ "$verb" = 'set-environment' ]; then | |
843cfcb1 ZJS |
355 | comps=$( __systemctl $mode show-environment \ |
356 | | while read -r line; do echo " ${line%%=*}="; done ) | |
357 | compopt -o nospace | |
d611dadc | 358 | |
34199208 ZJS |
359 | elif [ "$verb" = 'import-environment' ]; then |
360 | COMPREPLY=( $(compgen -A variable -- "$cur_orig") ) | |
361 | return 0 | |
362 | ||
843cfcb1 ZJS |
363 | elif __contains_word "$verb" ${VERBS[FILE]}; then |
364 | comps=$( compgen -A file -- "$cur" ) | |
365 | compopt -o filenames | |
035dd8c0 | 366 | |
843cfcb1 ZJS |
367 | elif __contains_word "$verb" ${VERBS[TARGETS]}; then |
368 | comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \ | |
369 | | { while read -r a b; do echo " $a"; done; } ) | |
4171837b YW |
370 | elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then |
371 | comps='debug info notice warning err crit alert emerg' | |
372 | elif __contains_word "$verb" ${VERBS[LOG_TARGET]}; then | |
373 | comps='console journal kmsg journal-or-kmsg null' | |
79272d30 LB |
374 | elif __contains_word "$verb" ${VERBS[SERVICE_LOG_LEVEL]}; then |
375 | if __contains_word "$prev" ${VERBS[SERVICE_LOG_LEVEL]}; then | |
376 | comps=$( __get_all_unit_files $mode "$cur" ) | |
377 | elif __contains_word "$prev" debug info notice warning err crit alert emerg; then | |
378 | return 0 | |
379 | else | |
380 | comps='debug info notice warning err crit alert emerg' | |
381 | fi | |
382 | elif __contains_word "$verb" ${VERBS[SERVICE_LOG_TARGET]}; then | |
383 | if __contains_word "$prev" ${VERBS[SERVICE_LOG_TARGET]}; then | |
384 | comps=$( __get_all_unit_files $mode "$cur" ) | |
385 | elif __contains_word "$prev" console journal kmsg journal-or-kmsg null; then | |
386 | return 0 | |
387 | else | |
388 | comps='console journal kmsg journal-or-kmsg null' | |
389 | fi | |
4171837b YW |
390 | elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then |
391 | comps='on off' | |
c24c63e9 LB |
392 | elif __contains_word "$verb" ${VERBS[MOUNT]}; then |
393 | if __contains_word "$prev" ${VERBS[MOUNT]}; then | |
394 | comps=$( __get_active_services $mode "$cur" ) | |
395 | elif [[ "$prev" =~ .service ]]; then | |
396 | comps=$( compgen -A file -- "$cur" ) | |
397 | compopt -o filenames | |
398 | else | |
399 | return 0 | |
400 | fi | |
843cfcb1 | 401 | fi |
d611dadc | 402 | |
72c9177d | 403 | COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") ) |
843cfcb1 | 404 | return 0 |
d611dadc MB |
405 | } |
406 | ||
407 | complete -F _systemctl systemctl |