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