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