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
;
749 g_variant_type_free (state
->type
);
751 g_free (state
->l10n_context
);
753 if (state
->unparsed_default_value
)
754 g_string_free (state
->unparsed_default_value
, TRUE
);
756 if (state
->default_value
)
757 g_variant_unref (state
->default_value
);
760 g_string_free (state
->strinfo
, TRUE
);
763 g_variant_unref (state
->minimum
);
766 g_variant_unref (state
->maximum
);
768 if (state
->serialised
)
769 g_variant_unref (state
->serialised
);
771 g_slice_free (KeyState
, state
);
774 /* Key name validity {{{1 */
775 static gboolean allow_any_name
= FALSE
;
778 is_valid_keyname (const gchar
*key
,
785 g_set_error_literal (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
786 _("Empty names are not permitted"));
793 if (!g_ascii_islower (key
[0]))
795 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
796 _("Invalid name “%s”: names must begin "
797 "with a lowercase letter"), key
);
801 for (i
= 1; key
[i
]; i
++)
804 !g_ascii_islower (key
[i
]) &&
805 !g_ascii_isdigit (key
[i
]))
807 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
808 _("Invalid name “%s”: invalid character “%c”; "
809 "only lowercase letters, numbers and hyphen (“-”) "
810 "are permitted"), key
, key
[i
]);
814 if (key
[i
] == '-' && key
[i
+ 1] == '-')
816 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
817 _("Invalid name “%s”: two successive hyphens (“--”) "
818 "are not permitted"), key
);
823 if (key
[i
- 1] == '-')
825 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
826 _("Invalid name “%s”: the last character may not be a "
827 "hyphen (“-”)"), key
);
833 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
834 _("Invalid name “%s”: maximum length is 1024"), key
);
841 /* Handling of <schema> {{{1 */
842 typedef struct _SchemaState SchemaState
;
845 SchemaState
*extends
;
848 gchar
*gettext_domain
;
856 schema_state_new (const gchar
*path
,
857 const gchar
*gettext_domain
,
858 SchemaState
*extends
,
859 const gchar
*extends_name
,
860 const gchar
*list_of
)
864 state
= g_slice_new (SchemaState
);
865 state
->path
= g_strdup (path
);
866 state
->gettext_domain
= g_strdup (gettext_domain
);
867 state
->extends
= extends
;
868 state
->extends_name
= g_strdup (extends_name
);
869 state
->list_of
= g_strdup (list_of
);
870 state
->keys
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
871 g_free
, key_state_free
);
877 schema_state_free (gpointer data
)
879 SchemaState
*state
= data
;
881 g_free (state
->path
);
882 g_free (state
->gettext_domain
);
883 g_hash_table_unref (state
->keys
);
884 g_slice_free (SchemaState
, state
);
888 schema_state_add_child (SchemaState
*state
,
895 if (!is_valid_keyname (name
, error
))
898 childname
= g_strconcat (name
, "/", NULL
);
900 if (g_hash_table_lookup (state
->keys
, childname
))
902 g_set_error (error
, G_MARKUP_ERROR
,
903 G_MARKUP_ERROR_INVALID_CONTENT
,
904 _("<child name='%s'> already specified"), name
);
908 g_hash_table_insert (state
->keys
, childname
,
909 key_state_new_child (schema
));
913 schema_state_add_key (SchemaState
*state
,
914 GHashTable
*enum_table
,
915 GHashTable
*flags_table
,
917 const gchar
*type_string
,
918 const gchar
*enum_type
,
919 const gchar
*flags_type
,
928 g_set_error_literal (error
, G_MARKUP_ERROR
,
929 G_MARKUP_ERROR_INVALID_CONTENT
,
930 _("Cannot add keys to a “list-of” schema"));
934 if (!is_valid_keyname (name
, error
))
937 if (g_hash_table_lookup (state
->keys
, name
))
939 g_set_error (error
, G_MARKUP_ERROR
,
940 G_MARKUP_ERROR_INVALID_CONTENT
,
941 _("<key name='%s'> already specified"), name
);
945 for (node
= state
; node
; node
= node
->extends
)
950 shadow
= g_hash_table_lookup (node
->extends
->keys
, name
);
952 /* in case of <key> <override> <key> make sure we report the
953 * location of the original <key>, not the <override>.
955 if (shadow
&& !shadow
->is_override
)
957 g_set_error (error
, G_MARKUP_ERROR
,
958 G_MARKUP_ERROR_INVALID_CONTENT
,
959 _("<key name='%s'> shadows <key name='%s'> in "
960 "<schema id='%s'>; use <override> to modify value"),
961 name
, name
, node
->extends_name
);
966 if ((type_string
!= NULL
) + (enum_type
!= NULL
) + (flags_type
!= NULL
) != 1)
968 g_set_error (error
, G_MARKUP_ERROR
,
969 G_MARKUP_ERROR_MISSING_ATTRIBUTE
,
970 _("Exactly one of “type”, “enum” or “flags” must "
971 "be specified as an attribute to <key>"));
975 if (type_string
== NULL
) /* flags or enums was specified */
977 EnumState
*enum_state
;
980 enum_state
= g_hash_table_lookup (enum_table
, enum_type
);
982 enum_state
= g_hash_table_lookup (flags_table
, flags_type
);
985 if (enum_state
== NULL
)
987 g_set_error (error
, G_MARKUP_ERROR
,
988 G_MARKUP_ERROR_INVALID_CONTENT
,
989 _("<%s id='%s'> not (yet) defined."),
990 flags_type
? "flags" : "enum",
991 flags_type
? flags_type
: enum_type
);
995 type_string
= flags_type
? "as" : "s";
996 strinfo
= enum_state
->strinfo
;
1000 if (!g_variant_type_string_is_valid (type_string
))
1002 g_set_error (error
, G_MARKUP_ERROR
,
1003 G_MARKUP_ERROR_INVALID_CONTENT
,
1004 _("Invalid GVariant type string “%s”"), type_string
);
1011 key
= key_state_new (type_string
, state
->gettext_domain
,
1012 enum_type
!= NULL
, flags_type
!= NULL
, strinfo
);
1013 g_hash_table_insert (state
->keys
, g_strdup (name
), key
);
1019 schema_state_add_override (SchemaState
*state
,
1020 KeyState
**key_state
,
1024 const gchar
*context
,
1027 SchemaState
*parent
;
1030 if (state
->extends
== NULL
)
1032 g_set_error_literal (error
, G_MARKUP_ERROR
,
1033 G_MARKUP_ERROR_INVALID_CONTENT
,
1034 _("<override> given but schema isn’t "
1035 "extending anything"));
1039 for (parent
= state
->extends
; parent
; parent
= parent
->extends
)
1040 if ((original
= g_hash_table_lookup (parent
->keys
, key
)))
1043 if (original
== NULL
)
1045 g_set_error (error
, G_MARKUP_ERROR
,
1046 G_MARKUP_ERROR_INVALID_CONTENT
,
1047 _("No <key name='%s'> to override"), key
);
1051 if (g_hash_table_lookup (state
->keys
, key
))
1053 g_set_error (error
, G_MARKUP_ERROR
,
1054 G_MARKUP_ERROR_INVALID_CONTENT
,
1055 _("<override name='%s'> already specified"), key
);
1059 *key_state
= key_state_override (original
, state
->gettext_domain
);
1060 *string
= key_state_start_default (*key_state
, l10n
, context
, error
);
1061 g_hash_table_insert (state
->keys
, g_strdup (key
), *key_state
);
1065 override_state_end (KeyState
**key_state
,
1069 key_state_end_default (*key_state
, string
, error
);
1073 /* Handling of toplevel state {{{1 */
1076 gboolean strict
; /* TRUE if --strict was given */
1078 GHashTable
*schema_table
; /* string -> SchemaState */
1079 GHashTable
*flags_table
; /* string -> EnumState */
1080 GHashTable
*enum_table
; /* string -> EnumState */
1082 GSList
*this_file_schemas
; /* strings: <schema>s in this file */
1083 GSList
*this_file_flagss
; /* strings: <flags>s in this file */
1084 GSList
*this_file_enums
; /* strings: <enum>s in this file */
1086 gchar
*schemalist_domain
; /* the <schemalist> gettext domain */
1088 SchemaState
*schema_state
; /* non-NULL when inside <schema> */
1089 KeyState
*key_state
; /* non-NULL when inside <key> */
1090 EnumState
*enum_state
; /* non-NULL when inside <enum> */
1092 GString
*string
; /* non-NULL when accepting text */
1096 is_subclass (const gchar
*class_name
,
1097 const gchar
*possible_parent
,
1098 GHashTable
*schema_table
)
1102 if (strcmp (class_name
, possible_parent
) == 0)
1105 class = g_hash_table_lookup (schema_table
, class_name
);
1106 g_assert (class != NULL
);
1108 return class->extends_name
&&
1109 is_subclass (class->extends_name
, possible_parent
, schema_table
);
1113 parse_state_start_schema (ParseState
*state
,
1116 const gchar
*gettext_domain
,
1117 const gchar
*extends_name
,
1118 const gchar
*list_of
,
1121 SchemaState
*extends
;
1124 if (g_hash_table_lookup (state
->schema_table
, id
))
1126 g_set_error (error
, G_MARKUP_ERROR
,
1127 G_MARKUP_ERROR_INVALID_CONTENT
,
1128 _("<schema id='%s'> already specified"), id
);
1134 extends
= g_hash_table_lookup (state
->schema_table
, extends_name
);
1136 if (extends
== NULL
)
1138 g_set_error (error
, G_MARKUP_ERROR
,
1139 G_MARKUP_ERROR_INVALID_CONTENT
,
1140 _("<schema id='%s'> extends not yet existing "
1141 "schema “%s”"), id
, extends_name
);
1152 if (!(tmp
= g_hash_table_lookup (state
->schema_table
, list_of
)))
1154 g_set_error (error
, G_MARKUP_ERROR
,
1155 G_MARKUP_ERROR_INVALID_CONTENT
,
1156 _("<schema id='%s'> is list of not yet existing "
1157 "schema “%s”"), id
, list_of
);
1163 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1164 _("Cannot be a list of a schema with a path"));
1173 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1174 _("Cannot extend a schema with a path"));
1180 if (extends
->list_of
== NULL
)
1182 g_set_error (error
, G_MARKUP_ERROR
,
1183 G_MARKUP_ERROR_INVALID_CONTENT
,
1184 _("<schema id='%s'> is a list, extending "
1185 "<schema id='%s'> which is not a list"),
1190 if (!is_subclass (list_of
, extends
->list_of
, state
->schema_table
))
1192 g_set_error (error
, G_MARKUP_ERROR
,
1193 G_MARKUP_ERROR_INVALID_CONTENT
,
1194 _("<schema id='%s' list-of='%s'> extends <schema "
1195 "id='%s' list-of='%s'> but “%s” does not "
1196 "extend “%s”"), id
, list_of
, extends_name
,
1197 extends
->list_of
, list_of
, extends
->list_of
);
1202 /* by default we are a list of the same thing that the schema
1203 * we are extending is a list of (which might be nothing)
1205 list_of
= extends
->list_of
;
1208 if (path
&& !(g_str_has_prefix (path
, "/") && g_str_has_suffix (path
, "/")))
1210 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1211 _("A path, if given, must begin and end with a slash"));
1215 if (path
&& list_of
&& !g_str_has_suffix (path
, ":/"))
1217 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1218 _("The path of a list must end with “:/”"));
1222 if (path
&& (g_str_has_prefix (path
, "/apps/") ||
1223 g_str_has_prefix (path
, "/desktop/") ||
1224 g_str_has_prefix (path
, "/system/")))
1226 gchar
*message
= NULL
;
1227 message
= g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1228 "Paths starting with "
1229 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1231 g_printerr ("%s\n", message
);
1235 state
->schema_state
= schema_state_new (path
, gettext_domain
,
1236 extends
, extends_name
, list_of
);
1238 my_id
= g_strdup (id
);
1239 state
->this_file_schemas
= g_slist_prepend (state
->this_file_schemas
, my_id
);
1240 g_hash_table_insert (state
->schema_table
, my_id
, state
->schema_state
);
1244 parse_state_start_enum (ParseState
*state
,
1249 GSList
**list
= is_flags
? &state
->this_file_flagss
: &state
->this_file_enums
;
1250 GHashTable
*table
= is_flags
? state
->flags_table
: state
->enum_table
;
1253 if (g_hash_table_lookup (table
, id
))
1255 g_set_error (error
, G_MARKUP_ERROR
,
1256 G_MARKUP_ERROR_INVALID_CONTENT
,
1257 _("<%s id='%s'> already specified"),
1258 is_flags
? "flags" : "enum", id
);
1262 state
->enum_state
= enum_state_new (is_flags
);
1264 my_id
= g_strdup (id
);
1265 *list
= g_slist_prepend (*list
, my_id
);
1266 g_hash_table_insert (table
, my_id
, state
->enum_state
);
1269 /* GMarkup Parser Functions {{{1 */
1271 /* Start element {{{2 */
1273 start_element (GMarkupParseContext
*context
,
1274 const gchar
*element_name
,
1275 const gchar
**attribute_names
,
1276 const gchar
**attribute_values
,
1280 ParseState
*state
= user_data
;
1281 const GSList
*element_stack
;
1282 const gchar
*container
;
1284 element_stack
= g_markup_parse_context_get_element_stack (context
);
1285 container
= element_stack
->next
? element_stack
->next
->data
: NULL
;
1287 #define COLLECT(first, ...) \
1288 g_markup_collect_attributes (element_name, \
1289 attribute_names, attribute_values, error, \
1290 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1291 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1292 #define STRDUP G_MARKUP_COLLECT_STRDUP
1293 #define STRING G_MARKUP_COLLECT_STRING
1294 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1296 /* Toplevel items {{{3 */
1297 if (container
== NULL
)
1299 if (strcmp (element_name
, "schemalist") == 0)
1301 COLLECT (OPTIONAL
| STRDUP
,
1303 &state
->schemalist_domain
);
1309 /* children of <schemalist> {{{3 */
1310 else if (strcmp (container
, "schemalist") == 0)
1312 if (strcmp (element_name
, "schema") == 0)
1314 const gchar
*id
, *path
, *gettext_domain
, *extends
, *list_of
;
1315 if (COLLECT (STRING
, "id", &id
,
1316 OPTIONAL
| STRING
, "path", &path
,
1317 OPTIONAL
| STRING
, "gettext-domain", &gettext_domain
,
1318 OPTIONAL
| STRING
, "extends", &extends
,
1319 OPTIONAL
| STRING
, "list-of", &list_of
))
1320 parse_state_start_schema (state
, id
, path
,
1321 gettext_domain
? gettext_domain
1322 : state
->schemalist_domain
,
1323 extends
, list_of
, error
);
1327 else if (strcmp (element_name
, "enum") == 0)
1330 if (COLLECT (STRING
, "id", &id
))
1331 parse_state_start_enum (state
, id
, FALSE
, error
);
1335 else if (strcmp (element_name
, "flags") == 0)
1338 if (COLLECT (STRING
, "id", &id
))
1339 parse_state_start_enum (state
, id
, TRUE
, error
);
1345 /* children of <schema> {{{3 */
1346 else if (strcmp (container
, "schema") == 0)
1348 if (strcmp (element_name
, "key") == 0)
1350 const gchar
*name
, *type_string
, *enum_type
, *flags_type
;
1352 if (COLLECT (STRING
, "name", &name
,
1353 OPTIONAL
| STRING
, "type", &type_string
,
1354 OPTIONAL
| STRING
, "enum", &enum_type
,
1355 OPTIONAL
| STRING
, "flags", &flags_type
))
1357 state
->key_state
= schema_state_add_key (state
->schema_state
,
1361 enum_type
, flags_type
,
1365 else if (strcmp (element_name
, "child") == 0)
1367 const gchar
*name
, *schema
;
1369 if (COLLECT (STRING
, "name", &name
, STRING
, "schema", &schema
))
1370 schema_state_add_child (state
->schema_state
,
1371 name
, schema
, error
);
1374 else if (strcmp (element_name
, "override") == 0)
1376 const gchar
*name
, *l10n
, *context
;
1378 if (COLLECT (STRING
, "name", &name
,
1379 OPTIONAL
| STRING
, "l10n", &l10n
,
1380 OPTIONAL
| STRING
, "context", &context
))
1381 schema_state_add_override (state
->schema_state
,
1382 &state
->key_state
, &state
->string
,
1383 name
, l10n
, context
, error
);
1388 /* children of <key> {{{3 */
1389 else if (strcmp (container
, "key") == 0)
1391 if (strcmp (element_name
, "default") == 0)
1393 const gchar
*l10n
, *context
;
1394 if (COLLECT (STRING
| OPTIONAL
, "l10n", &l10n
,
1395 STRING
| OPTIONAL
, "context", &context
))
1396 state
->string
= key_state_start_default (state
->key_state
,
1397 l10n
, context
, error
);
1401 else if (strcmp (element_name
, "summary") == 0)
1405 if (state
->key_state
->summary_seen
&& state
->strict
)
1406 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1407 _("Only one <%s> element allowed inside <%s>"),
1408 element_name
, container
);
1410 state
->string
= g_string_new (NULL
);
1412 state
->key_state
->summary_seen
= TRUE
;
1417 else if (strcmp (element_name
, "description") == 0)
1421 if (state
->key_state
->description_seen
&& state
->strict
)
1422 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1423 _("Only one <%s> element allowed inside <%s>"),
1424 element_name
, container
);
1426 state
->string
= g_string_new (NULL
);
1428 state
->key_state
->description_seen
= TRUE
;
1433 else if (strcmp (element_name
, "range") == 0)
1435 const gchar
*min
, *max
;
1436 if (COLLECT (STRING
| OPTIONAL
, "min", &min
,
1437 STRING
| OPTIONAL
, "max", &max
))
1438 key_state_set_range (state
->key_state
, min
, max
, error
);
1442 else if (strcmp (element_name
, "choices") == 0)
1445 key_state_start_choices (state
->key_state
, error
);
1449 else if (strcmp (element_name
, "aliases") == 0)
1452 key_state_start_aliases (state
->key_state
, error
);
1458 /* children of <choices> {{{3 */
1459 else if (strcmp (container
, "choices") == 0)
1461 if (strcmp (element_name
, "choice") == 0)
1464 if (COLLECT (STRING
, "value", &value
))
1465 key_state_add_choice (state
->key_state
, value
, error
);
1471 /* children of <aliases> {{{3 */
1472 else if (strcmp (container
, "aliases") == 0)
1474 if (strcmp (element_name
, "alias") == 0)
1476 const gchar
*value
, *target
;
1477 if (COLLECT (STRING
, "value", &value
, STRING
, "target", &target
))
1478 key_state_add_alias (state
->key_state
, value
, target
, error
);
1484 /* children of <enum> {{{3 */
1485 else if (strcmp (container
, "enum") == 0 ||
1486 strcmp (container
, "flags") == 0)
1488 if (strcmp (element_name
, "value") == 0)
1490 const gchar
*nick
, *valuestr
;
1491 if (COLLECT (STRING
, "nick", &nick
,
1492 STRING
, "value", &valuestr
))
1493 enum_state_add_value (state
->enum_state
, nick
, valuestr
, error
);
1500 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1501 _("Element <%s> not allowed inside <%s>"),
1502 element_name
, container
);
1504 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1505 _("Element <%s> not allowed at the top level"), element_name
);
1508 /* End element {{{2 */
1511 key_state_end (KeyState
**state_ptr
,
1519 if (state
->default_value
== NULL
)
1521 g_set_error_literal (error
,
1522 G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1523 _("Element <default> is required in <key>"));
1529 schema_state_end (SchemaState
**state_ptr
,
1536 end_element (GMarkupParseContext
*context
,
1537 const gchar
*element_name
,
1541 ParseState
*state
= user_data
;
1543 if (strcmp (element_name
, "schemalist") == 0)
1545 g_free (state
->schemalist_domain
);
1546 state
->schemalist_domain
= NULL
;
1549 else if (strcmp (element_name
, "enum") == 0 ||
1550 strcmp (element_name
, "flags") == 0)
1551 enum_state_end (&state
->enum_state
, error
);
1553 else if (strcmp (element_name
, "schema") == 0)
1554 schema_state_end (&state
->schema_state
, error
);
1556 else if (strcmp (element_name
, "override") == 0)
1557 override_state_end (&state
->key_state
, &state
->string
, error
);
1559 else if (strcmp (element_name
, "key") == 0)
1560 key_state_end (&state
->key_state
, error
);
1562 else if (strcmp (element_name
, "default") == 0)
1563 key_state_end_default (state
->key_state
, &state
->string
, error
);
1565 else if (strcmp (element_name
, "choices") == 0)
1566 key_state_end_choices (state
->key_state
, error
);
1568 else if (strcmp (element_name
, "aliases") == 0)
1569 key_state_end_aliases (state
->key_state
, error
);
1573 g_string_free (state
->string
, TRUE
);
1574 state
->string
= NULL
;
1579 text (GMarkupParseContext
*context
,
1585 ParseState
*state
= user_data
;
1589 /* we are expecting a string, so store the text data.
1591 * we store the data verbatim here and deal with whitespace
1592 * later on. there are two reasons for that:
1594 * 1) whitespace is handled differently depending on the tag
1597 * 2) we could do leading whitespace removal by refusing to
1598 * insert it into state->string if it's at the start, but for
1599 * trailing whitespace, we have no idea if there is another
1600 * text() call coming or not.
1602 g_string_append_len (state
->string
, text
, text_len
);
1606 /* string is not expected: accept (and ignore) pure whitespace */
1609 for (i
= 0; i
< text_len
; i
++)
1610 if (!g_ascii_isspace (text
[i
]))
1612 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1613 _("Text may not appear inside <%s>"),
1614 g_markup_parse_context_get_element (context
));
1620 /* Write to GVDB {{{1 */
1628 gvdb_pair_init (GvdbPair
*pair
)
1630 pair
->table
= gvdb_hash_table_new (NULL
, NULL
);
1631 pair
->root
= gvdb_hash_table_insert (pair
->table
, "");
1635 gvdb_pair_clear (GvdbPair
*pair
)
1637 g_hash_table_unref (pair
->table
);
1642 GHashTable
*schema_table
;
1648 GHashTable
*schema_table
;
1654 output_key (gpointer key
,
1658 OutputSchemaData
*data
;
1662 GVariant
*serialised
= NULL
;
1668 item
= gvdb_hash_table_insert (data
->pair
.table
, name
);
1669 gvdb_item_set_parent (item
, data
->pair
.root
);
1670 serialised
= key_state_serialise (state
);
1671 gvdb_item_set_value (item
, serialised
);
1672 g_variant_unref (serialised
);
1677 if (state
->child_schema
&&
1678 !g_hash_table_lookup (data
->schema_table
, state
->child_schema
))
1680 gchar
*message
= NULL
;
1681 message
= g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1682 state
->child_schema
);
1683 g_printerr ("%s\n", message
);
1689 output_schema (gpointer key
,
1693 WriteToFileData
*wtf_data
= user_data
;
1694 OutputSchemaData data
;
1695 GvdbPair
*root_pair
;
1702 root_pair
= &wtf_data
->root_pair
;
1704 data
.schema_table
= wtf_data
->schema_table
;
1705 gvdb_pair_init (&data
.pair
);
1708 item
= gvdb_hash_table_insert (root_pair
->table
, id
);
1709 gvdb_item_set_parent (item
, root_pair
->root
);
1710 gvdb_item_set_hash_table (item
, data
.pair
.table
);
1712 g_hash_table_foreach (state
->keys
, output_key
, &data
);
1715 gvdb_hash_table_insert_string (data
.pair
.table
, ".path", state
->path
);
1717 if (state
->extends_name
)
1718 gvdb_hash_table_insert_string (data
.pair
.table
, ".extends",
1719 state
->extends_name
);
1722 gvdb_hash_table_insert_string (data
.pair
.table
, ".list-of",
1726 gvdb_hash_table_insert_string (data
.pair
.table
,
1728 state
->gettext_domain
);
1730 gvdb_pair_clear (&data
.pair
);
1734 write_to_file (GHashTable
*schema_table
,
1735 const gchar
*filename
,
1738 WriteToFileData data
;
1741 data
.schema_table
= schema_table
;
1743 gvdb_pair_init (&data
.root_pair
);
1745 g_hash_table_foreach (schema_table
, output_schema
, &data
);
1747 success
= gvdb_table_write_contents (data
.root_pair
.table
, filename
,
1748 G_BYTE_ORDER
!= G_LITTLE_ENDIAN
,
1750 g_hash_table_unref (data
.root_pair
.table
);
1755 /* Parser driver {{{1 */
1757 parse_gschema_files (gchar
**files
,
1760 GMarkupParser parser
= { start_element
, end_element
, text
};
1761 ParseState state
= { 0, };
1762 const gchar
*filename
;
1763 GError
*error
= NULL
;
1765 state
.strict
= strict
;
1767 state
.enum_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1768 g_free
, enum_state_free
);
1770 state
.flags_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1771 g_free
, enum_state_free
);
1773 state
.schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1774 g_free
, schema_state_free
);
1776 while ((filename
= *files
++) != NULL
)
1778 GMarkupParseContext
*context
;
1783 if (!g_file_get_contents (filename
, &contents
, &size
, &error
))
1785 fprintf (stderr
, "%s\n", error
->message
);
1786 g_clear_error (&error
);
1790 context
= g_markup_parse_context_new (&parser
,
1791 G_MARKUP_TREAT_CDATA_AS_TEXT
|
1792 G_MARKUP_PREFIX_ERROR_POSITION
|
1793 G_MARKUP_IGNORE_QUALIFIED
,
1797 if (!g_markup_parse_context_parse (context
, contents
, size
, &error
) ||
1798 !g_markup_parse_context_end_parse (context
, &error
))
1802 /* back out any changes from this file */
1803 for (item
= state
.this_file_schemas
; item
; item
= item
->next
)
1804 g_hash_table_remove (state
.schema_table
, item
->data
);
1806 for (item
= state
.this_file_flagss
; item
; item
= item
->next
)
1807 g_hash_table_remove (state
.flags_table
, item
->data
);
1809 for (item
= state
.this_file_enums
; item
; item
= item
->next
)
1810 g_hash_table_remove (state
.enum_table
, item
->data
);
1813 g_markup_parse_context_get_position (context
, &line
, &col
);
1814 fprintf (stderr
, "%s:%d:%d %s. ", filename
, line
, col
, error
->message
);
1815 g_clear_error (&error
);
1819 /* Translators: Do not translate "--strict". */
1820 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1821 g_hash_table_unref (state
.schema_table
);
1822 g_hash_table_unref (state
.flags_table
);
1823 g_hash_table_unref (state
.enum_table
);
1830 fprintf (stderr
, _("This entire file has been ignored.\n"));
1835 g_markup_parse_context_free (context
);
1836 g_slist_free (state
.this_file_schemas
);
1837 g_slist_free (state
.this_file_flagss
);
1838 g_slist_free (state
.this_file_enums
);
1839 state
.this_file_schemas
= NULL
;
1840 state
.this_file_flagss
= NULL
;
1841 state
.this_file_enums
= NULL
;
1844 g_hash_table_unref (state
.flags_table
);
1845 g_hash_table_unref (state
.enum_table
);
1847 return state
.schema_table
;
1851 compare_strings (gconstpointer a
,
1854 gchar
*one
= *(gchar
**) a
;
1855 gchar
*two
= *(gchar
**) b
;
1858 cmp
= g_str_has_suffix (two
, ".enums.xml") -
1859 g_str_has_suffix (one
, ".enums.xml");
1862 cmp
= strcmp (one
, two
);
1868 set_overrides (GHashTable
*schema_table
,
1872 const gchar
*filename
;
1873 GError
*error
= NULL
;
1875 while ((filename
= *files
++))
1881 key_file
= g_key_file_new ();
1882 if (!g_key_file_load_from_file (key_file
, filename
, 0, &error
))
1884 fprintf (stderr
, "%s: %s. ", filename
, error
->message
);
1885 g_key_file_free (key_file
);
1886 g_clear_error (&error
);
1890 fprintf (stderr
, _("Ignoring this file.\n"));
1894 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1898 groups
= g_key_file_get_groups (key_file
, NULL
);
1900 for (i
= 0; groups
[i
]; i
++)
1902 const gchar
*group
= groups
[i
];
1903 SchemaState
*schema
;
1907 schema
= g_hash_table_lookup (schema_table
, group
);
1910 /* Having the schema not be installed is expected to be a
1911 * common case. Don't even emit an error message about
1916 keys
= g_key_file_get_keys (key_file
, group
, NULL
, NULL
);
1917 g_assert (keys
!= NULL
);
1919 for (j
= 0; keys
[j
]; j
++)
1921 const gchar
*key
= keys
[j
];
1926 state
= g_hash_table_lookup (schema
->keys
, key
);
1930 fprintf (stderr
, _("No such key '%s' in schema '%s' as "
1931 "specified in override file '%s'"),
1932 key
, group
, filename
);
1936 fprintf (stderr
, _("; ignoring override for this key.\n"));
1940 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1941 g_key_file_free (key_file
);
1942 g_strfreev (groups
);
1948 string
= g_key_file_get_value (key_file
, group
, key
, NULL
);
1949 g_assert (string
!= NULL
);
1951 value
= g_variant_parse (state
->type
, string
,
1952 NULL
, NULL
, &error
);
1956 fprintf (stderr
, _("error parsing key '%s' in schema '%s' "
1957 "as specified in override file '%s': "
1959 key
, group
, filename
, error
->message
);
1961 g_clear_error (&error
);
1966 fprintf (stderr
, _("Ignoring override for this key.\n"));
1970 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1971 g_key_file_free (key_file
);
1972 g_strfreev (groups
);
1980 if (g_variant_compare (value
, state
->minimum
) < 0 ||
1981 g_variant_compare (value
, state
->maximum
) > 0)
1984 _("override for key '%s' in schema '%s' in "
1985 "override file '%s' is outside the range "
1986 "given in the schema"),
1987 key
, group
, filename
);
1989 g_variant_unref (value
);
1994 fprintf (stderr
, _("; ignoring override for this key.\n"));
1998 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1999 g_key_file_free (key_file
);
2000 g_strfreev (groups
);
2007 else if (state
->strinfo
->len
)
2009 if (!is_valid_choices (value
, state
->strinfo
))
2012 _("override for key '%s' in schema '%s' in "
2013 "override file '%s' is not in the list "
2014 "of valid choices"),
2015 key
, group
, filename
);
2017 g_variant_unref (value
);
2022 fprintf (stderr
, _("; ignoring override for this key.\n"));
2026 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2027 g_key_file_free (key_file
);
2028 g_strfreev (groups
);
2035 g_variant_unref (state
->default_value
);
2036 state
->default_value
= value
;
2043 g_strfreev (groups
);
2050 main (int argc
, char **argv
)
2052 GError
*error
= NULL
;
2053 GHashTable
*table
= NULL
;
2056 const gchar
*srcdir
;
2057 gboolean show_version_and_exit
= FALSE
;
2058 gchar
*targetdir
= NULL
;
2059 gchar
*target
= NULL
;
2060 gboolean dry_run
= FALSE
;
2061 gboolean strict
= FALSE
;
2062 gchar
**schema_files
= NULL
;
2063 gchar
**override_files
= NULL
;
2064 GOptionContext
*context
= NULL
;
2066 GOptionEntry entries
[] = {
2067 { "version", 0, 0, G_OPTION_ARG_NONE
, &show_version_and_exit
, N_("Show program version and exit"), NULL
},
2068 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME
, &targetdir
, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2069 { "strict", 0, 0, G_OPTION_ARG_NONE
, &strict
, N_("Abort on any errors in schemas"), NULL
},
2070 { "dry-run", 0, 0, G_OPTION_ARG_NONE
, &dry_run
, N_("Do not write the gschema.compiled file"), NULL
},
2071 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE
, &allow_any_name
, N_("Do not enforce key name restrictions") },
2073 /* These options are only for use in the gschema-compile tests */
2074 { "schema-file", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_FILENAME_ARRAY
, &schema_files
, NULL
, NULL
},
2082 setlocale (LC_ALL
, "");
2083 textdomain (GETTEXT_PACKAGE
);
2086 tmp
= _glib_get_locale_dir ();
2087 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
2089 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
2092 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2093 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
2096 context
= g_option_context_new (N_("DIRECTORY"));
2097 g_option_context_set_translation_domain (context
, GETTEXT_PACKAGE
);
2098 g_option_context_set_summary (context
,
2099 N_("Compile all GSettings schema files into a schema cache.\n"
2100 "Schema files are required to have the extension .gschema.xml,\n"
2101 "and the cache file is called gschemas.compiled."));
2102 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
2104 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
2106 fprintf (stderr
, "%s\n", error
->message
);
2111 if (show_version_and_exit
)
2113 g_print (PACKAGE_VERSION
"\n");
2118 if (!schema_files
&& argc
!= 2)
2120 fprintf (stderr
, _("You should give exactly one directory name\n"));
2127 target
= g_build_filename (targetdir
? targetdir
: srcdir
, "gschemas.compiled", NULL
);
2131 GPtrArray
*overrides
;
2134 files
= g_ptr_array_new ();
2135 overrides
= g_ptr_array_new ();
2137 dir
= g_dir_open (srcdir
, 0, &error
);
2140 fprintf (stderr
, "%s\n", error
->message
);
2142 g_ptr_array_unref (files
);
2143 g_ptr_array_unref (overrides
);
2149 while ((file
= g_dir_read_name (dir
)) != NULL
)
2151 if (g_str_has_suffix (file
, ".gschema.xml") ||
2152 g_str_has_suffix (file
, ".enums.xml"))
2153 g_ptr_array_add (files
, g_build_filename (srcdir
, file
, NULL
));
2155 else if (g_str_has_suffix (file
, ".gschema.override"))
2156 g_ptr_array_add (overrides
,
2157 g_build_filename (srcdir
, file
, NULL
));
2160 if (files
->len
== 0)
2162 fprintf (stdout
, _("No schema files found: "));
2164 if (g_unlink (target
))
2165 fprintf (stdout
, _("doing nothing.\n"));
2168 fprintf (stdout
, _("removed existing output file.\n"));
2170 g_ptr_array_unref (files
);
2171 g_ptr_array_unref (overrides
);
2176 g_ptr_array_sort (files
, compare_strings
);
2177 g_ptr_array_add (files
, NULL
);
2179 g_ptr_array_sort (overrides
, compare_strings
);
2180 g_ptr_array_add (overrides
, NULL
);
2182 schema_files
= (char **) g_ptr_array_free (files
, FALSE
);
2183 override_files
= (gchar
**) g_ptr_array_free (overrides
, FALSE
);
2186 if ((table
= parse_gschema_files (schema_files
, strict
)) == NULL
)
2192 if (override_files
!= NULL
&&
2193 !set_overrides (table
, override_files
, strict
))
2199 if (!dry_run
&& !write_to_file (table
, target
, &error
))
2201 fprintf (stderr
, "%s\n", error
->message
);
2210 g_clear_error (&error
);
2211 g_clear_pointer (&table
, g_hash_table_unref
);
2212 g_clear_pointer (&dir
, g_dir_close
);
2215 g_strfreev (schema_files
);
2216 g_strfreev (override_files
);
2217 g_option_context_free (context
);
2228 /* vim:set foldmethod=marker: */