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()
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 void parse_register(ObtParseInst *i, const gchar *tag,
91 ObtParseCallback func, gpointer data)
95 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
96 g_error("Tag '%s' already registered", tag);
100 c = g_new(struct Callback, 1);
101 c->tag = g_strdup(tag);
104 g_hash_table_insert(i->callbacks, c->tag, c);
107 static gboolean load_file(ObtParseInst *i,
109 const gchar *filename,
110 const gchar *root_node,
116 g_assert(i->doc == NULL); /* another doc isn't open already? */
118 for (it = paths; !r && it; it = g_slist_next(it)) {
122 path = g_build_filename(it->data, domain, filename, NULL);
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 |
130 i->root = xmlDocGetRootElement(i->doc);
134 g_message("%s is an empty XML document", path);
136 else if (xmlStrcmp(i->root->name,
137 (const xmlChar*)root_node)) {
141 g_message("XML document %s is of wrong type. Root "
142 "node is not '%s'", path, root_node);
145 i->path = g_strdup(path);
157 gboolean obt_parse_load_config_file(ObtParseInst *i,
159 const gchar *filename,
160 const gchar *root_node)
162 GSList *it, *paths = NULL;
165 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
166 paths = g_slist_append(paths, g_strdup(it->data));
168 r = load_file(i, domain, filename, root_node, paths);
172 paths = g_slist_delete_link(paths, paths);
177 gboolean obt_parse_load_data_file(ObtParseInst *i,
179 const gchar *filename,
180 const gchar *root_node)
182 GSList *it, *paths = NULL;
185 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
186 paths = g_slist_append(paths, g_strdup(it->data));
188 r = load_file(i, domain, filename, root_node, paths);
192 paths = g_slist_delete_link(paths, paths);
197 gboolean obt_parse_load_theme_file(ObtParseInst *i,
200 const gchar *filename,
201 const gchar *root_node)
203 GSList *it, *paths = NULL;
206 /* use ~/.themes for backwards compatibility */
207 paths = g_slist_append
208 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
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));
214 r = load_file(i, domain, filename, root_node, paths);
218 paths = g_slist_delete_link(paths, paths);
224 gboolean obt_parse_load_mem(ObtParseInst *i,
225 gpointer data, guint len, const gchar *root_node)
229 g_assert(i->doc == NULL); /* another doc isn't open already? */
231 i->doc = xmlParseMemory(data, len);
233 i->root = xmlDocGetRootElement(i->doc);
237 g_message("Given memory is an empty document");
239 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
243 g_message("XML Document in given memory is of wrong "
244 "type. Root node is not '%s'\n", root_node);
252 void obt_parse_close(ObtParseInst *i)
263 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
265 g_assert(i->doc); /* a doc is open? */
268 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
269 if (c) c->func(i, i->doc, node, c->data);
274 gchar *obt_parse_node_string(xmlNodePtr node)
276 xmlChar *c = xmlNodeGetContent(node);
277 gchar *s = g_strdup(c ? (gchar*)c : "");
282 gint obt_parse_node_int(xmlNodePtr node)
284 xmlChar *c = xmlNodeGetContent(node);
285 gint i = c ? atoi((gchar*)c) : 0;
290 gboolean obt_parse_node_bool(xmlNodePtr node)
292 xmlChar *c = xmlNodeGetContent(node);
294 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
296 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
298 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
304 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
306 xmlChar *c = xmlNodeGetContent(node);
308 r = !xmlStrcasecmp(c, (const xmlChar*) val);
313 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
316 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
323 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
326 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
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;
346 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
348 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
351 *value = atoi((gchar*)c);
358 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
361 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
364 *value = g_strdup((gchar*)c);
371 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
374 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
377 r = !xmlStrcasecmp(c, (const xmlChar*) val);
382 static gint slist_path_cmp(const gchar *a, const gchar *b)
387 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
389 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
396 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
397 list = func(list, data);
404 static GSList* split_paths(const gchar *paths)
411 spl = g_strsplit(paths, ":", -1);
412 for (it = spl; *it; ++it)
413 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
418 void parse_paths_startup()
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);
430 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
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);
437 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
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);
444 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
446 (GSListFunc) g_slist_append);
447 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
451 (GSListFunc) g_slist_append);
453 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
454 g_strdup(xdg_config_home_path),
455 (GSListFunc) g_slist_prepend);
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);
461 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
463 (GSListFunc) g_slist_append);
464 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
467 "usr", "local", "share", NULL),
468 (GSListFunc) g_slist_append);
469 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
472 "usr", "share", NULL),
473 (GSListFunc) g_slist_append);
475 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
476 g_strdup(xdg_data_home_path),
477 (GSListFunc) g_slist_prepend);
480 void parse_paths_shutdown()
488 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
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))
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;
502 gchar *parse_expand_tilde(const gchar *f)
509 spl = g_strsplit(f, "~", 0);
510 ret = g_strjoinv(g_get_home_dir(), spl);
515 gboolean parse_mkdir(const gchar *path, gint mode)
519 g_return_val_if_fail(path != NULL, FALSE);
520 g_return_val_if_fail(path[0] != '\0', FALSE);
522 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
523 if (mkdir(path, mode) == -1)
529 gboolean parse_mkdir_path(const gchar *path, gint mode)
533 g_return_val_if_fail(path != NULL, FALSE);
534 g_return_val_if_fail(path[0] == '/', FALSE);
536 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
541 while ((e = strchr(e + 1, '/'))) {
543 if (!(ret = parse_mkdir(c, mode)))
544 goto parse_mkdir_path_end;
547 ret = parse_mkdir(c, mode);
549 parse_mkdir_path_end:
556 const gchar* parse_xdg_config_home_path()
558 return xdg_config_home_path;
561 const gchar* parse_xdg_data_home_path()
563 return xdg_data_home_path;
566 GSList* parse_xdg_config_dir_paths()
568 return xdg_config_dir_paths;
571 GSList* parse_xdg_data_dir_paths()
573 return xdg_data_dir_paths;