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 : : static struct cmdline *cl;
23 : :
24 : : static int
25 : 0 : is_comment(char *in)
26 : : {
27 : 0 : if ((strlen(in) && index("!#%;", in[0])) ||
28 : 0 : (strncmp(in, "//", 2) == 0) ||
29 : 0 : (strncmp(in, "--", 2) == 0))
30 : 0 : return 1;
31 : :
32 : : return 0;
33 : : }
34 : :
35 : : void
36 : 0 : cli_init(void)
37 : : {
38 : 0 : cl = cmdline_stdin_new(modules_ctx, "");
39 : 0 : }
40 : :
41 : : void
42 : 0 : cli_exit(void)
43 : : {
44 : 0 : cmdline_stdin_exit(cl);
45 : 0 : }
46 : :
47 : : void
48 : 0 : cli_process(char *in, char *out, size_t out_size, __rte_unused void *obj)
49 : : {
50 : : int rc;
51 : :
52 : 0 : if (is_comment(in))
53 : : return;
54 : :
55 : 0 : rc = cmdline_parse(cl, in);
56 : 0 : if (rc == CMDLINE_PARSE_AMBIGUOUS)
57 : : snprintf(out, out_size, MSG_CMD_FAIL, "Ambiguous command");
58 : 0 : else if (rc == CMDLINE_PARSE_NOMATCH)
59 : : snprintf(out, out_size, MSG_CMD_FAIL, "Command mismatch");
60 : 0 : else if (rc == CMDLINE_PARSE_BAD_ARGS)
61 : : snprintf(out, out_size, MSG_CMD_FAIL, "Bad arguments");
62 : :
63 : : return;
64 : :
65 : : }
66 : :
67 : : int
68 : 0 : cli_script_process(const char *file_name, size_t msg_in_len_max, size_t msg_out_len_max, void *obj)
69 : : {
70 : : char *msg_in = NULL, *msg_out = NULL;
71 : : int rc = -EINVAL;
72 : : FILE *f = NULL;
73 : :
74 : : /* Check input arguments */
75 : 0 : if ((file_name == NULL) || (strlen(file_name) == 0) || (msg_in_len_max == 0) ||
76 : 0 : (msg_out_len_max == 0))
77 : : return rc;
78 : :
79 : 0 : msg_in = malloc(msg_in_len_max + 1);
80 : 0 : msg_out = malloc(msg_out_len_max + 1);
81 : 0 : if ((msg_in == NULL) || (msg_out == NULL)) {
82 : : rc = -ENOMEM;
83 : 0 : goto exit;
84 : : }
85 : :
86 : : /* Open input file */
87 : 0 : f = fopen(file_name, "r");
88 : 0 : if (f == NULL) {
89 : : rc = -EIO;
90 : 0 : goto exit;
91 : : }
92 : :
93 : : /* Read file */
94 : 0 : while (fgets(msg_in, msg_in_len_max, f) != NULL) {
95 : 0 : msg_out[0] = 0;
96 : :
97 : 0 : cli_process(msg_in, msg_out, msg_out_len_max, obj);
98 : :
99 : 0 : if (strlen(msg_out))
100 : : printf("%s", msg_out);
101 : : }
102 : :
103 : : /* Close file */
104 : 0 : fclose(f);
105 : : rc = 0;
106 : :
107 : 0 : exit:
108 : 0 : free(msg_out);
109 : 0 : free(msg_in);
110 : 0 : return rc;
111 : : }
|