summaryrefslogtreecommitdiffstats
path: root/src/printer_tree.c
blob: 6aa2814289e70626a9b5e93595444cca91bba2a0 (plain)
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
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
/**
 * @file printer_tree.c
 * @author Adam Piecek <piecek@cesnet.cz>
 * @brief RFC tree printer for libyang data structure
 *
 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 *
 * @section TRP_DESIGN Design
 *
 * @code
 *          +---------+    +---------+    +---------+
 *   output |   trp   |    |   trb   |    |   tro   |
 *      <---+  Print  +<---+  Browse +<-->+  Obtain |
 *          |         |    |         |    |         |
 *          +---------+    +----+----+    +---------+
 *                              ^
 *                              |
 *                         +----+----+
 *                         |   trm   |
 *                         | Manager |
 *                         |         |
 *                         +----+----+
 *                              ^
 *                              | input
 *                              +
 * @endcode
 *
 * @subsection TRP_GLOSSARY Glossary
 *
 * @subsubsection TRP_trm trm
 * Manager functions are at the peak of abstraction. They are
 * able to print individual sections of the YANG tree diagram
 * (eg module, notifications, rpcs ...) and they call
 * Browse functions (@ref TRP_trb).
 *
 * @subsubsection TRP_trb trb
 * Browse functions contain a general algorithm (Preorder DFS)
 * for traversing the tree. It does not matter what data type
 * the tree contains (@ref lysc_node or @ref lysp_node), because it
 * requires a ready-made getter functions for traversing the tree
 * (@ref trt_fp_all) and transformation function to its own node
 * data type (@ref trt_node). These getter functions are generally
 * referred to as @ref TRP_tro. Browse functions can repeatedly
 * traverse nodes in the tree, for example, to calculate the alignment
 * gap before the nodes \<type\> in the YANG Tree Diagram.
 * The obtained @ref trt_node is passed to the @ref TRP_trp functions
 * to print the Tree diagram.
 *
 * @subsubsection TRP_tro tro
 * Functions that provide an extra wrapper for the libyang library.
 * The Obtain functions are further specialized according to whether
 * they operate on lysp_tree (@ref TRP_trop) or lysc_tree
 * (@ref TRP_troc). If they are general algorithms, then they have the
 * prefix \b tro_. The Obtain functions provide information to
 * @ref TRP_trb functions for printing the Tree diagram.
 *
 * @subsubsection TRP_trop trop
 * Functions for Obtaining information from Parsed schema tree.
 *
 * @subsubsection TRP_troc troc
 * Functions for Obtaining information from Compiled schema tree.
 *
 * @subsubsection TRP_trp trp
 * Print functions take care of the printing YANG diagram. They can
 * also split one node into multiple lines if the node does not fit
 * on one line.
 *
 * @subsubsection TRP_trt trt
 * Data type marking in the printer_tree module.
 *
 * @subsubsection TRP_trg trg
 * General functions.
 *
 * @subsection TRP_ADJUSTMENTS Adjustments
 * It is assumed that the changes are likely to take place mainly for
 * @ref TRP_tro, @ref TRP_trop or @ref TRP_troc functions because
 * they are the only ones dependent on libyang implementation.
 * In special cases, changes will also need to be made to the
 * @ref TRP_trp functions if a special algorithm is needed to print
 * (right now this is prepared for printing list's keys
 * and if-features).
 */

#include <assert.h>
#include <string.h>

#include "common.h"
#include "compat.h"
#include "out_internal.h"
#include "plugins_exts.h"
#include "plugins_types.h"
#include "printer_internal.h"
#include "printer_schema.h"
#include "tree_schema_internal.h"
#include "xpath.h"

/**
 * @brief List of available actions.
 */
typedef enum {
    TRD_PRINT = 0,  /**< Normal behavior. It just prints. */
    TRD_CHAR_COUNT  /**< Characters will be counted instead of printing. */
} trt_ly_out_clb_arg_flag;

/**
 * @brief Structure is passed as 'writeclb' argument
 * to the ::ly_out_new_clb().
 */
struct ly_out_clb_arg {
    trt_ly_out_clb_arg_flag mode;   /**< flag specifying which action to take. */
    struct ly_out *out;             /**< The ly_out pointer delivered to the printer tree module via the main interface. */
    size_t counter;                 /**< Counter of printed characters. */
    LY_ERR last_error;              /**< The last error that occurred. If no error has occurred, it will be ::LY_SUCCESS. */
};

/**
 * @brief Initialize struct ly_out_clb_arg with default settings.
 */
#define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \
    (struct ly_out_clb_arg) { \
        .mode = MODE, .out = OUT, \
        .counter = COUNTER, .last_error = LAST_ERROR \
    }

/**********************************************************************
 * Print getters
 *********************************************************************/

/**
 * @brief Callback functions that prints special cases.
 *
 * It just groups together tree context with trt_fp_print.
 */
struct trt_cf_print {
    const struct trt_tree_ctx *ctx;                             /**< Context of libyang tree. */

    void (*pf)(const struct trt_tree_ctx *, struct ly_out *);   /**< Pointing to function which printing list's keys or features. */
};

/**
 * @brief Callback functions for printing special cases.
 *
 * Functions with the suffix 'trp' can print most of the text on
 * output, just by setting the pointer to the string. But in some
 * cases, it's not that simple, because its entire string is fragmented
 * in memory. For example, for printing list's keys or if-features.
 * However, this depends on how the libyang library is implemented.
 * This implementation of the printer_tree module goes through
 * a lysp tree, but if it goes through a lysc tree, these special cases
 * would be different.
 * Functions must print including spaces or delimiters between names.
 */
struct trt_fp_print {
    void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *);   /**< Print list of features without {}? wrapper. */
    void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *);             /**< Print list's keys without [] wrapper. */
};

/**
 * @brief Package which only groups getter function.
 */
struct trt_pck_print {
    const struct trt_tree_ctx *tree_ctx;    /**< Context of libyang tree. */
    struct trt_fp_print fps;                /**< Print function. */
};

/**
 * @brief Initialize struct trt_pck_print by parameters.
 */
#define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \
    (struct trt_pck_print) {.tree_ctx = TREE_CTX, .fps = FP_PRINT}

/**********************************************************************
 * Indent
 *********************************************************************/

/**
 * @brief Constants which are defined in the RFC or are observable
 * from the pyang tool.
 */
typedef enum {
    TRD_INDENT_EMPTY = 0,               /**< If the node is a case node, there is no space before the \<name\>. */
    TRD_INDENT_LONG_LINE_BREAK = 2,     /**< The new line should be indented so that it starts below \<name\> with
                                             a whitespace offset of at least two characters. */
    TRD_INDENT_LINE_BEGIN = 2,          /**< Indent below the keyword (module, augment ...).  */
    TRD_INDENT_BTW_SIBLINGS = 2,        /**< Indent between | and | characters. */
    TRD_INDENT_BEFORE_KEYS = 1,         /**< "..."___\<keys\>. */
    TRD_INDENT_BEFORE_TYPE = 4,         /**< "..."___\<type\>, but if mark is set then indent == 3. */
    TRD_INDENT_BEFORE_IFFEATURES = 1    /**< "..."___\<iffeatures\>. */
} trt_cnf_indent;

/**
 * @brief Type of indent in node.
 */
typedef enum {
    TRD_INDENT_IN_NODE_NORMAL = 0,  /**< Node fits on one line. */
    TRD_INDENT_IN_NODE_DIVIDED,     /**< The node must be split into multiple rows. */
    TRD_INDENT_IN_NODE_FAILED       /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */
} trt_indent_in_node_type;

/** Constant to indicate the need to break a line. */
#define TRD_LINEBREAK -1

/**
 * @brief Records the alignment between the individual
 * elements of the node.
 *
 * @see trp_default_indent_in_node, trp_try_normal_indent_in_node
 */
struct trt_indent_in_node {
    trt_indent_in_node_type type;   /**< Type of indent in node. */
    int16_t btw_name_opts;          /**< Indent between node name and \<opts\>. */
    int16_t btw_opts_type;          /**< Indent between \<opts\> and \<type\>. */
    int16_t btw_type_iffeatures;    /**< Indent between type and features. Ignored if \<type\> missing. */
};

/**
 * @brief Type of wrappers to be printed.
 */
typedef enum {
    TRD_WRAPPER_TOP = 0,    /**< Related to the module. */
    TRD_WRAPPER_BODY        /**< Related to e.g. Augmentations or Groupings */
} trd_wrapper_type;

/**
 * @brief For resolving sibling symbol ('|') placement.
 *
 * Bit indicates where the sibling symbol must be printed.
 * This place is in multiples of ::TRD_INDENT_BTW_SIBLINGS.
 *
 * @see TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY,
 * trp_wrapper_set_mark, trp_wrapper_set_shift,
 * trp_wrapper_if_last_sibling, trp_wrapper_eq, trp_print_wrapper
 */
struct trt_wrapper {
    trd_wrapper_type type;  /**< Location of the wrapper. */
    uint64_t bit_marks1;    /**< The set bits indicate where the '|' character is to be printed.
                                 It follows that the maximum immersion of the printable node is 64. */
    uint32_t actual_pos;    /**< Actual position in bit_marks. */
};

/**
 * @brief Get wrapper related to the module section.
 *
 * @code
 * module: <module-name>
 *   +--<node>
 *   |
 * @endcode
 */
#define TRP_INIT_WRAPPER_TOP \
    (struct trt_wrapper) { \
        .type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0 \
    }

/**
 * @brief Get wrapper related to subsection
 * e.g. Augmenations or Groupings.
 *
 * @code
 * module: <module-name>
 *   +--<node>
 *
 *   augment <target-node>:
 *     +--<node>
 * @endcode
 */
#define TRP_INIT_WRAPPER_BODY \
    (struct trt_wrapper) { \
        .type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0 \
    }

/**
 * @brief Package which only groups wrapper and indent in node.
 */
struct trt_pck_indent {
    struct trt_wrapper wrapper;         /**< Coded "  |  |  " sequence. */
    struct trt_indent_in_node in_node;  /**< Indent in node. */
};

/**
 * @brief Initialize struct trt_pck_indent by parameters.
 */
#define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \
    (struct trt_pck_indent){ \
        .wrapper = WRAPPER, .in_node = INDENT_IN_NODE \
    }

/**********************************************************************
 * flags
 *********************************************************************/

#define TRD_FLAGS_TYPE_EMPTY "--"
#define TRD_FLAGS_TYPE_RW "rw"
#define TRD_FLAGS_TYPE_RO "ro"
#define TRD_FLAGS_TYPE_RPC_INPUT_PARAMS "-w"
#define TRD_FLAGS_TYPE_USES_OF_GROUPING "-u"
#define TRD_FLAGS_TYPE_RPC "-x"
#define TRD_FLAGS_TYPE_NOTIF "-n"
#define TRD_FLAGS_TYPE_MOUNT_POINT "mp"

/**********************************************************************
 * node_name and opts
 *********************************************************************/

#define TRD_NODE_NAME_PREFIX_CHOICE "("
#define TRD_NODE_NAME_PREFIX_CASE ":("
#define TRD_NODE_NAME_TRIPLE_DOT "..."

/**
 * @brief Type of the node.
 *
 * Used mainly to complete the correct \<opts\> next to or
 * around the \<name\>.
 */
typedef enum {
    TRD_NODE_ELSE = 0,          /**< For some node which does not require special treatment. \<name\> */
    TRD_NODE_CASE,              /**< For case node. :(\<name\>) */
    TRD_NODE_CHOICE,            /**< For choice node. (\<name\>) */
    TRD_NODE_TRIPLE_DOT         /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */
} trt_node_type;

#define TRD_NODE_OPTIONAL "?"          /**< For an optional leaf, anydata, or anyxml. \<name\>? */
#define TRD_NODE_CONTAINER "!"         /**< For a presence container. \<name\>! */
#define TRD_NODE_LISTLEAFLIST "*"      /**< For a leaf-list or list. \<name\>* */

/**
 * @brief Type of node and his name.
 *
 * @see TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY,
 * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys
 */
struct trt_node_name {
    trt_node_type type;         /**< Type of the node relevant for printing. */
    ly_bool keys;               /**< Set to 1 if [\<keys\>] are to be printed. Valid for some types only. */
    const char *module_prefix;  /**< If the node is augmented into the tree from another module,
                                     so this is the prefix of that module. */
    const char *str;            /**< Name of the node. */
    const char *add_opts;       /**< Additional opts symbol from plugin. */
    const char *opts;           /**< The \<opts\> symbol. */
};

/**
 * @brief Create struct trt_node_name as empty.
 */
#define TRP_EMPTY_NODE_NAME \
    (struct trt_node_name) { \
        .type = TRD_NODE_ELSE, .keys = 0, .module_prefix = NULL, .str = NULL, .opts = NULL, .add_opts = NULL \
    }

/**
 * @brief Check if struct trt_node_name is empty.
 */
#define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \
    !NODE_NAME.str

/**********************************************************************
 * type
 *********************************************************************/

/**
 * @brief Type of the \<type\>
 */
typedef enum {
    TRD_TYPE_NAME = 0,  /**< Type is just a name that does not require special treatment. */
    TRD_TYPE_TARGET,    /**< Should have a form "-> TARGET", where TARGET is the leafref path. */
    TRD_TYPE_LEAFREF,   /**< This type is set automatically by the 'trp' algorithm.
                             So set type as ::TRD_TYPE_TARGET. */
    TRD_TYPE_EMPTY      /**< Type is not used at all. */
} trt_type_type;

/**
 * @brief \<type\> in the \<node\>.
 *
 * @see TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type
 */
struct trt_type {
    trt_type_type type; /**< Type of the \<type\>. */
    const char *str;    /**< Path or name of the type. */
};

/**
 * @brief Create empty struct trt_type.
 */
#define TRP_EMPTY_TRT_TYPE \
    (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL}

/**
 * @brief Check if struct trt_type is empty.
 */
#define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \
    TYPE_OF_TYPE.type == TRD_TYPE_EMPTY

/**
 * @brief Initialize struct trt_type by parameters.
 */
#define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \
    (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING}

/**
 * @brief If-feature type.
 */
typedef enum {
    TRD_IFF_NON_PRESENT = 0,    /**< iffeatures are not present. */
    TRD_IFF_PRESENT,            /**< iffeatures are present and will be printed by
                                     trt_fp_print.print_features_names callback */
    TRD_IFF_OVERR               /**< iffeatures are override by plugin */
} trt_iffeatures_type;

/**
 * @brief \<if-features\>.
 */
struct trt_iffeatures {
    trt_iffeatures_type type;   /**< Type of iffeature. */
    char *str;                  /**< iffeatures string ready to print. Set if TRD_IFF_OVERR is set. */
};

/**
 * @brief Create empty iffeatures.
 */
#define TRP_EMPTY_TRT_IFFEATURES \
    (struct trt_iffeatures) {.type = TRD_IFF_NON_PRESENT}

/**
 * @brief Check if iffeatures is empty.
 *
 * @param[in] IFF_TYPE value from trt_iffeatures.type.
 * @return 1 if is empty.
 */
#define TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(IFF_TYPE) \
    (IFF_TYPE == TRD_IFF_NON_PRESENT)

/**********************************************************************
 * node
 *********************************************************************/

/**
 * @brief \<node\> data for printing.
 *
 * It contains RFC's:
 * \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>.
 * Item \<opts\> is moved to part struct trt_node_name.
 * For printing [\<keys\>] and if-features is required special
 * functions which prints them.
 *
 * @see TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty,
 * trp_print_node_up_to_name, trp_print_divided_node_up_to_name,
 * trp_print_node
 */
struct trt_node {
    const char *status;                 /**< \<status\>. */
    const char *flags;                  /**< \<flags\>. */
    struct trt_node_name name;          /**< \<node\> with \<opts\> mark or [\<keys\>]. */
    struct trt_type type;               /**< \<type\> contains the name of the type or type for leafref. */
    struct trt_iffeatures iffeatures;   /**< \<if-features\>. */
    ly_bool last_one;                   /**< Information about whether the node is the last. */
};

/**
 * @brief Create struct trt_node as empty.
 */
#define TRP_EMPTY_NODE \
    (struct trt_node) { \
        .status = NULL, \
        .flags = NULL, \
        .name = TRP_EMPTY_NODE_NAME, \
        .type = TRP_EMPTY_TRT_TYPE, \
        .iffeatures = TRP_EMPTY_TRT_IFFEATURES, \
        .last_one = 1 \
    }

/**
 * @brief Package which only groups indent and node.
 */
struct trt_pair_indent_node {
    struct trt_indent_in_node indent;
    struct trt_node node;
};

/**
 * @brief Initialize struct trt_pair_indent_node by parameters.
 */
#define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \
    (struct trt_pair_indent_node) { \
        .indent = INDENT_IN_NODE, .node = NODE \
    }

/**********************************************************************
 * statement
 *********************************************************************/

#define TRD_KEYWORD_MODULE "module"
#define TRD_KEYWORD_SUBMODULE "submodule"
#define TRD_KEYWORD_AUGMENT "augment"
#define TRD_KEYWORD_RPC "rpcs"
#define TRD_KEYWORD_NOTIF "notifications"
#define TRD_KEYWORD_GROUPING "grouping"

/**
 * @brief Main sign of the tree nodes.
 *
 * @see TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY
 * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str,
 * trt_print_keyword_stmt_end, trp_print_keyword_stmt
 */
struct trt_keyword_stmt {
    const char *section_name;   /**< String containing section name. */
    const char *argument;       /**< Name or path located begind section name. */
    ly_bool has_node;           /**< Flag if section has any nodes. */
};

/**
 * @brief Create struct trt_keyword_stmt as empty.
 */
