From a54421dd976fd8081e96c11c2621076876c9986b Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Tue, 17 Feb 2026 11:07:37 +0100 Subject: Replace CLI arguments with config-file parser and add integration tests Introduce a reusable key=value config parser (config.h/c) and convert all three programs to read their settings from config files instead of positional command-line arguments. Add example config files in config/ and 6 CTest integration tests covering None/Basic256Sha256/Aes128 with anonymous and user authentication. Remove the now-obsolete launch.sh. --- src/config.h | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/config.h (limited to 'src/config.h') diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..ee29ada --- /dev/null +++ b/src/config.h @@ -0,0 +1,120 @@ +/** + * @file config.h + * @brief Simple key=value configuration file parser. + * + * Parses configuration files with one key=value pair per line. + * Lines starting with '#' are comments. Blank lines are ignored. + * Repeated keys are allowed (used for list-valued settings like + * trustList). + */ + +#ifndef DISCOVERY_CONFIG_H +#define DISCOVERY_CONFIG_H + +#include + +/** + * @brief A single key=value entry from a config file. + * + * Both key and value are heap-allocated, null-terminated strings + * with leading/trailing whitespace trimmed. + */ +typedef struct +{ + char *key; + char *value; +} ConfigEntry; + +/** + * @brief A parsed configuration file. + * + * Holds a dynamic array of ConfigEntry items. Duplicate keys are + * allowed (used for list-valued settings like trustList). + */ +typedef struct +{ + ConfigEntry *entries; + size_t count; + size_t capacity; +} Config; + +/** + * @brief Parses a configuration file into a Config structure. + * + * Each non-blank, non-comment line must contain '=' separating key + * and value. Whitespace is trimmed around both key and value. + * + * @param path Path to the configuration file. + * @param cfg Output: the parsed configuration. Must be freed + * with configFree() when no longer needed. + * @return 0 on success, -1 on error (logged via UA_LOG_ERROR). + */ +int configLoad (const char *path, Config *cfg); + +/** + * @brief Looks up a single-valued key. + * + * Returns the value of the first entry matching @p key, or NULL + * if the key is not present. + * + * @param cfg The parsed configuration. + * @param key The key to search for. + * @return The value string (owned by cfg), or NULL. + */ +const char *configGet (const Config *cfg, const char *key); + +/** + * @brief Looks up a required single-valued key. + * + * Like configGet(), but logs a FATAL error and returns NULL when + * the key is missing. + * + * @param cfg The parsed configuration. + * @param key The key to search for. + * @param program Program name (for error messages). + * @return The value string (owned by cfg), or NULL if missing. + */ +const char *configRequire (const Config *cfg, const char *key, + const char *program); + +/** + * @brief Looks up a required integer-valued key. + * + * Parses the value of @p key as an integer. If the key is + * missing or the value is not a valid integer, logs a FATAL + * error and returns -1. + * + * @param cfg The parsed configuration. + * @param key The key to search for. + * @param program Program name (for error messages). + * @return The parsed integer, or -1 on error. + */ +int configRequireInt (const Config *cfg, const char *key, const char *program); + +/** + * @brief Collects all values for a repeated key. + * + * Allocates an array of char* pointers to the values stored + * in @p cfg. The caller must free the array itself (but not + * the strings, which are owned by cfg). + * + * @param cfg The parsed configuration. + * @param key The key to collect (e.g. "trustList"). + * @param out Output: heap-allocated array of string pointers. + * Set to NULL if the key is not present. + * @param size Output: number of entries in @p out. + */ +void configGetAll (const Config *cfg, const char *key, char ***out, + size_t *size); + +/** + * @brief Frees all memory owned by a Config structure. + * + * After this call the Config is zeroed and must not be used + * unless configLoad() is called again. + * + * @param cfg The configuration to free. + */ +void configFree (Config *cfg); + +#endif /* DISCOVERY_CONFIG_H */ -- cgit v1.2.3