1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.24 2002/05/18 13:30:28 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #include <videolan/vlc.h>
26 #include <stdio.h> /* sprintf() */
27 #include <stdlib.h> /* free(), strtol() */
28 #include <string.h> /* strdup() */
29 #include <errno.h> /* errno */
32 # include <unistd.h> /* getuid() */
35 #ifdef HAVE_GETOPT_LONG
37 # include <getopt.h> /* getopt() */
40 # include "GNUgetopt/getopt.h"
43 #if defined(HAVE_GETPWUID)
44 #include <pwd.h> /* getpwuid() */
48 #include <sys/types.h>
50 /*****************************************************************************
51 * config_GetIntVariable: get the value of an int variable
52 *****************************************************************************
53 * This function is used to get the value of variables which are internally
54 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
55 * MODULE_CONFIG_ITEM_BOOL).
56 *****************************************************************************/
57 int config_GetIntVariable( const char *psz_name )
59 module_config_t *p_config;
61 p_config = config_FindConfig( psz_name );
66 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
69 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
70 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
72 intf_ErrMsg( "config error: option %s doesn't refer to an int",
77 return p_config->i_value;
80 /*****************************************************************************
81 * config_GetFloatVariable: get the value of a float variable
82 *****************************************************************************
83 * This function is used to get the value of variables which are internally
84 * represented by a float (MODULE_CONFIG_ITEM_FLOAT).
85 *****************************************************************************/
86 float config_GetFloatVariable( const char *psz_name )
88 module_config_t *p_config;
90 p_config = config_FindConfig( psz_name );
95 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
98 if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
100 intf_ErrMsg( "config error: option %s doesn't refer to a float",
105 return p_config->f_value;
108 /*****************************************************************************
109 * config_GetPszVariable: get the string value of a string variable
110 *****************************************************************************
111 * This function is used to get the value of variables which are internally
112 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
113 * and MODULE_CONFIG_ITEM_MODULE).
115 * Important note: remember to free() the returned char* because it a duplicate
116 * of the actual value. It isn't safe to return a pointer to the actual value
117 * as it can be modified at any time.
118 *****************************************************************************/
119 char * config_GetPszVariable( const char *psz_name )
121 module_config_t *p_config;
122 char *psz_value = NULL;
124 p_config = config_FindConfig( psz_name );
129 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
132 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
133 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
134 (p_config->i_type!=MODULE_CONFIG_ITEM_MODULE) )
136 intf_ErrMsg( "config error: option %s doesn't refer to a string",
141 /* return a copy of the string */
142 vlc_mutex_lock( p_config->p_lock );
143 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
144 vlc_mutex_unlock( p_config->p_lock );
149 /*****************************************************************************
150 * config_PutPszVariable: set the string value of a string variable
151 *****************************************************************************
152 * This function is used to set the value of variables which are internally
153 * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
154 * and MODULE_CONFIG_ITEM_MODULE).
155 *****************************************************************************/
156 void config_PutPszVariable( const char *psz_name, char *psz_value )
158 module_config_t *p_config;
160 p_config = config_FindConfig( psz_name );
165 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
168 if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
169 (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
170 (p_config->i_type!=MODULE_CONFIG_ITEM_MODULE) )
172 intf_ErrMsg( "config error: option %s doesn't refer to a string",
177 vlc_mutex_lock( p_config->p_lock );
179 /* free old string */
180 if( p_config->psz_value ) free( p_config->psz_value );
182 if( psz_value ) p_config->psz_value = strdup( psz_value );
183 else p_config->psz_value = NULL;
185 vlc_mutex_unlock( p_config->p_lock );
187 if( p_config->p_callback )
189 ((void(*)(void))p_config->p_callback)();
193 /*****************************************************************************
194 * config_PutIntVariable: set the integer value of an int variable
195 *****************************************************************************
196 * This function is used to set the value of variables which are internally
197 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
198 * MODULE_CONFIG_ITEM_BOOL).
199 *****************************************************************************/
200 void config_PutIntVariable( const char *psz_name, int i_value )
202 module_config_t *p_config;
204 p_config = config_FindConfig( psz_name );
209 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
212 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
213 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
215 intf_ErrMsg( "config error: option %s doesn't refer to an int",
220 p_config->i_value = i_value;
222 if( p_config->p_callback )
224 ((void(*)(void))p_config->p_callback)();
228 /*****************************************************************************
229 * config_PutFloatVariable: set the value of a float variable
230 *****************************************************************************
231 * This function is used to set the value of variables which are internally
232 * represented by a float (MODULE_CONFIG_ITEM_FLOAT).
233 *****************************************************************************/
234 void config_PutFloatVariable( const char *psz_name, float f_value )
236 module_config_t *p_config;
238 p_config = config_FindConfig( psz_name );
243 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
246 if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
248 intf_ErrMsg( "config error: option %s doesn't refer to a float",
253 p_config->f_value = f_value;
255 if( p_config->p_callback )
257 ((void(*)(void))p_config->p_callback)();
261 /*****************************************************************************
262 * config_FindConfig: find the config structure associated with an option.
263 *****************************************************************************
264 * FIXME: This function really needs to be optimized.
265 *****************************************************************************/
266 module_config_t *config_FindConfig( const char *psz_name )
269 module_config_t *p_item;
271 if( !psz_name ) return NULL;
273 for( p_module = p_module_bank->first ;
275 p_module = p_module->next )
277 for( p_item = p_module->p_config;
278 p_item->i_type != MODULE_CONFIG_HINT_END;
281 if( p_item->i_type & MODULE_CONFIG_HINT )
284 if( !strcmp( psz_name, p_item->psz_name ) )
292 /*****************************************************************************
293 * config_Duplicate: creates a duplicate of a module's configuration data.
294 *****************************************************************************
295 * Unfortunatly we cannot work directly with the module's config data as
296 * this module might be unloaded from memory at any time (remember HideModule).
297 * This is why we need to create an exact copy of the config data.
298 *****************************************************************************/
299 module_config_t *config_Duplicate( module_config_t *p_orig )
302 module_config_t *p_config;
304 /* Calculate the structure length */
305 for( p_config = p_orig, i_lines = 1;
306 p_config->i_type != MODULE_CONFIG_HINT_END;
307 p_config++, i_lines++ );
309 /* Allocate memory */
310 p_config = (module_config_t *)malloc( sizeof(module_config_t) * i_lines );
311 if( p_config == NULL )
313 intf_ErrMsg( "config error: can't duplicate p_config" );
317 /* Do the duplication job */
318 for( i = 0; i < i_lines ; i++ )
320 p_config[i].i_type = p_orig[i].i_type;
321 p_config[i].i_short = p_orig[i].i_short;
322 p_config[i].i_value = p_orig[i].i_value;
323 p_config[i].f_value = p_orig[i].f_value;
324 p_config[i].b_dirty = p_orig[i].b_dirty;
326 p_config[i].psz_name = p_orig[i].psz_name ?
327 strdup( _(p_orig[i].psz_name) ) : NULL;
328 p_config[i].psz_text = p_orig[i].psz_text ?
329 strdup( _(p_orig[i].psz_text) ) : NULL;
330 p_config[i].psz_longtext = p_orig[i].psz_longtext ?
331 strdup( _(p_orig[i].psz_longtext) ) : NULL;
332 p_config[i].psz_value = p_orig[i].psz_value ?
333 strdup( _(p_orig[i].psz_value) ) : NULL;
335 /* the callback pointer is only valid when the module is loaded so this
336 * value is set in ActivateModule() and reset in DeactivateModule() */
337 p_config[i].p_callback = NULL;
343 /*****************************************************************************
344 * config_SetCallbacks: sets callback functions in the duplicate p_config.
345 *****************************************************************************
346 * Unfortunatly we cannot work directly with the module's config data as
347 * this module might be unloaded from memory at any time (remember HideModule).
348 * This is why we need to duplicate callbacks each time we reload the module.
349 *****************************************************************************/
350 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
352 while( p_new->i_type != MODULE_CONFIG_HINT_END )
354 p_new->p_callback = p_orig->p_callback;
360 /*****************************************************************************
361 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
362 *****************************************************************************
363 * We simply undo what we did in config_SetCallbacks.
364 *****************************************************************************/
365 void config_UnsetCallbacks( module_config_t *p_new )
367 while( p_new->i_type != MODULE_CONFIG_HINT_END )
369 p_new->p_callback = NULL;
374 /*****************************************************************************
375 * config_LoadConfigFile: loads the configuration file.
376 *****************************************************************************
377 * This function is called to load the config options stored in the config
379 *****************************************************************************/
380 int config_LoadConfigFile( const char *psz_module_name )
383 module_config_t *p_item;
386 char *p_index, *psz_option_name, *psz_option_value;
387 char *psz_filename, *psz_homedir;
389 /* Acquire config file lock */
390 vlc_mutex_lock( &p_main->config_lock );
392 psz_homedir = p_main->psz_homedir;
395 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
396 vlc_mutex_unlock( &p_main->config_lock );
399 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
400 strlen(psz_homedir) + 1 );
403 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
404 vlc_mutex_unlock( &p_main->config_lock );
407 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
409 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
411 file = fopen( psz_filename, "rt" );
414 intf_WarnMsg( 1, "config: config file %s doesn't already exist",
416 free( psz_filename );
417 vlc_mutex_unlock( &p_main->config_lock );
421 /* Look for the selected module, if NULL then save everything */
422 for( p_module = p_module_bank->first ; p_module != NULL ;
423 p_module = p_module->next )
426 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
429 /* The config file is organized in sections, one per module. Look for
430 * the interesting section ( a section is of the form [foo] ) */
432 while( fgets( line, 1024, file ) )
434 if( (line[0] == '[') && (p_index = strchr(line,']')) &&
435 (p_index - &line[1] == strlen(p_module->psz_name) ) &&
436 !memcmp( &line[1], p_module->psz_name,
437 strlen(p_module->psz_name) ) )
439 intf_WarnMsg( 5, "config: loading config for module <%s>",
440 p_module->psz_name );
445 /* either we found the section or we're at the EOF */
447 /* Now try to load the options in this section */
448 while( fgets( line, 1024, file ) )
450 if( line[0] == '[' ) break; /* end of section */
452 /* ignore comments or empty lines */
453 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
456 /* get rid of line feed */
457 if( line[strlen(line)-1] == '\n' )
458 line[strlen(line)-1] = (char)0;
460 /* look for option name */
461 psz_option_name = line;
462 psz_option_value = NULL;
463 p_index = strchr( line, '=' );
464 if( !p_index ) break; /* this ain't an option!!! */
467 psz_option_value = p_index + 1;
469 /* try to match this option with one of the module's options */
470 for( p_item = p_module->p_config;
471 p_item->i_type != MODULE_CONFIG_HINT_END;
474 if( p_item->i_type & MODULE_CONFIG_HINT )
478 if( !strcmp( p_item->psz_name, psz_option_name ) )
481 switch( p_item->i_type )
483 case MODULE_CONFIG_ITEM_BOOL:
484 case MODULE_CONFIG_ITEM_INTEGER:
485 if( !*psz_option_value )
486 break; /* ignore empty option */
487 p_item->i_value = atoi( psz_option_value);
488 intf_WarnMsg( 7, "config: found <%s> option %s=%i",
490 p_item->psz_name, p_item->i_value );
493 case MODULE_CONFIG_ITEM_FLOAT:
494 if( !*psz_option_value )
495 break; /* ignore empty option */
496 p_item->f_value = (float)atof( psz_option_value);
497 intf_WarnMsg( 7, "config: found <%s> option %s=%f",
498 p_module->psz_name, p_item->psz_name,
499 (double)p_item->f_value );
503 vlc_mutex_lock( p_item->p_lock );
505 /* free old string */
506 if( p_item->psz_value )
507 free( p_item->psz_value );
509 p_item->psz_value = *psz_option_value ?
510 strdup( psz_option_value ) : NULL;
512 vlc_mutex_unlock( p_item->p_lock );
514 intf_WarnMsg( 7, "config: found <%s> option %s=%s",
517 p_item->psz_value != NULL ?
518 p_item->psz_value : "(NULL)" );
528 free( psz_filename );
530 vlc_mutex_unlock( &p_main->config_lock );
535 /*****************************************************************************
536 * config_SaveConfigFile: Save a module's config options.
537 *****************************************************************************
538 * This will save the specified module's config options to the config file.
539 * If psz_module_name is NULL then we save all the modules config options.
540 * It's no use to save the config options that kept their default values, so
541 * we'll try to be a bit clever here.
543 * When we save we mustn't delete the config options of the modules that
544 * haven't been loaded. So we cannot just create a new config file with the
545 * config structures we've got in memory.
546 * I don't really know how to deal with this nicely, so I will use a completly
548 * I will load the config file in memory, but skipping all the sections of the
549 * modules we want to save. Then I will create a brand new file, dump the file
550 * loaded in memory and then append the sections of the modules we want to
553 *****************************************************************************/
554 int config_SaveConfigFile( const char *psz_module_name )
557 module_config_t *p_item;
559 char p_line[1024], *p_index2;
561 char *p_bigbuffer, *p_index;
563 char *psz_filename, *psz_homedir;
565 /* Acquire config file lock */
566 vlc_mutex_lock( &p_main->config_lock );
568 psz_homedir = p_main->psz_homedir;
571 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
572 vlc_mutex_unlock( &p_main->config_lock );
575 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
576 strlen(psz_homedir) + 1 );
579 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
580 vlc_mutex_unlock( &p_main->config_lock );
583 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
586 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
588 if( mkdir( psz_filename ) && errno != EEXIST )
591 intf_ErrMsg( "config error: couldn't create %s (%s)",
592 psz_filename, strerror(errno) );
595 strcat( psz_filename, "/" CONFIG_FILE );
598 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
600 file = fopen( psz_filename, "rt" );
603 intf_WarnMsg( 1, "config: config file %s doesn't already exist",
608 /* look for file size */
609 fseek( file, 0, SEEK_END );
610 i_sizebuf = ftell( file );
614 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
617 intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
618 if( file ) fclose( file );
619 free( psz_filename );
620 vlc_mutex_unlock( &p_main->config_lock );
625 /* backup file into memory, we only need to backup the sections we won't
628 while( file && fgets( p_line, 1024, file ) )
630 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
632 /* we found a section, check if we need to do a backup */
633 for( p_module = p_module_bank->first; p_module != NULL;
634 p_module = p_module->next )
636 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
637 !memcmp( &p_line[1], p_module->psz_name,
638 strlen(p_module->psz_name) ) )
640 if( !psz_module_name )
642 else if( !strcmp( psz_module_name, p_module->psz_name ) )
649 /* we don't have this section in our list so we need to back
652 intf_WarnMsg( 5, "config: backing up config for "
653 "unknown module <%s>", &p_line[1] );
664 /* save line if requested and line is valid (doesn't begin with a
665 * space, tab, or eol) */
666 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
667 && (p_line[0] != '\t') )
669 strcpy( p_index, p_line );
670 p_index += strlen( p_line );
673 if( file ) fclose( file );
677 * Save module config in file
680 file = fopen( psz_filename, "wt" );
683 intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
685 free( psz_filename );
686 vlc_mutex_unlock( &p_main->config_lock );
690 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
692 /* Look for the selected module, if NULL then save everything */
693 for( p_module = p_module_bank->first ; p_module != NULL ;
694 p_module = p_module->next )
697 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
700 if( !p_module->i_config_items )
703 intf_WarnMsg( 5, "config: saving config for module <%s>",
704 p_module->psz_name );
706 fprintf( file, "[%s]", p_module->psz_name );
707 if( p_module->psz_longname )
708 fprintf( file, " # %s\n\n", p_module->psz_longname );
710 fprintf( file, "\n\n" );
712 for( p_item = p_module->p_config;
713 p_item->i_type != MODULE_CONFIG_HINT_END;
716 if( p_item->i_type & MODULE_CONFIG_HINT )
720 switch( p_item->i_type )
722 case MODULE_CONFIG_ITEM_BOOL:
723 case MODULE_CONFIG_ITEM_INTEGER:
724 if( p_item->psz_text )
725 fprintf( file, "# %s (%s)\n", p_item->psz_text,
726 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL) ?
727 _("boolean") : _("integer") );
728 fprintf( file, "%s=%i\n", p_item->psz_name,
732 case MODULE_CONFIG_ITEM_FLOAT:
733 if( p_item->psz_text )
734 fprintf( file, "# %s (%s)\n", p_item->psz_text,
736 fprintf( file, "%s=%f\n", p_item->psz_name,
737 (double)p_item->f_value );
741 if( p_item->psz_text )
742 fprintf( file, "# %s (%s)\n", p_item->psz_text,
744 fprintf( file, "%s=%s\n", p_item->psz_name,
745 p_item->psz_value ? p_item->psz_value : "" );
749 fprintf( file, "\n" );
754 * Restore old settings from the config in file
756 fputs( p_bigbuffer, file );
760 free( psz_filename );
761 vlc_mutex_unlock( &p_main->config_lock );
766 /*****************************************************************************
767 * config_LoadCmdLine: parse command line
768 *****************************************************************************
769 * Parse command line for configuration options.
770 * Now that the module_bank has been initialized, we can dynamically
771 * generate the longopts structure used by getops. We have to do it this way
772 * because we don't know (and don't want to know) in advance the configuration
773 * options used (ie. exported) by each module.
774 *****************************************************************************/
775 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
776 boolean_t b_ignore_errors )
778 int i_cmd, i_index, i_opts, i_shortopts;
780 module_config_t *p_item;
781 struct option *p_longopts;
784 module_config_t *pp_shortopts[256];
787 /* Reset warning level */
788 p_main->i_warning_level = 0;
790 /* Set default configuration and copy arguments */
791 p_main->i_argc = *pi_argc;
792 p_main->ppsz_argv = ppsz_argv;
794 p_main->p_channel = NULL;
797 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
798 * is the PSN - process serial number (a unique PID-ish thingie)
799 * still ok for real Darwin & when run from command line */
800 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
801 /* for example -psn_0_9306113 */
803 /* GDMF!... I can't do this or else the MacOSX window server will
804 * not pick up the PSN and not register the app and we crash...
805 * hence the following kludge otherwise we'll get confused w/ argv[1]
806 * being an input file name */
808 ppsz_argv[ 1 ] = NULL;
810 *pi_argc = *pi_argc - 1;
817 * Generate the longopts and shortopts structures used by getopt_long
821 for( p_module = p_module_bank->first;
823 p_module = p_module->next )
825 /* count the number of exported configuration options (to allocate
827 i_opts += p_module->i_config_items;
830 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
831 if( p_longopts == NULL )
833 intf_ErrMsg( "config error: couldn't allocate p_longopts" );
837 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
838 if( psz_shortopts == NULL )
840 intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
845 /* If we are requested to ignore errors, then we must work on a copy
846 * of the ppsz_argv array, otherwise getopt_long will reorder it for
847 * us, ignoring the arity of the options */
848 if( b_ignore_errors )
850 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
851 if( ppsz_argv == NULL )
853 intf_ErrMsg( "config error: couldn't duplicate ppsz_argv" );
854 free( psz_shortopts );
858 memcpy( ppsz_argv, p_main->ppsz_argv, *pi_argc * sizeof(char *) );
861 psz_shortopts[0] = 'v';
863 for( i_index = 0; i_index < 256; i_index++ )
865 pp_shortopts[i_index] = NULL;
868 /* Fill the p_longopts and psz_shortopts structures */
870 for( p_module = p_module_bank->first ;
872 p_module = p_module->next )
874 for( p_item = p_module->p_config;
875 p_item->i_type != MODULE_CONFIG_HINT_END;
879 if( p_item->i_type & MODULE_CONFIG_HINT )
882 /* Add item to long options */
883 p_longopts[i_index].name = p_item->psz_name;
884 p_longopts[i_index].has_arg =
885 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL)?
886 no_argument : required_argument;
887 p_longopts[i_index].flag = 0;
888 p_longopts[i_index].val = 0;
891 /* If item also has a short option, add it */
892 if( p_item->i_short )
894 pp_shortopts[(int)p_item->i_short] = p_item;
895 psz_shortopts[i_shortopts] = p_item->i_short;
897 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
899 psz_shortopts[i_shortopts] = ':';
906 /* Close the longopts and shortopts structures */
907 memset( &p_longopts[i_index], 0, sizeof(struct option) );
908 psz_shortopts[i_shortopts] = '\0';
911 * Parse the command line options
915 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
916 p_longopts, &i_index ) ) != EOF )
918 /* A long option has been recognized */
921 module_config_t *p_conf;
923 /* Store the configuration option */
924 p_conf = config_FindConfig( p_longopts[i_index].name );
926 switch( p_conf->i_type )
928 case MODULE_CONFIG_ITEM_STRING:
929 case MODULE_CONFIG_ITEM_FILE:
930 case MODULE_CONFIG_ITEM_MODULE:
931 config_PutPszVariable( p_longopts[i_index].name, optarg );
933 case MODULE_CONFIG_ITEM_INTEGER:
934 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
936 case MODULE_CONFIG_ITEM_FLOAT:
937 config_PutFloatVariable( p_longopts[i_index].name,
938 (float)atof(optarg) );
940 case MODULE_CONFIG_ITEM_BOOL:
941 config_PutIntVariable( p_longopts[i_index].name, 1 );
948 /* A short option has been recognized */
949 if( pp_shortopts[i_cmd] != NULL )
951 switch( pp_shortopts[i_cmd]->i_type )
953 case MODULE_CONFIG_ITEM_STRING:
954 case MODULE_CONFIG_ITEM_FILE:
955 case MODULE_CONFIG_ITEM_MODULE:
956 config_PutPszVariable( pp_shortopts[i_cmd]->psz_name, optarg );
958 case MODULE_CONFIG_ITEM_INTEGER:
959 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
962 case MODULE_CONFIG_ITEM_BOOL:
963 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
970 /* Either it's a -v or it's an unknown short option */
973 p_main->i_warning_level++;
977 /* Internal error: unknown option */
978 if( !b_ignore_errors )
980 intf_ErrMsg( "config error: unknown option `%s'",
981 ppsz_argv[optind-1] );
982 intf_Msg( "Try `%s --help' for more information.\n",
986 free( psz_shortopts );
987 if( b_ignore_errors ) free( ppsz_argv );
993 free( psz_shortopts );
994 if( b_ignore_errors ) free( ppsz_argv );
996 /* Update the warning level */
997 p_main->i_warning_level += config_GetIntVariable( "warning" );
998 p_main->i_warning_level = ( p_main->i_warning_level < 0 ) ? 0 :
999 p_main->i_warning_level;
1000 config_PutIntVariable( "warning", p_main->i_warning_level );
1005 /*****************************************************************************
1006 * config_GetHomeDir: find the user's home directory.
1007 *****************************************************************************
1008 * This function will try by different ways to find the user's home path.
1009 * Note that this function is not reentrant, it should be called only once
1010 * at the beginning of main where the result will be stored for later use.
1011 *****************************************************************************/
1012 char *config_GetHomeDir( void )
1014 char *p_tmp, *p_homedir = NULL;
1016 #if defined(HAVE_GETPWUID)
1017 struct passwd *p_pw = NULL;
1021 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1023 # define CSIDL_FLAG_CREATE 0x8000
1024 # define CSIDL_APPDATA 0x1A
1025 # define SHGFP_TYPE_CURRENT 0
1027 HINSTANCE shfolder_dll;
1028 SHGETFOLDERPATH SHGetFolderPath ;
1030 /* load the shell32 dll to retreive SHGetFolderPath */
1031 if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1033 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1034 "SHGetFolderPathA" );
1035 if ( SHGetFolderPath != NULL )
1037 p_homedir = (char *)malloc( MAX_PATH );
1040 intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1044 /* get the "Application Data" folder for the current user */
1045 if( S_OK == SHGetFolderPath( NULL,
1046 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1047 NULL, SHGFP_TYPE_CURRENT,
1050 FreeLibrary( shfolder_dll );
1055 FreeLibrary( shfolder_dll );
1059 #if defined(HAVE_GETPWUID)
1060 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1063 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1065 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1067 p_homedir = strdup( "/tmp" );
1069 else p_homedir = strdup( p_tmp );
1071 intf_ErrMsg( "config error: unable to get home directory, "
1072 "using %s instead", p_homedir );
1075 else p_homedir = strdup( p_tmp );
1077 #if defined(HAVE_GETPWUID)
1080 p_homedir = strdup( p_pw->pw_dir );