]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/busctl
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 # 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
26 __get_machines() {
27 local a b
28 machinectl list --no-legend --no-pager 2>/dev/null |
29 { while read a b; do echo " $a"; done; };
30 }
31
32 __get_busnames() {
33 local mode=$1
34 local a b
35 busctl $mode list --no-legend --no-pager 2>/dev/null |
36 { while read a b; do echo " $a"; done; };
37 }
38
39 __get_objects() {
40 local mode=$1
41 local busname=$2
42 local a b
43 busctl $mode tree --list --no-legend --no-pager $busname 2>/dev/null |
44 { 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 c
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; };
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
62 local flags=$6
63 local a b c d e
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; };
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
75 local a b c d
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; };
78 }
79
80 _busctl() {
81 local i n verb comps mode
82 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
83 local -A OPTS=(
84 [STANDALONE]='-h --help --version --no-pager --no-legend --system --user
85 --show-machine --unique --acquired --activatable --list
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'
90 )
91
92 if __contains_word "--user" ${COMP_WORDS[*]}; then
93 mode=--user
94 else
95 mode=--system
96 fi
97
98 if __contains_word "$prev" ${OPTS[ARG]}; then
99 case $prev in
100 --host|-H)
101 comps=$(compgen -A hostname)
102 ;;
103 --machine|-M)
104 comps=$( __get_machines )
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=(
116 [STANDALONE]='list help'
117 [BUSNAME]='status monitor capture tree'
118 [OBJECT]='introspect'
119 [METHOD]='call'
120 [PROPERTY_GET]='get-property'
121 [PROPERTY_SET]='set-property'
122 )
123
124 for ((i=0; i < COMP_CWORD; i++)); do
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
132 n=$(($COMP_CWORD - $i))
133
134 if [[ -z $verb ]]; then
135 comps=${VERBS[*]}
136 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
137 comps=''
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
184 comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable)
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
190 fi
191
192 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
193 return 0
194 }
195
196 complete -F _busctl busctl