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