summaryrefslogtreecommitdiff
path: root/src/confmisc.c
blob: 7c1128d7204d32c1561184e8f7480ac03418add0 (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 *  Miscellaneous configuration helper functions
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>,
 *			  Jaroslav Kysela <perex@suse.cz>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library 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 Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "local.h"
#include "asoundlib.h"

/**
 * \brief Get the boolean value from given ASCII string
 * \param ascii The ASCII string to be parsed
 * \return a positive value when success otherwise a negative error number
 */
int snd_config_get_bool_ascii(const char *ascii)
{
	unsigned int k;
	static struct {
		const char *str;
		int val;
	} b[] = {
		{ "0", 0 },
		{ "1", 1 },
		{ "false", 0 },
		{ "true", 1 },
		{ "no", 0 },
		{ "yes", 1 },
		{ "off", 0 },
		{ "on", 1 },
	};
	for (k = 0; k < sizeof(b) / sizeof(*b); k++) {
		if (strcasecmp(b[k].str, ascii) == 0)
			return b[k].val;
	}
	return -EINVAL;
}

/**
 * \brief Get the boolean value
 * \param conf The configuration node to be parsed
 * \return a positive value when success otherwise a negative error number
 */
int snd_config_get_bool(snd_config_t *conf)
{
	long v;
	const char *str;
	int err;

	err = snd_config_get_integer(conf, &v);
	if (err >= 0) {
		if (v < 0 || v > 1) {
		_invalid_value:
			SNDERR("Invalid value for %s", snd_config_get_id(conf));
			return -EINVAL;
		}
		return v;
	}
	err = snd_config_get_string(conf, &str);
	if (err < 0) {
		SNDERR("Invalid type for %s", snd_config_get_id(conf));
		return -EINVAL;
	}
	err = snd_config_get_bool_ascii(str);
	if (err < 0)
		goto _invalid_value;
	return err;
}

/**
 * \brief Get the control interface index from given ASCII string
 * \param ascii The ASCII string to be parsed
 * \return a positive value when success otherwise a negative error number
 */ 
int snd_config_get_ctl_iface_ascii(const char *ascii)
{
	long v;
	snd_ctl_elem_iface_t idx;
	if (isdigit(ascii[0])) {
		if (safe_strtol(ascii, &v) >= 0) {
			if (v < 0 || v > SND_CTL_ELEM_IFACE_LAST)
				return -EINVAL;
			return v;
		}
	}
	for (idx = 0; idx <= SND_CTL_ELEM_IFACE_LAST; idx++) {
		if (strcasecmp(snd_ctl_elem_iface_name(idx), ascii) == 0)
			return idx;
	}
	return -EINVAL;
}

/**
 * \brief Get the control interface index
 * \param conf The configuration node to be parsed
 * \return a positive value when success otherwise a negative error number
 */ 
int snd_config_get_ctl_iface(snd_config_t *conf)
{
	long v;
	const char *str;
	int err;
	err = snd_config_get_integer(conf, &v);
	if (err >= 0) {
		if (v < 0 || v > SND_CTL_ELEM_IFACE_LAST) {
		_invalid_value:
			SNDERR("Invalid value for %s", snd_config_get_id(conf));
			return -EINVAL;
		}
		return v;
	}
	err = snd_config_get_string(conf, &str);
	if (err < 0) {
		SNDERR("Invalid type for %s", snd_config_get_id(conf));
		return -EINVAL;
	}
	err = snd_config_get_ctl_iface_ascii(str);
	if (err < 0)
		goto _invalid_value;
	return err;
}

/**
 * \brief Expand the dynamic contents
 * \param src Source string
 * \param idchr Identification character
 * \param callback Callback function
 * \param private_data Private data for the given callback function
 * \param dst Destination string
 */
int snd_config_string_replace(const char *src, char idchr,
			      snd_config_string_replace_callback_t *callback,
			      void *private_data,
			      char **dst)
{
	int len = 0, len1, err;
	const char *ptr, *end;
	char *tmp, *what, *fptr, *rdst = NULL;

	assert(src && idchr && dst);
	while (*src != '\0') {
		ptr = strchr(src, idchr);
		end = NULL;
		if (ptr == src && *(ptr + 1) == '(' && (end = strchr(ptr + 2, ')')) != NULL) {
			src = end + 1;
			if (callback == NULL)
				continue;
			len1 = end - (ptr + 2);
			if (len1 == 0)		/* empty */
				continue;
			what = malloc(len1 + 1);
			memcpy(what, ptr + 2, len1);
			what[len1] = '\0';
			fptr = NULL;
			err = callback(what, &fptr, private_data);
			free(what);
			if (err < 0) {
				if (*dst != NULL)
					free(*dst);
				return err;
			}
			if (fptr == NULL)	/* empty */
				continue;
			len1 = strlen(ptr = fptr);
		} else {
			if (ptr == NULL) {
				len1 = strlen(ptr = src);
			} else {
				len1 = ptr - src;
				ptr = src;
			}
			src += len1;
			fptr = NULL;
		}
		tmp = realloc(rdst, len + len1 + 1);
		if (tmp == NULL) {
			if (*dst != NULL)
				free(*dst);
			return -ENOMEM;
		}
		memcpy(tmp + len, ptr, len1);
		tmp[len+=len1] = '\0';
		if (fptr)
			free(fptr);
		rdst = tmp;
	}
	*dst = rdst;
	return 0;
}

/*
 *  Helper functions for the configuration file
 */

int snd_func_getenv(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n, *d;
	snd_config_iterator_t i, next;
	char *res, *def = NULL;
	int idx = 0, err;
	
	err = snd_config_search(src, "envname", &n);
	if (err < 0) {
		SNDERR("field envname not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating envname");
		goto __error;
	}
	err = snd_config_search(src, "default", &d);
	if (err < 0) {
		SNDERR("field default not found");
		goto __error;
	}
	err = snd_config_evaluate(d, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating default");
		goto __error;
	}
	err = snd_config_get_ascii(d, &def);
	if (err < 0) {
		SNDERR("error getting field default");
		goto __error;
	}
      __retry:
	snd_config_for_each(i, next, n) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id = snd_config_get_id(n);
		const char *ptr, *env;
		long i;
		if (snd_config_get_type(n) != SND_CONFIG_TYPE_STRING) {
			SNDERR("field %s is not a string", id);
			err = -EINVAL;
			goto __error;
		}
		err = safe_strtol(id, &i);
		if (err < 0) {
			SNDERR("id of field %s is not an integer", id);
			err = -EINVAL;
			goto __error;
		}
		if (i == idx) {
			idx++;
			snd_config_get_string(n, &ptr);
			env = getenv(ptr);
			if (env != NULL && *env != '\0') {
				res = strdup(env);
				goto __ok;
			}
			goto __retry;
		}
	}
	res = def;
	def = NULL;
      __ok:
	err = res == NULL ? -ENOMEM : 0;
	if (err >= 0) {
		err = snd_config_make_string(dst, snd_config_get_id(src));
		if (err >= 0)
			snd_config_set_string(*dst, res);
		free(res);
	}
      __error:
      	if (def)
      		free(def);
	return err;
}

