Use macros for refcount types API
[glib.git] / gio / glib-compile-schemas.c
blob6794ca910778eb506924121f2af76e0a0d9889a9
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 g_free (state->child_schema);
750 if (state->type)
751 g_variant_type_free (state->type);
753 g_free (state->l10n_context);
755 if (state->unparsed_default_value)
756 g_string_free (state->unparsed_default_value, TRUE);
758 if (state->default_value)
759 g_variant_unref (state->default_value);
761 if (state->strinfo)
762 g_string_free (state->strinfo, TRUE);
764 if (state->minimum)
765 g_variant_unref (state->minimum);
767 if (state->maximum)
768 g_variant_unref (state->maximum);
770 if (state->serialised)
771 g_variant_unref (state->serialised);
773 g_slice_free (KeyState, state);
776 /* Key name validity {{{1 */
777 static gboolean allow_any_name = FALSE;
779 static gboolean
780 is_valid_keyname (const gchar *key,
781 GError **error)
783 gint i;
785 if (key[0] == '\0')
787 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
788 _("Empty names are not permitted"));
789 return FALSE;
792 if (allow_any_name)
793 return TRUE;
795 if (!g_ascii_islower (key[0]))
797 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
798 _("Invalid name “%s”: names must begin "
799 "with a lowercase letter"), key);
800 return FALSE;
803 for (i = 1; key[i]; i++)
805 if (key[i] != '-' &&
806 !g_ascii_islower (key[i]) &&
807 !g_ascii_isdigit (key[i]))
809 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
810 _("Invalid name “%s”: invalid character “%c”; "
811 "only lowercase letters, numbers and hyphen (“-”) "
812 "are permitted"), key, key[i]);
813 return FALSE;
816 if (key[i] == '-' && key[i + 1] == '-')
818 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
819 _("Invalid name “%s”: two successive hyphens (“--”) "
820 "are not permitted"), key);
821 return FALSE;
825 if (key[i - 1] == '-')
827 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
828 _("Invalid name “%s”: the last character may not be a "
829 "hyphen (“-”)"), key);
830 return FALSE;
833 if (i > 1024)
835 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
836 _("Invalid name “%s”: maximum length is 1024"), key);
837 return FALSE;
840 return TRUE;
843 /* Handling of <schema> {{{1 */
844 typedef struct _SchemaState SchemaState;
845 struct _SchemaState
847 SchemaState *extends;
849 gchar *path;
850 gchar *gettext_domain;
851 gchar *extends_name;
852 gchar *list_of;
854 GHashTable *keys;
857 static SchemaState *
858 schema_state_new (const gchar *path,
859 const gchar *gettext_domain,
860 SchemaState *extends,
861 const gchar *extends_name,
862 const gchar *list_of)
864 SchemaState *state;
866 state = g_slice_new (SchemaState);
867 state->path = g_strdup (path);
868 state->gettext_domain = g_strdup (gettext_domain);
869 state->extends = extends;
870 state->extends_name = g_strdup (extends_name);
871 state->list_of = g_strdup (list_of);
872 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
873 g_free, key_state_free);
875 return state;
878 static void
879 schema_state_free (gpointer data)
881 SchemaState *state = data;
883 g_free (state->path);
884 g_free (state->gettext_domain);
885 g_free (state->extends_name);
886 g_free (state->list_of);
887 g_hash_table_unref (state->keys);
888 g_slice_free (SchemaState, state);
891 static void
892 schema_state_add_child (SchemaState *state,
893 const gchar *name,
894 const gchar *schema,
895 GError **error)
897 gchar *childname;
899 if (!is_valid_keyname (name, error))
900 return;
902 childname = g_strconcat (name, "/", NULL);
904 if (g_hash_table_lookup (state->keys, childname))
906 g_set_error (error, G_MARKUP_ERROR,
907 G_MARKUP_ERROR_INVALID_CONTENT,
908 _("<child name='%s'> already specified"), name);
909 return;
912 g_hash_table_insert (state->keys, childname,
913 key_state_new_child (schema));
916 static KeyState *
917 schema_state_add_key (SchemaState *state,
918 GHashTable *enum_table,
919 GHashTable *flags_table,
920 const gchar *name,
921 const gchar *type_string,
922 const gchar *enum_type,
923 const gchar *flags_type,
924 GError **error)
926 SchemaState *node;
927 GString *strinfo;
928 KeyState *key;
930 if (state->list_of)
932 g_set_error_literal (error, G_MARKUP_ERROR,
933 G_MARKUP_ERROR_INVALID_CONTENT,
934 _("Cannot add keys to a “list-of” schema"));
935 return NULL;
938 if (!is_valid_keyname (name, error))
939 return NULL;
941 if (g_hash_table_lookup (state->keys, name))
943 g_set_error (error, G_MARKUP_ERROR,
944 G_MARKUP_ERROR_INVALID_CONTENT,
945 _("<key name='%s'> already specified"), name);
946 return NULL;
949 for (node = state; node; node = node->extends)
950 if (node->extends)
952 KeyState *shadow;
954 shadow = g_hash_table_lookup (node->extends->keys, name);
956 /* in case of <key> <override> <key> make sure we report the
957 * location of the original <key>, not the <override>.
959 if (shadow && !shadow->is_override)
961 g_set_error (error, G_MARKUP_ERROR,
962 G_MARKUP_ERROR_INVALID_CONTENT,
963 _("<key name='%s'> shadows <key name='%s'> in "
964 "<schema id='%s'>; use <override> to modify value"),
965 name, name, node->extends_name);
966 return NULL;
970 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
972 g_set_error (error, G_MARKUP_ERROR,
973 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
974 _("Exactly one of “type”, “enum” or “flags” must "
975 "be specified as an attribute to <key>"));
976 return NULL;
979 if (type_string == NULL) /* flags or enums was specified */
981 EnumState *enum_state;
983 if (enum_type)
984 enum_state = g_hash_table_lookup (enum_table, enum_type);
985 else
986 enum_state = g_hash_table_lookup (flags_table, flags_type);
989 if (enum_state == NULL)
991 g_set_error (error, G_MARKUP_ERROR,
992 G_MARKUP_ERROR_INVALID_CONTENT,
993 _("<%s id='%s'> not (yet) defined."),
994 flags_type ? "flags" : "enum",
995 flags_type ? flags_type : enum_type);
996 return NULL;
999 type_string = flags_type ? "as" : "s";
1000 strinfo = enum_state->strinfo;
1002 else
1004 if (!g_variant_type_string_is_valid (type_string))
1006 g_set_error (error, G_MARKUP_ERROR,
1007 G_MARKUP_ERROR_INVALID_CONTENT,
1008 _("Invalid GVariant type string “%s”"), type_string);
1009 return NULL;
1012 strinfo = NULL;
1015 key = key_state_new (type_string, state->gettext_domain,
1016 enum_type != NULL, flags_type != NULL, strinfo);
1017 g_hash_table_insert (state->keys, g_strdup (name), key);
1019 return key;
1022 static void
1023 schema_state_add_override (SchemaState *state,
1024 KeyState **key_state,
1025 GString **string,
1026 const gchar *key,
1027 const gchar *l10n,
1028 const gchar *context,
1029 GError **error)
1031 SchemaState *parent;
1032 KeyState *original;
1034 if (state->extends == NULL)
1036 g_set_error_literal (error, G_MARKUP_ERROR,
1037 G_MARKUP_ERROR_INVALID_CONTENT,
1038 _("<override> given but schema isn’t "
1039 "extending anything"));
1040 return;
1043 for (parent = state->extends; parent; parent = parent->extends)
1044 if ((original = g_hash_table_lookup (parent->keys, key)))
1045 break;
1047 if (original == NULL)
1049 g_set_error (error, G_MARKUP_ERROR,
1050 G_MARKUP_ERROR_INVALID_CONTENT,
1051 _("No <key name='%s'> to override"), key);
1052 return;
1055 if (g_hash_table_lookup (state->keys, key))
1057 g_set_error (error, G_MARKUP_ERROR,
1058 G_MARKUP_ERROR_INVALID_CONTENT,
1059 _("<override name='%s'> already specified"), key);
1060 return;
1063 *key_state = key_state_override (original, state->gettext_domain);
1064 *string = key_state_start_default (*key_state, l10n, context, error);
1065 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1068 static void
1069 override_state_end (KeyState **key_state,
1070 GString **string,
1071 GError **error)
1073 key_state_end_default (*key_state, string, error);
1074 *key_state = NULL;
1077 /* Handling of toplevel state {{{1 */
1078 typedef struct
1080 gboolean strict; /* TRUE if --strict was given */
1082 GHashTable *schema_table; /* string -> SchemaState */
1083 GHashTable *flags_table; /* string -> EnumState */
1084 GHashTable *enum_table; /* string -> EnumState */
1086 GSList *this_file_schemas; /* strings: <schema>s in this file */
1087 GSList *this_file_flagss; /* strings: <flags>s in this file */
1088 GSList *this_file_enums; /* strings: <enum>s in this file */
1090 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1092 SchemaState *schema_state; /* non-NULL when inside <schema> */
1093 KeyState *key_state; /* non-NULL when inside <key> */
1094 EnumState *enum_state; /* non-NULL when inside <enum> */
1096 GString *string; /* non-NULL when accepting text */
1097 } ParseState;
1099 static gboolean
1100 is_subclass (const gchar *class_name,
1101 const gchar *possible_parent,
1102 GHashTable *schema_table)
1104 SchemaState *class;
1106 if (strcmp (class_name, possible_parent) == 0)
1107 return TRUE;
1109 class = g_hash_table_lookup (schema_table, class_name);
1110 g_assert (class != NULL);
1112 return class->extends_name &&
1113 is_subclass (class->extends_name, possible_parent, schema_table);
1116 static void
1117 parse_state_start_schema (ParseState *state,
1118 const gchar *id,
1119 const gchar *path,
1120 const gchar *gettext_domain,
1121 const gchar *extends_name,
1122 const gchar *list_of,
1123 GError **error)
1125 SchemaState *extends;
1126 gchar *my_id;
1128 if (g_hash_table_lookup (state->schema_table, id))
1130 g_set_error (error, G_MARKUP_ERROR,
1131 G_MARKUP_ERROR_INVALID_CONTENT,
1132 _("<schema id='%s'> already specified"), id);
1133 return;
1136 if (extends_name)
1138 extends = g_hash_table_lookup (state->schema_table, extends_name);
1140 if (extends == NULL)
1142 g_set_error (error, G_MARKUP_ERROR,
1143 G_MARKUP_ERROR_INVALID_CONTENT,
1144 _("<schema id='%s'> extends not yet existing "
1145 "schema “%s”"), id, extends_name);
1146 return;
1149 else
1150 extends = NULL;
1152 if (list_of)
1154 SchemaState *tmp;
1156 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1158 g_set_error (error, G_MARKUP_ERROR,
1159 G_MARKUP_ERROR_INVALID_CONTENT,
1160 _("<schema id='%s'> is list of not yet existing "
1161 "schema “%s”"), id, list_of);
1162 return;
1165 if (tmp->path)
1167 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1168 _("Cannot be a list of a schema with a path"));
1169 return;
1173 if (extends)
1175 if (extends->path)
1177 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1178 _("Cannot extend a schema with a path"));
1179 return;
1182 if (list_of)
1184 if (extends->list_of == NULL)
1186 g_set_error (error, G_MARKUP_ERROR,
1187 G_MARKUP_ERROR_INVALID_CONTENT,
1188 _("<schema id='%s'> is a list, extending "
1189 "<schema id='%s'> which is not a list"),
1190 id, extends_name);
1191 return;
1194 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1196 g_set_error (error, G_MARKUP_ERROR,
1197 G_MARKUP_ERROR_INVALID_CONTENT,
1198 _("<schema id='%s' list-of='%s'> extends <schema "
1199 "id='%s' list-of='%s'> but “%s” does not "
1200 "extend “%s”"), id, list_of, extends_name,
1201 extends->list_of, list_of, extends->list_of);
1202 return;
1205 else
1206 /* by default we are a list of the same thing that the schema
1207 * we are extending is a list of (which might be nothing)
1209 list_of = extends->list_of;
1212 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1214 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1215 _("A path, if given, must begin and end with a slash"));
1216 return;
1219 if (path && list_of && !g_str_has_suffix (path, ":/"))
1221 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1222 _("The path of a list must end with “:/”"));
1223 return;
1226 if (path && (g_str_has_prefix (path, "/apps/") ||
1227 g_str_has_prefix (path, "/desktop/") ||
1228 g_str_has_prefix (path, "/system/")))
1230 gchar *message = NULL;
1231 message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1232 "Paths starting with "
1233 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1234 id, path);
1235 g_printerr ("%s\n", message);
1236 g_free (message);
1239 state->schema_state = schema_state_new (path, gettext_domain,
1240 extends, extends_name, list_of);
1242 my_id = g_strdup (id);
1243 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1244 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1247 static void
1248 parse_state_start_enum (ParseState *state,
1249 const gchar *id,
1250 gboolean is_flags,
1251 GError **error)
1253 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1254 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1255 gchar *my_id;
1257 if (g_hash_table_lookup (table, id))
1259 g_set_error (error, G_MARKUP_ERROR,
1260 G_MARKUP_ERROR_INVALID_CONTENT,
1261 _("<%s id='%s'> already specified"),
1262 is_flags ? "flags" : "enum", id);
1263 return;
1266 state->enum_state = enum_state_new (is_flags);
1268 my_id = g_strdup (id);
1269 *list = g_slist_prepend (*list, my_id);
1270 g_hash_table_insert (table, my_id, state->enum_state);
1273 /* GMarkup Parser Functions {{{1 */
1275 /* Start element {{{2 */
1276 static void
1277 start_element (GMarkupParseContext *context,
1278 const gchar *element_name,
1279 const gchar **attribute_names,
1280 const gchar **attribute_values,
1281 gpointer user_data,
1282 GError **error)
1284 ParseState *state = user_data;
1285 const GSList *element_stack;
1286 const gchar *container;
1288 element_stack = g_markup_parse_context_get_element_stack (context);
1289 container = element_stack->next ? element_stack->next->data : NULL;
1291 #define COLLECT(first, ...) \
1292 g_markup_collect_attributes (element_name, \
1293 attribute_names, attribute_values, error, \
1294 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1295 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1296 #define STRDUP G_MARKUP_COLLECT_STRDUP
1297 #define STRING G_MARKUP_COLLECT_STRING
1298 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1300 /* Toplevel items {{{3 */
1301 if (container == NULL)
1303 if (strcmp (element_name, "schemalist") == 0)
1305 COLLECT (OPTIONAL | STRDUP,
1306 "gettext-domain",
1307 &state->schemalist_domain);
1308 return;
1313 /* children of <schemalist> {{{3 */
1314 else if (strcmp (container, "schemalist") == 0)
1316 if (strcmp (element_name, "schema") == 0)
1318 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1319 if (COLLECT (STRING, "id", &id,
1320 OPTIONAL | STRING, "path", &path,
1321 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1322 OPTIONAL | STRING, "extends", &extends,
1323 OPTIONAL | STRING, "list-of", &list_of))
1324 parse_state_start_schema (state, id, path,
1325 gettext_domain ? gettext_domain
1326 : state->schemalist_domain,
1327 extends, list_of, error);
1328 return;
1331 else if (strcmp (element_name, "enum") == 0)
1333 const gchar *id;
1334 if (COLLECT (STRING, "id", &id))
1335 parse_state_start_enum (state, id, FALSE, error);
1336 return;
1339 else if (strcmp (element_name, "flags") == 0)
1341 const gchar *id;
1342 if (COLLECT (STRING, "id", &id))
1343 parse_state_start_enum (state, id, TRUE, error);
1344 return;
1349 /* children of <schema> {{{3 */
1350 else if (strcmp (container, "schema") == 0)
1352 if (strcmp (element_name, "key") == 0)
1354 const gchar *name, *type_string, *enum_type, *flags_type;
1356 if (COLLECT (STRING, "name", &name,
1357 OPTIONAL | STRING, "type", &type_string,
1358 OPTIONAL | STRING, "enum", &enum_type,
1359 OPTIONAL | STRING, "flags", &flags_type))
1361 state->key_state = schema_state_add_key (state->schema_state,
1362 state->enum_table,
1363 state->flags_table,
1364 name, type_string,
1365 enum_type, flags_type,
1366 error);
1367 return;
1369 else if (strcmp (element_name, "child") == 0)
1371 const gchar *name, *schema;
1373 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1374 schema_state_add_child (state->schema_state,
1375 name, schema, error);
1376 return;
1378 else if (strcmp (element_name, "override") == 0)
1380 const gchar *name, *l10n, *context;
1382 if (COLLECT (STRING, "name", &name,
1383 OPTIONAL | STRING, "l10n", &l10n,
1384 OPTIONAL | STRING, "context", &context))
1385 schema_state_add_override (state->schema_state,
1386 &state->key_state, &state->string,
1387 name, l10n, context, error);
1388 return;
1392 /* children of <key> {{{3 */
1393 else if (strcmp (container, "key") == 0)
1395 if (strcmp (element_name, "default") == 0)
1397 const gchar *l10n, *context;
1398 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1399 STRING | OPTIONAL, "context", &context))
1400 state->string = key_state_start_default (state->key_state,
1401 l10n, context, error);
1402 return;
1405 else if (strcmp (element_name, "summary") == 0)
1407 if (NO_ATTRS ())
1409 if (state->key_state->summary_seen && state->strict)
1410 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1411 _("Only one <%s> element allowed inside <%s>"),
1412 element_name, container);
1413 else
1414 state->string = g_string_new (NULL);
1416 state->key_state->summary_seen = TRUE;
1418 return;
1421 else if (strcmp (element_name, "description") == 0)
1423 if (NO_ATTRS ())
1425 if (state->key_state->description_seen && state->strict)
1426 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1427 _("Only one <%s> element allowed inside <%s>"),
1428 element_name, container);
1429 else
1430 state->string = g_string_new (NULL);
1432 state->key_state->description_seen = TRUE;
1434 return;
1437 else if (strcmp (element_name, "range") == 0)
1439 const gchar *min, *max;
1440 if (COLLECT (STRING | OPTIONAL, "min", &min,
1441 STRING | OPTIONAL, "max", &max))
1442 key_state_set_range (state->key_state, min, max, error);
1443 return;
1446 else if (strcmp (element_name, "choices") == 0)
1448 if (NO_ATTRS ())
1449 key_state_start_choices (state->key_state, error);
1450 return;
1453 else if (strcmp (element_name, "aliases") == 0)
1455 if (NO_ATTRS ())
1456 key_state_start_aliases (state->key_state, error);
1457 return;
1462 /* children of <choices> {{{3 */
1463 else if (strcmp (container, "choices") == 0)
1465 if (strcmp (element_name, "choice") == 0)
1467 const gchar *value;
1468 if (COLLECT (STRING, "value", &value))
1469 key_state_add_choice (state->key_state, value, error);
1470 return;
1475 /* children of <aliases> {{{3 */
1476 else if (strcmp (container, "aliases") == 0)
1478 if (strcmp (element_name, "alias") == 0)
1480 const gchar *value, *target;
1481 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1482 key_state_add_alias (state->key_state, value, target, error);
1483 return;
1488 /* children of <enum> {{{3 */
1489 else if (strcmp (container, "enum") == 0 ||
1490 strcmp (container, "flags") == 0)
1492 if (strcmp (element_name, "value") == 0)
1494 const gchar *nick, *valuestr;
1495 if (COLLECT (STRING, "nick", &nick,
1496 STRING, "value", &valuestr))
1497 enum_state_add_value (state->enum_state, nick, valuestr, error);
1498 return;
1501 /* 3}}} */
1503 if (container)
1504 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1505 _("Element <%s> not allowed inside <%s>"),
1506 element_name, container);
1507 else
1508 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1509 _("Element <%s> not allowed at the top level"), element_name);
1511 /* 2}}} */
1512 /* End element {{{2 */
1514 static void
1515 key_state_end (KeyState **state_ptr,
1516 GError **error)
1518 KeyState *state;
1520 state = *state_ptr;
1521 *state_ptr = NULL;
1523 if (state->default_value == NULL)
1525 g_set_error_literal (error,
1526 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1527 _("Element <default> is required in <key>"));
1528 return;
1532 static void
1533 schema_state_end (SchemaState **state_ptr,
1534 GError **error)
1536 *state_ptr = NULL;
1539 static void
1540 end_element (GMarkupParseContext *context,
1541 const gchar *element_name,
1542 gpointer user_data,
1543 GError **error)
1545 ParseState *state = user_data;
1547 if (strcmp (element_name, "schemalist") == 0)
1549 g_free (state->schemalist_domain);
1550 state->schemalist_domain = NULL;
1553 else if (strcmp (element_name, "enum") == 0 ||
1554 strcmp (element_name, "flags") == 0)
1555 enum_state_end (&state->enum_state, error);
1557 else if (strcmp (element_name, "schema") == 0)
1558 schema_state_end (&state->schema_state, error);
1560 else if (strcmp (element_name, "override") == 0)
1561 override_state_end (&state->key_state, &state->string, error);
1563 else if (strcmp (element_name, "key") == 0)
1564 key_state_end (&state->key_state, error);
1566 else if (strcmp (element_name, "default") == 0)
1567 key_state_end_default (state->key_state, &state->string, error);
1569 else if (strcmp (element_name, "choices") == 0)
1570 key_state_end_choices (state->key_state, error);
1572 else if (strcmp (element_name, "aliases") == 0)
1573 key_state_end_aliases (state->key_state, error);
1575 if (state->string)
1577 g_string_free (state->string, TRUE);
1578 state->string = NULL;
1581 /* Text {{{2 */
1582 static void
1583 text (GMarkupParseContext *context,
1584 const gchar *text,
1585 gsize text_len,
1586 gpointer user_data,
1587 GError **error)
1589 ParseState *state = user_data;
1591 if (state->string)
1593 /* we are expecting a string, so store the text data.
1595 * we store the data verbatim here and deal with whitespace
1596 * later on. there are two reasons for that:
1598 * 1) whitespace is handled differently depending on the tag
1599 * type.
1601 * 2) we could do leading whitespace removal by refusing to
1602 * insert it into state->string if it's at the start, but for
1603 * trailing whitespace, we have no idea if there is another
1604 * text() call coming or not.
1606 g_string_append_len (state->string, text, text_len);
1608 else
1610 /* string is not expected: accept (and ignore) pure whitespace */
1611 gsize i;
1613 for (i = 0; i < text_len; i++)
1614 if (!g_ascii_isspace (text[i]))
1616 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1617 _("Text may not appear inside <%s>"),
1618 g_markup_parse_context_get_element (context));
1619 break;
1624 /* Write to GVDB {{{1 */
1625 typedef struct
1627 GHashTable *table;
1628 GvdbItem *root;
1629 } GvdbPair;
1631 static void
1632 gvdb_pair_init (GvdbPair *pair)
1634 pair->table = gvdb_hash_table_new (NULL, NULL);
1635 pair->root = gvdb_hash_table_insert (pair->table, "");
1638 static void
1639 gvdb_pair_clear (GvdbPair *pair)
1641 g_hash_table_unref (pair->table);
1644 typedef struct
1646 GHashTable *schema_table;
1647 GvdbPair root_pair;
1648 } WriteToFileData;
1650 typedef struct
1652 GHashTable *schema_table;
1653 GvdbPair pair;
1654 gboolean l10n;
1655 } OutputSchemaData;
1657 static void
1658 output_key (gpointer key,
1659 gpointer value,
1660 gpointer user_data)
1662 OutputSchemaData *data;
1663 const gchar *name;
1664 KeyState *state;
1665 GvdbItem *item;
1666 GVariant *serialised = NULL;
1668 name = key;
1669 state = value;
1670 data = user_data;
1672 item = gvdb_hash_table_insert (data->pair.table, name);
1673 gvdb_item_set_parent (item, data->pair.root);
1674 serialised = key_state_serialise (state);
1675 gvdb_item_set_value (item, serialised);
1676 g_variant_unref (serialised);
1678 if (state->l10n)
1679 data->l10n = TRUE;
1681 if (state->child_schema &&
1682 !g_hash_table_lookup (data->schema_table, state->child_schema))
1684 gchar *message = NULL;
1685 message = g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1686 state->child_schema);
1687 g_printerr ("%s\n", message);
1688 g_free (message);
1692 static void
1693 output_schema (gpointer key,
1694 gpointer value,
1695 gpointer user_data)
1697 WriteToFileData *wtf_data = user_data;
1698 OutputSchemaData data;
1699 GvdbPair *root_pair;
1700 SchemaState *state;
1701 const gchar *id;
1702 GvdbItem *item;
1704 id = key;
1705 state = value;
1706 root_pair = &wtf_data->root_pair;
1708 data.schema_table = wtf_data->schema_table;
1709 gvdb_pair_init (&data.pair);
1710 data.l10n = FALSE;
1712 item = gvdb_hash_table_insert (root_pair->table, id);
1713 gvdb_item_set_parent (item, root_pair->root);
1714 gvdb_item_set_hash_table (item, data.pair.table);
1716 g_hash_table_foreach (state->keys, output_key, &data);
1718 if (state->path)
1719 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1721 if (state->extends_name)
1722 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1723 state->extends_name);
1725 if (state->list_of)
1726 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1727 state->list_of);
1729 if (data.l10n)
1730 gvdb_hash_table_insert_string (data.pair.table,
1731 ".gettext-domain",
1732 state->gettext_domain);
1734 gvdb_pair_clear (&data.pair);
1737 static gboolean
1738 write_to_file (GHashTable *schema_table,
1739 const gchar *filename,
1740 GError **error)
1742 WriteToFileData data;
1743 gboolean success;
1745 data.schema_table = schema_table;
1747 gvdb_pair_init (&data.root_pair);
1749 g_hash_table_foreach (schema_table, output_schema, &data);
1751 success = gvdb_table_write_contents (data.root_pair.table, filename,
1752 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1753 error);
1754 g_hash_table_unref (data.root_pair.table);
1756 return success;
1759 /* Parser driver {{{1 */
1760 static GHashTable *
1761 parse_gschema_files (gchar **files,
1762 gboolean strict)
1764 GMarkupParser parser = { start_element, end_element, text };
1765 ParseState state = { 0, };
1766 const gchar *filename;
1767 GError *error = NULL;
1769 state.strict = strict;
1771 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1772 g_free, enum_state_free);
1774 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1775 g_free, enum_state_free);
1777 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1778 g_free, schema_state_free);
1780 while ((filename = *files++) != NULL)
1782 GMarkupParseContext *context;
1783 gchar *contents;
1784 gsize size;
1785 gint line, col;
1787 if (!g_file_get_contents (filename, &contents, &size, &error))
1789 fprintf (stderr, "%s\n", error->message);
1790 g_clear_error (&error);
1791 continue;
1794 context = g_markup_parse_context_new (&parser,
1795 G_MARKUP_TREAT_CDATA_AS_TEXT |
1796 G_MARKUP_PREFIX_ERROR_POSITION |
1797 G_MARKUP_IGNORE_QUALIFIED,
1798 &state, NULL);
1801 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1802 !g_markup_parse_context_end_parse (context, &error))
1804 GSList *item;
1806 /* back out any changes from this file */
1807 for (item = state.this_file_schemas; item; item = item->next)
1808 g_hash_table_remove (state.schema_table, item->data);
1810 for (item = state.this_file_flagss; item; item = item->next)
1811 g_hash_table_remove (state.flags_table, item->data);
1813 for (item = state.this_file_enums; item; item = item->next)
1814 g_hash_table_remove (state.enum_table, item->data);
1816 /* let them know */
1817 g_markup_parse_context_get_position (context, &line, &col);
1818 fprintf (stderr, "%s:%d:%d %s. ", filename, line, col, error->message);
1819 g_clear_error (&error);
1821 if (strict)
1823 /* Translators: Do not translate "--strict". */
1824 fprintf (stderr, _("--strict was specified; exiting.\n"));
1825 g_hash_table_unref (state.schema_table);
1826 g_hash_table_unref (state.flags_table);
1827 g_hash_table_unref (state.enum_table);
1829 g_free (contents);
1831 return NULL;
1833 else
1834 fprintf (stderr, _("This entire file has been ignored.\n"));
1837 /* cleanup */
1838 g_free (contents);
1839 g_markup_parse_context_free (context);
1840 g_slist_free (state.this_file_schemas);
1841 g_slist_free (state.this_file_flagss);
1842 g_slist_free (state.this_file_enums);
1843 state.this_file_schemas = NULL;
1844 state.this_file_flagss = NULL;
1845 state.this_file_enums = NULL;
1848 g_hash_table_unref (state.flags_table);
1849 g_hash_table_unref (state.enum_table);
1851 return state.schema_table;
1854 static gint
1855 compare_strings (gconstpointer a,
1856 gconstpointer b)
1858 gchar *one = *(gchar **) a;
1859 gchar *two = *(gchar **) b;
1860 gint cmp;
1862 cmp = g_str_has_suffix (two, ".enums.xml") -
1863 g_str_has_suffix (one, ".enums.xml");
1865 if (!cmp)
1866 cmp = strcmp (one, two);
1868 return cmp;
1871 static gboolean
1872 set_overrides (GHashTable *schema_table,
1873 gchar **files,
1874 gboolean strict)
1876 const gchar *filename;
1877 GError *error = NULL;
1879 while ((filename = *files++))
1881 GKeyFile *key_file;
1882 gchar **groups;
1883 gint i;
1885 key_file = g_key_file_new ();
1886 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1888 fprintf (stderr, "%s: %s. ", filename, error->message);
1889 g_key_file_free (key_file);
1890 g_clear_error (&error);
1892 if (!strict)
1894 fprintf (stderr, _("Ignoring this file.\n"));
1895 continue;
1898 fprintf (stderr, _("--strict was specified; exiting.\n"));
1899 return FALSE;
1902 groups = g_key_file_get_groups (key_file, NULL);
1904 for (i = 0; groups[i]; i++)
1906 const gchar *group = groups[i];
1907 SchemaState *schema;
1908 gchar **keys;
1909 gint j;
1911 schema = g_hash_table_lookup (schema_table, group);
1913 if (schema == NULL)
1914 /* Having the schema not be installed is expected to be a
1915 * common case. Don't even emit an error message about
1916 * that.
1918 continue;
1920 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1921 g_assert (keys != NULL);
1923 for (j = 0; keys[j]; j++)
1925 const gchar *key = keys[j];
1926 KeyState *state;
1927 GVariant *value;
1928 gchar *string;
1930 state = g_hash_table_lookup (schema->keys, key);
1932 if (state == NULL)
1934 fprintf (stderr, _("No such key “%s” in schema “%s” as "
1935 "specified in override file “%s”"),
1936 key, group, filename);
1938 if (!strict)
1940 fprintf (stderr, _("; ignoring override for this key.\n"));
1941 continue;
1944 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1945 g_key_file_free (key_file);
1946 g_strfreev (groups);
1947 g_strfreev (keys);
1949 return FALSE;
1952 string = g_key_file_get_value (key_file, group, key, NULL);
1953 g_assert (string != NULL);
1955 value = g_variant_parse (state->type, string,
1956 NULL, NULL, &error);
1958 if (value == NULL)
1960 fprintf (stderr, _("error parsing key “%s” in schema “%s” "
1961 "as specified in override file “%s”: "
1962 "%s."),
1963 key, group, filename, error->message);
1965 g_clear_error (&error);
1966 g_free (string);
1968 if (!strict)
1970 fprintf (stderr, _("Ignoring override for this key.\n"));
1971 continue;
1974 fprintf (stderr, _("--strict was specified; exiting.\n"));
1975 g_key_file_free (key_file);
1976 g_strfreev (groups);
1977 g_strfreev (keys);
1979 return FALSE;
1982 if (state->minimum)
1984 if (g_variant_compare (value, state->minimum) < 0 ||
1985 g_variant_compare (value, state->maximum) > 0)
1987 fprintf (stderr,
1988 _("override for key “%s” in schema “%s” in "
1989 "override file “%s” is outside the range "
1990 "given in the schema"),
1991 key, group, filename);
1993 g_variant_unref (value);
1994 g_free (string);
1996 if (!strict)
1998 fprintf (stderr, _("; ignoring override for this key.\n"));
1999 continue;
2002 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2003 g_key_file_free (key_file);
2004 g_strfreev (groups);
2005 g_strfreev (keys);
2007 return FALSE;
2011 else if (state->strinfo->len)
2013 if (!is_valid_choices (value, state->strinfo))
2015 fprintf (stderr,
2016 _("override for key “%s” in schema “%s” in "
2017 "override file “%s” is not in the list "
2018 "of valid choices"),
2019 key, group, filename);
2021 g_variant_unref (value);
2022 g_free (string);
2024 if (!strict)
2026 fprintf (stderr, _("; ignoring override for this key.\n"));
2027 continue;
2030 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2031 g_key_file_free (key_file);
2032 g_strfreev (groups);
2033 g_strfreev (keys);
2035 return FALSE;
2039 g_variant_unref (state->default_value);
2040 state->default_value = value;
2041 g_free (string);
2044 g_strfreev (keys);
2047 g_strfreev (groups);
2050 return TRUE;
2054 main (int argc, char **argv)
2056 GError *error = NULL;
2057 GHashTable *table = NULL;
2058 GDir *dir = NULL;
2059 const gchar *file;
2060 const gchar *srcdir;
2061 gboolean show_version_and_exit = FALSE;
2062 gchar *targetdir = NULL;
2063 gchar *target = NULL;
2064 gboolean dry_run = FALSE;
2065 gboolean strict = FALSE;
2066 gchar **schema_files = NULL;
2067 gchar **override_files = NULL;
2068 GOptionContext *context = NULL;
2069 gint retval;
2070 GOptionEntry entries[] = {
2071 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
2072 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2073 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2074 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2075 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2077 /* These options are only for use in the gschema-compile tests */
2078 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2079 { NULL }
2082 #ifdef G_OS_WIN32
2083 gchar *tmp = NULL;
2084 #endif
2086 setlocale (LC_ALL, "");
2087 textdomain (GETTEXT_PACKAGE);
2089 #ifdef G_OS_WIN32
2090 tmp = _glib_get_locale_dir ();
2091 bindtextdomain (GETTEXT_PACKAGE, tmp);
2092 #else
2093 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2094 #endif
2096 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2097 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2098 #endif
2100 context = g_option_context_new (N_("DIRECTORY"));
2101 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2102 g_option_context_set_summary (context,
2103 N_("Compile all GSettings schema files into a schema cache.\n"
2104 "Schema files are required to have the extension .gschema.xml,\n"
2105 "and the cache file is called gschemas.compiled."));
2106 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2108 if (!g_option_context_parse (context, &argc, &argv, &error))
2110 fprintf (stderr, "%s\n", error->message);
2111 retval = 1;
2112 goto done;
2115 if (show_version_and_exit)
2117 g_print (PACKAGE_VERSION "\n");
2118 retval = 0;
2119 goto done;
2122 if (!schema_files && argc != 2)
2124 fprintf (stderr, _("You should give exactly one directory name\n"));
2125 retval = 1;
2126 goto done;
2129 srcdir = argv[1];
2131 target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
2133 if (!schema_files)
2135 GPtrArray *overrides;
2136 GPtrArray *files;
2138 files = g_ptr_array_new ();
2139 overrides = g_ptr_array_new ();
2141 dir = g_dir_open (srcdir, 0, &error);
2142 if (dir == NULL)
2144 fprintf (stderr, "%s\n", error->message);
2146 g_ptr_array_unref (files);
2147 g_ptr_array_unref (overrides);
2149 retval = 1;
2150 goto done;
2153 while ((file = g_dir_read_name (dir)) != NULL)
2155 if (g_str_has_suffix (file, ".gschema.xml") ||
2156 g_str_has_suffix (file, ".enums.xml"))
2157 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2159 else if (g_str_has_suffix (file, ".gschema.override"))
2160 g_ptr_array_add (overrides,
2161 g_build_filename (srcdir, file, NULL));
2164 if (files->len == 0)
2166 fprintf (stdout, _("No schema files found: "));
2168 if (g_unlink (target))
2169 fprintf (stdout, _("doing nothing.\n"));
2171 else
2172 fprintf (stdout, _("removed existing output file.\n"));
2174 g_ptr_array_unref (files);
2175 g_ptr_array_unref (overrides);
2177 retval = 0;
2178 goto done;
2180 g_ptr_array_sort (files, compare_strings);
2181 g_ptr_array_add (files, NULL);
2183 g_ptr_array_sort (overrides, compare_strings);
2184 g_ptr_array_add (overrides, NULL);
2186 schema_files = (char **) g_ptr_array_free (files, FALSE);
2187 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2190 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2192 retval = 1;
2193 goto done;
2196 if (override_files != NULL &&
2197 !set_overrides (table, override_files, strict))
2199 retval = 1;
2200 goto done;
2203 if (!dry_run && !write_to_file (table, target, &error))
2205 fprintf (stderr, "%s\n", error->message);
2206 retval = 1;
2207 goto done;
2210 /* Success. */
2211 retval = 0;
2213 done:
2214 g_clear_error (&error);
2215 g_clear_pointer (&table, g_hash_table_unref);
2216 g_clear_pointer (&dir, g_dir_close);
2217 g_free (targetdir);
2218 g_free (target);
2219 g_strfreev (schema_files);
2220 g_strfreev (override_files);
2221 g_option_context_free (context);
2223 #ifdef G_OS_WIN32
2224 g_free (tmp);
2225 #endif
2227 return retval;
2230 /* Epilogue {{{1 */
2232 /* vim:set foldmethod=marker: */