4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include "config-parser.h"
29 #define MAX_SECTION 64
31 static inline char *trim_str(char *s)
35 s += strspn(s, WHITESPACE);
38 for (t = strchr(s, 0); t > s; t--)
39 if (!strchr(WHITESPACE, t[-1]))
45 int config_parse(const char *file_name, int cb(struct parse_result *result,
46 void *user_data), void *user_data)
49 struct parse_result result;
50 /* use stack for parsing */
52 char section[MAX_SECTION];
53 char *start, *end, *name, *value;
54 int lineno = 0, ret = 0;
56 if (!file_name || !cb) {
62 f = fopen(file_name, "r");
64 _E("Failed to open file %s", file_name);
69 /* parsing line by line */
70 while (fgets(line, LINE_MAX, f) != NULL) {
74 start[strcspn(start, NEWLINE)] = '\0';
75 start = trim_str(start);
77 if (*start == COMMENT) {
79 } else if (*start == '[') {
81 end = strchr(start, ']');
82 if (!end || *end != ']') {
88 strncpy(section, start + 1, sizeof(section)-1);
89 section[MAX_SECTION-1] = '\0';
91 /* parse name & value */
92 end = strchr(start, '=');
93 if (!end || *end != '=') {
98 name = trim_str(start);
99 value = trim_str(end + 1);
100 end = strchr(value, COMMENT);
101 if (end && *end == COMMENT) {
103 value = trim_str(value);
106 result.section = section;
108 result.value = value;
109 /* callback with parse result */
110 ret = cb(&result, user_data);
117 _D("Success to load %s", file_name);
124 _E("Failed to read %s:%d!", file_name, lineno);
128 static int config_table_lookup(void *table,
131 ConfigParserCallback *func,
143 for (t = table; t->lvalue; t++) {
145 if (!streq(lvalue, t->lvalue))
148 if (!streq_ptr(section, t->section))
160 /* Run the user supplied parser for an assignment */
161 static int config_parse_table(const char *filename,
168 ConfigParserCallback cb = NULL;
178 r = config_table_lookup(table,
199 int config_parse_new(const char *filename, void *table)
201 _cleanup_fclose_ FILE *f = NULL;
202 char *sections[MAX_SECTION] = { 0 };
203 char *section = NULL, *n, *e, l[LINE_MAX];
205 int i, r, num_section = 0;
211 f = fopen(filename, "r");
213 _E("Failed to open file %s", filename);
218 _cleanup_free_ char *lvalue = NULL, *rvalue = NULL;
221 if (fgets(l, LINE_MAX, f) == NULL) {
225 _E("Failed to parse configuration file '%s': %m", filename);
233 if (strchr(COMMENTS NEWLINE, *l))
238 if (l[len-1] != ']') {
239 _E("Error: Invalid section header: %s", l);
244 n = strndup(l+1, len-2);
251 for (i = 0; i < num_section; i++) {
252 if (streq(n, sections[i])) {
253 section = sections[i];
264 sections[num_section] = n;
266 if (num_section > MAX_SECTION) {
267 _E("Error: max number of section reached: %d", num_section);
280 _D("Warning: config: no '=' character in line '%s'.", l);
284 lvalue = strndup(l, e-l);
301 r = config_parse_table(filename,
314 for (i = 0; i < num_section; i++)
321 int config_parse_dir(const char *dir, ConfigParseFunc fp, void *data)
323 _cleanup_closedir_ DIR *d = NULL;
328 _E("Failed to open dir: %s", dir);
332 for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) {
337 } else if (streq(de->d_name, ".") || streq(de->d_name, "..")) {
341 _cleanup_free_ char *path = NULL;
344 if (de->d_type != DT_REG)
347 r = asprintf(&path, "%s/%s", dir, de->d_name);
352 /* Do not just break loop until parse all file of
353 * dir. Just only put log */
355 _D("Failed to parse config: %s", de->d_name);
361 int config_parse_bool(const char *filename,
377 k = parse_boolean(rvalue);
379 _E("Failed to parse boolean value, ignoring: %s", rvalue);
388 #define DEFINE_PARSER(type, vartype, conv_func) \
389 int config_parse_##type( \
390 const char *filename, \
392 const char *section, \
393 const char *lvalue, \
395 const char *rvalue, \
405 *i = conv_func(rvalue); \
409 DEFINE_PARSER(int, int, atoi)
410 DEFINE_PARSER(float, float, atof)
411 DEFINE_PARSER(long, long, atol)
413 int config_parse_string(const char *filename,
428 if (is_empty(rvalue))
442 int config_parse_bytes(const char *filename,
458 if (is_empty(rvalue))
461 r = parse_bytes(rvalue, s);
469 int config_parse_strv(const char *filename,
478 char **o = NULL, **v = NULL, **vv = NULL;
486 if (is_empty(rvalue))
489 r = str_to_strv(rvalue, &v, WHITESPACE);
495 r = strv_attach(o, v, &vv, true);