int snd_func_igetenv(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *d;
	const char *str;
	int err;
	long v;
	err = snd_func_getenv(&d, src, private_data);
	if (err < 0)
		return err;
	err = snd_config_get_string(d, &str);
	if (err < 0)
		goto _end;
	err = safe_strtol(str, &v);
	if (err < 0)
		goto _end;
	err = snd_config_make_integer(dst, snd_config_get_id(src));
	if (err < 0)
		goto _end;
	snd_config_set_integer(*dst, v);
	err = 0;

 _end:
	return err;
}
	
	

int snd_func_concat(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n;
	snd_config_iterator_t i, next;
	char *res = NULL, *tmp;
	int idx = 0, len = 0, len1, err;
	
	err = snd_config_search(src, "strings", &n);
	if (err < 0) {
		SNDERR("field strings not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating strings");
		goto __error;
	}
      __retry:
	snd_config_for_each(i, next, n) {
		snd_config_t *n = snd_config_iterator_entry(i);
		char *ptr;
		const char *id = snd_config_get_id(n);
		long i;
		err = safe_strtol(id, &i);
		if (err < 0) {
			SNDERR("id of field %s is not an integer", id);
			err = -EINVAL;
			goto __error;
		}
		if (i == idx) {
			idx++;
			snd_config_get_ascii(n, &ptr);
			len1 = strlen(ptr);
			tmp = realloc(res, len + len1 + 1);
			if (tmp == NULL) {
				free(ptr);
				if (res)
					free(res);
				err = -ENOMEM;
				goto __error;
			}
			memcpy(tmp + len, ptr, len1);
			free(ptr);
			len += len1;
			tmp[len] = '\0';
			res = tmp;
			goto __retry;
		}
	}
	if (res == NULL) {
		SNDERR("empty string is not accepted");
		err = -EINVAL;
		goto __error;
	}
	err = snd_config_make_string(dst, snd_config_get_id(src));
	if (err >= 0)
		snd_config_set_string(*dst, res);
	free(res);
      __error:
	return err;
}

