2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
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 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 maxmimum");
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
,
604 "alias target '%s' is not in %s", target
,
605 state
->is_enum
? "enumerated type" : "<choices>");
609 state
->has_aliases
= TRUE
;
613 key_state_end_aliases (KeyState
*state
,
616 if (!state
->has_aliases
)
618 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
619 "<aliases> must contain at least one <alias>");
625 key_state_check (KeyState
*state
,
631 return state
->checked
= TRUE
;
635 key_state_serialise (KeyState
*state
)
637 if (state
->serialised
== NULL
)
639 if (state
->child_schema
)
641 state
->serialised
= g_variant_new_string (state
->child_schema
);
646 GVariantBuilder builder
;
648 g_assert (key_state_check (state
, NULL
));
650 g_variant_builder_init (&builder
, G_VARIANT_TYPE_TUPLE
);
653 g_variant_builder_add_value (&builder
, state
->default_value
);
658 /* We are going to store the untranslated default for
659 * runtime translation according to the current locale.
660 * We need to strip leading and trailing whitespace from
661 * the string so that it's exactly the same as the one
662 * that ended up in the .po file for translation.
664 * We want to do this so that
666 * <default l10n='messages'>
670 * ends up in the .po file like "['a', 'b', 'c']",
671 * omitting the extra whitespace at the start and end.
673 strip_string (state
->unparsed_default_value
);
675 if (state
->l10n_context
)
679 /* Contextified messages are supported by prepending
680 * the context, followed by '\004' to the start of the
681 * message string. We do that here to save GSettings
684 len
= strlen (state
->l10n_context
);
685 state
->l10n_context
[len
] = '\004';
686 g_string_prepend_len (state
->unparsed_default_value
,
687 state
->l10n_context
, len
+ 1);
688 g_free (state
->l10n_context
);
689 state
->l10n_context
= NULL
;
692 g_variant_builder_add (&builder
, "(y(y&s))", 'l', state
->l10n
,
693 state
->unparsed_default_value
->str
);
694 g_string_free (state
->unparsed_default_value
, TRUE
);
695 state
->unparsed_default_value
= NULL
;
698 /* choice, aliases, enums */
699 if (state
->strinfo
->len
)
707 data
= state
->strinfo
->str
;
708 size
= state
->strinfo
->len
;
711 for (i
= 0; i
< size
/ sizeof (guint32
); i
++)
712 words
[i
] = GUINT32_TO_LE (words
[i
]);
714 array
= g_variant_new_from_data (G_VARIANT_TYPE ("au"),
718 g_string_free (state
->strinfo
, FALSE
);
719 state
->strinfo
= NULL
;
721 g_variant_builder_add (&builder
, "(y@au)",
722 state
->is_flags
? 'f' :
723 state
->is_enum
? 'e' : 'c',
728 if (state
->minimum
|| state
->maximum
)
729 g_variant_builder_add (&builder
, "(y(**))", 'r',
730 state
->minimum
, state
->maximum
);
732 state
->serialised
= g_variant_builder_end (&builder
);
735 g_variant_ref_sink (state
->serialised
);
738 return g_variant_ref (state
->serialised
);
742 key_state_free (gpointer data
)
744 KeyState
*state
= data
;
747 g_variant_type_free (state
->type
);
749 g_free (state
->l10n_context
);
751 if (state
->unparsed_default_value
)
752 g_string_free (state
->unparsed_default_value
, TRUE
);
754 if (state
->default_value
)
755 g_variant_unref (state
->default_value
);
758 g_string_free (state
->strinfo
, TRUE
);
761 g_variant_unref (state
->minimum
);
764 g_variant_unref (state
->maximum
);
766 if (state
->serialised
)
767 g_variant_unref (state
->serialised
);
769 g_slice_free (KeyState
, state
);
772 /* Key name validity {{{1 */
773 static gboolean allow_any_name
= FALSE
;
776 is_valid_keyname (const gchar
*key
,
783 g_set_error_literal (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
784 _("empty names are not permitted"));
791 if (!g_ascii_islower (key
[0]))
793 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
794 _("invalid name '%s': names must begin "
795 "with a lowercase letter"), key
);
799 for (i
= 1; key
[i
]; i
++)
802 !g_ascii_islower (key
[i
]) &&
803 !g_ascii_isdigit (key
[i
]))
805 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
806 _("invalid name '%s': invalid character '%c'; "
807 "only lowercase letters, numbers and hyphen ('-') "
808 "are permitted."), key
, key
[i
]);
812 if (key
[i
] == '-' && key
[i
+ 1] == '-')
814 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
815 _("invalid name '%s': two successive hyphens ('--') "
816 "are not permitted."), key
);
821 if (key
[i
- 1] == '-')
823 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
824 _("invalid name '%s': the last character may not be a "
825 "hyphen ('-')."), key
);
831 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
832 _("invalid name '%s': maximum length is 1024"), key
);
839 /* Handling of <schema> {{{1 */
840 typedef struct _SchemaState SchemaState
;
843 SchemaState
*extends
;
846 gchar
*gettext_domain
;
854 schema_state_new (const gchar
*path
,
855 const gchar
*gettext_domain
,
856 SchemaState
*extends
,
857 const gchar
*extends_name
,
858 const gchar
*list_of
)
862 state
= g_slice_new (SchemaState
);
863 state
->path
= g_strdup (path
);
864 state
->gettext_domain
= g_strdup (gettext_domain
);
865 state
->extends
= extends
;
866 state
->extends_name
= g_strdup (extends_name
);
867 state
->list_of
= g_strdup (list_of
);
868 state
->keys
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
869 g_free
, key_state_free
);
875 schema_state_free (gpointer data
)
877 SchemaState
*state
= data
;
879 g_free (state
->path
);
880 g_free (state
->gettext_domain
);
881 g_hash_table_unref (state
->keys
);
882 g_slice_free (SchemaState
, state
);
886 schema_state_add_child (SchemaState
*state
,
893 if (!is_valid_keyname (name
, error
))
896 childname
= g_strconcat (name
, "/", NULL
);
898 if (g_hash_table_lookup (state
->keys
, childname
))
900 g_set_error (error
, G_MARKUP_ERROR
,
901 G_MARKUP_ERROR_INVALID_CONTENT
,
902 _("<child name='%s'> already specified"), name
);
906 g_hash_table_insert (state
->keys
, childname
,
907 key_state_new_child (schema
));
911 schema_state_add_key (SchemaState
*state
,
912 GHashTable
*enum_table
,
913 GHashTable
*flags_table
,
915 const gchar
*type_string
,
916 const gchar
*enum_type
,
917 const gchar
*flags_type
,
926 g_set_error_literal (error
, G_MARKUP_ERROR
,
927 G_MARKUP_ERROR_INVALID_CONTENT
,
928 _("cannot add keys to a 'list-of' schema"));
932 if (!is_valid_keyname (name
, error
))
935 if (g_hash_table_lookup (state
->keys
, name
))
937 g_set_error (error
, G_MARKUP_ERROR
,
938 G_MARKUP_ERROR_INVALID_CONTENT
,
939 _("<key name='%s'> already specified"), name
);
943 for (node
= state
; node
; node
= node
->extends
)
948 shadow
= g_hash_table_lookup (node
->extends
->keys
, name
);
950 /* in case of <key> <override> <key> make sure we report the
951 * location of the original <key>, not the <override>.
953 if (shadow
&& !shadow
->is_override
)
955 g_set_error (error
, G_MARKUP_ERROR
,
956 G_MARKUP_ERROR_INVALID_CONTENT
,
957 _("<key name='%s'> shadows <key name='%s'> in "
958 "<schema id='%s'>; use <override> to modify value"),
959 name
, name
, node
->extends_name
);
964 if ((type_string
!= NULL
) + (enum_type
!= NULL
) + (flags_type
!= NULL
) != 1)
966 g_set_error (error
, G_MARKUP_ERROR
,
967 G_MARKUP_ERROR_MISSING_ATTRIBUTE
,
968 _("exactly one of 'type', 'enum' or 'flags' must "
969 "be specified as an attribute to <key>"));
973 if (type_string
== NULL
) /* flags or enums was specified */
975 EnumState
*enum_state
;
978 enum_state
= g_hash_table_lookup (enum_table
, enum_type
);
980 enum_state
= g_hash_table_lookup (flags_table
, flags_type
);
983 if (enum_state
== NULL
)
985 g_set_error (error
, G_MARKUP_ERROR
,
986 G_MARKUP_ERROR_INVALID_CONTENT
,
987 _("<%s id='%s'> not (yet) defined."),
988 flags_type
? "flags" : "enum",
989 flags_type
? flags_type
: enum_type
);
993 type_string
= flags_type
? "as" : "s";
994 strinfo
= enum_state
->strinfo
;
998 if (!g_variant_type_string_is_valid (type_string
))
1000 g_set_error (error
, G_MARKUP_ERROR
,
1001 G_MARKUP_ERROR_INVALID_CONTENT
,
1002 _("invalid GVariant type string '%s'"), type_string
);
1009 key
= key_state_new (type_string
, state
->gettext_domain
,
1010 enum_type
!= NULL
, flags_type
!= NULL
, strinfo
);
1011 g_hash_table_insert (state
->keys
, g_strdup (name
), key
);
1017 schema_state_add_override (SchemaState
*state
,
1018 KeyState
**key_state
,
1022 const gchar
*context
,
1025 SchemaState
*parent
;
1028 if (state
->extends
== NULL
)
1030 g_set_error_literal (error
, G_MARKUP_ERROR
,
1031 G_MARKUP_ERROR_INVALID_CONTENT
,
1032 _("<override> given but schema isn't "
1033 "extending anything"));
1037 for (parent
= state
->extends
; parent
; parent
= parent
->extends
)
1038 if ((original
= g_hash_table_lookup (parent
->keys
, key
)))
1041 if (original
== NULL
)
1043 g_set_error (error
, G_MARKUP_ERROR
,
1044 G_MARKUP_ERROR_INVALID_CONTENT
,
1045 _("no <key name='%s'> to override"), key
);
1049 if (g_hash_table_lookup (state
->keys
, key
))
1051 g_set_error (error
, G_MARKUP_ERROR
,
1052 G_MARKUP_ERROR_INVALID_CONTENT
,
1053 _("<override name='%s'> already specified"), key
);
1057 *key_state
= key_state_override (original
, state
->gettext_domain
);
1058 *string
= key_state_start_default (*key_state
, l10n
, context
, error
);
1059 g_hash_table_insert (state
->keys
, g_strdup (key
), *key_state
);
1063 override_state_end (KeyState
**key_state
,
1067 key_state_end_default (*key_state
, string
, error
);
1071 /* Handling of toplevel state {{{1 */
1074 gboolean strict
; /* TRUE if --strict was given */
1076 GHashTable
*schema_table
; /* string -> SchemaState */
1077 GHashTable
*flags_table
; /* string -> EnumState */
1078 GHashTable
*enum_table
; /* string -> EnumState */
1080 GSList
*this_file_schemas
; /* strings: <schema>s in this file */
1081 GSList
*this_file_flagss
; /* strings: <flags>s in this file */
1082 GSList
*this_file_enums
; /* strings: <enum>s in this file */
1084 gchar
*schemalist_domain
; /* the <schemalist> gettext domain */
1086 SchemaState
*schema_state
; /* non-NULL when inside <schema> */
1087 KeyState
*key_state
; /* non-NULL when inside <key> */
1088 EnumState
*enum_state
; /* non-NULL when inside <enum> */
1090 GString
*string
; /* non-NULL when accepting text */
1094 is_subclass (const gchar
*class_name
,
1095 const gchar
*possible_parent
,
1096 GHashTable
*schema_table
)
1100 if (strcmp (class_name
, possible_parent
) == 0)
1103 class = g_hash_table_lookup (schema_table
, class_name
);
1104 g_assert (class != NULL
);
1106 return class->extends_name
&&
1107 is_subclass (class->extends_name
, possible_parent
, schema_table
);
1111 parse_state_start_schema (ParseState
*state
,
1114 const gchar
*gettext_domain
,
1115 const gchar
*extends_name
,
1116 const gchar
*list_of
,
1119 SchemaState
*extends
;
1122 if (g_hash_table_lookup (state
->schema_table
, id
))
1124 g_set_error (error
, G_MARKUP_ERROR
,
1125 G_MARKUP_ERROR_INVALID_CONTENT
,
1126 _("<schema id='%s'> already specified"), id
);
1132 extends
= g_hash_table_lookup (state
->schema_table
, extends_name
);
1134 if (extends
== NULL
)
1136 g_set_error (error
, G_MARKUP_ERROR
,
1137 G_MARKUP_ERROR_INVALID_CONTENT
,
1138 _("<schema id='%s'> extends not yet existing "
1139 "schema '%s'"), id
, extends_name
);
1150 if (!(tmp
= g_hash_table_lookup (state
->schema_table
, list_of
)))
1152 g_set_error (error
, G_MARKUP_ERROR
,
1153 G_MARKUP_ERROR_INVALID_CONTENT
,
1154 _("<schema id='%s'> is list of not yet existing "
1155 "schema '%s'"), id
, list_of
);
1161 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1162 _("Can not be a list of a schema with a path"));
1171 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1172 _("Can not extend a schema with a path"));
1178 if (extends
->list_of
== NULL
)
1180 g_set_error (error
, G_MARKUP_ERROR
,
1181 G_MARKUP_ERROR_INVALID_CONTENT
,
1182 _("<schema id='%s'> is a list, extending "
1183 "<schema id='%s'> which is not a list"),
1188 if (!is_subclass (list_of
, extends
->list_of
, state
->schema_table
))
1190 g_set_error (error
, G_MARKUP_ERROR
,
1191 G_MARKUP_ERROR_INVALID_CONTENT
,
1192 _("<schema id='%s' list-of='%s'> extends <schema "
1193 "id='%s' list-of='%s'> but '%s' does not "
1194 "extend '%s'"), id
, list_of
, extends_name
,
1195 extends
->list_of
, list_of
, extends
->list_of
);
1200 /* by default we are a list of the same thing that the schema
1201 * we are extending is a list of (which might be nothing)
1203 list_of
= extends
->list_of
;
1206 if (path
&& !(g_str_has_prefix (path
, "/") && g_str_has_suffix (path
, "/")))
1208 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1209 _("a path, if given, must begin and end with a slash"));
1213 if (path
&& list_of
&& !g_str_has_suffix (path
, ":/"))
1215 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1216 _("the path of a list must end with ':/'"));
1220 if (path
&& (g_str_has_prefix (path
, "/apps/") ||
1221 g_str_has_prefix (path
, "/desktop/") ||
1222 g_str_has_prefix (path
, "/system/")))
1223 g_printerr ("warning: Schema '%s' has path '%s'. Paths starting with "
1224 "'/apps/', '/desktop/' or '/system/' are deprecated.\n", id
, path
);
1226 state
->schema_state
= schema_state_new (path
, gettext_domain
,
1227 extends
, extends_name
, list_of
);
1229 my_id
= g_strdup (id
);
1230 state
->this_file_schemas
= g_slist_prepend (state
->this_file_schemas
, my_id
);
1231 g_hash_table_insert (state
->schema_table
, my_id
, state
->schema_state
);
1235 parse_state_start_enum (ParseState
*state
,
1240 GSList
**list
= is_flags
? &state
->this_file_flagss
: &state
->this_file_enums
;
1241 GHashTable
*table
= is_flags
? state
->flags_table
: state
->enum_table
;
1244 if (g_hash_table_lookup (table
, id
))
1246 g_set_error (error
, G_MARKUP_ERROR
,
1247 G_MARKUP_ERROR_INVALID_CONTENT
,
1248 _("<%s id='%s'> already specified"),
1249 is_flags
? "flags" : "enum", id
);
1253 state
->enum_state
= enum_state_new (is_flags
);
1255 my_id
= g_strdup (id
);
1256 *list
= g_slist_prepend (*list
, my_id
);
1257 g_hash_table_insert (table
, my_id
, state
->enum_state
);
1260 /* GMarkup Parser Functions {{{1 */
1262 /* Start element {{{2 */
1264 start_element (GMarkupParseContext
*context
,
1265 const gchar
*element_name
,
1266 const gchar
**attribute_names
,
1267 const gchar
**attribute_values
,
1271 ParseState
*state
= user_data
;
1272 const GSList
*element_stack
;
1273 const gchar
*container
;
1275 element_stack
= g_markup_parse_context_get_element_stack (context
);
1276 container
= element_stack
->next
? element_stack
->next
->data
: NULL
;
1278 #define COLLECT(first, ...) \
1279 g_markup_collect_attributes (element_name, \
1280 attribute_names, attribute_values, error, \
1281 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1282 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1283 #define STRDUP G_MARKUP_COLLECT_STRDUP
1284 #define STRING G_MARKUP_COLLECT_STRING
1285 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1287 /* Toplevel items {{{3 */
1288 if (container
== NULL
)
1290 if (strcmp (element_name
, "schemalist") == 0)
1292 COLLECT (OPTIONAL
| STRDUP
,
1294 &state
->schemalist_domain
);
1300 /* children of <schemalist> {{{3 */
1301 else if (strcmp (container
, "schemalist") == 0)
1303 if (strcmp (element_name
, "schema") == 0)
1305 const gchar
*id
, *path
, *gettext_domain
, *extends
, *list_of
;
1306 if (COLLECT (STRING
, "id", &id
,
1307 OPTIONAL
| STRING
, "path", &path
,
1308 OPTIONAL
| STRING
, "gettext-domain", &gettext_domain
,
1309 OPTIONAL
| STRING
, "extends", &extends
,
1310 OPTIONAL
| STRING
, "list-of", &list_of
))
1311 parse_state_start_schema (state
, id
, path
,
1312 gettext_domain
? gettext_domain
1313 : state
->schemalist_domain
,
1314 extends
, list_of
, error
);
1318 else if (strcmp (element_name
, "enum") == 0)
1321 if (COLLECT (STRING
, "id", &id
))
1322 parse_state_start_enum (state
, id
, FALSE
, error
);
1326 else if (strcmp (element_name
, "flags") == 0)
1329 if (COLLECT (STRING
, "id", &id
))
1330 parse_state_start_enum (state
, id
, TRUE
, error
);
1336 /* children of <schema> {{{3 */
1337 else if (strcmp (container
, "schema") == 0)
1339 if (strcmp (element_name
, "key") == 0)
1341 const gchar
*name
, *type_string
, *enum_type
, *flags_type
;
1343 if (COLLECT (STRING
, "name", &name
,
1344 OPTIONAL
| STRING
, "type", &type_string
,
1345 OPTIONAL
| STRING
, "enum", &enum_type
,
1346 OPTIONAL
| STRING
, "flags", &flags_type
))
1348 state
->key_state
= schema_state_add_key (state
->schema_state
,
1352 enum_type
, flags_type
,
1356 else if (strcmp (element_name
, "child") == 0)
1358 const gchar
*name
, *schema
;
1360 if (COLLECT (STRING
, "name", &name
, STRING
, "schema", &schema
))
1361 schema_state_add_child (state
->schema_state
,
1362 name
, schema
, error
);
1365 else if (strcmp (element_name
, "override") == 0)
1367 const gchar
*name
, *l10n
, *context
;
1369 if (COLLECT (STRING
, "name", &name
,
1370 OPTIONAL
| STRING
, "l10n", &l10n
,
1371 OPTIONAL
| STRING
, "context", &context
))
1372 schema_state_add_override (state
->schema_state
,
1373 &state
->key_state
, &state
->string
,
1374 name
, l10n
, context
, error
);
1379 /* children of <key> {{{3 */
1380 else if (strcmp (container
, "key") == 0)
1382 if (strcmp (element_name
, "default") == 0)
1384 const gchar
*l10n
, *context
;
1385 if (COLLECT (STRING
| OPTIONAL
, "l10n", &l10n
,
1386 STRING
| OPTIONAL
, "context", &context
))
1387 state
->string
= key_state_start_default (state
->key_state
,
1388 l10n
, context
, error
);
1392 else if (strcmp (element_name
, "summary") == 0)
1396 if (state
->key_state
->summary_seen
&& state
->strict
)
1397 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1398 _("Only one <%s> element allowed inside <%s>"),
1399 element_name
, container
);
1401 state
->string
= g_string_new (NULL
);
1403 state
->key_state
->summary_seen
= TRUE
;
1408 else if (strcmp (element_name
, "description") == 0)
1412 if (state
->key_state
->description_seen
&& state
->strict
)
1413 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1414 _("Only one <%s> element allowed inside <%s>"),
1415 element_name
, container
);
1417 state
->string
= g_string_new (NULL
);
1419 state
->key_state
->description_seen
= TRUE
;
1424 else if (strcmp (element_name
, "range") == 0)
1426 const gchar
*min
, *max
;
1427 if (COLLECT (STRING
| OPTIONAL
, "min", &min
,
1428 STRING
| OPTIONAL
, "max", &max
))
1429 key_state_set_range (state
->key_state
, min
, max
, error
);
1433 else if (strcmp (element_name
, "choices") == 0)
1436 key_state_start_choices (state
->key_state
, error
);
1440 else if (strcmp (element_name
, "aliases") == 0)
1443 key_state_start_aliases (state
->key_state
, error
);
1449 /* children of <choices> {{{3 */
1450 else if (strcmp (container
, "choices") == 0)
1452 if (strcmp (element_name
, "choice") == 0)
1455 if (COLLECT (STRING
, "value", &value
))
1456 key_state_add_choice (state
->key_state
, value
, error
);
1462 /* children of <aliases> {{{3 */
1463 else if (strcmp (container
, "aliases") == 0)
1465 if (strcmp (element_name
, "alias") == 0)
1467 const gchar
*value
, *target
;
1468 if (COLLECT (STRING
, "value", &value
, STRING
, "target", &target
))
1469 key_state_add_alias (state
->key_state
, value
, target
, error
);
1475 /* children of <enum> {{{3 */
1476 else if (strcmp (container
, "enum") == 0 ||
1477 strcmp (container
, "flags") == 0)
1479 if (strcmp (element_name
, "value") == 0)
1481 const gchar
*nick
, *valuestr
;
1482 if (COLLECT (STRING
, "nick", &nick
,
1483 STRING
, "value", &valuestr
))
1484 enum_state_add_value (state
->enum_state
, nick
, valuestr
, error
);
1491 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1492 _("Element <%s> not allowed inside <%s>"),
1493 element_name
, container
);
1495 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1496 _("Element <%s> not allowed at the top level"), element_name
);
1499 /* End element {{{2 */
1502 key_state_end (KeyState
**state_ptr
,
1510 if (state
->default_value
== NULL
)
1512 g_set_error_literal (error
,
1513 G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1514 "element <default> is required in <key>");
1520 schema_state_end (SchemaState
**state_ptr
,
1527 end_element (GMarkupParseContext
*context
,
1528 const gchar
*element_name
,
1532 ParseState
*state
= user_data
;
1534 if (strcmp (element_name
, "schemalist") == 0)
1536 g_free (state
->schemalist_domain
);
1537 state
->schemalist_domain
= NULL
;
1540 else if (strcmp (element_name
, "enum") == 0 ||
1541 strcmp (element_name
, "flags") == 0)
1542 enum_state_end (&state
->enum_state
, error
);
1544 else if (strcmp (element_name
, "schema") == 0)
1545 schema_state_end (&state
->schema_state
, error
);
1547 else if (strcmp (element_name
, "override") == 0)
1548 override_state_end (&state
->key_state
, &state
->string
, error
);
1550 else if (strcmp (element_name
, "key") == 0)
1551 key_state_end (&state
->key_state
, error
);
1553 else if (strcmp (element_name
, "default") == 0)
1554 key_state_end_default (state
->key_state
, &state
->string
, error
);
1556 else if (strcmp (element_name
, "choices") == 0)
1557 key_state_end_choices (state
->key_state
, error
);
1559 else if (strcmp (element_name
, "aliases") == 0)
1560 key_state_end_aliases (state
->key_state
, error
);
1564 g_string_free (state
->string
, TRUE
);
1565 state
->string
= NULL
;
1570 text (GMarkupParseContext
*context
,
1576 ParseState
*state
= user_data
;
1580 /* we are expecting a string, so store the text data.
1582 * we store the data verbatim here and deal with whitespace
1583 * later on. there are two reasons for that:
1585 * 1) whitespace is handled differently depending on the tag
1588 * 2) we could do leading whitespace removal by refusing to
1589 * insert it into state->string if it's at the start, but for
1590 * trailing whitespace, we have no idea if there is another
1591 * text() call coming or not.
1593 g_string_append_len (state
->string
, text
, text_len
);
1597 /* string is not expected: accept (and ignore) pure whitespace */
1600 for (i
= 0; i
< text_len
; i
++)
1601 if (!g_ascii_isspace (text
[i
]))
1603 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1604 _("text may not appear inside <%s>"),
1605 g_markup_parse_context_get_element (context
));
1611 /* Write to GVDB {{{1 */
1619 gvdb_pair_init (GvdbPair
*pair
)
1621 pair
->table
= gvdb_hash_table_new (NULL
, NULL
);
1622 pair
->root
= gvdb_hash_table_insert (pair
->table
, "");
1626 gvdb_pair_clear (GvdbPair
*pair
)
1628 g_hash_table_unref (pair
->table
);
1633 GHashTable
*schema_table
;
1639 GHashTable
*schema_table
;
1645 output_key (gpointer key
,
1649 OutputSchemaData
*data
;
1653 GVariant
*serialised
= NULL
;
1659 item
= gvdb_hash_table_insert (data
->pair
.table
, name
);
1660 gvdb_item_set_parent (item
, data
->pair
.root
);
1661 serialised
= key_state_serialise (state
);
1662 gvdb_item_set_value (item
, serialised
);
1663 g_variant_unref (serialised
);
1668 if (state
->child_schema
&&
1669 !g_hash_table_lookup (data
->schema_table
, state
->child_schema
))
1670 g_printerr ("warning: undefined reference to <schema id='%s'/>\n",
1671 state
->child_schema
);
1675 output_schema (gpointer key
,
1679 WriteToFileData
*wtf_data
= user_data
;
1680 OutputSchemaData data
;
1681 GvdbPair
*root_pair
;
1688 root_pair
= &wtf_data
->root_pair
;
1690 data
.schema_table
= wtf_data
->schema_table
;
1691 gvdb_pair_init (&data
.pair
);
1694 item
= gvdb_hash_table_insert (root_pair
->table
, id
);
1695 gvdb_item_set_parent (item
, root_pair
->root
);
1696 gvdb_item_set_hash_table (item
, data
.pair
.table
);
1698 g_hash_table_foreach (state
->keys
, output_key
, &data
);
1701 gvdb_hash_table_insert_string (data
.pair
.table
, ".path", state
->path
);
1703 if (state
->extends_name
)
1704 gvdb_hash_table_insert_string (data
.pair
.table
, ".extends",
1705 state
->extends_name
);
1708 gvdb_hash_table_insert_string (data
.pair
.table
, ".list-of",
1712 gvdb_hash_table_insert_string (data
.pair
.table
,
1714 state
->gettext_domain
);
1716 gvdb_pair_clear (&data
.pair
);
1720 write_to_file (GHashTable
*schema_table
,
1721 const gchar
*filename
,
1724 WriteToFileData data
;
1727 data
.schema_table
= schema_table
;
1729 gvdb_pair_init (&data
.root_pair
);
1731 g_hash_table_foreach (schema_table
, output_schema
, &data
);
1733 success
= gvdb_table_write_contents (data
.root_pair
.table
, filename
,
1734 G_BYTE_ORDER
!= G_LITTLE_ENDIAN
,
1736 g_hash_table_unref (data
.root_pair
.table
);
1741 /* Parser driver {{{1 */
1743 parse_gschema_files (gchar
**files
,
1746 GMarkupParser parser
= { start_element
, end_element
, text
};
1747 ParseState state
= { 0, };
1748 const gchar
*filename
;
1749 GError
*error
= NULL
;
1751 state
.strict
= strict
;
1753 state
.enum_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1754 g_free
, enum_state_free
);
1756 state
.flags_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1757 g_free
, enum_state_free
);
1759 state
.schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1760 g_free
, schema_state_free
);
1762 while ((filename
= *files
++) != NULL
)
1764 GMarkupParseContext
*context
;
1769 if (!g_file_get_contents (filename
, &contents
, &size
, &error
))
1771 fprintf (stderr
, "%s\n", error
->message
);
1772 g_clear_error (&error
);
1776 context
= g_markup_parse_context_new (&parser
,
1777 G_MARKUP_TREAT_CDATA_AS_TEXT
|
1778 G_MARKUP_PREFIX_ERROR_POSITION
|
1779 G_MARKUP_IGNORE_QUALIFIED
,
1783 if (!g_markup_parse_context_parse (context
, contents
, size
, &error
) ||
1784 !g_markup_parse_context_end_parse (context
, &error
))
1788 /* back out any changes from this file */
1789 for (item
= state
.this_file_schemas
; item
; item
= item
->next
)
1790 g_hash_table_remove (state
.schema_table
, item
->data
);
1792 for (item
= state
.this_file_flagss
; item
; item
= item
->next
)
1793 g_hash_table_remove (state
.flags_table
, item
->data
);
1795 for (item
= state
.this_file_enums
; item
; item
= item
->next
)
1796 g_hash_table_remove (state
.enum_table
, item
->data
);
1799 g_markup_parse_context_get_position (context
, &line
, &col
);
1800 fprintf (stderr
, "%s:%d:%d %s. ", filename
, line
, col
, error
->message
);
1801 g_clear_error (&error
);
1805 /* Translators: Do not translate "--strict". */
1806 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1807 g_hash_table_unref (state
.schema_table
);
1808 g_hash_table_unref (state
.flags_table
);
1809 g_hash_table_unref (state
.enum_table
);
1816 fprintf (stderr
, _("This entire file has been ignored.\n"));
1821 g_markup_parse_context_free (context
);
1822 g_slist_free (state
.this_file_schemas
);
1823 g_slist_free (state
.this_file_flagss
);
1824 g_slist_free (state
.this_file_enums
);
1825 state
.this_file_schemas
= NULL
;
1826 state
.this_file_flagss
= NULL
;
1827 state
.this_file_enums
= NULL
;
1830 g_hash_table_unref (state
.flags_table
);
1831 g_hash_table_unref (state
.enum_table
);
1833 return state
.schema_table
;
1837 compare_strings (gconstpointer a
,
1840 gchar
*one
= *(gchar
**) a
;
1841 gchar
*two
= *(gchar
**) b
;
1844 cmp
= g_str_has_suffix (two
, ".enums.xml") -
1845 g_str_has_suffix (one
, ".enums.xml");
1848 cmp
= strcmp (one
, two
);
1854 set_overrides (GHashTable
*schema_table
,
1858 const gchar
*filename
;
1859 GError
*error
= NULL
;
1861 while ((filename
= *files
++))
1867 key_file
= g_key_file_new ();
1868 if (!g_key_file_load_from_file (key_file
, filename
, 0, &error
))
1870 fprintf (stderr
, "%s: %s. ", filename
, error
->message
);
1871 g_key_file_free (key_file
);
1872 g_clear_error (&error
);
1876 fprintf (stderr
, _("Ignoring this file.\n"));
1880 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1884 groups
= g_key_file_get_groups (key_file
, NULL
);
1886 for (i
= 0; groups
[i
]; i
++)
1888 const gchar
*group
= groups
[i
];
1889 SchemaState
*schema
;
1893 schema
= g_hash_table_lookup (schema_table
, group
);
1896 /* Having the schema not be installed is expected to be a
1897 * common case. Don't even emit an error message about
1902 keys
= g_key_file_get_keys (key_file
, group
, NULL
, NULL
);
1903 g_assert (keys
!= NULL
);
1905 for (j
= 0; keys
[j
]; j
++)
1907 const gchar
*key
= keys
[j
];
1912 state
= g_hash_table_lookup (schema
->keys
, key
);
1916 fprintf (stderr
, _("No such key '%s' in schema '%s' as "
1917 "specified in override file '%s'"),
1918 key
, group
, filename
);
1922 fprintf (stderr
, _("; ignoring override for this key.\n"));
1926 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1927 g_key_file_free (key_file
);
1928 g_strfreev (groups
);
1934 string
= g_key_file_get_value (key_file
, group
, key
, NULL
);
1935 g_assert (string
!= NULL
);
1937 value
= g_variant_parse (state
->type
, string
,
1938 NULL
, NULL
, &error
);
1942 fprintf (stderr
, _("error parsing key '%s' in schema '%s' "
1943 "as specified in override file '%s': "
1945 key
, group
, filename
, error
->message
);
1947 g_clear_error (&error
);
1952 fprintf (stderr
, _("Ignoring override for this key.\n"));
1956 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1957 g_key_file_free (key_file
);
1958 g_strfreev (groups
);
1966 if (g_variant_compare (value
, state
->minimum
) < 0 ||
1967 g_variant_compare (value
, state
->maximum
) > 0)
1970 _("override for key '%s' in schema '%s' in "
1971 "override file '%s' is outside the range "
1972 "given in the schema"),
1973 key
, group
, filename
);
1975 g_variant_unref (value
);
1980 fprintf (stderr
, _("; ignoring override for this key.\n"));
1984 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1985 g_key_file_free (key_file
);
1986 g_strfreev (groups
);
1993 else if (state
->strinfo
->len
)
1995 if (!is_valid_choices (value
, state
->strinfo
))
1998 _("override for key '%s' in schema '%s' in "
1999 "override file '%s' is not in the list "
2000 "of valid choices"),
2001 key
, group
, filename
);
2003 g_variant_unref (value
);
2008 fprintf (stderr
, _("; ignoring override for this key.\n"));
2012 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2013 g_key_file_free (key_file
);
2014 g_strfreev (groups
);
2021 g_variant_unref (state
->default_value
);
2022 state
->default_value
= value
;
2029 g_strfreev (groups
);
2036 main (int argc
, char **argv
)
2038 GError
*error
= NULL
;
2039 GHashTable
*table
= NULL
;
2042 const gchar
*srcdir
;
2043 gboolean show_version_and_exit
= FALSE
;
2044 gchar
*targetdir
= NULL
;
2045 gchar
*target
= NULL
;
2046 gboolean dry_run
= FALSE
;
2047 gboolean strict
= FALSE
;
2048 gchar
**schema_files
= NULL
;
2049 gchar
**override_files
= NULL
;
2050 GOptionContext
*context
= NULL
;
2052 GOptionEntry entries
[] = {
2053 { "version", 0, 0, G_OPTION_ARG_NONE
, &show_version_and_exit
, N_("Show program version and exit"), NULL
},
2054 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME
, &targetdir
, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2055 { "strict", 0, 0, G_OPTION_ARG_NONE
, &strict
, N_("Abort on any errors in schemas"), NULL
},
2056 { "dry-run", 0, 0, G_OPTION_ARG_NONE
, &dry_run
, N_("Do not write the gschema.compiled file"), NULL
},
2057 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE
, &allow_any_name
, N_("Do not enforce key name restrictions") },
2059 /* These options are only for use in the gschema-compile tests */
2060 { "schema-file", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_FILENAME_ARRAY
, &schema_files
, NULL
, NULL
},
2068 setlocale (LC_ALL
, "");
2069 textdomain (GETTEXT_PACKAGE
);
2072 tmp
= _glib_get_locale_dir ();
2073 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
2075 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
2078 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2079 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
2082 context
= g_option_context_new (N_("DIRECTORY"));
2083 g_option_context_set_translation_domain (context
, GETTEXT_PACKAGE
);
2084 g_option_context_set_summary (context
,
2085 N_("Compile all GSettings schema files into a schema cache.\n"
2086 "Schema files are required to have the extension .gschema.xml,\n"
2087 "and the cache file is called gschemas.compiled."));
2088 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
2090 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
2092 fprintf (stderr
, "%s\n", error
->message
);
2097 if (show_version_and_exit
)
2099 g_print (PACKAGE_VERSION
"\n");
2104 if (!schema_files
&& argc
!= 2)
2106 fprintf (stderr
, _("You should give exactly one directory name\n"));
2113 target
= g_build_filename (targetdir
? targetdir
: srcdir
, "gschemas.compiled", NULL
);
2117 GPtrArray
*overrides
;
2120 files
= g_ptr_array_new ();
2121 overrides
= g_ptr_array_new ();
2123 dir
= g_dir_open (srcdir
, 0, &error
);
2126 fprintf (stderr
, "%s\n", error
->message
);
2128 g_ptr_array_unref (files
);
2129 g_ptr_array_unref (overrides
);
2135 while ((file
= g_dir_read_name (dir
)) != NULL
)
2137 if (g_str_has_suffix (file
, ".gschema.xml") ||
2138 g_str_has_suffix (file
, ".enums.xml"))
2139 g_ptr_array_add (files
, g_build_filename (srcdir
, file
, NULL
));
2141 else if (g_str_has_suffix (file
, ".gschema.override"))
2142 g_ptr_array_add (overrides
,
2143 g_build_filename (srcdir
, file
, NULL
));
2146 if (files
->len
== 0)
2148 fprintf (stdout
, _("No schema files found: "));
2150 if (g_unlink (target
))
2151 fprintf (stdout
, _("doing nothing.\n"));
2154 fprintf (stdout
, _("removed existing output file.\n"));
2156 g_ptr_array_unref (files
);
2157 g_ptr_array_unref (overrides
);
2162 g_ptr_array_sort (files
, compare_strings
);
2163 g_ptr_array_add (files
, NULL
);
2165 g_ptr_array_sort (overrides
, compare_strings
);
2166 g_ptr_array_add (overrides
, NULL
);
2168 schema_files
= (char **) g_ptr_array_free (files
, FALSE
);
2169 override_files
= (gchar
**) g_ptr_array_free (overrides
, FALSE
);
2172 if ((table
= parse_gschema_files (schema_files
, strict
)) == NULL
)
2178 if (override_files
!= NULL
&&
2179 !set_overrides (table
, override_files
, strict
))
2185 if (!dry_run
&& !write_to_file (table
, target
, &error
))
2187 fprintf (stderr
, "%s\n", error
->message
);
2196 g_clear_error (&error
);
2197 g_clear_pointer (&table
, g_hash_table_unref
);
2198 g_clear_pointer (&dir
, g_dir_close
);
2201 g_strfreev (schema_files
);
2202 g_strfreev (override_files
);
2203 g_option_context_free (context
);
2214 /* vim:set foldmethod=marker: */