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