int snd_func_datadir(snd_config_t **dst, snd_config_t *src, void *private_data ATTRIBUTE_UNUSED)
{
	int err = snd_config_make_string(dst, snd_config_get_id(src));
	if (err >= 0)
		err = snd_config_set_string(*dst, DATADIR "/alsa");
	return 0;
}

static int open_ctl(long card, snd_ctl_t **ctl)
{
	char name[16];
	snprintf(name, sizeof(name), "hw:%li", card);
	name[sizeof(name)-1] = '\0';
	return snd_ctl_open(ctl, name, 0);
}

#if 0
static int string_from_integer(char **dst, long v)
{
	char str[32];
	char *res;
	sprintf(str, "%li", v);
	res = strdup(str);
	if (res == NULL)
		return -ENOMEM;
	*dst = res;
	return 0;
}
#endif

int snd_func_card_strtype(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n;
	char *res = NULL;
	snd_ctl_t *ctl = NULL;
	snd_ctl_card_info_t *info;
	long v;
	int err;
	
	err = snd_config_search(src, "card", &n);
	if (err < 0) {
		SNDERR("field card not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating card");
		goto __error;
	}
	err = snd_config_get_integer(n, &v);
	if (err < 0) {
		SNDERR("field card is not an integer");
		goto __error;
	}
	err = open_ctl(v, &ctl);
	if (err < 0) {
		SNDERR("could not open control for card %li", v);
		goto __error;
	}
	snd_ctl_card_info_alloca(&info);
	err = snd_ctl_card_info(ctl, info);
	if (err < 0) {
		SNDERR("snd_ctl_card_info error: %s", snd_strerror(err));
		goto __error;
	}
	err = snd_card_type_enum_to_string(snd_ctl_card_info_get_type(info), &res);
	if (err < 0) {
		SNDERR("snd_card_type_enum_to_string failed for %i", (int)snd_ctl_card_info_get_type(info));
		goto __error;
	}
	err = snd_config_make_string(dst, snd_config_get_id(src));
	if (err >= 0)
		err = snd_config_set_string(*dst, res);
	free(res);
      __error:
      	if (ctl)
      		snd_ctl_close(ctl);
	return err;
}

int snd_func_card_id(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n;
	char *res = NULL;
	snd_ctl_t *ctl = NULL;
	snd_ctl_card_info_t *info;
	long v;
	int err;
	
	err = snd_config_search(src, "card", &n);
	if (err < 0) {
		SNDERR("field card not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating card");
		goto __error;
	}
	err = snd_config_get_integer(n, &v);
	if (err < 0) {
		SNDERR("field card is not an integer");
		goto __error;
	}
	err = open_ctl(v, &ctl);
	if (err < 0) {
		SNDERR("could not open control for card %li", v);
		goto __error;
	}
	snd_ctl_card_info_alloca(&info);
	err = snd_ctl_card_info(ctl, info);
	if (err < 0) {
		SNDERR("snd_ctl_card_info error: %s", snd_strerror(err));
		goto __error;
	}
	res = strdup(snd_ctl_card_info_get_id(info));
	if (res == NULL) {
		err = -ENOMEM;
		goto __error;
	}
	err = snd_config_make_string(dst, snd_config_get_id(src));
	if (err >= 0)
		err = snd_config_set_string(*dst, res);
	free(res);
      __error:
      	if (ctl)
      		snd_ctl_close(ctl);
	return err;
}

