diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/logger/logger.c | 49 | ||||
-rw-r--r-- | src/plugins/logger/logger.h | 3 | ||||
-rw-r--r-- | src/plugins/relay/relay-client-weechat.c | 29 | ||||
-rw-r--r-- | src/plugins/scripts/script-api.c | 80 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 30 | ||||
-rw-r--r-- | src/plugins/xfer/xfer-chat.c | 16 |
6 files changed, 103 insertions, 104 deletions
diff --git a/src/plugins/logger/logger.c b/src/plugins/logger/logger.c index f722114bb..1641e528d 100644 --- a/src/plugins/logger/logger.c +++ b/src/plugins/logger/logger.c @@ -54,7 +54,6 @@ WEECHAT_PLUGIN_LICENSE(WEECHAT_LICENSE); struct t_weechat_plugin *weechat_logger_plugin = NULL; struct t_hook *logger_timer = NULL; /* timer to flush log files */ -char *logger_buf_write = NULL; /* buffer for writing a line */ /* @@ -514,18 +513,12 @@ void logger_write_line (struct t_logger_buffer *logger_buffer, const char *format, ...) { - va_list argptr; - char *message, buf_time[256]; + char *message, buf_time[256], buf_beginning[1024]; const char *charset; time_t seconds; struct tm *date_tmp; int log_level; - if (!logger_buf_write) - logger_buf_write = malloc (LOGGER_BUF_WRITE_SIZE); - if (!logger_buf_write) - return; - charset = weechat_info_get ("charset_terminal", ""); if (!logger_buffer->log_file) @@ -580,13 +573,13 @@ logger_write_line (struct t_logger_buffer *logger_buffer, weechat_config_string (logger_config_file_time_format), date_tmp); } - snprintf (logger_buf_write, LOGGER_BUF_WRITE_SIZE, + snprintf (buf_beginning, sizeof (buf_beginning), _("%s\t**** Beginning of log ****"), buf_time); message = (charset) ? - weechat_iconv_from_internal (charset, logger_buf_write) : NULL; + weechat_iconv_from_internal (charset, buf_beginning) : NULL; fprintf (logger_buffer->log_file, - "%s\n", (message) ? message : logger_buf_write); + "%s\n", (message) ? message : buf_beginning); if (message) free (message); logger_buffer->flush_needed = 1; @@ -594,23 +587,22 @@ logger_write_line (struct t_logger_buffer *logger_buffer, logger_buffer->write_start_info_line = 0; } - va_start (argptr, format); - vsnprintf (logger_buf_write, LOGGER_BUF_WRITE_SIZE, format, argptr); - va_end (argptr); - - message = (charset) ? - weechat_iconv_from_internal (charset, logger_buf_write) : NULL; - - fprintf (logger_buffer->log_file, - "%s\n", (message) ? message : logger_buf_write); - if (message) - free (message); - logger_buffer->flush_needed = 1; - - if (!logger_timer) + weechat_va_format (format); + if (vbuffer) { - fflush (logger_buffer->log_file); - logger_buffer->flush_needed = 0; + message = (charset) ? + weechat_iconv_from_internal (charset, vbuffer) : NULL; + fprintf (logger_buffer->log_file, + "%s\n", (message) ? message : vbuffer); + if (message) + free (message); + logger_buffer->flush_needed = 1; + if (!logger_timer) + { + fflush (logger_buffer->log_file); + logger_buffer->flush_needed = 0; + } + free (vbuffer); } } @@ -1339,8 +1331,5 @@ weechat_plugin_end (struct t_weechat_plugin *plugin) logger_config_free (); - if (logger_buf_write) - free (logger_buf_write); - return WEECHAT_RC_OK; } diff --git a/src/plugins/logger/logger.h b/src/plugins/logger/logger.h index 7a11e747b..020cbb2ca 100644 --- a/src/plugins/logger/logger.h +++ b/src/plugins/logger/logger.h @@ -23,8 +23,7 @@ #define weechat_plugin weechat_logger_plugin #define LOGGER_PLUGIN_NAME "logger" -#define LOGGER_BUF_WRITE_SIZE (16*1024) -#define LOGGER_LEVEL_DEFAULT 9 +#define LOGGER_LEVEL_DEFAULT 9 extern struct t_weechat_plugin *weechat_logger_plugin; diff --git a/src/plugins/relay/relay-client-weechat.c b/src/plugins/relay/relay-client-weechat.c index 45170d1b2..ccfffcdf8 100644 --- a/src/plugins/relay/relay-client-weechat.c +++ b/src/plugins/relay/relay-client-weechat.c @@ -43,25 +43,30 @@ int relay_client_weechat_sendf (struct t_relay_client *client, const char *format, ...) { - va_list args; - static char buffer[4096]; char str_length[8]; - int length, num_sent; + int length_vbuffer, num_sent, total_sent; if (!client) return 0; - va_start (args, format); - vsnprintf (buffer + 7, sizeof (buffer) - 7 - 1, format, args); - va_end (args); + weechat_va_format (format); + if (!vbuffer) + return 0; + length_vbuffer = strlen (vbuffer); - length = strlen (buffer + 7); - snprintf (str_length, sizeof (str_length), "%07d", length); - memcpy (buffer, str_length, 7); + total_sent = 0; - num_sent = send (client->sock, buffer, length + 7, 0); + snprintf (str_length, sizeof (str_length), "%07d", length_vbuffer); - client->bytes_sent += length + 7; + num_sent = send (client->sock, str_length, 7, 0); + client->bytes_sent += 7; + total_sent += num_sent; + if (num_sent >= 0) + { + num_sent = send (client->sock, vbuffer, length_vbuffer, 0); + client->bytes_sent += length_vbuffer; + total_sent += num_sent; + } if (num_sent < 0) { @@ -71,7 +76,7 @@ relay_client_weechat_sendf (struct t_relay_client *client, strerror (errno)); } - return num_sent; + return total_sent; } /* diff --git a/src/plugins/scripts/script-api.c b/src/plugins/scripts/script-api.c index 9f05f6d2c..2c04d0b80 100644 --- a/src/plugins/scripts/script-api.c +++ b/src/plugins/scripts/script-api.c @@ -609,24 +609,19 @@ script_api_printf (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, struct t_gui_buffer *buffer, const char *format, ...) { - va_list argptr; - char *buf, *buf2; + char *buf2; - buf = malloc (128 * 1024); - if (!buf) + weechat_va_format (format); + if (!vbuffer) return; - va_start (argptr, format); - vsnprintf (buf, 128 * 1024, format, argptr); - va_end (argptr); - buf2 = (script && script->charset && script->charset[0]) ? - weechat_iconv_to_internal (script->charset, buf) : NULL; - weechat_printf (buffer, "%s", (buf2) ? buf2 : buf); - - free (buf); + weechat_iconv_to_internal (script->charset, vbuffer) : NULL; + weechat_printf (buffer, "%s", (buf2) ? buf2 : vbuffer); if (buf2) free (buf2); + + free (vbuffer); } /* @@ -640,25 +635,20 @@ script_api_printf_date_tags (struct t_weechat_plugin *weechat_plugin, time_t date, const char *tags, const char *format, ...) { - va_list argptr; - char *buf, *buf2; - - buf = malloc (128 * 1024); - if (!buf) - return; + char *buf2; - va_start (argptr, format); - vsnprintf (buf, 128 * 1024, format, argptr); - va_end (argptr); + weechat_va_format (format); + if (!vbuffer) + return; buf2 = (script->charset && script->charset[0]) ? - weechat_iconv_to_internal (script->charset, buf) : NULL; + weechat_iconv_to_internal (script->charset, vbuffer) : NULL; weechat_printf_date_tags (buffer, date, tags, - "%s", (buf2) ? buf2 : buf); - - free (buf); + "%s", (buf2) ? buf2 : vbuffer); if (buf2) free (buf2); + + free (vbuffer); } /* @@ -671,24 +661,19 @@ script_api_printf_y (struct t_weechat_plugin *weechat_plugin, struct t_gui_buffer *buffer, int y, const char *format, ...) { - va_list argptr; - char *buf, *buf2; - - buf = malloc (128 * 1024); - if (!buf) - return; + char *buf2; - va_start (argptr, format); - vsnprintf (buf, 128 * 1024, format, argptr); - va_end (argptr); + weechat_va_format (format); + if (!vbuffer) + return; buf2 = (script->charset && script->charset[0]) ? - weechat_iconv_to_internal (script->charset, buf) : NULL; - weechat_printf_y (buffer, y, "%s", (buf2) ? buf2 : buf); - - free (buf); + weechat_iconv_to_internal (script->charset, vbuffer) : NULL; + weechat_printf_y (buffer, y, "%s", (buf2) ? buf2 : vbuffer); if (buf2) free (buf2); + + free (vbuffer); } /* @@ -700,22 +685,19 @@ script_api_log_printf (struct t_weechat_plugin *weechat_plugin, struct t_plugin_script *script, const char *format, ...) { - va_list argptr; - char *buf, *buf2; - - buf = malloc (128 * 1024); + char *buf2; - va_start (argptr, format); - vsnprintf (buf, 128 * 1024, format, argptr); - va_end (argptr); + weechat_va_format (format); + if (!vbuffer) + return; buf2 = (script->charset && script->charset[0]) ? - weechat_iconv_to_internal (script->charset, buf) : NULL; - weechat_log_printf ("%s", (buf2) ? buf2 : buf); - - free (buf); + weechat_iconv_to_internal (script->charset, vbuffer) : NULL; + weechat_log_printf ("%s", (buf2) ? buf2 : vbuffer); if (buf2) free (buf2); + + free (vbuffer); } /* diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 2eb8a49a3..5c8837b6c 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -46,7 +46,7 @@ struct timeval; */ /* API version (used to check that plugin has same API and can be loaded) */ -#define WEECHAT_PLUGIN_API_VERSION "20110820-01" +#define WEECHAT_PLUGIN_API_VERSION "20110826-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -145,6 +145,34 @@ struct timeval; #define WEECHAT_HOOK_SIGNAL_INT "int" #define WEECHAT_HOOK_SIGNAL_POINTER "pointer" +/* macro to format string with variable args, using dynamic buffer size */ +#define weechat_va_format(__format) \ + va_list argptr; \ + int vaa_size, vaa_num; \ + char *vbuffer, *vaa_buffer2; \ + vaa_size = 1024; \ + vbuffer = malloc (vaa_size); \ + if (vbuffer) \ + { \ + while (1) \ + { \ + va_start (argptr, __format); \ + vaa_num = vsnprintf (vbuffer, vaa_size, __format, argptr); \ + va_end (argptr); \ + if ((vaa_num >= 0) && (vaa_num < vaa_size)) \ + break; \ + vaa_size = (vaa_num >= 0) ? vaa_num + 1 : vaa_size * 2; \ + vaa_buffer2 = realloc (vbuffer, vaa_size); \ + if (!vaa_buffer2) \ + { \ + free (vbuffer); \ + vbuffer = NULL; \ + break; \ + } \ + vbuffer = vaa_buffer2; \ + } \ + } + struct t_weechat_plugin { /* plugin variables */ diff --git a/src/plugins/xfer/xfer-chat.c b/src/plugins/xfer/xfer-chat.c index 44c406595..baf2acdb9 100644 --- a/src/plugins/xfer/xfer-chat.c +++ b/src/plugins/xfer/xfer-chat.c @@ -54,27 +54,21 @@ xfer_chat_send (struct t_xfer *xfer, const char *buffer, int size_buf) void xfer_chat_sendf (struct t_xfer *xfer, const char *format, ...) { - va_list args; - static char buffer[4096]; - int size_buf; char *ptr_msg, *msg_encoded; if (!xfer || (xfer->sock < 0)) return; - va_start (args, format); - size_buf = vsnprintf (buffer, sizeof (buffer) - 1, format, args); - va_end (args); - if (size_buf == 0) + weechat_va_format (format); + if (!vbuffer) return; - buffer[sizeof (buffer) - 1] = '\0'; msg_encoded = (xfer->charset_modifier) ? weechat_hook_modifier_exec ("charset_encode", xfer->charset_modifier, - buffer) : NULL; + vbuffer) : NULL; - ptr_msg = (msg_encoded) ? msg_encoded : buffer; + ptr_msg = (msg_encoded) ? msg_encoded : vbuffer; if (xfer_chat_send (xfer, ptr_msg, strlen (ptr_msg)) <= 0) { @@ -87,6 +81,8 @@ xfer_chat_sendf (struct t_xfer *xfer, const char *format, ...) if (msg_encoded) free (msg_encoded); + + free (vbuffer); } /* |