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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#ifdef AS_BUILD_DEP11
#include <yaml.h>
#endif
#include "as-node.h"
#include "as-ref-string.h"
#include "as-yaml.h"
typedef enum {
AS_YAML_NODE_KIND_UNKNOWN,
AS_YAML_NODE_KIND_MAP,
AS_YAML_NODE_KIND_SEQ,
AS_YAML_NODE_KIND_KEY,
AS_YAML_NODE_KIND_KEY_VALUE,
AS_YAML_NODE_KIND_LAST
} AsYamlNodeKind;
typedef struct {
AsRefString *key;
AsRefString *value;
AsYamlNodeKind kind;
} AsYamlNode;
const gchar *
as_yaml_node_get_key (const AsNode *node)
{
AsYamlNode *ym;
if (node == NULL)
return NULL;
ym = node->data;
if (ym == NULL)
return NULL;
if (ym->key == NULL || ym->key[0] == '\0')
return NULL;
return ym->key;
}
const gchar *
as_yaml_node_get_value (const AsNode *node)
{
AsYamlNode *ym;
if (node == NULL)
return NULL;
ym = node->data;
if (ym == NULL)
return NULL;
if (ym->value == NULL || ym->value[0] == '\0')
return NULL;
return ym->value;
}
gint
as_yaml_node_get_value_as_int (const AsNode *node)
{
const gchar *tmp;
gchar *endptr = NULL;
gint64 value_tmp;
tmp = as_yaml_node_get_value (node);
if (tmp == NULL)
return G_MAXINT;
value_tmp = g_ascii_strtoll (tmp, &endptr, 10);
if (value_tmp == 0 && tmp == endptr)
return G_MAXINT;
if (value_tmp > G_MAXINT || value_tmp < G_MININT)
return G_MAXINT;
return (gint) value_tmp;
}
guint
as_yaml_node_get_value_as_uint (const AsNode *node)
{
const gchar *tmp;
gchar *endptr = NULL;
guint64 value_tmp;
tmp = as_yaml_node_get_value (node);
if (tmp == NULL)
return G_MAXUINT;
value_tmp = g_ascii_strtoull (tmp, &endptr, 10);
if (value_tmp == 0 && tmp == endptr)
return G_MAXUINT;
if (value_tmp > G_MAXUINT)
return G_MAXUINT;
return (guint) value_tmp;
}
static gboolean
as_node_yaml_destroy_node_cb (AsNode *node, gpointer data)
{
AsYamlNode *ym = node->data;
if (ym == NULL)
return FALSE;
if (ym->key != NULL)
as_ref_string_unref (ym->key);
if (ym->value != NULL)
as_ref_string_unref (ym->value);
g_slice_free (AsYamlNode, ym);
return FALSE;
}
void
as_yaml_unref (AsNode *node)
{
g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
as_node_yaml_destroy_node_cb, NULL);
g_node_destroy (node);
}
static gboolean
as_yaml_to_string_cb (AsNode *node, gpointer data)
{
AsYamlNode *ym = node->data;
GString *str = (GString *) data;
guint depth;
guint i;
depth = g_node_depth (node);
if (depth >= 2) {
for (i = 0; i < depth - 2; i++)
g_string_append (str, " ");
}
if (ym == NULL)
return FALSE;
switch (ym->kind) {
case AS_YAML_NODE_KIND_MAP:
g_string_append (str, "[MAP]");
break;
case AS_YAML_NODE_KIND_SEQ:
g_string_append (str, "[SEQ]");
break;
case AS_YAML_NODE_KIND_KEY:
g_string_append (str, "[KEY]");
break;
case AS_YAML_NODE_KIND_KEY_VALUE:
g_string_append (str, "[KVL]");
break;
case AS_YAML_NODE_KIND_UNKNOWN:
default:
g_string_append (str, "???: ");
break;
}
if (ym->value != NULL) {
g_string_append_printf (str, "%s=%s\n", ym->key, ym->value);
return FALSE;
}
g_string_append_printf (str, "%s\n", ym->key);
return FALSE;
}
GString *
as_yaml_to_string (AsNode *node)
{
GString *str = g_string_new ("");
g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
as_yaml_to_string_cb, str);
return str;
}
#ifdef AS_BUILD_DEP11
static AsYamlNodeKind
as_yaml_node_get_kind (AsNode *node)
{
AsYamlNode *ym;
if (node == NULL)
return AS_YAML_NODE_KIND_UNKNOWN;
ym = node->data;
if (ym == NULL)
return AS_YAML_NODE_KIND_UNKNOWN;
return ym->kind;
}
static AsYamlNode *
as_yaml_node_new (AsYamlNodeKind kind, AsRefString *id)
{
AsYamlNode *ym;
ym = g_slice_new0 (AsYamlNode);
ym->kind = kind;
if (id != NULL)
ym->key = as_ref_string_ref (id);
return ym;
}
static gchar *
as_yaml_mark_to_str (yaml_mark_t *mark)
{
return g_strdup_printf ("ln:%" G_GSIZE_FORMAT " col:%" G_GSIZE_FORMAT,
mark->line + 1,
mark->column + 1);
}
static gboolean
as_yaml_parser_error_to_gerror (yaml_parser_t *parser, GError **error)
{
g_autofree gchar *problem_str = NULL;
g_autofree gchar *context_str = NULL;
switch (parser->error) {
case YAML_MEMORY_ERROR:
g_set_error_literal (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"not enough memory for parsing");
break;
case YAML_READER_ERROR:
if (parser->problem_value != -1) {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"reader error: %s: #%X at %" G_GSIZE_FORMAT "",
parser->problem,
(guint) parser->problem_value,
parser->problem_offset);
} else {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"reader error: %s at %" G_GSIZE_FORMAT "",
parser->problem,
parser->problem_offset);
}
break;
case YAML_SCANNER_ERROR:
problem_str = as_yaml_mark_to_str (&parser->problem_mark);
if (parser->context) {
context_str = as_yaml_mark_to_str (&parser->context_mark);
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"scanner error: %s at %s, %s at %s",
parser->context,
context_str,
parser->problem,
problem_str);
} else {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"scanner error: %s at %s ",
parser->problem,
problem_str);
}
break;
case YAML_PARSER_ERROR:
problem_str = as_yaml_mark_to_str (&parser->problem_mark);
if (parser->context) {
context_str = as_yaml_mark_to_str (&parser->context_mark);
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"parser error: %s at %s, %s at %s",
parser->context,
context_str,
parser->problem,
problem_str);
} else {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"parser error: %s at %s ",
parser->problem,
problem_str);
}
break;
case YAML_COMPOSER_ERROR:
problem_str = as_yaml_mark_to_str (&parser->problem_mark);
if (parser->context) {
context_str = as_yaml_mark_to_str (&parser->context_mark);
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"composer error: %s at %s, %s at %s",
parser->context,
context_str,
parser->problem,
problem_str);
} else {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"composer error: %s at %s ",
parser->problem, problem_str);
}
break;
default:
/* can't happen */
g_set_error_literal (error,
AS_NODE_ERROR,
AS_NODE_ERROR_INVALID_MARKUP,
"internal error");
break;
}
return FALSE;
}
typedef struct {
AsYamlFromFlags flags;
const gchar * const *locales;
yaml_parser_t *parser;
} AsYamlContext;
static gboolean
as_yaml_node_valid (AsYamlContext *ctx, AsNode *parent, const gchar *key)
{
const gchar *sections[] = { "Name", "Summary", "Description", NULL };
AsYamlNode *ym = parent->data;
/* if native filtering enabled */
if ((ctx->flags & AS_YAML_FROM_FLAG_ONLY_NATIVE_LANGS) == 0)
return TRUE;
/* filter by translatable sections */
if (!g_strv_contains (sections, ym->key))
return TRUE;
/* non-native language */
if (g_strv_contains (ctx->locales, key))
return TRUE;
return FALSE;
}
static gboolean
as_node_yaml_process_layer (AsYamlContext *ctx, AsNode *parent, GError **error)
{
AsYamlNode *ym;
AsNode *last_scalar = NULL;
AsNode *new;
const gchar *tmp;
gboolean valid = TRUE;
yaml_event_t event;
AsRefString *map_str = as_ref_string_new_static ("{");
AsRefString *seq_str = as_ref_string_new_static ("[");
while (valid) {
/* process event */
if (!yaml_parser_parse (ctx->parser, &event))
return as_yaml_parser_error_to_gerror (ctx->parser, error);
switch (event.type) {
case YAML_SCALAR_EVENT:
tmp = (const gchar *) event.data.scalar.value;
if (as_yaml_node_get_kind (parent) != AS_YAML_NODE_KIND_SEQ &&
last_scalar != NULL) {
ym = last_scalar->data;
if (ym->key != NULL)
ym->value = as_ref_string_new (tmp);
ym->kind = AS_YAML_NODE_KIND_KEY_VALUE;
last_scalar = NULL;
} else {
if (as_yaml_node_valid (ctx, parent, tmp)) {
g_autoptr(AsRefString) rstr = as_ref_string_new (tmp);
ym = as_yaml_node_new (AS_YAML_NODE_KIND_KEY, rstr);
} else {
ym = as_yaml_node_new (AS_YAML_NODE_KIND_KEY, NULL);
}
last_scalar = g_node_append_data (parent, ym);
}
break;
case YAML_MAPPING_START_EVENT:
if (last_scalar == NULL) {
ym = as_yaml_node_new (AS_YAML_NODE_KIND_MAP, map_str);
new = g_node_append_data (parent, ym);
} else {
ym = last_scalar->data;
ym->kind = AS_YAML_NODE_KIND_MAP;
new = last_scalar;
}
if (!as_node_yaml_process_layer (ctx, new, error))
return FALSE;
last_scalar = NULL;
break;
case YAML_SEQUENCE_START_EVENT:
if (last_scalar == NULL) {
ym = as_yaml_node_new (AS_YAML_NODE_KIND_SEQ, seq_str);
new = g_node_append_data (parent, ym);
} else {
ym = last_scalar->data;
ym->kind = AS_YAML_NODE_KIND_SEQ;
new = last_scalar;
}
if (!as_node_yaml_process_layer (ctx, new, error))
return FALSE;
last_scalar = NULL;
break;
case YAML_STREAM_START_EVENT:
break;
case YAML_MAPPING_END_EVENT:
case YAML_SEQUENCE_END_EVENT:
case YAML_STREAM_END_EVENT:
valid = FALSE;
break;
default:
break;
}
yaml_event_delete (&event);
}
return TRUE;
}
typedef yaml_parser_t* AsYamlParser;
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(AsYamlParser, yaml_parser_delete, NULL);
#endif
AsNode *
as_yaml_from_data (const gchar *data,
gssize data_len,
AsYamlFromFlags flags,
GError **error)
{
g_autoptr(AsYaml) node = NULL;
#ifdef AS_BUILD_DEP11
AsYamlContext ctx;
yaml_parser_t parser;
g_auto(AsYamlParser) parser_cleanup = NULL;
/* parse */
if (!yaml_parser_initialize (&parser)) {
as_yaml_parser_error_to_gerror (&parser, error);
return NULL;
}
parser_cleanup = &parser;
g_assert (parser_cleanup != NULL);
if (data_len < 0)
data_len = (gssize) strlen (data);
yaml_parser_set_input_string (&parser, (guchar *) data, (gsize) data_len);
node = g_node_new (NULL);
ctx.parser = &parser;
ctx.flags = flags;
ctx.locales = g_get_language_names ();
if (!as_node_yaml_process_layer (&ctx, node, error))
return NULL;
#else
g_set_error_literal (error,
AS_NODE_ERROR,
AS_NODE_ERROR_NO_SUPPORT,
"No DEP-11 support, needs libyaml");
#endif
return g_steal_pointer (&node);
}
#ifdef AS_BUILD_DEP11
static int
as_yaml_read_handler_cb (void *data,
unsigned char *buffer,
size_t size,
size_t *size_read)
{
GInputStream *stream = G_INPUT_STREAM (data);
gssize len = g_input_stream_read (stream,
buffer,
(gsize) size,
NULL,
NULL);
if (len < 0)
return 0;
*size_read = (gsize) len;
return 1;
}
#endif
AsNode *
as_yaml_from_file (GFile *file, AsYamlFromFlags flags, GCancellable *cancellable, GError **error)
{
g_autoptr(AsYaml) node = NULL;
#ifdef AS_BUILD_DEP11
const gchar *content_type = NULL;
yaml_parser_t parser;
g_auto(AsYamlParser) parser_cleanup = NULL;
g_autofree gchar *data = NULL;
g_autoptr(GConverter) conv = NULL;
g_autoptr(GFileInfo) info = NULL;
g_autoptr(GInputStream) file_stream = NULL;
g_autoptr(GInputStream) stream_data = NULL;
AsYamlContext ctx;
/* what kind of file is this */
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
G_FILE_QUERY_INFO_NONE,
cancellable,
error);
if (info == NULL)
return NULL;
/* decompress if required */
file_stream = G_INPUT_STREAM (g_file_read (file, cancellable, error));
if (file_stream == NULL)
return NULL;
content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
if (g_strcmp0 (content_type, "application/gzip") == 0 ||
g_strcmp0 (content_type, "application/x-gzip") == 0) {
conv = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
stream_data = g_converter_input_stream_new (file_stream, conv);
} else if (g_strcmp0 (content_type, "application/x-yaml") == 0) {
stream_data = g_object_ref (file_stream);
} else {
g_set_error (error,
AS_NODE_ERROR,
AS_NODE_ERROR_FAILED,
"cannot process file of type %s",
content_type);
return NULL;
}
/* parse */
if (!yaml_parser_initialize (&parser)) {
as_yaml_parser_error_to_gerror (&parser, error);
return NULL;
}
parser_cleanup = &parser;
g_assert (parser_cleanup != NULL);
yaml_parser_set_input (&parser, as_yaml_read_handler_cb, stream_data);
node = g_node_new (NULL);
ctx.parser = &parser;
ctx.flags = flags;
ctx.locales = g_get_language_names ();
if (!as_node_yaml_process_layer (&ctx, node, error))
return NULL;
#else
g_set_error_literal (error,
AS_NODE_ERROR,
AS_NODE_ERROR_NO_SUPPORT,
"No DEP-11 support, needs libyaml");
#endif
return g_steal_pointer (&node);
}
|