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
RevLine 
[2477]1//config.c:
2
3/*
[5823]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013
[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
[5838]38#define LEN_AUTHFILE 1024
39
[2895]40static struct roar_libroar_config_codec *
[5357]41           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config);
[2895]42
43
[2478]44struct roar_libroar_config * roar_libroar_get_config_ptr(void) {
45 static struct roar_libroar_config config;
[3218]46 static int    inited = 0;
[5838]47 static char   authfile[LEN_AUTHFILE];
[4517]48 const  char * home = roar_env_get_home(0);
[2478]49
50 if ( !inited ) {
51  memset(&config, 0, sizeof(config));
[2567]52
[4779]53#ifdef ROAR_SUPPORT_TRAP
[5373]54  config.trap_policy      = ROAR_TRAP_IGNORE;
[4779]55#endif
[5373]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;
[5465]62  config.protocolversion  = -1; // use default.
[3218]63
64  if ( home != NULL ) {
[5373]65   snprintf(authfile, sizeof(authfile)-1, "%s/.roarauth", home);
66   authfile[sizeof(authfile)-1] = 0;
[3375]67   config.authfile    = authfile;
[4348]68   config.serversfile = NULL;
[3218]69  }
[2567]70
[2478]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;
[5754]80 const char * next;
81 char * buf;
[2478]82
83 if ( !inited ) {
[5201]84  inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse().
85
[5754]86  next = roar_env_get("ROAR_OPTIONS");
[2480]87
[2884]88  if ( next != NULL ) {
[5754]89   if ( (buf = roar_mm_strdup(next)) != NULL ) {
90    roar_libroar_config_parse(buf, " ");
91    roar_mm_free(buf);
92   }
[2480]93  }
[2478]94 }
95
96 return config;
97}
98
[5010]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
[5838]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
[5010]115 return 0;
116}
117
[2890]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;
[5357]124 int32_t codec;
[2890]125 char * codec_str, * option_str, * value_str;
[5006]126 char * toksave = NULL;
[2890]127
[2912]128 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
129
[2890]130 if ( config == NULL || txt == NULL )
131  return -1;
132
[2912]133 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
134
[5006]135 codec_str = roar_mm_strtok_r(txt, ":", &toksave);
[2890]136
[2892]137 if ( codec_str == NULL )
138  return -1;
139
[5006]140 option_str = roar_mm_strtok_r(NULL, ":", &toksave);
[2890]141
142 if ( option_str == NULL )
143  return -1;
144
[5006]145 value_str = roar_mm_strtok_r(NULL, ":", &toksave);
[2890]146
147 if ( value_str == NULL )
148  return -1;
149
[2912]150 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt);
151
[2890]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
[2912]157 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec);
158
[2893]159 if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL )
[2890]160  return -1;
161
[2912]162 ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg);
163
[2890]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);
[2926]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);
[2962]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  }
[2890]193 } else {
[2891]194  ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str);
[2892]195  return -1;
[2890]196 }
197
[2892]198 return 0;
[2890]199}
200
[2884]201int    roar_libroar_config_parse(char * txt, char * delm) {
202 struct roar_libroar_config * config = roar_libroar_get_config_ptr();
[5006]203 ssize_t len;
[2884]204 char * k, * v, * next = txt;
205
206 while (next != NULL) {
207  k = next;
[2886]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
[2884]216  if ( next != NULL ) {
217   *next = 0;
218    next++;
219  }
220
[2888]221  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
222
[2887]223  // strip leading spaces:
224  while ( *k == ' ' ) k++;
225
[2888]226  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
227
[2887]228  // strip tailing new lions:
[5006]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;
[2887]233
[2888]234  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
235
[2887]236  // comments
237  if ( *k == '#' )
238   continue;
239
[2888]240  ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k);
241
[2887]242  // empty options:
243  if ( *k == 0 )
244   continue;
245
[2884]246  if ( (v = strstr(k, ":")) != NULL ) {
247   *v = 0;
248    v++;
249  }
250
[2888]251  ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v);
252
[2884]253  if ( !strcmp(k, "workaround") ) {
254   if ( !strcmp(v, "use-execed") ) {
255    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED;
[4653]256   } else if ( !strcmp(v, "no-slp") ) {
257    config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
[2884]258   } else {
[2888]259    ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v);
[2884]260   }
261  } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) {
262   if ( !strcmp(v, "sysio") ) {
263    config->warnings.sysio = ROAR_WARNING_ALWAYS;
[3855]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;
[2884]269   } else {
[2888]270    ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v);
[2884]271   }
[5183]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;
[5202]277   } else if ( !strcmp(v, "ms") ) {
278    config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_MS;
[5183]279   } else {
280    ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v);
281   }
[4779]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
[4784]298   } else if ( !strcmp(v, "die") ) {
299    config->trap_policy = ROAR_TRAP_DIE;
[4779]300   } else {
301    ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v);
302   }
303#endif
[3136]304  } else if ( !strcmp(k, "force-rate") ) {
[5466]305   config->info.rate = roar_str2rate(v);
[3136]306  } else if ( !strcmp(k, "force-bits") ) {
[5466]307   config->info.bits = roar_str2bits(v);
[3136]308  } else if ( !strcmp(k, "force-channels") ) {
[5466]309   config->info.channels = roar_str2channels(v);
[3136]310  } else if ( !strcmp(k, "force-codec") ) {
311   config->info.codec = roar_str2codec(v);
[2890]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   }
[2896]316  } else if ( !strcmp(k, "set-server") ) {
[2899]317   if ( roar_libroar_get_server() == NULL )
318    roar_libroar_set_server(v);
[3218]319  } else if ( !strcmp(k, "set-authfile") ) {
[5838]320   strncpy(config->authfile, v, LEN_AUTHFILE-1);
321   config->authfile[LEN_AUTHFILE-1] = 0;
[3375]322  } else if ( !strcmp(k, "x11-display") ) {
[5838]323   if ( config->x11.display != NULL )
324    roar_mm_free(config->x11.display);
325   config->x11.display = roar_mm_strdup(v);
[5373]326  } else if ( !strcmp(k, "daemonimage") ) {
[5838]327   if ( config->daemonimage != NULL )
328    roar_mm_free(config->daemonimage);
329   config->daemonimage = roar_mm_strdup(v);
[5373]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   }
[5465]340  } else if ( !strcmp(k, "protocolversion") ) {
341   config->protocolversion = atoi(v);
[2884]342  } else {
[2888]343   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k);
[2884]344  }
345 }
346
347 return 0;
348}
349
[5357]350struct roar_libroar_config_codec * roar_libroar_config_codec_get(int32_t codec, int create) {
[2893]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 *
[5357]356           roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config) {
[5270]357 size_t i;
[2886]358 int need_new = 1;
359
[2912]360 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
361
[2886]362 if ( codec < 0 || create < 0 )
363  return NULL;
364
[2912]365 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
366
[2886]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++) {
[5357]373   if ( config->codecs.codec[i].codec == (uint32_t)codec )
[2886]374    return &(config->codecs.codec[i]);
[5270]375   if ( config->codecs.codec[i].codec == (uint32_t)-1 )
[2886]376    need_new = 0;
377  }
378 }
379
[2912]380 ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config);
381
[2886]382 if ( !create )
383  return NULL;
384
385 if ( !need_new ) {
386  for (i = 0; i < config->codecs.num; i++) {
[5270]387   if ( config->codecs.codec[i].codec == (uint32_t)-1 ) {
[2886]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 ) {
[4957]396  config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec));
[2886]397 } else {
[4957]398  config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec));
[2886]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++) {
[5270]406  config->codecs.codec[i].codec = (uint32_t)-1;
[2886]407 }
408
409 i = config->codecs.num;
410 config->codecs.num += 16;
411
[2913]412 memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec));
413 config->codecs.codec[i].codec = codec;
414
[2886]415 return &(config->codecs.codec[i]);
416}
417
[5351]418int    roar_libroar_set_server(const char * server) {
[2567]419 roar_libroar_get_config_ptr()->server = server;
420 return 0;
421}
422
[5351]423const char * roar_libroar_get_server(void) {
[2567]424 return roar_libroar_get_config_ptr()->server;
425}
[2478]426
[5758]427int    roar_libroar_set_forkapi(const struct roar_libroar_forkapi * api) {
[5373]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
[3855]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
[5832]452static const struct pathinfo {
[5747]453 const char * name;
454 const char * path;
[5810]455 const int    with_product; // 0 = no, 1 = yes, 2 = just product.
[5747]456 const int    with_provider;
457} __paths[] = {
[5831]458 // prefixes:
[5812]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},
[5810]474 {"prefix-pc",          ROAR_PREFIX_PC, 0, 0},
475 {"prefix-ckport",      ROAR_PREFIX_CKPORT, 0, 0},
[5812]476 {"prefix-sysconf",     ROAR_PREFIX_SYSCONF, 2, 0},
[5810]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},
[5831]491 {"prefix-buildsystem", ROAR_PREFIX_BUILDSYSTEM, 0, 0},
492
493 // bins:
[5840]494 {"bin-default-daemonimage", "roard", 0, 0},
495
[5839]496#ifdef ROAR_HAVE_BIN_SH
[5831]497 {"bin-sh",             ROAR_HAVE_BIN_SH, 0, 0},
[5839]498#endif
499#ifdef ROAR_HAVE_BIN_OGG123
[5831]500 {"bin-ogg123",         ROAR_HAVE_BIN_OGG123, 0, 0},
[5839]501#endif
502#ifdef ROAR_HAVE_BIN_FLAC
[5831]503 {"bin-flac",           ROAR_HAVE_BIN_FLAC, 0, 0},
[5839]504#endif
505#ifdef ROAR_HAVE_BIN_TIMIDITY
[5831]506 {"bin-timidity",       ROAR_HAVE_BIN_TIMIDITY, 0, 0},
[5839]507#endif
508#ifdef ROAR_HAVE_BIN_CDPARANOIA
[5831]509 {"bin-cdparanoia",     ROAR_HAVE_BIN_CDPARANOIA, 0, 0},
[5839]510#endif
511#ifdef ROAR_HAVE_BIN_GNUPLOT
[5831]512 {"bin-gnuplot",        ROAR_HAVE_BIN_GNUPLOT, 0, 0},
[5839]513#endif
514#ifdef ROAR_HAVE_BIN_SSH
[5831]515 {"bin-ssh",            ROAR_HAVE_BIN_SSH, 0, 0},
[5839]516#endif
517#ifdef ROAR_HAVE_BIN_PINENTRY
[5831]518 {"bin-pinentry",       ROAR_HAVE_BIN_PINENTRY, 0, 0},
[5839]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
[5832]524 {"bin-gtk-led-askpass", ROAR_HAVE_BIN_GTK_LED_ASKPASS, 0, 0},
[5839]525#endif
526#ifdef ROAR_HAVE_BIN_X11_SSH_ASKPASS
[5832]527 {"bin-x11-ssh-askpass", ROAR_HAVE_BIN_X11_SSH_ASKPASS, 0, 0},
[5839]528#endif
529#ifdef ROAR_HAVE_BIN_GNOME_SSH_ASKPASS
[5832]530 {"bin-gnome-ssh-askpass", ROAR_HAVE_BIN_GNOME_SSH_ASKPASS, 0, 0},
[5839]531#endif
532#ifdef ROAR_HAVE_BIN_GPG
[5831]533 {"bin-gpg",            ROAR_HAVE_BIN_GPG, 0, 0},
[5839]534#endif
535#ifdef ROAR_HAVE_BIN_EJECT
[5831]536 {"bin-eject",          ROAR_HAVE_BIN_EJECT, 0, 0},
[5839]537#endif
[5831]538
539 // devices:
[5832]540 {"dev-stdin",                  ROAR_PREFIX_DEV "/stdin", 0, 0},
[5831]541 {"dev-default-pwmled",         ROAR_PREFIX_DEV "/ttyS0", 0, 0},
[5832]542 {"dev-default-dmx4linux",      ROAR_PREFIX_DEV "/dmx", 0, 0},
[5831]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
[5832]559 {"proc-net-decnet",            ROAR_PROC_NET_DECNET, 0, 0},
[5831]560#endif
561#ifdef ROAR_PROC_NET_DECNET_NEIGH
[5832]562 {"proc-net-decnet-neigh",      ROAR_PROC_NET_DECNET_NEIGH, 0, 0},
[5831]563#endif
564#ifdef ROAR_PROC_NET_ARP
[5832]565 {"proc-net-arp",               ROAR_PROC_NET_ARP, 0, 0},
[5831]566#endif
567
568 // sysconf:
[5832]569 {"sysconf-hosts",              ROAR_PREFIX_SYSCONF "/hosts", 0, 0},
570 {"sysconf-roarserver",         ROAR_PREFIX_SYSCONF "/roarserver", 0, 0},
[5831]571
572 // special dirs:
[5832]573 {"dir-nx-home",                "/NX-HOME-DIR", 0, 0}
[5747]574};
575
[5810]576static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product, int type) {
[5747]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 ) {
[5810]584   if ( type == 2 ) {
585    snprintf(path, pathlen, "/universal");
586   } else {
587    snprintf(path, pathlen, "/universal/universal");
588   }
[5747]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
[5810]608 if ( type == 2 ) {
609  snprintf(path, pathlen, "/%.*s", (int)(size_t)(b-product), product);
610 } else if ( s == NULL ) {
[5747]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
[5832]653static const struct pathinfo * __lookup_path(const char * name) {
[5747]654 size_t i;
[5830]655
[5747]656 if ( name == NULL ) {
[5830]657  roar_err_set(ROAR_ERROR_FAULT);
[5747]658  return NULL;
659 }
660
[5832]661 for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++)
662  if ( !strcmp(__paths[i].name, name) )
663   return &(__paths[i]);
[5747]664
665 roar_err_set(ROAR_ERROR_NOENT);
666 return NULL;
667}
668
[5833]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
[5832]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
[5833]725 __strip_double_slashes(ret);
726
[5832]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
[5828]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
[5830]744 ROAR_DBG("roar_libroar_list_path(list=%p, len=%lu, offset=%lu) = ?", list, (long unsigned int)len, (long unsigned int)offset);
745
[5828]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
[5830]757 for (i = offset; idx < len && i < (sizeof(__paths)/sizeof(*__paths)); i++) {
[5828]758  list[idx++] = __paths[i].name;
759 }
760
761 return idx;
762}
763
[2477]764//ll
Note: See TracBrowser for help on using the repository browser.