]>
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
,
91 if ((r
= parse_load(file
, "openbox_config", doc
, root
)))
92 *fileused
= g_strdup(file
);
94 /* this won't run if the above code loaded a config */
95 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
98 path
= g_build_filename(it
->data
, "openbox", "rc.xml", NULL
);
99 if ((r
= parse_load(path
, "openbox_config", doc
, root
)))
106 g_warning("Unable to find a valid config file, using defaults");
110 gboolean
parse_load_theme(const gchar
*name
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
117 /* backward compatibility.. */
118 path
= g_build_filename(g_get_home_dir(), ".themes", name
,
119 "openbox-3", "themerc.xml", NULL
);
120 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
121 *retpath
= g_path_get_dirname(path
);
125 for (it
= xdg_data_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
126 path
= g_build_filename(it
->data
, "themes", name
, "openbox-3",
127 "themerc.xml", NULL
);
128 if ((r
= parse_load(path
, "openbox_theme", doc
, root
)))
129 *retpath
= g_path_get_dirname(path
);
134 g_warning("Unable to load the theme %s", name
);
138 gboolean
parse_load_menu(const gchar
*file
, xmlDocPtr
*doc
, xmlNodePtr
*root
)
144 if (file
[0] == '/') {
145 r
= parse_load(file
, "openbox_menu", doc
, root
);
147 for (it
= xdg_config_dir_paths
; !r
&& it
; it
= g_slist_next(it
)) {
148 path
= g_build_filename(it
->data
, "openbox", file
, NULL
);
149 r
= parse_load(path
, "openbox_menu", doc
, root
);
154 g_warning("Unable to find a valid menu file '%s'", file
);
158 gboolean
parse_load(const gchar
*path
, const gchar
*rootname
,
159 xmlDocPtr
*doc
, xmlNodePtr
*root
)
162 if (stat(path
, &s
) < 0)
165 /* XML_PARSE_BLANKS is needed apparently. When it loads a theme file,
166 without this option, the tree is weird and has extra nodes in it. */
167 if ((*doc
= xmlReadFile(path
, NULL
,
168 XML_PARSE_NOBLANKS
| XML_PARSE_RECOVER
))) {
169 *root
= xmlDocGetRootElement(*doc
);
173 g_warning("%s is an empty document", path
);
175 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
178 g_warning("Document %s is of wrong type. root node is "
179 "not '%s'", path
, rootname
);
188 gboolean
parse_load_mem(gpointer data
, guint len
, const gchar
*rootname
,
189 xmlDocPtr
*doc
, xmlNodePtr
*root
)
191 if ((*doc
= xmlParseMemory(data
, len
))) {
192 *root
= xmlDocGetRootElement(*doc
);
196 g_warning("Given memory is an empty document");
198 if (xmlStrcmp((*root
)->name
, (const xmlChar
*)rootname
)) {
201 g_warning("Document in given memory is of wrong type. root "
202 "node is not '%s'", rootname
);
211 void parse_close(xmlDocPtr doc
)
216 void parse_tree(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
)
219 struct Callback
*c
= g_hash_table_lookup(i
->callbacks
, node
->name
);
222 c
->func(i
, doc
, node
, c
->data
);
228 gchar
*parse_string(xmlDocPtr doc
, xmlNodePtr node
)
230 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
231 gchar
*s
= g_strdup(c
? (gchar
*)c
: "");
236 gint
parse_int(xmlDocPtr doc
, xmlNodePtr node
)
238 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
239 gint i
= atoi((gchar
*)c
);
244 gboolean
parse_bool(xmlDocPtr doc
, xmlNodePtr node
)
246 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
248 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
250 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
252 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
258 gboolean
parse_contains(const gchar
*val
, xmlDocPtr doc
, xmlNodePtr node
)
260 xmlChar
*c
= xmlNodeListGetString(doc
, node
->children
, TRUE
);
262 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
267 xmlNodePtr
parse_find_node(const gchar
*tag
, xmlNodePtr node
)
270 if (!xmlStrcmp(node
->name
, (const xmlChar
*) tag
))
277 gboolean
parse_attr_bool(const gchar
*name
, xmlNodePtr node
, gboolean
*value
)
279 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
282 if (!xmlStrcasecmp(c
, (const xmlChar
*) "true"))
283 *value
= TRUE
, r
= TRUE
;
284 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "yes"))
285 *value
= TRUE
, r
= TRUE
;
286 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "on"))
287 *value
= TRUE
, r
= TRUE
;
288 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "false"))
289 *value
= FALSE
, r
= TRUE
;
290 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "no"))
291 *value
= FALSE
, r
= TRUE
;
292 else if (!xmlStrcasecmp(c
, (const xmlChar
*) "off"))
293 *value
= FALSE
, r
= TRUE
;
299 gboolean
parse_attr_int(const gchar
*name
, xmlNodePtr node
, gint
*value
)
301 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
304 *value
= atoi((gchar
*)c
);
311 gboolean
parse_attr_string(const gchar
*name
, xmlNodePtr node
, gchar
**value
)
313 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
316 *value
= g_strdup((gchar
*)c
);
323 gboolean
parse_attr_contains(const gchar
*val
, xmlNodePtr node
,
326 xmlChar
*c
= xmlGetProp(node
, (const xmlChar
*) name
);
329 r
= !xmlStrcasecmp(c
, (const xmlChar
*) val
);
334 static gint
slist_path_cmp(const gchar
*a
, const gchar
*b
)
339 typedef GSList
* (*GSListFunc
) (gpointer list
, gconstpointer data
);
341 static GSList
* slist_path_add(GSList
*list
, gpointer data
, GSListFunc func
)
348 if (!g_slist_find_custom(list
, data
, (GCompareFunc
) slist_path_cmp
))
349 list
= func(list
, data
);
356 static GSList
* split_paths(const gchar
*paths
)
363 spl
= g_strsplit(paths
, ":", -1);
364 for (it
= spl
; *it
; ++it
)
365 list
= slist_path_add(list
, *it
, (GSListFunc
) g_slist_append
);
370 void parse_paths_startup()
378 path
= g_getenv("XDG_CONFIG_HOME");
379 if (path
&& path
[0] != '\0') /* not unset or empty */
380 xdg_config_home_path
= g_build_filename(path
, NULL
);
382 xdg_config_home_path
= g_build_filename(g_get_home_dir(), ".config",
385 path
= g_getenv("XDG_DATA_HOME");
386 if (path
&& path
[0] != '\0') /* not unset or empty */
387 xdg_data_home_path
= g_build_filename(path
, NULL
);
389 xdg_data_home_path
= g_build_filename(g_get_home_dir(), ".local",
392 path
= g_getenv("XDG_CONFIG_DIRS");
393 if (path
&& path
[0] != '\0') /* not unset or empty */
394 xdg_config_dir_paths
= split_paths(path
);
396 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
398 (GSListFunc
) g_slist_append
);
399 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
403 (GSListFunc
) g_slist_append
);
405 xdg_config_dir_paths
= slist_path_add(xdg_config_dir_paths
,
406 g_strdup(xdg_config_home_path
),
407 (GSListFunc
) g_slist_prepend
);
409 path
= g_getenv("XDG_DATA_DIRS");
410 if (path
&& path
[0] != '\0') /* not unset or empty */
411 xdg_data_dir_paths
= split_paths(path
);
413 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
416 "usr", "local", "share", NULL
),
417 (GSListFunc
) g_slist_append
);
418 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
421 "usr", "share", NULL
),
422 (GSListFunc
) g_slist_append
);
423 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
425 (GSListFunc
) g_slist_append
);
427 xdg_data_dir_paths
= slist_path_add(xdg_data_dir_paths
,
428 g_strdup(xdg_data_home_path
),
429 (GSListFunc
) g_slist_prepend
);
432 void parse_paths_shutdown()
440 for (it
= xdg_config_dir_paths
; it
; it
= g_slist_next(it
))
442 g_slist_free(xdg_config_dir_paths
);
443 xdg_config_dir_paths
= NULL
;
444 for (it
= xdg_data_dir_paths
; it
; it
= g_slist_next(it
))
446 g_slist_free(xdg_data_dir_paths
);
447 xdg_data_dir_paths
= NULL
;
448 g_free(xdg_config_home_path
);
449 xdg_config_home_path
= NULL
;
450 g_free(xdg_data_home_path
);
451 xdg_data_home_path
= NULL
;
454 gchar
*parse_expand_tilde(const gchar
*f
)
461 spl
= g_strsplit(f
, "~", 0);
462 ret
= g_strjoinv(g_get_home_dir(), spl
);
467 gboolean
parse_mkdir(const gchar
*path
, gint mode
)
471 g_return_val_if_fail(path
!= NULL
, FALSE
);
472 g_return_val_if_fail(path
[0] != '\0', FALSE
);
474 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
))
475 if (mkdir(path
, mode
) == -1)
481 gboolean
parse_mkdir_path(const gchar
*path
, gint mode
)
485 g_return_val_if_fail(path
!= NULL
, FALSE
);
486 g_return_val_if_fail(path
[0] == '/', FALSE
);
488 if (!g_file_test(path
, G_FILE_TEST_IS_DIR
)) {
493 while ((e
= strchr(e
+ 1, '/'))) {
495 if (!(ret
= parse_mkdir(c
, mode
)))
496 goto parse_mkdir_path_end
;
499 ret
= parse_mkdir(c
, mode
);
501 parse_mkdir_path_end
:
508 const gchar
* parse_xdg_config_home_path()
510 return xdg_config_home_path
;
513 const gchar
* parse_xdg_data_home_path()
515 return xdg_data_home_path
;
518 GSList
* parse_xdg_config_dir_paths()
520 return xdg_config_dir_paths
;
523 GSList
* parse_xdg_data_dir_paths()
525 return xdg_data_dir_paths
;
This page took 0.057616 seconds and 4 git commands to generate.