glib-compile-schemas.c 67.4 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright © 2010 Codethink Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8 9 10 11 12 13 14
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

20
/* Prologue {{{1 */
21 22
#include "config.h"

23
#include <gstdio.h>
24 25
#include <gi18n.h>

26 27
#include <string.h>
#include <stdio.h>
28
#include <locale.h>
29 30

#include "gvdb/gvdb-builder.h"
31
#include "strinfo.c"
32

33 34 35 36
#ifdef G_OS_WIN32
#include "glib/glib-private.h"
#endif

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static void
strip_string (GString *string)
{
  gint i;

  for (i = 0; g_ascii_isspace (string->str[i]); i++);
  g_string_erase (string, 0, i);

  if (string->len > 0)
    {
      /* len > 0, so there must be at least one non-whitespace character */
      for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
      g_string_truncate (string, i + 1);
    }
}

53
/* Handling of <enum> {{{1 */
54 55 56 57 58 59
typedef struct
{
  GString *strinfo;

  gboolean is_flags;
} EnumState;
60 61 62 63 64 65

static void
enum_state_free (gpointer data)
{
  EnumState *state = data;

66 67 68 69
  g_string_free (state->strinfo, TRUE);
  g_slice_free (EnumState, state);
}

70
static EnumState *
71 72 73 74 75 76 77 78 79
enum_state_new (gboolean is_flags)
{
  EnumState *state;

  state = g_slice_new (EnumState);
  state->strinfo = g_string_new (NULL);
  state->is_flags = is_flags;

  return state;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

static void
enum_state_add_value (EnumState    *state,
                      const gchar  *nick,
                      const gchar  *valuestr,
                      GError      **error)
{
  gint64 value;
  gchar *end;

  if (nick[0] == '\0' || nick[1] == '\0')
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
95
                   _("nick must be a minimum of 2 characters"));
96 97 98 99
      return;
    }

  value = g_ascii_strtoll (valuestr, &end, 0);
100
  if (*end || (state->is_flags ?
101
                (value > G_MAXUINT32 || value < 0) :
102
                (value > G_MAXINT32 || value < G_MININT32)))
103 104 105
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
106
                   _("Invalid numeric value"));
107 108 109
      return;
    }

110
  if (strinfo_builder_contains (state->strinfo, nick))
111 112 113
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
114
                   _("<value nick='%s'/> already specified"), nick);
115 116 117
      return;
    }

118 119 120 121
  if (strinfo_builder_contains_value (state->strinfo, value))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
122
                   _("value='%s' already specified"), valuestr);
123 124 125
      return;
    }

126 127 128 129 130 131
  /* Silently drop the null case if it is mentioned.
   * It is properly denoted with an empty array.
   */
  if (state->is_flags && value == 0)
    return;

132 133 134 135
  if (state->is_flags && (value & (value - 1)))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
136
                   _("flags values must have at most 1 bit set"));
137 138 139 140 141 142 143 144 145 146
      return;
    }

  /* Since we reject exact duplicates of value='' and we only allow one
   * bit to be set, it's not possible to have overlaps.
   *
   * If we loosen the one-bit-set restriction we need an overlap check.
   */

  strinfo_builder_append_item (state->strinfo, nick, value);
147 148 149 150 151
}

static void
enum_state_end (EnumState **state_ptr,
                GError    **error)
152
{
153
  EnumState *state;
154

155 156
  state = *state_ptr;
  *state_ptr = NULL;
157

158
  if (state->strinfo->len == 0)
159 160
    g_set_error (error,
                 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
161
                 _("<%s> must contain at least one <value>"),
162
                 state->is_flags ? "flags" : "enum");
163 164 165 166 167 168 169 170 171
}

/* Handling of <key> {{{1 */
typedef struct
{
  /* for <child>, @child_schema will be set.
   * for <key>, everything else will be set.
   */
  gchar        *child_schema;
172 173 174


  GVariantType *type;
175 176 177 178 179 180 181
  gboolean      have_gettext_domain;

  gchar         l10n;
  gchar        *l10n_context;
  GString      *unparsed_default_value;
  GVariant     *default_value;

182 183
  GVariantDict *desktop_overrides;

184 185
  GString      *strinfo;
  gboolean      is_enum;
186
  gboolean      is_flags;
187 188 189 190 191 192

  GVariant     *minimum;
  GVariant     *maximum;

  gboolean      has_choices;
  gboolean      has_aliases;
193
  gboolean      is_override;
194 195 196

  gboolean      checked;
  GVariant     *serialised;
197 198 199

  gboolean      summary_seen;
  gboolean      description_seen;
200 201 202 203 204
} KeyState;

static KeyState *
key_state_new (const gchar *type_string,
               const gchar *gettext_domain,
205 206 207
               gboolean     is_enum,
               gboolean     is_flags,
               GString     *strinfo)
208 209 210 211 212 213
{
  KeyState *state;

  state = g_slice_new0 (KeyState);
  state->type = g_variant_type_new (type_string);
  state->have_gettext_domain = gettext_domain != NULL;
214 215
  state->is_enum = is_enum;
  state->is_flags = is_flags;
216 217
  state->summary_seen = FALSE;
  state->description_seen = FALSE;
218

219 220
  if (strinfo)
    state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
221 222 223 224 225 226
  else
    state->strinfo = g_string_new (NULL);

  return state;
}

