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>
37 #include "vlc_interface.h"
39 /*****************************************************************************
41 *****************************************************************************/
44 module{option=*:option=*}[:module{option=*:...}]
46 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
47 #define SKIPTRAILINGSPACE( p, e ) \
48 { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }
50 /* go accross " " and { } */
51 static const char *_get_chain_end( const char *str )
60 if( !*p || *p == ',' || *p == '}' ) return p;
62 if( *p != '{' && *p != '"' && *p != '\'' )
68 if( *p == '{' ) c = '}';
76 if( *p == c ) return ++p;
77 else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
83 char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg, const char *psz_chain )
85 config_chain_t *p_cfg = NULL;
86 const char *p = psz_chain;
95 while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
97 if( p == psz_chain ) return NULL;
99 *ppsz_name = strndup( psz_chain, p - psz_chain );
105 const char *psz_name;
117 while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
118 *p != ' ' && *p != '\t' ) p++;
120 /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
123 fprintf( stderr, "config_ChainCreate: invalid options (empty) \n" );
127 cfg.psz_name = strndup( psz_name, p - psz_name );
131 if( *p == '=' || *p == '{' )
134 bool b_keep_brackets = (*p == '{');
138 end = _get_chain_end( p );
141 cfg.psz_value = NULL;
145 /* Skip heading and trailing spaces.
146 * This ain't necessary but will avoid simple
153 cfg.psz_value = NULL;
157 if( *p == '\'' || *p == '"' ||
158 ( !b_keep_brackets && *p == '{' ) )
162 if( *(end-1) != '\'' && *(end-1) == '"' )
163 SKIPTRAILINGSPACE( p, end );
165 if( end - 1 <= p ) cfg.psz_value = NULL;
166 else cfg.psz_value = strndup( p, end -1 - p );
170 SKIPTRAILINGSPACE( p, end );
171 if( end <= p ) cfg.psz_value = NULL;
172 else cfg.psz_value = strndup( p, end - p );
181 cfg.psz_value = NULL;
187 p_cfg->p_next = malloc( sizeof( config_chain_t ) );
188 memcpy( p_cfg->p_next, &cfg, sizeof( config_chain_t ) );
190 p_cfg = p_cfg->p_next;
194 p_cfg = malloc( sizeof( config_chain_t ) );
195 memcpy( p_cfg, &cfg, sizeof( config_chain_t ) );
210 if( *p == ':' ) return( strdup( p + 1 ) );
215 void config_ChainDestroy( config_chain_t *p_cfg )
217 while( p_cfg != NULL )
219 config_chain_t *p_next;
221 p_next = p_cfg->p_next;
223 FREENULL( p_cfg->psz_name );
224 FREENULL( p_cfg->psz_value );
231 void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
232 const char *const *ppsz_options, config_chain_t *cfg )
234 if( psz_prefix == NULL ) psz_prefix = "";
235 size_t plen = 1 + strlen( psz_prefix );
237 /* First, var_Create all variables */
238 for( size_t i = 0; ppsz_options[i] != NULL; i++ )
240 const char *optname = ppsz_options[i];
241 if (optname[0] == '*')
244 char name[plen + strlen( optname )];
245 snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
246 if( var_Create( p_this, name,
247 config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
248 return /* VLC_xxx */;
251 /* Now parse options and set value */
252 for(; cfg; cfg = cfg->p_next )
257 module_config_t *p_conf;
261 if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
264 for( i = 0; ppsz_options[i] != NULL; i++ )
266 if( !strcmp( ppsz_options[i], cfg->psz_name ) )
270 if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
271 !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
272 ( !strncmp( cfg->psz_name, "no", 2 ) &&
273 !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
279 if( *ppsz_options[i] == '*' &&
280 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
288 if( ppsz_options[i] == NULL )
290 msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
295 char name[plen + strlen( ppsz_options[i] )];
296 const char *psz_name = name;
297 snprintf( name, sizeof (name), "%s%s", psz_prefix,
298 b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
300 /* Check if the option is deprecated */
301 p_conf = config_FindConfig( p_this, name );
303 /* This is basically cut and paste from src/misc/configuration.c
304 * with slight changes */
307 if( p_conf->b_removed )
309 msg_Err( p_this, "Option %s is not supported anymore.",
311 /* TODO: this should return an error and end option parsing
312 * ... but doing this would change the VLC API and all the
313 * modules so i'll do it later */
316 if( p_conf->psz_oldname
317 && !strcmp( p_conf->psz_oldname, name ) )
319 psz_name = p_conf->psz_name;
320 msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
324 /* </Check if the option is deprecated> */
326 /* get the type of the variable */
327 i_type = config_GetType( p_this, psz_name );
330 msg_Warn( p_this, "unknown option %s (value=%s)",
331 cfg->psz_name, cfg->psz_value );
335 i_type &= CONFIG_ITEM;
337 if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
339 msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
342 if( i_type != VLC_VAR_STRING && b_once )
344 msg_Warn( p_this, "*option_name need to be a string option" );
353 case VLC_VAR_INTEGER:
354 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
358 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
362 val.psz_string = cfg->psz_value;
365 msg_Warn( p_this, "unhandled config var type (%d)", i_type );
366 memset( &val, 0, sizeof( vlc_value_t ) );
373 var_Get( p_this, psz_name, &val2 );
374 if( *val2.psz_string )
376 free( val2.psz_string );
377 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
380 free( val2.psz_string );
382 var_Set( p_this, psz_name, val );
383 msg_Dbg( p_this, "set config option: %s to %s", psz_name,
384 cfg->psz_value ? cfg->psz_value : "(null)" );