]>
Dogcows Code - chaz/openbox/blob - parser/parse.c
43d076e1e95450eb11e699f5e77ad7e8982c7913
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 parse.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
24 #include <sys/types.h>
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
;
40 GHashTable
*callbacks
;
43 static void destfunc(struct Callback
*c
)
49 ObParseInst
* parse_startup()
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
);
57 void parse_shutdown(ObParseInst
*i
)
60 g_hash_table_destroy(i
->callbacks
);
65 void parse_register(ObParseInst
*i
, const gchar
*tag
,
66 ParseCallback func
, gpointer data
)
70 if ((c
= g_hash_table_lookup(i
->callbacks
, tag
))) {
71 g_error("Tag '%s' already registered", tag
);
75 c
= g_new(struct Callback
, 1);
76 c
->tag
= g_strdup(tag
);
79 g_hash_table_insert(i
->callbacks
, c
->tag
, c
);
82 gboolean
parse_load_rc(const gchar
*type
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
89 fname
= g_strdup("rc.xml");
91 fname
= g_strdup_printf("rc-%s.xml", type
);
93 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
96 path
= g_build_filename(it
->data
, "openbox", fname
, NULL
);
97 r
= parse_load(path
, "openbox_config", doc
, root
);
105 gboolean
parse_load_theme(const gchar
*name
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
112 /* backward compatibility.. */
113 path
= g_build_filename(g_get_home_dir(), ".themes", name
,
114 "openbox-3", "themerc.xml", NULL
);
115 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
116 *retpath
= g_path_get_dirname(path
);
120 for (it
= xdg_data_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
121 path
= g_build_filename(it
->data
, "themes", name
, "openbox-3",
122 "themerc.xml", NULL
);
123 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
124 *retpath
= g_path_get_dirname(path
);
131 gboolean
parse_load_menu(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
137 if (file
[0] == '/') {
138 r
= parse_load(file
, "openbox_menu", doc
, root
);
140 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
141 path
= g_build_filename(it
->data
, "openbox", file
, NULL
);
142 r
= parse_load(path
, "openbox_menu", doc
, root
);
149 gboolean
parse_load(const gchar
*path
, const gchar
*rootname
,
150 xmlDocPtr
*doc
, xmlNodePtr
*root
)
153 if (stat(path
, &s
) < 0)
156 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
157 without this option, the tree is weird and has extra nodes in it. */
158 if ((*doc
= xmlReadFile(path
, NULL
,
159 XML_PARSE_NOBLANKS
| XML_PARSE_RECOVER
))) {
160 *root
= xmlDocGetRootElement(*doc
);
164 g_message("%s is an empty document", path
);
166 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
169 g_message("XML Document %s is of wrong type. Root "
170 "node is not '%s'", path
, rootname
);
179 gboolean
parse_load_mem(gpointer data
, guint len
, const gchar
*rootname
,
180 xmlDocPtr
*doc
, xmlNodePtr
*root
)
182 if ((*doc
= xmlParseMemory(data
, len
))) {
183 *root
= xmlDocGetRootElement(*doc
);
187 g_message("Given memory is an empty document");
189 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
192 g_message("XML Document in given memory is of wrong "
193 "type. Root node is not '%s'\n", rootname
);
202 void parse_close(xmlDocPtr doc
)
207 void parse_tree(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
)
210 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
213 c
->func(i
, doc
, node
, c
->data
);
219 gchar
*parse_string(xmlDocPtr doc
, xmlNodePtr node
)
221 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
222 gchar
*s
= g_strdup(c
? (gchar
*)c
: "");
227 gint
parse_int(xmlDocPtr doc
, xmlNodePtr node
)
229 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
230 gint i
= atoi((gchar
*)c
);
235 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
237 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
239 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
241 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
243 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
249 gboolean
parse_contains(const gchar
*val
, xmlDocPtr doc
, xmlNodePtr node
)
251 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
253 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
258 xmlNodePtr
parse_find_node(const gchar
*tag
, xmlNodePtr node
)
261 if (!xmlStrcmp(node
->name
, (const xmlChar
*) tag
))
268 gboolean
parse_attr_bool(const gchar
*name
, xmlNodePtr node
, gboolean
*value
)
270 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
273 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
274 *value
= TRUE
, r
= TRUE
;
275 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
276 *value
= TRUE
, r
= TRUE
;
277 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
278 *value
= TRUE
, r
= TRUE
;
279 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "false"))
280 *value
= FALSE
, r
= TRUE
;
281 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "no"))
282 *value
= FALSE
, r
= TRUE
;
283 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "off"))
284 *value
= FALSE
, r
= TRUE
;
290 gboolean
parse_attr_int(const gchar
*name
, xmlNodePtr node
, gint
*value
)
292 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
295 *value
= atoi((gchar
*)c
);
302 gboolean
parse_attr_string(const gchar
*name
, xmlNodePtr node
, gchar
**value
)
304 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
307 *value
= g_strdup((gchar
*)c
);
314 gboolean
parse_attr_contains(const gchar
*val
, xmlNodePtr node
,
317 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
320 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
325 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
330 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
332 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
339 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
340 list
= func(list
, data
);
347 static GSList
* split_paths(const gchar
*paths
)
354 spl
= g_strsplit(paths
, ":", -1);
355 for (it
= spl
; *it
; ++it
)
356 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
361 void parse_paths_startup()
369 path
= g_getenv("XDG_CONFIG_HOME");
370 if (path
&& path
[0] != '\0') /* not unset or empty */
371 xdg_config_home_path
= g_build_filename(path
, NULL
);
373 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
376 path
= g_getenv("XDG_DATA_HOME");
377 if (path
&& path
[0] != '\0') /* not unset or empty */
378 xdg_data_home_path
= g_build_filename(path
, NULL
);
380 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
383 path
= g_getenv("XDG_CONFIG_DIRS");
384 if (path
&& path
[0] != '\0') /* not unset or empty */
385 xdg_config_dir_paths
= split_paths(path
);
387 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
389 (GSListFunc
) g_slist_append
);
390 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
394 (GSListFunc
) g_slist_append
);
396 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
397 g_strdup(xdg_config_home_path
),
398 (GSListFunc
) g_slist_prepend
);
400 path
= g_getenv("XDG_DATA_DIRS");
401 if (path
&& path
[0] != '\0') /* not unset or empty */
402 xdg_data_dir_paths
= split_paths(path
);
404 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
407 "usr", "local", "share", NULL
),
408 (GSListFunc
) g_slist_append
);
409 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
412 "usr", "share", NULL
),
413 (GSListFunc
) g_slist_append
);
414 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
416 (GSListFunc
) g_slist_append
);
418 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
419 g_strdup(xdg_data_home_path
),
420 (GSListFunc
) g_slist_prepend
);
423 void parse_paths_shutdown()
431 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
433 g_slist_free(xdg_config_dir_paths
);
434 xdg_config_dir_paths
= NULL
;
435 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
437 g_slist_free(xdg_data_dir_paths
);
438 xdg_data_dir_paths
= NULL
;
439 g_free(xdg_config_home_path
);
440 xdg_config_home_path
= NULL
;
441 g_free(xdg_data_home_path
);
442 xdg_data_home_path
= NULL
;
445 gchar
*parse_expand_tilde(const gchar
*f
)
452 spl
= g_strsplit(f
, "~", 0);
453 ret
= g_strjoinv(g_get_home_dir(), spl
);
458 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
462 g_return_val_if_fail(path
!= NULL
, FALSE
);
463 g_return_val_if_fail(path
[0] != '\0', FALSE
);
465 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
466 if (mkdir(path
, mode
) == -1)
472 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
476 g_return_val_if_fail(path
!= NULL
, FALSE
);
477 g_return_val_if_fail(path
[0] == '/', FALSE
);
479 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
484 while ((e
= strchr(e
+ 1, '/'))) {
486 if (!(ret
= parse_mkdir(c
, mode
)))
487 goto parse_mkdir_path_end
;
490 ret
= parse_mkdir(c
, mode
);
492 parse_mkdir_path_end
:
499 const gchar
* parse_xdg_config_home_path()
501 return xdg_config_home_path
;
504 const gchar
* parse_xdg_data_home_path()
506 return xdg_data_home_path
;
509 GSList
* parse_xdg_config_dir_paths()
511 return xdg_config_dir_paths
;
514 GSList
* parse_xdg_data_dir_paths()
516 return xdg_data_dir_paths
;
This page took 0.059754 seconds and 4 git commands to generate.