configuration.c 29.5 KB
Newer Older
gbazin's avatar
 
gbazin committed
1 2 3 4
/*****************************************************************************
 * configuration.c management of the modules configuration
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
5
 * $Id: configuration.c,v 1.14 2002/04/17 11:43:31 sam 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

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

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() */
ipkiss's avatar
 
ipkiss committed
29
#include <errno.h>					           /* errno */
gbazin's avatar
 
gbazin committed
30

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

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

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

#include <sys/stat.h>
#include <sys/types.h>

gbazin's avatar
 
gbazin committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*****************************************************************************
 * config_GetIntVariable: get the value of an int variable
 *****************************************************************************
 * This function is used to get the value of variables which are internally
 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
 * MODULE_CONFIG_ITEM_BOOL).
 *****************************************************************************/
int config_GetIntVariable( const char *psz_name )
{
    module_config_t *p_config;

    p_config = config_FindConfig( psz_name );

    /* sanity checks */
    if( !p_config )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
66
        intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
gbazin's avatar
 
gbazin committed
67 68 69 70 71
        return -1;
    }
    if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
72 73
        intf_ErrMsg( "config error: option %s doesn't refer to an int",
                     psz_name );
gbazin's avatar
 
gbazin committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        return -1;
    }

    return p_config->i_value;
}

/*****************************************************************************
 * config_GetPszVariable: get the string value of a string variable
 *****************************************************************************
 * This function is used to get the value of variables which are internally
 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
 * and MODULE_CONFIG_ITEM_PLUGIN).
 *
 * Important note: remember to free() the returned char* because it 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.
 *****************************************************************************/
char * config_GetPszVariable( const char *psz_name )
{
    module_config_t *p_config;
    char *psz_value = NULL;

    p_config = config_FindConfig( psz_name );

    /* sanity checks */
    if( !p_config )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
101
        intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
gbazin's avatar
 
gbazin committed
102 103 104 105 106 107
        return NULL;
    }
    if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
108 109
        intf_ErrMsg( "config error: option %s doesn't refer to a string",
                     psz_name );
gbazin's avatar
 
gbazin committed
110 111 112 113
        return NULL;
    }

    /* return a copy of the string */
gbazin's avatar
 
gbazin committed
114
    vlc_mutex_lock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
115
    if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
gbazin's avatar
 
gbazin committed
116
    vlc_mutex_unlock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    return psz_value;
}

/*****************************************************************************
 * config_PutPszVariable: set the string value of a string variable
 *****************************************************************************
 * This function is used to set the value of variables which are internally
 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
 * and MODULE_CONFIG_ITEM_PLUGIN).
 *****************************************************************************/
void config_PutPszVariable( const char *psz_name, char *psz_value )
{
    module_config_t *p_config;

    p_config = config_FindConfig( psz_name );

    /* sanity checks */
    if( !p_config )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
137
        intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
gbazin's avatar
 
gbazin committed
138 139 140 141 142 143
        return;
    }
    if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
144 145
        intf_ErrMsg( "config error: option %s doesn't refer to a string",
                     psz_name );
gbazin's avatar
 
gbazin committed
146 147 148
        return;
    }

gbazin's avatar
 
gbazin committed
149 150 151 152 153
    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
154 155 156
    if( psz_value ) p_config->psz_value = strdup( psz_value );
    else p_config->psz_value = NULL;

gbazin's avatar
 
gbazin committed
157
    vlc_mutex_unlock( p_config->p_lock );
gbazin's avatar
 
gbazin committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

}

/*****************************************************************************
 * config_PutIntVariable: set the integer value of an int variable
 *****************************************************************************
 * This function is used to set the value of variables which are internally
 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
 * MODULE_CONFIG_ITEM_BOOL).
 *****************************************************************************/
void config_PutIntVariable( const char *psz_name, int i_value )
{
    module_config_t *p_config;

    p_config = config_FindConfig( psz_name );

    /* sanity checks */
    if( !p_config )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
177
        intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
gbazin's avatar
 
gbazin committed
178 179 180 181 182
        return;
    }
    if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
        (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
183 184
        intf_ErrMsg( "config error: option %s doesn't refer to an int",
                     psz_name );
gbazin's avatar
 
gbazin committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        return;
    }

    p_config->i_value = i_value;
}

/*****************************************************************************
 * config_FindConfig: find the config structure associated with an option.
 *****************************************************************************
 * FIXME: This function really needs to be optimized.
 *****************************************************************************/
