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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
32 #include "gvdb/gvdb-builder.h"
38 GVariantBuilder key_options;
40 gchar *schemalist_domain;
43 GvdbItem *schema_root;
57 static gboolean allow_any_name = FALSE;
60 is_valid_keyname (const gchar *key,
67 g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
68 "empty names are not permitted");
75 if (!g_ascii_islower (key[0]))
77 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
78 "invalid name '%s': names must begin "
79 "with a lowercase letter", key);
83 for (i = 1; key[i]; i++)
86 !g_ascii_islower (key[i]) &&
87 !g_ascii_isdigit (key[i]))
89 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
90 "invalid name '%s': invalid character '%c'; "
91 "only lowercase letters, numbers and dash ('-') "
92 "are permitted.", key, key[i]);
96 if (key[i] == '-' && key[i + 1] == '-')
98 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
99 "invalid name '%s': two successive dashes ('--') are "
100 "not permitted.", key);
105 if (key[i - 1] == '-')
107 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
108 "invalid name '%s': the last character may not be a "
115 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
116 "invalid name '%s': maximum length is 32", key);
124 type_allows_choices (const GVariantType *type)
126 if (g_variant_type_is_array (type) ||
127 g_variant_type_is_maybe (type))
128 return type_allows_choices (g_variant_type_element (type));
130 return g_variant_type_equal (type, G_VARIANT_TYPE_STRING);
134 type_allows_range (const GVariantType *type)
136 static const char range_types[] = "ynqiuxtd";
138 return strchr (range_types, *(const char*) type) != NULL;
142 is_valid_choices (GVariant *variant,
143 const gchar *choices)
145 switch (g_variant_classify (variant))
147 case G_VARIANT_CLASS_MAYBE:
148 case G_VARIANT_CLASS_ARRAY:
154 n = g_variant_n_children (variant);
155 for (i = 0; i < n; ++i)
157 child = g_variant_get_child_value (variant, i);
158 is_valid = is_valid_choices (child, choices);
159 g_variant_unref (child);
168 case G_VARIANT_CLASS_STRING:
172 g_variant_get (variant, "&s", &string);
174 while ((choices = strstr (choices, string)) && choices[-1] != 0xff);
176 return choices != NULL;
180 g_assert_not_reached ();
185 start_element (GMarkupParseContext *context,
186 const gchar *element_name,
187 const gchar **attribute_names,
188 const gchar **attribute_values,
192 ParseState *state = user_data;
193 const GSList *element_stack;
194 const gchar *container;
196 element_stack = g_markup_parse_context_get_element_stack (context);
197 container = element_stack->next ? element_stack->next->data : NULL;
199 #define COLLECT(first, ...) \
200 g_markup_collect_attributes (element_name, \
201 attribute_names, attribute_values, error, \
202 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
203 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
204 #define STRDUP G_MARKUP_COLLECT_STRDUP
205 #define STRING G_MARKUP_COLLECT_STRING
206 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
208 if (container == NULL)
210 if (strcmp (element_name, "schemalist") == 0)
212 COLLECT (OPTIONAL | STRDUP,
214 &state->schemalist_domain);
218 else if (strcmp (container, "schemalist") == 0)
220 if (strcmp (element_name, "schema") == 0)
222 const gchar *id, *path;
224 if (COLLECT (STRING, "id", &id,
225 OPTIONAL | STRING, "path", &path,
226 OPTIONAL | STRDUP, "gettext-domain",
227 &state->schema_domain))
229 if (!g_hash_table_lookup (state->schemas, id))
231 state->schema = gvdb_hash_table_new (state->schemas, id);
232 state->schema_root = gvdb_hash_table_insert (state->schema, "");
235 gvdb_hash_table_insert_string (state->schema,
239 g_set_error (error, G_MARKUP_ERROR,
240 G_MARKUP_ERROR_INVALID_CONTENT,
241 "<schema id='%s'> already specified", id);
246 else if (strcmp (container, "schema") == 0)
248 if (strcmp (element_name, "key") == 0)
250 const gchar *name, *type;
252 if (COLLECT (STRING, "name", &name, STRING, "type", &type))
254 if (!is_valid_keyname (name, error))
257 if (!g_hash_table_lookup (state->schema, name))
259 state->key = gvdb_hash_table_insert (state->schema, name);
260 gvdb_item_set_parent (state->key, state->schema_root);
264 g_set_error (error, G_MARKUP_ERROR,
265 G_MARKUP_ERROR_INVALID_CONTENT,
266 "<key name='%s'> already specified", name);
270 if (g_variant_type_string_is_valid (type))
271 state->type = g_variant_type_new (type);
274 g_set_error (error, G_MARKUP_ERROR,
275 G_MARKUP_ERROR_INVALID_CONTENT,
276 "invalid GVariant type string '%s'", type);
280 g_variant_builder_init (&state->key_options,
281 G_VARIANT_TYPE ("a{sv}"));
286 else if (strcmp (element_name, "child") == 0)
288 const gchar *name, *schema;
290 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
294 if (!is_valid_keyname (name, error))
297 childname = g_strconcat (name, "/", NULL);
299 if (!g_hash_table_lookup (state->schema, childname))
300 gvdb_hash_table_insert_string (state->schema, childname, schema);
302 g_set_error (error, G_MARKUP_ERROR,
303 G_MARKUP_ERROR_INVALID_CONTENT,
304 "<child name='%s'> already specified", name);
311 else if (strcmp (container, "key") == 0)
313 if (strcmp (element_name, "default") == 0)
317 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
318 STRDUP | OPTIONAL, "context", &state->context))
322 if (!g_hash_table_lookup (state->schema, ".gettext-domain"))
324 const gchar *domain = state->schema_domain ?
325 state->schema_domain :
326 state->schemalist_domain;
330 g_set_error_literal (error, G_MARKUP_ERROR,
331 G_MARKUP_ERROR_INVALID_CONTENT,
332 "l10n requested, but no "
333 "gettext domain given");
337 gvdb_hash_table_insert_string (state->schema,
341 if (strcmp (l10n, "messages") == 0)
343 else if (strcmp (l10n, "time") == 0)
347 g_set_error (error, G_MARKUP_ERROR,
348 G_MARKUP_ERROR_INVALID_CONTENT,
349 "unsupported l10n category: %s", l10n);
358 if (state->context != NULL)
360 g_set_error_literal (error, G_MARKUP_ERROR,
361 G_MARKUP_ERROR_INVALID_CONTENT,
362 "translation context given for "
363 " value without l10n enabled");
368 state->string = g_string_new (NULL);
373 else if (strcmp (element_name, "summary") == 0 ||
374 strcmp (element_name, "description") == 0)
376 state->string = g_string_new (NULL);
380 else if (strcmp (element_name, "range") == 0)
382 const gchar *min_str, *max_str;
384 if (!type_allows_range (state->type))
386 gchar *type = g_variant_type_dup_string (state->type);
387 g_set_error (error, G_MARKUP_ERROR,
388 G_MARKUP_ERROR_INVALID_CONTENT,
389 "Element <%s> not allowed for keys of type \"%s\"\n",
395 if (!COLLECT (STRING, "min", &min_str,
396 STRING, "max", &max_str))
399 state->min = g_variant_parse (state->type, min_str, NULL, NULL, error);
400 if (state->min == NULL)
403 state->max = g_variant_parse (state->type, max_str, NULL, NULL, error);
404 if (state->max == NULL)
407 if (g_variant_compare (state->min, state->max) > 0)
409 g_set_error (error, G_MARKUP_ERROR,
410 G_MARKUP_ERROR_INVALID_CONTENT,
411 "Element <%s> specified minimum is greater than maxmimum",
416 g_variant_builder_add (&state->key_options, "{sv}", "range",
417 g_variant_new ("(@?@?)", state->min, state->max));
420 else if (strcmp (element_name, "choices") == 0)
422 if (!type_allows_choices (state->type))
424 gchar *type = g_variant_type_dup_string (state->type);
425 g_set_error (error, G_MARKUP_ERROR,
426 G_MARKUP_ERROR_INVALID_CONTENT,
427 "Element <%s> not allowed for keys of type \"%s\"\n",
433 state->choices = g_string_new ("\xff");
439 else if (strcmp (container, "choices") == 0)
441 if (strcmp (element_name, "choice") == 0)
445 if (COLLECT (STRING, "value", &value))
446 g_string_append_printf (state->choices, "%s\xff", value);
453 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
454 "Element <%s> not allowed inside <%s>\n",
455 element_name, container);
457 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
458 "Element <%s> not allowed at toplevel\n", element_name);
462 end_element (GMarkupParseContext *context,
463 const gchar *element_name,
467 ParseState *state = user_data;
469 if (strcmp (element_name, "default") == 0)
471 state->value = g_variant_parse (state->type, state->string->str,
473 if (state->value == NULL)
482 /* Contextified messages are supported by prepending the
483 * context, followed by '\004' to the start of the message
484 * string. We do that here to save GSettings the work
487 * Note: we are about to g_free() the context anyway...
489 len = strlen (state->context);
490 state->context[len] = '\004';
491 g_string_prepend_len (state->string, state->context, len + 1);
494 g_variant_builder_add (&state->key_options, "{sv}", "l10n",
495 g_variant_new ("(ys)",
497 state->string->str));
500 g_string_free (state->string, TRUE);
501 state->string = NULL;
502 g_free (state->context);
503 state->context = NULL;
506 else if (strcmp (element_name, "key") == 0)
508 if (state->value == NULL)
510 g_set_error_literal (error,
511 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
512 "element <default> is required in <key>\n");
516 if (state->min != NULL)
518 if (g_variant_compare (state->value, state->min) < 0 ||
519 g_variant_compare (state->value, state->max) > 0)
521 g_set_error (error, G_MARKUP_ERROR,
522 G_MARKUP_ERROR_INVALID_CONTENT,
523 "<default> is not contained in the specified range");
527 state->min = state->max = NULL;
529 else if (state->choices != NULL)
531 if (!is_valid_choices (state->value, state->choices->str))
533 g_set_error_literal (error, G_MARKUP_ERROR,
534 G_MARKUP_ERROR_INVALID_CONTENT,
535 "<default> contains string not in <choices>");
539 state->choices = NULL;
542 gvdb_item_set_value (state->key, state->value);
543 gvdb_item_set_options (state->key,
544 g_variant_builder_end (&state->key_options));
549 else if (strcmp (element_name, "summary") == 0 ||
550 strcmp (element_name, "description") == 0)
552 g_string_free (state->string, TRUE);
553 state->string = NULL;
556 else if (strcmp (element_name, "choices") == 0)
560 choices = g_string_free (state->choices, FALSE);
561 g_variant_builder_add (&state->key_options, "{sv}", "choices",
562 g_variant_new_byte_array (choices, -1));
568 text (GMarkupParseContext *context,
574 ParseState *state = user_data;
577 for (i = 0; i < text_len; i++)
578 if (!g_ascii_isspace (text[i]))
581 g_string_append_len (state->string, text, text_len);
584 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
585 "text may not appear inside <%s>\n",
586 g_markup_parse_context_get_element (context));
593 parse_gschema_files (gchar **files,
597 GMarkupParser parser = { start_element, end_element, text };
598 GMarkupParseContext *context;
599 ParseState state = { byteswap, };
600 const gchar *filename;
602 context = g_markup_parse_context_new (&parser,
603 G_MARKUP_PREFIX_ERROR_POSITION,
605 state.schemas = gvdb_hash_table_new (NULL, NULL);
607 while ((filename = *files++) != NULL)
612 if (!g_file_get_contents (filename, &contents, &size, error))
615 if (!g_markup_parse_context_parse (context, contents, size, error))
617 g_prefix_error (error, "%s: ", filename);
621 if (!g_markup_parse_context_end_parse (context, error))
623 g_prefix_error (error, "%s: ", filename);
628 return state.schemas;
632 main (int argc, char **argv)
634 gboolean byteswap = G_BYTE_ORDER != G_LITTLE_ENDIAN;
640 gchar *targetdir = NULL;
642 gboolean uninstall = FALSE;
643 gboolean dry_run = FALSE;
644 gchar **schema_files = NULL;
645 GOptionContext *context;
646 GOptionEntry entries[] = {
647 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
648 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
649 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("Do not give error for empty directory"), NULL },
650 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
652 /* These options are only for use in the gschema-compile tests */
653 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
657 setlocale (LC_ALL, "");
659 context = g_option_context_new (N_("DIRECTORY"));
660 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
661 g_option_context_set_summary (context,
662 N_("Compile all GSettings schema files into a schema cache.\n"
663 "Schema files are required to have the extension .gschema.xml,\n"
664 "and the cache file is called gschemas.compiled."));
665 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
668 if (!g_option_context_parse (context, &argc, &argv, &error))
670 fprintf (stderr, "%s", error->message);
674 g_option_context_free (context);
676 if (!schema_files && argc != 2)
678 fprintf (stderr, _("You should give exactly one directory name\n"));
684 if (targetdir == NULL)
687 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
693 files = g_ptr_array_new ();
695 dir = g_dir_open (srcdir, 0, &error);
698 fprintf (stderr, "%s\n", error->message);
702 while ((file = g_dir_read_name (dir)) != NULL)
704 if (g_str_has_suffix (file, ".gschema.xml"))
705 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
717 fprintf (stderr, _("No schema files found\n"));
721 g_ptr_array_add (files, NULL);
723 schema_files = (char **) g_ptr_array_free (files, FALSE);
727 if (!(table = parse_gschema_files (schema_files, byteswap, &error)) ||
728 (!dry_run && !gvdb_table_write_contents (table, target, byteswap, &error)))
730 fprintf (stderr, "%s\n", error->message);