1 # systemctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
4 # This file is part of systemd.
6 # Copyright 2010 Ran Benita
10 systemctl $mode --full --no-legend "$@" 2>/dev/null
13 __systemd_properties() {
15 { __systemctl $mode show --all;
16 @rootlibexecdir@/systemd --dump-configuration-items; } |
17 while IFS='=' read -r key value; do
18 [[ $value ]] && echo "$key"
23 local w word=$1; shift
25 [[ $w = "$word" ]] && return
29 __filter_units_by_property () {
30 local mode=$1 property=$2 value=$3 ; shift 3
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
42 __filter_units_by_properties () {
43 local mode=$1 properties=$2 values=$3 ; shift 3
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]}" )
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
63 if (( j == ${#conditions[*]} )); then
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; }; }
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; }
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; } )
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; }; }
105 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
106 { while read a b; do echo " $a"; done; }
110 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
111 local i verb comps mode
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'
122 if __contains_word "--user" ${COMP_WORDS[*]}; then
124 elif __contains_word "--global" ${COMP_WORDS[*]}; then
130 if __contains_word "$prev" ${OPTS[ARG]}; then
137 comps=$(__systemctl $mode -t help)
140 comps=$(__systemctl $mode --state=help)
143 comps='fail replace replace-irreversibly isolate
144 ignore-dependencies ignore-requirements flush'
147 comps='all control main'
150 comps=$(compgen -A directory -- "$cur" )
154 comps=$(compgen -A hostname)
157 comps=$(__systemd_properties $mode)
160 comps='full enable-only disable-only'
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'
167 comps=$( __get_machines )
170 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
174 if [[ "$cur" = -* ]]; then
175 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
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'
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'
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]}
213 if [[ -z $verb ]]; then
216 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
217 comps=$( __get_all_units $mode )
220 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
221 comps=$( __get_enabled_units $mode )
224 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
225 comps=$( __get_disabled_units $mode;
226 __get_template_names $mode)
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)
235 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
236 comps=$( __get_startable_units $mode;
237 __get_template_names $mode)
240 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
241 comps=$( __get_restartable_units $mode;
242 __get_template_names $mode)
245 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
246 comps=$( __filter_units_by_property $mode CanStop yes \
247 $( __get_active_units $mode ) )
250 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
251 comps=$( __filter_units_by_property $mode CanReload yes \
252 $( __get_active_units $mode ) )
255 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
256 comps=$( __filter_units_by_property $mode AllowIsolate yes \
257 $( __get_all_units $mode ) )
260 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
261 comps=$( __get_failed_units $mode )
264 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
265 comps=$( __get_masked_units $mode )
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; } )
274 comps=$( __get_all_unit_files $mode )
278 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
281 elif __contains_word "$verb" ${VERBS[JOBS]}; then
282 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
284 elif __contains_word "$verb" ${VERBS[ENVS]}; then
285 comps=$( __systemctl $mode show-environment \
286 | while read -r line; do echo " ${line%%=*}="; done )
289 elif __contains_word "$verb" ${VERBS[FILE]}; then
290 comps=$( compgen -A file -- "$cur" )
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; } )
298 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
302 complete -F _systemctl systemctl