module_config_t *config_FindConfig( const char *psz_name )
{
    module_t *p_module;
    int i;

    if( !psz_name ) return NULL;

    for( p_module = p_module_bank->first ;
         p_module != NULL ;
         p_module = p_module->next )
    {
gbazin's avatar
 
gbazin committed
207
        for( i = 0; i < p_module->i_config_lines; i++ )
gbazin's avatar
 
gbazin committed
208
        {
gbazin's avatar
 
gbazin committed
209 210
            if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
                /* ignore hints */
gbazin's avatar
 
gbazin committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                continue;
            if( !strcmp( psz_name, p_module->p_config[i].psz_name ) )
                return &p_module->p_config[i];
        }
    }

    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.
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
227
module_config_t *config_Duplicate( module_t *p_module )
gbazin's avatar
 
gbazin committed
228 229 230 231 232 233
{
    int i;
    module_config_t *p_config;

    /* allocate memory */
    p_config = (module_config_t *)malloc( sizeof(module_config_t)
gbazin's avatar
 
gbazin committed
234
                                          * p_module->i_config_lines );
gbazin's avatar
 
gbazin committed
235 236
    if( p_config == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
237
        intf_ErrMsg( "config error: can't duplicate p_config" );
gbazin's avatar
 
gbazin committed
238 239 240
        return( NULL );
    }

gbazin's avatar
 
gbazin committed
241
    for( i = 0; i < p_module->i_config_lines ; i++ )
gbazin's avatar
 
gbazin committed
242
    {
gbazin's avatar
 
gbazin committed
243 244 245 246 247 248 249
        p_config[i].i_type = p_module->p_config_orig[i].i_type;
        p_config[i].i_value = p_module->p_config_orig[i].i_value;
        p_config[i].b_dirty = p_module->p_config_orig[i].b_dirty;
        p_config[i].p_lock = &p_module->config_lock;
        if( p_module->p_config_orig[i].psz_name )
            p_config[i].psz_name =
                strdup( p_module->p_config_orig[i].psz_name );
gbazin's avatar
 
gbazin committed
250
        else p_config[i].psz_name = NULL;
gbazin's avatar
 
gbazin committed
251 252 253
        if( p_module->p_config_orig[i].psz_text )
            p_config[i].psz_text =
                strdup( p_module->p_config_orig[i].psz_text );
gbazin's avatar
 
gbazin committed
254
        else p_config[i].psz_text = NULL;
gbazin's avatar
 
gbazin committed
255 256 257
        if( p_module->p_config_orig[i].psz_longtext )
            p_config[i].psz_longtext =
                strdup( p_module->p_config_orig[i].psz_longtext );
gbazin's avatar
 
gbazin committed
258
        else p_config[i].psz_longtext = NULL;
gbazin's avatar
 
gbazin committed
259 260 261
        if( p_module->p_config_orig[i].psz_value )
            p_config[i].psz_value =
                strdup( p_module->p_config_orig[i].psz_value );
gbazin's avatar
 
gbazin committed
262
        else p_config[i].psz_value = NULL;
gbazin's avatar
 
gbazin committed
263 264 265

        /* the callback pointer is only valid when the module is loaded so this
         * value is set in ActivateModule() and reset in DeactivateModule() */
gbazin's avatar
 
gbazin committed
266
        p_config[i].p_callback = NULL;
gbazin's avatar
 
gbazin committed
267 268 269 270
    }

    return p_config;
}
gbazin's avatar
 
gbazin committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

/*****************************************************************************
 * config_LoadConfigFile: loads the configuration file.
 *****************************************************************************
 * This function is called to load the config options stored in the config
 * file.
 *****************************************************************************/
int config_LoadConfigFile( const char *psz_module_name )
{
    module_t *p_module;
    FILE *file;
    char line[1024];
    char *p_index, *psz_option_name, *psz_option_value;
    int i;
    char *psz_filename, *psz_homedir;

    /* Acquire config file lock */
    vlc_mutex_lock( &p_main->config_lock );

gbazin's avatar
 
gbazin committed
290
    psz_homedir = p_main->psz_homedir;
gbazin's avatar
 
gbazin committed
291 292
    if( !psz_homedir )
    {
gbazin's avatar
 
gbazin committed
293
        intf_ErrMsg( "config error: p_main->psz_homedir is null" );
gbazin's avatar
 
gbazin committed
294 295 296
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
297
    psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
gbazin's avatar
 
gbazin committed
298 299 300
                                   strlen(psz_homedir) + 1 );
    if( !psz_filename )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
301
        intf_ErrMsg( "config error: couldn't malloc psz_filename" );
gbazin's avatar
 
gbazin committed
302 303 304
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
305
    sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
gbazin's avatar
 
gbazin committed
306

Sam Hocevar's avatar
 
Sam Hocevar committed
307
    intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
gbazin's avatar
 
gbazin committed
308 309 310 311

    file = fopen( psz_filename, "r" );
    if( !file )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
312 313
        intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
                         psz_filename, strerror(errno) );
gbazin's avatar
 
gbazin committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        free( psz_filename );
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }

    /* Look for the selected module, if NULL then save everything */
    for( p_module = p_module_bank->first ; p_module != NULL ;
         p_module = p_module->next )
    {

        if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
            continue;

        /* The config file is organized in sections, one per module. Look for
         * the interesting section ( a section is of the form [foo] ) */
        rewind( file );
        while( fgets( line, 1024, file ) )
        {
            if( (line[0] == '[') && (p_index = strchr(line,']')) &&
                (p_index - &line[1] == strlen(p_module->psz_name) ) &&
                !memcmp( &line[1], p_module->psz_name,
                         strlen(p_module->psz_name) ) )
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
337 338
                intf_WarnMsg( 5, "config: loading config for module <%s>",
                                 p_module->psz_name );
gbazin's avatar
 
gbazin committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

                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 */
            if( (line[0] == '#') || (line[0] == (char)0) ) continue;

            /* 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;

            /* try to match this option with one of the module's options */
            for( i = 0; i < p_module->i_config_lines; i++ )
            {
                if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
                    /* ignore hints */
                    continue;
                if( !strcmp( p_module->p_config[i].psz_name,
                             psz_option_name ) )
                {
                    /* We found it */
                    switch( p_module->p_config[i].i_type )
                    {
                    case MODULE_CONFIG_ITEM_BOOL:
                    case MODULE_CONFIG_ITEM_INTEGER:
gbazin's avatar
 
gbazin committed
380 381
                        if( !*psz_option_value )
                            break;                    /* ignore empty option */
gbazin's avatar
 
gbazin committed
382 383
                        p_module->p_config[i].i_value =
                            atoi( psz_option_value);
Sam Hocevar's avatar
 
Sam Hocevar committed
384
                        intf_WarnMsg( 7, "config: found <%s> option %s=%i",
gbazin's avatar
 
gbazin committed
385 386 387 388 389 390 391 392 393 394 395 396
                                      p_module->psz_name,
                                      p_module->p_config[i].psz_name,
                                      p_module->p_config[i].i_value );
                        break;

                    default:
                        vlc_mutex_lock( p_module->p_config[i].p_lock );

                        /* free old string */
                        if( p_module->p_config[i].psz_value )
                            free( p_module->p_config[i].psz_value );

gbazin's avatar
 
gbazin committed
397 398
                        p_module->p_config[i].psz_value = *psz_option_value ?
                            strdup( psz_option_value ) : NULL;
gbazin's avatar
 
gbazin committed
399 400 401

                        vlc_mutex_unlock( p_module->p_config[i].p_lock );

Sam Hocevar's avatar
 
Sam Hocevar committed
402
                        intf_WarnMsg( 7, "config: found <%s> option %s=%s",
gbazin's avatar
 
gbazin committed
403 404
                                      p_module->psz_name,
                                      p_module->p_config[i].psz_name,
Sam Hocevar's avatar
 
Sam Hocevar committed
405 406 407
                                      p_module->p_config[i].psz_value != NULL ?
                                        p_module->p_config[i].psz_value :
                                        "(NULL)" );
gbazin's avatar
 
gbazin committed
408 409 410 411 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
                        break;
                    }
                }
            }
        }

    }
    
    fclose( file );
    free( psz_filename );

    vlc_mutex_unlock( &p_main->config_lock );

    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.
 *
 * When we save we mustn't delete the config options of the plugins that
 * 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 ?
 *****************************************************************************/
