fe7f2ab4e5a50ed21368ebe561f1dd9fcaab6606
[dpdk.git] / lib / librte_eal / common / rte_option.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation.
3  */
4
5 #include <getopt.h>
6 #include <unistd.h>
7 #include <string.h>
8
9 #include <rte_eal.h>
10 #include <rte_option.h>
11
12 #include "eal_private.h"
13 #include "eal_internal_cfg.h" /* Necessary for eal_options.h */
14 #include "eal_options.h"
15
16 TAILQ_HEAD(rte_option_list, rte_option);
17
18 struct rte_option_list rte_option_list =
19         TAILQ_HEAD_INITIALIZER(rte_option_list);
20
21 int
22 rte_option_parse(const char *opt)
23 {
24         struct rte_option *option;
25
26         if (strlen(opt) <= 2 ||
27             strncmp(opt, "--", 2))
28                 return -1;
29
30         /* Check if the option is registered */
31         TAILQ_FOREACH(option, &rte_option_list, next) {
32                 if (strcmp(&opt[2], option->name) == 0) {
33                         option->enabled = 1;
34                         return 0;
35                 }
36         }
37
38         return -1;
39 }
40
41 __rte_experimental
42 int
43 rte_option_register(struct rte_option *opt)
44 {
45         struct rte_option *option;
46         const struct option *gopt;
47
48         gopt = &eal_long_options[0];
49         while (gopt->name != NULL) {
50                 if (strcmp(gopt->name, opt->name) == 0) {
51                         RTE_LOG(ERR, EAL, "Option %s is already a common EAL option.\n",
52                                         opt->name);
53                         return -1;
54                 }
55                 gopt++;
56         }
57
58         TAILQ_FOREACH(option, &rte_option_list, next) {
59                 if (strcmp(opt->name, option->name) == 0) {
60                         RTE_LOG(ERR, EAL, "Option %s has already been registered.\n",
61                                         opt->name);
62                         return -1;
63                 }
64         }
65
66         TAILQ_INSERT_HEAD(&rte_option_list, opt, next);
67         return 0;
68 }
69
70 void
71 rte_option_init(void)
72 {
73         struct rte_option *option;
74
75         TAILQ_FOREACH(option, &rte_option_list, next) {
76                 if (option->enabled)
77                         option->cb();
78         }
79 }
80
81 void
82 rte_option_usage(void)
83 {
84         struct rte_option *option;
85         int opt_count = 0;
86
87         TAILQ_FOREACH(option, &rte_option_list, next)
88                 opt_count += 1;
89         if (opt_count == 0)
90                 return;
91
92         printf("EAL dynamic options:\n");
93         TAILQ_FOREACH(option, &rte_option_list, next)
94                 printf("  --%-*s %s\n", 17, option->name, option->usage);
95         printf("\n");
96 }