summaryrefslogtreecommitdiff
path: root/rebuild.c (plain)
blob: 7b605bdb3208e020600fe92cc0248aef988a403b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* -*- indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Parse defaults out of schema files and store in database
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include <stdarg.h>
#include <string.h>
#include <stdarg.h>

#include "gconf-defaults-hack.h"

typedef struct _SchemaParseState SchemaParseState;

typedef enum {
  SCHEMA_LEAF_NONE,
  SCHEMA_LEAF_APPLYTO,
  SCHEMA_LEAF_DEFAULT,
  SCHEMA_LEAF_CAR_TYPE,
  SCHEMA_LEAF_CDR_TYPE,
  SCHEMA_LEAF_LIST_TYPE,
  SCHEMA_LEAF_TYPE
} SchemaLeafElement;

struct _SchemaParseState
{
  GConfDefaultsHackDatabase *database;
  const char *filename;
  gboolean in_schema;
  SchemaLeafElement leaf_element;
  char *applyto;
  GConfDefaultsHackEntry entry;
  GString *text;
};

static void
schema_parser_start_element (GMarkupParseContext *context,
                             const gchar         *element_name,
                             const gchar        **attribute_names,
                             const gchar        **attribute_values,
                             gpointer             user_data,
                             GError             **error)
{
  SchemaParseState *state = user_data;

  if (state->leaf_element != SCHEMA_LEAF_NONE)
    {
      g_set_error (error,
                   G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
                   "contents inside a what should be a leaf element");
      return;
    }

  if (state->in_schema)
    {
      switch (element_name[0])
        {
        case 'a':
          if (strcmp (element_name, "applyto") == 0 && state->applyto == NULL)
            state->leaf_element = SCHEMA_LEAF_APPLYTO;
          break;
        case 'c':
          if (strcmp (element_name, "car_type") == 0 && state->entry.car_type == NULL)
            state->leaf_element = SCHEMA_LEAF_CAR_TYPE;
          else if (strcmp (element_name, "cdr_type") == 0 && state->entry.cdr_type == NULL)
            state->leaf_element = SCHEMA_LEAF_CDR_TYPE;
          break;
        case 'd':
          if (strcmp (element_name, "default") == 0 && state->entry.default_value == NULL)
            state->leaf_element = SCHEMA_LEAF_DEFAULT;
          break;
        case 'l':
          if (strcmp (element_name, "list_type") == 0 && state->entry.list_type == NULL)
            state->leaf_element = SCHEMA_LEAF_LIST_TYPE;
          break;
        case 's':
          if (strcmp (element_name, "schema") == 0)
            {
              g_set_error (error,
                           G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
                           "nested <schema/> elements");
              return;
            }
          break;
        case 't':
          if (strcmp (element_name, "type") == 0 && state->entry.type == NULL)
            state->leaf_element = SCHEMA_LEAF_TYPE;
          break;
        }
    }
  else
    {
      if (strcmp (element_name, "schema") == 0)
        state->in_schema = TRUE;
    }
}

static char *
steal_text (SchemaParseState *state)
{
  if (state->text)
    {
      char *result = g_string_free (state->text, FALSE);
      result = g_strstrip (result);
      state->text = NULL;

      return result;
    }
  else
    return NULL;
}

static void skip (GMarkupParseContext *context,
                  SchemaParseState *state,
                  const char       *format,
                  ...) G_GNUC_PRINTF (3, 4);

static void
skip (GMarkupParseContext *context,
      SchemaParseState    *state,
      const char          *format,
      ...)
{
  if (gconf_defaults_hack_verbose ())
    {
      char *error_str;
      va_list vap;
      int line_number;

      va_start (vap, format);
      error_str = g_strdup_vprintf (format, vap);
      va_end (vap);

      g_markup_parse_context_get_position (context, &line_number, NULL);

      if (state->applyto)
        g_printerr ("%s:%d (%s): %s\n",
                    state->filename, line_number, state->applyto, error_str);
      else
        g_printerr ("%s:%d: %s\n",
                    state->filename, line_number, error_str);

      g_free (error_str);
    }
}

static void
output (GMarkupParseContext *context,
        SchemaParseState    *state)
{
  gconf_defaults_hack_database_put (state->database,
                                    state->applyto,
                                    &state->entry);
}

static gboolean
validate_element_type (const char *str)
{
  GConfValueType type = gconf_defaults_hack_value_type_from_string (str);
  if (type == GCONF_VALUE_INVALID ||
      type == GCONF_VALUE_LIST ||
      type == GCONF_VALUE_PAIR)
    return FALSE;

  return TRUE;
}