int config_SaveConfigFile( const char *psz_module_name )
{
    module_t *p_module;
    FILE *file;
    char p_line[1024], *p_index2;
    int i, i_sizebuf = 0;
    char *p_bigbuffer, *p_index;
    boolean_t b_backup;
    char *psz_filename, *psz_homedir;

    /* Acquire config file lock */
    vlc_mutex_lock( &p_main->config_lock );

gbazin's avatar
 
gbazin committed
456
    psz_homedir = p_main->psz_homedir;
gbazin's avatar
 
gbazin committed
457 458
    if( !psz_homedir )
    {
gbazin's avatar
 
gbazin committed
459
        intf_ErrMsg( "config error: p_main->psz_homedir is null" );
gbazin's avatar
 
gbazin committed
460 461 462
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
463
    psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
gbazin's avatar
 
gbazin committed
464 465 466
                                   strlen(psz_homedir) + 1 );
    if( !psz_filename )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
467
        intf_ErrMsg( "config error: couldn't malloc psz_filename" );
gbazin's avatar
 
gbazin committed
468 469 470
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
471
    sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
gbazin's avatar
 
gbazin committed
472
#ifndef WIN32
Sam Hocevar's avatar
 
Sam Hocevar committed
473
    i = mkdir( psz_filename, 0755 );
gbazin's avatar
 
gbazin committed
474
#else
Sam Hocevar's avatar
 
Sam Hocevar committed
475
    i = mkdir( psz_filename );
gbazin's avatar
 
gbazin committed
476
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
477
    if( i && errno != EEXIST )
Sam Hocevar's avatar
 
Sam Hocevar committed
478
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
479 480
        intf_ErrMsg( "config error: couldn't create %s (%s)",
                     psz_filename, strerror(errno) );
Sam Hocevar's avatar
 
Sam Hocevar committed
481 482
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
483
    strcat( psz_filename, "/" CONFIG_FILE );
gbazin's avatar
 
gbazin committed
484 485


Sam Hocevar's avatar
 
Sam Hocevar committed
486
    intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
gbazin's avatar
 
gbazin committed
487 488 489 490

    file = fopen( psz_filename, "r" );
    if( !file )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
491 492
        intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
                         psz_filename, strerror(errno) );
