-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathflb_cf_yaml.c
More file actions
3054 lines (2566 loc) · 87.9 KB
/
flb_cf_yaml.c
File metadata and controls
3054 lines (2566 loc) · 87.9 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2026 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_config_format.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_slist.h>
#include <cfl/cfl.h>
#include <cfl/cfl_sds.h>
#include <cfl/cfl_variant.h>
#include <cfl/cfl_kvlist.h>
#include <yaml.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <glob.h>
#endif
#ifdef _WIN32
#include <Windows.h>
#include <strsafe.h>
#define PATH_MAX MAX_PATH
#endif
#include <stdio.h>
enum section {
SECTION_ENV,
SECTION_INCLUDE,
SECTION_SERVICE,
SECTION_PIPELINE,
SECTION_CUSTOM,
SECTION_INPUT,
SECTION_FILTER,
SECTION_OUTPUT,
SECTION_PROCESSOR,
SECTION_PARSER,
SECTION_MULTILINE_PARSER,
SECTION_MULTILINE_PARSER_RULE,
SECTION_STREAM_PROCESSOR,
SECTION_PLUGINS,
SECTION_UPSTREAM_SERVERS,
SECTION_OTHER,
};
static char *section_names[] = {
"env",
"include",
"service",
"pipeline",
"custom",
"input",
"filter",
"output",
"processor",
"parser",
"multiline_parser",
"multiline_parser_rule",
"stream_processor",
"plugins",
"upstream_servers",
"other"
};
struct file_state {
/* file */
flb_sds_t name; /* file name */
flb_sds_t path; /* file root path */
/* parent file state */
struct file_state *parent;
};
struct local_ctx {
int level; /* inclusion level */
struct cfl_list states;
struct mk_list includes;
int service_set;
};
/* yaml_* functions return 1 on success and 0 on failure. */
enum status {
YAML_SUCCESS = 1,
YAML_FAILURE = 0
};
enum state {
STATE_START, /* start state */
STATE_STREAM, /* start/end stream */
STATE_DOCUMENT, /* start/end document */
STATE_SECTION, /* top level */
STATE_SECTION_KEY,
STATE_SECTION_VAL,
STATE_SERVICE, /* 'service' section */
STATE_INCLUDE, /* 'includes' section */
STATE_OTHER, /* any other unknown section */
STATE_CUSTOM, /* custom plugins */
STATE_PIPELINE, /* pipeline groups customs inputs, filters and outputs */
STATE_PLUGIN_INPUT, /* input plugins section */
STATE_PLUGIN_FILTER, /* filter plugins section */
STATE_PLUGIN_OUTPUT, /* output plugins section */
STATE_PLUGIN_START,
STATE_PLUGIN_KEY,
STATE_PLUGIN_VAL,
STATE_PLUGIN_VAL_LIST,
STATE_PLUGIN_VARIANT,
STATE_GROUP_KEY,
STATE_GROUP_VAL,
STATE_INPUT_PROCESSORS,
STATE_INPUT_PROCESSOR,
/* Parser */
STATE_PARSER, /* parser section */
STATE_PARSER_ENTRY, /* a parser definition */
STATE_PARSER_KEY, /* reading a key inside a parser */
STATE_PARSER_VALUE, /* reading a value inside a parser */
/* Multiline Parser */
STATE_MULTILINE_PARSER, /* multiline parser section */
STATE_MULTILINE_PARSER_ENTRY, /* a multiline parser definition */
STATE_MULTILINE_PARSER_VALUE, /* reading a value inside a multiline parser */
STATE_MULTILINE_PARSER_RULE, /* reading a multiline parser rule */
/* Stream Processor */
STATE_STREAM_PROCESSOR,
STATE_STREAM_PROCESSOR_ENTRY,
STATE_STREAM_PROCESSOR_KEY,
/* Plugins */
STATE_PLUGINS,
/* Upstream Servers */
STATE_UPSTREAM_SERVERS,
STATE_UPSTREAM_SERVER,
STATE_UPSTREAM_SERVER_VALUE,
STATE_UPSTREAM_NODE_GROUP,
STATE_UPSTREAM_NODE,
STATE_UPSTREAM_NODE_VALUE,
/* environment variables */
STATE_ENV,
STATE_STOP /* end state */
};
/* parser state allocation flags */
#define HAS_KEY (1 << 0)
#define HAS_KEYVALS (1 << 1)
struct parser_state {
/* tokens state */
enum state state;
/* nesting level */
int level;
/* active section (if any) */
enum section section;
/* active section */
struct flb_cf_section *cf_section;
/* active group */
struct flb_cf_group *cf_group;
/* key value */
flb_sds_t key;
/* section key/value list */
struct cfl_kvlist *keyvals;
/* pointer to current values in a list. */
struct cfl_array *values;
/* pointer to current variant */
struct cfl_variant *variant;
/* if the current variant is reading the key of a kvlist */
cfl_sds_t variant_kvlist_key;
/* are we the owner of the values? */
int allocation_flags;
struct file_state *file;
struct cfl_list _head;
};
static struct parser_state *state_push(struct local_ctx *, enum state);
static struct parser_state *state_push_variant(struct local_ctx *,
struct parser_state *,
int);
static struct parser_state *state_push_withvals(struct local_ctx *,
struct parser_state *,
enum state);
static struct parser_state *state_push_witharr(struct local_ctx *,
struct parser_state *,
enum state);
static struct parser_state *state_push_section(struct local_ctx *, enum state,
enum section);
static struct parser_state *state_push_key(struct local_ctx *, enum state,
const char *key);
static int state_create_section(struct flb_cf *, struct parser_state *, char *);
static int state_create_group(struct flb_cf *, struct parser_state *, char *);
static struct cfl_variant * state_variant_parse_scalar(yaml_event_t *event);
static int state_variant_set_child(struct local_ctx *,
struct parser_state *,
struct cfl_variant *);
static struct parser_state *state_pop(struct local_ctx *ctx);
static struct parser_state *state_create(struct file_state *parent, struct file_state *file);
static void state_destroy(struct parser_state *s);
static int read_config(struct flb_cf *cf, struct local_ctx *ctx,
struct file_state *parent, char *cfg_file);
static char *state_str(enum state val)
{
switch (val) {
case STATE_START:
return "start";
case STATE_STREAM:
return "stream";
case STATE_DOCUMENT:
return "document";
case STATE_SECTION:
return "section";
case STATE_SECTION_KEY:
return "section-key";
case STATE_SECTION_VAL:
return "section-value";
case STATE_SERVICE:
return "service";
case STATE_INCLUDE:
return "include";
case STATE_OTHER:
return "other";
case STATE_CUSTOM:
return "custom";
case STATE_PIPELINE:
return "pipeline";
case STATE_PLUGIN_INPUT:
return "input";
case STATE_PLUGIN_FILTER:
return "filter";
case STATE_PLUGIN_OUTPUT:
return "output";
case STATE_PLUGIN_START:
return "plugin-start";
case STATE_PLUGIN_KEY:
return "plugin-key";
case STATE_PLUGIN_VAL:
return "plugin-value";
case STATE_PLUGIN_VAL_LIST:
return "plugin-values";
case STATE_PLUGIN_VARIANT:
return "plugin-variant";
case STATE_GROUP_KEY:
return "group-key";
case STATE_GROUP_VAL:
return "group-val";
case STATE_INPUT_PROCESSORS:
return "processors";
case STATE_INPUT_PROCESSOR:
return "processor";
case STATE_ENV:
return "env";
case STATE_PARSER:
return "parser";
case STATE_MULTILINE_PARSER:
return "multiline-parser";
case STATE_STREAM_PROCESSOR:
return "stream-processor";
case STATE_PLUGINS:
return "plugins";
case STATE_UPSTREAM_SERVERS:
return "upstream-servers";
case STATE_STOP:
return "stop";
default:
return "unknown";
}
}
static int add_section_type(struct flb_cf *conf, struct parser_state *state)
{
if (conf == NULL || state == NULL) {
return -1;
}
if (state->section == SECTION_INPUT) {
state->cf_section = flb_cf_section_create(conf, "input", 0);
}
else if (state->section == SECTION_FILTER) {
state->cf_section = flb_cf_section_create(conf, "filter", 0);
}
else if (state->section == SECTION_OUTPUT) {
state->cf_section = flb_cf_section_create(conf, "output", 0);
}
else if (state->section == SECTION_CUSTOM) {
state->cf_section = flb_cf_section_create(conf, "customs", 0);
}
else if (state->section == SECTION_PARSER) {
state->cf_section = flb_cf_section_create(conf, "parser", 0);
}
else if (state->section == SECTION_MULTILINE_PARSER) {
state->cf_section = flb_cf_section_create(conf, "multiline_parser", 0);
}
else if (state->section == SECTION_STREAM_PROCESSOR) {
state->cf_section = flb_cf_section_create(conf, "stream_processor", 0);
}
else if (state->section == SECTION_PLUGINS) {
state->cf_section = flb_cf_section_create(conf, "plugins", 0);
}
else if (state->section == SECTION_UPSTREAM_SERVERS) {
state->cf_section = flb_cf_section_create(conf, "upstream_servers", 0);
}
else {
state->cf_section = flb_cf_section_create(conf, "other", 0);
}
if (!state->cf_section) {
return -1;
}
return 0;
}
static char *event_type_str(yaml_event_t *event)
{
switch (event->type) {
case YAML_NO_EVENT:
return "no-event";
case YAML_STREAM_START_EVENT:
return "stream-start-event";
case YAML_STREAM_END_EVENT:
return "stream-end-event";
case YAML_DOCUMENT_START_EVENT:
return "document-start-event";
case YAML_DOCUMENT_END_EVENT:
return "document-end-event";
case YAML_ALIAS_EVENT:
return "alias-event";
case YAML_SCALAR_EVENT:
return "scalar-event";
case YAML_SEQUENCE_START_EVENT:
return "sequence-start-event";
break;
case YAML_SEQUENCE_END_EVENT:
return "sequence-end-event";
case YAML_MAPPING_START_EVENT:
return "mapping-start-event";
case YAML_MAPPING_END_EVENT:
return "mapping-end-event";
default:
return "unknown";
}
}
static char *state_get_last(struct local_ctx *ctx)
{
struct flb_slist_entry *entry;
entry = mk_list_entry_last(&ctx->includes, struct flb_slist_entry, _head);
if (entry == NULL) {
return NULL;
}
return entry->str;
}
static void yaml_error_event_line(struct local_ctx *ctx, struct parser_state *state,
yaml_event_t *event, int line)
{
struct flb_slist_entry *entry;
if (event == NULL) {
flb_error("[config] YAML error found but with no state or event");
return;
}
if (state == NULL) {
flb_error("[config] YAML error found but with no state, line %zu, column %zu: "
"unexpected event '%s' (%d).",
event->start_mark.line + 1, event->start_mark.column,
event_type_str(event), event->type);
return;
}
entry = mk_list_entry_last(&ctx->includes, struct flb_slist_entry, _head);
if (entry == NULL) {
flb_error("[config] YAML error found (no file info), line %zu, column %zu: "
"unexpected event '%s' (%d) in state '%s' (%d).",
event->start_mark.line + 1, event->start_mark.column,
event_type_str(event), event->type, state_str(state->state), state->state);
return;
}
flb_error("[config] YAML error found in file \"%s\", line %zu, column %zu: "
"unexpected event '%s' (%d) in state '%s' (%d).",
entry->str, event->start_mark.line + 1, event->start_mark.column,
event_type_str(event), event->type, state_str(state->state), state->state);
}
#define yaml_error_event(ctx, state, event) \
yaml_error_event_line(ctx, state, event, __LINE__)
static void yaml_error_definition(struct local_ctx *ctx, struct parser_state *state,
yaml_event_t *event, char *value)
{
flb_error("[config] YAML error found in file \"%s\", line %zu, column %zu: "
"duplicated definition of '%s'",
state->file->name, event->start_mark.line + 1, event->start_mark.column,
value);
}
static void yaml_error_plugin_category(struct local_ctx *ctx, struct parser_state *state,
yaml_event_t *event, char *value)
{
flb_error("[config] YAML error found in file \"%s\", line %zu, column %zu: "
"the pipeline component '%s' is not valid. Try one of these values: "
"customs, inputs, filters or outputs.",
state->file->name, event->start_mark.line + 1, event->start_mark.column,
value);
}
static int is_file_included(struct local_ctx *ctx, const char *path)
{
struct mk_list *head;
struct flb_slist_entry *entry;
mk_list_foreach(head, &ctx->includes) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
if (strcmp(entry->str, path) == 0) {
return FLB_TRUE;
}
}
return FLB_FALSE;
}
#ifndef _WIN32
static int read_glob(struct flb_cf *conf, struct local_ctx *ctx,
struct parser_state *state, const char *path)
{
int ret = -1;
glob_t glb;
char tmp[PATH_MAX+1];
const char *glb_path;
size_t idx;
int ret_glb = -1;
if (state->file->path && path[0] != '/') {
ret = snprintf(tmp, PATH_MAX, "%s/%s", state->file->path, path);
if (ret > PATH_MAX) {
return -1;
}
glb_path = tmp;
}
else {
glb_path = path;
}
ret_glb = glob(glb_path, GLOB_NOSORT, NULL, &glb);
if (ret_glb != 0) {
switch(ret_glb){
case GLOB_NOSPACE:
flb_warn("[%s] glob: [%s] no space", __FUNCTION__, glb_path);
break;
case GLOB_NOMATCH:
flb_warn("[%s] glob: [%s] no match", __FUNCTION__, glb_path);
break;
case GLOB_ABORTED:
flb_warn("[%s] glob: [%s] aborted", __FUNCTION__, glb_path);
break;
default:
flb_warn("[%s] glob: [%s] other error", __FUNCTION__, glb_path);
}
return ret;
}
for (idx = 0; idx < glb.gl_pathc; idx++) {
ret = read_config(conf, ctx, state->file, glb.gl_pathv[idx]);
if (ret < 0) {
break;
}
}
globfree(&glb);
return ret;
}
#else
static char *dirname(char *path)
{
char *ptr;
ptr = strrchr(path, '\\');
if (ptr == NULL) {
/* No directory component */
return ".";
}
*ptr++='\0';
return path;
}
static int read_glob(struct flb_cf *conf, struct local_ctx *ctx,
struct parser_state *state, const char *path)
{
char *star, *p0, *p1;
char pattern[MAX_PATH];
char buf[MAX_PATH];
int ret;
struct stat st;
HANDLE hnd;
WIN32_FIND_DATA data;
if (strlen(path) > MAX_PATH - 1) {
return -1;
}
star = strchr(path, '*');
if (star == NULL) {
return -1;
}
/*
* C:\data\tmp\input_*.conf
* 0<-----|
*/
p0 = star;
while (path <= p0 && *p0 != '\\') {
p0--;
}
/*
* C:\data\tmp\input_*.conf
* |---->1
*/
p1 = star;
while (*p1 && *p1 != '\\') {
p1++;
}
memcpy(pattern, path, (p1 - path));
pattern[p1 - path] = '\0';
hnd = FindFirstFileA(pattern, &data);
if (hnd == INVALID_HANDLE_VALUE) {
return 0;
}
do {
/* Ignore the current and parent dirs */
if (!strcmp(".", data.cFileName) || !strcmp("..", data.cFileName)) {
continue;
}
/* Avoid an infinite loop */
if (strchr(data.cFileName, '*')) {
continue;
}
/* Create a path (prefix + filename + suffix) */
memcpy(buf, path, p0 - path + 1);
buf[p0 - path + 1] = '\0';
if (FAILED(StringCchCatA(buf, MAX_PATH, data.cFileName))) {
continue;
}
if (FAILED(StringCchCatA(buf, MAX_PATH, p1))) {
continue;
}
if (strchr(p1, '*')) {
read_glob(conf, ctx, state, buf); /* recursive */
continue;
}
ret = stat(buf, &st);
if (ret == 0 && (st.st_mode & S_IFMT) == S_IFREG) {
if (read_config(conf, ctx, state->file, buf) < 0) {
return -1;
}
}
} while (FindNextFileA(hnd, &data) != 0);
FindClose(hnd);
return 0;
}
#endif
static void print_current_state(struct local_ctx *ctx, struct parser_state *state,
yaml_event_t *event)
{
/* note: change this to flb_info() for debugging purposes */
flb_debug("%*s%s->%s", state->level*2, "", state_str(state->state),
event_type_str(event));
}
static void print_current_properties(struct parser_state *state)
{
struct cfl_list *head;
struct cfl_kvpair *prop;
struct cfl_variant *var;
int idx;
/* note: change flb_debug with flb_info() for debugging purposes */
flb_debug("%*s[%s] PROPERTIES:", state->level*2, "", section_names[state->section]);
cfl_list_foreach(head, &state->keyvals->list) {
prop = cfl_list_entry(head, struct cfl_kvpair, _head);
switch (prop->val->type) {
case CFL_VARIANT_STRING:
flb_debug("%*s%s: %s", (state->level+2)*2, "", prop->key, prop->val->data.as_string);
break;
case CFL_VARIANT_ARRAY:
flb_debug("%*s%s: [", (state->level+2)*2, "", prop->key);
for (idx = 0; idx < prop->val->data.as_array->entry_count; idx++) {
var = cfl_array_fetch_by_index(prop->val->data.as_array, idx);
flb_debug("%*s%s", (state->level+3)*2, "", var->data.as_string);
}
flb_debug("%*s]", (state->level+2)*2, "");
break;
}
}
}
static struct parser_state *get_current_state(struct local_ctx *ctx)
{
struct parser_state *state;
if (cfl_list_size(&ctx->states) <= 0) {
return NULL;
}
state = cfl_list_entry_last(&ctx->states, struct parser_state, _head);
return state;
}
static enum status state_move_into_config_group(struct parser_state *state, struct flb_cf_group *cf_group)
{
struct cfl_list *head;
struct cfl_list *tmp;
struct cfl_kvpair *kvp;
struct cfl_variant *varr;
struct cfl_array *arr;
struct cfl_kvlist *copy;
if (cf_group == NULL) {
flb_error("no group for processor properties");
return YAML_FAILURE;
}
varr = cfl_kvlist_fetch(cf_group->properties, state->key);
if (varr == NULL) {
arr = cfl_array_create(1);
if (arr == NULL) {
flb_error("unable to allocate array");
return YAML_FAILURE;
}
cfl_array_resizable(arr, CFL_TRUE);
if (cfl_kvlist_insert_array(cf_group->properties, state->key, arr) < 0) {
cfl_array_destroy(arr);
flb_error("unable to insert into array");
return YAML_FAILURE;
}
}
else {
arr = varr->data.as_array;
}
copy = cfl_kvlist_create();
if (copy == NULL) {
cfl_array_destroy(arr);
flb_error("unable to allocate kvlist");
return YAML_FAILURE;
}
cfl_list_foreach_safe(head, tmp, &state->keyvals->list) {
kvp = cfl_list_entry(head, struct cfl_kvpair, _head);
if (cfl_kvlist_insert(copy, kvp->key, kvp->val) < 0) {
flb_error("unable to insert to kvlist");
cfl_kvlist_destroy(copy);
return YAML_FAILURE;
}
/* ownership moved to the config group */
kvp->val = NULL;
cfl_kvpair_destroy(kvp);
}
if (cfl_array_append_kvlist(arr, copy) < 0) {
flb_error("unable to insert array into kvlist");
cfl_kvlist_destroy(copy);
return YAML_FAILURE;
}
return YAML_SUCCESS;
}
static enum status state_copy_into_properties(struct parser_state *state, struct flb_cf *conf, struct cfl_kvlist *properties)
{
struct cfl_list *head;
struct cfl_kvpair *kvp;
struct cfl_variant *var;
struct cfl_array *arr;
size_t idx;
size_t entry_count;
int array_all_strings;
cfl_list_foreach(head, &state->keyvals->list) {
kvp = cfl_list_entry(head, struct cfl_kvpair, _head);
switch (kvp->val->type) {
case CFL_VARIANT_STRING:
var = flb_cf_section_property_add(conf,
properties,
kvp->key,
cfl_sds_len(kvp->key),
kvp->val->data.as_string,
cfl_sds_len(kvp->val->data.as_string));
if (var == NULL) {
flb_error("unable to add variant value property");
return YAML_FAILURE;
}
break;
case CFL_VARIANT_ARRAY:
entry_count = kvp->val->data.as_array->entry_count;
array_all_strings = 1;
for (idx = 0; idx < entry_count; idx++) {
var = cfl_array_fetch_by_index(kvp->val->data.as_array, idx);
if (var == NULL || var->type != CFL_VARIANT_STRING) {
array_all_strings = 0;
break;
}
}
if (array_all_strings == 1) {
arr = flb_cf_section_property_add_list(conf, properties,
kvp->key, cfl_sds_len(kvp->key));
if (arr == NULL) {
flb_error("unable to add property list");
return YAML_FAILURE;
}
for (idx = 0; idx < entry_count; idx++) {
var = cfl_array_fetch_by_index(kvp->val->data.as_array, idx);
if (cfl_array_append_string(arr, var->data.as_string) < 0) {
flb_error("unable to append string to array");
return YAML_FAILURE;
}
}
}
else {
if (flb_cf_section_property_add_variant(conf,
properties,
kvp->key,
cfl_sds_len(kvp->key),
kvp->val) == NULL) {
flb_error("unable to add variant property");
return YAML_FAILURE;
}
kvp->val = NULL;
}
break;
case CFL_VARIANT_KVLIST:
if (flb_cf_section_property_add_variant(conf,
properties,
kvp->key,
cfl_sds_len(kvp->key),
kvp->val) == NULL) {
flb_error("unable to add variant property");
return YAML_FAILURE;
}
kvp->val = NULL;
break;
default:
flb_error("unknown value type for properties: %d", kvp->val->type);
return YAML_FAILURE;
}
}
return YAML_SUCCESS;
}
static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
yaml_event_t *event)
{
struct cfl_variant *variant;
struct parser_state *state;
enum status status;
int ret;
char *value;
struct flb_kv *keyval;
char *last_included;
last_included = state_get_last(ctx);
if (last_included == NULL) {
last_included = "**unknown**";
}
state = get_current_state(ctx);
if (state == NULL) {
flb_error("unable to parse yaml: no state");
return YAML_FAILURE;
}
print_current_state(ctx, state, event);
switch (state->state) {
case STATE_START:
switch (event->type) {
case YAML_STREAM_START_EVENT:
state = state_push(ctx, STATE_STREAM);
if (state == NULL) {
flb_error("unable to allocate state");
return YAML_FAILURE;
}
break;
case YAML_NO_EVENT:
state->state = STATE_STOP;
break;
default:
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
case STATE_STREAM:
switch (event->type) {
case YAML_DOCUMENT_START_EVENT:
state = state_push(ctx, STATE_DOCUMENT);
if (state == NULL) {
flb_error("unable to allocate state");
return YAML_FAILURE;
}
break;
case YAML_STREAM_END_EVENT:
state = state_pop(ctx);
if (state == NULL) {
flb_error("no state left");
return YAML_FAILURE;
}
break;
default:
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
case STATE_DOCUMENT:
switch (event->type) {
case YAML_MAPPING_START_EVENT:
state = state_push(ctx, STATE_SECTION);
if (state == NULL) {
flb_error("unable to allocate state");
return YAML_FAILURE;
}
break;
case YAML_DOCUMENT_END_EVENT:
state = state_pop(ctx);
if (state == NULL) {
flb_error("no state left");
return YAML_FAILURE;
}
break;
default:
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
/*
* 'includes'
* --------
*/
case STATE_INCLUDE:
switch (event->type) {
case YAML_SEQUENCE_START_EVENT:
break;
case YAML_SEQUENCE_END_EVENT:
state = state_pop(ctx);
if (state == NULL) {
flb_error("no state left");
return YAML_FAILURE;
}
if (state->state != STATE_SECTION) {
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
case YAML_SCALAR_EVENT:
value = (char *) event->data.scalar.value;
flb_debug("[config yaml] including: %s", value);
if (strchr(value, '*') != NULL) {
ret = read_glob(conf, ctx, state, value);
}
else {
ret = read_config(conf, ctx, state->file, value);
}
if (ret == -1) {
flb_error("[config] including file '%s' at %s:%zu",
value,
last_included, event->start_mark.line + 1);
return YAML_FAILURE;
}
ctx->level++;
break;
default:
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
/* end of 'includes' */
/* Handle the 'parsers' section */
case STATE_PARSER:
switch (event->type) {
case YAML_SEQUENCE_START_EVENT:
/* Start of the parsers list */
break;
case YAML_MAPPING_START_EVENT:
/* we handle each parser definition as a new section */
if (add_section_type(conf, state) == -1) {
flb_error("Unable to add parsers section");
return YAML_FAILURE;
}
/* Start of an individual parser entry */
state = state_push_withvals(ctx, state, STATE_PARSER_ENTRY);
if (!state) {
flb_error("Unable to allocate state for parser entry");
return YAML_FAILURE;
}
break;