#define TRP_EMPTY_KEYWORD_STMT \
    (struct trt_keyword_stmt) {.section_name = NULL, .argument = NULL, .has_node = 0}

/**********************************************************************
 * Modify getters
 *********************************************************************/

struct trt_parent_cache;

/**
 * @brief Functions that change the state of the tree_ctx structure.
 *
 * The 'trop' or 'troc' functions are set here, which provide data
 * for the 'trp' printing functions and are also called from the
 * 'trb' browsing functions when walking through a tree. These callback
 * functions need to be checked or reformulated if changes to the
 * libyang library affect the printing tree. For all, if the value
 * cannot be returned, its empty version obtained by relevant TRP_EMPTY
 * macro is returned.
 */
struct trt_fp_modify_ctx {
    ly_bool (*parent)(struct trt_tree_ctx *);                                           /**< Jump to parent node. Return true if parent exists. */
    struct trt_node (*first_sibling)(struct trt_parent_cache, struct trt_tree_ctx *);   /**< Jump on the first of the siblings. */
    struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *);    /**< Jump to next sibling of the current node. */
    struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *);      /**< Jump to the child of the current node. */
};

/**
 * @brief Create modify functions for compiled tree.
 */
#define TRP_TRT_FP_MODIFY_COMPILED \
    (struct trt_fp_modify_ctx) { \
        .parent = troc_modi_parent, \
        .first_sibling = troc_modi_first_sibling, \
        .next_sibling = troc_modi_next_sibling, \
        .next_child = troc_modi_next_child, \
    }

/**
 * @brief Create modify functions for parsed tree.
 */
#define TRP_TRT_FP_MODIFY_PARSED \
    (struct trt_fp_modify_ctx) { \
        .parent = trop_modi_parent, \
        .first_sibling = trop_modi_first_sibling, \
        .next_sibling = trop_modi_next_sibling, \
        .next_child = trop_modi_next_child, \
    }

/**********************************************************************
 * Read getters
 *********************************************************************/

/**
 * @brief Functions that do not change the state of the tree_structure.
 *
 * For details see trt_fp_modify_ctx.
 */
struct trt_fp_read {
    struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *);            /**< Get name of the module. */
    struct trt_node (*node)(struct trt_parent_cache, struct trt_tree_ctx *);        /**< Get current node. */
    ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *);                      /**< Check if node's sibling exists. */
    ly_bool (*if_parent_exists)(const struct trt_tree_ctx *);                       /**< Check if node's parent exists. */
};

/**
 * @brief Create read functions for compiled tree.
 */
#define TRP_TRT_FP_READ_COMPILED \
    (struct trt_fp_read) { \
        .module_name = tro_read_module_name, \
        .node = troc_read_node, \
        .if_sibling_exists = troc_read_if_sibling_exists, \
        .if_parent_exists = tro_read_if_sibling_exists \
    }

/**
 * @brief Create read functions for parsed tree.
 */
#define TRP_TRT_FP_READ_PARSED \
    (struct trt_fp_read) { \
        .module_name = tro_read_module_name, \
        .node = trop_read_node, \
        .if_sibling_exists = trop_read_if_sibling_exists, \
        .if_parent_exists = tro_read_if_sibling_exists \
    }

/**********************************************************************
 * All getters
 *********************************************************************/

/**
 * @brief A set of all necessary functions that must be provided
 * for the printer.
 */
struct trt_fp_all {
    struct trt_fp_modify_ctx modify;    /**< Function pointers which modify state of trt_tree_ctx. */
    struct trt_fp_read read;            /**< Function pointers which only reads state of trt_tree_ctx. */
    struct trt_fp_print print;          /**< Functions pointers for printing special items in node. */
};

/**********************************************************************
 * Printer context
 *********************************************************************/

/**
 * @brief Main structure for @ref TRP_trp part.
 */
struct trt_printer_ctx {
    struct ly_out *out;     /**< Handler to printing. */
    struct trt_fp_all fp;   /**< @ref TRP_tro functions callbacks. */
    size_t max_line_length; /**< The maximum number of characters that can be
                               printed on one line, including the last. */
};

/**********************************************************************
 * Tro functions
 *********************************************************************/

/**
 * @brief The name of the section to which the node belongs.
 */
typedef enum {
    TRD_SECT_MODULE = 0,    /**< The node belongs to the "module: <module_name>:" label. */
    TRD_SECT_AUGMENT,       /**< The node belongs to some "augment <target-node>:" label. */
    TRD_SECT_RPCS,          /**< The node belongs to the "rpcs:" label. */
    TRD_SECT_NOTIF,         /**< The node belongs to the "notifications:" label. */
    TRD_SECT_GROUPING,      /**< The node belongs to some "grouping <grouping-name>:" label. */
    TRD_SECT_PLUG_DATA      /**< The node belongs to some plugin section. */
} trt_actual_section;

/**
 * @brief Types of nodes that have some effect on their children.
 */
typedef enum {
    TRD_ANCESTOR_ELSE = 0,      /**< Everything not listed. */
    TRD_ANCESTOR_RPC_INPUT,     /**< ::LYS_INPUT */
    TRD_ANCESTOR_RPC_OUTPUT,    /**< ::LYS_OUTPUT */
    TRD_ANCESTOR_NOTIF          /**< ::LYS_NOTIF */
} trt_ancestor_type;

/**
 * @brief Saved information when browsing the tree downwards.
 *
 * This structure helps prevent frequent retrieval of information
 * from the tree. Functions @ref TRP_trb are designed to preserve
 * this structures during their recursive calls. This functions do not
 * interfere in any way with this data. This structure
 * is used by @ref TRP_trop functions which, thanks to this
 * structure, can return a node with the correct data. The word
 * \b parent is in the structure name, because this data refers to
 * the last parent and at the same time the states of its
 * ancestors data. Only the function jumping on the child
 * (next_child(...)) creates this structure, because the pointer
 * to the current node moves down the tree. It's like passing
 * the genetic code to children. Some data must be inherited and
 * there are two approaches to this problem. Either it will always
 * be determined which inheritance states belong to the current node
 * (which can lead to regular travel to the root node) or
 * the inheritance states will be stored during the recursive calls.
 * So the problem was solved by the second option. Why does
 * the structure contain this data? Because it walks through
 * the lysp tree. For walks through the lysc tree is trt_parent_cache
 * useless.
 *
 * @see TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child
 */
struct trt_parent_cache {
    trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */
    uint16_t lys_status;        /**< Inherited status CURR, DEPRC, OBSLT. */
    uint16_t lys_config;        /**< Inherited config W or R. */
    const struct lysp_node_list *last_list; /**< The last ::LYS_LIST passed. */
};

/**
 * @brief Return trt_parent_cache filled with default values.
 */
#define TRP_EMPTY_PARENT_CACHE \
    (struct trt_parent_cache) { \
        .ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \
        .lys_config = LYS_CONFIG_W, .last_list = NULL \
    }

/**
 * @brief Node override from plugin.
 */
struct lyplg_ext_sprinter_tree_node_override {
    const char *flags;        /**< Override for \<flags\>. */
    const char *add_opts;     /**< Additional symbols for \<opts\>. */
};

/**
 * @brief Context for plugin extension.
 */
struct trt_plugin_ctx {
    struct lyspr_tree_ctx *ctx;                                 /**< Pointer to main context. */
    struct lyspr_tree_schema *schema;                           /**< Current schema to print. */
    ly_bool filtered;                                           /**< Flag if current node is filtered. */
    struct lyplg_ext_sprinter_tree_node_override node_overr;    /**< Current node override. */
    ly_bool last_schema;                                        /**< Flag if schema is last. */
    ly_bool last_error;                                         /**< Last error from plugin. */
};

/**
 * @brief Main structure for browsing the libyang tree
 */
struct trt_tree_ctx {
    ly_bool lysc_tree;                              /**< The lysc nodes are used for browsing through the tree.
                                                         It is assumed that once set, it does not change.
                                                         If it is true then trt_tree_ctx.pn and
                                                         trt_tree_ctx.tpn are not used.
                                                         If it is false then trt_tree_ctx.cn is not used. */
    trt_actual_section section;                     /**< To which section pn points. */
    const struct lysp_module *pmod;                 /**< Parsed YANG schema tree. */
    const struct lysc_module *cmod;                 /**< Compiled YANG schema tree. */
    const struct lysp_node *pn;                     /**< Actual pointer to parsed node. */
    const struct lysp_node *tpn;                    /**< Pointer to actual top-node. */
    const struct lysc_node *cn;                     /**< Actual pointer to compiled node. */
    LY_ERR last_error;                              /**< Error value during printing. */

    struct trt_plugin_ctx plugin_ctx;               /**< Context for plugin. */
};

/**
 * @brief Create empty node override.
 */
#define TRP_TREE_CTX_EMPTY_NODE_OVERR \
    (struct lyplg_ext_sprinter_tree_node_override) { \
        .flags = NULL, \
        .add_opts = NULL, \
    }

/**
 * @brief Check if lysp node is available from
 * the current compiled node.
 *
 * Use only if trt_tree_ctx.lysc_tree is set to true.
 */
#define TRP_TREE_CTX_LYSP_NODE_PRESENT(CN) \
    (CN->priv)

/**
 * @brief Get lysp_node from trt_tree_ctx.cn.
 *
 * Use only if :TRP_TREE_CTX_LYSP_NODE_PRESENT returns true
 * for that node.
 */
#define TRP_TREE_CTX_GET_LYSP_NODE(CN) \
    ((const struct lysp_node *)CN->priv)

/** Getter function for ::trop_node_charptr(). */
typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn);

/**
 * @brief Simple getter functions for lysp and lysc nodes.
 *
 * This structure is useful if we have a general algorithm
 * (tro function) that can be used for both lysc and lysp nodes.
 * Thanks to this structure, we prevent code redundancy.
 * We don't have to write basically the same algorithm twice
 * for lysp and lysc trees.
 */
struct tro_getters {
    uint16_t (*nodetype)(const void *);         /**< Get nodetype. */
    const void *(*next)(const void *);          /**< Get sibling. */
    const void *(*parent)(const void *);        /**< Get parent. */
    const void *(*child)(const void *);         /**< Get child. */
    const void *(*actions)(const void *);       /**< Get actions. */
    const void *(*action_input)(const void *);  /**< Get input action from action node. */
    const void *(*action_output)(const void *); /**< Get output action from action node. */
    const void *(*notifs)(const void *);        /**< Get notifs. */
};

/**********************************************************************
 * Definition of the general Trg functions
 *********************************************************************/

/**
 * @brief Print a substring but limited to the maximum length.
 * @param[in] str is pointer to source.
 * @param[in] len is number of characters to be printed.
 * @param[in,out] out is output handler.
 * @return str parameter shifted by len.
 */
static const char *
trg_print_substr(const char *str, size_t len, struct ly_out *out)
{
    for (size_t i = 0; i < len; i++) {
        ly_print_(out, "%c", str[0]);
        str++;
    }
    return str;
}

/**
 * @brief Pointer is not NULL and does not point to an empty string.
 * @param[in] str is pointer to string to be checked.
 * @return 1 if str pointing to non empty string otherwise 0.
 */
static ly_bool
trg_charptr_has_data(const char *str)
{
    return (str) && (str[0] != '\0');
}

/**
 * @brief Check if @p word in @p src is present where words are
 * delimited by @p delim.
 * @param[in] src is source where words are separated by @p delim.
 * @param[in] word to be searched.
 * @param[in] delim is delimiter between @p words in @p src.
 * @return 1 if src contains @p word otherwise 0.
 */
static ly_bool
trg_word_is_present(const char *src, const char *word, char delim)
{
    const char *hit;

    if ((!src) || (src[0] == '\0') || (!word)) {
        return 0;
    }

    hit = strstr(src, word);

    if (hit) {
        /* word was founded at the begin of src
         * OR it match somewhere after delim
         */
        if ((hit == src) || (hit[-1] == delim)) {
            /* end of word was founded at the end of src
             * OR end of word was match somewhere before delim
             */
            char delim_or_end = (hit + strlen(word))[0];

            if ((delim_or_end == '\0') || (delim_or_end == delim)) {
                return 1;
            }
        }
        /* after -> hit is just substr and it's not the whole word */
        /* jump to the next word */
        for ( ; (src[0] != '\0') && (src[0] != delim); src++) {}
        /* skip delim */
        src = src[0] == '\0' ? src : src + 1;
        /* continue with searching */
        return trg_word_is_present(src, word, delim);
    } else {
        return 0;
    }
}

/**********************************************************************
 * Definition of printer functions
 *********************************************************************/

/**
 * @brief Write callback for ::ly_out_new_clb().
 *
 * @param[in] user_data is type of struct ly_out_clb_arg.
 * @param[in] buf contains input characters
 * @param[in] count is number of characters in buf.
 * @return Number of printed bytes.
 * @return Negative value in case of error.
 */
static ssize_t
trp_ly_out_clb_func(void *user_data, const void *buf, size_t count)
{
    LY_ERR erc = LY_SUCCESS;
    struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data;

    switch (data->mode) {
    case TRD_PRINT:
        erc = ly_write_(data->out, buf, count);
        break;
    case TRD_CHAR_COUNT:
        data->counter = data->counter + count;
        break;
    default:
        break;
    }

    if (erc != LY_SUCCESS) {
        data->last_error = erc;
        return -1;
    } else {
        return count;
    }
}

/**
 * @brief Check that indent in node can be considered as equivalent.
 * @param[in] first is the first indent in node.
 * @param[in] second is the second indent in node.
 * @return 1 if indents are equivalent otherwise 0.
 */
static ly_bool
trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second)
{
    const ly_bool a = first.type == second.type;
    const ly_bool b = first.btw_name_opts == second.btw_name_opts;
    const ly_bool c = first.btw_opts_type == second.btw_opts_type;
    const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures;

    return a && b && c && d;
}

/**
 * @brief Setting space character because node is last sibling.
 * @param[in] wr is wrapper over which the shift operation
 * is to be performed.
 * @return New shifted wrapper.
 */
static struct trt_wrapper
trp_wrapper_set_shift(struct trt_wrapper wr)
{
    assert(wr.actual_pos < 64);
    /* +--<node>
     *    +--<node>
     */
    wr.actual_pos++;
    return wr;
}

/**
 * @brief Setting '|' symbol because node is divided or
 * it is not last sibling.
 * @param[in] wr is source of wrapper.
 * @return New wrapper which is marked at actual position and shifted.
 */
static struct trt_wrapper
trp_wrapper_set_mark(struct trt_wrapper wr)
{
    assert(wr.actual_pos < 64);
    wr.bit_marks1 |= 1U << wr.actual_pos;
    return trp_wrapper_set_shift(wr);
}

/**
 * @brief Setting ' ' symbol if node is last sibling otherwise set '|'.
 * @param[in] wr is actual wrapper.
 * @param[in] last_one is flag. Value 1 saying if the node is the last
 * and has no more siblings.
 * @return New wrapper for the actual node.
 */
static struct trt_wrapper
trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one)
{
    return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
}

/**
 * @brief Test if the wrappers are equivalent.
 * @param[in] first is the first wrapper.
 * @param[in] second is the second wrapper.
 * @return 1 if the wrappers are equivalent otherwise 0.
 */
static ly_bool
trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second)
{
    const ly_bool a = first.type == second.type;
    const ly_bool b = first.bit_marks1 == second.bit_marks1;
    const ly_bool c = first.actual_pos == second.actual_pos;

    return a && b && c;
}

/**
 * @brief Print "  |  " sequence on line.
 * @param[in] wr is wrapper to be printed.
 * @param[in,out] out is output handler.
 */
static void
trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out)
{
    uint32_t lb;

    if (wr.type == TRD_WRAPPER_TOP) {
        lb = TRD_INDENT_LINE_BEGIN;
    } else if (wr.type == TRD_WRAPPER_BODY) {
        lb = TRD_INDENT_LINE_BEGIN * 2;
    } else {
        lb = TRD_INDENT_LINE_BEGIN;
    }

    ly_print_(out, "%*c", lb, ' ');

    if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) {
        return;
    }

    for (uint32_t i = 0; i < wr.actual_pos; i++) {
        /** Test if the bit on the index is set. */
        if ((wr.bit_marks1 >> i) & 1U) {
            ly_print_(out, "|");
        } else {
            ly_print_(out, " ");
        }

        if (i != wr.actual_pos) {
            ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
        }
    }
}

/**
 * @brief Check if struct trt_node is empty.
 * @param[in] node is item to test.
 * @return 1 if node is considered empty otherwise 0.
 */
static ly_bool
trp_node_is_empty(const struct trt_node *node)
{
    const ly_bool a = TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(node->iffeatures.type);
    const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node->type);
    const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node->name);
    const ly_bool d = node->flags == NULL;
    const ly_bool e = node->status == NULL;

    return a && b && c && d && e;
}

/**
 * @brief Check if [\<keys\>], \<type\> and
 * \<iffeatures\> are empty/not_set.
 * @param[in] node is item to test.
 * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\>
 * otherwise 0.
 */
static ly_bool
trp_node_body_is_empty(const struct trt_node *node)
{
    const ly_bool a = TRP_EMPTY_TRT_IFFEATURES_IS_EMPTY(node->iffeatures.type);
    const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node->type);
    const ly_bool c = !node->name.keys;

    return a && b && c;
}

/**
 * @brief Print entire struct trt_node_name structure.
 * @param[in] node_name is item to print.
 * @param[in,out] out is output handler.
 */
