chain.c 12.4 KB
Newer Older
1
/*****************************************************************************
2
 * chain.c : configuration module chain parsing stuff
3
 *****************************************************************************
4
 * Copyright (C) 2002-2007 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * $Id$
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *          Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include "libvlc.h"
36

37
38
#include "vlc_interface.h"

39
40
41
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
42
43
44
45
46
47
48
49
50
51
52
53
54
55
static bool IsEscapeNeeded( char c )
{
    return c == '\'' || c == '"' || c == '\\';
}
static bool IsEscape( const char *psz )
{
    if( !psz )
        return false;
    return psz[0] == '\\' && IsEscapeNeeded( psz[1] );
}
static bool IsSpace( char c  )
{
    return c == ' ' || c == '\t';
}
56

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57
#define SKIPSPACE( p ) p += strspn( p, " \t" )
58
#define SKIPTRAILINGSPACE( p, e ) \
59
60
61
62
63
64
65
66
67
68
    do { while( e > p && IsSpace( *(e-1) ) ) e--; } while(0)

/**
 * This function will return a pointer after the end of a string element.
 * It will search the closing element which is
 * } for { (it will handle nested { ... })
 * " for "
 * ' for '
 */
static const char *ChainGetEnd( const char *psz_string )
69
{
70
    const char *p = psz_string;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
71
    char c;
72

73
74
75
76
    if( !psz_string )
        return NULL;

    /* Look for a opening character */
77
78
    SKIPSPACE( p );

79
    for( ;; p++)
80
    {
81
82
        if( *p == '\0' || *p == ',' || *p == '}' )
            return p;
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        if( *p == '{' || *p == '"' || *p == '\'' )
            break;
    }

    /* Set c to the closing character */
    if( *p == '{' )
        c = '}';
    else
        c = *p;
    p++;

    /* Search the closing character, handle nested {..} */
    for( ;; )
    {
        if( *p == '\0')
            return p;

        if( IsEscape( p ) )
            p += 2;
        else if( *p == c )
            return ++p;
        else if( *p == '{' && c == '}' )
            p = ChainGetEnd( p );
        else
108
            p++;
109
110
111
112
113
114
115
116
117
118
    }
}

/**
 * It will extract an option value (=... or {...}).
 * It will remove the initial = if present but keep the {}
 */
static char *ChainGetValue( const char **ppsz_string )
{
    const char *p = *ppsz_string;
119

120
121
122
123
124
    char *psz_value = NULL;
    const char *end;
    bool b_keep_brackets = (*p == '{');

    if( *p == '=' )
125
126
        p++;

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    end = ChainGetEnd( p );
    if( end <= p )
    {
        psz_value = NULL;
    }
    else
    {
        /* Skip heading and trailing spaces.
         * This ain't necessary but will avoid simple
         * user mistakes. */
        SKIPSPACE( p );
    }

    if( end <= p )
    {
        psz_value = NULL;
    }
    else
    {
        if( *p == '\'' || *p == '"' || ( !b_keep_brackets && *p == '{' ) )
147
        {
148
149
150
151
            p++;

            if( *(end-1) != '\'' && *(end-1) == '"' )
                SKIPTRAILINGSPACE( p, end );
152

153
154
155
156
157
158
159
160
161
162
163
164
            if( end - 1 <= p )
                psz_value = NULL;
            else
                psz_value = strndup( p, end -1 - p );
        }
        else
        {
            SKIPTRAILINGSPACE( p, end );
            if( end <= p )
                psz_value = NULL;
            else
                psz_value = strndup( p, end - p );
165
166
        }
    }
167
168
169
170
171
172
173
174

    /* */
    if( psz_value )
        config_StringUnescape( psz_value );

    /* */
    *ppsz_string = end;
    return psz_value;
175
176
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
177
178
char *config_ChainCreate( char **ppsz_name, config_chain_t **pp_cfg,
                          const char *psz_chain )
179
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180
181
    config_chain_t **pp_next = pp_cfg;
    size_t len;
182
183
184
185

    *ppsz_name = NULL;
    *pp_cfg    = NULL;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
186
    if( !psz_chain )
187
        return NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
188
    psz_chain += strspn( psz_chain, " \t" );
189

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
190
191
192
193
    /* Look for parameter (a {...} or :...) or end of name (space or nul) */
    len = strcspn( psz_chain, "{: \t" );
    *ppsz_name = strndup( psz_chain, len );
    psz_chain += len;
194

195
    /* Parse the parameters */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
196
197
    psz_chain += strspn( psz_chain, " \t" );
    if( *psz_chain == '{' )
198
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199
200
        /* Parse all name=value[,] elements */
        do
201
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
202
203
            psz_chain++; /* skip previous delimiter */
            psz_chain += strspn( psz_chain, " \t" );
204

205
            /* Look for the end of the name (,={}_space_) */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
206
207
208
            len = strcspn( psz_chain, "=,{} \t" );
            if( len == 0 )
                continue; /* ignore empty parameter */
209

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
210
211
212
            /* Append the new parameter */
            config_chain_t *p_cfg = malloc( sizeof(*p_cfg) );
            if( !p_cfg )
213
                break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
214
215
216
217
            p_cfg->psz_name = strndup( psz_chain, len );
            psz_chain += len;
            p_cfg->psz_value = NULL;
            p_cfg->p_next = NULL;
218

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
219
220
            *pp_next = p_cfg;
            pp_next = &p_cfg->p_next;
221

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
222
223
224
            /* Extract the option value */
            psz_chain += strspn( psz_chain, " \t" );
            if( strchr( "={", *psz_chain ) )
225
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
226
227
                p_cfg->psz_value = ChainGetValue( &psz_chain );
                psz_chain += strspn( psz_chain, " \t" );
228
229
            }
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
230
231
232
233
        while( !memchr( "}", *psz_chain, 2 ) );

        if( *psz_chain ) psz_chain++; /* skip '}' */;
        psz_chain += strspn( psz_chain, " \t" );
