1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.51 2003/02/20 01:52:47 sigmunau Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include <stdio.h> /* sprintf() */
27 #include <stdlib.h> /* free(), strtol() */
28 #include <string.h> /* strdup() */
30 # include <errno.h> /* errno */
34 # include <unistd.h> /* getuid() */
37 #ifdef HAVE_GETOPT_LONG
39 # include <getopt.h> /* getopt() */
42 # include "../extras/getopt.h"
45 #if defined(HAVE_GETPWUID)
46 # include <pwd.h> /* getpwuid() */
49 #if defined( HAVE_SYS_STAT_H )
50 # include <sys/stat.h>
52 #if defined( HAVE_SYS_TYPES_H )
53 # include <sys/types.h>
55 #if defined( WIN32 ) && !defined( UNDER_CE )
59 /*****************************************************************************
60 * config_GetInt: get the value of an int variable
61 *****************************************************************************
62 * This function is used to get the value of variables which are internally
63 * represented by an integer (CONFIG_ITEM_INTEGER and
65 *****************************************************************************/
66 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
68 module_config_t *p_config;
70 p_config = config_FindConfig( p_this, psz_name );
75 msg_Err( p_this, "option %s does not exist", psz_name );
78 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
79 (p_config->i_type!=CONFIG_ITEM_BOOL) )
81 msg_Err( p_this, "option %s does not refer to an int", psz_name );
85 return p_config->i_value;
88 /*****************************************************************************
89 * config_GetFloat: get the value of a float variable
90 *****************************************************************************
91 * This function is used to get the value of variables which are internally
92 * represented by a float (CONFIG_ITEM_FLOAT).
93 *****************************************************************************/
94 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
96 module_config_t *p_config;
98 p_config = config_FindConfig( p_this, psz_name );
103 msg_Err( p_this, "option %s does not exist", psz_name );
106 if( p_config->i_type != CONFIG_ITEM_FLOAT )
108 msg_Err( p_this, "option %s does not refer to a float", psz_name );
112 return p_config->f_value;
115 /*****************************************************************************
116 * config_GetPsz: get the string value of a string variable
117 *****************************************************************************
118 * This function is used to get the value of variables which are internally
119 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
120 * and CONFIG_ITEM_MODULE).
122 * Important note: remember to free() the returned char* because it's a
123 * duplicate of the actual value. It isn't safe to return a pointer to the
124 * actual value as it can be modified at any time.
125 *****************************************************************************/
126 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
128 module_config_t *p_config;
129 char *psz_value = NULL;
131 p_config = config_FindConfig( p_this, psz_name );
136 msg_Err( p_this, "option %s does not exist", psz_name );
139 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
140 (p_config->i_type!=CONFIG_ITEM_FILE) &&
141 (p_config->i_type!=CONFIG_ITEM_MODULE) )
143 msg_Err( p_this, "option %s does not refer to a string", psz_name );
147 /* return a copy of the string */
148 vlc_mutex_lock( p_config->p_lock );
149 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
150 vlc_mutex_unlock( p_config->p_lock );
155 /*****************************************************************************
156 * config_PutPsz: set the string value of a string variable
157 *****************************************************************************
158 * This function is used to set the value of variables which are internally
159 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
160 * and CONFIG_ITEM_MODULE).
161 *****************************************************************************/
162 void __config_PutPsz( vlc_object_t *p_this,
163 const char *psz_name, const char *psz_value )
165 module_config_t *p_config;
167 p_config = config_FindConfig( p_this, psz_name );
172 msg_Err( p_this, "option %s does not exist", psz_name );
175 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
176 (p_config->i_type!=CONFIG_ITEM_FILE) &&
177 (p_config->i_type!=CONFIG_ITEM_MODULE) )
179 msg_Err( p_this, "option %s does not refer to a string", psz_name );
183 vlc_mutex_lock( p_config->p_lock );
185 /* free old string */
186 if( p_config->psz_value ) free( p_config->psz_value );
188 if( psz_value ) p_config->psz_value = strdup( psz_value );
189 else p_config->psz_value = NULL;
191 vlc_mutex_unlock( p_config->p_lock );
193 if( p_config->pf_callback )
195 p_config->pf_callback( p_this );
199 /*****************************************************************************
200 * config_PutInt: set the integer value of an int variable
201 *****************************************************************************
202 * This function is used to set the value of variables which are internally
203 * represented by an integer (CONFIG_ITEM_INTEGER and
205 *****************************************************************************/
206 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
208 module_config_t *p_config;
210 p_config = config_FindConfig( p_this, psz_name );
215 msg_Err( p_this, "option %s does not exist", psz_name );
218 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
219 (p_config->i_type!=CONFIG_ITEM_BOOL) )
221 msg_Err( p_this, "option %s does not refer to an int", psz_name );
225 /* if i_min == i_max == 0, then do not use them */
226 if ((p_config->i_min == 0) && (p_config->i_max == 0))
228 p_config->i_value = i_value;
230 else if (i_value < p_config->i_min)
232 p_config->i_value = p_config->i_min;
234 else if (i_value > p_config->i_max)
236 p_config->i_value = p_config->i_max;
240 p_config->i_value = i_value;
243 if( p_config->pf_callback )
245 p_config->pf_callback( p_this );
249 /*****************************************************************************
250 * config_PutFloat: set the value of a float variable
251 *****************************************************************************
252 * This function is used to set the value of variables which are internally
253 * represented by a float (CONFIG_ITEM_FLOAT).
254 *****************************************************************************/
255 void __config_PutFloat( vlc_object_t *p_this,
256 const char *psz_name, float f_value )
258 module_config_t *p_config;
260 p_config = config_FindConfig( p_this, psz_name );
265 msg_Err( p_this, "option %s does not exist", psz_name );
268 if( p_config->i_type != CONFIG_ITEM_FLOAT )
270 msg_Err( p_this, "option %s does not refer to a float", psz_name );
274 /* if f_min == f_max == 0, then do not use them */
275 if ((p_config->f_min == 0) && (p_config->f_max == 0))
277 p_config->f_value = f_value;
279 else if (f_value < p_config->f_min)
281 p_config->f_value = p_config->f_min;
283 else if (f_value > p_config->f_max)
285 p_config->f_value = p_config->f_max;
289 p_config->f_value = f_value;
292 if( p_config->pf_callback )
294 p_config->pf_callback( p_this );
298 /*****************************************************************************
299 * config_FindConfig: find the config structure associated with an option.
300 *****************************************************************************
301 * FIXME: This function really needs to be optimized.
302 * FIXME: And now even more.
303 *****************************************************************************/
304 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
308 module_config_t *p_item;
311 if( !psz_name ) return NULL;
313 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
315 for( i_index = 0; i_index < p_list->i_count; i_index++ )
317 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
319 if( !p_parser->i_config_items )
322 for( p_item = p_parser->p_config;
323 p_item->i_type != CONFIG_HINT_END;
326 if( p_item->i_type & CONFIG_HINT )
329 if( !strcmp( psz_name, p_item->psz_name ) )
331 vlc_list_release( p_list );
337 vlc_list_release( p_list );
342 /*****************************************************************************
343 * config_Duplicate: creates a duplicate of a module's configuration data.
344 *****************************************************************************
345 * Unfortunatly we cannot work directly with the module's config data as
346 * this module might be unloaded from memory at any time (remember HideModule).
347 * This is why we need to create an exact copy of the config data.
348 *****************************************************************************/
349 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
351 int i, j, i_lines = 1;
352 module_config_t *p_item;
354 /* Calculate the structure length */
355 p_module->i_config_items = 0;
356 p_module->i_bool_items = 0;
358 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
362 if( p_item->i_type & CONFIG_ITEM )
364 p_module->i_config_items++;
367 if( p_item->i_type == CONFIG_ITEM_BOOL )
369 p_module->i_bool_items++;
373 /* Allocate memory */
374 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
376 if( p_module->p_config == NULL )
378 msg_Err( p_module, "config error: can't duplicate p_config" );
382 /* Do the duplication job */
383 for( i = 0; i < i_lines ; i++ )
385 p_module->p_config[i].i_type = p_orig[i].i_type;
386 p_module->p_config[i].i_short = p_orig[i].i_short;
387 p_module->p_config[i].i_value = p_orig[i].i_value;
388 p_module->p_config[i].i_min = p_orig[i].i_min;
389 p_module->p_config[i].i_max = p_orig[i].i_max;
390 p_module->p_config[i].f_value = p_orig[i].f_value;
391 p_module->p_config[i].f_min = p_orig[i].f_min;
392 p_module->p_config[i].f_max = p_orig[i].f_max;
393 p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
394 p_module->p_config[i].b_advanced = p_orig[i].b_advanced;
396 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
397 strdup( _(p_orig[i].psz_type) ) : NULL;
398 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
399 strdup( _(p_orig[i].psz_name) ) : NULL;
400 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
401 strdup( _(p_orig[i].psz_text) ) : NULL;
402 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
403 strdup( _(p_orig[i].psz_longtext) ) : NULL;
404 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
405 strdup( p_orig[i].psz_value ) : NULL;
407 p_module->p_config[i].p_lock = &p_module->object_lock;
409 /* duplicate the string list */
410 p_module->p_config[i].ppsz_list = NULL;
411 if( p_orig[i].ppsz_list )
413 for( j = 0; p_orig[i].ppsz_list[j]; j++ );
414 p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
415 if( p_module->p_config[i].ppsz_list )
417 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
418 p_module->p_config[i].ppsz_list[j] =
419 strdup( p_orig[i].ppsz_list[j] );
421 p_module->p_config[i].ppsz_list[j] = NULL;
424 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
428 /*****************************************************************************
429 * config_Free: frees a duplicated module's configuration data.
430 *****************************************************************************
431 * This function frees all the data duplicated by config_Duplicate.
432 *****************************************************************************/
433 void config_Free( module_t *p_module )
435 module_config_t *p_item = p_module->p_config;
443 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
445 if( p_item->psz_type )
446 free( p_item->psz_type );
448 if( p_item->psz_name )
449 free( p_item->psz_name );
451 if( p_item->psz_text )
452 free( p_item->psz_text );
454 if( p_item->psz_longtext )
455 free( p_item->psz_longtext );
457 if( p_item->psz_value )
458 free( p_item->psz_value );
460 if( p_item->ppsz_list )
462 for( i = 0; p_item->ppsz_list[i]; i++ )
463 free(p_item->ppsz_list[i]);
464 free( p_item->ppsz_list );
468 free( p_module->p_config );
469 p_module->p_config = NULL;
472 /*****************************************************************************
473 * config_SetCallbacks: sets callback functions in the duplicate p_config.
474 *****************************************************************************
475 * Unfortunatly we cannot work directly with the module's config data as
476 * this module might be unloaded from memory at any time (remember HideModule).
477 * This is why we need to duplicate callbacks each time we reload the module.
478 *****************************************************************************/
479 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
481 while( p_new->i_type != CONFIG_HINT_END )
483 p_new->pf_callback = p_orig->pf_callback;
489 /*****************************************************************************
490 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
491 *****************************************************************************
492 * We simply undo what we did in config_SetCallbacks.
493 *****************************************************************************/
494 void config_UnsetCallbacks( module_config_t *p_new )
496 while( p_new->i_type != CONFIG_HINT_END )
498 p_new->pf_callback = NULL;
503 /*****************************************************************************
504 * config_LoadConfigFile: loads the configuration file.
505 *****************************************************************************
506 * This function is called to load the config options stored in the config
508 *****************************************************************************/
509 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
513 module_config_t *p_item;
516 char *p_index, *psz_option_name, *psz_option_value;
517 char *psz_filename, *psz_homedir;
520 psz_homedir = p_this->p_vlc->psz_homedir;
523 msg_Err( p_this, "psz_homedir is null" );
526 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
527 strlen(psz_homedir) + 1 );
530 msg_Err( p_this, "out of memory" );
533 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
535 msg_Dbg( p_this, "opening config file %s", psz_filename );
537 /* Acquire config file lock */
538 vlc_mutex_lock( &p_this->p_vlc->config_lock );
540 file = fopen( psz_filename, "rt" );
543 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
544 free( psz_filename );
545 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
549 /* Look for the selected module, if NULL then save everything */
550 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
552 for( i_index = 0; i_index < p_list->i_count; i_index++ )
554 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
557 && strcmp( psz_module_name, p_parser->psz_object_name ) )
562 /* The config file is organized in sections, one per module. Look for
563 * the interesting section ( a section is of the form [foo] ) */
564 fseek( file, 0L, SEEK_SET );
565 while( fgets( line, 1024, file ) )
568 && (p_index = strchr(line,']'))
569 && (p_index - &line[1]
570 == (int)strlen(p_parser->psz_object_name))
571 && !memcmp( &line[1], p_parser->psz_object_name,
572 strlen(p_parser->psz_object_name) ) )
575 msg_Dbg( p_this, "loading config for module \"%s\"",
576 p_parser->psz_object_name );
582 /* either we found the section or we're at the EOF */
584 /* Now try to load the options in this section */
585 while( fgets( line, 1024, file ) )
587 if( line[0] == '[' ) break; /* end of section */
589 /* ignore comments or empty lines */
590 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
593 /* get rid of line feed */
594 if( line[strlen(line)-1] == '\n' )
595 line[strlen(line)-1] = (char)0;
597 /* look for option name */
598 psz_option_name = line;
599 psz_option_value = NULL;
600 p_index = strchr( line, '=' );
601 if( !p_index ) break; /* this ain't an option!!! */
604 psz_option_value = p_index + 1;
606 if( !p_parser->i_config_items )
611 /* try to match this option with one of the module's options */
612 for( p_item = p_parser->p_config;
613 p_item->i_type != CONFIG_HINT_END;
616 if( p_item->i_type & CONFIG_HINT )
620 if( !strcmp( p_item->psz_name, psz_option_name ) )
623 switch( p_item->i_type )
625 case CONFIG_ITEM_BOOL:
626 case CONFIG_ITEM_INTEGER:
627 if( !*psz_option_value )
628 break; /* ignore empty option */
629 p_item->i_value = atoi( psz_option_value);
631 msg_Dbg( p_this, "option \"%s\", value %i",
632 p_item->psz_name, p_item->i_value );
636 case CONFIG_ITEM_FLOAT:
637 if( !*psz_option_value )
638 break; /* ignore empty option */
639 p_item->f_value = (float)atof( psz_option_value);
641 msg_Dbg( p_this, "option \"%s\", value %f",
642 p_item->psz_name, (double)p_item->f_value );
647 vlc_mutex_lock( p_item->p_lock );
649 /* free old string */
650 if( p_item->psz_value )
651 free( p_item->psz_value );
653 p_item->psz_value = *psz_option_value ?
654 strdup( psz_option_value ) : NULL;
656 vlc_mutex_unlock( p_item->p_lock );
659 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
661 p_item->psz_value ? p_item->psz_value : "" );
671 vlc_list_release( p_list );
674 free( psz_filename );
676 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
681 /*****************************************************************************
682 * config_SaveConfigFile: Save a module's config options.
683 *****************************************************************************
684 * This will save the specified module's config options to the config file.
685 * If psz_module_name is NULL then we save all the modules config options.
686 * It's no use to save the config options that kept their default values, so
687 * we'll try to be a bit clever here.
689 * When we save we mustn't delete the config options of the modules that
690 * haven't been loaded. So we cannot just create a new config file with the
691 * config structures we've got in memory.
692 * I don't really know how to deal with this nicely, so I will use a completly
694 * I will load the config file in memory, but skipping all the sections of the
695 * modules we want to save. Then I will create a brand new file, dump the file
696 * loaded in memory and then append the sections of the modules we want to
699 *****************************************************************************/
700 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
704 module_config_t *p_item;
706 char p_line[1024], *p_index2;
708 char *p_bigbuffer, *p_index;
710 char *psz_filename, *psz_homedir;
713 /* Acquire config file lock */
714 vlc_mutex_lock( &p_this->p_vlc->config_lock );
716 psz_homedir = p_this->p_vlc->psz_homedir;
719 msg_Err( p_this, "psz_homedir is null" );
720 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
723 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
724 strlen(psz_homedir) + 1 );
727 msg_Err( p_this, "out of memory" );
728 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
731 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
733 #if defined( UNDER_CE )
735 wchar_t psz_new[ MAX_PATH ];
736 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
737 if( CreateDirectory( psz_new, NULL ) )
739 msg_Err( p_this, "could not create %s", psz_filename );
743 #elif defined( HAVE_ERRNO_H )
744 # if defined( WIN32 )
745 if( mkdir( psz_filename ) && errno != EEXIST )
747 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
750 msg_Err( p_this, "could not create %s (%s)",
751 psz_filename, strerror(errno) );
755 if( mkdir( psz_filename ) )
757 msg_Err( p_this, "could not create %s", psz_filename );
762 strcat( psz_filename, "/" CONFIG_FILE );
765 msg_Dbg( p_this, "opening config file %s", psz_filename );
767 file = fopen( psz_filename, "rt" );
770 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
774 /* look for file size */
775 fseek( file, 0L, SEEK_END );
776 i_sizebuf = ftell( file );
777 fseek( file, 0L, SEEK_SET );
780 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
783 msg_Err( p_this, "out of memory" );
784 if( file ) fclose( file );
785 free( psz_filename );
786 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
791 /* List all available modules */
792 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
794 /* backup file into memory, we only need to backup the sections we won't
797 while( file && fgets( p_line, 1024, file ) )
799 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
802 /* we found a section, check if we need to do a backup */
803 for( i_index = 0; i_index < p_list->i_count; i_index++ )
805 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
807 if( ((p_index2 - &p_line[1])
808 == (int)strlen(p_parser->psz_object_name) )
809 && !memcmp( &p_line[1], p_parser->psz_object_name,
810 strlen(p_parser->psz_object_name) ) )
812 if( !psz_module_name )
814 else if( !strcmp( psz_module_name,
815 p_parser->psz_object_name ) )
820 if( i_index == p_list->i_count )
822 /* we don't have this section in our list so we need to back
825 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
837 /* save line if requested and line is valid (doesn't begin with a
838 * space, tab, or eol) */
839 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
840 && (p_line[0] != '\t') )
842 strcpy( p_index, p_line );
843 p_index += strlen( p_line );
846 if( file ) fclose( file );
850 * Save module config in file
853 file = fopen( psz_filename, "wt" );
856 msg_Warn( p_this, "could not open config file %s for writing",
858 free( psz_filename );
859 vlc_list_release( p_list );
860 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
864 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
866 /* Look for the selected module, if NULL then save everything */
867 for( i_index = 0; i_index < p_list->i_count; i_index++ )
869 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
871 if( psz_module_name && strcmp( psz_module_name,
872 p_parser->psz_object_name ) )
875 if( !p_parser->i_config_items )
878 msg_Dbg( p_this, "saving config for module \"%s\"",
879 p_parser->psz_object_name );
881 fprintf( file, "[%s]", p_parser->psz_object_name );
882 if( p_parser->psz_longname )
883 fprintf( file, " # %s\n\n", p_parser->psz_longname );
885 fprintf( file, "\n\n" );
887 for( p_item = p_parser->p_config;
888 p_item->i_type != CONFIG_HINT_END;
891 if( p_item->i_type & CONFIG_HINT )
895 switch( p_item->i_type )
897 case CONFIG_ITEM_BOOL:
898 case CONFIG_ITEM_INTEGER:
899 if( p_item->psz_text )
900 fprintf( file, "# %s (%s)\n", p_item->psz_text,
901 (p_item->i_type == CONFIG_ITEM_BOOL) ?
902 _("boolean") : _("integer") );
903 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
906 case CONFIG_ITEM_FLOAT:
907 if( p_item->psz_text )
908 fprintf( file, "# %s (%s)\n", p_item->psz_text,
910 fprintf( file, "%s=%f\n", p_item->psz_name,
911 (double)p_item->f_value );
915 if( p_item->psz_text )
916 fprintf( file, "# %s (%s)\n", p_item->psz_text,
918 fprintf( file, "%s=%s\n", p_item->psz_name,
919 p_item->psz_value ? p_item->psz_value : "" );
923 fprintf( file, "\n" );
926 vlc_list_release( p_list );
929 * Restore old settings from the config in file
931 fputs( p_bigbuffer, file );
935 free( psz_filename );
936 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
941 /*****************************************************************************
942 * config_LoadCmdLine: parse command line
943 *****************************************************************************
944 * Parse command line for configuration options.
945 * Now that the module_bank has been initialized, we can dynamically
946 * generate the longopts structure used by getops. We have to do it this way
947 * because we don't know (and don't want to know) in advance the configuration
948 * options used (ie. exported) by each module.
949 *****************************************************************************/
950 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
951 vlc_bool_t b_ignore_errors )
953 int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
956 module_config_t *p_item;
957 struct option *p_longopts;
961 module_config_t *pp_shortopts[256];
964 /* Set default configuration and copy arguments */
965 p_this->p_vlc->i_argc = *pi_argc;
966 p_this->p_vlc->ppsz_argv = ppsz_argv;
968 p_this->p_vlc->p_channel = NULL;
971 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
972 * is the PSN - process serial number (a unique PID-ish thingie)
973 * still ok for real Darwin & when run from command line */
974 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
975 /* for example -psn_0_9306113 */
977 /* GDMF!... I can't do this or else the MacOSX window server will
978 * not pick up the PSN and not register the app and we crash...
979 * hence the following kludge otherwise we'll get confused w/ argv[1]
980 * being an input file name */
982 ppsz_argv[ 1 ] = NULL;
984 *pi_argc = *pi_argc - 1;
990 /* List all modules */
991 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
994 * Generate the longopts and shortopts structures used by getopt_long
998 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1001 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1003 /* count the number of exported configuration options (to allocate
1004 * longopts). We also need to allocate space for too options when
1005 * dealing with boolean to allow for --foo and --no-foo */
1006 i_opts += p_parser->i_config_items
1007 + 2 * p_parser->i_bool_items;
1010 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1011 if( p_longopts == NULL )
1013 msg_Err( p_this, "out of memory" );
1014 vlc_list_release( p_list );
1018 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1019 if( psz_shortopts == NULL )
1021 msg_Err( p_this, "out of memory" );
1023 vlc_list_release( p_list );
1027 /* If we are requested to ignore errors, then we must work on a copy
1028 * of the ppsz_argv array, otherwise getopt_long will reorder it for
1029 * us, ignoring the arity of the options */
1030 if( b_ignore_errors )
1032 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1033 if( ppsz_argv == NULL )
1035 msg_Err( p_this, "out of memory" );
1036 free( psz_shortopts );
1038 vlc_list_release( p_list );
1041 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1042 *pi_argc * sizeof(char *) );
1046 for( i_index = 0; i_index < 256; i_index++ )
1048 pp_shortopts[i_index] = NULL;
1051 /* Fill the p_longopts and psz_shortopts structures */
1053 for( i_modules_index = 0; i_modules_index < p_list->i_count;
1056 p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1058 if( !p_parser->i_config_items )
1061 for( p_item = p_parser->p_config;
1062 p_item->i_type != CONFIG_HINT_END;
1066 if( p_item->i_type & CONFIG_HINT )
1069 /* Add item to long options */
1070 p_longopts[i_index].name = strdup( p_item->psz_name );
1071 if( p_longopts[i_index].name == NULL ) continue;
1072 p_longopts[i_index].has_arg =
1073 (p_item->i_type == CONFIG_ITEM_BOOL)?
1074 no_argument : required_argument;
1075 p_longopts[i_index].flag = &flag;
1076 p_longopts[i_index].val = 0;
1079 /* When dealing with bools we also need to add the --no-foo
1081 if( p_item->i_type == CONFIG_ITEM_BOOL )
1083 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1084 if( psz_name == NULL ) continue;
1085 strcpy( psz_name, "no" );
1086 strcat( psz_name, p_item->psz_name );
1088 p_longopts[i_index].name = psz_name;
1089 p_longopts[i_index].has_arg = no_argument;
1090 p_longopts[i_index].flag = &flag;
1091 p_longopts[i_index].val = 1;
1094 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1095 if( psz_name == NULL ) continue;
1096 strcpy( psz_name, "no-" );
1097 strcat( psz_name, p_item->psz_name );
1099 p_longopts[i_index].name = psz_name;
1100 p_longopts[i_index].has_arg = no_argument;
1101 p_longopts[i_index].flag = &flag;
1102 p_longopts[i_index].val = 1;
1106 /* If item also has a short option, add it */
1107 if( p_item->i_short )
1109 pp_shortopts[(int)p_item->i_short] = p_item;
1110 psz_shortopts[i_shortopts] = p_item->i_short;
1112 if( p_item->i_type != CONFIG_ITEM_BOOL )
1114 psz_shortopts[i_shortopts] = ':';
1117 if( p_item->i_short == 'v' )
1119 psz_shortopts[i_shortopts] = ':';
1127 /* We don't need the module list anymore */
1128 vlc_list_release( p_list );
1130 /* Close the longopts and shortopts structures */
1131 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1132 psz_shortopts[i_shortopts] = '\0';
1135 * Parse the command line options
1139 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1140 p_longopts, &i_index ) ) != EOF )
1142 /* A long option has been recognized */
1145 module_config_t *p_conf;
1146 char *psz_name = (char *)p_longopts[i_index].name;
1148 /* Check if we deal with a --nofoo or --no-foo long option */
1149 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1151 /* Store the configuration option */
1152 p_conf = config_FindConfig( p_this, psz_name );
1154 if( p_conf ) switch( p_conf->i_type )
1156 case CONFIG_ITEM_STRING:
1157 case CONFIG_ITEM_FILE:
1158 case CONFIG_ITEM_MODULE:
1159 config_PutPsz( p_this, psz_name, optarg );
1161 case CONFIG_ITEM_INTEGER:
1162 config_PutInt( p_this, psz_name, atoi(optarg));
1164 case CONFIG_ITEM_FLOAT:
1165 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1167 case CONFIG_ITEM_BOOL:
1168 config_PutInt( p_this, psz_name, !flag );
1175 /* A short option has been recognized */
1176 if( pp_shortopts[i_cmd] != NULL )
1178 switch( pp_shortopts[i_cmd]->i_type )
1180 case CONFIG_ITEM_STRING:
1181 case CONFIG_ITEM_FILE:
1182 case CONFIG_ITEM_MODULE:
1183 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1185 case CONFIG_ITEM_INTEGER:
1190 if( *optarg == 'v' ) /* eg. -vvv */
1193 while( *optarg == 'v' )
1201 i_verbose += atoi( optarg ); /* eg. -v2 */
1206 i_verbose++; /* -v */
1208 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1213 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1217 case CONFIG_ITEM_BOOL:
1218 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1225 /* Internal error: unknown option */
1226 if( !b_ignore_errors )
1228 fprintf( stderr, "%s: unknown option ",
1229 p_this->p_vlc->psz_object_name );
1232 fprintf( stderr, "`-%c'\n", optopt );
1236 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1238 fprintf( stderr, "Try `%s --help' for more information.\n",
1239 p_this->p_vlc->psz_object_name );
1242 free( psz_shortopts );
1243 if( b_ignore_errors ) free( ppsz_argv );
1248 /* Free allocated resources */
1249 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1250 free( (char *)p_longopts[i_index].name );
1252 free( psz_shortopts );
1253 if( b_ignore_errors ) free( ppsz_argv );
1258 /*****************************************************************************
1259 * config_GetHomeDir: find the user's home directory.
1260 *****************************************************************************
1261 * This function will try by different ways to find the user's home path.
1262 * Note that this function is not reentrant, it should be called only once
1263 * at the beginning of main where the result will be stored for later use.
1264 *****************************************************************************/
1265 char *config_GetHomeDir( void )
1267 char *p_tmp, *p_homedir = NULL;
1269 #if defined(HAVE_GETPWUID)
1270 struct passwd *p_pw = NULL;
1273 #if defined(WIN32) || defined(UNDER_CE)
1274 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1276 # define CSIDL_FLAG_CREATE 0x8000
1277 # define CSIDL_APPDATA 0x1A
1278 # define SHGFP_TYPE_CURRENT 0
1280 HINSTANCE shfolder_dll;
1281 SHGETFOLDERPATH SHGetFolderPath ;
1283 /* load the shell32 dll to retreive SHGetFolderPath */
1284 if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1286 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1287 "SHGetFolderPathA" );
1288 if ( SHGetFolderPath != NULL )
1290 p_homedir = (char *)malloc( MAX_PATH );
1296 /* get the "Application Data" folder for the current user */
1297 if( S_OK == SHGetFolderPath( NULL,
1298 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1299 NULL, SHGFP_TYPE_CURRENT,
1302 FreeLibrary( shfolder_dll );
1307 FreeLibrary( shfolder_dll );
1311 #if defined(HAVE_GETPWUID)
1312 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1315 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1317 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1323 p_homedir = strdup( p_tmp );
1325 #if defined(HAVE_GETPWUID)
1328 p_homedir = strdup( p_pw->pw_dir );