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