kvargs: simpler parsing and allow duplicated keys
[dpdk.git] / lib / librte_kvargs / rte_kvargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include <rte_log.h>
38 #include <rte_string_fns.h>
39
40 #include "rte_kvargs.h"
41
42 /*
43  * Receive a string with a list of arguments following the pattern
44  * key=value;key=value;... and insert them into the list.
45  * strtok() is used so the params string will be copied to be modified.
46  */
47 static int
48 rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
49 {
50         unsigned i;
51         char *str, *ctx1, *ctx2;
52
53         /* Copy the const char *params to a modifiable string
54          * to pass to rte_strsplit
55          */
56         kvlist->str = strdup(params);
57         if (kvlist->str == NULL) {
58                 RTE_LOG(ERR, PMD, "Cannot parse arguments: not enough memory\n");
59                 return -1;
60         }
61
62         /* browse each key/value pair and add it in kvlist */
63         str = kvlist->str;
64         while ((str = strtok_r(str, RTE_KVARGS_PAIRS_DELIM, &ctx1)) != NULL) {
65
66                 i = kvlist->count;
67                 if (i >= RTE_KVARGS_MAX) {
68                         RTE_LOG(ERR, PMD, "Cannot parse arguments: list full\n");
69                         return -1;
70                 }
71
72                 kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2);
73                 kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2);
74                 if (kvlist->pairs[i].key == NULL || kvlist->pairs[i].value == NULL) {
75                         RTE_LOG(ERR, PMD,
76                                 "Cannot parse arguments: wrong key or value\n"
77                                 "params=<%s>\n", params);
78                         return -1;
79                 }
80
81                 kvlist->count++;
82                 str = NULL;
83         }
84
85         return 0;
86 }
87
88 /*
89  * Determine whether a key is valid or not by looking
90  * into a list of valid keys.
91  */
92 static int
93 is_valid_key(const char *valid[], const char *key_match)
94 {
95         const char **valid_ptr;
96
97         for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++)
98                 if (strstr(key_match, *valid_ptr) != NULL)
99                         return 1;
100         return 0;
101 }
102
103 /*
104  * Determine whether all keys are valid or not by looking
105  * into a list of valid keys.
106  */
107 static int
108 check_for_valid_keys(struct rte_kvargs *kvlist,
109                 const char *valid[])
110 {
111         unsigned i, ret;
112         struct rte_kvargs_pair *pair;
113
114         for (i = 0; i < kvlist->count; i++) {
115                 pair = &kvlist->pairs[i];
116                 ret = is_valid_key(valid, pair->key);
117                 if (!ret) {
118                         RTE_LOG(ERR, PMD,
119                                 "Error parsing device, invalid key <%s>\n",
120                                 pair->key);
121                         return -1;
122                 }
123         }
124         return 0;
125 }
126
127 /*
128  * Return the number of times a given arg_name exists in the key/value list.
129  * E.g. given a list = { rx = 0, rx = 1, tx = 2 } the number of args for
130  * arg "rx" will be 2.
131  */
132 unsigned
133 rte_kvargs_count(const struct rte_kvargs *kvlist, const char *key_match)
134 {
135         const struct rte_kvargs_pair *pair;
136         unsigned i, ret;
137
138         ret = 0;
139         for (i = 0; i < kvlist->count; i++) {
140                 pair = &kvlist->pairs[i];
141                 if (strcmp(pair->key, key_match) == 0)
142                         ret++;
143         }
144
145         return ret;
146 }
147
148 /*
149  * For each matching key, call the given handler function.
150  */
151 int
152 rte_kvargs_process(const struct rte_kvargs *kvlist,
153                 const char *key_match,
154                 arg_handler_t handler,
155                 void *opaque_arg)
156 {
157         const struct rte_kvargs_pair *pair;
158         unsigned i;
159
160         for (i = 0; i < kvlist->count; i++) {
161                 pair = &kvlist->pairs[i];
162                 if (strstr(pair->key, key_match) != NULL) {
163                         if ((*handler)(pair->value, opaque_arg) < 0)
164                                 return -1;
165                 }
166         }
167         return 0;
168 }
169
170 /* free the rte_kvargs structure */
171 void
172 rte_kvargs_free(struct rte_kvargs *kvlist)
173 {
174         if (kvlist->str != NULL)
175                 free(kvlist->str);
176         free(kvlist);
177 }
178
179 /*
180  * Parse the arguments "key=value;key=value;..." string and return
181  * an allocated structure that contains a key/value list. Also
182  * check if only valid keys were used.
183  */
184 struct rte_kvargs *
185 rte_kvargs_parse(const char *args, const char *valid_keys[])
186 {
187         struct rte_kvargs *kvlist;
188
189         kvlist = malloc(sizeof(*kvlist));
190         if (kvlist == NULL)
191                 return NULL;
192         memset(kvlist, 0, sizeof(*kvlist));
193
194         if (rte_kvargs_tokenize(kvlist, args) < 0) {
195                 rte_kvargs_free(kvlist);
196                 return NULL;
197         }
198
199         if (valid_keys != NULL && check_for_valid_keys(kvlist, valid_keys) < 0) {
200                 rte_kvargs_free(kvlist);
201                 return NULL;
202         }
203
204         return kvlist;
205 }