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 --no-pager "$@" 2>/dev/null
13 __systemd_properties() {
14 @rootlibexecdir@/systemd --dump-bus-properties
18 local w word=$1; shift
20 [[ $w = "$word" ]] && return
24 __filter_units_by_property () {
25 local mode=$1 property=$2 value=$3 ; shift 3
28 IFS=$'\n' read -rd '' -a props < \
29 <(__systemctl $mode show --property "$property" -- "${units[@]}")
30 for ((i=0; $i < ${#units[*]}; i++)); do
31 if [[ "${props[i]}" = "$property=$value" ]]; then
37 __filter_units_by_properties () {
38 local mode=$1 properties=$2 values=$3 ; shift 3
40 local props i j conditions=()
41 IFS=$'\n' read -rd '' -a props < \
42 <(__systemctl $mode show --property "$properties" -- "${units[@]}")
43 IFS=$',' read -r -a properties < <(echo $properties)
44 IFS=$',' read -r -a values < <(echo $values)
45 for ((i=0; i < ${#properties[*]}; i++)); do
46 for ((j=0; j < ${#properties[*]}; j++)); do
47 if [[ ${props[i]%%=*} == ${properties[j]} ]]; then
48 conditions+=( "${properties[j]}=${values[j]}" )
52 for ((i=0; i < ${#units[*]}; i++)); do
53 for ((j=0; j < ${#conditions[*]}; j++)); do
54 if [[ "${props[i * ${#conditions[*]} + j]}" != "${conditions[j]}" ]]; then
58 if (( j == ${#conditions[*]} )); then
64 __get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \
65 | { while read -r a b; do echo " $a"; done; }; }
66 __get_non_template_units() { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \
67 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
68 __get_template_names () { __systemctl $1 list-unit-files "$2*" \
69 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
71 __get_active_units () { __systemctl $1 list-units "$2*" \
72 | { while read -r a b; do echo " $a"; done; }; }
73 __get_startable_units () {
74 # find startable inactive units
75 __filter_units_by_properties $1 ActiveState,CanStart inactive,yes $(
76 { __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \
77 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
78 __systemctl $1 list-units --state inactive,failed "$2*" | \
79 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
82 __get_restartable_units () {
83 # filter out masked and not-found
84 __filter_units_by_property $1 CanStart yes $(
85 __systemctl $1 list-unit-files --state enabled,disabled,static "$2*" | \
86 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
87 __systemctl $1 list-units "$2*" | \
88 { while read -r a b; do echo " $a"; done; } )
90 __get_failed_units () { __systemctl $1 list-units "$2*" \
91 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
92 __get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
93 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
94 __get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
95 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
96 __get_masked_units () { __systemctl $1 list-unit-files "$2*" \
97 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
98 __get_all_unit_files () { { __systemctl $1 list-unit-files; } | { while read -r a b; do echo " $a"; done; }; }
102 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
103 { while read a b; do echo " $a"; done; }
107 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
108 local i verb comps mode
111 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
112 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
113 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
114 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
115 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
116 --preset-mode -n --lines -o --output -M --machine --message'
119 if __contains_word "--user" ${COMP_WORDS[*]}; then
121 elif __contains_word "--global" ${COMP_WORDS[*]}; then
127 if __contains_word "$prev" ${OPTS[ARG]}; then
134 comps=$(__systemctl $mode -t help)
137 comps=$(__systemctl $mode --state=help)
140 comps='fail replace replace-irreversibly isolate
141 ignore-dependencies ignore-requirements flush'
144 comps='all control main'
147 comps=$(compgen -A directory -- "$cur" )
151 comps=$(compgen -A hostname)
154 comps=$(__systemd_properties)
157 comps='full enable-only disable-only'
160 comps='short short-full short-iso short-iso-precise short-precise short-monotonic short-unix verbose export json
161 json-pretty json-sse cat'
164 comps=$( __get_machines )
167 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
171 if [[ "$cur" = -* ]]; then
172 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
177 [ALL_UNITS]='cat mask'
178 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
179 [ENABLED_UNITS]='disable'
180 [DISABLED_UNITS]='enable'
181 [REENABLABLE_UNITS]='reenable'
182 [FAILED_UNITS]='reset-failed'
183 [STARTABLE_UNITS]='start'
184 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
185 [ISOLATABLE_UNITS]='isolate'
186 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
187 [RESTARTABLE_UNITS]='restart reload-or-restart'
188 [TARGET_AND_UNITS]='add-wants add-requires'
189 [MASKED_UNITS]='unmask'
191 [ENVS]='set-environment unset-environment import-environment'
192 [STANDALONE]='daemon-reexec daemon-reload default
193 emergency exit halt hibernate hybrid-sleep
194 suspend-then-hibernate kexec list-jobs list-sockets
195 list-timers list-units list-unit-files poweroff
196 reboot rescue show-environment suspend get-default
197 is-system-running preset-all'
198 [FILE]='link switch-root'
199 [TARGETS]='set-default'
200 [MACHINES]='list-machines'
203 for ((i=0; i < COMP_CWORD; i++)); do
204 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
205 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
206 verb=${COMP_WORDS[i]}
211 if [[ -z $verb ]]; then
214 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
215 comps=$( __get_all_units $mode )
218 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
219 comps=$( __get_non_template_units $mode )
222 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
223 comps=$( __get_enabled_units $mode "$cur" )
226 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
227 comps=$( __get_disabled_units $mode "$cur";
228 __get_template_names $mode "$cur")
231 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
232 comps=$( __get_disabled_units $mode "$cur";
233 __get_enabled_units $mode "$cur";
234 __get_template_names $mode "$cur")
237 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
238 comps=$( __get_startable_units $mode "$cur";
239 __get_template_names $mode "$cur")
242 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
243 comps=$( __get_restartable_units $mode "$cur";
244 __get_template_names $mode "$cur")
247 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
248 comps=$( __filter_units_by_property $mode CanStop yes \
249 $( __get_active_units $mode "$cur" ) )
252 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
253 comps=$( __filter_units_by_property $mode CanReload yes \
254 $( __get_active_units $mode "$cur" ) )
257 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
258 comps=$( __filter_units_by_property $mode AllowIsolate yes \
259 $( __get_all_units $mode ) )
262 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
263 comps=$( __get_failed_units $mode "$cur" )
266 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
267 comps=$( __get_masked_units $mode "$cur" )
270 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
271 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
272 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
273 comps=$( __systemctl $mode list-unit-files --type target --all \
274 | { while read -r a b; do echo " $a"; done; } )
276 comps=$( __get_all_unit_files $mode )
280 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
283 elif __contains_word "$verb" ${VERBS[JOBS]}; then
284 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
286 elif __contains_word "$verb" ${VERBS[ENVS]}; then
287 comps=$( __systemctl $mode show-environment \
288 | while read -r line; do echo " ${line%%=*}="; done )
291 elif __contains_word "$verb" ${VERBS[FILE]}; then
292 comps=$( compgen -A file -- "$cur" )
295 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
296 comps=$( __systemctl $mode list-unit-files --type target --full --all \
297 | { while read -r a b; do echo " $a"; done; } )
300 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
304 complete -F _systemctl systemctl