]> git.sesse.net Git - vlc/blobdiff - src/modules/configuration.c
Adds CONFIG_ITEM_PASSWORD for showing stars in preferences dialog (still to be implem...
[vlc] / src / modules / configuration.c
index 9f54778e2878089a51d5532501dde23c53519a39..3d8eb51a7f15384302241f7f44983526a203e1ae 100644 (file)
@@ -22,6 +22,7 @@
  *****************************************************************************/
 
 #include <vlc/vlc.h>
+#include "../libvlc.h"
 #include "vlc_keys.h"
 #include "vlc_charset.h"
 
 #endif
 
 #include "configuration.h"
+#include "modules/modules.h"
 
 static int ConfigStringToKey( const char * );
 static char *ConfigKeyToString( int );
 
-static inline void freenull (const void *p)
-{
-    if (p != NULL)
-        free ((void *)p);
-}
-
 static inline char *strdupnull (const char *src)
 {
     if (src == NULL)
@@ -88,6 +84,38 @@ static inline char *_strdupnull (const char *src)
     return strdup (_(src));
 }
 
+/* Item types that use a string value (i.e. serialized in the module cache) */
+int IsConfigStringType (int type)
+{
+    static const unsigned char config_types[] = 
+    {
+        CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
+        CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
+        CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT
+    };
+
+    /* NOTE: this needs to be changed if we ever get more than 255 types */
+    return memchr (config_types, type, sizeof (config_types)) != NULL;
+}
+
+
+static int IsConfigIntegerType (int type)
+{
+    static const unsigned char config_types[] = 
+    {
+        CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
+        CONFIG_CATEGORY, CONFIG_SUBCATEGORY
+    };
+
+    return memchr (config_types, type, sizeof (config_types)) != NULL;
+}
+
+
+static inline int IsConfigFloatType (int type)
+{
+    return type == CONFIG_ITEM_FLOAT;
+}
+
 
 /*****************************************************************************
  * config_GetType: get the type of a variable (bool, int, float, string)
@@ -133,6 +161,10 @@ int __config_GetType( vlc_object_t *p_this, const char *psz_name )
         i_type = VLC_VAR_STRING;
         break;
 
+    case CONFIG_ITEM_PASSWORD:
+        i_type = VLC_VAR_STRING;
+        break;
+
     case CONFIG_ITEM_FILE:
         i_type = VLC_VAR_FILE;
         break;
@@ -211,7 +243,7 @@ float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
  *****************************************************************************
  * This function is used to get the value of variables which are internally
  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
- * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
+ * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
  *
  * Important note: remember to free() the returned char* because it's a
  *   duplicate of the actual value. It isn't safe to return a pointer to the
@@ -249,7 +281,7 @@ char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
  *****************************************************************************
  * This function is used to set the value of variables which are internally
  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
- * CONFIG_ITEM_DIRECTORY, and CONFIG_ITEM_MODULE).
+ * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
  *****************************************************************************/
 void __config_PutPsz( vlc_object_t *p_this,
                       const char *psz_name, const char *psz_value )
@@ -645,9 +677,9 @@ void config_Free( module_t *p_module )
 
         if (IsConfigStringType (p_item->i_type))
         {
-            freenull (p_item->value.psz);
-            freenull (p_item->orig.psz);
-            freenull (p_item->saved.psz);
+            free ((char *)p_item->value.psz);
+            free ((char *)p_item->orig.psz);
+            free ((char *)p_item->saved.psz);
         }
 
         if( p_item->i_list )
@@ -743,7 +775,7 @@ void __config_ResetAll( vlc_object_t *p_this )
             else
             if (IsConfigStringType (p_module->p_config[i].i_type))
             {
-                freenull (p_module->p_config[i].value.psz);
+                free ((char *)p_module->p_config[i].value.psz);
                 p_module->p_config[i].value.psz =
                         strdupnull (p_module->p_config[i].orig.psz);
             }
@@ -791,6 +823,25 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode )
 }
 
 
+static int strtoi (const char *str)
+{
+    char *end;
+    long l;
+
+    errno = 0;
+    l = strtol (str, &end, 0);
+
+    if (!errno)
+    {
+        if ((l > INT_MAX) || (l < INT_MIN))
+            errno = ERANGE;
+        if (*end)
+            errno = EINVAL;
+    }
+    return (int)l;
+}
+
+
 /*****************************************************************************
  * config_LoadConfigFile: loads the configuration file.
  *****************************************************************************
@@ -899,27 +950,28 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
                 continue;
 
             /* We found it */
+            errno = 0;
+
             switch( p_item->i_type )
             {
                 case CONFIG_ITEM_BOOL:
                 case CONFIG_ITEM_INTEGER:
-                    if( !*psz_option_value )
-                        break;                    /* ignore empty option */
-                    p_item->value.i = strtol( psz_option_value, 0, 0 );
-                    p_item->saved.i = p_item->value.i;
-
-                    /*msg_Dbg (p_this, "option \"%s\", value %i",
-                             psz_option_name, p_item->value.i);*/
+                {
+                    long l = strtoi (psz_option_value);
+                    if (errno)
+                        msg_Warn (p_this, "Integer value (%s) for %s: %s",
+                                  psz_option_value, psz_option_name,
+                                  strerror (errno));
+                    else
+                        p_item->saved.i = p_item->value.i = (int)l;
                     break;
+                }
 
                 case CONFIG_ITEM_FLOAT:
                     if( !*psz_option_value )
                         break;                    /* ignore empty option */
                     p_item->value.f = (float)i18n_atof( psz_option_value);
                     p_item->saved.f = p_item->value.f;
-
-                    /*msg_Dbg (p_this, "option \"%s\", value %f",
-                             psz_option_name, (double)p_item->value.f);*/
                     break;
 
                 case CONFIG_ITEM_KEY:
@@ -940,9 +992,6 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
                     p_item->saved.psz = strdupnull (p_item->value.psz);
 
                     vlc_mutex_unlock( p_item->p_lock );
-
-                    /*msg_Dbg (p_this, "option \"%s\", value \"%s\"",
-                             psz_option_name, psz_option_value ?: "");*/
                     break;
             }
 
