source: roaraudio/libroar/config.c @ 5831:c7c74fda8b83

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

added more usefull paths

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