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