]> git.sesse.net Git - vlc/blob - src/modules/configuration.c
8ee62f3dfd4b3195c7ad6eefb64e2c5eddc357ee
[vlc] / src / modules / configuration.c
1 /*****************************************************************************
2  * configuration.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include "vlc_keys.h"
26 #include "vlc_charset.h"
27
28 #include <stdio.h>                                              /* sprintf() */
29 #include <stdlib.h>                                      /* free(), strtol() */
30 #include <string.h>                                              /* strdup() */
31 #include <errno.h>                                                  /* errno */
32
33 #ifdef HAVE_LIMITS_H
34 #   include <limits.h>
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 #    include <unistd.h>                                          /* getuid() */
39 #endif
40
41 #ifdef HAVE_GETOPT_LONG
42 #   ifdef HAVE_GETOPT_H
43 #       include <getopt.h>                                       /* getopt() */
44 #   endif
45 #else
46 #   include "../extras/getopt.h"
47 #endif
48
49 #if defined(HAVE_GETPWUID)
50 #   include <pwd.h>                                            /* getpwuid() */
51 #endif
52
53 #if defined( HAVE_SYS_STAT_H )
54 #   include <sys/stat.h>
55 #endif
56 #if defined( HAVE_SYS_TYPES_H )
57 #   include <sys/types.h>
58 #endif
59 #if defined( WIN32 )
60 #   if !defined( UNDER_CE )
61 #       include <direct.h>
62 #   endif
63 #include <tchar.h>
64 #endif
65
66 #include "configuration.h"
67 #include "modules/modules.h"
68
69 static int ConfigStringToKey( const char * );
70 static char *ConfigKeyToString( int );
71
72 static inline void freenull (const void *p)
73 {
74     if (p != NULL)
75         free ((void *)p);
76 }
77
78 static inline char *strdupnull (const char *src)
79 {
80     if (src == NULL)
81         return NULL;
82     return strdup (src);
83 }
84
85 static inline char *_strdupnull (const char *src)
86 {
87     if (src == NULL)
88         return NULL;
89     return strdup (_(src));
90 }
91
92
93 /*****************************************************************************
94  * config_GetType: get the type of a variable (bool, int, float, string)
95  *****************************************************************************
96  * This function is used to get the type of a variable from its name.
97  * Beware, this is quite slow.
98  *****************************************************************************/
99 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
100 {
101     module_config_t *p_config;
102     int i_type;
103
104     p_config = config_FindConfig( p_this, psz_name );
105
106     /* sanity checks */
107     if( !p_config )
108     {
109         return 0;
110     }
111
112     switch( p_config->i_type )
113     {
114     case CONFIG_ITEM_BOOL:
115         i_type = VLC_VAR_BOOL;
116         break;
117
118     case CONFIG_ITEM_INTEGER:
119         i_type = VLC_VAR_INTEGER;
120         break;
121
122     case CONFIG_ITEM_FLOAT:
123         i_type = VLC_VAR_FLOAT;
124         break;
125
126     case CONFIG_ITEM_MODULE:
127     case CONFIG_ITEM_MODULE_CAT:
128     case CONFIG_ITEM_MODULE_LIST:
129     case CONFIG_ITEM_MODULE_LIST_CAT:
130         i_type = VLC_VAR_MODULE;
131         break;
132
133     case CONFIG_ITEM_STRING:
134         i_type = VLC_VAR_STRING;
135         break;
136
137     case CONFIG_ITEM_FILE:
138         i_type = VLC_VAR_FILE;
139         break;
140
141     case CONFIG_ITEM_DIRECTORY:
142         i_type = VLC_VAR_DIRECTORY;
143         break;
144
145     default:
146         i_type = 0;
147         break;
148     }
149
150     return i_type;
151 }
152
153 /*****************************************************************************
154  * config_GetInt: get the value of an int variable
155  *****************************************************************************
156  * This function is used to get the value of variables which are internally
157  * represented by an integer (CONFIG_ITEM_INTEGER and
158  * CONFIG_ITEM_BOOL).
159  *****************************************************************************/
160 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
161 {
162     module_config_t *p_config;
163
164     p_config = config_FindConfig( p_this, psz_name );
165
166     /* sanity checks */
167     if( !p_config )
168     {
169         msg_Err( p_this, "option %s does not exist", psz_name );
170         return -1;
171     }
172
173     if (!IsConfigIntegerType (p_config->i_type))
174     {
175         msg_Err( p_this, "option %s does not refer to an int", psz_name );
176         return -1;
177     }
178
179     return p_config->value.i;
180 }
181
182 /*****************************************************************************
183  * config_GetFloat: get the value of a float variable
184  *****************************************************************************
185  * This function is used to get the value of variables which are internally
186  * represented by a float (CONFIG_ITEM_FLOAT).
187  *****************************************************************************/
188 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
189 {
190     module_config_t *p_config;
191
192     p_config = config_FindConfig( p_this, psz_name );
193
194     /* sanity checks */
195     if( !p_config )
196     {
197         msg_Err( p_this, "option %s does not exist", psz_name );
198         return -1;
199     }
200
201     if (!IsConfigFloatType (p_config->i_type))
202     {
203         msg_Err( p_this, "option %s does not refer to a float", psz_name );
204         return -1;
205     }
206
207     return p_config->value.f;
208 }
209
210 /*****************************************************************************
211  * config_GetPsz: get the string value of a string variable
212  *****************************************************************************
213  * This function is used to get the value of variables which are internally
214  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
215  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
216  *
217  * Important note: remember to free() the returned char* because it's a
218  *   duplicate of the actual value. It isn't safe to return a pointer to the
219  *   actual value as it can be modified at any time.
220  *****************************************************************************/
221 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
222 {
223     module_config_t *p_config;
224
225     p_config = config_FindConfig( p_this, psz_name );
226
227     /* sanity checks */
228     if( !p_config )
229     {
230         msg_Err( p_this, "option %s does not exist", psz_name );
231         return NULL;
232     }
233
234     if (!IsConfigStringType (p_config->i_type))
235     {
236         msg_Err( p_this, "option %s does not refer to a string", psz_name );
237         return NULL;
238     }
239
240     /* return a copy of the string */
241     vlc_mutex_lock( p_config->p_lock );
242     char *psz_value = strdupnull (p_config->value.psz);
243     vlc_mutex_unlock( p_config->p_lock );
244
245     return psz_value;
246 }
247
248 /*****************************************************************************
249  * config_PutPsz: set the string value of a string variable
250  *****************************************************************************
251  * This function is used to set the value of variables which are internally
252  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
253  * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
254  *****************************************************************************/
255 void __config_PutPsz( vlc_object_t *p_this,
256                       const char *psz_name, const char *psz_value )
257 {
258     module_config_t *p_config;
259     vlc_value_t oldval, val;
260
261     p_config = config_FindConfig( p_this, psz_name );
262
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
271     if (!IsConfigStringType (p_config->i_type))
272     {
273         msg_Err( p_this, "option %s does not refer to a string", psz_name );
274         return;
275     }
276
277     vlc_mutex_lock( p_config->p_lock );
278
279     /* backup old value */
280     oldval.psz_string = (char *)p_config->value.psz;
281
282     if ((psz_value != NULL) && *psz_value)
283         p_config->value.psz = strdup (psz_value);
284     else
285         p_config->value.psz = NULL;
286
287     p_config->b_dirty = VLC_TRUE;
288
289     val.psz_string = (char *)p_config->value.psz;
290
291     vlc_mutex_unlock( p_config->p_lock );
292
293     if( p_config->pf_callback )
294     {
295         p_config->pf_callback( p_this, psz_name, oldval, val,
296                                p_config->p_callback_data );
297     }
298
299     /* free old string */
300     if( oldval.psz_string ) free( oldval.psz_string );
301 }
302
303 /*****************************************************************************
304  * config_PutInt: set the integer value of an int variable
305  *****************************************************************************
306  * This function is used to set the value of variables which are internally
307  * represented by an integer (CONFIG_ITEM_INTEGER and
308  * CONFIG_ITEM_BOOL).
309  *****************************************************************************/
310 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
311 {
312     module_config_t *p_config;
313     vlc_value_t oldval, val;
314
315     p_config = config_FindConfig( p_this, psz_name );
316
317     /* sanity checks */
318     if( !p_config )
319     {
320         msg_Warn( p_this, "option %s does not exist", psz_name );
321         return;
322     }
323
324     if (!IsConfigIntegerType (p_config->i_type))
325     {
326         msg_Err( p_this, "option %s does not refer to an int", psz_name );
327         return;
328     }
329
330     /* backup old value */
331     oldval.i_int = p_config->value.i;
332
333     /* if i_min == i_max == 0, then do not use them */
334     if ((p_config->min.i == 0) && (p_config->max.i == 0))
335     {
336         p_config->value.i = i_value;
337     }
338     else if (i_value < p_config->min.i)
339     {
340         p_config->value.i = p_config->min.i;
341     }
342     else if (i_value > p_config->max.i)
343     {
344         p_config->value.i = p_config->max.i;
345     }
346     else
347     {
348         p_config->value.i = i_value;
349     }
350
351     p_config->b_dirty = VLC_TRUE;
352
353     val.i_int = p_config->value.i;
354
355     if( p_config->pf_callback )
356     {
357         p_config->pf_callback( p_this, psz_name, oldval, val,
358                                p_config->p_callback_data );
359     }
360 }
361
362 /*****************************************************************************
363  * config_PutFloat: set the value of a float variable
364  *****************************************************************************
365  * This function is used to set the value of variables which are internally
366  * represented by a float (CONFIG_ITEM_FLOAT).
367  *****************************************************************************/
368 void __config_PutFloat( vlc_object_t *p_this,
369                         const char *psz_name, float f_value )
370 {
371     module_config_t *p_config;
372     vlc_value_t oldval, val;
373
374     p_config = config_FindConfig( p_this, psz_name );
375
376     /* sanity checks */
377     if( !p_config )
378     {
379         msg_Warn( p_this, "option %s does not exist", psz_name );
380         return;
381     }
382
383     if (!IsConfigFloatType (p_config->i_type))
384     {
385         msg_Err( p_this, "option %s does not refer to a float", psz_name );
386         return;
387     }
388
389     /* backup old value */
390     oldval.f_float = p_config->value.f;
391
392     /* if f_min == f_max == 0, then do not use them */
393     if ((p_config->min.f == 0) && (p_config->max.f == 0))
394     {
395         p_config->value.f = f_value;
396     }
397     else if (f_value < p_config->min.f)
398     {
399         p_config->value.f = p_config->min.f;
400     }
401     else if (f_value > p_config->max.f)
402     {
403         p_config->value.f = p_config->max.f;
404     }
405     else
406     {
407         p_config->value.f = f_value;
408     }
409
410     p_config->b_dirty = VLC_TRUE;
411
412     val.f_float = p_config->value.f;
413
414     if( p_config->pf_callback )
415     {
416         p_config->pf_callback( p_this, psz_name, oldval, val,
417                                p_config->p_callback_data );
418     }
419 }
420
421 /*****************************************************************************
422  * config_FindConfig: find the config structure associated with an option.
423  *****************************************************************************
424  * FIXME: This function really needs to be optimized.
425  * FIXME: And now even more.
426  *****************************************************************************/
427 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
428 {
429     vlc_list_t *p_list;
430     int i_index;
431
432     if( !psz_name ) return NULL;
433
434     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
435
436     for( i_index = 0; i_index < p_list->i_count; i_index++ )
437     {
438         module_config_t *p_item, *p_end;
439         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
440
441         if( !p_parser->i_config_items )
442             continue;
443
444         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
445              p_item < p_end;
446              p_item++ )
447         {
448             if( p_item->i_type & CONFIG_HINT )
449                 /* ignore hints */
450                 continue;
451             if( !strcmp( psz_name, p_item->psz_name ) )
452             {
453                 vlc_list_release( p_list );
454                 return p_item;
455             }
456         }
457     }
458
459     vlc_list_release( p_list );
460
461     return NULL;
462 }
463
464 /*****************************************************************************
465  * config_FindModule: find a specific module structure.
466  *****************************************************************************/
467 module_t *config_FindModule( vlc_object_t *p_this, const char *psz_name )
468 {
469     vlc_list_t *p_list;
470     module_t *p_module, *p_result = NULL;
471     int i_index;
472
473     if( !psz_name ) return NULL;
474
475     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
476
477     for( i_index = 0; i_index < p_list->i_count; i_index++ )
478     {
479         p_module = (module_t *)p_list->p_values[i_index].p_object;
480         if( !strcmp( p_module->psz_object_name, psz_name ) )
481         {
482              p_result = p_module;
483              break;
484         }
485     }
486
487     vlc_list_release( p_list );
488
489     return p_result;
490 }
491
492 /*****************************************************************************
493  * config_Duplicate: creates a duplicate of a module's configuration data.
494  *****************************************************************************
495  * Unfortunatly we cannot work directly with the module's config data as
496  * this module might be unloaded from memory at any time (remember HideModule).
497  * This is why we need to create an exact copy of the config data.
498  *****************************************************************************/
499 int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
500                       size_t n )
501 {
502     int j;
503     const module_config_t *p_item, *p_end = p_orig + n;
504
505     /* Calculate the structure length */
506     p_module->i_config_items = 0;
507     p_module->i_bool_items = 0;
508
509     for( p_item = p_orig; p_item < p_end; p_item++ )
510     {
511         if( p_item->i_type & CONFIG_ITEM )
512         {
513             p_module->i_config_items++;
514         }
515
516         if( p_item->i_type == CONFIG_ITEM_BOOL )
517         {
518             p_module->i_bool_items++;
519         }
520     }
521
522     /* Allocate memory */
523     p_module->p_config = (module_config_t *)calloc( n, sizeof(*p_orig) );
524     if( p_module->p_config == NULL )
525     {
526         msg_Err( p_module, "config error: can't duplicate p_config" );
527         return VLC_ENOMEM;
528     }
529     p_module->confsize = n;
530
531     /* Do the duplication job */
532     for( size_t i = 0; i < n ; i++ )
533     {
534         p_module->p_config[i] = p_orig[i];
535
536         if (IsConfigIntegerType (p_module->p_config[i].i_type))
537         {
538             p_module->p_config[i].orig.i = p_orig[i].value.i;
539             p_module->p_config[i].saved.i = p_orig[i].value.i;
540         }
541         else
542         if (IsConfigFloatType (p_module->p_config[i].i_type))
543         {
544             p_module->p_config[i].orig.f = p_orig[i].value.f;
545             p_module->p_config[i].saved.f = p_orig[i].value.f;
546         }
547         else
548         if (IsConfigStringType (p_module->p_config[i].i_type))
549         {
550             p_module->p_config[i].value.psz = strdupnull (p_orig[i].value.psz);
551             p_module->p_config[i].orig.psz = strdupnull (p_orig[i].value.psz);
552             p_module->p_config[i].saved.psz = NULL;
553         }
554
555         p_module->p_config[i].psz_type = strdupnull (p_orig[i].psz_type);
556         p_module->p_config[i].psz_name = strdupnull (p_orig[i].psz_name);
557         p_module->p_config[i].psz_current = strdupnull (p_orig[i].psz_current);
558         p_module->p_config[i].psz_text = _strdupnull (p_orig[i].psz_text);
559         p_module->p_config[i].psz_longtext = _strdupnull (p_orig[i].psz_longtext);
560
561         p_module->p_config[i].p_lock = &p_module->object_lock;
562
563         /* duplicate the string list */
564         if( p_orig[i].i_list )
565         {
566             if( p_orig[i].ppsz_list )
567             {
568                 p_module->p_config[i].ppsz_list =
569                     malloc( (p_orig[i].i_list + 1) * sizeof(char *) );
570                 if( p_module->p_config[i].ppsz_list )
571                 {
572                     for( j = 0; j < p_orig[i].i_list; j++ )
573                         p_module->p_config[i].ppsz_list[j] =
574                                 strdupnull (p_orig[i].ppsz_list[j]);
575                     p_module->p_config[i].ppsz_list[j] = NULL;
576                 }
577             }
578             if( p_orig[i].ppsz_list_text )
579             {
580                 p_module->p_config[i].ppsz_list_text =
581                     calloc( (p_orig[i].i_list + 1), sizeof(char *) );
582                 if( p_module->p_config[i].ppsz_list_text )
583                 {
584                     for( j = 0; j < p_orig[i].i_list; j++ )
585                         p_module->p_config[i].ppsz_list_text[j] =
586                                 strdupnull (_(p_orig[i].ppsz_list_text[j]));
587                     p_module->p_config[i].ppsz_list_text[j] = NULL;
588                 }
589             }
590             if( p_orig[i].pi_list )
591             {
592                 p_module->p_config[i].pi_list =
593                     malloc( (p_orig[i].i_list + 1) * sizeof(int) );
594                 if( p_module->p_config[i].pi_list )
595                 {
596                     for( j = 0; j < p_orig[i].i_list; j++ )
597                         p_module->p_config[i].pi_list[j] =
598                             p_orig[i].pi_list[j];
599                 }
600             }
601         }
602
603         /* duplicate the actions list */
604         if( p_orig[i].i_action )
605         {
606             int j;
607
608             p_module->p_config[i].ppf_action =
609                 malloc( p_orig[i].i_action * sizeof(void *) );
610             p_module->p_config[i].ppsz_action_text =
611                 malloc( p_orig[i].i_action * sizeof(char *) );
612
613             for( j = 0; j < p_orig[i].i_action; j++ )
614             {
615                 p_module->p_config[i].ppf_action[j] =
616                     p_orig[i].ppf_action[j];
617                 p_module->p_config[i].ppsz_action_text[j] =
618                     strdupnull (p_orig[i].ppsz_action_text[j]);
619             }
620         }
621
622         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
623     }
624     return VLC_SUCCESS;
625 }
626
627
628 /*****************************************************************************
629  * config_Free: frees a duplicated module's configuration data.
630  *****************************************************************************
631  * This function frees all the data duplicated by config_Duplicate.
632  *****************************************************************************/
633 void config_Free( module_t *p_module )
634 {
635     int i;
636
637     for (size_t j = 0; j < p_module->confsize; j++)
638     {
639         module_config_t *p_item = p_module->p_config + j;
640
641         free( (char*) p_item->psz_type );
642         free( (char*) p_item->psz_name );
643         free( (char*) p_item->psz_current );
644         free( (char*) p_item->psz_text );
645         free( (char*) p_item->psz_longtext );
646
647         if (IsConfigStringType (p_item->i_type))
648         {
649             freenull (p_item->value.psz);
650             freenull (p_item->orig.psz);
651             freenull (p_item->saved.psz);
652         }
653
654         if( p_item->i_list )
655         {
656             for( i = 0; i < p_item->i_list; i++ )
657             {
658                 if( p_item->ppsz_list && p_item->ppsz_list[i] )
659                     free( (char*) p_item->ppsz_list[i] );
660                 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
661                     free( (char*) p_item->ppsz_list_text[i] );
662             }
663             if( p_item->ppsz_list ) free( p_item->ppsz_list );
664             if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
665             if( p_item->pi_list ) free( p_item->pi_list );
666         }
667
668         if( p_item->i_action )
669         {
670             for( i = 0; i < p_item->i_action; i++ )
671             {
672                 free( (char*) p_item->ppsz_action_text[i] );
673             }
674             if( p_item->ppf_action ) free( p_item->ppf_action );
675             if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
676         }
677     }
678
679     if (p_module->p_config != NULL)
680     {
681         free (p_module->p_config);
682         p_module->p_config = NULL;
683     }
684 }
685
686 /*****************************************************************************
687  * config_SetCallbacks: sets callback functions in the duplicate p_config.
688  *****************************************************************************
689  * Unfortunatly we cannot work directly with the module's config data as
690  * this module might be unloaded from memory at any time (remember HideModule).
691  * This is why we need to duplicate callbacks each time we reload the module.
692  *****************************************************************************/
693 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig,
694                           size_t n )
695 {
696     for (size_t i = 0; i < n; i++)
697     {
698         p_new->pf_callback = p_orig->pf_callback;
699         p_new++;
700         p_orig++;
701     }
702 }
703
704 /*****************************************************************************
705  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
706  *****************************************************************************
707  * We simply undo what we did in config_SetCallbacks.
708  *****************************************************************************/
709 void config_UnsetCallbacks( module_config_t *p_new, size_t n )
710 {
711     for (size_t i = 0; i < n; i++)
712     {
713         p_new->pf_callback = NULL;
714         p_new++;
715     }
716 }
717
718 /*****************************************************************************
719  * config_ResetAll: reset the configuration data for all the modules.
720  *****************************************************************************/
721 void __config_ResetAll( vlc_object_t *p_this )
722 {
723     int i_index;
724     vlc_list_t *p_list;
725     module_t *p_module;
726
727     /* Acquire config file lock */
728     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
729
730     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
731
732     for( i_index = 0; i_index < p_list->i_count; i_index++ )
733     {
734         p_module = (module_t *)p_list->p_values[i_index].p_object ;
735         if( p_module->b_submodule ) continue;
736
737         for (size_t i = 0; i < p_module->confsize; i++ )
738         {
739             if (IsConfigIntegerType (p_module->p_config[i].i_type))
740                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
741             else
742             if (IsConfigFloatType (p_module->p_config[i].i_type))
743                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
744             else
745             if (IsConfigStringType (p_module->p_config[i].i_type))
746             {
747                 freenull (p_module->p_config[i].value.psz);
748                 p_module->p_config[i].value.psz =
749                         strdupnull (p_module->p_config[i].orig.psz);
750             }
751         }
752     }
753
754     vlc_list_release( p_list );
755     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
756 }
757
758
759 static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
760 {
761     static const char psz_subpath[] = DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE;
762     const char *psz_filename = p_obj->p_libvlc->psz_configfile;
763     const char *psz_homedir;
764     size_t i_buflen = 0;
765     FILE *p_stream;
766
767     if( psz_filename == NULL )
768     {
769         psz_homedir = p_obj->p_libvlc->psz_homedir;
770         if( psz_homedir == NULL )
771         {
772             msg_Err( p_obj, "no home directory defined" );
773             return NULL;
774         }
775
776         i_buflen = strlen(psz_homedir) + sizeof(psz_subpath) + 1;
777     }
778
779     char buf[i_buflen];
780     if( psz_filename == NULL )
781     {
782         sprintf( buf, "%s%s", psz_homedir, psz_subpath );
783         psz_filename = buf;
784     }
785
786     msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
787     p_stream = utf8_fopen( psz_filename, mode );
788     if( p_stream == NULL && errno != ENOENT )
789         msg_Err( p_obj, "cannot open config file (%s): %s", psz_filename, strerror(errno) );
790
791     return p_stream;
792 }
793
794
795 /*****************************************************************************
796  * config_LoadConfigFile: loads the configuration file.
797  *****************************************************************************
798  * This function is called to load the config options stored in the config
799  * file.
800  *****************************************************************************/
801 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
802 {
803     vlc_list_t *p_list;
804     FILE *file;
805
806     file = config_OpenConfigFile (p_this, "rt");
807     if (file == NULL)
808         return VLC_EGENERIC;
809
810     /* Acquire config file lock */
811     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
812
813     /* Look for the selected module, if NULL then save everything */
814     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
815
816     /* Look for UTF-8 Byte Order Mark */
817     char * (*convert) (const char *) = strdupnull;
818     char bom[3];
819
820     if ((fread (bom, 1, 3, file) != 3)
821      || memcmp (bom, "\xEF\xBB\xBF", 3))
822     {
823         convert = FromLocaleDup;
824         rewind (file); /* no BOM, rewind */
825     }
826
827     module_t *module = NULL;
828     char line[1024], section[1022];
829     section[0] = '\0';
830
831     while (fgets (line, 1024, file) != NULL)
832     {
833         /* Ignore comments and empty lines */
834         switch (line[0])
835         {
836             case '#':
837             case '\n':
838             case '\0':
839                 continue;
840         }
841
842         if (line[0] == '[')
843         {
844             char *ptr = strchr (line, ']');
845             if (ptr == NULL)
846                 continue; /* syntax error; */
847             *ptr = '\0';
848
849             /* New section ( = a given module) */
850             strcpy (section, line + 1);
851             module = NULL;
852
853             if ((psz_module_name == NULL)
854              || (strcmp (psz_module_name, section) == 0))
855             {
856                 for (int i = 0; i < p_list->i_count; i++)
857                 {
858                     module_t *m = (module_t *)p_list->p_values[i].p_object;
859
860                     if ((strcmp (section, m->psz_object_name) == 0)
861                      && (m->i_config_items > 0)) /* ignore config-less modules */
862                     {
863                         module = m;
864                         if (psz_module_name != NULL)
865                             msg_Dbg (p_this,
866                                      "loading config for module \"%s\"",
867                                      section);
868                         break;
869                     }
870                 }
871             }
872
873             continue;
874         }
875
876         if (module == NULL)
877             continue; /* no need to parse if there is no matching module */
878
879         char *ptr = strchr (line, '\n');
880         if (ptr != NULL)
881             *ptr = '\0';
882
883         /* look for option name */
884         const char *psz_option_name = line;
885
886         ptr = strchr (line, '=');
887         if (ptr == NULL)
888             continue; /* syntax error */
889
890         *ptr = '\0';
891         const char *psz_option_value = ptr + 1;
892
893         /* try to match this option with one of the module's options */
894         for (size_t i = 0; i < module->confsize; i++)
895         {
896             module_config_t *p_item = module->p_config + i;
897
898             if ((p_item->i_type & CONFIG_HINT)
899              || strcmp (p_item->psz_name, psz_option_name))
900                 continue;
901
902             /* We found it */
903             switch( p_item->i_type )
904             {
905                 case CONFIG_ITEM_BOOL:
906                 case CONFIG_ITEM_INTEGER:
907                     if( !*psz_option_value )
908                         break;                    /* ignore empty option */
909                     p_item->value.i = strtol( psz_option_value, 0, 0 );
910                     p_item->saved.i = p_item->value.i;
911
912                     /*msg_Dbg (p_this, "option \"%s\", value %i",
913                              psz_option_name, p_item->value.i);*/
914                     break;
915
916                 case CONFIG_ITEM_FLOAT:
917                     if( !*psz_option_value )
918                         break;                    /* ignore empty option */
919                     p_item->value.f = (float)i18n_atof( psz_option_value);
920                     p_item->saved.f = p_item->value.f;
921
922                     /*msg_Dbg (p_this, "option \"%s\", value %f",
923                              psz_option_name, (double)p_item->value.f);*/
924                     break;
925
926                 case CONFIG_ITEM_KEY:
927                     if( !*psz_option_value )
928                         break;                    /* ignore empty option */
929                     p_item->value.i = ConfigStringToKey(psz_option_value);
930                     p_item->saved.i = p_item->value.i;
931                     break;
932
933                 default:
934                     vlc_mutex_lock( p_item->p_lock );
935
936                     /* free old string */
937                     free( (char*) p_item->value.psz );
938                     free( (char*) p_item->saved.psz );
939
940                     p_item->value.psz = convert (psz_option_value);
941                     p_item->saved.psz = strdupnull (p_item->value.psz);
942
943                     vlc_mutex_unlock( p_item->p_lock );
944
945                     /*msg_Dbg (p_this, "option \"%s\", value \"%s\"",
946                              psz_option_name, psz_option_value ?: "");*/
947                     break;
948             }
949
950             break;
951         }
952     }
953
954     vlc_list_release( p_list );
955
956     fclose( file );
957
958     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
959     return 0;
960 }
961
962 /*****************************************************************************
963  * config_CreateDir: Create configuration directory if it doesn't exist.
964  *****************************************************************************/
965 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
966 {
967     if( !psz_dirname && !*psz_dirname ) return -1;
968
969     if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) )
970     {
971         msg_Err( p_this, "could not create %s (%s)",
972                  psz_dirname, strerror(errno) );
973         return -1;
974     }
975
976     return 0;
977 }
978
979 /*****************************************************************************
980  * config_SaveConfigFile: Save a module's config options.
981  *****************************************************************************
982  * This will save the specified module's config options to the config file.
983  * If psz_module_name is NULL then we save all the modules config options.
984  * It's no use to save the config options that kept their default values, so
985  * we'll try to be a bit clever here.
986  *
987  * When we save we mustn't delete the config options of the modules that
988  * haven't been loaded. So we cannot just create a new config file with the
989  * config structures we've got in memory.
990  * I don't really know how to deal with this nicely, so I will use a completly
991  * dumb method ;-)
992  * I will load the config file in memory, but skipping all the sections of the
993  * modules we want to save. Then I will create a brand new file, dump the file
994  * loaded in memory and then append the sections of the modules we want to
995  * save.
996  * Really stupid no ?
997  *****************************************************************************/
998 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
999                            vlc_bool_t b_autosave )
1000 {
1001     module_t *p_parser;
1002     vlc_list_t *p_list;
1003     FILE *file;
1004     char p_line[1024], *p_index2;
1005     int i_sizebuf = 0;
1006     char *p_bigbuffer, *p_index;
1007     vlc_bool_t b_backup;
1008     int i_index;
1009
1010     /* Acquire config file lock */
1011     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1012
1013     if (p_this->p_libvlc->psz_configfile == NULL)
1014     {
1015         const char *psz_homedir = p_this->p_libvlc->psz_homedir;
1016         if( !psz_homedir )
1017         {
1018             msg_Err( p_this, "no home directory defined" );
1019             vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1020             return -1;
1021         }
1022
1023         char dirname[strlen (psz_homedir) + sizeof (DIR_SEP CONFIG_DIR)];
1024         sprintf (dirname, "%s" DIR_SEP CONFIG_DIR, psz_homedir);
1025         config_CreateDir (p_this, dirname);
1026     }
1027
1028     file = config_OpenConfigFile (p_this, "rt");
1029     if (file != NULL)
1030     {
1031         /* look for file size */
1032         fseek( file, 0L, SEEK_END );
1033         i_sizebuf = ftell( file );
1034         fseek( file, 0L, SEEK_SET );
1035     }
1036
1037     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1038     if( !p_bigbuffer )
1039     {
1040         msg_Err( p_this, "out of memory" );
1041         if( file ) fclose( file );
1042         vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1043         return -1;
1044     }
1045     p_bigbuffer[0] = 0;
1046
1047     /* List all available modules */
1048     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1049
1050     /* backup file into memory, we only need to backup the sections we won't
1051      * save later on */
1052     b_backup = 0;
1053     while( file && fgets( p_line, 1024, file ) )
1054     {
1055         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1056         {
1057
1058             /* we found a section, check if we need to do a backup */
1059             for( i_index = 0; i_index < p_list->i_count; i_index++ )
1060             {
1061                 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1062
1063                 if( ((p_index2 - &p_line[1])
1064                        == (int)strlen(p_parser->psz_object_name) )
1065                     && !memcmp( &p_line[1], p_parser->psz_object_name,
1066                                 strlen(p_parser->psz_object_name) ) )
1067                 {
1068                     if( !psz_module_name )
1069                         break;
1070                     else if( !strcmp( psz_module_name,
1071                                       p_parser->psz_object_name ) )
1072                         break;
1073                 }
1074             }
1075
1076             if( i_index == p_list->i_count )
1077             {
1078                 /* we don't have this section in our list so we need to back
1079                  * it up */
1080                 *p_index2 = 0;
1081 #if 0
1082                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1083                                  &p_line[1] );
1084 #endif
1085                 *p_index2 = ']';
1086
1087                 b_backup = 1;
1088             }
1089             else
1090             {
1091                 b_backup = 0;
1092             }
1093         }
1094
1095         /* save line if requested and line is valid (doesn't begin with a
1096          * space, tab, or eol) */
1097         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1098             && (p_line[0] != '\t') )
1099         {
1100             strcpy( p_index, p_line );
1101             p_index += strlen( p_line );
1102         }
1103     }
1104     if( file ) fclose( file );
1105
1106
1107     /*
1108      * Save module config in file
1109      */
1110
1111     file = config_OpenConfigFile (p_this, "wt");
1112     if( !file )
1113     {
1114         vlc_list_release( p_list );
1115         free( p_bigbuffer );
1116         vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1117         return -1;
1118     }
1119
1120     fprintf( file, "\xEF\xBB\xBF###\n###  " COPYRIGHT_MESSAGE "\n###\n\n"
1121        "###\n### lines begining with a '#' character are comments\n###\n\n" );
1122
1123     /* Look for the selected module, if NULL then save everything */
1124     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1125     {
1126         module_config_t *p_item, *p_end;
1127         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1128
1129         if( psz_module_name && strcmp( psz_module_name,
1130                                        p_parser->psz_object_name ) )
1131             continue;
1132
1133         if( !p_parser->i_config_items )
1134             continue;
1135
1136         if( psz_module_name )
1137             msg_Dbg( p_this, "saving config for module \"%s\"",
1138                      p_parser->psz_object_name );
1139
1140         fprintf( file, "[%s]", p_parser->psz_object_name );
1141         if( p_parser->psz_longname )
1142             fprintf( file, " # %s\n\n", p_parser->psz_longname );
1143         else
1144             fprintf( file, "\n\n" );
1145
1146         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1147              p_item < p_end;
1148              p_item++ )
1149         {
1150             char  *psz_key;
1151             int   i_value = p_item->value.i;
1152             float f_value = p_item->value.f;
1153             const char  *psz_value = p_item->value.psz;
1154
1155             if( p_item->i_type & CONFIG_HINT )
1156                 /* ignore hints */
1157                 continue;
1158             /* Ignore deprecated options */
1159             if( p_item->psz_current )
1160                 continue;
1161             if( p_item->b_unsaveable )
1162                 /*obvious*/
1163                 continue;
1164
1165             if( b_autosave && !p_item->b_autosave )
1166             {
1167                 i_value = p_item->saved.i;
1168                 f_value = p_item->saved.f;
1169                 psz_value = p_item->saved.psz;
1170                 if( !psz_value ) psz_value = p_item->orig.psz;
1171             }
1172             else
1173             {
1174                 p_item->b_dirty = VLC_FALSE;
1175             }
1176
1177             switch( p_item->i_type )
1178             {
1179             case CONFIG_ITEM_BOOL:
1180             case CONFIG_ITEM_INTEGER:
1181                 if( p_item->psz_text )
1182                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1183                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
1184                              _("boolean") : _("integer") );
1185                 if( i_value == p_item->orig.i )
1186                     fputc ('#', file);
1187                 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1188
1189                 p_item->saved.i = i_value;
1190                 break;
1191
1192             case CONFIG_ITEM_KEY:
1193                 if( p_item->psz_text )
1194                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1195                              _("key") );
1196                 if( i_value == p_item->orig.i )
1197                     fputc ('#', file);
1198                 psz_key = ConfigKeyToString( i_value );
1199                 fprintf( file, "%s=%s\n", p_item->psz_name,
1200                          psz_key ? psz_key : "" );
1201                 freenull (psz_key);
1202
1203                 p_item->saved.i = i_value;
1204                 break;
1205
1206             case CONFIG_ITEM_FLOAT:
1207                 if( p_item->psz_text )
1208                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1209                              _("float") );
1210                 if( f_value == p_item->orig.f )
1211                     fputc ('#', file);
1212                 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1213
1214                 p_item->saved.f = f_value;
1215                 break;
1216
1217             default:
1218                 if( p_item->psz_text )
1219                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1220                              _("string") );
1221                 if( (!psz_value && !p_item->orig.psz) ||
1222                     (psz_value && p_item->orig.psz &&
1223                      !strcmp( psz_value, p_item->orig.psz )) )
1224                     fputc ('#', file);
1225                 fprintf( file, "%s=%s\n", p_item->psz_name,
1226                          psz_value ?: "" );
1227
1228                 if( b_autosave && !p_item->b_autosave ) break;
1229
1230                 freenull (p_item->saved.psz);
1231                 if( (psz_value && p_item->orig.psz &&
1232                      strcmp( psz_value, p_item->orig.psz )) ||
1233                     !psz_value || !p_item->orig.psz)
1234                     p_item->saved.psz = strdupnull (psz_value);
1235                 else
1236                     p_item->saved.psz = NULL;
1237             }
1238         }
1239
1240         fputc ('\n', file);
1241     }
1242
1243     vlc_list_release( p_list );
1244
1245     /*
1246      * Restore old settings from the config in file
1247      */
1248     fputs( p_bigbuffer, file );
1249     free( p_bigbuffer );
1250
1251     fclose( file );
1252     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1253
1254     return 0;
1255 }
1256
1257 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1258 {
1259     vlc_list_t *p_list;
1260     int i_index, i_count;
1261
1262     if( !p_this ) return -1;
1263
1264     /* Check if there's anything to save */
1265     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
1266     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1267     i_count = p_list->i_count;
1268     for( i_index = 0; i_index < i_count; i_index++ )
1269     {
1270         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1271         module_config_t *p_item, *p_end;
1272
1273         if( !p_parser->i_config_items ) continue;
1274
1275         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1276              p_item < p_end;
1277              p_item++ )
1278         {
1279             if( p_item->b_autosave && p_item->b_dirty ) break;
1280         }
1281         break;
1282     }
1283     vlc_list_release( p_list );
1284     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
1285
1286     if( i_index == i_count ) return VLC_SUCCESS;
1287     return SaveConfigFile( p_this, 0, VLC_TRUE );
1288 }
1289
1290 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1291 {
1292     return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1293 }
1294
1295 /*****************************************************************************
1296  * config_LoadCmdLine: parse command line
1297  *****************************************************************************
1298  * Parse command line for configuration options.
1299  * Now that the module_bank has been initialized, we can dynamically
1300  * generate the longopts structure used by getops. We have to do it this way
1301  * because we don't know (and don't want to know) in advance the configuration
1302  * options used (ie. exported) by each module.
1303  *****************************************************************************/
1304 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1305                           vlc_bool_t b_ignore_errors )
1306 {
1307     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1308     module_t *p_parser;
1309     vlc_list_t *p_list;
1310     struct option *p_longopts;
1311     int i_modules_index;
1312
1313     /* Short options */
1314     module_config_t *pp_shortopts[256];
1315     char *psz_shortopts;
1316
1317     /* Set default configuration and copy arguments */
1318     p_this->p_libvlc->i_argc    = *pi_argc;
1319     p_this->p_libvlc->ppsz_argv = ppsz_argv;
1320
1321 #ifdef __APPLE__
1322     /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
1323      * is the PSN - process serial number (a unique PID-ish thingie)
1324      * still ok for real Darwin & when run from command line */
1325     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1326                                         /* for example -psn_0_9306113 */
1327     {
1328         /* GDMF!... I can't do this or else the MacOSX window server will
1329          * not pick up the PSN and not register the app and we crash...
1330          * hence the following kludge otherwise we'll get confused w/ argv[1]
1331          * being an input file name */
1332 #if 0
1333         ppsz_argv[ 1 ] = NULL;
1334 #endif
1335         *pi_argc = *pi_argc - 1;
1336         pi_argc--;
1337         return 0;
1338     }
1339 #endif
1340
1341     /* List all modules */
1342     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1343
1344     /*
1345      * Generate the longopts and shortopts structures used by getopt_long
1346      */
1347
1348     i_opts = 0;
1349     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1350          i_modules_index++ )
1351     {
1352         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1353
1354         /* count the number of exported configuration options (to allocate
1355          * longopts). We also need to allocate space for two options when
1356          * dealing with boolean to allow for --foo and --no-foo */
1357         i_opts += p_parser->i_config_items
1358                      + 2 * p_parser->i_bool_items;
1359     }
1360
1361     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1362     if( p_longopts == NULL )
1363     {
1364         msg_Err( p_this, "out of memory" );
1365         vlc_list_release( p_list );
1366         return -1;
1367     }
1368
1369     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1370     if( psz_shortopts == NULL )
1371     {
1372         msg_Err( p_this, "out of memory" );
1373         free( p_longopts );
1374         vlc_list_release( p_list );
1375         return -1;
1376     }
1377
1378     /* If we are requested to ignore errors, then we must work on a copy
1379      * of the ppsz_argv array, otherwise getopt_long will reorder it for
1380      * us, ignoring the arity of the options */
1381     if( b_ignore_errors )
1382     {
1383         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1384         if( ppsz_argv == NULL )
1385         {
1386             msg_Err( p_this, "out of memory" );
1387             free( psz_shortopts );
1388             free( p_longopts );
1389             vlc_list_release( p_list );
1390             return -1;
1391         }
1392         memcpy( ppsz_argv, p_this->p_libvlc->ppsz_argv,
1393                 *pi_argc * sizeof(char *) );
1394     }
1395
1396     i_shortopts = 0;
1397     for( i_index = 0; i_index < 256; i_index++ )
1398     {
1399         pp_shortopts[i_index] = NULL;
1400     }
1401
1402     /* Fill the p_longopts and psz_shortopts structures */
1403     i_index = 0;
1404     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1405          i_modules_index++ )
1406     {
1407         module_config_t *p_item, *p_end;
1408         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1409
1410         if( !p_parser->i_config_items )
1411             continue;
1412
1413         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
1414              p_item < p_end;
1415              p_item++ )
1416         {
1417             /* Ignore hints */
1418             if( p_item->i_type & CONFIG_HINT )
1419                 continue;
1420
1421             /* Add item to long options */
1422             p_longopts[i_index].name = strdup( p_item->psz_name );
1423             if( p_longopts[i_index].name == NULL ) continue;
1424             p_longopts[i_index].has_arg =
1425                 (p_item->i_type == CONFIG_ITEM_BOOL)?
1426                                                no_argument : required_argument;
1427             p_longopts[i_index].flag = &flag;
1428             p_longopts[i_index].val = 0;
1429             i_index++;
1430
1431             /* When dealing with bools we also need to add the --no-foo
1432              * option */
1433             if( p_item->i_type == CONFIG_ITEM_BOOL )
1434             {
1435                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1436                 if( psz_name == NULL ) continue;
1437                 strcpy( psz_name, "no" );
1438                 strcat( psz_name, p_item->psz_name );
1439
1440                 p_longopts[i_index].name = psz_name;
1441                 p_longopts[i_index].has_arg = no_argument;
1442                 p_longopts[i_index].flag = &flag;
1443                 p_longopts[i_index].val = 1;
1444                 i_index++;
1445
1446                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1447                 if( psz_name == NULL ) continue;
1448                 strcpy( psz_name, "no-" );
1449                 strcat( psz_name, p_item->psz_name );
1450
1451                 p_longopts[i_index].name = psz_name;
1452                 p_longopts[i_index].has_arg = no_argument;
1453                 p_longopts[i_index].flag = &flag;
1454                 p_longopts[i_index].val = 1;
1455                 i_index++;
1456             }
1457
1458             /* If item also has a short option, add it */
1459             if( p_item->i_short )
1460             {
1461                 pp_shortopts[(int)p_item->i_short] = p_item;
1462                 psz_shortopts[i_shortopts] = p_item->i_short;
1463                 i_shortopts++;
1464                 if( p_item->i_type != CONFIG_ITEM_BOOL )
1465                 {
1466                     psz_shortopts[i_shortopts] = ':';
1467                     i_shortopts++;
1468
1469                     if( p_item->i_short == 'v' )
1470                     {
1471                         psz_shortopts[i_shortopts] = ':';
1472                         i_shortopts++;
1473                     }
1474                 }
1475             }
1476         }
1477     }
1478
1479     /* We don't need the module list anymore */
1480     vlc_list_release( p_list );
1481
1482     /* Close the longopts and shortopts structures */
1483     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1484     psz_shortopts[i_shortopts] = '\0';
1485
1486     /*
1487      * Parse the command line options
1488      */
1489     opterr = 0;
1490     optind = 0; /* set to 0 to tell GNU getopt to reinitialize */
1491     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1492                                   p_longopts, &i_index ) ) != -1 )
1493     {
1494         /* A long option has been recognized */
1495         if( i_cmd == 0 )
1496         {
1497             module_config_t *p_conf;
1498             char *psz_name = (char *)p_longopts[i_index].name;
1499
1500             /* Check if we deal with a --nofoo or --no-foo long option */
1501             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1502
1503             /* Store the configuration option */
1504             p_conf = config_FindConfig( p_this, psz_name );
1505             if( p_conf )
1506             {
1507                 /* Check if the option is deprecated */
1508                 if( p_conf->psz_current )
1509                 {
1510                     if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1511                     {
1512                         if( !b_ignore_errors )
1513                         {
1514                             fprintf(stderr,
1515                                     "Warning: option --%s is no longer used.\n",
1516                                     p_conf->psz_name);
1517                         }
1518                        continue;
1519                     }
1520                     if( !b_ignore_errors )
1521                     {
1522                         if( p_conf->b_strict )
1523                         {
1524                             fprintf( stderr,
1525                                      "Error: option --%s is deprecated. "
1526                                      "Use --%s instead.\n",
1527                                      p_conf->psz_name, p_conf->psz_current);
1528                             /*free */
1529                             for( i_index = 0; p_longopts[i_index].name; i_index++ )
1530                                 free( (char *)p_longopts[i_index].name );
1531
1532                             free( p_longopts );
1533                             free( psz_shortopts );
1534                             return -1;
1535                         }
1536                         fprintf(stderr,
1537                                 "Warning: option --%s is deprecated. "
1538                                 "You should use --%s instead.\n",
1539                                 p_conf->psz_name, p_conf->psz_current);
1540                     }
1541                     psz_name = (char *)p_conf->psz_current;
1542                     p_conf = config_FindConfig( p_this, psz_name );
1543                 }
1544
1545                 switch( p_conf->i_type )
1546                 {
1547                     case CONFIG_ITEM_STRING:
1548                     case CONFIG_ITEM_FILE:
1549                     case CONFIG_ITEM_DIRECTORY:
1550                     case CONFIG_ITEM_MODULE:
1551                     case CONFIG_ITEM_MODULE_LIST:
1552                     case CONFIG_ITEM_MODULE_LIST_CAT:
1553                     case CONFIG_ITEM_MODULE_CAT:
1554                         config_PutPsz( p_this, psz_name, optarg );
1555                         break;
1556                     case CONFIG_ITEM_INTEGER:
1557                         config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1558                         break;
1559                     case CONFIG_ITEM_FLOAT:
1560                         config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1561                         break;
1562                     case CONFIG_ITEM_KEY:
1563                         config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1564                         break;
1565                     case CONFIG_ITEM_BOOL:
1566                         config_PutInt( p_this, psz_name, !flag );
1567                         break;
1568                 }
1569                 continue;
1570             }
1571         }
1572
1573         /* A short option has been recognized */
1574         if( pp_shortopts[i_cmd] != NULL )
1575         {
1576             switch( pp_shortopts[i_cmd]->i_type )
1577             {
1578                 case CONFIG_ITEM_STRING:
1579                 case CONFIG_ITEM_FILE:
1580                 case CONFIG_ITEM_DIRECTORY:
1581                 case CONFIG_ITEM_MODULE:
1582                 case CONFIG_ITEM_MODULE_CAT:
1583                 case CONFIG_ITEM_MODULE_LIST:
1584                 case CONFIG_ITEM_MODULE_LIST_CAT:
1585                     config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1586                     break;
1587                 case CONFIG_ITEM_INTEGER:
1588                     if( i_cmd == 'v' )
1589                     {
1590                         if( optarg )
1591                         {
1592                             if( *optarg == 'v' ) /* eg. -vvv */
1593                             {
1594                                 i_verbose++;
1595                                 while( *optarg == 'v' )
1596                                 {
1597                                     i_verbose++;
1598                                     optarg++;
1599                                 }
1600                             }
1601                             else
1602                             {
1603                                 i_verbose += atoi( optarg ); /* eg. -v2 */
1604                             }
1605                         }
1606                         else
1607                         {
1608                             i_verbose++; /* -v */
1609                         }
1610                         config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1611                                                i_verbose );
1612                     }
1613                     else
1614                     {
1615                         config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1616                                                strtol(optarg, 0, 0) );
1617                     }
1618                     break;
1619                 case CONFIG_ITEM_BOOL:
1620                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1621                     break;
1622             }
1623
1624             continue;
1625         }
1626
1627         /* Internal error: unknown option */
1628         if( !b_ignore_errors )
1629         {
1630             fprintf( stderr, "%s: unknown option"
1631                      " or missing mandatory argument ",
1632                      p_this->p_libvlc->psz_object_name );
1633             if( optopt )
1634             {
1635                 fprintf( stderr, "`-%c'\n", optopt );
1636             }
1637             else
1638             {
1639                 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1640             }
1641             fprintf( stderr, "Try `%s --help' for more information.\n",
1642                              p_this->p_libvlc->psz_object_name );
1643
1644             for( i_index = 0; p_longopts[i_index].name; i_index++ )
1645                 free( (char *)p_longopts[i_index].name );
1646             free( p_longopts );
1647             free( psz_shortopts );
1648             return -1;
1649         }
1650     }
1651
1652     /* Free allocated resources */
1653     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1654         free( (char *)p_longopts[i_index].name );
1655     free( p_longopts );
1656     free( psz_shortopts );
1657     if( b_ignore_errors ) free( ppsz_argv );
1658
1659     return 0;
1660 }
1661
1662 /**
1663  * config_GetDataDir: find directory where shared data is installed
1664  *
1665  * @return a string (always succeeds).
1666  */
1667 const char *config_GetDataDir( const vlc_object_t *p_this )
1668 {
1669 #if defined (WIN32) || defined (UNDER_CE)
1670     return p_this->p_libvlc_global->psz_vlcpath;
1671 #elif defined(__APPLE__) || defined (SYS_BEOS)
1672     static char path[PATH_MAX] = "";
1673
1674     if( *path == '\0' )
1675     {
1676         snprintf( path, sizeof( path ), "%s/share",
1677                   p_this->p_libvlc_global->psz_vlcpath );
1678         path[sizeof( path ) - 1] = '\0';
1679     }
1680     return path;
1681 #else
1682     (void)p_this;
1683     return DATA_PATH;
1684 #endif
1685 }
1686
1687 /*****************************************************************************
1688  * config_GetHomeDir, config_GetUserDir: find the user's home directory.
1689  *****************************************************************************
1690  * This function will try by different ways to find the user's home path.
1691  * Note that this function is not reentrant, it should be called only once
1692  * at the beginning of main where the result will be stored for later use.
1693  *****************************************************************************/
1694 static char *GetDir( vlc_bool_t b_appdata )
1695 {
1696     const char *psz_localhome = NULL;
1697
1698 #if defined(WIN32) && !defined(UNDER_CE)
1699     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1700                                                LPWSTR );
1701 #ifndef CSIDL_FLAG_CREATE
1702 #   define CSIDL_FLAG_CREATE 0x8000
1703 #endif
1704 #ifndef CSIDL_APPDATA
1705 #   define CSIDL_APPDATA 0x1A
1706 #endif
1707 #ifndef CSIDL_PROFILE
1708 #   define CSIDL_PROFILE 0x28
1709 #endif
1710 #ifndef SHGFP_TYPE_CURRENT
1711 #   define SHGFP_TYPE_CURRENT 0
1712 #endif
1713
1714     HINSTANCE shfolder_dll;
1715     SHGETFOLDERPATH SHGetFolderPath ;
1716
1717     /* load the shfolder dll to retrieve SHGetFolderPath */
1718     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1719     {
1720         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1721                                                   _T("SHGetFolderPathW") );
1722         if ( SHGetFolderPath != NULL )
1723         {
1724             wchar_t whomedir[MAX_PATH];
1725
1726             /* get the "Application Data" folder for the current user */
1727             if( S_OK == SHGetFolderPath( NULL,
1728                                          (b_appdata ? CSIDL_APPDATA :
1729                                            CSIDL_PROFILE) | CSIDL_FLAG_CREATE,
1730                                          NULL, SHGFP_TYPE_CURRENT,
1731                                          whomedir ) )
1732             {
1733                 FreeLibrary( shfolder_dll );
1734                 return FromWide( whomedir );
1735             }
1736         }
1737         FreeLibrary( shfolder_dll );
1738     }
1739
1740 #elif defined(UNDER_CE)
1741
1742 #ifndef CSIDL_APPDATA
1743 #   define CSIDL_APPDATA 0x1A
1744 #endif
1745
1746     wchar_t whomedir[MAX_PATH];
1747
1748     /* get the "Application Data" folder for the current user */
1749     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
1750         return FromWide( whomedir );
1751 #endif
1752
1753 #if defined(HAVE_GETPWUID)
1754     struct passwd *p_pw;
1755     (void)b_appdata;
1756
1757     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1758 #endif
1759     {
1760         psz_localhome = getenv( "HOME" );
1761         if( psz_localhome == NULL )
1762         {
1763             psz_localhome = getenv( "TMP" );
1764             if( psz_localhome == NULL )
1765                 psz_localhome = "/tmp";
1766         }
1767     }
1768 #if defined(HAVE_GETPWUID)
1769     else
1770         psz_localhome = p_pw->pw_dir;
1771 #endif
1772
1773     return FromLocaleDup( psz_localhome );
1774 }
1775
1776 char *config_GetHomeDir( void )
1777 {
1778     return GetDir( VLC_TRUE );
1779 }
1780
1781 char *config_GetUserDir( void )
1782 {
1783     return GetDir( VLC_FALSE );
1784 }
1785
1786
1787 static int ConfigStringToKey( const char *psz_key )
1788 {
1789     int i_key = 0;
1790     unsigned int i;
1791     const char *psz_parser = strchr( psz_key, '-' );
1792     while( psz_parser && psz_parser != psz_key )
1793     {
1794         for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1795         {
1796             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1797                               strlen( vlc_modifiers[i].psz_key_string ) ) )
1798             {
1799                 i_key |= vlc_modifiers[i].i_key_code;
1800             }
1801         }
1802         psz_key = psz_parser + 1;
1803         psz_parser = strchr( psz_key, '-' );
1804     }
1805     for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1806     {
1807         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1808         {
1809             i_key |= vlc_keys[i].i_key_code;
1810             break;
1811         }
1812     }
1813     return i_key;
1814 }
1815
1816 static char *ConfigKeyToString( int i_key )
1817 {
1818     char *psz_key = malloc( 100 );
1819     char *p;
1820     size_t index;
1821
1822     if ( !psz_key )
1823     {
1824         return NULL;
1825     }
1826     *psz_key = '\0';
1827     p = psz_key;
1828     for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1829          index++ )
1830     {
1831         if( i_key & vlc_modifiers[index].i_key_code )
1832         {
1833             p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1834         }
1835     }
1836     for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1837          index++)
1838     {
1839         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1840         {
1841             p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
1842             break;
1843         }
1844     }
1845     return psz_key;
1846 }