Add --uninstall option to glib-compile-schemas
[platform/upstream/glib.git] / gio / gschema-compile.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include <gstdio.h>
25 #include <locale.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdio.h>
29
30 #include <gi18n.h>
31
32 #include "gvdb/gvdb-builder.h"
33
34 typedef struct
35 {
36   gboolean byteswap;
37
38   GVariantBuilder key_options;
39   GHashTable *schemas;
40   gchar *schemalist_domain;
41
42   GHashTable *schema;
43   GvdbItem *schema_root;
44   gchar *schema_domain;
45
46   GString *string;
47
48   GvdbItem *key;
49   GVariant *value;
50   GVariant *min, *max;
51   GString *choices;
52   gchar l10n;
53   gchar *context;
54   GVariantType *type;
55 } ParseState;
56
57 static gboolean allow_any_name = FALSE;
58
59 static gboolean
60 is_valid_keyname (const gchar  *key,
61                   GError      **error)
62 {
63   gint i;
64
65   if (key[0] == '\0')
66     {
67       g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
68                            "empty names are not permitted");
69       return FALSE;
70     }
71
72   if (allow_any_name)
73     return TRUE;
74
75   if (!g_ascii_islower (key[0]))
76     {
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);
80       return FALSE;
81     }
82
83   for (i = 1; key[i]; i++)
84     {
85       if (key[i] != '-' &&
86           !g_ascii_islower (key[i]) &&
87           !g_ascii_isdigit (key[i]))
88         {
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]);
93           return FALSE;
94         }
95
96       if (key[i] == '-' && key[i + 1] == '-')
97         {
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);
101           return FALSE;
102         }
103     }
104
105   if (key[i - 1] == '-')
106     {
107       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
108                    "invalid name '%s': the last character may not be a "
109                    "dash ('-').", key);
110       return FALSE;
111     }
112
113   if (i > 32)
114     {
115       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
116                    "invalid name '%s': maximum length is 32", key);
117       return FALSE;
118     }
119
120   return TRUE;
121 }
122
123 static gboolean
124 type_allows_choices (const GVariantType *type)
125 {
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));
129
130   return g_variant_type_equal (type, G_VARIANT_TYPE_STRING);
131 }
132
133 static gboolean
134 type_allows_range (const GVariantType *type)
135 {
136   static const char range_types[] = "ynqiuxtd";
137
138   return strchr (range_types, *(const char*) type) != NULL;
139 }
140
141 static gboolean
142 is_valid_choices (GVariant    *variant,
143                   const gchar *choices)
144 {
145   switch (g_variant_classify (variant))
146     {
147       case G_VARIANT_CLASS_MAYBE:
148       case G_VARIANT_CLASS_ARRAY:
149         {
150           gsize i, n;
151           GVariant *child;
152           gboolean is_valid;
153
154           n = g_variant_n_children (variant);
155           for (i = 0; i < n; ++i)
156             {
157               child = g_variant_get_child_value (variant, i);
158               is_valid = is_valid_choices (child, choices);
159               g_variant_unref (child);
160
161               if (!is_valid)
162                 return FALSE;
163             }
164
165           return TRUE;
166         }
167
168       case G_VARIANT_CLASS_STRING:
169         {
170           const gchar *string;
171
172           g_variant_get (variant, "&s", &string);
173
174           while ((choices = strstr (choices, string)) && choices[-1] != 0xff);
175
176           return choices != NULL;
177         }
178
179       default:
180         g_assert_not_reached ();
181     }
182 }
183
184 static void
185 start_element (GMarkupParseContext  *context,
186                const gchar          *element_name,
187                const gchar         **attribute_names,
188                const gchar         **attribute_values,
189                gpointer              user_data,
190                GError              **error)
191 {
192   ParseState *state = user_data;
193   const GSList *element_stack;
194   const gchar *container;
195
196   element_stack = g_markup_parse_context_get_element_stack (context);
197   container = element_stack->next ? element_stack->next->data : NULL;
198
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)
207
208   if (container == NULL)
209     {
210       if (strcmp (element_name, "schemalist") == 0)
211         {
212           COLLECT (OPTIONAL | STRDUP,
213                    "gettext-domain",
214                    &state->schemalist_domain);
215           return;
216         }
217     }
218   else if (strcmp (container, "schemalist") == 0)
219     {
220       if (strcmp (element_name, "schema") == 0)
221         {
222           const gchar *id, *path;
223
224           if (COLLECT (STRING, "id", &id,
225                        OPTIONAL | STRING, "path", &path,
226                        OPTIONAL | STRDUP, "gettext-domain",
227                                           &state->schema_domain))
228             {
229               if (!g_hash_table_lookup (state->schemas, id))
230                 {
231                   state->schema = gvdb_hash_table_new (state->schemas, id);
232                   state->schema_root = gvdb_hash_table_insert (state->schema, "");
233
234                   if (path != NULL)
235                     gvdb_hash_table_insert_string (state->schema,
236                                                    ".path", path);
237                 }
238               else
239                 g_set_error (error, G_MARKUP_ERROR,
240                              G_MARKUP_ERROR_INVALID_CONTENT,
241                              "<schema id='%s'> already specified", id);
242             }
243           return;
244         }
245     }
246   else if (strcmp (container, "schema") == 0)
247     {
248       if (strcmp (element_name, "key") == 0)
249         {
250           const gchar *name, *type;
251
252           if (COLLECT (STRING, "name", &name, STRING, "type", &type))
253             {
254               if (!is_valid_keyname (name, error))
255                 return;
256
257               if (!g_hash_table_lookup (state->schema, name))
258                 {
259                   state->key = gvdb_hash_table_insert (state->schema, name);
260                   gvdb_item_set_parent (state->key, state->schema_root);
261                 }
262               else
263                 {
264                   g_set_error (error, G_MARKUP_ERROR,
265                                G_MARKUP_ERROR_INVALID_CONTENT,
266                                "<key name='%s'> already specified", name);
267                   return;
268                 }
269
270               if (g_variant_type_string_is_valid (type))
271                 state->type = g_variant_type_new (type);
272               else
273                 {
274                   g_set_error (error, G_MARKUP_ERROR,
275                                G_MARKUP_ERROR_INVALID_CONTENT,
276                                "invalid GVariant type string '%s'", type);
277                   return;
278                 }
279
280               g_variant_builder_init (&state->key_options,
281                                       G_VARIANT_TYPE ("a{sv}"));
282             }
283
284           return;
285         }
286       else if (strcmp (element_name, "child") == 0)
287         {
288           const gchar *name, *schema;
289
290           if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
291             {
292               gchar *childname;
293
294               if (!is_valid_keyname (name, error))
295                 return;
296
297               childname = g_strconcat (name, "/", NULL);
298
299               if (!g_hash_table_lookup (state->schema, childname))
300                 gvdb_hash_table_insert_string (state->schema, childname, schema);
301               else
302                 g_set_error (error, G_MARKUP_ERROR,
303                              G_MARKUP_ERROR_INVALID_CONTENT,
304                              "<child name='%s'> already specified", name);
305
306               g_free (childname);
307               return;
308             }
309         }
310     }
311   else if (strcmp (container, "key") == 0)
312     {
313       if (strcmp (element_name, "default") == 0)
314         {
315           const gchar *l10n;
316
317           if (COLLECT (STRING | OPTIONAL, "l10n", &l10n,
318                        STRDUP | OPTIONAL, "context", &state->context))
319             {
320               if (l10n != NULL)
321                 {
322                   if (!g_hash_table_lookup (state->schema, ".gettext-domain"))
323                     {
324                       const gchar *domain = state->schema_domain ?
325                                             state->schema_domain :
326                                             state->schemalist_domain;
327
328                       if (domain == NULL)
329                         {
330                           g_set_error_literal (error, G_MARKUP_ERROR,
331                                                G_MARKUP_ERROR_INVALID_CONTENT,
332                                                "l10n requested, but no "
333                                                "gettext domain given");
334                           return;
335                         }
336
337                       gvdb_hash_table_insert_string (state->schema,
338                                                      ".gettext-domain",
339                                                      domain);
340
341                       if (strcmp (l10n, "messages") == 0)
342                         state->l10n = 'm';
343                       else if (strcmp (l10n, "time") == 0)
344                         state->l10n = 't';
345                       else
346                         {
347                           g_set_error (error, G_MARKUP_ERROR,
348                                        G_MARKUP_ERROR_INVALID_CONTENT,
349                                        "unsupported l10n category: %s", l10n);
350                           return;
351                         }
352                     }
353                 }
354               else
355                 {
356                   state->l10n = '\0';
357
358                   if (state->context != NULL)
359                     {
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");
364                       return;
365                     }
366                 }
367
368               state->string = g_string_new (NULL);
369             }
370
371           return;
372         }
373       else if (strcmp (element_name, "summary") == 0 ||
374                strcmp (element_name, "description") == 0)
375         {
376           state->string = g_string_new (NULL);
377           NO_ATTRS ();
378           return;
379         }
380       else if (strcmp (element_name, "range") == 0)
381         {
382           const gchar *min_str, *max_str;
383
384           if (!type_allows_range (state->type))
385             {
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",
390                           element_name, type);
391               g_free (type);
392               return;
393             }
394
395           if (!COLLECT (STRING, "min", &min_str,
396                         STRING, "max", &max_str))
397             return;
398
399           state->min = g_variant_parse (state->type, min_str, NULL, NULL, error);
400           if (state->min == NULL)
401             return;
402
403           state->max = g_variant_parse (state->type, max_str, NULL, NULL, error);
404           if (state->max == NULL)
405             return;
406
407           if (g_variant_compare (state->min, state->max) > 0)
408             {
409               g_set_error (error, G_MARKUP_ERROR,
410                            G_MARKUP_ERROR_INVALID_CONTENT,
411                            "Element <%s> specified minimum is greater than maxmimum",
412                            element_name);
413               return;
414             }
415
416           g_variant_builder_add (&state->key_options, "{sv}", "range",
417                                  g_variant_new ("(@?@?)", state->min, state->max));
418           return;
419         }
420       else if (strcmp (element_name, "choices") == 0)
421         {
422           if (!type_allows_choices (state->type))
423             {
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",
428                            element_name, type);
429               g_free (type);
430               return;
431             }
432
433           state->choices = g_string_new ("\xff");
434
435           NO_ATTRS ();
436           return;
437         }
438     }
439   else if (strcmp (container, "choices") == 0)
440     {
441       if (strcmp (element_name, "choice") == 0)
442         {
443           const gchar *value;
444
445           if (COLLECT (STRING, "value", &value))
446             g_string_append_printf (state->choices, "%s\xff", value);
447
448           return;
449         }
450     }
451
452   if (container)
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);
456   else
457     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
458                  "Element <%s> not allowed at toplevel\n", element_name);
459 }
460
461 static void
462 end_element (GMarkupParseContext  *context,
463              const gchar          *element_name,
464              gpointer              user_data,
465              GError              **error)
466 {
467   ParseState *state = user_data;
468
469   if (strcmp (element_name, "default") == 0)
470     {
471       state->value = g_variant_parse (state->type, state->string->str,
472                                       NULL, NULL, error);
473       if (state->value == NULL)
474         return;
475
476       if (state->l10n)
477         {
478           if (state->context)
479             {
480               gint len;
481
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
485                * later on.
486                *
487                * Note: we are about to g_free() the context anyway...
488                */
489               len = strlen (state->context);
490               state->context[len] = '\004';
491               g_string_prepend_len (state->string, state->context, len + 1);
492             }
493
494           g_variant_builder_add (&state->key_options, "{sv}", "l10n",
495                                  g_variant_new ("(ys)",
496                                                 state->l10n,
497                                                 state->string->str));
498         }
499
500       g_string_free (state->string, TRUE);
501       state->string = NULL;
502       g_free (state->context);
503       state->context = NULL;
504     }
505
506   else if (strcmp (element_name, "key") == 0)
507     {
508       if (state->value == NULL)
509         {
510           g_set_error_literal (error,
511                                G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
512                                "element <default> is required in <key>\n");
513           return;
514         }
515
516       if (state->min != NULL)
517         {
518           if (g_variant_compare (state->value, state->min) < 0 ||
519               g_variant_compare (state->value, state->max) > 0)
520             {
521               g_set_error (error, G_MARKUP_ERROR,
522                            G_MARKUP_ERROR_INVALID_CONTENT,
523                            "<default> is not contained in the specified range");
524               return;
525             }
526
527           state->min = state->max = NULL;
528         }
529       else if (state->choices != NULL)
530         {
531           if (!is_valid_choices (state->value, state->choices->str))
532             {
533               g_set_error_literal (error, G_MARKUP_ERROR,
534                                    G_MARKUP_ERROR_INVALID_CONTENT,
535                                    "<default> contains string not in <choices>");
536               return;
537             }
538
539           state->choices = NULL;
540         }
541
542       gvdb_item_set_value (state->key, state->value);
543       gvdb_item_set_options (state->key,
544                              g_variant_builder_end (&state->key_options));
545
546       state->value = NULL;
547     }
548
549   else if (strcmp (element_name, "summary") == 0 ||
550            strcmp (element_name, "description") == 0)
551     {
552       g_string_free (state->string, TRUE);
553       state->string = NULL;
554     }
555
556   else if (strcmp (element_name, "choices") == 0)
557     {
558       gchar *choices;
559
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));
563       g_free (choices);
564     }
565 }
566
567 static void
568 text (GMarkupParseContext  *context,
569       const gchar          *text,
570       gsize                 text_len,
571       gpointer              user_data,
572       GError              **error)
573 {
574   ParseState *state = user_data;
575   gsize i;
576
577   for (i = 0; i < text_len; i++)
578     if (!g_ascii_isspace (text[i]))
579       {
580         if (state->string)
581           g_string_append_len (state->string, text, text_len);
582
583         else
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));
587
588         break;
589       }
590 }
591
592 static GHashTable *
593 parse_gschema_files (gchar    **files,
594                      gboolean   byteswap,
595                      GError   **error)
596 {
597   GMarkupParser parser = { start_element, end_element, text };
598   GMarkupParseContext *context;
599   ParseState state = { byteswap, };
600   const gchar *filename;
601
602   context = g_markup_parse_context_new (&parser,
603                                         G_MARKUP_PREFIX_ERROR_POSITION,
604                                         &state, NULL);
605   state.schemas = gvdb_hash_table_new (NULL, NULL);
606
607   while ((filename = *files++) != NULL)
608     {
609       gchar *contents;
610       gsize size;
611
612       if (!g_file_get_contents (filename, &contents, &size, error))
613         return FALSE;
614
615       if (!g_markup_parse_context_parse (context, contents, size, error))
616         {
617           g_prefix_error (error, "%s: ", filename);
618           return FALSE;
619         }
620
621       if (!g_markup_parse_context_end_parse (context, error))
622         {
623           g_prefix_error (error, "%s: ", filename);
624           return FALSE;
625         }
626     }
627
628   return state.schemas;
629 }
630
631 int
632 main (int argc, char **argv)
633 {
634   gboolean byteswap = G_BYTE_ORDER != G_LITTLE_ENDIAN;
635   GError *error;
636   GHashTable *table;
637   GDir *dir;
638   const gchar *file;
639   gchar *srcdir;
640   gchar *targetdir = NULL;
641   gchar *target;
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") },
651
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 },
654     { NULL }
655   };
656
657   setlocale (LC_ALL, "");
658
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);
666
667   error = NULL;
668   if (!g_option_context_parse (context, &argc, &argv, &error))
669     {
670       fprintf (stderr, "%s", error->message);
671       return 1;
672     }
673
674   g_option_context_free (context);
675
676   if (!schema_files && argc != 2)
677     {
678       fprintf (stderr, _("You should give exactly one directory name\n"));
679       return 1;
680     }
681
682   srcdir = argv[1];
683
684   if (targetdir == NULL)
685     targetdir = srcdir;
686
687   target = g_build_filename (targetdir, "gschemas.compiled", NULL);
688
689   if (!schema_files)
690     {
691       GPtrArray *files;
692
693       files = g_ptr_array_new ();
694
695       dir = g_dir_open (srcdir, 0, &error);
696       if (dir == NULL)
697         {
698           fprintf (stderr, "%s\n", error->message);
699           return 1;
700         }
701
702       while ((file = g_dir_read_name (dir)) != NULL)
703         {
704           if (g_str_has_suffix (file, ".gschema.xml"))
705             g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
706         }
707
708       if (files->len == 0)
709         {
710           if (uninstall)
711             {
712               g_unlink (target);
713               return 0;
714             }
715           else
716             {
717               fprintf (stderr, _("No schema files found\n"));
718               return 1;
719             }
720         }
721       g_ptr_array_add (files, NULL);
722
723       schema_files = (char **) g_ptr_array_free (files, FALSE);
724     }
725
726
727   if (!(table = parse_gschema_files (schema_files, byteswap, &error)) ||
728       (!dry_run && !gvdb_table_write_contents (table, target, byteswap, &error)))
729     {
730       fprintf (stderr, "%s\n", error->message);
731       return 1;
732     }
733
734   g_free (target);
735
736   return 0;
737 }