test: move unit tests to separate directory
[dpdk.git] / test / cmdline_test / commands.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <termios.h>
37 #include <inttypes.h>
38
39 #include <cmdline_rdline.h>
40 #include <cmdline_parse.h>
41 #include <cmdline_parse_string.h>
42 #include <cmdline_parse_num.h>
43 #include <cmdline.h>
44
45 #include "cmdline_test.h"
46
47 /*** quit ***/
48 /* exit application */
49
50 struct cmd_quit_result {
51         cmdline_fixed_string_t quit;
52 };
53
54 static void
55 cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
56                 struct cmdline *cl,
57                 __attribute__((unused)) void *data)
58 {
59         cmdline_quit(cl);
60 }
61
62 cmdline_parse_token_string_t cmd_quit_tok =
63         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit,
64                                  "quit");
65
66 cmdline_parse_inst_t cmd_quit = {
67         .f = cmd_quit_parsed,  /* function to call */
68         .data = NULL,      /* 2nd arg of func */
69         .help_str = "exit application",
70         .tokens = {        /* token list, NULL terminated */
71                 (void *)&cmd_quit_tok,
72                 NULL,
73         },
74 };
75
76
77
78 /*** single ***/
79 /* a simple single-word command */
80
81 struct cmd_single_result {
82         cmdline_fixed_string_t single;
83 };
84
85 static void
86 cmd_single_parsed(__attribute__((unused)) void *parsed_result,
87                 struct cmdline *cl,
88                 __attribute__((unused)) void *data)
89 {
90         cmdline_printf(cl, "Single word command parsed!\n");
91 }
92
93 cmdline_parse_token_string_t cmd_single_tok =
94         TOKEN_STRING_INITIALIZER(struct cmd_single_result, single,
95                                  "single");
96
97 cmdline_parse_inst_t cmd_single = {
98         .f = cmd_single_parsed,  /* function to call */
99         .data = NULL,      /* 2nd arg of func */
100         .help_str = "a simple single-word command",
101         .tokens = {        /* token list, NULL terminated */
102                 (void *)&cmd_single_tok,
103                 NULL,
104         },
105 };
106
107
108
109 /*** single_long ***/
110 /* a variant of "single" command. useful to test autocomplete */
111
112 struct cmd_single_long_result {
113         cmdline_fixed_string_t single_long;
114 };
115
116 static void
117 cmd_single_long_parsed(__attribute__((unused)) void *parsed_result,
118                 struct cmdline *cl,
119                 __attribute__((unused)) void *data)
120 {
121         cmdline_printf(cl, "Single long word command parsed!\n");
122 }
123
124 cmdline_parse_token_string_t cmd_single_long_tok =
125         TOKEN_STRING_INITIALIZER(struct cmd_single_long_result, single_long,
126                                  "single_long");
127
128 cmdline_parse_inst_t cmd_single_long = {
129         .f = cmd_single_long_parsed,  /* function to call */
130         .data = NULL,      /* 2nd arg of func */
131         .help_str = "a variant of \"single\" command, useful to test autocomplete",
132         .tokens = {        /* token list, NULL terminated */
133                 (void *)&cmd_single_long_tok,
134                 NULL,
135         },
136 };
137
138
139
140 /*** autocomplete_1 ***/
141 /* first command to test autocomplete when multiple commands have chars
142  * in common but none should complete due to ambiguity
143  */
144
145 struct cmd_autocomplete_1_result {
146         cmdline_fixed_string_t token;
147 };
148
149 static void
150 cmd_autocomplete_1_parsed(__attribute__((unused)) void *parsed_result,
151                 struct cmdline *cl,
152                 __attribute__((unused)) void *data)
153 {
154         cmdline_printf(cl, "Autocomplete command 1 parsed!\n");
155 }
156
157 cmdline_parse_token_string_t cmd_autocomplete_1_tok =
158         TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_1_result, token,
159                                  "autocomplete_1");
160
161 cmdline_parse_inst_t cmd_autocomplete_1 = {
162         .f = cmd_autocomplete_1_parsed,  /* function to call */
163         .data = NULL,      /* 2nd arg of func */
164         .help_str = "first ambiguous autocomplete command",
165         .tokens = {        /* token list, NULL terminated */
166                 (void *)&cmd_autocomplete_1_tok,
167                 NULL,
168         },
169 };
170
171
172
173 /*** autocomplete_2 ***/
174 /* second command to test autocomplete when multiple commands have chars
175  * in common but none should complete due to ambiguity
176  */
177
178 struct cmd_autocomplete_2_result {
179         cmdline_fixed_string_t token;
180 };
181
182 static void
183 cmd_autocomplete_2_parsed(__attribute__((unused)) void *parsed_result,
184                 struct cmdline *cl,
185                 __attribute__((unused)) void *data)
186 {
187         cmdline_printf(cl, "Autocomplete command 2 parsed!\n");
188 }
189
190 cmdline_parse_token_string_t cmd_autocomplete_2_tok =
191         TOKEN_STRING_INITIALIZER(struct cmd_autocomplete_2_result, token,
192                                  "autocomplete_2");
193
194 cmdline_parse_inst_t cmd_autocomplete_2 = {
195         .f = cmd_autocomplete_2_parsed,  /* function to call */
196         .data = NULL,      /* 2nd arg of func */
197         .help_str = "second ambiguous autocomplete command",
198         .tokens = {        /* token list, NULL terminated */
199                 (void *)&cmd_autocomplete_2_tok,
200                 NULL,
201         },
202 };
203
204
205
206 /*** number command ***/
207 /* a command that simply returns whatever (uint32) number is supplied to it */
208
209 struct cmd_num_result {
210         unsigned num;
211 };
212
213 static void
214 cmd_num_parsed(void *parsed_result,
215                 struct cmdline *cl,
216                 __attribute__((unused)) void *data)
217 {
218         unsigned result = ((struct cmd_num_result*)parsed_result)->num;
219         cmdline_printf(cl, "%u\n", result);
220 }
221
222 cmdline_parse_token_num_t cmd_num_tok =
223         TOKEN_NUM_INITIALIZER(struct cmd_num_result, num, UINT32);
224
225 cmdline_parse_inst_t cmd_num = {
226         .f = cmd_num_parsed,  /* function to call */
227         .data = NULL,      /* 2nd arg of func */
228         .help_str = "a command that simply returns whatever number is entered",
229         .tokens = {        /* token list, NULL terminated */
230                 (void *)&cmd_num_tok,
231                 NULL,
232         },
233 };
234
235
236
237 /*** ambiguous first|ambiguous ***/
238 /* first command used to test command ambiguity */
239
240 struct cmd_ambig_result_1 {
241         cmdline_fixed_string_t common_part;
242         cmdline_fixed_string_t ambig_part;
243 };
244
245 static void
246 cmd_ambig_1_parsed(__attribute__((unused)) void *parsed_result,
247                 struct cmdline *cl,
248                 __attribute__((unused)) void *data)
249 {
250         cmdline_printf(cl, "Command 1 parsed!\n");
251 }
252
253 cmdline_parse_token_string_t cmd_ambig_common_1 =
254         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, common_part,
255                                  "ambiguous");
256 cmdline_parse_token_string_t cmd_ambig_ambig_1 =
257         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_1, ambig_part,
258                                  "first#ambiguous#ambiguous2");
259
260 cmdline_parse_inst_t cmd_ambig_1 = {
261         .f = cmd_ambig_1_parsed,  /* function to call */
262         .data = NULL,      /* 2nd arg of func */
263         .help_str = "first command used to test command ambiguity",
264         .tokens = {        /* token list, NULL terminated */
265                 (void *)&cmd_ambig_common_1,
266                 (void*)&cmd_ambig_ambig_1,
267                 NULL,
268         },
269 };
270
271
272
273 /*** ambiguous second|ambiguous ***/
274 /* second command used to test command ambiguity */
275
276 struct cmd_ambig_result_2 {
277         cmdline_fixed_string_t common_part;
278         cmdline_fixed_string_t ambig_part;
279 };
280
281 static void
282 cmd_ambig_2_parsed(__attribute__((unused)) void *parsed_result,
283                 struct cmdline *cl,
284                 __attribute__((unused)) void *data)
285 {
286         cmdline_printf(cl, "Command 2 parsed!\n");
287 }
288
289 cmdline_parse_token_string_t cmd_ambig_common_2 =
290         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, common_part,
291                                  "ambiguous");
292 cmdline_parse_token_string_t cmd_ambig_ambig_2 =
293         TOKEN_STRING_INITIALIZER(struct cmd_ambig_result_2, ambig_part,
294                                  "second#ambiguous#ambiguous2");
295
296 cmdline_parse_inst_t cmd_ambig_2 = {
297         .f = cmd_ambig_2_parsed,  /* function to call */
298         .data = NULL,      /* 2nd arg of func */
299         .help_str = "second command used to test command ambiguity",
300         .tokens = {        /* token list, NULL terminated */
301                 (void *)&cmd_ambig_common_2,
302                 (void*)&cmd_ambig_ambig_2,
303                 NULL,
304         },
305 };
306
307
308
309 /*** get_history_bufsize ***/
310 /* command that displays total space in history buffer
311  * this will be useful for testing history (to fill it up just enough to
312  * remove the last entry, we need to know how big it is).
313  */
314
315 struct cmd_get_history_bufsize_result {
316         cmdline_fixed_string_t str;
317 };
318
319 static void
320 cmd_get_history_bufsize_parsed(__attribute__((unused)) void *parsed_result,
321                 struct cmdline *cl,
322                 __attribute__((unused)) void *data)
323 {
324         cmdline_printf(cl, "History buffer size: %zu\n",
325                         sizeof(cl->rdl.history_buf));
326 }
327
328 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
329         TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
330                                  "get_history_bufsize");
331
332 cmdline_parse_inst_t cmd_get_history_bufsize = {
333         .f = cmd_get_history_bufsize_parsed,  /* function to call */
334         .data = NULL,      /* 2nd arg of func */
335         .help_str = "command that displays total space in history buffer",
336         .tokens = {        /* token list, NULL terminated */
337                 (void *)&cmd_get_history_bufsize_tok,
338                 NULL,
339         },
340 };
341
342
343
344 /*** clear_history ***/
345 /* clears history buffer */
346
347 struct cmd_clear_history_result {
348         cmdline_fixed_string_t str;
349 };
350
351 static void
352 cmd_clear_history_parsed(__attribute__((unused)) void *parsed_result,
353                 struct cmdline *cl,
354                 __attribute__((unused)) void *data)
355 {
356         rdline_clear_history(&cl->rdl);
357 }
358
359 cmdline_parse_token_string_t cmd_clear_history_tok =
360         TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
361                                  "clear_history");
362
363 cmdline_parse_inst_t cmd_clear_history = {
364         .f = cmd_clear_history_parsed,  /* function to call */
365         .data = NULL,      /* 2nd arg of func */
366         .help_str = "clear command history",
367         .tokens = {        /* token list, NULL terminated */
368                 (void *)&cmd_clear_history_tok,
369                 NULL,
370         },
371 };
372
373
374
375 /****************/
376
377 cmdline_parse_ctx_t main_ctx[] = {
378                 (cmdline_parse_inst_t *)&cmd_quit,
379                 (cmdline_parse_inst_t *)&cmd_ambig_1,
380                 (cmdline_parse_inst_t *)&cmd_ambig_2,
381                 (cmdline_parse_inst_t *)&cmd_single,
382                 (cmdline_parse_inst_t *)&cmd_single_long,
383                 (cmdline_parse_inst_t *)&cmd_num,
384                 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
385                 (cmdline_parse_inst_t *)&cmd_clear_history,
386                 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
387                 (cmdline_parse_inst_t *)&cmd_autocomplete_2,
388         NULL,
389 };