cfgfile: support runtime modification
[dpdk.git] / 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 <errno.h>
39 #include <rte_common.h>
40
41 #include "rte_cfgfile.h"
42
43 struct rte_cfgfile_section {
44         char name[CFG_NAME_LEN];
45         int num_entries;
46         int allocated_entries;
47         struct rte_cfgfile_entry *entries;
48 };
49
50 struct rte_cfgfile {
51         int flags;
52         int num_sections;
53         int allocated_sections;
54         struct rte_cfgfile_section *sections;
55 };
56
57 /** when we resize a file structure, how many extra entries
58  * for new sections do we add in */
59 #define CFG_ALLOC_SECTION_BATCH 8
60 /** when we resize a section structure, how many extra entries
61  * for new entries do we add in */
62 #define CFG_ALLOC_ENTRY_BATCH 16
63
64 /**
65  * Default cfgfile load parameters.
66  */
67 static const struct rte_cfgfile_parameters default_cfgfile_params = {
68         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
69 };
70
71 /**
72  * Defines the list of acceptable comment characters supported by this
73  * library.
74  */
75 static const char valid_comment_chars[] = {
76         '!',
77         '#',
78         '%',
79         ';',
80         '@'
81 };
82
83 static unsigned
84 _strip(char *str, unsigned len)
85 {
86         int newlen = len;
87         if (len == 0)
88                 return 0;
89
90         if (isspace(str[len-1])) {
91                 /* strip trailing whitespace */
92                 while (newlen > 0 && isspace(str[newlen - 1]))
93                         str[--newlen] = '\0';
94         }
95
96         if (isspace(str[0])) {
97                 /* strip leading whitespace */
98                 int i, start = 1;
99                 while (isspace(str[start]) && start < newlen)
100                         start++
101                         ; /* do nothing */
102                 newlen -= start;
103                 for (i = 0; i < newlen; i++)
104                         str[i] = str[i+start];
105                 str[i] = '\0';
106         }
107         return newlen;
108 }
109
110 static struct rte_cfgfile_section *
111 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
112 {
113         int i;
114
115         for (i = 0; i < cfg->num_sections; i++) {
116                 if (strncmp(cfg->sections[i].name, sectionname,
117                                 sizeof(cfg->sections[0].name)) == 0)
118                         return &cfg->sections[i];
119         }
120         return NULL;
121 }
122
123 static int
124 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
125                 const char *entryvalue)
126 {
127         /* resize entry structure if we don't have room for more entries */
128         if (section->num_entries == section->allocated_entries) {
129                 struct rte_cfgfile_entry *n_entries = realloc(
130                                 section->entries,
131                                 sizeof(struct rte_cfgfile_entry) *
132                                 ((section->allocated_entries) +
133                                                 CFG_ALLOC_ENTRY_BATCH));
134
135                 if (n_entries == NULL)
136                         return -ENOMEM;
137
138                 section->entries = n_entries;
139                 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
140         }
141         /* fill up entry fields with key name and value */
142         struct rte_cfgfile_entry *curr_entry =
143                                         &section->entries[section->num_entries];
144
145         snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
146         snprintf(curr_entry->value,
147                                 sizeof(curr_entry->value), "%s", entryvalue);
148         section->num_entries++;
149
150         return 0;
151 }
152
153 static int
154 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
155 {
156         unsigned int valid_comment;
157         unsigned int i;
158
159         if (!params) {
160                 printf("Error - missing cfgfile parameters\n");
161                 return -EINVAL;
162         }
163
164         valid_comment = 0;
165         for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
166                 if (params->comment_character == valid_comment_chars[i]) {
167                         valid_comment = 1;
168                         break;
169                 }
170         }
171
172         if (valid_comment == 0) {
173                 printf("Error - invalid comment characters %c\n",
174                        params->comment_character);
175                 return -ENOTSUP;
176         }
177
178         return 0;
179 }
180
181 struct rte_cfgfile *
182 rte_cfgfile_load(const char *filename, int flags)
183 {
184         return rte_cfgfile_load_with_params(filename, flags,
185                                             &default_cfgfile_params);
186 }
187
188 struct rte_cfgfile *
189 rte_cfgfile_load_with_params(const char *filename, int flags,
190                              const struct rte_cfgfile_parameters *params)
191 {
192         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
193         int allocated_entries = 0;
194         int curr_section = -1;
195         int curr_entry = -1;
196         char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
197         int lineno = 0;
198         struct rte_cfgfile *cfg = NULL;
199
200         if (rte_cfgfile_check_params(params))
201                 return NULL;
202
203         FILE *f = fopen(filename, "r");
204         if (f == NULL)
205                 return NULL;
206
207         cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
208                 allocated_sections);
209         if (cfg == NULL)
210                 goto error2;
211
212         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
213
214         if (flags & CFG_FLAG_GLOBAL_SECTION) {
215                 curr_section = 0;
216                 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
217                 cfg->sections = malloc(
218                         sizeof(cfg->sections[0]) +
219                         sizeof(cfg->sections[0].entries) *
220                         allocated_entries);
221                 if (cfg->sections == NULL) {
222                         printf("Error - no memory for global section\n");
223                         goto error1;
224                 }
225
226                 snprintf(cfg->sections[curr_section].name,
227                                  sizeof(cfg->sections[0].name), "GLOBAL");
228         }
229
230         while (fgets(buffer, sizeof(buffer), f) != NULL) {
231                 char *pos = NULL;
232                 size_t len = strnlen(buffer, sizeof(buffer));
233                 lineno++;
234                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
235                         printf("Error line %d - no \\n found on string. "
236                                         "Check if line too long\n", lineno);
237                         goto error1;
238                 }
239                 pos = memchr(buffer, params->comment_character, len);
240                 if (pos != NULL) {
241                         *pos = '\0';
242                         len = pos -  buffer;
243                 }
244
245                 len = _strip(buffer, len);
246                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
247                         continue;
248
249                 if (buffer[0] == '[') {
250                         /* section heading line */
251                         char *end = memchr(buffer, ']', len);
252                         if (end == NULL) {
253                                 printf("Error line %d - no terminating '['"
254                                         "character found\n", lineno);
255                                 goto error1;
256                         }
257                         *end = '\0';
258                         _strip(&buffer[1], end - &buffer[1]);
259
260                         /* close off old section and add start new one */
261                         if (curr_section >= 0)
262                                 cfg->sections[curr_section].num_entries =
263                                         curr_entry + 1;
264                         curr_section++;
265
266                         /* resize overall struct if we don't have room for more
267                         sections */
268                         if (curr_section == allocated_sections) {
269                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
270                                 struct rte_cfgfile *n_cfg = realloc(cfg,
271                                         sizeof(*cfg) + sizeof(cfg->sections[0])
272                                         * allocated_sections);
273                                 if (n_cfg == NULL) {
274                                         curr_section--;
275                                         printf("Error - no more memory\n");
276                                         goto error1;
277                                 }
278                                 cfg = n_cfg;
279                         }
280
281                         /* allocate space for new section */
282                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
283                         curr_entry = -1;
284                         cfg->sections = malloc(
285                                 sizeof(cfg->sections[0]) +
286                                 sizeof(cfg->sections[0].entries) *
287                                 allocated_entries);
288                         if (cfg->sections == NULL) {
289                                 printf("Error - no more memory\n");
290                                 goto error1;
291                         }
292
293                         snprintf(cfg->sections[curr_section].name,
294                                         sizeof(cfg->sections[0].name),
295                                         "%s", &buffer[1]);
296                 } else {
297                         /* value line */
298                         if (curr_section < 0) {
299                                 printf("Error line %d - value outside of"
300                                         "section\n", lineno);
301                                 goto error1;
302                         }
303
304                         struct rte_cfgfile_section *sect = cfg->sections;
305
306                         char *split[2] = {NULL};
307                         split[0] = buffer;
308                         split[1] = memchr(buffer, '=', len);
309
310                         /* when delimeter not found */
311                         if (split[1] == NULL) {
312                                 printf("Error at line %d - cannot "
313                                         "split string\n", lineno);
314                                 goto error1;
315                         } else {
316                                 /* when delimeter found */
317                                 *split[1] = '\0';
318                                 split[1]++;
319
320                                 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
321                                                 (*split[1] == '\0')) {
322                                         printf("Error at line %d - cannot "
323                                                 "split string\n", lineno);
324                                         goto error1;
325                                 }
326                         }
327
328                         curr_entry++;
329                         if (curr_entry == allocated_entries) {
330                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
331                                 struct rte_cfgfile_section *n_sect = realloc(
332                                         sect, sizeof(*sect) +
333                                         sizeof(sect->entries[0]) *
334                                         allocated_entries);
335                                 if (n_sect == NULL) {
336                                         curr_entry--;
337                                         printf("Error - no more memory\n");
338                                         goto error1;
339                                 }
340                                 sect = cfg->sections = n_sect;
341                         }
342
343                         sect->entries = malloc(
344                                 sizeof(sect->entries[0]));
345                         if (sect->entries == NULL) {
346                                 printf("Error - no more memory\n");
347                                 goto error1;
348                         }
349
350                         struct rte_cfgfile_entry *entry = sect->entries;
351                         snprintf(entry->name, sizeof(entry->name), "%s",
352                                 split[0]);
353                         snprintf(entry->value, sizeof(entry->value), "%s",
354                                  split[1] ? split[1] : "");
355                         _strip(entry->name, strnlen(entry->name,
356                                 sizeof(entry->name)));
357                         _strip(entry->value, strnlen(entry->value,
358                                 sizeof(entry->value)));
359                 }
360         }
361         fclose(f);
362         cfg->flags = flags;
363         cfg->num_sections = curr_section + 1;
364         /* curr_section will still be -1 if we have an empty file */
365         if (curr_section >= 0)
366                 cfg->sections[curr_section].num_entries = curr_entry + 1;
367         return cfg;
368
369 error1:
370         cfg->num_sections = curr_section + 1;
371         if (curr_section >= 0)
372                 cfg->sections[curr_section].num_entries = curr_entry + 1;
373         rte_cfgfile_close(cfg);
374 error2:
375         fclose(f);
376         return NULL;
377 }
378
379 struct rte_cfgfile *
380 rte_cfgfile_create(int flags)
381 {
382         int i;
383         struct rte_cfgfile *cfg = NULL;
384
385         cfg = malloc(sizeof(*cfg));
386
387         if (cfg == NULL)
388                 return NULL;
389
390         cfg->flags = flags;
391         cfg->num_sections = 0;
392
393         /* allocate first batch of sections and entries */
394         cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
395                         CFG_ALLOC_SECTION_BATCH);
396
397         if (cfg->sections == NULL)
398                 return NULL;
399
400         cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
401
402         for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
403                 cfg->sections[i].entries = malloc(sizeof(
404                         struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
405
406                 if (cfg->sections[i].entries == NULL)
407                         return NULL;
408
409                 cfg->sections[i].num_entries = 0;
410                 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
411         }
412
413         if (flags & CFG_FLAG_GLOBAL_SECTION)
414                 rte_cfgfile_add_section(cfg, "GLOBAL");
415         return cfg;
416 }
417
418 int
419 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
420 {
421         int i;
422
423         if (cfg == NULL)
424                 return -EINVAL;
425
426         if (sectionname == NULL)
427                 return -EINVAL;
428
429         /* resize overall struct if we don't have room for more sections */
430         if (cfg->num_sections == cfg->allocated_sections) {
431
432                 struct rte_cfgfile_section *n_sections =
433                                 realloc(cfg->sections,
434                                 sizeof(struct rte_cfgfile_section) *
435                                 ((cfg->allocated_sections) +
436                                 CFG_ALLOC_SECTION_BATCH));
437
438                 if (n_sections == NULL)
439                         return -ENOMEM;
440
441                 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
442                         n_sections[i + cfg->allocated_sections].num_entries = 0;
443                         n_sections[i +
444                                  cfg->allocated_sections].allocated_entries = 0;
445                         n_sections[i + cfg->allocated_sections].entries = NULL;
446                 }
447                 cfg->sections = n_sections;
448                 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
449         }
450
451         snprintf(cfg->sections[cfg->num_sections].name,
452                         sizeof(cfg->sections[0].name), "%s", sectionname);
453         cfg->sections[cfg->num_sections].num_entries = 0;
454         cfg->num_sections++;
455
456         return 0;
457 }
458
459 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
460                 const char *sectionname, const char *entryname,
461                 const char *entryvalue)
462 {
463         int ret;
464
465         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
466                         || (entryvalue == NULL))
467                 return -EINVAL;
468
469         if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
470                 return -EEXIST;
471
472         /* search for section pointer by sectionname */
473         struct rte_cfgfile_section *curr_section = _get_section(cfg,
474                                                                 sectionname);
475         if (curr_section == NULL)
476                 return -EINVAL;
477
478         ret = _add_entry(curr_section, entryname, entryvalue);
479
480         return ret;
481 }
482
483 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
484                 const char *entryname, const char *entryvalue)
485 {
486         int i;
487
488         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
489                 return -EINVAL;
490
491         /* search for section pointer by sectionname */
492         struct rte_cfgfile_section *curr_section = _get_section(cfg,
493                                                                 sectionname);
494         if (curr_section == NULL)
495                 return -EINVAL;
496
497         if (entryvalue == NULL)
498                 entryvalue = "";
499
500         for (i = 0; i < curr_section->num_entries; i++)
501                 if (!strcmp(curr_section->entries[i].name, entryname)) {
502                         snprintf(curr_section->entries[i].value,
503                                         sizeof(curr_section->entries[i].value),
504                                                         "%s", entryvalue);
505                         return 0;
506                 }
507         printf("Error - entry name doesn't exist\n");
508         return -EINVAL;
509 }
510
511 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
512 {
513         int i, j;
514
515         if ((cfg == NULL) || (filename == NULL))
516                 return -EINVAL;
517
518         FILE *f = fopen(filename, "w");
519
520         if (f == NULL)
521                 return -EINVAL;
522
523         for (i = 0; i < cfg->num_sections; i++) {
524                 fprintf(f, "[%s]\n", cfg->sections[i].name);
525
526                 for (j = 0; j < cfg->sections[i].num_entries; j++) {
527                         fprintf(f, "%s=%s\n",
528                                         cfg->sections[i].entries[j].name,
529                                         cfg->sections[i].entries[j].value);
530                 }
531         }
532         return fclose(f);
533 }
534
535 int rte_cfgfile_close(struct rte_cfgfile *cfg)
536 {
537         int i;
538
539         if (cfg == NULL)
540                 return -1;
541
542         if (cfg->sections != NULL) {
543                 for (i = 0; i < cfg->allocated_sections; i++) {
544                         if (cfg->sections[i].entries != NULL) {
545                                 free(cfg->sections[i].entries);
546                                 cfg->sections[i].entries = NULL;
547                         }
548                 }
549                 free(cfg->sections);
550                 cfg->sections = NULL;
551         }
552         free(cfg);
553         cfg = NULL;
554
555         return 0;
556 }
557
558 int
559 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
560 size_t length)
561 {
562         int i;
563         int num_sections = 0;
564         for (i = 0; i < cfg->num_sections; i++) {
565                 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
566                         num_sections++;
567         }
568         return num_sections;
569 }
570
571 int
572 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
573         int max_sections)
574 {
575         int i;
576
577         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
578                 snprintf(sections[i], CFG_NAME_LEN, "%s",
579                 cfg->sections[i].name);
580
581         return i;
582 }
583
584 int
585 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
586 {
587         return _get_section(cfg, sectionname) != NULL;
588 }
589
590 int
591 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
592         const char *sectionname)
593 {
594         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
595         if (s == NULL)
596                 return -1;
597         return s->num_entries;
598 }
599
600 int
601 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
602         char *sectionname, int index)
603 {
604         if (index < 0 || index >= cfg->num_sections)
605                 return -1;
606
607         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
608
609         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
610         return sect->num_entries;
611 }
612 int
613 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
614                 struct rte_cfgfile_entry *entries, int max_entries)
615 {
616         int i;
617         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
618         if (sect == NULL)
619                 return -1;
620         for (i = 0; i < max_entries && i < sect->num_entries; i++)
621                 entries[i] = sect->entries[i];
622         return i;
623 }
624
625 int
626 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
627                 char *sectionname,
628                 struct rte_cfgfile_entry *entries, int max_entries)
629 {
630         int i;
631         const struct rte_cfgfile_section *sect;
632
633         if (index < 0 || index >= cfg->num_sections)
634                 return -1;
635         sect = &cfg->sections[index];
636         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
637         for (i = 0; i < max_entries && i < sect->num_entries; i++)
638                 entries[i] = sect->entries[i];
639         return i;
640 }
641
642 const char *
643 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
644                 const char *entryname)
645 {
646         int i;
647         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
648         if (sect == NULL)
649                 return NULL;
650         for (i = 0; i < sect->num_entries; i++)
651                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
652                                                                         == 0)
653                         return sect->entries[i].value;
654         return NULL;
655 }
656
657 int
658 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
659                 const char *entryname)
660 {
661         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
662 }