]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl.in
Merge pull request #14409 from poettering/shutdown-modernization
[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
ZJS
9 local mode=$1; shift 1
10 systemctl $mode --full --no-legend --no-pager "$@" 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
ZJS
115 local a b
116 { machinectl list-images --no-legend --no-pager; machinectl list --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'
214 )
215
216 for ((i=0; i < COMP_CWORD; i++)); do
217 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
218 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
219 verb=${COMP_WORDS[i]}
220 break
221 fi
222 done
d611dadc 223
72c9177d 224 # When trying to match a unit name with certain special characters in its name (i.e
cec82cb9
FS
225 # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible
226 # match to fail.
227 # The following condition solves two cases:
228 # 1) We're trying to complete an already escaped unit name part,
229 # i.e foo\\x2dba. In this case we need to unescape the name, so it
230 # gets properly matched with the systemctl output (i.e. foo\x2dba).
231 # However, we need to keep the original escaped name as well for the
232 # final match, as the completion machinery does the unescaping
233 # automagically.
234 # 2) We're trying to complete an unescaped (literal) unit name part,
235 # i.e. foo\x2dba. That means we don't have to do the unescaping
236 # required for correct matching with systemctl's output, however,
237 # we need to escape the name for the final match, where the completion
238 # expects the string to be escaped.
239 cur_orig=$cur
240 if [[ $cur =~ '\\' ]]; then
241 cur="$(echo $cur | xargs echo)"
242 else
243 cur_orig="$(printf '%q' $cur)"
244 fi
72c9177d 245
843cfcb1
ZJS
246 if [[ -z $verb ]]; then
247 comps="${VERBS[*]}"
aea447c9 248
843cfcb1
ZJS
249 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
250 comps=$( __get_all_units $mode "$cur" )
251 compopt -o filenames
d611dadc 252
843cfcb1
ZJS
253 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
254 comps=$( __get_non_template_units $mode "$cur" )
255 compopt -o filenames
d611dadc 256
843cfcb1
ZJS
257 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
258 comps=$( __get_enabled_units $mode "$cur" )
259 compopt -o filenames
c2e09812 260
843cfcb1
ZJS
261 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
262 comps=$( __get_disabled_units $mode "$cur";
263 __get_template_names $mode "$cur")
264 compopt -o filenames
d611dadc 265
843cfcb1
ZJS
266 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
267 comps=$( __get_disabled_units $mode "$cur";
268 __get_enabled_units $mode "$cur";
269 __get_template_names $mode "$cur")
270 compopt -o filenames
d611dadc 271
843cfcb1
ZJS
272 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
273 comps=$( __get_startable_units $mode "$cur" )
274 compopt -o filenames
d611dadc 275
843cfcb1
ZJS
276 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
277 comps=$( __get_restartable_units $mode "$cur" )
278 compopt -o filenames
d611dadc 279
843cfcb1
ZJS
280 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
281 comps=$( __get_stoppable_units $mode "$cur" )
282 compopt -o filenames
d611dadc 283
843cfcb1
ZJS
284 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
285 comps=$( __get_reloadable_units $mode "$cur" )
286 compopt -o filenames
d611dadc 287
843cfcb1
ZJS
288 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
289 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
babf4f68 290 $( __get_non_template_units $mode "$cur" ) )
843cfcb1
ZJS
291 compopt -o filenames
292
293 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
294 comps=$( __get_failed_units $mode "$cur" )
295 compopt -o filenames
d611dadc 296
843cfcb1
ZJS
297 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
298 comps=$( __get_masked_units $mode "$cur" )
299 compopt -o filenames
300
301 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
302 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
8fc5cd71 303 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
843cfcb1
ZJS
304 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
305 | { while read -r a b; do echo " $a"; done; } )
306 else
307 comps=$( __get_all_unit_files $mode "$cur" )
308 fi
309 compopt -o filenames
8fc5cd71 310
843cfcb1
ZJS
311 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
312 comps=''
d611dadc 313
843cfcb1
ZJS
314 elif __contains_word "$verb" ${VERBS[JOBS]}; then
315 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc 316
843cfcb1
ZJS
317 elif __contains_word "$verb" ${VERBS[ENVS]}; then
318 comps=$( __systemctl $mode show-environment \
319 | while read -r line; do echo " ${line%%=*}="; done )
320 compopt -o nospace
d611dadc 321
843cfcb1
ZJS
322 elif __contains_word "$verb" ${VERBS[FILE]}; then
323 comps=$( compgen -A file -- "$cur" )
324 compopt -o filenames
035dd8c0 325
843cfcb1
ZJS
326 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
327 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
328 | { while read -r a b; do echo " $a"; done; } )
329 fi
d611dadc 330
72c9177d 331 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
843cfcb1 332 return 0
d611dadc
MB
333}
334
335complete -F _systemctl systemctl