]> icculus.org git repositories - mikachu/openbox.git/blob - parser/parse.c
set micro_version correctly in configure.ac
[mikachu/openbox.git] / parser / parse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    parse.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
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.
10
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.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
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>
26
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;
32
33 struct Callback {
34     gchar *tag;
35     ParseCallback func;
36     gpointer data;
37 };
38
39 struct _ObParseInst {
40     GHashTable *callbacks;
41 };
42
43 static void destfunc(struct Callback *c)
44 {
45     g_free(c->tag);
46     g_free(c);
47 }
48
49 ObParseInst* parse_startup()
50 {
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;
55 }
56
57 void parse_shutdown(ObParseInst *i)
58 {
59     if (i) {
60         g_hash_table_destroy(i->callbacks);
61         g_free(i);
62     }
63 }
64
65 void parse_register(ObParseInst *i, const gchar *tag,
66                     ParseCallback func, gpointer data)
67 {
68     struct Callback *c;
69
70     if ((c = g_hash_table_lookup(i->callbacks, tag))) {
71         g_warning("Tag '%s' already registered", tag);
72         return;
73     }
74
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);
80 }
81
82 gboolean parse_load_rc(xmlDocPtr *doc, xmlNodePtr *root)
83 {
84     GSList *it;
85     gchar *path;
86     gboolean r = FALSE;
87
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);
91         g_free(path);
92     }
93     if (!r)
94         g_warning("Unable to find a valid config file, using defaults");
95     return r;
96 }
97
98 gboolean parse_load_theme(const gchar *name, xmlDocPtr *doc, xmlNodePtr *root,
99                           gchar **retpath)
100 {
101     GSList *it;
102     gchar *path;
103     gboolean r = FALSE;
104
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);
110     g_free(path);
111
112     if (!r) {
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);
118             g_free(path);
119         }
120     }
121     if (!r)
122         g_warning("Unable to load the theme %s", name);
123     return r;
124 }
125
126 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
127 {
128     GSList *it;
129     gchar *path;
130     gboolean r = FALSE;
131
132     if (file[0] == '/') {
133         r = parse_load(file, "openbox_menu", doc, root);
134     } else {
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);
138             g_free(path);
139         }
140     }
141     if (!r)
142         g_warning("Unable to find a valid menu file '%s'", file);
143     return r;
144 }
145
146 gboolean parse_load(const gchar *path, const gchar *rootname,
147                     xmlDocPtr *doc, xmlNodePtr *root)
148 {
149     struct stat s;
150     if (stat(path, &s) < 0)
151         return FALSE;
152
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);
158         if (!*root) {
159             xmlFreeDoc(*doc);
160             *doc = NULL;
161             g_warning("%s is an empty document", path);
162         } else {
163             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
164                 xmlFreeDoc(*doc);
165                 *doc = NULL;
166                 g_warning("Document %s is of wrong type. root node is "
167                           "not '%s'", path, rootname);
168             }
169         }
170     }
171     if (!*doc)
172         return FALSE;
173     return TRUE;
174 }
175
176 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
177                         xmlDocPtr *doc, xmlNodePtr *root)
178 {
179     if ((*doc = xmlParseMemory(data, len))) {
180         *root = xmlDocGetRootElement(*doc);
181         if (!*root) {
182             xmlFreeDoc(*doc);
183             *doc = NULL;
184             g_warning("Given memory is an empty document");
185         } else {
186             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
187                 xmlFreeDoc(*doc);
188                 *doc = NULL;
189                 g_warning("Document in given memory is of wrong type. root "
190                           "node is not '%s'", rootname);
191             }
192         }
193     }
194     if (!*doc)
195         return FALSE;
196     return TRUE;
197 }
198
199 void parse_close(xmlDocPtr doc)
200 {
201     xmlFreeDoc(doc);
202 }
203
204 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
205 {
206     while (node) {
207         struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
208
209         if (c)
210             c->func(i, doc, node, c->data);
211
212         node = node->next;
213     }
214 }
215
216 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
217 {
218     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
219     gchar *s = g_strdup(c ? (gchar*)c : "");
220     xmlFree(c);
221     return s;
222 }
223
224 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
225 {
226     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
227     gint i = atoi((gchar*)c);
228     xmlFree(c);
229     return i;
230 }
231
232 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
233 {
234     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
235     gboolean b = FALSE;
236     if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
237         b = TRUE;
238     else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
239         b = TRUE;
240     else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
241         b = TRUE;
242     xmlFree(c);
243     return b;
244 }
245
246 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
247 {
248     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
249     gboolean r;
250     r = !xmlStrcasecmp(c, (const xmlChar*) val);
251     xmlFree(c);
252     return r;
253 }
254
255 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
256 {
257     while (node) {
258         if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
259             return node;
260         node = node->next;
261     }
262     return NULL;
263 }
264
265 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
266 {
267     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
268     gboolean r = FALSE;
269     if (c) {
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;
282     }
283     xmlFree(c);
284     return r;
285 }
286
287 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
288 {
289     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
290     gboolean r = FALSE;
291     if (c) {
292         *value = atoi((gchar*)c);
293         r = TRUE;
294     }
295     xmlFree(c);
296     return r;
297 }
298
299 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
300 {
301     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
302     gboolean r = FALSE;
303     if (c) {
304         *value = g_strdup((gchar*)c);
305         r = TRUE;
306     }
307     xmlFree(c);
308     return r;
309 }
310
311 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
312                              const gchar *name)
313 {
314     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
315     gboolean r = FALSE;
316     if (c)
317         r = !xmlStrcasecmp(c, (const xmlChar*) val);
318     xmlFree(c);
319     return r;
320 }
321
322 static gint slist_path_cmp(const gchar *a, const gchar *b)
323 {
324     return strcmp(a, b);
325 }
326
327 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
328
329 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
330 {
331     g_assert(func);
332
333     if (!data)
334         return list;
335
336     if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
337         list = func(list, data);
338
339     return list;
340 }
341
342 static GSList* split_paths(const gchar *paths)
343 {
344     GSList *list = NULL;
345     gchar **spl, **it;
346
347     if (!paths)
348         return NULL;
349     spl = g_strsplit(paths, ":", -1);
350     for (it = spl; *it; ++it)
351         list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
352     g_free(spl);
353     return list;
354 }
355
356 void parse_paths_startup()
357 {
358     const gchar *path;
359
360     if (xdg_start)
361         return;
362     xdg_start = TRUE;
363
364     path = g_getenv("XDG_CONFIG_HOME");
365     if (path && path[0] != '\0') /* not unset or empty */
366         xdg_config_home_path = g_build_filename(path, NULL);
367     else
368         xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
369                                                 NULL);
370
371     path = g_getenv("XDG_DATA_HOME");
372     if (path && path[0] != '\0') /* not unset or empty */
373         xdg_data_home_path = g_build_filename(path, NULL);
374     else
375         xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
376                                               "share", NULL);
377
378     path = g_getenv("XDG_CONFIG_DIRS");
379     if (path && path[0] != '\0') /* not unset or empty */
380         xdg_config_dir_paths = split_paths(path);
381     else {
382         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
383                                               g_build_filename
384                                               (G_DIR_SEPARATOR_S,
385                                                "etc", "xdg", NULL),
386                                               (GSListFunc) g_slist_append);
387         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
388                                               g_strdup(CONFIGDIR),
389                                               (GSListFunc) g_slist_append);
390     }
391     xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
392                                           xdg_config_home_path,
393                                           (GSListFunc) g_slist_prepend);
394     
395     path = g_getenv("XDG_DATA_DIRS");
396     if (path && path[0] != '\0') /* not unset or empty */
397         xdg_data_dir_paths = split_paths(path);
398     else {
399         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
400                                             g_build_filename
401                                             (G_DIR_SEPARATOR_S,
402                                              "usr", "local", "share", NULL),
403                                             (GSListFunc) g_slist_append);
404         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
405                                             g_build_filename
406                                             (G_DIR_SEPARATOR_S,
407                                              "usr", "share", NULL),
408                                             (GSListFunc) g_slist_append);
409         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
410                                             g_strdup(DATADIR),
411                                             (GSListFunc) g_slist_append);
412     }
413     xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
414                                         xdg_data_home_path,
415                                         (GSListFunc) g_slist_prepend);
416 }
417
418 void parse_paths_shutdown()
419 {
420     GSList *it;
421
422     if (!xdg_start)
423         return;
424     xdg_start = FALSE;
425
426     for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
427         g_free(it->data);
428     g_slist_free(xdg_config_dir_paths);
429     xdg_config_dir_paths = NULL;
430     for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
431         g_free(it->data);
432     g_slist_free(xdg_data_dir_paths);
433     xdg_data_dir_paths = NULL;
434     g_free(xdg_config_home_path);
435     xdg_config_home_path = NULL;
436     g_free(xdg_data_home_path);
437     xdg_data_home_path = NULL;
438 }
439
440 gchar *parse_expand_tilde(const gchar *f)
441 {
442     gchar **spl;
443     gchar *ret;
444
445     if (!f)
446         return NULL;
447     spl = g_strsplit(f, "~", 0);
448     ret = g_strjoinv(g_get_home_dir(), spl);
449     g_strfreev(spl);
450     return ret;
451 }
452
453 gboolean parse_mkdir(const gchar *path, gint mode)
454 {
455     gboolean ret = TRUE;
456
457     g_return_val_if_fail(path != NULL, FALSE);
458     g_return_val_if_fail(path[0] != '\0', FALSE);
459
460     if (!g_file_test(path, G_FILE_TEST_IS_DIR))
461         if (mkdir(path, mode) == -1)
462             ret = FALSE;
463
464     return ret;
465 }
466
467 gboolean parse_mkdir_path(const gchar *path, gint mode)
468 {
469     gboolean ret = TRUE;
470
471     g_return_val_if_fail(path != NULL, FALSE);
472     g_return_val_if_fail(path[0] == '/', FALSE);
473
474     if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
475         gchar *c, *e;
476
477         c = g_strdup(path);
478         e = c;
479         while ((e = strchr(e + 1, '/'))) {
480             *e = '\0';
481             if (!(ret = parse_mkdir(c, mode)))
482                 goto parse_mkdir_path_end;
483             *e = '/';
484         }
485         ret = parse_mkdir(c, mode);
486
487     parse_mkdir_path_end:
488         g_free(c);
489     }
490
491     return ret;
492 }
493
494 const gchar* parse_xdg_config_home_path()
495 {
496     return xdg_config_home_path;
497 }
498
499 const gchar* parse_xdg_data_home_path()
500 {
501     return xdg_data_home_path;
502 }
503
504 GSList* parse_xdg_config_dir_paths()
505 {
506     return xdg_config_dir_paths;
507 }
508
509 GSList* parse_xdg_data_dir_paths()
510 {
511     return xdg_data_dir_paths;
512 }