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 dash ('-') "
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 dashes ('--') "
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 "dash ('-')."), 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 "
1126 "existing 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 "
1142 "existing 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 state->schema_state = schema_state_new (path, gettext_domain,
1208 extends, extends_name, list_of);
1210 my_id = g_strdup (id);
1211 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1212 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1216 parse_state_start_enum (ParseState *state,
1221 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1222 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1225 if (g_hash_table_lookup (table, id))
1227 g_set_error (error, G_MARKUP_ERROR,
1228 G_MARKUP_ERROR_INVALID_CONTENT,
1229 _("<%s id='%s'> already specified"),
1230 is_flags ? "flags" : "enum", id);
1234 state->enum_state = enum_state_new (is_flags);
1236 my_id = g_strdup (id);
1237 *list = g_slist_prepend (*list, my_id);
1238 g_hash_table_insert (table, my_id, state->enum_state);
1241 /* GMarkup Parser Functions {{{1 */
1243 /* Start element {{{2 */
1245 start_element (GMarkupParseContext *context,
1246 const gchar *element_name,
1247 const gchar **attribute_names,
1248 const gchar **attribute_values,
1252 ParseState *state = user_data;
1253 const GSList *element_stack;
1254 const gchar *container;
1256 element_stack = g_markup_parse_context_get_element_stack (context);
1257 container = element_stack->next ? element_stack->next->data : NULL;
1259 #define COLLECT(first, ...) \
1260 g_markup_collect_attributes (element_name, \
1261 attribute_names, attribute_values, error, \
1262 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1263 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1264 #define STRDUP G_MARKUP_COLLECT_STRDUP
1265 #define STRING G_MARKUP_COLLECT_STRING
1266 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1268 /* Toplevel items {{{3 */
1269 if (container == NULL)
1271 if (strcmp (element_name, "schemalist") == 0)
1273 COLLECT (OPTIONAL | STRDUP,
1275 &state->schemalist_domain);
1281 /* children of <schemalist> {{{3 */
1282 else if (strcmp (container, "schemalist") == 0)
1284 if (strcmp (element_name, "schema") == 0)
1286 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1287 if (COLLECT (STRING, "id", &id,
1288 OPTIONAL | STRING, "path", &path,
1289 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1290 OPTIONAL | STRING, "extends", &extends,
1291 OPTIONAL | STRING, "list-of", &list_of))
1292 parse_state_start_schema (state, id, path,
1293 gettext_domain ? gettext_domain
1294 : state->schemalist_domain,
1295 extends, list_of, error);
1299 else if (strcmp (element_name, "enum") == 0)
1302 if (COLLECT (STRING, "id", &id))
1303 parse_state_start_enum (state, id, FALSE, error);
1307 else if (strcmp (element_name, "flags") == 0)
1310 if (COLLECT (STRING, "id", &id))
1311 parse_state_start_enum (state, id, TRUE, error);
1317 /* children of <schema> {{{3 */
1318 else if (strcmp (container, "schema") == 0)
1320 if (strcmp (element_name, "key") == 0)
1322 const gchar *name, *type_string, *enum_type, *flags_type;
1324 if (COLLECT (STRING, "name", &name,
1325 OPTIONAL | STRING, "type", &type_string,
1326 OPTIONAL | STRING, "enum", &enum_type,
1327 OPTIONAL | STRING, "flags", &flags_type))
1329 state->key_state = schema_state_add_key (state->schema_state,
1333 enum_type, flags_type,
1337 else if (strcmp (element_name, "child") == 0)
1339 const gchar *name, *schema;
1341 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1342 schema_state_add_child (state->schema_state,
1343 name, schema, error);
1346 else if (strcmp (element_name, "override") == 0)
1348 const gchar *name, *l10n, *context;
1350 if (COLLECT (STRING, "name", &name,
1351 OPTIONAL | STRING, "l10n", &l10n,
1352 OPTIONAL | STRING, "context", &context))
1353 schema_state_add_override (state->schema_state,
1354 &state->key_state, &state->string,
1355 name, l10n, context, error);
1360 /* children of <key> {{{3 */
1361 else if (strcmp (container, "key") == 0)
1363 if (strcmp (element_name, "default") == 0)
1365 const gchar *l10n, *context;
1366 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1367 STRING | OPTIONAL, "context", &context))
1368 state->string = key_state_start_default (state->key_state,
1369 l10n, context, error);
1373 else if (strcmp (element_name, "summary") == 0 ||
1374 strcmp (element_name, "description") == 0)
1377 state->string = g_string_new (NULL);
1381 else if (strcmp (element_name, "range") == 0)
1383 const gchar *min, *max;
1384 if (COLLECT (STRING | OPTIONAL, "min", &min,
1385 STRING | OPTIONAL, "max", &max))
1386 key_state_set_range (state->key_state, min, max, error);
1390 else if (strcmp (element_name, "choices") == 0)
1393 key_state_start_choices (state->key_state, error);
1397 else if (strcmp (element_name, "aliases") == 0)
1400 key_state_start_aliases (state->key_state, error);
1406 /* children of <choices> {{{3 */
1407 else if (strcmp (container, "choices") == 0)
1409 if (strcmp (element_name, "choice") == 0)
1412 if (COLLECT (STRING, "value", &value))
1413 key_state_add_choice (state->key_state, value, error);
1419 /* children of <aliases> {{{3 */
1420 else if (strcmp (container, "aliases") == 0)
1422 if (strcmp (element_name, "alias") == 0)
1424 const gchar *value, *target;
1425 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1426 key_state_add_alias (state->key_state, value, target, error);
1432 /* children of <enum> {{{3 */
1433 else if (strcmp (container, "enum") == 0 ||
1434 strcmp (container, "flags") == 0)
1436 if (strcmp (element_name, "value") == 0)
1438 const gchar *nick, *valuestr;
1439 if (COLLECT (STRING, "nick", &nick,
1440 STRING, "value", &valuestr))
1441 enum_state_add_value (state->enum_state, nick, valuestr, error);
1448 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1449 _("Element <%s> not allowed inside <%s>"),
1450 element_name, container);
1452 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1453 _("Element <%s> not allowed at toplevel"), element_name);
1456 /* End element {{{2 */
1459 key_state_end (KeyState **state_ptr,
1467 if (state->default_value == NULL)
1469 g_set_error_literal (error,
1470 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1471 "element <default> is required in <key>");
1477 schema_state_end (SchemaState **state_ptr,
1484 end_element (GMarkupParseContext *context,
1485 const gchar *element_name,
1489 ParseState *state = user_data;
1491 if (strcmp (element_name, "schemalist") == 0)
1493 g_free (state->schemalist_domain);
1494 state->schemalist_domain = NULL;
1497 else if (strcmp (element_name, "enum") == 0 ||
1498 strcmp (element_name, "flags") == 0)
1499 enum_state_end (&state->enum_state, error);
1501 else if (strcmp (element_name, "schema") == 0)
1502 schema_state_end (&state->schema_state, error);
1504 else if (strcmp (element_name, "override") == 0)
1505 override_state_end (&state->key_state, &state->string, error);
1507 else if (strcmp (element_name, "key") == 0)
1508 key_state_end (&state->key_state, error);
1510 else if (strcmp (element_name, "default") == 0)
1511 key_state_end_default (state->key_state, &state->string, error);
1513 else if (strcmp (element_name, "choices") == 0)
1514 key_state_end_choices (state->key_state, error);
1516 else if (strcmp (element_name, "aliases") == 0)
1517 key_state_end_aliases (state->key_state, error);
1521 g_string_free (state->string, TRUE);
1522 state->string = NULL;
1527 text (GMarkupParseContext *context,
1533 ParseState *state = user_data;
1537 /* we are expecting a string, so store the text data.
1539 * we store the data verbatim here and deal with whitespace
1540 * later on. there are two reasons for that:
1542 * 1) whitespace is handled differently depending on the tag
1545 * 2) we could do leading whitespace removal by refusing to
1546 * insert it into state->string if it's at the start, but for
1547 * trailing whitespace, we have no idea if there is another
1548 * text() call coming or not.
1550 g_string_append_len (state->string, text, text_len);
1554 /* string is not expected: accept (and ignore) pure whitespace */
1557 for (i = 0; i < text_len; i++)
1558 if (!g_ascii_isspace (text[i]))
1560 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1561 _("text may not appear inside <%s>"),
1562 g_markup_parse_context_get_element (context));
1568 /* Write to GVDB {{{1 */
1576 gvdb_pair_init (GvdbPair *pair)
1578 pair->table = gvdb_hash_table_new (NULL, NULL);
1579 pair->root = gvdb_hash_table_insert (pair->table, "");
1584 GHashTable *schema_table;
1590 GHashTable *schema_table;
1596 output_key (gpointer key,
1600 OutputSchemaData *data;
1609 item = gvdb_hash_table_insert (data->pair.table, name);
1610 gvdb_item_set_parent (item, data->pair.root);
1611 gvdb_item_set_value (item, key_state_serialise (state));
1616 if (state->child_schema &&
1617 !g_hash_table_lookup (data->schema_table, state->child_schema))
1618 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1619 state->child_schema);
1623 output_schema (gpointer key,
1627 WriteToFileData *wtf_data = user_data;
1628 OutputSchemaData data;
1629 GvdbPair *root_pair;
1636 root_pair = &wtf_data->root_pair;
1638 data.schema_table = wtf_data->schema_table;
1639 gvdb_pair_init (&data.pair);
1642 item = gvdb_hash_table_insert (root_pair->table, id);
1643 gvdb_item_set_parent (item, root_pair->root);
1644 gvdb_item_set_hash_table (item, data.pair.table);
1646 g_hash_table_foreach (state->keys, output_key, &data);
1649 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1651 if (state->extends_name)
1652 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1653 state->extends_name);
1656 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1660 gvdb_hash_table_insert_string (data.pair.table,
1662 state->gettext_domain);
1666 write_to_file (GHashTable *schema_table,
1667 const gchar *filename,
1670 WriteToFileData data;
1673 data.schema_table = schema_table;
1675 gvdb_pair_init (&data.root_pair);
1677 g_hash_table_foreach (schema_table, output_schema, &data);
1679 success = gvdb_table_write_contents (data.root_pair.table, filename,
1680 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1682 g_hash_table_unref (data.root_pair.table);
1687 /* Parser driver {{{1 */
1689 parse_gschema_files (gchar **files,
1692 GMarkupParser parser = { start_element, end_element, text };
1693 ParseState state = { 0, };
1694 const gchar *filename;
1695 GError *error = NULL;
1697 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1698 g_free, enum_state_free);
1700 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1701 g_free, enum_state_free);
1703 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1704 g_free, schema_state_free);
1706 while ((filename = *files++) != NULL)
1708 GMarkupParseContext *context;
1712 if (!g_file_get_contents (filename, &contents, &size, &error))
1714 fprintf (stderr, "%s\n", error->message);
1715 g_clear_error (&error);
1719 context = g_markup_parse_context_new (&parser,
1720 G_MARKUP_TREAT_CDATA_AS_TEXT |
1721 G_MARKUP_PREFIX_ERROR_POSITION,
1725 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1726 !g_markup_parse_context_end_parse (context, &error))
1730 /* back out any changes from this file */
1731 for (item = state.this_file_schemas; item; item = item->next)
1732 g_hash_table_remove (state.schema_table, item->data);
1734 for (item = state.this_file_flagss; item; item = item->next)
1735 g_hash_table_remove (state.flags_table, item->data);
1737 for (item = state.this_file_enums; item; item = item->next)
1738 g_hash_table_remove (state.enum_table, item->data);
1741 fprintf (stderr, "%s: %s. ", filename, error->message);
1742 g_clear_error (&error);
1746 /* Translators: Do not translate "--strict". */
1747 fprintf (stderr, _("--strict was specified; exiting.\n"));
1748 g_hash_table_unref (state.schema_table);
1749 g_hash_table_unref (state.flags_table);
1750 g_hash_table_unref (state.enum_table);
1755 fprintf (stderr, _("This entire file has been ignored.\n"));
1759 g_markup_parse_context_free (context);
1760 g_slist_free (state.this_file_schemas);
1761 g_slist_free (state.this_file_flagss);
1762 g_slist_free (state.this_file_enums);
1763 state.this_file_schemas = NULL;
1764 state.this_file_flagss = NULL;
1765 state.this_file_enums = NULL;
1768 g_hash_table_unref (state.flags_table);
1769 g_hash_table_unref (state.enum_table);
1771 return state.schema_table;
1775 compare_strings (gconstpointer a,
1778 gchar *one = *(gchar **) a;
1779 gchar *two = *(gchar **) b;
1782 cmp = g_str_has_suffix (two, ".enums.xml") -
1783 g_str_has_suffix (one, ".enums.xml");
1786 cmp = strcmp (one, two);
1792 set_overrides (GHashTable *schema_table,
1796 const gchar *filename;
1797 GError *error = NULL;
1799 while ((filename = *files++))
1805 key_file = g_key_file_new ();
1806 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1808 fprintf (stderr, "%s: %s. ", filename, error->message);
1809 g_key_file_free (key_file);
1810 g_clear_error (&error);
1814 fprintf (stderr, _("Ignoring this file.\n"));
1818 fprintf (stderr, _("--strict was specified; exiting.\n"));
1822 groups = g_key_file_get_groups (key_file, NULL);
1824 for (i = 0; groups[i]; i++)
1826 const gchar *group = groups[i];
1827 SchemaState *schema;
1831 schema = g_hash_table_lookup (schema_table, group);
1834 /* Having the schema not be installed is expected to be a
1835 * common case. Don't even emit an error message about
1840 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1841 g_assert (keys != NULL);
1843 for (j = 0; keys[j]; j++)
1845 const gchar *key = keys[j];
1850 state = g_hash_table_lookup (schema->keys, key);
1854 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1855 "specified in override file `%s'"),
1856 key, group, filename);
1860 fprintf (stderr, _("; ignoring override for this key.\n"));
1864 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1865 g_key_file_free (key_file);
1866 g_strfreev (groups);
1872 string = g_key_file_get_value (key_file, group, key, NULL);
1873 g_assert (string != NULL);
1875 value = g_variant_parse (state->type, string,
1876 NULL, NULL, &error);
1880 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1881 "as specified in override file `%s': "
1883 key, group, filename, error->message);
1885 g_clear_error (&error);
1890 fprintf (stderr, _("Ignoring override for this key.\n"));
1894 fprintf (stderr, _("--strict was specified; exiting.\n"));
1895 g_key_file_free (key_file);
1896 g_strfreev (groups);
1904 if (g_variant_compare (value, state->minimum) < 0 ||
1905 g_variant_compare (value, state->maximum) > 0)
1908 _("override for key `%s' in schema `%s' in "
1909 "override file `%s' is out of the range "
1910 "given in the schema"),
1911 key, group, filename);
1913 g_variant_unref (value);
1918 fprintf (stderr, _("; ignoring override for this key.\n"));
1922 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1923 g_key_file_free (key_file);
1924 g_strfreev (groups);
1931 else if (state->strinfo->len)
1933 if (!is_valid_choices (value, state->strinfo))
1936 _("override for key `%s' in schema `%s' in "
1937 "override file `%s' is not in the list "
1938 "of valid choices"),
1939 key, group, filename);
1941 g_variant_unref (value);
1946 fprintf (stderr, _("; ignoring override for this key.\n"));
1950 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1951 g_key_file_free (key_file);
1952 g_strfreev (groups);
1959 g_variant_unref (state->default_value);
1960 state->default_value = value;
1967 g_strfreev (groups);
1974 main (int argc, char **argv)
1981 gchar *targetdir = NULL;
1983 gboolean dry_run = FALSE;
1984 gboolean strict = FALSE;
1985 gchar **schema_files = NULL;
1986 gchar **override_files = NULL;
1987 GOptionContext *context;
1988 GOptionEntry entries[] = {
1989 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1990 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1991 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1992 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1994 /* These options are only for use in the gschema-compile tests */
1995 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2000 extern gchar *_glib_get_locale_dir (void);
2004 setlocale (LC_ALL, "");
2005 textdomain (GETTEXT_PACKAGE);
2008 tmp = _glib_get_locale_dir ();
2009 bindtextdomain (GETTEXT_PACKAGE, tmp);
2012 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2015 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2016 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2019 context = g_option_context_new (N_("DIRECTORY"));
2020 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2021 g_option_context_set_summary (context,
2022 N_("Compile all GSettings schema files into a schema cache.\n"
2023 "Schema files are required to have the extension .gschema.xml,\n"
2024 "and the cache file is called gschemas.compiled."));
2025 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2028 if (!g_option_context_parse (context, &argc, &argv, &error))
2030 fprintf (stderr, "%s\n", error->message);
2034 g_option_context_free (context);
2036 if (!schema_files && argc != 2)
2038 fprintf (stderr, _("You should give exactly one directory name\n"));
2044 if (targetdir == NULL)
2047 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2051 GPtrArray *overrides;
2054 files = g_ptr_array_new ();
2055 overrides = g_ptr_array_new ();
2057 dir = g_dir_open (srcdir, 0, &error);
2060 fprintf (stderr, "%s\n", error->message);
2064 while ((file = g_dir_read_name (dir)) != NULL)
2066 if (g_str_has_suffix (file, ".gschema.xml") ||
2067 g_str_has_suffix (file, ".enums.xml"))
2068 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2070 else if (g_str_has_suffix (file, ".gschema.override"))
2071 g_ptr_array_add (overrides,
2072 g_build_filename (srcdir, file, NULL));
2075 if (files->len == 0)
2077 fprintf (stdout, _("No schema files found: "));
2079 if (g_unlink (target))
2080 fprintf (stdout, _("doing nothing.\n"));
2083 fprintf (stdout, _("removed existing output file.\n"));
2087 g_ptr_array_sort (files, compare_strings);
2088 g_ptr_array_add (files, NULL);
2090 g_ptr_array_sort (overrides, compare_strings);
2091 g_ptr_array_add (overrides, NULL);
2093 schema_files = (char **) g_ptr_array_free (files, FALSE);
2094 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2097 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2103 if (override_files != NULL &&
2104 !set_overrides (table, override_files, strict))
2110 if (!dry_run && !write_to_file (table, target, &error))
2112 fprintf (stderr, "%s\n", error->message);
2124 /* vim:set foldmethod=marker: */