1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-loader-libxml.c libxml2 XML loader
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config-parser.h"
25 #include <dbus/dbus-internals.h>
26 #include <libxml/xmlreader.h>
27 #include <libxml/parser.h>
28 #include <libxml/globals.h>
29 #include <libxml/xmlmemory.h>
34 libxml_malloc (size_t size)
36 return dbus_malloc (size);
40 libxml_realloc (void *ptr, size_t size)
42 return dbus_realloc (ptr, size);
46 libxml_free (void *ptr)
52 libxml_strdup (const char *str)
54 return _dbus_strdup (str);
58 xml_text_reader_error (void *arg,
60 xmlParserSeverities severity,
61 xmlTextReaderLocatorPtr locator)
63 DBusError *error = arg;
65 if (!dbus_error_is_set (error))
67 dbus_set_error (error, DBUS_ERROR_FAILED,
68 "Error loading config file: %s",
74 bus_config_load (const DBusString *file,
77 xmlTextReader *reader;
79 BusConfigParser *parser;
83 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
85 _dbus_string_get_const_data (file, &filename);
88 dbus_error_init (&tmp_error);
90 if (xmlMemSetup (libxml_free,
95 /* Current libxml can't possibly fail here, but just being
96 * paranoid; don't really know why xmlMemSetup() returns an
97 * error code, assuming some version of libxml had a reason.
99 dbus_set_error (error, DBUS_ERROR_FAILED,
100 "xmlMemSetup() didn't work for some reason\n");
104 parser = bus_config_parser_new ();
107 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
112 reader = xmlNewTextReaderFilename (filename);
116 dbus_set_error (error, DBUS_ERROR_FAILED,
117 "Failed to load configuration file %s: %s\n",
119 errno != 0 ? strerror (errno) : "Unknown error");
124 xmlTextReaderSetErrorHandler (reader, xml_text_reader_error, &tmp_error);
126 while ((ret = xmlTextReaderRead (reader)) == 1)
130 if (dbus_error_is_set (&tmp_error))
133 /* "enum" anyone? http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html for
136 type = xmlTextReaderNodeType (reader);
137 if (dbus_error_is_set (&tmp_error))
140 /* FIXME I don't really know exactly what I need to do to
141 * resolve all entities and so on to get the full content of a
142 * node or attribute value. I'm worried about whether I need to
143 * manually handle stuff like <
149 if (!dbus_error_is_set (&tmp_error))
150 dbus_set_error (&tmp_error,
152 "Unknown failure loading configuration file");
156 xmlFreeTextReader (reader);
158 if (dbus_error_is_set (&tmp_error))
160 dbus_move_error (&tmp_error, error);
164 if (!bus_config_parser_finished (parser, error))
167 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
171 _DBUS_ASSERT_ERROR_IS_SET (error);
173 bus_config_parser_unref (parser);
174 _dbus_assert (reader == NULL); /* must go to reader_out first */