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