Branch data Line data Source code
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 *)&graph_config_cmd_ctx,
24 : : (cmdline_parse_inst_t *)&graph_start_cmd_ctx,
25 : : (cmdline_parse_inst_t *)&graph_stats_cmd_ctx,
26 : : (cmdline_parse_inst_t *)&graph_help_cmd_ctx,
27 : : (cmdline_parse_inst_t *)&mempool_config_cmd_ctx,
28 : : (cmdline_parse_inst_t *)&mempool_help_cmd_ctx,
29 : : (cmdline_parse_inst_t *)ðdev_show_cmd_ctx,
30 : : (cmdline_parse_inst_t *)ðdev_stats_cmd_ctx,
31 : : (cmdline_parse_inst_t *)ðdev_mtu_cmd_ctx,
32 : : (cmdline_parse_inst_t *)ðdev_prom_mode_cmd_ctx,
33 : : (cmdline_parse_inst_t *)ðdev_ip4_cmd_ctx,
34 : : (cmdline_parse_inst_t *)ðdev_ip6_cmd_ctx,
35 : : (cmdline_parse_inst_t *)ðdev_cmd_ctx,
36 : : (cmdline_parse_inst_t *)ðdev_help_cmd_ctx,
37 : : (cmdline_parse_inst_t *)ðdev_rx_cmd_ctx,
38 : : (cmdline_parse_inst_t *)ðdev_rx_help_cmd_ctx,
39 : : (cmdline_parse_inst_t *)&ipv4_lookup_cmd_ctx,
40 : : (cmdline_parse_inst_t *)&ipv4_lookup_help_cmd_ctx,
41 : : (cmdline_parse_inst_t *)&ipv6_lookup_cmd_ctx,
42 : : (cmdline_parse_inst_t *)&ipv6_lookup_help_cmd_ctx,
43 : : (cmdline_parse_inst_t *)&neigh_v4_cmd_ctx,
44 : : (cmdline_parse_inst_t *)&neigh_v6_cmd_ctx,
45 : : (cmdline_parse_inst_t *)&neigh_help_cmd_ctx,
46 : : NULL,
47 : : };
48 : :
49 : : static struct cmdline *cl;
50 : :
51 : : static int
52 : 0 : is_comment(char *in)
53 : : {
54 : 0 : if ((strlen(in) && index("!#%;", in[0])) ||
55 : 0 : (strncmp(in, "//", 2) == 0) ||
56 : 0 : (strncmp(in, "--", 2) == 0))
57 : 0 : return 1;
58 : :
59 : : return 0;
60 : : }
61 : :
62 : : void
63 : 0 : cli_init(void)
64 : : {
65 : 0 : cl = cmdline_stdin_new(modules_ctx, "");
66 : 0 : }
67 : :
68 : : void
69 : 0 : cli_exit(void)
70 : : {
71 : 0 : cmdline_stdin_exit(cl);
72 : 0 : }
73 : :
74 : : void
75 : 0 : cli_process(char *in, char *out, size_t out_size, __rte_unused void *obj)
76 : : {
77 : : int rc;
78 : :
79 : 0 : if (is_comment(in))
80 : : return;
81 : :
82 : 0 : rc = cmdline_parse(cl, in);
83 : 0 : if (rc == CMDLINE_PARSE_AMBIGUOUS)
84 : : snprintf(out, out_size, MSG_CMD_FAIL, "Ambiguous command");
85 : 0 : else if (rc == CMDLINE_PARSE_NOMATCH)
86 : : snprintf(out, out_size, MSG_CMD_FAIL, "Command mismatch");
87 : 0 : else if (rc == CMDLINE_PARSE_BAD_ARGS)
88 : : snprintf(out, out_size, MSG_CMD_FAIL, "Bad arguments");
89 : :
90 : : return;
91 : :
92 : : }
93 : :
94 : : int
95 : 0 : cli_script_process(const char *file_name, size_t msg_in_len_max, size_t msg_out_len_max, void *obj)
96 : : {
97 : : char *msg_in = NULL, *msg_out = NULL;
98 : : int rc = -EINVAL;
99 : : FILE *f = NULL;
100 : :
101 : : /* Check input arguments */
102 : 0 : if ((file_name == NULL) || (strlen(file_name) == 0) || (msg_in_len_max == 0) ||
103 : 0 : (msg_out_len_max == 0))
104 : : return rc;
105 : :
106 : 0 : msg_in = malloc(msg_in_len_max + 1);
107 : 0 : msg_out = malloc(msg_out_len_max + 1);
108 : 0 : if ((msg_in == NULL) || (msg_out == NULL)) {
109 : : rc = -ENOMEM;
110 : 0 : goto exit;
111 : : }
112 : :
113 : : /* Open input file */
114 : 0 : f = fopen(file_name, "r");
115 : 0 : if (f == NULL) {
116 : : rc = -EIO;
117 : 0 : goto exit;
118 : : }
119 : :
120 : : /* Read file */
121 : 0 : while (fgets(msg_in, msg_in_len_max, f) != NULL) {
122 : 0 : msg_out[0] = 0;
123 : :
124 : 0 : cli_process(msg_in, msg_out, msg_out_len_max, obj);
125 : :
126 : 0 : if (strlen(msg_out))
127 : : printf("%s", msg_out);
128 : : }
129 : :
130 : : /* Close file */
131 : 0 : fclose(f);
132 : : rc = 0;
133 : :
134 : 0 : exit:
135 : 0 : free(msg_out);
136 : 0 : free(msg_in);
137 : 0 : return rc;
138 : : }
|