]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/busctl
Merge pull request #7844 from yuwata/bash-completion
[thirdparty/systemd.git] / shell-completion / bash / busctl
1 # busctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # Copyright 2013 Zbigniew Jędrzejewski-Szmek
7 # Copyright 2014 Thomas H.P. Andersen
8 #
9 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
22 __contains_word () {
23 local w word=$1; shift
24 for w in "$@"; do
25 [[ $w = "$word" ]] && return
26 done
27 }
28
29 __get_machines() {
30 local a b
31 machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
32 }
33
34 __get_busnames() {
35 local mode=$1
36 local a b
37 busctl $mode list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
38 }
39
40 __get_objects() {
41 local mode=$1
42 local busname=$2
43 local a b
44 busctl $mode tree --list --no-legend --no-pager $busname | { while read a b; do echo " $a"; done; };
45 }
46
47 __get_interfaces() {
48 local mode=$1
49 local busname=$2
50 local path=$3
51 local a b
52 busctl $mode introspect --list --no-legend --no-pager $busname $path | { while read a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; };
53 }
54
55 __get_members() {
56 local mode=$1
57 local busname=$2
58 local path=$3
59 local interface=$4
60 local type=$5
61 local a b
62 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c; do [[ "$b" == "$type" ]] && echo " $a"; done; };
63 }
64
65 __get_signature() {
66 local mode=$1
67 local busname=$2
68 local path=$3
69 local interface=$4
70 local member=$5
71 local a b
72 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" ]] && echo " \"$c\""; done; };
73 }
74
75 _busctl() {
76 local i verb comps mode
77 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
78 local -A OPTS=(
79 [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
80 --show-machine --unique --acquired --activatable --list
81 -q --quiet --verbose --expect-reply=no --auto-start=no
82 --allow-interactive-authorization=no --augment-creds=no
83 --watch-bind=yes'
84 [ARG]='--address -H --host -M --machine --match --timeout --size'
85 )
86
87 if __contains_word "--user" ${COMP_WORDS[*]}; then
88 mode=--user
89 else
90 mode=--system
91 fi
92
93 if __contains_word "$prev" ${OPTS[ARG]}; then
94 case $prev in
95 --host|-H)
96 comps=$(compgen -A hostname)
97 ;;
98 --machine|-M)
99 comps=$( __get_machines )
100 esac
101 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
102 return 0
103 fi
104
105 if [[ "$cur" = -* ]]; then
106 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
107 return 0
108 fi
109
110 local -A VERBS=(
111 [STANDALONE]='list help'
112 [BUSNAME]='status monitor capture tree'
113 [OBJECT]='introspect'
114 [METHOD]='call'
115 [PROPERTY_GET]='get-property'
116 [PROPERTY_SET]='set-property'
117 )
118
119 for ((i=0; i < COMP_CWORD; i++)); do
120 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
121 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
122 verb=${COMP_WORDS[i]}
123 break
124 fi
125 done
126
127 n=$(($COMP_CWORD - $i))
128
129 if [[ -z $verb ]]; then
130 comps=${VERBS[*]}
131 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
132 comps=''
133 elif __contains_word "$verb" ${VERBS[BUSNAME]}; then
134 comps=$( __get_busnames $mode)
135 elif __contains_word "$verb" ${VERBS[OBJECT]}; then
136 if [[ $n -eq 1 ]] ; then
137 comps=$( __get_busnames $mode)
138 elif [[ $n -eq 2 ]] ; then
139 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
140 elif [[ $n -eq 3 ]] ; then
141 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
142 else
143 comps=''
144 fi
145 elif __contains_word "$verb" ${VERBS[METHOD]}; then
146 if [[ $n -eq 1 ]] ; then
147 comps=$( __get_busnames $mode)
148 elif [[ $n -eq 2 ]] ; then
149 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
150 elif [[ $n -eq 3 ]] ; then
151 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
152 elif [[ $n -eq 4 ]] ; then
153 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method)
154 elif [[ $n -eq 5 ]] ; then
155 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
156 else
157 comps=''
158 fi
159 elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then
160 if [[ $n -eq 1 ]] ; then
161 comps=$( __get_busnames $mode)
162 elif [[ $n -eq 2 ]] ; then
163 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
164 elif [[ $n -eq 3 ]] ; then
165 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
166 elif [[ $n -eq 4 ]] ; then
167 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
168 else
169 comps=''
170 fi
171 elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then
172 if [[ $n -eq 1 ]] ; then
173 comps=$( __get_busnames $mode)
174 elif [[ $n -eq 2 ]] ; then
175 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
176 elif [[ $n -eq 3 ]] ; then
177 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
178 elif [[ $n -eq 4 ]] ; then
179 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
180 elif [[ $n -eq 5 ]] ; then
181 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
182 else
183 comps=''
184 fi
185 fi
186
187 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
188 return 0
189 }
190
191 complete -F _busctl busctl