]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl.in
Merge pull request #15862 from hunger/master
[thirdparty/systemd.git] / shell-completion / bash / systemctl.in
CommitLineData
d611dadc 1# systemctl(1) completion -*- shell-script -*-
7059062c 2# SPDX-License-Identifier: LGPL-2.1+
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
7
8__systemctl() {
843cfcb1 9 local mode=$1; shift 1
1cabd2d0 10 systemctl $mode --full --no-legend --no-pager --plain "$@" 2>/dev/null
d611dadc
MB
11}
12
caffaf58 13__systemd_properties() {
843cfcb1 14 @rootlibexecdir@/systemd --dump-bus-properties
caffaf58
ZJS
15}
16
d611dadc 17__contains_word () {
843cfcb1
ZJS
18 local w word=$1; shift
19 for w in "$@"; do
20 [[ $w = "$word" ]] && return
21 done
d611dadc
MB
22}
23
b1bdb649 24__filter_units_by_properties () {
843cfcb1
ZJS
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
f28255e2 38 echo $names
843cfcb1
ZJS
39 fi
40 names=
41 count=0
42 else
43 (( count++ ))
44 if [[ ${props[i]%%=*} == 'Names' ]]; then
45 names=${props[i]#*=}
46 fi
f28255e2 47 fi
843cfcb1
ZJS
48 done
49 if (( count == n )) && [[ -n $names ]]; then
50 echo $names
51 fi
b1bdb649
YW
52}
53
98476dc8 54__get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 55 | { while read -r a b; do echo " $a"; done; }; }
98476dc8 56__get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 57 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
c839b729 58__get_template_names () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 59 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
c839b729 60__get_active_units () { __systemctl $1 list-units "$2*" \
843cfcb1 61 | { while read -r a b; do echo " $a"; done; }; }
f28255e2
YW
62
63__get_not_masked_unit_files() {
843cfcb1
ZJS
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; }
f28255e2
YW
67}
68
f29c77bc 69__get_startable_units () {
843cfcb1
ZJS
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 )
9ff8af54 76}
f29c77bc 77__get_restartable_units () {
843cfcb1
ZJS
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 )
f28255e2
YW
83}
84
85__get_stoppable_units () {
843cfcb1
ZJS
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
9ff8af54 94}
f28255e2
YW
95
96__get_reloadable_units () {
843cfcb1
ZJS
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 )
f28255e2
YW
102}
103
c839b729 104__get_failed_units () { __systemctl $1 list-units "$2*" \
843cfcb1 105 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
c839b729 106__get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 107 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
c839b729 108__get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 109 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
c839b729 110__get_masked_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 111 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
98476dc8 112__get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; }
d611dadc 113
3a221b5d 114__get_machines() {
843cfcb1 115 local a b
e2268fa4 116 { machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; } | \
9521d558 117 { while read a b; do echo " $a"; done; }
3a221b5d
EV
118}
119
d611dadc 120_systemctl () {
843cfcb1 121 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
cec82cb9 122 local i verb comps mode cur_orig
d611dadc 123
843cfcb1
ZJS
124 local -A OPTS=(
125 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
0067c7b2 126 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
035dd8c0
YW
127 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
128 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
843cfcb1 129 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
035dd8c0 130 --preset-mode -n --lines -o --output -M --machine --message'
843cfcb1
ZJS
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
903e7c37 155 ignore-dependencies ignore-requirements flush'
843cfcb1
ZJS
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
d611dadc 183
843cfcb1
ZJS
184 if [[ "$cur" = -* ]]; then
185 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
186 return 0
187 fi
d611dadc 188
843cfcb1
ZJS
189 local -A VERBS=(
190 [ALL_UNITS]='cat mask'
aea447c9 191 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
843cfcb1
ZJS
192 [ENABLED_UNITS]='disable'
193 [DISABLED_UNITS]='enable'
c2e09812 194 [REENABLABLE_UNITS]='reenable'
843cfcb1
ZJS
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'
d611dadc 200 [RESTARTABLE_UNITS]='restart reload-or-restart'
843cfcb1
ZJS
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
c58493c0 206 emergency exit halt hibernate hybrid-sleep
e68c79db 207 suspend-then-hibernate kexec list-jobs list-sockets
c58493c0 208 list-timers list-units list-unit-files poweroff
1cf3c30c 209 reboot rescue show-environment suspend get-default
035dd8c0 210 is-system-running preset-all'
843cfcb1
ZJS
211 [FILE]='link switch-root'
212 [TARGETS]='set-default'
213 [MACHINES]='list-machines'
4171837b
YW
214 [LOG_LEVEL]='log-level'
215 [LOG_TARGET]='log-target'
216 [SERVICE_WATCHDOGS]='service-watchdogs'
843cfcb1
ZJS
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
d611dadc 226
72c9177d 227 # When trying to match a unit name with certain special characters in its name (i.e
cec82cb9
FS
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
72c9177d 248
843cfcb1
ZJS
249 if [[ -z $verb ]]; then
250 comps="${VERBS[*]}"
aea447c9 251
843cfcb1
ZJS
252 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
253 comps=$( __get_all_units $mode "$cur" )
254 compopt -o filenames
d611dadc 255
843cfcb1
ZJS
256 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
257 comps=$( __get_non_template_units $mode "$cur" )
258 compopt -o filenames
d611dadc 259
843cfcb1
ZJS
260 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
261 comps=$( __get_enabled_units $mode "$cur" )
262 compopt -o filenames
c2e09812 263
843cfcb1
ZJS
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
d611dadc 268
843cfcb1
ZJS
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
d611dadc 274
843cfcb1
ZJS
275 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
276 comps=$( __get_startable_units $mode "$cur" )
277 compopt -o filenames
d611dadc 278
843cfcb1
ZJS
279 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
280 comps=$( __get_restartable_units $mode "$cur" )
281 compopt -o filenames
d611dadc 282
843cfcb1
ZJS
283 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
284 comps=$( __get_stoppable_units $mode "$cur" )
285 compopt -o filenames
d611dadc 286
843cfcb1
ZJS
287 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
288 comps=$( __get_reloadable_units $mode "$cur" )
289 compopt -o filenames
d611dadc 290
843cfcb1
ZJS
291 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
292 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
babf4f68 293 $( __get_non_template_units $mode "$cur" ) )
843cfcb1
ZJS
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
d611dadc 299
843cfcb1
ZJS
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]} \
8fc5cd71 306 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
843cfcb1
ZJS
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
8fc5cd71 313
843cfcb1
ZJS
314 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
315 comps=''
d611dadc 316
843cfcb1
ZJS
317 elif __contains_word "$verb" ${VERBS[JOBS]}; then
318 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc 319
843cfcb1
ZJS
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
d611dadc 324
843cfcb1
ZJS
325 elif __contains_word "$verb" ${VERBS[FILE]}; then
326 comps=$( compgen -A file -- "$cur" )
327 compopt -o filenames
035dd8c0 328
843cfcb1
ZJS
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; } )
4171837b
YW
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'
843cfcb1 338 fi
d611dadc 339
72c9177d 340 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
843cfcb1 341 return 0
d611dadc
MB
342}
343
344complete -F _systemctl systemctl