]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
f8ea0aaaa4451bb29cd2eb3808d1c3c9bffd04cb
[vlc] / src / misc / configuration.c
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 $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <videolan/vlc.h>
25
26 #include <stdio.h>                                              /* sprintf() */
27 #include <stdlib.h>                                      /* free(), strtol() */
28 #include <string.h>                                              /* strdup() */
29 #include <errno.h>                                                  /* errno */
30
31 #ifdef HAVE_UNISTD_H
32 #    include <unistd.h>                                          /* getuid() */
33 #endif
34
35 #ifdef HAVE_GETOPT_LONG
36 #   ifdef HAVE_GETOPT_H
37 #       include <getopt.h>                                       /* getopt() */
38 #   endif
39 #else
40 #   include "GNUgetopt/getopt.h"
41 #endif
42
43 #if defined(HAVE_GETPWUID)
44 #include <pwd.h>                                               /* getpwuid() */
45 #endif
46
47 #include <sys/stat.h>
48 #include <sys/types.h>
49
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 )
58 {
59     module_config_t *p_config;
60
61     p_config = config_FindConfig( psz_name );
62
63     /* sanity checks */
64     if( !p_config )
65     {
66         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
67         return -1;
68     }
69     if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
70         (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
71     {
72         intf_ErrMsg( "config error: option %s doesn't refer to an int",
73                      psz_name );
74         return -1;
75     }
76
77     return p_config->i_value;
78 }
79
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 )
87 {
88     module_config_t *p_config;
89
90     p_config = config_FindConfig( psz_name );
91
92     /* sanity checks */
93     if( !p_config )
94     {
95         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
96         return -1;
97     }
98     if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
99     {
100         intf_ErrMsg( "config error: option %s doesn't refer to a float",
101                      psz_name );
102         return -1;
103     }
104
105     return p_config->f_value;
106 }
107
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).
114  *
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 )
120 {
121     module_config_t *p_config;
122     char *psz_value = NULL;
123
124     p_config = config_FindConfig( psz_name );
125
126     /* sanity checks */
127     if( !p_config )
128     {
129         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
130         return NULL;
131     }
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) )
135     {
136         intf_ErrMsg( "config error: option %s doesn't refer to a string",
137                      psz_name );
138         return NULL;
139     }
140
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 );
145
146     return psz_value;
147 }
148
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 )
157 {
158     module_config_t *p_config;
159
160     p_config = config_FindConfig( psz_name );
161
162     /* sanity checks */
163     if( !p_config )
164     {
165         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
166         return;
167     }
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) )
171     {
172         intf_ErrMsg( "config error: option %s doesn't refer to a string",
173                      psz_name );
174         return;
175     }
176
177     vlc_mutex_lock( p_config->p_lock );
178
179     /* free old string */
180     if( p_config->psz_value ) free( p_config->psz_value );
181
182     if( psz_value ) p_config->psz_value = strdup( psz_value );
183     else p_config->psz_value = NULL;
184
185     vlc_mutex_unlock( p_config->p_lock );
186
187 }
188
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 )
197 {
198     module_config_t *p_config;
199
200     p_config = config_FindConfig( psz_name );
201
202     /* sanity checks */
203     if( !p_config )
204     {
205         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
206         return;
207     }
208     if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
209         (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
210     {
211         intf_ErrMsg( "config error: option %s doesn't refer to an int",
212                      psz_name );
213         return;
214     }
215
216     p_config->i_value = i_value;
217 }
218
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 )
226 {
227     module_config_t *p_config;
228
229     p_config = config_FindConfig( psz_name );
230
231     /* sanity checks */
232     if( !p_config )
233     {
234         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
235         return;
236     }
237     if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
238     {
239         intf_ErrMsg( "config error: option %s doesn't refer to a float",
240                      psz_name );
241         return;
242     }
243
244     p_config->f_value = f_value;
245 }
246
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 )
253 {
254     module_t *p_module;
255     module_config_t *p_item;
256
257     if( !psz_name ) return NULL;
258
259     for( p_module = p_module_bank->first ;
260          p_module != NULL ;
261          p_module = p_module->next )
262     {
263         for( p_item = p_module->p_config;
264              p_item->i_type != MODULE_CONFIG_HINT_END;
265              p_item++ )
266         {
267             if( p_item->i_type & MODULE_CONFIG_HINT )
268                 /* ignore hints */
269                 continue;
270             if( !strcmp( psz_name, p_item->psz_name ) )
271                 return p_item;
272         }
273     }
274
275     return NULL;
276 }
277
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 )
286 {
287     int i, i_lines;
288     module_config_t *p_config;
289
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++ );
294
295     /* Allocate memory */
296     p_config = (module_config_t *)malloc( sizeof(module_config_t) * i_lines );
297     if( p_config == NULL )
298     {
299         intf_ErrMsg( "config error: can't duplicate p_config" );
300         return( NULL );
301     }
302
303     /* Do the duplication job */
304     for( i = 0; i < i_lines ; i++ )
305     {
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;
311
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;
320
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;
324     }
325
326     return p_config;
327 }
328
329 /*****************************************************************************
330  * config_LoadConfigFile: loads the configuration file.
331  *****************************************************************************
332  * This function is called to load the config options stored in the config
333  * file.
334  *****************************************************************************/
335 int config_LoadConfigFile( const char *psz_module_name )
336 {
337     module_t *p_module;
338     module_config_t *p_item;
339     FILE *file;
340     char line[1024];
341     char *p_index, *psz_option_name, *psz_option_value;
342     char *psz_filename, *psz_homedir;
343
344     /* Acquire config file lock */
345     vlc_mutex_lock( &p_main->config_lock );
346
347     psz_homedir = p_main->psz_homedir;
348     if( !psz_homedir )
349     {
350         intf_ErrMsg( "config error: p_main->psz_homedir is null" );
351         vlc_mutex_unlock( &p_main->config_lock );
352         return -1;
353     }
354     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
355                                    strlen(psz_homedir) + 1 );
356     if( !psz_filename )
357     {
358         intf_ErrMsg( "config error: couldn't malloc psz_filename" );
359         vlc_mutex_unlock( &p_main->config_lock );
360         return -1;
361     }
362     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
363
364     intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
365
366     file = fopen( psz_filename, "r" );
367     if( !file )
368     {
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 );
373         return -1;
374     }
375
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 )
379     {
380
381         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
382             continue;
383
384         /* The config file is organized in sections, one per module. Look for
385          * the interesting section ( a section is of the form [foo] ) */
386         rewind( file );
387         while( fgets( line, 1024, file ) )
388         {
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) ) )
393             {
394                 intf_WarnMsg( 5, "config: loading config for module <%s>",
395                                  p_module->psz_name );
396
397                 break;
398             }
399         }
400         /* either we found the section or we're at the EOF */
401
402         /* Now try to load the options in this section */
403         while( fgets( line, 1024, file ) )
404         {
405             if( line[0] == '[' ) break; /* end of section */
406
407             /* ignore comments or empty lines */
408             if( (line[0] == '#') || (line[0] == (char)0) ) continue;
409
410             /* get rid of line feed */
411             if( line[strlen(line)-1] == '\n' )
412                 line[strlen(line)-1] = (char)0;
413
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!!! */
419
420             *p_index = (char)0;
421             psz_option_value = p_index + 1;
422
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;
426                  p_item++ )
427             {
428                 if( p_item->i_type & MODULE_CONFIG_HINT )
429                     /* ignore hints */
430                     continue;
431
432                 if( !strcmp( p_item->psz_name, psz_option_name ) )
433                 {
434                     /* We found it */
435                     switch( p_item->i_type )
436                     {
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",
443                                          p_module->psz_name,
444                                          p_item->psz_name, p_item->i_value );
445                         break;
446
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 );
454                         break;
455
456                     default:
457                         vlc_mutex_lock( p_item->p_lock );
458
459                         /* free old string */
460                         if( p_item->psz_value )
461                             free( p_item->psz_value );
462
463                         p_item->psz_value = *psz_option_value ?
464                             strdup( psz_option_value ) : NULL;
465
466                         vlc_mutex_unlock( p_item->p_lock );
467
468                         intf_WarnMsg( 7, "config: found <%s> option %s=%s",
469                                          p_module->psz_name,
470                                          p_item->psz_name,
471                                          p_item->psz_value != NULL ?
472                                            p_item->psz_value : "(NULL)" );
473                         break;
474                     }
475                 }
476             }
477         }
478
479     }
480     
481     fclose( file );
482     free( psz_filename );
483
484     vlc_mutex_unlock( &p_main->config_lock );
485
486     return 0;
487 }
488
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.
496  *
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
501  * dumb method ;-)
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
505  * save.
506  * Really stupid no ?
507  *****************************************************************************/
508 int config_SaveConfigFile( const char *psz_module_name )
509 {
510     module_t *p_module;
511     module_config_t *p_item;
512     FILE *file;
513     char p_line[1024], *p_index2;
514     int i_sizebuf = 0;
515     char *p_bigbuffer, *p_index;
516     boolean_t b_backup;
517     char *psz_filename, *psz_homedir;
518
519     /* Acquire config file lock */
520     vlc_mutex_lock( &p_main->config_lock );
521
522     psz_homedir = p_main->psz_homedir;
523     if( !psz_homedir )
524     {
525         intf_ErrMsg( "config error: p_main->psz_homedir is null" );
526         vlc_mutex_unlock( &p_main->config_lock );
527         return -1;
528     }
529     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
530                                    strlen(psz_homedir) + 1 );
531     if( !psz_filename )
532     {
533         intf_ErrMsg( "config error: couldn't malloc psz_filename" );
534         vlc_mutex_unlock( &p_main->config_lock );
535         return -1;
536     }
537     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
538
539 #ifndef WIN32
540     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
541 #else
542     if( mkdir( psz_filename ) && errno != EEXIST )
543 #endif
544     {
545         intf_ErrMsg( "config error: couldn't create %s (%s)",
546                      psz_filename, strerror(errno) );
547     }
548
549     strcat( psz_filename, "/" CONFIG_FILE );
550
551
552     intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
553
554     file = fopen( psz_filename, "r" );
555     if( !file )
556     {
557         intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
558                          psz_filename, strerror(errno) );
559     }
560     else
561     {
562         /* look for file size */
563         fseek( file, 0, SEEK_END );
564         i_sizebuf = ftell( file );
565         rewind( file );
566     }
567
568     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
569     if( !p_bigbuffer )
570     {
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 );
575         return -1;
576     }
577     p_bigbuffer[0] = 0;
578
579     /* backup file into memory, we only need to backup the sections we won't
580      * save later on */
581     b_backup = 0;
582     while( file && fgets( p_line, 1024, file ) )
583     {
584         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
585         {
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 )
589             {
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) ) )
593                 {
594                     if( !psz_module_name )
595                         break;
596                     else if( !strcmp( psz_module_name, p_module->psz_name ) )
597                         break;
598                 }
599             }
600
601             if( !p_module )
602             {
603                 /* we don't have this section in our list so we need to back
604                  * it up */
605                 *p_index2 = 0;
606                 intf_WarnMsg( 5, "config: backing up config for "
607                                  "unknown module <%s>", &p_line[1] );
608                 *p_index2 = ']';
609
610                 b_backup = 1;
611             }
612             else
613             {
614                 b_backup = 0;
615             }
616         }
617
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') )
622         {
623             strcpy( p_index, p_line );
624             p_index += strlen( p_line );
625         }
626     }
627     if( file ) fclose( file );
628
629
630     /*
631      * Save module config in file
632      */
633
634     file = fopen( psz_filename, "w" );
635     if( !file )
636     {
637         intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
638                          psz_filename );
639         free( psz_filename );
640         vlc_mutex_unlock( &p_main->config_lock );
641         return -1;
642     }
643
644     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
645
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 )
649     {
650
651         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
652             continue;
653
654         if( !p_module->i_config_items )
655             continue;
656
657         intf_WarnMsg( 5, "config: saving config for module <%s>",
658                          p_module->psz_name );
659
660         fprintf( file, "[%s]\n", p_module->psz_name );
661
662         if( p_module->psz_longname )
663             fprintf( file, "###\n###  %s\n###\n", p_module->psz_longname );
664
665         for( p_item = p_module->p_config;
666              p_item->i_type != MODULE_CONFIG_HINT_END;
667              p_item++ )
668         {
669             if( p_item->i_type & MODULE_CONFIG_HINT )
670                 /* ignore hints */
671                 continue;
672
673             switch( p_item->i_type )
674             {
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,
682                          p_item->i_value );
683                 break;
684
685             case MODULE_CONFIG_ITEM_FLOAT:
686                 if( p_item->psz_text )
687                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
688                              _("float") );
689                 fprintf( file, "%s=%f\n", p_item->psz_name,
690                          (double)p_item->f_value );
691                 break;
692
693             default:
694                 if( p_item->psz_text )
695                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
696                              _("string") );
697                 fprintf( file, "%s=%s\n", p_item->psz_name,
698                          p_item->psz_value ? p_item->psz_value : "" );
699             }
700         }
701
702         fprintf( file, "\n" );
703     }
704
705
706     /*
707      * Restore old settings from the config in file
708      */
709     fputs( p_bigbuffer, file );
710     free( p_bigbuffer );
711
712     fclose( file );
713     free( psz_filename );
714     vlc_mutex_unlock( &p_main->config_lock );
715
716     return 0;
717 }
718
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 )
730 {
731     int i_cmd, i_index, i_opts, i_shortopts;
732     module_t *p_module;
733     module_config_t *p_item;
734     struct option *p_longopts;
735
736     /* Short options */
737     module_config_t *pp_shortopts[256];
738     char *psz_shortopts;
739
740     /* reset warning level */
741     p_main->i_warning_level = 0;
742
743     /* Set default configuration and copy arguments */
744     p_main->i_argc    = *pi_argc;
745     p_main->ppsz_argv = ppsz_argv;
746
747     p_main->p_channel = NULL;
748
749 #ifdef SYS_DARWIN
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 */
755     {
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 */
760 #if 0
761         ppsz_argv[ 1 ] = NULL;
762 #endif
763         *pi_argc = *pi_argc - 1;
764         pi_argc--;
765         return( 0 );
766     }
767 #endif
768
769     /*
770      * Generate the longopts and shortopts structure used by getopt_long
771      */
772
773     i_opts = 0;
774     for( p_module = p_module_bank->first;
775          p_module != NULL ;
776          p_module = p_module->next )
777     {
778         /* count the number of exported configuration options (to allocate
779          * longopts). */
780         i_opts += p_module->i_config_items;
781     }
782
783     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
784     if( p_longopts == NULL )
785     {
786         intf_ErrMsg( "config error: couldn't allocate p_longopts" );
787         return( -1 );
788     }
789
790     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
791     if( psz_shortopts == NULL )
792     {
793         intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
794         free( p_longopts );
795         return( -1 );
796     }
797
798     psz_shortopts[0] = 'v';
799     i_shortopts = 1;
800     for( i_index = 0; i_index < 256; i_index++ )
801     {
802         pp_shortopts[i_index] = NULL;
803     }
804
805     /* Fill the longopts structure */
806     i_index = 0;
807     for( p_module = p_module_bank->first ;
808          p_module != NULL ;
809          p_module = p_module->next )
810     {
811         for( p_item = p_module->p_config;
812              p_item->i_type != MODULE_CONFIG_HINT_END;
813              p_item++ )
814         {
815             if( p_item->i_type & MODULE_CONFIG_HINT )
816                 /* ignore hints */
817                 continue;
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 )
825             {
826                 pp_shortopts[(int)p_item->i_short] = p_item;
827                 psz_shortopts[i_shortopts] = p_item->i_short;
828                 i_shortopts++;
829                 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
830                 {
831                     psz_shortopts[i_shortopts] = ':';
832                     i_shortopts++;
833                 }
834             }
835             i_index++;
836         }
837     }
838
839     /* Close the longopts and shortopts structures */
840     memset( &p_longopts[i_index], 0, sizeof(struct option) );
841     psz_shortopts[i_shortopts] = '\0';
842
843     /*
844      * Parse the command line options
845      */
846     opterr = 0;
847     optind = 1;
848     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
849                                   p_longopts, &i_index ) ) != EOF )
850     {
851         /* A long option has been recognized */
852         if( i_cmd == 0 )
853         {
854             module_config_t *p_conf;
855
856             /* Store the configuration option */
857             p_conf = config_FindConfig( p_longopts[i_index].name );
858
859             switch( p_conf->i_type )
860             {
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 );
865                 break;
866             case MODULE_CONFIG_ITEM_INTEGER:
867                 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
868                 break;
869             case MODULE_CONFIG_ITEM_FLOAT:
870                 config_PutFloatVariable( p_longopts[i_index].name,
871                                          (float)atof(optarg) );
872                 break;
873             case MODULE_CONFIG_ITEM_BOOL:
874                 config_PutIntVariable( p_longopts[i_index].name, 1 );
875                 break;
876             }
877
878             continue;
879         }
880
881         /* A short option has been recognized */
882         if( pp_shortopts[i_cmd] != NULL )
883         {
884             switch( pp_shortopts[i_cmd]->i_type )
885             {
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 );
890                 break;
891             case MODULE_CONFIG_ITEM_INTEGER:
892                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
893                                        atoi(optarg));
894                 break;
895             case MODULE_CONFIG_ITEM_BOOL:
896                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
897                 break;
898             }
899
900             continue;
901         }
902
903         /* Either it's a -v or it's an unknown short option */
904         if( i_cmd == 'v' )
905         {
906             p_main->i_warning_level++;
907             continue;
908         }
909
910         /* Internal error: unknown option */
911         if( !b_ignore_errors )
912         {
913             intf_ErrMsg( "config error: unknown option `%s'",
914                          ppsz_argv[optind-1] );
915             intf_Msg( "Try `%s --help' for more information.\n",
916                       p_main->psz_arg0 );
917
918             free( p_longopts );
919             free( psz_shortopts );
920             return( -1 );
921         }
922     }
923
924     free( p_longopts );
925     free( psz_shortopts );
926
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 );
932
933     return( 0 );
934 }
935
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 )
944 {
945     char *p_tmp, *p_homedir = NULL;
946
947 #if defined(HAVE_GETPWUID)
948     struct passwd *p_pw = NULL;
949 #endif
950
951 #if defined(HAVE_GETPWUID)
952     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
953 #endif
954     {
955         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
956         {
957             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
958             {
959                 p_homedir = strdup( "/tmp" );
960             }
961             else p_homedir = strdup( p_tmp );
962
963             intf_ErrMsg( "config error: unable to get home directory, "
964                          "using %s instead", p_homedir );
965
966         }
967         else p_homedir = strdup( p_tmp );
968     }
969 #if defined(HAVE_GETPWUID)
970     else
971     {
972         p_homedir = strdup( p_pw->pw_dir );
973     }
974 #endif
975
976     return p_homedir;
977 }