234
235
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
236
237
    if( *psz_chain == ':' )
        return strdup( psz_chain + 1 );
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

    return NULL;
}

void config_ChainDestroy( config_chain_t *p_cfg )
{
    while( p_cfg != NULL )
    {
        config_chain_t *p_next;

        p_next = p_cfg->p_next;

        FREENULL( p_cfg->psz_name );
        FREENULL( p_cfg->psz_value );
        free( p_cfg );

        p_cfg = p_next;
    }
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
258
void __config_ChainParse( vlc_object_t *p_this, const char *psz_prefix,
259
                          const char *const *ppsz_options, config_chain_t *cfg )
260
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
261
262
    if( psz_prefix == NULL ) psz_prefix = "";
    size_t plen = 1 + strlen( psz_prefix );
263
264

    /* First, var_Create all variables */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
265
    for( size_t i = 0; ppsz_options[i] != NULL; i++ )
266
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
267
268
269
270
271
272
273
274
        const char *optname = ppsz_options[i];
        if (optname[0] == '*')
            optname++;

        char name[plen + strlen( optname )];
        snprintf( name, sizeof (name), "%s%s", psz_prefix, optname );
        if( var_Create( p_this, name,
                        config_GetType( p_this, name ) | VLC_VAR_DOINHERIT ) )
275
            return /* VLC_xxx */;
276
277
278
    }

    /* Now parse options and set value */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
279
    for(; cfg; cfg = cfg->p_next )
