]> Dogcows Code - chaz/openbox/blob - obt/parse.c
741b19e11fc7cdd712af1af9f97683572cf823f6
[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 void obt_parse_tree_from_root(ObtParseInst *i)
308 {
309 obt_parse_tree(i, i->root->children);
310 }
311
312 gchar *obt_parse_node_string(xmlNodePtr node)
313 {
314 xmlChar *c = xmlNodeGetContent(node);
315 gchar *s = g_strdup(c ? (gchar*)c : "");
316 xmlFree(c);
317 return s;
318 }
319
320 gint obt_parse_node_int(xmlNodePtr node)
321 {
322 xmlChar *c = xmlNodeGetContent(node);
323 gint i = c ? atoi((gchar*)c) : 0;
324 xmlFree(c);
325 return i;
326 }
327
328 gboolean obt_parse_node_bool(xmlNodePtr node)
329 {
330 xmlChar *c = xmlNodeGetContent(node);
331 gboolean b = FALSE;
332 if (c && !xmlStrcasecmp(c, (const xmlChar*) "true"))
333 b = TRUE;
334 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "yes"))
335 b = TRUE;
336 else if (c && !xmlStrcasecmp(c, (const xmlChar*) "on"))
337 b = TRUE;
338 xmlFree(c);
339 return b;
340 }
341
342 gboolean obt_parse_node_contains(xmlNodePtr node, const gchar *val)
343 {
344 xmlChar *c = xmlNodeGetContent(node);
345 gboolean r;
346 r = !xmlStrcasecmp(c, (const xmlChar*) val);
347 xmlFree(c);
348 return r;
349 }
350
351 xmlNodePtr obt_parse_find_node(xmlNodePtr node, const gchar *tag)
352 {
353 while (node) {
354 if (!xmlStrcmp(node->name, (const xmlChar*) tag))
355 return node;
356 node = node->next;
357 }
358 return NULL;
359 }
360
361 gboolean obt_parse_attr_bool(xmlNodePtr node, const gchar *name,
362 gboolean *value)
363 {
364 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
365 gboolean r = FALSE;
366 if (c) {
367 if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
368 *value = TRUE, r = TRUE;
369 else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
370 *value = TRUE, r = TRUE;
371 else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
372 *value = TRUE, r = TRUE;
373 else if (!xmlStrcasecmp(c, (const xmlChar*) "false"))
374 *value = FALSE, r = TRUE;
375 else if (!xmlStrcasecmp(c, (const xmlChar*) "no"))
376 *value = FALSE, r = TRUE;
377 else if (!xmlStrcasecmp(c, (const xmlChar*) "off"))
378 *value = FALSE, r = TRUE;
379 }
380 xmlFree(c);
381 return r;
382 }
383
384 gboolean obt_parse_attr_int(xmlNodePtr node, const gchar *name, gint *value)
385 {
386 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
387 gboolean r = FALSE;
388 if (c) {
389 *value = atoi((gchar*)c);
390 r = TRUE;
391 }
392 xmlFree(c);
393 return r;
394 }
395
396 gboolean obt_parse_attr_string(xmlNodePtr node, const gchar *name,
397 gchar **value)
398 {
399 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
400 gboolean r = FALSE;
401 if (c) {
402 *value = g_strdup((gchar*)c);
403 r = TRUE;
404 }
405 xmlFree(c);
406 return r;
407 }
408
409 gboolean obt_parse_attr_contains(xmlNodePtr node, const gchar *name,
410 const gchar *val)
411 {
412 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
413 gboolean r = FALSE;
414 if (c)
415 r = !xmlStrcasecmp(c, (const xmlChar*) val);
416 xmlFree(c);
417 return r;
418 }
419
420 static gint slist_path_cmp(const gchar *a, const gchar *b)
421 {
422 return strcmp(a, b);
423 }
424
425 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
426
427 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
428 {
429 g_assert(func);
430
431 if (!data)
432 return list;
433
434 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
435 list = func(list, data);
436 else
437 g_free(data);
438
439 return list;
440 }
441
442 static GSList* split_paths(const gchar *paths)
443 {
444 GSList *list = NULL;
445 gchar **spl, **it;
446
447 if (!paths)
448 return NULL;
449 spl = g_strsplit(paths, ":", -1);
450 for (it = spl; *it; ++it)
451 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
452 g_free(spl);
453 return list;
454 }
455
456 void parse_paths_startup(void)
457 {
458 const gchar *path;
459
460 if (xdg_start)
461 return;
462 xdg_start = TRUE;
463
464 path = g_getenv("XDG_CONFIG_HOME");
465 if (path && path[0] != '\0') /* not unset or empty */
466 xdg_config_home_path = g_build_filename(path, NULL);
467 else
468 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
469 NULL);
470
471 path = g_getenv("XDG_DATA_HOME");
472 if (path && path[0] != '\0') /* not unset or empty */
473 xdg_data_home_path = g_build_filename(path, NULL);
474 else
475 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
476 "share", NULL);
477
478 path = g_getenv("XDG_CONFIG_DIRS");
479 if (path && path[0] != '\0') /* not unset or empty */
480 xdg_config_dir_paths = split_paths(path);
481 else {
482 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
483 g_strdup(CONFIGDIR),
484 (GSListFunc) g_slist_append);
485 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
486 g_build_filename
487 (G_DIR_SEPARATOR_S,
488 "etc", "xdg", NULL),
489 (GSListFunc) g_slist_append);
490 }
491 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
492 g_strdup(xdg_config_home_path),
493 (GSListFunc) g_slist_prepend);
494
495 path = g_getenv("XDG_DATA_DIRS");
496 if (path && path[0] != '\0') /* not unset or empty */
497 xdg_data_dir_paths = split_paths(path);
498 else {
499 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
500 g_strdup(DATADIR),
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", "local", "share", NULL),
506 (GSListFunc) g_slist_append);
507 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
508 g_build_filename
509 (G_DIR_SEPARATOR_S,
510 "usr", "share", NULL),
511 (GSListFunc) g_slist_append);
512 }
513 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
514 g_strdup(xdg_data_home_path),
515 (GSListFunc) g_slist_prepend);
516 }
517
518 void parse_paths_shutdown(void)
519 {
520 GSList *it;
521
522 if (!xdg_start)
523 return;
524 xdg_start = FALSE;
525
526 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
527 g_free(it->data);
528 g_slist_free(xdg_config_dir_paths);
529 xdg_config_dir_paths = NULL;
530 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
531 g_free(it->data);
532 g_slist_free(xdg_data_dir_paths);
533 xdg_data_dir_paths = NULL;
534 g_free(xdg_config_home_path);
535 xdg_config_home_path = NULL;
536 g_free(xdg_data_home_path);
537 xdg_data_home_path = NULL;
538 }
539
540 gchar *parse_expand_tilde(const gchar *f)
541 {
542 gchar **spl;
543 gchar *ret;
544
545 if (!f)
546 return NULL;
547 spl = g_strsplit(f, "~", 0);
548 ret = g_strjoinv(g_get_home_dir(), spl);
549 g_strfreev(spl);
550 return ret;
551 }
552
553 gboolean parse_mkdir(const gchar *path, gint mode)
554 {
555 gboolean ret = TRUE;
556
557 g_return_val_if_fail(path != NULL, FALSE);
558 g_return_val_if_fail(path[0] != '\0', FALSE);
559
560 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
561 if (mkdir(path, mode) == -1)
562 ret = FALSE;
563
564 return ret;
565 }
566
567 gboolean parse_mkdir_path(const gchar *path, gint mode)
568 {
569 gboolean ret = TRUE;
570
571 g_return_val_if_fail(path != NULL, FALSE);
572 g_return_val_if_fail(path[0] == '/', FALSE);
573
574 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
575 gchar *c, *e;
576
577 c = g_strdup(path);
578 e = c;
579 while ((e = strchr(e + 1, '/'))) {
580 *e = '\0';
581 if (!(ret = parse_mkdir(c, mode)))
582 goto parse_mkdir_path_end;
583 *e = '/';
584 }
585 ret = parse_mkdir(c, mode);
586
587 parse_mkdir_path_end:
588 g_free(c);
589 }
590
591 return ret;
592 }
593
594 const gchar* parse_xdg_config_home_path(void)
595 {
596 return xdg_config_home_path;
597 }
598
599 const gchar* parse_xdg_data_home_path(void)
600 {
601 return xdg_data_home_path;
602 }
603
604 GSList* parse_xdg_config_dir_paths(void)
605 {
606 return xdg_config_dir_paths;
607 }
608
609 GSList* parse_xdg_data_dir_paths(void)
610 {
611 return xdg_data_dir_paths;
612 }
This page took 0.0589 seconds and 4 git commands to generate.