chiark / gitweb /
conf-parser: generate 7 parsing functions from a macro
[elogind.git] / src / shared / conf-parser.c
index b09e90ae8bd40c672c338b1073f09fa34d6f6819..a98805e215106c91bc0692e5966b109f136624de 100644 (file)
@@ -174,7 +174,7 @@ static int parse_line(
         if (!*l)
                 return 0;
 
-        if (strchr(COMMENTS, *l))
+        if (strchr(COMMENTS "\n", *l))
                 return 0;
 
         if (startswith(l, ".include ")) {
@@ -262,23 +262,19 @@ int config_parse(
                 void *userdata) {
 
         unsigned line = 0;
-        char *section = NULL;
+        char _cleanup_free_ *section = NULL, *continuation = NULL;
+        FILE _cleanup_fclose_ *ours = NULL;
         int r;
-        bool ours = false;
-        char *continuation = NULL;
 
         assert(filename);
         assert(lookup);
 
         if (!f) {
-                f = fopen(filename, "re");
+                f = ours = fopen(filename, "re");
                 if (!f) {
-                        r = -errno;
-                        log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
-                        goto finish;
+                        log_error("Failed to open configuration file '%s': %m", filename);
+                        return -errno;
                 }
-
-                ours = true;
         }
 
         while (!feof(f)) {
@@ -289,19 +285,16 @@ int config_parse(
                         if (feof(f))
                                 break;
 
-                        r = -errno;
-                        log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
-                        goto finish;
+                        log_error("Failed to read configuration file '%s': %m", filename);
+                        return -errno;
                 }
 
                 truncate_nl(l);
 
                 if (continuation) {
                         c = strappend(continuation, l);
-                        if (!c) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                        if (!c)
+                                return -ENOMEM;
 
                         free(continuation);
                         continuation = NULL;
@@ -323,10 +316,8 @@ int config_parse(
                                 continuation = c;
                         else {
                                 continuation = strdup(l);
-                                if (!continuation) {
-                                        r = -ENOMEM;
-                                        goto finish;
-                                }
+                                if (!continuation)
+                                        return -ENOMEM;
                         }
 
                         continue;
@@ -344,155 +335,46 @@ int config_parse(
                 free(c);
 
                 if (r < 0)
-                        goto finish;
-        }
-
-        r = 0;
-
-finish:
-        free(section);
-        free(continuation);
-
-        if (f && ours)
-                fclose(f);
-
-        return r;
-}
-
-int config_parse_int(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        int *i = data;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = safe_atoi(rvalue, i);
-        if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ingoring: %s", filename, line, rvalue);
-                return 0;
-        }
-
-        return 0;
-}
-
-int config_parse_long(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        long *i = data;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = safe_atoli(rvalue, i);
-        if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
-                return 0;
-        }
-
-        return 0;
-}
-
-int config_parse_uint64(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        uint64_t *u = data;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = safe_atou64(rvalue, u);
-        if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
-                return 0;
+                        return r;
         }
 
         return 0;
 }
 
-int config_parse_unsigned(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
+#define DEFINE_PARSER(type, vartype, conv_func)                         \
+        int config_parse_##type(const char *filename,                   \
+                                unsigned line,                          \
+                                const char *section,                    \
+                                const char *lvalue,                     \
+                                int ltype,                              \
+                                const char *rvalue,                     \
+                                void *data,                             \
+                                void *userdata) {                       \
+                                                                        \
+                vartype *i = data;                                      \
+                int r;                                                  \
+                                                                        \
+                assert(filename);                                       \
+                assert(lvalue);                                         \
+                assert(rvalue);                                         \
+                assert(data);                                           \
+                                                                        \
+                r = conv_func(rvalue, i);                               \
+                if (r < 0)                                              \
+                        log_error("[%s:%u] Failed to parse %s value, ignoring: %s", \
+                                  filename, line, #vartype, rvalue);    \
+                                                                        \
+                return 0;                                               \
+        }
+
+DEFINE_PARSER(int, int, safe_atoi)
+DEFINE_PARSER(long, long, safe_atoli)
+DEFINE_PARSER(uint64, uint64_t, safe_atou64)
+DEFINE_PARSER(unsigned, unsigned, safe_atou)
+DEFINE_PARSER(double, double, safe_atod)
+DEFINE_PARSER(nsec, nsec_t, parse_nsec)
+DEFINE_PARSER(sec, usec_t, parse_sec)
 
-        unsigned *u = data;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = safe_atou(rvalue, u);
-        if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
-                return r;
-        }
-
-        return 0;
-}
-
-int config_parse_double(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        double *d = data;
-        int r;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        r = safe_atod(rvalue, d);
-        if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
-                return r;
-        }
-
-        return 0;
-}
 
 int config_parse_bytes_size(
                 const char *filename,
@@ -705,9 +587,18 @@ int config_parse_strv(
         assert(data);
 
         if (isempty(rvalue)) {
-                /* Empty assignment resets the list */
+                char **empty;
+
+                /* Empty assignment resets the list. As a special rule
+                 * we actually fill in a real empty array here rather
+                 * than NULL, since some code wants to know if
+                 * something was set at all... */
+                empty = strv_new(NULL, NULL);
+                if (!empty)
+                        return log_oom();
+
                 strv_free(*sv);
-                *sv = NULL;
+                *sv = empty;
                 return 0;
         }
 
@@ -783,56 +674,6 @@ int config_parse_path_strv(
         return 0;
 }
 
-int config_parse_usec(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        usec_t *usec = data;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        if (parse_usec(rvalue, usec) < 0) {
-                log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
-                return 0;
-        }
-
-        return 0;
-}
-
-int config_parse_nsec(
-                const char *filename,
-                unsigned line,
-                const char *section,
-                const char *lvalue,
-                int ltype,
-                const char *rvalue,
-                void *data,
-                void *userdata) {
-
-        nsec_t *nsec = data;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-        assert(data);
-
-        if (parse_nsec(rvalue, nsec) < 0) {
-                log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
-                return 0;
-        }
-
-        return 0;
-}
-
 int config_parse_mode(
                 const char *filename,
                 unsigned line,