config: support sections in config_ api
This commit wraps some functions from iniparser to support sections in
our config files.
The new functions become part of the config_* api and will help to
support the uart mux feature.
Change-Id: I7fa9aa1d60b67458943d5c53a1aeb90406c5fcf3
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/config.c b/config.c
index ed7f052..615c498 100644
--- a/config.c
+++ b/config.c
@@ -32,8 +32,8 @@
#include <iniparser/iniparser.h>
-#include "config.h"
#include "config-internal.h"
+#include "config.h"
#include "util.h"
static const char *config_default_filename = SYSCONFDIR "/obmc-console.conf";
@@ -109,6 +109,27 @@
return config;
}
+const char *config_get_section_value(struct config *config, const char *secname,
+ const char *name)
+{
+ char buf[CONFIG_MAX_KEY_LENGTH];
+ int rc;
+
+ rc = snprintf(buf, sizeof(buf), "%s:%s", secname, name);
+ if (rc < 0) {
+ return NULL;
+ }
+
+ if ((size_t)rc >= sizeof(buf)) {
+ // error / key too long for the buffer
+ warnx("config: section:key too long for buffer: '%s':'%s'",
+ secname, name);
+ return NULL;
+ }
+
+ return iniparser_getstring(config->dict, buf, NULL);
+}
+
void config_fini(struct config *config)
{
if (!config) {
@@ -286,3 +307,13 @@
return DEFAULT_CONSOLE_ID;
}
+
+int config_count_sections(struct config *config)
+{
+ return iniparser_getnsec(config->dict);
+}
+
+const char *config_get_section_name(struct config *config, int i)
+{
+ return iniparser_getsecname(config->dict, i);
+}
diff --git a/config.h b/config.h
index e8f6f8d..0a8a4cd 100644
--- a/config.h
+++ b/config.h
@@ -22,6 +22,8 @@
struct config;
+const char *config_get_section_value(struct config *config, const char *secname,
+ const char *name);
const char *config_get_value(struct config *config, const char *name);
struct config *config_init(const char *filename);
const char *config_resolve_console_id(struct config *config,
@@ -32,3 +34,6 @@
uint32_t parse_baud_to_int(speed_t speed);
speed_t parse_int_to_baud(uint32_t baud);
int config_parse_bytesize(const char *size_str, size_t *size);
+
+int config_count_sections(struct config *config);
+const char *config_get_section_name(struct config *config, int i);