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