static void
trp_print_node_name(struct trt_node_name node_name, struct ly_out *out)
{
    const char *mod_prefix;
    const char *colon;
    const char trd_node_name_suffix_choice[] = ")";
    const char trd_node_name_suffix_case[] = ")";

    if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
        return;
    }

    if (node_name.module_prefix) {
        mod_prefix = node_name.module_prefix;
        colon = ":";
    } else {
        mod_prefix = "";
        colon = "";
    }

    switch (node_name.type) {
    case TRD_NODE_ELSE:
        ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str);
        break;
    case TRD_NODE_CASE:
        ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case);
        break;
    case TRD_NODE_CHOICE:
        ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE,  mod_prefix, colon, node_name.str, trd_node_name_suffix_choice);
        break;
    case TRD_NODE_TRIPLE_DOT:
        ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT);
        break;
    default:
        break;
    }

    if (node_name.add_opts) {
        ly_print_(out, "%s", node_name.add_opts);
    }
    if (node_name.opts) {
        ly_print_(out, "%s", node_name.opts);
    }
}

/**
 * @brief Check if mark (?, !, *, /, @) is implicitly contained in
 * struct trt_node_name.
 * @param[in] node_name is structure containing the 'mark'.
 * @return 1 if contain otherwise 0.
 */
static ly_bool
trp_mark_is_used(struct trt_node_name node_name)
{
    if (TRP_NODE_NAME_IS_EMPTY(node_name)) {
        return 0;
    } else if (node_name.keys) {
        return 0;
    }

    switch (node_name.type) {
    case TRD_NODE_ELSE:
    case TRD_NODE_CASE:
        return 0;
    default:
        if (node_name.add_opts || node_name.opts) {
            return 1;
        } else {
            return 0;
        }
    }
}

/**
 * @brief Print opts keys.
 * @param[in] node_name contains type of the node with his name.
 * @param[in] btw_name_opts is number of spaces between name and [keys].
 * @param[in] cf is basically a pointer to the function that prints
 * the keys.
 * @param[in,out] out is output handler.
 */
static void
trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out)
{
    if (!node_name.keys) {
        return;
    }

    /* <name><mark>___<keys>*/
    if (btw_name_opts > 0) {
        ly_print_(out, "%*c", btw_name_opts, ' ');
    }
    ly_print_(out, "[");
    cf.pf(cf.ctx, out);
    ly_print_(out, "]");
}

/**
 * @brief Print entire struct trt_type structure.
 * @param[in] type is item to print.
 * @param[in,out] out is output handler.
 */
static void
trp_print_type(struct trt_type type, struct ly_out *out)
{
    if (TRP_TRT_TYPE_IS_EMPTY(type)) {
        return;
    }

    switch (type.type) {
    case TRD_TYPE_NAME:
        ly_print_(out, "%s", type.str);
        break;
    case TRD_TYPE_TARGET:
        ly_print_(out, "-> %s", type.str);
        break;
    case TRD_TYPE_LEAFREF:
        ly_print_(out, "leafref");
    default:
        break;
    }
}

/**
 * @brief Print all iffeatures of node
 *
 * @param[in] iff is iffeatures to print.
 * @param[in] cf is basically a pointer to the function that prints the list of features.
 * @param[in,out] out is output handler.
 */
static void
trp_print_iffeatures(struct trt_iffeatures iff, struct trt_cf_print cf, struct ly_out *out)
{
    if (iff.type == TRD_IFF_PRESENT) {
        ly_print_(out, "{");
        cf.pf(cf.ctx, out);
        ly_print_(out, "}?");
    } else if (iff.type == TRD_IFF_OVERR) {
        ly_print_(out, "%s", iff.str);
    }
}

/**
 * @brief Print just \<status\>--\<flags\> \<name\> with opts mark.
 * @param[in] node contains items to print.
 * @param[in] out is output handler.
 */
static void
trp_print_node_up_to_name(const struct trt_node *node, struct ly_out *out)
{
    if (node->name.type == TRD_NODE_TRIPLE_DOT) {
        trp_print_node_name(node->name, out);
        return;
    }
    /* <status>--<flags> */
    ly_print_(out, "%s", node->status);
    ly_print_(out, "--");
    /* If the node is a case node, there is no space before the <name>
     * also case node has no flags.
     */
    if (node->flags && (node->name.type != TRD_NODE_CASE)) {
        ly_print_(out, "%s", node->flags);
        ly_print_(out, " ");
    }
    /* <name> */
    trp_print_node_name(node->name, out);
}

/**
 * @brief Print alignment (spaces) instead of
 * \<status\>--\<flags\> \<name\> for divided node.
 * @param[in] node contains items to print.
 * @param[in] out is output handler.
 */
static void
trp_print_divided_node_up_to_name(const struct trt_node *node, struct ly_out *out)
{
    uint32_t space = strlen(node->flags);

    if (node->name.type == TRD_NODE_CASE) {
        /* :(<name> */
        space += strlen(TRD_NODE_NAME_PREFIX_CASE);
    } else if (node->name.type == TRD_NODE_CHOICE) {
        /* (<name> */
        space += strlen(TRD_NODE_NAME_PREFIX_CHOICE);
    } else {
        /* _<name> */
        space += strlen(" ");
    }

    /* <name>
     * __
     */
    space += TRD_INDENT_LONG_LINE_BREAK;

    ly_print_(out, "%*c", space, ' ');
}

/**
 * @brief Print struct trt_node structure.
 * @param[in] node is item to print.
 * @param[in] pck package of functions for
 * printing [\<keys\>] and \<iffeatures\>.
 * @param[in] indent is the indent in node.
 * @param[in,out] out is output handler.
 */
static void
trp_print_node(const struct trt_node *node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out)
{
    ly_bool triple_dot;
    ly_bool divided;
    struct trt_cf_print cf_print_keys;
    struct trt_cf_print cf_print_iffeatures;

    if (trp_node_is_empty(node)) {
        return;
    }

    /* <status>--<flags> <name><opts> <type> <if-features> */
    triple_dot = node->name.type == TRD_NODE_TRIPLE_DOT;
    divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED;

    if (triple_dot) {
        trp_print_node_name(node->name, out);
        return;
    } else if (!divided) {
        trp_print_node_up_to_name(node, out);
    } else {
        trp_print_divided_node_up_to_name(node, out);
    }

    /* <opts> */
    /* <name>___<opts>*/
    cf_print_keys.ctx = pck.tree_ctx;
    cf_print_keys.pf = pck.fps.print_keys;

    trp_print_opts_keys(node->name, indent.btw_name_opts, cf_print_keys, out);

    /* <opts>__<type> */
    if (indent.btw_opts_type > 0) {
        ly_print_(out, "%*c", indent.btw_opts_type, ' ');
    }

    /* <type> */
    trp_print_type(node->type, out);

    /* <type>__<iffeatures> */
    if (indent.btw_type_iffeatures > 0) {
        ly_print_(out, "%*c", indent.btw_type_iffeatures, ' ');
    }

    /* <iffeatures> */
    cf_print_iffeatures.ctx = pck.tree_ctx;
    cf_print_iffeatures.pf = pck.fps.print_features_names;

    trp_print_iffeatures(node->iffeatures, cf_print_iffeatures, out);
}

/**
 * @brief Print keyword based on trt_keyword_stmt.type.
 * @param[in] ks is keyword statement to print.
 * @param[in,out] out is output handler
 */
static void
trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out)
{
    if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) ||
            !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) {
        ly_print_(out, "%s: ", ks.section_name);
        return;
    }

    ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' ');
    if (ks.argument) {
        ly_print_(out, "%s ", ks.section_name);
    } else {
        ly_print_(out, "%s", ks.section_name);
    }
}

/**
 * @brief Print trt_keyword_stmt.str which is string of name or path.
 * @param[in] ks is keyword statement structure.
 * @param[in] mll is max line length.
 * @param[in,out] out is output handler.
 */
static void
trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
{
    uint32_t ind_initial;
    uint32_t ind_divided;
    /* flag if path must be splitted to more lines */
    ly_bool linebreak_was_set;
    /* flag if at least one subpath was printed */
    ly_bool subpath_printed;
    /* the sum of the sizes of the substrings on the current line */
    uint32_t how_far;
    /* pointer to start of the subpath */
    const char *sub_ptr;
    /* size of subpath from sub_ptr */
    size_t sub_len;

    if ((!ks.argument) || (ks.argument[0] == '\0')) {
        return;
    }

    /* module name cannot be splitted */
    if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) {
        ly_print_(out, "%s", ks.argument);
        return;
    }

    /* after -> for trd_keyword_stmt_body do */

    /* set begin indentation */
    ind_initial = TRD_INDENT_LINE_BEGIN + strlen(ks.section_name) + 1;
    ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK;
    linebreak_was_set = 0;
    subpath_printed = 0;
    how_far = 0;
    sub_ptr = ks.argument;
    sub_len = 0;

    while (sub_ptr[0] != '\0') {
        uint32_t ind;
        /* skip slash */
        const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr;

        /* get position of the end of substr */
        tmp = strchr(tmp, '/');
        /* set correct size if this is a last substring */
        sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr);
        /* actualize sum of the substring's sizes on the current line */
        how_far += sub_len;
        /* correction due to colon character if it this is last substring */
        how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far;
        /* choose indentation which depends on
         * whether the string is printed on multiple lines or not
         */
        ind = linebreak_was_set ? ind_divided : ind_initial;
        if (ind + how_far <= mll) {
            /* printing before max line length */
            sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
            subpath_printed = 1;
        } else {
            /* printing on new line */
            if (subpath_printed == 0) {
                /* first subpath is too long
                 * but print it at first line anyway
                 */
                sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
                subpath_printed = 1;
                continue;
            }
            ly_print_(out, "\n");
            ly_print_(out, "%*c", ind_divided, ' ');
            linebreak_was_set = 1;
            sub_ptr = trg_print_substr(sub_ptr, sub_len, out);
            how_far = sub_len;
            subpath_printed = 1;
        }
    }
}

/**
 * @brief Print separator based on trt_keyword_stmt.type
 * @param[in] ks is keyword statement structure.
 * @param[in,out] out is output handler.
 */
static void
trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, struct ly_out *out)
{
    if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) {
        return;
    } else if (ks.has_node) {
        ly_print_(out, ":");
    }
}

/**
 * @brief Print entire struct trt_keyword_stmt structure.
 * @param[in] ks is item to print.
 * @param[in] mll is max line length.
 * @param[in,out] out is output handler.
 */
static void
trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out)
{
    assert(ks.section_name);
    trt_print_keyword_stmt_begin(ks, out);
    trt_print_keyword_stmt_str(ks, mll, out);
    trt_print_keyword_stmt_end(ks, out);
}

/**********************************************************************
 * Main trp functions
 *********************************************************************/

/**
 * @brief Printing one line including wrapper and node
 * which can be incomplete (divided).
 * @param[in] node is \<node\> representation.
 * @param[in] pck contains special printing functions callback.
 * @param[in] indent contains wrapper and indent in node numbers.
 * @param[in,out] out is output handler.
 */
static void
trp_print_line(const struct trt_node *node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out)
{
    trp_print_wrapper(indent.wrapper, out);
    trp_print_node(node, pck, indent.in_node, out);
}

/**
 * @brief Printing one line including wrapper and
 * \<status\>--\<flags\> \<name\>\<option_mark\>.
 * @param[in] node is \<node\> representation.
 * @param[in] wr is wrapper for printing indentation before node.
 * @param[in] out is output handler.
 */
static void
trp_print_line_up_to_node_name(const struct trt_node *node, struct trt_wrapper wr, struct ly_out *out)
{
    trp_print_wrapper(wr, out);
    trp_print_node_up_to_name(node, out);
}

/**
 * @brief Check if leafref target must be change to string 'leafref'
 * because his target string is too long.
 * @param[in] node containing leafref target.
 * @param[in] wr is wrapper for printing indentation before node.
 * @param[in] mll is max line length.
 * @param[in] out is output handler.
 * @return true if leafref must be changed to string 'leafref'.
 */
static ly_bool
trp_leafref_target_is_too_long(const struct trt_node *node, struct trt_wrapper wr, size_t mll, struct ly_out *out)
{
    size_t type_len;
    struct ly_out_clb_arg *data;

    if (node->type.type != TRD_TYPE_TARGET) {
        return 0;
    }

    /* set ly_out to counting characters */
    data = out->method.clb.arg;

    data->counter = 0;
    data->mode = TRD_CHAR_COUNT;
    /* count number of printed bytes */
    trp_print_wrapper(wr, out);
    ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' ');
    trp_print_divided_node_up_to_name(node, out);
    data->mode = TRD_PRINT;
    type_len = strlen(node->type.str);

    return data->counter + type_len > mll;
}

/**
 * @brief Get default indent in node based on node values.
 * @param[in] node is \<node\> representation.
 * @return Default indent in node assuming that the node
 * will not be divided.
 */
static struct trt_indent_in_node
trp_default_indent_in_node(const struct trt_node *node)
{
    struct trt_indent_in_node ret;
    uint32_t opts_len = 0;

    ret.type = TRD_INDENT_IN_NODE_NORMAL;

    /* btw_name_opts */
    ret.btw_name_opts = node->name.keys ? TRD_INDENT_BEFORE_KEYS : 0;

    /* btw_opts_type */
    if (!(TRP_TRT_TYPE_IS_EMPTY(node->type))) {
        if (trp_mark_is_used(node->name)) {
            opts_len += node->name.add_opts ? strlen(node->name.add_opts) : 0;
            opts_len += node->name.opts ? strlen(node->name.opts) : 0;
            ret.btw_opts_type = TRD_INDENT_BEFORE_TYPE > opts_len ? 1 : TRD_INDENT_BEFORE_TYPE - opts_len;
        } else {
            ret.btw_opts_type = TRD_INDENT_BEFORE_TYPE;
        }
    } else {
        ret.btw_opts_type = 0;
    }

    /* btw_type_iffeatures */
    ret.btw_type_iffeatures = node->iffeatures.type == TRD_IFF_PRESENT ? TRD_INDENT_BEFORE_IFFEATURES : 0;

    return ret;
}

/**
 * @brief Setting linebreaks in trt_indent_in_node.
 *
 * The order where the linebreak tag can be placed is from the end.
 *
 * @param[in] indent containing alignment lengths
 * or already linebreak marks.
 * @return indent with a newly placed linebreak tag.
 * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible
 * to place a more linebreaks.
 */
static struct trt_indent_in_node
trp_indent_in_node_place_break(struct trt_indent_in_node indent)
{
    /* somewhere must be set a line break in node */
    struct trt_indent_in_node ret = indent;

    /* gradually break the node from the end */
    if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) {
        ret.btw_type_iffeatures = TRD_LINEBREAK;
    } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) {
        ret.btw_opts_type = TRD_LINEBREAK;
    } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) {
        /* set line break between name and opts */
        ret.btw_name_opts = TRD_LINEBREAK;
    } else {
        /* it is not possible to place a more line breaks,
         * unfortunately the max_line_length constraint is violated
         */
        ret.type = TRD_INDENT_IN_NODE_FAILED;
    }
    return ret;
}

/**
 * @brief Set the first half of the node based on the linebreak mark.
 *
 * Items in the second half of the node will be empty.
 *
 * @param[in,out] innod contains information in which part of the \<node\>
 * the first half ends. Set first half of the node, indent is unchanged.
 */
static void
trp_first_half_node(struct trt_pair_indent_node *innod)
{
    if (innod->indent.btw_name_opts == TRD_LINEBREAK) {
        innod->node.type = TRP_EMPTY_TRT_TYPE;
        innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES;
    } else if (innod->indent.btw_opts_type == TRD_LINEBREAK) {
        innod->node.type = TRP_EMPTY_TRT_TYPE;
        innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES;
    } else if (innod->indent.btw_type_iffeatures == TRD_LINEBREAK) {
        innod->node.iffeatures = TRP_EMPTY_TRT_IFFEATURES;
    }
}

/**
 * @brief Set the second half of the node based on the linebreak mark.
 *
 * Items in the first half of the node will be empty.
 * Indentations belonging to the first node will be reset to zero.
 *
 * @param[in,out] innod contains information in which part of the \<node\>
 * the second half starts. Set second half of the node, indent is newly set.
 */
static void
trp_second_half_node(struct trt_pair_indent_node *innod)
{
    if (innod->indent.btw_name_opts < 0) {
        /* Logically, the information up to token <opts> should
         * be deleted, but the the trp_print_node function needs it to
         * create the correct indent.
         */
        innod->indent.btw_name_opts = 0;
        innod->indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(innod->node.type) ? 0 : TRD_INDENT_BEFORE_TYPE;
        innod->indent.btw_type_iffeatures = innod->node.iffeatures.type == TRD_IFF_NON_PRESENT ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
    } else if (innod->indent.btw_opts_type == TRD_LINEBREAK) {
        innod->indent.btw_name_opts = 0;
        innod->indent.btw_opts_type = 0;
        innod->indent.btw_type_iffeatures = innod->node.iffeatures.type == TRD_IFF_NON_PRESENT ? 0 : TRD_INDENT_BEFORE_IFFEATURES;
    } else if (innod->indent.btw_type_iffeatures == TRD_LINEBREAK) {
        innod->node.type = TRP_EMPTY_TRT_TYPE;
        innod->indent.btw_name_opts = 0;
        innod->indent.btw_opts_type = 0;
        innod->indent.btw_type_iffeatures = 0;
    }
}

/**
 * @brief Get the correct alignment for the node.
 *
 * This function is recursively called itself. It's like a backend
 * function for a function ::trp_try_normal_indent_in_node().
 *
 * @param[in] pck contains speciall callback functions for printing.
 * @param[in] wrapper contains information about '|' context.
 * @param[in] mll is max line length.
 * @param[in,out] cnt counting number of characters to print.
 * @param[in,out] out is output handler.
 * @param[in,out] innod pair of node and indentation numbers of that node.
 */
