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