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