int snd_func_pcm_id(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n;
	char *res = NULL;
	snd_ctl_t *ctl = NULL;
	snd_pcm_info_t *info;
	long card, device, subdevice = 0;
	int err;
	
	err = snd_config_search(src, "card", &n);
	if (err < 0) {
		SNDERR("field card not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating card");
		goto __error;
	}
	err = snd_config_get_integer(n, &card);
	if (err < 0) {
		SNDERR("field card is not an integer");
		goto __error;
	}
	err = snd_config_search(src, "device", &n);
	if (err < 0) {
		SNDERR("field device not found");
		goto __error;
	}
	err = snd_config_evaluate(n, private_data, NULL);
	if (err < 0) {
		SNDERR("error evaluating device");
		goto __error;
	}
	err = snd_config_get_integer(n, &device);
	if (err < 0) {
		SNDERR("field device is not an integer");
		goto __error;
	}
	if (snd_config_search(src, "subdevice", &n) >= 0) {
		err = snd_config_evaluate(n, private_data, NULL);
		if (err < 0) {
			SNDERR("error evaluating subdevice");
			goto __error;
		}
		err = snd_config_get_integer(n, &subdevice);
		if (err < 0) {
			SNDERR("field subdevice is not an integer");
			goto __error;
		}
	}
	err = open_ctl(card, &ctl);
	if (err < 0) {
		SNDERR("could not open control for card %li", card);
		goto __error;
	}
	snd_pcm_info_alloca(&info);
	snd_pcm_info_set_device(info, device);
	snd_pcm_info_set_subdevice(info, subdevice);
	err = snd_ctl_pcm_info(ctl, info);
	if (err < 0) {
		SNDERR("snd_ctl_pcm_info error: %s", snd_strerror(err));
		goto __error;
	}
	res = strdup(snd_pcm_info_get_id(info));
	if (res == NULL) {
		err = -ENOMEM;
		goto __error;
	}
	err = snd_config_make_string(dst, snd_config_get_id(src));
	if (err >= 0)
		err = snd_config_set_string(*dst, res);
	free(res);
      __error:
      	if (ctl)
      		snd_ctl_close(ctl);
	return err;
}

int snd_func_refer(snd_config_t **dst, snd_config_t *src, void *private_data)
{
	snd_config_t *n;
	snd_config_t *root = NULL;
	const char *file = NULL, *name = NULL;
	int err;
	
	err = snd_config_search(src, "file", &n);
	if (err >= 0) {
		err = snd_config_evaluate(n, private_data, NULL);
		if (err < 0) {
			SNDERR("error evaluating file");
			goto _end;
		}
		err = snd_config_get_string(n, &file);
		if (err < 0) {
			SNDERR("file is not a string");
			goto _end;
		}
	}
	err = snd_config_search(src, "name", &n);
	if (err >= 0) {
		err = snd_config_evaluate(n, private_data, NULL);
		if (err < 0) {
			SNDERR("error evaluating name");
			goto _end;
		}
		err = snd_config_get_string(n, &name);
		if (err < 0) {
			SNDERR("name is not a string");
			goto _end;
		}
	}
	if (!file && !name) {
		err = -EINVAL;
		SNDERR("neither file or name are specified");
		goto _end;
	}
	if (!file)
		root = snd_config;
	else {
		snd_input_t *input;
		err = snd_config_top(&root);
		if (err < 0)
			goto _end;
		err = snd_input_stdio_open(&input, file, "r");
		if (err < 0) {
			SNDERR("Unable to open file %s: %s", file, snd_strerror(err));
			goto _end;
		}
		err = snd_config_load(root, input);
		if (err < 0) {
			snd_input_close(input);
			goto _end;
		}
	}
	if (!name) {
		if (root == snd_config)
			snd_config_copy(dst, root);
		else
			*dst = root;
		err = 0;
	} else
		err = snd_config_search_definition(root, 0, name, dst);
	if (err >= 0)
		err = snd_config_set_id(*dst, snd_config_get_id(src));
 _end:
	if (root && root != snd_config)
		snd_config_delete(root);
	return err;
}