configuration.c 43.3 KB
Newer Older
gbazin's avatar
 
gbazin committed
1 2 3 4
/*****************************************************************************
 * configuration.c management of the modules configuration
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: configuration.c,v 1.48 2003/01/06 00:37:30 garf Exp $
gbazin's avatar
 
gbazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
23

24
#include <vlc/vlc.h>
gbazin's avatar
 
gbazin committed
25

gbazin's avatar
 
gbazin committed
26
#include <stdio.h>                                              /* sprintf() */
gbazin's avatar
 
gbazin committed
27
#include <stdlib.h>                                      /* free(), strtol() */
gbazin's avatar
 
gbazin committed
28
#include <string.h>                                              /* strdup() */
29 30 31
#ifdef HAVE_ERRNO_H
#   include <errno.h>                                               /* errno */
#endif
gbazin's avatar
 
gbazin committed
32

gbazin's avatar
 
gbazin committed
33 34 35
#ifdef HAVE_UNISTD_H
#    include <unistd.h>                                          /* getuid() */
#endif
gbazin's avatar
 
gbazin committed
36

gbazin's avatar
 
gbazin committed
37 38 39 40 41
#ifdef HAVE_GETOPT_LONG
#   ifdef HAVE_GETOPT_H
#       include <getopt.h>                                       /* getopt() */
#   endif
#else
42
#   include "../extras/getopt.h"
gbazin's avatar
 
gbazin committed
43 44 45
#endif

#if defined(HAVE_GETPWUID)
46
#   include <pwd.h>                                            /* getpwuid() */
gbazin's avatar
 
gbazin committed
47 48
#endif

49 50 51 52 53 54
#if defined( HAVE_SYS_STAT_H )
#   include <sys/stat.h>
#endif
#if defined( HAVE_SYS_TYPES_H )
#   include <sys/types.h>
#endif
55 56 57
#if defined( WIN32 ) && !defined( UNDER_CE )
#   include <direct.h>
#endif
gbazin's avatar
 
gbazin committed
58

gbazin's avatar
 
gbazin committed
59
/*****************************************************************************
60
 * config_GetInt: get the value of an int variable
gbazin's avatar
 
gbazin committed
61 62
 *****************************************************************************
 * This function is used to get the value of variables which are internally
gbazin's avatar
 
gbazin committed
63 64
 * represented by an integer (CONFIG_ITEM_INTEGER and
 * CONFIG_ITEM_BOOL).
gbazin's avatar
 
gbazin committed
65
 *****************************************************************************/
66
int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
gbazin's avatar
 
gbazin committed
67 68 69
{
    module_config_t *p_config;

70
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
71 72 73 74

    /* sanity checks */
    if( !p_config )
    {
75
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
76 77
        return -1;
    }
gbazin's avatar
 
gbazin committed
78 79
    if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
        (p_config->i_type!=CONFIG_ITEM_BOOL) )
gbazin's avatar
 
gbazin committed
80
    {
81
        msg_Err( p_this, "option %s does not refer to an int", psz_name );
gbazin's avatar
 
gbazin committed
82 83 84 85 86 87
        return -1;
    }

    return p_config->i_value;
}

gbazin's avatar
 
gbazin committed
88
/*****************************************************************************
89
 * config_GetFloat: get the value of a float variable
gbazin's avatar
 
gbazin committed
90 91
 *****************************************************************************
 * This function is used to get the value of variables which are internally
gbazin's avatar
 
gbazin committed
92
 * represented by a float (CONFIG_ITEM_FLOAT).
gbazin's avatar
 
gbazin committed
93
 *****************************************************************************/
94
float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
gbazin's avatar
 
gbazin committed
95 96 97
{
    module_config_t *p_config;

98
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
99 100 101 102

    /* sanity checks */
    if( !p_config )
    {
103
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
104 105
        return -1;
    }
gbazin's avatar
 
gbazin committed
106
    if( p_config->i_type != CONFIG_ITEM_FLOAT )
gbazin's avatar
 
gbazin committed
107
    {
108
        msg_Err( p_this, "option %s does not refer to a float", psz_name );
gbazin's avatar
 
gbazin committed
109 110 111 112 113 114
        return -1;
    }

    return p_config->f_value;
}

gbazin's avatar
 
gbazin committed
115
/*****************************************************************************
116
 * config_GetPsz: get the string value of a string variable
gbazin's avatar
 
gbazin committed
117 118
 *****************************************************************************
 * This function is used to get the value of variables which are internally
gbazin's avatar
 
gbazin committed
119 120
 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
 * and CONFIG_ITEM_MODULE).
gbazin's avatar
 
gbazin committed
121
 *
gbazin's avatar
 
gbazin committed
122 123 124
 * Important note: remember to free() the returned char* because it's a
 *   duplicate of the actual value. It isn't safe to return a pointer to the
 *   actual value as it can be modified at any time.
gbazin's avatar
 
gbazin committed
125
 *****************************************************************************/
