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