summaryrefslogtreecommitdiff
path: root/topology/topology.c
blob: 0c36e7cbee701a697860da0c72f7923236d4fdce (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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
  Copyright(c) 2019 Red Hat Inc.
  Copyright(c) 2014-2015 Intel Corporation
  Copyright(c) 2010-2011 Texas Instruments Incorporated,
  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  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 St - Fifth Floor, Boston, MA 02110-1301 USA.
  The full GNU General Public License is included in this distribution
  in the file called LICENSE.GPL.
*/

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <getopt.h>
#include <assert.h>

#include <alsa/asoundlib.h>
#include <alsa/topology.h>
#include "gettext.h"
#include "version.h"
#include "topology.h"

bool pre_process_config = false;

static snd_output_t *log;

static void usage(const char *name)
{
	printf(
_("Usage: %s [OPTIONS]...\n"
"\n"
"-h, --help              help\n"
"-c, --compile=FILE      compile configuration file\n"
"-p, --pre-process       pre-process Topology2.0 configuration file before compilation\n"
"-P, --pre-process=FILE  pre-process Topology2.0 configuration file\n"
"-d, --decode=FILE       decode binary topology file\n"
"-n, --normalize=FILE    normalize configuration file\n"
"-u, --dump=FILE         dump (reparse) configuration file\n"
"-v, --verbose=LEVEL     set verbosity level (0...1)\n"
"-o, --output=FILE       set output file\n"
"-s, --sort              sort the identifiers in the normalized output\n"
"-g, --group             save configuration by group indexes\n"
"-x, --nocheck           save configuration without additional integrity checks\n"
"-z, --dapm-nosort       do not sort the DAPM widgets\n"
"-V, --version           print version\n"
), name);
}

static void version(const char *name)
{
	printf(
_("%s version %s\n"
"libasound version %s\n"
"libatopology version %s\n"
), name, SND_UTIL_VERSION_STR,
   snd_asoundlib_version(), snd_tplg_version());
}

static int load(const char *source_file, void **dst, size_t *dst_size)
{
	int fd;
	void *buf, *buf2;
	size_t size, pos;
	ssize_t r;

	if (strcmp(source_file, "-") == 0) {
		fd = fileno(stdin);
	} else {
		fd = open(source_file, O_RDONLY);
		if (fd < 0) {
			fprintf(stderr, _("Unable to open input file '%s': %s\n"),
				source_file, strerror(-errno));
			return 1;
		}
	}

	size = 16*1024;
	pos = 0;
	buf = malloc(size);
	if (buf == NULL)
		goto _nomem;
	while (1) {
		r = read(fd, buf + pos, size - pos);
		if (r < 0 && (errno == EAGAIN || errno == EINTR))
			continue;
		if (r <= 0)
			break;
		pos += r;
		size += 8*1024;
		buf2 = realloc(buf, size);
		if (buf2 == NULL)
			goto _nomem;
		buf = buf2;
	}
	if (r < 0) {
		fprintf(stderr, _("Read error: %s\n"), strerror(-errno));
		goto _err;
	}

	if (fd != fileno(stdin))
		close(fd);

	*dst = buf;
	*dst_size = pos;
	return 0;

_nomem:
	fprintf(stderr, _("No enough memory\n"));
_err:
	if (fd != fileno(stdin))
		close(fd);
	free(buf);
	return 1;
}

static int load_topology(snd_tplg_t **tplg, char *config,
			 size_t config_size, int cflags)
{
	int err;

	*tplg = snd_tplg_create(cflags);
	if (*tplg == NULL) {
		fprintf(stderr, _("failed to create new topology context\n"));
		return 1;
	}

	err = snd_tplg_load(*tplg, config, config_size);
	if (err < 0) {
		fprintf(stderr, _("Unable to load configuration: %s\n"),
			snd_strerror(-err));
		snd_tplg_free(*tplg);
		return 1;
	}

	return 0;
}

static int save(const char *output_file, void *buf, size_t size)
{
	char *fname = NULL;
	int fd;
	ssize_t r;

	if (strcmp(output_file, "-") == 0) {
		fd = fileno(stdout);
	} else {
		fname = alloca(strlen(output_file) + 5);
		strcpy(fname, output_file);
		strcat(fname, ".new");
		fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
		if (fd < 0) {
			fprintf(stderr, _("Unable to open output file '%s': %s\n"),
				fname, strerror(-errno));
			return 1;
		}
	}

	r = 0;
	while (size > 0) {
		r = write(fd, buf, size);
		if (r < 0 && (errno == EAGAIN || errno == EINTR))
			continue;
		if (r < 0)
			break;
		size -= r;
		buf += r;
	}

	if (r < 0) {
		fprintf(stderr, _("Write error: %s\n"), strerror(-errno));
		if (fd != fileno(stdout)) {
			if (fname && remove(fname))
				fprintf(stderr, _("Unable to remove file %s: %s\n"),
						fname, strerror(-errno));
			close(fd);
		}
		return 1;
	}

	if (fd != fileno(stdout))
		close(fd);

	if (fname && rename(fname, output_file)) {
		fprintf(stderr, _("Unable to rename file '%s' to '%s': %s\n"),
			fname, output_file, strerror(-errno));
		return 1;
	}

	return 0;
}

static int dump(const char *source_file, const char *output_file, int cflags, int sflags)
{
	snd_tplg_t *tplg;
	char *config, *text;
	size_t size;
	int err;

	err = load(source_file, (void **)&config, &size);
	if (err)
		return err;
	err = load_topology(&tplg, config, size, cflags);
	free(config);
	if (err)
		return err;
	err = snd_tplg_save(tplg, &text, sflags);
	snd_tplg_free(tplg);
	if (err < 0) {
		fprintf(stderr, _("Unable to save parsed configuration: %s\n"),
			snd_strerror(-err));
		return 1;
	}
	err = save(output_file, text, strlen(text));
	free(text);
	return err;
}

static char *get_inc_path(const char *filename)
{
	const char *s = strrchr(filename, '/');
	char *r = strdup(filename);
	if (r && s)
		r[s - filename] = '\0';
	return r;
}

/* Convert Topology2.0 conf to the existing conf syntax */
static int pre_process_conf(const char *source_file, const char *output_file,
			    const char *pre_processor_defs)
{
	struct tplg_pre_processor *tplg_pp;
	size_t config_size;
	char *config, *inc_path;
	int err;

	err = load(source_file, (void **)&config, &config_size);
	if (err)
		return err;

	/* init pre-processor */
	err = init_pre_processor(&tplg_pp, SND_OUTPUT_STDIO, output_file);
	if (err < 0) {
		fprintf(stderr, _("failed to init pre-processor for Topology2.0\n"));
		free(config);
		return err;
	}

	/* pre-process conf file */
	inc_path = get_inc_path(source_file);
	err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
	free(inc_path);

	/* free pre-processor */
	free_pre_preprocessor(tplg_pp);
	free(config);
	return err;
}

static int compile(const char *source_file, const char *output_file, int cflags,
		   const char *pre_processor_defs)
{
	struct tplg_pre_processor *tplg_pp = NULL;
	snd_tplg_t *tplg;
	char *config, *inc_path;
	void *bin;
	size_t config_size, size;
	int err;

	err = load(source_file, (void **)&config, &config_size);
	if (err)
		return err;

	/* pre-process before compiling */
	if (pre_process_config) {
		char *pconfig;
		size_t size;

		/* init pre-processor */
		init_pre_processor(&tplg_pp, SND_OUTPUT_BUFFER, NULL);

		/* pre-process conf file */
		inc_path = get_inc_path(source_file);
		err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
		free(inc_path);
		if (err) {
			free_pre_preprocessor(tplg_pp);
			free(config);
			return err;
		}

		/* load topology */
		size = snd_output_buffer_string(tplg_pp->output, &pconfig);
		err = load_topology(&tplg, pconfig, size, cflags);

		/* free pre-processor */
		free_pre_preprocessor(tplg_pp);
	} else {
		err = load_topology(&tplg, config, config_size, cflags);
	}
	free(config);
	if (err)
		return err;
	err = snd_tplg_build_bin(tplg, &bin, &size);
	snd_tplg_free(tplg);
	if (err < 0 || size == 0) {
		fprintf(stderr, _("failed to compile context %s: %s\n"),
			source_file, snd_strerror(-err));
		return 1;
	}
	err = save(output_file, bin, size);
	free(bin);
	return err;
}

static int decode(const char *source_file, const char *output_file,
		  int cflags, int dflags, int sflags)
{
	snd_tplg_t *tplg;
	void *bin;
	char *text;
	size_t size;
	int err;

	if (load(source_file, &bin, &size))
		return 1;
	tplg = snd_tplg_create(cflags);
	if (tplg == NULL) {
		fprintf(stderr, _("failed to create new topology context\n"));
		return 1;
	}
	err = snd_tplg_decode(tplg, bin, size, dflags);
	free(bin);
	if (err < 0) {
		snd_tplg_free(tplg);
		fprintf(stderr, _("failed to decode context %s: %s\n"),
			source_file, snd_strerror(-err));
		return 1;
	}
	err = snd_tplg_save(tplg, &text, sflags);
	snd_tplg_free(tplg);
	if (err < 0) {
		fprintf(stderr, _("Unable to save parsed configuration: %s\n"),
			snd_strerror(-err));
		return 1;
	}
	err = save(output_file, text, strlen(text));
	free(text);
	return err;
}

int main(int argc, char *argv[])
{
	static const char short_options[] = "hc:d:n:u:v:o:pP:sgxzVD:";
	static const struct option long_options[] = {
		{"help", 0, NULL, 'h'},
		{"verbose", 1, NULL, 'v'},
		{"compile", 1, NULL, 'c'},
		{"pre-process", 1, NULL, 'p'},
		{"decode", 1, NULL, 'd'},
		{"normalize", 1, NULL, 'n'},
		{"dump", 1, NULL, 'u'},
		{"output", 1, NULL, 'o'},
		{"sort", 0, NULL, 's'},
		{"group", 0, NULL, 'g'},
		{"nocheck", 0, NULL, 'x'},
		{"dapm-nosort", 0, NULL, 'z'},
		{"version", 0, NULL, 'V'},
		{0, 0, 0, 0},
	};
	char *source_file = NULL;
	char *output_file = NULL;
	const char *pre_processor_defs = NULL;
	int c, err, op = 'c', cflags = 0, dflags = 0, sflags = 0, option_index;

#ifdef ENABLE_NLS
	setlocale(LC_ALL, "");
	textdomain(PACKAGE);
#endif

	err = snd_output_stdio_attach(&log, stderr, 0);
	assert(err >= 0);

	while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage(argv[0]);
			return 0;
		case 'v':
			cflags |= SND_TPLG_CREATE_VERBOSE;
			break;
		case 'z':
			cflags |= SND_TPLG_CREATE_DAPM_NOSORT;
			break;
		case 'c':
		case 'd':
		case 'n':
		case 'u':
			if (source_file) {
				fprintf(stderr, _("Cannot combine operations (compile, normalize, pre-process, dump)\n"));
				return 1;
			}
			source_file = optarg;
			op = c;
			break;
		case 'o':
			output_file = optarg;
			break;
		case 's':
			sflags |= SND_TPLG_SAVE_SORT;
			break;
		case 'P':
			op = 'P';
			source_file = optarg;
			break;
		case 'p':
			pre_process_config = true;
			break;
		case 'g':
			sflags |= SND_TPLG_SAVE_GROUPS;
			break;
		case 'x':
			sflags |= SND_TPLG_SAVE_NOCHECK;
			break;
		case 'D':
			pre_processor_defs = optarg;
			break;
		case 'V':
			version(argv[0]);
			return 0;
		default:
			fprintf(stderr, _("Try `%s --help' for more information.\n"), argv[0]);
			return 1;
		}
	}

	if (source_file == NULL || output_file == NULL) {
		usage(argv[0]);
		return 1;
	}

	if (op == 'n') {
		if (sflags != 0 && sflags != SND_TPLG_SAVE_SORT) {
			fprintf(stderr, _("Wrong parameters for the normalize operation!\n"));
			return 1;
		}
		/* normalize has predefined output */
		sflags = SND_TPLG_SAVE_SORT;
	}

	switch (op) {
	case 'c':
		err = compile(source_file, output_file, cflags, pre_processor_defs);
		break;
	case 'd':
		err = decode(source_file, output_file, cflags, dflags, sflags);
		break;
	case 'P':
		err = pre_process_conf(source_file, output_file, pre_processor_defs);
		break;
	default:
		err = dump(source_file, output_file, cflags, sflags);
		break;
	}

	snd_output_close(log);
	return err ? 1 : 0;
}