2 * Copyright © 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
33 #include "gvdb/gvdb-builder.h"
35 #include "glib/glib-private.h"
38 strip_string (GString *string)
42 for (i = 0; g_ascii_isspace (string->str[i]); i++);
43 g_string_erase (string, 0, i);
47 /* len > 0, so there must be at least one non-whitespace character */
48 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
49 g_string_truncate (string, i + 1);
53 /* Handling of <enum> {{{1 */
62 enum_state_free (gpointer data)
64 EnumState *state = data;
66 g_string_free (state->strinfo, TRUE);
67 g_slice_free (EnumState, state);
71 enum_state_new (gboolean is_flags)
75 state = g_slice_new (EnumState);
76 state->strinfo = g_string_new (NULL);
77 state->is_flags = is_flags;
83 enum_state_add_value (EnumState *state,
85 const gchar *valuestr,
91 if (nick[0] == '\0' || nick[1] == '\0')
93 g_set_error (error, G_MARKUP_ERROR,
94 G_MARKUP_ERROR_INVALID_CONTENT,
95 _("nick must be a minimum of 2 characters"));
99 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)))
104 g_set_error (error, G_MARKUP_ERROR,
105 G_MARKUP_ERROR_INVALID_CONTENT,
106 _("Invalid numeric value"));
110 if (strinfo_builder_contains (state->strinfo, nick))
112 g_set_error (error, G_MARKUP_ERROR,
113 G_MARKUP_ERROR_INVALID_CONTENT,
114 _("<value nick='%s'/> already specified"), nick);
118 if (strinfo_builder_contains_value (state->strinfo, value))
120 g_set_error (error, G_MARKUP_ERROR,
121 G_MARKUP_ERROR_INVALID_CONTENT,
122 _("value='%s' already specified"), valuestr);
126 /* Silently drop the null case if it is mentioned.
127 * It is properly denoted with an empty array.
129 if (state->is_flags && value == 0)
132 if (state->is_flags && (value & (value - 1)))
134 g_set_error (error, G_MARKUP_ERROR,
135 G_MARKUP_ERROR_INVALID_CONTENT,
136 _("flags values must have at most 1 bit set"));
140 /* Since we reject exact duplicates of value='' and we only allow one
141 * bit to be set, it's not possible to have overlaps.
143 * If we loosen the one-bit-set restriction we need an overlap check.
146 strinfo_builder_append_item (state->strinfo, nick, value);
150 enum_state_end (EnumState **state_ptr,
158 if (state->strinfo->len == 0)
160 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
161 _("<%s> must contain at least one <value>"),
162 state->is_flags ? "flags" : "enum");
165 /* Handling of <key> {{{1 */
168 /* for <child>, @child_schema will be set.
169 * for <key>, everything else will be set.
175 gboolean have_gettext_domain;
179 GString *unparsed_default_value;
180 GVariant *default_value;
182 GVariantDict *desktop_overrides;
191 gboolean has_choices;
192 gboolean has_aliases;
193 gboolean is_override;
196 GVariant *serialised;
198 gboolean summary_seen;
199 gboolean description_seen;
203 key_state_new (const gchar *type_string,
204 const gchar *gettext_domain,
211 state = g_slice_new0 (KeyState);
212 state->type = g_variant_type_new (type_string);
213 state->have_gettext_domain = gettext_domain != NULL;
214 state->is_enum = is_enum;
215 state->is_flags = is_flags;
216 state->summary_seen = FALSE;
217 state->description_seen = FALSE;
220 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
222 state->strinfo = g_string_new (NULL);
228 key_state_override (KeyState *state,
229 const gchar *gettext_domain)
233 copy = g_slice_new0 (KeyState);
234 copy->type = g_variant_type_copy (state->type);
235 copy->have_gettext_domain = gettext_domain != NULL;
236 copy->strinfo = g_string_new_len (state->strinfo->str,
237 state->strinfo->len);
238 copy->is_enum = state->is_enum;
239 copy->is_flags = state->is_flags;
240 copy->is_override = TRUE;
244 copy->minimum = g_variant_ref (state->minimum);
245 copy->maximum = g_variant_ref (state->maximum);
252 key_state_new_child (const gchar *child_schema)
256 state = g_slice_new0 (KeyState);
257 state->child_schema = g_strdup (child_schema);
263 is_valid_choices (GVariant *variant,
266 switch (g_variant_classify (variant))
268 case G_VARIANT_CLASS_MAYBE:
269 case G_VARIANT_CLASS_ARRAY:
271 gboolean valid = TRUE;
274 g_variant_iter_init (&iter, variant);
276 while (valid && (variant = g_variant_iter_next_value (&iter)))
278 valid = is_valid_choices (variant, strinfo);
279 g_variant_unref (variant);
285 case G_VARIANT_CLASS_STRING:
286 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
288 g_variant_get_string (variant, NULL));
291 g_assert_not_reached ();
296 /* Gets called at </default> </choices> or <range/> to check for
297 * validity of the default value so that any inconsistency is
298 * reported as soon as it is encountered.
301 key_state_check_range (KeyState *state,
304 if (state->default_value)
308 tag = state->is_override ? "override" : "default";
312 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
313 g_variant_compare (state->default_value, state->maximum) > 0)
315 g_set_error (error, G_MARKUP_ERROR,
316 G_MARKUP_ERROR_INVALID_CONTENT,
317 _("<%s> is not contained in "
318 "the specified range"), tag);
322 else if (state->strinfo->len)
324 if (!is_valid_choices (state->default_value, state->strinfo))
327 g_set_error (error, G_MARKUP_ERROR,
328 G_MARKUP_ERROR_INVALID_CONTENT,
329 _("<%s> is not a valid member of "
330 "the specified enumerated type"), tag);
332 else if (state->is_flags)
333 g_set_error (error, G_MARKUP_ERROR,
334 G_MARKUP_ERROR_INVALID_CONTENT,
335 _("<%s> contains string not in the "
336 "specified flags type"), tag);
339 g_set_error (error, G_MARKUP_ERROR,
340 G_MARKUP_ERROR_INVALID_CONTENT,
341 _("<%s> contains a string not in "
349 key_state_set_range (KeyState *state,
350 const gchar *min_str,
351 const gchar *max_str,
360 { 'n', "-32768", "32767" },
361 { 'q', "0", "65535" },
362 { 'i', "-2147483648", "2147483647" },
363 { 'u', "0", "4294967295" },
364 { 'x', "-9223372036854775808", "9223372036854775807" },
365 { 't', "0", "18446744073709551615" },
366 { 'd', "-inf", "inf" },
368 gboolean type_ok = FALSE;
373 g_set_error_literal (error, G_MARKUP_ERROR,
374 G_MARKUP_ERROR_INVALID_CONTENT,
375 _("<range/> already specified for this key"));
379 for (i = 0; i < G_N_ELEMENTS (table); i++)
380 if (*(char *) state->type == table[i].type)
382 min_str = min_str ? min_str : table[i].min;
383 max_str = max_str ? max_str : table[i].max;
390 gchar *type = g_variant_type_dup_string (state->type);
391 g_set_error (error, G_MARKUP_ERROR,
392 G_MARKUP_ERROR_INVALID_CONTENT,
393 _("<range> not allowed for keys of type “%s”"), type);
398 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
399 if (state->minimum == NULL)
402 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
403 if (state->maximum == NULL)
406 if (g_variant_compare (state->minimum, state->maximum) > 0)
408 g_set_error (error, G_MARKUP_ERROR,
409 G_MARKUP_ERROR_INVALID_CONTENT,
410 _("<range> specified minimum is greater than maximum"));
414 key_state_check_range (state, error);
418 key_state_start_default (KeyState *state,
420 const gchar *context,
425 if (strcmp (l10n, "messages") == 0)
428 else if (strcmp (l10n, "time") == 0)
433 g_set_error (error, G_MARKUP_ERROR,
434 G_MARKUP_ERROR_INVALID_CONTENT,
435 _("unsupported l10n category: %s"), l10n);
439 if (!state->have_gettext_domain)
441 g_set_error_literal (error, G_MARKUP_ERROR,
442 G_MARKUP_ERROR_INVALID_CONTENT,
443 _("l10n requested, but no "
444 "gettext domain given"));
448 state->l10n_context = g_strdup (context);
451 else if (context != NULL)
453 g_set_error_literal (error, G_MARKUP_ERROR,
454 G_MARKUP_ERROR_INVALID_CONTENT,
455 _("translation context given for "
456 "value without l10n enabled"));
460 return g_string_new (NULL);
464 key_state_end_default (KeyState *state,
468 state->unparsed_default_value = *string;
471 state->default_value = g_variant_parse (state->type,
472 state->unparsed_default_value->str,
474 if (!state->default_value)
476 gchar *type = g_variant_type_dup_string (state->type);
477 g_prefix_error (error, _("Failed to parse <default> value of type “%s”: "), type);
481 key_state_check_range (state, error);
485 key_state_start_choices (KeyState *state,
488 const GVariantType *type = state->type;
492 g_set_error_literal (error, G_MARKUP_ERROR,
493 G_MARKUP_ERROR_INVALID_CONTENT,
494 _("<choices> cannot be specified for keys "
495 "tagged as having an enumerated type"));
499 if (state->has_choices)
501 g_set_error_literal (error, G_MARKUP_ERROR,
502 G_MARKUP_ERROR_INVALID_CONTENT,
503 _("<choices> already specified for this key"));
507 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
508 type = g_variant_type_element (type);
510 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
512 gchar *type_string = g_variant_type_dup_string (state->type);
513 g_set_error (error, G_MARKUP_ERROR,
514 G_MARKUP_ERROR_INVALID_CONTENT,
515 _("<choices> not allowed for keys of type “%s”"),
517 g_free (type_string);
523 key_state_add_choice (KeyState *state,
527 if (strinfo_builder_contains (state->strinfo, choice))
529 g_set_error (error, G_MARKUP_ERROR,
530 G_MARKUP_ERROR_INVALID_CONTENT,
531 _("<choice value='%s'/> already given"), choice);
535 strinfo_builder_append_item (state->strinfo, choice, 0);
536 state->has_choices = TRUE;
540 key_state_end_choices (KeyState *state,
543 if (!state->has_choices)
545 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
546 _("<choices> must contain at least one <choice>"));
550 key_state_check_range (state, error);
554 key_state_start_aliases (KeyState *state,
557 if (state->has_aliases)
558 g_set_error_literal (error, G_MARKUP_ERROR,
559 G_MARKUP_ERROR_INVALID_CONTENT,
560 _("<aliases> already specified for this key"));
561 else if (!state->is_flags && !state->is_enum && !state->has_choices)
562 g_set_error_literal (error, G_MARKUP_ERROR,
563 G_MARKUP_ERROR_INVALID_CONTENT,
564 _("<aliases> can only be specified for keys with "
565 "enumerated or flags types or after <choices>"));
569 key_state_add_alias (KeyState *state,
574 if (strinfo_builder_contains (state->strinfo, alias))
576 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
577 state->strinfo->len / 4,
581 g_set_error (error, G_MARKUP_ERROR,
582 G_MARKUP_ERROR_INVALID_CONTENT,
583 _("<alias value='%s'/> given when “%s” is already "
584 "a member of the enumerated type"), alias, alias);
587 g_set_error (error, G_MARKUP_ERROR,
588 G_MARKUP_ERROR_INVALID_CONTENT,
589 _("<alias value='%s'/> given when "
590 "<choice value='%s'/> was already given"),
595 g_set_error (error, G_MARKUP_ERROR,
596 G_MARKUP_ERROR_INVALID_CONTENT,
597 _("<alias value='%s'/> already specified"), alias);
602 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
604 g_set_error (error, G_MARKUP_ERROR,
605 G_MARKUP_ERROR_INVALID_CONTENT,
607 _("alias target “%s” is not in enumerated type") :
608 _("alias target “%s” is not in <choices>"),
613 state->has_aliases = TRUE;
617 key_state_end_aliases (KeyState *state,
620 if (!state->has_aliases)
622 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
623 _("<aliases> must contain at least one <alias>"));
629 key_state_check (KeyState *state,
635 return state->checked = TRUE;
639 key_state_serialise (KeyState *state)
641 if (state->serialised == NULL)
643 if (state->child_schema)
645 state->serialised = g_variant_new_string (state->child_schema);
650 GVariantBuilder builder;
651 gboolean checked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
653 checked = key_state_check (state, NULL);
656 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
659 g_variant_builder_add_value (&builder, state->default_value);
664 /* We are going to store the untranslated default for
665 * runtime translation according to the current locale.
666 * We need to strip leading and trailing whitespace from
667 * the string so that it's exactly the same as the one
668 * that ended up in the .po file for translation.
670 * We want to do this so that
672 * <default l10n='messages'>
676 * ends up in the .po file like "['a', 'b', 'c']",
677 * omitting the extra whitespace at the start and end.
679 strip_string (state->unparsed_default_value);
681 if (state->l10n_context)
685 /* Contextified messages are supported by prepending
686 * the context, followed by '\004' to the start of the
687 * message string. We do that here to save GSettings
690 len = strlen (state->l10n_context);
691 state->l10n_context[len] = '\004';
692 g_string_prepend_len (state->unparsed_default_value,
693 state->l10n_context, len + 1);
694 g_free (state->l10n_context);
695 state->l10n_context = NULL;
698 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
699 state->unparsed_default_value->str);
700 g_string_free (state->unparsed_default_value, TRUE);
701 state->unparsed_default_value = NULL;
704 /* choice, aliases, enums */
705 if (state->strinfo->len)
713 size = state->strinfo->len;
714 data = g_string_free_and_steal (g_steal_pointer (&state->strinfo));
717 for (i = 0; i < size / sizeof (guint32); i++)
718 words[i] = GUINT32_TO_LE (words[i]);
720 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
724 g_variant_builder_add (&builder, "(y@au)",
725 state->is_flags ? 'f' :
726 state->is_enum ? 'e' : 'c',
731 if (state->minimum || state->maximum)
732 g_variant_builder_add (&builder, "(y(**))", 'r',
733 state->minimum, state->maximum);
735 /* per-desktop overrides */
736 if (state->desktop_overrides)
737 g_variant_builder_add (&builder, "(y@a{sv})", 'd',
738 g_variant_dict_end (state->desktop_overrides));
740 state->serialised = g_variant_builder_end (&builder);
743 g_variant_ref_sink (state->serialised);
746 return g_variant_ref (state->serialised);
750 key_state_free (gpointer data)
752 KeyState *state = data;
754 g_free (state->child_schema);
757 g_variant_type_free (state->type);
759 g_free (state->l10n_context);
761 if (state->unparsed_default_value)
762 g_string_free (state->unparsed_default_value, TRUE);
764 if (state->default_value)
765 g_variant_unref (state->default_value);
768 g_string_free (state->strinfo, TRUE);
771 g_variant_unref (state->minimum);
774 g_variant_unref (state->maximum);
776 if (state->serialised)
777 g_variant_unref (state->serialised);
779 if (state->desktop_overrides)
780 g_variant_dict_unref (state->desktop_overrides);
782 g_slice_free (KeyState, state);
785 /* Key name validity {{{1 */
786 static gboolean allow_any_name = FALSE;
789 is_valid_keyname (const gchar *key,
796 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
797 _("Empty names are not permitted"));
804 if (!g_ascii_islower (key[0]))
806 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
807 _("Invalid name “%s”: names must begin "
808 "with a lowercase letter"), key);
812 for (i = 1; key[i]; i++)
815 !g_ascii_islower (key[i]) &&
816 !g_ascii_isdigit (key[i]))
818 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
819 _("Invalid name “%s”: invalid character “%c”; "
820 "only lowercase letters, numbers and hyphen (“-”) "
821 "are permitted"), key, key[i]);
825 if (key[i] == '-' && key[i + 1] == '-')
827 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
828 _("Invalid name “%s”: two successive hyphens (“--”) "
829 "are not permitted"), key);
834 if (key[i - 1] == '-')
836 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
837 _("Invalid name “%s”: the last character may not be a "
838 "hyphen (“-”)"), key);
844 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
845 _("Invalid name “%s”: maximum length is 1024"), key);
852 /* Handling of <schema> {{{1 */
853 typedef struct _SchemaState SchemaState;
856 SchemaState *extends;
859 gchar *gettext_domain;
867 schema_state_new (const gchar *path,
868 const gchar *gettext_domain,
869 SchemaState *extends,
870 const gchar *extends_name,
871 const gchar *list_of)
875 state = g_slice_new (SchemaState);
876 state->path = g_strdup (path);
877 state->gettext_domain = g_strdup (gettext_domain);
878 state->extends = extends;
879 state->extends_name = g_strdup (extends_name);
880 state->list_of = g_strdup (list_of);
881 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
882 g_free, key_state_free);
888 schema_state_free (gpointer data)
890 SchemaState *state = data;
892 g_free (state->path);
893 g_free (state->gettext_domain);
894 g_free (state->extends_name);
895 g_free (state->list_of);
896 g_hash_table_unref (state->keys);
897 g_slice_free (SchemaState, state);
901 schema_state_add_child (SchemaState *state,
908 if (!is_valid_keyname (name, error))
911 childname = g_strconcat (name, "/", NULL);
913 if (g_hash_table_lookup (state->keys, childname))
915 g_set_error (error, G_MARKUP_ERROR,
916 G_MARKUP_ERROR_INVALID_CONTENT,
917 _("<child name='%s'> already specified"), name);
921 g_hash_table_insert (state->keys, childname,
922 key_state_new_child (schema));
926 schema_state_add_key (SchemaState *state,
927 GHashTable *enum_table,
928 GHashTable *flags_table,
930 const gchar *type_string,
931 const gchar *enum_type,
932 const gchar *flags_type,
941 g_set_error_literal (error, G_MARKUP_ERROR,
942 G_MARKUP_ERROR_INVALID_CONTENT,
943 _("Cannot add keys to a “list-of” schema"));
947 if (!is_valid_keyname (name, error))
950 if (g_hash_table_lookup (state->keys, name))
952 g_set_error (error, G_MARKUP_ERROR,
953 G_MARKUP_ERROR_INVALID_CONTENT,
954 _("<key name='%s'> already specified"), name);
958 for (node = state; node; node = node->extends)
963 shadow = g_hash_table_lookup (node->extends->keys, name);
965 /* in case of <key> <override> <key> make sure we report the
966 * location of the original <key>, not the <override>.
968 if (shadow && !shadow->is_override)
970 g_set_error (error, G_MARKUP_ERROR,
971 G_MARKUP_ERROR_INVALID_CONTENT,
972 _("<key name='%s'> shadows <key name='%s'> in "
973 "<schema id='%s'>; use <override> to modify value"),
974 name, name, node->extends_name);
979 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
981 g_set_error (error, G_MARKUP_ERROR,
982 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
983 _("Exactly one of “type”, “enum” or “flags” must "
984 "be specified as an attribute to <key>"));
988 if (type_string == NULL) /* flags or enums was specified */
990 EnumState *enum_state;
993 enum_state = g_hash_table_lookup (enum_table, enum_type);
995 enum_state = g_hash_table_lookup (flags_table, flags_type);
998 if (enum_state == NULL)
1000 g_set_error (error, G_MARKUP_ERROR,
1001 G_MARKUP_ERROR_INVALID_CONTENT,
1002 _("<%s id='%s'> not (yet) defined."),
1003 flags_type ? "flags" : "enum",
1004 flags_type ? flags_type : enum_type);
1008 type_string = flags_type ? "as" : "s";
1009 strinfo = enum_state->strinfo;
1013 if (!g_variant_type_string_is_valid (type_string))
1015 g_set_error (error, G_MARKUP_ERROR,
1016 G_MARKUP_ERROR_INVALID_CONTENT,
1017 _("Invalid GVariant type string “%s”"), type_string);
1024 key = key_state_new (type_string, state->gettext_domain,
1025 enum_type != NULL, flags_type != NULL, strinfo);
1026 g_hash_table_insert (state->keys, g_strdup (name), key);
1032 schema_state_add_override (SchemaState *state,
1033 KeyState **key_state,
1037 const gchar *context,
1040 SchemaState *parent;
1041 KeyState *original = NULL;
1043 if (state->extends == NULL)
1045 g_set_error_literal (error, G_MARKUP_ERROR,
1046 G_MARKUP_ERROR_INVALID_CONTENT,
1047 _("<override> given but schema isn’t "
1048 "extending anything"));
1052 for (parent = state->extends; parent; parent = parent->extends)
1053 if ((original = g_hash_table_lookup (parent->keys, key)))
1056 if (original == NULL)
1058 g_set_error (error, G_MARKUP_ERROR,
1059 G_MARKUP_ERROR_INVALID_CONTENT,
1060 _("No <key name='%s'> to override"), key);
1064 if (g_hash_table_lookup (state->keys, key))
1066 g_set_error (error, G_MARKUP_ERROR,
1067 G_MARKUP_ERROR_INVALID_CONTENT,
1068 _("<override name='%s'> already specified"), key);
1072 *key_state = key_state_override (original, state->gettext_domain);
1073 *string = key_state_start_default (*key_state, l10n, context, error);
1074 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1078 override_state_end (KeyState **key_state,
1082 key_state_end_default (*key_state, string, error);
1086 /* Handling of toplevel state {{{1 */
1089 gboolean strict; /* TRUE if --strict was given */
1091 GHashTable *schema_table; /* string -> SchemaState */
1092 GHashTable *flags_table; /* string -> EnumState */
1093 GHashTable *enum_table; /* string -> EnumState */
1095 GSList *this_file_schemas; /* strings: <schema>s in this file */
1096 GSList *this_file_flagss; /* strings: <flags>s in this file */
1097 GSList *this_file_enums; /* strings: <enum>s in this file */
1099 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1101 SchemaState *schema_state; /* non-NULL when inside <schema> */
1102 KeyState *key_state; /* non-NULL when inside <key> */
1103 EnumState *enum_state; /* non-NULL when inside <enum> */
1105 GString *string; /* non-NULL when accepting text */
1109 is_subclass (const gchar *class_name,
1110 const gchar *possible_parent,
1111 GHashTable *schema_table)
1115 if (strcmp (class_name, possible_parent) == 0)
1118 class = g_hash_table_lookup (schema_table, class_name);
1119 g_assert (class != NULL);
1121 return class->extends_name &&
1122 is_subclass (class->extends_name, possible_parent, schema_table);
1126 parse_state_start_schema (ParseState *state,
1129 const gchar *gettext_domain,
1130 const gchar *extends_name,
1131 const gchar *list_of,
1134 SchemaState *extends;
1137 if (g_hash_table_lookup (state->schema_table, id))
1139 g_set_error (error, G_MARKUP_ERROR,
1140 G_MARKUP_ERROR_INVALID_CONTENT,
1141 _("<schema id='%s'> already specified"), id);
1147 extends = g_hash_table_lookup (state->schema_table, extends_name);
1149 if (extends == NULL)
1151 g_set_error (error, G_MARKUP_ERROR,
1152 G_MARKUP_ERROR_INVALID_CONTENT,
1153 _("<schema id='%s'> extends not yet existing "
1154 "schema “%s”"), id, extends_name);
1165 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1167 g_set_error (error, G_MARKUP_ERROR,
1168 G_MARKUP_ERROR_INVALID_CONTENT,
1169 _("<schema id='%s'> is list of not yet existing "
1170 "schema “%s”"), id, list_of);
1176 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1177 _("Cannot be a list of a schema with a path"));
1186 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1187 _("Cannot extend a schema with a path"));
1193 if (extends->list_of == NULL)
1195 g_set_error (error, G_MARKUP_ERROR,
1196 G_MARKUP_ERROR_INVALID_CONTENT,
1197 _("<schema id='%s'> is a list, extending "
1198 "<schema id='%s'> which is not a list"),
1203 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1205 g_set_error (error, G_MARKUP_ERROR,
1206 G_MARKUP_ERROR_INVALID_CONTENT,
1207 _("<schema id='%s' list-of='%s'> extends <schema "
1208 "id='%s' list-of='%s'> but “%s” does not "
1209 "extend “%s”"), id, list_of, extends_name,
1210 extends->list_of, list_of, extends->list_of);
1215 /* by default we are a list of the same thing that the schema
1216 * we are extending is a list of (which might be nothing)
1218 list_of = extends->list_of;
1221 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1223 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1224 _("A path, if given, must begin and end with a slash"));
1228 if (path && list_of && !g_str_has_suffix (path, ":/"))
1230 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1231 _("The path of a list must end with “:/”"));
1235 if (path && (g_str_has_prefix (path, "/apps/") ||
1236 g_str_has_prefix (path, "/desktop/") ||
1237 g_str_has_prefix (path, "/system/")))
1239 gchar *message = NULL;
1240 message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1241 "Paths starting with "
1242 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1244 g_printerr ("%s\n", message);
1248 state->schema_state = schema_state_new (path, gettext_domain,
1249 extends, extends_name, list_of);
1251 my_id = g_strdup (id);
1252 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1253 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1257 parse_state_start_enum (ParseState *state,
1262 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1263 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1266 if (g_hash_table_lookup (table, id))
1268 g_set_error (error, G_MARKUP_ERROR,
1269 G_MARKUP_ERROR_INVALID_CONTENT,
1270 _("<%s id='%s'> already specified"),
1271 is_flags ? "flags" : "enum", id);
1275 state->enum_state = enum_state_new (is_flags);
1277 my_id = g_strdup (id);
1278 *list = g_slist_prepend (*list, my_id);
1279 g_hash_table_insert (table, my_id, state->enum_state);
1282 /* GMarkup Parser Functions {{{1 */
1284 /* Start element {{{2 */
1286 start_element (GMarkupParseContext *context,
1287 const gchar *element_name,
1288 const gchar **attribute_names,
1289 const gchar **attribute_values,
1293 ParseState *state = user_data;
1294 const GSList *element_stack;
1295 const gchar *container;
1297 element_stack = g_markup_parse_context_get_element_stack (context);
1298 container = element_stack->next ? element_stack->next->data : NULL;
1300 #define COLLECT(first, ...) \
1301 g_markup_collect_attributes (element_name, \
1302 attribute_names, attribute_values, error, \
1303 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1304 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1305 #define STRDUP G_MARKUP_COLLECT_STRDUP
1306 #define STRING G_MARKUP_COLLECT_STRING
1307 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1309 /* Toplevel items {{{3 */
1310 if (container == NULL)
1312 if (strcmp (element_name, "schemalist") == 0)
1314 COLLECT (OPTIONAL | STRDUP,
1316 &state->schemalist_domain);
1322 /* children of <schemalist> {{{3 */
1323 else if (strcmp (container, "schemalist") == 0)
1325 if (strcmp (element_name, "schema") == 0)
1327 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1328 if (COLLECT (STRING, "id", &id,
1329 OPTIONAL | STRING, "path", &path,
1330 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1331 OPTIONAL | STRING, "extends", &extends,
1332 OPTIONAL | STRING, "list-of", &list_of))
1333 parse_state_start_schema (state, id, path,
1334 gettext_domain ? gettext_domain
1335 : state->schemalist_domain,
1336 extends, list_of, error);
1340 else if (strcmp (element_name, "enum") == 0)
1343 if (COLLECT (STRING, "id", &id))
1344 parse_state_start_enum (state, id, FALSE, error);
1348 else if (strcmp (element_name, "flags") == 0)
1351 if (COLLECT (STRING, "id", &id))
1352 parse_state_start_enum (state, id, TRUE, error);
1358 /* children of <schema> {{{3 */
1359 else if (strcmp (container, "schema") == 0)
1361 if (strcmp (element_name, "key") == 0)
1363 const gchar *name, *type_string, *enum_type, *flags_type;
1365 if (COLLECT (STRING, "name", &name,
1366 OPTIONAL | STRING, "type", &type_string,
1367 OPTIONAL | STRING, "enum", &enum_type,
1368 OPTIONAL | STRING, "flags", &flags_type))
1370 state->key_state = schema_state_add_key (state->schema_state,
1374 enum_type, flags_type,
1378 else if (strcmp (element_name, "child") == 0)
1380 const gchar *name, *schema;
1382 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1383 schema_state_add_child (state->schema_state,
1384 name, schema, error);
1387 else if (strcmp (element_name, "override") == 0)
1389 const gchar *name, *l10n, *str_context;
1391 if (COLLECT (STRING, "name", &name,
1392 OPTIONAL | STRING, "l10n", &l10n,
1393 OPTIONAL | STRING, "context", &str_context))
1394 schema_state_add_override (state->schema_state,
1395 &state->key_state, &state->string,
1396 name, l10n, str_context, error);
1401 /* children of <key> {{{3 */
1402 else if (strcmp (container, "key") == 0)
1404 if (strcmp (element_name, "default") == 0)
1406 const gchar *l10n, *str_context;
1407 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1408 STRING | OPTIONAL, "context", &str_context))
1409 state->string = key_state_start_default (state->key_state,
1410 l10n, str_context, error);
1414 else if (strcmp (element_name, "summary") == 0)
1418 if (state->key_state->summary_seen && state->strict)
1419 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1420 _("Only one <%s> element allowed inside <%s>"),
1421 element_name, container);
1423 state->string = g_string_new (NULL);
1425 state->key_state->summary_seen = TRUE;
1430 else if (strcmp (element_name, "description") == 0)
1434 if (state->key_state->description_seen && state->strict)
1435 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1436 _("Only one <%s> element allowed inside <%s>"),
1437 element_name, container);
1439 state->string = g_string_new (NULL);
1441 state->key_state->description_seen = TRUE;
1446 else if (strcmp (element_name, "range") == 0)
1448 const gchar *min, *max;
1449 if (COLLECT (STRING | OPTIONAL, "min", &min,
1450 STRING | OPTIONAL, "max", &max))
1451 key_state_set_range (state->key_state, min, max, error);
1455 else if (strcmp (element_name, "choices") == 0)
1458 key_state_start_choices (state->key_state, error);
1462 else if (strcmp (element_name, "aliases") == 0)
1465 key_state_start_aliases (state->key_state, error);
1471 /* children of <choices> {{{3 */
1472 else if (strcmp (container, "choices") == 0)
1474 if (strcmp (element_name, "choice") == 0)
1477 if (COLLECT (STRING, "value", &value))
1478 key_state_add_choice (state->key_state, value, error);
1484 /* children of <aliases> {{{3 */
1485 else if (strcmp (container, "aliases") == 0)
1487 if (strcmp (element_name, "alias") == 0)
1489 const gchar *value, *target;
1490 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1491 key_state_add_alias (state->key_state, value, target, error);
1497 /* children of <enum> {{{3 */
1498 else if (strcmp (container, "enum") == 0 ||
1499 strcmp (container, "flags") == 0)
1501 if (strcmp (element_name, "value") == 0)
1503 const gchar *nick, *valuestr;
1504 if (COLLECT (STRING, "nick", &nick,
1505 STRING, "value", &valuestr))
1506 enum_state_add_value (state->enum_state, nick, valuestr, error);
1513 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1514 _("Element <%s> not allowed inside <%s>"),
1515 element_name, container);
1517 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1518 _("Element <%s> not allowed at the top level"), element_name);
1521 /* End element {{{2 */
1524 key_state_end (KeyState **state_ptr,
1532 if (state->default_value == NULL)
1534 g_set_error_literal (error,
1535 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1536 _("Element <default> is required in <key>"));
1542 schema_state_end (SchemaState **state_ptr,
1549 end_element (GMarkupParseContext *context,
1550 const gchar *element_name,
1554 ParseState *state = user_data;
1556 if (strcmp (element_name, "schemalist") == 0)
1558 g_free (state->schemalist_domain);
1559 state->schemalist_domain = NULL;
1562 else if (strcmp (element_name, "enum") == 0 ||
1563 strcmp (element_name, "flags") == 0)
1564 enum_state_end (&state->enum_state, error);
1566 else if (strcmp (element_name, "schema") == 0)
1567 schema_state_end (&state->schema_state, error);
1569 else if (strcmp (element_name, "override") == 0)
1570 override_state_end (&state->key_state, &state->string, error);
1572 else if (strcmp (element_name, "key") == 0)
1573 key_state_end (&state->key_state, error);
1575 else if (strcmp (element_name, "default") == 0)
1576 key_state_end_default (state->key_state, &state->string, error);
1578 else if (strcmp (element_name, "choices") == 0)
1579 key_state_end_choices (state->key_state, error);
1581 else if (strcmp (element_name, "aliases") == 0)
1582 key_state_end_aliases (state->key_state, error);
1586 g_string_free (state->string, TRUE);
1587 state->string = NULL;
1592 text (GMarkupParseContext *context,
1598 ParseState *state = user_data;
1602 /* we are expecting a string, so store the text data.
1604 * we store the data verbatim here and deal with whitespace
1605 * later on. there are two reasons for that:
1607 * 1) whitespace is handled differently depending on the tag
1610 * 2) we could do leading whitespace removal by refusing to
1611 * insert it into state->string if it's at the start, but for
1612 * trailing whitespace, we have no idea if there is another
1613 * text() call coming or not.
1615 g_string_append_len (state->string, text, text_len);
1619 /* string is not expected: accept (and ignore) pure whitespace */
1622 for (i = 0; i < text_len; i++)
1623 if (!g_ascii_isspace (text[i]))
1625 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1626 _("Text may not appear inside <%s>"),
1627 g_markup_parse_context_get_element (context));
1633 /* Write to GVDB {{{1 */
1641 gvdb_pair_init (GvdbPair *pair)
1643 pair->table = gvdb_hash_table_new (NULL, NULL);
1644 pair->root = gvdb_hash_table_insert (pair->table, "");
1648 gvdb_pair_clear (GvdbPair *pair)
1650 g_hash_table_unref (pair->table);
1655 GHashTable *schema_table;
1661 GHashTable *schema_table;
1667 output_key (gpointer key,
1671 OutputSchemaData *data;
1675 GVariant *serialised = NULL;
1681 item = gvdb_hash_table_insert (data->pair.table, name);
1682 gvdb_item_set_parent (item, data->pair.root);
1683 serialised = key_state_serialise (state);
1684 gvdb_item_set_value (item, serialised);
1685 g_variant_unref (serialised);
1690 if (state->child_schema &&
1691 !g_hash_table_lookup (data->schema_table, state->child_schema))
1693 gchar *message = NULL;
1694 message = g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1695 state->child_schema);
1696 g_printerr ("%s\n", message);
1702 output_schema (gpointer key,
1706 WriteToFileData *wtf_data = user_data;
1707 OutputSchemaData data;
1708 GvdbPair *root_pair;
1715 root_pair = &wtf_data->root_pair;
1717 data.schema_table = wtf_data->schema_table;
1718 gvdb_pair_init (&data.pair);
1721 item = gvdb_hash_table_insert (root_pair->table, id);
1722 gvdb_item_set_parent (item, root_pair->root);
1723 gvdb_item_set_hash_table (item, data.pair.table);
1725 g_hash_table_foreach (state->keys, output_key, &data);
1728 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1730 if (state->extends_name)
1731 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1732 state->extends_name);
1735 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1739 gvdb_hash_table_insert_string (data.pair.table,
1741 state->gettext_domain);
1743 gvdb_pair_clear (&data.pair);
1747 write_to_file (GHashTable *schema_table,
1748 const gchar *filename,
1751 WriteToFileData data;
1754 data.schema_table = schema_table;
1756 gvdb_pair_init (&data.root_pair);
1758 g_hash_table_foreach (schema_table, output_schema, &data);
1760 success = gvdb_table_write_contents (data.root_pair.table, filename,
1761 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1763 g_hash_table_unref (data.root_pair.table);
1768 /* Parser driver {{{1 */
1770 parse_gschema_files (gchar **files,
1773 GMarkupParser parser = { start_element, end_element, text, NULL, NULL };
1774 ParseState state = { 0, };
1775 const gchar *filename;
1776 GError *error = NULL;
1778 state.strict = strict;
1780 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1781 g_free, enum_state_free);
1783 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1784 g_free, enum_state_free);
1786 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1787 g_free, schema_state_free);
1789 while ((filename = *files++) != NULL)
1791 GMarkupParseContext *context;
1796 if (!g_file_get_contents (filename, &contents, &size, &error))
1798 fprintf (stderr, "%s\n", error->message);
1799 g_clear_error (&error);
1803 context = g_markup_parse_context_new (&parser,
1804 G_MARKUP_TREAT_CDATA_AS_TEXT |
1805 G_MARKUP_PREFIX_ERROR_POSITION |
1806 G_MARKUP_IGNORE_QUALIFIED,
1810 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1811 !g_markup_parse_context_end_parse (context, &error))
1815 /* back out any changes from this file */
1816 for (item = state.this_file_schemas; item; item = item->next)
1817 g_hash_table_remove (state.schema_table, item->data);
1819 for (item = state.this_file_flagss; item; item = item->next)
1820 g_hash_table_remove (state.flags_table, item->data);
1822 for (item = state.this_file_enums; item; item = item->next)
1823 g_hash_table_remove (state.enum_table, item->data);
1826 g_markup_parse_context_get_position (context, &line, &col);
1827 fprintf (stderr, "%s:%d:%d %s. ", filename, line, col, error->message);
1828 g_clear_error (&error);
1832 /* Translators: Do not translate "--strict". */
1833 fprintf (stderr, "%s\n", _("--strict was specified; exiting."));
1835 g_hash_table_unref (state.schema_table);
1836 g_hash_table_unref (state.flags_table);
1837 g_hash_table_unref (state.enum_table);
1845 fprintf (stderr, "%s\n", _("This entire file has been ignored."));
1851 g_markup_parse_context_free (context);
1852 g_slist_free (state.this_file_schemas);
1853 g_slist_free (state.this_file_flagss);
1854 g_slist_free (state.this_file_enums);
1855 state.this_file_schemas = NULL;
1856 state.this_file_flagss = NULL;
1857 state.this_file_enums = NULL;
1860 g_hash_table_unref (state.flags_table);
1861 g_hash_table_unref (state.enum_table);
1863 return state.schema_table;
1867 compare_strings (gconstpointer a,
1870 const gchar *one = a;
1871 const gchar *two = b;
1874 cmp = g_str_has_suffix (two, ".enums.xml") -
1875 g_str_has_suffix (one, ".enums.xml");
1878 cmp = strcmp (one, two);
1884 set_overrides (GHashTable *schema_table,
1888 const gchar *filename;
1889 GError *error = NULL;
1891 while ((filename = *files++))
1897 g_debug ("Processing override file '%s'", filename);
1899 key_file = g_key_file_new ();
1900 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1902 fprintf (stderr, "%s: %s. ", filename, error->message);
1903 g_key_file_free (key_file);
1904 g_clear_error (&error);
1908 fprintf (stderr, "%s\n", _("Ignoring this file."));
1912 fprintf (stderr, "%s\n", _("--strict was specified; exiting."));
1916 groups = g_key_file_get_groups (key_file, NULL);
1918 for (i = 0; groups[i]; i++)
1920 const gchar *group = groups[i];
1921 const gchar *schema_name;
1922 const gchar *desktop_id;
1923 SchemaState *schema;
1928 pieces = g_strsplit (group, ":", 2);
1929 schema_name = pieces[0];
1930 desktop_id = pieces[1];
1932 g_debug ("Processing group '%s' (schema '%s', %s)",
1933 group, schema_name, desktop_id ? desktop_id : "all desktops");
1935 schema = g_hash_table_lookup (schema_table, schema_name);
1939 /* Having the schema not be installed is expected to be a
1940 * common case. Don't even emit an error message about
1943 g_strfreev (pieces);
1947 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1948 g_assert (keys != NULL);
1950 for (j = 0; keys[j]; j++)
1952 const gchar *key = keys[j];
1957 state = g_hash_table_lookup (schema->keys, key);
1963 fprintf (stderr, _("No such key “%s” in schema “%s” as "
1964 "specified in override file “%s”; "
1965 "ignoring override for this key."),
1966 key, group, filename);
1967 fprintf (stderr, "\n");
1971 fprintf (stderr, _("No such key “%s” in schema “%s” as "
1972 "specified in override file “%s” and "
1973 "--strict was specified; exiting."),
1974 key, group, filename);
1975 fprintf (stderr, "\n");
1977 g_key_file_free (key_file);
1978 g_strfreev (pieces);
1979 g_strfreev (groups);
1985 if (desktop_id != NULL && state->l10n)
1987 /* Let's avoid the n*m case of per-desktop localised
1988 * default values, and just forbid it.
1993 _("Cannot provide per-desktop overrides for "
1994 "localized key “%s” in schema “%s” (override "
1995 "file “%s”); ignoring override for this key."),
1996 key, group, filename);
1997 fprintf (stderr, "\n");
2002 _("Cannot provide per-desktop overrides for "
2003 "localized key “%s” in schema “%s” (override "
2004 "file “%s”) and --strict was specified; exiting."),
2005 key, group, filename);
2006 fprintf (stderr, "\n");
2008 g_key_file_free (key_file);
2009 g_strfreev (pieces);
2010 g_strfreev (groups);
2016 string = g_key_file_get_value (key_file, group, key, NULL);
2017 g_assert (string != NULL);
2019 value = g_variant_parse (state->type, string,
2020 NULL, NULL, &error);
2026 fprintf (stderr, _("Error parsing key “%s” in schema “%s” "
2027 "as specified in override file “%s”: "
2028 "%s. Ignoring override for this key."),
2029 key, group, filename, error->message);
2030 fprintf (stderr, "\n");
2032 g_clear_error (&error);
2038 fprintf (stderr, _("Error parsing key “%s” in schema “%s” "
2039 "as specified in override file “%s”: "
2040 "%s. --strict was specified; exiting."),
2041 key, group, filename, error->message);
2042 fprintf (stderr, "\n");
2044 g_clear_error (&error);
2046 g_key_file_free (key_file);
2047 g_strfreev (pieces);
2048 g_strfreev (groups);
2056 if (g_variant_compare (value, state->minimum) < 0 ||
2057 g_variant_compare (value, state->maximum) > 0)
2059 g_variant_unref (value);
2065 _("Override for key “%s” in schema “%s” in "
2066 "override file “%s” is outside the range "
2067 "given in the schema; ignoring override "
2069 key, group, filename);
2070 fprintf (stderr, "\n");
2075 _("Override for key “%s” in schema “%s” in "
2076 "override file “%s” is outside the range "
2077 "given in the schema and --strict was "
2078 "specified; exiting."),
2079 key, group, filename);
2080 fprintf (stderr, "\n");
2082 g_key_file_free (key_file);
2083 g_strfreev (pieces);
2084 g_strfreev (groups);
2091 else if (state->strinfo->len)
2093 if (!is_valid_choices (value, state->strinfo))
2095 g_variant_unref (value);
2101 _("Override for key “%s” in schema “%s” in "
2102 "override file “%s” is not in the list "
2103 "of valid choices; ignoring override for "
2105 key, group, filename);
2106 fprintf (stderr, "\n");
2111 _("Override for key “%s” in schema “%s” in "
2112 "override file “%s” is not in the list "
2113 "of valid choices and --strict was specified; "
2115 key, group, filename);
2116 fprintf (stderr, "\n");
2117 g_key_file_free (key_file);
2118 g_strfreev (pieces);
2119 g_strfreev (groups);
2126 if (desktop_id != NULL)
2128 if (state->desktop_overrides == NULL)
2129 state->desktop_overrides = g_variant_dict_new (NULL);
2131 g_variant_dict_insert_value (state->desktop_overrides, desktop_id, value);
2132 g_variant_unref (value);
2136 g_variant_unref (state->default_value);
2137 state->default_value = value;
2143 g_strfreev (pieces);
2147 g_strfreev (groups);
2148 g_key_file_free (key_file);
2155 main (int argc, char **argv)
2157 GError *error = NULL;
2158 GHashTable *table = NULL;
2161 const gchar *srcdir;
2162 gboolean show_version_and_exit = FALSE;
2163 gchar *targetdir = NULL;
2164 gchar *target = NULL;
2165 gboolean dry_run = FALSE;
2166 gboolean strict = FALSE;
2167 gchar **schema_files = NULL;
2168 gchar **override_files = NULL;
2169 GOptionContext *context = NULL;
2171 GOptionEntry entries[] = {
2172 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
2173 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("Where to store the gschemas.compiled file"), N_("DIRECTORY") },
2174 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2175 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2176 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions"), NULL },
2178 /* These options are only for use in the gschema-compile tests */
2179 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2180 { "override-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &override_files, NULL, NULL },
2186 gchar **command_line = NULL;
2189 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
2190 textdomain (GETTEXT_PACKAGE);
2193 tmp = _glib_get_locale_dir ();
2194 bindtextdomain (GETTEXT_PACKAGE, tmp);
2196 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2199 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2200 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2203 context = g_option_context_new (N_("DIRECTORY"));
2204 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2205 g_option_context_set_summary (context,
2206 N_("Compile all GSettings schema files into a schema cache.\n"
2207 "Schema files are required to have the extension .gschema.xml,\n"
2208 "and the cache file is called gschemas.compiled."));
2209 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2212 command_line = g_win32_get_command_line ();
2213 if (!g_option_context_parse_strv (context, &command_line, &error))
2215 fprintf (stderr, "%s\n", error->message);
2219 argc = g_strv_length (command_line);
2221 if (!g_option_context_parse (context, &argc, &argv, &error))
2223 fprintf (stderr, "%s\n", error->message);
2229 if (show_version_and_exit)
2231 g_print (PACKAGE_VERSION "\n");
2236 if (!schema_files && argc != 2)
2238 fprintf (stderr, "%s\n", _("You should give exactly one directory name"));
2244 srcdir = command_line[1];
2249 target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
2253 GPtrArray *overrides;
2256 files = g_ptr_array_new ();
2257 overrides = g_ptr_array_new ();
2259 dir = g_dir_open (srcdir, 0, &error);
2262 fprintf (stderr, "%s\n", error->message);
2264 g_ptr_array_unref (files);
2265 g_ptr_array_unref (overrides);
2271 while ((file = g_dir_read_name (dir)) != NULL)
2273 if (g_str_has_suffix (file, ".gschema.xml") ||
2274 g_str_has_suffix (file, ".enums.xml"))
2275 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2277 else if (g_str_has_suffix (file, ".gschema.override"))
2278 g_ptr_array_add (overrides,
2279 g_build_filename (srcdir, file, NULL));
2282 if (files->len == 0)
2284 if (g_unlink (target))
2285 fprintf (stdout, "%s\n", _("No schema files found: doing nothing."));
2287 fprintf (stdout, "%s\n", _("No schema files found: removed existing output file."));
2289 g_ptr_array_unref (files);
2290 g_ptr_array_unref (overrides);
2295 g_ptr_array_sort_values (files, compare_strings);
2296 g_ptr_array_add (files, NULL);
2298 g_ptr_array_sort_values (overrides, compare_strings);
2299 g_ptr_array_add (overrides, NULL);
2301 schema_files = (char **) g_ptr_array_free (files, FALSE);
2302 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2305 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2311 if (override_files != NULL &&
2312 !set_overrides (table, override_files, strict))
2318 if (!dry_run && !write_to_file (table, target, &error))
2320 fprintf (stderr, "%s\n", error->message);
2329 g_clear_error (&error);
2330 g_clear_pointer (&table, g_hash_table_unref);
2331 g_clear_pointer (&dir, g_dir_close);
2334 g_strfreev (schema_files);
2335 g_strfreev (override_files);
2336 g_option_context_free (context);
2340 g_strfreev (command_line);
2348 /* vim:set foldmethod=marker: */