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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
36 #include "gvdb/gvdb-builder.h"
39 /* Handling of <enum> {{{1 */
48 enum_state_free (gpointer data)
50 EnumState *state = data;
52 g_string_free (state->strinfo, TRUE);
53 g_slice_free (EnumState, state);
57 enum_state_new (gboolean is_flags)
61 state = g_slice_new (EnumState);
62 state->strinfo = g_string_new (NULL);
63 state->is_flags = is_flags;
69 enum_state_add_value (EnumState *state,
71 const gchar *valuestr,
77 if (nick[0] == '\0' || nick[1] == '\0')
79 g_set_error (error, G_MARKUP_ERROR,
80 G_MARKUP_ERROR_INVALID_CONTENT,
81 "nick must be a minimum of 2 characters");
85 value = g_ascii_strtoll (valuestr, &end, 0);
86 if (*end || state->is_flags ?
87 (value > G_MAXUINT32 || value < 0) :
88 (value > G_MAXINT32 || value < G_MININT32))
90 g_set_error (error, G_MARKUP_ERROR,
91 G_MARKUP_ERROR_INVALID_CONTENT,
92 "invalid numeric value");
96 if (strinfo_builder_contains (state->strinfo, nick))
98 g_set_error (error, G_MARKUP_ERROR,
99 G_MARKUP_ERROR_INVALID_CONTENT,
100 "<value nick='%s'/> already specified", nick);
104 if (strinfo_builder_contains_value (state->strinfo, value))
106 g_set_error (error, G_MARKUP_ERROR,
107 G_MARKUP_ERROR_INVALID_CONTENT,
108 "value='%s' already specified", valuestr);
112 /* Silently drop the null case if it is mentioned.
113 * It is properly denoted with an empty array.
115 if (state->is_flags && value == 0)
118 if (state->is_flags && (value & (value - 1)))
120 g_set_error (error, G_MARKUP_ERROR,
121 G_MARKUP_ERROR_INVALID_CONTENT,
122 "flags values must have at most 1 bit set");
126 /* Since we reject exact duplicates of value='' and we only allow one
127 * bit to be set, it's not possible to have overlaps.
129 * If we loosen the one-bit-set restriction we need an overlap check.
132 strinfo_builder_append_item (state->strinfo, nick, value);
136 enum_state_end (EnumState **state_ptr,
144 if (state->strinfo->len == 0)
146 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
147 "<%s> must contain at least one <value>",
148 state->is_flags ? "flags" : "enum");
151 /* Handling of <key> {{{1 */
154 /* for <child>, @child_schema will be set.
155 * for <key>, everything else will be set.
161 gboolean have_gettext_domain;
165 GString *unparsed_default_value;
166 GVariant *default_value;
175 gboolean has_choices;
176 gboolean has_aliases;
177 gboolean is_override;
180 GVariant *serialised;
184 key_state_new (const gchar *type_string,
185 const gchar *gettext_domain,
192 state = g_slice_new0 (KeyState);
193 state->type = g_variant_type_new (type_string);
194 state->have_gettext_domain = gettext_domain != NULL;
195 state->is_enum = is_enum;
196 state->is_flags = is_flags;
199 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
201 state->strinfo = g_string_new (NULL);
207 key_state_override (KeyState *state,
208 const gchar *gettext_domain)
212 copy = g_slice_new0 (KeyState);
213 copy->type = g_variant_type_copy (state->type);
214 copy->have_gettext_domain = gettext_domain != NULL;
215 copy->strinfo = g_string_new_len (state->strinfo->str,
216 state->strinfo->len);
217 copy->is_enum = state->is_enum;
218 copy->is_flags = state->is_flags;
219 copy->is_override = TRUE;
223 copy->minimum = g_variant_ref (state->minimum);
224 copy->maximum = g_variant_ref (state->maximum);
231 key_state_new_child (const gchar *child_schema)
235 state = g_slice_new0 (KeyState);
236 state->child_schema = g_strdup (child_schema);
242 is_valid_choices (GVariant *variant,
245 switch (g_variant_classify (variant))
247 case G_VARIANT_CLASS_MAYBE:
248 case G_VARIANT_CLASS_ARRAY:
250 gboolean valid = TRUE;
253 g_variant_iter_init (&iter, variant);
255 while (valid && (variant = g_variant_iter_next_value (&iter)))
257 valid = is_valid_choices (variant, strinfo);
258 g_variant_unref (variant);
264 case G_VARIANT_CLASS_STRING:
265 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
267 g_variant_get_string (variant, NULL));
270 g_assert_not_reached ();
275 /* Gets called at </default> </choices> or <range/> to check for
276 * validity of the default value so that any inconsistency is
277 * reported as soon as it is encountered.
280 key_state_check_range (KeyState *state,
283 if (state->default_value)
287 tag = state->is_override ? "override" : "default";
291 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
292 g_variant_compare (state->default_value, state->maximum) > 0)
294 g_set_error (error, G_MARKUP_ERROR,
295 G_MARKUP_ERROR_INVALID_CONTENT,
296 "<%s> is not contained in "
297 "the specified range", tag);
301 else if (state->strinfo->len)
303 if (!is_valid_choices (state->default_value, state->strinfo))
306 g_set_error (error, G_MARKUP_ERROR,
307 G_MARKUP_ERROR_INVALID_CONTENT,
308 "<%s> is not a valid member of "
309 "the specified enumerated type", tag);
311 else if (state->is_flags)
312 g_set_error (error, G_MARKUP_ERROR,
313 G_MARKUP_ERROR_INVALID_CONTENT,
314 "<%s> contains string not in the "
315 "specified flags type", tag);
318 g_set_error (error, G_MARKUP_ERROR,
319 G_MARKUP_ERROR_INVALID_CONTENT,
320 "<%s> contains string not in "
328 key_state_set_range (KeyState *state,
329 const gchar *min_str,
330 const gchar *max_str,
339 { 'n', "-32768", "32767" },
340 { 'q', "0", "65535" },
341 { 'i', "-2147483648", "2147483647" },
342 { 'u', "0", "4294967295" },
343 { 'x', "-9223372036854775808", "9223372036854775807" },
344 { 't', "0", "18446744073709551615" },
345 { 'd', "-inf", "inf" },
347 gboolean type_ok = FALSE;
352 g_set_error_literal (error, G_MARKUP_ERROR,
353 G_MARKUP_ERROR_INVALID_CONTENT,
354 "<range/> already specified for this key");
358 for (i = 0; i < G_N_ELEMENTS (table); i++)
359 if (*(char *) state->type == table[i].type)
361 min_str = min_str ? min_str : table[i].min;
362 max_str = max_str ? max_str : table[i].max;
369 gchar *type = g_variant_type_dup_string (state->type);
370 g_set_error (error, G_MARKUP_ERROR,
371 G_MARKUP_ERROR_INVALID_CONTENT,
372 "<range> not allowed for keys of type '%s'", type);
377 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
378 if (state->minimum == NULL)
381 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
382 if (state->maximum == NULL)
385 if (g_variant_compare (state->minimum, state->maximum) > 0)
387 g_set_error (error, G_MARKUP_ERROR,
388 G_MARKUP_ERROR_INVALID_CONTENT,
389 "<range> specified minimum is greater than maxmimum");
393 key_state_check_range (state, error);
397 key_state_start_default (KeyState *state,
399 const gchar *context,
404 if (strcmp (l10n, "messages") == 0)
407 else if (strcmp (l10n, "time") == 0)
412 g_set_error (error, G_MARKUP_ERROR,
413 G_MARKUP_ERROR_INVALID_CONTENT,
414 "unsupported l10n category: %s", l10n);
418 if (!state->have_gettext_domain)
420 g_set_error_literal (error, G_MARKUP_ERROR,
421 G_MARKUP_ERROR_INVALID_CONTENT,
422 "l10n requested, but no "
423 "gettext domain given");
427 state->l10n_context = g_strdup (context);
430 else if (context != NULL)
432 g_set_error_literal (error, G_MARKUP_ERROR,
433 G_MARKUP_ERROR_INVALID_CONTENT,
434 "translation context given for "
435 " value without l10n enabled");
439 return g_string_new (NULL);
443 key_state_end_default (KeyState *state,
447 state->unparsed_default_value = *string;
450 state->default_value = g_variant_parse (state->type,
451 state->unparsed_default_value->str,
453 key_state_check_range (state, error);
457 key_state_start_choices (KeyState *state,
460 const GVariantType *type = state->type;
464 g_set_error_literal (error, G_MARKUP_ERROR,
465 G_MARKUP_ERROR_INVALID_CONTENT,
466 "<choices> cannot be specified for keys "
467 "tagged as having an enumerated type");
471 if (state->has_choices)
473 g_set_error_literal (error, G_MARKUP_ERROR,
474 G_MARKUP_ERROR_INVALID_CONTENT,
475 "<choices> already specified for this key");
479 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
480 type = g_variant_type_element (type);
482 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
484 gchar *type_string = g_variant_type_dup_string (state->type);
485 g_set_error (error, G_MARKUP_ERROR,
486 G_MARKUP_ERROR_INVALID_CONTENT,
487 "<choices> not allowed for keys of type '%s'",
489 g_free (type_string);
495 key_state_add_choice (KeyState *state,
499 if (strinfo_builder_contains (state->strinfo, choice))
501 g_set_error (error, G_MARKUP_ERROR,
502 G_MARKUP_ERROR_INVALID_CONTENT,
503 "<choice value='%s'/> already given", choice);
507 strinfo_builder_append_item (state->strinfo, choice, 0);
508 state->has_choices = TRUE;
512 key_state_end_choices (KeyState *state,
515 if (!state->has_choices)
517 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
518 "<choices> must contain at least one <choice>");
522 key_state_check_range (state, error);
526 key_state_start_aliases (KeyState *state,
529 if (state->has_aliases)
530 g_set_error_literal (error, G_MARKUP_ERROR,
531 G_MARKUP_ERROR_INVALID_CONTENT,
532 "<aliases> already specified for this key");
533 else if (!state->is_flags && !state->is_enum && !state->has_choices)
534 g_set_error_literal (error, G_MARKUP_ERROR,
535 G_MARKUP_ERROR_INVALID_CONTENT,
536 "<aliases> can only be specified for keys with "
537 "enumerated or flags types or after <choices>");
541 key_state_add_alias (KeyState *state,
546 if (strinfo_builder_contains (state->strinfo, alias))
548 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
549 state->strinfo->len / 4,
553 g_set_error (error, G_MARKUP_ERROR,
554 G_MARKUP_ERROR_INVALID_CONTENT,
555 "<alias value='%s'/> given when '%s' is already "
556 "a member of the enumerated type", alias, alias);
559 g_set_error (error, G_MARKUP_ERROR,
560 G_MARKUP_ERROR_INVALID_CONTENT,
561 "<alias value='%s'/> given when "
562 "<choice value='%s'/> was already given",
567 g_set_error (error, G_MARKUP_ERROR,
568 G_MARKUP_ERROR_INVALID_CONTENT,
569 "<alias value='%s'/> already specified", alias);
574 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
576 g_set_error (error, G_MARKUP_ERROR,
577 G_MARKUP_ERROR_INVALID_CONTENT,
578 "alias target '%s' is not in %s", target,
579 state->is_enum ? "enumerated type" : "<choices>");
583 state->has_aliases = TRUE;
587 key_state_end_aliases (KeyState *state,
590 if (!state->has_aliases)
592 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
593 "<aliases> must contain at least one <alias>");
599 key_state_check (KeyState *state,
605 return state->checked = TRUE;
609 key_state_serialise (KeyState *state)
611 if (state->serialised == NULL)
613 if (state->child_schema)
615 state->serialised = g_variant_new_string (state->child_schema);
620 GVariantBuilder builder;
622 g_assert (key_state_check (state, NULL));
624 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
627 g_variant_builder_add_value (&builder, state->default_value);
632 if (state->l10n_context)
636 /* Contextified messages are supported by prepending
637 * the context, followed by '\004' to the start of the
638 * message string. We do that here to save GSettings
641 len = strlen (state->l10n_context);
642 state->l10n_context[len] = '\004';
643 g_string_prepend_len (state->unparsed_default_value,
644 state->l10n_context, len + 1);
645 g_free (state->l10n_context);
646 state->l10n_context = NULL;
649 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
650 state->unparsed_default_value->str);
651 g_string_free (state->unparsed_default_value, TRUE);
652 state->unparsed_default_value = NULL;
655 /* choice, aliases, enums */
656 if (state->strinfo->len)
664 data = state->strinfo->str;
665 size = state->strinfo->len;
668 for (i = 0; i < size / sizeof (guint32); i++)
669 words[i] = GUINT32_TO_LE (words[i]);
671 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
675 g_string_free (state->strinfo, FALSE);
676 state->strinfo = NULL;
678 g_variant_builder_add (&builder, "(y@au)",
679 state->is_flags ? 'f' :
680 state->is_enum ? 'e' : 'c',
685 if (state->minimum || state->maximum)
686 g_variant_builder_add (&builder, "(y(**))", 'r',
687 state->minimum, state->maximum);
689 state->serialised = g_variant_builder_end (&builder);
692 g_variant_ref_sink (state->serialised);
695 return g_variant_ref (state->serialised);
699 key_state_free (gpointer data)
701 KeyState *state = data;
704 g_variant_type_free (state->type);
706 g_free (state->l10n_context);
708 if (state->unparsed_default_value)
709 g_string_free (state->unparsed_default_value, TRUE);
711 if (state->default_value)
712 g_variant_unref (state->default_value);
715 g_string_free (state->strinfo, TRUE);
718 g_variant_unref (state->minimum);
721 g_variant_unref (state->maximum);
723 if (state->serialised)
724 g_variant_unref (state->serialised);
726 g_slice_free (KeyState, state);
729 /* Key name validity {{{1 */
730 static gboolean allow_any_name = FALSE;
733 is_valid_keyname (const gchar *key,
740 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
741 _("empty names are not permitted"));
748 if (!g_ascii_islower (key[0]))
750 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
751 _("invalid name '%s': names must begin "
752 "with a lowercase letter"), key);
756 for (i = 1; key[i]; i++)
759 !g_ascii_islower (key[i]) &&
760 !g_ascii_isdigit (key[i]))
762 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
763 _("invalid name '%s': invalid character '%c'; "
764 "only lowercase letters, numbers and dash ('-') "
765 "are permitted."), key, key[i]);
769 if (key[i] == '-' && key[i + 1] == '-')
771 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
772 _("invalid name '%s': two successive dashes ('--') "
773 "are not permitted."), key);
778 if (key[i - 1] == '-')
780 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
781 _("invalid name '%s': the last character may not be a "
782 "dash ('-')."), key);
788 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
789 _("invalid name '%s': maximum length is 1024"), key);
796 /* Handling of <schema> {{{1 */
797 typedef struct _SchemaState SchemaState;
800 SchemaState *extends;
803 gchar *gettext_domain;
811 schema_state_new (const gchar *path,
812 const gchar *gettext_domain,
813 SchemaState *extends,
814 const gchar *extends_name,
815 const gchar *list_of)
819 state = g_slice_new (SchemaState);
820 state->path = g_strdup (path);
821 state->gettext_domain = g_strdup (gettext_domain);
822 state->extends = extends;
823 state->extends_name = g_strdup (extends_name);
824 state->list_of = g_strdup (list_of);
825 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
826 g_free, key_state_free);
832 schema_state_free (gpointer data)
834 SchemaState *state = data;
836 g_free (state->path);
837 g_free (state->gettext_domain);
838 g_hash_table_unref (state->keys);
842 schema_state_add_child (SchemaState *state,
849 if (!is_valid_keyname (name, error))
852 childname = g_strconcat (name, "/", NULL);
854 if (g_hash_table_lookup (state->keys, childname))
856 g_set_error (error, G_MARKUP_ERROR,
857 G_MARKUP_ERROR_INVALID_CONTENT,
858 _("<child name='%s'> already specified"), name);
862 g_hash_table_insert (state->keys, childname,
863 key_state_new_child (schema));
867 schema_state_add_key (SchemaState *state,
868 GHashTable *enum_table,
869 GHashTable *flags_table,
871 const gchar *type_string,
872 const gchar *enum_type,
873 const gchar *flags_type,
882 g_set_error_literal (error, G_MARKUP_ERROR,
883 G_MARKUP_ERROR_INVALID_CONTENT,
884 _("cannot add keys to a 'list-of' schema"));
888 if (!is_valid_keyname (name, error))
891 if (g_hash_table_lookup (state->keys, name))
893 g_set_error (error, G_MARKUP_ERROR,
894 G_MARKUP_ERROR_INVALID_CONTENT,
895 _("<key name='%s'> already specified"), name);
899 for (node = state; node; node = node->extends)
904 shadow = g_hash_table_lookup (node->extends->keys, name);
906 /* in case of <key> <override> <key> make sure we report the
907 * location of the original <key>, not the <override>.
909 if (shadow && !shadow->is_override)
911 g_set_error (error, G_MARKUP_ERROR,
912 G_MARKUP_ERROR_INVALID_CONTENT,
913 _("<key name='%s'> shadows <key name='%s'> in "
914 "<schema id='%s'>; use <override> to modify value"),
915 name, name, node->extends_name);
920 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
922 g_set_error (error, G_MARKUP_ERROR,
923 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
924 _("exactly one of 'type', 'enum' or 'flags' must "
925 "be specified as an attribute to <key>"));
929 if (type_string == NULL) /* flags or enums was specified */
931 EnumState *enum_state;
934 enum_state = g_hash_table_lookup (enum_table, enum_type);
936 enum_state = g_hash_table_lookup (flags_table, flags_type);
939 if (enum_state == NULL)
941 g_set_error (error, G_MARKUP_ERROR,
942 G_MARKUP_ERROR_INVALID_CONTENT,
943 _("<%s id='%s'> not (yet) defined."),
944 flags_type ? "flags" : "enum",
945 flags_type ? flags_type : enum_type);
949 type_string = flags_type ? "as" : "s";
950 strinfo = enum_state->strinfo;
954 if (!g_variant_type_string_is_valid (type_string))
956 g_set_error (error, G_MARKUP_ERROR,
957 G_MARKUP_ERROR_INVALID_CONTENT,
958 _("invalid GVariant type string '%s'"), type_string);
965 key = key_state_new (type_string, state->gettext_domain,
966 enum_type != NULL, flags_type != NULL, strinfo);
967 g_hash_table_insert (state->keys, g_strdup (name), key);
973 schema_state_add_override (SchemaState *state,
974 KeyState **key_state,
978 const gchar *context,
984 if (state->extends == NULL)
986 g_set_error_literal (error, G_MARKUP_ERROR,
987 G_MARKUP_ERROR_INVALID_CONTENT,
988 _("<override> given but schema isn't "
989 "extending anything"));
993 for (parent = state->extends; parent; parent = parent->extends)
994 if ((original = g_hash_table_lookup (parent->keys, key)))
997 if (original == NULL)
999 g_set_error (error, G_MARKUP_ERROR,
1000 G_MARKUP_ERROR_INVALID_CONTENT,
1001 _("no <key name='%s'> to override"), key);
1005 if (g_hash_table_lookup (state->keys, key))
1007 g_set_error (error, G_MARKUP_ERROR,
1008 G_MARKUP_ERROR_INVALID_CONTENT,
1009 _("<override name='%s'> already specified"), key);
1013 *key_state = key_state_override (original, state->gettext_domain);
1014 *string = key_state_start_default (*key_state, l10n, context, error);
1015 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1019 override_state_end (KeyState **key_state,
1023 key_state_end_default (*key_state, string, error);
1027 /* Handling of toplevel state {{{1 */
1030 GHashTable *schema_table; /* string -> SchemaState */
1031 GHashTable *flags_table; /* string -> EnumState */
1032 GHashTable *enum_table; /* string -> EnumState */
1034 GSList *this_file_schemas; /* strings: <schema>s in this file */
1035 GSList *this_file_flagss; /* strings: <flags>s in this file */
1036 GSList *this_file_enums; /* strings: <enum>s in this file */
1038 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1040 SchemaState *schema_state; /* non-NULL when inside <schema> */
1041 KeyState *key_state; /* non-NULL when inside <key> */
1042 EnumState *enum_state; /* non-NULL when inside <enum> */
1044 GString *string; /* non-NULL when accepting text */
1048 is_subclass (const gchar *class_name,
1049 const gchar *possible_parent,
1050 GHashTable *schema_table)
1054 if (strcmp (class_name, possible_parent) == 0)
1057 class = g_hash_table_lookup (schema_table, class_name);
1058 g_assert (class != NULL);
1060 return class->extends_name &&
1061 is_subclass (class->extends_name, possible_parent, schema_table);
1065 parse_state_start_schema (ParseState *state,
1068 const gchar *gettext_domain,
1069 const gchar *extends_name,
1070 const gchar *list_of,
1073 SchemaState *extends;
1076 if (g_hash_table_lookup (state->schema_table, id))
1078 g_set_error (error, G_MARKUP_ERROR,
1079 G_MARKUP_ERROR_INVALID_CONTENT,
1080 _("<schema id='%s'> already specified"), id);
1086 extends = g_hash_table_lookup (state->schema_table, extends_name);
1088 if (extends == NULL)
1090 g_set_error (error, G_MARKUP_ERROR,
1091 G_MARKUP_ERROR_INVALID_CONTENT,
1092 _("<schema id='%s'> extends not yet "
1093 "existing schema '%s'"), id, extends_name);
1104 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1106 g_set_error (error, G_MARKUP_ERROR,
1107 G_MARKUP_ERROR_INVALID_CONTENT,
1108 _("<schema id='%s'> is list of not yet "
1109 "existing schema '%s'"), id, list_of);
1115 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1116 _("Can not be a list of a schema with a path"));
1125 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1126 _("Can not extend a schema with a path"));
1132 if (extends->list_of == NULL)
1134 g_set_error (error, G_MARKUP_ERROR,
1135 G_MARKUP_ERROR_INVALID_CONTENT,
1136 _("<schema id='%s'> is a list, extending "
1137 "<schema id='%s'> which is not a list"),
1142 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1144 g_set_error (error, G_MARKUP_ERROR,
1145 G_MARKUP_ERROR_INVALID_CONTENT,
1146 _("<schema id='%s' list-of='%s'> extends <schema "
1147 "id='%s' list-of='%s'> but '%s' does not "
1148 "extend '%s'"), id, list_of, extends_name,
1149 extends->list_of, list_of, extends->list_of);
1154 /* by default we are a list of the same thing that the schema
1155 * we are extending is a list of (which might be nothing)
1157 list_of = extends->list_of;
1160 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1162 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1163 _("a path, if given, must begin and end with a slash"));
1167 if (path && list_of && !g_str_has_suffix (path, ":/"))
1169 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1170 _("the path of a list must end with ':/'"));
1174 state->schema_state = schema_state_new (path, gettext_domain,
1175 extends, extends_name, list_of);
1177 my_id = g_strdup (id);
1178 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1179 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1183 parse_state_start_enum (ParseState *state,
1188 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1189 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1192 if (g_hash_table_lookup (table, id))
1194 g_set_error (error, G_MARKUP_ERROR,
1195 G_MARKUP_ERROR_INVALID_CONTENT,
1196 _("<%s id='%s'> already specified"),
1197 is_flags ? "flags" : "enum", id);
1201 state->enum_state = enum_state_new (is_flags);
1203 my_id = g_strdup (id);
1204 *list = g_slist_prepend (*list, my_id);
1205 g_hash_table_insert (table, my_id, state->enum_state);
1208 /* GMarkup Parser Functions {{{1 */
1210 /* Start element {{{2 */
1212 start_element (GMarkupParseContext *context,
1213 const gchar *element_name,
1214 const gchar **attribute_names,
1215 const gchar **attribute_values,
1219 ParseState *state = user_data;
1220 const GSList *element_stack;
1221 const gchar *container;
1223 element_stack = g_markup_parse_context_get_element_stack (context);
1224 container = element_stack->next ? element_stack->next->data : NULL;
1226 #define COLLECT(first, ...) \
1227 g_markup_collect_attributes (element_name, \
1228 attribute_names, attribute_values, error, \
1229 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1230 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1231 #define STRDUP G_MARKUP_COLLECT_STRDUP
1232 #define STRING G_MARKUP_COLLECT_STRING
1233 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1235 /* Toplevel items {{{3 */
1236 if (container == NULL)
1238 if (strcmp (element_name, "schemalist") == 0)
1240 COLLECT (OPTIONAL | STRDUP,
1242 &state->schemalist_domain);
1248 /* children of <schemalist> {{{3 */
1249 else if (strcmp (container, "schemalist") == 0)
1251 if (strcmp (element_name, "schema") == 0)
1253 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1254 if (COLLECT (STRING, "id", &id,
1255 OPTIONAL | STRING, "path", &path,
1256 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1257 OPTIONAL | STRING, "extends", &extends,
1258 OPTIONAL | STRING, "list-of", &list_of))
1259 parse_state_start_schema (state, id, path,
1260 gettext_domain ? gettext_domain
1261 : state->schemalist_domain,
1262 extends, list_of, error);
1266 else if (strcmp (element_name, "enum") == 0)
1269 if (COLLECT (STRING, "id", &id))
1270 parse_state_start_enum (state, id, FALSE, error);
1274 else if (strcmp (element_name, "flags") == 0)
1277 if (COLLECT (STRING, "id", &id))
1278 parse_state_start_enum (state, id, TRUE, error);
1284 /* children of <schema> {{{3 */
1285 else if (strcmp (container, "schema") == 0)
1287 if (strcmp (element_name, "key") == 0)
1289 const gchar *name, *type_string, *enum_type, *flags_type;
1291 if (COLLECT (STRING, "name", &name,
1292 OPTIONAL | STRING, "type", &type_string,
1293 OPTIONAL | STRING, "enum", &enum_type,
1294 OPTIONAL | STRING, "flags", &flags_type))
1296 state->key_state = schema_state_add_key (state->schema_state,
1300 enum_type, flags_type,
1304 else if (strcmp (element_name, "child") == 0)
1306 const gchar *name, *schema;
1308 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1309 schema_state_add_child (state->schema_state,
1310 name, schema, error);
1313 else if (strcmp (element_name, "override") == 0)
1315 const gchar *name, *l10n, *context;
1317 if (COLLECT (STRING, "name", &name,
1318 OPTIONAL | STRING, "l10n", &l10n,
1319 OPTIONAL | STRING, "context", &context))
1320 schema_state_add_override (state->schema_state,
1321 &state->key_state, &state->string,
1322 name, l10n, context, error);
1327 /* children of <key> {{{3 */
1328 else if (strcmp (container, "key") == 0)
1330 if (strcmp (element_name, "default") == 0)
1332 const gchar *l10n, *context;
1333 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1334 STRING | OPTIONAL, "context", &context))
1335 state->string = key_state_start_default (state->key_state,
1336 l10n, context, error);
1340 else if (strcmp (element_name, "summary") == 0 ||
1341 strcmp (element_name, "description") == 0)
1344 state->string = g_string_new (NULL);
1348 else if (strcmp (element_name, "range") == 0)
1350 const gchar *min, *max;
1351 if (COLLECT (STRING | OPTIONAL, "min", &min,
1352 STRING | OPTIONAL, "max", &max))
1353 key_state_set_range (state->key_state, min, max, error);
1357 else if (strcmp (element_name, "choices") == 0)
1360 key_state_start_choices (state->key_state, error);
1364 else if (strcmp (element_name, "aliases") == 0)
1367 key_state_start_aliases (state->key_state, error);
1373 /* children of <choices> {{{3 */
1374 else if (strcmp (container, "choices") == 0)
1376 if (strcmp (element_name, "choice") == 0)
1379 if (COLLECT (STRING, "value", &value))
1380 key_state_add_choice (state->key_state, value, error);
1386 /* children of <aliases> {{{3 */
1387 else if (strcmp (container, "aliases") == 0)
1389 if (strcmp (element_name, "alias") == 0)
1391 const gchar *value, *target;
1392 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1393 key_state_add_alias (state->key_state, value, target, error);
1399 /* children of <enum> {{{3 */
1400 else if (strcmp (container, "enum") == 0 ||
1401 strcmp (container, "flags") == 0)
1403 if (strcmp (element_name, "value") == 0)
1405 const gchar *nick, *valuestr;
1406 if (COLLECT (STRING, "nick", &nick,
1407 STRING, "value", &valuestr))
1408 enum_state_add_value (state->enum_state, nick, valuestr, error);
1415 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1416 _("Element <%s> not allowed inside <%s>"),
1417 element_name, container);
1419 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1420 _("Element <%s> not allowed at toplevel"), element_name);
1423 /* End element {{{2 */
1426 key_state_end (KeyState **state_ptr,
1434 if (state->default_value == NULL)
1436 g_set_error_literal (error,
1437 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1438 "element <default> is required in <key>");
1444 schema_state_end (SchemaState **state_ptr,
1451 end_element (GMarkupParseContext *context,
1452 const gchar *element_name,
1456 ParseState *state = user_data;
1458 if (strcmp (element_name, "schemalist") == 0)
1460 g_free (state->schemalist_domain);
1461 state->schemalist_domain = NULL;
1464 else if (strcmp (element_name, "enum") == 0 ||
1465 strcmp (element_name, "flags") == 0)
1466 enum_state_end (&state->enum_state, error);
1468 else if (strcmp (element_name, "schema") == 0)
1469 schema_state_end (&state->schema_state, error);
1471 else if (strcmp (element_name, "override") == 0)
1472 override_state_end (&state->key_state, &state->string, error);
1474 else if (strcmp (element_name, "key") == 0)
1475 key_state_end (&state->key_state, error);
1477 else if (strcmp (element_name, "default") == 0)
1478 key_state_end_default (state->key_state, &state->string, error);
1480 else if (strcmp (element_name, "choices") == 0)
1481 key_state_end_choices (state->key_state, error);
1483 else if (strcmp (element_name, "aliases") == 0)
1484 key_state_end_aliases (state->key_state, error);
1488 g_string_free (state->string, TRUE);
1489 state->string = NULL;
1494 text (GMarkupParseContext *context,
1500 ParseState *state = user_data;
1503 for (i = 0; i < text_len; i++)
1504 if (!g_ascii_isspace (text[i]))
1507 g_string_append_len (state->string, text, text_len);
1510 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1511 _("text may not appear inside <%s>"),
1512 g_markup_parse_context_get_element (context));
1518 /* Write to GVDB {{{1 */
1526 gvdb_pair_init (GvdbPair *pair)
1528 pair->table = gvdb_hash_table_new (NULL, NULL);
1529 pair->root = gvdb_hash_table_insert (pair->table, "");
1534 GHashTable *schema_table;
1540 GHashTable *schema_table;
1546 output_key (gpointer key,
1550 OutputSchemaData *data;
1559 item = gvdb_hash_table_insert (data->pair.table, name);
1560 gvdb_item_set_parent (item, data->pair.root);
1561 gvdb_item_set_value (item, key_state_serialise (state));
1566 if (state->child_schema &&
1567 !g_hash_table_lookup (data->schema_table, state->child_schema))
1568 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1569 state->child_schema);
1573 output_schema (gpointer key,
1577 WriteToFileData *wtf_data = user_data;
1578 OutputSchemaData data;
1579 GvdbPair *root_pair;
1586 root_pair = &wtf_data->root_pair;
1588 data.schema_table = wtf_data->schema_table;
1589 gvdb_pair_init (&data.pair);
1592 item = gvdb_hash_table_insert (root_pair->table, id);
1593 gvdb_item_set_parent (item, root_pair->root);
1594 gvdb_item_set_hash_table (item, data.pair.table);
1596 g_hash_table_foreach (state->keys, output_key, &data);
1599 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1601 if (state->extends_name)
1602 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1603 state->extends_name);
1606 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1610 gvdb_hash_table_insert_string (data.pair.table,
1612 state->gettext_domain);
1616 write_to_file (GHashTable *schema_table,
1617 const gchar *filename,
1620 WriteToFileData data;
1623 data.schema_table = schema_table;
1625 gvdb_pair_init (&data.root_pair);
1627 g_hash_table_foreach (schema_table, output_schema, &data);
1629 success = gvdb_table_write_contents (data.root_pair.table, filename,
1630 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1632 g_hash_table_unref (data.root_pair.table);
1637 /* Parser driver {{{1 */
1639 parse_gschema_files (gchar **files,
1642 GMarkupParser parser = { start_element, end_element, text };
1643 ParseState state = { 0, };
1644 const gchar *filename;
1645 GError *error = NULL;
1647 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1648 g_free, enum_state_free);
1650 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1651 g_free, enum_state_free);
1653 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1654 g_free, schema_state_free);
1656 while ((filename = *files++) != NULL)
1658 GMarkupParseContext *context;
1662 if (!g_file_get_contents (filename, &contents, &size, &error))
1664 fprintf (stderr, "%s\n", error->message);
1665 g_clear_error (&error);
1669 context = g_markup_parse_context_new (&parser,
1670 G_MARKUP_TREAT_CDATA_AS_TEXT |
1671 G_MARKUP_PREFIX_ERROR_POSITION,
1675 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1676 !g_markup_parse_context_end_parse (context, &error))
1680 /* back out any changes from this file */
1681 for (item = state.this_file_schemas; item; item = item->next)
1682 g_hash_table_remove (state.schema_table, item->data);
1684 for (item = state.this_file_flagss; item; item = item->next)
1685 g_hash_table_remove (state.flags_table, item->data);
1687 for (item = state.this_file_enums; item; item = item->next)
1688 g_hash_table_remove (state.enum_table, item->data);
1691 fprintf (stderr, "%s: %s. ", filename, error->message);
1692 g_clear_error (&error);
1696 /* Translators: Do not translate "--strict". */
1697 fprintf (stderr, _("--strict was specified; exiting.\n"));
1698 g_hash_table_unref (state.schema_table);
1699 g_hash_table_unref (state.flags_table);
1700 g_hash_table_unref (state.enum_table);
1705 fprintf (stderr, _("This entire file has been ignored.\n"));
1709 g_markup_parse_context_free (context);
1710 g_slist_free (state.this_file_schemas);
1711 g_slist_free (state.this_file_flagss);
1712 g_slist_free (state.this_file_enums);
1713 state.this_file_schemas = NULL;
1714 state.this_file_flagss = NULL;
1715 state.this_file_enums = NULL;
1718 g_hash_table_unref (state.flags_table);
1719 g_hash_table_unref (state.enum_table);
1721 return state.schema_table;
1725 compare_strings (gconstpointer a,
1728 gchar *one = *(gchar **) a;
1729 gchar *two = *(gchar **) b;
1732 cmp = g_str_has_suffix (two, ".enums.xml") -
1733 g_str_has_suffix (one, ".enums.xml");
1736 cmp = strcmp (one, two);
1742 set_overrides (GHashTable *schema_table,
1746 const gchar *filename;
1747 GError *error = NULL;
1749 while ((filename = *files++))
1755 key_file = g_key_file_new ();
1756 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1758 fprintf (stderr, "%s: %s. ", filename, error->message);
1759 g_key_file_free (key_file);
1760 g_clear_error (&error);
1764 fprintf (stderr, _("Ignoring this file.\n"));
1768 fprintf (stderr, _("--strict was specified; exiting.\n"));
1772 groups = g_key_file_get_groups (key_file, NULL);
1774 for (i = 0; groups[i]; i++)
1776 const gchar *group = groups[i];
1777 SchemaState *schema;
1781 schema = g_hash_table_lookup (schema_table, group);
1784 /* Having the schema not be installed is expected to be a
1785 * common case. Don't even emit an error message about
1790 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1791 g_assert (keys != NULL);
1793 for (j = 0; keys[j]; j++)
1795 const gchar *key = keys[j];
1800 state = g_hash_table_lookup (schema->keys, key);
1804 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1805 "specified in override file `%s'"),
1806 key, group, filename);
1810 fprintf (stderr, _("; ignoring override for this key.\n"));
1814 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1815 g_key_file_free (key_file);
1816 g_strfreev (groups);
1822 string = g_key_file_get_value (key_file, group, key, NULL);
1823 g_assert (string != NULL);
1825 value = g_variant_parse (state->type, string,
1826 NULL, NULL, &error);
1830 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1831 "as specified in override file `%s': "
1833 key, group, filename, error->message);
1835 g_clear_error (&error);
1840 fprintf (stderr, _("Ignoring override for this key.\n"));
1844 fprintf (stderr, _("--strict was specified; exiting.\n"));
1845 g_key_file_free (key_file);
1846 g_strfreev (groups);
1854 if (g_variant_compare (value, state->minimum) < 0 ||
1855 g_variant_compare (value, state->maximum) > 0)
1858 _("override for key `%s' in schema `%s' in "
1859 "override file `%s' is out of the range "
1860 "given in the schema"),
1861 key, group, filename);
1863 g_variant_unref (value);
1868 fprintf (stderr, _("; ignoring override for this key.\n"));
1872 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1873 g_key_file_free (key_file);
1874 g_strfreev (groups);
1881 else if (state->strinfo->len)
1883 if (!is_valid_choices (value, state->strinfo))
1886 _("override for key `%s' in schema `%s' in "
1887 "override file `%s' is not in the list "
1888 "of valid choices"),
1889 key, group, filename);
1891 g_variant_unref (value);
1896 fprintf (stderr, _("; ignoring override for this key.\n"));
1900 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1901 g_key_file_free (key_file);
1902 g_strfreev (groups);
1909 g_variant_unref (state->default_value);
1910 state->default_value = value;
1917 g_strfreev (groups);
1924 main (int argc, char **argv)
1931 gchar *targetdir = NULL;
1933 gboolean dry_run = FALSE;
1934 gboolean strict = FALSE;
1935 gchar **schema_files = NULL;
1936 gchar **override_files = NULL;
1937 GOptionContext *context;
1938 GOptionEntry entries[] = {
1939 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1940 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1941 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1942 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1944 /* These options are only for use in the gschema-compile tests */
1945 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1950 extern gchar *_glib_get_locale_dir (void);
1954 setlocale (LC_ALL, "");
1955 textdomain (GETTEXT_PACKAGE);
1958 tmp = _glib_get_locale_dir ();
1959 bindtextdomain (GETTEXT_PACKAGE, tmp);
1962 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1965 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1966 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1969 context = g_option_context_new (N_("DIRECTORY"));
1970 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1971 g_option_context_set_summary (context,
1972 N_("Compile all GSettings schema files into a schema cache.\n"
1973 "Schema files are required to have the extension .gschema.xml,\n"
1974 "and the cache file is called gschemas.compiled."));
1975 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1978 if (!g_option_context_parse (context, &argc, &argv, &error))
1980 fprintf (stderr, "%s\n", error->message);
1984 g_option_context_free (context);
1986 if (!schema_files && argc != 2)
1988 fprintf (stderr, _("You should give exactly one directory name\n"));
1994 if (targetdir == NULL)
1997 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2001 GPtrArray *overrides;
2004 files = g_ptr_array_new ();
2005 overrides = g_ptr_array_new ();
2007 dir = g_dir_open (srcdir, 0, &error);
2010 fprintf (stderr, "%s\n", error->message);
2014 while ((file = g_dir_read_name (dir)) != NULL)
2016 if (g_str_has_suffix (file, ".gschema.xml") ||
2017 g_str_has_suffix (file, ".enums.xml"))
2018 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2020 else if (g_str_has_suffix (file, ".gschema.override"))
2021 g_ptr_array_add (overrides,
2022 g_build_filename (srcdir, file, NULL));
2025 if (files->len == 0)
2027 fprintf (stdout, _("No schema files found: "));
2029 if (g_unlink (target))
2030 fprintf (stdout, _("doing nothing.\n"));
2033 fprintf (stdout, _("removed existing output file.\n"));
2037 g_ptr_array_sort (files, compare_strings);
2038 g_ptr_array_add (files, NULL);
2040 g_ptr_array_sort (overrides, compare_strings);
2041 g_ptr_array_add (overrides, NULL);
2043 schema_files = (char **) g_ptr_array_free (files, FALSE);
2044 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2047 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2053 if (override_files != NULL &&
2054 !set_overrides (table, override_files, strict))
2060 if (!dry_run && !write_to_file (table, target, &error))
2062 fprintf (stderr, "%s\n", error->message);
2074 /* vim:set foldmethod=marker: */