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