1 /*
2 get.c : irssi configuration - get settings from 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
config_node_find(CONFIG_NODE * node,const char * key)23 CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
24 {
25 GSList *tmp;
26
27 g_return_val_if_fail(node != NULL, NULL);
28 g_return_val_if_fail(key != NULL, NULL);
29 g_return_val_if_fail(is_node_list(node), NULL);
30
31 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
32 CONFIG_NODE *node = tmp->data;
33
34 if (node->key != NULL && g_ascii_strcasecmp(node->key, key) == 0)
35 return node;
36 }
37
38 return NULL;
39 }
40
config_node_section(CONFIG_REC * rec,CONFIG_NODE * parent,const char * key,int new_type)41 CONFIG_NODE *config_node_section(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int new_type)
42 {
43 return config_node_section_index(rec, parent, key, -1, new_type);
44 }
45
config_node_section_index(CONFIG_REC * rec,CONFIG_NODE * parent,const char * key,int index,int new_type)46 CONFIG_NODE *config_node_section_index(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key,
47 int index, int new_type)
48 {
49 CONFIG_NODE *node;
50 int nindex;
51
52 g_return_val_if_fail(parent != NULL, NULL);
53 g_return_val_if_fail(is_node_list(parent), NULL);
54
55 node = key == NULL ? NULL : config_node_find(parent, key);
56 if (node != NULL) {
57 nindex = g_slist_index(parent->value, node);
58 if (index >= 0 && nindex != index &&
59 nindex <= g_slist_length(parent->value)) {
60 /* move it to wanted position */
61 parent->value = g_slist_remove(parent->value, node);
62 parent->value = g_slist_insert(parent->value, node, index);
63 }
64 if (!is_node_list(node)) {
65 int error = 0;
66
67 if (new_type != -1) {
68 config_node_remove(rec, parent, node);
69 node = NULL;
70 error = 1;
71 } else if (!g_hash_table_lookup_extended(rec->cache_nodes, node, NULL, NULL)) {
72 g_hash_table_insert(rec->cache_nodes, node, NULL);
73 error = 1;
74 }
75 if (error)
76 g_critical("Expected %s node at `%s%s/%s' was of scalar type. Corrupt config?",
77 new_type == NODE_TYPE_LIST ? "list" : new_type == NODE_TYPE_BLOCK ? "block" : "section",
78 parent == rec->mainnode ? "" : "..",
79 parent->key == NULL ? "" : parent->key, key);
80 } else {
81 g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
82 return node;
83 }
84 }
85
86 if (new_type == -1)
87 return NULL;
88
89 node = g_new0(CONFIG_NODE, 1);
90 parent->value = index < 0 ? g_slist_append(parent->value, node) :
91 g_slist_insert(parent->value, node, index);
92
93 node->type = new_type;
94 node->key = key == NULL ? NULL : g_strdup(key);
95
96 return node;
97 }
98
config_node_traverse(CONFIG_REC * rec,const char * section,int create)99 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
100 {
101 CONFIG_NODE *node;
102 char **list, **tmp, *str;
103 int is_list, new_type;
104
105 g_return_val_if_fail(rec != NULL, NULL);
106
107 if (section == NULL || *section == '\0')
108 return rec->mainnode;
109
110 /* check if it already exists in cache */
111 node = g_hash_table_lookup(rec->cache, section);
112 if (node != NULL) {
113 if (create) {
114 const char *path = strrchr(section, '/');
115 if (path == NULL) path = section;
116 else path++;
117 new_type = *path == '(' ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
118 if (node->type != new_type) {
119 g_critical("Expected %s node at `%s' was of %s type. Corrupt config?",
120 new_type == NODE_TYPE_LIST ? "list" : "block", section,
121 node->type == NODE_TYPE_LIST ? "list" : "block");
122 node->type = new_type;
123 }
124 }
125 return node;
126 }
127
128 new_type = -1;
129
130 node = rec->mainnode;
131 list = g_strsplit(section, "/", -1);
132 for (tmp = list; *tmp != NULL; tmp++) {
133 is_list = **tmp == '(';
134 if (create) {
135 CONFIG_NODE *tmpnode;
136
137 new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
138 tmpnode = config_node_find(node, *tmp + is_list);
139 if (tmpnode != NULL && tmpnode->type != new_type) {
140 g_critical("Expected %s node at `%s' was of scalar type. Corrupt config?", is_list ? "list" : "block", section);
141 config_node_remove(rec, node, tmpnode);
142 }
143 }
144
145 node = config_node_section(rec, node, *tmp + is_list, new_type);
146 if (node == NULL) {
147 g_strfreev(list);
148 return NULL;
149 }
150 }
151 g_strfreev(list);
152
153 if (!is_node_list(node)) {
154 /* Will die. Better to not corrupt the config further in this case. */
155 g_critical("Attempt to use non-list node `%s' as list. Corrupt config?", section);
156 return NULL;
157 }
158
159 /* save to cache */
160 str = g_strdup(section);
161 g_hash_table_insert(rec->cache, str, node);
162 g_hash_table_insert(rec->cache_nodes, node, str);
163 return node;
164 }
165
config_get_str(CONFIG_REC * rec,const char * section,const char * key,const char * def)166 char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
167 {
168 CONFIG_NODE *parent, *node;
169 char *path;
170
171 g_return_val_if_fail(rec != NULL, (char *) def);
172 g_return_val_if_fail(key != NULL, (char *) def);
173
174 /* check if it already exists in cache */
175 path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
176 node = g_hash_table_lookup(rec->cache, path);
177
178 if (node != NULL)
179 g_free(path);
180 else {
181 parent = config_node_traverse(rec, section, FALSE);
182 node = parent == NULL ? NULL :
183 config_node_find(parent, key);
184
185 /* save to cache */
186 if (node == NULL)
187 g_free(path);
188 else {
189 g_hash_table_insert(rec->cache, path, node);
190 g_hash_table_insert(rec->cache_nodes, node, path);
191 }
192 }
193
194 return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
195 }
196
config_get_int(CONFIG_REC * rec,const char * section,const char * key,int def)197 int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
198 {
199 char *str;
200
201 str = config_get_str(rec, section, key, NULL);
202 if (str == NULL) return def;
203
204 return atoi(str);
205 }
206
config_get_bool(CONFIG_REC * rec,const char * section,const char * key,int def)207 int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
208 {
209 char *str;
210
211 str = config_get_str(rec, section, key, NULL);
212 if (str == NULL) return def;
213
214 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y';
215 }
216
config_node_get_str(CONFIG_NODE * parent,const char * key,const char * def)217 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
218 {
219 CONFIG_NODE *node;
220
221 if (parent == NULL) return (char *) def;
222
223 node = config_node_find(parent, key);
224 return (char *) ((node != NULL && has_node_value(node)) ?
225 node->value : def);
226 }
227
config_node_get_int(CONFIG_NODE * parent,const char * key,int def)228 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
229 {
230 char *str;
231
232 str = config_node_get_str(parent, key, NULL);
233 if (str == NULL) return def;
234
235 return atoi(str);
236 }
237
config_node_get_bool(CONFIG_NODE * parent,const char * key,int def)238 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
239 {
240 char *str;
241
242 str = config_node_get_str(parent, key, NULL);
243 if (str == NULL) return def;
244
245 return i_toupper(*str) == 'T' || i_toupper(*str) == 'Y' ||
246 (i_toupper(*str) == 'O' && i_toupper(str[1]) == 'N');
247 }
248
config_node_get_list(CONFIG_NODE * node)249 char **config_node_get_list(CONFIG_NODE *node)
250 {
251 GString *values;
252 GSList *tmp;
253 char **ret;
254
255 g_return_val_if_fail(node != NULL, NULL);
256 g_return_val_if_fail(is_node_list(node), NULL);
257
258 /* put values to string */
259 values = g_string_new(NULL);
260 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
261 node = tmp->data;
262
263 if (node->type == NODE_TYPE_VALUE)
264 g_string_append_printf(values, "%s ", (char *) node->value);
265 }
266
267 /* split the values to **str array */
268 if (values->len == 0)
269 ret = NULL;
270 else {
271 g_string_truncate(values, values->len-1);
272 ret = g_strsplit(values->str, " ", -1);
273 }
274
275 g_string_free(values, TRUE);
276 return ret;
277 }
278
config_node_nth(CONFIG_NODE * node,int index)279 CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index)
280 {
281 GSList *tmp;
282
283 g_return_val_if_fail(node != NULL, NULL);
284 g_return_val_if_fail(is_node_list(node), NULL);
285
286 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
287 CONFIG_NODE *node = tmp->data;
288
289 if (node->type != NODE_TYPE_COMMENT) {
290 if (index == 0)
291 return node;
292 index--;
293 }
294 }
295
296 return NULL;
297 }
298
config_node_index(CONFIG_NODE * parent,const char * key)299 int config_node_index(CONFIG_NODE *parent, const char *key)
300 {
301 CONFIG_NODE *node;
302 GSList *tmp;
303 int index;
304
305 g_return_val_if_fail(parent != NULL, -1);
306 g_return_val_if_fail(key != NULL, -1);
307
308 node = config_node_find(parent, key);
309 if (node == NULL)
310 return -1;
311
312 index = 0;
313 for (tmp = parent->value; tmp != NULL; tmp = tmp->next) {
314 CONFIG_NODE *tmpnode = tmp->data;
315
316 if (tmpnode == node)
317 return index;
318
319 if (tmpnode->type != NODE_TYPE_COMMENT)
320 index++;
321 }
322
323 return -1;
324 }
325
config_node_first(GSList * list)326 GSList *config_node_first(GSList *list)
327 {
328 while (list != NULL) {
329 CONFIG_NODE *node = list->data;
330
331 if (node->type != NODE_TYPE_COMMENT)
332 break;
333 list = list->next;
334 }
335 return list;
336 }
337
config_node_next(GSList * list)338 GSList *config_node_next(GSList *list)
339 {
340 list = list->next;
341 return config_node_first(list);
342 }
343