1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_string_fns.h>
11 #include <rte_common.h>
14 #include "rte_cfgfile.h"
16 struct rte_cfgfile_section {
17 char name[CFG_NAME_LEN];
19 int allocated_entries;
20 struct rte_cfgfile_entry *entries;
26 int allocated_sections;
27 struct rte_cfgfile_section *sections;
30 static int cfgfile_logtype;
32 #define CFG_LOG(level, fmt, args...) \
33 rte_log(RTE_LOG_ ## level, cfgfile_logtype, "%s(): " fmt "\n", \
36 /** when we resize a file structure, how many extra entries
37 * for new sections do we add in */
38 #define CFG_ALLOC_SECTION_BATCH 8
39 /** when we resize a section structure, how many extra entries
40 * for new entries do we add in */
41 #define CFG_ALLOC_ENTRY_BATCH 16
44 * Default cfgfile load parameters.
46 static const struct rte_cfgfile_parameters default_cfgfile_params = {
47 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
51 * Defines the list of acceptable comment characters supported by this
54 static const char valid_comment_chars[] = {
63 _strip(char *str, unsigned len)
69 if (isspace(str[len-1])) {
70 /* strip trailing whitespace */
71 while (newlen > 0 && isspace(str[newlen - 1]))
75 if (isspace(str[0])) {
76 /* strip leading whitespace */
78 while (isspace(str[start]) && start < newlen)
82 for (i = 0; i < newlen; i++)
83 str[i] = str[i+start];
89 static struct rte_cfgfile_section *
90 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
94 for (i = 0; i < cfg->num_sections; i++) {
95 if (strncmp(cfg->sections[i].name, sectionname,
96 sizeof(cfg->sections[0].name)) == 0)
97 return &cfg->sections[i];
103 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
104 const char *entryvalue)
106 /* resize entry structure if we don't have room for more entries */
107 if (section->num_entries == section->allocated_entries) {
108 struct rte_cfgfile_entry *n_entries = realloc(
110 sizeof(struct rte_cfgfile_entry) *
111 ((section->allocated_entries) +
112 CFG_ALLOC_ENTRY_BATCH));
114 if (n_entries == NULL)
117 section->entries = n_entries;
118 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
120 /* fill up entry fields with key name and value */
121 struct rte_cfgfile_entry *curr_entry =
122 §ion->entries[section->num_entries];
124 strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
125 strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
126 section->num_entries++;
132 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
134 unsigned int valid_comment;
138 CFG_LOG(ERR, "missing cfgfile parameters\n");
143 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
144 if (params->comment_character == valid_comment_chars[i]) {
150 if (valid_comment == 0) {
151 CFG_LOG(ERR, "invalid comment characters %c\n",
152 params->comment_character);
160 rte_cfgfile_load(const char *filename, int flags)
162 return rte_cfgfile_load_with_params(filename, flags,
163 &default_cfgfile_params);
167 rte_cfgfile_load_with_params(const char *filename, int flags,
168 const struct rte_cfgfile_parameters *params)
170 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4];
172 struct rte_cfgfile *cfg;
174 if (rte_cfgfile_check_params(params))
177 FILE *f = fopen(filename, "r");
181 cfg = rte_cfgfile_create(flags);
183 while (fgets(buffer, sizeof(buffer), f) != NULL) {
185 size_t len = strnlen(buffer, sizeof(buffer));
187 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
188 CFG_LOG(ERR, " line %d - no \\n found on string. "
189 "Check if line too long\n", lineno);
192 /* skip parsing if comment character found */
193 pos = memchr(buffer, params->comment_character, len);
194 if (pos != NULL && (*(pos-1) != '\\')) {
199 len = _strip(buffer, len);
200 /* skip lines without useful content */
201 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
204 if (buffer[0] == '[') {
205 /* section heading line */
206 char *end = memchr(buffer, ']', len);
209 "line %d - no terminating ']' character found\n",
214 _strip(&buffer[1], end - &buffer[1]);
216 rte_cfgfile_add_section(cfg, &buffer[1]);
218 /* key and value line */
219 char *split[2] = {NULL};
222 split[1] = memchr(buffer, '=', len);
223 if (split[1] == NULL) {
225 "line %d - no '=' character found\n",
232 _strip(split[0], strlen(split[0]));
233 _strip(split[1], strlen(split[1]));
234 char *end = memchr(split[1], '\\', strlen(split[1]));
236 size_t split_len = strlen(split[1]) + 1;
237 while (end != NULL) {
238 if (*(end+1) == params->comment_character) {
240 strlcat(split[1], end+1, split_len);
243 end = memchr(end, '\\', strlen(end));
246 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
247 (*split[1] == '\0')) {
249 "line %d - cannot use empty values\n",
254 if (cfg->num_sections == 0)
257 _add_entry(&cfg->sections[cfg->num_sections - 1],
264 rte_cfgfile_close(cfg);
270 rte_cfgfile_create(int flags)
273 struct rte_cfgfile *cfg;
275 /* future proof flags usage */
276 if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
279 cfg = malloc(sizeof(*cfg));
285 cfg->num_sections = 0;
287 /* allocate first batch of sections and entries */
288 cfg->sections = calloc(CFG_ALLOC_SECTION_BATCH,
289 sizeof(struct rte_cfgfile_section));
290 if (cfg->sections == NULL)
293 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
295 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
296 cfg->sections[i].entries = calloc(CFG_ALLOC_ENTRY_BATCH,
297 sizeof(struct rte_cfgfile_entry));
299 if (cfg->sections[i].entries == NULL)
302 cfg->sections[i].num_entries = 0;
303 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
306 if (flags & CFG_FLAG_GLOBAL_SECTION)
307 rte_cfgfile_add_section(cfg, "GLOBAL");
311 if (cfg->sections != NULL) {
312 for (i = 0; i < cfg->allocated_sections; i++) {
313 if (cfg->sections[i].entries != NULL) {
314 free(cfg->sections[i].entries);
315 cfg->sections[i].entries = NULL;
319 cfg->sections = NULL;
326 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
333 if (sectionname == NULL)
336 /* resize overall struct if we don't have room for more sections */
337 if (cfg->num_sections == cfg->allocated_sections) {
339 struct rte_cfgfile_section *n_sections =
340 realloc(cfg->sections,
341 sizeof(struct rte_cfgfile_section) *
342 ((cfg->allocated_sections) +
343 CFG_ALLOC_SECTION_BATCH));
345 if (n_sections == NULL)
348 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
349 n_sections[i + cfg->allocated_sections].num_entries = 0;
351 cfg->allocated_sections].allocated_entries = 0;
352 n_sections[i + cfg->allocated_sections].entries = NULL;
354 cfg->sections = n_sections;
355 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
358 strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
359 sizeof(cfg->sections[0].name));
360 cfg->sections[cfg->num_sections].num_entries = 0;
366 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
367 const char *sectionname, const char *entryname,
368 const char *entryvalue)
372 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
373 || (entryvalue == NULL))
376 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
379 /* search for section pointer by sectionname */
380 struct rte_cfgfile_section *curr_section = _get_section(cfg,
382 if (curr_section == NULL)
385 ret = _add_entry(curr_section, entryname, entryvalue);
390 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
391 const char *entryname, const char *entryvalue)
395 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
398 /* search for section pointer by sectionname */
399 struct rte_cfgfile_section *curr_section = _get_section(cfg,
401 if (curr_section == NULL)
404 if (entryvalue == NULL)
407 for (i = 0; i < curr_section->num_entries; i++)
408 if (!strcmp(curr_section->entries[i].name, entryname)) {
409 strlcpy(curr_section->entries[i].value, entryvalue,
410 sizeof(curr_section->entries[i].value));
414 CFG_LOG(ERR, "entry name doesn't exist\n");
418 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
422 if ((cfg == NULL) || (filename == NULL))
425 FILE *f = fopen(filename, "w");
430 for (i = 0; i < cfg->num_sections; i++) {
431 fprintf(f, "[%s]\n", cfg->sections[i].name);
433 for (j = 0; j < cfg->sections[i].num_entries; j++) {
434 fprintf(f, "%s=%s\n",
435 cfg->sections[i].entries[j].name,
436 cfg->sections[i].entries[j].value);
442 int rte_cfgfile_close(struct rte_cfgfile *cfg)
449 if (cfg->sections != NULL) {
450 for (i = 0; i < cfg->allocated_sections; i++) {
451 if (cfg->sections[i].entries != NULL) {
452 free(cfg->sections[i].entries);
453 cfg->sections[i].entries = NULL;
457 cfg->sections = NULL;
466 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
470 int num_sections = 0;
471 for (i = 0; i < cfg->num_sections; i++) {
472 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
479 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
484 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
485 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
491 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
493 return _get_section(cfg, sectionname) != NULL;
497 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
498 const char *sectionname)
500 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
503 return s->num_entries;
507 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
508 char *sectionname, int index)
510 if (index < 0 || index >= cfg->num_sections)
513 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
515 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
516 return sect->num_entries;
519 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
520 struct rte_cfgfile_entry *entries, int max_entries)
523 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
526 for (i = 0; i < max_entries && i < sect->num_entries; i++)
527 entries[i] = sect->entries[i];
532 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
534 struct rte_cfgfile_entry *entries, int max_entries)
537 const struct rte_cfgfile_section *sect;
539 if (index < 0 || index >= cfg->num_sections)
541 sect = &cfg->sections[index];
542 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
543 for (i = 0; i < max_entries && i < sect->num_entries; i++)
544 entries[i] = sect->entries[i];
549 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
550 const char *entryname)
553 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
556 for (i = 0; i < sect->num_entries; i++)
557 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
559 return sect->entries[i].value;
564 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
565 const char *entryname)
567 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
570 RTE_INIT(cfgfile_init)
572 cfgfile_logtype = rte_log_register("lib.cfgfile");
573 if (cfgfile_logtype >= 0)
574 rte_log_set_level(cfgfile_logtype, RTE_LOG_INFO);