1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.10 2002/03/26 22:30:09 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include <stdio.h> /* sprintf() */
25 #include <stdlib.h> /* free(), strtol() */
26 #include <string.h> /* strdup() */
27 #include <unistd.h> /* getuid() */
29 #include <videolan/vlc.h>
31 #ifdef HAVE_GETOPT_LONG
33 # include <getopt.h> /* getopt() */
36 # include "GNUgetopt/getopt.h"
39 #if defined(HAVE_GETPWUID)
40 #include <pwd.h> /* getpwuid() */
44 #include <sys/types.h>
46 /*****************************************************************************
47 * config_GetIntVariable: get the value of an int variable
48 *****************************************************************************
49 * This function is used to get the value of variables which are internally
50 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
51 * MODULE_CONFIG_ITEM_BOOL).
52 *****************************************************************************/
53 int config_GetIntVariable( const char *psz_name )
55 module_config_t *p_config;
57 p_config = config_FindConfig( psz_name );
62 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
65 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
66 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
68 intf_ErrMsg( "config error: option %s doesn't refer to an int",
73 return p_config->i_value;
76 /*****************************************************************************
77 * config_GetPszVariable: get the string value of a string variable
78 *****************************************************************************
79 * This function is used to get the value of variables which are internally
80 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
81 * and MODULE_CONFIG_ITEM_PLUGIN).
83 * Important note: remember to free() the returned char* because it a duplicate
84 * of the actual value. It isn't safe to return a pointer to the actual value
85 * as it can be modified at any time.
86 *****************************************************************************/
87 char * config_GetPszVariable( const char *psz_name )
89 module_config_t *p_config;
90 char *psz_value = NULL;
92 p_config = config_FindConfig( psz_name );
97 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
100 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
101 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
102 (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
104 intf_ErrMsg( "config error: option %s doesn't refer to a string",
109 /* return a copy of the string */
110 vlc_mutex_lock( p_config->p_lock );
111 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
112 vlc_mutex_unlock( p_config->p_lock );
117 /*****************************************************************************
118 * config_PutPszVariable: set the string value of a string variable
119 *****************************************************************************
120 * This function is used to set the value of variables which are internally
121 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
122 * and MODULE_CONFIG_ITEM_PLUGIN).
123 *****************************************************************************/
124 void config_PutPszVariable( const char *psz_name, char *psz_value )
126 module_config_t *p_config;
128 p_config = config_FindConfig( psz_name );
133 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
136 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
137 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
138 (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
140 intf_ErrMsg( "config error: option %s doesn't refer to a string",
145 vlc_mutex_lock( p_config->p_lock );
147 /* free old string */
148 if( p_config->psz_value ) free( p_config->psz_value );
150 if( psz_value ) p_config->psz_value = strdup( psz_value );
151 else p_config->psz_value = NULL;
153 vlc_mutex_unlock( p_config->p_lock );
157 /*****************************************************************************
158 * config_PutIntVariable: set the integer value of an int variable
159 *****************************************************************************
160 * This function is used to set the value of variables which are internally
161 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
162 * MODULE_CONFIG_ITEM_BOOL).
163 *****************************************************************************/
164 void config_PutIntVariable( const char *psz_name, int i_value )
166 module_config_t *p_config;
168 p_config = config_FindConfig( psz_name );
173 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
176 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
177 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
179 intf_ErrMsg( "config error: option %s doesn't refer to an int",
184 p_config->i_value = i_value;
187 /*****************************************************************************
188 * config_FindConfig: find the config structure associated with an option.
189 *****************************************************************************
190 * FIXME: This function really needs to be optimized.
191 *****************************************************************************/
192 module_config_t *config_FindConfig( const char *psz_name )
197 if( !psz_name ) return NULL;
199 for( p_module = p_module_bank->first ;
201 p_module = p_module->next )
203 for( i = 0; i < p_module->i_config_lines; i++ )
205 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
208 if( !strcmp( psz_name, p_module->p_config[i].psz_name ) )
209 return &p_module->p_config[i];
216 /*****************************************************************************
217 * config_Duplicate: creates a duplicate of a module's configuration data.
218 *****************************************************************************
219 * Unfortunatly we cannot work directly with the module's config data as
220 * this module might be unloaded from memory at any time (remember HideModule).
221 * This is why we need to create an exact copy of the config data.
222 *****************************************************************************/
223 module_config_t *config_Duplicate( module_t *p_module )
226 module_config_t *p_config;
228 /* allocate memory */
229 p_config = (module_config_t *)malloc( sizeof(module_config_t)
230 * p_module->i_config_lines );
231 if( p_config == NULL )
233 intf_ErrMsg( "config error: can't duplicate p_config" );
237 for( i = 0; i < p_module->i_config_lines ; i++ )
239 p_config[i].i_type = p_module->p_config_orig[i].i_type;
240 p_config[i].i_value = p_module->p_config_orig[i].i_value;
241 p_config[i].b_dirty = p_module->p_config_orig[i].b_dirty;
242 p_config[i].p_lock = &p_module->config_lock;
243 if( p_module->p_config_orig[i].psz_name )
244 p_config[i].psz_name =
245 strdup( p_module->p_config_orig[i].psz_name );
246 else p_config[i].psz_name = NULL;
247 if( p_module->p_config_orig[i].psz_text )
248 p_config[i].psz_text =
249 strdup( p_module->p_config_orig[i].psz_text );
250 else p_config[i].psz_text = NULL;
251 if( p_module->p_config_orig[i].psz_longtext )
252 p_config[i].psz_longtext =
253 strdup( p_module->p_config_orig[i].psz_longtext );
254 else p_config[i].psz_longtext = NULL;
255 if( p_module->p_config_orig[i].psz_value )
256 p_config[i].psz_value =
257 strdup( p_module->p_config_orig[i].psz_value );
258 else p_config[i].psz_value = NULL;
260 /* the callback pointer is only valid when the module is loaded so this
261 * value is set in ActivateModule() and reset in DeactivateModule() */
262 p_config[i].p_callback = NULL;
268 /*****************************************************************************
269 * config_LoadConfigFile: loads the configuration file.
270 *****************************************************************************
271 * This function is called to load the config options stored in the config
273 *****************************************************************************/
274 int config_LoadConfigFile( const char *psz_module_name )
279 char *p_index, *psz_option_name, *psz_option_value;
281 char *psz_filename, *psz_homedir;
283 /* Acquire config file lock */
284 vlc_mutex_lock( &p_main->config_lock );
286 psz_homedir = p_main->psz_homedir;
289 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
290 vlc_mutex_unlock( &p_main->config_lock );
293 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
294 strlen(psz_homedir) + 1 );
297 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
298 vlc_mutex_unlock( &p_main->config_lock );
301 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
303 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
305 file = fopen( psz_filename, "r" );
308 intf_WarnMsg( 1, "config: couldn't open config file %s for reading",
310 free( psz_filename );
311 vlc_mutex_unlock( &p_main->config_lock );
315 /* Look for the selected module, if NULL then save everything */
316 for( p_module = p_module_bank->first ; p_module != NULL ;
317 p_module = p_module->next )
320 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
323 /* The config file is organized in sections, one per module. Look for
324 * the interesting section ( a section is of the form [foo] ) */
326 while( fgets( line, 1024, file ) )
328 if( (line[0] == '[') && (p_index = strchr(line,']')) &&
329 (p_index - &line[1] == strlen(p_module->psz_name) ) &&
330 !memcmp( &line[1], p_module->psz_name,
331 strlen(p_module->psz_name) ) )
333 intf_WarnMsg( 5, "config: loading config for module <%s>",
334 p_module->psz_name );
339 /* either we found the section or we're at the EOF */
341 /* Now try to load the options in this section */
342 while( fgets( line, 1024, file ) )
344 if( line[0] == '[' ) break; /* end of section */
346 /* ignore comments or empty lines */
347 if( (line[0] == '#') || (line[0] == (char)0) ) continue;
349 /* get rid of line feed */
350 if( line[strlen(line)-1] == '\n' )
351 line[strlen(line)-1] = (char)0;
353 /* look for option name */
354 psz_option_name = line;
355 psz_option_value = NULL;
356 p_index = strchr( line, '=' );
357 if( !p_index ) break; /* this ain't an option!!! */
360 psz_option_value = p_index + 1;
362 /* try to match this option with one of the module's options */
363 for( i = 0; i < p_module->i_config_lines; i++ )
365 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
368 if( !strcmp( p_module->p_config[i].psz_name,
372 switch( p_module->p_config[i].i_type )
374 case MODULE_CONFIG_ITEM_BOOL:
375 case MODULE_CONFIG_ITEM_INTEGER:
376 if( !*psz_option_value )
377 break; /* ignore empty option */
378 p_module->p_config[i].i_value =
379 atoi( psz_option_value);
380 intf_WarnMsg( 7, "config: found <%s> option %s=%i",
382 p_module->p_config[i].psz_name,
383 p_module->p_config[i].i_value );
387 vlc_mutex_lock( p_module->p_config[i].p_lock );
389 /* free old string */
390 if( p_module->p_config[i].psz_value )
391 free( p_module->p_config[i].psz_value );
393 p_module->p_config[i].psz_value = *psz_option_value ?
394 strdup( psz_option_value ) : NULL;
396 vlc_mutex_unlock( p_module->p_config[i].p_lock );
398 intf_WarnMsg( 7, "config: found <%s> option %s=%s",
400 p_module->p_config[i].psz_name,
401 p_module->p_config[i].psz_value );
411 free( psz_filename );
413 vlc_mutex_unlock( &p_main->config_lock );
418 /*****************************************************************************
419 * config_SaveConfigFile: Save a module's config options.
420 *****************************************************************************
421 * This will save the specified module's config options to the config file.
422 * If psz_module_name is NULL then we save all the modules config options.
423 * It's no use to save the config options that kept their default values, so
424 * we'll try to be a bit clever here.
426 * When we save we mustn't delete the config options of the plugins that
427 * haven't been loaded. So we cannot just create a new config file with the
428 * config structures we've got in memory.
429 * I don't really know how to deal with this nicely, so I will use a completly
431 * I will load the config file in memory, but skipping all the sections of the
432 * modules we want to save. Then I will create a brand new file, dump the file
433 * loaded in memory and then append the sections of the modules we want to
436 *****************************************************************************/
437 int config_SaveConfigFile( const char *psz_module_name )
441 char p_line[1024], *p_index2;
442 int i, i_sizebuf = 0;
443 char *p_bigbuffer, *p_index;
445 char *psz_filename, *psz_homedir;
447 /* Acquire config file lock */
448 vlc_mutex_lock( &p_main->config_lock );
450 psz_homedir = p_main->psz_homedir;
453 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
454 vlc_mutex_unlock( &p_main->config_lock );
457 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
458 strlen(psz_homedir) + 1 );
461 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
462 vlc_mutex_unlock( &p_main->config_lock );
465 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
467 mkdir( psz_filename, 0755 );
469 mkdir( psz_filename );
471 strcat( psz_filename, "/" CONFIG_FILE );
474 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
476 file = fopen( psz_filename, "r" );
479 intf_WarnMsg( 1, "config: couldn't open config file %s for reading",
484 /* look for file size */
485 fseek( file, 0, SEEK_END );
486 i_sizebuf = ftell( file );
490 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
493 intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
494 if( file ) fclose( file );
495 free( psz_filename );
496 vlc_mutex_unlock( &p_main->config_lock );
501 /* backup file into memory, we only need to backup the sections we won't
504 while( file && fgets( p_line, 1024, file ) )
506 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
508 /* we found a section, check if we need to do a backup */
509 for( p_module = p_module_bank->first; p_module != NULL;
510 p_module = p_module->next )
512 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
513 !memcmp( &p_line[1], p_module->psz_name,
514 strlen(p_module->psz_name) ) )
516 if( !psz_module_name )
518 else if( !strcmp( psz_module_name, p_module->psz_name ) )
525 /* we don't have this section in our list so we need to back
528 intf_WarnMsg( 5, "config: backing up config for "
529 "unknown module <%s>", &p_line[1] );
540 /* save line if requested and line is valid (doesn't begin with a
541 * space, tab, or eol) */
542 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
543 && (p_line[0] != '\t') )
545 strcpy( p_index, p_line );
546 p_index += strlen( p_line );
549 if( file ) fclose( file );
553 * Save module config in file
556 file = fopen( psz_filename, "w" );
559 intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
561 free( psz_filename );
562 vlc_mutex_unlock( &p_main->config_lock );
566 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
568 /* Look for the selected module, if NULL then save everything */
569 for( p_module = p_module_bank->first ; p_module != NULL ;
570 p_module = p_module->next )
573 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
576 if( !p_module->i_config_items )
579 intf_WarnMsg( 5, "config: saving config for module <%s>",
580 p_module->psz_name );
582 fprintf( file, "[%s]\n", p_module->psz_name );
584 if( p_module->psz_longname )
585 fprintf( file, "###\n### %s\n###\n", p_module->psz_longname );
587 for( i = 0; i < p_module->i_config_lines; i++ )
589 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
593 switch( p_module->p_config[i].i_type )
595 case MODULE_CONFIG_ITEM_BOOL:
596 case MODULE_CONFIG_ITEM_INTEGER:
597 if( p_module->p_config[i].psz_text )
598 fprintf( file, "# %s %s\n", p_module->p_config[i].psz_text,
599 MODULE_CONFIG_ITEM_BOOL?"<boolean>":"<integer>" );
600 fprintf( file, "%s=%i\n", p_module->p_config[i].psz_name,
601 p_module->p_config[i].i_value );
605 if( p_module->p_config[i].psz_text )
606 fprintf( file, "# %s <string>\n",
607 p_module->p_config[i].psz_text );
608 fprintf( file, "%s=%s\n", p_module->p_config[i].psz_name,
609 p_module->p_config[i].psz_value?
610 p_module->p_config[i].psz_value:"" );
614 fprintf( file, "\n" );
619 * Restore old settings from the config in file
621 fputs( p_bigbuffer, file );
625 free( psz_filename );
626 vlc_mutex_unlock( &p_main->config_lock );
631 /*****************************************************************************
632 * config_LoadCmdLine: parse command line
633 *****************************************************************************
634 * Parse command line for configuration options.
635 * Now that the module_bank has been initialized, we can dynamically
636 * generate the longopts structure used by getops. We have to do it this way
637 * because we don't know (and don't want to know) in advance the configuration
638 * options used (ie. exported) by each module.
639 *****************************************************************************/
640 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
641 boolean_t b_ignore_errors )
643 int i_cmd, i, i_index, i_longopts_size;
645 struct option *p_longopts;
648 const char *psz_shortopts = "hHvlp:";
650 /* Set default configuration and copy arguments */
651 p_main->i_argc = *pi_argc;
652 p_main->ppsz_argv = ppsz_argv;
654 p_main->p_channel = NULL;
657 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
658 * is the PSN - process serial number (a unique PID-ish thingie)
659 * still ok for real Darwin & when run from command line */
660 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
661 /* for example -psn_0_9306113 */
663 /* GDMF!... I can't do this or else the MacOSX window server will
664 * not pick up the PSN and not register the app and we crash...
665 * hence the following kludge otherwise we'll get confused w/ argv[1]
666 * being an input file name */
668 ppsz_argv[ 1 ] = NULL;
670 *pi_argc = *pi_argc - 1;
678 * Generate the longopts structure used by getopt_long
681 for( p_module = p_module_bank->first;
683 p_module = p_module->next )
685 /* count the number of exported configuration options (to allocate
687 i_longopts_size += p_module->i_config_items;
690 p_longopts = (struct option *)malloc( sizeof(struct option)
691 * (i_longopts_size + 1) );
692 if( p_longopts == NULL )
694 intf_ErrMsg( "config error: couldn't allocate p_longopts" );
698 /* Fill the longopts structure */
700 for( p_module = p_module_bank->first ;
702 p_module = p_module->next )
704 for( i = 0; i < p_module->i_config_lines; i++ )
706 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
709 p_longopts[i_index].name = p_module->p_config[i].psz_name;
710 p_longopts[i_index].has_arg =
711 (p_module->p_config[i].i_type == MODULE_CONFIG_ITEM_BOOL)?
712 no_argument : required_argument;
713 p_longopts[i_index].flag = 0;
714 p_longopts[i_index].val = 0;
718 /* Close the longopts structure */
719 memset( &p_longopts[i_index], 0, sizeof(struct option) );
723 * Parse the command line options
727 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
728 p_longopts, &i_index ) ) != EOF )
733 /* A long option has been recognized */
735 module_config_t *p_conf;
737 /* Store the configuration option */
738 p_conf = config_FindConfig( p_longopts[i_index].name );
740 switch( p_conf->i_type )
742 case MODULE_CONFIG_ITEM_STRING:
743 case MODULE_CONFIG_ITEM_FILE:
744 case MODULE_CONFIG_ITEM_PLUGIN:
745 config_PutPszVariable( p_longopts[i_index].name, optarg );
747 case MODULE_CONFIG_ITEM_INTEGER:
748 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
750 case MODULE_CONFIG_ITEM_BOOL:
751 config_PutIntVariable( p_longopts[i_index].name, 1 );
758 /* short options handled here for now */
762 /* General/common options */
763 case 'h': /* -h, --help */
764 config_PutIntVariable( "help", 1 );
766 case 'H': /* -H, --longhelp */
767 config_PutIntVariable( "longhelp", 1 );
769 case 'l': /* -l, --list */
770 config_PutIntVariable( "list", 1 );
772 case 'p': /* -p, --plugin */
773 config_PutPszVariable( "plugin", optarg );
775 case 'v': /* -v, --verbose */
776 p_main->i_warning_level++;
779 /* Internal error: unknown option */
783 if( !b_ignore_errors )
785 intf_ErrMsg( "config error: unknown option `%s'",
786 ppsz_argv[optind-1] );
787 intf_Msg( "Try `%s --help' for more information.\n",
797 if( p_main->i_warning_level < 0 )
799 p_main->i_warning_level = 0;
806 /*****************************************************************************
807 * config_GetHomeDir: find the user's home directory.
808 *****************************************************************************
809 * This function will try by different ways to find the user's home path.
810 * Note that this function is not reentrant, it should be called only once
811 * at the beginning of main where the result will be stored for later use.
812 *****************************************************************************/
813 char *config_GetHomeDir( void )
815 char *p_tmp, *p_homedir = NULL;
817 #if defined(HAVE_GETPWUID)
818 struct passwd *p_pw = NULL;
821 #if defined(HAVE_GETPWUID)
822 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
825 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
827 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
829 p_homedir = strdup( "/tmp" );
831 else p_homedir = strdup( p_tmp );
833 intf_ErrMsg( "config error: unable to get home directory, "
834 "using %s instead", p_homedir );
837 else p_homedir = strdup( p_tmp );
839 #if defined(HAVE_GETPWUID)
842 p_homedir = strdup( p_pw->pw_dir );