Bug 555311 – format not a string literal and no format arguments
[platform/upstream/glib.git] / glib / gmarkup.c
index ca6c445..ccc7626 100644 (file)
@@ -1,6 +1,7 @@
 /* gmarkup.c - Simple XML-like parser
  *
  *  Copyright 2000, 2003 Red Hat, Inc.
+ *  Copyright 2007, 2008 Ryan Lortie <desrt@desrt.ca>
  *
  * GLib is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
@@ -57,6 +58,13 @@ typedef enum
   STATE_ERROR
 } GMarkupParseState;
 
+typedef struct
+{
+  const char *prev_element;
+  const GMarkupParser *prev_parser;
+  gpointer prev_user_data;
+} GMarkupRecursionTracker;
+
 struct _GMarkupParseContext
 {
   const GMarkupParser *parser;
@@ -95,7 +103,13 @@ struct _GMarkupParseContext
 
   guint document_empty : 1;
   guint parsing : 1;
+  guint awaiting_pop : 1;
   gint balance;
+
+  /* subparser support */
+  GSList *subparser_stack; /* (GMarkupRecursionTracker *) */
+  const char *subparser_element;
+  gpointer held_user_data;
 };
 
 /**
@@ -153,6 +167,13 @@ g_markup_parse_context_new (const GMarkupParser *parser,
   context->document_empty = TRUE;
   context->parsing = FALSE;
 
+  context->awaiting_pop = FALSE;
+  context->subparser_stack = NULL;
+  context->subparser_element = NULL;
+
+  /* this is only looked at if awaiting_pop = TRUE.  initialise anyway. */
+  context->held_user_data = NULL;
+
   context->balance = 0;
 
   return context;
@@ -163,14 +184,16 @@ g_markup_parse_context_new (const GMarkupParser *parser,
  * @context: a #GMarkupParseContext
  * 
  * Frees a #GMarkupParseContext. Can't be called from inside
- * one of the #GMarkupParser functions.
- * 
+ * one of the #GMarkupParser functions. Can't be called while
+ * a subparser is pushed.
  **/
 void
 g_markup_parse_context_free (GMarkupParseContext *context)
 {
   g_return_if_fail (context != NULL);
   g_return_if_fail (!context->parsing);
+  g_return_if_fail (!context->subparser_stack);
+  g_return_if_fail (!context->awaiting_pop);
 
   if (context->dnotify)
     (* context->dnotify) (context->user_data);
@@ -190,6 +213,8 @@ g_markup_parse_context_free (GMarkupParseContext *context)
   g_free (context);
 }
 
+static void pop_subparser_stack (GMarkupParseContext *context);
+
 static void
 mark_error (GMarkupParseContext *context,
             GError              *error)
@@ -198,6 +223,16 @@ mark_error (GMarkupParseContext *context,
 
   if (context->parser->error)
     (*context->parser->error) (context, error, context->user_data);
+
+  /* report the error all the way up to free all the user-data */
+  while (context->subparser_stack)
+    {
+      pop_subparser_stack (context);
+      context->awaiting_pop = FALSE; /* already been freed */
+
+      if (context->parser->error)
+        (*context->parser->error) (context, error, context->user_data);
+    }
 }
 
 static void set_error (GMarkupParseContext *context,
@@ -207,31 +242,47 @@ static void set_error (GMarkupParseContext *context,
                       ...) G_GNUC_PRINTF (4, 5);
 
 static void
+set_error_literal (GMarkupParseContext *context,
+                   GError             **error,
+                   GMarkupError         code,
+                   const gchar         *message)
+{
+  GError *tmp_error;
+
+  tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, message);
+
+  g_prefix_error (&tmp_error,
+                  _("Error on line %d char %d: "),
+                  context->line_number,
+                  context->char_number);
+
+  mark_error (context, tmp_error);
+
+  g_propagate_error (error, tmp_error);
+}
+
+static void
 set_error (GMarkupParseContext *context,
            GError             **error,
            GMarkupError         code,
            const gchar         *format,
            ...)
 {
-  GError *tmp_error;
   gchar *s;
+  gchar *s_valid;
   va_list args;
 
   va_start (args, format);
   s = g_strdup_vprintf (format, args);
   va_end (args);
 
-  tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, s);
-  g_free (s);
-
-  g_prefix_error (&tmp_error,
-                  _("Error on line %d char %d: "),
-                  context->line_number,
-                  context->char_number);
+  /* Make sure that the GError message is valid UTF-8 even if it is
+   * complaining about invalid UTF-8 in the markup: */
+  s_valid = _g_utf8_make_valid (s);
+  set_error_literal (context, error, code, s);
 
-  mark_error (context, tmp_error);
-
-  g_propagate_error (error, tmp_error);
+  g_free (s);
+  g_free (s_valid);
 }
 
 static void
