libmpm

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

commit 9045b97790d69f2f5eabfbd14e0eb2e2b5e782fd
parent 32d7ec6730fb4998681a677e28292e619e1c745a
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 14 Feb 2017 17:57:24 +0100

Add(Config): Better macros, handling error

Diffstat:
Minc/config.h | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/config.c | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mtests/base.conf | 70+++++++++++++++++++++++++++++++++++-----------------------------------
Mtests/test_config.c | 6+++---
4 files changed, 199 insertions(+), 115 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -20,38 +20,68 @@ # include <confuse.h> # include <morphux.h> -# define CONFIG_DEF_PATH "/etc/mpm/mpm.conf" +# define CONFIG_DEF_PATH "/etc/mpm/mpm.conf" -# define CONFIG_MAIN_SEC "main" -# define CONFIG_MAIN_INST_MODE "install-mode" -# define CONFIG_MAIN_THREAD_INST "thread-install" +/* Main section */ +# define CONFIG_MAIN_SEC "main" +# define CONFIG_MAIN_INST_MODE "install-mode" +# define CONFIG_MAIN_INST_MODE_DEF "sources" +# define CONFIG_MAIN_THREAD_INST "thread-install" +# define CONFIG_MAIN_THREAD_INST_DEF 0 -# define CONFIG_LOG_SEC "log" -# define CONFIG_LOG_VERBOSE_LVL "verbose-level" -# define CONFIG_LOG_DIR "directory" +/* Log section */ +# define CONFIG_LOG_SEC "log" +# define CONFIG_LOG_VERBOSE_LVL "verbose-level" +# define CONFIG_LOG_VERBOSE_LVL_DEF 1 +# define CONFIG_LOG_DIR "directory" +# define CONFIG_LOG_DIR_DEF "/var/log/mpm" -# define CONFIG_DL_SEC "download" -# define CONFIG_DL_MAIN_SITE "main-site" -# define CONFIG_DL_THREAD_DL "thread-downloading" -# define CONFIG_DL_DIR "directory" -# define CONFIG_DL_MIRROR_LIST "mirror-list" +/* Download section */ +# define CONFIG_DL_SEC "download" +# define CONFIG_DL_MAIN_SITE "main-site" +# define CONFIG_DL_MAIN_SITE_DEF "pkgs.morphux.org" +# define CONFIG_DL_THREAD_DL "thread-downloading" +# define CONFIG_DL_THREAD_DL_DEF 0 +# define CONFIG_DL_DIR "directory" +# define CONFIG_DL_DIR_DEF "/var/cache/mpm/pkgs" +# define CONFIG_DL_MIRROR_LIST "mirror-list" +# define CONFIG_DL_MIRROR_LIST_DEF "/var/lib/mpm/mirrors" -# define CONFIG_COMP_SEC "compile" -# define CONFIG_COMP_SBU "sbu" -# define CONFIG_COMP_CONF_DEF "configure-def" -# define CONFIG_COMP_MAKE_DEF "make-def" +/* Compile section */ +# define CONFIG_COMP_SEC "compile" +# define CONFIG_COMP_SBU "sbu" +# define CONFIG_COMP_SBU_DEF 0 +# define CONFIG_COMP_CONF_ARG "configure-args" +# define CONFIG_COMP_CONF_ARG_DEF "{\"--prefix=/usr\"}" +# define CONFIG_COMP_MAKE_ARG "make-args" +# define CONFIG_COMP_MAKE_ARG_DEF "{-j2}" -# define CONFIG_PKG_SEC "packages" -# define CONFIG_PKG_DB "database" -# define CONFIG_PKG_TREE "tree" +/* Packages section */ +# define CONFIG_PKG_SEC "packages" +# define CONFIG_PKG_DB "database" +# define CONFIG_PKG_DB_DEF "/var/lib/mpm/package.db" +# define CONFIG_PKG_TREE "tree" +# define CONFIG_PKG_TREE_DEF "/var/lib/mpm/tree" -# define CONFIG_KERN_SEC "kernel" -# define CONFIG_KERN_SRC "sources" +/* Kernel section */ +# define CONFIG_KERN_SEC "kernel" +# define CONFIG_KERN_SRC "sources" +# define CONFIG_KERN_SRC_DEF "/usr/src/linux-current/" -# define CONFIG_BOOT_SEC "boot" -# define CONFIG_BOOT_MNT_WHEN_NEEDED "mount-when-needed" -# define CONFIG_BOOT_GRUB_DIR "grub-dir" +/* Boot section */ +# define CONFIG_BOOT_SEC "boot" +# define CONFIG_BOOT_MNT_WHEN_NEEDED "mount-when-needed" +# define CONFIG_BOOT_MNT_WHEN_NEEDED_DEF 0 +# define CONFIG_BOOT_GRUB_DIR "grub-dir" +# define CONFIG_BOOT_GRUB_DIR_DEF "/boot/grub" -u8_t parse_config(const char *path, cfg_t **config); +typedef struct s_config { + cfg_t *ptr; + char *fn; +} 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); #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -16,57 +16,111 @@ #include "config.h" -u8_t parse_config(const char *path, cfg_t **config) { - cfg_opt_t opts_main[] = { - CFG_INT(CONFIG_MAIN_THREAD_INST,0, CFGF_NONE), - CFG_STR(CONFIG_MAIN_INST_MODE, "binary", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_log[] = { - CFG_INT(CONFIG_LOG_VERBOSE_LVL, 1, CFGF_NONE), - CFG_STR(CONFIG_LOG_DIR, "/var/log/mpm", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_dl[] = { - CFG_STR(CONFIG_DL_MAIN_SITE, "pkgs.morphux.org", CFGF_NONE), - CFG_INT(CONFIG_DL_THREAD_DL, 0, CFGF_NONE), - CFG_STR(CONFIG_DL_DIR, "/var/cache/mpm/pkgs/", CFGF_NONE), - CFG_STR(CONFIG_DL_MIRROR_LIST, "/var/lib/mpm/mirrors", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_comp[] = { - CFG_INT(CONFIG_COMP_SBU, 0, CFGF_NONE), - CFG_STR_LIST(CONFIG_COMP_CONF_DEF,"{\"--prefix=/usr\"}", CFGF_NONE), - CFG_STR_LIST(CONFIG_COMP_MAKE_DEF,"{-j2}", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_pkg[] = { - CFG_STR(CONFIG_PKG_DB, "/var/lib/mpm/package.db", CFGF_NONE), - CFG_STR(CONFIG_PKG_TREE, "/var/cache/mpm/tree", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_kern[] = { - CFG_STR(CONFIG_KERN_SRC, "/usr/src/linux-current", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts_boot[] = { - CFG_INT(CONFIG_BOOT_MNT_WHEN_NEEDED, 0, CFGF_NONE), - CFG_STR(CONFIG_BOOT_GRUB_DIR, "/boot/grub", CFGF_NONE), - CFG_END() - }; - cfg_opt_t opts[] = { - CFG_SEC(CONFIG_MAIN_SEC, opts_main, CFGF_NONE), - CFG_SEC(CONFIG_LOG_SEC, opts_log, CFGF_NONE), - CFG_SEC(CONFIG_DL_SEC, opts_dl, CFGF_NONE), - CFG_SEC(CONFIG_COMP_SEC, opts_comp, CFGF_NONE), - CFG_SEC(CONFIG_PKG_SEC, opts_pkg, CFGF_NONE), - CFG_SEC(CONFIG_KERN_SEC, opts_kern, CFGF_NONE), - CFG_SEC(CONFIG_BOOT_SEC, opts_boot, CFGF_NONE), - CFG_END() - }; - u8_t ret; +/*! + * \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 + * \param ret Return code of the parse function + * + * This function will open a file, read a configuration in it. + * If anything is wrong, the ret variable will be set to the proper error value. + * + * \return A freshly allocated structure + */ +config_t *parse_config(const char *path, u8_t *ret) { + config_t *config = NULL; - *config = cfg_init(opts, CFGF_NONE); - ret = cfg_parse(*config, path != NULL ? path : CONFIG_DEF_PATH); - return ret; + /* Main section */ + cfg_opt_t opts_main[] = { + CFG_INT(CONFIG_MAIN_THREAD_INST,CONFIG_MAIN_THREAD_INST_DEF, CFGF_NONE), + CFG_STR(CONFIG_MAIN_INST_MODE, CONFIG_MAIN_INST_MODE_DEF, CFGF_NONE), + CFG_END() + }; + + /* Log section */ + cfg_opt_t opts_log[] = { + CFG_INT(CONFIG_LOG_VERBOSE_LVL, CONFIG_LOG_VERBOSE_LVL_DEF, CFGF_NONE), + CFG_STR(CONFIG_LOG_DIR, CONFIG_LOG_DIR_DEF, CFGF_NONE), + CFG_END() + }; + + /* Download section */ + cfg_opt_t opts_dl[] = { + CFG_STR(CONFIG_DL_MAIN_SITE, CONFIG_DL_MAIN_SITE_DEF, CFGF_NONE), + CFG_INT(CONFIG_DL_THREAD_DL, CONFIG_DL_THREAD_DL_DEF, CFGF_NONE), + CFG_STR(CONFIG_DL_DIR, CONFIG_DL_DIR_DEF, CFGF_NONE), + CFG_STR(CONFIG_DL_MIRROR_LIST, CONFIG_DL_MIRROR_LIST_DEF, CFGF_NONE), + CFG_END() + }; + + /* Compile section */ + cfg_opt_t opts_comp[] = { + CFG_INT(CONFIG_COMP_SBU, CONFIG_COMP_SBU_DEF, CFGF_NONE), + CFG_STR_LIST(CONFIG_COMP_CONF_ARG, CONFIG_COMP_CONF_ARG_DEF, CFGF_NONE), + CFG_STR_LIST(CONFIG_COMP_MAKE_ARG, CONFIG_COMP_MAKE_ARG_DEF, CFGF_NONE), + CFG_END() + }; + + /* Package section */ + cfg_opt_t opts_pkg[] = { + CFG_STR(CONFIG_PKG_DB, CONFIG_PKG_DB_DEF, CFGF_NONE), + CFG_STR(CONFIG_PKG_TREE, CONFIG_PKG_TREE_DEF, CFGF_NONE), + CFG_END() + }; + + /* Kernel section */ + cfg_opt_t opts_kern[] = { + CFG_STR(CONFIG_KERN_SRC, CONFIG_KERN_SRC_DEF, CFGF_NONE), + CFG_END() + }; + + /* Boot section */ + cfg_opt_t opts_boot[] = { + CFG_INT(CONFIG_BOOT_MNT_WHEN_NEEDED, CONFIG_BOOT_MNT_WHEN_NEEDED_DEF, CFGF_NONE), + CFG_STR(CONFIG_BOOT_GRUB_DIR, CONFIG_BOOT_GRUB_DIR_DEF, CFGF_NONE), + CFG_END() + }; + + /* All sections */ + cfg_opt_t opts[] = { + CFG_SEC(CONFIG_MAIN_SEC, opts_main, CFGF_NONE), + CFG_SEC(CONFIG_LOG_SEC, opts_log, CFGF_NONE), + CFG_SEC(CONFIG_DL_SEC, opts_dl, CFGF_NONE), + CFG_SEC(CONFIG_COMP_SEC, opts_comp, CFGF_NONE), + CFG_SEC(CONFIG_PKG_SEC, opts_pkg, CFGF_NONE), + CFG_SEC(CONFIG_KERN_SEC, opts_kern, CFGF_NONE), + CFG_SEC(CONFIG_BOOT_SEC, opts_boot, CFGF_NONE), + CFG_END() + }; + + config = malloc(sizeof(config_t)); + assert(config != 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); + + 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; +} + +/*! + * \brief Free a config_t pointer + * \param ptr Pointer to free + */ +void config_free(config_t *ptr) { + if (ptr != NULL) { + cfg_free(ptr->ptr); + free(ptr); + } +} + +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); } diff --git a/tests/base.conf b/tests/base.conf @@ -2,63 +2,63 @@ # Main section main { - # Installation mode (sources | binary) - install-mode = sources - # Threading install: - # If possible, the package manager will execute concurrent installation - thread-install = 0 + # Installation mode (sources | binary) + install-mode = sources + # Threading install: + # If possible, the package manager will execute concurrent installation + thread-install = 0 } # Log section log { - # Verbose level: - # 0: None - # 1: Basic - # 2: Informative - # 3: Debug - verbose-level = 2 - # Log directory - directory = "/var/log/mpm/" + # Verbose level: + # 0: None + # 1: Basic + # 2: Informative + # 3: Debug + verbose-level = 2 + # Log directory + directory = "/var/log/mpm/" } # Download section download { - # Packages main site - main-site = "pkgs.morphux.org" - # Download multiple archives at the same time - thread-downloading = 0 - # Temporay directory of the downloaded archives - directory = "/var/cache/mpm/pkgs/" - # Mirror lists - mirror-list = "/var/lib/mpm/mirrors" + # Packages main site + main-site = "pkgs.morphux.org" + # Download multiple archives at the same time + thread-downloading = 0 + # Temporay directory of the downloaded archives + directory = "/var/cache/mpm/pkgs/" + # Mirror lists + mirror-list = "/var/lib/mpm/mirrors" } # Compile section compile { - sbu = 60 - configure-def = {"--prefix=/usr"} - make-def = {"-j2"} + sbu = 60 + configure-def = {"--prefix=/usr"} + make-def = {"-j2"} } # Packages section packages { - # Database file path - database = "/var/lib/mpm/package.db" - # Binary tree file path - tree = "/var/cache/mpm/tree" + # Database file path + database = "/var/lib/mpm/package.db" + # Binary tree file path + tree = "/var/cache/mpm/tree" } # Kernel section kernel { - # Kernel source directory - sources = "/usr/src/linux-current" + # Kernel source directory + sources = "/usr/src/linux-current" } # Boot section boot { - # Only mount /boot when needed - # (Grub reconfiguration | Kernel recompilation) - mount-when-needed = 0 - # Grub config directory - grub-dir = "/boot/grub" + # Only mount /boot when needed + # (Grub reconfiguration | Kernel recompilation) + mount-when-needed = 0 + # Grub config directory + grub-dir = "/boot/grub" } diff --git a/tests/test_config.c b/tests/test_config.c @@ -2,11 +2,11 @@ TEST(config_basic) { u8_t ret; - cfg_t *conf = NULL; + config_t *conf = NULL; - ret = parse_config("base.conf", &conf); + conf = parse_config("base.conf", &ret); TEST_ASSERT((ret == 0), "Can't open configuration file"); - cfg_free(conf); + config_free(conf); return TEST_SUCCESS; }