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