2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/hashmap.h>
31 #include <pulsecore/idxset.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/macro.h>
39 pa_hashmap *unescaped;
46 static int add_key_value(pa_modargs *ma, char *key, char *value, const char* const valid_keys[], bool ignore_dupes) {
52 pa_assert(ma->unescaped);
56 if (pa_hashmap_get(ma->unescaped, key)) {
68 for (v = valid_keys; *v; v++)
69 if (pa_streq(*v, key))
79 raw = pa_xstrdup(value);
81 e = pa_xnew(struct entry, 1);
83 e->value = pa_unescape(value);
84 pa_hashmap_put(ma->unescaped, key, e);
86 if (pa_streq(raw, value))
89 e = pa_xnew(struct entry, 1);
90 e->key = pa_xstrdup(key);
92 pa_hashmap_put(ma->raw, key, e);
98 static void free_func(void *p) {
107 static int parse(pa_modargs *ma, const char *args, const char* const* valid_keys, bool ignore_dupes) {
113 VALUE_SIMPLE_ESCAPED,
115 VALUE_DOUBLE_QUOTES_ESCAPED,
120 const char *p, *key = NULL, *value = NULL;
121 size_t key_len = 0, value_len = 0;
125 for (p = args; *p; p++) {
131 else if (!isspace((unsigned char)*p)) {
141 else if (isspace((unsigned char)*p))
152 } else if (*p == '"') {
153 state = VALUE_DOUBLE_QUOTES;
156 } else if (isspace((unsigned char)*p)) {
157 if (add_key_value(ma,
158 pa_xstrndup(key, key_len),
164 } else if (*p == '\\') {
165 state = VALUE_SIMPLE_ESCAPED;
169 state = VALUE_SIMPLE;
176 if (isspace((unsigned char)*p)) {
177 if (add_key_value(ma,
178 pa_xstrndup(key, key_len),
179 pa_xstrndup(value, value_len),
184 } else if (*p == '\\') {
185 state = VALUE_SIMPLE_ESCAPED;
191 case VALUE_SIMPLE_ESCAPED:
192 state = VALUE_SIMPLE;
196 case VALUE_DOUBLE_QUOTES:
198 if (add_key_value(ma,
199 pa_xstrndup(key, key_len),
200 pa_xstrndup(value, value_len),
205 } else if (*p == '\\') {
206 state = VALUE_DOUBLE_QUOTES_ESCAPED;
212 case VALUE_DOUBLE_QUOTES_ESCAPED:
213 state = VALUE_DOUBLE_QUOTES;
219 if (add_key_value(ma,
220 pa_xstrndup(key, key_len),
221 pa_xstrndup(value, value_len),
226 } else if (*p == '\\') {
227 state = VALUE_TICKS_ESCAPED;
233 case VALUE_TICKS_ESCAPED:
240 if (state == VALUE_START) {
241 if (add_key_value(ma, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys, ignore_dupes) < 0)
243 } else if (state == VALUE_SIMPLE) {
244 if (add_key_value(ma, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys, ignore_dupes) < 0)
246 } else if (state != WHITESPACE)
255 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
256 pa_modargs *ma = pa_xnew(pa_modargs, 1);
258 ma->raw = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, free_func);
259 ma->unescaped = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, free_func);
261 if (args && parse(ma, args, valid_keys, false) < 0)
271 int pa_modargs_append(pa_modargs *ma, const char *args, const char* const* valid_keys) {
272 return parse(ma, args, valid_keys, true);
275 int pa_modargs_remove_key(pa_modargs *ma, const char *key) {
276 if (pa_hashmap_remove_and_free(ma->unescaped, key) == 0) {
277 pa_hashmap_remove_and_free(ma->raw, key);
284 void pa_modargs_free(pa_modargs*ma) {
287 pa_hashmap_free(ma->raw);
288 pa_hashmap_free(ma->unescaped);
292 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
298 if (!(e = pa_hashmap_get(ma->unescaped, key)))
304 static const char *modargs_get_value_raw(pa_modargs *ma, const char *key, const char *def) {
310 if (!(e = pa_hashmap_get(ma->raw, key)))
311 if (!(e = pa_hashmap_get(ma->unescaped, key)))
317 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
322 if (!(v = pa_modargs_get_value(ma, key, NULL)))
325 if (pa_atou(v, value) < 0)
331 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
336 if (!(v = pa_modargs_get_value(ma, key, NULL)))
339 if (pa_atoi(v, value) < 0)
345 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, bool *value) {
351 if (!(v = pa_modargs_get_value(ma, key, NULL)))
357 if ((r = pa_parse_boolean(v)) < 0)
364 int pa_modargs_get_value_double(pa_modargs *ma, const char *key, double *value) {
369 if (!(v = pa_modargs_get_value(ma, key, NULL)))
372 if (pa_atod(v, value) < 0)
378 int pa_modargs_get_value_volume(pa_modargs *ma, const char *key, pa_volume_t *value) {
383 if (!(v = pa_modargs_get_value(ma, key, NULL)))
386 if (pa_parse_volume(v, value) < 0)
392 int pa_modargs_get_sample_rate(pa_modargs *ma, uint32_t *rate) {
398 if ((pa_modargs_get_value_u32(ma, "rate", &rate_local)) < 0 ||
399 !pa_sample_rate_valid(rate_local))
407 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
415 if ((pa_modargs_get_sample_rate(ma, &ss.rate)) < 0)
418 channels = ss.channels;
419 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0 ||
420 !pa_channels_valid(channels))
422 ss.channels = (uint8_t) channels;
424 if ((format = pa_modargs_get_value(ma, "format", NULL)))
425 if ((ss.format = pa_parse_sample_format(format)) < 0)
428 if (!pa_sample_spec_valid(&ss))
436 int pa_modargs_get_alternate_sample_rate(pa_modargs *ma, uint32_t *alternate_rate) {
439 pa_assert(alternate_rate);
441 rate_local = *alternate_rate;
442 if ((pa_modargs_get_value_u32(ma, "alternate_rate", &rate_local)) < 0 ||
443 !pa_sample_rate_valid(*alternate_rate))
446 *alternate_rate = rate_local;
451 int pa_modargs_get_channel_map(pa_modargs *ma, const char *name, pa_channel_map *rmap) {
459 if ((cm = pa_modargs_get_value(ma, name ? name : "channel_map", NULL)))
460 if (!pa_channel_map_parse(&map, cm))
463 if (!pa_channel_map_valid(&map))
470 int pa_modargs_get_resample_method(pa_modargs *ma, pa_resample_method_t *rmethod) {
476 if ((m = pa_modargs_get_value(ma, "resample_method", NULL))) {
477 pa_resample_method_t method = pa_parse_resample_method(m);
479 if (method == PA_RESAMPLER_INVALID)
488 int pa_modargs_get_sample_spec_and_channel_map(
491 pa_channel_map *rmap,
492 pa_channel_map_def_t def) {
502 if (pa_modargs_get_sample_spec(ma, &ss) < 0)
507 if (ss.channels != map.channels)
508 pa_channel_map_init_extend(&map, ss.channels, def);
510 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0)
513 if (map.channels != ss.channels) {
514 if (!pa_modargs_get_value(ma, "channels", NULL))
515 ss.channels = map.channels;
526 int pa_modargs_get_proplist(pa_modargs *ma, const char *name, pa_proplist *p, pa_update_mode_t m) {
534 if (!(v = modargs_get_value_raw(ma, name, NULL)))
537 if (!(n = pa_proplist_from_string(v)))
540 pa_proplist_update(p, m, n);
546 const char *pa_modargs_iterate(pa_modargs *ma, void **state) {
551 if (!(e = pa_hashmap_iterate(ma->unescaped, state, NULL)))
557 int pa_modargs_merge_missing(pa_modargs *dst, pa_modargs *src, const char* const valid_keys[]) {
559 const char *key, *value;
562 for (state = NULL, key = pa_modargs_iterate(src, &state); key; key = pa_modargs_iterate(src, &state)) {
563 value = pa_modargs_get_value(src, key, NULL);
564 if (value && add_key_value(dst, pa_xstrdup(key), pa_xstrdup(value), valid_keys, true) < 0) {
565 pa_log_warn("Failed to add module argument '%s=%s'", key, value);
567 /* continue to gather all errors */