cfgfile: check flags on creation for future proofing
[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         /* future proof flags usage */
276         if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
277                 return NULL;
278
279         cfg = malloc(sizeof(*cfg));
280
281         if (cfg == NULL)
282                 return NULL;
283
284         cfg->flags = flags;
285         cfg->num_sections = 0;
286
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)
291                 goto error1;
292
293         cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
294
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));
298
299                 if (cfg->sections[i].entries == NULL)
300                         goto error1;
301
302                 cfg->sections[i].num_entries = 0;
303                 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
304         }
305
306         if (flags & CFG_FLAG_GLOBAL_SECTION)
307                 rte_cfgfile_add_section(cfg, "GLOBAL");
308
309         return cfg;
310 error1:
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;
316                         }
317                 }
318                 free(cfg->sections);
319                 cfg->sections = NULL;
320         }
321         free(cfg);
322         return NULL;
323 }
324
325 int
326 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
327 {
328         int i;
329
330         if (cfg == NULL)
331                 return -EINVAL;
332
333         if (sectionname == NULL)
334                 return -EINVAL;
335
336         /* resize overall struct if we don't have room for more sections */
337         if (cfg->num_sections == cfg->allocated_sections) {
338
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));
344
345                 if (n_sections == NULL)
346                         return -ENOMEM;
347
348                 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
349                         n_sections[i + cfg->allocated_sections].num_entries = 0;
350                         n_sections[i +
351                                  cfg->allocated_sections].allocated_entries = 0;
352                         n_sections[i + cfg->allocated_sections].entries = NULL;
353                 }
354                 cfg->sections = n_sections;
355                 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
356         }
357
358         strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
359                 sizeof(cfg->sections[0].name));
360         cfg->sections[cfg->num_sections].num_entries = 0;
361         cfg->num_sections++;
362
363         return 0;
364 }
365
366 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
367                 const char *sectionname, const char *entryname,
368                 const char *entryvalue)
369 {
370         int ret;
371
372         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
373                         || (entryvalue == NULL))
374                 return -EINVAL;
375
376         if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
377                 return -EEXIST;
378
379         /* search for section pointer by sectionname */
380         struct rte_cfgfile_section *curr_section = _get_section(cfg,
381                                                                 sectionname);
382         if (curr_section == NULL)
383                 return -EINVAL;
384
385         ret = _add_entry(curr_section, entryname, entryvalue);
386
387         return ret;
388 }
389
390 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
391                 const char *entryname, const char *entryvalue)
392 {
393         int i;
394
395         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
396                 return -EINVAL;
397
398         /* search for section pointer by sectionname */
399         struct rte_cfgfile_section *curr_section = _get_section(cfg,
400                                                                 sectionname);
401         if (curr_section == NULL)
402                 return -EINVAL;
403
404         if (entryvalue == NULL)
405                 entryvalue = "";
406
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));
411                         return 0;
412                 }
413
414         CFG_LOG(ERR, "entry name doesn't exist\n");
415         return -EINVAL;
416 }
417
418 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
419 {
420         int i, j;
421
422         if ((cfg == NULL) || (filename == NULL))
423                 return -EINVAL;
424
425         FILE *f = fopen(filename, "w");
426
427         if (f == NULL)
428                 return -EINVAL;
429
430         for (i = 0; i < cfg->num_sections; i++) {
431                 fprintf(f, "[%s]\n", cfg->sections[i].name);
432
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);
437                 }
438         }
439         return fclose(f);
440 }
441
442 int rte_cfgfile_close(struct rte_cfgfile *cfg)
443 {
444         int i;
445
446         if (cfg == NULL)
447                 return -1;
448
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;
454                         }
455                 }
456                 free(cfg->sections);
457                 cfg->sections = NULL;
458         }
459         free(cfg);
460         cfg = NULL;
461
462         return 0;
463 }
464
465 int
466 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
467 size_t length)
468 {
469         int i;
470         int num_sections = 0;
471         for (i = 0; i < cfg->num_sections; i++) {
472                 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
473                         num_sections++;
474         }
475         return num_sections;
476 }
477
478 int
479 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
480         int max_sections)
481 {
482         int i;
483
484         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
485                 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
486
487         return i;
488 }
489
490 int
491 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
492 {
493         return _get_section(cfg, sectionname) != NULL;
494 }
495
496 int
497 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
498         const char *sectionname)
499 {
500         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
501         if (s == NULL)
502                 return -1;
503         return s->num_entries;
504 }
505
506 int
507 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
508         char *sectionname, int index)
509 {
510         if (index < 0 || index >= cfg->num_sections)
511                 return -1;
512
513         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
514
515         strlcpy(sectionname, sect->name, CFG_NAME_LEN);
516         return sect->num_entries;
517 }
518 int
519 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
520                 struct rte_cfgfile_entry *entries, int max_entries)
521 {
522         int i;
523         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
524         if (sect == NULL)
525                 return -1;
526         for (i = 0; i < max_entries && i < sect->num_entries; i++)
527                 entries[i] = sect->entries[i];
528         return i;
529 }
530
531 int
532 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
533                 char *sectionname,
534                 struct rte_cfgfile_entry *entries, int max_entries)
535 {
536         int i;
537         const struct rte_cfgfile_section *sect;
538
539         if (index < 0 || index >= cfg->num_sections)
540                 return -1;
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];
545         return i;
546 }
547
548 const char *
549 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
550                 const char *entryname)
551 {
552         int i;
553         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
554         if (sect == NULL)
555                 return NULL;
556         for (i = 0; i < sect->num_entries; i++)
557                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
558                                                                         == 0)
559                         return sect->entries[i].value;
560         return NULL;
561 }
562
563 int
564 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
565                 const char *entryname)
566 {
567         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
568 }
569
570 RTE_INIT(cfgfile_init)
571 {
572         cfgfile_logtype = rte_log_register("lib.cfgfile");
573         if (cfgfile_logtype >= 0)
574                 rte_log_set_level(cfgfile_logtype, RTE_LOG_INFO);
575 }