diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-backtrace.c | 17 | ||||
-rw-r--r-- | src/core/wee-completion.c | 2 | ||||
-rw-r--r-- | src/core/wee-config-file.c | 20 | ||||
-rw-r--r-- | src/core/wee-log.c | 65 | ||||
-rw-r--r-- | src/core/wee-string.c | 31 | ||||
-rw-r--r-- | src/gui/gui-chat.c | 36 | ||||
-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 |
12 files changed, 188 insertions, 190 deletions
diff --git a/src/core/wee-backtrace.c b/src/core/wee-backtrace.c index 91c4f4727..fd121f084 100644 --- a/src/core/wee-backtrace.c +++ b/src/core/wee-backtrace.c @@ -44,6 +44,7 @@ #include "wee-backtrace.h" #include "wee-log.h" #include "wee-string.h" +#include "../plugins/plugin.h" /* @@ -54,15 +55,13 @@ void weechat_backtrace_printf (const char *message, ...) { - static char buffer[4096]; - va_list argptr; - - va_start (argptr, message); - vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); - va_end (argptr); - - string_iconv_fprintf (stderr, "%s\n", buffer); - log_printf ("%s", buffer); + weechat_va_format (message); + if (vbuffer) + { + string_iconv_fprintf (stderr, "%s\n", vbuffer); + log_printf ("%s", vbuffer); + free (vbuffer); + } } /* diff --git a/src/core/wee-completion.c b/src/core/wee-completion.c index 8cc3c974e..6f4bc06e2 100644 --- a/src/core/wee-completion.c +++ b/src/core/wee-completion.c @@ -42,7 +42,6 @@ #include "wee-list.h" #include "wee-proxy.h" #include "wee-string.h" -#include "../plugins/plugin.h" #include "../gui/gui-completion.h" #include "../gui/gui-bar.h" #include "../gui/gui-bar-window.h" @@ -52,6 +51,7 @@ #include "../gui/gui-key.h" #include "../gui/gui-nicklist.h" #include "../gui/gui-window.h" +#include "../plugins/plugin.h" /* diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c index 966169e9d..12f3ac612 100644 --- a/src/core/wee-config-file.c +++ b/src/core/wee-config-file.c @@ -1872,22 +1872,24 @@ int config_file_write_line (struct t_config_file *config_file, const char *option_name, const char *value, ...) { - char buf[4096]; - va_list argptr; + int rc; if (!config_file || !option_name) return 0; if (value && value[0]) { - va_start (argptr, value); - vsnprintf (buf, sizeof (buf) - 1, value, argptr); - va_end (argptr); - - if (buf[0]) + weechat_va_format (value); + if (vbuffer) { - return (string_iconv_fprintf (config_file->file, "%s = %s\n", - option_name, buf)); + if (vbuffer[0]) + { + rc = string_iconv_fprintf (config_file->file, "%s = %s\n", + option_name, vbuffer); + free (vbuffer); + return rc; + } + free (vbuffer); } } diff --git a/src/core/wee-log.c b/src/core/wee-log.c index 716a05a53..97f5347bc 100644 --- a/src/core/wee-log.c +++ b/src/core/wee-log.c @@ -43,6 +43,7 @@ #include "wee-log.h" #include "wee-debug.h" #include "wee-string.h" +#include "../plugins/plugin.h" char *weechat_log_filename = NULL; /* log name (~/.weechat/weechat.log) */ @@ -125,48 +126,48 @@ log_init () void log_printf (const char *message, ...) { - static char buffer[4096]; char *ptr_buffer; - va_list argptr; static time_t seconds; struct tm *date_tmp; if (!weechat_log_file) return; - va_start (argptr, message); - vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); - va_end (argptr); - - /* keep only valid chars */ - ptr_buffer = buffer; - while (ptr_buffer[0]) - { - if ((ptr_buffer[0] != '\n') - && (ptr_buffer[0] != '\r') - && ((unsigned char)(ptr_buffer[0]) < 32)) - ptr_buffer[0] = '.'; - ptr_buffer++; - } - - if (!weechat_log_use_time) - string_iconv_fprintf (weechat_log_file, "%s\n", buffer); - else + weechat_va_format (message); + if (vbuffer) { - seconds = time (NULL); - date_tmp = localtime (&seconds); - if (date_tmp) - string_iconv_fprintf (weechat_log_file, - "[%04d-%02d-%02d %02d:%02d:%02d] %s\n", - date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, - date_tmp->tm_mday, date_tmp->tm_hour, - date_tmp->tm_min, date_tmp->tm_sec, - buffer); + /* keep only valid chars */ + ptr_buffer = vbuffer; + while (ptr_buffer[0]) + { + if ((ptr_buffer[0] != '\n') + && (ptr_buffer[0] != '\r') + && ((unsigned char)(ptr_buffer[0]) < 32)) + ptr_buffer[0] = '.'; + ptr_buffer++; + } + + if (!weechat_log_use_time) + string_iconv_fprintf (weechat_log_file, "%s\n", vbuffer); else - string_iconv_fprintf (weechat_log_file, "%s\n", buffer); + { + seconds = time (NULL); + date_tmp = localtime (&seconds); + if (date_tmp) + string_iconv_fprintf (weechat_log_file, + "[%04d-%02d-%02d %02d:%02d:%02d] %s\n", + date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, + date_tmp->tm_mday, date_tmp->tm_hour, + date_tmp->tm_min, date_tmp->tm_sec, + vbuffer); + else + string_iconv_fprintf (weechat_log_file, "%s\n", vbuffer); + } + + fflush (weechat_log_file); + + free (vbuffer); } - - fflush (weechat_log_file); } /* diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 06695b290..7ebee8c44 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -57,6 +57,7 @@ #include "wee-hashtable.h" #include "wee-utf8.h" #include "../gui/gui-color.h" +#include "../plugins/plugin.h" /* @@ -1325,26 +1326,20 @@ string_iconv_from_internal (const char *charset, const char *string) int string_iconv_fprintf (FILE *file, const char *data, ...) { - va_list argptr; - char *buf, *buf2; + char *buf2; int rc, num_written; - buf = malloc (128 * 1024); - if (!buf) - return 0; - - va_start (argptr, data); - vsnprintf (buf, 128 * 1024, data, argptr); - va_end (argptr); - - buf2 = string_iconv_from_internal (NULL, buf); - num_written = fprintf (file, "%s", (buf2) ? buf2 : buf); - - rc = (num_written == (int)strlen ((buf2) ? buf2 : buf)) ? 1 : 0; - - free (buf); - if (buf2) - free (buf2); + rc = 0; + weechat_va_format (data); + if (vbuffer) + { + buf2 = string_iconv_from_internal (NULL, vbuffer); + num_written = fprintf (file, "%s", (buf2) ? buf2 : vbuffer); + rc = (num_written == (int)strlen ((buf2) ? buf2 : vbuffer)) ? 1 : 0; + if (buf2) + free (buf2); + free (vbuffer); + } return rc; } diff --git a/src/gui/gui-chat.c b/src/gui/gui-chat.c index 00fff1ab7..2cfd45453 100644 --- a/src/gui/gui-chat.c +++ b/src/gui/gui-chat.c @@ -567,10 +567,9 @@ void gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, const char *tags, const char *message, ...) { - va_list argptr; time_t date_printed; int display_time, length, at_least_one_message_printed; - char strbuf[8192], *pos, *pos_prefix, *pos_tab, *pos_end; + char *pos, *pos_prefix, *pos_tab, *pos_end; char *modifier_data, *new_msg, *ptr_msg; struct t_gui_line *ptr_line; @@ -601,11 +600,11 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, && (gui_chat_mute_buffer == buffer))) return; - va_start (argptr, message); - vsnprintf (strbuf, sizeof (strbuf) - 1, message, argptr); - va_end (argptr); + weechat_va_format (message); + if (!vbuffer) + return; - utf8_normalize (strbuf, '?'); + utf8_normalize (vbuffer, '?'); date_printed = time (NULL); if (date <= 0) @@ -613,7 +612,7 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, at_least_one_message_printed = 0; - pos = strbuf; + pos = vbuffer; while (pos) { /* display until next end of line */ @@ -648,7 +647,7 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, * print anything */ free (new_msg); - return; + goto end; } if (strcmp (message, new_msg) == 0) { @@ -718,6 +717,9 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, if (gui_init_ok && at_least_one_message_printed) gui_buffer_ask_chat_refresh (buffer, 1); + +end: + free (vbuffer); } /* @@ -729,9 +731,7 @@ gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date, void gui_chat_printf_y (struct t_gui_buffer *buffer, int y, const char *message, ...) { - va_list argptr; struct t_gui_line *ptr_line; - char strbuf[8192]; int i, num_lines_to_add; if (gui_init_ok) @@ -746,14 +746,14 @@ gui_chat_printf_y (struct t_gui_buffer *buffer, int y, const char *message, ...) return; } - va_start (argptr, message); - vsnprintf (strbuf, sizeof (strbuf) - 1, message, argptr); - va_end (argptr); + weechat_va_format (message); + if (!vbuffer) + return; - utf8_normalize (strbuf, '?'); + utf8_normalize (vbuffer, '?'); /* no message: delete line */ - if (!strbuf[0]) + if (!vbuffer[0]) { if (gui_init_ok) { @@ -794,12 +794,14 @@ gui_chat_printf_y (struct t_gui_buffer *buffer, int y, const char *message, ...) gui_line_add_y (buffer, i, ""); } } - gui_line_add_y (buffer, y, strbuf); + gui_line_add_y (buffer, y, vbuffer); gui_buffer_ask_chat_refresh (buffer, 1); } else - string_iconv_fprintf (stdout, "%s\n", strbuf); + string_iconv_fprintf (stdout, "%s\n", vbuffer); } + + free (vbuffer); } /* 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); } /* |