1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*! \file osmo-config-merge.c
* Utility program for merging config files with patches */
/*
* (C) 2018 by Harald Welte <laforge@gnumonks.org>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
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.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgfile.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;
};
struct osmo_patch_entry {
struct llist_head list;
struct node *tree;
};
/* 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;
unsigned int line_num = 0;
infile = fopen(fname, "r");
if (!infile) {
fprintf(stderr, "Could not open file '%s': %s\n",
fname, strerror(errno));
return NULL;
}
root = node_alloc(ctx);
last = root;
while (fgets(line, sizeof(line), infile)) {
line_num++;
chomp(line);
int indent = count_indent(line);
struct node *n;
if (indent > cur_indent) {
if (indent > cur_indent+1) {
fprintf(stderr, "File '%s' isn't well-formed in line %u, aborting!\n",
fname, line_num);
fclose(infile);
return NULL;
}
/* new child to last node */
n = node_alloc_child(last);
} else if (indent < cur_indent) {
int i;
for (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;
}
fclose(infile);
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) {
int i;
for (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>...<config-patch> [--debug]\n");
exit(rc);
}
int main(int argc, char **argv)
{
struct node *base_tree;
struct osmo_config_list *trees;
struct osmo_patch_entry *entry;
bool debug_enabled = false;
unsigned i;
void *ctx;
if (argc < 3)
exit_usage(1);
ctx = talloc_named_const(NULL, 0, "root");
base_tree = file_read(ctx, argv[1]);
trees = talloc_zero(ctx, struct osmo_config_list);
if (!base_tree || !trees) {
talloc_free(ctx);
return 2;
}
INIT_LLIST_HEAD(&trees->entry);
for (i = 2; i < argc; i++) {
if (!strcmp(argv[i], "--debug"))
debug_enabled = true;
else {
entry = talloc_zero(trees, struct osmo_patch_entry);
if (!entry) {
talloc_free(ctx);
return 3;
}
entry->tree = file_read(ctx, argv[i]);
if (!entry->tree) {
talloc_free(ctx);
return 4;
}
llist_add_tail(&entry->list, &trees->entry);
}
}
if (llist_empty(&trees->entry))
exit_usage(1);
if (debug_enabled) {
fprintf(stderr, "====== dumping tree (base)\n");
dump_node(base_tree, stderr, true);
}
llist_for_each_entry(entry, &trees->entry, list) {
append_patch(base_tree, entry->tree);
if (debug_enabled) {
fprintf(stderr, "====== dumping tree (patch)\n");
dump_node(entry->tree, stderr, true);
}
}
if (debug_enabled)
fprintf(stderr, "====== dumping tree (patched)\n");
dump_node(base_tree, stdout, false);
fflush(stdout);
/* make AddressSanitizer / LeakSanitizer happy by recursively freeing the trees */
talloc_free(trees);
talloc_free(base_tree);
talloc_free(ctx);
return 0;
}
|