static void
validate_and_output (GMarkupParseContext *context,
                     SchemaParseState    *state)
{
  if (state->applyto == NULL)
    skip (context, state, "No applyto");
  else if (state->entry.default_value == NULL)
    skip (context, state, "No default");
  else if (state->entry.type == NULL)
    skip (context, state, "No type");
  else
    {
      GConfValueType type = gconf_defaults_hack_value_type_from_string (state->entry.type);
      if (type == GCONF_VALUE_INVALID)
        {
          skip (context, state, "bad type '%s'", state->entry.type);
        }
      else if (type == GCONF_VALUE_LIST)
        {
          if (state->entry.list_type == NULL)
            skip (context, state, "list without list_type");
          else if (!validate_element_type (state->entry.list_type))
            skip (context, state, "bad list_type '%s'", state->entry.type);
          else
            output (context, state);
        }
      else if (type == GCONF_VALUE_PAIR)
        {
          if (state->entry.car_type == NULL)
            skip (context, state, "pair without car_type");
          else if (state->entry.cdr_type == NULL)
            skip (context, state, "pair without cdr_type");
          else if (!validate_element_type (state->entry.car_type))
            skip (context, state, "bad car_type '%s'", state->entry.car_type);
          else if (!validate_element_type (state->entry.cdr_type))
            skip (context, state, "bad cdr_type '%s'", state->entry.cdr_type);
          else
            output (context, state);
        }
      else
        {
            output (context, state);
        }
    }
}

static void
schema_parser_end_element (GMarkupParseContext *context,
                           const gchar         *element_name,
                           gpointer             user_data,
                           GError             **error)
{
  SchemaParseState *state = user_data;

  if (state->leaf_element != SCHEMA_LEAF_NONE)
    {
      switch (state->leaf_element)
        {
        case SCHEMA_LEAF_APPLYTO:
          state->applyto = steal_text (state);
          break;
        case SCHEMA_LEAF_CAR_TYPE:
          state->entry.car_type = steal_text (state);
          break;
        case SCHEMA_LEAF_CDR_TYPE:
          state->entry.cdr_type = steal_text (state);
          break;
        case SCHEMA_LEAF_DEFAULT:
          state->entry.default_value = steal_text (state);
          break;
        case SCHEMA_LEAF_LIST_TYPE:
          state->entry.list_type = steal_text (state);
          break;
        case SCHEMA_LEAF_TYPE:
          state->entry.type = steal_text (state);
          break;
        case SCHEMA_LEAF_NONE:
          g_assert_not_reached ();
          break;
        }

      state->leaf_element = SCHEMA_LEAF_NONE;
    }
  else if (state->in_schema)
    {
      if (strcmp (element_name, "schema") == 0)
        {
          validate_and_output (context, state);

          g_free (state->applyto);
          state->applyto = NULL;
          g_free (state->entry.car_type);
          state->entry.car_type = NULL;
          g_free (state->entry.cdr_type);
          state->entry.cdr_type = NULL;
          g_free (state->entry.default_value);
          state->entry.default_value = NULL;
          g_free (state->entry.list_type);
          state->entry.list_type = NULL;
          g_free (state->entry.type);
          state->entry.type = NULL;

          state->in_schema = FALSE;
        }
    }
}

static void
schema_parser_text (GMarkupParseContext *context,
                    const gchar         *text,
                    gsize                text_len,
                    gpointer             user_data,
                    GError             **error)
{
  SchemaParseState *state = user_data;

  if (state->leaf_element != SCHEMA_LEAF_NONE)
    {
      if (!state->text)
        state->text = g_string_new (NULL);

      g_string_append_len (state->text, text, text_len);
    }
}

static GMarkupParser schema_parser = {
  schema_parser_start_element,
  schema_parser_end_element,
  schema_parser_text,
  NULL, /* passthrough */
  NULL /* error */
};

static void
parse_schema_file (GConfDefaultsHackDatabase *database,
                   const char                *filename)
{
  GMarkupParseContext *context = NULL;
  SchemaParseState state = { 0 };
  char *contents = NULL;
  gsize len;
  GError *error = NULL;

  state.database = database;
  state.filename = filename;

  if (!g_file_get_contents (filename, &contents, &len, &error))
    goto out;

  context = g_markup_parse_context_new (&schema_parser, 0,
                                        &state, NULL);

  if (!g_markup_parse_context_parse (context, contents, len, &error))
    goto out;

  if (!g_markup_parse_context_end_parse (context, &error))
    goto out;

 out:
  if (state.text)
    g_string_free (state.text, TRUE);

  if (contents != NULL)
   g_free (contents);

  if (context != NULL)
    g_markup_parse_context_free (context);

  if (error != NULL)
    {
      g_printerr ("Cannot parse schema file %s: %s\n", filename, error->message);
      g_error_free (error);
    }
}

void
gconf_defaults_hack_rebuild_database (GConfDefaultsHackDatabase *database,
                                      const char                *schemas_dir)
{
  GDir *dir = NULL;
  GError *error = NULL;

  dir = g_dir_open (schemas_dir, 0, &error);
  if (!dir)
    {
      g_printerr ("Cannot read schema directory %s: %s\n", schemas_dir, error->message);
      g_error_free (error);

      goto out;
    }

  gconf_defaults_hack_database_clear (database);
  gconf_defaults_hack_database_begin_transaction (database);

  while (TRUE)
    {
      char *filename;
      const char *name = g_dir_read_name (dir);
      if (!name)
        break;

      if (!g_str_has_suffix (name, ".schemas"))
        continue;

      filename = g_build_filename (schemas_dir, name, NULL);
      parse_schema_file (database, filename);
      g_free (filename);
    }

  gconf_defaults_hack_database_commit (database);

 out:
  if (dir)
    g_dir_close (dir);
}