]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/busctl
tree-wide: drop license boilerplate
[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.
5#
6# Copyright 2013 Zbigniew Jędrzejewski-Szmek
8ec76163 7# Copyright 2014 Thomas H.P. Andersen
86cb0691
ZJS
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
8ec76163
TA
29__get_machines() {
30 local a b
31 machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
32}
33
e275f5e2 34__get_busnames() {
51e430a5 35 local mode=$1
8ec76163 36 local a b
51e430a5 37 busctl $mode list --no-legend --no-pager | { while read a b; do echo " $a"; done; };
8ec76163
TA
38}
39
e275f5e2
LP
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
7f9c3eca 51 local a b c
e275f5e2
LP
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
1e58b1dc 61 local flags=$6
7f9c3eca 62 local a b c d e
1e58b1dc 63 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d e; do [[ "$b" == "$type" && ( -z $flags || "$e" == "$flags" ) ]] && echo " $a"; done; };
e275f5e2
LP
64}
65
66__get_signature() {
67 local mode=$1
68 local busname=$2
69 local path=$3
70 local interface=$4
71 local member=$5
7f9c3eca 72 local a b c d
4cbb7c50 73 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface | sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" && "$c" != '-' ]] && echo " \"$c\""; done; };
e275f5e2
LP
74}
75
86cb0691 76_busctl() {
7f9c3eca 77 local i n verb comps mode
86cb0691
ZJS
78 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
79 local -A OPTS=(
17d47d8d 80 [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
e275f5e2 81 --show-machine --unique --acquired --activatable --list
cdb8ec2d
YW
82 -q --quiet --verbose --expect-reply=no --auto-start=no
83 --allow-interactive-authorization=no --augment-creds=no
84 --watch-bind=yes'
85 [ARG]='--address -H --host -M --machine --match --timeout --size'
86cb0691
ZJS
86 )
87
51e430a5 88 if __contains_word "--user" ${COMP_WORDS[*]}; then
e275f5e2 89 mode=--user
51e430a5 90 else
e275f5e2 91 mode=--system
51e430a5
KS
92 fi
93
86cb0691
ZJS
94 if __contains_word "$prev" ${OPTS[ARG]}; then
95 case $prev in
96 --host|-H)
97 comps=$(compgen -A hostname)
98 ;;
8ec76163
TA
99 --machine|-M)
100 comps=$( __get_machines )
86cb0691
ZJS
101 esac
102 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
103 return 0
104 fi
105
106 if [[ "$cur" = -* ]]; then
107 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
108 return 0
109 fi
110
111 local -A VERBS=(
8ec76163 112 [STANDALONE]='list help'
e275f5e2
LP
113 [BUSNAME]='status monitor capture tree'
114 [OBJECT]='introspect'
115 [METHOD]='call'
116 [PROPERTY_GET]='get-property'
117 [PROPERTY_SET]='set-property'
86cb0691
ZJS
118 )
119
3ce09b7d 120 for ((i=0; i < COMP_CWORD; i++)); do
86cb0691
ZJS
121 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
122 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
123 verb=${COMP_WORDS[i]}
124 break
125 fi
126 done
127
e275f5e2
LP
128 n=$(($COMP_CWORD - $i))
129
86cb0691
ZJS
130 if [[ -z $verb ]]; then
131 comps=${VERBS[*]}
132 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
133 comps=''
e275f5e2
LP
134 elif __contains_word "$verb" ${VERBS[BUSNAME]}; then
135 comps=$( __get_busnames $mode)
136 elif __contains_word "$verb" ${VERBS[OBJECT]}; then
137 if [[ $n -eq 1 ]] ; then
138 comps=$( __get_busnames $mode)
139 elif [[ $n -eq 2 ]] ; then
140 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
141 elif [[ $n -eq 3 ]] ; then
142 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
143 else
144 comps=''
145 fi
146 elif __contains_word "$verb" ${VERBS[METHOD]}; then
147 if [[ $n -eq 1 ]] ; then
148 comps=$( __get_busnames $mode)
149 elif [[ $n -eq 2 ]] ; then
150 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
151 elif [[ $n -eq 3 ]] ; then
152 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
153 elif [[ $n -eq 4 ]] ; then
154 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method)
155 elif [[ $n -eq 5 ]] ; then
156 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
157 else
158 comps=''
159 fi
160 elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then
161 if [[ $n -eq 1 ]] ; then
162 comps=$( __get_busnames $mode)
163 elif [[ $n -eq 2 ]] ; then
164 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
165 elif [[ $n -eq 3 ]] ; then
166 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
167 elif [[ $n -eq 4 ]] ; then
168 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
169 else
170 comps=''
171 fi
172 elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then
173 if [[ $n -eq 1 ]] ; then
174 comps=$( __get_busnames $mode)
175 elif [[ $n -eq 2 ]] ; then
176 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
177 elif [[ $n -eq 3 ]] ; then
178 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
179 elif [[ $n -eq 4 ]] ; then
1e58b1dc 180 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable)
e275f5e2
LP
181 elif [[ $n -eq 5 ]] ; then
182 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
183 else
184 comps=''
185 fi
86cb0691
ZJS
186 fi
187
188 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
189 return 0
190}
191
192complete -F _busctl busctl