docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / glib-compile-schemas.c
blobcf02389c217ea3534de39b1a3162bf6e13166942
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 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 #include "gvdb/gvdb-builder.h"
37 #include "strinfo.c"
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 dash ('-') "
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 dashes ('--') "
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 "dash ('-')."), 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 "
1126 "existing 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 "
1142 "existing 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 state->schema_state = schema_state_new (path, gettext_domain,
1208 extends, extends_name, list_of);
1210 my_id = g_strdup (id);
1211 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1212 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1215 static void
1216 parse_state_start_enum (ParseState *state,
1217 const gchar *id,
1218 gboolean is_flags,
1219 GError **error)
1221 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1222 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1223 gchar *my_id;
1225 if (g_hash_table_lookup (table, id))
1227 g_set_error (error, G_MARKUP_ERROR,
1228 G_MARKUP_ERROR_INVALID_CONTENT,
1229 _("<%s id='%s'> already specified"),
1230 is_flags ? "flags" : "enum", id);
1231 return;
1234 state->enum_state = enum_state_new (is_flags);
1236 my_id = g_strdup (id);
1237 *list = g_slist_prepend (*list, my_id);
1238 g_hash_table_insert (table, my_id, state->enum_state);
1241 /* GMarkup Parser Functions {{{1 */
1243 /* Start element {{{2 */
1244 static void
1245 start_element (GMarkupParseContext *context,
1246 const gchar *element_name,
1247 const gchar **attribute_names,
1248 const gchar **attribute_values,
1249 gpointer user_data,
1250 GError **error)
1252 ParseState *state = user_data;
1253 const GSList *element_stack;
1254 const gchar *container;
1256 element_stack = g_markup_parse_context_get_element_stack (context);
1257 container = element_stack->next ? element_stack->next->data : NULL;
1259 #define COLLECT(first, ...) \
1260 g_markup_collect_attributes (element_name, \
1261 attribute_names, attribute_values, error, \
1262 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1263 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1264 #define STRDUP G_MARKUP_COLLECT_STRDUP
1265 #define STRING G_MARKUP_COLLECT_STRING
1266 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1268 /* Toplevel items {{{3 */
1269 if (container == NULL)
1271 if (strcmp (element_name, "schemalist") == 0)
1273 COLLECT (OPTIONAL | STRDUP,
1274 "gettext-domain",
1275 &state->schemalist_domain);
1276 return;
1281 /* children of <schemalist> {{{3 */
1282 else if (strcmp (container, "schemalist") == 0)
1284 if (strcmp (element_name, "schema") == 0)
1286 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1287 if (COLLECT (STRING, "id", &id,
1288 OPTIONAL | STRING, "path", &path,
1289 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1290 OPTIONAL | STRING, "extends", &extends,
1291 OPTIONAL | STRING, "list-of", &list_of))
1292 parse_state_start_schema (state, id, path,
1293 gettext_domain ? gettext_domain
1294 : state->schemalist_domain,
1295 extends, list_of, error);
1296 return;
1299 else if (strcmp (element_name, "enum") == 0)
1301 const gchar *id;
1302 if (COLLECT (STRING, "id", &id))
1303 parse_state_start_enum (state, id, FALSE, error);
1304 return;
1307 else if (strcmp (element_name, "flags") == 0)
1309 const gchar *id;
1310 if (COLLECT (STRING, "id", &id))
1311 parse_state_start_enum (state, id, TRUE, error);
1312 return;
1317 /* children of <schema> {{{3 */
1318 else if (strcmp (container, "schema") == 0)
1320 if (strcmp (element_name, "key") == 0)
1322 const gchar *name, *type_string, *enum_type, *flags_type;
1324 if (COLLECT (STRING, "name", &name,
1325 OPTIONAL | STRING, "type", &type_string,
1326 OPTIONAL | STRING, "enum", &enum_type,
1327 OPTIONAL | STRING, "flags", &flags_type))
1329 state->key_state = schema_state_add_key (state->schema_state,
1330 state->enum_table,
1331 state->flags_table,
1332 name, type_string,
1333 enum_type, flags_type,
1334 error);
1335 return;
1337 else if (strcmp (element_name, "child") == 0)
1339 const gchar *name, *schema;
1341 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1342 schema_state_add_child (state->schema_state,
1343 name, schema, error);
1344 return;
1346 else if (strcmp (element_name, "override") == 0)
1348 const gchar *name, *l10n, *context;
1350 if (COLLECT (STRING, "name", &name,
1351 OPTIONAL | STRING, "l10n", &l10n,
1352 OPTIONAL | STRING, "context", &context))
1353 schema_state_add_override (state->schema_state,
1354 &state->key_state, &state->string,
1355 name, l10n, context, error);
1356 return;
1360 /* children of <key> {{{3 */
1361 else if (strcmp (container, "key") == 0)
1363 if (strcmp (element_name, "default") == 0)
1365 const gchar *l10n, *context;
1366 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1367 STRING | OPTIONAL, "context", &context))
1368 state->string = key_state_start_default (state->key_state,
1369 l10n, context, error);
1370 return;
1373 else if (strcmp (element_name, "summary") == 0 ||
1374 strcmp (element_name, "description") == 0)
1376 if (NO_ATTRS ())
1377 state->string = g_string_new (NULL);
1378 return;
1381 else if (strcmp (element_name, "range") == 0)
1383 const gchar *min, *max;
1384 if (COLLECT (STRING | OPTIONAL, "min", &min,
1385 STRING | OPTIONAL, "max", &max))
1386 key_state_set_range (state->key_state, min, max, error);
1387 return;
1390 else if (strcmp (element_name, "choices") == 0)
1392 if (NO_ATTRS ())
1393 key_state_start_choices (state->key_state, error);
1394 return;
1397 else if (strcmp (element_name, "aliases") == 0)
1399 if (NO_ATTRS ())
1400 key_state_start_aliases (state->key_state, error);
1401 return;
1406 /* children of <choices> {{{3 */
1407 else if (strcmp (container, "choices") == 0)
1409 if (strcmp (element_name, "choice") == 0)
1411 const gchar *value;
1412 if (COLLECT (STRING, "value", &value))
1413 key_state_add_choice (state->key_state, value, error);
1414 return;
1419 /* children of <aliases> {{{3 */
1420 else if (strcmp (container, "aliases") == 0)
1422 if (strcmp (element_name, "alias") == 0)
1424 const gchar *value, *target;
1425 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1426 key_state_add_alias (state->key_state, value, target, error);
1427 return;
1432 /* children of <enum> {{{3 */
1433 else if (strcmp (container, "enum") == 0 ||
1434 strcmp (container, "flags") == 0)
1436 if (strcmp (element_name, "value") == 0)
1438 const gchar *nick, *valuestr;
1439 if (COLLECT (STRING, "nick", &nick,
1440 STRING, "value", &valuestr))
1441 enum_state_add_value (state->enum_state, nick, valuestr, error);
1442 return;
1445 /* 3}}} */
1447 if (container)
1448 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1449 _("Element <%s> not allowed inside <%s>"),
1450 element_name, container);
1451 else
1452 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1453 _("Element <%s> not allowed at toplevel"), element_name);
1455 /* 2}}} */
1456 /* End element {{{2 */
1458 static void
1459 key_state_end (KeyState **state_ptr,
1460 GError **error)
1462 KeyState *state;
1464 state = *state_ptr;
1465 *state_ptr = NULL;
1467 if (state->default_value == NULL)
1469 g_set_error_literal (error,
1470 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1471 "element <default> is required in <key>");
1472 return;
1476 static void
1477 schema_state_end (SchemaState **state_ptr,
1478 GError **error)
1480 *state_ptr = NULL;
1483 static void
1484 end_element (GMarkupParseContext *context,
1485 const gchar *element_name,
1486 gpointer user_data,
1487 GError **error)
1489 ParseState *state = user_data;
1491 if (strcmp (element_name, "schemalist") == 0)
1493 g_free (state->schemalist_domain);
1494 state->schemalist_domain = NULL;
1497 else if (strcmp (element_name, "enum") == 0 ||
1498 strcmp (element_name, "flags") == 0)
1499 enum_state_end (&state->enum_state, error);
1501 else if (strcmp (element_name, "schema") == 0)
1502 schema_state_end (&state->schema_state, error);
1504 else if (strcmp (element_name, "override") == 0)
1505 override_state_end (&state->key_state, &state->string, error);
1507 else if (strcmp (element_name, "key") == 0)
1508 key_state_end (&state->key_state, error);
1510 else if (strcmp (element_name, "default") == 0)
1511 key_state_end_default (state->key_state, &state->string, error);
1513 else if (strcmp (element_name, "choices") == 0)
1514 key_state_end_choices (state->key_state, error);
1516 else if (strcmp (element_name, "aliases") == 0)
1517 key_state_end_aliases (state->key_state, error);
1519 if (state->string)
1521 g_string_free (state->string, TRUE);
1522 state->string = NULL;
1525 /* Text {{{2 */
1526 static void
1527 text (GMarkupParseContext *context,
1528 const gchar *text,
1529 gsize text_len,
1530 gpointer user_data,
1531 GError **error)
1533 ParseState *state = user_data;
1535 if (state->string)
1537 /* we are expecting a string, so store the text data.
1539 * we store the data verbatim here and deal with whitespace
1540 * later on. there are two reasons for that:
1542 * 1) whitespace is handled differently depending on the tag
1543 * type.
1545 * 2) we could do leading whitespace removal by refusing to
1546 * insert it into state->string if it's at the start, but for
1547 * trailing whitespace, we have no idea if there is another
1548 * text() call coming or not.
1550 g_string_append_len (state->string, text, text_len);
1552 else
1554 /* string is not expected: accept (and ignore) pure whitespace */
1555 gsize i;
1557 for (i = 0; i < text_len; i++)
1558 if (!g_ascii_isspace (text[i]))
1560 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1561 _("text may not appear inside <%s>"),
1562 g_markup_parse_context_get_element (context));
1563 break;
1568 /* Write to GVDB {{{1 */
1569 typedef struct
1571 GHashTable *table;
1572 GvdbItem *root;
1573 } GvdbPair;
1575 static void
1576 gvdb_pair_init (GvdbPair *pair)
1578 pair->table = gvdb_hash_table_new (NULL, NULL);
1579 pair->root = gvdb_hash_table_insert (pair->table, "");
1582 typedef struct
1584 GHashTable *schema_table;
1585 GvdbPair root_pair;
1586 } WriteToFileData;
1588 typedef struct
1590 GHashTable *schema_table;
1591 GvdbPair pair;
1592 gboolean l10n;
1593 } OutputSchemaData;
1595 static void
1596 output_key (gpointer key,
1597 gpointer value,
1598 gpointer user_data)
1600 OutputSchemaData *data;
1601 const gchar *name;
1602 KeyState *state;
1603 GvdbItem *item;
1605 name = key;
1606 state = value;
1607 data = user_data;
1609 item = gvdb_hash_table_insert (data->pair.table, name);
1610 gvdb_item_set_parent (item, data->pair.root);
1611 gvdb_item_set_value (item, key_state_serialise (state));
1613 if (state->l10n)
1614 data->l10n = TRUE;
1616 if (state->child_schema &&
1617 !g_hash_table_lookup (data->schema_table, state->child_schema))
1618 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1619 state->child_schema);
1622 static void
1623 output_schema (gpointer key,
1624 gpointer value,
1625 gpointer user_data)
1627 WriteToFileData *wtf_data = user_data;
1628 OutputSchemaData data;
1629 GvdbPair *root_pair;
1630 SchemaState *state;
1631 const gchar *id;
1632 GvdbItem *item;
1634 id = key;
1635 state = value;
1636 root_pair = &wtf_data->root_pair;
1638 data.schema_table = wtf_data->schema_table;
1639 gvdb_pair_init (&data.pair);
1640 data.l10n = FALSE;
1642 item = gvdb_hash_table_insert (root_pair->table, id);
1643 gvdb_item_set_parent (item, root_pair->root);
1644 gvdb_item_set_hash_table (item, data.pair.table);
1646 g_hash_table_foreach (state->keys, output_key, &data);
1648 if (state->path)
1649 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1651 if (state->extends_name)
1652 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1653 state->extends_name);
1655 if (state->list_of)
1656 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1657 state->list_of);
1659 if (data.l10n)
1660 gvdb_hash_table_insert_string (data.pair.table,
1661 ".gettext-domain",
1662 state->gettext_domain);
1665 static gboolean
1666 write_to_file (GHashTable *schema_table,
1667 const gchar *filename,
1668 GError **error)
1670 WriteToFileData data;
1671 gboolean success;
1673 data.schema_table = schema_table;
1675 gvdb_pair_init (&data.root_pair);
1677 g_hash_table_foreach (schema_table, output_schema, &data);
1679 success = gvdb_table_write_contents (data.root_pair.table, filename,
1680 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1681 error);
1682 g_hash_table_unref (data.root_pair.table);
1684 return success;
1687 /* Parser driver {{{1 */
1688 static GHashTable *
1689 parse_gschema_files (gchar **files,
1690 gboolean strict)
1692 GMarkupParser parser = { start_element, end_element, text };
1693 ParseState state = { 0, };
1694 const gchar *filename;
1695 GError *error = NULL;
1697 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1698 g_free, enum_state_free);
1700 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1701 g_free, enum_state_free);
1703 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1704 g_free, schema_state_free);
1706 while ((filename = *files++) != NULL)
1708 GMarkupParseContext *context;
1709 gchar *contents;
1710 gsize size;
1712 if (!g_file_get_contents (filename, &contents, &size, &error))
1714 fprintf (stderr, "%s\n", error->message);
1715 g_clear_error (&error);
1716 continue;
1719 context = g_markup_parse_context_new (&parser,
1720 G_MARKUP_TREAT_CDATA_AS_TEXT |
1721 G_MARKUP_PREFIX_ERROR_POSITION,
1722 &state, NULL);
1725 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1726 !g_markup_parse_context_end_parse (context, &error))
1728 GSList *item;
1730 /* back out any changes from this file */
1731 for (item = state.this_file_schemas; item; item = item->next)
1732 g_hash_table_remove (state.schema_table, item->data);
1734 for (item = state.this_file_flagss; item; item = item->next)
1735 g_hash_table_remove (state.flags_table, item->data);
1737 for (item = state.this_file_enums; item; item = item->next)
1738 g_hash_table_remove (state.enum_table, item->data);
1740 /* let them know */
1741 fprintf (stderr, "%s: %s. ", filename, error->message);
1742 g_clear_error (&error);
1744 if (strict)
1746 /* Translators: Do not translate "--strict". */
1747 fprintf (stderr, _("--strict was specified; exiting.\n"));
1748 g_hash_table_unref (state.schema_table);
1749 g_hash_table_unref (state.flags_table);
1750 g_hash_table_unref (state.enum_table);
1752 return NULL;
1754 else
1755 fprintf (stderr, _("This entire file has been ignored.\n"));
1758 /* cleanup */
1759 g_markup_parse_context_free (context);
1760 g_slist_free (state.this_file_schemas);
1761 g_slist_free (state.this_file_flagss);
1762 g_slist_free (state.this_file_enums);
1763 state.this_file_schemas = NULL;
1764 state.this_file_flagss = NULL;
1765 state.this_file_enums = NULL;
1768 g_hash_table_unref (state.flags_table);
1769 g_hash_table_unref (state.enum_table);
1771 return state.schema_table;
1774 static gint
1775 compare_strings (gconstpointer a,
1776 gconstpointer b)
1778 gchar *one = *(gchar **) a;
1779 gchar *two = *(gchar **) b;
1780 gint cmp;
1782 cmp = g_str_has_suffix (two, ".enums.xml") -
1783 g_str_has_suffix (one, ".enums.xml");
1785 if (!cmp)
1786 cmp = strcmp (one, two);
1788 return cmp;
1791 static gboolean
1792 set_overrides (GHashTable *schema_table,
1793 gchar **files,
1794 gboolean strict)
1796 const gchar *filename;
1797 GError *error = NULL;
1799 while ((filename = *files++))
1801 GKeyFile *key_file;
1802 gchar **groups;
1803 gint i;
1805 key_file = g_key_file_new ();
1806 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1808 fprintf (stderr, "%s: %s. ", filename, error->message);
1809 g_key_file_free (key_file);
1810 g_clear_error (&error);
1812 if (!strict)
1814 fprintf (stderr, _("Ignoring this file.\n"));
1815 continue;
1818 fprintf (stderr, _("--strict was specified; exiting.\n"));
1819 return FALSE;
1822 groups = g_key_file_get_groups (key_file, NULL);
1824 for (i = 0; groups[i]; i++)
1826 const gchar *group = groups[i];
1827 SchemaState *schema;
1828 gchar **keys;
1829 gint j;
1831 schema = g_hash_table_lookup (schema_table, group);
1833 if (schema == NULL)
1834 /* Having the schema not be installed is expected to be a
1835 * common case. Don't even emit an error message about
1836 * that.
1838 continue;
1840 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1841 g_assert (keys != NULL);
1843 for (j = 0; keys[j]; j++)
1845 const gchar *key = keys[j];
1846 KeyState *state;
1847 GVariant *value;
1848 gchar *string;
1850 state = g_hash_table_lookup (schema->keys, key);
1852 if (state == NULL)
1854 fprintf (stderr, _("No such key `%s' in schema `%s' as "
1855 "specified in override file `%s'"),
1856 key, group, filename);
1858 if (!strict)
1860 fprintf (stderr, _("; ignoring override for this key.\n"));
1861 continue;
1864 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1865 g_key_file_free (key_file);
1866 g_strfreev (groups);
1867 g_strfreev (keys);
1869 return FALSE;
1872 string = g_key_file_get_value (key_file, group, key, NULL);
1873 g_assert (string != NULL);
1875 value = g_variant_parse (state->type, string,
1876 NULL, NULL, &error);
1878 if (value == NULL)
1880 fprintf (stderr, _("error parsing key `%s' in schema `%s' "
1881 "as specified in override file `%s': "
1882 "%s. "),
1883 key, group, filename, error->message);
1885 g_clear_error (&error);
1886 g_free (string);
1888 if (!strict)
1890 fprintf (stderr, _("Ignoring override for this key.\n"));
1891 continue;
1894 fprintf (stderr, _("--strict was specified; exiting.\n"));
1895 g_key_file_free (key_file);
1896 g_strfreev (groups);
1897 g_strfreev (keys);
1899 return FALSE;
1902 if (state->minimum)
1904 if (g_variant_compare (value, state->minimum) < 0 ||
1905 g_variant_compare (value, state->maximum) > 0)
1907 fprintf (stderr,
1908 _("override for key `%s' in schema `%s' in "
1909 "override file `%s' is out of the range "
1910 "given in the schema"),
1911 key, group, filename);
1913 g_variant_unref (value);
1914 g_free (string);
1916 if (!strict)
1918 fprintf (stderr, _("; ignoring override for this key.\n"));
1919 continue;
1922 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1923 g_key_file_free (key_file);
1924 g_strfreev (groups);
1925 g_strfreev (keys);
1927 return FALSE;
1931 else if (state->strinfo->len)
1933 if (!is_valid_choices (value, state->strinfo))
1935 fprintf (stderr,
1936 _("override for key `%s' in schema `%s' in "
1937 "override file `%s' is not in the list "
1938 "of valid choices"),
1939 key, group, filename);
1941 g_variant_unref (value);
1942 g_free (string);
1944 if (!strict)
1946 fprintf (stderr, _("; ignoring override for this key.\n"));
1947 continue;
1950 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1951 g_key_file_free (key_file);
1952 g_strfreev (groups);
1953 g_strfreev (keys);
1955 return FALSE;
1959 g_variant_unref (state->default_value);
1960 state->default_value = value;
1961 g_free (string);
1964 g_strfreev (keys);
1967 g_strfreev (groups);
1970 return TRUE;
1974 main (int argc, char **argv)
1976 GError *error;
1977 GHashTable *table;
1978 GDir *dir;
1979 const gchar *file;
1980 gchar *srcdir;
1981 gchar *targetdir = NULL;
1982 gchar *target;
1983 gboolean dry_run = FALSE;
1984 gboolean strict = FALSE;
1985 gchar **schema_files = NULL;
1986 gchar **override_files = NULL;
1987 GOptionContext *context;
1988 GOptionEntry entries[] = {
1989 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1990 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1991 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1992 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1994 /* These options are only for use in the gschema-compile tests */
1995 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1996 { NULL }
1999 #ifdef G_OS_WIN32
2000 extern gchar *_glib_get_locale_dir (void);
2001 gchar *tmp;
2002 #endif
2004 setlocale (LC_ALL, "");
2005 textdomain (GETTEXT_PACKAGE);
2007 #ifdef G_OS_WIN32
2008 tmp = _glib_get_locale_dir ();
2009 bindtextdomain (GETTEXT_PACKAGE, tmp);
2010 g_free (tmp);
2011 #else
2012 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2013 #endif
2015 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2016 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2017 #endif
2019 context = g_option_context_new (N_("DIRECTORY"));
2020 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2021 g_option_context_set_summary (context,
2022 N_("Compile all GSettings schema files into a schema cache.\n"
2023 "Schema files are required to have the extension .gschema.xml,\n"
2024 "and the cache file is called gschemas.compiled."));
2025 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2027 error = NULL;
2028 if (!g_option_context_parse (context, &argc, &argv, &error))
2030 fprintf (stderr, "%s\n", error->message);
2031 return 1;
2034 g_option_context_free (context);
2036 if (!schema_files && argc != 2)
2038 fprintf (stderr, _("You should give exactly one directory name\n"));
2039 return 1;
2042 srcdir = argv[1];
2044 if (targetdir == NULL)
2045 targetdir = srcdir;
2047 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2049 if (!schema_files)
2051 GPtrArray *overrides;
2052 GPtrArray *files;
2054 files = g_ptr_array_new ();
2055 overrides = g_ptr_array_new ();
2057 dir = g_dir_open (srcdir, 0, &error);
2058 if (dir == NULL)
2060 fprintf (stderr, "%s\n", error->message);
2061 return 1;
2064 while ((file = g_dir_read_name (dir)) != NULL)
2066 if (g_str_has_suffix (file, ".gschema.xml") ||
2067 g_str_has_suffix (file, ".enums.xml"))
2068 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2070 else if (g_str_has_suffix (file, ".gschema.override"))
2071 g_ptr_array_add (overrides,
2072 g_build_filename (srcdir, file, NULL));
2075 if (files->len == 0)
2077 fprintf (stdout, _("No schema files found: "));
2079 if (g_unlink (target))
2080 fprintf (stdout, _("doing nothing.\n"));
2082 else
2083 fprintf (stdout, _("removed existing output file.\n"));
2085 return 0;
2087 g_ptr_array_sort (files, compare_strings);
2088 g_ptr_array_add (files, NULL);
2090 g_ptr_array_sort (overrides, compare_strings);
2091 g_ptr_array_add (overrides, NULL);
2093 schema_files = (char **) g_ptr_array_free (files, FALSE);
2094 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2097 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2099 g_free (target);
2100 return 1;
2103 if (override_files != NULL &&
2104 !set_overrides (table, override_files, strict))
2106 g_free (target);
2107 return 1;
2110 if (!dry_run && !write_to_file (table, target, &error))
2112 fprintf (stderr, "%s\n", error->message);
2113 g_free (target);
2114 return 1;
2117 g_free (target);
2119 return 0;
2122 /* Epilogue {{{1 */
2124 /* vim:set foldmethod=marker: */