]> icculus.org git repositories - dana/openbox.git/blob - parser/parse.c
scary commit..but here goes.
[dana/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, XML_PARSE_NOBLANKS))) {
156         *root = xmlDocGetRootElement(*doc);
157         if (!*root) {
158             xmlFreeDoc(*doc);
159             *doc = NULL;
160             g_warning("%s is an empty document", path);
161         } else {
162             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
163                 xmlFreeDoc(*doc);
164                 *doc = NULL;
165                 g_warning("Document %s is of wrong type. root node is "
166                           "not '%s'", path, rootname);
167             }
168         }
169     }
170     if (!*doc)
171         return FALSE;
172     return TRUE;
173 }
174
175 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
176                         xmlDocPtr *doc, xmlNodePtr *root)
177 {
178     if ((*doc = xmlParseMemory(data, len))) {
179         *root = xmlDocGetRootElement(*doc);
180         if (!*root) {
181             xmlFreeDoc(*doc);
182             *doc = NULL;
183             g_warning("Given memory is an empty document");
184         } else {
185             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
186                 xmlFreeDoc(*doc);
187                 *doc = NULL;
188                 g_warning("Document in given memory is of wrong type. root "
189                           "node is not '%s'", rootname);
190             }
191         }
192     }
193     if (!*doc)
194         return FALSE;
195     return TRUE;
196 }
197
198 void parse_close(xmlDocPtr doc)
199 {
200     xmlFreeDoc(doc);
201 }
202
203 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
204 {
205     while (node) {
206         struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
207
208         if (c)
209             c->func(i, doc, node, c->data);
210
211         node = node->next;
212     }
213 }
214
215 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
216 {
217     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
218     gchar *s = g_strdup(c ? (gchar*)c : "");
219     xmlFree(c);
220     return s;
221 }
222
223 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
224 {
225     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
226     gint i = atoi((gchar*)c);
227     xmlFree(c);
228     return i;
229 }
230
231 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
232 {
233     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
234     gboolean b = FALSE;
235     if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
236         b = TRUE;
237     else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
238         b = TRUE;
239     else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
240         b = TRUE;
241     xmlFree(c);
242     return b;
243 }
244
245 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
246 {
247     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
248     gboolean r;
249     r = !xmlStrcasecmp(c, (const xmlChar*) val);
250     xmlFree(c);
251     return r;
252 }
253
254 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
255 {
256     while (node) {
257         if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
258             return node;
259         node = node->next;
260     }
261     return NULL;
262 }
263
264 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
265 {
266     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
267     gboolean r = FALSE;
268     if (c) {
269         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
270             *value = TRUE, r = TRUE;
271         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
272             *value = TRUE, r = TRUE;
273         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
274             *value = TRUE, r = TRUE;
275         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
276             *value = FALSE, r = TRUE;
277         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
278             *value = FALSE, r = TRUE;
279         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
280             *value = FALSE, r = TRUE;
281     }
282     xmlFree(c);
283     return r;
284 }
285
286 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
287 {
288     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
289     gboolean r = FALSE;
290     if (c) {
291         *value = atoi((gchar*)c);
292         r = TRUE;
293     }
294     xmlFree(c);
295     return r;
296 }
297
298 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
299 {
300     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
301     gboolean r = FALSE;
302     if (c) {
303         *value = g_strdup((gchar*)c);
304         r = TRUE;
305     }
306     xmlFree(c);
307     return r;
308 }
309
310 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
311                              const gchar *name)
312 {
313     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
314     gboolean r = FALSE;
315     if (c)
316         r = !xmlStrcasecmp(c, (const xmlChar*) val);
317     xmlFree(c);
318     return r;
319 }
320
321 static gint slist_path_cmp(const gchar *a, const gchar *b)
322 {
323     return strcmp(a, b);
324 }
325
326 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
327
328 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
329 {
330     g_assert(func);
331
332     if (!data)
333         return list;
334
335     if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
336         list = func(list, data);
337
338     return list;
339 }
340
341 static GSList* split_paths(const gchar *paths)
342 {
343     GSList *list = NULL;
344     gchar **spl, **it;
345
346     if (!paths)
347         return NULL;
348     spl = g_strsplit(paths, ":", -1);
349     for (it = spl; *it; ++it)
350         list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
351     g_free(spl);
352     return list;
353 }
354
355 void parse_paths_startup()
356 {
357     const gchar *path;
358
359     if (xdg_start)
360         return;
361     xdg_start = TRUE;
362
363     path = g_getenv("XDG_CONFIG_HOME");
364     if (path && path[0] != '\0') /* not unset or empty */
365         xdg_config_home_path = g_build_filename(path, NULL);
366     else
367         xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
368                                                 NULL);
369
370     path = g_getenv("XDG_DATA_HOME");
371     if (path && path[0] != '\0') /* not unset or empty */
372         xdg_data_home_path = g_build_filename(path, NULL);
373     else
374         xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
375                                               "share", NULL);
376
377     path = g_getenv("XDG_CONFIG_DIRS");
378     if (path && path[0] != '\0') /* not unset or empty */
379         xdg_config_dir_paths = split_paths(path);
380     else {
381         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
382                                               g_build_filename
383                                               (G_DIR_SEPARATOR_S,
384                                                "etc", "xdg", NULL),
385                                               (GSListFunc) g_slist_append);
386         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
387                                               g_strdup(CONFIGDIR),
388                                               (GSListFunc) g_slist_append);
389     }
390     xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
391                                           xdg_config_home_path,
392                                           (GSListFunc) g_slist_prepend);
393     
394     path = g_getenv("XDG_DATA_DIRS");
395     if (path && path[0] != '\0') /* not unset or empty */
396         xdg_data_dir_paths = split_paths(path);
397     else {
398         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
399                                             g_build_filename
400                                             (G_DIR_SEPARATOR_S,
401                                              "usr", "local", "share", NULL),
402                                             (GSListFunc) g_slist_append);
403         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
404                                             g_build_filename
405                                             (G_DIR_SEPARATOR_S,
406                                              "usr", "share", NULL),
407                                             (GSListFunc) g_slist_append);
408         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
409                                             g_strdup(DATADIR),
410                                             (GSListFunc) g_slist_append);
411     }
412     xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
413                                         xdg_data_home_path,
414                                         (GSListFunc) g_slist_prepend);
415 }
416
417 void parse_paths_shutdown()
418 {
419     GSList *it;
420
421     if (!xdg_start)
422         return;
423     xdg_start = FALSE;
424
425     for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
426         g_free(it->data);
427     g_slist_free(xdg_config_dir_paths);
428     xdg_config_dir_paths = NULL;
429     for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
430         g_free(it->data);
431     g_slist_free(xdg_data_dir_paths);
432     xdg_data_dir_paths = NULL;
433     g_free(xdg_config_home_path);
434     xdg_config_home_path = NULL;
435     g_free(xdg_data_home_path);
436     xdg_data_home_path = NULL;
437 }
438
439 gchar *parse_expand_tilde(const gchar *f)
440 {
441     gchar **spl;
442     gchar *ret;
443
444     if (!f)
445         return NULL;
446     spl = g_strsplit(f, "~", 0);
447     ret = g_strjoinv(g_get_home_dir(), spl);
448     g_strfreev(spl);
449     return ret;
450 }
451
452 gboolean parse_mkdir(const gchar *path, gint mode)
453 {
454     gboolean ret = TRUE;
455
456     g_return_val_if_fail(path != NULL, FALSE);
457     g_return_val_if_fail(path[0] != '\0', FALSE);
458
459     if (!g_file_test(path, G_FILE_TEST_IS_DIR))
460         if (mkdir(path, mode) == -1)
461             ret = FALSE;
462
463     return ret;
464 }
465
466 gboolean parse_mkdir_path(const gchar *path, gint mode)
467 {
468     gboolean ret = TRUE;
469
470     g_return_val_if_fail(path != NULL, FALSE);
471     g_return_val_if_fail(path[0] == '/', FALSE);
472
473     if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
474         gchar *c, *e;
475
476         c = g_strdup(path);
477         e = c;
478         while ((e = strchr(e + 1, '/'))) {
479             *e = '\0';
480             if (!(ret = parse_mkdir(c, mode)))
481                 goto parse_mkdir_path_end;
482             *e = '/';
483         }
484         ret = parse_mkdir(c, mode);
485
486     parse_mkdir_path_end:
487         g_free(c);
488     }
489
490     return ret;
491 }
492
493 const gchar* parse_xdg_config_home_path()
494 {
495     return xdg_config_home_path;
496 }
497
498 const gchar* parse_xdg_data_home_path()
499 {
500     return xdg_data_home_path;
501 }
502
503 GSList* parse_xdg_config_dir_paths()
504 {
505     return xdg_config_dir_paths;
506 }
507
508 GSList* parse_xdg_data_dir_paths()
509 {
510     return xdg_data_dir_paths;
511 }