227 228 229 230 231 232 233 234 235 236 237 238
static KeyState *
key_state_override (KeyState    *state,
                    const gchar *gettext_domain)
{
  KeyState *copy;

  copy = g_slice_new0 (KeyState);
  copy->type = g_variant_type_copy (state->type);
  copy->have_gettext_domain = gettext_domain != NULL;
  copy->strinfo = g_string_new_len (state->strinfo->str,
                                    state->strinfo->len);
  copy->is_enum = state->is_enum;
239
  copy->is_flags = state->is_flags;
240 241 242 243 244 245 246 247 248 249 250
  copy->is_override = TRUE;

  if (state->minimum)
    {
      copy->minimum = g_variant_ref (state->minimum);
      copy->maximum = g_variant_ref (state->maximum);
    }

  return copy;
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
static KeyState *
key_state_new_child (const gchar *child_schema)
{
  KeyState *state;

  state = g_slice_new0 (KeyState);
  state->child_schema = g_strdup (child_schema);

  return state;
}

static gboolean
is_valid_choices (GVariant *variant,
                  GString  *strinfo)
{
  switch (g_variant_classify (variant))
    {
      case G_VARIANT_CLASS_MAYBE:
      case G_VARIANT_CLASS_ARRAY:
        {
          gboolean valid = TRUE;
          GVariantIter iter;

          g_variant_iter_init (&iter, variant);

          while (valid && (variant = g_variant_iter_next_value (&iter)))
            {
              valid = is_valid_choices (variant, strinfo);
              g_variant_unref (variant);
            }

          return valid;
        }

      case G_VARIANT_CLASS_STRING:
        return strinfo_is_string_valid ((const guint32 *) strinfo->str,
                                        strinfo->len / 4,
                                        g_variant_get_string (variant, NULL));
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
      default:
        g_assert_not_reached ();
    }
}


/* Gets called at </default> </choices> or <range/> to check for
 * validity of the default value so that any inconsistency is
 * reported as soon as it is encountered.
 */
static void
key_state_check_range (KeyState  *state,
                       GError   **error)
{
  if (state->default_value)
    {
306 307 308 309
      const gchar *tag;

      tag = state->is_override ? "override" : "default";

310 311 312 313 314 315 316
      if (state->minimum)
        {
          if (g_variant_compare (state->default_value, state->minimum) < 0 ||
              g_variant_compare (state->default_value, state->maximum) > 0)
            {
              g_set_error (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
317 318
                           _("<%s> is not contained in "
                           "the specified range"), tag);
319 320 321 322 323 324 325 326
            }
        }

      else if (state->strinfo->len)
        {
          if (!is_valid_choices (state->default_value, state->strinfo))
            {
              if (state->is_enum)
327 328
                g_set_error (error, G_MARKUP_ERROR,
                             G_MARKUP_ERROR_INVALID_CONTENT,
329 330
                             _("<%s> is not a valid member of "
                             "the specified enumerated type"), tag);
331

332 333 334
              else if (state->is_flags)
                g_set_error (error, G_MARKUP_ERROR,
                             G_MARKUP_ERROR_INVALID_CONTENT,
335 336
                             _("<%s> contains string not in the "
                             "specified flags type"), tag);
337

338
              else
339 340
                g_set_error (error, G_MARKUP_ERROR,
                             G_MARKUP_ERROR_INVALID_CONTENT,
341
                             _("<%s> contains a string not in "
342
                             "<choices>"), tag);
343 344 345 346 347 348 349 350 351 352 353
            }
        }
    }
}

static void
key_state_set_range (KeyState     *state,
                     const gchar  *min_str,
                     const gchar  *max_str,
                     GError      **error)
{
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  const struct {
    const gchar  type;
    const gchar *min;
    const gchar *max;
  } table[] = {
    { 'y',                    "0",                  "255" },
    { 'n',               "-32768",                "32767" },
    { 'q',                    "0",                "65535" },
    { 'i',          "-2147483648",           "2147483647" },
    { 'u',                    "0",           "4294967295" },
    { 'x', "-9223372036854775808",  "9223372036854775807" },
    { 't',                    "0", "18446744073709551615" },
    { 'd',                 "-inf",                  "inf" },
  };
  gboolean type_ok = FALSE;
  gint i;

371 372 373 374
  if (state->minimum)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
375
                           _("<range/> already specified for this key"));
376 377 378
      return;
    }

379 380 381 382 383 384 385 386 387 388
  for (i = 0; i < G_N_ELEMENTS (table); i++)
    if (*(char *) state->type == table[i].type)
      {
        min_str = min_str ? min_str : table[i].min;
        max_str = max_str ? max_str : table[i].max;
        type_ok = TRUE;
        break;
      }

  if (!type_ok)
389 390 391 392
    {
      gchar *type = g_variant_type_dup_string (state->type);
      g_set_error (error, G_MARKUP_ERROR,
                  G_MARKUP_ERROR_INVALID_CONTENT,
393
                  _("<range> not allowed for keys of type “%s”"), type);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
      g_free (type);
      return;
    }

  state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
  if (state->minimum == NULL)
    return;

  state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
  if (state->maximum == NULL)
    return;

  if (g_variant_compare (state->minimum, state->maximum) > 0)
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
410
                   _("<range> specified minimum is greater than maximum"));
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      return;
    }

  key_state_check_range (state, error);
}

static GString *
key_state_start_default (KeyState     *state,
                         const gchar  *l10n,
                         const gchar  *context,
                         GError      **error)
{
  if (l10n != NULL)
    {
      if (strcmp (l10n, "messages") == 0)
        state->l10n = 'm';

      else if (strcmp (l10n, "time") == 0)
        state->l10n = 't';

      else
        {
          g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
435
                       _("unsupported l10n category: %s"), l10n);
436 437 438 439 440 441 442
          return NULL;
        }

      if (!state->have_gettext_domain)
        {
          g_set_error_literal (error, G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
443 444
                               _("l10n requested, but no "
                               "gettext domain given"));
445 446 447 448 449 450 451 452 453 454
          return NULL;
        }

      state->l10n_context = g_strdup (context);
    }

  else if (context != NULL)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
