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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
|
/*
* fy-emit.c - Internal YAML emitter 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 <limits.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#elif defined (_MSC_VER)
#define STDOUT_FILENO _fileno(stdin)
#endif
#include <ctype.h>
#include <errno.h>
#include <libfyaml.h>
#include "fy-parse.h"
#include "fy-emit.h"
/* fwd decl */
void fy_emit_write(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str, int len);
void fy_emit_printf(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *fmt, ...)
FY_ATTRIBUTE(format(printf, 3, 4));
static inline bool fy_emit_is_json_mode(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags;
if (emit->force_json)
return true;
flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_JSON || flags == FYECF_MODE_JSON_TP || flags == FYECF_MODE_JSON_ONELINE;
}
static inline bool fy_emit_is_flow_mode(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_FLOW || flags == FYECF_MODE_FLOW_ONELINE;
}
static inline bool fy_emit_is_block_mode(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_BLOCK || flags == FYECF_MODE_DEJSON || flags == FYECF_MODE_PRETTY;
}
static inline bool fy_emit_is_oneline(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_FLOW_ONELINE || flags == FYECF_MODE_JSON_ONELINE;
}
static inline bool fy_emit_is_dejson_mode(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_DEJSON;
}
static inline bool fy_emit_is_pretty_mode(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_PRETTY;
}
static inline bool fy_emit_is_manual(const struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
return flags == FYECF_MODE_MANUAL;
}
static inline int fy_emit_indent(struct fy_emitter *emit)
{
int indent;
indent = (emit->cfg.flags & FYECF_INDENT(FYECF_INDENT_MASK)) >> FYECF_INDENT_SHIFT;
return indent ? indent : 2;
}
static inline int fy_emit_width(struct fy_emitter *emit)
{
int width;
width = (emit->cfg.flags & FYECF_WIDTH(FYECF_WIDTH_MASK)) >> FYECF_WIDTH_SHIFT;
if (width == 0)
return 80;
if (width == FYECF_WIDTH_MASK)
return INT_MAX;
return width;
}
static inline bool fy_emit_output_comments(struct fy_emitter *emit)
{
return !!(emit->cfg.flags & FYECF_OUTPUT_COMMENTS);
}
static int fy_emit_node_check_json(struct fy_emitter *emit, struct fy_node *fyn)
{
struct fy_document *fyd;
struct fy_node *fyni;
struct fy_node_pair *fynp, *fynpi;
int ret;
if (!fyn)
return 0;
fyd = fyn->fyd;
switch (fyn->type) {
case FYNT_SCALAR:
FYD_TOKEN_ERROR_CHECK(fyd, fyn->scalar, FYEM_INTERNAL,
!fy_node_is_alias(fyn), err_out,
"aliases not allowed in JSON emit mode");
break;
case FYNT_SEQUENCE:
for (fyni = fy_node_list_head(&fyn->sequence); fyni;
fyni = fy_node_next(&fyn->sequence, fyni)) {
ret = fy_emit_node_check_json(emit, fyni);
if (ret)
return ret;
}
break;
case FYNT_MAPPING:
for (fynp = fy_node_pair_list_head(&fyn->mapping); fynp; fynp = fynpi) {
fynpi = fy_node_pair_next(&fyn->mapping, fynp);
ret = fy_emit_node_check_json(emit, fynp->key);
if (ret)
return ret;
ret = fy_emit_node_check_json(emit, fynp->value);
if (ret)
return ret;
}
break;
}
return 0;
err_out:
return -1;
}
static int fy_emit_node_check(struct fy_emitter *emit, struct fy_node *fyn)
{
int ret;
if (!fyn)
return 0;
if (fy_emit_is_json_mode(emit) && !emit->source_json) {
ret = fy_emit_node_check_json(emit, fyn);
if (ret)
return ret;
}
return 0;
}
void fy_emit_node_internal(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent, bool is_key);
void fy_emit_scalar(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent, bool is_key);
void fy_emit_sequence(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent);
void fy_emit_mapping(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent);
void fy_emit_write(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str, int len)
{
int c, w;
const char *m, *e;
int outlen;
if (!len)
return;
outlen = emit->cfg.output(emit, type, str, len, emit->cfg.userdata);
if (outlen != len)
emit->output_error = true;
e = str + len;
while ((c = fy_utf8_get(str, (e - str), &w)) >= 0) {
/* special handling for MSDOS */
if (c == '\r' && (e - str) > 1 && str[1] == '\n') {
str += 2;
emit->column = 0;
emit->line++;
continue;
}
/* regular line break */
if (fy_is_lb_r_n(c)) {
emit->column = 0;
emit->line++;
str += w;
continue;
}
/* completely ignore ANSI color escape sequences */
if (c == '\x1b' && (e - str) > 2 && str[1] == '[' &&
(m = memchr(str, 'm', e - str)) != NULL) {
str = m + 1;
continue;
}
emit->column++;
str += w;
}
}
void fy_emit_puts(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str)
{
fy_emit_write(emit, type, str, strlen(str));
}
void fy_emit_putc(struct fy_emitter *emit, enum fy_emitter_write_type type, int c)
{
char buf[FY_UTF8_FORMAT_BUFMIN];
fy_utf8_format(c, buf, fyue_none);
fy_emit_puts(emit, type, buf);
}
void fy_emit_vprintf(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *fmt, va_list ap)
{
char *str;
int size;
va_list ap2;
va_copy(ap2, ap);
size = vsnprintf(NULL, 0, fmt, ap);
if (size < 0)
return;
str = FY_ALLOCA(size + 1);
size = vsnprintf(str, size + 1, fmt, ap2);
if (size < 0)
return;
fy_emit_write(emit, type, str, size);
}
void fy_emit_printf(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fy_emit_vprintf(emit, type, fmt, ap);
va_end(ap);
}
void fy_emit_write_ws(struct fy_emitter *emit)
{
fy_emit_putc(emit, fyewt_whitespace, ' ');
emit->flags |= FYEF_WHITESPACE;
}
void fy_emit_write_indent(struct fy_emitter *emit, int indent)
{
int len;
char *ws;
indent = indent > 0 ? indent : 0;
if (!fy_emit_indentation(emit) || emit->column > indent ||
(emit->column == indent && !fy_emit_whitespace(emit)))
fy_emit_putc(emit, fyewt_linebreak, '\n');
if (emit->column < indent) {
len = indent - emit->column;
ws = FY_ALLOCA(len + 1);
memset(ws, ' ', len);
ws[len] = '\0';
fy_emit_write(emit, fyewt_indent, ws, len);
}
emit->flags |= FYEF_WHITESPACE | FYEF_INDENTATION;
}
enum document_indicator {
di_question_mark,
di_colon,
di_dash,
di_left_bracket,
di_right_bracket,
di_left_brace,
di_right_brace,
di_comma,
di_bar,
di_greater,
di_single_quote_start,
di_single_quote_end,
di_double_quote_start,
di_double_quote_end,
di_ambersand,
di_star,
};
void fy_emit_write_indicator(struct fy_emitter *emit,
enum document_indicator indicator,
int flags, int indent,
enum fy_emitter_write_type wtype)
{
switch (indicator) {
case di_question_mark:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, '?');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_OPEN_ENDED);
break;
case di_colon:
if (!(flags & DDNF_SIMPLE)) {
if (!emit->flow_level && !fy_emit_is_oneline(emit))
fy_emit_write_indent(emit, indent);
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
}
fy_emit_putc(emit, wtype, ':');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_OPEN_ENDED);
break;
case di_dash:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, '-');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_OPEN_ENDED);
break;
case di_left_bracket:
case di_left_brace:
emit->flow_level++;
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, indicator == di_left_bracket ? '[' : '{');
emit->flags |= FYEF_WHITESPACE;
emit->flags &= ~(FYEF_INDENTATION | FYEF_OPEN_ENDED);
break;
case di_right_bracket:
case di_right_brace:
emit->flow_level--;
fy_emit_putc(emit, wtype, indicator == di_right_bracket ? ']' : '}');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION | FYEF_OPEN_ENDED);
break;
case di_comma:
fy_emit_putc(emit, wtype, ',');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION | FYEF_OPEN_ENDED);
break;
case di_bar:
case di_greater:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, indicator == di_bar ? '|' : '>');
emit->flags &= ~(FYEF_INDENTATION | FYEF_WHITESPACE | FYEF_OPEN_ENDED);
break;
case di_single_quote_start:
case di_double_quote_start:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, indicator == di_single_quote_start ? '\'' : '"');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION | FYEF_OPEN_ENDED);
break;
case di_single_quote_end:
case di_double_quote_end:
fy_emit_putc(emit, wtype, indicator == di_single_quote_end ? '\'' : '"');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION | FYEF_OPEN_ENDED);
break;
case di_ambersand:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, '&');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION);
break;
case di_star:
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
fy_emit_putc(emit, wtype, '*');
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION);
break;
}
}
int fy_emit_increase_indent(struct fy_emitter *emit, int flags, int indent)
{
if (indent < 0)
return (flags & DDNF_FLOW) ? fy_emit_indent(emit) : 0;
if (!(flags & DDNF_INDENTLESS))
return indent + fy_emit_indent(emit);
return indent;
}
void fy_emit_write_comment(struct fy_emitter *emit, int flags, int indent, const char *str, size_t len, struct fy_atom *handle)
{
const char *s, *e, *sr;
int c, w;
bool breaks;
if (!str || !len)
return;
if (len == (size_t)-1)
len = strlen(str);
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
indent = emit->column;
s = str;
e = str + len;
sr = s; /* start of normal output run */
breaks = false;
while (s < e && (c = fy_utf8_get(s, e - s, &w)) > 0) {
if (fy_is_lb_m(c, fy_atom_lb_mode(handle))) {
/* output run */
fy_emit_write(emit, fyewt_comment, sr, s - sr);
sr = s + w;
fy_emit_write_indent(emit, indent);
emit->flags |= FYEF_INDENTATION;
breaks = true;
} else {
if (breaks) {
fy_emit_write(emit, fyewt_comment, sr, s - sr);
sr = s;
fy_emit_write_indent(emit, indent);
}
emit->flags &= ~FYEF_INDENTATION;
breaks = false;
}
s += w;
}
/* dump what's remaining */
fy_emit_write(emit, fyewt_comment, sr, s - sr);
emit->flags |= (FYEF_WHITESPACE | FYEF_INDENTATION);
}
struct fy_atom *fy_emit_token_comment_handle(struct fy_emitter *emit, struct fy_token *fyt, enum fy_comment_placement placement)
{
struct fy_atom *handle;
handle = fy_token_comment_handle(fyt, placement, false);
return handle && fy_atom_is_set(handle) ? handle : NULL;
}
void fy_emit_document_start_indicator(struct fy_emitter *emit)
{
/* do not emit twice */
if (emit->flags & FYEF_HAD_DOCUMENT_START)
return;
/* do not try to emit if it's json mode */
if (fy_emit_is_json_mode(emit))
goto no_doc_emit;
/* output linebreak anyway */
if (emit->column)
fy_emit_putc(emit, fyewt_linebreak, '\n');
/* stripping doc indicators, do not emit */
if (emit->cfg.flags & FYECF_STRIP_DOC)
goto no_doc_emit;
/* ok, emit document start indicator */
fy_emit_puts(emit, fyewt_document_indicator, "---");
emit->flags &= ~FYEF_WHITESPACE;
emit->flags |= FYEF_HAD_DOCUMENT_START;
return;
no_doc_emit:
emit->flags &= ~FYEF_HAD_DOCUMENT_START;
}
struct fy_token *fy_node_value_token(struct fy_node *fyn)
{
struct fy_token *fyt;
if (!fyn)
return NULL;
switch (fyn->type) {
case FYNT_SCALAR:
fyt = fyn->scalar;
break;
case FYNT_SEQUENCE:
fyt = fyn->sequence_start;
break;
case FYNT_MAPPING:
fyt = fyn->mapping_start;
break;
default:
fyt = NULL;
break;
}
return fyt;
}
bool fy_emit_token_has_comment(struct fy_emitter *emit, struct fy_token *fyt, enum fy_comment_placement placement)
{
return fy_emit_token_comment_handle(emit, fyt, placement) ? true : false;
}
bool fy_emit_node_has_comment(struct fy_emitter *emit, struct fy_node *fyn, enum fy_comment_placement placement)
{
return fy_emit_token_has_comment(emit, fy_node_value_token(fyn), placement);
}
void fy_emit_token_comment(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent,
enum fy_comment_placement placement)
{
struct fy_atom *handle;
char *text;
const char *t;
int len;
handle = fy_emit_token_comment_handle(emit, fyt, placement);
if (!handle)
return;
len = fy_atom_format_text_length(handle);
if (len < 0)
return;
text = FY_ALLOCA(len + 1);
if (placement == fycp_top || placement == fycp_bottom) {
fy_emit_write_indent(emit, indent);
emit->flags |= FYEF_WHITESPACE;
}
t = fy_atom_format_text(handle, text, len + 1);
fy_emit_write_comment(emit, flags, indent, t, len, handle);
emit->flags &= ~FYEF_INDENTATION;
if (placement == fycp_top || placement == fycp_bottom) {
fy_emit_write_indent(emit, indent);
emit->flags |= FYEF_WHITESPACE;
}
}
void fy_emit_node_comment(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent,
enum fy_comment_placement placement)
{
struct fy_token *fyt;
if (!fy_emit_output_comments(emit) || (unsigned int)placement >= fycp_max)
return;
fyt = fy_node_value_token(fyn);
if (!fyt)
return;
fy_emit_token_comment(emit, fyt, flags, indent, placement);
}
void fy_emit_common_node_preamble(struct fy_emitter *emit,
struct fy_token *fyt_anchor,
struct fy_token *fyt_tag,
int flags, int indent)
{
const char *anchor = NULL;
const char *tag = NULL;
const char *td_prefix __FY_DEBUG_UNUSED__;
const char *td_handle;
size_t td_prefix_size, td_handle_size;
size_t tag_len = 0, anchor_len = 0;
bool json_mode = false;
json_mode = fy_emit_is_json_mode(emit);
if (!json_mode) {
if (!(emit->cfg.flags & FYECF_STRIP_LABELS)) {
if (fyt_anchor)
anchor = fy_token_get_text(fyt_anchor, &anchor_len);
}
if (!(emit->cfg.flags & FYECF_STRIP_TAGS)) {
if (fyt_tag)
tag = fy_token_get_text(fyt_tag, &tag_len);
}
if (anchor) {
fy_emit_write_indicator(emit, di_ambersand, flags, indent, fyewt_anchor);
fy_emit_write(emit, fyewt_anchor, anchor, anchor_len);
}
if (tag) {
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
td_handle = fy_tag_token_get_directive_handle(fyt_tag, &td_handle_size);
assert(td_handle);
td_prefix = fy_tag_token_get_directive_prefix(fyt_tag, &td_prefix_size);
assert(td_prefix);
if (!td_handle_size)
fy_emit_printf(emit, fyewt_tag, "!<%.*s>", (int)tag_len, tag);
else
fy_emit_printf(emit, fyewt_tag, "%.*s%.*s",
(int)td_handle_size, td_handle,
(int)(tag_len - td_prefix_size), tag + td_prefix_size);
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION);
}
}
/* content for root always starts on a new line */
if ((flags & DDNF_ROOT) && emit->column != 0 &&
!(emit->flags & FYEF_HAD_DOCUMENT_START)) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
}
void fy_emit_node_internal(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent, bool is_key)
{
enum fy_node_type type;
struct fy_anchor *fya;
struct fy_token *fyt_anchor = NULL;
if (!(emit->cfg.flags & FYECF_STRIP_LABELS)) {
fya = fy_document_lookup_anchor_by_node(emit->fyd, fyn);
if (fya)
fyt_anchor = fya->anchor;
}
fy_emit_common_node_preamble(emit, fyt_anchor, fyn->tag, flags, indent);
type = fyn ? fyn->type : FYNT_SCALAR;
if (type != FYNT_SCALAR && (flags & DDNF_ROOT) && emit->column != 0) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
switch (type) {
case FYNT_SCALAR:
/* if we're pretty and root at column 0 (meaning it's a single scalar document) output --- */
if ((flags & DDNF_ROOT) && fy_emit_is_pretty_mode(emit) && !emit->column &&
!fy_emit_is_flow_mode(emit) && !(flags & DDNF_FLOW))
fy_emit_document_start_indicator(emit);
fy_emit_scalar(emit, fyn, flags, indent, is_key);
break;
case FYNT_SEQUENCE:
FYD_TOKEN_ERROR_CHECK(fyn->fyd, fyn->sequence_start, FYEM_INTERNAL,
!is_key || !fy_emit_is_json_mode(emit), err_out,
"JSON does not allow sequences as keys");
fy_emit_sequence(emit, fyn, flags, indent);
break;
case FYNT_MAPPING:
FYD_TOKEN_ERROR_CHECK(fyn->fyd, fyn->mapping_start, FYEM_INTERNAL,
!is_key || !fy_emit_is_json_mode(emit), err_out,
"JSON does not allow mappings as keys");
fy_emit_mapping(emit, fyn, flags, indent);
break;
}
err_out:
/* nothing */
return;
}
void fy_emit_token_write_plain(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent)
{
bool allow_breaks, should_indent, spaces, breaks;
int c;
enum fy_emitter_write_type wtype;
const char *str = NULL;
size_t len = 0;
struct fy_atom *atom;
struct fy_atom_iter iter;
/* null and not json mode */
if (!fyt && !fy_emit_is_json_mode(emit))
goto out;
wtype = (flags & DDNF_SIMPLE_SCALAR_KEY) ? fyewt_plain_scalar_key : fyewt_plain_scalar;
atom = fy_token_atom(fyt);
/* null and json mode */
if (fy_emit_is_json_mode(emit) && (/* (!fyt || !atom || atom->size0) || */ fyt->scalar.is_null)) {
fy_emit_puts(emit, wtype, "null");
goto out;
}
/* simple case first (90% of cases) */
str = fy_token_get_direct_output(fyt, &len);
if (str && fy_token_atom_style(fyt) == FYAS_PLAIN) {
fy_emit_write(emit, wtype, str, len);
goto out;
}
if (!atom)
goto out;
allow_breaks = !(flags & DDNF_SIMPLE) && !fy_emit_is_json_mode(emit) && !fy_emit_is_oneline(emit);
spaces = false;
breaks = false;
fy_atom_iter_start(atom, &iter);
fy_emit_accum_start(&emit->ea, emit->column, fy_token_atom_lb_mode(fyt));
while ((c = fy_atom_iter_utf8_get(&iter)) > 0) {
if (fy_is_ws(c)) {
should_indent = allow_breaks && !spaces &&
fy_emit_accum_column(&emit->ea) > fy_emit_width(emit);
if (should_indent && !fy_is_ws(fy_atom_iter_utf8_peek(&iter))) {
fy_emit_output_accum(emit, wtype, &emit->ea);
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
} else
fy_emit_accum_utf8_put(&emit->ea, c);
spaces = true;
} else if (fy_is_lb_m(c, fy_token_atom_lb_mode(fyt))) {
/* blergh */
if (!allow_breaks)
break;
/* output run */
if (!breaks) {
fy_emit_output_accum(emit, wtype, &emit->ea);
fy_emit_write_indent(emit, indent);
}
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
breaks = true;
} else {
if (breaks)
fy_emit_write_indent(emit, indent);
fy_emit_accum_utf8_put(&emit->ea, c);
emit->flags &= ~FYEF_INDENTATION;
spaces = false;
breaks = false;
}
}
fy_emit_output_accum(emit, wtype, &emit->ea);
fy_emit_accum_finish(&emit->ea);
fy_atom_iter_finish(&iter);
out:
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION);
}
void fy_emit_token_write_alias(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent)
{
const char *str = NULL;
size_t len = 0;
struct fy_atom_iter iter;
int c;
if (!fyt)
return;
fy_emit_write_indicator(emit, di_star, flags, indent, fyewt_alias);
/* try direct output first (99% of cases) */
str = fy_token_get_direct_output(fyt, &len);
if (str) {
fy_emit_write(emit, fyewt_alias, str, len);
return;
}
/* corner case, use iterator */
fy_atom_iter_start(fy_token_atom(fyt), &iter);
fy_emit_accum_start(&emit->ea, emit->column, fy_token_atom_lb_mode(fyt));
while ((c = fy_atom_iter_utf8_get(&iter)) > 0)
fy_emit_accum_utf8_put(&emit->ea, c);
fy_emit_output_accum(emit, fyewt_alias, &emit->ea);
fy_emit_accum_finish(&emit->ea);
fy_atom_iter_finish(&iter);
}
void fy_emit_token_write_quoted(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent, char qc)
{
bool allow_breaks, spaces, breaks;
int c, i, w, digit;
enum fy_emitter_write_type wtype;
const char *str = NULL;
size_t len = 0;
bool should_indent, done_esc;
struct fy_atom *atom;
struct fy_atom_iter iter;
enum fy_atom_style target_style;
uint32_t hi_surrogate, lo_surrogate;
uint8_t non_utf8[4];
size_t non_utf8_len, k;
wtype = qc == '\'' ?
((flags & DDNF_SIMPLE_SCALAR_KEY) ?
fyewt_single_quoted_scalar_key : fyewt_single_quoted_scalar) :
((flags & DDNF_SIMPLE_SCALAR_KEY) ?
fyewt_double_quoted_scalar_key : fyewt_double_quoted_scalar);
fy_emit_write_indicator(emit,
qc == '\'' ? di_single_quote_start : di_double_quote_start,
flags, indent, wtype);
/* XXX check whether this is ever the case */
if (!fyt)
goto out;
/* note that if the original target style and the target differ
* we can note use direct output
*/
target_style = qc == '"' ? FYAS_DOUBLE_QUOTED : FYAS_SINGLE_QUOTED;
/* simple case of direct output (large amount of cases) */
str = fy_token_get_direct_output(fyt, &len);
if (str && fy_token_atom_style(fyt) == target_style) {
fy_emit_write(emit, wtype, str, len);
goto out;
}
/* no atom? i.e. empty */
atom = fy_token_atom(fyt);
if (!atom)
goto out;
allow_breaks = !(flags & DDNF_SIMPLE) && !fy_emit_is_json_mode(emit) && !fy_emit_is_oneline(emit);
spaces = false;
breaks = false;
fy_atom_iter_start(atom, &iter);
fy_emit_accum_start(&emit->ea, emit->column, fy_token_atom_lb_mode(fyt));
for (;;) {
non_utf8_len = sizeof(non_utf8);
c = fy_atom_iter_utf8_quoted_get(&iter, &non_utf8_len, non_utf8);
if (c < 0)
break;
if (c == 0 && non_utf8_len > 0) {
for (k = 0; k < non_utf8_len; k++) {
c = (int)non_utf8[k] & 0xff;
fy_emit_accum_utf8_put(&emit->ea, '\\');
fy_emit_accum_utf8_put(&emit->ea, 'x');
digit = ((unsigned int)c >> 4) & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
digit = (unsigned int)c & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
}
continue;
}
if (fy_is_space(c) || (qc == '\'' && fy_is_ws(c))) {
should_indent = allow_breaks && !spaces &&
fy_emit_accum_column(&emit->ea) > fy_emit_width(emit);
if (should_indent &&
((qc == '\'' && fy_is_ws(fy_atom_iter_utf8_peek(&iter))) ||
qc == '"')) {
fy_emit_output_accum(emit, wtype, &emit->ea);
if (qc == '"' && fy_is_ws(fy_atom_iter_utf8_peek(&iter)))
fy_emit_putc(emit, wtype, '\\');
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
} else
fy_emit_accum_utf8_put(&emit->ea, c);
spaces = true;
breaks = false;
} else if (qc == '\'' && fy_is_lb_m(c, fy_token_atom_lb_mode(fyt))) {
/* blergh */
if (!allow_breaks)
break;
/* output run */
if (!breaks) {
fy_emit_output_accum(emit, wtype, &emit->ea);
fy_emit_write_indent(emit, indent);
}
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
breaks = true;
} else {
/* output run */
if (breaks) {
fy_emit_output_accum(emit, wtype, &emit->ea);
fy_emit_write_indent(emit, indent);
}
/* escape */
if (qc == '\'' && c == '\'') {
fy_emit_accum_utf8_put(&emit->ea, '\'');
fy_emit_accum_utf8_put(&emit->ea, '\'');
} else if (qc == '"' &&
((!fy_is_printq(c) || c == '"' || c == '\\') ||
(fy_emit_is_json_mode(emit) && !fy_is_json_unescaped(c)))) {
fy_emit_accum_utf8_put(&emit->ea, '\\');
/* common YAML and JSON escapes */
done_esc = false;
switch (c) {
case '\b':
fy_emit_accum_utf8_put(&emit->ea, 'b');
done_esc = true;
break;
case '\f':
fy_emit_accum_utf8_put(&emit->ea, 'f');
done_esc = true;
break;
case '\n':
fy_emit_accum_utf8_put(&emit->ea, 'n');
done_esc = true;
break;
case '\r':
fy_emit_accum_utf8_put(&emit->ea, 'r');
done_esc = true;
break;
case '\t':
fy_emit_accum_utf8_put(&emit->ea, 't');
done_esc = true;
break;
case '"':
fy_emit_accum_utf8_put(&emit->ea, '"');
done_esc = true;
break;
case '\\':
fy_emit_accum_utf8_put(&emit->ea, '\\');
done_esc = true;
break;
}
if (done_esc)
goto done;
if (!fy_emit_is_json_mode(emit)) {
switch (c) {
case '\0':
fy_emit_accum_utf8_put(&emit->ea, '0');
break;
case '\a':
fy_emit_accum_utf8_put(&emit->ea, 'a');
break;
case '\v':
fy_emit_accum_utf8_put(&emit->ea, 'v');
break;
case '\e':
fy_emit_accum_utf8_put(&emit->ea, 'e');
break;
case 0x85:
fy_emit_accum_utf8_put(&emit->ea, 'N');
break;
case 0xa0:
fy_emit_accum_utf8_put(&emit->ea, '_');
break;
case 0x2028:
fy_emit_accum_utf8_put(&emit->ea, 'L');
break;
case 0x2029:
fy_emit_accum_utf8_put(&emit->ea, 'P');
break;
default:
if ((unsigned int)c <= 0xff) {
fy_emit_accum_utf8_put(&emit->ea, 'x');
w = 2;
} else if ((unsigned int)c <= 0xffff) {
fy_emit_accum_utf8_put(&emit->ea, 'u');
w = 4;
} else if ((unsigned int)c <= 0xffffffff) {
fy_emit_accum_utf8_put(&emit->ea, 'U');
w = 8;
}
for (i = w - 1; i >= 0; i--) {
digit = ((unsigned int)c >> (i * 4)) & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
}
break;
}
} else {
/* JSON escapes all others in \uXXXX and \uXXXX\uXXXX */
w = 4;
if ((unsigned int)c <= 0xffff) {
fy_emit_accum_utf8_put(&emit->ea, 'u');
for (i = w - 1; i >= 0; i--) {
digit = ((unsigned int)c >> (i * 4)) & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
}
} else {
hi_surrogate = 0xd800 | ((((c >> 16) & 0x1f) - 1) << 6) | ((c >> 10) & 0x3f);
lo_surrogate = 0xdc00 | (c & 0x3ff);
fy_emit_accum_utf8_put(&emit->ea, 'u');
for (i = w - 1; i >= 0; i--) {
digit = ((unsigned int)hi_surrogate >> (i * 4)) & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
}
fy_emit_accum_utf8_put(&emit->ea, '\\');
fy_emit_accum_utf8_put(&emit->ea, 'u');
for (i = w - 1; i >= 0; i--) {
digit = ((unsigned int)lo_surrogate >> (i * 4)) & 15;
fy_emit_accum_utf8_put(&emit->ea,
digit <= 9 ? ('0' + digit) : ('A' + digit - 10));
}
}
}
} else
fy_emit_accum_utf8_put(&emit->ea, c);
done:
emit->flags &= ~FYEF_INDENTATION;
spaces = false;
breaks = false;
}
}
fy_emit_output_accum(emit, wtype, &emit->ea);
fy_emit_accum_finish(&emit->ea);
fy_atom_iter_finish(&iter);
out:
fy_emit_write_indicator(emit,
qc == '\'' ? di_single_quote_end : di_double_quote_end,
flags, indent, wtype);
}
bool fy_emit_token_write_block_hints(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent, char *chompp)
{
char chomp = '\0';
bool explicit_chomp = false;
struct fy_atom *atom;
atom = fy_token_atom(fyt);
if (!atom) {
emit->flags &= ~FYEF_OPEN_ENDED;
chomp = '-';
goto out;
}
if (atom->starts_with_ws || atom->starts_with_lb) {
fy_emit_putc(emit, fyewt_indicator, '0' + fy_emit_indent(emit));
explicit_chomp = true;
}
if (!atom->ends_with_lb) {
emit->flags &= ~FYEF_OPEN_ENDED;
chomp = '-';
goto out;
}
if (atom->trailing_lb) {
emit->flags |= FYEF_OPEN_ENDED;
chomp = '+';
goto out;
}
emit->flags &= ~FYEF_OPEN_ENDED;
out:
if (chomp)
fy_emit_putc(emit, fyewt_indicator, chomp);
*chompp = chomp;
return explicit_chomp;
}
void fy_emit_token_write_literal(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent)
{
bool breaks;
int c;
char chomp;
struct fy_atom *atom;
struct fy_atom_iter iter;
fy_emit_write_indicator(emit, di_bar, flags, indent, fyewt_indicator);
fy_emit_token_write_block_hints(emit, fyt, flags, indent, &chomp);
if (flags & DDNF_ROOT)
indent += fy_emit_indent(emit);
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags |= FYEF_WHITESPACE | FYEF_INDENTATION;
atom = fy_token_atom(fyt);
if (!atom)
goto out;
breaks = true;
fy_atom_iter_start(atom, &iter);
fy_emit_accum_start(&emit->ea, emit->column, fy_token_atom_lb_mode(fyt));
while ((c = fy_atom_iter_utf8_get(&iter)) > 0) {
if (breaks) {
fy_emit_write_indent(emit, indent);
breaks = false;
}
if (fy_is_lb_m(c, fy_token_atom_lb_mode(fyt))) {
fy_emit_output_accum(emit, fyewt_literal_scalar, &emit->ea);
emit->flags &= ~FYEF_INDENTATION;
breaks = true;
} else
fy_emit_accum_utf8_put(&emit->ea, c);
}
fy_emit_output_accum(emit, fyewt_literal_scalar, &emit->ea);
fy_emit_accum_finish(&emit->ea);
fy_atom_iter_finish(&iter);
out:
emit->flags &= ~FYEF_INDENTATION;
}
void fy_emit_token_write_folded(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent)
{
bool leading_spaces, breaks;
int c, nrbreaks, nrbreakslim;
char chomp;
struct fy_atom *atom;
struct fy_atom_iter iter;
fy_emit_write_indicator(emit, di_greater, flags, indent, fyewt_indicator);
fy_emit_token_write_block_hints(emit, fyt, flags, indent, &chomp);
if (flags & DDNF_ROOT)
indent += fy_emit_indent(emit);
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags |= FYEF_WHITESPACE | FYEF_INDENTATION;
atom = fy_token_atom(fyt);
if (!atom)
return;
breaks = true;
leading_spaces = true;
fy_atom_iter_start(atom, &iter);
fy_emit_accum_start(&emit->ea, emit->column, fy_token_atom_lb_mode(fyt));
while ((c = fy_atom_iter_utf8_get(&iter)) > 0) {
if (fy_is_lb_m(c, fy_token_atom_lb_mode(fyt))) {
/* output run */
if (!fy_emit_accum_empty(&emit->ea)) {
fy_emit_output_accum(emit, fyewt_literal_scalar, &emit->ea);
/* do not output a newline (indent) if at the end or
* this is a leading spaces line */
if (!fy_is_z(fy_atom_iter_utf8_peek(&iter)) && !leading_spaces)
fy_emit_write_indent(emit, indent);
}
/* count the number of consecutive breaks */
nrbreaks = 1;
while (fy_is_lb_m((c = fy_atom_iter_utf8_peek(&iter)), fy_token_atom_lb_mode(fyt))) {
nrbreaks++;
(void)fy_atom_iter_utf8_get(&iter);
}
/* NOTE: Because the number of indents is tricky
* if it's a non blank, non end, it's the number of breaks
* if it's a blank, it's the number of breaks minus 1
* if it's the end, it's the number of breaks minus 2
*/
nrbreakslim = fy_is_z(c) ? 2 : fy_is_blank(c) ? 1 : 0;
while (nrbreaks-- > nrbreakslim) {
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
}
breaks = true;
} else {
/* if we had a break, output an indent */
if (breaks) {
fy_emit_write_indent(emit, indent);
/* if this line starts with whitespace we need to know */
leading_spaces = fy_is_ws(c);
}
if (!breaks && fy_is_space(c) &&
!fy_is_space(fy_atom_iter_utf8_peek(&iter)) &&
fy_emit_accum_column(&emit->ea) > fy_emit_width(emit)) {
fy_emit_output_accum(emit, fyewt_folded_scalar, &emit->ea);
emit->flags &= ~FYEF_INDENTATION;
fy_emit_write_indent(emit, indent);
} else
fy_emit_accum_utf8_put(&emit->ea, c);
breaks = false;
}
}
fy_emit_output_accum(emit, fyewt_folded_scalar, &emit->ea);
fy_emit_accum_finish(&emit->ea);
fy_atom_iter_finish(&iter);
}
static enum fy_node_style
fy_emit_token_scalar_style(struct fy_emitter *emit, struct fy_token *fyt,
int flags, int indent, enum fy_node_style style,
struct fy_token *fyt_tag)
{
const char *value = NULL;
size_t len = 0;
bool json, flow, is_null_scalar, is_json_plain;
struct fy_atom *atom;
int aflags = -1;
const char *tag;
size_t tag_len;
atom = fy_token_atom(fyt);
flow = fy_emit_is_flow_mode(emit) || (flags & DDNF_FLOW);
/* check if style is allowed (i.e. no block styles in flow context) */
if (flow && (style == FYNS_LITERAL || style == FYNS_FOLDED))
style = FYNS_ANY;
json = fy_emit_is_json_mode(emit);
is_null_scalar = !atom || fyt->scalar.is_null;
/* is this a plain json atom? */
is_json_plain = (json || emit->source_json || fy_emit_is_dejson_mode(emit)) &&
(is_null_scalar ||
!fy_atom_strcmp(atom, "false") ||
!fy_atom_strcmp(atom, "true") ||
!fy_atom_strcmp(atom, "null") ||
fy_atom_is_number(atom));
if (json) {
/* literal in JSON mode is output as quoted */
if (style == FYNS_LITERAL || style == FYNS_FOLDED)
return FYNS_DOUBLE_QUOTED;
if (is_json_plain) {
tag = fy_token_get_text(fyt_tag, &tag_len);
/* XXX hardcoded string tag resultion */
if (tag && tag_len &&
((tag_len == 1 && *tag == '!') ||
(tag_len == 21 && !memcmp(tag, "tag:yaml.org,2002:str", 21))))
return FYNS_DOUBLE_QUOTED;
}
/* JSON NULL, but with plain style */
if ((style == FYNS_PLAIN || style == FYNS_ANY) &&
(is_null_scalar || (is_json_plain && !atom->size0)))
return FYNS_PLAIN;
return FYNS_DOUBLE_QUOTED;
}
aflags = fy_token_text_analyze(fyt);
if (flow && (style == FYNS_ANY || style == FYNS_LITERAL || style == FYNS_FOLDED)) {
if (fyt && !value)
value = fy_token_get_text(fyt, &len);
/* if there's a linebreak, use double quoted style */
if (fy_find_any_lb(value, len)) {
style = FYNS_DOUBLE_QUOTED;
goto out;
}
/* check if there's a non printable */
if (!fy_find_non_print(value, len)) {
style = FYNS_SINGLE_QUOTED;
goto out;
}
/* anything not empty is double quoted here */
style = !(aflags & FYTTAF_EMPTY) ? FYNS_PLAIN : FYNS_DOUBLE_QUOTED;
}
/* try to pretify */
if (!flow && fy_emit_is_pretty_mode(emit) &&
(style == FYNS_ANY || style == FYNS_DOUBLE_QUOTED || style == FYNS_SINGLE_QUOTED)) {
/* any original style can be a plain, but contains linebreaks, do a literal */
if ((aflags & (FYTTAF_CAN_BE_PLAIN | FYTTAF_HAS_LB)) == (FYTTAF_CAN_BE_PLAIN | FYTTAF_HAS_LB)) {
style = FYNS_LITERAL;
goto out;
}
/* any style, can be just a plain, just make it so */
if (style == FYNS_ANY && (aflags & (FYTTAF_CAN_BE_PLAIN | FYTTAF_HAS_LB)) == FYTTAF_CAN_BE_PLAIN) {
style = FYNS_PLAIN;
goto out;
}
}
if (!flow && emit->source_json && fy_emit_is_dejson_mode(emit)) {
if (is_json_plain || (aflags & (FYTTAF_CAN_BE_PLAIN | FYTTAF_HAS_LB)) == FYTTAF_CAN_BE_PLAIN) {
style = FYNS_PLAIN;
goto out;
}
}
out:
if (style == FYNS_ANY) {
if (fyt)
value = fy_token_get_text(fyt, &len);
style = (aflags & FYTTAF_CAN_BE_PLAIN) ?
FYNS_PLAIN : FYNS_DOUBLE_QUOTED;
}
if (style == FYNS_PLAIN) {
/* plains in flow mode not being able to be plains
* - plain in block mode that can't be plain in flow mode
* - special handling for plains on start of line
*/
if ((flow && !(aflags & FYTTAF_CAN_BE_PLAIN_FLOW) && !is_null_scalar) ||
((aflags & FYTTAF_QUOTE_AT_0) && indent == 0))
style = FYNS_DOUBLE_QUOTED;
}
return style;
}
void fy_emit_token_scalar(struct fy_emitter *emit, struct fy_token *fyt, int flags, int indent,
enum fy_node_style style, struct fy_token *fyt_tag)
{
assert(style != FYNS_FLOW && style != FYNS_BLOCK);
indent = fy_emit_increase_indent(emit, flags, indent);
if (!fy_emit_whitespace(emit))
fy_emit_write_ws(emit);
style = fy_emit_token_scalar_style(emit, fyt, flags, indent, style, fyt_tag);
switch (style) {
case FYNS_ALIAS:
fy_emit_token_write_alias(emit, fyt, flags, indent);
break;
case FYNS_PLAIN:
fy_emit_token_write_plain(emit, fyt, flags, indent);
break;
case FYNS_DOUBLE_QUOTED:
fy_emit_token_write_quoted(emit, fyt, flags, indent, '"');
break;
case FYNS_SINGLE_QUOTED:
fy_emit_token_write_quoted(emit, fyt, flags, indent, '\'');
break;
case FYNS_LITERAL:
fy_emit_token_write_literal(emit, fyt, flags, indent);
break;
case FYNS_FOLDED:
fy_emit_token_write_folded(emit, fyt, flags, indent);
break;
default:
break;
}
}
void fy_emit_scalar(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent, bool is_key)
{
enum fy_node_style style;
/* default style */
style = fyn ? fyn->style : FYNS_ANY;
/* all JSON keys are double quoted */
if (fy_emit_is_json_mode(emit) && is_key)
style = FYNS_DOUBLE_QUOTED;
fy_emit_token_scalar(emit,
fyn ? fyn->scalar : NULL,
flags, indent,
style, fyn->tag);
}
static void fy_emit_sequence_prolog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
bool json = fy_emit_is_json_mode(emit);
bool oneline = fy_emit_is_oneline(emit);
bool was_flow = sc->flow;
sc->old_indent = sc->indent;
if (!json) {
if (fy_emit_is_manual(emit))
sc->flow = (sc->xstyle == FYNS_BLOCK && was_flow) || sc->xstyle == FYNS_FLOW;
else if (fy_emit_is_block_mode(emit))
sc->flow = sc->empty;
else
sc->flow = fy_emit_is_flow_mode(emit) || emit->flow_level || sc->flow_token || sc->empty;
if (sc->flow) {
if (!emit->flow_level) {
sc->indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
sc->old_indent = sc->indent;
}
sc->flags = (sc->flags | DDNF_FLOW) | (sc->flags & ~DDNF_INDENTLESS);
fy_emit_write_indicator(emit, di_left_bracket, sc->flags, sc->indent, fyewt_indicator);
} else {
sc->flags = (sc->flags & ~DDNF_FLOW);
}
} else {
sc->flags = (sc->flags | DDNF_FLOW) | (sc->flags & ~DDNF_INDENTLESS);
fy_emit_write_indicator(emit, di_left_bracket, sc->flags, sc->indent, fyewt_indicator);
}
if (!oneline) {
if (was_flow || (sc->flags & (DDNF_ROOT | DDNF_SEQ)))
sc->indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
}
sc->flags &= ~DDNF_ROOT;
}
static void fy_emit_sequence_epilog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
if (sc->flow || fy_emit_is_json_mode(emit)) {
if (!fy_emit_is_oneline(emit) && !sc->empty)
fy_emit_write_indent(emit, sc->old_indent);
fy_emit_write_indicator(emit, di_right_bracket, sc->flags, sc->old_indent, fyewt_indicator);
}
}
static void fy_emit_sequence_item_prolog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
struct fy_token *fyt_value)
{
int tmp_indent;
sc->flags |= DDNF_SEQ;
if (!fy_emit_is_oneline(emit))
fy_emit_write_indent(emit, sc->indent);
if (!sc->flow && !fy_emit_is_json_mode(emit))
fy_emit_write_indicator(emit, di_dash, sc->flags, sc->indent, fyewt_indicator);
tmp_indent = sc->indent;
if (fy_emit_token_has_comment(emit, fyt_value, fycp_top)) {
if (!sc->flow && !fy_emit_is_json_mode(emit))
tmp_indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
fy_emit_token_comment(emit, fyt_value, sc->flags, tmp_indent, fycp_top);
}
}
static void fy_emit_sequence_item_epilog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
bool last, struct fy_token *fyt_value)
{
if ((sc->flow || fy_emit_is_json_mode(emit)) && !last)
fy_emit_write_indicator(emit, di_comma, sc->flags, sc->indent, fyewt_indicator);
fy_emit_token_comment(emit, fyt_value, sc->flags, sc->indent, fycp_right);
if (last && (sc->flow || fy_emit_is_json_mode(emit)) && !fy_emit_is_oneline(emit) && !sc->empty)
fy_emit_write_indent(emit, sc->old_indent);
sc->flags &= ~DDNF_SEQ;
}
void fy_emit_sequence(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent)
{
struct fy_node *fyni, *fynin;
struct fy_token *fyt_value;
bool last;
struct fy_emit_save_ctx sct, *sc = &sct;
memset(sc, 0, sizeof(*sc));
sc->flags = flags;
sc->indent = indent;
sc->empty = fy_node_list_empty(&fyn->sequence);
sc->flow_token = fyn->style == FYNS_FLOW;
sc->flow = !!(flags & DDNF_FLOW);
sc->xstyle = fyn->style;
sc->old_indent = sc->indent;
fy_emit_sequence_prolog(emit, sc);
for (fyni = fy_node_list_head(&fyn->sequence); fyni; fyni = fynin) {
fynin = fy_node_next(&fyn->sequence, fyni);
last = !fynin;
fyt_value = fy_node_value_token(fyni);
fy_emit_sequence_item_prolog(emit, sc, fyt_value);
fy_emit_node_internal(emit, fyni, (sc->flags & ~DDNF_ROOT), sc->indent, false);
fy_emit_sequence_item_epilog(emit, sc, last, fyt_value);
}
fy_emit_sequence_epilog(emit, sc);
}
static void fy_emit_mapping_prolog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
bool json = fy_emit_is_json_mode(emit);
bool oneline = fy_emit_is_oneline(emit);
bool was_flow = sc->flow;
sc->old_indent = sc->indent;
if (!json) {
if (fy_emit_is_manual(emit))
sc->flow = (sc->xstyle == FYNS_BLOCK && was_flow) || sc->xstyle == FYNS_FLOW;
else if (fy_emit_is_block_mode(emit))
sc->flow = sc->empty;
else
sc->flow = fy_emit_is_flow_mode(emit) || emit->flow_level || sc->flow_token || sc->empty;
if (sc->flow) {
if (!emit->flow_level) {
sc->indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
sc->old_indent = sc->indent;
}
sc->flags = (sc->flags | DDNF_FLOW) | (sc->flags & ~DDNF_INDENTLESS);
fy_emit_write_indicator(emit, di_left_brace, sc->flags, sc->indent, fyewt_indicator);
} else {
sc->flags &= ~(DDNF_FLOW | DDNF_INDENTLESS);
}
} else {
sc->flags = (sc->flags | DDNF_FLOW) | (sc->flags & ~DDNF_INDENTLESS);
fy_emit_write_indicator(emit, di_left_brace, sc->flags, sc->indent, fyewt_indicator);
}
if (!oneline && !sc->empty)
sc->indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
sc->flags &= ~DDNF_ROOT;
}
static void fy_emit_mapping_epilog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
if (sc->flow || fy_emit_is_json_mode(emit)) {
if (!fy_emit_is_oneline(emit) && !sc->empty)
fy_emit_write_indent(emit, sc->old_indent);
fy_emit_write_indicator(emit, di_right_brace, sc->flags, sc->old_indent, fyewt_indicator);
}
}
static void fy_emit_mapping_key_prolog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
struct fy_token *fyt_key, bool simple_key)
{
sc->flags = DDNF_MAP | (sc->flags & DDNF_FLOW);
if (simple_key) {
sc->flags |= DDNF_SIMPLE;
if (fyt_key && fyt_key->type == FYTT_SCALAR)
sc->flags |= DDNF_SIMPLE_SCALAR_KEY;
} else {
/* do not emit the ? in flow modes at all */
if (fy_emit_is_flow_mode(emit))
sc->flags |= DDNF_SIMPLE;
}
if (!fy_emit_is_oneline(emit))
fy_emit_write_indent(emit, sc->indent);
/* complex? */
if (!(sc->flags & DDNF_SIMPLE))
fy_emit_write_indicator(emit, di_question_mark, sc->flags, sc->indent, fyewt_indicator);
}
static void fy_emit_mapping_key_epilog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
struct fy_token *fyt_key)
{
int tmp_indent;
/* if the key is an alias, always output an extra whitespace */
if (fyt_key && fyt_key->type == FYTT_ALIAS)
fy_emit_write_ws(emit);
sc->flags &= ~DDNF_MAP;
fy_emit_write_indicator(emit, di_colon, sc->flags, sc->indent, fyewt_indicator);
tmp_indent = sc->indent;
if (fy_emit_token_has_comment(emit, fyt_key, fycp_right)) {
if (!sc->flow && !fy_emit_is_json_mode(emit))
tmp_indent = fy_emit_increase_indent(emit, sc->flags, sc->indent);
fy_emit_token_comment(emit, fyt_key, sc->flags, tmp_indent, fycp_right);
fy_emit_write_indent(emit, tmp_indent);
}
sc->flags = DDNF_MAP | (sc->flags & DDNF_FLOW);
}
static void fy_emit_mapping_value_prolog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
struct fy_token *fyt_value)
{
/* nothing */
}
static void fy_emit_mapping_value_epilog(struct fy_emitter *emit, struct fy_emit_save_ctx *sc,
bool last, struct fy_token *fyt_value)
{
if ((sc->flow || fy_emit_is_json_mode(emit)) && !last)
fy_emit_write_indicator(emit, di_comma, sc->flags, sc->indent, fyewt_indicator);
fy_emit_token_comment(emit, fyt_value, sc->flags, sc->indent, fycp_right);
if (last && (sc->flow || fy_emit_is_json_mode(emit)) && !fy_emit_is_oneline(emit) && !sc->empty)
fy_emit_write_indent(emit, sc->old_indent);
sc->flags &= ~DDNF_MAP;
}
void fy_emit_mapping(struct fy_emitter *emit, struct fy_node *fyn, int flags, int indent)
{
struct fy_node_pair *fynp, *fynpn, **fynpp = NULL;
struct fy_token *fyt_key, *fyt_value;
bool last, simple_key, used_malloc = false;
int aflags, i, count;
struct fy_emit_save_ctx sct, *sc = &sct;
memset(sc, 0, sizeof(*sc));
sc->flags = flags;
sc->indent = indent;
sc->empty = fy_node_pair_list_empty(&fyn->mapping);
sc->flow_token = fyn->style == FYNS_FLOW;
sc->flow = !!(flags & DDNF_FLOW);
sc->xstyle = fyn->style;
sc->old_indent = sc->indent;
fy_emit_mapping_prolog(emit, sc);
if (!(emit->cfg.flags & (FYECF_SORT_KEYS | FYECF_STRIP_EMPTY_KV))) {
fynp = fy_node_pair_list_head(&fyn->mapping);
fynpp = NULL;
} else {
count = fy_node_mapping_item_count(fyn);
/* heuristic, avoid allocation for small maps */
if (count > 64) {
fynpp = malloc((count + 1) * sizeof(*fynpp));
fyd_error_check(fyn->fyd, fynpp, err_out,
"malloc() failed");
used_malloc = true;
} else
fynpp = FY_ALLOCA((count + 1) * sizeof(*fynpp));
/* fill (removing empty KVs) */
i = 0;
for (fynp = fy_node_pair_list_head(&fyn->mapping); fynp;
fynp = fy_node_pair_next(&fyn->mapping, fynp)) {
/* strip key/value pair from the output if it's empty */
if ((emit->cfg.flags & FYECF_STRIP_EMPTY_KV) && fy_node_is_empty(fynp->value))
continue;
fynpp[i++] = fynp;
}
count = i;
fynpp[count] = NULL;
/* sort the keys */
if (emit->cfg.flags & FYECF_SORT_KEYS)
fy_node_mapping_perform_sort(fyn, NULL, NULL, fynpp, count);
i = 0;
fynp = fynpp[i];
}
for (; fynp; fynp = fynpn) {
if (!fynpp)
fynpn = fy_node_pair_next(&fyn->mapping, fynp);
else
fynpn = fynpp[++i];
last = !fynpn;
fyt_key = fy_node_value_token(fynp->key);
fyt_value = fy_node_value_token(fynp->value);
FYD_NODE_ERROR_CHECK(fynp->fyd, fynp->key, FYEM_INTERNAL,
!fy_emit_is_json_mode(emit) ||
(fynp->key && fynp->key->type == FYNT_SCALAR),
err_out, "Non scalar keys are not allowed in JSON emit mode");
simple_key = false;
if (fynp->key) {
switch (fynp->key->type) {
case FYNT_SCALAR:
aflags = fy_token_text_analyze(fynp->key->scalar);
simple_key = fy_emit_is_json_mode(emit) ||
!!(aflags & FYTTAF_CAN_BE_SIMPLE_KEY);
break;
case FYNT_SEQUENCE:
simple_key = fy_node_list_empty(&fynp->key->sequence);
break;
case FYNT_MAPPING:
simple_key = fy_node_pair_list_empty(&fynp->key->mapping);
break;
}
}
fy_emit_mapping_key_prolog(emit, sc, fyt_key, simple_key);
if (fynp->key)
fy_emit_node_internal(emit, fynp->key, (sc->flags & ~DDNF_ROOT), sc->indent, true);
fy_emit_mapping_key_epilog(emit, sc, fyt_key);
fy_emit_mapping_value_prolog(emit, sc, fyt_value);
if (fynp->value)
fy_emit_node_internal(emit, fynp->value, (sc->flags & ~DDNF_ROOT), sc->indent, false);
fy_emit_mapping_value_epilog(emit, sc, last, fyt_value);
}
if (fynpp && used_malloc)
free(fynpp);
fy_emit_mapping_epilog(emit, sc);
err_out:
return;
}
int fy_emit_common_document_start(struct fy_emitter *emit,
struct fy_document_state *fyds,
bool root_tag_or_anchor)
{
struct fy_token *fyt_chk;
const char *td_handle, *td_prefix;
size_t td_handle_size, td_prefix_size;
enum fy_emitter_cfg_flags flags = emit->cfg.flags;
enum fy_emitter_cfg_flags vd_flags = flags & FYECF_VERSION_DIR(FYECF_VERSION_DIR_MASK);
enum fy_emitter_cfg_flags td_flags = flags & FYECF_TAG_DIR(FYECF_TAG_DIR_MASK);
enum fy_emitter_cfg_flags dsm_flags = flags & FYECF_DOC_START_MARK(FYECF_DOC_START_MARK_MASK);
bool vd, td, dsm;
bool had_non_default_tag = false;
if (!emit || !fyds || emit->fyds)
return -1;
emit->fyds = fyds;
vd = ((vd_flags == FYECF_VERSION_DIR_AUTO && fyds->version_explicit) ||
vd_flags == FYECF_VERSION_DIR_ON) &&
!(emit->cfg.flags & FYECF_STRIP_DOC);
td = ((td_flags == FYECF_TAG_DIR_AUTO && fyds->tags_explicit) ||
td_flags == FYECF_TAG_DIR_ON) &&
!(emit->cfg.flags & FYECF_STRIP_DOC);
/* if either a version or directive tags exist, and no previous
* explicit document end existed, output one now
*/
if (!fy_emit_is_json_mode(emit) && (vd || td) && !(emit->flags & FYEF_HAD_DOCUMENT_END)) {
if (emit->column)
fy_emit_putc(emit, fyewt_linebreak, '\n');
if (!(emit->cfg.flags & FYECF_STRIP_DOC)) {
fy_emit_puts(emit, fyewt_document_indicator, "...");
emit->flags &= ~FYEF_WHITESPACE;
emit->flags |= FYEF_HAD_DOCUMENT_END;
}
}
if (!fy_emit_is_json_mode(emit) && vd) {
if (emit->column)
fy_emit_putc(emit, fyewt_linebreak, '\n');
fy_emit_printf(emit, fyewt_version_directive, "%%YAML %d.%d",
fyds->version.major, fyds->version.minor);
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
if (!fy_emit_is_json_mode(emit) && td) {
for (fyt_chk = fy_token_list_first(&fyds->fyt_td); fyt_chk; fyt_chk = fy_token_next(&fyds->fyt_td, fyt_chk)) {
td_handle = fy_tag_directive_token_handle(fyt_chk, &td_handle_size);
td_prefix = fy_tag_directive_token_prefix(fyt_chk, &td_prefix_size);
assert(td_handle && td_prefix);
if (fy_tag_is_default_internal(td_handle, td_handle_size, td_prefix, td_prefix_size))
continue;
had_non_default_tag = true;
if (emit->column)
fy_emit_putc(emit, fyewt_linebreak, '\n');
fy_emit_printf(emit, fyewt_tag_directive, "%%TAG %.*s %.*s",
(int)td_handle_size, td_handle,
(int)td_prefix_size, td_prefix);
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
}
/* always output document start indicator:
* - was explicit
* - document has tags
* - document has an explicit version
* - root exists & has a tag or an anchor
*/
dsm = (dsm_flags == FYECF_DOC_START_MARK_AUTO &&
(!fyds->start_implicit ||
fyds->tags_explicit || fyds->version_explicit ||
had_non_default_tag)) ||
dsm_flags == FYECF_DOC_START_MARK_ON;
/* if there was previous output without document end */
if (!dsm && (emit->flags & FYEF_HAD_DOCUMENT_OUTPUT) &&
!(emit->flags & FYEF_HAD_DOCUMENT_END))
dsm = true;
/* output document start indicator if we should */
if (dsm)
fy_emit_document_start_indicator(emit);
/* clear that in any case */
emit->flags &= ~FYEF_HAD_DOCUMENT_END;
return 0;
}
int fy_emit_document_start(struct fy_emitter *emit, struct fy_document *fyd,
struct fy_node *fyn_root)
{
struct fy_node *root;
bool root_tag_or_anchor;
int ret;
if (!emit || !fyd || !fyd->fyds)
return -1;
root = fyn_root ? fyn_root : fy_document_root(fyd);
root_tag_or_anchor = root && (root->tag || fy_document_lookup_anchor_by_node(fyd, root));
ret = fy_emit_common_document_start(emit, fyd->fyds, root_tag_or_anchor);
if (ret)
return ret;
emit->fyd = fyd;
return 0;
}
int fy_emit_common_document_end(struct fy_emitter *emit, bool override_state, bool implicit_override)
{
const struct fy_document_state *fyds;
enum fy_emitter_cfg_flags flags = emit->cfg.flags;
enum fy_emitter_cfg_flags dem_flags = flags & FYECF_DOC_END_MARK(FYECF_DOC_END_MARK_MASK);
bool implicit, dem;
if (!emit || !emit->fyds)
return -1;
fyds = emit->fyds;
implicit = fyds->end_implicit;
if (override_state)
implicit = implicit_override;
dem = ((dem_flags == FYECF_DOC_END_MARK_AUTO && !implicit) ||
dem_flags == FYECF_DOC_END_MARK_ON) &&
!(emit->cfg.flags & FYECF_STRIP_DOC);
if (!(emit->cfg.flags & FYECF_NO_ENDING_NEWLINE)) {
if (emit->column != 0) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
if (!fy_emit_is_json_mode(emit) && dem) {
fy_emit_puts(emit, fyewt_document_indicator, "...");
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
emit->flags |= FYEF_HAD_DOCUMENT_END;
} else
emit->flags &= ~FYEF_HAD_DOCUMENT_END;
} else {
if (!fy_emit_is_json_mode(emit) && dem) {
if (emit->column != 0) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
fy_emit_puts(emit, fyewt_document_indicator, "...");
emit->flags &= ~(FYEF_WHITESPACE | FYEF_INDENTATION);
emit->flags |= FYEF_HAD_DOCUMENT_END;
} else
emit->flags &= ~FYEF_HAD_DOCUMENT_END;
}
/* mark that we did output a document earlier */
emit->flags |= FYEF_HAD_DOCUMENT_OUTPUT;
/* stop our association with the document */
emit->fyds = NULL;
return 0;
}
int fy_emit_document_end(struct fy_emitter *emit)
{
int ret;
ret = fy_emit_common_document_end(emit, false, false);
if (ret)
return ret;
emit->fyd = NULL;
return 0;
}
int fy_emit_common_explicit_document_end(struct fy_emitter *emit)
{
if (!emit)
return -1;
if (emit->column != 0) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
if (!fy_emit_is_json_mode(emit)) {
fy_emit_puts(emit, fyewt_document_indicator, "...");
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
emit->flags |= FYEF_HAD_DOCUMENT_END;
} else
emit->flags &= ~FYEF_HAD_DOCUMENT_END;
/* mark that we did output a document earlier */
emit->flags |= FYEF_HAD_DOCUMENT_OUTPUT;
/* stop our association with the document */
emit->fyds = NULL;
return 0;
}
int fy_emit_explicit_document_end(struct fy_emitter *emit)
{
int ret;
ret = fy_emit_common_explicit_document_end(emit);
if (ret)
return ret;
emit->fyd = NULL;
return 0;
}
void fy_emit_reset(struct fy_emitter *emit, bool reset_events)
{
struct fy_eventp *fyep;
emit->line = 0;
emit->column = 0;
emit->flow_level = 0;
emit->output_error = 0;
/* start as if there was a previous document with an explicit end */
/* this allows implicit documents start without an indicator */
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION | FYEF_HAD_DOCUMENT_END;
emit->state = FYES_NONE;
/* reset the accumulator */
fy_emit_accum_reset(&emit->ea);
/* streaming mode indent */
emit->s_indent = -1;
/* streaming mode flags */
emit->s_flags = DDNF_ROOT;
emit->state_stack_top = 0;
emit->sc_stack_top = 0;
/* and release any queued events */
if (reset_events) {
while ((fyep = fy_eventp_list_pop(&emit->queued_events)) != NULL)
fy_eventp_release(fyep);
}
}
int fy_emit_setup(struct fy_emitter *emit, const struct fy_emitter_cfg *cfg)
{
struct fy_diag *diag;
if (!cfg)
return -1;
memset(emit, 0, sizeof(*emit));
emit->cfg = *cfg;
if (!emit->cfg.output)
emit->cfg.output = fy_emitter_default_output;
diag = cfg->diag;
if (!diag) {
diag = fy_diag_create(NULL);
if (!diag)
return -1;
} else
fy_diag_ref(diag);
emit->diag = diag;
fy_emit_accum_init(&emit->ea, emit->ea_inplace_buf, sizeof(emit->ea_inplace_buf), 0, fylb_cr_nl);
fy_eventp_list_init(&emit->queued_events);
emit->state_stack = emit->state_stack_inplace;
emit->state_stack_alloc = sizeof(emit->state_stack_inplace)/sizeof(emit->state_stack_inplace[0]);
emit->sc_stack = emit->sc_stack_inplace;
emit->sc_stack_alloc = sizeof(emit->sc_stack_inplace)/sizeof(emit->sc_stack_inplace[0]);
fy_eventp_list_init(&emit->recycled_eventp);
fy_token_list_init(&emit->recycled_token);
/* suppress recycling if we must */
emit->suppress_recycling_force = getenv("FY_VALGRIND") && !getenv("FY_VALGRIND_RECYCLING");
emit->suppress_recycling = emit->suppress_recycling_force;
if (!emit->suppress_recycling) {
emit->recycled_eventp_list = &emit->recycled_eventp;
emit->recycled_token_list = &emit->recycled_token;
} else {
emit->recycled_eventp_list = NULL;
emit->recycled_token_list = NULL;
}
fy_emit_reset(emit, false);
return 0;
}
void fy_emit_cleanup(struct fy_emitter *emit)
{
struct fy_eventp *fyep;
struct fy_token *fyt;
/* call the finalizer if it exists */
if (emit->finalizer)
emit->finalizer(emit);
while ((fyt = fy_token_list_pop(&emit->recycled_token)) != NULL)
fy_token_free(fyt);
while ((fyep = fy_eventp_list_pop(&emit->recycled_eventp)) != NULL)
fy_eventp_free(fyep);
if (!emit->fyd && emit->fyds)
fy_document_state_unref(emit->fyds);
fy_emit_accum_cleanup(&emit->ea);
while ((fyep = fy_eventp_list_pop(&emit->queued_events)) != NULL)
fy_eventp_release(fyep);
if (emit->state_stack && emit->state_stack != emit->state_stack_inplace)
free(emit->state_stack);
if (emit->sc_stack && emit->sc_stack != emit->sc_stack_inplace)
free(emit->sc_stack);
fy_diag_unref(emit->diag);
}
int fy_emit_node_no_check(struct fy_emitter *emit, struct fy_node *fyn)
{
if (fyn)
fy_emit_node_internal(emit, fyn, DDNF_ROOT, -1, false);
return 0;
}
int fy_emit_node(struct fy_emitter *emit, struct fy_node *fyn)
{
int ret;
ret = fy_emit_node_check(emit, fyn);
if (ret)
return ret;
return fy_emit_node_no_check(emit, fyn);
}
int fy_emit_root_node_no_check(struct fy_emitter *emit, struct fy_node *fyn)
{
if (!emit || !fyn)
return -1;
/* top comment first */
fy_emit_node_comment(emit, fyn, DDNF_ROOT, -1, fycp_top);
fy_emit_node_internal(emit, fyn, DDNF_ROOT, -1, false);
/* right comment next */
fy_emit_node_comment(emit, fyn, DDNF_ROOT, -1, fycp_right);
/* bottom comment last */
fy_emit_node_comment(emit, fyn, DDNF_ROOT, -1, fycp_bottom);
return 0;
}
int fy_emit_root_node(struct fy_emitter *emit, struct fy_node *fyn)
{
int ret;
if (!emit || !fyn)
return -1;
ret = fy_emit_node_check(emit, fyn);
if (ret)
return ret;
return fy_emit_root_node_no_check(emit, fyn);
}
void fy_emit_prepare_document_state(struct fy_emitter *emit, struct fy_document_state *fyds)
{
if (!emit || !fyds)
return;
/* if the original document was JSON and the mode is ORIGINAL turn on JSON mode */
emit->source_json = fyds && fyds->json_mode;
emit->force_json = (emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK)) == FYECF_MODE_ORIGINAL &&
emit->source_json;
}
int fy_emit_document_no_check(struct fy_emitter *emit, struct fy_document *fyd)
{
int rc;
rc = fy_emit_document_start(emit, fyd, NULL);
if (rc)
return rc;
rc = fy_emit_root_node_no_check(emit, fyd->root);
if (rc)
return rc;
rc = fy_emit_document_end(emit);
return rc;
}
int fy_emit_document(struct fy_emitter *emit, struct fy_document *fyd)
{
int ret;
if (!emit)
return -1;
if (fyd) {
fy_emit_prepare_document_state(emit, fyd->fyds);
if (fyd->root) {
ret = fy_emit_node_check(emit, fyd->root);
if (ret)
return ret;
}
}
return fy_emit_document_no_check(emit, fyd);
}
struct fy_emitter *fy_emitter_create(const struct fy_emitter_cfg *cfg)
{
struct fy_emitter *emit;
int rc;
if (!cfg)
return NULL;
emit = malloc(sizeof(*emit));
if (!emit)
return NULL;
rc = fy_emit_setup(emit, cfg);
if (rc) {
free(emit);
return NULL;
}
return emit;
}
void fy_emitter_destroy(struct fy_emitter *emit)
{
if (!emit)
return;
fy_emit_cleanup(emit);
free(emit);
}
const struct fy_emitter_cfg *fy_emitter_get_cfg(struct fy_emitter *emit)
{
if (!emit)
return NULL;
return &emit->cfg;
}
struct fy_diag *fy_emitter_get_diag(struct fy_emitter *emit)
{
if (!emit || !emit->diag)
return NULL;
return fy_diag_ref(emit->diag);
}
int fy_emitter_set_diag(struct fy_emitter *emit, struct fy_diag *diag)
{
struct fy_diag_cfg dcfg;
if (!emit)
return -1;
/* default? */
if (!diag) {
fy_diag_cfg_default(&dcfg);
diag = fy_diag_create(&dcfg);
if (!diag)
return -1;
}
fy_diag_unref(emit->diag);
emit->diag = fy_diag_ref(diag);
return 0;
}
void fy_emitter_set_finalizer(struct fy_emitter *emit,
void (*finalizer)(struct fy_emitter *emit))
{
if (!emit)
return;
emit->finalizer = finalizer;
}
struct fy_emit_buffer_state {
char **bufp;
size_t *sizep;
char *buf;
size_t size;
size_t pos;
size_t need;
bool allocate_buffer;
};
static int do_buffer_output(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str, int leni, void *userdata)
{
struct fy_emit_buffer_state *state = emit->cfg.userdata;
size_t left, pagesize, size, len;
char *bufnew;
/* convert to unsigned and use that */
len = (size_t)leni;
/* no funky business */
if (len < 0)
return -1;
state->need += len;
left = state->size - state->pos;
if (left < len) {
if (!state->allocate_buffer)
return 0;
pagesize = fy_get_pagesize();
size = state->need + pagesize - 1;
size = size - size % pagesize;
bufnew = realloc(state->buf, size);
if (!bufnew)
return -1;
state->buf = bufnew;
state->size = size;
left = state->size - state->pos;
}
if (len > left)
len = left;
if (state->buf)
memcpy(state->buf + state->pos, str, len);
state->pos += len;
return len;
}
static void
fy_emitter_str_finalizer(struct fy_emitter *emit)
{
struct fy_emit_buffer_state *state;
if (!emit || !(state = emit->cfg.userdata))
return;
/* if the buffer is allowed to allocate_buffer... */
if (state->allocate_buffer && state->buf)
free(state->buf);
free(state);
emit->cfg.userdata = NULL;
}
static struct fy_emitter *
fy_emitter_create_str_internal(enum fy_emitter_cfg_flags flags, char **bufp, size_t *sizep, bool allocate_buffer)
{
struct fy_emitter *emit;
struct fy_emitter_cfg emit_cfg;
struct fy_emit_buffer_state *state;
state = malloc(sizeof(*state));
if (!state)
return NULL;
/* if any of these NULL, it's a allocation case */
if ((!bufp || !sizep) && !allocate_buffer)
return NULL;
if (bufp && sizep) {
state->bufp = bufp;
state->buf = *bufp;
state->sizep = sizep;
state->size = *sizep;
} else {
state->bufp = NULL;
state->buf = NULL;
state->sizep = NULL;
state->size = 0;
}
state->pos = 0;
state->need = 0;
state->allocate_buffer = allocate_buffer;
memset(&emit_cfg, 0, sizeof(emit_cfg));
emit_cfg.output = do_buffer_output;
emit_cfg.userdata = state;
emit_cfg.flags = flags;
emit = fy_emitter_create(&emit_cfg);
if (!emit)
goto err_out;
/* set finalizer to cleanup */
fy_emitter_set_finalizer(emit, fy_emitter_str_finalizer);
return emit;
err_out:
if (state)
free(state);
return NULL;
}
static int
fy_emitter_collect_str_internal(struct fy_emitter *emit, char **bufp, size_t *sizep)
{
struct fy_emit_buffer_state *state;
char *buf;
int rc;
state = emit->cfg.userdata;
assert(state);
/* if NULL, then use the values stored on the state */
if (!bufp)
bufp = state->bufp;
if (!sizep)
sizep = state->sizep;
/* terminating zero */
rc = do_buffer_output(emit, fyewt_terminating_zero, "\0", 1, state);
if (rc != 1)
goto err_out;
state->size = state->need;
if (state->allocate_buffer) {
/* resize */
buf = realloc(state->buf, state->size);
/* very likely since we shrink the buffer, but make sure we don't error out */
if (buf)
state->buf = buf;
}
/* retreive the buffer and size */
*sizep = state->size;
*bufp = state->buf;
/* reset the buffer, ownership now to the caller */
state->buf = NULL;
state->size = 0;
state->pos = 0;
state->bufp = NULL;
state->sizep = NULL;
return 0;
err_out:
*bufp = NULL;
*sizep = 0;
return -1;
}
static int fy_emit_str_internal(struct fy_document *fyd,
enum fy_emitter_cfg_flags flags,
struct fy_node *fyn, char **bufp, size_t *sizep,
bool allocate_buffer)
{
struct fy_emitter *emit = NULL;
int rc = -1;
emit = fy_emitter_create_str_internal(flags, bufp, sizep, allocate_buffer);
if (!emit)
goto out_err;
if (fyd) {
fy_emit_prepare_document_state(emit, fyd->fyds);
rc = 0;
if (fyd->root)
rc = fy_emit_node_check(emit, fyd->root);
if (!rc)
rc = fy_emit_document_no_check(emit, fyd);
} else {
rc = fy_emit_node_check(emit, fyn);
if (!rc)
rc = fy_emit_node_no_check(emit, fyn);
}
if (rc)
goto out_err;
rc = fy_emitter_collect_str_internal(emit, NULL, NULL);
if (rc)
goto out_err;
/* OK, all done */
out_err:
fy_emitter_destroy(emit);
return rc;
}
int fy_emit_document_to_buffer(struct fy_document *fyd, enum fy_emitter_cfg_flags flags, char *buf, size_t size)
{
int rc;
rc = fy_emit_str_internal(fyd, flags, NULL, &buf, &size, false);
if (rc != 0)
return -1;
return size;
}
char *fy_emit_document_to_string(struct fy_document *fyd, enum fy_emitter_cfg_flags flags)
{
char *buf;
size_t size;
int rc;
buf = NULL;
size = 0;
rc = fy_emit_str_internal(fyd, flags, NULL, &buf, &size, true);
if (rc != 0)
return NULL;
return buf;
}
struct fy_emitter *
fy_emit_to_buffer(enum fy_emitter_cfg_flags flags, char *buf, size_t size)
{
if (!buf)
return NULL;
return fy_emitter_create_str_internal(flags, &buf, &size, false);
}
char *
fy_emit_to_buffer_collect(struct fy_emitter *emit, size_t *sizep)
{
int rc;
char *buf;
if (!emit || !sizep)
return NULL;
rc = fy_emitter_collect_str_internal(emit, &buf, sizep);
if (rc) {
*sizep = 0;
return NULL;
}
return buf;
}
struct fy_emitter *
fy_emit_to_string(enum fy_emitter_cfg_flags flags)
{
return fy_emitter_create_str_internal(flags, NULL, NULL, true);
}
char *
fy_emit_to_string_collect(struct fy_emitter *emit, size_t *sizep)
{
int rc;
char *buf;
if (!emit || !sizep)
return NULL;
rc = fy_emitter_collect_str_internal(emit, &buf, sizep);
if (rc) {
*sizep = 0;
return NULL;
}
return buf;
}
static int do_file_output(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str, int leni, void *userdata)
{
FILE *fp = userdata;
size_t len;
len = (size_t)leni;
/* no funky stuff */
if (len < 0)
return -1;
return fwrite(str, 1, len, fp);
}
int fy_emit_document_to_fp(struct fy_document *fyd, enum fy_emitter_cfg_flags flags,
FILE *fp)
{
struct fy_emitter emit_state, *emit = &emit_state;
struct fy_emitter_cfg emit_cfg;
int rc;
if (!fp)
return -1;
memset(&emit_cfg, 0, sizeof(emit_cfg));
emit_cfg.output = do_file_output;
emit_cfg.userdata = fp;
emit_cfg.flags = flags;
fy_emit_setup(emit, &emit_cfg);
fy_emit_prepare_document_state(emit, fyd->fyds);
rc = 0;
if (fyd->root)
rc = fy_emit_node_check(emit, fyd->root);
rc = fy_emit_document_no_check(emit, fyd);
fy_emit_cleanup(emit);
return rc ? rc : 0;
}
int fy_emit_document_to_file(struct fy_document *fyd,
enum fy_emitter_cfg_flags flags,
const char *filename)
{
FILE *fp;
int rc;
fp = filename ? fopen(filename, "wa") : stdout;
if (!fp)
return -1;
rc = fy_emit_document_to_fp(fyd, flags, fp);
if (fp != stdout)
fclose(fp);
return rc ? rc : 0;
}
static int do_fd_output(struct fy_emitter *emit, enum fy_emitter_write_type type, const char *str, int leni, void *userdata)
{
size_t len;
int fd;
ssize_t wrn;
int total;
len = (size_t)leni;
/* no funky stuff */
if (len < 0)
return -1;
/* get the file descriptor */
fd = (int)(uintptr_t)userdata;
if (fd < 0)
return -1;
/* loop output to fd */
total = 0;
while (len > 0) {
do {
wrn = write(fd, str, len);
} while (wrn == -1 && errno == EAGAIN);
if (wrn == -1)
return -1;
if (wrn == 0)
return total;
len -= wrn;
str += wrn;
total += wrn;
}
return total;
}
int fy_emit_document_to_fd(struct fy_document *fyd, enum fy_emitter_cfg_flags flags, int fd)
{
struct fy_emitter emit_state, *emit = &emit_state;
struct fy_emitter_cfg emit_cfg;
int rc;
if (fd < 0)
return -1;
memset(&emit_cfg, 0, sizeof(emit_cfg));
emit_cfg.output = do_fd_output;
emit_cfg.userdata = (void *)(uintptr_t)fd;
emit_cfg.flags = flags;
fy_emit_setup(emit, &emit_cfg);
fy_emit_prepare_document_state(emit, fyd->fyds);
rc = 0;
if (fyd->root)
rc = fy_emit_node_check(emit, fyd->root);
rc = fy_emit_document_no_check(emit, fyd);
fy_emit_cleanup(emit);
return rc ? rc : 0;
}
int fy_emit_node_to_buffer(struct fy_node *fyn, enum fy_emitter_cfg_flags flags, char *buf, size_t size)
{
int rc;
rc = fy_emit_str_internal(NULL, flags, fyn, &buf, &size, false);
if (rc != 0)
return -1;
return size;
}
char *fy_emit_node_to_string(struct fy_node *fyn, enum fy_emitter_cfg_flags flags)
{
char *buf;
size_t size;
int rc;
buf = NULL;
size = 0;
rc = fy_emit_str_internal(NULL, flags, fyn, &buf, &size, true);
if (rc != 0)
return NULL;
return buf;
}
static bool fy_emit_ready(struct fy_emitter *emit)
{
struct fy_eventp *fyep;
int need, count, level;
/* no events in the list, not ready */
fyep = fy_eventp_list_head(&emit->queued_events);
if (!fyep)
return false;
/* some events need more than one */
switch (fyep->e.type) {
case FYET_DOCUMENT_START:
need = 1;
break;
case FYET_SEQUENCE_START:
need = 2;
break;
case FYET_MAPPING_START:
need = 3;
break;
default:
need = 0;
break;
}
/* if we don't need any more, that's enough */
if (!need)
return true;
level = 0;
count = 0;
for (; fyep; fyep = fy_eventp_next(&emit->queued_events, fyep)) {
count++;
if (count > need)
return true;
switch (fyep->e.type) {
case FYET_STREAM_START:
case FYET_DOCUMENT_START:
case FYET_SEQUENCE_START:
case FYET_MAPPING_START:
level++;
break;
case FYET_STREAM_END:
case FYET_DOCUMENT_END:
case FYET_SEQUENCE_END:
case FYET_MAPPING_END:
level--;
break;
default:
break;
}
if (!level)
return true;
}
return false;
}
extern const char *fy_event_type_txt[];
const char *fy_emitter_state_txt[] = {
[FYES_NONE] = "NONE",
[FYES_STREAM_START] = "STREAM_START",
[FYES_FIRST_DOCUMENT_START] = "FIRST_DOCUMENT_START",
[FYES_DOCUMENT_START] = "DOCUMENT_START",
[FYES_DOCUMENT_CONTENT] = "DOCUMENT_CONTENT",
[FYES_DOCUMENT_END] = "DOCUMENT_END",
[FYES_SEQUENCE_FIRST_ITEM] = "SEQUENCE_FIRST_ITEM",
[FYES_SEQUENCE_ITEM] = "SEQUENCE_ITEM",
[FYES_MAPPING_FIRST_KEY] = "MAPPING_FIRST_KEY",
[FYES_MAPPING_KEY] = "MAPPING_KEY",
[FYES_MAPPING_SIMPLE_VALUE] = "MAPPING_SIMPLE_VALUE",
[FYES_MAPPING_VALUE] = "MAPPING_VALUE",
[FYES_END] = "END",
};
struct fy_eventp *
fy_emit_next_event(struct fy_emitter *emit)
{
if (!fy_emit_ready(emit))
return NULL;
return fy_eventp_list_pop(&emit->queued_events);
}
struct fy_eventp *
fy_emit_peek_next_event(struct fy_emitter *emit)
{
if (!fy_emit_ready(emit))
return NULL;
return fy_eventp_list_head(&emit->queued_events);
}
bool fy_emit_streaming_sequence_empty(struct fy_emitter *emit)
{
enum fy_emitter_cfg_flags flags = emit->cfg.flags & FYECF_MODE(FYECF_MODE_MASK);
struct fy_eventp *fyepn;
struct fy_event *fyen;
if (flags == FYECF_MODE_BLOCK || flags == FYECF_MODE_DEJSON)
return false;
fyepn = fy_emit_peek_next_event(emit);
fyen = fyepn ? &fyepn->e : NULL;
return !fyen || fyen->type == FYET_SEQUENCE_END;
}
bool fy_emit_streaming_mapping_empty(struct fy_emitter *emit)
{
struct fy_eventp *fyepn;
struct fy_event *fyen;
fyepn = fy_emit_peek_next_event(emit);
fyen = fyepn ? &fyepn->e : NULL;
return !fyen || fyen->type == FYET_MAPPING_END;
}
static void fy_emit_goto_state(struct fy_emitter *emit, enum fy_emitter_state state)
{
if (emit->state == state)
return;
emit->state = state;
}
static int fy_emit_push_state(struct fy_emitter *emit, enum fy_emitter_state state)
{
enum fy_emitter_state *states;
if (emit->state_stack_top >= emit->state_stack_alloc) {
states = realloc(emit->state_stack == emit->state_stack_inplace ? NULL : emit->state_stack,
sizeof(emit->state_stack[0]) * emit->state_stack_alloc * 2);
if (!states)
return -1;
if (emit->state_stack == emit->state_stack_inplace)
memcpy(states, emit->state_stack, sizeof(emit->state_stack[0]) * emit->state_stack_top);
emit->state_stack = states;
emit->state_stack_alloc *= 2;
}
emit->state_stack[emit->state_stack_top++] = state;
return 0;
}
enum fy_emitter_state fy_emit_pop_state(struct fy_emitter *emit)
{
if (!emit->state_stack_top)
return FYES_NONE;
return emit->state_stack[--emit->state_stack_top];
}
int fy_emit_push_sc(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
struct fy_emit_save_ctx *scs;
if (emit->sc_stack_top >= emit->sc_stack_alloc) {
scs = realloc(emit->sc_stack == emit->sc_stack_inplace ? NULL : emit->sc_stack,
sizeof(emit->sc_stack[0]) * emit->sc_stack_alloc * 2);
if (!scs)
return -1;
if (emit->sc_stack == emit->sc_stack_inplace)
memcpy(scs, emit->sc_stack, sizeof(emit->sc_stack[0]) * emit->sc_stack_top);
emit->sc_stack = scs;
emit->sc_stack_alloc *= 2;
}
emit->sc_stack[emit->sc_stack_top++] = *sc;
return 0;
}
int fy_emit_pop_sc(struct fy_emitter *emit, struct fy_emit_save_ctx *sc)
{
if (!emit->sc_stack_top)
return -1;
*sc = emit->sc_stack[--emit->sc_stack_top];
return 0;
}
static int fy_emit_streaming_node(struct fy_emitter *emit, struct fy_eventp *fyep, int flags)
{
struct fy_event *fye = &fyep->e;
struct fy_emit_save_ctx *sc = &emit->s_sc;
enum fy_node_style style, xstyle;
int ret, s_flags, s_indent;
if (fye->type != FYET_ALIAS && fye->type != FYET_SCALAR &&
(emit->s_flags & DDNF_ROOT) && emit->column != 0) {
fy_emit_putc(emit, fyewt_linebreak, '\n');
emit->flags = FYEF_WHITESPACE | FYEF_INDENTATION;
}
emit->s_flags = flags;
switch (fye->type) {
case FYET_ALIAS:
fy_emit_token_write_alias(emit, fye->alias.anchor, emit->s_flags, emit->s_indent);
fy_emit_goto_state(emit, fy_emit_pop_state(emit));
break;
case FYET_SCALAR:
/* if we're pretty and at column 0 (meaning it's a single scalar document) output --- */
if ((emit->s_flags & DDNF_ROOT) && fy_emit_is_pretty_mode(emit) && !emit->column &&
!fy_emit_is_flow_mode(emit) && !(emit->s_flags & DDNF_FLOW))
fy_emit_document_start_indicator(emit);
fy_emit_common_node_preamble(emit, fye->scalar.anchor, fye->scalar.tag, emit->s_flags, emit->s_indent);
style = fye->scalar.value ?
fy_node_style_from_scalar_style(fye->scalar.value->scalar.style) :
FYNS_PLAIN;
fy_emit_token_scalar(emit, fye->scalar.value, emit->s_flags, emit->s_indent, style, fye->scalar.tag);
fy_emit_goto_state(emit, fy_emit_pop_state(emit));
break;
case FYET_SEQUENCE_START:
/* save this context */
ret = fy_emit_push_sc(emit, sc);
if (ret)
return ret;
s_flags = emit->s_flags;
s_indent = emit->s_indent;
xstyle = fye->sequence_start.sequence_start->type == FYTT_BLOCK_SEQUENCE_START ?
FYNS_BLOCK : FYNS_FLOW;
fy_emit_common_node_preamble(emit, fye->sequence_start.anchor, fye->sequence_start.tag, emit->s_flags, emit->s_indent);
/* create new context */
memset(sc, 0, sizeof(*sc));
sc->flags = emit->s_flags & (DDNF_ROOT | DDNF_SEQ | DDNF_MAP);
sc->indent = emit->s_indent;
sc->empty = fy_emit_streaming_sequence_empty(emit);
sc->flow_token = xstyle == FYNS_FLOW;
sc->flow = !!(s_flags & DDNF_FLOW);
sc->xstyle = xstyle;
sc->old_indent = sc->indent;
sc->s_flags = s_flags;
sc->s_indent = s_indent;
sc->s_flags = s_flags;
sc->s_indent = s_indent;
fy_emit_sequence_prolog(emit, sc);
sc->flags &= ~DDNF_MAP;
sc->flags |= DDNF_SEQ;
emit->s_flags = sc->flags;
emit->s_indent = sc->indent;
fy_emit_goto_state(emit, FYES_SEQUENCE_FIRST_ITEM);
break;
case FYET_MAPPING_START:
/* save this context */
ret = fy_emit_push_sc(emit, sc);
if (ret)
return ret;
s_flags = emit->s_flags;
s_indent = emit->s_indent;
xstyle = fye->mapping_start.mapping_start->type == FYTT_BLOCK_MAPPING_START ?
FYNS_BLOCK : FYNS_FLOW;
fy_emit_common_node_preamble(emit, fye->mapping_start.anchor, fye->mapping_start.tag, emit->s_flags, emit->s_indent);
/* create new context */
memset(sc, 0, sizeof(*sc));
sc->flags = emit->s_flags & (DDNF_ROOT | DDNF_SEQ | DDNF_MAP);
sc->indent = emit->s_indent;
sc->empty = fy_emit_streaming_mapping_empty(emit);
sc->flow_token = xstyle == FYNS_FLOW;
sc->flow = !!(s_flags & DDNF_FLOW);
sc->xstyle = xstyle;
sc->old_indent = sc->indent;
sc->s_flags = s_flags;
sc->s_indent = s_indent;
sc->s_flags = s_flags;
sc->s_indent = s_indent;
fy_emit_mapping_prolog(emit, sc);
sc->flags &= ~DDNF_SEQ;
sc->flags |= DDNF_MAP;
emit->s_flags = sc->flags;
emit->s_indent = sc->indent;
fy_emit_goto_state(emit, FYES_MAPPING_FIRST_KEY);
break;
default:
fy_error(emit->diag, "%s: expected ALIAS|SCALAR|SEQUENCE_START|MAPPING_START", __func__);
return -1;
}
return 0;
}
static int fy_emit_handle_stream_start(struct fy_emitter *emit, struct fy_eventp *fyep)
{
struct fy_event *fye = &fyep->e;
if (fye->type != FYET_STREAM_START) {
fy_error(emit->diag, "%s: expected FYET_STREAM_START", __func__);
return -1;
}
fy_emit_reset(emit, false);
fy_emit_goto_state(emit, FYES_FIRST_DOCUMENT_START);
return 0;
}
static int fy_emit_handle_document_start(struct fy_emitter *emit, struct fy_eventp *fyep, bool first)
{
struct fy_event *fye = &fyep->e;
struct fy_document_state *fyds;
if (fye->type != FYET_DOCUMENT_START &&
fye->type != FYET_STREAM_END) {
fy_error(emit->diag, "%s: expected FYET_DOCUMENT_START|FYET_STREAM_END", __func__);
return -1;
}
if (fye->type == FYET_STREAM_END) {
fy_emit_goto_state(emit, FYES_END);
return 0;
}
/* transfer ownership to the emitter */
fyds = fye->document_start.document_state;
fye->document_start.document_state = NULL;
/* prepare (i.e. adapt to the document state) */
fy_emit_prepare_document_state(emit, fyds);
fy_emit_common_document_start(emit, fyds, false);
fy_emit_goto_state(emit, FYES_DOCUMENT_CONTENT);
return 0;
}
static int fy_emit_handle_document_end(struct fy_emitter *emit, struct fy_eventp *fyep)
{
struct fy_document_state *fyds;
struct fy_event *fye = &fyep->e;
int ret;
if (fye->type != FYET_DOCUMENT_END) {
fy_error(emit->diag, "%s: expected FYET_DOCUMENT_END", __func__);
return -1;
}
fyds = emit->fyds;
ret = fy_emit_common_document_end(emit, true, fye->document_end.implicit);
if (ret)
return ret;
fy_document_state_unref(fyds);
fy_emit_reset(emit, false);
fy_emit_goto_state(emit, FYES_DOCUMENT_START);
return 0;
}
static int fy_emit_handle_document_content(struct fy_emitter *emit, struct fy_eventp *fyep)
{
struct fy_event *fye = &fyep->e;
int ret;
/* empty document? */
if (fye->type == FYET_DOCUMENT_END)
return fy_emit_handle_document_end(emit, fyep);
ret = fy_emit_push_state(emit, FYES_DOCUMENT_END);
if (ret)
return ret;
return fy_emit_streaming_node(emit, fyep, DDNF_ROOT);
}
static int fy_emit_handle_sequence_item(struct fy_emitter *emit, struct fy_eventp *fyep, bool first)
{
struct fy_event *fye = &fyep->e;
struct fy_emit_save_ctx *sc = &emit->s_sc;
struct fy_token *fyt_item = NULL;
int ret;
fy_token_unref_rl(emit->recycled_token_list, sc->fyt_last_value);
sc->fyt_last_value = NULL;
switch (fye->type) {
case FYET_SEQUENCE_END:
fy_emit_sequence_item_epilog(emit, sc, true, sc->fyt_last_value);
/* emit epilog */
fy_emit_sequence_epilog(emit, sc);
/* pop state */
ret = fy_emit_pop_sc(emit, sc);
/* pop state */
fy_emit_goto_state(emit, fy_emit_pop_state(emit));
/* restore indent and flags */
emit->s_indent = sc->s_indent;
emit->s_flags = sc->s_flags;
return ret;
case FYET_ALIAS:
fyt_item = fye->alias.anchor;
break;
case FYET_SCALAR:
fyt_item = fye->scalar.value;
break;
case FYET_SEQUENCE_START:
fyt_item = fye->sequence_start.sequence_start;
break;
case FYET_MAPPING_START:
fyt_item = fye->mapping_start.mapping_start;
break;
default:
fy_error(emit->diag, "%s: expected SEQUENCE_END|ALIAS|SCALAR|SEQUENCE_START|MAPPING_START", __func__);
return -1;
}
ret = fy_emit_push_state(emit, FYES_SEQUENCE_ITEM);
if (ret)
return ret;
/* reset indent and flags for each item */
emit->s_indent = sc->indent;
emit->s_flags = sc->flags;
if (!first)
fy_emit_sequence_item_epilog(emit, sc, false, sc->fyt_last_value);
sc->fyt_last_value = fyt_item;
fy_emit_sequence_item_prolog(emit, sc, fyt_item);
ret = fy_emit_streaming_node(emit, fyep, sc->flags);
switch (fye->type) {
case FYET_ALIAS:
fye->alias.anchor = NULL; /* take ownership */
break;
case FYET_SCALAR:
fye->scalar.value = NULL; /* take ownership */
break;
case FYET_SEQUENCE_START:
fye->sequence_start.sequence_start = NULL; /* take ownership */
break;
case FYET_MAPPING_START:
fye->mapping_start.mapping_start = NULL; /* take ownership */
break;
default:
break;
}
return ret;
}
static int fy_emit_handle_mapping_key(struct fy_emitter *emit, struct fy_eventp *fyep, bool first)
{
struct fy_event *fye = &fyep->e;
struct fy_emit_save_ctx *sc = &emit->s_sc;
struct fy_token *fyt_key = NULL;
int ret, aflags;
bool simple_key;
fy_token_unref_rl(emit->recycled_token_list, sc->fyt_last_key);
sc->fyt_last_key = NULL;
fy_token_unref_rl(emit->recycled_token_list, sc->fyt_last_value);
sc->fyt_last_value = NULL;
simple_key = false;
switch (fye->type) {
case FYET_MAPPING_END:
fy_emit_mapping_value_epilog(emit, sc, true, sc->fyt_last_value);
/* emit epilog */
fy_emit_mapping_epilog(emit, sc);
/* pop state */
ret = fy_emit_pop_sc(emit, sc);
/* pop state */
fy_emit_goto_state(emit, fy_emit_pop_state(emit));
/* restore indent and flags */
emit->s_indent = sc->s_indent;
emit->s_flags = sc->s_flags;
return ret;
case FYET_ALIAS:
fyt_key = fye->alias.anchor;
simple_key = true;
break;
case FYET_SCALAR:
fyt_key = fye->scalar.value;
aflags = fy_token_text_analyze(fyt_key);
simple_key = !!(aflags & FYTTAF_CAN_BE_SIMPLE_KEY);
break;
case FYET_SEQUENCE_START:
fyt_key = fye->sequence_start.sequence_start;
simple_key = fy_emit_streaming_sequence_empty(emit);
break;
case FYET_MAPPING_START:
fyt_key = fye->mapping_start.mapping_start;
simple_key = fy_emit_streaming_mapping_empty(emit);
break;
default:
fy_error(emit->diag, "%s: expected MAPPING_END|ALIAS|SCALAR|SEQUENCE_START|MAPPING_START", __func__);
return -1;
}
ret = fy_emit_push_state(emit, FYES_MAPPING_VALUE);
if (ret)
return ret;
/* reset indent and flags for each key/value pair */
emit->s_indent = sc->indent;
emit->s_flags = sc->flags;
if (!first)
fy_emit_mapping_value_epilog(emit, sc, false, sc->fyt_last_value);
sc->fyt_last_key = fyt_key;
fy_emit_mapping_key_prolog(emit, sc, fyt_key, simple_key);
ret = fy_emit_streaming_node(emit, fyep, sc->flags);
switch (fye->type) {
case FYET_ALIAS:
fye->alias.anchor = NULL; /* take ownership */
break;
case FYET_SCALAR:
fye->scalar.value = NULL; /* take ownership */
break;
case FYET_SEQUENCE_START:
fye->sequence_start.sequence_start = NULL; /* take ownership */
break;
case FYET_MAPPING_START:
fye->mapping_start.mapping_start = NULL; /* take ownership */
break;
default:
break;
}
return ret;
}
static int fy_emit_handle_mapping_value(struct fy_emitter *emit, struct fy_eventp *fyep, bool simple)
{
struct fy_event *fye = &fyep->e;
struct fy_emit_save_ctx *sc = &emit->s_sc;
struct fy_token *fyt_value = NULL;
int ret;
switch (fye->type) {
case FYET_ALIAS:
fyt_value = fye->alias.anchor;
break;
case FYET_SCALAR:
fyt_value = fye->scalar.value; /* take ownership */
break;
case FYET_SEQUENCE_START:
fyt_value = fye->sequence_start.sequence_start;
break;
case FYET_MAPPING_START:
fyt_value = fye->mapping_start.mapping_start;
break;
default:
fy_error(emit->diag, "%s: expected ALIAS|SCALAR|SEQUENCE_START|MAPPING_START", __func__);
return -1;
}
ret = fy_emit_push_state(emit, FYES_MAPPING_KEY);
if (ret)
return ret;
fy_emit_mapping_key_epilog(emit, sc, sc->fyt_last_key);
sc->fyt_last_value = fyt_value;
fy_emit_mapping_value_prolog(emit, sc, fyt_value);
ret = fy_emit_streaming_node(emit, fyep, sc->flags);
switch (fye->type) {
case FYET_ALIAS:
fye->alias.anchor = NULL; /* take ownership */
break;
case FYET_SCALAR:
fye->scalar.value = NULL; /* take ownership */
break;
case FYET_SEQUENCE_START:
fye->sequence_start.sequence_start = NULL; /* take ownership */
break;
case FYET_MAPPING_START:
fye->mapping_start.mapping_start = NULL; /* take ownership */
break;
default:
break;
}
return ret;
}
int fy_emit_event_from_parser(struct fy_emitter *emit, struct fy_parser *fyp, struct fy_event *fye)
{
struct fy_eventp *fyep;
int ret;
if (!emit || !fye)
return -1;
/* we're using the raw emitter interface, now mark first state */
if (emit->state == FYES_NONE)
emit->state = FYES_STREAM_START;
fyep = fy_container_of(fye, struct fy_eventp, e);
fy_eventp_list_add_tail(&emit->queued_events, fyep);
ret = 0;
while ((fyep = fy_emit_next_event(emit)) != NULL) {
switch (emit->state) {
case FYES_STREAM_START:
ret = fy_emit_handle_stream_start(emit, fyep);
break;
case FYES_FIRST_DOCUMENT_START:
case FYES_DOCUMENT_START:
ret = fy_emit_handle_document_start(emit, fyep,
emit->state == FYES_FIRST_DOCUMENT_START);
break;
case FYES_DOCUMENT_CONTENT:
ret = fy_emit_handle_document_content(emit, fyep);
break;
case FYES_DOCUMENT_END:
ret = fy_emit_handle_document_end(emit, fyep);
break;
case FYES_SEQUENCE_FIRST_ITEM:
case FYES_SEQUENCE_ITEM:
ret = fy_emit_handle_sequence_item(emit, fyep,
emit->state == FYES_SEQUENCE_FIRST_ITEM);
break;
case FYES_MAPPING_FIRST_KEY:
case FYES_MAPPING_KEY:
ret = fy_emit_handle_mapping_key(emit, fyep,
emit->state == FYES_MAPPING_FIRST_KEY);
break;
case FYES_MAPPING_SIMPLE_VALUE:
case FYES_MAPPING_VALUE:
ret = fy_emit_handle_mapping_value(emit, fyep,
emit->state == FYES_MAPPING_SIMPLE_VALUE);
break;
case FYES_END:
ret = -1;
break;
default:
assert(1); /* Invalid state. */
}
/* always release the event */
if (!fyp)
fy_eventp_release(fyep);
else
fy_parse_eventp_recycle(fyp, fyep);
if (ret)
break;
}
return ret;
}
int fy_emit_event(struct fy_emitter *emit, struct fy_event *fye)
{
return fy_emit_event_from_parser(emit, NULL, fye);
}
struct fy_document_state *
fy_emitter_get_document_state(struct fy_emitter *emit)
{
return emit ? emit->fyds : NULL;
}
int fy_emitter_default_output(struct fy_emitter *fye, enum fy_emitter_write_type type, const char *str, int len, void *userdata)
{
struct fy_emitter_default_output_data d_local, *d;
FILE *fp;
int ret, w;
const char *color = NULL;
const char *s, *e;
d = userdata;
if (!d) {
/* kinda inneficient but should not matter */
d = &d_local;
d->fp = stdout;
d->colorize = isatty(STDOUT_FILENO);
d->visible = false;
}
fp = d->fp;
s = str;
e = str + len;
if (d->colorize) {
switch (type) {
case fyewt_document_indicator:
color = "\x1b[36m";
break;
case fyewt_tag_directive:
case fyewt_version_directive:
color = "\x1b[33m";
break;
case fyewt_indent:
if (d->visible) {
fputs("\x1b[32m", fp);
while (s < e && (w = fy_utf8_width_by_first_octet(((uint8_t)*s))) > 0) {
/* open box - U+2423 */
fputs("\xe2\x90\xa3", fp);
s += w;
}
fputs("\x1b[0m", fp);
return len;
}
break;
case fyewt_indicator:
if (len == 1 && (str[0] == '\'' || str[0] == '"'))
color = "\x1b[33m";
else if (len == 1 && str[0] == '&')
color = "\x1b[32;1m";
else
color = "\x1b[35m";
break;
case fyewt_whitespace:
if (d->visible) {
fputs("\x1b[32m", fp);
while (s < e && (w = fy_utf8_width_by_first_octet(((uint8_t)*s))) > 0) {
/* symbol for space - U+2420 */
/* symbol for interpunct - U+00B7 */
fputs("\xc2\xb7", fp);
s += w;
}
fputs("\x1b[0m", fp);
return len;
}
break;
case fyewt_plain_scalar:
color = "\x1b[37;1m";
break;
case fyewt_single_quoted_scalar:
case fyewt_double_quoted_scalar:
color = "\x1b[33m";
break;
case fyewt_literal_scalar:
case fyewt_folded_scalar:
color = "\x1b[33m";
break;
case fyewt_anchor:
case fyewt_tag:
case fyewt_alias:
color = "\x1b[32;1m";
break;
case fyewt_linebreak:
if (d->visible) {
fputs("\x1b[32m", fp);
while (s < e && (w = fy_utf8_width_by_first_octet(((uint8_t)*s))) > 0) {
/* symbol for space - ^M */
/* fprintf(fp, "^M\n"); */
/* down arrow - U+2193 */
fputs("\xe2\x86\x93\n", fp);
s += w;
}
fputs("\x1b[0m", fp);
return len;
}
color = NULL;
break;
case fyewt_terminating_zero:
color = NULL;
break;
case fyewt_plain_scalar_key:
case fyewt_single_quoted_scalar_key:
case fyewt_double_quoted_scalar_key:
color = "\x1b[36;1m";
break;
case fyewt_comment:
color = "\x1b[34;1m";
break;
}
}
/* don't output the terminating zero */
if (type == fyewt_terminating_zero)
return len;
if (color)
fputs(color, fp);
ret = fwrite(str, 1, len, fp);
if (color)
fputs("\x1b[0m", fp);
return ret;
}
int fy_document_default_emit_to_fp(struct fy_document *fyd, FILE *fp)
{
struct fy_emitter emit_local, *emit = &emit_local;
struct fy_emitter_cfg ecfg_local, *ecfg = &ecfg_local;
struct fy_emitter_default_output_data d_local, *d = &d_local;
int rc;
memset(d, 0, sizeof(*d));
d->fp = fp;
d->colorize = isatty(fileno(fp));
d->visible = false;
memset(ecfg, 0, sizeof(*ecfg));
ecfg->diag = fyd->diag;
ecfg->userdata = d;
rc = fy_emit_setup(emit, ecfg);
if (rc)
goto err_setup;
fy_emit_prepare_document_state(emit, fyd->fyds);
rc = 0;
if (fyd->root)
rc = fy_emit_node_check(emit, fyd->root);
rc = fy_emit_document_no_check(emit, fyd);
if (rc)
goto err_emit;
fy_emit_cleanup(emit);
return 0;
err_emit:
fy_emit_cleanup(emit);
err_setup:
return -1;
}
|