1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.23 2002/05/15 01:29:07 sam 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: couldn't open config file %s for reading (%s)",
415 psz_filename, strerror(errno) );
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] == (char)0) ) continue;
455 /* get rid of line feed */
456 if( line[strlen(line)-1] == '\n' )
457 line[strlen(line)-1] = (char)0;
459 /* look for option name */
460 psz_option_name = line;
461 psz_option_value = NULL;
462 p_index = strchr( line, '=' );
463 if( !p_index ) break; /* this ain't an option!!! */
466 psz_option_value = p_index + 1;
468 /* try to match this option with one of the module's options */
469 for( p_item = p_module->p_config;
470 p_item->i_type != MODULE_CONFIG_HINT_END;
473 if( p_item->i_type & MODULE_CONFIG_HINT )
477 if( !strcmp( p_item->psz_name, psz_option_name ) )
480 switch( p_item->i_type )
482 case MODULE_CONFIG_ITEM_BOOL:
483 case MODULE_CONFIG_ITEM_INTEGER:
484 if( !*psz_option_value )
485 break; /* ignore empty option */
486 p_item->i_value = atoi( psz_option_value);
487 intf_WarnMsg( 7, "config: found <%s> option %s=%i",
489 p_item->psz_name, p_item->i_value );
492 case MODULE_CONFIG_ITEM_FLOAT:
493 if( !*psz_option_value )
494 break; /* ignore empty option */
495 p_item->f_value = (float)atof( psz_option_value);
496 intf_WarnMsg( 7, "config: found <%s> option %s=%f",
497 p_module->psz_name, p_item->psz_name,
498 (double)p_item->f_value );
502 vlc_mutex_lock( p_item->p_lock );
504 /* free old string */
505 if( p_item->psz_value )
506 free( p_item->psz_value );
508 p_item->psz_value = *psz_option_value ?
509 strdup( psz_option_value ) : NULL;
511 vlc_mutex_unlock( p_item->p_lock );
513 intf_WarnMsg( 7, "config: found <%s> option %s=%s",
516 p_item->psz_value != NULL ?
517 p_item->psz_value : "(NULL)" );
527 free( psz_filename );
529 vlc_mutex_unlock( &p_main->config_lock );
534 /*****************************************************************************
535 * config_SaveConfigFile: Save a module's config options.
536 *****************************************************************************
537 * This will save the specified module's config options to the config file.
538 * If psz_module_name is NULL then we save all the modules config options.
539 * It's no use to save the config options that kept their default values, so
540 * we'll try to be a bit clever here.
542 * When we save we mustn't delete the config options of the modules that
543 * haven't been loaded. So we cannot just create a new config file with the
544 * config structures we've got in memory.
545 * I don't really know how to deal with this nicely, so I will use a completly
547 * I will load the config file in memory, but skipping all the sections of the
548 * modules we want to save. Then I will create a brand new file, dump the file
549 * loaded in memory and then append the sections of the modules we want to
552 *****************************************************************************/
553 int config_SaveConfigFile( const char *psz_module_name )
556 module_config_t *p_item;
558 char p_line[1024], *p_index2;
560 char *p_bigbuffer, *p_index;
562 char *psz_filename, *psz_homedir;
564 /* Acquire config file lock */
565 vlc_mutex_lock( &p_main->config_lock );
567 psz_homedir = p_main->psz_homedir;
570 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
571 vlc_mutex_unlock( &p_main->config_lock );
574 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
575 strlen(psz_homedir) + 1 );
578 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
579 vlc_mutex_unlock( &p_main->config_lock );
582 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
585 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
587 if( mkdir( psz_filename ) && errno != EEXIST )
590 intf_ErrMsg( "config error: couldn't create %s (%s)",
591 psz_filename, strerror(errno) );
594 strcat( psz_filename, "/" CONFIG_FILE );
597 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
599 file = fopen( psz_filename, "rt" );
602 intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
603 psz_filename, strerror(errno) );
607 /* look for file size */
608 fseek( file, 0, SEEK_END );
609 i_sizebuf = ftell( file );
613 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
616 intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
617 if( file ) fclose( file );
618 free( psz_filename );
619 vlc_mutex_unlock( &p_main->config_lock );
624 /* backup file into memory, we only need to backup the sections we won't
627 while( file && fgets( p_line, 1024, file ) )
629 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
631 /* we found a section, check if we need to do a backup */
632 for( p_module = p_module_bank->first; p_module != NULL;
633 p_module = p_module->next )
635 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
636 !memcmp( &p_line[1], p_module->psz_name,
637 strlen(p_module->psz_name) ) )
639 if( !psz_module_name )
641 else if( !strcmp( psz_module_name, p_module->psz_name ) )
648 /* we don't have this section in our list so we need to back
651 intf_WarnMsg( 5, "config: backing up config for "
652 "unknown module <%s>", &p_line[1] );
663 /* save line if requested and line is valid (doesn't begin with a
664 * space, tab, or eol) */
665 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
666 && (p_line[0] != '\t') )
668 strcpy( p_index, p_line );
669 p_index += strlen( p_line );
672 if( file ) fclose( file );
676 * Save module config in file
679 file = fopen( psz_filename, "wt" );
682 intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
684 free( psz_filename );
685 vlc_mutex_unlock( &p_main->config_lock );
689 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
691 /* Look for the selected module, if NULL then save everything */
692 for( p_module = p_module_bank->first ; p_module != NULL ;
693 p_module = p_module->next )
696 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
699 if( !p_module->i_config_items )
702 intf_WarnMsg( 5, "config: saving config for module <%s>",
703 p_module->psz_name );
705 fprintf( file, "[%s]\n", p_module->psz_name );
707 if( p_module->psz_longname )
708 fprintf( file, "###\n### %s\n###\n", p_module->psz_longname );
710 for( p_item = p_module->p_config;
711 p_item->i_type != MODULE_CONFIG_HINT_END;
714 if( p_item->i_type & MODULE_CONFIG_HINT )
718 switch( p_item->i_type )
720 case MODULE_CONFIG_ITEM_BOOL:
721 case MODULE_CONFIG_ITEM_INTEGER:
722 if( p_item->psz_text )
723 fprintf( file, "# %s (%s)\n", p_item->psz_text,
724 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL) ?
725 _("boolean") : _("integer") );
726 fprintf( file, "%s=%i\n", p_item->psz_name,
730 case MODULE_CONFIG_ITEM_FLOAT:
731 if( p_item->psz_text )
732 fprintf( file, "# %s (%s)\n", p_item->psz_text,
734 fprintf( file, "%s=%f\n", p_item->psz_name,
735 (double)p_item->f_value );
739 if( p_item->psz_text )
740 fprintf( file, "# %s (%s)\n", p_item->psz_text,
742 fprintf( file, "%s=%s\n", p_item->psz_name,
743 p_item->psz_value ? p_item->psz_value : "" );
747 fprintf( file, "\n" );
752 * Restore old settings from the config in file
754 fputs( p_bigbuffer, file );
758 free( psz_filename );
759 vlc_mutex_unlock( &p_main->config_lock );
764 /*****************************************************************************
765 * config_LoadCmdLine: parse command line
766 *****************************************************************************
767 * Parse command line for configuration options.
768 * Now that the module_bank has been initialized, we can dynamically
769 * generate the longopts structure used by getops. We have to do it this way
770 * because we don't know (and don't want to know) in advance the configuration
771 * options used (ie. exported) by each module.
772 *****************************************************************************/
773 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
774 boolean_t b_ignore_errors )
776 int i_cmd, i_index, i_opts, i_shortopts;
778 module_config_t *p_item;
779 struct option *p_longopts;
782 module_config_t *pp_shortopts[256];
785 /* Reset warning level */
786 p_main->i_warning_level = 0;
788 /* Set default configuration and copy arguments */
789 p_main->i_argc = *pi_argc;
790 p_main->ppsz_argv = ppsz_argv;
792 p_main->p_channel = NULL;
795 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
796 * is the PSN - process serial number (a unique PID-ish thingie)
797 * still ok for real Darwin & when run from command line */
798 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
799 /* for example -psn_0_9306113 */
801 /* GDMF!... I can't do this or else the MacOSX window server will
802 * not pick up the PSN and not register the app and we crash...
803 * hence the following kludge otherwise we'll get confused w/ argv[1]
804 * being an input file name */
806 ppsz_argv[ 1 ] = NULL;
808 *pi_argc = *pi_argc - 1;
815 * Generate the longopts and shortopts structures used by getopt_long
819 for( p_module = p_module_bank->first;
821 p_module = p_module->next )
823 /* count the number of exported configuration options (to allocate
825 i_opts += p_module->i_config_items;
828 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
829 if( p_longopts == NULL )
831 intf_ErrMsg( "config error: couldn't allocate p_longopts" );
835 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
836 if( psz_shortopts == NULL )
838 intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
843 /* If we are requested to ignore errors, then we must work on a copy
844 * of the ppsz_argv array, otherwise getopt_long will reorder it for
845 * us, ignoring the arity of the options */
846 if( b_ignore_errors )
848 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
849 if( ppsz_argv == NULL )
851 intf_ErrMsg( "config error: couldn't duplicate ppsz_argv" );
852 free( psz_shortopts );
856 memcpy( ppsz_argv, p_main->ppsz_argv, *pi_argc * sizeof(char *) );
859 psz_shortopts[0] = 'v';
861 for( i_index = 0; i_index < 256; i_index++ )
863 pp_shortopts[i_index] = NULL;
866 /* Fill the p_longopts and psz_shortopts structures */
868 for( p_module = p_module_bank->first ;
870 p_module = p_module->next )
872 for( p_item = p_module->p_config;
873 p_item->i_type != MODULE_CONFIG_HINT_END;
877 if( p_item->i_type & MODULE_CONFIG_HINT )
880 /* Add item to long options */
881 p_longopts[i_index].name = p_item->psz_name;
882 p_longopts[i_index].has_arg =
883 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL)?
884 no_argument : required_argument;
885 p_longopts[i_index].flag = 0;
886 p_longopts[i_index].val = 0;
889 /* If item also has a short option, add it */
890 if( p_item->i_short )
892 pp_shortopts[(int)p_item->i_short] = p_item;
893 psz_shortopts[i_shortopts] = p_item->i_short;
895 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
897 psz_shortopts[i_shortopts] = ':';
904 /* Close the longopts and shortopts structures */
905 memset( &p_longopts[i_index], 0, sizeof(struct option) );
906 psz_shortopts[i_shortopts] = '\0';
909 * Parse the command line options
913 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
914 p_longopts, &i_index ) ) != EOF )
916 /* A long option has been recognized */
919 module_config_t *p_conf;
921 /* Store the configuration option */
922 p_conf = config_FindConfig( p_longopts[i_index].name );
924 switch( p_conf->i_type )
926 case MODULE_CONFIG_ITEM_STRING:
927 case MODULE_CONFIG_ITEM_FILE:
928 case MODULE_CONFIG_ITEM_MODULE:
929 config_PutPszVariable( p_longopts[i_index].name, optarg );
931 case MODULE_CONFIG_ITEM_INTEGER:
932 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
934 case MODULE_CONFIG_ITEM_FLOAT:
935 config_PutFloatVariable( p_longopts[i_index].name,
936 (float)atof(optarg) );
938 case MODULE_CONFIG_ITEM_BOOL:
939 config_PutIntVariable( p_longopts[i_index].name, 1 );
946 /* A short option has been recognized */
947 if( pp_shortopts[i_cmd] != NULL )
949 switch( pp_shortopts[i_cmd]->i_type )
951 case MODULE_CONFIG_ITEM_STRING:
952 case MODULE_CONFIG_ITEM_FILE:
953 case MODULE_CONFIG_ITEM_MODULE:
954 config_PutPszVariable( pp_shortopts[i_cmd]->psz_name, optarg );
956 case MODULE_CONFIG_ITEM_INTEGER:
957 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
960 case MODULE_CONFIG_ITEM_BOOL:
961 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
968 /* Either it's a -v or it's an unknown short option */
971 p_main->i_warning_level++;
975 /* Internal error: unknown option */
976 if( !b_ignore_errors )
978 intf_ErrMsg( "config error: unknown option `%s'",
979 ppsz_argv[optind-1] );
980 intf_Msg( "Try `%s --help' for more information.\n",
984 free( psz_shortopts );
985 if( b_ignore_errors ) free( ppsz_argv );
991 free( psz_shortopts );
992 if( b_ignore_errors ) free( ppsz_argv );
994 /* Update the warning level */
995 p_main->i_warning_level += config_GetIntVariable( "warning" );
996 p_main->i_warning_level = ( p_main->i_warning_level < 0 ) ? 0 :
997 p_main->i_warning_level;
998 config_PutIntVariable( "warning", p_main->i_warning_level );
1003 /*****************************************************************************
1004 * config_GetHomeDir: find the user's home directory.
1005 *****************************************************************************
1006 * This function will try by different ways to find the user's home path.
1007 * Note that this function is not reentrant, it should be called only once
1008 * at the beginning of main where the result will be stored for later use.
1009 *****************************************************************************/
1010 char *config_GetHomeDir( void )
1012 char *p_tmp, *p_homedir = NULL;
1014 #if defined(HAVE_GETPWUID)
1015 struct passwd *p_pw = NULL;
1019 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1021 # define CSIDL_FLAG_CREATE 0x8000
1022 # define CSIDL_APPDATA 0x1A
1023 # define SHGFP_TYPE_CURRENT 0
1025 HINSTANCE shell32_dll;
1026 SHGETFOLDERPATH SHGetFolderPath ;
1028 /* load the shell32 dll to retreive SHGetFolderPath */
1029 if( ( shell32_dll = LoadLibrary("shell32.dll") ) != NULL )
1031 SHGetFolderPath = (void *)GetProcAddress( shell32_dll,
1032 "SHGetFolderPathA" );
1033 if ( SHGetFolderPath != NULL )
1035 p_homedir = (char *)malloc( MAX_PATH );
1038 intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1042 /* get the "Application Data" folder for the current user */
1043 if( S_OK == SHGetFolderPath( NULL,
1044 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1045 NULL, SHGFP_TYPE_CURRENT,
1048 FreeLibrary( shell32_dll );
1053 FreeLibrary( shell32_dll );
1057 #if defined(HAVE_GETPWUID)
1058 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1061 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1063 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1065 p_homedir = strdup( "/tmp" );
1067 else p_homedir = strdup( p_tmp );
1069 intf_ErrMsg( "config error: unable to get home directory, "
1070 "using %s instead", p_homedir );
1073 else p_homedir = strdup( p_tmp );
1075 #if defined(HAVE_GETPWUID)
1078 p_homedir = strdup( p_pw->pw_dir );