1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.14 2002/04/17 11:43:31 sam 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 <videolan/vlc.h>
26 #include <stdio.h> /* sprintf() */
27 #include <stdlib.h> /* free(), strtol() */
28 #include <string.h> /* strdup() */
29 #include <errno.h> /* errno */
32 # include <unistd.h> /* getuid() */
35 #ifdef HAVE_GETOPT_LONG
37 # include <getopt.h> /* getopt() */
40 # include "GNUgetopt/getopt.h"
43 #if defined(HAVE_GETPWUID)
44 #include <pwd.h> /* getpwuid() */
48 #include <sys/types.h>
50 /*****************************************************************************
51 * config_GetIntVariable: get the value of an int variable
52 *****************************************************************************
53 * This function is used to get the value of variables which are internally
54 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
55 * MODULE_CONFIG_ITEM_BOOL).
56 *****************************************************************************/
57 int config_GetIntVariable( const char *psz_name )
59 module_config_t *p_config;
61 p_config = config_FindConfig( psz_name );
66 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
69 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
70 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
72 intf_ErrMsg( "config error: option %s doesn't refer to an int",
77 return p_config->i_value;
80 /*****************************************************************************
81 * config_GetPszVariable: get the string value of a string variable
82 *****************************************************************************
83 * This function is used to get the value of variables which are internally
84 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
85 * and MODULE_CONFIG_ITEM_PLUGIN).
87 * Important note: remember to free() the returned char* because it a duplicate
88 * of the actual value. It isn't safe to return a pointer to the actual value
89 * as it can be modified at any time.
90 *****************************************************************************/
91 char * config_GetPszVariable( const char *psz_name )
93 module_config_t *p_config;
94 char *psz_value = NULL;
96 p_config = config_FindConfig( psz_name );
101 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
104 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
105 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
106 (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
108 intf_ErrMsg( "config error: option %s doesn't refer to a string",
113 /* return a copy of the string */
114 vlc_mutex_lock( p_config->p_lock );
115 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
116 vlc_mutex_unlock( p_config->p_lock );
121 /*****************************************************************************
122 * config_PutPszVariable: set the string value of a string variable
123 *****************************************************************************
124 * This function is used to set the value of variables which are internally
125 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
126 * and MODULE_CONFIG_ITEM_PLUGIN).
127 *****************************************************************************/
128 void config_PutPszVariable( const char *psz_name, char *psz_value )
130 module_config_t *p_config;
132 p_config = config_FindConfig( psz_name );
137 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
140 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
141 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
142 (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
144 intf_ErrMsg( "config error: option %s doesn't refer to a string",
149 vlc_mutex_lock( p_config->p_lock );
151 /* free old string */
152 if( p_config->psz_value ) free( p_config->psz_value );
154 if( psz_value ) p_config->psz_value = strdup( psz_value );
155 else p_config->psz_value = NULL;
157 vlc_mutex_unlock( p_config->p_lock );
161 /*****************************************************************************
162 * config_PutIntVariable: set the integer value of an int variable
163 *****************************************************************************
164 * This function is used to set the value of variables which are internally
165 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
166 * MODULE_CONFIG_ITEM_BOOL).
167 *****************************************************************************/
168 void config_PutIntVariable( const char *psz_name, int i_value )
170 module_config_t *p_config;
172 p_config = config_FindConfig( psz_name );
177 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
180 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
181 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
183 intf_ErrMsg( "config error: option %s doesn't refer to an int",
188 p_config->i_value = i_value;
191 /*****************************************************************************
192 * config_FindConfig: find the config structure associated with an option.
193 *****************************************************************************
194 * FIXME: This function really needs to be optimized.
195 *****************************************************************************/
196 module_config_t *config_FindConfig( const char *psz_name )
201 if( !psz_name ) return NULL;
203 for( p_module = p_module_bank->first ;
205 p_module = p_module->next )
207 for( i = 0; i < p_module->i_config_lines; i++ )
209 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
212 if( !strcmp( psz_name, p_module->p_config[i].psz_name ) )
213 return &p_module->p_config[i];
220 /*****************************************************************************
221 * config_Duplicate: creates a duplicate of a module's configuration data.
222 *****************************************************************************
223 * Unfortunatly we cannot work directly with the module's config data as
224 * this module might be unloaded from memory at any time (remember HideModule).
225 * This is why we need to create an exact copy of the config data.
226 *****************************************************************************/
227 module_config_t *config_Duplicate( module_t *p_module )
230 module_config_t *p_config;
232 /* allocate memory */
233 p_config = (module_config_t *)malloc( sizeof(module_config_t)
234 * p_module->i_config_lines );
235 if( p_config == NULL )
237 intf_ErrMsg( "config error: can't duplicate p_config" );
241 for( i = 0; i < p_module->i_config_lines ; i++ )
243 p_config[i].i_type = p_module->p_config_orig[i].i_type;
244 p_config[i].i_value = p_module->p_config_orig[i].i_value;
245 p_config[i].b_dirty = p_module->p_config_orig[i].b_dirty;
246 p_config[i].p_lock = &p_module->config_lock;
247 if( p_module->p_config_orig[i].psz_name )
248 p_config[i].psz_name =
249 strdup( p_module->p_config_orig[i].psz_name );
250 else p_config[i].psz_name = NULL;
251 if( p_module->p_config_orig[i].psz_text )
252 p_config[i].psz_text =
253 strdup( p_module->p_config_orig[i].psz_text );
254 else p_config[i].psz_text = NULL;
255 if( p_module->p_config_orig[i].psz_longtext )
256 p_config[i].psz_longtext =
257 strdup( p_module->p_config_orig[i].psz_longtext );
258 else p_config[i].psz_longtext = NULL;
259 if( p_module->p_config_orig[i].psz_value )
260 p_config[i].psz_value =
261 strdup( p_module->p_config_orig[i].psz_value );
262 else p_config[i].psz_value = NULL;
264 /* the callback pointer is only valid when the module is loaded so this
265 * value is set in ActivateModule() and reset in DeactivateModule() */
266 p_config[i].p_callback = NULL;
272 /*****************************************************************************
273 * config_LoadConfigFile: loads the configuration file.
274 *****************************************************************************
275 * This function is called to load the config options stored in the config
277 *****************************************************************************/
278 int config_LoadConfigFile( const char *psz_module_name )
283 char *p_index, *psz_option_name, *psz_option_value;
285 char *psz_filename, *psz_homedir;
287 /* Acquire config file lock */
288 vlc_mutex_lock( &p_main->config_lock );
290 psz_homedir = p_main->psz_homedir;
293 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
294 vlc_mutex_unlock( &p_main->config_lock );
297 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
298 strlen(psz_homedir) + 1 );
301 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
302 vlc_mutex_unlock( &p_main->config_lock );
305 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
307 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
309 file = fopen( psz_filename, "r" );
312 intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
313 psz_filename, strerror(errno) );
314 free( psz_filename );
315 vlc_mutex_unlock( &p_main->config_lock );
319 /* Look for the selected module, if NULL then save everything */
320 for( p_module = p_module_bank->first ; p_module != NULL ;
321 p_module = p_module->next )
324 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
327 /* The config file is organized in sections, one per module. Look for
328 * the interesting section ( a section is of the form [foo] ) */
330 while( fgets( line, 1024, file ) )
332 if( (line[0] == '[') && (p_index = strchr(line,']')) &&
333 (p_index - &line[1] == strlen(p_module->psz_name) ) &&
334 !memcmp( &line[1], p_module->psz_name,
335 strlen(p_module->psz_name) ) )
337 intf_WarnMsg( 5, "config: loading config for module <%s>",
338 p_module->psz_name );
343 /* either we found the section or we're at the EOF */
345 /* Now try to load the options in this section */
346 while( fgets( line, 1024, file ) )
348 if( line[0] == '[' ) break; /* end of section */
350 /* ignore comments or empty lines */
351 if( (line[0] == '#') || (line[0] == (char)0) ) continue;
353 /* get rid of line feed */
354 if( line[strlen(line)-1] == '\n' )
355 line[strlen(line)-1] = (char)0;
357 /* look for option name */
358 psz_option_name = line;
359 psz_option_value = NULL;
360 p_index = strchr( line, '=' );
361 if( !p_index ) break; /* this ain't an option!!! */
364 psz_option_value = p_index + 1;
366 /* try to match this option with one of the module's options */
367 for( i = 0; i < p_module->i_config_lines; i++ )
369 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
372 if( !strcmp( p_module->p_config[i].psz_name,
376 switch( p_module->p_config[i].i_type )
378 case MODULE_CONFIG_ITEM_BOOL:
379 case MODULE_CONFIG_ITEM_INTEGER:
380 if( !*psz_option_value )
381 break; /* ignore empty option */
382 p_module->p_config[i].i_value =
383 atoi( psz_option_value);
384 intf_WarnMsg( 7, "config: found <%s> option %s=%i",
386 p_module->p_config[i].psz_name,
387 p_module->p_config[i].i_value );
391 vlc_mutex_lock( p_module->p_config[i].p_lock );
393 /* free old string */
394 if( p_module->p_config[i].psz_value )
395 free( p_module->p_config[i].psz_value );
397 p_module->p_config[i].psz_value = *psz_option_value ?
398 strdup( psz_option_value ) : NULL;
400 vlc_mutex_unlock( p_module->p_config[i].p_lock );
402 intf_WarnMsg( 7, "config: found <%s> option %s=%s",
404 p_module->p_config[i].psz_name,
405 p_module->p_config[i].psz_value != NULL ?
406 p_module->p_config[i].psz_value :
417 free( psz_filename );
419 vlc_mutex_unlock( &p_main->config_lock );
424 /*****************************************************************************
425 * config_SaveConfigFile: Save a module's config options.
426 *****************************************************************************
427 * This will save the specified module's config options to the config file.
428 * If psz_module_name is NULL then we save all the modules config options.
429 * It's no use to save the config options that kept their default values, so
430 * we'll try to be a bit clever here.
432 * When we save we mustn't delete the config options of the plugins that
433 * haven't been loaded. So we cannot just create a new config file with the
434 * config structures we've got in memory.
435 * I don't really know how to deal with this nicely, so I will use a completly
437 * I will load the config file in memory, but skipping all the sections of the
438 * modules we want to save. Then I will create a brand new file, dump the file
439 * loaded in memory and then append the sections of the modules we want to
442 *****************************************************************************/
443 int config_SaveConfigFile( const char *psz_module_name )
447 char p_line[1024], *p_index2;
448 int i, i_sizebuf = 0;
449 char *p_bigbuffer, *p_index;
451 char *psz_filename, *psz_homedir;
453 /* Acquire config file lock */
454 vlc_mutex_lock( &p_main->config_lock );
456 psz_homedir = p_main->psz_homedir;
459 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
460 vlc_mutex_unlock( &p_main->config_lock );
463 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
464 strlen(psz_homedir) + 1 );
467 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
468 vlc_mutex_unlock( &p_main->config_lock );
471 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
473 i = mkdir( psz_filename, 0755 );
475 i = mkdir( psz_filename );
477 if( i && errno != EEXIST )
479 intf_ErrMsg( "config error: couldn't create %s (%s)",
480 psz_filename, strerror(errno) );
483 strcat( psz_filename, "/" CONFIG_FILE );
486 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
488 file = fopen( psz_filename, "r" );
491 intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
492 psz_filename, strerror(errno) );
496 /* look for file size */
497 fseek( file, 0, SEEK_END );
498 i_sizebuf = ftell( file );
502 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
505 intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
506 if( file ) fclose( file );
507 free( psz_filename );
508 vlc_mutex_unlock( &p_main->config_lock );
513 /* backup file into memory, we only need to backup the sections we won't
516 while( file && fgets( p_line, 1024, file ) )
518 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
520 /* we found a section, check if we need to do a backup */
521 for( p_module = p_module_bank->first; p_module != NULL;
522 p_module = p_module->next )
524 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
525 !memcmp( &p_line[1], p_module->psz_name,
526 strlen(p_module->psz_name) ) )
528 if( !psz_module_name )
530 else if( !strcmp( psz_module_name, p_module->psz_name ) )
537 /* we don't have this section in our list so we need to back
540 intf_WarnMsg( 5, "config: backing up config for "
541 "unknown module <%s>", &p_line[1] );
552 /* save line if requested and line is valid (doesn't begin with a
553 * space, tab, or eol) */
554 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
555 && (p_line[0] != '\t') )
557 strcpy( p_index, p_line );
558 p_index += strlen( p_line );
561 if( file ) fclose( file );
565 * Save module config in file
568 file = fopen( psz_filename, "w" );
571 intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
573 free( psz_filename );
574 vlc_mutex_unlock( &p_main->config_lock );
578 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
580 /* Look for the selected module, if NULL then save everything */
581 for( p_module = p_module_bank->first ; p_module != NULL ;
582 p_module = p_module->next )
585 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
588 if( !p_module->i_config_items )
591 intf_WarnMsg( 5, "config: saving config for module <%s>",
592 p_module->psz_name );
594 fprintf( file, "[%s]\n", p_module->psz_name );
596 if( p_module->psz_longname )
597 fprintf( file, "###\n### %s\n###\n", p_module->psz_longname );
599 for( i = 0; i < p_module->i_config_lines; i++ )
601 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
605 switch( p_module->p_config[i].i_type )
607 case MODULE_CONFIG_ITEM_BOOL:
608 case MODULE_CONFIG_ITEM_INTEGER:
609 if( p_module->p_config[i].psz_text )
610 fprintf( file, "# %s %s\n", p_module->p_config[i].psz_text,
611 MODULE_CONFIG_ITEM_BOOL?"<boolean>":"<integer>" );
612 fprintf( file, "%s=%i\n", p_module->p_config[i].psz_name,
613 p_module->p_config[i].i_value );
617 if( p_module->p_config[i].psz_text )
618 fprintf( file, "# %s <string>\n",
619 p_module->p_config[i].psz_text );
620 fprintf( file, "%s=%s\n", p_module->p_config[i].psz_name,
621 p_module->p_config[i].psz_value?
622 p_module->p_config[i].psz_value:"" );
626 fprintf( file, "\n" );
631 * Restore old settings from the config in file
633 fputs( p_bigbuffer, file );
637 free( psz_filename );
638 vlc_mutex_unlock( &p_main->config_lock );
643 /*****************************************************************************
644 * config_LoadCmdLine: parse command line
645 *****************************************************************************
646 * Parse command line for configuration options.
647 * Now that the module_bank has been initialized, we can dynamically
648 * generate the longopts structure used by getops. We have to do it this way
649 * because we don't know (and don't want to know) in advance the configuration
650 * options used (ie. exported) by each module.
651 *****************************************************************************/
652 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
653 boolean_t b_ignore_errors )
655 int i_cmd, i, i_index, i_longopts_size;
657 struct option *p_longopts;
660 const char *psz_shortopts = "hHvlp:";
662 /* Set default configuration and copy arguments */
663 p_main->i_argc = *pi_argc;
664 p_main->ppsz_argv = ppsz_argv;
666 p_main->p_channel = NULL;
669 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
670 * is the PSN - process serial number (a unique PID-ish thingie)
671 * still ok for real Darwin & when run from command line */
672 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
673 /* for example -psn_0_9306113 */
675 /* GDMF!... I can't do this or else the MacOSX window server will
676 * not pick up the PSN and not register the app and we crash...
677 * hence the following kludge otherwise we'll get confused w/ argv[1]
678 * being an input file name */
680 ppsz_argv[ 1 ] = NULL;
682 *pi_argc = *pi_argc - 1;
690 * Generate the longopts structure used by getopt_long
693 for( p_module = p_module_bank->first;
695 p_module = p_module->next )
697 /* count the number of exported configuration options (to allocate
699 i_longopts_size += p_module->i_config_items;
702 p_longopts = (struct option *)malloc( sizeof(struct option)
703 * (i_longopts_size + 1) );
704 if( p_longopts == NULL )
706 intf_ErrMsg( "config error: couldn't allocate p_longopts" );
710 /* Fill the longopts structure */
712 for( p_module = p_module_bank->first ;
714 p_module = p_module->next )
716 for( i = 0; i < p_module->i_config_lines; i++ )
718 if( p_module->p_config[i].i_type & MODULE_CONFIG_HINT )
721 p_longopts[i_index].name = p_module->p_config[i].psz_name;
722 p_longopts[i_index].has_arg =
723 (p_module->p_config[i].i_type == MODULE_CONFIG_ITEM_BOOL)?
724 no_argument : required_argument;
725 p_longopts[i_index].flag = 0;
726 p_longopts[i_index].val = 0;
730 /* Close the longopts structure */
731 memset( &p_longopts[i_index], 0, sizeof(struct option) );
735 * Parse the command line options
739 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
740 p_longopts, &i_index ) ) != EOF )
745 /* A long option has been recognized */
747 module_config_t *p_conf;
749 /* Store the configuration option */
750 p_conf = config_FindConfig( p_longopts[i_index].name );
752 switch( p_conf->i_type )
754 case MODULE_CONFIG_ITEM_STRING:
755 case MODULE_CONFIG_ITEM_FILE:
756 case MODULE_CONFIG_ITEM_PLUGIN:
757 config_PutPszVariable( p_longopts[i_index].name, optarg );
759 case MODULE_CONFIG_ITEM_INTEGER:
760 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
762 case MODULE_CONFIG_ITEM_BOOL:
763 config_PutIntVariable( p_longopts[i_index].name, 1 );
770 /* short options handled here for now */
774 /* General/common options */
775 case 'h': /* -h, --help */
776 config_PutIntVariable( "help", 1 );
778 case 'H': /* -H, --longhelp */
779 config_PutIntVariable( "longhelp", 1 );
781 case 'l': /* -l, --list */
782 config_PutIntVariable( "list", 1 );
784 case 'p': /* -p, --plugin */
785 config_PutPszVariable( "plugin", optarg );
787 case 'v': /* -v, --verbose */
788 p_main->i_warning_level++;
791 /* Internal error: unknown option */
795 if( !b_ignore_errors )
797 intf_ErrMsg( "config error: unknown option `%s'",
798 ppsz_argv[optind-1] );
799 intf_Msg( "Try `%s --help' for more information.\n",
809 if( p_main->i_warning_level < 0 )
811 p_main->i_warning_level = 0;
818 /*****************************************************************************
819 * config_GetHomeDir: find the user's home directory.
820 *****************************************************************************
821 * This function will try by different ways to find the user's home path.
822 * Note that this function is not reentrant, it should be called only once
823 * at the beginning of main where the result will be stored for later use.
824 *****************************************************************************/
825 char *config_GetHomeDir( void )
827 char *p_tmp, *p_homedir = NULL;
829 #if defined(HAVE_GETPWUID)
830 struct passwd *p_pw = NULL;
833 #if defined(HAVE_GETPWUID)
834 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
837 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
839 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
841 p_homedir = strdup( "/tmp" );
843 else p_homedir = strdup( p_tmp );
845 intf_ErrMsg( "config error: unable to get home directory, "
846 "using %s instead", p_homedir );
849 else p_homedir = strdup( p_tmp );
851 #if defined(HAVE_GETPWUID)
854 p_homedir = strdup( p_pw->pw_dir );