From 0931308c2334c9208d8682cc452cd05a33d53ae8 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sun, 29 Jul 2012 08:54:52 +0200 Subject: scripts: fix function unhook_all, fix deletion of configuration files when script is unloaded (bug #36977) --- ChangeLog | 6 +- src/plugins/scripts/guile/weechat-guile-api.c | 2 +- src/plugins/scripts/lua/weechat-lua-api.c | 2 +- src/plugins/scripts/perl/weechat-perl-api.c | 2 +- src/plugins/scripts/python/weechat-python-api.c | 2 +- src/plugins/scripts/ruby/weechat-ruby-api.c | 2 +- src/plugins/scripts/script-api.c | 1030 ++++++++--------------- src/plugins/scripts/script-api.h | 3 +- src/plugins/scripts/script-callback.c | 51 +- src/plugins/scripts/script-callback.h | 11 +- src/plugins/scripts/script.c | 167 ++-- src/plugins/scripts/script.h | 3 +- src/plugins/scripts/tcl/weechat-tcl-api.c | 2 +- 13 files changed, 468 insertions(+), 815 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2708827db..616cc487a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,7 @@ WeeChat ChangeLog ================= Sébastien Helleu -v0.3.9-dev, 2012-07-27 +v0.3.9-dev, 2012-07-29 Version 0.3.9 (under dev!) @@ -62,6 +62,10 @@ Version 0.3.9 (under dev!) * relay: fix freeze when writing on relay socket (use non-blocking sockets in relay for irc and weechat protocols) (bug #36655) * ruby: detect ruby version 1.9.3 in cmake and autotools +* scripts: fix deletion of configuration files when script is unloaded + (bug #36977) +* scripts: fix function unhook_all: delete only callbacks of hooks and add + missing call to unhook * scripts: ignore call to "register" (with a warning) if script is already registered diff --git a/src/plugins/scripts/guile/weechat-guile-api.c b/src/plugins/scripts/guile/weechat-guile-api.c index 789d5d89f..f2523ea4e 100644 --- a/src/plugins/scripts/guile/weechat-guile-api.c +++ b/src/plugins/scripts/guile/weechat-guile-api.c @@ -3365,7 +3365,7 @@ weechat_guile_api_unhook_all () { API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (guile_current_script); + script_api_unhook_all (weechat_guile_plugin, guile_current_script); API_RETURN_OK; } diff --git a/src/plugins/scripts/lua/weechat-lua-api.c b/src/plugins/scripts/lua/weechat-lua-api.c index 11df83064..036de514c 100644 --- a/src/plugins/scripts/lua/weechat-lua-api.c +++ b/src/plugins/scripts/lua/weechat-lua-api.c @@ -3666,7 +3666,7 @@ weechat_lua_api_unhook_all (lua_State *L) { API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (lua_current_script); + script_api_unhook_all (weechat_lua_plugin, lua_current_script); API_RETURN_OK; } diff --git a/src/plugins/scripts/perl/weechat-perl-api.c b/src/plugins/scripts/perl/weechat-perl-api.c index 8a51ecbcc..9434e3410 100644 --- a/src/plugins/scripts/perl/weechat-perl-api.c +++ b/src/plugins/scripts/perl/weechat-perl-api.c @@ -3480,7 +3480,7 @@ XS (XS_weechat_api_unhook_all) API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (perl_current_script); + script_api_unhook_all (weechat_perl_plugin, perl_current_script); API_RETURN_OK; } diff --git a/src/plugins/scripts/python/weechat-python-api.c b/src/plugins/scripts/python/weechat-python-api.c index 15584d518..0dac4b641 100644 --- a/src/plugins/scripts/python/weechat-python-api.c +++ b/src/plugins/scripts/python/weechat-python-api.c @@ -3626,7 +3626,7 @@ weechat_python_api_unhook_all (PyObject *self, PyObject *args) API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (python_current_script); + script_api_unhook_all (weechat_python_plugin, python_current_script); API_RETURN_OK; } diff --git a/src/plugins/scripts/ruby/weechat-ruby-api.c b/src/plugins/scripts/ruby/weechat-ruby-api.c index 6d7f2eff0..6c5aadaa6 100644 --- a/src/plugins/scripts/ruby/weechat-ruby-api.c +++ b/src/plugins/scripts/ruby/weechat-ruby-api.c @@ -4106,7 +4106,7 @@ weechat_ruby_api_unhook_all (VALUE class) { API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (ruby_current_script); + script_api_unhook_all (weechat_ruby_plugin, ruby_current_script); API_RETURN_OK; } diff --git a/src/plugins/scripts/script-api.c b/src/plugins/scripts/script-api.c index a54940960..4431dd086 100644 --- a/src/plugins/scripts/script-api.c +++ b/src/plugins/scripts/script-api.c @@ -60,33 +60,19 @@ script_api_config_new (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_config_file *new_config_file; - if (function && function[0]) - { - new_script_callback = script_callback_alloc (); - if (!new_script_callback) - return NULL; - - new_config_file = weechat_config_new (name, callback_reload, - new_script_callback); - if (!new_config_file) - { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; - } - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->config_file = new_config_file; + script_cb = script_callback_add (script, function, data); + if (!script_cb) + return NULL; - script_callback_add (script, new_script_callback); - } + new_config_file = weechat_config_new (name, callback_reload, + (function && function[0]) ? script_cb : NULL); + if (new_config_file) + script_cb->config_file = new_config_file; else - { - new_config_file = weechat_config_new (name, NULL, NULL); - } + script_callback_remove (script, script_cb); return new_config_file; } @@ -134,208 +120,66 @@ script_api_config_new_section (struct t_weechat_plugin *weechat_plugin, const char *function_delete_option, const char *data_delete_option) { - struct t_script_callback *new_script_callback1, *new_script_callback2; - struct t_script_callback *new_script_callback3, *new_script_callback4; - struct t_script_callback *new_script_callback5; + struct t_script_callback *script_cb_read, *script_cb_write; + struct t_script_callback *script_cb_write_default, *script_cb_create_option; + struct t_script_callback *script_cb_delete_option; struct t_config_section *new_section; - void *callback1, *callback2, *callback3, *callback4, *callback5; - - new_script_callback1 = NULL; - new_script_callback2 = NULL; - new_script_callback3 = NULL; - new_script_callback4 = NULL; - new_script_callback5 = NULL; - callback1 = NULL; - callback2 = NULL; - callback3 = NULL; - callback4 = NULL; - callback5 = NULL; - - if (function_read && function_read[0]) - { - new_script_callback1 = script_callback_alloc (); - if (!new_script_callback1) - return NULL; - callback1 = callback_read; - } - - if (function_write && function_write[0]) - { - new_script_callback2 = script_callback_alloc (); - if (!new_script_callback2) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - return NULL; - } - callback2 = callback_write; - } - - if (function_write_default && function_write_default[0]) - { - new_script_callback3 = script_callback_alloc (); - if (!new_script_callback3) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - return NULL; - } - callback3 = callback_write_default; - } - - if (function_create_option && function_create_option[0]) - { - new_script_callback4 = script_callback_alloc (); - if (!new_script_callback4) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - if (new_script_callback3) - { - script_callback_free_data (new_script_callback3); - free (new_script_callback3); - } - return NULL; - } - callback4 = callback_create_option; - } - if (function_delete_option && function_delete_option[0]) + script_cb_read = script_callback_add (script, function_read, data_read); + script_cb_write = script_callback_add (script, function_write, data_write); + script_cb_write_default = script_callback_add (script, function_write_default, data_write_default); + script_cb_create_option = script_callback_add (script, function_create_option, data_create_option); + script_cb_delete_option = script_callback_add (script, function_delete_option, data_delete_option); + if (!script_cb_read || !script_cb_write || !script_cb_write_default + || !script_cb_create_option || !script_cb_delete_option) { - new_script_callback5 = script_callback_alloc (); - if (!new_script_callback5) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - if (new_script_callback3) - { - script_callback_free_data (new_script_callback3); - free (new_script_callback3); - } - if (new_script_callback4) - { - script_callback_free_data (new_script_callback4); - free (new_script_callback4); - } - return NULL; - } - callback5 = callback_delete_option; + if (script_cb_read) + script_callback_remove (script, script_cb_read); + if (script_cb_write) + script_callback_remove (script, script_cb_write); + if (script_cb_write_default) + script_callback_remove (script, script_cb_write_default); + if (script_cb_create_option) + script_callback_remove (script, script_cb_create_option); + if (script_cb_delete_option) + script_callback_remove (script, script_cb_delete_option); + return NULL; } new_section = weechat_config_new_section (config_file, name, user_can_add_options, user_can_delete_options, - callback1, - new_script_callback1, - callback2, - new_script_callback2, - callback3, - new_script_callback3, - callback4, - new_script_callback4, - callback5, - new_script_callback5); - if (!new_section) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - if (new_script_callback3) - { - script_callback_free_data (new_script_callback3); - free (new_script_callback3); - } - if (new_script_callback4) - { - script_callback_free_data (new_script_callback4); - free (new_script_callback4); - } - if (new_script_callback5) - { - script_callback_free_data (new_script_callback5); - free (new_script_callback5); - } - return NULL; - } - - if (new_script_callback1) + (function_read && function_read[0]) ? callback_read : NULL, + (function_read && function_read[0]) ? script_cb_read : NULL, + (function_write && function_write[0]) ? callback_write : NULL, + (function_write && function_write[0]) ? script_cb_write : NULL, + (function_write_default && function_write_default[0]) ? callback_write_default : NULL, + (function_write_default && function_write_default[0]) ? script_cb_write_default : NULL, + (function_create_option && function_create_option[0]) ? callback_create_option : NULL, + (function_create_option && function_create_option[0]) ? script_cb_create_option : NULL, + (function_delete_option && function_delete_option[0]) ? callback_delete_option : NULL, + (function_delete_option && function_delete_option[0]) ? script_cb_delete_option : NULL); + if (new_section) { - script_callback_init (new_script_callback1, script, - function_read, data_read); - new_script_callback1->config_file = config_file; - new_script_callback1->config_section = new_section; - script_callback_add (script, new_script_callback1); + script_cb_read->config_file = config_file; + script_cb_read->config_section = new_section; + script_cb_write->config_file = config_file; + script_cb_write->config_section = new_section; + script_cb_write_default->config_file = config_file; + script_cb_write_default->config_section = new_section; + script_cb_create_option->config_file = config_file; + script_cb_create_option->config_section = new_section; + script_cb_delete_option->config_file = config_file; + script_cb_delete_option->config_section = new_section; } - - if (new_script_callback2) - { - script_callback_init (new_script_callback2, script, - function_write, data_write); - new_script_callback2->config_file = config_file; - new_script_callback2->config_section = new_section; - script_callback_add (script, new_script_callback2); - } - - if (new_script_callback3) - { - script_callback_init (new_script_callback3, script, - function_write_default, data_write_default); - new_script_callback3->config_file = config_file; - new_script_callback3->config_section = new_section; - script_callback_add (script, new_script_callback3); - } - - if (new_script_callback4) - { - script_callback_init (new_script_callback4, script, - function_create_option, data_create_option); - new_script_callback4->config_file = config_file; - new_script_callback4->config_section = new_section; - script_callback_add (script, new_script_callback4); - } - - if (new_script_callback5) + else { - script_callback_init (new_script_callback5, script, - function_delete_option, data_delete_option); - new_script_callback5->config_file = config_file; - new_script_callback5->config_section = new_section; - script_callback_add (script, new_script_callback5); + script_callback_remove (script, script_cb_read); + script_callback_remove (script, script_cb_write); + script_callback_remove (script, script_cb_write_default); + script_callback_remove (script, script_cb_create_option); + script_callback_remove (script, script_cb_delete_option); } return new_section; @@ -371,116 +215,51 @@ script_api_config_new_option (struct t_weechat_plugin *weechat_plugin, const char *function_delete, const char *data_delete) { - struct t_script_callback *new_script_callback1, *new_script_callback2; - struct t_script_callback *new_script_callback3; - void *callback1, *callback2, *callback3; + struct t_script_callback *script_cb_check_value, *script_cb_change; + struct t_script_callback *script_cb_delete; struct t_config_option *new_option; - new_script_callback1 = NULL; - new_script_callback2 = NULL; - new_script_callback3 = NULL; - callback1 = NULL; - callback2 = NULL; - callback3 = NULL; - - if (function_check_value && function_check_value[0]) + script_cb_check_value = script_callback_add (script, function_check_value, data_check_value); + script_cb_change = script_callback_add (script, function_change, data_change); + script_cb_delete = script_callback_add (script, function_delete, data_delete); + if (!script_cb_check_value || !script_cb_change || !script_cb_delete) { - new_script_callback1 = script_callback_alloc (); - if (!new_script_callback1) - return NULL; - callback1 = callback_check_value; - } - - if (function_change && function_change[0]) - { - new_script_callback2 = script_callback_alloc (); - if (!new_script_callback2) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - return NULL; - } - callback2 = callback_change; - } - - if (function_delete && function_delete[0]) - { - new_script_callback3 = script_callback_alloc (); - if (!new_script_callback3) - { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - return NULL; - } - callback3 = callback_delete; + if (script_cb_check_value) + script_callback_remove (script, script_cb_check_value); + if (script_cb_change) + script_callback_remove (script, script_cb_change); + if (script_cb_delete) + script_callback_remove (script, script_cb_delete); + return NULL; } new_option = weechat_config_new_option (config_file, section, name, type, description, string_values, min, max, default_value, value, null_value_allowed, - callback1, new_script_callback1, - callback2, new_script_callback2, - callback3, new_script_callback3); - if (!new_option) + (function_check_value && function_check_value[0]) ? callback_check_value : NULL, + (function_check_value && function_check_value[0]) ? script_cb_check_value : NULL, + (function_change && function_change[0]) ? callback_change : NULL, + (function_change && function_change[0]) ? script_cb_change : NULL, + (function_delete && function_delete[0]) ? callback_delete : NULL, + (function_delete && function_delete[0]) ? script_cb_delete : NULL); + if (new_option) { - if (new_script_callback1) - { - script_callback_free_data (new_script_callback1); - free (new_script_callback1); - } - if (new_script_callback2) - { - script_callback_free_data (new_script_callback2); - free (new_script_callback2); - } - if (new_script_callback3) - { - script_callback_free_data (new_script_callback3); - free (new_script_callback3); - } - return NULL; + script_cb_check_value->config_file = config_file; + script_cb_check_value->config_section = section; + script_cb_check_value->config_option = new_option; + script_cb_change->config_file = config_file; + script_cb_change->config_section = section; + script_cb_change->config_option = new_option; + script_cb_delete->config_file = config_file; + script_cb_delete->config_section = section; + script_cb_delete->config_option = new_option; } - - if (new_script_callback1) - { - script_callback_init (new_script_callback1, script, - function_check_value, data_check_value); - new_script_callback1->config_file = config_file; - new_script_callback1->config_section = section; - new_script_callback1->config_option = new_option; - script_callback_add (script, new_script_callback1); - } - - if (new_script_callback2) - { - script_callback_init (new_script_callback2, script, - function_change, data_change); - new_script_callback2->config_file = config_file; - new_script_callback2->config_section = section; - new_script_callback2->config_option = new_option; - script_callback_add (script, new_script_callback2); - } - - if (new_script_callback3) + else { - script_callback_init (new_script_callback3, script, - function_delete, data_delete); - new_script_callback3->config_file = config_file; - new_script_callback3->config_section = section; - new_script_callback3->config_option = new_option; - script_callback_add (script, new_script_callback3); + script_callback_remove (script, script_cb_check_value); + script_callback_remove (script, script_cb_change); + script_callback_remove (script, script_cb_delete); } return new_option; @@ -495,22 +274,22 @@ script_api_config_option_free (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_config_option *option) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !option) return; weechat_config_option_free (option); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->config_option == option) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->config_option == option) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -524,23 +303,23 @@ script_api_config_section_free_options (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_config_section *section) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !section) return; weechat_config_section_free_options (section); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if ((ptr_script_callback->config_section == section) - && ptr_script_callback->config_option) - script_callback_remove (script, ptr_script_callback); + if ((ptr_script_cb->config_section == section) + && ptr_script_cb->config_option) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -553,22 +332,22 @@ script_api_config_section_free (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_config_section *section) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !section) return; weechat_config_section_free (section); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->config_section == section) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->config_section == section) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -581,22 +360,22 @@ script_api_config_free (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_config_file *config_file) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !config_file) return; weechat_config_free (config_file); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->config_file == config_file) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->config_file == config_file) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -718,28 +497,23 @@ script_api_hook_command (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_command (command, description, args, args_description, completion, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -759,27 +533,22 @@ script_api_hook_command_run (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_command_run (command, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -798,27 +567,22 @@ script_api_hook_timer (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_timer (interval, align_second, max_calls, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -837,27 +601,22 @@ script_api_hook_fd (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_fd (fd, flag_read, flag_write, flag_exception, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -881,27 +640,22 @@ script_api_hook_process_hashtable (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - script_callback_init (new_script_callback, script, function, data); - script_callback_add (script, new_script_callback); - new_hook = weechat_hook_process_hashtable (command, options, timeout, - callback, new_script_callback); - - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_remove (script, new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - new_script_callback->hook = new_hook; + else + script_callback_remove (script, script_cb); return new_hook; } @@ -949,29 +703,24 @@ script_api_hook_connect (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_connect (proxy, address, port, sock, ipv6, gnutls_sess, gnutls_cb, gnutls_dhkey_size, gnutls_priorities, local_hostname, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -996,27 +745,22 @@ script_api_hook_print (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_print (buffer, tags, message, strip_colors, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1036,26 +780,21 @@ script_api_hook_signal (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - new_hook = weechat_hook_signal (signal, callback, new_script_callback); - if (!new_hook) + new_hook = weechat_hook_signal (signal, callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1074,26 +813,21 @@ script_api_hook_hsignal (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - new_hook = weechat_hook_hsignal (signal, callback, new_script_callback); - if (!new_hook) + new_hook = weechat_hook_hsignal (signal, callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1112,26 +846,21 @@ script_api_hook_config (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - new_hook = weechat_hook_config (option, callback, new_script_callback); - if (!new_hook) + new_hook = weechat_hook_config (option, callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1153,27 +882,22 @@ script_api_hook_completion (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_completion (completion, description, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1193,26 +917,21 @@ script_api_hook_modifier (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - new_hook = weechat_hook_modifier (modifier, callback, new_script_callback); - if (!new_hook) + new_hook = weechat_hook_modifier (modifier, callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1234,27 +953,22 @@ script_api_hook_info (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_info (info_name, description, args_description, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1277,29 +991,24 @@ script_api_hook_info_hashtable (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_info_hashtable (info_name, description, args_description, output_description, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1323,28 +1032,23 @@ script_api_hook_infolist (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; new_hook = weechat_hook_infolist (infolist_name, description, pointer_description, args_description, - callback, new_script_callback); - if (!new_hook) + callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1363,26 +1067,21 @@ script_api_hook_focus (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_hook *new_hook; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - new_hook = weechat_hook_focus (area, callback, new_script_callback); - if (!new_hook) + new_hook = weechat_hook_focus (area, callback, script_cb); + if (new_hook) { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; + weechat_hook_set (new_hook, "subplugin", script->name); + script_cb->hook = new_hook; } - weechat_hook_set (new_hook, "subplugin", script->name); - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->hook = new_hook; - - script_callback_add (script, new_script_callback); + else + script_callback_remove (script, script_cb); return new_hook; } @@ -1396,22 +1095,22 @@ script_api_unhook (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_hook *hook) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !hook) return; weechat_unhook (hook); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->hook == hook) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->hook == hook) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -1420,18 +1119,23 @@ script_api_unhook (struct t_weechat_plugin *weechat_plugin, */ void -script_api_unhook_all (struct t_plugin_script *script) +script_api_unhook_all (struct t_weechat_plugin *weechat_plugin, + struct t_plugin_script *script) { - struct t_script_callback *ptr_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; - ptr_callback = script->callbacks; - while (ptr_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - script_callback_remove (script, ptr_callback); + if (ptr_script_cb->hook) + { + weechat_unhook (ptr_script_cb->hook); + script_callback_remove (script, ptr_script_cb); + } - ptr_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -1453,90 +1157,44 @@ script_api_buffer_new (struct t_weechat_plugin *weechat_plugin, const char *function_close, const char *data_close) { - struct t_script_callback *new_script_callback_input; - struct t_script_callback *new_script_callback_close; + struct t_script_callback *script_cb_input; + struct t_script_callback *script_cb_close; struct t_gui_buffer *new_buffer; - if ((!function_input || !function_input[0]) - && (!function_close || !function_close[0])) - return weechat_buffer_new (name, NULL, NULL, NULL, NULL); - - new_script_callback_input = NULL; - new_script_callback_close = NULL; - - if (function_input && function_input[0]) - { - new_script_callback_input = script_callback_alloc (); - if (!new_script_callback_input) - return NULL; - } - - if (function_close && function_close[0]) + script_cb_input = script_callback_add (script, function_input, data_input); + script_cb_close = script_callback_add (script, function_close, data_close); + if (!script_cb_input || !script_cb_close) { - new_script_callback_close = script_callback_alloc (); - if (!new_script_callback_close) - { - if (new_script_callback_input) - { - script_callback_free_data (new_script_callback_input); - free (new_script_callback_input); - } - return NULL; - } - } - - new_buffer = weechat_buffer_new (name, - (new_script_callback_input) ? - input_callback : NULL, - (new_script_callback_input) ? - new_script_callback_input : NULL, - (new_script_callback_close) ? - close_callback : NULL, - (new_script_callback_close) ? - new_script_callback_close : NULL); - if (!new_buffer) - { - if (new_script_callback_input) - { - script_callback_free_data (new_script_callback_input); - free (new_script_callback_input); - } - if (new_script_callback_close) - { - script_callback_free_data (new_script_callback_close); - free (new_script_callback_close); - } + if (script_cb_input) + script_callback_remove (script, script_cb_input); + if (script_cb_close) + script_callback_remove (script, script_cb_close); return NULL; } - if (new_script_callback_input) + new_buffer = weechat_buffer_new (name, + (function_input && function_input[0]) ? input_callback : NULL, + (function_input && function_input[0]) ? script_cb_input : NULL, + (function_close && function_close[0]) ? close_callback : NULL, + (function_close && function_close[0]) ? script_cb_close : NULL); + if (new_buffer) { - script_callback_init (new_script_callback_input, - script, function_input, data_input); - new_script_callback_input->buffer = new_buffer; - script_callback_add (script, new_script_callback_input); + script_cb_input->buffer = new_buffer; + script_cb_close->buffer = new_buffer; + + /* used when upgrading weechat, to set callbacks */ + weechat_buffer_set (new_buffer, "localvar_set_script_name", script->name); + weechat_buffer_set (new_buffer, "localvar_set_script_input_cb", function_input); + weechat_buffer_set (new_buffer, "localvar_set_script_input_cb_data", data_input); + weechat_buffer_set (new_buffer, "localvar_set_script_close_cb", function_close); + weechat_buffer_set (new_buffer, "localvar_set_script_close_cb_data", data_close); } - - if (new_script_callback_close) + else { - script_callback_init (new_script_callback_close, - script, function_close, data_close); - new_script_callback_close->buffer = new_buffer; - script_callback_add (script, new_script_callback_close); + script_callback_remove (script, script_cb_input); + script_callback_remove (script, script_cb_close); } - /* used when upgrading weechat, to set callbacks */ - weechat_buffer_set (new_buffer, "localvar_set_script_name", - script->name); - weechat_buffer_set (new_buffer, "localvar_set_script_input_cb", - function_input); - weechat_buffer_set (new_buffer, "localvar_set_script_input_cb_data", - data_input); - weechat_buffer_set (new_buffer, "localvar_set_script_close_cb", - function_close); - weechat_buffer_set (new_buffer, "localvar_set_script_close_cb_data", - data_close); - return new_buffer; } @@ -1549,22 +1207,22 @@ script_api_buffer_close (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_gui_buffer *buffer) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !buffer) return; weechat_buffer_close (buffer); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->buffer == buffer) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->buffer == buffer) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -1582,29 +1240,20 @@ script_api_bar_item_new (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; struct t_gui_bar_item *new_item; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return NULL; - script_callback_init (new_script_callback, script, function, data); - new_item = weechat_bar_item_new (name, - (function && function[0]) ? - build_callback : NULL, - (function && function[0]) ? - new_script_callback : NULL); - if (!new_item) - { - script_callback_free_data (new_script_callback); - free (new_script_callback); - return NULL; - } - - new_script_callback->bar_item = new_item; - script_callback_add (script, new_script_callback); + (function && function[0]) ? build_callback : NULL, + (function && function[0]) ? script_cb : NULL); + if (new_item) + script_cb->bar_item = new_item; + else + script_callback_remove (script, script_cb); return new_item; } @@ -1618,22 +1267,22 @@ script_api_bar_item_remove (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_gui_bar_item *item) { - struct t_script_callback *ptr_script_callback, *next_callback; + struct t_script_callback *ptr_script_cb, *next_callback; if (!weechat_plugin || !script || !item) return; weechat_bar_item_remove (item); - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + ptr_script_cb = script->callbacks; + while (ptr_script_cb) { - next_callback = ptr_script_callback->next_callback; + next_callback = ptr_script_cb->next_callback; - if (ptr_script_callback->bar_item == item) - script_callback_remove (script, ptr_script_callback); + if (ptr_script_cb->bar_item == item) + script_callback_remove (script, ptr_script_cb); - ptr_script_callback = next_callback; + ptr_script_cb = next_callback; } } @@ -1809,25 +1458,22 @@ script_api_upgrade_read (struct t_weechat_plugin *weechat_plugin, const char *function, const char *data) { - struct t_script_callback *new_script_callback; + struct t_script_callback *script_cb; int rc; if (!function || !function[0]) return 0; - new_script_callback = script_callback_alloc (); - if (!new_script_callback) + script_cb = script_callback_add (script, function, data); + if (!script_cb) return 0; - - script_callback_init (new_script_callback, script, function, data); - new_script_callback->upgrade_file = upgrade_file; - script_callback_add (script, new_script_callback); + script_cb->upgrade_file = upgrade_file; rc = weechat_upgrade_read (upgrade_file, callback_read, - new_script_callback); + script_cb); - script_callback_remove (script, new_script_callback); + script_callback_remove (script, script_cb); return rc; } diff --git a/src/plugins/scripts/script-api.h b/src/plugins/scripts/script-api.h index b2178af55..c60f10a70 100644 --- a/src/plugins/scripts/script-api.h +++ b/src/plugins/scripts/script-api.h @@ -298,7 +298,8 @@ extern struct t_hook *script_api_hook_focus (struct t_weechat_plugin *weechat_pl extern void script_api_unhook (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_hook *hook); -extern void script_api_unhook_all (struct t_plugin_script *script); +extern void script_api_unhook_all (struct t_weechat_plugin *weechat_plugin, + struct t_plugin_script *script); extern struct t_gui_buffer *script_api_buffer_new (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, const char *name, diff --git a/src/plugins/scripts/script-callback.c b/src/plugins/scripts/script-callback.c index 7a1f0dee0..4c1fa7414 100644 --- a/src/plugins/scripts/script-callback.c +++ b/src/plugins/scripts/script-callback.c @@ -59,36 +59,37 @@ script_callback_alloc () } /* - * script_callback_init: initialize callback with script, function and data + * script_callback_add: allocate a new callback, initialize it + * (script/function/data) and add it to list + * return pointer to new callback or NULL if error */ -void -script_callback_init (struct t_script_callback *script_callback, - struct t_plugin_script *script, - const char *function, - const char *data) +struct t_script_callback * +script_callback_add (struct t_plugin_script *script, + const char *function, + const char *data) { - if (script_callback) - { - script_callback->script = script; - script_callback->function = (function) ? strdup (function) : NULL; - script_callback->data = (data) ? strdup (data) : NULL; - } -} + struct t_script_callback *script_cb; + if (!script) + return NULL; -/* - * script_callback_add: add a callback to list - */ + script_cb = script_callback_alloc (); + if (!script_cb) + return NULL; -void -script_callback_add (struct t_plugin_script *script, - struct t_script_callback *callback) -{ + /* initialize callback */ + script_cb->script = script; + script_cb->function = (function) ? strdup (function) : NULL; + script_cb->data = (data) ? strdup (data) : NULL; + + /* add callback to list */ if (script->callbacks) - script->callbacks->prev_callback = callback; - callback->prev_callback = NULL; - callback->next_callback = script->callbacks; - script->callbacks = callback; + script->callbacks->prev_callback = script_cb; + script_cb->prev_callback = NULL; + script_cb->next_callback = script->callbacks; + script->callbacks = script_cb; + + return script_cb; } /* @@ -100,7 +101,7 @@ script_callback_free_data (struct t_script_callback *script_callback) { if (script_callback->function) free (script_callback->function); - if (script_callback->data) + if (script_callback->data) free (script_callback->data); } diff --git a/src/plugins/scripts/script-callback.h b/src/plugins/scripts/script-callback.h index 4c7826787..f48fdb13c 100644 --- a/src/plugins/scripts/script-callback.h +++ b/src/plugins/scripts/script-callback.h @@ -36,14 +36,9 @@ struct t_script_callback struct t_script_callback *next_callback; /* link to previous callback */ }; -extern struct t_script_callback *script_callback_alloc (); -extern void script_callback_init (struct t_script_callback *script_callback, - struct t_plugin_script *script, - const char *function, - const char *data); -extern void script_callback_add (struct t_plugin_script *script, - struct t_script_callback *callback); -extern void script_callback_free_data (struct t_script_callback *script_callback); +extern struct t_script_callback *script_callback_add (struct t_plugin_script *script, + const char *function, + const char *data); extern void script_callback_remove (struct t_plugin_script *script, struct t_script_callback *script_callback); extern void script_callback_remove_all (struct t_plugin_script *script); diff --git a/src/plugins/scripts/script.c b/src/plugins/scripts/script.c index 3f4268a4a..7f5ec2d2e 100644 --- a/src/plugins/scripts/script.c +++ b/src/plugins/scripts/script.c @@ -563,6 +563,7 @@ script_add (struct t_weechat_plugin *weechat_plugin, strdup (shutdown_func) : NULL; new_script->charset = (charset) ? strdup (charset) : NULL; new_script->callbacks = NULL; + new_script->unloading = 0; script_insert_sorted (weechat_plugin, scripts, last_script, new_script); @@ -594,11 +595,11 @@ script_set_buffer_callbacks (struct t_weechat_plugin *weechat_plugin, { struct t_infolist *infolist; struct t_gui_buffer *ptr_buffer; - const char *script_name, *script_input_cb, *script_input_cb_data; - const char *script_close_cb, *script_close_cb_data; + const char *script_name, *str_script_input_cb, *str_script_input_cb_data; + const char *str_script_close_cb, *str_script_close_cb_data; struct t_plugin_script *ptr_script; - struct t_script_callback *new_script_callback_input; - struct t_script_callback *new_script_callback_close; + struct t_script_callback *script_cb_input; + struct t_script_callback *script_cb_close; infolist = weechat_infolist_get ("buffer", NULL, NULL); if (infolist) @@ -615,53 +616,45 @@ script_set_buffer_callbacks (struct t_weechat_plugin *weechat_plugin, script_name); if (ptr_script && (ptr_script == script)) { - script_input_cb = weechat_buffer_get_string (ptr_buffer, - "localvar_script_input_cb"); - script_input_cb_data = weechat_buffer_get_string (ptr_buffer, - "localvar_script_input_cb_data"); - script_close_cb = weechat_buffer_get_string (ptr_buffer, - "localvar_script_close_cb"); - script_close_cb_data = weechat_buffer_get_string (ptr_buffer, - "localvar_script_close_cb_data"); - - if (script_input_cb && script_input_cb[0]) + str_script_input_cb = weechat_buffer_get_string (ptr_buffer, + "localvar_script_input_cb"); + str_script_input_cb_data = weechat_buffer_get_string (ptr_buffer, + "localvar_script_input_cb_data"); + str_script_close_cb = weechat_buffer_get_string (ptr_buffer, + "localvar_script_close_cb"); + str_script_close_cb_data = weechat_buffer_get_string (ptr_buffer, + "localvar_script_close_cb_data"); + + if (str_script_input_cb && str_script_input_cb[0]) { - new_script_callback_input = script_callback_alloc (); - if (new_script_callback_input) + script_cb_input = script_callback_add (ptr_script, + str_script_input_cb, + str_script_input_cb_data); + if (script_cb_input) { - script_callback_init (new_script_callback_input, - ptr_script, - script_input_cb, - script_input_cb_data); - new_script_callback_input->buffer = ptr_buffer; - script_callback_add (ptr_script, - new_script_callback_input); + script_cb_input->buffer = ptr_buffer; weechat_buffer_set_pointer (ptr_buffer, "input_callback", callback_buffer_input); weechat_buffer_set_pointer (ptr_buffer, "input_callback_data", - new_script_callback_input); + script_cb_input); } } - if (script_close_cb && script_close_cb[0]) + if (str_script_close_cb && str_script_close_cb[0]) { - new_script_callback_close = script_callback_alloc (); - if (new_script_callback_close) + script_cb_close = script_callback_add (ptr_script, + str_script_close_cb, + str_script_close_cb_data); + if (script_cb_close) { - script_callback_init (new_script_callback_close, - ptr_script, - script_close_cb, - script_close_cb_data); - new_script_callback_close->buffer = ptr_buffer; - script_callback_add (ptr_script, - new_script_callback_close); + script_cb_close->buffer = ptr_buffer; weechat_buffer_set_pointer (ptr_buffer, "close_callback", callback_buffer_close); weechat_buffer_set_pointer (ptr_buffer, "close_callback_data", - new_script_callback_close); + script_cb_close); } } } @@ -682,20 +675,27 @@ script_remove_buffer_callbacks (struct t_plugin_script *scripts, struct t_gui_buffer *buffer) { struct t_plugin_script *ptr_script; - struct t_script_callback *ptr_script_callback, *next_script_callback; + struct t_script_callback *ptr_script_cb, *next_script_cb; for (ptr_script = scripts; ptr_script; ptr_script = ptr_script->next_script) { - ptr_script_callback = ptr_script->callbacks; - while (ptr_script_callback) + /* + * do not remove buffer callbacks if script is being unloaded + * (because all callbacks will be removed anyway) + */ + if (!ptr_script->unloading) { - next_script_callback = ptr_script_callback->next_callback; + ptr_script_cb = ptr_script->callbacks; + while (ptr_script_cb) + { + next_script_cb = ptr_script_cb->next_callback; - if (ptr_script_callback->buffer == buffer) - script_callback_remove (ptr_script, ptr_script_callback); + if (ptr_script_cb->buffer == buffer) + script_callback_remove (ptr_script, ptr_script_cb); - ptr_script_callback = next_script_callback; + ptr_script_cb = next_script_cb; + } } } } @@ -710,51 +710,55 @@ script_remove (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script **last_script, struct t_plugin_script *script) { - struct t_script_callback *ptr_script_callback, *next_script_callback; + struct t_script_callback *ptr_script_cb, *ptr_script_cb2; - for (ptr_script_callback = script->callbacks; ptr_script_callback; - ptr_script_callback = ptr_script_callback->next_callback) - { - /* unhook */ - if (ptr_script_callback->hook) - { - weechat_unhook (ptr_script_callback->hook); - } - } + script->unloading = 1; - ptr_script_callback = script->callbacks; - while (ptr_script_callback) + for (ptr_script_cb = script->callbacks; ptr_script_cb; + ptr_script_cb = ptr_script_cb->next_callback) { - next_script_callback = ptr_script_callback->next_callback; - /* free config file */ - if (ptr_script_callback->config_file - && !ptr_script_callback->config_section - && !ptr_script_callback->config_option) + if (ptr_script_cb->config_file) { if (weechat_config_boolean (weechat_config_get ("weechat.plugin.save_config_on_unload"))) - weechat_config_write (ptr_script_callback->config_file); - weechat_config_free (ptr_script_callback->config_file); + weechat_config_write (ptr_script_cb->config_file); + weechat_config_free (ptr_script_cb->config_file); } - /* remove bar item */ - if (ptr_script_callback->bar_item) - weechat_bar_item_remove (ptr_script_callback->bar_item); + /* unhook */ + if (ptr_script_cb->hook) + weechat_unhook (ptr_script_cb->hook); - /* remove buffer */ - if (ptr_script_callback->buffer) + /* close buffer */ + if (ptr_script_cb->buffer) + weechat_buffer_close (ptr_script_cb->buffer); + + /* remove bar item */ + if (ptr_script_cb->bar_item) + weechat_bar_item_remove (ptr_script_cb->bar_item); + + /* + * remove same pointers in other callbacks + * (to not free 2 times with same pointer!) + */ + for (ptr_script_cb2 = ptr_script_cb->next_callback; ptr_script_cb2; + ptr_script_cb2 = ptr_script_cb2->next_callback) { - for (next_script_callback = ptr_script_callback->next_callback; - next_script_callback; - next_script_callback = next_script_callback->next_callback) - { - if (next_script_callback->buffer != ptr_script_callback->buffer) - break; - } - weechat_buffer_close (ptr_script_callback->buffer); + if (ptr_script_cb2->config_file == ptr_script_cb->config_file) + ptr_script_cb2->config_file = NULL; + if (ptr_script_cb2->config_section == ptr_script_cb->config_section) + ptr_script_cb2->config_section = NULL; + if (ptr_script_cb2->config_option == ptr_script_cb->config_option) + ptr_script_cb2->config_option = NULL; + if (ptr_script_cb2->hook == ptr_script_cb->hook) + ptr_script_cb2->hook = NULL; + if (ptr_script_cb2->buffer == ptr_script_cb->buffer) + ptr_script_cb2->buffer = NULL; + if (ptr_script_cb2->bar_item == ptr_script_cb->bar_item) + ptr_script_cb2->bar_item = NULL; + if (ptr_script_cb2->upgrade_file == ptr_script_cb->upgrade_file) + ptr_script_cb2->upgrade_file = NULL; } - - ptr_script_callback = next_script_callback; } /* remove all callbacks created by this script */ @@ -1175,6 +1179,8 @@ script_add_to_infolist (struct t_weechat_plugin *weechat_plugin, return 0; if (!weechat_infolist_new_var_string (ptr_item, "charset", script->charset)) return 0; + if (!weechat_infolist_new_var_integer (ptr_item, "unloading", script->unloading)) + return 0; return 1; } @@ -1264,7 +1270,7 @@ script_print_log (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *scripts) { struct t_plugin_script *ptr_script; - struct t_script_callback *ptr_script_callback; + struct t_script_callback *ptr_script_cb; weechat_log_printf (""); weechat_log_printf ("***** \"%s\" plugin dump *****", @@ -1285,13 +1291,14 @@ script_print_log (struct t_weechat_plugin *weechat_plugin, weechat_log_printf (" shutdown_func . . . : '%s'", ptr_script->shutdown_func); weechat_log_printf (" charset . . . . . . : '%s'", ptr_script->charset); weechat_log_printf (" callbacks . . . . . : 0x%lx", ptr_script->callbacks); + weechat_log_printf (" unloading . . . . . : %d", ptr_script->unloading); weechat_log_printf (" prev_script . . . . : 0x%lx", ptr_script->prev_script); weechat_log_printf (" next_script . . . . : 0x%lx", ptr_script->next_script); - for (ptr_script_callback = ptr_script->callbacks; ptr_script_callback; - ptr_script_callback = ptr_script_callback->next_callback) + for (ptr_script_cb = ptr_script->callbacks; ptr_script_cb; + ptr_script_cb = ptr_script_cb->next_callback) { - script_callback_print_log (weechat_plugin, ptr_script_callback); + script_callback_print_log (weechat_plugin, ptr_script_cb); } } diff --git a/src/plugins/scripts/script.h b/src/plugins/scripts/script.h index 55b692a1a..f8373613e 100644 --- a/src/plugins/scripts/script.h +++ b/src/plugins/scripts/script.h @@ -61,9 +61,8 @@ struct t_plugin_script char *description; /* plugin description */ char *shutdown_func; /* function when script is unloaded*/ char *charset; /* script charset */ - struct t_script_callback *callbacks; /* callbacks for script */ - + int unloading; /* script is being unloaded */ struct t_plugin_script *prev_script; /* link to previous script */ struct t_plugin_script *next_script; /* link to next script */ }; diff --git a/src/plugins/scripts/tcl/weechat-tcl-api.c b/src/plugins/scripts/tcl/weechat-tcl-api.c index 785a0261f..3af454fa6 100644 --- a/src/plugins/scripts/tcl/weechat-tcl-api.c +++ b/src/plugins/scripts/tcl/weechat-tcl-api.c @@ -3981,7 +3981,7 @@ weechat_tcl_api_unhook_all (ClientData clientData, Tcl_Interp *interp, API_FUNC(1, "unhook_all", API_RETURN_ERROR); - script_api_unhook_all (tcl_current_script); + script_api_unhook_all (weechat_tcl_plugin, tcl_current_script); API_RETURN_OK; } -- cgit v1.2.3