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