1 /*****************************************************************************
2 * configuration_chain.c : configuration module chain parsing stuff
3 *****************************************************************************
4 * Copyright (C) 2002-2006 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 *****************************************************************************/
32 /*****************************************************************************
34 *****************************************************************************/
37 module{option=*:option=*}[:module{option=*:...}]
39 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
40 #define SKIPTRAILINGSPACE( p, e ) \
41 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
43 /* go accross " " and { } */
44 static const char *_get_chain_end( const char *str )
53 if( !*p || *p == ',' || *p == '}' ) return p;
55 if( *p != '{' && *p != '"' && *p != '\'' )
61 if( *p == '{' ) c = '}';
69 if( *p == c ) return ++p;
70 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
76 char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg, const char *psz_chain )
78 config_chain_t *p_cfg = NULL;
79 const char *p = psz_chain;
88 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
90 if( p == psz_chain ) return NULL;
92 *ppsz_name = strndup( psz_chain, p - psz_chain );
110 while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
111 *p != ' ' && *p != '\t' ) p++;
113 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
116 fprintf( stderr, "invalid options (empty)" );
120 cfg.psz_name = strndup( psz_name, p - psz_name );
124 if( *p == '=' || *p == '{' )
127 vlc_bool_t b_keep_brackets = (*p == '{');
131 end = _get_chain_end( p );
134 cfg.psz_value = NULL;
138 /* Skip heading and trailing spaces.
139 * This ain't necessary but will avoid simple
146 cfg.psz_value = NULL;
150 if( *p == '\'' || *p == '"' ||
151 ( !b_keep_brackets && *p == '{' ) )
155 if( *(end-1) != '\'' && *(end-1) == '"' )
156 SKIPTRAILINGSPACE( p, end );
158 if( end - 1 <= p ) cfg.psz_value = NULL;
159 else cfg.psz_value = strndup( p, end -1 - p );
163 SKIPTRAILINGSPACE( p, end );
164 if( end <= p ) cfg.psz_value = NULL;
165 else cfg.psz_value = strndup( p, end - p );
174 cfg.psz_value = NULL;
180 p_cfg->p_next = malloc( sizeof( config_chain_t ) );
181 memcpy( p_cfg->p_next, &cfg, sizeof( config_chain_t ) );
183 p_cfg = p_cfg->p_next;
187 p_cfg = malloc( sizeof( config_chain_t ) );
188 memcpy( p_cfg, &cfg, sizeof( config_chain_t ) );
203 if( *p == ':' ) return( strdup( p + 1 ) );
208 void config_ChainDestroy( config_chain_t *p_cfg )
210 while( p_cfg != NULL )
212 config_chain_t *p_next;
214 p_next = p_cfg->p_next;
216 FREENULL( p_cfg->psz_name );
217 FREENULL( p_cfg->psz_value );
224 void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
225 const char *const *ppsz_options, config_chain_t *cfg )
227 if( psz_prefix == NULL ) psz_prefix = "";
228 size_t plen = 1 + strlen( psz_prefix );
230 /* First, var_Create all variables */
231 for( size_t i = 0; ppsz_options[i] != NULL; i++ )
233 const char *optname = ppsz_options[i];
234 if (optname[0] == '*')
237 char name[plen + strlen( optname )];
238 snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
239 if( var_Create( p_this, name,
240 config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
241 return /* VLC_xxx */;
244 /* Now parse options and set value */
245 for(; cfg; cfg = cfg->p_next )
248 vlc_bool_t b_yes = VLC_TRUE;
249 vlc_bool_t b_once = VLC_FALSE;
250 module_config_t *p_conf;
254 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
257 for( i = 0; ppsz_options[i] != NULL; i++ )
259 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
263 if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
264 !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
265 ( !strncmp( cfg->psz_name, "no", 2 ) &&
266 !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
272 if( *ppsz_options[i] == '*' &&
273 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
281 if( ppsz_options[i] == NULL )
283 msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
288 char name[plen + strlen( ppsz_options[i] )];
289 const char *psz_name = name;
290 snprintf( name, sizeof (name), "%s%s", psz_prefix,
291 b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
293 /* Check if the option is deprecated */
294 p_conf = config_FindConfig( p_this, name );
296 /* This is basically cut and paste from src/misc/configuration.c
297 * with slight changes */
298 if( p_conf && p_conf->psz_current )
300 if( p_conf->b_strict )
302 msg_Err( p_this, "Option %s is not supported anymore.",
304 /* TODO: this should return an error and end option parsing
305 * ... but doing this would change the VLC API and all the
306 * modules so i'll do it later */
309 msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
310 p_conf->psz_name, p_conf->psz_current );
311 psz_name = p_conf->psz_current;
313 /* </Check if the option is deprecated> */
315 /* get the type of the variable */
316 i_type = config_GetType( p_this, psz_name );
319 msg_Warn( p_this, "unknown option %s (value=%s)",
320 cfg->psz_name, cfg->psz_value );
324 i_type &= CONFIG_ITEM;
326 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
328 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
331 if( i_type != VLC_VAR_STRING && b_once )
333 msg_Warn( p_this, "*option_name need to be a string option" );
342 case VLC_VAR_INTEGER:
343 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
347 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
351 val.psz_string = cfg->psz_value;
354 msg_Warn( p_this, "unhandled config var type (%d)", i_type );
355 memset( &val, 0, sizeof( vlc_value_t ) );
362 var_Get( p_this, psz_name, &val2 );
363 if( *val2.psz_string )
365 free( val2.psz_string );
366 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
369 free( val2.psz_string );
371 var_Set( p_this, psz_name, val );
372 msg_Dbg( p_this, "set config option: %s to %s", psz_name,
373 cfg->psz_value ? cfg->psz_value : "(null)" );