126
char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
gbazin's avatar
 
gbazin committed
127 128 129 130
{
    module_config_t *p_config;
    char *psz_value = NULL;

131
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
132 133 134 135

    /* sanity checks */
    if( !p_config )
    {
136
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
137 138
        return NULL;
    }
gbazin's avatar
 
gbazin committed
139 140 141
    if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
        (p_config->i_type!=CONFIG_ITEM_FILE) &&
        (p_config->i_type!=CONFIG_ITEM_MODULE) )
gbazin's avatar
 
gbazin committed
142
    {
143
        msg_Err( p_this, "option %s does not refer to a string", psz_name );
gbazin's avatar
 
gbazin committed
144 145 146 147
        return NULL;
    }

    /* return a copy of the string */
gbazin's avatar
 
gbazin committed
148
    vlc_mutex_lock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
149
    if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
gbazin's avatar
 
gbazin committed
150
    vlc_mutex_unlock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
151 152 153 154 155

    return psz_value;
}

/*****************************************************************************
156
 * config_PutPsz: set the string value of a string variable
gbazin's avatar
 
gbazin committed
157 158
 *****************************************************************************
 * This function is used to set the value of variables which are internally
gbazin's avatar
 
gbazin committed
159 160
 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
 * and CONFIG_ITEM_MODULE).
gbazin's avatar
 
gbazin committed
161
 *****************************************************************************/
162
void __config_PutPsz( vlc_object_t *p_this, 
163
                      const char *psz_name, const char *psz_value )
gbazin's avatar
 
gbazin committed
164 165 166
{
    module_config_t *p_config;

167
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
168 169 170 171

    /* sanity checks */
    if( !p_config )
    {
172
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
173 174
        return;
    }
gbazin's avatar
 
gbazin committed
175 176 177
    if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
        (p_config->i_type!=CONFIG_ITEM_FILE) &&
        (p_config->i_type!=CONFIG_ITEM_MODULE) )
gbazin's avatar
 
gbazin committed
178
    {
179
        msg_Err( p_this, "option %s does not refer to a string", psz_name );
gbazin's avatar
 
gbazin committed
180 181 182
        return;
    }

gbazin's avatar
 
gbazin committed
183 184 185 186 187
    vlc_mutex_lock( p_config->p_lock );

    /* free old string */
    if( p_config->psz_value ) free( p_config->psz_value );

gbazin's avatar
 
gbazin committed
188 189 190
    if( psz_value ) p_config->psz_value = strdup( psz_value );
    else p_config->psz_value = NULL;

gbazin's avatar
 
gbazin committed
191
    vlc_mutex_unlock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
192

193
    if( p_config->pf_callback )
194
    {
195
        p_config->pf_callback( p_this );
196
    }
gbazin's avatar
 
gbazin committed
197 198 199
}

/*****************************************************************************
200
 * config_PutInt: set the integer value of an int variable
gbazin's avatar
 
gbazin committed
201 202
 *****************************************************************************
 * This function is used to set the value of variables which are internally
gbazin's avatar
 
gbazin committed
203 204
 * represented by an integer (CONFIG_ITEM_INTEGER and
 * CONFIG_ITEM_BOOL).
gbazin's avatar
 
gbazin committed
205
 *****************************************************************************/
206
void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
gbazin's avatar
 
gbazin committed
207 208 209
{
    module_config_t *p_config;

210
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
211 212 213 214

    /* sanity checks */
    if( !p_config )
    {
215
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
216 217
        return;
    }
gbazin's avatar
 
gbazin committed
218 219
    if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
        (p_config->i_type!=CONFIG_ITEM_BOOL) )
gbazin's avatar
 
gbazin committed
220
    {
221
        msg_Err( p_this, "option %s does not refer to an int", psz_name );
gbazin's avatar
 
gbazin committed
222 223 224
        return;
    }

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    /* if i_min == i_max == 0, then do not use them */
    if ((p_config->i_min == 0) && (p_config->i_max == 0))
    {
        p_config->i_value = i_value;
    }
    else if (i_value < p_config->i_min)
    {
        p_config->i_value = p_config->i_min;
    }
    else if (i_value > p_config->i_max)
    {
        p_config->i_value = p_config->i_max;
    }
    else
    {
        p_config->i_value = i_value;
    }
