1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.53 2003/04/06 01:00:07 massiot 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 *****************************************************************************/
26 #include <stdio.h> /* sprintf() */
27 #include <stdlib.h> /* free(), strtol() */
28 #include <string.h> /* strdup() */
30 # include <errno.h> /* errno */
34 # include <unistd.h> /* getuid() */
37 #ifdef HAVE_GETOPT_LONG
39 # include <getopt.h> /* getopt() */
42 # include "../extras/getopt.h"
45 #if defined(HAVE_GETPWUID)
46 # include <pwd.h> /* getpwuid() */
49 #if defined( HAVE_SYS_STAT_H )
50 # include <sys/stat.h>
52 #if defined( HAVE_SYS_TYPES_H )
53 # include <sys/types.h>
55 #if defined( WIN32 ) && !defined( UNDER_CE )
59 /*****************************************************************************
60 * config_GetInt: get the value of an int variable
61 *****************************************************************************
62 * This function is used to get the value of variables which are internally
63 * represented by an integer (CONFIG_ITEM_INTEGER and
65 *****************************************************************************/
66 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
68 module_config_t *p_config;
70 p_config = config_FindConfig( p_this, psz_name );
75 msg_Err( p_this, "option %s does not exist", psz_name );
78 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
79 (p_config->i_type!=CONFIG_ITEM_BOOL) )
81 msg_Err( p_this, "option %s does not refer to an int", psz_name );
85 return p_config->i_value;
88 /*****************************************************************************
89 * config_GetFloat: get the value of a float variable
90 *****************************************************************************
91 * This function is used to get the value of variables which are internally
92 * represented by a float (CONFIG_ITEM_FLOAT).
93 *****************************************************************************/
94 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
96 module_config_t *p_config;
98 p_config = config_FindConfig( p_this, psz_name );
103 msg_Err( p_this, "option %s does not exist", psz_name );
106 if( p_config->i_type != CONFIG_ITEM_FLOAT )
108 msg_Err( p_this, "option %s does not refer to a float", psz_name );
112 return p_config->f_value;
115 /*****************************************************************************
116 * config_GetPsz: get the string value of a string variable
117 *****************************************************************************
118 * This function is used to get the value of variables which are internally
119 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
120 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
122 * Important note: remember to free() the returned char* because it's a
123 * duplicate of the actual value. It isn't safe to return a pointer to the
124 * actual value as it can be modified at any time.
125 *****************************************************************************/
126 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
128 module_config_t *p_config;
129 char *psz_value = NULL;
131 p_config = config_FindConfig( p_this, psz_name );
136 msg_Err( p_this, "option %s does not exist", psz_name );
139 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
140 (p_config->i_type!=CONFIG_ITEM_FILE) &&
141 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
142 (p_config->i_type!=CONFIG_ITEM_MODULE) )
144 msg_Err( p_this, "option %s does not refer to a string", psz_name );
148 /* return a copy of the string */
149 vlc_mutex_lock( p_config->p_lock );
150 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
151 vlc_mutex_unlock( p_config->p_lock );
156 /*****************************************************************************
157 * config_PutPsz: set the string value of a string variable
158 *****************************************************************************
159 * This function is used to set the value of variables which are internally
160 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
161 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
162 *****************************************************************************/
163 void __config_PutPsz( vlc_object_t *p_this,
164 const char *psz_name, const char *psz_value )
166 module_config_t *p_config;
168 p_config = config_FindConfig( p_this, psz_name );
173 msg_Warn( p_this, "option %s does not exist", psz_name );
176 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
177 (p_config->i_type!=CONFIG_ITEM_FILE) &&
178 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
179 (p_config->i_type!=CONFIG_ITEM_MODULE) )
181 msg_Err( p_this, "option %s does not refer to a string", psz_name );
185 vlc_mutex_lock( p_config->p_lock );
187 /* free old string */
188 if( p_config->psz_value ) free( p_config->psz_value );
190 if( psz_value ) p_config->psz_value = strdup( psz_value );
191 else p_config->psz_value = NULL;
193 vlc_mutex_unlock( p_config->p_lock );
195 if( p_config->pf_callback )
197 p_config->pf_callback( p_this );
201 /*****************************************************************************
202 * config_PutInt: set the integer value of an int variable
203 *****************************************************************************
204 * This function is used to set the value of variables which are internally
205 * represented by an integer (CONFIG_ITEM_INTEGER and
207 *****************************************************************************/
208 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
210 module_config_t *p_config;
212 p_config = config_FindConfig( p_this, psz_name );
217 msg_Warn( p_this, "option %s does not exist", psz_name );
220 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
221 (p_config->i_type!=CONFIG_ITEM_BOOL) )
223 msg_Err( p_this, "option %s does not refer to an int", psz_name );
227 /* if i_min == i_max == 0, then do not use them */
228 if ((p_config->i_min == 0) && (p_config->i_max == 0))
230 p_config->i_value = i_value;
232 else if (i_value < p_config->i_min)
234 p_config->i_value = p_config->i_min;
236 else if (i_value > p_config->i_max)
238 p_config->i_value = p_config->i_max;
242 p_config->i_value = i_value;
245 if( p_config->pf_callback )
247 p_config->pf_callback( p_this );
251 /*****************************************************************************
252 * config_PutFloat: set the value of a float variable
253 *****************************************************************************
254 * This function is used to set the value of variables which are internally
255 * represented by a float (CONFIG_ITEM_FLOAT).
256 *****************************************************************************/
257 void __config_PutFloat( vlc_object_t *p_this,
258 const char *psz_name, float f_value )
260 module_config_t *p_config;
262 p_config = config_FindConfig( p_this, psz_name );
267 msg_Warn( p_this, "option %s does not exist", psz_name );
270 if( p_config->i_type != CONFIG_ITEM_FLOAT )
272 msg_Err( p_this, "option %s does not refer to a float", psz_name );
276 /* if f_min == f_max == 0, then do not use them */
277 if ((p_config->f_min == 0) && (p_config->f_max == 0))
279 p_config->f_value = f_value;
281 else if (f_value < p_config->f_min)
283 p_config->f_value = p_config->f_min;
285 else if (f_value > p_config->f_max)
287 p_config->f_value = p_config->f_max;
291 p_config->f_value = f_value;
294 if( p_config->pf_callback )
296 p_config->pf_callback( p_this );
300 /*****************************************************************************
301 * config_FindConfig: find the config structure associated with an option.
302 *****************************************************************************
303 * FIXME: This function really needs to be optimized.
304 * FIXME: And now even more.
305 *****************************************************************************/
306 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
310 module_config_t *p_item;
313 if( !psz_name ) return NULL;
315 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
317 for( i_index = 0; i_index < p_list->i_count; i_index++ )
319 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
321 if( !p_parser->i_config_items )
324 for( p_item = p_parser->p_config;
325 p_item->i_type != CONFIG_HINT_END;
328 if( p_item->i_type & CONFIG_HINT )
331 if( !strcmp( psz_name, p_item->psz_name ) )
333 vlc_list_release( p_list );
339 vlc_list_release( p_list );
344 /*****************************************************************************
345 * config_Duplicate: creates a duplicate of a module's configuration data.
346 *****************************************************************************
347 * Unfortunatly we cannot work directly with the module's config data as
348 * this module might be unloaded from memory at any time (remember HideModule).
349 * This is why we need to create an exact copy of the config data.
350 *****************************************************************************/
351 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
353 int i, j, i_lines = 1;
354 module_config_t *p_item;
356 /* Calculate the structure length */
357 p_module->i_config_items = 0;
358 p_module->i_bool_items = 0;
360 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
364 if( p_item->i_type & CONFIG_ITEM )
366 p_module->i_config_items++;
369 if( p_item->i_type == CONFIG_ITEM_BOOL )
371 p_module->i_bool_items++;
375 /* Allocate memory */
376 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
378 if( p_module->p_config == NULL )
380 msg_Err( p_module, "config error: can't duplicate p_config" );
384 /* Do the duplication job */
385 for( i = 0; i < i_lines ; i++ )
387 p_module->p_config[i].i_type = p_orig[i].i_type;
388 p_module->p_config[i].i_short = p_orig[i].i_short;
389 p_module->p_config[i].i_value = p_orig[i].i_value;
390 p_module->p_config[i].i_min = p_orig[i].i_min;
391 p_module->p_config[i].i_max = p_orig[i].i_max;
392 p_module->p_config[i].f_value = p_orig[i].f_value;
393 p_module->p_config[i].f_min = p_orig[i].f_min;
394 p_module->p_config[i].f_max = p_orig[i].f_max;
395 p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
396 p_module->p_config[i].b_advanced = p_orig[i].b_advanced;
398 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
399 strdup( _(p_orig[i].psz_type) ) : NULL;
400 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
401 strdup( _(p_orig[i].psz_name) ) : NULL;
402 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
403 strdup( _(p_orig[i].psz_text) ) : NULL;
404 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
405 strdup( _(p_orig[i].psz_longtext) ) : NULL;
406 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
407 strdup( p_orig[i].psz_value ) : NULL;
409 p_module->p_config[i].p_lock = &p_module->object_lock;
411 /* duplicate the string list */
412 p_module->p_config[i].ppsz_list = NULL;
413 if( p_orig[i].ppsz_list )
415 for( j = 0; p_orig[i].ppsz_list[j]; j++ );
416 p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
417 if( p_module->p_config[i].ppsz_list )
419 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
420 p_module->p_config[i].ppsz_list[j] =
421 strdup( p_orig[i].ppsz_list[j] );
423 p_module->p_config[i].ppsz_list[j] = NULL;
426 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
430 /*****************************************************************************
431 * config_Free: frees a duplicated module's configuration data.
432 *****************************************************************************
433 * This function frees all the data duplicated by config_Duplicate.
434 *****************************************************************************/
435 void config_Free( module_t *p_module )
437 module_config_t *p_item = p_module->p_config;
445 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
447 if( p_item->psz_type )
448 free( p_item->psz_type );
450 if( p_item->psz_name )
451 free( p_item->psz_name );
453 if( p_item->psz_text )
454 free( p_item->psz_text );
456 if( p_item->psz_longtext )
457 free( p_item->psz_longtext );
459 if( p_item->psz_value )
460 free( p_item->psz_value );
462 if( p_item->ppsz_list )
464 for( i = 0; p_item->ppsz_list[i]; i++ )
465 free(p_item->ppsz_list[i]);
466 free( p_item->ppsz_list );
470 free( p_module->p_config );
471 p_module->p_config = NULL;
474 /*****************************************************************************
475 * config_SetCallbacks: sets callback functions in the duplicate p_config.
476 *****************************************************************************
477 * Unfortunatly we cannot work directly with the module's config data as
478 * this module might be unloaded from memory at any time (remember HideModule).
479 * This is why we need to duplicate callbacks each time we reload the module.
480 *****************************************************************************/
481 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
483 while( p_new->i_type != CONFIG_HINT_END )
485 p_new->pf_callback = p_orig->pf_callback;
491 /*****************************************************************************
492 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
493 *****************************************************************************
494 * We simply undo what we did in config_SetCallbacks.
495 *****************************************************************************/
496 void config_UnsetCallbacks( module_config_t *p_new )
498 while( p_new->i_type != CONFIG_HINT_END )
500 p_new->pf_callback = NULL;
505 /*****************************************************************************
506 * config_LoadConfigFile: loads the configuration file.
507 *****************************************************************************
508 * This function is called to load the config options stored in the config
510 *****************************************************************************/
511 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
515 module_config_t *p_item;
518 char *p_index, *psz_option_name, *psz_option_value;
519 char *psz_filename, *psz_homedir;
522 psz_homedir = p_this->p_vlc->psz_homedir;
525 msg_Err( p_this, "psz_homedir is null" );
528 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
529 strlen(psz_homedir) + 1 );
532 msg_Err( p_this, "out of memory" );
535 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
537 msg_Dbg( p_this, "opening config file %s", psz_filename );
539 /* Acquire config file lock */
540 vlc_mutex_lock( &p_this->p_vlc->config_lock );
542 file = fopen( psz_filename, "rt" );
545 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
546 free( psz_filename );
547 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
551 /* Look for the selected module, if NULL then save everything */
552 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
554 for( i_index = 0; i_index < p_list->i_count; i_index++ )
556 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
559 && strcmp( psz_module_name, p_parser->psz_object_name ) )
564 /* The config file is organized in sections, one per module. Look for
565 * the interesting section ( a section is of the form [foo] ) */
566 fseek( file, 0L, SEEK_SET );
567 while( fgets( line, 1024, file ) )
570 && (p_index = strchr(line,']'))
571 && (p_index - &line[1]
572 == (int)strlen(p_parser->psz_object_name))
573 && !memcmp( &line[1], p_parser->psz_object_name,
574 strlen(p_parser->psz_object_name) ) )
577 msg_Dbg( p_this, "loading config for module \"%s\"",
578 p_parser->psz_object_name );
584 /* either we found the section or we're at the EOF */
586 /* Now try to load the options in this section */
587 while( fgets( line, 1024, file ) )
589 if( line[0] == '[' ) break; /* end of section */
591 /* ignore comments or empty lines */
592 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
595 /* get rid of line feed */
596 if( line[strlen(line)-1] == '\n' )
597 line[strlen(line)-1] = (char)0;
599 /* look for option name */
600 psz_option_name = line;
601 psz_option_value = NULL;
602 p_index = strchr( line, '=' );
603 if( !p_index ) break; /* this ain't an option!!! */
606 psz_option_value = p_index + 1;
608 if( !p_parser->i_config_items )
613 /* try to match this option with one of the module's options */
614 for( p_item = p_parser->p_config;
615 p_item->i_type != CONFIG_HINT_END;
618 if( p_item->i_type & CONFIG_HINT )
622 if( !strcmp( p_item->psz_name, psz_option_name ) )
625 switch( p_item->i_type )
627 case CONFIG_ITEM_BOOL:
628 case CONFIG_ITEM_INTEGER:
629 if( !*psz_option_value )
630 break; /* ignore empty option */
631 p_item->i_value = atoi( psz_option_value);
633 msg_Dbg( p_this, "option \"%s\", value %i",
634 p_item->psz_name, p_item->i_value );
638 case CONFIG_ITEM_FLOAT:
639 if( !*psz_option_value )
640 break; /* ignore empty option */
641 p_item->f_value = (float)atof( psz_option_value);
643 msg_Dbg( p_this, "option \"%s\", value %f",
644 p_item->psz_name, (double)p_item->f_value );
649 vlc_mutex_lock( p_item->p_lock );
651 /* free old string */
652 if( p_item->psz_value )
653 free( p_item->psz_value );
655 p_item->psz_value = *psz_option_value ?
656 strdup( psz_option_value ) : NULL;
658 vlc_mutex_unlock( p_item->p_lock );
661 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
663 p_item->psz_value ? p_item->psz_value : "" );
673 vlc_list_release( p_list );
676 free( psz_filename );
678 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
683 /*****************************************************************************
684 * config_SaveConfigFile: Save a module's config options.
685 *****************************************************************************
686 * This will save the specified module's config options to the config file.
687 * If psz_module_name is NULL then we save all the modules config options.
688 * It's no use to save the config options that kept their default values, so
689 * we'll try to be a bit clever here.
691 * When we save we mustn't delete the config options of the modules that
692 * haven't been loaded. So we cannot just create a new config file with the
693 * config structures we've got in memory.
694 * I don't really know how to deal with this nicely, so I will use a completly
696 * I will load the config file in memory, but skipping all the sections of the
697 * modules we want to save. Then I will create a brand new file, dump the file
698 * loaded in memory and then append the sections of the modules we want to
701 *****************************************************************************/
702 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
706 module_config_t *p_item;
708 char p_line[1024], *p_index2;
710 char *p_bigbuffer, *p_index;
712 char *psz_filename, *psz_homedir;
715 /* Acquire config file lock */
716 vlc_mutex_lock( &p_this->p_vlc->config_lock );
718 psz_homedir = p_this->p_vlc->psz_homedir;
721 msg_Err( p_this, "psz_homedir is null" );
722 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
725 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
726 strlen(psz_homedir) + 1 );
729 msg_Err( p_this, "out of memory" );
730 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
733 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
735 #if defined( UNDER_CE )
737 wchar_t psz_new[ MAX_PATH ];
738 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
739 if( CreateDirectory( psz_new, NULL ) )
741 msg_Err( p_this, "could not create %s", psz_filename );
745 #elif defined( HAVE_ERRNO_H )
746 # if defined( WIN32 )
747 if( mkdir( psz_filename ) && errno != EEXIST )
749 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
752 msg_Err( p_this, "could not create %s (%s)",
753 psz_filename, strerror(errno) );
757 if( mkdir( psz_filename ) )
759 msg_Err( p_this, "could not create %s", psz_filename );
764 strcat( psz_filename, "/" CONFIG_FILE );
767 msg_Dbg( p_this, "opening config file %s", psz_filename );
769 file = fopen( psz_filename, "rt" );
772 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
776 /* look for file size */
777 fseek( file, 0L, SEEK_END );
778 i_sizebuf = ftell( file );
779 fseek( file, 0L, SEEK_SET );
782 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
785 msg_Err( p_this, "out of memory" );
786 if( file ) fclose( file );
787 free( psz_filename );
788 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
793 /* List all available modules */
794 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
796 /* backup file into memory, we only need to backup the sections we won't
799 while( file && fgets( p_line, 1024, file ) )
801 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
804 /* we found a section, check if we need to do a backup */
805 for( i_index = 0; i_index < p_list->i_count; i_index++ )
807 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
809 if( ((p_index2 - &p_line[1])
810 == (int)strlen(p_parser->psz_object_name) )
811 && !memcmp( &p_line[1], p_parser->psz_object_name,
812 strlen(p_parser->psz_object_name) ) )
814 if( !psz_module_name )
816 else if( !strcmp( psz_module_name,
817 p_parser->psz_object_name ) )
822 if( i_index == p_list->i_count )
824 /* we don't have this section in our list so we need to back
827 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
839 /* save line if requested and line is valid (doesn't begin with a
840 * space, tab, or eol) */
841 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
842 && (p_line[0] != '\t') )
844 strcpy( p_index, p_line );
845 p_index += strlen( p_line );
848 if( file ) fclose( file );
852 * Save module config in file
855 file = fopen( psz_filename, "wt" );
858 msg_Warn( p_this, "could not open config file %s for writing",
860 free( psz_filename );
861 vlc_list_release( p_list );
862 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
866 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
868 /* Look for the selected module, if NULL then save everything */
869 for( i_index = 0; i_index < p_list->i_count; i_index++ )
871 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
873 if( psz_module_name && strcmp( psz_module_name,
874 p_parser->psz_object_name ) )
877 if( !p_parser->i_config_items )
880 msg_Dbg( p_this, "saving config for module \"%s\"",
881 p_parser->psz_object_name );
883 fprintf( file, "[%s]", p_parser->psz_object_name );
884 if( p_parser->psz_longname )
885 fprintf( file, " # %s\n\n", p_parser->psz_longname );
887 fprintf( file, "\n\n" );
889 for( p_item = p_parser->p_config;
890 p_item->i_type != CONFIG_HINT_END;
893 if( p_item->i_type & CONFIG_HINT )
897 switch( p_item->i_type )
899 case CONFIG_ITEM_BOOL:
900 case CONFIG_ITEM_INTEGER:
901 if( p_item->psz_text )
902 fprintf( file, "# %s (%s)\n", p_item->psz_text,
903 (p_item->i_type == CONFIG_ITEM_BOOL) ?
904 _("boolean") : _("integer") );
905 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
908 case CONFIG_ITEM_FLOAT:
909 if( p_item->psz_text )
910 fprintf( file, "# %s (%s)\n", p_item->psz_text,
912 fprintf( file, "%s=%f\n", p_item->psz_name,
913 (double)p_item->f_value );
917 if( p_item->psz_text )
918 fprintf( file, "# %s (%s)\n", p_item->psz_text,
920 fprintf( file, "%s=%s\n", p_item->psz_name,
921 p_item->psz_value ? p_item->psz_value : "" );
925 fprintf( file, "\n" );
928 vlc_list_release( p_list );
931 * Restore old settings from the config in file
933 fputs( p_bigbuffer, file );
937 free( psz_filename );
938 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
943 /*****************************************************************************
944 * config_LoadCmdLine: parse command line
945 *****************************************************************************
946 * Parse command line for configuration options.
947 * Now that the module_bank has been initialized, we can dynamically
948 * generate the longopts structure used by getops. We have to do it this way
949 * because we don't know (and don't want to know) in advance the configuration
950 * options used (ie. exported) by each module.
951 *****************************************************************************/
952 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
953 vlc_bool_t b_ignore_errors )
955 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
958 module_config_t *p_item;
959 struct option *p_longopts;
963 module_config_t *pp_shortopts[256];
966 /* Set default configuration and copy arguments */
967 p_this->p_vlc->i_argc = *pi_argc;
968 p_this->p_vlc->ppsz_argv = ppsz_argv;
970 p_this->p_vlc->p_channel = NULL;
973 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
974 * is the PSN - process serial number (a unique PID-ish thingie)
975 * still ok for real Darwin & when run from command line */
976 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
977 /* for example -psn_0_9306113 */
979 /* GDMF!... I can't do this or else the MacOSX window server will
980 * not pick up the PSN and not register the app and we crash...
981 * hence the following kludge otherwise we'll get confused w/ argv[1]
982 * being an input file name */
984 ppsz_argv[ 1 ] = NULL;
986 *pi_argc = *pi_argc - 1;
992 /* List all modules */
993 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
996 * Generate the longopts and shortopts structures used by getopt_long
1000 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1003 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1005 /* count the number of exported configuration options (to allocate
1006 * longopts). We also need to allocate space for too options when
1007 * dealing with boolean to allow for --foo and --no-foo */
1008 i_opts += p_parser->i_config_items
1009 + 2 * p_parser->i_bool_items;
1012 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1013 if( p_longopts == NULL )
1015 msg_Err( p_this, "out of memory" );
1016 vlc_list_release( p_list );
1020 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1021 if( psz_shortopts == NULL )
1023 msg_Err( p_this, "out of memory" );
1025 vlc_list_release( p_list );
1029 /* If we are requested to ignore errors, then we must work on a copy
1030 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1031 * us, ignoring the arity of the options */
1032 if( b_ignore_errors )
1034 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1035 if( ppsz_argv == NULL )
1037 msg_Err( p_this, "out of memory" );
1038 free( psz_shortopts );
1040 vlc_list_release( p_list );
1043 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1044 *pi_argc * sizeof(char *) );
1048 for( i_index = 0; i_index < 256; i_index++ )
1050 pp_shortopts[i_index] = NULL;
1053 /* Fill the p_longopts and psz_shortopts structures */
1055 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1058 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1060 if( !p_parser->i_config_items )
1063 for( p_item = p_parser->p_config;
1064 p_item->i_type != CONFIG_HINT_END;
1068 if( p_item->i_type & CONFIG_HINT )
1071 /* Add item to long options */
1072 p_longopts[i_index].name = strdup( p_item->psz_name );
1073 if( p_longopts[i_index].name == NULL ) continue;
1074 p_longopts[i_index].has_arg =
1075 (p_item->i_type == CONFIG_ITEM_BOOL)?
1076 no_argument : required_argument;
1077 p_longopts[i_index].flag = &flag;
1078 p_longopts[i_index].val = 0;
1081 /* When dealing with bools we also need to add the --no-foo
1083 if( p_item->i_type == CONFIG_ITEM_BOOL )
1085 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1086 if( psz_name == NULL ) continue;
1087 strcpy( psz_name, "no" );
1088 strcat( psz_name, p_item->psz_name );
1090 p_longopts[i_index].name = psz_name;
1091 p_longopts[i_index].has_arg = no_argument;
1092 p_longopts[i_index].flag = &flag;
1093 p_longopts[i_index].val = 1;
1096 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1097 if( psz_name == NULL ) continue;
1098 strcpy( psz_name, "no-" );
1099 strcat( psz_name, p_item->psz_name );
1101 p_longopts[i_index].name = psz_name;
1102 p_longopts[i_index].has_arg = no_argument;
1103 p_longopts[i_index].flag = &flag;
1104 p_longopts[i_index].val = 1;
1108 /* If item also has a short option, add it */
1109 if( p_item->i_short )
1111 pp_shortopts[(int)p_item->i_short] = p_item;
1112 psz_shortopts[i_shortopts] = p_item->i_short;
1114 if( p_item->i_type != CONFIG_ITEM_BOOL )
1116 psz_shortopts[i_shortopts] = ':';
1119 if( p_item->i_short == 'v' )
1121 psz_shortopts[i_shortopts] = ':';
1129 /* We don't need the module list anymore */
1130 vlc_list_release( p_list );
1132 /* Close the longopts and shortopts structures */
1133 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1134 psz_shortopts[i_shortopts] = '\0';
1137 * Parse the command line options
1141 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1142 p_longopts, &i_index ) ) != EOF )
1144 /* A long option has been recognized */
1147 module_config_t *p_conf;
1148 char *psz_name = (char *)p_longopts[i_index].name;
1150 /* Check if we deal with a --nofoo or --no-foo long option */
1151 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1153 /* Store the configuration option */
1154 p_conf = config_FindConfig( p_this, psz_name );
1156 if( p_conf ) switch( p_conf->i_type )
1158 case CONFIG_ITEM_STRING:
1159 case CONFIG_ITEM_FILE:
1160 case CONFIG_ITEM_DIRECTORY:
1161 case CONFIG_ITEM_MODULE:
1162 config_PutPsz( p_this, psz_name, optarg );
1164 case CONFIG_ITEM_INTEGER:
1165 config_PutInt( p_this, psz_name, atoi(optarg));
1167 case CONFIG_ITEM_FLOAT:
1168 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1170 case CONFIG_ITEM_BOOL:
1171 config_PutInt( p_this, psz_name, !flag );
1178 /* A short option has been recognized */
1179 if( pp_shortopts[i_cmd] != NULL )
1181 switch( pp_shortopts[i_cmd]->i_type )
1183 case CONFIG_ITEM_STRING:
1184 case CONFIG_ITEM_FILE:
1185 case CONFIG_ITEM_DIRECTORY:
1186 case CONFIG_ITEM_MODULE:
1187 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1189 case CONFIG_ITEM_INTEGER:
1194 if( *optarg == 'v' ) /* eg. -vvv */
1197 while( *optarg == 'v' )
1205 i_verbose += atoi( optarg ); /* eg. -v2 */
1210 i_verbose++; /* -v */
1212 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1217 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1221 case CONFIG_ITEM_BOOL:
1222 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1229 /* Internal error: unknown option */
1230 if( !b_ignore_errors )
1232 fprintf( stderr, "%s: unknown option ",
1233 p_this->p_vlc->psz_object_name );
1236 fprintf( stderr, "`-%c'\n", optopt );
1240 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1242 fprintf( stderr, "Try `%s --help' for more information.\n",
1243 p_this->p_vlc->psz_object_name );
1246 free( psz_shortopts );
1247 if( b_ignore_errors ) free( ppsz_argv );
1252 /* Free allocated resources */
1253 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1254 free( (char *)p_longopts[i_index].name );
1256 free( psz_shortopts );
1257 if( b_ignore_errors ) free( ppsz_argv );
1262 /*****************************************************************************
1263 * config_GetHomeDir: find the user's home directory.
1264 *****************************************************************************
1265 * This function will try by different ways to find the user's home path.
1266 * Note that this function is not reentrant, it should be called only once
1267 * at the beginning of main where the result will be stored for later use.
1268 *****************************************************************************/
1269 char *config_GetHomeDir( void )
1271 char *p_tmp, *p_homedir = NULL;
1273 #if defined(HAVE_GETPWUID)
1274 struct passwd *p_pw = NULL;
1277 #if defined(WIN32) || defined(UNDER_CE)
1278 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1280 # define CSIDL_FLAG_CREATE 0x8000
1281 # define CSIDL_APPDATA 0x1A
1282 # define SHGFP_TYPE_CURRENT 0
1284 HINSTANCE shfolder_dll;
1285 SHGETFOLDERPATH SHGetFolderPath ;
1287 /* load the shell32 dll to retreive SHGetFolderPath */
1288 if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1290 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1291 "SHGetFolderPathA" );
1292 if ( SHGetFolderPath != NULL )
1294 p_homedir = (char *)malloc( MAX_PATH );
1300 /* get the "Application Data" folder for the current user */
1301 if( S_OK == SHGetFolderPath( NULL,
1302 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1303 NULL, SHGFP_TYPE_CURRENT,
1306 FreeLibrary( shfolder_dll );
1311 FreeLibrary( shfolder_dll );
1315 #if defined(HAVE_GETPWUID)
1316 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1319 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1321 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1327 p_homedir = strdup( p_tmp );
1329 #if defined(HAVE_GETPWUID)
1332 p_homedir = strdup( p_pw->pw_dir );