add parse_attr_bool, and fix a possible segfault
[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        Ben 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
26 static gboolean xdg_start;
27 static gchar   *xdg_config_home_path;
28 static gchar   *xdg_data_home_path;
29 static GSList  *xdg_config_dir_paths;
30 static GSList  *xdg_data_dir_paths;
31
32 struct Callback {
33     gchar *tag;
34     ParseCallback func;
35     gpointer data;
36 };
37
38 struct _ObParseInst {
39     GHashTable *callbacks;
40 };
41
42 static void destfunc(struct Callback *c)
43 {
44     g_free(c->tag);
45     g_free(c);
46 }
47
48 ObParseInst* parse_startup()
49 {
50     ObParseInst *i = g_new(ObParseInst, 1);
51     i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
52                                          (GDestroyNotify)destfunc);
53     return i;
54 }
55
56 void parse_shutdown(ObParseInst *i)
57 {
58     if (i) {
59         g_hash_table_destroy(i->callbacks);
60         g_free(i);
61     }
62 }
63
64 void parse_register(ObParseInst *i, const gchar *tag,
65                     ParseCallback func, gpointer data)
66 {
67     struct Callback *c;
68
69     if ((c = g_hash_table_lookup(i->callbacks, tag))) {
70         g_warning("tag '%s' already registered", tag);
71         return;
72     }
73
74     c = g_new(struct Callback, 1);
75     c->tag = g_strdup(tag);
76     c->func = func;
77     c->data = data;
78     g_hash_table_insert(i->callbacks, c->tag, c);
79 }
80
81 gboolean parse_load_rc(xmlDocPtr *doc, xmlNodePtr *root)
82 {
83     GSList *it;
84     gchar *path;
85     gboolean r = FALSE;
86
87     for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
88         path = g_build_filename(it->data, "openbox", "rc.xml", NULL);
89         r = parse_load(path, "openbox_config", doc, root);
90         g_free(path);
91     }
92     if (!r)
93         g_warning("unable to find a valid config file, using defaults");
94     return r;
95 }
96
97 gboolean parse_load_menu(const gchar *file, xmlDocPtr *doc, xmlNodePtr *root)
98 {
99     GSList *it;
100     gchar *path;
101     gboolean r = FALSE;
102
103     if (file[0] == '/') {
104         r = parse_load(file, "openbox_menu", doc, root);
105     } else {
106         for (it = xdg_config_dir_paths; !r && it; it = g_slist_next(it)) {
107             path = g_build_filename(it->data, "openbox", file, NULL);
108             r = parse_load(path, "openbox_menu", doc, root);
109             g_free(path);
110         }
111     }
112     if (!r)
113         g_warning("unable to find a valid menu file '%s'", file);
114     return r;
115 }
116
117 gboolean parse_load(const gchar *path, const gchar *rootname,
118                     xmlDocPtr *doc, xmlNodePtr *root)
119 {
120     if ((*doc = xmlParseFile(path))) {
121         *root = xmlDocGetRootElement(*doc);
122         if (!*root) {
123             xmlFreeDoc(*doc);
124             *doc = NULL;
125             g_warning("%s is an empty document", path);
126         } else {
127             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
128                 xmlFreeDoc(*doc);
129                 *doc = NULL;
130                 g_warning("document %s is of wrong type. root node is "
131                           "not '%s'", path, rootname);
132             }
133         }
134     }
135     if (!*doc)
136         return FALSE;
137     return TRUE;
138 }
139
140 gboolean parse_load_mem(gpointer data, guint len, const gchar *rootname,
141                         xmlDocPtr *doc, xmlNodePtr *root)
142 {
143     if ((*doc = xmlParseMemory(data, len))) {
144         *root = xmlDocGetRootElement(*doc);
145         if (!*root) {
146             xmlFreeDoc(*doc);
147             *doc = NULL;
148             g_warning("Given memory is an empty document");
149         } else {
150             if (xmlStrcasecmp((*root)->name, (const xmlChar*)rootname)) {
151                 xmlFreeDoc(*doc);
152                 *doc = NULL;
153                 g_warning("document in given memory is of wrong type. root "
154                           "node is not '%s'", rootname);
155             }
156         }
157     }
158     if (!*doc)
159         return FALSE;
160     return TRUE;
161 }
162
163 void parse_close(xmlDocPtr doc)
164 {
165     xmlFreeDoc(doc);
166 }
167
168 void parse_tree(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node)
169 {
170     while (node) {
171         struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
172
173         if (c)
174             c->func(i, doc, node, c->data);
175
176         node = node->next;
177     }
178 }
179
180 gchar *parse_string(xmlDocPtr doc, xmlNodePtr node)
181 {
182     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
183     gchar *s = g_strdup(c ? (gchar*)c : "");
184     xmlFree(c);
185     return s;
186 }
187
188 gint parse_int(xmlDocPtr doc, xmlNodePtr node)
189 {
190     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
191     gint i = atoi((gchar*)c);
192     xmlFree(c);
193     return i;
194 }
195
196 gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
197 {
198     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
199     gboolean b = FALSE;
200     if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
201         b = TRUE;
202     else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
203         b = TRUE;
204     else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
205         b = TRUE;
206     xmlFree(c);
207     return b;
208 }
209
210 gboolean parse_contains(const gchar *val, xmlDocPtr doc, xmlNodePtr node)
211 {
212     xmlChar *c = xmlNodeListGetString(doc, node->children, TRUE);
213     gboolean r;
214     r = !xmlStrcasecmp(c, (const xmlChar*) val);
215     xmlFree(c);
216     return r;
217 }
218
219 xmlNodePtr parse_find_node(const gchar *tag, xmlNodePtr node)
220 {
221     while (node) {
222         if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
223             return node;
224         node = node->next;
225     }
226     return NULL;
227 }
228
229 gboolean parse_attr_bool(const gchar *name, xmlNodePtr node, gboolean *value)
230 {
231     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
232     gboolean r = FALSE;
233     if (c) {
234         if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
235             *value = TRUE, r = TRUE;
236         else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
237             *value = TRUE, r = TRUE;
238         else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
239             *value = TRUE, r = TRUE;
240         else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
241             *value = FALSE, r = TRUE;
242         else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
243             *value = FALSE, r = TRUE;
244         else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
245             *value = FALSE, r = TRUE;
246     }
247     xmlFree(c);
248     return r;
249 }
250
251 gboolean parse_attr_int(const gchar *name, xmlNodePtr node, gint *value)
252 {
253     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
254     gboolean r = FALSE;
255     if (c) {
256         *value = atoi((gchar*)c);
257         r = TRUE;
258     }
259     xmlFree(c);
260     return r;
261 }
262
263 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
264 {
265     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
266     gboolean r = FALSE;
267     if (c) {
268         *value = g_strdup((gchar*)c);
269         r = TRUE;
270     }
271     xmlFree(c);
272     return r;
273 }
274
275 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
276                              const gchar *name)
277 {
278     xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
279     gboolean r = FALSE;
280     if (c)
281         r = !xmlStrcasecmp(c, (const xmlChar*) val);
282     xmlFree(c);
283     return r;
284 }
285
286 static gint slist_path_cmp(const gchar *a, const gchar *b)
287 {
288     return strcmp(a, b);
289 }
290
291 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
292
293 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
294 {
295     g_assert(func);
296
297     if (!data)
298         return list;
299
300     if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
301         list = func(list, data);
302
303     return list;
304 }
305
306 static GSList* split_paths(const gchar *paths)
307 {
308     GSList *list = NULL;
309     gchar **spl, **it;
310
311     if (!paths)
312         return NULL;
313     spl = g_strsplit(paths, ":", -1);
314     for (it = spl; *it; ++it)
315         list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
316     g_free(spl);
317     return list;
318 }
319
320 void parse_paths_startup()
321 {
322     const gchar *path;
323
324     if (xdg_start)
325         return;
326     xdg_start = TRUE;
327
328     path = g_getenv("XDG_CONFIG_HOME");
329     if (path && path[0] != '\0') /* not unset or empty */
330         xdg_config_home_path = g_build_filename(path, NULL);
331     else
332         xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
333                                                 NULL);
334
335     path = g_getenv("XDG_DATA_HOME");
336     if (path && path[0] != '\0') /* not unset or empty */
337         xdg_data_home_path = g_build_filename(path, NULL);
338     else
339         xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
340                                               "share", NULL);
341
342     path = g_getenv("XDG_CONFIG_DIRS");
343     if (path && path[0] != '\0') /* not unset or empty */
344         xdg_config_dir_paths = split_paths(path);
345     else {
346         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
347                                               g_build_filename
348                                               (G_DIR_SEPARATOR_S,
349                                                "etc", "xdg", NULL),
350                                               (GSListFunc) g_slist_append);
351         xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
352                                               g_strdup(CONFIGDIR),
353                                               (GSListFunc) g_slist_append);
354     }
355     xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
356                                           xdg_config_home_path,
357                                           (GSListFunc) g_slist_prepend);
358     
359     path = g_getenv("XDG_DATA_DIRS");
360     if (path && path[0] != '\0') /* not unset or empty */
361         xdg_data_dir_paths = split_paths(path);
362     else {
363         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
364                                             g_build_filename
365                                             (G_DIR_SEPARATOR_S,
366                                              "usr", "local", "share", NULL),
367                                             (GSListFunc) g_slist_append);
368         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
369                                             g_build_filename
370                                             (G_DIR_SEPARATOR_S,
371                                              "usr", "share", NULL),
372                                             (GSListFunc) g_slist_append);
373         xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
374                                             g_strdup(DATADIR),
375                                             (GSListFunc) g_slist_append);
376     }
377     xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
378                                         xdg_data_home_path,
379                                         (GSListFunc) g_slist_prepend);
380 }
381
382 void parse_paths_shutdown()
383 {
384     GSList *it;
385
386     if (!xdg_start)
387         return;
388     xdg_start = FALSE;
389
390     for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
391         g_free(it->data);
392     g_slist_free(xdg_config_dir_paths);
393     xdg_config_dir_paths = NULL;
394     for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
395         g_free(it->data);
396     g_slist_free(xdg_data_dir_paths);
397     xdg_data_dir_paths = NULL;
398 }
399
400 gchar *parse_expand_tilde(const gchar *f)
401 {
402     gchar **spl;
403     gchar *ret;
404
405     if (!f)
406         return NULL;
407     spl = g_strsplit(f, "~", 0);
408     ret = g_strjoinv(g_get_home_dir(), spl);
409     g_strfreev(spl);
410     return ret;
411 }
412
413 gboolean parse_mkdir(const gchar *path, gint mode)
414 {
415     gboolean ret = TRUE;
416
417     g_return_val_if_fail(path != NULL, FALSE);
418     g_return_val_if_fail(path[0] != '\0', FALSE);
419
420     if (!g_file_test(path, G_FILE_TEST_IS_DIR))
421         if (mkdir(path, mode) == -1)
422             ret = FALSE;
423
424     return ret;
425 }
426
427 gboolean parse_mkdir_path(const gchar *path, gint mode)
428 {
429     gboolean ret = TRUE;
430
431     g_return_val_if_fail(path != NULL, FALSE);
432     g_return_val_if_fail(path[0] == '/', FALSE);
433
434     if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
435         gchar *c, *e;
436
437         c = g_strdup(path);
438         e = c;
439         while ((e = strchr(e + 1, '/'))) {
440             *e = '\0';
441             if (!(ret = parse_mkdir(c, mode)))
442                 goto parse_mkdir_path_end;
443             *e = '/';
444         }
445         ret = parse_mkdir(c, mode);
446
447     parse_mkdir_path_end:
448         g_free(c);
449     }
450
451     return ret;
452 }
453
454 const gchar* parse_xdg_config_home_path()
455 {
456     return xdg_config_home_path;
457 }
458
459 const gchar* parse_xdg_data_home_path()
460 {
461     return xdg_data_home_path;
462 }
463
464 GSList* parse_xdg_config_dir_paths()
465 {
466     return xdg_config_dir_paths;
467 }
468
469 GSList* parse_xdg_data_dir_paths()
470 {
471     return xdg_data_dir_paths;
472 }