chiark / gitweb /
make use of logging API wherever appropriate
[elogind.git] / conf-parser.c
index e50597491ba4a2e6cce8fef6b5a890fdc5aa302d..2ea6911257db25ab302c5af7b20ffee3dfbeea9b 100644 (file)
@@ -10,6 +10,7 @@
 #include "util.h"
 #include "macro.h"
 #include "strv.h"
 #include "util.h"
 #include "macro.h"
 #include "strv.h"
+#include "log.h"
 
 #define WHITESPACE " \t\n"
 #define COMMENTS "#;\n"
 
 #define WHITESPACE " \t\n"
 #define COMMENTS "#;\n"
@@ -44,7 +45,7 @@ static int next_assignment(
                 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
         }
 
                 return t->parse(filename, line, section, lvalue, rvalue, t->data, userdata);
         }
 
-        fprintf(stderr, "[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section));
+        log_error("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename, line, lvalue, strna(section));
         return -EBADMSG;
 }
 
         return -EBADMSG;
 }
 
@@ -76,7 +77,7 @@ static char *strip(char *s) {
 }
 
 /* Parse a variable assignment line */
 }
 
 /* Parse a variable assignment line */
-static int parse_line(const char *filename, unsigned line, char **section, const ConfigItem *t, char *l, void *userdata) {
+static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, char *l, void *userdata) {
         char *e, *c, *b;
 
         b = l+strspn(l, WHITESPACE);
         char *e, *c, *b;
 
         b = l+strspn(l, WHITESPACE);
@@ -109,7 +110,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
                         }
                 }
 
                         }
                 }
 
-                r = config_parse(fn, t, userdata);
+                r = config_parse(fn, sections, t, userdata);
                 free(path);
                 return r;
         }
                 free(path);
                 return r;
         }
@@ -122,13 +123,28 @@ static int parse_line(const char *filename, unsigned line, char **section, const
                 assert(k > 0);
 
                 if (b[k-1] != ']') {
                 assert(k > 0);
 
                 if (b[k-1] != ']') {
-                        fprintf(stderr, "[%s:%u] Invalid section header.", filename, line);
+                        log_error("[%s:%u] Invalid section header.", filename, line);
                         return -EBADMSG;
                 }
 
                 if (!(n = strndup(b+1, k-2)))
                         return -ENOMEM;
 
                         return -EBADMSG;
                 }
 
                 if (!(n = strndup(b+1, k-2)))
                         return -ENOMEM;
 
+                if (sections) {
+                        const char * const * i;
+                        bool good = false;
+                        STRV_FOREACH(i, sections)
+                                if (streq(*i, n)) {
+                                        good = true;
+                                        break;
+                                }
+
+                        if (!good) {
+                                free(n);
+                                return -EBADMSG;
+                        }
+                }
+
                 free(*section);
                 *section = n;
 
                 free(*section);
                 *section = n;
 
@@ -136,7 +152,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
         }
 
         if (!(e = strchr(b, '='))) {
         }
 
         if (!(e = strchr(b, '='))) {
-                fprintf(stderr, "[%s:%u] Missing '='.", filename, line);
+                log_error("[%s:%u] Missing '='.", filename, line);
                 return -EBADMSG;
         }
 
                 return -EBADMSG;
         }
 
@@ -147,7 +163,7 @@ static int parse_line(const char *filename, unsigned line, char **section, const
 }
 
 /* Go through the file and parse each line */
 }
 
 /* Go through the file and parse each line */
-int config_parse(const char *filename, const ConfigItem *t, void *userdata) {
+int config_parse(const char *filename, const char* const * sections, const ConfigItem *t, void *userdata) {
         unsigned line = 0;
         char *section = NULL;
         FILE *f;
         unsigned line = 0;
         char *section = NULL;
         FILE *f;
@@ -158,7 +174,7 @@ int config_parse(const char *filename, const ConfigItem *t, void *userdata) {
 
         if (!(f = fopen(filename, "re"))) {
                 r = -errno;
 
         if (!(f = fopen(filename, "re"))) {
                 r = -errno;
-                fprintf(stderr, "Failed to open configuration file '%s': %s", filename, strerror(-r));
+                log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
                 goto finish;
         }
 
                 goto finish;
         }
 
@@ -170,11 +186,11 @@ int config_parse(const char *filename, const ConfigItem *t, void *userdata) {
                                 break;
 
                         r = -errno;
                                 break;
 
                         r = -errno;
-                        fprintf(stderr, "Failed to read configuration file '%s': %s", filename, strerror(-r));
+                        log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
                         goto finish;
                 }
 
                         goto finish;
                 }
 
-                if ((r = parse_line(filename, ++line, &section, t, l, userdata)) < 0)
+                if ((r = parse_line(filename, ++line, &section, sections, t, l, userdata)) < 0)
                         goto finish;
         }
 
                         goto finish;
         }
 
@@ -207,7 +223,7 @@ int config_parse_int(
         assert(data);
 
         if ((r = safe_atoi(rvalue, i)) < 0) {
         assert(data);
 
         if ((r = safe_atoi(rvalue, i)) < 0) {
-                fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
+                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
                 return r;
         }
 
                 return r;
         }
 
@@ -232,7 +248,7 @@ int config_parse_unsigned(
         assert(data);
 
         if ((r = safe_atou(rvalue, u)) < 0) {
         assert(data);
 
         if ((r = safe_atou(rvalue, u)) < 0) {
-                fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
+                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
                 return r;
         }
 
                 return r;
         }
 
@@ -258,7 +274,7 @@ int config_parse_size(
         assert(data);
 
         if ((r = safe_atou(rvalue, &u)) < 0) {
         assert(data);
 
         if ((r = safe_atou(rvalue, &u)) < 0) {
-                fprintf(stderr, "[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
+                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
                 return r;
         }
 
                 return r;
         }
 
@@ -284,7 +300,7 @@ int config_parse_bool(
         assert(data);
 
         if ((k = parse_boolean(rvalue)) < 0) {
         assert(data);
 
         if ((k = parse_boolean(rvalue)) < 0) {
-                fprintf(stderr, "[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
+                log_error("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
                 return k;
         }
 
                 return k;
         }