1 /*****************************************************************************
2 * configuration.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: configuration.c,v 1.40 2002/10/03 18:56:09 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 *****************************************************************************/
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 "extras/GNUgetopt/getopt.h"
43 #if defined(HAVE_GETPWUID)
44 #include <pwd.h> /* getpwuid() */
48 #include <sys/types.h>
50 /*****************************************************************************
51 * config_GetInt: 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 (CONFIG_ITEM_INTEGER and
56 *****************************************************************************/
57 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
59 module_config_t *p_config;
61 p_config = config_FindConfig( p_this, psz_name );
66 msg_Err( p_this, "option %s does not exist", psz_name );
69 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
70 (p_config->i_type!=CONFIG_ITEM_BOOL) )
72 msg_Err( p_this, "option %s does not refer to an int", psz_name );
76 return p_config->i_value;
79 /*****************************************************************************
80 * config_GetFloat: get the value of a float variable
81 *****************************************************************************
82 * This function is used to get the value of variables which are internally
83 * represented by a float (CONFIG_ITEM_FLOAT).
84 *****************************************************************************/
85 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
87 module_config_t *p_config;
89 p_config = config_FindConfig( p_this, psz_name );
94 msg_Err( p_this, "option %s does not exist", psz_name );
97 if( p_config->i_type != CONFIG_ITEM_FLOAT )
99 msg_Err( p_this, "option %s does not refer to a float", psz_name );
103 return p_config->f_value;
106 /*****************************************************************************
107 * config_GetPsz: get the string value of a string variable
108 *****************************************************************************
109 * This function is used to get the value of variables which are internally
110 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
111 * and CONFIG_ITEM_MODULE).
113 * Important note: remember to free() the returned char* because it's a
114 * duplicate of the actual value. It isn't safe to return a pointer to the
115 * actual value as it can be modified at any time.
116 *****************************************************************************/
117 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
119 module_config_t *p_config;
120 char *psz_value = NULL;
122 p_config = config_FindConfig( p_this, psz_name );
127 msg_Err( p_this, "option %s does not exist", psz_name );
130 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
131 (p_config->i_type!=CONFIG_ITEM_FILE) &&
132 (p_config->i_type!=CONFIG_ITEM_MODULE) )
134 msg_Err( p_this, "option %s does not refer to a string", psz_name );
138 /* return a copy of the string */
139 vlc_mutex_lock( p_config->p_lock );
140 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
141 vlc_mutex_unlock( p_config->p_lock );
146 /*****************************************************************************
147 * config_PutPsz: set the string value of a string variable
148 *****************************************************************************
149 * This function is used to set the value of variables which are internally
150 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
151 * and CONFIG_ITEM_MODULE).
152 *****************************************************************************/
153 void __config_PutPsz( vlc_object_t *p_this,
154 const char *psz_name, const char *psz_value )
156 module_config_t *p_config;
158 p_config = config_FindConfig( p_this, psz_name );
163 msg_Err( p_this, "option %s does not exist", psz_name );
166 if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
167 (p_config->i_type!=CONFIG_ITEM_FILE) &&
168 (p_config->i_type!=CONFIG_ITEM_MODULE) )
170 msg_Err( p_this, "option %s does not refer to a string", psz_name );
174 vlc_mutex_lock( p_config->p_lock );
176 /* free old string */
177 if( p_config->psz_value ) free( p_config->psz_value );
179 if( psz_value ) p_config->psz_value = strdup( psz_value );
180 else p_config->psz_value = NULL;
182 vlc_mutex_unlock( p_config->p_lock );
184 if( p_config->pf_callback )
186 p_config->pf_callback( p_this );
190 /*****************************************************************************
191 * config_PutInt: set the integer value of an int variable
192 *****************************************************************************
193 * This function is used to set the value of variables which are internally
194 * represented by an integer (CONFIG_ITEM_INTEGER and
196 *****************************************************************************/
197 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
199 module_config_t *p_config;
201 p_config = config_FindConfig( p_this, psz_name );
206 msg_Err( p_this, "option %s does not exist", psz_name );
209 if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
210 (p_config->i_type!=CONFIG_ITEM_BOOL) )
212 msg_Err( p_this, "option %s does not refer to an int", psz_name );
216 p_config->i_value = i_value;
218 if( p_config->pf_callback )
220 p_config->pf_callback( p_this );
224 /*****************************************************************************
225 * config_PutFloat: set the value of a float variable
226 *****************************************************************************
227 * This function is used to set the value of variables which are internally
228 * represented by a float (CONFIG_ITEM_FLOAT).
229 *****************************************************************************/
230 void __config_PutFloat( vlc_object_t *p_this,
231 const char *psz_name, float f_value )
233 module_config_t *p_config;
235 p_config = config_FindConfig( p_this, psz_name );
240 msg_Err( p_this, "option %s does not exist", psz_name );
243 if( p_config->i_type != CONFIG_ITEM_FLOAT )
245 msg_Err( p_this, "option %s does not refer to a float", psz_name );
249 p_config->f_value = f_value;
251 if( p_config->pf_callback )
253 p_config->pf_callback( p_this );
257 /*****************************************************************************
258 * config_FindConfig: find the config structure associated with an option.
259 *****************************************************************************
260 * FIXME: This function really needs to be optimized.
261 * FIXME: And now even more.
262 *****************************************************************************/
263 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
266 module_t **pp_parser;
267 module_config_t *p_item;
269 if( !psz_name ) return NULL;
271 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
273 for( pp_parser = (module_t **)p_list->pp_objects ;
277 if( !(*pp_parser)->i_config_items )
280 for( p_item = (*pp_parser)->p_config;
281 p_item->i_type != CONFIG_HINT_END;
284 if( p_item->i_type & CONFIG_HINT )
287 if( !strcmp( psz_name, p_item->psz_name ) )
289 vlc_list_release( p_list );
295 vlc_list_release( p_list );
300 /*****************************************************************************
301 * config_Duplicate: creates a duplicate of a module's configuration data.
302 *****************************************************************************
303 * Unfortunatly we cannot work directly with the module's config data as
304 * this module might be unloaded from memory at any time (remember HideModule).
305 * This is why we need to create an exact copy of the config data.
306 *****************************************************************************/
307 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
309 int i, j, i_lines = 1;
310 module_config_t *p_item;
312 /* Calculate the structure length */
313 p_module->i_config_items = 0;
314 p_module->i_bool_items = 0;
316 for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
320 if( p_item->i_type & CONFIG_ITEM )
322 p_module->i_config_items++;
325 if( p_item->i_type == CONFIG_ITEM_BOOL )
327 p_module->i_bool_items++;
331 /* Allocate memory */
332 p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
334 if( p_module->p_config == NULL )
336 msg_Err( p_module, "config error: can't duplicate p_config" );
340 /* Do the duplication job */
341 for( i = 0; i < i_lines ; i++ )
343 p_module->p_config[i].i_type = p_orig[i].i_type;
344 p_module->p_config[i].i_short = p_orig[i].i_short;
345 p_module->p_config[i].i_value = p_orig[i].i_value;
346 p_module->p_config[i].f_value = p_orig[i].f_value;
347 p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
349 p_module->p_config[i].psz_type = p_orig[i].psz_type ?
350 strdup( _(p_orig[i].psz_type) ) : NULL;
351 p_module->p_config[i].psz_name = p_orig[i].psz_name ?
352 strdup( _(p_orig[i].psz_name) ) : NULL;
353 p_module->p_config[i].psz_text = p_orig[i].psz_text ?
354 strdup( _(p_orig[i].psz_text) ) : NULL;
355 p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
356 strdup( _(p_orig[i].psz_longtext) ) : NULL;
357 p_module->p_config[i].psz_value = p_orig[i].psz_value ?
358 strdup( p_orig[i].psz_value ) : NULL;
360 p_module->p_config[i].p_lock = &p_module->object_lock;
362 /* duplicate the string list */
363 p_module->p_config[i].ppsz_list = NULL;
364 if( p_orig[i].ppsz_list )
366 for( j = 0; p_orig[i].ppsz_list[j]; j++ );
367 p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
368 if( p_module->p_config[i].ppsz_list )
370 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
371 p_module->p_config[i].ppsz_list[j] =
372 strdup( p_orig[i].ppsz_list[j] );
374 p_module->p_config[i].ppsz_list[j] = NULL;
377 p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
381 /*****************************************************************************
382 * config_Free: frees a duplicated module's configuration data.
383 *****************************************************************************
384 * This function frees all the data duplicated by config_Duplicate.
385 *****************************************************************************/
386 void config_Free( module_t *p_module )
388 module_config_t *p_item = p_module->p_config;
396 for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
398 if( p_item->psz_type )
399 free( p_item->psz_type );
401 if( p_item->psz_name )
402 free( p_item->psz_name );
404 if( p_item->psz_text )
405 free( p_item->psz_text );
407 if( p_item->psz_longtext )
408 free( p_item->psz_longtext );
410 if( p_item->psz_value )
411 free( p_item->psz_value );
413 if( p_item->ppsz_list )
415 for( i = 0; p_item->ppsz_list[i]; i++ )
416 free(p_item->ppsz_list[i]);
417 free( p_item->ppsz_list );
421 free( p_module->p_config );
422 p_module->p_config = NULL;
425 /*****************************************************************************
426 * config_SetCallbacks: sets callback functions in the duplicate p_config.
427 *****************************************************************************
428 * Unfortunatly we cannot work directly with the module's config data as
429 * this module might be unloaded from memory at any time (remember HideModule).
430 * This is why we need to duplicate callbacks each time we reload the module.
431 *****************************************************************************/
432 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
434 while( p_new->i_type != CONFIG_HINT_END )
436 p_new->pf_callback = p_orig->pf_callback;
442 /*****************************************************************************
443 * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
444 *****************************************************************************
445 * We simply undo what we did in config_SetCallbacks.
446 *****************************************************************************/
447 void config_UnsetCallbacks( module_config_t *p_new )
449 while( p_new->i_type != CONFIG_HINT_END )
451 p_new->pf_callback = NULL;
456 /*****************************************************************************
457 * config_LoadConfigFile: loads the configuration file.
458 *****************************************************************************
459 * This function is called to load the config options stored in the config
461 *****************************************************************************/
462 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
465 module_t **pp_parser;
466 module_config_t *p_item;
469 char *p_index, *psz_option_name, *psz_option_value;
470 char *psz_filename, *psz_homedir;
472 psz_homedir = p_this->p_vlc->psz_homedir;
475 msg_Err( p_this, "psz_homedir is null" );
478 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
479 strlen(psz_homedir) + 1 );
482 msg_Err( p_this, "out of memory" );
485 sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
487 msg_Dbg( p_this, "opening config file %s", psz_filename );
489 /* Acquire config file lock */
490 vlc_mutex_lock( &p_this->p_vlc->config_lock );
492 file = fopen( psz_filename, "rt" );
495 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
496 free( psz_filename );
497 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
501 /* Look for the selected module, if NULL then save everything */
502 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
504 for( pp_parser = (module_t **)p_list->pp_objects ;
510 && strcmp( psz_module_name, (*pp_parser)->psz_object_name ) )
515 /* The config file is organized in sections, one per module. Look for
516 * the interesting section ( a section is of the form [foo] ) */
518 while( fgets( line, 1024, file ) )
521 && (p_index = strchr(line,']'))
522 && (p_index - &line[1] == strlen((*pp_parser)->psz_object_name))
523 && !memcmp( &line[1], (*pp_parser)->psz_object_name,
524 strlen((*pp_parser)->psz_object_name) ) )
527 msg_Dbg( p_this, "loading config for module \"%s\"",
528 (*pp_parser)->psz_object_name );
534 /* either we found the section or we're at the EOF */
536 /* Now try to load the options in this section */
537 while( fgets( line, 1024, file ) )
539 if( line[0] == '[' ) break; /* end of section */
541 /* ignore comments or empty lines */
542 if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
545 /* get rid of line feed */
546 if( line[strlen(line)-1] == '\n' )
547 line[strlen(line)-1] = (char)0;
549 /* look for option name */
550 psz_option_name = line;
551 psz_option_value = NULL;
552 p_index = strchr( line, '=' );
553 if( !p_index ) break; /* this ain't an option!!! */
556 psz_option_value = p_index + 1;
558 if( !(*pp_parser)->i_config_items )
563 /* try to match this option with one of the module's options */
564 for( p_item = (*pp_parser)->p_config;
565 p_item->i_type != CONFIG_HINT_END;
568 if( p_item->i_type & CONFIG_HINT )
572 if( !strcmp( p_item->psz_name, psz_option_name ) )
575 switch( p_item->i_type )
577 case CONFIG_ITEM_BOOL:
578 case CONFIG_ITEM_INTEGER:
579 if( !*psz_option_value )
580 break; /* ignore empty option */
581 p_item->i_value = atoi( psz_option_value);
583 msg_Dbg( p_this, "option \"%s\", value %i",
584 p_item->psz_name, p_item->i_value );
588 case CONFIG_ITEM_FLOAT:
589 if( !*psz_option_value )
590 break; /* ignore empty option */
591 p_item->f_value = (float)atof( psz_option_value);
593 msg_Dbg( p_this, "option \"%s\", value %f",
594 p_item->psz_name, (double)p_item->f_value );
599 vlc_mutex_lock( p_item->p_lock );
601 /* free old string */
602 if( p_item->psz_value )
603 free( p_item->psz_value );
605 p_item->psz_value = *psz_option_value ?
606 strdup( psz_option_value ) : NULL;
608 vlc_mutex_unlock( p_item->p_lock );
611 msg_Dbg( p_this, "option \"%s\", value \"%s\"",
613 p_item->psz_value ? p_item->psz_value : "" );
623 vlc_list_release( p_list );
626 free( psz_filename );
628 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
633 /*****************************************************************************
634 * config_SaveConfigFile: Save a module's config options.
635 *****************************************************************************
636 * This will save the specified module's config options to the config file.
637 * If psz_module_name is NULL then we save all the modules config options.
638 * It's no use to save the config options that kept their default values, so
639 * we'll try to be a bit clever here.
641 * When we save we mustn't delete the config options of the modules that
642 * haven't been loaded. So we cannot just create a new config file with the
643 * config structures we've got in memory.
644 * I don't really know how to deal with this nicely, so I will use a completly
646 * I will load the config file in memory, but skipping all the sections of the
647 * modules we want to save. Then I will create a brand new file, dump the file
648 * loaded in memory and then append the sections of the modules we want to
651 *****************************************************************************/
652 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
654 module_t **pp_parser;
656 module_config_t *p_item;
658 char p_line[1024], *p_index2;
660 char *p_bigbuffer, *p_index;
662 char *psz_filename, *psz_homedir;
664 /* Acquire config file lock */
665 vlc_mutex_lock( &p_this->p_vlc->config_lock );
667 psz_homedir = p_this->p_vlc->psz_homedir;
670 msg_Err( p_this, "psz_homedir is null" );
671 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
674 psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
675 strlen(psz_homedir) + 1 );
678 msg_Err( p_this, "out of memory" );
679 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
682 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
685 if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
687 if( mkdir( psz_filename ) && errno != EEXIST )
690 msg_Err( p_this, "could not create %s (%s)",
691 psz_filename, strerror(errno) );
694 strcat( psz_filename, "/" CONFIG_FILE );
697 msg_Dbg( p_this, "opening config file %s", psz_filename );
699 file = fopen( psz_filename, "rt" );
702 msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
706 /* look for file size */
707 fseek( file, 0, SEEK_END );
708 i_sizebuf = ftell( file );
712 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
715 msg_Err( p_this, "out of memory" );
716 if( file ) fclose( file );
717 free( psz_filename );
718 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
723 /* List all available modules */
724 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
726 /* backup file into memory, we only need to backup the sections we won't
729 while( file && fgets( p_line, 1024, file ) )
731 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
734 /* we found a section, check if we need to do a backup */
735 for( pp_parser = (module_t **)p_list->pp_objects ;
739 if( ((p_index2 - &p_line[1])
740 == strlen((*pp_parser)->psz_object_name) ) &&
741 !memcmp( &p_line[1], (*pp_parser)->psz_object_name,
742 strlen((*pp_parser)->psz_object_name) ) )
744 if( !psz_module_name )
746 else if( !strcmp( psz_module_name,
747 (*pp_parser)->psz_object_name ) )
754 /* we don't have this section in our list so we need to back
757 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
769 /* save line if requested and line is valid (doesn't begin with a
770 * space, tab, or eol) */
771 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
772 && (p_line[0] != '\t') )
774 strcpy( p_index, p_line );
775 p_index += strlen( p_line );
778 if( file ) fclose( file );
782 * Save module config in file
785 file = fopen( psz_filename, "wt" );
788 msg_Warn( p_this, "could not open config file %s for writing",
790 free( psz_filename );
791 vlc_list_release( p_list );
792 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
796 fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
798 /* Look for the selected module, if NULL then save everything */
799 for( pp_parser = (module_t **)p_list->pp_objects ;
804 if( psz_module_name && strcmp( psz_module_name,
805 (*pp_parser)->psz_object_name ) )
808 if( !(*pp_parser)->i_config_items )
811 msg_Dbg( p_this, "saving config for module \"%s\"",
812 (*pp_parser)->psz_object_name );
814 fprintf( file, "[%s]", (*pp_parser)->psz_object_name );
815 if( (*pp_parser)->psz_longname )
816 fprintf( file, " # %s\n\n", (*pp_parser)->psz_longname );
818 fprintf( file, "\n\n" );
820 for( p_item = (*pp_parser)->p_config;
821 p_item->i_type != CONFIG_HINT_END;
824 if( p_item->i_type & CONFIG_HINT )
828 switch( p_item->i_type )
830 case CONFIG_ITEM_BOOL:
831 case CONFIG_ITEM_INTEGER:
832 if( p_item->psz_text )
833 fprintf( file, "# %s (%s)\n", p_item->psz_text,
834 (p_item->i_type == CONFIG_ITEM_BOOL) ?
835 _("boolean") : _("integer") );
836 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
839 case CONFIG_ITEM_FLOAT:
840 if( p_item->psz_text )
841 fprintf( file, "# %s (%s)\n", p_item->psz_text,
843 fprintf( file, "%s=%f\n", p_item->psz_name,
844 (double)p_item->f_value );
848 if( p_item->psz_text )
849 fprintf( file, "# %s (%s)\n", p_item->psz_text,
851 fprintf( file, "%s=%s\n", p_item->psz_name,
852 p_item->psz_value ? p_item->psz_value : "" );
856 fprintf( file, "\n" );
859 vlc_list_release( p_list );
862 * Restore old settings from the config in file
864 fputs( p_bigbuffer, file );
868 free( psz_filename );
869 vlc_mutex_unlock( &p_this->p_vlc->config_lock );
874 /*****************************************************************************
875 * config_LoadCmdLine: parse command line
876 *****************************************************************************
877 * Parse command line for configuration options.
878 * Now that the module_bank has been initialized, we can dynamically
879 * generate the longopts structure used by getops. We have to do it this way
880 * because we don't know (and don't want to know) in advance the configuration
881 * options used (ie. exported) by each module.
882 *****************************************************************************/
883 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
884 vlc_bool_t b_ignore_errors )
886 int i_cmd, i_index, i_opts, i_shortopts, flag;
887 module_t **pp_parser;
889 module_config_t *p_item;
890 struct option *p_longopts;
893 module_config_t *pp_shortopts[256];
896 /* Set default configuration and copy arguments */
897 p_this->p_vlc->i_argc = *pi_argc;
898 p_this->p_vlc->ppsz_argv = ppsz_argv;
900 p_this->p_vlc->p_channel = NULL;
903 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
904 * is the PSN - process serial number (a unique PID-ish thingie)
905 * still ok for real Darwin & when run from command line */
906 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
907 /* for example -psn_0_9306113 */
909 /* GDMF!... I can't do this or else the MacOSX window server will
910 * not pick up the PSN and not register the app and we crash...
911 * hence the following kludge otherwise we'll get confused w/ argv[1]
912 * being an input file name */
914 ppsz_argv[ 1 ] = NULL;
916 *pi_argc = *pi_argc - 1;
922 /* List all modules */
923 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
926 * Generate the longopts and shortopts structures used by getopt_long
930 for( pp_parser = (module_t **)p_list->pp_objects ;
934 /* count the number of exported configuration options (to allocate
935 * longopts). We also need to allocate space for too options when
936 * dealing with boolean to allow for --foo and --no-foo */
937 i_opts += (*pp_parser)->i_config_items
938 + 2 * (*pp_parser)->i_bool_items;
941 p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
942 if( p_longopts == NULL )
944 msg_Err( p_this, "out of memory" );
945 vlc_list_release( p_list );
949 psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
950 if( psz_shortopts == NULL )
952 msg_Err( p_this, "out of memory" );
954 vlc_list_release( p_list );
958 /* If we are requested to ignore errors, then we must work on a copy
959 * of the ppsz_argv array, otherwise getopt_long will reorder it for
960 * us, ignoring the arity of the options */
961 if( b_ignore_errors )
963 ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
964 if( ppsz_argv == NULL )
966 msg_Err( p_this, "out of memory" );
967 free( psz_shortopts );
969 vlc_list_release( p_list );
972 memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
973 *pi_argc * sizeof(char *) );
977 for( i_index = 0; i_index < 256; i_index++ )
979 pp_shortopts[i_index] = NULL;
982 /* Fill the p_longopts and psz_shortopts structures */
984 for( pp_parser = (module_t **)p_list->pp_objects ;
988 if( !(*pp_parser)->i_config_items )
991 for( p_item = (*pp_parser)->p_config;
992 p_item->i_type != CONFIG_HINT_END;
996 if( p_item->i_type & CONFIG_HINT )
999 /* Add item to long options */
1000 p_longopts[i_index].name = strdup( p_item->psz_name );
1001 if( p_longopts[i_index].name == NULL ) continue;
1002 p_longopts[i_index].has_arg =
1003 (p_item->i_type == CONFIG_ITEM_BOOL)?
1004 no_argument : required_argument;
1005 p_longopts[i_index].flag = &flag;
1006 p_longopts[i_index].val = 0;
1009 /* When dealing with bools we also need to add the --no-foo
1011 if( p_item->i_type == CONFIG_ITEM_BOOL )
1013 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1014 if( psz_name == NULL ) continue;
1015 strcpy( psz_name, "no" );
1016 strcat( psz_name, p_item->psz_name );
1018 p_longopts[i_index].name = psz_name;
1019 p_longopts[i_index].has_arg = no_argument;
1020 p_longopts[i_index].flag = &flag;
1021 p_longopts[i_index].val = 1;
1024 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1025 if( psz_name == NULL ) continue;
1026 strcpy( psz_name, "no-" );
1027 strcat( psz_name, p_item->psz_name );
1029 p_longopts[i_index].name = psz_name;
1030 p_longopts[i_index].has_arg = no_argument;
1031 p_longopts[i_index].flag = &flag;
1032 p_longopts[i_index].val = 1;
1036 /* If item also has a short option, add it */
1037 if( p_item->i_short )
1039 pp_shortopts[(int)p_item->i_short] = p_item;
1040 psz_shortopts[i_shortopts] = p_item->i_short;
1042 if( p_item->i_type != CONFIG_ITEM_BOOL )
1044 psz_shortopts[i_shortopts] = ':';
1047 if( p_item->i_short == 'v' )
1049 psz_shortopts[i_shortopts] = ':';
1057 /* We don't need the module list anymore */
1058 vlc_list_release( p_list );
1060 /* Close the longopts and shortopts structures */
1061 memset( &p_longopts[i_index], 0, sizeof(struct option) );
1062 psz_shortopts[i_shortopts] = '\0';
1065 * Parse the command line options
1069 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1070 p_longopts, &i_index ) ) != EOF )
1072 /* A long option has been recognized */
1075 module_config_t *p_conf;
1076 char *psz_name = (char *)p_longopts[i_index].name;
1078 /* Check if we deal with a --nofoo or --no-foo long option */
1079 if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1081 /* Store the configuration option */
1082 p_conf = config_FindConfig( p_this, psz_name );
1084 if( p_conf ) switch( p_conf->i_type )
1086 case CONFIG_ITEM_STRING:
1087 case CONFIG_ITEM_FILE:
1088 case CONFIG_ITEM_MODULE:
1089 config_PutPsz( p_this, psz_name, optarg );
1091 case CONFIG_ITEM_INTEGER:
1092 config_PutInt( p_this, psz_name, atoi(optarg));
1094 case CONFIG_ITEM_FLOAT:
1095 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1097 case CONFIG_ITEM_BOOL:
1098 config_PutInt( p_this, psz_name, !flag );
1105 /* A short option has been recognized */
1106 if( pp_shortopts[i_cmd] != NULL )
1108 switch( pp_shortopts[i_cmd]->i_type )
1110 case CONFIG_ITEM_STRING:
1111 case CONFIG_ITEM_FILE:
1112 case CONFIG_ITEM_MODULE:
1113 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1115 case CONFIG_ITEM_INTEGER:
1122 if( *optarg == 'v' ) /* eg. -vvvvv */
1125 while( *optarg == 'v' )
1133 i_verbose += atoi( optarg ); /* eg. -v2 */
1138 i_verbose = 1; /* -v */
1140 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1145 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1149 case CONFIG_ITEM_BOOL:
1150 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1157 /* Internal error: unknown option */
1158 if( !b_ignore_errors )
1160 fprintf( stderr, "unknown option `%s'\n", ppsz_argv[optind-1] );
1161 fprintf( stderr, "Try `%s --help' for more information.\n",
1162 p_this->p_vlc->psz_object_name );
1165 free( psz_shortopts );
1166 if( b_ignore_errors ) free( ppsz_argv );
1171 /* Free allocated resources */
1172 for( i_index = 0; p_longopts[i_index].name; i_index++ )
1173 free( (char *)p_longopts[i_index].name );
1175 free( psz_shortopts );
1176 if( b_ignore_errors ) free( ppsz_argv );
1181 /*****************************************************************************
1182 * config_GetHomeDir: find the user's home directory.
1183 *****************************************************************************
1184 * This function will try by different ways to find the user's home path.
1185 * Note that this function is not reentrant, it should be called only once
1186 * at the beginning of main where the result will be stored for later use.
1187 *****************************************************************************/
1188 char *config_GetHomeDir( void )
1190 char *p_tmp, *p_homedir = NULL;
1192 #if defined(HAVE_GETPWUID)
1193 struct passwd *p_pw = NULL;
1197 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1199 # define CSIDL_FLAG_CREATE 0x8000
1200 # define CSIDL_APPDATA 0x1A
1201 # define SHGFP_TYPE_CURRENT 0
1203 HINSTANCE shfolder_dll;
1204 SHGETFOLDERPATH SHGetFolderPath ;
1206 /* load the shell32 dll to retreive SHGetFolderPath */
1207 if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1209 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1210 "SHGetFolderPathA" );
1211 if ( SHGetFolderPath != NULL )
1213 p_homedir = (char *)malloc( MAX_PATH );
1219 /* get the "Application Data" folder for the current user */
1220 if( S_OK == SHGetFolderPath( NULL,
1221 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1222 NULL, SHGFP_TYPE_CURRENT,
1225 FreeLibrary( shfolder_dll );
1230 FreeLibrary( shfolder_dll );
1234 #if defined(HAVE_GETPWUID)
1235 if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1238 if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1240 if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1246 p_homedir = strdup( p_tmp );
1248 #if defined(HAVE_GETPWUID)
1251 p_homedir = strdup( p_pw->pw_dir );