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