Begin to use validate function in config handling

This commit is contained in:
Julien Danjou 2008-03-18 13:00:10 +01:00
parent 214a5c4bfa
commit 9204066788
5 changed files with 35 additions and 8 deletions

View File

@ -33,8 +33,6 @@
#include <sys/stat.h>
#include <string.h>
#include <confuse.h>
#include <X11/Xutil.h>
#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)))
{

View File

@ -26,8 +26,6 @@
#include <unistd.h>
#include <stdlib.h>
#include <confuse.h>
#include <X11/Xlib.h>
#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)))
{

View File

@ -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;
}

View File

@ -22,6 +22,9 @@
#ifndef AWESOME_COMMON_CONFIGOPTS_H
#define AWESOME_COMMON_CONFIGOPTS_H
#include <confuse.h>
cfg_t * cfg_new(void);
char * config_file(void);
int config_check(const char *);

View File

@ -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);