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