From b87722e6ed97aaea7ded638ad585491081faaa8b Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Sat, 23 Jul 2011 22:46:37 +0200 Subject: Moved generic parts of the config file parser to the common library Modified the eurephiadm client_config section to make use of the common version as well. Signed-off-by: David Sommerseth --- common/CMakeLists.txt | 1 + common/eurephia_cfgfile.c | 152 +++++++++++++++++++++++++++++++++++++++++++++ common/eurephia_cfgfile.h | 38 ++++++++++++ eurephiadm/client_config.c | 97 ++--------------------------- 4 files changed, 197 insertions(+), 91 deletions(-) create mode 100644 common/eurephia_cfgfile.c create mode 100644 common/eurephia_cfgfile.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 9fcf8da..8048b0b 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -45,6 +45,7 @@ ADD_LIBRARY( eurephiacommon STATIC eurephia_log.c eurephia_nullsafe.c eurephia_values.c + eurephia_cfgfile.c passwd.c randstr.c sha512.c diff --git a/common/eurephia_cfgfile.c b/common/eurephia_cfgfile.c new file mode 100644 index 0000000..ac47ee7 --- /dev/null +++ b/common/eurephia_cfgfile.c @@ -0,0 +1,152 @@ +/* eurephia_cfgfile.c + * + * Simple generic ini-style config file parser + * + * GPLv2 only - Copyright (C) 2011 + * David Sommerseth + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/** + * @file eurephia_cfgfile.c + * @author David Sommerseth + * @date 2011-07-23 + * + * @brief A simple, generic ini-style config file parser + * + */ + +#include +#include +#include + +#include +#include +#include +#include + + +/** + * Parse one single configuration line into a eurephiaVALUES key/value + * pair. It will also ignore comment lines, and also remove the + * comments on the line of the configuration line so that only the + * key/value information is extracted. + * + * @param line Input configuration line + * + * @return eurephiaVALUES pointer containing the parsed result. On + * error or if no valid config line was found, NULL is returned. + */ +static inline eurephiaVALUES *parse_config_line(eurephiaCTX *ctx, const char *line) { + char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL; + eurephiaVALUES *ret = NULL; + + if( *line == '#' ) { + return NULL; + } + + cp = strdup(line); + key = cp; + val = strpbrk(cp, "="); + if( val == NULL ) { + free_nullsafe(NULL, cp); + return NULL; + } + *val = '\0'; val++; + + // Discard comments at the end of a line + if( (ptr = strpbrk(val, "#")) != NULL ) { + *ptr = '\0'; + } + + // Left trim + while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) { + key++; + } + while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) { + val++; + } + + // Right trim + ptr = key + strlen_nullsafe(key) - 1; + while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) { + ptr--; + } + ptr++; + *ptr = '\0'; + + ptr = val + strlen_nullsafe(val) - 1; + while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) { + ptr--; + } + ptr++; + *ptr = '\0'; + + // Put key/value into a eurephiaVALUES struct and return it + ret = eCreate_value_space(ctx, 20); + ret->key = strdup(key); + ret->val = strdup(val); + + free_nullsafe(ctx, cp); + return ret; +} + + +/** + * Parses a complete config file and puts it into an eurephiaVALUES + * key/value stack + * + * @param fname Complete file name with full path to the configuration + * file to parse + * + * @return Returns a pointer to an eurephiaVALUES stack containing the + * configuration on success, otherwise NULL. + */ +eurephiaVALUES *ecfg_ReadConfig(eurephiaCTX *ctx, const char *fname) +{ + FILE *fp = NULL; + char *buf = NULL; + eurephiaVALUES *cfg = NULL; + struct stat fi; + + if( stat(fname, &fi) == -1 ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "Could not open the config file: %s\n", fname); + return NULL; + } + + if( (fp = fopen(fname, "r")) == NULL ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "Could not open the config file: %s\n", fname); + return NULL; + } + + buf = (char *) malloc_nullsafe(ctx, fi.st_size+2); + + cfg = eCreate_value_space(ctx, 20); + while( fgets(buf, fi.st_size, fp) != NULL ) { + eurephiaVALUES *prm = parse_config_line(ctx, buf); + if( prm != NULL ) { + eAdd_valuestruct(ctx, cfg, prm); + } + }; + free_nullsafe(ctx, buf); + fclose(fp); fp = NULL; + + return cfg; +} + diff --git a/common/eurephia_cfgfile.h b/common/eurephia_cfgfile.h new file mode 100644 index 0000000..b7f38e3 --- /dev/null +++ b/common/eurephia_cfgfile.h @@ -0,0 +1,38 @@ +/* eurephia_cfgfile.h + * + * Simple generic ini-style config file parser + * + * GPLv2 only - Copyright (C) 2011 + * David Sommerseth + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/** + * @file eurephia_cfgfile.h + * @author David Sommerseth + * @date 2011-07-23 + * + * @brief A simple, generic ini-style config file parser + * + */ + +#ifndef EUREPHIA_CFGFILE_H_ +#define EUREPHIA_CFGFILE_H_ + +eurephiaVALUES *ecfg_ReadConfig(eurephiaCTX *ctx, const char *cfgname); + +#endif /* EUREPHIA_CFGFILE_H_ */ diff --git a/eurephiadm/client_config.c b/eurephiadm/client_config.c index 0f5a01f..b01903c 100644 --- a/eurephiadm/client_config.c +++ b/eurephiadm/client_config.c @@ -1,6 +1,6 @@ /* client_config.c -- Handles reading and parsing of config files * - * GPLv2 only - Copyright (C) 2008 - 2010 + * GPLv2 only - Copyright (C) 2008 - 2011 * David Sommerseth * * This program is free software; you can redistribute it and/or @@ -24,7 +24,7 @@ * @author David Sommerseth * @date 2008-12-01 * - * @brief Config file parser + * @brief Config file parser for eurephiadm * */ @@ -36,6 +36,7 @@ #include #include +#include /** * Retrieve a the full path of a file name. Will try to look for the file in different places, like @@ -99,71 +100,6 @@ char *get_config_filename(const char *env, const char *file) { } -/** - * Parse one single configuration line into a eurephiaVALUES key/value pair. It will also ignore - * comment lines, and also remove the comments on the line of the configuration line so that only - * the key/value information is extracted. - * - * @param line Input configuration line - * - * @return eurephiaVALUES pointer containing the parsed result. On error or if no valid config - * line was found, NULL is returned. - */ -eurephiaVALUES *parse_config_line(const char *line) { - char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL;; - eurephiaVALUES *ret = NULL; - - if( *line == '#' ) { - return NULL; - } - - cp = strdup(line); - key = cp; - val = strpbrk(cp, "="); - if( val == NULL ) { - free_nullsafe(NULL, cp); - return NULL; - } - *val = '\0'; val++; - - // Discard comments at the end of a line - if( (ptr = strpbrk(val, "#")) != NULL ) { - *ptr = '\0'; - } - - // Left trim - while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) { - key++; - } - while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) { - val++; - } - - // Right trim - ptr = key + strlen_nullsafe(key) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - ptr = val + strlen_nullsafe(val) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - // Put key/value into a eurephiaVALUES struct and return it - ret = eCreate_value_space(NULL, 20); - ret->key = strdup(key); - ret->val = strdup(val); - - free_nullsafe(NULL, cp); - return ret; -} - - /** * Parses a complete config file and puts it into an eurephiaVALUES key/value stack * @@ -175,10 +111,7 @@ eurephiaVALUES *parse_config_line(const char *line) { */ eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { char *fname = NULL; - FILE *fp = NULL; - char *buf = NULL; eurephiaVALUES *cfg = NULL; - struct stat fi; fname = get_config_filename(env, cfgname); if( fname == NULL ) { @@ -186,27 +119,9 @@ eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { return NULL; } - if( stat(fname, &fi) == -1 ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; - } - - if( (fp = fopen(fname, "r")) == NULL ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; + cfg = ecfg_ReadConfig(NULL, fname); + if( cfg == NULL ) { + fprintf(stderr, "Failed to parse the config file (%s)\n", cfgname); } - - buf = (char *) malloc_nullsafe(NULL, fi.st_size+2); - - cfg = eCreate_value_space(NULL, 20); - while( fgets(buf, fi.st_size, fp) != NULL ) { - eurephiaVALUES *prm = parse_config_line(buf); - if( prm != NULL ) { - eAdd_valuestruct(NULL, cfg, prm); - } - }; - free_nullsafe(NULL, buf); - fclose(fp); fp = NULL; - return cfg; } -- cgit