37ddf33e81ea9a87ec6432e3ed0ff4ddb8725dba
[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 /* Prologue {{{1 */
23 #define _GNU_SOURCE
24 #include "config.h"
25
26 #include <gstdio.h>
27 #include <locale.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdio.h>
31
32 #include <gi18n.h>
33
34 #include "gvdb/gvdb-builder.h"
35 #include "strinfo.c"
36
37 /* Handling of <enum> {{{1 */
38 typedef GString EnumState;
39
40 static void
41 enum_state_free (gpointer data)
42 {
43   EnumState *state = data;
44
45   g_string_free (state, TRUE);
46 }
47
48 static void
49 enum_state_add_value (EnumState    *state,
50                       const gchar  *nick,
51                       const gchar  *valuestr,
52                       GError      **error)
53 {
54   gint64 value;
55   gchar *end;
56
57   if (nick[0] == '\0' || nick[1] == '\0')
58     {
59       g_set_error (error, G_MARKUP_ERROR,
60                    G_MARKUP_ERROR_INVALID_CONTENT,
61                    "enum nick must be a minimum of 2 characters");
62       return;
63     }
64
65   value = g_ascii_strtoll (valuestr, &end, 0);
66   if (*end || value > G_MAXINT32 || value < G_MININT32)
67     {
68       g_set_error (error, G_MARKUP_ERROR,
69                    G_MARKUP_ERROR_INVALID_CONTENT,
70                    "invalid numeric value");
71       return;
72     }
73
74   if (strinfo_builder_contains (state, nick))
75     {
76       g_set_error (error, G_MARKUP_ERROR,
77                    G_MARKUP_ERROR_INVALID_CONTENT,
78                    "<value nick='%s'> already specified", nick);
79       return;
80     }
81
82   strinfo_builder_append_item (state, nick, value);
83 }
84
85 static void
86 enum_state_end (EnumState **state_ptr,
87                 GError    **error)
88 {
89   EnumState *state;
90
91   state = *state_ptr;
92   *state_ptr = NULL;
93
94   if (state->len == 0)
95     g_set_error_literal (error,
96                          G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
97                          "<enum> must contain at least one <value>");
98 }
99
100 /* Handling of <key> {{{1 */
101 typedef struct
102 {
103   /* for <child>, @child_schema will be set.
104    * for <key>, everything else will be set.
105    */
106   gchar        *child_schema;
107
108
109   GVariantType *type;
110   gboolean      have_gettext_domain;
111
112   gchar         l10n;
113   gchar        *l10n_context;
114   GString      *unparsed_default_value;
115   GVariant     *default_value;
116
117   GString      *strinfo;
118   gboolean      is_enum;
119
120   GVariant     *minimum;
121   GVariant     *maximum;
122
123   gboolean      has_choices;
124   gboolean      has_aliases;
125   gboolean      is_override;
126
127   gboolean      checked;
128   GVariant     *serialised;
129 } KeyState;
130
131 static KeyState *
132 key_state_new (const gchar *type_string,
133                const gchar *gettext_domain,
134                EnumState   *enum_data)
135 {
136   KeyState *state;
137
138   state = g_slice_new0 (KeyState);
139   state->type = g_variant_type_new (type_string);
140   state->have_gettext_domain = gettext_domain != NULL;
141
142   if ((state->is_enum = (enum_data != NULL)))
143     state->strinfo = g_string_new_len (enum_data->str, enum_data->len);
144   else
145     state->strinfo = g_string_new (NULL);
146
147   return state;
148 }
149
150 static KeyState *
151 key_state_override (KeyState    *state,
152                     const gchar *gettext_domain)
153 {
154   KeyState *copy;
155
156   copy = g_slice_new0 (KeyState);
157   copy->type = g_variant_type_copy (state->type);
158   copy->have_gettext_domain = gettext_domain != NULL;
159   copy->strinfo = g_string_new_len (state->strinfo->str,
160                                     state->strinfo->len);
161   copy->is_enum = state->is_enum;
162   copy->is_override = TRUE;
163
164   if (state->minimum)
165     {
166       copy->minimum = g_variant_ref (state->minimum);
167       copy->maximum = g_variant_ref (state->maximum);
168     }
169
170   return copy;
171 }
172
173 static KeyState *
174 key_state_new_child (const gchar *child_schema)
175 {
176   KeyState *state;
177
178   state = g_slice_new0 (KeyState);
179   state->child_schema = g_strdup (child_schema);
180
181   return state;
182 }
183
184 static gboolean
185 is_valid_choices (GVariant *variant,
186                   GString  *strinfo)
187 {
188   switch (g_variant_classify (variant))
189     {
190       case G_VARIANT_CLASS_MAYBE:
191       case G_VARIANT_CLASS_ARRAY:
192         {
193           gboolean valid = TRUE;
194           GVariantIter iter;
195
196           g_variant_iter_init (&iter, variant);
197
198           while (valid && (variant = g_variant_iter_next_value (&iter)))
199             {
200               valid = is_valid_choices (variant, strinfo);
201               g_variant_unref (variant);
202             }
203
204           return valid;
205         }
206
207       case G_VARIANT_CLASS_STRING:
208         return strinfo_is_string_valid ((const guint32 *) strinfo->str,
209                                         strinfo->len / 4,
210                                         g_variant_get_string (variant, NULL));
211
212       default:
213         g_assert_not_reached ();
214     }
215 }
216
217
218 /* Gets called at </default> </choices> or <range/> to check for
219  * validity of the default value so that any inconsistency is
220  * reported as soon as it is encountered.
221  */
222 static void
223 key_state_check_range (KeyState  *state,
224                        GError   **error)
225 {
226   if (state->default_value)
227     {
228       const gchar *tag;
229
230       tag = state->is_override ? "override" : "default";
231
232       if (state->minimum)
233         {
234           if (g_variant_compare (state->default_value, state->minimum) < 0 ||
235               g_variant_compare (state->default_value, state->maximum) > 0)
236             {
237               g_set_error (error, G_MARKUP_ERROR,
238                            G_MARKUP_ERROR_INVALID_CONTENT,
239                            "<%s> is not contained in "
240                            "the specified range", tag);
241             }
242         }
243
244       else if (state->strinfo->len)
245         {
246           if (!is_valid_choices (state->default_value, state->strinfo))
247             {
248               if (state->is_enum)
249                 g_set_error (error, G_MARKUP_ERROR,
250                              G_MARKUP_ERROR_INVALID_CONTENT,
251                              "<%s> is not a valid member of "
252                              "the specified enumerated type", tag);
253
254               else
255                 g_set_error (error, G_MARKUP_ERROR,
256                              G_MARKUP_ERROR_INVALID_CONTENT,
257                              "<%s> contains string not in "
258                              "<choices>", tag);
259             }
260         }
261     }
262 }
263
264 static void
265 key_state_set_range (KeyState     *state,
266                      const gchar  *min_str,
267                      const gchar  *max_str,
268                      GError      **error)
269 {
270   if (state->minimum)
271     {
272       g_set_error_literal (error, G_MARKUP_ERROR,
273                            G_MARKUP_ERROR_INVALID_CONTENT,
274                            "<range/> already specified for this key");
275       return;
276     }
277
278   if (strchr ("ynqiuxtd", *(char *) state->type) == NULL)
279     {
280       gchar *type = g_variant_type_dup_string (state->type);
281       g_set_error (error, G_MARKUP_ERROR,
282                   G_MARKUP_ERROR_INVALID_CONTENT,
283                   "<range> not allowed for keys of type '%s'", type);
284       g_free (type);
285       return;
286     }
287
288   state->minimum = g_variant_parse (state->type, min_str, NULL, NULL, error);
289   if (state->minimum == NULL)
290     return;
291
292   state->maximum = g_variant_parse (state->type, max_str, NULL, NULL, error);
293   if (state->maximum == NULL)
294     return;
295
296   if (g_variant_compare (state->minimum, state->maximum) > 0)
297     {
298       g_set_error (error, G_MARKUP_ERROR,
299                    G_MARKUP_ERROR_INVALID_CONTENT,
300                    "<range> specified minimum is greater than maxmimum");
301       return;
302     }
303
304   key_state_check_range (state, error);
305 }
306
307 static GString *
308 key_state_start_default (KeyState     *state,
309                          const gchar  *l10n,
310                          const gchar  *context,
311                          GError      **error)
312 {
313   if (l10n != NULL)
314     {
315       if (strcmp (l10n, "messages") == 0)
316         state->l10n = 'm';
317
318       else if (strcmp (l10n, "time") == 0)
319         state->l10n = 't';
320
321       else
322         {
323           g_set_error (error, G_MARKUP_ERROR,
324                        G_MARKUP_ERROR_INVALID_CONTENT,
325                        "unsupported l10n category: %s", l10n);
326           return NULL;
327         }
328
329       if (!state->have_gettext_domain)
330         {
331           g_set_error_literal (error, G_MARKUP_ERROR,
332                                G_MARKUP_ERROR_INVALID_CONTENT,
333                                "l10n requested, but no "
334                                "gettext domain given");
335           return NULL;
336         }
337
338       state->l10n_context = g_strdup (context);
339     }
340
341   else if (context != NULL)
342     {
343       g_set_error_literal (error, G_MARKUP_ERROR,
344                            G_MARKUP_ERROR_INVALID_CONTENT,
345                            "translation context given for "
346                            " value without l10n enabled");
347       return NULL;
348     }
349
350   return g_string_new (NULL);
351 }
352
353 static void
354 key_state_end_default (KeyState  *state,
355                        GString  **string,
356                        GError   **error)
357 {
358   state->unparsed_default_value = *string;
359   *string = NULL;
360
361   state->default_value = g_variant_parse (state->type,
362                                           state->unparsed_default_value->str,
363                                           NULL, NULL, error);
364   key_state_check_range (state, error);
365 }
366
367 static void
368 key_state_start_choices (KeyState  *state,
369                          GError   **error)
370 {
371   const GVariantType *type = state->type;
372
373   if (state->is_enum)
374     {
375       g_set_error_literal (error, G_MARKUP_ERROR,
376                            G_MARKUP_ERROR_INVALID_CONTENT,
377                            "<choices> can not be specified for keys "
378                            "tagged as having an enumerated type");
379       return;
380     }
381
382   if (state->has_choices)
383     {
384       g_set_error_literal (error, G_MARKUP_ERROR,
385                            G_MARKUP_ERROR_INVALID_CONTENT,
386                            "<choices> already specified for this key");
387       return;
388     }
389
390   while (g_variant_type_is_maybe (type) || g_variant_type_is_array (type))
391     type = g_variant_type_element (type);
392
393   if (!g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
394     {
395       gchar *type_string = g_variant_type_dup_string (state->type);
396       g_set_error (error, G_MARKUP_ERROR,
397                    G_MARKUP_ERROR_INVALID_CONTENT,
398                    "<choices> not allowed for keys of type '%s'",
399                    type_string);
400       g_free (type_string);
401       return;
402     }
403 }
404
405 static void
406 key_state_add_choice (KeyState     *state,
407                       const gchar  *choice,
408                       GError      **error)
409 {
410   if (strinfo_builder_contains (state->strinfo, choice))
411     {
412       g_set_error (error, G_MARKUP_ERROR,
413                    G_MARKUP_ERROR_INVALID_CONTENT,
414                    "<choice value='%s'/> already given", choice);
415       return;
416     }
417
418   strinfo_builder_append_item (state->strinfo, choice, 0);
419   state->has_choices = TRUE;
420 }
421
422 static void
423 key_state_end_choices (KeyState  *state,
424                        GError   **error)
425 {
426   if (!state->has_choices)
427     {
428       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
429                    "<choices> must contain at least one <choice>");
430       return;
431     }
432
433   key_state_check_range (state, error);
434 }
435
436 static void
437 key_state_start_aliases (KeyState  *state,
438                          GError   **error)
439 {
440   if (state->has_aliases)
441     g_set_error_literal (error, G_MARKUP_ERROR,
442                          G_MARKUP_ERROR_INVALID_CONTENT,
443                          "<aliases> already specified for this key");
444
445   if (!state->is_enum && !state->has_choices)
446     g_set_error_literal (error, G_MARKUP_ERROR,
447                          G_MARKUP_ERROR_INVALID_CONTENT,
448                          "<aliases> can only be specified for keys with "
449                          "enumerated types or after <choices>");
450 }
451
452 static void
453 key_state_add_alias (KeyState     *state,
454                      const gchar  *alias,
455                      const gchar  *target,
456                      GError      **error)
457 {
458   if (strinfo_builder_contains (state->strinfo, alias))
459     {
460       if (strinfo_is_string_valid ((guint32 *) state->strinfo->str,
461                                    state->strinfo->len / 4,
462                                    alias))
463         {
464           if (state->is_enum)
465             g_set_error (error, G_MARKUP_ERROR,
466                          G_MARKUP_ERROR_INVALID_CONTENT,
467                          "<alias value='%s'/> given when '%s' is already "
468                          "a member of the enumerated type", alias, alias);
469
470           else
471             g_set_error (error, G_MARKUP_ERROR,
472                          G_MARKUP_ERROR_INVALID_CONTENT,
473                          "<alias value='%s'/> given when "
474                          "<choice value='%s'/> was already given",
475                          alias, alias);
476         }
477
478       else
479         g_set_error (error, G_MARKUP_ERROR,
480                      G_MARKUP_ERROR_INVALID_CONTENT,
481                      "<alias value='%s'/> already specified", alias);
482
483       return;
484     }
485
486   if (!strinfo_builder_append_alias (state->strinfo, alias, target))
487     {
488       g_set_error (error, G_MARKUP_ERROR,
489                    G_MARKUP_ERROR_INVALID_CONTENT,
490                    "alias target '%s' is not in %s", target,
491                    state->is_enum ? "enumerated type" : "<choices>");
492       return;
493     }
494
495   state->has_aliases = TRUE;
496 }
497
498 static void
499 key_state_end_aliases (KeyState  *state,
500                        GError   **error)
501 {
502   if (!state->has_aliases)
503     {
504       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
505                    "<aliases> must contain at least one <alias>");
506       return;
507     }
508 }
509
510 static gboolean
511 key_state_check (KeyState  *state,
512                  GError   **error)
513 {
514   if (state->checked)
515     return TRUE;
516
517   return state->checked = TRUE;
518 }
519
520 static GVariant *
521 key_state_serialise (KeyState *state)
522 {
523   if (state->serialised == NULL)
524     {
525       if (state->child_schema)
526         {
527           state->serialised = g_variant_new_string (state->child_schema);
528         }
529
530       else
531         {
532           GVariantBuilder builder;
533
534           g_assert (key_state_check (state, NULL));
535
536           g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
537
538           /* default value */
539           g_variant_builder_add_value (&builder, state->default_value);
540
541           /* translation */
542           if (state->l10n)
543             {
544               if (state->l10n_context)
545                 {
546                   gint len;
547
548                   /* Contextified messages are supported by prepending
549                    * the context, followed by '\004' to the start of the
550                    * message string.  We do that here to save GSettings
551                    * the work later on.
552                    */
553                   len = strlen (state->l10n_context);
554                   state->l10n_context[len] = '\004';
555                   g_string_prepend_len (state->unparsed_default_value,
556                                         state->l10n_context, len + 1);
557                   g_free (state->l10n_context);
558                   state->l10n_context = NULL;
559                 }
560
561               g_variant_builder_add (&builder, "(y(y&s))", 'l', state->l10n,
562                                      state->unparsed_default_value);
563               g_string_free (state->unparsed_default_value, TRUE);
564               state->unparsed_default_value = NULL;
565             }
566
567           /* choice, aliases, enums */
568           if (state->strinfo->len)
569             {
570               GVariant *array;
571               gpointer data;
572               gsize size;
573
574               data = state->strinfo->str;
575               size = state->strinfo->len;
576
577               array = g_variant_new_from_data (G_VARIANT_TYPE ("au"),
578                                                data, size, TRUE,
579                                                g_free, data);
580
581               g_string_free (state->strinfo, FALSE);
582               state->strinfo = NULL;
583
584               g_variant_builder_add (&builder, "(y@au)",
585                                      state->is_enum ? 'e' : 'c',
586                                      array);
587             }
588
589           /* range */
590           if (state->minimum || state->maximum)
591             g_variant_builder_add (&builder, "(y(**))", 'r',
592                                    state->minimum, state->maximum);
593
594           state->serialised = g_variant_builder_end (&builder);
595         }
596
597       g_variant_ref_sink (state->serialised);
598     }
599
600   return g_variant_ref (state->serialised);
601 }
602
603 static void
604 key_state_free (gpointer data)
605 {
606   KeyState *state = data;
607
608   if (state->type)
609     g_variant_type_free (state->type);
610
611   g_free (state->l10n_context);
612
613   if (state->unparsed_default_value)
614     g_string_free (state->unparsed_default_value, TRUE);
615
616   if (state->default_value)
617     g_variant_unref (state->default_value);
618
619   if (state->strinfo)
620     g_string_free (state->strinfo, TRUE);
621
622   if (state->minimum)
623     g_variant_unref (state->minimum);
624
625   if (state->maximum)
626     g_variant_unref (state->maximum);
627
628   if (state->serialised)
629     g_variant_unref (state->serialised);
630
631   g_slice_free (KeyState, state);
632 }
633
634 /* Key name validity {{{1 */
635 static gboolean allow_any_name = FALSE;
636
637 static gboolean
638 is_valid_keyname (const gchar  *key,
639                   GError      **error)
640 {
641   gint i;
642
643   if (key[0] == '\0')
644     {
645       g_set_error_literal (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
646                            "empty names are not permitted");
647       return FALSE;
648     }
649
650   if (allow_any_name)
651     return TRUE;
652
653   if (!g_ascii_islower (key[0]))
654     {
655       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
656                    "invalid name '%s': names must begin "
657                    "with a lowercase letter", key);
658       return FALSE;
659     }
660
661   for (i = 1; key[i]; i++)
662     {
663       if (key[i] != '-' &&
664           !g_ascii_islower (key[i]) &&
665           !g_ascii_isdigit (key[i]))
666         {
667           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
668                        "invalid name '%s': invalid character '%c'; "
669                        "only lowercase letters, numbers and dash ('-') "
670                        "are permitted.", key, key[i]);
671           return FALSE;
672         }
673
674       if (key[i] == '-' && key[i + 1] == '-')
675         {
676           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
677                        "invalid name '%s': two successive dashes ('--') are "
678                        "not permitted.", key);
679           return FALSE;
680         }
681     }
682
683   if (key[i - 1] == '-')
684     {
685       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
686                    "invalid name '%s': the last character may not be a "
687                    "dash ('-').", key);
688       return FALSE;
689     }
690
691   if (i > 32)
692     {
693       g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
694                    "invalid name '%s': maximum length is 32", key);
695       return FALSE;
696     }
697
698   return TRUE;
699 }
700
701 /* Handling of <schema> {{{1 */
702 typedef struct _SchemaState SchemaState;
703 struct _SchemaState
704 {
705   SchemaState *extends;
706
707   gchar       *path;
708   gchar       *gettext_domain;
709   gchar       *extends_name;
710   gchar       *list_of;
711
712   GHashTable  *keys;
713 };
714
715 static SchemaState *
716 schema_state_new (const gchar  *path,
717                   const gchar  *gettext_domain,
718                   SchemaState  *extends,
719                   const gchar  *extends_name,
720                   const gchar  *list_of)
721 {
722   SchemaState *state;
723
724   state = g_slice_new (SchemaState);
725   state->path = g_strdup (path);
726   state->gettext_domain = g_strdup (gettext_domain);
727   state->extends = extends;
728   state->extends_name = g_strdup (extends_name);
729   state->list_of = g_strdup (list_of);
730   state->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
731                                        g_free, key_state_free);
732
733   return state;
734 }
735
736 static void
737 schema_state_free (gpointer data)
738 {
739   SchemaState *state = data;
740
741   g_free (state->path);
742   g_free (state->gettext_domain);
743   g_hash_table_unref (state->keys);
744 }
745
746 static void
747 schema_state_add_child (SchemaState  *state,
748                         const gchar  *name,
749                         const gchar  *schema,
750                         GError      **error)
751 {
752   gchar *childname;
753
754   if (!is_valid_keyname (name, error))
755     return;
756
757   childname = g_strconcat (name, "/", NULL);
758
759   if (g_hash_table_lookup (state->keys, childname))
760     {
761       g_set_error (error, G_MARKUP_ERROR,
762                    G_MARKUP_ERROR_INVALID_CONTENT,
763                    "<child name='%s'> already specified", name);
764       return;
765     }
766
767   g_hash_table_insert (state->keys, childname,
768                        key_state_new_child (schema));
769 }
770
771 static KeyState *
772 schema_state_add_key (SchemaState  *state,
773                       GHashTable   *enum_table,
774                       const gchar  *name,
775                       const gchar  *type_string,
776                       const gchar  *enum_type,
777                       GError      **error)
778 {
779   GString *enum_data;
780   SchemaState *node;
781   KeyState *key;
782
783   if (state->list_of)
784     {
785       g_set_error_literal (error, G_MARKUP_ERROR,
786                            G_MARKUP_ERROR_INVALID_CONTENT,
787                            "can not add keys to a list-of schema");
788       return NULL;
789     }
790
791   if (!is_valid_keyname (name, error))
792     return NULL;
793
794   if (g_hash_table_lookup (state->keys, name))
795     {
796       g_set_error (error, G_MARKUP_ERROR,
797                    G_MARKUP_ERROR_INVALID_CONTENT,
798                    "<key name='%s'> already specified", name);
799       return NULL;
800     }
801
802   for (node = state; node; node = node->extends)
803     if (node->extends)
804       {
805         KeyState *shadow;
806
807         shadow = g_hash_table_lookup (node->extends->keys, name);
808
809         /* in case of <key> <override> <key> make sure we report the
810          * location of the original <key>, not the <override>.
811          */
812         if (shadow && !shadow->is_override)
813           {
814             g_set_error (error, G_MARKUP_ERROR,
815                          G_MARKUP_ERROR_INVALID_CONTENT,
816                          "<key name='%s'> shadows <key name='%s'> in "
817                          "<schema id='%s'>; use <override> to modify value",
818                          name, name, node->extends_name);
819             return NULL;
820           }
821       }
822
823   if ((type_string == NULL) == (enum_type == NULL))
824     {
825       g_set_error (error, G_MARKUP_ERROR,
826                    G_MARKUP_ERROR_MISSING_ATTRIBUTE,
827                    "exactly one of 'type' or 'enum' must "
828                    "be specified as an attribute to <key>");
829       return NULL;
830     }
831
832   if (enum_type != NULL)
833     {
834       enum_data = g_hash_table_lookup (enum_table, enum_type);
835
836       if (enum_data == NULL)
837         {
838           g_set_error (error, G_MARKUP_ERROR,
839                        G_MARKUP_ERROR_INVALID_CONTENT,
840                        "<enum id='%s'> not (yet) defined.", enum_type);
841           return NULL;
842         }
843
844       g_assert (type_string == NULL);
845       type_string = "s";
846     }
847   else
848     {
849       if (!g_variant_type_string_is_valid (type_string))
850         {
851           g_set_error (error, G_MARKUP_ERROR,
852                        G_MARKUP_ERROR_INVALID_CONTENT,
853                        "invalid GVariant type string '%s'", type_string);
854           return NULL;
855         }
856
857       enum_data = NULL;
858     }
859
860   key = key_state_new (type_string, state->gettext_domain, enum_data);
861   g_hash_table_insert (state->keys, g_strdup (name), key);
862
863   return key;
864 }
865
866 static void
867 schema_state_add_override (SchemaState  *state,
868                            KeyState    **key_state,
869                            GString     **string,
870                            const gchar  *key,
871                            const gchar  *l10n,
872                            const gchar  *context,
873                            GError      **error)
874 {
875   SchemaState *parent;
876   KeyState *original;
877
878   if (state->extends == NULL)
879     {
880       g_set_error_literal (error, G_MARKUP_ERROR,
881                            G_MARKUP_ERROR_INVALID_CONTENT,
882                            "<override> given but schema isn't "
883                            "extending anything");
884       return;
885     }
886
887   for (parent = state->extends; parent; parent = parent->extends)
888     if ((original = g_hash_table_lookup (parent->keys, key)))
889       break;
890
891   if (original == NULL)
892     {
893       g_set_error (error, G_MARKUP_ERROR,
894                    G_MARKUP_ERROR_INVALID_CONTENT,
895                    "no <key name='%s'> to override", key);
896       return;
897     }
898
899   if (g_hash_table_lookup (state->keys, key))
900     {
901       g_set_error (error, G_MARKUP_ERROR,
902                    G_MARKUP_ERROR_INVALID_CONTENT,
903                    "<override name='%s'> already specified", key);
904       return;
905     }
906
907   *key_state = key_state_override (original, state->gettext_domain);
908   *string = key_state_start_default (*key_state, l10n, context, error);
909   g_hash_table_insert (state->keys, g_strdup (key), *key_state);
910 }
911
912 static void
913 override_state_end (KeyState **key_state,
914                     GString  **string,
915                     GError   **error)
916 {
917   key_state_end_default (*key_state, string, error);
918   *key_state = NULL;
919 }
920
921 /* Handling of toplevel state {{{1 */
922 typedef struct
923 {
924   GHashTable  *schema_table;            /* string -> SchemaState */
925   GHashTable  *enum_table;              /* string -> GString */
926
927   gchar       *schemalist_domain;       /* the <schemalist> gettext domain */
928
929   SchemaState *schema_state;            /* non-NULL when inside <schema> */
930   KeyState    *key_state;               /* non-NULL when inside <key> */
931   GString     *enum_state;              /* non-NULL when inside <enum> */
932
933   GString     *string;                  /* non-NULL when accepting text */
934 } ParseState;
935
936 static gboolean
937 is_subclass (const gchar *class_name,
938              const gchar *possible_parent,
939              GHashTable  *schema_table)
940 {
941   SchemaState *class;
942
943   if (strcmp (class_name, possible_parent) == 0)
944     return TRUE;
945
946   class = g_hash_table_lookup (schema_table, class_name);
947   g_assert (class != NULL);
948
949   return class->extends_name &&
950          is_subclass (class->extends_name, possible_parent, schema_table);
951 }
952
953 static void
954 parse_state_start_schema (ParseState  *state,
955                           const gchar  *id,
956                           const gchar  *path,
957                           const gchar  *gettext_domain,
958                           const gchar  *extends_name,
959                           const gchar  *list_of,
960                           GError      **error)
961 {
962   SchemaState *extends;
963
964   if (g_hash_table_lookup (state->schema_table, id))
965     {
966       g_set_error (error, G_MARKUP_ERROR,
967                    G_MARKUP_ERROR_INVALID_CONTENT,
968                    "<schema id='%s'> already specified", id);
969       return;
970     }
971
972   if (extends_name)
973     {
974       extends = g_hash_table_lookup (state->schema_table, extends_name);
975
976       if (extends == NULL)
977         {
978           g_set_error (error, G_MARKUP_ERROR,
979                        G_MARKUP_ERROR_INVALID_CONTENT,
980                        "<schema id='%s'> extends not yet "
981                        "existing schema '%s'", id, extends_name);
982           return;
983         }
984     }
985   else
986     extends = NULL;
987
988   if (list_of)
989     {
990       if (!g_hash_table_lookup (state->schema_table, list_of))
991         {
992           g_set_error (error, G_MARKUP_ERROR,
993                        G_MARKUP_ERROR_INVALID_CONTENT,
994                        "<schema id='%s'> is list of not yet "
995                        "existing schema '%s'", id, list_of);
996           return;
997         }
998     }
999
1000   if (extends)
1001     {
1002       if (list_of)
1003         {
1004           if (extends->list_of == NULL)
1005             {
1006               g_set_error (error, G_MARKUP_ERROR,
1007                            G_MARKUP_ERROR_INVALID_CONTENT,
1008                            "<schema id='%s'> is a list, extending "
1009                            "<schema id='%s'> which is not a list",
1010                            id, extends_name);
1011               return;
1012             }
1013
1014           if (!is_subclass (list_of, extends->list_of, state->schema_table))
1015             {
1016               g_set_error (error, G_MARKUP_ERROR,
1017                            G_MARKUP_ERROR_INVALID_CONTENT,
1018                            "<schema id='%s' list-of='%s'> extends <schema "
1019                            "id='%s' list-of='%s'> but '%s' does not extend '%s'",
1020                            id, list_of, extends_name, extends->list_of,
1021                            list_of, extends->list_of);
1022               return;
1023             }
1024         }
1025       else
1026         /* by default we are a list of the same thing that the schema
1027          * we are extending is a list of (which might be nothing)
1028          */
1029         list_of = extends->list_of;
1030     }
1031
1032   if (path && !(g_str_has_prefix (path, "/") && g_str_has_suffix (path, "/")))
1033     {
1034       g_set_error (error, G_MARKUP_ERROR,
1035                    G_MARKUP_ERROR_INVALID_CONTENT,
1036                    "a path, if given, must begin and "
1037                    "end with a slash");
1038       return;
1039     }
1040
1041   state->schema_state = schema_state_new (path, gettext_domain,
1042                                           extends, extends_name, list_of);
1043   g_hash_table_insert (state->schema_table, g_strdup (id),
1044                        state->schema_state);
1045 }
1046
1047 static void
1048 parse_state_start_enum (ParseState   *state,
1049                         const gchar  *id,
1050                         GError      **error)
1051 {
1052   if (g_hash_table_lookup (state->enum_table, id))
1053     {
1054       g_set_error (error, G_MARKUP_ERROR,
1055                    G_MARKUP_ERROR_INVALID_CONTENT,
1056                    "<enum id='%s'> already specified", id);
1057       return;
1058     }
1059
1060   state->enum_state = g_string_new (NULL);
1061   g_hash_table_insert (state->enum_table, g_strdup (id), state->enum_state);
1062 }
1063
1064 /* GMarkup Parser Functions {{{1 */
1065
1066 /* Start element {{{2 */
1067 static void
1068 start_element (GMarkupParseContext  *context,
1069                const gchar          *element_name,
1070                const gchar         **attribute_names,
1071                const gchar         **attribute_values,
1072                gpointer              user_data,
1073                GError              **error)
1074 {
1075   ParseState *state = user_data;
1076   const GSList *element_stack;
1077   const gchar *container;
1078
1079   element_stack = g_markup_parse_context_get_element_stack (context);
1080   container = element_stack->next ? element_stack->next->data : NULL;
1081
1082 #define COLLECT(first, ...) \
1083   g_markup_collect_attributes (element_name,                                 \
1084                                attribute_names, attribute_values, error,     \
1085                                first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
1086 #define OPTIONAL   G_MARKUP_COLLECT_OPTIONAL
1087 #define STRDUP     G_MARKUP_COLLECT_STRDUP
1088 #define STRING     G_MARKUP_COLLECT_STRING
1089 #define NO_ATTRS()  COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
1090
1091   /* Toplevel items {{{3 */
1092   if (container == NULL)
1093     {
1094       if (strcmp (element_name, "schemalist") == 0)
1095         {
1096           COLLECT (OPTIONAL | STRDUP,
1097                    "gettext-domain",
1098                    &state->schemalist_domain);
1099           return;
1100         }
1101     }
1102
1103
1104   /* children of <schemalist> {{{3 */
1105   else if (strcmp (container, "schemalist") == 0)
1106     {
1107       if (strcmp (element_name, "schema") == 0)
1108         {
1109           const gchar *id, *path, *gettext_domain, *extends, *list_of;
1110           if (COLLECT (STRING, "id", &id,
1111                        OPTIONAL | STRING, "path", &path,
1112                        OPTIONAL | STRING, "gettext-domain", &gettext_domain,
1113                        OPTIONAL | STRING, "extends", &extends,
1114                        OPTIONAL | STRING, "list-of", &list_of))
1115             parse_state_start_schema (state, id, path, gettext_domain,
1116                                       extends, list_of, error);
1117           return;
1118         }
1119
1120       else if (strcmp (element_name, "enum") == 0)
1121         {
1122           const gchar *id;
1123           if (COLLECT (STRING, "id", &id))
1124             parse_state_start_enum (state, id, error);
1125           return;
1126         }
1127     }
1128
1129
1130   /* children of <schema> {{{3 */
1131   else if (strcmp (container, "schema") == 0)
1132     {
1133       if (strcmp (element_name, "key") == 0)
1134         {
1135           const gchar *name, *type_string, *enum_type;
1136
1137           if (COLLECT (STRING,            "name", &name,
1138                        OPTIONAL | STRING, "type", &type_string,
1139                        OPTIONAL | STRING, "enum", &enum_type))
1140
1141             state->key_state = schema_state_add_key (state->schema_state,
1142                                                      state->enum_table,
1143                                                      name, type_string,
1144                                                      enum_type, error);
1145           return;
1146         }
1147       else if (strcmp (element_name, "child") == 0)
1148         {
1149           const gchar *name, *schema;
1150
1151           if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1152             schema_state_add_child (state->schema_state,
1153                                     name, schema, error);
1154           return;
1155         }
1156       else if (strcmp (element_name, "override") == 0)
1157         {
1158           const gchar *name, *l10n, *context;
1159
1160           if (COLLECT (STRING,            "name",    &name,
1161                        OPTIONAL | STRING, "l10n",    &l10n,
1162                        OPTIONAL | STRING, "context", &context))
1163             schema_state_add_override (state->schema_state,
1164                                        &state->key_state, &state->string,
1165                                        name, l10n, context, error);
1166           return;
1167         }
1168     }
1169
1170   /* children of <key> {{{3 */
1171   else if (strcmp (container, "key") == 0)
1172     {
1173       if (strcmp (element_name, "default") == 0)
1174         {
1175           const gchar *l10n, *context;
1176           if (COLLECT (STRING | OPTIONAL, "l10n",    &l10n,
1177                        STRING | OPTIONAL, "context", &context))
1178             state->string = key_state_start_default (state->key_state,
1179                                                      l10n, context, error);
1180           return;
1181         }
1182
1183       else if (strcmp (element_name, "summary") == 0 ||
1184                strcmp (element_name, "description") == 0)
1185         {
1186           if (NO_ATTRS ())
1187             state->string = g_string_new (NULL);
1188           return;
1189         }
1190
1191       else if (strcmp (element_name, "range") == 0)
1192         {
1193           const gchar *min, *max;
1194           if (COLLECT (STRING, "min", &min, STRING, "max", &max))
1195             key_state_set_range (state->key_state, min, max, error);
1196           return;
1197         }
1198
1199       else if (strcmp (element_name, "choices") == 0)
1200         {
1201           if (NO_ATTRS ())
1202             key_state_start_choices (state->key_state, error);
1203           return;
1204         }
1205
1206       else if (strcmp (element_name, "aliases") == 0)
1207         {
1208           if (NO_ATTRS ())
1209             key_state_start_aliases (state->key_state, error);
1210           return;
1211         }
1212     }
1213
1214
1215   /* children of <choices> {{{3 */
1216   else if (strcmp (container, "choices") == 0)
1217     {
1218       if (strcmp (element_name, "choice") == 0)
1219         {
1220           const gchar *value;
1221           if (COLLECT (STRING, "value", &value))
1222             key_state_add_choice (state->key_state, value, error);
1223           return;
1224         }
1225     }
1226
1227
1228   /* children of <aliases> {{{3 */
1229   else if (strcmp (container, "aliases") == 0)
1230     {
1231       if (strcmp (element_name, "alias") == 0)
1232         {
1233           const gchar *value, *target;
1234           if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1235             key_state_add_alias (state->key_state, value, target, error);
1236           return;
1237         }
1238     }
1239
1240
1241   /* children of <enum> {{{3 */
1242   else if (strcmp (container, "enum") == 0)
1243     {
1244       if (strcmp (element_name, "value") == 0)
1245         {
1246           const gchar *nick, *valuestr;
1247           if (COLLECT (STRING, "nick", &nick,
1248                        STRING, "value", &valuestr))
1249             enum_state_add_value (state->enum_state, nick, valuestr, error);
1250           return;
1251         }
1252     }
1253   /* 3}}} */
1254
1255   if (container)
1256     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1257                  "Element <%s> not allowed inside <%s>",
1258                  element_name, container);
1259   else
1260     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1261                  "Element <%s> not allowed at toplevel", element_name);
1262 }
1263 /* 2}}} */
1264 /* End element {{{2 */
1265
1266 static void
1267 key_state_end (KeyState **state_ptr,
1268                GError   **error)
1269 {
1270   KeyState *state;
1271
1272   state = *state_ptr;
1273   *state_ptr = NULL;
1274
1275   if (state->default_value == NULL)
1276     {
1277       g_set_error_literal (error,
1278                            G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1279                            "element <default> is required in <key>");
1280       return;
1281     }
1282 }
1283
1284 static void
1285 schema_state_end (SchemaState **state_ptr,
1286                   GError      **error)
1287 {
1288   SchemaState *state;
1289
1290   state = *state_ptr;
1291   *state_ptr = NULL;
1292 }
1293
1294 static void
1295 end_element (GMarkupParseContext  *context,
1296              const gchar          *element_name,
1297              gpointer              user_data,
1298              GError              **error)
1299 {
1300   ParseState *state = user_data;
1301
1302   if (strcmp (element_name, "schemalist") == 0)
1303     {
1304       g_free (state->schemalist_domain);
1305       state->schemalist_domain = NULL;
1306     }
1307
1308   else if (strcmp (element_name, "enum") == 0)
1309     enum_state_end (&state->enum_state, error);
1310
1311   else if (strcmp (element_name, "schema") == 0)
1312     schema_state_end (&state->schema_state, error);
1313
1314   else if (strcmp (element_name, "override") == 0)
1315     override_state_end (&state->key_state, &state->string, error);
1316
1317   else if (strcmp (element_name, "key") == 0)
1318     key_state_end (&state->key_state, error);
1319
1320   else if (strcmp (element_name, "default") == 0)
1321     key_state_end_default (state->key_state, &state->string, error);
1322
1323   else if (strcmp (element_name, "choices") == 0)
1324     key_state_end_choices (state->key_state, error);
1325
1326   else if (strcmp (element_name, "aliases") == 0)
1327     key_state_end_aliases (state->key_state, error);
1328
1329   if (state->string)
1330     {
1331       g_string_free (state->string, TRUE);
1332       state->string = NULL;
1333     }
1334 }
1335 /* Text {{{2 */
1336 static void
1337 text (GMarkupParseContext  *context,
1338       const gchar          *text,
1339       gsize                 text_len,
1340       gpointer              user_data,
1341       GError              **error)
1342 {
1343   ParseState *state = user_data;
1344   gsize i;
1345
1346   for (i = 0; i < text_len; i++)
1347     if (!g_ascii_isspace (text[i]))
1348       {
1349         if (state->string)
1350           g_string_append_len (state->string, text, text_len);
1351
1352         else
1353           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1354                        "text may not appear inside <%s>",
1355                        g_markup_parse_context_get_element (context));
1356
1357         break;
1358       }
1359 }
1360
1361 /* Write to GVDB {{{1 */
1362 typedef struct
1363 {
1364   GHashTable *table;
1365   GvdbItem *root;
1366 } GvdbPair;
1367
1368 static void
1369 gvdb_pair_init (GvdbPair *pair)
1370 {
1371   pair->table = gvdb_hash_table_new (NULL, NULL);
1372   pair->root = gvdb_hash_table_insert (pair->table, "");
1373 }
1374
1375 typedef struct
1376 {
1377   GvdbPair pair;
1378   gboolean l10n;
1379 } OutputSchemaData;
1380
1381 static void
1382 output_key (gpointer key,
1383             gpointer value,
1384             gpointer user_data)
1385 {
1386   OutputSchemaData *data;
1387   const gchar *name;
1388   KeyState *state;
1389   GvdbItem *item;
1390
1391   name = key;
1392   state = value;
1393   data = user_data;
1394
1395   item = gvdb_hash_table_insert (data->pair.table, name);
1396   gvdb_item_set_parent (item, data->pair.root);
1397   gvdb_item_set_value (item, key_state_serialise (state));
1398
1399   if (state->l10n)
1400     data->l10n = TRUE;
1401 }
1402
1403 static void
1404 output_schema (gpointer key,
1405                gpointer value,
1406                gpointer user_data)
1407 {
1408   OutputSchemaData data;
1409   GvdbPair *root_pair;
1410   SchemaState *state;
1411   const gchar *id;
1412   GvdbItem *item;
1413
1414   id = key;
1415   state = value;
1416   root_pair = user_data;
1417
1418   gvdb_pair_init (&data.pair);
1419   data.l10n = FALSE;
1420
1421   item = gvdb_hash_table_insert (root_pair->table, id);
1422   gvdb_item_set_parent (item, root_pair->root);
1423   gvdb_item_set_hash_table (item, data.pair.table);
1424
1425   g_hash_table_foreach (state->keys, output_key, &data);
1426
1427   if (state->path)
1428     gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1429
1430   if (state->extends_name)
1431     gvdb_hash_table_insert_string (data.pair.table, ".extends",
1432                                    state->extends_name);
1433
1434   if (state->list_of)
1435     gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1436                                    state->extends_name);
1437
1438   if (data.l10n)
1439     gvdb_hash_table_insert_string (data.pair.table,
1440                                    ".gettext-domain",
1441                                    state->gettext_domain);
1442 }
1443
1444 static gboolean
1445 write_to_file (GHashTable   *schema_table,
1446                const gchar  *filename,
1447                GError      **error)
1448 {
1449   gboolean success;
1450   GvdbPair pair;
1451
1452   gvdb_pair_init (&pair);
1453
1454   g_hash_table_foreach (schema_table, output_schema, &pair);
1455
1456   success = gvdb_table_write_contents (pair.table, filename,
1457                                        G_BYTE_ORDER != G_LITTLE_ENDIAN,
1458                                        error);
1459   g_hash_table_unref (pair.table);
1460
1461   return success;
1462 }
1463
1464 /* Parser driver {{{1 */
1465 static GHashTable *
1466 parse_gschema_files (gchar  **files,
1467                      GError **error)
1468 {
1469   GMarkupParser parser = { start_element, end_element, text };
1470   GMarkupParseContext *context;
1471   ParseState state = { 0, };
1472   const gchar *filename;
1473
1474   context = g_markup_parse_context_new (&parser,
1475                                         G_MARKUP_PREFIX_ERROR_POSITION,
1476                                         &state, NULL);
1477
1478   state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1479                                             g_free, enum_state_free);
1480
1481   state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1482                                               g_free, schema_state_free);
1483
1484   while ((filename = *files++) != NULL)
1485     {
1486       gchar *contents;
1487       gsize size;
1488
1489       if (!g_file_get_contents (filename, &contents, &size, error))
1490         return FALSE;
1491
1492       if (!g_markup_parse_context_parse (context, contents, size, error))
1493         {
1494           g_prefix_error (error, "%s: ", filename);
1495           return FALSE;
1496         }
1497
1498       if (!g_markup_parse_context_end_parse (context, error))
1499         {
1500           g_prefix_error (error, "%s: ", filename);
1501           return FALSE;
1502         }
1503     }
1504
1505   g_hash_table_unref (state.enum_table);
1506
1507   return state.schema_table;
1508 }
1509
1510 static gint
1511 compare_strings (gconstpointer a,
1512                  gconstpointer b)
1513 {
1514   gchar *one = *(gchar **) a;
1515   gchar *two = *(gchar **) b;
1516
1517   return strcmp (one, two);
1518 }
1519
1520 int
1521 main (int argc, char **argv)
1522 {
1523   GError *error;
1524   GHashTable *table;
1525   GDir *dir;
1526   const gchar *file;
1527   gchar *srcdir;
1528   gchar *targetdir = NULL;
1529   gchar *target;
1530   gboolean uninstall = FALSE;
1531   gboolean dry_run = FALSE;
1532   gchar **schema_files = NULL;
1533   GOptionContext *context;
1534   GOptionEntry entries[] = {
1535     { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1536     { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1537     { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1538     { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1539
1540     /* These options are only for use in the gschema-compile tests */
1541     { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1542     { NULL }
1543   };
1544
1545   setlocale (LC_ALL, "");
1546
1547   context = g_option_context_new (N_("DIRECTORY"));
1548   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1549   g_option_context_set_summary (context,
1550     N_("Compile all GSettings schema files into a schema cache.\n"
1551        "Schema files are required to have the extension .gschema.xml,\n"
1552        "and the cache file is called gschemas.compiled."));
1553   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1554
1555   error = NULL;
1556   if (!g_option_context_parse (context, &argc, &argv, &error))
1557     {
1558       fprintf (stderr, "%s", error->message);
1559       return 1;
1560     }
1561
1562   g_option_context_free (context);
1563
1564   if (!schema_files && argc != 2)
1565     {
1566       fprintf (stderr, _("You should give exactly one directory name\n"));
1567       return 1;
1568     }
1569
1570   srcdir = argv[1];
1571
1572   if (targetdir == NULL)
1573     targetdir = srcdir;
1574
1575   target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1576
1577   if (!schema_files)
1578     {
1579       GPtrArray *files;
1580
1581       files = g_ptr_array_new ();
1582
1583       dir = g_dir_open (srcdir, 0, &error);
1584       if (dir == NULL)
1585         {
1586           fprintf (stderr, "%s\n", error->message);
1587           return 1;
1588         }
1589
1590       while ((file = g_dir_read_name (dir)) != NULL)
1591         {
1592           if (g_str_has_suffix (file, ".gschema.xml") ||
1593               g_str_has_suffix (file, ".enums.xml"))
1594             g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
1595         }
1596
1597       if (files->len == 0)
1598         {
1599           fprintf (stderr, _("No schema files found: "));
1600
1601           if (g_unlink (target))
1602             fprintf (stderr, _("doing nothing.\n"));
1603
1604           else
1605             fprintf (stderr, _("removed existing output file.\n"));
1606
1607           return 0;
1608         }
1609       g_ptr_array_sort (files, compare_strings);
1610       g_ptr_array_add (files, NULL);
1611
1612       schema_files = (char **) g_ptr_array_free (files, FALSE);
1613     }
1614
1615
1616   if (!(table = parse_gschema_files (schema_files, &error)) ||
1617       (!dry_run && !write_to_file (table, target, &error)))
1618     {
1619       fprintf (stderr, "%s\n", error->message);
1620       return 1;
1621     }
1622
1623   g_free (target);
1624
1625   return 0;
1626 }
1627
1628 /* Epilogue {{{1 */
1629
1630 /* vim:set foldmethod=marker: */