summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2007-12-01 00:35:57 +0100
committerSebastien Helleu <flashcode@flashtux.org>2007-12-01 00:35:57 +0100
commit59fb878b77320844d7d2150c95ce22c253ffb4e6 (patch)
treedbb9ee486748b0c64fcf558732ae643474e33b3f /src/plugins
parent32cc6ae5d8a2c0753eb2ef8ecb7194f0afff4302 (diff)
downloadweechat-59fb878b77320844d7d2150c95ce22c253ffb4e6.zip
New config functions, almost entirely rewritten from scratch
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/plugin-api.c82
-rw-r--r--src/plugins/plugin-config.c219
-rw-r--r--src/plugins/plugin-config.h22
-rw-r--r--src/plugins/plugin-list.c2
-rw-r--r--src/plugins/plugin.c26
5 files changed, 161 insertions, 190 deletions
diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c
index 97202d154..7c91277e3 100644
--- a/src/plugins/plugin-api.c
+++ b/src/plugins/plugin-api.c
@@ -41,6 +41,7 @@
#include "../core/wee-utf8.h"
#include "../core/wee-util.h"
#include "../gui/gui-chat.h"
+#include "../gui/gui-color.h"
#include "../gui/gui-infobar.h"
#include "../gui/gui-input.h"
#include "../gui/gui-keyboard.h"
@@ -1110,26 +1111,22 @@ plugin_api_get_config_str_value (struct t_config_option *option)
switch (option->type)
{
- case OPTION_TYPE_BOOLEAN:
- return (*((int *)(option->ptr_int))) ?
+ case CONFIG_OPTION_BOOLEAN:
+ return (CONFIG_BOOLEAN(option) == CONFIG_BOOLEAN_TRUE) ?
strdup ("on") : strdup ("off");
- break;
- case OPTION_TYPE_INT:
- snprintf (buf_temp, sizeof (buf_temp), "%d",
- *((int *)(option->ptr_int)));
+ case CONFIG_OPTION_INTEGER:
+ if (option->string_values)
+ snprintf (buf_temp, sizeof (buf_temp), "%s",
+ option->string_values[CONFIG_INTEGER(option)]);
+ else
+ snprintf (buf_temp, sizeof (buf_temp), "%d",
+ CONFIG_INTEGER(option));
return strdup (buf_temp);
- break;
- case OPTION_TYPE_INT_WITH_STRING:
- return strdup (option->array_values[*((int *)(option->ptr_int))]);
- break;
- case OPTION_TYPE_STRING:
- return (*((char **)(option->ptr_string))) ?
- strdup (*((char **)(option->ptr_string))) : strdup ("");
- break;
- case OPTION_TYPE_COLOR:
- color_name = gui_color_get_name (*((int *)(option->ptr_int)));
+ case CONFIG_OPTION_STRING:
+ return strdup (CONFIG_STRING(option));
+ case CONFIG_OPTION_COLOR:
+ color_name = gui_color_get_name (CONFIG_INTEGER(option));
return (color_name) ? strdup (color_name) : strdup ("");
- break;
}
/* should never be executed! */
@@ -1137,7 +1134,7 @@ plugin_api_get_config_str_value (struct t_config_option *option)
}
/*
- * plugin_api_config_get: get value of a config option
+ * plugin_api_config_get: get value of a WeeChat config option
*/
char *
@@ -1147,11 +1144,9 @@ plugin_api_config_get (struct t_weechat_plugin *plugin, char *option_name)
/* make C compiler happy */
(void) plugin;
-
- /* search a WeeChat command */
- ptr_option = config_option_section_option_search (weechat_config_sections,
- weechat_config_options,
- option_name);
+
+ /* search WeeChat config option */
+ ptr_option = config_file_search_option (weechat_config, NULL, option_name);
if (ptr_option)
return plugin_api_get_config_str_value (ptr_option);
@@ -1168,6 +1163,7 @@ plugin_api_config_set (struct t_weechat_plugin *plugin, char *option_name,
char *value)
{
struct t_config_option *ptr_option;
+ int rc;
/* make C compiler happy */
(void) plugin;
@@ -1175,22 +1171,16 @@ plugin_api_config_set (struct t_weechat_plugin *plugin, char *option_name,
if (!option_name || !value)
return 0;
- /* search and set WeeChat option if found */
- ptr_option = config_option_section_option_search (weechat_config_sections,
- weechat_config_options,
- option_name);
+ /* search and set WeeChat config option if found */
+ ptr_option = config_file_search_option (weechat_config, NULL, option_name);
if (ptr_option)
{
- if (ptr_option->handler_change)
- {
- if (config_option_set (ptr_option, value) == 0)
- {
- (void) (ptr_option->handler_change());
- return 1;
- }
- }
- else
+ rc = config_file_option_set (ptr_option, value);
+ if ((rc == 2) && (ptr_option->callback_change))
+ (void) (ptr_option->callback_change) ();
+ if (rc == 0)
return 0;
+ return 1;
}
/* failed to set config option */
@@ -1202,16 +1192,16 @@ plugin_api_config_set (struct t_weechat_plugin *plugin, char *option_name,
*/
char *
-plugin_api_plugin_config_get (struct t_weechat_plugin *plugin, char *option)
+plugin_api_plugin_config_get (struct t_weechat_plugin *plugin, char *option_name)
{
- struct t_plugin_option *ptr_plugin_option;
+ struct t_config_option *ptr_option;
- if (!option)
+ if (!option_name)
return NULL;
- ptr_plugin_option = plugin_config_search (plugin->name, option);
- if (ptr_plugin_option)
- return (ptr_plugin_option->value) ? strdup (ptr_plugin_option->value) : NULL;
+ ptr_option = plugin_config_search (plugin->name, option_name);
+ if (ptr_option)
+ return (ptr_option->value) ? strdup (ptr_option->value) : NULL;
/* option not found */
return NULL;
@@ -1222,13 +1212,13 @@ plugin_api_plugin_config_get (struct t_weechat_plugin *plugin, char *option)
*/
int
-plugin_api_plugin_config_set (struct t_weechat_plugin *plugin, char *option,
- char *value)
+plugin_api_plugin_config_set (struct t_weechat_plugin *plugin,
+ char *option_name, char *value)
{
- if (!option)
+ if (!option_name)
return 0;
- if (plugin_config_set (plugin->name, option, value))
+ if (plugin_config_set (plugin->name, option_name, value))
{
plugin_config_write ();
return 1;
diff --git a/src/plugins/plugin-config.c b/src/plugins/plugin-config.c
index 197fe1182..b621e0a71 100644
--- a/src/plugins/plugin-config.c
+++ b/src/plugins/plugin-config.c
@@ -37,20 +37,9 @@
#include "plugin-config.h"
-struct t_plugin_option *plugin_options = NULL;
-struct t_plugin_option *last_plugin_option = NULL;
-
-char *plugin_config_sections[] =
-{ "plugin", NULL };
-
-struct t_config_option *plugin_config_options[] =
-{ NULL, NULL };
-
-t_config_func_read_option *plugin_config_read_functions[] =
-{ plugin_config_read_option, NULL };
-
-t_config_func_write_options *plugin_config_write_functions[] =
-{ plugin_config_write_options, NULL };
+struct t_config_file *plugin_config = NULL;
+struct t_config_option *plugin_options = NULL;
+struct t_config_option *last_plugin_option = NULL;
/*
@@ -58,17 +47,17 @@ t_config_func_write_options *plugin_config_write_functions[] =
* This function should not be called directly.
*/
-struct t_plugin_option *
-plugin_config_search_internal (char *option)
+struct t_config_option *
+plugin_config_search_internal (char *option_name)
{
- struct t_plugin_option *ptr_plugin_option;
+ struct t_config_option *ptr_option;
- for (ptr_plugin_option = plugin_options; ptr_plugin_option;
- ptr_plugin_option = ptr_plugin_option->next_option)
+ for (ptr_option = plugin_options; ptr_option;
+ ptr_option = ptr_option->next_option)
{
- if (string_strcasecmp (ptr_plugin_option->name, option) == 0)
+ if (string_strcasecmp (ptr_option->name, option_name) == 0)
{
- return ptr_plugin_option;
+ return ptr_option;
}
}
@@ -80,36 +69,36 @@ plugin_config_search_internal (char *option)
* plugin_config_search: search a plugin option
*/
-struct t_plugin_option *
-plugin_config_search (char *plugin_name, char *option)
+struct t_config_option *
+plugin_config_search (char *plugin_name, char *option_name)
{
char *internal_option;
- struct t_plugin_option *ptr_plugin_option;
+ struct t_config_option *ptr_option;
internal_option = (char *)malloc (strlen (plugin_name) +
- strlen (option) + 2);
+ strlen (option_name) + 2);
if (!internal_option)
return NULL;
strcpy (internal_option, plugin_name);
strcat (internal_option, ".");
- strcat (internal_option, option);
+ strcat (internal_option, option_name);
- ptr_plugin_option = plugin_config_search_internal (internal_option);
+ ptr_option = plugin_config_search_internal (internal_option);
free (internal_option);
- return ptr_plugin_option;
+ return ptr_option;
}
/*
* plugin_config_find_pos: find position for a plugin option (for sorting options)
*/
-struct t_plugin_option *
+struct t_config_option *
plugin_config_find_pos (char *name)
{
- struct t_plugin_option *ptr_option;
+ struct t_config_option *ptr_option;
for (ptr_option = plugin_options; ptr_option;
ptr_option = ptr_option->next_option)
@@ -117,6 +106,8 @@ plugin_config_find_pos (char *name)
if (string_strcasecmp (name, ptr_option->name) < 0)
return ptr_option;
}
+
+ /* position not found (we will add to the end of list) */
return NULL;
}
@@ -129,33 +120,33 @@ plugin_config_find_pos (char *name)
int
plugin_config_set_internal (char *option, char *value)
{
- struct t_plugin_option *ptr_plugin_option, *pos_option;
+ struct t_config_option *ptr_option, *pos_option;
int rc;
rc = 0;
- ptr_plugin_option = plugin_config_search_internal (option);
- if (ptr_plugin_option)
+ ptr_option = plugin_config_search_internal (option);
+ if (ptr_option)
{
if (!value || !value[0])
{
/* remove option from list */
- if (ptr_plugin_option->prev_option)
- (ptr_plugin_option->prev_option)->next_option =
- ptr_plugin_option->next_option;
+ if (ptr_option->prev_option)
+ (ptr_option->prev_option)->next_option =
+ ptr_option->next_option;
else
- plugin_options = ptr_plugin_option->next_option;
- if (ptr_plugin_option->next_option)
- (ptr_plugin_option->next_option)->prev_option =
- ptr_plugin_option->prev_option;
+ plugin_options = ptr_option->next_option;
+ if (ptr_option->next_option)
+ (ptr_option->next_option)->prev_option =
+ ptr_option->prev_option;
rc = 1;
}
else
{
/* replace old value by new one */
- if (ptr_plugin_option->value)
- free (ptr_plugin_option->value);
- ptr_plugin_option->value = strdup (value);
+ if (ptr_option->value)
+ free (ptr_option->value);
+ ptr_option->value = strdup (value);
rc = 1;
}
}
@@ -163,43 +154,50 @@ plugin_config_set_internal (char *option, char *value)
{
if (value && value[0])
{
- ptr_plugin_option = (struct t_plugin_option *)malloc (sizeof (struct t_plugin_option));
- if (ptr_plugin_option)
+ ptr_option = (struct t_config_option *)malloc (sizeof (struct t_config_option));
+ if (ptr_option)
{
/* create new option */
- ptr_plugin_option->name = strdup (option);
- string_tolower (ptr_plugin_option->name);
- ptr_plugin_option->value = strdup (value);
+ ptr_option->name = strdup (option);
+ string_tolower (ptr_option->name);
+ ptr_option->type = CONFIG_OPTION_STRING;
+ ptr_option->description = NULL;
+ ptr_option->string_values = NULL;
+ ptr_option->min = 0;
+ ptr_option->max = 0;
+ ptr_option->default_value = NULL;
+ ptr_option->value = strdup (value);
+ ptr_option->callback_change = NULL;
if (plugin_options)
{
- pos_option = plugin_config_find_pos (ptr_plugin_option->name);
+ pos_option = plugin_config_find_pos (ptr_option->name);
if (pos_option)
{
/* insert option into the list (before option found) */
- ptr_plugin_option->prev_option = pos_option->prev_option;
- ptr_plugin_option->next_option = pos_option;
+ ptr_option->prev_option = pos_option->prev_option;
+ ptr_option->next_option = pos_option;
if (pos_option->prev_option)
- pos_option->prev_option->next_option = ptr_plugin_option;
+ pos_option->prev_option->next_option = ptr_option;
else
- plugin_options = ptr_plugin_option;
- pos_option->prev_option = ptr_plugin_option;
+ plugin_options = ptr_option;
+ pos_option->prev_option = ptr_option;
}
else
{
/* add option to the end */
- ptr_plugin_option->prev_option = last_plugin_option;
- ptr_plugin_option->next_option = NULL;
- last_plugin_option->next_option = ptr_plugin_option;
- last_plugin_option = ptr_plugin_option;
+ ptr_option->prev_option = last_plugin_option;
+ ptr_option->next_option = NULL;
+ last_plugin_option->next_option = ptr_option;
+ last_plugin_option = ptr_option;
}
}
else
{
- ptr_plugin_option->prev_option = NULL;
- ptr_plugin_option->next_option = NULL;
- plugin_options = ptr_plugin_option;
- last_plugin_option = ptr_plugin_option;
+ ptr_option->prev_option = NULL;
+ ptr_option->next_option = NULL;
+ plugin_options = ptr_option;
+ last_plugin_option = ptr_option;
}
rc = 1;
}
@@ -220,19 +218,19 @@ plugin_config_set_internal (char *option, char *value)
*/
int
-plugin_config_set (char *plugin_name, char *option, char *value)
+plugin_config_set (char *plugin_name, char *option_name, char *value)
{
char *internal_option;
int return_code;
internal_option = (char *)malloc (strlen (plugin_name) +
- strlen (option) + 2);
+ strlen (option_name) + 2);
if (!internal_option)
return 0;
strcpy (internal_option, plugin_name);
strcat (internal_option, ".");
- strcat (internal_option, option);
+ strcat (internal_option, option_name);
return_code = plugin_config_set_internal (internal_option, value);
free (internal_option);
@@ -247,76 +245,67 @@ plugin_config_set (char *plugin_name, char *option, char *value)
* -2 = bad format/value
*/
-int
-plugin_config_read_option (struct t_config_option *options,
+void
+plugin_config_read_option (struct t_config_file *config_file,
char *option_name, char *value)
{
char *value2;
/* make C compiler happy */
- (void) options;
+ (void) config_file;
- if (option_name)
- {
- value2 = string_iconv_to_internal (NULL, value);
- plugin_config_set_internal (option_name,
- (value2) ? value2 : value);
- if (value2)
- free (value2);
- }
- else
+ value2 = string_iconv_to_internal (NULL, value);
+ plugin_config_set_internal (option_name,
+ (value2) ? value2 : value);
+ if (value2)
+ free (value2);
+}
+
+/*
+ * plugin_config_write_options: write plugin options in configuration file
+ */
+
+void
+plugin_config_write_options (struct t_config_file *config_file)
+{
+ struct t_config_option *ptr_option;
+
+ for (ptr_option = plugin_options; ptr_option;
+ ptr_option = ptr_option->next_option)
{
- /* does nothing for new [plugin] section */
+ config_file_write_line (config_file,
+ ptr_option->name,
+ ptr_option->value);
}
-
- /* all ok */
- return 0;
-}
+}
/*
* plugin_config_read: read WeeChat plugins configuration file
* return: 0 = successful
* -1 = config file file not found
* -2 = error in config file
+ * -3 = not enough memory
*/
int
plugin_config_read ()
{
- return config_file_read (plugin_config_sections, plugin_config_options,
- plugin_config_read_functions,
- plugin_config_read_option,
- NULL,
- WEECHAT_PLUGIN_CONFIG_NAME);
-}
-
-/*
- * plugin_config_write_options: write plugin options in configuration file
- * Return: 0 = successful
- * -1 = write error
- */
-
-int
-plugin_config_write_options (FILE *file, char *section_name,
- struct t_config_option *options)
-{
- /* make C compiler happy */
- (void) options;
-
- struct t_plugin_option *ptr_plugin_option;
-
- string_iconv_fprintf (file, "\n[%s]\n", section_name);
-
- for (ptr_plugin_option = plugin_options; ptr_plugin_option;
- ptr_plugin_option = ptr_plugin_option->next_option)
+ if (!plugin_config)
{
- string_iconv_fprintf (file, "%s = \"%s\"\n",
- ptr_plugin_option->name,
- ptr_plugin_option->value);
+ plugin_config = config_file_new (PLUGIN_CONFIG_FILENAME);
+ if (plugin_config)
+ {
+ config_file_new_section (plugin_config, "plugin",
+ &plugin_config_read_option,
+ &plugin_config_write_options,
+ NULL);
+ }
}
- /* all ok */
- return 0;
+ if (!plugin_config)
+ return -3;
+
+ return config_file_read (plugin_config);
}
/*
@@ -329,7 +318,5 @@ int
plugin_config_write ()
{
log_printf (_("Saving plugins configuration to disk\n"));
- return config_file_write (plugin_config_sections, plugin_config_options,
- plugin_config_write_functions,
- WEECHAT_PLUGIN_CONFIG_NAME);
+ return config_file_write (plugin_config, 0);
}
diff --git a/src/plugins/plugin-config.h b/src/plugins/plugin-config.h
index 27ce60b4f..7558388ba 100644
--- a/src/plugins/plugin-config.h
+++ b/src/plugins/plugin-config.h
@@ -20,28 +20,16 @@
#ifndef __WEECHAT_PLUGIN_CONFIG_H
#define __WEECHAT_PLUGIN_CONFIG_H 1
-#include "../core/wee-config-option.h"
+#define PLUGIN_CONFIG_FILENAME "plugins.rc"
-#define WEECHAT_PLUGIN_CONFIG_NAME "plugins.rc"
+extern struct t_config_file *plugin_config;
+extern struct t_config_option *plugin_options;
-struct t_plugin_option
-{
- char *name; /* option name in config file */
- char *value; /* value of option */
- struct t_plugin_option *prev_option; /* link to previous option */
- struct t_plugin_option *next_option; /* link to next option */
-};
-
-extern struct t_plugin_option *plugin_options;
-
-extern struct t_plugin_option *plugin_config_search_internal (char *);
-extern struct t_plugin_option *plugin_config_search (char *, char *);
+extern struct t_config_option *plugin_config_search_internal (char *);
+extern struct t_config_option *plugin_config_search (char *, char *);
extern int plugin_config_set_internal (char *, char *);
extern int plugin_config_set (char *, char *, char *);
-extern int plugin_config_read_option (struct t_config_option *, char *, char *);
extern int plugin_config_read ();
-extern int plugin_config_write_options (FILE *, char *,
- struct t_config_option *);
extern int plugin_config_write ();
#endif /* plugin-config.h */
diff --git a/src/plugins/plugin-list.c b/src/plugins/plugin-list.c
index 8a1c56809..da3de5dac 100644
--- a/src/plugins/plugin-list.c
+++ b/src/plugins/plugin-list.c
@@ -588,7 +588,7 @@ plugin_list_print_log ()
switch (ptr_var->type)
{
case PLUGIN_LIST_VAR_INTEGER:
- log_printf (" value (int). . . . . : %d\n", *((int *)ptr_var->value));
+ log_printf (" value (integer). . . : %d\n", *((int *)ptr_var->value));
break;
case PLUGIN_LIST_VAR_STRING:
log_printf (" value (string) . . . : '%s'\n", (char *)ptr_var->value);
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index 7734d8503..54eea1191 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -338,12 +338,14 @@ plugin_auto_load_file (struct t_weechat_plugin *plugin, char *filename)
/* make C compiler happy */
(void) plugin;
- if (cfg_plugins_extension && cfg_plugins_extension[0])
+ if (CONFIG_STRING(config_plugins_extension)
+ && CONFIG_STRING(config_plugins_extension)[0])
{
- pos = strstr (filename, cfg_plugins_extension);
+ pos = strstr (filename, CONFIG_STRING(config_plugins_extension));
if (pos)
{
- if (string_strcasecmp (pos, cfg_plugins_extension) == 0)
+ if (string_strcasecmp (pos,
+ CONFIG_STRING(config_plugins_extension)) == 0)
plugin_load (filename);
}
}
@@ -362,22 +364,26 @@ plugin_auto_load ()
char *ptr_home, *dir_name, *plugins_path, *plugins_path2;
char *list_plugins, *pos, *pos2;
- if (cfg_plugins_autoload && cfg_plugins_autoload[0])
+ if (CONFIG_STRING(config_plugins_autoload)
+ && CONFIG_STRING(config_plugins_autoload)[0])
{
- if (string_strcasecmp (cfg_plugins_autoload, "*") == 0)
+ if (string_strcasecmp (CONFIG_STRING(config_plugins_autoload),
+ "*") == 0)
{
/* auto-load plugins in WeeChat home dir */
- if (cfg_plugins_path && cfg_plugins_path[0])
+ if (CONFIG_STRING(config_plugins_path)
+ && CONFIG_STRING(config_plugins_path)[0])
{
ptr_home = getenv ("HOME");
- plugins_path = string_replace (cfg_plugins_path, "~", ptr_home);
+ plugins_path = string_replace (CONFIG_STRING(config_plugins_path),
+ "~", ptr_home);
plugins_path2 = string_replace ((plugins_path) ?
- plugins_path : cfg_plugins_path,
+ plugins_path : CONFIG_STRING(config_plugins_path),
"%h", weechat_home);
plugin_exec_on_files (NULL,
(plugins_path2) ?
plugins_path2 : ((plugins_path) ?
- plugins_path : cfg_plugins_path),
+ plugins_path : CONFIG_STRING(config_plugins_path)),
&plugin_auto_load_file);
if (plugins_path)
free (plugins_path);
@@ -397,7 +403,7 @@ plugin_auto_load ()
}
else
{
- list_plugins = strdup (cfg_plugins_autoload);
+ list_plugins = strdup (CONFIG_STRING(config_plugins_autoload));
if (list_plugins)
{
pos = list_plugins;