diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2017-06-07 19:56:15 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2017-06-22 20:42:44 +0200 |
commit | 3835b6e49975039a9f72b8920238f3141e7becea (patch) | |
tree | e15f492bfcb7c65ba24495c562d6d58998e40a7c | |
parent | bf534010cb6163bd6ebdef132ee62cccb2b2c9ba (diff) | |
download | liberty-3835b6e49975039a9f72b8920238f3141e7becea.tar.gz liberty-3835b6e49975039a9f72b8920238f3141e7becea.tar.xz liberty-3835b6e49975039a9f72b8920238f3141e7becea.zip |
Improve simple_config_update_from_file()
- considerably shorter
- catch file read errors as we should
- better error messages, now including the filename
- disallow empty keys as they are never used
- allow whitespace before start of comment
NUL characters stop processing now, though. If anyone cares.
-rw-r--r-- | liberty.c | 63 |
1 files changed, 18 insertions, 45 deletions
@@ -3495,14 +3495,10 @@ write_file_safe (const char *filename, const void *data, size_t data_len, // --- Simple configuration ---------------------------------------------------- -// The keys are stripped of surrounding whitespace, the values are not. +// This is the bare minimum to make an application configurable. +// Keys are stripped of surrounding whitespace, values are not. -struct simple_config_item -{ - const char *key; - const char *default_value; - const char *description; -}; +struct simple_config_item { const char *key, *default_value, *description; }; static void simple_config_load_defaults @@ -3520,51 +3516,28 @@ simple_config_update_from_file (struct str_map *config, struct error **e) { char *filename = resolve_filename (PROGRAM_NAME ".conf", resolve_relative_config_filename); - if (!filename) - return true; - - FILE *fp = fopen (filename, "r"); - if (!fp) + struct str s = str_make (); + bool ok = !filename || read_file (filename, &s, e); + size_t line_no = 0; + for (char *x = strtok (s.str, "\r\n"); ok && x; x = strtok (NULL, "\r\n")) { - error_set (e, "could not open `%s' for reading: %s", - filename, strerror (errno)); - free (filename); - return false; - } - - struct str line = str_make (); - bool errors = false; - for (unsigned line_no = 1; read_line (fp, &line); line_no++) - { - char *start = line.str; - if (*start == '#') + line_no++; + if (strchr ("#", *(x += strspn (x, " \t")))) continue; - while (isspace (*start)) - start++; - - char *end = strchr (start, '='); - if (end) - { - char *value = end + 1; - do - *end = '\0'; - while (isspace (*--end)); - - str_map_set (config, start, xstrdup (value)); - } - else if (*start) + char *equals = strchr (x, '='); + if (!equals || equals == x) + ok = error_set (e, "%s: malformed line %zu", filename, line_no); + else { - error_set (e, "line %u in config: %s", line_no, "malformed input"); - errors = true; - break; + char *end = equals++; + do *end = '\0'; while (strchr (" \t", *--end)); + str_map_set (config, x, xstrdup (equals)); } } - - str_free (&line); - fclose (fp); + str_free (&s); free (filename); - return !errors; + return ok; } static char * |