455
                           _("translation context given for "
456
                           "value without l10n enabled"));
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      return NULL;
    }

  return g_string_new (NULL);
}

static void
key_state_end_default (KeyState  *state,
                       GString  **string,
                       GError   **error)
{
  state->unparsed_default_value = *string;
  *string = NULL;

  state->default_value = g_variant_parse (state->type,
                                          state->unparsed_default_value->str,
                                          NULL, NULL, error);
474 475 476
  if (!state->default_value)
    {
      gchar *type = g_variant_type_dup_string (state->type);
477
      g_prefix_error (error, _("Failed to parse <default> value of type “%s”: "), type);
478 479 480
      g_free (type);
    }

481 482 483 484 485 486 487 488 489 490 491 492 493
  key_state_check_range (state, error);
}

static void
key_state_start_choices (KeyState  *state,
                         GError   **error)
{
  const GVariantType *type = state->type;

  if (state->is_enum)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
494 495
                           _("<choices> cannot be specified for keys "
                           "tagged as having an enumerated type"));
496 497 498 499 500 501 502
      return;
    }

  if (state->has_choices)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
503
                           _("<choices> already specified for this key"));
504 505 506 507 508 509 510 511 512 513 514
      return;
    }

  while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
    type = g_variant_type_element (type);

  if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
    {
      gchar *type_string = g_variant_type_dup_string (state->type);
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
515
                   _("<choices> not allowed for keys of type “%s”"),
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
                   type_string);
      g_free (type_string);
      return;
    }
}

static void
key_state_add_choice (KeyState     *state,
                      const gchar  *choice,
                      GError      **error)
{
  if (strinfo_builder_contains (state->strinfo, choice))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
531
                   _("<choice value='%s'/> already given"), choice);
532 533 534 535 536 537 538 539 540 541 542 543 544 545
      return;
    }

  strinfo_builder_append_item (state->strinfo, choice, 0);
  state->has_choices = TRUE;
}

static void
key_state_end_choices (KeyState  *state,
                       GError   **error)
{
  if (!state->has_choices)
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
546
                   _("<choices> must contain at least one <choice>"));
547 548 549 550 551 552 553 554 555 556 557 558 559
      return;
    }

  key_state_check_range (state, error);
}

static void
key_state_start_aliases (KeyState  *state,
                         GError   **error)
{
  if (state->has_aliases)
    g_set_error_literal (error, G_MARKUP_ERROR,
                         G_MARKUP_ERROR_INVALID_CONTENT,
560
                         _("<aliases> already specified for this key"));
Matthias Clasen's avatar
Matthias Clasen committed
561
  else if (!state->is_flags && !state->is_enum && !state->has_choices)
562 563
    g_set_error_literal (error, G_MARKUP_ERROR,
                         G_MARKUP_ERROR_INVALID_CONTENT,
564 565
                         _("<aliases> can only be specified for keys with "
                         "enumerated or flags types or after <choices>"));
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
}

static void
key_state_add_alias (KeyState     *state,
                     const gchar  *alias,
                     const gchar  *target,
                     GError      **error)
{
  if (strinfo_builder_contains (state->strinfo, alias))
    {
      if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
                                   state->strinfo->len / 4,
                                   alias))
        {
          if (state->is_enum)
            g_set_error (error, G_MARKUP_ERROR,
                         G_MARKUP_ERROR_INVALID_CONTENT,
583
                         _("<alias value='%s'/> given when “%s” is already "
584
                         "a member of the enumerated type"), alias, alias);
585 586 587 588

          else
            g_set_error (error, G_MARKUP_ERROR,
                         G_MARKUP_ERROR_INVALID_CONTENT,
589 590
                         _("<alias value='%s'/> given when "
                         "<choice value='%s'/> was already given"),
591 592 593 594 595 596
                         alias, alias);
        }

      else
        g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
597
                     _("<alias value='%s'/> already specified"), alias);
598 599 600 601 602 603 604 605

      return;
    }

  if (!strinfo_builder_append_alias (state->strinfo, alias, target))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
606
                   state->is_enum ?
607 608
                     _("alias target “%s” is not in enumerated type") :
                     _("alias target “%s” is not in <choices>"),
609
                   target);
610 611 612 613 614 615 616 617 618 619 620 621 622
      return;
    }

  state->has_aliases = TRUE;
}

static void
key_state_end_aliases (KeyState  *state,
                       GError   **error)
{
  if (!state->has_aliases)
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
623
                   _("<aliases> must contain at least one <alias>"));
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
      return;
    }
}

static gboolean
key_state_check (KeyState  *state,
                 GError   **error)
{
  if (state->checked)
    return TRUE;

  return state->checked = TRUE;
}

