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