eal: check against common option on register
[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 void __rte_experimental
42 rte_option_register(struct rte_option *opt)
43 {
44         struct rte_option *option;
45         const struct option *gopt;
46
47         gopt = &eal_long_options[0];
48         while (gopt->name != NULL) {
49                 if (strcmp(gopt->name, opt->name) == 0) {
50                         RTE_LOG(ERR, EAL, "Option %s is already a common EAL option.\n",
51                                         opt->name);
52                         return;
53                 }
54                 gopt++;
55         }
56
57         TAILQ_FOREACH(option, &rte_option_list, next) {
58                 if (strcmp(opt->name, option->name) == 0) {
59                         RTE_LOG(INFO, EAL, "Option %s has already been registered.\n",
60                                         opt->name);
61                         return;
62                 }
63         }
64
65         TAILQ_INSERT_HEAD(&rte_option_list, opt, next);
66 }
67
68 void
69 rte_option_init(void)
70 {
71         struct rte_option *option;
72
73         TAILQ_FOREACH(option, &rte_option_list, next) {
74                 if (option->enabled)
75                         option->cb();
76         }
77 }
78
79 void
80 rte_option_usage(void)
81 {
82         struct rte_option *option;
83         int opt_count = 0;
84
85         TAILQ_FOREACH(option, &rte_option_list, next)
86                 opt_count += 1;
87         if (opt_count == 0)
88                 return;
89
90         printf("EAL dynamic options:\n");
91         TAILQ_FOREACH(option, &rte_option_list, next)
92                 printf("  --%-*s %s\n", 17, option->name, option->usage);
93         printf("\n");
94 }