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 $mode show --all;
28 @rootlibexecdir@/systemd --dump-configuration-items; } |
29 while IFS='=' read -r key value; do
30 [[ $value ]] && echo "$key"
35 local w word=$1; shift
37 [[ $w = "$word" ]] && return
41 __filter_units_by_property () {
42 local mode=$1 property=$2 value=$3 ; shift 3
45 IFS=$'\n' read -rd '' -a props < \
46 <(__systemctl $mode show --property "$property" -- "${units[@]}")
47 for ((i=0; $i < ${#units[*]}; i++)); do
48 if [[ "${props[i]}" = "$property=$value" ]]; then
54 __get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \
55 | { while read -r a b; do echo " $a"; done; }; }
56 __get_template_names () { __systemctl $1 list-unit-files \
57 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
59 __get_active_units () { __systemctl $1 list-units \
60 | { while read -r a b; do echo " $a"; done; }; }
61 __get_startable_units () {
62 # find inactive or failed units, filter out masked and not-found
63 __systemctl $1 list-units --state inactive,failed -- $( __get_all_units ) | \
64 { while read -r a b c d; do [[ $b == "loaded" ]] && echo " $a"; done; }; }
65 __get_restartable_units () {
66 # find !masked, filter out masked and not-found
67 __systemctl $1 list-units --state active,inactive,failed -- $( __get_all_units ) | \
68 { while read -r a b c d; do [[ $b == "loaded" ]] && echo " $a"; done; }; }
69 __get_failed_units () { __systemctl $1 list-units \
70 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
71 __get_enabled_units () { __systemctl $1 list-unit-files \
72 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
73 __get_disabled_units () { __systemctl $1 list-unit-files \
74 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
75 __get_masked_units () { __systemctl $1 list-unit-files \
76 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
77 __get_all_unit_files () { { __systemctl $1 list-unit-files; } | { while read -r a b; do echo " $a"; done; }; }
80 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
81 local i verb comps mode
84 [STANDALONE]='--all -a --reverse --after --before --defaults --fail --ignore-dependencies --failed --force -f --full -l --global
85 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
86 --quiet -q --privileged -P --system --user --version --runtime --recursive -r'
87 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --root'
90 if __contains_word "--user" ${COMP_WORDS[*]}; then
96 if __contains_word "$prev" ${OPTS[ARG]}; then
99 comps=$(compgen -A signal)
102 comps='automount busname device mount path service snapshot socket swap target timer'
105 comps='loaded not-found stub
107 dead elapsed exited listening mounted plugged running waiting'
110 comps='all control main'
113 comps=$(compgen -A directory -- "$cur" )
117 comps=$(compgen -A hostname)
120 comps=$(__systemd_properties $mode)
123 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
127 if [[ "$cur" = -* ]]; then
128 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
133 [ALL_UNITS]='is-active is-failed is-enabled status show cat mask preset help list-dependencies'
134 [ENABLED_UNITS]='disable'
135 [DISABLED_UNITS]='enable'
136 [REENABLABLE_UNITS]='reenable'
137 [FAILED_UNITS]='reset-failed'
138 [STARTABLE_UNITS]='start'
139 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
140 [ISOLATABLE_UNITS]='isolate'
141 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
142 [RESTARTABLE_UNITS]='restart reload-or-restart'
143 [TARGET_AND_UNITS]='add-wants add-requires'
144 [MASKED_UNITS]='unmask'
147 [ENVS]='set-environment unset-environment'
148 [STANDALONE]='daemon-reexec daemon-reload default
149 emergency exit halt hibernate hybrid-sleep kexec list-jobs
150 list-sockets list-timers list-units list-unit-files poweroff
151 reboot rescue show-environment suspend get-default'
154 [TARGETS]='set-default'
157 for ((i=0; i < COMP_CWORD; i++)); do
158 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
159 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
160 verb=${COMP_WORDS[i]}
165 if [[ -z $verb ]]; then
168 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
169 comps=$( __get_all_units $mode )
172 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
173 comps=$( __get_enabled_units $mode )
176 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
177 comps=$( __get_disabled_units $mode;
178 __get_template_names $mode)
181 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
182 comps=$( __get_disabled_units $mode;
183 __get_enabled_units $mode;
184 __get_template_names $mode)
187 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
188 comps=$( __filter_units_by_property $mode CanStart yes \
189 $( __get_startable_units $mode);
190 __get_template_names $mode)
193 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
194 comps=$( __filter_units_by_property $mode CanStart yes \
195 $( __get_restartable_units $mode); \
196 __get_template_names $mode)
199 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
200 comps=$( __filter_units_by_property $mode CanStop yes \
201 $( __get_active_units $mode ) )
204 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
205 comps=$( __filter_units_by_property $mode CanReload yes \
206 $( __get_active_units $mode ) )
209 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
210 comps=$( __filter_units_by_property $mode AllowIsolate yes \
211 $( __get_all_units $mode ) )
214 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
215 comps=$( __get_failed_units $mode )
218 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
219 comps=$( __get_masked_units $mode )
222 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
223 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
224 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
225 comps=$( __systemctl $mode list-unit-files --type target --all \
226 | { while read -r a b; do echo " $a"; done; } )
228 comps=$( __get_all_unit_files $mode )
232 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
235 elif __contains_word "$verb" ${VERBS[JOBS]}; then
236 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
238 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
239 comps=$( __systemctl $mode list-units --type snapshot --full --all \
240 | { while read -r a b; do echo " $a"; done; } )
242 elif __contains_word "$verb" ${VERBS[ENVS]}; then
243 comps=$( __systemctl $mode show-environment \
244 | while read -r line; do echo " ${line%%=*}=";done )
247 elif __contains_word "$verb" ${VERBS[FILE]}; then
248 comps=$( compgen -A file -- "$cur" )
250 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
251 comps=$( __systemctl $mode list-unit-files --type target --full --all \
252 | { while read -r a b; do echo " $a"; done; } )
255 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur") )
259 complete -F _systemctl systemctl