gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / glib-compile-schemas.c
blobe190ff5afcd3c9a1991d6aebd5f1641bd187ab6d
1 /*
2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 /* Prologue {{{1 */
21 #include "config.h"
23 #include <gstdio.h>
24 #include <gi18n.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <locale.h>
30 #include "gvdb/gvdb-builder.h"
31 #include "strinfo.c"
33 #ifdef G_OS_WIN32
34 #include "glib/glib-private.h"
35 #endif
37 static void
38 strip_string (GString *string)
40 gint i;
42 for (i = 0; g_ascii_isspace (string->str[i]); i++);
43 g_string_erase (string, 0, i);
45 if (string->len > 0)
47 /* len > 0, so there must be at least one non-whitespace character */
48 for (i = string->len - 1; g_ascii_isspace (string->str[i]); i--);
49 g_string_truncate (string, i + 1);
53 /* Handling of <enum> {{{1 */
54 typedef struct
56 GString *strinfo;
58 gboolean is_flags;
59 } EnumState;
61 static void
62 enum_state_free (gpointer data)
64 EnumState *state = data;
66 g_string_free (state->strinfo, TRUE);
67 g_slice_free (EnumState, state);
70 static EnumState *
71 enum_state_new (gboolean is_flags)
73 EnumState *state;
75 state = g_slice_new (EnumState);
76 state->strinfo = g_string_new (NULL);
77 state->is_flags = is_flags;
79 return state;
82 static void
83 enum_state_add_value (EnumState *state,
84 const gchar *nick,
85 const gchar *valuestr,
86 GError **error)
88 gint64 value;
89 gchar *end;
91 if (nick[0] == '\0' || nick[1] == '\0')
93 g_set_error (error, G_MARKUP_ERROR,
94 G_MARKUP_ERROR_INVALID_CONTENT,
95 "nick must be a minimum of 2 characters");
96 return;
99 value = g_ascii_strtoll (valuestr, &end, 0);
100 if (*end || state->is_flags ?
101 (value > G_MAXUINT32 || value < 0) :
102 (value > G_MAXINT32 || value < G_MININT32))
104 g_set_error (error, G_MARKUP_ERROR,
105 G_MARKUP_ERROR_INVALID_CONTENT,
106 "invalid numeric value");
107 return;
110 if (strinfo_builder_contains (state->strinfo, nick))
112 g_set_error (error, G_MARKUP_ERROR,
113 G_MARKUP_ERROR_INVALID_CONTENT,
114 "<value nick='%s'/> already specified", nick);
115 return;
118 if (strinfo_builder_contains_value (state->strinfo, value))
120 g_set_error (error, G_MARKUP_ERROR,
121 G_MARKUP_ERROR_INVALID_CONTENT,
122 "value='%s' already specified", valuestr);
123 return;
126 /* Silently drop the null case if it is mentioned.
127 * It is properly denoted with an empty array.
129 if (state->is_flags && value == 0)
130 return;
132 if (state->is_flags && (value & (value - 1)))
134 g_set_error (error, G_MARKUP_ERROR,
135 G_MARKUP_ERROR_INVALID_CONTENT,
136 "flags values must have at most 1 bit set");
137 return;
140 /* Since we reject exact duplicates of value='' and we only allow one
141 * bit to be set, it's not possible to have overlaps.
143 * If we loosen the one-bit-set restriction we need an overlap check.
146 strinfo_builder_append_item (state->strinfo, nick, value);
149 static void
150 enum_state_end (EnumState **state_ptr,
151 GError **error)
153 EnumState *state;
155 state = *state_ptr;
156 *state_ptr = NULL;
158 if (state->strinfo->len == 0)
159 g_set_error (error,
160 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
161 "<%s> must contain at least one <value>",
162 state->is_flags ? "flags" : "enum");
165 /* Handling of <key> {{{1 */
166 typedef struct
168 /* for <child>, @child_schema will be set.
169 * for <key>, everything else will be set.
171 gchar *child_schema;
174 GVariantType *type;
175 gboolean have_gettext_domain;
177 gchar l10n;
178 gchar *l10n_context;
179 GString *unparsed_default_value;
180 GVariant *default_value;
182 GString *strinfo;
183 gboolean is_enum;
184 gboolean is_flags;
186 GVariant *minimum;
187 GVariant *maximum;
189 gboolean has_choices;
190 gboolean has_aliases;
191 gboolean is_override;
193 gboolean checked;
194 GVariant *serialised;
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 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 maxmimum");
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 "alias target '%s' is not in %s", target,
605 state->is_enum ? "enumerated type" : "<choices>");
606 return;
609 state->has_aliases = TRUE;
612 static void
613 key_state_end_aliases (KeyState *state,
614 GError **error)
616 if (!state->has_aliases)
618 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
619 "<aliases> must contain at least one <alias>");
620 return;
624 static gboolean
625 key_state_check (KeyState *state,
626 GError **error)
628 if (state->checked)
629 return TRUE;
631 return state->checked = TRUE;
634 static GVariant *
635 key_state_serialise (KeyState *state)
637 if (state->serialised == NULL)
639 if (state->child_schema)
641 state->serialised = g_variant_new_string (state->child_schema);
644 else
646 GVariantBuilder builder;
648 g_assert (key_state_check (state, NULL));
650 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
652 /* default value */
653 g_variant_builder_add_value (&builder, state->default_value);
655 /* translation */
656 if (state->l10n)
658 /* We are going to store the untranslated default for
659 * runtime translation according to the current locale.
660 * We need to strip leading and trailing whitespace from
661 * the string so that it's exactly the same as the one
662 * that ended up in the .po file for translation.
664 * We want to do this so that
666 * <default l10n='messages'>
667 * ['a', 'b', 'c']
668 * </default>
670 * ends up in the .po file like "['a', 'b', 'c']",
671 * omitting the extra whitespace at the start and end.
673 strip_string (state->unparsed_default_value);
675 if (state->l10n_context)
677 gint len;
679 /* Contextified messages are supported by prepending
680 * the context, followed by '\004' to the start of the
681 * message string. We do that here to save GSettings
682 * the work later on.
684 len = strlen (state->l10n_context);
685 state->l10n_context[len] = '\004';
686 g_string_prepend_len (state->unparsed_default_value,
687 state->l10n_context, len + 1);
688 g_free (state->l10n_context);
689 state->l10n_context = NULL;
692 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
693 state->unparsed_default_value->str);
694 g_string_free (state->unparsed_default_value, TRUE);
695 state->unparsed_default_value = NULL;
698 /* choice, aliases, enums */
699 if (state->strinfo->len)
701 GVariant *array;
702 guint32 *words;
703 gpointer data;
704 gsize size;
705 gint i;
707 data = state->strinfo->str;
708 size = state->strinfo->len;
710 words = data;
711 for (i = 0; i < size / sizeof (guint32); i++)
712 words[i] = GUINT32_TO_LE (words[i]);
714 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
715 data, size, TRUE,
716 g_free, data);
718 g_string_free (state->strinfo, FALSE);
719 state->strinfo = NULL;
721 g_variant_builder_add (&builder, "(y@au)",
722 state->is_flags ? 'f' :
723 state->is_enum ? 'e' : 'c',
724 array);
727 /* range */
728 if (state->minimum || state->maximum)
729 g_variant_builder_add (&builder, "(y(**))", 'r',
730 state->minimum, state->maximum);
732 state->serialised = g_variant_builder_end (&builder);
735 g_variant_ref_sink (state->serialised);
738 return g_variant_ref (state->serialised);
741 static void
742 key_state_free (gpointer data)
744 KeyState *state = data;
746 if (state->type)
747 g_variant_type_free (state->type);
749 g_free (state->l10n_context);
751 if (state->unparsed_default_value)
752 g_string_free (state->unparsed_default_value, TRUE);
754 if (state->default_value)
755 g_variant_unref (state->default_value);
757 if (state->strinfo)
758 g_string_free (state->strinfo, TRUE);
760 if (state->minimum)
761 g_variant_unref (state->minimum);
763 if (state->maximum)
764 g_variant_unref (state->maximum);
766 if (state->serialised)
767 g_variant_unref (state->serialised);
769 g_slice_free (KeyState, state);
772 /* Key name validity {{{1 */
773 static gboolean allow_any_name = FALSE;
775 static gboolean
776 is_valid_keyname (const gchar *key,
777 GError **error)
779 gint i;
781 if (key[0] == '\0')
783 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
784 _("empty names are not permitted"));
785 return FALSE;
788 if (allow_any_name)
789 return TRUE;
791 if (!g_ascii_islower (key[0]))
793 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
794 _("invalid name '%s': names must begin "
795 "with a lowercase letter"), key);
796 return FALSE;
799 for (i = 1; key[i]; i++)
801 if (key[i] != '-' &&
802 !g_ascii_islower (key[i]) &&
803 !g_ascii_isdigit (key[i]))
805 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
806 _("invalid name '%s': invalid character '%c'; "
807 "only lowercase letters, numbers and hyphen ('-') "
808 "are permitted."), key, key[i]);
809 return FALSE;
812 if (key[i] == '-' && key[i + 1] == '-')
814 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
815 _("invalid name '%s': two successive hyphens ('--') "
816 "are not permitted."), key);
817 return FALSE;
821 if (key[i - 1] == '-')
823 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
824 _("invalid name '%s': the last character may not be a "
825 "hyphen ('-')."), key);
826 return FALSE;
829 if (i > 1024)
831 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
832 _("invalid name '%s': maximum length is 1024"), key);
833 return FALSE;
836 return TRUE;
839 /* Handling of <schema> {{{1 */
840 typedef struct _SchemaState SchemaState;
841 struct _SchemaState
843 SchemaState *extends;
845 gchar *path;
846 gchar *gettext_domain;
847 gchar *extends_name;
848 gchar *list_of;
850 GHashTable *keys;
853 static SchemaState *
854 schema_state_new (const gchar *path,
855 const gchar *gettext_domain,
856 SchemaState *extends,
857 const gchar *extends_name,
858 const gchar *list_of)
860 SchemaState *state;
862 state = g_slice_new (SchemaState);
863 state->path = g_strdup (path);
864 state->gettext_domain = g_strdup (gettext_domain);
865 state->extends = extends;
866 state->extends_name = g_strdup (extends_name);
867 state->list_of = g_strdup (list_of);
868 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
869 g_free, key_state_free);
871 return state;
874 static void
875 schema_state_free (gpointer data)
877 SchemaState *state = data;
879 g_free (state->path);
880 g_free (state->gettext_domain);
881 g_hash_table_unref (state->keys);
882 g_slice_free (SchemaState, state);
885 static void
886 schema_state_add_child (SchemaState *state,
887 const gchar *name,
888 const gchar *schema,
889 GError **error)
891 gchar *childname;
893 if (!is_valid_keyname (name, error))
894 return;
896 childname = g_strconcat (name, "/", NULL);
898 if (g_hash_table_lookup (state->keys, childname))
900 g_set_error (error, G_MARKUP_ERROR,
901 G_MARKUP_ERROR_INVALID_CONTENT,
902 _("<child name='%s'> already specified"), name);
903 return;
906 g_hash_table_insert (state->keys, childname,
907 key_state_new_child (schema));
910 static KeyState *
911 schema_state_add_key (SchemaState *state,
912 GHashTable *enum_table,
913 GHashTable *flags_table,
914 const gchar *name,
915 const gchar *type_string,
916 const gchar *enum_type,
917 const gchar *flags_type,
918 GError **error)
920 SchemaState *node;
921 GString *strinfo;
922 KeyState *key;
924 if (state->list_of)
926 g_set_error_literal (error, G_MARKUP_ERROR,
927 G_MARKUP_ERROR_INVALID_CONTENT,
928 _("cannot add keys to a 'list-of' schema"));
929 return NULL;
932 if (!is_valid_keyname (name, error))
933 return NULL;
935 if (g_hash_table_lookup (state->keys, name))
937 g_set_error (error, G_MARKUP_ERROR,
938 G_MARKUP_ERROR_INVALID_CONTENT,
939 _("<key name='%s'> already specified"), name);
940 return NULL;
943 for (node = state; node; node = node->extends)
944 if (node->extends)
946 KeyState *shadow;
948 shadow = g_hash_table_lookup (node->extends->keys, name);
950 /* in case of <key> <override> <key> make sure we report the
951 * location of the original <key>, not the <override>.
953 if (shadow && !shadow->is_override)
955 g_set_error (error, G_MARKUP_ERROR,
956 G_MARKUP_ERROR_INVALID_CONTENT,
957 _("<key name='%s'> shadows <key name='%s'> in "
958 "<schema id='%s'>; use <override> to modify value"),
959 name, name, node->extends_name);
960 return NULL;
964 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
966 g_set_error (error, G_MARKUP_ERROR,
967 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
968 _("exactly one of 'type', 'enum' or 'flags' must "
969 "be specified as an attribute to <key>"));
970 return NULL;
973 if (type_string == NULL) /* flags or enums was specified */
975 EnumState *enum_state;
977 if (enum_type)
978 enum_state = g_hash_table_lookup (enum_table, enum_type);
979 else
980 enum_state = g_hash_table_lookup (flags_table, flags_type);
983 if (enum_state == NULL)
985 g_set_error (error, G_MARKUP_ERROR,
986 G_MARKUP_ERROR_INVALID_CONTENT,
987 _("<%s id='%s'> not (yet) defined."),
988 flags_type ? "flags" : "enum",
989 flags_type ? flags_type : enum_type);
990 return NULL;
993 type_string = flags_type ? "as" : "s";
994 strinfo = enum_state->strinfo;
996 else
998 if (!g_variant_type_string_is_valid (type_string))
1000 g_set_error (error, G_MARKUP_ERROR,
1001 G_MARKUP_ERROR_INVALID_CONTENT,
1002 _("invalid GVariant type string '%s'"), type_string);
1003 return NULL;
1006 strinfo = NULL;
1009 key = key_state_new (type_string, state->gettext_domain,
1010 enum_type != NULL, flags_type != NULL, strinfo);
1011 g_hash_table_insert (state->keys, g_strdup (name), key);
1013 return key;
1016 static void
1017 schema_state_add_override (SchemaState *state,
1018 KeyState **key_state,
1019 GString **string,
1020 const gchar *key,
1021 const gchar *l10n,
1022 const gchar *context,
1023 GError **error)
1025 SchemaState *parent;
1026 KeyState *original;
1028 if (state->extends == NULL)
1030 g_set_error_literal (error, G_MARKUP_ERROR,
1031 G_MARKUP_ERROR_INVALID_CONTENT,
1032 _("<override> given but schema isn't "
1033 "extending anything"));
1034 return;
1037 for (parent = state->extends; parent; parent = parent->extends)
1038 if ((original = g_hash_table_lookup (parent->keys, key)))
1039 break;
1041 if (original == NULL)
1043 g_set_error (error, G_MARKUP_ERROR,
1044 G_MARKUP_ERROR_INVALID_CONTENT,
1045 _("no <key name='%s'> to override"), key);
1046 return;
1049 if (g_hash_table_lookup (state->keys, key))
1051 g_set_error (error, G_MARKUP_ERROR,
1052 G_MARKUP_ERROR_INVALID_CONTENT,
1053 _("<override name='%s'> already specified"), key);
1054 return;
1057 *key_state = key_state_override (original, state->gettext_domain);
1058 *string = key_state_start_default (*key_state, l10n, context, error);
1059 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1062 static void
1063 override_state_end (KeyState **key_state,
1064 GString **string,
1065 GError **error)
1067 key_state_end_default (*key_state, string, error);
1068 *key_state = NULL;
1071 /* Handling of toplevel state {{{1 */
1072 typedef struct
1074 gboolean strict; /* TRUE if --strict was given */
1076 GHashTable *schema_table; /* string -> SchemaState */
1077 GHashTable *flags_table; /* string -> EnumState */
1078 GHashTable *enum_table; /* string -> EnumState */
1080 GSList *this_file_schemas; /* strings: <schema>s in this file */
1081 GSList *this_file_flagss; /* strings: <flags>s in this file */
1082 GSList *this_file_enums; /* strings: <enum>s in this file */
1084 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1086 SchemaState *schema_state; /* non-NULL when inside <schema> */
1087 KeyState *key_state; /* non-NULL when inside <key> */
1088 EnumState *enum_state; /* non-NULL when inside <enum> */
1090 GString *string; /* non-NULL when accepting text */
1091 } ParseState;
1093 static gboolean
1094 is_subclass (const gchar *class_name,
1095 const gchar *possible_parent,
1096 GHashTable *schema_table)
1098 SchemaState *class;
1100 if (strcmp (class_name, possible_parent) == 0)
1101 return TRUE;
1103 class = g_hash_table_lookup (schema_table, class_name);
1104 g_assert (class != NULL);
1106 return class->extends_name &&
1107 is_subclass (class->extends_name, possible_parent, schema_table);
1110 static void
1111 parse_state_start_schema (ParseState *state,
1112 const gchar *id,
1113 const gchar *path,
1114 const gchar *gettext_domain,
1115 const gchar *extends_name,
1116 const gchar *list_of,
1117 GError **error)
1119 SchemaState *extends;
1120 gchar *my_id;
1122 if (g_hash_table_lookup (state->schema_table, id))
1124 g_set_error (error, G_MARKUP_ERROR,
1125 G_MARKUP_ERROR_INVALID_CONTENT,
1126 _("<schema id='%s'> already specified"), id);
1127 return;
1130 if (extends_name)
1132 extends = g_hash_table_lookup (state->schema_table, extends_name);
1134 if (extends == NULL)
1136 g_set_error (error, G_MARKUP_ERROR,
1137 G_MARKUP_ERROR_INVALID_CONTENT,
1138 _("<schema id='%s'> extends not yet existing "
1139 "schema '%s'"), id, extends_name);
1140 return;
1143 else
1144 extends = NULL;
1146 if (list_of)
1148 SchemaState *tmp;
1150 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1152 g_set_error (error, G_MARKUP_ERROR,
1153 G_MARKUP_ERROR_INVALID_CONTENT,
1154 _("<schema id='%s'> is list of not yet existing "
1155 "schema '%s'"), id, list_of);
1156 return;
1159 if (tmp->path)
1161 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1162 _("Can not be a list of a schema with a path"));
1163 return;
1167 if (extends)
1169 if (extends->path)
1171 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1172 _("Can not extend a schema with a path"));
1173 return;
1176 if (list_of)
1178 if (extends->list_of == NULL)
1180 g_set_error (error, G_MARKUP_ERROR,
1181 G_MARKUP_ERROR_INVALID_CONTENT,
1182 _("<schema id='%s'> is a list, extending "
1183 "<schema id='%s'> which is not a list"),
1184 id, extends_name);
1185 return;
1188 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1190 g_set_error (error, G_MARKUP_ERROR,
1191 G_MARKUP_ERROR_INVALID_CONTENT,
1192 _("<schema id='%s' list-of='%s'> extends <schema "
1193 "id='%s' list-of='%s'> but '%s' does not "
1194 "extend '%s'"), id, list_of, extends_name,
1195 extends->list_of, list_of, extends->list_of);
1196 return;
1199 else
1200 /* by default we are a list of the same thing that the schema
1201 * we are extending is a list of (which might be nothing)
1203 list_of = extends->list_of;
1206 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1208 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1209 _("a path, if given, must begin and end with a slash"));
1210 return;
1213 if (path && list_of && !g_str_has_suffix (path, ":/"))
1215 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1216 _("the path of a list must end with ':/'"));
1217 return;
1220 if (path && (g_str_has_prefix (path, "/apps/") ||
1221 g_str_has_prefix (path, "/desktop/") ||
1222 g_str_has_prefix (path, "/system/")))
1223 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1224 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id, path);
1226 state->schema_state = schema_state_new (path, gettext_domain,
1227 extends, extends_name, list_of);
1229 my_id = g_strdup (id);
1230 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1231 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1234 static void
1235 parse_state_start_enum (ParseState *state,
1236 const gchar *id,
1237 gboolean is_flags,
1238 GError **error)
1240 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1241 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1242 gchar *my_id;
1244 if (g_hash_table_lookup (table, id))
1246 g_set_error (error, G_MARKUP_ERROR,
1247 G_MARKUP_ERROR_INVALID_CONTENT,
1248 _("<%s id='%s'> already specified"),
1249 is_flags ? "flags" : "enum", id);
1250 return;
1253 state->enum_state = enum_state_new (is_flags);
1255 my_id = g_strdup (id);
1256 *list = g_slist_prepend (*list, my_id);
1257 g_hash_table_insert (table, my_id, state->enum_state);
1260 /* GMarkup Parser Functions {{{1 */
1262 /* Start element {{{2 */
1263 static void
1264 start_element (GMarkupParseContext *context,
1265 const gchar *element_name,
1266 const gchar **attribute_names,
1267 const gchar **attribute_values,
1268 gpointer user_data,
1269 GError **error)
1271 ParseState *state = user_data;
1272 const GSList *element_stack;
1273 const gchar *container;
1275 element_stack = g_markup_parse_context_get_element_stack (context);
1276 container = element_stack->next ? element_stack->next->data : NULL;
1278 #define COLLECT(first, ...) \
1279 g_markup_collect_attributes (element_name, \
1280 attribute_names, attribute_values, error, \
1281 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1282 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1283 #define STRDUP G_MARKUP_COLLECT_STRDUP
1284 #define STRING G_MARKUP_COLLECT_STRING
1285 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1287 /* Toplevel items {{{3 */
1288 if (container == NULL)
1290 if (strcmp (element_name, "schemalist") == 0)
1292 COLLECT (OPTIONAL | STRDUP,
1293 "gettext-domain",
1294 &state->schemalist_domain);
1295 return;
1300 /* children of <schemalist> {{{3 */
1301 else if (strcmp (container, "schemalist") == 0)
1303 if (strcmp (element_name, "schema") == 0)
1305 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1306 if (COLLECT (STRING, "id", &id,
1307 OPTIONAL | STRING, "path", &path,
1308 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1309 OPTIONAL | STRING, "extends", &extends,
1310 OPTIONAL | STRING, "list-of", &list_of))
1311 parse_state_start_schema (state, id, path,
1312 gettext_domain ? gettext_domain
1313 : state->schemalist_domain,
1314 extends, list_of, error);
1315 return;
1318 else if (strcmp (element_name, "enum") == 0)
1320 const gchar *id;
1321 if (COLLECT (STRING, "id", &id))
1322 parse_state_start_enum (state, id, FALSE, error);
1323 return;
1326 else if (strcmp (element_name, "flags") == 0)
1328 const gchar *id;
1329 if (COLLECT (STRING, "id", &id))
1330 parse_state_start_enum (state, id, TRUE, error);
1331 return;
1336 /* children of <schema> {{{3 */
1337 else if (strcmp (container, "schema") == 0)
1339 if (strcmp (element_name, "key") == 0)
1341 const gchar *name, *type_string, *enum_type, *flags_type;
1343 if (COLLECT (STRING, "name", &name,
1344 OPTIONAL | STRING, "type", &type_string,
1345 OPTIONAL | STRING, "enum", &enum_type,
1346 OPTIONAL | STRING, "flags", &flags_type))
1348 state->key_state = schema_state_add_key (state->schema_state,
1349 state->enum_table,
1350 state->flags_table,
1351 name, type_string,
1352 enum_type, flags_type,
1353 error);
1354 return;
1356 else if (strcmp (element_name, "child") == 0)
1358 const gchar *name, *schema;
1360 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1361 schema_state_add_child (state->schema_state,
1362 name, schema, error);
1363 return;
1365 else if (strcmp (element_name, "override") == 0)
1367 const gchar *name, *l10n, *context;
1369 if (COLLECT (STRING, "name", &name,
1370 OPTIONAL | STRING, "l10n", &l10n,
1371 OPTIONAL | STRING, "context", &context))
1372 schema_state_add_override (state->schema_state,
1373 &state->key_state, &state->string,
1374 name, l10n, context, error);
1375 return;
1379 /* children of <key> {{{3 */
1380 else if (strcmp (container, "key") == 0)
1382 if (strcmp (element_name, "default") == 0)
1384 const gchar *l10n, *context;
1385 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1386 STRING | OPTIONAL, "context", &context))
1387 state->string = key_state_start_default (state->key_state,
1388 l10n, context, error);
1389 return;
1392 else if (strcmp (element_name, "summary") == 0)
1394 if (NO_ATTRS ())
1396 if (state->key_state->summary_seen && state->strict)
1397 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1398 _("Only one <%s> element allowed inside <%s>"),
1399 element_name, container);
1400 else
1401 state->string = g_string_new (NULL);
1403 state->key_state->summary_seen = TRUE;
1405 return;
1408 else if (strcmp (element_name, "description") == 0)
1410 if (NO_ATTRS ())
1412 if (state->key_state->description_seen && state->strict)
1413 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1414 _("Only one <%s> element allowed inside <%s>"),
1415 element_name, container);
1416 else
1417 state->string = g_string_new (NULL);
1419 state->key_state->description_seen = TRUE;
1421 return;
1424 else if (strcmp (element_name, "range") == 0)
1426 const gchar *min, *max;
1427 if (COLLECT (STRING | OPTIONAL, "min", &min,
1428 STRING | OPTIONAL, "max", &max))
1429 key_state_set_range (state->key_state, min, max, error);
1430 return;
1433 else if (strcmp (element_name, "choices") == 0)
1435 if (NO_ATTRS ())
1436 key_state_start_choices (state->key_state, error);
1437 return;
1440 else if (strcmp (element_name, "aliases") == 0)
1442 if (NO_ATTRS ())
1443 key_state_start_aliases (state->key_state, error);
1444 return;
1449 /* children of <choices> {{{3 */
1450 else if (strcmp (container, "choices") == 0)
1452 if (strcmp (element_name, "choice") == 0)
1454 const gchar *value;
1455 if (COLLECT (STRING, "value", &value))
1456 key_state_add_choice (state->key_state, value, error);
1457 return;
1462 /* children of <aliases> {{{3 */
1463 else if (strcmp (container, "aliases") == 0)
1465 if (strcmp (element_name, "alias") == 0)
1467 const gchar *value, *target;
1468 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1469 key_state_add_alias (state->key_state, value, target, error);
1470 return;
1475 /* children of <enum> {{{3 */
1476 else if (strcmp (container, "enum") == 0 ||
1477 strcmp (container, "flags") == 0)
1479 if (strcmp (element_name, "value") == 0)
1481 const gchar *nick, *valuestr;
1482 if (COLLECT (STRING, "nick", &nick,
1483 STRING, "value", &valuestr))
1484 enum_state_add_value (state->enum_state, nick, valuestr, error);
1485 return;
1488 /* 3}}} */
1490 if (container)
1491 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1492 _("Element <%s> not allowed inside <%s>"),
1493 element_name, container);
1494 else
1495 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1496 _("Element <%s> not allowed at the top level"), element_name);
1498 /* 2}}} */
1499 /* End element {{{2 */
1501 static void
1502 key_state_end (KeyState **state_ptr,
1503 GError **error)
1505 KeyState *state;
1507 state = *state_ptr;
1508 *state_ptr = NULL;
1510 if (state->default_value == NULL)
1512 g_set_error_literal (error,
1513 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1514 "element <default> is required in <key>");
1515 return;
1519 static void
1520 schema_state_end (SchemaState **state_ptr,
1521 GError **error)
1523 *state_ptr = NULL;
1526 static void
1527 end_element (GMarkupParseContext *context,
1528 const gchar *element_name,
1529 gpointer user_data,
1530 GError **error)
1532 ParseState *state = user_data;
1534 if (strcmp (element_name, "schemalist") == 0)
1536 g_free (state->schemalist_domain);
1537 state->schemalist_domain = NULL;
1540 else if (strcmp (element_name, "enum") == 0 ||
1541 strcmp (element_name, "flags") == 0)
1542 enum_state_end (&state->enum_state, error);
1544 else if (strcmp (element_name, "schema") == 0)
1545 schema_state_end (&state->schema_state, error);
1547 else if (strcmp (element_name, "override") == 0)
1548 override_state_end (&state->key_state, &state->string, error);
1550 else if (strcmp (element_name, "key") == 0)
1551 key_state_end (&state->key_state, error);
1553 else if (strcmp (element_name, "default") == 0)
1554 key_state_end_default (state->key_state, &state->string, error);
1556 else if (strcmp (element_name, "choices") == 0)
1557 key_state_end_choices (state->key_state, error);
1559 else if (strcmp (element_name, "aliases") == 0)
1560 key_state_end_aliases (state->key_state, error);
1562 if (state->string)
1564 g_string_free (state->string, TRUE);
1565 state->string = NULL;
1568 /* Text {{{2 */
1569 static void
1570 text (GMarkupParseContext *context,
1571 const gchar *text,
1572 gsize text_len,
1573 gpointer user_data,
1574 GError **error)
1576 ParseState *state = user_data;
1578 if (state->string)
1580 /* we are expecting a string, so store the text data.
1582 * we store the data verbatim here and deal with whitespace
1583 * later on. there are two reasons for that:
1585 * 1) whitespace is handled differently depending on the tag
1586 * type.
1588 * 2) we could do leading whitespace removal by refusing to
1589 * insert it into state->string if it's at the start, but for
1590 * trailing whitespace, we have no idea if there is another
1591 * text() call coming or not.
1593 g_string_append_len (state->string, text, text_len);
1595 else
1597 /* string is not expected: accept (and ignore) pure whitespace */
1598 gsize i;
1600 for (i = 0; i < text_len; i++)
1601 if (!g_ascii_isspace (text[i]))
1603 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1604 _("text may not appear inside <%s>"),
1605 g_markup_parse_context_get_element (context));
1606 break;
1611 /* Write to GVDB {{{1 */
1612 typedef struct
1614 GHashTable *table;
1615 GvdbItem *root;
1616 } GvdbPair;
1618 static void
1619 gvdb_pair_init (GvdbPair *pair)
1621 pair->table = gvdb_hash_table_new (NULL, NULL);
1622 pair->root = gvdb_hash_table_insert (pair->table, "");
1625 static void
1626 gvdb_pair_clear (GvdbPair *pair)
1628 g_hash_table_unref (pair->table);
1631 typedef struct
1633 GHashTable *schema_table;
1634 GvdbPair root_pair;
1635 } WriteToFileData;
1637 typedef struct
1639 GHashTable *schema_table;
1640 GvdbPair pair;
1641 gboolean l10n;
1642 } OutputSchemaData;
1644 static void
1645 output_key (gpointer key,
1646 gpointer value,
1647 gpointer user_data)
1649 OutputSchemaData *data;
1650 const gchar *name;
1651 KeyState *state;
1652 GvdbItem *item;
1653 GVariant *serialised = NULL;
1655 name = key;
1656 state = value;
1657 data = user_data;
1659 item = gvdb_hash_table_insert (data->pair.table, name);
1660 gvdb_item_set_parent (item, data->pair.root);
1661 serialised = key_state_serialise (state);
1662 gvdb_item_set_value (item, serialised);
1663 g_variant_unref (serialised);
1665 if (state->l10n)
1666 data->l10n = TRUE;
1668 if (state->child_schema &&
1669 !g_hash_table_lookup (data->schema_table, state->child_schema))
1670 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1671 state->child_schema);
1674 static void
1675 output_schema (gpointer key,
1676 gpointer value,
1677 gpointer user_data)
1679 WriteToFileData *wtf_data = user_data;
1680 OutputSchemaData data;
1681 GvdbPair *root_pair;
1682 SchemaState *state;
1683 const gchar *id;
1684 GvdbItem *item;
1686 id = key;
1687 state = value;
1688 root_pair = &wtf_data->root_pair;
1690 data.schema_table = wtf_data->schema_table;
1691 gvdb_pair_init (&data.pair);
1692 data.l10n = FALSE;
1694 item = gvdb_hash_table_insert (root_pair->table, id);
1695 gvdb_item_set_parent (item, root_pair->root);
1696 gvdb_item_set_hash_table (item, data.pair.table);
1698 g_hash_table_foreach (state->keys, output_key, &data);
1700 if (state->path)
1701 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1703 if (state->extends_name)
1704 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1705 state->extends_name);
1707 if (state->list_of)
1708 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1709 state->list_of);
1711 if (data.l10n)
1712 gvdb_hash_table_insert_string (data.pair.table,
1713 ".gettext-domain",
1714 state->gettext_domain);
1716 gvdb_pair_clear (&data.pair);
1719 static gboolean
1720 write_to_file (GHashTable *schema_table,
1721 const gchar *filename,
1722 GError **error)
1724 WriteToFileData data;
1725 gboolean success;
1727 data.schema_table = schema_table;
1729 gvdb_pair_init (&data.root_pair);
1731 g_hash_table_foreach (schema_table, output_schema, &data);
1733 success = gvdb_table_write_contents (data.root_pair.table, filename,
1734 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1735 error);
1736 g_hash_table_unref (data.root_pair.table);
1738 return success;
1741 /* Parser driver {{{1 */
1742 static GHashTable *
1743 parse_gschema_files (gchar **files,
1744 gboolean strict)
1746 GMarkupParser parser = { start_element, end_element, text };
1747 ParseState state = { 0, };
1748 const gchar *filename;
1749 GError *error = NULL;
1751 state.strict = strict;
1753 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1754 g_free, enum_state_free);
1756 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1757 g_free, enum_state_free);
1759 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1760 g_free, schema_state_free);
1762 while ((filename = *files++) != NULL)
1764 GMarkupParseContext *context;
1765 gchar *contents;
1766 gsize size;
1767 gint line, col;
1769 if (!g_file_get_contents (filename, &contents, &size, &error))
1771 fprintf (stderr, "%s\n", error->message);
1772 g_clear_error (&error);
1773 continue;
1776 context = g_markup_parse_context_new (&parser,
1777 G_MARKUP_TREAT_CDATA_AS_TEXT |
1778 G_MARKUP_PREFIX_ERROR_POSITION |
1779 G_MARKUP_IGNORE_QUALIFIED,
1780 &state, NULL);
1783 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1784 !g_markup_parse_context_end_parse (context, &error))
1786 GSList *item;
1788 /* back out any changes from this file */
1789 for (item = state.this_file_schemas; item; item = item->next)
1790 g_hash_table_remove (state.schema_table, item->data);
1792 for (item = state.this_file_flagss; item; item = item->next)
1793 g_hash_table_remove (state.flags_table, item->data);
1795 for (item = state.this_file_enums; item; item = item->next)
1796 g_hash_table_remove (state.enum_table, item->data);
1798 /* let them know */
1799 g_markup_parse_context_get_position (context, &line, &col);
1800 fprintf (stderr, "%s:%d:%d %s. ", filename, line, col, error->message);
1801 g_clear_error (&error);
1803 if (strict)
1805 /* Translators: Do not translate "--strict". */
1806 fprintf (stderr, _("--strict was specified; exiting.\n"));
1807 g_hash_table_unref (state.schema_table);
1808 g_hash_table_unref (state.flags_table);
1809 g_hash_table_unref (state.enum_table);
1811 g_free (contents);
1813 return NULL;
1815 else
1816 fprintf (stderr, _("This entire file has been ignored.\n"));
1819 /* cleanup */
1820 g_free (contents);
1821 g_markup_parse_context_free (context);
1822 g_slist_free (state.this_file_schemas);
1823 g_slist_free (state.this_file_flagss);
1824 g_slist_free (state.this_file_enums);
1825 state.this_file_schemas = NULL;
1826 state.this_file_flagss = NULL;
1827 state.this_file_enums = NULL;
1830 g_hash_table_unref (state.flags_table);
1831 g_hash_table_unref (state.enum_table);
1833 return state.schema_table;
1836 static gint
1837 compare_strings (gconstpointer a,
1838 gconstpointer b)
1840 gchar *one = *(gchar **) a;
1841 gchar *two = *(gchar **) b;
1842 gint cmp;
1844 cmp = g_str_has_suffix (two, ".enums.xml") -
1845 g_str_has_suffix (one, ".enums.xml");
1847 if (!cmp)
1848 cmp = strcmp (one, two);
1850 return cmp;
1853 static gboolean
1854 set_overrides (GHashTable *schema_table,
1855 gchar **files,
1856 gboolean strict)
1858 const gchar *filename;
1859 GError *error = NULL;
1861 while ((filename = *files++))
1863 GKeyFile *key_file;
1864 gchar **groups;
1865 gint i;
1867 key_file = g_key_file_new ();
1868 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1870 fprintf (stderr, "%s: %s. ", filename, error->message);
1871 g_key_file_free (key_file);
1872 g_clear_error (&error);
1874 if (!strict)
1876 fprintf (stderr, _("Ignoring this file.\n"));
1877 continue;
1880 fprintf (stderr, _("--strict was specified; exiting.\n"));
1881 return FALSE;
1884 groups = g_key_file_get_groups (key_file, NULL);
1886 for (i = 0; groups[i]; i++)
1888 const gchar *group = groups[i];
1889 SchemaState *schema;
1890 gchar **keys;
1891 gint j;
1893 schema = g_hash_table_lookup (schema_table, group);
1895 if (schema == NULL)
1896 /* Having the schema not be installed is expected to be a
1897 * common case. Don't even emit an error message about
1898 * that.
1900 continue;
1902 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1903 g_assert (keys != NULL);
1905 for (j = 0; keys[j]; j++)
1907 const gchar *key = keys[j];
1908 KeyState *state;
1909 GVariant *value;
1910 gchar *string;
1912 state = g_hash_table_lookup (schema->keys, key);
1914 if (state == NULL)
1916 fprintf (stderr, _("No such key '%s' in schema '%s' as "
1917 "specified in override file '%s'"),
1918 key, group, filename);
1920 if (!strict)
1922 fprintf (stderr, _("; ignoring override for this key.\n"));
1923 continue;
1926 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1927 g_key_file_free (key_file);
1928 g_strfreev (groups);
1929 g_strfreev (keys);
1931 return FALSE;
1934 string = g_key_file_get_value (key_file, group, key, NULL);
1935 g_assert (string != NULL);
1937 value = g_variant_parse (state->type, string,
1938 NULL, NULL, &error);
1940 if (value == NULL)
1942 fprintf (stderr, _("error parsing key '%s' in schema '%s' "
1943 "as specified in override file '%s': "
1944 "%s."),
1945 key, group, filename, error->message);
1947 g_clear_error (&error);
1948 g_free (string);
1950 if (!strict)
1952 fprintf (stderr, _("Ignoring override for this key.\n"));
1953 continue;
1956 fprintf (stderr, _("--strict was specified; exiting.\n"));
1957 g_key_file_free (key_file);
1958 g_strfreev (groups);
1959 g_strfreev (keys);
1961 return FALSE;
1964 if (state->minimum)
1966 if (g_variant_compare (value, state->minimum) < 0 ||
1967 g_variant_compare (value, state->maximum) > 0)
1969 fprintf (stderr,
1970 _("override for key '%s' in schema '%s' in "
1971 "override file '%s' is outside the range "
1972 "given in the schema"),
1973 key, group, filename);
1975 g_variant_unref (value);
1976 g_free (string);
1978 if (!strict)
1980 fprintf (stderr, _("; ignoring override for this key.\n"));
1981 continue;
1984 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1985 g_key_file_free (key_file);
1986 g_strfreev (groups);
1987 g_strfreev (keys);
1989 return FALSE;
1993 else if (state->strinfo->len)
1995 if (!is_valid_choices (value, state->strinfo))
1997 fprintf (stderr,
1998 _("override for key '%s' in schema '%s' in "
1999 "override file '%s' is not in the list "
2000 "of valid choices"),
2001 key, group, filename);
2003 g_variant_unref (value);
2004 g_free (string);
2006 if (!strict)
2008 fprintf (stderr, _("; ignoring override for this key.\n"));
2009 continue;
2012 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2013 g_key_file_free (key_file);
2014 g_strfreev (groups);
2015 g_strfreev (keys);
2017 return FALSE;
2021 g_variant_unref (state->default_value);
2022 state->default_value = value;
2023 g_free (string);
2026 g_strfreev (keys);
2029 g_strfreev (groups);
2032 return TRUE;
2036 main (int argc, char **argv)
2038 GError *error = NULL;
2039 GHashTable *table = NULL;
2040 GDir *dir = NULL;
2041 const gchar *file;
2042 const gchar *srcdir;
2043 gboolean show_version_and_exit = FALSE;
2044 gchar *targetdir = NULL;
2045 gchar *target = NULL;
2046 gboolean dry_run = FALSE;
2047 gboolean strict = FALSE;
2048 gchar **schema_files = NULL;
2049 gchar **override_files = NULL;
2050 GOptionContext *context = NULL;
2051 gint retval;
2052 GOptionEntry entries[] = {
2053 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
2054 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2055 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2056 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2057 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2059 /* These options are only for use in the gschema-compile tests */
2060 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2061 { NULL }
2064 #ifdef G_OS_WIN32
2065 gchar *tmp = NULL;
2066 #endif
2068 setlocale (LC_ALL, "");
2069 textdomain (GETTEXT_PACKAGE);
2071 #ifdef G_OS_WIN32
2072 tmp = _glib_get_locale_dir ();
2073 bindtextdomain (GETTEXT_PACKAGE, tmp);
2074 #else
2075 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2076 #endif
2078 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2079 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2080 #endif
2082 context = g_option_context_new (N_("DIRECTORY"));
2083 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2084 g_option_context_set_summary (context,
2085 N_("Compile all GSettings schema files into a schema cache.\n"
2086 "Schema files are required to have the extension .gschema.xml,\n"
2087 "and the cache file is called gschemas.compiled."));
2088 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2090 if (!g_option_context_parse (context, &argc, &argv, &error))
2092 fprintf (stderr, "%s\n", error->message);
2093 retval = 1;
2094 goto done;
2097 if (show_version_and_exit)
2099 g_print (PACKAGE_VERSION "\n");
2100 retval = 0;
2101 goto done;
2104 if (!schema_files && argc != 2)
2106 fprintf (stderr, _("You should give exactly one directory name\n"));
2107 retval = 1;
2108 goto done;
2111 srcdir = argv[1];
2113 target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
2115 if (!schema_files)
2117 GPtrArray *overrides;
2118 GPtrArray *files;
2120 files = g_ptr_array_new ();
2121 overrides = g_ptr_array_new ();
2123 dir = g_dir_open (srcdir, 0, &error);
2124 if (dir == NULL)
2126 fprintf (stderr, "%s\n", error->message);
2128 g_ptr_array_unref (files);
2129 g_ptr_array_unref (overrides);
2131 retval = 1;
2132 goto done;
2135 while ((file = g_dir_read_name (dir)) != NULL)
2137 if (g_str_has_suffix (file, ".gschema.xml") ||
2138 g_str_has_suffix (file, ".enums.xml"))
2139 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2141 else if (g_str_has_suffix (file, ".gschema.override"))
2142 g_ptr_array_add (overrides,
2143 g_build_filename (srcdir, file, NULL));
2146 if (files->len == 0)
2148 fprintf (stdout, _("No schema files found: "));
2150 if (g_unlink (target))
2151 fprintf (stdout, _("doing nothing.\n"));
2153 else
2154 fprintf (stdout, _("removed existing output file.\n"));
2156 g_ptr_array_unref (files);
2157 g_ptr_array_unref (overrides);
2159 retval = 0;
2160 goto done;
2162 g_ptr_array_sort (files, compare_strings);
2163 g_ptr_array_add (files, NULL);
2165 g_ptr_array_sort (overrides, compare_strings);
2166 g_ptr_array_add (overrides, NULL);
2168 schema_files = (char **) g_ptr_array_free (files, FALSE);
2169 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2172 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2174 retval = 1;
2175 goto done;
2178 if (override_files != NULL &&
2179 !set_overrides (table, override_files, strict))
2181 retval = 1;
2182 goto done;
2185 if (!dry_run && !write_to_file (table, target, &error))
2187 fprintf (stderr, "%s\n", error->message);
2188 retval = 1;
2189 goto done;
2192 /* Success. */
2193 retval = 0;
2195 done:
2196 g_clear_error (&error);
2197 g_clear_pointer (&table, g_hash_table_unref);
2198 g_clear_pointer (&dir, g_dir_close);
2199 g_free (targetdir);
2200 g_free (target);
2201 g_strfreev (schema_files);
2202 g_strfreev (override_files);
2203 g_option_context_free (context);
2205 #ifdef G_OS_WIN32
2206 g_free (tmp);
2207 #endif
2209 return retval;
2212 /* Epilogue {{{1 */
2214 /* vim:set foldmethod=marker: */