summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/wee-alias.c2
-rw-r--r--src/core/wee-hook.c25
-rw-r--r--src/core/wee-hook.h6
-rw-r--r--src/core/wee-input.c26
-rw-r--r--src/core/wee-string.c28
-rw-r--r--src/core/wee-string.h2
-rw-r--r--src/plugins/demo/demo.c11
-rw-r--r--src/plugins/plugin-api.c14
-rw-r--r--src/plugins/plugin-api.h5
-rw-r--r--src/plugins/weechat-plugin.h10
10 files changed, 82 insertions, 47 deletions
diff --git a/src/core/wee-alias.c b/src/core/wee-alias.c
index fd7829d0c..d801c4ce8 100644
--- a/src/core/wee-alias.c
+++ b/src/core/wee-alias.c
@@ -225,7 +225,7 @@ alias_replace_args (char *alias_args, char *user_args)
char **argv, *start, *pos, *res;
int argc, length_res, args_count;
- argv = string_explode (user_args, " ", 0, &argc);
+ argv = string_explode (user_args, " ", 0, 0, &argc);
res = NULL;
length_res = 0;
diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c
index bdf228ed8..cea271b7d 100644
--- a/src/core/wee-hook.c
+++ b/src/core/wee-hook.c
@@ -150,26 +150,45 @@ hook_command (void *plugin, char *command, char *description,
*/
int
-hook_command_exec (void *plugin, char *command, char *args)
+hook_command_exec (void *plugin, char *string)
{
struct t_hook *ptr_hook;
+ char **argv, **argv_eol;
+ int argc;
+
+ if (!string || !string[0])
+ return -1;
+
+ argv = string_explode (string, " ", 0, 0, &argc);
+ if (argc == 0)
+ {
+ if (argv)
+ string_free_exploded (argv);
+ return -1;
+ }
+ argv_eol = string_explode (string, " ", 1, 0, NULL);
for (ptr_hook = weechat_hooks; ptr_hook;
ptr_hook = ptr_hook->next_hook)
{
if ((ptr_hook->type == HOOK_TYPE_COMMAND)
&& (!plugin || (plugin == ptr_hook->plugin))
- && (string_strcasecmp (command,
+ && (string_strcasecmp (argv[0] + 1,
HOOK_COMMAND(ptr_hook, command)) == 0))
{
if ((int) (HOOK_COMMAND(ptr_hook, callback))
- (ptr_hook->callback_data, args) == PLUGIN_RC_FAILED)
+ (ptr_hook->callback_data, argc, argv, argv_eol) == PLUGIN_RC_FAILED)
return 0;
else
return 1;
}
}
+ if (argv)
+ string_free_exploded (argv);
+ if (argv_eol)
+ string_free_exploded (argv_eol);
+
/* no hook found */
return -1;
}
diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h
index a080e9fae..f14eba1b7 100644
--- a/src/core/wee-hook.h
+++ b/src/core/wee-hook.h
@@ -43,8 +43,6 @@ enum t_hook_type
#define HOOK_TIMER(hook, var) (((struct t_hook_timer *)hook->hook_data)->var)
#define HOOK_FD(hook, var) (((struct t_hook_fd *)hook->hook_data)->var)
-typedef int (t_hook_callback) (void *);
-
struct t_hook
{
/* data common to all hooks */
@@ -61,7 +59,7 @@ struct t_hook
struct t_hook *next_hook; /* pointer to next hook */
};
-typedef int (t_hook_callback_command)(void *, char *);
+typedef int (t_hook_callback_command)(void *, int, char **, char **);
struct t_hook_command
{
@@ -120,7 +118,7 @@ extern struct t_hook *last_weechat_hook;
extern int hook_valid_for_plugin (void *, struct t_hook *);
extern struct t_hook *hook_command (void *, char *, char *, char *, char *,
char *, t_hook_callback_command *, void *);
-extern int hook_command_exec (void *, char *, char *);
+extern int hook_command_exec (void *, char *);
extern struct t_hook *hook_message (void *, char *, t_hook_callback_message *,
void *);
extern struct t_hook *hook_config (void *, char *, char *,
diff --git a/src/core/wee-input.c b/src/core/wee-input.c
index 0ce9260dc..faad27150 100644
--- a/src/core/wee-input.c
+++ b/src/core/wee-input.c
@@ -73,6 +73,17 @@ input_exec_command (struct t_gui_buffer *buffer, char *string,
pos[1] = '\0';
}
+ rc = -1;
+ if (!only_builtin)
+ {
+ rc = hook_command_exec (buffer->plugin, command);
+ /*vars_replaced = alias_replace_vars (window, ptr_args);
+ rc = plugin_cmd_handler_exec (window->buffer->protocol, command + 1,
+ (vars_replaced) ? vars_replaced : ptr_args);
+ if (vars_replaced)
+ free (vars_replaced);*/
+ }
+
pos = strchr (command, ' ');
if (pos)
{
@@ -84,17 +95,6 @@ input_exec_command (struct t_gui_buffer *buffer, char *string,
if (!ptr_args[0])
ptr_args = NULL;
}
-
- rc = -1;
- if (!only_builtin)
- {
- rc = hook_command_exec (buffer->plugin, command + 1, ptr_args);
- /*vars_replaced = alias_replace_vars (window, ptr_args);
- rc = plugin_cmd_handler_exec (window->buffer->protocol, command + 1,
- (vars_replaced) ? vars_replaced : ptr_args);
- if (vars_replaced)
- free (vars_replaced);*/
- }
switch (rc)
{
@@ -107,7 +107,7 @@ input_exec_command (struct t_gui_buffer *buffer, char *string,
case 1: /* plugin handler OK, executed */
break;
default: /* plugin handler not found */
- argv = string_explode (ptr_args, " ", 0, &argc);
+ argv = string_explode (ptr_args, " ", 0, 0, &argc);
/* look for alias */
if (!only_builtin)
@@ -380,7 +380,7 @@ input_data (struct t_gui_buffer *buffer, char *data, int only_builtin)
if ((ptr_data[0] == '/') && (ptr_data[1] == '/'))
ptr_data++;
- hook_command_exec (buffer->plugin, "", ptr_data);
+ hook_command_exec (buffer->plugin, ptr_data);
if (buffer->input_data_cb)
{
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 7b9e70ff7..54f9c641e 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -326,11 +326,22 @@ string_convert_hex_chars (char *string)
/*
* string_explode: explode a string according to separators
+ * examples:
+ * string_explode ("abc de fghi", " ", 0, 0, NULL)
+ * ==> array[0] = "abc"
+ * array[1] = "de"
+ * array[2] = "fghi"
+ * array[3] = NULL
+ * string_explode ("abc de fghi", " ", 1, 0, NULL)
+ * ==> array[0] = "abc de fghi"
+ * array[1] = "de fghi"
+ * array[2] = "fghi"
+ * array[3] = NULL
*/
char **
-string_explode (char *string, char *separators, int num_items_max,
- int *num_items)
+string_explode (char *string, char *separators, int keep_eol,
+ int num_items_max, int *num_items)
{
int i, n_items;
char **array;
@@ -379,10 +390,15 @@ string_explode (char *string, char *separators, int num_items_max,
{
if (ptr2 - ptr1 > 0)
{
- array[i] =
- (char *) malloc ((ptr2 - ptr1 + 1) * sizeof (char));
- array[i] = strncpy (array[i], ptr1, ptr2 - ptr1);
- array[i][ptr2 - ptr1] = '\0';
+ if (keep_eol)
+ array[i] = strdup (ptr1);
+ else
+ {
+ array[i] =
+ (char *) malloc ((ptr2 - ptr1 + 1) * sizeof (char));
+ array[i] = strncpy (array[i], ptr1, ptr2 - ptr1);
+ array[i][ptr2 - ptr1] = '\0';
+ }
ptr1 = ++ptr2;
}
else
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index c7ca959db..94ed642d0 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -30,7 +30,7 @@ extern int string_strncasecmp (char *, char *, int);
extern char *string_strcasestr (char *, char *);
extern char *string_replace (char *, char *, char *);
extern char *string_convert_hex_chars (char *);
-extern char **string_explode (char *, char *, int, int *);
+extern char **string_explode (char *, char *, int, int, int *);
extern void string_free_exploded (char **);
extern char **string_split_multi_command (char *, char);
extern void string_free_multi_command (char **);
diff --git a/src/plugins/demo/demo.c b/src/plugins/demo/demo.c
index 16c55323b..9b63e63a6 100644
--- a/src/plugins/demo/demo.c
+++ b/src/plugins/demo/demo.c
@@ -55,7 +55,7 @@ demo_print_list (void *list, char *item_name)
fields = weechat_list_fields (list);
if (fields)
{
- argv = weechat_string_explode (fields, ",", 0, &argc);
+ argv = weechat_string_explode (fields, ",", 0, 0, &argc);
if (argv && (argc > 0))
{
for (j = 0; j < argc; j++)
@@ -119,14 +119,15 @@ demo_buffer_infos ()
*/
static int
-demo_command (void *data, char *args)
+demo_command (void *data, int argc, char **argv, char **argv_eol)
{
/* make C compiler happy */
(void) data;
-
- if (args)
+ (void) argv_eol;
+
+ if (argc > 1)
{
- if (weechat_strcasecmp (args, "buffer") == 0)
+ if (weechat_strcasecmp (argv[1], "buffer") == 0)
{
demo_buffer_infos ();
return PLUGIN_RC_SUCCESS;
diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c
index 6f0ec9e25..348bb49f5 100644
--- a/src/plugins/plugin-api.c
+++ b/src/plugins/plugin-api.c
@@ -160,17 +160,14 @@ plugin_api_strncasecmp (struct t_weechat_plugin *plugin,
char **
plugin_api_string_explode (struct t_weechat_plugin *plugin, char *string,
- char *separators, int num_items_max,
- int *num_items)
+ char *separators, int keep_eol,
+ int num_items_max, int *num_items)
{
- /* make C compiler happy */
- (void) plugin;
-
if (!plugin || !string || !separators || !num_items)
return NULL;
- return string_explode (string, separators, num_items_max,
- num_items);
+ return string_explode (string, separators, keep_eol,
+ num_items_max, num_items);
}
/*
@@ -371,7 +368,8 @@ struct t_hook *
plugin_api_hook_command (struct t_weechat_plugin *plugin, char *command,
char *description, char *args,
char *args_desc, char *completion,
- int (*callback)(void *, char *), void *data)
+ int (*callback)(void *, int, char **, char **),
+ void *data)
{
if (plugin && callback)
return hook_command (plugin, command, description, args,
diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h
index 8ac174e7c..7f34c16e6 100644
--- a/src/plugins/plugin-api.h
+++ b/src/plugins/plugin-api.h
@@ -33,7 +33,7 @@ extern int plugin_api_strcasecmp (struct t_weechat_plugin *,char *, char *);
extern int plugin_api_strncasecmp (struct t_weechat_plugin *,char *, char *,
int);
extern char **plugin_api_string_explode (struct t_weechat_plugin *, char *,
- char *, int, int *);
+ char *, int, int, int *);
extern void plugin_api_string_free_exploded (struct t_weechat_plugin *,
char **);
@@ -54,7 +54,8 @@ extern void plugin_api_infobar_remove (struct t_weechat_plugin *, int);
/* hooks */
extern struct t_hook *plugin_api_hook_command (struct t_weechat_plugin *,
char *, char *, char *, char *,
- char *, int (*)(void *, char *),
+ char *,
+ int (*)(void *, int, char **, char **),
void *);
extern struct t_hook *plugin_api_hook_message (struct t_weechat_plugin *,
char *, int (*)(void *, char *),
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index 19569da37..924891335 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -64,7 +64,7 @@ struct t_weechat_plugin
int (*strcasecmp) (struct t_weechat_plugin *, char *, char *);
int (*strncasecmp) (struct t_weechat_plugin *, char *, char *, int);
char **(*string_explode) (struct t_weechat_plugin *, char *, char *, int,
- int *);
+ int, int *);
void (*string_free_exploded) (struct t_weechat_plugin *, char **);
/* directories */
@@ -82,7 +82,8 @@ struct t_weechat_plugin
/* hooks */
struct t_hook *(*hook_command) (struct t_weechat_plugin *, char *, char *,
char *, char *, char *,
- int (*)(void *, char *),void *);
+ int (*)(void *, int, char **, char **),
+ void *);
struct t_hook *(*hook_message) (struct t_weechat_plugin *, char *,
int (*)(void *, char *), void *);
struct t_hook *(*hook_config) (struct t_weechat_plugin *, char *, char *,
@@ -148,9 +149,10 @@ struct t_weechat_plugin
weechat_plugin->strcasecmp(weechat_plugin, string1, string2)
#define weechat_strncasecmp(string1, string2, max) \
weechat_plugin->strncasecmp(weechat_plugin, string1, string2, max)
-#define weechat_string_explode(string1, separator, max, num_items) \
+#define weechat_string_explode(string1, separator, eol, max, \
+ num_items) \
weechat_plugin->string_explode(weechat_plugin, string1, separator, \
- max, num_items)
+ eol, max, num_items)
#define weechat_string_free_exploded(array_str) \
weechat_plugin->string_free_exploded(weechat_plugin, array_str)