2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
34 #include "gvdb/gvdb-builder.h"
37 /* Handling of <enum> {{{1 */
38 typedef GString EnumState;
41 enum_state_free (gpointer data)
43 EnumState *state = data;
45 g_string_free (state, TRUE);
49 enum_state_add_value (EnumState *state,
51 const gchar *valuestr,
57 if (nick[0] == '\0' || nick[1] == '\0')
59 g_set_error (error, G_MARKUP_ERROR,
60 G_MARKUP_ERROR_INVALID_CONTENT,
61 "enum nick must be a minimum of 2 characters");
65 value = g_ascii_strtoll (valuestr, &end, 0);
66 if (*end || value > G_MAXINT32 || value < G_MININT32)
68 g_set_error (error, G_MARKUP_ERROR,
69 G_MARKUP_ERROR_INVALID_CONTENT,
70 "invalid numeric value");
74 if (strinfo_builder_contains (state, nick))
76 g_set_error (error, G_MARKUP_ERROR,
77 G_MARKUP_ERROR_INVALID_CONTENT,
78 "<value nick='%s'> already specified", nick);
82 strinfo_builder_append_item (state, nick, value);
86 enum_state_end (EnumState **state_ptr,
95 g_set_error_literal (error,
96 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
97 "<enum> must contain at least one <value>");
100 /* Handling of <key> {{{1 */
103 /* for <child>, @child_schema will be set.
104 * for <key>, everything else will be set.
110 gboolean have_gettext_domain;
114 GString *unparsed_default_value;
115 GVariant *default_value;
123 gboolean has_choices;
124 gboolean has_aliases;
125 gboolean is_override;
128 GVariant *serialised;
132 key_state_new (const gchar *type_string,
133 const gchar *gettext_domain,
134 EnumState *enum_data)
138 state = g_slice_new0 (KeyState);
139 state->type = g_variant_type_new (type_string);
140 state->have_gettext_domain = gettext_domain != NULL;
142 if ((state->is_enum = (enum_data != NULL)))
143 state->strinfo = g_string_new_len (enum_data->str, enum_data->len);
145 state->strinfo = g_string_new (NULL);
151 key_state_override (KeyState *state,
152 const gchar *gettext_domain)
156 copy = g_slice_new0 (KeyState);
157 copy->type = g_variant_type_copy (state->type);
158 copy->have_gettext_domain = gettext_domain != NULL;
159 copy->strinfo = g_string_new_len (state->strinfo->str,
160 state->strinfo->len);
161 copy->is_enum = state->is_enum;
162 copy->is_override = TRUE;
166 copy->minimum = g_variant_ref (state->minimum);
167 copy->maximum = g_variant_ref (state->maximum);
174 key_state_new_child (const gchar *child_schema)
178 state = g_slice_new0 (KeyState);
179 state->child_schema = g_strdup (child_schema);
185 is_valid_choices (GVariant *variant,
188 switch (g_variant_classify (variant))
190 case G_VARIANT_CLASS_MAYBE:
191 case G_VARIANT_CLASS_ARRAY:
193 gboolean valid = TRUE;
196 g_variant_iter_init (&iter, variant);
198 while (valid && (variant = g_variant_iter_next_value (&iter)))
200 valid = is_valid_choices (variant, strinfo);
201 g_variant_unref (variant);
207 case G_VARIANT_CLASS_STRING:
208 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
210 g_variant_get_string (variant, NULL));
213 g_assert_not_reached ();
218 /* Gets called at </default> </choices> or <range/> to check for
219 * validity of the default value so that any inconsistency is
220 * reported as soon as it is encountered.
223 key_state_check_range (KeyState *state,
226 if (state->default_value)
230 tag = state->is_override ? "override" : "default";
234 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
235 g_variant_compare (state->default_value, state->maximum) > 0)
237 g_set_error (error, G_MARKUP_ERROR,
238 G_MARKUP_ERROR_INVALID_CONTENT,
239 "<%s> is not contained in "
240 "the specified range", tag);
244 else if (state->strinfo->len)
246 if (!is_valid_choices (state->default_value, state->strinfo))
249 g_set_error (error, G_MARKUP_ERROR,
250 G_MARKUP_ERROR_INVALID_CONTENT,
251 "<%s> is not a valid member of "
252 "the specified enumerated type", tag);
255 g_set_error (error, G_MARKUP_ERROR,
256 G_MARKUP_ERROR_INVALID_CONTENT,
257 "<%s> contains string not in "
265 key_state_set_range (KeyState *state,
266 const gchar *min_str,
267 const gchar *max_str,
272 g_set_error_literal (error, G_MARKUP_ERROR,
273 G_MARKUP_ERROR_INVALID_CONTENT,
274 "<range/> already specified for this key");
278 if (strchr ("ynqiuxtd", *(char *) state->type) == NULL)
280 gchar *type = g_variant_type_dup_string (state->type);
281 g_set_error (error, G_MARKUP_ERROR,
282 G_MARKUP_ERROR_INVALID_CONTENT,
283 "<range> not allowed for keys of type '%s'", type);
288 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
289 if (state->minimum == NULL)
292 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
293 if (state->maximum == NULL)
296 if (g_variant_compare (state->minimum, state->maximum) > 0)
298 g_set_error (error, G_MARKUP_ERROR,
299 G_MARKUP_ERROR_INVALID_CONTENT,
300 "<range> specified minimum is greater than maxmimum");
304 key_state_check_range (state, error);
308 key_state_start_default (KeyState *state,
310 const gchar *context,
315 if (strcmp (l10n, "messages") == 0)
318 else if (strcmp (l10n, "time") == 0)
323 g_set_error (error, G_MARKUP_ERROR,
324 G_MARKUP_ERROR_INVALID_CONTENT,
325 "unsupported l10n category: %s", l10n);
329 if (!state->have_gettext_domain)
331 g_set_error_literal (error, G_MARKUP_ERROR,
332 G_MARKUP_ERROR_INVALID_CONTENT,
333 "l10n requested, but no "
334 "gettext domain given");
338 state->l10n_context = g_strdup (context);
341 else if (context != NULL)
343 g_set_error_literal (error, G_MARKUP_ERROR,
344 G_MARKUP_ERROR_INVALID_CONTENT,
345 "translation context given for "
346 " value without l10n enabled");
350 return g_string_new (NULL);
354 key_state_end_default (KeyState *state,
358 state->unparsed_default_value = *string;
361 state->default_value = g_variant_parse (state->type,
362 state->unparsed_default_value->str,
364 key_state_check_range (state, error);
368 key_state_start_choices (KeyState *state,
371 const GVariantType *type = state->type;
375 g_set_error_literal (error, G_MARKUP_ERROR,
376 G_MARKUP_ERROR_INVALID_CONTENT,
377 "<choices> can not be specified for keys "
378 "tagged as having an enumerated type");
382 if (state->has_choices)
384 g_set_error_literal (error, G_MARKUP_ERROR,
385 G_MARKUP_ERROR_INVALID_CONTENT,
386 "<choices> already specified for this key");
390 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
391 type = g_variant_type_element (type);
393 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
395 gchar *type_string = g_variant_type_dup_string (state->type);
396 g_set_error (error, G_MARKUP_ERROR,
397 G_MARKUP_ERROR_INVALID_CONTENT,
398 "<choices> not allowed for keys of type '%s'",
400 g_free (type_string);
406 key_state_add_choice (KeyState *state,
410 if (strinfo_builder_contains (state->strinfo, choice))
412 g_set_error (error, G_MARKUP_ERROR,
413 G_MARKUP_ERROR_INVALID_CONTENT,
414 "<choice value='%s'/> already given", choice);
418 strinfo_builder_append_item (state->strinfo, choice, 0);
419 state->has_choices = TRUE;
423 key_state_end_choices (KeyState *state,
426 if (!state->has_choices)
428 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
429 "<choices> must contain at least one <choice>");
433 key_state_check_range (state, error);
437 key_state_start_aliases (KeyState *state,
440 if (state->has_aliases)
441 g_set_error_literal (error, G_MARKUP_ERROR,
442 G_MARKUP_ERROR_INVALID_CONTENT,
443 "<aliases> already specified for this key");
445 if (!state->is_enum && !state->has_choices)
446 g_set_error_literal (error, G_MARKUP_ERROR,
447 G_MARKUP_ERROR_INVALID_CONTENT,
448 "<aliases> can only be specified for keys with "
449 "enumerated types or after <choices>");
453 key_state_add_alias (KeyState *state,
458 if (strinfo_builder_contains (state->strinfo, alias))
460 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
461 state->strinfo->len / 4,
465 g_set_error (error, G_MARKUP_ERROR,
466 G_MARKUP_ERROR_INVALID_CONTENT,
467 "<alias value='%s'/> given when '%s' is already "
468 "a member of the enumerated type", alias, alias);
471 g_set_error (error, G_MARKUP_ERROR,
472 G_MARKUP_ERROR_INVALID_CONTENT,
473 "<alias value='%s'/> given when "
474 "<choice value='%s'/> was already given",
479 g_set_error (error, G_MARKUP_ERROR,
480 G_MARKUP_ERROR_INVALID_CONTENT,
481 "<alias value='%s'/> already specified", alias);
486 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
488 g_set_error (error, G_MARKUP_ERROR,
489 G_MARKUP_ERROR_INVALID_CONTENT,
490 "alias target '%s' is not in %s", target,
491 state->is_enum ? "enumerated type" : "<choices>");
495 state->has_aliases = TRUE;
499 key_state_end_aliases (KeyState *state,
502 if (!state->has_aliases)
504 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
505 "<aliases> must contain at least one <alias>");
511 key_state_check (KeyState *state,
517 return state->checked = TRUE;
521 key_state_serialise (KeyState *state)
523 if (state->serialised == NULL)
525 if (state->child_schema)
527 state->serialised = g_variant_new_string (state->child_schema);
532 GVariantBuilder builder;
534 g_assert (key_state_check (state, NULL));
536 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
539 g_variant_builder_add_value (&builder, state->default_value);
544 if (state->l10n_context)
548 /* Contextified messages are supported by prepending
549 * the context, followed by '\004' to the start of the
550 * message string. We do that here to save GSettings
553 len = strlen (state->l10n_context);
554 state->l10n_context[len] = '\004';
555 g_string_prepend_len (state->unparsed_default_value,
556 state->l10n_context, len + 1);
557 g_free (state->l10n_context);
558 state->l10n_context = NULL;
561 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
562 state->unparsed_default_value);
563 g_string_free (state->unparsed_default_value, TRUE);
564 state->unparsed_default_value = NULL;
567 /* choice, aliases, enums */
568 if (state->strinfo->len)
574 data = state->strinfo->str;
575 size = state->strinfo->len;
577 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
581 g_string_free (state->strinfo, FALSE);
582 state->strinfo = NULL;
584 g_variant_builder_add (&builder, "(y@au)",
585 state->is_enum ? 'e' : 'c',
590 if (state->minimum || state->maximum)
591 g_variant_builder_add (&builder, "(y(**))", 'r',
592 state->minimum, state->maximum);
594 state->serialised = g_variant_builder_end (&builder);
597 g_variant_ref_sink (state->serialised);
600 return g_variant_ref (state->serialised);
604 key_state_free (gpointer data)
606 KeyState *state = data;
609 g_variant_type_free (state->type);
611 g_free (state->l10n_context);
613 if (state->unparsed_default_value)
614 g_string_free (state->unparsed_default_value, TRUE);
616 if (state->default_value)
617 g_variant_unref (state->default_value);
620 g_string_free (state->strinfo, TRUE);
623 g_variant_unref (state->minimum);
626 g_variant_unref (state->maximum);
628 if (state->serialised)
629 g_variant_unref (state->serialised);
631 g_slice_free (KeyState, state);
634 /* Key name validity {{{1 */
635 static gboolean allow_any_name = FALSE;
638 is_valid_keyname (const gchar *key,
645 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
646 "empty names are not permitted");
653 if (!g_ascii_islower (key[0]))
655 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
656 "invalid name '%s': names must begin "
657 "with a lowercase letter", key);
661 for (i = 1; key[i]; i++)
664 !g_ascii_islower (key[i]) &&
665 !g_ascii_isdigit (key[i]))
667 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
668 "invalid name '%s': invalid character '%c'; "
669 "only lowercase letters, numbers and dash ('-') "
670 "are permitted.", key, key[i]);
674 if (key[i] == '-' && key[i + 1] == '-')
676 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
677 "invalid name '%s': two successive dashes ('--') are "
678 "not permitted.", key);
683 if (key[i - 1] == '-')
685 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
686 "invalid name '%s': the last character may not be a "
693 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
694 "invalid name '%s': maximum length is 32", key);
701 /* Handling of <schema> {{{1 */
702 typedef struct _SchemaState SchemaState;
705 SchemaState *extends;
708 gchar *gettext_domain;
716 schema_state_new (const gchar *path,
717 const gchar *gettext_domain,
718 SchemaState *extends,
719 const gchar *extends_name,
720 const gchar *list_of)
724 state = g_slice_new (SchemaState);
725 state->path = g_strdup (path);
726 state->gettext_domain = g_strdup (gettext_domain);
727 state->extends = extends;
728 state->extends_name = g_strdup (extends_name);
729 state->list_of = g_strdup (list_of);
730 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
731 g_free, key_state_free);
737 schema_state_free (gpointer data)
739 SchemaState *state = data;
741 g_free (state->path);
742 g_free (state->gettext_domain);
743 g_hash_table_unref (state->keys);
747 schema_state_add_child (SchemaState *state,
754 if (!is_valid_keyname (name, error))
757 childname = g_strconcat (name, "/", NULL);
759 if (g_hash_table_lookup (state->keys, childname))
761 g_set_error (error, G_MARKUP_ERROR,
762 G_MARKUP_ERROR_INVALID_CONTENT,
763 "<child name='%s'> already specified", name);
767 g_hash_table_insert (state->keys, childname,
768 key_state_new_child (schema));
772 schema_state_add_key (SchemaState *state,
773 GHashTable *enum_table,
775 const gchar *type_string,
776 const gchar *enum_type,
785 g_set_error_literal (error, G_MARKUP_ERROR,
786 G_MARKUP_ERROR_INVALID_CONTENT,
787 "can not add keys to a list-of schema");
791 if (!is_valid_keyname (name, error))
794 if (g_hash_table_lookup (state->keys, name))
796 g_set_error (error, G_MARKUP_ERROR,
797 G_MARKUP_ERROR_INVALID_CONTENT,
798 "<key name='%s'> already specified", name);
802 for (node = state; node; node = node->extends)
807 shadow = g_hash_table_lookup (node->extends->keys, name);
809 /* in case of <key> <override> <key> make sure we report the
810 * location of the original <key>, not the <override>.
812 if (shadow && !shadow->is_override)
814 g_set_error (error, G_MARKUP_ERROR,
815 G_MARKUP_ERROR_INVALID_CONTENT,
816 "<key name='%s'> shadows <key name='%s'> in "
817 "<schema id='%s'>; use <override> to modify value",
818 name, name, node->extends_name);
823 if ((type_string == NULL) == (enum_type == NULL))
825 g_set_error (error, G_MARKUP_ERROR,
826 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
827 "exactly one of 'type' or 'enum' must "
828 "be specified as an attribute to <key>");
832 if (enum_type != NULL)
834 enum_data = g_hash_table_lookup (enum_table, enum_type);
836 if (enum_data == NULL)
838 g_set_error (error, G_MARKUP_ERROR,
839 G_MARKUP_ERROR_INVALID_CONTENT,
840 "<enum id='%s'> not (yet) defined.", enum_type);
844 g_assert (type_string == NULL);
849 if (!g_variant_type_string_is_valid (type_string))
851 g_set_error (error, G_MARKUP_ERROR,
852 G_MARKUP_ERROR_INVALID_CONTENT,
853 "invalid GVariant type string '%s'", type_string);
860 key = key_state_new (type_string, state->gettext_domain, enum_data);
861 g_hash_table_insert (state->keys, g_strdup (name), key);
867 schema_state_add_override (SchemaState *state,
868 KeyState **key_state,
872 const gchar *context,
878 if (state->extends == NULL)
880 g_set_error_literal (error, G_MARKUP_ERROR,
881 G_MARKUP_ERROR_INVALID_CONTENT,
882 "<override> given but schema isn't "
883 "extending anything");
887 for (parent = state->extends; parent; parent = parent->extends)
888 if ((original = g_hash_table_lookup (parent->keys, key)))
891 if (original == NULL)
893 g_set_error (error, G_MARKUP_ERROR,
894 G_MARKUP_ERROR_INVALID_CONTENT,
895 "no <key name='%s'> to override", key);
899 if (g_hash_table_lookup (state->keys, key))
901 g_set_error (error, G_MARKUP_ERROR,
902 G_MARKUP_ERROR_INVALID_CONTENT,
903 "<override name='%s'> already specified", key);
907 *key_state = key_state_override (original, state->gettext_domain);
908 *string = key_state_start_default (*key_state, l10n, context, error);
909 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
913 override_state_end (KeyState **key_state,
917 key_state_end_default (*key_state, string, error);
921 /* Handling of toplevel state {{{1 */
924 GHashTable *schema_table; /* string -> SchemaState */
925 GHashTable *enum_table; /* string -> GString */
927 gchar *schemalist_domain; /* the <schemalist> gettext domain */
929 SchemaState *schema_state; /* non-NULL when inside <schema> */
930 KeyState *key_state; /* non-NULL when inside <key> */
931 GString *enum_state; /* non-NULL when inside <enum> */
933 GString *string; /* non-NULL when accepting text */
937 is_subclass (const gchar *class_name,
938 const gchar *possible_parent,
939 GHashTable *schema_table)
943 if (strcmp (class_name, possible_parent) == 0)
946 class = g_hash_table_lookup (schema_table, class_name);
947 g_assert (class != NULL);
949 return class->extends_name &&
950 is_subclass (class->extends_name, possible_parent, schema_table);
954 parse_state_start_schema (ParseState *state,
957 const gchar *gettext_domain,
958 const gchar *extends_name,
959 const gchar *list_of,
962 SchemaState *extends;
964 if (g_hash_table_lookup (state->schema_table, id))
966 g_set_error (error, G_MARKUP_ERROR,
967 G_MARKUP_ERROR_INVALID_CONTENT,
968 "<schema id='%s'> already specified", id);
974 extends = g_hash_table_lookup (state->schema_table, extends_name);
978 g_set_error (error, G_MARKUP_ERROR,
979 G_MARKUP_ERROR_INVALID_CONTENT,
980 "<schema id='%s'> extends not yet "
981 "existing schema '%s'", id, extends_name);
990 if (!g_hash_table_lookup (state->schema_table, list_of))
992 g_set_error (error, G_MARKUP_ERROR,
993 G_MARKUP_ERROR_INVALID_CONTENT,
994 "<schema id='%s'> is list of not yet "
995 "existing schema '%s'", id, list_of);
1004 if (extends->list_of == NULL)
1006 g_set_error (error, G_MARKUP_ERROR,
1007 G_MARKUP_ERROR_INVALID_CONTENT,
1008 "<schema id='%s'> is a list, extending "
1009 "<schema id='%s'> which is not a list",
1014 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1016 g_set_error (error, G_MARKUP_ERROR,
1017 G_MARKUP_ERROR_INVALID_CONTENT,
1018 "<schema id='%s' list-of='%s'> extends <schema "
1019 "id='%s' list-of='%s'> but '%s' does not extend '%s'",
1020 id, list_of, extends_name, extends->list_of,
1021 list_of, extends->list_of);
1026 /* by default we are a list of the same thing that the schema
1027 * we are extending is a list of (which might be nothing)
1029 list_of = extends->list_of;
1032 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1034 g_set_error (error, G_MARKUP_ERROR,
1035 G_MARKUP_ERROR_INVALID_CONTENT,
1036 "a path, if given, must begin and "
1037 "end with a slash");
1041 state->schema_state = schema_state_new (path, gettext_domain,
1042 extends, extends_name, list_of);
1043 g_hash_table_insert (state->schema_table, g_strdup (id),
1044 state->schema_state);
1048 parse_state_start_enum (ParseState *state,
1052 if (g_hash_table_lookup (state->enum_table, id))
1054 g_set_error (error, G_MARKUP_ERROR,
1055 G_MARKUP_ERROR_INVALID_CONTENT,
1056 "<enum id='%s'> already specified", id);
1060 state->enum_state = g_string_new (NULL);
1061 g_hash_table_insert (state->enum_table, g_strdup (id), state->enum_state);
1064 /* GMarkup Parser Functions {{{1 */
1066 /* Start element {{{2 */
1068 start_element (GMarkupParseContext *context,
1069 const gchar *element_name,
1070 const gchar **attribute_names,
1071 const gchar **attribute_values,
1075 ParseState *state = user_data;
1076 const GSList *element_stack;
1077 const gchar *container;
1079 element_stack = g_markup_parse_context_get_element_stack (context);
1080 container = element_stack->next ? element_stack->next->data : NULL;
1082 #define COLLECT(first, ...) \
1083 g_markup_collect_attributes (element_name, \
1084 attribute_names, attribute_values, error, \
1085 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1086 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1087 #define STRDUP G_MARKUP_COLLECT_STRDUP
1088 #define STRING G_MARKUP_COLLECT_STRING
1089 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1091 /* Toplevel items {{{3 */
1092 if (container == NULL)
1094 if (strcmp (element_name, "schemalist") == 0)
1096 COLLECT (OPTIONAL | STRDUP,
1098 &state->schemalist_domain);
1104 /* children of <schemalist> {{{3 */
1105 else if (strcmp (container, "schemalist") == 0)
1107 if (strcmp (element_name, "schema") == 0)
1109 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1110 if (COLLECT (STRING, "id", &id,
1111 OPTIONAL | STRING, "path", &path,
1112 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1113 OPTIONAL | STRING, "extends", &extends,
1114 OPTIONAL | STRING, "list-of", &list_of))
1115 parse_state_start_schema (state, id, path, gettext_domain,
1116 extends, list_of, error);
1120 else if (strcmp (element_name, "enum") == 0)
1123 if (COLLECT (STRING, "id", &id))
1124 parse_state_start_enum (state, id, error);
1130 /* children of <schema> {{{3 */
1131 else if (strcmp (container, "schema") == 0)
1133 if (strcmp (element_name, "key") == 0)
1135 const gchar *name, *type_string, *enum_type;
1137 if (COLLECT (STRING, "name", &name,
1138 OPTIONAL | STRING, "type", &type_string,
1139 OPTIONAL | STRING, "enum", &enum_type))
1141 state->key_state = schema_state_add_key (state->schema_state,
1147 else if (strcmp (element_name, "child") == 0)
1149 const gchar *name, *schema;
1151 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1152 schema_state_add_child (state->schema_state,
1153 name, schema, error);
1156 else if (strcmp (element_name, "override") == 0)
1158 const gchar *name, *l10n, *context;
1160 if (COLLECT (STRING, "name", &name,
1161 OPTIONAL | STRING, "l10n", &l10n,
1162 OPTIONAL | STRING, "context", &context))
1163 schema_state_add_override (state->schema_state,
1164 &state->key_state, &state->string,
1165 name, l10n, context, error);
1170 /* children of <key> {{{3 */
1171 else if (strcmp (container, "key") == 0)
1173 if (strcmp (element_name, "default") == 0)
1175 const gchar *l10n, *context;
1176 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1177 STRING | OPTIONAL, "context", &context))
1178 state->string = key_state_start_default (state->key_state,
1179 l10n, context, error);
1183 else if (strcmp (element_name, "summary") == 0 ||
1184 strcmp (element_name, "description") == 0)
1187 state->string = g_string_new (NULL);
1191 else if (strcmp (element_name, "range") == 0)
1193 const gchar *min, *max;
1194 if (COLLECT (STRING, "min", &min, STRING, "max", &max))
1195 key_state_set_range (state->key_state, min, max, error);
1199 else if (strcmp (element_name, "choices") == 0)
1202 key_state_start_choices (state->key_state, error);
1206 else if (strcmp (element_name, "aliases") == 0)
1209 key_state_start_aliases (state->key_state, error);
1215 /* children of <choices> {{{3 */
1216 else if (strcmp (container, "choices") == 0)
1218 if (strcmp (element_name, "choice") == 0)
1221 if (COLLECT (STRING, "value", &value))
1222 key_state_add_choice (state->key_state, value, error);
1228 /* children of <aliases> {{{3 */
1229 else if (strcmp (container, "aliases") == 0)
1231 if (strcmp (element_name, "alias") == 0)
1233 const gchar *value, *target;
1234 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1235 key_state_add_alias (state->key_state, value, target, error);
1241 /* children of <enum> {{{3 */
1242 else if (strcmp (container, "enum") == 0)
1244 if (strcmp (element_name, "value") == 0)
1246 const gchar *nick, *valuestr;
1247 if (COLLECT (STRING, "nick", &nick,
1248 STRING, "value", &valuestr))
1249 enum_state_add_value (state->enum_state, nick, valuestr, error);
1256 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1257 "Element <%s> not allowed inside <%s>",
1258 element_name, container);
1260 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1261 "Element <%s> not allowed at toplevel", element_name);
1264 /* End element {{{2 */
1267 key_state_end (KeyState **state_ptr,
1275 if (state->default_value == NULL)
1277 g_set_error_literal (error,
1278 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1279 "element <default> is required in <key>");
1285 schema_state_end (SchemaState **state_ptr,
1295 end_element (GMarkupParseContext *context,
1296 const gchar *element_name,
1300 ParseState *state = user_data;
1302 if (strcmp (element_name, "schemalist") == 0)
1304 g_free (state->schemalist_domain);
1305 state->schemalist_domain = NULL;
1308 else if (strcmp (element_name, "enum") == 0)
1309 enum_state_end (&state->enum_state, error);
1311 else if (strcmp (element_name, "schema") == 0)
1312 schema_state_end (&state->schema_state, error);
1314 else if (strcmp (element_name, "override") == 0)
1315 override_state_end (&state->key_state, &state->string, error);
1317 else if (strcmp (element_name, "key") == 0)
1318 key_state_end (&state->key_state, error);
1320 else if (strcmp (element_name, "default") == 0)
1321 key_state_end_default (state->key_state, &state->string, error);
1323 else if (strcmp (element_name, "choices") == 0)
1324 key_state_end_choices (state->key_state, error);
1326 else if (strcmp (element_name, "aliases") == 0)
1327 key_state_end_aliases (state->key_state, error);
1331 g_string_free (state->string, TRUE);
1332 state->string = NULL;
1337 text (GMarkupParseContext *context,
1343 ParseState *state = user_data;
1346 for (i = 0; i < text_len; i++)
1347 if (!g_ascii_isspace (text[i]))
1350 g_string_append_len (state->string, text, text_len);
1353 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1354 "text may not appear inside <%s>",
1355 g_markup_parse_context_get_element (context));
1361 /* Write to GVDB {{{1 */
1369 gvdb_pair_init (GvdbPair *pair)
1371 pair->table = gvdb_hash_table_new (NULL, NULL);
1372 pair->root = gvdb_hash_table_insert (pair->table, "");
1382 output_key (gpointer key,
1386 OutputSchemaData *data;
1395 item = gvdb_hash_table_insert (data->pair.table, name);
1396 gvdb_item_set_parent (item, data->pair.root);
1397 gvdb_item_set_value (item, key_state_serialise (state));
1404 output_schema (gpointer key,
1408 OutputSchemaData data;
1409 GvdbPair *root_pair;
1416 root_pair = user_data;
1418 gvdb_pair_init (&data.pair);
1421 item = gvdb_hash_table_insert (root_pair->table, id);
1422 gvdb_item_set_parent (item, root_pair->root);
1423 gvdb_item_set_hash_table (item, data.pair.table);
1425 g_hash_table_foreach (state->keys, output_key, &data);
1428 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1430 if (state->extends_name)
1431 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1432 state->extends_name);
1435 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1436 state->extends_name);
1439 gvdb_hash_table_insert_string (data.pair.table,
1441 state->gettext_domain);
1445 write_to_file (GHashTable *schema_table,
1446 const gchar *filename,
1452 gvdb_pair_init (&pair);
1454 g_hash_table_foreach (schema_table, output_schema, &pair);
1456 success = gvdb_table_write_contents (pair.table, filename,
1457 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1459 g_hash_table_unref (pair.table);
1464 /* Parser driver {{{1 */
1466 parse_gschema_files (gchar **files,
1469 GMarkupParser parser = { start_element, end_element, text };
1470 GMarkupParseContext *context;
1471 ParseState state = { 0, };
1472 const gchar *filename;
1474 context = g_markup_parse_context_new (&parser,
1475 G_MARKUP_PREFIX_ERROR_POSITION,
1478 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1479 g_free, enum_state_free);
1481 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1482 g_free, schema_state_free);
1484 while ((filename = *files++) != NULL)
1489 if (!g_file_get_contents (filename, &contents, &size, error))
1492 if (!g_markup_parse_context_parse (context, contents, size, error))
1494 g_prefix_error (error, "%s: ", filename);
1498 if (!g_markup_parse_context_end_parse (context, error))
1500 g_prefix_error (error, "%s: ", filename);
1505 g_hash_table_unref (state.enum_table);
1507 return state.schema_table;
1511 compare_strings (gconstpointer a,
1514 gchar *one = *(gchar **) a;
1515 gchar *two = *(gchar **) b;
1517 return strcmp (one, two);
1521 main (int argc, char **argv)
1528 gchar *targetdir = NULL;
1530 gboolean uninstall = FALSE;
1531 gboolean dry_run = FALSE;
1532 gchar **schema_files = NULL;
1533 GOptionContext *context;
1534 GOptionEntry entries[] = {
1535 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1536 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1537 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1538 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1540 /* These options are only for use in the gschema-compile tests */
1541 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1545 setlocale (LC_ALL, "");
1547 context = g_option_context_new (N_("DIRECTORY"));
1548 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1549 g_option_context_set_summary (context,
1550 N_("Compile all GSettings schema files into a schema cache.\n"
1551 "Schema files are required to have the extension .gschema.xml,\n"
1552 "and the cache file is called gschemas.compiled."));
1553 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1556 if (!g_option_context_parse (context, &argc, &argv, &error))
1558 fprintf (stderr, "%s", error->message);
1562 g_option_context_free (context);
1564 if (!schema_files && argc != 2)
1566 fprintf (stderr, _("You should give exactly one directory name\n"));
1572 if (targetdir == NULL)
1575 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1581 files = g_ptr_array_new ();
1583 dir = g_dir_open (srcdir, 0, &error);
1586 fprintf (stderr, "%s\n", error->message);
1590 while ((file = g_dir_read_name (dir)) != NULL)
1592 if (g_str_has_suffix (file, ".gschema.xml") ||
1593 g_str_has_suffix (file, ".enums.xml"))
1594 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
1597 if (files->len == 0)
1599 fprintf (stderr, _("No schema files found: "));
1601 if (g_unlink (target))
1602 fprintf (stderr, _("doing nothing.\n"));
1605 fprintf (stderr, _("removed existing output file.\n"));
1609 g_ptr_array_sort (files, compare_strings);
1610 g_ptr_array_add (files, NULL);
1612 schema_files = (char **) g_ptr_array_free (files, FALSE);
1616 if (!(table = parse_gschema_files (schema_files, &error)) ||
1617 (!dry_run && !write_to_file (table, target, &error)))
1619 fprintf (stderr, "%s\n", error->message);
1630 /* vim:set foldmethod=marker: */