static void
trp_try_normal_indent_in_node_(struct trt_pck_print pck, struct trt_wrapper wrapper, size_t mll, size_t *cnt,
        struct ly_out *out, struct trt_pair_indent_node *innod)
{
    trp_print_line(&innod->node, pck, TRP_INIT_PCK_INDENT(wrapper, innod->indent), out);

    if (*cnt <= mll) {
        /* success */
        return;
    } else {
        innod->indent = trp_indent_in_node_place_break(innod->indent);
        if (innod->indent.type != TRD_INDENT_IN_NODE_FAILED) {
            /* erase information in node due to line break */
            trp_first_half_node(innod);
            /* check if line fits, recursive call */
            *cnt = 0;
            trp_try_normal_indent_in_node_(pck, wrapper, mll, cnt, out, innod);
            /* make sure that the result will be with the status divided
             * or eventually with status failed */
            innod->indent.type = innod->indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED;
        }
        return;
    }
}

/**
 * @brief Get the correct alignment for the node.
 *
 * @param[in] node is \<node\> representation.
 * @param[in] pck contains speciall callback functions for printing.
 * @param[in] indent contains wrapper and indent in node numbers.
 * @param[in] mll is max line length.
 * @param[in,out] out is output handler.
 * @param[out] innod If the node does not fit in the line, some indent variable has negative value as a line break sign
 * and therefore ::TRD_INDENT_IN_NODE_DIVIDED is set.
 * If the node fits into the line, all indent variables values has non-negative number and therefore
 * ::TRD_INDENT_IN_NODE_NORMAL is set.
 * If the node does not fit into the line, all indent variables has negative or zero values, function failed
 * and therefore ::TRD_INDENT_IN_NODE_FAILED is set.
 */
static void
trp_try_normal_indent_in_node(const struct trt_node *node, struct trt_pck_print pck, struct trt_pck_indent indent,
        size_t mll, struct ly_out *out, struct trt_pair_indent_node *innod)
{
    struct ly_out_clb_arg *data;

    *innod = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, *node);

    /* set ly_out to counting characters */
    data = out->method.clb.arg;

    data->counter = 0;
    data->mode = TRD_CHAR_COUNT;
    trp_try_normal_indent_in_node_(pck, indent.wrapper, mll, &data->counter, out, innod);
    data->mode = TRD_PRINT;
}

/**
 * @brief Auxiliary function for ::trp_print_entire_node()
 * that prints split nodes.
 * @param[in] node is node representation.
 * @param[in] ppck contains speciall callback functions for printing.
 * @param[in] ipck contains wrapper and indent in node numbers.
 * @param[in] mll is max line length.
 * @param[in,out] out is output handler.
 */
static void
trp_print_divided_node(const struct trt_node *node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out)
{
    ly_bool entire_node_was_printed;
    struct trt_pair_indent_node innod;

    trp_try_normal_indent_in_node(node, ppck, ipck, mll, out, &innod);

    if (innod.indent.type == TRD_INDENT_IN_NODE_FAILED) {
        /* nothing can be done, continue as usual */
        innod.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
    }

    trp_print_line(&innod.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent), out);
    entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, innod.indent);

    if (!entire_node_was_printed) {
        ly_print_(out, "\n");
        /* continue with second half node */
        innod.node = *node;
        trp_second_half_node(&innod);
        /* continue with printing node */
        trp_print_divided_node(&innod.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent), mll, out);
    } else {
        return;
    }
}

/**
 * @brief Printing of the wrapper and the whole node,
 * which can be divided into several lines.
 * @param[in] node_p is node representation.
 * @param[in] ppck contains speciall callback functions for printing.
 * @param[in] ipck contains wrapper and indent in node numbers.
 * @param[in] mll is max line length.
 * @param[in,out] out is output handler.
 */
static void
trp_print_entire_node(const struct trt_node *node_p, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll,
        struct ly_out *out)
{
    struct trt_pair_indent_node innod;
    struct trt_pck_indent tmp;
    struct trt_node node;

    node = *node_p;
    if (trp_leafref_target_is_too_long(&node, ipck.wrapper, mll, out)) {
        node.type.type = TRD_TYPE_LEAFREF;
    }

    /* check if normal indent is possible */
    trp_try_normal_indent_in_node(&node, ppck, ipck, mll, out, &innod);

    if (innod.indent.type == TRD_INDENT_IN_NODE_NORMAL) {
        /* node fits to one line */
        trp_print_line(&node, ppck, ipck, out);
    } else if (innod.indent.type == TRD_INDENT_IN_NODE_DIVIDED) {
        /* node will be divided */
        /* print first half */
        tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, innod.indent);
        /* pretend that this is normal node */
        tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL;

        trp_print_line(&innod.node, ppck, tmp, out);
        ly_print_(out, "\n");

        /* continue with second half on new line */
        innod.node = node;
        trp_second_half_node(&innod);
        tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), innod.indent);

        trp_print_divided_node(&innod.node, ppck, tmp, mll, out);
    } else if (innod.indent.type == TRD_INDENT_IN_NODE_FAILED) {
        /* node name is too long */
        trp_print_line_up_to_node_name(&node, ipck.wrapper, out);

        if (trp_node_body_is_empty(&node)) {
            return;
        } else {
            ly_print_(out, "\n");

            innod.node = node;
            trp_second_half_node(&innod);
            innod.indent.type = TRD_INDENT_IN_NODE_DIVIDED;
            tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), innod.indent);

            trp_print_divided_node(&innod.node, ppck, tmp, mll, out);
        }
    }
}

/**
 * @brief Check if parent-stmt is valid for printing extensinon.
 *
 * @param[in] lysc_tree flag if ext is from compiled tree.
 * @param[in] ext Extension to check.
 * @return 1 if extension is valid.
 */
static ly_bool
trp_ext_parent_is_valid(ly_bool lysc_tree, void *ext)
{
    enum ly_stmt parent_stmt;

    if (lysc_tree) {
        parent_stmt = ((struct lysc_ext_instance *)ext)->parent_stmt;
    } else {
        parent_stmt = ((struct lysp_ext_instance *)ext)->parent_stmt;
    }
    if ((parent_stmt & LY_STMT_OP_MASK) || (parent_stmt & LY_STMT_DATA_NODE_MASK) ||
            (parent_stmt & LY_STMT_SUBMODULE) || parent_stmt & LY_STMT_MODULE) {
        return 1;
    } else {
        return 0;
    }
}

/**
 * @brief Check if printer_tree can use node extension.
 *
 * @param[in] lysc_tree Flag if @p node is compiled.
 * @param[in] node to check. Its type is lysc_node or lysp_node.
 * @return Pointer to extension instance which printer_tree can used.
 */
static void *
trp_ext_is_present(ly_bool lysc_tree, const void *node)
{
    const struct lysp_node *pn;
    const struct lysc_node *cn;
    LY_ARRAY_COUNT_TYPE i;
    void *ret = NULL;

    if (!node) {
        return NULL;
    }

    if (lysc_tree) {
        cn = (const struct lysc_node *)node;
        LY_ARRAY_FOR(cn->exts, i) {
            if (!(cn->exts && cn->exts->def->plugin && cn->exts->def->plugin->printer_ctree)) {
                continue;
            }
            if (!trp_ext_parent_is_valid(1, &cn->exts[i])) {
                continue;
            }
            ret = &cn->exts[i];
            break;
        }
    } else {
        pn = (const struct lysp_node *)node;
        LY_ARRAY_FOR(pn->exts, i) {
            if (!(pn->exts && pn->exts->record && pn->exts->record->plugin.printer_ptree)) {
                continue;
            }
            if (!trp_ext_parent_is_valid(0, &pn->exts[i])) {
                continue;
            }
            ret = &pn->exts[i];
            break;
        }
    }

    return ret;
}

/**
 * @brief Check if printer_tree can use node extension.
 *
 * @param[in] tc Context with current node.
 * @return 1 if some extension for printer_tree is valid.
 */
static ly_bool
trp_ext_is_present_in_node(struct trt_tree_ctx *tc)
{
    if (tc->lysc_tree && trp_ext_is_present(tc->lysc_tree, tc->cn)) {
        return 1;
    } else if (trp_ext_is_present(tc->lysc_tree, tc->pn)) {
        return 1;
    }

    return 0;
}

/**
 * @brief Release allocated memory and set pointers to NULL.
 *
 * @param[in,out] overr is override structure to release.
 * @param[out] filtered is flag to reset.
 */
static void
trp_ext_free_node_override(struct lyplg_ext_sprinter_tree_node_override *overr, ly_bool *filtered)
{
    *filtered = 0;
    overr->flags = NULL;
    overr->add_opts = NULL;
}

/**
 * @brief Release private plugin data.
 *
 * @param[in,out] plug_ctx is plugin context.
 */
static void
trp_ext_free_plugin_ctx(struct lyspr_tree_ctx *plug_ctx)
{
    LY_ARRAY_FREE(plug_ctx->schemas);
    if (plug_ctx->free_plugin_priv) {
        plug_ctx->free_plugin_priv(plug_ctx->plugin_priv);
    }
}

/**********************************************************************
 * trop and troc getters
 *********************************************************************/

/**
 * @brief Get nodetype.
 * @param[in] node is any lysp_node.
 */
static uint16_t
trop_nodetype(const void *node)
{
    return ((const struct lysp_node *)node)->nodetype;
}

/**
 * @brief Get sibling.
 * @param[in] node is any lysp_node.
 */
static const void *
trop_next(const void *node)
{
    return ((const struct lysp_node *)node)->next;
}

/**
 * @brief Get parent.
 * @param[in] node is any lysp_node.
 */
static const void *
trop_parent(const void *node)
{
    return ((const struct lysp_node *)node)->parent;
}

/**
 * @brief Try to get child.
 * @param[in] node is any lysp_node.
 */
static const void *
trop_child(const void *node)
{
    return lysp_node_child(node);
}

/**
 * @brief Try to get action.
 * @param[in] node is any lysp_node.
 */
static const void *
trop_actions(const void *node)
{
    return lysp_node_actions(node);
}

/**
 * @brief Try to get action.
 * @param[in] node must be of type lysp_node_action.
 */
static const void *
trop_action_input(const void *node)
{
    return &((const struct lysp_node_action *)node)->input;
}

/**
 * @brief Try to get action.
 * @param[in] node must be of type lysp_node_action.
 */
static const void *
trop_action_output(const void *node)
{
    return &((const struct lysp_node_action *)node)->output;
}

/**
 * @brief Try to get action.
 * @param[in] node is any lysp_node.
 */
static const void *
trop_notifs(const void *node)
{
    return lysp_node_notifs(node);
}

/**
 * @brief Fill struct tro_getters with @ref TRP_trop getters
 * which are adapted to lysp nodes.
 */
static struct tro_getters
trop_init_getters(void)
{
    return (struct tro_getters) {
               .nodetype = trop_nodetype,
               .next = trop_next,
               .parent = trop_parent,
               .child = trop_child,
               .actions = trop_actions,
               .action_input = trop_action_input,
               .action_output = trop_action_output,
               .notifs = trop_notifs
    };
}

/**
 * @brief Get nodetype.
 * @param[in] node is any lysc_node.
 */
static uint16_t
troc_nodetype(const void *node)
{
    return ((const struct lysc_node *)node)->nodetype;
}

/**
 * @brief Get sibling.
 * @param[in] node is any lysc_node.
 */
static const void *
troc_next(const void *node)
{
    return ((const struct lysc_node *)node)->next;
}

/**
 * @brief Get parent.
 * @param[in] node is any lysc_node.
 */
static const void *
troc_parent(const void *node)
{
    return ((const struct lysc_node *)node)->parent;
}

/**
 * @brief Try to get child.
 * @param[in] node is any lysc_node.
 */
static const void *
troc_child(const void *node)
{
    return lysc_node_child(node);
}

/**
 * @brief Try to get action.
 * @param[in] node is any lysc_node.
 */
static const void *
troc_actions(const void *node)
{
    return lysc_node_actions(node);
}

/**
 * @brief Try to get action.
 * @param[in] node must be of type lysc_node_action.
 */
static const void *
troc_action_input(const void *node)
{
    return &((const struct lysc_node_action *)node)->input;
}

/**
 * @brief Try to get action.
 * @param[in] node must be of type lysc_node_action.
 */
static const void *
troc_action_output(const void *node)
{
    return &((const struct lysc_node_action *)node)->output;
}

/**
 * @brief Try to get action.
 * @param[in] node is any lysc_node.
 */
static const void *
troc_notifs(const void *node)
{
    return lysc_node_notifs(node);
}

/**
 * @brief Fill struct tro_getters with @ref TRP_troc getters
 * which are adapted to lysc nodes.
 */
static struct tro_getters
troc_init_getters(void)
{
    return (struct tro_getters) {
               .nodetype = troc_nodetype,
               .next = troc_next,
               .parent = troc_parent,
               .child = troc_child,
               .actions = troc_actions,
               .action_input = troc_action_input,
               .action_output = troc_action_output,
               .notifs = troc_notifs
    };
}

/**********************************************************************
 * tro functions
 *********************************************************************/

/**
 * @brief Call override function for @p node.
 *
 * @param[in] lysc_tree if @p node is compiled.
 * @param[in] node to create override.
 * @param[in] erase_node_overr if override structure must be reseted.
 * @param[in,out] plc current plugin context.
 * @return pointer to override structure or NULL. Override structure in @p plc is updated too.
 */
static struct lyplg_ext_sprinter_tree_node_override *
tro_set_node_overr(ly_bool lysc_tree, const void *node, ly_bool erase_node_overr, struct trt_plugin_ctx *plc)
{
    LY_ERR rc = LY_SUCCESS;
    struct lyplg_ext_sprinter_tree_node_override *no;
    struct lyspr_tree_ctx *plug_ctx;
    struct lysc_ext_instance *ce;
    struct lysp_ext_instance *pe;

    if (erase_node_overr) {
        trp_ext_free_node_override(&plc->node_overr, &plc->filtered);
    }
    no = &plc->node_overr;
    if (!plc->ctx && lysc_tree && (ce = trp_ext_is_present(lysc_tree, node))) {
        rc = ce->def->plugin->printer_ctree(ce, NULL, &no->flags, &no->add_opts);
    } else if (!plc->ctx && (pe = trp_ext_is_present(lysc_tree, node))) {
        rc = pe->record->plugin.printer_ptree(pe, NULL, &no->flags, &no->add_opts);
    } else if (plc->ctx) {
        if (plc->schema && plc->schema->compiled && plc->schema->cn_overr) {
            rc = plc->schema->cn_overr(node, plc->ctx->plugin_priv, &plc->filtered, &no->flags, &no->add_opts);
        } else if (plc->schema && plc->schema->pn_overr) {
            rc = plc->schema->pn_overr(node, plc->ctx->plugin_priv, &plc->filtered, &no->flags, &no->add_opts);
        } else {
            no = NULL;
        }
        if (trp_ext_is_present(lysc_tree, node)) {
            plug_ctx = plc->ctx;
            plc->ctx = NULL;
            tro_set_node_overr(lysc_tree, node, 0, plc);
            plc->ctx = plug_ctx;
        }
    } else {
        no = NULL;
    }

    if (rc) {
        plc->last_error = rc;
        no = NULL;
    }

    return no;
}

/**
 * @brief Get next sibling of the current node.
 *
 * This is a general algorithm that is able to
 * work with lysp_node or lysc_node.
 *
 * @param[in] node points to lysp_node or lysc_node.
 * @param[in] tc current tree context.
 * @return next sibling node.
 */
static const void *
tro_next_sibling(const void *node, const struct trt_tree_ctx *tc)
{
    struct tro_getters get;
    struct trt_plugin_ctx plugin_ctx;
    const void *tmp, *parent, *sibl;

    assert(node);

    get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();

    if (get.nodetype(node) & (LYS_RPC | LYS_ACTION)) {
        if ((tmp = get.next(node))) {
            /* next action exists */
            sibl = tmp;
        } else if ((parent = get.parent(node))) {
            /* maybe if notif exists as sibling */
            sibl = get.notifs(parent);
        } else {
            sibl = NULL;
        }
    } else if (get.nodetype(node) & LYS_INPUT) {
        if ((parent = get.parent(node))) {
            /* if output action has data */
            if (get.child(get.action_output(parent))) {
                /* then next sibling is output action */
                sibl = get.action_output(parent);
            } else {
                /* input action cannot have siblings other
                 * than output action.
                 */
                sibl = NULL;
            }
        } else {
            /* there is no way how to get output action */
            sibl = NULL;
        }
    } else if (get.nodetype(node) & LYS_OUTPUT) {
        /* output action cannot have siblings */
        sibl = NULL;
    } else if (get.nodetype(node) & LYS_NOTIF) {
        /* must have as a sibling only notif */
        sibl = get.next(node);
    } else {
        /* for rest of nodes */
        if ((tmp = get.next(node))) {
            /* some sibling exists */
            sibl = tmp;
        } else if ((parent = get.parent(node))) {
            /* Action and notif are siblings too.
             * They can be reached through parent.
             */
            if ((tmp = get.actions(parent))) {
                /* next sibling is action */
                sibl = tmp;
            } else if ((tmp = get.notifs(parent))) {
                /* next sibling is notif */
                sibl = tmp;
            } else {
                /* sibling not exists */
                sibl = NULL;
            }
        } else {
            /* sibling not exists */
            sibl = NULL;
        }
    }

    plugin_ctx = tc->plugin_ctx;
    if (sibl && tro_set_node_overr(tc->lysc_tree, sibl, 1, &plugin_ctx) && plugin_ctx.filtered) {
        return tro_next_sibling(sibl, tc);
    }

    return sibl;
}

