1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
16 #ifndef CMD_MAX_TOKENS
17 #define CMD_MAX_TOKENS 256
20 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
21 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
22 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
23 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
24 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
25 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
26 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
27 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
28 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
29 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
34 if ((strlen(in) && index("!#%;", in[0])) ||
35 (strncmp(in, "//", 2) == 0) ||
36 (strncmp(in, "--", 2) == 0))
43 * mempool <mempool_name>
44 * buffer <buffer_size>
50 cmd_mempool(char **tokens,
55 struct mempool_params p;
57 struct mempool *mempool;
60 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
66 if (strcmp(tokens[2], "buffer") != 0) {
67 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
71 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
72 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
76 if (strcmp(tokens[4], "pool") != 0) {
77 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
81 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
82 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
86 if (strcmp(tokens[6], "cache") != 0) {
87 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
91 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
92 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
96 if (strcmp(tokens[8], "cpu") != 0) {
97 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
101 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
102 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
106 mempool = mempool_create(name, &p);
107 if (mempool == NULL) {
108 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
114 cli_process(char *in, char *out, size_t out_size)
116 char *tokens[CMD_MAX_TOKENS];
117 uint32_t n_tokens = RTE_DIM(tokens);
123 status = parse_tokenize_string(in, tokens, &n_tokens);
125 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
132 if (strcmp(tokens[0], "mempool") == 0) {
133 cmd_mempool(tokens, n_tokens, out, out_size);
137 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
141 cli_script_process(const char *file_name,
142 size_t msg_in_len_max,
143 size_t msg_out_len_max)
145 char *msg_in = NULL, *msg_out = NULL;
148 /* Check input arguments */
149 if ((file_name == NULL) ||
150 (strlen(file_name) == 0) ||
151 (msg_in_len_max == 0) ||
152 (msg_out_len_max == 0))
155 msg_in = malloc(msg_in_len_max + 1);
156 msg_out = malloc(msg_out_len_max + 1);
157 if ((msg_in == NULL) ||
164 /* Open input file */
165 f = fopen(file_name, "r");
174 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
177 printf("%s", msg_in);
185 printf("%s", msg_out);