1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/parse.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/parse.h"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
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;
47 ObtParseCallback func;
51 struct _ObtParseInst {
53 GHashTable *callbacks;
59 static void destfunc(struct Callback *c)
65 ObtParseInst* obt_parse_instance_new(void)
67 ObtParseInst *i = g_new(ObtParseInst, 1);
69 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify)destfunc);
77 void obt_parse_instance_ref(ObtParseInst *i)
82 void obt_parse_instance_unref(ObtParseInst *i)
84 if (i && --i->ref == 0) {
85 g_hash_table_destroy(i->callbacks);
90 xmlDocPtr obt_parse_instance_doc(ObtParseInst *i)
92 g_assert(i->doc); /* a doc is open? */
96 xmlNodePtr obt_parse_instance_root(ObtParseInst *i)
98 g_assert(i->doc); /* a doc is open? */
102 void obt_parse_register(ObtParseInst *i, const gchar *tag,
103 ObtParseCallback func, gpointer data)
107 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
108 g_error("Tag '%s' already registered", tag);
112 c = g_new(struct Callback, 1);
113 c->tag = g_strdup(tag);
116 g_hash_table_insert(i->callbacks, c->tag, c);
119 static gboolean load_file(ObtParseInst *i,
121 const gchar *filename,
122 const gchar *root_node,
128 g_assert(i->doc == NULL); /* another doc isn't open already? */
130 for (it = paths; !r && it; it = g_slist_next(it)) {
134 if (!domain && !filename) /* given a full path to the file */
135 path = g_strdup(it->data);
137 path = g_build_filename(it->data, domain, filename, NULL);
139 if (stat(path, &s) >= 0) {
140 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
141 with extra nodes in it. */
142 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
145 i->root = xmlDocGetRootElement(i->doc);
149 g_message("%s is an empty XML document", path);
151 else if (xmlStrcmp(i->root->name,
152 (const xmlChar*)root_node)) {
156 g_message("XML document %s is of wrong type. Root "
157 "node is not '%s'", path, root_node);
160 i->path = g_strdup(path);
172 gboolean obt_parse_load_file(ObtParseInst *i,
174 const gchar *root_node)
179 paths = g_slist_append(NULL, g_strdup(path));
181 r = load_file(i, NULL, NULL, root_node, paths);
185 paths = g_slist_delete_link(paths, paths);
190 gboolean obt_parse_load_config_file(ObtParseInst *i,
192 const gchar *filename,
193 const gchar *root_node)
195 GSList *it, *paths = NULL;
198 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
199 paths = g_slist_append(paths, g_strdup(it->data));
201 r = load_file(i, domain, filename, root_node, paths);
205 paths = g_slist_delete_link(paths, paths);
210 gboolean obt_parse_load_data_file(ObtParseInst *i,
212 const gchar *filename,
213 const gchar *root_node)
215 GSList *it, *paths = NULL;
218 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
219 paths = g_slist_append(paths, g_strdup(it->data));
221 r = load_file(i, domain, filename, root_node, paths);
225 paths = g_slist_delete_link(paths, paths);
230 gboolean obt_parse_load_theme_file(ObtParseInst *i,
233 const gchar *filename,
234 const gchar *root_node)
236 GSList *it, *paths = NULL;
239 /* use ~/.themes for backwards compatibility */
240 paths = g_slist_append
241 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
243 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
244 paths = g_slist_append
245 (paths, g_build_filename(it->data, "themes", theme, NULL));
247 r = load_file(i, domain, filename, root_node, paths);
251 paths = g_slist_delete_link(paths, paths);
257 gboolean obt_parse_load_mem(ObtParseInst *i,
258 gpointer data, guint len, const gchar *root_node)
262 g_assert(i->doc == NULL); /* another doc isn't open already? */
264 i->doc = xmlParseMemory(data, len);
266 i->root = xmlDocGetRootElement(i->doc);
270 g_message("Given memory is an empty document");
272 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
276 g_message("XML Document in given memory is of wrong "
277 "type. Root node is not '%s'\n", root_node);
285 void obt_parse_close(ObtParseInst *i)
296 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
298 g_assert(i->doc); /* a doc is open? */
301 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
302 if (c) c->func(node, c->data);
307 void obt_parse_tree_from_root(ObtParseInst *i)
309 obt_parse_tree(i, i->root->children);
312 gchar *obt_parse_node_string(xmlNodePtr node)
314 xmlChar *c = xmlNodeGetContent(node);
315 gchar *s = g_strdup(c ? (gchar*)c : "");
320 gint obt_parse_node_int(xmlNodePtr node)
322 xmlChar *c = xmlNodeGetContent(node);
323 gint i = c ? atoi((gchar*)c) : 0;
328 gboolean obt_parse_node_bool(xmlNodePtr node)
330 xmlChar *c = xmlNodeGetContent(node);
332 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
334 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
336 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
342 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
344 xmlChar *c = xmlNodeGetContent(node);
346 r = !xmlStrcasecmp(c, (const xmlChar*) val);
351 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
354 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
361 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
364 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
367 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
368 *value = TRUE, r = TRUE;
369 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
370 *value = TRUE, r = TRUE;
371 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
372 *value = TRUE, r = TRUE;
373 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
374 *value = FALSE, r = TRUE;
375 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
376 *value = FALSE, r = TRUE;
377 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
378 *value = FALSE, r = TRUE;
384 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
386 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
389 *value = atoi((gchar*)c);
396 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
399 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
402 *value = g_strdup((gchar*)c);
409 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
412 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
415 r = !xmlStrcasecmp(c, (const xmlChar*) val);
420 static gint slist_path_cmp(const gchar *a, const gchar *b)
425 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
427 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
434 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
435 list = func(list, data);
442 static GSList* split_paths(const gchar *paths)
449 spl = g_strsplit(paths, ":", -1);
450 for (it = spl; *it; ++it)
451 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
456 void parse_paths_startup(void)
464 path = g_getenv("XDG_CONFIG_HOME");
465 if (path && path[0] != '\0') /* not unset or empty */
466 xdg_config_home_path = g_build_filename(path, NULL);
468 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
471 path = g_getenv("XDG_DATA_HOME");
472 if (path && path[0] != '\0') /* not unset or empty */
473 xdg_data_home_path = g_build_filename(path, NULL);
475 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
478 path = g_getenv("XDG_CONFIG_DIRS");
479 if (path && path[0] != '\0') /* not unset or empty */
480 xdg_config_dir_paths = split_paths(path);
482 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
484 (GSListFunc) g_slist_append);
485 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
489 (GSListFunc) g_slist_append);
491 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
492 g_strdup(xdg_config_home_path),
493 (GSListFunc) g_slist_prepend);
495 path = g_getenv("XDG_DATA_DIRS");
496 if (path && path[0] != '\0') /* not unset or empty */
497 xdg_data_dir_paths = split_paths(path);
499 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
501 (GSListFunc) g_slist_append);
502 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
505 "usr", "local", "share", NULL),
506 (GSListFunc) g_slist_append);
507 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
510 "usr", "share", NULL),
511 (GSListFunc) g_slist_append);
513 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
514 g_strdup(xdg_data_home_path),
515 (GSListFunc) g_slist_prepend);
518 void parse_paths_shutdown(void)
526 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
528 g_slist_free(xdg_config_dir_paths);
529 xdg_config_dir_paths = NULL;
530 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
532 g_slist_free(xdg_data_dir_paths);
533 xdg_data_dir_paths = NULL;
534 g_free(xdg_config_home_path);
535 xdg_config_home_path = NULL;
536 g_free(xdg_data_home_path);
537 xdg_data_home_path = NULL;
540 gchar *parse_expand_tilde(const gchar *f)
547 spl = g_strsplit(f, "~", 0);
548 ret = g_strjoinv(g_get_home_dir(), spl);
553 gboolean parse_mkdir(const gchar *path, gint mode)
557 g_return_val_if_fail(path != NULL, FALSE);
558 g_return_val_if_fail(path[0] != '\0', FALSE);
560 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
561 if (mkdir(path, mode) == -1)
567 gboolean parse_mkdir_path(const gchar *path, gint mode)
571 g_return_val_if_fail(path != NULL, FALSE);
572 g_return_val_if_fail(path[0] == '/', FALSE);
574 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
579 while ((e = strchr(e + 1, '/'))) {
581 if (!(ret = parse_mkdir(c, mode)))
582 goto parse_mkdir_path_end;
585 ret = parse_mkdir(c, mode);
587 parse_mkdir_path_end:
594 const gchar* parse_xdg_config_home_path(void)
596 return xdg_config_home_path;
599 const gchar* parse_xdg_data_home_path(void)
601 return xdg_data_home_path;
604 GSList* parse_xdg_config_dir_paths(void)
606 return xdg_config_dir_paths;
609 GSList* parse_xdg_data_dir_paths(void)
611 return xdg_data_dir_paths;