maintainers: update for mempool
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <rte_common.h>
11
12 #include "rte_cfgfile.h"
13
14 struct rte_cfgfile_section {
15         char name[CFG_NAME_LEN];
16         int num_entries;
17         int allocated_entries;
18         struct rte_cfgfile_entry *entries;
19 };
20
21 struct rte_cfgfile {
22         int flags;
23         int num_sections;
24         int allocated_sections;
25         struct rte_cfgfile_section *sections;
26 };
27
28 /** when we resize a file structure, how many extra entries
29  * for new sections do we add in */
30 #define CFG_ALLOC_SECTION_BATCH 8
31 /** when we resize a section structure, how many extra entries
32  * for new entries do we add in */
33 #define CFG_ALLOC_ENTRY_BATCH 16
34
35 /**
36  * Default cfgfile load parameters.
37  */
38 static const struct rte_cfgfile_parameters default_cfgfile_params = {
39         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
40 };
41
42 /**
43  * Defines the list of acceptable comment characters supported by this
44  * library.
45  */
46 static const char valid_comment_chars[] = {
47         '!',
48         '#',
49         '%',
50         ';',
51         '@'
52 };
53
54 static unsigned
55 _strip(char *str, unsigned len)
56 {
57         int newlen = len;
58         if (len == 0)
59                 return 0;
60
61         if (isspace(str[len-1])) {
62                 /* strip trailing whitespace */
63                 while (newlen > 0 && isspace(str[newlen - 1]))
64                         str[--newlen] = '\0';
65         }
66
67         if (isspace(str[0])) {
68                 /* strip leading whitespace */
69                 int i, start = 1;
70                 while (isspace(str[start]) && start < newlen)
71                         start++
72                         ; /* do nothing */
73                 newlen -= start;
74                 for (i = 0; i < newlen; i++)
75                         str[i] = str[i+start];
76                 str[i] = '\0';
77         }
78         return newlen;
79 }
80
81 static struct rte_cfgfile_section *
82 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
83 {
84         int i;
85
86         for (i = 0; i < cfg->num_sections; i++) {
87                 if (strncmp(cfg->sections[i].name, sectionname,
88                                 sizeof(cfg->sections[0].name)) == 0)
89                         return &cfg->sections[i];
90         }
91         return NULL;
92 }
93
94 static int
95 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
96                 const char *entryvalue)
97 {
98         /* resize entry structure if we don't have room for more entries */
99         if (section->num_entries == section->allocated_entries) {
100                 struct rte_cfgfile_entry *n_entries = realloc(
101                                 section->entries,
102                                 sizeof(struct rte_cfgfile_entry) *
103                                 ((section->allocated_entries) +
104                                                 CFG_ALLOC_ENTRY_BATCH));
105
106                 if (n_entries == NULL)
107                         return -ENOMEM;
108
109                 section->entries = n_entries;
110                 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
111         }
112         /* fill up entry fields with key name and value */
113         struct rte_cfgfile_entry *curr_entry =
114                                         &section->entries[section->num_entries];
115
116         snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
117         snprintf(curr_entry->value,
118                                 sizeof(curr_entry->value), "%s", entryvalue);
119         section->num_entries++;
120
121         return 0;
122 }
123
124 static int
125 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
126 {
127         unsigned int valid_comment;
128         unsigned int i;
129
130         if (!params) {
131                 printf("Error - missing cfgfile parameters\n");
132                 return -EINVAL;
133         }
134
135         valid_comment = 0;
136         for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
137                 if (params->comment_character == valid_comment_chars[i]) {
138                         valid_comment = 1;
139                         break;
140                 }
141         }
142
143         if (valid_comment == 0) {
144                 printf("Error - invalid comment characters %c\n",
145                        params->comment_character);
146                 return -ENOTSUP;
147         }
148
149         return 0;
150 }
151
152 struct rte_cfgfile *
153 rte_cfgfile_load(const char *filename, int flags)
154 {
155         return rte_cfgfile_load_with_params(filename, flags,
156                                             &default_cfgfile_params);
157 }
158
159 struct rte_cfgfile *
160 rte_cfgfile_load_with_params(const char *filename, int flags,
161                              const struct rte_cfgfile_parameters *params)
162 {
163         char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
164         int lineno = 0;
165         struct rte_cfgfile *cfg = NULL;
166
167         if (rte_cfgfile_check_params(params))
168                 return NULL;
169
170         FILE *f = fopen(filename, "r");
171         if (f == NULL)
172                 return NULL;
173
174         cfg = rte_cfgfile_create(flags);
175
176         while (fgets(buffer, sizeof(buffer), f) != NULL) {
177                 char *pos = NULL;
178                 size_t len = strnlen(buffer, sizeof(buffer));
179                 lineno++;
180                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
181                         printf("Error line %d - no \\n found on string. "
182                                         "Check if line too long\n", lineno);
183                         goto error1;
184                 }
185                 /* skip parsing if comment character found */
186                 pos = memchr(buffer, params->comment_character, len);
187                 if (pos != NULL && (*(pos-1) != '\\')) {
188                         *pos = '\0';
189                         len = pos -  buffer;
190                 }
191
192                 len = _strip(buffer, len);
193                 /* skip lines without useful content */
194                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
195                         continue;
196
197                 if (buffer[0] == '[') {
198                         /* section heading line */
199                         char *end = memchr(buffer, ']', len);
200                         if (end == NULL) {
201                                 printf("Error line %d - no terminating ']'"
202                                         "character found\n", lineno);
203                                 goto error1;
204                         }
205                         *end = '\0';
206                         _strip(&buffer[1], end - &buffer[1]);
207
208                         rte_cfgfile_add_section(cfg, &buffer[1]);
209                 } else {
210                         /* key and value line */
211                         char *split[2] = {NULL};
212
213                         split[0] = buffer;
214                         split[1] = memchr(buffer, '=', len);
215                         if (split[1] == NULL) {
216                                 printf("Error line %d - no '='"
217                                         "character found\n", lineno);
218                                 goto error1;
219                         }
220                         *split[1] = '\0';
221                         split[1]++;
222
223                         _strip(split[0], strlen(split[0]));
224                         _strip(split[1], strlen(split[1]));
225                         char *end = memchr(split[1], '\\', strlen(split[1]));
226
227                         while (end != NULL) {
228                                 if (*(end+1) == params->comment_character) {
229                                         *end = '\0';
230                                         strcat(split[1], end+1);
231                                 } else
232                                         end++;
233                                 end = memchr(end, '\\', strlen(end));
234                         }
235
236                         if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
237                                         (*split[1] == '\0')) {
238                                 printf("Error at line %d - cannot use empty "
239                                                         "values\n", lineno);
240                                 goto error1;
241                         }
242
243                         if (cfg->num_sections == 0)
244                                 goto error1;
245
246                         _add_entry(&cfg->sections[cfg->num_sections - 1],
247                                         split[0], split[1]);
248                 }
249         }
250         fclose(f);
251         return cfg;
252 error1:
253         rte_cfgfile_close(cfg);
254         fclose(f);
255         return NULL;
256 }
257
258 struct rte_cfgfile *
259 rte_cfgfile_create(int flags)
260 {
261         int i;
262         struct rte_cfgfile *cfg = NULL;
263
264         cfg = malloc(sizeof(*cfg));
265
266         if (cfg == NULL)
267                 return NULL;
268
269         cfg->flags = flags;
270         cfg->num_sections = 0;
271
272         /* allocate first batch of sections and entries */
273         cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
274                         CFG_ALLOC_SECTION_BATCH);
275
276         if (cfg->sections == NULL)
277                 goto error1;
278
279         cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
280
281         for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
282                 cfg->sections[i].entries = malloc(sizeof(
283                         struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
284
285                 if (cfg->sections[i].entries == NULL)
286                         goto error1;
287
288                 cfg->sections[i].num_entries = 0;
289                 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
290         }
291
292         if (flags & CFG_FLAG_GLOBAL_SECTION)
293                 rte_cfgfile_add_section(cfg, "GLOBAL");
294
295         return cfg;
296 error1:
297         if (cfg->sections != NULL) {
298                 for (i = 0; i < cfg->allocated_sections; i++) {
299                         if (cfg->sections[i].entries != NULL) {
300                                 free(cfg->sections[i].entries);
301                                 cfg->sections[i].entries = NULL;
302                         }
303                 }
304                 free(cfg->sections);
305                 cfg->sections = NULL;
306         }
307         free(cfg);
308         return NULL;
309 }
310
311 int
312 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
313 {
314         int i;
315
316         if (cfg == NULL)
317                 return -EINVAL;
318
319         if (sectionname == NULL)
320                 return -EINVAL;
321
322         /* resize overall struct if we don't have room for more sections */
323         if (cfg->num_sections == cfg->allocated_sections) {
324
325                 struct rte_cfgfile_section *n_sections =
326                                 realloc(cfg->sections,
327                                 sizeof(struct rte_cfgfile_section) *
328                                 ((cfg->allocated_sections) +
329                                 CFG_ALLOC_SECTION_BATCH));
330
331                 if (n_sections == NULL)
332                         return -ENOMEM;
333
334                 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
335                         n_sections[i + cfg->allocated_sections].num_entries = 0;
336                         n_sections[i +
337                                  cfg->allocated_sections].allocated_entries = 0;
338                         n_sections[i + cfg->allocated_sections].entries = NULL;
339                 }
340                 cfg->sections = n_sections;
341                 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
342         }
343
344         snprintf(cfg->sections[cfg->num_sections].name,
345                         sizeof(cfg->sections[0].name), "%s", sectionname);
346         cfg->sections[cfg->num_sections].num_entries = 0;
347         cfg->num_sections++;
348
349         return 0;
350 }
351
352 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
353                 const char *sectionname, const char *entryname,
354                 const char *entryvalue)
355 {
356         int ret;
357
358         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
359                         || (entryvalue == NULL))
360                 return -EINVAL;
361
362         if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
363                 return -EEXIST;
364
365         /* search for section pointer by sectionname */
366         struct rte_cfgfile_section *curr_section = _get_section(cfg,
367                                                                 sectionname);
368         if (curr_section == NULL)
369                 return -EINVAL;
370
371         ret = _add_entry(curr_section, entryname, entryvalue);
372
373         return ret;
374 }
375
376 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
377                 const char *entryname, const char *entryvalue)
378 {
379         int i;
380
381         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
382                 return -EINVAL;
383
384         /* search for section pointer by sectionname */
385         struct rte_cfgfile_section *curr_section = _get_section(cfg,
386                                                                 sectionname);
387         if (curr_section == NULL)
388                 return -EINVAL;
389
390         if (entryvalue == NULL)
391                 entryvalue = "";
392
393         for (i = 0; i < curr_section->num_entries; i++)
394                 if (!strcmp(curr_section->entries[i].name, entryname)) {
395                         snprintf(curr_section->entries[i].value,
396                                         sizeof(curr_section->entries[i].value),
397                                                         "%s", entryvalue);
398                         return 0;
399                 }
400         printf("Error - entry name doesn't exist\n");
401         return -EINVAL;
402 }
403
404 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
405 {
406         int i, j;
407
408         if ((cfg == NULL) || (filename == NULL))
409                 return -EINVAL;
410
411         FILE *f = fopen(filename, "w");
412
413         if (f == NULL)
414                 return -EINVAL;
415
416         for (i = 0; i < cfg->num_sections; i++) {
417                 fprintf(f, "[%s]\n", cfg->sections[i].name);
418
419                 for (j = 0; j < cfg->sections[i].num_entries; j++) {
420                         fprintf(f, "%s=%s\n",
421                                         cfg->sections[i].entries[j].name,
422                                         cfg->sections[i].entries[j].value);
423                 }
424         }
425         return fclose(f);
426 }
427
428 int rte_cfgfile_close(struct rte_cfgfile *cfg)
429 {
430         int i;
431
432         if (cfg == NULL)
433                 return -1;
434
435         if (cfg->sections != NULL) {
436                 for (i = 0; i < cfg->allocated_sections; i++) {
437                         if (cfg->sections[i].entries != NULL) {
438                                 free(cfg->sections[i].entries);
439                                 cfg->sections[i].entries = NULL;
440                         }
441                 }
442                 free(cfg->sections);
443                 cfg->sections = NULL;
444         }
445         free(cfg);
446         cfg = NULL;
447
448         return 0;
449 }
450
451 int
452 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
453 size_t length)
454 {
455         int i;
456         int num_sections = 0;
457         for (i = 0; i < cfg->num_sections; i++) {
458                 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
459                         num_sections++;
460         }
461         return num_sections;
462 }
463
464 int
465 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
466         int max_sections)
467 {
468         int i;
469
470         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
471                 snprintf(sections[i], CFG_NAME_LEN, "%s",
472                 cfg->sections[i].name);
473
474         return i;
475 }
476
477 int
478 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
479 {
480         return _get_section(cfg, sectionname) != NULL;
481 }
482
483 int
484 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
485         const char *sectionname)
486 {
487         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
488         if (s == NULL)
489                 return -1;
490         return s->num_entries;
491 }
492
493 int
494 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
495         char *sectionname, int index)
496 {
497         if (index < 0 || index >= cfg->num_sections)
498                 return -1;
499
500         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
501
502         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
503         return sect->num_entries;
504 }
505 int
506 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
507                 struct rte_cfgfile_entry *entries, int max_entries)
508 {
509         int i;
510         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
511         if (sect == NULL)
512                 return -1;
513         for (i = 0; i < max_entries && i < sect->num_entries; i++)
514                 entries[i] = sect->entries[i];
515         return i;
516 }
517
518 int
519 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
520                 char *sectionname,
521                 struct rte_cfgfile_entry *entries, int max_entries)
522 {
523         int i;
524         const struct rte_cfgfile_section *sect;
525
526         if (index < 0 || index >= cfg->num_sections)
527                 return -1;
528         sect = &cfg->sections[index];
529         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
530         for (i = 0; i < max_entries && i < sect->num_entries; i++)
531                 entries[i] = sect->entries[i];
532         return i;
533 }
534
535 const char *
536 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
537                 const char *entryname)
538 {
539         int i;
540         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
541         if (sect == NULL)
542                 return NULL;
543         for (i = 0; i < sect->num_entries; i++)
544                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
545                                                                         == 0)
546                         return sect->entries[i].value;
547         return NULL;
548 }
549
550 int
551 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
552                 const char *entryname)
553 {
554         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
555 }