1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2013 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
10 #include <rte_os_shim.h>
11 #include <rte_string_fns.h>
13 #include "rte_kvargs.h"
16 * Receive a string with a list of arguments following the pattern
17 * key=value,key=value,... and insert them into the list.
18 * Params string will be copied to be modified.
19 * list "[]" and list element splitter ",", "-" is treated as value.
23 * k1=x[0-1]y[1,3-5,9]z
26 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
29 bool in_list = false, end_key = false, end_value = false;
30 bool save = false, end_pair = false;
32 /* Copy the const char *params to a modifiable string
33 * to pass to rte_strsplit
35 kvlist->str = strdup(params);
36 if (kvlist->str == NULL)
39 /* browse each key/value pair and add it in kvlist */
41 start = str; /* start of current key or value */
44 case '=': /* End of key. */
49 /* End of value, skip comma in middle of range */
59 case '[': /* Start of list. */
62 case ']': /* End of list. */
66 case '\0': /* End of string */
79 /* Continue if not end of key or value. */
84 if (kvlist->count >= RTE_KVARGS_MAX)
89 kvlist->pairs[kvlist->count].value = start;
92 kvlist->pairs[kvlist->count].key = start;
95 if (end_value || str != start)
96 /* Ignore empty pair. */
103 if (*str == '\0') /* End of string. */
115 * Determine whether a key is valid or not by looking
116 * into a list of valid keys.
119 is_valid_key(const char * const valid[], const char *key_match)
121 const char * const *valid_ptr;
123 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
124 if (strcmp(key_match, *valid_ptr) == 0)
131 * Determine whether all keys are valid or not by looking
132 * into a list of valid keys.
135 check_for_valid_keys(struct rte_kvargs *kvlist,
136 const char * const valid[])
139 struct rte_kvargs_pair *pair;
141 for (i = 0; i < kvlist->count; i++) {
142 pair = &kvlist->pairs[i];
143 ret = is_valid_key(valid, pair->key);
151 * Return the number of times a given arg_name exists in the key/value list.
152 * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
153 * arg "rx" will be 2.
156 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
158 const struct rte_kvargs_pair *pair;
162 for (i = 0; i < kvlist->count; i++) {
163 pair = &kvlist->pairs[i];
164 if (key_match == NULL || strcmp(pair->key, key_match) == 0)
172 * For each matching key, call the given handler function.
175 rte_kvargs_process(const struct rte_kvargs *kvlist,
176 const char *key_match,
177 arg_handler_t handler,
180 const struct rte_kvargs_pair *pair;
186 for (i = 0; i < kvlist->count; i++) {
187 pair = &kvlist->pairs[i];
188 if (key_match == NULL || strcmp(pair->key, key_match) == 0) {
189 if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
196 /* free the rte_kvargs structure */
198 rte_kvargs_free(struct rte_kvargs *kvlist)
207 /* Lookup a value in an rte_kvargs list by its key and value. */
209 rte_kvargs_get_with_value(const struct rte_kvargs *kvlist, const char *key,
216 for (i = 0; i < kvlist->count; ++i) {
217 if (key != NULL && strcmp(kvlist->pairs[i].key, key) != 0)
219 if (value != NULL && strcmp(kvlist->pairs[i].value, value) != 0)
221 return kvlist->pairs[i].value;
226 /* Lookup a value in an rte_kvargs list by its key. */
228 rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
230 if (kvlist == NULL || key == NULL)
232 return rte_kvargs_get_with_value(kvlist, key, NULL);
236 * Parse the arguments "key=value,key=value,..." string and return
237 * an allocated structure that contains a key/value list. Also
238 * check if only valid keys were used.
241 rte_kvargs_parse(const char *args, const char * const valid_keys[])
243 struct rte_kvargs *kvlist;
245 kvlist = malloc(sizeof(*kvlist));
248 memset(kvlist, 0, sizeof(*kvlist));
250 if (rte_kvargs_tokenize(kvlist, args) < 0) {
251 rte_kvargs_free(kvlist);
255 if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
256 rte_kvargs_free(kvlist);
264 rte_kvargs_parse_delim(const char *args, const char * const valid_keys[],
265 const char *valid_ends)
267 struct rte_kvargs *kvlist = NULL;
271 if (valid_ends == NULL)
272 return rte_kvargs_parse(args, valid_keys);
278 len = strcspn(copy, valid_ends);
281 kvlist = rte_kvargs_parse(copy, valid_keys);