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 if (state->is_flags && (value & (value - 1)))
112 g_set_error (error, G_MARKUP_ERROR,
113 G_MARKUP_ERROR_INVALID_CONTENT,
114 "flags values must have at most 1 bit set");
118 /* Since we reject exact duplicates of value='' and we only allow one
119 * bit to be set, it's not possible to have overlaps.
121 * If we loosen the one-bit-set restriction we need an overlap check.
125 strinfo_builder_append_item (state->strinfo, nick, value);
129 enum_state_end (EnumState **state_ptr,
137 if (state->strinfo->len == 0)
139 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
140 "<%s> must contain at least one <value>",
141 state->is_flags ? "flags" : "enum");
144 /* Handling of <key> {{{1 */
147 /* for <child>, @child_schema will be set.
148 * for <key>, everything else will be set.
154 gboolean have_gettext_domain;
158 GString *unparsed_default_value;
159 GVariant *default_value;
168 gboolean has_choices;
169 gboolean has_aliases;
170 gboolean is_override;
173 GVariant *serialised;
177 key_state_new (const gchar *type_string,
178 const gchar *gettext_domain,
185 state = g_slice_new0 (KeyState);
186 state->type = g_variant_type_new (type_string);
187 state->have_gettext_domain = gettext_domain != NULL;
188 state->is_enum = is_enum;
189 state->is_flags = is_flags;
192 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
194 state->strinfo = g_string_new (NULL);
200 key_state_override (KeyState *state,
201 const gchar *gettext_domain)
205 copy = g_slice_new0 (KeyState);
206 copy->type = g_variant_type_copy (state->type);
207 copy->have_gettext_domain = gettext_domain != NULL;
208 copy->strinfo = g_string_new_len (state->strinfo->str,
209 state->strinfo->len);
210 copy->is_enum = state->is_enum;
211 copy->is_flags = state->is_flags;
212 copy->is_override = TRUE;
216 copy->minimum = g_variant_ref (state->minimum);
217 copy->maximum = g_variant_ref (state->maximum);
224 key_state_new_child (const gchar *child_schema)
228 state = g_slice_new0 (KeyState);
229 state->child_schema = g_strdup (child_schema);
235 is_valid_choices (GVariant *variant,
238 switch (g_variant_classify (variant))
240 case G_VARIANT_CLASS_MAYBE:
241 case G_VARIANT_CLASS_ARRAY:
243 gboolean valid = TRUE;
246 g_variant_iter_init (&iter, variant);
248 while (valid && (variant = g_variant_iter_next_value (&iter)))
250 valid = is_valid_choices (variant, strinfo);
251 g_variant_unref (variant);
257 case G_VARIANT_CLASS_STRING:
258 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
260 g_variant_get_string (variant, NULL));
263 g_assert_not_reached ();
268 /* Gets called at </default> </choices> or <range/> to check for
269 * validity of the default value so that any inconsistency is
270 * reported as soon as it is encountered.
273 key_state_check_range (KeyState *state,
276 if (state->default_value)
280 tag = state->is_override ? "override" : "default";
284 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
285 g_variant_compare (state->default_value, state->maximum) > 0)
287 g_set_error (error, G_MARKUP_ERROR,
288 G_MARKUP_ERROR_INVALID_CONTENT,
289 "<%s> is not contained in "
290 "the specified range", tag);
294 else if (state->strinfo->len)
296 if (!is_valid_choices (state->default_value, state->strinfo))
299 g_set_error (error, G_MARKUP_ERROR,
300 G_MARKUP_ERROR_INVALID_CONTENT,
301 "<%s> is not a valid member of "
302 "the specified enumerated type", tag);
304 else if (state->is_flags)
305 g_set_error (error, G_MARKUP_ERROR,
306 G_MARKUP_ERROR_INVALID_CONTENT,
307 "<%s> contains string not in the "
308 "specified flags type", tag);
311 g_set_error (error, G_MARKUP_ERROR,
312 G_MARKUP_ERROR_INVALID_CONTENT,
313 "<%s> contains string not in "
321 key_state_set_range (KeyState *state,
322 const gchar *min_str,
323 const gchar *max_str,
328 g_set_error_literal (error, G_MARKUP_ERROR,
329 G_MARKUP_ERROR_INVALID_CONTENT,
330 "<range/> already specified for this key");
334 if (strchr ("ynqiuxtd", *(char *) state->type) == NULL)
336 gchar *type = g_variant_type_dup_string (state->type);
337 g_set_error (error, G_MARKUP_ERROR,
338 G_MARKUP_ERROR_INVALID_CONTENT,
339 "<range> not allowed for keys of type '%s'", type);
344 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
345 if (state->minimum == NULL)
348 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
349 if (state->maximum == NULL)
352 if (g_variant_compare (state->minimum, state->maximum) > 0)
354 g_set_error (error, G_MARKUP_ERROR,
355 G_MARKUP_ERROR_INVALID_CONTENT,
356 "<range> specified minimum is greater than maxmimum");
360 key_state_check_range (state, error);
364 key_state_start_default (KeyState *state,
366 const gchar *context,
371 if (strcmp (l10n, "messages") == 0)
374 else if (strcmp (l10n, "time") == 0)
379 g_set_error (error, G_MARKUP_ERROR,
380 G_MARKUP_ERROR_INVALID_CONTENT,
381 "unsupported l10n category: %s", l10n);
385 if (!state->have_gettext_domain)
387 g_set_error_literal (error, G_MARKUP_ERROR,
388 G_MARKUP_ERROR_INVALID_CONTENT,
389 "l10n requested, but no "
390 "gettext domain given");
394 state->l10n_context = g_strdup (context);
397 else if (context != NULL)
399 g_set_error_literal (error, G_MARKUP_ERROR,
400 G_MARKUP_ERROR_INVALID_CONTENT,
401 "translation context given for "
402 " value without l10n enabled");
406 return g_string_new (NULL);
410 key_state_end_default (KeyState *state,
414 state->unparsed_default_value = *string;
417 state->default_value = g_variant_parse (state->type,
418 state->unparsed_default_value->str,
420 key_state_check_range (state, error);
424 key_state_start_choices (KeyState *state,
427 const GVariantType *type = state->type;
431 g_set_error_literal (error, G_MARKUP_ERROR,
432 G_MARKUP_ERROR_INVALID_CONTENT,
433 "<choices> can not be specified for keys "
434 "tagged as having an enumerated type");
438 if (state->has_choices)
440 g_set_error_literal (error, G_MARKUP_ERROR,
441 G_MARKUP_ERROR_INVALID_CONTENT,
442 "<choices> already specified for this key");
446 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
447 type = g_variant_type_element (type);
449 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
451 gchar *type_string = g_variant_type_dup_string (state->type);
452 g_set_error (error, G_MARKUP_ERROR,
453 G_MARKUP_ERROR_INVALID_CONTENT,
454 "<choices> not allowed for keys of type '%s'",
456 g_free (type_string);
462 key_state_add_choice (KeyState *state,
466 if (strinfo_builder_contains (state->strinfo, choice))
468 g_set_error (error, G_MARKUP_ERROR,
469 G_MARKUP_ERROR_INVALID_CONTENT,
470 "<choice value='%s'/> already given", choice);
474 strinfo_builder_append_item (state->strinfo, choice, 0);
475 state->has_choices = TRUE;
479 key_state_end_choices (KeyState *state,
482 if (!state->has_choices)
484 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
485 "<choices> must contain at least one <choice>");
489 key_state_check_range (state, error);
493 key_state_start_aliases (KeyState *state,
496 if (state->has_aliases)
497 g_set_error_literal (error, G_MARKUP_ERROR,
498 G_MARKUP_ERROR_INVALID_CONTENT,
499 "<aliases> already specified for this key");
501 if (!state->is_flags && !state->is_enum && !state->has_choices)
502 g_set_error_literal (error, G_MARKUP_ERROR,
503 G_MARKUP_ERROR_INVALID_CONTENT,
504 "<aliases> can only be specified for keys with "
505 "enumerated or flags types or after <choices>");
509 key_state_add_alias (KeyState *state,
514 if (strinfo_builder_contains (state->strinfo, alias))
516 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
517 state->strinfo->len / 4,
521 g_set_error (error, G_MARKUP_ERROR,
522 G_MARKUP_ERROR_INVALID_CONTENT,
523 "<alias value='%s'/> given when '%s' is already "
524 "a member of the enumerated type", alias, alias);
527 g_set_error (error, G_MARKUP_ERROR,
528 G_MARKUP_ERROR_INVALID_CONTENT,
529 "<alias value='%s'/> given when "
530 "<choice value='%s'/> was already given",
535 g_set_error (error, G_MARKUP_ERROR,
536 G_MARKUP_ERROR_INVALID_CONTENT,
537 "<alias value='%s'/> already specified", alias);
542 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
544 g_set_error (error, G_MARKUP_ERROR,
545 G_MARKUP_ERROR_INVALID_CONTENT,
546 "alias target '%s' is not in %s", target,
547 state->is_enum ? "enumerated type" : "<choices>");
551 state->has_aliases = TRUE;
555 key_state_end_aliases (KeyState *state,
558 if (!state->has_aliases)
560 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
561 "<aliases> must contain at least one <alias>");
567 key_state_check (KeyState *state,
573 return state->checked = TRUE;
577 key_state_serialise (KeyState *state)
579 if (state->serialised == NULL)
581 if (state->child_schema)
583 state->serialised = g_variant_new_string (state->child_schema);
588 GVariantBuilder builder;
590 g_assert (key_state_check (state, NULL));
592 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
595 g_variant_builder_add_value (&builder, state->default_value);
600 if (state->l10n_context)
604 /* Contextified messages are supported by prepending
605 * the context, followed by '\004' to the start of the
606 * message string. We do that here to save GSettings
609 len = strlen (state->l10n_context);
610 state->l10n_context[len] = '\004';
611 g_string_prepend_len (state->unparsed_default_value,
612 state->l10n_context, len + 1);
613 g_free (state->l10n_context);
614 state->l10n_context = NULL;
617 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
618 state->unparsed_default_value->str);
619 g_string_free (state->unparsed_default_value, TRUE);
620 state->unparsed_default_value = NULL;
623 /* choice, aliases, enums */
624 if (state->strinfo->len)
630 data = state->strinfo->str;
631 size = state->strinfo->len;
633 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
637 g_string_free (state->strinfo, FALSE);
638 state->strinfo = NULL;
640 g_variant_builder_add (&builder, "(y@au)",
641 state->is_flags ? 'f' :
642 state->is_enum ? 'e' : 'c',
647 if (state->minimum || state->maximum)
648 g_variant_builder_add (&builder, "(y(**))", 'r',
649 state->minimum, state->maximum);
651 state->serialised = g_variant_builder_end (&builder);
654 g_variant_ref_sink (state->serialised);
657 return g_variant_ref (state->serialised);
661 key_state_free (gpointer data)
663 KeyState *state = data;
666 g_variant_type_free (state->type);
668 g_free (state->l10n_context);
670 if (state->unparsed_default_value)
671 g_string_free (state->unparsed_default_value, TRUE);
673 if (state->default_value)
674 g_variant_unref (state->default_value);
677 g_string_free (state->strinfo, TRUE);
680 g_variant_unref (state->minimum);
683 g_variant_unref (state->maximum);
685 if (state->serialised)
686 g_variant_unref (state->serialised);
688 g_slice_free (KeyState, state);
691 /* Key name validity {{{1 */
692 static gboolean allow_any_name = FALSE;
695 is_valid_keyname (const gchar *key,
702 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
703 _("empty names are not permitted"));
710 if (!g_ascii_islower (key[0]))
712 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
713 _("invalid name '%s': names must begin "
714 "with a lowercase letter"), key);
718 for (i = 1; key[i]; i++)
721 !g_ascii_islower (key[i]) &&
722 !g_ascii_isdigit (key[i]))
724 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
725 _("invalid name '%s': invalid character '%c'; "
726 "only lowercase letters, numbers and dash ('-') "
727 "are permitted."), key, key[i]);
731 if (key[i] == '-' && key[i + 1] == '-')
733 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
734 _("invalid name '%s': two successive dashes ('--') "
735 "are not permitted."), key);
740 if (key[i - 1] == '-')
742 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
743 _("invalid name '%s': the last character may not be a "
744 "dash ('-')."), key);
750 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
751 _("invalid name '%s': maximum length is 32"), key);
758 /* Handling of <schema> {{{1 */
759 typedef struct _SchemaState SchemaState;
762 SchemaState *extends;
765 gchar *gettext_domain;
773 schema_state_new (const gchar *path,
774 const gchar *gettext_domain,
775 SchemaState *extends,
776 const gchar *extends_name,
777 const gchar *list_of)
781 state = g_slice_new (SchemaState);
782 state->path = g_strdup (path);
783 state->gettext_domain = g_strdup (gettext_domain);
784 state->extends = extends;
785 state->extends_name = g_strdup (extends_name);
786 state->list_of = g_strdup (list_of);
787 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
788 g_free, key_state_free);
794 schema_state_free (gpointer data)
796 SchemaState *state = data;
798 g_free (state->path);
799 g_free (state->gettext_domain);
800 g_hash_table_unref (state->keys);
804 schema_state_add_child (SchemaState *state,
811 if (!is_valid_keyname (name, error))
814 childname = g_strconcat (name, "/", NULL);
816 if (g_hash_table_lookup (state->keys, childname))
818 g_set_error (error, G_MARKUP_ERROR,
819 G_MARKUP_ERROR_INVALID_CONTENT,
820 _("<child name='%s'> already specified"), name);
824 g_hash_table_insert (state->keys, childname,
825 key_state_new_child (schema));
829 schema_state_add_key (SchemaState *state,
830 GHashTable *enum_table,
831 GHashTable *flags_table,
833 const gchar *type_string,
834 const gchar *enum_type,
835 const gchar *flags_type,
844 g_set_error_literal (error, G_MARKUP_ERROR,
845 G_MARKUP_ERROR_INVALID_CONTENT,
846 _("can not add keys to a 'list-of' schema"));
850 if (!is_valid_keyname (name, error))
853 if (g_hash_table_lookup (state->keys, name))
855 g_set_error (error, G_MARKUP_ERROR,
856 G_MARKUP_ERROR_INVALID_CONTENT,
857 _("<key name='%s'> already specified"), name);
861 for (node = state; node; node = node->extends)
866 shadow = g_hash_table_lookup (node->extends->keys, name);
868 /* in case of <key> <override> <key> make sure we report the
869 * location of the original <key>, not the <override>.
871 if (shadow && !shadow->is_override)
873 g_set_error (error, G_MARKUP_ERROR,
874 G_MARKUP_ERROR_INVALID_CONTENT,
875 _("<key name='%s'> shadows <key name='%s'> in "
876 "<schema id='%s'>; use <override> to modify value"),
877 name, name, node->extends_name);
882 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
884 g_set_error (error, G_MARKUP_ERROR,
885 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
886 _("exactly one of 'type', 'enum' or 'flags' must "
887 "be specified as an attribute to <key>"));
891 if (type_string == NULL) /* flags or enums was specified */
893 EnumState *enum_state;
896 enum_state = g_hash_table_lookup (enum_table, enum_type);
898 enum_state = g_hash_table_lookup (flags_table, flags_type);
901 if (enum_state == NULL)
903 g_set_error (error, G_MARKUP_ERROR,
904 G_MARKUP_ERROR_INVALID_CONTENT,
905 _("<%s id='%s'> not (yet) defined."),
906 flags_type ? "flags" : "enum",
907 flags_type ? flags_type : enum_type);
911 type_string = flags_type ? "as" : "s";
912 strinfo = enum_state->strinfo;
916 if (!g_variant_type_string_is_valid (type_string))
918 g_set_error (error, G_MARKUP_ERROR,
919 G_MARKUP_ERROR_INVALID_CONTENT,
920 _("invalid GVariant type string '%s'"), type_string);
927 key = key_state_new (type_string, state->gettext_domain,
928 enum_type != NULL, flags_type != NULL, strinfo);
929 g_hash_table_insert (state->keys, g_strdup (name), key);
935 schema_state_add_override (SchemaState *state,
936 KeyState **key_state,
940 const gchar *context,
946 if (state->extends == NULL)
948 g_set_error_literal (error, G_MARKUP_ERROR,
949 G_MARKUP_ERROR_INVALID_CONTENT,
950 _("<override> given but schema isn't "
951 "extending anything"));
955 for (parent = state->extends; parent; parent = parent->extends)
956 if ((original = g_hash_table_lookup (parent->keys, key)))
959 if (original == NULL)
961 g_set_error (error, G_MARKUP_ERROR,
962 G_MARKUP_ERROR_INVALID_CONTENT,
963 _("no <key name='%s'> to override"), key);
967 if (g_hash_table_lookup (state->keys, key))
969 g_set_error (error, G_MARKUP_ERROR,
970 G_MARKUP_ERROR_INVALID_CONTENT,
971 _("<override name='%s'> already specified"), key);
975 *key_state = key_state_override (original, state->gettext_domain);
976 *string = key_state_start_default (*key_state, l10n, context, error);
977 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
981 override_state_end (KeyState **key_state,
985 key_state_end_default (*key_state, string, error);
989 /* Handling of toplevel state {{{1 */
992 GHashTable *schema_table; /* string -> SchemaState */
993 GHashTable *flags_table; /* string -> EnumState */
994 GHashTable *enum_table; /* string -> EnumState */
996 GSList *this_file_schemas; /* strings: <schema>s in this file */
997 GSList *this_file_flagss; /* strings: <flags>s in this file */
998 GSList *this_file_enums; /* strings: <enum>s in this file */
1000 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1002 SchemaState *schema_state; /* non-NULL when inside <schema> */
1003 KeyState *key_state; /* non-NULL when inside <key> */
1004 EnumState *enum_state; /* non-NULL when inside <enum> */
1006 GString *string; /* non-NULL when accepting text */
1010 is_subclass (const gchar *class_name,
1011 const gchar *possible_parent,
1012 GHashTable *schema_table)
1016 if (strcmp (class_name, possible_parent) == 0)
1019 class = g_hash_table_lookup (schema_table, class_name);
1020 g_assert (class != NULL);
1022 return class->extends_name &&
1023 is_subclass (class->extends_name, possible_parent, schema_table);
1027 parse_state_start_schema (ParseState *state,
1030 const gchar *gettext_domain,
1031 const gchar *extends_name,
1032 const gchar *list_of,
1035 SchemaState *extends;
1038 if (g_hash_table_lookup (state->schema_table, id))
1040 g_set_error (error, G_MARKUP_ERROR,
1041 G_MARKUP_ERROR_INVALID_CONTENT,
1042 _("<schema id='%s'> already specified"), id);
1048 extends = g_hash_table_lookup (state->schema_table, extends_name);
1050 if (extends == NULL)
1052 g_set_error (error, G_MARKUP_ERROR,
1053 G_MARKUP_ERROR_INVALID_CONTENT,
1054 _("<schema id='%s'> extends not yet "
1055 "existing schema '%s'"), id, extends_name);
1066 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1068 g_set_error (error, G_MARKUP_ERROR,
1069 G_MARKUP_ERROR_INVALID_CONTENT,
1070 _("<schema id='%s'> is list of not yet "
1071 "existing schema '%s'"), id, list_of);
1077 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1078 _("Can not be a list of a schema with a path"));
1087 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1088 _("Can not extend a schema with a path"));
1094 if (extends->list_of == NULL)
1096 g_set_error (error, G_MARKUP_ERROR,
1097 G_MARKUP_ERROR_INVALID_CONTENT,
1098 _("<schema id='%s'> is a list, extending "
1099 "<schema id='%s'> which is not a list"),
1104 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1106 g_set_error (error, G_MARKUP_ERROR,
1107 G_MARKUP_ERROR_INVALID_CONTENT,
1108 _("<schema id='%s' list-of='%s'> extends <schema "
1109 "id='%s' list-of='%s'> but '%s' does not "
1110 "extend '%s'"), id, list_of, extends_name,
1111 extends->list_of, list_of, extends->list_of);
1116 /* by default we are a list of the same thing that the schema
1117 * we are extending is a list of (which might be nothing)
1119 list_of = extends->list_of;
1122 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1124 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1125 _("a path, if given, must begin and end with a slash"));
1129 if (path && list_of && !g_str_has_suffix (path, ":/"))
1131 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1132 _("the path of a list must end with ':/'"));
1136 state->schema_state = schema_state_new (path, gettext_domain,
1137 extends, extends_name, list_of);
1139 my_id = g_strdup (id);
1140 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1141 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1145 parse_state_start_enum (ParseState *state,
1150 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1151 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1154 if (g_hash_table_lookup (table, id))
1156 g_set_error (error, G_MARKUP_ERROR,
1157 G_MARKUP_ERROR_INVALID_CONTENT,
1158 _("<%s id='%s'> already specified"),
1159 is_flags ? "flags" : "enum", id);
1163 state->enum_state = enum_state_new (is_flags);
1165 my_id = g_strdup (id);
1166 *list = g_slist_prepend (*list, my_id);
1167 g_hash_table_insert (table, my_id, state->enum_state);
1170 /* GMarkup Parser Functions {{{1 */
1172 /* Start element {{{2 */
1174 start_element (GMarkupParseContext *context,
1175 const gchar *element_name,
1176 const gchar **attribute_names,
1177 const gchar **attribute_values,
1181 ParseState *state = user_data;
1182 const GSList *element_stack;
1183 const gchar *container;
1185 element_stack = g_markup_parse_context_get_element_stack (context);
1186 container = element_stack->next ? element_stack->next->data : NULL;
1188 #define COLLECT(first, ...) \
1189 g_markup_collect_attributes (element_name, \
1190 attribute_names, attribute_values, error, \
1191 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1192 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1193 #define STRDUP G_MARKUP_COLLECT_STRDUP
1194 #define STRING G_MARKUP_COLLECT_STRING
1195 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1197 /* Toplevel items {{{3 */
1198 if (container == NULL)
1200 if (strcmp (element_name, "schemalist") == 0)
1202 COLLECT (OPTIONAL | STRDUP,
1204 &state->schemalist_domain);
1210 /* children of <schemalist> {{{3 */
1211 else if (strcmp (container, "schemalist") == 0)
1213 if (strcmp (element_name, "schema") == 0)
1215 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1216 if (COLLECT (STRING, "id", &id,
1217 OPTIONAL | STRING, "path", &path,
1218 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1219 OPTIONAL | STRING, "extends", &extends,
1220 OPTIONAL | STRING, "list-of", &list_of))
1221 parse_state_start_schema (state, id, path, gettext_domain,
1222 extends, list_of, error);
1226 else if (strcmp (element_name, "enum") == 0)
1229 if (COLLECT (STRING, "id", &id))
1230 parse_state_start_enum (state, id, FALSE, error);
1234 else if (strcmp (element_name, "flags") == 0)
1237 if (COLLECT (STRING, "id", &id))
1238 parse_state_start_enum (state, id, TRUE, error);
1244 /* children of <schema> {{{3 */
1245 else if (strcmp (container, "schema") == 0)
1247 if (strcmp (element_name, "key") == 0)
1249 const gchar *name, *type_string, *enum_type, *flags_type;
1251 if (COLLECT (STRING, "name", &name,
1252 OPTIONAL | STRING, "type", &type_string,
1253 OPTIONAL | STRING, "enum", &enum_type,
1254 OPTIONAL | STRING, "flags", &flags_type))
1256 state->key_state = schema_state_add_key (state->schema_state,
1260 enum_type, flags_type,
1264 else if (strcmp (element_name, "child") == 0)
1266 const gchar *name, *schema;
1268 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1269 schema_state_add_child (state->schema_state,
1270 name, schema, error);
1273 else if (strcmp (element_name, "override") == 0)
1275 const gchar *name, *l10n, *context;
1277 if (COLLECT (STRING, "name", &name,
1278 OPTIONAL | STRING, "l10n", &l10n,
1279 OPTIONAL | STRING, "context", &context))
1280 schema_state_add_override (state->schema_state,
1281 &state->key_state, &state->string,
1282 name, l10n, context, error);
1287 /* children of <key> {{{3 */
1288 else if (strcmp (container, "key") == 0)
1290 if (strcmp (element_name, "default") == 0)
1292 const gchar *l10n, *context;
1293 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1294 STRING | OPTIONAL, "context", &context))
1295 state->string = key_state_start_default (state->key_state,
1296 l10n, context, error);
1300 else if (strcmp (element_name, "summary") == 0 ||
1301 strcmp (element_name, "description") == 0)
1304 state->string = g_string_new (NULL);
1308 else if (strcmp (element_name, "range") == 0)
1310 const gchar *min, *max;
1311 if (COLLECT (STRING, "min", &min, STRING, "max", &max))
1312 key_state_set_range (state->key_state, min, max, error);
1316 else if (strcmp (element_name, "choices") == 0)
1319 key_state_start_choices (state->key_state, error);
1323 else if (strcmp (element_name, "aliases") == 0)
1326 key_state_start_aliases (state->key_state, error);
1332 /* children of <choices> {{{3 */
1333 else if (strcmp (container, "choices") == 0)
1335 if (strcmp (element_name, "choice") == 0)
1338 if (COLLECT (STRING, "value", &value))
1339 key_state_add_choice (state->key_state, value, error);
1345 /* children of <aliases> {{{3 */
1346 else if (strcmp (container, "aliases") == 0)
1348 if (strcmp (element_name, "alias") == 0)
1350 const gchar *value, *target;
1351 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1352 key_state_add_alias (state->key_state, value, target, error);
1358 /* children of <enum> {{{3 */
1359 else if (strcmp (container, "enum") == 0 ||
1360 strcmp (container, "flags") == 0)
1362 if (strcmp (element_name, "value") == 0)
1364 const gchar *nick, *valuestr;
1365 if (COLLECT (STRING, "nick", &nick,
1366 STRING, "value", &valuestr))
1367 enum_state_add_value (state->enum_state, nick, valuestr, error);
1374 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1375 _("Element <%s> not allowed inside <%s>"),
1376 element_name, container);
1378 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1379 _("Element <%s> not allowed at toplevel"), element_name);
1382 /* End element {{{2 */
1385 key_state_end (KeyState **state_ptr,
1393 if (state->default_value == NULL)
1395 g_set_error_literal (error,
1396 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1397 "element <default> is required in <key>");
1403 schema_state_end (SchemaState **state_ptr,
1413 end_element (GMarkupParseContext *context,
1414 const gchar *element_name,
1418 ParseState *state = user_data;
1420 if (strcmp (element_name, "schemalist") == 0)
1422 g_free (state->schemalist_domain);
1423 state->schemalist_domain = NULL;
1426 else if (strcmp (element_name, "enum") == 0 ||
1427 strcmp (element_name, "flags") == 0)
1428 enum_state_end (&state->enum_state, error);
1430 else if (strcmp (element_name, "schema") == 0)
1431 schema_state_end (&state->schema_state, error);
1433 else if (strcmp (element_name, "override") == 0)
1434 override_state_end (&state->key_state, &state->string, error);
1436 else if (strcmp (element_name, "key") == 0)
1437 key_state_end (&state->key_state, error);
1439 else if (strcmp (element_name, "default") == 0)
1440 key_state_end_default (state->key_state, &state->string, error);
1442 else if (strcmp (element_name, "choices") == 0)
1443 key_state_end_choices (state->key_state, error);
1445 else if (strcmp (element_name, "aliases") == 0)
1446 key_state_end_aliases (state->key_state, error);
1450 g_string_free (state->string, TRUE);
1451 state->string = NULL;
1456 text (GMarkupParseContext *context,
1462 ParseState *state = user_data;
1465 for (i = 0; i < text_len; i++)
1466 if (!g_ascii_isspace (text[i]))
1469 g_string_append_len (state->string, text, text_len);
1472 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1473 _("text may not appear inside <%s>"),
1474 g_markup_parse_context_get_element (context));
1480 /* Write to GVDB {{{1 */
1488 gvdb_pair_init (GvdbPair *pair)
1490 pair->table = gvdb_hash_table_new (NULL, NULL);
1491 pair->root = gvdb_hash_table_insert (pair->table, "");
1501 output_key (gpointer key,
1505 OutputSchemaData *data;
1514 item = gvdb_hash_table_insert (data->pair.table, name);
1515 gvdb_item_set_parent (item, data->pair.root);
1516 gvdb_item_set_value (item, key_state_serialise (state));
1523 output_schema (gpointer key,
1527 OutputSchemaData data;
1528 GvdbPair *root_pair;
1535 root_pair = user_data;
1537 gvdb_pair_init (&data.pair);
1540 item = gvdb_hash_table_insert (root_pair->table, id);
1541 gvdb_item_set_parent (item, root_pair->root);
1542 gvdb_item_set_hash_table (item, data.pair.table);
1544 g_hash_table_foreach (state->keys, output_key, &data);
1547 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1549 if (state->extends_name)
1550 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1551 state->extends_name);
1554 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1555 state->extends_name);
1558 gvdb_hash_table_insert_string (data.pair.table,
1560 state->gettext_domain);
1564 write_to_file (GHashTable *schema_table,
1565 const gchar *filename,
1571 gvdb_pair_init (&pair);
1573 g_hash_table_foreach (schema_table, output_schema, &pair);
1575 success = gvdb_table_write_contents (pair.table, filename,
1576 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1578 g_hash_table_unref (pair.table);
1583 /* Parser driver {{{1 */
1585 parse_gschema_files (gchar **files,
1588 GMarkupParser parser = { start_element, end_element, text };
1589 ParseState state = { 0, };
1590 const gchar *filename;
1591 GError *error = NULL;
1593 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1594 g_free, enum_state_free);
1596 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1597 g_free, enum_state_free);
1599 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1600 g_free, schema_state_free);
1602 while ((filename = *files++) != NULL)
1604 GMarkupParseContext *context;
1608 if (!g_file_get_contents (filename, &contents, &size, &error))
1610 fprintf (stderr, "%s\n", error->message);
1611 g_clear_error (&error);
1615 context = g_markup_parse_context_new (&parser,
1616 G_MARKUP_PREFIX_ERROR_POSITION,
1620 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1621 !g_markup_parse_context_end_parse (context, &error))
1625 /* back out any changes from this file */
1626 for (item = state.this_file_schemas; item; item = item->next)
1627 g_hash_table_remove (state.schema_table, item->data);
1629 for (item = state.this_file_flagss; item; item = item->next)
1630 g_hash_table_remove (state.flags_table, item->data);
1632 for (item = state.this_file_enums; item; item = item->next)
1633 g_hash_table_remove (state.enum_table, item->data);
1636 fprintf (stderr, "%s: %s. ", filename, error->message);
1640 /* Translators: Do not translate "--strict". */
1641 fprintf (stderr, _("--strict was specified; exiting.\n"));
1642 g_hash_table_unref (state.schema_table);
1643 g_hash_table_unref (state.flags_table);
1644 g_hash_table_unref (state.enum_table);
1649 fprintf (stderr, _("This entire file has been ignored.\n"));
1653 g_markup_parse_context_free (context);
1654 g_slist_free (state.this_file_schemas);
1655 g_slist_free (state.this_file_flagss);
1656 g_slist_free (state.this_file_enums);
1657 state.this_file_schemas = NULL;
1658 state.this_file_flagss = NULL;
1659 state.this_file_enums = NULL;
1662 g_hash_table_unref (state.flags_table);
1663 g_hash_table_unref (state.enum_table);
1665 return state.schema_table;
1669 compare_strings (gconstpointer a,
1672 gchar *one = *(gchar **) a;
1673 gchar *two = *(gchar **) b;
1676 cmp = g_str_has_suffix (two, ".enums.xml") -
1677 g_str_has_suffix (one, ".enums.xml");
1680 cmp = strcmp (one, two);
1686 set_overrides (GHashTable *schema_table,
1690 const gchar *filename;
1692 while ((filename = *files++))
1698 key_file = g_key_file_new ();
1699 if (!g_key_file_load_from_file (key_file, filename, 0, error))
1701 g_key_file_free (key_file);
1706 groups = g_key_file_get_groups (key_file, NULL);
1708 for (i = 0; groups[i]; i++)
1710 const gchar *group = groups[i];
1711 SchemaState *schema;
1715 schema = g_hash_table_lookup (schema_table, group);
1719 g_set_error (error, G_KEY_FILE_ERROR,
1720 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1721 _("No such schema `%s' specified in "
1722 "override file `%s'"), group, filename);
1723 g_key_file_free (key_file);
1724 g_strfreev (groups);
1729 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1730 g_assert (keys != NULL);
1732 for (j = 0; keys[j]; j++)
1734 const gchar *key = keys[j];
1739 state = g_hash_table_lookup (schema->keys, key);
1743 g_set_error (error, G_KEY_FILE_ERROR,
1744 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1745 _("No such key `%s' in schema `%s' as "
1746 "specified in override file `%s'"),
1747 key, group, filename);
1748 g_key_file_free (key_file);
1749 g_strfreev (groups);
1755 string = g_key_file_get_value (key_file, group, key, NULL);
1756 g_assert (string != NULL);
1758 value = g_variant_parse (state->type, string,
1763 g_key_file_free (key_file);
1764 g_strfreev (groups);
1773 if (g_variant_compare (value, state->minimum) < 0 ||
1774 g_variant_compare (value, state->maximum) > 0)
1776 g_set_error (error, G_MARKUP_ERROR,
1777 G_MARKUP_ERROR_INVALID_CONTENT,
1778 _("override for key `%s' in schema `%s' in "
1779 "override file `%s' is out of the range "
1780 "given in the schema"),
1781 key, group, filename);
1783 g_key_file_free (key_file);
1784 g_variant_unref (value);
1785 g_strfreev (groups);
1793 else if (state->strinfo->len)
1795 if (!is_valid_choices (value, state->strinfo))
1797 g_set_error (error, G_MARKUP_ERROR,
1798 G_MARKUP_ERROR_INVALID_CONTENT,
1799 _("override for key `%s' in schema `%s' in "
1800 "override file `%s' is not in the list "
1801 "of valid choices"),
1802 key, group, filename);
1804 g_key_file_free (key_file);
1805 g_variant_unref (value);
1806 g_strfreev (groups);
1814 g_variant_unref (state->default_value);
1815 state->default_value = value;
1822 g_strfreev (groups);
1829 main (int argc, char **argv)
1836 gchar *targetdir = NULL;
1838 gboolean uninstall = FALSE;
1839 gboolean dry_run = FALSE;
1840 gboolean strict = FALSE;
1841 gchar **schema_files = NULL;
1842 gchar **override_files = NULL;
1843 GOptionContext *context;
1844 GOptionEntry entries[] = {
1845 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1846 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1847 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1848 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1849 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1851 /* These options are only for use in the gschema-compile tests */
1852 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1856 setlocale (LC_ALL, "");
1858 context = g_option_context_new (N_("DIRECTORY"));
1859 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1860 g_option_context_set_summary (context,
1861 N_("Compile all GSettings schema files into a schema cache.\n"
1862 "Schema files are required to have the extension .gschema.xml,\n"
1863 "and the cache file is called gschemas.compiled."));
1864 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1867 if (!g_option_context_parse (context, &argc, &argv, &error))
1869 fprintf (stderr, "%s\n", error->message);
1873 g_option_context_free (context);
1875 if (!schema_files && argc != 2)
1877 fprintf (stderr, _("You should give exactly one directory name\n"));
1883 if (targetdir == NULL)
1886 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1890 GPtrArray *overrides;
1893 files = g_ptr_array_new ();
1894 overrides = g_ptr_array_new ();
1896 dir = g_dir_open (srcdir, 0, &error);
1899 fprintf (stderr, "%s\n", error->message);
1903 while ((file = g_dir_read_name (dir)) != NULL)
1905 if (g_str_has_suffix (file, ".gschema.xml") ||
1906 g_str_has_suffix (file, ".enums.xml"))
1907 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
1909 else if (g_str_has_suffix (file, ".gschema.override"))
1910 g_ptr_array_add (overrides,
1911 g_build_filename (srcdir, file, NULL));
1914 if (files->len == 0)
1916 fprintf (stderr, _("No schema files found: "));
1918 if (g_unlink (target))
1919 fprintf (stderr, _("doing nothing.\n"));
1922 fprintf (stderr, _("removed existing output file.\n"));
1926 g_ptr_array_sort (files, compare_strings);
1927 g_ptr_array_add (files, NULL);
1929 g_ptr_array_sort (overrides, compare_strings);
1930 g_ptr_array_add (overrides, NULL);
1932 schema_files = (char **) g_ptr_array_free (files, FALSE);
1933 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
1936 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
1942 if ((override_files != NULL && !set_overrides (table, override_files, &error)) ||
1943 (!dry_run && !write_to_file (table, target, &error)))
1945 fprintf (stderr, "%s\n", error->message);
1957 /* vim:set foldmethod=marker: */