]> git.sesse.net Git - vlc/blob - src/config/file.c
Split the big config file
[vlc] / src / config / file.c
1 /*****************************************************************************
2  * file.c: configuration file handling
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include "../libvlc.h"
26 #include "vlc_charset.h"
27 #include "vlc_keys.h"
28
29 #include <errno.h>                                                  /* errno */
30
31 #ifdef HAVE_LIMITS_H
32 #   include <limits.h>
33 #endif
34
35 #include "config.h"
36 #include "modules/modules.h"
37
38 static char *ConfigKeyToString( int );
39
40 static inline char *strdupnull (const char *src)
41 {
42     return src ? strdup (src) : NULL;
43 }
44
45 static inline char *_strdupnull (const char *src)
46 {
47     return src ? strdup (_(src)) : NULL;
48 }
49
50
51 static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
52 {
53     char *psz_filename = p_obj->p_libvlc->psz_configfile;
54     FILE *p_stream;
55
56     if( !psz_filename )
57     {
58         psz_filename = config_GetConfigFile( p_obj->p_libvlc );
59     }
60
61     msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
62
63     p_stream = utf8_fopen( psz_filename, mode );
64     if( p_stream == NULL && errno != ENOENT )
65     {
66         msg_Err( p_obj, "cannot open config file (%s): %m",
67                  psz_filename );
68
69     }
70 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
71     else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' )
72     {
73         /* This is the fallback for pre XDG Base Directory
74          * Specification configs */
75         char *psz_old;
76         if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
77                   p_obj->p_libvlc->psz_homedir ) != -1 )
78         {
79             p_stream = utf8_fopen( psz_old, mode );
80             if( p_stream )
81             {
82                 /* Old config file found. We want to write it at the
83                  * new location now. */
84                 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
85                           "VLC will now use %s.", psz_old, psz_filename );
86                 char *psz_readme;
87                 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
88                               p_obj->p_libvlc->psz_homedir ) != -1 )
89                 {
90                     FILE *p_readme = utf8_fopen( psz_readme, "wt" );
91                     if( p_readme )
92                     {
93                         fputs( "The VLC media player configuration folder has "
94                                "moved to comply with the XDG Base "
95                                "Directory Specification version 0.6. Your "
96                                "configuration has been copied to the new "
97                                "location (", p_readme );
98                         fputs( p_obj->p_libvlc->psz_configdir, p_readme );
99                         fputs( "). You can delete this directory and "
100                                "all its contents.", p_readme );
101                         fclose( p_readme );
102                     }
103                     free( psz_readme );
104                 }
105             }
106             free( psz_old );
107         }
108     }
109 #endif
110     else if( p_stream != NULL )
111     {
112         p_obj->p_libvlc->psz_configfile = psz_filename;
113     }
114
115     return p_stream;
116 }
117
118
119 static int strtoi (const char *str)
120 {
121     char *end;
122     long l;
123
124     errno = 0;
125     l = strtol (str, &end, 0);
126
127     if (!errno)
128     {
129         if ((l > INT_MAX) || (l < INT_MIN))
130             errno = ERANGE;
131         if (*end)
132             errno = EINVAL;
133     }
134     return (int)l;
135 }
136
137
138 /*****************************************************************************
139  * config_LoadConfigFile: loads the configuration file.
140  *****************************************************************************
141  * This function is called to load the config options stored in the config
142  * file.
143  *****************************************************************************/
144 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
145 {
146     vlc_list_t *p_list;
147     FILE *file;
148
149     file = config_OpenConfigFile (p_this, "rt");
150     if (file == NULL)
151         return VLC_EGENERIC;
152
153     /* Acquire config file lock */
154     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
155
156     /* Look for the selected module, if NULL then save everything */
157     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
158
159     /* Look for UTF-8 Byte Order Mark */
160     char * (*convert) (const char *) = strdupnull;
161     char bom[3];
162
163     if ((fread (bom, 1, 3, file) != 3)
164      || memcmp (bom, "\xEF\xBB\xBF", 3))
165     {
166         convert = FromLocaleDup;
167         rewind (file); /* no BOM, rewind */
168     }
169
170     module_t *module = NULL;
171     char line[1024], section[1022];
172     section[0] = '\0';
173
174     while (fgets (line, 1024, file) != NULL)
175     {
176         /* Ignore comments and empty lines */
177         switch (line[0])
178         {
179             case '#':
180             case '\n':
181             case '\0':
182                 continue;
183         }
184
185         if (line[0] == '[')
186         {
187             char *ptr = strchr (line, ']');
188             if (ptr == NULL)
189                 continue; /* syntax error; */
190             *ptr = '\0';
191
192             /* New section ( = a given module) */
193             strcpy (section, line + 1);
194             module = NULL;
195
196             if ((psz_module_name == NULL)
197              || (strcmp (psz_module_name, section) == 0))
198             {
199                 for (int i = 0; i < p_list->i_count; i++)
200                 {
201                     module_t *m = (module_t *)p_list->p_values[i].p_object;
202
203                     if ((strcmp (section, m->psz_object_name) == 0)
204                      && (m->i_config_items > 0)) /* ignore config-less modules */
205                     {
206                         module = m;
207                         if (psz_module_name != NULL)
208                             msg_Dbg (p_this,
209                                      "loading config for module \"%s\"",
210                                      section);
211                         break;
212                     }
213                 }
214             }
215
216             continue;
217         }
218
219         if (module == NULL)
220             continue; /* no need to parse if there is no matching module */
221
222         char *ptr = strchr (line, '\n');
223         if (ptr != NULL)
224             *ptr = '\0';
225
226         /* look for option name */
227         const char *psz_option_name = line;
228
229         ptr = strchr (line, '=');
230         if (ptr == NULL)
231             continue; /* syntax error */
232
233         *ptr = '\0';
234         const char *psz_option_value = ptr + 1;
235
236         /* try to match this option with one of the module's options */
237         for (size_t i = 0; i < module->confsize; i++)
238         {
239             module_config_t *p_item = module->p_config + i;
240
241             if ((p_item->i_type & CONFIG_HINT)
242              || strcmp (p_item->psz_name, psz_option_name))
243                 continue;
244
245             /* We found it */
246             errno = 0;
247
248             switch( p_item->i_type )
249             {
250                 case CONFIG_ITEM_BOOL:
251                 case CONFIG_ITEM_INTEGER:
252                 {
253                     long l = strtoi (psz_option_value);
254                     if (errno)
255                         msg_Warn (p_this, "Integer value (%s) for %s: %m",
256                                   psz_option_value, psz_option_name);
257                     else
258                         p_item->saved.i = p_item->value.i = (int)l;
259                     break;
260                 }
261
262                 case CONFIG_ITEM_FLOAT:
263                     if( !*psz_option_value )
264                         break;                    /* ignore empty option */
265                     p_item->value.f = (float)i18n_atof( psz_option_value);
266                     p_item->saved.f = p_item->value.f;
267                     break;
268
269                 case CONFIG_ITEM_KEY:
270                     if( !*psz_option_value )
271                         break;                    /* ignore empty option */
272                     p_item->value.i = ConfigStringToKey(psz_option_value);
273                     p_item->saved.i = p_item->value.i;
274                     break;
275
276                 default:
277                     vlc_mutex_lock( p_item->p_lock );
278
279                     /* free old string */
280                     free( (char*) p_item->value.psz );
281                     free( (char*) p_item->saved.psz );
282
283                     p_item->value.psz = convert (psz_option_value);
284                     p_item->saved.psz = strdupnull (p_item->value.psz);
285
286                     vlc_mutex_unlock( p_item->p_lock );
287                     break;
288             }
289
290             break;
291         }
292     }
293
294     if (ferror (file))
295     {
296         msg_Err (p_this, "error reading configuration: %m");
297         clearerr (file);
298     }
299     fclose (file);
300
301     vlc_list_release( p_list );
302
303     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
304     return 0;
305 }
306
307 /*****************************************************************************
308  * config_CreateDir: Create configuration directory if it doesn't exist.
309  *****************************************************************************/
310 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
311 {
312     if( !psz_dirname && !*psz_dirname ) return -1;
313
314     if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
315         return 0;
316
317     switch( errno )
318     {
319         case EEXIST:
320             return 0;
321
322         case ENOENT:
323         {
324             /* Let's try to create the parent directory */
325             char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
326             strcpy( psz_parent, psz_dirname );
327
328             psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
329             if( psz_end && psz_end != psz_parent )
330             {
331                 *psz_end = '\0';
332                 if( config_CreateDir( p_this, psz_parent ) == 0 )
333                 {
334                     if( !utf8_mkdir( psz_dirname, 0700 ) )
335                         return 0;
336                 }
337             }
338         }
339     }
340
341     msg_Err( p_this, "could not create %s: %m", psz_dirname );
342     return -1;
343 }
344
345 /*****************************************************************************
346  * config_SaveConfigFile: Save a module's config options.
347  *****************************************************************************
348  * This will save the specified module's config options to the config file.
349  * If psz_module_name is NULL then we save all the modules config options.
350  * It's no use to save the config options that kept their default values, so
351  * we'll try to be a bit clever here.
352  *
353  * When we save we mustn't delete the config options of the modules that
354  * haven't been loaded. So we cannot just create a new config file with the
355  * config structures we've got in memory.
356  * I don't really know how to deal with this nicely, so I will use a completly
357  * dumb method ;-)
358  * I will load the config file in memory, but skipping all the sections of the
359  * modules we want to save. Then I will create a brand new file, dump the file
360  * loaded in memory and then append the sections of the modules we want to
361  * save.
362  * Really stupid no ?
363  *****************************************************************************/
364 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
365                            vlc_bool_t b_autosave )
366 {
367     module_t *p_parser;
368     vlc_list_t *p_list;
369     FILE *file;
370     char p_line[1024], *p_index2;
371     int i_sizebuf = 0;
372     char *p_bigbuffer, *p_index;
373     vlc_bool_t b_backup;
374     int i_index;
375
376     /* Acquire config file lock */
377     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
378
379     if( p_this->p_libvlc->psz_configfile == NULL )
380     {
381         const char *psz_configdir = p_this->p_libvlc->psz_configdir;
382         if( !psz_configdir ) /* XXX: This should never happen */
383         {
384             msg_Err( p_this, "no configuration directory defined" );
385             vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
386             return -1;
387         }
388
389         config_CreateDir( p_this, psz_configdir );
390     }
391
392     file = config_OpenConfigFile( p_this, "rt" );
393     if( file != NULL )
394     {
395         /* look for file size */
396         fseek( file, 0L, SEEK_END );
397         i_sizebuf = ftell( file );
398         fseek( file, 0L, SEEK_SET );
399     }
400
401     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
402     if( !p_bigbuffer )
403     {
404         msg_Err( p_this, "out of memory" );
405         if( file ) fclose( file );
406         vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
407         return -1;
408     }
409     p_bigbuffer[0] = 0;
410
411     /* List all available modules */
412     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
413
414     /* backup file into memory, we only need to backup the sections we won't
415      * save later on */
416     b_backup = 0;
417     while( file && fgets( p_line, 1024, file ) )
418     {
419         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
420         {
421
422             /* we found a section, check if we need to do a backup */
423             for( i_index = 0; i_index < p_list->i_count; i_index++ )
424             {
425                 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
426
427                 if( ((p_index2 - &p_line[1])
428                        == (int)strlen(p_parser->psz_object_name) )
429                     && !memcmp( &p_line[1], p_parser->psz_object_name,
430                                 strlen(p_parser->psz_object_name) ) )
431                 {
432                     if( !psz_module_name )
433                         break;
434                     else if( !strcmp( psz_module_name,
435                                       p_parser->psz_object_name ) )
436                         break;
437                 }
438             }
439
440             if( i_index == p_list->i_count )
441             {
442                 /* we don't have this section in our list so we need to back
443                  * it up */
444                 *p_index2 = 0;
445 #if 0
446                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
447                                  &p_line[1] );
448 #endif
449                 *p_index2 = ']';
450
451                 b_backup = 1;
452             }
453             else
454             {
455                 b_backup = 0;
456             }
457         }
458
459         /* save line if requested and line is valid (doesn't begin with a
460          * space, tab, or eol) */
461         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
462             && (p_line[0] != '\t') )
463         {
464             strcpy( p_index, p_line );
465             p_index += strlen( p_line );
466         }
467     }
468     if( file ) fclose( file );
469
470
471     /*
472      * Save module config in file
473      */
474
475     file = config_OpenConfigFile (p_this, "wt");
476     if( !file )
477     {
478         vlc_list_release( p_list );
479         free( p_bigbuffer );
480         vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
481         return -1;
482     }
483
484     fprintf( file, "\xEF\xBB\xBF###\n###  " COPYRIGHT_MESSAGE "\n###\n\n"
485        "###\n### lines begining with a '#' character are comments\n###\n\n" );
486
487     /* Look for the selected module, if NULL then save everything */
488     for( i_index = 0; i_index < p_list->i_count; i_index++ )
489     {
490         module_config_t *p_item, *p_end;
491         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
492
493         if( psz_module_name && strcmp( psz_module_name,
494                                        p_parser->psz_object_name ) )
495             continue;
496
497         if( !p_parser->i_config_items )
498             continue;
499
500         if( psz_module_name )
501             msg_Dbg( p_this, "saving config for module \"%s\"",
502                      p_parser->psz_object_name );
503
504         fprintf( file, "[%s]", p_parser->psz_object_name );
505         if( p_parser->psz_longname )
506             fprintf( file, " # %s\n\n", p_parser->psz_longname );
507         else
508             fprintf( file, "\n\n" );
509
510         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
511              p_item < p_end;
512              p_item++ )
513         {
514             char  *psz_key;
515             int   i_value = p_item->value.i;
516             float f_value = p_item->value.f;
517             const char  *psz_value = p_item->value.psz;
518
519             if( p_item->i_type & CONFIG_HINT )
520                 /* ignore hints */
521                 continue;
522             /* Ignore deprecated options */
523             if( p_item->psz_current )
524                 continue;
525             if( p_item->b_unsaveable )
526                 /*obvious*/
527                 continue;
528
529             if( b_autosave && !p_item->b_autosave )
530             {
531                 i_value = p_item->saved.i;
532                 f_value = p_item->saved.f;
533                 psz_value = p_item->saved.psz;
534                 if( !psz_value ) psz_value = p_item->orig.psz;
535             }
536             else
537             {
538                 p_item->b_dirty = VLC_FALSE;
539             }
540
541             switch( p_item->i_type )
542             {
543             case CONFIG_ITEM_BOOL:
544             case CONFIG_ITEM_INTEGER:
545                 if( p_item->psz_text )
546                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
547                              (p_item->i_type == CONFIG_ITEM_BOOL) ?
548                              _("boolean") : _("integer") );
549                 if( i_value == p_item->orig.i )
550                     fputc ('#', file);
551                 fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
552
553                 p_item->saved.i = i_value;
554                 break;
555
556             case CONFIG_ITEM_KEY:
557                 if( p_item->psz_text )
558                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
559                              _("key") );
560                 if( i_value == p_item->orig.i )
561                     fputc ('#', file);
562                 psz_key = ConfigKeyToString( i_value );
563                 fprintf( file, "%s=%s\n", p_item->psz_name,
564                          psz_key ? psz_key : "" );
565                 free (psz_key);
566
567                 p_item->saved.i = i_value;
568                 break;
569
570             case CONFIG_ITEM_FLOAT:
571                 if( p_item->psz_text )
572                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
573                              _("float") );
574                 if( f_value == p_item->orig.f )
575                     fputc ('#', file);
576                 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
577
578                 p_item->saved.f = f_value;
579                 break;
580
581             default:
582                 if( p_item->psz_text )
583                     fprintf( file, "# %s (%s)\n", p_item->psz_text,
584                              _("string") );
585                 if( (!psz_value && !p_item->orig.psz) ||
586                     (psz_value && p_item->orig.psz &&
587                      !strcmp( psz_value, p_item->orig.psz )) )
588                     fputc ('#', file);
589                 fprintf( file, "%s=%s\n", p_item->psz_name,
590                          psz_value ?: "" );
591
592                 if( b_autosave && !p_item->b_autosave ) break;
593
594                 free ((char *)p_item->saved.psz);
595                 if( (psz_value && p_item->orig.psz &&
596                      strcmp( psz_value, p_item->orig.psz )) ||
597                     !psz_value || !p_item->orig.psz)
598                     p_item->saved.psz = strdupnull (psz_value);
599                 else
600                     p_item->saved.psz = NULL;
601             }
602         }
603
604         fputc ('\n', file);
605     }
606
607     vlc_list_release( p_list );
608
609     /*
610      * Restore old settings from the config in file
611      */
612     fputs( p_bigbuffer, file );
613     free( p_bigbuffer );
614
615     fclose( file );
616     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
617
618     return 0;
619 }
620
621 int config_AutoSaveConfigFile( vlc_object_t *p_this )
622 {
623     vlc_list_t *p_list;
624     int i_index, i_count;
625
626     if( !p_this ) return -1;
627
628     /* Check if there's anything to save */
629     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
630     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
631     i_count = p_list->i_count;
632     for( i_index = 0; i_index < i_count; i_index++ )
633     {
634         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
635         module_config_t *p_item, *p_end;
636
637         if( !p_parser->i_config_items ) continue;
638
639         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
640              p_item < p_end;
641              p_item++ )
642         {
643             if( p_item->b_autosave && p_item->b_dirty ) break;
644         }
645         break;
646     }
647     vlc_list_release( p_list );
648     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
649
650     if( i_index == i_count ) return VLC_SUCCESS;
651     return SaveConfigFile( p_this, 0, VLC_TRUE );
652 }
653
654 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
655 {
656     return SaveConfigFile( p_this, psz_module_name, VLC_FALSE );
657 }
658
659 /**
660  * Get the user's configuration file
661  */
662 char *config_GetConfigFile( libvlc_int_t *p_libvlc )
663 {
664     char *psz_configfile;
665     if( asprintf( &psz_configfile, "%s" DIR_SEP CONFIG_FILE,
666                   p_libvlc->psz_configdir ) == -1 )
667         return NULL;
668     return psz_configfile;
669 }
670
671 /**
672  * Get the user's configuration file when given with the --config option
673  */
674 char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc )
675 {
676     char *psz_configfile = config_GetPsz( p_libvlc, "config" );
677     if( psz_configfile != NULL )
678     {
679         if( psz_configfile[0] == '~' && psz_configfile[1] == '/' )
680         {
681             /* This is incomplete: we should also support the ~cmassiot/ syntax */
682             char *psz_buf;
683             if( asprintf( &psz_buf, "%s/%s", p_libvlc->psz_homedir,
684                           psz_configfile + 2 ) == -1 )
685             {
686                 free( psz_configfile );
687                 return NULL;
688             }
689             free( psz_configfile );
690             psz_configfile = psz_buf;
691         }
692     }
693     return psz_configfile;
694 }
695
696 int ConfigStringToKey( const char *psz_key )
697 {
698     int i_key = 0;
699     unsigned int i;
700     const char *psz_parser = strchr( psz_key, '-' );
701     while( psz_parser && psz_parser != psz_key )
702     {
703         for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
704         {
705             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
706                               strlen( vlc_modifiers[i].psz_key_string ) ) )
707             {
708                 i_key |= vlc_modifiers[i].i_key_code;
709             }
710         }
711         psz_key = psz_parser + 1;
712         psz_parser = strchr( psz_key, '-' );
713     }
714     for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
715     {
716         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
717         {
718             i_key |= vlc_keys[i].i_key_code;
719             break;
720         }
721     }
722     return i_key;
723 }
724
725 char *ConfigKeyToString( int i_key )
726 {
727     char *psz_key = malloc( 100 );
728     char *p;
729     size_t index;
730
731     if ( !psz_key )
732     {
733         return NULL;
734     }
735     *psz_key = '\0';
736     p = psz_key;
737
738     for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
739          index++ )
740     {
741         if( i_key & vlc_modifiers[index].i_key_code )
742         {
743             p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
744         }
745     }
746     for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
747          index++)
748     {
749         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
750         {
751             p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
752             break;
753         }
754     }
755     return psz_key;
756 }
757