/**
 * @brief Get child of the current node.
 *
 * This is a general algorithm that is able to
 * work with lysp_node or lysc_node.
 *
 * @param[in] node points to lysp_node or lysc_node.
 * @param[in] tc current tree context.
 * @return child node.
 */
static const void *
tro_next_child(const void *node, const struct trt_tree_ctx *tc)
{
    struct tro_getters get;
    struct trt_plugin_ctx plugin_ctx;
    const void *tmp, *child;

    assert(node);

    get = tc->lysc_tree ? troc_init_getters() : trop_init_getters();

    if (get.nodetype(node) & (LYS_ACTION | LYS_RPC)) {
        if (get.child(get.action_input(node))) {
            /* go to LYS_INPUT */
            child = get.action_input(node);
        } else if (get.child(get.action_output(node))) {
            /* go to LYS_OUTPUT */
            child = get.action_output(node);
        } else {
            /* input action and output action have no data */
            child = NULL;
        }
    } else {
        if ((tmp = get.child(node))) {
            child = tmp;
        } else {
            /* current node can't have children or has no children */
            /* but maybe has some actions or notifs */
            if ((tmp = get.actions(node))) {
                child = tmp;
            } else if ((tmp = get.notifs(node))) {
                child = tmp;
            } else {
                child = NULL;
            }
        }
    }

    plugin_ctx = tc->plugin_ctx;
    if (child && tro_set_node_overr(tc->lysc_tree, child, 1, &plugin_ctx) && plugin_ctx.filtered) {
        return tro_next_sibling(child, tc);
    }

    return child;
}

/**
 * @brief Get new trt_parent_cache if we apply the transfer
 * to the child node in the tree.
 * @param[in] ca is parent cache for current node.
 * @param[in] tc contains current tree node.
 * @return Cache for the current node.
 */
static struct trt_parent_cache
tro_parent_cache_for_child(struct trt_parent_cache ca, const struct trt_tree_ctx *tc)
{
    struct trt_parent_cache ret = TRP_EMPTY_PARENT_CACHE;

    if (!tc->lysc_tree) {
        const struct lysp_node *pn = tc->pn;

        ret.ancestor =
                pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT :
                pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT :
                pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF :
                ca.ancestor;

        ret.lys_status =
                pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags :
                ca.lys_status;

        ret.lys_config =
                ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */
                ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R :
                pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags :
                ca.lys_config;

        ret.last_list =
                pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn :
                ca.last_list;
    }

    return ret;
}

/**
 * @brief Transformation of the Schema nodes flags to
 * Tree diagram \<status\>.
 * @param[in] flags is node's flags obtained from the tree.
 */
static char *
tro_flags2status(uint16_t flags)
{
    return flags & LYS_STATUS_OBSLT ? "o" :
           flags & LYS_STATUS_DEPRC ? "x" :
           "+";
}

/**
 * @brief Transformation of the Schema nodes flags to Tree diagram
 * \<flags\> but more specifically 'ro' or 'rw'.
 * @param[in] flags is node's flags obtained from the tree.
 */
static char *
tro_flags2config(uint16_t flags)
{
    return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO :
           flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW :
           TRD_FLAGS_TYPE_EMPTY;
}

/**
 * @brief Print current node's iffeatures.
 * @param[in] tc is tree context.
 * @param[in,out] out is output handler.
 */
static void
tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out)
{
    const struct lysp_qname *iffs;

    if (tc->lysc_tree) {
        assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
        iffs = TRP_TREE_CTX_GET_LYSP_NODE(tc->cn)->iffeatures;
    } else {
        iffs = tc->pn->iffeatures;
    }
    LY_ARRAY_COUNT_TYPE i;

    LY_ARRAY_FOR(iffs, i) {
        if (i == 0) {
            ly_print_(out, "%s", iffs[i].str);
        } else {
            ly_print_(out, ",%s", iffs[i].str);
        }
    }

}

/**
 * @brief Print current list's keys.
 *
 * Well, actually printing keys in the lysp_tree is trivial,
 * because char* points to all keys. However, special functions have
 * been reserved for this, because in principle the list of elements
 * can have more implementations.
 *
 * @param[in] tc is tree context.
 * @param[in,out] out is output handler.
 */
static void
tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out)
{
    const struct lysp_node_list *list;

    if (tc->lysc_tree) {
        assert(TRP_TREE_CTX_LYSP_NODE_PRESENT(tc->cn));
        list = (const struct lysp_node_list *)TRP_TREE_CTX_GET_LYSP_NODE(tc->cn);
    } else {
        list = (const struct lysp_node_list *)tc->pn;
    }
    assert(list->nodetype & LYS_LIST);

    if (trg_charptr_has_data(list->key)) {
        ly_print_(out, "%s", list->key);
    }
}

/**
 * @brief Get address of the current node.
 * @param[in] tc contains current node.
 * @return Address of lysc_node or lysp_node, or NULL.
 */
static const void *
tro_tree_ctx_get_node(const struct trt_tree_ctx *tc)
{
    return tc->lysc_tree ?
           (const void *)tc->cn :
           (const void *)tc->pn;
}

/**
 * @brief Get address of current node's child.
 * @param[in,out] tc contains current node.
 */
static const void *
tro_tree_ctx_get_child(const struct trt_tree_ctx *tc)
{
    if (!tro_tree_ctx_get_node(tc)) {
        return NULL;
    }

    if (tc->lysc_tree) {
        return lysc_node_child(tc->cn);
    } else {
        return lysp_node_child(tc->pn);
    }
}

/**
 * @brief Get rpcs section if exists.
 * @param[in,out] tc is tree context.
 * @return Section representation if it exists. The @p tc is modified
 * and his pointer points to the first node in rpcs section.
 * @return Empty section representation otherwise.
 */
static struct trt_keyword_stmt
tro_modi_get_rpcs(struct trt_tree_ctx *tc)
{
    assert(tc);
    const void *actions;
    struct trt_keyword_stmt ret = {0};

    if (tc->lysc_tree) {
        actions = tc->cmod->rpcs;
        if (actions) {
            tc->cn = actions;
        }
    } else {
        actions = tc->pmod->rpcs;
        if (actions) {
            tc->pn = actions;
            tc->tpn = tc->pn;
        }
    }

    if (actions) {
        tc->section = TRD_SECT_RPCS;
        ret.section_name = TRD_KEYWORD_RPC;
        ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0;
    }

    return ret;
}

/**
 * @brief Get notification section if exists
 * @param[in,out] tc is tree context.
 * @return Section representation if it exists.
 * The @p tc is modified and his pointer points to the
 * first node in notification section.
 * @return Empty section representation otherwise.
 */
static struct trt_keyword_stmt
tro_modi_get_notifications(struct trt_tree_ctx *tc)
{
    assert(tc);
    const void *notifs;
    struct trt_keyword_stmt ret = {0};

    if (tc->lysc_tree) {
        notifs = tc->cmod->notifs;
        if (notifs) {
            tc->cn = notifs;
        }
    } else {
        notifs = tc->pmod->notifs;
        if (notifs) {
            tc->pn = notifs;
            tc->tpn = tc->pn;
        }
    }

    if (notifs) {
        tc->section = TRD_SECT_NOTIF;
        ret.section_name = TRD_KEYWORD_NOTIF;
        ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0;
    }

    return ret;
}

static struct trt_keyword_stmt
tro_get_ext_section(struct trt_tree_ctx *tc, void *ext, struct lyspr_tree_ctx *plug_ctx)
{
    struct trt_keyword_stmt ret = {0};
    struct lysc_ext_instance *ce = NULL;
    struct lysp_ext_instance *pe = NULL;

    if (tc->lysc_tree) {
        ce = ext;
        ret.section_name = ce->def->name;
        ret.argument = ce->argument;
        ret.has_node = plug_ctx->schemas->ctree ? 1 : 0;
    } else {
        pe = ext;
        ret.section_name = pe->def->name;
        ret.argument = pe->argument;
        ret.has_node = plug_ctx->schemas->ptree ? 1 : 0;
    }

    return ret;
}

/**
 * @brief Get name of the module.
 * @param[in] tc is context of the tree.
 */
static struct trt_keyword_stmt
tro_read_module_name(const struct trt_tree_ctx *tc)
{
    assert(tc);
    struct trt_keyword_stmt ret;

    ret.section_name = !tc->lysc_tree && tc->pmod->is_submod ?
            TRD_KEYWORD_SUBMODULE :
            TRD_KEYWORD_MODULE;

    ret.argument = !tc->lysc_tree ?
            LYSP_MODULE_NAME(tc->pmod) :
            tc->cmod->mod->name;

    ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0;

    return ret;
}

static ly_bool
tro_read_if_sibling_exists(const struct trt_tree_ctx *tc)
{
    const void *parent;

    if (tc->lysc_tree) {
        parent = troc_parent(tc->cn);
    } else {
        parent = trop_parent(tc->pn);
    }

    return parent ? 1 : 0;
}

/**
 * @brief Create implicit "case" node as parent of @p node.
 * @param[in] node child of implicit case node.
 * @param[out] case_node created case node.
 */
static void
tro_create_implicit_case_node(const struct trt_node *node, struct trt_node *case_node)
{
    case_node->status = node->status;
    case_node->flags = TRD_FLAGS_TYPE_EMPTY;
    case_node->name.type = TRD_NODE_CASE;
    case_node->name.keys = node->name.keys;
    case_node->name.module_prefix = node->name.module_prefix;
    case_node->name.str = node->name.str;
    case_node->name.opts = node->name.opts;
    case_node->name.add_opts = node->name.add_opts;
    case_node->type = TRP_EMPTY_TRT_TYPE;
    case_node->iffeatures = TRP_EMPTY_TRT_IFFEATURES;
    case_node->last_one = node->last_one;
}

/**********************************************************************
 * Definition of trop reading functions
 *********************************************************************/

/**
 * @brief Check if list statement has keys.
 * @param[in] pn is pointer to the list.
 * @return 1 if has keys, otherwise 0.
 */
static ly_bool
trop_list_has_keys(const struct lysp_node *pn)
{
    return trg_charptr_has_data(((const struct lysp_node_list *)pn)->key);
}

/**
 * @brief Check if it contains at least one feature.
 * @param[in] pn is current node.
 * @return 1 if has if-features, otherwise 0.
 */
static ly_bool
trop_node_has_iffeature(const struct lysp_node *pn)
{
    LY_ARRAY_COUNT_TYPE u;
    const struct lysp_qname *iffs;

    ly_bool ret = 0;

    iffs = pn->iffeatures;
    LY_ARRAY_FOR(iffs, u) {
        ret = 1;
        break;
    }
    return ret;
}

/**
 * @brief Find out if leaf is also the key in last list.
 * @param[in] pn is pointer to leaf.
 * @param[in] ca_last_list is pointer to last visited list.
 * Obtained from trt_parent_cache.
 * @return 1 if leaf is also the key, otherwise 0.
 */
static ly_bool
trop_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list)
{
    const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;
    const struct lysp_node_list *list = ca_last_list;

    if (!list) {
        return 0;
    }
    return trg_charptr_has_data(list->key) ?
           trg_word_is_present(list->key, leaf->name, ' ') : 0;
}

/**
 * @brief Check if container's type is presence.
 * @param[in] pn is pointer to container.
 * @return 1 if container has presence statement, otherwise 0.
 */
static ly_bool
trop_container_has_presence(const struct lysp_node *pn)
{
    return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence);
}

/**
 * @brief Get leaflist's path without lysp_node type control.
 * @param[in] pn is pointer to the leaflist.
 */
static const char *
trop_leaflist_refpath(const struct lysp_node *pn)
{
    const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;

    return list->type.path ? list->type.path->expr : NULL;
}

/**
 * @brief Get leaflist's type name without lysp_node type control.
 * @param[in] pn is pointer to the leaflist.
 */
static const char *
trop_leaflist_type_name(const struct lysp_node *pn)
{
    const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn;

    return list->type.name;
}

/**
 * @brief Get leaf's path without lysp_node type control.
 * @param[in] pn is pointer to the leaf node.
 */
static const char *
trop_leaf_refpath(const struct lysp_node *pn)
{
    const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;

    return leaf->type.path ? leaf->type.path->expr : NULL;
}

/**
 * @brief Get leaf's type name without lysp_node type control.
 * @param[in] pn is pointer to the leaf's type name.
 */
static const char *
trop_leaf_type_name(const struct lysp_node *pn)
{
    const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn;

    return leaf->type.name;
}

/**
 * @brief Get pointer to data using node type specification
 * and getter function.
 *
 * @param[in] flags is node type specification.
 * If it is the correct node, the getter function is called.
 * @param[in] f is getter function which provides the desired
 * char pointer from the structure.
 * @param[in] pn pointer to node.
 * @return NULL if node has wrong type or getter function return
 * pointer to NULL.
 * @return Pointer to desired char pointer obtained from the node.
 */
static const char *
trop_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn)
{
    if (pn->nodetype & flags) {
        const char *ret = f(pn);

        return trg_charptr_has_data(ret) ? ret : NULL;
    } else {
        return NULL;
    }
}

/**
 * @brief Resolve \<status\> of the current node.
 * @param[in] nodetype is node's type obtained from the tree.
 * @param[in] flags is node's flags obtained from the tree.
 * @param[in] ca_lys_status is inherited status obtained from trt_parent_cache.
 * @return The status type.
 */
static char *
trop_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status)
{
    if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
        /* LYS_INPUT and LYS_OUTPUT is special case */
        return tro_flags2status(ca_lys_status);
        /* if ancestor's status is deprc or obslt
         * and also node's status is not set
         */
    } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
        /* get ancestor's status */
        return tro_flags2status(ca_lys_status);
    } else {
        /* else get node's status */
        return tro_flags2status(flags);
    }
}

/**
 * @brief Resolve \<flags\> of the current node.
 * @param[in] nodetype is node's type obtained from the tree.
 * @param[in] flags is node's flags obtained from the tree.
 * @param[in] ca_ancestor is ancestor type obtained from trt_parent_cache.
 * @param[in] ca_lys_config is inherited config item obtained from trt_parent_cache.
 * @param[in] no Override structure for flags.
 * @return The flags type.
 */
static const char *
trop_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config,
        struct lyplg_ext_sprinter_tree_node_override *no)
{
    if (no && no->flags) {
        return no->flags;
    } else if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) {
        return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
    } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) {
        return TRD_FLAGS_TYPE_RO;
    } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) {
        return TRD_FLAGS_TYPE_RO;
    } else if (nodetype & LYS_NOTIF) {
        return TRD_FLAGS_TYPE_NOTIF;
    } else if (nodetype & LYS_USES) {
        return TRD_FLAGS_TYPE_USES_OF_GROUPING;
    } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
        return TRD_FLAGS_TYPE_RPC;
    } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) {
        /* config is not set. Look at ancestor's config */
        return tro_flags2config(ca_lys_config);
    } else {
        return tro_flags2config(flags);
    }
}

/**
 * @brief Resolve node type of the current node.
 * @param[in] pn is pointer to the current node in the tree.
 * @param[in] ca_last_list is pointer to the last visited list. Obtained from the trt_parent_cache.
 * @param[out] type Resolved type of node.
 * @param[out] opts Resolved opts of node.
 */
static void
trop_resolve_node_opts(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list, trt_node_type *type,
        const char **opts)
{
    if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
        *type = TRD_NODE_ELSE;
    } else if (pn->nodetype & LYS_CASE) {
        *type = TRD_NODE_CASE;
    } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) {
        *type = TRD_NODE_CHOICE;
        *opts = TRD_NODE_OPTIONAL;
    } else if (pn->nodetype & LYS_CHOICE) {
        *type = TRD_NODE_CHOICE;
    } else if ((pn->nodetype & LYS_CONTAINER) && (trop_container_has_presence(pn))) {
        *opts = TRD_NODE_CONTAINER;
    } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
        *opts = TRD_NODE_LISTLEAFLIST;
    } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) {
        *opts = TRD_NODE_OPTIONAL;
    } else if ((pn->nodetype & LYS_LEAF) && !(pn->flags & LYS_MAND_TRUE) && (!trop_leaf_is_key(pn, ca_last_list))) {
        *opts = TRD_NODE_OPTIONAL;
    } else {
        *type = TRD_NODE_ELSE;
    }
}

/**
 * @brief Resolve \<type\> of the current node.
 * @param[in] pn is current node.
 * @return Resolved type.
 */
static struct trt_type
trop_resolve_type(const struct lysp_node *pn)
{
    const char *tmp = NULL;

    if (!pn) {
        return TRP_EMPTY_TRT_TYPE;
    } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_refpath, pn))) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
    } else if ((tmp = trop_node_charptr(LYS_LEAFLIST, trop_leaflist_type_name, pn))) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
    } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_refpath, pn))) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp);
    } else if ((tmp = trop_node_charptr(LYS_LEAF, trop_leaf_type_name, pn))) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp);
    } else if (pn->nodetype == LYS_ANYDATA) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata");
    } else if (pn->nodetype & LYS_ANYXML) {
        return TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml");
    } else {
        return TRP_EMPTY_TRT_TYPE;
    }
}

/**
 * @brief Resolve iffeatures.
 *
 * @param[in] pn is current parsed node.
 * @return Resolved iffeatures.
 */
static struct trt_iffeatures
trop_resolve_iffeatures(const struct lysp_node *pn)
{
    struct trt_iffeatures iff;

    if (pn && trop_node_has_iffeature(pn)) {
        iff.type = TRD_IFF_PRESENT;
        iff.str = NULL;
    } else {
        iff.type = TRD_IFF_NON_PRESENT;
        iff.str = NULL;
    }

    return iff;
}

/**
 * @brief Transformation of current lysp_node to struct trt_node.
 * @param[in] ca contains stored important data
 * when browsing the tree downwards.
 * @param[in] tc is context of the tree.
 */
