Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / glib-compile-schemas.c
blobd4340d463f3d40f6e1f627851583a2694224bfc8
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 GVariantDict *desktop_overrides;
184 GString *strinfo;
185 gboolean is_enum;
186 gboolean is_flags;
188 GVariant *minimum;
189 GVariant *maximum;
191 gboolean has_choices;
192 gboolean has_aliases;
193 gboolean is_override;
195 gboolean checked;
196 GVariant *serialised;
198 gboolean summary_seen;
199 gboolean description_seen;
200 } KeyState;
202 static KeyState *
203 key_state_new (const gchar *type_string,
204 const gchar *gettext_domain,
205 gboolean is_enum,
206 gboolean is_flags,
207 GString *strinfo)
209 KeyState *state;
211 state = g_slice_new0 (KeyState);
212 state->type = g_variant_type_new (type_string);
213 state->have_gettext_domain = gettext_domain != NULL;
214 state->is_enum = is_enum;
215 state->is_flags = is_flags;
216 state->summary_seen = FALSE;
217 state->description_seen = FALSE;
219 if (strinfo)
220 state->strinfo = g_string_new_len (strinfo->str, strinfo->len);
221 else
222 state->strinfo = g_string_new (NULL);
224 return state;
227 static KeyState *
228 key_state_override (KeyState *state,
229 const gchar *gettext_domain)
231 KeyState *copy;
233 copy = g_slice_new0 (KeyState);
234 copy->type = g_variant_type_copy (state->type);
235 copy->have_gettext_domain = gettext_domain != NULL;
236 copy->strinfo = g_string_new_len (state->strinfo->str,
237 state->strinfo->len);
238 copy->is_enum = state->is_enum;
239 copy->is_flags = state->is_flags;
240 copy->is_override = TRUE;
242 if (state->minimum)
244 copy->minimum = g_variant_ref (state->minimum);
245 copy->maximum = g_variant_ref (state->maximum);
248 return copy;
251 static KeyState *
252 key_state_new_child (const gchar *child_schema)
254 KeyState *state;
256 state = g_slice_new0 (KeyState);
257 state->child_schema = g_strdup (child_schema);
259 return state;
262 static gboolean
263 is_valid_choices (GVariant *variant,
264 GString *strinfo)
266 switch (g_variant_classify (variant))
268 case G_VARIANT_CLASS_MAYBE:
269 case G_VARIANT_CLASS_ARRAY:
271 gboolean valid = TRUE;
272 GVariantIter iter;
274 g_variant_iter_init (&iter, variant);
276 while (valid && (variant = g_variant_iter_next_value (&iter)))
278 valid = is_valid_choices (variant, strinfo);
279 g_variant_unref (variant);
282 return valid;
285 case G_VARIANT_CLASS_STRING:
286 return strinfo_is_string_valid ((const guint32 *) strinfo->str,
287 strinfo->len / 4,
288 g_variant_get_string (variant, NULL));
290 default:
291 g_assert_not_reached ();
296 /* Gets called at </default> </choices> or <range/> to check for
297 * validity of the default value so that any inconsistency is
298 * reported as soon as it is encountered.
300 static void
301 key_state_check_range (KeyState *state,
302 GError **error)
304 if (state->default_value)
306 const gchar *tag;
308 tag = state->is_override ? "override" : "default";
310 if (state->minimum)
312 if (g_variant_compare (state->default_value, state->minimum) < 0 ||
313 g_variant_compare (state->default_value, state->maximum) > 0)
315 g_set_error (error, G_MARKUP_ERROR,
316 G_MARKUP_ERROR_INVALID_CONTENT,
317 _("<%s> is not contained in "
318 "the specified range"), tag);
322 else if (state->strinfo->len)
324 if (!is_valid_choices (state->default_value, state->strinfo))
326 if (state->is_enum)
327 g_set_error (error, G_MARKUP_ERROR,
328 G_MARKUP_ERROR_INVALID_CONTENT,
329 _("<%s> is not a valid member of "
330 "the specified enumerated type"), tag);
332 else if (state->is_flags)
333 g_set_error (error, G_MARKUP_ERROR,
334 G_MARKUP_ERROR_INVALID_CONTENT,
335 _("<%s> contains string not in the "
336 "specified flags type"), tag);
338 else
339 g_set_error (error, G_MARKUP_ERROR,
340 G_MARKUP_ERROR_INVALID_CONTENT,
341 _("<%s> contains a string not in "
342 "<choices>"), tag);
348 static void
349 key_state_set_range (KeyState *state,
350 const gchar *min_str,
351 const gchar *max_str,
352 GError **error)
354 const struct {
355 const gchar type;
356 const gchar *min;
357 const gchar *max;
358 } table[] = {
359 { 'y', "0", "255" },
360 { 'n', "-32768", "32767" },
361 { 'q', "0", "65535" },
362 { 'i', "-2147483648", "2147483647" },
363 { 'u', "0", "4294967295" },
364 { 'x', "-9223372036854775808", "9223372036854775807" },
365 { 't', "0", "18446744073709551615" },
366 { 'd', "-inf", "inf" },
368 gboolean type_ok = FALSE;
369 gint i;
371 if (state->minimum)
373 g_set_error_literal (error, G_MARKUP_ERROR,
374 G_MARKUP_ERROR_INVALID_CONTENT,
375 _("<range/> already specified for this key"));
376 return;
379 for (i = 0; i < G_N_ELEMENTS (table); i++)
380 if (*(char *) state->type == table[i].type)
382 min_str = min_str ? min_str : table[i].min;
383 max_str = max_str ? max_str : table[i].max;
384 type_ok = TRUE;
385 break;
388 if (!type_ok)
390 gchar *type = g_variant_type_dup_string (state->type);
391 g_set_error (error, G_MARKUP_ERROR,
392 G_MARKUP_ERROR_INVALID_CONTENT,
393 _("<range> not allowed for keys of type “%s”"), type);
394 g_free (type);
395 return;
398 state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
399 if (state->minimum == NULL)
400 return;
402 state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
403 if (state->maximum == NULL)
404 return;
406 if (g_variant_compare (state->minimum, state->maximum) > 0)
408 g_set_error (error, G_MARKUP_ERROR,
409 G_MARKUP_ERROR_INVALID_CONTENT,
410 _("<range> specified minimum is greater than maximum"));
411 return;
414 key_state_check_range (state, error);
417 static GString *
418 key_state_start_default (KeyState *state,
419 const gchar *l10n,
420 const gchar *context,
421 GError **error)
423 if (l10n != NULL)
425 if (strcmp (l10n, "messages") == 0)
426 state->l10n = 'm';
428 else if (strcmp (l10n, "time") == 0)
429 state->l10n = 't';
431 else
433 g_set_error (error, G_MARKUP_ERROR,
434 G_MARKUP_ERROR_INVALID_CONTENT,
435 _("unsupported l10n category: %s"), l10n);
436 return NULL;
439 if (!state->have_gettext_domain)
441 g_set_error_literal (error, G_MARKUP_ERROR,
442 G_MARKUP_ERROR_INVALID_CONTENT,
443 _("l10n requested, but no "
444 "gettext domain given"));
445 return NULL;
448 state->l10n_context = g_strdup (context);
451 else if (context != NULL)
453 g_set_error_literal (error, G_MARKUP_ERROR,
454 G_MARKUP_ERROR_INVALID_CONTENT,
455 _("translation context given for "
456 "value without l10n enabled"));
457 return NULL;
460 return g_string_new (NULL);
463 static void
464 key_state_end_default (KeyState *state,
465 GString **string,
466 GError **error)
468 state->unparsed_default_value = *string;
469 *string = NULL;
471 state->default_value = g_variant_parse (state->type,
472 state->unparsed_default_value->str,
473 NULL, NULL, error);
474 if (!state->default_value)
476 gchar *type = g_variant_type_dup_string (state->type);
477 g_prefix_error (error, _("Failed to parse <default> value of type “%s”: "), type);
478 g_free (type);
481 key_state_check_range (state, error);
484 static void
485 key_state_start_choices (KeyState *state,
486 GError **error)
488 const GVariantType *type = state->type;
490 if (state->is_enum)
492 g_set_error_literal (error, G_MARKUP_ERROR,
493 G_MARKUP_ERROR_INVALID_CONTENT,
494 _("<choices> cannot be specified for keys "
495 "tagged as having an enumerated type"));
496 return;
499 if (state->has_choices)
501 g_set_error_literal (error, G_MARKUP_ERROR,
502 G_MARKUP_ERROR_INVALID_CONTENT,
503 _("<choices> already specified for this key"));
504 return;
507 while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
508 type = g_variant_type_element (type);
510 if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
512 gchar *type_string = g_variant_type_dup_string (state->type);
513 g_set_error (error, G_MARKUP_ERROR,
514 G_MARKUP_ERROR_INVALID_CONTENT,
515 _("<choices> not allowed for keys of type “%s”"),
516 type_string);
517 g_free (type_string);
518 return;
522 static void
523 key_state_add_choice (KeyState *state,
524 const gchar *choice,
525 GError **error)
527 if (strinfo_builder_contains (state->strinfo, choice))
529 g_set_error (error, G_MARKUP_ERROR,
530 G_MARKUP_ERROR_INVALID_CONTENT,
531 _("<choice value='%s'/> already given"), choice);
532 return;
535 strinfo_builder_append_item (state->strinfo, choice, 0);
536 state->has_choices = TRUE;
539 static void
540 key_state_end_choices (KeyState *state,
541 GError **error)
543 if (!state->has_choices)
545 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
546 _("<choices> must contain at least one <choice>"));
547 return;
550 key_state_check_range (state, error);
553 static void
554 key_state_start_aliases (KeyState *state,
555 GError **error)
557 if (state->has_aliases)
558 g_set_error_literal (error, G_MARKUP_ERROR,
559 G_MARKUP_ERROR_INVALID_CONTENT,
560 _("<aliases> already specified for this key"));
561 else if (!state->is_flags && !state->is_enum && !state->has_choices)
562 g_set_error_literal (error, G_MARKUP_ERROR,
563 G_MARKUP_ERROR_INVALID_CONTENT,
564 _("<aliases> can only be specified for keys with "
565 "enumerated or flags types or after <choices>"));
568 static void
569 key_state_add_alias (KeyState *state,
570 const gchar *alias,
571 const gchar *target,
572 GError **error)
574 if (strinfo_builder_contains (state->strinfo, alias))
576 if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
577 state->strinfo->len / 4,
578 alias))
580 if (state->is_enum)
581 g_set_error (error, G_MARKUP_ERROR,
582 G_MARKUP_ERROR_INVALID_CONTENT,
583 _("<alias value='%s'/> given when “%s” is already "
584 "a member of the enumerated type"), alias, alias);
586 else
587 g_set_error (error, G_MARKUP_ERROR,
588 G_MARKUP_ERROR_INVALID_CONTENT,
589 _("<alias value='%s'/> given when "
590 "<choice value='%s'/> was already given"),
591 alias, alias);
594 else
595 g_set_error (error, G_MARKUP_ERROR,
596 G_MARKUP_ERROR_INVALID_CONTENT,
597 _("<alias value='%s'/> already specified"), alias);
599 return;
602 if (!strinfo_builder_append_alias (state->strinfo, alias, target))
604 g_set_error (error, G_MARKUP_ERROR,
605 G_MARKUP_ERROR_INVALID_CONTENT,
606 state->is_enum ?
607 _("alias target “%s” is not in enumerated type") :
608 _("alias target “%s” is not in <choices>"),
609 target);
610 return;
613 state->has_aliases = TRUE;
616 static void
617 key_state_end_aliases (KeyState *state,
618 GError **error)
620 if (!state->has_aliases)
622 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
623 _("<aliases> must contain at least one <alias>"));
624 return;
628 static gboolean
629 key_state_check (KeyState *state,
630 GError **error)
632 if (state->checked)
633 return TRUE;
635 return state->checked = TRUE;
638 static GVariant *
639 key_state_serialise (KeyState *state)
641 if (state->serialised == NULL)
643 if (state->child_schema)
645 state->serialised = g_variant_new_string (state->child_schema);
648 else
650 GVariantBuilder builder;
652 g_assert (key_state_check (state, NULL));
654 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
656 /* default value */
657 g_variant_builder_add_value (&builder, state->default_value);
659 /* translation */
660 if (state->l10n)
662 /* We are going to store the untranslated default for
663 * runtime translation according to the current locale.
664 * We need to strip leading and trailing whitespace from
665 * the string so that it's exactly the same as the one
666 * that ended up in the .po file for translation.
668 * We want to do this so that
670 * <default l10n='messages'>
671 * ['a', 'b', 'c']
672 * </default>
674 * ends up in the .po file like "['a', 'b', 'c']",
675 * omitting the extra whitespace at the start and end.
677 strip_string (state->unparsed_default_value);
679 if (state->l10n_context)
681 gint len;
683 /* Contextified messages are supported by prepending
684 * the context, followed by '\004' to the start of the
685 * message string. We do that here to save GSettings
686 * the work later on.
688 len = strlen (state->l10n_context);
689 state->l10n_context[len] = '\004';
690 g_string_prepend_len (state->unparsed_default_value,
691 state->l10n_context, len + 1);
692 g_free (state->l10n_context);
693 state->l10n_context = NULL;
696 g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
697 state->unparsed_default_value->str);
698 g_string_free (state->unparsed_default_value, TRUE);
699 state->unparsed_default_value = NULL;
702 /* choice, aliases, enums */
703 if (state->strinfo->len)
705 GVariant *array;
706 guint32 *words;
707 gpointer data;
708 gsize size;
709 gint i;
711 data = state->strinfo->str;
712 size = state->strinfo->len;
714 words = data;
715 for (i = 0; i < size / sizeof (guint32); i++)
716 words[i] = GUINT32_TO_LE (words[i]);
718 array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
719 data, size, TRUE,
720 g_free, data);
722 g_string_free (state->strinfo, FALSE);
723 state->strinfo = NULL;
725 g_variant_builder_add (&builder, "(y@au)",
726 state->is_flags ? 'f' :
727 state->is_enum ? 'e' : 'c',
728 array);
731 /* range */
732 if (state->minimum || state->maximum)
733 g_variant_builder_add (&builder, "(y(**))", 'r',
734 state->minimum, state->maximum);
736 /* per-desktop overrides */
737 if (state->desktop_overrides)
738 g_variant_builder_add (&builder, "(y@a{sv})", 'd',
739 g_variant_dict_end (state->desktop_overrides));
741 state->serialised = g_variant_builder_end (&builder);
744 g_variant_ref_sink (state->serialised);
747 return g_variant_ref (state->serialised);
750 static void
751 key_state_free (gpointer data)
753 KeyState *state = data;
755 g_free (state->child_schema);
757 if (state->type)
758 g_variant_type_free (state->type);
760 g_free (state->l10n_context);
762 if (state->unparsed_default_value)
763 g_string_free (state->unparsed_default_value, TRUE);
765 if (state->default_value)
766 g_variant_unref (state->default_value);
768 if (state->strinfo)
769 g_string_free (state->strinfo, TRUE);
771 if (state->minimum)
772 g_variant_unref (state->minimum);
774 if (state->maximum)
775 g_variant_unref (state->maximum);
777 if (state->serialised)
778 g_variant_unref (state->serialised);
780 if (state->desktop_overrides)
781 g_variant_dict_unref (state->desktop_overrides);
783 g_slice_free (KeyState, state);
786 /* Key name validity {{{1 */
787 static gboolean allow_any_name = FALSE;
789 static gboolean
790 is_valid_keyname (const gchar *key,
791 GError **error)
793 gint i;
795 if (key[0] == '\0')
797 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
798 _("Empty names are not permitted"));
799 return FALSE;
802 if (allow_any_name)
803 return TRUE;
805 if (!g_ascii_islower (key[0]))
807 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
808 _("Invalid name “%s”: names must begin "
809 "with a lowercase letter"), key);
810 return FALSE;
813 for (i = 1; key[i]; i++)
815 if (key[i] != '-' &&
816 !g_ascii_islower (key[i]) &&
817 !g_ascii_isdigit (key[i]))
819 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
820 _("Invalid name “%s”: invalid character “%c”; "
821 "only lowercase letters, numbers and hyphen (“-”) "
822 "are permitted"), key, key[i]);
823 return FALSE;
826 if (key[i] == '-' && key[i + 1] == '-')
828 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
829 _("Invalid name “%s”: two successive hyphens (“--”) "
830 "are not permitted"), key);
831 return FALSE;
835 if (key[i - 1] == '-')
837 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
838 _("Invalid name “%s”: the last character may not be a "
839 "hyphen (“-”)"), key);
840 return FALSE;
843 if (i > 1024)
845 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
846 _("Invalid name “%s”: maximum length is 1024"), key);
847 return FALSE;
850 return TRUE;
853 /* Handling of <schema> {{{1 */
854 typedef struct _SchemaState SchemaState;
855 struct _SchemaState
857 SchemaState *extends;
859 gchar *path;
860 gchar *gettext_domain;
861 gchar *extends_name;
862 gchar *list_of;
864 GHashTable *keys;
867 static SchemaState *
868 schema_state_new (const gchar *path,
869 const gchar *gettext_domain,
870 SchemaState *extends,
871 const gchar *extends_name,
872 const gchar *list_of)
874 SchemaState *state;
876 state = g_slice_new (SchemaState);
877 state->path = g_strdup (path);
878 state->gettext_domain = g_strdup (gettext_domain);
879 state->extends = extends;
880 state->extends_name = g_strdup (extends_name);
881 state->list_of = g_strdup (list_of);
882 state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
883 g_free, key_state_free);
885 return state;
888 static void
889 schema_state_free (gpointer data)
891 SchemaState *state = data;
893 g_free (state->path);
894 g_free (state->gettext_domain);
895 g_free (state->extends_name);
896 g_free (state->list_of);
897 g_hash_table_unref (state->keys);
898 g_slice_free (SchemaState, state);
901 static void
902 schema_state_add_child (SchemaState *state,
903 const gchar *name,
904 const gchar *schema,
905 GError **error)
907 gchar *childname;
909 if (!is_valid_keyname (name, error))
910 return;
912 childname = g_strconcat (name, "/", NULL);
914 if (g_hash_table_lookup (state->keys, childname))
916 g_set_error (error, G_MARKUP_ERROR,
917 G_MARKUP_ERROR_INVALID_CONTENT,
918 _("<child name='%s'> already specified"), name);
919 return;
922 g_hash_table_insert (state->keys, childname,
923 key_state_new_child (schema));
926 static KeyState *
927 schema_state_add_key (SchemaState *state,
928 GHashTable *enum_table,
929 GHashTable *flags_table,
930 const gchar *name,
931 const gchar *type_string,
932 const gchar *enum_type,
933 const gchar *flags_type,
934 GError **error)
936 SchemaState *node;
937 GString *strinfo;
938 KeyState *key;
940 if (state->list_of)
942 g_set_error_literal (error, G_MARKUP_ERROR,
943 G_MARKUP_ERROR_INVALID_CONTENT,
944 _("Cannot add keys to a “list-of” schema"));
945 return NULL;
948 if (!is_valid_keyname (name, error))
949 return NULL;
951 if (g_hash_table_lookup (state->keys, name))
953 g_set_error (error, G_MARKUP_ERROR,
954 G_MARKUP_ERROR_INVALID_CONTENT,
955 _("<key name='%s'> already specified"), name);
956 return NULL;
959 for (node = state; node; node = node->extends)
960 if (node->extends)
962 KeyState *shadow;
964 shadow = g_hash_table_lookup (node->extends->keys, name);
966 /* in case of <key> <override> <key> make sure we report the
967 * location of the original <key>, not the <override>.
969 if (shadow && !shadow->is_override)
971 g_set_error (error, G_MARKUP_ERROR,
972 G_MARKUP_ERROR_INVALID_CONTENT,
973 _("<key name='%s'> shadows <key name='%s'> in "
974 "<schema id='%s'>; use <override> to modify value"),
975 name, name, node->extends_name);
976 return NULL;
980 if ((type_string != NULL) + (enum_type != NULL) + (flags_type != NULL) != 1)
982 g_set_error (error, G_MARKUP_ERROR,
983 G_MARKUP_ERROR_MISSING_ATTRIBUTE,
984 _("Exactly one of “type”, “enum” or “flags” must "
985 "be specified as an attribute to <key>"));
986 return NULL;
989 if (type_string == NULL) /* flags or enums was specified */
991 EnumState *enum_state;
993 if (enum_type)
994 enum_state = g_hash_table_lookup (enum_table, enum_type);
995 else
996 enum_state = g_hash_table_lookup (flags_table, flags_type);
999 if (enum_state == NULL)
1001 g_set_error (error, G_MARKUP_ERROR,
1002 G_MARKUP_ERROR_INVALID_CONTENT,
1003 _("<%s id='%s'> not (yet) defined."),
1004 flags_type ? "flags" : "enum",
1005 flags_type ? flags_type : enum_type);
1006 return NULL;
1009 type_string = flags_type ? "as" : "s";
1010 strinfo = enum_state->strinfo;
1012 else
1014 if (!g_variant_type_string_is_valid (type_string))
1016 g_set_error (error, G_MARKUP_ERROR,
1017 G_MARKUP_ERROR_INVALID_CONTENT,
1018 _("Invalid GVariant type string “%s”"), type_string);
1019 return NULL;
1022 strinfo = NULL;
1025 key = key_state_new (type_string, state->gettext_domain,
1026 enum_type != NULL, flags_type != NULL, strinfo);
1027 g_hash_table_insert (state->keys, g_strdup (name), key);
1029 return key;
1032 static void
1033 schema_state_add_override (SchemaState *state,
1034 KeyState **key_state,
1035 GString **string,
1036 const gchar *key,
1037 const gchar *l10n,
1038 const gchar *context,
1039 GError **error)
1041 SchemaState *parent;
1042 KeyState *original;
1044 if (state->extends == NULL)
1046 g_set_error_literal (error, G_MARKUP_ERROR,
1047 G_MARKUP_ERROR_INVALID_CONTENT,
1048 _("<override> given but schema isn’t "
1049 "extending anything"));
1050 return;
1053 for (parent = state->extends; parent; parent = parent->extends)
1054 if ((original = g_hash_table_lookup (parent->keys, key)))
1055 break;
1057 if (original == NULL)
1059 g_set_error (error, G_MARKUP_ERROR,
1060 G_MARKUP_ERROR_INVALID_CONTENT,
1061 _("No <key name='%s'> to override"), key);
1062 return;
1065 if (g_hash_table_lookup (state->keys, key))
1067 g_set_error (error, G_MARKUP_ERROR,
1068 G_MARKUP_ERROR_INVALID_CONTENT,
1069 _("<override name='%s'> already specified"), key);
1070 return;
1073 *key_state = key_state_override (original, state->gettext_domain);
1074 *string = key_state_start_default (*key_state, l10n, context, error);
1075 g_hash_table_insert (state->keys, g_strdup (key), *key_state);
1078 static void
1079 override_state_end (KeyState **key_state,
1080 GString **string,
1081 GError **error)
1083 key_state_end_default (*key_state, string, error);
1084 *key_state = NULL;
1087 /* Handling of toplevel state {{{1 */
1088 typedef struct
1090 gboolean strict; /* TRUE if --strict was given */
1092 GHashTable *schema_table; /* string -> SchemaState */
1093 GHashTable *flags_table; /* string -> EnumState */
1094 GHashTable *enum_table; /* string -> EnumState */
1096 GSList *this_file_schemas; /* strings: <schema>s in this file */
1097 GSList *this_file_flagss; /* strings: <flags>s in this file */
1098 GSList *this_file_enums; /* strings: <enum>s in this file */
1100 gchar *schemalist_domain; /* the <schemalist> gettext domain */
1102 SchemaState *schema_state; /* non-NULL when inside <schema> */
1103 KeyState *key_state; /* non-NULL when inside <key> */
1104 EnumState *enum_state; /* non-NULL when inside <enum> */
1106 GString *string; /* non-NULL when accepting text */
1107 } ParseState;
1109 static gboolean
1110 is_subclass (const gchar *class_name,
1111 const gchar *possible_parent,
1112 GHashTable *schema_table)
1114 SchemaState *class;
1116 if (strcmp (class_name, possible_parent) == 0)
1117 return TRUE;
1119 class = g_hash_table_lookup (schema_table, class_name);
1120 g_assert (class != NULL);
1122 return class->extends_name &&
1123 is_subclass (class->extends_name, possible_parent, schema_table);
1126 static void
1127 parse_state_start_schema (ParseState *state,
1128 const gchar *id,
1129 const gchar *path,
1130 const gchar *gettext_domain,
1131 const gchar *extends_name,
1132 const gchar *list_of,
1133 GError **error)
1135 SchemaState *extends;
1136 gchar *my_id;
1138 if (g_hash_table_lookup (state->schema_table, id))
1140 g_set_error (error, G_MARKUP_ERROR,
1141 G_MARKUP_ERROR_INVALID_CONTENT,
1142 _("<schema id='%s'> already specified"), id);
1143 return;
1146 if (extends_name)
1148 extends = g_hash_table_lookup (state->schema_table, extends_name);
1150 if (extends == NULL)
1152 g_set_error (error, G_MARKUP_ERROR,
1153 G_MARKUP_ERROR_INVALID_CONTENT,
1154 _("<schema id='%s'> extends not yet existing "
1155 "schema “%s”"), id, extends_name);
1156 return;
1159 else
1160 extends = NULL;
1162 if (list_of)
1164 SchemaState *tmp;
1166 if (!(tmp = g_hash_table_lookup (state->schema_table, list_of)))
1168 g_set_error (error, G_MARKUP_ERROR,
1169 G_MARKUP_ERROR_INVALID_CONTENT,
1170 _("<schema id='%s'> is list of not yet existing "
1171 "schema “%s”"), id, list_of);
1172 return;
1175 if (tmp->path)
1177 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1178 _("Cannot be a list of a schema with a path"));
1179 return;
1183 if (extends)
1185 if (extends->path)
1187 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1188 _("Cannot extend a schema with a path"));
1189 return;
1192 if (list_of)
1194 if (extends->list_of == NULL)
1196 g_set_error (error, G_MARKUP_ERROR,
1197 G_MARKUP_ERROR_INVALID_CONTENT,
1198 _("<schema id='%s'> is a list, extending "
1199 "<schema id='%s'> which is not a list"),
1200 id, extends_name);
1201 return;
1204 if (!is_subclass (list_of, extends->list_of, state->schema_table))
1206 g_set_error (error, G_MARKUP_ERROR,
1207 G_MARKUP_ERROR_INVALID_CONTENT,
1208 _("<schema id='%s' list-of='%s'> extends <schema "
1209 "id='%s' list-of='%s'> but “%s” does not "
1210 "extend “%s”"), id, list_of, extends_name,
1211 extends->list_of, list_of, extends->list_of);
1212 return;
1215 else
1216 /* by default we are a list of the same thing that the schema
1217 * we are extending is a list of (which might be nothing)
1219 list_of = extends->list_of;
1222 if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1224 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1225 _("A path, if given, must begin and end with a slash"));
1226 return;
1229 if (path && list_of && !g_str_has_suffix (path, ":/"))
1231 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1232 _("The path of a list must end with “:/”"));
1233 return;
1236 if (path && (g_str_has_prefix (path, "/apps/") ||
1237 g_str_has_prefix (path, "/desktop/") ||
1238 g_str_has_prefix (path, "/system/")))
1240 gchar *message = NULL;
1241 message = g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1242 "Paths starting with "
1243 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1244 id, path);
1245 g_printerr ("%s\n", message);
1246 g_free (message);
1249 state->schema_state = schema_state_new (path, gettext_domain,
1250 extends, extends_name, list_of);
1252 my_id = g_strdup (id);
1253 state->this_file_schemas = g_slist_prepend (state->this_file_schemas, my_id);
1254 g_hash_table_insert (state->schema_table, my_id, state->schema_state);
1257 static void
1258 parse_state_start_enum (ParseState *state,
1259 const gchar *id,
1260 gboolean is_flags,
1261 GError **error)
1263 GSList **list = is_flags ? &state->this_file_flagss : &state->this_file_enums;
1264 GHashTable *table = is_flags ? state->flags_table : state->enum_table;
1265 gchar *my_id;
1267 if (g_hash_table_lookup (table, id))
1269 g_set_error (error, G_MARKUP_ERROR,
1270 G_MARKUP_ERROR_INVALID_CONTENT,
1271 _("<%s id='%s'> already specified"),
1272 is_flags ? "flags" : "enum", id);
1273 return;
1276 state->enum_state = enum_state_new (is_flags);
1278 my_id = g_strdup (id);
1279 *list = g_slist_prepend (*list, my_id);
1280 g_hash_table_insert (table, my_id, state->enum_state);
1283 /* GMarkup Parser Functions {{{1 */
1285 /* Start element {{{2 */
1286 static void
1287 start_element (GMarkupParseContext *context,
1288 const gchar *element_name,
1289 const gchar **attribute_names,
1290 const gchar **attribute_values,
1291 gpointer user_data,
1292 GError **error)
1294 ParseState *state = user_data;
1295 const GSList *element_stack;
1296 const gchar *container;
1298 element_stack = g_markup_parse_context_get_element_stack (context);
1299 container = element_stack->next ? element_stack->next->data : NULL;
1301 #define COLLECT(first, ...) \
1302 g_markup_collect_attributes (element_name, \
1303 attribute_names, attribute_values, error, \
1304 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1305 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1306 #define STRDUP G_MARKUP_COLLECT_STRDUP
1307 #define STRING G_MARKUP_COLLECT_STRING
1308 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1310 /* Toplevel items {{{3 */
1311 if (container == NULL)
1313 if (strcmp (element_name, "schemalist") == 0)
1315 COLLECT (OPTIONAL | STRDUP,
1316 "gettext-domain",
1317 &state->schemalist_domain);
1318 return;
1323 /* children of <schemalist> {{{3 */
1324 else if (strcmp (container, "schemalist") == 0)
1326 if (strcmp (element_name, "schema") == 0)
1328 const gchar *id, *path, *gettext_domain, *extends, *list_of;
1329 if (COLLECT (STRING, "id", &id,
1330 OPTIONAL | STRING, "path", &path,
1331 OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1332 OPTIONAL | STRING, "extends", &extends,
1333 OPTIONAL | STRING, "list-of", &list_of))
1334 parse_state_start_schema (state, id, path,
1335 gettext_domain ? gettext_domain
1336 : state->schemalist_domain,
1337 extends, list_of, error);
1338 return;
1341 else if (strcmp (element_name, "enum") == 0)
1343 const gchar *id;
1344 if (COLLECT (STRING, "id", &id))
1345 parse_state_start_enum (state, id, FALSE, error);
1346 return;
1349 else if (strcmp (element_name, "flags") == 0)
1351 const gchar *id;
1352 if (COLLECT (STRING, "id", &id))
1353 parse_state_start_enum (state, id, TRUE, error);
1354 return;
1359 /* children of <schema> {{{3 */
1360 else if (strcmp (container, "schema") == 0)
1362 if (strcmp (element_name, "key") == 0)
1364 const gchar *name, *type_string, *enum_type, *flags_type;
1366 if (COLLECT (STRING, "name", &name,
1367 OPTIONAL | STRING, "type", &type_string,
1368 OPTIONAL | STRING, "enum", &enum_type,
1369 OPTIONAL | STRING, "flags", &flags_type))
1371 state->key_state = schema_state_add_key (state->schema_state,
1372 state->enum_table,
1373 state->flags_table,
1374 name, type_string,
1375 enum_type, flags_type,
1376 error);
1377 return;
1379 else if (strcmp (element_name, "child") == 0)
1381 const gchar *name, *schema;
1383 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1384 schema_state_add_child (state->schema_state,
1385 name, schema, error);
1386 return;
1388 else if (strcmp (element_name, "override") == 0)
1390 const gchar *name, *l10n, *context;
1392 if (COLLECT (STRING, "name", &name,
1393 OPTIONAL | STRING, "l10n", &l10n,
1394 OPTIONAL | STRING, "context", &context))
1395 schema_state_add_override (state->schema_state,
1396 &state->key_state, &state->string,
1397 name, l10n, context, error);
1398 return;
1402 /* children of <key> {{{3 */
1403 else if (strcmp (container, "key") == 0)
1405 if (strcmp (element_name, "default") == 0)
1407 const gchar *l10n, *context;
1408 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
1409 STRING | OPTIONAL, "context", &context))
1410 state->string = key_state_start_default (state->key_state,
1411 l10n, context, error);
1412 return;
1415 else if (strcmp (element_name, "summary") == 0)
1417 if (NO_ATTRS ())
1419 if (state->key_state->summary_seen && state->strict)
1420 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1421 _("Only one <%s> element allowed inside <%s>"),
1422 element_name, container);
1423 else
1424 state->string = g_string_new (NULL);
1426 state->key_state->summary_seen = TRUE;
1428 return;
1431 else if (strcmp (element_name, "description") == 0)
1433 if (NO_ATTRS ())
1435 if (state->key_state->description_seen && state->strict)
1436 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1437 _("Only one <%s> element allowed inside <%s>"),
1438 element_name, container);
1439 else
1440 state->string = g_string_new (NULL);
1442 state->key_state->description_seen = TRUE;
1444 return;
1447 else if (strcmp (element_name, "range") == 0)
1449 const gchar *min, *max;
1450 if (COLLECT (STRING | OPTIONAL, "min", &min,
1451 STRING | OPTIONAL, "max", &max))
1452 key_state_set_range (state->key_state, min, max, error);
1453 return;
1456 else if (strcmp (element_name, "choices") == 0)
1458 if (NO_ATTRS ())
1459 key_state_start_choices (state->key_state, error);
1460 return;
1463 else if (strcmp (element_name, "aliases") == 0)
1465 if (NO_ATTRS ())
1466 key_state_start_aliases (state->key_state, error);
1467 return;
1472 /* children of <choices> {{{3 */
1473 else if (strcmp (container, "choices") == 0)
1475 if (strcmp (element_name, "choice") == 0)
1477 const gchar *value;
1478 if (COLLECT (STRING, "value", &value))
1479 key_state_add_choice (state->key_state, value, error);
1480 return;
1485 /* children of <aliases> {{{3 */
1486 else if (strcmp (container, "aliases") == 0)
1488 if (strcmp (element_name, "alias") == 0)
1490 const gchar *value, *target;
1491 if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1492 key_state_add_alias (state->key_state, value, target, error);
1493 return;
1498 /* children of <enum> {{{3 */
1499 else if (strcmp (container, "enum") == 0 ||
1500 strcmp (container, "flags") == 0)
1502 if (strcmp (element_name, "value") == 0)
1504 const gchar *nick, *valuestr;
1505 if (COLLECT (STRING, "nick", &nick,
1506 STRING, "value", &valuestr))
1507 enum_state_add_value (state->enum_state, nick, valuestr, error);
1508 return;
1511 /* 3}}} */
1513 if (container)
1514 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1515 _("Element <%s> not allowed inside <%s>"),
1516 element_name, container);
1517 else
1518 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1519 _("Element <%s> not allowed at the top level"), element_name);
1521 /* 2}}} */
1522 /* End element {{{2 */
1524 static void
1525 key_state_end (KeyState **state_ptr,
1526 GError **error)
1528 KeyState *state;
1530 state = *state_ptr;
1531 *state_ptr = NULL;
1533 if (state->default_value == NULL)
1535 g_set_error_literal (error,
1536 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1537 _("Element <default> is required in <key>"));
1538 return;
1542 static void
1543 schema_state_end (SchemaState **state_ptr,
1544 GError **error)
1546 *state_ptr = NULL;
1549 static void
1550 end_element (GMarkupParseContext *context,
1551 const gchar *element_name,
1552 gpointer user_data,
1553 GError **error)
1555 ParseState *state = user_data;
1557 if (strcmp (element_name, "schemalist") == 0)
1559 g_free (state->schemalist_domain);
1560 state->schemalist_domain = NULL;
1563 else if (strcmp (element_name, "enum") == 0 ||
1564 strcmp (element_name, "flags") == 0)
1565 enum_state_end (&state->enum_state, error);
1567 else if (strcmp (element_name, "schema") == 0)
1568 schema_state_end (&state->schema_state, error);
1570 else if (strcmp (element_name, "override") == 0)
1571 override_state_end (&state->key_state, &state->string, error);
1573 else if (strcmp (element_name, "key") == 0)
1574 key_state_end (&state->key_state, error);
1576 else if (strcmp (element_name, "default") == 0)
1577 key_state_end_default (state->key_state, &state->string, error);
1579 else if (strcmp (element_name, "choices") == 0)
1580 key_state_end_choices (state->key_state, error);
1582 else if (strcmp (element_name, "aliases") == 0)
1583 key_state_end_aliases (state->key_state, error);
1585 if (state->string)
1587 g_string_free (state->string, TRUE);
1588 state->string = NULL;
1591 /* Text {{{2 */
1592 static void
1593 text (GMarkupParseContext *context,
1594 const gchar *text,
1595 gsize text_len,
1596 gpointer user_data,
1597 GError **error)
1599 ParseState *state = user_data;
1601 if (state->string)
1603 /* we are expecting a string, so store the text data.
1605 * we store the data verbatim here and deal with whitespace
1606 * later on. there are two reasons for that:
1608 * 1) whitespace is handled differently depending on the tag
1609 * type.
1611 * 2) we could do leading whitespace removal by refusing to
1612 * insert it into state->string if it's at the start, but for
1613 * trailing whitespace, we have no idea if there is another
1614 * text() call coming or not.
1616 g_string_append_len (state->string, text, text_len);
1618 else
1620 /* string is not expected: accept (and ignore) pure whitespace */
1621 gsize i;
1623 for (i = 0; i < text_len; i++)
1624 if (!g_ascii_isspace (text[i]))
1626 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1627 _("Text may not appear inside <%s>"),
1628 g_markup_parse_context_get_element (context));
1629 break;
1634 /* Write to GVDB {{{1 */
1635 typedef struct
1637 GHashTable *table;
1638 GvdbItem *root;
1639 } GvdbPair;
1641 static void
1642 gvdb_pair_init (GvdbPair *pair)
1644 pair->table = gvdb_hash_table_new (NULL, NULL);
1645 pair->root = gvdb_hash_table_insert (pair->table, "");
1648 static void
1649 gvdb_pair_clear (GvdbPair *pair)
1651 g_hash_table_unref (pair->table);
1654 typedef struct
1656 GHashTable *schema_table;
1657 GvdbPair root_pair;
1658 } WriteToFileData;
1660 typedef struct
1662 GHashTable *schema_table;
1663 GvdbPair pair;
1664 gboolean l10n;
1665 } OutputSchemaData;
1667 static void
1668 output_key (gpointer key,
1669 gpointer value,
1670 gpointer user_data)
1672 OutputSchemaData *data;
1673 const gchar *name;
1674 KeyState *state;
1675 GvdbItem *item;
1676 GVariant *serialised = NULL;
1678 name = key;
1679 state = value;
1680 data = user_data;
1682 item = gvdb_hash_table_insert (data->pair.table, name);
1683 gvdb_item_set_parent (item, data->pair.root);
1684 serialised = key_state_serialise (state);
1685 gvdb_item_set_value (item, serialised);
1686 g_variant_unref (serialised);
1688 if (state->l10n)
1689 data->l10n = TRUE;
1691 if (state->child_schema &&
1692 !g_hash_table_lookup (data->schema_table, state->child_schema))
1694 gchar *message = NULL;
1695 message = g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1696 state->child_schema);
1697 g_printerr ("%s\n", message);
1698 g_free (message);
1702 static void
1703 output_schema (gpointer key,
1704 gpointer value,
1705 gpointer user_data)
1707 WriteToFileData *wtf_data = user_data;
1708 OutputSchemaData data;
1709 GvdbPair *root_pair;
1710 SchemaState *state;
1711 const gchar *id;
1712 GvdbItem *item;
1714 id = key;
1715 state = value;
1716 root_pair = &wtf_data->root_pair;
1718 data.schema_table = wtf_data->schema_table;
1719 gvdb_pair_init (&data.pair);
1720 data.l10n = FALSE;
1722 item = gvdb_hash_table_insert (root_pair->table, id);
1723 gvdb_item_set_parent (item, root_pair->root);
1724 gvdb_item_set_hash_table (item, data.pair.table);
1726 g_hash_table_foreach (state->keys, output_key, &data);
1728 if (state->path)
1729 gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1731 if (state->extends_name)
1732 gvdb_hash_table_insert_string (data.pair.table, ".extends",
1733 state->extends_name);
1735 if (state->list_of)
1736 gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1737 state->list_of);
1739 if (data.l10n)
1740 gvdb_hash_table_insert_string (data.pair.table,
1741 ".gettext-domain",
1742 state->gettext_domain);
1744 gvdb_pair_clear (&data.pair);
1747 static gboolean
1748 write_to_file (GHashTable *schema_table,
1749 const gchar *filename,
1750 GError **error)
1752 WriteToFileData data;
1753 gboolean success;
1755 data.schema_table = schema_table;
1757 gvdb_pair_init (&data.root_pair);
1759 g_hash_table_foreach (schema_table, output_schema, &data);
1761 success = gvdb_table_write_contents (data.root_pair.table, filename,
1762 G_BYTE_ORDER != G_LITTLE_ENDIAN,
1763 error);
1764 g_hash_table_unref (data.root_pair.table);
1766 return success;
1769 /* Parser driver {{{1 */
1770 static GHashTable *
1771 parse_gschema_files (gchar **files,
1772 gboolean strict)
1774 GMarkupParser parser = { start_element, end_element, text };
1775 ParseState state = { 0, };
1776 const gchar *filename;
1777 GError *error = NULL;
1779 state.strict = strict;
1781 state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1782 g_free, enum_state_free);
1784 state.flags_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1785 g_free, enum_state_free);
1787 state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1788 g_free, schema_state_free);
1790 while ((filename = *files++) != NULL)
1792 GMarkupParseContext *context;
1793 gchar *contents;
1794 gsize size;
1795 gint line, col;
1797 if (!g_file_get_contents (filename, &contents, &size, &error))
1799 fprintf (stderr, "%s\n", error->message);
1800 g_clear_error (&error);
1801 continue;
1804 context = g_markup_parse_context_new (&parser,
1805 G_MARKUP_TREAT_CDATA_AS_TEXT |
1806 G_MARKUP_PREFIX_ERROR_POSITION |
1807 G_MARKUP_IGNORE_QUALIFIED,
1808 &state, NULL);
1811 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
1812 !g_markup_parse_context_end_parse (context, &error))
1814 GSList *item;
1816 /* back out any changes from this file */
1817 for (item = state.this_file_schemas; item; item = item->next)
1818 g_hash_table_remove (state.schema_table, item->data);
1820 for (item = state.this_file_flagss; item; item = item->next)
1821 g_hash_table_remove (state.flags_table, item->data);
1823 for (item = state.this_file_enums; item; item = item->next)
1824 g_hash_table_remove (state.enum_table, item->data);
1826 /* let them know */
1827 g_markup_parse_context_get_position (context, &line, &col);
1828 fprintf (stderr, "%s:%d:%d %s. ", filename, line, col, error->message);
1829 g_clear_error (&error);
1831 if (strict)
1833 /* Translators: Do not translate "--strict". */
1834 fprintf (stderr, _("--strict was specified; exiting.\n"));
1835 g_hash_table_unref (state.schema_table);
1836 g_hash_table_unref (state.flags_table);
1837 g_hash_table_unref (state.enum_table);
1839 g_free (contents);
1841 return NULL;
1843 else
1844 fprintf (stderr, _("This entire file has been ignored.\n"));
1847 /* cleanup */
1848 g_free (contents);
1849 g_markup_parse_context_free (context);
1850 g_slist_free (state.this_file_schemas);
1851 g_slist_free (state.this_file_flagss);
1852 g_slist_free (state.this_file_enums);
1853 state.this_file_schemas = NULL;
1854 state.this_file_flagss = NULL;
1855 state.this_file_enums = NULL;
1858 g_hash_table_unref (state.flags_table);
1859 g_hash_table_unref (state.enum_table);
1861 return state.schema_table;
1864 static gint
1865 compare_strings (gconstpointer a,
1866 gconstpointer b)
1868 gchar *one = *(gchar **) a;
1869 gchar *two = *(gchar **) b;
1870 gint cmp;
1872 cmp = g_str_has_suffix (two, ".enums.xml") -
1873 g_str_has_suffix (one, ".enums.xml");
1875 if (!cmp)
1876 cmp = strcmp (one, two);
1878 return cmp;
1881 static gboolean
1882 set_overrides (GHashTable *schema_table,
1883 gchar **files,
1884 gboolean strict)
1886 const gchar *filename;
1887 GError *error = NULL;
1889 while ((filename = *files++))
1891 GKeyFile *key_file;
1892 gchar **groups;
1893 gint i;
1895 g_debug ("Processing override file '%s'", filename);
1897 key_file = g_key_file_new ();
1898 if (!g_key_file_load_from_file (key_file, filename, 0, &error))
1900 fprintf (stderr, "%s: %s. ", filename, error->message);
1901 g_key_file_free (key_file);
1902 g_clear_error (&error);
1904 if (!strict)
1906 fprintf (stderr, _("Ignoring this file.\n"));
1907 continue;
1910 fprintf (stderr, _("--strict was specified; exiting.\n"));
1911 return FALSE;
1914 groups = g_key_file_get_groups (key_file, NULL);
1916 for (i = 0; groups[i]; i++)
1918 const gchar *group = groups[i];
1919 const gchar *schema_name;
1920 const gchar *desktop_id;
1921 SchemaState *schema;
1922 gchar **pieces;
1923 gchar **keys;
1924 gint j;
1926 pieces = g_strsplit (group, ":", 2);
1927 schema_name = pieces[0];
1928 desktop_id = pieces[1];
1930 g_debug ("Processing group '%s' (schema '%s', %s)",
1931 group, schema_name, desktop_id ? desktop_id : "all desktops");
1933 schema = g_hash_table_lookup (schema_table, schema_name);
1935 if (schema == NULL)
1937 /* Having the schema not be installed is expected to be a
1938 * common case. Don't even emit an error message about
1939 * that.
1941 g_strfreev (pieces);
1942 continue;
1945 keys = g_key_file_get_keys (key_file, group, NULL, NULL);
1946 g_assert (keys != NULL);
1948 for (j = 0; keys[j]; j++)
1950 const gchar *key = keys[j];
1951 KeyState *state;
1952 GVariant *value;
1953 gchar *string;
1955 state = g_hash_table_lookup (schema->keys, key);
1957 if (state == NULL)
1959 fprintf (stderr, _("No such key “%s” in schema “%s” as "
1960 "specified in override file “%s”"),
1961 key, group, filename);
1963 if (!strict)
1965 fprintf (stderr, _("; ignoring override for this key.\n"));
1966 continue;
1969 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1970 g_key_file_free (key_file);
1971 g_strfreev (pieces);
1972 g_strfreev (groups);
1973 g_strfreev (keys);
1975 return FALSE;
1978 if (desktop_id != NULL && state->l10n)
1980 /* Let's avoid the n*m case of per-desktop localised
1981 * default values, and just forbid it.
1983 fprintf (stderr,
1984 _("cannot provide per-desktop overrides for localised "
1985 "key “%s” in schema “%s” (override file “%s”)"),
1986 key, group, filename);
1988 if (!strict)
1990 fprintf (stderr, _("; ignoring override for this key.\n"));
1991 continue;
1994 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
1995 g_key_file_free (key_file);
1996 g_strfreev (pieces);
1997 g_strfreev (groups);
1998 g_strfreev (keys);
2000 return FALSE;
2003 string = g_key_file_get_value (key_file, group, key, NULL);
2004 g_assert (string != NULL);
2006 value = g_variant_parse (state->type, string,
2007 NULL, NULL, &error);
2009 if (value == NULL)
2011 fprintf (stderr, _("error parsing key “%s” in schema “%s” "
2012 "as specified in override file “%s”: "
2013 "%s."),
2014 key, group, filename, error->message);
2016 g_clear_error (&error);
2017 g_free (string);
2019 if (!strict)
2021 fprintf (stderr, _("Ignoring override for this key.\n"));
2022 continue;
2025 fprintf (stderr, _("--strict was specified; exiting.\n"));
2026 g_key_file_free (key_file);
2027 g_strfreev (pieces);
2028 g_strfreev (groups);
2029 g_strfreev (keys);
2031 return FALSE;
2034 if (state->minimum)
2036 if (g_variant_compare (value, state->minimum) < 0 ||
2037 g_variant_compare (value, state->maximum) > 0)
2039 fprintf (stderr,
2040 _("override for key “%s” in schema “%s” in "
2041 "override file “%s” is outside the range "
2042 "given in the schema"),
2043 key, group, filename);
2045 g_variant_unref (value);
2046 g_free (string);
2048 if (!strict)
2050 fprintf (stderr, _("; ignoring override for this key.\n"));
2051 continue;
2054 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2055 g_key_file_free (key_file);
2056 g_strfreev (pieces);
2057 g_strfreev (groups);
2058 g_strfreev (keys);
2060 return FALSE;
2064 else if (state->strinfo->len)
2066 if (!is_valid_choices (value, state->strinfo))
2068 fprintf (stderr,
2069 _("override for key “%s” in schema “%s” in "
2070 "override file “%s” is not in the list "
2071 "of valid choices"),
2072 key, group, filename);
2074 g_variant_unref (value);
2075 g_free (string);
2077 if (!strict)
2079 fprintf (stderr, _("; ignoring override for this key.\n"));
2080 continue;
2083 fprintf (stderr, _(" and --strict was specified; exiting.\n"));
2084 g_key_file_free (key_file);
2085 g_strfreev (pieces);
2086 g_strfreev (groups);
2087 g_strfreev (keys);
2089 return FALSE;
2093 if (desktop_id != NULL)
2095 if (state->desktop_overrides == NULL)
2096 state->desktop_overrides = g_variant_dict_new (NULL);
2098 g_variant_dict_insert_value (state->desktop_overrides, desktop_id, value);
2099 g_variant_unref (value);
2101 else
2103 g_variant_unref (state->default_value);
2104 state->default_value = value;
2107 g_free (string);
2110 g_strfreev (pieces);
2111 g_strfreev (keys);
2114 g_strfreev (groups);
2117 return TRUE;
2121 main (int argc, char **argv)
2123 GError *error = NULL;
2124 GHashTable *table = NULL;
2125 GDir *dir = NULL;
2126 const gchar *file;
2127 const gchar *srcdir;
2128 gboolean show_version_and_exit = FALSE;
2129 gchar *targetdir = NULL;
2130 gchar *target = NULL;
2131 gboolean dry_run = FALSE;
2132 gboolean strict = FALSE;
2133 gchar **schema_files = NULL;
2134 gchar **override_files = NULL;
2135 GOptionContext *context = NULL;
2136 gint retval;
2137 GOptionEntry entries[] = {
2138 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
2139 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2140 { "strict", 0, 0, G_OPTION_ARG_NONE, &strict, N_("Abort on any errors in schemas"), NULL },
2141 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
2142 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
2144 /* These options are only for use in the gschema-compile tests */
2145 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
2146 { "override-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &override_files, NULL, NULL },
2147 { NULL }
2150 #ifdef G_OS_WIN32
2151 gchar *tmp = NULL;
2152 #endif
2154 setlocale (LC_ALL, "");
2155 textdomain (GETTEXT_PACKAGE);
2157 #ifdef G_OS_WIN32
2158 tmp = _glib_get_locale_dir ();
2159 bindtextdomain (GETTEXT_PACKAGE, tmp);
2160 #else
2161 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2162 #endif
2164 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2165 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2166 #endif
2168 context = g_option_context_new (N_("DIRECTORY"));
2169 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
2170 g_option_context_set_summary (context,
2171 N_("Compile all GSettings schema files into a schema cache.\n"
2172 "Schema files are required to have the extension .gschema.xml,\n"
2173 "and the cache file is called gschemas.compiled."));
2174 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
2176 if (!g_option_context_parse (context, &argc, &argv, &error))
2178 fprintf (stderr, "%s\n", error->message);
2179 retval = 1;
2180 goto done;
2183 if (show_version_and_exit)
2185 g_print (PACKAGE_VERSION "\n");
2186 retval = 0;
2187 goto done;
2190 if (!schema_files && argc != 2)
2192 fprintf (stderr, _("You should give exactly one directory name\n"));
2193 retval = 1;
2194 goto done;
2197 srcdir = argv[1];
2199 target = g_build_filename (targetdir ? targetdir : srcdir, "gschemas.compiled", NULL);
2201 if (!schema_files)
2203 GPtrArray *overrides;
2204 GPtrArray *files;
2206 files = g_ptr_array_new ();
2207 overrides = g_ptr_array_new ();
2209 dir = g_dir_open (srcdir, 0, &error);
2210 if (dir == NULL)
2212 fprintf (stderr, "%s\n", error->message);
2214 g_ptr_array_unref (files);
2215 g_ptr_array_unref (overrides);
2217 retval = 1;
2218 goto done;
2221 while ((file = g_dir_read_name (dir)) != NULL)
2223 if (g_str_has_suffix (file, ".gschema.xml") ||
2224 g_str_has_suffix (file, ".enums.xml"))
2225 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
2227 else if (g_str_has_suffix (file, ".gschema.override"))
2228 g_ptr_array_add (overrides,
2229 g_build_filename (srcdir, file, NULL));
2232 if (files->len == 0)
2234 fprintf (stdout, _("No schema files found: "));
2236 if (g_unlink (target))
2237 fprintf (stdout, _("doing nothing.\n"));
2239 else
2240 fprintf (stdout, _("removed existing output file.\n"));
2242 g_ptr_array_unref (files);
2243 g_ptr_array_unref (overrides);
2245 retval = 0;
2246 goto done;
2248 g_ptr_array_sort (files, compare_strings);
2249 g_ptr_array_add (files, NULL);
2251 g_ptr_array_sort (overrides, compare_strings);
2252 g_ptr_array_add (overrides, NULL);
2254 schema_files = (char **) g_ptr_array_free (files, FALSE);
2255 override_files = (gchar **) g_ptr_array_free (overrides, FALSE);
2258 if ((table = parse_gschema_files (schema_files, strict)) == NULL)
2260 retval = 1;
2261 goto done;
2264 if (override_files != NULL &&
2265 !set_overrides (table, override_files, strict))
2267 retval = 1;
2268 goto done;
2271 if (!dry_run && !write_to_file (table, target, &error))
2273 fprintf (stderr, "%s\n", error->message);
2274 retval = 1;
2275 goto done;
2278 /* Success. */
2279 retval = 0;
2281 done:
2282 g_clear_error (&error);
2283 g_clear_pointer (&table, g_hash_table_unref);
2284 g_clear_pointer (&dir, g_dir_close);
2285 g_free (targetdir);
2286 g_free (target);
2287 g_strfreev (schema_files);
2288 g_strfreev (override_files);
2289 g_option_context_free (context);
2291 #ifdef G_OS_WIN32
2292 g_free (tmp);
2293 #endif
2295 return retval;
2298 /* Epilogue {{{1 */
2300 /* vim:set foldmethod=marker: */