@@ -950,9 +999,14 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         }
     }
 
-    vlc_list_release( p_list );
+    if (ferror (file))
+    {
+        msg_Err (p_this, "error reading configuration: %s", strerror (errno));
+        clearerr (file);
+    }
+    fclose (file);
 
-    fclose( file );
+    vlc_list_release( p_list );
 
     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
     return 0;
@@ -1197,7 +1251,7 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
                 psz_key = ConfigKeyToString( i_value );
                 fprintf( file, "%s=%s\n", p_item->psz_name,
                          psz_key ? psz_key : "" );
-                freenull (psz_key);
+                free (psz_key);
 
                 p_item->saved.i = i_value;
                 break;
@@ -1226,7 +1280,7 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
 
                 if( b_autosave && !p_item->b_autosave ) break;
 
-                freenull (p_item->saved.psz);
+                free ((char *)p_item->saved.psz);
                 if( (psz_value && p_item->orig.psz &&
                      strcmp( psz_value, p_item->orig.psz )) ||
                     !psz_value || !p_item->orig.psz)
@@ -1506,37 +1560,29 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
                 /* Check if the option is deprecated */
                 if( p_conf->psz_current )
                 {
-                    if( !strcmp(p_conf->psz_current,"SUPPRESSED") )
+                    if( p_conf->b_strict )
                     {
-                        if( !b_ignore_errors )
-                        {
-                            fprintf(stderr,
-                                    "Warning: option --%s is no longer used.\n",
-                                    p_conf->psz_name);
-                        }
+                        fprintf(stderr,
+                                "Warning: option --%s no longer exists.\n",
+                                p_conf->psz_name);
                        continue;
                     }
+
+                    fprintf( stderr,
+                             "%s: option --%s is deprecated. Use --%s instead.\n",
+                             b_ignore_errors ? "Warning" : "Error",
+                             p_conf->psz_name, p_conf->psz_current);
                     if( !b_ignore_errors )
                     {
-                        if( p_conf->b_strict )
-                        {
-                            fprintf( stderr,
-                                     "Error: option --%s is deprecated. "
-                                     "Use --%s instead.\n",
-                                     p_conf->psz_name, p_conf->psz_current);
-                            /*free */
-                            for( i_index = 0; p_longopts[i_index].name; i_index++ )
-                                free( (char *)p_longopts[i_index].name );
-
-                            free( p_longopts );
-                            free( psz_shortopts );
-                            return -1;
-                        }
-                        fprintf(stderr,
-                                "Warning: option --%s is deprecated. "
-                                "You should use --%s instead.\n",
-                                p_conf->psz_name, p_conf->psz_current);
+                        /*free */
+                        for( i_index = 0; p_longopts[i_index].name; i_index++ )
+                             free( (char *)p_longopts[i_index].name );
+
+                        free( p_longopts );
+                        free( psz_shortopts );
+                        return -1;
                     }
+  
                     psz_name = (char *)p_conf->psz_current;
                     p_conf = config_FindConfig( p_this, psz_name );
                 }
@@ -1544,6 +1590,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
                 switch( p_conf->i_type )
                 {
                     case CONFIG_ITEM_STRING:
+                    case CONFIG_ITEM_PASSWORD:
                     case CONFIG_ITEM_FILE:
                     case CONFIG_ITEM_DIRECTORY:
                     case CONFIG_ITEM_MODULE:
@@ -1575,6 +1622,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
             switch( pp_shortopts[i_cmd]->i_type )
             {
                 case CONFIG_ITEM_STRING:
+                case CONFIG_ITEM_PASSWORD:
                 case CONFIG_ITEM_FILE:
                 case CONFIG_ITEM_DIRECTORY:
                 case CONFIG_ITEM_MODULE:
@@ -1663,22 +1711,21 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
  *
  * @return a string (always succeeds).
  */
-const char *config_GetDataDir( const vlc_object_t *p_this )
+const char *config_GetDataDir( void )
 {
 #if defined (WIN32) || defined (UNDER_CE)
-    return p_this->p_libvlc_global->psz_vlcpath;
+    return vlc_global()->psz_vlcpath;
 #elif defined(__APPLE__) || defined (SYS_BEOS)
     static char path[PATH_MAX] = "";
 
     if( *path == '\0' )
     {
         snprintf( path, sizeof( path ), "%s/share",
-                  p_this->p_libvlc_global->psz_vlcpath );
+                  vlc_global()->psz_vlcpath );
         path[sizeof( path ) - 1] = '\0';
     }
     return path;
 #else
-    (void)p_this;
     return DATA_PATH;
 #endif
 }