diff --git a/CMakeLists.txt b/CMakeLists.txt index cd74140..0cc360b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ set(mbusd_SOURCES src/state.c src/sig.c src/sock.c + src/util.c ) add_executable(mbusd ${mbusd_SOURCES}) install(TARGETS mbusd DESTINATION bin) diff --git a/src/cfg.c b/src/cfg.c index 998bc82..545410c 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -34,9 +34,10 @@ #include "cfg.h" #include -#include #include +#include "util.h" + #define CFG_MAX_LINE_LENGTH 200 #define CFG_NAME_MATCH(n) strcmp(n, name) == 0 @@ -81,37 +82,6 @@ cfg_init(void) cfg.replyonbroadcast=0; } -static char * -cfg_rtrim(char *s) -{ - char *p = s + strlen(s); - while (p > s && isspace((unsigned char )(*--p))) - *p = '\0'; - return s; -} - -static char * -cfg_ltrim(const char *s) -{ - while (*s && isspace((unsigned char )(*s))) - s++; - return (char *) s; -} - -static int -cfg_is_empty(char *s) -{ - char *p = s + strlen(s); - if (strlen(s) == 0) - return 1; - while (p > s && isspace((unsigned char )(*--p))) - { //no-op - } - if (p == s && isspace((unsigned char )(*p))) - return 1; - return 0; -} - int cfg_handle_param(char *name, char *value) { @@ -255,15 +225,15 @@ cfg_handle_param(char *name, char *value) { /* report about invalid log level */ CFG_ERR("invalid loglevel value: %s (must be 0-2)", value); # endif - return(0); + return 0; } } else if (CFG_NAME_MATCH("logfile")) { - if (cfg_is_empty(value)) + if (!strlen(value)) { CFG_ERR("missing logfile value", value); - return(0); + return 0; } else if (*value != '/') { @@ -279,9 +249,6 @@ cfg_handle_param(char *name, char *value) } } else strncpy(cfg.logname, value, INTBUFSIZE); - - - #endif } else { @@ -315,7 +282,7 @@ cfg_parse_file(void *file) { lineno++; - start = cfg_ltrim(cfg_rtrim(line)); + start = util_trim(line); if (*start == '#') { @@ -330,8 +297,8 @@ cfg_parse_file(void *file) { *end = '\0'; - name = cfg_rtrim(start); - value = cfg_ltrim(cfg_rtrim(end + 1)); + name = util_rtrim(start); + value = util_trim(end + 1); /* handle name/value pair */ if (!cfg_handle_param(name, value)) diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..f135995 --- /dev/null +++ b/src/util.c @@ -0,0 +1,80 @@ +/* + * OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway + * + * util.h - utility functions + * + * Copyright (c) 2002-2023, Victor Antonovich (v.antonovich@gmail.com) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "util.h" + +#include +#include + +/** + * Removes leading whitespace from the string. + * Parameters: s - the string to be trimmed. + * Return: a pointer to the first non-whitespace character in string. + */ +char * +util_ltrim(const char *s) +{ + while (*s && isspace((unsigned char )(*s))) + s++; + return (char *) s; +} + +/** + * Removes trailing whitespace from the string. + * The first trailing whitespace is replaced with a NUL-terminator + * in the given string. + * Parameters: s - the string to be trimmed. + * Return: a pointer to the string. + */ +char * +util_rtrim(char *s) +{ + char *p = s + strlen(s); + while (p > s && isspace((unsigned char )(*--p))) + *p = '\0'; + return s; +} + +/** + * Removes leading and trailing whitespace from the string. + * The first trailing whitespace is replaced with a NUL-terminator + * in the given string. + * Parameters: s - the string to be trimmed. + * Return: a pointer to the first non-whitespace character in string. + */ +char * +util_trim(char *s) +{ + if (!strlen(s)) + return s; + + return util_ltrim(util_rtrim(s)); +} \ No newline at end of file diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..a4ba7dc --- /dev/null +++ b/src/util.h @@ -0,0 +1,39 @@ +/* + * OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway + * + * util.h - utility functions + * + * Copyright (c) 2002-2023, Victor Antonovich (v.antonovich@gmail.com) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _UTIL_H +#define _UTIL_H + +char *util_ltrim(const char *s); +char *util_rtrim(char *s); +char *util_trim(char *s); + +#endif /* _UTIL_H */ \ No newline at end of file