]>
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(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
90 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
92 if ((r
= parse_load(file
, "openbox_config", doc
, root
)))
93 *fileused
= g_strdup(file
);
97 path
= g_build_filename(it
->data
, "openbox", "rc.xml", NULL
);
98 if ((r
= parse_load(path
, "openbox_config", doc
, root
)))
105 g_warning("Unable to find a valid config file, using defaults");
109 gboolean
parse_load_theme(const gchar
*name
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
116 /* backward compatibility.. */
117 path
= g_build_filename(g_get_home_dir(), ".themes", name
,
118 "openbox-3", "themerc.xml", NULL
);
119 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
120 *retpath
= g_path_get_dirname(path
);
124 for (it
= xdg_data_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
125 path
= g_build_filename(it
->data
, "themes", name
, "openbox-3",
126 "themerc.xml", NULL
);
127 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
128 *retpath
= g_path_get_dirname(path
);
133 g_warning("Unable to load the theme %s", name
);
137 gboolean
parse_load_menu(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
143 if (file
[0] == '/') {
144 r
= parse_load(file
, "openbox_menu", doc
, root
);
146 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
147 path
= g_build_filename(it
->data
, "openbox", file
, NULL
);
148 r
= parse_load(path
, "openbox_menu", doc
, root
);
153 g_warning("Unable to find a valid menu file '%s'", file
);
157 gboolean
parse_load(const gchar
*path
, const gchar
*rootname
,
158 xmlDocPtr
*doc
, xmlNodePtr
*root
)
161 if (stat(path
, &s
) < 0)
164 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
165 without this option, the tree is weird and has extra nodes in it. */
166 if ((*doc
= xmlReadFile(path
, NULL
,
167 XML_PARSE_NOBLANKS
| XML_PARSE_RECOVER
))) {
168 *root
= xmlDocGetRootElement(*doc
);
172 g_warning("%s is an empty document", path
);
174 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
177 g_warning("Document %s is of wrong type. root node is "
178 "not '%s'", path
, rootname
);
187 gboolean
parse_load_mem(gpointer data
, guint len
, const gchar
*rootname
,
188 xmlDocPtr
*doc
, xmlNodePtr
*root
)
190 if ((*doc
= xmlParseMemory(data
, len
))) {
191 *root
= xmlDocGetRootElement(*doc
);
195 g_warning("Given memory is an empty document");
197 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
200 g_warning("Document in given memory is of wrong type. root "
201 "node is not '%s'", rootname
);
210 void parse_close(xmlDocPtr doc
)
215 void parse_tree(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
)
218 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
221 c
->func(i
, doc
, node
, c
->data
);
227 gchar
*parse_string(xmlDocPtr doc
, xmlNodePtr node
)
229 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
230 gchar
*s
= g_strdup(c
? (gchar
*)c
: "");
235 gint
parse_int(xmlDocPtr doc
, xmlNodePtr node
)
237 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
238 gint i
= atoi((gchar
*)c
);
243 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
245 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
247 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
249 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
251 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
257 gboolean
parse_contains(const gchar
*val
, xmlDocPtr doc
, xmlNodePtr node
)
259 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
261 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
266 xmlNodePtr
parse_find_node(const gchar
*tag
, xmlNodePtr node
)
269 if (!xmlStrcmp(node
->name
, (const xmlChar
*) tag
))
276 gboolean
parse_attr_bool(const gchar
*name
, xmlNodePtr node
, gboolean
*value
)
278 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
281 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
282 *value
= TRUE
, r
= TRUE
;
283 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
284 *value
= TRUE
, r
= TRUE
;
285 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
286 *value
= TRUE
, r
= TRUE
;
287 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "false"))
288 *value
= FALSE
, r
= TRUE
;
289 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "no"))
290 *value
= FALSE
, r
= TRUE
;
291 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "off"))
292 *value
= FALSE
, r
= TRUE
;
298 gboolean
parse_attr_int(const gchar
*name
, xmlNodePtr node
, gint
*value
)
300 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
303 *value
= atoi((gchar
*)c
);
310 gboolean
parse_attr_string(const gchar
*name
, xmlNodePtr node
, gchar
**value
)
312 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
315 *value
= g_strdup((gchar
*)c
);
322 gboolean
parse_attr_contains(const gchar
*val
, xmlNodePtr node
,
325 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
328 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
333 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
338 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
340 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
347 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
348 list
= func(list
, data
);
355 static GSList
* split_paths(const gchar
*paths
)
362 spl
= g_strsplit(paths
, ":", -1);
363 for (it
= spl
; *it
; ++it
)
364 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
369 void parse_paths_startup()
377 path
= g_getenv("XDG_CONFIG_HOME");
378 if (path
&& path
[0] != '\0') /* not unset or empty */
379 xdg_config_home_path
= g_build_filename(path
, NULL
);
381 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
384 path
= g_getenv("XDG_DATA_HOME");
385 if (path
&& path
[0] != '\0') /* not unset or empty */
386 xdg_data_home_path
= g_build_filename(path
, NULL
);
388 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
391 path
= g_getenv("XDG_CONFIG_DIRS");
392 if (path
&& path
[0] != '\0') /* not unset or empty */
393 xdg_config_dir_paths
= split_paths(path
);
395 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
399 (GSListFunc
) g_slist_append
);
400 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
402 (GSListFunc
) g_slist_append
);
404 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
405 g_strdup(xdg_config_home_path
),
406 (GSListFunc
) g_slist_prepend
);
408 path
= g_getenv("XDG_DATA_DIRS");
409 if (path
&& path
[0] != '\0') /* not unset or empty */
410 xdg_data_dir_paths
= split_paths(path
);
412 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
415 "usr", "local", "share", NULL
),
416 (GSListFunc
) g_slist_append
);
417 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
420 "usr", "share", NULL
),
421 (GSListFunc
) g_slist_append
);
422 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
424 (GSListFunc
) g_slist_append
);
426 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
427 g_strdup(xdg_data_home_path
),
428 (GSListFunc
) g_slist_prepend
);
431 void parse_paths_shutdown()
439 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
441 g_slist_free(xdg_config_dir_paths
);
442 xdg_config_dir_paths
= NULL
;
443 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
445 g_slist_free(xdg_data_dir_paths
);
446 xdg_data_dir_paths
= NULL
;
447 g_free(xdg_config_home_path
);
448 xdg_config_home_path
= NULL
;
449 g_free(xdg_data_home_path
);
450 xdg_data_home_path
= NULL
;
453 gchar
*parse_expand_tilde(const gchar
*f
)
460 spl
= g_strsplit(f
, "~", 0);
461 ret
= g_strjoinv(g_get_home_dir(), spl
);
466 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
470 g_return_val_if_fail(path
!= NULL
, FALSE
);
471 g_return_val_if_fail(path
[0] != '\0', FALSE
);
473 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
474 if (mkdir(path
, mode
) == -1)
480 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
484 g_return_val_if_fail(path
!= NULL
, FALSE
);
485 g_return_val_if_fail(path
[0] == '/', FALSE
);
487 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
492 while ((e
= strchr(e
+ 1, '/'))) {
494 if (!(ret
= parse_mkdir(c
, mode
)))
495 goto parse_mkdir_path_end
;
498 ret
= parse_mkdir(c
, mode
);
500 parse_mkdir_path_end
:
507 const gchar
* parse_xdg_config_home_path()
509 return xdg_config_home_path
;
512 const gchar
* parse_xdg_data_home_path()
514 return xdg_data_home_path
;
517 GSList
* parse_xdg_config_dir_paths()
519 return xdg_config_dir_paths
;
522 GSList
* parse_xdg_data_dir_paths()
524 return xdg_data_dir_paths
;
This page took 0.059169 seconds and 5 git commands to generate.