Debian Codegraph @

shell-completion/bash/systemctl.in

raw
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 2>/dev/null
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 -r 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 --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 --kernel-cmdline --drop-in --when'
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 --when)
206 comps='show cancel auto'
207 ;;
208 esac
209 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
210 return 0
211 fi
212
213 if [[ "$cur" = -* ]]; then
214 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
215 return 0
216 fi
217
218 local -A VERBS=(
219 [ALL_UNITS]='cat mask'
220 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
221 [ENABLED_UNITS]='disable'
222 [DISABLED_UNITS]='enable'
223 [REENABLABLE_UNITS]='reenable'
224 [FAILED_UNITS]='reset-failed'
225 [STARTABLE_UNITS]='start'
226 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart freeze thaw'
227 [ISOLATABLE_UNITS]='isolate'
228 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
229 [RESTARTABLE_UNITS]='restart reload-or-restart'
230 [TARGET_AND_UNITS]='add-wants add-requires'
231 [MASKED_UNITS]='unmask'
232 [JOBS]='cancel'
233 [ENVS]='set-environment unset-environment import-environment'
234 [STANDALONE]='daemon-reexec daemon-reload default whoami
235 emergency exit halt hibernate hybrid-sleep sleep
236 suspend-then-hibernate kexec soft-reboot list-jobs list-sockets
237 list-timers list-units list-unit-files poweroff
238 reboot rescue show-environment suspend get-default
239 is-system-running preset-all list-automounts list-paths
240 enqueue-marked'
241 [FILE]='link switch-root'
242 [TARGETS]='set-default'
243 [MACHINES]='list-machines'
244 [LOG_LEVEL]='log-level'
245 [LOG_TARGET]='log-target'
246 [SERVICE_LOG_LEVEL]='service-log-level'
247 [SERVICE_LOG_TARGET]='service-log-target'
248 [SERVICE_WATCHDOGS]='service-watchdogs'
249 [MOUNT]='bind mount-image'
250 )
251
252 for ((i=0; i < COMP_CWORD; i++)); do
253 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
254 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
255 verb=${COMP_WORDS[i]}
256 break
257 fi
258 done
259
260 # When trying to match a unit name with certain special characters in its name (i.e
261 # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible
262 # match to fail.
263 # The following condition solves two cases:
264 # 1) We're trying to complete an already escaped unit name part,
265 # i.e foo\\x2dba. In this case we need to unescape the name, so it
266 # gets properly matched with the systemctl output (i.e. foo\x2dba).
267 # However, we need to keep the original escaped name as well for the
268 # final match, as the completion machinery does the unescaping
269 # automagically.
270 # 2) We're trying to complete an unescaped (literal) unit name part,
271 # i.e. foo\x2dba. That means we don't have to do the unescaping
272 # required for correct matching with systemctl's output, however,
273 # we need to escape the name for the final match, where the completion
274 # expects the string to be escaped.
275 cur_orig=$cur
276 if [[ $cur =~ '\\' ]]; then
277 cur="$(echo $cur | xargs echo)"
278 else
279 cur_orig="$(printf '%q' $cur)"
280 fi
281
282 if [[ -z ${verb-} ]]; then
283 comps="${VERBS[*]}"
284
285 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
286 comps=$( __get_all_units $mode "$cur" )
287 compopt -o filenames
288
289 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
290 comps=$( __get_non_template_units $mode "$cur" )
291 compopt -o filenames
292
293 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
294 comps=$( __get_enabled_units $mode "$cur" )
295 compopt -o filenames
296
297 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
298 comps=$( __get_disabled_units $mode "$cur"
299 __get_template_names $mode "$cur")
300 compopt -o filenames
301
302 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
303 comps=$( __get_disabled_units $mode "$cur"
304 __get_enabled_units $mode "$cur"
305 __get_template_names $mode "$cur")
306 compopt -o filenames
307
308 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
309 comps=$( __get_startable_units $mode "$cur" )
310 compopt -o filenames
311
312 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
313 comps=$( __get_restartable_units $mode "$cur" )
314 compopt -o filenames
315
316 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
317 comps=$( __get_stoppable_units $mode "$cur" )
318 compopt -o filenames
319
320 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
321 comps=$( __get_reloadable_units $mode "$cur" )
322 compopt -o filenames
323
324 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
325 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
326 $( __get_non_template_units $mode "$cur" ) )
327 compopt -o filenames
328
329 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
330 comps=$( __get_failed_units $mode "$cur" )
331 compopt -o filenames
332
333 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
334 comps=$( __get_masked_units $mode "$cur" )
335 compopt -o filenames
336
337 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
338 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} ||
339 __contains_word "$prev" ${OPTS[STANDALONE]}; then
340 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" |
341 { while read -r a b; do echo " $a"; done; } )
342 else
343 comps=$( __get_all_unit_files $mode "$cur" )
344 fi
345 compopt -o filenames
346
347 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
348 comps=''
349
350 elif __contains_word "$verb" ${VERBS[JOBS]}; then
351 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
352
353 elif [ "$verb" = 'unset-environment' ]; then
354 comps=$( __systemctl $mode show-environment |
355 while read -r line; do echo " ${line%%=*}"; done )
356 compopt -o nospace
357
358 elif [ "$verb" = 'set-environment' ]; then
359 comps=$( __systemctl $mode show-environment |
360 while read -r line; do echo " ${line%%=*}="; done )
361 compopt -o nospace
362
363 elif [ "$verb" = 'import-environment' ]; then
364 COMPREPLY=( $(compgen -A variable -- "$cur_orig") )
365 return 0
366
367 elif __contains_word "$verb" ${VERBS[FILE]}; then
368 comps=$( compgen -A file -- "$cur" )
369 compopt -o filenames
370
371 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
372 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" |
373 { while read -r a b; do echo " $a"; done; } )
374 elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then
375 comps='debug info notice warning err crit alert emerg'
376 elif __contains_word "$verb" ${VERBS[LOG_TARGET]}; then
377 comps='console journal kmsg journal-or-kmsg null'
378 elif __contains_word "$verb" ${VERBS[SERVICE_LOG_LEVEL]}; then
379 if __contains_word "$prev" ${VERBS[SERVICE_LOG_LEVEL]}; then
380 comps=$( __get_all_unit_files $mode "$cur" )
381 elif __contains_word "$prev" debug info notice warning err crit alert emerg; then
382 return 0
383 else
384 comps='debug info notice warning err crit alert emerg'
385 fi
386 elif __contains_word "$verb" ${VERBS[SERVICE_LOG_TARGET]}; then
387 if __contains_word "$prev" ${VERBS[SERVICE_LOG_TARGET]}; then
388 comps=$( __get_all_unit_files $mode "$cur" )
389 elif __contains_word "$prev" console journal kmsg journal-or-kmsg null; then
390 return 0
391 else
392 comps='console journal kmsg journal-or-kmsg null'
393 fi
394 elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then
395 comps='on off'
396 elif __contains_word "$verb" ${VERBS[MOUNT]}; then
397 if __contains_word "$prev" ${VERBS[MOUNT]}; then
398 comps=$( __get_active_services $mode "$cur" )
399 elif [[ "$prev" =~ .service ]]; then
400 comps=$( compgen -A file -- "$cur" )
401 compopt -o filenames
402 else
403 return 0
404 fi
405 fi
406
407 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
408 return 0
409 }
410
411 complete -F _systemctl systemctl