cfgfile: fix uninitialized variable on load error
[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_string_fns.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 static unsigned
62 _strip(char *str, unsigned len)
63 {
64         int newlen = len;
65         if (len == 0)
66                 return 0;
67
68         if (isspace(str[len-1])) {
69                 /* strip trailing whitespace */
70                 while (newlen > 0 && isspace(str[newlen - 1]))
71                         str[--newlen] = '\0';
72         }
73
74         if (isspace(str[0])) {
75                 /* strip leading whitespace */
76                 int i, start = 1;
77                 while (isspace(str[start]) && start < newlen)
78                         start++
79                         ; /* do nothing */
80                 newlen -= start;
81                 for (i = 0; i < newlen; i++)
82                         str[i] = str[i+start];
83                 str[i] = '\0';
84         }
85         return newlen;
86 }
87
88 struct rte_cfgfile *
89 rte_cfgfile_load(const char *filename, int flags)
90 {
91         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
92         int allocated_entries = 0;
93         int curr_section = -1;
94         int curr_entry = -1;
95         char buffer[256] = {0};
96         int lineno = 0;
97         struct rte_cfgfile *cfg = NULL;
98
99         FILE *f = fopen(filename, "r");
100         if (f == NULL)
101                 return NULL;
102
103         cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
104                 allocated_sections);
105         if (cfg == NULL)
106                 goto error2;
107
108         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
109
110         while (fgets(buffer, sizeof(buffer), f) != NULL) {
111                 char *pos = NULL;
112                 size_t len = strnlen(buffer, sizeof(buffer));
113                 lineno++;
114                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
115                         printf("Error line %d - no \\n found on string. "
116                                         "Check if line too long\n", lineno);
117                         goto error1;
118                 }
119                 pos = memchr(buffer, ';', sizeof(buffer));
120                 if (pos != NULL) {
121                         *pos = '\0';
122                         len = pos -  buffer;
123                 }
124
125                 len = _strip(buffer, len);
126                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
127                         continue;
128
129                 if (buffer[0] == '[') {
130                         /* section heading line */
131                         char *end = memchr(buffer, ']', len);
132                         if (end == NULL) {
133                                 printf("Error line %d - no terminating '['"
134                                         "character found\n", lineno);
135                                 goto error1;
136                         }
137                         *end = '\0';
138                         _strip(&buffer[1], end - &buffer[1]);
139
140                         /* close off old section and add start new one */
141                         if (curr_section >= 0)
142                                 cfg->sections[curr_section]->num_entries =
143                                         curr_entry + 1;
144                         curr_section++;
145
146                         /* resize overall struct if we don't have room for more
147                         sections */
148                         if (curr_section == allocated_sections) {
149                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
150                                 struct rte_cfgfile *n_cfg = realloc(cfg,
151                                         sizeof(*cfg) + sizeof(cfg->sections[0])
152                                         * allocated_sections);
153                                 if (n_cfg == NULL) {
154                                         curr_section--;
155                                         printf("Error - no more memory\n");
156                                         goto error1;
157                                 }
158                                 cfg = n_cfg;
159                         }
160
161                         /* allocate space for new section */
162                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
163                         curr_entry = -1;
164                         cfg->sections[curr_section] = malloc(
165                                 sizeof(*cfg->sections[0]) +
166                                 sizeof(cfg->sections[0]->entries[0]) *
167                                 allocated_entries);
168                         if (cfg->sections[curr_section] == NULL) {
169                                 printf("Error - no more memory\n");
170                                 goto error1;
171                         }
172
173                         snprintf(cfg->sections[curr_section]->name,
174                                         sizeof(cfg->sections[0]->name),
175                                         "%s", &buffer[1]);
176                 } else {
177                         /* value line */
178                         if (curr_section < 0) {
179                                 printf("Error line %d - value outside of"
180                                         "section\n", lineno);
181                                 goto error1;
182                         }
183
184                         struct rte_cfgfile_section *sect =
185                                 cfg->sections[curr_section];
186                         char *split[2];
187                         if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
188                                 != 2) {
189                                 printf("Error at line %d - cannot split "
190                                         "string\n", lineno);
191                                 goto error1;
192                         }
193
194                         curr_entry++;
195                         if (curr_entry == allocated_entries) {
196                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
197                                 struct rte_cfgfile_section *n_sect = realloc(
198                                         sect, sizeof(*sect) +
199                                         sizeof(sect->entries[0]) *
200                                         allocated_entries);
201                                 if (n_sect == NULL) {
202                                         curr_entry--;
203                                         printf("Error - no more memory\n");
204                                         goto error1;
205                                 }
206                                 sect = cfg->sections[curr_section] = n_sect;
207                         }
208
209                         sect->entries[curr_entry] = malloc(
210                                 sizeof(*sect->entries[0]));
211                         if (sect->entries[curr_entry] == NULL) {
212                                 printf("Error - no more memory\n");
213                                 goto error1;
214                         }
215
216                         struct rte_cfgfile_entry *entry = sect->entries[
217                                 curr_entry];
218                         snprintf(entry->name, sizeof(entry->name), "%s",
219                                 split[0]);
220                         snprintf(entry->value, sizeof(entry->value), "%s",
221                                 split[1]);
222                         _strip(entry->name, strnlen(entry->name,
223                                 sizeof(entry->name)));
224                         _strip(entry->value, strnlen(entry->value,
225                                 sizeof(entry->value)));
226                 }
227         }
228         fclose(f);
229         cfg->flags = flags;
230         cfg->num_sections = curr_section + 1;
231         /* curr_section will still be -1 if we have an empty file */
232         if (curr_section >= 0)
233                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
234         return cfg;
235
236 error1:
237         cfg->num_sections = curr_section + 1;
238         if (curr_section >= 0)
239                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
240         rte_cfgfile_close(cfg);
241 error2:
242         fclose(f);
243         return NULL;
244 }
245
246
247 int rte_cfgfile_close(struct rte_cfgfile *cfg)
248 {
249         int i, j;
250
251         if (cfg == NULL)
252                 return -1;
253
254         for (i = 0; i < cfg->num_sections; i++) {
255                 if (cfg->sections[i] != NULL) {
256                         if (cfg->sections[i]->num_entries) {
257                                 for (j = 0; j < cfg->sections[i]->num_entries;
258                                         j++) {
259                                         if (cfg->sections[i]->entries[j] !=
260                                                 NULL)
261                                                 free(cfg->sections[i]->
262                                                         entries[j]);
263                                 }
264                         }
265                         free(cfg->sections[i]);
266                 }
267         }
268         free(cfg);
269
270         return 0;
271 }
272
273 int
274 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
275 size_t length)
276 {
277         int i;
278         int num_sections = 0;
279         for (i = 0; i < cfg->num_sections; i++) {
280                 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
281                         num_sections++;
282         }
283         return num_sections;
284 }
285
286 int
287 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
288         int max_sections)
289 {
290         int i;
291
292         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
293                 snprintf(sections[i], CFG_NAME_LEN, "%s",
294                 cfg->sections[i]->name);
295
296         return i;
297 }
298
299 static const struct rte_cfgfile_section *
300 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
301 {
302         int i;
303         for (i = 0; i < cfg->num_sections; i++) {
304                 if (strncmp(cfg->sections[i]->name, sectionname,
305                                 sizeof(cfg->sections[0]->name)) == 0)
306                         return cfg->sections[i];
307         }
308         return NULL;
309 }
310
311 int
312 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
313 {
314         return _get_section(cfg, sectionname) != NULL;
315 }
316
317 int
318 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
319         const char *sectionname)
320 {
321         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
322         if (s == NULL)
323                 return -1;
324         return s->num_entries;
325 }
326
327
328 int
329 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
330                 struct rte_cfgfile_entry *entries, int max_entries)
331 {
332         int i;
333         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
334         if (sect == NULL)
335                 return -1;
336         for (i = 0; i < max_entries && i < sect->num_entries; i++)
337                 entries[i] = *sect->entries[i];
338         return i;
339 }
340
341 int
342 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
343                 char *sectionname,
344                 struct rte_cfgfile_entry *entries, int max_entries)
345 {
346         int i;
347         const struct rte_cfgfile_section *sect;
348
349         if (index < 0 || index >= cfg->num_sections)
350                 return -1;
351
352         sect = cfg->sections[index];
353         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
354         for (i = 0; i < max_entries && i < sect->num_entries; i++)
355                 entries[i] = *sect->entries[i];
356         return i;
357 }
358
359 const char *
360 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
361                 const char *entryname)
362 {
363         int i;
364         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
365         if (sect == NULL)
366                 return NULL;
367         for (i = 0; i < sect->num_entries; i++)
368                 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
369                         == 0)
370                         return sect->entries[i]->value;
371         return NULL;
372 }
373
374 int
375 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
376                 const char *entryname)
377 {
378         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
379 }