chiark / gitweb /
shell-completion: replace printf with echo
[elogind.git] / shell-completion / bash / systemctl
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() {
21         local mode=$1; shift 1
22         systemctl $mode --full --no-legend "$@"
23 }
24
25 __contains_word () {
26         local word=$1; shift
27         for w in $*; do [[ $w = $word ]] && return 0; done
28         return 1
29 }
30
31 __filter_units_by_property () {
32         local mode=$1 property=$2 value=$3 ; shift 3
33         local units=("$@")
34         local props
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                         echo " ${units[i]}"
40                 fi
41         done
42 }
43
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" ]] && 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; }; }
58
59 _systemctl () {
60         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
61         local i verb comps mode
62
63         local -A OPTS=(
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'
68         )
69
70         if __contains_word "$prev" ${OPTS[ARG]}; then
71                 case $prev in
72                         --signal|-s)
73                                 comps=$(compgen -A signal)
74                         ;;
75                         --type|-t)
76                                 comps='automount device mount path service snapshot socket swap target timer'
77                         ;;
78                         --kill-who)
79                                 comps='all control main'
80                         ;;
81                         --kill-mode)
82                                 comps='control-group process'
83                         ;;
84                         --root)
85                                 comps=$(compgen -A directory -- "$cur" )
86                                 compopt -o filenames
87                         ;;
88                         --host|-H)
89                                 comps=$(compgen -A hostname)
90                         ;;
91                         --property|-p)
92                                 comps=''
93                         ;;
94                 esac
95                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
96                 return 0
97         fi
98
99         if [[ "$cur" = -* ]]; then
100                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
101                 return 0
102         fi
103
104         if __contains_word "--user" ${COMP_WORDS[*]}; then
105             mode=--user
106         else
107             mode=--system
108         fi
109
110         local -A VERBS=(
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'
121                      [JOBS]='cancel'
122                 [SNAPSHOTS]='delete'
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'
129                      [FILE]='link'
130         )
131
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]}
136                         break
137                 fi
138         done
139
140         if   [[ -z $verb ]]; then
141                 comps="${VERBS[*]}"
142
143         elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
144                 comps=$( __get_all_units $mode )
145
146         elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
147                 comps=$( __get_enabled_units $mode )
148
149         elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
150                 comps=$( __get_disabled_units $mode )
151
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)$ ]] || echo " $line"; \
157                         done ))
158
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)$ ]] || echo " $line"; \
164                         done ))
165
166         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
167                 comps=$( __filter_units_by_property $mode CanStop yes \
168                       $( __get_active_units $mode ) )
169
170         elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
171                 comps=$( __filter_units_by_property $mode CanReload yes \
172                       $( __get_active_units $mode ) )
173
174         elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
175                 comps=$( __filter_units_by_property $mode AllowIsolate yes \
176                       $( __get_all_units $mode ) )
177
178         elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
179                 comps=$( __get_failed_units $mode )
180
181         elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
182                 comps=$( __get_masked_units $mode )
183
184         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
185                 comps=''
186
187         elif __contains_word "$verb" ${VERBS[JOBS]}; then
188                 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
189
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 echo " $a"; done; } )
193
194         elif __contains_word "$verb" ${VERBS[ENVS]}; then
195                 comps=$( __systemctl $mode show-environment \
196                     | while read -r line; do echo " ${line%%=*}=";done )
197                 compopt -o nospace
198
199         elif __contains_word "$verb" ${VERBS[FILE]}; then
200                 comps=$( compgen -A file -- "$cur" )
201                 compopt -o filenames
202         fi
203
204         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
205         return 0
206 }
207
208 complete -F _systemctl systemctl