@@ -593,13 +644,10 @@ unescape_text_state_after_charref_hash (UnescapeContext *ucontext,
             }
           else
             {
-              /* characters XML permits */
-              if (l == 0x9 ||
-                  l == 0xA ||
-                  l == 0xD ||
-                  (l >= 0x20 && l <= 0xD7FF) ||
-                  (l >= 0xE000 && l <= 0xFFFD) ||
-                  (l >= 0x10000 && l <= 0x10FFFF))
+              /* characters XML 1.1 permits */
+              if ((0 < l && l <= 0xD7FF) ||
+                  (0xE000 <= l && l <= 0xFFFD) ||
+                  (0x10000 <= l && l <= 0x10FFFF))
                 {
                   gchar buf[8];
                   g_string_append (ucontext->str, char_str (l, buf));
@@ -821,6 +869,49 @@ current_element (GMarkupParseContext *context)
   return context->tag_stack->data;
 }
 
+static void
+pop_subparser_stack (GMarkupParseContext *context)
+{
+  GMarkupRecursionTracker *tracker;
+
+  g_assert (context->subparser_stack);
+
+  tracker = context->subparser_stack->data;
+
+  context->awaiting_pop = TRUE;
+  context->held_user_data = context->user_data;
+
+  context->user_data = tracker->prev_user_data;
+  context->parser = tracker->prev_parser;
+  context->subparser_element = tracker->prev_element;
+  g_slice_free (GMarkupRecursionTracker, tracker);
+
+  context->subparser_stack = g_slist_delete_link (context->subparser_stack,
+                                                  context->subparser_stack);
+}
+
+static void
+possibly_finish_subparser (GMarkupParseContext *context)
+{
+  if (current_element (context) == context->subparser_element)
+    pop_subparser_stack (context);
+}
+
+static void
+ensure_no_outstanding_subparser (GMarkupParseContext *context)
+{
+  if (context->awaiting_pop)
+    g_critical ("During the first end_element call after invoking a "
+               "subparser you must pop the subparser stack and handle "
+               "the freeing of the subparser user_data.  This can be "
+               "done by calling the end function of the subparser.  "
+               "Very probably, your program just leaked memory.");
+
+  /* let valgrind watch the pointer disappear... */
+  context->held_user_data = NULL;
+  context->awaiting_pop = FALSE;
+}
+
 static const gchar*
 current_attribute (GMarkupParseContext *context)
 {
@@ -966,10 +1057,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
               /* The leftover char portion is too big to be
                * a UTF-8 character
                */
-              set_error (context,
-                         error,
-                         G_MARKUP_ERROR_BAD_UTF8,
-                         _("Invalid UTF-8 encoded text - overlong sequence"));
+              set_error_literal (context,
+                                 error,
+                                 G_MARKUP_ERROR_BAD_UTF8,
+                                 _("Invalid UTF-8 encoded text - overlong sequence"));
             }
           
           goto finished;
@@ -994,10 +1085,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
    */
   if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
     {
-      set_error (context,
-                 error,
-                 G_MARKUP_ERROR_BAD_UTF8,
-                 _("Invalid UTF-8 encoded text - not a start char"));
+      set_error_literal (context,
+                         error,
+                         G_MARKUP_ERROR_BAD_UTF8,
+                         _("Invalid UTF-8 encoded text - not a start char"));
       goto finished;
     }
 
@@ -1015,6 +1106,8 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
     {
       gint newlines = 0;
       const gchar *p, *q;
+      gchar *current_text_dup;
+
       q = p = context->current_text;
       while (p != first_invalid)
         {
@@ -1030,12 +1123,13 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
       context->line_number += newlines;
       context->char_number += g_utf8_strlen (q, first_invalid - q);
 
+      current_text_dup = g_strndup (context->current_text, context->current_text_len);
       set_error (context,
                  error,
                  G_MARKUP_ERROR_BAD_UTF8,
                  _("Invalid UTF-8 encoded text - not valid '%s'"),
-                 g_strndup (context->current_text,
-                            context->current_text_len));
+                 current_text_dup);
+      g_free (current_text_dup);
       goto finished;
     }
 
@@ -1068,10 +1162,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                 }
               else
                 {
-                  set_error (context,
-                             error,
-                             G_MARKUP_ERROR_PARSE,
-                             _("Document must begin with an element (e.g. <book>)"));
+                  set_error_literal (context,
+                                     error,
+                                     G_MARKUP_ERROR_PARSE,
+                                     _("Document must begin with an element (e.g. <book>)"));
                 }
             }
           break;
