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 #include "glib/glib-private.h"
44 strip_string (GString *string)
48 for (i = 0; g_ascii_isspace (string->str[i]); i++);
49 g_string_erase (string, 0, i);
53 /* len > 0, so there must be at least one non-whitespace character */
54 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
55 g_string_truncate (string, i + 1);
59 /* Handling of <enum> {{{1 */
68 enum_state_free (gpointer data)
70 EnumState *state = data;
72 g_string_free (state->strinfo, TRUE);
73 g_slice_free (EnumState, state);
77 enum_state_new (gboolean is_flags)
81 state = g_slice_new (EnumState);
82 state->strinfo = g_string_new (NULL);
83 state->is_flags = is_flags;
89 enum_state_add_value (EnumState *state,
91 const gchar *valuestr,
97 if (nick[0] == '\0' || nick[1] == '\0')
99 g_set_error (error, G_MARKUP_ERROR,
100 G_MARKUP_ERROR_INVALID_CONTENT,
101 "nick must be a minimum of 2 characters");
105 value = g_ascii_strtoll (valuestr, &end, 0);
106 if (*end || state->is_flags ?
107 (value > G_MAXUINT32 || value < 0) :
108 (value > G_MAXINT32 || value < G_MININT32))
110 g_set_error (error, G_MARKUP_ERROR,
111 G_MARKUP_ERROR_INVALID_CONTENT,
112 "invalid numeric value");
116 if (strinfo_builder_contains (state->strinfo, nick))
118 g_set_error (error, G_MARKUP_ERROR,
119 G_MARKUP_ERROR_INVALID_CONTENT,
120 "<value nick='%s'/> already specified", nick);
124 if (strinfo_builder_contains_value (state->strinfo, value))
126 g_set_error (error, G_MARKUP_ERROR,
127 G_MARKUP_ERROR_INVALID_CONTENT,
128 "value='%s' already specified", valuestr);
132 /* Silently drop the null case if it is mentioned.
133 * It is properly denoted with an empty array.
135 if (state->is_flags && value == 0)
138 if (state->is_flags && (value & (value - 1)))
140 g_set_error (error, G_MARKUP_ERROR,
141 G_MARKUP_ERROR_INVALID_CONTENT,
142 "flags values must have at most 1 bit set");
146 /* Since we reject exact duplicates of value='' and we only allow one
147 * bit to be set, it's not possible to have overlaps.
149 * If we loosen the one-bit-set restriction we need an overlap check.
152 strinfo_builder_append_item (state->strinfo, nick, value);
156 enum_state_end (EnumState **state_ptr,
164 if (state->strinfo->len == 0)
166 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
167 "<%s> must contain at least one <value>",
168 state->is_flags ? "flags" : "enum");
171 /* Handling of <key> {{{1 */
174 /* for <child>, @child_schema will be set.
175 * for <key>, everything else will be set.
181 gboolean have_gettext_domain;
185 GString *unparsed_default_value;
186 GVariant *default_value;
195 gboolean has_choices;
196 gboolean has_aliases;
197 gboolean is_override;
200 GVariant *serialised;
204 key_state_new (const gchar *type_string,
205 const gchar *gettext_domain,
212 state = g_slice_new0 (KeyState);
213 state->type = g_variant_type_new (type_string);
214 state->have_gettext_domain = gettext_domain != NULL;
215 state->is_enum = is_enum;
216 state->is_flags = is_flags;
219 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
221 state->strinfo = g_string_new (NULL);
227 key_state_override (KeyState *state,
228 const gchar *gettext_domain)
232 copy = g_slice_new0 (KeyState);
233 copy->type = g_variant_type_copy (state->type);
234 copy->have_gettext_domain = gettext_domain != NULL;
235 copy->strinfo = g_string_new_len (state->strinfo->str,
236 state->strinfo->len);
237 copy->is_enum = state->is_enum;
238 copy->is_flags = state->is_flags;
239 copy->is_override = TRUE;
243 copy->minimum = g_variant_ref (state->minimum);
244 copy->maximum = g_variant_ref (state->maximum);
251 key_state_new_child (const gchar *child_schema)
255 state = g_slice_new0 (KeyState);
256 state->child_schema = g_strdup (child_schema);
262 is_valid_choices (GVariant *variant,
265 switch (g_variant_classify (variant))
267 case G_VARIANT_CLASS_MAYBE:
268 case G_VARIANT_CLASS_ARRAY:
270 gboolean valid = TRUE;
273 g_variant_iter_init (&iter, variant);
275 while (valid && (variant = g_variant_iter_next_value (&iter)))
277 valid = is_valid_choices (variant, strinfo);
278 g_variant_unref (variant);
284 case G_VARIANT_CLASS_STRING:
285 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
287 g_variant_get_string (variant, NULL));
290 g_assert_not_reached ();
295 /* Gets called at </default> </choices> or <range/> to check for
296 * validity of the default value so that any inconsistency is
297 * reported as soon as it is encountered.
300 key_state_check_range (KeyState *state,
303 if (state->default_value)
307 tag = state->is_override ? "override" : "default";
311 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
312 g_variant_compare (state->default_value, state->maximum) > 0)
314 g_set_error (error, G_MARKUP_ERROR,
315 G_MARKUP_ERROR_INVALID_CONTENT,
316 "<%s> is not contained in "
317 "the specified range", tag);
321 else if (state->strinfo->len)
323 if (!is_valid_choices (state->default_value, state->strinfo))
326 g_set_error (error, G_MARKUP_ERROR,
327 G_MARKUP_ERROR_INVALID_CONTENT,
328 "<%s> is not a valid member of "
329 "the specified enumerated type", tag);
331 else if (state->is_flags)
332 g_set_error (error, G_MARKUP_ERROR,
333 G_MARKUP_ERROR_INVALID_CONTENT,
334 "<%s> contains string not in the "
335 "specified flags type", tag);
338 g_set_error (error, G_MARKUP_ERROR,
339 G_MARKUP_ERROR_INVALID_CONTENT,
340 "<%s> contains string not in "
348 key_state_set_range (KeyState *state,
349 const gchar *min_str,
350 const gchar *max_str,
359 { 'n', "-32768", "32767" },
360 { 'q', "0", "65535" },
361 { 'i', "-2147483648", "2147483647" },
362 { 'u', "0", "4294967295" },
363 { 'x', "-9223372036854775808", "9223372036854775807" },
364 { 't', "0", "18446744073709551615" },
365 { 'd', "-inf", "inf" },
367 gboolean type_ok = FALSE;
372 g_set_error_literal (error, G_MARKUP_ERROR,
373 G_MARKUP_ERROR_INVALID_CONTENT,
374 "<range/> already specified for this key");
378 for (i = 0; i < G_N_ELEMENTS (table); i++)
379 if (*(char *) state->type == table[i].type)
381 min_str = min_str ? min_str : table[i].min;
382 max_str = max_str ? max_str : table[i].max;
389 gchar *type = g_variant_type_dup_string (state->type);
390 g_set_error (error, G_MARKUP_ERROR,
391 G_MARKUP_ERROR_INVALID_CONTENT,
392 "<range> not allowed for keys of type '%s'", type);
397 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
398 if (state->minimum == NULL)
401 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
402 if (state->maximum == NULL)
405 if (g_variant_compare (state->minimum, state->maximum) > 0)
407 g_set_error (error, G_MARKUP_ERROR,
408 G_MARKUP_ERROR_INVALID_CONTENT,
409 "<range> specified minimum is greater than maxmimum");
413 key_state_check_range (state, error);
417 key_state_start_default (KeyState *state,
419 const gchar *context,
424 if (strcmp (l10n, "messages") == 0)
427 else if (strcmp (l10n, "time") == 0)
432 g_set_error (error, G_MARKUP_ERROR,
433 G_MARKUP_ERROR_INVALID_CONTENT,
434 "unsupported l10n category: %s", l10n);
438 if (!state->have_gettext_domain)
440 g_set_error_literal (error, G_MARKUP_ERROR,
441 G_MARKUP_ERROR_INVALID_CONTENT,
442 "l10n requested, but no "
443 "gettext domain given");
447 state->l10n_context = g_strdup (context);
450 else if (context != NULL)
452 g_set_error_literal (error, G_MARKUP_ERROR,
453 G_MARKUP_ERROR_INVALID_CONTENT,
454 "translation context given for "
455 " value without l10n enabled");
459 return g_string_new (NULL);
463 key_state_end_default (KeyState *state,
467 state->unparsed_default_value = *string;
470 state->default_value = g_variant_parse (state->type,
471 state->unparsed_default_value->str,
473 key_state_check_range (state, error);
477 key_state_start_choices (KeyState *state,
480 const GVariantType *type = state->type;
484 g_set_error_literal (error, G_MARKUP_ERROR,
485 G_MARKUP_ERROR_INVALID_CONTENT,
486 "<choices> cannot be specified for keys "
487 "tagged as having an enumerated type");
491 if (state->has_choices)
493 g_set_error_literal (error, G_MARKUP_ERROR,
494 G_MARKUP_ERROR_INVALID_CONTENT,
495 "<choices> already specified for this key");
499 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
500 type = g_variant_type_element (type);
502 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
504 gchar *type_string = g_variant_type_dup_string (state->type);
505 g_set_error (error, G_MARKUP_ERROR,
506 G_MARKUP_ERROR_INVALID_CONTENT,
507 "<choices> not allowed for keys of type '%s'",
509 g_free (type_string);
515 key_state_add_choice (KeyState *state,
519 if (strinfo_builder_contains (state->strinfo, choice))
521 g_set_error (error, G_MARKUP_ERROR,
522 G_MARKUP_ERROR_INVALID_CONTENT,
523 "<choice value='%s'/> already given", choice);
527 strinfo_builder_append_item (state->strinfo, choice, 0);
528 state->has_choices = TRUE;
532 key_state_end_choices (KeyState *state,
535 if (!state->has_choices)
537 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
538 "<choices> must contain at least one <choice>");
542 key_state_check_range (state, error);
546 key_state_start_aliases (KeyState *state,
549 if (state->has_aliases)
550 g_set_error_literal (error, G_MARKUP_ERROR,
551 G_MARKUP_ERROR_INVALID_CONTENT,
552 "<aliases> already specified for this key");
553 else if (!state->is_flags && !state->is_enum && !state->has_choices)
554 g_set_error_literal (error, G_MARKUP_ERROR,
555 G_MARKUP_ERROR_INVALID_CONTENT,
556 "<aliases> can only be specified for keys with "
557 "enumerated or flags types or after <choices>");
561 key_state_add_alias (KeyState *state,
566 if (strinfo_builder_contains (state->strinfo, alias))
568 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
569 state->strinfo->len / 4,
573 g_set_error (error, G_MARKUP_ERROR,
574 G_MARKUP_ERROR_INVALID_CONTENT,
575 "<alias value='%s'/> given when '%s' is already "
576 "a member of the enumerated type", alias, alias);
579 g_set_error (error, G_MARKUP_ERROR,
580 G_MARKUP_ERROR_INVALID_CONTENT,
581 "<alias value='%s'/> given when "
582 "<choice value='%s'/> was already given",
587 g_set_error (error, G_MARKUP_ERROR,
588 G_MARKUP_ERROR_INVALID_CONTENT,
589 "<alias value='%s'/> already specified", alias);
594 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
596 g_set_error (error, G_MARKUP_ERROR,
597 G_MARKUP_ERROR_INVALID_CONTENT,
598 "alias target '%s' is not in %s", target,
599 state->is_enum ? "enumerated type" : "<choices>");
603 state->has_aliases = TRUE;
607 key_state_end_aliases (KeyState *state,
610 if (!state->has_aliases)
612 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
613 "<aliases> must contain at least one <alias>");
619 key_state_check (KeyState *state,
625 return state->checked = TRUE;
629 key_state_serialise (KeyState *state)
631 if (state->serialised == NULL)
633 if (state->child_schema)
635 state->serialised = g_variant_new_string (state->child_schema);
640 GVariantBuilder builder;
642 g_assert (key_state_check (state, NULL));
644 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
647 g_variant_builder_add_value (&builder, state->default_value);
652 /* We are going to store the untranslated default for
653 * runtime translation according to the current locale.
654 * We need to strip leading and trailing whitespace from
655 * the string so that it's exactly the same as the one
656 * that ended up in the .po file for translation.
658 * We want to do this so that
660 * <default l10n='messages'>
664 * ends up in the .po file like "['a', 'b', 'c']",
665 * omitting the extra whitespace at the start and end.
667 strip_string (state->unparsed_default_value);
669 if (state->l10n_context)
673 /* Contextified messages are supported by prepending
674 * the context, followed by '\004' to the start of the
675 * message string. We do that here to save GSettings
678 len = strlen (state->l10n_context);
679 state->l10n_context[len] = '\004';
680 g_string_prepend_len (state->unparsed_default_value,
681 state->l10n_context, len + 1);
682 g_free (state->l10n_context);
683 state->l10n_context = NULL;
686 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
687 state->unparsed_default_value->str);
688 g_string_free (state->unparsed_default_value, TRUE);
689 state->unparsed_default_value = NULL;
692 /* choice, aliases, enums */
693 if (state->strinfo->len)
701 data = state->strinfo->str;
702 size = state->strinfo->len;
705 for (i = 0; i < size / sizeof (guint32); i++)
706 words[i] = GUINT32_TO_LE (words[i]);
708 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
712 g_string_free (state->strinfo, FALSE);
713 state->strinfo = NULL;
715 g_variant_builder_add (&builder, "(y@au)",
716 state->is_flags ? 'f' :
717 state->is_enum ? 'e' : 'c',
722 if (state->minimum || state->maximum)
723 g_variant_builder_add (&builder, "(y(**))", 'r',
724 state->minimum, state->maximum);
726 state->serialised = g_variant_builder_end (&builder);
729 g_variant_ref_sink (state->serialised);
732 return g_variant_ref (state->serialised);
736 key_state_free (gpointer data)
738 KeyState *state = data;
741 g_variant_type_free (state->type);
743 g_free (state->l10n_context);
745 if (state->unparsed_default_value)
746 g_string_free (state->unparsed_default_value, TRUE);
748 if (state->default_value)
749 g_variant_unref (state->default_value);
752 g_string_free (state->strinfo, TRUE);
755 g_variant_unref (state->minimum);
758 g_variant_unref (state->maximum);
760 if (state->serialised)
761 g_variant_unref (state->serialised);
763 g_slice_free (KeyState, state);
766 /* Key name validity {{{1 */
767 static gboolean allow_any_name = FALSE;
770 is_valid_keyname (const gchar *key,
777 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
778 _("empty names are not permitted"));
785 if (!g_ascii_islower (key[0]))
787 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
788 _("invalid name '%s': names must begin "
789 "with a lowercase letter"), key);
793 for (i = 1; key[i]; i++)
796 !g_ascii_islower (key[i]) &&
797 !g_ascii_isdigit (key[i]))
799 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
800 _("invalid name '%s': invalid character '%c'; "
801 "only lowercase letters, numbers and hyphen ('-') "
802 "are permitted."), key, key[i]);
806 if (key[i] == '-' && key[i + 1] == '-')
808 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
809 _("invalid name '%s': two successive hyphens ('--') "
810 "are not permitted."), key);
815 if (key[i - 1] == '-')
817 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
818 _("invalid name '%s': the last character may not be a "
819 "hyphen ('-')."), key);
825 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
826 _("invalid name '%s': maximum length is 1024"), key);
833 /* Handling of <schema> {{{1 */
834 typedef struct _SchemaState SchemaState;
837 SchemaState *extends;
840 gchar *gettext_domain;
848 schema_state_new (const gchar *path,
849 const gchar *gettext_domain,
850 SchemaState *extends,
851 const gchar *extends_name,
852 const gchar *list_of)
856 state = g_slice_new (SchemaState);
857 state->path = g_strdup (path);
858 state->gettext_domain = g_strdup (gettext_domain);
859 state->extends = extends;
860 state->extends_name = g_strdup (extends_name);
861 state->list_of = g_strdup (list_of);
862 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
863 g_free, key_state_free);
869 schema_state_free (gpointer data)
871 SchemaState *state = data;
873 g_free (state->path);
874 g_free (state->gettext_domain);
875 g_hash_table_unref (state->keys);
879 schema_state_add_child (SchemaState *state,
886 if (!is_valid_keyname (name, error))
889 childname = g_strconcat (name, "/", NULL);
891 if (g_hash_table_lookup (state->keys, childname))
893 g_set_error (error, G_MARKUP_ERROR,
894 G_MARKUP_ERROR_INVALID_CONTENT,
895 _("<child name='%s'> already specified"), name);
899 g_hash_table_insert (state->keys, childname,
900 key_state_new_child (schema));
904 schema_state_add_key (SchemaState *state,
905 GHashTable *enum_table,
906 GHashTable *flags_table,
908 const gchar *type_string,
909 const gchar *enum_type,
910 const gchar *flags_type,
919 g_set_error_literal (error, G_MARKUP_ERROR,
920 G_MARKUP_ERROR_INVALID_CONTENT,
921 _("cannot add keys to a 'list-of' schema"));
925 if (!is_valid_keyname (name, error))
928 if (g_hash_table_lookup (state->keys, name))
930 g_set_error (error, G_MARKUP_ERROR,
931 G_MARKUP_ERROR_INVALID_CONTENT,
932 _("<key name='%s'> already specified"), name);
936 for (node = state; node; node = node->extends)
941 shadow = g_hash_table_lookup (node->extends->keys, name);
943 /* in case of <key> <override> <key> make sure we report the
944 * location of the original <key>, not the <override>.
946 if (shadow && !shadow->is_override)
948 g_set_error (error, G_MARKUP_ERROR,
949 G_MARKUP_ERROR_INVALID_CONTENT,
950 _("<key name='%s'> shadows <key name='%s'> in "
951 "<schema id='%s'>; use <override> to modify value"),
952 name, name, node->extends_name);
957 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
959 g_set_error (error, G_MARKUP_ERROR,
960 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
961 _("exactly one of 'type', 'enum' or 'flags' must "
962 "be specified as an attribute to <key>"));
966 if (type_string == NULL) /* flags or enums was specified */
968 EnumState *enum_state;
971 enum_state = g_hash_table_lookup (enum_table, enum_type);
973 enum_state = g_hash_table_lookup (flags_table, flags_type);
976 if (enum_state == NULL)
978 g_set_error (error, G_MARKUP_ERROR,
979 G_MARKUP_ERROR_INVALID_CONTENT,
980 _("<%s id='%s'> not (yet) defined."),
981 flags_type ? "flags" : "enum",
982 flags_type ? flags_type : enum_type);
986 type_string = flags_type ? "as" : "s";
987 strinfo = enum_state->strinfo;
991 if (!g_variant_type_string_is_valid (type_string))
993 g_set_error (error, G_MARKUP_ERROR,
994 G_MARKUP_ERROR_INVALID_CONTENT,
995 _("invalid GVariant type string '%s'"), type_string);
1002 key = key_state_new (type_string, state->gettext_domain,
1003 enum_type != NULL, flags_type != NULL, strinfo);
1004 g_hash_table_insert (state->keys, g_strdup (name), key);
1010 schema_state_add_override (SchemaState *state,
1011 KeyState **key_state,
1015 const gchar *context,
1018 SchemaState *parent;
1021 if (state->extends == NULL)
1023 g_set_error_literal (error, G_MARKUP_ERROR,
1024 G_MARKUP_ERROR_INVALID_CONTENT,
1025 _("<override> given but schema isn't "
1026 "extending anything"));
1030 for (parent = state->extends; parent; parent = parent->extends)
1031 if ((original = g_hash_table_lookup (parent->keys, key)))
1034 if (original == NULL)
1036 g_set_error (error, G_MARKUP_ERROR,
1037 G_MARKUP_ERROR_INVALID_CONTENT,
1038 _("no <key name='%s'> to override"), key);
1042 if (g_hash_table_lookup (state->keys, key))
1044 g_set_error (error, G_MARKUP_ERROR,
1045 G_MARKUP_ERROR_INVALID_CONTENT,
1046 _("<override name='%s'> already specified"), key);
1050 *key_state = key_state_override (original, state->gettext_domain);
1051 *string = key_state_start_default (*key_state, l10n, context, error);
1052 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1056 override_state_end (KeyState **key_state,
1060 key_state_end_default (*key_state, string, error);
1064 /* Handling of toplevel state {{{1 */
1067 GHashTable *schema_table; /* string -> SchemaState */
1068 GHashTable *flags_table; /* string -> EnumState */
1069 GHashTable *enum_table; /* string -> EnumState */
1071 GSList *this_file_schemas; /* strings: <schema>s in this file */
1072 GSList *this_file_flagss; /* strings: <flags>s in this file */
1073 GSList *this_file_enums; /* strings: <enum>s in this file */
1075 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1077 SchemaState *schema_state; /* non-NULL when inside <schema> */
1078 KeyState *key_state; /* non-NULL when inside <key> */
1079 EnumState *enum_state; /* non-NULL when inside <enum> */
1081 GString *string; /* non-NULL when accepting text */
1085 is_subclass (const gchar *class_name,
1086 const gchar *possible_parent,
1087 GHashTable *schema_table)
1091 if (strcmp (class_name, possible_parent) == 0)
1094 class = g_hash_table_lookup (schema_table, class_name);
1095 g_assert (class != NULL);
1097 return class->extends_name &&
1098 is_subclass (class->extends_name, possible_parent, schema_table);
1102 parse_state_start_schema (ParseState *state,
1105 const gchar *gettext_domain,
1106 const gchar *extends_name,
1107 const gchar *list_of,
1110 SchemaState *extends;
1113 if (g_hash_table_lookup (state->schema_table, id))
1115 g_set_error (error, G_MARKUP_ERROR,
1116 G_MARKUP_ERROR_INVALID_CONTENT,
1117 _("<schema id='%s'> already specified"), id);
1123 extends = g_hash_table_lookup (state->schema_table, extends_name);
1125 if (extends == NULL)
1127 g_set_error (error, G_MARKUP_ERROR,
1128 G_MARKUP_ERROR_INVALID_CONTENT,
1129 _("<schema id='%s'> extends not yet existing "
1130 "schema '%s'"), id, extends_name);
1141 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1143 g_set_error (error, G_MARKUP_ERROR,
1144 G_MARKUP_ERROR_INVALID_CONTENT,
1145 _("<schema id='%s'> is list of not yet existing "
1146 "schema '%s'"), id, list_of);
1152 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1153 _("Can not be a list of a schema with a path"));
1162 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1163 _("Can not extend a schema with a path"));
1169 if (extends->list_of == NULL)
1171 g_set_error (error, G_MARKUP_ERROR,
1172 G_MARKUP_ERROR_INVALID_CONTENT,
1173 _("<schema id='%s'> is a list, extending "
1174 "<schema id='%s'> which is not a list"),
1179 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1181 g_set_error (error, G_MARKUP_ERROR,
1182 G_MARKUP_ERROR_INVALID_CONTENT,
1183 _("<schema id='%s' list-of='%s'> extends <schema "
1184 "id='%s' list-of='%s'> but '%s' does not "
1185 "extend '%s'"), id, list_of, extends_name,
1186 extends->list_of, list_of, extends->list_of);
1191 /* by default we are a list of the same thing that the schema
1192 * we are extending is a list of (which might be nothing)
1194 list_of = extends->list_of;
1197 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1199 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1200 _("a path, if given, must begin and end with a slash"));
1204 if (path && list_of && !g_str_has_suffix (path, ":/"))
1206 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1207 _("the path of a list must end with ':/'"));
1211 if (path && (g_str_has_prefix (path, "/apps/") ||
1212 g_str_has_prefix (path, "/desktop/") ||
1213 g_str_has_prefix (path, "/system/")))
1214 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1215 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1217 state->schema_state = schema_state_new (path, gettext_domain,
1218 extends, extends_name, list_of);
1220 my_id = g_strdup (id);
1221 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1222 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1226 parse_state_start_enum (ParseState *state,
1231 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1232 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1235 if (g_hash_table_lookup (table, id))
1237 g_set_error (error, G_MARKUP_ERROR,
1238 G_MARKUP_ERROR_INVALID_CONTENT,
1239 _("<%s id='%s'> already specified"),
1240 is_flags ? "flags" : "enum", id);
1244 state->enum_state = enum_state_new (is_flags);
1246 my_id = g_strdup (id);
1247 *list = g_slist_prepend (*list, my_id);
1248 g_hash_table_insert (table, my_id, state->enum_state);
1251 /* GMarkup Parser Functions {{{1 */
1253 /* Start element {{{2 */
1255 start_element (GMarkupParseContext *context,
1256 const gchar *element_name,
1257 const gchar **attribute_names,
1258 const gchar **attribute_values,
1262 ParseState *state = user_data;
1263 const GSList *element_stack;
1264 const gchar *container;
1266 element_stack = g_markup_parse_context_get_element_stack (context);
1267 container = element_stack->next ? element_stack->next->data : NULL;
1269 #define COLLECT(first, ...) \
1270 g_markup_collect_attributes (element_name, \
1271 attribute_names, attribute_values, error, \
1272 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1273 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1274 #define STRDUP G_MARKUP_COLLECT_STRDUP
1275 #define STRING G_MARKUP_COLLECT_STRING
1276 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1278 /* Toplevel items {{{3 */
1279 if (container == NULL)
1281 if (strcmp (element_name, "schemalist") == 0)
1283 COLLECT (OPTIONAL | STRDUP,
1285 &state->schemalist_domain);
1291 /* children of <schemalist> {{{3 */
1292 else if (strcmp (container, "schemalist") == 0)
1294 if (strcmp (element_name, "schema") == 0)
1296 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1297 if (COLLECT (STRING, "id", &id,
1298 OPTIONAL | STRING, "path", &path,
1299 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1300 OPTIONAL | STRING, "extends", &extends,
1301 OPTIONAL | STRING, "list-of", &list_of))
1302 parse_state_start_schema (state, id, path,
1303 gettext_domain ? gettext_domain
1304 : state->schemalist_domain,
1305 extends, list_of, error);
1309 else if (strcmp (element_name, "enum") == 0)
1312 if (COLLECT (STRING, "id", &id))
1313 parse_state_start_enum (state, id, FALSE, error);
1317 else if (strcmp (element_name, "flags") == 0)
1320 if (COLLECT (STRING, "id", &id))
1321 parse_state_start_enum (state, id, TRUE, error);
1327 /* children of <schema> {{{3 */
1328 else if (strcmp (container, "schema") == 0)
1330 if (strcmp (element_name, "key") == 0)
1332 const gchar *name, *type_string, *enum_type, *flags_type;
1334 if (COLLECT (STRING, "name", &name,
1335 OPTIONAL | STRING, "type", &type_string,
1336 OPTIONAL | STRING, "enum", &enum_type,
1337 OPTIONAL | STRING, "flags", &flags_type))
1339 state->key_state = schema_state_add_key (state->schema_state,
1343 enum_type, flags_type,
1347 else if (strcmp (element_name, "child") == 0)
1349 const gchar *name, *schema;
1351 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1352 schema_state_add_child (state->schema_state,
1353 name, schema, error);
1356 else if (strcmp (element_name, "override") == 0)
1358 const gchar *name, *l10n, *context;
1360 if (COLLECT (STRING, "name", &name,
1361 OPTIONAL | STRING, "l10n", &l10n,
1362 OPTIONAL | STRING, "context", &context))
1363 schema_state_add_override (state->schema_state,
1364 &state->key_state, &state->string,
1365 name, l10n, context, error);
1370 /* children of <key> {{{3 */
1371 else if (strcmp (container, "key") == 0)
1373 if (strcmp (element_name, "default") == 0)
1375 const gchar *l10n, *context;
1376 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1377 STRING | OPTIONAL, "context", &context))
1378 state->string = key_state_start_default (state->key_state,
1379 l10n, context, error);
1383 else if (strcmp (element_name, "summary") == 0 ||
1384 strcmp (element_name, "description") == 0)
1387 state->string = g_string_new (NULL);
1391 else if (strcmp (element_name, "range") == 0)
1393 const gchar *min, *max;
1394 if (COLLECT (STRING | OPTIONAL, "min", &min,
1395 STRING | OPTIONAL, "max", &max))
1396 key_state_set_range (state->key_state, min, max, error);
1400 else if (strcmp (element_name, "choices") == 0)
1403 key_state_start_choices (state->key_state, error);
1407 else if (strcmp (element_name, "aliases") == 0)
1410 key_state_start_aliases (state->key_state, error);
1416 /* children of <choices> {{{3 */
1417 else if (strcmp (container, "choices") == 0)
1419 if (strcmp (element_name, "choice") == 0)
1422 if (COLLECT (STRING, "value", &value))
1423 key_state_add_choice (state->key_state, value, error);
1429 /* children of <aliases> {{{3 */
1430 else if (strcmp (container, "aliases") == 0)
1432 if (strcmp (element_name, "alias") == 0)
1434 const gchar *value, *target;
1435 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1436 key_state_add_alias (state->key_state, value, target, error);
1442 /* children of <enum> {{{3 */
1443 else if (strcmp (container, "enum") == 0 ||
1444 strcmp (container, "flags") == 0)
1446 if (strcmp (element_name, "value") == 0)
1448 const gchar *nick, *valuestr;
1449 if (COLLECT (STRING, "nick", &nick,
1450 STRING, "value", &valuestr))
1451 enum_state_add_value (state->enum_state, nick, valuestr, error);
1458 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1459 _("Element <%s> not allowed inside <%s>"),
1460 element_name, container);
1462 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1463 _("Element <%s> not allowed at the top level"), element_name);
1466 /* End element {{{2 */
1469 key_state_end (KeyState **state_ptr,
1477 if (state->default_value == NULL)
1479 g_set_error_literal (error,
1480 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1481 "element <default> is required in <key>");
1487 schema_state_end (SchemaState **state_ptr,
1494 end_element (GMarkupParseContext *context,
1495 const gchar *element_name,
1499 ParseState *state = user_data;
1501 if (strcmp (element_name, "schemalist") == 0)
1503 g_free (state->schemalist_domain);
1504 state->schemalist_domain = NULL;
1507 else if (strcmp (element_name, "enum") == 0 ||
1508 strcmp (element_name, "flags") == 0)
1509 enum_state_end (&state->enum_state, error);
1511 else if (strcmp (element_name, "schema") == 0)
1512 schema_state_end (&state->schema_state, error);
1514 else if (strcmp (element_name, "override") == 0)
1515 override_state_end (&state->key_state, &state->string, error);
1517 else if (strcmp (element_name, "key") == 0)
1518 key_state_end (&state->key_state, error);
1520 else if (strcmp (element_name, "default") == 0)
1521 key_state_end_default (state->key_state, &state->string, error);
1523 else if (strcmp (element_name, "choices") == 0)
1524 key_state_end_choices (state->key_state, error);
1526 else if (strcmp (element_name, "aliases") == 0)
1527 key_state_end_aliases (state->key_state, error);
1531 g_string_free (state->string, TRUE);
1532 state->string = NULL;
1537 text (GMarkupParseContext *context,
1543 ParseState *state = user_data;
1547 /* we are expecting a string, so store the text data.
1549 * we store the data verbatim here and deal with whitespace
1550 * later on. there are two reasons for that:
1552 * 1) whitespace is handled differently depending on the tag
1555 * 2) we could do leading whitespace removal by refusing to
1556 * insert it into state->string if it's at the start, but for
1557 * trailing whitespace, we have no idea if there is another
1558 * text() call coming or not.
1560 g_string_append_len (state->string, text, text_len);
1564 /* string is not expected: accept (and ignore) pure whitespace */
1567 for (i = 0; i < text_len; i++)
1568 if (!g_ascii_isspace (text[i]))
1570 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1571 _("text may not appear inside <%s>"),
1572 g_markup_parse_context_get_element (context));
1578 /* Write to GVDB {{{1 */
1586 gvdb_pair_init (GvdbPair *pair)
1588 pair->table = gvdb_hash_table_new (NULL, NULL);
1589 pair->root = gvdb_hash_table_insert (pair->table, "");
1594 GHashTable *schema_table;
1600 GHashTable *schema_table;
1606 output_key (gpointer key,
1610 OutputSchemaData *data;
1619 item = gvdb_hash_table_insert (data->pair.table, name);
1620 gvdb_item_set_parent (item, data->pair.root);
1621 gvdb_item_set_value (item, key_state_serialise (state));
1626 if (state->child_schema &&
1627 !g_hash_table_lookup (data->schema_table, state->child_schema))
1628 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1629 state->child_schema);
1633 output_schema (gpointer key,
1637 WriteToFileData *wtf_data = user_data;
1638 OutputSchemaData data;
1639 GvdbPair *root_pair;
1646 root_pair = &wtf_data->root_pair;
1648 data.schema_table = wtf_data->schema_table;
1649 gvdb_pair_init (&data.pair);
1652 item = gvdb_hash_table_insert (root_pair->table, id);
1653 gvdb_item_set_parent (item, root_pair->root);
1654 gvdb_item_set_hash_table (item, data.pair.table);
1656 g_hash_table_foreach (state->keys, output_key, &data);
1659 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1661 if (state->extends_name)
1662 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1663 state->extends_name);
1666 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1670 gvdb_hash_table_insert_string (data.pair.table,
1672 state->gettext_domain);
1676 write_to_file (GHashTable *schema_table,
1677 const gchar *filename,
1680 WriteToFileData data;
1683 data.schema_table = schema_table;
1685 gvdb_pair_init (&data.root_pair);
1687 g_hash_table_foreach (schema_table, output_schema, &data);
1689 success = gvdb_table_write_contents (data.root_pair.table, filename,
1690 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1692 g_hash_table_unref (data.root_pair.table);
1697 /* Parser driver {{{1 */
1699 parse_gschema_files (gchar **files,
1702 GMarkupParser parser = { start_element, end_element, text };
1703 ParseState state = { 0, };
1704 const gchar *filename;
1705 GError *error = NULL;
1707 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1708 g_free, enum_state_free);
1710 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1711 g_free, enum_state_free);
1713 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1714 g_free, schema_state_free);
1716 while ((filename = *files++) != NULL)
1718 GMarkupParseContext *context;
1722 if (!g_file_get_contents (filename, &contents, &size, &error))
1724 fprintf (stderr, "%s\n", error->message);
1725 g_clear_error (&error);
1729 context = g_markup_parse_context_new (&parser,
1730 G_MARKUP_TREAT_CDATA_AS_TEXT |
1731 G_MARKUP_PREFIX_ERROR_POSITION |
1732 G_MARKUP_IGNORE_QUALIFIED,
1736 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1737 !g_markup_parse_context_end_parse (context, &error))
1741 /* back out any changes from this file */
1742 for (item = state.this_file_schemas; item; item = item->next)
1743 g_hash_table_remove (state.schema_table, item->data);
1745 for (item = state.this_file_flagss; item; item = item->next)
1746 g_hash_table_remove (state.flags_table, item->data);
1748 for (item = state.this_file_enums; item; item = item->next)
1749 g_hash_table_remove (state.enum_table, item->data);
1752 fprintf (stderr, "%s: %s. ", filename, error->message);
1753 g_clear_error (&error);
1757 /* Translators: Do not translate "--strict". */
1758 fprintf (stderr, _("--strict was specified; exiting.\n"));
1759 g_hash_table_unref (state.schema_table);
1760 g_hash_table_unref (state.flags_table);
1761 g_hash_table_unref (state.enum_table);
1766 fprintf (stderr, _("This entire file has been ignored.\n"));
1770 g_markup_parse_context_free (context);
1771 g_slist_free (state.this_file_schemas);
1772 g_slist_free (state.this_file_flagss);
1773 g_slist_free (state.this_file_enums);
1774 state.this_file_schemas = NULL;
1775 state.this_file_flagss = NULL;
1776 state.this_file_enums = NULL;
1779 g_hash_table_unref (state.flags_table);
1780 g_hash_table_unref (state.enum_table);
1782 return state.schema_table;
1786 compare_strings (gconstpointer a,
1789 gchar *one = *(gchar **) a;
1790 gchar *two = *(gchar **) b;
1793 cmp = g_str_has_suffix (two, ".enums.xml") -
1794 g_str_has_suffix (one, ".enums.xml");
1797 cmp = strcmp (one, two);
1803 set_overrides (GHashTable *schema_table,
1807 const gchar *filename;
1808 GError *error = NULL;
1810 while ((filename = *files++))
1816 key_file = g_key_file_new ();
1817 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1819 fprintf (stderr, "%s: %s. ", filename, error->message);
1820 g_key_file_free (key_file);
1821 g_clear_error (&error);
1825 fprintf (stderr, _("Ignoring this file.\n"));
1829 fprintf (stderr, _("--strict was specified; exiting.\n"));
1833 groups = g_key_file_get_groups (key_file, NULL);
1835 for (i = 0; groups[i]; i++)
1837 const gchar *group = groups[i];
1838 SchemaState *schema;
1842 schema = g_hash_table_lookup (schema_table, group);
1845 /* Having the schema not be installed is expected to be a
1846 * common case. Don't even emit an error message about
1851 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1852 g_assert (keys != NULL);
1854 for (j = 0; keys[j]; j++)
1856 const gchar *key = keys[j];
1861 state = g_hash_table_lookup (schema->keys, key);
1865 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1866 "specified in override file '%s'"),
1867 key, group, filename);
1871 fprintf (stderr, _("; ignoring override for this key.\n"));
1875 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1876 g_key_file_free (key_file);
1877 g_strfreev (groups);
1883 string = g_key_file_get_value (key_file, group, key, NULL);
1884 g_assert (string != NULL);
1886 value = g_variant_parse (state->type, string,
1887 NULL, NULL, &error);
1891 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1892 "as specified in override file '%s': "
1894 key, group, filename, error->message);
1896 g_clear_error (&error);
1901 fprintf (stderr, _("Ignoring override for this key.\n"));
1905 fprintf (stderr, _("--strict was specified; exiting.\n"));
1906 g_key_file_free (key_file);
1907 g_strfreev (groups);
1915 if (g_variant_compare (value, state->minimum) < 0 ||
1916 g_variant_compare (value, state->maximum) > 0)
1919 _("override for key '%s' in schema '%s' in "
1920 "override file '%s' is outside the range "
1921 "given in the schema"),
1922 key, group, filename);
1924 g_variant_unref (value);
1929 fprintf (stderr, _("; ignoring override for this key.\n"));
1933 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1934 g_key_file_free (key_file);
1935 g_strfreev (groups);
1942 else if (state->strinfo->len)
1944 if (!is_valid_choices (value, state->strinfo))
1947 _("override for key '%s' in schema '%s' in "
1948 "override file '%s' is not in the list "
1949 "of valid choices"),
1950 key, group, filename);
1952 g_variant_unref (value);
1957 fprintf (stderr, _("; ignoring override for this key.\n"));
1961 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1962 g_key_file_free (key_file);
1963 g_strfreev (groups);
1970 g_variant_unref (state->default_value);
1971 state->default_value = value;
1978 g_strfreev (groups);
1985 main (int argc, char **argv)
1992 gchar *targetdir = NULL;
1994 gboolean dry_run = FALSE;
1995 gboolean strict = FALSE;
1996 gchar **schema_files = NULL;
1997 gchar **override_files = NULL;
1998 GOptionContext *context;
1999 GOptionEntry entries[] = {
2000 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2001 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2002 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2003 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2005 /* These options are only for use in the gschema-compile tests */
2006 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2014 setlocale (LC_ALL, "");
2015 textdomain (GETTEXT_PACKAGE);
2018 tmp = _glib_get_locale_dir ();
2019 bindtextdomain (GETTEXT_PACKAGE, tmp);
2022 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2025 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2026 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2029 context = g_option_context_new (N_("DIRECTORY"));
2030 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2031 g_option_context_set_summary (context,
2032 N_("Compile all GSettings schema files into a schema cache.\n"
2033 "Schema files are required to have the extension .gschema.xml,\n"
2034 "and the cache file is called gschemas.compiled."));
2035 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2038 if (!g_option_context_parse (context, &argc, &argv, &error))
2040 fprintf (stderr, "%s\n", error->message);
2044 g_option_context_free (context);
2046 if (!schema_files && argc != 2)
2048 fprintf (stderr, _("You should give exactly one directory name\n"));
2054 if (targetdir == NULL)
2057 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2061 GPtrArray *overrides;
2064 files = g_ptr_array_new ();
2065 overrides = g_ptr_array_new ();
2067 dir = g_dir_open (srcdir, 0, &error);
2070 fprintf (stderr, "%s\n", error->message);
2074 while ((file = g_dir_read_name (dir)) != NULL)
2076 if (g_str_has_suffix (file, ".gschema.xml") ||
2077 g_str_has_suffix (file, ".enums.xml"))
2078 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2080 else if (g_str_has_suffix (file, ".gschema.override"))
2081 g_ptr_array_add (overrides,
2082 g_build_filename (srcdir, file, NULL));
2085 if (files->len == 0)
2087 fprintf (stdout, _("No schema files found: "));
2089 if (g_unlink (target))
2090 fprintf (stdout, _("doing nothing.\n"));
2093 fprintf (stdout, _("removed existing output file.\n"));
2097 g_ptr_array_sort (files, compare_strings);
2098 g_ptr_array_add (files, NULL);
2100 g_ptr_array_sort (overrides, compare_strings);
2101 g_ptr_array_add (overrides, NULL);
2103 schema_files = (char **) g_ptr_array_free (files, FALSE);
2104 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2107 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2113 if (override_files != NULL &&
2114 !set_overrides (table, override_files, strict))
2120 if (!dry_run && !write_to_file (table, target, &error))
2122 fprintf (stderr, "%s\n", error->message);
2134 /* vim:set foldmethod=marker: */