1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-loader-expat.c expat 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>
28 static XML_Memory_Handling_Suite memsuite =
37 BusConfigParser *parser;
45 expat_StartElementHandler (void *userData,
47 const XML_Char **atts)
49 ExpatParseContext *context = userData;
54 /* Expat seems to suck and can't abort the parse if we
55 * throw an error. Expat 2.0 is supposed to fix this.
60 /* "atts" is key, value, key, value, NULL */
61 for (i = 0; atts[i] != NULL; ++i)
64 _dbus_assert (i % 2 == 0);
65 names = dbus_new0 (char *, i / 2 + 1);
66 values = dbus_new0 (char *, i / 2 + 1);
68 if (names == NULL || values == NULL)
70 dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
71 context->failed = TRUE;
78 while (atts[i] != NULL)
80 _dbus_assert (i % 2 == 0);
81 names [i / 2] = (char*) atts[i];
82 values[i / 2 + 1] = (char*) atts[i+1];
87 if (!bus_config_parser_start_element (context->parser,
89 (const char **) names,
90 (const char **) values,
95 context->failed = TRUE;
104 expat_EndElementHandler (void *userData,
105 const XML_Char *name)
107 ExpatParseContext *context = userData;
111 if (_dbus_string_get_length (&context->content) > 0)
113 if (!bus_config_parser_content (context->parser,
117 context->failed = TRUE;
120 _dbus_string_set_length (&context->content, 0);
123 if (!bus_config_parser_end_element (context->parser,
127 context->failed = TRUE;
132 /* s is not 0 terminated. */
134 expat_CharacterDataHandler (void *userData,
138 ExpatParseContext *context = userData;
142 if (!_dbus_string_append_len (&context->content,
145 dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
146 context->failed = TRUE;
153 bus_config_load (const DBusString *file,
157 const char *filename;
158 BusConfigParser *parser;
159 ExpatParseContext context;
161 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
165 context.error = error;
166 context.failed = FALSE;
168 _dbus_string_get_const_data (file, &filename);
170 if (!_dbus_string_init (&context.content, _DBUS_INT_MAX))
172 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
176 expat = XML_ParserCreate_MM ("UTF-8", &memsuite, NULL);
179 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
183 parser = bus_config_parser_new ();
186 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
190 XML_SetUserData (expat, &context);
191 XML_SetElementHandler (expat,
192 expat_StartElementHandler,
193 expat_EndElementHandler);
194 XML_SetCharacterDataHandler (expat,
195 expat_CharacterDataHandler);
199 const char *data_str;
201 if (!_dbus_string_init (&data, _DBUS_INT_MAX))
203 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
207 if (!_dbus_file_get_contents (&data, file, error))
209 _dbus_string_free (&data);
213 _dbus_string_get_const_data (&data, &data_str);
215 if (!XML_Parse (expat, data_str, _dbus_string_get_length (&data), TRUE))
217 if (context.error != NULL &&
218 !dbus_error_is_set (context.error))
222 e = XML_GetErrorCode (expat);
223 if (e == XML_ERROR_NO_MEMORY)
224 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
226 dbus_set_error (error, DBUS_ERROR_FAILED,
227 "Error in file %s, line %d, column %d: %s\n",
229 XML_GetCurrentLineNumber (expat),
230 XML_GetCurrentColumnNumber (expat),
231 XML_ErrorString (e));
234 _dbus_string_free (&data);
238 _dbus_string_free (&data);
244 if (!bus_config_parser_finished (parser, error))
247 _dbus_string_free (&context.content);
248 XML_ParserFree (expat);
250 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
254 _DBUS_ASSERT_ERROR_IS_SET (error);
256 _dbus_string_free (&context.content);
258 XML_ParserFree (expat);
260 bus_config_parser_unref (parser);