1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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 *****************************************************************************/
27 #include <stdio.h> /* sprintf() */
28 #include <stdlib.h> /* free(), strtol() */
29 #include <string.h> /* strdup() */
31 # include <errno.h> /* errno */
35 # include <unistd.h> /* getuid() */
38 #ifdef HAVE_GETOPT_LONG
40 # include <getopt.h> /* getopt() */
43 # include "../extras/getopt.h"
46 #if defined(HAVE_GETPWUID)
47 # include <pwd.h> /* getpwuid() */
50 #if defined( HAVE_SYS_STAT_H )
51 # include <sys/stat.h>
53 #if defined( HAVE_SYS_TYPES_H )
54 # include <sys/types.h>
57 # if !defined( UNDER_CE )
64 static int ConfigStringToKey( char * );
65 static char *ConfigKeyToString( int );
67 /*****************************************************************************
68 * config_GetType: get the type of a variable (bool, int, float, string)
69 *****************************************************************************
70 * This function is used to get the type of a variable from its name.
71 * Beware, this is quite slow.
72 *****************************************************************************/
73 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
75 module_config_t *p_config;
78 p_config = config_FindConfig( p_this, psz_name );
86 switch( p_config->i_type )
88 case CONFIG_ITEM_BOOL:
89 i_type = VLC_VAR_BOOL;
92 case CONFIG_ITEM_INTEGER:
93 i_type = VLC_VAR_INTEGER;
96 case CONFIG_ITEM_FLOAT:
97 i_type = VLC_VAR_FLOAT;
100 case CONFIG_ITEM_MODULE:
101 i_type = VLC_VAR_MODULE;
104 case CONFIG_ITEM_STRING:
105 i_type = VLC_VAR_STRING;
108 case CONFIG_ITEM_FILE:
109 i_type = VLC_VAR_FILE;
112 case CONFIG_ITEM_DIRECTORY:
113 i_type = VLC_VAR_DIRECTORY;
124 /*****************************************************************************
125 * config_GetInt: get the value of an int variable
126 *****************************************************************************
127 * This function is used to get the value of variables which are internally
128 * represented by an integer (CONFIG_ITEM_INTEGER and
130 *****************************************************************************/
131 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
133 module_config_t *p_config;
135 p_config = config_FindConfig( p_this, psz_name );
140 msg_Err( p_this, "option %s does not exist", psz_name );
143 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
144 (p_config->i_type!=CONFIG_ITEM_KEY) &&
145 (p_config->i_type!=CONFIG_ITEM_BOOL) )
147 msg_Err( p_this, "option %s does not refer to an int", psz_name );
151 return p_config->i_value;
154 /*****************************************************************************
155 * config_GetFloat: get the value of a float variable
156 *****************************************************************************
157 * This function is used to get the value of variables which are internally
158 * represented by a float (CONFIG_ITEM_FLOAT).
159 *****************************************************************************/
160 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
162 module_config_t *p_config;
164 p_config = config_FindConfig( p_this, psz_name );
169 msg_Err( p_this, "option %s does not exist", psz_name );
172 if( p_config->i_type != CONFIG_ITEM_FLOAT )
174 msg_Err( p_this, "option %s does not refer to a float", psz_name );
178 return p_config->f_value;
181 /*****************************************************************************
182 * config_GetPsz: get the string value of a string variable
183 *****************************************************************************
184 * This function is used to get the value of variables which are internally
185 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
186 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
188 * Important note: remember to free() the returned char* because it's a
189 * duplicate of the actual value. It isn't safe to return a pointer to the
190 * actual value as it can be modified at any time.
191 *****************************************************************************/
192 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
194 module_config_t *p_config;
195 char *psz_value = NULL;
197 p_config = config_FindConfig( p_this, psz_name );
202 msg_Err( p_this, "option %s does not exist", psz_name );
205 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
206 (p_config->i_type!=CONFIG_ITEM_FILE) &&
207 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
208 (p_config->i_type!=CONFIG_ITEM_MODULE) )
210 msg_Err( p_this, "option %s does not refer to a string", psz_name );
214 /* return a copy of the string */
215 vlc_mutex_lock( p_config->p_lock );
216 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
217 vlc_mutex_unlock( p_config->p_lock );
222 /*****************************************************************************
223 * config_PutPsz: set the string value of a string variable
224 *****************************************************************************
225 * This function is used to set the value of variables which are internally
226 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
227 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
228 *****************************************************************************/
229 void __config_PutPsz( vlc_object_t *p_this,
230 const char *psz_name, const char *psz_value )
232 module_config_t *p_config;
233 vlc_value_t oldval, val;
235 p_config = config_FindConfig( p_this, psz_name );
240 msg_Warn( p_this, "option %s does not exist", psz_name );
243 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
244 (p_config->i_type!=CONFIG_ITEM_FILE) &&
245 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
246 (p_config->i_type!=CONFIG_ITEM_MODULE) )
248 msg_Err( p_this, "option %s does not refer to a string", psz_name );
252 vlc_mutex_lock( p_config->p_lock );
254 /* backup old value */
255 oldval.psz_string = p_config->psz_value;
257 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
258 else p_config->psz_value = NULL;
260 val.psz_string = p_config->psz_value;
262 vlc_mutex_unlock( p_config->p_lock );
264 if( p_config->pf_callback )
266 p_config->pf_callback( p_this, psz_name, oldval, val,
267 p_config->p_callback_data );
270 /* free old string */
271 if( oldval.psz_string ) free( oldval.psz_string );
274 /*****************************************************************************
275 * config_PutInt: set the integer value of an int variable
276 *****************************************************************************
277 * This function is used to set the value of variables which are internally
278 * represented by an integer (CONFIG_ITEM_INTEGER and
280 *****************************************************************************/
281 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
283 module_config_t *p_config;
284 vlc_value_t oldval, val;
286 p_config = config_FindConfig( p_this, psz_name );
291 msg_Warn( p_this, "option %s does not exist", psz_name );
294 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
295 (p_config->i_type!=CONFIG_ITEM_KEY) &&
296 (p_config->i_type!=CONFIG_ITEM_BOOL) )
298 msg_Err( p_this, "option %s does not refer to an int", psz_name );
302 /* backup old value */
303 oldval.i_int = p_config->i_value;
305 /* if i_min == i_max == 0, then do not use them */
306 if ((p_config->i_min == 0) && (p_config->i_max == 0))
308 p_config->i_value = i_value;
310 else if (i_value < p_config->i_min)
312 p_config->i_value = p_config->i_min;
314 else if (i_value > p_config->i_max)
316 p_config->i_value = p_config->i_max;
320 p_config->i_value = i_value;
323 val.i_int = p_config->i_value;
325 if( p_config->pf_callback )
327 p_config->pf_callback( p_this, psz_name, oldval, val,
328 p_config->p_callback_data );
332 /*****************************************************************************
333 * config_PutFloat: set the value of a float variable
334 *****************************************************************************
335 * This function is used to set the value of variables which are internally
336 * represented by a float (CONFIG_ITEM_FLOAT).
337 *****************************************************************************/
338 void __config_PutFloat( vlc_object_t *p_this,
339 const char *psz_name, float f_value )
341 module_config_t *p_config;
342 vlc_value_t oldval, val;
344 p_config = config_FindConfig( p_this, psz_name );
349 msg_Warn( p_this, "option %s does not exist", psz_name );
352 if( p_config->i_type != CONFIG_ITEM_FLOAT )
354 msg_Err( p_this, "option %s does not refer to a float", psz_name );
358 /* backup old value */
359 oldval.f_float = p_config->f_value;
361 /* if f_min == f_max == 0, then do not use them */
362 if ((p_config->f_min == 0) && (p_config->f_max == 0))
364 p_config->f_value = f_value;
366 else if (f_value < p_config->f_min)
368 p_config->f_value = p_config->f_min;
370 else if (f_value > p_config->f_max)
372 p_config->f_value = p_config->f_max;
376 p_config->f_value = f_value;
379 val.f_float = p_config->f_value;
381 if( p_config->pf_callback )
383 p_config->pf_callback( p_this, psz_name, oldval, val,
384 p_config->p_callback_data );
388 /*****************************************************************************
389 * config_FindConfig: find the config structure associated with an option.
390 *****************************************************************************
391 * FIXME: This function really needs to be optimized.
392 * FIXME: And now even more.
393 *****************************************************************************/
394 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
398 module_config_t *p_item;
401 if( !psz_name ) return NULL;
403 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
405 for( i_index = 0; i_index < p_list->i_count; i_index++ )
407 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
409 if( !p_parser->i_config_items )
412 for( p_item = p_parser->p_config;
413 p_item->i_type != CONFIG_HINT_END;
416 if( p_item->i_type & CONFIG_HINT )
419 if( !strcmp( psz_name, p_item->psz_name ) )
421 vlc_list_release( p_list );
427 vlc_list_release( p_list );
432 /*****************************************************************************
433 * config_FindModule: find a specific module structure.
434 *****************************************************************************/
435 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
438 module_t *p_module, *p_result = NULL;
441 if( !psz_name ) return NULL;
443 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
445 for( i_index = 0; i_index < p_list->i_count; i_index++ )
447 p_module = (module_t *)p_list->p_values[i_index].p_object;
448 if( !strcmp( p_module->psz_object_name, psz_name ) )
455 vlc_list_release( p_list );
460 /*****************************************************************************
461 * config_Duplicate: creates a duplicate of a module's configuration data.
462 *****************************************************************************
463 * Unfortunatly we cannot work directly with the module's config data as
464 * this module might be unloaded from memory at any time (remember HideModule).
465 * This is why we need to create an exact copy of the config data.
466 *****************************************************************************/
467 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
469 int i, j, i_lines = 1;
470 module_config_t *p_item;
472 /* Calculate the structure length */
473 p_module->i_config_items = 0;
474 p_module->i_bool_items = 0;
476 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
480 if( p_item->i_type & CONFIG_ITEM )
482 p_module->i_config_items++;
485 if( p_item->i_type == CONFIG_ITEM_BOOL )
487 p_module->i_bool_items++;
491 /* Allocate memory */
492 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
494 if( p_module->p_config == NULL )
496 msg_Err( p_module, "config error: can't duplicate p_config" );
500 /* Do the duplication job */
501 for( i = 0; i < i_lines ; i++ )
503 p_module->p_config[i] = p_orig[i];
505 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
506 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
508 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
509 strdup( p_orig[i].psz_type ) : NULL;
510 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
511 strdup( p_orig[i].psz_name ) : NULL;
512 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
513 strdup( _(p_orig[i].psz_text) ) : NULL;
514 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
515 strdup( _(p_orig[i].psz_longtext) ) : NULL;
516 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
517 strdup( p_orig[i].psz_value ) : NULL;
518 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
519 strdup( p_orig[i].psz_value ) : NULL;
521 p_module->p_config[i].p_lock = &p_module->object_lock;
523 /* duplicate the string list */
524 if( p_orig[i].i_list )
526 if( p_orig[i].ppsz_list )
528 p_module->p_config[i].ppsz_list =
529 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
530 if( p_module->p_config[i].ppsz_list )
532 for( j = 0; j < p_orig[i].i_list; j++ )
533 p_module->p_config[i].ppsz_list[j] =
534 strdup( p_orig[i].ppsz_list[j] );
535 p_module->p_config[i].ppsz_list[j] = NULL;
538 if( p_orig[i].ppsz_list_text )
540 p_module->p_config[i].ppsz_list_text =
541 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
542 if( p_module->p_config[i].ppsz_list_text )
544 for( j = 0; j < p_orig[i].i_list; j++ )
545 p_module->p_config[i].ppsz_list_text[j] =
546 strdup( _(p_orig[i].ppsz_list_text[j]) );
547 p_module->p_config[i].ppsz_list_text[j] = NULL;
550 if( p_orig[i].pi_list )
552 p_module->p_config[i].pi_list =
553 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
554 if( p_module->p_config[i].pi_list )
556 for( j = 0; j < p_orig[i].i_list; j++ )
557 p_module->p_config[i].pi_list[j] =
558 p_orig[i].pi_list[j];
563 /* duplicate the actions list */
564 if( p_orig[i].i_action )
568 p_module->p_config[i].ppf_action =
569 malloc( p_orig[i].i_action * sizeof(void *) );
570 p_module->p_config[i].ppsz_action_text =
571 malloc( p_orig[i].i_action * sizeof(char *) );
573 for( j = 0; j < p_orig[i].i_action; j++ )
575 p_module->p_config[i].ppf_action[j] =
576 p_orig[i].ppf_action[j];
577 p_module->p_config[i].ppsz_action_text[j] =
578 p_orig[i].ppsz_action_text[j] ?
579 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
583 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
587 /*****************************************************************************
588 * config_Free: frees a duplicated module's configuration data.
589 *****************************************************************************
590 * This function frees all the data duplicated by config_Duplicate.
591 *****************************************************************************/
592 void config_Free( module_t *p_module )
594 module_config_t *p_item = p_module->p_config;
602 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
604 if( p_item->psz_type )
605 free( p_item->psz_type );
607 if( p_item->psz_name )
608 free( p_item->psz_name );
610 if( p_item->psz_text )
611 free( p_item->psz_text );
613 if( p_item->psz_longtext )
614 free( p_item->psz_longtext );
616 if( p_item->psz_value )
617 free( p_item->psz_value );
619 if( p_item->psz_value_orig )
620 free( p_item->psz_value_orig );
624 for( i = 0; i < p_item->i_list; i++ )
626 if( p_item->ppsz_list && p_item->ppsz_list[i] )
627 free( p_item->ppsz_list[i] );
628 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
629 free( p_item->ppsz_list_text[i] );
631 if( p_item->ppsz_list ) free( p_item->ppsz_list );
632 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
633 if( p_item->pi_list ) free( p_item->pi_list );
636 if( p_item->i_action )
638 for( i = 0; i < p_item->i_action; i++ )
640 if( p_item->ppsz_action_text[i] )
641 free( p_item->ppsz_action_text[i] );
643 if( p_item->ppf_action ) free( p_item->ppf_action );
644 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
648 free( p_module->p_config );
649 p_module->p_config = NULL;
652 /*****************************************************************************
653 * config_SetCallbacks: sets callback functions in the duplicate p_config.
654 *****************************************************************************
655 * Unfortunatly we cannot work directly with the module's config data as
656 * this module might be unloaded from memory at any time (remember HideModule).
657 * This is why we need to duplicate callbacks each time we reload the module.
658 *****************************************************************************/
659 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
661 while( p_new->i_type != CONFIG_HINT_END )
663 p_new->pf_callback = p_orig->pf_callback;
669 /*****************************************************************************
670 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
671 *****************************************************************************
672 * We simply undo what we did in config_SetCallbacks.
673 *****************************************************************************/
674 void config_UnsetCallbacks( module_config_t *p_new )
676 while( p_new->i_type != CONFIG_HINT_END )
678 p_new->pf_callback = NULL;
683 /*****************************************************************************
684 * config_ResetAll: reset the configuration data for all the modules.
685 *****************************************************************************/
686 void __config_ResetAll( vlc_object_t *p_this )
692 /* Acquire config file lock */
693 vlc_mutex_lock( &p_this->p_vlc->config_lock );
695 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
697 for( i_index = 0; i_index < p_list->i_count; i_index++ )
699 p_module = (module_t *)p_list->p_values[i_index].p_object ;
700 if( p_module->b_submodule ) continue;
702 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
704 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
705 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
706 if( p_module->p_config[i].psz_value )
707 free( p_module->p_config[i].psz_value );
708 p_module->p_config[i].psz_value =
709 p_module->p_config[i].psz_value_orig ?
710 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
714 vlc_list_release( p_list );
715 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
718 /*****************************************************************************
719 * config_LoadConfigFile: loads the configuration file.
720 *****************************************************************************
721 * This function is called to load the config options stored in the config
723 *****************************************************************************/
724 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
728 module_config_t *p_item;
731 char *p_index, *psz_option_name, *psz_option_value;
732 char *psz_filename, *psz_homedir, *psz_configfile;
735 psz_configfile = p_this->p_vlc->psz_configfile;
736 if( !psz_configfile || !psz_configfile )
738 psz_homedir = p_this->p_vlc->psz_homedir;
741 msg_Err( p_this, "psz_homedir is null" );
744 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
745 strlen(psz_homedir) );
747 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
752 psz_filename = strdup( psz_configfile );
757 msg_Err( p_this, "out of memory" );
761 msg_Dbg( p_this, "opening config file %s", psz_filename );
763 /* Acquire config file lock */
764 vlc_mutex_lock( &p_this->p_vlc->config_lock );
766 file = fopen( psz_filename, "rt" );
769 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
770 free( psz_filename );
771 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
775 /* Look for the selected module, if NULL then save everything */
776 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
778 for( i_index = 0; i_index < p_list->i_count; i_index++ )
780 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
783 && strcmp( psz_module_name, p_parser->psz_object_name ) )
788 /* The config file is organized in sections, one per module. Look for
789 * the interesting section ( a section is of the form [foo] ) */
790 fseek( file, 0L, SEEK_SET );
791 while( fgets( line, 1024, file ) )
794 && (p_index = strchr(line,']'))
795 && (p_index - &line[1]
796 == (int)strlen(p_parser->psz_object_name))
797 && !memcmp( &line[1], p_parser->psz_object_name,
798 strlen(p_parser->psz_object_name) ) )
801 msg_Dbg( p_this, "loading config for module \"%s\"",
802 p_parser->psz_object_name );
808 /* either we found the section or we're at the EOF */
810 /* Now try to load the options in this section */
811 while( fgets( line, 1024, file ) )
813 if( line[0] == '[' ) break; /* end of section */
815 /* ignore comments or empty lines */
816 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
819 /* get rid of line feed */
820 if( line[strlen(line)-1] == '\n' )
821 line[strlen(line)-1] = (char)0;
823 /* look for option name */
824 psz_option_name = line;
825 psz_option_value = NULL;
826 p_index = strchr( line, '=' );
827 if( !p_index ) break; /* this ain't an option!!! */
830 psz_option_value = p_index + 1;
832 if( !p_parser->i_config_items )
837 /* try to match this option with one of the module's options */
838 for( p_item = p_parser->p_config;
839 p_item->i_type != CONFIG_HINT_END;
842 if( p_item->i_type & CONFIG_HINT )
846 if( !strcmp( p_item->psz_name, psz_option_name ) )
849 switch( p_item->i_type )
851 case CONFIG_ITEM_BOOL:
852 case CONFIG_ITEM_INTEGER:
853 if( !*psz_option_value )
854 break; /* ignore empty option */
855 p_item->i_value = strtol( psz_option_value, 0, 0 );
857 msg_Dbg( p_this, "option \"%s\", value %i",
858 p_item->psz_name, p_item->i_value );
862 case CONFIG_ITEM_FLOAT:
863 if( !*psz_option_value )
864 break; /* ignore empty option */
865 p_item->f_value = (float)atof( psz_option_value);
867 msg_Dbg( p_this, "option \"%s\", value %f",
868 p_item->psz_name, (double)p_item->f_value );
871 case CONFIG_ITEM_KEY:
872 if( !*psz_option_value )
873 break; /* ignore empty option */
874 p_item->i_value = ConfigStringToKey( psz_option_value );
878 vlc_mutex_lock( p_item->p_lock );
880 /* free old string */
881 if( p_item->psz_value )
882 free( p_item->psz_value );
884 p_item->psz_value = *psz_option_value ?
885 strdup( psz_option_value ) : NULL;
887 vlc_mutex_unlock( p_item->p_lock );
890 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
892 p_item->psz_value ? p_item->psz_value : "" );
902 vlc_list_release( p_list );
905 free( psz_filename );
907 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
912 /*****************************************************************************
913 * config_CreateDir: Create configuration directory if it doesn't exist.
914 *****************************************************************************/
915 int config_CreateDir( vlc_object_t *p_this, char *psz_dirname )
917 if( !psz_dirname && !*psz_dirname ) return -1;
919 #if defined( UNDER_CE )
921 wchar_t psz_new[ MAX_PATH ];
922 MultiByteToWideChar( CP_ACP, 0, psz_dirname, -1, psz_new, MAX_PATH );
923 if( CreateDirectory( psz_new, NULL ) )
925 msg_Err( p_this, "could not create %s", psz_filename );
929 #elif defined( HAVE_ERRNO_H )
930 # if defined( WIN32 )
931 if( mkdir( psz_dirname ) && errno != EEXIST )
933 if( mkdir( psz_dirname, 0755 ) && errno != EEXIST )
936 msg_Err( p_this, "could not create %s (%s)",
937 psz_dirname, strerror(errno) );
941 if( mkdir( psz_dirname ) )
943 msg_Err( p_this, "could not create %s", psz_dirname );
951 /*****************************************************************************
952 * config_SaveConfigFile: Save a module's config options.
953 *****************************************************************************
954 * This will save the specified module's config options to the config file.
955 * If psz_module_name is NULL then we save all the modules config options.
956 * It's no use to save the config options that kept their default values, so
957 * we'll try to be a bit clever here.
959 * When we save we mustn't delete the config options of the modules that
960 * haven't been loaded. So we cannot just create a new config file with the
961 * config structures we've got in memory.
962 * I don't really know how to deal with this nicely, so I will use a completly
964 * I will load the config file in memory, but skipping all the sections of the
965 * modules we want to save. Then I will create a brand new file, dump the file
966 * loaded in memory and then append the sections of the modules we want to
969 *****************************************************************************/
970 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
974 module_config_t *p_item;
976 char p_line[1024], *p_index2;
978 char *p_bigbuffer, *p_index;
980 char *psz_filename, *psz_homedir, *psz_configfile;
983 /* Acquire config file lock */
984 vlc_mutex_lock( &p_this->p_vlc->config_lock );
986 psz_configfile = p_this->p_vlc->psz_configfile;
987 if( !psz_configfile || !psz_configfile )
989 psz_homedir = p_this->p_vlc->psz_homedir;
992 msg_Err( p_this, "psz_homedir is null" );
993 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
996 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
997 strlen(psz_homedir) );
1000 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1004 msg_Err( p_this, "out of memory" );
1005 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1009 config_CreateDir( p_this, psz_filename );
1011 strcat( psz_filename, "/" CONFIG_FILE );
1015 psz_filename = strdup( psz_configfile );
1018 msg_Err( p_this, "out of memory" );
1019 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1024 msg_Dbg( p_this, "opening config file %s", psz_filename );
1026 file = fopen( psz_filename, "rt" );
1029 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1033 /* look for file size */
1034 fseek( file, 0L, SEEK_END );
1035 i_sizebuf = ftell( file );
1036 fseek( file, 0L, SEEK_SET );
1039 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1042 msg_Err( p_this, "out of memory" );
1043 if( file ) fclose( file );
1044 free( psz_filename );
1045 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1050 /* List all available modules */
1051 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1053 /* backup file into memory, we only need to backup the sections we won't
1056 while( file && fgets( p_line, 1024, file ) )
1058 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1061 /* we found a section, check if we need to do a backup */
1062 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1064 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1066 if( ((p_index2 - &p_line[1])
1067 == (int)strlen(p_parser->psz_object_name) )
1068 && !memcmp( &p_line[1], p_parser->psz_object_name,
1069 strlen(p_parser->psz_object_name) ) )
1071 if( !psz_module_name )
1073 else if( !strcmp( psz_module_name,
1074 p_parser->psz_object_name ) )
1079 if( i_index == p_list->i_count )
1081 /* we don't have this section in our list so we need to back
1084 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1096 /* save line if requested and line is valid (doesn't begin with a
1097 * space, tab, or eol) */
1098 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1099 && (p_line[0] != '\t') )
1101 strcpy( p_index, p_line );
1102 p_index += strlen( p_line );
1105 if( file ) fclose( file );
1109 * Save module config in file
1112 file = fopen( psz_filename, "wt" );
1115 msg_Warn( p_this, "could not open config file %s for writing",
1117 free( psz_filename );
1118 vlc_list_release( p_list );
1119 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1123 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
1125 /* Look for the selected module, if NULL then save everything */
1126 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1128 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1130 if( psz_module_name && strcmp( psz_module_name,
1131 p_parser->psz_object_name ) )
1134 if( !p_parser->i_config_items )
1137 msg_Dbg( p_this, "saving config for module \"%s\"",
1138 p_parser->psz_object_name );
1140 fprintf( file, "[%s]", p_parser->psz_object_name );
1141 if( p_parser->psz_longname )
1142 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1144 fprintf( file, "\n\n" );
1146 for( p_item = p_parser->p_config;
1147 p_item->i_type != CONFIG_HINT_END;
1151 if( p_item->i_type & CONFIG_HINT )
1155 switch( p_item->i_type )
1157 case CONFIG_ITEM_BOOL:
1158 case CONFIG_ITEM_INTEGER:
1159 if( p_item->psz_text )
1160 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1161 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1162 _("boolean") : _("integer") );
1163 if( p_item->i_value == p_item->i_value_orig )
1164 fprintf( file, "#" );
1165 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
1167 case CONFIG_ITEM_KEY:
1168 if( p_item->psz_text )
1169 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1171 if( p_item->i_value == p_item->i_value_orig )
1172 fprintf( file, "#" );
1173 psz_key = ConfigKeyToString( p_item->i_value );
1174 fprintf( file, "%s=%s\n", p_item->psz_name,
1175 psz_key ? psz_key : "" );
1176 if ( psz_key ) free( psz_key );
1179 case CONFIG_ITEM_FLOAT:
1180 if( p_item->psz_text )
1181 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1183 if( p_item->f_value == p_item->f_value_orig )
1184 fprintf( file, "#" );
1185 fprintf( file, "%s=%f\n", p_item->psz_name,
1186 (double)p_item->f_value );
1190 if( p_item->psz_text )
1191 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1193 if( (!p_item->psz_value && !p_item->psz_value_orig) ||
1194 (p_item->psz_value && p_item->psz_value_orig &&
1195 !strcmp( p_item->psz_value, p_item->psz_value_orig )) )
1196 fprintf( file, "#" );
1197 fprintf( file, "%s=%s\n", p_item->psz_name,
1198 p_item->psz_value ? p_item->psz_value : "" );
1202 fprintf( file, "\n" );
1205 vlc_list_release( p_list );
1208 * Restore old settings from the config in file
1210 fputs( p_bigbuffer, file );
1211 free( p_bigbuffer );
1214 free( psz_filename );
1215 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1220 /*****************************************************************************
1221 * config_LoadCmdLine: parse command line
1222 *****************************************************************************
1223 * Parse command line for configuration options.
1224 * Now that the module_bank has been initialized, we can dynamically
1225 * generate the longopts structure used by getops. We have to do it this way
1226 * because we don't know (and don't want to know) in advance the configuration
1227 * options used (ie. exported) by each module.
1228 *****************************************************************************/
1229 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1230 vlc_bool_t b_ignore_errors )
1232 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1235 module_config_t *p_item;
1236 struct option *p_longopts;
1237 int i_modules_index;
1240 module_config_t *pp_shortopts[256];
1241 char *psz_shortopts;
1243 /* Set default configuration and copy arguments */
1244 p_this->p_vlc->i_argc = *pi_argc;
1245 p_this->p_vlc->ppsz_argv = ppsz_argv;
1248 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1249 * is the PSN - process serial number (a unique PID-ish thingie)
1250 * still ok for real Darwin & when run from command line */
1251 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1252 /* for example -psn_0_9306113 */
1254 /* GDMF!... I can't do this or else the MacOSX window server will
1255 * not pick up the PSN and not register the app and we crash...
1256 * hence the following kludge otherwise we'll get confused w/ argv[1]
1257 * being an input file name */
1259 ppsz_argv[ 1 ] = NULL;
1261 *pi_argc = *pi_argc - 1;
1267 /* List all modules */
1268 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1271 * Generate the longopts and shortopts structures used by getopt_long
1275 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1278 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1280 /* count the number of exported configuration options (to allocate
1281 * longopts). We also need to allocate space for too options when
1282 * dealing with boolean to allow for --foo and --no-foo */
1283 i_opts += p_parser->i_config_items
1284 + 2 * p_parser->i_bool_items;
1287 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1288 if( p_longopts == NULL )
1290 msg_Err( p_this, "out of memory" );
1291 vlc_list_release( p_list );
1295 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1296 if( psz_shortopts == NULL )
1298 msg_Err( p_this, "out of memory" );
1300 vlc_list_release( p_list );
1304 /* If we are requested to ignore errors, then we must work on a copy
1305 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1306 * us, ignoring the arity of the options */
1307 if( b_ignore_errors )
1309 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1310 if( ppsz_argv == NULL )
1312 msg_Err( p_this, "out of memory" );
1313 free( psz_shortopts );
1315 vlc_list_release( p_list );
1318 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1319 *pi_argc * sizeof(char *) );
1323 for( i_index = 0; i_index < 256; i_index++ )
1325 pp_shortopts[i_index] = NULL;
1328 /* Fill the p_longopts and psz_shortopts structures */
1330 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1333 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1335 if( !p_parser->i_config_items )
1338 for( p_item = p_parser->p_config;
1339 p_item->i_type != CONFIG_HINT_END;
1343 if( p_item->i_type & CONFIG_HINT )
1346 /* Add item to long options */
1347 p_longopts[i_index].name = strdup( p_item->psz_name );
1348 if( p_longopts[i_index].name == NULL ) continue;
1349 p_longopts[i_index].has_arg =
1350 (p_item->i_type == CONFIG_ITEM_BOOL)?
1351 no_argument : required_argument;
1352 p_longopts[i_index].flag = &flag;
1353 p_longopts[i_index].val = 0;
1356 /* When dealing with bools we also need to add the --no-foo
1358 if( p_item->i_type == CONFIG_ITEM_BOOL )
1360 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1361 if( psz_name == NULL ) continue;
1362 strcpy( psz_name, "no" );
1363 strcat( psz_name, p_item->psz_name );
1365 p_longopts[i_index].name = psz_name;
1366 p_longopts[i_index].has_arg = no_argument;
1367 p_longopts[i_index].flag = &flag;
1368 p_longopts[i_index].val = 1;
1371 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1372 if( psz_name == NULL ) continue;
1373 strcpy( psz_name, "no-" );
1374 strcat( psz_name, p_item->psz_name );
1376 p_longopts[i_index].name = psz_name;
1377 p_longopts[i_index].has_arg = no_argument;
1378 p_longopts[i_index].flag = &flag;
1379 p_longopts[i_index].val = 1;
1383 /* If item also has a short option, add it */
1384 if( p_item->i_short )
1386 pp_shortopts[(int)p_item->i_short] = p_item;
1387 psz_shortopts[i_shortopts] = p_item->i_short;
1389 if( p_item->i_type != CONFIG_ITEM_BOOL )
1391 psz_shortopts[i_shortopts] = ':';
1394 if( p_item->i_short == 'v' )
1396 psz_shortopts[i_shortopts] = ':';
1404 /* We don't need the module list anymore */
1405 vlc_list_release( p_list );
1407 /* Close the longopts and shortopts structures */
1408 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1409 psz_shortopts[i_shortopts] = '\0';
1412 * Parse the command line options
1416 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1417 p_longopts, &i_index ) ) != EOF )
1419 /* A long option has been recognized */
1422 module_config_t *p_conf;
1423 char *psz_name = (char *)p_longopts[i_index].name;
1425 /* Check if we deal with a --nofoo or --no-foo long option */
1426 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1428 /* Store the configuration option */
1429 p_conf = config_FindConfig( p_this, psz_name );
1431 if( p_conf ) switch( p_conf->i_type )
1433 case CONFIG_ITEM_STRING:
1434 case CONFIG_ITEM_FILE:
1435 case CONFIG_ITEM_DIRECTORY:
1436 case CONFIG_ITEM_MODULE:
1437 config_PutPsz( p_this, psz_name, optarg );
1439 case CONFIG_ITEM_INTEGER:
1440 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1442 case CONFIG_ITEM_FLOAT:
1443 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1445 case CONFIG_ITEM_KEY:
1446 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1448 case CONFIG_ITEM_BOOL:
1449 config_PutInt( p_this, psz_name, !flag );
1456 /* A short option has been recognized */
1457 if( pp_shortopts[i_cmd] != NULL )
1459 switch( pp_shortopts[i_cmd]->i_type )
1461 case CONFIG_ITEM_STRING:
1462 case CONFIG_ITEM_FILE:
1463 case CONFIG_ITEM_DIRECTORY:
1464 case CONFIG_ITEM_MODULE:
1465 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1467 case CONFIG_ITEM_INTEGER:
1472 if( *optarg == 'v' ) /* eg. -vvv */
1475 while( *optarg == 'v' )
1483 i_verbose += atoi( optarg ); /* eg. -v2 */
1488 i_verbose++; /* -v */
1490 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1495 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1496 strtol(optarg, 0, 0) );
1499 case CONFIG_ITEM_BOOL:
1500 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1507 /* Internal error: unknown option */
1508 if( !b_ignore_errors )
1510 fprintf( stderr, "%s: unknown option ",
1511 p_this->p_vlc->psz_object_name );
1514 fprintf( stderr, "`-%c'\n", optopt );
1518 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1520 fprintf( stderr, "Try `%s --help' for more information.\n",
1521 p_this->p_vlc->psz_object_name );
1524 free( psz_shortopts );
1529 /* Free allocated resources */
1530 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1531 free( (char *)p_longopts[i_index].name );
1533 free( psz_shortopts );
1534 if( b_ignore_errors ) free( ppsz_argv );
1539 /*****************************************************************************
1540 * config_GetHomeDir: find the user's home directory.
1541 *****************************************************************************
1542 * This function will try by different ways to find the user's home path.
1543 * Note that this function is not reentrant, it should be called only once
1544 * at the beginning of main where the result will be stored for later use.
1545 *****************************************************************************/
1546 char *config_GetHomeDir( void )
1548 char *p_tmp, *p_homedir = NULL;
1550 #if defined(HAVE_GETPWUID)
1551 struct passwd *p_pw = NULL;
1554 #if defined(WIN32) || defined(UNDER_CE)
1555 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1557 # define CSIDL_FLAG_CREATE 0x8000
1558 # define CSIDL_APPDATA 0x1A
1559 # define SHGFP_TYPE_CURRENT 0
1561 HINSTANCE shfolder_dll;
1562 SHGETFOLDERPATH SHGetFolderPath ;
1564 /* load the shfolder dll to retrieve SHGetFolderPath */
1565 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1567 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1568 _T("SHGetFolderPathA") );
1569 if ( SHGetFolderPath != NULL )
1571 p_homedir = (char *)malloc( MAX_PATH );
1577 /* get the "Application Data" folder for the current user */
1578 if( S_OK == SHGetFolderPath( NULL,
1579 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1580 NULL, SHGFP_TYPE_CURRENT,
1583 FreeLibrary( shfolder_dll );
1588 FreeLibrary( shfolder_dll );
1592 #if defined(HAVE_GETPWUID)
1593 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1596 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1598 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1604 p_homedir = strdup( p_tmp );
1606 #if defined(HAVE_GETPWUID)
1609 p_homedir = strdup( p_pw->pw_dir );
1617 static int ConfigStringToKey( char *psz_key )
1621 char *psz_parser = strchr( psz_key, '-' );
1622 while( psz_parser && psz_parser != psz_key )
1624 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1626 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1627 strlen( vlc_modifiers[i].psz_key_string ) ) )
1629 i_key |= vlc_modifiers[i].i_key_code;
1632 psz_key = psz_parser + 1;
1633 psz_parser = strchr( psz_key, '-' );
1635 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1637 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1639 i_key |= vlc_keys[i].i_key_code;
1646 static char *ConfigKeyToString( int i_key )
1648 char *psz_key = malloc( 100 );
1658 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1661 if( i_key & vlc_modifiers[index].i_key_code )
1663 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1666 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1669 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1671 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );