Debian Codegraph @

yaml-filter/src/yaml-path.c

raw symbols SCIP
1 #include <stdlib.h>
2 #include <stdbool.h>
3 #include <string.h>
4 #include <sys/queue.h>
5 #include <assert.h>
6
7 #include <yaml.h>
8
9 #include "yaml-path.h"
10
11
12 #define YAML_PATH_MAX_SECTION_ITEMS 256
13
14 #define _STR(x) #x
15 #define STR(x) _STR(x)
16
17
18 typedef enum yaml_path_section_type {
19 YAML_PATH_SECTION_ROOT,
20 YAML_PATH_SECTION_ANCHOR,
21 YAML_PATH_SECTION_INDEX,
22 YAML_PATH_SECTION_SET,
23 YAML_PATH_SECTION_KEY,
24 YAML_PATH_SECTION_SELECTION,
25 } yaml_path_section_type_t;
26
27 typedef struct yaml_path_selection_key_raw {
28 const char *start;
29 size_t len;
30 } yaml_path_selection_key_raw_t;
31
32
33 typedef struct yaml_path_key {
34 const char *key;
35 TAILQ_ENTRY(yaml_path_key) entries;
36 } yaml_path_key_t;
37
38 typedef TAILQ_HEAD(path_key_list, yaml_path_key) path_key_list_t;
39
40
41 typedef struct yaml_path_section {
42 yaml_path_section_type_t type;
43 size_t level;
44 union {
45 const char *anchor;
46 size_t index;
47 size_t *set;
48 const char *key;
49 path_key_list_t selection;
50 } data;
51 TAILQ_ENTRY(yaml_path_section) entries;
52
53 yaml_node_type_t node_type;
54 size_t counter;
55 bool valid;
56 bool next_valid;
57 } yaml_path_section_t;
58
59 typedef TAILQ_HEAD(path_section_list, yaml_path_section) path_section_list_t;
60
61
62 struct yaml_path {
63 path_section_list_t sections_list;
64 size_t sections_count;
65 size_t current_level;
66 size_t start_level;
67
68 yaml_path_error_t error;
69 };
70
71
72 static size_t
73 yaml_path_set_snprint (const size_t *set, char *s, size_t max_len)
74 {
75 assert(set != NULL);
76 if (s == NULL)
77 return -1;
78 size_t len = 0;
79 if (set[0] == 0) {
80 len += snprintf(s, max_len, "[:]");
81 } else {
82 for (size_t i = 1; i <= set[0]; i++)
83 len += snprintf(s + (len < max_len ? len : max_len), max_len - (len < max_len ? len : max_len), "%s%zu", (len ? "," : "["), set[i]);
84 len += snprintf(s + (len < max_len ? len : max_len), max_len - (len < max_len ? len : max_len), "]");
85 }
86 return len;
87 }
88
89 static size_t
90 yaml_path_selection_snprint (const path_key_list_t *selection, char *s, size_t max_len)
91 {
92 assert(selection != NULL);
93 if (s == NULL)
94 return -1;
95 size_t len = 0;
96 yaml_path_key_t *el;
97 if TAILQ_EMPTY(selection) {
98 len += snprintf(s, max_len, ".*");
99 } else {
100 TAILQ_FOREACH(el, selection, entries) {
101 char quote = strchr(el->key, '\'') ? '"' : '\'';
102 len += snprintf(s + (len < max_len ? len : max_len), max_len - (len < max_len ? len : max_len), "%s%c%s%c", (len ? "," : "["), quote, el->key, quote);
103 }
104 len += snprintf(s + (len < max_len ? len : max_len), max_len - (len < max_len ? len : max_len), "]");
105 }
106 return len;
107 }
108
109 static bool
110 yaml_path_set_is_empty (const size_t *set)
111 {
112 assert(set != NULL);
113 return set[0] == 0;
114 }
115
116 static bool
117 yaml_path_set_has_index (const size_t *set, size_t idx)
118 {
119 assert(set != NULL);
120 for (size_t i = 1; i <= set[0]; i++)
121 if (set[i] == idx)
122 return true;
123 return false;
124 }
125
126 static bool
127 yaml_path_selection_is_empty (path_key_list_t *selection)
128 {
129 assert(selection != NULL);
130 return TAILQ_EMPTY(selection) ? true : false;
131 }
132
133 static const char*
134 yaml_path_selection_key_get (path_key_list_t *selection, const char *key)
135 {
136 assert(selection != NULL);
137 yaml_path_key_t *el;
138 TAILQ_FOREACH(el, selection, entries) {
139 if (!strcmp(el->key, key))
140 return el->key;
141 }
142 return NULL;
143 }
144
145 static size_t
146 yaml_path_selection_keys_add (path_key_list_t *selection, yaml_path_selection_key_raw_t *raw_keys, size_t count)
147 {
148 assert(selection != NULL);
149 assert(raw_keys != NULL);
150 for (size_t i = 0; i < count; i++) {
151 yaml_path_key_t *el = malloc(sizeof(*el));
152 if (el == NULL)
153 return i;
154 TAILQ_INSERT_TAIL(selection, el, entries);
155 el->key = strndup(raw_keys[i].start, raw_keys[i].len);
156 if (el->key == NULL)
157 return i;
158 }
159 return count;
160 }
161
162 static void
163 yaml_path_selection_keys_remove (path_key_list_t *selection)
164 {
165 assert(selection != NULL);
166 while (!TAILQ_EMPTY(selection)) {
167 yaml_path_key_t *el = TAILQ_FIRST(selection);
168 TAILQ_REMOVE(selection, el, entries);
169 free((void *)el->key);
170 free(el);
171 }
172 }
173
174 static void
175 yaml_path_sections_remove (yaml_path_t *path)
176 {
177 assert(path != NULL);
178 while (!TAILQ_EMPTY(&path->sections_list)) {
179 yaml_path_section_t *el = TAILQ_FIRST(&path->sections_list);
180 TAILQ_REMOVE(&path->sections_list, el, entries);
181 path->sections_count--;
182 switch (el->type) {
183 case YAML_PATH_SECTION_KEY:
184 free((void *)el->data.key);
185 break;
186 case YAML_PATH_SECTION_ANCHOR:
187 free((void *)el->data.anchor);
188 break;
189 case YAML_PATH_SECTION_SET:
190 free((void *)el->data.set);
191 break;
192 case YAML_PATH_SECTION_SELECTION:
193 yaml_path_selection_keys_remove(&el->data.selection);
194 break;
195 default:
196 break;
197 }
198 free(el);
199 }
200 }
201
202 static yaml_path_section_t*
203 yaml_path_section_create (yaml_path_t *path, yaml_path_section_type_t section_type)
204 {
205 yaml_path_section_t *el = malloc(sizeof(*el));
206 if (el != NULL) {
207 memset(el, 0, sizeof(*el));
208 path->sections_count++;
209 el->level = path->sections_count;
210 el->type = section_type;
211 el->node_type = YAML_NO_NODE;
212 TAILQ_INSERT_TAIL(&path->sections_list, el, entries);
213 if (el->type == YAML_PATH_SECTION_SELECTION) {
214 TAILQ_INIT(&el->data.selection);
215 }
216 }
217 return el;
218 }
219
220 static size_t
221 yaml_path_section_snprint (yaml_path_section_t *section, char *s, size_t max_len)
222 {
223 assert(section != NULL);
224 if (s == NULL)
225 return -1;
226 size_t len;
227 switch (section->type) {
228 case YAML_PATH_SECTION_ROOT:
229 len = snprintf(s, max_len, "$");
230 break;
231 case YAML_PATH_SECTION_KEY: {
232 char quote = '\0';
233 if (strpbrk(section->data.key, "[]().$&*"))
234 quote = strchr(section->data.key, '\'') ? '"' : '\'';
235 if (quote) {
236 len = snprintf(s, max_len, "[%c%s%c]", quote, section->data.key, quote);
237 } else {
238 len = snprintf(s, max_len, ".%s", section->data.key);
239 }
240 }
241 break;
242 case YAML_PATH_SECTION_ANCHOR:
243 len = snprintf(s, max_len, "&%s", section->data.anchor);
244 break;
245 case YAML_PATH_SECTION_INDEX:
246 len = snprintf(s, max_len, "[%zu]", section->data.index);
247 break;
248 case YAML_PATH_SECTION_SET:
249 len = yaml_path_set_snprint(section->data.set, s, max_len);
250 break;
251 case YAML_PATH_SECTION_SELECTION:
252 len = yaml_path_selection_snprint(&section->data.selection, s, max_len);
253 break;
254 default:
255 len = snprintf(s, max_len, "<?>");
256 break;
257 }
258 return len;
259 }
260
261 static void
262 yaml_path_error_set (yaml_path_t *path, yaml_path_error_type_t error_type, const char *message, size_t pos)
263 {
264 assert(path != NULL);
265 path->error.type = error_type;
266 path->error.message = message;
267 path->error.pos = pos;
268 }
269
270 static void
271 yaml_path_error_clear (yaml_path_t *path)
272 {
273 yaml_path_error_set(path, YAML_PATH_ERROR_NONE, NULL, 0);
274 }
275
276 #define return_with_error(error_type, error_text, error_pos) \
277 do { \
278 yaml_path_error_set(path, error_type, (error_text), (error_pos)); \
279 goto error; \
280 } while (0)
281
282 static void
283 yaml_path_parse_impl (yaml_path_t *path, char *s_path) {
284 char *sp = s_path;
285 char *spe = NULL;
286
287 assert(path != NULL);
288
289 if (s_path == NULL || !s_path[0]) {
290 yaml_path_error_set(path, YAML_PATH_ERROR_PARSE, "Path string is NULL or empty", 0);
291 return;
292 }
293
294 while (*sp != '\0') {
295 switch (*sp) {
296 case '.':
297 case '[':
298 if (path->sections_count == 0) {
299 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_ROOT);
300 if (sec == NULL)
301 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
302 }
303 if (*sp == '.') {
304 // Key or Selection
305 spe = sp + 1;
306 if (*spe == '*') {
307 // Empty key selection section means that all keys were selected
308 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_SELECTION);
309 if (sec == NULL)
310 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
311 while (*spe != '.' && *spe != '[' && *spe != '\0')
312 spe++;
313 } else {
314 while (*spe != '.' && *spe != '[' && *spe != '\0')
315 spe++;
316 if (spe == sp+1)
317 return_with_error(YAML_PATH_ERROR_PARSE, "Segment key is missing", sp - s_path);
318 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_KEY);
319 if (sec == NULL)
320 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
321 sec->data.key = strndup(sp + 1, spe-sp - 1);
322 if (sec->data.key == NULL)
323 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (key)", sp - s_path);
324 }
325 sp = spe-1;
326 } else if (*sp == '[') {
327 spe = sp + 1;
328 if (*spe == '*' && *(spe+1) == ']') {
329 // Empty key selection section means that all keys were selected
330 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_SELECTION);
331 if (sec == NULL)
332 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
333 sp = spe+1;
334 } else if (*spe == '\'' || *spe == '"') {
335 // Key(s)
336 size_t keys_count = 0;
337 yaml_path_selection_key_raw_t raw_keys[YAML_PATH_MAX_SECTION_ITEMS] = {{NULL, 0}};
338 sp = spe;
339 while (*spe != ']' && *spe != '\0') {
340 if (keys_count >= YAML_PATH_MAX_SECTION_ITEMS)
341 return_with_error(YAML_PATH_ERROR_SECTION, "Segment keys selection has reached the limit of keys: "STR(YAML_PATH_MAX_SECTION_ITEMS), sp - s_path);
342 char quote = *spe;
343 spe++;
344 while (*spe != quote && *spe != '\0')
345 spe++;
346 if (*spe == '\0')
347 return_with_error(YAML_PATH_ERROR_PARSE, "Segment key is invalid (unexpected end of string, missing closing quotation mark)", sp - s_path);
348 if (spe == sp+1)
349 return_with_error(YAML_PATH_ERROR_PARSE, "Segment key is missing", sp - s_path);
350 spe++;
351 if (*spe == '\0')
352 return_with_error(YAML_PATH_ERROR_PARSE, "Segment key is invalid (unexpected end of string, missing ']')", sp - s_path);
353 if (*spe == ']') {
354 raw_keys[keys_count].start = sp + 1;
355 raw_keys[keys_count].len = spe-sp - 2;
356 keys_count++;
357 } else if (*spe == ',') {
358 spe++;
359 if (*spe != '\'' && *spe != '"')
360 return_with_error(YAML_PATH_ERROR_PARSE, "Segment keys selection is invalid (invalid character)", spe - s_path);
361 raw_keys[keys_count].start = sp + 1;
362 raw_keys[keys_count].len = spe-sp - 3;
363 keys_count++;
364 sp = spe;
365 } else {
366 return_with_error(YAML_PATH_ERROR_PARSE, "Segment key is invalid (invalid character)", spe - s_path);
367 }
368 }
369 if (keys_count == 1) {
370 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_KEY);
371 if (sec == NULL)
372 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
373 sec->data.key = strndup(raw_keys[0].start, raw_keys[0].len);
374 if (sec->data.key == NULL)
375 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (key)", sp - s_path);
376 } else if (keys_count > 1) {
377 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_SELECTION);
378 if (sec == NULL)
379 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
380 if (yaml_path_selection_keys_add(&sec->data.selection, raw_keys, keys_count) != keys_count)
381 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (keys selection)", sp - s_path);
382 }
383 sp = spe;
384 } else {
385 // Indices
386 if (*spe == ':' && *(spe+1) == ']') {
387 // Set (all-inclusive)
388 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_SET);
389 if (sec == NULL)
390 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
391 sec->data.set = malloc(sizeof(size_t) * 1);
392 if (sec->data.set == NULL)
393 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (set)", sp - s_path);
394 sec->data.set[0] = 0;
395 sp = spe+1;
396 } else {
397 while (*spe == ' ' || *spe == '\t')
398 spe++;
399 if (*spe == '-')
400 return_with_error(YAML_PATH_ERROR_PARSE, "Segment index is invalid (negative number)", spe - s_path);
401 size_t idx = strtoul(spe, &spe, 10);
402 if (*spe == ']') {
403 // Index
404 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_INDEX);
405 if (sec == NULL)
406 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
407 sec->data.index = idx;
408 sp = spe;
409 } else if (*spe == ',') {
410 // Set
411 size_t indices[YAML_PATH_MAX_SECTION_ITEMS+1] = {0};
412 while (*spe == ',' && spe > sp+1) {
413 if (indices[0] >= YAML_PATH_MAX_SECTION_ITEMS)
414 return_with_error(YAML_PATH_ERROR_SECTION, "Segment indices set has reached the limit of indices: "STR(YAML_PATH_MAX_SECTION_ITEMS), sp - s_path);
415 sp = spe++;
416 indices[0]++;
417 indices[indices[0]] = idx;
418 while (*spe == ' ' || *spe == '\t')
419 spe++;
420 if (*spe == '-')
421 return_with_error(YAML_PATH_ERROR_PARSE, "Segment set index is invalid (negative number)", spe - s_path);
422 idx = strtoul(spe, &spe, 10);
423 }
424 if (*spe == ']' && spe > sp+1) {
425 indices[0]++;
426 indices[indices[0]] = idx;
427 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_SET);
428 if (sec == NULL)
429 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
430 sec->data.set = malloc(sizeof(*indices) * (indices[0] + 1));
431 if (sec->data.set == NULL)
432 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (set)", sp - s_path);
433 memcpy(sec->data.set, indices, sizeof(*indices) * (indices[0] + 1));
434 sp = spe;
435 } else {
436 return_with_error(YAML_PATH_ERROR_PARSE, "Segment set is invalid (invalid character)", spe - s_path);
437 }
438 } else if (*spe == '\0') {
439 return_with_error(YAML_PATH_ERROR_PARSE, "Segment index is invalid (unexpected end of string, missing ']')", spe - s_path);
440 } else {
441 return_with_error(YAML_PATH_ERROR_PARSE, "Segment index is invalid (invalid character)", spe - s_path);
442 }
443 }
444 }
445 }
446 break;
447 case '&':
448 if (path->sections_count == 0) {
449 spe = sp + 1;
450 while (*spe != '.' && *spe != '[' && *spe != '\0')
451 spe++;
452 if (spe - sp > 1) {
453 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_ANCHOR);
454 if (sec == NULL)
455 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
456 sec->data.anchor = strndup(sp+1, spe-sp-1);
457 if (sec->data.anchor == NULL)
458 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (anchor)", sp - s_path);
459 } else {
460 return_with_error(YAML_PATH_ERROR_PARSE, "Segment anchor is invalid (empty)", spe - s_path);
461 }
462 } else {
463 return_with_error(YAML_PATH_ERROR_SECTION, "Anchor segment is only allowed at the beginning of the path", sp - s_path);
464 }
465 sp = spe - 1;
466 break;
467 case '$':
468 if (path->sections_count == 0) {
469 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_ROOT);
470 if (sec == NULL)
471 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
472 } else {
473 return_with_error(YAML_PATH_ERROR_SECTION, "Root segment is only allowed at the beginning of the path", sp - s_path);
474 }
475 break;
476 default:
477 if (path->sections_count == 0) {
478 spe = sp + 1;
479 // Special beginning of the path (implicit key)
480 while (*spe != '.' && *spe != '[' && *spe != '\0')
481 spe++;
482 yaml_path_section_t *sec = yaml_path_section_create(path, YAML_PATH_SECTION_ROOT);
483 if (sec == NULL)
484 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
485 sec = yaml_path_section_create(path, YAML_PATH_SECTION_KEY);
486 if (sec == NULL)
487 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (section)", sp - s_path);
488 sec->data.key = strndup(sp, spe-sp);
489 if (sec->data.key == NULL)
490 return_with_error(YAML_PATH_ERROR_NOMEM, "Unable to allocate memory (key)", sp - s_path);
491 sp = spe-1;
492 }
493 break;
494 }
495 sp++;
496 }
497
498 if (path->sections_count == 0)
499 return_with_error(YAML_PATH_ERROR_SECTION, "Invalid, empty or meaningless path", 0);
500
501 return; // OK
502
503 error:
504 yaml_path_sections_remove(path);
505 if (path->error.type == YAML_PATH_ERROR_NONE)
506 yaml_path_error_set(path, YAML_PATH_ERROR_PARSE, "Unable to parse the path string", 0);
507 }
508
509 static yaml_path_section_t*
510 yaml_path_section_get_at_level (yaml_path_t *path, size_t level)
511 {
512 assert(path != NULL);
513 yaml_path_section_t *el;
514 TAILQ_FOREACH(el, &path->sections_list, entries) {
515 if (el->level == level)
516 return el;
517 }
518 return NULL;
519 }
520
521 static yaml_path_section_t*
522 yaml_path_section_get_first (yaml_path_t *path)
523 {
524 return yaml_path_section_get_at_level(path, 1);
525 }
526
527 static yaml_path_section_t*
528 yaml_path_section_get_current (yaml_path_t *path)
529 {
530 assert(path != NULL);
531 if (!path->start_level)
532 return NULL;
533 return yaml_path_section_get_at_level(path, path->current_level - path->start_level + 1);
534 }
535
536 static bool
537 yaml_path_section_current_is_last (yaml_path_t *path)
538 {
539 assert(path != NULL);
540 yaml_path_section_t *sec = yaml_path_section_get_current(path);
541 if (sec == NULL)
542 return false;
543 return sec->level == path->sections_count;
544 }
545
546 static bool
547 yaml_path_sections_prev_are_valid (yaml_path_t *path)
548 {
549 assert(path != NULL);
550 int valid = true;
551 yaml_path_section_t *el;
552 TAILQ_FOREACH(el, &path->sections_list, entries) {
553 if (el->level < path->current_level - path->start_level + 1)
554 valid = el->valid && valid;
555 }
556 return valid;
557 }
558
559 static bool
560 yaml_path_section_is_mandatory_container (yaml_path_section_t *sec)
561 {
562 assert(sec != NULL);
563 bool res = false;
564 if ((sec->type == YAML_PATH_SECTION_SELECTION && sec->node_type == YAML_MAPPING_NODE)
565 ||
566 (sec->type == YAML_PATH_SECTION_SET && sec->node_type == YAML_SEQUENCE_NODE))
567 res = true;
568 return res;
569 }
570
571 static const char *
572 yaml_path_filter_event_get_anchor (const yaml_event_t *event)
573 {
574 assert(event != NULL);
575 switch(event->type) {
576 case YAML_MAPPING_START_EVENT:
577 return (const char *)event->data.mapping_start.anchor;
578 case YAML_SEQUENCE_START_EVENT:
579 return (const char *)event->data.sequence_start.anchor;
580 case YAML_SCALAR_EVENT:
581 return (const char *)event->data.scalar.anchor;
582 default:
583 break;
584 }
585 return NULL;
586 }
587
588 static bool
589 yaml_path_is_valid (yaml_path_t *path)
590 {
591 assert(path != NULL);
592 bool valid = true;
593 yaml_path_section_t *el;
594 TAILQ_FOREACH(el, &path->sections_list, entries) {
595 valid = el->valid && valid;
596 }
597 return valid;
598 }
599
600
601 /* Public API -------------------------------------------------------------- */
602
603 yaml_path_t*
604 yaml_path_create (void)
605 {
606 yaml_path_t *ypath = malloc(sizeof(*ypath));
607 if (ypath != NULL) {
608 memset (ypath, 0, sizeof(*ypath));
609 TAILQ_INIT(&ypath->sections_list);
610 }
611 return ypath;
612 }
613
614 int
615 yaml_path_parse (yaml_path_t *path, char *s_path)
616 {
617 if (path == NULL)
618 return -1;
619
620 yaml_path_sections_remove(path);
621 yaml_path_error_clear(path);
622
623 yaml_path_parse_impl(path, s_path);
624 if (path->error.type != YAML_PATH_ERROR_NONE)
625 return -2;
626
627 return 0;
628 }
629
630 void
631 yaml_path_destroy (yaml_path_t *path)
632 {
633 if (path == NULL)
634 return;
635 yaml_path_sections_remove(path);
636 free(path);
637 }
638
639 const yaml_path_error_t*
640 yaml_path_error_get (yaml_path_t *path)
641 {
642 if (path == NULL)
643 return NULL;
644 return &path->error;
645 }
646
647 size_t
648 yaml_path_snprint (yaml_path_t *path, char *s, size_t max_len)
649 {
650 if (s == NULL)
651 return -1;
652 if (path == NULL)
653 return 0;
654
655 size_t len = 0;
656 yaml_path_section_t *el;
657 TAILQ_FOREACH(el, &path->sections_list, entries) {
658 len += yaml_path_section_snprint(el, s + (len < max_len ? len : max_len), max_len - (len < max_len ? len : max_len));
659 }
660 return len;
661 }
662
663 yaml_path_filter_result_t
664 yaml_path_filter_event (yaml_path_t *path, yaml_parser_t *parser, yaml_event_t *event)
665 {
666 if (path == NULL || parser == NULL || event == NULL || path->sections_count == 0)
667 return YAML_PATH_FILTER_RESULT_OUT;
668
669 int res = YAML_PATH_FILTER_RESULT_OUT;
670
671 const char *anchor = yaml_path_filter_event_get_anchor(event);
672
673 if (!path->start_level) {
674 switch (yaml_path_section_get_first(path)->type) {
675 case YAML_PATH_SECTION_ROOT:
676 if (event->type == YAML_DOCUMENT_START_EVENT) {
677 path->start_level = 1;
678 yaml_path_section_get_first(path)->valid = true;
679 }
680 break;
681 case YAML_PATH_SECTION_ANCHOR:
682 if (anchor != NULL && !strcmp(yaml_path_section_get_first(path)->data.anchor, anchor)) {
683 path->start_level = path->current_level;
684 }
685 break;
686 default:
687 break;
688 }
689 }
690
691 yaml_path_section_t *current_section = yaml_path_section_get_current(path);
692 if (current_section) {
693 switch (event->type) {
694 case YAML_DOCUMENT_START_EVENT:
695 case YAML_MAPPING_START_EVENT:
696 case YAML_SEQUENCE_START_EVENT:
697 case YAML_ALIAS_EVENT:
698 case YAML_SCALAR_EVENT:
699 switch (current_section->node_type) {
700 case YAML_NO_NODE:
701 if (current_section->type == YAML_PATH_SECTION_ANCHOR) {
702 current_section->valid = false;
703 if (anchor != NULL && !strcmp(current_section->data.anchor, anchor))
704 current_section->valid = true;
705 }
706 break;
707 case YAML_MAPPING_NODE:
708 if (current_section->type == YAML_PATH_SECTION_KEY) {
709 if (current_section->counter % 2) {
710 current_section->valid = current_section->next_valid;
711 current_section->next_valid = false;
712 } else {
713 current_section->next_valid = !strcmp(current_section->data.key, (const char *)event->data.scalar.value);
714 current_section->valid = false;
715 }
716 } else if (current_section->type == YAML_PATH_SECTION_SELECTION) {
717 if (current_section->counter % 2) {
718 current_section->valid = current_section->next_valid;
719 current_section->next_valid = false;
720 } else {
721 current_section->next_valid = yaml_path_selection_is_empty(&current_section->data.selection)
722 || yaml_path_selection_key_get(&current_section->data.selection, (const char *)event->data.scalar.value) != NULL;
723 current_section->valid = current_section->next_valid;
724 }
725 } else {
726 current_section->valid = false;
727 }
728 break;
729 case YAML_SEQUENCE_NODE:
730 if (current_section->type == YAML_PATH_SECTION_INDEX) {
731 current_section->valid = current_section->data.index == current_section->counter;
732 } else if (current_section->type == YAML_PATH_SECTION_SET) {
733 current_section->valid = yaml_path_set_is_empty(current_section->data.set)
734 || yaml_path_set_has_index(current_section->data.set, current_section->counter);
735 } else {
736 current_section->valid = false;
737 }
738 break;
739 default:
740 break;
741 }
742 current_section->counter++;
743 default:
744 break;
745 }
746 }
747
748 switch (event->type) {
749 case YAML_STREAM_START_EVENT:
750 case YAML_STREAM_END_EVENT:
751 case YAML_NO_EVENT:
752 res = YAML_PATH_FILTER_RESULT_IN;
753 break;
754 case YAML_DOCUMENT_START_EVENT:
755 if (path->start_level == 1)
756 path->current_level++;
757 res = YAML_PATH_FILTER_RESULT_IN;
758 break;
759 case YAML_DOCUMENT_END_EVENT:
760 if (path->start_level == 1)
761 path->current_level--;
762 res = YAML_PATH_FILTER_RESULT_IN;
763 break;
764 case YAML_MAPPING_START_EVENT:
765 case YAML_SEQUENCE_START_EVENT:
766 if (current_section) {
767 if (yaml_path_section_current_is_last(path))
768 if (yaml_path_is_valid(path))
769 res = YAML_PATH_FILTER_RESULT_IN;
770 } else {
771 if (path->current_level > path->start_level) {
772 if (yaml_path_is_valid(path))
773 res = YAML_PATH_FILTER_RESULT_IN;
774 }
775 }
776 path->current_level++;
777 current_section = yaml_path_section_get_current(path);
778 if (current_section) {
779 current_section->node_type = event->type == YAML_MAPPING_START_EVENT ? YAML_MAPPING_NODE : YAML_SEQUENCE_NODE;
780 current_section->counter = 0;
781 }
782 if (current_section) {
783 if (yaml_path_section_is_mandatory_container(current_section) && yaml_path_sections_prev_are_valid(path))
784 res = YAML_PATH_FILTER_RESULT_IN;
785 }
786 break;
787 case YAML_MAPPING_END_EVENT:
788 case YAML_SEQUENCE_END_EVENT:
789 if (current_section) {
790 if (yaml_path_section_is_mandatory_container(current_section) && yaml_path_sections_prev_are_valid(path))
791 res = YAML_PATH_FILTER_RESULT_IN;
792
793 }
794 path->current_level--;
795 current_section = yaml_path_section_get_current(path);
796 if (current_section) {
797 if (yaml_path_section_current_is_last(path))
798 if (yaml_path_is_valid(path))
799 res = YAML_PATH_FILTER_RESULT_IN;
800 } else {
801 if (path->current_level > path->start_level) {
802 if (yaml_path_is_valid(path))
803 res = YAML_PATH_FILTER_RESULT_IN;
804 }
805 }
806 break;
807 case YAML_ALIAS_EVENT:
808 case YAML_SCALAR_EVENT:
809 if (!current_section) {
810 if (path->current_level >= path->start_level)
811 if (yaml_path_is_valid(path))
812 res = YAML_PATH_FILTER_RESULT_IN;
813 } else {
814 if (yaml_path_section_current_is_last(path) && yaml_path_is_valid(path))
815 res = YAML_PATH_FILTER_RESULT_IN;
816 if (current_section->valid
817 && current_section->node_type == YAML_MAPPING_NODE
818 && current_section->counter % 2) {
819 if (yaml_path_section_is_mandatory_container(current_section) && yaml_path_sections_prev_are_valid(path))
820 res = YAML_PATH_FILTER_RESULT_IN_DANGLING_KEY;
821 }
822 }
823 break;
824 default:
825 break;
826 }
827
828 return res;
829 }