1 | //config.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | static struct roar_libroar_config_codec * |
---|
39 | roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config); |
---|
40 | |
---|
41 | |
---|
42 | struct roar_libroar_config * roar_libroar_get_config_ptr(void) { |
---|
43 | static struct roar_libroar_config config; |
---|
44 | static int inited = 0; |
---|
45 | static char authfile[1024]; |
---|
46 | const char * home = roar_env_get_home(0); |
---|
47 | |
---|
48 | if ( !inited ) { |
---|
49 | memset(&config, 0, sizeof(config)); |
---|
50 | |
---|
51 | #ifdef ROAR_SUPPORT_TRAP |
---|
52 | config.trap_policy = ROAR_TRAP_IGNORE; |
---|
53 | #endif |
---|
54 | config.opmode = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL; |
---|
55 | config.server = NULL; |
---|
56 | config.authfile = NULL; |
---|
57 | config.forkapi = NULL; |
---|
58 | config.connect_internal = NULL; |
---|
59 | config.daemonimage = NULL; |
---|
60 | config.protocolversion = -1; // use default. |
---|
61 | |
---|
62 | if ( home != NULL ) { |
---|
63 | snprintf(authfile, sizeof(authfile)-1, "%s/.roarauth", home); |
---|
64 | authfile[sizeof(authfile)-1] = 0; |
---|
65 | config.authfile = authfile; |
---|
66 | config.serversfile = NULL; |
---|
67 | } |
---|
68 | |
---|
69 | inited++; |
---|
70 | } |
---|
71 | |
---|
72 | return &config; |
---|
73 | } |
---|
74 | |
---|
75 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
76 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
77 | static int inited = 0; |
---|
78 | char * next; |
---|
79 | |
---|
80 | if ( !inited ) { |
---|
81 | inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse(). |
---|
82 | |
---|
83 | next = getenv("ROAR_OPTIONS"); |
---|
84 | |
---|
85 | if ( next != NULL ) { |
---|
86 | roar_libroar_config_parse(next, " "); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return config; |
---|
91 | } |
---|
92 | |
---|
93 | int roar_libroar_reset_config(void) { |
---|
94 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
95 | |
---|
96 | if ( config->codecs.codec != NULL ) { |
---|
97 | roar_mm_free(config->codecs.codec); |
---|
98 | config->codecs.num = 0; |
---|
99 | } |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | #define _P_FP(v) ((int)(atof((v))*256.0)) |
---|
105 | #define _P_INT(v) (atoi((v))) |
---|
106 | #define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0) |
---|
107 | |
---|
108 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
109 | struct roar_libroar_config_codec * codec_cfg; |
---|
110 | int32_t codec; |
---|
111 | char * codec_str, * option_str, * value_str; |
---|
112 | char * toksave = NULL; |
---|
113 | |
---|
114 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
115 | |
---|
116 | if ( config == NULL || txt == NULL ) |
---|
117 | return -1; |
---|
118 | |
---|
119 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
120 | |
---|
121 | codec_str = roar_mm_strtok_r(txt, ":", &toksave); |
---|
122 | |
---|
123 | if ( codec_str == NULL ) |
---|
124 | return -1; |
---|
125 | |
---|
126 | option_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
127 | |
---|
128 | if ( option_str == NULL ) |
---|
129 | return -1; |
---|
130 | |
---|
131 | value_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
132 | |
---|
133 | if ( value_str == NULL ) |
---|
134 | return -1; |
---|
135 | |
---|
136 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
137 | |
---|
138 | if ( (codec = roar_str2codec(codec_str)) == -1 ) { |
---|
139 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str); |
---|
140 | return -1; |
---|
141 | } |
---|
142 | |
---|
143 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec); |
---|
144 | |
---|
145 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
146 | return -1; |
---|
147 | |
---|
148 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg); |
---|
149 | |
---|
150 | if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) { |
---|
151 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q; |
---|
152 | codec_cfg->q = _P_FP(value_str); |
---|
153 | } else if ( !strcmp(option_str, "complexity") ) { |
---|
154 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY; |
---|
155 | codec_cfg->complexity = _P_FP(value_str); |
---|
156 | } else if ( !strcmp(option_str, "dtx") ) { |
---|
157 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX; |
---|
158 | codec_cfg->dtx = _P_BOOL(value_str); |
---|
159 | } else if ( !strcmp(option_str, "cc-max") ) { |
---|
160 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC; |
---|
161 | codec_cfg->max_cc = _P_INT(value_str); |
---|
162 | } else if ( !strcmp(option_str, "vbr") ) { |
---|
163 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR; |
---|
164 | codec_cfg->vbr = _P_BOOL(value_str); |
---|
165 | } else if ( !strcmp(option_str, "mode") ) { |
---|
166 | if ( !strcmp(value_str, "nb") ) { |
---|
167 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
168 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_NB; |
---|
169 | } else if ( !strcmp(value_str, "wb") ) { |
---|
170 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
171 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_WB; |
---|
172 | } else if ( !strcmp(value_str, "uwb") ) { |
---|
173 | codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE; |
---|
174 | codec_cfg->mode = ROAR_LIBROAR_CONFIG_MODE_UWB; |
---|
175 | } else { |
---|
176 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str); |
---|
177 | return -1; |
---|
178 | } |
---|
179 | } else { |
---|
180 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
188 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
189 | ssize_t len; |
---|
190 | char * k, * v, * next = txt; |
---|
191 | |
---|
192 | while (next != NULL) { |
---|
193 | k = next; |
---|
194 | |
---|
195 | if ( delm == NULL ) { |
---|
196 | // no delm -> we have only one option |
---|
197 | next = NULL; |
---|
198 | } else { |
---|
199 | next = strstr(next, delm); |
---|
200 | } |
---|
201 | |
---|
202 | if ( next != NULL ) { |
---|
203 | *next = 0; |
---|
204 | next++; |
---|
205 | } |
---|
206 | |
---|
207 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
208 | |
---|
209 | // strip leading spaces: |
---|
210 | while ( *k == ' ' ) k++; |
---|
211 | |
---|
212 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
213 | |
---|
214 | // strip tailing new lions: |
---|
215 | len = roar_mm_strlen(k); |
---|
216 | if ( len != -1 ) |
---|
217 | for (len--; len && (k[len] == '\r' || k[len] == '\n'); len--) |
---|
218 | k[len] = 0; |
---|
219 | |
---|
220 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
221 | |
---|
222 | // comments |
---|
223 | if ( *k == '#' ) |
---|
224 | continue; |
---|
225 | |
---|
226 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
227 | |
---|
228 | // empty options: |
---|
229 | if ( *k == 0 ) |
---|
230 | continue; |
---|
231 | |
---|
232 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
233 | *v = 0; |
---|
234 | v++; |
---|
235 | } |
---|
236 | |
---|
237 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
238 | |
---|
239 | if ( !strcmp(k, "workaround") ) { |
---|
240 | if ( !strcmp(v, "use-execed") ) { |
---|
241 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
242 | } else if ( !strcmp(v, "no-slp") ) { |
---|
243 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
244 | } else { |
---|
245 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
246 | } |
---|
247 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
248 | if ( !strcmp(v, "sysio") ) { |
---|
249 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
250 | } else if ( !strcmp(v, "obsolete") ) { |
---|
251 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
252 | } else if ( !strcmp(v, "all") ) { |
---|
253 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
254 | config->warnings.obsolete = ROAR_WARNING_ALWAYS; |
---|
255 | } else { |
---|
256 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
257 | } |
---|
258 | } else if ( !strcmp(k, "opmode") ) { |
---|
259 | if ( !strcmp(v, "normal") ) { |
---|
260 | config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL; |
---|
261 | } else if ( !strcmp(v, "funny") ) { |
---|
262 | config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_FUNNY; |
---|
263 | } else if ( !strcmp(v, "ms") ) { |
---|
264 | config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_MS; |
---|
265 | } else { |
---|
266 | ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v); |
---|
267 | } |
---|
268 | #ifdef ROAR_SUPPORT_TRAP |
---|
269 | } else if ( !strcmp(k, "trap-policy") ) { |
---|
270 | if ( !strcmp(v, "ignore") ) { |
---|
271 | config->trap_policy = ROAR_TRAP_IGNORE; |
---|
272 | } else if ( !strcmp(v, "warn") ) { |
---|
273 | config->trap_policy = ROAR_TRAP_WARN; |
---|
274 | } else if ( !strcmp(v, "abort") ) { |
---|
275 | config->trap_policy = ROAR_TRAP_ABORT; |
---|
276 | #ifdef SIGKILL |
---|
277 | } else if ( !strcmp(v, "kill") ) { |
---|
278 | config->trap_policy = ROAR_TRAP_KILL; |
---|
279 | #endif |
---|
280 | #ifdef SIGSTOP |
---|
281 | } else if ( !strcmp(v, "stop") ) { |
---|
282 | config->trap_policy = ROAR_TRAP_STOP; |
---|
283 | #endif |
---|
284 | } else if ( !strcmp(v, "die") ) { |
---|
285 | config->trap_policy = ROAR_TRAP_DIE; |
---|
286 | } else { |
---|
287 | ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v); |
---|
288 | } |
---|
289 | #endif |
---|
290 | } else if ( !strcmp(k, "force-rate") ) { |
---|
291 | config->info.rate = roar_str2rate(v); |
---|
292 | } else if ( !strcmp(k, "force-bits") ) { |
---|
293 | config->info.bits = roar_str2bits(v); |
---|
294 | } else if ( !strcmp(k, "force-channels") ) { |
---|
295 | config->info.channels = roar_str2channels(v); |
---|
296 | } else if ( !strcmp(k, "force-codec") ) { |
---|
297 | config->info.codec = roar_str2codec(v); |
---|
298 | } else if ( !strcmp(k, "codec") ) { |
---|
299 | if ( roar_libroar_config_parse_codec(config, v) == -1 ) { |
---|
300 | ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option"); |
---|
301 | } |
---|
302 | } else if ( !strcmp(k, "set-server") ) { |
---|
303 | if ( roar_libroar_get_server() == NULL ) |
---|
304 | roar_libroar_set_server(v); |
---|
305 | } else if ( !strcmp(k, "set-authfile") ) { |
---|
306 | strncpy(config->authfile, v, 1023); |
---|
307 | config->authfile[1023] = 0; |
---|
308 | } else if ( !strcmp(k, "x11-display") ) { |
---|
309 | config->x11.display = v; |
---|
310 | } else if ( !strcmp(k, "daemonimage") ) { |
---|
311 | config->daemonimage = v; |
---|
312 | } else if ( !strcmp(k, "serverflags") ) { |
---|
313 | if ( !strcmp(v, "nonblock") ) { |
---|
314 | config->serverflags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
315 | } else if ( !strcmp(v, "hardnonblock") ) { |
---|
316 | config->serverflags |= ROAR_ENUM_FLAG_HARDNONBLOCK; |
---|
317 | } else if ( !strcmp(v, "localonly") ) { |
---|
318 | config->serverflags |= ROAR_ENUM_FLAG_LOCALONLY; |
---|
319 | } else { |
---|
320 | ROAR_WARN("roar_libroar_config_parse(*): Unknown serverflag: %s", v); |
---|
321 | } |
---|
322 | } else if ( !strcmp(k, "protocolversion") ) { |
---|
323 | config->protocolversion = atoi(v); |
---|
324 | } else { |
---|
325 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|
332 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int32_t codec, int create) { |
---|
333 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
334 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
335 | } |
---|
336 | |
---|
337 | static struct roar_libroar_config_codec * |
---|
338 | roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config) { |
---|
339 | size_t i; |
---|
340 | int need_new = 1; |
---|
341 | |
---|
342 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
343 | |
---|
344 | if ( codec < 0 || create < 0 ) |
---|
345 | return NULL; |
---|
346 | |
---|
347 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
348 | |
---|
349 | if ( config->codecs.num == 0 ) { |
---|
350 | // no match case: |
---|
351 | if ( !create ) |
---|
352 | return NULL; |
---|
353 | } else { |
---|
354 | for (i = 0; i < config->codecs.num; i++) { |
---|
355 | if ( config->codecs.codec[i].codec == (uint32_t)codec ) |
---|
356 | return &(config->codecs.codec[i]); |
---|
357 | if ( config->codecs.codec[i].codec == (uint32_t)-1 ) |
---|
358 | need_new = 0; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
363 | |
---|
364 | if ( !create ) |
---|
365 | return NULL; |
---|
366 | |
---|
367 | if ( !need_new ) { |
---|
368 | for (i = 0; i < config->codecs.num; i++) { |
---|
369 | if ( config->codecs.codec[i].codec == (uint32_t)-1 ) { |
---|
370 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
371 | config->codecs.codec[i].codec = codec; |
---|
372 | return &(config->codecs.codec[i]); |
---|
373 | } |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | if ( config->codecs.num == 0 ) { |
---|
378 | config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
379 | } else { |
---|
380 | config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
381 | } |
---|
382 | |
---|
383 | if ( config->codecs.codec == NULL ) |
---|
384 | return NULL; |
---|
385 | |
---|
386 | memset(&(config->codecs.codec[config->codecs.num]), 0, 16); |
---|
387 | for (i = config->codecs.num; i < (config->codecs.num+16); i++) { |
---|
388 | config->codecs.codec[i].codec = (uint32_t)-1; |
---|
389 | } |
---|
390 | |
---|
391 | i = config->codecs.num; |
---|
392 | config->codecs.num += 16; |
---|
393 | |
---|
394 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
395 | config->codecs.codec[i].codec = codec; |
---|
396 | |
---|
397 | return &(config->codecs.codec[i]); |
---|
398 | } |
---|
399 | |
---|
400 | int roar_libroar_set_server(const char * server) { |
---|
401 | roar_libroar_get_config_ptr()->server = server; |
---|
402 | return 0; |
---|
403 | } |
---|
404 | |
---|
405 | const char * roar_libroar_get_server(void) { |
---|
406 | return roar_libroar_get_config_ptr()->server; |
---|
407 | } |
---|
408 | |
---|
409 | int roar_libroar_set_forkapi(struct roar_libroar_forkapi * api) { |
---|
410 | roar_libroar_get_config_ptr()->forkapi = api; |
---|
411 | return 0; |
---|
412 | } |
---|
413 | |
---|
414 | int roar_libroar_set_connect_internal(struct roar_vio_calls * (*func)(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout)) { |
---|
415 | roar_libroar_get_config_ptr()->connect_internal = func; |
---|
416 | return 0; |
---|
417 | } |
---|
418 | |
---|
419 | void roar_libroar_nowarn(void) { |
---|
420 | roar_libroar_get_config_ptr()->nowarncounter++; |
---|
421 | } |
---|
422 | |
---|
423 | void roar_libroar_warn(void) { |
---|
424 | struct roar_libroar_config * cfg = roar_libroar_get_config_ptr(); |
---|
425 | |
---|
426 | if ( cfg->nowarncounter == 0 ) { |
---|
427 | ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)"); |
---|
428 | return; |
---|
429 | } |
---|
430 | |
---|
431 | cfg->nowarncounter--; |
---|
432 | } |
---|
433 | |
---|
434 | static const struct { |
---|
435 | const char * name; |
---|
436 | const char * path; |
---|
437 | const int with_product; |
---|
438 | const int with_provider; |
---|
439 | } __paths[] = { |
---|
440 | {"prefix", ROAR_PREFIX, 0, 0}, |
---|
441 | {"prefix-bin", ROAR_PREFIX_BIN, 0, 0}, |
---|
442 | {"prefix-lib", ROAR_PREFIX_LIB, 0, 0}, |
---|
443 | {"prefix-inc", ROAR_PREFIX_INC, 0, 0}, |
---|
444 | {"prefix-man", ROAR_PREFIX_MAN, 0, 0}, |
---|
445 | {"prefix-pc", ROAR_PREFIX_PC, 0, 0}, |
---|
446 | {"prefix-comp-libs", ROAR_PREFIX_COMP_LIBS, 0, 0}, |
---|
447 | {"prefix-comp-bins", ROAR_PREFIX_COMP_BINS, 0, 0}, |
---|
448 | {"prefix-plugins", ROAR_PREFIX_PLUGINS, 1, 1}, |
---|
449 | {"prefix-buildsystem", ROAR_PREFIX_BUILDSYSTEM, 0, 0} |
---|
450 | }; |
---|
451 | |
---|
452 | static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product) { |
---|
453 | const char * c; |
---|
454 | const char * e; |
---|
455 | const char * s; |
---|
456 | const char * b; |
---|
457 | |
---|
458 | if ( product == NULL ) { |
---|
459 | if ( null_as_universal ) { |
---|
460 | snprintf(path, pathlen, "/universal/universal"); |
---|
461 | } else { |
---|
462 | path[0] = 0; |
---|
463 | } |
---|
464 | return 0; |
---|
465 | } |
---|
466 | |
---|
467 | b = strstr(product, " "); |
---|
468 | c = strstr(product, "<"); |
---|
469 | e = strstr(product, ">"); |
---|
470 | |
---|
471 | if ( b == NULL || c == NULL || e == NULL || c < b || e < c ) { |
---|
472 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
473 | return -1; |
---|
474 | } |
---|
475 | |
---|
476 | c++; |
---|
477 | |
---|
478 | s = strstr(c, "/"); |
---|
479 | |
---|
480 | if ( s == NULL ) { |
---|
481 | snprintf(path, pathlen, "/unreg-%.*s/%.*s", (int)(size_t)(e-c), c, (int)(size_t)(b-product), product); |
---|
482 | } else { |
---|
483 | snprintf(path, pathlen, "/%.*s-%.*s/%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1, (int)(size_t)(b-product), product); |
---|
484 | } |
---|
485 | |
---|
486 | return 0; |
---|
487 | } |
---|
488 | |
---|
489 | static int __provider2path(char * path, size_t pathlen, const char * provider) { |
---|
490 | const char * c; |
---|
491 | const char * e; |
---|
492 | const char * s; |
---|
493 | |
---|
494 | if ( provider == NULL ) { |
---|
495 | path[0] = 0; |
---|
496 | return 0; |
---|
497 | } |
---|
498 | |
---|
499 | c = strstr(provider, "<"); |
---|
500 | e = strstr(provider, ">"); |
---|
501 | |
---|
502 | if ( c == NULL || e == NULL || e < c ) { |
---|
503 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
504 | return -1; |
---|
505 | } |
---|
506 | |
---|
507 | c++; |
---|
508 | |
---|
509 | s = strstr(c, "/"); |
---|
510 | |
---|
511 | if ( s == NULL ) { |
---|
512 | snprintf(path, pathlen, "/unreg-%.*s", (int)(size_t)(e-c), c); |
---|
513 | } else { |
---|
514 | snprintf(path, pathlen, "/%.*s-%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1); |
---|
515 | } |
---|
516 | |
---|
517 | return 0; |
---|
518 | |
---|
519 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
520 | return -1; |
---|
521 | } |
---|
522 | |
---|
523 | char * roar_libroar_get_path(const char * name, int null_as_universal, const char * product, const char * provider) { |
---|
524 | char buf_product[1024]; |
---|
525 | char buf_provider[1024]; |
---|
526 | ssize_t len_prefix, len_product, len_provider; |
---|
527 | size_t i; |
---|
528 | char * ret, * p; |
---|
529 | |
---|
530 | if ( name == NULL ) { |
---|
531 | roar_err_set(ROAR_ERROR_NOENT); |
---|
532 | return NULL; |
---|
533 | } |
---|
534 | |
---|
535 | for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++) { |
---|
536 | if ( !!strcmp(__paths[i].name, name) ) |
---|
537 | continue; |
---|
538 | |
---|
539 | if ( ((null_as_universal || product != NULL) && !__paths[i].with_product) || |
---|
540 | (provider != NULL && !__paths[i].with_provider) ) { |
---|
541 | roar_err_set(ROAR_ERROR_INVAL); |
---|
542 | return NULL; |
---|
543 | } |
---|
544 | |
---|
545 | if ( __product2path(buf_product, sizeof(buf_product), null_as_universal, product) == -1 ) |
---|
546 | return NULL; |
---|
547 | |
---|
548 | if ( __provider2path(buf_provider, sizeof(buf_provider), provider) == -1 ) |
---|
549 | return NULL; |
---|
550 | |
---|
551 | len_prefix = roar_mm_strlen(__paths[i].path); |
---|
552 | len_product = roar_mm_strlen(buf_product); |
---|
553 | len_provider = roar_mm_strlen(buf_provider); |
---|
554 | |
---|
555 | p = ret = roar_mm_malloc(len_prefix+len_product+len_provider+1); |
---|
556 | if ( ret == NULL ) |
---|
557 | return NULL; |
---|
558 | |
---|
559 | memcpy(p, __paths[i].path, len_prefix); |
---|
560 | p += len_prefix; |
---|
561 | if ( p[-1] == '/' ) |
---|
562 | p--; |
---|
563 | memcpy(p, buf_product, len_product); |
---|
564 | p += len_product; |
---|
565 | memcpy(p, buf_provider, len_provider); |
---|
566 | p += len_provider; |
---|
567 | |
---|
568 | *p = 0; |
---|
569 | |
---|
570 | return ret; |
---|
571 | } |
---|
572 | |
---|
573 | roar_err_set(ROAR_ERROR_NOENT); |
---|
574 | return NULL; |
---|
575 | } |
---|
576 | |
---|
577 | //ll |
---|