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