Change in libosmocore[master]: Add new osmo-config-merge utility

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Tue Sep 25 17:48:27 UTC 2018


Harald Welte has uploaded this change for review. ( https://gerrit.osmocom.org/11084


Change subject: Add new osmo-config-merge utility
......................................................................

Add new osmo-config-merge utility

This utility allows you to merge an incremental config "patch"
into an osmocom-style config file.

The patch file follows the same syntax as the original config file.

It works by appending the leaf nodes of the patch file to the respective
nodes of the input config file.

This process allows configuration file changes/updates to be performed
in a more stable/reliable way than by means of [unified] diff files,
as they break every time the context lines break.

osmo-config-merge doesn't suffer from this problem, as it understands
the tree-like nature of VTY config files.

NITE: This only works with configuration files that have proper
indenting, i.e. every level in the hierarchy must be indented excatly
one character, not multiple.

Change-Id: I61997a3668cc3a40d12ca023272f6d782e6fbefe
---
M utils/Makefile.am
A utils/osmo-config-merge.c
2 files changed, 216 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/84/11084/1

diff --git a/utils/Makefile.am b/utils/Makefile.am
index d4999bd..fb79190 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -5,12 +5,16 @@
 
 EXTRA_DIST = conv_gen.py conv_codes_gsm.py
 
-bin_PROGRAMS = osmo-arfcn osmo-auc-gen
+bin_PROGRAMS = osmo-arfcn osmo-auc-gen osmo-config-merge
 
 osmo_arfcn_SOURCES = osmo-arfcn.c
 
 osmo_auc_gen_SOURCES = osmo-auc-gen.c
 
+osmo_config_merge_SOURCES = osmo-config-merge.c
+osmo_config_merge_LDADD = $(LDADD) $(TALLOC_LIBS)
+osmo_config_merge_CFLAGS = $(TALLOC_CFLAGS)
+
 if ENABLE_PCSC
 noinst_PROGRAMS = osmo-sim-test
 osmo_sim_test_SOURCES = osmo-sim-test.c
diff --git a/utils/osmo-config-merge.c b/utils/osmo-config-merge.c
new file mode 100644
index 0000000..bd22cef
--- /dev/null
+++ b/utils/osmo-config-merge.c
@@ -0,0 +1,211 @@
+#include <stdio.h>
+#include <string.h>
+
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
+
+struct node {
+	struct node *parent;		/* back-pointer */
+	struct llist_head list;		/* part of parent->children */
+	struct llist_head children;	/* our own children */
+	char *line;
+};
+
+/* allocate a new node */
+static struct node *node_alloc(void *ctx)
+{
+	struct node *node = talloc_zero(ctx, struct node);
+	OSMO_ASSERT(node);
+	INIT_LLIST_HEAD(&node->children);
+	return node;
+}
+
+/* allocate a new node as child of given parent */
+static struct node *node_alloc_child(struct node *parent)
+{
+	struct node *node = node_alloc(parent);
+	node->parent = parent;
+	llist_add_tail(&node->list, &parent->children);
+	return node;
+}
+
+/* find a given child specified by name/line string within given parent */
+static struct node *node_find_child(struct node *parent, const char *line)
+{
+	struct node *n;
+
+	llist_for_each_entry(n, &parent->children, list) {
+		if (!strcmp(line, n->line))
+			return n;
+	}
+	return NULL;
+}
+
+/* count the number of spaces / indent level */
+static int count_indent(const char *line)
+{
+	int i;
+
+	for (i = 0; i < strlen(line); i++) {
+		if (line[i] != ' ')
+			return i;
+	}
+	return i;
+}
+
+/* strip any triling CR / LF */
+static void chomp(char *line)
+{
+	while (1) {
+		int len = strlen(line);
+		if (len == 0)
+			return;
+		char *lastch = &line[len-1];
+		switch (*lastch) {
+		case '\n':
+		case '\r':
+			*lastch = '\0';
+		default:
+			return;
+		}
+	}
+}
+
+/* read a config file and parse it into a tree of nodes */
+static struct node *file_read(void *ctx, const char *fname)
+{
+	struct node *root, *last;
+	FILE *infile;
+	char line[1024];
+	int cur_indent = -1;
+
+	infile = fopen(fname, "r");
+	if (!infile)
+		return NULL;
+
+	root = node_alloc(ctx);
+	last = root;
+	while (fgets(line, sizeof(line), infile)) {
+		chomp(line);
+		int indent = count_indent(line);
+		struct node *n;
+		if (indent > cur_indent) {
+			/* new child to last node */
+			n = node_alloc_child(last);
+		} else if (indent < cur_indent) {
+			for (int i = 0; i < cur_indent - indent; i++) {
+				/* go to parent, add another sibling */
+				if (last->parent)
+					last = last->parent;
+			}
+			n = node_alloc_child(last->parent);
+		} else {
+			/* add a new sibling (child of parent) */
+			n = node_alloc_child(last->parent);
+		}
+		n->line = talloc_strdup(n, line);
+
+		last = n;
+		cur_indent = indent;
+	}
+
+	return root;
+}
+
+static void append_patch(struct node *cfg, struct node *patch)
+{
+	struct node *n;
+
+	llist_for_each_entry(n, &patch->children, list) {
+		if (llist_empty(&n->children)) {
+			struct node *t;
+			/* we are an end-node, i.e. something that needs to be
+			 * patched into the original tree.  We do this by simply
+			 * appending it to the list of siblings */
+			t = node_alloc_child(cfg);
+			t->line = talloc_strdup(t, n->line);
+		} else {
+			struct node *c;
+			/* we need to iterate / recurse further */
+
+			/* try to find the matching original node */
+			c = node_find_child(cfg, n->line);
+			if (!c) {
+				/* create it, if it's missing */
+				c = node_alloc_child(cfg);
+				c->line = talloc_strdup(c, n->line);
+			}
+			append_patch(c, n);
+		}
+	}
+}
+
+
+static int level = -1;
+
+static void dump_node(struct node *root, FILE *out, bool print_node_depth)
+{
+	struct node *n;
+	level++;
+
+	if (root->line) {
+		if (print_node_depth) {
+			for (int i = 0; i < level; i++)
+				fputc('*', out);
+		}
+
+		fprintf(out, "%s\n", root->line);
+	}
+
+	llist_for_each_entry(n, &root->children, list) {
+		dump_node(n, out, print_node_depth);
+	}
+	level--;
+}
+
+static void exit_usage(int rc)
+{
+	fprintf(stderr, "Usage: osmo-config-merge <config-file> <config-patch> [--debug]\n");
+	exit(rc);
+}
+
+
+int main(int argc, char **argv)
+{
+	const char *base_fname, *patch_fname;
+	struct node *base_tree, *patch_tree;
+	bool debug_enabled = false;
+
+	void *ctx = talloc_named_const(NULL, 0, "root");
+
+	if (argc < 3)
+		exit_usage(1);
+
+	base_fname = argv[1];
+	patch_fname = argv[2];
+
+	if (argc > 3) {
+		if (!strcmp(argv[3], "--debug"))
+			debug_enabled = true;
+		else
+			exit_usage(1);
+	}
+
+	base_tree = file_read(ctx, base_fname);
+	patch_tree = file_read(ctx, patch_fname);
+
+	if (debug_enabled) {
+		fprintf(stderr, "====== dumping tree (base)\n");
+		dump_node(base_tree, stderr, true);
+		fprintf(stderr, "====== dumping tree (patch)\n");
+		dump_node(patch_tree, stderr, true);
+	}
+
+	append_patch(base_tree, patch_tree);
+
+	if (debug_enabled)
+		fprintf(stderr, "====== dumping tree (patched)\n");
+	dump_node(base_tree, stdout, false);
+	fflush(stdout);
+}

-- 
To view, visit https://gerrit.osmocom.org/11084
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I61997a3668cc3a40d12ca023272f6d782e6fbefe
Gerrit-Change-Number: 11084
Gerrit-PatchSet: 1
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180925/d0a7b27e/attachment.htm>


More information about the gerrit-log mailing list