]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.33 2002/07/31 20:56:53 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->p_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_type = p_orig[i].psz_type ?
341                                    strdup( _(p_orig[i].psz_type) ) : NULL;
342         p_module->p_config[i].psz_name = p_orig[i].psz_name ?
343                                    strdup( _(p_orig[i].psz_name) ) : NULL;
344         p_module->p_config[i].psz_text = p_orig[i].psz_text ?
345                                    strdup( _(p_orig[i].psz_text) ) : NULL;
346         p_module->p_config[i].psz_longtext = p_orig[i].psz_longtext ?
347                                    strdup( _(p_orig[i].psz_longtext) ) : NULL;
348         p_module->p_config[i].psz_value = p_orig[i].psz_value ?
349                                    strdup( p_orig[i].psz_value ) : NULL;
350
351         p_module->p_config[i].p_lock = &p_module->object_lock;
352
353         /* duplicate the string list */
354         p_module->p_config[i].ppsz_list = NULL;
355         if( p_orig[i].ppsz_list )
356         {
357             for( j = 0; p_orig[i].ppsz_list[j]; j++ );
358             p_module->p_config[i].ppsz_list = malloc( (j+1) *sizeof(char *) );
359             if( p_module->p_config[i].ppsz_list )
360             {
361                 for( j = 0; p_orig[i].ppsz_list[j]; j++ )
362                     p_module->p_config[i].ppsz_list[j] =
363                         strdup( p_orig[i].ppsz_list[j] );
364             }
365             p_module->p_config[i].ppsz_list[j] = NULL;
366         }
367
368         /* the callback pointer is only valid when the module is loaded so this
369          * value is set in module_activate() and reset in module_deactivate() */
370         p_module->p_config[i].pf_callback = NULL;
371     }
372 }
373
374 /*****************************************************************************
375  * config_Free: frees a duplicated module's configuration data.
376  *****************************************************************************
377  * This function frees all the data duplicated by config_Duplicate.
378  *****************************************************************************/
379 void config_Free( module_t *p_module )
380 {
381     module_config_t *p_item = p_module->p_config;
382     int i;
383
384     for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
385     {
386         if( p_item->psz_type )
387             free( p_item->psz_type );
388
389         if( p_item->psz_name )
390             free( p_item->psz_name );
391
392         if( p_item->psz_text )
393             free( p_item->psz_text );
394
395         if( p_item->psz_longtext )
396             free( p_item->psz_longtext );
397
398         if( p_item->psz_value )
399             free( p_item->psz_value );
400
401         if( p_item->ppsz_list )
402         {
403             for( i = 0; p_item->ppsz_list[i]; i++ )
404                 free(p_item->ppsz_list[i]);
405             free( p_item->ppsz_list );
406         }
407     }
408
409     free( p_module->p_config );
410     p_module->p_config = NULL;
411
412     /* Remove the global lock */
413     vlc_mutex_destroy( &p_module->object_lock );
414 }
415
416 /*****************************************************************************
417  * config_SetCallbacks: sets callback functions in the duplicate p_config.
418  *****************************************************************************
419  * Unfortunatly we cannot work directly with the module's config data as
420  * this module might be unloaded from memory at any time (remember HideModule).
421  * This is why we need to duplicate callbacks each time we reload the module.
422  *****************************************************************************/
423 void config_SetCallbacks( module_config_t *p_new, module_config_t *p_orig )
424 {
425     while( p_new->i_type != CONFIG_HINT_END )
426     {
427         p_new->pf_callback = p_orig->pf_callback;
428         p_new++;
429         p_orig++;
430     }
431 }
432
433 /*****************************************************************************
434  * config_UnsetCallbacks: unsets callback functions in the duplicate p_config.
435  *****************************************************************************
436  * We simply undo what we did in config_SetCallbacks.
437  *****************************************************************************/
438 void config_UnsetCallbacks( module_config_t *p_new )
439 {
440     while( p_new->i_type != CONFIG_HINT_END )
441     {
442         p_new->pf_callback = NULL;
443         p_new++;
444     }
445 }
446
447 /*****************************************************************************
448  * config_LoadConfigFile: loads the configuration file.
449  *****************************************************************************
450  * This function is called to load the config options stored in the config
451  * file.
452  *****************************************************************************/
453 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
454 {
455     module_t *p_module;
456     module_config_t *p_item;
457     FILE *file;
458     char line[1024];
459     char *p_index, *psz_option_name, *psz_option_value;
460     char *psz_filename, *psz_homedir;
461
462     /* Acquire config file lock */
463     vlc_mutex_lock( &p_this->p_vlc->config_lock );
464
465     psz_homedir = p_this->p_vlc->psz_homedir;
466     if( !psz_homedir )
467     {
468         msg_Err( p_this, "psz_homedir is null" );
469         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
470         return -1;
471     }
472     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
473                                    strlen(psz_homedir) + 1 );
474     if( !psz_filename )
475     {
476         msg_Err( p_this, "out of memory" );
477         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
478         return -1;
479     }
480     sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
481
482     msg_Dbg( p_this, "opening config file %s", psz_filename );
483
484     file = fopen( psz_filename, "rt" );
485     if( !file )
486     {
487         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
488         free( psz_filename );
489         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
490         return -1;
491     }
492
493     /* Look for the selected module, if NULL then save everything */
494     for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
495          p_module = p_module->next )
496     {
497
498         if( psz_module_name
499              && strcmp( psz_module_name, p_module->psz_object_name ) )
500         {
501             continue;
502         }
503
504         /* The config file is organized in sections, one per module. Look for
505          * the interesting section ( a section is of the form [foo] ) */
506         rewind( file );
507         while( fgets( line, 1024, file ) )
508         {
509             if( (line[0] == '[') && (p_index = strchr(line,']')) &&
510                 (p_index - &line[1] == strlen(p_module->psz_object_name) ) &&
511                 !memcmp( &line[1], p_module->psz_object_name,
512                          strlen(p_module->psz_object_name) ) )
513             {
514                 msg_Dbg( p_this, "loading config for module \"%s\"",
515                                  p_module->psz_object_name );
516
517                 break;
518             }
519         }
520         /* either we found the section or we're at the EOF */
521
522         /* Now try to load the options in this section */
523         while( fgets( line, 1024, file ) )
524         {
525             if( line[0] == '[' ) break; /* end of section */
526
527             /* ignore comments or empty lines */
528             if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
529                 continue;
530
531             /* get rid of line feed */
532             if( line[strlen(line)-1] == '\n' )
533                 line[strlen(line)-1] = (char)0;
534
535             /* look for option name */
536             psz_option_name = line;
537             psz_option_value = NULL;
538             p_index = strchr( line, '=' );
539             if( !p_index ) break; /* this ain't an option!!! */
540
541             *p_index = (char)0;
542             psz_option_value = p_index + 1;
543
544             /* try to match this option with one of the module's options */
545             for( p_item = p_module->p_config;
546                  p_item->i_type != CONFIG_HINT_END;
547                  p_item++ )
548             {
549                 if( p_item->i_type & CONFIG_HINT )
550                     /* ignore hints */
551                     continue;
552
553                 if( !strcmp( p_item->psz_name, psz_option_name ) )
554                 {
555                     /* We found it */
556                     switch( p_item->i_type )
557                     {
558                     case CONFIG_ITEM_BOOL:
559                     case CONFIG_ITEM_INTEGER:
560                         if( !*psz_option_value )
561                             break;                    /* ignore empty option */
562                         p_item->i_value = atoi( psz_option_value);
563                         msg_Dbg( p_this, "option \"%s\", value %i",
564                                  p_item->psz_name, p_item->i_value );
565                         break;
566
567                     case CONFIG_ITEM_FLOAT:
568                         if( !*psz_option_value )
569                             break;                    /* ignore empty option */
570                         p_item->f_value = (float)atof( psz_option_value);
571                         msg_Dbg( p_this, "option \"%s\", value %f",
572                                  p_item->psz_name, (double)p_item->f_value );
573                         break;
574
575                     default:
576                         vlc_mutex_lock( p_item->p_lock );
577
578                         /* free old string */
579                         if( p_item->psz_value )
580                             free( p_item->psz_value );
581
582                         p_item->psz_value = *psz_option_value ?
583                             strdup( psz_option_value ) : NULL;
584
585                         vlc_mutex_unlock( p_item->p_lock );
586
587                         msg_Dbg( p_this, "option \"%s\", value \"%s\"",
588                                  p_item->psz_name,
589                                  p_item->psz_value ? p_item->psz_value : "" );
590                         break;
591                     }
592                 }
593             }
594         }
595
596     }
597     
598     fclose( file );
599     free( psz_filename );
600
601     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
602
603     return 0;
604 }
605
606 /*****************************************************************************
607  * config_SaveConfigFile: Save a module's config options.
608  *****************************************************************************
609  * This will save the specified module's config options to the config file.
610  * If psz_module_name is NULL then we save all the modules config options.
611  * It's no use to save the config options that kept their default values, so
612  * we'll try to be a bit clever here.
613  *
614  * When we save we mustn't delete the config options of the modules that
615  * haven't been loaded. So we cannot just create a new config file with the
616  * config structures we've got in memory. 
617  * I don't really know how to deal with this nicely, so I will use a completly
618  * dumb method ;-)
619  * I will load the config file in memory, but skipping all the sections of the
620  * modules we want to save. Then I will create a brand new file, dump the file
621  * loaded in memory and then append the sections of the modules we want to
622  * save.
623  * Really stupid no ?
624  *****************************************************************************/
625 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
626 {
627     module_t *p_module;
628     module_config_t *p_item;
629     FILE *file;
630     char p_line[1024], *p_index2;
631     int i_sizebuf = 0;
632     char *p_bigbuffer, *p_index;
633     vlc_bool_t b_backup;
634     char *psz_filename, *psz_homedir;
635
636     /* Acquire config file lock */
637     vlc_mutex_lock( &p_this->p_vlc->config_lock );
638
639     psz_homedir = p_this->p_vlc->psz_homedir;
640     if( !psz_homedir )
641     {
642         msg_Err( p_this, "psz_homedir is null" );
643         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
644         return -1;
645     }
646     psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
647                                    strlen(psz_homedir) + 1 );
648     if( !psz_filename )
649     {
650         msg_Err( p_this, "out of memory" );
651         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
652         return -1;
653     }
654     sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
655
656 #ifndef WIN32
657     if( mkdir( psz_filename, 0755 ) && errno != EEXIST )
658 #else
659     if( mkdir( psz_filename ) && errno != EEXIST )
660 #endif
661     {
662         msg_Err( p_this, "could not create %s (%s)",
663                          psz_filename, strerror(errno) );
664     }
665
666     strcat( psz_filename, "/" CONFIG_FILE );
667
668
669     msg_Dbg( p_this, "opening config file %s", psz_filename );
670
671     file = fopen( psz_filename, "rt" );
672     if( !file )
673     {
674         msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
675     }
676     else
677     {
678         /* look for file size */
679         fseek( file, 0, SEEK_END );
680         i_sizebuf = ftell( file );
681         rewind( file );
682     }
683
684     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
685     if( !p_bigbuffer )
686     {
687         msg_Err( p_this, "out of memory" );
688         if( file ) fclose( file );
689         free( psz_filename );
690         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
691         return -1;
692     }
693     p_bigbuffer[0] = 0;
694
695     /* backup file into memory, we only need to backup the sections we won't
696      * save later on */
697     b_backup = 0;
698     while( file && fgets( p_line, 1024, file ) )
699     {
700         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
701         {
702             /* we found a section, check if we need to do a backup */
703             for( p_module = p_this->p_vlc->p_module_bank->first;
704                  p_module != NULL;
705                  p_module = p_module->next )
706             {
707                 if( ((p_index2 - &p_line[1])
708                        == strlen(p_module->psz_object_name) ) &&
709                     !memcmp( &p_line[1], p_module->psz_object_name,
710                              strlen(p_module->psz_object_name) ) )
711                 {
712                     if( !psz_module_name )
713                         break;
714                     else if( !strcmp( psz_module_name,
715                                       p_module->psz_object_name ) )
716                         break;
717                 }
718             }
719
720             if( !p_module )
721             {
722                 /* we don't have this section in our list so we need to back
723                  * it up */
724                 *p_index2 = 0;
725                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
726                                  &p_line[1] );
727                 *p_index2 = ']';
728
729                 b_backup = 1;
730             }
731             else
732             {
733                 b_backup = 0;
734             }
735         }
736
737         /* save line if requested and line is valid (doesn't begin with a
738          * space, tab, or eol) */
739         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
740             && (p_line[0] != '\t') )
741         {
742             strcpy( p_index, p_line );
743             p_index += strlen( p_line );
744         }
745     }
746     if( file ) fclose( file );
747
748
749     /*
750      * Save module config in file
751      */
752
753     file = fopen( psz_filename, "wt" );
754     if( !file )
755     {
756         msg_Warn( p_this, "could not open config file %s for writing",
757                           psz_filename );
758         free( psz_filename );
759         vlc_mutex_unlock( &p_this->p_vlc->config_lock );
760         return -1;
761     }
762
763     fprintf( file, "###\n###  " COPYRIGHT_MESSAGE "\n###\n\n" );
764
765     /* Look for the selected module, if NULL then save everything */
766     for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
767          p_module = p_module->next )
768     {
769
770         if( psz_module_name && strcmp( psz_module_name,
771                                        p_module->psz_object_name ) )
772             continue;
773
774         if( !p_module->i_config_items )
775             continue;
776
777         msg_Dbg( p_this, "saving config for module \"%s\"",
778                          p_module->psz_object_name );
779
780         fprintf( file, "[%s]", p_module->psz_object_name );
781         if( p_module->psz_longname )
782             fprintf( file, " # %s\n\n", p_module->psz_longname );
783         else
784             fprintf( file, "\n\n" );
785
786         for( p_item = p_module->p_config;
787              p_item->i_type != CONFIG_HINT_END;
788              p_item++ )
789         {
790             if( p_item->i_type & CONFIG_HINT )
791                 /* ignore hints */
792                 continue;
793
794             switch( p_item->i_type )
795             {
796             case CONFIG_ITEM_BOOL:
797             case CONFIG_ITEM_INTEGER:
798                 if( p_item->psz_text )
799                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
800                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
801                              _("boolean") : _("integer") );
802                 fprintf( file, "%s=%i\n", p_item->psz_name, p_item->i_value );
803                 break;
804
805             case CONFIG_ITEM_FLOAT:
806                 if( p_item->psz_text )
807                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
808                              _("float") );
809                 fprintf( file, "%s=%f\n", p_item->psz_name,
810                          (double)p_item->f_value );
811                 break;
812
813             default:
814                 if( p_item->psz_text )
815                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
816                              _("string") );
817                 fprintf( file, "%s=%s\n", p_item->psz_name,
818                          p_item->psz_value ? p_item->psz_value : "" );
819             }
820         }
821
822         fprintf( file, "\n" );
823     }
824
825
826     /*
827      * Restore old settings from the config in file
828      */
829     fputs( p_bigbuffer, file );
830     free( p_bigbuffer );
831
832     fclose( file );
833     free( psz_filename );
834     vlc_mutex_unlock( &p_this->p_vlc->config_lock );
835
836     return 0;
837 }
838
839 /*****************************************************************************
840  * config_LoadCmdLine: parse command line
841  *****************************************************************************
842  * Parse command line for configuration options.
843  * Now that the module_bank has been initialized, we can dynamically
844  * generate the longopts structure used by getops. We have to do it this way
845  * because we don't know (and don't want to know) in advance the configuration
846  * options used (ie. exported) by each module.
847  *****************************************************************************/
848 int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
849                           vlc_bool_t b_ignore_errors )
850 {
851     int i_cmd, i_index, i_opts, i_shortopts, flag;
852     module_t *p_module;
853     module_config_t *p_item;
854     struct option *p_longopts;
855
856     /* Short options */
857     module_config_t *pp_shortopts[256];
858     char *psz_shortopts;
859
860     /* Set default configuration and copy arguments */
861     p_this->p_vlc->i_argc    = *pi_argc;
862     p_this->p_vlc->ppsz_argv = ppsz_argv;
863
864     p_this->p_vlc->p_channel = NULL;
865
866 #ifdef SYS_DARWIN
867     /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
868      * is the PSN - process serial number (a unique PID-ish thingie)
869      * still ok for real Darwin & when run from command line */
870     if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
871                                         /* for example -psn_0_9306113 */
872     {
873         /* GDMF!... I can't do this or else the MacOSX window server will
874          * not pick up the PSN and not register the app and we crash...
875          * hence the following kludge otherwise we'll get confused w/ argv[1]
876          * being an input file name */
877 #if 0
878         ppsz_argv[ 1 ] = NULL;
879 #endif
880         *pi_argc = *pi_argc - 1;
881         pi_argc--;
882         return 0;
883     }
884 #endif
885
886     /*
887      * Generate the longopts and shortopts structures used by getopt_long
888      */
889
890     i_opts = 0;
891     for( p_module = p_this->p_vlc->p_module_bank->first;
892          p_module != NULL ;
893          p_module = p_module->next )
894     {
895         /* count the number of exported configuration options (to allocate
896          * longopts). We also need to allocate space for too options when
897          * dealing with boolean to allow for --foo and --no-foo */
898         i_opts += (p_module->i_config_items + 2 * p_module->i_bool_items);
899     }
900
901     p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
902     if( p_longopts == NULL )
903     {
904         msg_Err( p_this, "out of memory" );
905         return -1;
906     }
907
908     psz_shortopts = malloc( sizeof( char ) * (2 * i_opts + 1) );
909     if( psz_shortopts == NULL )
910     {
911         msg_Err( p_this, "out of memory" );
912         free( p_longopts );
913         return -1;
914     }
915
916     /* If we are requested to ignore errors, then we must work on a copy
917      * of the ppsz_argv array, otherwise getopt_long will reorder it for
918      * us, ignoring the arity of the options */
919     if( b_ignore_errors )
920     {
921         ppsz_argv = (char**)malloc( *pi_argc * sizeof(char *) );
922         if( ppsz_argv == NULL )
923         {
924             msg_Err( p_this, "out of memory" );
925             free( psz_shortopts );
926             free( p_longopts );
927             return -1;
928         }
929         memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
930                 *pi_argc * sizeof(char *) );
931     }
932
933     i_shortopts = 0;
934     for( i_index = 0; i_index < 256; i_index++ )
935     {
936         pp_shortopts[i_index] = NULL;
937     }
938
939     /* Fill the p_longopts and psz_shortopts structures */
940     i_index = 0;
941     for( p_module = p_this->p_vlc->p_module_bank->first ;
942          p_module != NULL ;
943          p_module = p_module->next )
944     {
945         for( p_item = p_module->p_config;
946              p_item->i_type != CONFIG_HINT_END;
947              p_item++ )
948         {
949             /* Ignore hints */
950             if( p_item->i_type & CONFIG_HINT )
951                 continue;
952
953             /* Add item to long options */
954             p_longopts[i_index].name = strdup( p_item->psz_name );
955             if( p_longopts[i_index].name == NULL ) continue;
956             p_longopts[i_index].has_arg =
957                 (p_item->i_type == CONFIG_ITEM_BOOL)?
958                                                no_argument : required_argument;
959             p_longopts[i_index].flag = &flag;
960             p_longopts[i_index].val = 0;
961             i_index++;
962
963             /* When dealing with bools we also need to add the --no-foo
964              * option */
965             if( p_item->i_type == CONFIG_ITEM_BOOL )
966             {
967                 char *psz_name = malloc( strlen(p_item->psz_name) + 3 );
968                 if( psz_name == NULL ) continue;
969                 strcpy( psz_name, "no" );
970                 strcat( psz_name, p_item->psz_name );
971
972                 p_longopts[i_index].name = psz_name;
973                 p_longopts[i_index].has_arg = no_argument;
974                 p_longopts[i_index].flag = &flag;
975                 p_longopts[i_index].val = 1;
976                 i_index++;
977
978                 psz_name = malloc( strlen(p_item->psz_name) + 4 );
979                 if( psz_name == NULL ) continue;
980                 strcpy( psz_name, "no-" );
981                 strcat( psz_name, p_item->psz_name );
982
983                 p_longopts[i_index].name = psz_name;
984                 p_longopts[i_index].has_arg = no_argument;
985                 p_longopts[i_index].flag = &flag;
986                 p_longopts[i_index].val = 1;
987                 i_index++;
988             }
989
990             /* If item also has a short option, add it */
991             if( p_item->i_short )
992             {
993                 pp_shortopts[(int)p_item->i_short] = p_item;
994                 psz_shortopts[i_shortopts] = p_item->i_short;
995                 i_shortopts++;
996                 if( p_item->i_type != CONFIG_ITEM_BOOL )
997                 {
998                     psz_shortopts[i_shortopts] = ':';
999                     i_shortopts++;
1000                 }
1001             }
1002         }
1003     }
1004
1005     /* Close the longopts and shortopts structures */
1006     memset( &p_longopts[i_index], 0, sizeof(struct option) );
1007     psz_shortopts[i_shortopts] = '\0';
1008
1009     /*
1010      * Parse the command line options
1011      */
1012     opterr = 0;
1013     optind = 1;
1014     while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv, psz_shortopts,
1015                                   p_longopts, &i_index ) ) != EOF )
1016     {
1017         /* A long option has been recognized */
1018         if( i_cmd == 0 )
1019         {
1020             module_config_t *p_conf;
1021             char *psz_name = (char *)p_longopts[i_index].name;
1022
1023             /* Check if we deal with a --nofoo or --no-foo long option */
1024             if( flag ) psz_name += psz_name[2] == '-' ? 3 : 2;
1025
1026             /* Store the configuration option */
1027             p_conf = config_FindConfig( p_this, psz_name );
1028
1029             if( p_conf ) switch( p_conf->i_type )
1030             {
1031             case CONFIG_ITEM_STRING:
1032             case CONFIG_ITEM_FILE:
1033             case CONFIG_ITEM_MODULE:
1034                 config_PutPsz( p_this, psz_name, optarg );
1035                 break;
1036             case CONFIG_ITEM_INTEGER:
1037                 config_PutInt( p_this, psz_name, atoi(optarg));
1038                 break;
1039             case CONFIG_ITEM_FLOAT:
1040                 config_PutFloat( p_this, psz_name, (float)atof(optarg) );
1041                 break;
1042             case CONFIG_ITEM_BOOL:
1043                 config_PutInt( p_this, psz_name, !flag );
1044                 break;
1045             }
1046
1047             continue;
1048         }
1049
1050         /* A short option has been recognized */
1051         if( pp_shortopts[i_cmd] != NULL )
1052         {
1053             switch( pp_shortopts[i_cmd]->i_type )
1054             {
1055             case CONFIG_ITEM_STRING:
1056             case CONFIG_ITEM_FILE:
1057             case CONFIG_ITEM_MODULE:
1058                 config_PutPsz( p_this, pp_shortopts[i_cmd]->psz_name, optarg );
1059                 break;
1060             case CONFIG_ITEM_INTEGER:
1061                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name,
1062                                        atoi(optarg));
1063                 break;
1064             case CONFIG_ITEM_BOOL:
1065                 config_PutInt( p_this, pp_shortopts[i_cmd]->psz_name, 1 );
1066                 break;
1067             }
1068
1069             continue;
1070         }
1071
1072         /* Internal error: unknown option */
1073         if( !b_ignore_errors )
1074         {
1075             fprintf( stderr, "unknown option `%s'\n", ppsz_argv[optind-1] );
1076             fprintf( stderr, "Try `%s --help' for more information.\n",
1077                              p_this->p_vlc->psz_object_name );
1078
1079             free( p_longopts );
1080             free( psz_shortopts );
1081             if( b_ignore_errors ) free( ppsz_argv );
1082             return -1;
1083         }
1084     }
1085
1086     /* Free allocated resources */
1087     for( i_index = 0; p_longopts[i_index].name; i_index++ )
1088         free( (char *)p_longopts[i_index].name );
1089     free( p_longopts );
1090     free( psz_shortopts );
1091     if( b_ignore_errors ) free( ppsz_argv );
1092
1093     return 0;
1094 }
1095
1096 /*****************************************************************************
1097  * config_GetHomeDir: find the user's home directory.
1098  *****************************************************************************
1099  * This function will try by different ways to find the user's home path.
1100  * Note that this function is not reentrant, it should be called only once
1101  * at the beginning of main where the result will be stored for later use.
1102  *****************************************************************************/
1103 char *config_GetHomeDir( void )
1104 {
1105     char *p_tmp, *p_homedir = NULL;
1106
1107 #if defined(HAVE_GETPWUID)
1108     struct passwd *p_pw = NULL;
1109 #endif
1110
1111 #ifdef WIN32
1112     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
1113                                                LPTSTR );
1114 #   define CSIDL_FLAG_CREATE 0x8000
1115 #   define CSIDL_APPDATA 0x1A
1116 #   define SHGFP_TYPE_CURRENT 0
1117
1118     HINSTANCE shfolder_dll;
1119     SHGETFOLDERPATH SHGetFolderPath ;
1120
1121     /* load the shell32 dll to retreive SHGetFolderPath */
1122     if( ( shfolder_dll = LoadLibrary("shfolder.dll") ) != NULL )
1123     {
1124         SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
1125                                                   "SHGetFolderPathA" );
1126         if ( SHGetFolderPath != NULL )
1127         {
1128             p_homedir = (char *)malloc( MAX_PATH );
1129             if( !p_homedir )
1130             {
1131 //X                intf_ErrMsg( "config error: couldn't malloc p_homedir" );
1132                 return NULL;
1133             }
1134
1135             /* get the "Application Data" folder for the current user */
1136             if( S_OK == SHGetFolderPath( NULL,
1137                                          CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1138                                          NULL, SHGFP_TYPE_CURRENT,
1139                                          p_homedir ) )
1140             {
1141                 FreeLibrary( shfolder_dll );
1142                 return p_homedir;
1143             }
1144             free( p_homedir );
1145         }
1146         FreeLibrary( shfolder_dll );
1147     }
1148 #endif
1149
1150 #if defined(HAVE_GETPWUID)
1151     if( ( p_pw = getpwuid( getuid() ) ) == NULL )
1152 #endif
1153     {
1154         if( ( p_tmp = getenv( "HOME" ) ) == NULL )
1155         {
1156             if( ( p_tmp = getenv( "TMP" ) ) == NULL )
1157             {
1158                 p_homedir = strdup( "/tmp" );
1159             }
1160             else p_homedir = strdup( p_tmp );
1161
1162 //X            intf_ErrMsg( "config error: unable to get home directory, "
1163 //X                         "using %s instead", p_homedir );
1164
1165         }
1166         else p_homedir = strdup( p_tmp );
1167     }
1168 #if defined(HAVE_GETPWUID)
1169     else
1170     {
1171         p_homedir = strdup( p_pw->pw_dir );
1172     }
1173 #endif
1174
1175     return p_homedir;
1176 }