source: roaraudio/libroar/config.c @ 5006:1a7ae9683375

Last change on this file since 5006:1a7ae9683375 was 5006:1a7ae9683375, checked in by phi, 13 years ago

make file independed on strtok()

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