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 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
38 module{option=*:option=*}[:module{option=*:...}]
40 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
41 #define SKIPTRAILINGSPACE( p, e ) \
42 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
44 /* go accross " " and { } */
45 static const char *_get_chain_end( const char *str )
54 if( !*p || *p == ',' || *p == '}' ) return p;
56 if( *p != '{' && *p != '"' && *p != '\'' )
62 if( *p == '{' ) c = '}';
70 if( *p == c ) return ++p;
71 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
77 char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg, const char *psz_chain )
79 config_chain_t *p_cfg = NULL;
80 const char *p = psz_chain;
89 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
91 if( p == psz_chain ) return NULL;
93 *ppsz_name = strndup( psz_chain, p - psz_chain );
111 while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
112 *p != ' ' && *p != '\t' ) p++;
114 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
117 fprintf( stderr, "invalid options (empty)" );
121 cfg.psz_name = strndup( psz_name, p - psz_name );
125 if( *p == '=' || *p == '{' )
128 vlc_bool_t b_keep_brackets = (*p == '{');
132 end = _get_chain_end( p );
135 cfg.psz_value = NULL;
139 /* Skip heading and trailing spaces.
140 * This ain't necessary but will avoid simple
147 cfg.psz_value = NULL;
151 if( *p == '\'' || *p == '"' ||
152 ( !b_keep_brackets && *p == '{' ) )
156 if( *(end-1) != '\'' && *(end-1) == '"' )
157 SKIPTRAILINGSPACE( p, end );
159 if( end - 1 <= p ) cfg.psz_value = NULL;
160 else cfg.psz_value = strndup( p, end -1 - p );
164 SKIPTRAILINGSPACE( p, end );
165 if( end <= p ) cfg.psz_value = NULL;
166 else cfg.psz_value = strndup( p, end - p );
175 cfg.psz_value = NULL;
181 p_cfg->p_next = malloc( sizeof( config_chain_t ) );
182 memcpy( p_cfg->p_next, &cfg, sizeof( config_chain_t ) );
184 p_cfg = p_cfg->p_next;
188 p_cfg = malloc( sizeof( config_chain_t ) );
189 memcpy( p_cfg, &cfg, sizeof( config_chain_t ) );
204 if( *p == ':' ) return( strdup( p + 1 ) );
209 void config_ChainDestroy( config_chain_t *p_cfg )
211 while( p_cfg != NULL )
213 config_chain_t *p_next;
215 p_next = p_cfg->p_next;
217 FREENULL( p_cfg->psz_name );
218 FREENULL( p_cfg->psz_value );
225 void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
226 const char *const *ppsz_options, config_chain_t *cfg )
228 if( psz_prefix == NULL ) psz_prefix = "";
229 size_t plen = 1 + strlen( psz_prefix );
231 /* First, var_Create all variables */
232 for( size_t i = 0; ppsz_options[i] != NULL; i++ )
234 const char *optname = ppsz_options[i];
235 if (optname[0] == '*')
238 char name[plen + strlen( optname )];
239 snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
240 if( var_Create( p_this, name,
241 config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
242 return /* VLC_xxx */;
245 /* Now parse options and set value */
246 for(; cfg; cfg = cfg->p_next )
249 vlc_bool_t b_yes = VLC_TRUE;
250 vlc_bool_t b_once = VLC_FALSE;
251 module_config_t *p_conf;
255 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
258 for( i = 0; ppsz_options[i] != NULL; i++ )
260 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
264 if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
265 !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
266 ( !strncmp( cfg->psz_name, "no", 2 ) &&
267 !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
273 if( *ppsz_options[i] == '*' &&
274 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
282 if( ppsz_options[i] == NULL )
284 msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
289 char name[plen + strlen( ppsz_options[i] )];
290 const char *psz_name = name;
291 snprintf( name, sizeof (name), "%s%s", psz_prefix,
292 b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
294 /* Check if the option is deprecated */
295 p_conf = config_FindConfig( p_this, name );
297 /* This is basically cut and paste from src/misc/configuration.c
298 * with slight changes */
301 if( p_conf->b_removed )
303 msg_Err( p_this, "Option %s is not supported anymore.",
305 /* TODO: this should return an error and end option parsing
306 * ... but doing this would change the VLC API and all the
307 * modules so i'll do it later */
310 if( p_conf->psz_oldname
311 && !strcmp( p_conf->psz_oldname, name ) )
313 psz_name = p_conf->psz_name;
314 msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
318 /* </Check if the option is deprecated> */
320 /* get the type of the variable */
321 i_type = config_GetType( p_this, psz_name );
324 msg_Warn( p_this, "unknown option %s (value=%s)",
325 cfg->psz_name, cfg->psz_value );
329 i_type &= CONFIG_ITEM;
331 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
333 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
336 if( i_type != VLC_VAR_STRING && b_once )
338 msg_Warn( p_this, "*option_name need to be a string option" );
347 case VLC_VAR_INTEGER:
348 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
352 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
356 val.psz_string = cfg->psz_value;
359 msg_Warn( p_this, "unhandled config var type (%d)", i_type );
360 memset( &val, 0, sizeof( vlc_value_t ) );
367 var_Get( p_this, psz_name, &val2 );
368 if( *val2.psz_string )
370 free( val2.psz_string );
371 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
374 free( val2.psz_string );
376 var_Set( p_this, psz_name, val );
377 msg_Dbg( p_this, "set config option: %s to %s", psz_name,
378 cfg->psz_value ? cfg->psz_value : "(null)" );