]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
* src/misc/configuration.c: what the f??k, configuration variable names
[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.54 2003/04/07 11:48:51 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 <vlc/vlc.h>
25
26 #include <stdio.h>                                              /* sprintf() */
27 #include <stdlib.h>                                      /* free(), strtol() */
28 #include <string.h>                                              /* strdup() */
29 #ifdef HAVE_ERRNO_H
30 #   include <errno.h>                                               /* errno */
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #    include <unistd.h>                                          /* getuid() */
35 #endif
36
37 #ifdef HAVE_GETOPT_LONG
38 #   ifdef HAVE_GETOPT_H
39 #       include <getopt.h>                                       /* getopt() */
40 #   endif
41 #else
42 #   include "../extras/getopt.h"
43 #endif
44
45 #if defined(HAVE_GETPWUID)
46 #   include <pwd.h>                                            /* getpwuid() */
47 #endif
48
49 #if defined( HAVE_SYS_STAT_H )
50 #   include <sys/stat.h>
51 #endif
52 #if defined( HAVE_SYS_TYPES_H )
53 #   include <sys/types.h>
54 #endif
55 #if defined( WIN32 ) && !defined( UNDER_CE )
56 #   include <direct.h>
57 #endif
58
59 /*****************************************************************************
60  * config_GetInt: get the value of an int variable
61  *****************************************************************************
62  * This function is used to get the value of variables which are internally
63  * represented by an integer (CONFIG_ITEM_INTEGER and
64  * CONFIG_ITEM_BOOL).
65  *****************************************************************************/
66 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
67 {
68     module_config_t *p_config;
69
70     p_config = config_FindConfig( p_this, psz_name );
71
72     /* sanity checks */
73     if( !p_config )
74     {
75         msg_Err( p_this, "option %s does not exist", psz_name );
76         return -1;
77     }
78     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
79         (p_config->i_type!=CONFIG_ITEM_BOOL) )
80     {
81         msg_Err( p_this, "option %s does not refer to an int", psz_name );
82         return -1;
83     }
84
85     return p_config->i_value;
86 }
87
88 /*****************************************************************************
89  * config_GetFloat: get the value of a float variable
90  *****************************************************************************
91  * This function is used to get the value of variables which are internally
92  * represented by a float (CONFIG_ITEM_FLOAT).
93  *****************************************************************************/
94 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
95 {
96     module_config_t *p_config;
97
98     p_config = config_FindConfig( p_this, psz_name );
99
100     /* sanity checks */
101     if( !p_config )
102     {
103         msg_Err( p_this, "option %s does not exist", psz_name );
104         return -1;
105     }
106     if( p_config->i_type != CONFIG_ITEM_FLOAT )
107     {
108         msg_Err( p_this, "option %s does not refer to a float", psz_name );
109         return -1;
110     }
111
112     return p_config->f_value;
113 }
114
115 /*****************************************************************************
116  * config_GetPsz: get the string value of a string variable
117  *****************************************************************************
118  * This function is used to get the value of variables which are internally
119  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
120  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
121  *
122  * Important note: remember to free() the returned char* because it's a
123  *   duplicate of the actual value. It isn't safe to return a pointer to the
124  *   actual value as it can be modified at any time.
125  *****************************************************************************/
126 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
127 {
128     module_config_t *p_config;
129     char *psz_value = NULL;
130
131     p_config = config_FindConfig( p_this, psz_name );
132
133     /* sanity checks */
134     if( !p_config )
135     {
136         msg_Err( p_this, "option %s does not exist", psz_name );
137         return NULL;
138     }
139     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
140         (p_config->i_type!=CONFIG_ITEM_FILE) &&
141         (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
142         (p_config->i_type!=CONFIG_ITEM_MODULE) )
143     {
144         msg_Err( p_this, "option %s does not refer to a string", psz_name );
145         return NULL;
146     }
147
148     /* return a copy of the string */
149     vlc_mutex_lock( p_config->p_lock );
150     if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
151     vlc_mutex_unlock( p_config->p_lock );
152
153     return psz_value;
154 }
155
156 /*****************************************************************************
157  * config_PutPsz: set the string value of a string variable
158  *****************************************************************************
159  * This function is used to set the value of variables which are internally
160  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
161  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
162  *****************************************************************************/
163 void __config_PutPsz( vlc_object_t *p_this,
164                       const char *psz_name, const char *psz_value )
165 {
166     module_config_t *p_config;
167
168     p_config = config_FindConfig( p_this, psz_name );
169
170     /* sanity checks */
171     if( !p_config )
172     {
173         msg_Warn( p_this, "option %s does not exist", psz_name );
174         return;
175     }
176     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
177         (p_config->i_type!=CONFIG_ITEM_FILE) &&
178         (p_config->i_type!=CONFIG_ITEM_DIRECTORY) &&
179         (p_config->i_type!=CONFIG_ITEM_MODULE) )
180     {
181         msg_Err( p_this, "option %s does not refer to a string", psz_name );
182         return;
183     }
184
185     vlc_mutex_lock( p_config->p_lock );
186
187     /* free old string */
188     if( p_config->psz_value ) free( p_config->psz_value );
189
190     if( psz_value ) p_config->psz_value = strdup( psz_value );
191     else p_config->psz_value = NULL;
192
193     vlc_mutex_unlock( p_config->p_lock );
194
195     if( p_config->pf_callback )
196     {
197         p_config->pf_callback( p_this );
198     }
199 }
200
201 /*****************************************************************************
202  * config_PutInt: set the integer value of an int variable
203  *****************************************************************************
204  * This function is used to set the value of variables which are internally
205  * represented by an integer (CONFIG_ITEM_INTEGER and
206  * CONFIG_ITEM_BOOL).
207  *****************************************************************************/
208 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
209 {
210     module_config_t *p_config;
211
212     p_config = config_FindConfig( p_this, psz_name );
213
214     /* sanity checks */
215     if( !p_config )
216     {
217         msg_Warn( p_this, "option %s does not exist", psz_name );
218         return;
219     }
220     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
221         (p_config->i_type!=CONFIG_ITEM_BOOL) )
222     {
223         msg_Err( p_this, "option %s does not refer to an int", psz_name );
224         return;
225     }
226
227     /* if i_min == i_max == 0, then do not use them */
228     if ((p_config->i_min == 0) && (p_config->i_max == 0))
229     {
230         p_config->i_value = i_value;
231     }
232     else if (i_value < p_config->i_min)
233     {
234         p_config->i_value = p_config->i_min;
235     }
236     else if (i_value > p_config->i_max)
237     {
238         p_config->i_value = p_config->i_max;
239     }
240     else
241     {
242         p_config->i_value = i_value;
243     }
244
245     if( p_config->pf_callback )
246     {
247         p_config->pf_callback( p_this );
248     }
249 }
250
251 /*****************************************************************************
252  * config_PutFloat: set the value of a float variable
253  *****************************************************************************
254  * This function is used to set the value of variables which are internally
255  * represented by a float (CONFIG_ITEM_FLOAT).
256  *****************************************************************************/
257 void __config_PutFloat( vlc_object_t *p_this,
258                         const char *psz_name, float f_value )
259 {
260     module_config_t *p_config;
261
262     p_config = config_FindConfig( p_this, psz_name );
263
264     /* sanity checks */
265     if( !p_config )
266     {
267         msg_Warn( p_this, "option %s does not exist", psz_name );
268         return;
269     }
270     if( p_config->i_type != CONFIG_ITEM_FLOAT )
271     {
272         msg_Err( p_this, "option %s does not refer to a float", psz_name );
273         return;
274     }
275
276     /* if f_min == f_max == 0, then do not use them */
277     if ((p_config->f_min == 0) && (p_config->f_max == 0))
278     {
279         p_config->f_value = f_value;
280     }
281     else if (f_value < p_config->f_min)
282     {
283         p_config->f_value = p_config->f_min;
284     }
285     else if (f_value > p_config->f_max)
286     {
287         p_config->f_value = p_config->f_max;
288     }
289     else
290     {
291         p_config->f_value = f_value;
292     }
293
294     if( p_config->pf_callback )
295     {
296         p_config->pf_callback( p_this );
297     }
298 }
299
300 /*****************************************************************************
301  * config_FindConfig: find the config structure associated with an option.
302  *****************************************************************************
303  * FIXME: This function really needs to be optimized.
304  * FIXME: And now even more.
305  *****************************************************************************/
306 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
307 {
308     vlc_list_t *p_list;
309     module_t *p_parser;
310     module_config_t *p_item;
311     int i_index;
312
313     if( !psz_name ) return NULL;
314
315     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
316
317     for( i_index = 0; i_index < p_list->i_count; i_index++ )
318     {
319         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
320
321         if( !p_parser->i_config_items )
322             continue;
323
324         for( p_item = p_parser->p_config;
325              p_item->i_type != CONFIG_HINT_END;
326              p_item++ )
327         {
328             if( p_item->i_type & CONFIG_HINT )
329                 /* ignore hints */
330                 continue;
331             if( !strcmp( psz_name, p_item->psz_name ) )
332             {
333                 vlc_list_release( p_list );
334                 return p_item;
335             }
336         }
337     }
338
339     vlc_list_release( p_list );
340
341     return NULL;
342 }
343
344 /*****************************************************************************
345  * config_Duplicate: creates a duplicate of a module's configuration data.
346  *****************************************************************************
347  * Unfortunatly we cannot work directly with the module's config data as
348  * this module might be unloaded from memory at any time (remember HideModule).
349  * This is why we need to create an exact copy of the config data.
350  *****************************************************************************/
351 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
352 {
353     int i, j, i_lines = 1;
354     module_config_t *p_item;
355
356     /* Calculate the structure length */
357     p_module->i_config_items = 0;
358     p_module->i_bool_items = 0;
359
360     for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
361     {
362         i_lines++;
363
364         if( p_item->i_type & CONFIG_ITEM )
365         {
366             p_module->i_config_items++;
367         }
368
369         if( p_item->i_type == CONFIG_ITEM_BOOL )
370         {
371             p_module->i_bool_items++;
372         }
373     }
374
375     /* Allocate memory */
376     p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
377                                                      * i_lines );
378     if( p_module->p_config == NULL )
379     {
380         msg_Err( p_module, "config error: can't duplicate p_config" );
381         return;
382     }
383
384     /* Do the duplication job */
385     for( i = 0; i < i_lines ; i++ )
386     {
387         p_module->p_config[i].i_type = p_orig[i].i_type;
388         p_module->p_config[i].i_short = p_orig[i].i_short;
389         p_module->p_config[i].i_value = p_orig[i].i_value;
390         p_module->p_config[i].i_min = p_orig[i].i_min;
391         p_module->p_config[i].i_max = p_orig[i].i_max;
392         p_module->p_config[i].f_value = p_orig[i].f_value;
393         p_module->p_config[i].f_min = p_orig[i].f_min;
394         p_module->p_config[i].f_max = p_orig[i].f_max;
395         p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
396         p_module->p_config[i].b_advanced = p_orig[i].b_advanced;
397
398         p_module->p_config[i].psz_type = p_orig[i].psz_type ?
399                                    strdup( p_orig[i].psz_type ) : NULL;
400         p_module->p_config[i].psz_name = p_orig[i].psz_name ?
401                                    strdup( p_orig[i].psz_name ) : NULL;
402         p_module->p_config[i].psz_text = p_orig[i].psz_text ?
403                                    strdup( _(p_orig[i].psz_text) ) : NULL;
404         p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
405                                    strdup( _(p_orig[i].psz_longtext) ) : NULL;
406         p_module->p_config[i].psz_value = p_orig[i].psz_value ?
407                                    strdup( p_orig[i].psz_value ) : NULL;
408
409         p_module->p_config[i].p_lock = &p_module->object_lock;
410
411         /* duplicate the string list */
412         p_module->p_config[i].ppsz_list = NULL;
413         if( p_orig[i].ppsz_list )
414         {
415             for( j = 0; p_orig[i].ppsz_list[j]; j++ );
416             p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
417             if( p_module->p_config[i].ppsz_list )
418             {
419                 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
420                     p_module->p_config[i].ppsz_list[j] =
421                         strdup( p_orig[i].ppsz_list[j] );
422             }
423             p_module->p_config[i].ppsz_list[j] = NULL;
424         }
425
426         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
427     }
428 }
429
430 /*****************************************************************************
431  * config_Free: frees a duplicated module's configuration data.
432  *****************************************************************************
433  * This function frees all the data duplicated by config_Duplicate.
434  *****************************************************************************/
435 void config_Free( module_t *p_module )
436 {
437     module_config_t *p_item = p_module->p_config;
438     int i;
439
440     if( p_item == NULL )
441     {
442         return;
443     }
444
445     for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
446     {
447         if( p_item->psz_type )
448             free( p_item->psz_type );
449
450         if( p_item->psz_name )
451             free( p_item->psz_name );
452
453         if( p_item->psz_text )
454             free( p_item->psz_text );
455
456         if( p_item->psz_longtext )
457             free( p_item->psz_longtext );
458
459         if( p_item->psz_value )
460             free( p_item->psz_value );
461
462         if( p_item->ppsz_list )
463         {
464             for( i = 0; p_item->ppsz_list[i]; i++ )
465                 free(p_item->ppsz_list[i]);
466             free( p_item->ppsz_list );
467         }
468     }
469
470     free( p_module->p_config );
471     p_module->p_config = NULL;
472 }
473
474 /*****************************************************************************
475  * config_SetCallbacks: sets callback functions in the duplicate p_config.
476  *****************************************************************************
477  * Unfortunatly we cannot work directly with the module's config data as
478  * this module might be unloaded from memory at any time (remember HideModule).
479  * This is why we need to duplicate callbacks each time we reload the module.
480  *****************************************************************************/
481 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
482 {
483     while( p_new->i_type != CONFIG_HINT_END )
484     {
485         p_new->pf_callback = p_orig->pf_callback;
486         p_new++;
487         p_orig++;
488     }
489 }
490
491 /*****************************************************************************
492  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
493  *****************************************************************************
494  * We simply undo what we did in config_SetCallbacks.
495  *****************************************************************************/
496 void config_UnsetCallbacks( module_config_t *p_new )
497 {
498     while( p_new->i_type != CONFIG_HINT_END )
499     {
500         p_new->pf_callback = NULL;
501         p_new++;
502     }
503 }
504
505 /*****************************************************************************
506  * config_LoadConfigFile: loads the configuration file.
507  *****************************************************************************
508  * This function is called to load the config options stored in the config
509  * file.
510  *****************************************************************************/
511 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
512 {
513     vlc_list_t *p_list;
514     module_t *p_parser;
515     module_config_t *p_item;
516     FILE *file;
517     char line[1024];
518     char *p_index, *psz_option_name, *psz_option_value;
519     char *psz_filename, *psz_homedir;
520     int i_index;
521
522     psz_homedir = p_this->p_vlc->psz_homedir;
523     if( !psz_homedir )
524     {
525         msg_Err( p_this, "psz_homedir is null" );
526         return -1;
527     }
528     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
529                                    strlen(psz_homedir) + 1 );
530     if( !psz_filename )
531     {
532         msg_Err( p_this, "out of memory" );
533         return -1;
534     }
535     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
536
537     msg_Dbg( p_this, "opening config file %s", psz_filename );
538
539     /* Acquire config file lock */
540     vlc_mutex_lock( &p_this->p_vlc->config_lock );
541
542     file = fopen( psz_filename, "rt" );
543     if( !file )
544     {
545         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
546         free( psz_filename );
547         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
548         return -1;
549     }
550
551     /* Look for the selected module, if NULL then save everything */
552     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
553
554     for( i_index = 0; i_index < p_list->i_count; i_index++ )
555     {
556         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
557
558         if( psz_module_name
559              && strcmp( psz_module_name, p_parser->psz_object_name ) )
560         {
561             continue;
562         }
563
564         /* The config file is organized in sections, one per module. Look for
565          * the interesting section ( a section is of the form [foo] ) */
566         fseek( file, 0L, SEEK_SET );
567         while( fgets( line, 1024, file ) )
568         {
569             if( (line[0] == '[')
570                && (p_index = strchr(line,']'))
571                && (p_index - &line[1]
572                     == (int)strlen(p_parser->psz_object_name))
573                && !memcmp( &line[1], p_parser->psz_object_name,
574                            strlen(p_parser->psz_object_name) ) )
575             {
576 #if 0
577                 msg_Dbg( p_this, "loading config for module \"%s\"",
578                                  p_parser->psz_object_name );
579 #endif
580
581                 break;
582             }
583         }
584         /* either we found the section or we're at the EOF */
585
586         /* Now try to load the options in this section */
587         while( fgets( line, 1024, file ) )
588         {
589             if( line[0] == '[' ) break; /* end of section */
590
591             /* ignore comments or empty lines */
592             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
593                 continue;
594
595             /* get rid of line feed */
596             if( line[strlen(line)-1] == '\n' )
597                 line[strlen(line)-1] = (char)0;
598
599             /* look for option name */
600             psz_option_name = line;
601             psz_option_value = NULL;
602             p_index = strchr( line, '=' );
603             if( !p_index ) break; /* this ain't an option!!! */
604
605             *p_index = (char)0;
606             psz_option_value = p_index + 1;
607
608             if( !p_parser->i_config_items )
609             {
610                 continue;
611             }
612
613             /* try to match this option with one of the module's options */
614             for( p_item = p_parser->p_config;
615                  p_item->i_type != CONFIG_HINT_END;
616                  p_item++ )
617             {
618                 if( p_item->i_type & CONFIG_HINT )
619                     /* ignore hints */
620                     continue;
621
622                 if( !strcmp( p_item->psz_name, psz_option_name ) )
623                 {
624                     /* We found it */
625                     switch( p_item->i_type )
626                     {
627                     case CONFIG_ITEM_BOOL:
628                     case CONFIG_ITEM_INTEGER:
629                         if( !*psz_option_value )
630                             break;                    /* ignore empty option */
631                         p_item->i_value = atoi( psz_option_value);
632 #if 0
633                         msg_Dbg( p_this, "option \"%s\", value %i",
634                                  p_item->psz_name, p_item->i_value );
635 #endif
636                         break;
637
638                     case CONFIG_ITEM_FLOAT:
639                         if( !*psz_option_value )
640                             break;                    /* ignore empty option */
641                         p_item->f_value = (float)atof( psz_option_value);
642 #if O
643                         msg_Dbg( p_this, "option \"%s\", value %f",
644                                  p_item->psz_name, (double)p_item->f_value );
645 #endif
646                         break;
647
648                     default:
649                         vlc_mutex_lock( p_item->p_lock );
650
651                         /* free old string */
652                         if( p_item->psz_value )
653                             free( p_item->psz_value );
654
655                         p_item->psz_value = *psz_option_value ?
656                             strdup( psz_option_value ) : NULL;
657
658                         vlc_mutex_unlock( p_item->p_lock );
659
660 #if 0
661                         msg_Dbg( p_this, "option \"%s\", value \"%s\"",
662                                  p_item->psz_name,
663                                  p_item->psz_value ? p_item->psz_value : "" );
664 #endif
665                         break;
666                     }
667                 }
668             }
669         }
670
671     }
672
673     vlc_list_release( p_list );
674
675     fclose( file );
676     free( psz_filename );
677
678     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
679
680     return 0;
681 }
682
683 /*****************************************************************************
684  * config_SaveConfigFile: Save a module's config options.
685  *****************************************************************************
686  * This will save the specified module's config options to the config file.
687  * If psz_module_name is NULL then we save all the modules config options.
688  * It's no use to save the config options that kept their default values, so
689  * we'll try to be a bit clever here.
690  *
691  * When we save we mustn't delete the config options of the modules that
692  * haven't been loaded. So we cannot just create a new config file with the
693  * config structures we've got in memory.
694  * I don't really know how to deal with this nicely, so I will use a completly
695  * dumb method ;-)
696  * I will load the config file in memory, but skipping all the sections of the
697  * modules we want to save. Then I will create a brand new file, dump the file
698  * loaded in memory and then append the sections of the modules we want to
699  * save.
700  * Really stupid no ?
701  *****************************************************************************/
702 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
703 {
704     module_t *p_parser;
705     vlc_list_t *p_list;
706     module_config_t *p_item;
707     FILE *file;
708     char p_line[1024], *p_index2;
709     int i_sizebuf = 0;
710     char *p_bigbuffer, *p_index;
711     vlc_bool_t b_backup;
712     char *psz_filename, *psz_homedir;
713     int i_index;
714
715     /* Acquire config file lock */
716     vlc_mutex_lock( &p_this->p_vlc->config_lock );
717
718     psz_homedir = p_this->p_vlc->psz_homedir;
719     if( !psz_homedir )
720     {
721         msg_Err( p_this, "psz_homedir is null" );
722         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
723         return -1;
724     }
725     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
726                                    strlen(psz_homedir) + 1 );
727     if( !psz_filename )
728     {
729         msg_Err( p_this, "out of memory" );
730         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
731         return -1;
732     }
733     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
734
735 #if defined( UNDER_CE )
736     {
737         wchar_t psz_new[ MAX_PATH ];
738         MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_new, MAX_PATH );
739         if( CreateDirectory( psz_new, NULL ) )
740         {
741             msg_Err( p_this, "could not create %s", psz_filename );
742         }
743     }
744
745 #elif defined( HAVE_ERRNO_H )
746 #   if defined( WIN32 )
747     if( mkdir( psz_filename ) && errno != EEXIST )
748 #   else
749     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
750 #   endif
751     {
752         msg_Err( p_this, "could not create %s (%s)",
753                          psz_filename, strerror(errno) );
754     }
755
756 #else
757     if( mkdir( psz_filename ) )
758     {
759         msg_Err( p_this, "could not create %s", psz_filename );
760     }
761
762 #endif
763
764     strcat( psz_filename, "/" CONFIG_FILE );
765
766
767     msg_Dbg( p_this, "opening config file %s", psz_filename );
768
769     file = fopen( psz_filename, "rt" );
770     if( !file )
771     {
772         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
773     }
774     else
775     {
776         /* look for file size */
777         fseek( file, 0L, SEEK_END );
778         i_sizebuf = ftell( file );
779         fseek( file, 0L, SEEK_SET );
780     }
781
782     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
783     if( !p_bigbuffer )
784     {
785         msg_Err( p_this, "out of memory" );
786         if( file ) fclose( file );
787         free( psz_filename );
788         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
789         return -1;
790     }
791     p_bigbuffer[0] = 0;
792
793     /* List all available modules */
794     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
795
796     /* backup file into memory, we only need to backup the sections we won't
797      * save later on */
798     b_backup = 0;
799     while( file && fgets( p_line, 1024, file ) )
800     {
801         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
802         {
803
804             /* we found a section, check if we need to do a backup */
805             for( i_index = 0; i_index < p_list->i_count; i_index++ )
806             {
807                 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
808
809                 if( ((p_index2 - &p_line[1])
810                        == (int)strlen(p_parser->psz_object_name) )
811                     && !memcmp( &p_line[1], p_parser->psz_object_name,
812                                 strlen(p_parser->psz_object_name) ) )
813                 {
814                     if( !psz_module_name )
815                         break;
816                     else if( !strcmp( psz_module_name,
817                                       p_parser->psz_object_name ) )
818                         break;
819                 }
820             }
821
822             if( i_index == p_list->i_count )
823             {
824                 /* we don't have this section in our list so we need to back
825                  * it up */
826                 *p_index2 = 0;
827                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
828                                  &p_line[1] );
829                 *p_index2 = ']';
830
831                 b_backup = 1;
832             }
833             else
834             {
835                 b_backup = 0;
836             }
837         }
838
839         /* save line if requested and line is valid (doesn't begin with a
840          * space, tab, or eol) */
841         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
842             && (p_line[0] != '\t') )
843         {
844             strcpy( p_index, p_line );
845             p_index += strlen( p_line );
846         }
847     }
848     if( file ) fclose( file );
849
850
851     /*
852      * Save module config in file
853      */
854
855     file = fopen( psz_filename, "wt" );
856     if( !file )
857     {
858         msg_Warn( p_this, "could not open config file %s for writing",
859                           psz_filename );
860         free( psz_filename );
861         vlc_list_release( p_list );
862         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
863         return -1;
864     }
865
866     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
867
868     /* Look for the selected module, if NULL then save everything */
869     for( i_index = 0; i_index < p_list->i_count; i_index++ )
870     {
871         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
872
873         if( psz_module_name && strcmp( psz_module_name,
874                                        p_parser->psz_object_name ) )
875             continue;
876
877         if( !p_parser->i_config_items )
878             continue;
879
880         msg_Dbg( p_this, "saving config for module \"%s\"",
881                          p_parser->psz_object_name );
882
883         fprintf( file, "[%s]", p_parser->psz_object_name );
884         if( p_parser->psz_longname )
885             fprintf( file, " # %s\n\n", p_parser->psz_longname );
886         else
887             fprintf( file, "\n\n" );
888
889         for( p_item = p_parser->p_config;
890              p_item->i_type != CONFIG_HINT_END;
891              p_item++ )
892         {
893             if( p_item->i_type & CONFIG_HINT )
894                 /* ignore hints */
895                 continue;
896
897             switch( p_item->i_type )
898             {
899             case CONFIG_ITEM_BOOL:
900             case CONFIG_ITEM_INTEGER:
901                 if( p_item->psz_text )
902                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
903                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
904                              _("boolean") : _("integer") );
905                 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
906                 break;
907
908             case CONFIG_ITEM_FLOAT:
909                 if( p_item->psz_text )
910                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
911                              _("float") );
912                 fprintf( file, "%s=%f\n", p_item->psz_name,
913                          (double)p_item->f_value );
914                 break;
915
916             default:
917                 if( p_item->psz_text )
918                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
919                              _("string") );
920                 fprintf( file, "%s=%s\n", p_item->psz_name,
921                          p_item->psz_value ? p_item->psz_value : "" );
922             }
923         }
924
925         fprintf( file, "\n" );
926     }
927
928     vlc_list_release( p_list );
929
930     /*
931      * Restore old settings from the config in file
932      */
933     fputs( p_bigbuffer, file );
934     free( p_bigbuffer );
935
936     fclose( file );
937     free( psz_filename );
938     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
939
940     return 0;
941 }
942
943 /*****************************************************************************
944  * config_LoadCmdLine: parse command line
945  *****************************************************************************
946  * Parse command line for configuration options.
947  * Now that the module_bank has been initialized, we can dynamically
948  * generate the longopts structure used by getops. We have to do it this way
949  * because we don't know (and don't want to know) in advance the configuration
950  * options used (ie. exported) by each module.
951  *****************************************************************************/
952 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
953                           vlc_bool_t b_ignore_errors )
954 {
955     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
956     module_t *p_parser;
957     vlc_list_t *p_list;
958     module_config_t *p_item;
959     struct option *p_longopts;
960     int i_modules_index;
961
962     /* Short options */
963     module_config_t *pp_shortopts[256];
964     char *psz_shortopts;
965
966     /* Set default configuration and copy arguments */
967     p_this->p_vlc->i_argc    = *pi_argc;
968     p_this->p_vlc->ppsz_argv = ppsz_argv;
969
970     p_this->p_vlc->p_channel = NULL;
971
972 #ifdef SYS_DARWIN
973     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
974      * is the PSN - process serial number (a unique PID-ish thingie)
975      * still ok for real Darwin & when run from command line */
976     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
977                                         /* for example -psn_0_9306113 */
978     {
979         /* GDMF!... I can't do this or else the MacOSX window server will
980          * not pick up the PSN and not register the app and we crash...
981          * hence the following kludge otherwise we'll get confused w/ argv[1]
982          * being an input file name */
983 #if 0
984         ppsz_argv[ 1 ] = NULL;
985 #endif
986         *pi_argc = *pi_argc - 1;
987         pi_argc--;
988         return 0;
989     }
990 #endif
991
992     /* List all modules */
993     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
994
995     /*
996      * Generate the longopts and shortopts structures used by getopt_long
997      */
998
999     i_opts = 0;
1000     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1001          i_modules_index++ )
1002     {
1003         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1004
1005         /* count the number of exported configuration options (to allocate
1006          * longopts). We also need to allocate space for too options when
1007          * dealing with boolean to allow for --foo and --no-foo */
1008         i_opts += p_parser->i_config_items
1009                      + 2 * p_parser->i_bool_items;
1010     }
1011
1012     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1013     if( p_longopts == NULL )
1014     {
1015         msg_Err( p_this, "out of memory" );
1016         vlc_list_release( p_list );
1017         return -1;
1018     }
1019
1020     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1021     if( psz_shortopts == NULL )
1022     {
1023         msg_Err( p_this, "out of memory" );
1024         free( p_longopts );
1025         vlc_list_release( p_list );
1026         return -1;
1027     }
1028
1029     /* If we are requested to ignore errors, then we must work on a copy
1030      * of the ppsz_argv array, otherwise getopt_long will reorder it for
1031      * us, ignoring the arity of the options */
1032     if( b_ignore_errors )
1033     {
1034         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1035         if( ppsz_argv == NULL )
1036         {
1037             msg_Err( p_this, "out of memory" );
1038             free( psz_shortopts );
1039             free( p_longopts );
1040             vlc_list_release( p_list );
1041             return -1;
1042         }
1043         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1044                 *pi_argc * sizeof(char *) );
1045     }
1046
1047     i_shortopts = 0;
1048     for( i_index = 0; i_index < 256; i_index++ )
1049     {
1050         pp_shortopts[i_index] = NULL;
1051     }
1052
1053     /* Fill the p_longopts and psz_shortopts structures */
1054     i_index = 0;
1055     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1056          i_modules_index++ )
1057     {
1058         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1059
1060         if( !p_parser->i_config_items )
1061             continue;
1062
1063         for( p_item = p_parser->p_config;
1064              p_item->i_type != CONFIG_HINT_END;
1065              p_item++ )
1066         {
1067             /* Ignore hints */
1068             if( p_item->i_type & CONFIG_HINT )
1069                 continue;
1070
1071             /* Add item to long options */
1072             p_longopts[i_index].name = strdup( p_item->psz_name );
1073             if( p_longopts[i_index].name == NULL ) continue;
1074             p_longopts[i_index].has_arg =
1075                 (p_item->i_type == CONFIG_ITEM_BOOL)?
1076                                                no_argument : required_argument;
1077             p_longopts[i_index].flag = &flag;
1078             p_longopts[i_index].val = 0;
1079             i_index++;
1080
1081             /* When dealing with bools we also need to add the --no-foo
1082              * option */
1083             if( p_item->i_type == CONFIG_ITEM_BOOL )
1084             {
1085                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1086                 if( psz_name == NULL ) continue;
1087                 strcpy( psz_name, "no" );
1088                 strcat( psz_name, p_item->psz_name );
1089
1090                 p_longopts[i_index].name = psz_name;
1091                 p_longopts[i_index].has_arg = no_argument;
1092                 p_longopts[i_index].flag = &flag;
1093                 p_longopts[i_index].val = 1;
1094                 i_index++;
1095
1096                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1097                 if( psz_name == NULL ) continue;
1098                 strcpy( psz_name, "no-" );
1099                 strcat( psz_name, p_item->psz_name );
1100
1101                 p_longopts[i_index].name = psz_name;
1102                 p_longopts[i_index].has_arg = no_argument;
1103                 p_longopts[i_index].flag = &flag;
1104                 p_longopts[i_index].val = 1;
1105                 i_index++;
1106             }
1107
1108             /* If item also has a short option, add it */
1109             if( p_item->i_short )
1110             {
1111                 pp_shortopts[(int)p_item->i_short] = p_item;
1112                 psz_shortopts[i_shortopts] = p_item->i_short;
1113                 i_shortopts++;
1114                 if( p_item->i_type != CONFIG_ITEM_BOOL )
1115                 {
1116                     psz_shortopts[i_shortopts] = ':';
1117                     i_shortopts++;
1118
1119                     if( p_item->i_short == 'v' )
1120                     {
1121                         psz_shortopts[i_shortopts] = ':';
1122                         i_shortopts++;
1123                     }
1124                 }
1125             }
1126         }
1127     }
1128
1129     /* We don't need the module list anymore */
1130     vlc_list_release( p_list );
1131
1132     /* Close the longopts and shortopts structures */
1133     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1134     psz_shortopts[i_shortopts] = '\0';
1135
1136     /*
1137      * Parse the command line options
1138      */
1139     opterr = 0;
1140     optind = 1;
1141     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1142                                   p_longopts, &i_index ) ) != EOF )
1143     {
1144         /* A long option has been recognized */
1145         if( i_cmd == 0 )
1146         {
1147             module_config_t *p_conf;
1148             char *psz_name = (char *)p_longopts[i_index].name;
1149
1150             /* Check if we deal with a --nofoo or --no-foo long option */
1151             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1152
1153             /* Store the configuration option */
1154             p_conf = config_FindConfig( p_this, psz_name );
1155
1156             if( p_conf ) switch( p_conf->i_type )
1157             {
1158             case CONFIG_ITEM_STRING:
1159             case CONFIG_ITEM_FILE:
1160             case CONFIG_ITEM_DIRECTORY:
1161             case CONFIG_ITEM_MODULE:
1162                 config_PutPsz( p_this, psz_name, optarg );
1163                 break;
1164             case CONFIG_ITEM_INTEGER:
1165                 config_PutInt( p_this, psz_name, atoi(optarg));
1166                 break;
1167             case CONFIG_ITEM_FLOAT:
1168                 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1169                 break;
1170             case CONFIG_ITEM_BOOL:
1171                 config_PutInt( p_this, psz_name, !flag );
1172                 break;
1173             }
1174
1175             continue;
1176         }
1177
1178         /* A short option has been recognized */
1179         if( pp_shortopts[i_cmd] != NULL )
1180         {
1181             switch( pp_shortopts[i_cmd]->i_type )
1182             {
1183             case CONFIG_ITEM_STRING:
1184             case CONFIG_ITEM_FILE:
1185             case CONFIG_ITEM_DIRECTORY:
1186             case CONFIG_ITEM_MODULE:
1187                 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1188                 break;
1189             case CONFIG_ITEM_INTEGER:
1190                 if( i_cmd == 'v' )
1191                 {
1192                     if( optarg )
1193                     {
1194                         if( *optarg == 'v' ) /* eg. -vvv */
1195                         {
1196                             i_verbose++;
1197                             while( *optarg == 'v' )
1198                             {
1199                                 i_verbose++;
1200                                 optarg++;
1201                             }
1202                         }
1203                         else
1204                         {
1205                             i_verbose += atoi( optarg ); /* eg. -v2 */
1206                         }
1207                     }
1208                     else
1209                     {
1210                         i_verbose++; /* -v */
1211                     }
1212                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1213                                            i_verbose );
1214                 }
1215                 else
1216                 {
1217                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1218                                            atoi(optarg) );
1219                 }
1220                 break;
1221             case CONFIG_ITEM_BOOL:
1222                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1223                 break;
1224             }
1225
1226             continue;
1227         }
1228
1229         /* Internal error: unknown option */
1230         if( !b_ignore_errors )
1231         {
1232             fprintf( stderr, "%s: unknown option ",
1233                              p_this->p_vlc->psz_object_name );
1234             if( optopt )
1235             {
1236                 fprintf( stderr, "`-%c'\n", optopt );
1237             }
1238             else
1239             {
1240                 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1241             }
1242             fprintf( stderr, "Try `%s --help' for more information.\n",
1243                              p_this->p_vlc->psz_object_name );
1244
1245             free( p_longopts );
1246             free( psz_shortopts );
1247             if( b_ignore_errors ) free( ppsz_argv );
1248             return -1;
1249         }
1250     }
1251
1252     /* Free allocated resources */
1253     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1254         free( (char *)p_longopts[i_index].name );
1255     free( p_longopts );
1256     free( psz_shortopts );
1257     if( b_ignore_errors ) free( ppsz_argv );
1258
1259     return 0;
1260 }
1261
1262 /*****************************************************************************
1263  * config_GetHomeDir: find the user's home directory.
1264  *****************************************************************************
1265  * This function will try by different ways to find the user's home path.
1266  * Note that this function is not reentrant, it should be called only once
1267  * at the beginning of main where the result will be stored for later use.
1268  *****************************************************************************/
1269 char *config_GetHomeDir( void )
1270 {
1271     char *p_tmp, *p_homedir = NULL;
1272
1273 #if defined(HAVE_GETPWUID)
1274     struct passwd *p_pw = NULL;
1275 #endif
1276
1277 #if defined(WIN32) || defined(UNDER_CE)
1278     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1279                                                LPTSTR );
1280 #   define CSIDL_FLAG_CREATE 0x8000
1281 #   define CSIDL_APPDATA 0x1A
1282 #   define SHGFP_TYPE_CURRENT 0
1283
1284     HINSTANCE shfolder_dll;
1285     SHGETFOLDERPATH SHGetFolderPath ;
1286
1287     /* load the shell32 dll to retreive SHGetFolderPath */
1288     if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1289     {
1290         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1291                                                   "SHGetFolderPathA" );
1292         if ( SHGetFolderPath != NULL )
1293         {
1294             p_homedir = (char *)malloc( MAX_PATH );
1295             if( !p_homedir )
1296             {
1297                 return NULL;
1298             }
1299
1300             /* get the "Application Data" folder for the current user */
1301             if( S_OK == SHGetFolderPath( NULL,
1302                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1303                                          NULL, SHGFP_TYPE_CURRENT,
1304                                          p_homedir ) )
1305             {
1306                 FreeLibrary( shfolder_dll );
1307                 return p_homedir;
1308             }
1309             free( p_homedir );
1310         }
1311         FreeLibrary( shfolder_dll );
1312     }
1313 #endif
1314
1315 #if defined(HAVE_GETPWUID)
1316     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1317 #endif
1318     {
1319         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1320         {
1321             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1322             {
1323                 p_tmp = "/tmp";
1324             }
1325         }
1326
1327         p_homedir = strdup( p_tmp );
1328     }
1329 #if defined(HAVE_GETPWUID)
1330     else
1331     {
1332         p_homedir = strdup( p_pw->pw_dir );
1333     }
1334 #endif
1335
1336     return p_homedir;
1337 }