cfgfile: support configurable comment character
[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 <rte_common.h>
39 #include <rte_string_fns.h>
40
41 #include "rte_cfgfile.h"
42
43 struct rte_cfgfile_section {
44         char name[CFG_NAME_LEN];
45         int num_entries;
46         struct rte_cfgfile_entry *entries[0];
47 };
48
49 struct rte_cfgfile {
50         int flags;
51         int num_sections;
52         struct rte_cfgfile_section *sections[0];
53 };
54
55 /** when we resize a file structure, how many extra entries
56  * for new sections do we add in */
57 #define CFG_ALLOC_SECTION_BATCH 8
58 /** when we resize a section structure, how many extra entries
59  * for new entries do we add in */
60 #define CFG_ALLOC_ENTRY_BATCH 16
61
62 /**
63  * Default cfgfile load parameters.
64  */
65 static const struct rte_cfgfile_parameters default_cfgfile_params = {
66         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
67 };
68
69 /**
70  * Defines the list of acceptable comment characters supported by this
71  * library.
72  */
73 static const char valid_comment_chars[] = {
74         '!',
75         '#',
76         '%',
77         ';',
78         '@'
79 };
80
81 static unsigned
82 _strip(char *str, unsigned len)
83 {
84         int newlen = len;
85         if (len == 0)
86                 return 0;
87
88         if (isspace(str[len-1])) {
89                 /* strip trailing whitespace */
90                 while (newlen > 0 && isspace(str[newlen - 1]))
91                         str[--newlen] = '\0';
92         }
93
94         if (isspace(str[0])) {
95                 /* strip leading whitespace */
96                 int i, start = 1;
97                 while (isspace(str[start]) && start < newlen)
98                         start++
99                         ; /* do nothing */
100                 newlen -= start;
101                 for (i = 0; i < newlen; i++)
102                         str[i] = str[i+start];
103                 str[i] = '\0';
104         }
105         return newlen;
106 }
107
108 static int
109 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
110 {
111         unsigned int valid_comment;
112         unsigned int i;
113
114         if (!params) {
115                 printf("Error - missing cfgfile parameters\n");
116                 return -EINVAL;
117         }
118
119         valid_comment = 0;
120         for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
121                 if (params->comment_character == valid_comment_chars[i]) {
122                         valid_comment = 1;
123                         break;
124                 }
125         }
126
127         if (valid_comment == 0) {
128                 printf("Error - invalid comment characters %c\n",
129                        params->comment_character);
130                 return -ENOTSUP;
131         }
132
133         return 0;
134 }
135
136 struct rte_cfgfile *
137 rte_cfgfile_load(const char *filename, int flags)
138 {
139         return rte_cfgfile_load_with_params(filename, flags,
140                                             &default_cfgfile_params);
141 }
142
143 struct rte_cfgfile *
144 rte_cfgfile_load_with_params(const char *filename, int flags,
145                              const struct rte_cfgfile_parameters *params)
146 {
147         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
148         int allocated_entries = 0;
149         int curr_section = -1;
150         int curr_entry = -1;
151         char buffer[256] = {0};
152         int lineno = 0;
153         struct rte_cfgfile *cfg = NULL;
154
155         if (rte_cfgfile_check_params(params))
156                 return NULL;
157
158         FILE *f = fopen(filename, "r");
159         if (f == NULL)
160                 return NULL;
161
162         cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
163                 allocated_sections);
164         if (cfg == NULL)
165                 goto error2;
166
167         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
168
169         if (flags & CFG_FLAG_GLOBAL_SECTION) {
170                 curr_section = 0;
171                 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
172                 cfg->sections[curr_section] = malloc(
173                         sizeof(*cfg->sections[0]) +
174                         sizeof(cfg->sections[0]->entries[0]) *
175                         allocated_entries);
176                 if (cfg->sections[curr_section] == NULL) {
177                         printf("Error - no memory for global section\n");
178                         goto error1;
179                 }
180
181                 snprintf(cfg->sections[curr_section]->name,
182                                  sizeof(cfg->sections[0]->name), "GLOBAL");
183         }
184
185         while (fgets(buffer, sizeof(buffer), f) != NULL) {
186                 char *pos = NULL;
187                 size_t len = strnlen(buffer, sizeof(buffer));
188                 lineno++;
189                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
190                         printf("Error line %d - no \\n found on string. "
191                                         "Check if line too long\n", lineno);
192                         goto error1;
193                 }
194                 pos = memchr(buffer, params->comment_character, sizeof(buffer));
195                 if (pos != NULL) {
196                         *pos = '\0';
197                         len = pos -  buffer;
198                 }
199
200                 len = _strip(buffer, len);
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                                 printf("Error line %d - no terminating '['"
209                                         "character found\n", lineno);
210                                 goto error1;
211                         }
212                         *end = '\0';
213                         _strip(&buffer[1], end - &buffer[1]);
214
215                         /* close off old section and add start new one */
216                         if (curr_section >= 0)
217                                 cfg->sections[curr_section]->num_entries =
218                                         curr_entry + 1;
219                         curr_section++;
220
221                         /* resize overall struct if we don't have room for more
222                         sections */
223                         if (curr_section == allocated_sections) {
224                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
225                                 struct rte_cfgfile *n_cfg = realloc(cfg,
226                                         sizeof(*cfg) + sizeof(cfg->sections[0])
227                                         * allocated_sections);
228                                 if (n_cfg == NULL) {
229                                         curr_section--;
230                                         printf("Error - no more memory\n");
231                                         goto error1;
232                                 }
233                                 cfg = n_cfg;
234                         }
235
236                         /* allocate space for new section */
237                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
238                         curr_entry = -1;
239                         cfg->sections[curr_section] = malloc(
240                                 sizeof(*cfg->sections[0]) +
241                                 sizeof(cfg->sections[0]->entries[0]) *
242                                 allocated_entries);
243                         if (cfg->sections[curr_section] == NULL) {
244                                 printf("Error - no more memory\n");
245                                 goto error1;
246                         }
247
248                         snprintf(cfg->sections[curr_section]->name,
249                                         sizeof(cfg->sections[0]->name),
250                                         "%s", &buffer[1]);
251                 } else {
252                         /* value line */
253                         if (curr_section < 0) {
254                                 printf("Error line %d - value outside of"
255                                         "section\n", lineno);
256                                 goto error1;
257                         }
258
259                         struct rte_cfgfile_section *sect =
260                                 cfg->sections[curr_section];
261                         char *split[2];
262                         if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
263                                 != 2) {
264                                 printf("Error at line %d - cannot split "
265                                         "string\n", lineno);
266                                 goto error1;
267                         }
268
269                         curr_entry++;
270                         if (curr_entry == allocated_entries) {
271                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
272                                 struct rte_cfgfile_section *n_sect = realloc(
273                                         sect, sizeof(*sect) +
274                                         sizeof(sect->entries[0]) *
275                                         allocated_entries);
276                                 if (n_sect == NULL) {
277                                         curr_entry--;
278                                         printf("Error - no more memory\n");
279                                         goto error1;
280                                 }
281                                 sect = cfg->sections[curr_section] = n_sect;
282                         }
283
284                         sect->entries[curr_entry] = malloc(
285                                 sizeof(*sect->entries[0]));
286                         if (sect->entries[curr_entry] == NULL) {
287                                 printf("Error - no more memory\n");
288                                 goto error1;
289                         }
290
291                         struct rte_cfgfile_entry *entry = sect->entries[
292                                 curr_entry];
293                         snprintf(entry->name, sizeof(entry->name), "%s",
294                                 split[0]);
295                         snprintf(entry->value, sizeof(entry->value), "%s",
296                                 split[1]);
297                         _strip(entry->name, strnlen(entry->name,
298                                 sizeof(entry->name)));
299                         _strip(entry->value, strnlen(entry->value,
300                                 sizeof(entry->value)));
301                 }
302         }
303         fclose(f);
304         cfg->flags = flags;
305         cfg->num_sections = curr_section + 1;
306         /* curr_section will still be -1 if we have an empty file */
307         if (curr_section >= 0)
308                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
309         return cfg;
310
311 error1:
312         cfg->num_sections = curr_section + 1;
313         if (curr_section >= 0)
314                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
315         rte_cfgfile_close(cfg);
316 error2:
317         fclose(f);
318         return NULL;
319 }
320
321
322 int rte_cfgfile_close(struct rte_cfgfile *cfg)
323 {
324         int i, j;
325
326         if (cfg == NULL)
327                 return -1;
328
329         for (i = 0; i < cfg->num_sections; i++) {
330                 if (cfg->sections[i] != NULL) {
331                         if (cfg->sections[i]->num_entries) {
332                                 for (j = 0; j < cfg->sections[i]->num_entries;
333                                         j++) {
334                                         if (cfg->sections[i]->entries[j] !=
335                                                 NULL)
336                                                 free(cfg->sections[i]->
337                                                         entries[j]);
338                                 }
339                         }
340                         free(cfg->sections[i]);
341                 }
342         }
343         free(cfg);
344
345         return 0;
346 }
347
348 int
349 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
350 size_t length)
351 {
352         int i;
353         int num_sections = 0;
354         for (i = 0; i < cfg->num_sections; i++) {
355                 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
356                         num_sections++;
357         }
358         return num_sections;
359 }
360
361 int
362 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
363         int max_sections)
364 {
365         int i;
366
367         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
368                 snprintf(sections[i], CFG_NAME_LEN, "%s",
369                 cfg->sections[i]->name);
370
371         return i;
372 }
373
374 static const struct rte_cfgfile_section *
375 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
376 {
377         int i;
378         for (i = 0; i < cfg->num_sections; i++) {
379                 if (strncmp(cfg->sections[i]->name, sectionname,
380                                 sizeof(cfg->sections[0]->name)) == 0)
381                         return cfg->sections[i];
382         }
383         return NULL;
384 }
385
386 int
387 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
388 {
389         return _get_section(cfg, sectionname) != NULL;
390 }
391
392 int
393 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
394         const char *sectionname)
395 {
396         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
397         if (s == NULL)
398                 return -1;
399         return s->num_entries;
400 }
401
402
403 int
404 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
405                 struct rte_cfgfile_entry *entries, int max_entries)
406 {
407         int i;
408         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
409         if (sect == NULL)
410                 return -1;
411         for (i = 0; i < max_entries && i < sect->num_entries; i++)
412                 entries[i] = *sect->entries[i];
413         return i;
414 }
415
416 int
417 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
418                 char *sectionname,
419                 struct rte_cfgfile_entry *entries, int max_entries)
420 {
421         int i;
422         const struct rte_cfgfile_section *sect;
423
424         if (index < 0 || index >= cfg->num_sections)
425                 return -1;
426
427         sect = cfg->sections[index];
428         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
429         for (i = 0; i < max_entries && i < sect->num_entries; i++)
430                 entries[i] = *sect->entries[i];
431         return i;
432 }
433
434 const char *
435 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
436                 const char *entryname)
437 {
438         int i;
439         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
440         if (sect == NULL)
441                 return NULL;
442         for (i = 0; i < sect->num_entries; i++)
443                 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
444                         == 0)
445                         return sect->entries[i]->value;
446         return NULL;
447 }
448
449 int
450 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
451                 const char *entryname)
452 {
453         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
454 }