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
;
182 GVariantDict
*desktop_overrides
;
191 gboolean has_choices
;
192 gboolean has_aliases
;
193 gboolean is_override
;
196 GVariant
*serialised
;
198 gboolean summary_seen
;
199 gboolean description_seen
;
203 key_state_new (const gchar
*type_string
,
204 const gchar
*gettext_domain
,
211 state
= g_slice_new0 (KeyState
);
212 state
->type
= g_variant_type_new (type_string
);
213 state
->have_gettext_domain
= gettext_domain
!= NULL
;
214 state
->is_enum
= is_enum
;
215 state
->is_flags
= is_flags
;
216 state
->summary_seen
= FALSE
;
217 state
->description_seen
= FALSE
;
220 state
->strinfo
= g_string_new_len (strinfo
->str
, strinfo
->len
);
222 state
->strinfo
= g_string_new (NULL
);
228 key_state_override (KeyState
*state
,
229 const gchar
*gettext_domain
)
233 copy
= g_slice_new0 (KeyState
);
234 copy
->type
= g_variant_type_copy (state
->type
);
235 copy
->have_gettext_domain
= gettext_domain
!= NULL
;
236 copy
->strinfo
= g_string_new_len (state
->strinfo
->str
,
237 state
->strinfo
->len
);
238 copy
->is_enum
= state
->is_enum
;
239 copy
->is_flags
= state
->is_flags
;
240 copy
->is_override
= TRUE
;
244 copy
->minimum
= g_variant_ref (state
->minimum
);
245 copy
->maximum
= g_variant_ref (state
->maximum
);
252 key_state_new_child (const gchar
*child_schema
)
256 state
= g_slice_new0 (KeyState
);
257 state
->child_schema
= g_strdup (child_schema
);
263 is_valid_choices (GVariant
*variant
,
266 switch (g_variant_classify (variant
))
268 case G_VARIANT_CLASS_MAYBE
:
269 case G_VARIANT_CLASS_ARRAY
:
271 gboolean valid
= TRUE
;
274 g_variant_iter_init (&iter
, variant
);
276 while (valid
&& (variant
= g_variant_iter_next_value (&iter
)))
278 valid
= is_valid_choices (variant
, strinfo
);
279 g_variant_unref (variant
);
285 case G_VARIANT_CLASS_STRING
:
286 return strinfo_is_string_valid ((const guint32
*) strinfo
->str
,
288 g_variant_get_string (variant
, NULL
));
291 g_assert_not_reached ();
296 /* Gets called at </default> </choices> or <range/> to check for
297 * validity of the default value so that any inconsistency is
298 * reported as soon as it is encountered.
301 key_state_check_range (KeyState
*state
,
304 if (state
->default_value
)
308 tag
= state
->is_override
? "override" : "default";
312 if (g_variant_compare (state
->default_value
, state
->minimum
) < 0 ||
313 g_variant_compare (state
->default_value
, state
->maximum
) > 0)
315 g_set_error (error
, G_MARKUP_ERROR
,
316 G_MARKUP_ERROR_INVALID_CONTENT
,
317 _("<%s> is not contained in "
318 "the specified range"), tag
);
322 else if (state
->strinfo
->len
)
324 if (!is_valid_choices (state
->default_value
, state
->strinfo
))
327 g_set_error (error
, G_MARKUP_ERROR
,
328 G_MARKUP_ERROR_INVALID_CONTENT
,
329 _("<%s> is not a valid member of "
330 "the specified enumerated type"), tag
);
332 else if (state
->is_flags
)
333 g_set_error (error
, G_MARKUP_ERROR
,
334 G_MARKUP_ERROR_INVALID_CONTENT
,
335 _("<%s> contains string not in the "
336 "specified flags type"), tag
);
339 g_set_error (error
, G_MARKUP_ERROR
,
340 G_MARKUP_ERROR_INVALID_CONTENT
,
341 _("<%s> contains a string not in "
349 key_state_set_range (KeyState
*state
,
350 const gchar
*min_str
,
351 const gchar
*max_str
,
360 { 'n', "-32768", "32767" },
361 { 'q', "0", "65535" },
362 { 'i', "-2147483648", "2147483647" },
363 { 'u', "0", "4294967295" },
364 { 'x', "-9223372036854775808", "9223372036854775807" },
365 { 't', "0", "18446744073709551615" },
366 { 'd', "-inf", "inf" },
368 gboolean type_ok
= FALSE
;
373 g_set_error_literal (error
, G_MARKUP_ERROR
,
374 G_MARKUP_ERROR_INVALID_CONTENT
,
375 _("<range/> already specified for this key"));
379 for (i
= 0; i
< G_N_ELEMENTS (table
); i
++)
380 if (*(char *) state
->type
== table
[i
].type
)
382 min_str
= min_str
? min_str
: table
[i
].min
;
383 max_str
= max_str
? max_str
: table
[i
].max
;
390 gchar
*type
= g_variant_type_dup_string (state
->type
);
391 g_set_error (error
, G_MARKUP_ERROR
,
392 G_MARKUP_ERROR_INVALID_CONTENT
,
393 _("<range> not allowed for keys of type “%s”"), type
);
398 state
->minimum
= g_variant_parse (state
->type
, min_str
, NULL
, NULL
, error
);
399 if (state
->minimum
== NULL
)
402 state
->maximum
= g_variant_parse (state
->type
, max_str
, NULL
, NULL
, error
);
403 if (state
->maximum
== NULL
)
406 if (g_variant_compare (state
->minimum
, state
->maximum
) > 0)
408 g_set_error (error
, G_MARKUP_ERROR
,
409 G_MARKUP_ERROR_INVALID_CONTENT
,
410 _("<range> specified minimum is greater than maximum"));
414 key_state_check_range (state
, error
);
418 key_state_start_default (KeyState
*state
,
420 const gchar
*context
,
425 if (strcmp (l10n
, "messages") == 0)
428 else if (strcmp (l10n
, "time") == 0)
433 g_set_error (error
, G_MARKUP_ERROR
,
434 G_MARKUP_ERROR_INVALID_CONTENT
,
435 _("unsupported l10n category: %s"), l10n
);
439 if (!state
->have_gettext_domain
)
441 g_set_error_literal (error
, G_MARKUP_ERROR
,
442 G_MARKUP_ERROR_INVALID_CONTENT
,
443 _("l10n requested, but no "
444 "gettext domain given"));
448 state
->l10n_context
= g_strdup (context
);
451 else if (context
!= NULL
)
453 g_set_error_literal (error
, G_MARKUP_ERROR
,
454 G_MARKUP_ERROR_INVALID_CONTENT
,
455 _("translation context given for "
456 "value without l10n enabled"));
460 return g_string_new (NULL
);
464 key_state_end_default (KeyState
*state
,
468 state
->unparsed_default_value
= *string
;
471 state
->default_value
= g_variant_parse (state
->type
,
472 state
->unparsed_default_value
->str
,
474 if (!state
->default_value
)
476 gchar
*type
= g_variant_type_dup_string (state
->type
);
477 g_prefix_error (error
, _("Failed to parse <default> value of type “%s”: "), type
);
481 key_state_check_range (state
, error
);
485 key_state_start_choices (KeyState
*state
,
488 const GVariantType
*type
= state
->type
;
492 g_set_error_literal (error
, G_MARKUP_ERROR
,
493 G_MARKUP_ERROR_INVALID_CONTENT
,
494 _("<choices> cannot be specified for keys "
495 "tagged as having an enumerated type"));
499 if (state
->has_choices
)
501 g_set_error_literal (error
, G_MARKUP_ERROR
,
502 G_MARKUP_ERROR_INVALID_CONTENT
,
503 _("<choices> already specified for this key"));
507 while (g_variant_type_is_maybe (type
) || g_variant_type_is_array (type
))
508 type
= g_variant_type_element (type
);
510 if (!g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
))
512 gchar
*type_string
= g_variant_type_dup_string (state
->type
);
513 g_set_error (error
, G_MARKUP_ERROR
,
514 G_MARKUP_ERROR_INVALID_CONTENT
,
515 _("<choices> not allowed for keys of type “%s”"),
517 g_free (type_string
);
523 key_state_add_choice (KeyState
*state
,
527 if (strinfo_builder_contains (state
->strinfo
, choice
))
529 g_set_error (error
, G_MARKUP_ERROR
,
530 G_MARKUP_ERROR_INVALID_CONTENT
,
531 _("<choice value='%s'/> already given"), choice
);
535 strinfo_builder_append_item (state
->strinfo
, choice
, 0);
536 state
->has_choices
= TRUE
;
540 key_state_end_choices (KeyState
*state
,
543 if (!state
->has_choices
)
545 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
546 _("<choices> must contain at least one <choice>"));
550 key_state_check_range (state
, error
);
554 key_state_start_aliases (KeyState
*state
,
557 if (state
->has_aliases
)
558 g_set_error_literal (error
, G_MARKUP_ERROR
,
559 G_MARKUP_ERROR_INVALID_CONTENT
,
560 _("<aliases> already specified for this key"));
561 else if (!state
->is_flags
&& !state
->is_enum
&& !state
->has_choices
)
562 g_set_error_literal (error
, G_MARKUP_ERROR
,
563 G_MARKUP_ERROR_INVALID_CONTENT
,
564 _("<aliases> can only be specified for keys with "
565 "enumerated or flags types or after <choices>"));
569 key_state_add_alias (KeyState
*state
,
574 if (strinfo_builder_contains (state
->strinfo
, alias
))
576 if (strinfo_is_string_valid ((guint32
*) state
->strinfo
->str
,
577 state
->strinfo
->len
/ 4,
581 g_set_error (error
, G_MARKUP_ERROR
,
582 G_MARKUP_ERROR_INVALID_CONTENT
,
583 _("<alias value='%s'/> given when “%s” is already "
584 "a member of the enumerated type"), alias
, alias
);
587 g_set_error (error
, G_MARKUP_ERROR
,
588 G_MARKUP_ERROR_INVALID_CONTENT
,
589 _("<alias value='%s'/> given when "
590 "<choice value='%s'/> was already given"),
595 g_set_error (error
, G_MARKUP_ERROR
,
596 G_MARKUP_ERROR_INVALID_CONTENT
,
597 _("<alias value='%s'/> already specified"), alias
);
602 if (!strinfo_builder_append_alias (state
->strinfo
, alias
, target
))
604 g_set_error (error
, G_MARKUP_ERROR
,
605 G_MARKUP_ERROR_INVALID_CONTENT
,
607 _("alias target “%s” is not in enumerated type") :
608 _("alias target “%s” is not in <choices>"),
613 state
->has_aliases
= TRUE
;
617 key_state_end_aliases (KeyState
*state
,
620 if (!state
->has_aliases
)
622 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
623 _("<aliases> must contain at least one <alias>"));
629 key_state_check (KeyState
*state
,
635 return state
->checked
= TRUE
;
639 key_state_serialise (KeyState
*state
)
641 if (state
->serialised
== NULL
)
643 if (state
->child_schema
)
645 state
->serialised
= g_variant_new_string (state
->child_schema
);
650 GVariantBuilder builder
;
652 g_assert (key_state_check (state
, NULL
));
654 g_variant_builder_init (&builder
, G_VARIANT_TYPE_TUPLE
);
657 g_variant_builder_add_value (&builder
, state
->default_value
);
662 /* We are going to store the untranslated default for
663 * runtime translation according to the current locale.
664 * We need to strip leading and trailing whitespace from
665 * the string so that it's exactly the same as the one
666 * that ended up in the .po file for translation.
668 * We want to do this so that
670 * <default l10n='messages'>
674 * ends up in the .po file like "['a', 'b', 'c']",
675 * omitting the extra whitespace at the start and end.
677 strip_string (state
->unparsed_default_value
);
679 if (state
->l10n_context
)
683 /* Contextified messages are supported by prepending
684 * the context, followed by '\004' to the start of the
685 * message string. We do that here to save GSettings
688 len
= strlen (state
->l10n_context
);
689 state
->l10n_context
[len
] = '\004';
690 g_string_prepend_len (state
->unparsed_default_value
,
691 state
->l10n_context
, len
+ 1);
692 g_free (state
->l10n_context
);
693 state
->l10n_context
= NULL
;
696 g_variant_builder_add (&builder
, "(y(y&s))", 'l', state
->l10n
,
697 state
->unparsed_default_value
->str
);
698 g_string_free (state
->unparsed_default_value
, TRUE
);
699 state
->unparsed_default_value
= NULL
;
702 /* choice, aliases, enums */
703 if (state
->strinfo
->len
)
711 data
= state
->strinfo
->str
;
712 size
= state
->strinfo
->len
;
715 for (i
= 0; i
< size
/ sizeof (guint32
); i
++)
716 words
[i
] = GUINT32_TO_LE (words
[i
]);
718 array
= g_variant_new_from_data (G_VARIANT_TYPE ("au"),
722 g_string_free (state
->strinfo
, FALSE
);
723 state
->strinfo
= NULL
;
725 g_variant_builder_add (&builder
, "(y@au)",
726 state
->is_flags
? 'f' :
727 state
->is_enum
? 'e' : 'c',
732 if (state
->minimum
|| state
->maximum
)
733 g_variant_builder_add (&builder
, "(y(**))", 'r',
734 state
->minimum
, state
->maximum
);
736 /* per-desktop overrides */
737 if (state
->desktop_overrides
)
738 g_variant_builder_add (&builder
, "(y@a{sv})", 'd',
739 g_variant_dict_end (state
->desktop_overrides
));
741 state
->serialised
= g_variant_builder_end (&builder
);
744 g_variant_ref_sink (state
->serialised
);
747 return g_variant_ref (state
->serialised
);
751 key_state_free (gpointer data
)
753 KeyState
*state
= data
;
755 g_free (state
->child_schema
);
758 g_variant_type_free (state
->type
);
760 g_free (state
->l10n_context
);
762 if (state
->unparsed_default_value
)
763 g_string_free (state
->unparsed_default_value
, TRUE
);
765 if (state
->default_value
)
766 g_variant_unref (state
->default_value
);
769 g_string_free (state
->strinfo
, TRUE
);
772 g_variant_unref (state
->minimum
);
775 g_variant_unref (state
->maximum
);
777 if (state
->serialised
)
778 g_variant_unref (state
->serialised
);
780 if (state
->desktop_overrides
)
781 g_variant_dict_unref (state
->desktop_overrides
);
783 g_slice_free (KeyState
, state
);
786 /* Key name validity {{{1 */
787 static gboolean allow_any_name
= FALSE
;
790 is_valid_keyname (const gchar
*key
,
797 g_set_error_literal (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
798 _("Empty names are not permitted"));
805 if (!g_ascii_islower (key
[0]))
807 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
808 _("Invalid name “%s”: names must begin "
809 "with a lowercase letter"), key
);
813 for (i
= 1; key
[i
]; i
++)
816 !g_ascii_islower (key
[i
]) &&
817 !g_ascii_isdigit (key
[i
]))
819 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
820 _("Invalid name “%s”: invalid character “%c”; "
821 "only lowercase letters, numbers and hyphen (“-”) "
822 "are permitted"), key
, key
[i
]);
826 if (key
[i
] == '-' && key
[i
+ 1] == '-')
828 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
829 _("Invalid name “%s”: two successive hyphens (“--”) "
830 "are not permitted"), key
);
835 if (key
[i
- 1] == '-')
837 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
838 _("Invalid name “%s”: the last character may not be a "
839 "hyphen (“-”)"), key
);
845 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
846 _("Invalid name “%s”: maximum length is 1024"), key
);
853 /* Handling of <schema> {{{1 */
854 typedef struct _SchemaState SchemaState
;
857 SchemaState
*extends
;
860 gchar
*gettext_domain
;
868 schema_state_new (const gchar
*path
,
869 const gchar
*gettext_domain
,
870 SchemaState
*extends
,
871 const gchar
*extends_name
,
872 const gchar
*list_of
)
876 state
= g_slice_new (SchemaState
);
877 state
->path
= g_strdup (path
);
878 state
->gettext_domain
= g_strdup (gettext_domain
);
879 state
->extends
= extends
;
880 state
->extends_name
= g_strdup (extends_name
);
881 state
->list_of
= g_strdup (list_of
);
882 state
->keys
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
883 g_free
, key_state_free
);
889 schema_state_free (gpointer data
)
891 SchemaState
*state
= data
;
893 g_free (state
->path
);
894 g_free (state
->gettext_domain
);
895 g_free (state
->extends_name
);
896 g_free (state
->list_of
);
897 g_hash_table_unref (state
->keys
);
898 g_slice_free (SchemaState
, state
);
902 schema_state_add_child (SchemaState
*state
,
909 if (!is_valid_keyname (name
, error
))
912 childname
= g_strconcat (name
, "/", NULL
);
914 if (g_hash_table_lookup (state
->keys
, childname
))
916 g_set_error (error
, G_MARKUP_ERROR
,
917 G_MARKUP_ERROR_INVALID_CONTENT
,
918 _("<child name='%s'> already specified"), name
);
922 g_hash_table_insert (state
->keys
, childname
,
923 key_state_new_child (schema
));
927 schema_state_add_key (SchemaState
*state
,
928 GHashTable
*enum_table
,
929 GHashTable
*flags_table
,
931 const gchar
*type_string
,
932 const gchar
*enum_type
,
933 const gchar
*flags_type
,
942 g_set_error_literal (error
, G_MARKUP_ERROR
,
943 G_MARKUP_ERROR_INVALID_CONTENT
,
944 _("Cannot add keys to a “list-of” schema"));
948 if (!is_valid_keyname (name
, error
))
951 if (g_hash_table_lookup (state
->keys
, name
))
953 g_set_error (error
, G_MARKUP_ERROR
,
954 G_MARKUP_ERROR_INVALID_CONTENT
,
955 _("<key name='%s'> already specified"), name
);
959 for (node
= state
; node
; node
= node
->extends
)
964 shadow
= g_hash_table_lookup (node
->extends
->keys
, name
);
966 /* in case of <key> <override> <key> make sure we report the
967 * location of the original <key>, not the <override>.
969 if (shadow
&& !shadow
->is_override
)
971 g_set_error (error
, G_MARKUP_ERROR
,
972 G_MARKUP_ERROR_INVALID_CONTENT
,
973 _("<key name='%s'> shadows <key name='%s'> in "
974 "<schema id='%s'>; use <override> to modify value"),
975 name
, name
, node
->extends_name
);
980 if ((type_string
!= NULL
) + (enum_type
!= NULL
) + (flags_type
!= NULL
) != 1)
982 g_set_error (error
, G_MARKUP_ERROR
,
983 G_MARKUP_ERROR_MISSING_ATTRIBUTE
,
984 _("Exactly one of “type”, “enum” or “flags” must "
985 "be specified as an attribute to <key>"));
989 if (type_string
== NULL
) /* flags or enums was specified */
991 EnumState
*enum_state
;
994 enum_state
= g_hash_table_lookup (enum_table
, enum_type
);
996 enum_state
= g_hash_table_lookup (flags_table
, flags_type
);
999 if (enum_state
== NULL
)
1001 g_set_error (error
, G_MARKUP_ERROR
,
1002 G_MARKUP_ERROR_INVALID_CONTENT
,
1003 _("<%s id='%s'> not (yet) defined."),
1004 flags_type
? "flags" : "enum",
1005 flags_type
? flags_type
: enum_type
);
1009 type_string
= flags_type
? "as" : "s";
1010 strinfo
= enum_state
->strinfo
;
1014 if (!g_variant_type_string_is_valid (type_string
))
1016 g_set_error (error
, G_MARKUP_ERROR
,
1017 G_MARKUP_ERROR_INVALID_CONTENT
,
1018 _("Invalid GVariant type string “%s”"), type_string
);
1025 key
= key_state_new (type_string
, state
->gettext_domain
,
1026 enum_type
!= NULL
, flags_type
!= NULL
, strinfo
);
1027 g_hash_table_insert (state
->keys
, g_strdup (name
), key
);
1033 schema_state_add_override (SchemaState
*state
,
1034 KeyState
**key_state
,
1038 const gchar
*context
,
1041 SchemaState
*parent
;
1044 if (state
->extends
== NULL
)
1046 g_set_error_literal (error
, G_MARKUP_ERROR
,
1047 G_MARKUP_ERROR_INVALID_CONTENT
,
1048 _("<override> given but schema isn’t "
1049 "extending anything"));
1053 for (parent
= state
->extends
; parent
; parent
= parent
->extends
)
1054 if ((original
= g_hash_table_lookup (parent
->keys
, key
)))
1057 if (original
== NULL
)
1059 g_set_error (error
, G_MARKUP_ERROR
,
1060 G_MARKUP_ERROR_INVALID_CONTENT
,
1061 _("No <key name='%s'> to override"), key
);
1065 if (g_hash_table_lookup (state
->keys
, key
))
1067 g_set_error (error
, G_MARKUP_ERROR
,
1068 G_MARKUP_ERROR_INVALID_CONTENT
,
1069 _("<override name='%s'> already specified"), key
);
1073 *key_state
= key_state_override (original
, state
->gettext_domain
);
1074 *string
= key_state_start_default (*key_state
, l10n
, context
, error
);
1075 g_hash_table_insert (state
->keys
, g_strdup (key
), *key_state
);
1079 override_state_end (KeyState
**key_state
,
1083 key_state_end_default (*key_state
, string
, error
);
1087 /* Handling of toplevel state {{{1 */
1090 gboolean strict
; /* TRUE if --strict was given */
1092 GHashTable
*schema_table
; /* string -> SchemaState */
1093 GHashTable
*flags_table
; /* string -> EnumState */
1094 GHashTable
*enum_table
; /* string -> EnumState */
1096 GSList
*this_file_schemas
; /* strings: <schema>s in this file */
1097 GSList
*this_file_flagss
; /* strings: <flags>s in this file */
1098 GSList
*this_file_enums
; /* strings: <enum>s in this file */
1100 gchar
*schemalist_domain
; /* the <schemalist> gettext domain */
1102 SchemaState
*schema_state
; /* non-NULL when inside <schema> */
1103 KeyState
*key_state
; /* non-NULL when inside <key> */
1104 EnumState
*enum_state
; /* non-NULL when inside <enum> */
1106 GString
*string
; /* non-NULL when accepting text */
1110 is_subclass (const gchar
*class_name
,
1111 const gchar
*possible_parent
,
1112 GHashTable
*schema_table
)
1116 if (strcmp (class_name
, possible_parent
) == 0)
1119 class = g_hash_table_lookup (schema_table
, class_name
);
1120 g_assert (class != NULL
);
1122 return class->extends_name
&&
1123 is_subclass (class->extends_name
, possible_parent
, schema_table
);
1127 parse_state_start_schema (ParseState
*state
,
1130 const gchar
*gettext_domain
,
1131 const gchar
*extends_name
,
1132 const gchar
*list_of
,
1135 SchemaState
*extends
;
1138 if (g_hash_table_lookup (state
->schema_table
, id
))
1140 g_set_error (error
, G_MARKUP_ERROR
,
1141 G_MARKUP_ERROR_INVALID_CONTENT
,
1142 _("<schema id='%s'> already specified"), id
);
1148 extends
= g_hash_table_lookup (state
->schema_table
, extends_name
);
1150 if (extends
== NULL
)
1152 g_set_error (error
, G_MARKUP_ERROR
,
1153 G_MARKUP_ERROR_INVALID_CONTENT
,
1154 _("<schema id='%s'> extends not yet existing "
1155 "schema “%s”"), id
, extends_name
);
1166 if (!(tmp
= g_hash_table_lookup (state
->schema_table
, list_of
)))
1168 g_set_error (error
, G_MARKUP_ERROR
,
1169 G_MARKUP_ERROR_INVALID_CONTENT
,
1170 _("<schema id='%s'> is list of not yet existing "
1171 "schema “%s”"), id
, list_of
);
1177 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1178 _("Cannot be a list of a schema with a path"));
1187 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1188 _("Cannot extend a schema with a path"));
1194 if (extends
->list_of
== NULL
)
1196 g_set_error (error
, G_MARKUP_ERROR
,
1197 G_MARKUP_ERROR_INVALID_CONTENT
,
1198 _("<schema id='%s'> is a list, extending "
1199 "<schema id='%s'> which is not a list"),
1204 if (!is_subclass (list_of
, extends
->list_of
, state
->schema_table
))
1206 g_set_error (error
, G_MARKUP_ERROR
,
1207 G_MARKUP_ERROR_INVALID_CONTENT
,
1208 _("<schema id='%s' list-of='%s'> extends <schema "
1209 "id='%s' list-of='%s'> but “%s” does not "
1210 "extend “%s”"), id
, list_of
, extends_name
,
1211 extends
->list_of
, list_of
, extends
->list_of
);
1216 /* by default we are a list of the same thing that the schema
1217 * we are extending is a list of (which might be nothing)
1219 list_of
= extends
->list_of
;
1222 if (path
&& !(g_str_has_prefix (path
, "/") && g_str_has_suffix (path
, "/")))
1224 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1225 _("A path, if given, must begin and end with a slash"));
1229 if (path
&& list_of
&& !g_str_has_suffix (path
, ":/"))
1231 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1232 _("The path of a list must end with “:/”"));
1236 if (path
&& (g_str_has_prefix (path
, "/apps/") ||
1237 g_str_has_prefix (path
, "/desktop/") ||
1238 g_str_has_prefix (path
, "/system/")))
1240 gchar
*message
= NULL
;
1241 message
= g_strdup_printf (_("Warning: Schema “%s” has path “%s”. "
1242 "Paths starting with "
1243 "“/apps/”, “/desktop/” or “/system/” are deprecated."),
1245 g_printerr ("%s\n", message
);
1249 state
->schema_state
= schema_state_new (path
, gettext_domain
,
1250 extends
, extends_name
, list_of
);
1252 my_id
= g_strdup (id
);
1253 state
->this_file_schemas
= g_slist_prepend (state
->this_file_schemas
, my_id
);
1254 g_hash_table_insert (state
->schema_table
, my_id
, state
->schema_state
);
1258 parse_state_start_enum (ParseState
*state
,
1263 GSList
**list
= is_flags
? &state
->this_file_flagss
: &state
->this_file_enums
;
1264 GHashTable
*table
= is_flags
? state
->flags_table
: state
->enum_table
;
1267 if (g_hash_table_lookup (table
, id
))
1269 g_set_error (error
, G_MARKUP_ERROR
,
1270 G_MARKUP_ERROR_INVALID_CONTENT
,
1271 _("<%s id='%s'> already specified"),
1272 is_flags
? "flags" : "enum", id
);
1276 state
->enum_state
= enum_state_new (is_flags
);
1278 my_id
= g_strdup (id
);
1279 *list
= g_slist_prepend (*list
, my_id
);
1280 g_hash_table_insert (table
, my_id
, state
->enum_state
);
1283 /* GMarkup Parser Functions {{{1 */
1285 /* Start element {{{2 */
1287 start_element (GMarkupParseContext
*context
,
1288 const gchar
*element_name
,
1289 const gchar
**attribute_names
,
1290 const gchar
**attribute_values
,
1294 ParseState
*state
= user_data
;
1295 const GSList
*element_stack
;
1296 const gchar
*container
;
1298 element_stack
= g_markup_parse_context_get_element_stack (context
);
1299 container
= element_stack
->next
? element_stack
->next
->data
: NULL
;
1301 #define COLLECT(first, ...) \
1302 g_markup_collect_attributes (element_name, \
1303 attribute_names, attribute_values, error, \
1304 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1305 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
1306 #define STRDUP G_MARKUP_COLLECT_STRDUP
1307 #define STRING G_MARKUP_COLLECT_STRING
1308 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1310 /* Toplevel items {{{3 */
1311 if (container
== NULL
)
1313 if (strcmp (element_name
, "schemalist") == 0)
1315 COLLECT (OPTIONAL
| STRDUP
,
1317 &state
->schemalist_domain
);
1323 /* children of <schemalist> {{{3 */
1324 else if (strcmp (container
, "schemalist") == 0)
1326 if (strcmp (element_name
, "schema") == 0)
1328 const gchar
*id
, *path
, *gettext_domain
, *extends
, *list_of
;
1329 if (COLLECT (STRING
, "id", &id
,
1330 OPTIONAL
| STRING
, "path", &path
,
1331 OPTIONAL
| STRING
, "gettext-domain", &gettext_domain
,
1332 OPTIONAL
| STRING
, "extends", &extends
,
1333 OPTIONAL
| STRING
, "list-of", &list_of
))
1334 parse_state_start_schema (state
, id
, path
,
1335 gettext_domain
? gettext_domain
1336 : state
->schemalist_domain
,
1337 extends
, list_of
, error
);
1341 else if (strcmp (element_name
, "enum") == 0)
1344 if (COLLECT (STRING
, "id", &id
))
1345 parse_state_start_enum (state
, id
, FALSE
, error
);
1349 else if (strcmp (element_name
, "flags") == 0)
1352 if (COLLECT (STRING
, "id", &id
))
1353 parse_state_start_enum (state
, id
, TRUE
, error
);
1359 /* children of <schema> {{{3 */
1360 else if (strcmp (container
, "schema") == 0)
1362 if (strcmp (element_name
, "key") == 0)
1364 const gchar
*name
, *type_string
, *enum_type
, *flags_type
;
1366 if (COLLECT (STRING
, "name", &name
,
1367 OPTIONAL
| STRING
, "type", &type_string
,
1368 OPTIONAL
| STRING
, "enum", &enum_type
,
1369 OPTIONAL
| STRING
, "flags", &flags_type
))
1371 state
->key_state
= schema_state_add_key (state
->schema_state
,
1375 enum_type
, flags_type
,
1379 else if (strcmp (element_name
, "child") == 0)
1381 const gchar
*name
, *schema
;
1383 if (COLLECT (STRING
, "name", &name
, STRING
, "schema", &schema
))
1384 schema_state_add_child (state
->schema_state
,
1385 name
, schema
, error
);
1388 else if (strcmp (element_name
, "override") == 0)
1390 const gchar
*name
, *l10n
, *context
;
1392 if (COLLECT (STRING
, "name", &name
,
1393 OPTIONAL
| STRING
, "l10n", &l10n
,
1394 OPTIONAL
| STRING
, "context", &context
))
1395 schema_state_add_override (state
->schema_state
,
1396 &state
->key_state
, &state
->string
,
1397 name
, l10n
, context
, error
);
1402 /* children of <key> {{{3 */
1403 else if (strcmp (container
, "key") == 0)
1405 if (strcmp (element_name
, "default") == 0)
1407 const gchar
*l10n
, *context
;
1408 if (COLLECT (STRING
| OPTIONAL
, "l10n", &l10n
,
1409 STRING
| OPTIONAL
, "context", &context
))
1410 state
->string
= key_state_start_default (state
->key_state
,
1411 l10n
, context
, error
);
1415 else if (strcmp (element_name
, "summary") == 0)
1419 if (state
->key_state
->summary_seen
&& state
->strict
)
1420 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1421 _("Only one <%s> element allowed inside <%s>"),
1422 element_name
, container
);
1424 state
->string
= g_string_new (NULL
);
1426 state
->key_state
->summary_seen
= TRUE
;
1431 else if (strcmp (element_name
, "description") == 0)
1435 if (state
->key_state
->description_seen
&& state
->strict
)
1436 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1437 _("Only one <%s> element allowed inside <%s>"),
1438 element_name
, container
);
1440 state
->string
= g_string_new (NULL
);
1442 state
->key_state
->description_seen
= TRUE
;
1447 else if (strcmp (element_name
, "range") == 0)
1449 const gchar
*min
, *max
;
1450 if (COLLECT (STRING
| OPTIONAL
, "min", &min
,
1451 STRING
| OPTIONAL
, "max", &max
))
1452 key_state_set_range (state
->key_state
, min
, max
, error
);
1456 else if (strcmp (element_name
, "choices") == 0)
1459 key_state_start_choices (state
->key_state
, error
);
1463 else if (strcmp (element_name
, "aliases") == 0)
1466 key_state_start_aliases (state
->key_state
, error
);
1472 /* children of <choices> {{{3 */
1473 else if (strcmp (container
, "choices") == 0)
1475 if (strcmp (element_name
, "choice") == 0)
1478 if (COLLECT (STRING
, "value", &value
))
1479 key_state_add_choice (state
->key_state
, value
, error
);
1485 /* children of <aliases> {{{3 */
1486 else if (strcmp (container
, "aliases") == 0)
1488 if (strcmp (element_name
, "alias") == 0)
1490 const gchar
*value
, *target
;
1491 if (COLLECT (STRING
, "value", &value
, STRING
, "target", &target
))
1492 key_state_add_alias (state
->key_state
, value
, target
, error
);
1498 /* children of <enum> {{{3 */
1499 else if (strcmp (container
, "enum") == 0 ||
1500 strcmp (container
, "flags") == 0)
1502 if (strcmp (element_name
, "value") == 0)
1504 const gchar
*nick
, *valuestr
;
1505 if (COLLECT (STRING
, "nick", &nick
,
1506 STRING
, "value", &valuestr
))
1507 enum_state_add_value (state
->enum_state
, nick
, valuestr
, error
);
1514 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1515 _("Element <%s> not allowed inside <%s>"),
1516 element_name
, container
);
1518 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_UNKNOWN_ELEMENT
,
1519 _("Element <%s> not allowed at the top level"), element_name
);
1522 /* End element {{{2 */
1525 key_state_end (KeyState
**state_ptr
,
1533 if (state
->default_value
== NULL
)
1535 g_set_error_literal (error
,
1536 G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1537 _("Element <default> is required in <key>"));
1543 schema_state_end (SchemaState
**state_ptr
,
1550 end_element (GMarkupParseContext
*context
,
1551 const gchar
*element_name
,
1555 ParseState
*state
= user_data
;
1557 if (strcmp (element_name
, "schemalist") == 0)
1559 g_free (state
->schemalist_domain
);
1560 state
->schemalist_domain
= NULL
;
1563 else if (strcmp (element_name
, "enum") == 0 ||
1564 strcmp (element_name
, "flags") == 0)
1565 enum_state_end (&state
->enum_state
, error
);
1567 else if (strcmp (element_name
, "schema") == 0)
1568 schema_state_end (&state
->schema_state
, error
);
1570 else if (strcmp (element_name
, "override") == 0)
1571 override_state_end (&state
->key_state
, &state
->string
, error
);
1573 else if (strcmp (element_name
, "key") == 0)
1574 key_state_end (&state
->key_state
, error
);
1576 else if (strcmp (element_name
, "default") == 0)
1577 key_state_end_default (state
->key_state
, &state
->string
, error
);
1579 else if (strcmp (element_name
, "choices") == 0)
1580 key_state_end_choices (state
->key_state
, error
);
1582 else if (strcmp (element_name
, "aliases") == 0)
1583 key_state_end_aliases (state
->key_state
, error
);
1587 g_string_free (state
->string
, TRUE
);
1588 state
->string
= NULL
;
1593 text (GMarkupParseContext
*context
,
1599 ParseState
*state
= user_data
;
1603 /* we are expecting a string, so store the text data.
1605 * we store the data verbatim here and deal with whitespace
1606 * later on. there are two reasons for that:
1608 * 1) whitespace is handled differently depending on the tag
1611 * 2) we could do leading whitespace removal by refusing to
1612 * insert it into state->string if it's at the start, but for
1613 * trailing whitespace, we have no idea if there is another
1614 * text() call coming or not.
1616 g_string_append_len (state
->string
, text
, text_len
);
1620 /* string is not expected: accept (and ignore) pure whitespace */
1623 for (i
= 0; i
< text_len
; i
++)
1624 if (!g_ascii_isspace (text
[i
]))
1626 g_set_error (error
, G_MARKUP_ERROR
, G_MARKUP_ERROR_INVALID_CONTENT
,
1627 _("Text may not appear inside <%s>"),
1628 g_markup_parse_context_get_element (context
));
1634 /* Write to GVDB {{{1 */
1642 gvdb_pair_init (GvdbPair
*pair
)
1644 pair
->table
= gvdb_hash_table_new (NULL
, NULL
);
1645 pair
->root
= gvdb_hash_table_insert (pair
->table
, "");
1649 gvdb_pair_clear (GvdbPair
*pair
)
1651 g_hash_table_unref (pair
->table
);
1656 GHashTable
*schema_table
;
1662 GHashTable
*schema_table
;
1668 output_key (gpointer key
,
1672 OutputSchemaData
*data
;
1676 GVariant
*serialised
= NULL
;
1682 item
= gvdb_hash_table_insert (data
->pair
.table
, name
);
1683 gvdb_item_set_parent (item
, data
->pair
.root
);
1684 serialised
= key_state_serialise (state
);
1685 gvdb_item_set_value (item
, serialised
);
1686 g_variant_unref (serialised
);
1691 if (state
->child_schema
&&
1692 !g_hash_table_lookup (data
->schema_table
, state
->child_schema
))
1694 gchar
*message
= NULL
;
1695 message
= g_strdup_printf (_("Warning: undefined reference to <schema id='%s'/>"),
1696 state
->child_schema
);
1697 g_printerr ("%s\n", message
);
1703 output_schema (gpointer key
,
1707 WriteToFileData
*wtf_data
= user_data
;
1708 OutputSchemaData data
;
1709 GvdbPair
*root_pair
;
1716 root_pair
= &wtf_data
->root_pair
;
1718 data
.schema_table
= wtf_data
->schema_table
;
1719 gvdb_pair_init (&data
.pair
);
1722 item
= gvdb_hash_table_insert (root_pair
->table
, id
);
1723 gvdb_item_set_parent (item
, root_pair
->root
);
1724 gvdb_item_set_hash_table (item
, data
.pair
.table
);
1726 g_hash_table_foreach (state
->keys
, output_key
, &data
);
1729 gvdb_hash_table_insert_string (data
.pair
.table
, ".path", state
->path
);
1731 if (state
->extends_name
)
1732 gvdb_hash_table_insert_string (data
.pair
.table
, ".extends",
1733 state
->extends_name
);
1736 gvdb_hash_table_insert_string (data
.pair
.table
, ".list-of",
1740 gvdb_hash_table_insert_string (data
.pair
.table
,
1742 state
->gettext_domain
);
1744 gvdb_pair_clear (&data
.pair
);
1748 write_to_file (GHashTable
*schema_table
,
1749 const gchar
*filename
,
1752 WriteToFileData data
;
1755 data
.schema_table
= schema_table
;
1757 gvdb_pair_init (&data
.root_pair
);
1759 g_hash_table_foreach (schema_table
, output_schema
, &data
);
1761 success
= gvdb_table_write_contents (data
.root_pair
.table
, filename
,
1762 G_BYTE_ORDER
!= G_LITTLE_ENDIAN
,
1764 g_hash_table_unref (data
.root_pair
.table
);
1769 /* Parser driver {{{1 */
1771 parse_gschema_files (gchar
**files
,
1774 GMarkupParser parser
= { start_element
, end_element
, text
};
1775 ParseState state
= { 0, };
1776 const gchar
*filename
;
1777 GError
*error
= NULL
;
1779 state
.strict
= strict
;
1781 state
.enum_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1782 g_free
, enum_state_free
);
1784 state
.flags_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1785 g_free
, enum_state_free
);
1787 state
.schema_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1788 g_free
, schema_state_free
);
1790 while ((filename
= *files
++) != NULL
)
1792 GMarkupParseContext
*context
;
1797 if (!g_file_get_contents (filename
, &contents
, &size
, &error
))
1799 fprintf (stderr
, "%s\n", error
->message
);
1800 g_clear_error (&error
);
1804 context
= g_markup_parse_context_new (&parser
,
1805 G_MARKUP_TREAT_CDATA_AS_TEXT
|
1806 G_MARKUP_PREFIX_ERROR_POSITION
|
1807 G_MARKUP_IGNORE_QUALIFIED
,
1811 if (!g_markup_parse_context_parse (context
, contents
, size
, &error
) ||
1812 !g_markup_parse_context_end_parse (context
, &error
))
1816 /* back out any changes from this file */
1817 for (item
= state
.this_file_schemas
; item
; item
= item
->next
)
1818 g_hash_table_remove (state
.schema_table
, item
->data
);
1820 for (item
= state
.this_file_flagss
; item
; item
= item
->next
)
1821 g_hash_table_remove (state
.flags_table
, item
->data
);
1823 for (item
= state
.this_file_enums
; item
; item
= item
->next
)
1824 g_hash_table_remove (state
.enum_table
, item
->data
);
1827 g_markup_parse_context_get_position (context
, &line
, &col
);
1828 fprintf (stderr
, "%s:%d:%d %s. ", filename
, line
, col
, error
->message
);
1829 g_clear_error (&error
);
1833 /* Translators: Do not translate "--strict". */
1834 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1835 g_hash_table_unref (state
.schema_table
);
1836 g_hash_table_unref (state
.flags_table
);
1837 g_hash_table_unref (state
.enum_table
);
1844 fprintf (stderr
, _("This entire file has been ignored.\n"));
1849 g_markup_parse_context_free (context
);
1850 g_slist_free (state
.this_file_schemas
);
1851 g_slist_free (state
.this_file_flagss
);
1852 g_slist_free (state
.this_file_enums
);
1853 state
.this_file_schemas
= NULL
;
1854 state
.this_file_flagss
= NULL
;
1855 state
.this_file_enums
= NULL
;
1858 g_hash_table_unref (state
.flags_table
);
1859 g_hash_table_unref (state
.enum_table
);
1861 return state
.schema_table
;
1865 compare_strings (gconstpointer a
,
1868 gchar
*one
= *(gchar
**) a
;
1869 gchar
*two
= *(gchar
**) b
;
1872 cmp
= g_str_has_suffix (two
, ".enums.xml") -
1873 g_str_has_suffix (one
, ".enums.xml");
1876 cmp
= strcmp (one
, two
);
1882 set_overrides (GHashTable
*schema_table
,
1886 const gchar
*filename
;
1887 GError
*error
= NULL
;
1889 while ((filename
= *files
++))
1895 g_debug ("Processing override file '%s'", filename
);
1897 key_file
= g_key_file_new ();
1898 if (!g_key_file_load_from_file (key_file
, filename
, 0, &error
))
1900 fprintf (stderr
, "%s: %s. ", filename
, error
->message
);
1901 g_key_file_free (key_file
);
1902 g_clear_error (&error
);
1906 fprintf (stderr
, _("Ignoring this file.\n"));
1910 fprintf (stderr
, _("--strict was specified; exiting.\n"));
1914 groups
= g_key_file_get_groups (key_file
, NULL
);
1916 for (i
= 0; groups
[i
]; i
++)
1918 const gchar
*group
= groups
[i
];
1919 const gchar
*schema_name
;
1920 const gchar
*desktop_id
;
1921 SchemaState
*schema
;
1926 pieces
= g_strsplit (group
, ":", 2);
1927 schema_name
= pieces
[0];
1928 desktop_id
= pieces
[1];
1930 g_debug ("Processing group '%s' (schema '%s', %s)",
1931 group
, schema_name
, desktop_id
? desktop_id
: "all desktops");
1933 schema
= g_hash_table_lookup (schema_table
, schema_name
);
1937 /* Having the schema not be installed is expected to be a
1938 * common case. Don't even emit an error message about
1941 g_strfreev (pieces
);
1945 keys
= g_key_file_get_keys (key_file
, group
, NULL
, NULL
);
1946 g_assert (keys
!= NULL
);
1948 for (j
= 0; keys
[j
]; j
++)
1950 const gchar
*key
= keys
[j
];
1955 state
= g_hash_table_lookup (schema
->keys
, key
);
1959 fprintf (stderr
, _("No such key “%s” in schema “%s” as "
1960 "specified in override file “%s”"),
1961 key
, group
, filename
);
1965 fprintf (stderr
, _("; ignoring override for this key.\n"));
1969 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1970 g_key_file_free (key_file
);
1971 g_strfreev (pieces
);
1972 g_strfreev (groups
);
1978 if (desktop_id
!= NULL
&& state
->l10n
)
1980 /* Let's avoid the n*m case of per-desktop localised
1981 * default values, and just forbid it.
1984 _("cannot provide per-desktop overrides for localised "
1985 "key “%s” in schema “%s” (override file “%s”)"),
1986 key
, group
, filename
);
1990 fprintf (stderr
, _("; ignoring override for this key.\n"));
1994 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
1995 g_key_file_free (key_file
);
1996 g_strfreev (pieces
);
1997 g_strfreev (groups
);
2003 string
= g_key_file_get_value (key_file
, group
, key
, NULL
);
2004 g_assert (string
!= NULL
);
2006 value
= g_variant_parse (state
->type
, string
,
2007 NULL
, NULL
, &error
);
2011 fprintf (stderr
, _("error parsing key “%s” in schema “%s” "
2012 "as specified in override file “%s”: "
2014 key
, group
, filename
, error
->message
);
2016 g_clear_error (&error
);
2021 fprintf (stderr
, _("Ignoring override for this key.\n"));
2025 fprintf (stderr
, _("--strict was specified; exiting.\n"));
2026 g_key_file_free (key_file
);
2027 g_strfreev (pieces
);
2028 g_strfreev (groups
);
2036 if (g_variant_compare (value
, state
->minimum
) < 0 ||
2037 g_variant_compare (value
, state
->maximum
) > 0)
2040 _("override for key “%s” in schema “%s” in "
2041 "override file “%s” is outside the range "
2042 "given in the schema"),
2043 key
, group
, filename
);
2045 g_variant_unref (value
);
2050 fprintf (stderr
, _("; ignoring override for this key.\n"));
2054 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2055 g_key_file_free (key_file
);
2056 g_strfreev (pieces
);
2057 g_strfreev (groups
);
2064 else if (state
->strinfo
->len
)
2066 if (!is_valid_choices (value
, state
->strinfo
))
2069 _("override for key “%s” in schema “%s” in "
2070 "override file “%s” is not in the list "
2071 "of valid choices"),
2072 key
, group
, filename
);
2074 g_variant_unref (value
);
2079 fprintf (stderr
, _("; ignoring override for this key.\n"));
2083 fprintf (stderr
, _(" and --strict was specified; exiting.\n"));
2084 g_key_file_free (key_file
);
2085 g_strfreev (pieces
);
2086 g_strfreev (groups
);
2093 if (desktop_id
!= NULL
)
2095 if (state
->desktop_overrides
== NULL
)
2096 state
->desktop_overrides
= g_variant_dict_new (NULL
);
2098 g_variant_dict_insert_value (state
->desktop_overrides
, desktop_id
, value
);
2099 g_variant_unref (value
);
2103 g_variant_unref (state
->default_value
);
2104 state
->default_value
= value
;
2110 g_strfreev (pieces
);
2114 g_strfreev (groups
);
2121 main (int argc
, char **argv
)
2123 GError
*error
= NULL
;
2124 GHashTable
*table
= NULL
;
2127 const gchar
*srcdir
;
2128 gboolean show_version_and_exit
= FALSE
;
2129 gchar
*targetdir
= NULL
;
2130 gchar
*target
= NULL
;
2131 gboolean dry_run
= FALSE
;
2132 gboolean strict
= FALSE
;
2133 gchar
**schema_files
= NULL
;
2134 gchar
**override_files
= NULL
;
2135 GOptionContext
*context
= NULL
;
2137 GOptionEntry entries
[] = {
2138 { "version", 0, 0, G_OPTION_ARG_NONE
, &show_version_and_exit
, N_("Show program version and exit"), NULL
},
2139 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME
, &targetdir
, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
2140 { "strict", 0, 0, G_OPTION_ARG_NONE
, &strict
, N_("Abort on any errors in schemas"), NULL
},
2141 { "dry-run", 0, 0, G_OPTION_ARG_NONE
, &dry_run
, N_("Do not write the gschema.compiled file"), NULL
},
2142 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE
, &allow_any_name
, N_("Do not enforce key name restrictions") },
2144 /* These options are only for use in the gschema-compile tests */
2145 { "schema-file", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_FILENAME_ARRAY
, &schema_files
, NULL
, NULL
},
2146 { "override-file", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_FILENAME_ARRAY
, &override_files
, NULL
, NULL
},
2154 setlocale (LC_ALL
, "");
2155 textdomain (GETTEXT_PACKAGE
);
2158 tmp
= _glib_get_locale_dir ();
2159 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
2161 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
2164 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2165 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
2168 context
= g_option_context_new (N_("DIRECTORY"));
2169 g_option_context_set_translation_domain (context
, GETTEXT_PACKAGE
);
2170 g_option_context_set_summary (context
,
2171 N_("Compile all GSettings schema files into a schema cache.\n"
2172 "Schema files are required to have the extension .gschema.xml,\n"
2173 "and the cache file is called gschemas.compiled."));
2174 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
2176 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
2178 fprintf (stderr
, "%s\n", error
->message
);
2183 if (show_version_and_exit
)
2185 g_print (PACKAGE_VERSION
"\n");
2190 if (!schema_files
&& argc
!= 2)
2192 fprintf (stderr
, _("You should give exactly one directory name\n"));
2199 target
= g_build_filename (targetdir
? targetdir
: srcdir
, "gschemas.compiled", NULL
);
2203 GPtrArray
*overrides
;
2206 files
= g_ptr_array_new ();
2207 overrides
= g_ptr_array_new ();
2209 dir
= g_dir_open (srcdir
, 0, &error
);
2212 fprintf (stderr
, "%s\n", error
->message
);
2214 g_ptr_array_unref (files
);
2215 g_ptr_array_unref (overrides
);
2221 while ((file
= g_dir_read_name (dir
)) != NULL
)
2223 if (g_str_has_suffix (file
, ".gschema.xml") ||
2224 g_str_has_suffix (file
, ".enums.xml"))
2225 g_ptr_array_add (files
, g_build_filename (srcdir
, file
, NULL
));
2227 else if (g_str_has_suffix (file
, ".gschema.override"))
2228 g_ptr_array_add (overrides
,
2229 g_build_filename (srcdir
, file
, NULL
));
2232 if (files
->len
== 0)
2234 fprintf (stdout
, _("No schema files found: "));
2236 if (g_unlink (target
))
2237 fprintf (stdout
, _("doing nothing.\n"));
2240 fprintf (stdout
, _("removed existing output file.\n"));
2242 g_ptr_array_unref (files
);
2243 g_ptr_array_unref (overrides
);
2248 g_ptr_array_sort (files
, compare_strings
);
2249 g_ptr_array_add (files
, NULL
);
2251 g_ptr_array_sort (overrides
, compare_strings
);
2252 g_ptr_array_add (overrides
, NULL
);
2254 schema_files
= (char **) g_ptr_array_free (files
, FALSE
);
2255 override_files
= (gchar
**) g_ptr_array_free (overrides
, FALSE
);
2258 if ((table
= parse_gschema_files (schema_files
, strict
)) == NULL
)
2264 if (override_files
!= NULL
&&
2265 !set_overrides (table
, override_files
, strict
))
2271 if (!dry_run
&& !write_to_file (table
, target
, &error
))
2273 fprintf (stderr
, "%s\n", error
->message
);
2282 g_clear_error (&error
);
2283 g_clear_pointer (&table
, g_hash_table_unref
);
2284 g_clear_pointer (&dir
, g_dir_close
);
2287 g_strfreev (schema_files
);
2288 g_strfreev (override_files
);
2289 g_option_context_free (context
);
2300 /* vim:set foldmethod=marker: */