static GVariant *
key_state_serialise (KeyState *state)
{
  if (state->serialised == NULL)
    {
      if (state->child_schema)
        {
          state->serialised = g_variant_new_string (state->child_schema);
        }

      else
        {
          GVariantBuilder builder;

          g_assert (key_state_check (state, NULL));

          g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);

          /* default value */
          g_variant_builder_add_value (&builder, state->default_value);

          /* translation */
          if (state->l10n)
            {
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
              /* We are going to store the untranslated default for
               * runtime translation according to the current locale.
               * We need to strip leading and trailing whitespace from
               * the string so that it's exactly the same as the one
               * that ended up in the .po file for translation.
               *
               * We want to do this so that
               *
               *   <default l10n='messages'>
               *     ['a', 'b', 'c']
               *   </default>
               *
               * ends up in the .po file like "['a', 'b', 'c']",
               * omitting the extra whitespace at the start and end.
               */
              strip_string (state->unparsed_default_value);

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
              if (state->l10n_context)
                {
                  gint len;

                  /* Contextified messages are supported by prepending
                   * the context, followed by '\004' to the start of the
                   * message string.  We do that here to save GSettings
                   * the work later on.
                   */
                  len = strlen (state->l10n_context);
                  state->l10n_context[len] = '\004';
                  g_string_prepend_len (state->unparsed_default_value,
                                        state->l10n_context, len + 1);
                  g_free (state->l10n_context);
                  state->l10n_context = NULL;
                }

              g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
697
                                     state->unparsed_default_value->str);
698 699 700 701 702 703 704 705
              g_string_free (state->unparsed_default_value, TRUE);
              state->unparsed_default_value = NULL;
            }

          /* choice, aliases, enums */
          if (state->strinfo->len)
            {
              GVariant *array;
706
              guint32 *words;
707 708
              gpointer data;
              gsize size;
709
              gint i;
710 711 712 713

              data = state->strinfo->str;
              size = state->strinfo->len;

714 715 716 717
              words = data;
              for (i = 0; i < size / sizeof (guint32); i++)
                words[i] = GUINT32_TO_LE (words[i]);

718 719 720 721 722 723 724 725
              array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
                                               data, size, TRUE,
                                               g_free, data);

              g_string_free (state->strinfo, FALSE);
              state->strinfo = NULL;

              g_variant_builder_add (&builder, "(y@au)",
726
                                     state->is_flags ? 'f' :
727 728 729 730 731 732 733 734 735
                                     state->is_enum ? 'e' : 'c',
                                     array);
            }

          /* range */
          if (state->minimum || state->maximum)
            g_variant_builder_add (&builder, "(y(**))", 'r',
                                   state->minimum, state->maximum);

736 737 738 739 740
          /* per-desktop overrides */
          if (state->desktop_overrides)
            g_variant_builder_add (&builder, "(y@a{sv})", 'd',
                                   g_variant_dict_end (state->desktop_overrides));

741 742 743 744 745 746 747 748 749 750 751 752 753 754
          state->serialised = g_variant_builder_end (&builder);
        }

      g_variant_ref_sink (state->serialised);
    }

  return g_variant_ref (state->serialised);
}

static void
key_state_free (gpointer data)
{
  KeyState *state = data;

755 756
  g_free (state->child_schema);

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  if (state->type)
    g_variant_type_free (state->type);

  g_free (state->l10n_context);

  if (state->unparsed_default_value)
    g_string_free (state->unparsed_default_value, TRUE);

  if (state->default_value)
    g_variant_unref (state->default_value);

  if (state->strinfo)
    g_string_free (state->strinfo, TRUE);

  if (state->minimum)
    g_variant_unref (state->minimum);

  if (state->maximum)
    g_variant_unref (state->maximum);

  if (state->serialised)
    g_variant_unref (state->serialised);

780 781 782
  if (state->desktop_overrides)
    g_variant_dict_unref (state->desktop_overrides);

783 784 785 786
  g_slice_free (KeyState, state);
}

/* Key name validity {{{1 */
787 788
static gboolean allow_any_name = FALSE;

789 790 791 792 793 794 795 796
static gboolean
is_valid_keyname (const gchar  *key,
                  GError      **error)
{
  gint i;

  if (key[0] == '\0')
    {
797
      g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
798
                           _("Empty names are not permitted"));
799 800 801
      return FALSE;
    }

802 803 804
  if (allow_any_name)
    return TRUE;

805 806 807
  if (!g_ascii_islower (key[0]))
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
808
                   _("Invalid name “%s”: names must begin "
809
                     "with a lowercase letter"), key);
810 811 812 813 814 815 816 817 818 819
      return FALSE;
    }

  for (i = 1; key[i]; i++)
    {
      if (key[i] != '-' &&
          !g_ascii_islower (key[i]) &&
          !g_ascii_isdigit (key[i]))
        {
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
820 821
                       _("Invalid name “%s”: invalid character “%c”; "
                         "only lowercase letters, numbers and hyphen (“-”) "
822
                         "are permitted"), key, key[i]);
823 824 825 826 827 828
          return FALSE;
        }

      if (key[i] == '-' && key[i + 1] == '-')
        {
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
829
                       _("Invalid name “%s”: two successive hyphens (“--”) "
830
                         "are not permitted"), key);
831 832 833 834 835 836 837
          return FALSE;
        }
    }

  if (key[i - 1] == '-')
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
838 839
                   _("Invalid name “%s”: the last character may not be a "
                     "hyphen (“-”)"), key);
840 841 842
      return FALSE;
    }

843
  if (i > 1024)
844 845
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
846
                   _("Invalid name “%s”: maximum length is 1024"), key);
847 848 849 850 851 852
      return FALSE;
    }

  return TRUE;
}

853
/* Handling of <schema> {{{1 */
854 855
typedef struct _SchemaState SchemaState;
struct _SchemaState
856
{
857 858 859 860 861 862
  SchemaState *extends;

  gchar       *path;
  gchar       *gettext_domain;
  gchar       *extends_name;
  gchar       *list_of;
863

864 865
  GHashTable  *keys;
};
866 867 868

