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