]> code.delx.au - pulseaudio/blob - src/pulsecore/modargs.c
consider passing the same argument twice to a module an error, also consider a variab...
[pulseaudio] / src / pulsecore / modargs.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/hashmap.h>
35 #include <pulsecore/idxset.h>
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/macro.h>
42
43 #include "modargs.h"
44
45 struct entry {
46 char *key, *value;
47 };
48
49 static int add_key_value(pa_hashmap *map, char *key, char *value, const char* const valid_keys[]) {
50 struct entry *e;
51
52 pa_assert(map);
53 pa_assert(key);
54 pa_assert(value);
55
56 if (pa_hashmap_get(map, key)) {
57 pa_xfree(key);
58 pa_xfree(value);
59 return -1;
60 }
61
62 if (valid_keys) {
63 const char*const* v;
64 for (v = valid_keys; *v; v++)
65 if (strcmp(*v, key) == 0)
66 break;
67
68 if (!*v) {
69 pa_xfree(key);
70 pa_xfree(value);
71 return -1;
72 }
73 }
74
75 e = pa_xnew(struct entry, 1);
76 e->key = key;
77 e->value = value;
78 pa_hashmap_put(map, key, e);
79
80 return 0;
81 }
82
83 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
84 pa_hashmap *map = NULL;
85
86 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
87
88 if (args) {
89 enum {
90 WHITESPACE,
91 KEY,
92 VALUE_START,
93 VALUE_SIMPLE,
94 VALUE_DOUBLE_QUOTES,
95 VALUE_TICKS
96 } state;
97
98 const char *p, *key, *value;
99 size_t key_len = 0, value_len = 0;
100
101 key = value = NULL;
102 state = WHITESPACE;
103 for (p = args; *p; p++) {
104 switch (state) {
105 case WHITESPACE:
106 if (*p == '=')
107 goto fail;
108 else if (!isspace(*p)) {
109 key = p;
110 state = KEY;
111 key_len = 1;
112 }
113 break;
114 case KEY:
115 if (*p == '=')
116 state = VALUE_START;
117 else if (isspace(*p))
118 goto fail;
119 else
120 key_len++;
121 break;
122 case VALUE_START:
123 if (*p == '\'') {
124 state = VALUE_TICKS;
125 value = p+1;
126 value_len = 0;
127 } else if (*p == '"') {
128 state = VALUE_DOUBLE_QUOTES;
129 value = p+1;
130 value_len = 0;
131 } else if (isspace(*p)) {
132 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
133 goto fail;
134 state = WHITESPACE;
135 } else {
136 state = VALUE_SIMPLE;
137 value = p;
138 value_len = 1;
139 }
140 break;
141 case VALUE_SIMPLE:
142 if (isspace(*p)) {
143 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
144 goto fail;
145 state = WHITESPACE;
146 } else
147 value_len++;
148 break;
149 case VALUE_DOUBLE_QUOTES:
150 if (*p == '"') {
151 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
152 goto fail;
153 state = WHITESPACE;
154 } else
155 value_len++;
156 break;
157 case VALUE_TICKS:
158 if (*p == '\'') {
159 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
160 goto fail;
161 state = WHITESPACE;
162 } else
163 value_len++;
164 break;
165 }
166 }
167
168 if (state == VALUE_START) {
169 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
170 goto fail;
171 } else if (state == VALUE_SIMPLE) {
172 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
173 goto fail;
174 } else if (state != WHITESPACE)
175 goto fail;
176 }
177
178 return (pa_modargs*) map;
179
180 fail:
181
182 if (map)
183 pa_modargs_free((pa_modargs*) map);
184
185 return NULL;
186 }
187
188 static void free_func(void *p, PA_GCC_UNUSED void*userdata) {
189 struct entry *e = p;
190 pa_assert(e);
191
192 pa_xfree(e->key);
193 pa_xfree(e->value);
194 pa_xfree(e);
195 }
196
197 void pa_modargs_free(pa_modargs*ma) {
198 pa_hashmap *map = (pa_hashmap*) ma;
199 pa_hashmap_free(map, free_func, NULL);
200 }
201
202 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
203 pa_hashmap *map = (pa_hashmap*) ma;
204 struct entry*e;
205
206 if (!(e = pa_hashmap_get(map, key)))
207 return def;
208
209 return e->value;
210 }
211
212 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
213 const char *v;
214
215 pa_assert(ma);
216 pa_assert(key);
217 pa_assert(value);
218
219 if (!(v = pa_modargs_get_value(ma, key, NULL)))
220 return 0;
221
222 if (pa_atou(v, value) < 0)
223 return -1;
224
225 return 0;
226 }
227
228 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
229 const char *v;
230
231 pa_assert(ma);
232 pa_assert(key);
233 pa_assert(value);
234
235 if (!(v = pa_modargs_get_value(ma, key, NULL)))
236 return 0;
237
238 if (pa_atoi(v, value) < 0)
239 return -1;
240
241 return 0;
242 }
243
244 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, pa_bool_t *value) {
245 const char *v;
246 int r;
247
248 pa_assert(ma);
249 pa_assert(key);
250 pa_assert(value);
251
252 if (!(v = pa_modargs_get_value(ma, key, NULL)))
253 return 0;
254
255 if (!*v)
256 return -1;
257
258 if ((r = pa_parse_boolean(v)) < 0)
259 return -1;
260
261 *value = r;
262 return 0;
263 }
264
265 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
266 const char *format;
267 uint32_t channels;
268 pa_sample_spec ss;
269
270 pa_assert(ma);
271 pa_assert(rss);
272
273 ss = *rss;
274 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0)
275 return -1;
276
277 channels = ss.channels;
278 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0)
279 return -1;
280 ss.channels = (uint8_t) channels;
281
282 if ((format = pa_modargs_get_value(ma, "format", NULL)))
283 if ((ss.format = pa_parse_sample_format(format)) < 0)
284 return -1;
285
286 if (!pa_sample_spec_valid(&ss))
287 return -1;
288
289 *rss = ss;
290
291 return 0;
292 }
293
294 int pa_modargs_get_channel_map(pa_modargs *ma, const char *name, pa_channel_map *rmap) {
295 pa_channel_map map;
296 const char *cm;
297
298 pa_assert(ma);
299 pa_assert(rmap);
300
301 map = *rmap;
302
303 if ((cm = pa_modargs_get_value(ma, name ? name : "channel_map", NULL)))
304 if (!pa_channel_map_parse(&map, cm))
305 return -1;
306
307 if (!pa_channel_map_valid(&map))
308 return -1;
309
310 *rmap = map;
311 return 0;
312 }
313
314 int pa_modargs_get_sample_spec_and_channel_map(pa_modargs *ma, pa_sample_spec *rss, pa_channel_map *rmap, pa_channel_map_def_t def) {
315 pa_sample_spec ss;
316 pa_channel_map map;
317
318 pa_assert(ma);
319 pa_assert(rss);
320 pa_assert(rmap);
321
322 ss = *rss;
323
324 if (pa_modargs_get_sample_spec(ma, &ss) < 0)
325 return -1;
326
327 if (!pa_channel_map_init_auto(&map, ss.channels, def))
328 map.channels = 0;
329
330 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0)
331 return -1;
332
333 if (map.channels != ss.channels)
334 return -1;
335
336 *rmap = map;
337 *rss = ss;
338
339 return 0;
340 }