All of lore.kernel.org
 help / color / mirror / Atom feed
From: QI Fuli <fukuri.sai@gmail.com>
To: nvdimm@lists.linux.dev
Cc: QI Fuli <qi.fuli@fujitsu.com>
Subject: [ndctl PATCH 2/5] ndctl, util: add parse-configs helper
Date: Mon, 23 Aug 2021 05:30:12 +0900	[thread overview]
Message-ID: <20210822203015.528438-3-qi.fuli@fujitsu.com> (raw)
In-Reply-To: <20210822203015.528438-1-qi.fuli@fujitsu.com>

From: QI Fuli <qi.fuli@fujitsu.com>

Add parse-config util to help ndctl commands parse ndctl global
configuration file(s).

Signed-off-by: QI Fuli <qi.fuli@fujitsu.com>
---
 Makefile.am          |  2 ++
 util/parse-configs.c | 82 ++++++++++++++++++++++++++++++++++++++++++++
 util/parse-configs.h | 34 ++++++++++++++++++
 3 files changed, 118 insertions(+)
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h

diff --git a/Makefile.am b/Makefile.am
index 960b5e9..6e50741 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -74,6 +74,8 @@ noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
 	util/parse-options.c \
 	util/parse-options.h \
+	util/parse-configs.c \
+	util/parse-configs.h \
 	util/usage.c \
 	util/size.c \
 	util/main.c \
diff --git a/util/parse-configs.c b/util/parse-configs.c
new file mode 100644
index 0000000..b7ae1f0
--- /dev/null
+++ b/util/parse-configs.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include <errno.h>
+#include <util/parse-configs.h>
+#include <util/strbuf.h>
+#include <ccan/ciniparser/ciniparser.h>
+
+static void set_str_val(const char **value, char *val)
+{
+	struct strbuf buf = STRBUF_INIT;
+	size_t len = *value ? strlen(*value) : 0;
+
+	if (!val)
+		return;
+
+	if (len) {
+		strbuf_add(&buf, *value, len);
+		strbuf_addstr(&buf, " ");
+	}
+	strbuf_addstr(&buf, val);
+	*value = strbuf_detach(&buf, NULL);
+}
+
+static int parse_config_file(const char *config_file,
+			const struct config *configs)
+{
+	dictionary *dic;
+
+	dic = ciniparser_load(config_file);
+	if (!dic)
+		return -errno;
+
+	for (; configs->type != CONFIG_END; configs++) {
+		switch (configs->type) {
+		case CONFIG_STRING:
+			set_str_val((const char **)configs->value,
+					ciniparser_getstring(dic,
+					configs->key, configs->defval));
+			break;
+		case MONITOR_CALLBACK:
+		case CONFIG_END:
+			break;
+		}
+	}
+
+	ciniparser_freedict(dic);
+	return 0;
+}
+
+int parse_configs_prefix(const char *__config_files, const char *prefix,
+				const struct config *configs)
+{
+	char *config_files, *save;
+	const char *config_file;
+	int rc;
+
+	config_files = strdup(__config_files);
+	if (!config_files)
+		return -ENOMEM;
+
+	for (config_file = strtok_r(config_files, " ", &save); config_file;
+				config_file = strtok_r(NULL, " ", &save)) {
+
+		if (strncmp(config_file, "./", 2) != 0)
+			fix_filename(prefix, &config_file);
+
+		if ((configs->type == MONITOR_CALLBACK) &&
+				(strcmp(config_file, configs->key) == 0))
+			rc = configs->callback(configs, configs->key);
+		else
+			rc = parse_config_file(config_file, configs);
+
+		if (rc)
+			goto end;
+	}
+
+ end:
+	free(config_files);
+	return rc;
+
+}
diff --git a/util/parse-configs.h b/util/parse-configs.h
new file mode 100644
index 0000000..f70f58f
--- /dev/null
+++ b/util/parse-configs.h
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <util/util.h>
+
+enum parse_conf_type {
+	CONFIG_STRING,
+	CONFIG_END,
+	MONITOR_CALLBACK,
+};
+
+struct config;
+typedef int parse_conf_cb(const struct config *, const char *config_file);
+
+struct config {
+	enum parse_conf_type type;
+	const char *key;
+	void *value;
+	void *defval;
+	parse_conf_cb *callback;
+};
+
+#define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
+
+#define CONF_END() { .type = CONFIG_END }
+#define CONF_STR(k,v,d) \
+	{ .type = CONFIG_STRING, .key = (k), .value = check_vtype(v, const char **), .defval = (d) }
+#define CONF_MONITOR(k,f) \
+	{ .type = MONITOR_CALLBACK, .key = (k), .callback = (f)}
+
+int parse_configs_prefix(const char *__config_file, const char *prefix,
+				const struct config *configs);
-- 
2.31.1


  parent reply	other threads:[~2021-08-22 20:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-22 20:30 [ndctl PATCH 0/5] make ndctl support global configuration files QI Fuli
2021-08-22 20:30 ` [ndctl PATCH 1/5] ndctl, ccan: import ciniparser QI Fuli
2021-08-23 16:48   ` Dan Williams
2021-08-23 19:41     ` Verma, Vishal L
2021-08-24  2:30       ` qi.fuli
2021-08-22 20:30 ` QI Fuli [this message]
2021-08-22 20:30 ` [ndctl PATCH 3/5] ndctl: make ndctl support configuration files QI Fuli
2021-08-22 20:30 ` [ndctl PATCH 4/5] ndctl, config: add the default ndctl configuration file QI Fuli
2021-08-22 20:30 ` [ndctl PATCH 5/5] ndctl, monitor: refactor monitor for supporting multiple config files QI Fuli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210822203015.528438-3-qi.fuli@fujitsu.com \
    --to=fukuri.sai@gmail.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=qi.fuli@fujitsu.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.