]>
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. | |
5 | # | |
96b2fb93 LP |
6 | # Copyright © 2013 Zbigniew Jędrzejewski-Szmek |
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 | |
0887b054 ZJS |
31 | machinectl list --no-legend --no-pager 2>/dev/null | |
32 | { while read a b; do echo " $a"; done; }; | |
8ec76163 TA |
33 | } |
34 | ||
e275f5e2 | 35 | __get_busnames() { |
51e430a5 | 36 | local mode=$1 |
8ec76163 | 37 | local a b |
0887b054 ZJS |
38 | busctl $mode list --no-legend --no-pager 2>/dev/null | |
39 | { while read a b; do echo " $a"; done; }; | |
8ec76163 TA |
40 | } |
41 | ||
e275f5e2 LP |
42 | __get_objects() { |
43 | local mode=$1 | |
44 | local busname=$2 | |
45 | local a b | |
0887b054 ZJS |
46 | busctl $mode tree --list --no-legend --no-pager $busname 2>/dev/null | |
47 | { while read a b; do echo " $a"; done; }; | |
e275f5e2 LP |
48 | } |
49 | ||
50 | __get_interfaces() { | |
51 | local mode=$1 | |
52 | local busname=$2 | |
53 | local path=$3 | |
7f9c3eca | 54 | local a b c |
0887b054 ZJS |
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; }; | |
e275f5e2 LP |
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 | |
1e58b1dc | 65 | local flags=$6 |
7f9c3eca | 66 | local a b c d e |
0887b054 ZJS |
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; }; | |
e275f5e2 LP |
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 | |
7f9c3eca | 78 | local a b c d |
0887b054 ZJS |
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; }; | |
e275f5e2 LP |
81 | } |
82 | ||
86cb0691 | 83 | _busctl() { |
7f9c3eca | 84 | local i n verb comps mode |
86cb0691 ZJS |
85 | local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
86 | local -A OPTS=( | |
17d47d8d | 87 | [STANDALONE]='-h --help --version --no-pager --no-legend --system --user |
e275f5e2 | 88 | --show-machine --unique --acquired --activatable --list |
cdb8ec2d YW |
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' | |
86cb0691 ZJS |
93 | ) |
94 | ||
51e430a5 | 95 | if __contains_word "--user" ${COMP_WORDS[*]}; then |
e275f5e2 | 96 | mode=--user |
51e430a5 | 97 | else |
e275f5e2 | 98 | mode=--system |
51e430a5 KS |
99 | fi |
100 | ||
86cb0691 ZJS |
101 | if __contains_word "$prev" ${OPTS[ARG]}; then |
102 | case $prev in | |
103 | --host|-H) | |
104 | comps=$(compgen -A hostname) | |
105 | ;; | |
8ec76163 TA |
106 | --machine|-M) |
107 | comps=$( __get_machines ) | |
86cb0691 ZJS |
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=( | |
8ec76163 | 119 | [STANDALONE]='list help' |
e275f5e2 LP |
120 | [BUSNAME]='status monitor capture tree' |
121 | [OBJECT]='introspect' | |
122 | [METHOD]='call' | |
123 | [PROPERTY_GET]='get-property' | |
124 | [PROPERTY_SET]='set-property' | |
86cb0691 ZJS |
125 | ) |
126 | ||
3ce09b7d | 127 | for ((i=0; i < COMP_CWORD; i++)); do |
86cb0691 ZJS |
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 | ||
e275f5e2 LP |
135 | n=$(($COMP_CWORD - $i)) |
136 | ||
86cb0691 ZJS |
137 | if [[ -z $verb ]]; then |
138 | comps=${VERBS[*]} | |
139 | elif __contains_word "$verb" ${VERBS[STANDALONE]}; then | |
140 | comps='' | |
e275f5e2 LP |
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 | |
1e58b1dc | 187 | comps=$( __get_members $mode ${COMP_WORDS[COMP_CWORD-3]} ${COMP_WORDS[COMP_CWORD-2]} ${COMP_WORDS[COMP_CWORD-1]} property writable) |
e275f5e2 LP |
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 | |
86cb0691 ZJS |
193 | fi |
194 | ||
195 | COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) | |
196 | return 0 | |
197 | } | |
198 | ||
199 | complete -F _busctl busctl |