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>
34 #include "gvdb/gvdb-builder.h"
37 /* Handling of <enum> {{{1 */
46 enum_state_free (gpointer data)
48 EnumState *state = data;
50 g_string_free (state->strinfo, TRUE);
51 g_slice_free (EnumState, state);
55 enum_state_new (gboolean is_flags)
59 state = g_slice_new (EnumState);
60 state->strinfo = g_string_new (NULL);
61 state->is_flags = is_flags;
67 enum_state_add_value (EnumState *state,
69 const gchar *valuestr,
75 if (nick[0] == '\0' || nick[1] == '\0')
77 g_set_error (error, G_MARKUP_ERROR,
78 G_MARKUP_ERROR_INVALID_CONTENT,
79 "nick must be a minimum of 2 characters");
83 value = g_ascii_strtoll (valuestr, &end, 0);
84 if (*end || state->is_flags ?
85 (value > G_MAXUINT32 || value < 0) :
86 (value > G_MAXINT32 || value < G_MININT32))
88 g_set_error (error, G_MARKUP_ERROR,
89 G_MARKUP_ERROR_INVALID_CONTENT,
90 "invalid numeric value");
94 if (strinfo_builder_contains (state->strinfo, nick))
96 g_set_error (error, G_MARKUP_ERROR,
97 G_MARKUP_ERROR_INVALID_CONTENT,
98 "<value nick='%s'/> already specified", nick);
102 if (strinfo_builder_contains_value (state->strinfo, value))
104 g_set_error (error, G_MARKUP_ERROR,
105 G_MARKUP_ERROR_INVALID_CONTENT,
106 "value='%s' already specified", valuestr);
110 /* Silently drop the null case if it is mentioned.
111 * It is properly denoted with an empty array.
113 if (state->is_flags && value == 0)
116 if (state->is_flags && (value & (value - 1)))
118 g_set_error (error, G_MARKUP_ERROR,
119 G_MARKUP_ERROR_INVALID_CONTENT,
120 "flags values must have at most 1 bit set");
124 /* Since we reject exact duplicates of value='' and we only allow one
125 * bit to be set, it's not possible to have overlaps.
127 * If we loosen the one-bit-set restriction we need an overlap check.
130 strinfo_builder_append_item (state->strinfo, nick, value);
134 enum_state_end (EnumState **state_ptr,
142 if (state->strinfo->len == 0)
144 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
145 "<%s> must contain at least one <value>",
146 state->is_flags ? "flags" : "enum");
149 /* Handling of <key> {{{1 */
152 /* for <child>, @child_schema will be set.
153 * for <key>, everything else will be set.
159 gboolean have_gettext_domain;
163 GString *unparsed_default_value;
164 GVariant *default_value;
173 gboolean has_choices;
174 gboolean has_aliases;
175 gboolean is_override;
178 GVariant *serialised;
182 key_state_new (const gchar *type_string,
183 const gchar *gettext_domain,
190 state = g_slice_new0 (KeyState);
191 state->type = g_variant_type_new (type_string);
192 state->have_gettext_domain = gettext_domain != NULL;
193 state->is_enum = is_enum;
194 state->is_flags = is_flags;
197 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
199 state->strinfo = g_string_new (NULL);
205 key_state_override (KeyState *state,
206 const gchar *gettext_domain)
210 copy = g_slice_new0 (KeyState);
211 copy->type = g_variant_type_copy (state->type);
212 copy->have_gettext_domain = gettext_domain != NULL;
213 copy->strinfo = g_string_new_len (state->strinfo->str,
214 state->strinfo->len);
215 copy->is_enum = state->is_enum;
216 copy->is_flags = state->is_flags;
217 copy->is_override = TRUE;
221 copy->minimum = g_variant_ref (state->minimum);
222 copy->maximum = g_variant_ref (state->maximum);
229 key_state_new_child (const gchar *child_schema)
233 state = g_slice_new0 (KeyState);
234 state->child_schema = g_strdup (child_schema);
240 is_valid_choices (GVariant *variant,
243 switch (g_variant_classify (variant))
245 case G_VARIANT_CLASS_MAYBE:
246 case G_VARIANT_CLASS_ARRAY:
248 gboolean valid = TRUE;
251 g_variant_iter_init (&iter, variant);
253 while (valid && (variant = g_variant_iter_next_value (&iter)))
255 valid = is_valid_choices (variant, strinfo);
256 g_variant_unref (variant);
262 case G_VARIANT_CLASS_STRING:
263 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
265 g_variant_get_string (variant, NULL));
268 g_assert_not_reached ();
273 /* Gets called at </default> </choices> or <range/> to check for
274 * validity of the default value so that any inconsistency is
275 * reported as soon as it is encountered.
278 key_state_check_range (KeyState *state,
281 if (state->default_value)
285 tag = state->is_override ? "override" : "default";
289 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
290 g_variant_compare (state->default_value, state->maximum) > 0)
292 g_set_error (error, G_MARKUP_ERROR,
293 G_MARKUP_ERROR_INVALID_CONTENT,
294 "<%s> is not contained in "
295 "the specified range", tag);
299 else if (state->strinfo->len)
301 if (!is_valid_choices (state->default_value, state->strinfo))
304 g_set_error (error, G_MARKUP_ERROR,
305 G_MARKUP_ERROR_INVALID_CONTENT,
306 "<%s> is not a valid member of "
307 "the specified enumerated type", tag);
309 else if (state->is_flags)
310 g_set_error (error, G_MARKUP_ERROR,
311 G_MARKUP_ERROR_INVALID_CONTENT,
312 "<%s> contains string not in the "
313 "specified flags type", tag);
316 g_set_error (error, G_MARKUP_ERROR,
317 G_MARKUP_ERROR_INVALID_CONTENT,
318 "<%s> contains string not in "
326 key_state_set_range (KeyState *state,
327 const gchar *min_str,
328 const gchar *max_str,
333 g_set_error_literal (error, G_MARKUP_ERROR,
334 G_MARKUP_ERROR_INVALID_CONTENT,
335 "<range/> already specified for this key");
339 if (strchr ("ynqiuxtd", *(char *) state->type) == NULL)
341 gchar *type = g_variant_type_dup_string (state->type);
342 g_set_error (error, G_MARKUP_ERROR,
343 G_MARKUP_ERROR_INVALID_CONTENT,
344 "<range> not allowed for keys of type '%s'", type);
349 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
350 if (state->minimum == NULL)
353 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
354 if (state->maximum == NULL)
357 if (g_variant_compare (state->minimum, state->maximum) > 0)
359 g_set_error (error, G_MARKUP_ERROR,
360 G_MARKUP_ERROR_INVALID_CONTENT,
361 "<range> specified minimum is greater than maxmimum");
365 key_state_check_range (state, error);
369 key_state_start_default (KeyState *state,
371 const gchar *context,
376 if (strcmp (l10n, "messages") == 0)
379 else if (strcmp (l10n, "time") == 0)
384 g_set_error (error, G_MARKUP_ERROR,
385 G_MARKUP_ERROR_INVALID_CONTENT,
386 "unsupported l10n category: %s", l10n);
390 if (!state->have_gettext_domain)
392 g_set_error_literal (error, G_MARKUP_ERROR,
393 G_MARKUP_ERROR_INVALID_CONTENT,
394 "l10n requested, but no "
395 "gettext domain given");
399 state->l10n_context = g_strdup (context);
402 else if (context != NULL)
404 g_set_error_literal (error, G_MARKUP_ERROR,
405 G_MARKUP_ERROR_INVALID_CONTENT,
406 "translation context given for "
407 " value without l10n enabled");
411 return g_string_new (NULL);
415 key_state_end_default (KeyState *state,
419 state->unparsed_default_value = *string;
422 state->default_value = g_variant_parse (state->type,
423 state->unparsed_default_value->str,
425 key_state_check_range (state, error);
429 key_state_start_choices (KeyState *state,
432 const GVariantType *type = state->type;
436 g_set_error_literal (error, G_MARKUP_ERROR,
437 G_MARKUP_ERROR_INVALID_CONTENT,
438 "<choices> can not be specified for keys "
439 "tagged as having an enumerated type");
443 if (state->has_choices)
445 g_set_error_literal (error, G_MARKUP_ERROR,
446 G_MARKUP_ERROR_INVALID_CONTENT,
447 "<choices> already specified for this key");
451 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
452 type = g_variant_type_element (type);
454 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
456 gchar *type_string = g_variant_type_dup_string (state->type);
457 g_set_error (error, G_MARKUP_ERROR,
458 G_MARKUP_ERROR_INVALID_CONTENT,
459 "<choices> not allowed for keys of type '%s'",
461 g_free (type_string);
467 key_state_add_choice (KeyState *state,
471 if (strinfo_builder_contains (state->strinfo, choice))
473 g_set_error (error, G_MARKUP_ERROR,
474 G_MARKUP_ERROR_INVALID_CONTENT,
475 "<choice value='%s'/> already given", choice);
479 strinfo_builder_append_item (state->strinfo, choice, 0);
480 state->has_choices = TRUE;
484 key_state_end_choices (KeyState *state,
487 if (!state->has_choices)
489 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
490 "<choices> must contain at least one <choice>");
494 key_state_check_range (state, error);
498 key_state_start_aliases (KeyState *state,
501 if (state->has_aliases)
502 g_set_error_literal (error, G_MARKUP_ERROR,
503 G_MARKUP_ERROR_INVALID_CONTENT,
504 "<aliases> already specified for this key");
506 if (!state->is_flags && !state->is_enum && !state->has_choices)
507 g_set_error_literal (error, G_MARKUP_ERROR,
508 G_MARKUP_ERROR_INVALID_CONTENT,
509 "<aliases> can only be specified for keys with "
510 "enumerated or flags types or after <choices>");
514 key_state_add_alias (KeyState *state,
519 if (strinfo_builder_contains (state->strinfo, alias))
521 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
522 state->strinfo->len / 4,
526 g_set_error (error, G_MARKUP_ERROR,
527 G_MARKUP_ERROR_INVALID_CONTENT,
528 "<alias value='%s'/> given when '%s' is already "
529 "a member of the enumerated type", alias, alias);
532 g_set_error (error, G_MARKUP_ERROR,
533 G_MARKUP_ERROR_INVALID_CONTENT,
534 "<alias value='%s'/> given when "
535 "<choice value='%s'/> was already given",
540 g_set_error (error, G_MARKUP_ERROR,
541 G_MARKUP_ERROR_INVALID_CONTENT,
542 "<alias value='%s'/> already specified", alias);
547 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
549 g_set_error (error, G_MARKUP_ERROR,
550 G_MARKUP_ERROR_INVALID_CONTENT,
551 "alias target '%s' is not in %s", target,
552 state->is_enum ? "enumerated type" : "<choices>");
556 state->has_aliases = TRUE;
560 key_state_end_aliases (KeyState *state,
563 if (!state->has_aliases)
565 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
566 "<aliases> must contain at least one <alias>");
572 key_state_check (KeyState *state,
578 return state->checked = TRUE;
582 key_state_serialise (KeyState *state)
584 if (state->serialised == NULL)
586 if (state->child_schema)
588 state->serialised = g_variant_new_string (state->child_schema);
593 GVariantBuilder builder;
595 g_assert (key_state_check (state, NULL));
597 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
600 g_variant_builder_add_value (&builder, state->default_value);
605 if (state->l10n_context)
609 /* Contextified messages are supported by prepending
610 * the context, followed by '\004' to the start of the
611 * message string. We do that here to save GSettings
614 len = strlen (state->l10n_context);
615 state->l10n_context[len] = '\004';
616 g_string_prepend_len (state->unparsed_default_value,
617 state->l10n_context, len + 1);
618 g_free (state->l10n_context);
619 state->l10n_context = NULL;
622 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
623 state->unparsed_default_value->str);
624 g_string_free (state->unparsed_default_value, TRUE);
625 state->unparsed_default_value = NULL;
628 /* choice, aliases, enums */
629 if (state->strinfo->len)
637 data = state->strinfo->str;
638 size = state->strinfo->len;
641 for (i = 0; i < size / sizeof (guint32); i++)
642 words[i] = GUINT32_TO_LE (words[i]);
644 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
648 g_string_free (state->strinfo, FALSE);
649 state->strinfo = NULL;
651 g_variant_builder_add (&builder, "(y@au)",
652 state->is_flags ? 'f' :
653 state->is_enum ? 'e' : 'c',
658 if (state->minimum || state->maximum)
659 g_variant_builder_add (&builder, "(y(**))", 'r',
660 state->minimum, state->maximum);
662 state->serialised = g_variant_builder_end (&builder);
665 g_variant_ref_sink (state->serialised);
668 return g_variant_ref (state->serialised);
672 key_state_free (gpointer data)
674 KeyState *state = data;
677 g_variant_type_free (state->type);
679 g_free (state->l10n_context);
681 if (state->unparsed_default_value)
682 g_string_free (state->unparsed_default_value, TRUE);
684 if (state->default_value)
685 g_variant_unref (state->default_value);
688 g_string_free (state->strinfo, TRUE);
691 g_variant_unref (state->minimum);
694 g_variant_unref (state->maximum);
696 if (state->serialised)
697 g_variant_unref (state->serialised);
699 g_slice_free (KeyState, state);
702 /* Key name validity {{{1 */
703 static gboolean allow_any_name = FALSE;
706 is_valid_keyname (const gchar *key,
713 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
714 _("empty names are not permitted"));
721 if (!g_ascii_islower (key[0]))
723 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
724 _("invalid name '%s': names must begin "
725 "with a lowercase letter"), key);
729 for (i = 1; key[i]; i++)
732 !g_ascii_islower (key[i]) &&
733 !g_ascii_isdigit (key[i]))
735 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
736 _("invalid name '%s': invalid character '%c'; "
737 "only lowercase letters, numbers and dash ('-') "
738 "are permitted."), key, key[i]);
742 if (key[i] == '-' && key[i + 1] == '-')
744 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
745 _("invalid name '%s': two successive dashes ('--') "
746 "are not permitted."), key);
751 if (key[i - 1] == '-')
753 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
754 _("invalid name '%s': the last character may not be a "
755 "dash ('-')."), key);
761 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
762 _("invalid name '%s': maximum length is 32"), key);
769 /* Handling of <schema> {{{1 */
770 typedef struct _SchemaState SchemaState;
773 SchemaState *extends;
776 gchar *gettext_domain;
784 schema_state_new (const gchar *path,
785 const gchar *gettext_domain,
786 SchemaState *extends,
787 const gchar *extends_name,
788 const gchar *list_of)
792 state = g_slice_new (SchemaState);
793 state->path = g_strdup (path);
794 state->gettext_domain = g_strdup (gettext_domain);
795 state->extends = extends;
796 state->extends_name = g_strdup (extends_name);
797 state->list_of = g_strdup (list_of);
798 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
799 g_free, key_state_free);
805 schema_state_free (gpointer data)
807 SchemaState *state = data;
809 g_free (state->path);
810 g_free (state->gettext_domain);
811 g_hash_table_unref (state->keys);
815 schema_state_add_child (SchemaState *state,
822 if (!is_valid_keyname (name, error))
825 childname = g_strconcat (name, "/", NULL);
827 if (g_hash_table_lookup (state->keys, childname))
829 g_set_error (error, G_MARKUP_ERROR,
830 G_MARKUP_ERROR_INVALID_CONTENT,
831 _("<child name='%s'> already specified"), name);
835 g_hash_table_insert (state->keys, childname,
836 key_state_new_child (schema));
840 schema_state_add_key (SchemaState *state,
841 GHashTable *enum_table,
842 GHashTable *flags_table,
844 const gchar *type_string,
845 const gchar *enum_type,
846 const gchar *flags_type,
855 g_set_error_literal (error, G_MARKUP_ERROR,
856 G_MARKUP_ERROR_INVALID_CONTENT,
857 _("can not add keys to a 'list-of' schema"));
861 if (!is_valid_keyname (name, error))
864 if (g_hash_table_lookup (state->keys, name))
866 g_set_error (error, G_MARKUP_ERROR,
867 G_MARKUP_ERROR_INVALID_CONTENT,
868 _("<key name='%s'> already specified"), name);
872 for (node = state; node; node = node->extends)
877 shadow = g_hash_table_lookup (node->extends->keys, name);
879 /* in case of <key> <override> <key> make sure we report the
880 * location of the original <key>, not the <override>.
882 if (shadow && !shadow->is_override)
884 g_set_error (error, G_MARKUP_ERROR,
885 G_MARKUP_ERROR_INVALID_CONTENT,
886 _("<key name='%s'> shadows <key name='%s'> in "
887 "<schema id='%s'>; use <override> to modify value"),
888 name, name, node->extends_name);
893 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
895 g_set_error (error, G_MARKUP_ERROR,
896 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
897 _("exactly one of 'type', 'enum' or 'flags' must "
898 "be specified as an attribute to <key>"));
902 if (type_string == NULL) /* flags or enums was specified */
904 EnumState *enum_state;
907 enum_state = g_hash_table_lookup (enum_table, enum_type);
909 enum_state = g_hash_table_lookup (flags_table, flags_type);
912 if (enum_state == NULL)
914 g_set_error (error, G_MARKUP_ERROR,
915 G_MARKUP_ERROR_INVALID_CONTENT,
916 _("<%s id='%s'> not (yet) defined."),
917 flags_type ? "flags" : "enum",
918 flags_type ? flags_type : enum_type);
922 type_string = flags_type ? "as" : "s";
923 strinfo = enum_state->strinfo;
927 if (!g_variant_type_string_is_valid (type_string))
929 g_set_error (error, G_MARKUP_ERROR,
930 G_MARKUP_ERROR_INVALID_CONTENT,
931 _("invalid GVariant type string '%s'"), type_string);
938 key = key_state_new (type_string, state->gettext_domain,
939 enum_type != NULL, flags_type != NULL, strinfo);
940 g_hash_table_insert (state->keys, g_strdup (name), key);
946 schema_state_add_override (SchemaState *state,
947 KeyState **key_state,
951 const gchar *context,
957 if (state->extends == NULL)
959 g_set_error_literal (error, G_MARKUP_ERROR,
960 G_MARKUP_ERROR_INVALID_CONTENT,
961 _("<override> given but schema isn't "
962 "extending anything"));
966 for (parent = state->extends; parent; parent = parent->extends)
967 if ((original = g_hash_table_lookup (parent->keys, key)))
970 if (original == NULL)
972 g_set_error (error, G_MARKUP_ERROR,
973 G_MARKUP_ERROR_INVALID_CONTENT,
974 _("no <key name='%s'> to override"), key);
978 if (g_hash_table_lookup (state->keys, key))
980 g_set_error (error, G_MARKUP_ERROR,
981 G_MARKUP_ERROR_INVALID_CONTENT,
982 _("<override name='%s'> already specified"), key);
986 *key_state = key_state_override (original, state->gettext_domain);
987 *string = key_state_start_default (*key_state, l10n, context, error);
988 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
992 override_state_end (KeyState **key_state,
996 key_state_end_default (*key_state, string, error);
1000 /* Handling of toplevel state {{{1 */
1003 GHashTable *schema_table; /* string -> SchemaState */
1004 GHashTable *flags_table; /* string -> EnumState */
1005 GHashTable *enum_table; /* string -> EnumState */
1007 GSList *this_file_schemas; /* strings: <schema>s in this file */
1008 GSList *this_file_flagss; /* strings: <flags>s in this file */
1009 GSList *this_file_enums; /* strings: <enum>s in this file */
1011 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1013 SchemaState *schema_state; /* non-NULL when inside <schema> */
1014 KeyState *key_state; /* non-NULL when inside <key> */
1015 EnumState *enum_state; /* non-NULL when inside <enum> */
1017 GString *string; /* non-NULL when accepting text */
1021 is_subclass (const gchar *class_name,
1022 const gchar *possible_parent,
1023 GHashTable *schema_table)
1027 if (strcmp (class_name, possible_parent) == 0)
1030 class = g_hash_table_lookup (schema_table, class_name);
1031 g_assert (class != NULL);
1033 return class->extends_name &&
1034 is_subclass (class->extends_name, possible_parent, schema_table);
1038 parse_state_start_schema (ParseState *state,
1041 const gchar *gettext_domain,
1042 const gchar *extends_name,
1043 const gchar *list_of,
1046 SchemaState *extends;
1049 if (g_hash_table_lookup (state->schema_table, id))
1051 g_set_error (error, G_MARKUP_ERROR,
1052 G_MARKUP_ERROR_INVALID_CONTENT,
1053 _("<schema id='%s'> already specified"), id);
1059 extends = g_hash_table_lookup (state->schema_table, extends_name);
1061 if (extends == NULL)
1063 g_set_error (error, G_MARKUP_ERROR,
1064 G_MARKUP_ERROR_INVALID_CONTENT,
1065 _("<schema id='%s'> extends not yet "
1066 "existing schema '%s'"), id, extends_name);
1077 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1079 g_set_error (error, G_MARKUP_ERROR,
1080 G_MARKUP_ERROR_INVALID_CONTENT,
1081 _("<schema id='%s'> is list of not yet "
1082 "existing schema '%s'"), id, list_of);
1088 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1089 _("Can not be a list of a schema with a path"));
1098 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1099 _("Can not extend a schema with a path"));
1105 if (extends->list_of == NULL)
1107 g_set_error (error, G_MARKUP_ERROR,
1108 G_MARKUP_ERROR_INVALID_CONTENT,
1109 _("<schema id='%s'> is a list, extending "
1110 "<schema id='%s'> which is not a list"),
1115 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1117 g_set_error (error, G_MARKUP_ERROR,
1118 G_MARKUP_ERROR_INVALID_CONTENT,
1119 _("<schema id='%s' list-of='%s'> extends <schema "
1120 "id='%s' list-of='%s'> but '%s' does not "
1121 "extend '%s'"), id, list_of, extends_name,
1122 extends->list_of, list_of, extends->list_of);
1127 /* by default we are a list of the same thing that the schema
1128 * we are extending is a list of (which might be nothing)
1130 list_of = extends->list_of;
1133 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1135 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1136 _("a path, if given, must begin and end with a slash"));
1140 if (path && list_of && !g_str_has_suffix (path, ":/"))
1142 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1143 _("the path of a list must end with ':/'"));
1147 state->schema_state = schema_state_new (path, gettext_domain,
1148 extends, extends_name, list_of);
1150 my_id = g_strdup (id);
1151 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1152 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1156 parse_state_start_enum (ParseState *state,
1161 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1162 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1165 if (g_hash_table_lookup (table, id))
1167 g_set_error (error, G_MARKUP_ERROR,
1168 G_MARKUP_ERROR_INVALID_CONTENT,
1169 _("<%s id='%s'> already specified"),
1170 is_flags ? "flags" : "enum", id);
1174 state->enum_state = enum_state_new (is_flags);
1176 my_id = g_strdup (id);
1177 *list = g_slist_prepend (*list, my_id);
1178 g_hash_table_insert (table, my_id, state->enum_state);
1181 /* GMarkup Parser Functions {{{1 */
1183 /* Start element {{{2 */
1185 start_element (GMarkupParseContext *context,
1186 const gchar *element_name,
1187 const gchar **attribute_names,
1188 const gchar **attribute_values,
1192 ParseState *state = user_data;
1193 const GSList *element_stack;
1194 const gchar *container;
1196 element_stack = g_markup_parse_context_get_element_stack (context);
1197 container = element_stack->next ? element_stack->next->data : NULL;
1199 #define COLLECT(first, ...) \
1200 g_markup_collect_attributes (element_name, \
1201 attribute_names, attribute_values, error, \
1202 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1203 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1204 #define STRDUP G_MARKUP_COLLECT_STRDUP
1205 #define STRING G_MARKUP_COLLECT_STRING
1206 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1208 /* Toplevel items {{{3 */
1209 if (container == NULL)
1211 if (strcmp (element_name, "schemalist") == 0)
1213 COLLECT (OPTIONAL | STRDUP,
1215 &state->schemalist_domain);
1221 /* children of <schemalist> {{{3 */
1222 else if (strcmp (container, "schemalist") == 0)
1224 if (strcmp (element_name, "schema") == 0)
1226 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1227 if (COLLECT (STRING, "id", &id,
1228 OPTIONAL | STRING, "path", &path,
1229 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1230 OPTIONAL | STRING, "extends", &extends,
1231 OPTIONAL | STRING, "list-of", &list_of))
1232 parse_state_start_schema (state, id, path, gettext_domain,
1233 extends, list_of, error);
1237 else if (strcmp (element_name, "enum") == 0)
1240 if (COLLECT (STRING, "id", &id))
1241 parse_state_start_enum (state, id, FALSE, error);
1245 else if (strcmp (element_name, "flags") == 0)
1248 if (COLLECT (STRING, "id", &id))
1249 parse_state_start_enum (state, id, TRUE, error);
1255 /* children of <schema> {{{3 */
1256 else if (strcmp (container, "schema") == 0)
1258 if (strcmp (element_name, "key") == 0)
1260 const gchar *name, *type_string, *enum_type, *flags_type;
1262 if (COLLECT (STRING, "name", &name,
1263 OPTIONAL | STRING, "type", &type_string,
1264 OPTIONAL | STRING, "enum", &enum_type,
1265 OPTIONAL | STRING, "flags", &flags_type))
1267 state->key_state = schema_state_add_key (state->schema_state,
1271 enum_type, flags_type,
1275 else if (strcmp (element_name, "child") == 0)
1277 const gchar *name, *schema;
1279 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1280 schema_state_add_child (state->schema_state,
1281 name, schema, error);
1284 else if (strcmp (element_name, "override") == 0)
1286 const gchar *name, *l10n, *context;
1288 if (COLLECT (STRING, "name", &name,
1289 OPTIONAL | STRING, "l10n", &l10n,
1290 OPTIONAL | STRING, "context", &context))
1291 schema_state_add_override (state->schema_state,
1292 &state->key_state, &state->string,
1293 name, l10n, context, error);
1298 /* children of <key> {{{3 */
1299 else if (strcmp (container, "key") == 0)
1301 if (strcmp (element_name, "default") == 0)
1303 const gchar *l10n, *context;
1304 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1305 STRING | OPTIONAL, "context", &context))
1306 state->string = key_state_start_default (state->key_state,
1307 l10n, context, error);
1311 else if (strcmp (element_name, "summary") == 0 ||
1312 strcmp (element_name, "description") == 0)
1315 state->string = g_string_new (NULL);
1319 else if (strcmp (element_name, "range") == 0)
1321 const gchar *min, *max;
1322 if (COLLECT (STRING, "min", &min, STRING, "max", &max))
1323 key_state_set_range (state->key_state, min, max, error);
1327 else if (strcmp (element_name, "choices") == 0)
1330 key_state_start_choices (state->key_state, error);
1334 else if (strcmp (element_name, "aliases") == 0)
1337 key_state_start_aliases (state->key_state, error);
1343 /* children of <choices> {{{3 */
1344 else if (strcmp (container, "choices") == 0)
1346 if (strcmp (element_name, "choice") == 0)
1349 if (COLLECT (STRING, "value", &value))
1350 key_state_add_choice (state->key_state, value, error);
1356 /* children of <aliases> {{{3 */
1357 else if (strcmp (container, "aliases") == 0)
1359 if (strcmp (element_name, "alias") == 0)
1361 const gchar *value, *target;
1362 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1363 key_state_add_alias (state->key_state, value, target, error);
1369 /* children of <enum> {{{3 */
1370 else if (strcmp (container, "enum") == 0 ||
1371 strcmp (container, "flags") == 0)
1373 if (strcmp (element_name, "value") == 0)
1375 const gchar *nick, *valuestr;
1376 if (COLLECT (STRING, "nick", &nick,
1377 STRING, "value", &valuestr))
1378 enum_state_add_value (state->enum_state, nick, valuestr, error);
1385 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1386 _("Element <%s> not allowed inside <%s>"),
1387 element_name, container);
1389 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1390 _("Element <%s> not allowed at toplevel"), element_name);
1393 /* End element {{{2 */
1396 key_state_end (KeyState **state_ptr,
1404 if (state->default_value == NULL)
1406 g_set_error_literal (error,
1407 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1408 "element <default> is required in <key>");
1414 schema_state_end (SchemaState **state_ptr,
1424 end_element (GMarkupParseContext *context,
1425 const gchar *element_name,
1429 ParseState *state = user_data;
1431 if (strcmp (element_name, "schemalist") == 0)
1433 g_free (state->schemalist_domain);
1434 state->schemalist_domain = NULL;
1437 else if (strcmp (element_name, "enum") == 0 ||
1438 strcmp (element_name, "flags") == 0)
1439 enum_state_end (&state->enum_state, error);
1441 else if (strcmp (element_name, "schema") == 0)
1442 schema_state_end (&state->schema_state, error);
1444 else if (strcmp (element_name, "override") == 0)
1445 override_state_end (&state->key_state, &state->string, error);
1447 else if (strcmp (element_name, "key") == 0)
1448 key_state_end (&state->key_state, error);
1450 else if (strcmp (element_name, "default") == 0)
1451 key_state_end_default (state->key_state, &state->string, error);
1453 else if (strcmp (element_name, "choices") == 0)
1454 key_state_end_choices (state->key_state, error);
1456 else if (strcmp (element_name, "aliases") == 0)
1457 key_state_end_aliases (state->key_state, error);
1461 g_string_free (state->string, TRUE);
1462 state->string = NULL;
1467 text (GMarkupParseContext *context,
1473 ParseState *state = user_data;
1476 for (i = 0; i < text_len; i++)
1477 if (!g_ascii_isspace (text[i]))
1480 g_string_append_len (state->string, text, text_len);
1483 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1484 _("text may not appear inside <%s>"),
1485 g_markup_parse_context_get_element (context));
1491 /* Write to GVDB {{{1 */
1499 gvdb_pair_init (GvdbPair *pair)
1501 pair->table = gvdb_hash_table_new (NULL, NULL);
1502 pair->root = gvdb_hash_table_insert (pair->table, "");
1512 output_key (gpointer key,
1516 OutputSchemaData *data;
1525 item = gvdb_hash_table_insert (data->pair.table, name);
1526 gvdb_item_set_parent (item, data->pair.root);
1527 gvdb_item_set_value (item, key_state_serialise (state));
1534 output_schema (gpointer key,
1538 OutputSchemaData data;
1539 GvdbPair *root_pair;
1546 root_pair = user_data;
1548 gvdb_pair_init (&data.pair);
1551 item = gvdb_hash_table_insert (root_pair->table, id);
1552 gvdb_item_set_parent (item, root_pair->root);
1553 gvdb_item_set_hash_table (item, data.pair.table);
1555 g_hash_table_foreach (state->keys, output_key, &data);
1558 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1560 if (state->extends_name)
1561 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1562 state->extends_name);
1565 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1566 state->extends_name);
1569 gvdb_hash_table_insert_string (data.pair.table,
1571 state->gettext_domain);
1575 write_to_file (GHashTable *schema_table,
1576 const gchar *filename,
1582 gvdb_pair_init (&pair);
1584 g_hash_table_foreach (schema_table, output_schema, &pair);
1586 success = gvdb_table_write_contents (pair.table, filename,
1587 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1589 g_hash_table_unref (pair.table);
1594 /* Parser driver {{{1 */
1596 parse_gschema_files (gchar **files,
1599 GMarkupParser parser = { start_element, end_element, text };
1600 ParseState state = { 0, };
1601 const gchar *filename;
1602 GError *error = NULL;
1604 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1605 g_free, enum_state_free);
1607 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1608 g_free, enum_state_free);
1610 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1611 g_free, schema_state_free);
1613 while ((filename = *files++) != NULL)
1615 GMarkupParseContext *context;
1619 if (!g_file_get_contents (filename, &contents, &size, &error))
1621 fprintf (stderr, "%s\n", error->message);
1622 g_clear_error (&error);
1626 context = g_markup_parse_context_new (&parser,
1627 G_MARKUP_PREFIX_ERROR_POSITION,
1631 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1632 !g_markup_parse_context_end_parse (context, &error))
1636 /* back out any changes from this file */
1637 for (item = state.this_file_schemas; item; item = item->next)
1638 g_hash_table_remove (state.schema_table, item->data);
1640 for (item = state.this_file_flagss; item; item = item->next)
1641 g_hash_table_remove (state.flags_table, item->data);
1643 for (item = state.this_file_enums; item; item = item->next)
1644 g_hash_table_remove (state.enum_table, item->data);
1647 fprintf (stderr, "%s: %s. ", filename, error->message);
1651 /* Translators: Do not translate "--strict". */
1652 fprintf (stderr, _("--strict was specified; exiting.\n"));
1653 g_hash_table_unref (state.schema_table);
1654 g_hash_table_unref (state.flags_table);
1655 g_hash_table_unref (state.enum_table);
1660 fprintf (stderr, _("This entire file has been ignored.\n"));
1664 g_markup_parse_context_free (context);
1665 g_slist_free (state.this_file_schemas);
1666 g_slist_free (state.this_file_flagss);
1667 g_slist_free (state.this_file_enums);
1668 state.this_file_schemas = NULL;
1669 state.this_file_flagss = NULL;
1670 state.this_file_enums = NULL;
1673 g_hash_table_unref (state.flags_table);
1674 g_hash_table_unref (state.enum_table);
1676 return state.schema_table;
1680 compare_strings (gconstpointer a,
1683 gchar *one = *(gchar **) a;
1684 gchar *two = *(gchar **) b;
1687 cmp = g_str_has_suffix (two, ".enums.xml") -
1688 g_str_has_suffix (one, ".enums.xml");
1691 cmp = strcmp (one, two);
1697 set_overrides (GHashTable *schema_table,
1701 const gchar *filename;
1702 GError *error = NULL;
1704 while ((filename = *files++))
1710 key_file = g_key_file_new ();
1711 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1713 fprintf (stderr, "%s: %s. ", filename, error->message);
1714 g_key_file_free (key_file);
1715 g_clear_error (&error);
1719 fprintf (stderr, _("Ignoring this file.\n"));
1723 fprintf (stderr, _("--strict was specified; exiting.\n"));
1727 groups = g_key_file_get_groups (key_file, NULL);
1729 for (i = 0; groups[i]; i++)
1731 const gchar *group = groups[i];
1732 SchemaState *schema;
1736 schema = g_hash_table_lookup (schema_table, group);
1739 /* Having the schema not be installed is expected to be a
1740 * common case. Don't even emit an error message about
1745 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1746 g_assert (keys != NULL);
1748 for (j = 0; keys[j]; j++)
1750 const gchar *key = keys[j];
1755 state = g_hash_table_lookup (schema->keys, key);
1759 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1760 "specified in override file `%s'"),
1761 key, group, filename);
1765 fprintf (stderr, _("; ignoring override for this key.\n"));
1769 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1770 g_key_file_free (key_file);
1771 g_strfreev (groups);
1777 string = g_key_file_get_value (key_file, group, key, NULL);
1778 g_assert (string != NULL);
1780 value = g_variant_parse (state->type, string,
1781 NULL, NULL, &error);
1785 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1786 "as specified in override file `%s': "
1788 key, group, filename, error->message);
1790 g_clear_error (&error);
1795 fprintf (stderr, _("Ignoring override for this key.\n"));
1799 fprintf (stderr, _("--strict was specified; exiting.\n"));
1800 g_key_file_free (key_file);
1801 g_strfreev (groups);
1809 if (g_variant_compare (value, state->minimum) < 0 ||
1810 g_variant_compare (value, state->maximum) > 0)
1813 _("override for key `%s' in schema `%s' in "
1814 "override file `%s' is out of the range "
1815 "given in the schema"),
1816 key, group, filename);
1818 g_variant_unref (value);
1823 fprintf (stderr, _("; ignoring override for this key.\n"));
1827 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1828 g_key_file_free (key_file);
1829 g_strfreev (groups);
1836 else if (state->strinfo->len)
1838 if (!is_valid_choices (value, state->strinfo))
1841 _("override for key `%s' in schema `%s' in "
1842 "override file `%s' is not in the list "
1843 "of valid choices"),
1844 key, group, filename);
1846 g_variant_unref (value);
1851 fprintf (stderr, _("; ignoring override for this key.\n"));
1855 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1856 g_key_file_free (key_file);
1857 g_strfreev (groups);
1864 g_variant_unref (state->default_value);
1865 state->default_value = value;
1872 g_strfreev (groups);
1879 main (int argc, char **argv)
1886 gchar *targetdir = NULL;
1888 gboolean uninstall = FALSE;
1889 gboolean dry_run = FALSE;
1890 gboolean strict = FALSE;
1891 gchar **schema_files = NULL;
1892 gchar **override_files = NULL;
1893 GOptionContext *context;
1894 GOptionEntry entries[] = {
1895 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1896 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1897 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1898 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1899 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1901 /* These options are only for use in the gschema-compile tests */
1902 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1906 setlocale (LC_ALL, "");
1908 context = g_option_context_new (N_("DIRECTORY"));
1909 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1910 g_option_context_set_summary (context,
1911 N_("Compile all GSettings schema files into a schema cache.\n"
1912 "Schema files are required to have the extension .gschema.xml,\n"
1913 "and the cache file is called gschemas.compiled."));
1914 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1917 if (!g_option_context_parse (context, &argc, &argv, &error))
1919 fprintf (stderr, "%s\n", error->message);
1923 g_option_context_free (context);
1925 if (!schema_files && argc != 2)
1927 fprintf (stderr, _("You should give exactly one directory name\n"));
1933 if (targetdir == NULL)
1936 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1940 GPtrArray *overrides;
1943 files = g_ptr_array_new ();
1944 overrides = g_ptr_array_new ();
1946 dir = g_dir_open (srcdir, 0, &error);
1949 fprintf (stderr, "%s\n", error->message);
1953 while ((file = g_dir_read_name (dir)) != NULL)
1955 if (g_str_has_suffix (file, ".gschema.xml") ||
1956 g_str_has_suffix (file, ".enums.xml"))
1957 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
1959 else if (g_str_has_suffix (file, ".gschema.override"))
1960 g_ptr_array_add (overrides,
1961 g_build_filename (srcdir, file, NULL));
1964 if (files->len == 0)
1966 fprintf (stderr, _("No schema files found: "));
1968 if (g_unlink (target))
1969 fprintf (stderr, _("doing nothing.\n"));
1972 fprintf (stderr, _("removed existing output file.\n"));
1976 g_ptr_array_sort (files, compare_strings);
1977 g_ptr_array_add (files, NULL);
1979 g_ptr_array_sort (overrides, compare_strings);
1980 g_ptr_array_add (overrides, NULL);
1982 schema_files = (char **) g_ptr_array_free (files, FALSE);
1983 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
1986 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
1992 if (override_files != NULL &&
1993 !set_overrides (table, override_files, strict))
1999 if (!dry_run && !write_to_file (table, target, &error))
2001 fprintf (stderr, "%s\n", error->message);
2013 /* vim:set foldmethod=marker: */