]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/busctl
tree-wide: beautify remaining copyright statements
[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 2>/dev/null |
32 { while read a b; do echo " $a"; done; };
33 }
34
35 __get_busnames() {
36 local mode=$1
37 local a b
38 busctl $mode list --no-legend --no-pager 2>/dev/null |
39 { while read a b; do echo " $a"; done; };
40 }
41
42 __get_objects() {
43 local mode=$1
44 local busname=$2
45 local a b
46 busctl $mode tree --list --no-legend --no-pager $busname 2>/dev/null |
47 { while read a b; do echo " $a"; done; };
48 }
49
50 __get_interfaces() {
51 local mode=$1
52 local busname=$2
53 local path=$3
54 local a b c
55 busctl $mode introspect --list --no-legend --no-pager $busname $path 2>/dev/null |
56 { while read a b c; do [[ "$b" == "interface" ]] && echo " $a"; done; };
57 }
58
59 __get_members() {
60 local mode=$1
61 local busname=$2
62 local path=$3
63 local interface=$4
64 local type=$5
65 local flags=$6
66 local a b c d e
67 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
68 sed -e 's/^\.//' |
69 { while read a b c d e; do [[ "$b" == "$type" && ( -z $flags || "$e" == "$flags" ) ]] && echo " $a"; done; };
70 }
71
72 __get_signature() {
73 local mode=$1
74 local busname=$2
75 local path=$3
76 local interface=$4
77 local member=$5
78 local a b c d
79 busctl $mode introspect --list --no-legend --no-pager $busname $path $interface 2>/dev/null |
80 sed -e 's/^\.//' | { while read a b c d; do [[ "$a" == "$member" && "$c" != '-' ]] && echo " \"$c\""; done; };
81 }
82
83 _busctl() {
84 local i n verb comps mode
85 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
86 local -A OPTS=(
87 [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
88 --show-machine --unique --acquired --activatable --list
89 -q --quiet --verbose --expect-reply=no --auto-start=no
90 --allow-interactive-authorization=no --augment-creds=no
91 --watch-bind=yes'
92 [ARG]='--address -H --host -M --machine --match --timeout --size'
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 esac
109 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
110 return 0
111 fi
112
113 if [[ "$cur" = -* ]]; then
114 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
115 return 0
116 fi
117
118 local -A VERBS=(
119 [STANDALONE]='list help'
120 [BUSNAME]='status monitor capture tree'
121 [OBJECT]='introspect'
122 [METHOD]='call'
123 [PROPERTY_GET]='get-property'
124 [PROPERTY_SET]='set-property'
125 )
126
127 for ((i=0; i < COMP_CWORD; i++)); do
128 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
129 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
130 verb=${COMP_WORDS[i]}
131 break
132 fi
133 done
134
135 n=$(($COMP_CWORD - $i))
136
137 if [[ -z $verb ]]; then
138 comps=${VERBS[*]}
139 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
140 comps=''
141 elif __contains_word "$verb" ${VERBS[BUSNAME]}; then
142 comps=$( __get_busnames $mode)
143 elif __contains_word "$verb" ${VERBS[OBJECT]}; then
144 if [[ $n -eq 1 ]] ; then
145 comps=$( __get_busnames $mode)
146 elif [[ $n -eq 2 ]] ; then
147 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
148 elif [[ $n -eq 3 ]] ; then
149 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
150 else
151 comps=''
152 fi
153 elif __contains_word "$verb" ${VERBS[METHOD]}; then
154 if [[ $n -eq 1 ]] ; then
155 comps=$( __get_busnames $mode)
156 elif [[ $n -eq 2 ]] ; then
157 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
158 elif [[ $n -eq 3 ]] ; then
159 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
160 elif [[ $n -eq 4 ]] ; then
161 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method)
162 elif [[ $n -eq 5 ]] ; then
163 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
164 else
165 comps=''
166 fi
167 elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then
168 if [[ $n -eq 1 ]] ; then
169 comps=$( __get_busnames $mode)
170 elif [[ $n -eq 2 ]] ; then
171 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
172 elif [[ $n -eq 3 ]] ; then
173 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
174 elif [[ $n -eq 4 ]] ; then
175 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property)
176 else
177 comps=''
178 fi
179 elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then
180 if [[ $n -eq 1 ]] ; then
181 comps=$( __get_busnames $mode)
182 elif [[ $n -eq 2 ]] ; then
183 comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]})
184 elif [[ $n -eq 3 ]] ; then
185 comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
186 elif [[ $n -eq 4 ]] ; then
187 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable)
188 elif [[ $n -eq 5 ]] ; then
189 comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]})
190 else
191 comps=''
192 fi
193 fi
194
195 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
196 return 0
197 }
198
199 complete -F _busctl busctl