2000-04-14 07:27:14 -04:00
|
|
|
/*
|
|
|
|
get.c : irssi configuration - get settings from memory
|
|
|
|
|
|
|
|
Copyright (C) 1999 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-04-14 07:27:14 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
|
|
|
|
CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(node != NULL, NULL);
|
|
|
|
g_return_val_if_fail(key != NULL, NULL);
|
|
|
|
g_return_val_if_fail(is_node_list(node), NULL);
|
|
|
|
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CONFIG_NODE *node = tmp->data;
|
|
|
|
|
2014-06-10 12:06:19 -04:00
|
|
|
if (node->key != NULL && g_ascii_strcasecmp(node->key, key) == 0)
|
2000-04-14 07:27:14 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-06 21:40:34 -05:00
|
|
|
CONFIG_NODE *config_node_section(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int new_type)
|
2002-02-15 17:18:35 -05:00
|
|
|
{
|
2015-01-06 21:40:34 -05:00
|
|
|
return config_node_section_index(rec, parent, key, -1, new_type);
|
2002-02-15 17:18:35 -05:00
|
|
|
}
|
|
|
|
|
2015-01-06 21:40:34 -05:00
|
|
|
CONFIG_NODE *config_node_section_index(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key,
|
2002-02-15 17:18:35 -05:00
|
|
|
int index, int new_type)
|
2000-04-14 07:27:14 -04:00
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
2002-02-15 17:18:35 -05:00
|
|
|
int nindex;
|
2000-04-14 07:27:14 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(parent != NULL, NULL);
|
|
|
|
g_return_val_if_fail(is_node_list(parent), NULL);
|
|
|
|
|
|
|
|
node = key == NULL ? NULL : config_node_find(parent, key);
|
|
|
|
if (node != NULL) {
|
2002-02-15 17:18:35 -05:00
|
|
|
nindex = g_slist_index(parent->value, node);
|
|
|
|
if (index >= 0 && nindex != index &&
|
|
|
|
nindex <= g_slist_length(parent->value)) {
|
|
|
|
/* move it to wanted position */
|
|
|
|
parent->value = g_slist_remove(parent->value, node);
|
|
|
|
parent->value = g_slist_insert(parent->value, node, index);
|
|
|
|
}
|
2015-02-17 03:46:52 -05:00
|
|
|
if (!is_node_list(node)) {
|
|
|
|
int show_error = 0;
|
|
|
|
|
|
|
|
if (new_type != -1) {
|
|
|
|
config_node_remove(rec, parent, node);
|
|
|
|
node = NULL;
|
|
|
|
show_error = 1;
|
2015-04-19 07:14:54 -04:00
|
|
|
} else if (!g_hash_table_lookup_extended(rec->cache_nodes, node, NULL, NULL)) {
|
2015-02-17 03:46:52 -05:00
|
|
|
g_hash_table_insert(rec->cache_nodes, node, NULL);
|
|
|
|
show_error = 1;
|
|
|
|
}
|
|
|
|
if (show_error)
|
|
|
|
g_critical("Expected %s node at `..%s/%s' was of scalar type. Corrupt config?",
|
|
|
|
new_type == NODE_TYPE_LIST ? "list" : new_type == NODE_TYPE_BLOCK ? "block" : "section",
|
|
|
|
parent->key, key);
|
|
|
|
} else {
|
|
|
|
g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
|
|
|
|
return node;
|
|
|
|
}
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_type == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
node = g_new0(CONFIG_NODE, 1);
|
2002-02-15 17:18:35 -05:00
|
|
|
parent->value = index < 0 ? g_slist_append(parent->value, node) :
|
|
|
|
g_slist_insert(parent->value, node, index);
|
2000-04-14 07:27:14 -04:00
|
|
|
|
|
|
|
node->type = new_type;
|
|
|
|
node->key = key == NULL ? NULL : g_strdup(key);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
2000-05-10 09:57:42 -04:00
|
|
|
char **list, **tmp, *str;
|
2000-04-14 07:27:14 -04:00
|
|
|
int is_list, new_type;
|
|
|
|
|
|
|
|
g_return_val_if_fail(rec != NULL, NULL);
|
|
|
|
|
|
|
|
if (section == NULL || *section == '\0')
|
|
|
|
return rec->mainnode;
|
|
|
|
|
|
|
|
/* check if it already exists in cache */
|
|
|
|
node = g_hash_table_lookup(rec->cache, section);
|
2015-02-17 03:46:52 -05:00
|
|
|
if (node != NULL) {
|
|
|
|
if (create) {
|
|
|
|
const char *path = strrchr(section, '/');
|
|
|
|
if (path == NULL) path = section;
|
|
|
|
else path++;
|
|
|
|
new_type = *path == '(' ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
|
|
|
|
if (node->type != new_type) {
|
|
|
|
g_critical("Expected %s node at `%s' was of %s type. Corrupt config?",
|
|
|
|
new_type == NODE_TYPE_LIST ? "list" : "block", section,
|
|
|
|
node->type == NODE_TYPE_LIST ? "list" : "block");
|
|
|
|
node->type = new_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2000-04-14 07:27:14 -04:00
|
|
|
|
|
|
|
new_type = -1;
|
|
|
|
|
|
|
|
node = rec->mainnode;
|
|
|
|
list = g_strsplit(section, "/", -1);
|
|
|
|
for (tmp = list; *tmp != NULL; tmp++) {
|
|
|
|
is_list = **tmp == '(';
|
2015-02-17 03:46:52 -05:00
|
|
|
if (create) {
|
|
|
|
CONFIG_NODE *tmpnode;
|
|
|
|
|
|
|
|
new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
|
|
|
|
tmpnode = config_node_find(node, *tmp + is_list);
|
|
|
|
if (tmpnode != NULL && tmpnode->type != new_type) {
|
|
|
|
g_critical("Expected %s node at `%s' was of scalar type. Corrupt config?", is_list ? "list" : "block", section);
|
|
|
|
config_node_remove(rec, node, tmpnode);
|
|
|
|
}
|
|
|
|
}
|
2000-04-14 07:27:14 -04:00
|
|
|
|
2015-01-06 21:40:34 -05:00
|
|
|
node = config_node_section(rec, node, *tmp + is_list, new_type);
|
2000-08-15 20:48:51 -04:00
|
|
|
if (node == NULL) {
|
|
|
|
g_strfreev(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
g_strfreev(list);
|
|
|
|
|
2015-02-17 03:41:26 -05:00
|
|
|
if (!is_node_list(node)) {
|
2014-12-16 07:14:23 -05:00
|
|
|
/* Will die. Better to not corrupt the config further in this case. */
|
2015-02-17 03:41:26 -05:00
|
|
|
g_critical("Attempt to use non-list node `%s' as list. Corrupt config?", section);
|
2014-12-16 07:14:23 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-04-14 07:27:14 -04:00
|
|
|
/* save to cache */
|
2000-05-10 09:57:42 -04:00
|
|
|
str = g_strdup(section);
|
|
|
|
g_hash_table_insert(rec->cache, str, node);
|
|
|
|
g_hash_table_insert(rec->cache_nodes, node, str);
|
2000-04-14 07:27:14 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *parent, *node;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
g_return_val_if_fail(rec != NULL, (char *) def);
|
|
|
|
g_return_val_if_fail(key != NULL, (char *) def);
|
|
|
|
|
|
|
|
/* check if it already exists in cache */
|
2000-04-14 08:49:02 -04:00
|
|
|
path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
|
2000-04-14 07:27:14 -04:00
|
|
|
node = g_hash_table_lookup(rec->cache, path);
|
|
|
|
|
|
|
|
if (node != NULL)
|
|
|
|
g_free(path);
|
|
|
|
else {
|
|
|
|
parent = config_node_traverse(rec, section, FALSE);
|
|
|
|
node = parent == NULL ? NULL :
|
|
|
|
config_node_find(parent, key);
|
|
|
|
|
|
|
|
/* save to cache */
|
2000-05-10 09:57:42 -04:00
|
|
|
if (node == NULL)
|
|
|
|
g_free(path);
|
|
|
|
else {
|
2000-04-14 07:27:14 -04:00
|
|
|
g_hash_table_insert(rec->cache, path, node);
|
2000-05-10 09:57:42 -04:00
|
|
|
g_hash_table_insert(rec->cache_nodes, node, path);
|
|
|
|
}
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = config_get_str(rec, section, key, NULL);
|
|
|
|
if (str == NULL) return def;
|
|
|
|
|
|
|
|
return atoi(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = config_get_str(rec, section, key, NULL);
|
|
|
|
if (str == NULL) return def;
|
|
|
|
|
2002-01-27 15:45:59 -05:00
|
|
|
return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
|
2000-06-06 15:14:51 -04:00
|
|
|
if (parent == NULL) return (char *) def;
|
|
|
|
|
2000-04-14 07:27:14 -04:00
|
|
|
node = config_node_find(parent, key);
|
2000-11-23 16:40:07 -05:00
|
|
|
return (char *) ((node != NULL && has_node_value(node)) ?
|
|
|
|
node->value : def);
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = config_node_get_str(parent, key, NULL);
|
|
|
|
if (str == NULL) return def;
|
|
|
|
|
|
|
|
return atoi(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = config_node_get_str(parent, key, NULL);
|
|
|
|
if (str == NULL) return def;
|
|
|
|
|
2002-01-27 15:45:59 -05:00
|
|
|
return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
|
|
|
|
(i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
|
2000-04-14 07:27:14 -04:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:10:09 -04:00
|
|
|
char **config_node_get_list(CONFIG_NODE *node)
|
|
|
|
{
|
|
|
|
GString *values;
|
|
|
|
GSList *tmp;
|
|
|
|
char **ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(node != NULL, NULL);
|
|
|
|
g_return_val_if_fail(is_node_list(node), NULL);
|
|
|
|
|
|
|
|
/* put values to string */
|
|
|
|
values = g_string_new(NULL);
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type == NODE_TYPE_VALUE)
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(values, "%s ", (char *) node->value);
|
2000-04-26 04:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* split the values to **str array */
|
|
|
|
if (values->len == 0)
|
|
|
|
ret = NULL;
|
|
|
|
else {
|
|
|
|
g_string_truncate(values, values->len-1);
|
|
|
|
ret = g_strsplit(values->str, " ", -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(values, TRUE);
|
|
|
|
return ret;
|
|
|
|
}
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2002-02-15 17:18:35 -05:00
|
|
|
CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(node != NULL, NULL);
|
|
|
|
g_return_val_if_fail(is_node_list(node), NULL);
|
|
|
|
|
2002-10-25 09:57:16 -04:00
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CONFIG_NODE *node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_COMMENT) {
|
|
|
|
if (index == 0)
|
|
|
|
return node;
|
|
|
|
index--;
|
|
|
|
}
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-22 10:53:54 -04:00
|
|
|
|
2002-02-15 17:18:35 -05:00
|
|
|
int config_node_index(CONFIG_NODE *parent, const char *key)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
2002-10-25 09:57:16 -04:00
|
|
|
GSList *tmp;
|
|
|
|
int index;
|
2002-02-15 17:18:35 -05:00
|
|
|
|
|
|
|
g_return_val_if_fail(parent != NULL, -1);
|
|
|
|
g_return_val_if_fail(key != NULL, -1);
|
|
|
|
|
|
|
|
node = config_node_find(parent, key);
|
|
|
|
if (node == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2002-10-25 09:57:16 -04:00
|
|
|
index = 0;
|
|
|
|
for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CONFIG_NODE *tmpnode = tmp->data;
|
|
|
|
|
|
|
|
if (tmpnode == node)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
if (tmpnode->type != NODE_TYPE_COMMENT)
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2002-02-15 17:18:35 -05:00
|
|
|
}
|
|
|
|
|
2001-09-22 11:24:40 -04:00
|
|
|
GSList *config_node_first(GSList *list)
|
2001-09-22 10:53:54 -04:00
|
|
|
{
|
|
|
|
while (list != NULL) {
|
|
|
|
CONFIG_NODE *node = list->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_COMMENT)
|
|
|
|
break;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2001-09-22 11:24:40 -04:00
|
|
|
|
|
|
|
GSList *config_node_next(GSList *list)
|
|
|
|
{
|
|
|
|
list = list->next;
|
|
|
|
return config_node_first(list);
|
|
|
|
}
|