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 "$@"
27 for w in $*; do [[ $w = $word ]] && return 0; done
31 __filter_units_by_property () {
32 local mode=$1 property=$2 value=$3 ; shift 3
35 IFS=$'\n' read -rd '' -a props < \
36 <(__systemctl $mode show --property "$property" -- "${units[@]}")
37 for ((i=0; $i < ${#units[*]}; i++)); do
38 if [[ "${props[i]}" = "$property=$value" ]]; then
44 __get_all_units () { __systemctl $1 list-units --all \
45 | { while read -r a b; do echo " $a"; done; }; }
46 __get_active_units () { __systemctl $1 list-units \
47 | { while read -r a b; do echo " $a"; done; }; }
48 __get_inactive_units () { __systemctl $1 list-units --all \
49 | { while read -r a b c d; do [[ $c == "inactive" || $c == "failed " ]] && echo " $a"; done; }; }
50 __get_failed_units () { __systemctl $1 list-units \
51 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
52 __get_enabled_units () { __systemctl $1 list-unit-files \
53 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
54 __get_disabled_units () { __systemctl $1 list-unit-files \
55 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
56 __get_masked_units () { __systemctl $1 list-unit-files \
57 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
60 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
61 local i verb comps mode
64 [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
65 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
66 --quiet -q --privileged -P --system --user --version --runtime'
67 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
70 if __contains_word "$prev" ${OPTS[ARG]}; then
73 comps=$(compgen -A signal)
76 comps='automount device mount path service snapshot socket swap target timer'
79 comps='all control main'
82 comps='control-group process'
85 comps=$(compgen -A directory -- "$cur" )
89 comps=$(compgen -A hostname)
95 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
99 if [[ "$cur" = -* ]]; then
100 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
104 if __contains_word "--user" ${COMP_WORDS[*]}; then
111 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset'
112 [ENABLED_UNITS]='disable'
113 [DISABLED_UNITS]='enable'
114 [REENABLABLE_UNITS]='reenable'
115 [FAILED_UNITS]='reset-failed'
116 [STARTABLE_UNITS]='start'
117 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
118 [ISOLATABLE_UNITS]='isolate'
119 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
120 [RESTARTABLE_UNITS]='restart reload-or-restart'
121 [MASKED_UNITS]='unmask'
124 [ENVS]='set-environment unset-environment'
125 [STANDALONE]='daemon-reexec daemon-reload default dump
126 emergency exit halt hibernate hybrid-sleep kexec list-jobs
127 list-units list-unit-files poweroff reboot rescue
128 show-environment suspend'
129 [NAME]='snapshot load'
133 for ((i=0; $i <= $COMP_CWORD; i++)); do
134 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
135 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
136 verb=${COMP_WORDS[i]}
141 if [[ -z $verb ]]; then
144 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
145 comps=$( __get_all_units $mode )
147 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
148 comps=$( __get_enabled_units $mode )
150 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
151 comps=$( __get_disabled_units $mode )
153 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
154 comps=$( __get_disabled_units $mode;
155 __get_enabled_units $mode )
157 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
158 comps=$( __filter_units_by_property $mode CanStart yes \
159 $( __get_inactive_units $mode \
160 | while read -r line; do \
161 [[ "$line" =~ \.(device|snapshot)$ ]] || echo " $line"; \
164 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
165 comps=$( __filter_units_by_property $mode CanStart yes \
166 $( __get_all_units $mode \
167 | while read -r line; do \
168 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
171 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
172 comps=$( __filter_units_by_property $mode CanStop yes \
173 $( __get_active_units $mode ) )
175 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
176 comps=$( __filter_units_by_property $mode CanReload yes \
177 $( __get_active_units $mode ) )
179 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
180 comps=$( __filter_units_by_property $mode AllowIsolate yes \
181 $( __get_all_units $mode ) )
183 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
184 comps=$( __get_failed_units $mode )
186 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
187 comps=$( __get_masked_units $mode )
189 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
192 elif __contains_word "$verb" ${VERBS[JOBS]}; then
193 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
195 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
196 comps=$( __systemctl $mode list-units --type snapshot --full --all \
197 | { while read -r a b; do echo " $a"; done; } )
199 elif __contains_word "$verb" ${VERBS[ENVS]}; then
200 comps=$( __systemctl $mode show-environment \
201 | while read -r line; do echo " ${line%%=*}=";done )
204 elif __contains_word "$verb" ${VERBS[FILE]}; then
205 comps=$( compgen -A file -- "$cur" )
209 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
213 complete -F _systemctl systemctl