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