source: roaraudio/libroar/config.c @ 5840:f36d0a5127a2

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

use path system for default daemonimage.

File size: 22.3 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 {"bin-default-daemonimage", "roard", 0, 0},
495
496#ifdef ROAR_HAVE_BIN_SH
497 {"bin-sh",             ROAR_HAVE_BIN_SH, 0, 0},
498#endif
499#ifdef ROAR_HAVE_BIN_OGG123
500 {"bin-ogg123",         ROAR_HAVE_BIN_OGG123, 0, 0},
501#endif
502#ifdef ROAR_HAVE_BIN_FLAC
503 {"bin-flac",           ROAR_HAVE_BIN_FLAC, 0, 0},
504#endif
505#ifdef ROAR_HAVE_BIN_TIMIDITY
506 {"bin-timidity",       ROAR_HAVE_BIN_TIMIDITY, 0, 0},
507#endif
508#ifdef ROAR_HAVE_BIN_CDPARANOIA
509 {"bin-cdparanoia",     ROAR_HAVE_BIN_CDPARANOIA, 0, 0},
510#endif
511#ifdef ROAR_HAVE_BIN_GNUPLOT
512 {"bin-gnuplot",        ROAR_HAVE_BIN_GNUPLOT, 0, 0},
513#endif
514#ifdef ROAR_HAVE_BIN_SSH
515 {"bin-ssh",            ROAR_HAVE_BIN_SSH, 0, 0},
516#endif
517#ifdef ROAR_HAVE_BIN_PINENTRY
518 {"bin-pinentry",       ROAR_HAVE_BIN_PINENTRY, 0, 0},
519#endif
520#ifdef ROAR_HAVE_BIN_SSH_ASKPASS
521 {"bin-ssh-askpass",    ROAR_HAVE_BIN_SSH_ASKPASS, 0, 0},
522#endif
523#ifdef ROAR_HAVE_BIN_GTK_LED_ASKPASS
524 {"bin-gtk-led-askpass", ROAR_HAVE_BIN_GTK_LED_ASKPASS, 0, 0},
525#endif
526#ifdef ROAR_HAVE_BIN_X11_SSH_ASKPASS
527 {"bin-x11-ssh-askpass", ROAR_HAVE_BIN_X11_SSH_ASKPASS, 0, 0},
528#endif
529#ifdef ROAR_HAVE_BIN_GNOME_SSH_ASKPASS
530 {"bin-gnome-ssh-askpass", ROAR_HAVE_BIN_GNOME_SSH_ASKPASS, 0, 0},
531#endif
532#ifdef ROAR_HAVE_BIN_GPG
533 {"bin-gpg",            ROAR_HAVE_BIN_GPG, 0, 0},
534#endif
535#ifdef ROAR_HAVE_BIN_EJECT
536 {"bin-eject",          ROAR_HAVE_BIN_EJECT, 0, 0},
537#endif
538
539 // devices:
540 {"dev-stdin",                  ROAR_PREFIX_DEV "/stdin", 0, 0},
541 {"dev-default-pwmled",         ROAR_PREFIX_DEV "/ttyS0", 0, 0},
542 {"dev-default-dmx4linux",      ROAR_PREFIX_DEV "/dmx", 0, 0},
543
544#ifdef ROAR_DEFAULT_CDROM
545 {"dev-default-cdrom",          ROAR_DEFAULT_CDROM, 0, 0},
546#endif
547#ifdef ROAR_DEFAULT_TTY
548 {"dev-default-tty",            ROAR_DEFAULT_TTY, 0, 0},
549#endif
550#ifdef ROAR_DEFAULT_OSS_DEV
551 {"dev-default-oss-dev",        ROAR_DEFAULT_OSS_DEV, 0, 0},
552#endif
553#ifdef ROAR_DEFAULT_OSS_MIX_DEV
554 {"dev-default-oss-mix-dev",    ROAR_DEFAULT_OSS_MIX_DEV, 0, 0},
555#endif
556
557 // proc:
558#ifdef ROAR_PROC_NET_DECNET
559 {"proc-net-decnet",            ROAR_PROC_NET_DECNET, 0, 0},
560#endif
561#ifdef ROAR_PROC_NET_DECNET_NEIGH
562 {"proc-net-decnet-neigh",      ROAR_PROC_NET_DECNET_NEIGH, 0, 0},
563#endif
564#ifdef ROAR_PROC_NET_ARP
565 {"proc-net-arp",               ROAR_PROC_NET_ARP, 0, 0},
566#endif
567
568 // sysconf:
569 {"sysconf-hosts",              ROAR_PREFIX_SYSCONF "/hosts", 0, 0},
570 {"sysconf-roarserver",         ROAR_PREFIX_SYSCONF "/roarserver", 0, 0},
571
572 // special dirs:
573 {"dir-nx-home",                "/NX-HOME-DIR", 0, 0}
574};
575
576static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product, int type) {
577 const char * c;
578 const char * e;
579 const char * s;
580 const char * b;
581
582 if ( product == NULL ) {
583  if ( null_as_universal ) {
584   if ( type == 2 ) {
585    snprintf(path, pathlen, "/universal");
586   } else {
587    snprintf(path, pathlen, "/universal/universal");
588   }
589  } else {
590   path[0] = 0;
591  }
592  return 0;
593 }
594
595 b = strstr(product, " ");
596 c = strstr(product, "<");
597 e = strstr(product, ">");
598
599 if ( b == NULL || c == NULL || e == NULL || c < b || e < c ) {
600  roar_err_set(ROAR_ERROR_ILLSEQ);
601  return -1;
602 }
603
604 c++;
605
606 s = strstr(c, "/");
607
608 if ( type == 2 ) {
609  snprintf(path, pathlen, "/%.*s", (int)(size_t)(b-product), product);
610 } else if ( s == NULL ) {
611  snprintf(path, pathlen, "/unreg-%.*s/%.*s", (int)(size_t)(e-c), c, (int)(size_t)(b-product), product);
612 } else {
613  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);
614 }
615
616 return 0;
617}
618
619static int __provider2path(char * path, size_t pathlen, const char * provider) {
620 const char * c;
621 const char * e;
622 const char * s;
623
624 if ( provider == NULL ) {
625  path[0] = 0;
626  return 0;
627 }
628
629 c = strstr(provider, "<");
630 e = strstr(provider, ">");
631
632 if ( c == NULL || e == NULL || e < c ) {
633  roar_err_set(ROAR_ERROR_ILLSEQ);
634  return -1;
635 }
636
637 c++;
638
639 s = strstr(c, "/");
640
641 if ( s == NULL ) {
642  snprintf(path, pathlen, "/unreg-%.*s", (int)(size_t)(e-c), c);
643 } else {
644  snprintf(path, pathlen, "/%.*s-%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1);
645 }
646
647 return 0;
648
649 roar_err_set(ROAR_ERROR_NOTSUP);
650 return -1;
651}
652
653static const struct pathinfo * __lookup_path(const char * name) {
654 size_t i;
655
656 if ( name == NULL ) {
657  roar_err_set(ROAR_ERROR_FAULT);
658  return NULL;
659 }
660
661 for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++)
662  if ( !strcmp(__paths[i].name, name) )
663   return &(__paths[i]);
664
665 roar_err_set(ROAR_ERROR_NOENT);
666 return NULL;
667}
668
669void __strip_double_slashes(char * p) {
670 const char * in = p;
671
672 for (; *in; in++, p++) {
673  *p = *in;
674  if ( *in == '/' )
675   for (; in[1] == '/'; in++);
676 }
677
678 *p = 0;
679}
680
681char * roar_libroar_get_path(const char * name, int null_as_universal, const char * product, const char * provider) {
682 const struct pathinfo * path;
683 char buf_product[384];
684 char buf_provider[384];
685 ssize_t len_prefix, len_product, len_provider;
686 char * ret, * p;
687
688 ROAR_DBG("roar_libroar_get_path(name='%s', null_as_universal=%i, product='%s', provider='%s') = ?", name, null_as_universal, product, provider);
689
690 path = __lookup_path(name);
691 if ( path == NULL )
692  return NULL;
693
694 if ( ((null_as_universal || product != NULL) && !path->with_product) ||
695      (provider != NULL && !path->with_provider) ) {
696  roar_err_set(ROAR_ERROR_INVAL);
697  return NULL;
698 }
699
700 if ( __product2path(buf_product, sizeof(buf_product), null_as_universal, product, path->with_product) == -1 )
701  return NULL;
702
703 if ( __provider2path(buf_provider, sizeof(buf_provider), provider) == -1 )
704  return NULL;
705
706 len_prefix = roar_mm_strlen(path->path);
707 len_product = roar_mm_strlen(buf_product);
708 len_provider = roar_mm_strlen(buf_provider);
709
710 p = ret = roar_mm_malloc(len_prefix+len_product+len_provider+1);
711 if ( ret == NULL )
712  return NULL;
713
714 memcpy(p, path->path, len_prefix);
715 p += len_prefix;
716 if ( p[-1] == '/' )
717  p--;
718 memcpy(p, buf_product, len_product);
719 p += len_product;
720 memcpy(p, buf_provider, len_provider);
721 p += len_provider;
722
723 *p = 0;
724
725 __strip_double_slashes(ret);
726
727 return ret;
728}
729
730const char * roar_libroar_get_path_static(const char * name) {
731 const struct pathinfo * path;
732
733 path = __lookup_path(name);
734 if ( path == NULL )
735  return NULL;
736
737 return path->path;
738}
739
740ssize_t roar_libroar_list_path(const char ** list, size_t len, size_t offset) {
741 size_t i;
742 ssize_t idx = 0;
743
744 ROAR_DBG("roar_libroar_list_path(list=%p, len=%lu, offset=%lu) = ?", list, (long unsigned int)len, (long unsigned int)offset);
745
746 if ( list == NULL ) {
747  roar_err_set(ROAR_ERROR_FAULT);
748  return -1;
749 }
750
751 if ( len == 0 )
752  return 0;
753
754 if ( offset >= (sizeof(__paths)/sizeof(*__paths)) )
755  return 0;
756
757 for (i = offset; idx < len && i < (sizeof(__paths)/sizeof(*__paths)); i++) {
758  list[idx++] = __paths[i].name;
759 }
760
761 return idx;
762}
763
764//ll
Note: See TracBrowser for help on using the repository browser.