static SchemaState *
schema_state_new (const gchar  *path,
869 870 871 872
                  const gchar  *gettext_domain,
                  SchemaState  *extends,
                  const gchar  *extends_name,
                  const gchar  *list_of)
873 874 875 876 877 878
{
  SchemaState *state;

  state = g_slice_new (SchemaState);
  state->path = g_strdup (path);
  state->gettext_domain = g_strdup (gettext_domain);
879 880 881
  state->extends = extends;
  state->extends_name = g_strdup (extends_name);
  state->list_of = g_strdup (list_of);
882 883
  state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
                                       g_free, key_state_free);
884

885
  return state;
886 887
}

888 889
static void
schema_state_free (gpointer data)
890
{
891
  SchemaState *state = data;
892

893 894
  g_free (state->path);
  g_free (state->gettext_domain);
895 896
  g_free (state->extends_name);
  g_free (state->list_of);
897
  g_hash_table_unref (state->keys);
898
  g_slice_free (SchemaState, state);
899 900
}

901 902 903 904 905
static void
schema_state_add_child (SchemaState  *state,
                        const gchar  *name,
                        const gchar  *schema,
                        GError      **error)
906
{
907 908 909 910 911 912 913 914
  gchar *childname;

  if (!is_valid_keyname (name, error))
    return;

  childname = g_strconcat (name, "/", NULL);

  if (g_hash_table_lookup (state->keys, childname))
915
    {
916 917
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
918
                   _("<child name='%s'> already specified"), name);
919 920
      return;
    }
921

922 923 924
  g_hash_table_insert (state->keys, childname,
                       key_state_new_child (schema));
}
925

926 927 928
static KeyState *
schema_state_add_key (SchemaState  *state,
                      GHashTable   *enum_table,
929
                      GHashTable   *flags_table,
930 931 932
                      const gchar  *name,
                      const gchar  *type_string,
                      const gchar  *enum_type,
933
                      const gchar  *flags_type,
934 935
                      GError      **error)
{
936
  SchemaState *node;
937
  GString *strinfo;
938 939
  KeyState *key;

940 941 942 943
  if (state->list_of)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
944
                           _("Cannot add keys to a “list-of” schema"));
945 946 947
      return NULL;
    }

948 949 950 951 952 953 954
  if (!is_valid_keyname (name, error))
    return NULL;

  if (g_hash_table_lookup (state->keys, name))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
955
                   _("<key name='%s'> already specified"), name);
956 957 958
      return NULL;
    }

959 960 961 962 963 964 965 966 967 968 969 970 971 972
  for (node = state; node; node = node->extends)
    if (node->extends)
      {
        KeyState *shadow;

        shadow = g_hash_table_lookup (node->extends->keys, name);

        /* in case of <key> <override> <key> make sure we report the
         * location of the original <key>, not the <override>.
         */
        if (shadow && !shadow->is_override)
          {
            g_set_error (error, G_MARKUP_ERROR,
                         G_MARKUP_ERROR_INVALID_CONTENT,
973 974
                         _("<key name='%s'> shadows <key name='%s'> in "
                           "<schema id='%s'>; use <override> to modify value"),
975 976 977 978 979
                         name, name, node->extends_name);
            return NULL;
          }
      }

980
  if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
981 982 983
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_MISSING_ATTRIBUTE,
984
                   _("Exactly one of “type”, “enum” or “flags” must "
985
                     "be specified as an attribute to <key>"));
986 987
      return NULL;
    }
988

989
  if (type_string == NULL) /* flags or enums was specified */
990
    {
991 992 993 994 995 996
      EnumState *enum_state;

      if (enum_type)
        enum_state = g_hash_table_lookup (enum_table, enum_type);
      else
        enum_state = g_hash_table_lookup (flags_table, flags_type);
997

998 999

      if (enum_state == NULL)
1000 1001 1002
        {
          g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
1003
                       _("<%s id='%s'> not (yet) defined."),
1004 1005
                       flags_type ? "flags"    : "enum",
                       flags_type ? flags_type : enum_type);
1006
          return NULL;
1007 1008
        }

1009 1010
      type_string = flags_type ? "as" : "s";
      strinfo = enum_state->strinfo;
1011 1012 1013 1014
    }
  else
    {
      if (!g_variant_type_string_is_valid (type_string))
1015
        {
1016 1017
          g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
1018
                       _("Invalid GVariant type string “%s”"), type_string);
1019 1020
          return NULL;
        }
1021

1022
      strinfo = NULL;
1023
    }
1024

1025 1026
  key = key_state_new (type_string, state->gettext_domain,
                       enum_type != NULL, flags_type != NULL, strinfo);
1027
  g_hash_table_insert (state->keys, g_strdup (name), key);
1028

1029 1030
  return key;
}
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
static void
schema_state_add_override (SchemaState  *state,
                           KeyState    **key_state,
                           GString     **string,
                           const gchar  *key,
                           const gchar  *l10n,
                           const gchar  *context,
                           GError      **error)
{
  SchemaState *parent;
  KeyState *original;

  if (state->extends == NULL)
    {
      g_set_error_literal (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
1048
                           _("<override> given but schema isn’t "
1049
                             "extending anything"));
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
      return;
    }

  for (parent = state->extends; parent; parent = parent->extends)
    if ((original = g_hash_table_lookup (parent->keys, key)))
      break;

  if (original == NULL)
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
1061
                   _("No <key name='%s'> to override"), key);
1062 1063 1064 1065 1066 1067 1068
      return;
    }

  if (g_hash_table_lookup (state->keys, key))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
