1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-03 00:03:12 +02:00

core: fix check of tags in lines

All changes:
- fix check of tags in lines: check lines without tags, fix check of tags with
  negation ("!tag")
- add string functions string_split_tags and string_free_split_tags
- add tests on function gui_line_match_tags
This commit is contained in:
Sébastien Helleu
2018-08-07 21:50:04 +02:00
parent d699ae89aa
commit 12a6f74ec0
23 changed files with 382 additions and 275 deletions
+5 -31
View File
@@ -885,9 +885,6 @@ void
config_change_highlight_tags (const void *pointer, void *data,
struct t_config_option *option)
{
int i;
char **tags_array;
/* make C compiler happy */
(void) pointer;
(void) data;
@@ -895,11 +892,7 @@ config_change_highlight_tags (const void *pointer, void *data,
if (config_highlight_tags)
{
for (i = 0; i < config_num_highlight_tags; i++)
{
string_free_split (config_highlight_tags[i]);
}
free (config_highlight_tags);
string_free_split_tags (config_highlight_tags);
config_highlight_tags = NULL;
}
config_num_highlight_tags = 0;
@@ -907,22 +900,9 @@ config_change_highlight_tags (const void *pointer, void *data,
if (CONFIG_STRING(config_look_highlight_tags)
&& CONFIG_STRING(config_look_highlight_tags)[0])
{
tags_array = string_split (CONFIG_STRING(config_look_highlight_tags),
",", 0, 0, &config_num_highlight_tags);
if (tags_array)
{
config_highlight_tags = malloc (config_num_highlight_tags *
sizeof (*config_highlight_tags));
if (config_highlight_tags)
{
for (i = 0; i < config_num_highlight_tags; i++)
{
config_highlight_tags[i] = string_split (tags_array[i],
"+", 0, 0, NULL);
}
}
string_free_split (tags_array);
}
config_highlight_tags = string_split_tags (
CONFIG_STRING(config_look_highlight_tags),
&config_num_highlight_tags);
}
}
@@ -4618,8 +4598,6 @@ config_weechat_write ()
void
config_weechat_free ()
{
int i;
config_file_free (weechat_config_file);
if (config_highlight_regex)
@@ -4631,11 +4609,7 @@ config_weechat_free ()
if (config_highlight_tags)
{
for (i = 0; i < config_num_highlight_tags; i++)
{
string_free_split (config_highlight_tags[i]);
}
free (config_highlight_tags);
string_free_split_tags (config_highlight_tags);
config_highlight_tags = NULL;
}
config_num_highlight_tags = 0;
+16 -29
View File
@@ -2419,8 +2419,6 @@ hook_print (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer,
{
struct t_hook *new_hook;
struct t_hook_print *new_hook_print;
char **tags_array;
int i;
if (!callback)
return NULL;
@@ -2441,28 +2439,8 @@ hook_print (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer,
new_hook->hook_data = new_hook_print;
new_hook_print->callback = callback;
new_hook_print->buffer = buffer;
new_hook_print->tags_count = 0;
new_hook_print->tags_array = NULL;
if (tags)
{
tags_array = string_split (tags, ",", 0, 0,
&new_hook_print->tags_count);
if (tags_array)
{
new_hook_print->tags_array = malloc (new_hook_print->tags_count *
sizeof (*new_hook_print->tags_array));
if (new_hook_print->tags_array)
{
for (i = 0; i < new_hook_print->tags_count; i++)
{
new_hook_print->tags_array[i] = string_split (tags_array[i],
"+", 0, 0,
NULL);
}
}
string_free_split (tags_array);
}
}
new_hook_print->tags_array = string_split_tags (tags,
&new_hook_print->tags_count);
new_hook_print->message = (message) ? strdup (message) : NULL;
new_hook_print->strip_colors = strip_colors;
@@ -4016,11 +3994,7 @@ unhook (struct t_hook *hook)
case HOOK_TYPE_PRINT:
if (HOOK_PRINT(hook, tags_array))
{
for (i = 0; i < HOOK_PRINT(hook, tags_count); i++)
{
string_free_split (HOOK_PRINT(hook, tags_array)[i]);
}
free (HOOK_PRINT(hook, tags_array));
string_free_split_tags (HOOK_PRINT(hook, tags_array));
HOOK_PRINT(hook, tags_array) = NULL;
}
if (HOOK_PRINT(hook, message))
@@ -4897,6 +4871,19 @@ hook_print_log ()
log_printf (" buffer. . . . . . . . : 0x%lx", HOOK_PRINT(ptr_hook, buffer));
log_printf (" tags_count. . . . . . : %d", HOOK_PRINT(ptr_hook, tags_count));
log_printf (" tags_array. . . . . . : 0x%lx", HOOK_PRINT(ptr_hook, tags_array));
if (HOOK_PRINT(ptr_hook, tags_array))
{
for (i = 0; i < HOOK_PRINT(ptr_hook, tags_count); i++)
{
for (j = 0; HOOK_PRINT(ptr_hook, tags_array)[i][j]; j++)
{
log_printf (" tags_array[%03d][%03d]: '%s'",
i,
j,
HOOK_PRINT(ptr_hook, tags_array)[i][j]);
}
}
}
log_printf (" message . . . . . . . : '%s'", HOOK_PRINT(ptr_hook, message));
log_printf (" strip_colors. . . . . : %d", HOOK_PRINT(ptr_hook, strip_colors));
break;
+67
View File
@@ -2306,6 +2306,73 @@ string_free_split_command (char **split_command)
}
}
/*
* Splits tags in an array of tags.
*
* The format of tags is a list of tags separated by commas (logical OR),
* and for each item, multiple tags can be separated by "+" (logical AND).
*
* For example:
* irc_join
* irc_join,irc_quit
* irc_join+nick_toto,irc_quit
*/
char ***
string_split_tags (const char *tags, int *num_tags)
{
char ***tags_array, **tags_array_temp;
int i, tags_count;
tags_array = NULL;
tags_count = 0;
if (tags)
{
tags_array_temp = string_split (tags, ",", 0, 0, &tags_count);
if (tags_array_temp && (tags_count > 0))
{
tags_array = malloc ((tags_count + 1) * sizeof (*tags_array));
if (tags_array)
{
for (i = 0; i < tags_count; i++)
{
tags_array[i] = string_split_shared (tags_array_temp[i],
"+", 0, 0,
NULL);
}
tags_array[tags_count] = NULL;
}
}
if (tags_array_temp)
string_free_split (tags_array_temp);
}
if (num_tags)
*num_tags = tags_count;
return tags_array;
}
/*
* Frees tags split.
*/
void
string_free_split_tags (char ***split_tags)
{
int i;
if (split_tags)
{
for (i = 0; split_tags[i]; i++)
{
string_free_split_shared (split_tags[i]);
}
free (split_tags);
}
}
/*
* Converts a string to another charset.
*
+2
View File
@@ -95,6 +95,8 @@ extern char *string_build_with_split_string (const char **split_string,
const char *separator);
extern char **string_split_command (const char *command, char separator);
extern void string_free_split_command (char **split_command);
extern char ***string_split_tags (const char *tags, int *num_tags);
extern void string_free_split_tags (char ***split_tags);
extern char *string_iconv (int from_utf8, const char *from_code,
const char *to_code, const char *string);
extern char *string_iconv_to_internal (const char *charset, const char *string);
+11 -67
View File
@@ -1629,9 +1629,6 @@ void
gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
const char *new_tags)
{
int i;
char **tags_array;
if (!buffer)
return;
@@ -1642,11 +1639,7 @@ gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
}
if (buffer->highlight_tags_restrict_array)
{
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
{
string_free_split (buffer->highlight_tags_restrict_array[i]);
}
free (buffer->highlight_tags_restrict_array);
string_free_split_tags (buffer->highlight_tags_restrict_array);
buffer->highlight_tags_restrict_array = NULL;
}
buffer->highlight_tags_restrict_count = 0;
@@ -1658,24 +1651,9 @@ gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
if (!buffer->highlight_tags_restrict)
return;
tags_array = string_split (buffer->highlight_tags_restrict, ",", 0, 0,
&buffer->highlight_tags_restrict_count);
if (tags_array)
{
buffer->highlight_tags_restrict_array =
malloc (buffer->highlight_tags_restrict_count *
sizeof (*buffer->highlight_tags_restrict_array));
if (buffer->highlight_tags_restrict_array)
{
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
{
buffer->highlight_tags_restrict_array[i] = string_split (tags_array[i],
"+", 0, 0,
NULL);
}
}
string_free_split (tags_array);
}
buffer->highlight_tags_restrict_array = string_split_tags (
buffer->highlight_tags_restrict,
&buffer->highlight_tags_restrict_count);
}
/*
@@ -1686,9 +1664,6 @@ void
gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
const char *new_tags)
{
int i;
char **tags_array;
if (!buffer)
return;
@@ -1699,11 +1674,7 @@ gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
}
if (buffer->highlight_tags_array)
{
for (i = 0; i < buffer->highlight_tags_count; i++)
{
string_free_split (buffer->highlight_tags_array[i]);
}
free (buffer->highlight_tags_array);
string_free_split_tags (buffer->highlight_tags_array);
buffer->highlight_tags_array = NULL;
}
buffer->highlight_tags_count = 0;
@@ -1715,24 +1686,9 @@ gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
if (!buffer->highlight_tags)
return;
tags_array = string_split (buffer->highlight_tags, ",", 0, 0,
&buffer->highlight_tags_count);
if (tags_array)
{
buffer->highlight_tags_array =
malloc (buffer->highlight_tags_count *
sizeof (*buffer->highlight_tags_array));
if (buffer->highlight_tags_array)
{
for (i = 0; i < buffer->highlight_tags_count; i++)
{
buffer->highlight_tags_array[i] = string_split (tags_array[i],
"+", 0, 0,
NULL);
}
}
string_free_split (tags_array);
}
buffer->highlight_tags_array = string_split_tags (
buffer->highlight_tags,
&buffer->highlight_tags_count);
}
/*
@@ -2717,7 +2673,7 @@ gui_buffer_close (struct t_gui_buffer *buffer)
struct t_gui_window *ptr_window;
struct t_gui_buffer *ptr_buffer, *ptr_back_to_buffer;
struct t_gui_buffer *ptr_buffer_visited_buffer;
int index, i;
int index;
struct t_gui_buffer_visited *ptr_buffer_visited;
if (!buffer)
@@ -2877,23 +2833,11 @@ gui_buffer_close (struct t_gui_buffer *buffer)
if (buffer->highlight_tags_restrict)
free (buffer->highlight_tags_restrict);
if (buffer->highlight_tags_restrict_array)
{
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
{
string_free_split (buffer->highlight_tags_restrict_array[i]);
}
free (buffer->highlight_tags_restrict_array);
}
string_free_split_tags (buffer->highlight_tags_restrict_array);
if (buffer->highlight_tags)
free (buffer->highlight_tags);
if (buffer->highlight_tags_array)
{
for (i = 0; i < buffer->highlight_tags_count; i++)
{
string_free_split (buffer->highlight_tags_array[i]);
}
free (buffer->highlight_tags_array);
}
string_free_split_tags (buffer->highlight_tags_array);
if (buffer->input_callback_data)
free (buffer->input_callback_data);
if (buffer->close_callback_data)
+5 -33
View File
@@ -281,9 +281,9 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
{
struct t_gui_filter *new_filter;
regex_t *regex1, *regex2;
char *pos_tab, *regex_prefix, **tags_array, buf[512], str_error[512];
char *pos_tab, *regex_prefix, buf[512], str_error[512];
const char *ptr_start_regex, *pos_regex_message;
int i, rc;
int rc;
if (!name || !buffer_name || !tags || !regex)
{
@@ -389,28 +389,8 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
",", 0, 0,
&new_filter->num_buffers);
new_filter->tags = (tags) ? strdup (tags) : NULL;
new_filter->tags_count = 0;
new_filter->tags_array = NULL;
if (new_filter->tags)
{
tags_array = string_split (new_filter->tags, ",", 0, 0,
&new_filter->tags_count);
if (tags_array)
{
new_filter->tags_array = malloc (new_filter->tags_count *
sizeof (*new_filter->tags_array));
if (new_filter->tags_array)
{
for (i = 0; i < new_filter->tags_count; i++)
{
new_filter->tags_array[i] = string_split (tags_array[i],
"+", 0, 0,
NULL);
}
}
string_free_split (tags_array);
}
}
new_filter->tags_array = string_split_tags (new_filter->tags,
&new_filter->tags_count);
new_filter->regex = strdup (regex);
new_filter->regex_prefix = regex1;
new_filter->regex_message = regex2;
@@ -465,8 +445,6 @@ gui_filter_rename (struct t_gui_filter *filter, const char *new_name)
void
gui_filter_free (struct t_gui_filter *filter)
{
int i;
if (!filter)
return;
@@ -483,13 +461,7 @@ gui_filter_free (struct t_gui_filter *filter)
if (filter->tags)
free (filter->tags);
if (filter->tags_array)
{
for (i = 0; i < filter->tags_count; i++)
{
string_free_split (filter->tags_array[i]);
}
free (filter->tags_array);
}
string_free_split_tags (filter->tags_array);
if (filter->regex)
free (filter->regex);
if (filter->regex_prefix)
+21 -12
View File
@@ -604,35 +604,44 @@ gui_line_match_tags (struct t_gui_line_data *line_data,
int tags_count, char ***tags_array)
{
int i, j, k, match, tag_found, tag_negated;
const char *ptr_tag;
if (!line_data)
return 0;
if (line_data->tags_count == 0)
return 0;
for (i = 0; i < tags_count; i++)
{
match = 1;
for (j = 0; tags_array[i][j]; j++)
{
ptr_tag = tags_array[i][j];
tag_found = 0;
tag_negated = 0;
/* check if tag is negated (prefixed with a '!') */
if ((tags_array[i][j][0] == '!') && tags_array[i][j][1])
tag_negated = 1;
for (k = 0; k < line_data->tags_count; k++)
if ((ptr_tag[0] == '!') && ptr_tag[1])
{
if (string_match (line_data->tags_array[k],
(tag_negated) ? tags_array[i][j] + 1 : tags_array[i][j],
0))
ptr_tag++;
tag_negated = 1;
}
if (strcmp (ptr_tag, "*") == 0)
{
tag_found = 1;
}
else
{
for (k = 0; k < line_data->tags_count; k++)
{
tag_found = 1;
break;
if (string_match (line_data->tags_array[k], ptr_tag, 0))
{
tag_found = 1;
break;
}
}
}
if (tag_found && tag_negated)
return 0;
if ((!tag_found && !tag_negated) || (tag_found && tag_negated))
{
match = 0;
+3
View File
@@ -70,6 +70,9 @@ struct t_gui_lines
extern struct t_gui_lines *gui_lines_alloc ();
extern void gui_lines_free (struct t_gui_lines *lines);
extern void gui_line_tags_alloc (struct t_gui_line_data *line_data,
const char *tags);
extern void gui_line_tags_free (struct t_gui_line_data *line_data);
extern void gui_line_get_prefix_for_display (struct t_gui_line *line,
char **prefix, int *length,
char **color, int *prefix_is_nick);