280
281
    {
        vlc_value_t val;
282
283
        bool b_yes = true;
        bool b_once = false;
284
        module_config_t *p_conf;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
285
286
        int i_type;
        size_t i;
287
288
289

        if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
            continue;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
290

291
292
293
294
295
296
297
298
299
300
301
        for( i = 0; ppsz_options[i] != NULL; i++ )
        {
            if( !strcmp( ppsz_options[i], cfg->psz_name ) )
            {
                break;
            }
            if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
                  !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
                ( !strncmp( cfg->psz_name, "no", 2 ) &&
                  !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
            {
302
                b_yes = false;
303
304
305
306
307
308
                break;
            }

            if( *ppsz_options[i] == '*' &&
                !strcmp( &ppsz_options[i][1], cfg->psz_name ) )
            {
309
                b_once = true;
310
311
312
313
                break;
            }

        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
314

315
316
317
318
319
320
321
        if( ppsz_options[i] == NULL )
        {
            msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
            continue;
        }

        /* create name */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
322
323
324
325
        char name[plen + strlen( ppsz_options[i] )];
        const char *psz_name = name;
        snprintf( name, sizeof (name), "%s%s", psz_prefix,
                  b_once ? (ppsz_options[i] + 1) : ppsz_options[i] );
326
327

        /* Check if the option is deprecated */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
328
        p_conf = config_FindConfig( p_this, name );
329
330
331

        /* This is basically cut and paste from src/misc/configuration.c
         * with slight changes */
332
        if( p_conf )
333
        {
334
            if( p_conf->b_removed )
335
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
336
                msg_Err( p_this, "Option %s is not supported anymore.",
337
                         name );
338
339
340
                /* TODO: this should return an error and end option parsing
                 * ... but doing this would change the VLC API and all the
                 * modules so i'll do it later */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
341
                continue;
342
            }
343
344
345
346
347
348
349
            if( p_conf->psz_oldname
             && !strcmp( p_conf->psz_oldname, name ) )
            {
                 psz_name = p_conf->psz_name;
                 msg_Warn( p_this, "Option %s is obsolete. Use %s instead.",
                           name, psz_name );
            }
350
351
352
353
354
355
356
357
358
        }
        /* </Check if the option is deprecated> */

        /* get the type of the variable */
        i_type = config_GetType( p_this, psz_name );
        if( !i_type )
        {
            msg_Warn( p_this, "unknown option %s (value=%s)",
                      cfg->psz_name, cfg->psz_value );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
359
            continue;
360
        }
361

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
362
        i_type &= CONFIG_ITEM;
363

364
365
366
        if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
        {
            msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
367
            continue;
368
369
370
371
        }
        if( i_type != VLC_VAR_STRING && b_once )
        {
            msg_Warn( p_this, "*option_name need to be a string option" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
372
            continue;
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        }

        switch( i_type )
        {
            case VLC_VAR_BOOL:
                val.b_bool = b_yes;
                break;
            case VLC_VAR_INTEGER:
                val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
                                    NULL, 0 );
                break;
            case VLC_VAR_FLOAT:
                val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
                break;
            case VLC_VAR_STRING:
            case VLC_VAR_MODULE:
                val.psz_string = cfg->psz_value;
                break;
            default:
392
                msg_Warn( p_this, "unhandled config var type (%d)", i_type );
393
394
395
396
397
398
399
400
401
402
403
404
                memset( &val, 0, sizeof( vlc_value_t ) );
                break;
        }
        if( b_once )
        {
            vlc_value_t val2;

            var_Get( p_this, psz_name, &val2 );
            if( *val2.psz_string )
            {
                free( val2.psz_string );
                msg_Dbg( p_this, "ignoring option %s (not first occurrence)", psz_name );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
405
                continue;
406
407
408
409
            }
            free( val2.psz_string );
        }
        var_Set( p_this, psz_name, val );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
410
411
        msg_Dbg( p_this, "set config option: %s to %s", psz_name,
                 cfg->psz_value ? cfg->psz_value : "(null)" );
412
413
    }
}
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

char *config_StringUnescape( char *psz_string )
{
    char *psz_src = psz_string;
    char *psz_dst = psz_string;
    if( !psz_src )
        return NULL;

    while( *psz_src )
    {
        if( IsEscape( psz_src ) )
            psz_src++;
        *psz_dst++ = *psz_src++;
    }
    *psz_dst = '\0';

    return psz_string;
}

char *config_StringEscape( const char *psz_string )
{
    char *psz_return;
    char *psz_dst;
    int i_escape;

    if( !psz_string )
        return NULL;

    i_escape = 0;
    for( const char *p = psz_string; *p; p++ )
    {
        if( IsEscapeNeeded( *p ) )
            i_escape++;
    }

    psz_return = psz_dst = malloc( strlen( psz_string ) + i_escape + 1 );
    for( const char *p = psz_string; *p; p++ )
    {
        if( IsEscapeNeeded( *p ) )
            *psz_dst++ = '\\';
        *psz_dst++ = *p;
    }
    *psz_dst = '\0';

    return psz_return;
}