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