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>
37 #include "gvdb/gvdb-builder.h"
40 /* Handling of <enum> {{{1 */
49 enum_state_free (gpointer data)
51 EnumState *state = data;
53 g_string_free (state->strinfo, TRUE);
54 g_slice_free (EnumState, state);
58 enum_state_new (gboolean is_flags)
62 state = g_slice_new (EnumState);
63 state->strinfo = g_string_new (NULL);
64 state->is_flags = is_flags;
70 enum_state_add_value (EnumState *state,
72 const gchar *valuestr,
78 if (nick[0] == '\0' || nick[1] == '\0')
80 g_set_error (error, G_MARKUP_ERROR,
81 G_MARKUP_ERROR_INVALID_CONTENT,
82 "nick must be a minimum of 2 characters");
86 value = g_ascii_strtoll (valuestr, &end, 0);
87 if (*end || state->is_flags ?
88 (value > G_MAXUINT32 || value < 0) :
89 (value > G_MAXINT32 || value < G_MININT32))
91 g_set_error (error, G_MARKUP_ERROR,
92 G_MARKUP_ERROR_INVALID_CONTENT,
93 "invalid numeric value");
97 if (strinfo_builder_contains (state->strinfo, nick))
99 g_set_error (error, G_MARKUP_ERROR,
100 G_MARKUP_ERROR_INVALID_CONTENT,
101 "<value nick='%s'/> already specified", nick);
105 if (strinfo_builder_contains_value (state->strinfo, value))
107 g_set_error (error, G_MARKUP_ERROR,
108 G_MARKUP_ERROR_INVALID_CONTENT,
109 "value='%s' already specified", valuestr);
113 /* Silently drop the null case if it is mentioned.
114 * It is properly denoted with an empty array.
116 if (state->is_flags && value == 0)
119 if (state->is_flags && (value & (value - 1)))
121 g_set_error (error, G_MARKUP_ERROR,
122 G_MARKUP_ERROR_INVALID_CONTENT,
123 "flags values must have at most 1 bit set");
127 /* Since we reject exact duplicates of value='' and we only allow one
128 * bit to be set, it's not possible to have overlaps.
130 * If we loosen the one-bit-set restriction we need an overlap check.
133 strinfo_builder_append_item (state->strinfo, nick, value);
137 enum_state_end (EnumState **state_ptr,
145 if (state->strinfo->len == 0)
147 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
148 "<%s> must contain at least one <value>",
149 state->is_flags ? "flags" : "enum");
152 /* Handling of <key> {{{1 */
155 /* for <child>, @child_schema will be set.
156 * for <key>, everything else will be set.
162 gboolean have_gettext_domain;
166 GString *unparsed_default_value;
167 GVariant *default_value;
176 gboolean has_choices;
177 gboolean has_aliases;
178 gboolean is_override;
181 GVariant *serialised;
185 key_state_new (const gchar *type_string,
186 const gchar *gettext_domain,
193 state = g_slice_new0 (KeyState);
194 state->type = g_variant_type_new (type_string);
195 state->have_gettext_domain = gettext_domain != NULL;
196 state->is_enum = is_enum;
197 state->is_flags = is_flags;
200 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
202 state->strinfo = g_string_new (NULL);
208 key_state_override (KeyState *state,
209 const gchar *gettext_domain)
213 copy = g_slice_new0 (KeyState);
214 copy->type = g_variant_type_copy (state->type);
215 copy->have_gettext_domain = gettext_domain != NULL;
216 copy->strinfo = g_string_new_len (state->strinfo->str,
217 state->strinfo->len);
218 copy->is_enum = state->is_enum;
219 copy->is_flags = state->is_flags;
220 copy->is_override = TRUE;
224 copy->minimum = g_variant_ref (state->minimum);
225 copy->maximum = g_variant_ref (state->maximum);
232 key_state_new_child (const gchar *child_schema)
236 state = g_slice_new0 (KeyState);
237 state->child_schema = g_strdup (child_schema);
243 is_valid_choices (GVariant *variant,
246 switch (g_variant_classify (variant))
248 case G_VARIANT_CLASS_MAYBE:
249 case G_VARIANT_CLASS_ARRAY:
251 gboolean valid = TRUE;
254 g_variant_iter_init (&iter, variant);
256 while (valid && (variant = g_variant_iter_next_value (&iter)))
258 valid = is_valid_choices (variant, strinfo);
259 g_variant_unref (variant);
265 case G_VARIANT_CLASS_STRING:
266 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
268 g_variant_get_string (variant, NULL));
271 g_assert_not_reached ();
276 /* Gets called at </default> </choices> or <range/> to check for
277 * validity of the default value so that any inconsistency is
278 * reported as soon as it is encountered.
281 key_state_check_range (KeyState *state,
284 if (state->default_value)
288 tag = state->is_override ? "override" : "default";
292 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
293 g_variant_compare (state->default_value, state->maximum) > 0)
295 g_set_error (error, G_MARKUP_ERROR,
296 G_MARKUP_ERROR_INVALID_CONTENT,
297 "<%s> is not contained in "
298 "the specified range", tag);
302 else if (state->strinfo->len)
304 if (!is_valid_choices (state->default_value, state->strinfo))
307 g_set_error (error, G_MARKUP_ERROR,
308 G_MARKUP_ERROR_INVALID_CONTENT,
309 "<%s> is not a valid member of "
310 "the specified enumerated type", tag);
312 else if (state->is_flags)
313 g_set_error (error, G_MARKUP_ERROR,
314 G_MARKUP_ERROR_INVALID_CONTENT,
315 "<%s> contains string not in the "
316 "specified flags type", tag);
319 g_set_error (error, G_MARKUP_ERROR,
320 G_MARKUP_ERROR_INVALID_CONTENT,
321 "<%s> contains string not in "
329 key_state_set_range (KeyState *state,
330 const gchar *min_str,
331 const gchar *max_str,
340 { 'n', "-32768", "32767" },
341 { 'q', "0", "65535" },
342 { 'i', "-2147483648", "2147483647" },
343 { 'u', "0", "4294967295" },
344 { 'x', "-9223372036854775808", "9223372036854775807" },
345 { 't', "0", "18446744073709551615" },
346 { 'd', "-inf", "inf" },
348 gboolean type_ok = FALSE;
353 g_set_error_literal (error, G_MARKUP_ERROR,
354 G_MARKUP_ERROR_INVALID_CONTENT,
355 "<range/> already specified for this key");
359 for (i = 0; i < G_N_ELEMENTS (table); i++)
360 if (*(char *) state->type == table[i].type)
362 min_str = min_str ? min_str : table[i].min;
363 max_str = max_str ? max_str : table[i].max;
370 gchar *type = g_variant_type_dup_string (state->type);
371 g_set_error (error, G_MARKUP_ERROR,
372 G_MARKUP_ERROR_INVALID_CONTENT,
373 "<range> not allowed for keys of type '%s'", type);
378 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
379 if (state->minimum == NULL)
382 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
383 if (state->maximum == NULL)
386 if (g_variant_compare (state->minimum, state->maximum) > 0)
388 g_set_error (error, G_MARKUP_ERROR,
389 G_MARKUP_ERROR_INVALID_CONTENT,
390 "<range> specified minimum is greater than maxmimum");
394 key_state_check_range (state, error);
398 key_state_start_default (KeyState *state,
400 const gchar *context,
405 if (strcmp (l10n, "messages") == 0)
408 else if (strcmp (l10n, "time") == 0)
413 g_set_error (error, G_MARKUP_ERROR,
414 G_MARKUP_ERROR_INVALID_CONTENT,
415 "unsupported l10n category: %s", l10n);
419 if (!state->have_gettext_domain)
421 g_set_error_literal (error, G_MARKUP_ERROR,
422 G_MARKUP_ERROR_INVALID_CONTENT,
423 "l10n requested, but no "
424 "gettext domain given");
428 state->l10n_context = g_strdup (context);
431 else if (context != NULL)
433 g_set_error_literal (error, G_MARKUP_ERROR,
434 G_MARKUP_ERROR_INVALID_CONTENT,
435 "translation context given for "
436 " value without l10n enabled");
440 return g_string_new (NULL);
444 key_state_end_default (KeyState *state,
448 state->unparsed_default_value = *string;
451 state->default_value = g_variant_parse (state->type,
452 state->unparsed_default_value->str,
454 key_state_check_range (state, error);
458 key_state_start_choices (KeyState *state,
461 const GVariantType *type = state->type;
465 g_set_error_literal (error, G_MARKUP_ERROR,
466 G_MARKUP_ERROR_INVALID_CONTENT,
467 "<choices> can not be specified for keys "
468 "tagged as having an enumerated type");
472 if (state->has_choices)
474 g_set_error_literal (error, G_MARKUP_ERROR,
475 G_MARKUP_ERROR_INVALID_CONTENT,
476 "<choices> already specified for this key");
480 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
481 type = g_variant_type_element (type);
483 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
485 gchar *type_string = g_variant_type_dup_string (state->type);
486 g_set_error (error, G_MARKUP_ERROR,
487 G_MARKUP_ERROR_INVALID_CONTENT,
488 "<choices> not allowed for keys of type '%s'",
490 g_free (type_string);
496 key_state_add_choice (KeyState *state,
500 if (strinfo_builder_contains (state->strinfo, choice))
502 g_set_error (error, G_MARKUP_ERROR,
503 G_MARKUP_ERROR_INVALID_CONTENT,
504 "<choice value='%s'/> already given", choice);
508 strinfo_builder_append_item (state->strinfo, choice, 0);
509 state->has_choices = TRUE;
513 key_state_end_choices (KeyState *state,
516 if (!state->has_choices)
518 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
519 "<choices> must contain at least one <choice>");
523 key_state_check_range (state, error);
527 key_state_start_aliases (KeyState *state,
530 if (state->has_aliases)
531 g_set_error_literal (error, G_MARKUP_ERROR,
532 G_MARKUP_ERROR_INVALID_CONTENT,
533 "<aliases> already specified for this key");
534 else if (!state->is_flags && !state->is_enum && !state->has_choices)
535 g_set_error_literal (error, G_MARKUP_ERROR,
536 G_MARKUP_ERROR_INVALID_CONTENT,
537 "<aliases> can only be specified for keys with "
538 "enumerated or flags types or after <choices>");
542 key_state_add_alias (KeyState *state,
547 if (strinfo_builder_contains (state->strinfo, alias))
549 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
550 state->strinfo->len / 4,
554 g_set_error (error, G_MARKUP_ERROR,
555 G_MARKUP_ERROR_INVALID_CONTENT,
556 "<alias value='%s'/> given when '%s' is already "
557 "a member of the enumerated type", alias, alias);
560 g_set_error (error, G_MARKUP_ERROR,
561 G_MARKUP_ERROR_INVALID_CONTENT,
562 "<alias value='%s'/> given when "
563 "<choice value='%s'/> was already given",
568 g_set_error (error, G_MARKUP_ERROR,
569 G_MARKUP_ERROR_INVALID_CONTENT,
570 "<alias value='%s'/> already specified", alias);
575 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
577 g_set_error (error, G_MARKUP_ERROR,
578 G_MARKUP_ERROR_INVALID_CONTENT,
579 "alias target '%s' is not in %s", target,
580 state->is_enum ? "enumerated type" : "<choices>");
584 state->has_aliases = TRUE;
588 key_state_end_aliases (KeyState *state,
591 if (!state->has_aliases)
593 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
594 "<aliases> must contain at least one <alias>");
600 key_state_check (KeyState *state,
606 return state->checked = TRUE;
610 key_state_serialise (KeyState *state)
612 if (state->serialised == NULL)
614 if (state->child_schema)
616 state->serialised = g_variant_new_string (state->child_schema);
621 GVariantBuilder builder;
623 g_assert (key_state_check (state, NULL));
625 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
628 g_variant_builder_add_value (&builder, state->default_value);
633 if (state->l10n_context)
637 /* Contextified messages are supported by prepending
638 * the context, followed by '\004' to the start of the
639 * message string. We do that here to save GSettings
642 len = strlen (state->l10n_context);
643 state->l10n_context[len] = '\004';
644 g_string_prepend_len (state->unparsed_default_value,
645 state->l10n_context, len + 1);
646 g_free (state->l10n_context);
647 state->l10n_context = NULL;
650 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
651 state->unparsed_default_value->str);
652 g_string_free (state->unparsed_default_value, TRUE);
653 state->unparsed_default_value = NULL;
656 /* choice, aliases, enums */
657 if (state->strinfo->len)
665 data = state->strinfo->str;
666 size = state->strinfo->len;
669 for (i = 0; i < size / sizeof (guint32); i++)
670 words[i] = GUINT32_TO_LE (words[i]);
672 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
676 g_string_free (state->strinfo, FALSE);
677 state->strinfo = NULL;
679 g_variant_builder_add (&builder, "(y@au)",
680 state->is_flags ? 'f' :
681 state->is_enum ? 'e' : 'c',
686 if (state->minimum || state->maximum)
687 g_variant_builder_add (&builder, "(y(**))", 'r',
688 state->minimum, state->maximum);
690 state->serialised = g_variant_builder_end (&builder);
693 g_variant_ref_sink (state->serialised);
696 return g_variant_ref (state->serialised);
700 key_state_free (gpointer data)
702 KeyState *state = data;
705 g_variant_type_free (state->type);
707 g_free (state->l10n_context);
709 if (state->unparsed_default_value)
710 g_string_free (state->unparsed_default_value, TRUE);
712 if (state->default_value)
713 g_variant_unref (state->default_value);
716 g_string_free (state->strinfo, TRUE);
719 g_variant_unref (state->minimum);
722 g_variant_unref (state->maximum);
724 if (state->serialised)
725 g_variant_unref (state->serialised);
727 g_slice_free (KeyState, state);
730 /* Key name validity {{{1 */
731 static gboolean allow_any_name = FALSE;
734 is_valid_keyname (const gchar *key,
741 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
742 _("empty names are not permitted"));
749 if (!g_ascii_islower (key[0]))
751 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
752 _("invalid name '%s': names must begin "
753 "with a lowercase letter"), key);
757 for (i = 1; key[i]; i++)
760 !g_ascii_islower (key[i]) &&
761 !g_ascii_isdigit (key[i]))
763 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
764 _("invalid name '%s': invalid character '%c'; "
765 "only lowercase letters, numbers and dash ('-') "
766 "are permitted."), key, key[i]);
770 if (key[i] == '-' && key[i + 1] == '-')
772 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
773 _("invalid name '%s': two successive dashes ('--') "
774 "are not permitted."), key);
779 if (key[i - 1] == '-')
781 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
782 _("invalid name '%s': the last character may not be a "
783 "dash ('-')."), key);
789 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
790 _("invalid name '%s': maximum length is 32"), key);
797 /* Handling of <schema> {{{1 */
798 typedef struct _SchemaState SchemaState;
801 SchemaState *extends;
804 gchar *gettext_domain;
812 schema_state_new (const gchar *path,
813 const gchar *gettext_domain,
814 SchemaState *extends,
815 const gchar *extends_name,
816 const gchar *list_of)
820 state = g_slice_new (SchemaState);
821 state->path = g_strdup (path);
822 state->gettext_domain = g_strdup (gettext_domain);
823 state->extends = extends;
824 state->extends_name = g_strdup (extends_name);
825 state->list_of = g_strdup (list_of);
826 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
827 g_free, key_state_free);
833 schema_state_free (gpointer data)
835 SchemaState *state = data;
837 g_free (state->path);
838 g_free (state->gettext_domain);
839 g_hash_table_unref (state->keys);
843 schema_state_add_child (SchemaState *state,
850 if (!is_valid_keyname (name, error))
853 childname = g_strconcat (name, "/", NULL);
855 if (g_hash_table_lookup (state->keys, childname))
857 g_set_error (error, G_MARKUP_ERROR,
858 G_MARKUP_ERROR_INVALID_CONTENT,
859 _("<child name='%s'> already specified"), name);
863 g_hash_table_insert (state->keys, childname,
864 key_state_new_child (schema));
868 schema_state_add_key (SchemaState *state,
869 GHashTable *enum_table,
870 GHashTable *flags_table,
872 const gchar *type_string,
873 const gchar *enum_type,
874 const gchar *flags_type,
883 g_set_error_literal (error, G_MARKUP_ERROR,
884 G_MARKUP_ERROR_INVALID_CONTENT,
885 _("can not add keys to a 'list-of' schema"));
889 if (!is_valid_keyname (name, error))
892 if (g_hash_table_lookup (state->keys, name))
894 g_set_error (error, G_MARKUP_ERROR,
895 G_MARKUP_ERROR_INVALID_CONTENT,
896 _("<key name='%s'> already specified"), name);
900 for (node = state; node; node = node->extends)
905 shadow = g_hash_table_lookup (node->extends->keys, name);
907 /* in case of <key> <override> <key> make sure we report the
908 * location of the original <key>, not the <override>.
910 if (shadow && !shadow->is_override)
912 g_set_error (error, G_MARKUP_ERROR,
913 G_MARKUP_ERROR_INVALID_CONTENT,
914 _("<key name='%s'> shadows <key name='%s'> in "
915 "<schema id='%s'>; use <override> to modify value"),
916 name, name, node->extends_name);
921 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
923 g_set_error (error, G_MARKUP_ERROR,
924 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
925 _("exactly one of 'type', 'enum' or 'flags' must "
926 "be specified as an attribute to <key>"));
930 if (type_string == NULL) /* flags or enums was specified */
932 EnumState *enum_state;
935 enum_state = g_hash_table_lookup (enum_table, enum_type);
937 enum_state = g_hash_table_lookup (flags_table, flags_type);
940 if (enum_state == NULL)
942 g_set_error (error, G_MARKUP_ERROR,
943 G_MARKUP_ERROR_INVALID_CONTENT,
944 _("<%s id='%s'> not (yet) defined."),
945 flags_type ? "flags" : "enum",
946 flags_type ? flags_type : enum_type);
950 type_string = flags_type ? "as" : "s";
951 strinfo = enum_state->strinfo;
955 if (!g_variant_type_string_is_valid (type_string))
957 g_set_error (error, G_MARKUP_ERROR,
958 G_MARKUP_ERROR_INVALID_CONTENT,
959 _("invalid GVariant type string '%s'"), type_string);
966 key = key_state_new (type_string, state->gettext_domain,
967 enum_type != NULL, flags_type != NULL, strinfo);
968 g_hash_table_insert (state->keys, g_strdup (name), key);
974 schema_state_add_override (SchemaState *state,
975 KeyState **key_state,
979 const gchar *context,
985 if (state->extends == NULL)
987 g_set_error_literal (error, G_MARKUP_ERROR,
988 G_MARKUP_ERROR_INVALID_CONTENT,
989 _("<override> given but schema isn't "
990 "extending anything"));
994 for (parent = state->extends; parent; parent = parent->extends)
995 if ((original = g_hash_table_lookup (parent->keys, key)))
998 if (original == NULL)
1000 g_set_error (error, G_MARKUP_ERROR,
1001 G_MARKUP_ERROR_INVALID_CONTENT,
1002 _("no <key name='%s'> to override"), key);
1006 if (g_hash_table_lookup (state->keys, key))
1008 g_set_error (error, G_MARKUP_ERROR,
1009 G_MARKUP_ERROR_INVALID_CONTENT,
1010 _("<override name='%s'> already specified"), key);
1014 *key_state = key_state_override (original, state->gettext_domain);
1015 *string = key_state_start_default (*key_state, l10n, context, error);
1016 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1020 override_state_end (KeyState **key_state,
1024 key_state_end_default (*key_state, string, error);
1028 /* Handling of toplevel state {{{1 */
1031 GHashTable *schema_table; /* string -> SchemaState */
1032 GHashTable *flags_table; /* string -> EnumState */
1033 GHashTable *enum_table; /* string -> EnumState */
1035 GSList *this_file_schemas; /* strings: <schema>s in this file */
1036 GSList *this_file_flagss; /* strings: <flags>s in this file */
1037 GSList *this_file_enums; /* strings: <enum>s in this file */
1039 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1041 SchemaState *schema_state; /* non-NULL when inside <schema> */
1042 KeyState *key_state; /* non-NULL when inside <key> */
1043 EnumState *enum_state; /* non-NULL when inside <enum> */
1045 GString *string; /* non-NULL when accepting text */
1049 is_subclass (const gchar *class_name,
1050 const gchar *possible_parent,
1051 GHashTable *schema_table)
1055 if (strcmp (class_name, possible_parent) == 0)
1058 class = g_hash_table_lookup (schema_table, class_name);
1059 g_assert (class != NULL);
1061 return class->extends_name &&
1062 is_subclass (class->extends_name, possible_parent, schema_table);
1066 parse_state_start_schema (ParseState *state,
1069 const gchar *gettext_domain,
1070 const gchar *extends_name,
1071 const gchar *list_of,
1074 SchemaState *extends;
1077 if (g_hash_table_lookup (state->schema_table, id))
1079 g_set_error (error, G_MARKUP_ERROR,
1080 G_MARKUP_ERROR_INVALID_CONTENT,
1081 _("<schema id='%s'> already specified"), id);
1087 extends = g_hash_table_lookup (state->schema_table, extends_name);
1089 if (extends == NULL)
1091 g_set_error (error, G_MARKUP_ERROR,
1092 G_MARKUP_ERROR_INVALID_CONTENT,
1093 _("<schema id='%s'> extends not yet "
1094 "existing schema '%s'"), id, extends_name);
1105 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1107 g_set_error (error, G_MARKUP_ERROR,
1108 G_MARKUP_ERROR_INVALID_CONTENT,
1109 _("<schema id='%s'> is list of not yet "
1110 "existing schema '%s'"), id, list_of);
1116 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1117 _("Can not be a list of a schema with a path"));
1126 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1127 _("Can not extend a schema with a path"));
1133 if (extends->list_of == NULL)
1135 g_set_error (error, G_MARKUP_ERROR,
1136 G_MARKUP_ERROR_INVALID_CONTENT,
1137 _("<schema id='%s'> is a list, extending "
1138 "<schema id='%s'> which is not a list"),
1143 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1145 g_set_error (error, G_MARKUP_ERROR,
1146 G_MARKUP_ERROR_INVALID_CONTENT,
1147 _("<schema id='%s' list-of='%s'> extends <schema "
1148 "id='%s' list-of='%s'> but '%s' does not "
1149 "extend '%s'"), id, list_of, extends_name,
1150 extends->list_of, list_of, extends->list_of);
1155 /* by default we are a list of the same thing that the schema
1156 * we are extending is a list of (which might be nothing)
1158 list_of = extends->list_of;
1161 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1163 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1164 _("a path, if given, must begin and end with a slash"));
1168 if (path && list_of && !g_str_has_suffix (path, ":/"))
1170 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1171 _("the path of a list must end with ':/'"));
1175 state->schema_state = schema_state_new (path, gettext_domain,
1176 extends, extends_name, list_of);
1178 my_id = g_strdup (id);
1179 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1180 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1184 parse_state_start_enum (ParseState *state,
1189 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1190 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1193 if (g_hash_table_lookup (table, id))
1195 g_set_error (error, G_MARKUP_ERROR,
1196 G_MARKUP_ERROR_INVALID_CONTENT,
1197 _("<%s id='%s'> already specified"),
1198 is_flags ? "flags" : "enum", id);
1202 state->enum_state = enum_state_new (is_flags);
1204 my_id = g_strdup (id);
1205 *list = g_slist_prepend (*list, my_id);
1206 g_hash_table_insert (table, my_id, state->enum_state);
1209 /* GMarkup Parser Functions {{{1 */
1211 /* Start element {{{2 */
1213 start_element (GMarkupParseContext *context,
1214 const gchar *element_name,
1215 const gchar **attribute_names,
1216 const gchar **attribute_values,
1220 ParseState *state = user_data;
1221 const GSList *element_stack;
1222 const gchar *container;
1224 element_stack = g_markup_parse_context_get_element_stack (context);
1225 container = element_stack->next ? element_stack->next->data : NULL;
1227 #define COLLECT(first, ...) \
1228 g_markup_collect_attributes (element_name, \
1229 attribute_names, attribute_values, error, \
1230 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1231 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1232 #define STRDUP G_MARKUP_COLLECT_STRDUP
1233 #define STRING G_MARKUP_COLLECT_STRING
1234 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1236 /* Toplevel items {{{3 */
1237 if (container == NULL)
1239 if (strcmp (element_name, "schemalist") == 0)
1241 COLLECT (OPTIONAL | STRDUP,
1243 &state->schemalist_domain);
1249 /* children of <schemalist> {{{3 */
1250 else if (strcmp (container, "schemalist") == 0)
1252 if (strcmp (element_name, "schema") == 0)
1254 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1255 if (COLLECT (STRING, "id", &id,
1256 OPTIONAL | STRING, "path", &path,
1257 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1258 OPTIONAL | STRING, "extends", &extends,
1259 OPTIONAL | STRING, "list-of", &list_of))
1260 parse_state_start_schema (state, id, path,
1261 gettext_domain ? gettext_domain
1262 : state->schemalist_domain,
1263 extends, list_of, error);
1267 else if (strcmp (element_name, "enum") == 0)
1270 if (COLLECT (STRING, "id", &id))
1271 parse_state_start_enum (state, id, FALSE, error);
1275 else if (strcmp (element_name, "flags") == 0)
1278 if (COLLECT (STRING, "id", &id))
1279 parse_state_start_enum (state, id, TRUE, error);
1285 /* children of <schema> {{{3 */
1286 else if (strcmp (container, "schema") == 0)
1288 if (strcmp (element_name, "key") == 0)
1290 const gchar *name, *type_string, *enum_type, *flags_type;
1292 if (COLLECT (STRING, "name", &name,
1293 OPTIONAL | STRING, "type", &type_string,
1294 OPTIONAL | STRING, "enum", &enum_type,
1295 OPTIONAL | STRING, "flags", &flags_type))
1297 state->key_state = schema_state_add_key (state->schema_state,
1301 enum_type, flags_type,
1305 else if (strcmp (element_name, "child") == 0)
1307 const gchar *name, *schema;
1309 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1310 schema_state_add_child (state->schema_state,
1311 name, schema, error);
1314 else if (strcmp (element_name, "override") == 0)
1316 const gchar *name, *l10n, *context;
1318 if (COLLECT (STRING, "name", &name,
1319 OPTIONAL | STRING, "l10n", &l10n,
1320 OPTIONAL | STRING, "context", &context))
1321 schema_state_add_override (state->schema_state,
1322 &state->key_state, &state->string,
1323 name, l10n, context, error);
1328 /* children of <key> {{{3 */
1329 else if (strcmp (container, "key") == 0)
1331 if (strcmp (element_name, "default") == 0)
1333 const gchar *l10n, *context;
1334 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1335 STRING | OPTIONAL, "context", &context))
1336 state->string = key_state_start_default (state->key_state,
1337 l10n, context, error);
1341 else if (strcmp (element_name, "summary") == 0 ||
1342 strcmp (element_name, "description") == 0)
1345 state->string = g_string_new (NULL);
1349 else if (strcmp (element_name, "range") == 0)
1351 const gchar *min, *max;
1352 if (COLLECT (STRING | OPTIONAL, "min", &min,
1353 STRING | OPTIONAL, "max", &max))
1354 key_state_set_range (state->key_state, min, max, error);
1358 else if (strcmp (element_name, "choices") == 0)
1361 key_state_start_choices (state->key_state, error);
1365 else if (strcmp (element_name, "aliases") == 0)
1368 key_state_start_aliases (state->key_state, error);
1374 /* children of <choices> {{{3 */
1375 else if (strcmp (container, "choices") == 0)
1377 if (strcmp (element_name, "choice") == 0)
1380 if (COLLECT (STRING, "value", &value))
1381 key_state_add_choice (state->key_state, value, error);
1387 /* children of <aliases> {{{3 */
1388 else if (strcmp (container, "aliases") == 0)
1390 if (strcmp (element_name, "alias") == 0)
1392 const gchar *value, *target;
1393 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1394 key_state_add_alias (state->key_state, value, target, error);
1400 /* children of <enum> {{{3 */
1401 else if (strcmp (container, "enum") == 0 ||
1402 strcmp (container, "flags") == 0)
1404 if (strcmp (element_name, "value") == 0)
1406 const gchar *nick, *valuestr;
1407 if (COLLECT (STRING, "nick", &nick,
1408 STRING, "value", &valuestr))
1409 enum_state_add_value (state->enum_state, nick, valuestr, error);
1416 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1417 _("Element <%s> not allowed inside <%s>"),
1418 element_name, container);
1420 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1421 _("Element <%s> not allowed at toplevel"), element_name);
1424 /* End element {{{2 */
1427 key_state_end (KeyState **state_ptr,
1435 if (state->default_value == NULL)
1437 g_set_error_literal (error,
1438 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1439 "element <default> is required in <key>");
1445 schema_state_end (SchemaState **state_ptr,
1452 end_element (GMarkupParseContext *context,
1453 const gchar *element_name,
1457 ParseState *state = user_data;
1459 if (strcmp (element_name, "schemalist") == 0)
1461 g_free (state->schemalist_domain);
1462 state->schemalist_domain = NULL;
1465 else if (strcmp (element_name, "enum") == 0 ||
1466 strcmp (element_name, "flags") == 0)
1467 enum_state_end (&state->enum_state, error);
1469 else if (strcmp (element_name, "schema") == 0)
1470 schema_state_end (&state->schema_state, error);
1472 else if (strcmp (element_name, "override") == 0)
1473 override_state_end (&state->key_state, &state->string, error);
1475 else if (strcmp (element_name, "key") == 0)
1476 key_state_end (&state->key_state, error);
1478 else if (strcmp (element_name, "default") == 0)
1479 key_state_end_default (state->key_state, &state->string, error);
1481 else if (strcmp (element_name, "choices") == 0)
1482 key_state_end_choices (state->key_state, error);
1484 else if (strcmp (element_name, "aliases") == 0)
1485 key_state_end_aliases (state->key_state, error);
1489 g_string_free (state->string, TRUE);
1490 state->string = NULL;
1495 text (GMarkupParseContext *context,
1501 ParseState *state = user_data;
1504 for (i = 0; i < text_len; i++)
1505 if (!g_ascii_isspace (text[i]))
1508 g_string_append_len (state->string, text, text_len);
1511 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1512 _("text may not appear inside <%s>"),
1513 g_markup_parse_context_get_element (context));
1519 /* Write to GVDB {{{1 */
1527 gvdb_pair_init (GvdbPair *pair)
1529 pair->table = gvdb_hash_table_new (NULL, NULL);
1530 pair->root = gvdb_hash_table_insert (pair->table, "");
1535 GHashTable *schema_table;
1541 GHashTable *schema_table;
1547 output_key (gpointer key,
1551 OutputSchemaData *data;
1560 item = gvdb_hash_table_insert (data->pair.table, name);
1561 gvdb_item_set_parent (item, data->pair.root);
1562 gvdb_item_set_value (item, key_state_serialise (state));
1567 if (state->child_schema &&
1568 !g_hash_table_lookup (data->schema_table, state->child_schema))
1569 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1570 state->child_schema);
1574 output_schema (gpointer key,
1578 WriteToFileData *wtf_data = user_data;
1579 OutputSchemaData data;
1580 GvdbPair *root_pair;
1587 root_pair = &wtf_data->root_pair;
1589 data.schema_table = wtf_data->schema_table;
1590 gvdb_pair_init (&data.pair);
1593 item = gvdb_hash_table_insert (root_pair->table, id);
1594 gvdb_item_set_parent (item, root_pair->root);
1595 gvdb_item_set_hash_table (item, data.pair.table);
1597 g_hash_table_foreach (state->keys, output_key, &data);
1600 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1602 if (state->extends_name)
1603 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1604 state->extends_name);
1607 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1608 state->extends_name);
1611 gvdb_hash_table_insert_string (data.pair.table,
1613 state->gettext_domain);
1617 write_to_file (GHashTable *schema_table,
1618 const gchar *filename,
1621 WriteToFileData data;
1624 data.schema_table = schema_table;
1626 gvdb_pair_init (&data.root_pair);
1628 g_hash_table_foreach (schema_table, output_schema, &data);
1630 success = gvdb_table_write_contents (data.root_pair.table, filename,
1631 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1633 g_hash_table_unref (data.root_pair.table);
1638 /* Parser driver {{{1 */
1640 parse_gschema_files (gchar **files,
1643 GMarkupParser parser = { start_element, end_element, text };
1644 ParseState state = { 0, };
1645 const gchar *filename;
1646 GError *error = NULL;
1648 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1649 g_free, enum_state_free);
1651 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1652 g_free, enum_state_free);
1654 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1655 g_free, schema_state_free);
1657 while ((filename = *files++) != NULL)
1659 GMarkupParseContext *context;
1663 if (!g_file_get_contents (filename, &contents, &size, &error))
1665 fprintf (stderr, "%s\n", error->message);
1666 g_clear_error (&error);
1670 context = g_markup_parse_context_new (&parser,
1671 G_MARKUP_PREFIX_ERROR_POSITION,
1675 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1676 !g_markup_parse_context_end_parse (context, &error))
1680 /* back out any changes from this file */
1681 for (item = state.this_file_schemas; item; item = item->next)
1682 g_hash_table_remove (state.schema_table, item->data);
1684 for (item = state.this_file_flagss; item; item = item->next)
1685 g_hash_table_remove (state.flags_table, item->data);
1687 for (item = state.this_file_enums; item; item = item->next)
1688 g_hash_table_remove (state.enum_table, item->data);
1691 fprintf (stderr, "%s: %s. ", filename, error->message);
1692 g_clear_error (&error);
1696 /* Translators: Do not translate "--strict". */
1697 fprintf (stderr, _("--strict was specified; exiting.\n"));
1698 g_hash_table_unref (state.schema_table);
1699 g_hash_table_unref (state.flags_table);
1700 g_hash_table_unref (state.enum_table);
1705 fprintf (stderr, _("This entire file has been ignored.\n"));
1709 g_markup_parse_context_free (context);
1710 g_slist_free (state.this_file_schemas);
1711 g_slist_free (state.this_file_flagss);
1712 g_slist_free (state.this_file_enums);
1713 state.this_file_schemas = NULL;
1714 state.this_file_flagss = NULL;
1715 state.this_file_enums = NULL;
1718 g_hash_table_unref (state.flags_table);
1719 g_hash_table_unref (state.enum_table);
1721 return state.schema_table;
1725 compare_strings (gconstpointer a,
1728 gchar *one = *(gchar **) a;
1729 gchar *two = *(gchar **) b;
1732 cmp = g_str_has_suffix (two, ".enums.xml") -
1733 g_str_has_suffix (one, ".enums.xml");
1736 cmp = strcmp (one, two);
1742 set_overrides (GHashTable *schema_table,
1746 const gchar *filename;
1747 GError *error = NULL;
1749 while ((filename = *files++))
1755 key_file = g_key_file_new ();
1756 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1758 fprintf (stderr, "%s: %s. ", filename, error->message);
1759 g_key_file_free (key_file);
1760 g_clear_error (&error);
1764 fprintf (stderr, _("Ignoring this file.\n"));
1768 fprintf (stderr, _("--strict was specified; exiting.\n"));
1772 groups = g_key_file_get_groups (key_file, NULL);
1774 for (i = 0; groups[i]; i++)
1776 const gchar *group = groups[i];
1777 SchemaState *schema;
1781 schema = g_hash_table_lookup (schema_table, group);
1784 /* Having the schema not be installed is expected to be a
1785 * common case. Don't even emit an error message about
1790 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1791 g_assert (keys != NULL);
1793 for (j = 0; keys[j]; j++)
1795 const gchar *key = keys[j];
1800 state = g_hash_table_lookup (schema->keys, key);
1804 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1805 "specified in override file `%s'"),
1806 key, group, filename);
1810 fprintf (stderr, _("; ignoring override for this key.\n"));
1814 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1815 g_key_file_free (key_file);
1816 g_strfreev (groups);
1822 string = g_key_file_get_value (key_file, group, key, NULL);
1823 g_assert (string != NULL);
1825 value = g_variant_parse (state->type, string,
1826 NULL, NULL, &error);
1830 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1831 "as specified in override file `%s': "
1833 key, group, filename, error->message);
1835 g_clear_error (&error);
1840 fprintf (stderr, _("Ignoring override for this key.\n"));
1844 fprintf (stderr, _("--strict was specified; exiting.\n"));
1845 g_key_file_free (key_file);
1846 g_strfreev (groups);
1854 if (g_variant_compare (value, state->minimum) < 0 ||
1855 g_variant_compare (value, state->maximum) > 0)
1858 _("override for key `%s' in schema `%s' in "
1859 "override file `%s' is out of the range "
1860 "given in the schema"),
1861 key, group, filename);
1863 g_variant_unref (value);
1868 fprintf (stderr, _("; ignoring override for this key.\n"));
1872 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1873 g_key_file_free (key_file);
1874 g_strfreev (groups);
1881 else if (state->strinfo->len)
1883 if (!is_valid_choices (value, state->strinfo))
1886 _("override for key `%s' in schema `%s' in "
1887 "override file `%s' is not in the list "
1888 "of valid choices"),
1889 key, group, filename);
1891 g_variant_unref (value);
1896 fprintf (stderr, _("; ignoring override for this key.\n"));
1900 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1901 g_key_file_free (key_file);
1902 g_strfreev (groups);
1909 g_variant_unref (state->default_value);
1910 state->default_value = value;
1917 g_strfreev (groups);
1924 main (int argc, char **argv)
1931 gchar *targetdir = NULL;
1933 gboolean uninstall = FALSE;
1934 gboolean dry_run = FALSE;
1935 gboolean strict = FALSE;
1936 gchar **schema_files = NULL;
1937 gchar **override_files = NULL;
1938 GOptionContext *context;
1939 GOptionEntry entries[] = {
1940 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1941 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1942 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1943 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1944 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1946 /* These options are only for use in the gschema-compile tests */
1947 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1951 setlocale (LC_ALL, "");
1952 textdomain (GETTEXT_PACKAGE);
1954 extern gchar *_glib_get_locale_dir (void);
1955 gchar *tmp = _glib_get_locale_dir ();
1956 bindtextdomain (GETTEXT_PACKAGE, tmp);
1959 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1962 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1963 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1966 context = g_option_context_new (N_("DIRECTORY"));
1967 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1968 g_option_context_set_summary (context,
1969 N_("Compile all GSettings schema files into a schema cache.\n"
1970 "Schema files are required to have the extension .gschema.xml,\n"
1971 "and the cache file is called gschemas.compiled."));
1972 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1975 if (!g_option_context_parse (context, &argc, &argv, &error))
1977 fprintf (stderr, "%s\n", error->message);
1981 g_option_context_free (context);
1983 if (!schema_files && argc != 2)
1985 fprintf (stderr, _("You should give exactly one directory name\n"));
1991 if (targetdir == NULL)
1994 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1998 GPtrArray *overrides;
2001 files = g_ptr_array_new ();
2002 overrides = g_ptr_array_new ();
2004 dir = g_dir_open (srcdir, 0, &error);
2007 fprintf (stderr, "%s\n", error->message);
2011 while ((file = g_dir_read_name (dir)) != NULL)
2013 if (g_str_has_suffix (file, ".gschema.xml") ||
2014 g_str_has_suffix (file, ".enums.xml"))
2015 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2017 else if (g_str_has_suffix (file, ".gschema.override"))
2018 g_ptr_array_add (overrides,
2019 g_build_filename (srcdir, file, NULL));
2022 if (files->len == 0)
2024 fprintf (stderr, _("No schema files found: "));
2026 if (g_unlink (target))
2027 fprintf (stderr, _("doing nothing.\n"));
2030 fprintf (stderr, _("removed existing output file.\n"));
2034 g_ptr_array_sort (files, compare_strings);
2035 g_ptr_array_add (files, NULL);
2037 g_ptr_array_sort (overrides, compare_strings);
2038 g_ptr_array_add (overrides, NULL);
2040 schema_files = (char **) g_ptr_array_free (files, FALSE);
2041 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2044 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2050 if (override_files != NULL &&
2051 !set_overrides (table, override_files, strict))
2057 if (!dry_run && !write_to_file (table, target, &error))
2059 fprintf (stderr, "%s\n", error->message);
2071 /* vim:set foldmethod=marker: */