@@ -1148,12 +1242,16 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
           
             g_assert (context->tag_stack != NULL);
 
+            possibly_finish_subparser (context);
+
             tmp_error = NULL;
             if (context->parser->end_element)
               (* context->parser->end_element) (context,
                                                 context->tag_stack->data,
                                                 context->user_data,
                                                 &tmp_error);
+
+            ensure_no_outstanding_subparser (context);
           
             if (tmp_error)
               {
@@ -1176,7 +1274,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                error,
                                G_MARKUP_ERROR_PARSE,
                                _("Odd character '%s', expected a '>' character "
-                                 "to end the start tag of element '%s'"),
+                                 "to end the empty-element tag '%s'"),
                                utf8_str (context->iter, buf),
                                current_element (context));
                   }
@@ -1606,6 +1704,8 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                  context->state = STATE_AFTER_CLOSE_ANGLE;
                  context->start = NULL;
                  
+                  possibly_finish_subparser (context);
+
                  /* call the end_element callback */
                  tmp_error = NULL;
                  if (context->parser->end_element)
@@ -1614,6 +1714,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                                      context->user_data,
                                                      &tmp_error);
                  
+                  ensure_no_outstanding_subparser (context);
                  
                  /* Pop the tag stack */
                  g_free (context->tag_stack->data);
@@ -1753,8 +1854,8 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
 
   if (context->document_empty)
     {
-      set_error (context, error, G_MARKUP_ERROR_EMPTY,
-                 _("Document was empty or contained only whitespace"));
+      set_error_literal (context, error, G_MARKUP_ERROR_EMPTY,
+                         _("Document was empty or contained only whitespace"));
       return FALSE;
     }
   
@@ -1767,8 +1868,8 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_AFTER_OPEN_ANGLE:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly just after an open angle bracket '<'"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly just after an open angle bracket '<'"));
       break;
 
     case STATE_AFTER_CLOSE_ANGLE:
@@ -1789,33 +1890,33 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_INSIDE_OPEN_TAG_NAME:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an element name"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an element name"));
       break;
 
     case STATE_INSIDE_ATTRIBUTE_NAME:
     case STATE_AFTER_ATTRIBUTE_NAME:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an attribute name"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an attribute name"));
       break;
 
     case STATE_BETWEEN_ATTRIBUTES:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside an element-opening "
-                   "tag."));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside an element-opening "
+                           "tag."));
       break;
 
     case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly after the equals sign "
-                   "following an attribute name; no attribute value"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly after the equals sign "
+                           "following an attribute name; no attribute value"));
       break;
 
     case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
     case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly while inside an attribute "
-                   "value"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly while inside an attribute "
+                           "value"));
       break;
 
     case STATE_INSIDE_TEXT:
