]>
Commit | Line | Data |
---|---|---|
1 | # shellcheck shell=bash | |
2 | # systemctl(1) completion -*- shell-script -*- | |
3 | # vi: ft=sh | |
4 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
5 | # | |
6 | # This file is part of systemd. | |
7 | # | |
8 | # Copyright © 2010 Ran Benita | |
9 | ||
10 | __systemctl() { | |
11 | local mode=$1; shift 1 | |
12 | SYSTEMD_COLORS=0 systemctl $mode --full --legend=no --no-pager --plain "$@" 2>/dev/null | |
13 | } | |
14 | ||
15 | __systemd_properties() { | |
16 | {{LIBEXECDIR}}/systemd --dump-bus-properties | |
17 | } | |
18 | ||
19 | __contains_word () { | |
20 | local w word=$1; shift | |
21 | for w in "$@"; do | |
22 | [[ $w = "$word" ]] && return | |
23 | done | |
24 | } | |
25 | ||
26 | {% raw -%} | |
27 | __filter_units_by_properties () { | |
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 | |
41 | echo $names | |
42 | fi | |
43 | names= | |
44 | count=0 | |
45 | else | |
46 | (( count++ )) | |
47 | if [[ ${props[i]%%=*} == 'Names' ]]; then | |
48 | names=${props[i]#*=} | |
49 | fi | |
50 | fi | |
51 | done | |
52 | if (( count == n )) && [[ -n $names ]]; then | |
53 | echo $names | |
54 | fi | |
55 | } | |
56 | {% endraw %} | |
57 | ||
58 | __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ | |
59 | | { while read -r a b; do echo " $a"; done; }; } | |
60 | __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \ | |
61 | | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; } | |
62 | __get_template_names () { __systemctl $1 list-unit-files "$2*" \ | |
63 | | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; } | |
64 | __get_active_units () { __systemctl $1 list-units "$2*" \ | |
65 | | { while read -r a b; do echo " $a"; done; }; } | |
66 | __get_active_services() { __systemctl $1 list-units "$2*.service" \ | |
67 | | { while read -r a b; do echo " $a"; done; }; } | |
68 | ||
69 | __get_not_masked_unit_files() { | |
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; } | |
73 | } | |
74 | ||
75 | __get_startable_units () { | |
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 ) | |
82 | } | |
83 | __get_restartable_units () { | |
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 ) | |
89 | } | |
90 | ||
91 | __get_stoppable_units () { | |
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 | |
100 | } | |
101 | ||
102 | __get_reloadable_units () { | |
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 ) | |
108 | } | |
109 | ||
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; } | |
119 | ||
120 | __get_machines() { | |
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") | |
126 | } | |
127 | ||
128 | _systemctl () { | |
129 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} | |
130 | local i verb comps mode cur_orig | |
131 | ||
132 | local -A OPTS=( | |
133 | [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global | |
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' | |
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 | |
165 | ignore-dependencies ignore-requirements flush' | |
166 | ;; | |
167 | --kill-whom|--kill-who) | |
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 | ;; | |
177 | --property|-p|-P) | |
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 | ;; | |
189 | --timestamp) | |
190 | comps='pretty us µs utc us+utc µs+utc' | |
191 | ;; | |
192 | --check-inhibitors) | |
193 | comps='auto yes no' | |
194 | ;; | |
195 | --what) | |
196 | comps='configuration state cache logs runtime fdstore 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 | ;; | |
205 | esac | |
206 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
207 | return 0 | |
208 | fi | |
209 | ||
210 | if [[ "$cur" = -* ]]; then | |
211 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
212 | return 0 | |
213 | fi | |
214 | ||
215 | local -A VERBS=( | |
216 | [ALL_UNITS]='cat mask' | |
217 | [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert' | |
218 | [ENABLED_UNITS]='disable' | |
219 | [DISABLED_UNITS]='enable' | |
220 | [REENABLABLE_UNITS]='reenable' | |
221 | [FAILED_UNITS]='reset-failed' | |
222 | [STARTABLE_UNITS]='start' | |
223 | [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart freeze thaw' | |
224 | [ISOLATABLE_UNITS]='isolate' | |
225 | [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload' | |
226 | [RESTARTABLE_UNITS]='restart reload-or-restart' | |
227 | [TARGET_AND_UNITS]='add-wants add-requires' | |
228 | [MASKED_UNITS]='unmask' | |
229 | [JOBS]='cancel' | |
230 | [ENVS]='set-environment unset-environment import-environment' | |
231 | [STANDALONE]='daemon-reexec daemon-reload default whoami | |
232 | emergency exit halt hibernate hybrid-sleep sleep | |
233 | suspend-then-hibernate kexec soft-reboot list-jobs list-sockets | |
234 | list-timers list-units list-unit-files poweroff | |
235 | reboot rescue show-environment suspend get-default | |
236 | is-system-running preset-all list-automounts list-paths' | |
237 | [FILE]='link switch-root' | |
238 | [TARGETS]='set-default' | |
239 | [MACHINES]='list-machines' | |
240 | [LOG_LEVEL]='log-level' | |
241 | [LOG_TARGET]='log-target' | |
242 | [SERVICE_LOG_LEVEL]='service-log-level' | |
243 | [SERVICE_LOG_TARGET]='service-log-target' | |
244 | [SERVICE_WATCHDOGS]='service-watchdogs' | |
245 | [MOUNT]='bind mount-image' | |
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 | |
255 | ||
256 | # When trying to match a unit name with certain special characters in its name (i.e | |
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 | |
277 | ||
278 | if [[ -z ${verb-} ]]; then | |
279 | comps="${VERBS[*]}" | |
280 | ||
281 | elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then | |
282 | comps=$( __get_all_units $mode "$cur" ) | |
283 | compopt -o filenames | |
284 | ||
285 | elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then | |
286 | comps=$( __get_non_template_units $mode "$cur" ) | |
287 | compopt -o filenames | |
288 | ||
289 | elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then | |
290 | comps=$( __get_enabled_units $mode "$cur" ) | |
291 | compopt -o filenames | |
292 | ||
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 | |
297 | ||
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 | |
303 | ||
304 | elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then | |
305 | comps=$( __get_startable_units $mode "$cur" ) | |
306 | compopt -o filenames | |
307 | ||
308 | elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then | |
309 | comps=$( __get_restartable_units $mode "$cur" ) | |
310 | compopt -o filenames | |
311 | ||
312 | elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then | |
313 | comps=$( __get_stoppable_units $mode "$cur" ) | |
314 | compopt -o filenames | |
315 | ||
316 | elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then | |
317 | comps=$( __get_reloadable_units $mode "$cur" ) | |
318 | compopt -o filenames | |
319 | ||
320 | elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then | |
321 | comps=$( __filter_units_by_properties $mode AllowIsolate=yes \ | |
322 | $( __get_non_template_units $mode "$cur" ) ) | |
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 | |
328 | ||
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]} \ | |
335 | || __contains_word "$prev" ${OPTS[STANDALONE]}; then | |
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 | |
342 | ||
343 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then | |
344 | comps='' | |
345 | ||
346 | elif __contains_word "$verb" ${VERBS[JOBS]}; then | |
347 | comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } ) | |
348 | ||
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 | |
355 | comps=$( __systemctl $mode show-environment \ | |
356 | | while read -r line; do echo " ${line%%=*}="; done ) | |
357 | compopt -o nospace | |
358 | ||
359 | elif [ "$verb" = 'import-environment' ]; then | |
360 | COMPREPLY=( $(compgen -A variable -- "$cur_orig") ) | |
361 | return 0 | |
362 | ||
363 | elif __contains_word "$verb" ${VERBS[FILE]}; then | |
364 | comps=$( compgen -A file -- "$cur" ) | |
365 | compopt -o filenames | |
366 | ||
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; } ) | |
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' | |
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 | |
390 | elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then | |
391 | comps='on off' | |
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 | |
401 | fi | |
402 | ||
403 | COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") ) | |
404 | return 0 | |
405 | } | |
406 | ||
407 | complete -F _systemctl systemctl |