1 /*****************************************************************************
2 * chain.c : configuration module chain parsing stuff
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Eric Petit <titer@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_charset.h>
38 #include "vlc_interface.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static bool IsEscapeNeeded( char c )
45 return c == '\'' || c == '"' || c == '\\';
47 static bool IsEscape( const char *psz )
51 return psz[0] == '\\' && IsEscapeNeeded( psz[1] );
53 static bool IsSpace( char c )
55 return c == ' ' || c == '\t';
58 #define SKIPSPACE( p ) p += strspn( p, " \t" )
59 #define SKIPTRAILINGSPACE( p, e ) \
60 do { while( e > p && IsSpace( *(e-1) ) ) e--; } while(0)
63 * This function will return a pointer after the end of a string element.
64 * It will search the closing element which is
65 * } for { (it will handle nested { ... })
69 static const char *ChainGetEnd( const char *psz_string )
71 const char *p = psz_string;
77 /* Look for a opening character */
82 if( *p == '\0' || *p == ',' || *p == '}' )
85 if( *p == '{' || *p == '"' || *p == '\'' )
89 /* Set c to the closing character */
96 /* Search the closing character, handle nested {..} */
106 else if( *p == '{' && c == '}' )
107 p = ChainGetEnd( p );
114 * It will extract an option value (=... or {...}).
115 * It will remove the initial = if present but keep the {}
117 static char *ChainGetValue( const char **ppsz_string )
119 const char *p = *ppsz_string;
121 char *psz_value = NULL;
123 bool b_keep_brackets = (*p == '{');
128 end = ChainGetEnd( p );
135 /* Skip heading and trailing spaces.
136 * This ain't necessary but will avoid simple
147 if( *p == '\'' || *p == '"' || ( !b_keep_brackets && *p == '{' ) )
151 if( *(end-1) != '\'' && *(end-1) == '"' )
152 SKIPTRAILINGSPACE( p, end );
157 psz_value = strndup( p, end -1 - p );
161 SKIPTRAILINGSPACE( p, end );
165 psz_value = strndup( p, end - p );
171 config_StringUnescape( psz_value );
178 char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg,
179 const char *psz_chain )
181 config_chain_t **pp_next = pp_cfg;
189 psz_chain += strspn( psz_chain, " \t" );
191 /* Look for parameter (a {...} or :...) or end of name (space or nul) */
192 len = strcspn( psz_chain, "{: \t" );
193 *ppsz_name = strndup( psz_chain, len );
196 /* Parse the parameters */
197 psz_chain += strspn( psz_chain, " \t" );
198 if( *psz_chain == '{' )
200 /* Parse all name=value[,] elements */
203 psz_chain++; /* skip previous delimiter */
204 psz_chain += strspn( psz_chain, " \t" );
206 /* Look for the end of the name (,={}_space_) */
207 len = strcspn( psz_chain, "=,{} \t" );
209 continue; /* ignore empty parameter */
211 /* Append the new parameter */
212 config_chain_t *p_cfg = malloc( sizeof(*p_cfg) );
215 p_cfg->psz_name = strndup( psz_chain, len );
217 p_cfg->psz_value = NULL;
218 p_cfg->p_next = NULL;
221 pp_next = &p_cfg->p_next;
223 /* Extract the option value */
224 psz_chain += strspn( psz_chain, " \t" );
225 if( strchr( "={", *psz_chain ) )
227 p_cfg->psz_value = ChainGetValue( &psz_chain );
228 psz_chain += strspn( psz_chain, " \t" );
231 while( !memchr( "}", *psz_chain, 2 ) );
233 if( *psz_chain ) psz_chain++; /* skip '}' */;
234 psz_chain += strspn( psz_chain, " \t" );
237 if( *psz_chain == ':' )
238 return strdup( psz_chain + 1 );
243 void config_ChainDestroy( config_chain_t *p_cfg )
245 while( p_cfg != NULL )
247 config_chain_t *p_next;
249 p_next = p_cfg->p_next;
251 FREENULL( p_cfg->psz_name );
252 FREENULL( p_cfg->psz_value );
259 void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
260 const char *const *ppsz_options, config_chain_t *cfg )
262 if( psz_prefix == NULL ) psz_prefix = "";
263 size_t plen = 1 + strlen( psz_prefix );
265 /* First, var_Create all variables */
266 for( size_t i = 0; ppsz_options[i] != NULL; i++ )
268 const char *optname = ppsz_options[i];
269 if (optname[0] == '*')
272 char name[plen + strlen( optname )];
273 snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
274 if( var_Create( p_this, name,
275 config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
276 return /* VLC_xxx */;
279 /* Now parse options and set value */
280 for(; cfg; cfg = cfg->p_next )
285 module_config_t *p_conf;
289 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
292 for( i = 0; ppsz_options[i] != NULL; i++ )
294 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
298 if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
299 !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
300 ( !strncmp( cfg->psz_name, "no", 2 ) &&
301 !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
307 if( *ppsz_options[i] == '*' &&
308 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
316 if( ppsz_options[i] == NULL )
318 msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
323 char name[plen + strlen( ppsz_options[i] )];
324 const char *psz_name = name;
325 snprintf( name, sizeof (name), "%s%s", psz_prefix,
326 b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
328 /* Check if the option is deprecated */
329 p_conf = config_FindConfig( p_this, name );
331 /* This is basically cut and paste from src/misc/configuration.c
332 * with slight changes */
335 if( p_conf->b_removed )
337 msg_Err( p_this, "Option %s is not supported anymore.",
339 /* TODO: this should return an error and end option parsing
340 * ... but doing this would change the VLC API and all the
341 * modules so i'll do it later */
344 if( p_conf->psz_oldname
345 && !strcmp( p_conf->psz_oldname, name ) )
347 psz_name = p_conf->psz_name;
348 msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
352 /* </Check if the option is deprecated> */
354 /* get the type of the variable */
355 i_type = config_GetType( p_this, psz_name );
358 msg_Warn( p_this, "unknown option %s (value=%s)",
359 cfg->psz_name, cfg->psz_value );
363 i_type &= CONFIG_ITEM;
365 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
367 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
370 if( i_type != VLC_VAR_STRING && b_once )
372 msg_Warn( p_this, "*option_name need to be a string option" );
381 case VLC_VAR_INTEGER:
382 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
386 val.f_float = us_atof( cfg->psz_value ? cfg->psz_value : "0" );
390 val.psz_string = cfg->psz_value;
393 msg_Warn( p_this, "unhandled config var type (%d)", i_type );
394 memset( &val, 0, sizeof( vlc_value_t ) );
401 var_Get( p_this, psz_name, &val2 );
402 if( *val2.psz_string )
404 free( val2.psz_string );
405 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
408 free( val2.psz_string );
410 var_Set( p_this, psz_name, val );
411 msg_Dbg( p_this, "set config option: %s to %s", psz_name,
412 cfg->psz_value ? cfg->psz_value : "(null)" );
416 config_chain_t *config_ChainDuplicate( const config_chain_t *p_src )
418 config_chain_t *p_dst = NULL;
419 config_chain_t **pp_last = &p_dst;
421 for( ; p_src != NULL; p_src = p_src->p_next )
423 config_chain_t *p = malloc( sizeof(*p) );
427 p->psz_name = p_src->psz_name ? strdup( p_src->psz_name ) : NULL;
428 p->psz_value = p_src->psz_value ? strdup( p_src->psz_value ) : NULL;
431 pp_last = &p->p_next;
436 char *config_StringUnescape( char *psz_string )
438 char *psz_src = psz_string;
439 char *psz_dst = psz_string;
445 if( IsEscape( psz_src ) )
447 *psz_dst++ = *psz_src++;
454 char *config_StringEscape( const char *psz_string )
464 for( const char *p = psz_string; *p; p++ )
466 if( IsEscapeNeeded( *p ) )
470 psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 );
471 for( const char *p = psz_string; *p; p++ )
473 if( IsEscapeNeeded( *p ) )