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, "");
236 if (!g_str_has_prefix (path, "/") ||
237 !g_str_has_suffix (path, "/"))
239 g_set_error (error, G_MARKUP_ERROR,
240 G_MARKUP_ERROR_INVALID_CONTENT,
241 "a path, if given, must begin and "
246 gvdb_hash_table_insert_string (state->schema,
251 g_set_error (error, G_MARKUP_ERROR,
252 G_MARKUP_ERROR_INVALID_CONTENT,
253 "<schema id='%s'> already specified", id);
258 else if (strcmp (container, "schema") == 0)
260 if (strcmp (element_name, "key") == 0)
262 const gchar *name, *type;
264 if (COLLECT (STRING, "name", &name, STRING, "type", &type))
266 if (!is_valid_keyname (name, error))
269 if (!g_hash_table_lookup (state->schema, name))
271 state->key = gvdb_hash_table_insert (state->schema, name);
272 gvdb_item_set_parent (state->key, state->schema_root);
276 g_set_error (error, G_MARKUP_ERROR,
277 G_MARKUP_ERROR_INVALID_CONTENT,
278 "<key name='%s'> already specified", name);
282 if (g_variant_type_string_is_valid (type))
283 state->type = g_variant_type_new (type);
286 g_set_error (error, G_MARKUP_ERROR,
287 G_MARKUP_ERROR_INVALID_CONTENT,
288 "invalid GVariant type string '%s'", type);
292 g_variant_builder_init (&state->key_options,
293 G_VARIANT_TYPE ("a{sv}"));
298 else if (strcmp (element_name, "child") == 0)
300 const gchar *name, *schema;
302 if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
306 if (!is_valid_keyname (name, error))
309 childname = g_strconcat (name, "/", NULL);
311 if (!g_hash_table_lookup (state->schema, childname))
312 gvdb_hash_table_insert_string (state->schema, childname, schema);
314 g_set_error (error, G_MARKUP_ERROR,
315 G_MARKUP_ERROR_INVALID_CONTENT,
316 "<child name='%s'> already specified", name);
323 else if (strcmp (container, "key") == 0)
325 if (strcmp (element_name, "default") == 0)
329 if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
330 STRDUP | OPTIONAL, "context", &state->context))
334 if (!g_hash_table_lookup (state->schema, ".gettext-domain"))
336 const gchar *domain = state->schema_domain ?
337 state->schema_domain :
338 state->schemalist_domain;
342 g_set_error_literal (error, G_MARKUP_ERROR,
343 G_MARKUP_ERROR_INVALID_CONTENT,
344 "l10n requested, but no "
345 "gettext domain given");
349 gvdb_hash_table_insert_string (state->schema,
353 if (strcmp (l10n, "messages") == 0)
355 else if (strcmp (l10n, "time") == 0)
359 g_set_error (error, G_MARKUP_ERROR,
360 G_MARKUP_ERROR_INVALID_CONTENT,
361 "unsupported l10n category: %s", l10n);
370 if (state->context != NULL)
372 g_set_error_literal (error, G_MARKUP_ERROR,
373 G_MARKUP_ERROR_INVALID_CONTENT,
374 "translation context given for "
375 " value without l10n enabled");
380 state->string = g_string_new (NULL);
385 else if (strcmp (element_name, "summary") == 0 ||
386 strcmp (element_name, "description") == 0)
388 state->string = g_string_new (NULL);
392 else if (strcmp (element_name, "range") == 0)
394 const gchar *min_str, *max_str;
396 if (!type_allows_range (state->type))
398 gchar *type = g_variant_type_dup_string (state->type);
399 g_set_error (error, G_MARKUP_ERROR,
400 G_MARKUP_ERROR_INVALID_CONTENT,
401 "Element <%s> not allowed for keys of type \"%s\"\n",
407 if (!COLLECT (STRING, "min", &min_str,
408 STRING, "max", &max_str))
411 state->min = g_variant_parse (state->type, min_str, NULL, NULL, error);
412 if (state->min == NULL)
415 state->max = g_variant_parse (state->type, max_str, NULL, NULL, error);
416 if (state->max == NULL)
419 if (g_variant_compare (state->min, state->max) > 0)
421 g_set_error (error, G_MARKUP_ERROR,
422 G_MARKUP_ERROR_INVALID_CONTENT,
423 "Element <%s> specified minimum is greater than maxmimum",
428 g_variant_builder_add (&state->key_options, "{sv}", "range",
429 g_variant_new ("(@?@?)", state->min, state->max));
432 else if (strcmp (element_name, "choices") == 0)
434 if (!type_allows_choices (state->type))
436 gchar *type = g_variant_type_dup_string (state->type);
437 g_set_error (error, G_MARKUP_ERROR,
438 G_MARKUP_ERROR_INVALID_CONTENT,
439 "Element <%s> not allowed for keys of type \"%s\"\n",
445 state->choices = g_string_new ("\xff");
451 else if (strcmp (container, "choices") == 0)
453 if (strcmp (element_name, "choice") == 0)
457 if (COLLECT (STRING, "value", &value))
458 g_string_append_printf (state->choices, "%s\xff", value);
465 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
466 "Element <%s> not allowed inside <%s>\n",
467 element_name, container);
469 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
470 "Element <%s> not allowed at toplevel\n", element_name);
474 end_element (GMarkupParseContext *context,
475 const gchar *element_name,
479 ParseState *state = user_data;
481 if (strcmp (element_name, "default") == 0)
483 state->value = g_variant_parse (state->type, state->string->str,
485 if (state->value == NULL)
494 /* Contextified messages are supported by prepending the
495 * context, followed by '\004' to the start of the message
496 * string. We do that here to save GSettings the work
499 * Note: we are about to g_free() the context anyway...
501 len = strlen (state->context);
502 state->context[len] = '\004';
503 g_string_prepend_len (state->string, state->context, len + 1);
506 g_variant_builder_add (&state->key_options, "{sv}", "l10n",
507 g_variant_new ("(ys)",
509 state->string->str));
512 g_string_free (state->string, TRUE);
513 state->string = NULL;
514 g_free (state->context);
515 state->context = NULL;
518 else if (strcmp (element_name, "key") == 0)
520 if (state->value == NULL)
522 g_set_error_literal (error,
523 G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
524 "element <default> is required in <key>\n");
528 if (state->min != NULL)
530 if (g_variant_compare (state->value, state->min) < 0 ||
531 g_variant_compare (state->value, state->max) > 0)
533 g_set_error (error, G_MARKUP_ERROR,
534 G_MARKUP_ERROR_INVALID_CONTENT,
535 "<default> is not contained in the specified range");
539 state->min = state->max = NULL;
541 else if (state->choices != NULL)
543 if (!is_valid_choices (state->value, state->choices->str))
545 g_set_error_literal (error, G_MARKUP_ERROR,
546 G_MARKUP_ERROR_INVALID_CONTENT,
547 "<default> contains string not in <choices>");
551 state->choices = NULL;
554 gvdb_item_set_value (state->key,
555 g_variant_new ("(*a{sv})", state->value,
556 &state->key_options));
560 else if (strcmp (element_name, "summary") == 0 ||
561 strcmp (element_name, "description") == 0)
563 g_string_free (state->string, TRUE);
564 state->string = NULL;
567 else if (strcmp (element_name, "choices") == 0)
571 choices = g_string_free (state->choices, FALSE);
572 g_variant_builder_add (&state->key_options, "{sv}", "choices",
573 g_variant_new_byte_array (choices, -1));
579 text (GMarkupParseContext *context,
585 ParseState *state = user_data;
588 for (i = 0; i < text_len; i++)
589 if (!g_ascii_isspace (text[i]))
592 g_string_append_len (state->string, text, text_len);
595 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
596 "text may not appear inside <%s>\n",
597 g_markup_parse_context_get_element (context));
604 parse_gschema_files (gchar **files,
608 GMarkupParser parser = { start_element, end_element, text };
609 GMarkupParseContext *context;
610 ParseState state = { byteswap, };
611 const gchar *filename;
613 context = g_markup_parse_context_new (&parser,
614 G_MARKUP_PREFIX_ERROR_POSITION,
616 state.schemas = gvdb_hash_table_new (NULL, NULL);
618 while ((filename = *files++) != NULL)
623 if (!g_file_get_contents (filename, &contents, &size, error))
626 if (!g_markup_parse_context_parse (context, contents, size, error))
628 g_prefix_error (error, "%s: ", filename);
632 if (!g_markup_parse_context_end_parse (context, error))
634 g_prefix_error (error, "%s: ", filename);
639 return state.schemas;
643 main (int argc, char **argv)
645 gboolean byteswap = G_BYTE_ORDER != G_LITTLE_ENDIAN;
651 gchar *targetdir = NULL;
653 gboolean uninstall = FALSE;
654 gboolean dry_run = FALSE;
655 gchar **schema_files = NULL;
656 GOptionContext *context;
657 GOptionEntry entries[] = {
658 { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
659 { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
660 { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("Do not give error for empty directory"), NULL },
661 { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
663 /* These options are only for use in the gschema-compile tests */
664 { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
668 setlocale (LC_ALL, "");
670 context = g_option_context_new (N_("DIRECTORY"));
671 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
672 g_option_context_set_summary (context,
673 N_("Compile all GSettings schema files into a schema cache.\n"
674 "Schema files are required to have the extension .gschema.xml,\n"
675 "and the cache file is called gschemas.compiled."));
676 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
679 if (!g_option_context_parse (context, &argc, &argv, &error))
681 fprintf (stderr, "%s", error->message);
685 g_option_context_free (context);
687 if (!schema_files && argc != 2)
689 fprintf (stderr, _("You should give exactly one directory name\n"));
695 if (targetdir == NULL)
698 target = g_build_filename (targetdir, "gschemas.compiled", NULL);
704 files = g_ptr_array_new ();
706 dir = g_dir_open (srcdir, 0, &error);
709 fprintf (stderr, "%s\n", error->message);
713 while ((file = g_dir_read_name (dir)) != NULL)
715 if (g_str_has_suffix (file, ".gschema.xml"))
716 g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
728 fprintf (stderr, _("No schema files found\n"));
732 g_ptr_array_add (files, NULL);
734 schema_files = (char **) g_ptr_array_free (files, FALSE);
738 if (!(table = parse_gschema_files (schema_files, byteswap, &error)) ||
739 (!dry_run && !gvdb_table_write_contents (table, target, byteswap, &error)))
741 fprintf (stderr, "%s\n", error->message);