From 920406678808c716c6bbe8a0be5403bed1e0599b Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 18 Mar 2008 13:00:10 +0100 Subject: [PATCH] Begin to use validate function in config handling --- awesome-menu.c | 4 +--- awesome-message.c | 4 +--- common/configopts.c | 30 +++++++++++++++++++++++++++++- common/configopts.h | 3 +++ config.c | 2 +- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/awesome-menu.c b/awesome-menu.c index d4867bfe5..cc205283e 100644 --- a/awesome-menu.c +++ b/awesome-menu.c @@ -33,8 +33,6 @@ #include #include -#include - #include #include "common/swindow.h" @@ -131,7 +129,7 @@ config_parse(int screen, const char *confpatharg, else confpath = a_strdup(confpatharg); - cfg = cfg_init(awesome_opts, CFGF_NONE); + cfg = cfg_new(); switch((ret = cfg_parse(cfg, confpath))) { diff --git a/awesome-message.c b/awesome-message.c index 56083909e..99bc9437b 100644 --- a/awesome-message.c +++ b/awesome-message.c @@ -26,8 +26,6 @@ #include #include -#include - #include #include "common/swindow.h" @@ -75,7 +73,7 @@ config_parse(int screen, const char *confpatharg) else confpath = a_strdup(confpatharg); - cfg = cfg_init(awesome_opts, CFGF_NONE); + cfg = cfg_new(); switch((ret = cfg_parse(cfg, confpath))) { diff --git a/common/configopts.c b/common/configopts.c index 4a8c42a7d..fa4bf02bc 100644 --- a/common/configopts.c +++ b/common/configopts.c @@ -363,6 +363,33 @@ config_file(void) return confpath; } +static int +config_validate_unsigned_int(cfg_t *cfg, cfg_opt_t *opt) +{ + int value = cfg_opt_getnint(opt, cfg_opt_size(opt) - 1); + + if(value < 0) + { + cfg_error(cfg, "integer option '%s' must be positive in section '%s'", + opt->name, cfg->name); + return -1; + } + return 0; +} + +cfg_t * +cfg_new(void) +{ + cfg_t *cfg; + + cfg = cfg_init(awesome_opts, CFGF_NONE); + + cfg_set_validate_func(cfg, "screen|general|snap", config_validate_unsigned_int); + cfg_set_validate_func(cfg, "screen|general|border", config_validate_unsigned_int); + + return cfg; +} + /** Check configuration file syntax in regard of libconfuse parsing * \param path to config file * \return status returned by cfg_parse() @@ -374,7 +401,7 @@ config_check(const char *confpatharg) int ret; char *confpath; - cfg = cfg_init(awesome_opts, CFGF_NONE); + cfg = cfg_new(); if(confpatharg) confpath = a_strdup(confpatharg); @@ -395,6 +422,7 @@ config_check(const char *confpatharg) } p_delete(&confpath); + cfg_free(cfg); return ret; } diff --git a/common/configopts.h b/common/configopts.h index dc1fe9ce2..84b4d35de 100644 --- a/common/configopts.h +++ b/common/configopts.h @@ -22,6 +22,9 @@ #ifndef AWESOME_COMMON_CONFIGOPTS_H #define AWESOME_COMMON_CONFIGOPTS_H +#include + +cfg_t * cfg_new(void); char * config_file(void); int config_check(const char *); diff --git a/config.c b/config.c index 4e1772e83..c6acb741d 100644 --- a/config.c +++ b/config.c @@ -488,7 +488,7 @@ config_parse(const char *confpatharg) globalconf.configpath = a_strdup(confpath); - cfg = cfg_init(awesome_opts, CFGF_NONE); + cfg = cfg_new(); ret = cfg_parse(cfg, confpath);