1 /*****************************************************************************
2 * file.c: configuration file handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "../libvlc.h"
30 #include "vlc_charset.h"
33 #include <errno.h> /* errno */
42 #include "configuration.h"
43 #include "modules/modules.h"
45 static char *ConfigKeyToString( int );
47 static inline char *strdupnull (const char *src)
49 return src ? strdup (src) : NULL;
53 * Get the user's configuration file
55 static char *config_GetConfigFile( void )
57 char *psz_dir = config_GetUserConfDir();
60 if( asprintf( &psz_configfile, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
61 psz_configfile = NULL;
63 return psz_configfile;
66 static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
68 char *psz_filename = libvlc_priv (p_obj->p_libvlc)->psz_configfile;
73 psz_filename = config_GetConfigFile();
76 msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
78 p_stream = utf8_fopen( psz_filename, mode );
79 if( p_stream == NULL && errno != ENOENT )
81 msg_Err( p_obj, "cannot open config file (%s): %m",
85 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
86 else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' )
88 /* This is the fallback for pre XDG Base Directory
89 * Specification configs */
91 if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
92 config_GetHomeDir() ) != -1 )
94 p_stream = utf8_fopen( psz_old, mode );
97 /* Old config file found. We want to write it at the
98 * new location now. */
99 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
100 "VLC will now use %s.", psz_old, psz_filename );
102 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
103 config_GetHomeDir() ) != -1 )
105 FILE *p_readme = utf8_fopen( psz_readme, "wt" );
108 fprintf( p_readme, "The VLC media player "
109 "configuration folder has moved to comply\n"
110 "with the XDG Base Directory Specification "
111 "version 0.6. Your\nconfiguration has been "
112 "copied to the new location:\n%s\nYou can "
113 "delete this directory and all its contents.",
124 else if( p_stream != NULL )
126 libvlc_priv (p_obj->p_libvlc)->psz_configfile = psz_filename;
133 static int strtoi (const char *str)
139 l = strtol (str, &end, 0);
143 if ((l > INT_MAX) || (l < INT_MIN))
152 /*****************************************************************************
153 * config_LoadConfigFile: loads the configuration file.
154 *****************************************************************************
155 * This function is called to load the config options stored in the config
157 *****************************************************************************/
158 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
160 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
163 file = config_OpenConfigFile (p_this, "rt");
167 /* Acquire config file lock */
168 vlc_mutex_lock( &priv->config_lock );
170 /* Look for the selected module, if NULL then save everything */
171 module_t **list = module_list_get (NULL);
173 /* Look for UTF-8 Byte Order Mark */
174 char * (*convert) (const char *) = strdupnull;
177 if ((fread (bom, 1, 3, file) != 3)
178 || memcmp (bom, "\xEF\xBB\xBF", 3))
180 convert = FromLocaleDup;
181 rewind (file); /* no BOM, rewind */
184 module_t *module = NULL;
185 char line[1024], section[1022];
188 /* Ensure consistent number formatting... */
189 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
190 locale_t baseloc = uselocale (loc);
192 while (fgets (line, 1024, file) != NULL)
194 /* Ignore comments and empty lines */
205 char *ptr = strchr (line, ']');
207 continue; /* syntax error; */
210 /* New section ( = a given module) */
211 strcpy (section, line + 1);
214 if ((psz_module_name == NULL)
215 || (strcmp (psz_module_name, section) == 0))
217 for (int i = 0; list[i]; i++)
219 module_t *m = list[i];
221 if ((strcmp (section, m->psz_object_name) == 0)
222 && (m->i_config_items > 0)) /* ignore config-less modules */
225 if (psz_module_name != NULL)
227 "loading config for module \"%s\"",
238 continue; /* no need to parse if there is no matching module */
240 char *ptr = strchr (line, '\n');
244 /* look for option name */
245 const char *psz_option_name = line;
247 ptr = strchr (line, '=');
249 continue; /* syntax error */
252 const char *psz_option_value = ptr + 1;
254 /* try to match this option with one of the module's options */
255 for (size_t i = 0; i < module->confsize; i++)
257 module_config_t *p_item = module->p_config + i;
259 if ((p_item->i_type & CONFIG_HINT)
260 || strcmp (p_item->psz_name, psz_option_name))
266 switch( p_item->i_type )
268 case CONFIG_ITEM_BOOL:
269 case CONFIG_ITEM_INTEGER:
271 long l = strtoi (psz_option_value);
273 msg_Warn (p_this, "Integer value (%s) for %s: %m",
274 psz_option_value, psz_option_name);
276 p_item->saved.i = p_item->value.i = (int)l;
280 case CONFIG_ITEM_FLOAT:
281 if( !*psz_option_value )
282 break; /* ignore empty option */
283 p_item->value.f = (float)atof (psz_option_value);
284 p_item->saved.f = p_item->value.f;
287 case CONFIG_ITEM_KEY:
288 if( !*psz_option_value )
289 break; /* ignore empty option */
290 p_item->value.i = ConfigStringToKey(psz_option_value);
291 p_item->saved.i = p_item->value.i;
295 vlc_mutex_lock( p_item->p_lock );
297 /* free old string */
298 free( (char*) p_item->value.psz );
299 free( (char*) p_item->saved.psz );
301 p_item->value.psz = convert (psz_option_value);
302 p_item->saved.psz = strdupnull (p_item->value.psz);
304 vlc_mutex_unlock( p_item->p_lock );
314 msg_Err (p_this, "error reading configuration: %m");
319 module_list_free (list);
320 if (loc != (locale_t)0)
326 vlc_mutex_unlock( &priv->config_lock );
330 /*****************************************************************************
331 * config_CreateDir: Create configuration directory if it doesn't exist.
332 *****************************************************************************/
333 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
335 if( !psz_dirname || !*psz_dirname ) return -1;
337 if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
347 /* Let's try to create the parent directory */
348 char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
349 strcpy( psz_parent, psz_dirname );
351 psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
352 if( psz_end && psz_end != psz_parent )
355 if( config_CreateDir( p_this, psz_parent ) == 0 )
357 if( !utf8_mkdir( psz_dirname, 0700 ) )
364 msg_Err( p_this, "could not create %s: %m", psz_dirname );
369 config_Write (FILE *file, const char *type, const char *desc,
370 bool comment, const char *name, const char *fmt, ...)
378 if (fprintf (file, "# %s (%s)\n%s%s=", desc, gettext (type),
379 comment ? "#" : "", name) < 0)
383 ret = vfprintf (file, fmt, ap);
388 if (fputs ("\n\n", file) == EOF)
394 /*****************************************************************************
395 * config_SaveConfigFile: Save a module's config options.
396 *****************************************************************************
397 * This will save the specified module's config options to the config file.
398 * If psz_module_name is NULL then we save all the modules config options.
399 * It's no use to save the config options that kept their default values, so
400 * we'll try to be a bit clever here.
402 * When we save we mustn't delete the config options of the modules that
403 * haven't been loaded. So we cannot just create a new config file with the
404 * config structures we've got in memory.
405 * I don't really know how to deal with this nicely, so I will use a completly
407 * I will load the config file in memory, but skipping all the sections of the
408 * modules we want to save. Then I will create a brand new file, dump the file
409 * loaded in memory and then append the sections of the modules we want to
412 *****************************************************************************/
413 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
416 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
419 char p_line[1024], *p_index2;
421 char *p_bigbuffer, *p_index;
425 /* Acquire config file lock */
426 vlc_mutex_lock( &priv->config_lock );
428 if( libvlc_priv (p_this->p_libvlc)->psz_configfile == NULL )
430 char *psz_configdir = config_GetUserConfDir();
431 if( !psz_configdir ) /* XXX: This should never happen */
433 msg_Err( p_this, "no configuration directory defined" );
434 vlc_mutex_unlock( &priv->config_lock );
438 config_CreateDir( p_this, psz_configdir );
439 free( psz_configdir );
442 file = config_OpenConfigFile( p_this, "rt" );
445 /* look for file size */
446 fseek( file, 0L, SEEK_END );
447 i_sizebuf = ftell( file );
448 fseek( file, 0L, SEEK_SET );
451 p_bigbuffer = p_index = malloc( i_sizebuf+1 );
454 if( file ) fclose( file );
455 vlc_mutex_unlock( &priv->config_lock );
460 /* List all available modules */
461 module_t **list = module_list_get (NULL);
463 /* backup file into memory, we only need to backup the sections we won't
466 while( file && fgets( p_line, 1024, file ) )
468 if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
471 /* we found a section, check if we need to do a backup */
472 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
474 if( ((p_index2 - &p_line[1])
475 == (int)strlen(p_parser->psz_object_name) )
476 && !memcmp( &p_line[1], p_parser->psz_object_name,
477 strlen(p_parser->psz_object_name) ) )
479 if( !psz_module_name )
481 else if( !strcmp( psz_module_name,
482 p_parser->psz_object_name ) )
487 if( list[i_index] == NULL )
489 /* we don't have this section in our list so we need to back
493 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
506 /* save line if requested and line is valid (doesn't begin with a
507 * space, tab, or eol) */
508 if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
509 && (p_line[0] != '\t') )
511 strcpy( p_index, p_line );
512 p_index += strlen( p_line );
515 if( file ) fclose( file );
519 * Save module config in file
522 file = config_OpenConfigFile (p_this, "wt");
525 module_list_free (list);
527 vlc_mutex_unlock( &priv->config_lock );
531 fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n"
532 "###\n### lines beginning with a '#' character are comments\n###\n\n" );
534 /* Ensure consistent number formatting... */
535 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
536 locale_t baseloc = uselocale (loc);
538 /* Look for the selected module, if NULL then save everything */
539 for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
541 module_config_t *p_item, *p_end;
543 if( psz_module_name && strcmp( psz_module_name,
544 p_parser->psz_object_name ) )
547 if( !p_parser->i_config_items )
550 if( psz_module_name )
551 msg_Dbg( p_this, "saving config for module \"%s\"",
552 p_parser->psz_object_name );
554 fprintf( file, "[%s]", p_parser->psz_object_name );
555 if( p_parser->psz_longname )
556 fprintf( file, " # %s\n\n", p_parser->psz_longname );
558 fprintf( file, "\n\n" );
560 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
564 /* Do not save the new value in the configuration file
565 * if doing an autosave, and the item is not an "autosaved" one. */
566 bool b_retain = b_autosave && !p_item->b_autosave;
568 if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
569 || p_item->b_removed /* ignore deprecated option */
570 || p_item->b_unsaveable) /* ignore volatile option */
573 if (IsConfigIntegerType (p_item->i_type))
575 int val = b_retain ? p_item->saved.i : p_item->value.i;
576 if (p_item->i_type == CONFIG_ITEM_KEY)
578 char *psz_key = ConfigKeyToString (val);
579 config_Write (file, p_item->psz_text, N_("key"),
580 val == p_item->orig.i,
581 p_item->psz_name, "%s",
582 psz_key ? psz_key : "");
586 config_Write (file, p_item->psz_text,
587 (p_item->i_type == CONFIG_ITEM_BOOL)
588 ? N_("boolean") : N_("integer"),
589 val == p_item->orig.i,
590 p_item->psz_name, "%d", val);
591 p_item->saved.i = val;
594 if (IsConfigFloatType (p_item->i_type))
596 float val = b_retain ? p_item->saved.f : p_item->value.f;
597 config_Write (file, p_item->psz_text, N_("float"),
598 val == p_item->orig.f,
599 p_item->psz_name, "%f", val);
600 p_item->saved.f = val;
604 const char *psz_value = b_retain ? p_item->saved.psz
608 assert (IsConfigStringType (p_item->i_type));
610 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
611 psz_value = p_item->orig.psz;
615 ? ((p_item->orig.psz != NULL)
616 ? (strcmp (psz_value, p_item->orig.psz) != 0)
618 : (p_item->orig.psz != NULL);
620 config_Write (file, p_item->psz_text, N_("string"),
621 !modified, p_item->psz_name, "%s",
622 psz_value ? psz_value : "");
627 free ((char *)p_item->saved.psz);
628 if( (psz_value && p_item->orig.psz &&
629 strcmp( psz_value, p_item->orig.psz )) ||
630 !psz_value || !p_item->orig.psz)
631 p_item->saved.psz = strdupnull (psz_value);
633 p_item->saved.psz = NULL;
638 p_item->b_dirty = false;
642 module_list_free (list);
643 if (loc != (locale_t)0)
650 * Restore old settings from the config in file
652 fputs( p_bigbuffer, file );
656 vlc_mutex_unlock( &priv->config_lock );
661 int config_AutoSaveConfigFile( vlc_object_t *p_this )
663 libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
669 /* Check if there's anything to save */
670 vlc_mutex_lock( &priv->config_lock );
671 module_t **list = module_list_get (NULL);
672 for( i_index = 0; list[i_index]; i_index++ )
674 module_t *p_parser = list[i_index];
675 module_config_t *p_item, *p_end;
677 if( !p_parser->i_config_items ) continue;
679 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
683 if( p_item->b_autosave && p_item->b_dirty ) break;
685 if( p_item < p_end ) break;
687 done = list[i_index] == NULL;
688 module_list_free (list);
689 vlc_mutex_unlock( &priv->config_lock );
691 return done ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
694 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
696 return SaveConfigFile( p_this, psz_module_name, false );
700 * Get the user's configuration file when given with the --config option
702 char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc )
704 char *psz_configfile = config_GetPsz( p_libvlc, "config" );
705 if( psz_configfile != NULL )
707 if( psz_configfile[0] == '~' && psz_configfile[1] == '/' )
709 /* This is incomplete: we should also support the ~cmassiot/ syntax */
711 if( asprintf( &psz_buf, "%s/%s", config_GetHomeDir(),
712 psz_configfile + 2 ) == -1 )
714 free( psz_configfile );
717 free( psz_configfile );
718 psz_configfile = psz_buf;
721 return psz_configfile;
724 int ConfigStringToKey( const char *psz_key )
728 const char *psz_parser = strchr( psz_key, '-' );
729 while( psz_parser && psz_parser != psz_key )
731 for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
733 if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
734 strlen( vlc_modifiers[i].psz_key_string ) ) )
736 i_key |= vlc_modifiers[i].i_key_code;
739 psz_key = psz_parser + 1;
740 psz_parser = strchr( psz_key, '-' );
742 for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
744 if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
746 i_key |= vlc_keys[i].i_key_code;
753 char *ConfigKeyToString( int i_key )
755 char *psz_key = malloc( 100 );
766 for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
769 if( i_key & vlc_modifiers[index].i_key_code )
771 p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
774 for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
777 if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
779 p += sprintf( p, "%s", vlc_keys[index].psz_key_string );