1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 parse.c for the Openbox window manager
4 Copyright (c) 2003 Ben 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>
26 static gboolean xdg_start;
27 static gchar *xdg_config_home_path;
28 static gchar *xdg_data_home_path;
29 static GSList *xdg_config_dir_paths;
30 static GSList *xdg_data_dir_paths;
39 GHashTable *callbacks;
42 static void destfunc(struct Callback *c)
48 ObParseInst* parse_startup()
50 ObParseInst *i = g_new(ObParseInst, 1);
51 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
52 (GDestroyNotify)destfunc);
56 void parse_shutdown(ObParseInst *i)
59 g_hash_table_destroy(i->callbacks);
64 void parse_register(ObParseInst *i, const gchar *tag,
65 ParseCallback func, gpointer data)
69 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
70 g_warning("tag '%s' already registered", tag);
74 c = g_new(struct Callback, 1);
75 c->tag = g_strdup(tag);
78 g_hash_table_insert(i->callbacks, c->tag, c);
81 gboolean parse_load_rc(xmlDocPtr *doc, xmlNodePtr *root)
87 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
88 path = g_build_filename(it->data, "openbox", "rc.xml", NULL);
89 r = parse_load(path, "openbox_config", doc, root);
93 g_warning("unable to find a valid config file, using defaults");
97 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
103 if (file[0] == '/') {
104 r = parse_load(file, "openbox_menu", doc, root);
106 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
107 path = g_build_filename(it->data, "openbox", file, NULL);
108 r = parse_load(path, "openbox_menu", doc, root);
113 g_warning("unable to find a valid menu file '%s'", file);
117 gboolean parse_load(const gchar *path, const gchar *rootname,
118 xmlDocPtr *doc, xmlNodePtr *root)
120 if ((*doc = xmlParseFile(path))) {
121 *root = xmlDocGetRootElement(*doc);
125 g_warning("%s is an empty document", path);
127 if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
130 g_warning("document %s is of wrong type. root node is "
131 "not '%s'", path, rootname);
140 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
141 xmlDocPtr *doc, xmlNodePtr *root)
143 if ((*doc = xmlParseMemory(data, len))) {
144 *root = xmlDocGetRootElement(*doc);
148 g_warning("Given memory is an empty document");
150 if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
153 g_warning("document in given memory is of wrong type. root "
154 "node is not '%s'", rootname);
163 void parse_close(xmlDocPtr doc)
168 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
171 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
174 c->func(i, doc, node, c->data);
180 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
182 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
183 gchar *s = g_strdup(c ? (gchar*)c : "");
188 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
190 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
191 gint i = atoi((gchar*)c);
196 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
198 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
200 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
202 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
204 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
210 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
212 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
214 r = !xmlStrcasecmp(c, (const xmlChar*) val);
219 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
222 if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
229 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
231 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
234 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
235 *value = TRUE, r = TRUE;
236 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
237 *value = TRUE, r = TRUE;
238 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
239 *value = TRUE, r = TRUE;
240 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
241 *value = FALSE, r = TRUE;
242 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
243 *value = FALSE, r = TRUE;
244 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
245 *value = FALSE, r = TRUE;
251 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
253 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
256 *value = atoi((gchar*)c);
263 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
265 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
268 *value = g_strdup((gchar*)c);
275 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
278 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
281 r = !xmlStrcasecmp(c, (const xmlChar*) val);
286 static gint slist_path_cmp(const gchar *a, const gchar *b)
291 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
293 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
300 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
301 list = func(list, data);
306 static GSList* split_paths(const gchar *paths)
313 spl = g_strsplit(paths, ":", -1);
314 for (it = spl; *it; ++it)
315 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
320 void parse_paths_startup()
328 path = g_getenv("XDG_CONFIG_HOME");
329 if (path && path[0] != '\0') /* not unset or empty */
330 xdg_config_home_path = g_build_filename(path, NULL);
332 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
335 path = g_getenv("XDG_DATA_HOME");
336 if (path && path[0] != '\0') /* not unset or empty */
337 xdg_data_home_path = g_build_filename(path, NULL);
339 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
342 path = g_getenv("XDG_CONFIG_DIRS");
343 if (path && path[0] != '\0') /* not unset or empty */
344 xdg_config_dir_paths = split_paths(path);
346 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
350 (GSListFunc) g_slist_append);
351 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
353 (GSListFunc) g_slist_append);
355 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
356 xdg_config_home_path,
357 (GSListFunc) g_slist_prepend);
359 path = g_getenv("XDG_DATA_DIRS");
360 if (path && path[0] != '\0') /* not unset or empty */
361 xdg_data_dir_paths = split_paths(path);
363 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
366 "usr", "local", "share", NULL),
367 (GSListFunc) g_slist_append);
368 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
371 "usr", "share", NULL),
372 (GSListFunc) g_slist_append);
373 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
375 (GSListFunc) g_slist_append);
377 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
379 (GSListFunc) g_slist_prepend);
382 void parse_paths_shutdown()
390 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
392 g_slist_free(xdg_config_dir_paths);
393 xdg_config_dir_paths = NULL;
394 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
396 g_slist_free(xdg_data_dir_paths);
397 xdg_data_dir_paths = NULL;
400 gchar *parse_expand_tilde(const gchar *f)
407 spl = g_strsplit(f, "~", 0);
408 ret = g_strjoinv(g_get_home_dir(), spl);
413 gboolean parse_mkdir(const gchar *path, gint mode)
417 g_return_val_if_fail(path != NULL, FALSE);
418 g_return_val_if_fail(path[0] != '\0', FALSE);
420 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
421 if (mkdir(path, mode) == -1)
427 gboolean parse_mkdir_path(const gchar *path, gint mode)
431 g_return_val_if_fail(path != NULL, FALSE);
432 g_return_val_if_fail(path[0] == '/', FALSE);
434 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
439 while ((e = strchr(e + 1, '/'))) {
441 if (!(ret = parse_mkdir(c, mode)))
442 goto parse_mkdir_path_end;
445 ret = parse_mkdir(c, mode);
447 parse_mkdir_path_end:
454 const gchar* parse_xdg_config_home_path()
456 return xdg_config_home_path;
459 const gchar* parse_xdg_data_home_path()
461 return xdg_data_home_path;
464 GSList* parse_xdg_config_dir_paths()
466 return xdg_config_dir_paths;
469 GSList* parse_xdg_data_dir_paths()
471 return xdg_data_dir_paths;