Debian Codegraph @

shell-completion/bash/busctl

raw
1 # shellcheck shell=bash
2 # busctl(1) completion -*- shell-script -*-
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4 #
5 # This file is part of systemd.
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 <https://www.gnu.org/licenses/>.
19
20 __contains_word () {
21 local w word=$1; shift
22 for w in "$@"; do
23 [[ $w = "$word" ]] && return
24 done
25 }
26
27 __get_machines() {
28 local a b
29 { machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
30 { while read -r a b; do echo " $a"; done; } |
31 sort -u
32 }
33
34 __get_busnames() {
35 local mode=$1
36 local a b
37 busctl $mode list --no-legend --no-pager --full 2>/dev/null |
38 { while read -r a b; do echo " $a"; done; }
39 }
40
41 __get_objects() {
42 local mode=$1
43 local busname=$2
44 local a b
45 busctl $mode tree --list --no-legend --no-pager $busname 2>/dev/null |
46 { while read -r a b; do echo " $a"; done; }
47 }
48
49 __get_interfaces() {
50 local mode=$1
51 local busname=$2
52 local path=$3
53 local a b c
54 busctl $mode introspect --list --no-legend --no-pager $busname $path 2>/dev/null |
55 { while read -r a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; }
56 }
57
58 __get_members() {
59 local mode=$1
60 local busname=$2
61 local path=$3
62 local interface=$4
63 local type=$5
64 local flags=$6
65 local a b c d e
66 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
67 sed -e 's/^\.//' |
68 { while read -r a b c d e; do [[ "$b" == "$type" && ( -z $flags || "$e" == "$flags" ) ]] && echo " $a"; done; }
69 }
70
71 __get_signature() {
72 local mode=$1
73 local busname=$2
74 local path=$3
75 local interface=$4
76 local member=$5
77 local a b c d
78 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
79 sed -e 's/^\.//' | { while read -r a b c d; do [[ "$a" == "$member" && "$c" != '-' ]] && echo " \"$c\""; done; }
80 }
81
82 _busctl() {
83 local i n verb comps mode
84 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
85 local -A OPTS=(
86 [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
87 --show-machine --unique --acquired --activatable --list
88 -q --quiet --verbose --expect-reply=no --auto-start=no
89 --allow-interactive-authorization=no --augment-creds=no
90 --watch-bind=yes -j -l --full --xml-interface'
91 [ARG]='--address -H --host -M --machine --match --timeout --size --json
92 --destination -N --limit-messages'
93 )
94
95 if __contains_word "--user" ${COMP_WORDS[*]}; then
96 mode=--user
97 else
98 mode=--system
99 fi
100
101 if __contains_word "$prev" ${OPTS[ARG]}; then
102 case $prev in
103 --host|-H)
104 comps=$(compgen -A hostname)
105 ;;
106 --machine|-M)
107 comps=$( __get_machines )
108 ;;
109 --json)
110 comps=$( busctl --json=help 2>/dev/null )
111 ;;
112 --destination)
113 comps=$( __get_busnames $mode )
114 ;;
115 esac
116 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
117 return 0
118 fi
119
120 if [[ "$cur" = -* ]]; then
121 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
122 return 0
123 fi
124
125 local -A VERBS=(
126 [STANDALONE]='list help'
127 [BUSNAME]='status monitor capture tree'
128 [OBJECT]='introspect'
129 [METHOD]='call'
130 [EMIT]='emit'
131 [WAIT]='wait'
132 [PROPERTY_GET]='get-property'
133 [PROPERTY_SET]='set-property'
134 )
135
136 for ((i=0; i < COMP_CWORD; i++)); do
137 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
138 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
139 verb=${COMP_WORDS[i]}
140 break
141 fi
142 done
143
144 n=$((COMP_CWORD - i))
145
146 if [[ -z ${verb-} ]]; then
147 comps=${VERBS[*]}
148 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
149 comps=''
150 elif __contains_word "$verb" ${VERBS[BUSNAME]}; then
151 comps=$( __get_busnames $mode)
152 elif __contains_word "$verb" ${VERBS[OBJECT]}; then
153 if [[ $n -eq 1 ]] ; then
154 comps=$( __get_busnames $mode)
155 elif [[ $n -eq 2 ]] ; then
156 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
157 elif [[ $n -eq 3 ]] ; then
158 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
159 else
160 comps=''
161 fi
162 elif __contains_word "$verb" ${VERBS[METHOD]}; then
163 if [[ $n -eq 1 ]] ; then
164 comps=$( __get_busnames $mode)
165 elif [[ $n -eq 2 ]] ; then
166 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
167 elif [[ $n -eq 3 ]] ; then
168 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
169 elif [[ $n -eq 4 ]] ; then
170 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method)
171 elif [[ $n -eq 5 ]] ; then
172 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
173 else
174 comps=''
175 fi
176 elif __contains_word "$verb" ${VERBS[EMIT]}; then
177 comps=''
178 elif __contains_word "$verb" ${VERBS[WAIT]}; then
179 if [[ $n -eq 1 ]] ; then
180 comps=$( __get_busnames $mode)
181 elif [[ $n -eq 2 ]] ; then
182 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
183 elif [[ $n -eq 3 ]] ; then
184 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
185 elif [[ $n -eq 4 ]] ; then
186 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} signal)
187 else
188 comps=''
189 fi
190 elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then
191 if [[ $n -eq 1 ]] ; then
192 comps=$( __get_busnames $mode)
193 elif [[ $n -eq 2 ]] ; then
194 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
195 elif [[ $n -eq 3 ]] ; then
196 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
197 elif [[ $n -eq 4 ]] ; then
198 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
199 else
200 comps=''
201 fi
202 elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then
203 if [[ $n -eq 1 ]] ; then
204 comps=$( __get_busnames $mode)
205 elif [[ $n -eq 2 ]] ; then
206 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
207 elif [[ $n -eq 3 ]] ; then
208 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
209 elif [[ $n -eq 4 ]] ; then
210 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable)
211 elif [[ $n -eq 5 ]] ; then
212 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
213 else
214 comps=''
215 fi
216 fi
217
218 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
219 return 0
220 }
221
222 complete -F _busctl busctl