net: add rte prefix to ether structures
[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
13 #include "rte_cfgfile.h"
14
15 struct rte_cfgfile_section {
16         char name[CFG_NAME_LEN];
17         int num_entries;
18         int allocated_entries;
19         struct rte_cfgfile_entry *entries;
20 };
21
22 struct rte_cfgfile {
23         int flags;
24         int num_sections;
25         int allocated_sections;
26         struct rte_cfgfile_section *sections;
27 };
28
29 /** when we resize a file structure, how many extra entries
30  * for new sections do we add in */
31 #define CFG_ALLOC_SECTION_BATCH 8
32 /** when we resize a section structure, how many extra entries
33  * for new entries do we add in */
34 #define CFG_ALLOC_ENTRY_BATCH 16
35
36 /**
37  * Default cfgfile load parameters.
38  */
39 static const struct rte_cfgfile_parameters default_cfgfile_params = {
40         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
41 };
42
43 /**
44  * Defines the list of acceptable comment characters supported by this
45  * library.
46  */
47 static const char valid_comment_chars[] = {
48         '!',
49         '#',
50         '%',
51         ';',
52         '@'
53 };
54
55 static unsigned
56 _strip(char *str, unsigned len)
57 {
58         int newlen = len;
59         if (len == 0)
60                 return 0;
61
62         if (isspace(str[len-1])) {
63                 /* strip trailing whitespace */
64                 while (newlen > 0 && isspace(str[newlen - 1]))
65                         str[--newlen] = '\0';
66         }
67
68         if (isspace(str[0])) {
69                 /* strip leading whitespace */
70                 int i, start = 1;
71                 while (isspace(str[start]) && start < newlen)
72                         start++
73                         ; /* do nothing */
74                 newlen -= start;
75                 for (i = 0; i < newlen; i++)
76                         str[i] = str[i+start];
77                 str[i] = '\0';
78         }
79         return newlen;
80 }
81
82 static struct rte_cfgfile_section *
83 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
84 {
85         int i;
86
87         for (i = 0; i < cfg->num_sections; i++) {
88                 if (strncmp(cfg->sections[i].name, sectionname,
89                                 sizeof(cfg->sections[0].name)) == 0)
90                         return &cfg->sections[i];
91         }
92         return NULL;
93 }
94
95 static int
96 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
97                 const char *entryvalue)
98 {
99         /* resize entry structure if we don't have room for more entries */
100         if (section->num_entries == section->allocated_entries) {
101                 struct rte_cfgfile_entry *n_entries = realloc(
102                                 section->entries,
103                                 sizeof(struct rte_cfgfile_entry) *
104                                 ((section->allocated_entries) +
105                                                 CFG_ALLOC_ENTRY_BATCH));
106
107                 if (n_entries == NULL)
108                         return -ENOMEM;
109
110                 section->entries = n_entries;
111                 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
112         }
113         /* fill up entry fields with key name and value */
114         struct rte_cfgfile_entry *curr_entry =
115                                         &section->entries[section->num_entries];
116
117         strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
118         strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
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                         size_t split_len = strlen(split[1]) + 1;
228                         while (end != NULL) {
229                                 if (*(end+1) == params->comment_character) {
230                                         *end = '\0';
231                                         strlcat(split[1], end+1, split_len);
232                                 } else
233                                         end++;
234                                 end = memchr(end, '\\', strlen(end));
235                         }
236
237                         if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
238                                         (*split[1] == '\0')) {
239                                 printf("Error at line %d - cannot use empty "
240                                                         "values\n", lineno);
241                                 goto error1;
242                         }
243
244                         if (cfg->num_sections == 0)
245                                 goto error1;
246
247                         _add_entry(&cfg->sections[cfg->num_sections - 1],
248                                         split[0], split[1]);
249                 }
250         }
251         fclose(f);
252         return cfg;
253 error1:
254         rte_cfgfile_close(cfg);
255         fclose(f);
256         return NULL;
257 }
258
259 struct rte_cfgfile *
260 rte_cfgfile_create(int flags)
261 {
262         int i;
263         struct rte_cfgfile *cfg = NULL;
264
265         cfg = malloc(sizeof(*cfg));
266
267         if (cfg == NULL)
268                 return NULL;
269
270         cfg->flags = flags;
271         cfg->num_sections = 0;
272
273         /* allocate first batch of sections and entries */
274         cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
275                         CFG_ALLOC_SECTION_BATCH);
276
277         if (cfg->sections == NULL)
278                 goto error1;
279
280         cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
281
282         for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
283                 cfg->sections[i].entries = malloc(sizeof(
284                         struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
285
286                 if (cfg->sections[i].entries == NULL)
287                         goto error1;
288
289                 cfg->sections[i].num_entries = 0;
290                 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
291         }
292
293         if (flags & CFG_FLAG_GLOBAL_SECTION)
294                 rte_cfgfile_add_section(cfg, "GLOBAL");
295
296         return cfg;
297 error1:
298         if (cfg->sections != NULL) {
299                 for (i = 0; i < cfg->allocated_sections; i++) {
300                         if (cfg->sections[i].entries != NULL) {
301                                 free(cfg->sections[i].entries);
302                                 cfg->sections[i].entries = NULL;
303                         }
304                 }
305                 free(cfg->sections);
306                 cfg->sections = NULL;
307         }
308         free(cfg);
309         return NULL;
310 }
311
312 int
313 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
314 {
315         int i;
316
317         if (cfg == NULL)
318                 return -EINVAL;
319
320         if (sectionname == NULL)
321                 return -EINVAL;
322
323         /* resize overall struct if we don't have room for more sections */
324         if (cfg->num_sections == cfg->allocated_sections) {
325
326                 struct rte_cfgfile_section *n_sections =
327                                 realloc(cfg->sections,
328                                 sizeof(struct rte_cfgfile_section) *
329                                 ((cfg->allocated_sections) +
330                                 CFG_ALLOC_SECTION_BATCH));
331
332                 if (n_sections == NULL)
333                         return -ENOMEM;
334
335                 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
336                         n_sections[i + cfg->allocated_sections].num_entries = 0;
337                         n_sections[i +
338                                  cfg->allocated_sections].allocated_entries = 0;
339                         n_sections[i + cfg->allocated_sections].entries = NULL;
340                 }
341                 cfg->sections = n_sections;
342                 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
343         }
344
345         strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
346                 sizeof(cfg->sections[0].name));
347         cfg->sections[cfg->num_sections].num_entries = 0;
348         cfg->num_sections++;
349
350         return 0;
351 }
352
353 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
354                 const char *sectionname, const char *entryname,
355                 const char *entryvalue)
356 {
357         int ret;
358
359         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
360                         || (entryvalue == NULL))
361                 return -EINVAL;
362
363         if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
364                 return -EEXIST;
365
366         /* search for section pointer by sectionname */
367         struct rte_cfgfile_section *curr_section = _get_section(cfg,
368                                                                 sectionname);
369         if (curr_section == NULL)
370                 return -EINVAL;
371
372         ret = _add_entry(curr_section, entryname, entryvalue);
373
374         return ret;
375 }
376
377 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
378                 const char *entryname, const char *entryvalue)
379 {
380         int i;
381
382         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
383                 return -EINVAL;
384
385         /* search for section pointer by sectionname */
386         struct rte_cfgfile_section *curr_section = _get_section(cfg,
387                                                                 sectionname);
388         if (curr_section == NULL)
389                 return -EINVAL;
390
391         if (entryvalue == NULL)
392                 entryvalue = "";
393
394         for (i = 0; i < curr_section->num_entries; i++)
395                 if (!strcmp(curr_section->entries[i].name, entryname)) {
396                         strlcpy(curr_section->entries[i].value, entryvalue,
397                                 sizeof(curr_section->entries[i].value));
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                 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
472
473         return i;
474 }
475
476 int
477 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
478 {
479         return _get_section(cfg, sectionname) != NULL;
480 }
481
482 int
483 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
484         const char *sectionname)
485 {
486         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
487         if (s == NULL)
488                 return -1;
489         return s->num_entries;
490 }
491
492 int
493 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
494         char *sectionname, int index)
495 {
496         if (index < 0 || index >= cfg->num_sections)
497                 return -1;
498
499         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
500
501         strlcpy(sectionname, sect->name, CFG_NAME_LEN);
502         return sect->num_entries;
503 }
504 int
505 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
506                 struct rte_cfgfile_entry *entries, int max_entries)
507 {
508         int i;
509         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
510         if (sect == NULL)
511                 return -1;
512         for (i = 0; i < max_entries && i < sect->num_entries; i++)
513                 entries[i] = sect->entries[i];
514         return i;
515 }
516
517 int
518 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
519                 char *sectionname,
520                 struct rte_cfgfile_entry *entries, int max_entries)
521 {
522         int i;
523         const struct rte_cfgfile_section *sect;
524
525         if (index < 0 || index >= cfg->num_sections)
526                 return -1;
527         sect = &cfg->sections[index];
528         strlcpy(sectionname, sect->name, CFG_NAME_LEN);
529         for (i = 0; i < max_entries && i < sect->num_entries; i++)
530                 entries[i] = sect->entries[i];
531         return i;
532 }
533
534 const char *
535 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
536                 const char *entryname)
537 {
538         int i;
539         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
540         if (sect == NULL)
541                 return NULL;
542         for (i = 0; i < sect->num_entries; i++)
543                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
544                                                                         == 0)
545                         return sect->entries[i].value;
546         return NULL;
547 }
548
549 int
550 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
551                 const char *entryname)
552 {
553         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
554 }