]> git.sesse.net Git - vlc/blob - src/config/chain.c
Use C functions
[vlc] / src / config / chain.c
1 /*****************************************************************************
2  * chain.c : configuration module chain parsing stuff
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Eric Petit <titer@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
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  * Local prototypes
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 ) p += strspn( p, " \t" )
58 #define SKIPTRAILINGSPACE( p, e ) \
59     do { while( e > p && IsSpace( *(e-1) ) ) e--; } while(0)
60
61 /**
62  * This function will return a pointer after the end of a string element.
63  * It will search the closing element which is
64  * } for { (it will handle nested { ... })
65  * " for "
66  * ' for '
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     /* Look for a opening character */
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     /* Set c to the closing character */
89     if( *p == '{' )
90         c = '}';
91     else
92         c = *p;
93     p++;
94
95     /* Search the closing character, handle nested {..} */
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  * It will extract an option value (=... or {...}).
114  * It will remove the initial = if present but keep the {}
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         /* Skip heading and trailing spaces.
135          * This ain't necessary but will avoid simple
136          * user mistakes. */
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,
178                           const char *psz_chain )
179 {
180     config_chain_t **pp_next = pp_cfg;
181     size_t len;
182
183     *ppsz_name = NULL;
184     *pp_cfg    = NULL;
185
186     if( !psz_chain )
187         return NULL;
188     psz_chain += strspn( psz_chain, " \t" );
189
190     /* Look for parameter (a {...} or :...) or end of name (space or nul) */
191     len = strcspn( psz_chain, "{: \t" );
192     if( len == 0 )
193         return NULL;
194
195     /* Extract the name */
196     *ppsz_name = strndup( psz_chain, len );
197     psz_chain += len;
198
199     /* Parse the parameters */
200     psz_chain += strspn( psz_chain, " \t" );
201     if( *psz_chain == '{' )
202     {
203         /* Parse all name=value[,] elements */
204         do
205         {
206             psz_chain++; /* skip previous delimiter */
207             psz_chain += strspn( psz_chain, " \t" );
208
209             /* Look for the end of the name (,={}_space_) */
210             len = strcspn( psz_chain, "=,{} \t" );
211             if( len == 0 )
212                 continue; /* ignore empty parameter */
213
214             /* Append the new parameter */
215             config_chain_t *p_cfg = malloc( sizeof(*p_cfg) );
216             if( !p_cfg )
217                 break;
218             p_cfg->psz_name = strndup( psz_chain, len );
219             psz_chain += len;
220             p_cfg->psz_value = NULL;
221             p_cfg->p_next = NULL;
222
223             *pp_next = p_cfg;
224             pp_next = &p_cfg->p_next;
225
226             /* Extract the option value */
227             psz_chain += strspn( psz_chain, " \t" );
228             if( strchr( "={", *psz_chain ) )
229             {
230                 p_cfg->psz_value = ChainGetValue( &psz_chain );
231                 psz_chain += strspn( psz_chain, " \t" );
232             }
233         }
234         while( !memchr( "}", *psz_chain, 2 ) );
235
236         if( *psz_chain ) psz_chain++; /* skip '}' */;
237         psz_chain += strspn( psz_chain, " \t" );
238     }
239
240     if( *psz_chain == ':' )
241         return strdup( psz_chain + 1 );
242
243     return NULL;
244 }
245
246 void config_ChainDestroy( config_chain_t *p_cfg )
247 {
248     while( p_cfg != NULL )
249     {
250         config_chain_t *p_next;
251
252         p_next = p_cfg->p_next;
253
254         FREENULL( p_cfg->psz_name );
255         FREENULL( p_cfg->psz_value );
256         free( p_cfg );
257
258         p_cfg = p_next;
259     }
260 }
261
262 void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
263                           const char *const *ppsz_options, config_chain_t *cfg )
264 {
265     if( psz_prefix == NULL ) psz_prefix = "";
266     size_t plen = 1 + strlen( psz_prefix );
267
268     /* First, var_Create all variables */
269     for( size_t i = 0; ppsz_options[i] != NULL; i++ )
270     {
271         const char *optname = ppsz_options[i];
272         if (optname[0] == '*')
273             optname++;
274
275         char name[plen + strlen( optname )];
276         snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
277         if( var_Create( p_this, name,
278                         config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
279             return /* VLC_xxx */;
280     }
281
282     /* Now parse options and set value */
283     for(; cfg; cfg = cfg->p_next )
284     {
285         vlc_value_t val;
286         bool b_yes = true;
287         bool b_once = false;
288         module_config_t *p_conf;
289         int i_type;
290         size_t i;
291
292         if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
293             continue;
294
295         for( i = 0; ppsz_options[i] != NULL; i++ )
296         {
297             if( !strcmp( ppsz_options[i], cfg->psz_name ) )
298             {
299                 break;
300             }
301             if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
302                   !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
303                 ( !strncmp( cfg->psz_name, "no", 2 ) &&
304                   !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
305             {
306                 b_yes = false;
307                 break;
308             }
309
310             if( *ppsz_options[i] == '*' &&
311                 !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
312             {
313                 b_once = true;
314                 break;
315             }
316
317         }
318
319         if( ppsz_options[i] == NULL )
320         {
321             msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
322             continue;
323         }
324
325         /* create name */
326         char name[plen + strlen( ppsz_options[i] )];
327         const char *psz_name = name;
328         snprintf( name, sizeof (name), "%s%s", psz_prefix,
329                   b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
330
331         /* Check if the option is deprecated */
332         p_conf = config_FindConfig( p_this, name );
333
334         /* This is basically cut and paste from src/misc/configuration.c
335          * with slight changes */
336         if( p_conf )
337         {
338             if( p_conf->b_removed )
339             {
340                 msg_Err( p_this, "Option %s is not supported anymore.",
341                          name );
342                 /* TODO: this should return an error and end option parsing
343                  * ... but doing this would change the VLC API and all the
344                  * modules so i'll do it later */
345                 continue;
346             }
347             if( p_conf->psz_oldname
348              && !strcmp( p_conf->psz_oldname, name ) )
349             {
350                  psz_name = p_conf->psz_name;
351                  msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
352                            name, psz_name );
353             }
354         }
355         /* </Check if the option is deprecated> */
356
357         /* get the type of the variable */
358         i_type = config_GetType( p_this, psz_name );
359         if( !i_type )
360         {
361             msg_Warn( p_this, "unknown option %s (value=%s)",
362                       cfg->psz_name, cfg->psz_value );
363             continue;
364         }
365
366         i_type &= CONFIG_ITEM;
367
368         if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
369         {
370             msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
371             continue;
372         }
373         if( i_type != VLC_VAR_STRING && b_once )
374         {
375             msg_Warn( p_this, "*option_name need to be a string option" );
376             continue;
377         }
378
379         switch( i_type )
380         {
381             case VLC_VAR_BOOL:
382                 val.b_bool = b_yes;
383                 break;
384             case VLC_VAR_INTEGER:
385                 val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
386                                     NULL, 0 );
387                 break;
388             case VLC_VAR_FLOAT:
389                 val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
390                 break;
391             case VLC_VAR_STRING:
392             case VLC_VAR_MODULE:
393                 val.psz_string = cfg->psz_value;
394                 break;
395             default:
396                 msg_Warn( p_this, "unhandled config var type (%d)", i_type );
397                 memset( &val, 0, sizeof( vlc_value_t ) );
398                 break;
399         }
400         if( b_once )
401         {
402             vlc_value_t val2;
403
404             var_Get( p_this, psz_name, &val2 );
405             if( *val2.psz_string )
406             {
407                 free( val2.psz_string );
408                 msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
409                 continue;
410             }
411             free( val2.psz_string );
412         }
413         var_Set( p_this, psz_name, val );
414         msg_Dbg( p_this, "set config option: %s to %s", psz_name,
415                  cfg->psz_value ? cfg->psz_value : "(null)" );
416     }
417 }
418
419 char *config_StringUnescape( char *psz_string )
420 {
421     char *psz_src = psz_string;
422     char *psz_dst = psz_string;
423     if( !psz_src )
424         return NULL;
425
426     while( *psz_src )
427     {
428         if( IsEscape( psz_src ) )
429             psz_src++;
430         *psz_dst++ = *psz_src++;
431     }
432     *psz_dst = '\0';
433
434     return psz_string;
435 }
436
437 char *config_StringEscape( const char *psz_string )
438 {
439     char *psz_return;
440     char *psz_dst;
441     int i_escape;
442
443     if( !psz_string )
444         return NULL;
445
446     i_escape = 0;
447     for( const char *p = psz_string; *p; p++ )
448     {
449         if( IsEscapeNeeded( *p ) )
450             i_escape++;
451     }
452
453     psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 );
454     for( const char *p = psz_string; *p; p++ )
455     {
456         if( IsEscapeNeeded( *p ) )
457             *psz_dst++ = '\\';
458         *psz_dst++ = *p;
459     }
460     *psz_dst = '\0';
461
462     return psz_return;
463 }
464
465