A simple test for polling on a file-like descriptor
[glib.git] / gio / glib-compile-schemas.c
blobe42949b85d540ba7c52c264faacce3bdee7da5d8
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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 /* Prologue {{{1 */
21 #include "config.h"
23 #include <gstdio.h>
24 #include <gi18n.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <locale.h>
30 #include "gvdb/gvdb-builder.h"
31 #include "strinfo.c"
33 #ifdef G_OS_WIN32
34 #include "glib/glib-private.h"
35 #endif
37 static void
38 strip_string (GString *string)
40 gint i;
42 for (i = 0; g_ascii_isspace (string->str[i]); i++);
43 g_string_erase (string, 0, i);
45 if (string->len > 0)
47 /* len > 0, so there must be at least one non-whitespace character */
48 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
49 g_string_truncate (string, i + 1);
53 /* Handling of <enum> {{{1 */
54 typedef struct
56 GString *strinfo;
58 gboolean is_flags;
59 } EnumState;
61 static void
62 enum_state_free (gpointer data)
64 EnumState *state = data;
66 g_string_free (state->strinfo, TRUE);
67 g_slice_free (EnumState, state);
70 static EnumState *
71 enum_state_new (gboolean is_flags)
73 EnumState *state;
75 state = g_slice_new (EnumState);
76 state->strinfo = g_string_new (NULL);
77 state->is_flags = is_flags;
79 return state;
82 static void
83 enum_state_add_value (EnumState *state,
84 const gchar *nick,
85 const gchar *valuestr,
86 GError **error)
88 gint64 value;
89 gchar *end;
91 if (nick[0] == '\0' || nick[1] == '\0')
93 g_set_error (error, G_MARKUP_ERROR,
94 G_MARKUP_ERROR_INVALID_CONTENT,
95 "nick must be a minimum of 2 characters");
96 return;
99 value = g_ascii_strtoll (valuestr, &end, 0);
100 if (*end || state->is_flags ?
101 (value > G_MAXUINT32 || value < 0) :
102 (value > G_MAXINT32 || value < G_MININT32))
104 g_set_error (error, G_MARKUP_ERROR,
105 G_MARKUP_ERROR_INVALID_CONTENT,
106 "invalid numeric value");
107 return;
110 if (strinfo_builder_contains (state->strinfo, nick))
112 g_set_error (error, G_MARKUP_ERROR,
113 G_MARKUP_ERROR_INVALID_CONTENT,
114 "<value nick='%s'/> already specified", nick);
115 return;
118 if (strinfo_builder_contains_value (state->strinfo, value))
120 g_set_error (error, G_MARKUP_ERROR,
121 G_MARKUP_ERROR_INVALID_CONTENT,
122 "value='%s' already specified", valuestr);
123 return;
126 /* Silently drop the null case if it is mentioned.
127 * It is properly denoted with an empty array.
129 if (state->is_flags && value == 0)
130 return;
132 if (state->is_flags && (value & (value - 1)))
134 g_set_error (error, G_MARKUP_ERROR,
135 G_MARKUP_ERROR_INVALID_CONTENT,
136 "flags values must have at most 1 bit set");
137 return;
140 /* Since we reject exact duplicates of value='' and we only allow one
141 * bit to be set, it's not possible to have overlaps.
143 * If we loosen the one-bit-set restriction we need an overlap check.
146 strinfo_builder_append_item (state->strinfo, nick, value);
149 static void
150 enum_state_end (EnumState **state_ptr,
151 GError **error)
153 EnumState *state;
155 state = *state_ptr;
156 *state_ptr = NULL;
158 if (state->strinfo->len == 0)
159 g_set_error (error,
160 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
161 "<%s> must contain at least one <value>",
162 state->is_flags ? "flags" : "enum");
165 /* Handling of <key> {{{1 */
166 typedef struct
168 /* for <child>, @child_schema will be set.
169 * for <key>, everything else will be set.
171 gchar *child_schema;
174 GVariantType *type;
175 gboolean have_gettext_domain;
177 gchar l10n;
178 gchar *l10n_context;
179 GString *unparsed_default_value;
180 GVariant *default_value;
182 GString *strinfo;
183 gboolean is_enum;
184 gboolean is_flags;
186 GVariant *minimum;
187 GVariant *maximum;
189 gboolean has_choices;
190 gboolean has_aliases;
191 gboolean is_override;
193 gboolean checked;
194 GVariant *serialised;
195 } KeyState;
197 static KeyState *
198 key_state_new (const gchar *type_string,
199 const gchar *gettext_domain,
200 gboolean is_enum,
201 gboolean is_flags,
202 GString *strinfo)
204 KeyState *state;
206 state = g_slice_new0 (KeyState);
207 state->type = g_variant_type_new (type_string);
208 state->have_gettext_domain = gettext_domain != NULL;
209 state->is_enum = is_enum;
210 state->is_flags = is_flags;
212 if (strinfo)
213 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
214 else
215 state->strinfo = g_string_new (NULL);
217 return state;
220 static KeyState *
221 key_state_override (KeyState *state,
222 const gchar *gettext_domain)
224 KeyState *copy;
226 copy = g_slice_new0 (KeyState);
227 copy->type = g_variant_type_copy (state->type);
228 copy->have_gettext_domain = gettext_domain != NULL;
229 copy->strinfo = g_string_new_len (state->strinfo->str,
230 state->strinfo->len);
231 copy->is_enum = state->is_enum;
232 copy->is_flags = state->is_flags;
233 copy->is_override = TRUE;
235 if (state->minimum)
237 copy->minimum = g_variant_ref (state->minimum);
238 copy->maximum = g_variant_ref (state->maximum);
241 return copy;
244 static KeyState *
245 key_state_new_child (const gchar *child_schema)
247 KeyState *state;
249 state = g_slice_new0 (KeyState);
250 state->child_schema = g_strdup (child_schema);
252 return state;
255 static gboolean
256 is_valid_choices (GVariant *variant,
257 GString *strinfo)
259 switch (g_variant_classify (variant))
261 case G_VARIANT_CLASS_MAYBE:
262 case G_VARIANT_CLASS_ARRAY:
264 gboolean valid = TRUE;
265 GVariantIter iter;
267 g_variant_iter_init (&iter, variant);
269 while (valid && (variant = g_variant_iter_next_value (&iter)))
271 valid = is_valid_choices (variant, strinfo);
272 g_variant_unref (variant);
275 return valid;
278 case G_VARIANT_CLASS_STRING:
279 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
280 strinfo->len / 4,
281 g_variant_get_string (variant, NULL));
283 default:
284 g_assert_not_reached ();
289 /* Gets called at </default> </choices> or <range/> to check for
290 * validity of the default value so that any inconsistency is
291 * reported as soon as it is encountered.
293 static void
294 key_state_check_range (KeyState *state,
295 GError **error)
297 if (state->default_value)
299 const gchar *tag;
301 tag = state->is_override ? "override" : "default";
303 if (state->minimum)
305 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
306 g_variant_compare (state->default_value, state->maximum) > 0)
308 g_set_error (error, G_MARKUP_ERROR,
309 G_MARKUP_ERROR_INVALID_CONTENT,
310 "<%s> is not contained in "
311 "the specified range", tag);
315 else if (state->strinfo->len)
317 if (!is_valid_choices (state->default_value, state->strinfo))
319 if (state->is_enum)
320 g_set_error (error, G_MARKUP_ERROR,
321 G_MARKUP_ERROR_INVALID_CONTENT,
322 "<%s> is not a valid member of "
323 "the specified enumerated type", tag);
325 else if (state->is_flags)
326 g_set_error (error, G_MARKUP_ERROR,
327 G_MARKUP_ERROR_INVALID_CONTENT,
328 "<%s> contains string not in the "
329 "specified flags type", tag);
331 else
332 g_set_error (error, G_MARKUP_ERROR,
333 G_MARKUP_ERROR_INVALID_CONTENT,
334 "<%s> contains string not in "
335 "<choices>", tag);
341 static void
342 key_state_set_range (KeyState *state,
343 const gchar *min_str,
344 const gchar *max_str,
345 GError **error)
347 const struct {
348 const gchar type;
349 const gchar *min;
350 const gchar *max;
351 } table[] = {
352 { 'y', "0", "255" },
353 { 'n', "-32768", "32767" },
354 { 'q', "0", "65535" },
355 { 'i', "-2147483648", "2147483647" },
356 { 'u', "0", "4294967295" },
357 { 'x', "-9223372036854775808", "9223372036854775807" },
358 { 't', "0", "18446744073709551615" },
359 { 'd', "-inf", "inf" },
361 gboolean type_ok = FALSE;
362 gint i;
364 if (state->minimum)
366 g_set_error_literal (error, G_MARKUP_ERROR,
367 G_MARKUP_ERROR_INVALID_CONTENT,
368 "<range/> already specified for this key");
369 return;
372 for (i = 0; i < G_N_ELEMENTS (table); i++)
373 if (*(char *) state->type == table[i].type)
375 min_str = min_str ? min_str : table[i].min;
376 max_str = max_str ? max_str : table[i].max;
377 type_ok = TRUE;
378 break;
381 if (!type_ok)
383 gchar *type = g_variant_type_dup_string (state->type);
384 g_set_error (error, G_MARKUP_ERROR,
385 G_MARKUP_ERROR_INVALID_CONTENT,
386 "<range> not allowed for keys of type '%s'", type);
387 g_free (type);
388 return;
391 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
392 if (state->minimum == NULL)
393 return;
395 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
396 if (state->maximum == NULL)
397 return;
399 if (g_variant_compare (state->minimum, state->maximum) > 0)
401 g_set_error (error, G_MARKUP_ERROR,
402 G_MARKUP_ERROR_INVALID_CONTENT,
403 "<range> specified minimum is greater than maxmimum");
404 return;
407 key_state_check_range (state, error);
410 static GString *
411 key_state_start_default (KeyState *state,
412 const gchar *l10n,
413 const gchar *context,
414 GError **error)
416 if (l10n != NULL)
418 if (strcmp (l10n, "messages") == 0)
419 state->l10n = 'm';
421 else if (strcmp (l10n, "time") == 0)
422 state->l10n = 't';
424 else
426 g_set_error (error, G_MARKUP_ERROR,
427 G_MARKUP_ERROR_INVALID_CONTENT,
428 "unsupported l10n category: %s", l10n);
429 return NULL;
432 if (!state->have_gettext_domain)
434 g_set_error_literal (error, G_MARKUP_ERROR,
435 G_MARKUP_ERROR_INVALID_CONTENT,
436 "l10n requested, but no "
437 "gettext domain given");
438 return NULL;
441 state->l10n_context = g_strdup (context);
444 else if (context != NULL)
446 g_set_error_literal (error, G_MARKUP_ERROR,
447 G_MARKUP_ERROR_INVALID_CONTENT,
448 "translation context given for "
449 " value without l10n enabled");
450 return NULL;
453 return g_string_new (NULL);
456 static void
457 key_state_end_default (KeyState *state,
458 GString **string,
459 GError **error)
461 state->unparsed_default_value = *string;
462 *string = NULL;
464 state->default_value = g_variant_parse (state->type,
465 state->unparsed_default_value->str,
466 NULL, NULL, error);
467 key_state_check_range (state, error);
470 static void
471 key_state_start_choices (KeyState *state,
472 GError **error)
474 const GVariantType *type = state->type;
476 if (state->is_enum)
478 g_set_error_literal (error, G_MARKUP_ERROR,
479 G_MARKUP_ERROR_INVALID_CONTENT,
480 "<choices> cannot be specified for keys "
481 "tagged as having an enumerated type");
482 return;
485 if (state->has_choices)
487 g_set_error_literal (error, G_MARKUP_ERROR,
488 G_MARKUP_ERROR_INVALID_CONTENT,
489 "<choices> already specified for this key");
490 return;
493 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
494 type = g_variant_type_element (type);
496 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
498 gchar *type_string = g_variant_type_dup_string (state->type);
499 g_set_error (error, G_MARKUP_ERROR,
500 G_MARKUP_ERROR_INVALID_CONTENT,
501 "<choices> not allowed for keys of type '%s'",
502 type_string);
503 g_free (type_string);
504 return;
508 static void
509 key_state_add_choice (KeyState *state,
510 const gchar *choice,
511 GError **error)
513 if (strinfo_builder_contains (state->strinfo, choice))
515 g_set_error (error, G_MARKUP_ERROR,
516 G_MARKUP_ERROR_INVALID_CONTENT,
517 "<choice value='%s'/> already given", choice);
518 return;
521 strinfo_builder_append_item (state->strinfo, choice, 0);
522 state->has_choices = TRUE;
525 static void
526 key_state_end_choices (KeyState *state,
527 GError **error)
529 if (!state->has_choices)
531 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
532 "<choices> must contain at least one <choice>");
533 return;
536 key_state_check_range (state, error);
539 static void
540 key_state_start_aliases (KeyState *state,
541 GError **error)
543 if (state->has_aliases)
544 g_set_error_literal (error, G_MARKUP_ERROR,
545 G_MARKUP_ERROR_INVALID_CONTENT,
546 "<aliases> already specified for this key");
547 else if (!state->is_flags && !state->is_enum && !state->has_choices)
548 g_set_error_literal (error, G_MARKUP_ERROR,
549 G_MARKUP_ERROR_INVALID_CONTENT,
550 "<aliases> can only be specified for keys with "
551 "enumerated or flags types or after <choices>");
554 static void
555 key_state_add_alias (KeyState *state,
556 const gchar *alias,
557 const gchar *target,
558 GError **error)
560 if (strinfo_builder_contains (state->strinfo, alias))
562 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
563 state->strinfo->len / 4,
564 alias))
566 if (state->is_enum)
567 g_set_error (error, G_MARKUP_ERROR,
568 G_MARKUP_ERROR_INVALID_CONTENT,
569 "<alias value='%s'/> given when '%s' is already "
570 "a member of the enumerated type", alias, alias);
572 else
573 g_set_error (error, G_MARKUP_ERROR,
574 G_MARKUP_ERROR_INVALID_CONTENT,
575 "<alias value='%s'/> given when "
576 "<choice value='%s'/> was already given",
577 alias, alias);
580 else
581 g_set_error (error, G_MARKUP_ERROR,
582 G_MARKUP_ERROR_INVALID_CONTENT,
583 "<alias value='%s'/> already specified", alias);
585 return;
588 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
590 g_set_error (error, G_MARKUP_ERROR,
591 G_MARKUP_ERROR_INVALID_CONTENT,
592 "alias target '%s' is not in %s", target,
593 state->is_enum ? "enumerated type" : "<choices>");
594 return;
597 state->has_aliases = TRUE;
600 static void
601 key_state_end_aliases (KeyState *state,
602 GError **error)
604 if (!state->has_aliases)
606 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
607 "<aliases> must contain at least one <alias>");
608 return;
612 static gboolean
613 key_state_check (KeyState *state,
614 GError **error)
616 if (state->checked)
617 return TRUE;
619 return state->checked = TRUE;
622 static GVariant *
623 key_state_serialise (KeyState *state)
625 if (state->serialised == NULL)
627 if (state->child_schema)
629 state->serialised = g_variant_new_string (state->child_schema);
632 else
634 GVariantBuilder builder;
636 g_assert (key_state_check (state, NULL));
638 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
640 /* default value */
641 g_variant_builder_add_value (&builder, state->default_value);
643 /* translation */
644 if (state->l10n)
646 /* We are going to store the untranslated default for
647 * runtime translation according to the current locale.
648 * We need to strip leading and trailing whitespace from
649 * the string so that it's exactly the same as the one
650 * that ended up in the .po file for translation.
652 * We want to do this so that
654 * <default l10n='messages'>
655 * ['a', 'b', 'c']
656 * </default>
658 * ends up in the .po file like "['a', 'b', 'c']",
659 * omitting the extra whitespace at the start and end.
661 strip_string (state->unparsed_default_value);
663 if (state->l10n_context)
665 gint len;
667 /* Contextified messages are supported by prepending
668 * the context, followed by '\004' to the start of the
669 * message string. We do that here to save GSettings
670 * the work later on.
672 len = strlen (state->l10n_context);
673 state->l10n_context[len] = '\004';
674 g_string_prepend_len (state->unparsed_default_value,
675 state->l10n_context, len + 1);
676 g_free (state->l10n_context);
677 state->l10n_context = NULL;
680 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
681 state->unparsed_default_value->str);
682 g_string_free (state->unparsed_default_value, TRUE);
683 state->unparsed_default_value = NULL;
686 /* choice, aliases, enums */
687 if (state->strinfo->len)
689 GVariant *array;
690 guint32 *words;
691 gpointer data;
692 gsize size;
693 gint i;
695 data = state->strinfo->str;
696 size = state->strinfo->len;
698 words = data;
699 for (i = 0; i < size / sizeof (guint32); i++)
700 words[i] = GUINT32_TO_LE (words[i]);
702 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
703 data, size, TRUE,
704 g_free, data);
706 g_string_free (state->strinfo, FALSE);
707 state->strinfo = NULL;
709 g_variant_builder_add (&builder, "(y@au)",
710 state->is_flags ? 'f' :
711 state->is_enum ? 'e' : 'c',
712 array);
715 /* range */
716 if (state->minimum || state->maximum)
717 g_variant_builder_add (&builder, "(y(**))", 'r',
718 state->minimum, state->maximum);
720 state->serialised = g_variant_builder_end (&builder);
723 g_variant_ref_sink (state->serialised);
726 return g_variant_ref (state->serialised);
729 static void
730 key_state_free (gpointer data)
732 KeyState *state = data;
734 if (state->type)
735 g_variant_type_free (state->type);
737 g_free (state->l10n_context);
739 if (state->unparsed_default_value)
740 g_string_free (state->unparsed_default_value, TRUE);
742 if (state->default_value)
743 g_variant_unref (state->default_value);
745 if (state->strinfo)
746 g_string_free (state->strinfo, TRUE);
748 if (state->minimum)
749 g_variant_unref (state->minimum);
751 if (state->maximum)
752 g_variant_unref (state->maximum);
754 if (state->serialised)
755 g_variant_unref (state->serialised);
757 g_slice_free (KeyState, state);
760 /* Key name validity {{{1 */
761 static gboolean allow_any_name = FALSE;
763 static gboolean
764 is_valid_keyname (const gchar *key,
765 GError **error)
767 gint i;
769 if (key[0] == '\0')
771 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
772 _("empty names are not permitted"));
773 return FALSE;
776 if (allow_any_name)
777 return TRUE;
779 if (!g_ascii_islower (key[0]))
781 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
782 _("invalid name '%s': names must begin "
783 "with a lowercase letter"), key);
784 return FALSE;
787 for (i = 1; key[i]; i++)
789 if (key[i] != '-' &&
790 !g_ascii_islower (key[i]) &&
791 !g_ascii_isdigit (key[i]))
793 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
794 _("invalid name '%s': invalid character '%c'; "
795 "only lowercase letters, numbers and hyphen ('-') "
796 "are permitted."), key, key[i]);
797 return FALSE;
800 if (key[i] == '-' && key[i + 1] == '-')
802 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
803 _("invalid name '%s': two successive hyphens ('--') "
804 "are not permitted."), key);
805 return FALSE;
809 if (key[i - 1] == '-')
811 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
812 _("invalid name '%s': the last character may not be a "
813 "hyphen ('-')."), key);
814 return FALSE;
817 if (i > 1024)
819 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
820 _("invalid name '%s': maximum length is 1024"), key);
821 return FALSE;
824 return TRUE;
827 /* Handling of <schema> {{{1 */
828 typedef struct _SchemaState SchemaState;
829 struct _SchemaState
831 SchemaState *extends;
833 gchar *path;
834 gchar *gettext_domain;
835 gchar *extends_name;
836 gchar *list_of;
838 GHashTable *keys;
841 static SchemaState *
842 schema_state_new (const gchar *path,
843 const gchar *gettext_domain,
844 SchemaState *extends,
845 const gchar *extends_name,
846 const gchar *list_of)
848 SchemaState *state;
850 state = g_slice_new (SchemaState);
851 state->path = g_strdup (path);
852 state->gettext_domain = g_strdup (gettext_domain);
853 state->extends = extends;
854 state->extends_name = g_strdup (extends_name);
855 state->list_of = g_strdup (list_of);
856 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
857 g_free, key_state_free);
859 return state;
862 static void
863 schema_state_free (gpointer data)
865 SchemaState *state = data;
867 g_free (state->path);
868 g_free (state->gettext_domain);
869 g_hash_table_unref (state->keys);
872 static void
873 schema_state_add_child (SchemaState *state,
874 const gchar *name,
875 const gchar *schema,
876 GError **error)
878 gchar *childname;
880 if (!is_valid_keyname (name, error))
881 return;
883 childname = g_strconcat (name, "/", NULL);
885 if (g_hash_table_lookup (state->keys, childname))
887 g_set_error (error, G_MARKUP_ERROR,
888 G_MARKUP_ERROR_INVALID_CONTENT,
889 _("<child name='%s'> already specified"), name);
890 return;
893 g_hash_table_insert (state->keys, childname,
894 key_state_new_child (schema));
897 static KeyState *
898 schema_state_add_key (SchemaState *state,
899 GHashTable *enum_table,
900 GHashTable *flags_table,
901 const gchar *name,
902 const gchar *type_string,
903 const gchar *enum_type,
904 const gchar *flags_type,
905 GError **error)
907 SchemaState *node;
908 GString *strinfo;
909 KeyState *key;
911 if (state->list_of)
913 g_set_error_literal (error, G_MARKUP_ERROR,
914 G_MARKUP_ERROR_INVALID_CONTENT,
915 _("cannot add keys to a 'list-of' schema"));
916 return NULL;
919 if (!is_valid_keyname (name, error))
920 return NULL;
922 if (g_hash_table_lookup (state->keys, name))
924 g_set_error (error, G_MARKUP_ERROR,
925 G_MARKUP_ERROR_INVALID_CONTENT,
926 _("<key name='%s'> already specified"), name);
927 return NULL;
930 for (node = state; node; node = node->extends)
931 if (node->extends)
933 KeyState *shadow;
935 shadow = g_hash_table_lookup (node->extends->keys, name);
937 /* in case of <key> <override> <key> make sure we report the
938 * location of the original <key>, not the <override>.
940 if (shadow && !shadow->is_override)
942 g_set_error (error, G_MARKUP_ERROR,
943 G_MARKUP_ERROR_INVALID_CONTENT,
944 _("<key name='%s'> shadows <key name='%s'> in "
945 "<schema id='%s'>; use <override> to modify value"),
946 name, name, node->extends_name);
947 return NULL;
951 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
953 g_set_error (error, G_MARKUP_ERROR,
954 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
955 _("exactly one of 'type', 'enum' or 'flags' must "
956 "be specified as an attribute to <key>"));
957 return NULL;
960 if (type_string == NULL) /* flags or enums was specified */
962 EnumState *enum_state;
964 if (enum_type)
965 enum_state = g_hash_table_lookup (enum_table, enum_type);
966 else
967 enum_state = g_hash_table_lookup (flags_table, flags_type);
970 if (enum_state == NULL)
972 g_set_error (error, G_MARKUP_ERROR,
973 G_MARKUP_ERROR_INVALID_CONTENT,
974 _("<%s id='%s'> not (yet) defined."),
975 flags_type ? "flags" : "enum",
976 flags_type ? flags_type : enum_type);
977 return NULL;
980 type_string = flags_type ? "as" : "s";
981 strinfo = enum_state->strinfo;
983 else
985 if (!g_variant_type_string_is_valid (type_string))
987 g_set_error (error, G_MARKUP_ERROR,
988 G_MARKUP_ERROR_INVALID_CONTENT,
989 _("invalid GVariant type string '%s'"), type_string);
990 return NULL;
993 strinfo = NULL;
996 key = key_state_new (type_string, state->gettext_domain,
997 enum_type != NULL, flags_type != NULL, strinfo);
998 g_hash_table_insert (state->keys, g_strdup (name), key);
1000 return key;
1003 static void
1004 schema_state_add_override (SchemaState *state,
1005 KeyState **key_state,
1006 GString **string,
1007 const gchar *key,
1008 const gchar *l10n,
1009 const gchar *context,
1010 GError **error)
1012 SchemaState *parent;
1013 KeyState *original;
1015 if (state->extends == NULL)
1017 g_set_error_literal (error, G_MARKUP_ERROR,
1018 G_MARKUP_ERROR_INVALID_CONTENT,
1019 _("<override> given but schema isn't "
1020 "extending anything"));
1021 return;
1024 for (parent = state->extends; parent; parent = parent->extends)
1025 if ((original = g_hash_table_lookup (parent->keys, key)))
1026 break;
1028 if (original == NULL)
1030 g_set_error (error, G_MARKUP_ERROR,
1031 G_MARKUP_ERROR_INVALID_CONTENT,
1032 _("no <key name='%s'> to override"), key);
1033 return;
1036 if (g_hash_table_lookup (state->keys, key))
1038 g_set_error (error, G_MARKUP_ERROR,
1039 G_MARKUP_ERROR_INVALID_CONTENT,
1040 _("<override name='%s'> already specified"), key);
1041 return;
1044 *key_state = key_state_override (original, state->gettext_domain);
1045 *string = key_state_start_default (*key_state, l10n, context, error);
1046 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1049 static void
1050 override_state_end (KeyState **key_state,
1051 GString **string,
1052 GError **error)
1054 key_state_end_default (*key_state, string, error);
1055 *key_state = NULL;
1058 /* Handling of toplevel state {{{1 */
1059 typedef struct
1061 GHashTable *schema_table; /* string -> SchemaState */
1062 GHashTable *flags_table; /* string -> EnumState */
1063 GHashTable *enum_table; /* string -> EnumState */
1065 GSList *this_file_schemas; /* strings: <schema>s in this file */
1066 GSList *this_file_flagss; /* strings: <flags>s in this file */
1067 GSList *this_file_enums; /* strings: <enum>s in this file */
1069 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1071 SchemaState *schema_state; /* non-NULL when inside <schema> */
1072 KeyState *key_state; /* non-NULL when inside <key> */
1073 EnumState *enum_state; /* non-NULL when inside <enum> */
1075 GString *string; /* non-NULL when accepting text */
1076 } ParseState;
1078 static gboolean
1079 is_subclass (const gchar *class_name,
1080 const gchar *possible_parent,
1081 GHashTable *schema_table)
1083 SchemaState *class;
1085 if (strcmp (class_name, possible_parent) == 0)
1086 return TRUE;
1088 class = g_hash_table_lookup (schema_table, class_name);
1089 g_assert (class != NULL);
1091 return class->extends_name &&
1092 is_subclass (class->extends_name, possible_parent, schema_table);
1095 static void
1096 parse_state_start_schema (ParseState *state,
1097 const gchar *id,
1098 const gchar *path,
1099 const gchar *gettext_domain,
1100 const gchar *extends_name,
1101 const gchar *list_of,
1102 GError **error)
1104 SchemaState *extends;
1105 gchar *my_id;
1107 if (g_hash_table_lookup (state->schema_table, id))
1109 g_set_error (error, G_MARKUP_ERROR,
1110 G_MARKUP_ERROR_INVALID_CONTENT,
1111 _("<schema id='%s'> already specified"), id);
1112 return;
1115 if (extends_name)
1117 extends = g_hash_table_lookup (state->schema_table, extends_name);
1119 if (extends == NULL)
1121 g_set_error (error, G_MARKUP_ERROR,
1122 G_MARKUP_ERROR_INVALID_CONTENT,
1123 _("<schema id='%s'> extends not yet existing "
1124 "schema '%s'"), id, extends_name);
1125 return;
1128 else
1129 extends = NULL;
1131 if (list_of)
1133 SchemaState *tmp;
1135 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1137 g_set_error (error, G_MARKUP_ERROR,
1138 G_MARKUP_ERROR_INVALID_CONTENT,
1139 _("<schema id='%s'> is list of not yet existing "
1140 "schema '%s'"), id, list_of);
1141 return;
1144 if (tmp->path)
1146 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1147 _("Can not be a list of a schema with a path"));
1148 return;
1152 if (extends)
1154 if (extends->path)
1156 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1157 _("Can not extend a schema with a path"));
1158 return;
1161 if (list_of)
1163 if (extends->list_of == NULL)
1165 g_set_error (error, G_MARKUP_ERROR,
1166 G_MARKUP_ERROR_INVALID_CONTENT,
1167 _("<schema id='%s'> is a list, extending "
1168 "<schema id='%s'> which is not a list"),
1169 id, extends_name);
1170 return;
1173 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1175 g_set_error (error, G_MARKUP_ERROR,
1176 G_MARKUP_ERROR_INVALID_CONTENT,
1177 _("<schema id='%s' list-of='%s'> extends <schema "
1178 "id='%s' list-of='%s'> but '%s' does not "
1179 "extend '%s'"), id, list_of, extends_name,
1180 extends->list_of, list_of, extends->list_of);
1181 return;
1184 else
1185 /* by default we are a list of the same thing that the schema
1186 * we are extending is a list of (which might be nothing)
1188 list_of = extends->list_of;
1191 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1193 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1194 _("a path, if given, must begin and end with a slash"));
1195 return;
1198 if (path && list_of && !g_str_has_suffix (path, ":/"))
1200 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1201 _("the path of a list must end with ':/'"));
1202 return;
1205 if (path && (g_str_has_prefix (path, "/apps/") ||
1206 g_str_has_prefix (path, "/desktop/") ||
1207 g_str_has_prefix (path, "/system/")))
1208 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1209 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1211 state->schema_state = schema_state_new (path, gettext_domain,
1212 extends, extends_name, list_of);
1214 my_id = g_strdup (id);
1215 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1216 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1219 static void
1220 parse_state_start_enum (ParseState *state,
1221 const gchar *id,
1222 gboolean is_flags,
1223 GError **error)
1225 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1226 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1227 gchar *my_id;
1229 if (g_hash_table_lookup (table, id))
1231 g_set_error (error, G_MARKUP_ERROR,
1232 G_MARKUP_ERROR_INVALID_CONTENT,
1233 _("<%s id='%s'> already specified"),
1234 is_flags ? "flags" : "enum", id);
1235 return;
1238 state->enum_state = enum_state_new (is_flags);
1240 my_id = g_strdup (id);
1241 *list = g_slist_prepend (*list, my_id);
1242 g_hash_table_insert (table, my_id, state->enum_state);
1245 /* GMarkup Parser Functions {{{1 */
1247 /* Start element {{{2 */
1248 static void
1249 start_element (GMarkupParseContext *context,
1250 const gchar *element_name,
1251 const gchar **attribute_names,
1252 const gchar **attribute_values,
1253 gpointer user_data,
1254 GError **error)
1256 ParseState *state = user_data;
1257 const GSList *element_stack;
1258 const gchar *container;
1260 element_stack = g_markup_parse_context_get_element_stack (context);
1261 container = element_stack->next ? element_stack->next->data : NULL;
1263 #define COLLECT(first, ...) \
1264 g_markup_collect_attributes (element_name, \
1265 attribute_names, attribute_values, error, \
1266 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1267 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1268 #define STRDUP G_MARKUP_COLLECT_STRDUP
1269 #define STRING G_MARKUP_COLLECT_STRING
1270 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1272 /* Toplevel items {{{3 */
1273 if (container == NULL)
1275 if (strcmp (element_name, "schemalist") == 0)
1277 COLLECT (OPTIONAL | STRDUP,
1278 "gettext-domain",
1279 &state->schemalist_domain);
1280 return;
1285 /* children of <schemalist> {{{3 */
1286 else if (strcmp (container, "schemalist") == 0)
1288 if (strcmp (element_name, "schema") == 0)
1290 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1291 if (COLLECT (STRING, "id", &id,
1292 OPTIONAL | STRING, "path", &path,
1293 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1294 OPTIONAL | STRING, "extends", &extends,
1295 OPTIONAL | STRING, "list-of", &list_of))
1296 parse_state_start_schema (state, id, path,
1297 gettext_domain ? gettext_domain
1298 : state->schemalist_domain,
1299 extends, list_of, error);
1300 return;
1303 else if (strcmp (element_name, "enum") == 0)
1305 const gchar *id;
1306 if (COLLECT (STRING, "id", &id))
1307 parse_state_start_enum (state, id, FALSE, error);
1308 return;
1311 else if (strcmp (element_name, "flags") == 0)
1313 const gchar *id;
1314 if (COLLECT (STRING, "id", &id))
1315 parse_state_start_enum (state, id, TRUE, error);
1316 return;
1321 /* children of <schema> {{{3 */
1322 else if (strcmp (container, "schema") == 0)
1324 if (strcmp (element_name, "key") == 0)
1326 const gchar *name, *type_string, *enum_type, *flags_type;
1328 if (COLLECT (STRING, "name", &name,
1329 OPTIONAL | STRING, "type", &type_string,
1330 OPTIONAL | STRING, "enum", &enum_type,
1331 OPTIONAL | STRING, "flags", &flags_type))
1333 state->key_state = schema_state_add_key (state->schema_state,
1334 state->enum_table,
1335 state->flags_table,
1336 name, type_string,
1337 enum_type, flags_type,
1338 error);
1339 return;
1341 else if (strcmp (element_name, "child") == 0)
1343 const gchar *name, *schema;
1345 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1346 schema_state_add_child (state->schema_state,
1347 name, schema, error);
1348 return;
1350 else if (strcmp (element_name, "override") == 0)
1352 const gchar *name, *l10n, *context;
1354 if (COLLECT (STRING, "name", &name,
1355 OPTIONAL | STRING, "l10n", &l10n,
1356 OPTIONAL | STRING, "context", &context))
1357 schema_state_add_override (state->schema_state,
1358 &state->key_state, &state->string,
1359 name, l10n, context, error);
1360 return;
1364 /* children of <key> {{{3 */
1365 else if (strcmp (container, "key") == 0)
1367 if (strcmp (element_name, "default") == 0)
1369 const gchar *l10n, *context;
1370 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1371 STRING | OPTIONAL, "context", &context))
1372 state->string = key_state_start_default (state->key_state,
1373 l10n, context, error);
1374 return;
1377 else if (strcmp (element_name, "summary") == 0 ||
1378 strcmp (element_name, "description") == 0)
1380 if (NO_ATTRS ())
1381 state->string = g_string_new (NULL);
1382 return;
1385 else if (strcmp (element_name, "range") == 0)
1387 const gchar *min, *max;
1388 if (COLLECT (STRING | OPTIONAL, "min", &min,
1389 STRING | OPTIONAL, "max", &max))
1390 key_state_set_range (state->key_state, min, max, error);
1391 return;
1394 else if (strcmp (element_name, "choices") == 0)
1396 if (NO_ATTRS ())
1397 key_state_start_choices (state->key_state, error);
1398 return;
1401 else if (strcmp (element_name, "aliases") == 0)
1403 if (NO_ATTRS ())
1404 key_state_start_aliases (state->key_state, error);
1405 return;
1410 /* children of <choices> {{{3 */
1411 else if (strcmp (container, "choices") == 0)
1413 if (strcmp (element_name, "choice") == 0)
1415 const gchar *value;
1416 if (COLLECT (STRING, "value", &value))
1417 key_state_add_choice (state->key_state, value, error);
1418 return;
1423 /* children of <aliases> {{{3 */
1424 else if (strcmp (container, "aliases") == 0)
1426 if (strcmp (element_name, "alias") == 0)
1428 const gchar *value, *target;
1429 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1430 key_state_add_alias (state->key_state, value, target, error);
1431 return;
1436 /* children of <enum> {{{3 */
1437 else if (strcmp (container, "enum") == 0 ||
1438 strcmp (container, "flags") == 0)
1440 if (strcmp (element_name, "value") == 0)
1442 const gchar *nick, *valuestr;
1443 if (COLLECT (STRING, "nick", &nick,
1444 STRING, "value", &valuestr))
1445 enum_state_add_value (state->enum_state, nick, valuestr, error);
1446 return;
1449 /* 3}}} */
1451 if (container)
1452 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1453 _("Element <%s> not allowed inside <%s>"),
1454 element_name, container);
1455 else
1456 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1457 _("Element <%s> not allowed at the top level"), element_name);
1459 /* 2}}} */
1460 /* End element {{{2 */
1462 static void
1463 key_state_end (KeyState **state_ptr,
1464 GError **error)
1466 KeyState *state;
1468 state = *state_ptr;
1469 *state_ptr = NULL;
1471 if (state->default_value == NULL)
1473 g_set_error_literal (error,
1474 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1475 "element <default> is required in <key>");
1476 return;
1480 static void
1481 schema_state_end (SchemaState **state_ptr,
1482 GError **error)
1484 *state_ptr = NULL;
1487 static void
1488 end_element (GMarkupParseContext *context,
1489 const gchar *element_name,
1490 gpointer user_data,
1491 GError **error)
1493 ParseState *state = user_data;
1495 if (strcmp (element_name, "schemalist") == 0)
1497 g_free (state->schemalist_domain);
1498 state->schemalist_domain = NULL;
1501 else if (strcmp (element_name, "enum") == 0 ||
1502 strcmp (element_name, "flags") == 0)
1503 enum_state_end (&state->enum_state, error);
1505 else if (strcmp (element_name, "schema") == 0)
1506 schema_state_end (&state->schema_state, error);
1508 else if (strcmp (element_name, "override") == 0)
1509 override_state_end (&state->key_state, &state->string, error);
1511 else if (strcmp (element_name, "key") == 0)
1512 key_state_end (&state->key_state, error);
1514 else if (strcmp (element_name, "default") == 0)
1515 key_state_end_default (state->key_state, &state->string, error);
1517 else if (strcmp (element_name, "choices") == 0)
1518 key_state_end_choices (state->key_state, error);
1520 else if (strcmp (element_name, "aliases") == 0)
1521 key_state_end_aliases (state->key_state, error);
1523 if (state->string)
1525 g_string_free (state->string, TRUE);
1526 state->string = NULL;
1529 /* Text {{{2 */
1530 static void
1531 text (GMarkupParseContext *context,
1532 const gchar *text,
1533 gsize text_len,
1534 gpointer user_data,
1535 GError **error)
1537 ParseState *state = user_data;
1539 if (state->string)
1541 /* we are expecting a string, so store the text data.
1543 * we store the data verbatim here and deal with whitespace
1544 * later on. there are two reasons for that:
1546 * 1) whitespace is handled differently depending on the tag
1547 * type.
1549 * 2) we could do leading whitespace removal by refusing to
1550 * insert it into state->string if it's at the start, but for
1551 * trailing whitespace, we have no idea if there is another
1552 * text() call coming or not.
1554 g_string_append_len (state->string, text, text_len);
1556 else
1558 /* string is not expected: accept (and ignore) pure whitespace */
1559 gsize i;
1561 for (i = 0; i < text_len; i++)
1562 if (!g_ascii_isspace (text[i]))
1564 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1565 _("text may not appear inside <%s>"),
1566 g_markup_parse_context_get_element (context));
1567 break;
1572 /* Write to GVDB {{{1 */
1573 typedef struct
1575 GHashTable *table;
1576 GvdbItem *root;
1577 } GvdbPair;
1579 static void
1580 gvdb_pair_init (GvdbPair *pair)
1582 pair->table = gvdb_hash_table_new (NULL, NULL);
1583 pair->root = gvdb_hash_table_insert (pair->table, "");
1586 typedef struct
1588 GHashTable *schema_table;
1589 GvdbPair root_pair;
1590 } WriteToFileData;
1592 typedef struct
1594 GHashTable *schema_table;
1595 GvdbPair pair;
1596 gboolean l10n;
1597 } OutputSchemaData;
1599 static void
1600 output_key (gpointer key,
1601 gpointer value,
1602 gpointer user_data)
1604 OutputSchemaData *data;
1605 const gchar *name;
1606 KeyState *state;
1607 GvdbItem *item;
1609 name = key;
1610 state = value;
1611 data = user_data;
1613 item = gvdb_hash_table_insert (data->pair.table, name);
1614 gvdb_item_set_parent (item, data->pair.root);
1615 gvdb_item_set_value (item, key_state_serialise (state));
1617 if (state->l10n)
1618 data->l10n = TRUE;
1620 if (state->child_schema &&
1621 !g_hash_table_lookup (data->schema_table, state->child_schema))
1622 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1623 state->child_schema);
1626 static void
1627 output_schema (gpointer key,
1628 gpointer value,
1629 gpointer user_data)
1631 WriteToFileData *wtf_data = user_data;
1632 OutputSchemaData data;
1633 GvdbPair *root_pair;
1634 SchemaState *state;
1635 const gchar *id;
1636 GvdbItem *item;
1638 id = key;
1639 state = value;
1640 root_pair = &wtf_data->root_pair;
1642 data.schema_table = wtf_data->schema_table;
1643 gvdb_pair_init (&data.pair);
1644 data.l10n = FALSE;
1646 item = gvdb_hash_table_insert (root_pair->table, id);
1647 gvdb_item_set_parent (item, root_pair->root);
1648 gvdb_item_set_hash_table (item, data.pair.table);
1650 g_hash_table_foreach (state->keys, output_key, &data);
1652 if (state->path)
1653 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1655 if (state->extends_name)
1656 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1657 state->extends_name);
1659 if (state->list_of)
1660 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1661 state->list_of);
1663 if (data.l10n)
1664 gvdb_hash_table_insert_string (data.pair.table,
1665 ".gettext-domain",
1666 state->gettext_domain);
1669 static gboolean
1670 write_to_file (GHashTable *schema_table,
1671 const gchar *filename,
1672 GError **error)
1674 WriteToFileData data;
1675 gboolean success;
1677 data.schema_table = schema_table;
1679 gvdb_pair_init (&data.root_pair);
1681 g_hash_table_foreach (schema_table, output_schema, &data);
1683 success = gvdb_table_write_contents (data.root_pair.table, filename,
1684 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1685 error);
1686 g_hash_table_unref (data.root_pair.table);
1688 return success;
1691 /* Parser driver {{{1 */
1692 static GHashTable *
1693 parse_gschema_files (gchar **files,
1694 gboolean strict)
1696 GMarkupParser parser = { start_element, end_element, text };
1697 ParseState state = { 0, };
1698 const gchar *filename;
1699 GError *error = NULL;
1701 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1702 g_free, enum_state_free);
1704 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1705 g_free, enum_state_free);
1707 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1708 g_free, schema_state_free);
1710 while ((filename = *files++) != NULL)
1712 GMarkupParseContext *context;
1713 gchar *contents;
1714 gsize size;
1716 if (!g_file_get_contents (filename, &contents, &size, &error))
1718 fprintf (stderr, "%s\n", error->message);
1719 g_clear_error (&error);
1720 continue;
1723 context = g_markup_parse_context_new (&parser,
1724 G_MARKUP_TREAT_CDATA_AS_TEXT |
1725 G_MARKUP_PREFIX_ERROR_POSITION |
1726 G_MARKUP_IGNORE_QUALIFIED,
1727 &state, NULL);
1730 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1731 !g_markup_parse_context_end_parse (context, &error))
1733 GSList *item;
1735 /* back out any changes from this file */
1736 for (item = state.this_file_schemas; item; item = item->next)
1737 g_hash_table_remove (state.schema_table, item->data);
1739 for (item = state.this_file_flagss; item; item = item->next)
1740 g_hash_table_remove (state.flags_table, item->data);
1742 for (item = state.this_file_enums; item; item = item->next)
1743 g_hash_table_remove (state.enum_table, item->data);
1745 /* let them know */
1746 fprintf (stderr, "%s: %s. ", filename, error->message);
1747 g_clear_error (&error);
1749 if (strict)
1751 /* Translators: Do not translate "--strict". */
1752 fprintf (stderr, _("--strict was specified; exiting.\n"));
1753 g_hash_table_unref (state.schema_table);
1754 g_hash_table_unref (state.flags_table);
1755 g_hash_table_unref (state.enum_table);
1757 return NULL;
1759 else
1760 fprintf (stderr, _("This entire file has been ignored.\n"));
1763 /* cleanup */
1764 g_markup_parse_context_free (context);
1765 g_slist_free (state.this_file_schemas);
1766 g_slist_free (state.this_file_flagss);
1767 g_slist_free (state.this_file_enums);
1768 state.this_file_schemas = NULL;
1769 state.this_file_flagss = NULL;
1770 state.this_file_enums = NULL;
1773 g_hash_table_unref (state.flags_table);
1774 g_hash_table_unref (state.enum_table);
1776 return state.schema_table;
1779 static gint
1780 compare_strings (gconstpointer a,
1781 gconstpointer b)
1783 gchar *one = *(gchar **) a;
1784 gchar *two = *(gchar **) b;
1785 gint cmp;
1787 cmp = g_str_has_suffix (two, ".enums.xml") -
1788 g_str_has_suffix (one, ".enums.xml");
1790 if (!cmp)
1791 cmp = strcmp (one, two);
1793 return cmp;
1796 static gboolean
1797 set_overrides (GHashTable *schema_table,
1798 gchar **files,
1799 gboolean strict)
1801 const gchar *filename;
1802 GError *error = NULL;
1804 while ((filename = *files++))
1806 GKeyFile *key_file;
1807 gchar **groups;
1808 gint i;
1810 key_file = g_key_file_new ();
1811 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1813 fprintf (stderr, "%s: %s. ", filename, error->message);
1814 g_key_file_free (key_file);
1815 g_clear_error (&error);
1817 if (!strict)
1819 fprintf (stderr, _("Ignoring this file.\n"));
1820 continue;
1823 fprintf (stderr, _("--strict was specified; exiting.\n"));
1824 return FALSE;
1827 groups = g_key_file_get_groups (key_file, NULL);
1829 for (i = 0; groups[i]; i++)
1831 const gchar *group = groups[i];
1832 SchemaState *schema;
1833 gchar **keys;
1834 gint j;
1836 schema = g_hash_table_lookup (schema_table, group);
1838 if (schema == NULL)
1839 /* Having the schema not be installed is expected to be a
1840 * common case. Don't even emit an error message about
1841 * that.
1843 continue;
1845 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1846 g_assert (keys != NULL);
1848 for (j = 0; keys[j]; j++)
1850 const gchar *key = keys[j];
1851 KeyState *state;
1852 GVariant *value;
1853 gchar *string;
1855 state = g_hash_table_lookup (schema->keys, key);
1857 if (state == NULL)
1859 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1860 "specified in override file '%s'"),
1861 key, group, filename);
1863 if (!strict)
1865 fprintf (stderr, _("; ignoring override for this key.\n"));
1866 continue;
1869 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1870 g_key_file_free (key_file);
1871 g_strfreev (groups);
1872 g_strfreev (keys);
1874 return FALSE;
1877 string = g_key_file_get_value (key_file, group, key, NULL);
1878 g_assert (string != NULL);
1880 value = g_variant_parse (state->type, string,
1881 NULL, NULL, &error);
1883 if (value == NULL)
1885 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1886 "as specified in override file '%s': "
1887 "%s."),
1888 key, group, filename, error->message);
1890 g_clear_error (&error);
1891 g_free (string);
1893 if (!strict)
1895 fprintf (stderr, _("Ignoring override for this key.\n"));
1896 continue;
1899 fprintf (stderr, _("--strict was specified; exiting.\n"));
1900 g_key_file_free (key_file);
1901 g_strfreev (groups);
1902 g_strfreev (keys);
1904 return FALSE;
1907 if (state->minimum)
1909 if (g_variant_compare (value, state->minimum) < 0 ||
1910 g_variant_compare (value, state->maximum) > 0)
1912 fprintf (stderr,
1913 _("override for key '%s' in schema '%s' in "
1914 "override file '%s' is outside the range "
1915 "given in the schema"),
1916 key, group, filename);
1918 g_variant_unref (value);
1919 g_free (string);
1921 if (!strict)
1923 fprintf (stderr, _("; ignoring override for this key.\n"));
1924 continue;
1927 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1928 g_key_file_free (key_file);
1929 g_strfreev (groups);
1930 g_strfreev (keys);
1932 return FALSE;
1936 else if (state->strinfo->len)
1938 if (!is_valid_choices (value, state->strinfo))
1940 fprintf (stderr,
1941 _("override for key '%s' in schema '%s' in "
1942 "override file '%s' is not in the list "
1943 "of valid choices"),
1944 key, group, filename);
1946 g_variant_unref (value);
1947 g_free (string);
1949 if (!strict)
1951 fprintf (stderr, _("; ignoring override for this key.\n"));
1952 continue;
1955 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1956 g_key_file_free (key_file);
1957 g_strfreev (groups);
1958 g_strfreev (keys);
1960 return FALSE;
1964 g_variant_unref (state->default_value);
1965 state->default_value = value;
1966 g_free (string);
1969 g_strfreev (keys);
1972 g_strfreev (groups);
1975 return TRUE;
1979 main (int argc, char **argv)
1981 GError *error;
1982 GHashTable *table;
1983 GDir *dir;
1984 const gchar *file;
1985 gchar *srcdir;
1986 gchar *targetdir = NULL;
1987 gchar *target;
1988 gboolean dry_run = FALSE;
1989 gboolean strict = FALSE;
1990 gchar **schema_files = NULL;
1991 gchar **override_files = NULL;
1992 GOptionContext *context;
1993 GOptionEntry entries[] = {
1994 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1995 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
1996 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1997 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1999 /* These options are only for use in the gschema-compile tests */
2000 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2001 { NULL }
2004 #ifdef G_OS_WIN32
2005 gchar *tmp;
2006 #endif
2008 setlocale (LC_ALL, "");
2009 textdomain (GETTEXT_PACKAGE);
2011 #ifdef G_OS_WIN32
2012 tmp = _glib_get_locale_dir ();
2013 bindtextdomain (GETTEXT_PACKAGE, tmp);
2014 g_free (tmp);
2015 #else
2016 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2017 #endif
2019 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2020 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2021 #endif
2023 context = g_option_context_new (N_("DIRECTORY"));
2024 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2025 g_option_context_set_summary (context,
2026 N_("Compile all GSettings schema files into a schema cache.\n"
2027 "Schema files are required to have the extension .gschema.xml,\n"
2028 "and the cache file is called gschemas.compiled."));
2029 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2031 error = NULL;
2032 if (!g_option_context_parse (context, &argc, &argv, &error))
2034 fprintf (stderr, "%s\n", error->message);
2035 return 1;
2038 g_option_context_free (context);
2040 if (!schema_files && argc != 2)
2042 fprintf (stderr, _("You should give exactly one directory name\n"));
2043 return 1;
2046 srcdir = argv[1];
2048 if (targetdir == NULL)
2049 targetdir = srcdir;
2051 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
2053 if (!schema_files)
2055 GPtrArray *overrides;
2056 GPtrArray *files;
2058 files = g_ptr_array_new ();
2059 overrides = g_ptr_array_new ();
2061 dir = g_dir_open (srcdir, 0, &error);
2062 if (dir == NULL)
2064 fprintf (stderr, "%s\n", error->message);
2065 return 1;
2068 while ((file = g_dir_read_name (dir)) != NULL)
2070 if (g_str_has_suffix (file, ".gschema.xml") ||
2071 g_str_has_suffix (file, ".enums.xml"))
2072 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2074 else if (g_str_has_suffix (file, ".gschema.override"))
2075 g_ptr_array_add (overrides,
2076 g_build_filename (srcdir, file, NULL));
2079 if (files->len == 0)
2081 fprintf (stdout, _("No schema files found: "));
2083 if (g_unlink (target))
2084 fprintf (stdout, _("doing nothing.\n"));
2086 else
2087 fprintf (stdout, _("removed existing output file.\n"));
2089 return 0;
2091 g_ptr_array_sort (files, compare_strings);
2092 g_ptr_array_add (files, NULL);
2094 g_ptr_array_sort (overrides, compare_strings);
2095 g_ptr_array_add (overrides, NULL);
2097 schema_files = (char **) g_ptr_array_free (files, FALSE);
2098 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2101 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2103 g_free (target);
2104 return 1;
2107 if (override_files != NULL &&
2108 !set_overrides (table, override_files, strict))
2110 g_free (target);
2111 return 1;
2114 if (!dry_run && !write_to_file (table, target, &error))
2116 fprintf (stderr, "%s\n", error->message);
2117 g_free (target);
2118 return 1;
2121 g_free (target);
2123 return 0;
2126 /* Epilogue {{{1 */
2128 /* vim:set foldmethod=marker: */