Blame glib/gmarkup.c

Packit ae235b
/* gmarkup.c - Simple XML-like parser
Packit ae235b
 *
Packit ae235b
 *  Copyright 2000, 2003 Red Hat, Inc.
Packit ae235b
 *  Copyright 2007, 2008 Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdarg.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include "gmarkup.h"
Packit ae235b
Packit ae235b
#include "gatomic.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "galloca.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gstring.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:markup
Packit ae235b
 * @Title: Simple XML Subset Parser
Packit ae235b
 * @Short_description: parses a subset of XML
Packit ae235b
 * @See_also: [XML Specification](http://www.w3.org/TR/REC-xml/)
Packit ae235b
 *
Packit ae235b
 * The "GMarkup" parser is intended to parse a simple markup format
Packit ae235b
 * that's a subset of XML. This is a small, efficient, easy-to-use
Packit ae235b
 * parser. It should not be used if you expect to interoperate with
Packit ae235b
 * other applications generating full-scale XML. However, it's very
Packit ae235b
 * useful for application data files, config files, etc. where you
Packit ae235b
 * know your application will be the only one writing the file.
Packit ae235b
 * Full-scale XML parsers should be able to parse the subset used by
Packit ae235b
 * GMarkup, so you can easily migrate to full-scale XML at a later
Packit ae235b
 * time if the need arises.
Packit ae235b
 *
Packit ae235b
 * GMarkup is not guaranteed to signal an error on all invalid XML;
Packit ae235b
 * the parser may accept documents that an XML parser would not.
Packit ae235b
 * However, XML documents which are not well-formed (which is a
Packit ae235b
 * weaker condition than being valid. See the
Packit ae235b
 * [XML specification](http://www.w3.org/TR/REC-xml/)
Packit ae235b
 * for definitions of these terms.) are not considered valid GMarkup
Packit ae235b
 * documents.
Packit ae235b
 *
Packit ae235b
 * Simplifications to XML include:
Packit ae235b
 *
Packit ae235b
 * - Only UTF-8 encoding is allowed
Packit ae235b
 *
Packit ae235b
 * - No user-defined entities
Packit ae235b
 *
Packit ae235b
 * - Processing instructions, comments and the doctype declaration
Packit ae235b
 *   are "passed through" but are not interpreted in any way
Packit ae235b
 *
Packit ae235b
 * - No DTD or validation
Packit ae235b
 *
Packit ae235b
 * The markup format does support:
Packit ae235b
 *
Packit ae235b
 * - Elements
Packit ae235b
 *
Packit ae235b
 * - Attributes
Packit ae235b
 *
Packit ae235b
 * - 5 standard entities: & < > " '
Packit ae235b
 *
Packit ae235b
 * - Character references
Packit ae235b
 *
Packit ae235b
 * - Sections marked as CDATA
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-markup-error-quark, g_markup_error)
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  STATE_START,
Packit ae235b
  STATE_AFTER_OPEN_ANGLE,
Packit ae235b
  STATE_AFTER_CLOSE_ANGLE,
Packit ae235b
  STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
Packit ae235b
  STATE_INSIDE_OPEN_TAG_NAME,
Packit ae235b
  STATE_INSIDE_ATTRIBUTE_NAME,
Packit ae235b
  STATE_AFTER_ATTRIBUTE_NAME,
Packit ae235b
  STATE_BETWEEN_ATTRIBUTES,
Packit ae235b
  STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
Packit ae235b
  STATE_INSIDE_ATTRIBUTE_VALUE_SQ,
Packit ae235b
  STATE_INSIDE_ATTRIBUTE_VALUE_DQ,
Packit ae235b
  STATE_INSIDE_TEXT,
Packit ae235b
  STATE_AFTER_CLOSE_TAG_SLASH,
Packit ae235b
  STATE_INSIDE_CLOSE_TAG_NAME,
Packit ae235b
  STATE_AFTER_CLOSE_TAG_NAME,
Packit ae235b
  STATE_INSIDE_PASSTHROUGH,
Packit ae235b
  STATE_ERROR
Packit ae235b
} GMarkupParseState;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  const char *prev_element;
Packit ae235b
  const GMarkupParser *prev_parser;
Packit ae235b
  gpointer prev_user_data;
Packit ae235b
} GMarkupRecursionTracker;
Packit ae235b
Packit ae235b
struct _GMarkupParseContext
Packit ae235b
{
Packit ae235b
  const GMarkupParser *parser;
Packit ae235b
Packit ae235b
  volatile gint ref_count;
Packit ae235b
Packit ae235b
  GMarkupParseFlags flags;
Packit ae235b
Packit ae235b
  gint line_number;
Packit ae235b
  gint char_number;
Packit ae235b
Packit ae235b
  GMarkupParseState state;
Packit ae235b
Packit ae235b
  gpointer user_data;
Packit ae235b
  GDestroyNotify dnotify;
Packit ae235b
Packit ae235b
  /* A piece of character data or an element that
Packit ae235b
   * hasn't "ended" yet so we haven't yet called
Packit ae235b
   * the callback for it.
Packit ae235b
   */
Packit ae235b
  GString *partial_chunk;
Packit ae235b
  GSList *spare_chunks;
Packit ae235b
Packit ae235b
  GSList *tag_stack;
Packit ae235b
  GSList *tag_stack_gstr;
Packit ae235b
  GSList *spare_list_nodes;
Packit ae235b
Packit ae235b
  GString **attr_names;
Packit ae235b
  GString **attr_values;
Packit ae235b
  gint cur_attr;
Packit ae235b
  gint alloc_attrs;
Packit ae235b
Packit ae235b
  const gchar *current_text;
Packit ae235b
  gssize       current_text_len;
Packit ae235b
  const gchar *current_text_end;
Packit ae235b
Packit ae235b
  /* used to save the start of the last interesting thingy */
Packit ae235b
  const gchar *start;
Packit ae235b
Packit ae235b
  const gchar *iter;
Packit ae235b
Packit ae235b
  guint document_empty : 1;
Packit ae235b
  guint parsing : 1;
Packit ae235b
  guint awaiting_pop : 1;
Packit ae235b
  gint balance;
Packit ae235b
Packit ae235b
  /* subparser support */
Packit ae235b
  GSList *subparser_stack; /* (GMarkupRecursionTracker *) */
Packit ae235b
  const char *subparser_element;
Packit ae235b
  gpointer held_user_data;
Packit ae235b
};
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Helpers to reduce our allocation overhead, we have
Packit ae235b
 * a well defined allocation lifecycle.
Packit ae235b
 */
