source: roaraudio/libroar/config.c @ 5791:cfd78ecb471f

Last change on this file since 5791:cfd78ecb471f was 5791:cfd78ecb471f, checked in by phi, 11 years ago

added configured path prefix-ckport

File size: 16.8 KB
Line 
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
38static struct roar_libroar_config_codec *
39           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config);
40
41
42struct 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
75struct 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 const char * next;
79 char * buf;
80
81 if ( !inited ) {
82  inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse().
83
84  next = roar_env_get("ROAR_OPTIONS");
85
86  if ( next != NULL ) {
87   if ( (buf = roar_mm_strdup(next)) != NULL ) {
88    roar_libroar_config_parse(buf, " ");
89    roar_mm_free(buf);
90   }
91  }
92 }
93
94 return config;
95}
96
97int    roar_libroar_reset_config(void) {
98 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
99
100 if ( config->codecs.codec != NULL ) {
101  roar_mm_free(config->codecs.codec);
102  config->codecs.num = 0;
103 }
104
105 return 0;
106}
107
108#define _P_FP(v)   ((int)(atof((v))*256.0))
109#define _P_INT(v)  (atoi((v)))
110#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
111
112static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
113 struct roar_libroar_config_codec * codec_cfg;
114 int32_t codec;
115 char * codec_str, * option_str, * value_str;
116 char * toksave = NULL;
117
118 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
119
120 if ( config == NULL || txt == NULL )
121  return -1;
122
123 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
124
125 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
126
127 if ( codec_str == NULL )
128  return -1;
129
130 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
131
132 if ( option_str == NULL )
133  return -1;
134
135 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
136
137 if ( value_str == NULL )
138  return -1;
139
140 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
141
142 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
143  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
144  return -1;
145 }
146
147 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
148
149 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
150  return -1;
151
152 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
153
154 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
155  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
156  codec_cfg->q = _P_FP(value_str);
157 } else if ( !strcmp(option_str, "complexity") ) {
158  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
159  codec_cfg->complexity = _P_FP(value_str);
160 } else if ( !strcmp(option_str, "dtx") ) {
161  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
162  codec_cfg->dtx = _P_BOOL(value_str);
163 } else if ( !strcmp(option_str, "cc-max") ) {
164  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
165  codec_cfg->max_cc = _P_INT(value_str);
166 } else if ( !strcmp(option_str, "vbr") ) {
167  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
168  codec_cfg->vbr = _P_BOOL(value_str);
169 } else if ( !strcmp(option_str, "mode") ) {
170  if ( !strcmp(value_str, "nb") ) {
171   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
172   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_NB;
173  } else if ( !strcmp(value_str, "wb") ) {
174   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
175   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_WB;
176  } else if ( !strcmp(value_str, "uwb") ) {
177   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
178   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_UWB;
179  } else {
180   ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str);
181   return -1;
182  }
183 } else {
184  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
185  return -1;
186 }
187
188 return 0;
189}
190
191int    roar_libroar_config_parse(char * txt, char * delm) {
192 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
193 ssize_t len;
194 char * k, * v, * next = txt;
195
196 while (next != NULL) {
197  k = next;
198
199  if ( delm == NULL ) {
200   // no delm -> we have only one option
201   next = NULL;
202  } else {
203   next = strstr(next, delm);
204  }
205
206  if ( next != NULL ) {
207   *next = 0;
208    next++;
209  }
210
211  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
212
213  // strip leading spaces:
214  while ( *k == ' ' ) k++;
215
216  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
217
218  // strip tailing new lions:
219  len = roar_mm_strlen(k);
220  if ( len != -1 )
221   for (len--; len && (k[len] == '\r' || k[len] == '\n'); len--)
222    k[len] = 0;
223
224  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
225
226  // comments
227  if ( *k == '#' )
228   continue;
229
230  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
231
232  // empty options:
233  if ( *k == 0 )
234   continue;
235
236  if ( (v = strstr(k, ":")) != NULL ) {
237   *v = 0;
238    v++;
239  }
240
241  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
242
243  if ( !strcmp(k, "workaround") ) {
244   if ( !strcmp(v, "use-execed") ) {
245    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
246   } else if ( !strcmp(v, "no-slp") ) {
247    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
248   } else {
249    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
250   }
251  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
252   if ( !strcmp(v, "sysio") ) {
253    config->warnings.sysio = ROAR_WARNING_ALWAYS;
254   } else if ( !strcmp(v, "obsolete") ) {
255    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
256   } else if ( !strcmp(v, "all") ) {
257    config->warnings.sysio    = ROAR_WARNING_ALWAYS;
258    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
259   } else {
260    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
261   }
262  } else if ( !strcmp(k, "opmode") ) {
263   if ( !strcmp(v, "normal") ) {
264    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL;
265   } else if ( !strcmp(v, "funny") ) {
266    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_FUNNY;
267   } else if ( !strcmp(v, "ms") ) {
268    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_MS;
269   } else {
270    ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v);
271   }
272#ifdef ROAR_SUPPORT_TRAP
273  } else if ( !strcmp(k, "trap-policy") ) {
274   if ( !strcmp(v, "ignore") ) {
275    config->trap_policy = ROAR_TRAP_IGNORE;
276   } else if ( !strcmp(v, "warn") ) {
277    config->trap_policy = ROAR_TRAP_WARN;
278   } else if ( !strcmp(v, "abort") ) {
279    config->trap_policy = ROAR_TRAP_ABORT;
280#ifdef SIGKILL
281   } else if ( !strcmp(v, "kill") ) {
282    config->trap_policy = ROAR_TRAP_KILL;
283#endif
284#ifdef SIGSTOP
285   } else if ( !strcmp(v, "stop") ) {
286    config->trap_policy = ROAR_TRAP_STOP;
287#endif
288   } else if ( !strcmp(v, "die") ) {
289    config->trap_policy = ROAR_TRAP_DIE;
290   } else {
291    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
292   }
293#endif
294  } else if ( !strcmp(k, "force-rate") ) {
295   config->info.rate = roar_str2rate(v);
296  } else if ( !strcmp(k, "force-bits") ) {
297   config->info.bits = roar_str2bits(v);
298  } else if ( !strcmp(k, "force-channels") ) {
299   config->info.channels = roar_str2channels(v);
300  } else if ( !strcmp(k, "force-codec") ) {
301   config->info.codec = roar_str2codec(v);
302  } else if ( !strcmp(k, "codec") ) {
303   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
304    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
305   }
306  } else if ( !strcmp(k, "set-server") ) {
307   if ( roar_libroar_get_server() == NULL )
308    roar_libroar_set_server(v);
309  } else if ( !strcmp(k, "set-authfile") ) {
310   strncpy(config->authfile, v, 1023);
311   config->authfile[1023] = 0;
312  } else if ( !strcmp(k, "x11-display") ) {
313   config->x11.display = v;
314  } else if ( !strcmp(k, "daemonimage") ) {
315   config->daemonimage = v;
316  } else if ( !strcmp(k, "serverflags") ) {
317   if ( !strcmp(v, "nonblock") ) {
318    config->serverflags |= ROAR_ENUM_FLAG_NONBLOCK;
319   } else if ( !strcmp(v, "hardnonblock") ) {
320    config->serverflags |= ROAR_ENUM_FLAG_HARDNONBLOCK;
321   } else if ( !strcmp(v, "localonly") ) {
322    config->serverflags |= ROAR_ENUM_FLAG_LOCALONLY;
323   } else {
324    ROAR_WARN("roar_libroar_config_parse(*): Unknown serverflag: %s", v);
325   }
326  } else if ( !strcmp(k, "protocolversion") ) {
327   config->protocolversion = atoi(v);
328  } else {
329   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
330  }
331 }
332
333 return 0;
334}
335
336struct roar_libroar_config_codec * roar_libroar_config_codec_get(int32_t codec, int create) {
337 struct roar_libroar_config * config = roar_libroar_get_config();
338 return roar_libroar_config_codec_get_conf(codec, create, config);
339}
340
341static struct roar_libroar_config_codec *
342           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config) {
343 size_t i;
344 int need_new = 1;
345
346 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
347
348 if ( codec < 0 || create < 0 )
349  return NULL;
350
351 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
352
353 if ( config->codecs.num == 0 ) {
354  // no match case:
355  if ( !create )
356   return NULL;
357 } else {
358  for (i = 0; i < config->codecs.num; i++) {
359   if ( config->codecs.codec[i].codec == (uint32_t)codec )
360    return &(config->codecs.codec[i]);
361   if ( config->codecs.codec[i].codec == (uint32_t)-1 )
362    need_new = 0;
363  }
364 }
365
366 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
367
368 if ( !create )
369  return NULL;
370
371 if ( !need_new ) {
372  for (i = 0; i < config->codecs.num; i++) {
373   if ( config->codecs.codec[i].codec == (uint32_t)-1 ) {
374    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
375    config->codecs.codec[i].codec = codec;
376    return &(config->codecs.codec[i]);
377   }
378  }
379 }
380
381 if ( config->codecs.num == 0 ) {
382  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
383 } else {
384  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
385 }
386
387 if ( config->codecs.codec == NULL )
388  return NULL;
389
390 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
391 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
392  config->codecs.codec[i].codec = (uint32_t)-1;
393 }
394
395 i = config->codecs.num;
396 config->codecs.num += 16;
397
398 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
399 config->codecs.codec[i].codec = codec;
400
401 return &(config->codecs.codec[i]);
402}
403
404int    roar_libroar_set_server(const char * server) {
405 roar_libroar_get_config_ptr()->server = server;
406 return 0;
407}
408
409const char * roar_libroar_get_server(void) {
410 return roar_libroar_get_config_ptr()->server;
411}
412
413int    roar_libroar_set_forkapi(const struct roar_libroar_forkapi * api) {
414 roar_libroar_get_config_ptr()->forkapi = api;
415 return 0;
416}
417
418int    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)) {
419 roar_libroar_get_config_ptr()->connect_internal = func;
420 return 0;
421}
422
423void   roar_libroar_nowarn(void) {
424 roar_libroar_get_config_ptr()->nowarncounter++;
425}
426
427void   roar_libroar_warn(void) {
428 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
429
430 if ( cfg->nowarncounter == 0 ) {
431  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
432  return;
433 }
434
435 cfg->nowarncounter--;
436}
437
438static const struct {
439 const char * name;
440 const char * path;
441 const int    with_product;
442 const int    with_provider;
443} __paths[] = {
444 {"prefix",     ROAR_PREFIX, 0, 0},
445 {"prefix-bin", ROAR_PREFIX_BIN, 0, 0},
446 {"prefix-lib", ROAR_PREFIX_LIB, 0, 0},
447 {"prefix-inc", ROAR_PREFIX_INC, 0, 0},
448 {"prefix-man", ROAR_PREFIX_MAN, 0, 0},
449 {"prefix-pc",  ROAR_PREFIX_PC, 0, 0},
450 {"prefix-ckport",  ROAR_PREFIX_CKPORT, 0, 0},
451 {"prefix-comp-libs", ROAR_PREFIX_COMP_LIBS, 0, 0},
452 {"prefix-comp-bins", ROAR_PREFIX_COMP_BINS, 0, 0},
453 {"prefix-plugins", ROAR_PREFIX_PLUGINS, 1, 1},
454 {"prefix-buildsystem", ROAR_PREFIX_BUILDSYSTEM, 0, 0}
455};
456
457static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product) {
458 const char * c;
459 const char * e;
460 const char * s;
461 const char * b;
462
463 if ( product == NULL ) {
464  if ( null_as_universal ) {
465   snprintf(path, pathlen, "/universal/universal");
466  } else {
467   path[0] = 0;
468  }
469  return 0;
470 }
471
472 b = strstr(product, " ");
473 c = strstr(product, "<");
474 e = strstr(product, ">");
475
476 if ( b == NULL || c == NULL || e == NULL || c < b || e < c ) {
477  roar_err_set(ROAR_ERROR_ILLSEQ);
478  return -1;
479 }
480
481 c++;
482
483 s = strstr(c, "/");
484
485 if ( s == NULL ) {
486  snprintf(path, pathlen, "/unreg-%.*s/%.*s", (int)(size_t)(e-c), c, (int)(size_t)(b-product), product);
487 } else {
488  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);
489 }
490
491 return 0;
492}
493
494static int __provider2path(char * path, size_t pathlen, const char * provider) {
495 const char * c;
496 const char * e;
497 const char * s;
498
499 if ( provider == NULL ) {
500  path[0] = 0;
501  return 0;
502 }
503
504 c = strstr(provider, "<");
505 e = strstr(provider, ">");
506
507 if ( c == NULL || e == NULL || e < c ) {
508  roar_err_set(ROAR_ERROR_ILLSEQ);
509  return -1;
510 }
511
512 c++;
513
514 s = strstr(c, "/");
515
516 if ( s == NULL ) {
517  snprintf(path, pathlen, "/unreg-%.*s", (int)(size_t)(e-c), c);
518 } else {
519  snprintf(path, pathlen, "/%.*s-%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1);
520 }
521
522 return 0;
523
524 roar_err_set(ROAR_ERROR_NOTSUP);
525 return -1;
526}
527
528char * roar_libroar_get_path(const char * name, int null_as_universal, const char * product, const char * provider) {
529 char buf_product[1024];
530 char buf_provider[1024];
531 ssize_t len_prefix, len_product, len_provider;
532 size_t i;
533 char * ret, * p;
534
535 if ( name == NULL ) {
536  roar_err_set(ROAR_ERROR_NOENT);
537  return NULL;
538 }
539
540 for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++) {
541  if ( !!strcmp(__paths[i].name, name) )
542   continue;
543
544  if ( ((null_as_universal || product != NULL) && !__paths[i].with_product) ||
545       (provider != NULL && !__paths[i].with_provider) ) {
546   roar_err_set(ROAR_ERROR_INVAL);
547   return NULL;
548  }
549
550  if ( __product2path(buf_product, sizeof(buf_product), null_as_universal, product) == -1 )
551   return NULL;
552
553  if ( __provider2path(buf_provider, sizeof(buf_provider), provider) == -1 )
554   return NULL;
555
556  len_prefix = roar_mm_strlen(__paths[i].path);
557  len_product = roar_mm_strlen(buf_product);
558  len_provider = roar_mm_strlen(buf_provider);
559
560  p = ret = roar_mm_malloc(len_prefix+len_product+len_provider+1);
561  if ( ret == NULL )
562   return NULL;
563
564  memcpy(p, __paths[i].path, len_prefix);
565  p += len_prefix;
566  if ( p[-1] == '/' )
567   p--;
568  memcpy(p, buf_product, len_product);
569  p += len_product;
570  memcpy(p, buf_provider, len_provider);
571  p += len_provider;
572
573  *p = 0;
574
575  return ret;
576 }
577
578 roar_err_set(ROAR_ERROR_NOENT);
579 return NULL;
580}
581
582//ll
Note: See TracBrowser for help on using the repository browser.