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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
|
/*
* fy-input.c - YAML input methods
*
* Copyright (c) 2019 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
*
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#include <sys/mman.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <sys/ioctl.h>
#endif
#include <errno.h>
#include <libfyaml.h>
#include "fy-parse.h"
#include "fy-ctype.h"
#include "fy-input.h"
/* amount of multiplication of page size for CHOP size
* for a 4K page this is 64K blocks
*/
#ifndef FYI_CHOP_MULT
#define FYI_CHOP_MULT 16
#endif
struct fy_input *fy_input_alloc(void)
{
struct fy_input *fyi;
fyi = malloc(sizeof(*fyi));
if (!fyi)
return NULL;
memset(fyi, 0, sizeof(*fyi));
fyi->state = FYIS_NONE;
fyi->refs = 1;
return fyi;
}
void fy_input_free(struct fy_input *fyi)
{
if (!fyi)
return;
assert(fyi->refs == 1);
switch (fyi->state) {
case FYIS_NONE:
case FYIS_QUEUED:
/* nothing to do */
break;
case FYIS_PARSE_IN_PROGRESS:
case FYIS_PARSED:
fy_input_close(fyi);
break;
}
/* always release the memory of the alloc memory */
switch (fyi->cfg.type) {
case fyit_alloc:
free(fyi->cfg.alloc.data);
break;
default:
break;
}
if (fyi->name)
free(fyi->name);
free(fyi);
}
const char *fy_input_get_filename(struct fy_input *fyi)
{
if (!fyi)
return NULL;
return fyi->name;
}
static void fy_input_from_data_setup(struct fy_input *fyi,
struct fy_atom *handle, bool simple)
{
const char *data;
size_t size;
unsigned int aflags;
/* this is an internal method, you'd better to pass garbage */
data = fy_input_start(fyi);
size = fy_input_size(fyi);
fyi->buffer = NULL;
fyi->allocated = 0;
fyi->read = 0;
fyi->chunk = 0;
fyi->chop = 0;
fyi->fp = NULL;
if (!handle)
goto out;
memset(handle, 0, sizeof(*handle));
if (size > 0)
aflags = fy_analyze_scalar_content(data, size,
false, fylb_cr_nl, fyfws_space_tab); /* hardcoded yaml mode */
else
aflags = FYACF_EMPTY | FYACF_FLOW_PLAIN | FYACF_BLOCK_PLAIN | FYACF_SIZE0;
handle->start_mark.input_pos = 0;
handle->start_mark.line = 0;
handle->start_mark.column = 0;
handle->end_mark.input_pos = size;
handle->end_mark.line = 0;
handle->end_mark.column = fy_utf8_count(data, size);
/* if it's plain, all is good */
if (simple || (aflags & FYACF_FLOW_PLAIN)) {
handle->storage_hint = size; /* maximum */
handle->storage_hint_valid = false;
handle->direct_output = !!(aflags & FYACF_JSON_ESCAPE);
handle->style = FYAS_PLAIN;
} else {
handle->storage_hint = 0; /* just calculate */
handle->storage_hint_valid = false;
handle->direct_output = false;
handle->style = FYAS_DOUBLE_QUOTED_MANUAL;
}
handle->empty = !!(aflags & FYACF_EMPTY);
handle->has_lb = !!(aflags & FYACF_LB);
handle->has_ws = !!(aflags & FYACF_WS);
handle->starts_with_ws = !!(aflags & FYACF_STARTS_WITH_WS);
handle->starts_with_lb = !!(aflags & FYACF_STARTS_WITH_LB);
handle->ends_with_ws = !!(aflags & FYACF_ENDS_WITH_WS);
handle->ends_with_lb = !!(aflags & FYACF_ENDS_WITH_LB);
handle->trailing_lb = !!(aflags & FYACF_TRAILING_LB);
handle->size0 = !!(aflags & FYACF_SIZE0);
handle->valid_anchor = !!(aflags & FYACF_VALID_ANCHOR);
handle->chomp = FYAC_STRIP;
handle->increment = 0;
handle->fyi = fyi;
handle->fyi_generation = fyi->generation;
handle->tabsize = 0;
handle->json_mode = false; /* XXX hardcoded */
handle->lb_mode = fylb_cr_nl;
handle->fws_mode = fyfws_space_tab;
out:
fyi->state = FYIS_PARSED;
}
struct fy_input *fy_input_from_data(const char *data, size_t size,
struct fy_atom *handle, bool simple)
{
struct fy_input *fyi;
if (data && size == (size_t)-1)
size = strlen(data);
fyi = fy_input_alloc();
if (!fyi)
return NULL;
fyi->cfg.type = fyit_memory;
fyi->cfg.userdata = NULL;
fyi->cfg.memory.data = data;
fyi->cfg.memory.size = size;
fy_input_from_data_setup(fyi, handle, simple);
return fyi;
}
struct fy_input *fy_input_from_malloc_data(char *data, size_t size,
struct fy_atom *handle, bool simple)
{
struct fy_input *fyi;
if (data && size == (size_t)-1)
size = strlen(data);
fyi = fy_input_alloc();
if (!fyi)
return NULL;
fyi->cfg.type = fyit_alloc;
fyi->cfg.userdata = NULL;
fyi->cfg.alloc.data = data;
fyi->cfg.alloc.size = size;
fy_input_from_data_setup(fyi, handle, simple);
return fyi;
}
void fy_input_close(struct fy_input *fyi)
{
if (!fyi)
return;
switch (fyi->cfg.type) {
case fyit_file:
case fyit_fd:
#if !defined(_MSC_VER)
if (fyi->addr) {
munmap(fyi->addr, fyi->length);
fyi->addr = NULL;
}
#endif
if (fyi->fd != -1) {
if (!fyi->cfg.no_close_fd)
close(fyi->fd);
fyi->fd = -1;
}
if (fyi->buffer) {
free(fyi->buffer);
fyi->buffer = NULL;
}
if (fyi->fp) {
if (!fyi->cfg.no_fclose_fp)
fclose(fyi->fp);
fyi->fp = NULL;
}
break;
case fyit_stream:
case fyit_callback:
if (fyi->buffer) {
free(fyi->buffer);
fyi->buffer = NULL;
}
break;
case fyit_memory:
/* nothing */
break;
case fyit_alloc:
/* nothing */
break;
default:
break;
}
}
struct fy_diag *fy_reader_get_diag(struct fy_reader *fyr)
{
if (fyr && fyr->ops && fyr->ops->get_diag)
return fyr->ops->get_diag(fyr);
return NULL;
}
int fy_reader_file_open(struct fy_reader *fyr, const char *filename)
{
if (!fyr || !filename)
return -1;
if (fyr->ops && fyr->ops->file_open)
return fyr->ops->file_open(fyr, filename);
return open(filename, O_RDONLY);
}
void fy_reader_reset(struct fy_reader *fyr)
{
const struct fy_reader_ops *ops;
struct fy_diag *diag;
if (!fyr)
return;
ops = fyr->ops;
diag = fyr->diag;
fy_input_unref(fyr->current_input);
memset(fyr, 0, sizeof(*fyr));
/* by default we're always in yaml mode */
fyr->mode = fyrm_yaml;
fyr->ops = ops;
fyr->diag = diag;
fyr->current_c = -1;
}
void fy_reader_setup(struct fy_reader *fyr, const struct fy_reader_ops *ops)
{
if (!fyr)
return;
fyr->ops = ops;
fyr->diag = fy_reader_get_diag(fyr);
fyr->current_input = NULL;
fy_reader_reset(fyr);
}
void fy_reader_cleanup(struct fy_reader *fyr)
{
if (!fyr)
return;
fy_input_unref(fyr->current_input);
fyr->current_input = NULL;
fy_reader_reset(fyr);
}
void fy_reader_apply_mode(struct fy_reader *fyr)
{
struct fy_input *fyi;
assert(fyr);
/* set input mode from the current reader settings */
switch (fyr->mode) {
case fyrm_yaml:
fyr->json_mode = false;
fyr->lb_mode = fylb_cr_nl;
fyr->fws_mode = fyfws_space_tab;
break;
case fyrm_json:
fyr->json_mode = true;
fyr->lb_mode = fylb_cr_nl;
fyr->fws_mode = fyfws_space;
break;
case fyrm_yaml_1_1:
fyr->json_mode = false;
fyr->lb_mode = fylb_cr_nl_N_L_P;
fyr->fws_mode = fyfws_space_tab;
break;
}
fyi = fyr->current_input;
if (fyi) {
fyi->json_mode = fyr->json_mode;
fyi->lb_mode = fyr->lb_mode;
fyi->fws_mode = fyr->fws_mode;
}
}
int fy_reader_input_open(struct fy_reader *fyr, struct fy_input *fyi, const struct fy_reader_input_cfg *icfg)
{
struct stat sb;
int rc;
if (!fyi)
return -1;
/* unref any previous input */
fy_input_unref(fyr->current_input);
fyr->current_input = fy_input_ref(fyi);
fy_reader_apply_mode(fyr);
if (!icfg)
memset(&fyr->current_input_cfg, 0, sizeof(fyr->current_input_cfg));
else
fyr->current_input_cfg = *icfg;
/* reset common data */
fyi->buffer = NULL;
fyi->allocated = 0;
fyi->read = 0;
fyi->chunk = 0;
fyi->chop = 0;
fyi->fp = NULL;
switch (fyi->cfg.type) {
case fyit_file:
case fyit_fd:
switch (fyi->cfg.type) {
case fyit_file:
fyi->fd = fy_reader_file_open(fyr, fyi->cfg.file.filename);
fyr_error_check(fyr, fyi->fd != -1, err_out,
"failed to open %s", fyi->cfg.file.filename);
break;
case fyit_fd:
fyi->fd = fyi->cfg.fd.fd;
fyr_error_check(fyr, fyi->fd >= 0, err_out,
"bad file.fd %d", fyi->cfg.fd.fd);
break;
default:
assert(0); // will never happen
}
rc = fstat(fyi->fd, &sb);
fyr_error_check(fyr, rc != -1, err_out,
"failed to fstat %s", fyi->cfg.file.filename);
fyi->length = sb.st_size;
/* only map if not zero (and is not disabled) */
#if !defined(_MSC_VER)
if (sb.st_size > 0 && !fyr->current_input_cfg.disable_mmap_opt) {
fyi->addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fyi->fd, 0);
/* convert from MAP_FAILED to NULL */
if (fyi->addr == MAP_FAILED) {
fyr_debug(fyr, "mmap failed for file %s",
fyi->cfg.file.filename);
fyi->addr = NULL;
}
}
#endif
/* if we've managed to mmap, we' good */
if (fyi->addr)
break;
/* if we're not ignoring stdio, open a FILE* using the fd */
if (!fyi->cfg.ignore_stdio) {
fyi->fp = fdopen(fyi->fd, "r");
fyr_error_check(fyr, rc != -1, err_out, "failed to fdopen %s", fyi->name);
} else
fyi->fp = NULL;
break;
case fyit_stream:
if (!fyi->cfg.ignore_stdio)
fyi->fp = fyi->cfg.stream.fp;
else
fyi->fd = fileno(fyi->cfg.stream.fp);
break;
case fyit_memory:
/* nothing to do for memory */
break;
case fyit_alloc:
/* nothing to do for memory */
break;
case fyit_callback:
break;
default:
assert(0);
break;
}
switch (fyi->cfg.type) {
/* those two need no memory */
case fyit_memory:
case fyit_alloc:
break;
/* all the rest need it */
default:
/* if we're not in mmap mode */
#if !defined(_MSC_VER)
if (fyi->addr && !fyr->current_input_cfg.disable_mmap_opt)
break;
#endif
fyi->chunk = fyi->cfg.chunk;
if (!fyi->chunk)
fyi->chunk = fy_get_pagesize();
fyi->chop = fyi->chunk * FYI_CHOP_MULT;
fyi->buffer = malloc(fyi->chunk);
fyr_error_check(fyr, fyi->buffer, err_out,
"fy_alloc() failed");
fyi->allocated = fyi->chunk;
break;
}
fyr->this_input_start = 0;
fyr->current_input_pos = 0;
fyr->line = 0;
fyr->column = 0;
fyr->current_c = -1;
fyr->current_ptr = NULL;
fyr->current_w = 0;
fyr->current_left = 0;
fyi->state = FYIS_PARSE_IN_PROGRESS;
return 0;
err_out:
fy_input_close(fyi);
return -1;
}
int fy_reader_input_done(struct fy_reader *fyr)
{
struct fy_input *fyi;
void *buf;
if (!fyr)
return -1;
fyi = fyr->current_input;
if (!fyi)
return 0;
switch (fyi->cfg.type) {
case fyit_file:
case fyit_fd:
if (fyi->addr)
break;
/* fall-through */
case fyit_stream:
case fyit_callback:
/* chop extra buffer */
buf = realloc(fyi->buffer, fyr->current_input_pos);
fyr_error_check(fyr, buf || !fyr->current_input_pos, err_out,
"realloc() failed");
fyi->buffer = buf;
fyi->allocated = fyr->current_input_pos;
/* increate input generation; required for direct input to work */
fyi->generation++;
break;
default:
break;
}
fyi->state = FYIS_PARSED;
fy_input_unref(fyi);
fyr->current_input = NULL;
return 0;
err_out:
return -1;
}
int fy_reader_input_scan_token_mark_slow_path(struct fy_reader *fyr)
{
struct fy_input *fyi, *fyi_new = NULL;
assert(fyr);
if (!fy_reader_input_chop_active(fyr))
return 0;
fyi = fyr->current_input;
assert(fyi);
fyi_new = fy_input_alloc();
fyr_error_check(fyr, fyi_new, err_out,
"fy_input_alloc() failed\n");
/* copy the config over */
fyi_new->cfg = fyi->cfg;
fyi_new->name = strdup(fyi->name);
fyr_error_check(fyr, fyi_new->name, err_out,
"strdup() failed\n");
fyi_new->chunk = fyi->chunk;
fyi_new->chop = fyi->chop;
fyi_new->buffer = malloc(fyi->chunk);
fyr_error_check(fyr, fyi_new->buffer, err_out,
"fy_alloc() failed");
fyi_new->allocated = fyi->chunk;
fyi_new->fp = fyi->fp;
fyi->fp = NULL; /* the file pointer now assigned to the new */
fyi_new->lb_mode = fyi->lb_mode;
fyi_new->fws_mode = fyi->fws_mode;
fyi_new->state = FYIS_PARSE_IN_PROGRESS;
/* adjust and copy the left over reads */
assert(fyi->read >= fyr->current_input_pos);
fyi_new->read = fyi->read - fyr->current_input_pos;
if (fyi_new->read > 0)
memcpy(fyi_new->buffer, (char *)fyi->buffer + fyr->current_input_pos, fyi_new->read);
fyr->this_input_start += fyr->current_input_pos;
/* update the reader to point to the new input */
fyr->current_input = fyi_new;
fyr->current_input_pos = 0;
fyr->current_ptr = fyi_new->buffer;
fyr_debug(fyr, "chop at this_input_start=%zu chop=%zu\n", fyr->this_input_start, fyi->chop);
/* free the old input - while references to it exist it will hang around */
fyi->state = FYIS_PARSED;
fy_input_unref(fyi);
fyi = NULL;
return 0;
err_out:
fy_input_unref(fyi_new);
return -1;
}
const void *fy_reader_ptr_slow_path(struct fy_reader *fyr, size_t *leftp)
{
struct fy_input *fyi;
const void *p;
int left;
if (fyr->current_ptr) {
if (leftp)
*leftp = fyr->current_left;
return fyr->current_ptr;
}
fyi = fyr->current_input;
if (!fyi)
return NULL;
/* tokens cannot cross boundaries */
switch (fyi->cfg.type) {
case fyit_file:
case fyit_fd:
if (fyi->addr) {
left = fyi->length - (fyr->this_input_start + fyr->current_input_pos);
p = (char *)fyi->addr + fyr->current_input_pos;
break;
}
/* fall-through */
case fyit_stream:
case fyit_callback:
left = fyi->read - (fyr->this_input_start + fyr->current_input_pos);
p = (char *)fyi->buffer + fyr->current_input_pos;
break;
case fyit_memory:
left = fyi->cfg.memory.size - fyr->current_input_pos;
p = (char *)fyi->cfg.memory.data + fyr->current_input_pos;
break;
case fyit_alloc:
left = fyi->cfg.alloc.size - fyr->current_input_pos;
p = (char *)fyi->cfg.alloc.data + fyr->current_input_pos;
break;
default:
assert(0); /* no streams */
p = NULL;
left = 0;
break;
}
if (leftp)
*leftp = left;
fyr->current_ptr = p;
fyr->current_left = left;
fyr->current_c = fy_utf8_get(p, left, &fyr->current_w);
return p;
}
const void *fy_reader_input_try_pull(struct fy_reader *fyr, struct fy_input *fyi,
size_t pull, size_t *leftp)
{
const void *p;
size_t left, pos, size, nread, nreadreq, missing;
ssize_t snread;
size_t space __FY_DEBUG_UNUSED__;
void *buf;
if (!fyr || !fyi) {
if (leftp)
*leftp = 0;
return NULL;
}
p = NULL;
left = 0;
pos = fyr->current_input_pos;
switch (fyi->cfg.type) {
case fyit_file:
case fyit_fd:
if (fyi->addr) {
assert(fyi->length >= (fyr->this_input_start + pos));
left = fyi->length - (fyr->this_input_start + pos);
if (!left) {
fyr_debug(fyr, "file input exhausted");
break;
}
p = (char *)fyi->addr + pos;
break;
}
/* fall-through */
case fyit_stream:
case fyit_callback:
assert(fyi->read >= pos);
left = fyi->read - pos;
p = (char *)fyi->buffer + pos;
/* enough to satisfy directly */
if (left >= pull)
break;
/* no more */
if (fyi->eof) {
if (!left) {
fyr_debug(fyr, "input exhausted (EOF)");
p = NULL;
}
break;
}
space = fyi->allocated - pos;
/* if we're missing more than the buffer space */
missing = pull - left;
fyr_debug(fyr, "input: allocated=%zu read=%zu pos=%zu pull=%zu left=%zu space=%zu missing=%zu",
fyi->allocated, fyi->read, pos, pull, left, space, missing);
if (pos + pull > fyi->allocated) {
/* align size to chunk */
size = fyi->allocated + missing + fyi->chunk - 1;
size = size - size % fyi->chunk;
fyr_debug(fyr, "input buffer missing %zu bytes (pull=%zu)", missing, pull);
buf = realloc(fyi->buffer, size);
if (!buf) {
fyr_error(fyr, "realloc() failed");
goto err_out;
}
fyr_debug(fyr, "input read allocated=%zu new-size=%zu", fyi->allocated, size);
fyi->buffer = buf;
fyi->allocated = size;
fyi->generation++;
space = fyi->allocated - pos;
p = (char *)fyi->buffer + pos;
}
/* always try to read up to the allocated space */
do {
nreadreq = fyi->allocated - fyi->read;
assert(nreadreq > 0);
if (fyi->cfg.type == fyit_callback) {
fyr_debug(fyr, "performing callback request of %zu", nreadreq);
nread = fyi->cfg.callback.input(fyi->cfg.userdata, (char *)fyi->buffer + fyi->read, nreadreq);
fyr_debug(fyr, "callback returned %zu", nread);
if (nread <= 0) {
if (!nread) {
fyi->eof = true;
fyr_debug(fyr, "callback got EOF");
} else {
fyi->err = true;
fyr_debug(fyr, "callback got error");
}
break;
}
} else if (fyi->fp) {
fyr_debug(fyr, "performing fread request of %zu", nreadreq);
nread = fread((char *)fyi->buffer + fyi->read, 1, nreadreq, fyi->fp);
fyr_debug(fyr, "fread returned %zu", nread);
if (nread <= 0) {
fyi->err = ferror(fyi->fp);
if (fyi->err) {
fyi->eof = true;
fyr_debug(fyr, "fread got ERROR");
goto err_out;
}
fyi->eof = feof(fyi->fp);
if (fyi->eof)
fyr_debug(fyr, "fread got EOF");
nread = 0;
break;
}
} else if (fyi->fd >= 0) {
fyr_debug(fyr, "performing read request of %zu", nreadreq);
do {
snread = read(fyi->fd, (char *)fyi->buffer + fyi->read, nreadreq);
} while (snread == -1 && errno == EAGAIN);
fyr_debug(fyr, "read returned %zd", snread);
if (snread == -1) {
fyi->err = true;
fyi->eof = true;
fyr_error(fyr, "read() failed: %s", strerror(errno));
goto err_out;
}
if (!snread) {
fyi->eof = true;
nread = 0;
break;
}
nread = snread;
} else {
fyr_error(fyr, "No FILE* nor fd available?");
fyi->eof = true;
nread = 0;
goto err_out;
}
assert(nread > 0);
fyi->read += nread;
left = fyi->read - pos;
} while (left < pull);
/* no more, move it to parsed input chunk list */
if (!left) {
fyr_debug(fyr, "input exhausted");
p = NULL;
}
break;
case fyit_memory:
assert(fyi->cfg.memory.size >= pos);
left = fyi->cfg.memory.size - pos;
if (!left) {
fyr_debug(fyr, "memory input exhausted");
break;
}
p = (char *)fyi->cfg.memory.data + pos;
break;
case fyit_alloc:
assert(fyi->cfg.alloc.size >= pos);
left = fyi->cfg.alloc.size - pos;
if (!left) {
fyr_debug(fyr, "alloc input exhausted");
break;
}
p = (char *)fyi->cfg.alloc.data + pos;
break;
default:
assert(0);
break;
}
if (leftp)
*leftp = left;
return p;
err_out:
if (leftp)
*leftp = 0;
return NULL;
}
void
fy_reader_advance_slow_path(struct fy_reader *fyr, int c)
{
bool is_line_break = false;
size_t w;
/* skip this character (optimize case of being the current) */
w = c == fyr->current_c ? (size_t)fyr->current_w : fy_utf8_width(c);
fy_reader_advance_octets(fyr, w);
/* first check for CR/LF */
if (c == '\r' && fy_reader_peek(fyr) == '\n') {
fy_reader_advance_octets(fyr, 1);
is_line_break = true;
} else if (fy_reader_is_lb(fyr, c))
is_line_break = true;
if (is_line_break) {
fyr->column = 0;
fyr->line++;
} else if (fyr->tabsize && fy_is_tab(c))
fyr->column += (fyr->tabsize - (fyr->column % fyr->tabsize));
else
fyr->column++;
}
struct fy_input *fy_input_create(const struct fy_input_cfg *fyic)
{
struct fy_input *fyi = NULL;
int ret;
fyi = fy_input_alloc();
if (!fyi)
return NULL;
fyi->cfg = *fyic;
/* copy filename pointers and switch */
switch (fyic->type) {
case fyit_file:
fyi->name = strdup(fyic->file.filename);
break;
case fyit_fd:
ret = asprintf(&fyi->name, "<fd-%d>", fyic->fd.fd);
if (ret == -1)
fyi->name = NULL;
break;
case fyit_stream:
if (fyic->stream.name)
fyi->name = strdup(fyic->stream.name);
else if (fyic->stream.fp == stdin)
fyi->name = strdup("<stdin>");
else {
ret = asprintf(&fyi->name, "<stream-%d>",
fileno(fyic->stream.fp));
if (ret == -1)
fyi->name = NULL;
}
break;
case fyit_memory:
ret = asprintf(&fyi->name, "<memory-@%p-%p>",
fyic->memory.data, (char *)fyic->memory.data + fyic->memory.size - 1);
if (ret == -1)
fyi->name = NULL;
break;
case fyit_alloc:
ret = asprintf(&fyi->name, "<alloc-@%p-%p>",
fyic->memory.data, (char *)fyic->memory.data + fyic->memory.size - 1);
if (ret == -1)
fyi->name = NULL;
break;
case fyit_callback:
ret = asprintf(&fyi->name, "<callback>");
if (ret == -1)
fyi->name = NULL;
break;
default:
assert(0);
break;
}
if (!fyi->name)
goto err_out;
fyi->buffer = NULL;
fyi->allocated = 0;
fyi->read = 0;
fyi->chunk = 0;
fyi->chop = 0;
fyi->fp = NULL;
fyi->fd = -1;
fyi->addr = NULL;
fyi->length = -1;
/* default modes */
fyi->lb_mode = fylb_cr_nl;
fyi->fws_mode = fyfws_space_tab;
return fyi;
err_out:
fy_input_unref(fyi);
return NULL;
}
/* ensure that there are at least size octets available */
const void *fy_reader_ensure_lookahead_slow_path(struct fy_reader *fyr, size_t size, size_t *leftp)
{
const void *p;
size_t left;
if (!leftp)
leftp = &left;
p = fy_reader_ptr(fyr, leftp);
if (!p || *leftp < size) {
fyr_debug(fyr, "ensure lookahead size=%zd left=%zd (%s - %zu)",
size, *leftp,
fy_input_get_filename(fyr->current_input),
fyr->current_input_pos);
p = fy_reader_input_try_pull(fyr, fyr->current_input, size, leftp);
if (!p || *leftp < size)
return NULL;
fyr->current_ptr = p;
fyr->current_left = *leftp;
fyr->current_c = fy_utf8_get(fyr->current_ptr, fyr->current_left, &fyr->current_w);
}
return p;
}
|