]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/systemctl.in
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json
172 json-pretty json-sse json-seq cat with-unit'
173 ;;
174 --machine|-M)
175 comps=$( __get_machines )
176 ;;
177 esac
178 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
179 return 0
180 fi
181
182 if [[ "$cur" = -* ]]; then
183 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
184 return 0
185 fi
186
187 local -A VERBS=(
188 [ALL_UNITS]='cat mask'
189 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
190 [ENABLED_UNITS]='disable'
191 [DISABLED_UNITS]='enable'
192 [REENABLABLE_UNITS]='reenable'
193 [FAILED_UNITS]='reset-failed'
194 [STARTABLE_UNITS]='start'
195 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
196 [ISOLATABLE_UNITS]='isolate'
197 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
198 [RESTARTABLE_UNITS]='restart reload-or-restart'
199 [TARGET_AND_UNITS]='add-wants add-requires'
200 [MASKED_UNITS]='unmask'
201 [JOBS]='cancel'
202 [ENVS]='set-environment unset-environment import-environment'
203 [STANDALONE]='daemon-reexec daemon-reload default
204 emergency exit halt hibernate hybrid-sleep
205 suspend-then-hibernate kexec list-jobs list-sockets
206 list-timers list-units list-unit-files poweroff
207 reboot rescue show-environment suspend get-default
208 is-system-running preset-all'
209 [FILE]='link switch-root'
210 [TARGETS]='set-default'
211 [MACHINES]='list-machines'
212 )
213
214 for ((i=0; i < COMP_CWORD; i++)); do
215 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
216 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
217 verb=${COMP_WORDS[i]}
218 break
219 fi
220 done
221
222 if [[ -z $verb ]]; then
223 comps="${VERBS[*]}"
224
225 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
226 comps=$( __get_all_units $mode "$cur" )
227 compopt -o filenames
228
229 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
230 comps=$( __get_non_template_units $mode "$cur" )
231 compopt -o filenames
232
233 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
234 comps=$( __get_enabled_units $mode "$cur" )
235 compopt -o filenames
236
237 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
238 comps=$( __get_disabled_units $mode "$cur";
239 __get_template_names $mode "$cur")
240 compopt -o filenames
241
242 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
243 comps=$( __get_disabled_units $mode "$cur";
244 __get_enabled_units $mode "$cur";
245 __get_template_names $mode "$cur")
246 compopt -o filenames
247
248 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
249 comps=$( __get_startable_units $mode "$cur" )
250 compopt -o filenames
251
252 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
253 comps=$( __get_restartable_units $mode "$cur" )
254 compopt -o filenames
255
256 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
257 comps=$( __get_stoppable_units $mode "$cur" )
258 compopt -o filenames
259
260 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
261 comps=$( __get_reloadable_units $mode "$cur" )
262 compopt -o filenames
263
264 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
265 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
266 $( __get_all_units $mode "$cur" ) )
267 compopt -o filenames
268
269 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
270 comps=$( __get_failed_units $mode "$cur" )
271 compopt -o filenames
272
273 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
274 comps=$( __get_masked_units $mode "$cur" )
275 compopt -o filenames
276
277 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
278 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
279 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
280 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
281 | { while read -r a b; do echo " $a"; done; } )
282 else
283 comps=$( __get_all_unit_files $mode "$cur" )
284 fi
285 compopt -o filenames
286
287 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
288 comps=''
289
290 elif __contains_word "$verb" ${VERBS[JOBS]}; then
291 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
292
293 elif __contains_word "$verb" ${VERBS[ENVS]}; then
294 comps=$( __systemctl $mode show-environment \
295 | while read -r line; do echo " ${line%%=*}="; done )
296 compopt -o nospace
297
298 elif __contains_word "$verb" ${VERBS[FILE]}; then
299 comps=$( compgen -A file -- "$cur" )
300 compopt -o filenames
301
302 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
303 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
304 | { while read -r a b; do echo " $a"; done; } )
305 fi
306
307 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
308 return 0
309 }
310
311 complete -F _systemctl systemctl