From b7d6be593634c51d62c8041e32d888484b580ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Wed, 25 Jul 2018 20:19:26 +0200 Subject: [PATCH] scripts: fix duplicated lines in output of script eval (python, perl, ruby, lua and guile) When there was a call to a hook callback during the eval, the output buffer was cleared too late, and displayed multiple times in the buffer. This commit clears the buffer even before we display it (this display can trigger a hook callback). --- ChangeLog.adoc | 1 + src/plugins/guile/weechat-guile.c | 32 +++++++++++++++++------------ src/plugins/lua/weechat-lua.c | 32 +++++++++++++++++------------ src/plugins/perl/weechat-perl.c | 32 +++++++++++++++++------------ src/plugins/python/weechat-python.c | 32 +++++++++++++++++------------ src/plugins/ruby/weechat-ruby.c | 32 +++++++++++++++++------------ 6 files changed, 96 insertions(+), 65 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 235cc9ca2..c04b6a78a 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -21,6 +21,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] Bug fixes:: * core: send signal "key_pressed" for mouse code only if the string is UTF-8 valid (issue #1220) + * scripts: fix duplicated lines in output of script eval (python, perl, ruby, lua and guile) [[v2.2]] == Version 2.2 (2018-07-14) diff --git a/src/plugins/guile/weechat-guile.c b/src/plugins/guile/weechat-guile.c index 52f0c8631..7f4481489 100644 --- a/src/plugins/guile/weechat-guile.c +++ b/src/plugins/guile/weechat-guile.c @@ -118,36 +118,42 @@ void weechat_guile_output_flush () { const char *ptr_command; - char *command; + char *temp_buffer, *command; int length; if (!*guile_buffer_output[0]) return; + /* if there's no buffer, we catch the output, so there's no flush */ + if (guile_eval_mode && !guile_eval_buffer) + return; + + temp_buffer = strdup (*guile_buffer_output); + if (!temp_buffer) + return; + + weechat_string_dyn_copy (guile_buffer_output, NULL); + if (guile_eval_mode) { - /* if there's no buffer, we catch the output, so there's no flush */ - if (!guile_eval_buffer) - return; - if (guile_eval_send_input) { if (guile_eval_exec_commands) - ptr_command = *guile_buffer_output; + ptr_command = temp_buffer; else - ptr_command = weechat_string_input_for_buffer (*guile_buffer_output); + ptr_command = weechat_string_input_for_buffer (temp_buffer); if (ptr_command) { - weechat_command (guile_eval_buffer, *guile_buffer_output); + weechat_command (guile_eval_buffer, temp_buffer); } else { - length = 1 + strlen (*guile_buffer_output) + 1; + length = 1 + strlen (temp_buffer) + 1; command = malloc (length); if (command) { snprintf (command, length, "%c%s", - *guile_buffer_output[0], *guile_buffer_output); + temp_buffer[0], temp_buffer); weechat_command (guile_eval_buffer, (command[0]) ? command : " "); free (command); @@ -156,7 +162,7 @@ weechat_guile_output_flush () } else { - weechat_printf (guile_eval_buffer, "%s", *guile_buffer_output); + weechat_printf (guile_eval_buffer, "%s", temp_buffer); } } else @@ -167,10 +173,10 @@ weechat_guile_output_flush () weechat_gettext ("%s: stdout/stderr (%s): %s"), GUILE_PLUGIN_NAME, (guile_current_script) ? guile_current_script->name : "?", - *guile_buffer_output); + temp_buffer); } - weechat_string_dyn_copy (guile_buffer_output, NULL); + free (temp_buffer); } /* diff --git a/src/plugins/lua/weechat-lua.c b/src/plugins/lua/weechat-lua.c index 124ccbdab..b3e5b3c28 100644 --- a/src/plugins/lua/weechat-lua.c +++ b/src/plugins/lua/weechat-lua.c @@ -190,36 +190,42 @@ void weechat_lua_output_flush () { const char *ptr_command; - char *command; + char *temp_buffer, *command; int length; if (!*lua_buffer_output[0]) return; + /* if there's no buffer, we catch the output, so there's no flush */ + if (lua_eval_mode && !lua_eval_buffer) + return; + + temp_buffer = strdup (*lua_buffer_output); + if (!temp_buffer) + return; + + weechat_string_dyn_copy (lua_buffer_output, NULL); + if (lua_eval_mode) { - /* if there's no buffer, we catch the output, so there's no flush */ - if (!lua_eval_buffer) - return; - if (lua_eval_send_input) { if (lua_eval_exec_commands) - ptr_command = *lua_buffer_output; + ptr_command = temp_buffer; else - ptr_command = weechat_string_input_for_buffer (*lua_buffer_output); + ptr_command = weechat_string_input_for_buffer (temp_buffer); if (ptr_command) { - weechat_command (lua_eval_buffer, *lua_buffer_output); + weechat_command (lua_eval_buffer, temp_buffer); } else { - length = 1 + strlen (*lua_buffer_output) + 1; + length = 1 + strlen (temp_buffer) + 1; command = malloc (length); if (command) { snprintf (command, length, "%c%s", - *lua_buffer_output[0], *lua_buffer_output); + temp_buffer[0], temp_buffer); weechat_command (lua_eval_buffer, (command[0]) ? command : " "); free (command); @@ -228,7 +234,7 @@ weechat_lua_output_flush () } else { - weechat_printf (lua_eval_buffer, "%s", *lua_buffer_output); + weechat_printf (lua_eval_buffer, "%s", temp_buffer); } } else @@ -239,10 +245,10 @@ weechat_lua_output_flush () weechat_gettext ("%s: stdout/stderr (%s): %s"), LUA_PLUGIN_NAME, (lua_current_script) ? lua_current_script->name : "?", - *lua_buffer_output); + temp_buffer); } - weechat_string_dyn_copy (lua_buffer_output, NULL); + free (temp_buffer); } /* diff --git a/src/plugins/perl/weechat-perl.c b/src/plugins/perl/weechat-perl.c index 846a0ae6f..e06603165 100644 --- a/src/plugins/perl/weechat-perl.c +++ b/src/plugins/perl/weechat-perl.c @@ -226,36 +226,42 @@ void weechat_perl_output_flush () { const char *ptr_command; - char *command; + char *temp_buffer, *command; int length; if (!*perl_buffer_output[0]) return; + /* if there's no buffer, we catch the output, so there's no flush */ + if (perl_eval_mode && !perl_eval_buffer) + return; + + temp_buffer = strdup (*perl_buffer_output); + if (!temp_buffer) + return; + + weechat_string_dyn_copy (perl_buffer_output, NULL); + if (perl_eval_mode) { - /* if there's no buffer, we catch the output, so there's no flush */ - if (!perl_eval_buffer) - return; - if (perl_eval_send_input) { if (perl_eval_exec_commands) - ptr_command = *perl_buffer_output; + ptr_command = temp_buffer; else - ptr_command = weechat_string_input_for_buffer (*perl_buffer_output); + ptr_command = weechat_string_input_for_buffer (temp_buffer); if (ptr_command) { - weechat_command (perl_eval_buffer, *perl_buffer_output); + weechat_command (perl_eval_buffer, temp_buffer); } else { - length = 1 + strlen (*perl_buffer_output) + 1; + length = 1 + strlen (temp_buffer) + 1; command = malloc (length); if (command) { snprintf (command, length, "%c%s", - *perl_buffer_output[0], *perl_buffer_output); + temp_buffer[0], temp_buffer); weechat_command (perl_eval_buffer, (command[0]) ? command : " "); free (command); @@ -264,7 +270,7 @@ weechat_perl_output_flush () } else { - weechat_printf (perl_eval_buffer, "%s", *perl_buffer_output); + weechat_printf (perl_eval_buffer, "%s", temp_buffer); } } else @@ -275,10 +281,10 @@ weechat_perl_output_flush () weechat_gettext ("%s: stdout/stderr (%s): %s"), PERL_PLUGIN_NAME, (perl_current_script) ? perl_current_script->name : "?", - *perl_buffer_output); + temp_buffer); } - weechat_string_dyn_copy (perl_buffer_output, NULL); + free (temp_buffer); } /* diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c index 8ae89da93..7469ffb77 100644 --- a/src/plugins/python/weechat-python.c +++ b/src/plugins/python/weechat-python.c @@ -337,36 +337,42 @@ void weechat_python_output_flush () { const char *ptr_command; - char *command; + char *temp_buffer, *command; int length; if (!*python_buffer_output[0]) return; + /* if there's no buffer, we catch the output, so there's no flush */ + if (python_eval_mode && !python_eval_buffer) + return; + + temp_buffer = strdup (*python_buffer_output); + if (!temp_buffer) + return; + + weechat_string_dyn_copy (python_buffer_output, NULL); + if (python_eval_mode) { - /* if there's no buffer, we catch the output, so there's no flush */ - if (!python_eval_buffer) - return; - if (python_eval_send_input) { if (python_eval_exec_commands) - ptr_command = *python_buffer_output; + ptr_command = temp_buffer; else - ptr_command = weechat_string_input_for_buffer (*python_buffer_output); + ptr_command = weechat_string_input_for_buffer (temp_buffer); if (ptr_command) { - weechat_command (python_eval_buffer, *python_buffer_output); + weechat_command (python_eval_buffer, temp_buffer); } else { - length = 1 + strlen (*python_buffer_output) + 1; + length = 1 + strlen (temp_buffer) + 1; command = malloc (length); if (command) { snprintf (command, length, "%c%s", - *python_buffer_output[0], *python_buffer_output); + temp_buffer[0], temp_buffer); weechat_command (python_eval_buffer, (command[0]) ? command : " "); free (command); @@ -375,7 +381,7 @@ weechat_python_output_flush () } else { - weechat_printf (python_eval_buffer, "%s", *python_buffer_output); + weechat_printf (python_eval_buffer, "%s", temp_buffer); } } else @@ -386,10 +392,10 @@ weechat_python_output_flush () weechat_gettext ("%s: stdout/stderr (%s): %s"), PYTHON_PLUGIN_NAME, (python_current_script) ? python_current_script->name : "?", - *python_buffer_output); + temp_buffer); } - weechat_string_dyn_copy (python_buffer_output, NULL); + free (temp_buffer); } /* diff --git a/src/plugins/ruby/weechat-ruby.c b/src/plugins/ruby/weechat-ruby.c index 09aa122d5..39b3e55d2 100644 --- a/src/plugins/ruby/weechat-ruby.c +++ b/src/plugins/ruby/weechat-ruby.c @@ -362,36 +362,42 @@ void weechat_ruby_output_flush () { const char *ptr_command; - char *command; + char *temp_buffer, *command; int length; if (!*ruby_buffer_output[0]) return; + /* if there's no buffer, we catch the output, so there's no flush */ + if (ruby_eval_mode && !ruby_eval_buffer) + return; + + temp_buffer = strdup (*ruby_buffer_output); + if (!temp_buffer) + return; + + weechat_string_dyn_copy (ruby_buffer_output, NULL); + if (ruby_eval_mode) { - /* if there's no buffer, we catch the output, so there's no flush */ - if (!ruby_eval_buffer) - return; - if (ruby_eval_send_input) { if (ruby_eval_exec_commands) - ptr_command = *ruby_buffer_output; + ptr_command = temp_buffer; else - ptr_command = weechat_string_input_for_buffer (*ruby_buffer_output); + ptr_command = weechat_string_input_for_buffer (temp_buffer); if (ptr_command) { - weechat_command (ruby_eval_buffer, *ruby_buffer_output); + weechat_command (ruby_eval_buffer, temp_buffer); } else { - length = 1 + strlen (*ruby_buffer_output) + 1; + length = 1 + strlen (temp_buffer) + 1; command = malloc (length); if (command) { snprintf (command, length, "%c%s", - *ruby_buffer_output[0], *ruby_buffer_output); + temp_buffer[0], temp_buffer); weechat_command (ruby_eval_buffer, (command[0]) ? command : " "); free (command); @@ -400,7 +406,7 @@ weechat_ruby_output_flush () } else { - weechat_printf (ruby_eval_buffer, "%s", *ruby_buffer_output); + weechat_printf (ruby_eval_buffer, "%s", temp_buffer); } } else @@ -411,10 +417,10 @@ weechat_ruby_output_flush () weechat_gettext ("%s: stdout/stderr (%s): %s"), RUBY_PLUGIN_NAME, (ruby_current_script) ? ruby_current_script->name : "?", - *ruby_buffer_output); + temp_buffer); } - weechat_string_dyn_copy (ruby_buffer_output, NULL); + free (temp_buffer); } /*