libmpm

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

commit cde0ca0e549238f5790a38c00c77d0ae3d6ab26c
parent 9045b97790d69f2f5eabfbd14e0eb2e2b5e782fd
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 14 Feb 2017 18:52:14 +0100

Add(Config): Proper error handling, and some more required informations in config_t

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

diff --git a/inc/config.h b/inc/config.h @@ -75,13 +75,17 @@ # define CONFIG_BOOT_GRUB_DIR "grub-dir" # define CONFIG_BOOT_GRUB_DIR_DEF "/boot/grub" +# define ERROR_MAX_LEN 250 + typedef struct s_config { cfg_t *ptr; char *fn; + u8_t ret; } config_t; 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); #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -16,6 +16,8 @@ #include "config.h" +static char g_error[ERROR_MAX_LEN] = ""; + /*! * \brief Initialize a new config_t structure and read a config file * \param path Path of the config file. If null, the default PATH is used @@ -97,6 +99,7 @@ config_t *parse_config(const char *path, u8_t *ret) { 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); @@ -115,12 +118,28 @@ void config_free(config_t *ptr) { if (ptr != NULL) { cfg_free(ptr->ptr); free(ptr); + free(ptr->fn); } } void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap) { char err[250]; - /* TODO: get error through call */ - vsprintf(err, fmt, ap); - printf("\n%s - %d: %s\n", ptr->filename, ptr->line, err); + + (void)ptr; + vsnprintf(err, 250, fmt, ap); + if (strlen(err) < 250) + strcpy(g_error, err); +} + +const char *config_get_error_string(void) { + char *ret; + size_t len; + + len = strlen(g_error); + if (len == 0) + return NULL; + ret = malloc(len + 1); + strcpy(ret, g_error); + strcpy(g_error, ""); + return (const char *)ret; } diff --git a/tests/base.conf b/tests/base.conf @@ -36,8 +36,8 @@ download { # Compile section compile { sbu = 60 - configure-def = {"--prefix=/usr"} - make-def = {"-j2"} + configure-args = {"--prefix=/usr"} + make-args = {"-j2"} } # Packages section