[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 | |
---|
[2895] | 38 | static struct roar_libroar_config_codec * |
---|
[5357] | 39 | roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config); |
---|
[2895] | 40 | |
---|
| 41 | |
---|
[2478] | 42 | struct roar_libroar_config * roar_libroar_get_config_ptr(void) { |
---|
| 43 | static struct roar_libroar_config config; |
---|
[3218] | 44 | static int inited = 0; |
---|
| 45 | static char authfile[1024]; |
---|
[4517] | 46 | const char * home = roar_env_get_home(0); |
---|
[2478] | 47 | |
---|
| 48 | if ( !inited ) { |
---|
| 49 | memset(&config, 0, sizeof(config)); |
---|
[2567] | 50 | |
---|
[4779] | 51 | #ifdef ROAR_SUPPORT_TRAP |
---|
[5373] | 52 | config.trap_policy = ROAR_TRAP_IGNORE; |
---|
[4779] | 53 | #endif |
---|
[5373] | 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; |
---|
[5465] | 60 | config.protocolversion = -1; // use default. |
---|
[3218] | 61 | |
---|
| 62 | if ( home != NULL ) { |
---|
[5373] | 63 | snprintf(authfile, sizeof(authfile)-1, "%s/.roarauth", home); |
---|
| 64 | authfile[sizeof(authfile)-1] = 0; |
---|
[3375] | 65 | config.authfile = authfile; |
---|
[4348] | 66 | config.serversfile = NULL; |
---|
[3218] | 67 | } |
---|
[2567] | 68 | |
---|
[2478] | 69 | inited++; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | return &config; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | struct roar_libroar_config * roar_libroar_get_config(void) { |
---|
| 76 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
| 77 | static int inited = 0; |
---|
[5754] | 78 | const char * next; |
---|
| 79 | char * buf; |
---|
[2478] | 80 | |
---|
| 81 | if ( !inited ) { |
---|
[5201] | 82 | inited++; // we do this early so we can use ROAR_{DBG,INFO,WARN,ERR}() in roar_libroar_config_parse(). |
---|
| 83 | |
---|
[5754] | 84 | next = roar_env_get("ROAR_OPTIONS"); |
---|
[2480] | 85 | |
---|
[2884] | 86 | if ( next != NULL ) { |
---|
[5754] | 87 | if ( (buf = roar_mm_strdup(next)) != NULL ) { |
---|
| 88 | roar_libroar_config_parse(buf, " "); |
---|
| 89 | roar_mm_free(buf); |
---|
| 90 | } |
---|
[2480] | 91 | } |
---|
[2478] | 92 | } |
---|
| 93 | |
---|
| 94 | return config; |
---|
| 95 | } |
---|
| 96 | |
---|
[5010] | 97 | int 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 | |
---|
[2890] | 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 | |
---|
| 112 | static int roar_libroar_config_parse_codec(struct roar_libroar_config * config, char * txt) { |
---|
| 113 | struct roar_libroar_config_codec * codec_cfg; |
---|
[5357] | 114 | int32_t codec; |
---|
[2890] | 115 | char * codec_str, * option_str, * value_str; |
---|
[5006] | 116 | char * toksave = NULL; |
---|
[2890] | 117 | |
---|
[2912] | 118 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 119 | |
---|
[2890] | 120 | if ( config == NULL || txt == NULL ) |
---|
| 121 | return -1; |
---|
| 122 | |
---|
[2912] | 123 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 124 | |
---|
[5006] | 125 | codec_str = roar_mm_strtok_r(txt, ":", &toksave); |
---|
[2890] | 126 | |
---|
[2892] | 127 | if ( codec_str == NULL ) |
---|
| 128 | return -1; |
---|
| 129 | |
---|
[5006] | 130 | option_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
[2890] | 131 | |
---|
| 132 | if ( option_str == NULL ) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
[5006] | 135 | value_str = roar_mm_strtok_r(NULL, ":", &toksave); |
---|
[2890] | 136 | |
---|
| 137 | if ( value_str == NULL ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
[2912] | 140 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s') = ?", config, txt); |
---|
| 141 | |
---|
[2890] | 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 | |
---|
[2912] | 147 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i", config, txt, codec); |
---|
| 148 | |
---|
[2893] | 149 | if ( (codec_cfg = roar_libroar_config_codec_get_conf(codec, 1, config)) == NULL ) |
---|
[2890] | 150 | return -1; |
---|
| 151 | |
---|
[2912] | 152 | ROAR_DBG("roar_libroar_config_parse_codec(config=%p, txt='%s'): codec=%i, codec_cfg=%p", config, txt, codec, codec_cfg); |
---|
| 153 | |
---|
[2890] | 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); |
---|
[2926] | 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); |
---|
[2962] | 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 | } |
---|
[2890] | 183 | } else { |
---|
[2891] | 184 | ROAR_WARN("roar_libroar_config_parse_codec(*): Unknown codec option: %s", option_str); |
---|
[2892] | 185 | return -1; |
---|
[2890] | 186 | } |
---|
| 187 | |
---|
[2892] | 188 | return 0; |
---|
[2890] | 189 | } |
---|
| 190 | |
---|
[2884] | 191 | int roar_libroar_config_parse(char * txt, char * delm) { |
---|
| 192 | struct roar_libroar_config * config = roar_libroar_get_config_ptr(); |
---|
[5006] | 193 | ssize_t len; |
---|
[2884] | 194 | char * k, * v, * next = txt; |
---|
| 195 | |
---|
| 196 | while (next != NULL) { |
---|
| 197 | k = next; |
---|
[2886] | 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 | |
---|
[2884] | 206 | if ( next != NULL ) { |
---|
| 207 | *next = 0; |
---|
| 208 | next++; |
---|
| 209 | } |
---|
| 210 | |
---|
[2888] | 211 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 212 | |
---|
[2887] | 213 | // strip leading spaces: |
---|
| 214 | while ( *k == ' ' ) k++; |
---|
| 215 | |
---|
[2888] | 216 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 217 | |
---|
[2887] | 218 | // strip tailing new lions: |
---|
[5006] | 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; |
---|
[2887] | 223 | |
---|
[2888] | 224 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 225 | |
---|
[2887] | 226 | // comments |
---|
| 227 | if ( *k == '#' ) |
---|
| 228 | continue; |
---|
| 229 | |
---|
[2888] | 230 | ROAR_DBG("roar_libroar_config_parse(*): k='%s'", k); |
---|
| 231 | |
---|
[2887] | 232 | // empty options: |
---|
| 233 | if ( *k == 0 ) |
---|
| 234 | continue; |
---|
| 235 | |
---|
[2884] | 236 | if ( (v = strstr(k, ":")) != NULL ) { |
---|
| 237 | *v = 0; |
---|
| 238 | v++; |
---|
| 239 | } |
---|
| 240 | |
---|
[2888] | 241 | ROAR_DBG("roar_libroar_config_parse(*): k='%s', v='%s'", k, v); |
---|
| 242 | |
---|
[2884] | 243 | if ( !strcmp(k, "workaround") ) { |
---|
| 244 | if ( !strcmp(v, "use-execed") ) { |
---|
| 245 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_USE_EXECED; |
---|
[4653] | 246 | } else if ( !strcmp(v, "no-slp") ) { |
---|
| 247 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
[2884] | 248 | } else { |
---|
[2888] | 249 | ROAR_WARN("roar_libroar_config_parse(*): Unknown workaround option: %s", v); |
---|
[2884] | 250 | } |
---|
| 251 | } else if ( !strcmp(k, "warning") || !strcmp(k, "warn") ) { |
---|
| 252 | if ( !strcmp(v, "sysio") ) { |
---|
| 253 | config->warnings.sysio = ROAR_WARNING_ALWAYS; |
---|
[3855] | 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; |
---|
[2884] | 259 | } else { |
---|
[2888] | 260 | ROAR_WARN("roar_libroar_config_parse(*): Unknown warning option: %s", v); |
---|
[2884] | 261 | } |
---|
[5183] | 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; |
---|
[5202] | 267 | } else if ( !strcmp(v, "ms") ) { |
---|
| 268 | config->opmode = ROAR_LIBROAR_CONFIG_OPMODE_MS; |
---|
[5183] | 269 | } else { |
---|
| 270 | ROAR_WARN("roar_libroar_config_parse(*): Unknown opmode: %s", v); |
---|
| 271 | } |
---|
[4779] | 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 |
---|
[4784] | 288 | } else if ( !strcmp(v, "die") ) { |
---|
| 289 | config->trap_policy = ROAR_TRAP_DIE; |
---|
[4779] | 290 | } else { |
---|
| 291 | ROAR_WARN("roar_libroar_config_parse(*): Unknown trap policy: %s", v); |
---|
| 292 | } |
---|
| 293 | #endif |
---|
[3136] | 294 | } else if ( !strcmp(k, "force-rate") ) { |
---|
[5466] | 295 | config->info.rate = roar_str2rate(v); |
---|
[3136] | 296 | } else if ( !strcmp(k, "force-bits") ) { |
---|
[5466] | 297 | config->info.bits = roar_str2bits(v); |
---|
[3136] | 298 | } else if ( !strcmp(k, "force-channels") ) { |
---|
[5466] | 299 | config->info.channels = roar_str2channels(v); |
---|
[3136] | 300 | } else if ( !strcmp(k, "force-codec") ) { |
---|
| 301 | config->info.codec = roar_str2codec(v); |
---|
[2890] | 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 | } |
---|
[2896] | 306 | } else if ( !strcmp(k, "set-server") ) { |
---|
[2899] | 307 | if ( roar_libroar_get_server() == NULL ) |
---|
| 308 | roar_libroar_set_server(v); |
---|
[3218] | 309 | } else if ( !strcmp(k, "set-authfile") ) { |
---|
[3219] | 310 | strncpy(config->authfile, v, 1023); |
---|
| 311 | config->authfile[1023] = 0; |
---|
[3375] | 312 | } else if ( !strcmp(k, "x11-display") ) { |
---|
| 313 | config->x11.display = v; |
---|
[5373] | 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 | } |
---|
[5465] | 326 | } else if ( !strcmp(k, "protocolversion") ) { |
---|
| 327 | config->protocolversion = atoi(v); |
---|
[2884] | 328 | } else { |
---|
[2888] | 329 | ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); |
---|
[2884] | 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | return 0; |
---|
| 334 | } |
---|
| 335 | |
---|
[5357] | 336 | struct roar_libroar_config_codec * roar_libroar_config_codec_get(int32_t codec, int create) { |
---|
[2893] | 337 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 338 | return roar_libroar_config_codec_get_conf(codec, create, config); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | static struct roar_libroar_config_codec * |
---|
[5357] | 342 | roar_libroar_config_codec_get_conf(int32_t codec, int create, struct roar_libroar_config * config) { |
---|
[5270] | 343 | size_t i; |
---|
[2886] | 344 | int need_new = 1; |
---|
| 345 | |
---|
[2912] | 346 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 347 | |
---|
[2886] | 348 | if ( codec < 0 || create < 0 ) |
---|
| 349 | return NULL; |
---|
| 350 | |
---|
[2912] | 351 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 352 | |
---|
[2886] | 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++) { |
---|
[5357] | 359 | if ( config->codecs.codec[i].codec == (uint32_t)codec ) |
---|
[2886] | 360 | return &(config->codecs.codec[i]); |
---|
[5270] | 361 | if ( config->codecs.codec[i].codec == (uint32_t)-1 ) |
---|
[2886] | 362 | need_new = 0; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
[2912] | 366 | ROAR_DBG("roar_libroar_config_codec_get_conf(codec=%i, create=%i, config=%p) = ?", codec, create, config); |
---|
| 367 | |
---|
[2886] | 368 | if ( !create ) |
---|
| 369 | return NULL; |
---|
| 370 | |
---|
| 371 | if ( !need_new ) { |
---|
| 372 | for (i = 0; i < config->codecs.num; i++) { |
---|
[5270] | 373 | if ( config->codecs.codec[i].codec == (uint32_t)-1 ) { |
---|
[2886] | 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 ) { |
---|
[4957] | 382 | config->codecs.codec = roar_mm_malloc(16*sizeof(struct roar_libroar_config_codec)); |
---|
[2886] | 383 | } else { |
---|
[4957] | 384 | config->codecs.codec = roar_mm_realloc(config->codecs.codec, (config->codecs.num+16)*sizeof(struct roar_libroar_config_codec)); |
---|
[2886] | 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++) { |
---|
[5270] | 392 | config->codecs.codec[i].codec = (uint32_t)-1; |
---|
[2886] | 393 | } |
---|
| 394 | |
---|
| 395 | i = config->codecs.num; |
---|
| 396 | config->codecs.num += 16; |
---|
| 397 | |
---|
[2913] | 398 | memset(&(config->codecs.codec[i]), 0, sizeof(struct roar_libroar_config_codec)); |
---|
| 399 | config->codecs.codec[i].codec = codec; |
---|
| 400 | |
---|
[2886] | 401 | return &(config->codecs.codec[i]); |
---|
| 402 | } |
---|
| 403 | |
---|
[5351] | 404 | int roar_libroar_set_server(const char * server) { |
---|
[2567] | 405 | roar_libroar_get_config_ptr()->server = server; |
---|
| 406 | return 0; |
---|
| 407 | } |
---|
| 408 | |
---|
[5351] | 409 | const char * roar_libroar_get_server(void) { |
---|
[2567] | 410 | return roar_libroar_get_config_ptr()->server; |
---|
| 411 | } |
---|
[2478] | 412 | |
---|
[5758] | 413 | int roar_libroar_set_forkapi(const struct roar_libroar_forkapi * api) { |
---|
[5373] | 414 | roar_libroar_get_config_ptr()->forkapi = api; |
---|
| 415 | return 0; |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | 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)) { |
---|
| 419 | roar_libroar_get_config_ptr()->connect_internal = func; |
---|
| 420 | return 0; |
---|
| 421 | } |
---|
| 422 | |
---|
[3855] | 423 | void roar_libroar_nowarn(void) { |
---|
| 424 | roar_libroar_get_config_ptr()->nowarncounter++; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | void 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 | |
---|
[5747] | 438 | static const struct { |
---|
| 439 | const char * name; |
---|
| 440 | const char * path; |
---|
[5810] | 441 | const int with_product; // 0 = no, 1 = yes, 2 = just product. |
---|
[5747] | 442 | const int with_provider; |
---|
| 443 | } __paths[] = { |
---|
[5812] | 444 | {"prefix", ROAR_PREFIX, 0, 0}, |
---|
| 445 | {"prefix-bin", ROAR_PREFIX_BIN, 0, 0}, |
---|
| 446 | {"prefix-sbin", ROAR_PREFIX_SBIN, 0, 0}, |
---|
| 447 | {"prefix-lib", ROAR_PREFIX_LIB, 0, 0}, |
---|
| 448 | {"prefix-inc", ROAR_PREFIX_INC, 0, 0}, |
---|
| 449 | {"prefix-man", ROAR_PREFIX_MAN, 0, 0}, |
---|
| 450 | {"prefix-man1", ROAR_PREFIX_MAN "/man1", 0, 0}, |
---|
| 451 | {"prefix-man2", ROAR_PREFIX_MAN "/man2", 0, 0}, |
---|
| 452 | {"prefix-man3", ROAR_PREFIX_MAN "/man3", 0, 0}, |
---|
| 453 | {"prefix-man4", ROAR_PREFIX_MAN "/man4", 0, 0}, |
---|
| 454 | {"prefix-man5", ROAR_PREFIX_MAN "/man5", 0, 0}, |
---|
| 455 | {"prefix-man6", ROAR_PREFIX_MAN "/man6", 0, 0}, |
---|
| 456 | {"prefix-man7", ROAR_PREFIX_MAN "/man7", 0, 0}, |
---|
| 457 | {"prefix-man8", ROAR_PREFIX_MAN "/man8", 0, 0}, |
---|
| 458 | {"prefix-man9", ROAR_PREFIX_MAN "/man9", 0, 0}, |
---|
[5810] | 459 | {"prefix-pc", ROAR_PREFIX_PC, 0, 0}, |
---|
| 460 | {"prefix-ckport", ROAR_PREFIX_CKPORT, 0, 0}, |
---|
[5812] | 461 | {"prefix-sysconf", ROAR_PREFIX_SYSCONF, 2, 0}, |
---|
[5810] | 462 | {"prefix-dev", ROAR_PREFIX_DEV, 0, 0}, |
---|
| 463 | {"prefix-doc", ROAR_PREFIX_DOC, 2, 0}, |
---|
| 464 | {"prefix-tmp", ROAR_PREFIX_TMP, 0, 0}, |
---|
| 465 | {"prefix-var", ROAR_PREFIX_VAR, 0, 0}, |
---|
| 466 | {"prefix-cache", ROAR_PREFIX_CACHE, 2, 0}, |
---|
| 467 | {"prefix-data", ROAR_PREFIX_DATA, 2, 0}, |
---|
| 468 | {"prefix-lock", ROAR_PREFIX_LOCK, 0, 0}, |
---|
| 469 | {"prefix-log", ROAR_PREFIX_LOG, 0, 0}, |
---|
| 470 | {"prefix-mail", ROAR_PREFIX_MAIL, 0, 0}, |
---|
| 471 | {"prefix-run", ROAR_PREFIX_RUN, 0, 0}, |
---|
| 472 | {"prefix-spool", ROAR_PREFIX_SPOOL, 2, 0}, |
---|
| 473 | {"prefix-comp-libs", ROAR_PREFIX_COMP_LIBS, 0, 0}, |
---|
| 474 | {"prefix-comp-bins", ROAR_PREFIX_COMP_BINS, 0, 0}, |
---|
| 475 | {"prefix-plugins", ROAR_PREFIX_PLUGINS, 1, 1}, |
---|
[5747] | 476 | {"prefix-buildsystem", ROAR_PREFIX_BUILDSYSTEM, 0, 0} |
---|
| 477 | }; |
---|
| 478 | |
---|
[5810] | 479 | static int __product2path(char * path, size_t pathlen, int null_as_universal, const char * product, int type) { |
---|
[5747] | 480 | const char * c; |
---|
| 481 | const char * e; |
---|
| 482 | const char * s; |
---|
| 483 | const char * b; |
---|
| 484 | |
---|
| 485 | if ( product == NULL ) { |
---|
| 486 | if ( null_as_universal ) { |
---|
[5810] | 487 | if ( type == 2 ) { |
---|
| 488 | snprintf(path, pathlen, "/universal"); |
---|
| 489 | } else { |
---|
| 490 | snprintf(path, pathlen, "/universal/universal"); |
---|
| 491 | } |
---|
[5747] | 492 | } else { |
---|
| 493 | path[0] = 0; |
---|
| 494 | } |
---|
| 495 | return 0; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | b = strstr(product, " "); |
---|
| 499 | c = strstr(product, "<"); |
---|
| 500 | e = strstr(product, ">"); |
---|
| 501 | |
---|
| 502 | if ( b == NULL || c == NULL || e == NULL || c < b || e < c ) { |
---|
| 503 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
| 504 | return -1; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | c++; |
---|
| 508 | |
---|
| 509 | s = strstr(c, "/"); |
---|
| 510 | |
---|
[5810] | 511 | if ( type == 2 ) { |
---|
| 512 | snprintf(path, pathlen, "/%.*s", (int)(size_t)(b-product), product); |
---|
| 513 | } else if ( s == NULL ) { |
---|
[5747] | 514 | snprintf(path, pathlen, "/unreg-%.*s/%.*s", (int)(size_t)(e-c), c, (int)(size_t)(b-product), product); |
---|
| 515 | } else { |
---|
| 516 | 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); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | return 0; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | static int __provider2path(char * path, size_t pathlen, const char * provider) { |
---|
| 523 | const char * c; |
---|
| 524 | const char * e; |
---|
| 525 | const char * s; |
---|
| 526 | |
---|
| 527 | if ( provider == NULL ) { |
---|
| 528 | path[0] = 0; |
---|
| 529 | return 0; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | c = strstr(provider, "<"); |
---|
| 533 | e = strstr(provider, ">"); |
---|
| 534 | |
---|
| 535 | if ( c == NULL || e == NULL || e < c ) { |
---|
| 536 | roar_err_set(ROAR_ERROR_ILLSEQ); |
---|
| 537 | return -1; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | c++; |
---|
| 541 | |
---|
| 542 | s = strstr(c, "/"); |
---|
| 543 | |
---|
| 544 | if ( s == NULL ) { |
---|
| 545 | snprintf(path, pathlen, "/unreg-%.*s", (int)(size_t)(e-c), c); |
---|
| 546 | } else { |
---|
| 547 | snprintf(path, pathlen, "/%.*s-%.*s", (int)(size_t)(s-c), c, (int)(size_t)(e-s-1), s+1); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | return 0; |
---|
| 551 | |
---|
| 552 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 553 | return -1; |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | char * roar_libroar_get_path(const char * name, int null_as_universal, const char * product, const char * provider) { |
---|
[5812] | 557 | char buf_product[384]; |
---|
| 558 | char buf_provider[384]; |
---|
[5747] | 559 | ssize_t len_prefix, len_product, len_provider; |
---|
| 560 | size_t i; |
---|
| 561 | char * ret, * p; |
---|
| 562 | |
---|
| 563 | if ( name == NULL ) { |
---|
| 564 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 565 | return NULL; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | for (i = 0; i < (sizeof(__paths)/sizeof(*__paths)); i++) { |
---|
| 569 | if ( !!strcmp(__paths[i].name, name) ) |
---|
| 570 | continue; |
---|
| 571 | |
---|
| 572 | if ( ((null_as_universal || product != NULL) && !__paths[i].with_product) || |
---|
| 573 | (provider != NULL && !__paths[i].with_provider) ) { |
---|
| 574 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 575 | return NULL; |
---|
| 576 | } |
---|
| 577 | |
---|
[5810] | 578 | if ( __product2path(buf_product, sizeof(buf_product), null_as_universal, product, __paths[i].with_product) == -1 ) |
---|
[5747] | 579 | return NULL; |
---|
| 580 | |
---|
| 581 | if ( __provider2path(buf_provider, sizeof(buf_provider), provider) == -1 ) |
---|
| 582 | return NULL; |
---|
| 583 | |
---|
| 584 | len_prefix = roar_mm_strlen(__paths[i].path); |
---|
| 585 | len_product = roar_mm_strlen(buf_product); |
---|
| 586 | len_provider = roar_mm_strlen(buf_provider); |
---|
| 587 | |
---|
| 588 | p = ret = roar_mm_malloc(len_prefix+len_product+len_provider+1); |
---|
| 589 | if ( ret == NULL ) |
---|
| 590 | return NULL; |
---|
| 591 | |
---|
| 592 | memcpy(p, __paths[i].path, len_prefix); |
---|
| 593 | p += len_prefix; |
---|
| 594 | if ( p[-1] == '/' ) |
---|
| 595 | p--; |
---|
| 596 | memcpy(p, buf_product, len_product); |
---|
| 597 | p += len_product; |
---|
| 598 | memcpy(p, buf_provider, len_provider); |
---|
| 599 | p += len_provider; |
---|
| 600 | |
---|
| 601 | *p = 0; |
---|
| 602 | |
---|
| 603 | return ret; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 607 | return NULL; |
---|
| 608 | } |
---|
| 609 | |
---|
[2477] | 610 | //ll |
---|