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