libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 04e2e25e12e93429aad8f9b1127fbc24bfa71796
parent cde0ca0e549238f5790a38c00c77d0ae3d6ab26c
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 14 Feb 2017 19:06:51 +0100

Fix(Config): Better error string

Diffstat:
Minc/config.h | 5++++-
Msrc/config.c | 15+++++++--------
Mtests/base.conf | 1+
Mtests/test_config.c | 3+++
4 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -17,9 +17,12 @@ #ifndef CONFIG_H # define CONFIG_H +# define _GNU_SOURCE /* See feature_test_macros(7) */ +# include <stdio.h> # include <confuse.h> # include <morphux.h> + # define CONFIG_DEF_PATH "/etc/mpm/mpm.conf" /* Main section */ @@ -86,6 +89,6 @@ typedef struct s_config { config_t *parse_config(const char *path, u8_t *ret); void config_free(config_t *ptr); void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap); -const char *config_get_error_string(void); +const char *config_get_error_string(config_t *ptr); #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -96,17 +96,15 @@ config_t *parse_config(const char *path, u8_t *ret) { config = malloc(sizeof(config_t)); assert(config != NULL); + config->fn = NULL; config->ptr = cfg_init(opts, CFGF_NONE); cfg_set_error_function(config->ptr, &config_error_cb); *ret = cfg_parse(config->ptr, path != NULL ? path : CONFIG_DEF_PATH); config->ret = *ret; - if (*ret != 0) { - config_free(config); - return NULL; - } config->fn = malloc(strlen(path != NULL ? path : CONFIG_DEF_PATH)); strcpy(config->fn, path != NULL ? path : CONFIG_DEF_PATH); + return config; } @@ -131,15 +129,16 @@ void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap) { strcpy(g_error, err); } -const char *config_get_error_string(void) { - char *ret; +const char *config_get_error_string(config_t *ptr) { + char *ret = NULL; size_t len; len = strlen(g_error); if (len == 0) return NULL; - ret = malloc(len + 1); - strcpy(ret, g_error); + asprintf(&ret, "%s:%d: %s", ptr->fn, ptr->ptr->line, g_error); + +clean: strcpy(g_error, ""); return (const char *)ret; } diff --git a/tests/base.conf b/tests/base.conf @@ -38,6 +38,7 @@ compile { sbu = 60 configure-args = {"--prefix=/usr"} make-args = {"-j2"} + oui } # Packages section diff --git a/tests/test_config.c b/tests/test_config.c @@ -5,6 +5,9 @@ TEST(config_basic) { config_t *conf = NULL; conf = parse_config("base.conf", &ret); + if (ret != 0) { + m_panic("Error: %s\n", config_get_error_string(conf)); + } TEST_ASSERT((ret == 0), "Can't open configuration file"); config_free(conf); return TEST_SUCCESS;