Packit ae235b
static GSList *
Packit ae235b
get_list_node (GMarkupParseContext *context, gpointer data)
Packit ae235b
{
Packit ae235b
  GSList *node;
Packit ae235b
  if (context->spare_list_nodes != NULL)
Packit ae235b
    {
Packit ae235b
      node = context->spare_list_nodes;
Packit ae235b
      context->spare_list_nodes = g_slist_remove_link (context->spare_list_nodes, node);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    node = g_slist_alloc();
Packit ae235b
  node->data = data;
Packit ae235b
  return node;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_list_node (GMarkupParseContext *context, GSList *node)
Packit ae235b
{
Packit ae235b
  node->data = NULL;
Packit ae235b
  context->spare_list_nodes = g_slist_concat (node, context->spare_list_nodes);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
string_blank (GString *string)
Packit ae235b
{
Packit ae235b
  string->str[0] = '\0';
Packit ae235b
  string->len = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_new:
Packit ae235b
 * @parser: a #GMarkupParser
Packit ae235b
 * @flags: one or more #GMarkupParseFlags
Packit ae235b
 * @user_data: user data to pass to #GMarkupParser functions
Packit ae235b
 * @user_data_dnotify: user data destroy notifier called when
Packit ae235b
 *     the parse context is freed
Packit ae235b
 *
Packit ae235b
 * Creates a new parse context. A parse context is used to parse
Packit ae235b
 * marked-up documents. You can feed any number of documents into
Packit ae235b
 * a context, as long as no errors occur; once an error occurs,
Packit ae235b
 * the parse context can't continue to parse text (you have to
Packit ae235b
 * free it and create a new parse context).
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GMarkupParseContext
Packit ae235b
 **/
Packit ae235b
GMarkupParseContext *
Packit ae235b
g_markup_parse_context_new (const GMarkupParser *parser,
Packit ae235b
                            GMarkupParseFlags    flags,
Packit ae235b
                            gpointer             user_data,
Packit ae235b
                            GDestroyNotify       user_data_dnotify)
Packit ae235b
{
Packit ae235b
  GMarkupParseContext *context;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (parser != NULL, NULL);
Packit ae235b
Packit ae235b
  context = g_new (GMarkupParseContext, 1);
Packit ae235b
Packit ae235b
  context->ref_count = 1;
Packit ae235b
  context->parser = parser;
Packit ae235b
  context->flags = flags;
Packit ae235b
  context->user_data = user_data;
Packit ae235b
  context->dnotify = user_data_dnotify;
Packit ae235b
Packit ae235b
  context->line_number = 1;
Packit ae235b
  context->char_number = 1;
Packit ae235b
Packit ae235b
  context->partial_chunk = NULL;
Packit ae235b
  context->spare_chunks = NULL;
Packit ae235b
  context->spare_list_nodes = NULL;
Packit ae235b
Packit ae235b
  context->state = STATE_START;
Packit ae235b
  context->tag_stack = NULL;
Packit ae235b
  context->tag_stack_gstr = NULL;
Packit ae235b
  context->attr_names = NULL;
Packit ae235b
  context->attr_values = NULL;
Packit ae235b
  context->cur_attr = -1;
Packit ae235b
  context->alloc_attrs = 0;
Packit ae235b
Packit ae235b
  context->current_text = NULL;
Packit ae235b
  context->current_text_len = -1;
Packit ae235b
  context->current_text_end = NULL;
Packit ae235b
Packit ae235b
  context->start = NULL;
Packit ae235b
  context->iter = NULL;
Packit ae235b
Packit ae235b
  context->document_empty = TRUE;
Packit ae235b
  context->parsing = FALSE;
Packit ae235b
Packit ae235b
  context->awaiting_pop = FALSE;
Packit ae235b
  context->subparser_stack = NULL;
Packit ae235b
  context->subparser_element = NULL;
Packit ae235b
Packit ae235b
  /* this is only looked at if awaiting_pop = TRUE.  initialise anyway. */
Packit ae235b
  context->held_user_data = NULL;
Packit ae235b
Packit ae235b
  context->balance = 0;
Packit ae235b
Packit ae235b
  return context;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_ref:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Increases the reference count of @context.
Packit ae235b
 *
Packit ae235b
 * Returns: the same @context
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
GMarkupParseContext *
Packit ae235b
g_markup_parse_context_ref (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (context != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (context->ref_count > 0, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&context->ref_count);
Packit ae235b
Packit ae235b
  return context;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_unref:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Decreases the reference count of @context.  When its reference count
Packit ae235b
 * drops to 0, it is freed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_markup_parse_context_unref (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (context != NULL);
Packit ae235b
  g_return_if_fail (context->ref_count > 0);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&context->ref_count))
Packit ae235b
    g_markup_parse_context_free (context);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
string_full_free (gpointer ptr)
Packit ae235b
{
Packit ae235b
  g_string_free (ptr, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void clear_attributes (GMarkupParseContext *context);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_free:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Frees a #GMarkupParseContext.
Packit ae235b
 *
Packit ae235b
 * This function can't be called from inside one of the
Packit ae235b
 * #GMarkupParser functions or while a subparser is pushed.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_markup_parse_context_free (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (context != NULL);
Packit ae235b
  g_return_if_fail (!context->parsing);
Packit ae235b
  g_return_if_fail (!context->subparser_stack);
Packit ae235b
  g_return_if_fail (!context->awaiting_pop);
Packit ae235b
Packit ae235b
  if (context->dnotify)
Packit ae235b
    (* context->dnotify) (context->user_data);
Packit ae235b
Packit ae235b
  clear_attributes (context);
Packit ae235b
  g_free (context->attr_names);
Packit ae235b
  g_free (context->attr_values);
Packit ae235b
Packit ae235b
  g_slist_free_full (context->tag_stack_gstr, string_full_free);
Packit ae235b
  g_slist_free (context->tag_stack);
Packit ae235b
Packit ae235b
  g_slist_free_full (context->spare_chunks, string_full_free);
Packit ae235b
  g_slist_free (context->spare_list_nodes);
Packit ae235b
Packit ae235b
  if (context->partial_chunk)
Packit ae235b
    g_string_free (context->partial_chunk, TRUE);
Packit ae235b
Packit ae235b
  g_free (context);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void pop_subparser_stack (GMarkupParseContext *context);
Packit ae235b
Packit ae235b
static void
Packit ae235b
mark_error (GMarkupParseContext *context,
Packit ae235b
            GError              *error)
Packit ae235b
{
Packit ae235b
  context->state = STATE_ERROR;
Packit ae235b
Packit ae235b
  if (context->parser->error)
Packit ae235b
    (*context->parser->error) (context, error, context->user_data);
Packit ae235b
Packit ae235b
  /* report the error all the way up to free all the user-data */
Packit ae235b
  while (context->subparser_stack)
Packit ae235b
    {
Packit ae235b
      pop_subparser_stack (context);
Packit ae235b
      context->awaiting_pop = FALSE; /* already been freed */
Packit ae235b
Packit ae235b
      if (context->parser->error)
Packit ae235b
        (*context->parser->error) (context, error, context->user_data);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
set_error (GMarkupParseContext  *context,
Packit ae235b
           GError              **error,
Packit ae235b
           GMarkupError          code,
Packit ae235b
           const gchar          *format,
Packit ae235b
           ...) G_GNUC_PRINTF (4, 5);
Packit ae235b
Packit ae235b
static void
Packit ae235b
set_error_literal (GMarkupParseContext  *context,
Packit ae235b
                   GError              **error,
Packit ae235b
                   GMarkupError          code,
Packit ae235b
                   const gchar          *message)
Packit ae235b
{
Packit ae235b
  GError *tmp_error;
Packit ae235b
Packit ae235b
  tmp_error = g_error_new_literal (G_MARKUP_ERROR, code, message);
Packit ae235b
Packit ae235b
  g_prefix_error (&tmp_error,
Packit ae235b
                  _("Error on line %d char %d: "),
Packit ae235b
                  context->line_number,
Packit ae235b
                  context->char_number);
Packit ae235b
Packit ae235b
  mark_error (context, tmp_error);
Packit ae235b
Packit ae235b
  g_propagate_error (error, tmp_error);
Packit ae235b
}
Packit ae235b
Packit ae235b
G_GNUC_PRINTF(4, 5)
Packit ae235b
static void
Packit ae235b
set_error (GMarkupParseContext  *context,
Packit ae235b
           GError              **error,
Packit ae235b
           GMarkupError          code,
Packit ae235b
           const gchar          *format,
Packit ae235b
           ...)
Packit ae235b
{
Packit ae235b
  gchar *s;
Packit ae235b
  gchar *s_valid;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  s = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  /* Make sure that the GError message is valid UTF-8
Packit ae235b
   * even if it is complaining about invalid UTF-8 in the markup
Packit ae235b
   */
Packit ae235b
  s_valid = g_utf8_make_valid (s, -1);
Packit ae235b
  set_error_literal (context, error, code, s);
Packit ae235b
Packit ae235b
  g_free (s);
Packit ae235b
  g_free (s_valid);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
propagate_error (GMarkupParseContext  *context,
Packit ae235b
                 GError              **dest,
Packit ae235b
                 GError               *src)
Packit ae235b
{
Packit ae235b
  if (context->flags & G_MARKUP_PREFIX_ERROR_POSITION)
Packit ae235b
    g_prefix_error (&src,
Packit ae235b
                    _("Error on line %d char %d: "),
Packit ae235b
                    context->line_number,
Packit ae235b
                    context->char_number);
Packit ae235b
Packit ae235b
  mark_error (context, src);
Packit ae235b
Packit ae235b
  g_propagate_error (dest, src);
Packit ae235b
}
Packit ae235b
Packit ae235b
#define IS_COMMON_NAME_END_CHAR(c) \
Packit ae235b
  ((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ')
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
slow_name_validate (GMarkupParseContext  *context,
Packit ae235b
                    const gchar          *name,
Packit ae235b
                    GError              **error)
Packit ae235b
{
Packit ae235b
  const gchar *p = name;
Packit ae235b
Packit ae235b
  if (!g_utf8_validate (name, strlen (name), NULL))
Packit ae235b
    {
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
Packit ae235b
                 _("Invalid UTF-8 encoded text in name - not valid '%s'"), name);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!(g_ascii_isalpha (*p) ||
Packit ae235b
        (!IS_COMMON_NAME_END_CHAR (*p) &&
Packit ae235b
         (*p == '_' ||
Packit ae235b
          *p == ':' ||
Packit ae235b
          g_unichar_isalpha (g_utf8_get_char (p))))))
Packit ae235b
    {
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                 _("'%s' is not a valid name"), name);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (p = g_utf8_next_char (name); *p != '\0'; p = g_utf8_next_char (p))
Packit ae235b
    {
Packit ae235b
      /* is_name_char */
Packit ae235b
      if (!(g_ascii_isalnum (*p) ||
Packit ae235b
            (!IS_COMMON_NAME_END_CHAR (*p) &&
Packit ae235b
             (*p == '.' ||
Packit ae235b
              *p == '-' ||
Packit ae235b
              *p == '_' ||
Packit ae235b
              *p == ':' ||
Packit ae235b
              g_unichar_isalpha (g_utf8_get_char (p))))))
Packit ae235b
        {
Packit ae235b
          set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                     _("'%s' is not a valid name: '%c'"), name, *p);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Use me for elements, attributes etc.
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
name_validate (GMarkupParseContext  *context,
Packit ae235b
               const gchar          *name,
Packit ae235b
               GError              **error)
Packit ae235b
{
Packit ae235b
  char mask;
Packit ae235b
  const char *p;
Packit ae235b
Packit ae235b
  /* name start char */
Packit ae235b
  p = name;
Packit ae235b
  if (G_UNLIKELY (IS_COMMON_NAME_END_CHAR (*p) ||
Packit ae235b
                  !(g_ascii_isalpha (*p) || *p == '_' || *p == ':')))
Packit ae235b
    goto slow_validate;
Packit ae235b
Packit ae235b
  for (mask = *p++; *p != '\0'; p++)
Packit ae235b
    {
Packit ae235b
      mask |= *p;
Packit ae235b
Packit ae235b
      /* is_name_char */
Packit ae235b
      if (G_UNLIKELY (!(g_ascii_isalnum (*p) ||
Packit ae235b
                        (!IS_COMMON_NAME_END_CHAR (*p) &&
Packit ae235b
                         (*p == '.' ||
Packit ae235b
                          *p == '-' ||
Packit ae235b
                          *p == '_' ||
Packit ae235b
                          *p == ':')))))
Packit ae235b
        goto slow_validate;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (mask & 0x80) /* un-common / non-ascii */
Packit ae235b
    goto slow_validate;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
Packit ae235b
 slow_validate:
Packit ae235b
  return slow_name_validate (context, name, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
text_validate (GMarkupParseContext  *context,
Packit ae235b
               const gchar          *p,
Packit ae235b
               gint                  len,
Packit ae235b
               GError              **error)
Packit ae235b
{
Packit ae235b
  if (!g_utf8_validate (p, len, NULL))
Packit ae235b
    {
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
Packit ae235b
                 _("Invalid UTF-8 encoded text in name - not valid '%s'"), p);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
char_str (gunichar c,
Packit ae235b
          gchar   *buf)
Packit ae235b
{
Packit ae235b
  memset (buf, 0, 8);
Packit ae235b
  g_unichar_to_utf8 (c, buf);
Packit ae235b
  return buf;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
utf8_str (const gchar *utf8,
Packit ae235b
          gchar       *buf)
Packit ae235b
{
Packit ae235b
  char_str (g_utf8_get_char (utf8), buf);
Packit ae235b
  return buf;
Packit ae235b
}
Packit ae235b
Packit ae235b
G_GNUC_PRINTF(5, 6)
Packit ae235b
static void
Packit ae235b
set_unescape_error (GMarkupParseContext  *context,
Packit ae235b
                    GError              **error,
Packit ae235b
                    const gchar          *remaining_text,
Packit ae235b
                    GMarkupError          code,
Packit ae235b
                    const gchar          *format,
Packit ae235b
                    ...)
Packit ae235b
{
Packit ae235b
  GError *tmp_error;
Packit ae235b
  gchar *s;
Packit ae235b
  va_list args;
Packit ae235b
  gint remaining_newlines;
Packit ae235b
  const gchar *p;
Packit ae235b
Packit ae235b
  remaining_newlines = 0;
Packit ae235b
  p = remaining_text;
Packit ae235b
  while (*p != '\0')
Packit ae235b
    {
Packit ae235b
      if (*p == '\n')
Packit ae235b
        ++remaining_newlines;
Packit ae235b
      ++p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  s = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  tmp_error = g_error_new (G_MARKUP_ERROR,
Packit ae235b
                           code,
Packit ae235b
                           _("Error on line %d: %s"),
Packit ae235b
                           context->line_number - remaining_newlines,
Packit ae235b
                           s);
Packit ae235b
Packit ae235b
  g_free (s);
Packit ae235b
Packit ae235b
  mark_error (context, tmp_error);
Packit ae235b
Packit ae235b
  g_propagate_error (error, tmp_error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * re-write the GString in-place, unescaping anything that escaped.
Packit ae235b
 * most XML does not contain entities, or escaping.
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
unescape_gstring_inplace (GMarkupParseContext  *context,
Packit ae235b
                          GString              *string,
Packit ae235b
                          gboolean             *is_ascii,
Packit ae235b
                          GError              **error)
Packit ae235b
{
Packit ae235b
  char mask, *to;
Packit ae235b
  const char *from;
Packit ae235b
  gboolean normalize_attribute;
Packit ae235b
Packit ae235b
  *is_ascii = FALSE;
Packit ae235b
Packit ae235b
  /* are we unescaping an attribute or not ? */
Packit ae235b
  if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ ||
Packit ae235b
      context->state == STATE_INSIDE_ATTRIBUTE_VALUE_DQ)
Packit ae235b
    normalize_attribute = TRUE;
Packit ae235b
  else
Packit ae235b
    normalize_attribute = FALSE;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Meeks' theorem: unescaping can only shrink text.
Packit ae235b
   * for < etc. this is obvious, for ￿ more
Packit ae235b
   * thought is required, but this is patently so.
Packit ae235b
   */
Packit ae235b
  mask = 0;
Packit ae235b
  for (from = to = string->str; *from != '\0'; from++, to++)
Packit ae235b
    {
Packit ae235b
      *to = *from;
Packit ae235b
Packit ae235b
      mask |= *to;
Packit ae235b
      if (normalize_attribute && (*to == '\t' || *to == '\n'))
Packit ae235b
        *to = ' ';
Packit ae235b
      if (*to == '\r')
Packit ae235b
        {
Packit ae235b
          *to = normalize_attribute ? ' ' : '\n';
Packit ae235b
          if (from[1] == '\n')
Packit ae235b
            from++;
Packit ae235b
        }
Packit ae235b
      if (*from == '&')
Packit ae235b
        {
Packit ae235b
          from++;
Packit ae235b
          if (*from == '#')
Packit ae235b
            {
Packit ae235b
              gint base = 10;
Packit ae235b
              gulong l;
Packit ae235b
              gchar *end = NULL;
Packit ae235b
Packit ae235b
              from++;
Packit ae235b
Packit ae235b
              if (*from == 'x')
Packit ae235b
                {
Packit ae235b
                  base = 16;
Packit ae235b
                  from++;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              errno = 0;
Packit ae235b
              l = strtoul (from, &end, base);
Packit ae235b
Packit ae235b
              if (end == from || errno != 0)
Packit ae235b
                {
Packit ae235b
                  set_unescape_error (context, error,
Packit ae235b
                                      from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                      _("Failed to parse '%-.*s', which "
Packit ae235b
                                        "should have been a digit "
Packit ae235b
                                        "inside a character reference "
Packit ae235b
                                        "(ê for example) - perhaps "
Packit ae235b
                                        "the digit is too large"),
Packit ae235b
                                      (int)(end - from), from);
Packit ae235b
                  return FALSE;
Packit ae235b
                }
Packit ae235b
              else if (*end != ';')
Packit ae235b
                {
Packit ae235b
                  set_unescape_error (context, error,
Packit ae235b
                                      from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                      _("Character reference did not end with a "
Packit ae235b
                                        "semicolon; "
Packit ae235b
                                        "most likely you used an ampersand "
Packit ae235b
                                        "character without intending to start "
Packit ae235b
                                        "an entity - escape ampersand as &"));
Packit ae235b
                  return FALSE;
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  /* characters XML 1.1 permits */
Packit ae235b
                  if ((0 < l && l <= 0xD7FF) ||
Packit ae235b
                      (0xE000 <= l && l <= 0xFFFD) ||
Packit ae235b
                      (0x10000 <= l && l <= 0x10FFFF))
Packit ae235b
                    {
Packit ae235b
                      gchar buf[8];
Packit ae235b
                      char_str (l, buf);
Packit ae235b
                      strcpy (to, buf);
Packit ae235b
                      to += strlen (buf) - 1;
Packit ae235b
                      from = end;
Packit ae235b
                      if (l >= 0x80) /* not ascii */
Packit ae235b
                        mask |= 0x80;
Packit ae235b
                    }
Packit ae235b
                  else
Packit ae235b
                    {
Packit ae235b
                      set_unescape_error (context, error,
Packit ae235b
                                          from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                          _("Character reference '%-.*s' does not "
Packit ae235b
                                            "encode a permitted character"),
Packit ae235b
                                          (int)(end - from), from);
Packit ae235b
                      return FALSE;
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          else if (strncmp (from, "lt;", 3) == 0)
Packit ae235b
            {
Packit ae235b
              *to = '<';
Packit ae235b
              from += 2;
Packit ae235b
            }
Packit ae235b
          else if (strncmp (from, "gt;", 3) == 0)
Packit ae235b
            {
Packit ae235b
              *to = '>';
Packit ae235b
              from += 2;
Packit ae235b
            }
Packit ae235b
          else if (strncmp (from, "amp;", 4) == 0)
Packit ae235b
            {
Packit ae235b
              *to = '&';
Packit ae235b
              from += 3;
Packit ae235b
            }
Packit ae235b
          else if (strncmp (from, "quot;", 5) == 0)
Packit ae235b
            {
Packit ae235b
              *to = '"';
Packit ae235b
              from += 4;
Packit ae235b
            }
Packit ae235b
          else if (strncmp (from, "apos;", 5) == 0)
Packit ae235b
            {
Packit ae235b
              *to = '\'';
Packit ae235b
              from += 4;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              if (*from == ';')
Packit ae235b
                set_unescape_error (context, error,
Packit ae235b
                                    from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                    _("Empty entity '&;' seen; valid "
Packit ae235b
                                      "entities are: & " < > '"));
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  const char *end = strchr (from, ';');
Packit ae235b
                  if (end)
Packit ae235b
                    set_unescape_error (context, error,
Packit ae235b
                                        from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                        _("Entity name '%-.*s' is not known"),
Packit ae235b
                                        (int)(end - from), from);
Packit ae235b
                  else
Packit ae235b
                    set_unescape_error (context, error,
Packit ae235b
                                        from, G_MARKUP_ERROR_PARSE,
Packit ae235b
                                        _("Entity did not end with a semicolon; "
Packit ae235b
                                          "most likely you used an ampersand "
Packit ae235b
                                          "character without intending to start "
Packit ae235b
                                          "an entity - escape ampersand as &"));
Packit ae235b
                }
Packit ae235b
              return FALSE;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert (to - string->str <= string->len);
Packit ae235b
  if (to - string->str != string->len)
Packit ae235b
    g_string_truncate (string, to - string->str);
Packit ae235b
Packit ae235b
  *is_ascii = !(mask & 0x80);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline gboolean
Packit ae235b
advance_char (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  context->iter++;
Packit ae235b
  context->char_number++;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (context->iter == context->current_text_end))
Packit ae235b
      return FALSE;
Packit ae235b
Packit ae235b
  else if (G_UNLIKELY (*context->iter == '\n'))
Packit ae235b
    {
Packit ae235b
      context->line_number++;
Packit ae235b
      context->char_number = 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline gboolean
Packit ae235b
xml_isspace (char c)
Packit ae235b
{
Packit ae235b
  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
skip_spaces (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      if (!xml_isspace (*context->iter))
Packit ae235b
        return;
Packit ae235b
    }
Packit ae235b
  while (advance_char (context));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
advance_to_name_end (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      if (IS_COMMON_NAME_END_CHAR (*(context->iter)))
Packit ae235b
        return;
Packit ae235b
      if (xml_isspace (*(context->iter)))
Packit ae235b
        return;
Packit ae235b
    }
Packit ae235b
  while (advance_char (context));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
release_chunk (GMarkupParseContext *context, GString *str)
Packit ae235b
{
Packit ae235b
  GSList *node;
Packit ae235b
  if (!str)
Packit ae235b
    return;
Packit ae235b
  if (str->allocated_len > 256)
Packit ae235b
    { /* large strings are unusual and worth freeing */
Packit ae235b
      g_string_free (str, TRUE);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
  string_blank (str);
Packit ae235b
  node = get_list_node (context, str);
Packit ae235b
  context->spare_chunks = g_slist_concat (node, context->spare_chunks);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
add_to_partial (GMarkupParseContext *context,
Packit ae235b
                const gchar         *text_start,
Packit ae235b
                const gchar         *text_end)
Packit ae235b
{
Packit ae235b
  if (context->partial_chunk == NULL)
Packit ae235b
    { /* allocate a new chunk to parse into */
Packit ae235b
Packit ae235b
      if (context->spare_chunks != NULL)
Packit ae235b
        {
Packit ae235b
          GSList *node = context->spare_chunks;
Packit ae235b
          context->spare_chunks = g_slist_remove_link (context->spare_chunks, node);
Packit ae235b
          context->partial_chunk = node->data;
Packit ae235b
          free_list_node (context, node);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        context->partial_chunk = g_string_sized_new (MAX (28, text_end - text_start));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (text_start != text_end)
Packit ae235b
    g_string_insert_len (context->partial_chunk, -1,
Packit ae235b
                         text_start, text_end - text_start);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline void
Packit ae235b
truncate_partial (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  if (context->partial_chunk != NULL)
Packit ae235b
    string_blank (context->partial_chunk);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline const gchar*
Packit ae235b
current_element (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  return context->tag_stack->data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
pop_subparser_stack (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  GMarkupRecursionTracker *tracker;
Packit ae235b
Packit ae235b
  g_assert (context->subparser_stack);
Packit ae235b
Packit ae235b
  tracker = context->subparser_stack->data;
Packit ae235b
Packit ae235b
  context->awaiting_pop = TRUE;
Packit ae235b
  context->held_user_data = context->user_data;
Packit ae235b
Packit ae235b
  context->user_data = tracker->prev_user_data;
Packit ae235b
  context->parser = tracker->prev_parser;
Packit ae235b
  context->subparser_element = tracker->prev_element;
Packit ae235b
  g_slice_free (GMarkupRecursionTracker, tracker);
Packit ae235b
Packit ae235b
  context->subparser_stack = g_slist_delete_link (context->subparser_stack,
Packit ae235b
                                                  context->subparser_stack);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
push_partial_as_tag (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  GString *str = context->partial_chunk;
Packit ae235b
  /* sadly, this is exported by gmarkup_get_element_stack as-is */
Packit ae235b
  context->tag_stack = g_slist_concat (get_list_node (context, str->str), context->tag_stack);
Packit ae235b
  context->tag_stack_gstr = g_slist_concat (get_list_node (context, str), context->tag_stack_gstr);
Packit ae235b
  context->partial_chunk = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
pop_tag (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  GSList *nodea, *nodeb;
Packit ae235b
Packit ae235b
  nodea = context->tag_stack;
Packit ae235b
  nodeb = context->tag_stack_gstr;
Packit ae235b
  release_chunk (context, nodeb->data);
Packit ae235b
  context->tag_stack = g_slist_remove_link (context->tag_stack, nodea);
Packit ae235b
  context->tag_stack_gstr = g_slist_remove_link (context->tag_stack_gstr, nodeb);
Packit ae235b
  free_list_node (context, nodea);
Packit ae235b
  free_list_node (context, nodeb);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
possibly_finish_subparser (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  if (current_element (context) == context->subparser_element)
Packit ae235b
    pop_subparser_stack (context);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
ensure_no_outstanding_subparser (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  if (context->awaiting_pop)
Packit ae235b
    g_critical ("During the first end_element call after invoking a "
Packit ae235b
                "subparser you must pop the subparser stack and handle "
Packit ae235b
                "the freeing of the subparser user_data.  This can be "
Packit ae235b
                "done by calling the end function of the subparser.  "
Packit ae235b
                "Very probably, your program just leaked memory.");
Packit ae235b
Packit ae235b
  /* let valgrind watch the pointer disappear... */
Packit ae235b
  context->held_user_data = NULL;
Packit ae235b
  context->awaiting_pop = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const gchar*
Packit ae235b
current_attribute (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_assert (context->cur_attr >= 0);
Packit ae235b
  return context->attr_names[context->cur_attr]->str;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
add_attribute (GMarkupParseContext *context, GString *str)
Packit ae235b
{
Packit ae235b
  if (context->cur_attr + 2 >= context->alloc_attrs)
Packit ae235b
    {
Packit ae235b
      context->alloc_attrs += 5; /* silly magic number */
Packit ae235b
      context->attr_names = g_realloc (context->attr_names, sizeof(GString*)*context->alloc_attrs);
Packit ae235b
      context->attr_values = g_realloc (context->attr_values, sizeof(GString*)*context->alloc_attrs);
Packit ae235b
    }
Packit ae235b
  context->cur_attr++;
Packit ae235b
  context->attr_names[context->cur_attr] = str;
Packit ae235b
  context->attr_values[context->cur_attr] = NULL;
Packit ae235b
  context->attr_names[context->cur_attr+1] = NULL;
Packit ae235b
  context->attr_values[context->cur_attr+1] = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
clear_attributes (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  /* Go ahead and free the attributes. */
Packit ae235b
  for (; context->cur_attr >= 0; context->cur_attr--)
Packit ae235b
    {
Packit ae235b
      int pos = context->cur_attr;
Packit ae235b
      release_chunk (context, context->attr_names[pos]);
Packit ae235b
      release_chunk (context, context->attr_values[pos]);
Packit ae235b
      context->attr_names[pos] = context->attr_values[pos] = NULL;
Packit ae235b
    }
Packit ae235b
  g_assert (context->cur_attr == -1);
Packit ae235b
  g_assert (context->attr_names == NULL ||
Packit ae235b
            context->attr_names[0] == NULL);
Packit ae235b
  g_assert (context->attr_values == NULL ||
Packit ae235b
            context->attr_values[0] == NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* This has to be a separate function to ensure the alloca's
Packit ae235b
 * are unwound on exit - otherwise we grow & blow the stack
Packit ae235b
 * with large documents
Packit ae235b
 */
Packit ae235b
static inline void
Packit ae235b
emit_start_element (GMarkupParseContext  *context,
Packit ae235b
                    GError              **error)
Packit ae235b
{
Packit ae235b
  int i, j = 0;
Packit ae235b
  const gchar *start_name;
Packit ae235b
  const gchar **attr_names;
Packit ae235b
  const gchar **attr_values;
Packit ae235b
  GError *tmp_error;
Packit ae235b
Packit ae235b
  /* In case we want to ignore qualified tags and we see that we have
Packit ae235b
   * one here, we push a subparser.  This will ignore all tags inside of
Packit ae235b
   * the qualified tag.
Packit ae235b
   *
Packit ae235b
   * We deal with the end of the subparser from emit_end_element.
Packit ae235b
   */
Packit ae235b
  if ((context->flags & G_MARKUP_IGNORE_QUALIFIED) && strchr (current_element (context), ':'))
Packit ae235b
    {
Packit ae235b
      static const GMarkupParser ignore_parser;
Packit ae235b
      g_markup_parse_context_push (context, &ignore_parser, NULL);
Packit ae235b
      clear_attributes (context);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  attr_names = g_newa (const gchar *, context->cur_attr + 2);
Packit ae235b
  attr_values = g_newa (const gchar *, context->cur_attr + 2);
Packit ae235b
  for (i = 0; i < context->cur_attr + 1; i++)
Packit ae235b
    {
Packit ae235b
      /* Possibly omit qualified attribute names from the list */
Packit ae235b
      if ((context->flags & G_MARKUP_IGNORE_QUALIFIED) && strchr (context->attr_names[i]->str, ':'))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      attr_names[j] = context->attr_names[i]->str;
Packit ae235b
      attr_values[j] = context->attr_values[i]->str;
Packit ae235b
      j++;
Packit ae235b
    }
Packit ae235b
  attr_names[j] = NULL;
Packit ae235b
  attr_values[j] = NULL;
Packit ae235b
Packit ae235b
  /* Call user callback for element start */
Packit ae235b
  tmp_error = NULL;
Packit ae235b
  start_name = current_element (context);
Packit ae235b
Packit ae235b
  if (context->parser->start_element &&
Packit ae235b
      name_validate (context, start_name, error))
Packit ae235b
    (* context->parser->start_element) (context,
Packit ae235b
                                        start_name,
Packit ae235b
                                        (const gchar **)attr_names,
Packit ae235b
                                        (const gchar **)attr_values,
Packit ae235b
                                        context->user_data,
Packit ae235b
                                        &tmp_error);
Packit ae235b
  clear_attributes (context);
Packit ae235b
Packit ae235b
  if (tmp_error != NULL)
Packit ae235b
    propagate_error (context, error, tmp_error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
emit_end_element (GMarkupParseContext  *context,
Packit ae235b
                  GError              **error)
Packit ae235b
{
Packit ae235b
  /* We need to pop the tag stack and call the end_element
Packit ae235b
   * function, since this is the close tag
Packit ae235b
   */
Packit ae235b
  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
  g_assert (context->tag_stack != NULL);
Packit ae235b
Packit ae235b
  possibly_finish_subparser (context);
Packit ae235b
Packit ae235b
  /* We might have just returned from our ignore subparser */
Packit ae235b
  if ((context->flags & G_MARKUP_IGNORE_QUALIFIED) && strchr (current_element (context), ':'))
Packit ae235b
    {
Packit ae235b
      g_markup_parse_context_pop (context);
Packit ae235b
      pop_tag (context);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  tmp_error = NULL;
Packit ae235b
  if (context->parser->end_element)
Packit ae235b
    (* context->parser->end_element) (context,
Packit ae235b
                                      current_element (context),
Packit ae235b
                                      context->user_data,
Packit ae235b
                                      &tmp_error);
Packit ae235b
Packit ae235b
  ensure_no_outstanding_subparser (context);
Packit ae235b
Packit ae235b
  if (tmp_error)
Packit ae235b
    {
Packit ae235b
      mark_error (context, tmp_error);
Packit ae235b
      g_propagate_error (error, tmp_error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  pop_tag (context);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_parse:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 * @text: chunk of text to parse
Packit ae235b
 * @text_len: length of @text in bytes
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Feed some data to the #GMarkupParseContext.
Packit ae235b
 *
Packit ae235b
 * The data need not be valid UTF-8; an error will be signaled if
Packit ae235b
 * it's invalid. The data need not be an entire document; you can
Packit ae235b
 * feed a document into the parser incrementally, via multiple calls
Packit ae235b
 * to this function. Typically, as you receive data from a network
Packit ae235b
 * connection or file, you feed each received chunk of data into this
Packit ae235b
 * function, aborting the process if an error occurs. Once an error
Packit ae235b
 * is reported, no further data may be fed to the #GMarkupParseContext;
Packit ae235b
 * all errors are fatal.
Packit ae235b
 *
Packit ae235b
 * Returns: %FALSE if an error occurred, %TRUE on success
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_markup_parse_context_parse (GMarkupParseContext  *context,
Packit ae235b
                              const gchar          *text,
Packit ae235b
                              gssize                text_len,
Packit ae235b
                              GError              **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (context != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (text != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
Packit ae235b
  g_return_val_if_fail (!context->parsing, FALSE);
Packit ae235b
Packit ae235b
  if (text_len < 0)
Packit ae235b
    text_len = strlen (text);
Packit ae235b
Packit ae235b
  if (text_len == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  context->parsing = TRUE;
Packit ae235b
Packit ae235b
Packit ae235b
  context->current_text = text;
Packit ae235b
  context->current_text_len = text_len;
Packit ae235b
  context->current_text_end = context->current_text + text_len;
Packit ae235b
  context->iter = context->current_text;
Packit ae235b
  context->start = context->iter;
Packit ae235b
Packit ae235b
  while (context->iter != context->current_text_end)
Packit ae235b
    {
Packit ae235b
      switch (context->state)
Packit ae235b
        {
Packit ae235b
        case STATE_START:
Packit ae235b
          /* Possible next state: AFTER_OPEN_ANGLE */
Packit ae235b
Packit ae235b
          g_assert (context->tag_stack == NULL);
Packit ae235b
Packit ae235b
          /* whitespace is ignored outside of any elements */
Packit ae235b
          skip_spaces (context);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              if (*context->iter == '<')
Packit ae235b
                {
Packit ae235b
                  /* Move after the open angle */
Packit ae235b
                  advance_char (context);
Packit ae235b
Packit ae235b
                  context->state = STATE_AFTER_OPEN_ANGLE;
Packit ae235b
Packit ae235b
                  /* this could start a passthrough */
Packit ae235b
                  context->start = context->iter;
Packit ae235b
Packit ae235b
                  /* document is now non-empty */
Packit ae235b
                  context->document_empty = FALSE;
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  set_error_literal (context,
Packit ae235b
                                     error,
Packit ae235b
                                     G_MARKUP_ERROR_PARSE,
Packit ae235b
                                     _("Document must begin with an element (e.g. <book>)"));
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_OPEN_ANGLE:
Packit ae235b
          /* Possible next states: INSIDE_OPEN_TAG_NAME,
Packit ae235b
           *  AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
Packit ae235b
           */
Packit ae235b
          if (*context->iter == '?' ||
Packit ae235b
              *context->iter == '!')
Packit ae235b
            {
Packit ae235b
              /* include < in the passthrough */
Packit ae235b
              const gchar *openangle = "<";
Packit ae235b
              add_to_partial (context, openangle, openangle + 1);
Packit ae235b
              context->start = context->iter;
Packit ae235b
              context->balance = 1;
Packit ae235b
              context->state = STATE_INSIDE_PASSTHROUGH;
Packit ae235b
            }
Packit ae235b
          else if (*context->iter == '/')
Packit ae235b
            {
Packit ae235b
              /* move after it */
Packit ae235b
              advance_char (context);
Packit ae235b
Packit ae235b
              context->state = STATE_AFTER_CLOSE_TAG_SLASH;
Packit ae235b
            }
Packit ae235b
          else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
Packit ae235b
            {
Packit ae235b
              context->state = STATE_INSIDE_OPEN_TAG_NAME;
Packit ae235b
Packit ae235b
              /* start of tag name */
Packit ae235b
              context->start = context->iter;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              gchar buf[8];
Packit ae235b
Packit ae235b
              set_error (context,
Packit ae235b
                         error,
Packit ae235b
                         G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("'%s' is not a valid character following "
Packit ae235b
                           "a '<' character; it may not begin an "
Packit ae235b
                           "element name"),
Packit ae235b
                         utf8_str (context->iter, buf));
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
          /* The AFTER_CLOSE_ANGLE state is actually sort of
Packit ae235b
           * broken, because it doesn't correspond to a range
Packit ae235b
           * of characters in the input stream as the others do,
Packit ae235b
           * and thus makes things harder to conceptualize
Packit ae235b
           */
Packit ae235b
        case STATE_AFTER_CLOSE_ANGLE:
Packit ae235b
          /* Possible next states: INSIDE_TEXT, STATE_START */
Packit ae235b
          if (context->tag_stack == NULL)
Packit ae235b
            {
Packit ae235b
              context->start = NULL;
Packit ae235b
              context->state = STATE_START;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              context->start = context->iter;
Packit ae235b
              context->state = STATE_INSIDE_TEXT;
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_ELISION_SLASH:
Packit ae235b
          /* Possible next state: AFTER_CLOSE_ANGLE */
Packit ae235b
          if (*context->iter == '>')
Packit ae235b
            {
Packit ae235b
              /* move after the close angle */
Packit ae235b
              advance_char (context);
Packit ae235b
              context->state = STATE_AFTER_CLOSE_ANGLE;
Packit ae235b
              emit_end_element (context, error);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              gchar buf[8];
Packit ae235b
Packit ae235b
              set_error (context,
Packit ae235b
                         error,
Packit ae235b
                         G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Odd character '%s', expected a '>' character "
Packit ae235b
                           "to end the empty-element tag '%s'"),
Packit ae235b
                         utf8_str (context->iter, buf),
Packit ae235b
                         current_element (context));
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_OPEN_TAG_NAME:
Packit ae235b
          /* Possible next states: BETWEEN_ATTRIBUTES */
Packit ae235b
Packit ae235b
          /* if there's a partial chunk then it's the first part of the
Packit ae235b
           * tag name. If there's a context->start then it's the start
Packit ae235b
           * of the tag name in current_text, the partial chunk goes
Packit ae235b
           * before that start though.
Packit ae235b
           */
Packit ae235b
          advance_to_name_end (context);
Packit ae235b
Packit ae235b
          if (context->iter == context->current_text_end)
Packit ae235b
            {
Packit ae235b
              /* The name hasn't necessarily ended. Merge with
Packit ae235b
               * partial chunk, leave state unchanged.
Packit ae235b
               */
Packit ae235b
              add_to_partial (context, context->start, context->iter);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              /* The name has ended. Combine it with the partial chunk
Packit ae235b
               * if any; push it on the stack; enter next state.
Packit ae235b
               */
Packit ae235b
              add_to_partial (context, context->start, context->iter);
Packit ae235b
              push_partial_as_tag (context);
Packit ae235b
Packit ae235b
              context->state = STATE_BETWEEN_ATTRIBUTES;
Packit ae235b
              context->start = NULL;
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_ATTRIBUTE_NAME:
Packit ae235b
          /* Possible next states: AFTER_ATTRIBUTE_NAME */
Packit ae235b
Packit ae235b
          advance_to_name_end (context);
Packit ae235b
          add_to_partial (context, context->start, context->iter);
Packit ae235b
Packit ae235b
          /* read the full name, if we enter the equals sign state
Packit ae235b
           * then add the attribute to the list (without the value),
Packit ae235b
           * otherwise store a partial chunk to be prepended later.
Packit ae235b
           */
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            context->state = STATE_AFTER_ATTRIBUTE_NAME;
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_ATTRIBUTE_NAME:
Packit ae235b
          /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
Packit ae235b
Packit ae235b
          skip_spaces (context);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              /* The name has ended. Combine it with the partial chunk
Packit ae235b
               * if any; push it on the stack; enter next state.
Packit ae235b
               */
Packit ae235b
              if (!name_validate (context, context->partial_chunk->str, error))
Packit ae235b
                break;
Packit ae235b
Packit ae235b
              add_attribute (context, context->partial_chunk);
Packit ae235b
Packit ae235b
              context->partial_chunk = NULL;
Packit ae235b
              context->start = NULL;
Packit ae235b
Packit ae235b
              if (*context->iter == '=')
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  gchar buf[8];
Packit ae235b
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("Odd character '%s', expected a '=' after "
Packit ae235b
                               "attribute name '%s' of element '%s'"),
Packit ae235b
                             utf8_str (context->iter, buf),
Packit ae235b
                             current_attribute (context),
Packit ae235b
                             current_element (context));
Packit ae235b
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_BETWEEN_ATTRIBUTES:
Packit ae235b
          /* Possible next states: AFTER_CLOSE_ANGLE,
Packit ae235b
           * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
Packit ae235b
           */
Packit ae235b
          skip_spaces (context);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              if (*context->iter == '/')
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_AFTER_ELISION_SLASH;
Packit ae235b
                }
Packit ae235b
              else if (*context->iter == '>')
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_AFTER_CLOSE_ANGLE;
Packit ae235b
                }
Packit ae235b
              else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
Packit ae235b
                {
Packit ae235b
                  context->state = STATE_INSIDE_ATTRIBUTE_NAME;
Packit ae235b
                  /* start of attribute name */
Packit ae235b
                  context->start = context->iter;
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  gchar buf[8];
Packit ae235b
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("Odd character '%s', expected a '>' or '/' "
Packit ae235b
                               "character to end the start tag of "
Packit ae235b
                               "element '%s', or optionally an attribute; "
Packit ae235b
                               "perhaps you used an invalid character in "
Packit ae235b
                               "an attribute name"),
Packit ae235b
                             utf8_str (context->iter, buf),
Packit ae235b
                             current_element (context));
Packit ae235b
                }
Packit ae235b
Packit ae235b
              /* If we're done with attributes, invoke
Packit ae235b
               * the start_element callback
Packit ae235b
               */
Packit ae235b
              if (context->state == STATE_AFTER_ELISION_SLASH ||
Packit ae235b
                  context->state == STATE_AFTER_CLOSE_ANGLE)
Packit ae235b
                emit_start_element (context, error);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
Packit ae235b
          /* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
Packit ae235b
Packit ae235b
          skip_spaces (context);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              if (*context->iter == '"')
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
Packit ae235b
                  context->start = context->iter;
Packit ae235b
                }
Packit ae235b
              else if (*context->iter == '\'')
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
Packit ae235b
                  context->start = context->iter;
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  gchar buf[8];
Packit ae235b
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("Odd character '%s', expected an open quote mark "
Packit ae235b
                               "after the equals sign when giving value for "
Packit ae235b
                               "attribute '%s' of element '%s'"),
Packit ae235b
                             utf8_str (context->iter, buf),
Packit ae235b
                             current_attribute (context),
Packit ae235b
                             current_element (context));
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
Packit ae235b
        case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
Packit ae235b
          /* Possible next states: BETWEEN_ATTRIBUTES */
Packit ae235b
          {
Packit ae235b
            gchar delim;
Packit ae235b
Packit ae235b
            if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
Packit ae235b
              {
Packit ae235b
                delim = '\'';
Packit ae235b
              }
Packit ae235b
            else
Packit ae235b
              {
Packit ae235b
                delim = '"';
Packit ae235b
              }
Packit ae235b
Packit ae235b
            do
Packit ae235b
              {
Packit ae235b
                if (*context->iter == delim)
Packit ae235b
                  break;
Packit ae235b
              }
Packit ae235b
            while (advance_char (context));
Packit ae235b
          }
Packit ae235b
          if (context->iter == context->current_text_end)
Packit ae235b
            {
Packit ae235b
              /* The value hasn't necessarily ended. Merge with
Packit ae235b
               * partial chunk, leave state unchanged.
Packit ae235b
               */
Packit ae235b
              add_to_partial (context, context->start, context->iter);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              gboolean is_ascii;
Packit ae235b
              /* The value has ended at the quote mark. Combine it
Packit ae235b
               * with the partial chunk if any; set it for the current
Packit ae235b
               * attribute.
Packit ae235b
               */
Packit ae235b
              add_to_partial (context, context->start, context->iter);
Packit ae235b
Packit ae235b
              g_assert (context->cur_attr >= 0);
Packit ae235b
Packit ae235b
              if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) &&
Packit ae235b
                  (is_ascii || text_validate (context, context->partial_chunk->str,
Packit ae235b
                                              context->partial_chunk->len, error)))
Packit ae235b
                {
Packit ae235b
                  /* success, advance past quote and set state. */
Packit ae235b
                  context->attr_values[context->cur_attr] = context->partial_chunk;
Packit ae235b
                  context->partial_chunk = NULL;
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_BETWEEN_ATTRIBUTES;
Packit ae235b
                  context->start = NULL;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              truncate_partial (context);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_TEXT:
Packit ae235b
          /* Possible next states: AFTER_OPEN_ANGLE */
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              if (*context->iter == '<')
Packit ae235b
                break;
Packit ae235b
            }
Packit ae235b
          while (advance_char (context));
Packit ae235b
Packit ae235b
          /* The text hasn't necessarily ended. Merge with
Packit ae235b
           * partial chunk, leave state unchanged.
Packit ae235b
           */
Packit ae235b
Packit ae235b
          add_to_partial (context, context->start, context->iter);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              gboolean is_ascii;
Packit ae235b
Packit ae235b
              /* The text has ended at the open angle. Call the text
Packit ae235b
               * callback.
Packit ae235b
               */
Packit ae235b
              if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) &&
Packit ae235b
                  (is_ascii || text_validate (context, context->partial_chunk->str,
Packit ae235b
                                              context->partial_chunk->len, error)))
Packit ae235b
                {
Packit ae235b
                  GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
                  if (context->parser->text)
Packit ae235b
                    (*context->parser->text) (context,
Packit ae235b
                                              context->partial_chunk->str,
Packit ae235b
                                              context->partial_chunk->len,
Packit ae235b
                                              context->user_data,
Packit ae235b
                                              &tmp_error);
Packit ae235b
Packit ae235b
                  if (tmp_error == NULL)
Packit ae235b
                    {
Packit ae235b
                      /* advance past open angle and set state. */
Packit ae235b
                      advance_char (context);
Packit ae235b
                      context->state = STATE_AFTER_OPEN_ANGLE;
Packit ae235b
                      /* could begin a passthrough */
Packit ae235b
                      context->start = context->iter;
Packit ae235b
                    }
Packit ae235b
                  else
Packit ae235b
                    propagate_error (context, error, tmp_error);
Packit ae235b
                }
Packit ae235b
Packit ae235b
              truncate_partial (context);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_CLOSE_TAG_SLASH:
Packit ae235b
          /* Possible next state: INSIDE_CLOSE_TAG_NAME */
Packit ae235b
          if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
Packit ae235b
            {
Packit ae235b
              context->state = STATE_INSIDE_CLOSE_TAG_NAME;
Packit ae235b
Packit ae235b
              /* start of tag name */
Packit ae235b
              context->start = context->iter;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              gchar buf[8];
Packit ae235b
Packit ae235b
              set_error (context,
Packit ae235b
                         error,
Packit ae235b
                         G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("'%s' is not a valid character following "
Packit ae235b
                           "the characters '</'; '%s' may not begin an "
Packit ae235b
                           "element name"),
Packit ae235b
                         utf8_str (context->iter, buf),
Packit ae235b
                         utf8_str (context->iter, buf));
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_CLOSE_TAG_NAME:
Packit ae235b
          /* Possible next state: AFTER_CLOSE_TAG_NAME */
Packit ae235b
          advance_to_name_end (context);
Packit ae235b
          add_to_partial (context, context->start, context->iter);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            context->state = STATE_AFTER_CLOSE_TAG_NAME;
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_AFTER_CLOSE_TAG_NAME:
Packit ae235b
          /* Possible next state: AFTER_CLOSE_TAG_SLASH */
Packit ae235b
Packit ae235b
          skip_spaces (context);
Packit ae235b
Packit ae235b
          if (context->iter != context->current_text_end)
Packit ae235b
            {
Packit ae235b
              GString *close_name;
Packit ae235b
Packit ae235b
              close_name = context->partial_chunk;
Packit ae235b
              context->partial_chunk = NULL;
Packit ae235b
Packit ae235b
              if (*context->iter != '>')
Packit ae235b
                {
Packit ae235b
                  gchar buf[8];
Packit ae235b
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("'%s' is not a valid character following "
Packit ae235b
                               "the close element name '%s'; the allowed "
Packit ae235b
                               "character is '>'"),
Packit ae235b
                             utf8_str (context->iter, buf),
Packit ae235b
                             close_name->str);
Packit ae235b
                }
Packit ae235b
              else if (context->tag_stack == NULL)
Packit ae235b
                {
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("Element '%s' was closed, no element "
Packit ae235b
                               "is currently open"),
Packit ae235b
                             close_name->str);
Packit ae235b
                }
Packit ae235b
              else if (strcmp (close_name->str, current_element (context)) != 0)
Packit ae235b
                {
Packit ae235b
                  set_error (context,
Packit ae235b
                             error,
Packit ae235b
                             G_MARKUP_ERROR_PARSE,
Packit ae235b
                             _("Element '%s' was closed, but the currently "
Packit ae235b
                               "open element is '%s'"),
Packit ae235b
                             close_name->str,
Packit ae235b
                             current_element (context));
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  advance_char (context);
Packit ae235b
                  context->state = STATE_AFTER_CLOSE_ANGLE;
Packit ae235b
                  context->start = NULL;
Packit ae235b
Packit ae235b
                  emit_end_element (context, error);
Packit ae235b
                }
Packit ae235b
              context->partial_chunk = close_name;
Packit ae235b
              truncate_partial (context);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_INSIDE_PASSTHROUGH:
Packit ae235b
          /* Possible next state: AFTER_CLOSE_ANGLE */
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              if (*context->iter == '<')
Packit ae235b
                context->balance++;
Packit ae235b
              if (*context->iter == '>')
Packit ae235b
                {
Packit ae235b
                  gchar *str;
Packit ae235b
                  gsize len;
Packit ae235b
Packit ae235b
                  context->balance--;
Packit ae235b
                  add_to_partial (context, context->start, context->iter);
Packit ae235b
                  context->start = context->iter;
Packit ae235b
Packit ae235b
                  str = context->partial_chunk->str;
Packit ae235b
                  len = context->partial_chunk->len;
Packit ae235b
Packit ae235b
                  if (str[1] == '?' && str[len - 1] == '?')
Packit ae235b
                    break;
Packit ae235b
                  if (strncmp (str, "
Packit ae235b
                      strcmp (str + len - 2, "--") == 0)
Packit ae235b
                    break;
Packit ae235b
                  if (strncmp (str, "
Packit ae235b
                      strcmp (str + len - 2, "]]") == 0)
Packit ae235b
                    break;
Packit ae235b
                  if (strncmp (str, "
Packit ae235b
                      context->balance == 0)
Packit ae235b
                    break;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          while (advance_char (context));
Packit ae235b
Packit ae235b
          if (context->iter == context->current_text_end)
Packit ae235b
            {
Packit ae235b
              /* The passthrough hasn't necessarily ended. Merge with
Packit ae235b
               * partial chunk, leave state unchanged.
Packit ae235b
               */
Packit ae235b
               add_to_partial (context, context->start, context->iter);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              /* The passthrough has ended at the close angle. Combine
Packit ae235b
               * it with the partial chunk if any. Call the passthrough
Packit ae235b
               * callback. Note that the open/close angles are
Packit ae235b
               * included in the text of the passthrough.
Packit ae235b
               */
Packit ae235b
              GError *tmp_error = NULL;
Packit ae235b
Packit ae235b
              advance_char (context); /* advance past close angle */
Packit ae235b
              add_to_partial (context, context->start, context->iter);
Packit ae235b
Packit ae235b
              if (context->flags & G_MARKUP_TREAT_CDATA_AS_TEXT &&
Packit ae235b
                  strncmp (context->partial_chunk->str, "
Packit ae235b
                {
Packit ae235b
                  if (context->parser->text &&
Packit ae235b
                      text_validate (context,
Packit ae235b
                                     context->partial_chunk->str + 9,
Packit ae235b
                                     context->partial_chunk->len - 12,
Packit ae235b
                                     error))
Packit ae235b
                    (*context->parser->text) (context,
Packit ae235b
                                              context->partial_chunk->str + 9,
Packit ae235b
                                              context->partial_chunk->len - 12,
Packit ae235b
                                              context->user_data,
Packit ae235b
                                              &tmp_error);
Packit ae235b
                }
Packit ae235b
              else if (context->parser->passthrough &&
Packit ae235b
                       text_validate (context,
Packit ae235b
                                      context->partial_chunk->str,
Packit ae235b
                                      context->partial_chunk->len,
Packit ae235b
                                      error))
Packit ae235b
                (*context->parser->passthrough) (context,
Packit ae235b
                                                 context->partial_chunk->str,
Packit ae235b
                                                 context->partial_chunk->len,
Packit ae235b
                                                 context->user_data,
Packit ae235b
                                                 &tmp_error);
Packit ae235b
Packit ae235b
              truncate_partial (context);
Packit ae235b
Packit ae235b
              if (tmp_error == NULL)
Packit ae235b
                {
Packit ae235b
                  context->state = STATE_AFTER_CLOSE_ANGLE;
Packit ae235b
                  context->start = context->iter; /* could begin text */
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                propagate_error (context, error, tmp_error);
Packit ae235b
            }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case STATE_ERROR:
Packit ae235b
          goto finished;
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        default:
Packit ae235b
          g_assert_not_reached ();
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
 finished:
Packit ae235b
  context->parsing = FALSE;
Packit ae235b
Packit ae235b
  return context->state != STATE_ERROR;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_end_parse:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Signals to the #GMarkupParseContext that all data has been
Packit ae235b
 * fed into the parse context with g_markup_parse_context_parse().
Packit ae235b
 *
Packit ae235b
 * This function reports an error if the document isn't complete,
Packit ae235b
 * for example if elements are still open.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error was set
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_markup_parse_context_end_parse (GMarkupParseContext  *context,
Packit ae235b
                                  GError              **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (context != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (!context->parsing, FALSE);
Packit ae235b
  g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
Packit ae235b
Packit ae235b
  if (context->partial_chunk != NULL)
Packit ae235b
    {
Packit ae235b
      g_string_free (context->partial_chunk, TRUE);
Packit ae235b
      context->partial_chunk = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (context->document_empty)
Packit ae235b
    {
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_EMPTY,
Packit ae235b
                         _("Document was empty or contained only whitespace"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  context->parsing = TRUE;
Packit ae235b
Packit ae235b
  switch (context->state)
Packit ae235b
    {
Packit ae235b
    case STATE_START:
Packit ae235b
      /* Nothing to do */
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_AFTER_OPEN_ANGLE:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly just after an open angle bracket '<'"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_AFTER_CLOSE_ANGLE:
Packit ae235b
      if (context->tag_stack != NULL)
Packit ae235b
        {
Packit ae235b
          /* Error message the same as for INSIDE_TEXT */
Packit ae235b
          set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                     _("Document ended unexpectedly with elements still open - "
Packit ae235b
                       "'%s' was the last element opened"),
Packit ae235b
                     current_element (context));
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_AFTER_ELISION_SLASH:
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                 _("Document ended unexpectedly, expected to see a close angle "
Packit ae235b
                   "bracket ending the tag <%s/>"), current_element (context));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_INSIDE_OPEN_TAG_NAME:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly inside an element name"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_INSIDE_ATTRIBUTE_NAME:
Packit ae235b
    case STATE_AFTER_ATTRIBUTE_NAME:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly inside an attribute name"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_BETWEEN_ATTRIBUTES:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly inside an element-opening "
Packit ae235b
                           "tag."));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly after the equals sign "
Packit ae235b
                           "following an attribute name; no attribute value"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
Packit ae235b
    case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly while inside an attribute "
Packit ae235b
                           "value"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_INSIDE_TEXT:
Packit ae235b
      g_assert (context->tag_stack != NULL);
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                 _("Document ended unexpectedly with elements still open - "
Packit ae235b
                   "'%s' was the last element opened"),
Packit ae235b
                 current_element (context));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_AFTER_CLOSE_TAG_SLASH:
Packit ae235b
    case STATE_INSIDE_CLOSE_TAG_NAME:
Packit ae235b
    case STATE_AFTER_CLOSE_TAG_NAME:
Packit ae235b
      set_error (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                 _("Document ended unexpectedly inside the close tag for "
Packit ae235b
                   "element '%s'"), current_element (context));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_INSIDE_PASSTHROUGH:
Packit ae235b
      set_error_literal (context, error, G_MARKUP_ERROR_PARSE,
Packit ae235b
                         _("Document ended unexpectedly inside a comment or "
Packit ae235b
                           "processing instruction"));
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case STATE_ERROR:
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  context->parsing = FALSE;
Packit ae235b
Packit ae235b
  return context->state != STATE_ERROR;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_get_element:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Retrieves the name of the currently open element.
Packit ae235b
 *
Packit ae235b
 * If called from the start_element or end_element handlers this will
Packit ae235b
 * give the element_name as passed to those functions. For the parent
Packit ae235b
 * elements, see g_markup_parse_context_get_element_stack().
Packit ae235b
 *
Packit ae235b
 * Returns: the name of the currently open element, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.2
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_markup_parse_context_get_element (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (context != NULL, NULL);
Packit ae235b
Packit ae235b
  if (context->tag_stack == NULL)
Packit ae235b
    return NULL;
Packit ae235b
  else
Packit ae235b
    return current_element (context);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_get_element_stack:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Retrieves the element stack from the internal state of the parser.
Packit ae235b
 *
Packit ae235b
 * The returned #GSList is a list of strings where the first item is
Packit ae235b
 * the currently open tag (as would be returned by
Packit ae235b
 * g_markup_parse_context_get_element()) and the next item is its
Packit ae235b
 * immediate parent.
Packit ae235b
 *
Packit ae235b
 * This function is intended to be used in the start_element and
Packit ae235b
 * end_element handlers where g_markup_parse_context_get_element()
Packit ae235b
 * would merely return the name of the element that is being
Packit ae235b
 * processed.
Packit ae235b
 *
Packit ae235b
 * Returns: the element stack, which must not be modified
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
const GSList *
Packit ae235b
g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (context != NULL, NULL);
Packit ae235b
  return context->tag_stack;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_get_position:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 * @line_number: (nullable): return location for a line number, or %NULL
Packit ae235b
 * @char_number: (nullable): return location for a char-on-line number, or %NULL
Packit ae235b
 *
Packit ae235b
 * Retrieves the current line number and the number of the character on
Packit ae235b
 * that line. Intended for use in error messages; there are no strict
Packit ae235b
 * semantics for what constitutes the "current" line number other than
Packit ae235b
 * "the best number we could come up with for error messages."
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_markup_parse_context_get_position (GMarkupParseContext *context,
Packit ae235b
                                     gint                *line_number,
Packit ae235b
                                     gint                *char_number)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (context != NULL);
Packit ae235b
Packit ae235b
  if (line_number)
Packit ae235b
    *line_number = context->line_number;
Packit ae235b
Packit ae235b
  if (char_number)
Packit ae235b
    *char_number = context->char_number;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_get_user_data:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Returns the user_data associated with @context.
Packit ae235b
 *
Packit ae235b
 * This will either be the user_data that was provided to
Packit ae235b
 * g_markup_parse_context_new() or to the most recent call
Packit ae235b
 * of g_markup_parse_context_push().
Packit ae235b
 *
Packit ae235b
 * Returns: the provided user_data. The returned data belongs to
Packit ae235b
 *     the markup context and will be freed when
Packit ae235b
 *     g_markup_parse_context_free() is called.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_markup_parse_context_get_user_data (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  return context->user_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_push:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 * @parser: a #GMarkupParser
Packit ae235b
 * @user_data: user data to pass to #GMarkupParser functions
Packit ae235b
 *
Packit ae235b
 * Temporarily redirects markup data to a sub-parser.
Packit ae235b
 *
Packit ae235b
 * This function may only be called from the start_element handler of
Packit ae235b
 * a #GMarkupParser. It must be matched with a corresponding call to
Packit ae235b
 * g_markup_parse_context_pop() in the matching end_element handler
Packit ae235b
 * (except in the case that the parser aborts due to an error).
Packit ae235b
 *
Packit ae235b
 * All tags, text and other data between the matching tags is
Packit ae235b
 * redirected to the subparser given by @parser. @user_data is used
Packit ae235b
 * as the user_data for that parser. @user_data is also passed to the
Packit ae235b
 * error callback in the event that an error occurs. This includes
Packit ae235b
 * errors that occur in subparsers of the subparser.
Packit ae235b
 *
Packit ae235b
 * The end tag matching the start tag for which this call was made is
Packit ae235b
 * handled by the previous parser (which is given its own user_data)
Packit ae235b
 * which is why g_markup_parse_context_pop() is provided to allow "one
Packit ae235b
 * last access" to the @user_data provided to this function. In the
Packit ae235b
 * case of error, the @user_data provided here is passed directly to
Packit ae235b
 * the error callback of the subparser and g_markup_parse_context_pop()
Packit ae235b
 * should not be called. In either case, if @user_data was allocated
Packit ae235b
 * then it ought to be freed from both of these locations.
Packit ae235b
 *
Packit ae235b
 * This function is not intended to be directly called by users
Packit ae235b
 * interested in invoking subparsers. Instead, it is intended to be
Packit ae235b
 * used by the subparsers themselves to implement a higher-level
Packit ae235b
 * interface.
Packit ae235b
 *
Packit ae235b
 * As an example, see the following implementation of a simple
Packit ae235b
 * parser that counts the number of tags encountered.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * typedef struct
Packit ae235b
 * {
Packit ae235b
 *   gint tag_count;
Packit ae235b
 * } CounterData;
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * counter_start_element (GMarkupParseContext  *context,
Packit ae235b
 *                        const gchar          *element_name,
Packit ae235b
 *                        const gchar         **attribute_names,
Packit ae235b
 *                        const gchar         **attribute_values,
Packit ae235b
 *                        gpointer              user_data,
Packit ae235b
 *                        GError              **error)
Packit ae235b
 * {
Packit ae235b
 *   CounterData *data = user_data;
Packit ae235b
 *
Packit ae235b
 *   data->tag_count++;
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * counter_error (GMarkupParseContext *context,
Packit ae235b
 *                GError              *error,
Packit ae235b
 *                gpointer             user_data)
Packit ae235b
 * {
Packit ae235b
 *   CounterData *data = user_data;
Packit ae235b
 *
Packit ae235b
 *   g_slice_free (CounterData, data);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static GMarkupParser counter_subparser =
Packit ae235b
 * {
Packit ae235b
 *   counter_start_element,
Packit ae235b
 *   NULL,
Packit ae235b
 *   NULL,
Packit ae235b
 *   NULL,
Packit ae235b
 *   counter_error
Packit ae235b
 * };
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * In order to allow this parser to be easily used as a subparser, the
Packit ae235b
 * following interface is provided:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * void
Packit ae235b
 * start_counting (GMarkupParseContext *context)
Packit ae235b
 * {
Packit ae235b
 *   CounterData *data = g_slice_new (CounterData);
Packit ae235b
 *
Packit ae235b
 *   data->tag_count = 0;
Packit ae235b
 *   g_markup_parse_context_push (context, &counter_subparser, data);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * gint
Packit ae235b
 * end_counting (GMarkupParseContext *context)
Packit ae235b
 * {
Packit ae235b
 *   CounterData *data = g_markup_parse_context_pop (context);
Packit ae235b
 *   int result;
Packit ae235b
 *
Packit ae235b
 *   result = data->tag_count;
Packit ae235b
 *   g_slice_free (CounterData, data);
Packit ae235b
 *
Packit ae235b
 *   return result;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * The subparser would then be used as follows:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * static void start_element (context, element_name, ...)
Packit ae235b
 * {
Packit ae235b
 *   if (strcmp (element_name, "count-these") == 0)
Packit ae235b
 *     start_counting (context);
Packit ae235b
 *
Packit ae235b
 *   // else, handle other tags...
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void end_element (context, element_name, ...)
Packit ae235b
 * {
Packit ae235b
 *   if (strcmp (element_name, "count-these") == 0)
Packit ae235b
 *     g_print ("Counted %d tags\n", end_counting (context));
Packit ae235b
 *
Packit ae235b
 *   // else, handle other tags...
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_markup_parse_context_push (GMarkupParseContext *context,
Packit ae235b
                             const GMarkupParser *parser,
Packit ae235b
                             gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GMarkupRecursionTracker *tracker;
Packit ae235b
Packit ae235b
  tracker = g_slice_new (GMarkupRecursionTracker);
Packit ae235b
  tracker->prev_element = context->subparser_element;
Packit ae235b
  tracker->prev_parser = context->parser;
Packit ae235b
  tracker->prev_user_data = context->user_data;
Packit ae235b
Packit ae235b
  context->subparser_element = current_element (context);
Packit ae235b
  context->parser = parser;
Packit ae235b
  context->user_data = user_data;
Packit ae235b
Packit ae235b
  context->subparser_stack = g_slist_prepend (context->subparser_stack,
Packit ae235b
                                              tracker);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_parse_context_pop:
Packit ae235b
 * @context: a #GMarkupParseContext
Packit ae235b
 *
Packit ae235b
 * Completes the process of a temporary sub-parser redirection.
Packit ae235b
 *
Packit ae235b
 * This function exists to collect the user_data allocated by a
Packit ae235b
 * matching call to g_markup_parse_context_push(). It must be called
Packit ae235b
 * in the end_element handler corresponding to the start_element
Packit ae235b
 * handler during which g_markup_parse_context_push() was called.
Packit ae235b
 * You must not call this function from the error callback -- the
Packit ae235b
 * @user_data is provided directly to the callback in that case.
Packit ae235b
 *
Packit ae235b
 * This function is not intended to be directly called by users
Packit ae235b
 * interested in invoking subparsers. Instead, it is intended to
Packit ae235b
 * be used by the subparsers themselves to implement a higher-level
Packit ae235b
 * interface.
Packit ae235b
 *
Packit ae235b
 * Returns: the user data passed to g_markup_parse_context_push()
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_markup_parse_context_pop (GMarkupParseContext *context)
Packit ae235b
{
Packit ae235b
  gpointer user_data;
Packit ae235b
Packit ae235b
  if (!context->awaiting_pop)
Packit ae235b
    possibly_finish_subparser (context);
Packit ae235b
Packit ae235b
  g_assert (context->awaiting_pop);
Packit ae235b
Packit ae235b
  context->awaiting_pop = FALSE;
Packit ae235b
Packit ae235b
  /* valgrind friendliness */
Packit ae235b
  user_data = context->held_user_data;
Packit ae235b
  context->held_user_data = NULL;
Packit ae235b
Packit ae235b
  return user_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
append_escaped_text (GString     *str,
Packit ae235b
                     const gchar *text,
Packit ae235b
                     gssize       length)
Packit ae235b
{
Packit ae235b
  const gchar *p;
Packit ae235b
  const gchar *end;
Packit ae235b
  gunichar c;
Packit ae235b
Packit ae235b
  p = text;
Packit ae235b
  end = text + length;
Packit ae235b
Packit ae235b
  while (p < end)
Packit ae235b
    {
Packit ae235b
      const gchar *next;
Packit ae235b
      next = g_utf8_next_char (p);
Packit ae235b
Packit ae235b
      switch (*p)
Packit ae235b
        {
Packit ae235b
        case '&':
Packit ae235b
          g_string_append (str, "&");
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '<':
Packit ae235b
          g_string_append (str, "<");
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '>':
Packit ae235b
          g_string_append (str, ">");
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '\'':
Packit ae235b
          g_string_append (str, "'");
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case '"':
Packit ae235b
          g_string_append (str, """);
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        default:
Packit ae235b
          c = g_utf8_get_char (p);
Packit ae235b
          if ((0x1 <= c && c <= 0x8) ||
Packit ae235b
              (0xb <= c && c  <= 0xc) ||
Packit ae235b
              (0xe <= c && c <= 0x1f) ||
Packit ae235b
              (0x7f <= c && c <= 0x84) ||
Packit ae235b
              (0x86 <= c && c <= 0x9f))
Packit ae235b
            g_string_append_printf (str, "&#x%x;", c);
Packit ae235b
          else
Packit ae235b
            g_string_append_len (str, p, next - p);
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      p = next;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_escape_text:
Packit ae235b
 * @text: some valid UTF-8 text
Packit ae235b
 * @length: length of @text in bytes, or -1 if the text is nul-terminated
Packit ae235b
 *
Packit ae235b
 * Escapes text so that the markup parser will parse it verbatim.
Packit ae235b
 * Less than, greater than, ampersand, etc. are replaced with the
Packit ae235b
 * corresponding entities. This function would typically be used
Packit ae235b
 * when writing out a file to be parsed with the markup parser.
Packit ae235b
 *
Packit ae235b
 * Note that this function doesn't protect whitespace and line endings
Packit ae235b
 * from being processed according to the XML rules for normalization
Packit ae235b
 * of line endings and attribute values.
Packit ae235b
 *
Packit ae235b
 * Note also that this function will produce character references in
Packit ae235b
 * the range of  ...  for all control sequences
Packit ae235b
 * except for tabstop, newline and carriage return.  The character
Packit ae235b
 * references in this range are not valid XML 1.0, but they are
Packit ae235b
 * valid XML 1.1 and will be accepted by the GMarkup parser.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string with the escaped text
Packit ae235b
 */
Packit ae235b
gchar*
Packit ae235b
g_markup_escape_text (const gchar *text,
Packit ae235b
                      gssize       length)
Packit ae235b
{
Packit ae235b
  GString *str;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (text != NULL, NULL);
Packit ae235b
Packit ae235b
  if (length < 0)
Packit ae235b
    length = strlen (text);
Packit ae235b
Packit ae235b
  /* prealloc at least as long as original text */
Packit ae235b
  str = g_string_sized_new (length);
Packit ae235b
  append_escaped_text (str, text, length);
Packit ae235b
Packit ae235b
  return g_string_free (str, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * find_conversion:
Packit ae235b
 * @format: a printf-style format string
Packit ae235b
 * @after: location to store a pointer to the character after
Packit ae235b
 *     the returned conversion. On a %NULL return, returns the
Packit ae235b
 *     pointer to the trailing NUL in the string
Packit ae235b
 *
Packit ae235b
 * Find the next conversion in a printf-style format string.
Packit ae235b
 * Partially based on code from printf-parser.c,
Packit ae235b
 * Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
Packit ae235b
 *
Packit ae235b
 * Returns: pointer to the next conversion in @format,
Packit ae235b
 *  or %NULL, if none.
Packit ae235b
 */
Packit ae235b
static const char *
Packit ae235b
find_conversion (const char  *format,
Packit ae235b
                 const char **after)
Packit ae235b
{
Packit ae235b
  const char *start = format;
Packit ae235b
  const char *cp;
Packit ae235b
Packit ae235b
  while (*start != '\0' && *start != '%')
Packit ae235b
    start++;
Packit ae235b
Packit ae235b
  if (*start == '\0')
Packit ae235b
    {
Packit ae235b
      *after = start;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  cp = start + 1;
Packit ae235b
Packit ae235b
  if (*cp == '\0')
Packit ae235b
    {
Packit ae235b
      *after = cp;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Test for positional argument.  */
Packit ae235b
  if (*cp >= '0' && *cp <= '9')
Packit ae235b
    {
Packit ae235b
      const char *np;
Packit ae235b
Packit ae235b
      for (np = cp; *np >= '0' && *np <= '9'; np++)
Packit ae235b
        ;
Packit ae235b
      if (*np == '$')
Packit ae235b
        cp = np + 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Skip the flags.  */
Packit ae235b
  for (;;)
Packit ae235b
    {
Packit ae235b
      if (*cp == '\'' ||
Packit ae235b
          *cp == '-' ||
Packit ae235b
          *cp == '+' ||
Packit ae235b
          *cp == ' ' ||
Packit ae235b
          *cp == '#' ||
Packit ae235b
          *cp == '0')
Packit ae235b
        cp++;
Packit ae235b
      else
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Skip the field width.  */
Packit ae235b
  if (*cp == '*')
Packit ae235b
    {
Packit ae235b
      cp++;
Packit ae235b
Packit ae235b
      /* Test for positional argument.  */
Packit ae235b
      if (*cp >= '0' && *cp <= '9')
Packit ae235b
        {
Packit ae235b
          const char *np;
Packit ae235b
Packit ae235b
          for (np = cp; *np >= '0' && *np <= '9'; np++)
Packit ae235b
            ;
Packit ae235b
          if (*np == '$')
Packit ae235b
            cp = np + 1;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      for (; *cp >= '0' && *cp <= '9'; cp++)
Packit ae235b
        ;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Skip the precision.  */
Packit ae235b
  if (*cp == '.')
Packit ae235b
    {
Packit ae235b
      cp++;
Packit ae235b
      if (*cp == '*')
Packit ae235b
        {
Packit ae235b
          /* Test for positional argument.  */
Packit ae235b
          if (*cp >= '0' && *cp <= '9')
Packit ae235b
            {
Packit ae235b
              const char *np;
Packit ae235b
Packit ae235b
              for (np = cp; *np >= '0' && *np <= '9'; np++)
Packit ae235b
                ;
Packit ae235b
              if (*np == '$')
Packit ae235b
                cp = np + 1;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          for (; *cp >= '0' && *cp <= '9'; cp++)
Packit ae235b
            ;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Skip argument type/size specifiers.  */
Packit ae235b
  while (*cp == 'h' ||
Packit ae235b
         *cp == 'L' ||
Packit ae235b
         *cp == 'l' ||
Packit ae235b
         *cp == 'j' ||
Packit ae235b
         *cp == 'z' ||
Packit ae235b
         *cp == 'Z' ||
Packit ae235b
         *cp == 't')
Packit ae235b
    cp++;
Packit ae235b
Packit ae235b
  /* Skip the conversion character.  */
Packit ae235b
  cp++;
Packit ae235b
Packit ae235b
  *after = cp;
Packit ae235b
  return start;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_vprintf_escaped:
Packit ae235b
 * @format: printf() style format string
Packit ae235b
 * @args: variable argument list, similar to vprintf()
Packit ae235b
 *
Packit ae235b
 * Formats the data in @args according to @format, escaping
Packit ae235b
 * all string and character arguments in the fashion
Packit ae235b
 * of g_markup_escape_text(). See g_markup_printf_escaped().
Packit ae235b
 *
Packit ae235b
 * Returns: newly allocated result from formatting
Packit ae235b
 *  operation. Free with g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
#pragma GCC diagnostic push
Packit ae235b
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_markup_vprintf_escaped (const gchar *format,
Packit ae235b
                          va_list      args)
Packit ae235b
{
Packit ae235b
  GString *format1;
Packit ae235b
  GString *format2;
Packit ae235b
  GString *result = NULL;
Packit ae235b
  gchar *output1 = NULL;
Packit ae235b
  gchar *output2 = NULL;
Packit ae235b
  const char *p, *op1, *op2;
Packit ae235b
  va_list args2;
Packit ae235b
Packit ae235b
  /* The technique here, is that we make two format strings that
Packit ae235b
   * have the identical conversions in the identical order to the
Packit ae235b
   * original strings, but differ in the text in-between. We
Packit ae235b
   * then use the normal g_strdup_vprintf() to format the arguments
Packit ae235b
   * with the two new format strings. By comparing the results,
Packit ae235b
   * we can figure out what segments of the output come from
Packit ae235b
   * the original format string, and what from the arguments,
Packit ae235b
   * and thus know what portions of the string to escape.
Packit ae235b
   *
Packit ae235b
   * For instance, for:
Packit ae235b
   *
Packit ae235b
   *  g_markup_printf_escaped ("%s ate %d apples", "Susan & Fred", 5);
Packit ae235b
   *
Packit ae235b
   * We form the two format strings "%sX%dX" and %sY%sY". The results
Packit ae235b
   * of formatting with those two strings are
Packit ae235b
   *
Packit ae235b
   * "%sX%dX" => "Susan & FredX5X"
Packit ae235b
   * "%sY%dY" => "Susan & FredY5Y"
Packit ae235b
   *
Packit ae235b
   * To find the span of the first argument, we find the first position
Packit ae235b
   * where the two arguments differ, which tells us that the first
Packit ae235b
   * argument formatted to "Susan & Fred". We then escape that
Packit ae235b
   * to "Susan & Fred" and join up with the intermediate portions
Packit ae235b
   * of the format string and the second argument to get
Packit ae235b
   * "Susan & Fred ate 5 apples".
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* Create the two modified format strings
Packit ae235b
   */
Packit ae235b
  format1 = g_string_new (NULL);
Packit ae235b
  format2 = g_string_new (NULL);
Packit ae235b
  p = format;
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      const char *after;
Packit ae235b
      const char *conv = find_conversion (p, &after);
Packit ae235b
      if (!conv)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      g_string_append_len (format1, conv, after - conv);
Packit ae235b
      g_string_append_c (format1, 'X');
Packit ae235b
      g_string_append_len (format2, conv, after - conv);
Packit ae235b
      g_string_append_c (format2, 'Y');
Packit ae235b
Packit ae235b
      p = after;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Use them to format the arguments
Packit ae235b
   */
Packit ae235b
  G_VA_COPY (args2, args);
Packit ae235b
Packit ae235b
  output1 = g_strdup_vprintf (format1->str, args);
Packit ae235b
Packit ae235b
  if (!output1)
Packit ae235b
    {
Packit ae235b
      va_end (args2);
Packit ae235b
      goto cleanup;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  output2 = g_strdup_vprintf (format2->str, args2);
Packit ae235b
  va_end (args2);
Packit ae235b
  if (!output2)
Packit ae235b
    goto cleanup;
Packit ae235b
  result = g_string_new (NULL);
Packit ae235b
Packit ae235b
  /* Iterate through the original format string again,
Packit ae235b
   * copying the non-conversion portions and the escaped
Packit ae235b
   * converted arguments to the output string.
Packit ae235b
   */
Packit ae235b
  op1 = output1;
Packit ae235b
  op2 = output2;
Packit ae235b
  p = format;
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      const char *after;
Packit ae235b
      const char *output_start;
Packit ae235b
      const char *conv = find_conversion (p, &after);
Packit ae235b
      char *escaped;
Packit ae235b
Packit ae235b
      if (!conv)        /* The end, after points to the trailing \0 */
Packit ae235b
        {
Packit ae235b
          g_string_append_len (result, p, after - p);
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_string_append_len (result, p, conv - p);
Packit ae235b
      output_start = op1;
Packit ae235b
      while (*op1 == *op2)
Packit ae235b
        {
Packit ae235b
          op1++;
Packit ae235b
          op2++;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      escaped = g_markup_escape_text (output_start, op1 - output_start);
Packit ae235b
      g_string_append (result, escaped);
Packit ae235b
      g_free (escaped);
Packit ae235b
Packit ae235b
      p = after;
Packit ae235b
      op1++;
Packit ae235b
      op2++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 cleanup:
Packit ae235b
  g_string_free (format1, TRUE);
Packit ae235b
  g_string_free (format2, TRUE);
Packit ae235b
  g_free (output1);
Packit ae235b
  g_free (output2);
Packit ae235b
Packit ae235b
  if (result)
Packit ae235b
    return g_string_free (result, FALSE);
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
#pragma GCC diagnostic pop
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_printf_escaped:
Packit ae235b
 * @format: printf() style format string
Packit ae235b
 * @...: the arguments to insert in the format string
Packit ae235b
 *
Packit ae235b
 * Formats arguments according to @format, escaping
Packit ae235b
 * all string and character arguments in the fashion
Packit ae235b
 * of g_markup_escape_text(). This is useful when you
Packit ae235b
 * want to insert literal strings into XML-style markup
Packit ae235b
 * output, without having to worry that the strings
Packit ae235b
 * might themselves contain markup.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * const char *store = "Fortnum & Mason";
Packit ae235b
 * const char *item = "Tea";
Packit ae235b
 * char *output;
Packit ae235b
 * 
Packit ae235b
 * output = g_markup_printf_escaped ("<purchase>"
Packit ae235b
 *                                   "<store>%s</store>"
Packit ae235b
 *                                   "<item>%s</item>"
Packit ae235b
 *                                   "</purchase>",
Packit ae235b
 *                                   store, item);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: newly allocated result from formatting
Packit ae235b
 *    operation. Free with g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_markup_printf_escaped (const gchar *format, ...)
Packit ae235b
{
Packit ae235b
  char *result;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  result = g_markup_vprintf_escaped (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_markup_parse_boolean (const char  *string,
Packit ae235b
                        gboolean    *value)
Packit ae235b
{
Packit ae235b
  char const * const falses[] = { "false", "f", "no", "n", "0" };
Packit ae235b
  char const * const trues[] = { "true", "t", "yes", "y", "1" };
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (falses); i++)
Packit ae235b
    {
Packit ae235b
      if (g_ascii_strcasecmp (string, falses[i]) == 0)
Packit ae235b
        {
Packit ae235b
          if (value != NULL)
Packit ae235b
            *value = FALSE;
Packit ae235b
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (trues); i++)
Packit ae235b
    {
Packit ae235b
      if (g_ascii_strcasecmp (string, trues[i]) == 0)
Packit ae235b
        {
Packit ae235b
          if (value != NULL)
Packit ae235b
            *value = TRUE;
Packit ae235b
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GMarkupCollectType:
Packit ae235b
 * @G_MARKUP_COLLECT_INVALID: used to terminate the list of attributes
Packit ae235b
 *     to collect
Packit ae235b
 * @G_MARKUP_COLLECT_STRING: collect the string pointer directly from
Packit ae235b
 *     the attribute_values[] array. Expects a parameter of type (const
Packit ae235b
 *     char **). If %G_MARKUP_COLLECT_OPTIONAL is specified and the
Packit ae235b
 *     attribute isn't present then the pointer will be set to %NULL
Packit ae235b
 * @G_MARKUP_COLLECT_STRDUP: as with %G_MARKUP_COLLECT_STRING, but
Packit ae235b
 *     expects a parameter of type (char **) and g_strdup()s the
Packit ae235b
 *     returned pointer. The pointer must be freed with g_free()
Packit ae235b
 * @G_MARKUP_COLLECT_BOOLEAN: expects a parameter of type (gboolean *)
Packit ae235b
 *     and parses the attribute value as a boolean. Sets %FALSE if the
Packit ae235b
 *     attribute isn't present. Valid boolean values consist of
Packit ae235b
 *     (case-insensitive) "false", "f", "no", "n", "0" and "true", "t",
Packit ae235b
 *     "yes", "y", "1"
Packit ae235b
 * @G_MARKUP_COLLECT_TRISTATE: as with %G_MARKUP_COLLECT_BOOLEAN, but
Packit ae235b
 *     in the case of a missing attribute a value is set that compares
Packit ae235b
 *     equal to neither %FALSE nor %TRUE G_MARKUP_COLLECT_OPTIONAL is
Packit ae235b
 *     implied
Packit ae235b
 * @G_MARKUP_COLLECT_OPTIONAL: can be bitwise ORed with the other fields.
Packit ae235b
 *     If present, allows the attribute not to appear. A default value
Packit ae235b
 *     is set depending on what value type is used
Packit ae235b
 *
Packit ae235b
 * A mixed enumerated type and flags field. You must specify one type
Packit ae235b
 * (string, strdup, boolean, tristate).  Additionally, you may  optionally
Packit ae235b
 * bitwise OR the type with the flag %G_MARKUP_COLLECT_OPTIONAL.
Packit ae235b
 *
Packit ae235b
 * It is likely that this enum will be extended in the future to
Packit ae235b
 * support other types.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_markup_collect_attributes:
Packit ae235b
 * @element_name: the current tag name
Packit ae235b
 * @attribute_names: the attribute names
Packit ae235b
 * @attribute_values: the attribute values
Packit ae235b
 * @error: a pointer to a #GError or %NULL
Packit ae235b
 * @first_type: the #GMarkupCollectType of the first attribute
Packit ae235b
 * @first_attr: the name of the first attribute
Packit ae235b
 * @...: a pointer to the storage location of the first attribute
Packit ae235b
 *     (or %NULL), followed by more types names and pointers, ending
Packit ae235b
 *     with %G_MARKUP_COLLECT_INVALID
Packit ae235b
 *
Packit ae235b
 * Collects the attributes of the element from the data passed to the
Packit ae235b
 * #GMarkupParser start_element function, dealing with common error
Packit ae235b
 * conditions and supporting boolean values.
Packit ae235b
 *
Packit ae235b
 * This utility function is not required to write a parser but can save
Packit ae235b
 * a lot of typing.
Packit ae235b
 *
Packit ae235b
 * The @element_name, @attribute_names, @attribute_values and @error
Packit ae235b
 * parameters passed to the start_element callback should be passed
Packit ae235b
 * unmodified to this function.
Packit ae235b
 *
Packit ae235b
 * Following these arguments is a list of "supported" attributes to collect.
Packit ae235b
 * It is an error to specify multiple attributes with the same name. If any
Packit ae235b
 * attribute not in the list appears in the @attribute_names array then an
Packit ae235b
 * unknown attribute error will result.
Packit ae235b
 *
Packit ae235b
 * The #GMarkupCollectType field allows specifying the type of collection
Packit ae235b
 * to perform and if a given attribute must appear or is optional.
Packit ae235b
 *
Packit ae235b
 * The attribute name is simply the name of the attribute to collect.
Packit ae235b
 *
Packit ae235b
 * The pointer should be of the appropriate type (see the descriptions
Packit ae235b
 * under #GMarkupCollectType) and may be %NULL in case a particular
Packit ae235b
 * attribute is to be allowed but ignored.
Packit ae235b
 *
Packit ae235b
 * This function deals with issuing errors for missing attributes
Packit ae235b
 * (of type %G_MARKUP_ERROR_MISSING_ATTRIBUTE), unknown attributes
Packit ae235b
 * (of type %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE) and duplicate
Packit ae235b
 * attributes (of type %G_MARKUP_ERROR_INVALID_CONTENT) as well
Packit ae235b
 * as parse errors for boolean-valued attributes (again of type
Packit ae235b
 * %G_MARKUP_ERROR_INVALID_CONTENT). In all of these cases %FALSE
Packit ae235b
 * will be returned and @error will be set as appropriate.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if successful
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_markup_collect_attributes (const gchar         *element_name,
Packit ae235b
                             const gchar        **attribute_names,
Packit ae235b
                             const gchar        **attribute_values,
Packit ae235b
                             GError             **error,
Packit ae235b
                             GMarkupCollectType   first_type,
Packit ae235b
                             const gchar         *first_attr,
Packit ae235b
                             ...)
Packit ae235b
{
Packit ae235b
  GMarkupCollectType type;
Packit ae235b
  const gchar *attr;
Packit ae235b
  guint64 collected;
Packit ae235b
  int written;
Packit ae235b
  va_list ap;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  type = first_type;
Packit ae235b
  attr = first_attr;
Packit ae235b
  collected = 0;
Packit ae235b
  written = 0;
Packit ae235b
Packit ae235b
  va_start (ap, first_attr);
Packit ae235b
  while (type != G_MARKUP_COLLECT_INVALID)
Packit ae235b
    {
Packit ae235b
      gboolean mandatory;
Packit ae235b
      const gchar *value;
Packit ae235b
Packit ae235b
      mandatory = !(type & G_MARKUP_COLLECT_OPTIONAL);
Packit ae235b
      type &= (G_MARKUP_COLLECT_OPTIONAL - 1);
Packit ae235b
Packit ae235b
      /* tristate records a value != TRUE and != FALSE
Packit ae235b
       * for the case where the attribute is missing
Packit ae235b
       */
Packit ae235b
      if (type == G_MARKUP_COLLECT_TRISTATE)
Packit ae235b
        mandatory = FALSE;
Packit ae235b
Packit ae235b
      for (i = 0; attribute_names[i]; i++)
Packit ae235b
        if (i >= 40 || !(collected & (G_GUINT64_CONSTANT(1) << i)))
Packit ae235b
          if (!strcmp (attribute_names[i], attr))
Packit ae235b
            break;
Packit ae235b
Packit ae235b
      /* ISO C99 only promises that the user can pass up to 127 arguments.
Packit ae235b
       * Subtracting the first 4 arguments plus the final NULL and dividing
Packit ae235b
       * by 3 arguments per collected attribute, we are left with a maximum
Packit ae235b
       * number of supported attributes of (127 - 5) / 3 = 40.
Packit ae235b
       *
Packit ae235b
       * In reality, nobody is ever going to call us with anywhere close to
Packit ae235b
       * 40 attributes to collect, so it is safe to assume that if i > 40
Packit ae235b
       * then the user has given some invalid or repeated arguments.  These
Packit ae235b
       * problems will be caught and reported at the end of the function.
Packit ae235b
       *
Packit ae235b
       * We know at this point that we have an error, but we don't know
Packit ae235b
       * what error it is, so just continue...
Packit ae235b
       */
Packit ae235b
      if (i < 40)
Packit ae235b
        collected |= (G_GUINT64_CONSTANT(1) << i);
Packit ae235b
Packit ae235b
      value = attribute_values[i];
Packit ae235b
Packit ae235b
      if (value == NULL && mandatory)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_MISSING_ATTRIBUTE,
Packit ae235b
                       "element '%s' requires attribute '%s'",
Packit ae235b
                       element_name, attr);
Packit ae235b
Packit ae235b
          va_end (ap);
Packit ae235b
          goto failure;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      switch (type)
Packit ae235b
        {
Packit ae235b
        case G_MARKUP_COLLECT_STRING:
Packit ae235b
          {
Packit ae235b
            const char **str_ptr;
Packit ae235b
Packit ae235b
            str_ptr = va_arg (ap, const char **);
Packit ae235b
Packit ae235b
            if (str_ptr != NULL)
Packit ae235b
              *str_ptr = value;
Packit ae235b
          }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case G_MARKUP_COLLECT_STRDUP:
Packit ae235b
          {
Packit ae235b
            char **str_ptr;
Packit ae235b
Packit ae235b
            str_ptr = va_arg (ap, char **);
Packit ae235b
Packit ae235b
            if (str_ptr != NULL)
Packit ae235b
              *str_ptr = g_strdup (value);
Packit ae235b
          }
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        case G_MARKUP_COLLECT_BOOLEAN:
Packit ae235b
        case G_MARKUP_COLLECT_TRISTATE:
Packit ae235b
          if (value == NULL)
Packit ae235b
            {
Packit ae235b
              gboolean *bool_ptr;
Packit ae235b
Packit ae235b
              bool_ptr = va_arg (ap, gboolean *);
Packit ae235b
Packit ae235b
              if (bool_ptr != NULL)
Packit ae235b
                {
Packit ae235b
                  if (type == G_MARKUP_COLLECT_TRISTATE)
Packit ae235b
                    /* constructivists rejoice!
Packit ae235b
                     * neither false nor true...
Packit ae235b
                     */
Packit ae235b
                    *bool_ptr = -1;
Packit ae235b
Packit ae235b
                  else /* G_MARKUP_COLLECT_BOOLEAN */
Packit ae235b
                    *bool_ptr = FALSE;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              if (!g_markup_parse_boolean (value, va_arg (ap, gboolean *)))
Packit ae235b
                {
Packit ae235b
                  g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                               G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                               "element '%s', attribute '%s', value '%s' "
Packit ae235b
                               "cannot be parsed as a boolean value",
Packit ae235b
                               element_name, attr, value);
Packit ae235b
Packit ae235b
                  va_end (ap);
Packit ae235b
                  goto failure;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          break;
Packit ae235b
Packit ae235b
        default:
Packit ae235b
          g_assert_not_reached ();
Packit ae235b
        }
Packit ae235b
Packit ae235b
      type = va_arg (ap, GMarkupCollectType);
Packit ae235b
      attr = va_arg (ap, const char *);
Packit ae235b
      written++;
Packit ae235b
    }
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  /* ensure we collected all the arguments */
Packit ae235b
  for (i = 0; attribute_names[i]; i++)
Packit ae235b
    if ((collected & (G_GUINT64_CONSTANT(1) << i)) == 0)
Packit ae235b
      {
Packit ae235b
        /* attribute not collected:  could be caused by two things.
Packit ae235b
         *
Packit ae235b
         * 1) it doesn't exist in our list of attributes
Packit ae235b
         * 2) it existed but was matched by a duplicate attribute earlier
Packit ae235b
         *
Packit ae235b
         * find out.
Packit ae235b
         */
Packit ae235b
        int j;
Packit ae235b
Packit ae235b
        for (j = 0; j < i; j++)
Packit ae235b
          if (strcmp (attribute_names[i], attribute_names[j]) == 0)
Packit ae235b
            /* duplicate! */
Packit ae235b
            break;
Packit ae235b
Packit ae235b
        /* j is now the first occurrence of attribute_names[i] */
Packit ae235b
        if (i == j)
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
Packit ae235b
                       "attribute '%s' invalid for element '%s'",
Packit ae235b
                       attribute_names[i], element_name);
Packit ae235b
        else
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       "attribute '%s' given multiple times for element '%s'",
Packit ae235b
                       attribute_names[i], element_name);
Packit ae235b
Packit ae235b
        goto failure;
Packit ae235b
      }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
Packit ae235b
failure:
Packit ae235b
  /* replay the above to free allocations */
Packit ae235b
  type = first_type;
Packit ae235b
  attr = first_attr;
Packit ae235b
Packit ae235b
  va_start (ap, first_attr);
Packit ae235b
  while (type != G_MARKUP_COLLECT_INVALID)
Packit ae235b
    {
Packit ae235b
      gpointer ptr;
Packit ae235b
Packit ae235b
      ptr = va_arg (ap, gpointer);
Packit ae235b
Packit ae235b
      if (ptr != NULL)
Packit ae235b
        {
Packit ae235b
          switch (type & (G_MARKUP_COLLECT_OPTIONAL - 1))
Packit ae235b
            {
Packit ae235b
            case G_MARKUP_COLLECT_STRDUP:
Packit ae235b
              if (written)
Packit ae235b
                g_free (*(char **) ptr);
Packit ae235b
Packit ae235b
            case G_MARKUP_COLLECT_STRING:
Packit ae235b
              *(char **) ptr = NULL;
Packit ae235b
              break;
Packit ae235b
Packit ae235b
            case G_MARKUP_COLLECT_BOOLEAN:
Packit ae235b
              *(gboolean *) ptr = FALSE;
Packit ae235b
              break;
Packit ae235b
Packit ae235b
            case G_MARKUP_COLLECT_TRISTATE:
Packit ae235b
              *(gboolean *) ptr = -1;
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      type = va_arg (ap, GMarkupCollectType);
Packit ae235b
      attr = va_arg (ap, const char *);
Packit ae235b
    }
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}