]>
Dogcows Code - chaz/openbox/blob - parser/parse.c
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_warning("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(xmlDocPtr
*doc
, xmlNodePtr
*root
)
88 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
89 path
= g_build_filename(it
->data
, "openbox", "rc.xml", NULL
);
90 r
= parse_load(path
, "openbox_config", doc
, root
);
94 g_warning("Unable to find a valid config file, using defaults");
98 gboolean
parse_load_theme(const gchar
*name
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
105 /* backward compatibility.. */
106 path
= g_build_filename(g_get_home_dir(), ".themes", name
,
107 "openbox-3", "themerc.xml", NULL
);
108 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
109 *retpath
= g_path_get_dirname(path
);
113 for (it
= xdg_data_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
114 path
= g_build_filename(it
->data
, "themes", name
, "openbox-3",
115 "themerc.xml", NULL
);
116 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
117 *retpath
= g_path_get_dirname(path
);
122 g_warning("Unable to load the theme %s", name
);
126 gboolean
parse_load_menu(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
132 if (file
[0] == '/') {
133 r
= parse_load(file
, "openbox_menu", doc
, root
);
135 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
136 path
= g_build_filename(it
->data
, "openbox", file
, NULL
);
137 r
= parse_load(path
, "openbox_menu", doc
, root
);
142 g_warning("Unable to find a valid menu file '%s'", file
);
146 gboolean
parse_load(const gchar
*path
, const gchar
*rootname
,
147 xmlDocPtr
*doc
, xmlNodePtr
*root
)
150 if (stat(path
, &s
) < 0)
153 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
154 without this option, the tree is weird and has extra nodes in it. */
155 if ((*doc
= xmlReadFile(path
, NULL
, XML_PARSE_NOBLANKS
))) {
156 *root
= xmlDocGetRootElement(*doc
);
160 g_warning("%s is an empty document", path
);
162 if (xmlStrcasecmp((*root
)->name
, (const xmlChar
*)rootname
)) {
165 g_warning("Document %s is of wrong type. root node is "
166 "not '%s'", path
, rootname
);
175 gboolean
parse_load_mem(gpointer data
, guint len
, const gchar
*rootname
,
176 xmlDocPtr
*doc
, xmlNodePtr
*root
)
178 if ((*doc
= xmlParseMemory(data
, len
))) {
179 *root
= xmlDocGetRootElement(*doc
);
183 g_warning("Given memory is an empty document");
185 if (xmlStrcasecmp((*root
)->name
, (const xmlChar
*)rootname
)) {
188 g_warning("Document in given memory is of wrong type. root "
189 "node is not '%s'", rootname
);
198 void parse_close(xmlDocPtr doc
)
203 void parse_tree(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
)
206 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
209 c
->func(i
, doc
, node
, c
->data
);
215 gchar
*parse_string(xmlDocPtr doc
, xmlNodePtr node
)
217 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
218 gchar
*s
= g_strdup(c
? (gchar
*)c
: "");
223 gint
parse_int(xmlDocPtr doc
, xmlNodePtr node
)
225 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
226 gint i
= atoi((gchar
*)c
);
231 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
233 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
235 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
237 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
239 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
245 gboolean
parse_contains(const gchar
*val
, xmlDocPtr doc
, xmlNodePtr node
)
247 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
249 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
254 xmlNodePtr
parse_find_node(const gchar
*tag
, xmlNodePtr node
)
257 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) tag
))
264 gboolean
parse_attr_bool(const gchar
*name
, xmlNodePtr node
, gboolean
*value
)
266 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
269 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
270 *value
= TRUE
, r
= TRUE
;
271 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
272 *value
= TRUE
, r
= TRUE
;
273 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
274 *value
= TRUE
, r
= TRUE
;
275 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "false"))
276 *value
= FALSE
, r
= TRUE
;
277 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "no"))
278 *value
= FALSE
, r
= TRUE
;
279 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "off"))
280 *value
= FALSE
, r
= TRUE
;
286 gboolean
parse_attr_int(const gchar
*name
, xmlNodePtr node
, gint
*value
)
288 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
291 *value
= atoi((gchar
*)c
);
298 gboolean
parse_attr_string(const gchar
*name
, xmlNodePtr node
, gchar
**value
)
300 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
303 *value
= g_strdup((gchar
*)c
);
310 gboolean
parse_attr_contains(const gchar
*val
, xmlNodePtr node
,
313 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
316 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
321 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
326 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
328 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
335 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
336 list
= func(list
, data
);
341 static GSList
* split_paths(const gchar
*paths
)
348 spl
= g_strsplit(paths
, ":", -1);
349 for (it
= spl
; *it
; ++it
)
350 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
355 void parse_paths_startup()
363 path
= g_getenv("XDG_CONFIG_HOME");
364 if (path
&& path
[0] != '\0') /* not unset or empty */
365 xdg_config_home_path
= g_build_filename(path
, NULL
);
367 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
370 path
= g_getenv("XDG_DATA_HOME");
371 if (path
&& path
[0] != '\0') /* not unset or empty */
372 xdg_data_home_path
= g_build_filename(path
, NULL
);
374 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
377 path
= g_getenv("XDG_CONFIG_DIRS");
378 if (path
&& path
[0] != '\0') /* not unset or empty */
379 xdg_config_dir_paths
= split_paths(path
);
381 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
385 (GSListFunc
) g_slist_append
);
386 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
388 (GSListFunc
) g_slist_append
);
390 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
391 xdg_config_home_path
,
392 (GSListFunc
) g_slist_prepend
);
394 path
= g_getenv("XDG_DATA_DIRS");
395 if (path
&& path
[0] != '\0') /* not unset or empty */
396 xdg_data_dir_paths
= split_paths(path
);
398 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
401 "usr", "local", "share", NULL
),
402 (GSListFunc
) g_slist_append
);
403 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
406 "usr", "share", NULL
),
407 (GSListFunc
) g_slist_append
);
408 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
410 (GSListFunc
) g_slist_append
);
412 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
414 (GSListFunc
) g_slist_prepend
);
417 void parse_paths_shutdown()
425 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
427 g_slist_free(xdg_config_dir_paths
);
428 xdg_config_dir_paths
= NULL
;
429 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
431 g_slist_free(xdg_data_dir_paths
);
432 xdg_data_dir_paths
= NULL
;
433 g_free(xdg_config_home_path
);
434 xdg_config_home_path
= NULL
;
435 g_free(xdg_data_home_path
);
436 xdg_data_home_path
= NULL
;
439 gchar
*parse_expand_tilde(const gchar
*f
)
446 spl
= g_strsplit(f
, "~", 0);
447 ret
= g_strjoinv(g_get_home_dir(), spl
);
452 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
456 g_return_val_if_fail(path
!= NULL
, FALSE
);
457 g_return_val_if_fail(path
[0] != '\0', FALSE
);
459 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
460 if (mkdir(path
, mode
) == -1)
466 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
470 g_return_val_if_fail(path
!= NULL
, FALSE
);
471 g_return_val_if_fail(path
[0] == '/', FALSE
);
473 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
478 while ((e
= strchr(e
+ 1, '/'))) {
480 if (!(ret
= parse_mkdir(c
, mode
)))
481 goto parse_mkdir_path_end
;
484 ret
= parse_mkdir(c
, mode
);
486 parse_mkdir_path_end
:
493 const gchar
* parse_xdg_config_home_path()
495 return xdg_config_home_path
;
498 const gchar
* parse_xdg_data_home_path()
500 return xdg_data_home_path
;
503 GSList
* parse_xdg_config_dir_paths()
505 return xdg_config_dir_paths
;
508 GSList
* parse_xdg_data_dir_paths()
510 return xdg_data_dir_paths
;
This page took 0.057788 seconds and 5 git commands to generate.