1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2023 Marvell. 3 */ 4 5 #include <errno.h> 6 #include <stdio.h> 7 #include <stdint.h> 8 #include <stdlib.h> 9 #include <string.h> 10 11 #include <cmdline_parse.h> 12 #include <cmdline_parse_num.h> 13 #include <cmdline_parse_string.h> 14 #include <cmdline_socket.h> 15 #include <rte_common.h> 16 17 #include "module_api.h" 18 19 #define CMD_MAX_TOKENS 256 20 #define MAX_LINE_SIZE 2048 21 22 cmdline_parse_ctx_t modules_ctx[] = { 23 (cmdline_parse_inst_t *)&mempool_config_cmd_ctx, 24 (cmdline_parse_inst_t *)&mempool_help_cmd_ctx, 25 (cmdline_parse_inst_t *)ðdev_show_cmd_ctx, 26 (cmdline_parse_inst_t *)ðdev_stats_cmd_ctx, 27 (cmdline_parse_inst_t *)ðdev_mtu_cmd_ctx, 28 (cmdline_parse_inst_t *)ðdev_prom_mode_cmd_ctx, 29 (cmdline_parse_inst_t *)ðdev_ip4_cmd_ctx, 30 (cmdline_parse_inst_t *)ðdev_ip6_cmd_ctx, 31 (cmdline_parse_inst_t *)ðdev_cmd_ctx, 32 (cmdline_parse_inst_t *)ðdev_help_cmd_ctx, 33 (cmdline_parse_inst_t *)&ipv4_lookup_cmd_ctx, 34 (cmdline_parse_inst_t *)&ipv4_lookup_help_cmd_ctx, 35 (cmdline_parse_inst_t *)&ipv6_lookup_cmd_ctx, 36 (cmdline_parse_inst_t *)&ipv6_lookup_help_cmd_ctx, 37 NULL, 38 }; 39 40 static struct cmdline *cl; 41 42 static int 43 is_comment(char *in) 44 { 45 if ((strlen(in) && index("!#%;", in[0])) || 46 (strncmp(in, "//", 2) == 0) || 47 (strncmp(in, "--", 2) == 0)) 48 return 1; 49 50 return 0; 51 } 52 53 void 54 cli_init(void) 55 { 56 cl = cmdline_stdin_new(modules_ctx, ""); 57 } 58 59 void 60 cli_exit(void) 61 { 62 cmdline_stdin_exit(cl); 63 } 64 65 void 66 cli_process(char *in, char *out, size_t out_size, __rte_unused void *obj) 67 { 68 int rc; 69 70 if (is_comment(in)) 71 return; 72 73 rc = cmdline_parse(cl, in); 74 if (rc == CMDLINE_PARSE_AMBIGUOUS) 75 snprintf(out, out_size, MSG_CMD_FAIL, "Ambiguous command"); 76 else if (rc == CMDLINE_PARSE_NOMATCH) 77 snprintf(out, out_size, MSG_CMD_FAIL, "Command mismatch"); 78 else if (rc == CMDLINE_PARSE_BAD_ARGS) 79 snprintf(out, out_size, MSG_CMD_FAIL, "Bad arguments"); 80 81 return; 82 83 } 84 85 int 86 cli_script_process(const char *file_name, size_t msg_in_len_max, size_t msg_out_len_max, void *obj) 87 { 88 char *msg_in = NULL, *msg_out = NULL; 89 int rc = -EINVAL; 90 FILE *f = NULL; 91 92 /* Check input arguments */ 93 if ((file_name == NULL) || (strlen(file_name) == 0) || (msg_in_len_max == 0) || 94 (msg_out_len_max == 0)) 95 return rc; 96 97 msg_in = malloc(msg_in_len_max + 1); 98 msg_out = malloc(msg_out_len_max + 1); 99 if ((msg_in == NULL) || (msg_out == NULL)) { 100 rc = -ENOMEM; 101 goto exit; 102 } 103 104 /* Open input file */ 105 f = fopen(file_name, "r"); 106 if (f == NULL) { 107 rc = -EIO; 108 goto exit; 109 } 110 111 /* Read file */ 112 while (fgets(msg_in, msg_in_len_max, f) != NULL) { 113 msg_out[0] = 0; 114 115 cli_process(msg_in, msg_out, msg_out_len_max, obj); 116 117 if (strlen(msg_out)) 118 printf("%s", msg_out); 119 } 120 121 /* Close file */ 122 fclose(f); 123 rc = 0; 124 125 exit: 126 free(msg_out); 127 free(msg_in); 128 return rc; 129 } 130