2003-03-26 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / bus / config-loader-expat.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-loader-expat.c  expat XML loader
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
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 <expat.h>
27
28 static void*
29 expat_malloc (size_t size)
30 {
31   return dbus_malloc (size);
32 }
33
34 static void*
35 expat_realloc (void *ptr, size_t size)
36 {
37   return dbus_realloc (ptr, size);
38 }
39
40 static void
41 expat_free (void *ptr)
42 {
43   dbus_free (ptr);
44 }
45
46 static XML_Memory_Handling_Suite memsuite =
47 {
48   expat_malloc,
49   expat_realloc,
50   expat_free
51 };
52
53 typedef struct
54 {
55   BusConfigParser *parser;
56   const char *filename;
57   DBusString content;
58   DBusError *error;
59   dbus_bool_t failed;
60 } ExpatParseContext;
61
62 static void
63 expat_StartElementHandler (void            *userData,
64                            const XML_Char  *name,
65                            const XML_Char **atts)
66 {
67   ExpatParseContext *context = userData;
68   int i;
69   char **names;
70   char **values;
71   
72   /* Expat seems to suck and can't abort the parse if we
73    * throw an error. Expat 2.0 is supposed to fix this.
74    */
75   if (context->failed)
76     return;
77
78   /* "atts" is key, value, key, value, NULL */
79   for (i = 0; atts[i] != NULL; ++i)
80     ; /* nothing */
81
82   _dbus_assert (i % 2 == 0);
83   names = dbus_new0 (char *, i / 2 + 1);
84   values = dbus_new0 (char *, i / 2 + 1);
85
86   if (names == NULL || values == NULL)
87     {
88       dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
89       context->failed = TRUE;
90       dbus_free (names);
91       dbus_free (values);
92       return;
93     }
94   
95   i = 0;
96   while (atts[i] != NULL)
97     {
98       _dbus_assert (i % 2 == 0);
99       names [i / 2]     = (char*) atts[i];
100       values[i / 2 + 1] = (char*) atts[i+1];
101       
102       i += 2;
103     }
104   
105   if (!bus_config_parser_start_element (context->parser,
106                                         name,
107                                         (const char **) names,
108                                         (const char **) values,
109                                         context->error))
110     {
111       dbus_free (names);
112       dbus_free (values);
113       context->failed = TRUE;
114       return;
115     }
116
117   dbus_free (names);
118   dbus_free (values);
119 }
120
121 static void
122 expat_EndElementHandler (void           *userData,
123                          const XML_Char *name)
124 {
125   ExpatParseContext *context = userData;
126   if (context->failed)
127     return;
128
129   if (_dbus_string_get_length (&context->content) > 0)
130     {
131       if (!bus_config_parser_content (context->parser,
132                                       &context->content,
133                                       context->error))
134         {
135           context->failed = TRUE;
136           return;
137         }
138       _dbus_string_set_length (&context->content, 0);
139     }
140
141   if (!bus_config_parser_end_element (context->parser,
142                                       name,
143                                       context->error))
144     {
145       context->failed = TRUE;
146       return;
147     }
148 }
149
150 /* s is not 0 terminated. */
151 static void
152 expat_CharacterDataHandler (void           *userData,
153                             const XML_Char *s,
154                             int             len)
155 {
156   ExpatParseContext *context = userData;
157   if (context->failed)
158     return;
159
160   if (!_dbus_string_append_len (&context->content,
161                                 s, len))
162     {
163       dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
164       context->failed = TRUE;
165       return;
166     }
167 }
168
169
170 BusConfigParser*
171 bus_config_load (const DBusString *file,
172                  DBusError        *error)
173 {
174   XML_Parser *expat;
175   const char *filename;
176   BusConfigParser *parser;
177   ExpatParseContext context;
178   
179   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
180
181   parser = NULL;
182   expat = NULL;
183   context.error = error;
184   context.failed = FALSE;
185   
186   _dbus_string_get_const_data (file, &filename);
187   
188   if (!_dbus_string_init (&context.content, _DBUS_INT_MAX))
189     {
190       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
191       return NULL;
192     }
193   
194   expat = XML_ParserCreate_MM ("UTF-8", &memsuite, NULL);
195   if (expat == NULL)
196     {
197       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
198       goto failed;
199     }
200
201   parser = bus_config_parser_new ();
202   if (parser == NULL)
203     {
204       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
205       goto failed;
206     }
207
208   XML_SetUserData (expat, &context);
209   XML_SetElementHandler (expat,
210                          expat_StartElementHandler,
211                          expat_EndElementHandler);
212   XML_SetCharacterDataHandler (expat,
213                                expat_CharacterDataHandler);
214
215   {
216     DBusString data;
217     const char *data_str;
218     
219     if (!_dbus_string_init (&data, _DBUS_INT_MAX))
220       {
221         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
222         goto failed;
223       }
224     
225     if (!_dbus_file_get_contents (&data, file, error))
226       {
227         _dbus_string_free (&data);
228         goto failed;
229       }
230     
231     _dbus_string_get_const_data (&data, &data_str);
232     
233     if (!XML_Parse (expat, data_str, _dbus_string_get_length (&data), TRUE))
234       {
235         if (context.error != NULL &&
236             !dbus_error_is_set (context.error))
237           {
238             enum XML_Error e;
239             
240             e = XML_GetErrorCode (expat);
241             if (e == XML_ERROR_NO_MEMORY)
242               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
243             else
244               dbus_set_error (error, DBUS_ERROR_FAILED,
245                               "Error in file %s, line %d, column %d: %s\n",
246                               filename,
247                               XML_GetCurrentLineNumber (expat),
248                               XML_GetCurrentColumnNumber (expat),
249                               XML_ErrorString (e));
250           }
251         
252         _dbus_string_free (&data);
253         goto failed;
254       }
255
256     _dbus_string_free (&data);
257
258     if (context.failed)
259       goto failed;
260   }
261   
262   if (!bus_config_parser_finished (parser, error))
263     goto failed;
264
265   _dbus_string_free (&context.content);
266   XML_ParserFree (expat);
267
268   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
269   return parser;
270   
271  failed:
272   _DBUS_ASSERT_ERROR_IS_SET (error);
273   
274   _dbus_string_free (&context.content);
275   if (expat)
276     XML_ParserFree (expat);
277   if (parser)
278     bus_config_parser_unref (parser);
279   return NULL;
280 }