]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl.in
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / shell-completion / bash / systemctl.in
CommitLineData
d611dadc 1# systemctl(1) completion -*- shell-script -*-
db9ecf05 2# SPDX-License-Identifier: LGPL-2.1-or-later
d611dadc
MB
3#
4# This file is part of systemd.
5#
96b2fb93 6# Copyright © 2010 Ran Benita
d611dadc
MB
7
8__systemctl() {
843cfcb1 9 local mode=$1; shift 1
1cabd2d0 10 systemctl $mode --full --no-legend --no-pager --plain "$@" 2>/dev/null
d611dadc
MB
11}
12
caffaf58 13__systemd_properties() {
843cfcb1 14 @rootlibexecdir@/systemd --dump-bus-properties
caffaf58
ZJS
15}
16
d611dadc 17__contains_word () {
843cfcb1
ZJS
18 local w word=$1; shift
19 for w in "$@"; do
20 [[ $w = "$word" ]] && return
21 done
d611dadc
MB
22}
23
b1bdb649 24__filter_units_by_properties () {
843cfcb1
ZJS
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
f28255e2 38 echo $names
843cfcb1
ZJS
39 fi
40 names=
41 count=0
42 else
43 (( count++ ))
44 if [[ ${props[i]%%=*} == 'Names' ]]; then
45 names=${props[i]#*=}
46 fi
f28255e2 47 fi
843cfcb1
ZJS
48 done
49 if (( count == n )) && [[ -n $names ]]; then
50 echo $names
51 fi
b1bdb649
YW
52}
53
98476dc8 54__get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 55 | { while read -r a b; do echo " $a"; done; }; }
98476dc8 56__get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
843cfcb1 57 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
c839b729 58__get_template_names () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 59 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
c839b729 60__get_active_units () { __systemctl $1 list-units "$2*" \
843cfcb1 61 | { while read -r a b; do echo " $a"; done; }; }
f28255e2
YW
62
63__get_not_masked_unit_files() {
843cfcb1
ZJS
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; }
f28255e2
YW
67}
68
f29c77bc 69__get_startable_units () {
843cfcb1
ZJS
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 )
9ff8af54 76}
f29c77bc 77__get_restartable_units () {
843cfcb1
ZJS
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 )
f28255e2
YW
83}
84
85__get_stoppable_units () {
843cfcb1
ZJS
86 # filter out masked and not-found
87 local units=$(
88 { __get_not_masked_unit_files $1 $2
89 __get_active_units $1 $2
90 } | sort -u )
91 __filter_units_by_properties $1 ActiveState=active,CanStop=yes $units
92 __filter_units_by_properties $1 ActiveState=reloading,CanStop=yes $units
93 __filter_units_by_properties $1 ActiveState=activating,CanStop=yes $units
9ff8af54 94}
f28255e2
YW
95
96__get_reloadable_units () {
843cfcb1
ZJS
97 # filter out masked and not-found
98 __filter_units_by_properties $1 ActiveState=active,CanReload=yes $(
99 { __get_not_masked_unit_files $1 $2
100 __get_active_units $1 $2
101 } | sort -u )
f28255e2
YW
102}
103
c839b729 104__get_failed_units () { __systemctl $1 list-units "$2*" \
843cfcb1 105 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
c839b729 106__get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 107 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
c839b729 108__get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 109 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
c839b729 110__get_masked_units () { __systemctl $1 list-unit-files "$2*" \
843cfcb1 111 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
98476dc8 112__get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; }
d611dadc 113
3a221b5d 114__get_machines() {
843cfcb1 115 local a b
e2268fa4 116 { machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; } | \
9521d558 117 { while read a b; do echo " $a"; done; }
3a221b5d
EV
118}
119
d611dadc 120_systemctl () {
843cfcb1 121 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
cec82cb9 122 local i verb comps mode cur_orig
d611dadc 123
843cfcb1
ZJS
124 local -A OPTS=(
125 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
0067c7b2 126 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
035dd8c0 127 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
4327574f 128 --show-types --plain --failed --value --fail --dry-run --wait'
843cfcb1 129 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
4327574f 130 --preset-mode -n --lines -o --output -M --machine --message --timestamp --check-inhibitors'
843cfcb1
ZJS
131 )
132
133 if __contains_word "--user" ${COMP_WORDS[*]}; then
134 mode=--user
135 elif __contains_word "--global" ${COMP_WORDS[*]}; then
136 mode=--user
137 else
138 mode=--system
139 fi
140
141 if __contains_word "$prev" ${OPTS[ARG]}; then
142 case $prev in
143 --signal|-s)
144 _signals
145 return
146 ;;
147 --type|-t)
148 comps=$(__systemctl $mode -t help)
149 ;;
150 --state)
151 comps=$(__systemctl $mode --state=help)
152 ;;
153 --job-mode)
154 comps='fail replace replace-irreversibly isolate
903e7c37 155 ignore-dependencies ignore-requirements flush'
843cfcb1
ZJS
156 ;;
157 --kill-who)
158 comps='all control main'
159 ;;
160 --root)
161 comps=$(compgen -A directory -- "$cur" )
162 compopt -o filenames
163 ;;
164 --host|-H)
165 comps=$(compgen -A hostname)
166 ;;
167 --property|-p)
168 comps=$(__systemd_properties)
169 ;;
170 --preset-mode)
171 comps='full enable-only disable-only'
172 ;;
173 --output|-o)
174 comps=$( systemctl --output=help 2>/dev/null )
175 ;;
176 --machine|-M)
177 comps=$( __get_machines )
178 ;;
46ad9c53
LB
179 --timestamp)
180 comps='pretty us µs utc us+utc µs+utc'
181 ;;
4327574f
FS
182 --check-inhibitors)
183 comps='auto yes no'
184 ;;
843cfcb1
ZJS
185 esac
186 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
187 return 0
188 fi
d611dadc 189
843cfcb1
ZJS
190 if [[ "$cur" = -* ]]; then
191 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
192 return 0
193 fi
d611dadc 194
843cfcb1
ZJS
195 local -A VERBS=(
196 [ALL_UNITS]='cat mask'
aea447c9 197 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
843cfcb1
ZJS
198 [ENABLED_UNITS]='disable'
199 [DISABLED_UNITS]='enable'
c2e09812 200 [REENABLABLE_UNITS]='reenable'
843cfcb1
ZJS
201 [FAILED_UNITS]='reset-failed'
202 [STARTABLE_UNITS]='start'
203 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
204 [ISOLATABLE_UNITS]='isolate'
205 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
d611dadc 206 [RESTARTABLE_UNITS]='restart reload-or-restart'
843cfcb1
ZJS
207 [TARGET_AND_UNITS]='add-wants add-requires'
208 [MASKED_UNITS]='unmask'
209 [JOBS]='cancel'
210 [ENVS]='set-environment unset-environment import-environment'
211 [STANDALONE]='daemon-reexec daemon-reload default
c58493c0 212 emergency exit halt hibernate hybrid-sleep
e68c79db 213 suspend-then-hibernate kexec list-jobs list-sockets
c58493c0 214 list-timers list-units list-unit-files poweroff
1cf3c30c 215 reboot rescue show-environment suspend get-default
035dd8c0 216 is-system-running preset-all'
6faecbd3 217 [FILE]='link switch-root bind mount-image'
843cfcb1
ZJS
218 [TARGETS]='set-default'
219 [MACHINES]='list-machines'
4171837b
YW
220 [LOG_LEVEL]='log-level'
221 [LOG_TARGET]='log-target'
222 [SERVICE_WATCHDOGS]='service-watchdogs'
843cfcb1
ZJS
223 )
224
225 for ((i=0; i < COMP_CWORD; i++)); do
226 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
227 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
228 verb=${COMP_WORDS[i]}
229 break
230 fi
231 done
d611dadc 232
72c9177d 233 # When trying to match a unit name with certain special characters in its name (i.e
cec82cb9
FS
234 # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible
235 # match to fail.
236 # The following condition solves two cases:
237 # 1) We're trying to complete an already escaped unit name part,
238 # i.e foo\\x2dba. In this case we need to unescape the name, so it
239 # gets properly matched with the systemctl output (i.e. foo\x2dba).
240 # However, we need to keep the original escaped name as well for the
241 # final match, as the completion machinery does the unescaping
242 # automagically.
243 # 2) We're trying to complete an unescaped (literal) unit name part,
244 # i.e. foo\x2dba. That means we don't have to do the unescaping
245 # required for correct matching with systemctl's output, however,
246 # we need to escape the name for the final match, where the completion
247 # expects the string to be escaped.
248 cur_orig=$cur
249 if [[ $cur =~ '\\' ]]; then
250 cur="$(echo $cur | xargs echo)"
251 else
252 cur_orig="$(printf '%q' $cur)"
253 fi
72c9177d 254
843cfcb1
ZJS
255 if [[ -z $verb ]]; then
256 comps="${VERBS[*]}"
aea447c9 257
843cfcb1
ZJS
258 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
259 comps=$( __get_all_units $mode "$cur" )
260 compopt -o filenames
d611dadc 261
843cfcb1
ZJS
262 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
263 comps=$( __get_non_template_units $mode "$cur" )
264 compopt -o filenames
d611dadc 265
843cfcb1
ZJS
266 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
267 comps=$( __get_enabled_units $mode "$cur" )
268 compopt -o filenames
c2e09812 269
843cfcb1
ZJS
270 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
271 comps=$( __get_disabled_units $mode "$cur";
272 __get_template_names $mode "$cur")
273 compopt -o filenames
d611dadc 274
843cfcb1
ZJS
275 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
276 comps=$( __get_disabled_units $mode "$cur";
277 __get_enabled_units $mode "$cur";
278 __get_template_names $mode "$cur")
279 compopt -o filenames
d611dadc 280
843cfcb1
ZJS
281 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
282 comps=$( __get_startable_units $mode "$cur" )
283 compopt -o filenames
d611dadc 284
843cfcb1
ZJS
285 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
286 comps=$( __get_restartable_units $mode "$cur" )
287 compopt -o filenames
d611dadc 288
843cfcb1
ZJS
289 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
290 comps=$( __get_stoppable_units $mode "$cur" )
291 compopt -o filenames
d611dadc 292
843cfcb1
ZJS
293 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
294 comps=$( __get_reloadable_units $mode "$cur" )
295 compopt -o filenames
d611dadc 296
843cfcb1
ZJS
297 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
298 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
babf4f68 299 $( __get_non_template_units $mode "$cur" ) )
843cfcb1
ZJS
300 compopt -o filenames
301
302 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
303 comps=$( __get_failed_units $mode "$cur" )
304 compopt -o filenames
d611dadc 305
843cfcb1
ZJS
306 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
307 comps=$( __get_masked_units $mode "$cur" )
308 compopt -o filenames
309
310 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
311 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
8fc5cd71 312 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
843cfcb1
ZJS
313 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
314 | { while read -r a b; do echo " $a"; done; } )
315 else
316 comps=$( __get_all_unit_files $mode "$cur" )
317 fi
318 compopt -o filenames
8fc5cd71 319
843cfcb1
ZJS
320 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
321 comps=''
d611dadc 322
843cfcb1
ZJS
323 elif __contains_word "$verb" ${VERBS[JOBS]}; then
324 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc 325
34199208
ZJS
326 elif [ "$verb" = 'unset-environment' ]; then
327 comps=$( __systemctl $mode show-environment \
328 | while read -r line; do echo " ${line%%=*}"; done )
329 compopt -o nospace
330
331 elif [ "$verb" = 'set-environment' ]; then
843cfcb1
ZJS
332 comps=$( __systemctl $mode show-environment \
333 | while read -r line; do echo " ${line%%=*}="; done )
334 compopt -o nospace
d611dadc 335
34199208
ZJS
336 elif [ "$verb" = 'import-environment' ]; then
337 COMPREPLY=( $(compgen -A variable -- "$cur_orig") )
338 return 0
339
843cfcb1
ZJS
340 elif __contains_word "$verb" ${VERBS[FILE]}; then
341 comps=$( compgen -A file -- "$cur" )
342 compopt -o filenames
035dd8c0 343
843cfcb1
ZJS
344 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
345 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
346 | { while read -r a b; do echo " $a"; done; } )
4171837b
YW
347 elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then
348 comps='debug info notice warning err crit alert emerg'
349 elif __contains_word "$verb" ${VERBS[LOG_TARGET]}; then
350 comps='console journal kmsg journal-or-kmsg null'
351 elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then
352 comps='on off'
843cfcb1 353 fi
d611dadc 354
72c9177d 355 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
843cfcb1 356 return 0
d611dadc
MB
357}
358
359complete -F _systemctl systemctl