1 /*
2  set.c : irssi configuration - change settings in memory
3 
4     Copyright (C) 1999 Timo Sirainen
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #include "module.h"
22 
cache_remove(CONFIG_REC * rec,CONFIG_NODE * node)23 static void cache_remove(CONFIG_REC *rec, CONFIG_NODE *node)
24 {
25 	char *path;
26 
27 	path = g_hash_table_lookup(rec->cache_nodes, node);
28 	if (path != NULL) {
29 		g_hash_table_remove(rec->cache, path);
30 		g_hash_table_remove(rec->cache_nodes, node);
31                 g_free(path);
32 	}
33 }
34 
config_node_remove(CONFIG_REC * rec,CONFIG_NODE * parent,CONFIG_NODE * node)35 void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node)
36 {
37 	g_return_if_fail(node != NULL);
38 
39 	if (parent == NULL)
40                 parent = rec->mainnode;
41 
42 	rec->modifycounter++;
43 	cache_remove(rec, node);
44 	parent->value = g_slist_remove(parent->value, node);
45 
46 	switch (node->type) {
47 	case NODE_TYPE_KEY:
48 	case NODE_TYPE_VALUE:
49 	case NODE_TYPE_COMMENT:
50 		g_free_not_null(node->value);
51 		break;
52 	case NODE_TYPE_BLOCK:
53 	case NODE_TYPE_LIST:
54 		while (node->value != NULL)
55 			config_node_remove(rec, node, ((GSList *) node->value)->data);
56 		break;
57 	}
58 	g_free_not_null(node->key);
59         g_free(node);
60 }
61 
config_node_list_remove(CONFIG_REC * rec,CONFIG_NODE * node,int index)62 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index)
63 {
64 	CONFIG_NODE *child;
65 
66 	g_return_if_fail(node != NULL);
67 	g_return_if_fail(is_node_list(node));
68 
69 	child = config_node_nth(node, index);
70 	if (child != NULL) config_node_remove(rec, node, child);
71 }
72 
config_node_clear(CONFIG_REC * rec,CONFIG_NODE * node)73 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node)
74 {
75 	g_return_if_fail(node != NULL);
76 	g_return_if_fail(is_node_list(node));
77 
78 	while (node->value != NULL)
79                 config_node_remove(rec, node, ((GSList *) node->value)->data);
80 }
81 
config_nodes_remove_all(CONFIG_REC * rec)82 void config_nodes_remove_all(CONFIG_REC *rec)
83 {
84 	g_return_if_fail(rec != NULL);
85 	g_return_if_fail(is_node_list(rec->mainnode));
86 
87 	while (rec->mainnode->value != NULL)
88 		config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data);
89 }
90 
config_node_set_str(CONFIG_REC * rec,CONFIG_NODE * parent,const char * key,const char * value)91 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value)
92 {
93 	CONFIG_NODE *node;
94 	int no_key;
95 
96 	g_return_if_fail(rec != NULL);
97 	g_return_if_fail(parent != NULL);
98 	g_return_if_fail(is_node_list(parent));
99 
100 	no_key = key == NULL;
101 	node = no_key ? NULL : config_node_find(parent, key);
102 
103 	if (value == NULL) {
104                 /* remove the key */
105 		if (node != NULL) config_node_remove(rec, parent, node);
106 		return;
107 	}
108 
109 	if (node != NULL && !has_node_value(node)) {
110 		g_critical("Expected scalar node at `..%s/%s' was of complex type. Corrupt config?",
111 				   parent->key, key);
112 		config_node_remove(rec, parent, node);
113 		node = NULL;
114 	}
115 	if (node != NULL) {
116 		if (g_strcmp0(node->value, value) == 0)
117 			return;
118                 g_free(node->value);
119 	} else {
120 		node = g_new0(CONFIG_NODE, 1);
121 		parent->value = g_slist_append(parent->value, node);
122 
123 		node->type = no_key ? NODE_TYPE_VALUE : NODE_TYPE_KEY;
124 		node->key = no_key ? NULL : g_strdup(key);
125 	}
126 
127 	node->value = g_strdup(value);
128 	rec->modifycounter++;
129 }
130 
config_node_set_int(CONFIG_REC * rec,CONFIG_NODE * parent,const char * key,int value)131 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
132 {
133 	char str[MAX_INT_STRLEN];
134 
135 	g_snprintf(str, sizeof(str), "%d", value);
136 	config_node_set_str(rec, parent, key, str);
137 }
138 
config_node_set_bool(CONFIG_REC * rec,CONFIG_NODE * parent,const char * key,int value)139 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value)
140 {
141 	config_node_set_str(rec, parent, key, value ? "yes" : "no");
142 }
143 
config_set_str(CONFIG_REC * rec,const char * section,const char * key,const char * value)144 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value)
145 {
146 	CONFIG_NODE *parent;
147 
148 	g_return_val_if_fail(rec != NULL, -1);
149 
150 	parent = config_node_traverse(rec, section, TRUE);
151 	if (parent == NULL) return -1;
152 
153 	config_node_set_str(rec, parent, key, value);
154 	return 0;
155 }
156 
config_set_int(CONFIG_REC * rec,const char * section,const char * key,int value)157 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value)
158 {
159 	char str[MAX_INT_STRLEN];
160 
161 	g_snprintf(str, sizeof(str), "%d", value);
162 	return config_set_str(rec, section, key, str);
163 }
164 
config_set_bool(CONFIG_REC * rec,const char * section,const char * key,int value)165 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value)
166 {
167 	return config_set_str(rec, section, key, value ? "yes" : "no");
168 }
169 
config_node_add_list(CONFIG_REC * rec,CONFIG_NODE * node,char ** array)170 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array)
171 {
172 	char **tmp;
173 
174 	for (tmp = array; *tmp != NULL; tmp++)
175                 config_node_set_str(rec, node, NULL, *tmp);
176 }
177