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 systemctl --full --no-legend "$@"
26 for w in $*; do [[ $w = $word ]] && return 0; done
30 __filter_units_by_property () {
31 local property=$1 value=$2 ; shift 2
34 IFS=$'\n' read -rd '' -a props < \
35 <(__systemctl show --property "$property" -- "${units[@]}")
36 for ((i=0; $i < ${#units[*]}; i++)); do
37 if [[ "${props[i]}" = "$property=$value" ]]; then
38 printf "%s\n" "${units[i]}"
43 __get_all_units () { __systemctl list-units --all \
44 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
45 __get_active_units () { __systemctl list-units \
46 | { while read -r a b; do printf "%s\n" "$a"; done; }; }
47 __get_inactive_units () { __systemctl list-units --all \
48 | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
49 __get_failed_units () { __systemctl list-units \
50 | { while read -r a b c d; do [[ $c == "failed" ]] && printf "%s\n" "$a"; done; }; }
51 __get_enabled_units () { __systemctl list-unit-files \
52 | { while read -r a b c ; do [[ $b == "enabled" ]] && printf "%s\n" "$a"; done; }; }
53 __get_disabled_units () { __systemctl list-unit-files \
54 | { while read -r a b c ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
55 __get_masked_units () { __systemctl list-unit-files \
56 | { while read -r a b c ; do [[ $b == "masked" ]] && printf "%s\n" "$a"; done; }; }
59 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
63 [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
64 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
65 --quiet -q --privileged -P --system --user --version --runtime'
66 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
69 if __contains_word "$prev" ${OPTS[ARG]}; then
72 comps=$(compgen -A signal)
75 comps='automount device mount path service snapshot socket swap target timer'
78 comps='all control main'
81 comps='control-group process'
84 comps=$(compgen -A directory -- "$cur" )
88 comps=$(compgen -A hostname)
94 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
99 if [[ "$cur" = -* ]]; then
100 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
105 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset'
106 [ENABLED_UNITS]='disable reenable'
107 [DISABLED_UNITS]='enable'
108 [FAILED_UNITS]='reset-failed'
109 [STARTABLE_UNITS]='start'
110 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
111 [ISOLATABLE_UNITS]='isolate'
112 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
113 [RESTARTABLE_UNITS]='restart reload-or-restart'
114 [MASKED_UNITS]='unmask'
117 [ENVS]='set-environment unset-environment'
118 [STANDALONE]='daemon-reexec daemon-reload default dump
119 emergency exit halt hibernate hybrid-sleep kexec list-jobs
120 list-units list-unit-files poweroff reboot rescue
121 show-environment suspend'
122 [NAME]='snapshot load'
126 for ((i=0; $i <= $COMP_CWORD; i++)); do
127 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
128 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
129 verb=${COMP_WORDS[i]}
134 if [[ -z $verb ]]; then
137 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
138 comps=$( __get_all_units )
140 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
141 comps=$( __get_enabled_units )
143 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
144 comps=$( __get_disabled_units )
146 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
147 comps=$( __filter_units_by_property CanStart yes \
148 $( __get_inactive_units \
149 | while read -r line; do \
150 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
153 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
154 comps=$( __filter_units_by_property CanStart yes \
156 | while read -r line; do \
157 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
160 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
161 comps=$( __filter_units_by_property CanStop yes \
162 $( __get_active_units ) )
164 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
165 comps=$( __filter_units_by_property CanReload yes \
166 $( __get_active_units ) )
168 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
169 comps=$( __filter_units_by_property AllowIsolate yes \
170 $( __get_all_units ) )
172 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
173 comps=$( __get_failed_units )
175 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
176 comps=$( __get_masked_units )
178 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
181 elif __contains_word "$verb" ${VERBS[JOBS]}; then
182 comps=$( __systemctl list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
184 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
185 comps=$( __systemctl list-units --type snapshot --full --all \
186 | { while read -r a b; do printf "%s\n" "$a"; done; } )
188 elif __contains_word "$verb" ${VERBS[ENVS]}; then
189 comps=$( __systemctl show-environment \
190 | while read -r line; do printf "%s\n" "${line%%=*}=";done )
193 elif __contains_word "$verb" ${VERBS[FILE]}; then
194 comps=$( compgen -A file -- "$cur" )
198 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
202 complete -F _systemctl systemctl