gbazin's avatar
 
gbazin committed
493 494 495 496 497 498 499 500 501 502 503 504
    }
    else
    {
        /* look for file size */
        fseek( file, 0, SEEK_END );
        i_sizebuf = ftell( file );
        rewind( file );
    }

    p_bigbuffer = p_index = malloc( i_sizebuf+1 );
    if( !p_bigbuffer )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
505
        intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
gbazin's avatar
 
gbazin committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        if( file ) fclose( file );
        free( psz_filename );
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }
    p_bigbuffer[0] = 0;

    /* 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,']')))
        {
            /* we found a section, check if we need to do a backup */
            for( p_module = p_module_bank->first; p_module != NULL;
                 p_module = p_module->next )
            {
                if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
                    !memcmp( &p_line[1], p_module->psz_name,
                             strlen(p_module->psz_name) ) )
                {
                    if( !psz_module_name )
                        break;
                    else if( !strcmp( psz_module_name, p_module->psz_name ) )
                        break;
                }
            }

            if( !p_module )
            {
                /* we don't have this section in our list so we need to back
                 * it up */
                *p_index2 = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
540 541
                intf_WarnMsg( 5, "config: backing up config for "
                                 "unknown module <%s>", &p_line[1] );
gbazin's avatar
 
gbazin committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
                *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
     */

    file = fopen( psz_filename, "w" );
    if( !file )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
571 572
        intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
                         psz_filename );
gbazin's avatar
 
gbazin committed
573 574 575 576 577
        free( psz_filename );
        vlc_mutex_unlock( &p_main->config_lock );
        return -1;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
578
    fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
gbazin's avatar
 
