2.54.2
[glib.git] / gio / glib-compile-schemas.c
blobb8de0907248f6860d776e647b87ff2f42d1ea051
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.1 of the License, 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;
196 gboolean summary_seen;
197 gboolean description_seen;
198 } KeyState;
200 static KeyState *
201 key_state_new (const gchar *type_string,
202 const gchar *gettext_domain,
203 gboolean is_enum,
204 gboolean is_flags,
205 GString *strinfo)
207 KeyState *state;
209 state = g_slice_new0 (KeyState);
210 state->type = g_variant_type_new (type_string);
211 state->have_gettext_domain = gettext_domain != NULL;
212 state->is_enum = is_enum;
213 state->is_flags = is_flags;
214 state->summary_seen = FALSE;
215 state->description_seen = FALSE;
217 if (strinfo)
218 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
219 else
220 state->strinfo = g_string_new (NULL);
222 return state;
225 static KeyState *
226 key_state_override (KeyState *state,
227 const gchar *gettext_domain)
229 KeyState *copy;
231 copy = g_slice_new0 (KeyState);
232 copy->type = g_variant_type_copy (state->type);
233 copy->have_gettext_domain = gettext_domain != NULL;
234 copy->strinfo = g_string_new_len (state->strinfo->str,
235 state->strinfo->len);
236 copy->is_enum = state->is_enum;
237 copy->is_flags = state->is_flags;
238 copy->is_override = TRUE;
240 if (state->minimum)
242 copy->minimum = g_variant_ref (state->minimum);
243 copy->maximum = g_variant_ref (state->maximum);
246 return copy;
249 static KeyState *
250 key_state_new_child (const gchar *child_schema)
252 KeyState *state;
254 state = g_slice_new0 (KeyState);
255 state->child_schema = g_strdup (child_schema);
257 return state;
260 static gboolean
261 is_valid_choices (GVariant *variant,
262 GString *strinfo)
264 switch (g_variant_classify (variant))
266 case G_VARIANT_CLASS_MAYBE:
267 case G_VARIANT_CLASS_ARRAY:
269 gboolean valid = TRUE;
270 GVariantIter iter;
272 g_variant_iter_init (&iter, variant);
274 while (valid && (variant = g_variant_iter_next_value (&iter)))
276 valid = is_valid_choices (variant, strinfo);
277 g_variant_unref (variant);
280 return valid;
283 case G_VARIANT_CLASS_STRING:
284 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
285 strinfo->len / 4,
286 g_variant_get_string (variant, NULL));
288 default:
289 g_assert_not_reached ();
294 /* Gets called at </default> </choices> or <range/> to check for
295 * validity of the default value so that any inconsistency is
296 * reported as soon as it is encountered.
298 static void
299 key_state_check_range (KeyState *state,
300 GError **error)
302 if (state->default_value)
304 const gchar *tag;
306 tag = state->is_override ? "override" : "default";
308 if (state->minimum)
310 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
311 g_variant_compare (state->default_value, state->maximum) > 0)
313 g_set_error (error, G_MARKUP_ERROR,
314 G_MARKUP_ERROR_INVALID_CONTENT,
315 _("<%s> is not contained in "
316 "the specified range"), tag);
320 else if (state->strinfo->len)
322 if (!is_valid_choices (state->default_value, state->strinfo))
324 if (state->is_enum)
325 g_set_error (error, G_MARKUP_ERROR,
326 G_MARKUP_ERROR_INVALID_CONTENT,
327 _("<%s> is not a valid member of "
328 "the specified enumerated type"), tag);
330 else if (state->is_flags)
331 g_set_error (error, G_MARKUP_ERROR,
332 G_MARKUP_ERROR_INVALID_CONTENT,
333 _("<%s> contains string not in the "
334 "specified flags type"), tag);
336 else
337 g_set_error (error, G_MARKUP_ERROR,
338 G_MARKUP_ERROR_INVALID_CONTENT,
339 _("<%s> contains a string not in "
340 "<choices>"), tag);
346 static void
347 key_state_set_range (KeyState *state,
348 const gchar *min_str,
349 const gchar *max_str,
350 GError **error)
352 const struct {
353 const gchar type;
354 const gchar *min;
355 const gchar *max;
356 } table[] = {
357 { 'y', "0", "255" },
358 { 'n', "-32768", "32767" },
359 { 'q', "0", "65535" },
360 { 'i', "-2147483648", "2147483647" },
361 { 'u', "0", "4294967295" },
362 { 'x', "-9223372036854775808", "9223372036854775807" },
363 { 't', "0", "18446744073709551615" },
364 { 'd', "-inf", "inf" },
366 gboolean type_ok = FALSE;
367 gint i;
369 if (state->minimum)
371 g_set_error_literal (error, G_MARKUP_ERROR,
372 G_MARKUP_ERROR_INVALID_CONTENT,
373 _("<range/> already specified for this key"));
374 return;
377 for (i = 0; i < G_N_ELEMENTS (table); i++)
378 if (*(char *) state->type == table[i].type)
380 min_str = min_str ? min_str : table[i].min;
381 max_str = max_str ? max_str : table[i].max;
382 type_ok = TRUE;
383 break;
386 if (!type_ok)
388 gchar *type = g_variant_type_dup_string (state->type);
389 g_set_error (error, G_MARKUP_ERROR,
390 G_MARKUP_ERROR_INVALID_CONTENT,
391 _("<range> not allowed for keys of type “%s”"), type);
392 g_free (type);
393 return;
396 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
397 if (state->minimum == NULL)
398 return;
400 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
401 if (state->maximum == NULL)
402 return;
404 if (g_variant_compare (state->minimum, state->maximum) > 0)
406 g_set_error (error, G_MARKUP_ERROR,
407 G_MARKUP_ERROR_INVALID_CONTENT,
408 _("<range> specified minimum is greater than maximum"));
409 return;
412 key_state_check_range (state, error);
415 static GString *
416 key_state_start_default (KeyState *state,
417 const gchar *l10n,
418 const gchar *context,
419 GError **error)
421 if (l10n != NULL)
423 if (strcmp (l10n, "messages") == 0)
424 state->l10n = 'm';
426 else if (strcmp (l10n, "time") == 0)
427 state->l10n = 't';
429 else
431 g_set_error (error, G_MARKUP_ERROR,
432 G_MARKUP_ERROR_INVALID_CONTENT,
433 _("unsupported l10n category: %s"), l10n);
434 return NULL;
437 if (!state->have_gettext_domain)
439 g_set_error_literal (error, G_MARKUP_ERROR,
440 G_MARKUP_ERROR_INVALID_CONTENT,
441 _("l10n requested, but no "
442 "gettext domain given"));
443 return NULL;
446 state->l10n_context = g_strdup (context);
449 else if (context != NULL)
451 g_set_error_literal (error, G_MARKUP_ERROR,
452 G_MARKUP_ERROR_INVALID_CONTENT,
453 _("translation context given for "
454 "value without l10n enabled"));
455 return NULL;
458 return g_string_new (NULL);
461 static void
462 key_state_end_default (KeyState *state,
463 GString **string,
464 GError **error)
466 state->unparsed_default_value = *string;
467 *string = NULL;
469 state->default_value = g_variant_parse (state->type,
470 state->unparsed_default_value->str,
471 NULL, NULL, error);
472 if (!state->default_value)
474 gchar *type = g_variant_type_dup_string (state->type);
475 g_prefix_error (error, _("Failed to parse <default> value of type “%s”: "), type);
476 g_free (type);
479 key_state_check_range (state, error);
482 static void
483 key_state_start_choices (KeyState *state,
484 GError **error)
486 const GVariantType *type = state->type;
488 if (state->is_enum)
490 g_set_error_literal (error, G_MARKUP_ERROR,
491 G_MARKUP_ERROR_INVALID_CONTENT,
492 _("<choices> cannot be specified for keys "
493 "tagged as having an enumerated type"));
494 return;
497 if (state->has_choices)
499 g_set_error_literal (error, G_MARKUP_ERROR,
500 G_MARKUP_ERROR_INVALID_CONTENT,
501 _("<choices> already specified for this key"));
502 return;
505 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
506 type = g_variant_type_element (type);
508 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
510 gchar *type_string = g_variant_type_dup_string (state->type);
511 g_set_error (error, G_MARKUP_ERROR,
512 G_MARKUP_ERROR_INVALID_CONTENT,
513 _("<choices> not allowed for keys of type “%s”"),
514 type_string);
515 g_free (type_string);
516 return;
520 static void
521 key_state_add_choice (KeyState *state,
522 const gchar *choice,
523 GError **error)
525 if (strinfo_builder_contains (state->strinfo, choice))
527 g_set_error (error, G_MARKUP_ERROR,
528 G_MARKUP_ERROR_INVALID_CONTENT,
529 _("<choice value='%s'/> already given"), choice);
530 return;
533 strinfo_builder_append_item (state->strinfo, choice, 0);
534 state->has_choices = TRUE;
537 static void
538 key_state_end_choices (KeyState *state,
539 GError **error)
541 if (!state->has_choices)
543 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
544 _("<choices> must contain at least one <choice>"));
545 return;
548 key_state_check_range (state, error);
551 static void
552 key_state_start_aliases (KeyState *state,
553 GError **error)
555 if (state->has_aliases)
556 g_set_error_literal (error, G_MARKUP_ERROR,
557 G_MARKUP_ERROR_INVALID_CONTENT,
558 _("<aliases> already specified for this key"));
559 else if (!state->is_flags && !state->is_enum && !state->has_choices)
560 g_set_error_literal (error, G_MARKUP_ERROR,
561 G_MARKUP_ERROR_INVALID_CONTENT,
562 _("<aliases> can only be specified for keys with "
563 "enumerated or flags types or after <choices>"));
566 static void
567 key_state_add_alias (KeyState *state,
568 const gchar *alias,
569 const gchar *target,
570 GError **error)
572 if (strinfo_builder_contains (state->strinfo, alias))
574 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
575 state->strinfo->len / 4,
576 alias))
578 if (state->is_enum)
579 g_set_error (error, G_MARKUP_ERROR,
580 G_MARKUP_ERROR_INVALID_CONTENT,
581 _("<alias value='%s'/> given when “%s” is already "
582 "a member of the enumerated type"), alias, alias);
584 else
585 g_set_error (error, G_MARKUP_ERROR,
586 G_MARKUP_ERROR_INVALID_CONTENT,
587 _("<alias value='%s'/> given when "
588 "<choice value='%s'/> was already given"),
589 alias, alias);
592 else
593 g_set_error (error, G_MARKUP_ERROR,
594 G_MARKUP_ERROR_INVALID_CONTENT,
595 _("<alias value='%s'/> already specified"), alias);
597 return;
600 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
602 g_set_error (error, G_MARKUP_ERROR,
603 G_MARKUP_ERROR_INVALID_CONTENT,
604 state->is_enum ?
605 _("alias target “%s” is not in enumerated type") :
606 _("alias target “%s” is not in <choices>"),
607 target);
608 return;
611 state->has_aliases = TRUE;
614 static void
615 key_state_end_aliases (KeyState *state,
616 GError **error)
618 if (!state->has_aliases)
620 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
621 _("<aliases> must contain at least one <alias>"));
622 return;
626 static gboolean
627 key_state_check (KeyState *state,
628 GError **error)
630 if (state->checked)
631 return TRUE;
633 return state->checked = TRUE;
636 static GVariant *
637 key_state_serialise (KeyState *state)
639 if (state->serialised == NULL)
641 if (state->child_schema)
643 state->serialised = g_variant_new_string (state->child_schema);
646 else
648 GVariantBuilder builder;
650 g_assert (key_state_check (state, NULL));
652 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
654 /* default value */
655 g_variant_builder_add_value (&builder, state->default_value);
657 /* translation */
658 if (state->l10n)
660 /* We are going to store the untranslated default for
661 * runtime translation according to the current locale.
662 * We need to strip leading and trailing whitespace from
663 * the string so that it's exactly the same as the one
664 * that ended up in the .po file for translation.
666 * We want to do this so that
668 * <default l10n='messages'>
669 * ['a', 'b', 'c']
670 * </default>
672 * ends up in the .po file like "['a', 'b', 'c']",
673 * omitting the extra whitespace at the start and end.
675 strip_string (state->unparsed_default_value);
677 if (state->l10n_context)
679 gint len;
681 /* Contextified messages are supported by prepending
682 * the context, followed by '\004' to the start of the
683 * message string. We do that here to save GSettings
684 * the work later on.
686 len = strlen (state->l10n_context);
687 state->l10n_context[len] = '\004';
688 g_string_prepend_len (state->unparsed_default_value,
689 state->l10n_context, len + 1);
690 g_free (state->l10n_context);
691 state->l10n_context = NULL;
694 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
695 state->unparsed_default_value->str);
696 g_string_free (state->unparsed_default_value, TRUE);
697 state->unparsed_default_value = NULL;
700 /* choice, aliases, enums */
701 if (state->strinfo->len)
703 GVariant *array;
704 guint32 *words;
705 gpointer data;
706 gsize size;
707 gint i;
709 data = state->strinfo->str;
710 size = state->strinfo->len;
712 words = data;
713 for (i = 0; i < size / sizeof (guint32); i++)
714 words[i] = GUINT32_TO_LE (words[i]);
716 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
717 data, size, TRUE,
718 g_free, data);
720 g_string_free (state->strinfo, FALSE);
721 state->strinfo = NULL;
723 g_variant_builder_add (&builder, "(y@au)",
724 state->is_flags ? 'f' :
725 state->is_enum ? 'e' : 'c',
726 array);
729 /* range */
730 if (state->minimum || state->maximum)
731 g_variant_builder_add (&builder, "(y(**))", 'r',
732 state->minimum, state->maximum);
734 state->serialised = g_variant_builder_end (&builder);
737 g_variant_ref_sink (state->serialised);
740 return g_variant_ref (state->serialised);
743 static void
744 key_state_free (gpointer data)
746 KeyState *state = data;
748 if (state->type)
749 g_variant_type_free (state->type);
751 g_free (state->l10n_context);
753 if (state->unparsed_default_value)
754 g_string_free (state->unparsed_default_value, TRUE);
756 if (state->default_value)
757 g_variant_unref (state->default_value);
759 if (state->strinfo)
760 g_string_free (state->strinfo, TRUE);
762 if (state->minimum)
763 g_variant_unref (state->minimum);
765 if (state->maximum)
766 g_variant_unref (state->maximum);
768 if (state->serialised)
769 g_variant_unref (state->serialised);
771 g_slice_free (KeyState, state);
774 /* Key name validity {{{1 */
775 static gboolean allow_any_name = FALSE;
777 static gboolean
778 is_valid_keyname (const gchar *key,
779 GError **error)
781 gint i;
783 if (key[0] == '\0')
785 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
786 _("Empty names are not permitted"));
787 return FALSE;
790 if (allow_any_name)
791 return TRUE;
793 if (!g_ascii_islower (key[0]))
795 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
796 _("Invalid name “%s”: names must begin "
797 "with a lowercase letter"), key);
798 return FALSE;
801 for (i = 1; key[i]; i++)
803 if (key[i] != '-' &&
804 !g_ascii_islower (key[i]) &&
805 !g_ascii_isdigit (key[i]))
807 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
808 _("Invalid name “%s”: invalid character “%c”; "
809 "only lowercase letters, numbers and hyphen (“-”) "
810 "are permitted"), key, key[i]);
811 return FALSE;
814 if (key[i] == '-' && key[i + 1] == '-')
816 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
817 _("Invalid name “%s”: two successive hyphens (“--”) "
818 "are not permitted"), key);
819 return FALSE;
823 if (key[i - 1] == '-')
825 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
826 _("Invalid name “%s”: the last character may not be a "
827 "hyphen (“-”)"), key);
828 return FALSE;
831 if (i > 1024)
833 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
834 _("Invalid name “%s”: maximum length is 1024"), key);
835 return FALSE;
838 return TRUE;
841 /* Handling of <schema> {{{1 */
842 typedef struct _SchemaState SchemaState;
843 struct _SchemaState
845 SchemaState *extends;
847 gchar *path;
848 gchar *gettext_domain;
849 gchar *extends_name;
850 gchar *list_of;
852 GHashTable *keys;
855 static SchemaState *
856 schema_state_new (const gchar *path,
857 const gchar *gettext_domain,
858 SchemaState *extends,
859 const gchar *extends_name,
860 const gchar *list_of)
862 SchemaState *state;
864 state = g_slice_new (SchemaState);
865 state->path = g_strdup (path);
866 state->gettext_domain = g_strdup (gettext_domain);
867 state->extends = extends;
868 state->extends_name = g_strdup (extends_name);
869 state->list_of = g_strdup (list_of);
870 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
871 g_free, key_state_free);
873 return state;
876 static void
877 schema_state_free (gpointer data)
879 SchemaState *state = data;
881 g_free (state->path);
882 g_free (state->gettext_domain);
883 g_hash_table_unref (state->keys);
884 g_slice_free (SchemaState, state);
887 static void
888 schema_state_add_child (SchemaState *state,
889 const gchar *name,
890 const gchar *schema,
891 GError **error)
893 gchar *childname;
895 if (!is_valid_keyname (name, error))
896 return;
898 childname = g_strconcat (name, "/", NULL);
900 if (g_hash_table_lookup (state->keys, childname))
902 g_set_error (error, G_MARKUP_ERROR,
903 G_MARKUP_ERROR_INVALID_CONTENT,
904 _("<child name='%s'> already specified"), name);
905 return;
908 g_hash_table_insert (state->keys, childname,
909 key_state_new_child (schema));
912 static KeyState *
913 schema_state_add_key (SchemaState *state,
914 GHashTable *enum_table,
915 GHashTable *flags_table,
916 const gchar *name,
917 const gchar *type_string,
918 const gchar *enum_type,
919 const gchar *flags_type,
920 GError **error)
922 SchemaState *node;
923 GString *strinfo;
924 KeyState *key;
926 if (state->list_of)
928 g_set_error_literal (error, G_MARKUP_ERROR,
929 G_MARKUP_ERROR_INVALID_CONTENT,
930 _("Cannot add keys to a “list-of” schema"));
931 return NULL;
934 if (!is_valid_keyname (name, error))
935 return NULL;
937 if (g_hash_table_lookup (state->keys, name))
939 g_set_error (error, G_MARKUP_ERROR,
940 G_MARKUP_ERROR_INVALID_CONTENT,
941 _("<key name='%s'> already specified"), name);
942 return NULL;
945 for (node = state; node; node = node->extends)
946 if (node->extends)
948 KeyState *shadow;
950 shadow = g_hash_table_lookup (node->extends->keys, name);
952 /* in case of <key> <override> <key> make sure we report the
953 * location of the original <key>, not the <override>.
955 if (shadow && !shadow->is_override)
957 g_set_error (error, G_MARKUP_ERROR,
958 G_MARKUP_ERROR_INVALID_CONTENT,
959 _("<key name='%s'> shadows <key name='%s'> in "
960 "<schema id='%s'>; use <override> to modify value"),
961 name, name, node->extends_name);
962 return NULL;
966 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
968 g_set_error (error, G_MARKUP_ERROR,
969 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
970 _("Exactly one of “type”, “enum” or “flags” must "
971 "be specified as an attribute to <key>"));
972 return NULL;
975 if (type_string == NULL) /* flags or enums was specified */
977 EnumState *enum_state;
979 if (enum_type)
980 enum_state = g_hash_table_lookup (enum_table, enum_type);
981 else
982 enum_state = g_hash_table_lookup (flags_table, flags_type);
985 if (enum_state == NULL)
987 g_set_error (error, G_MARKUP_ERROR,
988 G_MARKUP_ERROR_INVALID_CONTENT,
989 _("<%s id='%s'> not (yet) defined."),
990 flags_type ? "flags" : "enum",
991 flags_type ? flags_type : enum_type);
992 return NULL;
995 type_string = flags_type ? "as" : "s";
996 strinfo = enum_state->strinfo;
998 else
1000 if (!g_variant_type_string_is_valid (type_string))
1002 g_set_error (error, G_MARKUP_ERROR,
1003 G_MARKUP_ERROR_INVALID_CONTENT,
1004 _("Invalid GVariant type string “%s”"), type_string);
1005 return NULL;
1008 strinfo = NULL;
1011 key = key_state_new (type_string, state->gettext_domain,
1012 enum_type != NULL, flags_type != NULL, strinfo);
1013 g_hash_table_insert (state->keys, g_strdup (name), key);
1015 return key;
1018 static void
1019 schema_state_add_override (SchemaState *state,
1020 KeyState **key_state,
1021 GString **string,
1022 const gchar *key,
1023 const gchar *l10n,
1024 const gchar *context,
1025 GError **error)
1027 SchemaState *parent;
1028 KeyState *original;
1030 if (state->extends == NULL)
1032 g_set_error_literal (error, G_MARKUP_ERROR,
1033 G_MARKUP_ERROR_INVALID_CONTENT,
1034 _("<override> given but schema isn’t "
1035 "extending anything"));
1036 return;
1039 for (parent = state->extends; parent; parent = parent->extends)
1040 if ((original = g_hash_table_lookup (parent->keys, key)))
1041 break;
1043 if (original == NULL)
1045 g_set_error (error, G_MARKUP_ERROR,
1046 G_MARKUP_ERROR_INVALID_CONTENT,
1047 _("No <key name='%s'> to override"), key);
1048 return;
1051 if (g_hash_table_lookup (state->keys, key))
1053 g_set_error (error, G_MARKUP_ERROR,
1054 G_MARKUP_ERROR_INVALID_CONTENT,
1055 _("<override name='%s'> already specified"), key);
1056 return;
1059 *key_state = key_state_override (original, state->gettext_domain);
1060 *string = key_state_start_default (*key_state, l10n, context, error);
1061 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1064 static void
1065 override_state_end (KeyState **key_state,
1066 GString **string,
1067 GError **error)
1069 key_state_end_default (*key_state, string, error);
1070 *key_state = NULL;
1073 /* Handling of toplevel state {{{1 */
1074 typedef struct
1076 gboolean strict; /* TRUE if --strict was given */
1078 GHashTable *schema_table; /* string -> SchemaState */
1079 GHashTable *flags_table; /* string -> EnumState */
1080 GHashTable *enum_table; /* string -> EnumState */
1082 GSList *this_file_schemas; /* strings: <schema>s in this file */
1083 GSList *this_file_flagss; /* strings: <flags>s in this file */
1084 GSList *this_file_enums; /* strings: <enum>s in this file */
1086 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1088 SchemaState *schema_state; /* non-NULL when inside <schema> */
1089 KeyState *key_state; /* non-NULL when inside <key> */
1090 EnumState *enum_state; /* non-NULL when inside <enum> */
1092 GString *string; /* non-NULL when accepting text */
1093 } ParseState;
1095 static gboolean
1096 is_subclass (const gchar *class_name,
1097 const gchar *possible_parent,
1098 GHashTable *schema_table)
1100 SchemaState *class;
1102 if (strcmp (class_name, possible_parent) == 0)
1103 return TRUE;
1105 class = g_hash_table_lookup (schema_table, class_name);
1106 g_assert (class != NULL);
1108 return class->extends_name &&
1109 is_subclass (class->extends_name, possible_parent, schema_table);
1112 static void
1113 parse_state_start_schema (ParseState *state,
1114 const gchar *id,
1115 const gchar *path,
1116 const gchar *gettext_domain,
1117 const gchar *extends_name,
1118 const gchar *list_of,
1119 GError **error)
1121 SchemaState *extends;
1122 gchar *my_id;
1124 if (g_hash_table_lookup (state->schema_table, id))
1126 g_set_error (error, G_MARKUP_ERROR,
1127 G_MARKUP_ERROR_INVALID_CONTENT,
1128 _("<schema id='%s'> already specified"), id);
1129 return;
1132 if (extends_name)
1134 extends = g_hash_table_lookup (state->schema_table, extends_name);
1136 if (extends == NULL)
1138 g_set_error (error, G_MARKUP_ERROR,
1139 G_MARKUP_ERROR_INVALID_CONTENT,
1140 _("<schema id='%s'> extends not yet existing "
1141 "schema “%s”"), id, extends_name);
1142 return;
1145 else
1146 extends = NULL;
1148 if (list_of)
1150 SchemaState *tmp;
1152 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1154 g_set_error (error, G_MARKUP_ERROR,
1155 G_MARKUP_ERROR_INVALID_CONTENT,
1156 _("<schema id='%s'> is list of not yet existing "
1157 "schema “%s”"), id, list_of);
1158 return;
1161 if (tmp->path)
1163 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1164 _("Cannot be a list of a schema with a path"));
1165 return;
1169 if (extends)
1171 if (extends->path)
1173 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1174 _("Cannot extend a schema with a path"));
1175 return;
1178 if (list_of)
1180 if (extends->list_of == NULL)
1182 g_set_error (error, G_MARKUP_ERROR,
1183 G_MARKUP_ERROR_INVALID_CONTENT,
1184 _("<schema id='%s'> is a list, extending "
1185 "<schema id='%s'> which is not a list"),
1186 id, extends_name);
1187 return;
1190 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1192 g_set_error (error, G_MARKUP_ERROR,
1193 G_MARKUP_ERROR_INVALID_CONTENT,
1194 _("<schema id='%s' list-of='%s'> extends <schema "
1195 "id='%s' list-of='%s'> but “%s” does not "
1196 "extend “%s”"), id, list_of, extends_name,
1197 extends->list_of, list_of, extends->list_of);
1198 return;
1201 else
1202 /* by default we are a list of the same thing that the schema
1203 * we are extending is a list of (which might be nothing)
1205 list_of = extends->list_of;
1208 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1210 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1211 _("A path, if given, must begin and end with a slash"));
1212 return;
1215 if (path && list_of && !g_str_has_suffix (path, ":/"))
1217 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1218 _("The path of a list must end with “:/”"));
1219 return;
1222 if (path && (g_str_has_prefix (path, "/apps/") ||
1223 g_str_has_prefix (path, "/desktop/") ||
1224 g_str_has_prefix (path, "/system/")))
1226 gchar *message = NULL;
1227 message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1228 "Paths starting with "
1229 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1230 id, path);
1231 g_printerr ("%s\n", message);
1232 g_free (message);
1235 state->schema_state = schema_state_new (path, gettext_domain,
1236 extends, extends_name, list_of);
1238 my_id = g_strdup (id);
1239 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1240 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1243 static void
1244 parse_state_start_enum (ParseState *state,
1245 const gchar *id,
1246 gboolean is_flags,
1247 GError **error)
1249 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1250 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1251 gchar *my_id;
1253 if (g_hash_table_lookup (table, id))
1255 g_set_error (error, G_MARKUP_ERROR,
1256 G_MARKUP_ERROR_INVALID_CONTENT,
1257 _("<%s id='%s'> already specified"),
1258 is_flags ? "flags" : "enum", id);
1259 return;
1262 state->enum_state = enum_state_new (is_flags);
1264 my_id = g_strdup (id);
1265 *list = g_slist_prepend (*list, my_id);
1266 g_hash_table_insert (table, my_id, state->enum_state);
1269 /* GMarkup Parser Functions {{{1 */
1271 /* Start element {{{2 */
1272 static void
1273 start_element (GMarkupParseContext *context,
1274 const gchar *element_name,
1275 const gchar **attribute_names,
1276 const gchar **attribute_values,
1277 gpointer user_data,
1278 GError **error)
1280 ParseState *state = user_data;
1281 const GSList *element_stack;
1282 const gchar *container;
1284 element_stack = g_markup_parse_context_get_element_stack (context);
1285 container = element_stack->next ? element_stack->next->data : NULL;
1287 #define COLLECT(first, ...) \
1288 g_markup_collect_attributes (element_name, \
1289 attribute_names, attribute_values, error, \
1290 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1291 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1292 #define STRDUP G_MARKUP_COLLECT_STRDUP
1293 #define STRING G_MARKUP_COLLECT_STRING
1294 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1296 /* Toplevel items {{{3 */
1297 if (container == NULL)
1299 if (strcmp (element_name, "schemalist") == 0)
1301 COLLECT (OPTIONAL | STRDUP,
1302 "gettext-domain",
1303 &state->schemalist_domain);
1304 return;
1309 /* children of <schemalist> {{{3 */
1310 else if (strcmp (container, "schemalist") == 0)
1312 if (strcmp (element_name, "schema") == 0)
1314 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1315 if (COLLECT (STRING, "id", &id,
1316 OPTIONAL | STRING, "path", &path,
1317 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1318 OPTIONAL | STRING, "extends", &extends,
1319 OPTIONAL | STRING, "list-of", &list_of))
1320 parse_state_start_schema (state, id, path,
1321 gettext_domain ? gettext_domain
1322 : state->schemalist_domain,
1323 extends, list_of, error);
1324 return;
1327 else if (strcmp (element_name, "enum") == 0)
1329 const gchar *id;
1330 if (COLLECT (STRING, "id", &id))
1331 parse_state_start_enum (state, id, FALSE, error);
1332 return;
1335 else if (strcmp (element_name, "flags") == 0)
1337 const gchar *id;
1338 if (COLLECT (STRING, "id", &id))
1339 parse_state_start_enum (state, id, TRUE, error);
1340 return;
1345 /* children of <schema> {{{3 */
1346 else if (strcmp (container, "schema") == 0)
1348 if (strcmp (element_name, "key") == 0)
1350 const gchar *name, *type_string, *enum_type, *flags_type;
1352 if (COLLECT (STRING, "name", &name,
1353 OPTIONAL | STRING, "type", &type_string,
1354 OPTIONAL | STRING, "enum", &enum_type,
1355 OPTIONAL | STRING, "flags", &flags_type))
1357 state->key_state = schema_state_add_key (state->schema_state,
1358 state->enum_table,
1359 state->flags_table,
1360 name, type_string,
1361 enum_type, flags_type,
1362 error);
1363 return;
1365 else if (strcmp (element_name, "child") == 0)
1367 const gchar *name, *schema;
1369 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1370 schema_state_add_child (state->schema_state,
1371 name, schema, error);
1372 return;
1374 else if (strcmp (element_name, "override") == 0)
1376 const gchar *name, *l10n, *context;
1378 if (COLLECT (STRING, "name", &name,
1379 OPTIONAL | STRING, "l10n", &l10n,
1380 OPTIONAL | STRING, "context", &context))
1381 schema_state_add_override (state->schema_state,
1382 &state->key_state, &state->string,
1383 name, l10n, context, error);
1384 return;
1388 /* children of <key> {{{3 */
1389 else if (strcmp (container, "key") == 0)
1391 if (strcmp (element_name, "default") == 0)
1393 const gchar *l10n, *context;
1394 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1395 STRING | OPTIONAL, "context", &context))
1396 state->string = key_state_start_default (state->key_state,
1397 l10n, context, error);
1398 return;
1401 else if (strcmp (element_name, "summary") == 0)
1403 if (NO_ATTRS ())
1405 if (state->key_state->summary_seen && state->strict)
1406 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1407 _("Only one <%s> element allowed inside <%s>"),
1408 element_name, container);
1409 else
1410 state->string = g_string_new (NULL);
1412 state->key_state->summary_seen = TRUE;
1414 return;
1417 else if (strcmp (element_name, "description") == 0)
1419 if (NO_ATTRS ())
1421 if (state->key_state->description_seen && state->strict)
1422 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1423 _("Only one <%s> element allowed inside <%s>"),
1424 element_name, container);
1425 else
1426 state->string = g_string_new (NULL);
1428 state->key_state->description_seen = TRUE;
1430 return;
1433 else if (strcmp (element_name, "range") == 0)
1435 const gchar *min, *max;
1436 if (COLLECT (STRING | OPTIONAL, "min", &min,
1437 STRING | OPTIONAL, "max", &max))
1438 key_state_set_range (state->key_state, min, max, error);
1439 return;
1442 else if (strcmp (element_name, "choices") == 0)
1444 if (NO_ATTRS ())
1445 key_state_start_choices (state->key_state, error);
1446 return;
1449 else if (strcmp (element_name, "aliases") == 0)
1451 if (NO_ATTRS ())
1452 key_state_start_aliases (state->key_state, error);
1453 return;
1458 /* children of <choices> {{{3 */
1459 else if (strcmp (container, "choices") == 0)
1461 if (strcmp (element_name, "choice") == 0)
1463 const gchar *value;
1464 if (COLLECT (STRING, "value", &value))
1465 key_state_add_choice (state->key_state, value, error);
1466 return;
1471 /* children of <aliases> {{{3 */
1472 else if (strcmp (container, "aliases") == 0)
1474 if (strcmp (element_name, "alias") == 0)
1476 const gchar *value, *target;
1477 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1478 key_state_add_alias (state->key_state, value, target, error);
1479 return;
1484 /* children of <enum> {{{3 */
1485 else if (strcmp (container, "enum") == 0 ||
1486 strcmp (container, "flags") == 0)
1488 if (strcmp (element_name, "value") == 0)
1490 const gchar *nick, *valuestr;
1491 if (COLLECT (STRING, "nick", &nick,
1492 STRING, "value", &valuestr))
1493 enum_state_add_value (state->enum_state, nick, valuestr, error);
1494 return;
1497 /* 3}}} */
1499 if (container)
1500 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1501 _("Element <%s> not allowed inside <%s>"),
1502 element_name, container);
1503 else
1504 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1505 _("Element <%s> not allowed at the top level"), element_name);
1507 /* 2}}} */
1508 /* End element {{{2 */
1510 static void
1511 key_state_end (KeyState **state_ptr,
1512 GError **error)
1514 KeyState *state;
1516 state = *state_ptr;
1517 *state_ptr = NULL;
1519 if (state->default_value == NULL)
1521 g_set_error_literal (error,
1522 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1523 _("Element <default> is required in <key>"));
1524 return;
1528 static void
1529 schema_state_end (SchemaState **state_ptr,
1530 GError **error)
1532 *state_ptr = NULL;
1535 static void
1536 end_element (GMarkupParseContext *context,
1537 const gchar *element_name,
1538 gpointer user_data,
1539 GError **error)
1541 ParseState *state = user_data;
1543 if (strcmp (element_name, "schemalist") == 0)
1545 g_free (state->schemalist_domain);
1546 state->schemalist_domain = NULL;
1549 else if (strcmp (element_name, "enum") == 0 ||
1550 strcmp (element_name, "flags") == 0)
1551 enum_state_end (&state->enum_state, error);
1553 else if (strcmp (element_name, "schema") == 0)
1554 schema_state_end (&state->schema_state, error);
1556 else if (strcmp (element_name, "override") == 0)
1557 override_state_end (&state->key_state, &state->string, error);
1559 else if (strcmp (element_name, "key") == 0)
1560 key_state_end (&state->key_state, error);
1562 else if (strcmp (element_name, "default") == 0)
1563 key_state_end_default (state->key_state, &state->string, error);
1565 else if (strcmp (element_name, "choices") == 0)
1566 key_state_end_choices (state->key_state, error);
1568 else if (strcmp (element_name, "aliases") == 0)
1569 key_state_end_aliases (state->key_state, error);
1571 if (state->string)
1573 g_string_free (state->string, TRUE);
1574 state->string = NULL;
1577 /* Text {{{2 */
1578 static void
1579 text (GMarkupParseContext *context,
1580 const gchar *text,
1581 gsize text_len,
1582 gpointer user_data,
1583 GError **error)
1585 ParseState *state = user_data;
1587 if (state->string)
1589 /* we are expecting a string, so store the text data.
1591 * we store the data verbatim here and deal with whitespace
1592 * later on. there are two reasons for that:
1594 * 1) whitespace is handled differently depending on the tag
1595 * type.
1597 * 2) we could do leading whitespace removal by refusing to
1598 * insert it into state->string if it's at the start, but for
1599 * trailing whitespace, we have no idea if there is another
1600 * text() call coming or not.
1602 g_string_append_len (state->string, text, text_len);
1604 else
1606 /* string is not expected: accept (and ignore) pure whitespace */
1607 gsize i;
1609 for (i = 0; i < text_len; i++)
1610 if (!g_ascii_isspace (text[i]))
1612 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1613 _("Text may not appear inside <%s>"),
1614 g_markup_parse_context_get_element (context));
1615 break;
1620 /* Write to GVDB {{{1 */
1621 typedef struct
1623 GHashTable *table;
1624 GvdbItem *root;
1625 } GvdbPair;
1627 static void
1628 gvdb_pair_init (GvdbPair *pair)
1630 pair->table = gvdb_hash_table_new (NULL, NULL);
1631 pair->root = gvdb_hash_table_insert (pair->table, "");
1634 static void
1635 gvdb_pair_clear (GvdbPair *pair)
1637 g_hash_table_unref (pair->table);
1640 typedef struct
1642 GHashTable *schema_table;
1643 GvdbPair root_pair;
1644 } WriteToFileData;
1646 typedef struct
1648 GHashTable *schema_table;
1649 GvdbPair pair;
1650 gboolean l10n;
1651 } OutputSchemaData;
1653 static void
1654 output_key (gpointer key,
1655 gpointer value,
1656 gpointer user_data)
1658 OutputSchemaData *data;
1659 const gchar *name;
1660 KeyState *state;
1661 GvdbItem *item;
1662 GVariant *serialised = NULL;
1664 name = key;
1665 state = value;
1666 data = user_data;
1668 item = gvdb_hash_table_insert (data->pair.table, name);
1669 gvdb_item_set_parent (item, data->pair.root);
1670 serialised = key_state_serialise (state);
1671 gvdb_item_set_value (item, serialised);
1672 g_variant_unref (serialised);
1674 if (state->l10n)
1675 data->l10n = TRUE;
1677 if (state->child_schema &&
1678 !g_hash_table_lookup (data->schema_table, state->child_schema))
1680 gchar *message = NULL;
1681 message = g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1682 state->child_schema);
1683 g_printerr ("%s\n", message);
1684 g_free (message);
1688 static void
1689 output_schema (gpointer key,
1690 gpointer value,
1691 gpointer user_data)
1693 WriteToFileData *wtf_data = user_data;
1694 OutputSchemaData data;
1695 GvdbPair *root_pair;
1696 SchemaState *state;
1697 const gchar *id;
1698 GvdbItem *item;
1700 id = key;
1701 state = value;
1702 root_pair = &wtf_data->root_pair;
1704 data.schema_table = wtf_data->schema_table;
1705 gvdb_pair_init (&data.pair);
1706 data.l10n = FALSE;
1708 item = gvdb_hash_table_insert (root_pair->table, id);
1709 gvdb_item_set_parent (item, root_pair->root);
1710 gvdb_item_set_hash_table (item, data.pair.table);
1712 g_hash_table_foreach (state->keys, output_key, &data);
1714 if (state->path)
1715 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1717 if (state->extends_name)
1718 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1719 state->extends_name);
1721 if (state->list_of)
1722 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1723 state->list_of);
1725 if (data.l10n)
1726 gvdb_hash_table_insert_string (data.pair.table,
1727 ".gettext-domain",
1728 state->gettext_domain);
1730 gvdb_pair_clear (&data.pair);
1733 static gboolean
1734 write_to_file (GHashTable *schema_table,
1735 const gchar *filename,
1736 GError **error)
1738 WriteToFileData data;
1739 gboolean success;
1741 data.schema_table = schema_table;
1743 gvdb_pair_init (&data.root_pair);
1745 g_hash_table_foreach (schema_table, output_schema, &data);
1747 success = gvdb_table_write_contents (data.root_pair.table, filename,
1748 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1749 error);
1750 g_hash_table_unref (data.root_pair.table);
1752 return success;
1755 /* Parser driver {{{1 */
1756 static GHashTable *
1757 parse_gschema_files (gchar **files,
1758 gboolean strict)
1760 GMarkupParser parser = { start_element, end_element, text };
1761 ParseState state = { 0, };
1762 const gchar *filename;
1763 GError *error = NULL;
1765 state.strict = strict;
1767 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1768 g_free, enum_state_free);
1770 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1771 g_free, enum_state_free);
1773 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1774 g_free, schema_state_free);
1776 while ((filename = *files++) != NULL)
1778 GMarkupParseContext *context;
1779 gchar *contents;
1780 gsize size;
1781 gint line, col;
1783 if (!g_file_get_contents (filename, &contents, &size, &error))
1785 fprintf (stderr, "%s\n", error->message);
1786 g_clear_error (&error);
1787 continue;
1790 context = g_markup_parse_context_new (&parser,
1791 G_MARKUP_TREAT_CDATA_AS_TEXT |
1792 G_MARKUP_PREFIX_ERROR_POSITION |
1793 G_MARKUP_IGNORE_QUALIFIED,
1794 &state, NULL);
1797 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1798 !g_markup_parse_context_end_parse (context, &error))
1800 GSList *item;
1802 /* back out any changes from this file */
1803 for (item = state.this_file_schemas; item; item = item->next)
1804 g_hash_table_remove (state.schema_table, item->data);
1806 for (item = state.this_file_flagss; item; item = item->next)
1807 g_hash_table_remove (state.flags_table, item->data);
1809 for (item = state.this_file_enums; item; item = item->next)
1810 g_hash_table_remove (state.enum_table, item->data);
1812 /* let them know */
1813 g_markup_parse_context_get_position (context, &line, &col);
1814 fprintf (stderr, "%s:%d:%d %s. ", filename, line, col, error->message);
1815 g_clear_error (&error);
1817 if (strict)
1819 /* Translators: Do not translate "--strict". */
1820 fprintf (stderr, _("--strict was specified; exiting.\n"));
1821 g_hash_table_unref (state.schema_table);
1822 g_hash_table_unref (state.flags_table);
1823 g_hash_table_unref (state.enum_table);
1825 g_free (contents);
1827 return NULL;
1829 else
1830 fprintf (stderr, _("This entire file has been ignored.\n"));
1833 /* cleanup */
1834 g_free (contents);
1835 g_markup_parse_context_free (context);
1836 g_slist_free (state.this_file_schemas);
1837 g_slist_free (state.this_file_flagss);
1838 g_slist_free (state.this_file_enums);
1839 state.this_file_schemas = NULL;
1840 state.this_file_flagss = NULL;
1841 state.this_file_enums = NULL;
1844 g_hash_table_unref (state.flags_table);
1845 g_hash_table_unref (state.enum_table);
1847 return state.schema_table;
1850 static gint
1851 compare_strings (gconstpointer a,
1852 gconstpointer b)
1854 gchar *one = *(gchar **) a;
1855 gchar *two = *(gchar **) b;
1856 gint cmp;
1858 cmp = g_str_has_suffix (two, ".enums.xml") -
1859 g_str_has_suffix (one, ".enums.xml");
1861 if (!cmp)
1862 cmp = strcmp (one, two);
1864 return cmp;
1867 static gboolean
1868 set_overrides (GHashTable *schema_table,
1869 gchar **files,
1870 gboolean strict)
1872 const gchar *filename;
1873 GError *error = NULL;
1875 while ((filename = *files++))
1877 GKeyFile *key_file;
1878 gchar **groups;
1879 gint i;
1881 key_file = g_key_file_new ();
1882 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1884 fprintf (stderr, "%s: %s. ", filename, error->message);
1885 g_key_file_free (key_file);
1886 g_clear_error (&error);
1888 if (!strict)
1890 fprintf (stderr, _("Ignoring this file.\n"));
1891 continue;
1894 fprintf (stderr, _("--strict was specified; exiting.\n"));
1895 return FALSE;
1898 groups = g_key_file_get_groups (key_file, NULL);
1900 for (i = 0; groups[i]; i++)
1902 const gchar *group = groups[i];
1903 SchemaState *schema;
1904 gchar **keys;
1905 gint j;
1907 schema = g_hash_table_lookup (schema_table, group);
1909 if (schema == NULL)
1910 /* Having the schema not be installed is expected to be a
1911 * common case. Don't even emit an error message about
1912 * that.
1914 continue;
1916 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1917 g_assert (keys != NULL);
1919 for (j = 0; keys[j]; j++)
1921 const gchar *key = keys[j];
1922 KeyState *state;
1923 GVariant *value;
1924 gchar *string;
1926 state = g_hash_table_lookup (schema->keys, key);
1928 if (state == NULL)
1930 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1931 "specified in override file '%s'"),
1932 key, group, filename);
1934 if (!strict)
1936 fprintf (stderr, _("; ignoring override for this key.\n"));
1937 continue;
1940 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1941 g_key_file_free (key_file);
1942 g_strfreev (groups);
1943 g_strfreev (keys);
1945 return FALSE;
1948 string = g_key_file_get_value (key_file, group, key, NULL);
1949 g_assert (string != NULL);
1951 value = g_variant_parse (state->type, string,
1952 NULL, NULL, &error);
1954 if (value == NULL)
1956 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1957 "as specified in override file '%s': "
1958 "%s."),
1959 key, group, filename, error->message);
1961 g_clear_error (&error);
1962 g_free (string);
1964 if (!strict)
1966 fprintf (stderr, _("Ignoring override for this key.\n"));
1967 continue;
1970 fprintf (stderr, _("--strict was specified; exiting.\n"));
1971 g_key_file_free (key_file);
1972 g_strfreev (groups);
1973 g_strfreev (keys);
1975 return FALSE;
1978 if (state->minimum)
1980 if (g_variant_compare (value, state->minimum) < 0 ||
1981 g_variant_compare (value, state->maximum) > 0)
1983 fprintf (stderr,
1984 _("override for key '%s' in schema '%s' in "
1985 "override file '%s' is outside the range "
1986 "given in the schema"),
1987 key, group, filename);
1989 g_variant_unref (value);
1990 g_free (string);
1992 if (!strict)
1994 fprintf (stderr, _("; ignoring override for this key.\n"));
1995 continue;
1998 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1999 g_key_file_free (key_file);
2000 g_strfreev (groups);
2001 g_strfreev (keys);
2003 return FALSE;
2007 else if (state->strinfo->len)
2009 if (!is_valid_choices (value, state->strinfo))
2011 fprintf (stderr,
2012 _("override for key '%s' in schema '%s' in "
2013 "override file '%s' is not in the list "
2014 "of valid choices"),
2015 key, group, filename);
2017 g_variant_unref (value);
2018 g_free (string);
2020 if (!strict)
2022 fprintf (stderr, _("; ignoring override for this key.\n"));
2023 continue;
2026 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2027 g_key_file_free (key_file);
2028 g_strfreev (groups);
2029 g_strfreev (keys);
2031 return FALSE;
2035 g_variant_unref (state->default_value);
2036 state->default_value = value;
2037 g_free (string);
2040 g_strfreev (keys);
2043 g_strfreev (groups);
2046 return TRUE;
2050 main (int argc, char **argv)
2052 GError *error = NULL;
2053 GHashTable *table = NULL;
2054 GDir *dir = NULL;
2055 const gchar *file;
2056 const gchar *srcdir;
2057 gboolean show_version_and_exit = FALSE;
2058 gchar *targetdir = NULL;
2059 gchar *target = NULL;
2060 gboolean dry_run = FALSE;
2061 gboolean strict = FALSE;
2062 gchar **schema_files = NULL;
2063 gchar **override_files = NULL;
2064 GOptionContext *context = NULL;
2065 gint retval;
2066 GOptionEntry entries[] = {
2067 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
2068 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2069 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2070 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2071 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2073 /* These options are only for use in the gschema-compile tests */
2074 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2075 { NULL }
2078 #ifdef G_OS_WIN32
2079 gchar *tmp = NULL;
2080 #endif
2082 setlocale (LC_ALL, "");
2083 textdomain (GETTEXT_PACKAGE);
2085 #ifdef G_OS_WIN32
2086 tmp = _glib_get_locale_dir ();
2087 bindtextdomain (GETTEXT_PACKAGE, tmp);
2088 #else
2089 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2090 #endif
2092 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2093 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2094 #endif
2096 context = g_option_context_new (N_("DIRECTORY"));
2097 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2098 g_option_context_set_summary (context,
2099 N_("Compile all GSettings schema files into a schema cache.\n"
2100 "Schema files are required to have the extension .gschema.xml,\n"
2101 "and the cache file is called gschemas.compiled."));
2102 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2104 if (!g_option_context_parse (context, &argc, &argv, &error))
2106 fprintf (stderr, "%s\n", error->message);
2107 retval = 1;
2108 goto done;
2111 if (show_version_and_exit)
2113 g_print (PACKAGE_VERSION "\n");
2114 retval = 0;
2115 goto done;
2118 if (!schema_files && argc != 2)
2120 fprintf (stderr, _("You should give exactly one directory name\n"));
2121 retval = 1;
2122 goto done;
2125 srcdir = argv[1];
2127 target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
2129 if (!schema_files)
2131 GPtrArray *overrides;
2132 GPtrArray *files;
2134 files = g_ptr_array_new ();
2135 overrides = g_ptr_array_new ();
2137 dir = g_dir_open (srcdir, 0, &error);
2138 if (dir == NULL)
2140 fprintf (stderr, "%s\n", error->message);
2142 g_ptr_array_unref (files);
2143 g_ptr_array_unref (overrides);
2145 retval = 1;
2146 goto done;
2149 while ((file = g_dir_read_name (dir)) != NULL)
2151 if (g_str_has_suffix (file, ".gschema.xml") ||
2152 g_str_has_suffix (file, ".enums.xml"))
2153 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2155 else if (g_str_has_suffix (file, ".gschema.override"))
2156 g_ptr_array_add (overrides,
2157 g_build_filename (srcdir, file, NULL));
2160 if (files->len == 0)
2162 fprintf (stdout, _("No schema files found: "));
2164 if (g_unlink (target))
2165 fprintf (stdout, _("doing nothing.\n"));
2167 else
2168 fprintf (stdout, _("removed existing output file.\n"));
2170 g_ptr_array_unref (files);
2171 g_ptr_array_unref (overrides);
2173 retval = 0;
2174 goto done;
2176 g_ptr_array_sort (files, compare_strings);
2177 g_ptr_array_add (files, NULL);
2179 g_ptr_array_sort (overrides, compare_strings);
2180 g_ptr_array_add (overrides, NULL);
2182 schema_files = (char **) g_ptr_array_free (files, FALSE);
2183 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2186 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2188 retval = 1;
2189 goto done;
2192 if (override_files != NULL &&
2193 !set_overrides (table, override_files, strict))
2195 retval = 1;
2196 goto done;
2199 if (!dry_run && !write_to_file (table, target, &error))
2201 fprintf (stderr, "%s\n", error->message);
2202 retval = 1;
2203 goto done;
2206 /* Success. */
2207 retval = 0;
2209 done:
2210 g_clear_error (&error);
2211 g_clear_pointer (&table, g_hash_table_unref);
2212 g_clear_pointer (&dir, g_dir_close);
2213 g_free (targetdir);
2214 g_free (target);
2215 g_strfreev (schema_files);
2216 g_strfreev (override_files);
2217 g_option_context_free (context);
2219 #ifdef G_OS_WIN32
2220 g_free (tmp);
2221 #endif
2223 return retval;
2226 /* Epilogue {{{1 */
2228 /* vim:set foldmethod=marker: */