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