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
cmd_quit_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_single_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_single_long_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_autocomplete_1_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_autocomplete_2_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_num_parsed(void * parsed_result,struct cmdline * cl,__rte_unused void * data)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, RTE_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
cmd_ambig_1_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_ambig_2_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)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
cmd_get_history_bufsize_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)293 cmd_get_history_bufsize_parsed(__rte_unused void *parsed_result,
294 struct cmdline *cl,
295 __rte_unused void *data)
296 {
297 struct rdline *rdl = cmdline_get_rdline(cl);
298
299 cmdline_printf(cl, "History buffer size: %zu\n",
300 rdline_get_history_buffer_size(rdl));
301 }
302
303 cmdline_parse_token_string_t cmd_get_history_bufsize_tok =
304 TOKEN_STRING_INITIALIZER(struct cmd_get_history_bufsize_result, str,
305 "get_history_bufsize");
306
307 cmdline_parse_inst_t cmd_get_history_bufsize = {
308 .f = cmd_get_history_bufsize_parsed, /* function to call */
309 .data = NULL, /* 2nd arg of func */
310 .help_str = "command that displays total space in history buffer",
311 .tokens = { /* token list, NULL terminated */
312 (void *)&cmd_get_history_bufsize_tok,
313 NULL,
314 },
315 };
316
317
318
319 /*** clear_history ***/
320 /* clears history buffer */
321
322 struct cmd_clear_history_result {
323 cmdline_fixed_string_t str;
324 };
325
326 static void
cmd_clear_history_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)327 cmd_clear_history_parsed(__rte_unused void *parsed_result,
328 struct cmdline *cl,
329 __rte_unused void *data)
330 {
331 struct rdline *rdl = cmdline_get_rdline(cl);
332
333 rdline_clear_history(rdl);
334 }
335
336 cmdline_parse_token_string_t cmd_clear_history_tok =
337 TOKEN_STRING_INITIALIZER(struct cmd_clear_history_result, str,
338 "clear_history");
339
340 cmdline_parse_inst_t cmd_clear_history = {
341 .f = cmd_clear_history_parsed, /* function to call */
342 .data = NULL, /* 2nd arg of func */
343 .help_str = "clear command history",
344 .tokens = { /* token list, NULL terminated */
345 (void *)&cmd_clear_history_tok,
346 NULL,
347 },
348 };
349
350
351
352 /****************/
353
354 cmdline_parse_ctx_t main_ctx[] = {
355 (cmdline_parse_inst_t *)&cmd_quit,
356 (cmdline_parse_inst_t *)&cmd_ambig_1,
357 (cmdline_parse_inst_t *)&cmd_ambig_2,
358 (cmdline_parse_inst_t *)&cmd_single,
359 (cmdline_parse_inst_t *)&cmd_single_long,
360 (cmdline_parse_inst_t *)&cmd_num,
361 (cmdline_parse_inst_t *)&cmd_get_history_bufsize,
362 (cmdline_parse_inst_t *)&cmd_clear_history,
363 (cmdline_parse_inst_t *)&cmd_autocomplete_1,
364 (cmdline_parse_inst_t *)&cmd_autocomplete_2,
365 NULL,
366 };
367