]> icculus.org git repositories - dana/openbox.git/blob - parser/parse.c
look in sysconfdir before looking in /etc/xdg. this kinda violates the spec, i think...
[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(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root,
83                        gchar **fileused)
84 {
85     GSList *it;
86     gboolean r = FALSE;
87
88     *fileused = NULL;
89
90     if (file) {
91         if ((r = parse_load(file, "openbox_config", doc, root)))
92             *fileused = g_strdup(file);
93     } else {
94         /* this won't run if the above code loaded a config */
95         for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
96             gchar *path;
97
98             path = g_build_filename(it->data, "openbox", "rc.xml", NULL);
99             if ((r = parse_load(path, "openbox_config", doc, root)))
100                 *fileused = path;
101             else
102                 g_free(path);
103         }
104     }
105     if (!r)
106         g_warning("Unable to find a valid config file, using defaults");
107     return r;
108 }
109
110 gboolean parse_load_theme(const gchar *name, xmlDocPtr *doc, xmlNodePtr *root,
111                           gchar **retpath)
112 {
113     GSList *it;
114     gchar *path;
115     gboolean r = FALSE;
116
117     /* backward compatibility.. */
118     path = g_build_filename(g_get_home_dir(), ".themes", name,
119                             "openbox-3", "themerc.xml", NULL);
120     if ((r = parse_load(path, "openbox_theme", doc, root)))
121         *retpath = g_path_get_dirname(path);
122     g_free(path);
123
124     if (!r) {
125         for (it = xdg_data_dir_paths; !r && it; it = g_slist_next(it)) {
126             path = g_build_filename(it->data, "themes", name, "openbox-3",
127                                     "themerc.xml", NULL);
128             if ((r = parse_load(path, "openbox_theme", doc, root)))
129                 *retpath = g_path_get_dirname(path);
130             g_free(path);
131         }
132     }
133     if (!r)
134         g_warning("Unable to load the theme %s", name);
135     return r;
136 }
137
138 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
139 {
140     GSList *it;
141     gchar *path;
142     gboolean r = FALSE;
143
144     if (file[0] == '/') {
145         r = parse_load(file, "openbox_menu", doc, root);
146     } else {
147         for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
148             path = g_build_filename(it->data, "openbox", file, NULL);
149             r = parse_load(path, "openbox_menu", doc, root);
150             g_free(path);
151         }
152     }
153     if (!r)
154         g_warning("Unable to find a valid menu file '%s'", file);
155     return r;
156 }
157
158 gboolean parse_load(const gchar *path, const gchar *rootname,
159                     xmlDocPtr *doc, xmlNodePtr *root)
160 {
161     struct stat s;
162     if (stat(path, &s) < 0)
163         return FALSE;
164
165     /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
166        without this option, the tree is weird and has extra nodes in it. */
167     if ((*doc = xmlReadFile(path, NULL,
168                             XML_PARSE_NOBLANKS | XML_PARSE_RECOVER))) {
169         *root = xmlDocGetRootElement(*doc);
170         if (!*root) {
171             xmlFreeDoc(*doc);
172             *doc = NULL;
173             g_warning("%s is an empty document", path);
174         } else {
175             if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
176                 xmlFreeDoc(*doc);
177                 *doc = NULL;
178                 g_warning("Document %s is of wrong type. root node is "
179                           "not '%s'", path, rootname);
180             }
181         }
182     }
183     if (!*doc)
184         return FALSE;
185     return TRUE;
186 }
187
188 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
189                         xmlDocPtr *doc, xmlNodePtr *root)
190 {
191     if ((*doc = xmlParseMemory(data, len))) {
192         *root = xmlDocGetRootElement(*doc);
193         if (!*root) {
194             xmlFreeDoc(*doc);
195             *doc = NULL;
196             g_warning("Given memory is an empty document");
197         } else {
198             if (xmlStrcmp((*root)->name, (const xmlChar*)rootname)) {
199                 xmlFreeDoc(*doc);
200                 *doc = NULL;
201                 g_warning("Document in given memory is of wrong type. root "
202                           "node is not '%s'", rootname);
203             }
204         }
205     }
206     if (!*doc)
207         return FALSE;
208     return TRUE;
209 }
210
211 void parse_close(xmlDocPtr doc)
212 {
213     xmlFreeDoc(doc);
214 }
215
216 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
217 {
218     while (node) {
219         struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
220
221         if (c)
222             c->func(i, doc, node, c->data);
223
224         node = node->next;
225     }
226 }
227
228 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
229 {
230     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
231     gchar *s = g_strdup(c ? (gchar*)c : "");
232     xmlFree(c);
233     return s;
234 }
235
236 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
237 {
238     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
239     gint i = atoi((gchar*)c);
240     xmlFree(c);
241     return i;
242 }
243
244 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
245 {
246     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
247     gboolean b = FALSE;
248     if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
249         b = TRUE;
250     else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
251         b = TRUE;
252     else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
253         b = TRUE;
254     xmlFree(c);
255     return b;
256 }
257
258 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
259 {
260     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
261     gboolean r;
262     r = !xmlStrcasecmp(c, (const xmlChar*) val);
263     xmlFree(c);
264     return r;
265 }
266
267 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
268 {
269     while (node) {
270         if (!xmlStrcmp(node->name, (const xmlChar*) tag))
271             return node;
272         node = node->next;
273     }
274     return NULL;
275 }
276
277 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
278 {
279     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
280     gboolean r = FALSE;
281     if (c) {
282         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
283             *value = TRUE, r = TRUE;
284         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
285             *value = TRUE, r = TRUE;
286         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
287             *value = TRUE, r = TRUE;
288         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
289             *value = FALSE, r = TRUE;
290         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
291             *value = FALSE, r = TRUE;
292         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
293             *value = FALSE, r = TRUE;
294     }
295     xmlFree(c);
296     return r;
297 }
298
299 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
300 {
301     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
302     gboolean r = FALSE;
303     if (c) {
304         *value = atoi((gchar*)c);
305         r = TRUE;
306     }
307     xmlFree(c);
308     return r;
309 }
310
311 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
312 {
313     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
314     gboolean r = FALSE;
315     if (c) {
316         *value = g_strdup((gchar*)c);
317         r = TRUE;
318     }
319     xmlFree(c);
320     return r;
321 }
322
323 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
324                              const gchar *name)
325 {
326     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
327     gboolean r = FALSE;
328     if (c)
329         r = !xmlStrcasecmp(c, (const xmlChar*) val);
330     xmlFree(c);
331     return r;
332 }
333
334 static gint slist_path_cmp(const gchar *a, const gchar *b)
335 {
336     return strcmp(a, b);
337 }
338
339 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
340
341 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
342 {
343     g_assert(func);
344
345     if (!data)
346         return list;
347
348     if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
349         list = func(list, data);
350     else
351         g_free(data);
352
353     return list;
354 }
355
356 static GSList* split_paths(const gchar *paths)
357 {
358     GSList *list = NULL;
359     gchar **spl, **it;
360
361     if (!paths)
362         return NULL;
363     spl = g_strsplit(paths, ":", -1);
364     for (it = spl; *it; ++it)
365         list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
366     g_free(spl);
367     return list;
368 }
369
370 void parse_paths_startup()
371 {
372     const gchar *path;
373
374     if (xdg_start)
375         return;
376     xdg_start = TRUE;
377
378     path = g_getenv("XDG_CONFIG_HOME");
379     if (path && path[0] != '\0') /* not unset or empty */
380         xdg_config_home_path = g_build_filename(path, NULL);
381     else
382         xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
383                                                 NULL);
384
385     path = g_getenv("XDG_DATA_HOME");
386     if (path && path[0] != '\0') /* not unset or empty */
387         xdg_data_home_path = g_build_filename(path, NULL);
388     else
389         xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
390                                               "share", NULL);
391
392     path = g_getenv("XDG_CONFIG_DIRS");
393     if (path && path[0] != '\0') /* not unset or empty */
394         xdg_config_dir_paths = split_paths(path);
395     else {
396         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
397                                               g_strdup(CONFIGDIR),
398                                               (GSListFunc) g_slist_append);
399         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
400                                               g_build_filename
401                                               (G_DIR_SEPARATOR_S,
402                                                "etc", "xdg", NULL),
403                                               (GSListFunc) g_slist_append);
404     }
405     xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
406                                           g_strdup(xdg_config_home_path),
407                                           (GSListFunc) g_slist_prepend);
408     
409     path = g_getenv("XDG_DATA_DIRS");
410     if (path && path[0] != '\0') /* not unset or empty */
411         xdg_data_dir_paths = split_paths(path);
412     else {
413         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
414                                             g_build_filename
415                                             (G_DIR_SEPARATOR_S,
416                                              "usr", "local", "share", NULL),
417                                             (GSListFunc) g_slist_append);
418         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
419                                             g_build_filename
420                                             (G_DIR_SEPARATOR_S,
421                                              "usr", "share", NULL),
422                                             (GSListFunc) g_slist_append);
423         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
424                                             g_strdup(DATADIR),
425                                             (GSListFunc) g_slist_append);
426     }
427     xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
428                                         g_strdup(xdg_data_home_path),
429                                         (GSListFunc) g_slist_prepend);
430 }
431
432 void parse_paths_shutdown()
433 {
434     GSList *it;
435
436     if (!xdg_start)
437         return;
438     xdg_start = FALSE;
439
440     for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
441         g_free(it->data);
442     g_slist_free(xdg_config_dir_paths);
443     xdg_config_dir_paths = NULL;
444     for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
445         g_free(it->data);
446     g_slist_free(xdg_data_dir_paths);
447     xdg_data_dir_paths = NULL;
448     g_free(xdg_config_home_path);
449     xdg_config_home_path = NULL;
450     g_free(xdg_data_home_path);
451     xdg_data_home_path = NULL;
452 }
453
454 gchar *parse_expand_tilde(const gchar *f)
455 {
456     gchar **spl;
457     gchar *ret;
458
459     if (!f)
460         return NULL;
461     spl = g_strsplit(f, "~", 0);
462     ret = g_strjoinv(g_get_home_dir(), spl);
463     g_strfreev(spl);
464     return ret;
465 }
466
467 gboolean parse_mkdir(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] != '\0', FALSE);
473
474     if (!g_file_test(path, G_FILE_TEST_IS_DIR))
475         if (mkdir(path, mode) == -1)
476             ret = FALSE;
477
478     return ret;
479 }
480
481 gboolean parse_mkdir_path(const gchar *path, gint mode)
482 {
483     gboolean ret = TRUE;
484
485     g_return_val_if_fail(path != NULL, FALSE);
486     g_return_val_if_fail(path[0] == '/', FALSE);
487
488     if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
489         gchar *c, *e;
490
491         c = g_strdup(path);
492         e = c;
493         while ((e = strchr(e + 1, '/'))) {
494             *e = '\0';
495             if (!(ret = parse_mkdir(c, mode)))
496                 goto parse_mkdir_path_end;
497             *e = '/';
498         }
499         ret = parse_mkdir(c, mode);
500
501     parse_mkdir_path_end:
502         g_free(c);
503     }
504
505     return ret;
506 }
507
508 const gchar* parse_xdg_config_home_path()
509 {
510     return xdg_config_home_path;
511 }
512
513 const gchar* parse_xdg_data_home_path()
514 {
515     return xdg_data_home_path;
516 }
517
518 GSList* parse_xdg_config_dir_paths()
519 {
520     return xdg_config_dir_paths;
521 }
522
523 GSList* parse_xdg_data_dir_paths()
524 {
525     return xdg_data_dir_paths;
526 }