]> Dogcows Code - chaz/openbox/blob - obt/parse.c
68b1b87f8ee4ffee3fb6bad46ecd0985f7b7d859
[chaz/openbox] / obt / parse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/parse.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/parse.h"
20
21 #include <glib.h>
22
23 #ifdef HAVE_STRING_H
24 # include <string.h>
25 #endif
26 #ifdef HAVE_ERRNO_H
27 # include <errno.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 static gboolean xdg_start;
40 static gchar *xdg_config_home_path;
41 static gchar *xdg_data_home_path;
42 static GSList *xdg_config_dir_paths;
43 static GSList *xdg_data_dir_paths;
44
45 struct Callback {
46 gchar *tag;
47 ObtParseCallback func;
48 gpointer data;
49 };
50
51 struct _ObtParseInst {
52 gint ref;
53 GHashTable *callbacks;
54 xmlDocPtr doc;
55 xmlNodePtr root;
56 gchar *path;
57 };
58
59 static void destfunc(struct Callback *c)
60 {
61 g_free(c->tag);
62 g_free(c);
63 }
64
65 ObtParseInst* obt_parse_instance_new()
66 {
67 ObtParseInst *i = g_new(ObtParseInst, 1);
68 i->ref = 1;
69 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify)destfunc);
71 i->doc = NULL;
72 i->root = NULL;
73 i->path = NULL;
74 return i;
75 }
76
77 void obt_parse_instance_ref(ObtParseInst *i)
78 {
79 ++i->ref;
80 }
81
82 void obt_parse_instance_unref(ObtParseInst *i)
83 {
84 if (i && --i->ref == 0) {
85 g_hash_table_destroy(i->callbacks);
86 g_free(i);
87 }
88 }
89
90 void parse_register(ObtParseInst *i, const gchar *tag,
91 ObtParseCallback func, gpointer data)
92 {
93 struct Callback *c;
94
95 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
96 g_error("Tag '%s' already registered", tag);
97 return;
98 }
99
100 c = g_new(struct Callback, 1);
101 c->tag = g_strdup(tag);
102 c->func = func;
103 c->data = data;
104 g_hash_table_insert(i->callbacks, c->tag, c);
105 }
106
107 static gboolean load_file(ObtParseInst *i,
108 const gchar *domain,
109 const gchar *filename,
110 const gchar *root_node,
111 GSList *paths)
112 {
113 GSList *it;
114 gboolean r = FALSE;
115
116 g_assert(i->doc == NULL); /* another doc isn't open already? */
117
118 for (it = paths; !r && it; it = g_slist_next(it)) {
119 gchar *path;
120 struct stat s;
121
122 path = g_build_filename(it->data, domain, filename, NULL);
123
124 if (stat(path, &s) >= 0) {
125 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
126 with extra nodes in it. */
127 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
128 XML_PARSE_RECOVER));
129 if (i->doc) {
130 i->root = xmlDocGetRootElement(i->doc);
131 if (!i->root) {
132 xmlFreeDoc(i->doc);
133 i->doc = NULL;
134 g_message("%s is an empty XML document", path);
135 }
136 else if (xmlStrcmp(i->root->name,
137 (const xmlChar*)root_node)) {
138 xmlFreeDoc(i->doc);
139 i->doc = NULL;
140 i->root = NULL;
141 g_message("XML document %s is of wrong type. Root "
142 "node is not '%s'", path, root_node);
143 }
144 else {
145 i->path = g_strdup(path);
146 r = TRUE; /* ok! */
147 }
148 }
149 }
150
151 g_free(path);
152 }
153
154 return r;
155 }
156
157 gboolean obt_parse_load_config_file(ObtParseInst *i,
158 const gchar *domain,
159 const gchar *filename,
160 const gchar *root_node)
161 {
162 GSList *it, *paths = NULL;
163 gboolean r;
164
165 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
166 paths = g_slist_append(paths, g_strdup(it->data));
167
168 r = load_file(i, domain, filename, root_node, paths);
169
170 while (paths) {
171 g_free(paths->data);
172 paths = g_slist_delete_link(paths, paths);
173 }
174 return r;
175 }
176
177 gboolean obt_parse_load_data_file(ObtParseInst *i,
178 const gchar *domain,
179 const gchar *filename,
180 const gchar *root_node)
181 {
182 GSList *it, *paths = NULL;
183 gboolean r;
184
185 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
186 paths = g_slist_append(paths, g_strdup(it->data));
187
188 r = load_file(i, domain, filename, root_node, paths);
189
190 while (paths) {
191 g_free(paths->data);
192 paths = g_slist_delete_link(paths, paths);
193 }
194 return r;
195 }
196
197 gboolean obt_parse_load_theme_file(ObtParseInst *i,
198 const gchar *theme,
199 const gchar *domain,
200 const gchar *filename,
201 const gchar *root_node)
202 {
203 GSList *it, *paths = NULL;
204 gboolean r;
205
206 /* use ~/.themes for backwards compatibility */
207 paths = g_slist_append
208 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
209
210 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
211 paths = g_slist_append
212 (paths, g_build_filename(it->data, "themes", theme, NULL));
213
214 r = load_file(i, domain, filename, root_node, paths);
215
216 while (paths) {
217 g_free(paths->data);
218 paths = g_slist_delete_link(paths, paths);
219 }
220 return r;
221 }
222
223
224 gboolean obt_parse_load_mem(ObtParseInst *i,
225 gpointer data, guint len, const gchar *root_node)
226 {
227 gboolean r = FALSE;
228
229 g_assert(i->doc == NULL); /* another doc isn't open already? */
230
231 i->doc = xmlParseMemory(data, len);
232 if (i) {
233 i->root = xmlDocGetRootElement(i->doc);
234 if (!i->root) {
235 xmlFreeDoc(i->doc);
236 i->doc = NULL;
237 g_message("Given memory is an empty document");
238 }
239 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
240 xmlFreeDoc(i->doc);
241 i->doc = NULL;
242 i->root = NULL;
243 g_message("XML Document in given memory is of wrong "
244 "type. Root node is not '%s'\n", root_node);
245 }
246 else
247 r = TRUE; /* ok ! */
248 }
249 return r;
250 }
251
252 void obt_parse_close(ObtParseInst *i)
253 {
254 if (i && i->doc) {
255 xmlFreeDoc(i->doc);
256 g_free(i->path);
257 i->doc = NULL;
258 i->root = NULL;
259 i->path = NULL;
260 }
261 }
262
263 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
264 {
265 g_assert(i->doc); /* a doc is open? */
266
267 while (node) {
268 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
269 if (c) c->func(i, i->doc, node, c->data);
270 node = node->next;
271 }
272 }
273
274 gchar *obt_parse_node_string(xmlNodePtr node)
275 {
276 xmlChar *c = xmlNodeGetContent(node);
277 gchar *s = g_strdup(c ? (gchar*)c : "");
278 xmlFree(c);
279 return s;
280 }
281
282 gint obt_parse_node_int(xmlNodePtr node)
283 {
284 xmlChar *c = xmlNodeGetContent(node);
285 gint i = c ? atoi((gchar*)c) : 0;
286 xmlFree(c);
287 return i;
288 }
289
290 gboolean obt_parse_node_bool(xmlNodePtr node)
291 {
292 xmlChar *c = xmlNodeGetContent(node);
293 gboolean b = FALSE;
294 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
295 b = TRUE;
296 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
297 b = TRUE;
298 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
299 b = TRUE;
300 xmlFree(c);
301 return b;
302 }
303
304 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
305 {
306 xmlChar *c = xmlNodeGetContent(node);
307 gboolean r;
308 r = !xmlStrcasecmp(c, (const xmlChar*) val);
309 xmlFree(c);
310 return r;
311 }
312
313 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
314 {
315 while (node) {
316 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
317 return node;
318 node = node->next;
319 }
320 return NULL;
321 }
322
323 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
324 gboolean *value)
325 {
326 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
327 gboolean r = FALSE;
328 if (c) {
329 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
330 *value = TRUE, r = TRUE;
331 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
332 *value = TRUE, r = TRUE;
333 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
334 *value = TRUE, r = TRUE;
335 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
336 *value = FALSE, r = TRUE;
337 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
338 *value = FALSE, r = TRUE;
339 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
340 *value = FALSE, r = TRUE;
341 }
342 xmlFree(c);
343 return r;
344 }
345
346 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
347 {
348 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
349 gboolean r = FALSE;
350 if (c) {
351 *value = atoi((gchar*)c);
352 r = TRUE;
353 }
354 xmlFree(c);
355 return r;
356 }
357
358 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
359 gchar **value)
360 {
361 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
362 gboolean r = FALSE;
363 if (c) {
364 *value = g_strdup((gchar*)c);
365 r = TRUE;
366 }
367 xmlFree(c);
368 return r;
369 }
370
371 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
372 const gchar *val)
373 {
374 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
375 gboolean r = FALSE;
376 if (c)
377 r = !xmlStrcasecmp(c, (const xmlChar*) val);
378 xmlFree(c);
379 return r;
380 }
381
382 static gint slist_path_cmp(const gchar *a, const gchar *b)
383 {
384 return strcmp(a, b);
385 }
386
387 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
388
389 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
390 {
391 g_assert(func);
392
393 if (!data)
394 return list;
395
396 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
397 list = func(list, data);
398 else
399 g_free(data);
400
401 return list;
402 }
403
404 static GSList* split_paths(const gchar *paths)
405 {
406 GSList *list = NULL;
407 gchar **spl, **it;
408
409 if (!paths)
410 return NULL;
411 spl = g_strsplit(paths, ":", -1);
412 for (it = spl; *it; ++it)
413 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
414 g_free(spl);
415 return list;
416 }
417
418 void parse_paths_startup()
419 {
420 const gchar *path;
421
422 if (xdg_start)
423 return;
424 xdg_start = TRUE;
425
426 path = g_getenv("XDG_CONFIG_HOME");
427 if (path && path[0] != '\0') /* not unset or empty */
428 xdg_config_home_path = g_build_filename(path, NULL);
429 else
430 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
431 NULL);
432
433 path = g_getenv("XDG_DATA_HOME");
434 if (path && path[0] != '\0') /* not unset or empty */
435 xdg_data_home_path = g_build_filename(path, NULL);
436 else
437 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
438 "share", NULL);
439
440 path = g_getenv("XDG_CONFIG_DIRS");
441 if (path && path[0] != '\0') /* not unset or empty */
442 xdg_config_dir_paths = split_paths(path);
443 else {
444 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
445 g_strdup(CONFIGDIR),
446 (GSListFunc) g_slist_append);
447 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
448 g_build_filename
449 (G_DIR_SEPARATOR_S,
450 "etc", "xdg", NULL),
451 (GSListFunc) g_slist_append);
452 }
453 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
454 g_strdup(xdg_config_home_path),
455 (GSListFunc) g_slist_prepend);
456
457 path = g_getenv("XDG_DATA_DIRS");
458 if (path && path[0] != '\0') /* not unset or empty */
459 xdg_data_dir_paths = split_paths(path);
460 else {
461 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
462 g_strdup(DATADIR),
463 (GSListFunc) g_slist_append);
464 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
465 g_build_filename
466 (G_DIR_SEPARATOR_S,
467 "usr", "local", "share", NULL),
468 (GSListFunc) g_slist_append);
469 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
470 g_build_filename
471 (G_DIR_SEPARATOR_S,
472 "usr", "share", NULL),
473 (GSListFunc) g_slist_append);
474 }
475 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
476 g_strdup(xdg_data_home_path),
477 (GSListFunc) g_slist_prepend);
478 }
479
480 void parse_paths_shutdown()
481 {
482 GSList *it;
483
484 if (!xdg_start)
485 return;
486 xdg_start = FALSE;
487
488 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
489 g_free(it->data);
490 g_slist_free(xdg_config_dir_paths);
491 xdg_config_dir_paths = NULL;
492 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
493 g_free(it->data);
494 g_slist_free(xdg_data_dir_paths);
495 xdg_data_dir_paths = NULL;
496 g_free(xdg_config_home_path);
497 xdg_config_home_path = NULL;
498 g_free(xdg_data_home_path);
499 xdg_data_home_path = NULL;
500 }
501
502 gchar *parse_expand_tilde(const gchar *f)
503 {
504 gchar **spl;
505 gchar *ret;
506
507 if (!f)
508 return NULL;
509 spl = g_strsplit(f, "~", 0);
510 ret = g_strjoinv(g_get_home_dir(), spl);
511 g_strfreev(spl);
512 return ret;
513 }
514
515 gboolean parse_mkdir(const gchar *path, gint mode)
516 {
517 gboolean ret = TRUE;
518
519 g_return_val_if_fail(path != NULL, FALSE);
520 g_return_val_if_fail(path[0] != '\0', FALSE);
521
522 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
523 if (mkdir(path, mode) == -1)
524 ret = FALSE;
525
526 return ret;
527 }
528
529 gboolean parse_mkdir_path(const gchar *path, gint mode)
530 {
531 gboolean ret = TRUE;
532
533 g_return_val_if_fail(path != NULL, FALSE);
534 g_return_val_if_fail(path[0] == '/', FALSE);
535
536 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
537 gchar *c, *e;
538
539 c = g_strdup(path);
540 e = c;
541 while ((e = strchr(e + 1, '/'))) {
542 *e = '\0';
543 if (!(ret = parse_mkdir(c, mode)))
544 goto parse_mkdir_path_end;
545 *e = '/';
546 }
547 ret = parse_mkdir(c, mode);
548
549 parse_mkdir_path_end:
550 g_free(c);
551 }
552
553 return ret;
554 }
555
556 const gchar* parse_xdg_config_home_path()
557 {
558 return xdg_config_home_path;
559 }
560
561 const gchar* parse_xdg_data_home_path()
562 {
563 return xdg_data_home_path;
564 }
565
566 GSList* parse_xdg_config_dir_paths()
567 {
568 return xdg_config_dir_paths;
569 }
570
571 GSList* parse_xdg_data_dir_paths()
572 {
573 return xdg_data_dir_paths;
574 }
This page took 0.060005 seconds and 4 git commands to generate.