gbazin committed
579 580 581 582 583 584 585 586 587 588 589 590

    /* Look for the selected module, if NULL then save everything */
    for( p_module = p_module_bank->first ; p_module != NULL ;
         p_module = p_module->next )
    {

        if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
            continue;

        if( !p_module->i_config_items )
            continue;

Sam Hocevar's avatar
 
Sam Hocevar committed
591 592
        intf_WarnMsg( 5, "config: saving config for module <%s>",
                         p_module->psz_name );
gbazin's avatar
 
gbazin committed
593

gbazin's avatar
 
gbazin committed
594 595
        fprintf( file, "[%s]\n", p_module->psz_name );

gbazin's avatar
 
gbazin committed
596
        if( p_module->psz_longname )
Sam Hocevar's avatar
 
Sam Hocevar committed
597 598
            fprintf( file, "###\n###  %s\n###\n", p_module->psz_longname );

gbazin's avatar
 
gbazin committed
599 600 601 602 603 604 605 606 607 608 609
        for( i = 0; i < p_module->i_config_lines; i++ )
        {
            if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
                /* ignore hints */
                continue;

            switch( p_module->p_config[i].i_type )
            {
            case MODULE_CONFIG_ITEM_BOOL:
            case MODULE_CONFIG_ITEM_INTEGER:
                if( p_module->p_config[i].psz_text )
gbazin's avatar
 
gbazin committed
610 611
                    fprintf( file, "# %s %s\n", p_module->p_config[i].psz_text,
                             MODULE_CONFIG_ITEM_BOOL?"<boolean>":"<integer>" );
gbazin's avatar
 
gbazin committed
612 613 614 615 616
                fprintf( file, "%s=%i\n", p_module->p_config[i].psz_name,
                         p_module->p_config[i].i_value );
                break;

            default:
gbazin's avatar
 
gbazin committed
617 618 619 620 621 622
                if( p_module->p_config[i].psz_text )
                    fprintf( file, "# %s <string>\n",
                             p_module->p_config[i].psz_text );
                fprintf( file, "%s=%s\n", p_module->p_config[i].psz_name,
                         p_module->p_config[i].psz_value?
                         p_module->p_config[i].psz_value:"" );
gbazin's avatar
 
gbazin committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
            }
        }

        fprintf( file, "\n" );
    }


    /*
     * Restore old settings from the config in file
     */
    fputs( p_bigbuffer, file );
    free( p_bigbuffer );

    fclose( file );
    free( psz_filename );
    vlc_mutex_unlock( &p_main->config_lock );

    return 0;
}

gbazin's avatar
 
gbazin committed
643 644 645 646 647 648 649 650 651 652
/*****************************************************************************
 * config_LoadCmdLine: parse command line
 *****************************************************************************
 * Parse command line for configuration options.
 * Now that the module_bank has been initialized, we can dynamically
 * generate the longopts structure used by getops. We have to do it this way
 * because we don't know (and don't want to know) in advance the configuration
 * options used (ie. exported) by each module.
 *****************************************************************************/
int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
gbazin's avatar
 
gbazin committed
653
                        boolean_t b_ignore_errors )
gbazin's avatar
 
gbazin committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
{
    int i_cmd, i, i_index, i_longopts_size;
    module_t *p_module;
    struct option *p_longopts;

    /* Short options */
    const char *psz_shortopts = "hHvlp:";

    /* Set default configuration and copy arguments */
    p_main->i_argc    = *pi_argc;
    p_main->ppsz_argv = ppsz_argv;

    p_main->p_channel = NULL;

#ifdef SYS_DARWIN
    /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
     * is the PSN - process serial number (a unique PID-ish thingie)
     * still ok for real Darwin & when run from command line */
    if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
                                        /* for example -psn_0_9306113 */
    {
        /* GDMF!... I can't do this or else the MacOSX window server will
         * not pick up the PSN and not register the app and we crash...
         * hence the following kludge otherwise we'll get confused w/ argv[1]
         * being an input file name */
#if 0
        ppsz_argv[ 1 ] = NULL;
#endif
        *pi_argc = *pi_argc - 1;
        pi_argc--;
        return( 0 );
    }
#endif


    /*
     * Generate the longopts structure used by getopt_long
     */
    i_longopts_size = 0;
    for( p_module = p_module_bank->first;
         p_module != NULL ;
         p_module = p_module->next )
    {
        /* count the number of exported configuration options (to allocate
         * longopts). */
        i_longopts_size += p_module->i_config_items;
    }

    p_longopts = (struct option *)malloc( sizeof(struct option)
                                          * (i_longopts_size + 1) );
    if( p_longopts == NULL )
    {
        intf_ErrMsg( "config error: couldn't allocate p_longopts" );
        return( -1 );
    }

    /* Fill the longopts structure */
    i_index = 0;
    for( p_module = p_module_bank->first ;
         p_module != NULL ;
         p_module = p_module->next )
    {
        for( i = 0; i < p_module->i_config_lines; i++ )
        {
            if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
                /* ignore hints */
                continue;
            p_longopts[i_index].name = p_module->p_config[i].psz_name;
            p_longopts[i_index].has_arg =
                (p_module->p_config[i].i_type == MODULE_CONFIG_ITEM_BOOL)?
                                               no_argument : required_argument;
            p_longopts[i_index].flag = 0;
            p_longopts[i_index].val = 0;
            i_index++;
        }
    }
    /* Close the longopts structure */
    memset( &p_longopts[i_index], 0, sizeof(struct option) );


    /*
     * Parse the command line options
     */
    opterr = 0;
    optind = 1;
    while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
                                  p_longopts, &i_index ) ) != EOF )
    {

        if( i_cmd == 0 )
        {
            /* A long option has been recognized */

            module_config_t *p_conf;

            /* Store the configuration option */
            p_conf = config_FindConfig( p_longopts[i_index].name );

            switch( p_conf->i_type )
            {
            case MODULE_CONFIG_ITEM_STRING:
            case MODULE_CONFIG_ITEM_FILE:
            case MODULE_CONFIG_ITEM_PLUGIN:
                config_PutPszVariable( p_longopts[i_index].name, optarg );
                break;
            case MODULE_CONFIG_ITEM_INTEGER:
                config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
                break;
            case MODULE_CONFIG_ITEM_BOOL:
                config_PutIntVariable( p_longopts[i_index].name, 1 );
                break;
            }

            continue;
        }

        /* short options handled here for now */
        switch( i_cmd )
        {

        /* General/common options */
        case 'h':                                              /* -h, --help */
            config_PutIntVariable( "help", 1 );
            break;
        case 'H':                                          /* -H, --longhelp */
            config_PutIntVariable( "longhelp", 1 );
            break;
        case 'l':                                              /* -l, --list */
            config_PutIntVariable( "list", 1 );
            break;
        case 'p':                                            /* -p, --plugin */
            config_PutPszVariable( "plugin", optarg );
            break;
        case 'v':                                           /* -v, --verbose */
            p_main->i_warning_level++;
            break;

        /* Internal error: unknown option */
        case '?':
        default:

            if( !b_ignore_errors )
            {
                intf_ErrMsg( "config error: unknown option `%s'",
                             ppsz_argv[optind-1] );
                intf_Msg( "Try `%s --help' for more information.\n",
                          p_main->psz_arg0 );

                free( p_longopts );
                return( -1 );
            }
        }

    }

    if( p_main->i_warning_level < 0 )
    {
        p_main->i_warning_level = 0;
    }

    free( p_longopts );
    return( 0 );
}
Sam Hocevar's avatar
 
