1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.60 2003/07/23 01:13:48 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 *****************************************************************************/
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_GetType: get the type of a variable (bool, int, float, string)
61 *****************************************************************************
62 * This function is used to get the type of a variable from its name.
63 * Beware, this is quite slow.
64 *****************************************************************************/
65 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
67 module_config_t *p_config;
70 p_config = config_FindConfig( p_this, psz_name );
78 switch( p_config->i_type )
80 case CONFIG_ITEM_BOOL:
81 i_type = VLC_VAR_BOOL;
84 case CONFIG_ITEM_INTEGER:
85 i_type = VLC_VAR_INTEGER;
88 case CONFIG_ITEM_FLOAT:
89 i_type = VLC_VAR_FLOAT;
92 case CONFIG_ITEM_MODULE:
93 case CONFIG_ITEM_STRING:
94 i_type = VLC_VAR_STRING;
97 case CONFIG_ITEM_FILE:
98 i_type = VLC_VAR_FILE;
101 case CONFIG_ITEM_DIRECTORY:
102 i_type = VLC_VAR_DIRECTORY;
113 /*****************************************************************************
114 * config_GetInt: get the value of an int variable
115 *****************************************************************************
116 * This function is used to get the value of variables which are internally
117 * represented by an integer (CONFIG_ITEM_INTEGER and
119 *****************************************************************************/
120 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
122 module_config_t *p_config;
124 p_config = config_FindConfig( p_this, psz_name );
129 msg_Err( p_this, "option %s does not exist", psz_name );
132 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
133 (p_config->i_type!=CONFIG_ITEM_BOOL) )
135 msg_Err( p_this, "option %s does not refer to an int", psz_name );
139 return p_config->i_value;
142 /*****************************************************************************
143 * config_GetFloat: get the value of a float variable
144 *****************************************************************************
145 * This function is used to get the value of variables which are internally
146 * represented by a float (CONFIG_ITEM_FLOAT).
147 *****************************************************************************/
148 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
150 module_config_t *p_config;
152 p_config = config_FindConfig( p_this, psz_name );
157 msg_Err( p_this, "option %s does not exist", psz_name );
160 if( p_config->i_type != CONFIG_ITEM_FLOAT )
162 msg_Err( p_this, "option %s does not refer to a float", psz_name );
166 return p_config->f_value;
169 /*****************************************************************************
170 * config_GetPsz: get the string value of a string variable
171 *****************************************************************************
172 * This function is used to get the value of variables which are internally
173 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
174 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
176 * Important note: remember to free() the returned char* because it's a
177 * duplicate of the actual value. It isn't safe to return a pointer to the
178 * actual value as it can be modified at any time.
179 *****************************************************************************/
180 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
182 module_config_t *p_config;
183 char *psz_value = NULL;
185 p_config = config_FindConfig( p_this, psz_name );
190 msg_Err( p_this, "option %s does not exist", psz_name );
193 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
194 (p_config->i_type!=CONFIG_ITEM_FILE) &&
195 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
196 (p_config->i_type!=CONFIG_ITEM_MODULE) )
198 msg_Err( p_this, "option %s does not refer to a string", psz_name );
202 /* return a copy of the string */
203 vlc_mutex_lock( p_config->p_lock );
204 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
205 vlc_mutex_unlock( p_config->p_lock );
210 /*****************************************************************************
211 * config_PutPsz: set the string value of a string variable
212 *****************************************************************************
213 * This function is used to set the value of variables which are internally
214 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
215 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
216 *****************************************************************************/
217 void __config_PutPsz( vlc_object_t *p_this,
218 const char *psz_name, const char *psz_value )
220 module_config_t *p_config;
222 p_config = config_FindConfig( p_this, psz_name );
227 msg_Warn( p_this, "option %s does not exist", psz_name );
230 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
231 (p_config->i_type!=CONFIG_ITEM_FILE) &&
232 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
233 (p_config->i_type!=CONFIG_ITEM_MODULE) )
235 msg_Err( p_this, "option %s does not refer to a string", psz_name );
239 vlc_mutex_lock( p_config->p_lock );
241 /* free old string */
242 if( p_config->psz_value ) free( p_config->psz_value );
244 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
245 else p_config->psz_value = NULL;
247 vlc_mutex_unlock( p_config->p_lock );
249 if( p_config->pf_callback )
251 p_config->pf_callback( p_this );
255 /*****************************************************************************
256 * config_PutInt: set the integer value of an int variable
257 *****************************************************************************
258 * This function is used to set the value of variables which are internally
259 * represented by an integer (CONFIG_ITEM_INTEGER and
261 *****************************************************************************/
262 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
264 module_config_t *p_config;
266 p_config = config_FindConfig( p_this, psz_name );
271 msg_Warn( p_this, "option %s does not exist", psz_name );
274 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
275 (p_config->i_type!=CONFIG_ITEM_BOOL) )
277 msg_Err( p_this, "option %s does not refer to an int", psz_name );
281 /* if i_min == i_max == 0, then do not use them */
282 if ((p_config->i_min == 0) && (p_config->i_max == 0))
284 p_config->i_value = i_value;
286 else if (i_value < p_config->i_min)
288 p_config->i_value = p_config->i_min;
290 else if (i_value > p_config->i_max)
292 p_config->i_value = p_config->i_max;
296 p_config->i_value = i_value;
299 if( p_config->pf_callback )
301 p_config->pf_callback( p_this );
305 /*****************************************************************************
306 * config_PutFloat: set the value of a float variable
307 *****************************************************************************
308 * This function is used to set the value of variables which are internally
309 * represented by a float (CONFIG_ITEM_FLOAT).
310 *****************************************************************************/
311 void __config_PutFloat( vlc_object_t *p_this,
312 const char *psz_name, float f_value )
314 module_config_t *p_config;
316 p_config = config_FindConfig( p_this, psz_name );
321 msg_Warn( p_this, "option %s does not exist", psz_name );
324 if( p_config->i_type != CONFIG_ITEM_FLOAT )
326 msg_Err( p_this, "option %s does not refer to a float", psz_name );
330 /* if f_min == f_max == 0, then do not use them */
331 if ((p_config->f_min == 0) && (p_config->f_max == 0))
333 p_config->f_value = f_value;
335 else if (f_value < p_config->f_min)
337 p_config->f_value = p_config->f_min;
339 else if (f_value > p_config->f_max)
341 p_config->f_value = p_config->f_max;
345 p_config->f_value = f_value;
348 if( p_config->pf_callback )
350 p_config->pf_callback( p_this );
354 /*****************************************************************************
355 * config_FindConfig: find the config structure associated with an option.
356 *****************************************************************************
357 * FIXME: This function really needs to be optimized.
358 * FIXME: And now even more.
359 *****************************************************************************/
360 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
364 module_config_t *p_item;
367 if( !psz_name ) return NULL;
369 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
371 for( i_index = 0; i_index < p_list->i_count; i_index++ )
373 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
375 if( !p_parser->i_config_items )
378 for( p_item = p_parser->p_config;
379 p_item->i_type != CONFIG_HINT_END;
382 if( p_item->i_type & CONFIG_HINT )
385 if( !strcmp( psz_name, p_item->psz_name ) )
387 vlc_list_release( p_list );
393 vlc_list_release( p_list );
398 /*****************************************************************************
399 * config_Duplicate: creates a duplicate of a module's configuration data.
400 *****************************************************************************
401 * Unfortunatly we cannot work directly with the module's config data as
402 * this module might be unloaded from memory at any time (remember HideModule).
403 * This is why we need to create an exact copy of the config data.
404 *****************************************************************************/
405 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
407 int i, j, i_lines = 1;
408 module_config_t *p_item;
410 /* Calculate the structure length */
411 p_module->i_config_items = 0;
412 p_module->i_bool_items = 0;
414 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
418 if( p_item->i_type & CONFIG_ITEM )
420 p_module->i_config_items++;
423 if( p_item->i_type == CONFIG_ITEM_BOOL )
425 p_module->i_bool_items++;
429 /* Allocate memory */
430 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
432 if( p_module->p_config == NULL )
434 msg_Err( p_module, "config error: can't duplicate p_config" );
438 /* Do the duplication job */
439 for( i = 0; i < i_lines ; i++ )
441 p_module->p_config[i].i_type = p_orig[i].i_type;
442 p_module->p_config[i].i_short = p_orig[i].i_short;
443 p_module->p_config[i].i_value = p_orig[i].i_value;
444 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
445 p_module->p_config[i].i_min = p_orig[i].i_min;
446 p_module->p_config[i].i_max = p_orig[i].i_max;
447 p_module->p_config[i].f_value = p_orig[i].f_value;
448 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
449 p_module->p_config[i].f_min = p_orig[i].f_min;
450 p_module->p_config[i].f_max = p_orig[i].f_max;
451 p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
452 p_module->p_config[i].b_advanced = p_orig[i].b_advanced;
454 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
455 strdup( p_orig[i].psz_type ) : NULL;
456 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
457 strdup( p_orig[i].psz_name ) : NULL;
458 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
459 strdup( _(p_orig[i].psz_text) ) : NULL;
460 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
461 strdup( _(p_orig[i].psz_longtext) ) : NULL;
462 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
463 strdup( p_orig[i].psz_value ) : NULL;
464 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
465 strdup( p_orig[i].psz_value ) : NULL;
467 p_module->p_config[i].p_lock = &p_module->object_lock;
469 /* duplicate the string list */
470 p_module->p_config[i].ppsz_list = NULL;
471 if( p_orig[i].ppsz_list )
473 for( j = 0; p_orig[i].ppsz_list[j]; j++ );
474 p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
475 if( p_module->p_config[i].ppsz_list )
477 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
478 p_module->p_config[i].ppsz_list[j] =
479 strdup( p_orig[i].ppsz_list[j] );
481 p_module->p_config[i].ppsz_list[j] = NULL;
484 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
488 /*****************************************************************************
489 * config_Free: frees a duplicated module's configuration data.
490 *****************************************************************************
491 * This function frees all the data duplicated by config_Duplicate.
492 *****************************************************************************/
493 void config_Free( module_t *p_module )
495 module_config_t *p_item = p_module->p_config;
503 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
505 if( p_item->psz_type )
506 free( p_item->psz_type );
508 if( p_item->psz_name )
509 free( p_item->psz_name );
511 if( p_item->psz_text )
512 free( p_item->psz_text );
514 if( p_item->psz_longtext )
515 free( p_item->psz_longtext );
517 if( p_item->psz_value )
518 free( p_item->psz_value );
520 if( p_item->psz_value_orig )
521 free( p_item->psz_value_orig );
523 if( p_item->ppsz_list )
525 for( i = 0; p_item->ppsz_list[i]; i++ )
526 free(p_item->ppsz_list[i]);
527 free( p_item->ppsz_list );
531 free( p_module->p_config );
532 p_module->p_config = NULL;
535 /*****************************************************************************
536 * config_SetCallbacks: sets callback functions in the duplicate p_config.
537 *****************************************************************************
538 * Unfortunatly we cannot work directly with the module's config data as
539 * this module might be unloaded from memory at any time (remember HideModule).
540 * This is why we need to duplicate callbacks each time we reload the module.
541 *****************************************************************************/
542 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
544 while( p_new->i_type != CONFIG_HINT_END )
546 p_new->pf_callback = p_orig->pf_callback;
552 /*****************************************************************************
553 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
554 *****************************************************************************
555 * We simply undo what we did in config_SetCallbacks.
556 *****************************************************************************/
557 void config_UnsetCallbacks( module_config_t *p_new )
559 while( p_new->i_type != CONFIG_HINT_END )
561 p_new->pf_callback = NULL;
566 /*****************************************************************************
567 * config_ResetAll: reset the configuration data for all the modules.
568 *****************************************************************************/
569 void __config_ResetAll( vlc_object_t *p_this )
575 /* Acquire config file lock */
576 vlc_mutex_lock( &p_this->p_vlc->config_lock );
578 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
580 for( i_index = 0; i_index < p_list->i_count; i_index++ )
582 p_module = (module_t *)p_list->p_values[i_index].p_object ;
583 if( p_module->b_submodule ) continue;
585 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
587 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
588 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
589 if( p_module->p_config[i].psz_value )
590 free( p_module->p_config[i].psz_value );
591 p_module->p_config[i].psz_value =
592 p_module->p_config[i].psz_value_orig ?
593 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
597 vlc_list_release( p_list );
598 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
601 /*****************************************************************************
602 * config_LoadConfigFile: loads the configuration file.
603 *****************************************************************************
604 * This function is called to load the config options stored in the config
606 *****************************************************************************/
607 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
611 module_config_t *p_item;
614 char *p_index, *psz_option_name, *psz_option_value;
615 char *psz_filename, *psz_homedir;
618 psz_homedir = p_this->p_vlc->psz_homedir;
621 msg_Err( p_this, "psz_homedir is null" );
624 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
625 strlen(psz_homedir) + 1 );
628 msg_Err( p_this, "out of memory" );
631 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
633 msg_Dbg( p_this, "opening config file %s", psz_filename );
635 /* Acquire config file lock */
636 vlc_mutex_lock( &p_this->p_vlc->config_lock );
638 file = fopen( psz_filename, "rt" );
641 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
642 free( psz_filename );
643 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
647 /* Look for the selected module, if NULL then save everything */
648 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
650 for( i_index = 0; i_index < p_list->i_count; i_index++ )
652 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
655 && strcmp( psz_module_name, p_parser->psz_object_name ) )
660 /* The config file is organized in sections, one per module. Look for
661 * the interesting section ( a section is of the form [foo] ) */
662 fseek( file, 0L, SEEK_SET );
663 while( fgets( line, 1024, file ) )
666 && (p_index = strchr(line,']'))
667 && (p_index - &line[1]
668 == (int)strlen(p_parser->psz_object_name))
669 && !memcmp( &line[1], p_parser->psz_object_name,
670 strlen(p_parser->psz_object_name) ) )
673 msg_Dbg( p_this, "loading config for module \"%s\"",
674 p_parser->psz_object_name );
680 /* either we found the section or we're at the EOF */
682 /* Now try to load the options in this section */
683 while( fgets( line, 1024, file ) )
685 if( line[0] == '[' ) break; /* end of section */
687 /* ignore comments or empty lines */
688 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
691 /* get rid of line feed */
692 if( line[strlen(line)-1] == '\n' )
693 line[strlen(line)-1] = (char)0;
695 /* look for option name */
696 psz_option_name = line;
697 psz_option_value = NULL;
698 p_index = strchr( line, '=' );
699 if( !p_index ) break; /* this ain't an option!!! */
702 psz_option_value = p_index + 1;
704 if( !p_parser->i_config_items )
709 /* try to match this option with one of the module's options */
710 for( p_item = p_parser->p_config;
711 p_item->i_type != CONFIG_HINT_END;
714 if( p_item->i_type & CONFIG_HINT )
718 if( !strcmp( p_item->psz_name, psz_option_name ) )
721 switch( p_item->i_type )
723 case CONFIG_ITEM_BOOL:
724 case CONFIG_ITEM_INTEGER:
725 if( !*psz_option_value )
726 break; /* ignore empty option */
727 p_item->i_value = atoi( psz_option_value);
729 msg_Dbg( p_this, "option \"%s\", value %i",
730 p_item->psz_name, p_item->i_value );
734 case CONFIG_ITEM_FLOAT:
735 if( !*psz_option_value )
736 break; /* ignore empty option */
737 p_item->f_value = (float)atof( psz_option_value);
739 msg_Dbg( p_this, "option \"%s\", value %f",
740 p_item->psz_name, (double)p_item->f_value );
745 vlc_mutex_lock( p_item->p_lock );
747 /* free old string */
748 if( p_item->psz_value )
749 free( p_item->psz_value );
751 p_item->psz_value = *psz_option_value ?
752 strdup( psz_option_value ) : NULL;
754 vlc_mutex_unlock( p_item->p_lock );
757 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
759 p_item->psz_value ? p_item->psz_value : "" );
769 vlc_list_release( p_list );
772 free( psz_filename );
774 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
779 /*****************************************************************************
780 * config_SaveConfigFile: Save a module's config options.
781 *****************************************************************************
782 * This will save the specified module's config options to the config file.
783 * If psz_module_name is NULL then we save all the modules config options.
784 * It's no use to save the config options that kept their default values, so
785 * we'll try to be a bit clever here.
787 * When we save we mustn't delete the config options of the modules that
788 * haven't been loaded. So we cannot just create a new config file with the
789 * config structures we've got in memory.
790 * I don't really know how to deal with this nicely, so I will use a completly
792 * I will load the config file in memory, but skipping all the sections of the
793 * modules we want to save. Then I will create a brand new file, dump the file
794 * loaded in memory and then append the sections of the modules we want to
797 *****************************************************************************/
798 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
802 module_config_t *p_item;
804 char p_line[1024], *p_index2;
806 char *p_bigbuffer, *p_index;
808 char *psz_filename, *psz_homedir;
811 /* Acquire config file lock */
812 vlc_mutex_lock( &p_this->p_vlc->config_lock );
814 psz_homedir = p_this->p_vlc->psz_homedir;
817 msg_Err( p_this, "psz_homedir is null" );
818 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
821 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
822 strlen(psz_homedir) + 1 );
825 msg_Err( p_this, "out of memory" );
826 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
829 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
831 #if defined( UNDER_CE )
833 wchar_t psz_new[ MAX_PATH ];
834 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
835 if( CreateDirectory( psz_new, NULL ) )
837 msg_Err( p_this, "could not create %s", psz_filename );
841 #elif defined( HAVE_ERRNO_H )
842 # if defined( WIN32 )
843 if( mkdir( psz_filename ) && errno != EEXIST )
845 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
848 msg_Err( p_this, "could not create %s (%s)",
849 psz_filename, strerror(errno) );
853 if( mkdir( psz_filename ) )
855 msg_Err( p_this, "could not create %s", psz_filename );
860 strcat( psz_filename, "/" CONFIG_FILE );
863 msg_Dbg( p_this, "opening config file %s", psz_filename );
865 file = fopen( psz_filename, "rt" );
868 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
872 /* look for file size */
873 fseek( file, 0L, SEEK_END );
874 i_sizebuf = ftell( file );
875 fseek( file, 0L, SEEK_SET );
878 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
881 msg_Err( p_this, "out of memory" );
882 if( file ) fclose( file );
883 free( psz_filename );
884 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
889 /* List all available modules */
890 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
892 /* backup file into memory, we only need to backup the sections we won't
895 while( file && fgets( p_line, 1024, file ) )
897 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
900 /* we found a section, check if we need to do a backup */
901 for( i_index = 0; i_index < p_list->i_count; i_index++ )
903 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
905 if( ((p_index2 - &p_line[1])
906 == (int)strlen(p_parser->psz_object_name) )
907 && !memcmp( &p_line[1], p_parser->psz_object_name,
908 strlen(p_parser->psz_object_name) ) )
910 if( !psz_module_name )
912 else if( !strcmp( psz_module_name,
913 p_parser->psz_object_name ) )
918 if( i_index == p_list->i_count )
920 /* we don't have this section in our list so we need to back
923 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
935 /* save line if requested and line is valid (doesn't begin with a
936 * space, tab, or eol) */
937 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
938 && (p_line[0] != '\t') )
940 strcpy( p_index, p_line );
941 p_index += strlen( p_line );
944 if( file ) fclose( file );
948 * Save module config in file
951 file = fopen( psz_filename, "wt" );
954 msg_Warn( p_this, "could not open config file %s for writing",
956 free( psz_filename );
957 vlc_list_release( p_list );
958 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
962 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
964 /* Look for the selected module, if NULL then save everything */
965 for( i_index = 0; i_index < p_list->i_count; i_index++ )
967 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
969 if( psz_module_name && strcmp( psz_module_name,
970 p_parser->psz_object_name ) )
973 if( !p_parser->i_config_items )
976 msg_Dbg( p_this, "saving config for module \"%s\"",
977 p_parser->psz_object_name );
979 fprintf( file, "[%s]", p_parser->psz_object_name );
980 if( p_parser->psz_longname )
981 fprintf( file, " # %s\n\n", p_parser->psz_longname );
983 fprintf( file, "\n\n" );
985 for( p_item = p_parser->p_config;
986 p_item->i_type != CONFIG_HINT_END;
989 if( p_item->i_type & CONFIG_HINT )
993 switch( p_item->i_type )
995 case CONFIG_ITEM_BOOL:
996 case CONFIG_ITEM_INTEGER:
997 if( p_item->psz_text )
998 fprintf( file, "# %s (%s)\n", p_item->psz_text,
999 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1000 _("boolean") : _("integer") );
1001 if( p_item->i_value == p_item->i_value_orig )
1002 fprintf( file, "#" );
1003 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
1006 case CONFIG_ITEM_FLOAT:
1007 if( p_item->psz_text )
1008 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1010 if( p_item->f_value == p_item->f_value_orig )
1011 fprintf( file, "#" );
1012 fprintf( file, "%s=%f\n", p_item->psz_name,
1013 (double)p_item->f_value );
1017 if( p_item->psz_text )
1018 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1020 if( (!p_item->psz_value && !p_item->psz_value_orig) ||
1021 (p_item->psz_value && p_item->psz_value_orig &&
1022 !strcmp( p_item->psz_value, p_item->psz_value_orig )) )
1023 fprintf( file, "#" );
1024 fprintf( file, "%s=%s\n", p_item->psz_name,
1025 p_item->psz_value ? p_item->psz_value : "" );
1029 fprintf( file, "\n" );
1032 vlc_list_release( p_list );
1035 * Restore old settings from the config in file
1037 fputs( p_bigbuffer, file );
1038 free( p_bigbuffer );
1041 free( psz_filename );
1042 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1047 /*****************************************************************************
1048 * config_LoadCmdLine: parse command line
1049 *****************************************************************************
1050 * Parse command line for configuration options.
1051 * Now that the module_bank has been initialized, we can dynamically
1052 * generate the longopts structure used by getops. We have to do it this way
1053 * because we don't know (and don't want to know) in advance the configuration
1054 * options used (ie. exported) by each module.
1055 *****************************************************************************/
1056 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1057 vlc_bool_t b_ignore_errors )
1059 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1062 module_config_t *p_item;
1063 struct option *p_longopts;
1064 int i_modules_index;
1067 module_config_t *pp_shortopts[256];
1068 char *psz_shortopts;
1070 /* Set default configuration and copy arguments */
1071 p_this->p_vlc->i_argc = *pi_argc;
1072 p_this->p_vlc->ppsz_argv = ppsz_argv;
1074 p_this->p_vlc->p_channel = NULL;
1077 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1078 * is the PSN - process serial number (a unique PID-ish thingie)
1079 * still ok for real Darwin & when run from command line */
1080 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1081 /* for example -psn_0_9306113 */
1083 /* GDMF!... I can't do this or else the MacOSX window server will
1084 * not pick up the PSN and not register the app and we crash...
1085 * hence the following kludge otherwise we'll get confused w/ argv[1]
1086 * being an input file name */
1088 ppsz_argv[ 1 ] = NULL;
1090 *pi_argc = *pi_argc - 1;
1096 /* List all modules */
1097 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1100 * Generate the longopts and shortopts structures used by getopt_long
1104 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1107 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1109 /* count the number of exported configuration options (to allocate
1110 * longopts). We also need to allocate space for too options when
1111 * dealing with boolean to allow for --foo and --no-foo */
1112 i_opts += p_parser->i_config_items
1113 + 2 * p_parser->i_bool_items;
1116 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1117 if( p_longopts == NULL )
1119 msg_Err( p_this, "out of memory" );
1120 vlc_list_release( p_list );
1124 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1125 if( psz_shortopts == NULL )
1127 msg_Err( p_this, "out of memory" );
1129 vlc_list_release( p_list );
1133 /* If we are requested to ignore errors, then we must work on a copy
1134 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1135 * us, ignoring the arity of the options */
1136 if( b_ignore_errors )
1138 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1139 if( ppsz_argv == NULL )
1141 msg_Err( p_this, "out of memory" );
1142 free( psz_shortopts );
1144 vlc_list_release( p_list );
1147 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1148 *pi_argc * sizeof(char *) );
1152 for( i_index = 0; i_index < 256; i_index++ )
1154 pp_shortopts[i_index] = NULL;
1157 /* Fill the p_longopts and psz_shortopts structures */
1159 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1162 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1164 if( !p_parser->i_config_items )
1167 for( p_item = p_parser->p_config;
1168 p_item->i_type != CONFIG_HINT_END;
1172 if( p_item->i_type & CONFIG_HINT )
1175 /* Add item to long options */
1176 p_longopts[i_index].name = strdup( p_item->psz_name );
1177 if( p_longopts[i_index].name == NULL ) continue;
1178 p_longopts[i_index].has_arg =
1179 (p_item->i_type == CONFIG_ITEM_BOOL)?
1180 no_argument : required_argument;
1181 p_longopts[i_index].flag = &flag;
1182 p_longopts[i_index].val = 0;
1185 /* When dealing with bools we also need to add the --no-foo
1187 if( p_item->i_type == CONFIG_ITEM_BOOL )
1189 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1190 if( psz_name == NULL ) continue;
1191 strcpy( psz_name, "no" );
1192 strcat( psz_name, p_item->psz_name );
1194 p_longopts[i_index].name = psz_name;
1195 p_longopts[i_index].has_arg = no_argument;
1196 p_longopts[i_index].flag = &flag;
1197 p_longopts[i_index].val = 1;
1200 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1201 if( psz_name == NULL ) continue;
1202 strcpy( psz_name, "no-" );
1203 strcat( psz_name, p_item->psz_name );
1205 p_longopts[i_index].name = psz_name;
1206 p_longopts[i_index].has_arg = no_argument;
1207 p_longopts[i_index].flag = &flag;
1208 p_longopts[i_index].val = 1;
1212 /* If item also has a short option, add it */
1213 if( p_item->i_short )
1215 pp_shortopts[(int)p_item->i_short] = p_item;
1216 psz_shortopts[i_shortopts] = p_item->i_short;
1218 if( p_item->i_type != CONFIG_ITEM_BOOL )
1220 psz_shortopts[i_shortopts] = ':';
1223 if( p_item->i_short == 'v' )
1225 psz_shortopts[i_shortopts] = ':';
1233 /* We don't need the module list anymore */
1234 vlc_list_release( p_list );
1236 /* Close the longopts and shortopts structures */
1237 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1238 psz_shortopts[i_shortopts] = '\0';
1241 * Parse the command line options
1245 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1246 p_longopts, &i_index ) ) != EOF )
1248 /* A long option has been recognized */
1251 module_config_t *p_conf;
1252 char *psz_name = (char *)p_longopts[i_index].name;
1254 /* Check if we deal with a --nofoo or --no-foo long option */
1255 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1257 /* Store the configuration option */
1258 p_conf = config_FindConfig( p_this, psz_name );
1260 if( p_conf ) switch( p_conf->i_type )
1262 case CONFIG_ITEM_STRING:
1263 case CONFIG_ITEM_FILE:
1264 case CONFIG_ITEM_DIRECTORY:
1265 case CONFIG_ITEM_MODULE:
1266 config_PutPsz( p_this, psz_name, optarg );
1268 case CONFIG_ITEM_INTEGER:
1269 config_PutInt( p_this, psz_name, atoi(optarg));
1271 case CONFIG_ITEM_FLOAT:
1272 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1274 case CONFIG_ITEM_BOOL:
1275 config_PutInt( p_this, psz_name, !flag );
1282 /* A short option has been recognized */
1283 if( pp_shortopts[i_cmd] != NULL )
1285 switch( pp_shortopts[i_cmd]->i_type )
1287 case CONFIG_ITEM_STRING:
1288 case CONFIG_ITEM_FILE:
1289 case CONFIG_ITEM_DIRECTORY:
1290 case CONFIG_ITEM_MODULE:
1291 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1293 case CONFIG_ITEM_INTEGER:
1298 if( *optarg == 'v' ) /* eg. -vvv */
1301 while( *optarg == 'v' )
1309 i_verbose += atoi( optarg ); /* eg. -v2 */
1314 i_verbose++; /* -v */
1316 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1321 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1325 case CONFIG_ITEM_BOOL:
1326 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1333 /* Internal error: unknown option */
1334 if( !b_ignore_errors )
1336 fprintf( stderr, "%s: unknown option ",
1337 p_this->p_vlc->psz_object_name );
1340 fprintf( stderr, "`-%c'\n", optopt );
1344 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1346 fprintf( stderr, "Try `%s --help' for more information.\n",
1347 p_this->p_vlc->psz_object_name );
1350 free( psz_shortopts );
1355 /* Free allocated resources */
1356 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1357 free( (char *)p_longopts[i_index].name );
1359 free( psz_shortopts );
1360 if( b_ignore_errors ) free( ppsz_argv );
1365 /*****************************************************************************
1366 * config_GetHomeDir: find the user's home directory.
1367 *****************************************************************************
1368 * This function will try by different ways to find the user's home path.
1369 * Note that this function is not reentrant, it should be called only once
1370 * at the beginning of main where the result will be stored for later use.
1371 *****************************************************************************/
1372 char *config_GetHomeDir( void )
1374 char *p_tmp, *p_homedir = NULL;
1376 #if defined(HAVE_GETPWUID)
1377 struct passwd *p_pw = NULL;
1380 #if defined(WIN32) || defined(UNDER_CE)
1381 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1383 # define CSIDL_FLAG_CREATE 0x8000
1384 # define CSIDL_APPDATA 0x1A
1385 # define SHGFP_TYPE_CURRENT 0
1387 HINSTANCE shfolder_dll;
1388 SHGETFOLDERPATH SHGetFolderPath ;
1390 /* load the shfolder dll to retrieve SHGetFolderPath */
1391 if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1393 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1394 "SHGetFolderPathA" );
1395 if ( SHGetFolderPath != NULL )
1397 p_homedir = (char *)malloc( MAX_PATH );
1403 /* get the "Application Data" folder for the current user */
1404 if( S_OK == SHGetFolderPath( NULL,
1405 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1406 NULL, SHGFP_TYPE_CURRENT,
1409 FreeLibrary( shfolder_dll );
1414 FreeLibrary( shfolder_dll );
1418 #if defined(HAVE_GETPWUID)
1419 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1422 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1424 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1430 p_homedir = strdup( p_tmp );
1432 #if defined(HAVE_GETPWUID)
1435 p_homedir = strdup( p_pw->pw_dir );