| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
#ifdef HAVE_CONFIG_H |
|---|
| 31 |
# include "config.h" |
|---|
| 32 |
#endif |
|---|
| 33 |
|
|---|
| 34 |
#include <vlc_common.h> |
|---|
| 35 |
#include "libvlc.h" |
|---|
| 36 |
|
|---|
| 37 |
#include "vlc_interface.h" |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
static bool IsEscapeNeeded( char c ) |
|---|
| 43 |
{ |
|---|
| 44 |
return c == '\'' || c == '"' || c == '\\'; |
|---|
| 45 |
} |
|---|
| 46 |
static bool IsEscape( const char *psz ) |
|---|
| 47 |
{ |
|---|
| 48 |
if( !psz ) |
|---|
| 49 |
return false; |
|---|
| 50 |
return psz[0] == '\\' && IsEscapeNeeded( psz[1] ); |
|---|
| 51 |
} |
|---|
| 52 |
static bool IsSpace( char c ) |
|---|
| 53 |
{ |
|---|
| 54 |
return c == ' ' || c == '\t'; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
#define SKIPSPACE( p ) do { while( *p && IsSpace( *p ) ) p++; } while(0) |
|---|
| 58 |
#define SKIPTRAILINGSPACE( p, e ) \ |
|---|
| 59 |
do { while( e > p && IsSpace( *(e-1) ) ) e--; } while(0) |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
static const char *ChainGetEnd( const char *psz_string ) |
|---|
| 69 |
{ |
|---|
| 70 |
const char *p = psz_string; |
|---|
| 71 |
char c; |
|---|
| 72 |
|
|---|
| 73 |
if( !psz_string ) |
|---|
| 74 |
return NULL; |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
SKIPSPACE( p ); |
|---|
| 78 |
|
|---|
| 79 |
for( ;; p++) |
|---|
| 80 |
{ |
|---|
| 81 |
if( *p == '\0' || *p == ',' || *p == '}' ) |
|---|
| 82 |
return p; |
|---|
| 83 |
|
|---|
| 84 |
if( *p == '{' || *p == '"' || *p == '\'' ) |
|---|
| 85 |
break; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
if( *p == '{' ) |
|---|
| 90 |
c = '}'; |
|---|
| 91 |
else |
|---|
| 92 |
c = *p; |
|---|
| 93 |
p++; |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
for( ;; ) |
|---|
| 97 |
{ |
|---|
| 98 |
if( *p == '\0') |
|---|
| 99 |
return p; |
|---|
| 100 |
|
|---|
| 101 |
if( IsEscape( p ) ) |
|---|
| 102 |
p += 2; |
|---|
| 103 |
else if( *p == c ) |
|---|
| 104 |
return ++p; |
|---|
| 105 |
else if( *p == '{' && c == '}' ) |
|---|
| 106 |
p = ChainGetEnd( p ); |
|---|
| 107 |
else |
|---|
| 108 |
p++; |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
static char *ChainGetValue( const char **ppsz_string ) |
|---|
| 117 |
{ |
|---|
| 118 |
const char *p = *ppsz_string; |
|---|
| 119 |
|
|---|
| 120 |
char *psz_value = NULL; |
|---|
| 121 |
const char *end; |
|---|
| 122 |
bool b_keep_brackets = (*p == '{'); |
|---|
| 123 |
|
|---|
| 124 |
if( *p == '=' ) |
|---|
| 125 |
p++; |
|---|
| 126 |
|
|---|
| 127 |
end = ChainGetEnd( p ); |
|---|
| 128 |
if( end <= p ) |
|---|
| 129 |
{ |
|---|
| 130 |
psz_value = NULL; |
|---|
| 131 |
} |
|---|
| 132 |
else |
|---|
| 133 |
{ |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
SKIPSPACE( p ); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
if( end <= p ) |
|---|
| 141 |
{ |
|---|
| 142 |
psz_value = NULL; |
|---|
| 143 |
} |
|---|
| 144 |
else |
|---|
| 145 |
{ |
|---|
| 146 |
if( *p == '\'' || *p == '"' || ( !b_keep_brackets && *p == '{' ) ) |
|---|
| 147 |
{ |
|---|
| 148 |
p++; |
|---|
| 149 |
|
|---|
| 150 |
if( *(end-1) != '\'' && *(end-1) == '"' ) |
|---|
| 151 |
SKIPTRAILINGSPACE( p, end ); |
|---|
| 152 |
|
|---|
| 153 |
if( end - 1 <= p ) |
|---|
| 154 |
psz_value = NULL; |
|---|
| 155 |
else |
|---|
| 156 |
psz_value = strndup( p, end -1 - p ); |
|---|
| 157 |
} |
|---|
| 158 |
else |
|---|
| 159 |
{ |
|---|
| 160 |
SKIPTRAILINGSPACE( p, end ); |
|---|
| 161 |
if( end <= p ) |
|---|
| 162 |
psz_value = NULL; |
|---|
| 163 |
else |
|---|
| 164 |
psz_value = strndup( p, end - p ); |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
if( psz_value ) |
|---|
| 170 |
config_StringUnescape( psz_value ); |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
*ppsz_string = end; |
|---|
| 174 |
return psz_value; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg, const char *psz_chain ) |
|---|
| 178 |
{ |
|---|
| 179 |
config_chain_t *p_cfg = NULL; |
|---|
| 180 |
const char *p = psz_chain; |
|---|
| 181 |
|
|---|
| 182 |
*ppsz_name = NULL; |
|---|
| 183 |
*pp_cfg = NULL; |
|---|
| 184 |
|
|---|
| 185 |
if( !p ) |
|---|
| 186 |
return NULL; |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
SKIPSPACE( p ); |
|---|
| 190 |
|
|---|
| 191 |
while( *p && *p != '{' && *p != ':' && !IsSpace( *p ) ) |
|---|
| 192 |
p++; |
|---|
| 193 |
|
|---|
| 194 |
if( p == psz_chain ) |
|---|
| 195 |
return NULL; |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
*ppsz_name = strndup( psz_chain, p - psz_chain ); |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
SKIPSPACE( p ); |
|---|
| 202 |
|
|---|
| 203 |
if( *p == '{' ) |
|---|
| 204 |
{ |
|---|
| 205 |
const char *psz_name; |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
p++; |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
for( ;; ) |
|---|
| 212 |
{ |
|---|
| 213 |
SKIPSPACE( p ); |
|---|
| 214 |
|
|---|
| 215 |
psz_name = p; |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' && !IsSpace( *p ) ) |
|---|
| 219 |
p++; |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
if( p == psz_name ) |
|---|
| 223 |
{ |
|---|
| 224 |
fprintf( stderr, "config_ChainCreate: invalid options (empty) \n" ); |
|---|
| 225 |
break; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
config_chain_t cfg; |
|---|
| 230 |
cfg.psz_name = strndup( psz_name, p - psz_name ); |
|---|
| 231 |
cfg.psz_value = NULL; |
|---|
| 232 |
cfg.p_next = NULL; |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
SKIPSPACE( p ); |
|---|
| 236 |
|
|---|
| 237 |
if( *p == '=' || *p == '{' ) |
|---|
| 238 |
{ |
|---|
| 239 |
cfg.psz_value = ChainGetValue( &p ); |
|---|
| 240 |
|
|---|
| 241 |
SKIPSPACE( p ); |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
config_chain_t *p_new = malloc( sizeof(*p_new) ); |
|---|
| 246 |
if( !p_new ) |
|---|
| 247 |
{ |
|---|
| 248 |
free( cfg.psz_name ); |
|---|
| 249 |
free( cfg.psz_value ); |
|---|
| 250 |
break; |
|---|
| 251 |
} |
|---|
| 252 |
*p_new = cfg; |
|---|
| 253 |
|
|---|
| 254 |
if( p_cfg ) |
|---|
| 255 |
{ |
|---|
| 256 |
p_cfg->p_next = p_new; |
|---|
| 257 |
p_cfg = p_cfg->p_next; |
|---|
| 258 |
} |
|---|
| 259 |
else |
|---|
| 260 |
{ |
|---|
| 261 |
*pp_cfg = p_cfg = p_new; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
if( *p == ',' ) |
|---|
| 266 |
p++; |
|---|
| 267 |
|
|---|
| 268 |
if( *p == '}' ) |
|---|
| 269 |
{ |
|---|
| 270 |
p++; |
|---|
| 271 |
break; |
|---|
| 272 |
} |
|---|
| 273 |
} |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
if( *p == ':' ) |
|---|
| 277 |
return strdup( &p[1] ); |
|---|
| 278 |
|
|---|
| 279 |
return NULL; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
void config_ChainDestroy( config_chain_t *p_cfg ) |
|---|
| 283 |
{ |
|---|
| 284 |
while( p_cfg != NULL ) |
|---|
| 285 |
{ |
|---|
| 286 |
config_chain_t *p_next; |
|---|
| 287 |
|
|---|
| 288 |
p_next = p_cfg->p_next; |
|---|
| 289 |
|
|---|
| 290 |
FREENULL( p_cfg->psz_name ); |
|---|
| 291 |
FREENULL( p_cfg->psz_value ); |
|---|
| 292 |
free( p_cfg ); |
|---|
| 293 |
|
|---|
| 294 |
p_cfg = p_next; |
|---|
| 295 |
} |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix, |
|---|
| 299 |
const char *const *ppsz_options, config_chain_t *cfg ) |
|---|
| 300 |
{ |
|---|
| 301 |
if( psz_prefix == NULL ) psz_prefix = ""; |
|---|
| 302 |
size_t plen = 1 + strlen( psz_prefix ); |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
for( size_t i = 0; ppsz_options[i] != NULL; i++ ) |
|---|
| 306 |
{ |
|---|
| 307 |
const char *optname = ppsz_options[i]; |
|---|
| 308 |
if (optname[0] == '*') |
|---|
| 309 |
optname++; |
|---|
| 310 |
|
|---|
| 311 |
char name[plen + strlen( optname )]; |
|---|
| 312 |
snprintf( name, sizeof (name), "%s%s", psz_prefix, optname ); |
|---|
| 313 |
if( var_Create( p_this, name, |
|---|
| 314 |
config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) ) |
|---|
| 315 |
return ; |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
for(; cfg; cfg = cfg->p_next ) |
|---|
| 320 |
{ |
|---|
| 321 |
vlc_value_t val; |
|---|
| 322 |
bool b_yes = true; |
|---|
| 323 |
bool b_once = false; |
|---|
| 324 |
module_config_t *p_conf; |
|---|
| 325 |
int i_type; |
|---|
| 326 |
size_t i; |
|---|
| 327 |
|
|---|
| 328 |
if( cfg->psz_name == NULL || *cfg->psz_name == '\0' ) |
|---|
| 329 |
continue; |
|---|
| 330 |
|
|---|
| 331 |
for( i = 0; ppsz_options[i] != NULL; i++ ) |
|---|
| 332 |
{ |
|---|
| 333 |
if( !strcmp( ppsz_options[i], cfg->psz_name ) ) |
|---|
| 334 |
{ |
|---|
| 335 |
break; |
|---|
| 336 |
} |
|---|
| 337 |
if( ( !strncmp( cfg->psz_name, "no-", 3 ) && |
|---|
| 338 |
!strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) || |
|---|
| 339 |
( !strncmp( cfg->psz_name, "no", 2 ) && |
|---|
| 340 |
!strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) ) |
|---|
| 341 |
{ |
|---|
| 342 |
b_yes = false; |
|---|
| 343 |
break; |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
if( *ppsz_options[i] == '*' && |
|---|
| 347 |
!strcmp( &ppsz_options[i][1], cfg->psz_name ) ) |
|---|
| 348 |
{ |
|---|
| 349 |
b_once = true; |
|---|
| 350 |
break; |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
if( ppsz_options[i] == NULL ) |
|---|
| 356 |
{ |
|---|
| 357 |
msg_Warn( p_this, "option %s is unknown", cfg->psz_name ); |
|---|
| 358 |
continue; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
char name[plen + strlen( ppsz_options[i] )]; |
|---|
| 363 |
const char *psz_name = name; |
|---|
| 364 |
snprintf( name, sizeof (name), "%s%s", psz_prefix, |
|---|
| 365 |
b_once ? (ppsz_options[i] + 1) : ppsz_options[i] ); |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
p_conf = config_FindConfig( p_this, name ); |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
if( p_conf ) |
|---|
| 373 |
{ |
|---|
| 374 |
if( p_conf->b_removed ) |
|---|
| 375 |
{ |
|---|
| 376 |
msg_Err( p_this, "Option %s is not supported anymore.", |
|---|
| 377 |
name ); |
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
continue; |
|---|
| 382 |
} |
|---|
| 383 |
if( p_conf->psz_oldname |
|---|
| 384 |
&& !strcmp( p_conf->psz_oldname, name ) ) |
|---|
| 385 |
{ |
|---|
| 386 |
psz_name = p_conf->psz_name; |
|---|
| 387 |
msg_Warn( p_this, "Option %s is obsolete. Use %s instead.", |
|---|
| 388 |
name, psz_name ); |
|---|
| 389 |
} |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
i_type = config_GetType( p_this, psz_name ); |
|---|
| 395 |
if( !i_type ) |
|---|
| 396 |
{ |
|---|
| 397 |
msg_Warn( p_this, "unknown option %s (value=%s)", |
|---|
| 398 |
cfg->psz_name, cfg->psz_value ); |
|---|
| 399 |
continue; |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
i_type &= CONFIG_ITEM; |
|---|
| 403 |
|
|---|
| 404 |
if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL ) |
|---|
| 405 |
{ |
|---|
| 406 |
msg_Warn( p_this, "missing value for option %s", cfg->psz_name ); |
|---|
| 407 |
continue; |
|---|
| 408 |
} |
|---|
| 409 |
if( i_type != VLC_VAR_STRING && b_once ) |
|---|
| 410 |
{ |
|---|
| 411 |
msg_Warn( p_this, "*option_name need to be a string option" ); |
|---|
| 412 |
continue; |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
switch( i_type ) |
|---|
| 416 |
{ |
|---|
| 417 |
case VLC_VAR_BOOL: |
|---|
| 418 |
val.b_bool = b_yes; |
|---|
| 419 |
break; |
|---|
| 420 |
case VLC_VAR_INTEGER: |
|---|
| 421 |
val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0", |
|---|
| 422 |
NULL, 0 ); |
|---|
| 423 |
break; |
|---|
| 424 |
case VLC_VAR_FLOAT: |
|---|
| 425 |
val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" ); |
|---|
| 426 |
break; |
|---|
| 427 |
case VLC_VAR_STRING: |
|---|
| 428 |
case VLC_VAR_MODULE: |
|---|
| 429 |
val.psz_string = cfg->psz_value; |
|---|
| 430 |
break; |
|---|
| 431 |
default: |
|---|
| 432 |
msg_Warn( p_this, "unhandled config var type (%d)", i_type ); |
|---|
| 433 |
memset( &val, 0, sizeof( vlc_value_t ) ); |
|---|
| 434 |
break; |
|---|
| 435 |
} |
|---|
| 436 |
if( b_once ) |
|---|
| 437 |
{ |
|---|
| 438 |
vlc_value_t val2; |
|---|
| 439 |
|
|---|
| 440 |
var_Get( p_this, psz_name, &val2 ); |
|---|
| 441 |
if( *val2.psz_string ) |
|---|
| 442 |
{ |
|---|
| 443 |
free( val2.psz_string ); |
|---|
| 444 |
msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name ); |
|---|
| 445 |
continue; |
|---|
| 446 |
} |
|---|
| 447 |
free( val2.psz_string ); |
|---|
| 448 |
} |
|---|
| 449 |
var_Set( p_this, psz_name, val ); |
|---|
| 450 |
msg_Dbg( p_this, "set config option: %s to %s", psz_name, |
|---|
| 451 |
cfg->psz_value ? cfg->psz_value : "(null)" ); |
|---|
| 452 |
} |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
char *config_StringUnescape( char *psz_string ) |
|---|
| 456 |
{ |
|---|
| 457 |
char *psz_src = psz_string; |
|---|
| 458 |
char *psz_dst = psz_string; |
|---|
| 459 |
if( !psz_src ) |
|---|
| 460 |
return NULL; |
|---|
| 461 |
|
|---|
| 462 |
while( *psz_src ) |
|---|
| 463 |
{ |
|---|
| 464 |
if( IsEscape( psz_src ) ) |
|---|
| 465 |
psz_src++; |
|---|
| 466 |
*psz_dst++ = *psz_src++; |
|---|
| 467 |
} |
|---|
| 468 |
*psz_dst = '\0'; |
|---|
| 469 |
|
|---|
| 470 |
return psz_string; |
|---|
| 471 |
} |
|---|
| 472 |
|
|---|
| 473 |
char *config_StringEscape( const char *psz_string ) |
|---|
| 474 |
{ |
|---|
| 475 |
char *psz_return; |
|---|
| 476 |
char *psz_dst; |
|---|
| 477 |
int i_escape; |
|---|
| 478 |
|
|---|
| 479 |
if( !psz_string ) |
|---|
| 480 |
return NULL; |
|---|
| 481 |
|
|---|
| 482 |
i_escape = 0; |
|---|
| 483 |
for( const char *p = psz_string; *p; p++ ) |
|---|
| 484 |
{ |
|---|
| 485 |
if( IsEscapeNeeded( *p ) ) |
|---|
| 486 |
i_escape++; |
|---|
| 487 |
} |
|---|
| 488 |
|
|---|
| 489 |
psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 ); |
|---|
| 490 |
for( const char *p = psz_string; *p; p++ ) |
|---|
| 491 |
{ |
|---|
| 492 |
if( IsEscapeNeeded( *p ) ) |
|---|
| 493 |
*psz_dst++ = '\\'; |
|---|
| 494 |
*psz_dst++ = *p; |
|---|
| 495 |
} |
|---|
| 496 |
*psz_dst = '\0'; |
|---|
| 497 |
|
|---|
| 498 |
return psz_return; |
|---|
| 499 |
} |
|---|
| 500 |
|
|---|
| 501 |
|
|---|