f5677e9bcef70f532867b490d6927bd805db6139
[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 # TODO
21 # This code does nothing for now. When there is a mecanism to get the
22 # existing sessions, use it to fill the sessions variable.
23 local sessions
24 sessions=""
25 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
26 return
27 }
28
29 # Generates COMPREPLY with whatever is in the $options variable.
30 _lttng_complete_options() {
31 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
32 }
33
34 # Generates COMPREPLY with whatever is in the $commands variable.
35 _lttng_complete_commands() {
36 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
37 }
38
39 _lttng_cmd_addcontext() {
40 options=$(lttng add-context --list-options)
41
42 case $prev in
43 --session|-s)
44 _lttng_complete_sessions
45 return
46 ;;
47 --channel|-c)
48 return
49 ;;
50 --type|-t)
51 return
52 ;;
53 esac
54
55 case $cur in
56 -*)
57 _lttng_complete_options
58 return
59 ;;
60 esac
61 }
62
63 _lttng_cmd_calibrate() {
64 options=$(lttng calibrate --list-options)
65
66 case $cur in
67 -*)
68 _lttng_complete_options
69 return
70 ;;
71 esac
72 }
73
74 _lttng_cmd_create() {
75 options=$(lttng create --list-options)
76
77 case $prev in
78 --output|-o)
79 _filedir -d
80 return
81 ;;
82 esac
83
84 case $cur in
85 -*)
86 _lttng_complete_options
87 return
88 ;;
89 esac
90 }
91
92 _lttng_cmd_destroy() {
93 options=$(lttng destroy --list-options)
94
95 case $cur in
96 -*)
97 _lttng_complete_options
98 return
99 ;;
100 *)
101 _lttng_complete_sessions
102 return
103 ;;
104 esac
105 }
106 _lttng_cmd_disablechannel() {
107 options=$(lttng disable-channel --list-options)
108
109 case $prev in
110 --session|-s)
111 _lttng_complete_sessions
112 return
113 ;;
114 esac
115
116 case $cur in
117 -*)
118 _lttng_complete_options
119 return
120 ;;
121 esac
122 }
123 _lttng_cmd_disableevent() {
124 options=$(lttng disable-event --list-options)
125
126 case $prev in
127 --session|-s)
128 _lttng_complete_sessions
129 return
130 ;;
131 --channel|-c)
132 return
133 ;;
134 esac
135
136 case $cur in
137 -*)
138 _lttng_complete_options
139 return
140 ;;
141 esac
142 }
143
144 _lttng_cmd_enablechannel() {
145 options=$(lttng enable-channel --list-options)
146
147 case $prev in
148 --session|-s)
149 _lttng_complete_sessions
150 return
151 ;;
152 esac
153
154 case $cur in
155 -*)
156 _lttng_complete_options
157 return
158 ;;
159 esac
160 }
161
162 _lttng_cmd_enableevent() {
163 options=$(lttng enable-event --list-options)
164
165 case $prev in
166 --session|-s)
167 _lttng_complete_sessions
168 return
169 ;;
170 --channel|-c)
171 return
172 ;;
173 --probe)
174 return
175 ;;
176 --function)
177 return
178 ;;
179 esac
180
181 case $cur in
182 -*)
183 _lttng_complete_options
184 return
185 ;;
186 esac
187 }
188
189 _lttng_cmd_list() {
190 options=$(lttng list --list-options)
191
192 case $prev in
193 --channel|-c)
194 return
195 ;;
196 esac
197
198 case $cur in
199 -*)
200 _lttng_complete_options
201 return
202 ;;
203 *)
204 _lttng_complete_sessions
205 return
206 esac
207 }
208
209 _lttng_cmd_setsession() {
210 options=$(lttng set-session --list-options)
211
212 case $cur in
213 -*)
214 _lttng_complete_options
215 return
216 ;;
217 *)
218 _lttng_complete_sessions
219 return
220 ;;
221 esac
222 }
223
224 _lttng_cmd_snapshot() {
225 options=$(lttng snapshot --list-options)
226 commands=$(lttng snapshot --list-commands)
227
228 _lttng_find_command $((command_found_index + 1))
229
230 if _lttng_cursor_is_after_command; then
231 case $prev in
232 --session|-s)
233 _lttng_complete_sessions
234 return
235 ;;
236 esac
237
238 case $cur in
239 -*)
240 _lttng_complete_options
241 ;;
242 esac
243 else
244 _lttng_complete_commands
245 fi
246 }
247
248 _lttng_cmd_start() {
249 options=$(lttng start --list-options)
250
251 case $cur in
252 -*)
253 _lttng_complete_options
254 return
255 ;;
256 *)
257 _lttng_complete_sessions
258 return
259 ;;
260 esac
261 }
262
263 _lttng_cmd_stop() {
264 options=$(lttng stop --list-options)
265
266 case $cur in
267 -*)
268 _lttng_complete_options
269 ;;
270 *)
271 _lttng_complete_sessions
272 ;;
273 esac
274 }
275
276 _lttng_cmd_version() {
277 options=$(lttng version --list-options)
278
279 case $cur in
280 -*)
281 _lttng_complete_options
282 ;;
283 esac
284 }
285
286 _lttng_cmd_view() {
287 options=$(lttng view --list-options)
288
289 case $cur in
290 -*)
291 _lttng_complete_options
292 ;;
293 esac
294 }
295
296
297
298 _lttng_before_command() {
299 # Check if the previous word should alter the behavior
300 case $prev in
301 --group|-g)
302 COMPREPLY=( $(compgen -g -- $cur) )
303 return
304 ;;
305 --sessiond-path)
306 _filedir
307 return
308 ;;
309 esac
310
311 case $cur in
312 -*)
313 # If the current word starts with a dash, complete with options
314 _lttng_complete_options
315 ;;
316 *)
317 # Otherwise complete with commands
318 _lttng_complete_commands
319 ;;
320 esac
321 }
322
323 _lttng_after_command() {
324 local cmd_name
325
326 cmd_name=_lttng_cmd_${command_found//-/}
327
328 type -t $cmd_name | grep -q 'function' && $cmd_name
329 }
330
331 # Check if the word passed as the first parameter corresponds to a
332 # command. $command must be set to the list of possible commands.
333 _lttng_is_command() {
334 for command in $commands; do
335 if [ "$1" == "$command" ]; then
336 return 0
337 fi
338 done
339
340 return 1
341 }
342
343 # Try to find a command in the current command line. Possible commands
344 # are passed in $commands.
345 #
346 # This function takes an optional parameter that indicates the index
347 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
348 #
349 # If a command is found, $command_found is filled with the name of the
350 # command and $command_found_index is set to the index of the command in
351 # $COMP_WORDS. If no command is found, $command_found is an empty string
352 # and $command_found_index is set to -1.
353 _lttng_find_command() {
354 start=${1:-1}
355
356 # The text of the found command
357 command_found=""
358
359 # The index of the found command in COMP_WORDS
360 command_found_index=-1
361
362 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
363 _lttng_is_command ${COMP_WORDS[$i]}
364 if [ $? -eq 0 ]; then
365 command_found=${COMP_WORDS[$i]}
366 command_found_index=$i
367 break
368 fi
369 done
370 }
371
372 _lttng_cursor_is_after_command() {
373 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
374 }
375
376 _lttng() {
377 local cur prev commands command_found command_found_index
378
379 # Get the current and previous word
380 _get_comp_words_by_ref cur prev
381
382 # Get the valid first-level LTTng commands and options
383 commands=$(lttng --list-commands)
384 options=$(lttng --list-options)
385
386 _lttng_find_command
387
388 # Check if the cursor is before or after the command keyword
389 if _lttng_cursor_is_after_command; then
390 _lttng_after_command
391 else
392 _lttng_before_command
393 fi
394 }
395
396 complete -F _lttng lttng
This page took 0.036862 seconds and 4 git commands to generate.