]>
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 Ben 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>
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
;
39 GHashTable
*callbacks
;
42 static void destfunc(struct Callback
*c
)
48 ObParseInst
* parse_startup()
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
);
56 void parse_shutdown(ObParseInst
*i
)
59 g_hash_table_destroy(i
->callbacks
);
64 void parse_register(ObParseInst
*i
, const gchar
*tag
,
65 ParseCallback func
, gpointer data
)
69 if ((c
= g_hash_table_lookup(i
->callbacks
, tag
))) {
70 g_warning("tag '%s' already registered", tag
);
74 c
= g_new(struct Callback
, 1);
75 c
->tag
= g_strdup(tag
);
78 g_hash_table_insert(i
->callbacks
, c
->tag
, c
);
81 gboolean
parse_load_rc(xmlDocPtr
*doc
, xmlNodePtr
*root
)
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
);
93 g_warning("unable to find a valid config file, using defaults");
97 gboolean
parse_load_menu(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
103 if (file
[0] == '/') {
104 r
= parse_load(file
, "openbox_menu", doc
, root
);
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
);
113 g_warning("unable to find a valid menu file '%s'", file
);
117 gboolean
parse_load(const gchar
*path
, const gchar
*rootname
,
118 xmlDocPtr
*doc
, xmlNodePtr
*root
)
120 if ((*doc
= xmlParseFile(path
))) {
121 *root
= xmlDocGetRootElement(*doc
);
125 g_warning("%s is an empty document", path
);
127 if (xmlStrcasecmp((*root
)->name
, (const xmlChar
*)rootname
)) {
130 g_warning("document %s is of wrong type. root node is "
131 "not '%s'", path
, rootname
);
140 gboolean
parse_load_mem(gpointer data
, guint len
, const gchar
*rootname
,
141 xmlDocPtr
*doc
, xmlNodePtr
*root
)
143 if ((*doc
= xmlParseMemory(data
, len
))) {
144 *root
= xmlDocGetRootElement(*doc
);
148 g_warning("Given memory is an empty document");
150 if (xmlStrcasecmp((*root
)->name
, (const xmlChar
*)rootname
)) {
153 g_warning("document in given memory is of wrong type. root "
154 "node is not '%s'", rootname
);
163 void parse_close(xmlDocPtr doc
)
168 void parse_tree(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
)
171 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
174 c
->func(i
, doc
, node
, c
->data
);
180 gchar
*parse_string(xmlDocPtr doc
, xmlNodePtr node
)
182 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
183 gchar
*s
= g_strdup(c
? (gchar
*)c
: "");
188 gint
parse_int(xmlDocPtr doc
, xmlNodePtr node
)
190 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
191 gint i
= atoi((gchar
*)c
);
196 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
198 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
200 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
202 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
204 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
210 gboolean
parse_contains(const gchar
*val
, xmlDocPtr doc
, xmlNodePtr node
)
212 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
214 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
219 xmlNodePtr
parse_find_node(const gchar
*tag
, xmlNodePtr node
)
222 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) tag
))
229 gboolean
parse_attr_int(const gchar
*name
, xmlNodePtr node
, gint
*value
)
231 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
234 *value
= atoi((gchar
*)c
);
241 gboolean
parse_attr_string(const gchar
*name
, xmlNodePtr node
, gchar
**value
)
243 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
246 *value
= g_strdup((gchar
*)c
);
253 gboolean
parse_attr_contains(const gchar
*val
, xmlNodePtr node
,
256 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
258 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
263 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
268 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
270 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
277 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
278 list
= func(list
, data
);
283 static GSList
* split_paths(const gchar
*paths
)
290 spl
= g_strsplit(paths
, ":", -1);
291 for (it
= spl
; *it
; ++it
)
292 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
297 void parse_paths_startup()
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
);
309 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
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
);
316 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
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
);
323 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
327 (GSListFunc
) g_slist_append
);
328 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
330 (GSListFunc
) g_slist_append
);
332 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
333 xdg_config_home_path
,
334 (GSListFunc
) g_slist_prepend
);
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
);
340 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
343 "usr", "local", "share", NULL
),
344 (GSListFunc
) g_slist_append
);
345 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
348 "usr", "share", NULL
),
349 (GSListFunc
) g_slist_append
);
350 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
352 (GSListFunc
) g_slist_append
);
354 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
356 (GSListFunc
) g_slist_prepend
);
359 void parse_paths_shutdown()
367 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
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
))
373 g_slist_free(xdg_data_dir_paths
);
374 xdg_data_dir_paths
= NULL
;
377 gchar
*parse_expand_tilde(const gchar
*f
)
384 spl
= g_strsplit(f
, "~", 0);
385 ret
= g_strjoinv(g_get_home_dir(), spl
);
390 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
394 g_return_val_if_fail(path
!= NULL
, FALSE
);
395 g_return_val_if_fail(path
[0] != '\0', FALSE
);
397 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
398 if (mkdir(path
, mode
) == -1)
404 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
408 g_return_val_if_fail(path
!= NULL
, FALSE
);
409 g_return_val_if_fail(path
[0] == '/', FALSE
);
411 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
416 while ((e
= strchr(e
+ 1, '/'))) {
418 if (!(ret
= parse_mkdir(c
, mode
)))
419 goto parse_mkdir_path_end
;
422 ret
= parse_mkdir(c
, mode
);
424 parse_mkdir_path_end
:
431 const gchar
* parse_xdg_config_home_path()
433 return xdg_config_home_path
;
436 const gchar
* parse_xdg_data_home_path()
438 return xdg_data_home_path
;
441 GSList
* parse_xdg_config_dir_paths()
443 return xdg_config_dir_paths
;
446 GSList
* parse_xdg_data_dir_paths()
448 return xdg_data_dir_paths
;
This page took 0.05099 seconds and 4 git commands to generate.