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