]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl.in
tree-wide: drop license boilerplate
[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#
6# Copyright 2010 Ran Benita
d611dadc
MB
7
8__systemctl() {
9e542e0b 9 local mode=$1; shift 1
99171d2f 10 systemctl $mode --full --no-legend "$@" 2>/dev/null
d611dadc
MB
11}
12
caffaf58
ZJS
13__systemd_properties() {
14 local mode=$1
c0a67aef
ZJS
15 { __systemctl $mode show --all;
16 @rootlibexecdir@/systemd --dump-configuration-items; } |
caffaf58
ZJS
17 while IFS='=' read -r key value; do
18 [[ $value ]] && echo "$key"
19 done
20}
21
d611dadc 22__contains_word () {
a72d698d
DR
23 local w word=$1; shift
24 for w in "$@"; do
25 [[ $w = "$word" ]] && return
26 done
d611dadc
MB
27}
28
29__filter_units_by_property () {
9e542e0b 30 local mode=$1 property=$2 value=$3 ; shift 3
d611dadc 31 local units=("$@")
c07c80bc 32 local props i
d611dadc 33 IFS=$'\n' read -rd '' -a props < \
9e542e0b 34 <(__systemctl $mode show --property "$property" -- "${units[@]}")
d611dadc
MB
35 for ((i=0; $i < ${#units[*]}; i++)); do
36 if [[ "${props[i]}" = "$property=$value" ]]; then
79c16383 37 echo " ${units[i]}"
d611dadc
MB
38 fi
39 done
40}
41
b1bdb649
YW
42__filter_units_by_properties () {
43 local mode=$1 properties=$2 values=$3 ; shift 3
44 local units=("$@")
c07c80bc 45 local props i j conditions=()
b1bdb649
YW
46 IFS=$'\n' read -rd '' -a props < \
47 <(__systemctl $mode show --property "$properties" -- "${units[@]}")
48 IFS=$',' read -r -a properties < <(echo $properties)
49 IFS=$',' read -r -a values < <(echo $values)
b1bdb649
YW
50 for ((i=0; i < ${#properties[*]}; i++)); do
51 for ((j=0; j < ${#properties[*]}; j++)); do
52 if [[ ${props[i]%%=*} == ${properties[j]} ]]; then
53 conditions+=( "${properties[j]}=${values[j]}" )
54 fi
55 done
56 done
b1bdb649 57 for ((i=0; i < ${#units[*]}; i++)); do
b1bdb649 58 for ((j=0; j < ${#conditions[*]}; j++)); do
6bda23dd 59 if [[ "${props[i * ${#conditions[*]} + j]}" != "${conditions[j]}" ]]; then
b1bdb649
YW
60 break
61 fi
62 done
c07c80bc 63 if (( j == ${#conditions[*]} )); then
b1bdb649
YW
64 echo " ${units[i]}"
65 fi
66 done
67}
68
c6a373a2 69__get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \
9ff8af54 70 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
e9a19bd8
ZJS
71__get_template_names () { __systemctl $1 list-unit-files \
72 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
73
9e542e0b 74__get_active_units () { __systemctl $1 list-units \
79c16383 75 | { while read -r a b; do echo " $a"; done; }; }
f29c77bc 76__get_startable_units () {
9ff8af54 77 # find startable inactive units
6bda23dd
YW
78 __filter_units_by_properties $1 ActiveState,CanStart inactive,yes $(
79 { __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient | \
b1bdb649 80 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
6bda23dd 81 __systemctl $1 list-units --state inactive,failed | \
b1bdb649
YW
82 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
83 } | sort -u )
9ff8af54 84}
f29c77bc 85__get_restartable_units () {
9ff8af54 86 # filter out masked and not-found
6bda23dd
YW
87 __filter_units_by_property $1 CanStart yes $(
88 __systemctl $1 list-unit-files --state enabled,disabled,static | \
372b2211 89 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
6bda23dd 90 __systemctl $1 list-units | \
372b2211 91 { while read -r a b; do echo " $a"; done; } )
9ff8af54 92}
9e542e0b 93__get_failed_units () { __systemctl $1 list-units \
79c16383 94 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
9e542e0b 95__get_enabled_units () { __systemctl $1 list-unit-files \
79c16383 96 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
9e542e0b 97__get_disabled_units () { __systemctl $1 list-unit-files \
79c16383 98 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
9e542e0b 99__get_masked_units () { __systemctl $1 list-unit-files \
79c16383 100 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
8fc5cd71 101__get_all_unit_files () { { __systemctl $1 list-unit-files; } | { while read -r a b; do echo " $a"; done; }; }
d611dadc 102
3a221b5d
EV
103__get_machines() {
104 local a b
4ed2f1b1
ZJS
105 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
106 { while read a b; do echo " $a"; done; }
3a221b5d
EV
107}
108
d611dadc
MB
109_systemctl () {
110 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
9e542e0b 111 local i verb comps mode
d611dadc
MB
112
113 local -A OPTS=(
ed119049 114 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
0067c7b2 115 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
035dd8c0
YW
116 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
117 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
3a221b5d 118 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
035dd8c0 119 --preset-mode -n --lines -o --output -M --machine --message'
d611dadc
MB
120 )
121
caffaf58
ZJS
122 if __contains_word "--user" ${COMP_WORDS[*]}; then
123 mode=--user
93557fcb
EV
124 elif __contains_word "--global" ${COMP_WORDS[*]}; then
125 mode=--user
caffaf58
ZJS
126 else
127 mode=--system
128 fi
129
d611dadc
MB
130 if __contains_word "$prev" ${OPTS[ARG]}; then
131 case $prev in
132 --signal|-s)
c2af8101
EV
133 _signals
134 return
d611dadc
MB
135 ;;
136 --type|-t)
adb0c25e 137 comps=$(__systemctl $mode -t help)
d611dadc 138 ;;
631c922c 139 --state)
840b2c0e 140 comps=$(__systemctl $mode --state=help)
631c922c 141 ;;
903e7c37
ZJS
142 --job-mode)
143 comps='fail replace replace-irreversibly isolate
144 ignore-dependencies ignore-requirements flush'
145 ;;
d611dadc
MB
146 --kill-who)
147 comps='all control main'
148 ;;
d611dadc
MB
149 --root)
150 comps=$(compgen -A directory -- "$cur" )
151 compopt -o filenames
152 ;;
153 --host|-H)
154 comps=$(compgen -A hostname)
155 ;;
156 --property|-p)
caffaf58 157 comps=$(__systemd_properties $mode)
d611dadc 158 ;;
3a221b5d
EV
159 --preset-mode)
160 comps='full enable-only disable-only'
161 ;;
162 --output|-o)
7e563bfc 163 comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json
3a221b5d
EV
164 json-pretty json-sse cat'
165 ;;
166 --machine|-M)
167 comps=$( __get_machines )
168 ;;
d611dadc
MB
169 esac
170 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
171 return 0
172 fi
173
d611dadc
MB
174 if [[ "$cur" = -* ]]; then
175 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
176 return 0
177 fi
178
179 local -A VERBS=(
0f8158bd 180 [ALL_UNITS]='is-active is-failed is-enabled status show cat mask preset help list-dependencies edit set-property revert'
c2e09812 181 [ENABLED_UNITS]='disable'
d611dadc 182 [DISABLED_UNITS]='enable'
c2e09812 183 [REENABLABLE_UNITS]='reenable'
d611dadc
MB
184 [FAILED_UNITS]='reset-failed'
185 [STARTABLE_UNITS]='start'
186 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
187 [ISOLATABLE_UNITS]='isolate'
aabf5d42 188 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
d611dadc 189 [RESTARTABLE_UNITS]='restart reload-or-restart'
8fc5cd71 190 [TARGET_AND_UNITS]='add-wants add-requires'
d611dadc
MB
191 [MASKED_UNITS]='unmask'
192 [JOBS]='cancel'
035dd8c0 193 [ENVS]='set-environment unset-environment import-environment'
299c397c 194 [STANDALONE]='daemon-reexec daemon-reload default
c58493c0 195 emergency exit halt hibernate hybrid-sleep
e68c79db 196 suspend-then-hibernate kexec list-jobs list-sockets
c58493c0 197 list-timers list-units list-unit-files poweroff
1cf3c30c 198 reboot rescue show-environment suspend get-default
035dd8c0 199 is-system-running preset-all'
7b742b31 200 [FILE]='link switch-root'
99504dd4 201 [TARGETS]='set-default'
035dd8c0 202 [MACHINES]='list-machines'
d611dadc
MB
203 )
204
3ce09b7d 205 for ((i=0; i < COMP_CWORD; i++)); do
d611dadc
MB
206 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
207 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
208 verb=${COMP_WORDS[i]}
209 break
210 fi
211 done
212
86cb0691 213 if [[ -z $verb ]]; then
d611dadc
MB
214 comps="${VERBS[*]}"
215
216 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
9e542e0b 217 comps=$( __get_all_units $mode )
c317a1a1 218 compopt -o filenames
d611dadc
MB
219
220 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
9e542e0b 221 comps=$( __get_enabled_units $mode )
c317a1a1 222 compopt -o filenames
d611dadc
MB
223
224 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
e9a19bd8
ZJS
225 comps=$( __get_disabled_units $mode;
226 __get_template_names $mode)
c317a1a1 227 compopt -o filenames
d611dadc 228
c2e09812
ZJS
229 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
230 comps=$( __get_disabled_units $mode;
e9a19bd8
ZJS
231 __get_enabled_units $mode;
232 __get_template_names $mode)
c317a1a1 233 compopt -o filenames
c2e09812 234
d611dadc 235 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
9ff8af54
ZJS
236 comps=$( __get_startable_units $mode;
237 __get_template_names $mode)
c317a1a1 238 compopt -o filenames
d611dadc
MB
239
240 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
9ff8af54
ZJS
241 comps=$( __get_restartable_units $mode;
242 __get_template_names $mode)
c317a1a1 243 compopt -o filenames
d611dadc
MB
244
245 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
9e542e0b
ZJS
246 comps=$( __filter_units_by_property $mode CanStop yes \
247 $( __get_active_units $mode ) )
c317a1a1 248 compopt -o filenames
d611dadc
MB
249
250 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
9e542e0b
ZJS
251 comps=$( __filter_units_by_property $mode CanReload yes \
252 $( __get_active_units $mode ) )
c317a1a1 253 compopt -o filenames
d611dadc
MB
254
255 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
9e542e0b
ZJS
256 comps=$( __filter_units_by_property $mode AllowIsolate yes \
257 $( __get_all_units $mode ) )
c317a1a1 258 compopt -o filenames
d611dadc
MB
259
260 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
9e542e0b 261 comps=$( __get_failed_units $mode )
c317a1a1 262 compopt -o filenames
d611dadc
MB
263
264 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
9e542e0b 265 comps=$( __get_masked_units $mode )
c317a1a1 266 compopt -o filenames
d611dadc 267
8fc5cd71
LN
268 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
269 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
270 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
271 comps=$( __systemctl $mode list-unit-files --type target --all \
272 | { while read -r a b; do echo " $a"; done; } )
273 else
274 comps=$( __get_all_unit_files $mode )
275 fi
276 compopt -o filenames
277
36b4a7ba 278 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
d611dadc
MB
279 comps=''
280
281 elif __contains_word "$verb" ${VERBS[JOBS]}; then
79c16383 282 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc 283
d611dadc 284 elif __contains_word "$verb" ${VERBS[ENVS]}; then
9e542e0b 285 comps=$( __systemctl $mode show-environment \
035dd8c0 286 | while read -r line; do echo " ${line%%=*}="; done )
d611dadc
MB
287 compopt -o nospace
288
289 elif __contains_word "$verb" ${VERBS[FILE]}; then
290 comps=$( compgen -A file -- "$cur" )
291 compopt -o filenames
035dd8c0 292
99504dd4
VP
293 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
294 comps=$( __systemctl $mode list-unit-files --type target --full --all \
295 | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
296 fi
297
c317a1a1 298 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
d611dadc
MB
299 return 0
300}
301
302complete -F _systemctl systemctl