conf: introduce conf_parse_file_f()
authorDavid Herrmann <dh.herrmann@googlemail.com>
Thu, 18 Oct 2012 12:10:48 +0000 (14:10 +0200)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Thu, 18 Oct 2012 12:22:55 +0000 (14:22 +0200)
This new helper allows easily parsing arbitrary filenames. It does the
same as conf_parse_file() but allows giving filenames in printf format.

The other parsers are slightly adjusted so they can be more easily reused
by other projects.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
src/conf.c
src/conf.h
src/kmscon_conf.c

index 44e70d4..77b08d0 100644 (file)
@@ -31,6 +31,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -701,9 +702,17 @@ int conf_parse_file(struct conf_option *opts, size_t len, const char *path)
        size_t size, pos;
        char *buf, *tmp;
 
-       if (!path)
+       if (!opts || !len || !path)
                return -EINVAL;
 
+       if (access(path, F_OK))
+               return 0;
+
+       if (access(path, R_OK)) {
+               log_error("read access to config file %s denied", path);
+               return -EACCES;
+       }
+
        log_info("reading config file %s", path);
        fd = open(path, O_RDONLY | O_CLOEXEC | O_NOCTTY);
        if (fd < 0) {
@@ -747,45 +756,46 @@ out_free:
        return ret;
 }
 
-int conf_parse_all_files(struct conf_option *opts, size_t len)
+int conf_parse_file_f(struct conf_option *opts, size_t len,
+                     const char *format, ...)
 {
-       int ret;
-       const char *file, *home;
+       va_list list;
        char *path;
+       int ret;
 
-       ret = 0;
+       if (!opts || !len || !format)
+               return -EINVAL;
 
-       file = "/etc/kmscon.conf";
-       if (!access(file, F_OK)) {
-               if (access(file, R_OK))
-                       log_warning("config file %s exists but read access was denied",
-                                   file);
-               else
-                       ret = conf_parse_file(opts, len, file);
+       va_start(list, format);
+       ret = vasprintf(&path, format, list);
+       va_end(list);
+
+       if (ret < 0) {
+               log_error("cannot allocate memory for config-file path");
+               return -ENOMEM;
        }
 
+       ret = conf_parse_file(opts, len, path);
+       free(path);
+       return ret;
+}
+
+int conf_parse_standard_files(struct conf_option *opts, size_t len,
+                             const char *fname)
+{
+       int ret;
+       const char *home;
+
+       ret = conf_parse_file_f(opts, len, "/etc/%s.conf", fname);
        if (ret)
-               goto err_out;
+               return ret;
 
        home = getenv("HOME");
        if (home) {
-               ret = asprintf(&path, "%s/.kmscon.conf", home);
-               if (ret < 0) {
-                       log_warning("cannot allocate enough resources to build a config-path");
-                       ret = -ENOMEM;
-               } else {
-                       ret = 0;
-                       if (!access(path, F_OK)) {
-                               if (access(path, R_OK))
-                                       log_warning("config file %s exists but read access was denied",
-                                                   path);
-                               else
-                                       ret = conf_parse_file(opts, len, path);
-                       }
-                       free(path);
-               }
+               ret = conf_parse_file_f(opts, len, "%s/.%s.conf", home, fname);
+               if (ret)
+                       return ret;
        }
 
-err_out:
-       return ret;
+       return 0;
 }
index 3a65af6..8d194fe 100644 (file)
@@ -169,6 +169,9 @@ void conf_free(struct conf_option *opts, size_t len);
 int conf_parse_argv(struct conf_option *opts, size_t len,
                    int argc, char **argv);
 int conf_parse_file(struct conf_option *opts, size_t len, const char *path);
-int conf_parse_all_files(struct conf_option *opts, size_t len);
+int conf_parse_file_f(struct conf_option *opts, size_t len,
+                     const char *format, ...);
+int conf_parse_standard_files(struct conf_option *opts, size_t len,
+                             const char *fname);
 
 #endif /* CONFIG_CONFIG_H */
index 453ef17..db7d817 100644 (file)
@@ -344,7 +344,7 @@ int kmscon_load_config(int argc, char **argv)
 
        log_print_init("kmscon");
 
-       ret = conf_parse_all_files(options, onum);
+       ret = conf_parse_standard_files(options, onum, "kmscon");
        if (ret)
                return ret;