Blame gio/glib-compile-schemas.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink Limited
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
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* Prologue {{{1 */
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gstdio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <locale.h>
Packit ae235b
Packit ae235b
#include "gvdb/gvdb-builder.h"
Packit ae235b
#include "strinfo.c"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include "glib/glib-private.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
strip_string (GString *string)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; g_ascii_isspace (string->str[i]); i++);
Packit ae235b
  g_string_erase (string, 0, i);
Packit ae235b
Packit ae235b
  if (string->len > 0)
Packit ae235b
    {
Packit ae235b
      /* len > 0, so there must be at least one non-whitespace character */
Packit ae235b
      for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
Packit ae235b
      g_string_truncate (string, i + 1);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Handling of <enum> {{{1 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GString *strinfo;
Packit ae235b
Packit ae235b
  gboolean is_flags;
Packit ae235b
} EnumState;
Packit ae235b
Packit ae235b
static void
Packit ae235b
enum_state_free (gpointer data)
Packit ae235b
{
Packit ae235b
  EnumState *state = data;
Packit ae235b
Packit ae235b
  g_string_free (state->strinfo, TRUE);
Packit ae235b
  g_slice_free (EnumState, state);
Packit ae235b
}
Packit ae235b
Packit ae235b
static EnumState *
Packit ae235b
enum_state_new (gboolean is_flags)
Packit ae235b
{
Packit ae235b
  EnumState *state;
Packit ae235b
Packit ae235b
  state = g_slice_new (EnumState);
Packit ae235b
  state->strinfo = g_string_new (NULL);
Packit ae235b
  state->is_flags = is_flags;
Packit ae235b
Packit ae235b
  return state;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enum_state_add_value (EnumState    *state,
Packit ae235b
                      const gchar  *nick,
Packit ae235b
                      const gchar  *valuestr,
Packit ae235b
                      GError      **error)
Packit ae235b
{
Packit ae235b
  gint64 value;
Packit ae235b
  gchar *end;
Packit ae235b
Packit ae235b
  if (nick[0] == '\0' || nick[1] == '\0')
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("nick must be a minimum of 2 characters"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  value = g_ascii_strtoll (valuestr, &end, 0);
Packit ae235b
  if (*end || (state->is_flags ?
Packit ae235b
                (value > G_MAXUINT32 || value < 0) :
Packit ae235b
                (value > G_MAXINT32 || value < G_MININT32)))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("Invalid numeric value"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (strinfo_builder_contains (state->strinfo, nick))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<value nick='%s'/> already specified"), nick);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (strinfo_builder_contains_value (state->strinfo, value))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("value='%s' already specified"), valuestr);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Silently drop the null case if it is mentioned.
Packit ae235b
   * It is properly denoted with an empty array.
Packit ae235b
   */
Packit ae235b
  if (state->is_flags && value == 0)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (state->is_flags && (value & (value - 1)))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("flags values must have at most 1 bit set"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Since we reject exact duplicates of value='' and we only allow one
Packit ae235b
   * bit to be set, it's not possible to have overlaps.
Packit ae235b
   *
Packit ae235b
   * If we loosen the one-bit-set restriction we need an overlap check.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  strinfo_builder_append_item (state->strinfo, nick, value);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enum_state_end (EnumState **state_ptr,
Packit ae235b
                GError    **error)
Packit ae235b
{
Packit ae235b
  EnumState *state;
Packit ae235b
Packit ae235b
  state = *state_ptr;
Packit ae235b
  *state_ptr = NULL;
Packit ae235b
Packit ae235b
  if (state->strinfo->len == 0)
Packit ae235b
    g_set_error (error,
Packit ae235b
                 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                 _("<%s> must contain at least one <value>"),
Packit ae235b
                 state->is_flags ? "flags" : "enum");
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Handling of <key> {{{1 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  /* for <child>, @child_schema will be set.
Packit ae235b
   * for <key>, everything else will be set.
Packit ae235b
   */
Packit ae235b
  gchar        *child_schema;
Packit ae235b
Packit ae235b
Packit ae235b
  GVariantType *type;
Packit ae235b
  gboolean      have_gettext_domain;
Packit ae235b
Packit ae235b
  gchar         l10n;
Packit ae235b
  gchar        *l10n_context;
Packit ae235b
  GString      *unparsed_default_value;
Packit ae235b
  GVariant     *default_value;
Packit ae235b
Packit ae235b
  GString      *strinfo;
Packit ae235b
  gboolean      is_enum;
Packit ae235b
  gboolean      is_flags;
Packit ae235b
Packit ae235b
  GVariant     *minimum;
Packit ae235b
  GVariant     *maximum;
Packit ae235b
Packit ae235b
  gboolean      has_choices;
Packit ae235b
  gboolean      has_aliases;
Packit ae235b
  gboolean      is_override;
Packit ae235b
Packit ae235b
  gboolean      checked;
Packit ae235b
  GVariant     *serialised;
Packit ae235b
Packit ae235b
  gboolean      summary_seen;
Packit ae235b
  gboolean      description_seen;
Packit ae235b
} KeyState;
Packit ae235b
Packit ae235b
static KeyState *
Packit ae235b
key_state_new (const gchar *type_string,
Packit ae235b
               const gchar *gettext_domain,
Packit ae235b
               gboolean     is_enum,
Packit ae235b
               gboolean     is_flags,
Packit ae235b
               GString     *strinfo)
Packit ae235b
{
Packit ae235b
  KeyState *state;
Packit ae235b
Packit ae235b
  state = g_slice_new0 (KeyState);
Packit ae235b
  state->type = g_variant_type_new (type_string);
Packit ae235b
  state->have_gettext_domain = gettext_domain != NULL;
Packit ae235b
  state->is_enum = is_enum;
Packit ae235b
  state->is_flags = is_flags;
Packit ae235b
  state->summary_seen = FALSE;
Packit ae235b
  state->description_seen = FALSE;
Packit ae235b
Packit ae235b
  if (strinfo)
Packit ae235b
    state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
Packit ae235b
  else
Packit ae235b
    state->strinfo = g_string_new (NULL);
Packit ae235b
Packit ae235b
  return state;
Packit ae235b
}
Packit ae235b
Packit ae235b
static KeyState *
Packit ae235b
key_state_override (KeyState    *state,
Packit ae235b
                    const gchar *gettext_domain)
Packit ae235b
{
Packit ae235b
  KeyState *copy;
Packit ae235b
Packit ae235b
  copy = g_slice_new0 (KeyState);
Packit ae235b
  copy->type = g_variant_type_copy (state->type);
Packit ae235b
  copy->have_gettext_domain = gettext_domain != NULL;
Packit ae235b
  copy->strinfo = g_string_new_len (state->strinfo->str,
Packit ae235b
                                    state->strinfo->len);
Packit ae235b
  copy->is_enum = state->is_enum;
Packit ae235b
  copy->is_flags = state->is_flags;
Packit ae235b
  copy->is_override = TRUE;
Packit ae235b
Packit ae235b
  if (state->minimum)
Packit ae235b
    {
Packit ae235b
      copy->minimum = g_variant_ref (state->minimum);
Packit ae235b
      copy->maximum = g_variant_ref (state->maximum);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return copy;
Packit ae235b
}
Packit ae235b
Packit ae235b
static KeyState *
Packit ae235b
key_state_new_child (const gchar *child_schema)
Packit ae235b
{
Packit ae235b
  KeyState *state;
Packit ae235b
Packit ae235b
  state = g_slice_new0 (KeyState);
Packit ae235b
  state->child_schema = g_strdup (child_schema);
Packit ae235b
Packit ae235b
  return state;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_choices (GVariant *variant,
Packit ae235b
                  GString  *strinfo)
Packit ae235b
{
Packit ae235b
  switch (g_variant_classify (variant))
Packit ae235b
    {
Packit ae235b
      case G_VARIANT_CLASS_MAYBE:
Packit ae235b
      case G_VARIANT_CLASS_ARRAY:
Packit ae235b
        {
Packit ae235b
          gboolean valid = TRUE;
Packit ae235b
          GVariantIter iter;
Packit ae235b
Packit ae235b
          g_variant_iter_init (&iter, variant);
Packit ae235b
Packit ae235b
          while (valid && (variant = g_variant_iter_next_value (&iter)))
Packit ae235b
            {
Packit ae235b
              valid = is_valid_choices (variant, strinfo);
Packit ae235b
              g_variant_unref (variant);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          return valid;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      case G_VARIANT_CLASS_STRING:
Packit ae235b
        return strinfo_is_string_valid ((const guint32 *) strinfo->str,
Packit ae235b
                                        strinfo->len / 4,
Packit ae235b
                                        g_variant_get_string (variant, NULL));
Packit ae235b
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* Gets called at </default> </choices> or <range/> to check for
Packit ae235b
 * validity of the default value so that any inconsistency is
Packit ae235b
 * reported as soon as it is encountered.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
key_state_check_range (KeyState  *state,
Packit ae235b
                       GError   **error)
Packit ae235b
{
Packit ae235b
  if (state->default_value)
Packit ae235b
    {
Packit ae235b
      const gchar *tag;
Packit ae235b
Packit ae235b
      tag = state->is_override ? "override" : "default";
Packit ae235b
Packit ae235b
      if (state->minimum)
Packit ae235b
        {
Packit ae235b
          if (g_variant_compare (state->default_value, state->minimum) < 0 ||
Packit ae235b
              g_variant_compare (state->default_value, state->maximum) > 0)
Packit ae235b
            {
Packit ae235b
              g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<%s> is not contained in "
Packit ae235b
                           "the specified range"), tag);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (state->strinfo->len)
Packit ae235b
        {
Packit ae235b
          if (!is_valid_choices (state->default_value, state->strinfo))
Packit ae235b
            {
Packit ae235b
              if (state->is_enum)
Packit ae235b
                g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                             G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                             _("<%s> is not a valid member of "
Packit ae235b
                             "the specified enumerated type"), tag);
Packit ae235b
Packit ae235b
              else if (state->is_flags)
Packit ae235b
                g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                             G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                             _("<%s> contains string not in the "
Packit ae235b
                             "specified flags type"), tag);
Packit ae235b
Packit ae235b
              else
Packit ae235b
                g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                             G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                             _("<%s> contains a string not in "
Packit ae235b
                             "<choices>"), tag);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_set_range (KeyState     *state,
Packit ae235b
                     const gchar  *min_str,
Packit ae235b
                     const gchar  *max_str,
Packit ae235b
                     GError      **error)
Packit ae235b
{
Packit ae235b
  const struct {
Packit ae235b
    const gchar  type;
Packit ae235b
    const gchar *min;
Packit ae235b
    const gchar *max;
Packit ae235b
  } table[] = {
Packit ae235b
    { 'y',                    "0",                  "255" },
Packit ae235b
    { 'n',               "-32768",                "32767" },
Packit ae235b
    { 'q',                    "0",                "65535" },
Packit ae235b
    { 'i',          "-2147483648",           "2147483647" },
Packit ae235b
    { 'u',                    "0",           "4294967295" },
Packit ae235b
    { 'x', "-9223372036854775808",  "9223372036854775807" },
Packit ae235b
    { 't',                    "0", "18446744073709551615" },
Packit ae235b
    { 'd',                 "-inf",                  "inf" },
Packit ae235b
  };
Packit ae235b
  gboolean type_ok = FALSE;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  if (state->minimum)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<range/> already specified for this key"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (table); i++)
Packit ae235b
    if (*(char *) state->type == table[i].type)
Packit ae235b
      {
Packit ae235b
        min_str = min_str ? min_str : table[i].min;
Packit ae235b
        max_str = max_str ? max_str : table[i].max;
Packit ae235b
        type_ok = TRUE;
Packit ae235b
        break;
Packit ae235b
      }
Packit ae235b
Packit ae235b
  if (!type_ok)
Packit ae235b
    {
Packit ae235b
      gchar *type = g_variant_type_dup_string (state->type);
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                  G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                  _("<range> not allowed for keys of type “%s”"), type);
Packit ae235b
      g_free (type);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
Packit ae235b
  if (state->minimum == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
Packit ae235b
  if (state->maximum == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  if (g_variant_compare (state->minimum, state->maximum) > 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<range> specified minimum is greater than maximum"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  key_state_check_range (state, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GString *
Packit ae235b
key_state_start_default (KeyState     *state,
Packit ae235b
                         const gchar  *l10n,
Packit ae235b
                         const gchar  *context,
Packit ae235b
                         GError      **error)
Packit ae235b
{
Packit ae235b
  if (l10n != NULL)
Packit ae235b
    {
Packit ae235b
      if (strcmp (l10n, "messages") == 0)
Packit ae235b
        state->l10n = 'm';
Packit ae235b
Packit ae235b
      else if (strcmp (l10n, "time") == 0)
Packit ae235b
        state->l10n = 't';
Packit ae235b
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("unsupported l10n category: %s"), l10n);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (!state->have_gettext_domain)
Packit ae235b
        {
Packit ae235b
          g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                               G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                               _("l10n requested, but no "
Packit ae235b
                               "gettext domain given"));
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      state->l10n_context = g_strdup (context);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (context != NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("translation context given for "
Packit ae235b
                           "value without l10n enabled"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_new (NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_end_default (KeyState  *state,
Packit ae235b
                       GString  **string,
Packit ae235b
                       GError   **error)
Packit ae235b
{
Packit ae235b
  state->unparsed_default_value = *string;
Packit ae235b
  *string = NULL;
Packit ae235b
Packit ae235b
  state->default_value = g_variant_parse (state->type,
Packit ae235b
                                          state->unparsed_default_value->str,
Packit ae235b
                                          NULL, NULL, error);
Packit ae235b
  if (!state->default_value)
Packit ae235b
    {
Packit ae235b
      gchar *type = g_variant_type_dup_string (state->type);
Packit ae235b
      g_prefix_error (error, _("Failed to parse <default> value of type “%s”: "), type);
Packit ae235b
      g_free (type);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  key_state_check_range (state, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_start_choices (KeyState  *state,
Packit ae235b
                         GError   **error)
Packit ae235b
{
Packit ae235b
  const GVariantType *type = state->type;
Packit ae235b
Packit ae235b
  if (state->is_enum)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<choices> cannot be specified for keys "
Packit ae235b
                           "tagged as having an enumerated type"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (state->has_choices)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<choices> already specified for this key"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
Packit ae235b
    type = g_variant_type_element (type);
Packit ae235b
Packit ae235b
  if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
Packit ae235b
    {
Packit ae235b
      gchar *type_string = g_variant_type_dup_string (state->type);
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<choices> not allowed for keys of type “%s”"),
Packit ae235b
                   type_string);
Packit ae235b
      g_free (type_string);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_add_choice (KeyState     *state,
Packit ae235b
                      const gchar  *choice,
Packit ae235b
                      GError      **error)
Packit ae235b
{
Packit ae235b
  if (strinfo_builder_contains (state->strinfo, choice))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<choice value='%s'/> already given"), choice);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strinfo_builder_append_item (state->strinfo, choice, 0);
Packit ae235b
  state->has_choices = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_end_choices (KeyState  *state,
Packit ae235b
                       GError   **error)
Packit ae235b
{
Packit ae235b
  if (!state->has_choices)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<choices> must contain at least one <choice>"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  key_state_check_range (state, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_start_aliases (KeyState  *state,
Packit ae235b
                         GError   **error)
Packit ae235b
{
Packit ae235b
  if (state->has_aliases)
Packit ae235b
    g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                         G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("<aliases> already specified for this key"));
Packit ae235b
  else if (!state->is_flags && !state->is_enum && !state->has_choices)
Packit ae235b
    g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                         G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("<aliases> can only be specified for keys with "
Packit ae235b
                         "enumerated or flags types or after <choices>"));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_add_alias (KeyState     *state,
Packit ae235b
                     const gchar  *alias,
Packit ae235b
                     const gchar  *target,
Packit ae235b
                     GError      **error)
Packit ae235b
{
Packit ae235b
  if (strinfo_builder_contains (state->strinfo, alias))
Packit ae235b
    {
Packit ae235b
      if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
Packit ae235b
                                   state->strinfo->len / 4,
Packit ae235b
                                   alias))
Packit ae235b
        {
Packit ae235b
          if (state->is_enum)
Packit ae235b
            g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                         G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("<alias value='%s'/> given when “%s” is already "
Packit ae235b
                         "a member of the enumerated type"), alias, alias);
Packit ae235b
Packit ae235b
          else
Packit ae235b
            g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                         G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("<alias value='%s'/> given when "
Packit ae235b
                         "<choice value='%s'/> was already given"),
Packit ae235b
                         alias, alias);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else
Packit ae235b
        g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                     G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                     _("<alias value='%s'/> already specified"), alias);
Packit ae235b
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!strinfo_builder_append_alias (state->strinfo, alias, target))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   state->is_enum ?
Packit ae235b
                     _("alias target “%s” is not in enumerated type") :
Packit ae235b
                     _("alias target “%s” is not in <choices>"),
Packit ae235b
                   target);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  state->has_aliases = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_end_aliases (KeyState  *state,
Packit ae235b
                       GError   **error)
Packit ae235b
{
Packit ae235b
  if (!state->has_aliases)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<aliases> must contain at least one <alias>"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
key_state_check (KeyState  *state,
Packit ae235b
                 GError   **error)
Packit ae235b
{
Packit ae235b
  if (state->checked)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  return state->checked = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
key_state_serialise (KeyState *state)
Packit ae235b
{
Packit ae235b
  if (state->serialised == NULL)
Packit ae235b
    {
Packit ae235b
      if (state->child_schema)
Packit ae235b
        {
Packit ae235b
          state->serialised = g_variant_new_string (state->child_schema);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          GVariantBuilder builder;
Packit ae235b
Packit ae235b
          g_assert (key_state_check (state, NULL));
Packit ae235b
Packit ae235b
          g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
Packit ae235b
Packit ae235b
          /* default value */
Packit ae235b
          g_variant_builder_add_value (&builder, state->default_value);
Packit ae235b
Packit ae235b
          /* translation */
Packit ae235b
          if (state->l10n)
Packit ae235b
            {
Packit ae235b
              /* We are going to store the untranslated default for
Packit ae235b
               * runtime translation according to the current locale.
Packit ae235b
               * We need to strip leading and trailing whitespace from
Packit ae235b
               * the string so that it's exactly the same as the one
Packit ae235b
               * that ended up in the .po file for translation.
Packit ae235b
               *
Packit ae235b
               * We want to do this so that
Packit ae235b
               *
Packit ae235b
               *   <default l10n='messages'>
Packit ae235b
               *     ['a', 'b', 'c']
Packit ae235b
               *   </default>
Packit ae235b
               *
Packit ae235b
               * ends up in the .po file like "['a', 'b', 'c']",
Packit ae235b
               * omitting the extra whitespace at the start and end.
Packit ae235b
               */
Packit ae235b
              strip_string (state->unparsed_default_value);
Packit ae235b
Packit ae235b
              if (state->l10n_context)
Packit ae235b
                {
Packit ae235b
                  gint len;
Packit ae235b
Packit ae235b
                  /* Contextified messages are supported by prepending
Packit ae235b
                   * the context, followed by '\004' to the start of the
Packit ae235b
                   * message string.  We do that here to save GSettings
Packit ae235b
                   * the work later on.
Packit ae235b
                   */
Packit ae235b
                  len = strlen (state->l10n_context);
Packit ae235b
                  state->l10n_context[len] = '\004';
Packit ae235b
                  g_string_prepend_len (state->unparsed_default_value,
Packit ae235b
                                        state->l10n_context, len + 1);
Packit ae235b
                  g_free (state->l10n_context);
Packit ae235b
                  state->l10n_context = NULL;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
Packit ae235b
                                     state->unparsed_default_value->str);
Packit ae235b
              g_string_free (state->unparsed_default_value, TRUE);
Packit ae235b
              state->unparsed_default_value = NULL;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* choice, aliases, enums */
Packit ae235b
          if (state->strinfo->len)
Packit ae235b
            {
Packit ae235b
              GVariant *array;
Packit ae235b
              guint32 *words;
Packit ae235b
              gpointer data;
Packit ae235b
              gsize size;
Packit ae235b
              gint i;
Packit ae235b
Packit ae235b
              data = state->strinfo->str;
Packit ae235b
              size = state->strinfo->len;
Packit ae235b
Packit ae235b
              words = data;
Packit ae235b
              for (i = 0; i < size / sizeof (guint32); i++)
Packit ae235b
                words[i] = GUINT32_TO_LE (words[i]);
Packit ae235b
Packit ae235b
              array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
Packit ae235b
                                               data, size, TRUE,
Packit ae235b
                                               g_free, data);
Packit ae235b
Packit ae235b
              g_string_free (state->strinfo, FALSE);
Packit ae235b
              state->strinfo = NULL;
Packit ae235b
Packit ae235b
              g_variant_builder_add (&builder, "(y@au)",
Packit ae235b
                                     state->is_flags ? 'f' :
Packit ae235b
                                     state->is_enum ? 'e' : 'c',
Packit ae235b
                                     array);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* range */
Packit ae235b
          if (state->minimum || state->maximum)
Packit ae235b
            g_variant_builder_add (&builder, "(y(**))", 'r',
Packit ae235b
                                   state->minimum, state->maximum);
Packit ae235b
Packit ae235b
          state->serialised = g_variant_builder_end (&builder);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_variant_ref_sink (state->serialised);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_variant_ref (state->serialised);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_free (gpointer data)
Packit ae235b
{
Packit ae235b
  KeyState *state = data;
Packit ae235b
Packit ae235b
  if (state->type)
Packit ae235b
    g_variant_type_free (state->type);
Packit ae235b
Packit ae235b
  g_free (state->l10n_context);
Packit ae235b
Packit ae235b
  if (state->unparsed_default_value)
Packit ae235b
    g_string_free (state->unparsed_default_value, TRUE);
Packit ae235b
Packit ae235b
  if (state->default_value)
Packit ae235b
    g_variant_unref (state->default_value);
Packit ae235b
Packit ae235b
  if (state->strinfo)
Packit ae235b
    g_string_free (state->strinfo, TRUE);
Packit ae235b
Packit ae235b
  if (state->minimum)
Packit ae235b
    g_variant_unref (state->minimum);
Packit ae235b
Packit ae235b
  if (state->maximum)
Packit ae235b
    g_variant_unref (state->maximum);
Packit ae235b
Packit ae235b
  if (state->serialised)
Packit ae235b
    g_variant_unref (state->serialised);
Packit ae235b
Packit ae235b
  g_slice_free (KeyState, state);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Key name validity {{{1 */
Packit ae235b
static gboolean allow_any_name = FALSE;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_keyname (const gchar  *key,
Packit ae235b
                  GError      **error)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  if (key[0] == '\0')
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("Empty names are not permitted"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (allow_any_name)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!g_ascii_islower (key[0]))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("Invalid name “%s”: names must begin "
Packit ae235b
                     "with a lowercase letter"), key);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 1; key[i]; i++)
Packit ae235b
    {
Packit ae235b
      if (key[i] != '-' &&
Packit ae235b
          !g_ascii_islower (key[i]) &&
Packit ae235b
          !g_ascii_isdigit (key[i]))
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("Invalid name “%s”: invalid character “%c”; "
Packit ae235b
                         "only lowercase letters, numbers and hyphen (“-”) "
Packit ae235b
                         "are permitted"), key, key[i]);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (key[i] == '-' && key[i + 1] == '-')
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("Invalid name “%s”: two successive hyphens (“--”) "
Packit ae235b
                         "are not permitted"), key);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (key[i - 1] == '-')
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("Invalid name “%s”: the last character may not be a "
Packit ae235b
                     "hyphen (“-”)"), key);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (i > 1024)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("Invalid name “%s”: maximum length is 1024"), key);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Handling of <schema> {{{1 */
Packit ae235b
typedef struct _SchemaState SchemaState;
Packit ae235b
struct _SchemaState
Packit ae235b
{
Packit ae235b
  SchemaState *extends;
Packit ae235b
Packit ae235b
  gchar       *path;
Packit ae235b
  gchar       *gettext_domain;
Packit ae235b
  gchar       *extends_name;
Packit ae235b
  gchar       *list_of;
Packit ae235b
Packit ae235b
  GHashTable  *keys;
Packit ae235b
};
Packit ae235b
Packit ae235b
static SchemaState *
Packit ae235b
schema_state_new (const gchar  *path,
Packit ae235b
                  const gchar  *gettext_domain,
Packit ae235b
                  SchemaState  *extends,
Packit ae235b
                  const gchar  *extends_name,
Packit ae235b
                  const gchar  *list_of)
Packit ae235b
{
Packit ae235b
  SchemaState *state;
Packit ae235b
Packit ae235b
  state = g_slice_new (SchemaState);
Packit ae235b
  state->path = g_strdup (path);
Packit ae235b
  state->gettext_domain = g_strdup (gettext_domain);
Packit ae235b
  state->extends = extends;
Packit ae235b
  state->extends_name = g_strdup (extends_name);
Packit ae235b
  state->list_of = g_strdup (list_of);
Packit ae235b
  state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
Packit ae235b
                                       g_free, key_state_free);
Packit ae235b
Packit ae235b
  return state;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schema_state_free (gpointer data)
Packit ae235b
{
Packit ae235b
  SchemaState *state = data;
Packit ae235b
Packit ae235b
  g_free (state->path);
Packit ae235b
  g_free (state->gettext_domain);
Packit ae235b
  g_hash_table_unref (state->keys);
Packit ae235b
  g_slice_free (SchemaState, state);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schema_state_add_child (SchemaState  *state,
Packit ae235b
                        const gchar  *name,
Packit ae235b
                        const gchar  *schema,
Packit ae235b
                        GError      **error)
Packit ae235b
{
Packit ae235b
  gchar *childname;
Packit ae235b
Packit ae235b
  if (!is_valid_keyname (name, error))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  childname = g_strconcat (name, "/", NULL);
Packit ae235b
Packit ae235b
  if (g_hash_table_lookup (state->keys, childname))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<child name='%s'> already specified"), name);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_hash_table_insert (state->keys, childname,
Packit ae235b
                       key_state_new_child (schema));
Packit ae235b
}
Packit ae235b
Packit ae235b
static KeyState *
Packit ae235b
schema_state_add_key (SchemaState  *state,
Packit ae235b
                      GHashTable   *enum_table,
Packit ae235b
                      GHashTable   *flags_table,
Packit ae235b
                      const gchar  *name,
Packit ae235b
                      const gchar  *type_string,
Packit ae235b
                      const gchar  *enum_type,
Packit ae235b
                      const gchar  *flags_type,
Packit ae235b
                      GError      **error)
Packit ae235b
{
Packit ae235b
  SchemaState *node;
Packit ae235b
  GString *strinfo;
Packit ae235b
  KeyState *key;
Packit ae235b
Packit ae235b
  if (state->list_of)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("Cannot add keys to a “list-of” schema"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!is_valid_keyname (name, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (g_hash_table_lookup (state->keys, name))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<key name='%s'> already specified"), name);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (node = state; node; node = node->extends)
Packit ae235b
    if (node->extends)
Packit ae235b
      {
Packit ae235b
        KeyState *shadow;
Packit ae235b
Packit ae235b
        shadow = g_hash_table_lookup (node->extends->keys, name);
Packit ae235b
Packit ae235b
        /* in case of <key> <override> <key> make sure we report the
Packit ae235b
         * location of the original <key>, not the <override>.
Packit ae235b
         */
Packit ae235b
        if (shadow && !shadow->is_override)
Packit ae235b
          {
Packit ae235b
            g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                         G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("<key name='%s'> shadows <key name='%s'> in "
Packit ae235b
                           "<schema id='%s'>; use <override> to modify value"),
Packit ae235b
                         name, name, node->extends_name);
Packit ae235b
            return NULL;
Packit ae235b
          }
Packit ae235b
      }
Packit ae235b
Packit ae235b
  if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_MISSING_ATTRIBUTE,
Packit ae235b
                   _("Exactly one of “type”, “enum” or “flags” must "
Packit ae235b
                     "be specified as an attribute to <key>"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (type_string == NULL) /* flags or enums was specified */
Packit ae235b
    {
Packit ae235b
      EnumState *enum_state;
Packit ae235b
Packit ae235b
      if (enum_type)
Packit ae235b
        enum_state = g_hash_table_lookup (enum_table, enum_type);
Packit ae235b
      else
Packit ae235b
        enum_state = g_hash_table_lookup (flags_table, flags_type);
Packit ae235b
Packit ae235b
Packit ae235b
      if (enum_state == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("<%s id='%s'> not (yet) defined."),
Packit ae235b
                       flags_type ? "flags"    : "enum",
Packit ae235b
                       flags_type ? flags_type : enum_type);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      type_string = flags_type ? "as" : "s";
Packit ae235b
      strinfo = enum_state->strinfo;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (!g_variant_type_string_is_valid (type_string))
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("Invalid GVariant type string “%s”"), type_string);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      strinfo = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  key = key_state_new (type_string, state->gettext_domain,
Packit ae235b
                       enum_type != NULL, flags_type != NULL, strinfo);
Packit ae235b
  g_hash_table_insert (state->keys, g_strdup (name), key);
Packit ae235b
Packit ae235b
  return key;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schema_state_add_override (SchemaState  *state,
Packit ae235b
                           KeyState    **key_state,
Packit ae235b
                           GString     **string,
Packit ae235b
                           const gchar  *key,
Packit ae235b
                           const gchar  *l10n,
Packit ae235b
                           const gchar  *context,
Packit ae235b
                           GError      **error)
Packit ae235b
{
Packit ae235b
  SchemaState *parent;
Packit ae235b
  KeyState *original;
Packit ae235b
Packit ae235b
  if (state->extends == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<override> given but schema isn’t "
Packit ae235b
                             "extending anything"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (parent = state->extends; parent; parent = parent->extends)
Packit ae235b
    if ((original = g_hash_table_lookup (parent->keys, key)))
Packit ae235b
      break;
Packit ae235b
Packit ae235b
  if (original == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("No <key name='%s'> to override"), key);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_hash_table_lookup (state->keys, key))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<override name='%s'> already specified"), key);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *key_state = key_state_override (original, state->gettext_domain);
Packit ae235b
  *string = key_state_start_default (*key_state, l10n, context, error);
Packit ae235b
  g_hash_table_insert (state->keys, g_strdup (key), *key_state);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
override_state_end (KeyState **key_state,
Packit ae235b
                    GString  **string,
Packit ae235b
                    GError   **error)
Packit ae235b
{
Packit ae235b
  key_state_end_default (*key_state, string, error);
Packit ae235b
  *key_state = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Handling of toplevel state {{{1 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gboolean     strict;                  /* TRUE if --strict was given */
Packit ae235b
Packit ae235b
  GHashTable  *schema_table;            /* string -> SchemaState */
Packit ae235b
  GHashTable  *flags_table;             /* string -> EnumState */
Packit ae235b
  GHashTable  *enum_table;              /* string -> EnumState */
Packit ae235b
Packit ae235b
  GSList      *this_file_schemas;       /* strings: <schema>s in this file */
Packit ae235b
  GSList      *this_file_flagss;        /* strings: <flags>s in this file */
Packit ae235b
  GSList      *this_file_enums;         /* strings: <enum>s in this file */
Packit ae235b
Packit ae235b
  gchar       *schemalist_domain;       /* the <schemalist> gettext domain */
Packit ae235b
Packit ae235b
  SchemaState *schema_state;            /* non-NULL when inside <schema> */
Packit ae235b
  KeyState    *key_state;               /* non-NULL when inside <key> */
Packit ae235b
  EnumState   *enum_state;              /* non-NULL when inside <enum> */
Packit ae235b
Packit ae235b
  GString     *string;                  /* non-NULL when accepting text */
Packit ae235b
} ParseState;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_subclass (const gchar *class_name,
Packit ae235b
             const gchar *possible_parent,
Packit ae235b
             GHashTable  *schema_table)
Packit ae235b
{
Packit ae235b
  SchemaState *class;
Packit ae235b
Packit ae235b
  if (strcmp (class_name, possible_parent) == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  class = g_hash_table_lookup (schema_table, class_name);
Packit ae235b
  g_assert (class != NULL);
Packit ae235b
Packit ae235b
  return class->extends_name &&
Packit ae235b
         is_subclass (class->extends_name, possible_parent, schema_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
parse_state_start_schema (ParseState  *state,
Packit ae235b
                          const gchar  *id,
Packit ae235b
                          const gchar  *path,
Packit ae235b
                          const gchar  *gettext_domain,
Packit ae235b
                          const gchar  *extends_name,
Packit ae235b
                          const gchar  *list_of,
Packit ae235b
                          GError      **error)
Packit ae235b
{
Packit ae235b
  SchemaState *extends;
Packit ae235b
  gchar *my_id;
Packit ae235b
Packit ae235b
  if (g_hash_table_lookup (state->schema_table, id))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<schema id='%s'> already specified"), id);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (extends_name)
Packit ae235b
    {
Packit ae235b
      extends = g_hash_table_lookup (state->schema_table, extends_name);
Packit ae235b
Packit ae235b
      if (extends == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("<schema id='%s'> extends not yet existing "
Packit ae235b
                         "schema “%s”"), id, extends_name);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    extends = NULL;
Packit ae235b
Packit ae235b
  if (list_of)
Packit ae235b
    {
Packit ae235b
      SchemaState *tmp;
Packit ae235b
Packit ae235b
      if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                       G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("<schema id='%s'> is list of not yet existing "
Packit ae235b
                         "schema “%s”"), id, list_of);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (tmp->path)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("Cannot be a list of a schema with a path"));
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (extends)
Packit ae235b
    {
Packit ae235b
      if (extends->path)
Packit ae235b
        {
Packit ae235b
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                       _("Cannot extend a schema with a path"));
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (list_of)
Packit ae235b
        {
Packit ae235b
          if (extends->list_of == NULL)
Packit ae235b
            {
Packit ae235b
              g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<schema id='%s'> is a list, extending "
Packit ae235b
                             "<schema id='%s'> which is not a list"),
Packit ae235b
                           id, extends_name);
Packit ae235b
              return;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (!is_subclass (list_of, extends->list_of, state->schema_table))
Packit ae235b
            {
Packit ae235b
              g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                           G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("<schema id='%s' list-of='%s'> extends 
Packit ae235b
                             "id='%s' list-of='%s'> but “%s” does not "
Packit ae235b
                             "extend “%s”"), id, list_of, extends_name,
Packit ae235b
                           extends->list_of, list_of, extends->list_of);
Packit ae235b
              return;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        /* by default we are a list of the same thing that the schema
Packit ae235b
         * we are extending is a list of (which might be nothing)
Packit ae235b
         */
Packit ae235b
        list_of = extends->list_of;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("A path, if given, must begin and end with a slash"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (path && list_of && !g_str_has_suffix (path, ":/"))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("The path of a list must end with “:/”"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (path && (g_str_has_prefix (path, "/apps/") ||
Packit ae235b
               g_str_has_prefix (path, "/desktop/") ||
Packit ae235b
               g_str_has_prefix (path, "/system/")))
Packit ae235b
    {
Packit ae235b
      gchar *message = NULL;
Packit ae235b
      message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”.  "
Packit ae235b
                                   "Paths starting with "
Packit ae235b
                                   "“/apps/”, “/desktop/” or “/system/” are deprecated."),
Packit ae235b
                                 id, path);
Packit ae235b
      g_printerr ("%s\n", message);
Packit ae235b
      g_free (message);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  state->schema_state = schema_state_new (path, gettext_domain,
Packit ae235b
                                          extends, extends_name, list_of);
Packit ae235b
Packit ae235b
  my_id = g_strdup (id);
Packit ae235b
  state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
Packit ae235b
  g_hash_table_insert (state->schema_table, my_id, state->schema_state);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
parse_state_start_enum (ParseState   *state,
Packit ae235b
                        const gchar  *id,
Packit ae235b
                        gboolean      is_flags,
Packit ae235b
                        GError      **error)
Packit ae235b
{
Packit ae235b
  GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
Packit ae235b
  GHashTable *table = is_flags ? state->flags_table : state->enum_table;
Packit ae235b
  gchar *my_id;
Packit ae235b
Packit ae235b
  if (g_hash_table_lookup (table, id))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_MARKUP_ERROR,
Packit ae235b
                   G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                   _("<%s id='%s'> already specified"),
Packit ae235b
                   is_flags ? "flags" : "enum", id);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  state->enum_state = enum_state_new (is_flags);
Packit ae235b
Packit ae235b
  my_id = g_strdup (id);
Packit ae235b
  *list = g_slist_prepend (*list, my_id);
Packit ae235b
  g_hash_table_insert (table, my_id, state->enum_state);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GMarkup Parser Functions {{{1 */
Packit ae235b
Packit ae235b
/* Start element {{{2 */
Packit ae235b
static void
Packit ae235b
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
  ParseState *state = user_data;
Packit ae235b
  const GSList *element_stack;
Packit ae235b
  const gchar *container;
Packit ae235b
Packit ae235b
  element_stack = g_markup_parse_context_get_element_stack (context);
Packit ae235b
  container = element_stack->next ? element_stack->next->data : NULL;
Packit ae235b
Packit ae235b
#define COLLECT(first, ...) \
Packit ae235b
  g_markup_collect_attributes (element_name,                                 \
Packit ae235b
                               attribute_names, attribute_values, error,     \
Packit ae235b
                               first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
Packit ae235b
#define OPTIONAL   G_MARKUP_COLLECT_OPTIONAL
Packit ae235b
#define STRDUP     G_MARKUP_COLLECT_STRDUP
Packit ae235b
#define STRING     G_MARKUP_COLLECT_STRING
Packit ae235b
#define NO_ATTRS()  COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
Packit ae235b
Packit ae235b
  /* Toplevel items {{{3 */
Packit ae235b
  if (container == NULL)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "schemalist") == 0)
Packit ae235b
        {
Packit ae235b
          COLLECT (OPTIONAL | STRDUP,
Packit ae235b
                   "gettext-domain",
Packit ae235b
                   &state->schemalist_domain);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  /* children of <schemalist> {{{3 */
Packit ae235b
  else if (strcmp (container, "schemalist") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "schema") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *id, *path, *gettext_domain, *extends, *list_of;
Packit ae235b
          if (COLLECT (STRING, "id", &id,
Packit ae235b
                       OPTIONAL | STRING, "path", &path,
Packit ae235b
                       OPTIONAL | STRING, "gettext-domain", &gettext_domain,
Packit ae235b
                       OPTIONAL | STRING, "extends", &extends,
Packit ae235b
                       OPTIONAL | STRING, "list-of", &list_of))
Packit ae235b
            parse_state_start_schema (state, id, path,
Packit ae235b
                                      gettext_domain ? gettext_domain
Packit ae235b
                                                     : state->schemalist_domain,
Packit ae235b
                                      extends, list_of, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "enum") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *id;
Packit ae235b
          if (COLLECT (STRING, "id", &id))
Packit ae235b
            parse_state_start_enum (state, id, FALSE, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "flags") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *id;
Packit ae235b
          if (COLLECT (STRING, "id", &id))
Packit ae235b
            parse_state_start_enum (state, id, TRUE, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  /* children of <schema> {{{3 */
Packit ae235b
  else if (strcmp (container, "schema") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "key") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *name, *type_string, *enum_type, *flags_type;
Packit ae235b
Packit ae235b
          if (COLLECT (STRING,            "name",  &name,
Packit ae235b
                       OPTIONAL | STRING, "type",  &type_string,
Packit ae235b
                       OPTIONAL | STRING, "enum",  &enum_type,
Packit ae235b
                       OPTIONAL | STRING, "flags", &flags_type))
Packit ae235b
Packit ae235b
            state->key_state = schema_state_add_key (state->schema_state,
Packit ae235b
                                                     state->enum_table,
Packit ae235b
                                                     state->flags_table,
Packit ae235b
                                                     name, type_string,
Packit ae235b
                                                     enum_type, flags_type,
Packit ae235b
                                                     error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (element_name, "child") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *name, *schema;
Packit ae235b
Packit ae235b
          if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
Packit ae235b
            schema_state_add_child (state->schema_state,
Packit ae235b
                                    name, schema, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (element_name, "override") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *name, *l10n, *context;
Packit ae235b
Packit ae235b
          if (COLLECT (STRING,            "name",    &name,
Packit ae235b
                       OPTIONAL | STRING, "l10n",    &l10n,
Packit ae235b
                       OPTIONAL | STRING, "context", &context))
Packit ae235b
            schema_state_add_override (state->schema_state,
Packit ae235b
                                       &state->key_state, &state->string,
Packit ae235b
                                       name, l10n, context, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* children of <key> {{{3 */
Packit ae235b
  else if (strcmp (container, "key") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "default") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *l10n, *context;
Packit ae235b
          if (COLLECT (STRING | OPTIONAL, "l10n",    &l10n,
Packit ae235b
                       STRING | OPTIONAL, "context", &context))
Packit ae235b
            state->string = key_state_start_default (state->key_state,
Packit ae235b
                                                     l10n, context, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "summary") == 0)
Packit ae235b
        {
Packit ae235b
          if (NO_ATTRS ())
Packit ae235b
            {
Packit ae235b
              if (state->key_state->summary_seen && state->strict)
Packit ae235b
                g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                             _("Only one <%s> element allowed inside <%s>"),
Packit ae235b
                             element_name, container);
Packit ae235b
              else
Packit ae235b
                state->string = g_string_new (NULL);
Packit ae235b
Packit ae235b
              state->key_state->summary_seen = TRUE;
Packit ae235b
            }
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "description") == 0)
Packit ae235b
        {
Packit ae235b
          if (NO_ATTRS ())
Packit ae235b
            {
Packit ae235b
              if (state->key_state->description_seen && state->strict)
Packit ae235b
                g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                             _("Only one <%s> element allowed inside <%s>"),
Packit ae235b
                             element_name, container);
Packit ae235b
              else
Packit ae235b
                state->string = g_string_new (NULL);
Packit ae235b
Packit ae235b
            state->key_state->description_seen = TRUE;
Packit ae235b
            }
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "range") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *min, *max;
Packit ae235b
          if (COLLECT (STRING | OPTIONAL, "min", &min,
Packit ae235b
                       STRING | OPTIONAL, "max", &max))
Packit ae235b
            key_state_set_range (state->key_state, min, max, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "choices") == 0)
Packit ae235b
        {
Packit ae235b
          if (NO_ATTRS ())
Packit ae235b
            key_state_start_choices (state->key_state, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (strcmp (element_name, "aliases") == 0)
Packit ae235b
        {
Packit ae235b
          if (NO_ATTRS ())
Packit ae235b
            key_state_start_aliases (state->key_state, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  /* children of <choices> {{{3 */
Packit ae235b
  else if (strcmp (container, "choices") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "choice") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *value;
Packit ae235b
          if (COLLECT (STRING, "value", &value))
Packit ae235b
            key_state_add_choice (state->key_state, value, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  /* children of <aliases> {{{3 */
Packit ae235b
  else if (strcmp (container, "aliases") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "alias") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *value, *target;
Packit ae235b
          if (COLLECT (STRING, "value", &value, STRING, "target", &target))
Packit ae235b
            key_state_add_alias (state->key_state, value, target, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  /* children of <enum> {{{3 */
Packit ae235b
  else if (strcmp (container, "enum") == 0 ||
Packit ae235b
           strcmp (container, "flags") == 0)
Packit ae235b
    {
Packit ae235b
      if (strcmp (element_name, "value") == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *nick, *valuestr;
Packit ae235b
          if (COLLECT (STRING, "nick", &nick,
Packit ae235b
                       STRING, "value", &valuestr))
Packit ae235b
            enum_state_add_value (state->enum_state, nick, valuestr, error);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  /* 3}}} */
Packit ae235b
Packit ae235b
  if (container)
Packit ae235b
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
Packit ae235b
                 _("Element <%s> not allowed inside <%s>"),
Packit ae235b
                 element_name, container);
Packit ae235b
  else
Packit ae235b
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
Packit ae235b
                 _("Element <%s> not allowed at the top level"), element_name);
Packit ae235b
}
Packit ae235b
/* 2}}} */
Packit ae235b
/* End element {{{2 */
Packit ae235b
Packit ae235b
static void
Packit ae235b
key_state_end (KeyState **state_ptr,
Packit ae235b
               GError   **error)
Packit ae235b
{
Packit ae235b
  KeyState *state;
Packit ae235b
Packit ae235b
  state = *state_ptr;
Packit ae235b
  *state_ptr = NULL;
Packit ae235b
Packit ae235b
  if (state->default_value == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                           _("Element <default> is required in <key>"));
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schema_state_end (SchemaState **state_ptr,
Packit ae235b
                  GError      **error)
Packit ae235b
{
Packit ae235b
  *state_ptr = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
end_element (GMarkupParseContext  *context,
Packit ae235b
             const gchar          *element_name,
Packit ae235b
             gpointer              user_data,
Packit ae235b
             GError              **error)
Packit ae235b
{
Packit ae235b
  ParseState *state = user_data;
Packit ae235b
Packit ae235b
  if (strcmp (element_name, "schemalist") == 0)
Packit ae235b
    {
Packit ae235b
      g_free (state->schemalist_domain);
Packit ae235b
      state->schemalist_domain = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "enum") == 0 ||
Packit ae235b
           strcmp (element_name, "flags") == 0)
Packit ae235b
    enum_state_end (&state->enum_state, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "schema") == 0)
Packit ae235b
    schema_state_end (&state->schema_state, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "override") == 0)
Packit ae235b
    override_state_end (&state->key_state, &state->string, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "key") == 0)
Packit ae235b
    key_state_end (&state->key_state, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "default") == 0)
Packit ae235b
    key_state_end_default (state->key_state, &state->string, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "choices") == 0)
Packit ae235b
    key_state_end_choices (state->key_state, error);
Packit ae235b
Packit ae235b
  else if (strcmp (element_name, "aliases") == 0)
Packit ae235b
    key_state_end_aliases (state->key_state, error);
Packit ae235b
Packit ae235b
  if (state->string)
Packit ae235b
    {
Packit ae235b
      g_string_free (state->string, TRUE);
Packit ae235b
      state->string = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
/* Text {{{2 */
Packit ae235b
static void
Packit ae235b
text (GMarkupParseContext  *context,
Packit ae235b
      const gchar          *text,
Packit ae235b
      gsize                 text_len,
Packit ae235b
      gpointer              user_data,
Packit ae235b
      GError              **error)
Packit ae235b
{
Packit ae235b
  ParseState *state = user_data;
Packit ae235b
Packit ae235b
  if (state->string)
Packit ae235b
    {
Packit ae235b
      /* we are expecting a string, so store the text data.
Packit ae235b
       *
Packit ae235b
       * we store the data verbatim here and deal with whitespace
Packit ae235b
       * later on.  there are two reasons for that:
Packit ae235b
       *
Packit ae235b
       *  1) whitespace is handled differently depending on the tag
Packit ae235b
       *     type.
Packit ae235b
       *
Packit ae235b
       *  2) we could do leading whitespace removal by refusing to
Packit ae235b
       *     insert it into state->string if it's at the start, but for
Packit ae235b
       *     trailing whitespace, we have no idea if there is another
Packit ae235b
       *     text() call coming or not.
Packit ae235b
       */
Packit ae235b
      g_string_append_len (state->string, text, text_len);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* string is not expected: accept (and ignore) pure whitespace */
Packit ae235b
      gsize i;
Packit ae235b
Packit ae235b
      for (i = 0; i < text_len; i++)
Packit ae235b
        if (!g_ascii_isspace (text[i]))
Packit ae235b
          {
Packit ae235b
            g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
Packit ae235b
                         _("Text may not appear inside <%s>"),
Packit ae235b
                         g_markup_parse_context_get_element (context));
Packit ae235b
            break;
Packit ae235b
          }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Write to GVDB {{{1 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable *table;
Packit ae235b
  GvdbItem *root;
Packit ae235b
} GvdbPair;
Packit ae235b
Packit ae235b
static void
Packit ae235b
gvdb_pair_init (GvdbPair *pair)
Packit ae235b
{
Packit ae235b
  pair->table = gvdb_hash_table_new (NULL, NULL);
Packit ae235b
  pair->root = gvdb_hash_table_insert (pair->table, "");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gvdb_pair_clear (GvdbPair *pair)
Packit ae235b
{
Packit ae235b
  g_hash_table_unref (pair->table);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable *schema_table;
Packit ae235b
  GvdbPair root_pair;
Packit ae235b
} WriteToFileData;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable *schema_table;
Packit ae235b
  GvdbPair pair;
Packit ae235b
  gboolean l10n;
Packit ae235b
} OutputSchemaData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
output_key (gpointer key,
Packit ae235b
            gpointer value,
Packit ae235b
            gpointer user_data)
Packit ae235b
{
Packit ae235b
  OutputSchemaData *data;
Packit ae235b
  const gchar *name;
Packit ae235b
  KeyState *state;
Packit ae235b
  GvdbItem *item;
Packit ae235b
  GVariant *serialised = NULL;
Packit ae235b
Packit ae235b
  name = key;
Packit ae235b
  state = value;
Packit ae235b
  data = user_data;
Packit ae235b
Packit ae235b
  item = gvdb_hash_table_insert (data->pair.table, name);
Packit ae235b
  gvdb_item_set_parent (item, data->pair.root);
Packit ae235b
  serialised = key_state_serialise (state);
Packit ae235b
  gvdb_item_set_value (item, serialised);
Packit ae235b
  g_variant_unref (serialised);
Packit ae235b
Packit ae235b
  if (state->l10n)
Packit ae235b
    data->l10n = TRUE;
Packit ae235b
Packit ae235b
  if (state->child_schema &&
Packit ae235b
      !g_hash_table_lookup (data->schema_table, state->child_schema))
Packit ae235b
    {
Packit ae235b
      gchar *message = NULL;
Packit ae235b
      message = g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
Packit ae235b
                                 state->child_schema);
Packit ae235b
      g_printerr ("%s\n", message);
Packit ae235b
      g_free (message);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
output_schema (gpointer key,
Packit ae235b
               gpointer value,
Packit ae235b
               gpointer user_data)
Packit ae235b
{
Packit ae235b
  WriteToFileData *wtf_data = user_data;
Packit ae235b
  OutputSchemaData data;
Packit ae235b
  GvdbPair *root_pair;
Packit ae235b
  SchemaState *state;
Packit ae235b
  const gchar *id;
Packit ae235b
  GvdbItem *item;
Packit ae235b
Packit ae235b
  id = key;
Packit ae235b
  state = value;
Packit ae235b
  root_pair = &wtf_data->root_pair;
Packit ae235b
Packit ae235b
  data.schema_table = wtf_data->schema_table;
Packit ae235b
  gvdb_pair_init (&data.pair);
Packit ae235b
  data.l10n = FALSE;
Packit ae235b
Packit ae235b
  item = gvdb_hash_table_insert (root_pair->table, id);
Packit ae235b
  gvdb_item_set_parent (item, root_pair->root);
Packit ae235b
  gvdb_item_set_hash_table (item, data.pair.table);
Packit ae235b
Packit ae235b
  g_hash_table_foreach (state->keys, output_key, &data);
Packit ae235b
Packit ae235b
  if (state->path)
Packit ae235b
    gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
Packit ae235b
Packit ae235b
  if (state->extends_name)
Packit ae235b
    gvdb_hash_table_insert_string (data.pair.table, ".extends",
Packit ae235b
                                   state->extends_name);
Packit ae235b
Packit ae235b
  if (state->list_of)
Packit ae235b
    gvdb_hash_table_insert_string (data.pair.table, ".list-of",
Packit ae235b
                                   state->list_of);
Packit ae235b
Packit ae235b
  if (data.l10n)
Packit ae235b
    gvdb_hash_table_insert_string (data.pair.table,
Packit ae235b
                                   ".gettext-domain",
Packit ae235b
                                   state->gettext_domain);
Packit ae235b
Packit ae235b
  gvdb_pair_clear (&data.pair);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
write_to_file (GHashTable   *schema_table,
Packit ae235b
               const gchar  *filename,
Packit ae235b
               GError      **error)
Packit ae235b
{
Packit ae235b
  WriteToFileData data;
Packit ae235b
  gboolean success;
Packit ae235b
Packit ae235b
  data.schema_table = schema_table;
Packit ae235b
Packit ae235b
  gvdb_pair_init (&data.root_pair);
Packit ae235b
Packit ae235b
  g_hash_table_foreach (schema_table, output_schema, &data);
Packit ae235b
Packit ae235b
  success = gvdb_table_write_contents (data.root_pair.table, filename,
Packit ae235b
                                       G_BYTE_ORDER != G_LITTLE_ENDIAN,
Packit ae235b
                                       error);
Packit ae235b
  g_hash_table_unref (data.root_pair.table);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Parser driver {{{1 */
Packit ae235b
static GHashTable *
Packit ae235b
parse_gschema_files (gchar    **files,
Packit ae235b
                     gboolean   strict)
Packit ae235b
{
Packit ae235b
  GMarkupParser parser = { start_element, end_element, text };
Packit ae235b
  ParseState state = { 0, };
Packit ae235b
  const gchar *filename;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  state.strict = strict;
Packit ae235b
Packit ae235b
  state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
Packit ae235b
                                            g_free, enum_state_free);
Packit ae235b
Packit ae235b
  state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
Packit ae235b
                                             g_free, enum_state_free);
Packit ae235b
Packit ae235b
  state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
Packit ae235b
                                              g_free, schema_state_free);
Packit ae235b
Packit ae235b
  while ((filename = *files++) != NULL)
Packit ae235b
    {
Packit ae235b
      GMarkupParseContext *context;
Packit ae235b
      gchar *contents;
Packit ae235b
      gsize size;
Packit ae235b
      gint line, col;
Packit ae235b
Packit ae235b
      if (!g_file_get_contents (filename, &contents, &size, &error))
Packit ae235b
        {
Packit ae235b
          fprintf (stderr, "%s\n", error->message);
Packit ae235b
          g_clear_error (&error);
Packit ae235b
          continue;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      context = g_markup_parse_context_new (&parser,
Packit ae235b
                                            G_MARKUP_TREAT_CDATA_AS_TEXT |
Packit ae235b
                                            G_MARKUP_PREFIX_ERROR_POSITION |
Packit ae235b
                                            G_MARKUP_IGNORE_QUALIFIED,
Packit ae235b
                                            &state, NULL);
Packit ae235b
Packit ae235b
Packit ae235b
      if (!g_markup_parse_context_parse (context, contents, size, &error) ||
Packit ae235b
          !g_markup_parse_context_end_parse (context, &error))
Packit ae235b
        {
Packit ae235b
          GSList *item;
Packit ae235b
Packit ae235b
          /* back out any changes from this file */
Packit ae235b
          for (item = state.this_file_schemas; item; item = item->next)
Packit ae235b
            g_hash_table_remove (state.schema_table, item->data);
Packit ae235b
Packit ae235b
          for (item = state.this_file_flagss; item; item = item->next)
Packit ae235b
            g_hash_table_remove (state.flags_table, item->data);
Packit ae235b
Packit ae235b
          for (item = state.this_file_enums; item; item = item->next)
Packit ae235b
            g_hash_table_remove (state.enum_table, item->data);
Packit ae235b
Packit ae235b
          /* let them know */
Packit ae235b
          g_markup_parse_context_get_position (context, &line, &col);
Packit ae235b
          fprintf (stderr, "%s:%d:%d  %s.  ", filename, line, col, error->message);
Packit ae235b
          g_clear_error (&error);
Packit ae235b
Packit ae235b
          if (strict)
Packit ae235b
            {
Packit ae235b
              /* Translators: Do not translate "--strict". */
Packit ae235b
              fprintf (stderr, _("--strict was specified; exiting.\n"));
Packit ae235b
              g_hash_table_unref (state.schema_table);
Packit ae235b
              g_hash_table_unref (state.flags_table);
Packit ae235b
              g_hash_table_unref (state.enum_table);
Packit ae235b
Packit ae235b
              g_free (contents);
Packit ae235b
Packit ae235b
              return NULL;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            fprintf (stderr, _("This entire file has been ignored.\n"));
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* cleanup */
Packit ae235b
      g_free (contents);
Packit ae235b
      g_markup_parse_context_free (context);
Packit ae235b
      g_slist_free (state.this_file_schemas);
Packit ae235b
      g_slist_free (state.this_file_flagss);
Packit ae235b
      g_slist_free (state.this_file_enums);
Packit ae235b
      state.this_file_schemas = NULL;
Packit ae235b
      state.this_file_flagss = NULL;
Packit ae235b
      state.this_file_enums = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_hash_table_unref (state.flags_table);
Packit ae235b
  g_hash_table_unref (state.enum_table);
Packit ae235b
Packit ae235b
  return state.schema_table;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
compare_strings (gconstpointer a,
Packit ae235b
                 gconstpointer b)
Packit ae235b
{
Packit ae235b
  gchar *one = *(gchar **) a;
Packit ae235b
  gchar *two = *(gchar **) b;
Packit ae235b
  gint cmp;
Packit ae235b
Packit ae235b
  cmp = g_str_has_suffix (two, ".enums.xml") -
Packit ae235b
        g_str_has_suffix (one, ".enums.xml");
Packit ae235b
Packit ae235b
  if (!cmp)
Packit ae235b
    cmp = strcmp (one, two);
Packit ae235b
Packit ae235b
  return cmp;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
set_overrides (GHashTable  *schema_table,
Packit ae235b
               gchar      **files,
Packit ae235b
               gboolean     strict)
Packit ae235b
{
Packit ae235b
  const gchar *filename;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  while ((filename = *files++))
Packit ae235b
    {
Packit ae235b
      GKeyFile *key_file;
Packit ae235b
      gchar **groups;
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      key_file = g_key_file_new ();
Packit ae235b
      if (!g_key_file_load_from_file (key_file, filename, 0, &error))
Packit ae235b
        {
Packit ae235b
          fprintf (stderr, "%s: %s.  ", filename, error->message);
Packit ae235b
          g_key_file_free (key_file);
Packit ae235b
          g_clear_error (&error);
Packit ae235b
Packit ae235b
          if (!strict)
Packit ae235b
            {
Packit ae235b
              fprintf (stderr, _("Ignoring this file.\n"));
Packit ae235b
              continue;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          fprintf (stderr, _("--strict was specified; exiting.\n"));
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      groups = g_key_file_get_groups (key_file, NULL);
Packit ae235b
Packit ae235b
      for (i = 0; groups[i]; i++)
Packit ae235b
        {
Packit ae235b
          const gchar *group = groups[i];
Packit ae235b
          SchemaState *schema;
Packit ae235b
          gchar **keys;
Packit ae235b
          gint j;
Packit ae235b
Packit ae235b
          schema = g_hash_table_lookup (schema_table, group);
Packit ae235b
Packit ae235b
          if (schema == NULL)
Packit ae235b
            /* Having the schema not be installed is expected to be a
Packit ae235b
             * common case.  Don't even emit an error message about
Packit ae235b
             * that.
Packit ae235b
             */
Packit ae235b
            continue;
Packit ae235b
Packit ae235b
          keys = g_key_file_get_keys (key_file, group, NULL, NULL);
Packit ae235b
          g_assert (keys != NULL);
Packit ae235b
Packit ae235b
          for (j = 0; keys[j]; j++)
Packit ae235b
            {
Packit ae235b
              const gchar *key = keys[j];
Packit ae235b
              KeyState *state;
Packit ae235b
              GVariant *value;
Packit ae235b
              gchar *string;
Packit ae235b
Packit ae235b
              state = g_hash_table_lookup (schema->keys, key);
Packit ae235b
Packit ae235b
              if (state == NULL)
Packit ae235b
                {
Packit ae235b
                  fprintf (stderr, _("No such key '%s' in schema '%s' as "
Packit ae235b
                                     "specified in override file '%s'"),
Packit ae235b
                           key, group, filename);
Packit ae235b
Packit ae235b
                  if (!strict)
Packit ae235b
                    {
Packit ae235b
                      fprintf (stderr, _("; ignoring override for this key.\n"));
Packit ae235b
                      continue;
Packit ae235b
                    }
Packit ae235b
Packit ae235b
                  fprintf (stderr, _(" and --strict was specified; exiting.\n"));
Packit ae235b
                  g_key_file_free (key_file);
Packit ae235b
                  g_strfreev (groups);
Packit ae235b
                  g_strfreev (keys);
Packit ae235b
Packit ae235b
                  return FALSE;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              string = g_key_file_get_value (key_file, group, key, NULL);
Packit ae235b
              g_assert (string != NULL);
Packit ae235b
Packit ae235b
              value = g_variant_parse (state->type, string,
Packit ae235b
                                       NULL, NULL, &error);
Packit ae235b
Packit ae235b
              if (value == NULL)
Packit ae235b
                {
Packit ae235b
                  fprintf (stderr, _("error parsing key '%s' in schema '%s' "
Packit ae235b
                                     "as specified in override file '%s': "
Packit ae235b
                                     "%s."),
Packit ae235b
                           key, group, filename, error->message);
Packit ae235b
Packit ae235b
                  g_clear_error (&error);
Packit ae235b
                  g_free (string);
Packit ae235b
Packit ae235b
                  if (!strict)
Packit ae235b
                    {
Packit ae235b
                      fprintf (stderr, _("Ignoring override for this key.\n"));
Packit ae235b
                      continue;
Packit ae235b
                    }
Packit ae235b
Packit ae235b
                  fprintf (stderr, _("--strict was specified; exiting.\n"));
Packit ae235b
                  g_key_file_free (key_file);
Packit ae235b
                  g_strfreev (groups);
Packit ae235b
                  g_strfreev (keys);
Packit ae235b
Packit ae235b
                  return FALSE;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              if (state->minimum)
Packit ae235b
                {
Packit ae235b
                  if (g_variant_compare (value, state->minimum) < 0 ||
Packit ae235b
                      g_variant_compare (value, state->maximum) > 0)
Packit ae235b
                    {
Packit ae235b
                      fprintf (stderr,
Packit ae235b
                               _("override for key '%s' in schema '%s' in "
Packit ae235b
                                 "override file '%s' is outside the range "
Packit ae235b
                                 "given in the schema"),
Packit ae235b
                               key, group, filename);
Packit ae235b
Packit ae235b
                      g_variant_unref (value);
Packit ae235b
                      g_free (string);
Packit ae235b
Packit ae235b
                      if (!strict)
Packit ae235b
                        {
Packit ae235b
                          fprintf (stderr, _("; ignoring override for this key.\n"));
Packit ae235b
                          continue;
Packit ae235b
                        }
Packit ae235b
Packit ae235b
                      fprintf (stderr, _(" and --strict was specified; exiting.\n"));
Packit ae235b
                      g_key_file_free (key_file);
Packit ae235b
                      g_strfreev (groups);
Packit ae235b
                      g_strfreev (keys);
Packit ae235b
Packit ae235b
                      return FALSE;
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
Packit ae235b
              else if (state->strinfo->len)
Packit ae235b
                {
Packit ae235b
                  if (!is_valid_choices (value, state->strinfo))
Packit ae235b
                    {
Packit ae235b
                      fprintf (stderr,
Packit ae235b
                               _("override for key '%s' in schema '%s' in "
Packit ae235b
                                 "override file '%s' is not in the list "
Packit ae235b
                                 "of valid choices"),
Packit ae235b
                               key, group, filename);
Packit ae235b
Packit ae235b
                      g_variant_unref (value);
Packit ae235b
                      g_free (string);
Packit ae235b
Packit ae235b
                      if (!strict)
Packit ae235b
                        {
Packit ae235b
                          fprintf (stderr, _("; ignoring override for this key.\n"));
Packit ae235b
                          continue;
Packit ae235b
                        }
Packit ae235b
Packit ae235b
                      fprintf (stderr, _(" and --strict was specified; exiting.\n"));
Packit ae235b
                      g_key_file_free (key_file);
Packit ae235b
                      g_strfreev (groups);
Packit ae235b
                      g_strfreev (keys);
Packit ae235b
Packit ae235b
                      return FALSE;
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
Packit ae235b
              g_variant_unref (state->default_value);
Packit ae235b
              state->default_value = value;
Packit ae235b
              g_free (string);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_strfreev (keys);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_strfreev (groups);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char **argv)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GHashTable *table = NULL;
Packit ae235b
  GDir *dir = NULL;
Packit ae235b
  const gchar *file;
Packit ae235b
  const gchar *srcdir;
Packit ae235b
  gboolean show_version_and_exit = FALSE;
Packit ae235b
  gchar *targetdir = NULL;
Packit ae235b
  gchar *target = NULL;
Packit ae235b
  gboolean dry_run = FALSE;
Packit ae235b
  gboolean strict = FALSE;
Packit ae235b
  gchar **schema_files = NULL;
Packit ae235b
  gchar **override_files = NULL;
Packit ae235b
  GOptionContext *context = NULL;
Packit ae235b
  gint retval;
Packit ae235b
  GOptionEntry entries[] = {
Packit ae235b
    { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
Packit ae235b
    { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
Packit ae235b
    { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
Packit ae235b
    { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
Packit ae235b
    { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
Packit ae235b
Packit ae235b
    /* These options are only for use in the gschema-compile tests */
Packit ae235b
    { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
Packit ae235b
    { NULL }
Packit ae235b
  };
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gchar *tmp = NULL;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  setlocale (LC_ALL, "");
Packit ae235b
  textdomain (GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  tmp = _glib_get_locale_dir ();
Packit ae235b
  bindtextdomain (GETTEXT_PACKAGE, tmp);
Packit ae235b
#else
Packit ae235b
  bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
Packit ae235b
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  context = g_option_context_new (N_("DIRECTORY"));
Packit ae235b
  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
    N_("Compile all GSettings schema files into a schema cache.\n"
Packit ae235b
       "Schema files are required to have the extension .gschema.xml,\n"
Packit ae235b
       "and the cache file is called gschemas.compiled."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "%s\n", error->message);
Packit ae235b
      retval = 1;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (show_version_and_exit)
Packit ae235b
    {
Packit ae235b
      g_print (PACKAGE_VERSION "\n");
Packit ae235b
      retval = 0;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!schema_files && argc != 2)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, _("You should give exactly one directory name\n"));
Packit ae235b
      retval = 1;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  srcdir = argv[1];
Packit ae235b
Packit ae235b
  target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
Packit ae235b
Packit ae235b
  if (!schema_files)
Packit ae235b
    {
Packit ae235b
      GPtrArray *overrides;
Packit ae235b
      GPtrArray *files;
Packit ae235b
Packit ae235b
      files = g_ptr_array_new ();
Packit ae235b
      overrides = g_ptr_array_new ();
Packit ae235b
Packit ae235b
      dir = g_dir_open (srcdir, 0, &error);
Packit ae235b
      if (dir == NULL)
Packit ae235b
        {
Packit ae235b
          fprintf (stderr, "%s\n", error->message);
Packit ae235b
Packit ae235b
          g_ptr_array_unref (files);
Packit ae235b
          g_ptr_array_unref (overrides);
Packit ae235b
Packit ae235b
          retval = 1;
Packit ae235b
          goto done;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      while ((file = g_dir_read_name (dir)) != NULL)
Packit ae235b
        {
Packit ae235b
          if (g_str_has_suffix (file, ".gschema.xml") ||
Packit ae235b
              g_str_has_suffix (file, ".enums.xml"))
Packit ae235b
            g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
Packit ae235b
Packit ae235b
          else if (g_str_has_suffix (file, ".gschema.override"))
Packit ae235b
            g_ptr_array_add (overrides,
Packit ae235b
                             g_build_filename (srcdir, file, NULL));
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (files->len == 0)
Packit ae235b
        {
Packit ae235b
          fprintf (stdout, _("No schema files found: "));
Packit ae235b
Packit ae235b
          if (g_unlink (target))
Packit ae235b
            fprintf (stdout, _("doing nothing.\n"));
Packit ae235b
Packit ae235b
          else
Packit ae235b
            fprintf (stdout, _("removed existing output file.\n"));
Packit ae235b
Packit ae235b
          g_ptr_array_unref (files);
Packit ae235b
          g_ptr_array_unref (overrides);
Packit ae235b
Packit ae235b
          retval = 0;
Packit ae235b
          goto done;
Packit ae235b
        }
Packit ae235b
      g_ptr_array_sort (files, compare_strings);
Packit ae235b
      g_ptr_array_add (files, NULL);
Packit ae235b
Packit ae235b
      g_ptr_array_sort (overrides, compare_strings);
Packit ae235b
      g_ptr_array_add (overrides, NULL);
Packit ae235b
Packit ae235b
      schema_files = (char **) g_ptr_array_free (files, FALSE);
Packit ae235b
      override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((table = parse_gschema_files (schema_files, strict)) == NULL)
Packit ae235b
    {
Packit ae235b
      retval = 1;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (override_files != NULL &&
Packit ae235b
      !set_overrides (table, override_files, strict))
Packit ae235b
    {
Packit ae235b
      retval = 1;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!dry_run && !write_to_file (table, target, &error))
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "%s\n", error->message);
Packit ae235b
      retval = 1;
Packit ae235b
      goto done;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Success. */
Packit ae235b
  retval = 0;
Packit ae235b
Packit ae235b
done:
Packit ae235b
  g_clear_error (&error);
Packit ae235b
  g_clear_pointer (&table, g_hash_table_unref);
Packit ae235b
  g_clear_pointer (&dir, g_dir_close);
Packit ae235b
  g_free (targetdir);
Packit ae235b
  g_free (target);
Packit ae235b
  g_strfreev (schema_files);
Packit ae235b
  g_strfreev (override_files);
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  g_free (tmp);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Epilogue {{{1 */
Packit ae235b
Packit ae235b
/* vim:set foldmethod=marker: */