242

243
    if( p_config->pf_callback )
244
    {
245
        p_config->pf_callback( p_this );
246
    }
gbazin's avatar
 
gbazin committed
247 248
}

gbazin's avatar
 
gbazin committed
249
/*****************************************************************************
250
 * config_PutFloat: set the value of a float variable
gbazin's avatar
 
gbazin committed
251 252
 *****************************************************************************
 * This function is used to set the value of variables which are internally
gbazin's avatar
 
gbazin committed
253
 * represented by a float (CONFIG_ITEM_FLOAT).
gbazin's avatar
 
gbazin committed
254
 *****************************************************************************/
255 256
void __config_PutFloat( vlc_object_t *p_this,
                        const char *psz_name, float f_value )
gbazin's avatar
 
gbazin committed
257 258 259
{
    module_config_t *p_config;

260
    p_config = config_FindConfig( p_this, psz_name );
gbazin's avatar
 
gbazin committed
261 262 263 264

    /* sanity checks */
    if( !p_config )
    {
265
        msg_Err( p_this, "option %s does not exist", psz_name );
gbazin's avatar
 
gbazin committed
266 267
        return;
    }
gbazin's avatar
 
gbazin committed
268
    if( p_config->i_type != CONFIG_ITEM_FLOAT )
gbazin's avatar
 
gbazin committed
269
    {
270
        msg_Err( p_this, "option %s does not refer to a float", psz_name );
gbazin's avatar
 
gbazin committed
271 272 273
        return;
    }

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    /* if f_min == f_max == 0, then do not use them */
    if ((p_config->f_min == 0) && (p_config->f_max == 0))
    {
        p_config->f_value = f_value;
    }
    else if (f_value < p_config->f_min)
    {
        p_config->f_value = p_config->f_min;
    }
    else if (f_value > p_config->f_max)
    {
        p_config->f_value = p_config->f_max;
    }
    else
    {
        p_config->f_value = f_value;
    }
291

292
    if( p_config->pf_callback )
293
    {
294
        p_config->pf_callback( p_this );
295
    }
gbazin's avatar
 
gbazin committed
296 297
}

gbazin's avatar
 
gbazin committed
298 299 300 301
/*****************************************************************************
 * config_FindConfig: find the config structure associated with an option.
 *****************************************************************************
 * FIXME: This function really needs to be optimized.
302
 * FIXME: And now even more.
gbazin's avatar
 
gbazin committed
303
 *****************************************************************************/
304
module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
gbazin's avatar
 
gbazin committed
305
{
gbazin's avatar
 
gbazin committed
306 307
    vlc_list_t list; 
    module_t *p_parser;
Sam Hocevar's avatar
 
Sam Hocevar committed
308
    module_config_t *p_item;
gbazin's avatar
 
gbazin committed
309
    int i_index;
gbazin's avatar
 
gbazin committed
310 311 312

    if( !psz_name ) return NULL;

gbazin's avatar
 
gbazin committed
313
    list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
314

gbazin's avatar
 
gbazin committed
315
    for( i_index = 0; i_index < list.i_count; i_index++ )
gbazin's avatar
 
gbazin committed
316
    {
gbazin's avatar
 
gbazin committed
317 318 319
        p_parser = (module_t *)list.p_values[i_index].p_object ;

        if( !p_parser->i_config_items )
320 321
            continue;

gbazin's avatar
 
gbazin committed
322
        for( p_item = p_parser->p_config;
gbazin's avatar
 
gbazin committed
323
             p_item->i_type != CONFIG_HINT_END;
Sam Hocevar's avatar
 
Sam Hocevar committed
324
             p_item++ )
gbazin's avatar
 
gbazin committed
325
        {
gbazin's avatar
 
gbazin committed
326
            if( p_item->i_type & CONFIG_HINT )
gbazin's avatar
 
gbazin committed
327
                /* ignore hints */
gbazin's avatar
 
gbazin committed
328
                continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
329
            if( !strcmp( psz_name, p_item->psz_name ) )
330
            {
gbazin's avatar
 
gbazin committed
331
                vlc_list_release( &list );
Sam Hocevar's avatar
 
Sam Hocevar committed
332
                return p_item;
333
            }
gbazin's avatar
 
gbazin committed
334 335 336
        }
    }

gbazin's avatar
 
gbazin committed
337
    vlc_list_release( &list );
338

gbazin's avatar
 
gbazin committed
339 340 341 342 343 344 345 346 347 348
    return NULL;
}

