install-sh should be executable
[openbox.git] / parser / parse.c
blob636b451f2b5d3a937b76279ebdf971bc3b767655
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.
19 #include "parse.h"
20 #include <glib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.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;
33 struct Callback {
34 gchar *tag;
35 ParseCallback func;
36 gpointer data;
39 struct _ObParseInst {
40 GHashTable *callbacks;
43 static void destfunc(struct Callback *c)
45 g_free(c->tag);
46 g_free(c);
49 ObParseInst* parse_startup(void)
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);
54 return i;
57 void parse_shutdown(ObParseInst *i)
59 if (i) {
60 g_hash_table_destroy(i->callbacks);
61 g_free(i);
65 void parse_register(ObParseInst *i, const gchar *tag,
66 ParseCallback func, gpointer data)
68 struct Callback *c;
70 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
71 g_error("Tag '%s' already registered", tag);
72 return;
75 c = g_new(struct Callback, 1);
76 c->tag = g_strdup(tag);
77 c->func = func;
78 c->data = data;
79 g_hash_table_insert(i->callbacks, c->tag, c);
82 gboolean parse_load_rc(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
84 GSList *it;
85 gboolean r = FALSE;
87 if (file && parse_load(file, "openbox_config", doc, root))
88 return TRUE;
90 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
91 gchar *path;
93 path = g_build_filename(it->data, "openbox", "rc.xml", NULL);
94 r = parse_load(path, "openbox_config", doc, root);
95 g_free(path);
98 return r;
101 gboolean parse_load_theme(const gchar *name, xmlDocPtr *doc, xmlNodePtr *root,
102 gchar **retpath)
104 GSList *it;
105 gchar *path;
106 gboolean r = FALSE;
107 gchar *eng;
109 /* backward compatibility.. */
110 path = g_build_filename(g_get_home_dir(), ".themes", name,
111 "openbox-3", "themerc.xml", NULL);
112 if (parse_load(path, "openbox_theme", doc, root) &&
113 parse_attr_string("engine", *root, &eng))
115 if (!strcmp(eng, "box")) {
116 *retpath = g_path_get_dirname(path);
117 r = TRUE;
119 g_free(eng);
121 g_free(path);
123 if (!r) {
124 for (it = xdg_data_dir_paths; !r && it; it = g_slist_next(it)) {
125 path = g_build_filename(it->data, "themes", name, "openbox-3",
126 "themerc.xml", NULL);
127 if (parse_load(path, "openbox_theme", doc, root) &&
128 parse_attr_string("engine", *root, &eng))
130 if (!strcmp(eng, "box")) {
131 *retpath = g_path_get_dirname(path);
132 r = TRUE;
134 g_free(eng);
136 g_free(path);
139 return r;
142 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
144 GSList *it;
145 gchar *path;
146 gboolean r = FALSE;
148 if (file[0] == '/') {
149 r = parse_load(file, "openbox_menu", doc, root);
150 } else {
151 for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
152 path = g_build_filename(it->data, "openbox", file, NULL);
153 r = parse_load(path, "openbox_menu", doc, root);
154 g_free(path);
157 return r;
160 gboolean parse_load(const gchar *path, const gchar *rootname,
161 xmlDocPtr *doc, xmlNodePtr *root)
163 struct stat s;
165 if (stat(path, &s) < 0)
166 return FALSE;
168 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
169 without this option, the tree is weird and has extra nodes in it. */
170 if ((*doc = xmlReadFile(path, NULL,
171 XML_PARSE_NOBLANKS | XML_PARSE_RECOVER))) {
172 *root = xmlDocGetRootElement(*doc);
173 if (!*root) {
174 xmlFreeDoc(*doc);
175 *doc = NULL;
176 g_message("%s is an empty document", path);
177 } else {
178 if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
179 xmlFreeDoc(*doc);
180 *doc = NULL;
181 g_message("XML Document %s is of wrong type. Root "
182 "node is not '%s'", path, rootname);
186 if (!*doc)
187 return FALSE;
188 return TRUE;
191 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
192 xmlDocPtr *doc, xmlNodePtr *root)
194 if ((*doc = xmlParseMemory(data, len))) {
195 *root = xmlDocGetRootElement(*doc);
196 if (!*root) {
197 xmlFreeDoc(*doc);
198 *doc = NULL;
199 g_message("Given memory is an empty document");
200 } else {
201 if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
202 xmlFreeDoc(*doc);
203 *doc = NULL;
204 g_message("XML Document in given memory is of wrong "
205 "type. Root node is not '%s'\n", rootname);
209 if (!*doc)
210 return FALSE;
211 return TRUE;
214 void parse_close(xmlDocPtr doc)
216 xmlFreeDoc(doc);
219 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
221 while (node) {
222 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
224 if (c)
225 c->func(i, doc, node, c->data);
227 node = node->next;
231 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
233 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
234 gchar *s = g_strdup(c ? (gchar*)c : "");
235 xmlFree(c);
236 return s;
239 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
241 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
242 gint i = c ? atoi((gchar*)c) : 0;
243 xmlFree(c);
244 return i;
247 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
249 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
250 gboolean b = FALSE;
251 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
252 b = TRUE;
253 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
254 b = TRUE;
255 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
256 b = TRUE;
257 xmlFree(c);
258 return b;
261 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
263 xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
264 gboolean r;
265 r = !xmlStrcasecmp(c, (const xmlChar*) val);
266 xmlFree(c);
267 return r;
270 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
272 while (node) {
273 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
274 return node;
275 node = node->next;
277 return NULL;
280 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
282 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
283 gboolean r = FALSE;
284 if (c) {
285 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
286 *value = TRUE, r = TRUE;
287 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
288 *value = TRUE, r = TRUE;
289 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
290 *value = TRUE, r = TRUE;
291 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
292 *value = FALSE, r = TRUE;
293 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
294 *value = FALSE, r = TRUE;
295 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
296 *value = FALSE, r = TRUE;
298 xmlFree(c);
299 return r;
302 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
304 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
305 gboolean r = FALSE;
306 if (c) {
307 *value = atoi((gchar*)c);
308 r = TRUE;
310 xmlFree(c);
311 return r;
314 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
316 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
317 gboolean r = FALSE;
318 if (c) {
319 *value = g_strdup((gchar*)c);
320 r = TRUE;
322 xmlFree(c);
323 return r;
326 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
327 const gchar *name)
329 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
330 gboolean r = FALSE;
331 if (c)
332 r = !xmlStrcasecmp(c, (const xmlChar*) val);
333 xmlFree(c);
334 return r;
337 static gint slist_path_cmp(const gchar *a, const gchar *b)
339 return strcmp(a, b);
342 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
344 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
346 g_assert(func);
348 if (!data)
349 return list;
351 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
352 list = func(list, data);
353 else
354 g_free(data);
356 return list;
359 static GSList* split_paths(const gchar *paths)
361 GSList *list = NULL;
362 gchar **spl, **it;
364 if (!paths)
365 return NULL;
366 spl = g_strsplit(paths, ":", -1);
367 for (it = spl; *it; ++it)
368 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
369 g_free(spl);
370 return list;
373 void parse_paths_startup(void)
375 const gchar *path;
377 if (xdg_start)
378 return;
379 xdg_start = TRUE;
381 path = g_getenv("XDG_CONFIG_HOME");
382 if (path && path[0] != '\0') /* not unset or empty */
383 xdg_config_home_path = g_build_filename(path, NULL);
384 else
385 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
386 NULL);
388 path = g_getenv("XDG_DATA_HOME");
389 if (path && path[0] != '\0') /* not unset or empty */
390 xdg_data_home_path = g_build_filename(path, NULL);
391 else
392 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
393 "share", NULL);
395 path = g_getenv("XDG_CONFIG_DIRS");
396 if (path && path[0] != '\0') /* not unset or empty */
397 xdg_config_dir_paths = split_paths(path);
398 else {
399 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
400 g_strdup(CONFIGDIR),
401 (GSListFunc) g_slist_append);
402 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
403 g_build_filename
404 (G_DIR_SEPARATOR_S,
405 "etc", "xdg", NULL),
406 (GSListFunc) g_slist_append);
408 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
409 g_strdup(xdg_config_home_path),
410 (GSListFunc) g_slist_prepend);
412 path = g_getenv("XDG_DATA_DIRS");
413 if (path && path[0] != '\0') /* not unset or empty */
414 xdg_data_dir_paths = split_paths(path);
415 else {
416 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
417 g_strdup(DATADIR),
418 (GSListFunc) g_slist_append);
419 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
420 g_build_filename
421 (G_DIR_SEPARATOR_S,
422 "usr", "local", "share", NULL),
423 (GSListFunc) g_slist_append);
424 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
425 g_build_filename
426 (G_DIR_SEPARATOR_S,
427 "usr", "share", NULL),
428 (GSListFunc) g_slist_append);
430 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
431 g_strdup(xdg_data_home_path),
432 (GSListFunc) g_slist_prepend);
435 void parse_paths_shutdown(void)
437 GSList *it;
439 if (!xdg_start)
440 return;
441 xdg_start = FALSE;
443 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
444 g_free(it->data);
445 g_slist_free(xdg_config_dir_paths);
446 xdg_config_dir_paths = NULL;
447 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
448 g_free(it->data);
449 g_slist_free(xdg_data_dir_paths);
450 xdg_data_dir_paths = NULL;
451 g_free(xdg_config_home_path);
452 xdg_config_home_path = NULL;
453 g_free(xdg_data_home_path);
454 xdg_data_home_path = NULL;
457 gchar *parse_expand_tilde(const gchar *f)
459 gchar **spl;
460 gchar *ret;
462 if (!f)
463 return NULL;
464 spl = g_strsplit(f, "~", 0);
465 ret = g_strjoinv(g_get_home_dir(), spl);
466 g_strfreev(spl);
467 return ret;
470 gboolean parse_mkdir(const gchar *path, gint mode)
472 gboolean ret = TRUE;
474 g_return_val_if_fail(path != NULL, FALSE);
475 g_return_val_if_fail(path[0] != '\0', FALSE);
477 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
478 if (mkdir(path, mode) == -1)
479 ret = FALSE;
481 return ret;
484 gboolean parse_mkdir_path(const gchar *path, gint mode)
486 gboolean ret = TRUE;
488 g_return_val_if_fail(path != NULL, FALSE);
489 g_return_val_if_fail(path[0] == '/', FALSE);
491 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
492 gchar *c, *e;
494 c = g_strdup(path);
495 e = c;
496 while ((e = strchr(e + 1, '/'))) {
497 *e = '\0';
498 if (!(ret = parse_mkdir(c, mode)))
499 goto parse_mkdir_path_end;
500 *e = '/';
502 ret = parse_mkdir(c, mode);
504 parse_mkdir_path_end:
505 g_free(c);
508 return ret;
511 const gchar* parse_xdg_config_home_path(void)
513 return xdg_config_home_path;
516 const gchar* parse_xdg_data_home_path(void)
518 return xdg_data_home_path;
521 GSList* parse_xdg_config_dir_paths(void)
523 return xdg_config_dir_paths;
526 GSList* parse_xdg_data_dir_paths(void)
528 return xdg_data_dir_paths;