static struct trt_node
trop_read_node(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    const struct lysp_node *pn;
    struct trt_node ret;
    struct lyplg_ext_sprinter_tree_node_override *no;

    assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN);

    no = tro_set_node_overr(tc->lysc_tree, tc->pn, 1, &tc->plugin_ctx);

    pn = tc->pn;
    ret = TRP_EMPTY_NODE;

    /* <status> */
    ret.status = trop_resolve_status(pn->nodetype, pn->flags, ca.lys_status);

    /* <flags> */
    ret.flags = trop_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config, no);

    /* set type of the node */
    trop_resolve_node_opts(pn, ca.last_list, &ret.name.type, &ret.name.opts);
    ret.name.add_opts = no && no->add_opts ? no->add_opts : NULL;
    ret.name.keys = (tc->pn->nodetype & LYS_LIST) && trop_list_has_keys(tc->pn);

    /* The parsed tree is not compiled, so no node can be augmented
     * from another module. This means that nodes from the parsed tree
     * will never have the prefix.
     */
    ret.name.module_prefix = NULL;

    /* set node's name */
    ret.name.str = pn->name;

    /* <type> */
    ret.type = trop_resolve_type(pn);

    /* <iffeature> */
    ret.iffeatures = trop_resolve_iffeatures(pn);

    ret.last_one = !tro_next_sibling(pn, tc);

    return ret;
}

/**
 * @brief Find out if the current node has siblings.
 * @param[in] tc is context of the tree.
 * @return 1 if sibling exists otherwise 0.
 */
static ly_bool
trop_read_if_sibling_exists(const struct trt_tree_ctx *tc)
{
    return tro_next_sibling(tc->pn, tc) != NULL;
}

/**********************************************************************
 * Modify trop getters
 *********************************************************************/

/**
 * @brief Change current node pointer to its parent
 * but only if parent exists.
 * @param[in,out] tc is tree context.
 * Contains pointer to the current node.
 * @return 1 if the node had parents and the change was successful.
 * @return 0 if the node did not have parents.
 * The pointer to the current node did not change.
 */
static ly_bool
trop_modi_parent(struct trt_tree_ctx *tc)
{
    assert(tc && tc->pn);
    /* If no parent exists, stay in actual node. */
    if ((tc->pn != tc->tpn) && (tc->pn->parent)) {
        tc->pn = tc->pn->parent;
        return 1;
    } else {
        return 0;
    }
}

/**
 * @brief Change the current node pointer to its child
 * but only if exists.
 * @param[in] ca contains inherited data from ancestors.
 * @param[in,out] tc is context of the tree.
 * Contains pointer to the current node.
 * @return Non-empty \<node\> representation of the current
 * node's child. The @p tc is modified.
 * @return Empty \<node\> representation if child don't exists.
 * The @p tc is not modified.
 */
static struct trt_node
trop_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    const struct lysp_node *tmp;

    assert(tc && tc->pn);

    if ((tmp = tro_next_child(tc->pn, tc))) {
        tc->pn = tmp;
        return trop_read_node(ca, tc);
    } else {
        return TRP_EMPTY_NODE;
    }
}

/**
 * @brief Change the pointer to the current node to its next sibling
 * only if exists.
 * @param[in] ca contains inherited data from ancestors.
 * @param[in,out] tc is tree context.
 * Contains pointer to the current node.
 * @return Non-empty \<node\> representation if sibling exists.
 * The @p tc is modified.
 * @return Empty \<node\> representation otherwise.
 * The @p tc is not modified.
 */
static struct trt_node
trop_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    const struct lysp_node *pn;

    assert(tc && tc->pn);

    pn = tro_next_sibling(tc->pn, tc);

    if (pn) {
        if ((tc->tpn == tc->pn) && (tc->section != TRD_SECT_PLUG_DATA)) {
            tc->tpn = pn;
        }
        tc->pn = pn;
        return trop_read_node(ca, tc);
    } else {
        return TRP_EMPTY_NODE;
    }
}

/**
 * @brief Change the current node pointer to the first child of node's
 * parent. If current node is already first sibling/child then nothing
 * will change.
 * @param[in] ca Settings of parent.
 * @param[in,out] tc is tree context.
 * @return node for printing.
 */
static struct trt_node
trop_modi_first_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    struct trt_node node;

    assert(tc && tc->pn);

    if (trop_modi_parent(tc)) {
        node = trop_modi_next_child(ca, tc);
    } else if (tc->plugin_ctx.schema) {
        tc->pn = tc->plugin_ctx.schema->ptree;
        tc->tpn = tc->pn;
        node = trop_read_node(ca, tc);
    } else {
        /* current node is top-node */
        switch (tc->section) {
        case TRD_SECT_MODULE:
            tc->pn = tc->pmod->data;
            tc->tpn = tc->pn;
            break;
        case TRD_SECT_AUGMENT:
            tc->pn = (const struct lysp_node *)tc->pmod->augments;
            tc->tpn = tc->pn;
            break;
        case TRD_SECT_RPCS:
            tc->pn = (const struct lysp_node *)tc->pmod->rpcs;
            tc->tpn = tc->pn;
            break;
        case TRD_SECT_NOTIF:
            tc->pn = (const struct lysp_node *)tc->pmod->notifs;
            tc->tpn = tc->pn;
            break;
        case TRD_SECT_GROUPING:
            tc->pn = (const struct lysp_node *)tc->pmod->groupings;
            tc->tpn = tc->pn;
            break;
        case TRD_SECT_PLUG_DATA:
            /* Nothing to do. */
            break;
        default:
            assert(0);
        }
        node = trop_read_node(ca, tc);
    }

    if (tc->plugin_ctx.filtered) {
        node = trop_modi_next_sibling(ca, tc);
    }

    return node;
}

/**
 * @brief Get next (or first) augment section if exists.
 * @param[in,out] tc is tree context. It is modified and his current
 * node is set to the lysp_node_augment.
 * @return Section's representation if (next augment) section exists.
 * @return Empty section structure otherwise.
 */
static struct trt_keyword_stmt
trop_modi_next_augment(struct trt_tree_ctx *tc)
{
    assert(tc);
    const struct lysp_node_augment *augs;
    struct trt_keyword_stmt ret = {0};

    /* if next_augment func was called for the first time */
    if (tc->section != TRD_SECT_AUGMENT) {
        tc->section = TRD_SECT_AUGMENT;
        augs = tc->pmod->augments;
    } else {
        /* get augment sibling from top-node pointer */
        augs = (const struct lysp_node_augment *)tc->tpn->next;
    }

    if (augs) {
        tc->pn = &augs->node;
        tc->tpn = tc->pn;
        ret.section_name = TRD_KEYWORD_AUGMENT;
        ret.argument = augs->nodeid;
        ret.has_node = tro_tree_ctx_get_node(tc) ? 1 : 0;
    }

    return ret;
}

/**
 * @brief Get next (or first) grouping section if exists
 * @param[in,out] tc is tree context. It is modified and his current
 * node is set to the lysp_node_grp.
 * @return The next (or first) section representation if it exists.
 * @return Empty section representation otherwise.
 */
static struct trt_keyword_stmt
trop_modi_next_grouping(struct trt_tree_ctx *tc)
{
    assert(tc);
    const struct lysp_node_grp *grps;
    struct trt_keyword_stmt ret = {0};

    if (tc->section != TRD_SECT_GROUPING) {
        tc->section = TRD_SECT_GROUPING;
        grps = tc->pmod->groupings;
    } else {
        grps = (const struct lysp_node_grp *)tc->tpn->next;
    }

    if (grps) {
        tc->pn = &grps->node;
        tc->tpn = tc->pn;
        ret.section_name = TRD_KEYWORD_GROUPING;
        ret.argument = grps->name;
        ret.has_node = tro_tree_ctx_get_child(tc) ? 1 : 0;
    }

    return ret;
}

/**********************************************************************
 * Definition of troc reading functions
 *********************************************************************/

/**
 * @copydoc trop_read_if_sibling_exists
 */
static ly_bool
troc_read_if_sibling_exists(const struct trt_tree_ctx *tc)
{
    return tro_next_sibling(tc->cn, tc) != NULL;
}

/**
 * @brief Resolve \<flags\> of the current node.
 *
 * Use this function only if trt_tree_ctx.lysc_tree is true.
 *
 * @param[in] nodetype is current lysc_node.nodetype.
 * @param[in] flags is current lysc_node.flags.
 * @param[in] no Override structure for flags.
 * @return The flags type.
 */
static const char *
troc_resolve_flags(uint16_t nodetype, uint16_t flags, struct lyplg_ext_sprinter_tree_node_override *no)
{
    if (no && no->flags) {
        return no->flags;
    } else if ((nodetype & LYS_INPUT) || (flags & LYS_IS_INPUT)) {
        return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS;
    } else if ((nodetype & LYS_OUTPUT) || (flags & LYS_IS_OUTPUT)) {
        return TRD_FLAGS_TYPE_RO;
    } else if (nodetype & LYS_IS_NOTIF) {
        return TRD_FLAGS_TYPE_RO;
    } else if (nodetype & LYS_NOTIF) {
        return TRD_FLAGS_TYPE_NOTIF;
    } else if (nodetype & LYS_USES) {
        return TRD_FLAGS_TYPE_USES_OF_GROUPING;
    } else if (nodetype & (LYS_RPC | LYS_ACTION)) {
        return TRD_FLAGS_TYPE_RPC;
    } else {
        return tro_flags2config(flags);
    }
}

/**
 * @brief Resolve node type of the current node.
 *
 * Use this function only if trt_tree_ctx.lysc_tree is true.
 *
 * @param[in] nodetype is current lysc_node.nodetype.
 * @param[in] flags is current lysc_node.flags.
 * @param[out] type Resolved type of node.
 * @param[out] opts Resolved opts.
 */
static void
troc_resolve_node_opts(uint16_t nodetype, uint16_t flags, trt_node_type *type, const char **opts)
{
    if (nodetype & (LYS_INPUT | LYS_OUTPUT)) {
        *type = TRD_NODE_ELSE;
    } else if (nodetype & LYS_CASE) {
        *type = TRD_NODE_CASE;
    } else if ((nodetype & LYS_CHOICE) && !(flags & LYS_MAND_TRUE)) {
        *type = TRD_NODE_CHOICE;
        *opts = TRD_NODE_OPTIONAL;
    } else if (nodetype & LYS_CHOICE) {
        *type = TRD_NODE_CHOICE;
    } else if ((nodetype & LYS_CONTAINER) && (flags & LYS_PRESENCE)) {
        *opts = TRD_NODE_CONTAINER;
    } else if (nodetype & (LYS_LIST | LYS_LEAFLIST)) {
        *opts = TRD_NODE_LISTLEAFLIST;
    } else if ((nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(flags & LYS_MAND_TRUE)) {
        *opts = TRD_NODE_OPTIONAL;
    } else if ((nodetype & LYS_LEAF) && !(flags & (LYS_MAND_TRUE | LYS_KEY))) {
        *opts = TRD_NODE_OPTIONAL;
    } else {
        *type = TRD_NODE_ELSE;
    }
}

/**
 * @brief Resolve prefix (\<prefix\>:\<name\>) of node that has been
 * placed from another module via an augment statement.
 *
 * @param[in] cn is current compiled node.
 * @param[in] current_compiled_module is module whose nodes are
 * currently being printed.
 * @return Prefix of foreign module or NULL.
 */
static const char *
troc_resolve_node_prefix(const struct lysc_node *cn, const struct lysc_module *current_compiled_module)
{
    const struct lys_module *node_module;
    const char *ret = NULL;

    node_module = cn->module;
    if (!node_module || !current_compiled_module) {
        return NULL;
    } else if (node_module->compiled != current_compiled_module) {
        ret = node_module->prefix;
    }

    return ret;
}

/**
 * @brief Transformation of current lysc_node to struct trt_node.
 * @param[in] ca is not used.
 * @param[in] tc is context of the tree.
 */
static struct trt_node
troc_read_node(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    (void) ca;
    const struct lysc_node *cn;
    struct trt_node ret;
    struct lyplg_ext_sprinter_tree_node_override *no;

    assert(tc && tc->cn);

    no = tro_set_node_overr(tc->lysc_tree, tc->cn, 1, &tc->plugin_ctx);

    cn = tc->cn;
    ret = TRP_EMPTY_NODE;

    /* <status> */
    ret.status = tro_flags2status(cn->flags);

    /* <flags> */
    ret.flags = troc_resolve_flags(cn->nodetype, cn->flags, no);

    /* set type of the node */
    troc_resolve_node_opts(cn->nodetype, cn->flags, &ret.name.type, &ret.name.opts);
    ret.name.add_opts = no && no->add_opts ? no->add_opts : NULL;
    ret.name.keys = (cn->nodetype & LYS_LIST) && !(cn->flags & LYS_KEYLESS);

    /* <prefix> */
    ret.name.module_prefix = troc_resolve_node_prefix(cn, tc->cmod);

    /* set node's name */
    ret.name.str = cn->name;

    /* <type> */
    ret.type = trop_resolve_type(TRP_TREE_CTX_GET_LYSP_NODE(cn));

    /* <iffeature> */
    ret.iffeatures = trop_resolve_iffeatures(TRP_TREE_CTX_GET_LYSP_NODE(cn));

    ret.last_one = !tro_next_sibling(cn, tc);

    return ret;
}

/**********************************************************************
 * Modify troc getters
 *********************************************************************/

/**
 * @copydoc ::trop_modi_parent()
 */
static ly_bool
troc_modi_parent(struct trt_tree_ctx *tc)
{
    assert(tc && tc->cn);
    /* If no parent exists, stay in actual node. */
    if (tc->cn->parent) {
        tc->cn = tc->cn->parent;
        return 1;
    } else {
        return 0;
    }
}

/**
 * @copydoc ::trop_modi_next_sibling()
 */
static struct trt_node
troc_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    const struct lysc_node *cn;

    assert(tc && tc->cn);

    cn = tro_next_sibling(tc->cn, tc);

    /* if next sibling exists */
    if (cn) {
        /* update trt_tree_ctx */
        tc->cn = cn;
        return troc_read_node(ca, tc);
    } else {
        return TRP_EMPTY_NODE;
    }
}

/**
 * @copydoc trop_modi_next_child()
 */
static struct trt_node
troc_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    const struct lysc_node *tmp;

    assert(tc && tc->cn);

    if ((tmp = tro_next_child(tc->cn, tc))) {
        tc->cn = tmp;
        return troc_read_node(ca, tc);
    } else {
        return TRP_EMPTY_NODE;
    }
}

/**
 * @copydoc ::trop_modi_first_sibling()
 */
static struct trt_node
troc_modi_first_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc)
{
    struct trt_node node;

    assert(tc && tc->cn);

    if (troc_modi_parent(tc)) {
        node = troc_modi_next_child(ca, tc);
    } else if (tc->plugin_ctx.schema) {
        tc->cn = tc->plugin_ctx.schema->ctree;
        node = troc_read_node(ca, tc);
    } else {
        /* current node is top-node */
        switch (tc->section) {
        case TRD_SECT_MODULE:
            tc->cn = tc->cn->module->compiled->data;
            break;
        case TRD_SECT_RPCS:
            tc->cn = (const struct lysc_node *)tc->cmod->rpcs;
            break;
        case TRD_SECT_NOTIF:
            tc->cn = (const struct lysc_node *)tc->cmod->notifs;
            break;
        case TRD_SECT_PLUG_DATA:
            /* nothing to do */
            break;
        default:
            assert(0);
        }
        node = troc_read_node(ca, tc);
    }

    if (tc->plugin_ctx.filtered) {
        node = troc_modi_next_sibling(ca, tc);
    }

    return node;
}

/**********************************************************************
 * Definition of tree browsing functions
 *********************************************************************/

static uint32_t
trb_gap_to_opts(const struct trt_node *node)
{
    uint32_t len = 0;

    if (node->name.keys) {
        return 0;
    }

    if (node->flags) {
        len += strlen(node->flags);
        /* space between flags and name */
        len += 1;
    } else {
        /* space between -- and name */
        len += 1;
    }

    switch (node->name.type) {
    case TRD_NODE_CASE:
        /* ':' is already counted. Plus parentheses. */
        len += 2;
        break;
    case TRD_NODE_CHOICE:
        /* Plus parentheses. */
        len += 2;
        break;
    default:
        break;
    }

    if (node->name.module_prefix) {
        /* prefix_name and ':' */
        len += strlen(node->name.module_prefix) + 1;
    }
    if (node->name.str) {
        len += strlen(node->name.str);
    }
    if (node->name.add_opts) {
        len += strlen(node->name.add_opts);
    }
    if (node->name.opts) {
        len += strlen(node->name.opts);
    }

    return len;
}

static uint32_t
trb_gap_to_type(const struct trt_node *node)
{
    uint32_t len, opts_len;

    if (node->name.keys) {
        return 0;
    }

    len = trb_gap_to_opts(node);
    /* Gap between opts and type. */
    opts_len = 0;
    opts_len += node->name.add_opts ? strlen(node->name.add_opts) : 0;
    opts_len += node->name.opts ? strlen(node->name.opts) : 0;
    if (opts_len >= TRD_INDENT_BEFORE_TYPE) {
        /* At least one space should be there. */
        len += 1;
    } else if (node->name.add_opts || node->name.opts) {
        len += TRD_INDENT_BEFORE_TYPE - opts_len;
    } else {
        len += TRD_INDENT_BEFORE_TYPE;
    }

    return len;
}

/**
 * @brief Calculate the trt_indent_in_node.btw_opts_type indent size
 * for a particular node.
 * @param[in] node for which we get btw_opts_type.
 * @param[in] max_gap_before_type is the maximum value of btw_opts_type
 * that it can have.
 * @return Indent between \<opts\> and \<type\> for node.
 */
