]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
0aec10be6e7c615a0ff62388284cb0d07b2f1c9d
[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.22 2002/05/03 20:49:30 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     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: couldn't open config file %s for reading (%s)",
415                          psz_filename, strerror(errno) );
416         free( psz_filename );
417         vlc_mutex_unlock( &p_main->config_lock );
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] == (char)0) ) continue;
454
455             /* get rid of line feed */
456             if( line[strlen(line)-1] == '\n' )
457                 line[strlen(line)-1] = (char)0;
458
459             /* look for option name */
460             psz_option_name = line;
461             psz_option_value = NULL;
462             p_index = strchr( line, '=' );
463             if( !p_index ) break; /* this ain't an option!!! */
464
465             *p_index = (char)0;
466             psz_option_value = p_index + 1;
467
468             /* try to match this option with one of the module's options */
469             for( p_item = p_module->p_config;
470                  p_item->i_type != MODULE_CONFIG_HINT_END;
471                  p_item++ )
472             {
473                 if( p_item->i_type & MODULE_CONFIG_HINT )
474                     /* ignore hints */
475                     continue;
476
477                 if( !strcmp( p_item->psz_name, psz_option_name ) )
478                 {
479                     /* We found it */
480                     switch( p_item->i_type )
481                     {
482                     case MODULE_CONFIG_ITEM_BOOL:
483                     case MODULE_CONFIG_ITEM_INTEGER:
484                         if( !*psz_option_value )
485                             break;                    /* ignore empty option */
486                         p_item->i_value = atoi( psz_option_value);
487                         intf_WarnMsg( 7, "config: found <%s> option %s=%i",
488                                          p_module->psz_name,
489                                          p_item->psz_name, p_item->i_value );
490                         break;
491
492                     case MODULE_CONFIG_ITEM_FLOAT:
493                         if( !*psz_option_value )
494                             break;                    /* ignore empty option */
495                         p_item->f_value = (float)atof( psz_option_value);
496                         intf_WarnMsg( 7, "config: found <%s> option %s=%f",
497                                          p_module->psz_name, p_item->psz_name,
498                                          (double)p_item->f_value );
499                         break;
500
501                     default:
502                         vlc_mutex_lock( p_item->p_lock );
503
504                         /* free old string */
505                         if( p_item->psz_value )
506                             free( p_item->psz_value );
507
508                         p_item->psz_value = *psz_option_value ?
509                             strdup( psz_option_value ) : NULL;
510
511                         vlc_mutex_unlock( p_item->p_lock );
512
513                         intf_WarnMsg( 7, "config: found <%s> option %s=%s",
514                                          p_module->psz_name,
515                                          p_item->psz_name,
516                                          p_item->psz_value != NULL ?
517                                            p_item->psz_value : "(NULL)" );
518                         break;
519                     }
520                 }
521             }
522         }
523
524     }
525     
526     fclose( file );
527     free( psz_filename );
528
529     vlc_mutex_unlock( &p_main->config_lock );
530
531     return 0;
532 }
533
534 /*****************************************************************************
535  * config_SaveConfigFile: Save a module's config options.
536  *****************************************************************************
537  * This will save the specified module's config options to the config file.
538  * If psz_module_name is NULL then we save all the modules config options.
539  * It's no use to save the config options that kept their default values, so
540  * we'll try to be a bit clever here.
541  *
542  * When we save we mustn't delete the config options of the modules that
543  * haven't been loaded. So we cannot just create a new config file with the
544  * config structures we've got in memory. 
545  * I don't really know how to deal with this nicely, so I will use a completly
546  * dumb method ;-)
547  * I will load the config file in memory, but skipping all the sections of the
548  * modules we want to save. Then I will create a brand new file, dump the file
549  * loaded in memory and then append the sections of the modules we want to
550  * save.
551  * Really stupid no ?
552  *****************************************************************************/
553 int config_SaveConfigFile( const char *psz_module_name )
554 {
555     module_t *p_module;
556     module_config_t *p_item;
557     FILE *file;
558     char p_line[1024], *p_index2;
559     int i_sizebuf = 0;
560     char *p_bigbuffer, *p_index;
561     boolean_t b_backup;
562     char *psz_filename, *psz_homedir;
563
564     /* Acquire config file lock */
565     vlc_mutex_lock( &p_main->config_lock );
566
567     psz_homedir = p_main->psz_homedir;
568     if( !psz_homedir )
569     {
570         intf_ErrMsg( "config error: p_main->psz_homedir is null" );
571         vlc_mutex_unlock( &p_main->config_lock );
572         return -1;
573     }
574     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
575                                    strlen(psz_homedir) + 1 );
576     if( !psz_filename )
577     {
578         intf_ErrMsg( "config error: couldn't malloc psz_filename" );
579         vlc_mutex_unlock( &p_main->config_lock );
580         return -1;
581     }
582     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
583
584 #ifndef WIN32
585     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
586 #else
587     if( mkdir( psz_filename ) && errno != EEXIST )
588 #endif
589     {
590         intf_ErrMsg( "config error: couldn't create %s (%s)",
591                      psz_filename, strerror(errno) );
592     }
593
594     strcat( psz_filename, "/" CONFIG_FILE );
595
596
597     intf_WarnMsg( 5, "config: opening config file %s", psz_filename );
598
599     file = fopen( psz_filename, "rt" );
600     if( !file )
601     {
602         intf_WarnMsg( 1, "config: couldn't open config file %s for reading (%s)",
603                          psz_filename, strerror(errno) );
604     }
605     else
606     {
607         /* look for file size */
608         fseek( file, 0, SEEK_END );
609         i_sizebuf = ftell( file );
610         rewind( file );
611     }
612
613     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
614     if( !p_bigbuffer )
615     {
616         intf_ErrMsg( "config error: couldn't malloc bigbuffer" );
617         if( file ) fclose( file );
618         free( psz_filename );
619         vlc_mutex_unlock( &p_main->config_lock );
620         return -1;
621     }
622     p_bigbuffer[0] = 0;
623
624     /* backup file into memory, we only need to backup the sections we won't
625      * save later on */
626     b_backup = 0;
627     while( file && fgets( p_line, 1024, file ) )
628     {
629         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
630         {
631             /* we found a section, check if we need to do a backup */
632             for( p_module = p_module_bank->first; p_module != NULL;
633                  p_module = p_module->next )
634             {
635                 if( ((p_index2 - &p_line[1]) == strlen(p_module->psz_name) ) &&
636                     !memcmp( &p_line[1], p_module->psz_name,
637                              strlen(p_module->psz_name) ) )
638                 {
639                     if( !psz_module_name )
640                         break;
641                     else if( !strcmp( psz_module_name, p_module->psz_name ) )
642                         break;
643                 }
644             }
645
646             if( !p_module )
647             {
648                 /* we don't have this section in our list so we need to back
649                  * it up */
650                 *p_index2 = 0;
651                 intf_WarnMsg( 5, "config: backing up config for "
652                                  "unknown module <%s>", &p_line[1] );
653                 *p_index2 = ']';
654
655                 b_backup = 1;
656             }
657             else
658             {
659                 b_backup = 0;
660             }
661         }
662
663         /* save line if requested and line is valid (doesn't begin with a
664          * space, tab, or eol) */
665         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
666             && (p_line[0] != '\t') )
667         {
668             strcpy( p_index, p_line );
669             p_index += strlen( p_line );
670         }
671     }
672     if( file ) fclose( file );
673
674
675     /*
676      * Save module config in file
677      */
678
679     file = fopen( psz_filename, "wt" );
680     if( !file )
681     {
682         intf_WarnMsg( 1, "config: couldn't open config file %s for writing",
683                          psz_filename );
684         free( psz_filename );
685         vlc_mutex_unlock( &p_main->config_lock );
686         return -1;
687     }
688
689     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
690
691     /* Look for the selected module, if NULL then save everything */
692     for( p_module = p_module_bank->first ; p_module != NULL ;
693          p_module = p_module->next )
694     {
695
696         if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
697             continue;
698
699         if( !p_module->i_config_items )
700             continue;
701
702         intf_WarnMsg( 5, "config: saving config for module <%s>",
703                          p_module->psz_name );
704
705         fprintf( file, "[%s]\n", p_module->psz_name );
706
707         if( p_module->psz_longname )
708             fprintf( file, "###\n###  %s\n###\n", p_module->psz_longname );
709
710         for( p_item = p_module->p_config;
711              p_item->i_type != MODULE_CONFIG_HINT_END;
712              p_item++ )
713         {
714             if( p_item->i_type & MODULE_CONFIG_HINT )
715                 /* ignore hints */
716                 continue;
717
718             switch( p_item->i_type )
719             {
720             case MODULE_CONFIG_ITEM_BOOL:
721             case MODULE_CONFIG_ITEM_INTEGER:
722                 if( p_item->psz_text )
723                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
724                              (p_item->i_type == MODULE_CONFIG_ITEM_BOOL) ?
725                              _("boolean") : _("integer") );
726                 fprintf( file, "%s=%i\n", p_item->psz_name,
727                          p_item->i_value );
728                 break;
729
730             case MODULE_CONFIG_ITEM_FLOAT:
731                 if( p_item->psz_text )
732                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
733                              _("float") );
734                 fprintf( file, "%s=%f\n", p_item->psz_name,
735                          (double)p_item->f_value );
736                 break;
737
738             default:
739                 if( p_item->psz_text )
740                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
741                              _("string") );
742                 fprintf( file, "%s=%s\n", p_item->psz_name,
743                          p_item->psz_value ? p_item->psz_value : "" );
744             }
745         }
746
747         fprintf( file, "\n" );
748     }
749
750
751     /*
752      * Restore old settings from the config in file
753      */
754     fputs( p_bigbuffer, file );
755     free( p_bigbuffer );
756
757     fclose( file );
758     free( psz_filename );
759     vlc_mutex_unlock( &p_main->config_lock );
760
761     return 0;
762 }
763
764 /*****************************************************************************
765  * config_LoadCmdLine: parse command line
766  *****************************************************************************
767  * Parse command line for configuration options.
768  * Now that the module_bank has been initialized, we can dynamically
769  * generate the longopts structure used by getops. We have to do it this way
770  * because we don't know (and don't want to know) in advance the configuration
771  * options used (ie. exported) by each module.
772  *****************************************************************************/
773 int config_LoadCmdLine( int *pi_argc, char *ppsz_argv[],
774                         boolean_t b_ignore_errors )
775 {
776     int i_cmd, i_index, i_opts, i_shortopts;
777     module_t *p_module;
778     module_config_t *p_item;
779     struct option *p_longopts;
780
781     /* Short options */
782     module_config_t *pp_shortopts[256];
783     char *psz_shortopts;
784
785     /* reset warning level */
786     p_main->i_warning_level = 0;
787
788     /* Set default configuration and copy arguments */
789     p_main->i_argc    = *pi_argc;
790     p_main->ppsz_argv = ppsz_argv;
791
792     p_main->p_channel = NULL;
793
794 #ifdef SYS_DARWIN
795     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
796      * is the PSN - process serial number (a unique PID-ish thingie)
797      * still ok for real Darwin & when run from command line */
798     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
799                                         /* for example -psn_0_9306113 */
800     {
801         /* GDMF!... I can't do this or else the MacOSX window server will
802          * not pick up the PSN and not register the app and we crash...
803          * hence the following kludge otherwise we'll get confused w/ argv[1]
804          * being an input file name */
805 #if 0
806         ppsz_argv[ 1 ] = NULL;
807 #endif
808         *pi_argc = *pi_argc - 1;
809         pi_argc--;
810         return( 0 );
811     }
812 #endif
813
814     /*
815      * Generate the longopts and shortopts structure used by getopt_long
816      */
817
818     i_opts = 0;
819     for( p_module = p_module_bank->first;
820          p_module != NULL ;
821          p_module = p_module->next )
822     {
823         /* count the number of exported configuration options (to allocate
824          * longopts). */
825         i_opts += p_module->i_config_items;
826     }
827
828     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
829     if( p_longopts == NULL )
830     {
831         intf_ErrMsg( "config error: couldn't allocate p_longopts" );
832         return( -1 );
833     }
834
835     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
836     if( psz_shortopts == NULL )
837     {
838         intf_ErrMsg( "config error: couldn't allocate psz_shortopts" );
839         free( p_longopts );
840         return( -1 );
841     }
842
843     psz_shortopts[0] = 'v';
844     i_shortopts = 1;
845     for( i_index = 0; i_index < 256; i_index++ )
846     {
847         pp_shortopts[i_index] = NULL;
848     }
849
850     /* Fill the longopts structure */
851     i_index = 0;
852     for( p_module = p_module_bank->first ;
853          p_module != NULL ;
854          p_module = p_module->next )
855     {
856         for( p_item = p_module->p_config;
857              p_item->i_type != MODULE_CONFIG_HINT_END;
858              p_item++ )
859         {
860             if( p_item->i_type & MODULE_CONFIG_HINT )
861                 /* ignore hints */
862                 continue;
863             p_longopts[i_index].name = p_item->psz_name;
864             p_longopts[i_index].has_arg =
865                 (p_item->i_type == MODULE_CONFIG_ITEM_BOOL)?
866                                                no_argument : required_argument;
867             p_longopts[i_index].flag = 0;
868             p_longopts[i_index].val = 0;
869             if( p_item->i_short )
870             {
871                 pp_shortopts[(int)p_item->i_short] = p_item;
872                 psz_shortopts[i_shortopts] = p_item->i_short;
873                 i_shortopts++;
874                 if( p_item->i_type != MODULE_CONFIG_ITEM_BOOL )
875                 {
876                     psz_shortopts[i_shortopts] = ':';
877                     i_shortopts++;
878                 }
879             }
880             i_index++;
881         }
882     }
883
884     /* Close the longopts and shortopts structures */
885     memset( &p_longopts[i_index], 0, sizeof(struct option) );
886     psz_shortopts[i_shortopts] = '\0';
887
888     /*
889      * Parse the command line options
890      */
891     opterr = 0;
892     optind = 1;
893     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
894                                   p_longopts, &i_index ) ) != EOF )
895     {
896         /* A long option has been recognized */
897         if( i_cmd == 0 )
898         {
899             module_config_t *p_conf;
900
901             /* Store the configuration option */
902             p_conf = config_FindConfig( p_longopts[i_index].name );
903
904             switch( p_conf->i_type )
905             {
906             case MODULE_CONFIG_ITEM_STRING:
907             case MODULE_CONFIG_ITEM_FILE:
908             case MODULE_CONFIG_ITEM_MODULE:
909                 config_PutPszVariable( p_longopts[i_index].name, optarg );
910                 break;
911             case MODULE_CONFIG_ITEM_INTEGER:
912                 config_PutIntVariable( p_longopts[i_index].name, atoi(optarg));
913                 break;
914             case MODULE_CONFIG_ITEM_FLOAT:
915                 config_PutFloatVariable( p_longopts[i_index].name,
916                                          (float)atof(optarg) );
917                 break;
918             case MODULE_CONFIG_ITEM_BOOL:
919                 config_PutIntVariable( p_longopts[i_index].name, 1 );
920                 break;
921             }
922
923             continue;
924         }
925
926         /* A short option has been recognized */
927         if( pp_shortopts[i_cmd] != NULL )
928         {
929             switch( pp_shortopts[i_cmd]->i_type )
930             {
931             case MODULE_CONFIG_ITEM_STRING:
932             case MODULE_CONFIG_ITEM_FILE:
933             case MODULE_CONFIG_ITEM_MODULE:
934                 config_PutPszVariable( pp_shortopts[i_cmd]->psz_name, optarg );
935                 break;
936             case MODULE_CONFIG_ITEM_INTEGER:
937                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name,
938                                        atoi(optarg));
939                 break;
940             case MODULE_CONFIG_ITEM_BOOL:
941                 config_PutIntVariable( pp_shortopts[i_cmd]->psz_name, 1 );
942                 break;
943             }
944
945             continue;
946         }
947
948         /* Either it's a -v or it's an unknown short option */
949         if( i_cmd == 'v' )
950         {
951             p_main->i_warning_level++;
952             continue;
953         }
954
955         /* Internal error: unknown option */
956         if( !b_ignore_errors )
957         {
958             intf_ErrMsg( "config error: unknown option `%s'",
959                          ppsz_argv[optind-1] );
960             intf_Msg( "Try `%s --help' for more information.\n",
961                       p_main->psz_arg0 );
962
963             free( p_longopts );
964             free( psz_shortopts );
965             return( -1 );
966         }
967     }
968
969     free( p_longopts );
970     free( psz_shortopts );
971
972     /* Update the warning level */
973     p_main->i_warning_level += config_GetIntVariable( "warning" );
974     p_main->i_warning_level = ( p_main->i_warning_level < 0 ) ? 0 :
975         p_main->i_warning_level;
976     config_PutIntVariable( "warning", p_main->i_warning_level );
977
978     return( 0 );
979 }
980
981 /*****************************************************************************
982  * config_GetHomeDir: find the user's home directory.
983  *****************************************************************************
984  * This function will try by different ways to find the user's home path.
985  * Note that this function is not reentrant, it should be called only once
986  * at the beginning of main where the result will be stored for later use.
987  *****************************************************************************/
988 char *config_GetHomeDir( void )
989 {
990     char *p_tmp, *p_homedir = NULL;
991
992 #if defined(HAVE_GETPWUID)
993     struct passwd *p_pw = NULL;
994 #endif
995
996 #ifdef WIN32
997     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
998                                                LPTSTR );
999 #   define CSIDL_FLAG_CREATE 0x8000
1000 #   define CSIDL_APPDATA 0x1A
1001 #   define SHGFP_TYPE_CURRENT 0
1002
1003     HINSTANCE shell32_dll;
1004     SHGETFOLDERPATH SHGetFolderPath ;
1005
1006     /* load the shell32 dll to retreive SHGetFolderPath */
1007     if( ( shell32_dll = LoadLibrary("shell32.dll") ) != NULL )
1008     {
1009         SHGetFolderPath = (void *)GetProcAddress( shell32_dll,
1010                                                   "SHGetFolderPathA" );
1011         if ( SHGetFolderPath != NULL )
1012         {
1013             p_homedir = (char *)malloc( MAX_PATH );
1014             if( !p_homedir )
1015             {
1016                 intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1017                 return NULL;
1018             }
1019
1020             /* get the "Application Data" folder for the current user */
1021             if( S_OK == SHGetFolderPath( NULL,
1022                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1023                                          NULL, SHGFP_TYPE_CURRENT,
1024                                          p_homedir ) )
1025             {
1026                 FreeLibrary( shell32_dll );
1027                 return p_homedir;
1028             }
1029             free( p_homedir );
1030         }
1031         FreeLibrary( shell32_dll );
1032     }
1033 #endif
1034
1035 #if defined(HAVE_GETPWUID)
1036     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1037 #endif
1038     {
1039         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1040         {
1041             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1042             {
1043                 p_homedir = strdup( "/tmp" );
1044             }
1045             else p_homedir = strdup( p_tmp );
1046
1047             intf_ErrMsg( "config error: unable to get home directory, "
1048                          "using %s instead", p_homedir );
1049
1050         }
1051         else p_homedir = strdup( p_tmp );
1052     }
1053 #if defined(HAVE_GETPWUID)
1054     else
1055     {
1056         p_homedir = strdup( p_pw->pw_dir );
1057     }
1058 #endif
1059
1060     return p_homedir;
1061 }