@@ -1835,9 +1936,9 @@ g_markup_parse_context_end_parse (GMarkupParseContext *context,
       break;
 
     case STATE_INSIDE_PASSTHROUGH:
-      set_error (context, error, G_MARKUP_ERROR_PARSE,
-                 _("Document ended unexpectedly inside a comment or "
-                   "processing instruction"));
+      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
+                         _("Document ended unexpectedly inside a comment or "
+                           "processing instruction"));
       break;
 
     case STATE_ERROR:
@@ -1892,7 +1993,7 @@ g_markup_parse_context_get_element (GMarkupParseContext *context)
  *
  * Returns: the element stack, which must not be modified
  *
- * Since 2.16
+ * Since: 2.16
  **/
 G_CONST_RETURN GSList *
 g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
@@ -1928,6 +2029,210 @@ g_markup_parse_context_get_position (GMarkupParseContext *context,
     *char_number = context->char_number;
 }
 
+/**
+ * g_markup_parse_context_get_user_data:
+ * @context: a #GMarkupParseContext
+ *
+ * Returns the user_data associated with @context.  This will either
+ * be the user_data that was provided to g_markup_parse_context_new()
+ * or to the most recent call of g_markup_parse_context_push().
+ *
+ * Returns: the provided user_data. The returned data belongs to
+ *     the markup context and will be freed when g_markup_context_free()
+ *     is called.
+ *
+ * Since: 2.18
+ **/
+gpointer
+g_markup_parse_context_get_user_data (GMarkupParseContext *context)
+{
+  return context->user_data;
+}
+
+/**
+ * g_markup_parse_context_push:
+ * @context: a #GMarkupParseContext
+ * @parser: a #GMarkupParser
+ * @user_data: user data to pass to #GMarkupParser functions
+ *
+ * Temporarily redirects markup data to a sub-parser.
+ *
+ * This function may only be called from the start_element handler of
+ * a #GMarkupParser.  It must be matched with a corresponding call to
+ * g_markup_parse_context_pop() in the matching end_element handler
+ * (except in the case that the parser aborts due to an error).
+ *
+ * All tags, text and other data between the matching tags is
+ * redirected to the subparser given by @parser.  @user_data is used
+ * as the user_data for that parser.  @user_data is also passed to the
+ * error callback in the event that an error occurs.  This includes
+ * errors that occur in subparsers of the subparser.
+ *
+ * The end tag matching the start tag for which this call was made is
+ * handled by the previous parser (which is given its own user_data)
+ * which is why g_markup_parse_context_pop() is provided to allow "one
+ * last access" to the @user_data provided to this function.  In the
+ * case of error, the @user_data provided here is passed directly to
+ * the error callback of the subparser and g_markup_parse_context()
+ * should not be called.  In either case, if @user_data was allocated
+ * then it ought to be freed from both of these locations.
+ *
+ * This function is not intended to be directly called by users
+ * interested in invoking subparsers.  Instead, it is intended to be
+ * used by the subparsers themselves to implement a higher-level
+ * interface.
+ *
+ * As an example, see the following implementation of a simple
+ * parser that counts the number of tags encountered.
+ *
+ * |[
+ * typedef struct
+ * {
+ *   gint tag_count;
+ * } CounterData;
+ * 
+ * static void
+ * counter_start_element (GMarkupParseContext  *context,
+ *                        const gchar          *element_name,
+ *                        const gchar         **attribute_names,
+ *                        const gchar         **attribute_values,
+ *                        gpointer              user_data,
+ *                        GError              **error)
+ * {
+ *   CounterData *data = user_data;
+ * 
+ *   data->tag_count++;
+ * }
+ * 
+ * static void
+ * counter_error (GMarkupParseContext *context,
+ *                GError              *error,
+ *                gpointer             user_data)
+ * {
+ *   CounterData *data = user_data;
+ * 
+ *   g_slice_free (CounterData, data);
+ * }
+ * 
+ * static GMarkupParser counter_subparser =
+ * {
+ *   counter_start_element,
+ *   NULL,
+ *   NULL,
+ *   NULL,
+ *   counter_error
+ * };
+ * ]|
+ *
+ * In order to allow this parser to be easily used as a subparser, the
+ * following interface is provided:
+ *
+ * |[
+ * void
+ * start_counting (GMarkupParseContext *context)
+ * {
+ *   CounterData *data = g_slice_new (CounterData);
+ * 
+ *   data->tag_count = 0;
+ *   g_markup_parse_context_push (context, &counter_subparser, data);
+ * }
+ * 
+ * gint
+ * end_counting (GMarkupParseContext *context)
+ * {
+ *   CounterData *data = g_markup_parse_context_pop (context);
+ *   int result;
+ * 
+ *   result = data->tag_count;
+ *   g_slice_free (CounterData, data);
+ * 
+ *   return result;
+ * }
+ * ]|
+ *
+ * The subparser would then be used as follows:
+ *
+ * |[
+ * static void start_element (context, element_name, ...)
+ * {
+ *   if (strcmp (element_name, "count-these") == 0)
+ *     start_counting (context);
+ * 
+ *   /&ast; else, handle other tags... &ast;/
+ * }
+ * 
+ * static void end_element (context, element_name, ...)
+ * {
+ *   if (strcmp (element_name, "count-these") == 0)
+ *     g_print ("Counted %d tags\n", end_counting (context));
+ * 
+ *   /&ast; else, handle other tags... &ast;/
+ * }
+ * ]|
+ *
+ * Since: 2.18
+ **/
+void
+g_markup_parse_context_push (GMarkupParseContext *context,
+                             GMarkupParser       *parser,
+                             gpointer             user_data)
+{
+  GMarkupRecursionTracker *tracker;
+
+  tracker = g_slice_new (GMarkupRecursionTracker);
+  tracker->prev_element = context->subparser_element;
+  tracker->prev_parser = context->parser;
+  tracker->prev_user_data = context->user_data;
+
+  context->subparser_element = current_element (context);
+  context->parser = parser;
+  context->user_data = user_data;
+
+  context->subparser_stack = g_slist_prepend (context->subparser_stack,
+                                              tracker);
+}
+
+/**
+ * g_markup_parse_context_pop:
+ * @context: a #GMarkupParseContext
+ *
+ * Completes the process of a temporary sub-parser redirection.
+ *
+ * This function exists to collect the user_data allocated by a
+ * matching call to g_markup_parse_context_push().  It must be called
+ * in the end_element handler corresponding to the start_element
+ * handler during which g_markup_parse_context_push() was called.  You
+ * must not call this function from the error callback -- the
+ * @user_data is provided directly to the callback in that case.
+ *
+ * This function is not intended to be directly called by users
+ * interested in invoking subparsers.  Instead, it is intended to be
+ * used by the subparsers themselves to implement a higher-level
+ * interface.
+ *
+ * Returns: the user_data passed to g_markup_parse_context_push().
+ *
+ * Since: 2.18
+ **/
+gpointer
+g_markup_parse_context_pop (GMarkupParseContext *context)
+{
+  gpointer user_data;
+
+  if (!context->awaiting_pop)
+    possibly_finish_subparser (context);
+
+  g_assert (context->awaiting_pop);
+
+  context->awaiting_pop = FALSE;
+  
+  /* valgrind friendliness */
+  user_data = context->held_user_data;
+  context->held_user_data = NULL;
+
+  return user_data;
+}
+
 static void
 append_escaped_text (GString     *str,
                      const gchar *text,
@@ -1997,6 +2302,13 @@ append_escaped_text (GString     *str,
  * Note that this function doesn't protect whitespace and line endings
  * from being processed according to the XML rules for normalization
  * of line endings and attribute values.
+ *
+ * Note also that if given a string containing them, this function
+ * will produce character references in the range of &amp;#x1; ..
+ * &amp;#x1f; for all control sequences except for tabstop, newline
+ * and carriage return.  The character references in this range are
+ * not valid XML 1.0, but they are valid XML 1.1 and will be accepted
+ * by the GMarkup parser.
  * 
  * Return value: a newly allocated string with the escaped text
  **/
@@ -2296,7 +2608,7 @@ g_markup_vprintf_escaped (const char *format,
  * output, without having to worry that the strings
  * might themselves contain markup.
  *
- * <informalexample><programlisting>
+ * |[
  * const char *store = "Fortnum &amp; Mason";
  * const char *item = "Tea";
  * char *output;
@@ -2306,7 +2618,7 @@ g_markup_vprintf_escaped (const char *format,
  *                                   "&lt;item&gt;&percnt;s&lt;/item&gt;"
  *                                   "&lt;/purchase&gt;",
  *                                   store, item);
- * </programlisting></informalexample>
+ * ]|
  * 
  * Return value: newly allocated result from formatting
  *  operation. Free with g_free().
@@ -2326,5 +2638,359 @@ g_markup_printf_escaped (const char *format, ...)
   return result;
 }
 
+static gboolean
+g_markup_parse_boolean (const char  *string,
+                        gboolean    *value)
+{
+  char const * const falses[] = { "false", "f", "no", "n", "0" };
+  char const * const trues[] = { "true", "t", "yes", "y", "1" };
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (falses); i++)
+    {
+      if (g_ascii_strcasecmp (string, falses[i]) == 0)
+        {
+          if (value != NULL)
+            *value = FALSE;
+
+          return TRUE;
+        }
+    }
+
+  for (i = 0; i < G_N_ELEMENTS (trues); i++)
+    {
+      if (g_ascii_strcasecmp (string, trues[i]) == 0)
+        {
+          if (value != NULL)
+            *value = TRUE;
+
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+/**
+ * GMarkupCollectType:
+ * @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes
+ *                            to collect.
+ * @G_MARKUP_COLLECT_STRING: collect the string pointer directly from
+ *                           the attribute_values[] array.  Expects a
+ *                           parameter of type (const char **).  If
+ *                           %G_MARKUP_COLLECT_OPTIONAL is specified
+ *                           and the attribute isn't present then the
+ *                           pointer will be set to %NULL.
+ * @G_MARKUP_COLLECT_STRDUP: as with %G_MARKUP_COLLECT_STRING, but
+ *                           expects a paramter of type (char **) and
+ *                           g_strdup()s the returned pointer.  The
+ *                           pointer must be freed with g_free().
+ * @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *)
+ *                            and parses the attribute value as a
+ *                            boolean.  Sets %FALSE if the attribute
+ *                            isn't present.  Valid boolean values
+ *                            consist of (case insensitive) "false",
+ *                            "f", "no", "n", "0" and "true", "t",
+ *                            "yes", "y", "1".
+ * @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but
+ *                             in the case of a missing attribute a
+ *                             value is set that compares equal to
+ *                             neither %FALSE nor %TRUE.
+ *                             G_MARKUP_COLLECT_OPTIONAL is implied.
+ * @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other
+ *                             fields.  If present, allows the
+ *                             attribute not to appear.  A default
+ *                             value is set depending on what value
+ *                             type is used.
+ *
+ * A mixed enumerated type and flags field.  You must specify one type
+ * (string, strdup, boolean, tristate).  Additionally, you may
+ * optionally bitwise OR the type with the flag
+ * %G_MARKUP_COLLECT_OPTIONAL.
+ *
+ * It is likely that this enum will be extended in the future to
+ * support other types.
+ **/
+
+/**
+ * g_markup_collect_attributes:
+ * @element_name: the current tag name
+ * @attribute_names: the attribute names
+ * @attribute_values: the attribute values
+ * @error: a pointer to a #GError or %NULL
+ * @first_type: the #GMarkupCollectType of the
+ *              first attribute
+ * @first_attr: the name of the first attribute
+ * @...: a pointer to the storage location of the
+ *       first attribute (or %NULL), followed by
+ *       more types names and pointers, ending
+ *       with %G_MARKUP_COLLECT_INVALID.
+ * 
+ * Collects the attributes of the element from the
+ * data passed to the #GMarkupParser start_element
+ * function, dealing with common error conditions
+ * and supporting boolean values.
+ *
+ * This utility function is not required to write
+ * a parser but can save a lot of typing.
+ *
+ * The @element_name, @attribute_names,
+ * @attribute_values and @error parameters passed
+ * to the start_element callback should be passed
+ * unmodified to this function.
+ *
+ * Following these arguments is a list of
+ * "supported" attributes to collect.  It is an
+ * error to specify multiple attributes with the
+ * same name.  If any attribute not in the list
+ * appears in the @attribute_names array then an
+ * unknown attribute error will result.
+ *
+ * The #GMarkupCollectType field allows specifying
+ * the type of collection to perform and if a
+ * given attribute must appear or is optional.
+ *
+ * The attribute name is simply the name of the
+ * attribute to collect.
+ *
+ * The pointer should be of the appropriate type
+ * (see the descriptions under
+ * #GMarkupCollectType) and may be %NULL in case a
+ * particular attribute is to be allowed but
+ * ignored.
+ *
+ * This function deals with issuing errors for missing attributes 
+ * (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes 
+ * (of type %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE) and duplicate 
+ * attributes (of type %G_MARKUP_ERROR_INVALID_CONTENT) as well 
+ * as parse errors for boolean-valued attributes (again of type
+ * %G_MARKUP_ERROR_INVALID_CONTENT). In all of these cases %FALSE 
+ * will be returned and @error will be set as appropriate.
+ *
+ * Return value: %TRUE if successful
+ *
+ * Since: 2.16
+ **/
+gboolean
+g_markup_collect_attributes (const gchar         *element_name,
+                             const gchar        **attribute_names,
+                             const gchar        **attribute_values,
+                             GError             **error,
+                             GMarkupCollectType   first_type,
+                             const gchar         *first_attr,
+                             ...)
+{
+  GMarkupCollectType type;
+  const gchar *attr;
+  guint64 collected;
+  int written;
+  va_list ap;
+  int i;
+
+  type = first_type;
+  attr = first_attr;
+  collected = 0;
+  written = 0;
+
+  va_start (ap, first_attr);
+  while (type != G_MARKUP_COLLECT_INVALID)
+    {
+      gboolean mandatory;
+      const gchar *value;
+
+      mandatory = !(type & G_MARKUP_COLLECT_OPTIONAL);
+      type &= (G_MARKUP_COLLECT_OPTIONAL - 1);
+
+      /* tristate records a value != TRUE and != FALSE
+       * for the case where the attribute is missing
+       */
+      if (type == G_MARKUP_COLLECT_TRISTATE)
+        mandatory = FALSE;
+
+      for (i = 0; attribute_names[i]; i++)
+        if (i >= 40 || !(collected & (G_GUINT64_CONSTANT(1) << i)))
+          if (!strcmp (attribute_names[i], attr))
+            break;
+
+      /* ISO C99 only promises that the user can pass up to 127 arguments.
+       * Subtracting the first 4 arguments plus the final NULL and dividing
+       * by 3 arguments per collected attribute, we are left with a maximum
+       * number of supported attributes of (127 - 5) / 3 = 40.
+       *
+       * In reality, nobody is ever going to call us with anywhere close to
+       * 40 attributes to collect, so it is safe to assume that if i > 40
+       * then the user has given some invalid or repeated arguments.  These
+       * problems will be caught and reported at the end of the function.
+       *
+       * We know at this point that we have an error, but we don't know
+       * what error it is, so just continue...
+       */
+      if (i < 40)
+        collected |= (G_GUINT64_CONSTANT(1) << i);
+
+      value = attribute_values[i];
+
+      if (value == NULL && mandatory)
+        {
+          g_set_error (error, G_MARKUP_ERROR,
+                       G_MARKUP_ERROR_MISSING_ATTRIBUTE,
+                       "element '%s' requires attribute '%s'",
+                       element_name, attr);
+
+          va_end (ap);
+          goto failure;
+        }
+
+      switch (type)
+        {
+        case G_MARKUP_COLLECT_STRING:
+          {
+            const char **str_ptr;
+
+            str_ptr = va_arg (ap, const char **);
+
+            if (str_ptr != NULL)
+              *str_ptr = value;
+          }
+          break;
+
+        case G_MARKUP_COLLECT_STRDUP:
+          {
+            char **str_ptr;
+
+            str_ptr = va_arg (ap, char **);
+
+            if (str_ptr != NULL)
+              *str_ptr = g_strdup (value);
+          }
+          break;
+
+        case G_MARKUP_COLLECT_BOOLEAN:
+        case G_MARKUP_COLLECT_TRISTATE:
+          if (value == NULL)
+            {
+              gboolean *bool_ptr;
+
+              bool_ptr = va_arg (ap, gboolean *);
+
+              if (bool_ptr != NULL)
+                {
+                  if (type == G_MARKUP_COLLECT_TRISTATE)
+                    /* constructivists rejoice!
+                     * neither false nor true...
+                     */
+                    *bool_ptr = -1;
+
+                  else /* G_MARKUP_COLLECT_BOOLEAN */
+                    *bool_ptr = FALSE;
+                }
+            }
+          else
+            {
+              if (!g_markup_parse_boolean (value, va_arg (ap, gboolean *)))
+                {
+                  g_set_error (error, G_MARKUP_ERROR,
+                               G_MARKUP_ERROR_INVALID_CONTENT,
+                               "element '%s', attribute '%s', value '%s' "
+                               "cannot be parsed as a boolean value",
+                               element_name, attr, value);
+
+                  va_end (ap);
+                  goto failure;
+                }
+            }
+
+          break;
+
+        default:
+          g_assert_not_reached ();
+        }
+
+      type = va_arg (ap, GMarkupCollectType);
+      attr = va_arg (ap, const char *);
+      written++;
+    }
+  va_end (ap);
+
+  /* ensure we collected all the arguments */
+  for (i = 0; attribute_names[i]; i++)
+    if ((collected & (G_GUINT64_CONSTANT(1) << i)) == 0)
+      {
+        /* attribute not collected:  could be caused by two things.
+         *
+         * 1) it doesn't exist in our list of attributes
+         * 2) it existed but was matched by a duplicate attribute earlier
+         *
+         * find out.
+         */
+        int j;
+
+        for (j = 0; j < i; j++)
+          if (strcmp (attribute_names[i], attribute_names[j]) == 0)
+            /* duplicate! */
+            break;
+
+        /* j is now the first occurance of attribute_names[i] */
+        if (i == j)
+          g_set_error (error, G_MARKUP_ERROR,
+                       G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
+                       "attribute '%s' invalid for element '%s'",
+                       attribute_names[i], element_name);
+        else
+          g_set_error (error, G_MARKUP_ERROR,
+                       G_MARKUP_ERROR_INVALID_CONTENT,
+                       "attribute '%s' given multiple times for element '%s'",
+                       attribute_names[i], element_name);
+
+        goto failure;
+      }
+
+  return TRUE;
+
+failure:
+  /* replay the above to free allocations */
+  type = first_type;
+  attr = first_attr;
+
+  va_start (ap, first_attr);
+  while (type != G_MARKUP_COLLECT_INVALID)
+    {
+      gpointer ptr;
+
+      ptr = va_arg (ap, gpointer);
+
+      if (ptr == NULL)
+        continue;
+
+      switch (type & (G_MARKUP_COLLECT_OPTIONAL - 1))
+        {
+        case G_MARKUP_COLLECT_STRDUP:
+          if (written)
+            g_free (*(char **) ptr);
+
+        case G_MARKUP_COLLECT_STRING:
+          *(char **) ptr = NULL;
+          break;
+
+        case G_MARKUP_COLLECT_BOOLEAN:
+          *(gboolean *) ptr = FALSE;
+          break;
+
+        case G_MARKUP_COLLECT_TRISTATE:
+          *(gboolean *) ptr = -1;
+          break;
+        }
+
+      type = va_arg (ap, GMarkupCollectType);
+      attr = va_arg (ap, const char *);
+
+      if (written)
+        written--;
+    }
+  va_end (ap);
+
+  return FALSE;
+}
+
 #define __G_MARKUP_C__
 #include "galiasdef.c"