2007-08-03 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / config-loader-libxml.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-loader-libxml.c  libxml2 XML loader
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
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>
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #include <string.h>
34
35 /* About the error handling: 
36  *  - setup a "structured" error handler that catches structural
37  *    errors and some oom errors 
38  *  - assume that a libxml function returning an error code means
39  *    out-of-memory
40  */
41 #define _DBUS_MAYBE_SET_OOM(e) (dbus_error_is_set(e) ? (void)0 : _DBUS_SET_OOM(e))
42
43
44 static dbus_bool_t
45 xml_text_start_element (BusConfigParser   *parser,
46                         xmlTextReader     *reader,
47                         DBusError         *error)
48 {
49   const char *name;
50   int n_attributes;
51   const char **attribute_names, **attribute_values;
52   dbus_bool_t ret;
53   int i, status, is_empty;
54
55   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
56
57   ret = FALSE;
58   attribute_names = NULL;
59   attribute_values = NULL;
60
61   name = xmlTextReaderConstName (reader);
62   n_attributes = xmlTextReaderAttributeCount (reader);
63   is_empty = xmlTextReaderIsEmptyElement (reader);
64
65   if (name == NULL || n_attributes < 0 || is_empty == -1)
66     {
67       _DBUS_MAYBE_SET_OOM (error);
68       goto out;
69     }
70
71   attribute_names = dbus_new0 (const char *, n_attributes + 1);
72   attribute_values = dbus_new0 (const char *, n_attributes + 1);
73   if (attribute_names == NULL || attribute_values == NULL)
74     {
75       _DBUS_SET_OOM (error);
76       goto out;
77     }
78   i = 0;
79   while ((status = xmlTextReaderMoveToNextAttribute (reader)) == 1)
80     {
81       _dbus_assert (i < n_attributes);
82       attribute_names[i] = xmlTextReaderConstName (reader);
83       attribute_values[i] = xmlTextReaderConstValue (reader);
84       if (attribute_names[i] == NULL || attribute_values[i] == NULL)
85         { 
86           _DBUS_MAYBE_SET_OOM (error);
87           goto out;
88         }
89       i++;
90     }
91   if (status == -1)
92     {
93       _DBUS_MAYBE_SET_OOM (error);
94       goto out;
95     }
96   _dbus_assert (i == n_attributes);
97
98   ret = bus_config_parser_start_element (parser, name,
99                                          attribute_names, attribute_values,
100                                          error);
101   if (ret && is_empty == 1)
102     ret = bus_config_parser_end_element (parser, name, error);
103
104  out:
105   dbus_free (attribute_names);
106   dbus_free (attribute_values);
107
108   return ret;
109 }
110
111 static void xml_shut_up (void *ctx, const char *msg, ...)
112 {
113     return;
114 }
115
116 static void
117 xml_text_reader_error (void *arg, xmlErrorPtr xml_error)
118 {
119   DBusError *error = arg;
120
121 #if 0
122   _dbus_verbose ("XML_ERROR level=%d, domain=%d, code=%d, msg=%s\n",
123                  xml_error->level, xml_error->domain,
124                  xml_error->code, xml_error->message);
125 #endif
126
127   if (!dbus_error_is_set (error))
128     {
129       if (xml_error->code == XML_ERR_NO_MEMORY)
130         _DBUS_SET_OOM (error);
131       else if (xml_error->level == XML_ERR_ERROR ||
132                xml_error->level == XML_ERR_FATAL)
133         dbus_set_error (error, DBUS_ERROR_FAILED,
134                         "Error loading config file: '%s'",
135                         xml_error->message);
136     }
137 }
138
139
140 BusConfigParser*
141 bus_config_load (const DBusString      *file,
142                  dbus_bool_t            is_toplevel,
143                  const BusConfigParser *parent,
144                  DBusError             *error)
145
146 {
147   xmlTextReader *reader;
148   BusConfigParser *parser;
149   DBusString dirname, data;
150   DBusError tmp_error;
151   int ret;
152   
153   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
154   
155   parser = NULL;
156   reader = NULL;
157
158   if (!_dbus_string_init (&dirname))
159     {
160       _DBUS_SET_OOM (error);
161       return NULL;
162     }
163
164   if (!_dbus_string_init (&data))
165     {
166       _DBUS_SET_OOM (error);
167       _dbus_string_free (&dirname);
168       return NULL;
169     }
170
171   if (is_toplevel)
172     {
173       /* xmlMemSetup only fails if one of the functions is NULL */
174       xmlMemSetup (dbus_free,
175                    dbus_malloc,
176                    dbus_realloc,
177                    _dbus_strdup);
178       xmlInitParser ();
179       xmlSetGenericErrorFunc (NULL, xml_shut_up);
180     }
181
182   if (!_dbus_string_get_dirname (file, &dirname))
183     {
184       _DBUS_SET_OOM (error);
185       goto failed;
186     }
187   
188   parser = bus_config_parser_new (&dirname, is_toplevel, parent);
189   if (parser == NULL)
190     {
191       _DBUS_SET_OOM (error);
192       goto failed;
193     }
194   
195   if (!_dbus_file_get_contents (&data, file, error))
196     goto failed;
197
198   reader = xmlReaderForMemory (_dbus_string_get_const_data (&data), 
199                                _dbus_string_get_length (&data),
200                                NULL, NULL, 0);
201   if (reader == NULL)
202     {
203       _DBUS_SET_OOM (error);
204       goto failed;
205     }
206
207   xmlTextReaderSetParserProp (reader, XML_PARSER_SUBST_ENTITIES, 1);
208
209   dbus_error_init (&tmp_error);
210   xmlTextReaderSetStructuredErrorHandler (reader, xml_text_reader_error, &tmp_error);
211
212   while ((ret = xmlTextReaderRead (reader)) == 1)
213     {
214       int type;
215       
216       if (dbus_error_is_set (&tmp_error))
217         goto reader_out;
218
219       type = xmlTextReaderNodeType (reader);
220       if (type == -1)
221         {
222           _DBUS_MAYBE_SET_OOM (&tmp_error);
223           goto reader_out;
224         }
225
226       switch ((xmlReaderTypes) type) {
227       case XML_READER_TYPE_ELEMENT:
228         xml_text_start_element (parser, reader, &tmp_error);
229         break;
230
231       case XML_READER_TYPE_TEXT:
232       case XML_READER_TYPE_CDATA:
233         {
234           DBusString content;
235           const char *value;
236           value = xmlTextReaderConstValue (reader);
237           if (value != NULL)
238             {
239               _dbus_string_init_const (&content, value);
240               bus_config_parser_content (parser, &content, &tmp_error);
241             }
242           else
243             _DBUS_MAYBE_SET_OOM (&tmp_error);
244           break;
245         }
246
247       case XML_READER_TYPE_DOCUMENT_TYPE:
248         {
249           const char *name;
250           name = xmlTextReaderConstName (reader);
251           if (name != NULL)
252             bus_config_parser_check_doctype (parser, name, &tmp_error);
253           else
254             _DBUS_MAYBE_SET_OOM (&tmp_error);
255           break;
256         }
257
258       case XML_READER_TYPE_END_ELEMENT:
259         {
260           const char *name;
261           name = xmlTextReaderConstName (reader);
262           if (name != NULL)
263             bus_config_parser_end_element (parser, name, &tmp_error);
264           else
265             _DBUS_MAYBE_SET_OOM (&tmp_error);
266           break;
267         }
268
269       case XML_READER_TYPE_DOCUMENT:
270       case XML_READER_TYPE_DOCUMENT_FRAGMENT:
271       case XML_READER_TYPE_PROCESSING_INSTRUCTION:
272       case XML_READER_TYPE_COMMENT:
273       case XML_READER_TYPE_ENTITY:
274       case XML_READER_TYPE_NOTATION:
275       case XML_READER_TYPE_WHITESPACE:
276       case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
277       case XML_READER_TYPE_END_ENTITY:
278       case XML_READER_TYPE_XML_DECLARATION:
279         /* nothing to do, just read on */
280         break;
281
282       case XML_READER_TYPE_NONE:
283       case XML_READER_TYPE_ATTRIBUTE:
284       case XML_READER_TYPE_ENTITY_REFERENCE:
285         _dbus_assert_not_reached ("unexpected nodes in XML");
286       }
287
288       if (dbus_error_is_set (&tmp_error))
289         goto reader_out;
290     }
291
292   if (ret == -1)
293     _DBUS_MAYBE_SET_OOM (&tmp_error);
294
295  reader_out:
296   xmlFreeTextReader (reader);
297   reader = NULL;
298   if (dbus_error_is_set (&tmp_error))
299     {
300       dbus_move_error (&tmp_error, error);
301       goto failed;
302     }
303   
304   if (!bus_config_parser_finished (parser, error))
305     goto failed;
306   _dbus_string_free (&dirname);
307   _dbus_string_free (&data);
308   if (is_toplevel)
309     xmlCleanupParser();
310   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
311   return parser;
312   
313  failed:
314   _DBUS_ASSERT_ERROR_IS_SET (error);
315   _dbus_string_free (&dirname);
316   _dbus_string_free (&data);
317   if (is_toplevel)
318     xmlCleanupParser();
319   if (parser)
320     bus_config_parser_unref (parser);
321   _dbus_assert (reader == NULL); /* must go to reader_out first */
322   return NULL;
323 }