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