1069
                   _("<override name='%s'> already specified"), key);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
      return;
    }

  *key_state = key_state_override (original, state->gettext_domain);
  *string = key_state_start_default (*key_state, l10n, context, error);
  g_hash_table_insert (state->keys, g_strdup (key), *key_state);
}

static void
override_state_end (KeyState **key_state,
                    GString  **string,
                    GError   **error)
{
  key_state_end_default (*key_state, string, error);
  *key_state = NULL;
}

1087 1088 1089
/* Handling of toplevel state {{{1 */
typedef struct
{
1090 1091
  gboolean     strict;                  /* TRUE if --strict was given */

1092
  GHashTable  *schema_table;            /* string -> SchemaState */
1093 1094
  GHashTable  *flags_table;             /* string -> EnumState */
  GHashTable  *enum_table;              /* string -> EnumState */
1095

1096 1097 1098 1099
  GSList      *this_file_schemas;       /* strings: <schema>s in this file */
  GSList      *this_file_flagss;        /* strings: <flags>s in this file */
  GSList      *this_file_enums;         /* strings: <enum>s in this file */

1100 1101 1102 1103
  gchar       *schemalist_domain;       /* the <schemalist> gettext domain */

  SchemaState *schema_state;            /* non-NULL when inside <schema> */
  KeyState    *key_state;               /* non-NULL when inside <key> */
1104
  EnumState   *enum_state;              /* non-NULL when inside <enum> */
1105 1106 1107 1108

  GString     *string;                  /* non-NULL when accepting text */
} ParseState;

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
static gboolean
is_subclass (const gchar *class_name,
             const gchar *possible_parent,
             GHashTable  *schema_table)
{
  SchemaState *class;

  if (strcmp (class_name, possible_parent) == 0)
    return TRUE;

  class = g_hash_table_lookup (schema_table, class_name);
  g_assert (class != NULL);

  return class->extends_name &&
         is_subclass (class->extends_name, possible_parent, schema_table);
}

1126 1127 1128 1129 1130
static void
parse_state_start_schema (ParseState  *state,
                          const gchar  *id,
                          const gchar  *path,
                          const gchar  *gettext_domain,
1131 1132
                          const gchar  *extends_name,
                          const gchar  *list_of,
1133 1134
                          GError      **error)
{
1135
  SchemaState *extends;
1136
  gchar *my_id;
1137

1138 1139 1140 1141
  if (g_hash_table_lookup (state->schema_table, id))
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
1142
                   _("<schema id='%s'> already specified"), id);
1143 1144 1145
      return;
    }

1146 1147 1148 1149 1150 1151 1152 1153
  if (extends_name)
    {
      extends = g_hash_table_lookup (state->schema_table, extends_name);

      if (extends == NULL)
        {
          g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
1154
                       _("<schema id='%s'> extends not yet existing "
1155
                         "schema “%s”"), id, extends_name);
1156 1157 1158 1159 1160 1161 1162 1163
          return;
        }
    }
  else
    extends = NULL;

  if (list_of)
    {
1164 1165 1166
      SchemaState *tmp;

      if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1167 1168 1169
        {
          g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
1170
                       _("<schema id='%s'> is list of not yet existing "
1171
                         "schema “%s”"), id, list_of);
1172 1173
          return;
        }
1174 1175 1176 1177

      if (tmp->path)
        {
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1178
                       _("Cannot be a list of a schema with a path"));
1179 1180
          return;
        }
1181 1182 1183 1184
    }

  if (extends)
    {
1185 1186 1187
      if (extends->path)
        {
          g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1188
                       _("Cannot extend a schema with a path"));
1189 1190 1191
          return;
        }

1192 1193 1194 1195 1196 1197
      if (list_of)
        {
          if (extends->list_of == NULL)
            {
              g_set_error (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
1198 1199
                           _("<schema id='%s'> is a list, extending "
                             "<schema id='%s'> which is not a list"),
1200 1201 1202 1203 1204 1205 1206 1207
                           id, extends_name);
              return;
            }

          if (!is_subclass (list_of, extends->list_of, state->schema_table))
            {
              g_set_error (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
1208
                           _("<schema id='%s' list-of='%s'> extends <schema "
1209 1210
                             "id='%s' list-of='%s'> but “%s” does not "
                             "extend “%s”"), id, list_of, extends_name,
1211
                           extends->list_of, list_of, extends->list_of);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
              return;
            }
        }
      else
        /* by default we are a list of the same thing that the schema
         * we are extending is a list of (which might be nothing)
         */
        list_of = extends->list_of;
    }

1222 1223
  if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
    {
1224
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1225
                   _("A path, if given, must begin and end with a slash"));
1226
      return;
1227
    }
1228

1229 1230 1231
  if (path && list_of && !g_str_has_suffix (path, ":/"))
    {
      g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1232
                   _("The path of a list must end with “:/”"));
1233 1234 1235
      return;
    }

1236 1237 1238
  if (path && (g_str_has_prefix (path, "/apps/") ||
               g_str_has_prefix (path, "/desktop/") ||
               g_str_has_prefix (path, "/system/")))
1239 1240
    {
      gchar *message = NULL;
1241
      message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”.  "
1242
                                   "Paths starting with "
1243
                                   "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1244 1245 1246 1247
                                 id, path);
      g_printerr ("%s\n", message);
      g_free (message);
    }
1248

1249 1250
  state->schema_state = schema_state_new (path, gettext_domain,
                                          extends, extends_name, list_of);
