From 6aeae8be93298cbce90d513d8cc4bae27c81d245 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Tue, 23 Oct 2012 11:41:34 +0200 Subject: [PATCH] conf: add "file" callback The "file" callback is used to parse configuration files. If it is not given, the string is parsed via "parse". Signed-off-by: David Herrmann --- src/conf.c | 16 ++++++++++++---- src/conf.h | 11 +++++++++-- src/kmscon_conf.c | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/conf.c b/src/conf.c index 8d4fe5f..7866a3a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -338,6 +338,18 @@ static int parse_kv_pair(struct conf_option *opts, size_t len, else continue; + /* ignore if already set by command-line arguments */ + if (opt->flags & CONF_LOCKED) + return 0; + + if (opt->file) { + ret = opt->file(opt, set, value); + if (ret) + return ret; + opt->flags |= CONF_DONE; + return 0; + } + if (opt->type->flags & CONF_HAS_ARG && !value) { log_error("config option '%s' requires an argument", key); @@ -348,10 +360,6 @@ static int parse_kv_pair(struct conf_option *opts, size_t len, return -EFAULT; } - /* ignore if already set by command-line arguments */ - if (opt->flags & CONF_LOCKED) - return 0; - if (opt->type->parse) { ret = opt->type->parse(opt, set, value); if (ret) diff --git a/src/conf.h b/src/conf.h index a350136..c098559 100644 --- a/src/conf.h +++ b/src/conf.h @@ -161,12 +161,13 @@ struct conf_option { int (*aftercheck) (struct conf_option *opt, int argc, char **argv, int idx); int (*copy) (struct conf_option *opt, const struct conf_option *src); + int (*file) (struct conf_option *opt, bool on, const char *arg); void *mem; void *def; }; -#define CONF_OPTION(_flags, _short, _long, _type, _aftercheck, _copy, _mem, _def) \ - { _flags, _short, "no-" _long, _type, _aftercheck, _copy, _mem, _def } +#define CONF_OPTION(_flags, _short, _long, _type, _aftercheck, _copy, _file, _mem, _def) \ + { _flags, _short, "no-" _long, _type, _aftercheck, _copy, _file, _mem, _def } #define CONF_OPTION_BOOL(_short, _long, _aftercheck, _copy, _mem, _def) \ CONF_OPTION(0, \ _short, \ @@ -174,6 +175,7 @@ struct conf_option { &conf_bool, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ _def) #define CONF_OPTION_INT(_short, _long, _aftercheck, _copy, _mem, _def) \ @@ -183,6 +185,7 @@ struct conf_option { &conf_int, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ (void*)(unsigned long)_def) #define CONF_OPTION_UINT(_short, _long, _aftercheck, _copy, _mem, _def) \ @@ -192,6 +195,7 @@ struct conf_option { &conf_uint, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ (void*)(unsigned long)_def) #define CONF_OPTION_STRING(_short, _long, _aftercheck, _copy, _mem, _def) \ @@ -201,6 +205,7 @@ struct conf_option { &conf_string, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ _def) #define CONF_OPTION_STRING_LIST(_short, _long, _aftercheck, _copy, _mem, _def) \ @@ -210,6 +215,7 @@ struct conf_option { &conf_string_list, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ _def) #define CONF_OPTION_GRAB(_short, _long, _aftercheck, _copy, _mem, _def) \ @@ -219,6 +225,7 @@ struct conf_option { &conf_grab, \ _aftercheck, \ _copy, \ + NULL, \ _mem, \ _def) diff --git a/src/kmscon_conf.c b/src/kmscon_conf.c index 5d521d1..5fb23de 100644 --- a/src/kmscon_conf.c +++ b/src/kmscon_conf.c @@ -357,7 +357,7 @@ int kmscon_conf_new(struct conf_ctx **out) CONF_OPTION_BOOL(0, "silent", NULL, NULL, &conf->silent, false), /* Seat Options */ - CONF_OPTION(0, 0, "vt", &conf_vt, NULL, NULL, &conf->vt, NULL), + CONF_OPTION(0, 0, "vt", &conf_vt, NULL, NULL, NULL, &conf->vt, NULL), CONF_OPTION_BOOL('s', "switchvt", NULL, NULL, &conf->switchvt, false), CONF_OPTION_STRING_LIST(0, "seats", aftercheck_seats, copy_seats, &conf->seats, def_seats),