static int16_t
trb_calc_btw_opts_type(const struct trt_node *node, int16_t max_gap_before_type)
{
    uint32_t to_opts_len;

    to_opts_len = trb_gap_to_opts(node);
    if (to_opts_len == 0) {
        return 1;
    } else {
        return max_gap_before_type - to_opts_len;
    }
}

/**
 * @brief Print node.
 *
 * This function is wrapper for ::trp_print_entire_node().
 * But difference is that take @p max_gap_before_type which will be
 * used to set the unified alignment.
 *
 * @param[in] node to print.
 * @param[in] max_gap_before_type is number of indent before \<type\>.
 * @param[in] wr is wrapper for printing indentation before node.
 * @param[in] pc contains mainly functions for printing.
 * @param[in] tc is tree context.
 */
static void
trb_print_entire_node(const struct trt_node *node, uint32_t max_gap_before_type, struct trt_wrapper wr,
        struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    struct trt_indent_in_node ind = trp_default_indent_in_node(node);

    if ((max_gap_before_type > 0) && (node->type.type != TRD_TYPE_EMPTY)) {
        /* print actual node with unified indent */
        ind.btw_opts_type = trb_calc_btw_opts_type(node, max_gap_before_type);
    }
    /* after -> print actual node with default indent */
    trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print),
            TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out);
}

/**
 * @brief Check if parent of the current node is the last
 * of his siblings.
 *
 * To mantain stability use this function only if the current node is
 * the first of the siblings.
 * Side-effect -> current node is set to the first sibling
 * if node has a parent otherwise no side-effect.
 *
 * @param[in] fp contains all @ref TRP_tro callback functions.
 * @param[in,out] tc is tree context.
 * @return 1 if parent is last sibling otherwise 0.
 */
static ly_bool
trb_node_is_last_sibling(const struct trt_fp_all *fp, struct trt_tree_ctx *tc)
{
    if (fp->read.if_parent_exists(tc)) {
        return !fp->read.if_sibling_exists(tc);
    } else {
        return !fp->read.if_sibling_exists(tc) && tc->plugin_ctx.last_schema;
    }
}

/**
 * @brief For all siblings find maximal space from '--' to \<type\>.
 *
 * Side-effect -> Current node is set to the first sibling.
 *
 * @param[in] ca contains inherited data from ancestors.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is tree context.
 * @return max space.
 */
static uint32_t
trb_max_gap_to_type(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    struct trt_node node;
    int32_t maxlen, len;

    maxlen = 0;
    for (node = pc->fp.modify.first_sibling(ca, tc);
            !trp_node_is_empty(&node);
            node = pc->fp.modify.next_sibling(ca, tc)) {
        len = trb_gap_to_type(&node);
        maxlen = maxlen < len ? len : maxlen;
    }
    pc->fp.modify.first_sibling(ca, tc);

    return maxlen;
}

/**
 * @brief Find out if it is possible to unify
 * the alignment before \<type\>.
 *
 * The goal is for all node siblings to have the same alignment
 * for \<type\> as if they were in a column. All siblings who cannot
 * adapt because they do not fit on the line at all are ignored.
 * Side-effect -> Current node is set to the first sibling.
 *
 * @param[in] ca contains inherited data from ancestors.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is tree context.
 * @return positive number indicating the maximum number of spaces
 * before \<type\> if the length of the flags, node name and opts is 0. To calculate
 * the trt_indent_in_node.btw_opts_type indent size for a particular
 * node, use the ::trb_calc_btw_opts_type().
*/
static uint32_t
trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    return trb_max_gap_to_type(ca, pc, tc);
}

/**
 * @brief Check if there is no case statement
 * under the choice statement.
 *
 * It can return true only if the Parsed schema tree
 * is used for browsing.
 *
 * @param[in] tc is tree context.
 * @return 1 if implicit case statement is present otherwise 0.
 */
static ly_bool
trb_need_implicit_node_case(struct trt_tree_ctx *tc)
{
    return !tc->lysc_tree && tc->pn->parent &&
           (tc->pn->parent->nodetype & LYS_CHOICE) &&
           (tc->pn->nodetype & (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER |
           LYS_LEAF | LYS_LEAFLIST));
}

static void trb_print_subtree_nodes(struct trt_node *node, uint32_t max_gap_before_type,
        struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc);

/**
 * @brief Print implicit case node and his subtree.
 *
 * @param[in] node is child of implicit case.
 * @param[in] wr is wrapper for printing identation before node.
 * @param[in] pc contains mainly functions for printing.
 * @param[in] tc is tree context. Its settings should be the same as
 * before the function call.
 * @return new indentation wrapper for @p node.
 */
static struct trt_wrapper
trb_print_implicit_node(const struct trt_node *node, struct trt_wrapper wr, struct trt_printer_ctx *pc,
        struct trt_tree_ctx *tc)
{
    struct trt_node case_node;
    struct trt_wrapper wr_case_child;

    tro_create_implicit_case_node(node, &case_node);
    ly_print_(pc->out, "\n");
    trb_print_entire_node(&case_node, 0, wr, pc, tc);
    ly_print_(pc->out, "\n");
    wr_case_child = pc->fp.read.if_sibling_exists(tc) ?
            trp_wrapper_set_mark(wr) : trp_wrapper_set_shift(wr);
    return wr_case_child;
}

/**
 * @brief Calculate the wrapper about how deep in the tree the node is.
 * @param[in] wr_in A wrapper to use as a starting point
 * @param[in] node from which to count.
 * @return wrapper for @p node.
 */
static struct trt_wrapper
trb_count_depth(const struct trt_wrapper *wr_in, const struct lysc_node *node)
{
    struct trt_wrapper wr = wr_in ? *wr_in : TRP_INIT_WRAPPER_TOP;
    const struct lysc_node *parent;

    if (!node) {
        return wr;
    }

    for (parent = node->parent; parent; parent = parent->parent) {
        wr = trp_wrapper_set_shift(wr);
    }

    return wr;
}

/**
 * @brief Print all parent nodes of @p node and the @p node itself.
 *
 * Side-effect -> trt_tree_ctx.cn will be set to @p node.
 *
 * @param[in] node on which the function is focused.
 * @param[in] wr_in for printing identation before node.
 * @param[in] pc is @ref TRP_trp settings.
 * @param[in,out] tc is context of tree printer.
 */
static void
trb_print_parents(const struct lysc_node *node, struct trt_wrapper *wr_in, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    uint32_t max_gap_before_type;
    struct trt_wrapper wr;
    struct trt_node print_node;

    assert(pc && tc && tc->section == TRD_SECT_MODULE);

    /* stop recursion */
    if (!node) {
        return;
    }
    trb_print_parents(node->parent, wr_in, pc, tc);

    /* setup for printing */
    tc->cn = node;
    wr = trb_count_depth(wr_in, node);

    /* print node */
    ly_print_(pc->out, "\n");
    print_node = pc->fp.read.node(TRP_EMPTY_PARENT_CACHE, tc);
    /* siblings do not print, so the node is always considered the last */
    print_node.last_one = 1;
    max_gap_before_type = trb_max_gap_to_type(TRP_EMPTY_PARENT_CACHE, pc, tc);
    tc->cn = node;
    trb_print_entire_node(&print_node, max_gap_before_type, wr, pc, tc);
}

/**
 * @brief Set current node on its child.
 * @param[in,out] tc contains current node.
 */
static void
trb_tree_ctx_set_child(struct trt_tree_ctx *tc)
{
    const void *node = tro_tree_ctx_get_child(tc);

    if (tc->lysc_tree) {
        tc->cn = node;
    } else {
        tc->pn = node;
    }
}

/**
 * @brief Move extension iterator to the next position.
 *
 * @param[in] lysc_tree flag if exts is from compiled tree.
 * @param[in] exts is current array of extensions.
 * @param[in,out] i is state of iterator.
 * @return Pointer to the first/next extension.
 */
static void *
trb_ext_iter_next(ly_bool lysc_tree, void *exts, uint64_t *i)
{
    void *ext = NULL;
    struct lysc_ext_instance *ce;
    struct lysp_ext_instance *pe;

    if (!exts) {
        return NULL;
    }

    if (lysc_tree) {
        ce = exts;
        while (*i < LY_ARRAY_COUNT(ce)) {
            if (ce->def->plugin && trp_ext_parent_is_valid(1, &ce[*i])) {
                ext = &ce[*i];
                break;
            }
            ++(*i);
        }
    } else {
        pe = exts;
        while (*i < LY_ARRAY_COUNT(pe)) {
            if (trp_ext_parent_is_valid(0, &pe[*i])) {
                ext = &pe[*i];
                break;
            }
            ++(*i);
        }
    }
    ++(*i);

    return ext;
}

/**
 * @brief Iterate over extensions in module.
 *
 * @param[in] tc contains current node.
 * @param[in,out] i is state of iterator.
 * @return First/next extension or NULL.
 */
static void *
trb_mod_ext_iter(const struct trt_tree_ctx *tc, uint64_t *i)
{
    if (tc->lysc_tree) {
        return trb_ext_iter_next(1, tc->cmod->exts, i);
    } else {
        return trb_ext_iter_next(0, tc->pmod->exts, i);
    }
}

/**
 * @brief Iterate over extensions in node.
 *
 * @param[in] tc contains current node.
 * @param[in,out] i is state of iterator.
 * @return First/next extension or NULL.
 */
static void *
trb_ext_iter(const struct trt_tree_ctx *tc, uint64_t *i)
{
    if (tc->lysc_tree) {
        return trb_ext_iter_next(1, tc->cn->exts, i);
    } else {
        return trb_ext_iter_next(0, tc->pn->exts, i);
    }
}

/**
 * @brief Initialize plugin context.
 *
 * @param[in] compiled if @p ext is lysc structure.
 * @param[in] ext current processed extension.
 * @param[out] plug_ctx is plugin context which will be initialized.
 * @param[out] ignore plugin callback is NULL.
 * @return LY_ERR value.
 */
static LY_ERR
tro_ext_printer_tree(ly_bool compiled, void *ext, const struct lyspr_tree_ctx *plug_ctx, ly_bool *ignore)
{
    struct lysc_ext_instance *ext_comp;
    struct lysp_ext_instance *ext_pars;
    const struct lyplg_ext *plugin;
    const char *flags = NULL, *add_opts = NULL;

    if (compiled) {
        ext_comp = ext;
        plugin = ext_comp->def->plugin;
        if (!plugin->printer_ctree) {
            *ignore = 1;
            return LY_SUCCESS;
        }
        return plugin->printer_ctree(ext, plug_ctx, &flags, &add_opts);
    } else {
        ext_pars = ext;
        plugin = &ext_pars->record->plugin;
        if (!plugin->printer_ptree) {
            *ignore = 1;
            return LY_SUCCESS;
        }
        return plugin->printer_ptree(ext, plug_ctx, &flags, &add_opts);
    }

    return LY_SUCCESS;
}

/**
 * @brief Reset tree context by plugin context.
 *
 * @param[in] plug_ctx is plugin context.
 * @param[in] i which index in schemas should be used.
 * @param[in] pc are printing functions.
 * @param[out] tc tree context which will be updated.
 */
static void
trm_reset_tree_ctx_by_plugin(struct lyspr_tree_ctx *plug_ctx, LY_ARRAY_COUNT_TYPE i, struct trt_printer_ctx *pc,
        struct trt_tree_ctx *tc)
{
    tc->plugin_ctx.ctx = plug_ctx;
    tc->pmod = NULL;
    tc->cmod = NULL;
    if (plug_ctx->schemas[i].compiled) {
        tc->lysc_tree = 1;
        tc->cn = plug_ctx->schemas[i].ctree;
        tc->plugin_ctx.schema = &plug_ctx->schemas[i];
        pc->fp.modify = TRP_TRT_FP_MODIFY_COMPILED;
        pc->fp.read = TRP_TRT_FP_READ_COMPILED;
    } else {
        tc->lysc_tree = 0;
        tc->pn = plug_ctx->schemas[i].ptree;
        tc->tpn = tc->pn;
        tc->plugin_ctx.schema = &plug_ctx->schemas[i];
        pc->fp.modify = TRP_TRT_FP_MODIFY_PARSED;
        pc->fp.read = TRP_TRT_FP_READ_PARSED;
    }
}

/**
 * @brief Print schemas from plugin context.
 *
 * @param[in] plug_ctx is plugin context.
 * @param[in] last_nodes if this schemas will be the last.
 * @param[in] max_gap_before_type is gap before type.
 * @param[in] wr is indentation wrapper.
 * @param[in] ca containing information from parent.
 * @param[in] pc functions for tree traversing.
 * @param[in] tc current tree context.
 */
static void
trb_ext_print_schemas(struct lyspr_tree_ctx *plug_ctx, ly_bool last_nodes, uint32_t max_gap_before_type,
        struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    LY_ARRAY_COUNT_TYPE i;
    struct trt_printer_ctx pc_dupl;
    struct trt_tree_ctx tc_dupl;
    struct trt_node node;

    tc_dupl = *tc;
    pc_dupl = *pc;

    LY_ARRAY_FOR(plug_ctx->schemas, i) {
        trm_reset_tree_ctx_by_plugin(plug_ctx, i, pc, tc);
        tc->plugin_ctx.last_schema = last_nodes && ((i + 1) == LY_ARRAY_COUNT(plug_ctx->schemas));
        node = TRP_EMPTY_NODE;
        trb_print_subtree_nodes(&node, max_gap_before_type, wr, ca, pc, tc);
        *tc = tc_dupl;
    }

    *pc = pc_dupl;
}

/**
 * @brief Count unified indentation across schemas from extension instance.
 *
 * @param[in] plug_ctx is plugin context.
 * @param[in] ca containing parent settings.
 * @param[out] max_gap_before_type is result of unified indent.
 * @param[in] pc functions for tree traversing.
 * @param[in] tc is tree context.
 */
static void
trb_ext_try_unified_indent(struct lyspr_tree_ctx *plug_ctx, struct trt_parent_cache ca, uint32_t *max_gap_before_type,
        struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    LY_ARRAY_COUNT_TYPE i;
    struct trt_printer_ctx pc_dupl;
    struct trt_tree_ctx tc_dupl;
    uint32_t max;

    tc_dupl = *tc;
    pc_dupl = *pc;

    LY_ARRAY_FOR(plug_ctx->schemas, i) {
        trm_reset_tree_ctx_by_plugin(plug_ctx, i, pc, tc);
        max = trb_try_unified_indent(ca, pc, tc);
        *max_gap_before_type = max > *max_gap_before_type ? max : *max_gap_before_type;
        *tc = tc_dupl;
    }

    *pc = pc_dupl;
}

/**
 * @brief For every extension instance print all schemas.
 *
 * @param[in] wr indentation wrapper for node.
 * @param[in] ca parent settings.
 * @param[in] pc function used for tree traversing.
 * @param[in] tc tree context.
 */
static void
trb_ext_print_instances(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc,
        struct trt_tree_ctx *tc)
{
    LY_ERR rc;
    LY_ARRAY_COUNT_TYPE i;
    uint64_t last_instance = UINT64_MAX;
    void *ext;
    ly_bool child_exists, ignore = 0;
    uint32_t max, max_gap_before_type = 0;

    ca = tro_parent_cache_for_child(ca, tc);
    /* if node is last sibling, then do not add '|' to wrapper */
    wr = trb_node_is_last_sibling(&pc->fp, tc) ?
            trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);

    if (tc->lysc_tree) {
        child_exists = tro_next_child(tc->cn, tc) ? 1 : 0;
    } else {
        child_exists = tro_next_child(tc->pn, tc) ? 1 : 0;
    }

    i = 0;
    while ((ext = trb_ext_iter(tc, &i))) {
        struct lyspr_tree_ctx plug_ctx = {0};

        rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore);
        LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end);
        if (ignore) {
            ignore = 0;
            continue;
        }
        trb_ext_try_unified_indent(&plug_ctx, ca, &max_gap_before_type, pc, tc);
        if (plug_ctx.schemas) {
            last_instance = i;
        }
        trp_ext_free_plugin_ctx(&plug_ctx);
    }

    if (child_exists) {
        pc->fp.modify.next_child(ca, tc);
        max = trb_try_unified_indent(ca, pc, tc);
        max_gap_before_type = max > max_gap_before_type ? max : max_gap_before_type;
        pc->fp.modify.parent(tc);
    }

    i = 0;
    while ((ext = trb_ext_iter(tc, &i))) {
        struct lyspr_tree_ctx plug_ctx = {0};

        rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore);
        LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end);
        if (ignore) {
            ignore = 0;
            continue;
        }
        if (!child_exists && (last_instance == i)) {
            trb_ext_print_schemas(&plug_ctx, 1, max_gap_before_type, wr, ca, pc, tc);
        } else {
            trb_ext_print_schemas(&plug_ctx, 0, max_gap_before_type, wr, ca, pc, tc);
        }
        trp_ext_free_plugin_ctx(&plug_ctx);
    }

end:
    return;
}

/**
 * @brief Print subtree of nodes.
 *
 * The current node is expected to be the root of the subtree.
 * Before root node is no linebreak printing. This must be addressed by
 * the caller. Root node will also be printed. Behind last printed node
 * is no linebreak.
 *
 * @param[in,out] node current processed node used as iterator.
 * @param[in] max_gap_before_type is result from
 * ::trb_try_unified_indent() function for root node.
 * Set parameter to 0 if distance does not matter.
 * @param[in] wr is wrapper saying how deep in the whole tree
 * is the root of the subtree.
 * @param[in] ca is parent_cache from root's parent.
 * If root is top-level node, insert ::TRP_EMPTY_PARENT_CACHE.
 * @param[in] pc is @ref TRP_trp settings.
 * @param[in,out] tc is context of tree printer.
 */
