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"
40 strip_string (GString *string)
44 for (i = 0; g_ascii_isspace (string->str[i]); i++);
45 g_string_erase (string, 0, i);
49 /* len > 0, so there must be at least one non-whitespace character */
50 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
51 g_string_truncate (string, i + 1);
55 /* Handling of <enum> {{{1 */
64 enum_state_free (gpointer data)
66 EnumState *state = data;
68 g_string_free (state->strinfo, TRUE);
69 g_slice_free (EnumState, state);
73 enum_state_new (gboolean is_flags)
77 state = g_slice_new (EnumState);
78 state->strinfo = g_string_new (NULL);
79 state->is_flags = is_flags;
85 enum_state_add_value (EnumState *state,
87 const gchar *valuestr,
93 if (nick[0] == '\0' || nick[1] == '\0')
95 g_set_error (error, G_MARKUP_ERROR,
96 G_MARKUP_ERROR_INVALID_CONTENT,
97 "nick must be a minimum of 2 characters");
101 value = g_ascii_strtoll (valuestr, &end, 0);
102 if (*end || state->is_flags ?
103 (value > G_MAXUINT32 || value < 0) :
104 (value > G_MAXINT32 || value < G_MININT32))
106 g_set_error (error, G_MARKUP_ERROR,
107 G_MARKUP_ERROR_INVALID_CONTENT,
108 "invalid numeric value");
112 if (strinfo_builder_contains (state->strinfo, nick))
114 g_set_error (error, G_MARKUP_ERROR,
115 G_MARKUP_ERROR_INVALID_CONTENT,
116 "<value nick='%s'/> already specified", nick);
120 if (strinfo_builder_contains_value (state->strinfo, value))
122 g_set_error (error, G_MARKUP_ERROR,
123 G_MARKUP_ERROR_INVALID_CONTENT,
124 "value='%s' already specified", valuestr);
128 /* Silently drop the null case if it is mentioned.
129 * It is properly denoted with an empty array.
131 if (state->is_flags && value == 0)
134 if (state->is_flags && (value & (value - 1)))
136 g_set_error (error, G_MARKUP_ERROR,
137 G_MARKUP_ERROR_INVALID_CONTENT,
138 "flags values must have at most 1 bit set");
142 /* Since we reject exact duplicates of value='' and we only allow one
143 * bit to be set, it's not possible to have overlaps.
145 * If we loosen the one-bit-set restriction we need an overlap check.
148 strinfo_builder_append_item (state->strinfo, nick, value);
152 enum_state_end (EnumState **state_ptr,
160 if (state->strinfo->len == 0)
162 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
163 "<%s> must contain at least one <value>",
164 state->is_flags ? "flags" : "enum");
167 /* Handling of <key> {{{1 */
170 /* for <child>, @child_schema will be set.
171 * for <key>, everything else will be set.
177 gboolean have_gettext_domain;
181 GString *unparsed_default_value;
182 GVariant *default_value;
191 gboolean has_choices;
192 gboolean has_aliases;
193 gboolean is_override;
196 GVariant *serialised;
200 key_state_new (const gchar *type_string,
201 const gchar *gettext_domain,
208 state = g_slice_new0 (KeyState);
209 state->type = g_variant_type_new (type_string);
210 state->have_gettext_domain = gettext_domain != NULL;
211 state->is_enum = is_enum;
212 state->is_flags = is_flags;
215 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
217 state->strinfo = g_string_new (NULL);
223 key_state_override (KeyState *state,
224 const gchar *gettext_domain)
228 copy = g_slice_new0 (KeyState);
229 copy->type = g_variant_type_copy (state->type);
230 copy->have_gettext_domain = gettext_domain != NULL;
231 copy->strinfo = g_string_new_len (state->strinfo->str,
232 state->strinfo->len);
233 copy->is_enum = state->is_enum;
234 copy->is_flags = state->is_flags;
235 copy->is_override = TRUE;
239 copy->minimum = g_variant_ref (state->minimum);
240 copy->maximum = g_variant_ref (state->maximum);
247 key_state_new_child (const gchar *child_schema)
251 state = g_slice_new0 (KeyState);
252 state->child_schema = g_strdup (child_schema);
258 is_valid_choices (GVariant *variant,
261 switch (g_variant_classify (variant))
263 case G_VARIANT_CLASS_MAYBE:
264 case G_VARIANT_CLASS_ARRAY:
266 gboolean valid = TRUE;
269 g_variant_iter_init (&iter, variant);
271 while (valid && (variant = g_variant_iter_next_value (&iter)))
273 valid = is_valid_choices (variant, strinfo);
274 g_variant_unref (variant);
280 case G_VARIANT_CLASS_STRING:
281 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
283 g_variant_get_string (variant, NULL));
286 g_assert_not_reached ();
291 /* Gets called at </default> </choices> or <range/> to check for
292 * validity of the default value so that any inconsistency is
293 * reported as soon as it is encountered.
296 key_state_check_range (KeyState *state,
299 if (state->default_value)
303 tag = state->is_override ? "override" : "default";
307 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
308 g_variant_compare (state->default_value, state->maximum) > 0)
310 g_set_error (error, G_MARKUP_ERROR,
311 G_MARKUP_ERROR_INVALID_CONTENT,
312 "<%s> is not contained in "
313 "the specified range", tag);
317 else if (state->strinfo->len)
319 if (!is_valid_choices (state->default_value, state->strinfo))
322 g_set_error (error, G_MARKUP_ERROR,
323 G_MARKUP_ERROR_INVALID_CONTENT,
324 "<%s> is not a valid member of "
325 "the specified enumerated type", tag);
327 else if (state->is_flags)
328 g_set_error (error, G_MARKUP_ERROR,
329 G_MARKUP_ERROR_INVALID_CONTENT,
330 "<%s> contains string not in the "
331 "specified flags type", tag);
334 g_set_error (error, G_MARKUP_ERROR,
335 G_MARKUP_ERROR_INVALID_CONTENT,
336 "<%s> contains string not in "
344 key_state_set_range (KeyState *state,
345 const gchar *min_str,
346 const gchar *max_str,
355 { 'n', "-32768", "32767" },
356 { 'q', "0", "65535" },
357 { 'i', "-2147483648", "2147483647" },
358 { 'u', "0", "4294967295" },
359 { 'x', "-9223372036854775808", "9223372036854775807" },
360 { 't', "0", "18446744073709551615" },
361 { 'd', "-inf", "inf" },
363 gboolean type_ok = FALSE;
368 g_set_error_literal (error, G_MARKUP_ERROR,
369 G_MARKUP_ERROR_INVALID_CONTENT,
370 "<range/> already specified for this key");
374 for (i = 0; i < G_N_ELEMENTS (table); i++)
375 if (*(char *) state->type == table[i].type)
377 min_str = min_str ? min_str : table[i].min;
378 max_str = max_str ? max_str : table[i].max;
385 gchar *type = g_variant_type_dup_string (state->type);
386 g_set_error (error, G_MARKUP_ERROR,
387 G_MARKUP_ERROR_INVALID_CONTENT,
388 "<range> not allowed for keys of type '%s'", type);
393 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
394 if (state->minimum == NULL)
397 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
398 if (state->maximum == NULL)
401 if (g_variant_compare (state->minimum, state->maximum) > 0)
403 g_set_error (error, G_MARKUP_ERROR,
404 G_MARKUP_ERROR_INVALID_CONTENT,
405 "<range> specified minimum is greater than maxmimum");
409 key_state_check_range (state, error);
413 key_state_start_default (KeyState *state,
415 const gchar *context,
420 if (strcmp (l10n, "messages") == 0)
423 else if (strcmp (l10n, "time") == 0)
428 g_set_error (error, G_MARKUP_ERROR,
429 G_MARKUP_ERROR_INVALID_CONTENT,
430 "unsupported l10n category: %s", l10n);
434 if (!state->have_gettext_domain)
436 g_set_error_literal (error, G_MARKUP_ERROR,
437 G_MARKUP_ERROR_INVALID_CONTENT,
438 "l10n requested, but no "
439 "gettext domain given");
443 state->l10n_context = g_strdup (context);
446 else if (context != NULL)
448 g_set_error_literal (error, G_MARKUP_ERROR,
449 G_MARKUP_ERROR_INVALID_CONTENT,
450 "translation context given for "
451 " value without l10n enabled");
455 return g_string_new (NULL);
459 key_state_end_default (KeyState *state,
463 state->unparsed_default_value = *string;
466 state->default_value = g_variant_parse (state->type,
467 state->unparsed_default_value->str,
469 key_state_check_range (state, error);
473 key_state_start_choices (KeyState *state,
476 const GVariantType *type = state->type;
480 g_set_error_literal (error, G_MARKUP_ERROR,
481 G_MARKUP_ERROR_INVALID_CONTENT,
482 "<choices> cannot be specified for keys "
483 "tagged as having an enumerated type");
487 if (state->has_choices)
489 g_set_error_literal (error, G_MARKUP_ERROR,
490 G_MARKUP_ERROR_INVALID_CONTENT,
491 "<choices> already specified for this key");
495 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
496 type = g_variant_type_element (type);
498 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
500 gchar *type_string = g_variant_type_dup_string (state->type);
501 g_set_error (error, G_MARKUP_ERROR,
502 G_MARKUP_ERROR_INVALID_CONTENT,
503 "<choices> not allowed for keys of type '%s'",
505 g_free (type_string);
511 key_state_add_choice (KeyState *state,
515 if (strinfo_builder_contains (state->strinfo, choice))
517 g_set_error (error, G_MARKUP_ERROR,
518 G_MARKUP_ERROR_INVALID_CONTENT,
519 "<choice value='%s'/> already given", choice);
523 strinfo_builder_append_item (state->strinfo, choice, 0);
524 state->has_choices = TRUE;
528 key_state_end_choices (KeyState *state,
531 if (!state->has_choices)
533 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
534 "<choices> must contain at least one <choice>");
538 key_state_check_range (state, error);
542 key_state_start_aliases (KeyState *state,
545 if (state->has_aliases)
546 g_set_error_literal (error, G_MARKUP_ERROR,
547 G_MARKUP_ERROR_INVALID_CONTENT,
548 "<aliases> already specified for this key");
549 else if (!state->is_flags && !state->is_enum && !state->has_choices)
550 g_set_error_literal (error, G_MARKUP_ERROR,
551 G_MARKUP_ERROR_INVALID_CONTENT,
552 "<aliases> can only be specified for keys with "
553 "enumerated or flags types or after <choices>");
557 key_state_add_alias (KeyState *state,
562 if (strinfo_builder_contains (state->strinfo, alias))
564 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
565 state->strinfo->len / 4,
569 g_set_error (error, G_MARKUP_ERROR,
570 G_MARKUP_ERROR_INVALID_CONTENT,
571 "<alias value='%s'/> given when '%s' is already "
572 "a member of the enumerated type", alias, alias);
575 g_set_error (error, G_MARKUP_ERROR,
576 G_MARKUP_ERROR_INVALID_CONTENT,
577 "<alias value='%s'/> given when "
578 "<choice value='%s'/> was already given",
583 g_set_error (error, G_MARKUP_ERROR,
584 G_MARKUP_ERROR_INVALID_CONTENT,
585 "<alias value='%s'/> already specified", alias);
590 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
592 g_set_error (error, G_MARKUP_ERROR,
593 G_MARKUP_ERROR_INVALID_CONTENT,
594 "alias target '%s' is not in %s", target,
595 state->is_enum ? "enumerated type" : "<choices>");
599 state->has_aliases = TRUE;
603 key_state_end_aliases (KeyState *state,
606 if (!state->has_aliases)
608 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
609 "<aliases> must contain at least one <alias>");
615 key_state_check (KeyState *state,
621 return state->checked = TRUE;
625 key_state_serialise (KeyState *state)
627 if (state->serialised == NULL)
629 if (state->child_schema)
631 state->serialised = g_variant_new_string (state->child_schema);
636 GVariantBuilder builder;
638 g_assert (key_state_check (state, NULL));
640 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
643 g_variant_builder_add_value (&builder, state->default_value);
648 /* We are going to store the untranslated default for
649 * runtime translation according to the current locale.
650 * We need to strip leading and trailing whitespace from
651 * the string so that it's exactly the same as the one
652 * that ended up in the .po file for translation.
654 * We want to do this so that
656 * <default l10n='messages'>
660 * ends up in the .po file like "['a', 'b', 'c']",
661 * omitting the extra whitespace at the start and end.
663 strip_string (state->unparsed_default_value);
665 if (state->l10n_context)
669 /* Contextified messages are supported by prepending
670 * the context, followed by '\004' to the start of the
671 * message string. We do that here to save GSettings
674 len = strlen (state->l10n_context);
675 state->l10n_context[len] = '\004';
676 g_string_prepend_len (state->unparsed_default_value,
677 state->l10n_context, len + 1);
678 g_free (state->l10n_context);
679 state->l10n_context = NULL;
682 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
683 state->unparsed_default_value->str);
684 g_string_free (state->unparsed_default_value, TRUE);
685 state->unparsed_default_value = NULL;
688 /* choice, aliases, enums */
689 if (state->strinfo->len)
697 data = state->strinfo->str;
698 size = state->strinfo->len;
701 for (i = 0; i < size / sizeof (guint32); i++)
702 words[i] = GUINT32_TO_LE (words[i]);
704 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
708 g_string_free (state->strinfo, FALSE);
709 state->strinfo = NULL;
711 g_variant_builder_add (&builder, "(y@au)",
712 state->is_flags ? 'f' :
713 state->is_enum ? 'e' : 'c',
718 if (state->minimum || state->maximum)
719 g_variant_builder_add (&builder, "(y(**))", 'r',
720 state->minimum, state->maximum);
722 state->serialised = g_variant_builder_end (&builder);
725 g_variant_ref_sink (state->serialised);
728 return g_variant_ref (state->serialised);
732 key_state_free (gpointer data)
734 KeyState *state = data;
737 g_variant_type_free (state->type);
739 g_free (state->l10n_context);
741 if (state->unparsed_default_value)
742 g_string_free (state->unparsed_default_value, TRUE);
744 if (state->default_value)
745 g_variant_unref (state->default_value);
748 g_string_free (state->strinfo, TRUE);
751 g_variant_unref (state->minimum);
754 g_variant_unref (state->maximum);
756 if (state->serialised)
757 g_variant_unref (state->serialised);
759 g_slice_free (KeyState, state);
762 /* Key name validity {{{1 */
763 static gboolean allow_any_name = FALSE;
766 is_valid_keyname (const gchar *key,
773 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
774 _("empty names are not permitted"));
781 if (!g_ascii_islower (key[0]))
783 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
784 _("invalid name '%s': names must begin "
785 "with a lowercase letter"), key);
789 for (i = 1; key[i]; i++)
792 !g_ascii_islower (key[i]) &&
793 !g_ascii_isdigit (key[i]))
795 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
796 _("invalid name '%s': invalid character '%c'; "
797 "only lowercase letters, numbers and hyphen ('-') "
798 "are permitted."), key, key[i]);
802 if (key[i] == '-' && key[i + 1] == '-')
804 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
805 _("invalid name '%s': two successive hyphens ('--') "
806 "are not permitted."), key);
811 if (key[i - 1] == '-')
813 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
814 _("invalid name '%s': the last character may not be a "
815 "hyphen ('-')."), key);
821 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
822 _("invalid name '%s': maximum length is 1024"), key);
829 /* Handling of <schema> {{{1 */
830 typedef struct _SchemaState SchemaState;
833 SchemaState *extends;
836 gchar *gettext_domain;
844 schema_state_new (const gchar *path,
845 const gchar *gettext_domain,
846 SchemaState *extends,
847 const gchar *extends_name,
848 const gchar *list_of)
852 state = g_slice_new (SchemaState);
853 state->path = g_strdup (path);
854 state->gettext_domain = g_strdup (gettext_domain);
855 state->extends = extends;
856 state->extends_name = g_strdup (extends_name);
857 state->list_of = g_strdup (list_of);
858 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
859 g_free, key_state_free);
865 schema_state_free (gpointer data)
867 SchemaState *state = data;
869 g_free (state->path);
870 g_free (state->gettext_domain);
871 g_hash_table_unref (state->keys);
875 schema_state_add_child (SchemaState *state,
882 if (!is_valid_keyname (name, error))
885 childname = g_strconcat (name, "/", NULL);
887 if (g_hash_table_lookup (state->keys, childname))
889 g_set_error (error, G_MARKUP_ERROR,
890 G_MARKUP_ERROR_INVALID_CONTENT,
891 _("<child name='%s'> already specified"), name);
895 g_hash_table_insert (state->keys, childname,
896 key_state_new_child (schema));
900 schema_state_add_key (SchemaState *state,
901 GHashTable *enum_table,
902 GHashTable *flags_table,
904 const gchar *type_string,
905 const gchar *enum_type,
906 const gchar *flags_type,
915 g_set_error_literal (error, G_MARKUP_ERROR,
916 G_MARKUP_ERROR_INVALID_CONTENT,
917 _("cannot add keys to a 'list-of' schema"));
921 if (!is_valid_keyname (name, error))
924 if (g_hash_table_lookup (state->keys, name))
926 g_set_error (error, G_MARKUP_ERROR,
927 G_MARKUP_ERROR_INVALID_CONTENT,
928 _("<key name='%s'> already specified"), name);
932 for (node = state; node; node = node->extends)
937 shadow = g_hash_table_lookup (node->extends->keys, name);
939 /* in case of <key> <override> <key> make sure we report the
940 * location of the original <key>, not the <override>.
942 if (shadow && !shadow->is_override)
944 g_set_error (error, G_MARKUP_ERROR,
945 G_MARKUP_ERROR_INVALID_CONTENT,
946 _("<key name='%s'> shadows <key name='%s'> in "
947 "<schema id='%s'>; use <override> to modify value"),
948 name, name, node->extends_name);
953 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
955 g_set_error (error, G_MARKUP_ERROR,
956 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
957 _("exactly one of 'type', 'enum' or 'flags' must "
958 "be specified as an attribute to <key>"));
962 if (type_string == NULL) /* flags or enums was specified */
964 EnumState *enum_state;
967 enum_state = g_hash_table_lookup (enum_table, enum_type);
969 enum_state = g_hash_table_lookup (flags_table, flags_type);
972 if (enum_state == NULL)
974 g_set_error (error, G_MARKUP_ERROR,
975 G_MARKUP_ERROR_INVALID_CONTENT,
976 _("<%s id='%s'> not (yet) defined."),
977 flags_type ? "flags" : "enum",
978 flags_type ? flags_type : enum_type);
982 type_string = flags_type ? "as" : "s";
983 strinfo = enum_state->strinfo;
987 if (!g_variant_type_string_is_valid (type_string))
989 g_set_error (error, G_MARKUP_ERROR,
990 G_MARKUP_ERROR_INVALID_CONTENT,
991 _("invalid GVariant type string '%s'"), type_string);
998 key = key_state_new (type_string, state->gettext_domain,
999 enum_type != NULL, flags_type != NULL, strinfo);
1000 g_hash_table_insert (state->keys, g_strdup (name), key);
1006 schema_state_add_override (SchemaState *state,
1007 KeyState **key_state,
1011 const gchar *context,
1014 SchemaState *parent;
1017 if (state->extends == NULL)
1019 g_set_error_literal (error, G_MARKUP_ERROR,
1020 G_MARKUP_ERROR_INVALID_CONTENT,
1021 _("<override> given but schema isn't "
1022 "extending anything"));
1026 for (parent = state->extends; parent; parent = parent->extends)
1027 if ((original = g_hash_table_lookup (parent->keys, key)))
1030 if (original == NULL)
1032 g_set_error (error, G_MARKUP_ERROR,
1033 G_MARKUP_ERROR_INVALID_CONTENT,
1034 _("no <key name='%s'> to override"), key);
1038 if (g_hash_table_lookup (state->keys, key))
1040 g_set_error (error, G_MARKUP_ERROR,
1041 G_MARKUP_ERROR_INVALID_CONTENT,
1042 _("<override name='%s'> already specified"), key);
1046 *key_state = key_state_override (original, state->gettext_domain);
1047 *string = key_state_start_default (*key_state, l10n, context, error);
1048 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1052 override_state_end (KeyState **key_state,
1056 key_state_end_default (*key_state, string, error);
1060 /* Handling of toplevel state {{{1 */
1063 GHashTable *schema_table; /* string -> SchemaState */
1064 GHashTable *flags_table; /* string -> EnumState */
1065 GHashTable *enum_table; /* string -> EnumState */
1067 GSList *this_file_schemas; /* strings: <schema>s in this file */
1068 GSList *this_file_flagss; /* strings: <flags>s in this file */
1069 GSList *this_file_enums; /* strings: <enum>s in this file */
1071 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1073 SchemaState *schema_state; /* non-NULL when inside <schema> */
1074 KeyState *key_state; /* non-NULL when inside <key> */
1075 EnumState *enum_state; /* non-NULL when inside <enum> */
1077 GString *string; /* non-NULL when accepting text */
1081 is_subclass (const gchar *class_name,
1082 const gchar *possible_parent,
1083 GHashTable *schema_table)
1087 if (strcmp (class_name, possible_parent) == 0)
1090 class = g_hash_table_lookup (schema_table, class_name);
1091 g_assert (class != NULL);
1093 return class->extends_name &&
1094 is_subclass (class->extends_name, possible_parent, schema_table);
1098 parse_state_start_schema (ParseState *state,
1101 const gchar *gettext_domain,
1102 const gchar *extends_name,
1103 const gchar *list_of,
1106 SchemaState *extends;
1109 if (g_hash_table_lookup (state->schema_table, id))
1111 g_set_error (error, G_MARKUP_ERROR,
1112 G_MARKUP_ERROR_INVALID_CONTENT,
1113 _("<schema id='%s'> already specified"), id);
1119 extends = g_hash_table_lookup (state->schema_table, extends_name);
1121 if (extends == NULL)
1123 g_set_error (error, G_MARKUP_ERROR,
1124 G_MARKUP_ERROR_INVALID_CONTENT,
1125 _("<schema id='%s'> extends not-yet-existing "
1126 "schema '%s'"), id, extends_name);
1137 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1139 g_set_error (error, G_MARKUP_ERROR,
1140 G_MARKUP_ERROR_INVALID_CONTENT,
1141 _("<schema id='%s'> is list of not-yet-existing "
1142 "schema '%s'"), id, list_of);
1148 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1149 _("Can not be a list of a schema with a path"));
1158 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1159 _("Can not extend a schema with a path"));
1165 if (extends->list_of == NULL)
1167 g_set_error (error, G_MARKUP_ERROR,
1168 G_MARKUP_ERROR_INVALID_CONTENT,
1169 _("<schema id='%s'> is a list, extending "
1170 "<schema id='%s'> which is not a list"),
1175 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1177 g_set_error (error, G_MARKUP_ERROR,
1178 G_MARKUP_ERROR_INVALID_CONTENT,
1179 _("<schema id='%s' list-of='%s'> extends <schema "
1180 "id='%s' list-of='%s'> but '%s' does not "
1181 "extend '%s'"), id, list_of, extends_name,
1182 extends->list_of, list_of, extends->list_of);
1187 /* by default we are a list of the same thing that the schema
1188 * we are extending is a list of (which might be nothing)
1190 list_of = extends->list_of;
1193 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1195 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1196 _("a path, if given, must begin and end with a slash"));
1200 if (path && list_of && !g_str_has_suffix (path, ":/"))
1202 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1203 _("the path of a list must end with ':/'"));
1207 if (path && (g_str_has_prefix (path, "/apps/") ||
1208 g_str_has_prefix (path, "/desktop/") ||
1209 g_str_has_prefix (path, "/system/")))
1210 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1211 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1213 state->schema_state = schema_state_new (path, gettext_domain,
1214 extends, extends_name, list_of);
1216 my_id = g_strdup (id);
1217 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1218 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1222 parse_state_start_enum (ParseState *state,
1227 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1228 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1231 if (g_hash_table_lookup (table, id))
1233 g_set_error (error, G_MARKUP_ERROR,
1234 G_MARKUP_ERROR_INVALID_CONTENT,
1235 _("<%s id='%s'> already specified"),
1236 is_flags ? "flags" : "enum", id);
1240 state->enum_state = enum_state_new (is_flags);
1242 my_id = g_strdup (id);
1243 *list = g_slist_prepend (*list, my_id);
1244 g_hash_table_insert (table, my_id, state->enum_state);
1247 /* GMarkup Parser Functions {{{1 */
1249 /* Start element {{{2 */
1251 start_element (GMarkupParseContext *context,
1252 const gchar *element_name,
1253 const gchar **attribute_names,
1254 const gchar **attribute_values,
1258 ParseState *state = user_data;
1259 const GSList *element_stack;
1260 const gchar *container;
1262 element_stack = g_markup_parse_context_get_element_stack (context);
1263 container = element_stack->next ? element_stack->next->data : NULL;
1265 #define COLLECT(first, ...) \
1266 g_markup_collect_attributes (element_name, \
1267 attribute_names, attribute_values, error, \
1268 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1269 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1270 #define STRDUP G_MARKUP_COLLECT_STRDUP
1271 #define STRING G_MARKUP_COLLECT_STRING
1272 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1274 /* Toplevel items {{{3 */
1275 if (container == NULL)
1277 if (strcmp (element_name, "schemalist") == 0)
1279 COLLECT (OPTIONAL | STRDUP,
1281 &state->schemalist_domain);
1287 /* children of <schemalist> {{{3 */
1288 else if (strcmp (container, "schemalist") == 0)
1290 if (strcmp (element_name, "schema") == 0)
1292 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1293 if (COLLECT (STRING, "id", &id,
1294 OPTIONAL | STRING, "path", &path,
1295 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1296 OPTIONAL | STRING, "extends", &extends,
1297 OPTIONAL | STRING, "list-of", &list_of))
1298 parse_state_start_schema (state, id, path,
1299 gettext_domain ? gettext_domain
1300 : state->schemalist_domain,
1301 extends, list_of, error);
1305 else if (strcmp (element_name, "enum") == 0)
1308 if (COLLECT (STRING, "id", &id))
1309 parse_state_start_enum (state, id, FALSE, error);
1313 else if (strcmp (element_name, "flags") == 0)
1316 if (COLLECT (STRING, "id", &id))
1317 parse_state_start_enum (state, id, TRUE, error);
1323 /* children of <schema> {{{3 */
1324 else if (strcmp (container, "schema") == 0)
1326 if (strcmp (element_name, "key") == 0)
1328 const gchar *name, *type_string, *enum_type, *flags_type;
1330 if (COLLECT (STRING, "name", &name,
1331 OPTIONAL | STRING, "type", &type_string,
1332 OPTIONAL | STRING, "enum", &enum_type,
1333 OPTIONAL | STRING, "flags", &flags_type))
1335 state->key_state = schema_state_add_key (state->schema_state,
1339 enum_type, flags_type,
1343 else if (strcmp (element_name, "child") == 0)
1345 const gchar *name, *schema;
1347 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1348 schema_state_add_child (state->schema_state,
1349 name, schema, error);
1352 else if (strcmp (element_name, "override") == 0)
1354 const gchar *name, *l10n, *context;
1356 if (COLLECT (STRING, "name", &name,
1357 OPTIONAL | STRING, "l10n", &l10n,
1358 OPTIONAL | STRING, "context", &context))
1359 schema_state_add_override (state->schema_state,
1360 &state->key_state, &state->string,
1361 name, l10n, context, error);
1366 /* children of <key> {{{3 */
1367 else if (strcmp (container, "key") == 0)
1369 if (strcmp (element_name, "default") == 0)
1371 const gchar *l10n, *context;
1372 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1373 STRING | OPTIONAL, "context", &context))
1374 state->string = key_state_start_default (state->key_state,
1375 l10n, context, error);
1379 else if (strcmp (element_name, "summary") == 0 ||
1380 strcmp (element_name, "description") == 0)
1383 state->string = g_string_new (NULL);
1387 else if (strcmp (element_name, "range") == 0)
1389 const gchar *min, *max;
1390 if (COLLECT (STRING | OPTIONAL, "min", &min,
1391 STRING | OPTIONAL, "max", &max))
1392 key_state_set_range (state->key_state, min, max, error);
1396 else if (strcmp (element_name, "choices") == 0)
1399 key_state_start_choices (state->key_state, error);
1403 else if (strcmp (element_name, "aliases") == 0)
1406 key_state_start_aliases (state->key_state, error);
1412 /* children of <choices> {{{3 */
1413 else if (strcmp (container, "choices") == 0)
1415 if (strcmp (element_name, "choice") == 0)
1418 if (COLLECT (STRING, "value", &value))
1419 key_state_add_choice (state->key_state, value, error);
1425 /* children of <aliases> {{{3 */
1426 else if (strcmp (container, "aliases") == 0)
1428 if (strcmp (element_name, "alias") == 0)
1430 const gchar *value, *target;
1431 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1432 key_state_add_alias (state->key_state, value, target, error);
1438 /* children of <enum> {{{3 */
1439 else if (strcmp (container, "enum") == 0 ||
1440 strcmp (container, "flags") == 0)
1442 if (strcmp (element_name, "value") == 0)
1444 const gchar *nick, *valuestr;
1445 if (COLLECT (STRING, "nick", &nick,
1446 STRING, "value", &valuestr))
1447 enum_state_add_value (state->enum_state, nick, valuestr, error);
1454 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1455 _("Element <%s> not allowed inside <%s>"),
1456 element_name, container);
1458 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1459 _("Element <%s> not allowed at the top level"), element_name);
1462 /* End element {{{2 */
1465 key_state_end (KeyState **state_ptr,
1473 if (state->default_value == NULL)
1475 g_set_error_literal (error,
1476 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1477 "element <default> is required in <key>");
1483 schema_state_end (SchemaState **state_ptr,
1490 end_element (GMarkupParseContext *context,
1491 const gchar *element_name,
1495 ParseState *state = user_data;
1497 if (strcmp (element_name, "schemalist") == 0)
1499 g_free (state->schemalist_domain);
1500 state->schemalist_domain = NULL;
1503 else if (strcmp (element_name, "enum") == 0 ||
1504 strcmp (element_name, "flags") == 0)
1505 enum_state_end (&state->enum_state, error);
1507 else if (strcmp (element_name, "schema") == 0)
1508 schema_state_end (&state->schema_state, error);
1510 else if (strcmp (element_name, "override") == 0)
1511 override_state_end (&state->key_state, &state->string, error);
1513 else if (strcmp (element_name, "key") == 0)
1514 key_state_end (&state->key_state, error);
1516 else if (strcmp (element_name, "default") == 0)
1517 key_state_end_default (state->key_state, &state->string, error);
1519 else if (strcmp (element_name, "choices") == 0)
1520 key_state_end_choices (state->key_state, error);
1522 else if (strcmp (element_name, "aliases") == 0)
1523 key_state_end_aliases (state->key_state, error);
1527 g_string_free (state->string, TRUE);
1528 state->string = NULL;
1533 text (GMarkupParseContext *context,
1539 ParseState *state = user_data;
1543 /* we are expecting a string, so store the text data.
1545 * we store the data verbatim here and deal with whitespace
1546 * later on. there are two reasons for that:
1548 * 1) whitespace is handled differently depending on the tag
1551 * 2) we could do leading whitespace removal by refusing to
1552 * insert it into state->string if it's at the start, but for
1553 * trailing whitespace, we have no idea if there is another
1554 * text() call coming or not.
1556 g_string_append_len (state->string, text, text_len);
1560 /* string is not expected: accept (and ignore) pure whitespace */
1563 for (i = 0; i < text_len; i++)
1564 if (!g_ascii_isspace (text[i]))
1566 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1567 _("text may not appear inside <%s>"),
1568 g_markup_parse_context_get_element (context));
1574 /* Write to GVDB {{{1 */
1582 gvdb_pair_init (GvdbPair *pair)
1584 pair->table = gvdb_hash_table_new (NULL, NULL);
1585 pair->root = gvdb_hash_table_insert (pair->table, "");
1590 GHashTable *schema_table;
1596 GHashTable *schema_table;
1602 output_key (gpointer key,
1606 OutputSchemaData *data;
1615 item = gvdb_hash_table_insert (data->pair.table, name);
1616 gvdb_item_set_parent (item, data->pair.root);
1617 gvdb_item_set_value (item, key_state_serialise (state));
1622 if (state->child_schema &&
1623 !g_hash_table_lookup (data->schema_table, state->child_schema))
1624 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1625 state->child_schema);
1629 output_schema (gpointer key,
1633 WriteToFileData *wtf_data = user_data;
1634 OutputSchemaData data;
1635 GvdbPair *root_pair;
1642 root_pair = &wtf_data->root_pair;
1644 data.schema_table = wtf_data->schema_table;
1645 gvdb_pair_init (&data.pair);
1648 item = gvdb_hash_table_insert (root_pair->table, id);
1649 gvdb_item_set_parent (item, root_pair->root);
1650 gvdb_item_set_hash_table (item, data.pair.table);
1652 g_hash_table_foreach (state->keys, output_key, &data);
1655 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1657 if (state->extends_name)
1658 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1659 state->extends_name);
1662 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1666 gvdb_hash_table_insert_string (data.pair.table,
1668 state->gettext_domain);
1672 write_to_file (GHashTable *schema_table,
1673 const gchar *filename,
1676 WriteToFileData data;
1679 data.schema_table = schema_table;
1681 gvdb_pair_init (&data.root_pair);
1683 g_hash_table_foreach (schema_table, output_schema, &data);
1685 success = gvdb_table_write_contents (data.root_pair.table, filename,
1686 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1688 g_hash_table_unref (data.root_pair.table);
1693 /* Parser driver {{{1 */
1695 parse_gschema_files (gchar **files,
1698 GMarkupParser parser = { start_element, end_element, text };
1699 ParseState state = { 0, };
1700 const gchar *filename;
1701 GError *error = NULL;
1703 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1704 g_free, enum_state_free);
1706 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1707 g_free, enum_state_free);
1709 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1710 g_free, schema_state_free);
1712 while ((filename = *files++) != NULL)
1714 GMarkupParseContext *context;
1718 if (!g_file_get_contents (filename, &contents, &size, &error))
1720 fprintf (stderr, "%s\n", error->message);
1721 g_clear_error (&error);
1725 context = g_markup_parse_context_new (&parser,
1726 G_MARKUP_TREAT_CDATA_AS_TEXT |
1727 G_MARKUP_PREFIX_ERROR_POSITION,
1731 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1732 !g_markup_parse_context_end_parse (context, &error))
1736 /* back out any changes from this file */
1737 for (item = state.this_file_schemas; item; item = item->next)
1738 g_hash_table_remove (state.schema_table, item->data);
1740 for (item = state.this_file_flagss; item; item = item->next)
1741 g_hash_table_remove (state.flags_table, item->data);
1743 for (item = state.this_file_enums; item; item = item->next)
1744 g_hash_table_remove (state.enum_table, item->data);
1747 fprintf (stderr, "%s: %s. ", filename, error->message);
1748 g_clear_error (&error);
1752 /* Translators: Do not translate "--strict". */
1753 fprintf (stderr, _("--strict was specified; exiting.\n"));
1754 g_hash_table_unref (state.schema_table);
1755 g_hash_table_unref (state.flags_table);
1756 g_hash_table_unref (state.enum_table);
1761 fprintf (stderr, _("This entire file has been ignored.\n"));
1765 g_markup_parse_context_free (context);
1766 g_slist_free (state.this_file_schemas);
1767 g_slist_free (state.this_file_flagss);
1768 g_slist_free (state.this_file_enums);
1769 state.this_file_schemas = NULL;
1770 state.this_file_flagss = NULL;
1771 state.this_file_enums = NULL;
1774 g_hash_table_unref (state.flags_table);
1775 g_hash_table_unref (state.enum_table);
1777 return state.schema_table;
1781 compare_strings (gconstpointer a,
1784 gchar *one = *(gchar **) a;
1785 gchar *two = *(gchar **) b;
1788 cmp = g_str_has_suffix (two, ".enums.xml") -
1789 g_str_has_suffix (one, ".enums.xml");
1792 cmp = strcmp (one, two);
1798 set_overrides (GHashTable *schema_table,
1802 const gchar *filename;
1803 GError *error = NULL;
1805 while ((filename = *files++))
1811 key_file = g_key_file_new ();
1812 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1814 fprintf (stderr, "%s: %s. ", filename, error->message);
1815 g_key_file_free (key_file);
1816 g_clear_error (&error);
1820 fprintf (stderr, _("Ignoring this file.\n"));
1824 fprintf (stderr, _("--strict was specified; exiting.\n"));
1828 groups = g_key_file_get_groups (key_file, NULL);
1830 for (i = 0; groups[i]; i++)
1832 const gchar *group = groups[i];
1833 SchemaState *schema;
1837 schema = g_hash_table_lookup (schema_table, group);
1840 /* Having the schema not be installed is expected to be a
1841 * common case. Don't even emit an error message about
1846 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1847 g_assert (keys != NULL);
1849 for (j = 0; keys[j]; j++)
1851 const gchar *key = keys[j];
1856 state = g_hash_table_lookup (schema->keys, key);
1860 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1861 "specified in override file `%s'"),
1862 key, group, filename);
1866 fprintf (stderr, _("; ignoring override for this key.\n"));
1870 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1871 g_key_file_free (key_file);
1872 g_strfreev (groups);
1878 string = g_key_file_get_value (key_file, group, key, NULL);
1879 g_assert (string != NULL);
1881 value = g_variant_parse (state->type, string,
1882 NULL, NULL, &error);
1886 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1887 "as specified in override file `%s': "
1889 key, group, filename, error->message);
1891 g_clear_error (&error);
1896 fprintf (stderr, _("Ignoring override for this key.\n"));
1900 fprintf (stderr, _("--strict was specified; exiting.\n"));
1901 g_key_file_free (key_file);
1902 g_strfreev (groups);
1910 if (g_variant_compare (value, state->minimum) < 0 ||
1911 g_variant_compare (value, state->maximum) > 0)
1914 _("override for key `%s' in schema `%s' in "
1915 "override file `%s' is outside the range "
1916 "given in the schema"),
1917 key, group, filename);
1919 g_variant_unref (value);
1924 fprintf (stderr, _("; ignoring override for this key.\n"));
1928 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1929 g_key_file_free (key_file);
1930 g_strfreev (groups);
1937 else if (state->strinfo->len)
1939 if (!is_valid_choices (value, state->strinfo))
1942 _("override for key `%s' in schema `%s' in "
1943 "override file `%s' is not in the list "
1944 "of valid choices"),
1945 key, group, filename);
1947 g_variant_unref (value);
1952 fprintf (stderr, _("; ignoring override for this key.\n"));
1956 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1957 g_key_file_free (key_file);
1958 g_strfreev (groups);
1965 g_variant_unref (state->default_value);
1966 state->default_value = value;
1973 g_strfreev (groups);
1980 main (int argc, char **argv)
1987 gchar *targetdir = NULL;
1989 gboolean dry_run = FALSE;
1990 gboolean strict = FALSE;
1991 gchar **schema_files = NULL;
1992 gchar **override_files = NULL;
1993 GOptionContext *context;
1994 GOptionEntry entries[] = {
1995 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1996 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1997 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1998 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2000 /* These options are only for use in the gschema-compile tests */
2001 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2006 extern gchar *_glib_get_locale_dir (void);
2010 setlocale (LC_ALL, "");
2011 textdomain (GETTEXT_PACKAGE);
2014 tmp = _glib_get_locale_dir ();
2015 bindtextdomain (GETTEXT_PACKAGE, tmp);
2018 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2021 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2022 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2025 context = g_option_context_new (N_("DIRECTORY"));
2026 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2027 g_option_context_set_summary (context,
2028 N_("Compile all GSettings schema files into a schema cache.\n"
2029 "Schema files are required to have the extension .gschema.xml,\n"
2030 "and the cache file is called gschemas.compiled."));
2031 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2034 if (!g_option_context_parse (context, &argc, &argv, &error))
2036 fprintf (stderr, "%s\n", error->message);
2040 g_option_context_free (context);
2042 if (!schema_files && argc != 2)
2044 fprintf (stderr, _("You should give exactly one directory name\n"));
2050 if (targetdir == NULL)
2053 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2057 GPtrArray *overrides;
2060 files = g_ptr_array_new ();
2061 overrides = g_ptr_array_new ();
2063 dir = g_dir_open (srcdir, 0, &error);
2066 fprintf (stderr, "%s\n", error->message);
2070 while ((file = g_dir_read_name (dir)) != NULL)
2072 if (g_str_has_suffix (file, ".gschema.xml") ||
2073 g_str_has_suffix (file, ".enums.xml"))
2074 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2076 else if (g_str_has_suffix (file, ".gschema.override"))
2077 g_ptr_array_add (overrides,
2078 g_build_filename (srcdir, file, NULL));
2081 if (files->len == 0)
2083 fprintf (stdout, _("No schema files found: "));
2085 if (g_unlink (target))
2086 fprintf (stdout, _("doing nothing.\n"));
2089 fprintf (stdout, _("removed existing output file.\n"));
2093 g_ptr_array_sort (files, compare_strings);
2094 g_ptr_array_add (files, NULL);
2096 g_ptr_array_sort (overrides, compare_strings);
2097 g_ptr_array_add (overrides, NULL);
2099 schema_files = (char **) g_ptr_array_free (files, FALSE);
2100 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2103 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2109 if (override_files != NULL &&
2110 !set_overrides (table, override_files, strict))
2116 if (!dry_run && !write_to_file (table, target, &error))
2118 fprintf (stderr, "%s\n", error->message);
2130 /* vim:set foldmethod=marker: */