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
39 printf "%s\n" "${units[i]}"
44 __get_all_units () { __systemctl $1 list-units --all \
45 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
46 __get_active_units () { __systemctl $1 list-units \
47 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
48 __get_inactive_units () { __systemctl $1 list-units --all \
49 | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
50 __get_failed_units () { __systemctl $1 list-units \
51 | { while read -r a b c d; do [[ $c == "failed" ]] && printf "%s\n" "$a"; done; }; }
52 __get_enabled_units () { __systemctl $1 list-unit-files \
53 | { while read -r a b c ; do [[ $b == "enabled" ]] && printf "%s\n" "$a"; done; }; }
54 __get_disabled_units () { __systemctl $1 list-unit-files \
55 | { while read -r a b c ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
56 __get_masked_units () { __systemctl $1 list-unit-files \
57 | { while read -r a b c ; do [[ $b == "masked" ]] && printf "%s\n" "$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 reenable'
113 [DISABLED_UNITS]='enable'
114 [FAILED_UNITS]='reset-failed'
115 [STARTABLE_UNITS]='start'
116 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
117 [ISOLATABLE_UNITS]='isolate'
118 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
119 [RESTARTABLE_UNITS]='restart reload-or-restart'
120 [MASKED_UNITS]='unmask'
123 [ENVS]='set-environment unset-environment'
124 [STANDALONE]='daemon-reexec daemon-reload default dump
125 emergency exit halt hibernate hybrid-sleep kexec list-jobs
126 list-units list-unit-files poweroff reboot rescue
127 show-environment suspend'
128 [NAME]='snapshot load'
132 for ((i=0; $i <= $COMP_CWORD; i++)); do
133 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
134 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
135 verb=${COMP_WORDS[i]}
140 if [[ -z $verb ]]; then
143 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
144 comps=$( __get_all_units $mode )
146 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
147 comps=$( __get_enabled_units $mode )
149 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
150 comps=$( __get_disabled_units $mode )
152 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
153 comps=$( __filter_units_by_property $mode CanStart yes \
154 $( __get_inactive_units $mode \
155 | while read -r line; do \
156 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
159 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
160 comps=$( __filter_units_by_property $mode CanStart yes \
161 $( __get_all_units $mode \
162 | while read -r line; do \
163 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
166 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
167 comps=$( __filter_units_by_property $mode CanStop yes \
168 $( __get_active_units $mode ) )
170 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
171 comps=$( __filter_units_by_property $mode CanReload yes \
172 $( __get_active_units $mode ) )
174 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
175 comps=$( __filter_units_by_property $mode AllowIsolate yes \
176 $( __get_all_units $mode ) )
178 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
179 comps=$( __get_failed_units $mode )
181 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
182 comps=$( __get_masked_units $mode )
184 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
187 elif __contains_word "$verb" ${VERBS[JOBS]}; then
188 comps=$( __systemctl $mode list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
190 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
191 comps=$( __systemctl $mode list-units --type snapshot --full --all \
192 | { while read -r a b; do printf "%s\n" "$a"; done; } )
194 elif __contains_word "$verb" ${VERBS[ENVS]}; then
195 comps=$( __systemctl $mode show-environment \
196 | while read -r line; do printf "%s\n" "${line%%=*}=";done )
199 elif __contains_word "$verb" ${VERBS[FILE]}; then
200 comps=$( compgen -A file -- "$cur" )
204 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
208 complete -F _systemctl systemctl