source: roaraudio/libroar/config.c @ 5201:4427d3c7382f

Last change on this file since 5201:4427d3c7382f was 5201:4427d3c7382f, checked in by phi, 12 years ago

fixed endless recursion in config loader

File size: 12.0 KB
RevLine 
[2477]1//config.c:
2
3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
[2477]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[2477]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
[2895]38static struct roar_libroar_config_codec *
39           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config);
40
41
[2478]42struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
43 static struct roar_libroar_config config;
[3218]44 static int    inited = 0;
45 static char   authfile[1024];
[4517]46 const  char * home = roar_env_get_home(0);
[2478]47
48 if ( !inited ) {
49  memset(&config, 0, sizeof(config));
[2567]50
[4779]51#ifdef ROAR_SUPPORT_TRAP
52  config.trap_policy = ROAR_TRAP_IGNORE;
53#endif
[5183]54  config.opmode      = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL;
[3218]55  config.server   = NULL;
56  config.authfile = NULL;
57
58  if ( home != NULL ) {
59   snprintf(authfile, 1023, "%s/.roarauth", home);
[3375]60   authfile[1023]     = 0;
61   config.authfile    = authfile;
[4348]62   config.serversfile = NULL;
[3218]63  }
[2567]64
[2478]65  inited++;
66 }
67
68 return &config;
69}
70
71struct roar_libroar_config * roar_libroar_get_config(void) {
72 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
73 static int inited = 0;
[2884]74 char * next;
[2478]75
76 if ( !inited ) {
[5201]77  inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse().
78
[2480]79  next = getenv("ROAR_OPTIONS");
80
[2884]81  if ( next != NULL ) {
82   roar_libroar_config_parse(next, " ");
[2480]83  }
[2478]84 }
85
86 return config;
87}
88
[5010]89int    roar_libroar_reset_config(void) {
90 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
91
92 if ( config->codecs.codec != NULL ) {
93  roar_mm_free(config->codecs.codec);
94  config->codecs.num = 0;
95 }
96
97 return 0;
98}
99
[2890]100#define _P_FP(v)   ((int)(atof((v))*256.0))
101#define _P_INT(v)  (atoi((v)))
102#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
103
104static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
105 struct roar_libroar_config_codec * codec_cfg;
106 int codec;
107 char * codec_str, * option_str, * value_str;
[5006]108 char * toksave = NULL;
[2890]109
[2912]110 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
111
[2890]112 if ( config == NULL || txt == NULL )
113  return -1;
114
[2912]115 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
116
[5006]117 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
[2890]118
[2892]119 if ( codec_str == NULL )
120  return -1;
121
[5006]122 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
[2890]123
124 if ( option_str == NULL )
125  return -1;
126
[5006]127 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
[2890]128
129 if ( value_str == NULL )
130  return -1;
131
[2912]132 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
133
[2890]134 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
135  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
136  return -1;
137 }
138
[2912]139 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
140
[2893]141 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
[2890]142  return -1;
143
[2912]144 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
145
[2890]146 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
147  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
148  codec_cfg->q = _P_FP(value_str);
149 } else if ( !strcmp(option_str, "complexity") ) {
150  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
151  codec_cfg->complexity = _P_FP(value_str);
152 } else if ( !strcmp(option_str, "dtx") ) {
153  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
154  codec_cfg->dtx = _P_BOOL(value_str);
155 } else if ( !strcmp(option_str, "cc-max") ) {
156  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
157  codec_cfg->max_cc = _P_INT(value_str);
[2926]158 } else if ( !strcmp(option_str, "vbr") ) {
159  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
160  codec_cfg->vbr = _P_BOOL(value_str);
[2962]161 } else if ( !strcmp(option_str, "mode") ) {
162  if ( !strcmp(value_str, "nb") ) {
163   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
164   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_NB;
165  } else if ( !strcmp(value_str, "wb") ) {
166   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
167   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_WB;
168  } else if ( !strcmp(value_str, "uwb") ) {
169   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
170   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_UWB;
171  } else {
172   ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str);
173   return -1;
174  }
[2890]175 } else {
[2891]176  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
[2892]177  return -1;
[2890]178 }
179
[2892]180 return 0;
[2890]181}
182
[2884]183int    roar_libroar_config_parse(char * txt, char * delm) {
184 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
[5006]185 ssize_t len;
[2884]186 char * k, * v, * next = txt;
187
188 while (next != NULL) {
189  k = next;
[2886]190
191  if ( delm == NULL ) {
192   // no delm -> we have only one option
193   next = NULL;
194  } else {
195   next = strstr(next, delm);
196  }
197
[2884]198  if ( next != NULL ) {
199   *next = 0;
200    next++;
201  }
202
[2888]203  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
204
[2887]205  // strip leading spaces:
206  while ( *k == ' ' ) k++;
207
[2888]208  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
209
[2887]210  // strip tailing new lions:
[5006]211  len = roar_mm_strlen(k);
212  if ( len != -1 )
213   for (len--; len && (k[len] == '\r' || k[len] == '\n'); len--)
214    k[len] = 0;
[2887]215
[2888]216  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
217
[2887]218  // comments
219  if ( *k == '#' )
220   continue;
221
[2888]222  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
223
[2887]224  // empty options:
225  if ( *k == 0 )
226   continue;
227
[2884]228  if ( (v = strstr(k, ":")) != NULL ) {
229   *v = 0;
230    v++;
231  }
232
[2888]233  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
234
[2884]235  if ( !strcmp(k, "workaround") ) {
236   if ( !strcmp(v, "use-execed") ) {
237    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
[4653]238   } else if ( !strcmp(v, "no-slp") ) {
239    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
[2884]240   } else {
[2888]241    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
[2884]242   }
243  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
244   if ( !strcmp(v, "sysio") ) {
245    config->warnings.sysio = ROAR_WARNING_ALWAYS;
[3855]246   } else if ( !strcmp(v, "obsolete") ) {
247    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
248   } else if ( !strcmp(v, "all") ) {
249    config->warnings.sysio    = ROAR_WARNING_ALWAYS;
250    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
[2884]251   } else {
[2888]252    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
[2884]253   }
[5183]254  } else if ( !strcmp(k, "opmode") ) {
255   if ( !strcmp(v, "normal") ) {
256    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL;
257   } else if ( !strcmp(v, "funny") ) {
258    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_FUNNY;
259   } else {
260    ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v);
261   }
[4779]262#ifdef ROAR_SUPPORT_TRAP
263  } else if ( !strcmp(k, "trap-policy") ) {
264   if ( !strcmp(v, "ignore") ) {
265    config->trap_policy = ROAR_TRAP_IGNORE;
266   } else if ( !strcmp(v, "warn") ) {
267    config->trap_policy = ROAR_TRAP_WARN;
268   } else if ( !strcmp(v, "abort") ) {
269    config->trap_policy = ROAR_TRAP_ABORT;
270#ifdef SIGKILL
271   } else if ( !strcmp(v, "kill") ) {
272    config->trap_policy = ROAR_TRAP_KILL;
273#endif
274#ifdef SIGSTOP
275   } else if ( !strcmp(v, "stop") ) {
276    config->trap_policy = ROAR_TRAP_STOP;
277#endif
[4784]278   } else if ( !strcmp(v, "die") ) {
279    config->trap_policy = ROAR_TRAP_DIE;
[4779]280   } else {
281    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
282   }
283#endif
[3136]284  } else if ( !strcmp(k, "force-rate") ) {
285   config->info.rate = atoi(v);
286  } else if ( !strcmp(k, "force-bits") ) {
287   config->info.bits = atoi(v);
288  } else if ( !strcmp(k, "force-channels") ) {
289   config->info.channels = atoi(v);
290  } else if ( !strcmp(k, "force-codec") ) {
291   config->info.codec = roar_str2codec(v);
[2890]292  } else if ( !strcmp(k, "codec") ) {
293   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
294    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
295   }
[2896]296  } else if ( !strcmp(k, "set-server") ) {
[2899]297   if ( roar_libroar_get_server() == NULL )
298    roar_libroar_set_server(v);
[3218]299  } else if ( !strcmp(k, "set-authfile") ) {
[3219]300   strncpy(config->authfile, v, 1023);
301   config->authfile[1023] = 0;
[3375]302  } else if ( !strcmp(k, "x11-display") ) {
303   config->x11.display = v;
[2884]304  } else {
[2888]305   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
[2884]306  }
307 }
308
309 return 0;
310}
311
[2886]312struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
[2893]313 struct roar_libroar_config * config = roar_libroar_get_config();
314 return roar_libroar_config_codec_get_conf(codec, create, config);
315}
316
317static struct roar_libroar_config_codec *
318           roar_libroar_config_codec_get_conf(int codec, int create, struct roar_libroar_config * config) {
[2886]319 int i;
320 int need_new = 1;
321
[2912]322 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
323
[2886]324 if ( codec < 0 || create < 0 )
325  return NULL;
326
[2912]327 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
328
[2886]329 if ( config->codecs.num == 0 ) {
330  // no match case:
331  if ( !create )
332   return NULL;
333 } else {
334  for (i = 0; i < config->codecs.num; i++) {
335   if ( config->codecs.codec[i].codec == codec )
336    return &(config->codecs.codec[i]);
337   if ( config->codecs.codec[i].codec == -1 )
338    need_new = 0;
339  }
340 }
341
[2912]342 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
343
[2886]344 if ( !create )
345  return NULL;
346
347 if ( !need_new ) {
348  for (i = 0; i < config->codecs.num; i++) {
349   if ( config->codecs.codec[i].codec == -1 ) {
350    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
351    config->codecs.codec[i].codec = codec;
352    return &(config->codecs.codec[i]);
353   }
354  }
355 }
356
357 if ( config->codecs.num == 0 ) {
[4957]358  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
[2886]359 } else {
[4957]360  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
[2886]361 }
362
363 if ( config->codecs.codec == NULL )
364  return NULL;
365
366 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
367 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
368  config->codecs.codec[i].codec = -1;
369 }
370
371 i = config->codecs.num;
372 config->codecs.num += 16;
373
[2913]374 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
375 config->codecs.codec[i].codec = codec;
376
[2886]377 return &(config->codecs.codec[i]);
378}
379
[2567]380int    roar_libroar_set_server(char * server) {
381 roar_libroar_get_config_ptr()->server = server;
382 return 0;
383}
384
385char * roar_libroar_get_server(void) {
386 return roar_libroar_get_config_ptr()->server;
387}
[2478]388
[3855]389void   roar_libroar_nowarn(void) {
390 roar_libroar_get_config_ptr()->nowarncounter++;
391}
392
393void   roar_libroar_warn(void) {
394 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
395
396 if ( cfg->nowarncounter == 0 ) {
397  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
398  return;
399 }
400
401 cfg->nowarncounter--;
402}
403
[2477]404//ll
Note: See TracBrowser for help on using the repository browser.