]> Dogcows Code - chaz/openbox/blob - obt/parse.c
61fe5b821e3a09b02002b0a846925792a12a06aa
[chaz/openbox] / obt / parse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/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 "obt/parse.h"
20
21 #include <glib.h>
22
23 #ifdef HAVE_STRING_H
24 # include <string.h>
25 #endif
26 #ifdef HAVE_ERRNO_H
27 # include <errno.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 static gboolean xdg_start;
40 static gchar *xdg_config_home_path;
41 static gchar *xdg_data_home_path;
42 static GSList *xdg_config_dir_paths;
43 static GSList *xdg_data_dir_paths;
44
45 struct Callback {
46 gchar *tag;
47 ObtParseCallback func;
48 gpointer data;
49 };
50
51 struct _ObtParseInst {
52 gint ref;
53 GHashTable *callbacks;
54 xmlDocPtr doc;
55 xmlNodePtr root;
56 gchar *path;
57 };
58
59 static void destfunc(struct Callback *c)
60 {
61 g_free(c->tag);
62 g_free(c);
63 }
64
65 ObtParseInst* obt_parse_instance_new(void)
66 {
67 ObtParseInst *i = g_new(ObtParseInst, 1);
68 i->ref = 1;
69 i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
70 (GDestroyNotify)destfunc);
71 i->doc = NULL;
72 i->root = NULL;
73 i->path = NULL;
74 return i;
75 }
76
77 void obt_parse_instance_ref(ObtParseInst *i)
78 {
79 ++i->ref;
80 }
81
82 void obt_parse_instance_unref(ObtParseInst *i)
83 {
84 if (i && --i->ref == 0) {
85 g_hash_table_destroy(i->callbacks);
86 g_free(i);
87 }
88 }
89
90 xmlDocPtr obt_parse_instance_doc(ObtParseInst *i)
91 {
92 g_assert(i->doc); /* a doc is open? */
93 return i->doc;
94 }
95
96 xmlNodePtr obt_parse_instance_root(ObtParseInst *i)
97 {
98 g_assert(i->doc); /* a doc is open? */
99 return i->root;
100 }
101
102 void obt_parse_register(ObtParseInst *i, const gchar *tag,
103 ObtParseCallback func, gpointer data)
104 {
105 struct Callback *c;
106
107 if ((c = g_hash_table_lookup(i->callbacks, tag))) {
108 g_error("Tag '%s' already registered", tag);
109 return;
110 }
111
112 c = g_new(struct Callback, 1);
113 c->tag = g_strdup(tag);
114 c->func = func;
115 c->data = data;
116 g_hash_table_insert(i->callbacks, c->tag, c);
117 }
118
119 static gboolean load_file(ObtParseInst *i,
120 const gchar *domain,
121 const gchar *filename,
122 const gchar *root_node,
123 GSList *paths)
124 {
125 GSList *it;
126 gboolean r = FALSE;
127
128 g_assert(i->doc == NULL); /* another doc isn't open already? */
129
130 for (it = paths; !r && it; it = g_slist_next(it)) {
131 gchar *path;
132 struct stat s;
133
134 if (!domain && !filename) /* given a full path to the file */
135 path = g_strdup(it->data);
136 else
137 path = g_build_filename(it->data, domain, filename, NULL);
138
139 if (stat(path, &s) >= 0) {
140 /* XML_PARSE_BLANKS is needed apparently, or the tree can end up
141 with extra nodes in it. */
142 i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS |
143 XML_PARSE_RECOVER));
144 if (i->doc) {
145 i->root = xmlDocGetRootElement(i->doc);
146 if (!i->root) {
147 xmlFreeDoc(i->doc);
148 i->doc = NULL;
149 g_message("%s is an empty XML document", path);
150 }
151 else if (xmlStrcmp(i->root->name,
152 (const xmlChar*)root_node)) {
153 xmlFreeDoc(i->doc);
154 i->doc = NULL;
155 i->root = NULL;
156 g_message("XML document %s is of wrong type. Root "
157 "node is not '%s'", path, root_node);
158 }
159 else {
160 i->path = g_strdup(path);
161 r = TRUE; /* ok! */
162 }
163 }
164 }
165
166 g_free(path);
167 }
168
169 return r;
170 }
171
172 gboolean obt_parse_load_file(ObtParseInst *i,
173 const gchar *path,
174 const gchar *root_node)
175 {
176 GSList *paths;
177 gboolean r;
178
179 paths = g_slist_append(NULL, g_strdup(path));
180
181 r = load_file(i, NULL, NULL, root_node, paths);
182
183 while (paths) {
184 g_free(paths->data);
185 paths = g_slist_delete_link(paths, paths);
186 }
187 return r;
188 }
189
190 gboolean obt_parse_load_config_file(ObtParseInst *i,
191 const gchar *domain,
192 const gchar *filename,
193 const gchar *root_node)
194 {
195 GSList *it, *paths = NULL;
196 gboolean r;
197
198 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
199 paths = g_slist_append(paths, g_strdup(it->data));
200
201 r = load_file(i, domain, filename, root_node, paths);
202
203 while (paths) {
204 g_free(paths->data);
205 paths = g_slist_delete_link(paths, paths);
206 }
207 return r;
208 }
209
210 gboolean obt_parse_load_data_file(ObtParseInst *i,
211 const gchar *domain,
212 const gchar *filename,
213 const gchar *root_node)
214 {
215 GSList *it, *paths = NULL;
216 gboolean r;
217
218 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
219 paths = g_slist_append(paths, g_strdup(it->data));
220
221 r = load_file(i, domain, filename, root_node, paths);
222
223 while (paths) {
224 g_free(paths->data);
225 paths = g_slist_delete_link(paths, paths);
226 }
227 return r;
228 }
229
230 gboolean obt_parse_load_theme_file(ObtParseInst *i,
231 const gchar *theme,
232 const gchar *domain,
233 const gchar *filename,
234 const gchar *root_node)
235 {
236 GSList *it, *paths = NULL;
237 gboolean r;
238
239 /* use ~/.themes for backwards compatibility */
240 paths = g_slist_append
241 (paths, g_build_filename(g_get_home_dir(), ".themes", theme, NULL));
242
243 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
244 paths = g_slist_append
245 (paths, g_build_filename(it->data, "themes", theme, NULL));
246
247 r = load_file(i, domain, filename, root_node, paths);
248
249 while (paths) {
250 g_free(paths->data);
251 paths = g_slist_delete_link(paths, paths);
252 }
253 return r;
254 }
255
256
257 gboolean obt_parse_load_mem(ObtParseInst *i,
258 gpointer data, guint len, const gchar *root_node)
259 {
260 gboolean r = FALSE;
261
262 g_assert(i->doc == NULL); /* another doc isn't open already? */
263
264 i->doc = xmlParseMemory(data, len);
265 if (i) {
266 i->root = xmlDocGetRootElement(i->doc);
267 if (!i->root) {
268 xmlFreeDoc(i->doc);
269 i->doc = NULL;
270 g_message("Given memory is an empty document");
271 }
272 else if (xmlStrcmp(i->root->name, (const xmlChar*)root_node)) {
273 xmlFreeDoc(i->doc);
274 i->doc = NULL;
275 i->root = NULL;
276 g_message("XML Document in given memory is of wrong "
277 "type. Root node is not '%s'\n", root_node);
278 }
279 else
280 r = TRUE; /* ok ! */
281 }
282 return r;
283 }
284
285 void obt_parse_close(ObtParseInst *i)
286 {
287 if (i && i->doc) {
288 xmlFreeDoc(i->doc);
289 g_free(i->path);
290 i->doc = NULL;
291 i->root = NULL;
292 i->path = NULL;
293 }
294 }
295
296 void obt_parse_tree(ObtParseInst *i, xmlNodePtr node)
297 {
298 g_assert(i->doc); /* a doc is open? */
299
300 while (node) {
301 struct Callback *c = g_hash_table_lookup(i->callbacks, node->name);
302 if (c) c->func(node, c->data);
303 node = node->next;
304 }
305 }
306
307 gchar *obt_parse_node_string(xmlNodePtr node)
308 {
309 xmlChar *c = xmlNodeGetContent(node);
310 gchar *s = g_strdup(c ? (gchar*)c : "");
311 xmlFree(c);
312 return s;
313 }
314
315 gint obt_parse_node_int(xmlNodePtr node)
316 {
317 xmlChar *c = xmlNodeGetContent(node);
318 gint i = c ? atoi((gchar*)c) : 0;
319 xmlFree(c);
320 return i;
321 }
322
323 gboolean obt_parse_node_bool(xmlNodePtr node)
324 {
325 xmlChar *c = xmlNodeGetContent(node);
326 gboolean b = FALSE;
327 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
328 b = TRUE;
329 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
330 b = TRUE;
331 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
332 b = TRUE;
333 xmlFree(c);
334 return b;
335 }
336
337 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
338 {
339 xmlChar *c = xmlNodeGetContent(node);
340 gboolean r;
341 r = !xmlStrcasecmp(c, (const xmlChar*) val);
342 xmlFree(c);
343 return r;
344 }
345
346 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
347 {
348 while (node) {
349 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
350 return node;
351 node = node->next;
352 }
353 return NULL;
354 }
355
356 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
357 gboolean *value)
358 {
359 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
360 gboolean r = FALSE;
361 if (c) {
362 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
363 *value = TRUE, r = TRUE;
364 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
365 *value = TRUE, r = TRUE;
366 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
367 *value = TRUE, r = TRUE;
368 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
369 *value = FALSE, r = TRUE;
370 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
371 *value = FALSE, r = TRUE;
372 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
373 *value = FALSE, r = TRUE;
374 }
375 xmlFree(c);
376 return r;
377 }
378
379 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
380 {
381 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
382 gboolean r = FALSE;
383 if (c) {
384 *value = atoi((gchar*)c);
385 r = TRUE;
386 }
387 xmlFree(c);
388 return r;
389 }
390
391 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
392 gchar **value)
393 {
394 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
395 gboolean r = FALSE;
396 if (c) {
397 *value = g_strdup((gchar*)c);
398 r = TRUE;
399 }
400 xmlFree(c);
401 return r;
402 }
403
404 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
405 const gchar *val)
406 {
407 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
408 gboolean r = FALSE;
409 if (c)
410 r = !xmlStrcasecmp(c, (const xmlChar*) val);
411 xmlFree(c);
412 return r;
413 }
414
415 static gint slist_path_cmp(const gchar *a, const gchar *b)
416 {
417 return strcmp(a, b);
418 }
419
420 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
421
422 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
423 {
424 g_assert(func);
425
426 if (!data)
427 return list;
428
429 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
430 list = func(list, data);
431 else
432 g_free(data);
433
434 return list;
435 }
436
437 static GSList* split_paths(const gchar *paths)
438 {
439 GSList *list = NULL;
440 gchar **spl, **it;
441
442 if (!paths)
443 return NULL;
444 spl = g_strsplit(paths, ":", -1);
445 for (it = spl; *it; ++it)
446 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
447 g_free(spl);
448 return list;
449 }
450
451 void parse_paths_startup(void)
452 {
453 const gchar *path;
454
455 if (xdg_start)
456 return;
457 xdg_start = TRUE;
458
459 path = g_getenv("XDG_CONFIG_HOME");
460 if (path && path[0] != '\0') /* not unset or empty */
461 xdg_config_home_path = g_build_filename(path, NULL);
462 else
463 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
464 NULL);
465
466 path = g_getenv("XDG_DATA_HOME");
467 if (path && path[0] != '\0') /* not unset or empty */
468 xdg_data_home_path = g_build_filename(path, NULL);
469 else
470 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
471 "share", NULL);
472
473 path = g_getenv("XDG_CONFIG_DIRS");
474 if (path && path[0] != '\0') /* not unset or empty */
475 xdg_config_dir_paths = split_paths(path);
476 else {
477 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
478 g_strdup(CONFIGDIR),
479 (GSListFunc) g_slist_append);
480 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
481 g_build_filename
482 (G_DIR_SEPARATOR_S,
483 "etc", "xdg", NULL),
484 (GSListFunc) g_slist_append);
485 }
486 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
487 g_strdup(xdg_config_home_path),
488 (GSListFunc) g_slist_prepend);
489
490 path = g_getenv("XDG_DATA_DIRS");
491 if (path && path[0] != '\0') /* not unset or empty */
492 xdg_data_dir_paths = split_paths(path);
493 else {
494 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
495 g_strdup(DATADIR),
496 (GSListFunc) g_slist_append);
497 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
498 g_build_filename
499 (G_DIR_SEPARATOR_S,
500 "usr", "local", "share", NULL),
501 (GSListFunc) g_slist_append);
502 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
503 g_build_filename
504 (G_DIR_SEPARATOR_S,
505 "usr", "share", NULL),
506 (GSListFunc) g_slist_append);
507 }
508 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
509 g_strdup(xdg_data_home_path),
510 (GSListFunc) g_slist_prepend);
511 }
512
513 void parse_paths_shutdown(void)
514 {
515 GSList *it;
516
517 if (!xdg_start)
518 return;
519 xdg_start = FALSE;
520
521 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
522 g_free(it->data);
523 g_slist_free(xdg_config_dir_paths);
524 xdg_config_dir_paths = NULL;
525 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
526 g_free(it->data);
527 g_slist_free(xdg_data_dir_paths);
528 xdg_data_dir_paths = NULL;
529 g_free(xdg_config_home_path);
530 xdg_config_home_path = NULL;
531 g_free(xdg_data_home_path);
532 xdg_data_home_path = NULL;
533 }
534
535 gchar *parse_expand_tilde(const gchar *f)
536 {
537 gchar **spl;
538 gchar *ret;
539
540 if (!f)
541 return NULL;
542 spl = g_strsplit(f, "~", 0);
543 ret = g_strjoinv(g_get_home_dir(), spl);
544 g_strfreev(spl);
545 return ret;
546 }
547
548 gboolean parse_mkdir(const gchar *path, gint mode)
549 {
550 gboolean ret = TRUE;
551
552 g_return_val_if_fail(path != NULL, FALSE);
553 g_return_val_if_fail(path[0] != '\0', FALSE);
554
555 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
556 if (mkdir(path, mode) == -1)
557 ret = FALSE;
558
559 return ret;
560 }
561
562 gboolean parse_mkdir_path(const gchar *path, gint mode)
563 {
564 gboolean ret = TRUE;
565
566 g_return_val_if_fail(path != NULL, FALSE);
567 g_return_val_if_fail(path[0] == '/', FALSE);
568
569 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
570 gchar *c, *e;
571
572 c = g_strdup(path);
573 e = c;
574 while ((e = strchr(e + 1, '/'))) {
575 *e = '\0';
576 if (!(ret = parse_mkdir(c, mode)))
577 goto parse_mkdir_path_end;
578 *e = '/';
579 }
580 ret = parse_mkdir(c, mode);
581
582 parse_mkdir_path_end:
583 g_free(c);
584 }
585
586 return ret;
587 }
588
589 const gchar* parse_xdg_config_home_path(void)
590 {
591 return xdg_config_home_path;
592 }
593
594 const gchar* parse_xdg_data_home_path(void)
595 {
596 return xdg_data_home_path;
597 }
598
599 GSList* parse_xdg_config_dir_paths(void)
600 {
601 return xdg_config_dir_paths;
602 }
603
604 GSList* parse_xdg_data_dir_paths(void)
605 {
606 return xdg_data_dir_paths;
607 }
This page took 0.062716 seconds and 3 git commands to generate.