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