summaryrefslogtreecommitdiff
path: root/src/ucm/ucm_include.c
blob: c69490f49c6e6f1109927ecf1a2c558bc63ba87e (plain)
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
/*
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  Support for the verb/device/modifier core logic and API,
 *  command line tool and file parser was kindly sponsored by
 *  Texas Instruments Inc.
 *  Support for multiple active modifiers and devices,
 *  transition sequences, multiple client access and user defined use
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
 *
 *  Copyright (C) 2020 Red Hat Inc.
 *  Authors: Jaroslav Kysela <perex@perex.cz>
 */

#include "ucm_local.h"

static int get_string(snd_config_t *compound, const char *key, const char **str)
{
	snd_config_t *node;
	int err;

	err = snd_config_search(compound, key, &node);
	if (err < 0)
		return err;
	return snd_config_get_string(node, str);
}

static int include_eval_one(snd_use_case_mgr_t *uc_mgr,
			    snd_config_t *inc,
			    snd_config_t **result,
			    snd_config_t **before,
			    snd_config_t **after)
{
	const char *file;
	char *s;
	int err;

	*result = NULL;

	if (snd_config_get_type(inc) != SND_CONFIG_TYPE_COMPOUND) {
		uc_error("compound type expected for Include.1");
		return -EINVAL;
	}

	err = get_string(inc, "File", &file);
	if (err < 0) {
		uc_error("file expected (Include)");
		return -EINVAL;
	}

	err = snd_config_search(inc, "Before", before);
	if (err < 0 && err != -ENOENT) {
		uc_error("before block identifier error");
		return -EINVAL;
	}

	err = snd_config_search(inc, "After", after);
	if (err < 0 && err != -ENOENT) {
		uc_error("before block identifier error");
		return -EINVAL;
	}

	err = uc_mgr_get_substituted_value(uc_mgr, &s, file);
	if (err < 0)
		return err;
	err = uc_mgr_config_load_file(uc_mgr, s, result);
	free(s);
	return err;
}

#if 0
static void config_dump(snd_config_t *cfg)
{
	snd_output_t *out;
	snd_output_stdio_attach(&out, stderr, 0);
	snd_output_printf(out, "-----\n");
	snd_config_save(cfg, out);
	snd_output_close(out);
}
#endif

static int find_position_node(snd_config_t **res, snd_config_t *dst,
			      const char *id, snd_config_t *pos)
{
	const char *s;
	int err;

	err = get_string(pos, id, &s);
	if (err < 0 && err != -ENOENT)
		return err;
	if (err == 0) {
		err = snd_config_search(dst, s, res);
		if (err < 0 && err != -ENOENT)
			return err;
	}
	return 0;
}

static int compound_merge(const char *id,
			  snd_config_t *dst, snd_config_t *src,
			  snd_config_t *before, snd_config_t *after)
{
	snd_config_iterator_t i, next;
	snd_config_t *n, *_before = NULL, *_after = NULL;
	char tmpid[32];
	int err, array, idx;

	if (snd_config_get_type(src) != SND_CONFIG_TYPE_COMPOUND) {
		uc_error("compound type expected for the merged block");
		return -EINVAL;
	}

	if (before) {
		err = find_position_node(&_before, dst, id, before);
		if (err < 0)
			return err;
	}
	if (after) {
		err = find_position_node(&_after, dst, id, after);
		if (err < 0)
			return err;
	}

	if (_before && _after) {
		uc_error("defined both before and after identifiers in the If or Include block");
		return -EINVAL;
	}

	array = snd_config_is_array(dst);
	if (array < 0) {
		uc_error("destination configuration node is not a compound");
		return array;
	}
	if (array && snd_config_is_array(src) <= 0) {
		uc_error("source configuration node is not an array");
		return -EINVAL;
	}

	idx = 0;

	/* for array, use a temporary non-clashing identifier */
	if (array > 0) {
		snd_config_for_each(i, next, dst) {
			n = snd_config_iterator_entry(i);
			snprintf(tmpid, sizeof(tmpid), "_tmp_%d", idx++);
			err = snd_config_set_id(n, tmpid);
			if (err < 0)
				return err;
		}
	}

	snd_config_for_each(i, next, src) {
		n = snd_config_iterator_entry(i);
		err = snd_config_remove(n);
		if (err < 0)
			return err;
		/* for array, use a temporary non-clashing identifier */
		if (array > 0) {
			snprintf(tmpid, sizeof(tmpid), "_tmp_%d", idx++);
			err = snd_config_set_id(n, tmpid);
			if (err < 0)
				return err;
		}
		if (_before) {
			err = snd_config_add_before(_before, n);
			if (err < 0)
				return err;
			_before = NULL;
			_after = n;
		} else if (_after) {
			err = snd_config_add_after(_after, n);
			if (err < 0)
				return err;
			_after = n;
		} else {
			err = snd_config_add(dst, n);
			if (err < 0)
				return err;
		}
	}

	/* set new indexes for the final array */
	if (array > 0) {
		idx = 0;
		snd_config_for_each(i, next, dst) {
			n = snd_config_iterator_entry(i);
			snprintf(tmpid, sizeof(tmpid), "%d", idx++);
			err = snd_config_set_id(n, tmpid);
			if (err < 0)
				return err;
		}
	}

	return 0;
}

int uc_mgr_config_tree_merge(snd_config_t *parent, snd_config_t *new_ctx,
			     snd_config_t *before, snd_config_t *after)
{
	snd_config_iterator_t i, next;
	snd_config_t *n, *parent2;
	const char *id;
	int err;

	snd_config_for_each(i, next, new_ctx) {
		n = snd_config_iterator_entry(i);
		err = snd_config_remove(n);
		if (err < 0)
			return err;
		err = snd_config_get_id(n, &id);
		if (err < 0) {
__add:
			err = snd_config_add(parent, n);
			if (err < 0)
				return err;
			continue;
		} else {
			err = snd_config_search(parent, id, &parent2);
			if (err == -ENOENT)
				goto __add;
			err = compound_merge(id, parent2, n, before, after);
			if (err < 0)
				return err;
		}
		snd_config_delete(n);
	}
	return 0;
}

/*
 * put back the included configuration to the parent
 */
int uc_mgr_evaluate_include(snd_use_case_mgr_t *uc_mgr,
			      snd_config_t *parent,
			      snd_config_t *inc)
{
	snd_config_iterator_t i, next;
	snd_config_t *a, *n, *before, *after;
	int err;

	if (uc_mgr->conf_format < 3) {
		uc_error("in-place include is supported in v3+ syntax");
		return -EINVAL;
	}

	if (snd_config_get_type(inc) != SND_CONFIG_TYPE_COMPOUND) {
		uc_error("compound type expected for Include");
		return -EINVAL;
	}

	snd_config_for_each(i, next, inc) {
		n = snd_config_iterator_entry(i);
		before = after = NULL;
		err = include_eval_one(uc_mgr, n, &a, &before, &after);
		if (err < 0)
			return err;
		if (a == NULL)
			continue;
		err = uc_mgr_evaluate_inplace(uc_mgr, a);
		if (err < 0)
			return err;
		err = uc_mgr_config_tree_merge(parent, a, before, after);
		if (err < 0)
			return err;
		snd_config_delete(a);
		
	}
	return 0;
}