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