[v2,20/30] lib/param_list: Add new parameter list routines
diff mbox series

Message ID 22b172c971f9e5989aca8346f539cf14e8a212d5.1533230644.git.geoff@infradead.org
State Accepted
Headers show
Series
  • [v2,01/30] docker: Add libfdt-dev
Related show

Commit Message

Geoff Levand Aug. 2, 2018, 5:29 p.m. UTC
Based on the powerpc param routines adds new generic routines
to manage a name + value parameter list.

Signed-off-by: Geoff Levand <geoff@infradead.org>
---
 lib/Makefile.am             |   4 +-
 lib/param_list/param_list.c | 118 ++++++++++++++++++++++++++++++++++++++++++++
 lib/param_list/param_list.h |  48 ++++++++++++++++++
 3 files changed, 169 insertions(+), 1 deletion(-)
 create mode 100644 lib/param_list/param_list.c
 create mode 100644 lib/param_list/param_list.h

Patch
diff mbox series

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 59d37ab..016a14d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -67,7 +67,9 @@  lib_libpbcore_la_SOURCES = \
 	lib/flash/flash.h \
 	lib/security/security.h \
 	lib/efi/efivar.h \
-	lib/efi/efivar.c
+	lib/efi/efivar.c \
+	lib/param_list/param_list.c \
+	lib/param_list/param_list.h
 
 if ENABLE_MTD
 lib_libpbcore_la_SOURCES += \
diff --git a/lib/param_list/param_list.c b/lib/param_list/param_list.c
new file mode 100644
index 0000000..b3a45f8
--- /dev/null
+++ b/lib/param_list/param_list.c
@@ -0,0 +1,118 @@ 
+
+#define _GNU_SOURCE
+
+#include <assert.h>
+#include <string.h>
+
+#include <log/log.h>
+#include <talloc/talloc.h>
+#include <param_list/param_list.h>
+
+const char **common_known_params(void)
+{
+	static const char *common[] = {
+		"auto-boot?",
+		"petitboot,network",
+		"petitboot,timeout",
+		"petitboot,bootdevs",
+		"petitboot,language",
+		"petitboot,debug?",
+		"petitboot,write?",
+		"petitboot,snapshots?",
+		"petitboot,console",
+		"petitboot,http_proxy",
+		"petitboot,https_proxy",
+		NULL,
+	};
+
+	return common;
+}
+
+void param_list_init(struct param_list *pl, const char *known_params[])
+{
+	assert(known_params);
+	list_init(&pl->params);
+	pl->known_params = known_params;
+}
+
+bool param_list_is_known_n(const struct param_list *pl, const char *name,
+	unsigned int name_len)
+{
+	const char **known;
+
+	assert(pl->known_params);
+
+	param_list_for_each_known_param(pl, known) {
+		if (name_len == strlen(*known) && !strncmp(name, *known, name_len)) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
+bool param_list_is_known(const struct param_list *pl, const char *name)
+{
+	const char **known;
+
+	assert(pl->known_params);
+
+	param_list_for_each_known_param(pl, known) {
+		if (!strcmp(name, *known)) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
+struct param *param_list_get_param(struct param_list *pl, const char *name)
+{
+	struct param *param;
+
+	param_list_for_each(pl, param) {
+		if (!strcmp(param->name, name))
+			return param;
+	}
+	return NULL;
+}
+
+void param_list_set(struct param_list *pl, const char *name, const char *value,
+	bool modified_on_create)
+{
+	struct param *param;
+
+	param_list_for_each(pl, param) {
+		if (strcmp(param->name, name))
+			continue;
+
+		if (!strcmp(param->value, value))
+			return;
+
+		/* Update existing list entry. */
+		talloc_free(param->value);
+		param->value = talloc_strdup(param, value);
+		param->modified = true;
+		pb_debug_fn("Updated: %s:%s\n", name, value);
+		return;
+	}
+
+	/* Add new entry to list. */
+	param = talloc(pl, struct param);
+	param->modified = modified_on_create;
+	param->name = talloc_strdup(pl, name);
+	param->value = talloc_strdup(pl, value);
+	list_add(&pl->params, &param->list);
+	pb_debug_fn("Created: %s:%s\n", name, value);
+}
+
+void param_list_set_non_empty(struct param_list *pl, const char *name, const char *value,
+	bool modified_on_create)
+{
+	if (!param_list_get_value(pl, name) && !strlen(value)) {
+		return;
+	}
+
+	param_list_set(pl, name, value, modified_on_create);
+}
+
diff --git a/lib/param_list/param_list.h b/lib/param_list/param_list.h
new file mode 100644
index 0000000..89ef400
--- /dev/null
+++ b/lib/param_list/param_list.h
@@ -0,0 +1,48 @@ 
+#ifndef PARAM_LIST_H
+#define PARAM_LIST_H
+
+#include <stddef.h>
+
+#include <list/list.h>
+
+struct param {
+	char *name;
+	char *value;
+	bool modified;
+	struct list_item list;
+};
+
+struct param_list {
+	struct list params;
+	const char **known_params;
+};
+
+#define param_list_for_each(_pl_ptr, _pos) \
+	list_for_each_entry(&(_pl_ptr)->params, _pos, list)
+
+#define param_list_for_each_known_param(_pl_ptr, _pos) \
+	for (_pos = (_pl_ptr)->known_params; *_pos; _pos++)
+
+const char **common_known_params(void);
+
+void param_list_init(struct param_list *pl, const char *known_params[]);
+bool param_list_is_known(const struct param_list *pl, const char *name);
+bool param_list_is_known_n(const struct param_list *pl, const char *name,
+	unsigned int name_len);
+struct param *param_list_get_param(struct param_list *pl, const char *name);
+static inline const char *param_list_get_value(const struct param_list *pl,
+	const char *name)
+{
+	const struct param *param =
+		param_list_get_param((struct param_list *)pl, name);
+	return param ? param->value : NULL;
+}
+void param_list_set(struct param_list *pl, const char *name, const char *value,
+	bool modified_on_create);
+
+/* param_list_set_non_empty - Won't create a new parameter that would be empty. */
+void param_list_set_non_empty(struct param_list *pl, const char *name,
+	const char *value, bool modified_on_create);
+
+#endif /* PARAM_LIST_H */
+