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