Commit | Line | Data |
---|---|---|
fc256d99 DG |
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 | ||
ba28686d | 18 | # Generates COMPREPLY with the existing session names |
fc256d99 | 19 | _lttng_complete_sessions() { |
4a096a5b SM |
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) ) | |
fc256d99 DG |
26 | return |
27 | } | |
28 | ||
ba28686d SM |
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 | ||
6c806062 | 39 | _lttng_cmd_addcontext() { |
ba28686d | 40 | options=$(lttng add-context --list-options) |
fc256d99 DG |
41 | |
42 | case $prev in | |
43 | --session|-s) | |
44 | _lttng_complete_sessions | |
45 | return | |
46 | ;; | |
47 | --channel|-c) | |
48 | return | |
49 | ;; | |
fc256d99 DG |
50 | --type|-t) |
51 | return | |
52 | ;; | |
53 | esac | |
54 | ||
55 | case $cur in | |
56 | -*) | |
ba28686d | 57 | _lttng_complete_options |
fc256d99 DG |
58 | return |
59 | ;; | |
60 | esac | |
61 | } | |
62 | ||
07bd6635 | 63 | _lttng_cmd_calibrate() { |
ba28686d | 64 | options=$(lttng calibrate --list-options) |
07bd6635 SM |
65 | |
66 | case $cur in | |
67 | -*) | |
ba28686d SM |
68 | _lttng_complete_options |
69 | return | |
07bd6635 SM |
70 | ;; |
71 | esac | |
72 | } | |
73 | ||
fc256d99 | 74 | _lttng_cmd_create() { |
ba28686d | 75 | options=$(lttng create --list-options) |
fc256d99 DG |
76 | |
77 | case $prev in | |
78 | --output|-o) | |
79 | _filedir -d | |
80 | return | |
81 | ;; | |
82 | esac | |
83 | ||
84 | case $cur in | |
85 | -*) | |
ba28686d | 86 | _lttng_complete_options |
fc256d99 DG |
87 | return |
88 | ;; | |
89 | esac | |
90 | } | |
91 | ||
92 | _lttng_cmd_destroy() { | |
ba28686d | 93 | options=$(lttng destroy --list-options) |
fc256d99 DG |
94 | |
95 | case $cur in | |
96 | -*) | |
ba28686d SM |
97 | _lttng_complete_options |
98 | return | |
fc256d99 DG |
99 | ;; |
100 | *) | |
101 | _lttng_complete_sessions | |
ba28686d | 102 | return |
fc256d99 DG |
103 | ;; |
104 | esac | |
105 | } | |
07bd6635 | 106 | _lttng_cmd_disablechannel() { |
ba28686d | 107 | options=$(lttng disable-channel --list-options) |
fc256d99 DG |
108 | |
109 | case $prev in | |
110 | --session|-s) | |
111 | _lttng_complete_sessions | |
112 | return | |
113 | ;; | |
114 | esac | |
115 | ||
116 | case $cur in | |
117 | -*) | |
ba28686d | 118 | _lttng_complete_options |
fc256d99 DG |
119 | return |
120 | ;; | |
121 | esac | |
122 | } | |
07bd6635 | 123 | _lttng_cmd_disableevent() { |
ba28686d | 124 | options=$(lttng disable-event --list-options) |
fc256d99 DG |
125 | |
126 | case $prev in | |
127 | --session|-s) | |
128 | _lttng_complete_sessions | |
129 | return | |
130 | ;; | |
131 | --channel|-c) | |
132 | return | |
133 | ;; | |
fc256d99 DG |
134 | esac |
135 | ||
136 | case $cur in | |
137 | -*) | |
ba28686d | 138 | _lttng_complete_options |
fc256d99 DG |
139 | return |
140 | ;; | |
141 | esac | |
142 | } | |
143 | ||
07bd6635 | 144 | _lttng_cmd_enablechannel() { |
ba28686d | 145 | options=$(lttng enable-channel --list-options) |
fc256d99 DG |
146 | |
147 | case $prev in | |
148 | --session|-s) | |
149 | _lttng_complete_sessions | |
150 | return | |
151 | ;; | |
152 | esac | |
153 | ||
154 | case $cur in | |
155 | -*) | |
ba28686d | 156 | _lttng_complete_options |
fc256d99 DG |
157 | return |
158 | ;; | |
159 | esac | |
160 | } | |
161 | ||
07bd6635 | 162 | _lttng_cmd_enableevent() { |
ba28686d | 163 | options=$(lttng enable-event --list-options) |
fc256d99 DG |
164 | |
165 | case $prev in | |
166 | --session|-s) | |
167 | _lttng_complete_sessions | |
168 | return | |
169 | ;; | |
170 | --channel|-c) | |
171 | return | |
172 | ;; | |
07bd6635 SM |
173 | --probe) |
174 | return | |
175 | ;; | |
176 | --function) | |
177 | return | |
178 | ;; | |
fc256d99 DG |
179 | esac |
180 | ||
181 | case $cur in | |
182 | -*) | |
ba28686d | 183 | _lttng_complete_options |
fc256d99 DG |
184 | return |
185 | ;; | |
186 | esac | |
187 | } | |
188 | ||
189 | _lttng_cmd_list() { | |
ba28686d | 190 | options=$(lttng list --list-options) |
fc256d99 DG |
191 | |
192 | case $prev in | |
193 | --channel|-c) | |
194 | return | |
195 | ;; | |
196 | esac | |
197 | ||
198 | case $cur in | |
199 | -*) | |
ba28686d | 200 | _lttng_complete_options |
fc256d99 DG |
201 | return |
202 | ;; | |
4a096a5b SM |
203 | *) |
204 | _lttng_complete_sessions | |
205 | return | |
fc256d99 DG |
206 | esac |
207 | } | |
208 | ||
6c806062 | 209 | _lttng_cmd_setsession() { |
ba28686d | 210 | options=$(lttng set-session --list-options) |
fc256d99 DG |
211 | |
212 | case $cur in | |
213 | -*) | |
ba28686d | 214 | _lttng_complete_options |
fc256d99 DG |
215 | return |
216 | ;; | |
4a096a5b SM |
217 | *) |
218 | _lttng_complete_sessions | |
219 | return | |
220 | ;; | |
fc256d99 DG |
221 | esac |
222 | } | |
223 | ||
ba28686d SM |
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 | ||
fc256d99 | 248 | _lttng_cmd_start() { |
ba28686d | 249 | options=$(lttng start --list-options) |
fc256d99 DG |
250 | |
251 | case $cur in | |
252 | -*) | |
ba28686d SM |
253 | _lttng_complete_options |
254 | return | |
fc256d99 DG |
255 | ;; |
256 | *) | |
257 | _lttng_complete_sessions | |
ba28686d | 258 | return |
fc256d99 DG |
259 | ;; |
260 | esac | |
261 | } | |
262 | ||
263 | _lttng_cmd_stop() { | |
ba28686d | 264 | options=$(lttng stop --list-options) |
fc256d99 DG |
265 | |
266 | case $cur in | |
267 | -*) | |
ba28686d | 268 | _lttng_complete_options |
fc256d99 DG |
269 | ;; |
270 | *) | |
271 | _lttng_complete_sessions | |
272 | ;; | |
273 | esac | |
274 | } | |
275 | ||
276 | _lttng_cmd_version() { | |
ba28686d | 277 | options=$(lttng version --list-options) |
fc256d99 DG |
278 | |
279 | case $cur in | |
280 | -*) | |
ba28686d | 281 | _lttng_complete_options |
fc256d99 DG |
282 | ;; |
283 | esac | |
284 | } | |
285 | ||
0c95f5b2 | 286 | _lttng_cmd_view() { |
ba28686d | 287 | options=$(lttng view --list-options) |
0c95f5b2 | 288 | |
ba28686d SM |
289 | case $cur in |
290 | -*) | |
291 | _lttng_complete_options | |
0c95f5b2 DG |
292 | ;; |
293 | esac | |
294 | } | |
295 | ||
fc256d99 | 296 | |
fc256d99 DG |
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 | |
ba28686d | 314 | _lttng_complete_options |
fc256d99 DG |
315 | ;; |
316 | *) | |
317 | # Otherwise complete with commands | |
ba28686d | 318 | _lttng_complete_commands |
fc256d99 DG |
319 | ;; |
320 | esac | |
321 | } | |
322 | ||
323 | _lttng_after_command() { | |
324 | local cmd_name | |
325 | ||
ba28686d | 326 | cmd_name=_lttng_cmd_${command_found//-/} |
fc256d99 DG |
327 | |
328 | type -t $cmd_name | grep -q 'function' && $cmd_name | |
329 | } | |
330 | ||
ba28686d SM |
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. | |
fc256d99 DG |
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 | ||
ba28686d SM |
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} | |
fc256d99 DG |
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 | ||
ba28686d | 362 | for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do |
fc256d99 DG |
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 | |
fc256d99 | 369 | done |
ba28686d SM |
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 | |
fc256d99 DG |
387 | |
388 | # Check if the cursor is before or after the command keyword | |
ba28686d | 389 | if _lttng_cursor_is_after_command; then |
fc256d99 DG |
390 | _lttng_after_command |
391 | else | |
392 | _lttng_before_command | |
393 | fi | |
394 | } | |
395 | ||
396 | complete -F _lttng lttng |