1 # systemctl(1) completion -*- shell-script -*-
3 # This file is part of systemd.
5 # Copyright 2010 Ran Benita
7 # systemd is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
12 # systemd is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 local mode=$1; shift 1
22 systemctl $mode --full --no-legend "$@"
25 __systemd_properties() {
27 { __systemctl -a $mode show;
28 systemd --dump-configuration-items; } |
29 while IFS='=' read -r key value; do
30 [[ $value ]] && echo "$key"
36 for w in $*; do [[ $w = $word ]] && return 0; done
40 __filter_units_by_property () {
41 local mode=$1 property=$2 value=$3 ; shift 3
44 IFS=$'\n' read -rd '' -a props < \
45 <(__systemctl $mode show --property "$property" -- "${units[@]}")
46 for ((i=0; $i < ${#units[*]}; i++)); do
47 if [[ "${props[i]}" = "$property=$value" ]]; then
53 __get_all_units () { __systemctl $1 list-units --all \
54 | { while read -r a b; do echo " $a"; done; }; }
55 __get_active_units () { __systemctl $1 list-units \
56 | { while read -r a b; do echo " $a"; done; }; }
57 __get_startable_units () { __systemctl $1 list-units --all -t service,timer,socket,mount,automount,path,snapshot,swap \
58 | { while read -r a b c d; do [[ $c == "inactive" || $c == "failed " ]] && echo " $a"; done; }; }
59 __get_failed_units () { __systemctl $1 list-units \
60 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
61 __get_enabled_units () { __systemctl $1 list-unit-files \
62 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
63 __get_disabled_units () { __systemctl $1 list-unit-files \
64 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
65 __get_masked_units () { __systemctl $1 list-unit-files \
66 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
69 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
70 local i verb comps mode
73 [STANDALONE]='--all -a --reverse --after --before --defaults --fail --ignore-dependencies --failed --force -f --full -l --global
74 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
75 --quiet -q --privileged -P --system --user --version --runtime'
76 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
79 if __contains_word "--user" ${COMP_WORDS[*]}; then
85 if __contains_word "$prev" ${OPTS[ARG]}; then
88 comps=$(compgen -A signal)
91 comps='automount device mount path service snapshot socket swap target timer'
94 comps='all control main'
97 comps='control-group process'
100 comps=$(compgen -A directory -- "$cur" )
104 comps=$(compgen -A hostname)
107 comps=$(__systemd_properties $mode)
110 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
114 if [[ "$cur" = -* ]]; then
115 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
120 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset help list-dependencies'
121 [ENABLED_UNITS]='disable'
122 [DISABLED_UNITS]='enable'
123 [REENABLABLE_UNITS]='reenable'
124 [FAILED_UNITS]='reset-failed'
125 [STARTABLE_UNITS]='start'
126 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
127 [ISOLATABLE_UNITS]='isolate'
128 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
129 [RESTARTABLE_UNITS]='restart reload-or-restart'
130 [MASKED_UNITS]='unmask'
133 [ENVS]='set-environment unset-environment'
134 [STANDALONE]='daemon-reexec daemon-reload default dump
135 emergency exit halt hibernate hybrid-sleep kexec list-jobs
136 list-units list-unit-files poweroff reboot rescue
137 show-environment suspend get-default'
138 [NAME]='snapshot load'
140 [TARGETS]='set-default'
143 for ((i=0; $i <= $COMP_CWORD; i++)); do
144 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
145 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
146 verb=${COMP_WORDS[i]}
151 if [[ -z $verb ]]; then
154 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
155 comps=$( __get_all_units $mode )
157 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
158 comps=$( __get_enabled_units $mode )
160 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
161 comps=$( __get_disabled_units $mode )
163 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
164 comps=$( __get_disabled_units $mode;
165 __get_enabled_units $mode )
167 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
168 comps=$( __filter_units_by_property $mode CanStart yes \
169 $( __get_startable_units $mode))
171 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
172 comps=$( __filter_units_by_property $mode CanStart yes \
173 $( __get_all_units $mode \
174 | while read -r line; do \
175 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
178 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
179 comps=$( __filter_units_by_property $mode CanStop yes \
180 $( __get_active_units $mode ) )
182 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
183 comps=$( __filter_units_by_property $mode CanReload yes \
184 $( __get_active_units $mode ) )
186 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
187 comps=$( __filter_units_by_property $mode AllowIsolate yes \
188 $( __get_all_units $mode ) )
190 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
191 comps=$( __get_failed_units $mode )
193 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
194 comps=$( __get_masked_units $mode )
196 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
199 elif __contains_word "$verb" ${VERBS[JOBS]}; then
200 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
202 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
203 comps=$( __systemctl $mode list-units --type snapshot --full --all \
204 | { while read -r a b; do echo " $a"; done; } )
206 elif __contains_word "$verb" ${VERBS[ENVS]}; then
207 comps=$( __systemctl $mode show-environment \
208 | while read -r line; do echo " ${line%%=*}=";done )
211 elif __contains_word "$verb" ${VERBS[FILE]}; then
212 comps=$( compgen -A file -- "$cur" )
214 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
215 comps=$( __systemctl $mode list-unit-files --type target --full --all \
216 | { while read -r a b; do echo " $a"; done; } )
219 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
223 complete -F _systemctl systemctl