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

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

make file independed on strtok()

File size: 11.4 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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(int 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.server   = NULL;
55  config.authfile = NULL;
56
57  if ( home != NULL ) {
58   snprintf(authfile, 1023, "%s/.roarauth", home);
59   authfile[1023]     = 0;
60   config.authfile    = authfile;
61   config.serversfile = NULL;
62  }
63
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;
73 char * next;
74
75 if ( !inited ) {
76  next = getenv("ROAR_OPTIONS");
77
78  if ( next != NULL ) {
79   roar_libroar_config_parse(next, " ");
80  }
81
82  inited++;
83 }
84
85 return config;
86}
87
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;
96 char * toksave = NULL;
97
98 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
99
100 if ( config == NULL || txt == NULL )
101  return -1;
102
103 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
104
105 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
106
107 if ( codec_str == NULL )
108  return -1;
109
110 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
111
112 if ( option_str == NULL )
113  return -1;
114
115 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
116
117 if ( value_str == NULL )
118  return -1;
119
120 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
121
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
127 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
128
129 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
130  return -1;
131
132 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
133
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);
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);
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  }
163 } else {
164  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
165  return -1;
166 }
167
168 return 0;
169}
170
171int    roar_libroar_config_parse(char * txt, char * delm) {
172 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
173 ssize_t len;
174 char * k, * v, * next = txt;
175
176 while (next != NULL) {
177  k = next;
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
186  if ( next != NULL ) {
187   *next = 0;
188    next++;
189  }
190
191  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
192
193  // strip leading spaces:
194  while ( *k == ' ' ) k++;
195
196  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
197
198  // strip tailing new lions:
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;
203
204  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
205
206  // comments
207  if ( *k == '#' )
208   continue;
209
210  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
211
212  // empty options:
213  if ( *k == 0 )
214   continue;
215
216  if ( (v = strstr(k, ":")) != NULL ) {
217   *v = 0;
218    v++;
219  }
220
221  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
222
223  if ( !strcmp(k, "workaround") ) {
224   if ( !strcmp(v, "use-execed") ) {
225    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
226   } else if ( !strcmp(v, "no-slp") ) {
227    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
228   } else {
229    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
230   }
231  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
232   if ( !strcmp(v, "sysio") ) {
233    config->warnings.sysio = ROAR_WARNING_ALWAYS;
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;
239   } else {
240    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
241   }
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
258   } else if ( !strcmp(v, "die") ) {
259    config->trap_policy = ROAR_TRAP_DIE;
260   } else {
261    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
262   }
263#endif
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);
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   }
276  } else if ( !strcmp(k, "set-server") ) {
277   if ( roar_libroar_get_server() == NULL )
278    roar_libroar_set_server(v);
279  } else if ( !strcmp(k, "set-authfile") ) {
280   strncpy(config->authfile, v, 1023);
281   config->authfile[1023] = 0;
282  } else if ( !strcmp(k, "x11-display") ) {
283   config->x11.display = v;
284  } else {
285   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
286  }
287 }
288
289 return 0;
290}
291
292struct roar_libroar_config_codec * roar_libroar_config_codec_get(int codec, int create) {
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) {
299 int i;
300 int need_new = 1;
301
302 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
303
304 if ( codec < 0 || create < 0 )
305  return NULL;
306
307 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
308
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
322 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
323
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 ) {
338  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
339 } else {
340  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
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
354 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
355 config->codecs.codec[i].codec = codec;
356
357 return &(config->codecs.codec[i]);
358}
359
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}
368
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
384//ll
Note: See TracBrowser for help on using the repository browser.