]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
c45499856363e14190eea1b80e1c3b009c6e1678
[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     setlocale( LC_NUMERIC, "C" );
762
763     psz_configfile = p_this->p_vlc->psz_configfile;
764     if( !psz_configfile || !psz_configfile )
765     {
766         psz_homedir = p_this->p_vlc->psz_homedir;
767         if( !psz_homedir )
768         {
769             msg_Err( p_this, "psz_homedir is null" );
770             return -1;
771         }
772         psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
773                                        strlen(psz_homedir) );
774         if( psz_filename )
775             sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE,
776                      psz_homedir );
777     }
778     else
779     {
780         psz_filename = strdup( psz_configfile );
781     }
782
783     if( !psz_filename )
784     {
785         msg_Err( p_this, "out of memory" );
786         return -1;
787     }
788
789     msg_Dbg( p_this, "opening config file %s", psz_filename );
790
791     /* Acquire config file lock */
792     vlc_mutex_lock( &p_this->p_vlc->config_lock );
793
794     file = fopen( psz_filename, "rt" );
795     if( !file )
796     {
797         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
798         free( psz_filename );
799         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
800         return -1;
801     }
802
803     /* Look for the selected module, if NULL then save everything */
804     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
805
806     for( i_index = 0; i_index < p_list->i_count; i_index++ )
807     {
808         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
809
810         if( psz_module_name
811              && strcmp( psz_module_name, p_parser->psz_object_name ) )
812         {
813             continue;
814         }
815
816         /* The config file is organized in sections, one per module. Look for
817          * the interesting section ( a section is of the form [foo] ) */
818         fseek( file, 0L, SEEK_SET );
819         while( fgets( line, 1024, file ) )
820         {
821             if( (line[0] == '[')
822                && (p_index = strchr(line,']'))
823                && (p_index - &line[1]
824                     == (int)strlen(p_parser->psz_object_name))
825                && !memcmp( &line[1], p_parser->psz_object_name,
826                            strlen(p_parser->psz_object_name) ) )
827             {
828 #if 0
829                 msg_Dbg( p_this, "loading config for module \"%s\"",
830                                  p_parser->psz_object_name );
831 #endif
832
833                 break;
834             }
835         }
836         /* either we found the section or we're at the EOF */
837
838         /* Now try to load the options in this section */
839         while( fgets( line, 1024, file ) )
840         {
841             if( line[0] == '[' ) break; /* end of section */
842
843             /* ignore comments or empty lines */
844             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
845                 continue;
846
847             /* get rid of line feed */
848             if( line[strlen(line)-1] == '\n' )
849                 line[strlen(line)-1] = (char)0;
850
851             /* look for option name */
852             psz_option_name = line;
853             psz_option_value = NULL;
854             p_index = strchr( line, '=' );
855             if( !p_index ) break; /* this ain't an option!!! */
856
857             *p_index = (char)0;
858             psz_option_value = p_index + 1;
859
860             if( !p_parser->i_config_items )
861             {
862                 continue;
863             }
864
865             /* try to match this option with one of the module's options */
866             for( p_item = p_parser->p_config;
867                  p_item->i_type != CONFIG_HINT_END;
868                  p_item++ )
869             {
870                 if( p_item->i_type & CONFIG_HINT )
871                     /* ignore hints */
872                     continue;
873
874                 if( !strcmp( p_item->psz_name, psz_option_name ) )
875                 {
876                     /* We found it */
877                     switch( p_item->i_type )
878                     {
879                     case CONFIG_ITEM_BOOL:
880                     case CONFIG_ITEM_INTEGER:
881                         if( !*psz_option_value )
882                             break;                    /* ignore empty option */
883                         p_item->i_value = strtol( psz_option_value, 0, 0 );
884                         p_item->i_value_saved = p_item->i_value;
885 #if 0
886                         msg_Dbg( p_this, "option \"%s\", value %i",
887                                  p_item->psz_name, p_item->i_value );
888 #endif
889                         break;
890
891                     case CONFIG_ITEM_FLOAT:
892                         if( !*psz_option_value )
893                             break;                    /* ignore empty option */
894                         p_item->f_value = (float)atof( psz_option_value);
895                         p_item->f_value_saved = p_item->f_value;
896 #if 0
897                         msg_Dbg( p_this, "option \"%s\", value %f",
898                                  p_item->psz_name, (double)p_item->f_value );
899 #endif
900                         break;
901                     case CONFIG_ITEM_KEY:
902                         if( !*psz_option_value )
903                             break;                    /* ignore empty option */
904                         p_item->i_value = ConfigStringToKey(psz_option_value);
905                         p_item->i_value_saved = p_item->i_value;
906                         break;
907
908                     default:
909                         vlc_mutex_lock( p_item->p_lock );
910
911                         /* free old string */
912                         if( p_item->psz_value )
913                             free( p_item->psz_value );
914
915                         p_item->psz_value = *psz_option_value ?
916                             strdup( psz_option_value ) : NULL;
917
918                         if( p_item->psz_value_saved )
919                             free( p_item->psz_value_saved );
920                         p_item->psz_value_saved = 0;
921                         if( !p_item->psz_value || !p_item->psz_value_orig ||
922                             (p_item->psz_value && p_item->psz_value_orig &&
923                              strcmp(p_item->psz_value,p_item->psz_value_orig)))
924                             p_item->psz_value_saved = p_item->psz_value ?
925                                 strdup( p_item->psz_value ) : 0;
926
927                         vlc_mutex_unlock( p_item->p_lock );
928
929 #if 0
930                         msg_Dbg( p_this, "option \"%s\", value \"%s\"",
931                                  p_item->psz_name,
932                                  p_item->psz_value ? p_item->psz_value : "" );
933 #endif
934                         break;
935                     }
936                 }
937             }
938         }
939
940     }
941
942     vlc_list_release( p_list );
943
944     fclose( file );
945     free( psz_filename );
946
947     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
948
949     return 0;
950 }
951
952 /*****************************************************************************
953  * config_CreateDir: Create configuration directory if it doesn't exist.
954  *****************************************************************************/
955 int config_CreateDir( vlc_object_t *p_this, char *psz_dirname )
956 {
957     if( !psz_dirname && !*psz_dirname ) return -1;
958
959 #if defined( UNDER_CE )
960     {
961         wchar_t psz_new[ MAX_PATH ];
962         char psz_mod[MAX_PATH];
963         int i = 0;
964
965         /* Convert '/' into '\' */
966         while( *psz_dirname )
967         {
968             if( *psz_dirname == '/' ) psz_mod[i] = '\\';
969             else psz_mod[i] = *psz_dirname;
970             psz_dirname++;
971             i++;
972         }
973         psz_mod[i] = 0;
974
975         MultiByteToWideChar( CP_ACP, 0, psz_mod, -1, psz_new, MAX_PATH );
976         if( CreateDirectory( psz_new, NULL ) )
977         {
978             msg_Err( p_this, "could not create %s", psz_mod );
979         }
980     }
981
982 #else
983 #   if defined( WIN32 )
984     if( mkdir( psz_dirname ) && errno != EEXIST )
985 #   else
986     if( mkdir( psz_dirname, 0755 ) && errno != EEXIST )
987 #   endif
988     {
989         msg_Err( p_this, "could not create %s (%s)",
990                  psz_dirname, strerror(errno) );
991     }
992
993 #endif
994
995     return 0;
996 }
997
998 /*****************************************************************************
999  * config_SaveConfigFile: Save a module's config options.
1000  *****************************************************************************
1001  * This will save the specified module's config options to the config file.
1002  * If psz_module_name is NULL then we save all the modules config options.
1003  * It's no use to save the config options that kept their default values, so
1004  * we'll try to be a bit clever here.
1005  *
1006  * When we save we mustn't delete the config options of the modules that
1007  * haven't been loaded. So we cannot just create a new config file with the
1008  * config structures we've got in memory.
1009  * I don't really know how to deal with this nicely, so I will use a completly
1010  * dumb method ;-)
1011  * I will load the config file in memory, but skipping all the sections of the
1012  * modules we want to save. Then I will create a brand new file, dump the file
1013  * loaded in memory and then append the sections of the modules we want to
1014  * save.
1015  * Really stupid no ?
1016  *****************************************************************************/
1017 int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
1018                     vlc_bool_t b_autosave )
1019 {
1020     module_t *p_parser;
1021     vlc_list_t *p_list;
1022     module_config_t *p_item;
1023     FILE *file;
1024     char p_line[1024], *p_index2;
1025     int i_sizebuf = 0;
1026     char *p_bigbuffer, *p_index;
1027     vlc_bool_t b_backup;
1028     char *psz_filename, *psz_homedir, *psz_configfile;
1029     int i_index;
1030
1031     setlocale( LC_NUMERIC, "C" );
1032
1033     /* Acquire config file lock */
1034     vlc_mutex_lock( &p_this->p_vlc->config_lock );
1035
1036     psz_configfile = p_this->p_vlc->psz_configfile;
1037     if( !psz_configfile || !psz_configfile )
1038     {
1039         psz_homedir = p_this->p_vlc->psz_homedir;
1040         if( !psz_homedir )
1041         {
1042             msg_Err( p_this, "psz_homedir is null" );
1043             vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1044             return -1;
1045         }
1046         psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
1047                                        strlen(psz_homedir) );
1048
1049         if( psz_filename )
1050             sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1051
1052         if( !psz_filename )
1053         {
1054             msg_Err( p_this, "out of memory" );
1055             vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1056             return -1;
1057         }
1058
1059         config_CreateDir( p_this, psz_filename );
1060
1061         strcat( psz_filename, "/" CONFIG_FILE );
1062     }
1063     else
1064     {
1065         psz_filename = strdup( psz_configfile );
1066         if( !psz_filename )
1067         {
1068             msg_Err( p_this, "out of memory" );
1069             vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1070             return -1;
1071         }
1072     }
1073
1074     msg_Dbg( p_this, "opening config file %s", psz_filename );
1075
1076     file = fopen( psz_filename, "rt" );
1077     if( !file )
1078     {
1079         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
1080     }
1081     else
1082     {
1083         /* look for file size */
1084         fseek( file, 0L, SEEK_END );
1085         i_sizebuf = ftell( file );
1086         fseek( file, 0L, SEEK_SET );
1087     }
1088
1089     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
1090     if( !p_bigbuffer )
1091     {
1092         msg_Err( p_this, "out of memory" );
1093         if( file ) fclose( file );
1094         free( psz_filename );
1095         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1096         return -1;
1097     }
1098     p_bigbuffer[0] = 0;
1099
1100     /* List all available modules */
1101     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1102
1103     /* backup file into memory, we only need to backup the sections we won't
1104      * save later on */
1105     b_backup = 0;
1106     while( file && fgets( p_line, 1024, file ) )
1107     {
1108         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
1109         {
1110
1111             /* we found a section, check if we need to do a backup */
1112             for( i_index = 0; i_index < p_list->i_count; i_index++ )
1113             {
1114                 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1115
1116                 if( ((p_index2 - &p_line[1])
1117                        == (int)strlen(p_parser->psz_object_name) )
1118                     && !memcmp( &p_line[1], p_parser->psz_object_name,
1119                                 strlen(p_parser->psz_object_name) ) )
1120                 {
1121                     if( !psz_module_name )
1122                         break;
1123                     else if( !strcmp( psz_module_name,
1124                                       p_parser->psz_object_name ) )
1125                         break;
1126                 }
1127             }
1128
1129             if( i_index == p_list->i_count )
1130             {
1131                 /* we don't have this section in our list so we need to back
1132                  * it up */
1133                 *p_index2 = 0;
1134 #if 0
1135                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
1136                                  &p_line[1] );
1137 #endif
1138                 *p_index2 = ']';
1139
1140                 b_backup = 1;
1141             }
1142             else
1143             {
1144                 b_backup = 0;
1145             }
1146         }
1147
1148         /* save line if requested and line is valid (doesn't begin with a
1149          * space, tab, or eol) */
1150         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
1151             && (p_line[0] != '\t') )
1152         {
1153             strcpy( p_index, p_line );
1154             p_index += strlen( p_line );
1155         }
1156     }
1157     if( file ) fclose( file );
1158
1159
1160     /*
1161      * Save module config in file
1162      */
1163
1164     file = fopen( psz_filename, "wt" );
1165     if( !file )
1166     {
1167         msg_Warn( p_this, "could not open config file %s for writing",
1168                           psz_filename );
1169         free( psz_filename );
1170         vlc_list_release( p_list );
1171         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1172         return -1;
1173     }
1174
1175     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
1176
1177     /* Look for the selected module, if NULL then save everything */
1178     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1179     {
1180         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1181
1182         if( psz_module_name && strcmp( psz_module_name,
1183                                        p_parser->psz_object_name ) )
1184             continue;
1185
1186         if( !p_parser->i_config_items )
1187             continue;
1188
1189         if( psz_module_name )
1190             msg_Dbg( p_this, "saving config for module \"%s\"",
1191                      p_parser->psz_object_name );
1192
1193         fprintf( file, "[%s]", p_parser->psz_object_name );
1194         if( p_parser->psz_longname )
1195             fprintf( file, " # %s\n\n", p_parser->psz_longname );
1196         else
1197             fprintf( file, "\n\n" );
1198
1199         for( p_item = p_parser->p_config;
1200              p_item->i_type != CONFIG_HINT_END;
1201              p_item++ )
1202         {
1203             char  *psz_key;
1204             int   i_value = p_item->i_value;
1205             float f_value = p_item->f_value;
1206             char  *psz_value = p_item->psz_value;
1207
1208             if( p_item->i_type & CONFIG_HINT )
1209                 /* ignore hints */
1210                 continue;
1211             /* Ignore deprecated options */
1212             if( p_item->psz_current )
1213                 continue;
1214             if( b_autosave && !p_item->b_autosave )
1215             {
1216                 i_value = p_item->i_value_saved;
1217                 f_value = p_item->f_value_saved;
1218                 psz_value = p_item->psz_value_saved;
1219                 if( !psz_value ) psz_value = p_item->psz_value_orig;
1220             }
1221             else
1222             {
1223                 p_item->b_dirty = VLC_FALSE;
1224             }
1225
1226             switch( p_item->i_type )
1227             {
1228             case CONFIG_ITEM_BOOL:
1229             case CONFIG_ITEM_INTEGER:
1230                 if( p_item->psz_text )
1231                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1232                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
1233                              _("boolean") : _("integer") );
1234                 if( i_value == p_item->i_value_orig )
1235                     fprintf( file, "#" );
1236                 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
1237
1238                 p_item->i_value_saved = i_value;
1239                 break;
1240
1241             case CONFIG_ITEM_KEY:
1242                 if( p_item->psz_text )
1243                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1244                              _("key") );
1245                 if( i_value == p_item->i_value_orig )
1246                     fprintf( file, "#" );
1247                 psz_key = ConfigKeyToString( i_value );
1248                 fprintf( file, "%s=%s\n", p_item->psz_name,
1249                          psz_key ? psz_key : "" );
1250                 if ( psz_key ) free( psz_key );
1251
1252                 p_item->i_value_saved = i_value;
1253                 break;
1254
1255             case CONFIG_ITEM_FLOAT:
1256                 if( p_item->psz_text )
1257                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1258                              _("float") );
1259                 if( f_value == p_item->f_value_orig )
1260                     fprintf( file, "#" );
1261                 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
1262
1263                 p_item->f_value_saved = f_value;
1264                 break;
1265
1266             default:
1267                 if( p_item->psz_text )
1268                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
1269                              _("string") );
1270                 if( (!psz_value && !p_item->psz_value_orig) ||
1271                     (psz_value && p_item->psz_value_orig &&
1272                      !strcmp( psz_value, p_item->psz_value_orig )) )
1273                     fprintf( file, "#" );
1274                 fprintf( file, "%s=%s\n", p_item->psz_name,
1275                          psz_value ? psz_value : "" );
1276
1277                 if( b_autosave && !p_item->b_autosave ) break;
1278
1279                 if( p_item->psz_value_saved ) free( p_item->psz_value_saved );
1280                 p_item->psz_value_saved = 0;
1281                 if( (psz_value && p_item->psz_value_orig &&
1282                      strcmp( psz_value, p_item->psz_value_orig )) ||
1283                     !psz_value || !p_item->psz_value_orig)
1284                     p_item->psz_value_saved = psz_value ? strdup(psz_value):0;
1285             }
1286         }
1287
1288         fprintf( file, "\n" );
1289     }
1290
1291     vlc_list_release( p_list );
1292
1293     /*
1294      * Restore old settings from the config in file
1295      */
1296     fputs( p_bigbuffer, file );
1297     free( p_bigbuffer );
1298
1299     fclose( file );
1300     free( psz_filename );
1301     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1302
1303     return 0;
1304 }
1305
1306 int config_AutoSaveConfigFile( vlc_object_t *p_this )
1307 {
1308     vlc_list_t *p_list;
1309     module_t *p_parser;
1310     module_config_t *p_item;
1311     int i_index, i_count;
1312
1313     /* Check if there's anything to save */
1314     vlc_mutex_lock( &p_this->p_vlc->config_lock );
1315     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1316     i_count = p_list->i_count;
1317     for( i_index = 0; i_index < i_count; i_index++ )
1318     {
1319         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1320
1321         if( !p_parser->i_config_items ) continue;
1322
1323         for( p_item = p_parser->p_config;
1324              p_item->i_type != CONFIG_HINT_END;
1325              p_item++ )
1326         {
1327             if( p_item->b_autosave && p_item->b_dirty ) break;
1328         }
1329         if( p_item->i_type != CONFIG_HINT_END ) break;
1330     }
1331     vlc_list_release( p_list );
1332     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
1333
1334     if( i_index == i_count ) return VLC_SUCCESS;
1335     return SaveConfigFile( p_this, 0, VLC_TRUE );
1336 }
1337
1338 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
1339 {
1340     return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
1341 }
1342
1343 /*****************************************************************************
1344  * config_LoadCmdLine: parse command line
1345  *****************************************************************************
1346  * Parse command line for configuration options.
1347  * Now that the module_bank has been initialized, we can dynamically
1348  * generate the longopts structure used by getops. We have to do it this way
1349  * because we don't know (and don't want to know) in advance the configuration
1350  * options used (ie. exported) by each module.
1351  *****************************************************************************/
1352 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
1353                           vlc_bool_t b_ignore_errors )
1354 {
1355     int i_cmd, i_index, i_opts, i_shortopts, flag, i_verbose = 0;
1356     module_t *p_parser;
1357     vlc_list_t *p_list;
1358     module_config_t *p_item;
1359     struct option *p_longopts;
1360     int i_modules_index;
1361
1362     /* Short options */
1363     module_config_t *pp_shortopts[256];
1364     char *psz_shortopts;
1365
1366     /* Set default configuration and copy arguments */
1367     p_this->p_vlc->i_argc    = *pi_argc;
1368     p_this->p_vlc->ppsz_argv = ppsz_argv;
1369
1370 #ifdef SYS_DARWIN
1371     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
1372      * is the PSN - process serial number (a unique PID-ish thingie)
1373      * still ok for real Darwin & when run from command line */
1374     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
1375                                         /* for example -psn_0_9306113 */
1376     {
1377         /* GDMF!... I can't do this or else the MacOSX window server will
1378          * not pick up the PSN and not register the app and we crash...
1379          * hence the following kludge otherwise we'll get confused w/ argv[1]
1380          * being an input file name */
1381 #if 0
1382         ppsz_argv[ 1 ] = NULL;
1383 #endif
1384         *pi_argc = *pi_argc - 1;
1385         pi_argc--;
1386         return 0;
1387     }
1388 #endif
1389
1390     /* List all modules */
1391     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1392
1393     /*
1394      * Generate the longopts and shortopts structures used by getopt_long
1395      */
1396
1397     i_opts = 0;
1398     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1399          i_modules_index++ )
1400     {
1401         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1402
1403         /* count the number of exported configuration options (to allocate
1404          * longopts). We also need to allocate space for too options when
1405          * dealing with boolean to allow for --foo and --no-foo */
1406         i_opts += p_parser->i_config_items
1407                      + 2 * p_parser->i_bool_items;
1408     }
1409
1410     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
1411     if( p_longopts == NULL )
1412     {
1413         msg_Err( p_this, "out of memory" );
1414         vlc_list_release( p_list );
1415         return -1;
1416     }
1417
1418     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
1419     if( psz_shortopts == NULL )
1420     {
1421         msg_Err( p_this, "out of memory" );
1422         free( p_longopts );
1423         vlc_list_release( p_list );
1424         return -1;
1425     }
1426
1427     /* If we are requested to ignore errors, then we must work on a copy
1428      * of the ppsz_argv array, otherwise getopt_long will reorder it for
1429      * us, ignoring the arity of the options */
1430     if( b_ignore_errors )
1431     {
1432         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
1433         if( ppsz_argv == NULL )
1434         {
1435             msg_Err( p_this, "out of memory" );
1436             free( psz_shortopts );
1437             free( p_longopts );
1438             vlc_list_release( p_list );
1439             return -1;
1440         }
1441         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
1442                 *pi_argc * sizeof(char *) );
1443     }
1444
1445     i_shortopts = 0;
1446     for( i_index = 0; i_index < 256; i_index++ )
1447     {
1448         pp_shortopts[i_index] = NULL;
1449     }
1450
1451     /* Fill the p_longopts and psz_shortopts structures */
1452     i_index = 0;
1453     for( i_modules_index = 0; i_modules_index < p_list->i_count;
1454          i_modules_index++ )
1455     {
1456         p_parser = (module_t *)p_list->p_values[i_modules_index].p_object ;
1457
1458         if( !p_parser->i_config_items )
1459             continue;
1460
1461         for( p_item = p_parser->p_config;
1462              p_item->i_type != CONFIG_HINT_END;
1463              p_item++ )
1464         {
1465             /* Ignore hints */
1466             if( p_item->i_type & CONFIG_HINT )
1467                 continue;
1468
1469             /* Add item to long options */
1470             p_longopts[i_index].name = strdup( p_item->psz_name );
1471             if( p_longopts[i_index].name == NULL ) continue;
1472             p_longopts[i_index].has_arg =
1473                 (p_item->i_type == CONFIG_ITEM_BOOL)?
1474                                                no_argument : required_argument;
1475             p_longopts[i_index].flag = &flag;
1476             p_longopts[i_index].val = 0;
1477             i_index++;
1478
1479             /* When dealing with bools we also need to add the --no-foo
1480              * option */
1481             if( p_item->i_type == CONFIG_ITEM_BOOL )
1482             {
1483                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
1484                 if( psz_name == NULL ) continue;
1485                 strcpy( psz_name, "no" );
1486                 strcat( psz_name, p_item->psz_name );
1487
1488                 p_longopts[i_index].name = psz_name;
1489                 p_longopts[i_index].has_arg = no_argument;
1490                 p_longopts[i_index].flag = &flag;
1491                 p_longopts[i_index].val = 1;
1492                 i_index++;
1493
1494                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
1495                 if( psz_name == NULL ) continue;
1496                 strcpy( psz_name, "no-" );
1497                 strcat( psz_name, p_item->psz_name );
1498
1499                 p_longopts[i_index].name = psz_name;
1500                 p_longopts[i_index].has_arg = no_argument;
1501                 p_longopts[i_index].flag = &flag;
1502                 p_longopts[i_index].val = 1;
1503                 i_index++;
1504             }
1505
1506             /* If item also has a short option, add it */
1507             if( p_item->i_short )
1508             {
1509                 pp_shortopts[(int)p_item->i_short] = p_item;
1510                 psz_shortopts[i_shortopts] = p_item->i_short;
1511                 i_shortopts++;
1512                 if( p_item->i_type != CONFIG_ITEM_BOOL )
1513                 {
1514                     psz_shortopts[i_shortopts] = ':';
1515                     i_shortopts++;
1516
1517                     if( p_item->i_short == 'v' )
1518                     {
1519                         psz_shortopts[i_shortopts] = ':';
1520                         i_shortopts++;
1521                     }
1522                 }
1523             }
1524         }
1525     }
1526
1527     /* We don't need the module list anymore */
1528     vlc_list_release( p_list );
1529
1530     /* Close the longopts and shortopts structures */
1531     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1532     psz_shortopts[i_shortopts] = '\0';
1533
1534     /*
1535      * Parse the command line options
1536      */
1537     opterr = 0;
1538     optind = 1;
1539     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1540                                   p_longopts, &i_index ) ) != EOF )
1541     {
1542         /* A long option has been recognized */
1543         if( i_cmd == 0 )
1544         {
1545             module_config_t *p_conf;
1546             char *psz_name = (char *)p_longopts[i_index].name;
1547
1548             /* Check if we deal with a --nofoo or --no-foo long option */
1549             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1550
1551             /* Store the configuration option */
1552             p_conf = config_FindConfig( p_this, psz_name );
1553             if( p_conf )
1554             {
1555                 /* Check if the option is deprecated */
1556                 if( p_conf->psz_current )
1557                 {
1558                     if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
1559                     {
1560                        if( !b_ignore_errors ) 
1561                         {
1562                             fprintf(stderr,
1563                                     "Warning: option --%s is no longer used.\n",
1564                                     p_conf->psz_name);
1565                         }
1566                        continue;
1567                     }
1568                     if( !b_ignore_errors )
1569                     {
1570                         if( p_conf->b_strict )
1571                         {
1572                             fprintf( stderr,
1573                                      "Error: option --%s is deprecated. "
1574                                      "Use --%s instead.\n",
1575                                      p_conf->psz_name, p_conf->psz_current);
1576                             /*free */
1577                             for( i_index = 0; p_longopts[i_index].name; i_index++ )
1578                                 free( (char *)p_longopts[i_index].name );
1579
1580                             free( p_longopts );
1581                             free( psz_shortopts );
1582                             return -1;
1583                         }
1584                         fprintf(stderr,
1585                                 "Warning: option --%s is deprecated. "
1586                                 "You should use --%s instead.\n",
1587                                 p_conf->psz_name, p_conf->psz_current);
1588                     }
1589                     psz_name=p_conf->psz_current;
1590                     p_conf = config_FindConfig( p_this, psz_name );
1591                 }
1592
1593             switch( p_conf->i_type )
1594             {
1595                 case CONFIG_ITEM_STRING:
1596                 case CONFIG_ITEM_FILE:
1597                 case CONFIG_ITEM_DIRECTORY:
1598                 case CONFIG_ITEM_MODULE:
1599                 case CONFIG_ITEM_MODULE_LIST:
1600                 case CONFIG_ITEM_MODULE_LIST_CAT:
1601                 case CONFIG_ITEM_MODULE_CAT:
1602                     config_PutPsz( p_this, psz_name, optarg );
1603                     break;
1604                 case CONFIG_ITEM_INTEGER:
1605                     config_PutInt( p_this, psz_name, strtol(optarg, 0, 0));
1606                     break;
1607                 case CONFIG_ITEM_FLOAT:
1608                     config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1609                     break;
1610                 case CONFIG_ITEM_KEY:
1611                     config_PutInt( p_this, psz_name, ConfigStringToKey( optarg ) );
1612                     break;
1613                 case CONFIG_ITEM_BOOL:
1614                     config_PutInt( p_this, psz_name, !flag );
1615                     break;
1616             }
1617
1618             continue;
1619         }
1620     }
1621     /* A short option has been recognized */
1622     if( pp_shortopts[i_cmd] != NULL )
1623     {
1624         switch( pp_shortopts[i_cmd]->i_type )
1625         {
1626             case CONFIG_ITEM_STRING:
1627             case CONFIG_ITEM_FILE:
1628             case CONFIG_ITEM_DIRECTORY:
1629             case CONFIG_ITEM_MODULE:
1630             case CONFIG_ITEM_MODULE_CAT:
1631             case CONFIG_ITEM_MODULE_LIST:
1632             case CONFIG_ITEM_MODULE_LIST_CAT:
1633                 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1634                 break;
1635             case CONFIG_ITEM_INTEGER:
1636                 if( i_cmd == 'v' )
1637                 {
1638                     if( optarg )
1639                     {
1640                         if( *optarg == 'v' ) /* eg. -vvv */
1641                         {
1642                             i_verbose++;
1643                             while( *optarg == 'v' )
1644                             {
1645                                 i_verbose++;
1646                                 optarg++;
1647                             }
1648                         }
1649                         else
1650                         {
1651                             i_verbose += atoi( optarg ); /* eg. -v2 */
1652                         }
1653                     }
1654                     else
1655                     {
1656                         i_verbose++; /* -v */
1657                     }
1658                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1659                                            i_verbose );
1660                 }
1661                 else
1662                 {
1663                     config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1664                                            strtol(optarg, 0, 0) );
1665                 }
1666                 break;
1667             case CONFIG_ITEM_BOOL:
1668                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1669                 break;
1670             }
1671
1672             continue;
1673         }
1674
1675         /* Internal error: unknown option */
1676         if( !b_ignore_errors )
1677         {
1678             fprintf( stderr, "%s: unknown option"
1679                      " or missing mandatory argument ",
1680                      p_this->p_vlc->psz_object_name );
1681             if( optopt )
1682             {
1683                 fprintf( stderr, "`-%c'\n", optopt );
1684             }
1685             else
1686             {
1687                 fprintf( stderr, "`%s'\n", ppsz_argv[optind-1] );
1688             }
1689             fprintf( stderr, "Try `%s --help' for more information.\n",
1690                              p_this->p_vlc->psz_object_name );
1691
1692             for( i_index = 0; p_longopts[i_index].name; i_index++ )
1693                 free( (char *)p_longopts[i_index].name );
1694             free( p_longopts );
1695             free( psz_shortopts );
1696             return -1;
1697         }
1698     }
1699
1700     /* Free allocated resources */
1701     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1702         free( (char *)p_longopts[i_index].name );
1703     free( p_longopts );
1704     free( psz_shortopts );
1705     if( b_ignore_errors ) free( ppsz_argv );
1706
1707     return 0;
1708 }
1709
1710 /*****************************************************************************
1711  * config_GetHomeDir: find the user's home directory.
1712  *****************************************************************************
1713  * This function will try by different ways to find the user's home path.
1714  * Note that this function is not reentrant, it should be called only once
1715  * at the beginning of main where the result will be stored for later use.
1716  *****************************************************************************/
1717 char *config_GetHomeDir( void )
1718 {
1719     char *p_tmp, *p_homedir = NULL;
1720
1721 #if defined(HAVE_GETPWUID)
1722     struct passwd *p_pw = NULL;
1723 #endif
1724
1725 #if defined(WIN32) && !defined(UNDER_CE)
1726     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1727                                                LPSTR );
1728 #ifndef CSIDL_FLAG_CREATE
1729 #   define CSIDL_FLAG_CREATE 0x8000
1730 #endif
1731 #ifndef CSIDL_APPDATA
1732 #   define CSIDL_APPDATA 0x1A
1733 #endif
1734 #ifndef SHGFP_TYPE_CURRENT
1735 #   define SHGFP_TYPE_CURRENT 0
1736 #endif
1737
1738     HINSTANCE shfolder_dll;
1739     SHGETFOLDERPATH SHGetFolderPath ;
1740
1741     /* load the shfolder dll to retrieve SHGetFolderPath */
1742     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
1743     {
1744         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1745                                                   _T("SHGetFolderPathA") );
1746         if ( SHGetFolderPath != NULL )
1747         {
1748             p_homedir = (char *)malloc( MAX_PATH );
1749             if( !p_homedir ) return NULL;
1750
1751             /* get the "Application Data" folder for the current user */
1752             if( S_OK == SHGetFolderPath( NULL,
1753                                          CSIDL_APPDATA | 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
1810 static int ConfigStringToKey( char *psz_key )
1811 {
1812     int i_key = 0;
1813     unsigned int i;
1814     char *psz_parser = strchr( psz_key, '-' );
1815     while( psz_parser && psz_parser != psz_key )
1816     {
1817         for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
1818         {
1819             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
1820                               strlen( vlc_modifiers[i].psz_key_string ) ) )
1821             {
1822                 i_key |= vlc_modifiers[i].i_key_code;
1823             }
1824         }
1825         psz_key = psz_parser + 1;
1826         psz_parser = strchr( psz_key, '-' );
1827     }
1828     for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
1829     {
1830         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
1831         {
1832             i_key |= vlc_keys[i].i_key_code;
1833             break;
1834         }
1835     }
1836     return i_key;
1837 }
1838
1839 static char *ConfigKeyToString( int i_key )
1840 {
1841     char *psz_key = malloc( 100 );
1842     char *p;
1843     size_t index;
1844
1845     if ( !psz_key )
1846     {
1847         return NULL;
1848     }
1849     *psz_key = '\0';
1850     p = psz_key;
1851     for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
1852          index++ )
1853     {
1854         if( i_key & vlc_modifiers[index].i_key_code )
1855         {
1856             p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
1857         }
1858     }
1859     for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
1860          index++)
1861     {
1862         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
1863         {
1864             p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
1865             break;
1866         }
1867     }
1868     return psz_key;
1869 }