/*****************************************************************************
 * config_Duplicate: creates a duplicate of a module's configuration data.
 *****************************************************************************
 * Unfortunatly we cannot work directly with the module's config data as
 * this module might be unloaded from memory at any time (remember HideModule).
 * This is why we need to create an exact copy of the config data.
 *****************************************************************************/
349
void config_Duplicate( module_t *p_module, module_config_t *p_orig )
gbazin's avatar
 
gbazin committed
350
{
gbazin's avatar
 
gbazin committed
351
    int i, j, i_lines = 1;
352
    module_config_t *p_item;
gbazin's avatar
 
gbazin committed
353

Sam Hocevar's avatar
 
Sam Hocevar committed
354
    /* Calculate the structure length */
355 356 357
    p_module->i_config_items = 0;
    p_module->i_bool_items = 0;

gbazin's avatar
 
gbazin committed
358
    for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
359 360 361
    {
        i_lines++;

gbazin's avatar
 
gbazin committed
362
        if( p_item->i_type & CONFIG_ITEM )
363 364 365 366
        {
            p_module->i_config_items++;
        }

gbazin's avatar
 
gbazin committed
367
        if( p_item->i_type == CONFIG_ITEM_BOOL )
368 369 370 371
        {
            p_module->i_bool_items++;
        }
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
372 373

    /* Allocate memory */
374 375 376
    p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
                                                     * i_lines );
    if( p_module->p_config == NULL )
gbazin's avatar
 
gbazin committed
377
    {
378 379
        msg_Err( p_module, "config error: can't duplicate p_config" );
        return;
gbazin's avatar
 
gbazin committed
380 381
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
382 383
    /* Do the duplication job */
    for( i = 0; i < i_lines ; i++ )
gbazin's avatar
 
gbazin committed
384
    {
385 386 387
        p_module->p_config[i].i_type = p_orig[i].i_type;
        p_module->p_config[i].i_short = p_orig[i].i_short;
        p_module->p_config[i].i_value = p_orig[i].i_value;
388 389
        p_module->p_config[i].i_min = p_orig[i].i_min;
        p_module->p_config[i].i_max = p_orig[i].i_max;
390
        p_module->p_config[i].f_value = p_orig[i].f_value;
391 392
        p_module->p_config[i].f_min = p_orig[i].f_min;
        p_module->p_config[i].f_max = p_orig[i].f_max;
393
        p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
Sam Hocevar's avatar
 
Sam Hocevar committed
394

395 396
        p_module->p_config[i].psz_type = p_orig[i].psz_type ?
                                   strdup( _(p_orig[i].psz_type) ) : NULL;
397
        p_module->p_config[i].psz_name = p_orig[i].psz_name ?
Sam Hocevar's avatar
 
Sam Hocevar committed
398
                                   strdup( _(p_orig[i].psz_name) ) : NULL;
399
        p_module->p_config[i].psz_text = p_orig[i].psz_text ?
Sam Hocevar's avatar
 
Sam Hocevar committed
400
                                   strdup( _(p_orig[i].psz_text) ) : NULL;
401
        p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
Sam Hocevar's avatar
 
Sam Hocevar committed
402
                                   strdup( _(p_orig[i].psz_longtext) ) : NULL;
403
        p_module->p_config[i].psz_value = p_orig[i].psz_value ?
gbazin's avatar
 
gbazin committed
404
                                   strdup( p_orig[i].psz_value ) : NULL;
gbazin's avatar
 
gbazin committed
405

Sam Hocevar's avatar
Sam Hocevar committed
406
        p_module->p_config[i].p_lock = &p_module->object_lock;
407

gbazin's avatar
 
gbazin committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        /* duplicate the string list */
        p_module->p_config[i].ppsz_list = NULL;
        if( p_orig[i].ppsz_list )
        {
            for( j = 0; p_orig[i].ppsz_list[j]; j++ );
            p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
            if( p_module->p_config[i].ppsz_list )
            {
                for( j = 0; p_orig[i].ppsz_list[j]; j++ )
                    p_module->p_config[i].ppsz_list[j] =
                        strdup( p_orig[i].ppsz_list[j] );
            }
            p_module->p_config[i].ppsz_list[j] = NULL;
        }

423
        p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
gbazin's avatar
 
gbazin committed
424
    }
425 426 427 428 429 430 431 432 433 434
}

/*****************************************************************************
 * config_Free: frees a duplicated module's configuration data.
 *****************************************************************************
 * This function frees all the data duplicated by config_Duplicate.
 *****************************************************************************/
void config_Free( module_t *p_module )
{
    module_config_t *p_item = p_module->p_config;
gbazin's avatar
 
gbazin committed
435
    int i;
436

437 438 439 440 441
    if( p_item == NULL )
    {
        return;
    }

gbazin's avatar
 
gbazin committed
442
    for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
443
    {
444 445 446
        if( p_item->psz_type )
            free( p_item->psz_type );

447 448
        if( p_item->psz_name )
            free( p_item->psz_name );
gbazin's avatar
 
gbazin committed
449

450 451 452 453 454 455 456 457
        if( p_item->psz_text )
            free( p_item->psz_text );

        if( p_item->psz_longtext )
            free( p_item->psz_longtext );

        if( p_item->psz_value )
            free( p_item->psz_value );
gbazin's avatar
 
gbazin committed
458 459

        if( p_item->ppsz_list )
gbazin's avatar
 
gbazin committed
460 461 462 463 464
        {
            for( i = 0; p_item->ppsz_list[i]; i++ )
                free(p_item->ppsz_list[i]);
            free( p_item->ppsz_list );
        }
465 466 467 468
    }

    free( p_module->p_config );
    p_module->p_config = NULL;
gbazin's avatar
 
gbazin committed
469
}
gbazin's avatar
 
gbazin committed
470

471 472 473 474 475 476 477 478 479
/*****************************************************************************
 * config_SetCallbacks: sets callback functions in the duplicate p_config.
 *****************************************************************************
 * Unfortunatly we cannot work directly with the module's config data as
 * this module might be unloaded from memory at any time (remember HideModule).
 * This is why we need to duplicate callbacks each time we reload the module.
 *****************************************************************************/
void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
{
gbazin's avatar
 
gbazin committed
480
    while( p_new->i_type != CONFIG_HINT_END )
481
    {
482
        p_new->pf_callback = p_orig->pf_callback;
483 484 485 486 487 488 489 490 491 492 493 494
        p_new++;
        p_orig++;
    }
}

/*****************************************************************************
 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
 *****************************************************************************
 * We simply undo what we did in config_SetCallbacks.
 *****************************************************************************/
void config_UnsetCallbacks( module_config_t *p_new )
{
gbazin's avatar
 
gbazin committed
495
    while( p_new->i_type != CONFIG_HINT_END )
496
    {
497
        p_new->pf_callback = NULL;
498 499 500 501
        p_new++;
    }
}

gbazin's avatar
 
gbazin committed
502 503 504 505 506 507
/*****************************************************************************
 * config_LoadConfigFile: loads the configuration file.
 *****************************************************************************
 * This function is called to load the config options stored in the config
 * file.
 *****************************************************************************/
508
int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
gbazin's avatar
 
gbazin committed
509
{
gbazin's avatar
 
gbazin committed
510 511
    vlc_list_t list; 
    module_t *p_parser;
Sam Hocevar's avatar
 
Sam Hocevar committed
512
    module_config_t *p_item;
gbazin's avatar
 
gbazin committed
513 514 515 516
    FILE *file;
    char line[1024];
    char *p_index, *psz_option_name, *psz_option_value;
    char *psz_filename, *psz_homedir;
gbazin's avatar
 
gbazin committed
517
    int i_index;
gbazin's avatar
 
gbazin committed
518

519
    psz_homedir = p_this->p_vlc->psz_homedir;
gbazin's avatar
 
gbazin committed
520 521
    if( !psz_homedir )
    {
522
        msg_Err( p_this, "psz_homedir is null" );
gbazin's avatar
 
gbazin committed
523 524
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
525
    psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
gbazin's avatar
 
gbazin committed
526 527 528
                                   strlen(psz_homedir) + 1 );
    if( !psz_filename )
    {
529
        msg_Err( p_this, "out of memory" );
gbazin's avatar
 
gbazin committed
530 531
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
532
    sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
gbazin's avatar
 
gbazin committed
533

534
    msg_Dbg( p_this, "opening config file %s", psz_filename );
gbazin's avatar
 
gbazin committed
535

536 537 538
    /* Acquire config file lock */
    vlc_mutex_lock( &p_this->p_vlc->config_lock );

gbazin's avatar
 
gbazin committed
539
    file = fopen( psz_filename, "rt" );
gbazin's avatar
 
gbazin committed
540 541
    if( !file )
    {
542
        msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
gbazin's avatar
 
gbazin committed
543
        free( psz_filename );
544
        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
545 546 547 548
        return -1;
    }

    /* Look for the selected module, if NULL then save everything */
gbazin's avatar
 
gbazin committed
549
    list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
550

gbazin's avatar
 
gbazin committed
551
    for( i_index = 0; i_index < list.i_count; i_index++ )
gbazin's avatar
 
gbazin committed
552
    {
gbazin's avatar
 
gbazin committed
553
        p_parser = (module_t *)list.p_values[i_index].p_object ;
gbazin's avatar
 
gbazin committed
554

555
        if( psz_module_name
gbazin's avatar
 
gbazin committed
556
             && strcmp( psz_module_name, p_parser->psz_object_name ) )
557
        {
gbazin's avatar
 
gbazin committed
558
            continue;
559
        }
gbazin's avatar
 
gbazin committed
560 561 562

        /* The config file is organized in sections, one per module. Look for
         * the interesting section ( a section is of the form [foo] ) */
563
        fseek( file, 0L, SEEK_SET );
gbazin's avatar
 
gbazin committed
564 565
        while( fgets( line, 1024, file ) )
        {
566 567
            if( (line[0] == '[')
               && (p_index = strchr(line,']'))
568
               && (p_index - &line[1]
gbazin's avatar
 
gbazin committed
569 570 571
                    == (int)strlen(p_parser->psz_object_name))
               && !memcmp( &line[1], p_parser->psz_object_name,
                           strlen(p_parser->psz_object_name) ) )
gbazin's avatar
 
gbazin committed
572
            {
573 574
#if 0
                msg_Dbg( p_this, "loading config for module \"%s\"",
gbazin's avatar
 
gbazin committed
575
                                 p_parser->psz_object_name );
576
#endif
gbazin's avatar
 
gbazin committed
577 578 579 580 581 582 583 584 585 586 587 588

                break;
            }
        }
        /* either we found the section or we're at the EOF */

        /* Now try to load the options in this section */
        while( fgets( line, 1024, file ) )
        {
            if( line[0] == '[' ) break; /* end of section */

            /* ignore comments or empty lines */
gbazin's avatar
 
gbazin committed
589 590
            if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
                continue;
gbazin's avatar
 
gbazin committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604

            /* get rid of line feed */
            if( line[strlen(line)-1] == '\n' )
                line[strlen(line)-1] = (char)0;

            /* look for option name */
            psz_option_name = line;
            psz_option_value = NULL;
            p_index = strchr( line, '=' );
            if( !p_index ) break; /* this ain't an option!!! */

            *p_index = (char)0;
            psz_option_value = p_index + 1;

gbazin's avatar
 
gbazin committed
605
            if( !p_parser->i_config_items )
606 607 608 609
            {
                continue;
            }

gbazin's avatar
 
gbazin committed
610
            /* try to match this option with one of the module's options */
gbazin's avatar
 
gbazin committed
611
            for( p_item = p_parser->p_config;
gbazin's avatar
 
gbazin committed
612
                 p_item->i_type != CONFIG_HINT_END;
Sam Hocevar's avatar
 
Sam Hocevar committed
613
                 p_item++ )
gbazin's avatar
 
gbazin committed
614
            {
gbazin's avatar
 
gbazin committed
615
                if( p_item->i_type & CONFIG_HINT )
gbazin's avatar
 
gbazin committed
616 617
                    /* ignore hints */
                    continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
618 619

                if( !strcmp( p_item->psz_name, psz_option_name ) )
gbazin's avatar
 
gbazin committed
620 621
                {
                    /* We found it */
Sam Hocevar's avatar
 
Sam Hocevar committed
622
                    switch( p_item->i_type )
gbazin's avatar
 
gbazin committed
623
                    {
gbazin's avatar
 
gbazin committed
624 625
                    case CONFIG_ITEM_BOOL:
                    case CONFIG_ITEM_INTEGER:
gbazin's avatar
 
gbazin committed
626 627
                        if( !*psz_option_value )
                            break;                    /* ignore empty option */
Sam Hocevar's avatar
 
Sam Hocevar committed
628
                        p_item->i_value = atoi( psz_option_value);
629 630 631 632
#if 0
                        msg_Dbg( p_this, "option \"%s\", value %i",
                                 p_item->psz_name, p_item->i_value );
#endif
gbazin's avatar
 
gbazin committed
633 634
                        break;

gbazin's avatar
 
gbazin committed
635
                    case CONFIG_ITEM_FLOAT:
gbazin's avatar
 
gbazin committed
636 637 638
                        if( !*psz_option_value )
                            break;                    /* ignore empty option */
                        p_item->f_value = (float)atof( psz_option_value);
639 640 641 642
#if O
                        msg_Dbg( p_this, "option \"%s\", value %f",
                                 p_item->psz_name, (double)p_item->f_value );
#endif
gbazin's avatar
 
gbazin committed
643 644
                        break;

gbazin's avatar
 
gbazin committed
645
                    default:
Sam Hocevar's avatar
 
Sam Hocevar committed
646
                        vlc_mutex_lock( p_item->p_lock );
gbazin's avatar
 
gbazin committed
647 648

                        /* free old string */
Sam Hocevar's avatar
 
Sam Hocevar committed
649 650
                        if( p_item->psz_value )
                            free( p_item->psz_value );
gbazin's avatar
 
gbazin committed
651

Sam Hocevar's avatar
 
Sam Hocevar committed
652
                        p_item->psz_value = *psz_option_value ?
gbazin's avatar
 
gbazin committed
653
                            strdup( psz_option_value ) : NULL;
gbazin's avatar
 
gbazin committed
654

Sam Hocevar's avatar
 
Sam Hocevar committed
655
                        vlc_mutex_unlock( p_item->p_lock );
gbazin's avatar
 
gbazin committed
656

657 658 659 660 661
#if 0
                        msg_Dbg( p_this, "option \"%s\", value \"%s\"",
                                 p_item->psz_name,
                                 p_item->psz_value ? p_item->psz_value : "" );
#endif
gbazin's avatar
 
gbazin committed
662 663 664 665 666 667 668 669
                        break;
                    }
                }
            }
        }

    }
    
gbazin's avatar
 
gbazin committed
670
    vlc_list_release( &list );
671

gbazin's avatar
 
gbazin committed
672 673 674
    fclose( file );
    free( psz_filename );

675
    vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
676 677 678 679 680 681 682 683 684 685 686 687

    return 0;
}

/*****************************************************************************
 * config_SaveConfigFile: Save a module's config options.
 *****************************************************************************
 * This will save the specified module's config options to the config file.
 * If psz_module_name is NULL then we save all the modules config options.
 * It's no use to save the config options that kept their default values, so
 * we'll try to be a bit clever here.
 *
688
 * When we save we mustn't delete the config options of the modules that
gbazin's avatar
 
gbazin committed
689 690 691 692 693 694 695 696 697 698
 * haven't been loaded. So we cannot just create a new config file with the
 * config structures we've got in memory. 
 * I don't really know how to deal with this nicely, so I will use a completly
 * dumb method ;-)
 * I will load the config file in memory, but skipping all the sections of the
 * modules we want to save. Then I will create a brand new file, dump the file
 * loaded in memory and then append the sections of the modules we want to
 * save.
 * Really stupid no ?
 *****************************************************************************/
699
int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
gbazin's avatar
 
gbazin committed
700
{
gbazin's avatar
 
gbazin committed
701 702
    module_t *p_parser;
    vlc_list_t list;
Sam Hocevar's avatar
 
Sam Hocevar committed
703
    module_config_t *p_item;
gbazin's avatar
 
gbazin committed
704 705
    FILE *file;
    char p_line[1024], *p_index2;
Sam Hocevar's avatar
 
Sam Hocevar committed
706
    int i_sizebuf = 0;
gbazin's avatar
 
gbazin committed
707
    char *p_bigbuffer, *p_index;
708
    vlc_bool_t b_backup;
gbazin's avatar
 
gbazin committed
709
    char *psz_filename, *psz_homedir;
gbazin's avatar
 
gbazin committed
710
    int i_index;
gbazin's avatar
 
gbazin committed
711 712

    /* Acquire config file lock */
713
    vlc_mutex_lock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
714

715
    psz_homedir = p_this->p_vlc->psz_homedir;
gbazin's avatar
 
gbazin committed
716 717
    if( !psz_homedir )
    {
718 719
        msg_Err( p_this, "psz_homedir is null" );
        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
720 721
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
722
    psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
gbazin's avatar
 
gbazin committed
723 724 725
                                   strlen(psz_homedir) + 1 );
    if( !psz_filename )
    {
726 727
        msg_Err( p_this, "out of memory" );
        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
728 729
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
730
    sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
Sam Hocevar's avatar
 
Sam Hocevar committed
731

732 733 734 735 736 737 738 739 740 741 742
#if defined( UNDER_CE )
    {
        wchar_t psz_new[ MAX_PATH ];
        MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
        if( CreateDirectory( psz_new, NULL ) )
        {
            msg_Err( p_this, "could not create %s", psz_filename );
        }
    }

#elif defined( HAVE_ERRNO_H )
743
#   if defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
744
    if( mkdir( psz_filename ) && errno != EEXIST )
745 746 747
#   else
    if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
748
    {
749 750
        msg_Err( p_this, "could not create %s (%s)",
                         psz_filename, strerror(errno) );
Sam Hocevar's avatar
 
Sam Hocevar committed
751 752
    }

753 754 755 756 757 758 759 760
#else
    if( mkdir( psz_filename ) )
    {
        msg_Err( p_this, "could not create %s", psz_filename );
    }

#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
761
    strcat( psz_filename, "/" CONFIG_FILE );
gbazin's avatar
 
gbazin committed
762 763


764
    msg_Dbg( p_this, "opening config file %s", psz_filename );
gbazin's avatar
 
gbazin committed
765

gbazin's avatar
 
gbazin committed
766
    file = fopen( psz_filename, "rt" );
gbazin's avatar
 
gbazin committed
767 768
    if( !file )
    {
769
        msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
gbazin's avatar
 
gbazin committed
770 771 772 773
    }
    else
    {
        /* look for file size */
774
        fseek( file, 0L, SEEK_END );
gbazin's avatar
 
gbazin committed
775
        i_sizebuf = ftell( file );
776
        fseek( file, 0L, SEEK_SET );
gbazin's avatar
 
gbazin committed
777 778 779 780 781
    }

    p_bigbuffer = p_index = malloc( i_sizebuf+1 );
    if( !p_bigbuffer )
    {
782
        msg_Err( p_this, "out of memory" );
gbazin's avatar
 
gbazin committed
783 784
        if( file ) fclose( file );
        free( psz_filename );
785
        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
786 787 788 789
        return -1;
    }
    p_bigbuffer[0] = 0;

790
    /* List all available modules */
gbazin's avatar
 
gbazin committed
791
    list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
792

gbazin's avatar
 
gbazin committed
793 794 795 796 797 798 799
    /* backup file into memory, we only need to backup the sections we won't
     * save later on */
    b_backup = 0;
    while( file && fgets( p_line, 1024, file ) )
    {
        if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
        {
800

gbazin's avatar
 
gbazin committed
801
            /* we found a section, check if we need to do a backup */
gbazin's avatar
 
gbazin committed
802
            for( i_index = 0; i_index < list.i_count; i_index++ )
gbazin's avatar
 
gbazin committed
803
            {
gbazin's avatar
 
gbazin committed
804 805
                p_parser = (module_t *)list.p_values[i_index].p_object ;

806
                if( ((p_index2 - &p_line[1])
gbazin's avatar
 
gbazin committed
807 808 809
                       == (int)strlen(p_parser->psz_object_name) )
                    && !memcmp( &p_line[1], p_parser->psz_object_name,
                                strlen(p_parser->psz_object_name) ) )
gbazin's avatar
 
gbazin committed
810 811 812
                {
                    if( !psz_module_name )
                        break;
813
                    else if( !strcmp( psz_module_name,
gbazin's avatar
 
gbazin committed
814
                                      p_parser->psz_object_name ) )
gbazin's avatar
 
gbazin committed
815 816 817 818
                        break;
                }
            }

gbazin's avatar
 
gbazin committed
819
            if( i_index == list.i_count )
gbazin's avatar
 
gbazin committed
820 821 822 823
            {
                /* we don't have this section in our list so we need to back
                 * it up */
                *p_index2 = 0;
824
                msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
825
                                 &p_line[1] );
gbazin's avatar
 
gbazin committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
                *p_index2 = ']';

                b_backup = 1;
            }
            else
            {
                b_backup = 0;
            }
        }

        /* save line if requested and line is valid (doesn't begin with a
         * space, tab, or eol) */
        if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
            && (p_line[0] != '\t') )
        {
            strcpy( p_index, p_line );
            p_index += strlen( p_line );
        }
    }
    if( file ) fclose( file );


    /*
     * Save module config in file
     */

gbazin's avatar
 
gbazin committed
852
    file = fopen( psz_filename, "wt" );
gbazin's avatar
 
gbazin committed
853 854
    if( !file )
    {
855 856
        msg_Warn( p_this, "could not open config file %s for writing",
                          psz_filename );
gbazin's avatar
 
gbazin committed
857
        free( psz_filename );
gbazin's avatar
 
gbazin committed
858
        vlc_list_release( &list );
859
        vlc_mutex_unlock( &p_this->p_vlc->config_lock );
gbazin's avatar
 
gbazin committed
860 861 862
        return -1;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
863
    fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
gbazin's avatar
 
gbazin committed
864 865

    /* Look for the selected module, if NULL then save everything */
gbazin's avatar
 
gbazin committed
866
    for( i_index = 0; i_index < list.i_count; i_index++ )
gbazin's avatar
 
gbazin committed
867
    {
gbazin's avatar
 
gbazin committed
868
        p_parser = (module_t *)list.p_values[i_index].p_object ;
gbazin's avatar
 
gbazin committed
869

870
        if( psz_module_name && strcmp( psz_module_nam