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