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
|
#include <assert.h>
#include <inttypes.h>
#include <msgpack/object.h>
#include <msgpack/pack.h>
#include <msgpack/sbuffer.h>
#include <msgpack/unpack.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <uv.h>
#include "auto/config.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/ascii_defs.h"
#include "nvim/buffer.h"
#include "nvim/cmdhist.h"
#include "nvim/eval.h"
#include "nvim/eval/decode.h"
#include "nvim/eval/encode.h"
#include "nvim/eval/typval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_docmd.h"
#include "nvim/fileio.h"
#include "nvim/garray.h"
#include "nvim/gettext.h"
#include "nvim/globals.h"
#include "nvim/hashtab.h"
#include "nvim/macros_defs.h"
#include "nvim/map_defs.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/option_vars.h"
#include "nvim/os/fileio.h"
#include "nvim/os/fs.h"
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/path.h"
#include "nvim/pos_defs.h"
#include "nvim/regexp.h"
#include "nvim/search.h"
#include "nvim/shada.h"
#include "nvim/strings.h"
#include "nvim/version.h"
#include "nvim/vim_defs.h"
#ifdef HAVE_BE64TOH
# define _BSD_SOURCE 1 // NOLINT(bugprone-reserved-identifier)
# define _DEFAULT_SOURCE 1 // NOLINT(bugprone-reserved-identifier)
# include ENDIAN_INCLUDE_FILE
#endif
#define SEARCH_KEY_MAGIC "sm"
#define SEARCH_KEY_SMARTCASE "sc"
#define SEARCH_KEY_HAS_LINE_OFFSET "sl"
#define SEARCH_KEY_PLACE_CURSOR_AT_END "se"
#define SEARCH_KEY_IS_LAST_USED "su"
#define SEARCH_KEY_IS_SUBSTITUTE_PATTERN "ss"
#define SEARCH_KEY_HIGHLIGHTED "sh"
#define SEARCH_KEY_OFFSET "so"
#define SEARCH_KEY_PAT "sp"
#define SEARCH_KEY_BACKWARD "sb"
#define REG_KEY_TYPE "rt"
#define REG_KEY_WIDTH "rw"
#define REG_KEY_CONTENTS "rc"
#define REG_KEY_UNNAMED "ru"
#define KEY_LNUM "l"
#define KEY_COL "c"
#define KEY_FILE "f"
#define KEY_NAME_CHAR "n"
// Error messages formerly used by viminfo code:
// E136: viminfo: Too many errors, skipping rest of file
// E137: Viminfo file is not writable: %s
// E138: Can't write viminfo file %s!
// E195: Cannot open ShaDa file for reading
// E574: Unknown register type %d
// E575: Illegal starting char
// E576: Missing '>'
// E577: Illegal register name
// E886: Can't rename viminfo file to %s!
// E929: Too many viminfo temp files, like %s!
// Now only six of them are used:
// E137: ShaDa file is not writeable (for pre-open checks)
// E929: All %s.tmp.X files exist, cannot write ShaDa file!
// RCERR (E576) for critical read errors.
// RNERR (E136) for various errors when renaming.
// RERR (E575) for various errors inside read ShaDa file.
// SERR (E886) for various “system” errors (always contains output of
// strerror)
// WERR (E574) for various ignorable write errors
/// Common prefix for all errors inside ShaDa file
///
/// I.e. errors occurred while parsing, but not system errors occurred while
/// reading.
#define RERR "E575: "
/// Common prefix for critical read errors
///
/// I.e. errors that make shada_read_next_item return kSDReadStatusNotShaDa.
#define RCERR "E576: "
/// Common prefix for all “system” errors
#define SERR "E886: "
/// Common prefix for all “rename” errors
#define RNERR "E136: "
/// Common prefix for all ignorable “write” errors
#define WERR "E574: "
/// Callback function for add_search_pattern
typedef void (*SearchPatternGetter)(SearchPattern *);
/// Possible ShaDa entry types
///
/// @warning Enum values are part of the API and must not be altered.
///
/// All values that are not in enum are ignored.
typedef enum {
kSDItemUnknown = -1, ///< Unknown item.
kSDItemMissing = 0, ///< Missing value. Should never appear in a file.
kSDItemHeader = 1, ///< Header. Present for debugging purposes.
kSDItemSearchPattern = 2, ///< Last search pattern (*not* history item).
///< Comes from user searches (e.g. when typing
///< "/pat") or :substitute command calls.
kSDItemSubString = 3, ///< Last substitute replacement string.
kSDItemHistoryEntry = 4, ///< History item.
kSDItemRegister = 5, ///< Register.
kSDItemVariable = 6, ///< Global variable.
kSDItemGlobalMark = 7, ///< Global mark definition.
kSDItemJump = 8, ///< Item from jump list.
kSDItemBufferList = 9, ///< Buffer list.
kSDItemLocalMark = 10, ///< Buffer-local mark.
kSDItemChange = 11, ///< Item from buffer change list.
} ShadaEntryType;
#define SHADA_LAST_ENTRY ((uint64_t)kSDItemChange)
/// Possible results when reading ShaDa file
typedef enum {
kSDReadStatusSuccess, ///< Reading was successful.
kSDReadStatusFinished, ///< Nothing more to read.
kSDReadStatusReadError, ///< Failed to read from file.
kSDReadStatusNotShaDa, ///< Input is most likely not a ShaDa file.
kSDReadStatusMalformed, ///< Error in the currently read item.
} ShaDaReadResult;
/// Possible results of shada_write function.
typedef enum {
kSDWriteSuccessful, ///< Writing was successful.
kSDWriteReadNotShada, ///< Writing was successful, but when reading it
///< attempted to read file that did not look like
///< a ShaDa file.
kSDWriteFailed, ///< Writing was not successful (e.g. because there
///< was no space left on device).
kSDWriteIgnError, ///< Writing resulted in a error which can be ignored
///< (e.g. when trying to dump a function reference or
///< self-referencing container in a variable).
} ShaDaWriteResult;
/// Flags for shada_read_next_item
enum SRNIFlags {
kSDReadHeader = (1 << kSDItemHeader), ///< Determines whether header should
///< be read (it is usually ignored).
kSDReadUndisableableData = (
(1 << kSDItemSearchPattern)
| (1 << kSDItemSubString)
| (1 << kSDItemJump)), ///< Data reading which cannot be disabled by
///< &shada or other options except for disabling
///< reading ShaDa as a whole.
kSDReadRegisters = (1 << kSDItemRegister), ///< Determines whether registers
///< should be read (may only be
///< disabled when writing, but
///< not when reading).
kSDReadHistory = (1 << kSDItemHistoryEntry), ///< Determines whether history
///< should be read (can only be
///< disabled by &history).
kSDReadVariables = (1 << kSDItemVariable), ///< Determines whether variables
///< should be read (disabled by
///< removing ! from &shada).
kSDReadBufferList = (1 << kSDItemBufferList), ///< Determines whether buffer
///< list should be read
///< (disabled by removing
///< % entry from &shada).
kSDReadUnknown = (1 << (SHADA_LAST_ENTRY + 1)), ///< Determines whether
///< unknown items should be
///< read (usually disabled).
kSDReadGlobalMarks = (1 << kSDItemGlobalMark), ///< Determines whether global
///< marks should be read. Can
///< only be disabled by
///< having f0 in &shada when
///< writing.
kSDReadLocalMarks = (1 << kSDItemLocalMark), ///< Determines whether local
///< marks should be read. Can
///< only be disabled by
///< disabling &shada or putting
///< '0 there. Is also used for
///< v:oldfiles.
kSDReadChanges = (1 << kSDItemChange), ///< Determines whether change list
///< should be read. Can only be
///< disabled by disabling &shada or
///< putting '0 there.
};
// Note: SRNIFlags enum name was created only to make it possible to reference
// it. This name is not actually used anywhere outside of the documentation.
/// Structure defining a single ShaDa file entry
typedef struct {
ShadaEntryType type;
Timestamp timestamp;
union {
Dictionary header;
struct shada_filemark {
char name;
pos_T mark;
char *fname;
dict_T *additional_data;
} filemark;
struct search_pattern {
bool magic;
bool smartcase;
bool has_line_offset;
bool place_cursor_at_end;
int64_t offset;
bool is_last_used;
bool is_substitute_pattern;
bool highlighted;
bool search_backward;
char *pat;
dict_T *additional_data;
} search_pattern;
struct history_item {
uint8_t histtype;
char *string;
char sep;
list_T *additional_elements;
} history_item;
struct reg { // yankreg_T
char name;
MotionType type;
char **contents;
bool is_unnamed;
size_t contents_size;
size_t width;
dict_T *additional_data;
} reg;
struct global_var {
char *name;
typval_T value;
list_T *additional_elements;
} global_var;
struct {
uint64_t type;
char *contents;
size_t size;
} unknown_item;
struct sub_string {
char *sub;
list_T *additional_elements;
} sub_string;
struct buffer_list {
size_t size;
struct buffer_list_buffer {
pos_T pos;
char *fname;
dict_T *additional_data;
} *buffers;
} buffer_list;
} data;
} ShadaEntry;
/// One entry in sized linked list
typedef struct hm_llist_entry {
ShadaEntry data; ///< Entry data.
bool can_free_entry; ///< True if data can be freed.
struct hm_llist_entry *next; ///< Pointer to next entry or NULL.
struct hm_llist_entry *prev; ///< Pointer to previous entry or NULL.
} HMLListEntry;
/// Sized linked list structure for history merger
typedef struct {
HMLListEntry *entries; ///< Pointer to the start of the allocated array of
///< entries.
HMLListEntry *first; ///< First entry in the list (is not necessary start
///< of the array) or NULL.
HMLListEntry *last; ///< Last entry in the list or NULL.
HMLListEntry *free_entry; ///< Last free entry removed by hmll_remove.
HMLListEntry *last_free_entry; ///< Last unused element in entries array.
size_t size; ///< Number of allocated entries.
size_t num_entries; ///< Number of entries already used.
PMap(cstr_t) contained_entries; ///< Map all history entry strings to
///< corresponding entry pointers.
} HMLList;
typedef struct {
HMLList hmll;
bool do_merge;
bool reading;
const void *iter;
ShadaEntry last_hist_entry;
uint8_t history_type;
} HistoryMergerState;
/// ShadaEntry structure that knows whether it should be freed
typedef struct {
ShadaEntry data; ///< ShadaEntry data.
bool can_free_entry; ///< True if entry can be freed.
} PossiblyFreedShadaEntry;
/// Structure that holds one file marks.
typedef struct {
PossiblyFreedShadaEntry marks[NLOCALMARKS]; ///< All file marks.
PossiblyFreedShadaEntry changes[JUMPLISTSIZE]; ///< All file changes.
size_t changes_size; ///< Number of changes occupied.
ShadaEntry *additional_marks; ///< All marks with unknown names.
size_t additional_marks_size; ///< Size of the additional_marks array.
Timestamp greatest_timestamp; ///< Greatest timestamp among marks.
} FileMarks;
/// State structure used by shada_write
///
/// Before actually writing most of the data is read to this structure.
typedef struct {
HistoryMergerState hms[HIST_COUNT]; ///< Structures for history merging.
PossiblyFreedShadaEntry global_marks[NMARKS]; ///< Named global marks.
PossiblyFreedShadaEntry numbered_marks[EXTRA_MARKS]; ///< Numbered marks.
PossiblyFreedShadaEntry registers[NUM_SAVED_REGISTERS]; ///< All registers.
PossiblyFreedShadaEntry jumps[JUMPLISTSIZE]; ///< All dumped jumps.
size_t jumps_size; ///< Number of jumps occupied.
PossiblyFreedShadaEntry search_pattern; ///< Last search pattern.
PossiblyFreedShadaEntry sub_search_pattern; ///< Last s/ search pattern.
PossiblyFreedShadaEntry replacement; ///< Last s// replacement string.
Set(cstr_t) dumped_variables; ///< Names of already dumped variables.
PMap(cstr_t) file_marks; ///< All file marks.
} WriteMergerState;
struct sd_read_def;
/// Function used to close files defined by ShaDaReadDef
typedef void (*ShaDaReadCloser)(struct sd_read_def *const sd_reader)
REAL_FATTR_NONNULL_ALL;
/// Function used to read ShaDa files
typedef ptrdiff_t (*ShaDaFileReader)(struct sd_read_def *const sd_reader,
void *const dest,
const size_t size)
REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
/// Function used to skip in ShaDa files
typedef int (*ShaDaFileSkipper)(struct sd_read_def *const sd_reader,
const size_t offset)
REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
/// Structure containing necessary pointers for reading ShaDa files
typedef struct sd_read_def {
ShaDaFileReader read; ///< Reader function.
ShaDaReadCloser close; ///< Close function.
ShaDaFileSkipper skip; ///< Function used to skip some bytes.
void *cookie; ///< Data describing object read from.
bool eof; ///< True if reader reached end of file.
const char *error; ///< Error message in case of error.
uintmax_t fpos; ///< Current position (amount of bytes read since
///< reader structure initialization). May overflow.
} ShaDaReadDef;
struct sd_write_def;
/// Function used to close files defined by ShaDaWriteDef
typedef void (*ShaDaWriteCloser)(struct sd_write_def *const sd_writer)
REAL_FATTR_NONNULL_ALL;
/// Function used to write ShaDa files
typedef ptrdiff_t (*ShaDaFileWriter)(struct sd_write_def *const sd_writer,
const void *const src,
const size_t size)
REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
/// Structure containing necessary pointers for writing ShaDa files
typedef struct sd_write_def {
ShaDaFileWriter write; ///< Writer function.
ShaDaWriteCloser close; ///< Close function.
void *cookie; ///< Data describing object written to.
const char *error; ///< Error message in case of error.
} ShaDaWriteDef;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "shada.c.generated.h"
#endif
#define DEF_SDE(name, attr, ...) \
[kSDItem##name] = { \
.timestamp = 0, \
.type = kSDItem##name, \
.data = { \
.attr = { __VA_ARGS__ } \
} \
}
#define DEFAULT_POS { 1, 0, 0 }
static const pos_T default_pos = DEFAULT_POS;
static const ShadaEntry sd_default_values[] = {
[kSDItemMissing] = { .type = kSDItemMissing, .timestamp = 0 },
DEF_SDE(Header, header, .size = 0),
DEF_SDE(SearchPattern, search_pattern,
.magic = true,
.smartcase = false,
.has_line_offset = false,
.place_cursor_at_end = false,
.offset = 0,
.is_last_used = true,
.is_substitute_pattern = false,
.highlighted = false,
.search_backward = false,
.pat = NULL,
.additional_data = NULL),
DEF_SDE(SubString, sub_string, .sub = NULL, .additional_elements = NULL),
DEF_SDE(HistoryEntry, history_item,
.histtype = HIST_CMD,
.string = NULL,
.sep = NUL,
.additional_elements = NULL),
DEF_SDE(Register, reg,
.name = NUL,
.type = kMTCharWise,
.contents = NULL,
.contents_size = 0,
.is_unnamed = false,
.width = 0,
.additional_data = NULL),
DEF_SDE(Variable, global_var,
.name = NULL,
.value = { .v_type = VAR_UNKNOWN, .vval = { .v_string = NULL } },
.additional_elements = NULL),
DEF_SDE(GlobalMark, filemark,
.name = '"',
.mark = DEFAULT_POS,
.fname = NULL,
.additional_data = NULL),
DEF_SDE(Jump, filemark,
.name = NUL,
.mark = DEFAULT_POS,
.fname = NULL,
.additional_data = NULL),
DEF_SDE(BufferList, buffer_list,
.size = 0,
.buffers = NULL),
DEF_SDE(LocalMark, filemark,
.name = '"',
.mark = DEFAULT_POS,
.fname = NULL,
.additional_data = NULL),
DEF_SDE(Change, filemark,
.name = NUL,
.mark = DEFAULT_POS,
.fname = NULL,
.additional_data = NULL),
};
#undef DEFAULT_POS
#undef DEF_SDE
/// Initialize new linked list
///
/// @param[out] hmll List to initialize.
/// @param[in] size Maximum size of the list.
static inline void hmll_init(HMLList *const hmll, const size_t size)
FUNC_ATTR_NONNULL_ALL
{
*hmll = (HMLList) {
.entries = xcalloc(size, sizeof(hmll->entries[0])),
.first = NULL,
.last = NULL,
.free_entry = NULL,
.size = size,
.num_entries = 0,
.contained_entries = MAP_INIT,
};
hmll->last_free_entry = hmll->entries;
}
/// Iterate over HMLList in forward direction
///
/// @param hmll Pointer to the list.
/// @param cur_entry Name of the variable to iterate over.
/// @param code Code to execute on each iteration.
///
/// @return `for` cycle header (use `HMLL_FORALL(hmll, cur_entry) {body}`).
#define HMLL_FORALL(hmll, cur_entry, code) \
for (HMLListEntry *(cur_entry) = (hmll)->first; (cur_entry) != NULL; \
(cur_entry) = (cur_entry)->next) { \
code \
} \
/// Remove entry from the linked list
///
/// @param hmll List to remove from.
/// @param hmll_entry Entry to remove.
static inline void hmll_remove(HMLList *const hmll, HMLListEntry *const hmll_entry)
FUNC_ATTR_NONNULL_ALL
{
if (hmll_entry == hmll->last_free_entry - 1) {
hmll->last_free_entry--;
} else {
assert(hmll->free_entry == NULL);
hmll->free_entry = hmll_entry;
}
ptr_t val = pmap_del(cstr_t)(&hmll->contained_entries,
hmll_entry->data.data.history_item.string, NULL);
assert(val);
(void)val;
if (hmll_entry->next == NULL) {
hmll->last = hmll_entry->prev;
} else {
hmll_entry->next->prev = hmll_entry->prev;
}
if (hmll_entry->prev == NULL) {
hmll->first = hmll_entry->next;
} else {
hmll_entry->prev->next = hmll_entry->next;
}
hmll->num_entries--;
if (hmll_entry->can_free_entry) {
shada_free_shada_entry(&hmll_entry->data);
}
}
/// Insert entry to the linked list
///
/// @param[out] hmll List to insert to.
/// @param[in] hmll_entry Entry to insert after or NULL if it is needed
/// to insert at the first entry.
/// @param[in] data Data to insert.
/// @param[in] can_free_entry True if data can be freed.
static inline void hmll_insert(HMLList *const hmll, HMLListEntry *hmll_entry, const ShadaEntry data,
const bool can_free_entry)
FUNC_ATTR_NONNULL_ARG(1)
{
if (hmll->num_entries == hmll->size) {
if (hmll_entry == hmll->first) {
hmll_entry = NULL;
}
assert(hmll->first != NULL);
hmll_remove(hmll, hmll->first);
}
HMLListEntry *target_entry;
if (hmll->free_entry == NULL) {
assert((size_t)(hmll->last_free_entry - hmll->entries)
== hmll->num_entries);
target_entry = hmll->last_free_entry++;
} else {
assert((size_t)(hmll->last_free_entry - hmll->entries) - 1
== hmll->num_entries);
target_entry = hmll->free_entry;
hmll->free_entry = NULL;
}
target_entry->data = data;
target_entry->can_free_entry = can_free_entry;
bool new_item = false;
ptr_t *val = pmap_put_ref(cstr_t)(&hmll->contained_entries, data.data.history_item.string,
NULL, &new_item);
if (new_item) {
*val = target_entry;
}
hmll->num_entries++;
target_entry->prev = hmll_entry;
if (hmll_entry == NULL) {
target_entry->next = hmll->first;
hmll->first = target_entry;
} else {
target_entry->next = hmll_entry->next;
hmll_entry->next = target_entry;
}
if (target_entry->next == NULL) {
hmll->last = target_entry;
} else {
target_entry->next->prev = target_entry;
}
}
/// Free linked list
///
/// @param[in] hmll List to free.
static inline void hmll_dealloc(HMLList *const hmll)
FUNC_ATTR_NONNULL_ALL
{
map_destroy(cstr_t, &hmll->contained_entries);
xfree(hmll->entries);
}
/// Wrapper for reading from file descriptors
///
/// @return -1 or number of bytes read.
static ptrdiff_t read_file(ShaDaReadDef *const sd_reader, void *const dest, const size_t size)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const ptrdiff_t ret = file_read(sd_reader->cookie, dest, size);
sd_reader->eof = file_eof(sd_reader->cookie);
if (ret < 0) {
sd_reader->error = os_strerror((int)ret);
return -1;
}
sd_reader->fpos += (size_t)ret;
return ret;
}
/// Read one character
static int read_char(ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
uint8_t ret;
ptrdiff_t read_bytes = sd_reader->read(sd_reader, &ret, 1);
if (read_bytes != 1) {
return EOF;
}
return (int)ret;
}
/// Wrapper for writing to file descriptors
///
/// @return -1 or number of bytes written.
static ptrdiff_t write_file(ShaDaWriteDef *const sd_writer, const void *const dest,
const size_t size)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const ptrdiff_t ret = file_write(sd_writer->cookie, dest, size);
if (ret < 0) {
sd_writer->error = os_strerror((int)ret);
return -1;
}
return ret;
}
/// Wrapper for closing file descriptors opened for reading
static void close_sd_reader(ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ALL
{
close_file(sd_reader->cookie);
}
/// Wrapper for closing file descriptors opened for writing
static void close_sd_writer(ShaDaWriteDef *const sd_writer)
FUNC_ATTR_NONNULL_ALL
{
close_file(sd_writer->cookie);
}
/// Wrapper for read that reads to IObuff and ignores bytes read
///
/// Used for skipping.
///
/// @param[in,out] sd_reader File read.
/// @param[in] offset Amount of bytes to skip.
///
/// @return FAIL in case of failure, OK in case of success. May set
/// sd_reader->eof or sd_reader->error.
static int sd_reader_skip_read(ShaDaReadDef *const sd_reader, const size_t offset)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const ptrdiff_t skip_bytes = file_skip(sd_reader->cookie, offset);
if (skip_bytes < 0) {
sd_reader->error = os_strerror((int)skip_bytes);
return FAIL;
} else if (skip_bytes != (ptrdiff_t)offset) {
assert(skip_bytes < (ptrdiff_t)offset);
sd_reader->eof = file_eof(sd_reader->cookie);
if (!sd_reader->eof) {
sd_reader->error = _("too few bytes read");
}
return FAIL;
}
sd_reader->fpos += (size_t)skip_bytes;
return OK;
}
/// Wrapper for read that can be used when lseek cannot be used
///
/// E.g. when trying to read from a pipe.
///
/// @param[in,out] sd_reader File read.
/// @param[in] offset Amount of bytes to skip.
///
/// @return kSDReadStatusReadError, kSDReadStatusNotShaDa or
/// kSDReadStatusSuccess.
static ShaDaReadResult sd_reader_skip(ShaDaReadDef *const sd_reader, const size_t offset)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
if (sd_reader->skip(sd_reader, offset) != OK) {
if (sd_reader->error != NULL) {
semsg(_(SERR "System error while skipping in ShaDa file: %s"),
sd_reader->error);
return kSDReadStatusReadError;
} else if (sd_reader->eof) {
semsg(_(RCERR "Error while reading ShaDa file: "
"last entry specified that it occupies %" PRIu64 " bytes, "
"but file ended earlier"),
(uint64_t)offset);
return kSDReadStatusNotShaDa;
}
abort();
}
return kSDReadStatusSuccess;
}
/// Open ShaDa file for reading
///
/// @param[in] fname File name to open.
/// @param[out] sd_reader Location where reader structure will be saved.
///
/// @return libuv error in case of error, 0 otherwise.
static int open_shada_file_for_reading(const char *const fname, ShaDaReadDef *sd_reader)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
int error;
*sd_reader = (ShaDaReadDef) {
.read = &read_file,
.close = &close_sd_reader,
.skip = &sd_reader_skip_read,
.error = NULL,
.eof = false,
.fpos = 0,
.cookie = file_open_new(&error, fname, kFileReadOnly, 0),
};
if (sd_reader->cookie == NULL) {
return error;
}
assert(strcmp(p_enc, "utf-8") == 0);
return 0;
}
/// Wrapper for closing file descriptors
static void close_file(void *cookie)
{
const int error = file_free(cookie, !!p_fs);
if (error != 0) {
semsg(_(SERR "System error while closing ShaDa file: %s"),
os_strerror(error));
}
}
/// Msgpack callback for writing to ShaDaWriteDef*
static int msgpack_sd_writer_write(void *data, const char *buf, size_t len)
{
ShaDaWriteDef *const sd_writer = (ShaDaWriteDef *)data;
ptrdiff_t written_bytes = sd_writer->write(sd_writer, buf, len);
if (written_bytes == -1) {
semsg(_(SERR "System error while writing ShaDa file: %s"),
sd_writer->error);
return -1;
}
return 0;
}
/// Check whether writing to shada file was disabled ("-i NONE" or "--clean").
///
/// @return true if it was disabled, false otherwise.
static bool shada_disabled(void)
FUNC_ATTR_PURE
{
return strequal(p_shadafile, "NONE");
}
/// Read ShaDa file
///
/// @param[in] file File to read or NULL to use default name.
/// @param[in] flags Flags, see ShaDaReadFileFlags enum.
///
/// @return FAIL if reading failed for some reason and OK otherwise.
static int shada_read_file(const char *const file, const int flags)
FUNC_ATTR_WARN_UNUSED_RESULT
{
if (shada_disabled()) {
return FAIL;
}
char *const fname = shada_filename(file);
ShaDaReadDef sd_reader;
const int of_ret = open_shada_file_for_reading(fname, &sd_reader);
if (p_verbose > 1) {
verbose_enter();
smsg(0, _("Reading ShaDa file \"%s\"%s%s%s%s"),
fname,
(flags & kShaDaWantInfo) ? _(" info") : "",
(flags & kShaDaWantMarks) ? _(" marks") : "",
(flags & kShaDaGetOldfiles) ? _(" oldfiles") : "",
of_ret != 0 ? _(" FAILED") : "");
verbose_leave();
}
if (of_ret != 0) {
if (of_ret != UV_ENOENT || (flags & kShaDaMissingError)) {
semsg(_(SERR "System error while opening ShaDa file %s for reading: %s"),
fname, os_strerror(of_ret));
}
xfree(fname);
return FAIL;
}
xfree(fname);
shada_read(&sd_reader, flags);
sd_reader.close(&sd_reader);
return OK;
}
/// Wrapper for hist_iter() function which produces ShadaEntry values
///
/// @param[in] iter Current iteration state.
/// @param[in] history_type Type of the history (HIST_*).
/// @param[in] zero If true, then item is removed from instance
/// memory upon reading.
/// @param[out] hist Location where iteration results should be saved.
///
/// @return Next iteration state.
static const void *shada_hist_iter(const void *const iter, const uint8_t history_type,
const bool zero, ShadaEntry *const hist)
FUNC_ATTR_NONNULL_ARG(4) FUNC_ATTR_WARN_UNUSED_RESULT
{
histentry_T hist_he;
const void *const ret = hist_iter(iter, history_type, zero, &hist_he);
if (hist_he.hisstr == NULL) {
*hist = (ShadaEntry) { .type = kSDItemMissing };
} else {
*hist = (ShadaEntry) {
.type = kSDItemHistoryEntry,
.timestamp = hist_he.timestamp,
.data = {
.history_item = {
.histtype = history_type,
.string = hist_he.hisstr,
.sep = (char)(history_type == HIST_SEARCH
? hist_he.hisstr[strlen(hist_he.hisstr) + 1]
: 0),
.additional_elements = hist_he.additional_elements,
}
}
};
}
return ret;
}
/// Insert history entry
///
/// Inserts history entry at the end of the ring buffer (may insert earlier
/// according to the timestamp). If entry was already in the ring buffer
/// existing entry will be removed unless it has greater timestamp.
///
/// Before the new entry entries from the current Neovim history will be
/// inserted unless `do_iter` argument is false.
///
/// @param[in,out] hms_p Ring buffer and associated structures.
/// @param[in] entry Inserted entry.
/// @param[in] do_iter Determines whether Neovim own history should
/// be used. Must be true only if inserting
/// entry from current Neovim history.
/// @param[in] can_free_entry True if entry can be freed.
static void hms_insert(HistoryMergerState *const hms_p, const ShadaEntry entry, const bool do_iter,
const bool can_free_entry)
FUNC_ATTR_NONNULL_ALL
{
if (do_iter) {
while (hms_p->last_hist_entry.type != kSDItemMissing
&& hms_p->last_hist_entry.timestamp < entry.timestamp) {
hms_insert(hms_p, hms_p->last_hist_entry, false, hms_p->reading);
if (hms_p->iter == NULL) {
hms_p->last_hist_entry.type = kSDItemMissing;
break;
}
hms_p->iter = shada_hist_iter(hms_p->iter, hms_p->history_type,
hms_p->reading, &hms_p->last_hist_entry);
}
}
HMLList *const hmll = &hms_p->hmll;
cstr_t *key_alloc = NULL;
ptr_t *val = pmap_ref(cstr_t)(&hms_p->hmll.contained_entries, entry.data.history_item.string,
&key_alloc);
if (val) {
HMLListEntry *const existing_entry = *val;
if (entry.timestamp > existing_entry->data.timestamp) {
hmll_remove(hmll, existing_entry);
} else if (!do_iter && entry.timestamp == existing_entry->data.timestamp) {
// Prefer entry from the current Neovim instance.
if (existing_entry->can_free_entry) {
shada_free_shada_entry(&existing_entry->data);
}
existing_entry->data = entry;
existing_entry->can_free_entry = can_free_entry;
// Previous key was freed above, as part of freeing the ShaDa entry.
*key_alloc = entry.data.history_item.string;
return;
} else {
return;
}
}
HMLListEntry *insert_after;
// Iterate over HMLList in backward direction
for (insert_after = hmll->last; insert_after != NULL; insert_after = insert_after->prev) {
if (insert_after->data.timestamp <= entry.timestamp) {
break;
}
}
hmll_insert(hmll, insert_after, entry, can_free_entry);
}
/// Initialize the history merger
///
/// @param[out] hms_p Structure to be initialized.
/// @param[in] history_type History type (one of HIST_\* values).
/// @param[in] num_elements Number of elements in the result.
/// @param[in] do_merge Prepare structure for merging elements.
/// @param[in] reading If true, then merger is reading history for use
/// in Neovim.
static inline void hms_init(HistoryMergerState *const hms_p, const uint8_t history_type,
const size_t num_elements, const bool do_merge, const bool reading)
FUNC_ATTR_NONNULL_ALL
{
hmll_init(&hms_p->hmll, num_elements);
hms_p->do_merge = do_merge;
hms_p->reading = reading;
hms_p->iter = shada_hist_iter(NULL, history_type, hms_p->reading,
&hms_p->last_hist_entry);
hms_p->history_type = history_type;
}
/// Merge in all remaining Neovim own history entries
///
/// @param[in,out] hms_p Merger structure into which history should be
/// inserted.
static inline void hms_insert_whole_neovim_history(HistoryMergerState *const hms_p)
FUNC_ATTR_NONNULL_ALL
{
while (hms_p->last_hist_entry.type != kSDItemMissing) {
hms_insert(hms_p, hms_p->last_hist_entry, false, hms_p->reading);
if (hms_p->iter == NULL) {
break;
}
hms_p->iter = shada_hist_iter(hms_p->iter, hms_p->history_type,
hms_p->reading, &hms_p->last_hist_entry);
}
}
/// Convert merger structure to Neovim internal structure for history
///
/// @param[in] hms_p Converted merger structure.
/// @param[out] hist_array Array with the results.
/// @param[out] new_hisidx New last history entry index.
/// @param[out] new_hisnum Amount of history items in merger structure.
static inline void hms_to_he_array(const HistoryMergerState *const hms_p,
histentry_T *const hist_array, int *const new_hisidx,
int *const new_hisnum)
FUNC_ATTR_NONNULL_ALL
{
histentry_T *hist = hist_array;
HMLL_FORALL(&hms_p->hmll, cur_entry, {
hist->timestamp = cur_entry->data.timestamp;
hist->hisnum = (int)(hist - hist_array) + 1;
hist->hisstr = cur_entry->data.data.history_item.string;
hist->additional_elements =
cur_entry->data.data.history_item.additional_elements;
hist++;
})
*new_hisnum = (int)(hist - hist_array);
*new_hisidx = *new_hisnum - 1;
}
/// Free history merger structure
///
/// @param[in] hms_p Structure to be freed.
static inline void hms_dealloc(HistoryMergerState *const hms_p)
FUNC_ATTR_NONNULL_ALL
{
hmll_dealloc(&hms_p->hmll);
}
/// Iterate over all history entries in history merger, in order
///
/// @param[in] hms_p Merger structure to iterate over.
/// @param[out] cur_entry Name of the iterator variable.
/// @param code Code to execute on each iteration.
///
/// @return for cycle header. Use `HMS_ITER(hms_p, cur_entry) {body}`.
#define HMS_ITER(hms_p, cur_entry, code) \
HMLL_FORALL(&((hms_p)->hmll), cur_entry, code)
/// Find buffer for given buffer name (cached)
///
/// @param[in,out] fname_bufs Cache containing fname to buffer mapping.
/// @param[in] fname File name to find.
///
/// @return Pointer to the buffer or NULL.
static buf_T *find_buffer(PMap(cstr_t) *const fname_bufs, const char *const fname)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
cstr_t *key_alloc = NULL;
bool new_item = false;
buf_T **ref = (buf_T **)pmap_put_ref(cstr_t)(fname_bufs, fname, &key_alloc, &new_item);
if (new_item) {
*key_alloc = xstrdup(fname);
} else {
return *ref; // item already existed (can be a NULL value)
}
FOR_ALL_BUFFERS(buf) {
if (buf->b_ffname != NULL) {
if (path_fnamecmp(fname, buf->b_ffname) == 0) {
*ref = buf;
return buf;
}
}
}
*ref = NULL;
return NULL;
}
/// Compare two marks
static inline bool marks_equal(const pos_T a, const pos_T b)
{
return (a.lnum == b.lnum) && (a.col == b.col);
}
#define MERGE_JUMPS(jumps_size, jumps, jumps_type, timestamp_attr, mark_attr, \
entry, fname_cond, free_func, fin_func, \
idxadj_func, afterfree_func) \
do { \
const int jl_len = (int)(jumps_size); \
int i; \
for (i = jl_len; i > 0; i--) { \
const jumps_type jl_entry = (jumps)[i - 1]; \
if (jl_entry.timestamp_attr <= (entry).timestamp) { \
if (marks_equal(jl_entry.mark_attr, (entry).data.filemark.mark) \
&& (fname_cond)) { \
i = -1; \
} \
break; \
} \
} \
if (i > 0) { \
if (jl_len == JUMPLISTSIZE) { \
free_func((jumps)[0]); \
i--; \
if (i > 0) { \
memmove(&(jumps)[0], &(jumps)[1], sizeof((jumps)[1]) * (size_t)i); \
} \
} else if (i != jl_len) { \
memmove(&(jumps)[i + 1], &(jumps)[i], \
sizeof((jumps)[0]) * (size_t)(jl_len - i)); \
} \
} else if (i == 0) { \
if (jl_len == JUMPLISTSIZE) { \
i = -1; \
} else if (jl_len > 0) { \
memmove(&(jumps)[1], &(jumps)[0], sizeof((jumps)[0]) * (size_t)jl_len); \
} \
} \
if (i != -1) { \
(jumps)[i] = fin_func(entry); \
if (jl_len < JUMPLISTSIZE) { \
(jumps_size)++; \
} \
idxadj_func(i); \
} else { \
shada_free_shada_entry(&(entry)); \
afterfree_func(entry); \
} \
} while (0)
/// Read data from ShaDa file
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[in] flags What to read, see ShaDaReadFileFlags enum.
static void shada_read(ShaDaReadDef *const sd_reader, const int flags)
FUNC_ATTR_NONNULL_ALL
{
list_T *oldfiles_list = get_vim_var_list(VV_OLDFILES);
const bool force = flags & kShaDaForceit;
const bool get_old_files = (flags & (kShaDaGetOldfiles | kShaDaForceit)
&& (force || tv_list_len(oldfiles_list) == 0));
const bool want_marks = flags & kShaDaWantMarks;
const unsigned srni_flags =
(unsigned)(
(flags & kShaDaWantInfo
? (kSDReadUndisableableData
| kSDReadRegisters
| kSDReadGlobalMarks
| (p_hi ? kSDReadHistory : 0)
| (find_shada_parameter('!') != NULL
? kSDReadVariables
: 0)
| (find_shada_parameter('%') != NULL
&& ARGCOUNT == 0
? kSDReadBufferList
: 0))
: 0)
| (want_marks && get_shada_parameter('\'') > 0
? kSDReadLocalMarks | kSDReadChanges
: 0)
| (get_old_files
? kSDReadLocalMarks
: 0));
if (srni_flags == 0) {
// Nothing to do.
return;
}
HistoryMergerState hms[HIST_COUNT];
if (srni_flags & kSDReadHistory) {
for (int i = 0; i < HIST_COUNT; i++) {
hms_init(&hms[i], (uint8_t)i, (size_t)p_hi, true, true);
}
}
ShadaEntry cur_entry;
Set(ptr_t) cl_bufs = SET_INIT;
PMap(cstr_t) fname_bufs = MAP_INIT;
Set(cstr_t) oldfiles_set = SET_INIT;
if (get_old_files && (oldfiles_list == NULL || force)) {
oldfiles_list = tv_list_alloc(kListLenUnknown);
set_vim_var_list(VV_OLDFILES, oldfiles_list);
}
ShaDaReadResult srni_ret;
while ((srni_ret = shada_read_next_item(sd_reader, &cur_entry, srni_flags, 0))
!= kSDReadStatusFinished) {
switch (srni_ret) {
case kSDReadStatusSuccess:
break;
case kSDReadStatusFinished:
// Should be handled by the while condition.
abort();
case kSDReadStatusNotShaDa:
case kSDReadStatusReadError:
goto shada_read_main_cycle_end;
case kSDReadStatusMalformed:
continue;
}
switch (cur_entry.type) {
case kSDItemMissing:
abort();
case kSDItemUnknown:
break;
case kSDItemHeader:
shada_free_shada_entry(&cur_entry);
break;
case kSDItemSearchPattern:
if (!force) {
SearchPattern pat;
if (cur_entry.data.search_pattern.is_substitute_pattern) {
get_substitute_pattern(&pat);
} else {
get_search_pattern(&pat);
}
if (pat.pat != NULL && pat.timestamp >= cur_entry.timestamp) {
shada_free_shada_entry(&cur_entry);
break;
}
}
SearchPattern spat = (SearchPattern) {
.magic = cur_entry.data.search_pattern.magic,
.no_scs = !cur_entry.data.search_pattern.smartcase,
.off = {
.dir = cur_entry.data.search_pattern.search_backward ? '?' : '/',
.line = cur_entry.data.search_pattern.has_line_offset,
.end = cur_entry.data.search_pattern.place_cursor_at_end,
.off = cur_entry.data.search_pattern.offset,
},
.pat = cur_entry.data.search_pattern.pat,
.additional_data = cur_entry.data.search_pattern.additional_data,
.timestamp = cur_entry.timestamp,
};
if (cur_entry.data.search_pattern.is_substitute_pattern) {
set_substitute_pattern(spat);
} else {
set_search_pattern(spat);
}
if (cur_entry.data.search_pattern.is_last_used) {
set_last_used_pattern(cur_entry.data.search_pattern.is_substitute_pattern);
set_no_hlsearch(!cur_entry.data.search_pattern.highlighted);
}
// Do not free shada entry: its allocated memory was saved above.
break;
case kSDItemSubString:
if (!force) {
SubReplacementString sub;
sub_get_replacement(&sub);
if (sub.sub != NULL && sub.timestamp >= cur_entry.timestamp) {
shada_free_shada_entry(&cur_entry);
break;
}
}
sub_set_replacement((SubReplacementString) {
.sub = cur_entry.data.sub_string.sub,
.timestamp = cur_entry.timestamp,
.additional_elements = cur_entry.data.sub_string.additional_elements,
});
// Without using regtilde and without / &cpo flag previous substitute
// string is close to useless: you can only use it with :& or :~ and
// that’s all because s//~ is not available until the first call to
// regtilde. Vim was not calling this for some reason.
(void)regtilde(cur_entry.data.sub_string.sub, magic_isset(), false);
// Do not free shada entry: its allocated memory was saved above.
break;
case kSDItemHistoryEntry:
if (cur_entry.data.history_item.histtype >= HIST_COUNT) {
shada_free_shada_entry(&cur_entry);
break;
}
hms_insert(hms + cur_entry.data.history_item.histtype, cur_entry, true,
true);
// Do not free shada entry: its allocated memory was saved above.
break;
case kSDItemRegister:
if (cur_entry.data.reg.type != kMTCharWise
&& cur_entry.data.reg.type != kMTLineWise
&& cur_entry.data.reg.type != kMTBlockWise) {
shada_free_shada_entry(&cur_entry);
break;
}
if (!force) {
const yankreg_T *const reg = op_reg_get(cur_entry.data.reg.name);
if (reg == NULL || reg->timestamp >= cur_entry.timestamp) {
shada_free_shada_entry(&cur_entry);
break;
}
}
if (!op_reg_set(cur_entry.data.reg.name, (yankreg_T) {
.y_array = cur_entry.data.reg.contents,
.y_size = cur_entry.data.reg.contents_size,
.y_type = cur_entry.data.reg.type,
.y_width = (colnr_T)cur_entry.data.reg.width,
.timestamp = cur_entry.timestamp,
.additional_data = cur_entry.data.reg.additional_data,
}, cur_entry.data.reg.is_unnamed)) {
shada_free_shada_entry(&cur_entry);
}
// Do not free shada entry: its allocated memory was saved above.
break;
case kSDItemVariable:
var_set_global(cur_entry.data.global_var.name,
cur_entry.data.global_var.value);
cur_entry.data.global_var.value.v_type = VAR_UNKNOWN;
shada_free_shada_entry(&cur_entry);
break;
case kSDItemJump:
case kSDItemGlobalMark: {
buf_T *buf = find_buffer(&fname_bufs, cur_entry.data.filemark.fname);
if (buf != NULL) {
XFREE_CLEAR(cur_entry.data.filemark.fname);
}
xfmark_T fm = (xfmark_T) {
.fname = buf == NULL ? cur_entry.data.filemark.fname : NULL,
.fmark = {
.mark = cur_entry.data.filemark.mark,
.fnum = (buf == NULL ? 0 : buf->b_fnum),
.timestamp = cur_entry.timestamp,
.view = INIT_FMARKV,
.additional_data = cur_entry.data.filemark.additional_data,
},
};
if (cur_entry.type == kSDItemGlobalMark) {
if (!mark_set_global(cur_entry.data.filemark.name, fm, !force)) {
shada_free_shada_entry(&cur_entry);
break;
}
} else {
#define SDE_TO_XFMARK(entry) fm
#define ADJUST_IDX(i) \
if (curwin->w_jumplistidx >= (i) \
&& curwin->w_jumplistidx + 1 <= curwin->w_jumplistlen) { \
curwin->w_jumplistidx++; \
}
#define DUMMY_AFTERFREE(entry)
MERGE_JUMPS(curwin->w_jumplistlen, curwin->w_jumplist, xfmark_T,
fmark.timestamp, fmark.mark, cur_entry,
(buf == NULL
? (jl_entry.fname != NULL
&& strcmp(fm.fname, jl_entry.fname) == 0)
: fm.fmark.fnum == jl_entry.fmark.fnum),
free_xfmark, SDE_TO_XFMARK, ADJUST_IDX, DUMMY_AFTERFREE);
#undef SDE_TO_XFMARK
#undef ADJUST_IDX
#undef DUMMY_AFTERFREE
}
// Do not free shada entry: its allocated memory was saved above.
break;
}
case kSDItemBufferList:
for (size_t i = 0; i < cur_entry.data.buffer_list.size; i++) {
char *const sfname =
path_try_shorten_fname(cur_entry.data.buffer_list.buffers[i].fname);
buf_T *const buf =
buflist_new(cur_entry.data.buffer_list.buffers[i].fname, sfname, 0, BLN_LISTED);
if (buf != NULL) {
fmarkv_T view = INIT_FMARKV;
RESET_FMARK(&buf->b_last_cursor,
cur_entry.data.buffer_list.buffers[i].pos, 0, view);
buflist_setfpos(buf, curwin, buf->b_last_cursor.mark.lnum,
buf->b_last_cursor.mark.col, false);
buf->additional_data =
cur_entry.data.buffer_list.buffers[i].additional_data;
cur_entry.data.buffer_list.buffers[i].additional_data = NULL;
}
}
shada_free_shada_entry(&cur_entry);
break;
case kSDItemChange:
case kSDItemLocalMark: {
if (get_old_files && !set_has(cstr_t, &oldfiles_set, cur_entry.data.filemark.fname)) {
char *fname = cur_entry.data.filemark.fname;
if (want_marks) {
// Do not bother with allocating memory for the string if already
// allocated string from cur_entry can be used. It cannot be used if
// want_marks is set because this way it may be used for a mark.
fname = xstrdup(fname);
}
set_put(cstr_t, &oldfiles_set, fname);
tv_list_append_allocated_string(oldfiles_list, fname);
if (!want_marks) {
// Avoid free because this string was already used.
cur_entry.data.filemark.fname = NULL;
}
}
if (!want_marks) {
shada_free_shada_entry(&cur_entry);
break;
}
buf_T *buf = find_buffer(&fname_bufs, cur_entry.data.filemark.fname);
if (buf == NULL) {
shada_free_shada_entry(&cur_entry);
break;
}
const fmark_T fm = (fmark_T) {
.mark = cur_entry.data.filemark.mark,
.fnum = 0,
.timestamp = cur_entry.timestamp,
.view = INIT_FMARKV,
.additional_data = cur_entry.data.filemark.additional_data,
};
if (cur_entry.type == kSDItemLocalMark) {
if (!mark_set_local(cur_entry.data.filemark.name, buf, fm, !force)) {
shada_free_shada_entry(&cur_entry);
break;
}
} else {
set_put(ptr_t, &cl_bufs, buf);
#define SDE_TO_FMARK(entry) fm
#define AFTERFREE(entry) (entry).data.filemark.fname = NULL
#define DUMMY_IDX_ADJ(i)
MERGE_JUMPS(buf->b_changelistlen, buf->b_changelist, fmark_T,
timestamp, mark, cur_entry, true,
free_fmark, SDE_TO_FMARK, DUMMY_IDX_ADJ, AFTERFREE);
#undef SDE_TO_FMARK
#undef AFTERFREE
#undef DUMMY_IDX_ADJ
}
// Do not free shada entry: except for fname, its allocated memory (i.e.
// additional_data attribute contents if non-NULL) was saved above.
xfree(cur_entry.data.filemark.fname);
break;
}
}
}
shada_read_main_cycle_end:
// Warning: shada_hist_iter returns ShadaEntry elements which use strings from
// original history list. This means that once such entry is removed
// from the history Neovim array will no longer be valid. To reduce
// amount of memory allocations ShaDa file reader allocates enough
// memory for the history string itself and separator character which
// may be assigned right away.
if (srni_flags & kSDReadHistory) {
for (int i = 0; i < HIST_COUNT; i++) {
hms_insert_whole_neovim_history(&hms[i]);
clr_history(i);
int *new_hisidx;
int *new_hisnum;
histentry_T *hist = hist_get_array((uint8_t)i, &new_hisidx, &new_hisnum);
if (hist != NULL) {
hms_to_he_array(&hms[i], hist, new_hisidx, new_hisnum);
}
hms_dealloc(&hms[i]);
}
}
if (cl_bufs.h.n_occupied) {
FOR_ALL_TAB_WINDOWS(tp, wp) {
(void)tp;
if (set_has(ptr_t, &cl_bufs, wp->w_buffer)) {
wp->w_changelistidx = wp->w_buffer->b_changelistlen;
}
}
}
set_destroy(ptr_t, &cl_bufs);
const char *key;
map_foreach_key(&fname_bufs, key, {
xfree((char *)key);
})
map_destroy(cstr_t, &fname_bufs);
set_destroy(cstr_t, &oldfiles_set);
}
/// Default shada file location: cached path
static char *default_shada_file = NULL;
/// Get the default ShaDa file
static const char *shada_get_default_file(void)
FUNC_ATTR_WARN_UNUSED_RESULT
{
if (default_shada_file == NULL) {
char *shada_dir = stdpaths_user_state_subpath("shada", 0, false);
default_shada_file = concat_fnames_realloc(shada_dir, "main.shada", true);
}
return default_shada_file;
}
/// Get the ShaDa file name to use
///
/// If "file" is given and not empty, use it (has already been expanded by
/// cmdline functions). Otherwise use "-i file_name", value from 'shada' or the
/// default, and expand environment variables.
///
/// @param[in] file Forced file name or NULL.
///
/// @return An allocated string containing shada file name.
static char *shada_filename(const char *file)
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT
{
if (file == NULL || *file == NUL) {
if (p_shadafile != NULL && *p_shadafile != NUL) {
file = p_shadafile;
} else {
if ((file = find_shada_parameter('n')) == NULL || *file == NUL) {
file = shada_get_default_file();
}
// XXX It used to be one level lower, so that whatever is in
// `p_shadafile` was expanded. I intentionally moved it here
// because various expansions must have already be done by the shell.
// If shell is not performing them then they should be done in main.c
// where arguments are parsed, *not here*.
expand_env((char *)file, &(NameBuff[0]), MAXPATHL);
file = &(NameBuff[0]);
}
}
return xstrdup(file);
}
#define PACK_STATIC_STR(s) \
do { \
msgpack_pack_str(spacker, sizeof(s) - 1); \
msgpack_pack_str_body(spacker, s, sizeof(s) - 1); \
} while (0)
#define PACK_BIN(s) \
do { \
const String s_ = (s); \
msgpack_pack_bin(spacker, s_.size); \
if (s_.size > 0) { \
msgpack_pack_bin_body(spacker, s_.data, s_.size); \
} \
} while (0)
/// Write single ShaDa entry
///
/// @param[in] packer Packer used to write entry.
/// @param[in] entry Entry written.
/// @param[in] max_kbyte Maximum size of an item in KiB. Zero means no
/// restrictions.
///
/// @return kSDWriteSuccessful, kSDWriteFailed or kSDWriteIgnError.
static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer, ShadaEntry entry,
const size_t max_kbyte)
FUNC_ATTR_NONNULL_ALL
{
ShaDaWriteResult ret = kSDWriteFailed;
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf);
msgpack_packer *spacker = msgpack_packer_new(&sbuf, &msgpack_sbuffer_write);
#define DUMP_ADDITIONAL_ELEMENTS(src, what) \
do { \
if ((src) != NULL) { \
TV_LIST_ITER((src), li, { \
if (encode_vim_to_msgpack(spacker, TV_LIST_ITEM_TV(li), \
_("additional elements of ShaDa " what)) \
== FAIL) { \
goto shada_pack_entry_error; \
} \
}); \
} \
} while (0)
#define DUMP_ADDITIONAL_DATA(src, what) \
do { \
dict_T *const d = (src); \
if (d != NULL) { \
size_t todo = d->dv_hashtab.ht_used; \
for (const hashitem_T *hi = d->dv_hashtab.ht_array; todo; hi++) { \
if (!HASHITEM_EMPTY(hi)) { \
todo--; \
dictitem_T *const di = TV_DICT_HI2DI(hi); \
const size_t key_len = strlen(hi->hi_key); \
msgpack_pack_str(spacker, key_len); \
msgpack_pack_str_body(spacker, hi->hi_key, key_len); \
if (encode_vim_to_msgpack(spacker, &di->di_tv, \
_("additional data of ShaDa " what)) \
== FAIL) { \
goto shada_pack_entry_error; \
} \
} \
} \
} \
} while (0)
#define CHECK_DEFAULT(entry, attr) \
(sd_default_values[(entry).type].data.attr == (entry).data.attr)
#define ONE_IF_NOT_DEFAULT(entry, attr) \
((size_t)(!CHECK_DEFAULT(entry, attr)))
#define PACK_BOOL(entry, name, attr) \
do { \
if (!CHECK_DEFAULT(entry, search_pattern.attr)) { \
PACK_STATIC_STR(name); \
if (sd_default_values[(entry).type].data.search_pattern.attr) { \
msgpack_pack_false(spacker); \
} else { \
msgpack_pack_true(spacker); \
} \
} \
} while (0)
switch (entry.type) {
case kSDItemMissing:
abort();
case kSDItemUnknown:
if (spacker->callback(spacker->data, entry.data.unknown_item.contents,
(unsigned)entry.data.unknown_item.size) == -1) {
goto shada_pack_entry_error;
}
break;
case kSDItemHistoryEntry: {
const bool is_hist_search =
entry.data.history_item.histtype == HIST_SEARCH;
const size_t arr_size = 2 + (size_t)is_hist_search + (size_t)(
tv_list_len(entry.data.
history_item.
additional_elements));
msgpack_pack_array(spacker, arr_size);
msgpack_pack_uint8(spacker, entry.data.history_item.histtype);
PACK_BIN(cstr_as_string(entry.data.history_item.string));
if (is_hist_search) {
msgpack_pack_uint8(spacker, (uint8_t)entry.data.history_item.sep);
}
DUMP_ADDITIONAL_ELEMENTS(entry.data.history_item.additional_elements,
"history entry item");
break;
}
case kSDItemVariable: {
if (entry.data.global_var.value.v_type == VAR_BLOB) {
// Strings and Blobs both pack as msgpack BINs; differentiate them by
// storing an additional VAR_TYPE_BLOB element alongside Blobs
list_T *const list = tv_list_alloc(1);
tv_list_append_number(list, VAR_TYPE_BLOB);
entry.data.global_var.additional_elements = list;
}
const size_t arr_size = 2 + (size_t)(tv_list_len(entry.data.global_var.additional_elements));
msgpack_pack_array(spacker, arr_size);
const String varname = cstr_as_string(entry.data.global_var.name);
PACK_BIN(varname);
char vardesc[256] = "variable g:";
memcpy(&vardesc[sizeof("variable g:") - 1], varname.data,
varname.size + 1);
if (encode_vim_to_msgpack(spacker, &entry.data.global_var.value, vardesc)
== FAIL) {
ret = kSDWriteIgnError;
semsg(_(WERR "Failed to write variable %s"),
entry.data.global_var.name);
goto shada_pack_entry_error;
}
DUMP_ADDITIONAL_ELEMENTS(entry.data.global_var.additional_elements,
"variable item");
break;
}
case kSDItemSubString: {
const size_t arr_size = 1 + (size_t)(
tv_list_len(entry.data.sub_string.additional_elements));
msgpack_pack_array(spacker, arr_size);
PACK_BIN(cstr_as_string(entry.data.sub_string.sub));
DUMP_ADDITIONAL_ELEMENTS(entry.data.sub_string.additional_elements,
"sub string item");
break;
}
case kSDItemSearchPattern: {
size_t entry_map_size = (
1 // Search pattern is always present
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.magic)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.is_last_used)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.smartcase)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.has_line_offset)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.place_cursor_at_end)
+ ONE_IF_NOT_DEFAULT(entry,
search_pattern.is_substitute_pattern)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.highlighted)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.offset)
+ ONE_IF_NOT_DEFAULT(entry, search_pattern.search_backward)
// finally, additional data:
+ (
entry.data.search_pattern.additional_data
? entry.data.search_pattern.additional_data->dv_hashtab.ht_used
: 0));
msgpack_pack_map(spacker, entry_map_size);
PACK_STATIC_STR(SEARCH_KEY_PAT);
PACK_BIN(cstr_as_string(entry.data.search_pattern.pat));
PACK_BOOL(entry, SEARCH_KEY_MAGIC, magic);
PACK_BOOL(entry, SEARCH_KEY_IS_LAST_USED, is_last_used);
PACK_BOOL(entry, SEARCH_KEY_SMARTCASE, smartcase);
PACK_BOOL(entry, SEARCH_KEY_HAS_LINE_OFFSET, has_line_offset);
PACK_BOOL(entry, SEARCH_KEY_PLACE_CURSOR_AT_END, place_cursor_at_end);
PACK_BOOL(entry, SEARCH_KEY_IS_SUBSTITUTE_PATTERN, is_substitute_pattern);
PACK_BOOL(entry, SEARCH_KEY_HIGHLIGHTED, highlighted);
PACK_BOOL(entry, SEARCH_KEY_BACKWARD, search_backward);
if (!CHECK_DEFAULT(entry, search_pattern.offset)) {
PACK_STATIC_STR(SEARCH_KEY_OFFSET);
msgpack_pack_int64(spacker, entry.data.search_pattern.offset);
}
#undef PACK_BOOL
DUMP_ADDITIONAL_DATA(entry.data.search_pattern.additional_data,
"search pattern item");
break;
}
case kSDItemChange:
case kSDItemGlobalMark:
case kSDItemLocalMark:
case kSDItemJump: {
size_t entry_map_size = (
1 // File name
+ ONE_IF_NOT_DEFAULT(entry, filemark.mark.lnum)
+ ONE_IF_NOT_DEFAULT(entry, filemark.mark.col)
+ ONE_IF_NOT_DEFAULT(entry, filemark.name)
// Additional entries, if any:
+ (
entry.data.filemark.additional_data == NULL
? 0
: entry.data.filemark.additional_data->dv_hashtab.ht_used));
msgpack_pack_map(spacker, entry_map_size);
PACK_STATIC_STR(KEY_FILE);
PACK_BIN(cstr_as_string(entry.data.filemark.fname));
if (!CHECK_DEFAULT(entry, filemark.mark.lnum)) {
PACK_STATIC_STR(KEY_LNUM);
msgpack_pack_long(spacker, entry.data.filemark.mark.lnum);
}
if (!CHECK_DEFAULT(entry, filemark.mark.col)) {
PACK_STATIC_STR(KEY_COL);
msgpack_pack_long(spacker, entry.data.filemark.mark.col);
}
assert(entry.type == kSDItemJump || entry.type == kSDItemChange
? CHECK_DEFAULT(entry, filemark.name)
: true);
if (!CHECK_DEFAULT(entry, filemark.name)) {
PACK_STATIC_STR(KEY_NAME_CHAR);
msgpack_pack_uint8(spacker, (uint8_t)entry.data.filemark.name);
}
DUMP_ADDITIONAL_DATA(entry.data.filemark.additional_data,
"mark (change, jump, global or local) item");
break;
}
case kSDItemRegister: {
size_t entry_map_size = (2 // Register contents and name
+ ONE_IF_NOT_DEFAULT(entry, reg.type)
+ ONE_IF_NOT_DEFAULT(entry, reg.width)
+ ONE_IF_NOT_DEFAULT(entry, reg.is_unnamed)
// Additional entries, if any:
+ (entry.data.reg.additional_data == NULL
? 0
: entry.data.reg.additional_data->dv_hashtab.ht_used));
msgpack_pack_map(spacker, entry_map_size);
PACK_STATIC_STR(REG_KEY_CONTENTS);
msgpack_pack_array(spacker, entry.data.reg.contents_size);
for (size_t i = 0; i < entry.data.reg.contents_size; i++) {
PACK_BIN(cstr_as_string(entry.data.reg.contents[i]));
}
PACK_STATIC_STR(KEY_NAME_CHAR);
msgpack_pack_char(spacker, entry.data.reg.name);
if (!CHECK_DEFAULT(entry, reg.type)) {
PACK_STATIC_STR(REG_KEY_TYPE);
msgpack_pack_uint8(spacker, (uint8_t)entry.data.reg.type);
}
if (!CHECK_DEFAULT(entry, reg.width)) {
PACK_STATIC_STR(REG_KEY_WIDTH);
msgpack_pack_uint64(spacker, (uint64_t)entry.data.reg.width);
}
if (!CHECK_DEFAULT(entry, reg.is_unnamed)) {
PACK_STATIC_STR(REG_KEY_UNNAMED);
if (entry.data.reg.is_unnamed) {
msgpack_pack_true(spacker);
} else {
msgpack_pack_false(spacker);
}
}
DUMP_ADDITIONAL_DATA(entry.data.reg.additional_data, "register item");
break;
}
case kSDItemBufferList:
msgpack_pack_array(spacker, entry.data.buffer_list.size);
for (size_t i = 0; i < entry.data.buffer_list.size; i++) {
size_t entry_map_size = (
1 // Buffer name
+ (size_t)(entry.data.buffer_list.buffers[i].pos.lnum
!= default_pos.lnum)
+ (size_t)(entry.data.buffer_list.buffers[i].pos.col
!= default_pos.col)
// Additional entries, if any:
+ (
entry.data.buffer_list.buffers[i].additional_data
== NULL
? 0
: (entry.data.buffer_list.buffers[i].additional_data
->dv_hashtab.ht_used)));
msgpack_pack_map(spacker, entry_map_size);
PACK_STATIC_STR(KEY_FILE);
PACK_BIN(cstr_as_string(entry.data.buffer_list.buffers[i].fname));
if (entry.data.buffer_list.buffers[i].pos.lnum != 1) {
PACK_STATIC_STR(KEY_LNUM);
msgpack_pack_uint64(spacker, (uint64_t)entry.data.buffer_list.buffers[i].pos.lnum);
}
if (entry.data.buffer_list.buffers[i].pos.col != 0) {
PACK_STATIC_STR(KEY_COL);
msgpack_pack_uint64(spacker, (uint64_t)entry.data.buffer_list.buffers[i].pos.col);
}
DUMP_ADDITIONAL_DATA(entry.data.buffer_list.buffers[i].additional_data,
"buffer list subitem");
}
break;
case kSDItemHeader:
msgpack_pack_map(spacker, entry.data.header.size);
for (size_t i = 0; i < entry.data.header.size; i++) {
const String s = entry.data.header.items[i].key;
msgpack_pack_str(spacker, s.size);
if (s.size) {
msgpack_pack_str_body(spacker, s.data, s.size);
}
const Object obj = entry.data.header.items[i].value;
switch (obj.type) {
case kObjectTypeString:
PACK_BIN(obj.data.string);
break;
case kObjectTypeInteger:
msgpack_pack_int64(spacker, (int64_t)obj.data.integer);
break;
default:
abort();
}
}
break;
}
#undef CHECK_DEFAULT
#undef ONE_IF_NOT_DEFAULT
if (!max_kbyte || sbuf.size <= max_kbyte * 1024) {
if (entry.type == kSDItemUnknown) {
if (msgpack_pack_uint64(packer, entry.data.unknown_item.type) == -1) {
goto shada_pack_entry_error;
}
} else {
if (msgpack_pack_uint64(packer, (uint64_t)entry.type) == -1) {
goto shada_pack_entry_error;
}
}
if (msgpack_pack_uint64(packer, (uint64_t)entry.timestamp) == -1) {
goto shada_pack_entry_error;
}
if (sbuf.size > 0) {
if ((msgpack_pack_uint64(packer, (uint64_t)sbuf.size) == -1)
|| (packer->callback(packer->data, sbuf.data,
(unsigned)sbuf.size) == -1)) {
goto shada_pack_entry_error;
}
}
}
msgpack_packer_free(spacker);
msgpack_sbuffer_destroy(&sbuf);
return kSDWriteSuccessful;
shada_pack_entry_error:
msgpack_packer_free(spacker);
msgpack_sbuffer_destroy(&sbuf);
return ret;
}
/// Write single ShaDa entry and free it afterwards
///
/// Will not free if entry could not be freed.
///
/// @param[in] packer Packer used to write entry.
/// @param[in] entry Entry written.
/// @param[in] max_kbyte Maximum size of an item in KiB. Zero means no
/// restrictions.
static inline ShaDaWriteResult shada_pack_pfreed_entry(msgpack_packer *const packer,
PossiblyFreedShadaEntry entry,
const size_t max_kbyte)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE
{
ShaDaWriteResult ret = kSDWriteSuccessful;
ret = shada_pack_entry(packer, entry.data, max_kbyte);
if (entry.can_free_entry) {
shada_free_shada_entry(&entry.data);
}
return ret;
}
/// Compare two FileMarks structure to order them by greatest_timestamp
///
/// Order is reversed: structure with greatest greatest_timestamp comes first.
/// Function signature is compatible with qsort.
static int compare_file_marks(const void *a, const void *b)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
const FileMarks *const *const a_fms = a;
const FileMarks *const *const b_fms = b;
return ((*a_fms)->greatest_timestamp == (*b_fms)->greatest_timestamp
? 0
: ((*a_fms)->greatest_timestamp > (*b_fms)->greatest_timestamp
? -1
: 1));
}
/// Parse msgpack object that has given length
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[in] length Object length.
/// @param[out] ret_unpacked Location where read result should be saved. If
/// NULL then unpacked data will be freed. Must be
/// NULL if `ret_buf` is NULL.
/// @param[out] ret_buf Buffer containing parsed string.
///
/// @return kSDReadStatusNotShaDa, kSDReadStatusReadError or
/// kSDReadStatusSuccess.
static inline ShaDaReadResult shada_parse_msgpack(ShaDaReadDef *const sd_reader,
const size_t length,
msgpack_unpacked *ret_unpacked,
char **const ret_buf)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
{
const uintmax_t initial_fpos = sd_reader->fpos;
char *const buf = xmalloc(length);
const ShaDaReadResult fl_ret = fread_len(sd_reader, buf, length);
if (fl_ret != kSDReadStatusSuccess) {
xfree(buf);
return fl_ret;
}
bool did_try_to_free = false;
shada_parse_msgpack_read_next: {}
size_t off = 0;
msgpack_unpacked unpacked;
msgpack_unpacked_init(&unpacked);
const msgpack_unpack_return result =
msgpack_unpack_next(&unpacked, buf, length, &off);
ShaDaReadResult ret = kSDReadStatusSuccess;
switch (result) {
case MSGPACK_UNPACK_SUCCESS:
if (off < length) {
goto shada_parse_msgpack_extra_bytes;
}
break;
case MSGPACK_UNPACK_PARSE_ERROR:
semsg(_(RCERR "Failed to parse ShaDa file due to a msgpack parser error "
"at position %" PRIu64),
(uint64_t)initial_fpos);
ret = kSDReadStatusNotShaDa;
break;
case MSGPACK_UNPACK_NOMEM_ERROR:
if (!did_try_to_free) {
did_try_to_free = true;
try_to_free_memory();
goto shada_parse_msgpack_read_next;
}
emsg(_(e_outofmem));
ret = kSDReadStatusReadError;
break;
case MSGPACK_UNPACK_CONTINUE:
semsg(_(RCERR "Failed to parse ShaDa file: incomplete msgpack string "
"at position %" PRIu64),
(uint64_t)initial_fpos);
ret = kSDReadStatusNotShaDa;
break;
case MSGPACK_UNPACK_EXTRA_BYTES:
shada_parse_msgpack_extra_bytes:
semsg(_(RCERR "Failed to parse ShaDa file: extra bytes in msgpack string "
"at position %" PRIu64),
(uint64_t)initial_fpos);
ret = kSDReadStatusNotShaDa;
break;
}
if (ret_buf != NULL && ret == kSDReadStatusSuccess) {
if (ret_unpacked == NULL) {
msgpack_unpacked_destroy(&unpacked);
} else {
*ret_unpacked = unpacked;
}
*ret_buf = buf;
} else {
assert(ret_buf == NULL || ret != kSDReadStatusSuccess);
msgpack_unpacked_destroy(&unpacked);
xfree(buf);
}
return ret;
}
/// Format shada entry for debugging purposes
///
/// @param[in] entry ShaDa entry to format.
///
/// @return string representing ShaDa entry in a static buffer.
static const char *shada_format_entry(const ShadaEntry entry)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_UNUSED FUNC_ATTR_NONNULL_RET
{
static char ret[1024];
ret[0] = 0;
vim_snprintf(S_LEN(ret), "%s", "[ ] ts=%" PRIu64 " ");
// ^ Space for `can_free_entry`
#define FORMAT_MARK_ENTRY(entry_name, name_fmt, name_fmt_arg) \
do { \
typval_T ad_tv = { \
.v_type = VAR_DICT, \
.vval.v_dict = entry.data.filemark.additional_data \
}; \
size_t ad_len; \
char *const ad = encode_tv2string(&ad_tv, &ad_len); \
vim_snprintf_add(S_LEN(ret), \
entry_name " {" name_fmt " file=[%zu]\"%.512s\", " \
"pos={l=%" PRIdLINENR ",c=%" PRIdCOLNR ",a=%" PRIdCOLNR "}, " \
"ad={%p:[%zu]%.64s} }", \
name_fmt_arg, \
strlen(entry.data.filemark.fname), \
entry.data.filemark.fname, \
entry.data.filemark.mark.lnum, \
entry.data.filemark.mark.col, \
entry.data.filemark.mark.coladd, \
(void *)entry.data.filemark.additional_data, \
ad_len, \
ad); \
} while (0)
switch (entry.type) {
case kSDItemMissing:
vim_snprintf_add(S_LEN(ret), "Missing");
break;
case kSDItemHeader:
vim_snprintf_add(S_LEN(ret), "Header { TODO }");
break;
case kSDItemBufferList:
vim_snprintf_add(S_LEN(ret), "BufferList { TODO }");
break;
case kSDItemUnknown:
vim_snprintf_add(S_LEN(ret), "Unknown { TODO }");
break;
case kSDItemSearchPattern:
vim_snprintf_add(S_LEN(ret), "SearchPattern { TODO }");
break;
case kSDItemSubString:
vim_snprintf_add(S_LEN(ret), "SubString { TODO }");
break;
case kSDItemHistoryEntry:
vim_snprintf_add(S_LEN(ret), "HistoryEntry { TODO }");
break;
case kSDItemRegister:
vim_snprintf_add(S_LEN(ret), "Register { TODO }");
break;
case kSDItemVariable:
vim_snprintf_add(S_LEN(ret), "Variable { TODO }");
break;
case kSDItemGlobalMark:
FORMAT_MARK_ENTRY("GlobalMark", " name='%c',", entry.data.filemark.name);
break;
case kSDItemChange:
FORMAT_MARK_ENTRY("Change", "%s", "");
break;
case kSDItemLocalMark:
FORMAT_MARK_ENTRY("LocalMark", " name='%c',", entry.data.filemark.name);
break;
case kSDItemJump:
FORMAT_MARK_ENTRY("Jump", "%s", "");
break;
#undef FORMAT_MARK_ENTRY
}
return ret;
}
/// Format possibly freed shada entry for debugging purposes
///
/// @param[in] entry ShaDa entry to format.
///
/// @return string representing ShaDa entry in a static buffer.
static const char *shada_format_pfreed_entry(const PossiblyFreedShadaEntry pfs_entry)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_UNUSED FUNC_ATTR_NONNULL_RET
{
char *ret = (char *)shada_format_entry(pfs_entry.data);
ret[1] = (pfs_entry.can_free_entry ? 'T' : 'F');
return ret;
}
/// Read and merge in ShaDa file, used when writing
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[in] srni_flags Flags determining what to read.
/// @param[in] max_kbyte Maximum size of one element.
/// @param[in,out] ret_wms Location where results are saved.
/// @param[out] packer MessagePack packer for entries which are not
/// merged.
static inline ShaDaWriteResult shada_read_when_writing(ShaDaReadDef *const sd_reader,
const unsigned srni_flags,
const size_t max_kbyte,
WriteMergerState *const wms,
msgpack_packer *const packer)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
ShaDaWriteResult ret = kSDWriteSuccessful;
ShadaEntry entry;
ShaDaReadResult srni_ret;
#define COMPARE_WITH_ENTRY(wms_entry_, entry) \
do { \
PossiblyFreedShadaEntry *const wms_entry = (wms_entry_); \
if (wms_entry->data.type != kSDItemMissing) { \
if (wms_entry->data.timestamp >= (entry).timestamp) { \
shada_free_shada_entry(&(entry)); \
break; \
} \
if (wms_entry->can_free_entry) { \
shada_free_shada_entry(&wms_entry->data); \
} \
} \
*wms_entry = pfs_entry; \
} while (0)
#define FREE_POSSIBLY_FREED_SHADA_ENTRY(entry) \
do { \
if ((entry).can_free_entry) { \
shada_free_shada_entry(&(entry).data); \
} \
} while (0)
#define SDE_TO_PFSDE(entry) \
((PossiblyFreedShadaEntry) { .can_free_entry = true, .data = (entry) })
while ((srni_ret = shada_read_next_item(sd_reader, &entry, srni_flags,
max_kbyte))
!= kSDReadStatusFinished) {
switch (srni_ret) {
case kSDReadStatusSuccess:
break;
case kSDReadStatusFinished:
// Should be handled by the while condition.
abort();
case kSDReadStatusNotShaDa:
ret = kSDWriteReadNotShada;
FALLTHROUGH;
case kSDReadStatusReadError:
return ret;
case kSDReadStatusMalformed:
continue;
}
const PossiblyFreedShadaEntry pfs_entry = {
.can_free_entry = true,
.data = entry,
};
switch (entry.type) {
case kSDItemMissing:
break;
case kSDItemHeader:
case kSDItemBufferList:
abort();
case kSDItemUnknown:
ret = shada_pack_entry(packer, entry, 0);
shada_free_shada_entry(&entry);
break;
case kSDItemSearchPattern:
COMPARE_WITH_ENTRY((entry.data.search_pattern.is_substitute_pattern
? &wms->sub_search_pattern
: &wms->search_pattern), entry);
break;
case kSDItemSubString:
COMPARE_WITH_ENTRY(&wms->replacement, entry);
break;
case kSDItemHistoryEntry:
if (entry.data.history_item.histtype >= HIST_COUNT) {
ret = shada_pack_entry(packer, entry, 0);
shada_free_shada_entry(&entry);
break;
}
if (wms->hms[entry.data.history_item.histtype].hmll.size != 0) {
hms_insert(&wms->hms[entry.data.history_item.histtype], entry, true,
true);
} else {
shada_free_shada_entry(&entry);
}
break;
case kSDItemRegister: {
const int idx = op_reg_index(entry.data.reg.name);
if (idx < 0) {
ret = shada_pack_entry(packer, entry, 0);
shada_free_shada_entry(&entry);
break;
}
COMPARE_WITH_ENTRY(&wms->registers[idx], entry);
break;
}
case kSDItemVariable:
if (!set_has(cstr_t, &wms->dumped_variables, entry.data.global_var.name)) {
ret = shada_pack_entry(packer, entry, 0);
}
shada_free_shada_entry(&entry);
break;
case kSDItemGlobalMark:
if (ascii_isdigit(entry.data.filemark.name)) {
bool processed_mark = false;
// Completely ignore numbered mark names, make a list sorted by
// timestamp.
for (size_t i = ARRAY_SIZE(wms->numbered_marks); i > 0; i--) {
ShadaEntry wms_entry = wms->numbered_marks[i - 1].data;
if (wms_entry.type != kSDItemGlobalMark) {
continue;
}
// Ignore duplicates.
if (wms_entry.timestamp == entry.timestamp
&& (wms_entry.data.filemark.additional_data == NULL
&& entry.data.filemark.additional_data == NULL)
&& marks_equal(wms_entry.data.filemark.mark,
entry.data.filemark.mark)
&& strcmp(wms_entry.data.filemark.fname,
entry.data.filemark.fname) == 0) {
shada_free_shada_entry(&entry);
processed_mark = true;
break;
}
if (wms_entry.timestamp >= entry.timestamp) {
processed_mark = true;
if (i < ARRAY_SIZE(wms->numbered_marks)) {
replace_numbered_mark(wms, i, pfs_entry);
} else {
shada_free_shada_entry(&entry);
}
break;
}
}
if (!processed_mark) {
replace_numbered_mark(wms, 0, pfs_entry);
}
} else {
const int idx = mark_global_index(entry.data.filemark.name);
if (idx < 0) {
ret = shada_pack_entry(packer, entry, 0);
shada_free_shada_entry(&entry);
break;
}
if (wms->global_marks[idx].data.type == kSDItemMissing) {
if (namedfm[idx].fmark.timestamp >= entry.timestamp) {
shada_free_shada_entry(&entry);
break;
}
}
COMPARE_WITH_ENTRY(&wms->global_marks[idx], entry);
}
break;
case kSDItemChange:
case kSDItemLocalMark: {
if (shada_removable(entry.data.filemark.fname)) {
shada_free_shada_entry(&entry);
break;
}
const char *const fname = entry.data.filemark.fname;
cstr_t *key = NULL;
ptr_t *val = pmap_put_ref(cstr_t)(&wms->file_marks, fname, &key, NULL);
if (*val == NULL) {
*val = xcalloc(1, sizeof(FileMarks));
}
FileMarks *const filemarks = *val;
if (entry.timestamp > filemarks->greatest_timestamp) {
filemarks->greatest_timestamp = entry.timestamp;
}
if (entry.type == kSDItemLocalMark) {
const int idx = mark_local_index(entry.data.filemark.name);
if (idx < 0) {
filemarks->additional_marks = xrealloc(filemarks->additional_marks,
(++filemarks->additional_marks_size
* sizeof(filemarks->additional_marks[0])));
filemarks->additional_marks[filemarks->additional_marks_size - 1] =
entry;
} else {
PossiblyFreedShadaEntry *const wms_entry = &filemarks->marks[idx];
bool set_wms = true;
if (wms_entry->data.type != kSDItemMissing) {
if (wms_entry->data.timestamp >= entry.timestamp) {
shada_free_shada_entry(&entry);
break;
}
if (wms_entry->can_free_entry) {
if (*key == wms_entry->data.data.filemark.fname) {
*key = entry.data.filemark.fname;
}
shada_free_shada_entry(&wms_entry->data);
}
} else {
FOR_ALL_BUFFERS(buf) {
if (buf->b_ffname != NULL
&& path_fnamecmp(entry.data.filemark.fname, buf->b_ffname) == 0) {
fmark_T fm;
mark_get(buf, curwin, &fm, kMarkBufLocal, (int)entry.data.filemark.name);
if (fm.timestamp >= entry.timestamp) {
set_wms = false;
shada_free_shada_entry(&entry);
break;
}
}
}
}
if (set_wms) {
*wms_entry = pfs_entry;
}
}
} else {
#define AFTERFREE_DUMMY(entry)
#define DUMMY_IDX_ADJ(i)
MERGE_JUMPS(filemarks->changes_size, filemarks->changes,
PossiblyFreedShadaEntry, data.timestamp,
data.data.filemark.mark, entry, true,
FREE_POSSIBLY_FREED_SHADA_ENTRY, SDE_TO_PFSDE,
DUMMY_IDX_ADJ, AFTERFREE_DUMMY);
}
break;
}
case kSDItemJump:
MERGE_JUMPS(wms->jumps_size, wms->jumps, PossiblyFreedShadaEntry,
data.timestamp, data.data.filemark.mark, entry,
strcmp(jl_entry.data.data.filemark.fname,
entry.data.filemark.fname) == 0,
FREE_POSSIBLY_FREED_SHADA_ENTRY, SDE_TO_PFSDE,
DUMMY_IDX_ADJ, AFTERFREE_DUMMY);
#undef FREE_POSSIBLY_FREED_SHADA_ENTRY
#undef SDE_TO_PFSDE
#undef DUMMY_IDX_ADJ
#undef AFTERFREE_DUMMY
break;
}
}
#undef COMPARE_WITH_ENTRY
return ret;
}
/// Check whether buffer should be ignored
///
/// @param[in] buf buf_T* to check.
/// @param[in] removable_bufs Cache of buffers ignored due to their location.
///
/// @return true or false.
static inline bool ignore_buf(const buf_T *const buf, Set(ptr_t) *const removable_bufs)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALWAYS_INLINE
{
return (buf->b_ffname == NULL || !buf->b_p_bl || bt_quickfix(buf) \
|| bt_terminal(buf) || set_has(ptr_t, removable_bufs, (ptr_t)buf));
}
/// Get list of buffers to write to the shada file
///
/// @param[in] removable_bufs Buffers which are ignored
///
/// @return ShadaEntry List of buffers to save, kSDItemBufferList entry.
static inline ShadaEntry shada_get_buflist(Set(ptr_t) *const removable_bufs)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALWAYS_INLINE
{
int max_bufs = get_shada_parameter('%');
size_t buf_count = 0;
FOR_ALL_BUFFERS(buf) {
if (!ignore_buf(buf, removable_bufs)
&& (max_bufs < 0 || buf_count < (size_t)max_bufs)) {
buf_count++;
}
}
ShadaEntry buflist_entry = (ShadaEntry) {
.type = kSDItemBufferList,
.timestamp = os_time(),
.data = {
.buffer_list = {
.size = buf_count,
.buffers = xmalloc(buf_count
* sizeof(*buflist_entry.data.buffer_list.buffers)),
},
},
};
size_t i = 0;
FOR_ALL_BUFFERS(buf) {
if (ignore_buf(buf, removable_bufs)) {
continue;
}
if (i >= buf_count) {
break;
}
buflist_entry.data.buffer_list.buffers[i] = (struct buffer_list_buffer) {
.pos = buf->b_last_cursor.mark,
.fname = buf->b_ffname,
.additional_data = buf->additional_data,
};
i++;
}
return buflist_entry;
}
/// Save search pattern to PossiblyFreedShadaEntry
///
/// @param[out] ret_pse Location where result will be saved.
/// @param[in] get_pattern Function used to get pattern.
/// @param[in] is_substitute_pattern True if pattern in question is substitute
/// pattern. Also controls whether some
/// fields should be initialized to default
/// or values from get_pattern.
/// @param[in] search_last_used Result of search_was_last_used().
/// @param[in] search_highlighted True if search pattern was highlighted by
/// &hlsearch and this information should be
/// saved.
static inline void add_search_pattern(PossiblyFreedShadaEntry *const ret_pse,
const SearchPatternGetter get_pattern,
const bool is_substitute_pattern, const bool search_last_used,
const bool search_highlighted)
FUNC_ATTR_ALWAYS_INLINE
{
const ShadaEntry defaults = sd_default_values[kSDItemSearchPattern];
SearchPattern pat;
get_pattern(&pat);
if (pat.pat != NULL) {
*ret_pse = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemSearchPattern,
.timestamp = pat.timestamp,
.data = {
.search_pattern = {
.magic = pat.magic,
.smartcase = !pat.no_scs,
.has_line_offset = (is_substitute_pattern
? defaults.data.search_pattern.has_line_offset
: pat.off.line),
.place_cursor_at_end = (
is_substitute_pattern
? defaults.data.search_pattern.place_cursor_at_end
: pat.off.end),
.offset = (is_substitute_pattern
? defaults.data.search_pattern.offset
: pat.off.off),
.is_last_used = (is_substitute_pattern ^ search_last_used),
.is_substitute_pattern = is_substitute_pattern,
.highlighted = ((is_substitute_pattern ^ search_last_used)
&& search_highlighted),
.pat = pat.pat,
.additional_data = pat.additional_data,
.search_backward = (!is_substitute_pattern && pat.off.dir == '?'),
}
}
}
};
}
}
/// Initialize registers for writing to the ShaDa file
///
/// @param[in] wms The WriteMergerState used when writing.
/// @param[in] max_reg_lines The maximum number of register lines.
static inline void shada_initialize_registers(WriteMergerState *const wms, int max_reg_lines)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE
{
iter_register_T reg_iter = ITER_REGISTER_NULL;
const bool limit_reg_lines = max_reg_lines >= 0;
do {
yankreg_T reg;
char name = NUL;
bool is_unnamed = false;
reg_iter = op_global_reg_iter(reg_iter, &name, ®, &is_unnamed);
if (name == NUL) {
break;
}
if (limit_reg_lines && reg.y_size > (size_t)max_reg_lines) {
continue;
}
wms->registers[op_reg_index(name)] = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemRegister,
.timestamp = reg.timestamp,
.data = {
.reg = {
.contents = reg.y_array,
.contents_size = reg.y_size,
.type = reg.y_type,
.width = (size_t)(reg.y_type == kMTBlockWise ? reg.y_width : 0),
.additional_data = reg.additional_data,
.name = name,
.is_unnamed = is_unnamed,
}
}
}
};
} while (reg_iter != ITER_REGISTER_NULL);
}
/// Replace numbered mark in WriteMergerState
///
/// Frees the last mark, moves (including adjusting mark names) marks from idx
/// to the last-but-one one and saves the new mark at given index.
///
/// @param[out] wms Merger state to adjust.
/// @param[in] idx Index at which new mark should be placed.
/// @param[in] entry New mark.
static inline void replace_numbered_mark(WriteMergerState *const wms, const size_t idx,
const PossiblyFreedShadaEntry entry)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE
{
if (ARRAY_LAST_ENTRY(wms->numbered_marks).can_free_entry) {
shada_free_shada_entry(&ARRAY_LAST_ENTRY(wms->numbered_marks).data);
}
for (size_t i = idx; i < ARRAY_SIZE(wms->numbered_marks) - 1; i++) {
if (wms->numbered_marks[i].data.type == kSDItemGlobalMark) {
wms->numbered_marks[i].data.data.filemark.name = (char)('0' + (int)i + 1);
}
}
memmove(wms->numbered_marks + idx + 1, wms->numbered_marks + idx,
sizeof(wms->numbered_marks[0])
* (ARRAY_SIZE(wms->numbered_marks) - 1 - idx));
wms->numbered_marks[idx] = entry;
wms->numbered_marks[idx].data.data.filemark.name = (char)('0' + (int)idx);
}
/// Find buffers ignored due to their location.
///
/// @param[out] removable_bufs Cache of buffers ignored due to their location.
static inline void find_removable_bufs(Set(ptr_t) *removable_bufs)
{
FOR_ALL_BUFFERS(buf) {
if (buf->b_ffname != NULL && shada_removable(buf->b_ffname)) {
set_put(ptr_t, removable_bufs, (ptr_t)buf);
}
}
}
/// Translate a history type number to the associated character
static int hist_type2char(const int type)
FUNC_ATTR_CONST
{
switch (type) {
case HIST_CMD:
return ':';
case HIST_SEARCH:
return '/';
case HIST_EXPR:
return '=';
case HIST_INPUT:
return '@';
case HIST_DEBUG:
return '>';
default:
abort();
}
return NUL;
}
/// Write ShaDa file
///
/// @param[in] sd_writer Structure containing file writer definition.
/// @param[in] sd_reader Structure containing file reader definition. If it is
/// not NULL then contents of this file will be merged
/// with current Neovim runtime.
static ShaDaWriteResult shada_write(ShaDaWriteDef *const sd_writer, ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ARG(1)
{
ShaDaWriteResult ret = kSDWriteSuccessful;
int max_kbyte_i = get_shada_parameter('s');
if (max_kbyte_i < 0) {
max_kbyte_i = 10;
}
if (max_kbyte_i == 0) {
return ret;
}
WriteMergerState *const wms = xcalloc(1, sizeof(*wms));
bool dump_one_history[HIST_COUNT];
const bool dump_global_vars = (find_shada_parameter('!') != NULL);
int max_reg_lines = get_shada_parameter('<');
if (max_reg_lines < 0) {
max_reg_lines = get_shada_parameter('"');
}
const bool dump_registers = (max_reg_lines != 0);
Set(ptr_t) removable_bufs = SET_INIT;
const size_t max_kbyte = (size_t)max_kbyte_i;
const size_t num_marked_files = (size_t)get_shada_parameter('\'');
const bool dump_global_marks = get_shada_parameter('f') != 0;
bool dump_history = false;
// Initialize history merger
for (int i = 0; i < HIST_COUNT; i++) {
int num_saved = get_shada_parameter(hist_type2char(i));
if (num_saved == -1) {
num_saved = (int)p_hi;
}
if (num_saved > 0) {
dump_history = true;
dump_one_history[i] = true;
hms_init(&wms->hms[i], (uint8_t)i, (size_t)num_saved, sd_reader != NULL, false);
} else {
dump_one_history[i] = false;
}
}
const unsigned srni_flags = (unsigned)(
kSDReadUndisableableData
| kSDReadUnknown
| (dump_history ? kSDReadHistory : 0)
| (dump_registers ? kSDReadRegisters : 0)
| (dump_global_vars ? kSDReadVariables : 0)
| (dump_global_marks ? kSDReadGlobalMarks : 0)
| (num_marked_files ? kSDReadLocalMarks |
kSDReadChanges : 0));
msgpack_packer *const packer = msgpack_packer_new(sd_writer,
&msgpack_sd_writer_write);
// Set b_last_cursor for all the buffers that have a window.
//
// It is needed to correctly save '"' mark on exit. Has a side effect of
// setting '"' mark in all windows on :wshada to the current cursor
// position (basically what :wviminfo used to do).
FOR_ALL_TAB_WINDOWS(tp, wp) {
set_last_cursor(wp);
}
find_removable_bufs(&removable_bufs);
// Write header
if (shada_pack_entry(packer, (ShadaEntry) {
.type = kSDItemHeader,
.timestamp = os_time(),
.data = {
.header = {
.size = 5,
.capacity = 5,
.items = ((KeyValuePair[]) {
{ STATIC_CSTR_AS_STRING("generator"),
STATIC_CSTR_AS_OBJ("nvim") },
{ STATIC_CSTR_AS_STRING("version"),
CSTR_AS_OBJ(longVersion) },
{ STATIC_CSTR_AS_STRING("max_kbyte"),
INTEGER_OBJ((Integer)max_kbyte) },
{ STATIC_CSTR_AS_STRING("pid"),
INTEGER_OBJ((Integer)os_get_pid()) },
{ STATIC_CSTR_AS_STRING("encoding"),
CSTR_AS_OBJ(p_enc) },
}),
}
}
}, 0) == kSDWriteFailed) {
ret = kSDWriteFailed;
goto shada_write_exit;
}
// Write buffer list
if (find_shada_parameter('%') != NULL) {
ShadaEntry buflist_entry = shada_get_buflist(&removable_bufs);
if (shada_pack_entry(packer, buflist_entry, 0) == kSDWriteFailed) {
xfree(buflist_entry.data.buffer_list.buffers);
ret = kSDWriteFailed;
goto shada_write_exit;
}
xfree(buflist_entry.data.buffer_list.buffers);
}
// Write some of the variables
if (dump_global_vars) {
const void *var_iter = NULL;
const Timestamp cur_timestamp = os_time();
do {
typval_T vartv;
const char *name = NULL;
var_iter = var_shada_iter(var_iter, &name, &vartv, VAR_FLAVOUR_SHADA);
if (name == NULL) {
break;
}
switch (vartv.v_type) {
case VAR_FUNC:
case VAR_PARTIAL:
tv_clear(&vartv);
continue;
case VAR_DICT: {
dict_T *di = vartv.vval.v_dict;
int copyID = get_copyID();
if (!set_ref_in_ht(&di->dv_hashtab, copyID, NULL)
&& copyID == di->dv_copyID) {
tv_clear(&vartv);
continue;
}
break;
}
case VAR_LIST: {
list_T *l = vartv.vval.v_list;
int copyID = get_copyID();
if (!set_ref_in_list(l, copyID, NULL)
&& copyID == l->lv_copyID) {
tv_clear(&vartv);
continue;
}
break;
}
default:
break;
}
typval_T tgttv;
tv_copy(&vartv, &tgttv);
ShaDaWriteResult spe_ret;
if ((spe_ret = shada_pack_entry(packer, (ShadaEntry) {
.type = kSDItemVariable,
.timestamp = cur_timestamp,
.data = {
.global_var = {
.name = (char *)name,
.value = tgttv,
.additional_elements = NULL,
}
}
}, max_kbyte)) == kSDWriteFailed) {
tv_clear(&vartv);
tv_clear(&tgttv);
ret = kSDWriteFailed;
goto shada_write_exit;
}
tv_clear(&vartv);
tv_clear(&tgttv);
if (spe_ret == kSDWriteSuccessful) {
set_put(cstr_t, &wms->dumped_variables, name);
}
} while (var_iter != NULL);
}
// Initialize jump list
wms->jumps_size = shada_init_jumps(wms->jumps, &removable_bufs);
const bool search_highlighted = !(no_hlsearch
|| find_shada_parameter('h') != NULL);
const bool search_last_used = search_was_last_used();
// Initialize search pattern
add_search_pattern(&wms->search_pattern, &get_search_pattern, false,
search_last_used, search_highlighted);
// Initialize substitute search pattern
add_search_pattern(&wms->sub_search_pattern, &get_substitute_pattern, true,
search_last_used, search_highlighted);
// Initialize substitute replacement string
{
SubReplacementString sub;
sub_get_replacement(&sub);
wms->replacement = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemSubString,
.timestamp = sub.timestamp,
.data = {
.sub_string = {
.sub = sub.sub,
.additional_elements = sub.additional_elements,
}
}
}
};
}
// Initialize global marks
if (dump_global_marks) {
const void *global_mark_iter = NULL;
size_t digit_mark_idx = 0;
do {
char name = NUL;
xfmark_T fm;
global_mark_iter = mark_global_iter(global_mark_iter, &name, &fm);
if (name == NUL) {
break;
}
const char *fname;
if (fm.fmark.fnum == 0) {
assert(fm.fname != NULL);
if (shada_removable(fm.fname)) {
continue;
}
fname = fm.fname;
} else {
const buf_T *const buf = buflist_findnr(fm.fmark.fnum);
if (buf == NULL || buf->b_ffname == NULL
|| set_has(ptr_t, &removable_bufs, (ptr_t)buf)) {
continue;
}
fname = buf->b_ffname;
}
const PossiblyFreedShadaEntry pf_entry = {
.can_free_entry = false,
.data = {
.type = kSDItemGlobalMark,
.timestamp = fm.fmark.timestamp,
.data = {
.filemark = {
.mark = fm.fmark.mark,
.name = name,
.additional_data = fm.fmark.additional_data,
.fname = (char *)fname,
}
}
},
};
if (ascii_isdigit(name)) {
replace_numbered_mark(wms, digit_mark_idx++, pf_entry);
} else {
wms->global_marks[mark_global_index(name)] = pf_entry;
}
} while (global_mark_iter != NULL);
}
// Initialize registers
if (dump_registers) {
shada_initialize_registers(wms, max_reg_lines);
}
// Initialize buffers
if (num_marked_files > 0) {
FOR_ALL_BUFFERS(buf) {
if (buf->b_ffname == NULL || set_has(ptr_t, &removable_bufs, buf)) {
continue;
}
const void *local_marks_iter = NULL;
const char *const fname = buf->b_ffname;
ptr_t *val = pmap_put_ref(cstr_t)(&wms->file_marks, fname, NULL, NULL);
if (*val == NULL) {
*val = xcalloc(1, sizeof(FileMarks));
}
FileMarks *const filemarks = *val;
do {
fmark_T fm;
char name = NUL;
local_marks_iter = mark_buffer_iter(local_marks_iter, buf, &name, &fm);
if (name == NUL) {
break;
}
filemarks->marks[mark_local_index(name)] = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemLocalMark,
.timestamp = fm.timestamp,
.data = {
.filemark = {
.mark = fm.mark,
.name = name,
.fname = (char *)fname,
.additional_data = fm.additional_data,
}
}
}
};
if (fm.timestamp > filemarks->greatest_timestamp) {
filemarks->greatest_timestamp = fm.timestamp;
}
} while (local_marks_iter != NULL);
for (int i = 0; i < buf->b_changelistlen; i++) {
const fmark_T fm = buf->b_changelist[i];
filemarks->changes[i] = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemChange,
.timestamp = fm.timestamp,
.data = {
.filemark = {
.mark = fm.mark,
.fname = (char *)fname,
.additional_data = fm.additional_data,
}
}
}
};
if (fm.timestamp > filemarks->greatest_timestamp) {
filemarks->greatest_timestamp = fm.timestamp;
}
}
filemarks->changes_size = (size_t)buf->b_changelistlen;
}
}
if (sd_reader != NULL) {
const ShaDaWriteResult srww_ret = shada_read_when_writing(sd_reader, srni_flags, max_kbyte, wms,
packer);
if (srww_ret != kSDWriteSuccessful) {
ret = srww_ret;
}
}
// Update numbered marks: '0' should be replaced with the current position,
// '9' should be removed and all other marks shifted.
if (!ignore_buf(curbuf, &removable_bufs) && curwin->w_cursor.lnum != 0) {
replace_numbered_mark(wms, 0, (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemGlobalMark,
.timestamp = os_time(),
.data = {
.filemark = {
.mark = curwin->w_cursor,
.name = '0',
.additional_data = NULL,
.fname = curbuf->b_ffname,
}
}
},
});
}
// Write the rest
#define PACK_WMS_ARRAY(wms_array) \
do { \
for (size_t i_ = 0; i_ < ARRAY_SIZE(wms_array); i_++) { \
if ((wms_array)[i_].data.type != kSDItemMissing) { \
if (shada_pack_pfreed_entry(packer, (wms_array)[i_], max_kbyte) \
== kSDWriteFailed) { \
ret = kSDWriteFailed; \
goto shada_write_exit; \
} \
} \
} \
} while (0)
PACK_WMS_ARRAY(wms->global_marks);
PACK_WMS_ARRAY(wms->numbered_marks);
PACK_WMS_ARRAY(wms->registers);
for (size_t i = 0; i < wms->jumps_size; i++) {
if (shada_pack_pfreed_entry(packer, wms->jumps[i], max_kbyte)
== kSDWriteFailed) {
ret = kSDWriteFailed;
goto shada_write_exit;
}
}
#define PACK_WMS_ENTRY(wms_entry) \
do { \
if ((wms_entry).data.type != kSDItemMissing) { \
if (shada_pack_pfreed_entry(packer, wms_entry, max_kbyte) \
== kSDWriteFailed) { \
ret = kSDWriteFailed; \
goto shada_write_exit; \
} \
} \
} while (0)
PACK_WMS_ENTRY(wms->search_pattern);
PACK_WMS_ENTRY(wms->sub_search_pattern);
PACK_WMS_ENTRY(wms->replacement);
#undef PACK_WMS_ENTRY
const size_t file_markss_size = map_size(&wms->file_marks);
FileMarks **const all_file_markss =
xmalloc(file_markss_size * sizeof(*all_file_markss));
FileMarks **cur_file_marks = all_file_markss;
ptr_t val;
map_foreach_value(&wms->file_marks, val, {
*cur_file_marks++ = val;
})
qsort((void *)all_file_markss, file_markss_size, sizeof(*all_file_markss),
&compare_file_marks);
const size_t file_markss_to_dump = MIN(num_marked_files, file_markss_size);
for (size_t i = 0; i < file_markss_to_dump; i++) {
PACK_WMS_ARRAY(all_file_markss[i]->marks);
for (size_t j = 0; j < all_file_markss[i]->changes_size; j++) {
if (shada_pack_pfreed_entry(packer, all_file_markss[i]->changes[j],
max_kbyte) == kSDWriteFailed) {
ret = kSDWriteFailed;
goto shada_write_exit;
}
}
for (size_t j = 0; j < all_file_markss[i]->additional_marks_size; j++) {
if (shada_pack_entry(packer, all_file_markss[i]->additional_marks[j],
0) == kSDWriteFailed) {
shada_free_shada_entry(&all_file_markss[i]->additional_marks[j]);
ret = kSDWriteFailed;
goto shada_write_exit;
}
shada_free_shada_entry(&all_file_markss[i]->additional_marks[j]);
}
xfree(all_file_markss[i]->additional_marks);
}
xfree(all_file_markss);
#undef PACK_WMS_ARRAY
if (dump_history) {
for (int i = 0; i < HIST_COUNT; i++) {
if (dump_one_history[i]) {
hms_insert_whole_neovim_history(&wms->hms[i]);
HMS_ITER(&wms->hms[i], cur_entry, {
if (shada_pack_pfreed_entry(packer, (PossiblyFreedShadaEntry) {
.data = cur_entry->data,
.can_free_entry = cur_entry->can_free_entry,
}, max_kbyte) == kSDWriteFailed) {
ret = kSDWriteFailed;
break;
}
})
if (ret == kSDWriteFailed) {
goto shada_write_exit;
}
}
}
}
shada_write_exit:
for (int i = 0; i < HIST_COUNT; i++) {
if (dump_one_history[i]) {
hms_dealloc(&wms->hms[i]);
}
}
map_foreach_value(&wms->file_marks, val, {
xfree(val);
})
map_destroy(cstr_t, &wms->file_marks);
set_destroy(ptr_t, &removable_bufs);
msgpack_packer_free(packer);
set_destroy(cstr_t, &wms->dumped_variables);
xfree(wms);
return ret;
}
#undef PACK_STATIC_STR
/// Write ShaDa file to a given location
///
/// @param[in] fname File to write to. If it is NULL or empty then default
/// location is used.
/// @param[in] nomerge If true then old file is ignored.
///
/// @return OK if writing was successful, FAIL otherwise.
int shada_write_file(const char *const file, bool nomerge)
{
if (shada_disabled()) {
return FAIL;
}
char *const fname = shada_filename(file);
char *tempname = NULL;
ShaDaWriteDef sd_writer = {
.write = &write_file,
.close = &close_sd_writer,
.error = NULL,
};
ShaDaReadDef sd_reader = { .close = NULL };
if (!nomerge) {
int error;
if ((error = open_shada_file_for_reading(fname, &sd_reader)) != 0) {
if (error != UV_ENOENT) {
semsg(_(SERR "System error while opening ShaDa file %s for reading "
"to merge before writing it: %s"),
fname, os_strerror(error));
// Try writing the file even if opening it emerged any issues besides
// file not existing: maybe writing will succeed nevertheless.
}
nomerge = true;
goto shada_write_file_nomerge;
}
tempname = modname(fname, ".tmp.a", false);
if (tempname == NULL) {
nomerge = true;
goto shada_write_file_nomerge;
}
// Save permissions from the original file, with modifications:
int perm = (int)os_getperm(fname);
perm = (perm >= 0) ? ((perm & 0777) | 0600) : 0600;
// ^3 ^1 ^2 ^2,3
// 1: Strip SUID bit if any.
// 2: Make sure that user can always read and write the result.
// 3: If somebody happened to delete the file after it was opened for
// reading use u=rw permissions.
shada_write_file_open: {}
sd_writer.cookie = file_open_new(&error, tempname, kFileCreateOnly|kFileNoSymlink, perm);
if (sd_writer.cookie == NULL) {
if (error == UV_EEXIST || error == UV_ELOOP) {
// File already exists, try another name
char *const wp = tempname + strlen(tempname) - 1;
if (*wp == 'z') {
// Tried names from .tmp.a to .tmp.z, all failed. Something must be
// wrong then.
semsg(_("E138: All %s.tmp.X files exist, cannot write ShaDa file!"),
fname);
xfree(fname);
xfree(tempname);
assert(sd_reader.close != NULL);
sd_reader.close(&sd_reader);
return FAIL;
}
(*wp)++;
goto shada_write_file_open;
} else {
semsg(_(SERR "System error while opening temporary ShaDa file %s "
"for writing: %s"), tempname, os_strerror(error));
}
}
}
if (nomerge) {
shada_write_file_nomerge: {}
char *const tail = path_tail_with_sep(fname);
if (tail != fname) {
const char tail_save = *tail;
*tail = NUL;
if (!os_isdir(fname)) {
int ret;
char *failed_dir;
if ((ret = os_mkdir_recurse(fname, 0700, &failed_dir, NULL)) != 0) {
semsg(_(SERR "Failed to create directory %s "
"for writing ShaDa file: %s"),
failed_dir, os_strerror(ret));
xfree(fname);
xfree(failed_dir);
return FAIL;
}
}
*tail = tail_save;
}
int error;
sd_writer.cookie = file_open_new(&error, fname, kFileCreate|kFileTruncate,
0600);
if (sd_writer.cookie == NULL) {
semsg(_(SERR "System error while opening ShaDa file %s for writing: %s"),
fname, os_strerror(error));
}
}
if (sd_writer.cookie == NULL) {
xfree(fname);
xfree(tempname);
if (sd_reader.cookie != NULL) {
sd_reader.close(&sd_reader);
}
return FAIL;
}
if (p_verbose > 1) {
verbose_enter();
smsg(0, _("Writing ShaDa file \"%s\""), fname);
verbose_leave();
}
const ShaDaWriteResult sw_ret = shada_write(&sd_writer, (nomerge
? NULL
: &sd_reader));
assert(sw_ret != kSDWriteIgnError);
if (!nomerge) {
sd_reader.close(&sd_reader);
bool did_remove = false;
if (sw_ret == kSDWriteSuccessful) {
FileInfo old_info;
if (!os_fileinfo(fname, &old_info)
|| S_ISDIR(old_info.stat.st_mode)
#ifdef UNIX
// For Unix we check the owner of the file. It's not very nice
// to overwrite a user's viminfo file after a "su root", with a
// viminfo file that the user can't read.
|| (getuid() != ROOT_UID
&& !(old_info.stat.st_uid == getuid()
? (old_info.stat.st_mode & 0200)
: (old_info.stat.st_gid == getgid()
? (old_info.stat.st_mode & 0020)
: (old_info.stat.st_mode & 0002))))
#endif
) {
semsg(_("E137: ShaDa file is not writable: %s"), fname);
goto shada_write_file_did_not_remove;
}
#ifdef UNIX
if (getuid() == ROOT_UID) {
if (old_info.stat.st_uid != ROOT_UID
|| old_info.stat.st_gid != getgid()) {
const uv_uid_t old_uid = (uv_uid_t)old_info.stat.st_uid;
const uv_gid_t old_gid = (uv_gid_t)old_info.stat.st_gid;
const int fchown_ret = os_fchown(file_fd(sd_writer.cookie),
old_uid, old_gid);
if (fchown_ret != 0) {
semsg(_(RNERR "Failed setting uid and gid for file %s: %s"),
tempname, os_strerror(fchown_ret));
goto shada_write_file_did_not_remove;
}
}
}
#endif
if (vim_rename(tempname, fname) == -1) {
semsg(_(RNERR "Can't rename ShaDa file from %s to %s!"),
tempname, fname);
} else {
did_remove = true;
os_remove(tempname);
}
} else {
if (sw_ret == kSDWriteReadNotShada) {
semsg(_(RNERR "Did not rename %s because %s "
"does not look like a ShaDa file"), tempname, fname);
} else {
semsg(_(RNERR "Did not rename %s to %s because there were errors "
"during writing it"), tempname, fname);
}
}
if (!did_remove) {
shada_write_file_did_not_remove:
semsg(_(RNERR "Do not forget to remove %s or rename it manually to %s."),
tempname, fname);
}
xfree(tempname);
}
sd_writer.close(&sd_writer);
xfree(fname);
return OK;
}
/// Read marks information from ShaDa file
///
/// @return OK in case of success, FAIL otherwise.
int shada_read_marks(void)
{
return shada_read_file(NULL, kShaDaWantMarks);
}
/// Read all information from ShaDa file
///
/// @param[in] fname File to write to. If it is NULL or empty then default
/// @param[in] forceit If true, use forced reading (prioritize file contents
/// over current Neovim state).
/// @param[in] missing_ok If true, do not error out when file is missing.
///
/// @return OK in case of success, FAIL otherwise.
int shada_read_everything(const char *const fname, const bool forceit, const bool missing_ok)
{
return shada_read_file(fname,
kShaDaWantInfo|kShaDaWantMarks|kShaDaGetOldfiles
|(forceit ? kShaDaForceit : 0)
|(missing_ok ? 0 : kShaDaMissingError));
}
static void shada_free_shada_entry(ShadaEntry *const entry)
{
if (entry == NULL) {
return;
}
switch (entry->type) {
case kSDItemMissing:
break;
case kSDItemUnknown:
xfree(entry->data.unknown_item.contents);
break;
case kSDItemHeader:
api_free_dictionary(entry->data.header);
break;
case kSDItemChange:
case kSDItemJump:
case kSDItemGlobalMark:
case kSDItemLocalMark:
tv_dict_unref(entry->data.filemark.additional_data);
xfree(entry->data.filemark.fname);
break;
case kSDItemSearchPattern:
tv_dict_unref(entry->data.search_pattern.additional_data);
xfree(entry->data.search_pattern.pat);
break;
case kSDItemRegister:
tv_dict_unref(entry->data.reg.additional_data);
for (size_t i = 0; i < entry->data.reg.contents_size; i++) {
xfree(entry->data.reg.contents[i]);
}
xfree(entry->data.reg.contents);
break;
case kSDItemHistoryEntry:
tv_list_unref(entry->data.history_item.additional_elements);
xfree(entry->data.history_item.string);
break;
case kSDItemVariable:
tv_list_unref(entry->data.global_var.additional_elements);
xfree(entry->data.global_var.name);
tv_clear(&entry->data.global_var.value);
break;
case kSDItemSubString:
tv_list_unref(entry->data.sub_string.additional_elements);
xfree(entry->data.sub_string.sub);
break;
case kSDItemBufferList:
for (size_t i = 0; i < entry->data.buffer_list.size; i++) {
xfree(entry->data.buffer_list.buffers[i].fname);
tv_dict_unref(entry->data.buffer_list.buffers[i].additional_data);
}
xfree(entry->data.buffer_list.buffers);
break;
}
}
#ifndef HAVE_BE64TOH
static inline uint64_t be64toh(uint64_t big_endian_64_bits)
{
# ifdef ORDER_BIG_ENDIAN
return big_endian_64_bits;
# else
// It may appear that when !defined(ORDER_BIG_ENDIAN) actual order is big
// endian. This variant is suboptimal, but it works regardless of actual
// order.
uint8_t *buf = (uint8_t *)&big_endian_64_bits;
uint64_t ret = 0;
for (size_t i = 8; i; i--) {
ret |= ((uint64_t)buf[i - 1]) << ((8 - i) * 8);
}
return ret;
# endif
}
#endif
/// Read given number of bytes into given buffer, display error if needed
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[out] buffer Where to save the results.
/// @param[in] length How many bytes should be read.
///
/// @return kSDReadStatusSuccess if everything was OK, kSDReadStatusNotShaDa if
/// there were not enough bytes to read or kSDReadStatusReadError if
/// there was some error while reading.
static ShaDaReadResult fread_len(ShaDaReadDef *const sd_reader, char *const buffer,
const size_t length)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const ptrdiff_t read_bytes = sd_reader->read(sd_reader, buffer, length);
if (read_bytes != (ptrdiff_t)length) {
if (sd_reader->error != NULL) {
semsg(_(SERR "System error while reading ShaDa file: %s"),
sd_reader->error);
return kSDReadStatusReadError;
}
semsg(_(RCERR "Error while reading ShaDa file: "
"last entry specified that it occupies %" PRIu64 " bytes, "
"but file ended earlier"),
(uint64_t)length);
return kSDReadStatusNotShaDa;
}
return kSDReadStatusSuccess;
}
/// Read next unsigned integer from file
///
/// Errors out if the result is not an unsigned integer.
///
/// Unlike msgpack own function this one works with `FILE *` and reads *exactly*
/// as much bytes as needed, making it possible to avoid both maintaining own
/// buffer and calling `fseek`.
///
/// One byte from file stream is always consumed, even if it is not correct.
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[out] result Location where result is saved.
///
/// @return kSDReadStatusSuccess if reading was successful,
/// kSDReadStatusNotShaDa if there were not enough bytes to read or
/// kSDReadStatusReadError if reading failed for whatever reason.
static ShaDaReadResult msgpack_read_uint64(ShaDaReadDef *const sd_reader, const int first_char,
uint64_t *const result)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
const uintmax_t fpos = sd_reader->fpos - 1;
if (first_char == EOF) {
if (sd_reader->error) {
semsg(_(SERR "System error while reading integer from ShaDa file: %s"),
sd_reader->error);
return kSDReadStatusReadError;
} else if (sd_reader->eof) {
semsg(_(RCERR "Error while reading ShaDa file: "
"expected positive integer at position %" PRIu64
", but got nothing"),
(uint64_t)fpos);
return kSDReadStatusNotShaDa;
}
}
if (~first_char & 0x80) {
// Positive fixnum
*result = (uint64_t)((uint8_t)first_char);
} else {
size_t length = 0;
switch (first_char) {
case 0xCC: // uint8
length = 1;
break;
case 0xCD: // uint16
length = 2;
break;
case 0xCE: // uint32
length = 4;
break;
case 0xCF: // uint64
length = 8;
break;
default:
semsg(_(RCERR "Error while reading ShaDa file: "
"expected positive integer at position %" PRIu64),
(uint64_t)fpos);
return kSDReadStatusNotShaDa;
}
uint64_t buf = 0;
char *buf_u8 = (char *)&buf;
ShaDaReadResult fl_ret;
if ((fl_ret = fread_len(sd_reader, &(buf_u8[sizeof(buf) - length]), length))
!= kSDReadStatusSuccess) {
return fl_ret;
}
*result = be64toh(buf);
}
return kSDReadStatusSuccess;
}
#define READERR(entry_name, error_desc) \
RERR "Error while reading ShaDa file: " \
entry_name " entry at position %" PRIu64 " " \
error_desc
#define CHECK_KEY(key, \
expected) ((key).via.str.size == (sizeof(expected) - 1) \
&& strncmp((key).via.str.ptr, expected, (sizeof(expected) - 1)) == 0)
#define CLEAR_GA_AND_ERROR_OUT(ga) \
do { \
ga_clear(&(ga)); \
goto shada_read_next_item_error; \
} while (0)
#define ID(s) s
#define BINDUP(b) xmemdupz((b).ptr, (b).size)
#define TOINT(s) ((int)(s))
#define TOCHAR(s) ((char)(s))
#define TOU8(s) ((uint8_t)(s))
#define TOSIZE(s) ((size_t)(s))
#define CHECKED_ENTRY(condition, error_desc, entry_name, obj, tgt, attr, \
proc) \
do { \
if (!(condition)) { \
semsg(_(READERR(entry_name, error_desc)), initial_fpos); \
CLEAR_GA_AND_ERROR_OUT(ad_ga); \
} \
(tgt) = proc((obj).via.attr); \
} while (0)
#define CHECK_KEY_IS_STR(un, entry_name) \
if ((un).data.via.map.ptr[i].key.type != MSGPACK_OBJECT_STR) { \
semsg(_(READERR(entry_name, "has key which is not a string")), \
initial_fpos); \
CLEAR_GA_AND_ERROR_OUT(ad_ga); \
} else if ((un).data.via.map.ptr[i].key.via.str.size == 0) { \
semsg(_(READERR(entry_name, "has empty key")), initial_fpos); \
CLEAR_GA_AND_ERROR_OUT(ad_ga); \
}
#define CHECKED_KEY(un, entry_name, name, error_desc, tgt, condition, attr, proc) \
else if (CHECK_KEY((un).data.via.map.ptr[i].key, name)) /* NOLINT(readability/braces) */ \
{ \
CHECKED_ENTRY(condition, \
"has " name " key value " error_desc, \
entry_name, \
(un).data.via.map.ptr[i].val, \
tgt, \
attr, \
proc); \
}
#define TYPED_KEY(un, entry_name, name, type_name, tgt, objtype, attr, proc) \
CHECKED_KEY(un, entry_name, name, "which is not " type_name, tgt, \
(un).data.via.map.ptr[i].val.type == MSGPACK_OBJECT_##objtype, \
attr, proc)
#define BOOLEAN_KEY(un, entry_name, name, tgt) \
TYPED_KEY(un, entry_name, name, "a boolean", tgt, BOOLEAN, boolean, ID)
#define STRING_KEY(un, entry_name, name, tgt) \
TYPED_KEY(un, entry_name, name, "a binary", tgt, BIN, bin, BINDUP)
#define CONVERTED_STRING_KEY(un, entry_name, name, tgt) \
TYPED_KEY(un, entry_name, name, "a binary", tgt, BIN, bin, \
BIN_CONVERTED)
#define INT_KEY(un, entry_name, name, tgt, proc) \
CHECKED_KEY(un, entry_name, name, "which is not an integer", tgt, \
(((un).data.via.map.ptr[i].val.type \
== MSGPACK_OBJECT_POSITIVE_INTEGER) \
|| ((un).data.via.map.ptr[i].val.type \
== MSGPACK_OBJECT_NEGATIVE_INTEGER)), \
i64, proc)
#define INTEGER_KEY(un, entry_name, name, tgt) \
INT_KEY(un, entry_name, name, tgt, TOINT)
#define ADDITIONAL_KEY(un) \
else { /* NOLINT(readability/braces) */ \
ga_grow(&ad_ga, 1); \
memcpy(((char *)ad_ga.ga_data) + ((size_t)ad_ga.ga_len \
* sizeof(*(un).data.via.map.ptr)), \
(un).data.via.map.ptr + i, \
sizeof(*(un).data.via.map.ptr)); \
ad_ga.ga_len++; \
}
#define BIN_CONVERTED(b) (xmemdupz(((b).ptr), ((b).size)))
#define SET_ADDITIONAL_DATA(tgt, name) \
do { \
if (ad_ga.ga_len) { \
msgpack_object obj = { \
.type = MSGPACK_OBJECT_MAP, \
.via = { \
.map = { \
.size = (uint32_t)ad_ga.ga_len, \
.ptr = ad_ga.ga_data, \
} \
} \
}; \
typval_T adtv; \
if (msgpack_to_vim(obj, &adtv) == FAIL \
|| adtv.v_type != VAR_DICT) { \
semsg(_(READERR(name, \
"cannot be converted to a Vimscript dictionary")), \
initial_fpos); \
ga_clear(&ad_ga); \
tv_clear(&adtv); \
goto shada_read_next_item_error; \
} \
(tgt) = adtv.vval.v_dict; \
} \
ga_clear(&ad_ga); \
} while (0)
#define SET_ADDITIONAL_ELEMENTS(src, src_maxsize, tgt, name) \
do { \
if ((src).size > (size_t)(src_maxsize)) { \
msgpack_object obj = { \
.type = MSGPACK_OBJECT_ARRAY, \
.via = { \
.array = { \
.size = ((src).size - (uint32_t)(src_maxsize)), \
.ptr = (src).ptr + (src_maxsize), \
} \
} \
}; \
typval_T aetv; \
if (msgpack_to_vim(obj, &aetv) == FAIL) { \
semsg(_(READERR(name, "cannot be converted to a Vimscript list")), \
initial_fpos); \
tv_clear(&aetv); \
goto shada_read_next_item_error; \
} \
assert(aetv.v_type == VAR_LIST); \
(tgt) = aetv.vval.v_list; \
} \
} while (0)
/// Iterate over shada file contents
///
/// @param[in] sd_reader Structure containing file reader definition.
/// @param[out] entry Address where next entry contents will be saved.
/// @param[in] flags Flags, determining whether and which items should be
/// skipped (see SRNIFlags enum).
/// @param[in] max_kbyte If non-zero, skip reading entries which have length
/// greater then given.
///
/// @return Any value from ShaDaReadResult enum.
static ShaDaReadResult shada_read_next_item(ShaDaReadDef *const sd_reader, ShadaEntry *const entry,
const unsigned flags, const size_t max_kbyte)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
ShaDaReadResult ret = kSDReadStatusMalformed;
shada_read_next_item_start:
// Set entry type to kSDItemMissing and also make sure that all pointers in
// data union are NULL so they are safe to xfree(). This is needed in case
// somebody calls goto shada_read_next_item_error before anything is set in
// the switch.
CLEAR_POINTER(entry);
if (sd_reader->eof) {
return kSDReadStatusFinished;
}
// First: manually unpack type, timestamp and length.
// This is needed to avoid both seeking and having to maintain a buffer.
uint64_t type_u64 = (uint64_t)kSDItemMissing;
uint64_t timestamp_u64;
uint64_t length_u64;
const uint64_t initial_fpos = (uint64_t)sd_reader->fpos;
const int first_char = read_char(sd_reader);
if (first_char == EOF && sd_reader->eof) {
return kSDReadStatusFinished;
}
ShaDaReadResult mru_ret;
if (((mru_ret = msgpack_read_uint64(sd_reader, first_char, &type_u64))
!= kSDReadStatusSuccess)
|| ((mru_ret = msgpack_read_uint64(sd_reader, read_char(sd_reader),
×tamp_u64))
!= kSDReadStatusSuccess)
|| ((mru_ret = msgpack_read_uint64(sd_reader, read_char(sd_reader),
&length_u64))
!= kSDReadStatusSuccess)) {
return mru_ret;
}
if (length_u64 > PTRDIFF_MAX) {
semsg(_(RCERR "Error while reading ShaDa file: "
"there is an item at position %" PRIu64 " "
"that is stated to be too long"),
initial_fpos);
return kSDReadStatusNotShaDa;
}
const size_t length = (size_t)length_u64;
entry->timestamp = (Timestamp)timestamp_u64;
if (type_u64 == 0) {
// kSDItemUnknown cannot possibly pass that far because it is -1 and that
// will fail in msgpack_read_uint64. But kSDItemMissing may and it will
// otherwise be skipped because (1 << 0) will never appear in flags.
semsg(_(RCERR "Error while reading ShaDa file: "
"there is an item at position %" PRIu64 " "
"that must not be there: Missing items are "
"for internal uses only"),
initial_fpos);
return kSDReadStatusNotShaDa;
}
if ((type_u64 > SHADA_LAST_ENTRY
? !(flags & kSDReadUnknown)
: !((unsigned)(1 << type_u64) & flags))
|| (max_kbyte && length > max_kbyte * 1024)) {
// First entry is unknown or equal to "\n" (10)? Most likely this means that
// current file is not a ShaDa file because first item should normally be
// a header (excluding tests where first item is tested item). Check this by
// parsing entry contents: in non-ShaDa files this will most likely result
// in incomplete MessagePack string.
if (initial_fpos == 0
&& (type_u64 == '\n' || type_u64 > SHADA_LAST_ENTRY)) {
const ShaDaReadResult spm_ret = shada_parse_msgpack(sd_reader, length,
NULL, NULL);
if (spm_ret != kSDReadStatusSuccess) {
return spm_ret;
}
} else {
const ShaDaReadResult srs_ret = sd_reader_skip(sd_reader, length);
if (srs_ret != kSDReadStatusSuccess) {
return srs_ret;
}
}
goto shada_read_next_item_start;
}
if (type_u64 > SHADA_LAST_ENTRY) {
entry->type = kSDItemUnknown;
entry->data.unknown_item.size = length;
entry->data.unknown_item.type = type_u64;
if (initial_fpos == 0) {
const ShaDaReadResult spm_ret = shada_parse_msgpack(sd_reader, length, NULL,
&entry->data.unknown_item.contents);
if (spm_ret != kSDReadStatusSuccess) {
entry->type = kSDItemMissing;
}
return spm_ret;
}
entry->data.unknown_item.contents = xmalloc(length);
const ShaDaReadResult fl_ret =
fread_len(sd_reader, entry->data.unknown_item.contents, length);
if (fl_ret != kSDReadStatusSuccess) {
shada_free_shada_entry(entry);
entry->type = kSDItemMissing;
}
return fl_ret;
}
msgpack_unpacked unpacked;
char *buf = NULL;
const ShaDaReadResult spm_ret = shada_parse_msgpack(sd_reader, length,
&unpacked, &buf);
if (spm_ret != kSDReadStatusSuccess) {
ret = spm_ret;
goto shada_read_next_item_error;
}
entry->data = sd_default_values[type_u64].data;
switch ((ShadaEntryType)type_u64) {
case kSDItemHeader:
if (!msgpack_rpc_to_dictionary(&(unpacked.data), &(entry->data.header))) {
semsg(_(READERR("header", "is not a dictionary")), initial_fpos);
goto shada_read_next_item_error;
}
break;
case kSDItemSearchPattern: {
if (unpacked.data.type != MSGPACK_OBJECT_MAP) {
semsg(_(READERR("search pattern", "is not a dictionary")),
initial_fpos);
goto shada_read_next_item_error;
}
garray_T ad_ga;
ga_init(&ad_ga, sizeof(*(unpacked.data.via.map.ptr)), 1);
for (size_t i = 0; i < unpacked.data.via.map.size; i++) {
CHECK_KEY_IS_STR(unpacked, "search pattern")
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_MAGIC,
entry->data.search_pattern.magic)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_SMARTCASE,
entry->data.search_pattern.smartcase)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_HAS_LINE_OFFSET,
entry->data.search_pattern.has_line_offset)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_PLACE_CURSOR_AT_END,
entry->data.search_pattern.place_cursor_at_end)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_IS_LAST_USED,
entry->data.search_pattern.is_last_used)
BOOLEAN_KEY(unpacked, "search pattern",
SEARCH_KEY_IS_SUBSTITUTE_PATTERN,
entry->data.search_pattern.is_substitute_pattern)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_HIGHLIGHTED,
entry->data.search_pattern.highlighted)
BOOLEAN_KEY(unpacked, "search pattern", SEARCH_KEY_BACKWARD,
entry->data.search_pattern.search_backward)
INTEGER_KEY(unpacked, "search pattern", SEARCH_KEY_OFFSET,
entry->data.search_pattern.offset)
CONVERTED_STRING_KEY(unpacked, "search pattern", SEARCH_KEY_PAT,
entry->data.search_pattern.pat)
ADDITIONAL_KEY(unpacked)
}
if (entry->data.search_pattern.pat == NULL) {
semsg(_(READERR("search pattern", "has no pattern")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
SET_ADDITIONAL_DATA(entry->data.search_pattern.additional_data,
"search pattern");
break;
}
case kSDItemChange:
case kSDItemJump:
case kSDItemGlobalMark:
case kSDItemLocalMark: {
if (unpacked.data.type != MSGPACK_OBJECT_MAP) {
semsg(_(READERR("mark", "is not a dictionary")), initial_fpos);
goto shada_read_next_item_error;
}
garray_T ad_ga;
ga_init(&ad_ga, sizeof(*(unpacked.data.via.map.ptr)), 1);
for (size_t i = 0; i < unpacked.data.via.map.size; i++) {
CHECK_KEY_IS_STR(unpacked, "mark")
if (CHECK_KEY(unpacked.data.via.map.ptr[i].key, KEY_NAME_CHAR)) {
if (type_u64 == kSDItemJump || type_u64 == kSDItemChange) {
semsg(_(READERR("mark", "has n key which is only valid for "
"local and global mark entries")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
CHECKED_ENTRY((unpacked.data.via.map.ptr[i].val.type
== MSGPACK_OBJECT_POSITIVE_INTEGER),
"has n key value which is not an unsigned integer",
"mark", unpacked.data.via.map.ptr[i].val,
entry->data.filemark.name, u64, TOCHAR);
}
INTEGER_KEY(unpacked, "mark", KEY_LNUM, entry->data.filemark.mark.lnum)
INTEGER_KEY(unpacked, "mark", KEY_COL, entry->data.filemark.mark.col)
STRING_KEY(unpacked, "mark", KEY_FILE, entry->data.filemark.fname)
ADDITIONAL_KEY(unpacked)
}
if (entry->data.filemark.fname == NULL) {
semsg(_(READERR("mark", "is missing file name")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
if (entry->data.filemark.mark.lnum <= 0) {
semsg(_(READERR("mark", "has invalid line number")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
if (entry->data.filemark.mark.col < 0) {
semsg(_(READERR("mark", "has invalid column number")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
SET_ADDITIONAL_DATA(entry->data.filemark.additional_data, "mark");
break;
}
case kSDItemRegister: {
if (unpacked.data.type != MSGPACK_OBJECT_MAP) {
semsg(_(READERR("register", "is not a dictionary")), initial_fpos);
goto shada_read_next_item_error;
}
garray_T ad_ga;
ga_init(&ad_ga, sizeof(*(unpacked.data.via.map.ptr)), 1);
for (size_t i = 0; i < unpacked.data.via.map.size; i++) {
CHECK_KEY_IS_STR(unpacked, "register")
if (CHECK_KEY(unpacked.data.via.map.ptr[i].key,
REG_KEY_CONTENTS)) {
if (unpacked.data.via.map.ptr[i].val.type != MSGPACK_OBJECT_ARRAY) {
semsg(_(READERR("register",
"has " REG_KEY_CONTENTS
" key with non-array value")),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
if (unpacked.data.via.map.ptr[i].val.via.array.size == 0) {
semsg(_(READERR("register",
"has " REG_KEY_CONTENTS " key with empty array")),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
const msgpack_object_array arr =
unpacked.data.via.map.ptr[i].val.via.array;
for (size_t j = 0; j < arr.size; j++) {
if (arr.ptr[j].type != MSGPACK_OBJECT_BIN) {
semsg(_(READERR("register", "has " REG_KEY_CONTENTS " array "
"with non-binary value")), initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
}
entry->data.reg.contents_size = arr.size;
entry->data.reg.contents = xmalloc(arr.size * sizeof(char *));
for (size_t j = 0; j < arr.size; j++) {
entry->data.reg.contents[j] = BIN_CONVERTED(arr.ptr[j].via.bin);
}
}
BOOLEAN_KEY(unpacked, "register", REG_KEY_UNNAMED,
entry->data.reg.is_unnamed)
TYPED_KEY(unpacked, "register", REG_KEY_TYPE, "an unsigned integer",
entry->data.reg.type, POSITIVE_INTEGER, u64, TOU8)
TYPED_KEY(unpacked, "register", KEY_NAME_CHAR, "an unsigned integer",
entry->data.reg.name, POSITIVE_INTEGER, u64, TOCHAR)
TYPED_KEY(unpacked, "register", REG_KEY_WIDTH, "an unsigned integer",
entry->data.reg.width, POSITIVE_INTEGER, u64, TOSIZE)
ADDITIONAL_KEY(unpacked)
}
if (entry->data.reg.contents == NULL) {
semsg(_(READERR("register", "has missing " REG_KEY_CONTENTS " array")),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
SET_ADDITIONAL_DATA(entry->data.reg.additional_data, "register");
break;
}
case kSDItemHistoryEntry: {
if (unpacked.data.type != MSGPACK_OBJECT_ARRAY) {
semsg(_(READERR("history", "is not an array")), initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.size < 2) {
semsg(_(READERR("history", "does not have enough elements")),
initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.ptr[0].type
!= MSGPACK_OBJECT_POSITIVE_INTEGER) {
semsg(_(READERR("history", "has wrong history type type")),
initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.ptr[1].type
!= MSGPACK_OBJECT_BIN) {
semsg(_(READERR("history", "has wrong history string type")),
initial_fpos);
goto shada_read_next_item_error;
}
if (memchr(unpacked.data.via.array.ptr[1].via.bin.ptr, 0,
unpacked.data.via.array.ptr[1].via.bin.size) != NULL) {
semsg(_(READERR("history", "contains string with zero byte inside")),
initial_fpos);
goto shada_read_next_item_error;
}
entry->data.history_item.histtype =
(uint8_t)unpacked.data.via.array.ptr[0].via.u64;
const bool is_hist_search =
entry->data.history_item.histtype == HIST_SEARCH;
if (is_hist_search) {
if (unpacked.data.via.array.size < 3) {
semsg(_(READERR("search history",
"does not have separator character")), initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.ptr[2].type
!= MSGPACK_OBJECT_POSITIVE_INTEGER) {
semsg(_(READERR("search history",
"has wrong history separator type")), initial_fpos);
goto shada_read_next_item_error;
}
entry->data.history_item.sep =
(char)unpacked.data.via.array.ptr[2].via.u64;
}
size_t strsize;
strsize = (
unpacked.data.via.array.ptr[1].via.bin.size
+ 1 // Zero byte
+ 1); // Separator character
entry->data.history_item.string = xmalloc(strsize);
memcpy(entry->data.history_item.string,
unpacked.data.via.array.ptr[1].via.bin.ptr,
unpacked.data.via.array.ptr[1].via.bin.size);
entry->data.history_item.string[strsize - 2] = 0;
entry->data.history_item.string[strsize - 1] =
entry->data.history_item.sep;
SET_ADDITIONAL_ELEMENTS(unpacked.data.via.array, (2 + is_hist_search),
entry->data.history_item.additional_elements,
"history");
break;
}
case kSDItemVariable: {
if (unpacked.data.type != MSGPACK_OBJECT_ARRAY) {
semsg(_(READERR("variable", "is not an array")), initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.size < 2) {
semsg(_(READERR("variable", "does not have enough elements")),
initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.ptr[0].type != MSGPACK_OBJECT_BIN) {
semsg(_(READERR("variable", "has wrong variable name type")),
initial_fpos);
goto shada_read_next_item_error;
}
entry->data.global_var.name =
xmemdupz(unpacked.data.via.array.ptr[0].via.bin.ptr,
unpacked.data.via.array.ptr[0].via.bin.size);
SET_ADDITIONAL_ELEMENTS(unpacked.data.via.array, 2,
entry->data.global_var.additional_elements,
"variable");
bool is_blob = false;
// A msgpack BIN could be a String or Blob; an additional VAR_TYPE_BLOB
// element is stored with Blobs which can be used to differentiate them
if (unpacked.data.via.array.ptr[1].type == MSGPACK_OBJECT_BIN) {
const listitem_T *type_item
= tv_list_first(entry->data.global_var.additional_elements);
if (type_item != NULL) {
const typval_T *type_tv = TV_LIST_ITEM_TV(type_item);
if (type_tv->v_type != VAR_NUMBER
|| type_tv->vval.v_number != VAR_TYPE_BLOB) {
semsg(_(READERR("variable", "has wrong variable type")),
initial_fpos);
goto shada_read_next_item_error;
}
is_blob = true;
}
}
if (is_blob) {
const msgpack_object_bin *const bin
= &unpacked.data.via.array.ptr[1].via.bin;
blob_T *const blob = tv_blob_alloc();
ga_concat_len(&blob->bv_ga, bin->ptr, (size_t)bin->size);
tv_blob_set_ret(&entry->data.global_var.value, blob);
} else if (msgpack_to_vim(unpacked.data.via.array.ptr[1],
&(entry->data.global_var.value)) == FAIL) {
semsg(_(READERR("variable", "has value that cannot "
"be converted to the Vimscript value")), initial_fpos);
goto shada_read_next_item_error;
}
break;
}
case kSDItemSubString:
if (unpacked.data.type != MSGPACK_OBJECT_ARRAY) {
semsg(_(READERR("sub string", "is not an array")), initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.size < 1) {
semsg(_(READERR("sub string", "does not have enough elements")),
initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.ptr[0].type != MSGPACK_OBJECT_BIN) {
semsg(_(READERR("sub string", "has wrong sub string type")),
initial_fpos);
goto shada_read_next_item_error;
}
entry->data.sub_string.sub =
BIN_CONVERTED(unpacked.data.via.array.ptr[0].via.bin);
SET_ADDITIONAL_ELEMENTS(unpacked.data.via.array, 1,
entry->data.sub_string.additional_elements,
"sub string");
break;
case kSDItemBufferList:
if (unpacked.data.type != MSGPACK_OBJECT_ARRAY) {
semsg(_(READERR("buffer list", "is not an array")), initial_fpos);
goto shada_read_next_item_error;
}
if (unpacked.data.via.array.size == 0) {
break;
}
entry->data.buffer_list.buffers =
xcalloc(unpacked.data.via.array.size,
sizeof(*entry->data.buffer_list.buffers));
for (size_t i = 0; i < unpacked.data.via.array.size; i++) {
entry->data.buffer_list.size++;
msgpack_unpacked unpacked_2 = (msgpack_unpacked) {
.data = unpacked.data.via.array.ptr[i],
};
{
if (unpacked_2.data.type != MSGPACK_OBJECT_MAP) {
semsg(_(RERR "Error while reading ShaDa file: "
"buffer list at position %" PRIu64 " "
"contains entry that is not a dictionary"),
initial_fpos);
goto shada_read_next_item_error;
}
entry->data.buffer_list.buffers[i].pos = default_pos;
garray_T ad_ga;
ga_init(&ad_ga, sizeof(*(unpacked_2.data.via.map.ptr)), 1);
{
// XXX: Temporarily reassign `i` because the macros depend on it.
const size_t j = i;
{
for (i = 0; i < unpacked_2.data.via.map.size; i++) {
CHECK_KEY_IS_STR(unpacked_2, "buffer list entry")
INTEGER_KEY(unpacked_2, "buffer list entry", KEY_LNUM,
entry->data.buffer_list.buffers[j].pos.lnum)
INTEGER_KEY(unpacked_2, "buffer list entry", KEY_COL,
entry->data.buffer_list.buffers[j].pos.col)
STRING_KEY(unpacked_2, "buffer list entry", KEY_FILE,
entry->data.buffer_list.buffers[j].fname)
ADDITIONAL_KEY(unpacked_2)
}
}
i = j; // XXX: Restore `i`.
}
if (entry->data.buffer_list.buffers[i].pos.lnum <= 0) {
semsg(_(RERR "Error while reading ShaDa file: "
"buffer list at position %" PRIu64 " "
"contains entry with invalid line number"),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
if (entry->data.buffer_list.buffers[i].pos.col < 0) {
semsg(_(RERR "Error while reading ShaDa file: "
"buffer list at position %" PRIu64 " "
"contains entry with invalid column number"),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
if (entry->data.buffer_list.buffers[i].fname == NULL) {
semsg(_(RERR "Error while reading ShaDa file: "
"buffer list at position %" PRIu64 " "
"contains entry that does not have a file name"),
initial_fpos);
CLEAR_GA_AND_ERROR_OUT(ad_ga);
}
SET_ADDITIONAL_DATA(entry->data.buffer_list.buffers[i].additional_data,
"buffer list entry");
}
}
break;
case kSDItemMissing:
case kSDItemUnknown:
abort();
}
entry->type = (ShadaEntryType)type_u64;
ret = kSDReadStatusSuccess;
shada_read_next_item_end:
if (buf != NULL) {
msgpack_unpacked_destroy(&unpacked);
xfree(buf);
}
return ret;
shada_read_next_item_error:
entry->type = (ShadaEntryType)type_u64;
shada_free_shada_entry(entry);
entry->type = kSDItemMissing;
goto shada_read_next_item_end;
}
#undef BIN_CONVERTED
#undef CHECK_KEY
#undef BOOLEAN_KEY
#undef CONVERTED_STRING_KEY
#undef STRING_KEY
#undef ADDITIONAL_KEY
#undef ID
#undef BINDUP
#undef TOCHAR
#undef TOINT
#undef TYPED_KEY
#undef INT_KEY
#undef INTEGER_KEY
#undef TOU8
#undef TOSIZE
#undef SET_ADDITIONAL_DATA
#undef SET_ADDITIONAL_ELEMENTS
#undef CLEAR_GA_AND_ERROR_OUT
/// Check whether "name" is on removable media (according to 'shada')
///
/// @param[in] name Checked name.
///
/// @return True if it is, false otherwise.
static bool shada_removable(const char *name)
FUNC_ATTR_WARN_UNUSED_RESULT
{
char part[MAXPATHL + 1];
bool retval = false;
char *new_name = home_replace_save(NULL, name);
for (char *p = p_shada; *p;) {
(void)copy_option_part(&p, part, ARRAY_SIZE(part), ", ");
if (part[0] == 'r') {
home_replace(NULL, part + 1, NameBuff, MAXPATHL, true);
size_t n = strlen(NameBuff);
if (mb_strnicmp(NameBuff, new_name, n) == 0) {
retval = true;
break;
}
}
}
xfree(new_name);
return retval;
}
/// Initialize ShaDa jumplist entries.
///
/// @param[in,out] jumps Array of ShaDa entries to set.
/// @param[in] removable_bufs Cache of buffers ignored due to their
/// location.
///
/// @return number of jumplist entries
static inline size_t shada_init_jumps(PossiblyFreedShadaEntry *jumps,
Set(ptr_t) *const removable_bufs)
{
// Initialize jump list
size_t jumps_size = 0;
const void *jump_iter = NULL;
setpcmark();
cleanup_jumplist(curwin, false);
do {
xfmark_T fm;
jump_iter = mark_jumplist_iter(jump_iter, curwin, &fm);
if (fm.fmark.mark.lnum == 0) {
siemsg("ShaDa: mark lnum zero (ji:%p, js:%p, len:%i)",
(void *)jump_iter, (void *)&curwin->w_jumplist[0],
curwin->w_jumplistlen);
continue;
}
const buf_T *const buf = (fm.fmark.fnum == 0
? NULL
: buflist_findnr(fm.fmark.fnum));
if (buf != NULL
? set_has(ptr_t, removable_bufs, (ptr_t)buf)
: fm.fmark.fnum != 0) {
continue;
}
const char *const fname =
(fm.fmark.fnum == 0 ? (fm.fname == NULL ? NULL : fm.fname) : buf ? buf->b_ffname : NULL);
if (fname == NULL) {
continue;
}
jumps[jumps_size++] = (PossiblyFreedShadaEntry) {
.can_free_entry = false,
.data = {
.type = kSDItemJump,
.timestamp = fm.fmark.timestamp,
.data = {
.filemark = {
.name = NUL,
.mark = fm.fmark.mark,
.fname = (char *)fname,
.additional_data = fm.fmark.additional_data,
}
}
}
};
} while (jump_iter != NULL);
return jumps_size;
}
/// Write registers ShaDa entries in given msgpack_sbuffer.
///
/// @param[in] sbuf target msgpack_sbuffer to write to.
void shada_encode_regs(msgpack_sbuffer *const sbuf)
FUNC_ATTR_NONNULL_ALL
{
WriteMergerState *const wms = xcalloc(1, sizeof(*wms));
shada_initialize_registers(wms, -1);
msgpack_packer packer;
msgpack_packer_init(&packer, sbuf, msgpack_sbuffer_write);
for (size_t i = 0; i < ARRAY_SIZE(wms->registers); i++) {
if (wms->registers[i].data.type == kSDItemRegister) {
if (kSDWriteFailed
== shada_pack_pfreed_entry(&packer, wms->registers[i], 0)) {
abort();
}
}
}
xfree(wms);
}
/// Write jumplist ShaDa entries in given msgpack_sbuffer.
///
/// @param[in] sbuf target msgpack_sbuffer to write to.
void shada_encode_jumps(msgpack_sbuffer *const sbuf)
FUNC_ATTR_NONNULL_ALL
{
Set(ptr_t) removable_bufs = SET_INIT;
find_removable_bufs(&removable_bufs);
PossiblyFreedShadaEntry jumps[JUMPLISTSIZE];
size_t jumps_size = shada_init_jumps(jumps, &removable_bufs);
msgpack_packer packer;
msgpack_packer_init(&packer, sbuf, msgpack_sbuffer_write);
for (size_t i = 0; i < jumps_size; i++) {
if (kSDWriteFailed == shada_pack_pfreed_entry(&packer, jumps[i], 0)) {
abort();
}
}
}
/// Write buffer list ShaDa entry in given msgpack_sbuffer.
///
/// @param[in] sbuf target msgpack_sbuffer to write to.
void shada_encode_buflist(msgpack_sbuffer *const sbuf)
FUNC_ATTR_NONNULL_ALL
{
Set(ptr_t) removable_bufs = SET_INIT;
find_removable_bufs(&removable_bufs);
ShadaEntry buflist_entry = shada_get_buflist(&removable_bufs);
msgpack_packer packer;
msgpack_packer_init(&packer, sbuf, msgpack_sbuffer_write);
if (kSDWriteFailed == shada_pack_entry(&packer, buflist_entry, 0)) {
abort();
}
xfree(buflist_entry.data.buffer_list.buffers);
}
/// Write global variables ShaDa entries in given msgpack_sbuffer.
///
/// @param[in] sbuf target msgpack_sbuffer to write to.
void shada_encode_gvars(msgpack_sbuffer *const sbuf)
FUNC_ATTR_NONNULL_ALL
{
msgpack_packer packer;
msgpack_packer_init(&packer, sbuf, msgpack_sbuffer_write);
const void *var_iter = NULL;
const Timestamp cur_timestamp = os_time();
do {
typval_T vartv;
const char *name = NULL;
var_iter = var_shada_iter(var_iter, &name, &vartv,
VAR_FLAVOUR_DEFAULT | VAR_FLAVOUR_SESSION | VAR_FLAVOUR_SHADA);
if (name == NULL) {
break;
}
if (vartv.v_type != VAR_FUNC && vartv.v_type != VAR_PARTIAL) {
typval_T tgttv;
tv_copy(&vartv, &tgttv);
ShaDaWriteResult r = shada_pack_entry(&packer, (ShadaEntry) {
.type = kSDItemVariable,
.timestamp = cur_timestamp,
.data = {
.global_var = {
.name = (char *)name,
.value = tgttv,
.additional_elements = NULL,
}
}
}, 0);
if (kSDWriteFailed == r) {
abort();
}
tv_clear(&tgttv);
}
tv_clear(&vartv);
} while (var_iter != NULL);
}
/// Wrapper for reading from msgpack_sbuffer.
///
/// @return number of bytes read.
static ptrdiff_t read_sbuf(ShaDaReadDef *const sd_reader, void *const dest, const size_t size)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
msgpack_sbuffer *sbuf = (msgpack_sbuffer *)sd_reader->cookie;
const uintmax_t bytes_read = MIN(size, sbuf->size - sd_reader->fpos);
if (bytes_read < size) {
sd_reader->eof = true;
}
memcpy(dest, sbuf->data + sd_reader->fpos, (size_t)bytes_read);
sd_reader->fpos += bytes_read;
return (ptrdiff_t)bytes_read;
}
/// Wrapper for read that ignores bytes read from msgpack_sbuffer.
///
/// Used for skipping.
///
/// @param[in,out] sd_reader ShaDaReadDef with msgpack_sbuffer.
/// @param[in] offset Amount of bytes to skip.
///
/// @return FAIL in case of failure, OK in case of success. May set
/// sd_reader->eof.
static int sd_sbuf_reader_skip_read(ShaDaReadDef *const sd_reader, const size_t offset)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
msgpack_sbuffer *sbuf = (msgpack_sbuffer *)sd_reader->cookie;
assert(sbuf->size >= sd_reader->fpos);
const uintmax_t skip_bytes = MIN(offset, sbuf->size - sd_reader->fpos);
if (skip_bytes < offset) {
sd_reader->eof = true;
return FAIL;
}
sd_reader->fpos += offset;
return OK;
}
/// Prepare ShaDaReadDef with msgpack_sbuffer for reading.
///
/// @param[in] sbuf msgpack_sbuffer to read from.
/// @param[out] sd_reader Location where reader structure will be saved.
static void open_shada_sbuf_for_reading(const msgpack_sbuffer *const sbuf, ShaDaReadDef *sd_reader)
FUNC_ATTR_NONNULL_ALL
{
*sd_reader = (ShaDaReadDef) {
.read = &read_sbuf,
.close = NULL,
.skip = &sd_sbuf_reader_skip_read,
.error = NULL,
.eof = false,
.fpos = 0,
.cookie = (void *)sbuf,
};
}
/// Read ShaDa from msgpack_sbuffer.
///
/// @param[in] file msgpack_sbuffer to read from.
/// @param[in] flags Flags, see ShaDaReadFileFlags enum.
void shada_read_sbuf(msgpack_sbuffer *const sbuf, const int flags)
FUNC_ATTR_NONNULL_ALL
{
assert(sbuf != NULL);
if (sbuf->data == NULL) {
return;
}
ShaDaReadDef sd_reader;
open_shada_sbuf_for_reading(sbuf, &sd_reader);
shada_read(&sd_reader, flags);
}
|