]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl
bash-completion: use a better definition of __contains_word
[thirdparty/systemd.git] / shell-completion / bash / systemctl
CommitLineData
d611dadc
MB
1# systemctl(1) completion -*- shell-script -*-
2#
3# This file is part of systemd.
4#
5# Copyright 2010 Ran Benita
6#
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.
11#
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.
16#
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/>.
19
20__systemctl() {
9e542e0b
ZJS
21 local mode=$1; shift 1
22 systemctl $mode --full --no-legend "$@"
d611dadc
MB
23}
24
caffaf58
ZJS
25__systemd_properties() {
26 local mode=$1
90cf049b 27 { __systemctl -a $mode show;
caffaf58
ZJS
28 systemd --dump-configuration-items; } |
29 while IFS='=' read -r key value; do
30 [[ $value ]] && echo "$key"
31 done
32}
33
d611dadc 34__contains_word () {
a72d698d
DR
35 local w word=$1; shift
36 for w in "$@"; do
37 [[ $w = "$word" ]] && return
38 done
d611dadc
MB
39}
40
41__filter_units_by_property () {
9e542e0b 42 local mode=$1 property=$2 value=$3 ; shift 3
d611dadc
MB
43 local units=("$@")
44 local props
45 IFS=$'\n' read -rd '' -a props < \
9e542e0b 46 <(__systemctl $mode show --property "$property" -- "${units[@]}")
d611dadc
MB
47 for ((i=0; $i < ${#units[*]}; i++)); do
48 if [[ "${props[i]}" = "$property=$value" ]]; then
79c16383 49 echo " ${units[i]}"
d611dadc
MB
50 fi
51 done
52}
53
9e542e0b 54__get_all_units () { __systemctl $1 list-units --all \
79c16383 55 | { while read -r a b; do echo " $a"; done; }; }
9e542e0b 56__get_active_units () { __systemctl $1 list-units \
79c16383 57 | { while read -r a b; do echo " $a"; done; }; }
20b3f379 58__get_startable_units () { __systemctl $1 list-units --all -t service,timer,socket,mount,automount,path,snapshot,swap \
c2e09812 59 | { while read -r a b c d; do [[ $c == "inactive" || $c == "failed " ]] && echo " $a"; done; }; }
9e542e0b 60__get_failed_units () { __systemctl $1 list-units \
79c16383 61 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
9e542e0b 62__get_enabled_units () { __systemctl $1 list-unit-files \
79c16383 63 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
9e542e0b 64__get_disabled_units () { __systemctl $1 list-unit-files \
79c16383 65 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
9e542e0b 66__get_masked_units () { __systemctl $1 list-unit-files \
79c16383 67 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
d611dadc
MB
68
69_systemctl () {
70 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
9e542e0b 71 local i verb comps mode
d611dadc
MB
72
73 local -A OPTS=(
98a6e132 74 [STANDALONE]='--all -a --reverse --after --before --defaults --fail --ignore-dependencies --failed --force -f --full -l --global
d611dadc
MB
75 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
76 --quiet -q --privileged -P --system --user --version --runtime'
77 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
78 )
79
caffaf58
ZJS
80 if __contains_word "--user" ${COMP_WORDS[*]}; then
81 mode=--user
82 else
83 mode=--system
84 fi
85
d611dadc
MB
86 if __contains_word "$prev" ${OPTS[ARG]}; then
87 case $prev in
88 --signal|-s)
89 comps=$(compgen -A signal)
90 ;;
91 --type|-t)
92 comps='automount device mount path service snapshot socket swap target timer'
93 ;;
94 --kill-who)
95 comps='all control main'
96 ;;
97 --kill-mode)
98 comps='control-group process'
99 ;;
100 --root)
101 comps=$(compgen -A directory -- "$cur" )
102 compopt -o filenames
103 ;;
104 --host|-H)
105 comps=$(compgen -A hostname)
106 ;;
107 --property|-p)
caffaf58 108 comps=$(__systemd_properties $mode)
d611dadc
MB
109 ;;
110 esac
111 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
112 return 0
113 fi
114
d611dadc
MB
115 if [[ "$cur" = -* ]]; then
116 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
117 return 0
118 fi
119
120 local -A VERBS=(
afba4199 121 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset help list-dependencies'
c2e09812 122 [ENABLED_UNITS]='disable'
d611dadc 123 [DISABLED_UNITS]='enable'
c2e09812 124 [REENABLABLE_UNITS]='reenable'
d611dadc
MB
125 [FAILED_UNITS]='reset-failed'
126 [STARTABLE_UNITS]='start'
127 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
128 [ISOLATABLE_UNITS]='isolate'
129 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
130 [RESTARTABLE_UNITS]='restart reload-or-restart'
131 [MASKED_UNITS]='unmask'
132 [JOBS]='cancel'
133 [SNAPSHOTS]='delete'
134 [ENVS]='set-environment unset-environment'
135 [STANDALONE]='daemon-reexec daemon-reload default dump
136 emergency exit halt hibernate hybrid-sleep kexec list-jobs
137 list-units list-unit-files poweroff reboot rescue
99504dd4 138 show-environment suspend get-default'
d611dadc
MB
139 [NAME]='snapshot load'
140 [FILE]='link'
99504dd4 141 [TARGETS]='set-default'
d611dadc
MB
142 )
143
144 for ((i=0; $i <= $COMP_CWORD; i++)); do
145 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
146 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
147 verb=${COMP_WORDS[i]}
148 break
149 fi
150 done
151
152 if [[ -z $verb ]]; then
153 comps="${VERBS[*]}"
154
155 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
9e542e0b 156 comps=$( __get_all_units $mode )
d611dadc
MB
157
158 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
9e542e0b 159 comps=$( __get_enabled_units $mode )
d611dadc
MB
160
161 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
9e542e0b 162 comps=$( __get_disabled_units $mode )
d611dadc 163
c2e09812
ZJS
164 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
165 comps=$( __get_disabled_units $mode;
166 __get_enabled_units $mode )
167
d611dadc 168 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
9e542e0b 169 comps=$( __filter_units_by_property $mode CanStart yes \
20b3f379 170 $( __get_startable_units $mode))
d611dadc
MB
171
172 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
9e542e0b
ZJS
173 comps=$( __filter_units_by_property $mode CanStart yes \
174 $( __get_all_units $mode \
d611dadc 175 | while read -r line; do \
79c16383 176 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
d611dadc
MB
177 done ))
178
179 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
9e542e0b
ZJS
180 comps=$( __filter_units_by_property $mode CanStop yes \
181 $( __get_active_units $mode ) )
d611dadc
MB
182
183 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
9e542e0b
ZJS
184 comps=$( __filter_units_by_property $mode CanReload yes \
185 $( __get_active_units $mode ) )
d611dadc
MB
186
187 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
9e542e0b
ZJS
188 comps=$( __filter_units_by_property $mode AllowIsolate yes \
189 $( __get_all_units $mode ) )
d611dadc
MB
190
191 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
9e542e0b 192 comps=$( __get_failed_units $mode )
d611dadc
MB
193
194 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
9e542e0b 195 comps=$( __get_masked_units $mode )
d611dadc
MB
196
197 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
198 comps=''
199
200 elif __contains_word "$verb" ${VERBS[JOBS]}; then
79c16383 201 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
202
203 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
9e542e0b 204 comps=$( __systemctl $mode list-units --type snapshot --full --all \
79c16383 205 | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
206
207 elif __contains_word "$verb" ${VERBS[ENVS]}; then
9e542e0b 208 comps=$( __systemctl $mode show-environment \
79c16383 209 | while read -r line; do echo " ${line%%=*}=";done )
d611dadc
MB
210 compopt -o nospace
211
212 elif __contains_word "$verb" ${VERBS[FILE]}; then
213 comps=$( compgen -A file -- "$cur" )
214 compopt -o filenames
99504dd4
VP
215 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
216 comps=$( __systemctl $mode list-unit-files --type target --full --all \
217 | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
218 fi
219
220 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
221 return 0
222}
223
224complete -F _systemctl systemctl