static void
trb_print_subtree_nodes(struct trt_node *node, uint32_t max_gap_before_type, struct trt_wrapper wr,
        struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    if (!trp_node_is_empty(node)) {
        /* Print root node. */
        trb_print_entire_node(node, max_gap_before_type, wr, pc, tc);
        if (trp_ext_is_present_in_node(tc)) {
            trb_ext_print_instances(wr, ca, pc, tc);
        }
        /* if node is last sibling, then do not add '|' to wrapper */
        wr = trb_node_is_last_sibling(&pc->fp, tc) ?
                trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr);
        /* go to the child */
        ca = tro_parent_cache_for_child(ca, tc);
        *node = pc->fp.modify.next_child(ca, tc);
        if (trp_node_is_empty(node)) {
            return;
        }
        /* TODO comment browse through instances + filtered. try unified indentation for children */
        max_gap_before_type = trb_try_unified_indent(ca, pc, tc);
    } else {
        /* Root node is ignored, continue with child. */
        *node = pc->fp.modify.first_sibling(ca, tc);
    }

    do {
        if (!tc->plugin_ctx.filtered && !trb_need_implicit_node_case(tc)) {
            /* normal behavior */
            ly_print_(pc->out, "\n");
            trb_print_subtree_nodes(node, max_gap_before_type, wr, ca, pc, tc);
        } else if (!tc->plugin_ctx.filtered) {
            struct trt_wrapper wr_case_child;

            wr_case_child = trb_print_implicit_node(node, wr, pc, tc);
            trb_print_subtree_nodes(node, max_gap_before_type, wr_case_child, ca, pc, tc);
        }
        /* go to the actual node's sibling */
        *node = pc->fp.modify.next_sibling(ca, tc);
    } while (!trp_node_is_empty(node));

    /* get back from child node to root node */
    pc->fp.modify.parent(tc);
}

/**
 * @brief Print all parents and their children.
 *
 * This function is suitable for printing top-level nodes that
 * do not have ancestors. Function call ::trb_print_subtree_nodes()
 * for all top-level siblings. Use this function after 'module' keyword
 * or 'augment' and so. The nodes may not be exactly top-level in the
 * tree, but the function considers them that way.
 *
 * @param[in] wr is wrapper saying how deeply the top-level nodes are
 * immersed in the tree.
 * @param[pc] pc contains mainly functions for printing.
 * @param[in,out] tc is tree context.
 */
static void
trb_print_family_tree(struct trt_wrapper wr, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    struct trt_parent_cache ca;
    struct trt_node node;
    uint32_t max_gap_before_type;

    if (!tro_tree_ctx_get_node(tc)) {
        return;
    }

    ca = TRP_EMPTY_PARENT_CACHE;
    max_gap_before_type = trb_try_unified_indent(ca, pc, tc);

    if (!tc->lysc_tree) {
        if ((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) {
            ca.lys_config = 0x0;
        }
    }

    for (node = pc->fp.modify.first_sibling(ca, tc);
            !trp_node_is_empty(&node);
            node = pc->fp.modify.next_sibling(ca, tc)) {
        ly_print_(pc->out, "\n");
        trb_print_subtree_nodes(&node, max_gap_before_type, wr, ca, pc, tc);
    }
}

/**********************************************************************
 * Definition of trm main functions
 *********************************************************************/

/**
 * @brief Settings if lysp_node are used for browsing through the tree.
 *
 * @param[in] module YANG schema tree structure representing
 * YANG module.
 * @param[in] out is output handler.
 * @param[in] max_line_length is the maximum line length limit
 * that should not be exceeded.
 * @param[in,out] pc will be adapted to lysp_tree.
 * @param[in,out] tc will be adapted to lysp_tree.
 */
static void
trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length,
        struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    *tc = (struct trt_tree_ctx) {
        .lysc_tree = 0,
        .section = TRD_SECT_MODULE,
        .pmod = module->parsed,
        .cmod = NULL,
        .pn = module->parsed ? module->parsed->data : NULL,
        .tpn = module->parsed ? module->parsed->data : NULL,
        .cn = NULL,
        .last_error = 0,
        .plugin_ctx.ctx = NULL,
        .plugin_ctx.schema = NULL,
        .plugin_ctx.filtered = 0,
        .plugin_ctx.node_overr = TRP_TREE_CTX_EMPTY_NODE_OVERR,
        .plugin_ctx.last_schema = 1,
        .plugin_ctx.last_error = 0
    };

    pc->out = out;

    pc->fp.modify = TRP_TRT_FP_MODIFY_PARSED;
    pc->fp.read = TRP_TRT_FP_READ_PARSED;

    pc->fp.print = (struct trt_fp_print) {
        .print_features_names = tro_print_features_names,
        .print_keys = tro_print_keys
    };

    pc->max_line_length = max_line_length;
}

/**
 * @brief Settings if lysc_node are used for browsing through the tree.
 *
 * Pointers to current nodes will be set to module data.
 *
 * @param[in] module YANG schema tree structure representing
 * YANG module.
 * @param[in] out is output handler.
 * @param[in] max_line_length is the maximum line length limit
 * that should not be exceeded.
 * @param[in,out] pc will be adapted to lysc_tree.
 * @param[in,out] tc will be adapted to lysc_tree.
 */
static void
trm_lysc_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length,
        struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    *tc = (struct trt_tree_ctx) {
        .lysc_tree = 1,
        .section = TRD_SECT_MODULE,
        .pmod = module->parsed,
        .cmod = module->compiled,
        .tpn = NULL,
        .pn = NULL,
        .cn = module->compiled->data,
        .last_error = 0,
        .plugin_ctx.ctx = NULL,
        .plugin_ctx.schema = NULL,
        .plugin_ctx.filtered = 0,
        .plugin_ctx.node_overr = TRP_TREE_CTX_EMPTY_NODE_OVERR,
        .plugin_ctx.last_schema = 1,
        .plugin_ctx.last_error = 0
    };

    pc->out = out;

    pc->fp.modify = TRP_TRT_FP_MODIFY_COMPILED;
    pc->fp.read = TRP_TRT_FP_READ_COMPILED;

    pc->fp.print = (struct trt_fp_print) {
        .print_features_names = tro_print_features_names,
        .print_keys = tro_print_keys
    };

    pc->max_line_length = max_line_length;
}

/**
 * @brief Reset settings to browsing through the lysc tree.
 * @param[in,out] pc resets to @ref TRP_troc functions.
 * @param[in,out] tc resets to lysc browsing.
 */
static void
trm_reset_to_lysc_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    LY_ERR erc;

    erc = tc->last_error;
    trp_ext_free_node_override(&tc->plugin_ctx.node_overr, &tc->plugin_ctx.filtered);
    trm_lysc_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
    tc->last_error = erc;
}

/**
 * @brief Reset settings to browsing through the lysp tree.
 * @param[in,out] pc resets to @ref TRP_trop functions.
 * @param[in,out] tc resets to lysp browsing.
 */
static void
trm_reset_to_lysp_tree_ctx(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    LY_ERR erc;

    erc = tc->last_error;
    trp_ext_free_node_override(&tc->plugin_ctx.node_overr, &tc->plugin_ctx.filtered);
    trm_lysp_tree_ctx(tc->pmod->mod, pc->out, pc->max_line_length, pc, tc);
    tc->last_error = erc;
}

/**
 * @brief If augment's target node is located on the current module.
 * @param[in] pn is examined augment.
 * @param[in] pmod is current module.
 * @return 1 if nodeid refers to the local node, otherwise 0.
 */
static ly_bool
trm_nodeid_target_is_local(const struct lysp_node_augment *pn, const struct lysp_module *pmod)
{
    const char *id, *prefix, *name;
    size_t prefix_len, name_len;
    const struct lys_module *mod;
    ly_bool ret = 0;

    if (pn == NULL) {
        return ret;
    }

    id = pn->nodeid;
    if (!id) {
        return ret;
    }
    /* only absolute-schema-nodeid is taken into account */
    assert(id[0] == '/');
    ++id;

    ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
    if (prefix) {
        mod = ly_resolve_prefix(pmod->mod->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, pmod);
        ret = mod ? (mod->parsed == pmod) : 0;
    } else {
        ret = 1;
    }

    return ret;
}

/**
 * @brief Printing section module, rpcs, notifications or yang-data.
 *
 * First node must be the first child of 'module',
 * 'rpcs', 'notifications' or 'yang-data'.
 *
 * @param[in] ks is section representation.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_section_as_family_tree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    assert(ks.section_name);

    trp_print_keyword_stmt(ks, pc->max_line_length, pc->out);
    if (!strcmp(ks.section_name, TRD_KEYWORD_MODULE) || !strcmp(ks.section_name, TRD_KEYWORD_SUBMODULE)) {
        trb_print_family_tree(TRP_INIT_WRAPPER_TOP, pc, tc);
    } else {
        trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
    }
}

/**
 * @brief Printing section augment or grouping.
 *
 * First node is 'augment' or 'grouping' itself.
 *
 * @param[in] ks is section representation.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    assert(ks.section_name);
    trp_print_keyword_stmt(ks, pc->max_line_length, pc->out);
    trb_tree_ctx_set_child(tc);
    trb_print_family_tree(TRP_INIT_WRAPPER_BODY, pc, tc);
}

/**
 * @brief Print 'module' keyword, its name and all nodes.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc);
}

/**
 * @brief For all augment sections: print 'augment' keyword,
 * its target node and all nodes.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    ly_bool once;
    ly_bool origin_was_lysc_tree = 0;
    struct trt_keyword_stmt ks;

    if (tc->lysc_tree) {
        origin_was_lysc_tree = 1;
        trm_reset_to_lysp_tree_ctx(pc, tc);
    }

    once = 1;
    for (ks = trop_modi_next_augment(tc); ks.section_name; ks = trop_modi_next_augment(tc)) {

        if (origin_was_lysc_tree) {
            /* if lysc tree is used, then only augments targeting
             * another module are printed
             */
            if (trm_nodeid_target_is_local((const struct lysp_node_augment *)tc->tpn, tc->pmod)) {
                continue;
            }
        }

        if (once) {
            ly_print_(pc->out, "\n");
            ly_print_(pc->out, "\n");
            once = 0;
        } else {
            ly_print_(pc->out, "\n");
        }

        trm_print_section_as_subtree(ks, pc, tc);
    }

    if (origin_was_lysc_tree) {
        trm_reset_to_lysc_tree_ctx(pc, tc);
    }
}

/**
 * @brief For rpcs section: print 'rpcs' keyword and all its nodes.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    struct trt_keyword_stmt rpc;

    rpc = tro_modi_get_rpcs(tc);

    if (rpc.section_name) {
        ly_print_(pc->out, "\n");
        ly_print_(pc->out, "\n");
        trm_print_section_as_family_tree(rpc, pc, tc);
    }
}

/**
 * @brief For notifications section: print 'notifications' keyword
 * and all its nodes.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    struct trt_keyword_stmt notifs;

    notifs = tro_modi_get_notifications(tc);

    if (notifs.section_name) {
        ly_print_(pc->out, "\n");
        ly_print_(pc->out, "\n");
        trm_print_section_as_family_tree(notifs, pc, tc);
    }
}

/**
 * @brief For all grouping sections: print 'grouping' keyword, its name
 * and all nodes.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    ly_bool once;
    struct trt_keyword_stmt ks;

    if (tc->lysc_tree) {
        return;
    }

    once = 1;
    for (ks = trop_modi_next_grouping(tc); ks.section_name; ks = trop_modi_next_grouping(tc)) {
        if (once) {
            ly_print_(pc->out, "\n");
            ly_print_(pc->out, "\n");
            once = 0;
        } else {
            ly_print_(pc->out, "\n");
        }
        trm_print_section_as_subtree(ks, pc, tc);
    }
}

/**
 * @brief Print all sections defined in plugins.
 *
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_plugin_ext(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    LY_ERR rc;
    ly_bool once;
    LY_ARRAY_COUNT_TYPE i = 0, j;
    struct trt_keyword_stmt ks, prev_ks = {0};
    struct trt_printer_ctx pc_dupl;
    struct trt_tree_ctx tc_dupl;
    struct trt_node node;
    ly_bool ignore = 0;
    uint32_t max_gap_before_type;
    void *ext;

    tc->section = TRD_SECT_PLUG_DATA;

    tc_dupl = *tc;
    pc_dupl = *pc;

    once = 1;

    while ((ext = trb_mod_ext_iter(tc, &i))) {
        struct lyspr_tree_ctx plug_ctx = {0};

        rc = tro_ext_printer_tree(tc->lysc_tree, ext, &plug_ctx, &ignore);
        LY_CHECK_ERR_GOTO(rc, tc->last_error = rc, end);
        if (!plug_ctx.schemas || ignore) {
            ignore = 0;
            continue;
        }

        ks = tro_get_ext_section(tc, ext, &plug_ctx);
        if (once || (prev_ks.section_name && strcmp(prev_ks.section_name, ks.section_name))) {
            ly_print_(pc->out, "\n");
            ly_print_(pc->out, "\n");
            once = 0;
        } else {
            ly_print_(pc->out, "\n");
        }
        trp_print_keyword_stmt(ks, pc->max_line_length, pc->out);

        max_gap_before_type = 0;
        trb_ext_try_unified_indent(&plug_ctx, TRP_EMPTY_PARENT_CACHE, &max_gap_before_type, pc, tc);
        LY_ARRAY_FOR(plug_ctx.schemas, j) {
            trm_reset_tree_ctx_by_plugin(&plug_ctx, j, pc, tc);
            node = TRP_EMPTY_NODE;
            trb_print_subtree_nodes(&node, max_gap_before_type, TRP_INIT_WRAPPER_BODY, TRP_EMPTY_PARENT_CACHE, pc, tc);
        }

        *tc = tc_dupl;
        trp_ext_free_plugin_ctx(&plug_ctx);
        prev_ks = ks;
    }

end:
    *pc = pc_dupl;
    return;
}

/**
 * @brief Print sections module, augment, rpcs, notifications,
 * grouping, yang-data.
 * @param[in] pc contains mainly functions for printing.
 * @param[in,out] tc is the tree context.
 */
static void
trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc)
{
    trm_print_module_section(pc, tc);
    trm_print_augmentations(pc, tc);
    trm_print_rpcs(pc, tc);
    trm_print_notifications(pc, tc);
    trm_print_groupings(pc, tc);
    trm_print_plugin_ext(pc, tc);
    ly_print_(pc->out, "\n");
}

static LY_ERR
tree_print_check_error(struct ly_out_clb_arg *out, struct trt_tree_ctx *tc)
{
    if (out->last_error) {
        return out->last_error;
    } else if (tc->last_error) {
        return tc->last_error;
    } else {
        return LY_SUCCESS;
    }
}

/**********************************************************************
 * Definition of module interface
 *********************************************************************/

LY_ERR
tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t UNUSED(options), size_t line_length)
{
    struct trt_printer_ctx pc;
    struct trt_tree_ctx tc;
    struct ly_out *new_out;
    LY_ERR erc;
    struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);

    LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL);

    if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
        return erc;
    }

    line_length = line_length == 0 ? SIZE_MAX : line_length;
    if ((module->ctx->flags & LY_CTX_SET_PRIV_PARSED) && module->compiled) {
        trm_lysc_tree_ctx(module, new_out, line_length, &pc, &tc);
    } else {
        trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc);
    }

    trm_print_sections(&pc, &tc);
    erc = tree_print_check_error(&clb_arg, &tc);

    ly_out_free(new_out, NULL, 1);

    return erc;
}

LY_ERR
tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length)
{
    struct trt_printer_ctx pc;
    struct trt_tree_ctx tc;
    struct ly_out *new_out;
    struct trt_wrapper wr;
    LY_ERR erc;
    struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);

    assert(out && node);

    if (!(node->module->ctx->flags & LY_CTX_SET_PRIV_PARSED)) {
        return LY_EINVAL;
    }

    if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
        return erc;
    }

    line_length = line_length == 0 ? SIZE_MAX : line_length;
    trm_lysc_tree_ctx(node->module, new_out, line_length, &pc, &tc);

    trp_print_keyword_stmt(pc.fp.read.module_name(&tc), pc.max_line_length, pc.out);
    trb_print_parents(node, NULL, &pc, &tc);

    if (!(options & LYS_PRINT_NO_SUBSTMT)) {
        tc.cn = lysc_node_child(node);
        wr = trb_count_depth(NULL, tc.cn);
        trb_print_family_tree(wr, &pc, &tc);
    }
    ly_print_(out, "\n");

    erc = tree_print_check_error(&clb_arg, &tc);
    ly_out_free(new_out, NULL, 1);

    return erc;
}

LY_ERR
tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t UNUSED(options),
        size_t line_length)
{
    struct trt_printer_ctx pc;
    struct trt_tree_ctx tc;
    struct ly_out *new_out;
    LY_ERR erc;
    struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS);

    assert(submodp);
    LY_CHECK_ARG_RET(submodp->mod->ctx, out, LY_EINVAL);

    if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) {
        return erc;
    }

    line_length = line_length == 0 ? SIZE_MAX : line_length;
    trm_lysp_tree_ctx(submodp->mod, new_out, line_length, &pc, &tc);
    tc.pmod = (struct lysp_module *)submodp;
    tc.tpn = submodp->data;
    tc.pn = tc.tpn;

    trm_print_sections(&pc, &tc);
    erc = tree_print_check_error(&clb_arg, &tc);

    ly_out_free(new_out, NULL, 1);

    return erc;
}