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