]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
03f43b620fd859d770f0aac9e19cc3704615f381
[vlc] / src / misc / configuration.c
1 /*****************************************************************************
2  * configuration.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: configuration.c,v 1.32 2002/07/03 19:40:49 sam Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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
26 #include <stdio.h>                                              /* sprintf() */
27 #include <stdlib.h>                                      /* free(), strtol() */
28 #include <string.h>                                              /* strdup() */
29 #include <errno.h>                                                  /* errno */
30
31 #ifdef HAVE_UNISTD_H
32 #    include <unistd.h>                                          /* getuid() */
33 #endif
34
35 #ifdef HAVE_GETOPT_LONG
36 #   ifdef HAVE_GETOPT_H
37 #       include <getopt.h>                                       /* getopt() */
38 #   endif
39 #else
40 #   include "GNUgetopt/getopt.h"
41 #endif
42
43 #if defined(HAVE_GETPWUID)
44 #include <pwd.h>                                               /* getpwuid() */
45 #endif
46
47 #include <sys/stat.h>
48 #include <sys/types.h>
49
50 /*****************************************************************************
51  * config_GetInt: get the value of an int variable
52  *****************************************************************************
53  * This function is used to get the value of variables which are internally
54  * represented by an integer (CONFIG_ITEM_INTEGER and
55  * CONFIG_ITEM_BOOL).
56  *****************************************************************************/
57 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
58 {
59     module_config_t *p_config;
60
61     p_config = config_FindConfig( p_this, psz_name );
62
63     /* sanity checks */
64     if( !p_config )
65     {
66         msg_Err( p_this, "option %s does not exist", psz_name );
67         return -1;
68     }
69     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
70         (p_config->i_type!=CONFIG_ITEM_BOOL) )
71     {
72         msg_Err( p_this, "option %s does not refer to an int", psz_name );
73         return -1;
74     }
75
76     return p_config->i_value;
77 }
78
79 /*****************************************************************************
80  * config_GetFloat: get the value of a float variable
81  *****************************************************************************
82  * This function is used to get the value of variables which are internally
83  * represented by a float (CONFIG_ITEM_FLOAT).
84  *****************************************************************************/
85 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
86 {
87     module_config_t *p_config;
88
89     p_config = config_FindConfig( p_this, psz_name );
90
91     /* sanity checks */
92     if( !p_config )
93     {
94         msg_Err( p_this, "option %s does not exist", psz_name );
95         return -1;
96     }
97     if( p_config->i_type != CONFIG_ITEM_FLOAT )
98     {
99         msg_Err( p_this, "option %s does not refer to a float", psz_name );
100         return -1;
101     }
102
103     return p_config->f_value;
104 }
105
106 /*****************************************************************************
107  * config_GetPsz: get the string value of a string variable
108  *****************************************************************************
109  * This function is used to get the value of variables which are internally
110  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
111  * and CONFIG_ITEM_MODULE).
112  *
113  * Important note: remember to free() the returned char* because it's a
114  *   duplicate of the actual value. It isn't safe to return a pointer to the
115  *   actual value as it can be modified at any time.
116  *****************************************************************************/
117 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
118 {
119     module_config_t *p_config;
120     char *psz_value = NULL;
121
122     p_config = config_FindConfig( p_this, psz_name );
123
124     /* sanity checks */
125     if( !p_config )
126     {
127         msg_Err( p_this, "option %s does not exist", psz_name );
128         return NULL;
129     }
130     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
131         (p_config->i_type!=CONFIG_ITEM_FILE) &&
132         (p_config->i_type!=CONFIG_ITEM_MODULE) )
133     {
134         msg_Err( p_this, "option %s does not refer to a string", psz_name );
135         return NULL;
136     }
137
138     /* return a copy of the string */
139     vlc_mutex_lock( p_config->p_lock );
140     if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
141     vlc_mutex_unlock( p_config->p_lock );
142
143     return psz_value;
144 }
145
146 /*****************************************************************************
147  * config_PutPsz: set the string value of a string variable
148  *****************************************************************************
149  * This function is used to set the value of variables which are internally
150  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
151  * and CONFIG_ITEM_MODULE).
152  *****************************************************************************/
153 void __config_PutPsz( vlc_object_t *p_this, 
154                       const char *psz_name, const char *psz_value )
155 {
156     module_config_t *p_config;
157
158     p_config = config_FindConfig( p_this, psz_name );
159
160     /* sanity checks */
161     if( !p_config )
162     {
163         msg_Err( p_this, "option %s does not exist", psz_name );
164         return;
165     }
166     if( (p_config->i_type!=CONFIG_ITEM_STRING) &&
167         (p_config->i_type!=CONFIG_ITEM_FILE) &&
168         (p_config->i_type!=CONFIG_ITEM_MODULE) )
169     {
170         msg_Err( p_this, "option %s does not refer to a string", psz_name );
171         return;
172     }
173
174     vlc_mutex_lock( p_config->p_lock );
175
176     /* free old string */
177     if( p_config->psz_value ) free( p_config->psz_value );
178
179     if( psz_value ) p_config->psz_value = strdup( psz_value );
180     else p_config->psz_value = NULL;
181
182     vlc_mutex_unlock( p_config->p_lock );
183
184     if( p_config->pf_callback )
185     {
186         p_config->pf_callback( p_this );
187     }
188 }
189
190 /*****************************************************************************
191  * config_PutInt: set the integer value of an int variable
192  *****************************************************************************
193  * This function is used to set the value of variables which are internally
194  * represented by an integer (CONFIG_ITEM_INTEGER and
195  * CONFIG_ITEM_BOOL).
196  *****************************************************************************/
197 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
198 {
199     module_config_t *p_config;
200
201     p_config = config_FindConfig( p_this, psz_name );
202
203     /* sanity checks */
204     if( !p_config )
205     {
206         msg_Err( p_this, "option %s does not exist", psz_name );
207         return;
208     }
209     if( (p_config->i_type!=CONFIG_ITEM_INTEGER) &&
210         (p_config->i_type!=CONFIG_ITEM_BOOL) )
211     {
212         msg_Err( p_this, "option %s does not refer to an int", psz_name );
213         return;
214     }
215
216     p_config->i_value = i_value;
217
218     if( p_config->pf_callback )
219     {
220         p_config->pf_callback( p_this );
221     }
222 }
223
224 /*****************************************************************************
225  * config_PutFloat: set the value of a float variable
226  *****************************************************************************
227  * This function is used to set the value of variables which are internally
228  * represented by a float (CONFIG_ITEM_FLOAT).
229  *****************************************************************************/
230 void __config_PutFloat( vlc_object_t *p_this,
231                         const char *psz_name, float f_value )
232 {
233     module_config_t *p_config;
234
235     p_config = config_FindConfig( p_this, psz_name );
236
237     /* sanity checks */
238     if( !p_config )
239     {
240         msg_Err( p_this, "option %s does not exist", psz_name );
241         return;
242     }
243     if( p_config->i_type != CONFIG_ITEM_FLOAT )
244     {
245         msg_Err( p_this, "option %s does not refer to a float", psz_name );
246         return;
247     }
248
249     p_config->f_value = f_value;
250
251     if( p_config->pf_callback )
252     {
253         p_config->pf_callback( p_this );
254     }
255 }
256
257 /*****************************************************************************
258  * config_FindConfig: find the config structure associated with an option.
259  *****************************************************************************
260  * FIXME: This function really needs to be optimized.
261  *****************************************************************************/
262 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name)
263 {
264     module_t *p_module;
265     module_config_t *p_item;
266
267     if( !psz_name ) return NULL;
268
269     for( p_module = p_this->p_vlc->module_bank.first ;
270          p_module != NULL ;
271          p_module = p_module->next )
272     {
273         for( p_item = p_module->p_config;
274              p_item->i_type != CONFIG_HINT_END;
275              p_item++ )
276         {
277             if( p_item->i_type & CONFIG_HINT )
278                 /* ignore hints */
279                 continue;
280             if( !strcmp( psz_name, p_item->psz_name ) )
281                 return p_item;
282         }
283     }
284
285     return NULL;
286 }
287
288 /*****************************************************************************
289  * config_Duplicate: creates a duplicate of a module's configuration data.
290  *****************************************************************************
291  * Unfortunatly we cannot work directly with the module's config data as
292  * this module might be unloaded from memory at any time (remember HideModule).
293  * This is why we need to create an exact copy of the config data.
294  *****************************************************************************/
295 void config_Duplicate( module_t *p_module, module_config_t *p_orig )
296 {
297     int i, j, i_lines = 1;
298     module_config_t *p_item;
299
300     /* Calculate the structure length */
301     p_module->i_config_items = 0;
302     p_module->i_bool_items = 0;
303
304     for( p_item = p_orig; p_item->i_type != CONFIG_HINT_END; p_item++ )
305     {
306         i_lines++;
307
308         if( p_item->i_type & CONFIG_ITEM )
309         {
310             p_module->i_config_items++;
311         }
312
313         if( p_item->i_type == CONFIG_ITEM_BOOL )
314         {
315             p_module->i_bool_items++;
316         }
317     }
318
319     /* Allocate memory */
320     p_module->p_config = (module_config_t *)malloc( sizeof(module_config_t)
321                                                      * i_lines );
322     if( p_module->p_config == NULL )
323     {
324         msg_Err( p_module, "config error: can't duplicate p_config" );
325         return;
326     }
327
328     /* Initialize the global lock */
329     vlc_mutex_init( p_module, &p_module->object_lock );
330
331     /* Do the duplication job */
332     for( i = 0; i < i_lines ; i++ )
333     {
334         p_module->p_config[i].i_type = p_orig[i].i_type;
335         p_module->p_config[i].i_short = p_orig[i].i_short;
336         p_module->p_config[i].i_value = p_orig[i].i_value;
337         p_module->p_config[i].f_value = p_orig[i].f_value;
338         p_module->p_config[i].b_dirty = p_orig[i].b_dirty;
339
340         p_module->p_config[i].psz_name = p_orig[i].psz_name ?
341                                    strdup( _(p_orig[i].psz_name) ) : NULL;
342         p_module->p_config[i].psz_text = p_orig[i].psz_text ?
343                                    strdup( _(p_orig[i].psz_text) ) : NULL;
344         p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
345                                    strdup( _(p_orig[i].psz_longtext) ) : NULL;
346         p_module->p_config[i].psz_value = p_orig[i].psz_value ?
347                                    strdup( p_orig[i].psz_value ) : NULL;
348
349         p_module->p_config[i].p_lock = &p_module->object_lock;
350
351         /* duplicate the string list */
352         p_module->p_config[i].ppsz_list = NULL;
353         if( p_orig[i].ppsz_list )
354         {
355             for( j = 0; p_orig[i].ppsz_list[j]; j++ );
356             p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
357             if( p_module->p_config[i].ppsz_list )
358             {
359                 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
360                     p_module->p_config[i].ppsz_list[j] =
361                         strdup( p_orig[i].ppsz_list[j] );
362             }
363             p_module->p_config[i].ppsz_list[j] = NULL;
364         }
365
366         /* the callback pointer is only valid when the module is loaded so this
367          * value is set in ActivateModule() and reset in DeactivateModule() */
368         p_module->p_config[i].pf_callback = NULL;
369     }
370 }
371
372 /*****************************************************************************
373  * config_Free: frees a duplicated module's configuration data.
374  *****************************************************************************
375  * This function frees all the data duplicated by config_Duplicate.
376  *****************************************************************************/
377 void config_Free( module_t *p_module )
378 {
379     module_config_t *p_item = p_module->p_config;
380     int i;
381
382     for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
383     {
384         if( p_item->psz_name )
385             free( p_item->psz_name );
386
387         if( p_item->psz_text )
388             free( p_item->psz_text );
389
390         if( p_item->psz_longtext )
391             free( p_item->psz_longtext );
392
393         if( p_item->psz_value )
394             free( p_item->psz_value );
395
396         if( p_item->ppsz_list )
397         {
398             for( i = 0; p_item->ppsz_list[i]; i++ )
399                 free(p_item->ppsz_list[i]);
400             free( p_item->ppsz_list );
401         }
402     }
403
404     free( p_module->p_config );
405     p_module->p_config = NULL;
406
407     /* Remove the global lock */
408     vlc_mutex_destroy( &p_module->object_lock );
409 }
410
411 /*****************************************************************************
412  * config_SetCallbacks: sets callback functions in the duplicate p_config.
413  *****************************************************************************
414  * Unfortunatly we cannot work directly with the module's config data as
415  * this module might be unloaded from memory at any time (remember HideModule).
416  * This is why we need to duplicate callbacks each time we reload the module.
417  *****************************************************************************/
418 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
419 {
420     while( p_new->i_type != CONFIG_HINT_END )
421     {
422         p_new->pf_callback = p_orig->pf_callback;
423         p_new++;
424         p_orig++;
425     }
426 }
427
428 /*****************************************************************************
429  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
430  *****************************************************************************
431  * We simply undo what we did in config_SetCallbacks.
432  *****************************************************************************/
433 void config_UnsetCallbacks( module_config_t *p_new )
434 {
435     while( p_new->i_type != CONFIG_HINT_END )
436     {
437         p_new->pf_callback = NULL;
438         p_new++;
439     }
440 }
441
442 /*****************************************************************************
443  * config_LoadConfigFile: loads the configuration file.
444  *****************************************************************************
445  * This function is called to load the config options stored in the config
446  * file.
447  *****************************************************************************/
448 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
449 {
450     module_t *p_module;
451     module_config_t *p_item;
452     FILE *file;
453     char line[1024];
454     char *p_index, *psz_option_name, *psz_option_value;
455     char *psz_filename, *psz_homedir;
456
457     /* Acquire config file lock */
458     vlc_mutex_lock( &p_this->p_vlc->config_lock );
459
460     psz_homedir = p_this->p_vlc->psz_homedir;
461     if( !psz_homedir )
462     {
463         msg_Err( p_this, "psz_homedir is null" );
464         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
465         return -1;
466     }
467     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
468                                    strlen(psz_homedir) + 1 );
469     if( !psz_filename )
470     {
471         msg_Err( p_this, "out of memory" );
472         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
473         return -1;
474     }
475     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
476
477     msg_Dbg( p_this, "opening config file %s", psz_filename );
478
479     file = fopen( psz_filename, "rt" );
480     if( !file )
481     {
482         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
483         free( psz_filename );
484         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
485         return -1;
486     }
487
488     /* Look for the selected module, if NULL then save everything */
489     for( p_module = p_this->p_vlc->module_bank.first ; p_module != NULL ;
490          p_module = p_module->next )
491     {
492
493         if( psz_module_name
494              && strcmp( psz_module_name, p_module->psz_object_name ) )
495         {
496             continue;
497         }
498
499         /* The config file is organized in sections, one per module. Look for
500          * the interesting section ( a section is of the form [foo] ) */
501         rewind( file );
502         while( fgets( line, 1024, file ) )
503         {
504             if( (line[0] == '[') && (p_index = strchr(line,']')) &&
505                 (p_index - &line[1] == strlen(p_module->psz_object_name) ) &&
506                 !memcmp( &line[1], p_module->psz_object_name,
507                          strlen(p_module->psz_object_name) ) )
508             {
509                 msg_Dbg( p_this, "loading config for module <%s>",
510                                  p_module->psz_object_name );
511
512                 break;
513             }
514         }
515         /* either we found the section or we're at the EOF */
516
517         /* Now try to load the options in this section */
518         while( fgets( line, 1024, file ) )
519         {
520             if( line[0] == '[' ) break; /* end of section */
521
522             /* ignore comments or empty lines */
523             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
524                 continue;
525
526             /* get rid of line feed */
527             if( line[strlen(line)-1] == '\n' )
528                 line[strlen(line)-1] = (char)0;
529
530             /* look for option name */
531             psz_option_name = line;
532             psz_option_value = NULL;
533             p_index = strchr( line, '=' );
534             if( !p_index ) break; /* this ain't an option!!! */
535
536             *p_index = (char)0;
537             psz_option_value = p_index + 1;
538
539             /* try to match this option with one of the module's options */
540             for( p_item = p_module->p_config;
541                  p_item->i_type != CONFIG_HINT_END;
542                  p_item++ )
543             {
544                 if( p_item->i_type & CONFIG_HINT )
545                     /* ignore hints */
546                     continue;
547
548                 if( !strcmp( p_item->psz_name, psz_option_name ) )
549                 {
550                     /* We found it */
551                     switch( p_item->i_type )
552                     {
553                     case CONFIG_ITEM_BOOL:
554                     case CONFIG_ITEM_INTEGER:
555                         if( !*psz_option_value )
556                             break;                    /* ignore empty option */
557                         p_item->i_value = atoi( psz_option_value);
558                         msg_Dbg( p_this, "found <%s> option %s=%i",
559                                  p_module->psz_object_name, p_item->psz_name,
560                                  p_item->i_value );
561                         break;
562
563                     case CONFIG_ITEM_FLOAT:
564                         if( !*psz_option_value )
565                             break;                    /* ignore empty option */
566                         p_item->f_value = (float)atof( psz_option_value);
567                         msg_Dbg( p_this, "found <%s> option %s=%f",
568                                  p_module->psz_object_name, p_item->psz_name,
569                                  (double)p_item->f_value );
570                         break;
571
572                     default:
573                         vlc_mutex_lock( p_item->p_lock );
574
575                         /* free old string */
576                         if( p_item->psz_value )
577                             free( p_item->psz_value );
578
579                         p_item->psz_value = *psz_option_value ?
580                             strdup( psz_option_value ) : NULL;
581
582                         vlc_mutex_unlock( p_item->p_lock );
583
584                         msg_Dbg( p_this, "found <%s> option %s=%s",
585                                  p_module->psz_object_name, p_item->psz_name,
586                                  p_item->psz_value != NULL ?
587                                       p_item->psz_value : "(NULL)" );
588                         break;
589                     }
590                 }
591             }
592         }
593
594     }
595     
596     fclose( file );
597     free( psz_filename );
598
599     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
600
601     return 0;
602 }
603
604 /*****************************************************************************
605  * config_SaveConfigFile: Save a module's config options.
606  *****************************************************************************
607  * This will save the specified module's config options to the config file.
608  * If psz_module_name is NULL then we save all the modules config options.
609  * It's no use to save the config options that kept their default values, so
610  * we'll try to be a bit clever here.
611  *
612  * When we save we mustn't delete the config options of the modules that
613  * haven't been loaded. So we cannot just create a new config file with the
614  * config structures we've got in memory. 
615  * I don't really know how to deal with this nicely, so I will use a completly
616  * dumb method ;-)
617  * I will load the config file in memory, but skipping all the sections of the
618  * modules we want to save. Then I will create a brand new file, dump the file
619  * loaded in memory and then append the sections of the modules we want to
620  * save.
621  * Really stupid no ?
622  *****************************************************************************/
623 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
624 {
625     module_t *p_module;
626     module_config_t *p_item;
627     FILE *file;
628     char p_line[1024], *p_index2;
629     int i_sizebuf = 0;
630     char *p_bigbuffer, *p_index;
631     vlc_bool_t b_backup;
632     char *psz_filename, *psz_homedir;
633
634     /* Acquire config file lock */
635     vlc_mutex_lock( &p_this->p_vlc->config_lock );
636
637     psz_homedir = p_this->p_vlc->psz_homedir;
638     if( !psz_homedir )
639     {
640         msg_Err( p_this, "psz_homedir is null" );
641         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
642         return -1;
643     }
644     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
645                                    strlen(psz_homedir) + 1 );
646     if( !psz_filename )
647     {
648         msg_Err( p_this, "out of memory" );
649         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
650         return -1;
651     }
652     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
653
654 #ifndef WIN32
655     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
656 #else
657     if( mkdir( psz_filename ) && errno != EEXIST )
658 #endif
659     {
660         msg_Err( p_this, "could not create %s (%s)",
661                          psz_filename, strerror(errno) );
662     }
663
664     strcat( psz_filename, "/" CONFIG_FILE );
665
666
667     msg_Dbg( p_this, "opening config file %s", psz_filename );
668
669     file = fopen( psz_filename, "rt" );
670     if( !file )
671     {
672         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
673     }
674     else
675     {
676         /* look for file size */
677         fseek( file, 0, SEEK_END );
678         i_sizebuf = ftell( file );
679         rewind( file );
680     }
681
682     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
683     if( !p_bigbuffer )
684     {
685         msg_Err( p_this, "out of memory" );
686         if( file ) fclose( file );
687         free( psz_filename );
688         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
689         return -1;
690     }
691     p_bigbuffer[0] = 0;
692
693     /* backup file into memory, we only need to backup the sections we won't
694      * save later on */
695     b_backup = 0;
696     while( file && fgets( p_line, 1024, file ) )
697     {
698         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
699         {
700             /* we found a section, check if we need to do a backup */
701             for( p_module = p_this->p_vlc->module_bank.first; p_module != NULL;
702                  p_module = p_module->next )
703             {
704                 if( ((p_index2 - &p_line[1])
705                        == strlen(p_module->psz_object_name) ) &&
706                     !memcmp( &p_line[1], p_module->psz_object_name,
707                              strlen(p_module->psz_object_name) ) )
708                 {
709                     if( !psz_module_name )
710                         break;
711                     else if( !strcmp( psz_module_name,
712                                       p_module->psz_object_name ) )
713                         break;
714                 }
715             }
716
717             if( !p_module )
718             {
719                 /* we don't have this section in our list so we need to back
720                  * it up */
721                 *p_index2 = 0;
722                 msg_Dbg( p_this, "backing up config for unknown module <%s>",
723                                  &p_line[1] );
724                 *p_index2 = ']';
725
726                 b_backup = 1;
727             }
728             else
729             {
730                 b_backup = 0;
731             }
732         }
733
734         /* save line if requested and line is valid (doesn't begin with a
735          * space, tab, or eol) */
736         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
737             && (p_line[0] != '\t') )
738         {
739             strcpy( p_index, p_line );
740             p_index += strlen( p_line );
741         }
742     }
743     if( file ) fclose( file );
744
745
746     /*
747      * Save module config in file
748      */
749
750     file = fopen( psz_filename, "wt" );
751     if( !file )
752     {
753         msg_Warn( p_this, "could not open config file %s for writing",
754                           psz_filename );
755         free( psz_filename );
756         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
757         return -1;
758     }
759
760     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
761
762     /* Look for the selected module, if NULL then save everything */
763     for( p_module = p_this->p_vlc->module_bank.first ; p_module != NULL ;
764          p_module = p_module->next )
765     {
766
767         if( psz_module_name && strcmp( psz_module_name,
768                                        p_module->psz_object_name ) )
769             continue;
770
771         if( !p_module->i_config_items )
772             continue;
773
774         msg_Dbg( p_this, "saving config for module <%s>",
775                          p_module->psz_object_name );
776
777         fprintf( file, "[%s]", p_module->psz_object_name );
778         if( p_module->psz_longname )
779             fprintf( file, " # %s\n\n", p_module->psz_longname );
780         else
781             fprintf( file, "\n\n" );
782
783         for( p_item = p_module->p_config;
784              p_item->i_type != CONFIG_HINT_END;
785              p_item++ )
786         {
787             if( p_item->i_type & CONFIG_HINT )
788                 /* ignore hints */
789                 continue;
790
791             switch( p_item->i_type )
792             {
793             case CONFIG_ITEM_BOOL:
794             case CONFIG_ITEM_INTEGER:
795                 if( p_item->psz_text )
796                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
797                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
798                              _("boolean") : _("integer") );
799                 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
800                 break;
801
802             case CONFIG_ITEM_FLOAT:
803                 if( p_item->psz_text )
804                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
805                              _("float") );
806                 fprintf( file, "%s=%f\n", p_item->psz_name,
807                          (double)p_item->f_value );
808                 break;
809
810             default:
811                 if( p_item->psz_text )
812                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
813                              _("string") );
814                 fprintf( file, "%s=%s\n", p_item->psz_name,
815                          p_item->psz_value ? p_item->psz_value : "" );
816             }
817         }
818
819         fprintf( file, "\n" );
820     }
821
822
823     /*
824      * Restore old settings from the config in file
825      */
826     fputs( p_bigbuffer, file );
827     free( p_bigbuffer );
828
829     fclose( file );
830     free( psz_filename );
831     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
832
833     return 0;
834 }
835
836 /*****************************************************************************
837  * config_LoadCmdLine: parse command line
838  *****************************************************************************
839  * Parse command line for configuration options.
840  * Now that the module_bank has been initialized, we can dynamically
841  * generate the longopts structure used by getops. We have to do it this way
842  * because we don't know (and don't want to know) in advance the configuration
843  * options used (ie. exported) by each module.
844  *****************************************************************************/
845 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
846                           vlc_bool_t b_ignore_errors )
847 {
848     int i_cmd, i_index, i_opts, i_shortopts, flag;
849     module_t *p_module;
850     module_config_t *p_item;
851     struct option *p_longopts;
852
853     /* Short options */
854     module_config_t *pp_shortopts[256];
855     char *psz_shortopts;
856
857     /* Set default configuration and copy arguments */
858     p_this->p_vlc->i_argc    = *pi_argc;
859     p_this->p_vlc->ppsz_argv = ppsz_argv;
860
861     p_this->p_vlc->p_channel = NULL;
862
863 #ifdef SYS_DARWIN
864     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
865      * is the PSN - process serial number (a unique PID-ish thingie)
866      * still ok for real Darwin & when run from command line */
867     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
868                                         /* for example -psn_0_9306113 */
869     {
870         /* GDMF!... I can't do this or else the MacOSX window server will
871          * not pick up the PSN and not register the app and we crash...
872          * hence the following kludge otherwise we'll get confused w/ argv[1]
873          * being an input file name */
874 #if 0
875         ppsz_argv[ 1 ] = NULL;
876 #endif
877         *pi_argc = *pi_argc - 1;
878         pi_argc--;
879         return 0;
880     }
881 #endif
882
883     /*
884      * Generate the longopts and shortopts structures used by getopt_long
885      */
886
887     i_opts = 0;
888     for( p_module = p_this->p_vlc->module_bank.first;
889          p_module != NULL ;
890          p_module = p_module->next )
891     {
892         /* count the number of exported configuration options (to allocate
893          * longopts). We also need to allocate space for too options when
894          * dealing with boolean to allow for --foo and --no-foo */
895         i_opts += (p_module->i_config_items + 2 * p_module->i_bool_items);
896     }
897
898     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
899     if( p_longopts == NULL )
900     {
901         msg_Err( p_this, "out of memory" );
902         return -1;
903     }
904
905     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
906     if( psz_shortopts == NULL )
907     {
908         msg_Err( p_this, "out of memory" );
909         free( p_longopts );
910         return -1;
911     }
912
913     /* If we are requested to ignore errors, then we must work on a copy
914      * of the ppsz_argv array, otherwise getopt_long will reorder it for
915      * us, ignoring the arity of the options */
916     if( b_ignore_errors )
917     {
918         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
919         if( ppsz_argv == NULL )
920         {
921             msg_Err( p_this, "out of memory" );
922             free( psz_shortopts );
923             free( p_longopts );
924             return -1;
925         }
926         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
927                 *pi_argc * sizeof(char *) );
928     }
929
930     i_shortopts = 0;
931     for( i_index = 0; i_index < 256; i_index++ )
932     {
933         pp_shortopts[i_index] = NULL;
934     }
935
936     /* Fill the p_longopts and psz_shortopts structures */
937     i_index = 0;
938     for( p_module = p_this->p_vlc->module_bank.first ;
939          p_module != NULL ;
940          p_module = p_module->next )
941     {
942         for( p_item = p_module->p_config;
943              p_item->i_type != CONFIG_HINT_END;
944              p_item++ )
945         {
946             /* Ignore hints */
947             if( p_item->i_type & CONFIG_HINT )
948                 continue;
949
950             /* Add item to long options */
951             p_longopts[i_index].name = strdup( p_item->psz_name );
952             if( p_longopts[i_index].name == NULL ) continue;
953             p_longopts[i_index].has_arg =
954                 (p_item->i_type == CONFIG_ITEM_BOOL)?
955                                                no_argument : required_argument;
956             p_longopts[i_index].flag = &flag;
957             p_longopts[i_index].val = 0;
958             i_index++;
959
960             /* When dealing with bools we also need to add the --no-foo
961              * option */
962             if( p_item->i_type == CONFIG_ITEM_BOOL )
963             {
964                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
965                 if( psz_name == NULL ) continue;
966                 strcpy( psz_name, "no" );
967                 strcat( psz_name, p_item->psz_name );
968
969                 p_longopts[i_index].name = psz_name;
970                 p_longopts[i_index].has_arg = no_argument;
971                 p_longopts[i_index].flag = &flag;
972                 p_longopts[i_index].val = 1;
973                 i_index++;
974
975                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
976                 if( psz_name == NULL ) continue;
977                 strcpy( psz_name, "no-" );
978                 strcat( psz_name, p_item->psz_name );
979
980                 p_longopts[i_index].name = psz_name;
981                 p_longopts[i_index].has_arg = no_argument;
982                 p_longopts[i_index].flag = &flag;
983                 p_longopts[i_index].val = 1;
984                 i_index++;
985             }
986
987             /* If item also has a short option, add it */
988             if( p_item->i_short )
989             {
990                 pp_shortopts[(int)p_item->i_short] = p_item;
991                 psz_shortopts[i_shortopts] = p_item->i_short;
992                 i_shortopts++;
993                 if( p_item->i_type != CONFIG_ITEM_BOOL )
994                 {
995                     psz_shortopts[i_shortopts] = ':';
996                     i_shortopts++;
997                 }
998             }
999         }
1000     }
1001
1002     /* Close the longopts and shortopts structures */
1003     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1004     psz_shortopts[i_shortopts] = '\0';
1005
1006     /*
1007      * Parse the command line options
1008      */
1009     opterr = 0;
1010     optind = 1;
1011     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1012                                   p_longopts, &i_index ) ) != EOF )
1013     {
1014         /* A long option has been recognized */
1015         if( i_cmd == 0 )
1016         {
1017             module_config_t *p_conf;
1018             char *psz_name = (char *)p_longopts[i_index].name;
1019
1020             /* Check if we deal with a --nofoo or --no-foo long option */
1021             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1022
1023             /* Store the configuration option */
1024             p_conf = config_FindConfig( p_this, psz_name );
1025
1026             if( p_conf ) switch( p_conf->i_type )
1027             {
1028             case CONFIG_ITEM_STRING:
1029             case CONFIG_ITEM_FILE:
1030             case CONFIG_ITEM_MODULE:
1031                 config_PutPsz( p_this, psz_name, optarg );
1032                 break;
1033             case CONFIG_ITEM_INTEGER:
1034                 config_PutInt( p_this, psz_name, atoi(optarg));
1035                 break;
1036             case CONFIG_ITEM_FLOAT:
1037                 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1038                 break;
1039             case CONFIG_ITEM_BOOL:
1040                 config_PutInt( p_this, psz_name, !flag );
1041                 break;
1042             }
1043
1044             continue;
1045         }
1046
1047         /* A short option has been recognized */
1048         if( pp_shortopts[i_cmd] != NULL )
1049         {
1050             switch( pp_shortopts[i_cmd]->i_type )
1051             {
1052             case CONFIG_ITEM_STRING:
1053             case CONFIG_ITEM_FILE:
1054             case CONFIG_ITEM_MODULE:
1055                 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1056                 break;
1057             case CONFIG_ITEM_INTEGER:
1058                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1059                                        atoi(optarg));
1060                 break;
1061             case CONFIG_ITEM_BOOL:
1062                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1063                 break;
1064             }
1065
1066             continue;
1067         }
1068
1069         /* Internal error: unknown option */
1070         if( !b_ignore_errors )
1071         {
1072             fprintf( stderr, "unknown option `%s'\n", ppsz_argv[optind-1] );
1073             fprintf( stderr, "Try `%s --help' for more information.\n",
1074                              p_this->p_vlc->psz_object_name );
1075
1076             free( p_longopts );
1077             free( psz_shortopts );
1078             if( b_ignore_errors ) free( ppsz_argv );
1079             return -1;
1080         }
1081     }
1082
1083     /* Free allocated resources */
1084     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1085         free( (char *)p_longopts[i_index].name );
1086     free( p_longopts );
1087     free( psz_shortopts );
1088     if( b_ignore_errors ) free( ppsz_argv );
1089
1090     return 0;
1091 }
1092
1093 /*****************************************************************************
1094  * config_GetHomeDir: find the user's home directory.
1095  *****************************************************************************
1096  * This function will try by different ways to find the user's home path.
1097  * Note that this function is not reentrant, it should be called only once
1098  * at the beginning of main where the result will be stored for later use.
1099  *****************************************************************************/
1100 char *config_GetHomeDir( void )
1101 {
1102     char *p_tmp, *p_homedir = NULL;
1103
1104 #if defined(HAVE_GETPWUID)
1105     struct passwd *p_pw = NULL;
1106 #endif
1107
1108 #ifdef WIN32
1109     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1110                                                LPTSTR );
1111 #   define CSIDL_FLAG_CREATE 0x8000
1112 #   define CSIDL_APPDATA 0x1A
1113 #   define SHGFP_TYPE_CURRENT 0
1114
1115     HINSTANCE shfolder_dll;
1116     SHGETFOLDERPATH SHGetFolderPath ;
1117
1118     /* load the shell32 dll to retreive SHGetFolderPath */
1119     if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1120     {
1121         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1122                                                   "SHGetFolderPathA" );
1123         if ( SHGetFolderPath != NULL )
1124         {
1125             p_homedir = (char *)malloc( MAX_PATH );
1126             if( !p_homedir )
1127             {
1128 //X                intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1129                 return NULL;
1130             }
1131
1132             /* get the "Application Data" folder for the current user */
1133             if( S_OK == SHGetFolderPath( NULL,
1134                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1135                                          NULL, SHGFP_TYPE_CURRENT,
1136                                          p_homedir ) )
1137             {
1138                 FreeLibrary( shfolder_dll );
1139                 return p_homedir;
1140             }
1141             free( p_homedir );
1142         }
1143         FreeLibrary( shfolder_dll );
1144     }
1145 #endif
1146
1147 #if defined(HAVE_GETPWUID)
1148     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1149 #endif
1150     {
1151         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1152         {
1153             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1154             {
1155                 p_homedir = strdup( "/tmp" );
1156             }
1157             else p_homedir = strdup( p_tmp );
1158
1159 //X            intf_ErrMsg( "config error: unable to get home directory, "
1160 //X                         "using %s instead", p_homedir );
1161
1162         }
1163         else p_homedir = strdup( p_tmp );
1164     }
1165 #if defined(HAVE_GETPWUID)
1166     else
1167     {
1168         p_homedir = strdup( p_pw->pw_dir );
1169     }
1170 #endif
1171
1172     return p_homedir;
1173 }