From: Michael Tremer Date: Mon, 30 Jan 2023 02:26:41 +0000 (+0000) Subject: networkd: Add scaffolding to read configuration files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c81a6335fd0e1912fa3e8268ba85b9cd048cf243;p=people%2Fms%2Fnetwork.git networkd: Add scaffolding to read configuration files Signed-off-by: Michael Tremer --- diff --git a/src/networkd/config.c b/src/networkd/config.c index d18fb12b..e808531b 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -18,9 +18,11 @@ # # #############################################################################*/ +#include #include #include "config.h" +#include "logging.h" struct nw_config { int nrefs; @@ -56,3 +58,51 @@ struct nw_config* nw_config_unref(struct nw_config* config) { nw_config_free(config); return NULL; } + +static int nw_config_parse(struct nw_config* config, FILE* f) { + // XXX TODO + + return 0; +} + +int nw_config_readf(struct nw_config** config, FILE* f) { + int r; + + // Create a new config object + r = nw_config_create(config); + if (r) + return r; + + // Parse the configuration + r = nw_config_parse(*config, f); + if (r) + goto ERROR; + + return 0; + +ERROR: + nw_config_free(*config); + return r; +} + +int nw_config_read(struct nw_config** config, const char* path) { + FILE* f = NULL; + int r; + + // Open the file + f = fopen(path, "r"); + if (!f) { + ERROR("Could not read configuration file %s: %m\n", path); + r = 1; + goto ERROR; + } + + // Read from file + r = nw_config_readf(config, f); + +ERROR: + if (f) + fclose(f); + + return r; +} diff --git a/src/networkd/config.h b/src/networkd/config.h index a8aed7f0..559ba948 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -28,4 +28,7 @@ int nw_config_create(struct nw_config** config); struct nw_config* nw_config_ref(struct nw_config* config); struct nw_config* nw_config_unref(struct nw_config* config); +int nw_config_readf(struct nw_config** config, FILE* f); +int nw_config_read(struct nw_config** config, const char* path); + #endif /* NETWORKD_CONFIG_H */