Sam Hocevar committed
817

gbazin's avatar
 
gbazin committed
818
/*****************************************************************************
gbazin's avatar
 
gbazin committed
819
 * config_GetHomeDir: find the user's home directory.
gbazin's avatar
 
gbazin committed
820 821
 *****************************************************************************
 * This function will try by different ways to find the user's home path.
gbazin's avatar
 
gbazin committed
822 823
 * Note that this function is not reentrant, it should be called only once
 * at the beginning of main where the result will be stored for later use.
gbazin's avatar
 
gbazin committed
824
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
825
char *config_GetHomeDir( void )
gbazin's avatar
 
gbazin committed
826 827 828
{
    char *p_tmp, *p_homedir = NULL;

gbazin's avatar
 
gbazin committed
829
#if defined(HAVE_GETPWUID)
gbazin's avatar
 
gbazin committed
830 831 832
    struct passwd *p_pw = NULL;
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
833 834
#if defined(HAVE_GETPWUID)
    if( ( p_pw = getpwuid( getuid() ) ) == NULL )
gbazin's avatar
 
gbazin committed
835
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
836
    {
gbazin's avatar
 
gbazin committed
837 838
        if( ( p_tmp = getenv( "HOME" ) ) == NULL )
        {
gbazin's avatar
 
gbazin committed
839 840 841
            if( ( p_tmp = getenv( "TMP" ) ) == NULL )
            {
                p_homedir = strdup( "/tmp" );
gbazin's avatar
 
gbazin committed
842
            }
gbazin's avatar
 
gbazin committed
843 844
            else p_homedir = strdup( p_tmp );

Sam Hocevar's avatar
 
Sam Hocevar committed
845
            intf_ErrMsg( "config error: unable to get home directory, "
gbazin's avatar
 
gbazin committed
846 847
                         "using %s instead", p_homedir );

gbazin's avatar
 
gbazin committed
848 849 850
        }
        else p_homedir = strdup( p_tmp );
    }
gbazin's avatar
 
gbazin committed
851
#if defined(HAVE_GETPWUID)
gbazin's avatar
 
gbazin committed
852 853
    else
    {
gbazin's avatar
 
gbazin committed
854
        p_homedir = strdup( p_pw->pw_dir );
gbazin's avatar
 
gbazin committed
855 856 857 858 859
    }
#endif

    return p_homedir;
}