examples/ip_pipeline: add CLI interface
[dpdk.git] / examples / ip_pipeline / cli.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <rte_common.h>
11
12 #include "cli.h"
13
14 static int
15 is_comment(char *in)
16 {
17         if ((strlen(in) && index("!#%;", in[0])) ||
18                 (strncmp(in, "//", 2) == 0) ||
19                 (strncmp(in, "--", 2) == 0))
20                 return 1;
21
22         return 0;
23 }
24
25 void
26 cli_process(char *in, char *out __rte_unused, size_t out_size __rte_unused)
27 {
28         if (is_comment(in))
29                 return;
30
31 }
32
33 int
34 cli_script_process(const char *file_name,
35         size_t msg_in_len_max,
36         size_t msg_out_len_max)
37 {
38         char *msg_in = NULL, *msg_out = NULL;
39         FILE *f = NULL;
40
41         /* Check input arguments */
42         if ((file_name == NULL) ||
43                 (strlen(file_name) == 0) ||
44                 (msg_in_len_max == 0) ||
45                 (msg_out_len_max == 0))
46                 return -EINVAL;
47
48         msg_in = malloc(msg_in_len_max + 1);
49         msg_out = malloc(msg_out_len_max + 1);
50         if ((msg_in == NULL) ||
51                 (msg_out == NULL)) {
52                 free(msg_out);
53                 free(msg_in);
54                 return -ENOMEM;
55         }
56
57         /* Open input file */
58         f = fopen(file_name, "r");
59         if (f == NULL) {
60                 free(msg_out);
61                 free(msg_in);
62                 return -EIO;
63         }
64
65         /* Read file */
66         for ( ; ; ) {
67                 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
68                         break;
69
70                 printf("%s", msg_in);
71                 msg_out[0] = 0;
72
73                 cli_process(msg_in,
74                         msg_out,
75                         msg_out_len_max);
76
77                 if (strlen(msg_out))
78                         printf("%s", msg_out);
79         }
80
81         /* Close file */
82         fclose(f);
83         free(msg_out);
84         free(msg_in);
85         return 0;
86 }