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() */
30 #include <errno.h> /* errno */
33 # include <unistd.h> /* getuid() */
36 #ifdef HAVE_GETOPT_LONG
38 # include <getopt.h> /* getopt() */
41 # include "../extras/getopt.h"
44 #if defined(HAVE_GETPWUID)
45 # include <pwd.h> /* getpwuid() */
48 #if defined( HAVE_SYS_STAT_H )
49 # include <sys/stat.h>
51 #if defined( HAVE_SYS_TYPES_H )
52 # include <sys/types.h>
55 # if !defined( UNDER_CE )
62 static int ConfigStringToKey( char * );
63 static char *ConfigKeyToString( int );
65 /*****************************************************************************
66 * config_GetType: get the type of a variable (bool, int, float, string)
67 *****************************************************************************
68 * This function is used to get the type of a variable from its name.
69 * Beware, this is quite slow.
70 *****************************************************************************/
71 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
73 module_config_t *p_config;
76 p_config = config_FindConfig( p_this, psz_name );
84 switch( p_config->i_type )
86 case CONFIG_ITEM_BOOL:
87 i_type = VLC_VAR_BOOL;
90 case CONFIG_ITEM_INTEGER:
91 i_type = VLC_VAR_INTEGER;
94 case CONFIG_ITEM_FLOAT:
95 i_type = VLC_VAR_FLOAT;
98 case CONFIG_ITEM_MODULE:
99 i_type = VLC_VAR_MODULE;
102 case CONFIG_ITEM_STRING:
103 i_type = VLC_VAR_STRING;
106 case CONFIG_ITEM_FILE:
107 i_type = VLC_VAR_FILE;
110 case CONFIG_ITEM_DIRECTORY:
111 i_type = VLC_VAR_DIRECTORY;
122 /*****************************************************************************
123 * config_GetInt: get the value of an int variable
124 *****************************************************************************
125 * This function is used to get the value of variables which are internally
126 * represented by an integer (CONFIG_ITEM_INTEGER and
128 *****************************************************************************/
129 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
131 module_config_t *p_config;
133 p_config = config_FindConfig( p_this, psz_name );
138 msg_Err( p_this, "option %s does not exist", psz_name );
141 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
142 (p_config->i_type!=CONFIG_ITEM_KEY) &&
143 (p_config->i_type!=CONFIG_ITEM_BOOL) )
145 msg_Err( p_this, "option %s does not refer to an int", psz_name );
149 return p_config->i_value;
152 /*****************************************************************************
153 * config_GetFloat: get the value of a float variable
154 *****************************************************************************
155 * This function is used to get the value of variables which are internally
156 * represented by a float (CONFIG_ITEM_FLOAT).
157 *****************************************************************************/
158 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
160 module_config_t *p_config;
162 p_config = config_FindConfig( p_this, psz_name );
167 msg_Err( p_this, "option %s does not exist", psz_name );
170 if( p_config->i_type != CONFIG_ITEM_FLOAT )
172 msg_Err( p_this, "option %s does not refer to a float", psz_name );
176 return p_config->f_value;
179 /*****************************************************************************
180 * config_GetPsz: get the string value of a string variable
181 *****************************************************************************
182 * This function is used to get the value of variables which are internally
183 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
184 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
186 * Important note: remember to free() the returned char* because it's a
187 * duplicate of the actual value. It isn't safe to return a pointer to the
188 * actual value as it can be modified at any time.
189 *****************************************************************************/
190 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
192 module_config_t *p_config;
193 char *psz_value = NULL;
195 p_config = config_FindConfig( p_this, psz_name );
200 msg_Err( p_this, "option %s does not exist", psz_name );
203 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
204 (p_config->i_type!=CONFIG_ITEM_FILE) &&
205 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
206 (p_config->i_type!=CONFIG_ITEM_MODULE) )
208 msg_Err( p_this, "option %s does not refer to a string", psz_name );
212 /* return a copy of the string */
213 vlc_mutex_lock( p_config->p_lock );
214 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
215 vlc_mutex_unlock( p_config->p_lock );
220 /*****************************************************************************
221 * config_PutPsz: set the string value of a string variable
222 *****************************************************************************
223 * This function is used to set the value of variables which are internally
224 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
225 * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
226 *****************************************************************************/
227 void __config_PutPsz( vlc_object_t *p_this,
228 const char *psz_name, const char *psz_value )
230 module_config_t *p_config;
231 vlc_value_t oldval, val;
233 p_config = config_FindConfig( p_this, psz_name );
238 msg_Warn( p_this, "option %s does not exist", psz_name );
241 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
242 (p_config->i_type!=CONFIG_ITEM_FILE) &&
243 (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
244 (p_config->i_type!=CONFIG_ITEM_MODULE) )
246 msg_Err( p_this, "option %s does not refer to a string", psz_name );
250 vlc_mutex_lock( p_config->p_lock );
252 /* backup old value */
253 oldval.psz_string = p_config->psz_value;
255 if( psz_value && *psz_value ) p_config->psz_value = strdup( psz_value );
256 else p_config->psz_value = NULL;
258 val.psz_string = p_config->psz_value;
260 vlc_mutex_unlock( p_config->p_lock );
262 if( p_config->pf_callback )
264 p_config->pf_callback( p_this, psz_name, oldval, val,
265 p_config->p_callback_data );
268 /* free old string */
269 if( oldval.psz_string ) free( oldval.psz_string );
272 /*****************************************************************************
273 * config_PutInt: set the integer value of an int variable
274 *****************************************************************************
275 * This function is used to set the value of variables which are internally
276 * represented by an integer (CONFIG_ITEM_INTEGER and
278 *****************************************************************************/
279 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
281 module_config_t *p_config;
282 vlc_value_t oldval, val;
284 p_config = config_FindConfig( p_this, psz_name );
289 msg_Warn( p_this, "option %s does not exist", psz_name );
292 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
293 (p_config->i_type!=CONFIG_ITEM_KEY) &&
294 (p_config->i_type!=CONFIG_ITEM_BOOL) )
296 msg_Err( p_this, "option %s does not refer to an int", psz_name );
300 /* backup old value */
301 oldval.i_int = p_config->i_value;
303 /* if i_min == i_max == 0, then do not use them */
304 if ((p_config->i_min == 0) && (p_config->i_max == 0))
306 p_config->i_value = i_value;
308 else if (i_value < p_config->i_min)
310 p_config->i_value = p_config->i_min;
312 else if (i_value > p_config->i_max)
314 p_config->i_value = p_config->i_max;
318 p_config->i_value = i_value;
321 val.i_int = p_config->i_value;
323 if( p_config->pf_callback )
325 p_config->pf_callback( p_this, psz_name, oldval, val,
326 p_config->p_callback_data );
330 /*****************************************************************************
331 * config_PutFloat: set the value of a float variable
332 *****************************************************************************
333 * This function is used to set the value of variables which are internally
334 * represented by a float (CONFIG_ITEM_FLOAT).
335 *****************************************************************************/
336 void __config_PutFloat( vlc_object_t *p_this,
337 const char *psz_name, float f_value )
339 module_config_t *p_config;
340 vlc_value_t oldval, val;
342 p_config = config_FindConfig( p_this, psz_name );
347 msg_Warn( p_this, "option %s does not exist", psz_name );
350 if( p_config->i_type != CONFIG_ITEM_FLOAT )
352 msg_Err( p_this, "option %s does not refer to a float", psz_name );
356 /* backup old value */
357 oldval.f_float = p_config->f_value;
359 /* if f_min == f_max == 0, then do not use them */
360 if ((p_config->f_min == 0) && (p_config->f_max == 0))
362 p_config->f_value = f_value;
364 else if (f_value < p_config->f_min)
366 p_config->f_value = p_config->f_min;
368 else if (f_value > p_config->f_max)
370 p_config->f_value = p_config->f_max;
374 p_config->f_value = f_value;
377 val.f_float = p_config->f_value;
379 if( p_config->pf_callback )
381 p_config->pf_callback( p_this, psz_name, oldval, val,
382 p_config->p_callback_data );
386 /*****************************************************************************
387 * config_FindConfig: find the config structure associated with an option.
388 *****************************************************************************
389 * FIXME: This function really needs to be optimized.
390 * FIXME: And now even more.
391 *****************************************************************************/
392 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
396 module_config_t *p_item;
399 if( !psz_name ) return NULL;
401 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
403 for( i_index = 0; i_index < p_list->i_count; i_index++ )
405 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
407 if( !p_parser->i_config_items )
410 for( p_item = p_parser->p_config;
411 p_item->i_type != CONFIG_HINT_END;
414 if( p_item->i_type & CONFIG_HINT )
417 if( !strcmp( psz_name, p_item->psz_name ) )
419 vlc_list_release( p_list );
425 vlc_list_release( p_list );
430 /*****************************************************************************
431 * config_FindModule: find a specific module structure.
432 *****************************************************************************/
433 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
436 module_t *p_module, *p_result = NULL;
439 if( !psz_name ) return NULL;
441 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
443 for( i_index = 0; i_index < p_list->i_count; i_index++ )
445 p_module = (module_t *)p_list->p_values[i_index].p_object;
446 if( !strcmp( p_module->psz_object_name, psz_name ) )
453 vlc_list_release( p_list );
458 /*****************************************************************************
459 * config_Duplicate: creates a duplicate of a module's configuration data.
460 *****************************************************************************
461 * Unfortunatly we cannot work directly with the module's config data as
462 * this module might be unloaded from memory at any time (remember HideModule).
463 * This is why we need to create an exact copy of the config data.
464 *****************************************************************************/
465 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
467 int i, j, i_lines = 1;
468 module_config_t *p_item;
470 /* Calculate the structure length */
471 p_module->i_config_items = 0;
472 p_module->i_bool_items = 0;
474 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
478 if( p_item->i_type & CONFIG_ITEM )
480 p_module->i_config_items++;
483 if( p_item->i_type == CONFIG_ITEM_BOOL )
485 p_module->i_bool_items++;
489 /* Allocate memory */
490 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
492 if( p_module->p_config == NULL )
494 msg_Err( p_module, "config error: can't duplicate p_config" );
498 /* Do the duplication job */
499 for( i = 0; i < i_lines ; i++ )
501 p_module->p_config[i] = p_orig[i];
503 p_module->p_config[i].i_value_orig = p_orig[i].i_value;
504 p_module->p_config[i].f_value_orig = p_orig[i].f_value;
506 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
507 strdup( p_orig[i].psz_type ) : NULL;
508 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
509 strdup( p_orig[i].psz_name ) : NULL;
510 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
511 strdup( _(p_orig[i].psz_text) ) : NULL;
512 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
513 strdup( _(p_orig[i].psz_longtext) ) : NULL;
514 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
515 strdup( p_orig[i].psz_value ) : NULL;
516 p_module->p_config[i].psz_value_orig = p_orig[i].psz_value ?
517 strdup( p_orig[i].psz_value ) : NULL;
519 p_module->p_config[i].p_lock = &p_module->object_lock;
521 /* duplicate the string list */
522 if( p_orig[i].i_list )
524 if( p_orig[i].ppsz_list )
526 p_module->p_config[i].ppsz_list =
527 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
528 if( p_module->p_config[i].ppsz_list )
530 for( j = 0; j < p_orig[i].i_list; j++ )
531 p_module->p_config[i].ppsz_list[j] = p_orig[i].ppsz_list[j] ?
532 strdup( p_orig[i].ppsz_list[j] ) : NULL ;
533 p_module->p_config[i].ppsz_list[j] = NULL;
536 if( p_orig[i].ppsz_list_text )
538 p_module->p_config[i].ppsz_list_text =
539 malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
540 if( p_module->p_config[i].ppsz_list_text )
542 for( j = 0; j < p_orig[i].i_list; j++ )
543 p_module->p_config[i].ppsz_list_text[j] = _(p_orig[i].ppsz_list_text[j]) ?
544 strdup( _(p_orig[i].ppsz_list_text[j] ) ) : NULL ;
545 p_module->p_config[i].ppsz_list_text[j] = NULL;
548 if( p_orig[i].pi_list )
550 p_module->p_config[i].pi_list =
551 malloc( (p_orig[i].i_list + 1) * sizeof(int) );
552 if( p_module->p_config[i].pi_list )
554 for( j = 0; j < p_orig[i].i_list; j++ )
555 p_module->p_config[i].pi_list[j] =
556 p_orig[i].pi_list[j];
561 /* duplicate the actions list */
562 if( p_orig[i].i_action )
566 p_module->p_config[i].ppf_action =
567 malloc( p_orig[i].i_action * sizeof(void *) );
568 p_module->p_config[i].ppsz_action_text =
569 malloc( p_orig[i].i_action * sizeof(char *) );
571 for( j = 0; j < p_orig[i].i_action; j++ )
573 p_module->p_config[i].ppf_action[j] =
574 p_orig[i].ppf_action[j];
575 p_module->p_config[i].ppsz_action_text[j] =
576 p_orig[i].ppsz_action_text[j] ?
577 strdup( p_orig[i].ppsz_action_text[j] ) : NULL;
581 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
585 /*****************************************************************************
586 * config_Free: frees a duplicated module's configuration data.
587 *****************************************************************************
588 * This function frees all the data duplicated by config_Duplicate.
589 *****************************************************************************/
590 void config_Free( module_t *p_module )
592 module_config_t *p_item = p_module->p_config;
600 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
602 if( p_item->psz_type )
603 free( p_item->psz_type );
605 if( p_item->psz_name )
606 free( p_item->psz_name );
608 if( p_item->psz_text )
609 free( p_item->psz_text );
611 if( p_item->psz_longtext )
612 free( p_item->psz_longtext );
614 if( p_item->psz_value )
615 free( p_item->psz_value );
617 if( p_item->psz_value_orig )
618 free( p_item->psz_value_orig );
622 for( i = 0; i < p_item->i_list; i++ )
624 if( p_item->ppsz_list && p_item->ppsz_list[i] )
625 free( p_item->ppsz_list[i] );
626 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
627 free( p_item->ppsz_list_text[i] );
629 if( p_item->ppsz_list ) free( p_item->ppsz_list );
630 if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
631 if( p_item->pi_list ) free( p_item->pi_list );
634 if( p_item->i_action )
636 for( i = 0; i < p_item->i_action; i++ )
638 if( p_item->ppsz_action_text[i] )
639 free( p_item->ppsz_action_text[i] );
641 if( p_item->ppf_action ) free( p_item->ppf_action );
642 if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
646 free( p_module->p_config );
647 p_module->p_config = NULL;
650 /*****************************************************************************
651 * config_SetCallbacks: sets callback functions in the duplicate p_config.
652 *****************************************************************************
653 * Unfortunatly we cannot work directly with the module's config data as
654 * this module might be unloaded from memory at any time (remember HideModule).
655 * This is why we need to duplicate callbacks each time we reload the module.
656 *****************************************************************************/
657 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
659 while( p_new->i_type != CONFIG_HINT_END )
661 p_new->pf_callback = p_orig->pf_callback;
667 /*****************************************************************************
668 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
669 *****************************************************************************
670 * We simply undo what we did in config_SetCallbacks.
671 *****************************************************************************/
672 void config_UnsetCallbacks( module_config_t *p_new )
674 while( p_new->i_type != CONFIG_HINT_END )
676 p_new->pf_callback = NULL;
681 /*****************************************************************************
682 * config_ResetAll: reset the configuration data for all the modules.
683 *****************************************************************************/
684 void __config_ResetAll( vlc_object_t *p_this )
690 /* Acquire config file lock */
691 vlc_mutex_lock( &p_this->p_vlc->config_lock );
693 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
695 for( i_index = 0; i_index < p_list->i_count; i_index++ )
697 p_module = (module_t *)p_list->p_values[i_index].p_object ;
698 if( p_module->b_submodule ) continue;
700 for( i = 0; p_module->p_config[i].i_type != CONFIG_HINT_END; i++ )
702 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
703 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
704 if( p_module->p_config[i].psz_value )
705 free( p_module->p_config[i].psz_value );
706 p_module->p_config[i].psz_value =
707 p_module->p_config[i].psz_value_orig ?
708 strdup( p_module->p_config[i].psz_value_orig ) : NULL;
712 vlc_list_release( p_list );
713 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
716 /*****************************************************************************
717 * config_LoadConfigFile: loads the configuration file.
718 *****************************************************************************
719 * This function is called to load the config options stored in the config
721 *****************************************************************************/
722 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
726 module_config_t *p_item;
729 char *p_index, *psz_option_name, *psz_option_value;
730 char *psz_filename, *psz_homedir, *psz_configfile;
733 psz_configfile = p_this->p_vlc->psz_configfile;
734 if( !psz_configfile || !psz_configfile )
736 psz_homedir = p_this->p_vlc->psz_homedir;
739 msg_Err( p_this, "psz_homedir is null" );
742 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
743 strlen(psz_homedir) );
745 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
750 psz_filename = strdup( psz_configfile );
755 msg_Err( p_this, "out of memory" );
759 msg_Dbg( p_this, "opening config file %s", psz_filename );
761 /* Acquire config file lock */
762 vlc_mutex_lock( &p_this->p_vlc->config_lock );
764 file = fopen( psz_filename, "rt" );
767 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
768 free( psz_filename );
769 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
773 /* Look for the selected module, if NULL then save everything */
774 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
776 for( i_index = 0; i_index < p_list->i_count; i_index++ )
778 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
781 && strcmp( psz_module_name, p_parser->psz_object_name ) )
786 /* The config file is organized in sections, one per module. Look for
787 * the interesting section ( a section is of the form [foo] ) */
788 fseek( file, 0L, SEEK_SET );
789 while( fgets( line, 1024, file ) )
792 && (p_index = strchr(line,']'))
793 && (p_index - &line[1]
794 == (int)strlen(p_parser->psz_object_name))
795 && !memcmp( &line[1], p_parser->psz_object_name,
796 strlen(p_parser->psz_object_name) ) )
799 msg_Dbg( p_this, "loading config for module \"%s\"",
800 p_parser->psz_object_name );
806 /* either we found the section or we're at the EOF */
808 /* Now try to load the options in this section */
809 while( fgets( line, 1024, file ) )
811 if( line[0] == '[' ) break; /* end of section */
813 /* ignore comments or empty lines */
814 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
817 /* get rid of line feed */
818 if( line[strlen(line)-1] == '\n' )
819 line[strlen(line)-1] = (char)0;
821 /* look for option name */
822 psz_option_name = line;
823 psz_option_value = NULL;
824 p_index = strchr( line, '=' );
825 if( !p_index ) break; /* this ain't an option!!! */
828 psz_option_value = p_index + 1;
830 if( !p_parser->i_config_items )
835 /* try to match this option with one of the module's options */
836 for( p_item = p_parser->p_config;
837 p_item->i_type != CONFIG_HINT_END;
840 if( p_item->i_type & CONFIG_HINT )
844 if( !strcmp( p_item->psz_name, psz_option_name ) )
847 switch( p_item->i_type )
849 case CONFIG_ITEM_BOOL:
850 case CONFIG_ITEM_INTEGER:
851 if( !*psz_option_value )
852 break; /* ignore empty option */
853 p_item->i_value = strtol( psz_option_value, 0, 0 );
855 msg_Dbg( p_this, "option \"%s\", value %i",
856 p_item->psz_name, p_item->i_value );
860 case CONFIG_ITEM_FLOAT:
861 if( !*psz_option_value )
862 break; /* ignore empty option */
863 p_item->f_value = (float)atof( psz_option_value);
865 msg_Dbg( p_this, "option \"%s\", value %f",
866 p_item->psz_name, (double)p_item->f_value );
869 case CONFIG_ITEM_KEY:
870 if( !*psz_option_value )
871 break; /* ignore empty option */
872 p_item->i_value = ConfigStringToKey( psz_option_value );
876 vlc_mutex_lock( p_item->p_lock );
878 /* free old string */
879 if( p_item->psz_value )
880 free( p_item->psz_value );
882 p_item->psz_value = *psz_option_value ?
883 strdup( psz_option_value ) : NULL;
885 vlc_mutex_unlock( p_item->p_lock );
888 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
890 p_item->psz_value ? p_item->psz_value : "" );
900 vlc_list_release( p_list );
903 free( psz_filename );
905 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
910 /*****************************************************************************
911 * config_CreateDir: Create configuration directory if it doesn't exist.
912 *****************************************************************************/
913 int config_CreateDir( vlc_object_t *p_this, char *psz_dirname )
915 if( !psz_dirname && !*psz_dirname ) return -1;
917 #if defined( UNDER_CE )
919 wchar_t psz_new[ MAX_PATH ];
920 char psz_mod[MAX_PATH];
923 /* Convert '/' into '\' */
924 while( *psz_dirname )
926 if( *psz_dirname == '/' ) psz_mod[i] = '\\';
927 else psz_mod[i] = *psz_dirname;
933 MultiByteToWideChar( CP_ACP, 0, psz_mod, -1, psz_new, MAX_PATH );
934 if( CreateDirectory( psz_new, NULL ) )
936 msg_Err( p_this, "could not create %s", psz_mod );
941 # if defined( WIN32 )
942 if( mkdir( psz_dirname ) && errno != EEXIST )
944 if( mkdir( psz_dirname, 0755 ) && errno != EEXIST )
947 msg_Err( p_this, "could not create %s (%s)",
948 psz_dirname, strerror(errno) );
956 /*****************************************************************************
957 * config_SaveConfigFile: Save a module's config options.
958 *****************************************************************************
959 * This will save the specified module's config options to the config file.
960 * If psz_module_name is NULL then we save all the modules config options.
961 * It's no use to save the config options that kept their default values, so
962 * we'll try to be a bit clever here.
964 * When we save we mustn't delete the config options of the modules that
965 * haven't been loaded. So we cannot just create a new config file with the
966 * config structures we've got in memory.
967 * I don't really know how to deal with this nicely, so I will use a completly
969 * I will load the config file in memory, but skipping all the sections of the
970 * modules we want to save. Then I will create a brand new file, dump the file
971 * loaded in memory and then append the sections of the modules we want to
974 *****************************************************************************/
975 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
979 module_config_t *p_item;
981 char p_line[1024], *p_index2;
983 char *p_bigbuffer, *p_index;
985 char *psz_filename, *psz_homedir, *psz_configfile;
988 /* Acquire config file lock */
989 vlc_mutex_lock( &p_this->p_vlc->config_lock );
991 psz_configfile = p_this->p_vlc->psz_configfile;
992 if( !psz_configfile || !psz_configfile )
994 psz_homedir = p_this->p_vlc->psz_homedir;
997 msg_Err( p_this, "psz_homedir is null" );
998 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1001 psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1002 strlen(psz_homedir) );
1005 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1009 msg_Err( p_this, "out of memory" );
1010 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1014 config_CreateDir( p_this, psz_filename );
1016 strcat( psz_filename, "/" CONFIG_FILE );
1020 psz_filename = strdup( psz_configfile );
1023 msg_Err( p_this, "out of memory" );
1024 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1029 msg_Dbg( p_this, "opening config file %s", psz_filename );
1031 file = fopen( psz_filename, "rt" );
1034 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1038 /* look for file size */
1039 fseek( file, 0L, SEEK_END );
1040 i_sizebuf = ftell( file );
1041 fseek( file, 0L, SEEK_SET );
1044 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1047 msg_Err( p_this, "out of memory" );
1048 if( file ) fclose( file );
1049 free( psz_filename );
1050 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1055 /* List all available modules */
1056 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1058 /* backup file into memory, we only need to backup the sections we won't
1061 while( file && fgets( p_line, 1024, file ) )
1063 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1066 /* we found a section, check if we need to do a backup */
1067 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1069 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1071 if( ((p_index2 - &p_line[1])
1072 == (int)strlen(p_parser->psz_object_name) )
1073 && !memcmp( &p_line[1], p_parser->psz_object_name,
1074 strlen(p_parser->psz_object_name) ) )
1076 if( !psz_module_name )
1078 else if( !strcmp( psz_module_name,
1079 p_parser->psz_object_name ) )
1084 if( i_index == p_list->i_count )
1086 /* we don't have this section in our list so we need to back
1089 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1101 /* save line if requested and line is valid (doesn't begin with a
1102 * space, tab, or eol) */
1103 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1104 && (p_line[0] != '\t') )
1106 strcpy( p_index, p_line );
1107 p_index += strlen( p_line );
1110 if( file ) fclose( file );
1114 * Save module config in file
1117 file = fopen( psz_filename, "wt" );
1120 msg_Warn( p_this, "could not open config file %s for writing",
1122 free( psz_filename );
1123 vlc_list_release( p_list );
1124 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1128 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
1130 /* Look for the selected module, if NULL then save everything */
1131 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1133 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1135 if( psz_module_name && strcmp( psz_module_name,
1136 p_parser->psz_object_name ) )
1139 if( !p_parser->i_config_items )
1142 msg_Dbg( p_this, "saving config for module \"%s\"",
1143 p_parser->psz_object_name );
1145 fprintf( file, "[%s]", p_parser->psz_object_name );
1146 if( p_parser->psz_longname )
1147 fprintf( file, " # %s\n\n", p_parser->psz_longname );
1149 fprintf( file, "\n\n" );
1151 for( p_item = p_parser->p_config;
1152 p_item->i_type != CONFIG_HINT_END;
1156 if( p_item->i_type & CONFIG_HINT )
1160 switch( p_item->i_type )
1162 case CONFIG_ITEM_BOOL:
1163 case CONFIG_ITEM_INTEGER:
1164 if( p_item->psz_text )
1165 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1166 (p_item->i_type == CONFIG_ITEM_BOOL) ?
1167 _("boolean") : _("integer") );
1168 if( p_item->i_value == p_item->i_value_orig )
1169 fprintf( file, "#" );
1170 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
1172 case CONFIG_ITEM_KEY:
1173 if( p_item->psz_text )
1174 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1176 if( p_item->i_value == p_item->i_value_orig )
1177 fprintf( file, "#" );
1178 psz_key = ConfigKeyToString( p_item->i_value );
1179 fprintf( file, "%s=%s\n", p_item->psz_name,
1180 psz_key ? psz_key : "" );
1181 if ( psz_key ) free( psz_key );
1184 case CONFIG_ITEM_FLOAT:
1185 if( p_item->psz_text )
1186 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1188 if( p_item->f_value == p_item->f_value_orig )
1189 fprintf( file, "#" );
1190 fprintf( file, "%s=%f\n", p_item->psz_name,
1191 (double)p_item->f_value );
1195 if( p_item->psz_text )
1196 fprintf( file, "# %s (%s)\n", p_item->psz_text,
1198 if( (!p_item->psz_value && !p_item->psz_value_orig) ||
1199 (p_item->psz_value && p_item->psz_value_orig &&
1200 !strcmp( p_item->psz_value, p_item->psz_value_orig )) )
1201 fprintf( file, "#" );
1202 fprintf( file, "%s=%s\n", p_item->psz_name,
1203 p_item->psz_value ? p_item->psz_value : "" );
1207 fprintf( file, "\n" );
1210 vlc_list_release( p_list );
1213 * Restore old settings from the config in file
1215 fputs( p_bigbuffer, file );
1216 free( p_bigbuffer );
1219 free( psz_filename );
1220 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1225 /*****************************************************************************
1226 * config_LoadCmdLine: parse command line
1227 *****************************************************************************
1228 * Parse command line for configuration options.
1229 * Now that the module_bank has been initialized, we can dynamically
1230 * generate the longopts structure used by getops. We have to do it this way
1231 * because we don't know (and don't want to know) in advance the configuration
1232 * options used (ie. exported) by each module.
1233 *****************************************************************************/
1234 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1235 vlc_bool_t b_ignore_errors )
1237 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1240 module_config_t *p_item;
1241 struct option *p_longopts;
1242 int i_modules_index;
1245 module_config_t *pp_shortopts[256];
1246 char *psz_shortopts;
1248 /* Set default configuration and copy arguments */
1249 p_this->p_vlc->i_argc = *pi_argc;
1250 p_this->p_vlc->ppsz_argv = ppsz_argv;
1253 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1254 * is the PSN - process serial number (a unique PID-ish thingie)
1255 * still ok for real Darwin & when run from command line */
1256 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1257 /* for example -psn_0_9306113 */
1259 /* GDMF!... I can't do this or else the MacOSX window server will
1260 * not pick up the PSN and not register the app and we crash...
1261 * hence the following kludge otherwise we'll get confused w/ argv[1]
1262 * being an input file name */
1264 ppsz_argv[ 1 ] = NULL;
1266 *pi_argc = *pi_argc - 1;
1272 /* List all modules */
1273 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1276 * Generate the longopts and shortopts structures used by getopt_long
1280 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1283 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1285 /* count the number of exported configuration options (to allocate
1286 * longopts). We also need to allocate space for too options when
1287 * dealing with boolean to allow for --foo and --no-foo */
1288 i_opts += p_parser->i_config_items
1289 + 2 * p_parser->i_bool_items;
1292 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1293 if( p_longopts == NULL )
1295 msg_Err( p_this, "out of memory" );
1296 vlc_list_release( p_list );
1300 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1301 if( psz_shortopts == NULL )
1303 msg_Err( p_this, "out of memory" );
1305 vlc_list_release( p_list );
1309 /* If we are requested to ignore errors, then we must work on a copy
1310 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1311 * us, ignoring the arity of the options */
1312 if( b_ignore_errors )
1314 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1315 if( ppsz_argv == NULL )
1317 msg_Err( p_this, "out of memory" );
1318 free( psz_shortopts );
1320 vlc_list_release( p_list );
1323 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1324 *pi_argc * sizeof(char *) );
1328 for( i_index = 0; i_index < 256; i_index++ )
1330 pp_shortopts[i_index] = NULL;
1333 /* Fill the p_longopts and psz_shortopts structures */
1335 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1338 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1340 if( !p_parser->i_config_items )
1343 for( p_item = p_parser->p_config;
1344 p_item->i_type != CONFIG_HINT_END;
1348 if( p_item->i_type & CONFIG_HINT )
1351 /* Add item to long options */
1352 p_longopts[i_index].name = strdup( p_item->psz_name );
1353 if( p_longopts[i_index].name == NULL ) continue;
1354 p_longopts[i_index].has_arg =
1355 (p_item->i_type == CONFIG_ITEM_BOOL)?
1356 no_argument : required_argument;
1357 p_longopts[i_index].flag = &flag;
1358 p_longopts[i_index].val = 0;
1361 /* When dealing with bools we also need to add the --no-foo
1363 if( p_item->i_type == CONFIG_ITEM_BOOL )
1365 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1366 if( psz_name == NULL ) continue;
1367 strcpy( psz_name, "no" );
1368 strcat( psz_name, p_item->psz_name );
1370 p_longopts[i_index].name = psz_name;
1371 p_longopts[i_index].has_arg = no_argument;
1372 p_longopts[i_index].flag = &flag;
1373 p_longopts[i_index].val = 1;
1376 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1377 if( psz_name == NULL ) continue;
1378 strcpy( psz_name, "no-" );
1379 strcat( psz_name, p_item->psz_name );
1381 p_longopts[i_index].name = psz_name;
1382 p_longopts[i_index].has_arg = no_argument;
1383 p_longopts[i_index].flag = &flag;
1384 p_longopts[i_index].val = 1;
1388 /* If item also has a short option, add it */
1389 if( p_item->i_short )
1391 pp_shortopts[(int)p_item->i_short] = p_item;
1392 psz_shortopts[i_shortopts] = p_item->i_short;
1394 if( p_item->i_type != CONFIG_ITEM_BOOL )
1396 psz_shortopts[i_shortopts] = ':';
1399 if( p_item->i_short == 'v' )
1401 psz_shortopts[i_shortopts] = ':';
1409 /* We don't need the module list anymore */
1410 vlc_list_release( p_list );
1412 /* Close the longopts and shortopts structures */
1413 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1414 psz_shortopts[i_shortopts] = '\0';
1417 * Parse the command line options
1421 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1422 p_longopts, &i_index ) ) != EOF )
1424 /* A long option has been recognized */
1427 module_config_t *p_conf;
1428 char *psz_name = (char *)p_longopts[i_index].name;
1430 /* Check if we deal with a --nofoo or --no-foo long option */
1431 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1433 /* Store the configuration option */
1434 p_conf = config_FindConfig( p_this, psz_name );
1436 if( p_conf ) switch( p_conf->i_type )
1438 case CONFIG_ITEM_STRING:
1439 case CONFIG_ITEM_FILE:
1440 case CONFIG_ITEM_DIRECTORY:
1441 case CONFIG_ITEM_MODULE:
1442 config_PutPsz( p_this, psz_name, optarg );
1444 case CONFIG_ITEM_INTEGER:
1445 config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1447 case CONFIG_ITEM_FLOAT:
1448 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1450 case CONFIG_ITEM_KEY:
1451 config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1453 case CONFIG_ITEM_BOOL:
1454 config_PutInt( p_this, psz_name, !flag );
1461 /* A short option has been recognized */
1462 if( pp_shortopts[i_cmd] != NULL )
1464 switch( pp_shortopts[i_cmd]->i_type )
1466 case CONFIG_ITEM_STRING:
1467 case CONFIG_ITEM_FILE:
1468 case CONFIG_ITEM_DIRECTORY:
1469 case CONFIG_ITEM_MODULE:
1470 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1472 case CONFIG_ITEM_INTEGER:
1477 if( *optarg == 'v' ) /* eg. -vvv */
1480 while( *optarg == 'v' )
1488 i_verbose += atoi( optarg ); /* eg. -v2 */
1493 i_verbose++; /* -v */
1495 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1500 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1501 strtol(optarg, 0, 0) );
1504 case CONFIG_ITEM_BOOL:
1505 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1512 /* Internal error: unknown option */
1513 if( !b_ignore_errors )
1515 fprintf( stderr, "%s: unknown option ",
1516 p_this->p_vlc->psz_object_name );
1519 fprintf( stderr, "`-%c'\n", optopt );
1523 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1525 fprintf( stderr, "Try `%s --help' for more information.\n",
1526 p_this->p_vlc->psz_object_name );
1529 free( psz_shortopts );
1534 /* Free allocated resources */
1535 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1536 free( (char *)p_longopts[i_index].name );
1538 free( psz_shortopts );
1539 if( b_ignore_errors ) free( ppsz_argv );
1544 /*****************************************************************************
1545 * config_GetHomeDir: find the user's home directory.
1546 *****************************************************************************
1547 * This function will try by different ways to find the user's home path.
1548 * Note that this function is not reentrant, it should be called only once
1549 * at the beginning of main where the result will be stored for later use.
1550 *****************************************************************************/
1551 char *config_GetHomeDir( void )
1553 char *p_tmp, *p_homedir = NULL;
1555 #if defined(HAVE_GETPWUID)
1556 struct passwd *p_pw = NULL;
1559 #if defined(WIN32) && !defined(UNDER_CE)
1560 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1562 #ifndef CSIDL_FLAG_CREATE
1563 # define CSIDL_FLAG_CREATE 0x8000
1565 #ifndef CSIDL_APPDATA
1566 # define CSIDL_APPDATA 0x1A
1568 #ifndef SHGFP_TYPE_CURRENT
1569 # define SHGFP_TYPE_CURRENT 0
1572 HINSTANCE shfolder_dll;
1573 SHGETFOLDERPATH SHGetFolderPath ;
1575 /* load the shfolder dll to retrieve SHGetFolderPath */
1576 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1578 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1579 _T("SHGetFolderPathA") );
1580 if ( SHGetFolderPath != NULL )
1582 p_homedir = (char *)malloc( MAX_PATH );
1583 if( !p_homedir ) return NULL;
1585 /* get the "Application Data" folder for the current user */
1586 if( S_OK == SHGetFolderPath( NULL,
1587 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1588 NULL, SHGFP_TYPE_CURRENT,
1591 FreeLibrary( shfolder_dll );
1597 FreeLibrary( shfolder_dll );
1600 #elif defined(UNDER_CE)
1602 wchar_t p_whomedir[MAX_PATH];
1604 /* get the "Application Data" folder for the current user */
1605 if( SHGetSpecialFolderPath( NULL, p_whomedir, CSIDL_APPDATA, 1 ) )
1607 p_homedir = (char *)malloc( MAX_PATH );
1608 if( !p_homedir ) return NULL;
1610 sprintf( p_homedir, "%ls", p_whomedir );
1615 #if defined(HAVE_GETPWUID)
1616 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1619 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1621 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1627 p_homedir = strdup( p_tmp );
1629 #if defined(HAVE_GETPWUID)
1632 p_homedir = strdup( p_pw->pw_dir );
1640 static int ConfigStringToKey( char *psz_key )
1644 char *psz_parser = strchr( psz_key, '-' );
1645 while( psz_parser && psz_parser != psz_key )
1647 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1649 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1650 strlen( vlc_modifiers[i].psz_key_string ) ) )
1652 i_key |= vlc_modifiers[i].i_key_code;
1655 psz_key = psz_parser + 1;
1656 psz_parser = strchr( psz_key, '-' );
1658 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1660 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1662 i_key |= vlc_keys[i].i_key_code;
1669 static char *ConfigKeyToString( int i_key )
1671 char *psz_key = malloc( 100 );
1681 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1684 if( i_key & vlc_modifiers[index].i_key_code )
1686 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1689 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1692 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1694 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );