diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2014-03-02 08:32:05 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2014-03-02 08:32:05 +0100 |
commit | ddf3e9043bed41a37d06bf3d5c6a104e7ddbeb79 (patch) | |
tree | 1f22c63480d85328bf842c8ebd357ec507490403 /src/plugins | |
parent | 8f0294f8d091b9a10f0b7a70838080c756f4902b (diff) | |
download | weechat-ddf3e9043bed41a37d06bf3d5c6a104e7ddbeb79.zip |
trigger: do not create the trigger with /trigger add if the regex is invalid
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/trigger/trigger-command.c | 45 | ||||
-rw-r--r-- | src/plugins/trigger/trigger-config.c | 26 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.c | 57 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.h | 9 |
4 files changed, 104 insertions, 33 deletions
diff --git a/src/plugins/trigger/trigger-command.c b/src/plugins/trigger/trigger-command.c index 7e1b7a76e..efc1371dd 100644 --- a/src/plugins/trigger/trigger-command.c +++ b/src/plugins/trigger/trigger-command.c @@ -285,10 +285,10 @@ trigger_command_list_default (int verbose) for (i = 0; trigger_config_default_list[i][0]; i++) { - trigger_split_regex (trigger_config_default_list[i][0], - trigger_config_default_list[i][5], - ®ex_count, - ®ex); + if (trigger_regex_split (trigger_config_default_list[i][5], + ®ex_count, + ®ex) < 0) + continue; trigger_split_command (trigger_config_default_list[i][6], &commands_count, &commands); @@ -309,7 +309,7 @@ trigger_command_list_default (int verbose) verbose); } - trigger_free_regex (®ex_count, ®ex); + trigger_regex_free (®ex_count, ®ex); if (commands) weechat_string_free_split (commands); } @@ -430,8 +430,10 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { struct t_trigger *ptr_trigger, *ptr_trigger2; + struct t_trigger_regex *regex; char *value, **sargv, **items, input[1024], str_pos[16]; int rc, i, type, count, index_option, enable, sargc, num_items, add_rc; + int regex_count, regex_rc; /* make C compiler happy */ (void) data; @@ -484,6 +486,39 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, weechat_prefix ("error"), sargv[1]); goto end; } + if ((sargc > 4) && sargv[4][0]) + { + regex_count = 0; + regex = NULL; + regex_rc = trigger_regex_split (sargv[4], ®ex_count, ®ex); + trigger_regex_free (®ex_count, ®ex); + switch (regex_rc) + { + case 0: /* OK */ + break; + case -1: /* format error */ + weechat_printf (NULL, + _("%sError: invalid format for regular " + "expression"), + weechat_prefix ("error")); + goto end; + break; + case -2: /* regex compilation error */ + weechat_printf (NULL, + _("%sError: invalid regular expression " + "(compilation failed)"), + weechat_prefix ("error")); + goto end; + break; + case -3: /* memory error */ + weechat_printf (NULL, + _("%s%s: not enough memory"), + weechat_prefix ("error"), + TRIGGER_PLUGIN_NAME); + goto end; + break; + } + } if ((sargc > 6) && sargv[6][0] && (trigger_search_return_code (sargv[6]) < 0)) { diff --git a/src/plugins/trigger/trigger-config.c b/src/plugins/trigger/trigger-config.c index 7f98b5321..ff158a440 100644 --- a/src/plugins/trigger/trigger-config.c +++ b/src/plugins/trigger/trigger-config.c @@ -186,8 +186,30 @@ trigger_config_change_trigger_regex (void *data, struct t_config_option *option) if (!ptr_trigger) return; - trigger_split_regex (ptr_trigger->name, weechat_config_string (option), - &ptr_trigger->regex_count, &ptr_trigger->regex); + switch (trigger_regex_split (weechat_config_string (option), + &ptr_trigger->regex_count, + &ptr_trigger->regex)) + { + case 0: /* OK */ + break; + case -1: /* format error */ + weechat_printf (NULL, + _("%sError: invalid format for option \"regex\", " + "see /help trigger.trigger.%s.regex"), + weechat_prefix ("error"), ptr_trigger->name); + break; + case -2: /* regex compilation error */ + weechat_printf (NULL, + _("%sError: invalid regular expression in option " + "\"regex\", see /help trigger.trigger.%s.regex"), + weechat_prefix ("error"), ptr_trigger->name); + break; + case -3: /* memory error */ + weechat_printf (NULL, + _("%s%s: not enough memory"), + weechat_prefix ("error"), TRIGGER_PLUGIN_NAME); + break; + } } /* diff --git a/src/plugins/trigger/trigger.c b/src/plugins/trigger/trigger.c index d74c2dda4..45953dcb1 100644 --- a/src/plugins/trigger/trigger.c +++ b/src/plugins/trigger/trigger.c @@ -449,7 +449,7 @@ trigger_hook (struct t_trigger *trigger) */ void -trigger_free_regex (int *regex_count, struct t_trigger_regex **regex) +trigger_regex_free (int *regex_count, struct t_trigger_regex **regex) { int i; @@ -479,25 +479,32 @@ trigger_free_regex (int *regex_count, struct t_trigger_regex **regex) /* * Splits the regex in structures, with regex and replacement text. + * + * Returns: + * 0: OK + * -1: format error + * -2: regex compilation error + * -3: not enough memory */ -void -trigger_split_regex (const char *trigger_name, const char *str_regex, +int +trigger_regex_split (const char *str_regex, int *regex_count, struct t_trigger_regex **regex) { const char *ptr_regex, *pos, *pos_replace, *pos_replace_end; const char *pos_next_regex; char *delimiter; - int index, length_delimiter; + int rc, index, length_delimiter; struct t_trigger_regex *new_regex; + rc = 0; delimiter = NULL; if (!regex_count || !regex) goto end; /* remove any existing regex */ - trigger_free_regex (regex_count, regex); + trigger_regex_free (regex_count, regex); if (!str_regex || !str_regex[0]) goto end; @@ -574,13 +581,15 @@ trigger_split_regex (const char *trigger_name, const char *str_regex, (*regex)[index].str_regex, REG_EXTENDED | REG_ICASE) != 0) { + /* weechat_printf (NULL, _("%s%s: error compiling regular expression \"%s\""), weechat_prefix ("error"), TRIGGER_PLUGIN_NAME, (*regex)[index].str_regex); + */ free ((*regex)[index].regex); (*regex)[index].regex = NULL; - goto end; + goto compile_error; } /* set replace and replace_eval */ @@ -627,24 +636,24 @@ trigger_split_regex (const char *trigger_name, const char *str_regex, goto end; format_error: - weechat_printf (NULL, - _("%s%s: invalid value for option \"regex\", " - "see /help trigger.trigger.%s.regex"), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME, - trigger_name); - trigger_free_regex (regex_count, regex); + rc = -1; + goto end; + +compile_error: + rc = -2; goto end; memory_error: - weechat_printf (NULL, - _("%s%s: not enough memory"), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME); - trigger_free_regex (regex_count, regex); + rc = -3; goto end; end: if (delimiter) free (delimiter); + if (rc < 0) + trigger_regex_free (regex_count, regex); + + return rc; } /* @@ -825,10 +834,16 @@ trigger_new_with_options (const char *name, struct t_config_option **options) trigger_add (new_trigger, &triggers, &last_trigger); triggers_count++; - trigger_split_regex (new_trigger->name, - weechat_config_string (new_trigger->options[TRIGGER_OPTION_REGEX]), - &new_trigger->regex_count, - &new_trigger->regex); + if (trigger_regex_split (weechat_config_string (new_trigger->options[TRIGGER_OPTION_REGEX]), + &new_trigger->regex_count, + &new_trigger->regex) < 0) + { + weechat_printf (NULL, + _("%sError: invalid regular expression in trigger " + "\"%s\""), + weechat_prefix ("error"), + name); + } trigger_split_command (weechat_config_string (new_trigger->options[TRIGGER_OPTION_COMMAND]), &new_trigger->commands_count, &new_trigger->commands); @@ -1020,7 +1035,7 @@ trigger_free (struct t_trigger *trigger) /* free data */ trigger_unhook (trigger); - trigger_free_regex (&trigger->regex_count, &trigger->regex); + trigger_regex_free (&trigger->regex_count, &trigger->regex); if (trigger->name) free (trigger->name); for (i = 0; i < TRIGGER_NUM_OPTIONS; i++) diff --git a/src/plugins/trigger/trigger.h b/src/plugins/trigger/trigger.h index 4c5e251ea..2c7346266 100644 --- a/src/plugins/trigger/trigger.h +++ b/src/plugins/trigger/trigger.h @@ -126,12 +126,11 @@ extern int trigger_search_hook_type (const char *type); extern int trigger_search_return_code (const char *return_code); extern struct t_trigger *trigger_search (const char *name); extern struct t_trigger *trigger_search_with_option (struct t_config_option *option); -extern void trigger_free_regex (int *regex_count, +extern void trigger_regex_free (int *regex_count, + struct t_trigger_regex **regex); +extern int trigger_regex_split (const char *str_regex, + int *regex_count, struct t_trigger_regex **regex); -extern void trigger_split_regex (const char *trigger_name, - const char *str_regex, - int *regex_count, - struct t_trigger_regex **regex); extern void trigger_split_command (const char *command, int *commands_count, char ***commands); extern void trigger_unhook (struct t_trigger *trigger); |