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