Bash completion: ignore namespace for xmllint parsing
[lttng-tools.git] / extras / lttng-bash_completion
1 #
2 # Copyright (c) - 2012 Simon Marchi <simon.marchi@polymtl.ca>
3 #
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by as published by
6 # the Free Software Foundation; only version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 # more details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17
18 # Generates COMPREPLY with the existing session names
19 _lttng_complete_sessions() {
20 local sessions
21 sessions=$(lttng --mi xml list | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/sessions/session/name" - 2>/dev/null | sed -e 's/<name>//g' -e $'s/<\/name>/\\n/g')
22 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
23 return
24 }
25 #
26
27 # Generates COMPREPLY with the available kernel event
28 _lttng_complete_kernel_events() {
29 local kernel_event
30 kernel_event=$(lttng --mi xml list -k |sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'KERNEL']/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
31 COMPREPLY=( $(compgen -W "${kernel_event}" -- $cur) )
32 return
33 }
34
35 # Generates COMPREPLY with the available ust event
36 _lttng_complete_ust_events() {
37 local ust_event
38 ust_event=$(lttng --mi xml list -u | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'UST']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
39 COMPREPLY=( $(compgen -W "${ust_event}" -- $cur) )
40 return
41 }
42
43 # Generates COMPREPLY with the available jul event
44 _lttng_complete_jul_events() {
45 local jul_event
46 jul_event=$(lttng --mi xml list -j | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'JUL']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
47 COMPREPLY=( $(compgen -W "${jul_event}" -- $cur) )
48 return
49 }
50
51
52
53 # Generates COMPREPLY with whatever is in the $options variable.
54 _lttng_complete_options() {
55 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
56 }
57
58 # Generates COMPREPLY with whatever is in the $commands variable.
59 _lttng_complete_commands() {
60 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
61 }
62
63 _lttng_cmd_addcontext() {
64 options=$(lttng add-context --list-options)
65
66 case $prev in
67 --session|-s)
68 _lttng_complete_sessions
69 return
70 ;;
71 --channel|-c)
72 return
73 ;;
74 --type|-t)
75 return
76 ;;
77 esac
78
79 case $cur in
80 -*)
81 _lttng_complete_options
82 return
83 ;;
84 esac
85 }
86
87 _lttng_cmd_create() {
88 options=$(lttng create --list-options)
89
90 case $prev in
91 --output|-o)
92 _filedir -d
93 return
94 ;;
95 esac
96
97 case $cur in
98 -*)
99 _lttng_complete_options
100 return
101 ;;
102 esac
103 }
104
105 _lttng_cmd_destroy() {
106 options=$(lttng destroy --list-options)
107
108 case $cur in
109 -*)
110 _lttng_complete_options
111 return
112 ;;
113 *)
114 _lttng_complete_sessions
115 return
116 ;;
117 esac
118 }
119 _lttng_cmd_disablechannel() {
120 options=$(lttng disable-channel --list-options)
121
122 case $prev in
123 --session|-s)
124 _lttng_complete_sessions
125 return
126 ;;
127 esac
128
129 case $cur in
130 -*)
131 _lttng_complete_options
132 return
133 ;;
134 esac
135 }
136 _lttng_cmd_disableevent() {
137 options=$(lttng disable-event --list-options)
138
139 case $prev in
140 --session|-s)
141 _lttng_complete_sessions
142 return
143 ;;
144 --channel|-c)
145 return
146 ;;
147 esac
148
149 case $cur in
150 -*)
151 _lttng_complete_options
152 return
153 ;;
154 esac
155 }
156
157 _lttng_cmd_enablechannel() {
158 options=$(lttng enable-channel --list-options)
159
160 case $prev in
161 --session|-s)
162 _lttng_complete_sessions
163 return
164 ;;
165 esac
166
167 case $cur in
168 -*)
169 _lttng_complete_options
170 return
171 ;;
172 esac
173 }
174
175 _lttng_cmd_enableevent() {
176 options=$(lttng enable-event --list-options)
177
178 case $prev in
179 --session|-s)
180 _lttng_complete_sessions
181 return
182 ;;
183 --channel|-c)
184 return
185 ;;
186 --probe)
187 return
188 ;;
189 --function)
190 return
191 ;;
192 esac
193
194
195 #Check if we want kernel event completion
196 if [[ "$COMP_LINE" == *"-k"* ]]; then
197 _lttng_complete_kernel_events
198 return
199 fi
200
201 #Check if we want ust event completion
202 if [[ "$COMP_LINE" == *"-u"* ]]; then
203 _lttng_complete_ust_events
204 return
205 fi
206
207 #Check if we want jul event completion
208 if [[ "$COMP_LINE" == *"-j"* ]]; then
209 _lttng_complete_jul_events
210 return
211 fi
212
213 case $cur in
214 -*)
215 _lttng_complete_options
216 return
217 ;;
218 esac
219
220 }
221
222 _lttng_cmd_list() {
223 options=$(lttng list --list-options)
224
225 case $prev in
226 --channel|-c)
227 return
228 ;;
229 esac
230
231 case $cur in
232 -*)
233 _lttng_complete_options
234 return
235 ;;
236 *)
237 _lttng_complete_sessions
238 return
239 esac
240 }
241
242 _lttng_cmd_setsession() {
243 options=$(lttng set-session --list-options)
244
245 case $cur in
246 -*)
247 _lttng_complete_options
248 return
249 ;;
250 *)
251 _lttng_complete_sessions
252 return
253 ;;
254 esac
255 }
256
257 _lttng_cmd_snapshot() {
258 options=$(lttng snapshot --list-options)
259 commands=$(lttng snapshot --list-commands)
260
261 _lttng_find_command $((command_found_index + 1))
262
263 if _lttng_cursor_is_after_command; then
264 case $prev in
265 --session|-s)
266 _lttng_complete_sessions
267 return
268 ;;
269 esac
270
271 case $cur in
272 -*)
273 _lttng_complete_options
274 ;;
275 esac
276 else
277 _lttng_complete_commands
278 fi
279 }
280
281 _lttng_cmd_start() {
282 options=$(lttng start --list-options)
283
284 case $cur in
285 -*)
286 _lttng_complete_options
287 return
288 ;;
289 *)
290 _lttng_complete_sessions
291 return
292 ;;
293 esac
294 }
295
296 _lttng_cmd_stop() {
297 options=$(lttng stop --list-options)
298
299 case $cur in
300 -*)
301 _lttng_complete_options
302 ;;
303 *)
304 _lttng_complete_sessions
305 ;;
306 esac
307 }
308
309 _lttng_cmd_version() {
310 options=$(lttng version --list-options)
311
312 case $cur in
313 -*)
314 _lttng_complete_options
315 ;;
316 esac
317 }
318
319 _lttng_cmd_view() {
320 options=$(lttng view --list-options)
321
322 case $cur in
323 -*)
324 _lttng_complete_options
325 ;;
326 esac
327 }
328
329
330
331 _lttng_before_command() {
332 # Check if the previous word should alter the behavior
333 case $prev in
334 --group|-g)
335 COMPREPLY=( $(compgen -g -- $cur) )
336 return
337 ;;
338 --sessiond-path)
339 _filedir
340 return
341 ;;
342 esac
343
344 case $cur in
345 -*)
346 # If the current word starts with a dash, complete with options
347 _lttng_complete_options
348 ;;
349 *)
350 # Otherwise complete with commands
351 _lttng_complete_commands
352 ;;
353 esac
354 }
355
356 _lttng_after_command() {
357 local cmd_name
358
359 cmd_name=_lttng_cmd_${command_found//-/}
360
361 type -t $cmd_name | grep -q 'function' && $cmd_name
362 }
363
364 # Check if the word passed as the first parameter corresponds to a
365 # command. $command must be set to the list of possible commands.
366 _lttng_is_command() {
367 for command in $commands; do
368 if [ "$1" == "$command" ]; then
369 return 0
370 fi
371 done
372
373 return 1
374 }
375
376 # Try to find a command in the current command line. Possible commands
377 # are passed in $commands.
378 #
379 # This function takes an optional parameter that indicates the index
380 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
381 #
382 # If a command is found, $command_found is filled with the name of the
383 # command and $command_found_index is set to the index of the command in
384 # $COMP_WORDS. If no command is found, $command_found is an empty string
385 # and $command_found_index is set to -1.
386 _lttng_find_command() {
387 start=${1:-1}
388
389 # The text of the found command
390 command_found=""
391
392 # The index of the found command in COMP_WORDS
393 command_found_index=-1
394
395 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
396 _lttng_is_command ${COMP_WORDS[$i]}
397 if [ $? -eq 0 ]; then
398 command_found=${COMP_WORDS[$i]}
399 command_found_index=$i
400 break
401 fi
402 done
403 }
404
405 _lttng_cursor_is_after_command() {
406 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
407 }
408
409 _lttng() {
410 local cur prev commands command_found command_found_index
411
412 # Get the current and previous word
413 _get_comp_words_by_ref cur prev
414
415 # Get the valid first-level LTTng commands and options
416 commands=$(lttng --list-commands)
417 options=$(lttng --list-options)
418
419 _lttng_find_command
420
421 # Check if the cursor is before or after the command keyword
422 if _lttng_cursor_is_after_command; then
423 _lttng_after_command
424 else
425 _lttng_before_command
426 fi
427 }
428
429 complete -F _lttng lttng
This page took 0.038128 seconds and 5 git commands to generate.