]>
Commit | Line | Data |
---|---|---|
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 () { | |
843cfcb1 ZJS |
20 | local w word=$1; shift |
21 | for w in "$@"; do | |
22 | [[ $w = "$word" ]] && return | |
23 | done | |
86cb0691 ZJS |
24 | } |
25 | ||
8ec76163 | 26 | __get_machines() { |
843cfcb1 ZJS |
27 | local a b |
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() { |
843cfcb1 ZJS |
33 | local mode=$1 |
34 | local a b | |
6552c29e | 35 | COLUMNS=65535 busctl $mode list --no-legend --no-pager 2>/dev/null | |
843cfcb1 | 36 | { while read a b; do echo " $a"; done; }; |
8ec76163 TA |
37 | } |
38 | ||
e275f5e2 | 39 | __get_objects() { |
843cfcb1 ZJS |
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; }; | |
e275f5e2 LP |
45 | } |
46 | ||
47 | __get_interfaces() { | |
843cfcb1 ZJS |
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; }; | |
e275f5e2 LP |
54 | } |
55 | ||
56 | __get_members() { | |
843cfcb1 ZJS |
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; }; | |
e275f5e2 LP |
67 | } |
68 | ||
69 | __get_signature() { | |
843cfcb1 ZJS |
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; }; | |
e275f5e2 LP |
78 | } |
79 | ||
86cb0691 | 80 | _busctl() { |
843cfcb1 ZJS |
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 -j' | |
89 | [ARG]='--address -H --host -M --machine --match --timeout --size --json | |
90 | --destination' | |
91 | ) | |
92 | ||
93 | if __contains_word "--user" ${COMP_WORDS[*]}; then | |
94 | mode=--user | |
95 | else | |
96 | mode=--system | |
97 | fi | |
98 | ||
99 | if __contains_word "$prev" ${OPTS[ARG]}; then | |
100 | case $prev in | |
101 | --host|-H) | |
102 | comps=$(compgen -A hostname) | |
103 | ;; | |
104 | --machine|-M) | |
105 | comps=$( __get_machines ) | |
106 | ;; | |
107 | --json) | |
108 | comps=$( busctl --json=help 2>/dev/null ) | |
109 | ;; | |
110 | --destination) | |
111 | comps=$( __get_busnames $mode ) | |
112 | ;; | |
113 | esac | |
114 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
115 | return 0 | |
116 | fi | |
117 | ||
118 | if [[ "$cur" = -* ]]; then | |
119 | COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) | |
120 | return 0 | |
121 | fi | |
122 | ||
123 | local -A VERBS=( | |
124 | [STANDALONE]='list help' | |
125 | [BUSNAME]='status monitor capture tree' | |
126 | [OBJECT]='introspect' | |
127 | [METHOD]='call' | |
128 | [EMIT]='emit' | |
129 | [PROPERTY_GET]='get-property' | |
130 | [PROPERTY_SET]='set-property' | |
131 | ) | |
132 | ||
133 | for ((i=0; i < COMP_CWORD; i++)); do | |
134 | if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && | |
135 | ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then | |
136 | verb=${COMP_WORDS[i]} | |
137 | break | |
138 | fi | |
139 | done | |
140 | ||
141 | n=$(($COMP_CWORD - $i)) | |
142 | ||
143 | if [[ -z $verb ]]; then | |
144 | comps=${VERBS[*]} | |
145 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then | |
146 | comps='' | |
147 | elif __contains_word "$verb" ${VERBS[BUSNAME]}; then | |
148 | comps=$( __get_busnames $mode) | |
149 | elif __contains_word "$verb" ${VERBS[OBJECT]}; then | |
150 | if [[ $n -eq 1 ]] ; then | |
151 | comps=$( __get_busnames $mode) | |
152 | elif [[ $n -eq 2 ]] ; then | |
153 | comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]}) | |
154 | elif [[ $n -eq 3 ]] ; then | |
155 | comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
51e430a5 | 156 | else |
843cfcb1 | 157 | comps='' |
51e430a5 | 158 | fi |
843cfcb1 ZJS |
159 | elif __contains_word "$verb" ${VERBS[METHOD]}; then |
160 | if [[ $n -eq 1 ]] ; then | |
161 | comps=$( __get_busnames $mode) | |
162 | elif [[ $n -eq 2 ]] ; then | |
163 | comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]}) | |
164 | elif [[ $n -eq 3 ]] ; then | |
165 | comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
166 | elif [[ $n -eq 4 ]] ; then | |
167 | comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} method) | |
168 | elif [[ $n -eq 5 ]] ; then | |
169 | comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
170 | else | |
171 | comps='' | |
86cb0691 | 172 | fi |
843cfcb1 ZJS |
173 | elif __contains_word "$verb" ${VERBS[EMIT]}; then |
174 | comps='' | |
175 | elif __contains_word "$verb" ${VERBS[PROPERTY_GET]}; then | |
176 | if [[ $n -eq 1 ]] ; then | |
177 | comps=$( __get_busnames $mode) | |
178 | elif [[ $n -eq 2 ]] ; then | |
179 | comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]}) | |
180 | elif [[ $n -eq 3 ]] ; then | |
181 | comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
182 | elif [[ $n -eq 4 ]] ; then | |
183 | comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property) | |
184 | else | |
185 | comps='' | |
86cb0691 | 186 | fi |
843cfcb1 ZJS |
187 | elif __contains_word "$verb" ${VERBS[PROPERTY_SET]}; then |
188 | if [[ $n -eq 1 ]] ; then | |
189 | comps=$( __get_busnames $mode) | |
190 | elif [[ $n -eq 2 ]] ; then | |
191 | comps=$( __get_objects $mode ${COMP_WORDS[COMP_CWORD-1]}) | |
192 | elif [[ $n -eq 3 ]] ; then | |
193 | comps=$( __get_interfaces $mode ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
194 | elif [[ $n -eq 4 ]] ; then | |
195 | comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable) | |
196 | elif [[ $n -eq 5 ]] ; then | |
197 | comps=$( __get_signature $mode ${COMP_WORDS[COMP_CWORD-4]} ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]}) | |
198 | else | |
199 | comps='' | |
86cb0691 | 200 | fi |
843cfcb1 | 201 | fi |
86cb0691 | 202 | |
843cfcb1 ZJS |
203 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
204 | return 0 | |
86cb0691 ZJS |
205 | } |
206 | ||
207 | complete -F _busctl busctl |