source: roaraudio/libroar/config.c @ 5839:bb64c0dfed90

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

make use of #ifdef so we do not end up on usage of undefined macros

File size: 22.2 KB
Line 
1//config.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013
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#define LEN_AUTHFILE 1024
39
40static struct roar_libroar_config_codec *
41           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config);
42
43
44struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
45 static struct roar_libroar_config config;
46 static int    inited = 0;
47 static char   authfile[LEN_AUTHFILE];
48 const  char * home = roar_env_get_home(0);
49
50 if ( !inited ) {
51  memset(&config, 0, sizeof(config));
52
53#ifdef ROAR_SUPPORT_TRAP
54  config.trap_policy      = ROAR_TRAP_IGNORE;
55#endif
56  config.opmode           = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL;
57  config.server           = NULL;
58  config.authfile         = NULL;
59  config.forkapi          = NULL;
60  config.connect_internal = NULL;
61  config.daemonimage      = NULL;
62  config.protocolversion  = -1; // use default.
63
64  if ( home != NULL ) {
65   snprintf(authfile, sizeof(authfile)-1, "%s/.roarauth", home);
66   authfile[sizeof(authfile)-1] = 0;
67   config.authfile    = authfile;
68   config.serversfile = NULL;
69  }
70
71  inited++;
72 }
73
74 return &config;
75}
76
77struct roar_libroar_config * roar_libroar_get_config(void) {
78 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
79 static int inited = 0;
80 const char * next;
81 char * buf;
82
83 if ( !inited ) {
84  inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse().
85
86  next = roar_env_get("ROAR_OPTIONS");
87
88  if ( next != NULL ) {
89   if ( (buf = roar_mm_strdup(next)) != NULL ) {
90    roar_libroar_config_parse(buf, " ");
91    roar_mm_free(buf);
92   }
93  }
94 }
95
96 return config;
97}
98
99int    roar_libroar_reset_config(void) {
100 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
101
102 if ( config->codecs.codec != NULL ) {
103  roar_mm_free(config->codecs.codec);
104  config->codecs.num = 0;
105 }
106
107 if ( config->x11.display != NULL )
108  roar_mm_free(config->x11.display);
109 config->x11.display = NULL;
110
111 if ( config->daemonimage != NULL )
112  roar_mm_free(config->daemonimage);
113 config->daemonimage = NULL;
114
115 return 0;
116}
117
118#define _P_FP(v)   ((int)(atof((v))*256.0))
119#define _P_INT(v)  (atoi((v)))
120#define _P_BOOL(v) (*(v) == 'y' || *(v) == 'j' || *(v) == 't' || *(v) == '1' ? 1 : 0)
121
122static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) {
123 struct roar_libroar_config_codec * codec_cfg;
124 int32_t codec;
125 char * codec_str, * option_str, * value_str;
126 char * toksave = NULL;
127
128 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
129
130 if ( config == NULL || txt == NULL )
131  return -1;
132
133 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
134
135 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
136
137 if ( codec_str == NULL )
138  return -1;
139
140 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
141
142 if ( option_str == NULL )
143  return -1;
144
145 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
146
147 if ( value_str == NULL )
148  return -1;
149
150 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
151
152 if ( (codec = roar_str2codec(codec_str)) == -1 ) {
153  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec: %s", codec_str);
154  return -1;
155 }
156
157 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
158
159 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
160  return -1;
161
162 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
163
164 if ( !strcmp(option_str, "q") || !strcmp(option_str, "quality") ) {
165  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_Q;
166  codec_cfg->q = _P_FP(value_str);
167 } else if ( !strcmp(option_str, "complexity") ) {
168  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY;
169  codec_cfg->complexity = _P_FP(value_str);
170 } else if ( !strcmp(option_str, "dtx") ) {
171  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_DTX;
172  codec_cfg->dtx = _P_BOOL(value_str);
173 } else if ( !strcmp(option_str, "cc-max") ) {
174  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MAX_CC;
175  codec_cfg->max_cc = _P_INT(value_str);
176 } else if ( !strcmp(option_str, "vbr") ) {
177  codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_VBR;
178  codec_cfg->vbr = _P_BOOL(value_str);
179 } else if ( !strcmp(option_str, "mode") ) {
180  if ( !strcmp(value_str, "nb") ) {
181   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
182   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_NB;
183  } else if ( !strcmp(value_str, "wb") ) {
184   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
185   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_WB;
186  } else if ( !strcmp(value_str, "uwb") ) {
187   codec_cfg->para_set |= ROAR_LIBROAR_CONFIG_PSET_MODE;
188   codec_cfg->mode      = ROAR_LIBROAR_CONFIG_MODE_UWB;
189  } else {
190   ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec mode: %s", value_str);
191   return -1;
192  }
193 } else {
194  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
195  return -1;
196 }
197
198 return 0;
199}
200
201int    roar_libroar_config_parse(char * txt, char * delm) {
202 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
203 ssize_t len;
204 char * k, * v, * next = txt;
205
206 while (next != NULL) {
207  k = next;
208
209  if ( delm == NULL ) {
210   // no delm -> we have only one option
211   next = NULL;
212  } else {
213   next = strstr(next, delm);
214  }
215
216  if ( next != NULL ) {
217   *next = 0;
218    next++;
219  }
220
221  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
222
223  // strip leading spaces:
224  while ( *k == ' ' ) k++;
225
226  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
227
228  // strip tailing new lions:
229  len = roar_mm_strlen(k);
230  if ( len != -1 )
231   for (len--; len && (k[len] == '\r' || k[len] == '\n'); len--)
232    k[len] = 0;
233
234  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
235
236  // comments
237  if ( *k == '#' )
238   continue;
239
240  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
241
242  // empty options:
243  if ( *k == 0 )
244   continue;
245
246  if ( (v = strstr(k, ":")) != NULL ) {
247   *v = 0;
248    v++;
249  }
250
251  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
252
253  if ( !strcmp(k, "workaround") ) {
254   if ( !strcmp(v, "use-execed") ) {
255    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
256   } else if ( !strcmp(v, "no-slp") ) {
257    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
258   } else {
259    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
260   }
261  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
262   if ( !strcmp(v, "sysio") ) {
263    config->warnings.sysio = ROAR_WARNING_ALWAYS;
264   } else if ( !strcmp(v, "obsolete") ) {
265    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
266   } else if ( !strcmp(v, "all") ) {
267    config->warnings.sysio    = ROAR_WARNING_ALWAYS;
268    config->warnings.obsolete = ROAR_WARNING_ALWAYS;
269   } else {
270    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
271   }
272  } else if ( !strcmp(k, "opmode") ) {
273   if ( !strcmp(v, "normal") ) {
274    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL;
275   } else if ( !strcmp(v, "funny") ) {
276    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_FUNNY;
277   } else if ( !strcmp(v, "ms") ) {
278    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_MS;
279   } else {
280    ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v);
281   }
282#ifdef ROAR_SUPPORT_TRAP
283  } else if ( !strcmp(k, "trap-policy") ) {
284   if ( !strcmp(v, "ignore") ) {
285    config->trap_policy = ROAR_TRAP_IGNORE;
286   } else if ( !strcmp(v, "warn") ) {
287    config->trap_policy = ROAR_TRAP_WARN;
288   } else if ( !strcmp(v, "abort") ) {
289    config->trap_policy = ROAR_TRAP_ABORT;
290#ifdef SIGKILL
291   } else if ( !strcmp(v, "kill") ) {
292    config->trap_policy = ROAR_TRAP_KILL;
293#endif
294#ifdef SIGSTOP
295   } else if ( !strcmp(v, "stop") ) {
296    config->trap_policy = ROAR_TRAP_STOP;
297#endif
298   } else if ( !strcmp(v, "die") ) {
299    config->trap_policy = ROAR_TRAP_DIE;
300   } else {
301    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
302   }
303#endif
304  } else if ( !strcmp(k, "force-rate") ) {
305   config->info.rate = roar_str2rate(v);
306  } else if ( !strcmp(k, "force-bits") ) {
307   config->info.bits = roar_str2bits(v);
308  } else if ( !strcmp(k, "force-channels") ) {
309   config->info.channels = roar_str2channels(v);
310  } else if ( !strcmp(k, "force-codec") ) {
311   config->info.codec = roar_str2codec(v);
312  } else if ( !strcmp(k, "codec") ) {
313   if ( roar_libroar_config_parse_codec(config, v) == -1 ) {
314    ROAR_WARN("roar_libroar_config_parse(*): Error parsing codec config option");
315   }
316  } else if ( !strcmp(k, "set-server") ) {
317   if ( roar_libroar_get_server() == NULL )
318    roar_libroar_set_server(v);
319  } else if ( !strcmp(k, "set-authfile") ) {
320   strncpy(config->authfile, v, LEN_AUTHFILE-1);
321   config->authfile[LEN_AUTHFILE-1] = 0;
322  } else if ( !strcmp(k, "x11-display") ) {
323   if ( config->x11.display != NULL )
324    roar_mm_free(config->x11.display);
325   config->x11.display = roar_mm_strdup(v);
326  } else if ( !strcmp(k, "daemonimage") ) {
327   if ( config->daemonimage != NULL )
328    roar_mm_free(config->daemonimage);
329   config->daemonimage = roar_mm_strdup(v);
330  } else if ( !strcmp(k, "serverflags") ) {
331   if ( !strcmp(v, "nonblock") ) {
332    config->serverflags |= ROAR_ENUM_FLAG_NONBLOCK;
333   } else if ( !strcmp(v, "hardnonblock") ) {
334    config->serverflags |= ROAR_ENUM_FLAG_HARDNONBLOCK;
335   } else if ( !strcmp(v, "localonly") ) {
336    config->serverflags |= ROAR_ENUM_FLAG_LOCALONLY;
337   } else {
338    ROAR_WARN("roar_libroar_config_parse(*): Unknown serverflag: %s", v);
339   }
340  } else if ( !strcmp(k, "protocolversion") ) {
341   config->protocolversion = atoi(v);
342  } else {
343   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
344  }
345 }
346
347 return 0;
348}
349
350struct roar_libroar_config_codec * roar_libroar_config_codec_get(int32_t codec, int create) {
351 struct roar_libroar_config * config = roar_libroar_get_config();
352 return roar_libroar_config_codec_get_conf(codec, create, config);
353}
354
355static struct roar_libroar_config_codec *
356           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config) {
357 size_t i;
358 int need_new = 1;
359
360 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
361
362 if ( codec < 0 || create < 0 )
363  return NULL;
364
365 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
366
367 if ( config->codecs.num == 0 ) {
368  // no match case:
369  if ( !create )
370   return NULL;
371 } else {
372  for (i = 0; i < config->codecs.num; i++) {
373   if ( config->codecs.codec[i].codec == (uint32_t)codec )
374    return &(config->codecs.codec[i]);
375   if ( config->codecs.codec[i].codec == (uint32_t)-1 )
376    need_new = 0;
377  }
378 }
379
380 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
381
382 if ( !create )
383  return NULL;
384
385 if ( !need_new ) {
386  for (i = 0; i < config->codecs.num; i++) {
387   if ( config->codecs.codec[i].codec == (uint32_t)-1 ) {
388    memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
389    config->codecs.codec[i].codec = codec;
390    return &(config->codecs.codec[i]);
391   }
392  }
393 }
394
395 if ( config->codecs.num == 0 ) {
396  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
397 } else {
398  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
399 }
400
401 if ( config->codecs.codec == NULL )
402  return NULL;
403
404 memset(&(config->codecs.codec[config->codecs.num]), 0, 16);
405 for (i = config->codecs.num; i < (config->codecs.num+16); i++) {
406  config->codecs.codec[i].codec = (uint32_t)-1;
407 }
408
409 i = config->codecs.num;
410 config->codecs.num += 16;
411
412 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
413 config->codecs.codec[i].codec = codec;
414
415 return &(config->codecs.codec[i]);
416}
417
418int    roar_libroar_set_server(const char * server) {
419 roar_libroar_get_config_ptr()->server = server;
420 return 0;
421}
422
423const char * roar_libroar_get_server(void) {
424 return roar_libroar_get_config_ptr()->server;
425}
426
427int    roar_libroar_set_forkapi(const struct roar_libroar_forkapi * api) {
428 roar_libroar_get_config_ptr()->forkapi = api;
429 return 0;
430}
431
432int    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)) {
433 roar_libroar_get_config_ptr()->connect_internal = func;
434 return 0;
435}
436
437void   roar_libroar_nowarn(void) {
438 roar_libroar_get_config_ptr()->nowarncounter++;
439}
440
441void   roar_libroar_warn(void) {
442 struct roar_libroar_config * cfg = roar_libroar_get_config_ptr();
443
444 if ( cfg->nowarncounter == 0 ) {
445  ROAR_WARN("roar_libroar_warn(): Re-Enabling already enabled warnings! (Application error?)");
446  return;
447 }
448
449 cfg->nowarncounter--;
450}
451
452static const struct pathinfo {
453 const char * name;
454 const char * path;
455 const int    with_product; // 0 = no, 1 = yes, 2 = just product.
456 const int    with_provider;
457} __paths[] = {
458 // prefixes:
459 {"prefix",             ROAR_PREFIX, 0, 0},
460 {"prefix-bin",         ROAR_PREFIX_BIN, 0, 0},
461 {"prefix-sbin",        ROAR_PREFIX_SBIN, 0, 0},
462 {"prefix-lib",         ROAR_PREFIX_LIB, 0, 0},
463 {"prefix-inc",         ROAR_PREFIX_INC, 0, 0},
464 {"prefix-man",         ROAR_PREFIX_MAN, 0, 0},
465 {"prefix-man1",        ROAR_PREFIX_MAN "/man1", 0, 0},
466 {"prefix-man2",        ROAR_PREFIX_MAN "/man2", 0, 0},
467 {"prefix-man3",        ROAR_PREFIX_MAN "/man3", 0, 0},
468 {"prefix-man4",        ROAR_PREFIX_MAN "/man4", 0, 0},
469 {"prefix-man5",        ROAR_PREFIX_MAN "/man5", 0, 0},
470 {"prefix-man6",        ROAR_PREFIX_MAN "/man6", 0, 0},
471 {"prefix-man7",        ROAR_PREFIX_MAN "/man7", 0, 0},
472 {"prefix-man8",        ROAR_PREFIX_MAN "/man8", 0, 0},
473 {"prefix-man9",        ROAR_PREFIX_MAN "/man9", 0, 0},
474 {"prefix-pc",          ROAR_PREFIX_PC, 0, 0},
475 {"prefix-ckport",      ROAR_PREFIX_CKPORT, 0, 0},
476 {"prefix-sysconf",     ROAR_PREFIX_SYSCONF, 2, 0},
477 {"prefix-dev",         ROAR_PREFIX_DEV, 0, 0},
478 {"prefix-doc",         ROAR_PREFIX_DOC, 2, 0},
479 {"prefix-tmp",         ROAR_PREFIX_TMP, 0, 0},
480 {"prefix-var",         ROAR_PREFIX_VAR, 0, 0},
481 {"prefix-cache",       ROAR_PREFIX_CACHE, 2, 0},
482 {"prefix-data",        ROAR_PREFIX_DATA, 2, 0},
483 {"prefix-lock",        ROAR_PREFIX_LOCK, 0, 0},
484 {"prefix-log",         ROAR_PREFIX_LOG, 0, 0},
485 {"prefix-mail",        ROAR_PREFIX_MAIL, 0, 0},
486 {"prefix-run",         ROAR_PREFIX_RUN, 0, 0},
487 {"prefix-spool",       ROAR_PREFIX_SPOOL, 2, 0},
488 {"prefix-comp-libs",   ROAR_PREFIX_COMP_LIBS, 0, 0},
489 {"prefix-comp-bins",   ROAR_PREFIX_COMP_BINS, 0, 0},
490 {"prefix-plugins",     ROAR_PREFIX_PLUGINS, 1, 1},
491 {"prefix-buildsystem", ROAR_PREFIX_BUILDSYSTEM, 0, 0},
492
493 // bins:
494#ifdef ROAR_HAVE_BIN_SH
495 {"bin-sh",             ROAR_HAVE_BIN_SH, 0, 0},
496#endif
497#ifdef ROAR_HAVE_BIN_OGG123
498 {"bin-ogg123",         ROAR_HAVE_BIN_OGG123, 0, 0},
499#endif
500#ifdef ROAR_HAVE_BIN_FLAC
501 {"bin-flac",           ROAR_HAVE_BIN_FLAC, 0, 0},
502#endif
503#ifdef ROAR_HAVE_BIN_TIMIDITY
504 {"bin-timidity",       ROAR_HAVE_BIN_TIMIDITY, 0, 0},
505#endif
506#ifdef ROAR_HAVE_BIN_CDPARANOIA
507 {"bin-cdparanoia",     ROAR_HAVE_BIN_CDPARANOIA, 0, 0},
508#endif
509#ifdef ROAR_HAVE_BIN_GNUPLOT
510 {"bin-gnuplot",        ROAR_HAVE_BIN_GNUPLOT, 0, 0},
511#endif
512#ifdef ROAR_HAVE_BIN_SSH
513 {"bin-ssh",            ROAR_HAVE_BIN_SSH, 0, 0},
514#endif
515#ifdef ROAR_HAVE_BIN_PINENTRY
516 {"bin-pinentry",       ROAR_HAVE_BIN_PINENTRY, 0, 0},
517#endif
518#ifdef ROAR_HAVE_BIN_SSH_ASKPASS
519 {"bin-ssh-askpass",    ROAR_HAVE_BIN_SSH_ASKPASS, 0, 0},
520#endif
521#ifdef ROAR_HAVE_BIN_GTK_LED_ASKPASS
522 {"bin-gtk-led-askpass", ROAR_HAVE_BIN_GTK_LED_ASKPASS, 0, 0},
523#endif
524#ifdef ROAR_HAVE_BIN_X11_SSH_ASKPASS
525 {"bin-x11-ssh-askpass", ROAR_HAVE_BIN_X11_SSH_ASKPASS, 0, 0},
526#endif
527#ifdef ROAR_HAVE_BIN_GNOME_SSH_ASKPASS
528 {"bin-gnome-ssh-askpass", ROAR_HAVE_BIN_GNOME_SSH_ASKPASS, 0, 0},
529#endif
530#ifdef ROAR_HAVE_BIN_GPG
531 {"bin-gpg",            ROAR_HAVE_BIN_GPG, 0, 0},
532#endif
533#ifdef ROAR_HAVE_BIN_EJECT
534 {"bin-eject",          ROAR_HAVE_BIN_EJECT, 0, 0},
535#endif
536
537 // devices:
538 {"dev-stdin",                  ROAR_PREFIX_DEV "/stdin", 0, 0},
539 {"dev-default-pwmled",         ROAR_PREFIX_DEV "/ttyS0", 0, 0},
540 {"dev-default-dmx4linux",      ROAR_PREFIX_DEV "/dmx", 0, 0},
541
542#ifdef ROAR_DEFAULT_CDROM
543 {"dev-default-cdrom",          ROAR_DEFAULT_CDROM, 0, 0},
544#endif
545#ifdef ROAR_DEFAULT_TTY
546 {"dev-default-tty",            ROAR_DEFAULT_TTY, 0, 0},
547#endif
548#ifdef ROAR_DEFAULT_OSS_DEV
549 {"dev-default-oss-dev",        ROAR_DEFAULT_OSS_DEV, 0, 0},
550#endif
551#ifdef ROAR_DEFAULT_OSS_MIX_DEV
552 {"dev-default-oss-mix-dev",    ROAR_DEFAULT_OSS_MIX_DEV, 0, 0},
553#endif
554
555 // proc:
556#ifdef ROAR_PROC_NET_DECNET
557 {"proc-net-decnet",            ROAR_PROC_NET_DECNET, 0, 0},
558#endif
559#ifdef ROAR_PROC_NET_DECNET_NEIGH
560 {"proc-net-decnet-neigh",      ROAR_PROC_NET_DECNET_NEIGH, 0, 0},
561#endif
562#ifdef ROAR_PROC_NET_ARP
563 {"proc-net-arp",               ROAR_PROC_NET_ARP, 0, 0},
564#endif
565
566 // sysconf:
567 {"sysconf-hosts",              ROAR_PREFIX_SYSCONF "/hosts", 0, 0},
568 {"sysconf-roarserver",         ROAR_PREFIX_SYSCONF "/roarserver", 0, 0},
569
570 // special dirs:
571 {"dir-nx-home",                "/NX-HOME-DIR", 0, 0}
572};
573
574static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product, int type) {
575 const char * c;
576 const char * e;
577 const char * s;
578 const char * b;
579
580 if ( product == NULL ) {
581  if ( null_as_universal ) {
582   if ( type == 2 ) {
583    snprintf(path, pathlen, "/universal");
584   } else {
585    snprintf(path, pathlen, "/universal/universal");
586   }
587  } else {
588   path[0] = 0;
589  }
590  return 0;
591 }
592
593 b = strstr(product, " ");
594 c = strstr(product, "<");
595 e = strstr(product, ">");
596
597 if ( b == NULL || c == NULL || e == NULL || c < b || e < c ) {
598  roar_err_set(ROAR_ERROR_ILLSEQ);
599  return -1;
600 }
601
602 c++;
603
604 s = strstr(c, "/");
605
606 if ( type == 2 ) {
607  snprintf(path, pathlen, "/%.*s", (int)(size_t)(b-product), product);
608 } else if ( s == NULL ) {
609  snprintf(path, pathlen, "/unreg-%.*s/%.*s", (int)(size_t)(e-c), c, (int)(size_t)(b-product), product);
610 } else {
611  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);
612 }
613
614 return 0;
615}
616
617static int __provider2path(char * path, size_t pathlen, const char * provider) {
618 const char * c;
619 const char * e;
620 const char * s;
621
622 if ( provider == NULL ) {
623  path[0] = 0;
624  return 0;
625 }
626
627 c = strstr(provider, "<");
628 e = strstr(provider, ">");
629
630 if ( c == NULL || e == NULL || e < c ) {
631  roar_err_set(ROAR_ERROR_ILLSEQ);
632  return -1;
633 }
634
635 c++;
636
637 s = strstr(c, "/");
638
639 if ( s == NULL ) {
640  snprintf(path, pathlen, "/unreg-%.*s", (int)(size_t)(e-c), c);
641 } else {
642  snprintf(path, pathlen, "/%.*s-%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1);
643 }
644
645 return 0;
646
647 roar_err_set(ROAR_ERROR_NOTSUP);
648 return -1;
649}
650
651static const struct pathinfo * __lookup_path(const char * name) {
652 size_t i;
653
654 if ( name == NULL ) {
655  roar_err_set(ROAR_ERROR_FAULT);
656  return NULL;
657 }
658
659 for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++)
660  if ( !strcmp(__paths[i].name, name) )
661   return &(__paths[i]);
662
663 roar_err_set(ROAR_ERROR_NOENT);
664 return NULL;
665}
666
667void __strip_double_slashes(char * p) {
668 const char * in = p;
669
670 for (; *in; in++, p++) {
671  *p = *in;
672  if ( *in == '/' )
673   for (; in[1] == '/'; in++);
674 }
675
676 *p = 0;
677}
678
679char * roar_libroar_get_path(const char * name, int null_as_universal, const char * product, const char * provider) {
680 const struct pathinfo * path;
681 char buf_product[384];
682 char buf_provider[384];
683 ssize_t len_prefix, len_product, len_provider;
684 char * ret, * p;
685
686 ROAR_DBG("roar_libroar_get_path(name='%s', null_as_universal=%i, product='%s', provider='%s') = ?", name, null_as_universal, product, provider);
687
688 path = __lookup_path(name);
689 if ( path == NULL )
690  return NULL;
691
692 if ( ((null_as_universal || product != NULL) && !path->with_product) ||
693      (provider != NULL && !path->with_provider) ) {
694  roar_err_set(ROAR_ERROR_INVAL);
695  return NULL;
696 }
697
698 if ( __product2path(buf_product, sizeof(buf_product), null_as_universal, product, path->with_product) == -1 )
699  return NULL;
700
701 if ( __provider2path(buf_provider, sizeof(buf_provider), provider) == -1 )
702  return NULL;
703
704 len_prefix = roar_mm_strlen(path->path);
705 len_product = roar_mm_strlen(buf_product);
706 len_provider = roar_mm_strlen(buf_provider);
707
708 p = ret = roar_mm_malloc(len_prefix+len_product+len_provider+1);
709 if ( ret == NULL )
710  return NULL;
711
712 memcpy(p, path->path, len_prefix);
713 p += len_prefix;
714 if ( p[-1] == '/' )
715  p--;
716 memcpy(p, buf_product, len_product);
717 p += len_product;
718 memcpy(p, buf_provider, len_provider);
719 p += len_provider;
720
721 *p = 0;
722
723 __strip_double_slashes(ret);
724
725 return ret;
726}
727
728const char * roar_libroar_get_path_static(const char * name) {
729 const struct pathinfo * path;
730
731 path = __lookup_path(name);
732 if ( path == NULL )
733  return NULL;
734
735 return path->path;
736}
737
738ssize_t roar_libroar_list_path(const char ** list, size_t len, size_t offset) {
739 size_t i;
740 ssize_t idx = 0;
741
742 ROAR_DBG("roar_libroar_list_path(list=%p, len=%lu, offset=%lu) = ?", list, (long unsigned int)len, (long unsigned int)offset);
743
744 if ( list == NULL ) {
745  roar_err_set(ROAR_ERROR_FAULT);
746  return -1;
747 }
748
749 if ( len == 0 )
750  return 0;
751
752 if ( offset >= (sizeof(__paths)/sizeof(*__paths)) )
753  return 0;
754
755 for (i = offset; idx < len && i < (sizeof(__paths)/sizeof(*__paths)); i++) {
756  list[idx++] = __paths[i].name;
757 }
758
759 return idx;
760}
761
762//ll
Note: See TracBrowser for help on using the repository browser.