Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / bus / config-loader-expat.c
index c7981bf..b571fda 100644 (file)
@@ -1,10 +1,10 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* config-loader-expat.c  expat XML loader
  *
  * Copyright (C) 2003 Red Hat, Inc.
  *
- * Licensed under the Academic Free License version 1.2
- * 
+ * Licensed under the Academic Free License version 2.1
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "config-parser.h"
 #include <dbus/dbus-internals.h>
 #include <expat.h>
 
-static XML_Memory_Handling_Suite memsuite =
-{
-  dbus_malloc,
-  dbus_realloc,
-  dbus_free
-};
+static XML_Memory_Handling_Suite memsuite;
 
 typedef struct
 {
@@ -41,6 +37,27 @@ typedef struct
   dbus_bool_t failed;
 } ExpatParseContext;
 
+static dbus_bool_t
+process_content (ExpatParseContext *context)
+{
+  if (context->failed)
+    return FALSE;
+
+  if (_dbus_string_get_length (&context->content) > 0)
+    {
+      if (!bus_config_parser_content (context->parser,
+                                      &context->content,
+                                      context->error))
+        {
+          context->failed = TRUE;
+          return FALSE;
+        }
+      _dbus_string_set_length (&context->content, 0);
+    }
+
+  return TRUE;
+}
+
 static void
 expat_StartElementHandler (void            *userData,
                            const XML_Char  *name,
@@ -50,13 +67,16 @@ expat_StartElementHandler (void            *userData,
   int i;
   char **names;
   char **values;
-  
+
   /* Expat seems to suck and can't abort the parse if we
    * throw an error. Expat 2.0 is supposed to fix this.
    */
   if (context->failed)
     return;
 
+  if (!process_content (context))
+    return;
+
   /* "atts" is key, value, key, value, NULL */
   for (i = 0; atts[i] != NULL; ++i)
     ; /* nothing */
@@ -73,17 +93,17 @@ expat_StartElementHandler (void            *userData,
       dbus_free (values);
       return;
     }
-  
+
   i = 0;
   while (atts[i] != NULL)
     {
       _dbus_assert (i % 2 == 0);
-      names [i / 2]     = (char*) atts[i];
-      values[i / 2 + 1] = (char*) atts[i+1];
-      
+      names [i / 2] = (char*) atts[i];
+      values[i / 2] = (char*) atts[i+1];
+
       i += 2;
     }
-  
+
   if (!bus_config_parser_start_element (context->parser,
                                         name,
                                         (const char **) names,
@@ -105,20 +125,9 @@ expat_EndElementHandler (void           *userData,
                          const XML_Char *name)
 {
   ExpatParseContext *context = userData;
-  if (context->failed)
-    return;
 
-  if (_dbus_string_get_length (&context->content) > 0)
-    {
-      if (!bus_config_parser_content (context->parser,
-                                      &context->content,
-                                      context->error))
-        {
-          context->failed = TRUE;
-          return;
-        }
-      _dbus_string_set_length (&context->content, 0);
-    }
+  if (!process_content (context))
+    return;
 
   if (!bus_config_parser_end_element (context->parser,
                                       name,
@@ -150,13 +159,16 @@ expat_CharacterDataHandler (void           *userData,
 
 
 BusConfigParser*
-bus_config_load (const DBusString *file,
-                 DBusError        *error)
+bus_config_load (const DBusString      *file,
+                 dbus_bool_t            is_toplevel,
+                 const BusConfigParser *parent,
+                 DBusError             *error)
 {
-  XML_Parser *expat;
+  XML_Parser expat;
   const char *filename;
   BusConfigParser *parser;
   ExpatParseContext context;
+  DBusString dirname;
   
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -164,15 +176,26 @@ bus_config_load (const DBusString *file,
   expat = NULL;
   context.error = error;
   context.failed = FALSE;
-  
-  _dbus_string_get_const_data (file, &filename);
-  
-  if (!_dbus_string_init (&context.content, _DBUS_INT_MAX))
+
+  filename = _dbus_string_get_const_data (file);
+
+  if (!_dbus_string_init (&context.content))
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       return NULL;
     }
-  
+
+  if (!_dbus_string_init (&dirname))
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      _dbus_string_free (&context.content);
+      return NULL;
+    }
+
+  memsuite.malloc_fcn = dbus_malloc;
+  memsuite.realloc_fcn = dbus_realloc;
+  memsuite.free_fcn = dbus_free;
+
   expat = XML_ParserCreate_MM ("UTF-8", &memsuite, NULL);
   if (expat == NULL)
     {
@@ -180,12 +203,19 @@ bus_config_load (const DBusString *file,
       goto failed;
     }
 
-  parser = bus_config_parser_new ();
+  if (!_dbus_string_get_dirname (file, &dirname))
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto failed;
+    }
+  
+  parser = bus_config_parser_new (&dirname, is_toplevel, parent);
   if (parser == NULL)
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       goto failed;
     }
+  context.parser = parser;
 
   XML_SetUserData (expat, &context);
   XML_SetElementHandler (expat,
@@ -197,28 +227,28 @@ bus_config_load (const DBusString *file,
   {
     DBusString data;
     const char *data_str;
-    
-    if (!_dbus_string_init (&data, _DBUS_INT_MAX))
+
+    if (!_dbus_string_init (&data))
       {
         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
         goto failed;
       }
-    
+
     if (!_dbus_file_get_contents (&data, file, error))
       {
         _dbus_string_free (&data);
         goto failed;
       }
-    
-    _dbus_string_get_const_data (&data, &data_str);
-    
+
+    data_str = _dbus_string_get_const_data (&data);
+
     if (!XML_Parse (expat, data_str, _dbus_string_get_length (&data), TRUE))
       {
         if (context.error != NULL &&
             !dbus_error_is_set (context.error))
           {
             enum XML_Error e;
-            
+
             e = XML_GetErrorCode (expat);
             if (e == XML_ERROR_NO_MEMORY)
               dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
@@ -230,7 +260,7 @@ bus_config_load (const DBusString *file,
                               XML_GetCurrentColumnNumber (expat),
                               XML_ErrorString (e));
           }
-        
+
         _dbus_string_free (&data);
         goto failed;
       }
@@ -240,19 +270,21 @@ bus_config_load (const DBusString *file,
     if (context.failed)
       goto failed;
   }
-  
+
   if (!bus_config_parser_finished (parser, error))
     goto failed;
 
+  _dbus_string_free (&dirname);
   _dbus_string_free (&context.content);
   XML_ParserFree (expat);
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   return parser;
-  
+
  failed:
   _DBUS_ASSERT_ERROR_IS_SET (error);
-  
+
+  _dbus_string_free (&dirname);
   _dbus_string_free (&context.content);
   if (expat)
     XML_ParserFree (expat);