New upstream version 18.08
[deb_dpdk.git] / lib / librte_kvargs / rte_kvargs.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2013 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <string.h>
7 #include <stdlib.h>
8
9 #include <rte_string_fns.h>
10
11 #include "rte_kvargs.h"
12
13 /*
14  * Receive a string with a list of arguments following the pattern
15  * key=value,key=value,... and insert them into the list.
16  * strtok() is used so the params string will be copied to be modified.
17  */
18 static int
19 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
20 {
21         unsigned i;
22         char *str;
23         char *ctx1 = NULL;
24         char *ctx2 = NULL;
25
26         /* Copy the const char *params to a modifiable string
27          * to pass to rte_strsplit
28          */
29         kvlist->str = strdup(params);
30         if (kvlist->str == NULL)
31                 return -1;
32
33         /* browse each key/value pair and add it in kvlist */
34         str = kvlist->str;
35         while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
36
37                 i = kvlist->count;
38                 if (i >= RTE_KVARGS_MAX)
39                         return -1;
40
41                 kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2);
42                 kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2);
43                 if (kvlist->pairs[i].key == NULL ||
44                     kvlist->pairs[i].value == NULL)
45                         return -1;
46
47                 kvlist->count++;
48                 str = NULL;
49         }
50
51         return 0;
52 }
53
54 /*
55  * Determine whether a key is valid or not by looking
56  * into a list of valid keys.
57  */
58 static int
59 is_valid_key(const char * const valid[], const char *key_match)
60 {
61         const char * const *valid_ptr;
62
63         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
64                 if (strcmp(key_match, *valid_ptr) == 0)
65                         return 1;
66         }
67         return 0;
68 }
69
70 /*
71  * Determine whether all keys are valid or not by looking
72  * into a list of valid keys.
73  */
74 static int
75 check_for_valid_keys(struct rte_kvargs *kvlist,
76                 const char * const valid[])
77 {
78         unsigned i, ret;
79         struct rte_kvargs_pair *pair;
80
81         for (i = 0; i < kvlist->count; i++) {
82                 pair = &kvlist->pairs[i];
83                 ret = is_valid_key(valid, pair->key);
84                 if (!ret)
85                         return -1;
86         }
87         return 0;
88 }
89
90 /*
91  * Return the number of times a given arg_name exists in the key/value list.
92  * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
93  * arg "rx" will be 2.
94  */
95 unsigned
96 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
97 {
98         const struct rte_kvargs_pair *pair;
99         unsigned i, ret;
100
101         ret = 0;
102         for (i = 0; i < kvlist->count; i++) {
103                 pair = &kvlist->pairs[i];
104                 if (key_match == NULL || strcmp(pair->key, key_match) == 0)
105                         ret++;
106         }
107
108         return ret;
109 }
110
111 /*
112  * For each matching key, call the given handler function.
113  */
114 int
115 rte_kvargs_process(const struct rte_kvargs *kvlist,
116                 const char *key_match,
117                 arg_handler_t handler,
118                 void *opaque_arg)
119 {
120         const struct rte_kvargs_pair *pair;
121         unsigned i;
122
123         for (i = 0; i < kvlist->count; i++) {
124                 pair = &kvlist->pairs[i];
125                 if (key_match == NULL || strcmp(pair->key, key_match) == 0) {
126                         if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
127                                 return -1;
128                 }
129         }
130         return 0;
131 }
132
133 /* free the rte_kvargs structure */
134 void
135 rte_kvargs_free(struct rte_kvargs *kvlist)
136 {
137         if (!kvlist)
138                 return;
139
140         free(kvlist->str);
141         free(kvlist);
142 }
143
144 /*
145  * Parse the arguments "key=value,key=value,..." string and return
146  * an allocated structure that contains a key/value list. Also
147  * check if only valid keys were used.
148  */
149 struct rte_kvargs *
150 rte_kvargs_parse(const char *args, const char * const valid_keys[])
151 {
152         struct rte_kvargs *kvlist;
153
154         kvlist = malloc(sizeof(*kvlist));
155         if (kvlist == NULL)
156                 return NULL;
157         memset(kvlist, 0, sizeof(*kvlist));
158
159         if (rte_kvargs_tokenize(kvlist, args) < 0) {
160                 rte_kvargs_free(kvlist);
161                 return NULL;
162         }
163
164         if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
165                 rte_kvargs_free(kvlist);
166                 return NULL;
167         }
168
169         return kvlist;
170 }
171
172 __rte_experimental
173 struct rte_kvargs *
174 rte_kvargs_parse_delim(const char *args, const char * const valid_keys[],
175                        const char *valid_ends)
176 {
177         struct rte_kvargs *kvlist = NULL;
178         char *copy;
179         size_t len;
180
181         if (valid_ends == NULL)
182                 return rte_kvargs_parse(args, valid_keys);
183
184         copy = strdup(args);
185         if (copy == NULL)
186                 return NULL;
187
188         len = strcspn(copy, valid_ends);
189         copy[len] = '\0';
190
191         kvlist = rte_kvargs_parse(copy, valid_keys);
192
193         free(copy);
194         return kvlist;
195 }
196
197 __rte_experimental
198 int
199 rte_kvargs_strcmp(const char *key __rte_unused,
200                   const char *value, void *opaque)
201 {
202         const char *str = opaque;
203
204         return -abs(strcmp(str, value));
205 }