glib/tests: Fix non-debug build of slice test
[glib.git] / gio / glib-compile-schemas.c
blobd2ae81aa769dc17208582c705fae7bcd3156829c
1 /*
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>
22 /* Prologue {{{1 */
23 #include "config.h"
25 #include <gstdio.h>
26 #include <gi18n.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <locale.h>
32 #include "gvdb/gvdb-builder.h"
33 #include "strinfo.c"
35 #ifdef G_OS_WIN32
36 #include "glib/glib-private.h"
37 #endif
39 static void
40 strip_string (GString *string)
42 gint i;
44 for (i = 0; g_ascii_isspace (string->str[i]); i++);
45 g_string_erase (string, 0, i);
47 if (string->len > 0)
49 /* len > 0, so there must be at least one non-whitespace character */
50 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
51 g_string_truncate (string, i + 1);
55 /* Handling of <enum> {{{1 */
56 typedef struct
58 GString *strinfo;
60 gboolean is_flags;
61 } EnumState;
63 static void
64 enum_state_free (gpointer data)
66 EnumState *state = data;
68 g_string_free (state->strinfo, TRUE);
69 g_slice_free (EnumState, state);
72 static EnumState *
73 enum_state_new (gboolean is_flags)
75 EnumState *state;
77 state = g_slice_new (EnumState);
78 state->strinfo = g_string_new (NULL);
79 state->is_flags = is_flags;
81 return state;
84 static void
85 enum_state_add_value (EnumState *state,
86 const gchar *nick,
87 const gchar *valuestr,
88 GError **error)
90 gint64 value;
91 gchar *end;
93 if (nick[0] == '\0' || nick[1] == '\0')
95 g_set_error (error, G_MARKUP_ERROR,
96 G_MARKUP_ERROR_INVALID_CONTENT,
97 "nick must be a minimum of 2 characters");
98 return;
101 value = g_ascii_strtoll (valuestr, &end, 0);
102 if (*end || state->is_flags ?
103 (value > G_MAXUINT32 || value < 0) :
104 (value > G_MAXINT32 || value < G_MININT32))
106 g_set_error (error, G_MARKUP_ERROR,
107 G_MARKUP_ERROR_INVALID_CONTENT,
108 "invalid numeric value");
109 return;
112 if (strinfo_builder_contains (state->strinfo, nick))
114 g_set_error (error, G_MARKUP_ERROR,
115 G_MARKUP_ERROR_INVALID_CONTENT,
116 "<value nick='%s'/> already specified", nick);
117 return;
120 if (strinfo_builder_contains_value (state->strinfo, value))
122 g_set_error (error, G_MARKUP_ERROR,
123 G_MARKUP_ERROR_INVALID_CONTENT,
124 "value='%s' already specified", valuestr);
125 return;
128 /* Silently drop the null case if it is mentioned.
129 * It is properly denoted with an empty array.
131 if (state->is_flags && value == 0)
132 return;
134 if (state->is_flags && (value & (value - 1)))
136 g_set_error (error, G_MARKUP_ERROR,
137 G_MARKUP_ERROR_INVALID_CONTENT,
138 "flags values must have at most 1 bit set");
139 return;
142 /* Since we reject exact duplicates of value='' and we only allow one
143 * bit to be set, it's not possible to have overlaps.
145 * If we loosen the one-bit-set restriction we need an overlap check.
148 strinfo_builder_append_item (state->strinfo, nick, value);
151 static void
152 enum_state_end (EnumState **state_ptr,
153 GError **error)
155 EnumState *state;
157 state = *state_ptr;
158 *state_ptr = NULL;
160 if (state->strinfo->len == 0)
161 g_set_error (error,
162 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
163 "<%s> must contain at least one <value>",
164 state->is_flags ? "flags" : "enum");
167 /* Handling of <key> {{{1 */
168 typedef struct
170 /* for <child>, @child_schema will be set.
171 * for <key>, everything else will be set.
173 gchar *child_schema;
176 GVariantType *type;
177 gboolean have_gettext_domain;
179 gchar l10n;
180 gchar *l10n_context;
181 GString *unparsed_default_value;
182 GVariant *default_value;
184 GString *strinfo;
185 gboolean is_enum;
186 gboolean is_flags;
188 GVariant *minimum;
189 GVariant *maximum;
191 gboolean has_choices;
192 gboolean has_aliases;
193 gboolean is_override;
195 gboolean checked;
196 GVariant *serialised;
197 } KeyState;
199 static KeyState *
200 key_state_new (const gchar *type_string,
201 const gchar *gettext_domain,
202 gboolean is_enum,
203 gboolean is_flags,
204 GString *strinfo)
206 KeyState *state;
208 state = g_slice_new0 (KeyState);
209 state->type = g_variant_type_new (type_string);
210 state->have_gettext_domain = gettext_domain != NULL;
211 state->is_enum = is_enum;
212 state->is_flags = is_flags;
214 if (strinfo)
215 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
216 else
217 state->strinfo = g_string_new (NULL);
219 return state;
222 static KeyState *
223 key_state_override (KeyState *state,
224 const gchar *gettext_domain)
226 KeyState *copy;
228 copy = g_slice_new0 (KeyState);
229 copy->type = g_variant_type_copy (state->type);
230 copy->have_gettext_domain = gettext_domain != NULL;
231 copy->strinfo = g_string_new_len (state->strinfo->str,
232 state->strinfo->len);
233 copy->is_enum = state->is_enum;
234 copy->is_flags = state->is_flags;
235 copy->is_override = TRUE;
237 if (state->minimum)
239 copy->minimum = g_variant_ref (state->minimum);
240 copy->maximum = g_variant_ref (state->maximum);
243 return copy;
246 static KeyState *
247 key_state_new_child (const gchar *child_schema)
249 KeyState *state;
251 state = g_slice_new0 (KeyState);
252 state->child_schema = g_strdup (child_schema);
254 return state;
257 static gboolean
258 is_valid_choices (GVariant *variant,
259 GString *strinfo)
261 switch (g_variant_classify (variant))
263 case G_VARIANT_CLASS_MAYBE:
264 case G_VARIANT_CLASS_ARRAY:
266 gboolean valid = TRUE;
267 GVariantIter iter;
269 g_variant_iter_init (&iter, variant);
271 while (valid && (variant = g_variant_iter_next_value (&iter)))
273 valid = is_valid_choices (variant, strinfo);
274 g_variant_unref (variant);
277 return valid;
280 case G_VARIANT_CLASS_STRING:
281 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
282 strinfo->len / 4,
283 g_variant_get_string (variant, NULL));
285 default:
286 g_assert_not_reached ();
291 /* Gets called at </default> </choices> or <range/> to check for
292 * validity of the default value so that any inconsistency is
293 * reported as soon as it is encountered.
295 static void
296 key_state_check_range (KeyState *state,
297 GError **error)
299 if (state->default_value)
301 const gchar *tag;
303 tag = state->is_override ? "override" : "default";
305 if (state->minimum)
307 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
308 g_variant_compare (state->default_value, state->maximum) > 0)
310 g_set_error (error, G_MARKUP_ERROR,
311 G_MARKUP_ERROR_INVALID_CONTENT,
312 "<%s> is not contained in "
313 "the specified range", tag);
317 else if (state->strinfo->len)
319 if (!is_valid_choices (state->default_value, state->strinfo))
321 if (state->is_enum)
322 g_set_error (error, G_MARKUP_ERROR,
323 G_MARKUP_ERROR_INVALID_CONTENT,
324 "<%s> is not a valid member of "
325 "the specified enumerated type", tag);
327 else if (state->is_flags)
328 g_set_error (error, G_MARKUP_ERROR,
329 G_MARKUP_ERROR_INVALID_CONTENT,
330 "<%s> contains string not in the "
331 "specified flags type", tag);
333 else
334 g_set_error (error, G_MARKUP_ERROR,
335 G_MARKUP_ERROR_INVALID_CONTENT,
336 "<%s> contains string not in "
337 "<choices>", tag);
343 static void
344 key_state_set_range (KeyState *state,
345 const gchar *min_str,
346 const gchar *max_str,
347 GError **error)
349 const struct {
350 const gchar type;
351 const gchar *min;
352 const gchar *max;
353 } table[] = {
354 { 'y', "0", "255" },
355 { 'n', "-32768", "32767" },
356 { 'q', "0", "65535" },
357 { 'i', "-2147483648", "2147483647" },
358 { 'u', "0", "4294967295" },
359 { 'x', "-9223372036854775808", "9223372036854775807" },
360 { 't', "0", "18446744073709551615" },
361 { 'd', "-inf", "inf" },
363 gboolean type_ok = FALSE;
364 gint i;
366 if (state->minimum)
368 g_set_error_literal (error, G_MARKUP_ERROR,
369 G_MARKUP_ERROR_INVALID_CONTENT,
370 "<range/> already specified for this key");
371 return;
374 for (i = 0; i < G_N_ELEMENTS (table); i++)
375 if (*(char *) state->type == table[i].type)
377 min_str = min_str ? min_str : table[i].min;
378 max_str = max_str ? max_str : table[i].max;
379 type_ok = TRUE;
380 break;
383 if (!type_ok)
385 gchar *type = g_variant_type_dup_string (state->type);
386 g_set_error (error, G_MARKUP_ERROR,
387 G_MARKUP_ERROR_INVALID_CONTENT,
388 "<range> not allowed for keys of type '%s'", type);
389 g_free (type);
390 return;
393 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
394 if (state->minimum == NULL)
395 return;
397 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
398 if (state->maximum == NULL)
399 return;
401 if (g_variant_compare (state->minimum, state->maximum) > 0)
403 g_set_error (error, G_MARKUP_ERROR,
404 G_MARKUP_ERROR_INVALID_CONTENT,
405 "<range> specified minimum is greater than maxmimum");
406 return;
409 key_state_check_range (state, error);
412 static GString *
413 key_state_start_default (KeyState *state,
414 const gchar *l10n,
415 const gchar *context,
416 GError **error)
418 if (l10n != NULL)
420 if (strcmp (l10n, "messages") == 0)
421 state->l10n = 'm';
423 else if (strcmp (l10n, "time") == 0)
424 state->l10n = 't';
426 else
428 g_set_error (error, G_MARKUP_ERROR,
429 G_MARKUP_ERROR_INVALID_CONTENT,
430 "unsupported l10n category: %s", l10n);
431 return NULL;
434 if (!state->have_gettext_domain)
436 g_set_error_literal (error, G_MARKUP_ERROR,
437 G_MARKUP_ERROR_INVALID_CONTENT,
438 "l10n requested, but no "
439 "gettext domain given");
440 return NULL;
443 state->l10n_context = g_strdup (context);
446 else if (context != NULL)
448 g_set_error_literal (error, G_MARKUP_ERROR,
449 G_MARKUP_ERROR_INVALID_CONTENT,
450 "translation context given for "
451 " value without l10n enabled");
452 return NULL;
455 return g_string_new (NULL);
458 static void
459 key_state_end_default (KeyState *state,
460 GString **string,
461 GError **error)
463 state->unparsed_default_value = *string;
464 *string = NULL;
466 state->default_value = g_variant_parse (state->type,
467 state->unparsed_default_value->str,
468 NULL, NULL, error);
469 key_state_check_range (state, error);
472 static void
473 key_state_start_choices (KeyState *state,
474 GError **error)
476 const GVariantType *type = state->type;
478 if (state->is_enum)
480 g_set_error_literal (error, G_MARKUP_ERROR,
481 G_MARKUP_ERROR_INVALID_CONTENT,
482 "<choices> cannot be specified for keys "
483 "tagged as having an enumerated type");
484 return;
487 if (state->has_choices)
489 g_set_error_literal (error, G_MARKUP_ERROR,
490 G_MARKUP_ERROR_INVALID_CONTENT,
491 "<choices> already specified for this key");
492 return;
495 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
496 type = g_variant_type_element (type);
498 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
500 gchar *type_string = g_variant_type_dup_string (state->type);
501 g_set_error (error, G_MARKUP_ERROR,
502 G_MARKUP_ERROR_INVALID_CONTENT,
503 "<choices> not allowed for keys of type '%s'",
504 type_string);
505 g_free (type_string);
506 return;
510 static void
511 key_state_add_choice (KeyState *state,
512 const gchar *choice,
513 GError **error)
515 if (strinfo_builder_contains (state->strinfo, choice))
517 g_set_error (error, G_MARKUP_ERROR,
518 G_MARKUP_ERROR_INVALID_CONTENT,
519 "<choice value='%s'/> already given", choice);
520 return;
523 strinfo_builder_append_item (state->strinfo, choice, 0);
524 state->has_choices = TRUE;
527 static void
528 key_state_end_choices (KeyState *state,
529 GError **error)
531 if (!state->has_choices)
533 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
534 "<choices> must contain at least one <choice>");
535 return;
538 key_state_check_range (state, error);
541 static void
542 key_state_start_aliases (KeyState *state,
543 GError **error)
545 if (state->has_aliases)
546 g_set_error_literal (error, G_MARKUP_ERROR,
547 G_MARKUP_ERROR_INVALID_CONTENT,
548 "<aliases> already specified for this key");
549 else if (!state->is_flags && !state->is_enum && !state->has_choices)
550 g_set_error_literal (error, G_MARKUP_ERROR,
551 G_MARKUP_ERROR_INVALID_CONTENT,
552 "<aliases> can only be specified for keys with "
553 "enumerated or flags types or after <choices>");
556 static void
557 key_state_add_alias (KeyState *state,
558 const gchar *alias,
559 const gchar *target,
560 GError **error)
562 if (strinfo_builder_contains (state->strinfo, alias))
564 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
565 state->strinfo->len / 4,
566 alias))
568 if (state->is_enum)
569 g_set_error (error, G_MARKUP_ERROR,
570 G_MARKUP_ERROR_INVALID_CONTENT,
571 "<alias value='%s'/> given when '%s' is already "
572 "a member of the enumerated type", alias, alias);
574 else
575 g_set_error (error, G_MARKUP_ERROR,
576 G_MARKUP_ERROR_INVALID_CONTENT,
577 "<alias value='%s'/> given when "
578 "<choice value='%s'/> was already given",
579 alias, alias);
582 else
583 g_set_error (error, G_MARKUP_ERROR,
584 G_MARKUP_ERROR_INVALID_CONTENT,
585 "<alias value='%s'/> already specified", alias);
587 return;
590 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
592 g_set_error (error, G_MARKUP_ERROR,
593 G_MARKUP_ERROR_INVALID_CONTENT,
594 "alias target '%s' is not in %s", target,
595 state->is_enum ? "enumerated type" : "<choices>");
596 return;
599 state->has_aliases = TRUE;
602 static void
603 key_state_end_aliases (KeyState *state,
604 GError **error)
606 if (!state->has_aliases)
608 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
609 "<aliases> must contain at least one <alias>");
610 return;
614 static gboolean
615 key_state_check (KeyState *state,
616 GError **error)
618 if (state->checked)
619 return TRUE;
621 return state->checked = TRUE;
624 static GVariant *
625 key_state_serialise (KeyState *state)
627 if (state->serialised == NULL)
629 if (state->child_schema)
631 state->serialised = g_variant_new_string (state->child_schema);
634 else
636 GVariantBuilder builder;
638 g_assert (key_state_check (state, NULL));
640 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
642 /* default value */
643 g_variant_builder_add_value (&builder, state->default_value);
645 /* translation */
646 if (state->l10n)
648 /* We are going to store the untranslated default for
649 * runtime translation according to the current locale.
650 * We need to strip leading and trailing whitespace from
651 * the string so that it's exactly the same as the one
652 * that ended up in the .po file for translation.
654 * We want to do this so that
656 * <default l10n='messages'>
657 * ['a', 'b', 'c']
658 * </default>
660 * ends up in the .po file like "['a', 'b', 'c']",
661 * omitting the extra whitespace at the start and end.
663 strip_string (state->unparsed_default_value);
665 if (state->l10n_context)
667 gint len;
669 /* Contextified messages are supported by prepending
670 * the context, followed by '\004' to the start of the
671 * message string. We do that here to save GSettings
672 * the work later on.
674 len = strlen (state->l10n_context);
675 state->l10n_context[len] = '\004';
676 g_string_prepend_len (state->unparsed_default_value,
677 state->l10n_context, len + 1);
678 g_free (state->l10n_context);
679 state->l10n_context = NULL;
682 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
683 state->unparsed_default_value->str);
684 g_string_free (state->unparsed_default_value, TRUE);
685 state->unparsed_default_value = NULL;
688 /* choice, aliases, enums */
689 if (state->strinfo->len)
691 GVariant *array;
692 guint32 *words;
693 gpointer data;
694 gsize size;
695 gint i;
697 data = state->strinfo->str;
698 size = state->strinfo->len;
700 words = data;
701 for (i = 0; i < size / sizeof (guint32); i++)
702 words[i] = GUINT32_TO_LE (words[i]);
704 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
705 data, size, TRUE,
706 g_free, data);
708 g_string_free (state->strinfo, FALSE);
709 state->strinfo = NULL;
711 g_variant_builder_add (&builder, "(y@au)",
712 state->is_flags ? 'f' :
713 state->is_enum ? 'e' : 'c',
714 array);
717 /* range */
718 if (state->minimum || state->maximum)
719 g_variant_builder_add (&builder, "(y(**))", 'r',
720 state->minimum, state->maximum);
722 state->serialised = g_variant_builder_end (&builder);
725 g_variant_ref_sink (state->serialised);
728 return g_variant_ref (state->serialised);
731 static void
732 key_state_free (gpointer data)
734 KeyState *state = data;
736 if (state->type)
737 g_variant_type_free (state->type);
739 g_free (state->l10n_context);
741 if (state->unparsed_default_value)
742 g_string_free (state->unparsed_default_value, TRUE);
744 if (state->default_value)
745 g_variant_unref (state->default_value);
747 if (state->strinfo)
748 g_string_free (state->strinfo, TRUE);
750 if (state->minimum)
751 g_variant_unref (state->minimum);
753 if (state->maximum)
754 g_variant_unref (state->maximum);
756 if (state->serialised)
757 g_variant_unref (state->serialised);
759 g_slice_free (KeyState, state);
762 /* Key name validity {{{1 */
763 static gboolean allow_any_name = FALSE;
765 static gboolean
766 is_valid_keyname (const gchar *key,
767 GError **error)
769 gint i;
771 if (key[0] == '\0')
773 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
774 _("empty names are not permitted"));
775 return FALSE;
778 if (allow_any_name)
779 return TRUE;
781 if (!g_ascii_islower (key[0]))
783 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
784 _("invalid name '%s': names must begin "
785 "with a lowercase letter"), key);
786 return FALSE;
789 for (i = 1; key[i]; i++)
791 if (key[i] != '-' &&
792 !g_ascii_islower (key[i]) &&
793 !g_ascii_isdigit (key[i]))
795 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
796 _("invalid name '%s': invalid character '%c'; "
797 "only lowercase letters, numbers and hyphen ('-') "
798 "are permitted."), key, key[i]);
799 return FALSE;
802 if (key[i] == '-' && key[i + 1] == '-')
804 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
805 _("invalid name '%s': two successive hyphens ('--') "
806 "are not permitted."), key);
807 return FALSE;
811 if (key[i - 1] == '-')
813 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
814 _("invalid name '%s': the last character may not be a "
815 "hyphen ('-')."), key);
816 return FALSE;
819 if (i > 1024)
821 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
822 _("invalid name '%s': maximum length is 1024"), key);
823 return FALSE;
826 return TRUE;
829 /* Handling of <schema> {{{1 */
830 typedef struct _SchemaState SchemaState;
831 struct _SchemaState
833 SchemaState *extends;
835 gchar *path;
836 gchar *gettext_domain;
837 gchar *extends_name;
838 gchar *list_of;
840 GHashTable *keys;
843 static SchemaState *
844 schema_state_new (const gchar *path,
845 const gchar *gettext_domain,
846 SchemaState *extends,
847 const gchar *extends_name,
848 const gchar *list_of)
850 SchemaState *state;
852 state = g_slice_new (SchemaState);
853 state->path = g_strdup (path);
854 state->gettext_domain = g_strdup (gettext_domain);
855 state->extends = extends;
856 state->extends_name = g_strdup (extends_name);
857 state->list_of = g_strdup (list_of);
858 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
859 g_free, key_state_free);
861 return state;
864 static void
865 schema_state_free (gpointer data)
867 SchemaState *state = data;
869 g_free (state->path);
870 g_free (state->gettext_domain);
871 g_hash_table_unref (state->keys);
874 static void
875 schema_state_add_child (SchemaState *state,
876 const gchar *name,
877 const gchar *schema,
878 GError **error)
880 gchar *childname;
882 if (!is_valid_keyname (name, error))
883 return;
885 childname = g_strconcat (name, "/", NULL);
887 if (g_hash_table_lookup (state->keys, childname))
889 g_set_error (error, G_MARKUP_ERROR,
890 G_MARKUP_ERROR_INVALID_CONTENT,
891 _("<child name='%s'> already specified"), name);
892 return;
895 g_hash_table_insert (state->keys, childname,
896 key_state_new_child (schema));
899 static KeyState *
900 schema_state_add_key (SchemaState *state,
901 GHashTable *enum_table,
902 GHashTable *flags_table,
903 const gchar *name,
904 const gchar *type_string,
905 const gchar *enum_type,
906 const gchar *flags_type,
907 GError **error)
909 SchemaState *node;
910 GString *strinfo;
911 KeyState *key;
913 if (state->list_of)
915 g_set_error_literal (error, G_MARKUP_ERROR,
916 G_MARKUP_ERROR_INVALID_CONTENT,
917 _("cannot add keys to a 'list-of' schema"));
918 return NULL;
921 if (!is_valid_keyname (name, error))
922 return NULL;
924 if (g_hash_table_lookup (state->keys, name))
926 g_set_error (error, G_MARKUP_ERROR,
927 G_MARKUP_ERROR_INVALID_CONTENT,
928 _("<key name='%s'> already specified"), name);
929 return NULL;
932 for (node = state; node; node = node->extends)
933 if (node->extends)
935 KeyState *shadow;
937 shadow = g_hash_table_lookup (node->extends->keys, name);
939 /* in case of <key> <override> <key> make sure we report the
940 * location of the original <key>, not the <override>.
942 if (shadow && !shadow->is_override)
944 g_set_error (error, G_MARKUP_ERROR,
945 G_MARKUP_ERROR_INVALID_CONTENT,
946 _("<key name='%s'> shadows <key name='%s'> in "
947 "<schema id='%s'>; use <override> to modify value"),
948 name, name, node->extends_name);
949 return NULL;
953 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
955 g_set_error (error, G_MARKUP_ERROR,
956 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
957 _("exactly one of 'type', 'enum' or 'flags' must "
958 "be specified as an attribute to <key>"));
959 return NULL;
962 if (type_string == NULL) /* flags or enums was specified */
964 EnumState *enum_state;
966 if (enum_type)
967 enum_state = g_hash_table_lookup (enum_table, enum_type);
968 else
969 enum_state = g_hash_table_lookup (flags_table, flags_type);
972 if (enum_state == NULL)
974 g_set_error (error, G_MARKUP_ERROR,
975 G_MARKUP_ERROR_INVALID_CONTENT,
976 _("<%s id='%s'> not (yet) defined."),
977 flags_type ? "flags" : "enum",
978 flags_type ? flags_type : enum_type);
979 return NULL;
982 type_string = flags_type ? "as" : "s";
983 strinfo = enum_state->strinfo;
985 else
987 if (!g_variant_type_string_is_valid (type_string))
989 g_set_error (error, G_MARKUP_ERROR,
990 G_MARKUP_ERROR_INVALID_CONTENT,
991 _("invalid GVariant type string '%s'"), type_string);
992 return NULL;
995 strinfo = NULL;
998 key = key_state_new (type_string, state->gettext_domain,
999 enum_type != NULL, flags_type != NULL, strinfo);
1000 g_hash_table_insert (state->keys, g_strdup (name), key);
1002 return key;
1005 static void
1006 schema_state_add_override (SchemaState *state,
1007 KeyState **key_state,
1008 GString **string,
1009 const gchar *key,
1010 const gchar *l10n,
1011 const gchar *context,
1012 GError **error)
1014 SchemaState *parent;
1015 KeyState *original;
1017 if (state->extends == NULL)
1019 g_set_error_literal (error, G_MARKUP_ERROR,
1020 G_MARKUP_ERROR_INVALID_CONTENT,
1021 _("<override> given but schema isn't "
1022 "extending anything"));
1023 return;
1026 for (parent = state->extends; parent; parent = parent->extends)
1027 if ((original = g_hash_table_lookup (parent->keys, key)))
1028 break;
1030 if (original == NULL)
1032 g_set_error (error, G_MARKUP_ERROR,
1033 G_MARKUP_ERROR_INVALID_CONTENT,
1034 _("no <key name='%s'> to override"), key);
1035 return;
1038 if (g_hash_table_lookup (state->keys, key))
1040 g_set_error (error, G_MARKUP_ERROR,
1041 G_MARKUP_ERROR_INVALID_CONTENT,
1042 _("<override name='%s'> already specified"), key);
1043 return;
1046 *key_state = key_state_override (original, state->gettext_domain);
1047 *string = key_state_start_default (*key_state, l10n, context, error);
1048 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1051 static void
1052 override_state_end (KeyState **key_state,
1053 GString **string,
1054 GError **error)
1056 key_state_end_default (*key_state, string, error);
1057 *key_state = NULL;
1060 /* Handling of toplevel state {{{1 */
1061 typedef struct
1063 GHashTable *schema_table; /* string -> SchemaState */
1064 GHashTable *flags_table; /* string -> EnumState */
1065 GHashTable *enum_table; /* string -> EnumState */
1067 GSList *this_file_schemas; /* strings: <schema>s in this file */
1068 GSList *this_file_flagss; /* strings: <flags>s in this file */
1069 GSList *this_file_enums; /* strings: <enum>s in this file */
1071 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1073 SchemaState *schema_state; /* non-NULL when inside <schema> */
1074 KeyState *key_state; /* non-NULL when inside <key> */
1075 EnumState *enum_state; /* non-NULL when inside <enum> */
1077 GString *string; /* non-NULL when accepting text */
1078 } ParseState;
1080 static gboolean
1081 is_subclass (const gchar *class_name,
1082 const gchar *possible_parent,
1083 GHashTable *schema_table)
1085 SchemaState *class;
1087 if (strcmp (class_name, possible_parent) == 0)
1088 return TRUE;
1090 class = g_hash_table_lookup (schema_table, class_name);
1091 g_assert (class != NULL);
1093 return class->extends_name &&
1094 is_subclass (class->extends_name, possible_parent, schema_table);
1097 static void
1098 parse_state_start_schema (ParseState *state,
1099 const gchar *id,
1100 const gchar *path,
1101 const gchar *gettext_domain,
1102 const gchar *extends_name,
1103 const gchar *list_of,
1104 GError **error)
1106 SchemaState *extends;
1107 gchar *my_id;
1109 if (g_hash_table_lookup (state->schema_table, id))
1111 g_set_error (error, G_MARKUP_ERROR,
1112 G_MARKUP_ERROR_INVALID_CONTENT,
1113 _("<schema id='%s'> already specified"), id);
1114 return;
1117 if (extends_name)
1119 extends = g_hash_table_lookup (state->schema_table, extends_name);
1121 if (extends == NULL)
1123 g_set_error (error, G_MARKUP_ERROR,
1124 G_MARKUP_ERROR_INVALID_CONTENT,
1125 _("<schema id='%s'> extends not yet existing "
1126 "schema '%s'"), id, extends_name);
1127 return;
1130 else
1131 extends = NULL;
1133 if (list_of)
1135 SchemaState *tmp;
1137 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1139 g_set_error (error, G_MARKUP_ERROR,
1140 G_MARKUP_ERROR_INVALID_CONTENT,
1141 _("<schema id='%s'> is list of not yet existing "
1142 "schema '%s'"), id, list_of);
1143 return;
1146 if (tmp->path)
1148 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1149 _("Can not be a list of a schema with a path"));
1150 return;
1154 if (extends)
1156 if (extends->path)
1158 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1159 _("Can not extend a schema with a path"));
1160 return;
1163 if (list_of)
1165 if (extends->list_of == NULL)
1167 g_set_error (error, G_MARKUP_ERROR,
1168 G_MARKUP_ERROR_INVALID_CONTENT,
1169 _("<schema id='%s'> is a list, extending "
1170 "<schema id='%s'> which is not a list"),
1171 id, extends_name);
1172 return;
1175 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1177 g_set_error (error, G_MARKUP_ERROR,
1178 G_MARKUP_ERROR_INVALID_CONTENT,
1179 _("<schema id='%s' list-of='%s'> extends <schema "
1180 "id='%s' list-of='%s'> but '%s' does not "
1181 "extend '%s'"), id, list_of, extends_name,
1182 extends->list_of, list_of, extends->list_of);
1183 return;
1186 else
1187 /* by default we are a list of the same thing that the schema
1188 * we are extending is a list of (which might be nothing)
1190 list_of = extends->list_of;
1193 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1195 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1196 _("a path, if given, must begin and end with a slash"));
1197 return;
1200 if (path && list_of && !g_str_has_suffix (path, ":/"))
1202 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1203 _("the path of a list must end with ':/'"));
1204 return;
1207 if (path && (g_str_has_prefix (path, "/apps/") ||
1208 g_str_has_prefix (path, "/desktop/") ||
1209 g_str_has_prefix (path, "/system/")))
1210 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1211 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1213 state->schema_state = schema_state_new (path, gettext_domain,
1214 extends, extends_name, list_of);
1216 my_id = g_strdup (id);
1217 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1218 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1221 static void
1222 parse_state_start_enum (ParseState *state,
1223 const gchar *id,
1224 gboolean is_flags,
1225 GError **error)
1227 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1228 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1229 gchar *my_id;
1231 if (g_hash_table_lookup (table, id))
1233 g_set_error (error, G_MARKUP_ERROR,
1234 G_MARKUP_ERROR_INVALID_CONTENT,
1235 _("<%s id='%s'> already specified"),
1236 is_flags ? "flags" : "enum", id);
1237 return;
1240 state->enum_state = enum_state_new (is_flags);
1242 my_id = g_strdup (id);
1243 *list = g_slist_prepend (*list, my_id);
1244 g_hash_table_insert (table, my_id, state->enum_state);
1247 /* GMarkup Parser Functions {{{1 */
1249 /* Start element {{{2 */
1250 static void
1251 start_element (GMarkupParseContext *context,
1252 const gchar *element_name,
1253 const gchar **attribute_names,
1254 const gchar **attribute_values,
1255 gpointer user_data,
1256 GError **error)
1258 ParseState *state = user_data;
1259 const GSList *element_stack;
1260 const gchar *container;
1262 element_stack = g_markup_parse_context_get_element_stack (context);
1263 container = element_stack->next ? element_stack->next->data : NULL;
1265 #define COLLECT(first, ...) \
1266 g_markup_collect_attributes (element_name, \
1267 attribute_names, attribute_values, error, \
1268 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1269 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1270 #define STRDUP G_MARKUP_COLLECT_STRDUP
1271 #define STRING G_MARKUP_COLLECT_STRING
1272 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1274 /* Toplevel items {{{3 */
1275 if (container == NULL)
1277 if (strcmp (element_name, "schemalist") == 0)
1279 COLLECT (OPTIONAL | STRDUP,
1280 "gettext-domain",
1281 &state->schemalist_domain);
1282 return;
1287 /* children of <schemalist> {{{3 */
1288 else if (strcmp (container, "schemalist") == 0)
1290 if (strcmp (element_name, "schema") == 0)
1292 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1293 if (COLLECT (STRING, "id", &id,
1294 OPTIONAL | STRING, "path", &path,
1295 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1296 OPTIONAL | STRING, "extends", &extends,
1297 OPTIONAL | STRING, "list-of", &list_of))
1298 parse_state_start_schema (state, id, path,
1299 gettext_domain ? gettext_domain
1300 : state->schemalist_domain,
1301 extends, list_of, error);
1302 return;
1305 else if (strcmp (element_name, "enum") == 0)
1307 const gchar *id;
1308 if (COLLECT (STRING, "id", &id))
1309 parse_state_start_enum (state, id, FALSE, error);
1310 return;
1313 else if (strcmp (element_name, "flags") == 0)
1315 const gchar *id;
1316 if (COLLECT (STRING, "id", &id))
1317 parse_state_start_enum (state, id, TRUE, error);
1318 return;
1323 /* children of <schema> {{{3 */
1324 else if (strcmp (container, "schema") == 0)
1326 if (strcmp (element_name, "key") == 0)
1328 const gchar *name, *type_string, *enum_type, *flags_type;
1330 if (COLLECT (STRING, "name", &name,
1331 OPTIONAL | STRING, "type", &type_string,
1332 OPTIONAL | STRING, "enum", &enum_type,
1333 OPTIONAL | STRING, "flags", &flags_type))
1335 state->key_state = schema_state_add_key (state->schema_state,
1336 state->enum_table,
1337 state->flags_table,
1338 name, type_string,
1339 enum_type, flags_type,
1340 error);
1341 return;
1343 else if (strcmp (element_name, "child") == 0)
1345 const gchar *name, *schema;
1347 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1348 schema_state_add_child (state->schema_state,
1349 name, schema, error);
1350 return;
1352 else if (strcmp (element_name, "override") == 0)
1354 const gchar *name, *l10n, *context;
1356 if (COLLECT (STRING, "name", &name,
1357 OPTIONAL | STRING, "l10n", &l10n,
1358 OPTIONAL | STRING, "context", &context))
1359 schema_state_add_override (state->schema_state,
1360 &state->key_state, &state->string,
1361 name, l10n, context, error);
1362 return;
1366 /* children of <key> {{{3 */
1367 else if (strcmp (container, "key") == 0)
1369 if (strcmp (element_name, "default") == 0)
1371 const gchar *l10n, *context;
1372 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1373 STRING | OPTIONAL, "context", &context))
1374 state->string = key_state_start_default (state->key_state,
1375 l10n, context, error);
1376 return;
1379 else if (strcmp (element_name, "summary") == 0 ||
1380 strcmp (element_name, "description") == 0)
1382 if (NO_ATTRS ())
1383 state->string = g_string_new (NULL);
1384 return;
1387 else if (strcmp (element_name, "range") == 0)
1389 const gchar *min, *max;
1390 if (COLLECT (STRING | OPTIONAL, "min", &min,
1391 STRING | OPTIONAL, "max", &max))
1392 key_state_set_range (state->key_state, min, max, error);
1393 return;
1396 else if (strcmp (element_name, "choices") == 0)
1398 if (NO_ATTRS ())
1399 key_state_start_choices (state->key_state, error);
1400 return;
1403 else if (strcmp (element_name, "aliases") == 0)
1405 if (NO_ATTRS ())
1406 key_state_start_aliases (state->key_state, error);
1407 return;
1412 /* children of <choices> {{{3 */
1413 else if (strcmp (container, "choices") == 0)
1415 if (strcmp (element_name, "choice") == 0)
1417 const gchar *value;
1418 if (COLLECT (STRING, "value", &value))
1419 key_state_add_choice (state->key_state, value, error);
1420 return;
1425 /* children of <aliases> {{{3 */
1426 else if (strcmp (container, "aliases") == 0)
1428 if (strcmp (element_name, "alias") == 0)
1430 const gchar *value, *target;
1431 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1432 key_state_add_alias (state->key_state, value, target, error);
1433 return;
1438 /* children of <enum> {{{3 */
1439 else if (strcmp (container, "enum") == 0 ||
1440 strcmp (container, "flags") == 0)
1442 if (strcmp (element_name, "value") == 0)
1444 const gchar *nick, *valuestr;
1445 if (COLLECT (STRING, "nick", &nick,
1446 STRING, "value", &valuestr))
1447 enum_state_add_value (state->enum_state, nick, valuestr, error);
1448 return;
1451 /* 3}}} */
1453 if (container)
1454 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1455 _("Element <%s> not allowed inside <%s>"),
1456 element_name, container);
1457 else
1458 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1459 _("Element <%s> not allowed at the top level"), element_name);
1461 /* 2}}} */
1462 /* End element {{{2 */
1464 static void
1465 key_state_end (KeyState **state_ptr,
1466 GError **error)
1468 KeyState *state;
1470 state = *state_ptr;
1471 *state_ptr = NULL;
1473 if (state->default_value == NULL)
1475 g_set_error_literal (error,
1476 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1477 "element <default> is required in <key>");
1478 return;
1482 static void
1483 schema_state_end (SchemaState **state_ptr,
1484 GError **error)
1486 *state_ptr = NULL;
1489 static void
1490 end_element (GMarkupParseContext *context,
1491 const gchar *element_name,
1492 gpointer user_data,
1493 GError **error)
1495 ParseState *state = user_data;
1497 if (strcmp (element_name, "schemalist") == 0)
1499 g_free (state->schemalist_domain);
1500 state->schemalist_domain = NULL;
1503 else if (strcmp (element_name, "enum") == 0 ||
1504 strcmp (element_name, "flags") == 0)
1505 enum_state_end (&state->enum_state, error);
1507 else if (strcmp (element_name, "schema") == 0)
1508 schema_state_end (&state->schema_state, error);
1510 else if (strcmp (element_name, "override") == 0)
1511 override_state_end (&state->key_state, &state->string, error);
1513 else if (strcmp (element_name, "key") == 0)
1514 key_state_end (&state->key_state, error);
1516 else if (strcmp (element_name, "default") == 0)
1517 key_state_end_default (state->key_state, &state->string, error);
1519 else if (strcmp (element_name, "choices") == 0)
1520 key_state_end_choices (state->key_state, error);
1522 else if (strcmp (element_name, "aliases") == 0)
1523 key_state_end_aliases (state->key_state, error);
1525 if (state->string)
1527 g_string_free (state->string, TRUE);
1528 state->string = NULL;
1531 /* Text {{{2 */
1532 static void
1533 text (GMarkupParseContext *context,
1534 const gchar *text,
1535 gsize text_len,
1536 gpointer user_data,
1537 GError **error)
1539 ParseState *state = user_data;
1541 if (state->string)
1543 /* we are expecting a string, so store the text data.
1545 * we store the data verbatim here and deal with whitespace
1546 * later on. there are two reasons for that:
1548 * 1) whitespace is handled differently depending on the tag
1549 * type.
1551 * 2) we could do leading whitespace removal by refusing to
1552 * insert it into state->string if it's at the start, but for
1553 * trailing whitespace, we have no idea if there is another
1554 * text() call coming or not.
1556 g_string_append_len (state->string, text, text_len);
1558 else
1560 /* string is not expected: accept (and ignore) pure whitespace */
1561 gsize i;
1563 for (i = 0; i < text_len; i++)
1564 if (!g_ascii_isspace (text[i]))
1566 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1567 _("text may not appear inside <%s>"),
1568 g_markup_parse_context_get_element (context));
1569 break;
1574 /* Write to GVDB {{{1 */
1575 typedef struct
1577 GHashTable *table;
1578 GvdbItem *root;
1579 } GvdbPair;
1581 static void
1582 gvdb_pair_init (GvdbPair *pair)
1584 pair->table = gvdb_hash_table_new (NULL, NULL);
1585 pair->root = gvdb_hash_table_insert (pair->table, "");
1588 typedef struct
1590 GHashTable *schema_table;
1591 GvdbPair root_pair;
1592 } WriteToFileData;
1594 typedef struct
1596 GHashTable *schema_table;
1597 GvdbPair pair;
1598 gboolean l10n;
1599 } OutputSchemaData;
1601 static void
1602 output_key (gpointer key,
1603 gpointer value,
1604 gpointer user_data)
1606 OutputSchemaData *data;
1607 const gchar *name;
1608 KeyState *state;
1609 GvdbItem *item;
1611 name = key;
1612 state = value;
1613 data = user_data;
1615 item = gvdb_hash_table_insert (data->pair.table, name);
1616 gvdb_item_set_parent (item, data->pair.root);
1617 gvdb_item_set_value (item, key_state_serialise (state));
1619 if (state->l10n)
1620 data->l10n = TRUE;
1622 if (state->child_schema &&
1623 !g_hash_table_lookup (data->schema_table, state->child_schema))
1624 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1625 state->child_schema);
1628 static void
1629 output_schema (gpointer key,
1630 gpointer value,
1631 gpointer user_data)
1633 WriteToFileData *wtf_data = user_data;
1634 OutputSchemaData data;
1635 GvdbPair *root_pair;
1636 SchemaState *state;
1637 const gchar *id;
1638 GvdbItem *item;
1640 id = key;
1641 state = value;
1642 root_pair = &wtf_data->root_pair;
1644 data.schema_table = wtf_data->schema_table;
1645 gvdb_pair_init (&data.pair);
1646 data.l10n = FALSE;
1648 item = gvdb_hash_table_insert (root_pair->table, id);
1649 gvdb_item_set_parent (item, root_pair->root);
1650 gvdb_item_set_hash_table (item, data.pair.table);
1652 g_hash_table_foreach (state->keys, output_key, &data);
1654 if (state->path)
1655 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1657 if (state->extends_name)
1658 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1659 state->extends_name);
1661 if (state->list_of)
1662 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1663 state->list_of);
1665 if (data.l10n)
1666 gvdb_hash_table_insert_string (data.pair.table,
1667 ".gettext-domain",
1668 state->gettext_domain);
1671 static gboolean
1672 write_to_file (GHashTable *schema_table,
1673 const gchar *filename,
1674 GError **error)
1676 WriteToFileData data;
1677 gboolean success;
1679 data.schema_table = schema_table;
1681 gvdb_pair_init (&data.root_pair);
1683 g_hash_table_foreach (schema_table, output_schema, &data);
1685 success = gvdb_table_write_contents (data.root_pair.table, filename,
1686 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1687 error);
1688 g_hash_table_unref (data.root_pair.table);
1690 return success;
1693 /* Parser driver {{{1 */
1694 static GHashTable *
1695 parse_gschema_files (gchar **files,
1696 gboolean strict)
1698 GMarkupParser parser = { start_element, end_element, text };
1699 ParseState state = { 0, };
1700 const gchar *filename;
1701 GError *error = NULL;
1703 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1704 g_free, enum_state_free);
1706 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1707 g_free, enum_state_free);
1709 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1710 g_free, schema_state_free);
1712 while ((filename = *files++) != NULL)
1714 GMarkupParseContext *context;
1715 gchar *contents;
1716 gsize size;
1718 if (!g_file_get_contents (filename, &contents, &size, &error))
1720 fprintf (stderr, "%s\n", error->message);
1721 g_clear_error (&error);
1722 continue;
1725 context = g_markup_parse_context_new (&parser,
1726 G_MARKUP_TREAT_CDATA_AS_TEXT |
1727 G_MARKUP_PREFIX_ERROR_POSITION |
1728 G_MARKUP_IGNORE_QUALIFIED,
1729 &state, NULL);
1732 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1733 !g_markup_parse_context_end_parse (context, &error))
1735 GSList *item;
1737 /* back out any changes from this file */
1738 for (item = state.this_file_schemas; item; item = item->next)
1739 g_hash_table_remove (state.schema_table, item->data);
1741 for (item = state.this_file_flagss; item; item = item->next)
1742 g_hash_table_remove (state.flags_table, item->data);
1744 for (item = state.this_file_enums; item; item = item->next)
1745 g_hash_table_remove (state.enum_table, item->data);
1747 /* let them know */
1748 fprintf (stderr, "%s: %s. ", filename, error->message);
1749 g_clear_error (&error);
1751 if (strict)
1753 /* Translators: Do not translate "--strict". */
1754 fprintf (stderr, _("--strict was specified; exiting.\n"));
1755 g_hash_table_unref (state.schema_table);
1756 g_hash_table_unref (state.flags_table);
1757 g_hash_table_unref (state.enum_table);
1759 return NULL;
1761 else
1762 fprintf (stderr, _("This entire file has been ignored.\n"));
1765 /* cleanup */
1766 g_markup_parse_context_free (context);
1767 g_slist_free (state.this_file_schemas);
1768 g_slist_free (state.this_file_flagss);
1769 g_slist_free (state.this_file_enums);
1770 state.this_file_schemas = NULL;
1771 state.this_file_flagss = NULL;
1772 state.this_file_enums = NULL;
1775 g_hash_table_unref (state.flags_table);
1776 g_hash_table_unref (state.enum_table);
1778 return state.schema_table;
1781 static gint
1782 compare_strings (gconstpointer a,
1783 gconstpointer b)
1785 gchar *one = *(gchar **) a;
1786 gchar *two = *(gchar **) b;
1787 gint cmp;
1789 cmp = g_str_has_suffix (two, ".enums.xml") -
1790 g_str_has_suffix (one, ".enums.xml");
1792 if (!cmp)
1793 cmp = strcmp (one, two);
1795 return cmp;
1798 static gboolean
1799 set_overrides (GHashTable *schema_table,
1800 gchar **files,
1801 gboolean strict)
1803 const gchar *filename;
1804 GError *error = NULL;
1806 while ((filename = *files++))
1808 GKeyFile *key_file;
1809 gchar **groups;
1810 gint i;
1812 key_file = g_key_file_new ();
1813 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1815 fprintf (stderr, "%s: %s. ", filename, error->message);
1816 g_key_file_free (key_file);
1817 g_clear_error (&error);
1819 if (!strict)
1821 fprintf (stderr, _("Ignoring this file.\n"));
1822 continue;
1825 fprintf (stderr, _("--strict was specified; exiting.\n"));
1826 return FALSE;
1829 groups = g_key_file_get_groups (key_file, NULL);
1831 for (i = 0; groups[i]; i++)
1833 const gchar *group = groups[i];
1834 SchemaState *schema;
1835 gchar **keys;
1836 gint j;
1838 schema = g_hash_table_lookup (schema_table, group);
1840 if (schema == NULL)
1841 /* Having the schema not be installed is expected to be a
1842 * common case. Don't even emit an error message about
1843 * that.
1845 continue;
1847 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1848 g_assert (keys != NULL);
1850 for (j = 0; keys[j]; j++)
1852 const gchar *key = keys[j];
1853 KeyState *state;
1854 GVariant *value;
1855 gchar *string;
1857 state = g_hash_table_lookup (schema->keys, key);
1859 if (state == NULL)
1861 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1862 "specified in override file '%s'"),
1863 key, group, filename);
1865 if (!strict)
1867 fprintf (stderr, _("; ignoring override for this key.\n"));
1868 continue;
1871 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1872 g_key_file_free (key_file);
1873 g_strfreev (groups);
1874 g_strfreev (keys);
1876 return FALSE;
1879 string = g_key_file_get_value (key_file, group, key, NULL);
1880 g_assert (string != NULL);
1882 value = g_variant_parse (state->type, string,
1883 NULL, NULL, &error);
1885 if (value == NULL)
1887 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1888 "as specified in override file '%s': "
1889 "%s."),
1890 key, group, filename, error->message);
1892 g_clear_error (&error);
1893 g_free (string);
1895 if (!strict)
1897 fprintf (stderr, _("Ignoring override for this key.\n"));
1898 continue;
1901 fprintf (stderr, _("--strict was specified; exiting.\n"));
1902 g_key_file_free (key_file);
1903 g_strfreev (groups);
1904 g_strfreev (keys);
1906 return FALSE;
1909 if (state->minimum)
1911 if (g_variant_compare (value, state->minimum) < 0 ||
1912 g_variant_compare (value, state->maximum) > 0)
1914 fprintf (stderr,
1915 _("override for key '%s' in schema '%s' in "
1916 "override file '%s' is outside the range "
1917 "given in the schema"),
1918 key, group, filename);
1920 g_variant_unref (value);
1921 g_free (string);
1923 if (!strict)
1925 fprintf (stderr, _("; ignoring override for this key.\n"));
1926 continue;
1929 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1930 g_key_file_free (key_file);
1931 g_strfreev (groups);
1932 g_strfreev (keys);
1934 return FALSE;
1938 else if (state->strinfo->len)
1940 if (!is_valid_choices (value, state->strinfo))
1942 fprintf (stderr,
1943 _("override for key '%s' in schema '%s' in "
1944 "override file '%s' is not in the list "
1945 "of valid choices"),
1946 key, group, filename);
1948 g_variant_unref (value);
1949 g_free (string);
1951 if (!strict)
1953 fprintf (stderr, _("; ignoring override for this key.\n"));
1954 continue;
1957 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1958 g_key_file_free (key_file);
1959 g_strfreev (groups);
1960 g_strfreev (keys);
1962 return FALSE;
1966 g_variant_unref (state->default_value);
1967 state->default_value = value;
1968 g_free (string);
1971 g_strfreev (keys);
1974 g_strfreev (groups);
1977 return TRUE;
1981 main (int argc, char **argv)
1983 GError *error;
1984 GHashTable *table;
1985 GDir *dir;
1986 const gchar *file;
1987 gchar *srcdir;
1988 gchar *targetdir = NULL;
1989 gchar *target;
1990 gboolean dry_run = FALSE;
1991 gboolean strict = FALSE;
1992 gchar **schema_files = NULL;
1993 gchar **override_files = NULL;
1994 GOptionContext *context;
1995 GOptionEntry entries[] = {
1996 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1997 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1998 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1999 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2001 /* These options are only for use in the gschema-compile tests */
2002 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2003 { NULL }
2006 #ifdef G_OS_WIN32
2007 gchar *tmp;
2008 #endif
2010 setlocale (LC_ALL, "");
2011 textdomain (GETTEXT_PACKAGE);
2013 #ifdef G_OS_WIN32
2014 tmp = _glib_get_locale_dir ();
2015 bindtextdomain (GETTEXT_PACKAGE, tmp);
2016 g_free (tmp);
2017 #else
2018 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2019 #endif
2021 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2022 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2023 #endif
2025 context = g_option_context_new (N_("DIRECTORY"));
2026 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2027 g_option_context_set_summary (context,
2028 N_("Compile all GSettings schema files into a schema cache.\n"
2029 "Schema files are required to have the extension .gschema.xml,\n"
2030 "and the cache file is called gschemas.compiled."));
2031 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2033 error = NULL;
2034 if (!g_option_context_parse (context, &argc, &argv, &error))
2036 fprintf (stderr, "%s\n", error->message);
2037 return 1;
2040 g_option_context_free (context);
2042 if (!schema_files && argc != 2)
2044 fprintf (stderr, _("You should give exactly one directory name\n"));
2045 return 1;
2048 srcdir = argv[1];
2050 if (targetdir == NULL)
2051 targetdir = srcdir;
2053 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2055 if (!schema_files)
2057 GPtrArray *overrides;
2058 GPtrArray *files;
2060 files = g_ptr_array_new ();
2061 overrides = g_ptr_array_new ();
2063 dir = g_dir_open (srcdir, 0, &error);
2064 if (dir == NULL)
2066 fprintf (stderr, "%s\n", error->message);
2067 return 1;
2070 while ((file = g_dir_read_name (dir)) != NULL)
2072 if (g_str_has_suffix (file, ".gschema.xml") ||
2073 g_str_has_suffix (file, ".enums.xml"))
2074 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2076 else if (g_str_has_suffix (file, ".gschema.override"))
2077 g_ptr_array_add (overrides,
2078 g_build_filename (srcdir, file, NULL));
2081 if (files->len == 0)
2083 fprintf (stdout, _("No schema files found: "));
2085 if (g_unlink (target))
2086 fprintf (stdout, _("doing nothing.\n"));
2088 else
2089 fprintf (stdout, _("removed existing output file.\n"));
2091 return 0;
2093 g_ptr_array_sort (files, compare_strings);
2094 g_ptr_array_add (files, NULL);
2096 g_ptr_array_sort (overrides, compare_strings);
2097 g_ptr_array_add (overrides, NULL);
2099 schema_files = (char **) g_ptr_array_free (files, FALSE);
2100 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2103 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2105 g_free (target);
2106 return 1;
2109 if (override_files != NULL &&
2110 !set_overrides (table, override_files, strict))
2112 g_free (target);
2113 return 1;
2116 if (!dry_run && !write_to_file (table, target, &error))
2118 fprintf (stderr, "%s\n", error->message);
2119 g_free (target);
2120 return 1;
2123 g_free (target);
2125 return 0;
2128 /* Epilogue {{{1 */
2130 /* vim:set foldmethod=marker: */