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

core: sort configuration files by name, reload them by priority (issue #1872)

This commit is contained in:
Sébastien Helleu
2023-01-12 21:44:17 +01:00
parent 202b4d82c0
commit 0dd1d1be1c
7 changed files with 183 additions and 25 deletions
+52 -2
View File
@@ -5585,6 +5585,33 @@ command_reload_file (struct t_config_file *config_file)
}
}
/*
* Compares two configuration files to sort them by priority (highest priority
* at beginning of list).
*
* Returns:
* -1: config1 has higher priority than config2
* 1: config1 has same or lower priority than config2
*/
int
command_reload_arraylist_cmp_config_cb (void *data,
struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
struct t_config_file *ptr_config1, *ptr_config2;
/* make C compiler happy */
(void) data;
(void) arraylist;
ptr_config1 = (struct t_config_file *)pointer1;
ptr_config2 = (struct t_config_file *)pointer2;
return (ptr_config1->priority > ptr_config2->priority) ? -1 : 1;
}
/*
* Callback for command "/reload": reloads a configuration file.
*/
@@ -5592,7 +5619,8 @@ command_reload_file (struct t_config_file *config_file)
COMMAND_CALLBACK(reload)
{
struct t_config_file *ptr_config_file;
int i;
struct t_arraylist *all_configs;
int i, list_size;
/* make C compiler happy */
(void) pointer;
@@ -5619,11 +5647,33 @@ COMMAND_CALLBACK(reload)
}
else
{
/*
* build a list of pointers to configs sorted by priority,
* so that configs with high priority are reloaded first
*/
all_configs = arraylist_new (
32, 1, 1,
&command_reload_arraylist_cmp_config_cb, NULL,
NULL, NULL);
if (!all_configs)
COMMAND_ERROR;
for (ptr_config_file = config_files; ptr_config_file;
ptr_config_file = ptr_config_file->next_config)
{
command_reload_file (ptr_config_file);
arraylist_add (all_configs, ptr_config_file);
}
list_size = arraylist_size (all_configs);
for (i = 0; i < list_size; i++)
{
ptr_config_file = (struct t_config_file *)arraylist_get (
all_configs, i);
if (config_file_valid (ptr_config_file))
command_reload_file (ptr_config_file);
}
arraylist_free (all_configs);
}
return WEECHAT_RC_OK;
+81 -11
View File
@@ -59,6 +59,33 @@ char *config_boolean_false[] = { "off", "no", "n", "false", "f", "0", NULL };
void config_file_option_free_data (struct t_config_option *option);
/*
* Checks if a configuration file pointer is valid.
*
* Returns:
* 1: configuration file exists
* 0: configuration file does not exist
*/
int
config_file_valid (struct t_config_file *config_file)
{
struct t_config_file *ptr_config;
if (!config_file)
return 0;
for (ptr_config = config_files; ptr_config;
ptr_config = ptr_config->next_config)
{
if (ptr_config == config_file)
return 1;
}
/* configuration file not found */
return 0;
}
/*
* Searches for a configuration file.
*/
@@ -67,15 +94,19 @@ struct t_config_file *
config_file_search (const char *name)
{
struct t_config_file *ptr_config;
int rc;
if (!name)
return NULL;
for (ptr_config = config_files; ptr_config;
ptr_config = ptr_config->next_config)
for (ptr_config = last_config_file; ptr_config;
ptr_config = ptr_config->prev_config)
{
if (string_strcasecmp (ptr_config->name, name) == 0)
rc = string_strcasecmp (ptr_config->name, name);
if (rc == 0)
return ptr_config;
else if (rc < 0)
break;
}
/* configuration file not found */
@@ -88,7 +119,7 @@ config_file_search (const char *name)
*/
struct t_config_file *
config_file_config_find_pos (const char *name)
config_file_find_pos (const char *name)
{
struct t_config_file *ptr_config;
@@ -106,6 +137,51 @@ config_file_config_find_pos (const char *name)
return NULL;
}
/*
* Inserts a configuration file (keeping files sorted by name).
*/
void
config_file_config_insert (struct t_config_file *config_file)
{
struct t_config_file *pos_config;
if (!config_file)
return;
if (config_files)
{
pos_config = config_file_find_pos (config_file->name);
if (pos_config)
{
/* insert config into the list (before config found) */
config_file->prev_config = pos_config->prev_config;
config_file->next_config = pos_config;
if (pos_config->prev_config)
(pos_config->prev_config)->next_config = config_file;
else
config_files = config_file;
pos_config->prev_config = config_file;
}
else
{
/* add config to the end */
config_file->prev_config = last_config_file;
config_file->next_config = NULL;
last_config_file->next_config = config_file;
last_config_file = config_file;
}
}
else
{
/* first config */
config_file->prev_config = NULL;
config_file->next_config = NULL;
config_files = config_file;
last_config_file = config_file;
}
}
/*
* Creates a new configuration file.
*
@@ -168,13 +244,7 @@ config_file_new (struct t_weechat_plugin *plugin, const char *name,
new_config_file->sections = NULL;
new_config_file->last_section = NULL;
new_config_file->prev_config = last_config_file;
new_config_file->next_config = NULL;
if (last_config_file)
last_config_file->next_config = new_config_file;
else
config_files = new_config_file;
last_config_file = new_config_file;
config_file_config_insert (new_config_file);
}
return new_config_file;
+1
View File
@@ -169,6 +169,7 @@ struct t_config_option
extern struct t_config_file *config_files;
extern struct t_config_file *last_config_file;
extern int config_file_valid (struct t_config_file *config_file);
extern struct t_config_file *config_file_search (const char *name);
extern struct t_config_file *config_file_new (struct t_weechat_plugin *plugin,
const char *name,
+6 -5
View File
@@ -222,22 +222,23 @@ record_stop ()
* The format of "message" argument is: "prefix message" (prefix and message
* separated by a space).
*
* Returns:
* 1: message has been displayed
* 0: message has NOT been displayed
* Returns index of message displayed (≥ 0), -1 if message has NOT been
* displayed.
*/
int
record_search (const char *buffer, const char *message)
{
char str_message[8192];
int index;
snprintf (str_message, sizeof (str_message),
"%s: \"%s\"",
buffer, message);
return (arraylist_search (recorded_messages, str_message,
NULL, NULL) != NULL);
arraylist_search (recorded_messages, str_message, &index, NULL);
return index;
}
/*
+9 -2
View File
@@ -42,7 +42,7 @@ extern "C"
command_record ("core.weechat", __command);
#define WEE_CHECK_MSG_BUFFER(__buffer_name, __message) \
if (!record_search (__buffer_name, __message)) \
if (record_search (__buffer_name, __message) < 0) \
{ \
char **msg = command_build_error ( \
"Message not displayed on buffer " __buffer_name ": " \
@@ -54,6 +54,8 @@ extern "C"
#define WEE_CHECK_MSG_CORE(__message) \
WEE_CHECK_MSG_BUFFER("core.weechat", __message);
#define WEE_SEARCH_MSG_CORE(__message) \
record_search ("core.weechat", __message)
TEST_GROUP(CoreCommand)
@@ -369,7 +371,12 @@ TEST(CoreCommand, Quit)
TEST(CoreCommand, Reload)
{
/* TODO: write tests */
WEE_CMD_CORE("/save");
WEE_CMD_CORE("/reload");
LONGS_EQUAL(0, WEE_SEARCH_MSG_CORE("Options reloaded from sec.conf"));
LONGS_EQUAL(1, WEE_SEARCH_MSG_CORE("Options reloaded from weechat.conf"));
LONGS_EQUAL(2, WEE_SEARCH_MSG_CORE("Options reloaded from plugins.conf"));
LONGS_EQUAL(3, WEE_SEARCH_MSG_CORE("Options reloaded from charset.conf"));
}
/*
+29 -1
View File
@@ -32,6 +32,7 @@ extern "C"
#include "src/gui/gui-color.h"
#include "src/plugins/plugin.h"
extern struct t_config_file *config_file_find_pos (const char *name);
extern char *config_file_option_full_name (struct t_config_option *option);
extern int config_file_string_boolean_is_valid (const char *text);
extern const char *config_file_option_escape (const char *name);
@@ -41,6 +42,20 @@ TEST_GROUP(CoreConfigFile)
{
};
/*
* Tests functions:
* config_file_valid
*/
TEST(CoreConfigFile, Valid)
{
LONGS_EQUAL(0, config_file_valid (NULL));
LONGS_EQUAL(0, config_file_valid ((struct t_config_file *)0x1));
LONGS_EQUAL(1, config_file_valid (config_file_search ("weechat")));
LONGS_EQUAL(1, config_file_valid (config_file_search ("sec")));
}
/*
* Tests functions:
* config_file_search
@@ -58,10 +73,23 @@ TEST(CoreConfigFile, Search)
/*
* Tests functions:
* config_file_config_find_pos
* config_file_find_pos
*/
TEST(CoreConfigFile, FindPos)
{
POINTERS_EQUAL(NULL, config_file_find_pos (NULL));
POINTERS_EQUAL(config_files, config_file_find_pos (""));
POINTERS_EQUAL(weechat_config_file->next_config, config_file_find_pos ("weechat"));
POINTERS_EQUAL(weechat_config_file->next_config, config_file_find_pos ("WEECHAT"));
}
/*
* Tests functions:
* config_file_config_insert
*/
TEST(CoreConfigFile, ConfigInsert)
{
/* TODO: write tests */
}
+5 -4
View File
@@ -78,7 +78,7 @@ extern char *irc_protocol_cap_to_enable (const char *capabilities,
server_recv (__irc_msg);
#define CHECK_CORE(__message) \
if (!record_search ("core.weechat", __message)) \
if (record_search ("core.weechat", __message) < 0) \
{ \
char **msg = server_build_error ( \
"Core message not displayed", \
@@ -89,7 +89,7 @@ extern char *irc_protocol_cap_to_enable (const char *capabilities,
}
#define CHECK_SRV(__message) \
if (!record_search ("irc.server." IRC_FAKE_SERVER, __message)) \
if (record_search ("irc.server." IRC_FAKE_SERVER, __message) < 0) \
{ \
char **msg = server_build_error ( \
"Server message not displayed", \
@@ -113,7 +113,7 @@ extern char *irc_protocol_cap_to_enable (const char *capabilities,
"(please report to developers): \"" __message "\"");
#define CHECK_CHAN(__message) \
if (!record_search ("irc." IRC_FAKE_SERVER ".#test", __message)) \
if (record_search ("irc." IRC_FAKE_SERVER ".#test", __message) < 0) \
{ \
char **msg = server_build_error ( \
"Channel message not displayed", \
@@ -124,7 +124,8 @@ extern char *irc_protocol_cap_to_enable (const char *capabilities,
}
#define CHECK_PV(__nick, __message) \
if (!record_search ("irc." IRC_FAKE_SERVER "." __nick, __message)) \
if (record_search ("irc." IRC_FAKE_SERVER "." __nick, \
__message) < 0) \
{ \
char **msg = server_build_error ( \
"Private message not displayed", \