]> Dogcows Code - chaz/openbox/blob - parser/parse.c
note that in the CHANGELOG
[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 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_int(const gchar *name, xmlNodePtr node, gint *value)
230 {
231 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
232 gboolean r = FALSE;
233 if (c) {
234 *value = atoi((gchar*)c);
235 r = TRUE;
236 }
237 xmlFree(c);
238 return r;
239 }
240
241 gboolean parse_attr_string(const gchar *name, xmlNodePtr node, gchar **value)
242 {
243 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
244 gboolean r = FALSE;
245 if (c) {
246 *value = g_strdup((gchar*)c);
247 r = TRUE;
248 }
249 xmlFree(c);
250 return r;
251 }
252
253 gboolean parse_attr_contains(const gchar *val, xmlNodePtr node,
254 const gchar *name)
255 {
256 xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
257 gboolean r;
258 r = !xmlStrcasecmp(c, (const xmlChar*) val);
259 xmlFree(c);
260 return r;
261 }
262
263 static gint slist_path_cmp(const gchar *a, const gchar *b)
264 {
265 return strcmp(a, b);
266 }
267
268 typedef GSList* (*GSListFunc) (gpointer list, gconstpointer data);
269
270 static GSList* slist_path_add(GSList *list, gpointer data, GSListFunc func)
271 {
272 g_assert(func);
273
274 if (!data)
275 return list;
276
277 if (!g_slist_find_custom(list, data, (GCompareFunc) slist_path_cmp))
278 list = func(list, data);
279
280 return list;
281 }
282
283 static GSList* split_paths(const gchar *paths)
284 {
285 GSList *list = NULL;
286 gchar **spl, **it;
287
288 if (!paths)
289 return NULL;
290 spl = g_strsplit(paths, ":", -1);
291 for (it = spl; *it; ++it)
292 list = slist_path_add(list, *it, (GSListFunc) g_slist_append);
293 g_free(spl);
294 return list;
295 }
296
297 void parse_paths_startup()
298 {
299 const gchar *path;
300
301 if (xdg_start)
302 return;
303 xdg_start = TRUE;
304
305 path = g_getenv("XDG_CONFIG_HOME");
306 if (path && path[0] != '\0') /* not unset or empty */
307 xdg_config_home_path = g_build_filename(path, NULL);
308 else
309 xdg_config_home_path = g_build_filename(g_get_home_dir(), ".config",
310 NULL);
311
312 path = g_getenv("XDG_DATA_HOME");
313 if (path && path[0] != '\0') /* not unset or empty */
314 xdg_data_home_path = g_build_filename(path, NULL);
315 else
316 xdg_data_home_path = g_build_filename(g_get_home_dir(), ".local",
317 "share", NULL);
318
319 path = g_getenv("XDG_CONFIG_DIRS");
320 if (path && path[0] != '\0') /* not unset or empty */
321 xdg_config_dir_paths = split_paths(path);
322 else {
323 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
324 g_build_filename
325 (G_DIR_SEPARATOR_S,
326 "etc", "xdg", NULL),
327 (GSListFunc) g_slist_append);
328 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
329 g_strdup(CONFIGDIR),
330 (GSListFunc) g_slist_append);
331 }
332 xdg_config_dir_paths = slist_path_add(xdg_config_dir_paths,
333 xdg_config_home_path,
334 (GSListFunc) g_slist_prepend);
335
336 path = g_getenv("XDG_DATA_DIRS");
337 if (path && path[0] != '\0') /* not unset or empty */
338 xdg_data_dir_paths = split_paths(path);
339 else {
340 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
341 g_build_filename
342 (G_DIR_SEPARATOR_S,
343 "usr", "local", "share", NULL),
344 (GSListFunc) g_slist_append);
345 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
346 g_build_filename
347 (G_DIR_SEPARATOR_S,
348 "usr", "share", NULL),
349 (GSListFunc) g_slist_append);
350 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
351 g_strdup(DATADIR),
352 (GSListFunc) g_slist_append);
353 }
354 xdg_data_dir_paths = slist_path_add(xdg_data_dir_paths,
355 xdg_data_home_path,
356 (GSListFunc) g_slist_prepend);
357 }
358
359 void parse_paths_shutdown()
360 {
361 GSList *it;
362
363 if (!xdg_start)
364 return;
365 xdg_start = FALSE;
366
367 for (it = xdg_config_dir_paths; it; it = g_slist_next(it))
368 g_free(it->data);
369 g_slist_free(xdg_config_dir_paths);
370 xdg_config_dir_paths = NULL;
371 for (it = xdg_data_dir_paths; it; it = g_slist_next(it))
372 g_free(it->data);
373 g_slist_free(xdg_data_dir_paths);
374 xdg_data_dir_paths = NULL;
375 }
376
377 gchar *parse_expand_tilde(const gchar *f)
378 {
379 gchar **spl;
380 gchar *ret;
381
382 if (!f)
383 return NULL;
384 spl = g_strsplit(f, "~", 0);
385 ret = g_strjoinv(g_get_home_dir(), spl);
386 g_strfreev(spl);
387 return ret;
388 }
389
390 gboolean parse_mkdir(const gchar *path, gint mode)
391 {
392 gboolean ret = TRUE;
393
394 g_return_val_if_fail(path != NULL, FALSE);
395 g_return_val_if_fail(path[0] != '\0', FALSE);
396
397 if (!g_file_test(path, G_FILE_TEST_IS_DIR))
398 if (mkdir(path, mode) == -1)
399 ret = FALSE;
400
401 return ret;
402 }
403
404 gboolean parse_mkdir_path(const gchar *path, gint mode)
405 {
406 gboolean ret = TRUE;
407
408 g_return_val_if_fail(path != NULL, FALSE);
409 g_return_val_if_fail(path[0] == '/', FALSE);
410
411 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
412 gchar *c, *e;
413
414 c = g_strdup(path);
415 e = c;
416 while ((e = strchr(e + 1, '/'))) {
417 *e = '\0';
418 if (!(ret = parse_mkdir(c, mode)))
419 goto parse_mkdir_path_end;
420 *e = '/';
421 }
422 ret = parse_mkdir(c, mode);
423
424 parse_mkdir_path_end:
425 g_free(c);
426 }
427
428 return ret;
429 }
430
431 const gchar* parse_xdg_config_home_path()
432 {
433 return xdg_config_home_path;
434 }
435
436 const gchar* parse_xdg_data_home_path()
437 {
438 return xdg_data_home_path;
439 }
440
441 GSList* parse_xdg_config_dir_paths()
442 {
443 return xdg_config_dir_paths;
444 }
445
446 GSList* parse_xdg_data_dir_paths()
447 {
448 return xdg_data_dir_paths;
449 }
This page took 0.059637 seconds and 4 git commands to generate.