xref: /dpdk/lib/cfgfile/rte_cfgfile.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
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 RTE_LOG_REGISTER_DEFAULT(cfgfile_logtype, INFO);
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 &&
195 		    (pos == buffer || *(pos-1) != '\\')) {
196 			*pos = '\0';
197 			len = pos -  buffer;
198 		}
199 
200 		len = _strip(buffer, len);
201 		/* skip lines without useful content */
202 		if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
203 			continue;
204 
205 		if (buffer[0] == '[') {
206 			/* section heading line */
207 			char *end = memchr(buffer, ']', len);
208 			if (end == NULL) {
209 				CFG_LOG(ERR,
210 					"line %d - no terminating ']' character found\n",
211 					lineno);
212 				goto error1;
213 			}
214 			*end = '\0';
215 			_strip(&buffer[1], end - &buffer[1]);
216 
217 			rte_cfgfile_add_section(cfg, &buffer[1]);
218 		} else {
219 			/* key and value line */
220 			char *split[2] = {NULL};
221 
222 			split[0] = buffer;
223 			split[1] = memchr(buffer, '=', len);
224 			if (split[1] == NULL) {
225 				CFG_LOG(ERR,
226 					"line %d - no '=' character found\n",
227 					lineno);
228 				goto error1;
229 			}
230 			*split[1] = '\0';
231 			split[1]++;
232 
233 			_strip(split[0], strlen(split[0]));
234 			_strip(split[1], strlen(split[1]));
235 			char *end = memchr(split[1], '\\', strlen(split[1]));
236 
237 			size_t split_len = strlen(split[1]) + 1;
238 			while (end != NULL) {
239 				if (*(end+1) == params->comment_character) {
240 					*end = '\0';
241 					strlcat(split[1], end+1, split_len);
242 				} else
243 					end++;
244 				end = memchr(end, '\\', strlen(end));
245 			}
246 
247 			if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
248 					(*split[1] == '\0')) {
249 				CFG_LOG(ERR,
250 					"line %d - cannot use empty values\n",
251 					lineno);
252 				goto error1;
253 			}
254 
255 			if (cfg->num_sections == 0)
256 				goto error1;
257 
258 			_add_entry(&cfg->sections[cfg->num_sections - 1],
259 					split[0], split[1]);
260 		}
261 	}
262 	fclose(f);
263 	return cfg;
264 error1:
265 	rte_cfgfile_close(cfg);
266 	fclose(f);
267 	return NULL;
268 }
269 
270 struct rte_cfgfile *
271 rte_cfgfile_create(int flags)
272 {
273 	int i;
274 	struct rte_cfgfile *cfg;
275 
276 	/* future proof flags usage */
277 	if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
278 		return NULL;
279 
280 	cfg = malloc(sizeof(*cfg));
281 
282 	if (cfg == NULL)
283 		return NULL;
284 
285 	cfg->flags = flags;
286 	cfg->num_sections = 0;
287 
288 	/* allocate first batch of sections and entries */
289 	cfg->sections = calloc(CFG_ALLOC_SECTION_BATCH,
290 			       sizeof(struct rte_cfgfile_section));
291 	if (cfg->sections == NULL)
292 		goto error1;
293 
294 	cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
295 
296 	for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
297 		cfg->sections[i].entries = calloc(CFG_ALLOC_ENTRY_BATCH,
298 					  sizeof(struct rte_cfgfile_entry));
299 
300 		if (cfg->sections[i].entries == NULL)
301 			goto error1;
302 
303 		cfg->sections[i].num_entries = 0;
304 		cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
305 	}
306 
307 	if (flags & CFG_FLAG_GLOBAL_SECTION)
308 		rte_cfgfile_add_section(cfg, "GLOBAL");
309 
310 	return cfg;
311 error1:
312 	if (cfg->sections != NULL) {
313 		for (i = 0; i < cfg->allocated_sections; i++) {
314 			if (cfg->sections[i].entries != NULL) {
315 				free(cfg->sections[i].entries);
316 				cfg->sections[i].entries = NULL;
317 			}
318 		}
319 		free(cfg->sections);
320 		cfg->sections = NULL;
321 	}
322 	free(cfg);
323 	return NULL;
324 }
325 
326 int
327 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
328 {
329 	int i;
330 
331 	if (cfg == NULL)
332 		return -EINVAL;
333 
334 	if (sectionname == NULL)
335 		return -EINVAL;
336 
337 	/* resize overall struct if we don't have room for more	sections */
338 	if (cfg->num_sections == cfg->allocated_sections) {
339 
340 		struct rte_cfgfile_section *n_sections =
341 				realloc(cfg->sections,
342 				sizeof(struct rte_cfgfile_section) *
343 				((cfg->allocated_sections) +
344 				CFG_ALLOC_SECTION_BATCH));
345 
346 		if (n_sections == NULL)
347 			return -ENOMEM;
348 
349 		for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
350 			n_sections[i + cfg->allocated_sections].num_entries = 0;
351 			n_sections[i +
352 				 cfg->allocated_sections].allocated_entries = 0;
353 			n_sections[i + cfg->allocated_sections].entries = NULL;
354 		}
355 		cfg->sections = n_sections;
356 		cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
357 	}
358 
359 	strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
360 		sizeof(cfg->sections[0].name));
361 	cfg->sections[cfg->num_sections].num_entries = 0;
362 	cfg->num_sections++;
363 
364 	return 0;
365 }
366 
367 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
368 		const char *sectionname, const char *entryname,
369 		const char *entryvalue)
370 {
371 	int ret;
372 
373 	if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
374 			|| (entryvalue == NULL))
375 		return -EINVAL;
376 
377 	if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
378 		return -EEXIST;
379 
380 	/* search for section pointer by sectionname */
381 	struct rte_cfgfile_section *curr_section = _get_section(cfg,
382 								sectionname);
383 	if (curr_section == NULL)
384 		return -EINVAL;
385 
386 	ret = _add_entry(curr_section, entryname, entryvalue);
387 
388 	return ret;
389 }
390 
391 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
392 		const char *entryname, const char *entryvalue)
393 {
394 	int i;
395 
396 	if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
397 		return -EINVAL;
398 
399 	/* search for section pointer by sectionname */
400 	struct rte_cfgfile_section *curr_section = _get_section(cfg,
401 								sectionname);
402 	if (curr_section == NULL)
403 		return -EINVAL;
404 
405 	if (entryvalue == NULL)
406 		entryvalue = "";
407 
408 	for (i = 0; i < curr_section->num_entries; i++)
409 		if (!strcmp(curr_section->entries[i].name, entryname)) {
410 			strlcpy(curr_section->entries[i].value, entryvalue,
411 				sizeof(curr_section->entries[i].value));
412 			return 0;
413 		}
414 
415 	CFG_LOG(ERR, "entry name doesn't exist\n");
416 	return -EINVAL;
417 }
418 
419 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
420 {
421 	int i, j;
422 
423 	if ((cfg == NULL) || (filename == NULL))
424 		return -EINVAL;
425 
426 	FILE *f = fopen(filename, "w");
427 
428 	if (f == NULL)
429 		return -EINVAL;
430 
431 	for (i = 0; i < cfg->num_sections; i++) {
432 		fprintf(f, "[%s]\n", cfg->sections[i].name);
433 
434 		for (j = 0; j < cfg->sections[i].num_entries; j++) {
435 			fprintf(f, "%s=%s\n",
436 					cfg->sections[i].entries[j].name,
437 					cfg->sections[i].entries[j].value);
438 		}
439 	}
440 	return fclose(f);
441 }
442 
443 int rte_cfgfile_close(struct rte_cfgfile *cfg)
444 {
445 	int i;
446 
447 	if (cfg == NULL)
448 		return -1;
449 
450 	if (cfg->sections != NULL) {
451 		for (i = 0; i < cfg->allocated_sections; i++) {
452 			if (cfg->sections[i].entries != NULL) {
453 				free(cfg->sections[i].entries);
454 				cfg->sections[i].entries = NULL;
455 			}
456 		}
457 		free(cfg->sections);
458 		cfg->sections = NULL;
459 	}
460 	free(cfg);
461 	cfg = NULL;
462 
463 	return 0;
464 }
465 
466 int
467 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
468 size_t length)
469 {
470 	int i;
471 	int num_sections = 0;
472 	for (i = 0; i < cfg->num_sections; i++) {
473 		if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
474 			num_sections++;
475 	}
476 	return num_sections;
477 }
478 
479 int
480 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
481 	int max_sections)
482 {
483 	int i;
484 
485 	for (i = 0; i < cfg->num_sections && i < max_sections; i++)
486 		strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
487 
488 	return i;
489 }
490 
491 int
492 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
493 {
494 	return _get_section(cfg, sectionname) != NULL;
495 }
496 
497 int
498 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
499 	const char *sectionname)
500 {
501 	const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
502 	if (s == NULL)
503 		return -1;
504 	return s->num_entries;
505 }
506 
507 int
508 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
509 	char *sectionname, int index)
510 {
511 	if (index < 0 || index >= cfg->num_sections)
512 		return -1;
513 
514 	const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
515 
516 	strlcpy(sectionname, sect->name, CFG_NAME_LEN);
517 	return sect->num_entries;
518 }
519 int
520 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
521 		struct rte_cfgfile_entry *entries, int max_entries)
522 {
523 	int i;
524 	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
525 	if (sect == NULL)
526 		return -1;
527 	for (i = 0; i < max_entries && i < sect->num_entries; i++)
528 		entries[i] = sect->entries[i];
529 	return i;
530 }
531 
532 int
533 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
534 		char *sectionname,
535 		struct rte_cfgfile_entry *entries, int max_entries)
536 {
537 	int i;
538 	const struct rte_cfgfile_section *sect;
539 
540 	if (index < 0 || index >= cfg->num_sections)
541 		return -1;
542 	sect = &cfg->sections[index];
543 	strlcpy(sectionname, sect->name, CFG_NAME_LEN);
544 	for (i = 0; i < max_entries && i < sect->num_entries; i++)
545 		entries[i] = sect->entries[i];
546 	return i;
547 }
548 
549 const char *
550 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
551 		const char *entryname)
552 {
553 	int i;
554 	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
555 	if (sect == NULL)
556 		return NULL;
557 	for (i = 0; i < sect->num_entries; i++)
558 		if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
559 									== 0)
560 			return sect->entries[i].value;
561 	return NULL;
562 }
563 
564 int
565 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
566 		const char *entryname)
567 {
568 	return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
569 }
570