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