1251 1252 1253 1254

  my_id = g_strdup (id);
  state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
  g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1255 1256
}

1257 1258 1259
static void
parse_state_start_enum (ParseState   *state,
                        const gchar  *id,
1260
                        gboolean      is_flags,
1261 1262
                        GError      **error)
{
1263
  GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1264
  GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1265
  gchar *my_id;
1266 1267

  if (g_hash_table_lookup (table, id))
1268 1269 1270
    {
      g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
1271
                   _("<%s id='%s'> already specified"),
1272
                   is_flags ? "flags" : "enum", id);
1273 1274 1275
      return;
    }

1276
  state->enum_state = enum_state_new (is_flags);
1277 1278 1279 1280

  my_id = g_strdup (id);
  *list = g_slist_prepend (*list, my_id);
  g_hash_table_insert (table, my_id, state->enum_state);
1281 1282 1283 1284 1285
}

/* GMarkup Parser Functions {{{1 */

/* Start element {{{2 */
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
static void
start_element (GMarkupParseContext  *context,
               const gchar          *element_name,
               const gchar         **attribute_names,
               const gchar         **attribute_values,
               gpointer              user_data,
               GError              **error)
{
  ParseState *state = user_data;
  const GSList *element_stack;
  const gchar *container;

  element_stack = g_markup_parse_context_get_element_stack (context);
  container = element_stack->next ? element_stack->next->data : NULL;

#define COLLECT(first, ...) \
  g_markup_collect_attributes (element_name,                                 \
                               attribute_names, attribute_values, error,     \
                               first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
#define OPTIONAL   G_MARKUP_COLLECT_OPTIONAL
#define STRDUP     G_MARKUP_COLLECT_STRDUP
#define STRING     G_MARKUP_COLLECT_STRING
1308
#define NO_ATTRS()  COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1309

1310
  /* Toplevel items {{{3 */
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  if (container == NULL)
    {
      if (strcmp (element_name, "schemalist") == 0)
        {
          COLLECT (OPTIONAL | STRDUP,
                   "gettext-domain",
                   &state->schemalist_domain);
          return;
        }
    }
1321 1322 1323


  /* children of <schemalist> {{{3 */
1324 1325 1326 1327
  else if (strcmp (container, "schemalist") == 0)
    {
      if (strcmp (element_name, "schema") == 0)
        {
1328
          const gchar *id, *path, *gettext_domain, *extends, *list_of;
1329 1330
          if (COLLECT (STRING, "id", &id,
                       OPTIONAL | STRING, "path", &path,
1331 1332 1333
                       OPTIONAL | STRING, "gettext-domain", &gettext_domain,
                       OPTIONAL | STRING, "extends", &extends,
                       OPTIONAL | STRING, "list-of", &list_of))
1334 1335 1336
            parse_state_start_schema (state, id, path,
                                      gettext_domain ? gettext_domain
                                                     : state->schemalist_domain,
1337
                                      extends, list_of, error);
1338 1339 1340
          return;
        }

1341
      else if (strcmp (element_name, "enum") == 0)
1342 1343 1344
        {
          const gchar *id;
          if (COLLECT (STRING, "id", &id))
1345 1346 1347 1348 1349 1350 1351 1352 1353
            parse_state_start_enum (state, id, FALSE, error);
          return;
        }

      else if (strcmp (element_name, "flags") == 0)
        {
          const gchar *id;
          if (COLLECT (STRING, "id", &id))
            parse_state_start_enum (state, id, TRUE, error);
1354 1355 1356
          return;
        }
    }
1357 1358 1359


  /* children of <schema> {{{3 */
1360 1361 1362 1363
  else if (strcmp (container, "schema") == 0)
    {
      if (strcmp (element_name, "key") == 0)
        {
1364
          const gchar *name, *type_string, *enum_type, *flags_type;
1365

1366 1367 1368 1369
          if (COLLECT (STRING,            "name",  &name,
                       OPTIONAL | STRING, "type",  &type_string,
                       OPTIONAL | STRING, "enum",  &enum_type,
                       OPTIONAL | STRING, "flags", &flags_type))
1370

1371 1372
            state->key_state = schema_state_add_key (state->schema_state,
                                                     state->enum_table,
1373
                                                     state->flags_table,
1374
                                                     name, type_string,
1375 1376
                                                     enum_type, flags_type,
                                                     error);
1377 1378 1379 1380 1381 1382 1383
          return;
        }
      else if (strcmp (element_name, "child") == 0)
        {
          const gchar *name, *schema;

          if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1384 1385 1386
            schema_state_add_child (state->schema_state,
                                    name, schema, error);
          return;
1387
        }
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
      else if (strcmp (element_name, "override") == 0)
        {
          const gchar *name, *l10n, *context;

          if (COLLECT (STRING,            "name",    &name,
                       OPTIONAL | STRING, "l10n",    &l10n,
                       OPTIONAL | STRING, "context", &context))
            schema_state_add_override (state->schema_state,
                                       &state->key_state, &state->string,
                                       name, l10n, context, error);
          return;
        }
1400
    }
1401 1402

  /* children of <key> {{{3 */
1403 1404 1405 1406
  else if (strcmp (container, "key") == 0)
    {
      if (strcmp (element_name, "default") == 0)
        {
1407 1408 1409 1410 1411
          const gchar *l10n, *context;
          if (COLLECT (STRING | OPTIONAL, "l10n",    &l10n,
                       STRING | OPTIONAL, "context", &context))
            state->string = key_state_start_default (state->key_state,
                                                     l10n, context, error);
1412 1413
          return;
        }
1414

1415
      else if (strcmp (element_name, "summary") == 0)
1416
        {
1417
          if (NO_ATTRS ())
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
            {
              if (state->key_state->summary_seen && state->strict)
                g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
                             _("Only one <%s> element allowed inside <%s>"),
                             element_name, container);
              else
                state->string = g_string_new (NULL);

              state->key_state->summary_seen = TRUE;
            }
1428 1429 1430 1431 1432
          return;
        }

      else if (strcmp (element_name, "description") == 0)
        {
1433
          if (NO_ATTRS ())
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
            {
              if (state->key_state->description_seen && state->strict)
                g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
                             _("Only one <%s> element allowed inside <%s>"),
                             element_name, container);
              else
                state->string = g_string_new (NULL);

            state->key_state->description_seen = TRUE;
            }
1444 1445
          return;
        }
1446

1447 1448
      else if (strcmp (element_name, "range") == 0)
        {
1449
          const gchar *min, *max;
1450 1451
          if (COLLECT (STRING | OPTIONAL, "min", &min,
                       STRING | OPTIONAL, "max", &max))
1452
            key_state_set_range (state->key_state, min, max, error);
1453 1454
          return;
        }
1455

1456
      else if (strcmp (element_name, "choices") == 0)
1457
        {
1458 1459 1460 1461
          if (NO_ATTRS ())
            key_state_start_choices (state->key_state, error);
          return;
        }
1462

1463 1464 1465 1466
      else if (strcmp (element_name, "aliases") == 0)
        {
          if (NO_ATTRS ())
            key_state_start_aliases (state->key_state, error);
1467 1468
          return;
        }
1469
    }
