5c2142d0e58cbe3e991532534f568856a6125b68
[dpdk.git] / app / cmdline_test / commands.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <termios.h>
38 #include <inttypes.h>
39
40 #include <cmdline_rdline.h>
41 #include <cmdline_parse.h>
42 #include <cmdline_parse_string.h>
43 #include <cmdline_parse_num.h>
44 #include <cmdline.h>
45
46 #include "cmdline_test.h"
47
48 /*** quit ***/
49 /* exit application */
50
51 struct cmd_quit_result {
52         cmdline_fixed_string_t quit;
53 };
54
55 static void
56 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
57                 struct cmdline *cl,
58                 __attribute__((unused)) void *data)
59 {
60         cmdline_quit(cl);
61 }
62
63 cmdline_parse_token_string_t cmd_quit_tok =
64         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
65                                  "quit");
66
67 cmdline_parse_inst_t cmd_quit = {
68         .f = cmd_quit_parsed,  /* function to call */
69         .data = NULL,      /* 2nd arg of func */
70         .help_str = "exit application",
71         .tokens = {        /* token list, NULL terminated */
72                 (void *)&cmd_quit_tok,
73                 NULL,
74         },
75 };
76
77
78
79 /*** single ***/
80 /* a simple single-word command */
81
82 struct cmd_single_result {
83         cmdline_fixed_string_t single;
84 };
85
86 static void
87 cmd_single_parsed(__attribute__((unused)) void *parsed_result,
88                 struct cmdline *cl,
89                 __attribute__((unused)) void *data)
90 {
91         cmdline_printf(cl, "Single word command parsed!\n");
92 }
93
94 cmdline_parse_token_string_t cmd_single_tok =
95         TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
96                                  "single");
97
98 cmdline_parse_inst_t cmd_single = {
99         .f = cmd_single_parsed,  /* function to call */
100         .data = NULL,      /* 2nd arg of func */
101         .help_str = "a simple single-word command",
102         .tokens = {        /* token list, NULL terminated */
103                 (void *)&cmd_single_tok,
104                 NULL,
105         },
106 };
107
108
109
110 /*** single_long ***/
111 /* a variant of "single" command. useful to test autocomplete */
112
113 struct cmd_single_long_result {
114         cmdline_fixed_string_t single_long;
115 };
116
117 static void
118 cmd_single_long_parsed(__attribute__((unused)) void *parsed_result,
119                 struct cmdline *cl,
120                 __attribute__((unused)) void *data)
121 {
122         cmdline_printf(cl, "Single long word command parsed!\n");
123 }
124
125 cmdline_parse_token_string_t cmd_single_long_tok =
126         TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
127                                  "single_long");
128
129 cmdline_parse_inst_t cmd_single_long = {
130         .f = cmd_single_long_parsed,  /* function to call */
131         .data = NULL,      /* 2nd arg of func */
132         .help_str = "a variant of \"single\" command, useful to test autocomplete",
133         .tokens = {        /* token list, NULL terminated */
134                 (void *)&cmd_single_long_tok,
135                 NULL,
136         },
137 };
138
139
140
141 /*** autocomplete_1 ***/
142 /* first command to test autocomplete when multiple commands have chars
143  * in common but none should complete due to ambiguity
144  */
145
146 struct cmd_autocomplete_1_result {
147         cmdline_fixed_string_t token;
148 };
149
150 static void
151 cmd_autocomplete_1_parsed(__attribute__((unused)) void *parsed_result,
152                 struct cmdline *cl,
153                 __attribute__((unused)) void *data)
154 {
155         cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
156 }
157
158 cmdline_parse_token_string_t cmd_autocomplete_1_tok =
159         TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
160                                  "autocomplete_1");
161
162 cmdline_parse_inst_t cmd_autocomplete_1 = {
163         .f = cmd_autocomplete_1_parsed,  /* function to call */
164         .data = NULL,      /* 2nd arg of func */
165         .help_str = "first ambiguous autocomplete command",
166         .tokens = {        /* token list, NULL terminated */
167                 (void *)&cmd_autocomplete_1_tok,
168                 NULL,
169         },
170 };
171
172
173
174 /*** autocomplete_2 ***/
175 /* second command to test autocomplete when multiple commands have chars
176  * in common but none should complete due to ambiguity
177  */
178
179 struct cmd_autocomplete_2_result {
180         cmdline_fixed_string_t token;
181 };
182
183 static void
184 cmd_autocomplete_2_parsed(__attribute__((unused)) void *parsed_result,
185                 struct cmdline *cl,
186                 __attribute__((unused)) void *data)
187 {
188         cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
189 }
190
191 cmdline_parse_token_string_t cmd_autocomplete_2_tok =
192         TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
193                                  "autocomplete_2");
194
195 cmdline_parse_inst_t cmd_autocomplete_2 = {
196         .f = cmd_autocomplete_2_parsed,  /* function to call */
197         .data = NULL,      /* 2nd arg of func */
198         .help_str = "second ambiguous autocomplete command",
199         .tokens = {        /* token list, NULL terminated */
200                 (void *)&cmd_autocomplete_2_tok,
201                 NULL,
202         },
203 };
204
205
206
207 /*** number command ***/
208 /* a command that simply returns whatever (uint32) number is supplied to it */
209
210 struct cmd_num_result {
211         unsigned num;
212 };
213
214 static void
215 cmd_num_parsed(void *parsed_result,
216                 struct cmdline *cl,
217                 __attribute__((unused)) void *data)
218 {
219         unsigned result = ((struct cmd_num_result*)parsed_result)->num;
220         cmdline_printf(cl, "%u\n", result);
221 }
222
223 cmdline_parse_token_num_t cmd_num_tok =
224         TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, UINT32);
225
226 cmdline_parse_inst_t cmd_num = {
227         .f = cmd_num_parsed,  /* function to call */
228         .data = NULL,      /* 2nd arg of func */
229         .help_str = "a command that simply returns whatever number is entered",
230         .tokens = {        /* token list, NULL terminated */
231                 (void *)&cmd_num_tok,
232                 NULL,
233         },
234 };
235
236
237
238 /*** ambiguous first|ambiguous ***/
239 /* first command used to test command ambiguity */
240
241 struct cmd_ambig_result_1 {
242         cmdline_fixed_string_t common_part;
243         cmdline_fixed_string_t ambig_part;
244 };
245
246 static void
247 cmd_ambig_1_parsed(__attribute__((unused)) void *parsed_result,
248                 struct cmdline *cl,
249                 __attribute__((unused)) void *data)
250 {
251         cmdline_printf(cl, "Command 1 parsed!\n");
252 }
253
254 cmdline_parse_token_string_t cmd_ambig_common_1 =
255         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
256                                  "ambiguous");
257 cmdline_parse_token_string_t cmd_ambig_ambig_1 =
258         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
259                                  "first#ambiguous#ambiguous2");
260
261 cmdline_parse_inst_t cmd_ambig_1 = {
262         .f = cmd_ambig_1_parsed,  /* function to call */
263         .data = NULL,      /* 2nd arg of func */
264         .help_str = "first command used to test command ambiguity",
265         .tokens = {        /* token list, NULL terminated */
266                 (void *)&cmd_ambig_common_1,
267                 (void*)&cmd_ambig_ambig_1,
268                 NULL,
269         },
270 };
271
272
273
274 /*** ambiguous second|ambiguous ***/
275 /* second command used to test command ambiguity */
276
277 struct cmd_ambig_result_2 {
278         cmdline_fixed_string_t common_part;
279         cmdline_fixed_string_t ambig_part;
280 };
281
282 static void
283 cmd_ambig_2_parsed(__attribute__((unused)) void *parsed_result,
284                 struct cmdline *cl,
285                 __attribute__((unused)) void *data)
286 {
287         cmdline_printf(cl, "Command 2 parsed!\n");
288 }
289
290 cmdline_parse_token_string_t cmd_ambig_common_2 =
291         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
292                                  "ambiguous");
293 cmdline_parse_token_string_t cmd_ambig_ambig_2 =
294         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
295                                  "second#ambiguous#ambiguous2");
296
297 cmdline_parse_inst_t cmd_ambig_2 = {
298         .f = cmd_ambig_2_parsed,  /* function to call */
299         .data = NULL,      /* 2nd arg of func */
300         .help_str = "second command used to test command ambiguity",
301         .tokens = {        /* token list, NULL terminated */
302                 (void *)&cmd_ambig_common_2,
303                 (void*)&cmd_ambig_ambig_2,
304                 NULL,
305         },
306 };
307
308
309
310 /*** get_history_bufsize ***/
311 /* command that displays total space in history buffer
312  * this will be useful for testing history (to fill it up just enough to
313  * remove the last entry, we need to know how big it is).
314  */
315
316 struct cmd_get_history_bufsize_result {
317         cmdline_fixed_string_t str;
318 };
319
320 static void
321 cmd_get_history_bufsize_parsed(__attribute__((unused)) void *parsed_result,
322                 struct cmdline *cl,
323                 __attribute__((unused)) void *data)
324 {
325         cmdline_printf(cl, "History buffer size: %u\n",
326                         sizeof(cl->rdl.history_buf));
327 }
328
329 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
330         TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
331                                  "get_history_bufsize");
332
333 cmdline_parse_inst_t cmd_get_history_bufsize = {
334         .f = cmd_get_history_bufsize_parsed,  /* function to call */
335         .data = NULL,      /* 2nd arg of func */
336         .help_str = "command that displays total space in history buffer",
337         .tokens = {        /* token list, NULL terminated */
338                 (void *)&cmd_get_history_bufsize_tok,
339                 NULL,
340         },
341 };
342
343
344
345 /*** clear_history ***/
346 /* clears history buffer */
347
348 struct cmd_clear_history_result {
349         cmdline_fixed_string_t str;
350 };
351
352 static void
353 cmd_clear_history_parsed(__attribute__((unused)) void *parsed_result,
354                 struct cmdline *cl,
355                 __attribute__((unused)) void *data)
356 {
357         rdline_clear_history(&cl->rdl);
358 }
359
360 cmdline_parse_token_string_t cmd_clear_history_tok =
361         TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
362                                  "clear_history");
363
364 cmdline_parse_inst_t cmd_clear_history = {
365         .f = cmd_clear_history_parsed,  /* function to call */
366         .data = NULL,      /* 2nd arg of func */
367         .help_str = "clear command history",
368         .tokens = {        /* token list, NULL terminated */
369                 (void *)&cmd_clear_history_tok,
370                 NULL,
371         },
372 };
373
374
375
376 /****************/
377
378 cmdline_parse_ctx_t main_ctx[] = {
379                 (cmdline_parse_inst_t *)&cmd_quit,
380                 (cmdline_parse_inst_t *)&cmd_ambig_1,
381                 (cmdline_parse_inst_t *)&cmd_ambig_2,
382                 (cmdline_parse_inst_t *)&cmd_single,
383                 (cmdline_parse_inst_t *)&cmd_single_long,
384                 (cmdline_parse_inst_t *)&cmd_num,
385                 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
386                 (cmdline_parse_inst_t *)&cmd_clear_history,
387                 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
388                 (cmdline_parse_inst_t *)&cmd_autocomplete_2,
389         NULL,
390 };