1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 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.
24 #include <sys/types.h>
27 static gboolean xdg_start;
28 static gchar *xdg_config_home_path;
29 static gchar *xdg_data_home_path;
30 static GSList *xdg_config_dir_paths;
31 static GSList *xdg_data_dir_paths;
40 GHashTable *callbacks;
43 static void destfunc(struct Callback *c)
49 ObParseInst* parse_startup()
51 ObParseInst *i = g_new(ObParseInst, 1);
52 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
53 (GDestroyNotify)destfunc);
57 void parse_shutdown(ObParseInst *i)
60 g_hash_table_destroy(i->callbacks);
65 void parse_register(ObParseInst *i, const gchar *tag,
66 ParseCallback func, gpointer data)
70 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
71 g_warning("Tag '%s' already registered", tag);
75 c = g_new(struct Callback, 1);
76 c->tag = g_strdup(tag);
79 g_hash_table_insert(i->callbacks, c->tag, c);
82 gboolean parse_load_rc(xmlDocPtr *doc, xmlNodePtr *root)
88 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
89 path = g_build_filename(it->data, "openbox", "rc.xml", NULL);
90 r = parse_load(path, "openbox_config", doc, root);
94 g_warning("Unable to find a valid config file, using defaults");
98 gboolean parse_load_theme(const gchar *name, xmlDocPtr *doc, xmlNodePtr *root,
105 /* backward compatibility.. */
106 path = g_build_filename(g_get_home_dir(), ".themes", name,
107 "openbox-3", "themerc.xml", NULL);
108 if ((r = parse_load(path, "openbox_theme", doc, root)))
109 *retpath = g_path_get_dirname(path);
113 for (it = xdg_data_dir_paths; !r && it; it = g_slist_next(it)) {
114 path = g_build_filename(it->data, "themes", name, "openbox-3",
115 "themerc.xml", NULL);
116 if ((r = parse_load(path, "openbox_theme", doc, root)))
117 *retpath = g_path_get_dirname(path);
122 g_warning("Unable to load the theme %s", name);
126 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
132 if (file[0] == '/') {
133 r = parse_load(file, "openbox_menu", doc, root);
135 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
136 path = g_build_filename(it->data, "openbox", file, NULL);
137 r = parse_load(path, "openbox_menu", doc, root);
142 g_warning("Unable to find a valid menu file '%s'", file);
146 gboolean parse_load(const gchar *path, const gchar *rootname,
147 xmlDocPtr *doc, xmlNodePtr *root)
150 if (stat(path, &s) < 0)
153 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
154 without this option, the tree is weird and has extra nodes in it. */
155 if ((*doc = xmlReadFile(path, NULL,
156 XML_PARSE_NOBLANKS | XML_PARSE_RECOVER))) {
157 *root = xmlDocGetRootElement(*doc);
161 g_warning("%s is an empty document", path);
163 if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
166 g_warning("Document %s is of wrong type. root node is "
167 "not '%s'", path, rootname);
176 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
177 xmlDocPtr *doc, xmlNodePtr *root)
179 if ((*doc = xmlParseMemory(data, len))) {
180 *root = xmlDocGetRootElement(*doc);
184 g_warning("Given memory is an empty document");
186 if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
189 g_warning("Document in given memory is of wrong type. root "
190 "node is not '%s'", rootname);
199 void parse_close(xmlDocPtr doc)
204 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
207 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
210 c->func(i, doc, node, c->data);
216 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
218 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
219 gchar *s = g_strdup(c ? (gchar*)c : "");
224 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
226 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
227 gint i = atoi((gchar*)c);
232 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
234 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
236 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
238 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
240 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
246 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
248 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
250 r = !xmlStrcasecmp(c, (const xmlChar*) val);
255 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
258 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
265 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
267 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
270 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
271 *value = TRUE, r = TRUE;
272 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
273 *value = TRUE, r = TRUE;
274 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
275 *value = TRUE, r = TRUE;
276 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
277 *value = FALSE, r = TRUE;
278 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
279 *value = FALSE, r = TRUE;
280 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
281 *value = FALSE, r = TRUE;
287 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
289 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
292 *value = atoi((gchar*)c);
299 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
301 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
304 *value = g_strdup((gchar*)c);
311 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
314 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
317 r = !xmlStrcasecmp(c, (const xmlChar*) val);
322 static gint slist_path_cmp(const gchar *a, const gchar *b)
327 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
329 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
336 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
337 list = func(list, data);
344 static GSList* split_paths(const gchar *paths)
351 spl = g_strsplit(paths, ":", -1);
352 for (it = spl; *it; ++it)
353 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
358 void parse_paths_startup()
366 path = g_getenv("XDG_CONFIG_HOME");
367 if (path && path[0] != '\0') /* not unset or empty */
368 xdg_config_home_path = g_build_filename(path, NULL);
370 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
373 path = g_getenv("XDG_DATA_HOME");
374 if (path && path[0] != '\0') /* not unset or empty */
375 xdg_data_home_path = g_build_filename(path, NULL);
377 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
380 path = g_getenv("XDG_CONFIG_DIRS");
381 if (path && path[0] != '\0') /* not unset or empty */
382 xdg_config_dir_paths = split_paths(path);
384 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
388 (GSListFunc) g_slist_append);
389 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
391 (GSListFunc) g_slist_append);
393 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
394 g_strdup(xdg_config_home_path),
395 (GSListFunc) g_slist_prepend);
397 path = g_getenv("XDG_DATA_DIRS");
398 if (path && path[0] != '\0') /* not unset or empty */
399 xdg_data_dir_paths = split_paths(path);
401 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
404 "usr", "local", "share", NULL),
405 (GSListFunc) g_slist_append);
406 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
409 "usr", "share", NULL),
410 (GSListFunc) g_slist_append);
411 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
413 (GSListFunc) g_slist_append);
415 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
416 g_strdup(xdg_data_home_path),
417 (GSListFunc) g_slist_prepend);
420 void parse_paths_shutdown()
428 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
430 g_slist_free(xdg_config_dir_paths);
431 xdg_config_dir_paths = NULL;
432 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
434 g_slist_free(xdg_data_dir_paths);
435 xdg_data_dir_paths = NULL;
436 g_free(xdg_config_home_path);
437 xdg_config_home_path = NULL;
438 g_free(xdg_data_home_path);
439 xdg_data_home_path = NULL;
442 gchar *parse_expand_tilde(const gchar *f)
449 spl = g_strsplit(f, "~", 0);
450 ret = g_strjoinv(g_get_home_dir(), spl);
455 gboolean parse_mkdir(const gchar *path, gint mode)
459 g_return_val_if_fail(path != NULL, FALSE);
460 g_return_val_if_fail(path[0] != '\0', FALSE);
462 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
463 if (mkdir(path, mode) == -1)
469 gboolean parse_mkdir_path(const gchar *path, gint mode)
473 g_return_val_if_fail(path != NULL, FALSE);
474 g_return_val_if_fail(path[0] == '/', FALSE);
476 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
481 while ((e = strchr(e + 1, '/'))) {
483 if (!(ret = parse_mkdir(c, mode)))
484 goto parse_mkdir_path_end;
487 ret = parse_mkdir(c, mode);
489 parse_mkdir_path_end:
496 const gchar* parse_xdg_config_home_path()
498 return xdg_config_home_path;
501 const gchar* parse_xdg_data_home_path()
503 return xdg_data_home_path;
506 GSList* parse_xdg_config_dir_paths()
508 return xdg_config_dir_paths;
511 GSList* parse_xdg_data_dir_paths()
513 return xdg_data_dir_paths;