GSettings: accept <flags> as an alias for <enum>
[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                strcmp (element_name, "flags") == 0)
1122         {
1123           const gchar *id;
1124           if (COLLECT (STRING, "id", &id))
1125             parse_state_start_enum (state, id, error);
1126           return;
1127         }
1128     }
1129
1130
1131   /* children of <schema> {{{3 */
1132   else if (strcmp (container, "schema") == 0)
1133     {
1134       if (strcmp (element_name, "key") == 0)
1135         {
1136           const gchar *name, *type_string, *enum_type;
1137
1138           if (COLLECT (STRING,            "name", &name,
1139                        OPTIONAL | STRING, "type", &type_string,
1140                        OPTIONAL | STRING, "enum", &enum_type))
1141
1142             state->key_state = schema_state_add_key (state->schema_state,
1143                                                      state->enum_table,
1144                                                      name, type_string,
1145                                                      enum_type, error);
1146           return;
1147         }
1148       else if (strcmp (element_name, "child") == 0)
1149         {
1150           const gchar *name, *schema;
1151
1152           if (COLLECT (STRING, "name", &name, STRING, "schema", &schema))
1153             schema_state_add_child (state->schema_state,
1154                                     name, schema, error);
1155           return;
1156         }
1157       else if (strcmp (element_name, "override") == 0)
1158         {
1159           const gchar *name, *l10n, *context;
1160
1161           if (COLLECT (STRING,            "name",    &name,
1162                        OPTIONAL | STRING, "l10n",    &l10n,
1163                        OPTIONAL | STRING, "context", &context))
1164             schema_state_add_override (state->schema_state,
1165                                        &state->key_state, &state->string,
1166                                        name, l10n, context, error);
1167           return;
1168         }
1169     }
1170
1171   /* children of <key> {{{3 */
1172   else if (strcmp (container, "key") == 0)
1173     {
1174       if (strcmp (element_name, "default") == 0)
1175         {
1176           const gchar *l10n, *context;
1177           if (COLLECT (STRING | OPTIONAL, "l10n",    &l10n,
1178                        STRING | OPTIONAL, "context", &context))
1179             state->string = key_state_start_default (state->key_state,
1180                                                      l10n, context, error);
1181           return;
1182         }
1183
1184       else if (strcmp (element_name, "summary") == 0 ||
1185                strcmp (element_name, "description") == 0)
1186         {
1187           if (NO_ATTRS ())
1188             state->string = g_string_new (NULL);
1189           return;
1190         }
1191
1192       else if (strcmp (element_name, "range") == 0)
1193         {
1194           const gchar *min, *max;
1195           if (COLLECT (STRING, "min", &min, STRING, "max", &max))
1196             key_state_set_range (state->key_state, min, max, error);
1197           return;
1198         }
1199
1200       else if (strcmp (element_name, "choices") == 0)
1201         {
1202           if (NO_ATTRS ())
1203             key_state_start_choices (state->key_state, error);
1204           return;
1205         }
1206
1207       else if (strcmp (element_name, "aliases") == 0)
1208         {
1209           if (NO_ATTRS ())
1210             key_state_start_aliases (state->key_state, error);
1211           return;
1212         }
1213     }
1214
1215
1216   /* children of <choices> {{{3 */
1217   else if (strcmp (container, "choices") == 0)
1218     {
1219       if (strcmp (element_name, "choice") == 0)
1220         {
1221           const gchar *value;
1222           if (COLLECT (STRING, "value", &value))
1223             key_state_add_choice (state->key_state, value, error);
1224           return;
1225         }
1226     }
1227
1228
1229   /* children of <aliases> {{{3 */
1230   else if (strcmp (container, "aliases") == 0)
1231     {
1232       if (strcmp (element_name, "alias") == 0)
1233         {
1234           const gchar *value, *target;
1235           if (COLLECT (STRING, "value", &value, STRING, "target", &target))
1236             key_state_add_alias (state->key_state, value, target, error);
1237           return;
1238         }
1239     }
1240
1241
1242   /* children of <enum> {{{3 */
1243   else if (strcmp (container, "enum") == 0 ||
1244            strcmp (container, "flags") == 0)
1245     {
1246       if (strcmp (element_name, "value") == 0)
1247         {
1248           const gchar *nick, *valuestr;
1249           if (COLLECT (STRING, "nick", &nick,
1250                        STRING, "value", &valuestr))
1251             enum_state_add_value (state->enum_state, nick, valuestr, error);
1252           return;
1253         }
1254     }
1255   /* 3}}} */
1256
1257   if (container)
1258     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1259                  "Element <%s> not allowed inside <%s>",
1260                  element_name, container);
1261   else
1262     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1263                  "Element <%s> not allowed at toplevel", element_name);
1264 }
1265 /* 2}}} */
1266 /* End element {{{2 */
1267
1268 static void
1269 key_state_end (KeyState **state_ptr,
1270                GError   **error)
1271 {
1272   KeyState *state;
1273
1274   state = *state_ptr;
1275   *state_ptr = NULL;
1276
1277   if (state->default_value == NULL)
1278     {
1279       g_set_error_literal (error,
1280                            G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1281                            "element <default> is required in <key>");
1282       return;
1283     }
1284 }
1285
1286 static void
1287 schema_state_end (SchemaState **state_ptr,
1288                   GError      **error)
1289 {
1290   SchemaState *state;
1291
1292   state = *state_ptr;
1293   *state_ptr = NULL;
1294 }
1295
1296 static void
1297 end_element (GMarkupParseContext  *context,
1298              const gchar          *element_name,
1299              gpointer              user_data,
1300              GError              **error)
1301 {
1302   ParseState *state = user_data;
1303
1304   if (strcmp (element_name, "schemalist") == 0)
1305     {
1306       g_free (state->schemalist_domain);
1307       state->schemalist_domain = NULL;
1308     }
1309
1310   else if (strcmp (element_name, "enum") == 0 ||
1311            strcmp (element_name, "flags") == 0)
1312     enum_state_end (&state->enum_state, error);
1313
1314   else if (strcmp (element_name, "schema") == 0)
1315     schema_state_end (&state->schema_state, error);
1316
1317   else if (strcmp (element_name, "override") == 0)
1318     override_state_end (&state->key_state, &state->string, error);
1319
1320   else if (strcmp (element_name, "key") == 0)
1321     key_state_end (&state->key_state, error);
1322
1323   else if (strcmp (element_name, "default") == 0)
1324     key_state_end_default (state->key_state, &state->string, error);
1325
1326   else if (strcmp (element_name, "choices") == 0)
1327     key_state_end_choices (state->key_state, error);
1328
1329   else if (strcmp (element_name, "aliases") == 0)
1330     key_state_end_aliases (state->key_state, error);
1331
1332   if (state->string)
1333     {
1334       g_string_free (state->string, TRUE);
1335       state->string = NULL;
1336     }
1337 }
1338 /* Text {{{2 */
1339 static void
1340 text (GMarkupParseContext  *context,
1341       const gchar          *text,
1342       gsize                 text_len,
1343       gpointer              user_data,
1344       GError              **error)
1345 {
1346   ParseState *state = user_data;
1347   gsize i;
1348
1349   for (i = 0; i < text_len; i++)
1350     if (!g_ascii_isspace (text[i]))
1351       {
1352         if (state->string)
1353           g_string_append_len (state->string, text, text_len);
1354
1355         else
1356           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
1357                        "text may not appear inside <%s>",
1358                        g_markup_parse_context_get_element (context));
1359
1360         break;
1361       }
1362 }
1363
1364 /* Write to GVDB {{{1 */
1365 typedef struct
1366 {
1367   GHashTable *table;
1368   GvdbItem *root;
1369 } GvdbPair;
1370
1371 static void
1372 gvdb_pair_init (GvdbPair *pair)
1373 {
1374   pair->table = gvdb_hash_table_new (NULL, NULL);
1375   pair->root = gvdb_hash_table_insert (pair->table, "");
1376 }
1377
1378 typedef struct
1379 {
1380   GvdbPair pair;
1381   gboolean l10n;
1382 } OutputSchemaData;
1383
1384 static void
1385 output_key (gpointer key,
1386             gpointer value,
1387             gpointer user_data)
1388 {
1389   OutputSchemaData *data;
1390   const gchar *name;
1391   KeyState *state;
1392   GvdbItem *item;
1393
1394   name = key;
1395   state = value;
1396   data = user_data;
1397
1398   item = gvdb_hash_table_insert (data->pair.table, name);
1399   gvdb_item_set_parent (item, data->pair.root);
1400   gvdb_item_set_value (item, key_state_serialise (state));
1401
1402   if (state->l10n)
1403     data->l10n = TRUE;
1404 }
1405
1406 static void
1407 output_schema (gpointer key,
1408                gpointer value,
1409                gpointer user_data)
1410 {
1411   OutputSchemaData data;
1412   GvdbPair *root_pair;
1413   SchemaState *state;
1414   const gchar *id;
1415   GvdbItem *item;
1416
1417   id = key;
1418   state = value;
1419   root_pair = user_data;
1420
1421   gvdb_pair_init (&data.pair);
1422   data.l10n = FALSE;
1423
1424   item = gvdb_hash_table_insert (root_pair->table, id);
1425   gvdb_item_set_parent (item, root_pair->root);
1426   gvdb_item_set_hash_table (item, data.pair.table);
1427
1428   g_hash_table_foreach (state->keys, output_key, &data);
1429
1430   if (state->path)
1431     gvdb_hash_table_insert_string (data.pair.table, ".path", state->path);
1432
1433   if (state->extends_name)
1434     gvdb_hash_table_insert_string (data.pair.table, ".extends",
1435                                    state->extends_name);
1436
1437   if (state->list_of)
1438     gvdb_hash_table_insert_string (data.pair.table, ".list-of",
1439                                    state->extends_name);
1440
1441   if (data.l10n)
1442     gvdb_hash_table_insert_string (data.pair.table,
1443                                    ".gettext-domain",
1444                                    state->gettext_domain);
1445 }
1446
1447 static gboolean
1448 write_to_file (GHashTable   *schema_table,
1449                const gchar  *filename,
1450                GError      **error)
1451 {
1452   gboolean success;
1453   GvdbPair pair;
1454
1455   gvdb_pair_init (&pair);
1456
1457   g_hash_table_foreach (schema_table, output_schema, &pair);
1458
1459   success = gvdb_table_write_contents (pair.table, filename,
1460                                        G_BYTE_ORDER != G_LITTLE_ENDIAN,
1461                                        error);
1462   g_hash_table_unref (pair.table);
1463
1464   return success;
1465 }
1466
1467 /* Parser driver {{{1 */
1468 static GHashTable *
1469 parse_gschema_files (gchar  **files,
1470                      GError **error)
1471 {
1472   GMarkupParser parser = { start_element, end_element, text };
1473   GMarkupParseContext *context;
1474   ParseState state = { 0, };
1475   const gchar *filename;
1476
1477   context = g_markup_parse_context_new (&parser,
1478                                         G_MARKUP_PREFIX_ERROR_POSITION,
1479                                         &state, NULL);
1480
1481   state.enum_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1482                                             g_free, enum_state_free);
1483
1484   state.schema_table = g_hash_table_new_full (g_str_hash, g_str_equal,
1485                                               g_free, schema_state_free);
1486
1487   while ((filename = *files++) != NULL)
1488     {
1489       gchar *contents;
1490       gsize size;
1491
1492       if (!g_file_get_contents (filename, &contents, &size, error))
1493         return FALSE;
1494
1495       if (!g_markup_parse_context_parse (context, contents, size, error))
1496         {
1497           g_prefix_error (error, "%s: ", filename);
1498           return FALSE;
1499         }
1500
1501       if (!g_markup_parse_context_end_parse (context, error))
1502         {
1503           g_prefix_error (error, "%s: ", filename);
1504           return FALSE;
1505         }
1506     }
1507
1508   g_hash_table_unref (state.enum_table);
1509
1510   return state.schema_table;
1511 }
1512
1513 static gint
1514 compare_strings (gconstpointer a,
1515                  gconstpointer b)
1516 {
1517   gchar *one = *(gchar **) a;
1518   gchar *two = *(gchar **) b;
1519
1520   return strcmp (one, two);
1521 }
1522
1523 int
1524 main (int argc, char **argv)
1525 {
1526   GError *error;
1527   GHashTable *table;
1528   GDir *dir;
1529   const gchar *file;
1530   gchar *srcdir;
1531   gchar *targetdir = NULL;
1532   gchar *target;
1533   gboolean uninstall = FALSE;
1534   gboolean dry_run = FALSE;
1535   gchar **schema_files = NULL;
1536   GOptionContext *context;
1537   GOptionEntry entries[] = {
1538     { "targetdir", 0, 0, G_OPTION_ARG_FILENAME, &targetdir, N_("where to store the gschemas.compiled file"), N_("DIRECTORY") },
1539     { "dry-run", 0, 0, G_OPTION_ARG_NONE, &dry_run, N_("Do not write the gschema.compiled file"), NULL },
1540     { "uninstall", 0, 0, G_OPTION_ARG_NONE, &uninstall, N_("This option will be removed soon.") },
1541     { "allow-any-name", 0, 0, G_OPTION_ARG_NONE, &allow_any_name, N_("Do not enforce key name restrictions") },
1542
1543     /* These options are only for use in the gschema-compile tests */
1544     { "schema-file", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME_ARRAY, &schema_files, NULL, NULL },
1545     { NULL }
1546   };
1547
1548   setlocale (LC_ALL, "");
1549
1550   context = g_option_context_new (N_("DIRECTORY"));
1551   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
1552   g_option_context_set_summary (context,
1553     N_("Compile all GSettings schema files into a schema cache.\n"
1554        "Schema files are required to have the extension .gschema.xml,\n"
1555        "and the cache file is called gschemas.compiled."));
1556   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1557
1558   error = NULL;
1559   if (!g_option_context_parse (context, &argc, &argv, &error))
1560     {
1561       fprintf (stderr, "%s", error->message);
1562       return 1;
1563     }
1564
1565   g_option_context_free (context);
1566
1567   if (!schema_files && argc != 2)
1568     {
1569       fprintf (stderr, _("You should give exactly one directory name\n"));
1570       return 1;
1571     }
1572
1573   srcdir = argv[1];
1574
1575   if (targetdir == NULL)
1576     targetdir = srcdir;
1577
1578   target = g_build_filename (targetdir, "gschemas.compiled", NULL);
1579
1580   if (!schema_files)
1581     {
1582       GPtrArray *files;
1583
1584       files = g_ptr_array_new ();
1585
1586       dir = g_dir_open (srcdir, 0, &error);
1587       if (dir == NULL)
1588         {
1589           fprintf (stderr, "%s\n", error->message);
1590           return 1;
1591         }
1592
1593       while ((file = g_dir_read_name (dir)) != NULL)
1594         {
1595           if (g_str_has_suffix (file, ".gschema.xml") ||
1596               g_str_has_suffix (file, ".enums.xml"))
1597             g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
1598         }
1599
1600       if (files->len == 0)
1601         {
1602           fprintf (stderr, _("No schema files found: "));
1603
1604           if (g_unlink (target))
1605             fprintf (stderr, _("doing nothing.\n"));
1606
1607           else
1608             fprintf (stderr, _("removed existing output file.\n"));
1609
1610           return 0;
1611         }
1612       g_ptr_array_sort (files, compare_strings);
1613       g_ptr_array_add (files, NULL);
1614
1615       schema_files = (char **) g_ptr_array_free (files, FALSE);
1616     }
1617
1618
1619   if (!(table = parse_gschema_files (schema_files, &error)) ||
1620       (!dry_run && !write_to_file (table, target, &error)))
1621     {
1622       fprintf (stderr, "%s\n", error->message);
1623       return 1;
1624     }
1625
1626   g_free (target);
1627
1628   return 0;
1629 }
1630
1631 /* Epilogue {{{1 */
1632
1633 /* vim:set foldmethod=marker: */