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