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