1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2013 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
9 #include <rte_string_fns.h>
11 #include "rte_kvargs.h"
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.
19 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
26 /* Copy the const char *params to a modifiable string
27 * to pass to rte_strsplit
29 kvlist->str = strdup(params);
30 if (kvlist->str == NULL)
33 /* browse each key/value pair and add it in kvlist */
35 while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
38 if (i >= RTE_KVARGS_MAX)
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)
47 /* Detect list [a,b] to skip comma delimiter in list. */
48 str = kvlist->pairs[i].value;
50 /* Find the end of the list. */
51 while (str[strlen(str) - 1] != ']') {
52 /* Restore the comma erased by strtok_r(). */
53 if (ctx1 == NULL || ctx1[0] == '\0')
54 return -1; /* no closing bracket */
55 str[strlen(str)] = ',';
56 /* Parse until next comma. */
57 str = strtok_r(NULL, RTE_KVARGS_PAIRS_DELIM, &ctx1);
59 return -1; /* no closing bracket */
71 * Determine whether a key is valid or not by looking
72 * into a list of valid keys.
75 is_valid_key(const char * const valid[], const char *key_match)
77 const char * const *valid_ptr;
79 for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) {
80 if (strcmp(key_match, *valid_ptr) == 0)
87 * Determine whether all keys are valid or not by looking
88 * into a list of valid keys.
91 check_for_valid_keys(struct rte_kvargs *kvlist,
92 const char * const valid[])
95 struct rte_kvargs_pair *pair;
97 for (i = 0; i < kvlist->count; i++) {
98 pair = &kvlist->pairs[i];
99 ret = is_valid_key(valid, pair->key);
107 * Return the number of times a given arg_name exists in the key/value list.
108 * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
109 * arg "rx" will be 2.
112 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
114 const struct rte_kvargs_pair *pair;
118 for (i = 0; i < kvlist->count; i++) {
119 pair = &kvlist->pairs[i];
120 if (key_match == NULL || strcmp(pair->key, key_match) == 0)
128 * For each matching key, call the given handler function.
131 rte_kvargs_process(const struct rte_kvargs *kvlist,
132 const char *key_match,
133 arg_handler_t handler,
136 const struct rte_kvargs_pair *pair;
142 for (i = 0; i < kvlist->count; i++) {
143 pair = &kvlist->pairs[i];
144 if (key_match == NULL || strcmp(pair->key, key_match) == 0) {
145 if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
152 /* free the rte_kvargs structure */
154 rte_kvargs_free(struct rte_kvargs *kvlist)
164 * Parse the arguments "key=value,key=value,..." string and return
165 * an allocated structure that contains a key/value list. Also
166 * check if only valid keys were used.
169 rte_kvargs_parse(const char *args, const char * const valid_keys[])
171 struct rte_kvargs *kvlist;
173 kvlist = malloc(sizeof(*kvlist));
176 memset(kvlist, 0, sizeof(*kvlist));
178 if (rte_kvargs_tokenize(kvlist, args) < 0) {
179 rte_kvargs_free(kvlist);
183 if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
184 rte_kvargs_free(kvlist);
192 rte_kvargs_parse_delim(const char *args, const char * const valid_keys[],
193 const char *valid_ends)
195 struct rte_kvargs *kvlist = NULL;
199 if (valid_ends == NULL)
200 return rte_kvargs_parse(args, valid_keys);
206 len = strcspn(copy, valid_ends);
209 kvlist = rte_kvargs_parse(copy, valid_keys);
216 rte_kvargs_strcmp(const char *key __rte_unused,
217 const char *value, void *opaque)
219 const char *str = opaque;
221 return -abs(strcmp(str, value));