1470 1471 1472


  /* children of <choices> {{{3 */
1473 1474 1475
  else if (strcmp (container, "choices") == 0)
    {
      if (strcmp (element_name, "choice") == 0)
1476
        {
1477 1478
          const gchar *value;
          if (COLLECT (STRING, "value", &value))
1479 1480 1481 1482 1483
            key_state_add_choice (state->key_state, value, error);
          return;
        }
    }

1484

1485 1486 1487 1488 1489 1490 1491 1492
  /* children of <aliases> {{{3 */
  else if (strcmp (container, "aliases") == 0)
    {
      if (strcmp (element_name, "alias") == 0)
        {
          const gchar *value, *target;
          if (COLLECT (STRING, "value", &value, STRING, "target", &target))
            key_state_add_alias (state->key_state, value, target, error);
1493 1494 1495 1496
          return;
        }
    }

1497 1498

  /* children of <enum> {{{3 */
1499 1500
  else if (strcmp (container, "enum") == 0 ||
           strcmp (container, "flags") == 0)
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
    {
      if (strcmp (element_name, "value") == 0)
        {
          const gchar *nick, *valuestr;
          if (COLLECT (STRING, "nick", &nick,
                       STRING, "value", &valuestr))
            enum_state_add_value (state->enum_state, nick, valuestr, error);
          return;
        }
    }
  /* 3}}} */

1513 1514
  if (container)
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1515
                 _("Element <%s> not allowed inside <%s>"),
1516 1517 1518
                 element_name, container);
  else
    g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1519
                 _("Element <%s> not allowed at the top level"), element_name);
1520
}
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
/* 2}}} */
/* End element {{{2 */

static void
key_state_end (KeyState **state_ptr,
               GError   **error)
{
  KeyState *state;

  state = *state_ptr;
  *state_ptr = NULL;

  if (state->default_value == NULL)
    {
      g_set_error_literal (error,
                           G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1537
                           _("Element <default> is required in <key>"));
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
      return;
    }
}

static void
schema_state_end (SchemaState **state_ptr,
                  GError      **error)
{
  *state_ptr = NULL;
}
1548 1549 1550 1551 1552 1553 1554 1555 1556

static void
end_element (GMarkupParseContext  *context,
             const gchar          *element_name,
             gpointer              user_data,
             GError              **error)
{
  ParseState *state = user_data;

1557
  if (strcmp (element_name, "schemalist") == 0)
1558
    {
1559 1560 1561
      g_free (state->schemalist_domain);
      state->schemalist_domain = NULL;
    }
1562

1563 1564
  else if (strcmp (element_name, "enum") == 0 ||
           strcmp (element_name, "flags") == 0)
1565
    enum_state_end (&state->enum_state, error);
1566

1567 1568
  else if (strcmp (element_name, "schema") == 0)
    schema_state_end (&state->schema_state, error);
1569

1570 1571 1572
  else if (strcmp (element_name, "override") == 0)
    override_state_end (&state->key_state, &state->string, error);

1573
  else if (strcmp (element_name, "key") == 0)
1574
    key_state_end (&state->key_state, error);
1575

1576 1577
  else if (strcmp (element_name, "default") == 0)
    key_state_end_default (state->key_state, &state->string, error);
1578

1579 1580
  else if (strcmp (element_name, "choices") == 0)
    key_state_end_choices (state->key_state, error);
1581

1582 1583
  else if (strcmp (element_name, "aliases") == 0)
    key_state_end_aliases (state->key_state, error);
1584

1585
  if (state->string)
1586 1587 1588 1589
    {
      g_string_free (state->string, TRUE);
      state->string = NULL;
    }
1590
}
1591
/* Text {{{2 */
1592 1593 1594 1595 1596 1597 1598 1599 1600
static void
text (GMarkupParseContext  *context,
      const gchar          *text,
      gsize                 text_len,
      gpointer              user_data,
      GError              **error)
{
  ParseState *state = user_data;

1601 1602