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