2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * 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/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "gvdb/gvdb-builder.h"
34 #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;
189 gboolean has_choices;
190 gboolean has_aliases;
191 gboolean is_override;
194 GVariant *serialised;
198 key_state_new (const gchar *type_string,
199 const gchar *gettext_domain,
206 state = g_slice_new0 (KeyState);
207 state->type = g_variant_type_new (type_string);
208 state->have_gettext_domain = gettext_domain != NULL;
209 state->is_enum = is_enum;
210 state->is_flags = is_flags;
213 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
215 state->strinfo = g_string_new (NULL);
221 key_state_override (KeyState *state,
222 const gchar *gettext_domain)
226 copy = g_slice_new0 (KeyState);
227 copy->type = g_variant_type_copy (state->type);
228 copy->have_gettext_domain = gettext_domain != NULL;
229 copy->strinfo = g_string_new_len (state->strinfo->str,
230 state->strinfo->len);
231 copy->is_enum = state->is_enum;
232 copy->is_flags = state->is_flags;
233 copy->is_override = TRUE;
237 copy->minimum = g_variant_ref (state->minimum);
238 copy->maximum = g_variant_ref (state->maximum);
245 key_state_new_child (const gchar *child_schema)
249 state = g_slice_new0 (KeyState);
250 state->child_schema = g_strdup (child_schema);
256 is_valid_choices (GVariant *variant,
259 switch (g_variant_classify (variant))
261 case G_VARIANT_CLASS_MAYBE:
262 case G_VARIANT_CLASS_ARRAY:
264 gboolean valid = TRUE;
267 g_variant_iter_init (&iter, variant);
269 while (valid && (variant = g_variant_iter_next_value (&iter)))
271 valid = is_valid_choices (variant, strinfo);
272 g_variant_unref (variant);
278 case G_VARIANT_CLASS_STRING:
279 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
281 g_variant_get_string (variant, NULL));
284 g_assert_not_reached ();
289 /* Gets called at </default> </choices> or <range/> to check for
290 * validity of the default value so that any inconsistency is
291 * reported as soon as it is encountered.
294 key_state_check_range (KeyState *state,
297 if (state->default_value)
301 tag = state->is_override ? "override" : "default";
305 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
306 g_variant_compare (state->default_value, state->maximum) > 0)
308 g_set_error (error, G_MARKUP_ERROR,
309 G_MARKUP_ERROR_INVALID_CONTENT,
310 "<%s> is not contained in "
311 "the specified range", tag);
315 else if (state->strinfo->len)
317 if (!is_valid_choices (state->default_value, state->strinfo))
320 g_set_error (error, G_MARKUP_ERROR,
321 G_MARKUP_ERROR_INVALID_CONTENT,
322 "<%s> is not a valid member of "
323 "the specified enumerated type", tag);
325 else if (state->is_flags)
326 g_set_error (error, G_MARKUP_ERROR,
327 G_MARKUP_ERROR_INVALID_CONTENT,
328 "<%s> contains string not in the "
329 "specified flags type", tag);
332 g_set_error (error, G_MARKUP_ERROR,
333 G_MARKUP_ERROR_INVALID_CONTENT,
334 "<%s> contains string not in "
342 key_state_set_range (KeyState *state,
343 const gchar *min_str,
344 const gchar *max_str,
353 { 'n', "-32768", "32767" },
354 { 'q', "0", "65535" },
355 { 'i', "-2147483648", "2147483647" },
356 { 'u', "0", "4294967295" },
357 { 'x', "-9223372036854775808", "9223372036854775807" },
358 { 't', "0", "18446744073709551615" },
359 { 'd', "-inf", "inf" },
361 gboolean type_ok = FALSE;
366 g_set_error_literal (error, G_MARKUP_ERROR,
367 G_MARKUP_ERROR_INVALID_CONTENT,
368 "<range/> already specified for this key");
372 for (i = 0; i < G_N_ELEMENTS (table); i++)
373 if (*(char *) state->type == table[i].type)
375 min_str = min_str ? min_str : table[i].min;
376 max_str = max_str ? max_str : table[i].max;
383 gchar *type = g_variant_type_dup_string (state->type);
384 g_set_error (error, G_MARKUP_ERROR,
385 G_MARKUP_ERROR_INVALID_CONTENT,
386 "<range> not allowed for keys of type '%s'", type);
391 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
392 if (state->minimum == NULL)
395 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
396 if (state->maximum == NULL)
399 if (g_variant_compare (state->minimum, state->maximum) > 0)
401 g_set_error (error, G_MARKUP_ERROR,
402 G_MARKUP_ERROR_INVALID_CONTENT,
403 "<range> specified minimum is greater than maxmimum");
407 key_state_check_range (state, error);
411 key_state_start_default (KeyState *state,
413 const gchar *context,
418 if (strcmp (l10n, "messages") == 0)
421 else if (strcmp (l10n, "time") == 0)
426 g_set_error (error, G_MARKUP_ERROR,
427 G_MARKUP_ERROR_INVALID_CONTENT,
428 "unsupported l10n category: %s", l10n);
432 if (!state->have_gettext_domain)
434 g_set_error_literal (error, G_MARKUP_ERROR,
435 G_MARKUP_ERROR_INVALID_CONTENT,
436 "l10n requested, but no "
437 "gettext domain given");
441 state->l10n_context = g_strdup (context);
444 else if (context != NULL)
446 g_set_error_literal (error, G_MARKUP_ERROR,
447 G_MARKUP_ERROR_INVALID_CONTENT,
448 "translation context given for "
449 " value without l10n enabled");
453 return g_string_new (NULL);
457 key_state_end_default (KeyState *state,
461 state->unparsed_default_value = *string;
464 state->default_value = g_variant_parse (state->type,
465 state->unparsed_default_value->str,
467 key_state_check_range (state, error);
471 key_state_start_choices (KeyState *state,
474 const GVariantType *type = state->type;
478 g_set_error_literal (error, G_MARKUP_ERROR,
479 G_MARKUP_ERROR_INVALID_CONTENT,
480 "<choices> cannot be specified for keys "
481 "tagged as having an enumerated type");
485 if (state->has_choices)
487 g_set_error_literal (error, G_MARKUP_ERROR,
488 G_MARKUP_ERROR_INVALID_CONTENT,
489 "<choices> already specified for this key");
493 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
494 type = g_variant_type_element (type);
496 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
498 gchar *type_string = g_variant_type_dup_string (state->type);
499 g_set_error (error, G_MARKUP_ERROR,
500 G_MARKUP_ERROR_INVALID_CONTENT,
501 "<choices> not allowed for keys of type '%s'",
503 g_free (type_string);
509 key_state_add_choice (KeyState *state,
513 if (strinfo_builder_contains (state->strinfo, choice))
515 g_set_error (error, G_MARKUP_ERROR,
516 G_MARKUP_ERROR_INVALID_CONTENT,
517 "<choice value='%s'/> already given", choice);
521 strinfo_builder_append_item (state->strinfo, choice, 0);
522 state->has_choices = TRUE;
526 key_state_end_choices (KeyState *state,
529 if (!state->has_choices)
531 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
532 "<choices> must contain at least one <choice>");
536 key_state_check_range (state, error);
540 key_state_start_aliases (KeyState *state,
543 if (state->has_aliases)
544 g_set_error_literal (error, G_MARKUP_ERROR,
545 G_MARKUP_ERROR_INVALID_CONTENT,
546 "<aliases> already specified for this key");
547 else if (!state->is_flags && !state->is_enum && !state->has_choices)
548 g_set_error_literal (error, G_MARKUP_ERROR,
549 G_MARKUP_ERROR_INVALID_CONTENT,
550 "<aliases> can only be specified for keys with "
551 "enumerated or flags types or after <choices>");
555 key_state_add_alias (KeyState *state,
560 if (strinfo_builder_contains (state->strinfo, alias))
562 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
563 state->strinfo->len / 4,
567 g_set_error (error, G_MARKUP_ERROR,
568 G_MARKUP_ERROR_INVALID_CONTENT,
569 "<alias value='%s'/> given when '%s' is already "
570 "a member of the enumerated type", alias, alias);
573 g_set_error (error, G_MARKUP_ERROR,
574 G_MARKUP_ERROR_INVALID_CONTENT,
575 "<alias value='%s'/> given when "
576 "<choice value='%s'/> was already given",
581 g_set_error (error, G_MARKUP_ERROR,
582 G_MARKUP_ERROR_INVALID_CONTENT,
583 "<alias value='%s'/> already specified", alias);
588 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
590 g_set_error (error, G_MARKUP_ERROR,
591 G_MARKUP_ERROR_INVALID_CONTENT,
592 "alias target '%s' is not in %s", target,
593 state->is_enum ? "enumerated type" : "<choices>");
597 state->has_aliases = TRUE;
601 key_state_end_aliases (KeyState *state,
604 if (!state->has_aliases)
606 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
607 "<aliases> must contain at least one <alias>");
613 key_state_check (KeyState *state,
619 return state->checked = TRUE;
623 key_state_serialise (KeyState *state)
625 if (state->serialised == NULL)
627 if (state->child_schema)
629 state->serialised = g_variant_new_string (state->child_schema);
634 GVariantBuilder builder;
636 g_assert (key_state_check (state, NULL));
638 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
641 g_variant_builder_add_value (&builder, state->default_value);
646 /* We are going to store the untranslated default for
647 * runtime translation according to the current locale.
648 * We need to strip leading and trailing whitespace from
649 * the string so that it's exactly the same as the one
650 * that ended up in the .po file for translation.
652 * We want to do this so that
654 * <default l10n='messages'>
658 * ends up in the .po file like "['a', 'b', 'c']",
659 * omitting the extra whitespace at the start and end.
661 strip_string (state->unparsed_default_value);
663 if (state->l10n_context)
667 /* Contextified messages are supported by prepending
668 * the context, followed by '\004' to the start of the
669 * message string. We do that here to save GSettings
672 len = strlen (state->l10n_context);
673 state->l10n_context[len] = '\004';
674 g_string_prepend_len (state->unparsed_default_value,
675 state->l10n_context, len + 1);
676 g_free (state->l10n_context);
677 state->l10n_context = NULL;
680 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
681 state->unparsed_default_value->str);
682 g_string_free (state->unparsed_default_value, TRUE);
683 state->unparsed_default_value = NULL;
686 /* choice, aliases, enums */
687 if (state->strinfo->len)
695 data = state->strinfo->str;
696 size = state->strinfo->len;
699 for (i = 0; i < size / sizeof (guint32); i++)
700 words[i] = GUINT32_TO_LE (words[i]);
702 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
706 g_string_free (state->strinfo, FALSE);
707 state->strinfo = NULL;
709 g_variant_builder_add (&builder, "(y@au)",
710 state->is_flags ? 'f' :
711 state->is_enum ? 'e' : 'c',
716 if (state->minimum || state->maximum)
717 g_variant_builder_add (&builder, "(y(**))", 'r',
718 state->minimum, state->maximum);
720 state->serialised = g_variant_builder_end (&builder);
723 g_variant_ref_sink (state->serialised);
726 return g_variant_ref (state->serialised);
730 key_state_free (gpointer data)
732 KeyState *state = data;
735 g_variant_type_free (state->type);
737 g_free (state->l10n_context);
739 if (state->unparsed_default_value)
740 g_string_free (state->unparsed_default_value, TRUE);
742 if (state->default_value)
743 g_variant_unref (state->default_value);
746 g_string_free (state->strinfo, TRUE);
749 g_variant_unref (state->minimum);
752 g_variant_unref (state->maximum);
754 if (state->serialised)
755 g_variant_unref (state->serialised);
757 g_slice_free (KeyState, state);
760 /* Key name validity {{{1 */
761 static gboolean allow_any_name = FALSE;
764 is_valid_keyname (const gchar *key,
771 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
772 _("empty names are not permitted"));
779 if (!g_ascii_islower (key[0]))
781 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
782 _("invalid name '%s': names must begin "
783 "with a lowercase letter"), key);
787 for (i = 1; key[i]; i++)
790 !g_ascii_islower (key[i]) &&
791 !g_ascii_isdigit (key[i]))
793 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
794 _("invalid name '%s': invalid character '%c'; "
795 "only lowercase letters, numbers and hyphen ('-') "
796 "are permitted."), key, key[i]);
800 if (key[i] == '-' && key[i + 1] == '-')
802 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
803 _("invalid name '%s': two successive hyphens ('--') "
804 "are not permitted."), key);
809 if (key[i - 1] == '-')
811 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
812 _("invalid name '%s': the last character may not be a "
813 "hyphen ('-')."), key);
819 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
820 _("invalid name '%s': maximum length is 1024"), key);
827 /* Handling of <schema> {{{1 */
828 typedef struct _SchemaState SchemaState;
831 SchemaState *extends;
834 gchar *gettext_domain;
842 schema_state_new (const gchar *path,
843 const gchar *gettext_domain,
844 SchemaState *extends,
845 const gchar *extends_name,
846 const gchar *list_of)
850 state = g_slice_new (SchemaState);
851 state->path = g_strdup (path);
852 state->gettext_domain = g_strdup (gettext_domain);
853 state->extends = extends;
854 state->extends_name = g_strdup (extends_name);
855 state->list_of = g_strdup (list_of);
856 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
857 g_free, key_state_free);
863 schema_state_free (gpointer data)
865 SchemaState *state = data;
867 g_free (state->path);
868 g_free (state->gettext_domain);
869 g_hash_table_unref (state->keys);
873 schema_state_add_child (SchemaState *state,
880 if (!is_valid_keyname (name, error))
883 childname = g_strconcat (name, "/", NULL);
885 if (g_hash_table_lookup (state->keys, childname))
887 g_set_error (error, G_MARKUP_ERROR,
888 G_MARKUP_ERROR_INVALID_CONTENT,
889 _("<child name='%s'> already specified"), name);
893 g_hash_table_insert (state->keys, childname,
894 key_state_new_child (schema));
898 schema_state_add_key (SchemaState *state,
899 GHashTable *enum_table,
900 GHashTable *flags_table,
902 const gchar *type_string,
903 const gchar *enum_type,
904 const gchar *flags_type,
913 g_set_error_literal (error, G_MARKUP_ERROR,
914 G_MARKUP_ERROR_INVALID_CONTENT,
915 _("cannot add keys to a 'list-of' schema"));
919 if (!is_valid_keyname (name, error))
922 if (g_hash_table_lookup (state->keys, name))
924 g_set_error (error, G_MARKUP_ERROR,
925 G_MARKUP_ERROR_INVALID_CONTENT,
926 _("<key name='%s'> already specified"), name);
930 for (node = state; node; node = node->extends)
935 shadow = g_hash_table_lookup (node->extends->keys, name);
937 /* in case of <key> <override> <key> make sure we report the
938 * location of the original <key>, not the <override>.
940 if (shadow && !shadow->is_override)
942 g_set_error (error, G_MARKUP_ERROR,
943 G_MARKUP_ERROR_INVALID_CONTENT,
944 _("<key name='%s'> shadows <key name='%s'> in "
945 "<schema id='%s'>; use <override> to modify value"),
946 name, name, node->extends_name);
951 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
953 g_set_error (error, G_MARKUP_ERROR,
954 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
955 _("exactly one of 'type', 'enum' or 'flags' must "
956 "be specified as an attribute to <key>"));
960 if (type_string == NULL) /* flags or enums was specified */
962 EnumState *enum_state;
965 enum_state = g_hash_table_lookup (enum_table, enum_type);
967 enum_state = g_hash_table_lookup (flags_table, flags_type);
970 if (enum_state == NULL)
972 g_set_error (error, G_MARKUP_ERROR,
973 G_MARKUP_ERROR_INVALID_CONTENT,
974 _("<%s id='%s'> not (yet) defined."),
975 flags_type ? "flags" : "enum",
976 flags_type ? flags_type : enum_type);
980 type_string = flags_type ? "as" : "s";
981 strinfo = enum_state->strinfo;
985 if (!g_variant_type_string_is_valid (type_string))
987 g_set_error (error, G_MARKUP_ERROR,
988 G_MARKUP_ERROR_INVALID_CONTENT,
989 _("invalid GVariant type string '%s'"), type_string);
996 key = key_state_new (type_string, state->gettext_domain,
997 enum_type != NULL, flags_type != NULL, strinfo);
998 g_hash_table_insert (state->keys, g_strdup (name), key);
1004 schema_state_add_override (SchemaState *state,
1005 KeyState **key_state,
1009 const gchar *context,
1012 SchemaState *parent;
1015 if (state->extends == NULL)
1017 g_set_error_literal (error, G_MARKUP_ERROR,
1018 G_MARKUP_ERROR_INVALID_CONTENT,
1019 _("<override> given but schema isn't "
1020 "extending anything"));
1024 for (parent = state->extends; parent; parent = parent->extends)
1025 if ((original = g_hash_table_lookup (parent->keys, key)))
1028 if (original == NULL)
1030 g_set_error (error, G_MARKUP_ERROR,
1031 G_MARKUP_ERROR_INVALID_CONTENT,
1032 _("no <key name='%s'> to override"), key);
1036 if (g_hash_table_lookup (state->keys, key))
1038 g_set_error (error, G_MARKUP_ERROR,
1039 G_MARKUP_ERROR_INVALID_CONTENT,
1040 _("<override name='%s'> already specified"), key);
1044 *key_state = key_state_override (original, state->gettext_domain);
1045 *string = key_state_start_default (*key_state, l10n, context, error);
1046 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1050 override_state_end (KeyState **key_state,
1054 key_state_end_default (*key_state, string, error);
1058 /* Handling of toplevel state {{{1 */
1061 GHashTable *schema_table; /* string -> SchemaState */
1062 GHashTable *flags_table; /* string -> EnumState */
1063 GHashTable *enum_table; /* string -> EnumState */
1065 GSList *this_file_schemas; /* strings: <schema>s in this file */
1066 GSList *this_file_flagss; /* strings: <flags>s in this file */
1067 GSList *this_file_enums; /* strings: <enum>s in this file */
1069 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1071 SchemaState *schema_state; /* non-NULL when inside <schema> */
1072 KeyState *key_state; /* non-NULL when inside <key> */
1073 EnumState *enum_state; /* non-NULL when inside <enum> */
1075 GString *string; /* non-NULL when accepting text */
1079 is_subclass (const gchar *class_name,
1080 const gchar *possible_parent,
1081 GHashTable *schema_table)
1085 if (strcmp (class_name, possible_parent) == 0)
1088 class = g_hash_table_lookup (schema_table, class_name);
1089 g_assert (class != NULL);
1091 return class->extends_name &&
1092 is_subclass (class->extends_name, possible_parent, schema_table);
1096 parse_state_start_schema (ParseState *state,
1099 const gchar *gettext_domain,
1100 const gchar *extends_name,
1101 const gchar *list_of,
1104 SchemaState *extends;
1107 if (g_hash_table_lookup (state->schema_table, id))
1109 g_set_error (error, G_MARKUP_ERROR,
1110 G_MARKUP_ERROR_INVALID_CONTENT,
1111 _("<schema id='%s'> already specified"), id);
1117 extends = g_hash_table_lookup (state->schema_table, extends_name);
1119 if (extends == NULL)
1121 g_set_error (error, G_MARKUP_ERROR,
1122 G_MARKUP_ERROR_INVALID_CONTENT,
1123 _("<schema id='%s'> extends not yet existing "
1124 "schema '%s'"), id, extends_name);
1135 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1137 g_set_error (error, G_MARKUP_ERROR,
1138 G_MARKUP_ERROR_INVALID_CONTENT,
1139 _("<schema id='%s'> is list of not yet existing "
1140 "schema '%s'"), id, list_of);
1146 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1147 _("Can not be a list of a schema with a path"));
1156 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1157 _("Can not extend a schema with a path"));
1163 if (extends->list_of == NULL)
1165 g_set_error (error, G_MARKUP_ERROR,
1166 G_MARKUP_ERROR_INVALID_CONTENT,
1167 _("<schema id='%s'> is a list, extending "
1168 "<schema id='%s'> which is not a list"),
1173 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1175 g_set_error (error, G_MARKUP_ERROR,
1176 G_MARKUP_ERROR_INVALID_CONTENT,
1177 _("<schema id='%s' list-of='%s'> extends <schema "
1178 "id='%s' list-of='%s'> but '%s' does not "
1179 "extend '%s'"), id, list_of, extends_name,
1180 extends->list_of, list_of, extends->list_of);
1185 /* by default we are a list of the same thing that the schema
1186 * we are extending is a list of (which might be nothing)
1188 list_of = extends->list_of;
1191 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1193 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1194 _("a path, if given, must begin and end with a slash"));
1198 if (path && list_of && !g_str_has_suffix (path, ":/"))
1200 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1201 _("the path of a list must end with ':/'"));
1205 if (path && (g_str_has_prefix (path, "/apps/") ||
1206 g_str_has_prefix (path, "/desktop/") ||
1207 g_str_has_prefix (path, "/system/")))
1208 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1209 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1211 state->schema_state = schema_state_new (path, gettext_domain,
1212 extends, extends_name, list_of);
1214 my_id = g_strdup (id);
1215 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1216 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1220 parse_state_start_enum (ParseState *state,
1225 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1226 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1229 if (g_hash_table_lookup (table, id))
1231 g_set_error (error, G_MARKUP_ERROR,
1232 G_MARKUP_ERROR_INVALID_CONTENT,
1233 _("<%s id='%s'> already specified"),
1234 is_flags ? "flags" : "enum", id);
1238 state->enum_state = enum_state_new (is_flags);
1240 my_id = g_strdup (id);
1241 *list = g_slist_prepend (*list, my_id);
1242 g_hash_table_insert (table, my_id, state->enum_state);
1245 /* GMarkup Parser Functions {{{1 */
1247 /* Start element {{{2 */
1249 start_element (GMarkupParseContext *context,
1250 const gchar *element_name,
1251 const gchar **attribute_names,
1252 const gchar **attribute_values,
1256 ParseState *state = user_data;
1257 const GSList *element_stack;
1258 const gchar *container;
1260 element_stack = g_markup_parse_context_get_element_stack (context);
1261 container = element_stack->next ? element_stack->next->data : NULL;
1263 #define COLLECT(first, ...) \
1264 g_markup_collect_attributes (element_name, \
1265 attribute_names, attribute_values, error, \
1266 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1267 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1268 #define STRDUP G_MARKUP_COLLECT_STRDUP
1269 #define STRING G_MARKUP_COLLECT_STRING
1270 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1272 /* Toplevel items {{{3 */
1273 if (container == NULL)
1275 if (strcmp (element_name, "schemalist") == 0)
1277 COLLECT (OPTIONAL | STRDUP,
1279 &state->schemalist_domain);
1285 /* children of <schemalist> {{{3 */
1286 else if (strcmp (container, "schemalist") == 0)
1288 if (strcmp (element_name, "schema") == 0)
1290 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1291 if (COLLECT (STRING, "id", &id,
1292 OPTIONAL | STRING, "path", &path,
1293 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1294 OPTIONAL | STRING, "extends", &extends,
1295 OPTIONAL | STRING, "list-of", &list_of))
1296 parse_state_start_schema (state, id, path,
1297 gettext_domain ? gettext_domain
1298 : state->schemalist_domain,
1299 extends, list_of, error);
1303 else if (strcmp (element_name, "enum") == 0)
1306 if (COLLECT (STRING, "id", &id))
1307 parse_state_start_enum (state, id, FALSE, error);
1311 else if (strcmp (element_name, "flags") == 0)
1314 if (COLLECT (STRING, "id", &id))
1315 parse_state_start_enum (state, id, TRUE, error);
1321 /* children of <schema> {{{3 */
1322 else if (strcmp (container, "schema") == 0)
1324 if (strcmp (element_name, "key") == 0)
1326 const gchar *name, *type_string, *enum_type, *flags_type;
1328 if (COLLECT (STRING, "name", &name,
1329 OPTIONAL | STRING, "type", &type_string,
1330 OPTIONAL | STRING, "enum", &enum_type,
1331 OPTIONAL | STRING, "flags", &flags_type))
1333 state->key_state = schema_state_add_key (state->schema_state,
1337 enum_type, flags_type,
1341 else if (strcmp (element_name, "child") == 0)
1343 const gchar *name, *schema;
1345 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1346 schema_state_add_child (state->schema_state,
1347 name, schema, error);
1350 else if (strcmp (element_name, "override") == 0)
1352 const gchar *name, *l10n, *context;
1354 if (COLLECT (STRING, "name", &name,
1355 OPTIONAL | STRING, "l10n", &l10n,
1356 OPTIONAL | STRING, "context", &context))
1357 schema_state_add_override (state->schema_state,
1358 &state->key_state, &state->string,
1359 name, l10n, context, error);
1364 /* children of <key> {{{3 */
1365 else if (strcmp (container, "key") == 0)
1367 if (strcmp (element_name, "default") == 0)
1369 const gchar *l10n, *context;
1370 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1371 STRING | OPTIONAL, "context", &context))
1372 state->string = key_state_start_default (state->key_state,
1373 l10n, context, error);
1377 else if (strcmp (element_name, "summary") == 0 ||
1378 strcmp (element_name, "description") == 0)
1381 state->string = g_string_new (NULL);
1385 else if (strcmp (element_name, "range") == 0)
1387 const gchar *min, *max;
1388 if (COLLECT (STRING | OPTIONAL, "min", &min,
1389 STRING | OPTIONAL, "max", &max))
1390 key_state_set_range (state->key_state, min, max, error);
1394 else if (strcmp (element_name, "choices") == 0)
1397 key_state_start_choices (state->key_state, error);
1401 else if (strcmp (element_name, "aliases") == 0)
1404 key_state_start_aliases (state->key_state, error);
1410 /* children of <choices> {{{3 */
1411 else if (strcmp (container, "choices") == 0)
1413 if (strcmp (element_name, "choice") == 0)
1416 if (COLLECT (STRING, "value", &value))
1417 key_state_add_choice (state->key_state, value, error);
1423 /* children of <aliases> {{{3 */
1424 else if (strcmp (container, "aliases") == 0)
1426 if (strcmp (element_name, "alias") == 0)
1428 const gchar *value, *target;
1429 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1430 key_state_add_alias (state->key_state, value, target, error);
1436 /* children of <enum> {{{3 */
1437 else if (strcmp (container, "enum") == 0 ||
1438 strcmp (container, "flags") == 0)
1440 if (strcmp (element_name, "value") == 0)
1442 const gchar *nick, *valuestr;
1443 if (COLLECT (STRING, "nick", &nick,
1444 STRING, "value", &valuestr))
1445 enum_state_add_value (state->enum_state, nick, valuestr, error);
1452 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1453 _("Element <%s> not allowed inside <%s>"),
1454 element_name, container);
1456 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1457 _("Element <%s> not allowed at the top level"), element_name);
1460 /* End element {{{2 */
1463 key_state_end (KeyState **state_ptr,
1471 if (state->default_value == NULL)
1473 g_set_error_literal (error,
1474 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1475 "element <default> is required in <key>");
1481 schema_state_end (SchemaState **state_ptr,
1488 end_element (GMarkupParseContext *context,
1489 const gchar *element_name,
1493 ParseState *state = user_data;
1495 if (strcmp (element_name, "schemalist") == 0)
1497 g_free (state->schemalist_domain);
1498 state->schemalist_domain = NULL;
1501 else if (strcmp (element_name, "enum") == 0 ||
1502 strcmp (element_name, "flags") == 0)
1503 enum_state_end (&state->enum_state, error);
1505 else if (strcmp (element_name, "schema") == 0)
1506 schema_state_end (&state->schema_state, error);
1508 else if (strcmp (element_name, "override") == 0)
1509 override_state_end (&state->key_state, &state->string, error);
1511 else if (strcmp (element_name, "key") == 0)
1512 key_state_end (&state->key_state, error);
1514 else if (strcmp (element_name, "default") == 0)
1515 key_state_end_default (state->key_state, &state->string, error);
1517 else if (strcmp (element_name, "choices") == 0)
1518 key_state_end_choices (state->key_state, error);
1520 else if (strcmp (element_name, "aliases") == 0)
1521 key_state_end_aliases (state->key_state, error);
1525 g_string_free (state->string, TRUE);
1526 state->string = NULL;
1531 text (GMarkupParseContext *context,
1537 ParseState *state = user_data;
1541 /* we are expecting a string, so store the text data.
1543 * we store the data verbatim here and deal with whitespace
1544 * later on. there are two reasons for that:
1546 * 1) whitespace is handled differently depending on the tag
1549 * 2) we could do leading whitespace removal by refusing to
1550 * insert it into state->string if it's at the start, but for
1551 * trailing whitespace, we have no idea if there is another
1552 * text() call coming or not.
1554 g_string_append_len (state->string, text, text_len);
1558 /* string is not expected: accept (and ignore) pure whitespace */
1561 for (i = 0; i < text_len; i++)
1562 if (!g_ascii_isspace (text[i]))
1564 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1565 _("text may not appear inside <%s>"),
1566 g_markup_parse_context_get_element (context));
1572 /* Write to GVDB {{{1 */
1580 gvdb_pair_init (GvdbPair *pair)
1582 pair->table = gvdb_hash_table_new (NULL, NULL);
1583 pair->root = gvdb_hash_table_insert (pair->table, "");
1588 GHashTable *schema_table;
1594 GHashTable *schema_table;
1600 output_key (gpointer key,
1604 OutputSchemaData *data;
1613 item = gvdb_hash_table_insert (data->pair.table, name);
1614 gvdb_item_set_parent (item, data->pair.root);
1615 gvdb_item_set_value (item, key_state_serialise (state));
1620 if (state->child_schema &&
1621 !g_hash_table_lookup (data->schema_table, state->child_schema))
1622 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1623 state->child_schema);
1627 output_schema (gpointer key,
1631 WriteToFileData *wtf_data = user_data;
1632 OutputSchemaData data;
1633 GvdbPair *root_pair;
1640 root_pair = &wtf_data->root_pair;
1642 data.schema_table = wtf_data->schema_table;
1643 gvdb_pair_init (&data.pair);
1646 item = gvdb_hash_table_insert (root_pair->table, id);
1647 gvdb_item_set_parent (item, root_pair->root);
1648 gvdb_item_set_hash_table (item, data.pair.table);
1650 g_hash_table_foreach (state->keys, output_key, &data);
1653 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1655 if (state->extends_name)
1656 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1657 state->extends_name);
1660 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1664 gvdb_hash_table_insert_string (data.pair.table,
1666 state->gettext_domain);
1670 write_to_file (GHashTable *schema_table,
1671 const gchar *filename,
1674 WriteToFileData data;
1677 data.schema_table = schema_table;
1679 gvdb_pair_init (&data.root_pair);
1681 g_hash_table_foreach (schema_table, output_schema, &data);
1683 success = gvdb_table_write_contents (data.root_pair.table, filename,
1684 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1686 g_hash_table_unref (data.root_pair.table);
1691 /* Parser driver {{{1 */
1693 parse_gschema_files (gchar **files,
1696 GMarkupParser parser = { start_element, end_element, text };
1697 ParseState state = { 0, };
1698 const gchar *filename;
1699 GError *error = NULL;
1701 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1702 g_free, enum_state_free);
1704 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1705 g_free, enum_state_free);
1707 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1708 g_free, schema_state_free);
1710 while ((filename = *files++) != NULL)
1712 GMarkupParseContext *context;
1716 if (!g_file_get_contents (filename, &contents, &size, &error))
1718 fprintf (stderr, "%s\n", error->message);
1719 g_clear_error (&error);
1723 context = g_markup_parse_context_new (&parser,
1724 G_MARKUP_TREAT_CDATA_AS_TEXT |
1725 G_MARKUP_PREFIX_ERROR_POSITION |
1726 G_MARKUP_IGNORE_QUALIFIED,
1730 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1731 !g_markup_parse_context_end_parse (context, &error))
1735 /* back out any changes from this file */
1736 for (item = state.this_file_schemas; item; item = item->next)
1737 g_hash_table_remove (state.schema_table, item->data);
1739 for (item = state.this_file_flagss; item; item = item->next)
1740 g_hash_table_remove (state.flags_table, item->data);
1742 for (item = state.this_file_enums; item; item = item->next)
1743 g_hash_table_remove (state.enum_table, item->data);
1746 fprintf (stderr, "%s: %s. ", filename, error->message);
1747 g_clear_error (&error);
1751 /* Translators: Do not translate "--strict". */
1752 fprintf (stderr, _("--strict was specified; exiting.\n"));
1753 g_hash_table_unref (state.schema_table);
1754 g_hash_table_unref (state.flags_table);
1755 g_hash_table_unref (state.enum_table);
1760 fprintf (stderr, _("This entire file has been ignored.\n"));
1764 g_markup_parse_context_free (context);
1765 g_slist_free (state.this_file_schemas);
1766 g_slist_free (state.this_file_flagss);
1767 g_slist_free (state.this_file_enums);
1768 state.this_file_schemas = NULL;
1769 state.this_file_flagss = NULL;
1770 state.this_file_enums = NULL;
1773 g_hash_table_unref (state.flags_table);
1774 g_hash_table_unref (state.enum_table);
1776 return state.schema_table;
1780 compare_strings (gconstpointer a,
1783 gchar *one = *(gchar **) a;
1784 gchar *two = *(gchar **) b;
1787 cmp = g_str_has_suffix (two, ".enums.xml") -
1788 g_str_has_suffix (one, ".enums.xml");
1791 cmp = strcmp (one, two);
1797 set_overrides (GHashTable *schema_table,
1801 const gchar *filename;
1802 GError *error = NULL;
1804 while ((filename = *files++))
1810 key_file = g_key_file_new ();
1811 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1813 fprintf (stderr, "%s: %s. ", filename, error->message);
1814 g_key_file_free (key_file);
1815 g_clear_error (&error);
1819 fprintf (stderr, _("Ignoring this file.\n"));
1823 fprintf (stderr, _("--strict was specified; exiting.\n"));
1827 groups = g_key_file_get_groups (key_file, NULL);
1829 for (i = 0; groups[i]; i++)
1831 const gchar *group = groups[i];
1832 SchemaState *schema;
1836 schema = g_hash_table_lookup (schema_table, group);
1839 /* Having the schema not be installed is expected to be a
1840 * common case. Don't even emit an error message about
1845 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1846 g_assert (keys != NULL);
1848 for (j = 0; keys[j]; j++)
1850 const gchar *key = keys[j];
1855 state = g_hash_table_lookup (schema->keys, key);
1859 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1860 "specified in override file '%s'"),
1861 key, group, filename);
1865 fprintf (stderr, _("; ignoring override for this key.\n"));
1869 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1870 g_key_file_free (key_file);
1871 g_strfreev (groups);
1877 string = g_key_file_get_value (key_file, group, key, NULL);
1878 g_assert (string != NULL);
1880 value = g_variant_parse (state->type, string,
1881 NULL, NULL, &error);
1885 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1886 "as specified in override file '%s': "
1888 key, group, filename, error->message);
1890 g_clear_error (&error);
1895 fprintf (stderr, _("Ignoring override for this key.\n"));
1899 fprintf (stderr, _("--strict was specified; exiting.\n"));
1900 g_key_file_free (key_file);
1901 g_strfreev (groups);
1909 if (g_variant_compare (value, state->minimum) < 0 ||
1910 g_variant_compare (value, state->maximum) > 0)
1913 _("override for key '%s' in schema '%s' in "
1914 "override file '%s' is outside the range "
1915 "given in the schema"),
1916 key, group, filename);
1918 g_variant_unref (value);
1923 fprintf (stderr, _("; ignoring override for this key.\n"));
1927 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1928 g_key_file_free (key_file);
1929 g_strfreev (groups);
1936 else if (state->strinfo->len)
1938 if (!is_valid_choices (value, state->strinfo))
1941 _("override for key '%s' in schema '%s' in "
1942 "override file '%s' is not in the list "
1943 "of valid choices"),
1944 key, group, filename);
1946 g_variant_unref (value);
1951 fprintf (stderr, _("; ignoring override for this key.\n"));
1955 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1956 g_key_file_free (key_file);
1957 g_strfreev (groups);
1964 g_variant_unref (state->default_value);
1965 state->default_value = value;
1972 g_strfreev (groups);
1979 main (int argc, char **argv)
1986 gchar *targetdir = NULL;
1988 gboolean dry_run = FALSE;
1989 gboolean strict = FALSE;
1990 gchar **schema_files = NULL;
1991 gchar **override_files = NULL;
1992 GOptionContext *context;
1993 GOptionEntry entries[] = {
1994 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1995 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1996 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1997 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1999 /* These options are only for use in the gschema-compile tests */
2000 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2008 setlocale (LC_ALL, "");
2009 textdomain (GETTEXT_PACKAGE);
2012 tmp = _glib_get_locale_dir ();
2013 bindtextdomain (GETTEXT_PACKAGE, tmp);
2016 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2019 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2020 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2023 context = g_option_context_new (N_("DIRECTORY"));
2024 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2025 g_option_context_set_summary (context,
2026 N_("Compile all GSettings schema files into a schema cache.\n"
2027 "Schema files are required to have the extension .gschema.xml,\n"
2028 "and the cache file is called gschemas.compiled."));
2029 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2032 if (!g_option_context_parse (context, &argc, &argv, &error))
2034 fprintf (stderr, "%s\n", error->message);
2038 g_option_context_free (context);
2040 if (!schema_files && argc != 2)
2042 fprintf (stderr, _("You should give exactly one directory name\n"));
2048 if (targetdir == NULL)
2051 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2055 GPtrArray *overrides;
2058 files = g_ptr_array_new ();
2059 overrides = g_ptr_array_new ();
2061 dir = g_dir_open (srcdir, 0, &error);
2064 fprintf (stderr, "%s\n", error->message);
2068 while ((file = g_dir_read_name (dir)) != NULL)
2070 if (g_str_has_suffix (file, ".gschema.xml") ||
2071 g_str_has_suffix (file, ".enums.xml"))
2072 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2074 else if (g_str_has_suffix (file, ".gschema.override"))
2075 g_ptr_array_add (overrides,
2076 g_build_filename (srcdir, file, NULL));
2079 if (files->len == 0)
2081 fprintf (stdout, _("No schema files found: "));
2083 if (g_unlink (target))
2084 fprintf (stdout, _("doing nothing.\n"));
2087 fprintf (stdout, _("removed existing output file.\n"));
2091 g_ptr_array_sort (files, compare_strings);
2092 g_ptr_array_add (files, NULL);
2094 g_ptr_array_sort (overrides, compare_strings);
2095 g_ptr_array_add (overrides, NULL);
2097 schema_files = (char **) g_ptr_array_free (files, FALSE);
2098 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2101 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2107 if (override_files != NULL &&
2108 !set_overrides (table, override_files, strict))
2114 if (!dry_run && !write_to_file (table, target, &error))
2116 fprintf (stderr, "%s\n", error->message);
2128 /* vim:set foldmethod=marker: */