]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/lib/librte_cfgfile/rte_cfgfile.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / dpdk / lib / librte_cfgfile / rte_cfgfile.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <rte_common.h>
39 #include <rte_string_fns.h>
40
41 #include "rte_cfgfile.h"
42
43 struct rte_cfgfile_section {
44 char name[CFG_NAME_LEN];
45 int num_entries;
46 struct rte_cfgfile_entry *entries[0];
47 };
48
49 struct rte_cfgfile {
50 int flags;
51 int num_sections;
52 struct rte_cfgfile_section *sections[0];
53 };
54
55 /** when we resize a file structure, how many extra entries
56 * for new sections do we add in */
57 #define CFG_ALLOC_SECTION_BATCH 8
58 /** when we resize a section structure, how many extra entries
59 * for new entries do we add in */
60 #define CFG_ALLOC_ENTRY_BATCH 16
61
62 /**
63 * Default cfgfile load parameters.
64 */
65 static const struct rte_cfgfile_parameters default_cfgfile_params = {
66 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
67 };
68
69 /**
70 * Defines the list of acceptable comment characters supported by this
71 * library.
72 */
73 static const char valid_comment_chars[] = {
74 '!',
75 '#',
76 '%',
77 ';',
78 '@'
79 };
80
81 static unsigned
82 _strip(char *str, unsigned len)
83 {
84 int newlen = len;
85 if (len == 0)
86 return 0;
87
88 if (isspace(str[len-1])) {
89 /* strip trailing whitespace */
90 while (newlen > 0 && isspace(str[newlen - 1]))
91 str[--newlen] = '\0';
92 }
93
94 if (isspace(str[0])) {
95 /* strip leading whitespace */
96 int i, start = 1;
97 while (isspace(str[start]) && start < newlen)
98 start++
99 ; /* do nothing */
100 newlen -= start;
101 for (i = 0; i < newlen; i++)
102 str[i] = str[i+start];
103 str[i] = '\0';
104 }
105 return newlen;
106 }
107
108 static int
109 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
110 {
111 unsigned int valid_comment;
112 unsigned int i;
113
114 if (!params) {
115 printf("Error - missing cfgfile parameters\n");
116 return -EINVAL;
117 }
118
119 valid_comment = 0;
120 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
121 if (params->comment_character == valid_comment_chars[i]) {
122 valid_comment = 1;
123 break;
124 }
125 }
126
127 if (valid_comment == 0) {
128 printf("Error - invalid comment characters %c\n",
129 params->comment_character);
130 return -ENOTSUP;
131 }
132
133 return 0;
134 }
135
136 struct rte_cfgfile *
137 rte_cfgfile_load(const char *filename, int flags)
138 {
139 return rte_cfgfile_load_with_params(filename, flags,
140 &default_cfgfile_params);
141 }
142
143 struct rte_cfgfile *
144 rte_cfgfile_load_with_params(const char *filename, int flags,
145 const struct rte_cfgfile_parameters *params)
146 {
147 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
148 int allocated_entries = 0;
149 int curr_section = -1;
150 int curr_entry = -1;
151 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
152 int lineno = 0;
153 struct rte_cfgfile *cfg = NULL;
154
155 if (rte_cfgfile_check_params(params))
156 return NULL;
157
158 FILE *f = fopen(filename, "r");
159 if (f == NULL)
160 return NULL;
161
162 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
163 allocated_sections);
164 if (cfg == NULL)
165 goto error2;
166
167 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
168
169 if (flags & CFG_FLAG_GLOBAL_SECTION) {
170 curr_section = 0;
171 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
172 cfg->sections[curr_section] = malloc(
173 sizeof(*cfg->sections[0]) +
174 sizeof(cfg->sections[0]->entries[0]) *
175 allocated_entries);
176 if (cfg->sections[curr_section] == NULL) {
177 printf("Error - no memory for global section\n");
178 goto error1;
179 }
180
181 snprintf(cfg->sections[curr_section]->name,
182 sizeof(cfg->sections[0]->name), "GLOBAL");
183 }
184
185 while (fgets(buffer, sizeof(buffer), f) != NULL) {
186 char *pos = NULL;
187 size_t len = strnlen(buffer, sizeof(buffer));
188 lineno++;
189 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
190 printf("Error line %d - no \\n found on string. "
191 "Check if line too long\n", lineno);
192 goto error1;
193 }
194 pos = memchr(buffer, params->comment_character, len);
195 if (pos != NULL) {
196 *pos = '\0';
197 len = pos - buffer;
198 }
199
200 len = _strip(buffer, len);
201 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
202 continue;
203
204 if (buffer[0] == '[') {
205 /* section heading line */
206 char *end = memchr(buffer, ']', len);
207 if (end == NULL) {
208 printf("Error line %d - no terminating '['"
209 "character found\n", lineno);
210 goto error1;
211 }
212 *end = '\0';
213 _strip(&buffer[1], end - &buffer[1]);
214
215 /* close off old section and add start new one */
216 if (curr_section >= 0)
217 cfg->sections[curr_section]->num_entries =
218 curr_entry + 1;
219 curr_section++;
220
221 /* resize overall struct if we don't have room for more
222 sections */
223 if (curr_section == allocated_sections) {
224 allocated_sections += CFG_ALLOC_SECTION_BATCH;
225 struct rte_cfgfile *n_cfg = realloc(cfg,
226 sizeof(*cfg) + sizeof(cfg->sections[0])
227 * allocated_sections);
228 if (n_cfg == NULL) {
229 curr_section--;
230 printf("Error - no more memory\n");
231 goto error1;
232 }
233 cfg = n_cfg;
234 }
235
236 /* allocate space for new section */
237 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
238 curr_entry = -1;
239 cfg->sections[curr_section] = malloc(
240 sizeof(*cfg->sections[0]) +
241 sizeof(cfg->sections[0]->entries[0]) *
242 allocated_entries);
243 if (cfg->sections[curr_section] == NULL) {
244 printf("Error - no more memory\n");
245 goto error1;
246 }
247
248 snprintf(cfg->sections[curr_section]->name,
249 sizeof(cfg->sections[0]->name),
250 "%s", &buffer[1]);
251 } else {
252 /* value line */
253 if (curr_section < 0) {
254 printf("Error line %d - value outside of"
255 "section\n", lineno);
256 goto error1;
257 }
258
259 struct rte_cfgfile_section *sect =
260 cfg->sections[curr_section];
261 int n;
262 char *split[2] = {NULL};
263 n = rte_strsplit(buffer, sizeof(buffer), split, 2, '=');
264 if (flags & CFG_FLAG_EMPTY_VALUES) {
265 if ((n < 1) || (n > 2)) {
266 printf("Error at line %d - cannot split string, n=%d\n",
267 lineno, n);
268 goto error1;
269 }
270 } else {
271 if (n != 2) {
272 printf("Error at line %d - cannot split string, n=%d\n",
273 lineno, n);
274 goto error1;
275 }
276 }
277
278 curr_entry++;
279 if (curr_entry == allocated_entries) {
280 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
281 struct rte_cfgfile_section *n_sect = realloc(
282 sect, sizeof(*sect) +
283 sizeof(sect->entries[0]) *
284 allocated_entries);
285 if (n_sect == NULL) {
286 curr_entry--;
287 printf("Error - no more memory\n");
288 goto error1;
289 }
290 sect = cfg->sections[curr_section] = n_sect;
291 }
292
293 sect->entries[curr_entry] = malloc(
294 sizeof(*sect->entries[0]));
295 if (sect->entries[curr_entry] == NULL) {
296 printf("Error - no more memory\n");
297 goto error1;
298 }
299
300 struct rte_cfgfile_entry *entry = sect->entries[
301 curr_entry];
302 snprintf(entry->name, sizeof(entry->name), "%s",
303 split[0]);
304 snprintf(entry->value, sizeof(entry->value), "%s",
305 split[1] ? split[1] : "");
306 _strip(entry->name, strnlen(entry->name,
307 sizeof(entry->name)));
308 _strip(entry->value, strnlen(entry->value,
309 sizeof(entry->value)));
310 }
311 }
312 fclose(f);
313 cfg->flags = flags;
314 cfg->num_sections = curr_section + 1;
315 /* curr_section will still be -1 if we have an empty file */
316 if (curr_section >= 0)
317 cfg->sections[curr_section]->num_entries = curr_entry + 1;
318 return cfg;
319
320 error1:
321 cfg->num_sections = curr_section + 1;
322 if (curr_section >= 0)
323 cfg->sections[curr_section]->num_entries = curr_entry + 1;
324 rte_cfgfile_close(cfg);
325 error2:
326 fclose(f);
327 return NULL;
328 }
329
330
331 int rte_cfgfile_close(struct rte_cfgfile *cfg)
332 {
333 int i, j;
334
335 if (cfg == NULL)
336 return -1;
337
338 for (i = 0; i < cfg->num_sections; i++) {
339 if (cfg->sections[i] != NULL) {
340 if (cfg->sections[i]->num_entries) {
341 for (j = 0; j < cfg->sections[i]->num_entries;
342 j++) {
343 if (cfg->sections[i]->entries[j] !=
344 NULL)
345 free(cfg->sections[i]->
346 entries[j]);
347 }
348 }
349 free(cfg->sections[i]);
350 }
351 }
352 free(cfg);
353
354 return 0;
355 }
356
357 int
358 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
359 size_t length)
360 {
361 int i;
362 int num_sections = 0;
363 for (i = 0; i < cfg->num_sections; i++) {
364 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
365 num_sections++;
366 }
367 return num_sections;
368 }
369
370 int
371 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
372 int max_sections)
373 {
374 int i;
375
376 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
377 snprintf(sections[i], CFG_NAME_LEN, "%s",
378 cfg->sections[i]->name);
379
380 return i;
381 }
382
383 static const struct rte_cfgfile_section *
384 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
385 {
386 int i;
387 for (i = 0; i < cfg->num_sections; i++) {
388 if (strncmp(cfg->sections[i]->name, sectionname,
389 sizeof(cfg->sections[0]->name)) == 0)
390 return cfg->sections[i];
391 }
392 return NULL;
393 }
394
395 int
396 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
397 {
398 return _get_section(cfg, sectionname) != NULL;
399 }
400
401 int
402 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
403 const char *sectionname)
404 {
405 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
406 if (s == NULL)
407 return -1;
408 return s->num_entries;
409 }
410
411
412 int
413 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
414 struct rte_cfgfile_entry *entries, int max_entries)
415 {
416 int i;
417 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
418 if (sect == NULL)
419 return -1;
420 for (i = 0; i < max_entries && i < sect->num_entries; i++)
421 entries[i] = *sect->entries[i];
422 return i;
423 }
424
425 int
426 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
427 char *sectionname,
428 struct rte_cfgfile_entry *entries, int max_entries)
429 {
430 int i;
431 const struct rte_cfgfile_section *sect;
432
433 if (index < 0 || index >= cfg->num_sections)
434 return -1;
435
436 sect = cfg->sections[index];
437 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
438 for (i = 0; i < max_entries && i < sect->num_entries; i++)
439 entries[i] = *sect->entries[i];
440 return i;
441 }
442
443 const char *
444 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
445 const char *entryname)
446 {
447 int i;
448 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
449 if (sect == NULL)
450 return NULL;
451 for (i = 0; i < sect->num_entries; i++)
452 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
453 == 0)
454 return sect->entries[i]->value;
455 return NULL;
456 }
457
458 int
459 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
460 const char *entryname)
461 {
462 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
463 }