]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
f2e95248d94a3964693d067c8ea9dc2dd4615242
[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.24 2002/05/18 13:30:28 gbazin 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     if( p_config->p_callback )
188     {
189         ((void(*)(void))p_config->p_callback)();
190     }
191 }
192
193 /*****************************************************************************
194  * config_PutIntVariable: set the integer value of an int variable
195  *****************************************************************************
196  * This function is used to set the value of variables which are internally
197  * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
198  * MODULE_CONFIG_ITEM_BOOL).
199  *****************************************************************************/
200 void config_PutIntVariable( const char *psz_name, int i_value )
201 {
202     module_config_t *p_config;
203
204     p_config = config_FindConfig( psz_name );
205
206     /* sanity checks */
207     if( !p_config )
208     {
209         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
210         return;
211     }
212     if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
213         (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
214     {
215         intf_ErrMsg( "config error: option %s doesn't refer to an int",
216                      psz_name );
217         return;
218     }
219
220     p_config->i_value = i_value;
221
222     if( p_config->p_callback )
223     {
224         ((void(*)(void))p_config->p_callback)();
225     }
226 }
227
228 /*****************************************************************************
229  * config_PutFloatVariable: set the value of a float variable
230  *****************************************************************************
231  * This function is used to set the value of variables which are internally
232  * represented by a float (MODULE_CONFIG_ITEM_FLOAT).
233  *****************************************************************************/
234 void config_PutFloatVariable( const char *psz_name, float f_value )
235 {
236     module_config_t *p_config;
237
238     p_config = config_FindConfig( psz_name );
239
240     /* sanity checks */
241     if( !p_config )
242     {
243         intf_ErrMsg( "config error: option %s doesn't exist", psz_name );
244         return;
245     }
246     if( p_config->i_type != MODULE_CONFIG_ITEM_FLOAT )
247     {
248         intf_ErrMsg( "config error: option %s doesn't refer to a float",
249                      psz_name );
250         return;
251     }
252
253     p_config->f_value = f_value;
254
255     if( p_config->p_callback )
256     {
257         ((void(*)(void))p_config->p_callback)();
258     }
259 }
260
261 /*****************************************************************************
262  * config_FindConfig: find the config structure associated with an option.
263  *****************************************************************************
264  * FIXME: This function really needs to be optimized.
265  *****************************************************************************/
266 module_config_t *config_FindConfig( const char *psz_name )
267 {
268     module_t *p_module;
269     module_config_t *p_item;
270
271     if( !psz_name ) return NULL;
272
273     for( p_module = p_module_bank->first ;
274          p_module != NULL ;
275          p_module = p_module->next )
276     {
277         for( p_item = p_module->p_config;
278              p_item->i_type != MODULE_CONFIG_HINT_END;
279              p_item++ )
280         {
281             if( p_item->i_type & MODULE_CONFIG_HINT )
282                 /* ignore hints */
283                 continue;
284             if( !strcmp( psz_name, p_item->psz_name ) )
285                 return p_item;
286         }
287     }
288
289     return NULL;
290 }
291
292 /*****************************************************************************
293  * config_Duplicate: creates a duplicate of a module's configuration data.
294  *****************************************************************************
295  * Unfortunatly we cannot work directly with the module's config data as
296  * this module might be unloaded from memory at any time (remember HideModule).
297  * This is why we need to create an exact copy of the config data.
298  *****************************************************************************/
299 module_config_t *config_Duplicate( module_config_t *p_orig )
300 {
301     int i, i_lines;
302     module_config_t *p_config;
303
304     /* Calculate the structure length */
305     for( p_config = p_orig, i_lines = 1;
306          p_config->i_type != MODULE_CONFIG_HINT_END;
307          p_config++, i_lines++ );
308
309     /* Allocate memory */
310     p_config = (module_config_t *)malloc( sizeof(module_config_t) * i_lines );
311     if( p_config == NULL )
312     {
313         intf_ErrMsg( "config error: can't duplicate p_config" );
314         return( NULL );
315     }
316
317     /* Do the duplication job */
318     for( i = 0; i < i_lines ; i++ )
319     {
320         p_config[i].i_type = p_orig[i].i_type;
321         p_config[i].i_short = p_orig[i].i_short;
322         p_config[i].i_value = p_orig[i].i_value;
323         p_config[i].f_value = p_orig[i].f_value;
324         p_config[i].b_dirty = p_orig[i].b_dirty;
325
326         p_config[i].psz_name = p_orig[i].psz_name ?
327                                    strdup( _(p_orig[i].psz_name) ) : NULL;
328         p_config[i].psz_text = p_orig[i].psz_text ?
329                                    strdup( _(p_orig[i].psz_text) ) : NULL;
330         p_config[i].psz_longtext = p_orig[i].psz_longtext ?
331                                    strdup( _(p_orig[i].psz_longtext) ) : NULL;
332         p_config[i].psz_value = p_orig[i].psz_value ?
333                                    strdup( _(p_orig[i].psz_value) ) : NULL;
334
335         /* the callback pointer is only valid when the module is loaded so this
336          * value is set in ActivateModule() and reset in DeactivateModule() */
337         p_config[i].p_callback = NULL;
338     }
339
340     return p_config;
341 }
342
343 /*****************************************************************************
344  * config_SetCallbacks: sets callback functions in the duplicate p_config.
345  *****************************************************************************
346  * Unfortunatly we cannot work directly with the module's config data as
347  * this module might be unloaded from memory at any time (remember HideModule).
348  * This is why we need to duplicate callbacks each time we reload the module.
349  *****************************************************************************/
350 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
351 {
352     while( p_new->i_type != MODULE_CONFIG_HINT_END )
353     {
354         p_new->p_callback = p_orig->p_callback;
355         p_new++;
356         p_orig++;
357     }
358 }
359
360 /*****************************************************************************
361  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
362  *****************************************************************************
363  * We simply undo what we did in config_SetCallbacks.
364  *****************************************************************************/
365 void config_UnsetCallbacks( module_config_t *p_new )
366 {
367     while( p_new->i_type != MODULE_CONFIG_HINT_END )
368     {
369         p_new->p_callback = NULL;
370         p_new++;
371     }
372 }
373
374 /*****************************************************************************
375  * config_LoadConfigFile: loads the configuration file.
376  *****************************************************************************
377  * This function is called to load the config options stored in the config
378  * file.
379  *****************************************************************************/
380 int config_LoadConfigFile( const char *psz_module_name )
381 {
382     module_t *p_module;
383     module_config_t *p_item;
384     FILE *file;
385     char line[1024];
386     char *p_index, *psz_option_name, *psz_option_value;
387     char *psz_filename, *psz_homedir;
388
389     /* Acquire config file lock */
390     vlc_mutex_lock( &p_main->config_lock );
391
392     psz_homedir = p_main->psz_homedir;
393     if( !psz_homedir )
394     {
395         intf_ErrMsg( "config error: p_main->psz_homedir is null" );
396         vlc_mutex_unlock( &p_main->config_lock );
397         return -1;
398     }
399     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
400                                    strlen(psz_homedir) + 1 );
401     if( !psz_filename )
402     {
403         intf_ErrMsg( "config error: couldn't malloc psz_filename" );
404         vlc_mutex_unlock( &p_main->config_lock );
405         return -1;
406     }
407     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
408
409     intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
410
411     file = fopen( psz_filename, "rt" );
412     if( !file )
413     {
414         intf_WarnMsg( 1, "config: config file %s doesn't already exist",
415                          psz_filename );
416         free( psz_filename );
417         vlc_mutex_unlock( &p_main->config_lock );
418         return -1;
419     }
420
421     /* Look for the selected module, if NULL then save everything */
422     for( p_module = p_module_bank->first ; p_module != NULL ;
423          p_module = p_module->next )
424     {
425
426         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
427             continue;
428
429         /* The config file is organized in sections, one per module. Look for
430          * the interesting section ( a section is of the form [foo] ) */
431         rewind( file );
432         while( fgets( line, 1024, file ) )
433         {
434             if( (line[0] == '[') && (p_index = strchr(line,']')) &&
435                 (p_index - &line[1] == strlen(p_module->psz_name) ) &&
436                 !memcmp( &line[1], p_module->psz_name,
437                          strlen(p_module->psz_name) ) )
438             {
439                 intf_WarnMsg( 5, "config: loading config for module <%s>",
440                                  p_module->psz_name );
441
442                 break;
443             }
444         }
445         /* either we found the section or we're at the EOF */
446
447         /* Now try to load the options in this section */
448         while( fgets( line, 1024, file ) )
449         {
450             if( line[0] == '[' ) break; /* end of section */
451
452             /* ignore comments or empty lines */
453             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
454                 continue;
455
456             /* get rid of line feed */
457             if( line[strlen(line)-1] == '\n' )
458                 line[strlen(line)-1] = (char)0;
459
460             /* look for option name */
461             psz_option_name = line;
462             psz_option_value = NULL;
463             p_index = strchr( line, '=' );
464             if( !p_index ) break; /* this ain't an option!!! */
465
466             *p_index = (char)0;
467             psz_option_value = p_index + 1;
468
469             /* try to match this option with one of the module's options */
470             for( p_item = p_module->p_config;
471                  p_item->i_type != MODULE_CONFIG_HINT_END;
472                  p_item++ )
473             {
474                 if( p_item->i_type & MODULE_CONFIG_HINT )
475                     /* ignore hints */
476                     continue;
477
478                 if( !strcmp( p_item->psz_name, psz_option_name ) )
479                 {
480                     /* We found it */
481                     switch( p_item->i_type )
482                     {
483                     case MODULE_CONFIG_ITEM_BOOL:
484                     case MODULE_CONFIG_ITEM_INTEGER:
485                         if( !*psz_option_value )
486                             break;                    /* ignore empty option */
487                         p_item->i_value = atoi( psz_option_value);
488                         intf_WarnMsg( 7, "config: found <%s> option %s=%i",
489                                          p_module->psz_name,
490                                          p_item->psz_name, p_item->i_value );
491                         break;
492
493                     case MODULE_CONFIG_ITEM_FLOAT:
494                         if( !*psz_option_value )
495                             break;                    /* ignore empty option */
496                         p_item->f_value = (float)atof( psz_option_value);
497                         intf_WarnMsg( 7, "config: found <%s> option %s=%f",
498                                          p_module->psz_name, p_item->psz_name,
499                                          (double)p_item->f_value );
500                         break;
501
502                     default:
503                         vlc_mutex_lock( p_item->p_lock );
504
505                         /* free old string */
506                         if( p_item->psz_value )
507                             free( p_item->psz_value );
508
509                         p_item->psz_value = *psz_option_value ?
510                             strdup( psz_option_value ) : NULL;
511
512                         vlc_mutex_unlock( p_item->p_lock );
513
514                         intf_WarnMsg( 7, "config: found <%s> option %s=%s",
515                                          p_module->psz_name,
516                                          p_item->psz_name,
517                                          p_item->psz_value != NULL ?
518                                            p_item->psz_value : "(NULL)" );
519                         break;
520                     }
521                 }
522             }
523         }
524
525     }
526     
527     fclose( file );
528     free( psz_filename );
529
530     vlc_mutex_unlock( &p_main->config_lock );
531
532     return 0;
533 }
534
535 /*****************************************************************************
536  * config_SaveConfigFile: Save a module's config options.
537  *****************************************************************************
538  * This will save the specified module's config options to the config file.
539  * If psz_module_name is NULL then we save all the modules config options.
540  * It's no use to save the config options that kept their default values, so
541  * we'll try to be a bit clever here.
542  *
543  * When we save we mustn't delete the config options of the modules that
544  * haven't been loaded. So we cannot just create a new config file with the
545  * config structures we've got in memory. 
546  * I don't really know how to deal with this nicely, so I will use a completly
547  * dumb method ;-)
548  * I will load the config file in memory, but skipping all the sections of the
549  * modules we want to save. Then I will create a brand new file, dump the file
550  * loaded in memory and then append the sections of the modules we want to
551  * save.
552  * Really stupid no ?
553  *****************************************************************************/
554 int config_SaveConfigFile( const char *psz_module_name )
555 {
556     module_t *p_module;
557     module_config_t *p_item;
558     FILE *file;
559     char p_line[1024], *p_index2;
560     int i_sizebuf = 0;
561     char *p_bigbuffer, *p_index;
562     boolean_t b_backup;
563     char *psz_filename, *psz_homedir;
564
565     /* Acquire config file lock */
566     vlc_mutex_lock( &p_main->config_lock );
567
568     psz_homedir = p_main->psz_homedir;
569     if( !psz_homedir )
570     {
571         intf_ErrMsg( "config error: p_main->psz_homedir is null" );
572         vlc_mutex_unlock( &p_main->config_lock );
573         return -1;
574     }
575     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
576                                    strlen(psz_homedir) + 1 );
577     if( !psz_filename )
578     {
579         intf_ErrMsg( "config error: couldn't malloc psz_filename" );
580         vlc_mutex_unlock( &p_main->config_lock );
581         return -1;
582     }
583     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
584
585 #ifndef WIN32
586     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
587 #else
588     if( mkdir( psz_filename ) && errno != EEXIST )
589 #endif
590     {
591         intf_ErrMsg( "config error: couldn't create %s (%s)",
592                      psz_filename, strerror(errno) );
593     }
594
595     strcat( psz_filename, "/" CONFIG_FILE );
596
597
598     intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
599
600     file = fopen( psz_filename, "rt" );
601     if( !file )
602     {
603         intf_WarnMsg( 1, "config: config file %s doesn't already exist",
604                          psz_filename );
605     }
606     else
607     {
608         /* look for file size */
609         fseek( file, 0, SEEK_END );
610         i_sizebuf = ftell( file );
611         rewind( file );
612     }
613
614     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
615     if( !p_bigbuffer )
616     {
617         intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
618         if( file ) fclose( file );
619         free( psz_filename );
620         vlc_mutex_unlock( &p_main->config_lock );
621         return -1;
622     }
623     p_bigbuffer[0] = 0;
624
625     /* backup file into memory, we only need to backup the sections we won't
626      * save later on */
627     b_backup = 0;
628     while( file && fgets( p_line, 1024, file ) )
629     {
630         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
631         {
632             /* we found a section, check if we need to do a backup */
633             for( p_module = p_module_bank->first; p_module != NULL;
634                  p_module = p_module->next )
635             {
636                 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
637                     !memcmp( &p_line[1], p_module->psz_name,
638                              strlen(p_module->psz_name) ) )
639                 {
640                     if( !psz_module_name )
641                         break;
642                     else if( !strcmp( psz_module_name, p_module->psz_name ) )
643                         break;
644                 }
645             }
646
647             if( !p_module )
648             {
649                 /* we don't have this section in our list so we need to back
650                  * it up */
651                 *p_index2 = 0;
652                 intf_WarnMsg( 5, "config: backing up config for "
653                                  "unknown module <%s>", &p_line[1] );
654                 *p_index2 = ']';
655
656                 b_backup = 1;
657             }
658             else
659             {
660                 b_backup = 0;
661             }
662         }
663
664         /* save line if requested and line is valid (doesn't begin with a
665          * space, tab, or eol) */
666         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
667             && (p_line[0] != '\t') )
668         {
669             strcpy( p_index, p_line );
670             p_index += strlen( p_line );
671         }
672     }
673     if( file ) fclose( file );
674
675
676     /*
677      * Save module config in file
678      */
679
680     file = fopen( psz_filename, "wt" );
681     if( !file )
682     {
683         intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
684                          psz_filename );
685         free( psz_filename );
686         vlc_mutex_unlock( &p_main->config_lock );
687         return -1;
688     }
689
690     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
691
692     /* Look for the selected module, if NULL then save everything */
693     for( p_module = p_module_bank->first ; p_module != NULL ;
694          p_module = p_module->next )
695     {
696
697         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
698             continue;
699
700         if( !p_module->i_config_items )
701             continue;
702
703         intf_WarnMsg( 5, "config: saving config for module <%s>",
704                          p_module->psz_name );
705
706         fprintf( file, "[%s]", p_module->psz_name );
707         if( p_module->psz_longname )
708             fprintf( file, " # %s\n\n", p_module->psz_longname );
709         else
710             fprintf( file, "\n\n" );
711
712         for( p_item = p_module->p_config;
713              p_item->i_type != MODULE_CONFIG_HINT_END;
714              p_item++ )
715         {
716             if( p_item->i_type & MODULE_CONFIG_HINT )
717                 /* ignore hints */
718                 continue;
719
720             switch( p_item->i_type )
721             {
722             case MODULE_CONFIG_ITEM_BOOL:
723             case MODULE_CONFIG_ITEM_INTEGER:
724                 if( p_item->psz_text )
725                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
726                              (p_item->i_type == MODULE_CONFIG_ITEM_BOOL) ?
727                              _("boolean") : _("integer") );
728                 fprintf( file, "%s=%i\n", p_item->psz_name,
729                          p_item->i_value );
730                 break;
731
732             case MODULE_CONFIG_ITEM_FLOAT:
733                 if( p_item->psz_text )
734                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
735                              _("float") );
736                 fprintf( file, "%s=%f\n", p_item->psz_name,
737                          (double)p_item->f_value );
738                 break;
739
740             default:
741                 if( p_item->psz_text )
742                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
743                              _("string") );
744                 fprintf( file, "%s=%s\n", p_item->psz_name,
745                          p_item->psz_value ? p_item->psz_value : "" );
746             }
747         }
748
749         fprintf( file, "\n" );
750     }
751
752
753     /*
754      * Restore old settings from the config in file
755      */
756     fputs( p_bigbuffer, file );
757     free( p_bigbuffer );
758
759     fclose( file );
760     free( psz_filename );
761     vlc_mutex_unlock( &p_main->config_lock );
762
763     return 0;
764 }
765
766 /*****************************************************************************
767  * config_LoadCmdLine: parse command line
768  *****************************************************************************
769  * Parse command line for configuration options.
770  * Now that the module_bank has been initialized, we can dynamically
771  * generate the longopts structure used by getops. We have to do it this way
772  * because we don't know (and don't want to know) in advance the configuration
773  * options used (ie. exported) by each module.
774  *****************************************************************************/
775 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
776                         boolean_t b_ignore_errors )
777 {
778     int i_cmd, i_index, i_opts, i_shortopts;
779     module_t *p_module;
780     module_config_t *p_item;
781     struct option *p_longopts;
782
783     /* Short options */
784     module_config_t *pp_shortopts[256];
785     char *psz_shortopts;
786
787     /* Reset warning level */
788     p_main->i_warning_level = 0;
789
790     /* Set default configuration and copy arguments */
791     p_main->i_argc    = *pi_argc;
792     p_main->ppsz_argv = ppsz_argv;
793
794     p_main->p_channel = NULL;
795
796 #ifdef SYS_DARWIN
797     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
798      * is the PSN - process serial number (a unique PID-ish thingie)
799      * still ok for real Darwin & when run from command line */
800     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
801                                         /* for example -psn_0_9306113 */
802     {
803         /* GDMF!... I can't do this or else the MacOSX window server will
804          * not pick up the PSN and not register the app and we crash...
805          * hence the following kludge otherwise we'll get confused w/ argv[1]
806          * being an input file name */
807 #if 0
808         ppsz_argv[ 1 ] = NULL;
809 #endif
810         *pi_argc = *pi_argc - 1;
811         pi_argc--;
812         return( 0 );
813     }
814 #endif
815
816     /*
817      * Generate the longopts and shortopts structures used by getopt_long
818      */
819
820     i_opts = 0;
821     for( p_module = p_module_bank->first;
822          p_module != NULL ;
823          p_module = p_module->next )
824     {
825         /* count the number of exported configuration options (to allocate
826          * longopts). */
827         i_opts += p_module->i_config_items;
828     }
829
830     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
831     if( p_longopts == NULL )
832     {
833         intf_ErrMsg( "config error: couldn't allocate p_longopts" );
834         return( -1 );
835     }
836
837     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
838     if( psz_shortopts == NULL )
839     {
840         intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
841         free( p_longopts );
842         return( -1 );
843     }
844
845     /* If we are requested to ignore errors, then we must work on a copy
846      * of the ppsz_argv array, otherwise getopt_long will reorder it for
847      * us, ignoring the arity of the options */
848     if( b_ignore_errors )
849     {
850         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
851         if( ppsz_argv == NULL )
852         {
853             intf_ErrMsg( "config error: couldn't duplicate ppsz_argv" );
854             free( psz_shortopts );
855             free( p_longopts );
856             return -1;
857         }
858         memcpy( ppsz_argv, p_main->ppsz_argv, *pi_argc * sizeof(char *) );
859     }
860
861     psz_shortopts[0] = 'v';
862     i_shortopts = 1;
863     for( i_index = 0; i_index < 256; i_index++ )
864     {
865         pp_shortopts[i_index] = NULL;
866     }
867
868     /* Fill the p_longopts and psz_shortopts structures */
869     i_index = 0;
870     for( p_module = p_module_bank->first ;
871          p_module != NULL ;
872          p_module = p_module->next )
873     {
874         for( p_item = p_module->p_config;
875              p_item->i_type != MODULE_CONFIG_HINT_END;
876              p_item++ )
877         {
878             /* Ignore hints */
879             if( p_item->i_type & MODULE_CONFIG_HINT )
880                 continue;
881
882             /* Add item to long options */
883             p_longopts[i_index].name = p_item->psz_name;
884             p_longopts[i_index].has_arg =
885                 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL)?
886                                                no_argument : required_argument;
887             p_longopts[i_index].flag = 0;
888             p_longopts[i_index].val = 0;
889             i_index++;
890
891             /* If item also has a short option, add it */
892             if( p_item->i_short )
893             {
894                 pp_shortopts[(int)p_item->i_short] = p_item;
895                 psz_shortopts[i_shortopts] = p_item->i_short;
896                 i_shortopts++;
897                 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
898                 {
899                     psz_shortopts[i_shortopts] = ':';
900                     i_shortopts++;
901                 }
902             }
903         }
904     }
905
906     /* Close the longopts and shortopts structures */
907     memset( &p_longopts[i_index], 0, sizeof(struct option) );
908     psz_shortopts[i_shortopts] = '\0';
909
910     /*
911      * Parse the command line options
912      */
913     opterr = 0;
914     optind = 1;
915     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
916                                   p_longopts, &i_index ) ) != EOF )
917     {
918         /* A long option has been recognized */
919         if( i_cmd == 0 )
920         {
921             module_config_t *p_conf;
922
923             /* Store the configuration option */
924             p_conf = config_FindConfig( p_longopts[i_index].name );
925
926             switch( p_conf->i_type )
927             {
928             case MODULE_CONFIG_ITEM_STRING:
929             case MODULE_CONFIG_ITEM_FILE:
930             case MODULE_CONFIG_ITEM_MODULE:
931                 config_PutPszVariable( p_longopts[i_index].name, optarg );
932                 break;
933             case MODULE_CONFIG_ITEM_INTEGER:
934                 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
935                 break;
936             case MODULE_CONFIG_ITEM_FLOAT:
937                 config_PutFloatVariable( p_longopts[i_index].name,
938                                          (float)atof(optarg) );
939                 break;
940             case MODULE_CONFIG_ITEM_BOOL:
941                 config_PutIntVariable( p_longopts[i_index].name, 1 );
942                 break;
943             }
944
945             continue;
946         }
947
948         /* A short option has been recognized */
949         if( pp_shortopts[i_cmd] != NULL )
950         {
951             switch( pp_shortopts[i_cmd]->i_type )
952             {
953             case MODULE_CONFIG_ITEM_STRING:
954             case MODULE_CONFIG_ITEM_FILE:
955             case MODULE_CONFIG_ITEM_MODULE:
956                 config_PutPszVariable( pp_shortopts[i_cmd]->psz_name, optarg );
957                 break;
958             case MODULE_CONFIG_ITEM_INTEGER:
959                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
960                                        atoi(optarg));
961                 break;
962             case MODULE_CONFIG_ITEM_BOOL:
963                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
964                 break;
965             }
966
967             continue;
968         }
969
970         /* Either it's a -v or it's an unknown short option */
971         if( i_cmd == 'v' )
972         {
973             p_main->i_warning_level++;
974             continue;
975         }
976
977         /* Internal error: unknown option */
978         if( !b_ignore_errors )
979         {
980             intf_ErrMsg( "config error: unknown option `%s'",
981                          ppsz_argv[optind-1] );
982             intf_Msg( "Try `%s --help' for more information.\n",
983                       p_main->psz_arg0 );
984
985             free( p_longopts );
986             free( psz_shortopts );
987             if( b_ignore_errors ) free( ppsz_argv );
988             return( -1 );
989         }
990     }
991
992     free( p_longopts );
993     free( psz_shortopts );
994     if( b_ignore_errors ) free( ppsz_argv );
995
996     /* Update the warning level */
997     p_main->i_warning_level += config_GetIntVariable( "warning" );
998     p_main->i_warning_level = ( p_main->i_warning_level < 0 ) ? 0 :
999         p_main->i_warning_level;
1000     config_PutIntVariable( "warning", p_main->i_warning_level );
1001
1002     return( 0 );
1003 }
1004
1005 /*****************************************************************************
1006  * config_GetHomeDir: find the user's home directory.
1007  *****************************************************************************
1008  * This function will try by different ways to find the user's home path.
1009  * Note that this function is not reentrant, it should be called only once
1010  * at the beginning of main where the result will be stored for later use.
1011  *****************************************************************************/
1012 char *config_GetHomeDir( void )
1013 {
1014     char *p_tmp, *p_homedir = NULL;
1015
1016 #if defined(HAVE_GETPWUID)
1017     struct passwd *p_pw = NULL;
1018 #endif
1019
1020 #ifdef WIN32
1021     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1022                                                LPTSTR );
1023 #   define CSIDL_FLAG_CREATE 0x8000
1024 #   define CSIDL_APPDATA 0x1A
1025 #   define SHGFP_TYPE_CURRENT 0
1026
1027     HINSTANCE shfolder_dll;
1028     SHGETFOLDERPATH SHGetFolderPath ;
1029
1030     /* load the shell32 dll to retreive SHGetFolderPath */
1031     if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1032     {
1033         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1034                                                   "SHGetFolderPathA" );
1035         if ( SHGetFolderPath != NULL )
1036         {
1037             p_homedir = (char *)malloc( MAX_PATH );
1038             if( !p_homedir )
1039             {
1040                 intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1041                 return NULL;
1042             }
1043
1044             /* get the "Application Data" folder for the current user */
1045             if( S_OK == SHGetFolderPath( NULL,
1046                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1047                                          NULL, SHGFP_TYPE_CURRENT,
1048                                          p_homedir ) )
1049             {
1050                 FreeLibrary( shfolder_dll );
1051                 return p_homedir;
1052             }
1053             free( p_homedir );
1054         }
1055         FreeLibrary( shfolder_dll );
1056     }
1057 #endif
1058
1059 #if defined(HAVE_GETPWUID)
1060     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1061 #endif
1062     {
1063         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1064         {
1065             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1066             {
1067                 p_homedir = strdup( "/tmp" );
1068             }
1069             else p_homedir = strdup( p_tmp );
1070
1071             intf_ErrMsg( "config error: unable to get home directory, "
1072                          "using %s instead", p_homedir );
1073
1074         }
1075         else p_homedir = strdup( p_tmp );
1076     }
1077 #if defined(HAVE_GETPWUID)
1078     else
1079     {
1080         p_homedir = strdup( p_pw->pw_dir );
1081     }
1082 #endif
1083
1084     return p_homedir;
1085 }