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