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>
30 #include "gvdb/gvdb-builder.h"
34 #include "glib/glib-private.h"
38 strip_string (GString
*string
)
42 for (i
= 0; g_ascii_isspace (string
->str
[i
]); i
++);
43 g_string_erase (string
, 0, i
);
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 */
62 enum_state_free (gpointer data
)
64 EnumState
*state
= data
;
66 g_string_free (state
->strinfo
, TRUE
);
67 g_slice_free (EnumState
, state
);
71 enum_state_new (gboolean is_flags
)
75 state
= g_slice_new (EnumState
);
76 state
->strinfo
= g_string_new (NULL
);
77 state
->is_flags
= is_flags
;
83 enum_state_add_value (EnumState
*state
,
85 const gchar
*valuestr
,
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"));
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"));
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
);
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
);
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)
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"));
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
);
150 enum_state_end (EnumState
**state_ptr
,
158 if (state
->strinfo
->len
== 0)
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 */
168 /* for <child>, @child_schema will be set.
169 * for <key>, everything else will be set.
175 gboolean have_gettext_domain
;
179 GString
*unparsed_default_value
;
180 GVariant
*default_value
;
189 gboolean has_choices
;
190 gboolean has_aliases
;
191 gboolean is_override
;
194 GVariant
*serialised
;
196 gboolean summary_seen
;
197 gboolean description_seen
;
201 key_state_new (const gchar
*type_string
,
202 const gchar
*gettext_domain
,
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
;
218 state
->strinfo
= g_string_new_len (strinfo
->str
, strinfo
->len
);
220 state
->strinfo
= g_string_new (NULL
);
226 key_state_override (KeyState
*state
,
227 const gchar
*gettext_domain
)
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
;
242 copy
->minimum
= g_variant_ref (state
->minimum
);
243 copy
->maximum
= g_variant_ref (state
->maximum
);
250 key_state_new_child (const gchar
*child_schema
)
254 state
= g_slice_new0 (KeyState
);
255 state
->child_schema
= g_strdup (child_schema
);
261 is_valid_choices (GVariant
*variant
,
264 switch (g_variant_classify (variant
))
266 case G_VARIANT_CLASS_MAYBE
:
267 case G_VARIANT_CLASS_ARRAY
:
269 gboolean valid
= TRUE
;
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
);
283 case G_VARIANT_CLASS_STRING
:
284 return strinfo_is_string_valid ((const guint32
*) strinfo
->str
,
286 g_variant_get_string (variant
, NULL
));
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.
299 key_state_check_range (KeyState
*state
,
302 if (state
->default_value
)
306 tag
= state
->is_override
? "override" : "default";
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
))
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
);
337 g_set_error (error
, G_MARKUP_ERROR
,
338 G_MARKUP_ERROR_INVALID_CONTENT
,
339 _("<%s> contains a string not in "
347 key_state_set_range (KeyState
*state
,
348 const gchar
*min_str
,
349 const gchar
*max_str
,
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
;
371 g_set_error_literal (error
, G_MARKUP_ERROR
,
372 G_MARKUP_ERROR_INVALID_CONTENT
,
373 _("<range/> already specified for this key"));
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
;
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
);
396 state
->minimum
= g_variant_parse (state
->type
, min_str
, NULL
, NULL
, error
);
397 if (state
->minimum
== NULL
)
400 state
->maximum
= g_variant_parse (state
->type
, max_str
, NULL
, NULL
, error
);
401 if (state
->maximum
== NULL
)
404 if (g_variant_compare (state
->minimum
, state
->maximum
) > 0)
406 g_set_error (error
, G_MARKUP_ERROR
,
407 G_MARKUP_ERROR_INVALID_CONTENT
,
408 _("<range> specified minimum is greater than maximum"));
412 key_state_check_range (state
, error
);
416 key_state_start_default (KeyState
*state
,
418 const gchar
*context
,
423 if (strcmp (l10n
, "messages") == 0)
426 else if (strcmp (l10n
, "time") == 0)
431 g_set_error (error
, G_MARKUP_ERROR
,
432 G_MARKUP_ERROR_INVALID_CONTENT
,
433 _("unsupported l10n category: %s"), l10n
);
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"));
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"));
458 return g_string_new (NULL
);
462 key_state_end_default (KeyState
*state
,
466 state
->unparsed_default_value
= *string
;
469 state
->default_value
= g_variant_parse (state
->type
,
470 state
->unparsed_default_value
->str
,
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
);
479 key_state_check_range (state
, error
);
483 key_state_start_choices (KeyState
*state
,
486 const GVariantType
*type
= state
->type
;
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"));
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"));
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”"),
515 g_free (type_string
);
521 key_state_add_choice (KeyState
*state
,
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
);
533 strinfo_builder_append_item (state
->strinfo
, choice
, 0);
534 state
->has_choices
= TRUE
;
538 key_state_end_choices (KeyState
*state
,
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>"));
548 key_state_check_range (state
, error
);
552 key_state_start_aliases (KeyState
*state
,
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>"));
567 key_state_add_alias (KeyState
*state
,
572 if (strinfo_builder_contains (state
->strinfo
, alias
))
574 if (strinfo_is_string_valid ((guint32
*) state
->strinfo
->str
,
575 state
->strinfo
->len
/ 4,
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
);
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"),
593 g_set_error (error
, G_MARKUP_ERROR
,
594 G_MARKUP_ERROR_INVALID_CONTENT
,
595 _("<alias value='%s'/> already specified"), alias
);
600 if (!strinfo_builder_append_alias (state
->strinfo
, alias
, target
))
602 g_set_error (error
, G_MARKUP_ERROR
,
603 G_MARKUP_ERROR_INVALID_CONTENT
,
605 _("alias target “%s” is not in enumerated type") :
606 _("alias target “%s” is not in <choices>"),
611 state
->has_aliases
= TRUE
;
615 key_state_end_aliases (KeyState
*state
,
618 if (!state
->has_aliases
)
620 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
621 _("<aliases> must contain at least one <alias>"));
627 key_state_check (KeyState
*state
,
633 return state
->checked
= TRUE
;
637 key_state_serialise (KeyState
*state
)
639 if (state
->serialised
== NULL
)
641 if (state
->child_schema
)
643 state
->serialised
= g_variant_new_string (state
->child_schema
);
648 GVariantBuilder builder
;
650 g_assert (key_state_check (state
, NULL
));
652 g_variant_builder_init (&builder
, G_VARIANT_TYPE_TUPLE
);
655 g_variant_builder_add_value (&builder
, state
->default_value
);
660 /* We are going to store the untranslated default for
661 * runtime translation according to the current locale.
662 * We need to strip leading and trailing whitespace from
663 * the string so that it's exactly the same as the one
664 * that ended up in the .po file for translation.
666 * We want to do this so that
668 * <default l10n='messages'>
672 * ends up in the .po file like "['a', 'b', 'c']",
673 * omitting the extra whitespace at the start and end.
675 strip_string (state
->unparsed_default_value
);
677 if (state
->l10n_context
)
681 /* Contextified messages are supported by prepending
682 * the context, followed by '\004' to the start of the
683 * message string. We do that here to save GSettings
686 len
= strlen (state
->l10n_context
);
687 state
->l10n_context
[len
] = '\004';
688 g_string_prepend_len (state
->unparsed_default_value
,
689 state
->l10n_context
, len
+ 1);
690 g_free (state
->l10n_context
);
691 state
->l10n_context
= NULL
;
694 g_variant_builder_add (&builder
, "(y(y&s))", 'l', state
->l10n
,
695 state
->unparsed_default_value
->str
);
696 g_string_free (state
->unparsed_default_value
, TRUE
);
697 state
->unparsed_default_value
= NULL
;
700 /* choice, aliases, enums */
701 if (state
->strinfo
->len
)
709 data
= state
->strinfo
->str
;
710 size
= state
->strinfo
->len
;
713 for (i
= 0; i
< size
/ sizeof (guint32
); i
++)
714 words
[i
] = GUINT32_TO_LE (words
[i
]);
716 array
= g_variant_new_from_data (G_VARIANT_TYPE ("au"),
720 g_string_free (state
->strinfo
, FALSE
);
721 state
->strinfo
= NULL
;
723 g_variant_builder_add (&builder
, "(y@au)",
724 state
->is_flags
? 'f' :
725 state
->is_enum
? 'e' : 'c',
730 if (state
->minimum
|| state
->maximum
)
731 g_variant_builder_add (&builder
, "(y(**))", 'r',
732 state
->minimum
, state
->maximum
);
734 state
->serialised
= g_variant_builder_end (&builder
);
737 g_variant_ref_sink (state
->serialised
);
740 return g_variant_ref (state
->serialised
);
744 key_state_free (gpointer data
)
746 KeyState
*state
= data
;
748 g_free (state
->child_schema
);
751 g_variant_type_free (state
->type
);
753 g_free (state
->l10n_context
);
755 if (state
->unparsed_default_value
)
756 g_string_free (state
->unparsed_default_value
, TRUE
);
758 if (state
->default_value
)
759 g_variant_unref (state
->default_value
);
762 g_string_free (state
->strinfo
, TRUE
);
765 g_variant_unref (state
->minimum
);
768 g_variant_unref (state
->maximum
);
770 if (state
->serialised
)
771 g_variant_unref (state
->serialised
);
773 g_slice_free (KeyState
, state
);
776 /* Key name validity {{{1 */
777 static gboolean allow_any_name
= FALSE
;
780 is_valid_keyname (const gchar
*key
,
787 g_set_error_literal (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
788 _("Empty names are not permitted"));
795 if (!g_ascii_islower (key
[0]))
797 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
798 _("Invalid name “%s”: names must begin "
799 "with a lowercase letter"), key
);
803 for (i
= 1; key
[i
]; i
++)
806 !g_ascii_islower (key
[i
]) &&
807 !g_ascii_isdigit (key
[i
]))
809 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
810 _("Invalid name “%s”: invalid character “%c”; "
811 "only lowercase letters, numbers and hyphen (“-”) "
812 "are permitted"), key
, key
[i
]);
816 if (key
[i
] == '-' && key
[i
+ 1] == '-')
818 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
819 _("Invalid name “%s”: two successive hyphens (“--”) "
820 "are not permitted"), key
);
825 if (key
[i
- 1] == '-')
827 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
828 _("Invalid name “%s”: the last character may not be a "
829 "hyphen (“-”)"), key
);
835 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
836 _("Invalid name “%s”: maximum length is 1024"), key
);
843 /* Handling of <schema> {{{1 */
844 typedef struct _SchemaState SchemaState
;
847 SchemaState
*extends
;
850 gchar
*gettext_domain
;
858 schema_state_new (const gchar
*path
,
859 const gchar
*gettext_domain
,
860 SchemaState
*extends
,
861 const gchar
*extends_name
,
862 const gchar
*list_of
)
866 state
= g_slice_new (SchemaState
);
867 state
->path
= g_strdup (path
);
868 state
->gettext_domain
= g_strdup (gettext_domain
);
869 state
->extends
= extends
;
870 state
->extends_name
= g_strdup (extends_name
);
871 state
->list_of
= g_strdup (list_of
);
872 state
->keys
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
873 g_free
, key_state_free
);
879 schema_state_free (gpointer data
)
881 SchemaState
*state
= data
;
883 g_free (state
->path
);
884 g_free (state
->gettext_domain
);
885 g_free (state
->extends_name
);
886 g_free (state
->list_of
);
887 g_hash_table_unref (state
->keys
);
888 g_slice_free (SchemaState
, state
);
892 schema_state_add_child (SchemaState
*state
,
899 if (!is_valid_keyname (name
, error
))
902 childname
= g_strconcat (name
, "/", NULL
);
904 if (g_hash_table_lookup (state
->keys
, childname
))
906 g_set_error (error
, G_MARKUP_ERROR
,
907 G_MARKUP_ERROR_INVALID_CONTENT
,
908 _("<child name='%s'> already specified"), name
);
912 g_hash_table_insert (state
->keys
, childname
,
913 key_state_new_child (schema
));
917 schema_state_add_key (SchemaState
*state
,
918 GHashTable
*enum_table
,
919 GHashTable
*flags_table
,
921 const gchar
*type_string
,
922 const gchar
*enum_type
,
923 const gchar
*flags_type
,
932 g_set_error_literal (error
, G_MARKUP_ERROR
,
933 G_MARKUP_ERROR_INVALID_CONTENT
,
934 _("Cannot add keys to a “list-of” schema"));
938 if (!is_valid_keyname (name
, error
))
941 if (g_hash_table_lookup (state
->keys
, name
))
943 g_set_error (error
, G_MARKUP_ERROR
,
944 G_MARKUP_ERROR_INVALID_CONTENT
,
945 _("<key name='%s'> already specified"), name
);
949 for (node
= state
; node
; node
= node
->extends
)
954 shadow
= g_hash_table_lookup (node
->extends
->keys
, name
);
956 /* in case of <key> <override> <key> make sure we report the
957 * location of the original <key>, not the <override>.
959 if (shadow
&& !shadow
->is_override
)
961 g_set_error (error
, G_MARKUP_ERROR
,
962 G_MARKUP_ERROR_INVALID_CONTENT
,
963 _("<key name='%s'> shadows <key name='%s'> in "
964 "<schema id='%s'>; use <override> to modify value"),
965 name
, name
, node
->extends_name
);
970 if ((type_string
!= NULL
) + (enum_type
!= NULL
) + (flags_type
!= NULL
) != 1)
972 g_set_error (error
, G_MARKUP_ERROR
,
973 G_MARKUP_ERROR_MISSING_ATTRIBUTE
,
974 _("Exactly one of “type”, “enum” or “flags” must "
975 "be specified as an attribute to <key>"));
979 if (type_string
== NULL
) /* flags or enums was specified */
981 EnumState
*enum_state
;
984 enum_state
= g_hash_table_lookup (enum_table
, enum_type
);
986 enum_state
= g_hash_table_lookup (flags_table
, flags_type
);
989 if (enum_state
== NULL
)
991 g_set_error (error
, G_MARKUP_ERROR
,
992 G_MARKUP_ERROR_INVALID_CONTENT
,
993 _("<%s id='%s'> not (yet) defined."),
994 flags_type
? "flags" : "enum",
995 flags_type
? flags_type
: enum_type
);
999 type_string
= flags_type
? "as" : "s";
1000 strinfo
= enum_state
->strinfo
;
1004 if (!g_variant_type_string_is_valid (type_string
))
1006 g_set_error (error
, G_MARKUP_ERROR
,
1007 G_MARKUP_ERROR_INVALID_CONTENT
,
1008 _("Invalid GVariant type string “%s”"), type_string
);
1015 key
= key_state_new (type_string
, state
->gettext_domain
,
1016 enum_type
!= NULL
, flags_type
!= NULL
, strinfo
);
1017 g_hash_table_insert (state
->keys
, g_strdup (name
), key
);
1023 schema_state_add_override (SchemaState
*state
,
1024 KeyState
**key_state
,
1028 const gchar
*context
,
1031 SchemaState
*parent
;
1034 if (state
->extends
== NULL
)
1036 g_set_error_literal (error
, G_MARKUP_ERROR
,
1037 G_MARKUP_ERROR_INVALID_CONTENT
,
1038 _("<override> given but schema isn’t "
1039 "extending anything"));
1043 for (parent
= state
->extends
; parent
; parent
= parent
->extends
)
1044 if ((original
= g_hash_table_lookup (parent
->keys
, key
)))
1047 if (original
== NULL
)
1049 g_set_error (error
, G_MARKUP_ERROR
,
1050 G_MARKUP_ERROR_INVALID_CONTENT
,
1051 _("No <key name='%s'> to override"), key
);
1055 if (g_hash_table_lookup (state
->keys
, key
))
1057 g_set_error (error
, G_MARKUP_ERROR
,
1058 G_MARKUP_ERROR_INVALID_CONTENT
,
1059 _("<override name='%s'> already specified"), key
);
1063 *key_state
= key_state_override (original
, state
->gettext_domain
);
1064 *string
= key_state_start_default (*key_state
, l10n
, context
, error
);
1065 g_hash_table_insert (state
->keys
, g_strdup (key
), *key_state
);
1069 override_state_end (KeyState
**key_state
,
1073 key_state_end_default (*key_state
, string
, error
);
1077 /* Handling of toplevel state {{{1 */
1080 gboolean strict
; /* TRUE if --strict was given */
1082 GHashTable
*schema_table
; /* string -> SchemaState */
1083 GHashTable
*flags_table
; /* string -> EnumState */
1084 GHashTable
*enum_table
; /* string -> EnumState */
1086 GSList
*this_file_schemas
; /* strings: <schema>s in this file */
1087 GSList
*this_file_flagss
; /* strings: <flags>s in this file */
1088 GSList
*this_file_enums
; /* strings: <enum>s in this file */
1090 gchar
*schemalist_domain
; /* the <schemalist> gettext domain */
1092 SchemaState
*schema_state
; /* non-NULL when inside <schema> */
1093 KeyState
*key_state
; /* non-NULL when inside <key> */
1094 EnumState
*enum_state
; /* non-NULL when inside <enum> */
1096 GString
*string
; /* non-NULL when accepting text */
1100 is_subclass (const gchar
*class_name
,
1101 const gchar
*possible_parent
,
1102 GHashTable
*schema_table
)
1106 if (strcmp (class_name
, possible_parent
) == 0)
1109 class = g_hash_table_lookup (schema_table
, class_name
);
1110 g_assert (class != NULL
);
1112 return class->extends_name
&&
1113 is_subclass (class->extends_name
, possible_parent
, schema_table
);
1117 parse_state_start_schema (ParseState
*state
,
1120 const gchar
*gettext_domain
,
1121 const gchar
*extends_name
,
1122 const gchar
*list_of
,
1125 SchemaState
*extends
;
1128 if (g_hash_table_lookup (state
->schema_table
, id
))
1130 g_set_error (error
, G_MARKUP_ERROR
,
1131 G_MARKUP_ERROR_INVALID_CONTENT
,
1132 _("<schema id='%s'> already specified"), id
);
1138 extends
= g_hash_table_lookup (state
->schema_table
, extends_name
);
1140 if (extends
== NULL
)
1142 g_set_error (error
, G_MARKUP_ERROR
,
1143 G_MARKUP_ERROR_INVALID_CONTENT
,
1144 _("<schema id='%s'> extends not yet existing "
1145 "schema “%s”"), id
, extends_name
);
1156 if (!(tmp
= g_hash_table_lookup (state
->schema_table
, list_of
)))
1158 g_set_error (error
, G_MARKUP_ERROR
,
1159 G_MARKUP_ERROR_INVALID_CONTENT
,
1160 _("<schema id='%s'> is list of not yet existing "
1161 "schema “%s”"), id
, list_of
);
1167 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1168 _("Cannot be a list of a schema with a path"));
1177 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1178 _("Cannot extend a schema with a path"));
1184 if (extends
->list_of
== NULL
)
1186 g_set_error (error
, G_MARKUP_ERROR
,
1187 G_MARKUP_ERROR_INVALID_CONTENT
,
1188 _("<schema id='%s'> is a list, extending "
1189 "<schema id='%s'> which is not a list"),
1194 if (!is_subclass (list_of
, extends
->list_of
, state
->schema_table
))
1196 g_set_error (error
, G_MARKUP_ERROR
,
1197 G_MARKUP_ERROR_INVALID_CONTENT
,
1198 _("<schema id='%s' list-of='%s'> extends <schema "
1199 "id='%s' list-of='%s'> but “%s” does not "
1200 "extend “%s”"), id
, list_of
, extends_name
,
1201 extends
->list_of
, list_of
, extends
->list_of
);
1206 /* by default we are a list of the same thing that the schema
1207 * we are extending is a list of (which might be nothing)
1209 list_of
= extends
->list_of
;
1212 if (path
&& !(g_str_has_prefix (path
, "/") && g_str_has_suffix (path
, "/")))
1214 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1215 _("A path, if given, must begin and end with a slash"));
1219 if (path
&& list_of
&& !g_str_has_suffix (path
, ":/"))
1221 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1222 _("The path of a list must end with “:/”"));
1226 if (path
&& (g_str_has_prefix (path
, "/apps/") ||
1227 g_str_has_prefix (path
, "/desktop/") ||
1228 g_str_has_prefix (path
, "/system/")))
1230 gchar
*message
= NULL
;
1231 message
= g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1232 "Paths starting with "
1233 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1235 g_printerr ("%s\n", message
);
1239 state
->schema_state
= schema_state_new (path
, gettext_domain
,
1240 extends
, extends_name
, list_of
);
1242 my_id
= g_strdup (id
);
1243 state
->this_file_schemas
= g_slist_prepend (state
->this_file_schemas
, my_id
);
1244 g_hash_table_insert (state
->schema_table
, my_id
, state
->schema_state
);
1248 parse_state_start_enum (ParseState
*state
,
1253 GSList
**list
= is_flags
? &state
->this_file_flagss
: &state
->this_file_enums
;
1254 GHashTable
*table
= is_flags
? state
->flags_table
: state
->enum_table
;
1257 if (g_hash_table_lookup (table
, id
))
1259 g_set_error (error
, G_MARKUP_ERROR
,
1260 G_MARKUP_ERROR_INVALID_CONTENT
,
1261 _("<%s id='%s'> already specified"),
1262 is_flags
? "flags" : "enum", id
);
1266 state
->enum_state
= enum_state_new (is_flags
);
1268 my_id
= g_strdup (id
);
1269 *list
= g_slist_prepend (*list
, my_id
);
1270 g_hash_table_insert (table
, my_id
, state
->enum_state
);
1273 /* GMarkup Parser Functions {{{1 */
1275 /* Start element {{{2 */
1277 start_element (GMarkupParseContext
*context
,
1278 const gchar
*element_name
,
1279 const gchar
**attribute_names
,
1280 const gchar
**attribute_values
,
1284 ParseState
*state
= user_data
;
1285 const GSList
*element_stack
;
1286 const gchar
*container
;
1288 element_stack
= g_markup_parse_context_get_element_stack (context
);
1289 container
= element_stack
->next
? element_stack
->next
->data
: NULL
;
1291 #define COLLECT(first, ...) \
1292 g_markup_collect_attributes (element_name, \
1293 attribute_names, attribute_values, error, \
1294 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1295 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1296 #define STRDUP G_MARKUP_COLLECT_STRDUP
1297 #define STRING G_MARKUP_COLLECT_STRING
1298 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1300 /* Toplevel items {{{3 */
1301 if (container
== NULL
)
1303 if (strcmp (element_name
, "schemalist") == 0)
1305 COLLECT (OPTIONAL
| STRDUP
,
1307 &state
->schemalist_domain
);
1313 /* children of <schemalist> {{{3 */
1314 else if (strcmp (container
, "schemalist") == 0)
1316 if (strcmp (element_name
, "schema") == 0)
1318 const gchar
*id
, *path
, *gettext_domain
, *extends
, *list_of
;
1319 if (COLLECT (STRING
, "id", &id
,
1320 OPTIONAL
| STRING
, "path", &path
,
1321 OPTIONAL
| STRING
, "gettext-domain", &gettext_domain
,
1322 OPTIONAL
| STRING
, "extends", &extends
,
1323 OPTIONAL
| STRING
, "list-of", &list_of
))
1324 parse_state_start_schema (state
, id
, path
,
1325 gettext_domain
? gettext_domain
1326 : state
->schemalist_domain
,
1327 extends
, list_of
, error
);
1331 else if (strcmp (element_name
, "enum") == 0)
1334 if (COLLECT (STRING
, "id", &id
))
1335 parse_state_start_enum (state
, id
, FALSE
, error
);
1339 else if (strcmp (element_name
, "flags") == 0)
1342 if (COLLECT (STRING
, "id", &id
))
1343 parse_state_start_enum (state
, id
, TRUE
, error
);
1349 /* children of <schema> {{{3 */
1350 else if (strcmp (container
, "schema") == 0)
1352 if (strcmp (element_name
, "key") == 0)
1354 const gchar
*name
, *type_string
, *enum_type
, *flags_type
;
1356 if (COLLECT (STRING
, "name", &name
,
1357 OPTIONAL
| STRING
, "type", &type_string
,
1358 OPTIONAL
| STRING
, "enum", &enum_type
,
1359 OPTIONAL
| STRING
, "flags", &flags_type
))
1361 state
->key_state
= schema_state_add_key (state
->schema_state
,
1365 enum_type
, flags_type
,
1369 else if (strcmp (element_name
, "child") == 0)
1371 const gchar
*name
, *schema
;
1373 if (COLLECT (STRING
, "name", &name
, STRING
, "schema", &schema
))
1374 schema_state_add_child (state
->schema_state
,
1375 name
, schema
, error
);
1378 else if (strcmp (element_name
, "override") == 0)
1380 const gchar
*name
, *l10n
, *context
;
1382 if (COLLECT (STRING
, "name", &name
,
1383 OPTIONAL
| STRING
, "l10n", &l10n
,
1384 OPTIONAL
| STRING
, "context", &context
))
1385 schema_state_add_override (state
->schema_state
,
1386 &state
->key_state
, &state
->string
,
1387 name
, l10n
, context
, error
);
1392 /* children of <key> {{{3 */
1393 else if (strcmp (container
, "key") == 0)
1395 if (strcmp (element_name
, "default") == 0)
1397 const gchar
*l10n
, *context
;
1398 if (COLLECT (STRING
| OPTIONAL
, "l10n", &l10n
,
1399 STRING
| OPTIONAL
, "context", &context
))
1400 state
->string
= key_state_start_default (state
->key_state
,
1401 l10n
, context
, error
);
1405 else if (strcmp (element_name
, "summary") == 0)
1409 if (state
->key_state
->summary_seen
&& state
->strict
)
1410 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1411 _("Only one <%s> element allowed inside <%s>"),
1412 element_name
, container
);
1414 state
->string
= g_string_new (NULL
);
1416 state
->key_state
->summary_seen
= TRUE
;
1421 else if (strcmp (element_name
, "description") == 0)
1425 if (state
->key_state
->description_seen
&& state
->strict
)
1426 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1427 _("Only one <%s> element allowed inside <%s>"),
1428 element_name
, container
);
1430 state
->string
= g_string_new (NULL
);
1432 state
->key_state
->description_seen
= TRUE
;
1437 else if (strcmp (element_name
, "range") == 0)
1439 const gchar
*min
, *max
;
1440 if (COLLECT (STRING
| OPTIONAL
, "min", &min
,
1441 STRING
| OPTIONAL
, "max", &max
))
1442 key_state_set_range (state
->key_state
, min
, max
, error
);
1446 else if (strcmp (element_name
, "choices") == 0)
1449 key_state_start_choices (state
->key_state
, error
);
1453 else if (strcmp (element_name
, "aliases") == 0)
1456 key_state_start_aliases (state
->key_state
, error
);
1462 /* children of <choices> {{{3 */
1463 else if (strcmp (container
, "choices") == 0)
1465 if (strcmp (element_name
, "choice") == 0)
1468 if (COLLECT (STRING
, "value", &value
))
1469 key_state_add_choice (state
->key_state
, value
, error
);
1475 /* children of <aliases> {{{3 */
1476 else if (strcmp (container
, "aliases") == 0)
1478 if (strcmp (element_name
, "alias") == 0)
1480 const gchar
*value
, *target
;
1481 if (COLLECT (STRING
, "value", &value
, STRING
, "target", &target
))
1482 key_state_add_alias (state
->key_state
, value
, target
, error
);
1488 /* children of <enum> {{{3 */
1489 else if (strcmp (container
, "enum") == 0 ||
1490 strcmp (container
, "flags") == 0)
1492 if (strcmp (element_name
, "value") == 0)
1494 const gchar
*nick
, *valuestr
;
1495 if (COLLECT (STRING
, "nick", &nick
,
1496 STRING
, "value", &valuestr
))
1497 enum_state_add_value (state
->enum_state
, nick
, valuestr
, error
);
1504 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1505 _("Element <%s> not allowed inside <%s>"),
1506 element_name
, container
);
1508 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1509 _("Element <%s> not allowed at the top level"), element_name
);
1512 /* End element {{{2 */
1515 key_state_end (KeyState
**state_ptr
,
1523 if (state
->default_value
== NULL
)
1525 g_set_error_literal (error
,
1526 G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1527 _("Element <default> is required in <key>"));
1533 schema_state_end (SchemaState
**state_ptr
,
1540 end_element (GMarkupParseContext
*context
,
1541 const gchar
*element_name
,
1545 ParseState
*state
= user_data
;
1547 if (strcmp (element_name
, "schemalist") == 0)
1549 g_free (state
->schemalist_domain
);
1550 state
->schemalist_domain
= NULL
;
1553 else if (strcmp (element_name
, "enum") == 0 ||
1554 strcmp (element_name
, "flags") == 0)
1555 enum_state_end (&state
->enum_state
, error
);
1557 else if (strcmp (element_name
, "schema") == 0)
1558 schema_state_end (&state
->schema_state
, error
);
1560 else if (strcmp (element_name
, "override") == 0)
1561 override_state_end (&state
->key_state
, &state
->string
, error
);
1563 else if (strcmp (element_name
, "key") == 0)
1564 key_state_end (&state
->key_state
, error
);
1566 else if (strcmp (element_name
, "default") == 0)
1567 key_state_end_default (state
->key_state
, &state
->string
, error
);
1569 else if (strcmp (element_name
, "choices") == 0)
1570 key_state_end_choices (state
->key_state
, error
);
1572 else if (strcmp (element_name
, "aliases") == 0)
1573 key_state_end_aliases (state
->key_state
, error
);
1577 g_string_free (state
->string
, TRUE
);
1578 state
->string
= NULL
;
1583 text (GMarkupParseContext
*context
,
1589 ParseState
*state
= user_data
;
1593 /* we are expecting a string, so store the text data.
1595 * we store the data verbatim here and deal with whitespace
1596 * later on. there are two reasons for that:
1598 * 1) whitespace is handled differently depending on the tag
1601 * 2) we could do leading whitespace removal by refusing to
1602 * insert it into state->string if it's at the start, but for
1603 * trailing whitespace, we have no idea if there is another
1604 * text() call coming or not.
1606 g_string_append_len (state
->string
, text
, text_len
);
1610 /* string is not expected: accept (and ignore) pure whitespace */
1613 for (i
= 0; i
< text_len
; i
++)
1614 if (!g_ascii_isspace (text
[i
]))
1616 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1617 _("Text may not appear inside <%s>"),
1618 g_markup_parse_context_get_element (context
));
1624 /* Write to GVDB {{{1 */
1632 gvdb_pair_init (GvdbPair
*pair
)
1634 pair
->table
= gvdb_hash_table_new (NULL
, NULL
);
1635 pair
->root
= gvdb_hash_table_insert (pair
->table
, "");
1639 gvdb_pair_clear (GvdbPair
*pair
)
1641 g_hash_table_unref (pair
->table
);
1646 GHashTable
*schema_table
;
1652 GHashTable
*schema_table
;
1658 output_key (gpointer key
,
1662 OutputSchemaData
*data
;
1666 GVariant
*serialised
= NULL
;
1672 item
= gvdb_hash_table_insert (data
->pair
.table
, name
);
1673 gvdb_item_set_parent (item
, data
->pair
.root
);
1674 serialised
= key_state_serialise (state
);
1675 gvdb_item_set_value (item
, serialised
);
1676 g_variant_unref (serialised
);
1681 if (state
->child_schema
&&
1682 !g_hash_table_lookup (data
->schema_table
, state
->child_schema
))
1684 gchar
*message
= NULL
;
1685 message
= g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1686 state
->child_schema
);
1687 g_printerr ("%s\n", message
);
1693 output_schema (gpointer key
,
1697 WriteToFileData
*wtf_data
= user_data
;
1698 OutputSchemaData data
;
1699 GvdbPair
*root_pair
;
1706 root_pair
= &wtf_data
->root_pair
;
1708 data
.schema_table
= wtf_data
->schema_table
;
1709 gvdb_pair_init (&data
.pair
);
1712 item
= gvdb_hash_table_insert (root_pair
->table
, id
);
1713 gvdb_item_set_parent (item
, root_pair
->root
);
1714 gvdb_item_set_hash_table (item
, data
.pair
.table
);
1716 g_hash_table_foreach (state
->keys
, output_key
, &data
);
1719 gvdb_hash_table_insert_string (data
.pair
.table
, ".path", state
->path
);
1721 if (state
->extends_name
)
1722 gvdb_hash_table_insert_string (data
.pair
.table
, ".extends",
1723 state
->extends_name
);
1726 gvdb_hash_table_insert_string (data
.pair
.table
, ".list-of",
1730 gvdb_hash_table_insert_string (data
.pair
.table
,
1732 state
->gettext_domain
);
1734 gvdb_pair_clear (&data
.pair
);
1738 write_to_file (GHashTable
*schema_table
,
1739 const gchar
*filename
,
1742 WriteToFileData data
;
1745 data
.schema_table
= schema_table
;
1747 gvdb_pair_init (&data
.root_pair
);
1749 g_hash_table_foreach (schema_table
, output_schema
, &data
);
1751 success
= gvdb_table_write_contents (data
.root_pair
.table
, filename
,
1752 G_BYTE_ORDER
!= G_LITTLE_ENDIAN
,
1754 g_hash_table_unref (data
.root_pair
.table
);
1759 /* Parser driver {{{1 */
1761 parse_gschema_files (gchar
**files
,
1764 GMarkupParser parser
= { start_element
, end_element
, text
};
1765 ParseState state
= { 0, };
1766 const gchar
*filename
;
1767 GError
*error
= NULL
;
1769 state
.strict
= strict
;
1771 state
.enum_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1772 g_free
, enum_state_free
);
1774 state
.flags_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1775 g_free
, enum_state_free
);
1777 state
.schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1778 g_free
, schema_state_free
);
1780 while ((filename
= *files
++) != NULL
)
1782 GMarkupParseContext
*context
;
1787 if (!g_file_get_contents (filename
, &contents
, &size
, &error
))
1789 fprintf (stderr
, "%s\n", error
->message
);
1790 g_clear_error (&error
);
1794 context
= g_markup_parse_context_new (&parser
,
1795 G_MARKUP_TREAT_CDATA_AS_TEXT
|
1796 G_MARKUP_PREFIX_ERROR_POSITION
|
1797 G_MARKUP_IGNORE_QUALIFIED
,
1801 if (!g_markup_parse_context_parse (context
, contents
, size
, &error
) ||
1802 !g_markup_parse_context_end_parse (context
, &error
))
1806 /* back out any changes from this file */
1807 for (item
= state
.this_file_schemas
; item
; item
= item
->next
)
1808 g_hash_table_remove (state
.schema_table
, item
->data
);
1810 for (item
= state
.this_file_flagss
; item
; item
= item
->next
)
1811 g_hash_table_remove (state
.flags_table
, item
->data
);
1813 for (item
= state
.this_file_enums
; item
; item
= item
->next
)
1814 g_hash_table_remove (state
.enum_table
, item
->data
);
1817 g_markup_parse_context_get_position (context
, &line
, &col
);
1818 fprintf (stderr
, "%s:%d:%d %s. ", filename
, line
, col
, error
->message
);
1819 g_clear_error (&error
);
1823 /* Translators: Do not translate "--strict". */
1824 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1825 g_hash_table_unref (state
.schema_table
);
1826 g_hash_table_unref (state
.flags_table
);
1827 g_hash_table_unref (state
.enum_table
);
1834 fprintf (stderr
, _("This entire file has been ignored.\n"));
1839 g_markup_parse_context_free (context
);
1840 g_slist_free (state
.this_file_schemas
);
1841 g_slist_free (state
.this_file_flagss
);
1842 g_slist_free (state
.this_file_enums
);
1843 state
.this_file_schemas
= NULL
;
1844 state
.this_file_flagss
= NULL
;
1845 state
.this_file_enums
= NULL
;
1848 g_hash_table_unref (state
.flags_table
);
1849 g_hash_table_unref (state
.enum_table
);
1851 return state
.schema_table
;
1855 compare_strings (gconstpointer a
,
1858 gchar
*one
= *(gchar
**) a
;
1859 gchar
*two
= *(gchar
**) b
;
1862 cmp
= g_str_has_suffix (two
, ".enums.xml") -
1863 g_str_has_suffix (one
, ".enums.xml");
1866 cmp
= strcmp (one
, two
);
1872 set_overrides (GHashTable
*schema_table
,
1876 const gchar
*filename
;
1877 GError
*error
= NULL
;
1879 while ((filename
= *files
++))
1885 key_file
= g_key_file_new ();
1886 if (!g_key_file_load_from_file (key_file
, filename
, 0, &error
))
1888 fprintf (stderr
, "%s: %s. ", filename
, error
->message
);
1889 g_key_file_free (key_file
);
1890 g_clear_error (&error
);
1894 fprintf (stderr
, _("Ignoring this file.\n"));
1898 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1902 groups
= g_key_file_get_groups (key_file
, NULL
);
1904 for (i
= 0; groups
[i
]; i
++)
1906 const gchar
*group
= groups
[i
];
1907 SchemaState
*schema
;
1911 schema
= g_hash_table_lookup (schema_table
, group
);
1914 /* Having the schema not be installed is expected to be a
1915 * common case. Don't even emit an error message about
1920 keys
= g_key_file_get_keys (key_file
, group
, NULL
, NULL
);
1921 g_assert (keys
!= NULL
);
1923 for (j
= 0; keys
[j
]; j
++)
1925 const gchar
*key
= keys
[j
];
1930 state
= g_hash_table_lookup (schema
->keys
, key
);
1934 fprintf (stderr
, _("No such key “%s” in schema “%s” as "
1935 "specified in override file “%s”"),
1936 key
, group
, filename
);
1940 fprintf (stderr
, _("; ignoring override for this key.\n"));
1944 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1945 g_key_file_free (key_file
);
1946 g_strfreev (groups
);
1952 string
= g_key_file_get_value (key_file
, group
, key
, NULL
);
1953 g_assert (string
!= NULL
);
1955 value
= g_variant_parse (state
->type
, string
,
1956 NULL
, NULL
, &error
);
1960 fprintf (stderr
, _("error parsing key “%s” in schema “%s” "
1961 "as specified in override file “%s”: "
1963 key
, group
, filename
, error
->message
);
1965 g_clear_error (&error
);
1970 fprintf (stderr
, _("Ignoring override for this key.\n"));
1974 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1975 g_key_file_free (key_file
);
1976 g_strfreev (groups
);
1984 if (g_variant_compare (value
, state
->minimum
) < 0 ||
1985 g_variant_compare (value
, state
->maximum
) > 0)
1988 _("override for key “%s” in schema “%s” in "
1989 "override file “%s” is outside the range "
1990 "given in the schema"),
1991 key
, group
, filename
);
1993 g_variant_unref (value
);
1998 fprintf (stderr
, _("; ignoring override for this key.\n"));
2002 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2003 g_key_file_free (key_file
);
2004 g_strfreev (groups
);
2011 else if (state
->strinfo
->len
)
2013 if (!is_valid_choices (value
, state
->strinfo
))
2016 _("override for key “%s” in schema “%s” in "
2017 "override file “%s” is not in the list "
2018 "of valid choices"),
2019 key
, group
, filename
);
2021 g_variant_unref (value
);
2026 fprintf (stderr
, _("; ignoring override for this key.\n"));
2030 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2031 g_key_file_free (key_file
);
2032 g_strfreev (groups
);
2039 g_variant_unref (state
->default_value
);
2040 state
->default_value
= value
;
2047 g_strfreev (groups
);
2054 main (int argc
, char **argv
)
2056 GError
*error
= NULL
;
2057 GHashTable
*table
= NULL
;
2060 const gchar
*srcdir
;
2061 gboolean show_version_and_exit
= FALSE
;
2062 gchar
*targetdir
= NULL
;
2063 gchar
*target
= NULL
;
2064 gboolean dry_run
= FALSE
;
2065 gboolean strict
= FALSE
;
2066 gchar
**schema_files
= NULL
;
2067 gchar
**override_files
= NULL
;
2068 GOptionContext
*context
= NULL
;
2070 GOptionEntry entries
[] = {
2071 { "version", 0, 0, G_OPTION_ARG_NONE
, &show_version_and_exit
, N_("Show program version and exit"), NULL
},
2072 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME
, &targetdir
, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2073 { "strict", 0, 0, G_OPTION_ARG_NONE
, &strict
, N_("Abort on any errors in schemas"), NULL
},
2074 { "dry-run", 0, 0, G_OPTION_ARG_NONE
, &dry_run
, N_("Do not write the gschema.compiled file"), NULL
},
2075 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE
, &allow_any_name
, N_("Do not enforce key name restrictions") },
2077 /* These options are only for use in the gschema-compile tests */
2078 { "schema-file", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_FILENAME_ARRAY
, &schema_files
, NULL
, NULL
},
2086 setlocale (LC_ALL
, "");
2087 textdomain (GETTEXT_PACKAGE
);
2090 tmp
= _glib_get_locale_dir ();
2091 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
2093 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
2096 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2097 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
2100 context
= g_option_context_new (N_("DIRECTORY"));
2101 g_option_context_set_translation_domain (context
, GETTEXT_PACKAGE
);
2102 g_option_context_set_summary (context
,
2103 N_("Compile all GSettings schema files into a schema cache.\n"
2104 "Schema files are required to have the extension .gschema.xml,\n"
2105 "and the cache file is called gschemas.compiled."));
2106 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
2108 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
2110 fprintf (stderr
, "%s\n", error
->message
);
2115 if (show_version_and_exit
)
2117 g_print (PACKAGE_VERSION
"\n");
2122 if (!schema_files
&& argc
!= 2)
2124 fprintf (stderr
, _("You should give exactly one directory name\n"));
2131 target
= g_build_filename (targetdir
? targetdir
: srcdir
, "gschemas.compiled", NULL
);
2135 GPtrArray
*overrides
;
2138 files
= g_ptr_array_new ();
2139 overrides
= g_ptr_array_new ();
2141 dir
= g_dir_open (srcdir
, 0, &error
);
2144 fprintf (stderr
, "%s\n", error
->message
);
2146 g_ptr_array_unref (files
);
2147 g_ptr_array_unref (overrides
);
2153 while ((file
= g_dir_read_name (dir
)) != NULL
)
2155 if (g_str_has_suffix (file
, ".gschema.xml") ||
2156 g_str_has_suffix (file
, ".enums.xml"))
2157 g_ptr_array_add (files
, g_build_filename (srcdir
, file
, NULL
));
2159 else if (g_str_has_suffix (file
, ".gschema.override"))
2160 g_ptr_array_add (overrides
,
2161 g_build_filename (srcdir
, file
, NULL
));
2164 if (files
->len
== 0)
2166 fprintf (stdout
, _("No schema files found: "));
2168 if (g_unlink (target
))
2169 fprintf (stdout
, _("doing nothing.\n"));
2172 fprintf (stdout
, _("removed existing output file.\n"));
2174 g_ptr_array_unref (files
);
2175 g_ptr_array_unref (overrides
);
2180 g_ptr_array_sort (files
, compare_strings
);
2181 g_ptr_array_add (files
, NULL
);
2183 g_ptr_array_sort (overrides
, compare_strings
);
2184 g_ptr_array_add (overrides
, NULL
);
2186 schema_files
= (char **) g_ptr_array_free (files
, FALSE
);
2187 override_files
= (gchar
**) g_ptr_array_free (overrides
, FALSE
);
2190 if ((table
= parse_gschema_files (schema_files
, strict
)) == NULL
)
2196 if (override_files
!= NULL
&&
2197 !set_overrides (table
, override_files
, strict
))
2203 if (!dry_run
&& !write_to_file (table
, target
, &error
))
2205 fprintf (stderr
, "%s\n", error
->message
);
2214 g_clear_error (&error
);
2215 g_clear_pointer (&table
, g_hash_table_unref
);
2216 g_clear_pointer (&dir
, g_dir_close
);
2219 g_strfreev (schema_files
);
2220 g_strfreev (override_files
);
2221 g_option_context_free (context
);
2232 /* vim:set foldmethod=marker: */