1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.19 2002/04/23 14:16:21 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 );
189 /*****************************************************************************
190 * config_PutIntVariable: set the integer value of an int variable
191 *****************************************************************************
192 * This function is used to set the value of variables which are internally
193 * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
194 * MODULE_CONFIG_ITEM_BOOL).
195 *****************************************************************************/
196 void config_PutIntVariable( const char *psz_name, int i_value )
198 module_config_t *p_config;
200 p_config = config_FindConfig( psz_name );
205 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
208 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
209 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
211 intf_ErrMsg( "config error: option %s doesn't refer to an int",
216 p_config->i_value = i_value;
219 /*****************************************************************************
220 * config_PutFloatVariable: set the value of a float variable
221 *****************************************************************************
222 * This function is used to set the value of variables which are internally
223 * represented by a float (MODULE_CONFIG_ITEM_FLOAT).
224 *****************************************************************************/
225 void config_PutFloatVariable( const char *psz_name, float f_value )
227 module_config_t *p_config;
229 p_config = config_FindConfig( psz_name );
234 intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
237 if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
239 intf_ErrMsg( "config error: option %s doesn't refer to a float",
244 p_config->f_value = f_value;
247 /*****************************************************************************
248 * config_FindConfig: find the config structure associated with an option.
249 *****************************************************************************
250 * FIXME: This function really needs to be optimized.
251 *****************************************************************************/
252 module_config_t *config_FindConfig( const char *psz_name )
255 module_config_t *p_item;
257 if( !psz_name ) return NULL;
259 for( p_module = p_module_bank->first ;
261 p_module = p_module->next )
263 for( p_item = p_module->p_config;
264 p_item->i_type != MODULE_CONFIG_HINT_END;
267 if( p_item->i_type & MODULE_CONFIG_HINT )
270 if( !strcmp( psz_name, p_item->psz_name ) )
278 /*****************************************************************************
279 * config_Duplicate: creates a duplicate of a module's configuration data.
280 *****************************************************************************
281 * Unfortunatly we cannot work directly with the module's config data as
282 * this module might be unloaded from memory at any time (remember HideModule).
283 * This is why we need to create an exact copy of the config data.
284 *****************************************************************************/
285 module_config_t *config_Duplicate( module_config_t *p_orig )
288 module_config_t *p_config;
290 /* Calculate the structure length */
291 for( p_config = p_orig, i_lines = 1;
292 p_config->i_type != MODULE_CONFIG_HINT_END;
293 p_config++, i_lines++ );
295 /* Allocate memory */
296 p_config = (module_config_t *)malloc( sizeof(module_config_t) * i_lines );
297 if( p_config == NULL )
299 intf_ErrMsg( "config error: can't duplicate p_config" );
303 /* Do the duplication job */
304 for( i = 0; i < i_lines ; i++ )
306 p_config[i].i_type = p_orig[i].i_type;
307 p_config[i].i_short = p_orig[i].i_short;
308 p_config[i].i_value = p_orig[i].i_value;
309 p_config[i].f_value = p_orig[i].f_value;
310 p_config[i].b_dirty = p_orig[i].b_dirty;
312 p_config[i].psz_name = p_orig[i].psz_name ?
313 strdup( _(p_orig[i].psz_name) ) : NULL;
314 p_config[i].psz_text = p_orig[i].psz_text ?
315 strdup( _(p_orig[i].psz_text) ) : NULL;
316 p_config[i].psz_longtext = p_orig[i].psz_longtext ?
317 strdup( _(p_orig[i].psz_longtext) ) : NULL;
318 p_config[i].psz_value = p_orig[i].psz_value ?
319 strdup( _(p_orig[i].psz_value) ) : NULL;
321 /* the callback pointer is only valid when the module is loaded so this
322 * value is set in ActivateModule() and reset in DeactivateModule() */
323 p_config[i].p_callback = NULL;
329 /*****************************************************************************
330 * config_LoadConfigFile: loads the configuration file.
331 *****************************************************************************
332 * This function is called to load the config options stored in the config
334 *****************************************************************************/
335 int config_LoadConfigFile( const char *psz_module_name )
338 module_config_t *p_item;
341 char *p_index, *psz_option_name, *psz_option_value;
342 char *psz_filename, *psz_homedir;
344 /* Acquire config file lock */
345 vlc_mutex_lock( &p_main->config_lock );
347 psz_homedir = p_main->psz_homedir;
350 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
351 vlc_mutex_unlock( &p_main->config_lock );
354 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
355 strlen(psz_homedir) + 1 );
358 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
359 vlc_mutex_unlock( &p_main->config_lock );
362 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
364 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
366 file = fopen( psz_filename, "r" );
369 intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
370 psz_filename, strerror(errno) );
371 free( psz_filename );
372 vlc_mutex_unlock( &p_main->config_lock );
376 /* Look for the selected module, if NULL then save everything */
377 for( p_module = p_module_bank->first ; p_module != NULL ;
378 p_module = p_module->next )
381 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
384 /* The config file is organized in sections, one per module. Look for
385 * the interesting section ( a section is of the form [foo] ) */
387 while( fgets( line, 1024, file ) )
389 if( (line[0] == '[') && (p_index = strchr(line,']')) &&
390 (p_index - &line[1] == strlen(p_module->psz_name) ) &&
391 !memcmp( &line[1], p_module->psz_name,
392 strlen(p_module->psz_name) ) )
394 intf_WarnMsg( 5, "config: loading config for module <%s>",
395 p_module->psz_name );
400 /* either we found the section or we're at the EOF */
402 /* Now try to load the options in this section */
403 while( fgets( line, 1024, file ) )
405 if( line[0] == '[' ) break; /* end of section */
407 /* ignore comments or empty lines */
408 if( (line[0] == '#') || (line[0] == (char)0) ) continue;
410 /* get rid of line feed */
411 if( line[strlen(line)-1] == '\n' )
412 line[strlen(line)-1] = (char)0;
414 /* look for option name */
415 psz_option_name = line;
416 psz_option_value = NULL;
417 p_index = strchr( line, '=' );
418 if( !p_index ) break; /* this ain't an option!!! */
421 psz_option_value = p_index + 1;
423 /* try to match this option with one of the module's options */
424 for( p_item = p_module->p_config;
425 p_item->i_type != MODULE_CONFIG_HINT_END;
428 if( p_item->i_type & MODULE_CONFIG_HINT )
432 if( !strcmp( p_item->psz_name, psz_option_name ) )
435 switch( p_item->i_type )
437 case MODULE_CONFIG_ITEM_BOOL:
438 case MODULE_CONFIG_ITEM_INTEGER:
439 if( !*psz_option_value )
440 break; /* ignore empty option */
441 p_item->i_value = atoi( psz_option_value);
442 intf_WarnMsg( 7, "config: found <%s> option %s=%i",
444 p_item->psz_name, p_item->i_value );
447 case MODULE_CONFIG_ITEM_FLOAT:
448 if( !*psz_option_value )
449 break; /* ignore empty option */
450 p_item->f_value = (float)atof( psz_option_value);
451 intf_WarnMsg( 7, "config: found <%s> option %s=%f",
452 p_module->psz_name, p_item->psz_name,
453 (double)p_item->f_value );
457 vlc_mutex_lock( p_item->p_lock );
459 /* free old string */
460 if( p_item->psz_value )
461 free( p_item->psz_value );
463 p_item->psz_value = *psz_option_value ?
464 strdup( psz_option_value ) : NULL;
466 vlc_mutex_unlock( p_item->p_lock );
468 intf_WarnMsg( 7, "config: found <%s> option %s=%s",
471 p_item->psz_value != NULL ?
472 p_item->psz_value : "(NULL)" );
482 free( psz_filename );
484 vlc_mutex_unlock( &p_main->config_lock );
489 /*****************************************************************************
490 * config_SaveConfigFile: Save a module's config options.
491 *****************************************************************************
492 * This will save the specified module's config options to the config file.
493 * If psz_module_name is NULL then we save all the modules config options.
494 * It's no use to save the config options that kept their default values, so
495 * we'll try to be a bit clever here.
497 * When we save we mustn't delete the config options of the modules that
498 * haven't been loaded. So we cannot just create a new config file with the
499 * config structures we've got in memory.
500 * I don't really know how to deal with this nicely, so I will use a completly
502 * I will load the config file in memory, but skipping all the sections of the
503 * modules we want to save. Then I will create a brand new file, dump the file
504 * loaded in memory and then append the sections of the modules we want to
507 *****************************************************************************/
508 int config_SaveConfigFile( const char *psz_module_name )
511 module_config_t *p_item;
513 char p_line[1024], *p_index2;
515 char *p_bigbuffer, *p_index;
517 char *psz_filename, *psz_homedir;
519 /* Acquire config file lock */
520 vlc_mutex_lock( &p_main->config_lock );
522 psz_homedir = p_main->psz_homedir;
525 intf_ErrMsg( "config error: p_main->psz_homedir is null" );
526 vlc_mutex_unlock( &p_main->config_lock );
529 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
530 strlen(psz_homedir) + 1 );
533 intf_ErrMsg( "config error: couldn't malloc psz_filename" );
534 vlc_mutex_unlock( &p_main->config_lock );
537 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
540 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
542 if( mkdir( psz_filename ) && errno != EEXIST )
545 intf_ErrMsg( "config error: couldn't create %s (%s)",
546 psz_filename, strerror(errno) );
549 strcat( psz_filename, "/" CONFIG_FILE );
552 intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
554 file = fopen( psz_filename, "r" );
557 intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
558 psz_filename, strerror(errno) );
562 /* look for file size */
563 fseek( file, 0, SEEK_END );
564 i_sizebuf = ftell( file );
568 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
571 intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
572 if( file ) fclose( file );
573 free( psz_filename );
574 vlc_mutex_unlock( &p_main->config_lock );
579 /* backup file into memory, we only need to backup the sections we won't
582 while( file && fgets( p_line, 1024, file ) )
584 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
586 /* we found a section, check if we need to do a backup */
587 for( p_module = p_module_bank->first; p_module != NULL;
588 p_module = p_module->next )
590 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
591 !memcmp( &p_line[1], p_module->psz_name,
592 strlen(p_module->psz_name) ) )
594 if( !psz_module_name )
596 else if( !strcmp( psz_module_name, p_module->psz_name ) )
603 /* we don't have this section in our list so we need to back
606 intf_WarnMsg( 5, "config: backing up config for "
607 "unknown module <%s>", &p_line[1] );
618 /* save line if requested and line is valid (doesn't begin with a
619 * space, tab, or eol) */
620 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
621 && (p_line[0] != '\t') )
623 strcpy( p_index, p_line );
624 p_index += strlen( p_line );
627 if( file ) fclose( file );
631 * Save module config in file
634 file = fopen( psz_filename, "w" );
637 intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
639 free( psz_filename );
640 vlc_mutex_unlock( &p_main->config_lock );
644 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
646 /* Look for the selected module, if NULL then save everything */
647 for( p_module = p_module_bank->first ; p_module != NULL ;
648 p_module = p_module->next )
651 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
654 if( !p_module->i_config_items )
657 intf_WarnMsg( 5, "config: saving config for module <%s>",
658 p_module->psz_name );
660 fprintf( file, "[%s]\n", p_module->psz_name );
662 if( p_module->psz_longname )
663 fprintf( file, "###\n### %s\n###\n", p_module->psz_longname );
665 for( p_item = p_module->p_config;
666 p_item->i_type != MODULE_CONFIG_HINT_END;
669 if( p_item->i_type & MODULE_CONFIG_HINT )
673 switch( p_item->i_type )
675 case MODULE_CONFIG_ITEM_BOOL:
676 case MODULE_CONFIG_ITEM_INTEGER:
677 if( p_item->psz_text )
678 fprintf( file, "# %s (%s)\n", p_item->psz_text,
679 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL) ?
680 _("boolean") : _("integer") );
681 fprintf( file, "%s=%i\n", p_item->psz_name,
685 case MODULE_CONFIG_ITEM_FLOAT:
686 if( p_item->psz_text )
687 fprintf( file, "# %s (%s)\n", p_item->psz_text,
689 fprintf( file, "%s=%f\n", p_item->psz_name,
690 (double)p_item->f_value );
694 if( p_item->psz_text )
695 fprintf( file, "# %s (%s)\n", p_item->psz_text,
697 fprintf( file, "%s=%s\n", p_item->psz_name,
698 p_item->psz_value ? p_item->psz_value : "" );
702 fprintf( file, "\n" );
707 * Restore old settings from the config in file
709 fputs( p_bigbuffer, file );
713 free( psz_filename );
714 vlc_mutex_unlock( &p_main->config_lock );
719 /*****************************************************************************
720 * config_LoadCmdLine: parse command line
721 *****************************************************************************
722 * Parse command line for configuration options.
723 * Now that the module_bank has been initialized, we can dynamically
724 * generate the longopts structure used by getops. We have to do it this way
725 * because we don't know (and don't want to know) in advance the configuration
726 * options used (ie. exported) by each module.
727 *****************************************************************************/
728 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
729 boolean_t b_ignore_errors )
731 int i_cmd, i_index, i_opts, i_shortopts;
733 module_config_t *p_item;
734 struct option *p_longopts;
737 module_config_t *pp_shortopts[256];
740 /* reset warning level */
741 p_main->i_warning_level = 0;
743 /* Set default configuration and copy arguments */
744 p_main->i_argc = *pi_argc;
745 p_main->ppsz_argv = ppsz_argv;
747 p_main->p_channel = NULL;
750 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
751 * is the PSN - process serial number (a unique PID-ish thingie)
752 * still ok for real Darwin & when run from command line */
753 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
754 /* for example -psn_0_9306113 */
756 /* GDMF!... I can't do this or else the MacOSX window server will
757 * not pick up the PSN and not register the app and we crash...
758 * hence the following kludge otherwise we'll get confused w/ argv[1]
759 * being an input file name */
761 ppsz_argv[ 1 ] = NULL;
763 *pi_argc = *pi_argc - 1;
770 * Generate the longopts and shortopts structure used by getopt_long
774 for( p_module = p_module_bank->first;
776 p_module = p_module->next )
778 /* count the number of exported configuration options (to allocate
780 i_opts += p_module->i_config_items;
783 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
784 if( p_longopts == NULL )
786 intf_ErrMsg( "config error: couldn't allocate p_longopts" );
790 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
791 if( psz_shortopts == NULL )
793 intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
798 psz_shortopts[0] = 'v';
800 for( i_index = 0; i_index < 256; i_index++ )
802 pp_shortopts[i_index] = NULL;
805 /* Fill the longopts structure */
807 for( p_module = p_module_bank->first ;
809 p_module = p_module->next )
811 for( p_item = p_module->p_config;
812 p_item->i_type != MODULE_CONFIG_HINT_END;
815 if( p_item->i_type & MODULE_CONFIG_HINT )
818 p_longopts[i_index].name = p_item->psz_name;
819 p_longopts[i_index].has_arg =
820 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL)?
821 no_argument : required_argument;
822 p_longopts[i_index].flag = 0;
823 p_longopts[i_index].val = 0;
824 if( p_item->i_short )
826 pp_shortopts[(int)p_item->i_short] = p_item;
827 psz_shortopts[i_shortopts] = p_item->i_short;
829 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
831 psz_shortopts[i_shortopts] = ':';
839 /* Close the longopts and shortopts structures */
840 memset( &p_longopts[i_index], 0, sizeof(struct option) );
841 psz_shortopts[i_shortopts] = '\0';
844 * Parse the command line options
848 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
849 p_longopts, &i_index ) ) != EOF )
851 /* A long option has been recognized */
854 module_config_t *p_conf;
856 /* Store the configuration option */
857 p_conf = config_FindConfig( p_longopts[i_index].name );
859 switch( p_conf->i_type )
861 case MODULE_CONFIG_ITEM_STRING:
862 case MODULE_CONFIG_ITEM_FILE:
863 case MODULE_CONFIG_ITEM_MODULE:
864 config_PutPszVariable( p_longopts[i_index].name, optarg );
866 case MODULE_CONFIG_ITEM_INTEGER:
867 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
869 case MODULE_CONFIG_ITEM_FLOAT:
870 config_PutFloatVariable( p_longopts[i_index].name,
871 (float)atof(optarg) );
873 case MODULE_CONFIG_ITEM_BOOL:
874 config_PutIntVariable( p_longopts[i_index].name, 1 );
881 /* A short option has been recognized */
882 if( pp_shortopts[i_cmd] != NULL )
884 switch( pp_shortopts[i_cmd]->i_type )
886 case MODULE_CONFIG_ITEM_STRING:
887 case MODULE_CONFIG_ITEM_FILE:
888 case MODULE_CONFIG_ITEM_MODULE:
889 config_PutPszVariable( pp_shortopts[i_cmd]->psz_name, optarg );
891 case MODULE_CONFIG_ITEM_INTEGER:
892 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
895 case MODULE_CONFIG_ITEM_BOOL:
896 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
903 /* Either it's a -v or it's an unknown short option */
906 p_main->i_warning_level++;
910 /* Internal error: unknown option */
911 if( !b_ignore_errors )
913 intf_ErrMsg( "config error: unknown option `%s'",
914 ppsz_argv[optind-1] );
915 intf_Msg( "Try `%s --help' for more information.\n",
919 free( psz_shortopts );
925 free( psz_shortopts );
927 /* Update the warning level */
928 p_main->i_warning_level += config_GetIntVariable( "warning" );
929 p_main->i_warning_level = ( p_main->i_warning_level < 0 ) ? 0 :
930 p_main->i_warning_level;
931 config_PutIntVariable( "warning", p_main->i_warning_level );
936 /*****************************************************************************
937 * config_GetHomeDir: find the user's home directory.
938 *****************************************************************************
939 * This function will try by different ways to find the user's home path.
940 * Note that this function is not reentrant, it should be called only once
941 * at the beginning of main where the result will be stored for later use.
942 *****************************************************************************/
943 char *config_GetHomeDir( void )
945 char *p_tmp, *p_homedir = NULL;
947 #if defined(HAVE_GETPWUID)
948 struct passwd *p_pw = NULL;
951 #if defined(HAVE_GETPWUID)
952 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
955 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
957 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
959 p_homedir = strdup( "/tmp" );
961 else p_homedir = strdup( p_tmp );
963 intf_ErrMsg( "config error: unable to get home directory, "
964 "using %s instead", p_homedir );
967 else p_homedir = strdup( p_tmp );
969 #if defined(HAVE_GETPWUID)
972 p_homedir = strdup( p_pw->pw_dir );