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
|
/* $OpenBSD: rcs.c,v 1.278 2008/06/26 21:31:40 joris Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "atomicio.h"
#include "cvs.h"
#include "diff.h"
#include "rcs.h"
#define RCS_BUFSIZE 16384
#define RCS_BUFEXTSIZE 8192
#define RCS_KWEXP_SIZE 1024
/* RCS token types */
#define RCS_TOK_ERR -1
#define RCS_TOK_EOF 0
#define RCS_TOK_NUM 1
#define RCS_TOK_ID 2
#define RCS_TOK_STRING 3
#define RCS_TOK_SCOLON 4
#define RCS_TOK_COLON 5
#define RCS_TOK_HEAD 8
#define RCS_TOK_BRANCH 9
#define RCS_TOK_ACCESS 10
#define RCS_TOK_SYMBOLS 11
#define RCS_TOK_LOCKS 12
#define RCS_TOK_COMMENT 13
#define RCS_TOK_EXPAND 14
#define RCS_TOK_DATE 15
#define RCS_TOK_AUTHOR 16
#define RCS_TOK_STATE 17
#define RCS_TOK_NEXT 18
#define RCS_TOK_BRANCHES 19
#define RCS_TOK_DESC 20
#define RCS_TOK_LOG 21
#define RCS_TOK_TEXT 22
#define RCS_TOK_STRICT 23
#define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
#define RCS_NOSCOL 0x01 /* no terminating semi-colon */
#define RCS_VOPT 0x02 /* value is optional */
#define ANNOTATE_NEVER 0
#define ANNOTATE_NOW 1
#define ANNOTATE_LATER 2
/* opaque parse data */
struct rcs_pdata {
u_int rp_lines;
char *rp_buf;
size_t rp_blen;
char *rp_bufend;
size_t rp_tlen;
/* pushback token buffer */
char rp_ptok[128];
int rp_pttype; /* token type, RCS_TOK_ERR if no token */
FILE *rp_file;
};
#define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
#define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
/* invalid characters in RCS symbol names */
static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
/* comment leaders, depending on the file's suffix */
static const struct rcs_comment {
const char *rc_suffix;
const char *rc_cstr;
} rcs_comments[] = {
{ "1", ".\\\" " },
{ "2", ".\\\" " },
{ "3", ".\\\" " },
{ "4", ".\\\" " },
{ "5", ".\\\" " },
{ "6", ".\\\" " },
{ "7", ".\\\" " },
{ "8", ".\\\" " },
{ "9", ".\\\" " },
{ "a", "-- " }, /* Ada */
{ "ada", "-- " },
{ "adb", "-- " },
{ "asm", ";; " }, /* assembler (MS-DOS) */
{ "ads", "-- " }, /* Ada */
{ "bat", ":: " }, /* batch (MS-DOS) */
{ "body", "-- " }, /* Ada */
{ "c", " * " }, /* C */
{ "c++", "// " }, /* C++ */
{ "cc", "// " },
{ "cpp", "// " },
{ "cxx", "// " },
{ "m", "// " }, /* Objective-C */
{ "cl", ";;; " }, /* Common Lisp */
{ "cmd", ":: " }, /* command (OS/2) */
{ "cmf", "c " }, /* CM Fortran */
{ "csh", "# " }, /* shell */
{ "e", "# " }, /* efl */
{ "epsf", "% " }, /* encapsulated postscript */
{ "epsi", "% " }, /* encapsulated postscript */
{ "el", "; " }, /* Emacs Lisp */
{ "f", "c " }, /* Fortran */
{ "for", "c " },
{ "h", " * " }, /* C-header */
{ "hh", "// " }, /* C++ header */
{ "hpp", "// " },
{ "hxx", "// " },
{ "in", "# " }, /* for Makefile.in */
{ "l", " * " }, /* lex */
{ "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
{ "mak", "# " }, /* makefile, e.g. Visual C++ */
{ "me", ".\\\" " }, /* me-macros t/nroff */
{ "ml", "; " }, /* mocklisp */
{ "mm", ".\\\" " }, /* mm-macros t/nroff */
{ "ms", ".\\\" " }, /* ms-macros t/nroff */
{ "man", ".\\\" " }, /* man-macros t/nroff */
{ "p", " * " }, /* pascal */
{ "pas", " * " },
{ "pl", "# " }, /* Perl (conflict with Prolog) */
{ "pm", "# " }, /* Perl module */
{ "ps", "% " }, /* postscript */
{ "psw", "% " }, /* postscript wrap */
{ "pswm", "% " }, /* postscript wrap */
{ "r", "# " }, /* ratfor */
{ "rc", " * " }, /* Microsoft Windows resource file */
{ "red", "% " }, /* psl/rlisp */
{ "sh", "# " }, /* shell */
{ "sl", "% " }, /* psl */
{ "spec", "-- " }, /* Ada */
{ "tex", "% " }, /* tex */
{ "y", " * " }, /* yacc */
{ "ye", " * " }, /* yacc-efl */
{ "yr", " * " }, /* yacc-ratfor */
};
struct rcs_kw rcs_expkw[] = {
{ "Author", RCS_KW_AUTHOR },
{ "Date", RCS_KW_DATE },
{ "Header", RCS_KW_HEADER },
{ "Id", RCS_KW_ID },
{ "Locker", RCS_KW_LOCKER },
{ "Log", RCS_KW_LOG },
{ "Name", RCS_KW_NAME },
{ "RCSfile", RCS_KW_RCSFILE },
{ "Revision", RCS_KW_REVISION },
{ "Source", RCS_KW_SOURCE },
{ "State", RCS_KW_STATE },
{ "Mdocdate", RCS_KW_MDOCDATE },
};
#define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
static struct rcs_key {
char rk_str[16];
int rk_id;
int rk_val;
int rk_flags;
} rcs_keys[] = {
{ "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
{ "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
{ "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
{ "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
{ "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
{ "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
{ "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
{ "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
{ "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
{ "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
{ "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
{ "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
{ "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
{ "strict", RCS_TOK_STRICT, 0, 0, },
{ "symbols", RCS_TOK_SYMBOLS, 0, 0 },
{ "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
};
#define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
static RCSNUM *rcs_get_revision(const char *, RCSFILE *);
int rcs_patch_lines(struct cvs_lines *, struct cvs_lines *,
struct cvs_line **, struct rcs_delta *);
static void rcs_parse_init(RCSFILE *);
static int rcs_parse_admin(RCSFILE *);
static int rcs_parse_delta(RCSFILE *);
static void rcs_parse_deltas(RCSFILE *, RCSNUM *);
static int rcs_parse_deltatext(RCSFILE *);
static void rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
static void rcs_parse_desc(RCSFILE *, RCSNUM *);
static int rcs_parse_access(RCSFILE *);
static int rcs_parse_symbols(RCSFILE *);
static int rcs_parse_locks(RCSFILE *);
static int rcs_parse_branches(RCSFILE *, struct rcs_delta *);
static void rcs_freedelta(struct rcs_delta *);
static void rcs_freepdata(struct rcs_pdata *);
static int rcs_gettok(RCSFILE *);
static int rcs_pushtok(RCSFILE *, const char *, int);
static void rcs_growbuf(RCSFILE *);
static void rcs_strprint(const u_char *, size_t, FILE *);
static void rcs_kwexp_line(char *, struct rcs_delta *, struct cvs_lines *,
struct cvs_line *, int mode);
static int rcs_ignore_keys = 0;
RCSFILE *
rcs_open(const char *path, int fd, int flags, ...)
{
int mode;
mode_t fmode;
RCSFILE *rfp;
va_list vap;
struct stat st;
struct rcs_delta *rdp;
struct rcs_lock *lkr;
fmode = S_IRUSR|S_IRGRP|S_IROTH;
flags &= 0xffff; /* ditch any internal flags */
if (flags & RCS_CREATE) {
va_start(vap, flags);
mode = va_arg(vap, int);
va_end(vap);
fmode = (mode_t)mode;
} else {
if (fstat(fd, &st) == -1)
fatal("rcs_open: %s: fstat: %s", path, strerror(errno));
fmode = st.st_mode;
}
fmode &= ~cvs_umask;
rfp = xcalloc(1, sizeof(*rfp));
rfp->rf_path = xstrdup(path);
rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
rfp->rf_mode = fmode;
rfp->fd = fd;
rfp->rf_dead = 0;
TAILQ_INIT(&(rfp->rf_delta));
TAILQ_INIT(&(rfp->rf_access));
TAILQ_INIT(&(rfp->rf_symbols));
TAILQ_INIT(&(rfp->rf_locks));
if (!(rfp->rf_flags & RCS_CREATE))
rcs_parse_init(rfp);
/* fill in rd_locker */
TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
rcs_close(rfp);
return (NULL);
}
rdp->rd_locker = xstrdup(lkr->rl_name);
}
return (rfp);
}
/*
* rcs_close()
*
* Close an RCS file handle.
*/
void
rcs_close(RCSFILE *rfp)
{
struct rcs_delta *rdp;
struct rcs_access *rap;
struct rcs_lock *rlp;
struct rcs_sym *rsp;
if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
rcs_write(rfp);
while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
rdp = TAILQ_FIRST(&(rfp->rf_delta));
TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
rcs_freedelta(rdp);
}
while (!TAILQ_EMPTY(&(rfp->rf_access))) {
rap = TAILQ_FIRST(&(rfp->rf_access));
TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
xfree(rap->ra_name);
xfree(rap);
}
while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
rsp = TAILQ_FIRST(&(rfp->rf_symbols));
TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
rcsnum_free(rsp->rs_num);
xfree(rsp->rs_name);
xfree(rsp);
}
while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
rlp = TAILQ_FIRST(&(rfp->rf_locks));
TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
rcsnum_free(rlp->rl_num);
xfree(rlp->rl_name);
xfree(rlp);
}
if (rfp->rf_head != NULL)
rcsnum_free(rfp->rf_head);
if (rfp->rf_branch != NULL)
rcsnum_free(rfp->rf_branch);
if (rfp->rf_path != NULL)
xfree(rfp->rf_path);
if (rfp->rf_comment != NULL)
xfree(rfp->rf_comment);
if (rfp->rf_expand != NULL)
xfree(rfp->rf_expand);
if (rfp->rf_desc != NULL)
xfree(rfp->rf_desc);
if (rfp->rf_pdata != NULL)
rcs_freepdata(rfp->rf_pdata);
xfree(rfp);
}
/*
* rcs_write()
*
* Write the contents of the RCS file handle <rfp> to disk in the file whose
* path is in <rf_path>.
*/
void
rcs_write(RCSFILE *rfp)
{
FILE *fp;
char buf[1024], numbuf[CVS_REV_BUFSZ], *fn, tmpdir[MAXPATHLEN];
struct rcs_access *ap;
struct rcs_sym *symp;
struct rcs_branch *brp;
struct rcs_delta *rdp;
struct rcs_lock *lkp;
size_t len;
int fd, saved_errno;
fd = -1;
if (rfp->rf_flags & RCS_SYNCED)
return;
if (cvs_noexec == 1)
return;
/* Write operations need the whole file parsed */
rcs_parse_deltatexts(rfp, NULL);
if (strlcpy(tmpdir, rfp->rf_path, sizeof(tmpdir)) >= sizeof(tmpdir))
fatal("rcs_write: truncation");
(void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", dirname(tmpdir));
if ((fd = mkstemp(fn)) == -1)
fatal("%s", fn);
if ((fp = fdopen(fd, "w")) == NULL) {
saved_errno = errno;
(void)unlink(fn);
fatal("fdopen %s: %s", fn, strerror(saved_errno));
}
if (rfp->rf_head != NULL)
rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
else
numbuf[0] = '\0';
fprintf(fp, "head\t%s;\n", numbuf);
if (rfp->rf_branch != NULL) {
rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
fprintf(fp, "branch\t%s;\n", numbuf);
}
fputs("access", fp);
TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
fprintf(fp, "\n\t%s", ap->ra_name);
}
fputs(";\n", fp);
fprintf(fp, "symbols");
TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
if (RCSNUM_ISBRANCH(symp->rs_num))
rcsnum_addmagic(symp->rs_num);
rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
fatal("rcs_write: string overflow");
fprintf(fp, "\n\t%s", buf);
}
fprintf(fp, ";\n");
fprintf(fp, "locks");
TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
}
fprintf(fp, ";");
if (rfp->rf_flags & RCS_SLOCK)
fprintf(fp, " strict;");
fputc('\n', fp);
fputs("comment\t@", fp);
if (rfp->rf_comment != NULL) {
rcs_strprint((const u_char *)rfp->rf_comment,
strlen(rfp->rf_comment), fp);
fputs("@;\n", fp);
} else
fputs("# @;\n", fp);
if (rfp->rf_expand != NULL) {
fputs("expand @", fp);
rcs_strprint((const u_char *)rfp->rf_expand,
strlen(rfp->rf_expand), fp);
fputs("@;\n", fp);
}
fputs("\n\n", fp);
TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
sizeof(numbuf)));
fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
fprintf(fp, "\tauthor %s;\tstate %s;\n",
rdp->rd_author, rdp->rd_state);
fputs("branches", fp);
TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
fprintf(fp, "\n\t%s", rcsnum_tostr(brp->rb_num, numbuf,
sizeof(numbuf)));
}
fputs(";\n", fp);
fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
numbuf, sizeof(numbuf)));
}
fputs("\ndesc\n@", fp);
if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
if (rfp->rf_desc[len-1] != '\n')
fputc('\n', fp);
}
fputs("@\n", fp);
/* deltatexts */
TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
sizeof(numbuf)));
fputs("log\n@", fp);
if (rdp->rd_log != NULL) {
len = strlen(rdp->rd_log);
rcs_strprint((const u_char *)rdp->rd_log, len, fp);
if (rdp->rd_log[len-1] != '\n')
fputc('\n', fp);
}
fputs("@\ntext\n@", fp);
if (rdp->rd_text != NULL) {
rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
}
fputs("@\n", fp);
}
if (fchmod(fd, rfp->rf_mode) == -1) {
saved_errno = errno;
(void)unlink(fn);
fatal("fchmod %s: %s", fn, strerror(saved_errno));
}
(void)fclose(fp);
if (rename(fn, rfp->rf_path) == -1) {
saved_errno = errno;
(void)unlink(fn);
fatal("rename(%s, %s): %s", fn, rfp->rf_path,
strerror(saved_errno));
}
rfp->rf_flags |= RCS_SYNCED;
if (fn != NULL)
xfree(fn);
}
/*
* rcs_head_get()
*
* Retrieve the revision number of the head revision for the RCS file <file>.
*/
RCSNUM *
rcs_head_get(RCSFILE *file)
{
struct rcs_branch *brp;
struct rcs_delta *rdp;
RCSNUM *rev, *rootrev;
if (file->rf_head == NULL)
return NULL;
rev = rcsnum_alloc();
if (file->rf_branch != NULL) {
/* we have a default branch, use that to calculate the
* real HEAD*/
rootrev = rcsnum_alloc();
rcsnum_cpy(file->rf_branch, rootrev, 2);
if ((rdp = rcs_findrev(file, rootrev)) == NULL)
fatal("rcs_head_get: could not find root revision");
/* HEAD should be the last revision on the default branch */
TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
if (TAILQ_NEXT(brp, rb_list) == NULL)
break;
}
rcsnum_free(rootrev);
if ((rdp = rcs_findrev(file, brp->rb_num)) == NULL)
fatal("rcs_head_get: could not find branch revision");
while (rdp->rd_next->rn_len != 0)
if ((rdp = rcs_findrev(file, rdp->rd_next)) == NULL)
fatal("rcs_head_get: could not find "
"next branch revision");
rcsnum_cpy(rdp->rd_num, rev, 0);
} else {
rcsnum_cpy(file->rf_head, rev, 0);
}
return (rev);
}
/*
* rcs_head_set()
*
* Set the revision number of the head revision for the RCS file <file> to
* <rev>, which must reference a valid revision within the file.
*/
int
rcs_head_set(RCSFILE *file, RCSNUM *rev)
{
if (rcs_findrev(file, rev) == NULL)
return (-1);
if (file->rf_head == NULL)
file->rf_head = rcsnum_alloc();
rcsnum_cpy(rev, file->rf_head, 0);
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_branch_new()
*
* Create a new branch out of supplied revision for the RCS file <file>.
*/
RCSNUM *
rcs_branch_new(RCSFILE *file, RCSNUM *rev)
{
RCSNUM *brev;
struct rcs_sym *sym;
if ((brev = rcsnum_new_branch(rev)) == NULL)
return (NULL);
for (;;) {
TAILQ_FOREACH(sym, &(file->rf_symbols), rs_list)
if (!rcsnum_cmp(sym->rs_num, brev, 0))
break;
if (sym != NULL) {
if (rcsnum_inc(brev) == NULL ||
rcsnum_inc(brev) == NULL)
return (NULL);
} else
break;
}
return (brev);
}
/*
* rcs_branch_get()
*
* Retrieve the default branch number for the RCS file <file>.
* Returns the number on success. If NULL is returned, then there is no
* default branch for this file.
*/
const RCSNUM *
rcs_branch_get(RCSFILE *file)
{
return (file->rf_branch);
}
/*
* rcs_branch_set()
*
* Set the default branch for the RCS file <file> to <bnum>.
* Returns 0 on success, -1 on failure.
*/
int
rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
{
if (file->rf_branch == NULL)
file->rf_branch = rcsnum_alloc();
rcsnum_cpy(bnum, file->rf_branch, 0);
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_access_add()
*
* Add the login name <login> to the access list for the RCS file <file>.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_access_add(RCSFILE *file, const char *login)
{
struct rcs_access *ap;
/* first look for duplication */
TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
if (strcmp(ap->ra_name, login) == 0)
return (-1);
}
ap = xmalloc(sizeof(*ap));
ap->ra_name = xstrdup(login);
TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_access_remove()
*
* Remove an entry with login name <login> from the access list of the RCS
* file <file>.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_access_remove(RCSFILE *file, const char *login)
{
struct rcs_access *ap;
TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
if (strcmp(ap->ra_name, login) == 0)
break;
if (ap == NULL)
return (-1);
TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
xfree(ap->ra_name);
xfree(ap);
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_sym_add()
*
* Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
* is named <sym> and is bound to the RCS revision <snum>.
*/
int
rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
{
struct rcs_sym *symp;
if (!rcs_sym_check(sym))
return (-1);
/* first look for duplication */
TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
if (strcmp(symp->rs_name, sym) == 0)
return (1);
}
symp = xmalloc(sizeof(*symp));
symp->rs_name = xstrdup(sym);
symp->rs_num = rcsnum_alloc();
rcsnum_cpy(snum, symp->rs_num, 0);
TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
/* not synced anymore */
rfp->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_sym_remove()
*
* Remove the symbol with name <sym> from the symbol list for the RCS file
* <file>. If no such symbol is found, the call fails and returns with an
* error.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_sym_remove(RCSFILE *file, const char *sym)
{
struct rcs_sym *symp;
if (!rcs_sym_check(sym))
return (-1);
TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
if (strcmp(symp->rs_name, sym) == 0)
break;
if (symp == NULL)
return (-1);
TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
xfree(symp->rs_name);
rcsnum_free(symp->rs_num);
xfree(symp);
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_sym_get()
*
* Find a specific symbol <sym> entry in the tree of the RCS file <file>.
*
* Returns a pointer to the symbol on success, or NULL on failure.
*/
struct rcs_sym *
rcs_sym_get(RCSFILE *file, const char *sym)
{
struct rcs_sym *symp;
TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
if (strcmp(symp->rs_name, sym) == 0)
return (symp);
return (NULL);
}
/*
* rcs_sym_getrev()
*
* Retrieve the RCS revision number associated with the symbol <sym> for the
* RCS file <file>. The returned value is a dynamically-allocated copy and
* should be freed by the caller once they are done with it.
* Returns the RCSNUM on success, or NULL on failure.
*/
RCSNUM *
rcs_sym_getrev(RCSFILE *file, const char *sym)
{
RCSNUM *num;
struct rcs_sym *symp;
if (!rcs_sym_check(sym) || file->rf_head == NULL)
return (NULL);
if (!strcmp(sym, RCS_HEAD_BRANCH)) {
num = rcsnum_alloc();
rcsnum_cpy(file->rf_head, num, 0);
return (num);
}
num = NULL;
TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
if (strcmp(symp->rs_name, sym) == 0)
break;
if (symp != NULL) {
num = rcsnum_alloc();
rcsnum_cpy(symp->rs_num, num, 0);
}
return (num);
}
/*
* rcs_sym_check()
*
* Check the RCS symbol name <sym> for any unsupported characters.
* Returns 1 if the tag is correct, 0 if it isn't valid.
*/
int
rcs_sym_check(const char *sym)
{
int ret;
const char *cp;
ret = 1;
cp = sym;
if (!isalpha(*cp++))
return (0);
for (; *cp != '\0'; cp++)
if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
ret = 0;
break;
}
return (ret);
}
/*
* rcs_lock_getmode()
*
* Retrieve the locking mode of the RCS file <file>.
*/
int
rcs_lock_getmode(RCSFILE *file)
{
return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
}
/*
* rcs_lock_setmode()
*
* Set the locking mode of the RCS file <file> to <mode>, which must either
* be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
* Returns the previous mode on success, or -1 on failure.
*/
int
rcs_lock_setmode(RCSFILE *file, int mode)
{
int pmode;
pmode = rcs_lock_getmode(file);
if (mode == RCS_LOCK_STRICT)
file->rf_flags |= RCS_SLOCK;
else if (mode == RCS_LOCK_LOOSE)
file->rf_flags &= ~RCS_SLOCK;
else
fatal("rcs_lock_setmode: invalid mode `%d'", mode);
file->rf_flags &= ~RCS_SYNCED;
return (pmode);
}
/*
* rcs_lock_add()
*
* Add an RCS lock for the user <user> on revision <rev>.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
{
struct rcs_lock *lkp;
/* first look for duplication */
TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
if (strcmp(lkp->rl_name, user) == 0 &&
rcsnum_cmp(rev, lkp->rl_num, 0) == 0)
return (-1);
}
lkp = xmalloc(sizeof(*lkp));
lkp->rl_name = xstrdup(user);
lkp->rl_num = rcsnum_alloc();
rcsnum_cpy(rev, lkp->rl_num, 0);
TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_lock_remove()
*
* Remove the RCS lock on revision <rev>.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
{
struct rcs_lock *lkp;
TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
if (strcmp(lkp->rl_name, user) == 0 &&
rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
break;
}
if (lkp == NULL)
return (-1);
TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
rcsnum_free(lkp->rl_num);
xfree(lkp->rl_name);
xfree(lkp);
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_desc_get()
*
* Retrieve the description for the RCS file <file>.
*/
const char *
rcs_desc_get(RCSFILE *file)
{
return (file->rf_desc);
}
/*
* rcs_desc_set()
*
* Set the description for the RCS file <file>.
*/
void
rcs_desc_set(RCSFILE *file, const char *desc)
{
char *tmp;
tmp = xstrdup(desc);
if (file->rf_desc != NULL)
xfree(file->rf_desc);
file->rf_desc = tmp;
file->rf_flags &= ~RCS_SYNCED;
}
/*
* rcs_comment_lookup()
*
* Lookup the assumed comment leader based on a file's suffix.
* Returns a pointer to the string on success, or NULL on failure.
*/
const char *
rcs_comment_lookup(const char *filename)
{
int i;
const char *sp;
if ((sp = strrchr(filename, '.')) == NULL)
return (NULL);
sp++;
for (i = 0; i < (int)NB_COMTYPES; i++)
if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
return (rcs_comments[i].rc_cstr);
return (NULL);
}
/*
* rcs_comment_get()
*
* Retrieve the comment leader for the RCS file <file>.
*/
const char *
rcs_comment_get(RCSFILE *file)
{
return (file->rf_comment);
}
/*
* rcs_comment_set()
*
* Set the comment leader for the RCS file <file>.
*/
void
rcs_comment_set(RCSFILE *file, const char *comment)
{
char *tmp;
tmp = xstrdup(comment);
if (file->rf_comment != NULL)
xfree(file->rf_comment);
file->rf_comment = tmp;
file->rf_flags &= ~RCS_SYNCED;
}
int
rcs_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines,
struct cvs_line **alines, struct rcs_delta *rdp)
{
u_char op;
char *ep;
struct cvs_line *lp, *dlp, *ndlp;
int i, lineno, nbln;
u_char tmp;
dlp = TAILQ_FIRST(&(dlines->l_lines));
lp = TAILQ_FIRST(&(plines->l_lines));
/* skip first bogus line */
for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
lp = TAILQ_NEXT(lp, l_list)) {
if (lp->l_len < 2)
fatal("line too short, RCS patch seems broken");
op = *(lp->l_line);
/* NUL-terminate line buffer for strtol() safety. */
tmp = lp->l_line[lp->l_len - 1];
lp->l_line[lp->l_len - 1] = '\0';
lineno = (int)strtol((char*)(lp->l_line + 1), &ep, 10);
if (lineno - 1 > dlines->l_nblines || lineno < 0) {
fatal("invalid line specification in RCS patch");
}
ep++;
nbln = (int)strtol(ep, &ep, 10);
/* Restore the last byte of the buffer */
lp->l_line[lp->l_len - 1] = tmp;
if (nbln < 0)
fatal("invalid line number specification in RCS patch");
/* find the appropriate line */
for (;;) {
if (dlp == NULL)
break;
if (dlp->l_lineno == lineno)
break;
if (dlp->l_lineno > lineno) {
dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
} else if (dlp->l_lineno < lineno) {
if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
ndlp->l_lineno > lineno)
break;
dlp = ndlp;
}
}
if (dlp == NULL)
fatal("can't find referenced line in RCS patch");
if (op == 'd') {
for (i = 0; (i < nbln) && (dlp != NULL); i++) {
ndlp = TAILQ_NEXT(dlp, l_list);
TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
if (alines != NULL && dlp->l_line != NULL) {
dlp->l_delta = rdp;
alines[dlp->l_lineno_orig - 1] =
dlp;
} else
xfree(dlp);
dlp = ndlp;
/* last line is gone - reset dlp */
if (dlp == NULL) {
ndlp = TAILQ_LAST(&(dlines->l_lines),
cvs_tqh);
dlp = ndlp;
}
}
} else if (op == 'a') {
for (i = 0; i < nbln; i++) {
ndlp = lp;
lp = TAILQ_NEXT(lp, l_list);
if (lp == NULL)
fatal("truncated RCS patch");
TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
if (alines != NULL) {
if (lp->l_needsfree == 1)
xfree(lp->l_line);
lp->l_line = NULL;
lp->l_needsfree = 0;
}
lp->l_delta = rdp;
TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
lp, l_list);
dlp = lp;
/* we don't want lookup to block on those */
lp->l_lineno = lineno;
lp = ndlp;
}
} else
fatal("unknown RCS patch operation `%c'", op);
/* last line of the patch, done */
if (lp->l_lineno == plines->l_nblines)
break;
}
/* once we're done patching, rebuild the line numbers */
lineno = 0;
TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
lp->l_lineno = lineno++;
dlines->l_nblines = lineno - 1;
return (0);
}
void
rcs_delta_stats(struct rcs_delta *rdp, int *ladded, int *lremoved)
{
struct cvs_lines *plines;
struct cvs_line *lp;
int added, i, lineno, nbln, removed;
char op, *ep;
u_char tmp;
added = removed = 0;
plines = cvs_splitlines(rdp->rd_text, rdp->rd_tlen);
lp = TAILQ_FIRST(&(plines->l_lines));
/* skip first bogus line */
for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
lp = TAILQ_NEXT(lp, l_list)) {
if (lp->l_len < 2)
fatal("line too short, RCS patch seems broken");
op = *(lp->l_line);
/* NUL-terminate line buffer for strtol() safety. */
tmp = lp->l_line[lp->l_len - 1];
lp->l_line[lp->l_len - 1] = '\0';
lineno = (int)strtol((lp->l_line + 1), &ep, 10);
ep++;
nbln = (int)strtol(ep, &ep, 10);
/* Restore the last byte of the buffer */
lp->l_line[lp->l_len - 1] = tmp;
if (nbln < 0)
fatal("invalid line number specification in RCS patch");
if (op == 'a') {
added += nbln;
for (i = 0; i < nbln; i++) {
lp = TAILQ_NEXT(lp, l_list);
if (lp == NULL)
fatal("truncated RCS patch");
}
}
else if (op == 'd')
removed += nbln;
else
fatal("unknown RCS patch operation '%c'", op);
}
cvs_freelines(plines);
*ladded = added;
*lremoved = removed;
}
/*
* rcs_rev_add()
*
* Add a revision to the RCS file <rf>. The new revision's number can be
* specified in <rev> (which can also be RCS_HEAD_REV, in which case the
* new revision will have a number equal to the previous head revision plus
* one). The <msg> argument specifies the log message for that revision, and
* <date> specifies the revision's date (a value of -1 is
* equivalent to using the current time).
* If <username> is NULL, set the author for this revision to the current user.
* Otherwise, set it to <username>.
* Returns 0 on success, or -1 on failure.
*/
int
rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
const char *username)
{
time_t now;
RCSNUM *root = NULL;
struct passwd *pw;
struct rcs_branch *brp, *obrp;
struct rcs_delta *ordp, *rdp;
if (rev == RCS_HEAD_REV) {
if (rf->rf_flags & RCS_CREATE) {
if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
return (-1);
if (rf->rf_head != NULL)
xfree(rf->rf_head);
rf->rf_head = rcsnum_alloc();
rcsnum_cpy(rev, rf->rf_head, 0);
} else if (rf->rf_head == NULL) {
return (-1);
} else {
rev = rcsnum_inc(rf->rf_head);
}
} else {
if ((rdp = rcs_findrev(rf, rev)) != NULL)
return (-1);
}
if ((pw = getpwuid(getuid())) == NULL)
fatal("getpwuid failed");
rdp = xcalloc(1, sizeof(*rdp));
TAILQ_INIT(&(rdp->rd_branches));
rdp->rd_num = rcsnum_alloc();
rcsnum_cpy(rev, rdp->rd_num, 0);
rdp->rd_next = rcsnum_alloc();
if (username == NULL)
username = pw->pw_name;
rdp->rd_author = xstrdup(username);
rdp->rd_state = xstrdup(RCS_STATE_EXP);
rdp->rd_log = xstrdup(msg);
if (date != (time_t)(-1))
now = date;
else
time(&now);
gmtime_r(&now, &(rdp->rd_date));
if (RCSNUM_ISBRANCHREV(rev))
TAILQ_INSERT_TAIL(&(rf->rf_delta), rdp, rd_list);
else
TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
rf->rf_ndelta++;
if (!(rf->rf_flags & RCS_CREATE)) {
if (RCSNUM_ISBRANCHREV(rev)) {
if (rev->rn_id[rev->rn_len - 1] == 1) {
/* a new branch */
root = rcsnum_branch_root(rev);
brp = xmalloc(sizeof(*brp));
brp->rb_num = rcsnum_alloc();
rcsnum_cpy(rdp->rd_num, brp->rb_num, 0);
if ((ordp = rcs_findrev(rf, root)) == NULL)
fatal("root node not found");
TAILQ_FOREACH(obrp, &(ordp->rd_branches),
rb_list) {
if (!rcsnum_cmp(obrp->rb_num,
brp->rb_num,
brp->rb_num->rn_len - 1))
break;
}
if (obrp == NULL) {
TAILQ_INSERT_TAIL(&(ordp->rd_branches),
brp, rb_list);
}
} else {
root = rcsnum_alloc();
rcsnum_cpy(rev, root, 0);
rcsnum_dec(root);
if ((ordp = rcs_findrev(rf, root)) == NULL)
fatal("previous revision not found");
rcsnum_cpy(rdp->rd_num, ordp->rd_next, 0);
}
} else {
ordp = TAILQ_NEXT(rdp, rd_list);
rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
}
}
if (root != NULL)
rcsnum_free(root);
/* not synced anymore */
rf->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_rev_remove()
*
* Remove the revision whose number is <rev> from the RCS file <rf>.
*/
int
rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
{
int fd1, fd2;
char *path_tmp1, *path_tmp2;
struct rcs_delta *rdp, *prevrdp, *nextrdp;
BUF *prevbuf, *newdiff, *newdeltatext;
if (rev == RCS_HEAD_REV)
rev = rf->rf_head;
if (rev == NULL)
return (-1);
/* do we actually have that revision? */
if ((rdp = rcs_findrev(rf, rev)) == NULL)
return (-1);
/*
* This is confusing, the previous delta is next in the TAILQ list.
* the next delta is the previous one in the TAILQ list.
*
* When the HEAD revision got specified, nextrdp will be NULL.
* When the first revision got specified, prevrdp will be NULL.
*/
prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, cvs_tqh, rd_list);
newdeltatext = NULL;
prevbuf = NULL;
path_tmp1 = path_tmp2 = NULL;
if (prevrdp != NULL && nextrdp != NULL) {
newdiff = cvs_buf_alloc(64);
/* calculate new diff */
(void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
fd1 = rcs_rev_write_stmp(rf, nextrdp->rd_num, path_tmp1, 0);
(void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
fd2 = rcs_rev_write_stmp(rf, prevrdp->rd_num, path_tmp2, 0);
diff_format = D_RCSDIFF;
if (cvs_diffreg(path_tmp1, path_tmp2,
fd1, fd2, newdiff) == D_ERROR)
fatal("rcs_diffreg failed");
close(fd1);
close(fd2);
newdeltatext = newdiff;
} else if (nextrdp == NULL && prevrdp != NULL) {
newdeltatext = prevbuf;
}
if (newdeltatext != NULL) {
if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
fatal("error setting new deltatext");
}
TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
/* update pointers */
if (prevrdp != NULL && nextrdp != NULL) {
rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
} else if (prevrdp != NULL) {
if (rcs_head_set(rf, prevrdp->rd_num) < 0)
fatal("rcs_head_set failed");
} else if (nextrdp != NULL) {
rcsnum_free(nextrdp->rd_next);
nextrdp->rd_next = rcsnum_alloc();
} else {
rcsnum_free(rf->rf_head);
rf->rf_head = NULL;
}
rf->rf_ndelta--;
rf->rf_flags &= ~RCS_SYNCED;
rcs_freedelta(rdp);
if (newdeltatext != NULL)
xfree(newdeltatext);
if (path_tmp1 != NULL)
xfree(path_tmp1);
if (path_tmp2 != NULL)
xfree(path_tmp2);
return (0);
}
/*
* rcs_findrev()
*
* Find a specific revision's delta entry in the tree of the RCS file <rfp>.
* The revision number is given in <rev>.
*
* Returns a pointer to the delta on success, or NULL on failure.
*/
struct rcs_delta *
rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
{
int isbrev;
struct rcs_delta *rdp;
if (rev == NULL)
return NULL;
isbrev = RCSNUM_ISBRANCHREV(rev);
/*
* We need to do more parsing if the last revision in the linked list
* is greater than the requested revision.
*/
rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
if (rdp == NULL ||
(!isbrev && rcsnum_cmp(rdp->rd_num, rev, 0) == -1) ||
((isbrev && rdp->rd_num->rn_len < 4) ||
(isbrev && rcsnum_differ(rev, rdp->rd_num)))) {
rcs_parse_deltas(rfp, rev);
}
TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
if (rcsnum_differ(rdp->rd_num, rev))
continue;
else
return (rdp);
}
return (NULL);
}
/*
* rcs_kwexp_set()
*
* Set the keyword expansion mode to use on the RCS file <file> to <mode>.
*/
void
rcs_kwexp_set(RCSFILE *file, int mode)
{
int i;
char *tmp, buf[8] = "";
if (RCS_KWEXP_INVAL(mode))
return;
i = 0;
if (mode == RCS_KWEXP_NONE)
buf[0] = 'b';
else if (mode == RCS_KWEXP_OLD)
buf[0] = 'o';
else {
if (mode & RCS_KWEXP_NAME)
buf[i++] = 'k';
if (mode & RCS_KWEXP_VAL)
buf[i++] = 'v';
if (mode & RCS_KWEXP_LKR)
buf[i++] = 'l';
}
tmp = xstrdup(buf);
if (file->rf_expand != NULL)
xfree(file->rf_expand);
file->rf_expand = tmp;
/* not synced anymore */
file->rf_flags &= ~RCS_SYNCED;
}
/*
* rcs_kwexp_get()
*
* Retrieve the keyword expansion mode to be used for the RCS file <file>.
*/
int
rcs_kwexp_get(RCSFILE *file)
{
return rcs_kflag_get(file->rf_expand);
}
/*
* rcs_kflag_get()
*
* Get the keyword expansion mode from a set of character flags given in
* <flags> and return the appropriate flag mask. In case of an error, the
* returned mask will have the RCS_KWEXP_ERR bit set to 1.
*/
int
rcs_kflag_get(const char *flags)
{
int fl;
size_t len;
const char *fp;
if (flags == NULL)
return 0;
fl = 0;
if (!(len = strlen(flags)))
return RCS_KWEXP_ERR;
for (fp = flags; *fp != '\0'; fp++) {
if (*fp == 'k')
fl |= RCS_KWEXP_NAME;
else if (*fp == 'v')
fl |= RCS_KWEXP_VAL;
else if (*fp == 'l')
fl |= RCS_KWEXP_LKR;
else if (*fp == 'o') {
if (len != 1)
fl |= RCS_KWEXP_ERR;
fl |= RCS_KWEXP_OLD;
} else if (*fp == 'b') {
if (len != 1)
fl |= RCS_KWEXP_ERR;
fl |= RCS_KWEXP_NONE;
} else /* unknown letter */
fl |= RCS_KWEXP_ERR;
}
return (fl);
}
/* rcs_parse_deltas()
*
* Parse deltas. If <rev> is not NULL, parse only as far as that
* revision. If <rev> is NULL, parse all deltas.
*/
static void
rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
{
int ret;
struct rcs_delta *enddelta;
if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
return;
for (;;) {
ret = rcs_parse_delta(rfp);
if (rev != NULL) {
enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
break;
}
if (ret == 0) {
rfp->rf_flags |= PARSED_DELTAS;
break;
}
else if (ret == -1)
fatal("error parsing deltas");
}
}
/* rcs_parse_deltatexts()
*
* Parse deltatexts. If <rev> is not NULL, parse only as far as that
* revision. If <rev> is NULL, parse everything.
*/
static void
rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
{
int ret;
struct rcs_delta *rdp;
if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
(rfp->rf_flags & RCS_CREATE))
return;
if (!(rfp->rf_flags & PARSED_DESC))
rcs_parse_desc(rfp, rev);
for (;;) {
if (rev != NULL) {
rdp = rcs_findrev(rfp, rev);
if (rdp->rd_text != NULL)
break;
else
ret = rcs_parse_deltatext(rfp);
} else
ret = rcs_parse_deltatext(rfp);
if (ret == 0) {
rfp->rf_flags |= PARSED_DELTATEXTS;
break;
}
else if (ret == -1)
fatal("problem parsing deltatexts");
}
}
/* rcs_parse_desc()
*
* Parse RCS description.
*/
static void
rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
{
int ret = 0;
if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
return;
if (!(rfp->rf_flags & PARSED_DELTAS))
rcs_parse_deltas(rfp, rev);
/* do parsing */
ret = rcs_gettok(rfp);
if (ret != RCS_TOK_DESC)
fatal("token `%s' found where RCS desc expected",
RCS_TOKSTR(rfp));
ret = rcs_gettok(rfp);
if (ret != RCS_TOK_STRING)
fatal("token `%s' found where RCS desc expected",
RCS_TOKSTR(rfp));
rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
rfp->rf_flags |= PARSED_DESC;
}
/*
* rcs_parse_init()
*
* Initial parsing of file <path>, which are in the RCS format.
* Just does admin section.
*/
static void
rcs_parse_init(RCSFILE *rfp)
{
struct rcs_pdata *pdp;
if (rfp->rf_flags & RCS_PARSED)
return;
pdp = xcalloc(1, sizeof(*pdp));
pdp->rp_lines = 0;
pdp->rp_pttype = RCS_TOK_ERR;
if ((pdp->rp_file = fdopen(rfp->fd, "r")) == NULL)
fatal("fdopen: `%s'", rfp->rf_path);
pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
pdp->rp_blen = RCS_BUFSIZE;
pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
/* ditch the strict lock */
rfp->rf_flags &= ~RCS_SLOCK;
rfp->rf_pdata = pdp;
if (rcs_parse_admin(rfp) < 0) {
rcs_freepdata(pdp);
fatal("could not parse admin data");
}
if (rfp->rf_flags & RCS_PARSE_FULLY) {
rcs_parse_deltatexts(rfp, NULL);
(void)close(rfp->fd);
rfp->fd = -1;
}
rfp->rf_flags |= RCS_SYNCED;
}
/*
* rcs_parse_admin()
*
* Parse the administrative portion of an RCS file.
* Returns the type of the first token found after the admin section on
* success, or -1 on failure.
*/
static int
rcs_parse_admin(RCSFILE *rfp)
{
u_int i;
int tok, ntok, hmask;
struct rcs_key *rk;
rfp->rf_head = NULL;
rfp->rf_branch = NULL;
/* hmask is a mask of the headers already encountered */
hmask = 0;
for (;;) {
tok = rcs_gettok(rfp);
if (tok == RCS_TOK_ERR) {
cvs_log(LP_ERR, "parse error in RCS admin section");
goto fail;
} else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
/*
* Assume this is the start of the first delta or
* that we are dealing with an empty RCS file and
* we just found the description.
*/
if (!(hmask & (1 << RCS_TOK_HEAD))) {
cvs_log(LP_ERR, "head missing");
goto fail;
}
rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
return (tok);
}
rk = NULL;
for (i = 0; i < RCS_NKEYS; i++)
if (rcs_keys[i].rk_id == tok)
rk = &(rcs_keys[i]);
if (hmask & (1 << tok)) {
cvs_log(LP_ERR, "duplicate RCS key");
goto fail;
}
hmask |= (1 << tok);
switch (tok) {
case RCS_TOK_HEAD:
case RCS_TOK_BRANCH:
case RCS_TOK_COMMENT:
case RCS_TOK_EXPAND:
ntok = rcs_gettok(rfp);
if (ntok == RCS_TOK_SCOLON)
break;
if (ntok != rk->rk_val) {
cvs_log(LP_ERR,
"invalid value type for RCS key `%s'",
rk->rk_str);
}
if (tok == RCS_TOK_HEAD) {
if (rfp->rf_head == NULL)
rfp->rf_head = rcsnum_alloc();
if (RCS_TOKSTR(rfp)[0] == '\0' ||
rcsnum_aton(RCS_TOKSTR(rfp), NULL,
rfp->rf_head) < 0) {
rcsnum_free(rfp->rf_head);
rfp->rf_head = NULL;
}
} else if (tok == RCS_TOK_BRANCH) {
if (rfp->rf_branch == NULL)
rfp->rf_branch = rcsnum_alloc();
if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
rfp->rf_branch) < 0)
goto fail;
} else if (tok == RCS_TOK_COMMENT) {
rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
} else if (tok == RCS_TOK_EXPAND) {
rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
}
/* now get the expected semi-colon */
ntok = rcs_gettok(rfp);
if (ntok != RCS_TOK_SCOLON) {
cvs_log(LP_ERR,
"missing semi-colon after RCS `%s' key",
rk->rk_str);
goto fail;
}
break;
case RCS_TOK_ACCESS:
if (rcs_parse_access(rfp) < 0)
goto fail;
break;
case RCS_TOK_SYMBOLS:
rcs_ignore_keys = 1;
if (rcs_parse_symbols(rfp) < 0)
goto fail;
break;
case RCS_TOK_LOCKS:
if (rcs_parse_locks(rfp) < 0)
goto fail;
break;
default:
cvs_log(LP_ERR,
"unexpected token `%s' in RCS admin section",
RCS_TOKSTR(rfp));
goto fail;
}
rcs_ignore_keys = 0;
}
fail:
return (-1);
}
/*
* rcs_parse_delta()
*
* Parse an RCS delta section and allocate the structure to store that delta's
* information in the <rfp> delta list.
* Returns 1 if the section was parsed OK, 0 if it is the last delta, and
* -1 on error.
*/
static int
rcs_parse_delta(RCSFILE *rfp)
{
int ret, tok, ntok, hmask;
u_int i;
char *tokstr;
RCSNUM *datenum;
struct rcs_delta *rdp;
struct rcs_key *rk;
tok = rcs_gettok(rfp);
if (tok == RCS_TOK_DESC) {
rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
return (0);
} else if (tok != RCS_TOK_NUM) {
cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
RCS_TOKSTR(rfp));
return (-1);
}
rdp = xcalloc(1, sizeof(*rdp));
rdp->rd_num = rcsnum_alloc();
rdp->rd_next = rcsnum_alloc();
TAILQ_INIT(&(rdp->rd_branches));
rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
hmask = 0;
ret = 0;
tokstr = NULL;
for (;;) {
tok = rcs_gettok(rfp);
if (tok == RCS_TOK_ERR) {
cvs_log(LP_ERR, "parse error in RCS delta section");
rcs_freedelta(rdp);
return (-1);
} else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
ret = (tok == RCS_TOK_NUM ? 1 : 0);
break;
}
rk = NULL;
for (i = 0; i < RCS_NKEYS; i++)
if (rcs_keys[i].rk_id == tok)
rk = &(rcs_keys[i]);
if (hmask & (1 << tok)) {
cvs_log(LP_ERR, "duplicate RCS key");
rcs_freedelta(rdp);
return (-1);
}
hmask |= (1 << tok);
switch (tok) {
case RCS_TOK_DATE:
case RCS_TOK_AUTHOR:
case RCS_TOK_STATE:
case RCS_TOK_NEXT:
ntok = rcs_gettok(rfp);
if (ntok == RCS_TOK_SCOLON) {
if (rk->rk_flags & RCS_VOPT)
break;
else {
cvs_log(LP_ERR, "missing mandatory "
"value to RCS key `%s'",
rk->rk_str);
rcs_freedelta(rdp);
return (-1);
}
}
if (ntok != rk->rk_val) {
cvs_log(LP_ERR,
"invalid value type for RCS key `%s'",
rk->rk_str);
rcs_freedelta(rdp);
return (-1);
}
if (tokstr != NULL)
xfree(tokstr);
tokstr = xstrdup(RCS_TOKSTR(rfp));
/* now get the expected semi-colon */
ntok = rcs_gettok(rfp);
if (ntok != RCS_TOK_SCOLON) {
cvs_log(LP_ERR,
"missing semi-colon after RCS `%s' key",
rk->rk_str);
xfree(tokstr);
rcs_freedelta(rdp);
return (-1);
}
if (tok == RCS_TOK_DATE) {
if ((datenum = rcsnum_parse(tokstr)) == NULL) {
xfree(tokstr);
rcs_freedelta(rdp);
return (-1);
}
if (datenum->rn_len != 6) {
cvs_log(LP_ERR,
"RCS date specification has %s "
"fields",
(datenum->rn_len > 6) ? "too many" :
"missing");
xfree(tokstr);
rcs_freedelta(rdp);
rcsnum_free(datenum);
return (-1);
}
rdp->rd_date.tm_year = datenum->rn_id[0];
if (rdp->rd_date.tm_year >= 1900)
rdp->rd_date.tm_year -= 1900;
rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
rdp->rd_date.tm_mday = datenum->rn_id[2];
rdp->rd_date.tm_hour = datenum->rn_id[3];
rdp->rd_date.tm_min = datenum->rn_id[4];
rdp->rd_date.tm_sec = datenum->rn_id[5];
rcsnum_free(datenum);
} else if (tok == RCS_TOK_AUTHOR) {
rdp->rd_author = tokstr;
tokstr = NULL;
} else if (tok == RCS_TOK_STATE) {
rdp->rd_state = tokstr;
tokstr = NULL;
} else if (tok == RCS_TOK_NEXT) {
rcsnum_aton(tokstr, NULL, rdp->rd_next);
}
break;
case RCS_TOK_BRANCHES:
if (rcs_parse_branches(rfp, rdp) < 0) {
rcs_freedelta(rdp);
return (-1);
}
break;
default:
cvs_log(LP_ERR, "unexpected token `%s' in RCS delta",
RCS_TOKSTR(rfp));
rcs_freedelta(rdp);
return (-1);
}
}
if (tokstr != NULL)
xfree(tokstr);
TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
rfp->rf_ndelta++;
return (ret);
}
/*
* rcs_parse_deltatext()
*
* Parse an RCS delta text section and fill in the log and text field of the
* appropriate delta section.
* Returns 1 if the section was parsed OK, 0 if it is the last delta, and
* -1 on error.
*/
static int
rcs_parse_deltatext(RCSFILE *rfp)
{
int tok;
RCSNUM *tnum;
struct rcs_delta *rdp;
tok = rcs_gettok(rfp);
if (tok == RCS_TOK_EOF)
return (0);
if (tok != RCS_TOK_NUM) {
cvs_log(LP_ERR,
"unexpected token `%s' at start of RCS delta text",
RCS_TOKSTR(rfp));
return (-1);
}
tnum = rcsnum_alloc();
rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
break;
}
rcsnum_free(tnum);
if (rdp == NULL) {
cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
RCS_TOKSTR(rfp));
return (-1);
}
tok = rcs_gettok(rfp);
if (tok != RCS_TOK_LOG) {
cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
RCS_TOKSTR(rfp));
return (-1);
}
tok = rcs_gettok(rfp);
if (tok != RCS_TOK_STRING) {
cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
RCS_TOKSTR(rfp));
return (-1);
}
rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
tok = rcs_gettok(rfp);
if (tok != RCS_TOK_TEXT) {
cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
RCS_TOKSTR(rfp));
return (-1);
}
tok = rcs_gettok(rfp);
if (tok != RCS_TOK_STRING) {
cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
RCS_TOKSTR(rfp));
return (-1);
}
if (RCS_TOKLEN(rfp) == 0) {
rdp->rd_text = xmalloc(1);
rdp->rd_text[0] = '\0';
rdp->rd_tlen = 0;
} else {
rdp->rd_text = xmalloc(RCS_TOKLEN(rfp));
memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
rdp->rd_tlen = RCS_TOKLEN(rfp);
}
return (1);
}
/*
* rcs_parse_access()
*
* Parse the access list given as value to the `access' keyword.
* Returns 0 on success, or -1 on failure.
*/
static int
rcs_parse_access(RCSFILE *rfp)
{
int type;
while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
if (type != RCS_TOK_ID) {
cvs_log(LP_ERR, "unexpected token `%s' in access list",
RCS_TOKSTR(rfp));
return (-1);
}
if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
return (-1);
}
return (0);
}
/*
* rcs_parse_symbols()
*
* Parse the symbol list given as value to the `symbols' keyword.
* Returns 0 on success, or -1 on failure.
*/
static int
rcs_parse_symbols(RCSFILE *rfp)
{
int type;
struct rcs_sym *symp;
for (;;) {
type = rcs_gettok(rfp);
if (type == RCS_TOK_SCOLON)
break;
if (type != RCS_TOK_ID) {
cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
RCS_TOKSTR(rfp));
return (-1);
}
symp = xmalloc(sizeof(*symp));
symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
symp->rs_num = rcsnum_alloc();
type = rcs_gettok(rfp);
if (type != RCS_TOK_COLON) {
cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
RCS_TOKSTR(rfp));
rcsnum_free(symp->rs_num);
xfree(symp->rs_name);
xfree(symp);
return (-1);
}
type = rcs_gettok(rfp);
if (type != RCS_TOK_NUM) {
cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
RCS_TOKSTR(rfp));
rcsnum_free(symp->rs_num);
xfree(symp->rs_name);
xfree(symp);
return (-1);
}
if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
RCS_TOKSTR(rfp));
rcsnum_free(symp->rs_num);
xfree(symp->rs_name);
xfree(symp);
return (-1);
}
TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
}
return (0);
}
/*
* rcs_parse_locks()
*
* Parse the lock list given as value to the `locks' keyword.
* Returns 0 on success, or -1 on failure.
*/
static int
rcs_parse_locks(RCSFILE *rfp)
{
int type;
struct rcs_lock *lkp;
for (;;) {
type = rcs_gettok(rfp);
if (type == RCS_TOK_SCOLON)
break;
if (type != RCS_TOK_ID) {
cvs_log(LP_ERR, "unexpected token `%s' in lock list",
RCS_TOKSTR(rfp));
return (-1);
}
lkp = xmalloc(sizeof(*lkp));
lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
lkp->rl_num = rcsnum_alloc();
type = rcs_gettok(rfp);
if (type != RCS_TOK_COLON) {
cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
RCS_TOKSTR(rfp));
rcsnum_free(lkp->rl_num);
xfree(lkp->rl_name);
xfree(lkp);
return (-1);
}
type = rcs_gettok(rfp);
if (type != RCS_TOK_NUM) {
cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
RCS_TOKSTR(rfp));
rcsnum_free(lkp->rl_num);
xfree(lkp->rl_name);
xfree(lkp);
return (-1);
}
if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
RCS_TOKSTR(rfp));
rcsnum_free(lkp->rl_num);
xfree(lkp->rl_name);
xfree(lkp);
return (-1);
}
TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
}
/* check if we have a `strict' */
type = rcs_gettok(rfp);
if (type != RCS_TOK_STRICT) {
rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
} else {
rfp->rf_flags |= RCS_SLOCK;
type = rcs_gettok(rfp);
if (type != RCS_TOK_SCOLON) {
cvs_log(LP_ERR,
"missing semi-colon after `strict' keyword");
return (-1);
}
}
return (0);
}
/*
* rcs_parse_branches()
*
* Parse the list of branches following a `branches' keyword in a delta.
* Returns 0 on success, or -1 on failure.
*/
static int
rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
{
int type;
struct rcs_branch *brp;
for (;;) {
type = rcs_gettok(rfp);
if (type == RCS_TOK_SCOLON)
break;
if (type != RCS_TOK_NUM) {
cvs_log(LP_ERR,
"unexpected token `%s' in list of branches",
RCS_TOKSTR(rfp));
return (-1);
}
brp = xmalloc(sizeof(*brp));
brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
if (brp->rb_num == NULL) {
xfree(brp);
return (-1);
}
TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
}
return (0);
}
/*
* rcs_freedelta()
*
* Free the contents of a delta structure.
*/
static void
rcs_freedelta(struct rcs_delta *rdp)
{
struct rcs_branch *rb;
if (rdp->rd_num != NULL)
rcsnum_free(rdp->rd_num);
if (rdp->rd_next != NULL)
rcsnum_free(rdp->rd_next);
if (rdp->rd_author != NULL)
xfree(rdp->rd_author);
if (rdp->rd_locker != NULL)
xfree(rdp->rd_locker);
if (rdp->rd_state != NULL)
xfree(rdp->rd_state);
if (rdp->rd_log != NULL)
xfree(rdp->rd_log);
if (rdp->rd_text != NULL)
xfree(rdp->rd_text);
while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
rcsnum_free(rb->rb_num);
xfree(rb);
}
xfree(rdp);
}
/*
* rcs_freepdata()
*
* Free the contents of the parser data structure.
*/
static void
rcs_freepdata(struct rcs_pdata *pd)
{
if (pd->rp_file != NULL)
(void)fclose(pd->rp_file);
if (pd->rp_buf != NULL)
xfree(pd->rp_buf);
xfree(pd);
}
/*
* rcs_gettok()
*
* Get the next RCS token from the string <str>.
*/
static int
rcs_gettok(RCSFILE *rfp)
{
u_int i;
int ch, last, type;
size_t len;
char *bp;
struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
type = RCS_TOK_ERR;
bp = pdp->rp_buf;
pdp->rp_tlen = 0;
*bp = '\0';
if (pdp->rp_pttype != RCS_TOK_ERR) {
type = pdp->rp_pttype;
if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
pdp->rp_blen)
fatal("rcs_gettok: strlcpy");
pdp->rp_pttype = RCS_TOK_ERR;
return (type);
}
/* skip leading whitespace */
/* XXX we must skip backspace too for compatibility, should we? */
do {
ch = getc(pdp->rp_file);
if (ch == '\n')
pdp->rp_lines++;
} while (isspace(ch));
if (ch == EOF) {
type = RCS_TOK_EOF;
} else if (ch == ';') {
type = RCS_TOK_SCOLON;
} else if (ch == ':') {
type = RCS_TOK_COLON;
} else if (isalpha(ch)) {
type = RCS_TOK_ID;
*(bp++) = ch;
for (;;) {
ch = getc(pdp->rp_file);
if (ch == EOF) {
type = RCS_TOK_EOF;
break;
} else if (!isalnum(ch) && ch != '_' && ch != '-' &&
ch != '/' && ch != '+' && ch != '|') {
ungetc(ch, pdp->rp_file);
break;
}
*(bp++) = ch;
pdp->rp_tlen++;
if (bp == pdp->rp_bufend - 1) {
len = bp - pdp->rp_buf;
rcs_growbuf(rfp);
bp = pdp->rp_buf + len;
}
}
*bp = '\0';
if (type != RCS_TOK_ERR && rcs_ignore_keys != 1) {
for (i = 0; i < RCS_NKEYS; i++) {
if (strcmp(rcs_keys[i].rk_str,
pdp->rp_buf) == 0) {
type = rcs_keys[i].rk_id;
break;
}
}
}
} else if (ch == '@') {
/* we have a string */
type = RCS_TOK_STRING;
for (;;) {
ch = getc(pdp->rp_file);
if (ch == EOF) {
type = RCS_TOK_EOF;
break;
} else if (ch == '@') {
ch = getc(pdp->rp_file);
if (ch != '@') {
ungetc(ch, pdp->rp_file);
break;
}
} else if (ch == '\n')
pdp->rp_lines++;
*(bp++) = ch;
pdp->rp_tlen++;
if (bp == pdp->rp_bufend - 1) {
len = bp - pdp->rp_buf;
rcs_growbuf(rfp);
bp = pdp->rp_buf + len;
}
}
*bp = '\0';
} else if (isdigit(ch)) {
*(bp++) = ch;
last = ch;
type = RCS_TOK_NUM;
for (;;) {
ch = getc(pdp->rp_file);
if (ch == EOF) {
type = RCS_TOK_EOF;
break;
}
if (bp == pdp->rp_bufend)
break;
if (!isdigit(ch) && ch != '.') {
ungetc(ch, pdp->rp_file);
break;
}
if (last == '.' && ch == '.') {
type = RCS_TOK_ERR;
break;
}
last = ch;
*(bp++) = ch;
pdp->rp_tlen++;
}
*bp = '\0';
}
return (type);
}
/*
* rcs_pushtok()
*
* Push a token back in the parser's token buffer.
*/
static int
rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
{
struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
if (pdp->rp_pttype != RCS_TOK_ERR)
return (-1);
pdp->rp_pttype = type;
if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
sizeof(pdp->rp_ptok))
fatal("rcs_pushtok: strlcpy");
return (0);
}
/*
* rcs_growbuf()
*
* Attempt to grow the internal parse buffer for the RCS file <rf> by
* RCS_BUFEXTSIZE.
* In case of failure, the original buffer is left unmodified.
*/
static void
rcs_growbuf(RCSFILE *rf)
{
struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
pdp->rp_buf = xrealloc(pdp->rp_buf, 1,
pdp->rp_blen + RCS_BUFEXTSIZE);
pdp->rp_blen += RCS_BUFEXTSIZE;
pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
}
/*
* rcs_strprint()
*
* Output an RCS string <str> of size <slen> to the stream <stream>. Any
* '@' characters are escaped. Otherwise, the string can contain arbitrary
* binary data.
*/
static void
rcs_strprint(const u_char *str, size_t slen, FILE *stream)
{
const u_char *ap, *ep, *sp;
if (slen == 0)
return;
ep = str + slen - 1;
for (sp = str; sp <= ep;) {
ap = memchr(sp, '@', ep - sp);
if (ap == NULL)
ap = ep;
(void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
if (*ap == '@')
putc('@', stream);
sp = ap + 1;
}
}
/*
* rcs_deltatext_set()
*
* Set deltatext for <rev> in RCS file <rfp> to <dtext>
* Returns -1 on error, 0 on success.
*/
int
rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
{
size_t len;
u_char *dtext;
struct rcs_delta *rdp;
/* Write operations require full parsing */
rcs_parse_deltatexts(rfp, NULL);
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (-1);
if (rdp->rd_text != NULL)
xfree(rdp->rd_text);
len = cvs_buf_len(bp);
dtext = cvs_buf_release(bp);
bp = NULL;
if (len != 0) {
rdp->rd_text = xmalloc(len);
rdp->rd_tlen = len;
(void)memcpy(rdp->rd_text, dtext, len);
} else {
rdp->rd_text = NULL;
rdp->rd_tlen = 0;
}
if (dtext != NULL)
xfree(dtext);
return (0);
}
/*
* rcs_rev_setlog()
*
* Sets the log message of revision <rev> to <logtext>
*/
int
rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
{
struct rcs_delta *rdp;
char buf[CVS_REV_BUFSZ];
rcsnum_tostr(rev, buf, sizeof(buf));
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (-1);
if (rdp->rd_log != NULL)
xfree(rdp->rd_log);
rdp->rd_log = xstrdup(logtext);
rfp->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_rev_getdate()
*
* Get the date corresponding to a given revision.
* Returns the date on success, -1 on failure.
*/
time_t
rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
{
struct rcs_delta *rdp;
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (-1);
return (timegm(&rdp->rd_date));
}
/*
* rcs_state_set()
*
* Sets the state of revision <rev> to <state>
* NOTE: default state is 'Exp'. States may not contain spaces.
*
* Returns -1 on failure, 0 on success.
*/
int
rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
{
struct rcs_delta *rdp;
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (-1);
if (rdp->rd_state != NULL)
xfree(rdp->rd_state);
rdp->rd_state = xstrdup(state);
rfp->rf_flags &= ~RCS_SYNCED;
return (0);
}
/*
* rcs_state_check()
*
* Check if string <state> is valid.
*
* Returns 0 if the string is valid, -1 otherwise.
*/
int
rcs_state_check(const char *state)
{
if (strchr(state, ' ') != NULL)
return (-1);
return (0);
}
/*
* rcs_state_get()
*
* Get the state for a given revision of a specified RCSFILE.
*
* Returns NULL on failure.
*/
const char *
rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
{
struct rcs_delta *rdp;
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (NULL);
return (rdp->rd_state);
}
/* rcs_get_revision() */
static RCSNUM *
rcs_get_revision(const char *revstr, RCSFILE *rfp)
{
RCSNUM *rev, *brev, *frev;
struct rcs_branch *brp;
struct rcs_delta *rdp;
size_t i;
rdp = NULL;
if (!strcmp(revstr, RCS_HEAD_BRANCH)) {
if (rfp->rf_head == NULL)
return NULL;
frev = rcsnum_alloc();
rcsnum_cpy(rfp->rf_head, frev, 0);
return (frev);
}
/* Possibly we could be passed a version number */
if ((rev = rcsnum_parse(revstr)) != NULL) {
/* Do not return if it is not in RCS file */
if ((rdp = rcs_findrev(rfp, rev)) != NULL) {
frev = rcsnum_alloc();
rcsnum_cpy(rev, frev, 0);
return (frev);
}
} else {
/* More likely we will be passed a symbol */
rev = rcs_sym_getrev(rfp, revstr);
}
if (rev == NULL)
return (NULL);
/*
* If it was not a branch, thats ok the symbolic
* name refered to a revision, so return the resolved
* revision for the given name. */
if (!RCSNUM_ISBRANCH(rev)) {
/* Sanity check: The first two elements of any
* revision (be it on a branch or on trunk) cannot
* be greater than HEAD.
*
* XXX: To avoid comparing to uninitialized memory,
* the minimum of both revision lengths is taken
* instead of just 2.
*/
if (rfp->rf_head == NULL)
return NULL;
if (rcsnum_cmp(rev, rfp->rf_head,
MIN(rfp->rf_head->rn_len, rev->rn_len)) < 0) {
rcsnum_free(rev);
return NULL;
}
return (rev);
}
brev = rcsnum_alloc();
rcsnum_cpy(rev, brev, rev->rn_len - 1);
if ((rdp = rcs_findrev(rfp, brev)) == NULL)
fatal("rcs_get_revision: tag `%s' does not exist", revstr);
rcsnum_free(brev);
TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
for (i = 0; i < rev->rn_len; i++)
if (brp->rb_num->rn_id[i] != rev->rn_id[i])
break;
if (i != rev->rn_len)
continue;
break;
}
rcsnum_free(rev);
frev = rcsnum_alloc();
if (brp == NULL) {
rcsnum_cpy(rdp->rd_num, frev, 0);
return (frev);
} else {
/* Fetch the delta with the correct branch num */
if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
fatal("rcs_get_revision: could not fetch branch "
"delta");
rcsnum_cpy(rdp->rd_num, frev, 0);
return (frev);
}
}
/*
* rcs_rev_getlines()
*
* Get the entire contents of revision <frev> from the RCSFILE <rfp> and
* return it as a pointer to a struct cvs_lines.
*/
struct cvs_lines *
rcs_rev_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
{
size_t plen;
int annotate, done, i, nextroot;
RCSNUM *tnum, *bnum;
struct rcs_branch *brp;
struct rcs_delta *hrdp, *prdp, *rdp, *trdp;
u_char *patch;
struct cvs_line *line, *nline;
struct cvs_lines *dlines, *plines;
if (rfp->rf_head == NULL ||
(hrdp = rcs_findrev(rfp, rfp->rf_head)) == NULL)
fatal("rcs_rev_getlines: no HEAD revision");
tnum = frev;
rcs_parse_deltatexts(rfp, hrdp->rd_num);
/* revision on branch, get the branch root */
nextroot = 2;
bnum = rcsnum_alloc();
if (RCSNUM_ISBRANCHREV(tnum))
rcsnum_cpy(tnum, bnum, nextroot);
else
rcsnum_cpy(tnum, bnum, tnum->rn_len);
if (alines != NULL) {
/* start with annotate first at requested revision */
annotate = ANNOTATE_LATER;
*alines = NULL;
} else
annotate = ANNOTATE_NEVER;
dlines = cvs_splitlines(hrdp->rd_text, hrdp->rd_tlen);
done = 0;
rdp = hrdp;
if (!rcsnum_differ(rdp->rd_num, bnum)) {
if (annotate == ANNOTATE_LATER) {
/* found requested revision for annotate */
i = 0;
TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
line->l_lineno_orig = line->l_lineno;
i++;
}
*alines = xcalloc(i + 1, sizeof(struct cvs_line *));
(*alines)[i] = NULL;
annotate = ANNOTATE_NOW;
/* annotate down to 1.1 from where we are */
rcsnum_free(bnum);
bnum = rcsnum_parse("1.1");
if (!rcsnum_differ(rdp->rd_num, bnum)) {
goto next;
}
} else
goto next;
}
prdp = hrdp;
if ((rdp = rcs_findrev(rfp, hrdp->rd_next)) == NULL)
goto done;
again:
for (;;) {
if (rdp->rd_next->rn_len != 0) {
trdp = rcs_findrev(rfp, rdp->rd_next);
if (trdp == NULL)
fatal("failed to grab next revision");
}
if (rdp->rd_tlen == 0) {
rcs_parse_deltatexts(rfp, rdp->rd_num);
if (rdp->rd_tlen == 0) {
if (!rcsnum_differ(rdp->rd_num, bnum))
break;
rdp = trdp;
continue;
}
}
plen = rdp->rd_tlen;
patch = rdp->rd_text;
plines = cvs_splitlines(patch, plen);
if (annotate == ANNOTATE_NOW)
rcs_patch_lines(dlines, plines, *alines, prdp);
else
rcs_patch_lines(dlines, plines, NULL, NULL);
cvs_freelines(plines);
if (!rcsnum_differ(rdp->rd_num, bnum)) {
if (annotate != ANNOTATE_LATER)
break;
/* found requested revision for annotate */
i = 0;
TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
line->l_lineno_orig = line->l_lineno;
i++;
}
*alines = xcalloc(i + 1, sizeof(struct cvs_line *));
(*alines)[i] = NULL;
annotate = ANNOTATE_NOW;
/* annotate down to 1.1 from where we are */
rcsnum_free(bnum);
bnum = rcsnum_parse("1.1");
if (!rcsnum_differ(rdp->rd_num, bnum))
break;
}
prdp = rdp;
rdp = trdp;
}
next:
if (!rcsnum_differ(rdp->rd_num, frev))
done = 1;
if (RCSNUM_ISBRANCHREV(frev) && done != 1) {
nextroot += 2;
rcsnum_cpy(frev, bnum, nextroot);
TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
for (i = 0; i < nextroot - 1; i++)
if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
break;
if (i == nextroot - 1)
break;
}
if (brp == NULL) {
if (annotate != ANNOTATE_NEVER) {
if (*alines != NULL)
xfree(*alines);
*alines = NULL;
cvs_freelines(dlines);
rcsnum_free(bnum);
return (NULL);
}
fatal("expected branch not found on branch list");
}
if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
fatal("rcs_rev_getlines: failed to get delta for target rev");
goto again;
}
done:
/* put remaining lines into annotate buffer */
if (annotate == ANNOTATE_NOW) {
for (line = TAILQ_FIRST(&(dlines->l_lines));
line != NULL; line = nline) {
nline = TAILQ_NEXT(line, l_list);
TAILQ_REMOVE(&(dlines->l_lines), line, l_list);
if (line->l_line == NULL) {
xfree(line);
continue;
}
line->l_delta = rdp;
(*alines)[line->l_lineno_orig - 1] = line;
}
cvs_freelines(dlines);
dlines = NULL;
}
if (bnum != tnum)
rcsnum_free(bnum);
return (dlines);
}
void
rcs_annotate_getlines(RCSFILE *rfp, RCSNUM *frev, struct cvs_line ***alines)
{
size_t plen;
int i, nextroot;
RCSNUM *bnum;
struct rcs_branch *brp;
struct rcs_delta *rdp, *trdp;
u_char *patch;
struct cvs_line *line;
struct cvs_lines *dlines, *plines;
if (!RCSNUM_ISBRANCHREV(frev))
fatal("rcs_annotate_getlines: branch revision expected");
/* revision on branch, get the branch root */
nextroot = 2;
bnum = rcsnum_alloc();
rcsnum_cpy(frev, bnum, nextroot);
/*
* Going from HEAD to 1.1 enables the use of an array, which is
* much faster. Unfortunately this is not possible with branch
* revisions, so copy over our alines (array) into dlines (tailq).
*/
dlines = xcalloc(1, sizeof(*dlines));
TAILQ_INIT(&(dlines->l_lines));
line = xcalloc(1, sizeof(*line));
TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
for (i = 0; (*alines)[i] != NULL; i++) {
line = (*alines)[i];
line->l_lineno = i + 1;
TAILQ_INSERT_TAIL(&(dlines->l_lines), line, l_list);
}
rdp = rcs_findrev(rfp, bnum);
if (rdp == NULL)
fatal("failed to grab branch root revision");
do {
nextroot += 2;
rcsnum_cpy(frev, bnum, nextroot);
TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
for (i = 0; i < nextroot - 1; i++)
if (brp->rb_num->rn_id[i] != bnum->rn_id[i])
break;
if (i == nextroot - 1)
break;
}
if (brp == NULL)
fatal("expected branch not found on branch list");
if ((rdp = rcs_findrev(rfp, brp->rb_num)) == NULL)
fatal("failed to get delta for target rev");
for (;;) {
if (rdp->rd_next->rn_len != 0) {
trdp = rcs_findrev(rfp, rdp->rd_next);
if (trdp == NULL)
fatal("failed to grab next revision");
}
if (rdp->rd_tlen == 0) {
rcs_parse_deltatexts(rfp, rdp->rd_num);
if (rdp->rd_tlen == 0) {
if (!rcsnum_differ(rdp->rd_num, bnum))
break;
rdp = trdp;
continue;
}
}
plen = rdp->rd_tlen;
patch = rdp->rd_text;
plines = cvs_splitlines(patch, plen);
rcs_patch_lines(dlines, plines, NULL, rdp);
cvs_freelines(plines);
if (!rcsnum_differ(rdp->rd_num, bnum))
break;
rdp = trdp;
}
} while (rcsnum_differ(rdp->rd_num, frev));
if (bnum != frev)
rcsnum_free(bnum);
/*
* All lines have been parsed, now they must be copied over
* into alines (array) again.
*/
xfree(*alines);
i = 0;
TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
if (line->l_line != NULL)
i++;
}
*alines = xcalloc(i + 1, sizeof(struct cvs_line *));
(*alines)[i] = NULL;
i = 0;
TAILQ_FOREACH(line, &(dlines->l_lines), l_list) {
if (line->l_line != NULL)
(*alines)[i++] = line;
}
}
/*
* rcs_rev_getbuf()
*
* XXX: This is really really slow and should be avoided if at all possible!
*
* Get the entire contents of revision <rev> from the RCSFILE <rfp> and
* return it as a BUF pointer.
*/
BUF *
rcs_rev_getbuf(RCSFILE *rfp, RCSNUM *rev, int mode)
{
int expmode, expand;
struct rcs_delta *rdp;
struct cvs_lines *lines;
struct cvs_line *lp, *nlp;
BUF *bp;
expand = 0;
lines = rcs_rev_getlines(rfp, rev, NULL);
bp = cvs_buf_alloc(1024 * 16);
if (!(mode & RCS_KWEXP_NONE)) {
if (rfp->rf_expand != NULL)
expmode = rcs_kwexp_get(rfp);
else
expmode = RCS_KWEXP_DEFAULT;
if (!(expmode & RCS_KWEXP_NONE)) {
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
fatal("could not fetch revision");
expand = 1;
}
}
for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
nlp = TAILQ_NEXT(lp, l_list);
if (lp->l_line == NULL) {
lp = nlp;
continue;
}
if (expand)
rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
do {
cvs_buf_append(bp, lp->l_line, lp->l_len);
} while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
}
cvs_freelines(lines);
return (bp);
}
/*
* rcs_rev_write_fd()
*
* Write the entire contents of revision <frev> from the rcsfile <rfp> to
* file descriptor <fd>.
*/
void
rcs_rev_write_fd(RCSFILE *rfp, RCSNUM *rev, int _fd, int mode)
{
int fd;
FILE *fp;
size_t ret;
int expmode, expand;
struct rcs_delta *rdp;
struct cvs_lines *lines;
struct cvs_line *lp, *nlp;
extern int print_stdout;
expand = 0;
lines = rcs_rev_getlines(rfp, rev, NULL);
if (!(mode & RCS_KWEXP_NONE)) {
if (rfp->rf_expand != NULL)
expmode = rcs_kwexp_get(rfp);
else
expmode = RCS_KWEXP_DEFAULT;
if (!(expmode & RCS_KWEXP_NONE)) {
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
fatal("could not fetch revision");
expand = 1;
}
}
fd = dup(_fd);
if (fd == -1)
fatal("rcs_rev_write_fd: dup: %s", strerror(errno));
if ((fp = fdopen(fd, "w")) == NULL)
fatal("rcs_rev_write_fd: fdopen: %s", strerror(errno));
for (lp = TAILQ_FIRST(&lines->l_lines); lp != NULL;) {
nlp = TAILQ_NEXT(lp, l_list);
if (lp->l_line == NULL) {
lp = nlp;
continue;
}
if (expand)
rcs_kwexp_line(rfp->rf_path, rdp, lines, lp, expmode);
do {
/*
* Solely for the checkout and update -p options.
*/
if (cvs_server_active == 1 &&
(cvs_cmdop == CVS_OP_CHECKOUT ||
cvs_cmdop == CVS_OP_UPDATE) && print_stdout == 1) {
ret = fwrite("M ", 1, 2, fp);
if (ret != 2)
fatal("rcs_rev_write_fd: %s",
strerror(errno));
}
ret = fwrite(lp->l_line, 1, lp->l_len, fp);
if (ret != lp->l_len)
fatal("rcs_rev_write_fd: %s", strerror(errno));
} while ((lp = TAILQ_NEXT(lp, l_list)) != nlp);
}
cvs_freelines(lines);
(void)fclose(fp);
}
/*
* rcs_rev_write_stmp()
*
* Write the contents of the rev <rev> to a temporary file whose path is
* specified using <template> (see mkstemp(3)). NB. This function will modify
* <template>, as per mkstemp.
*/
int
rcs_rev_write_stmp(RCSFILE *rfp, RCSNUM *rev, char *template, int mode)
{
int fd;
if ((fd = mkstemp(template)) == -1)
fatal("mkstemp: `%s': %s", template, strerror(errno));
cvs_worklist_add(template, &temp_files);
rcs_rev_write_fd(rfp, rev, fd, mode);
if (lseek(fd, 0, SEEK_SET) < 0)
fatal("rcs_rev_write_stmp: lseek: %s", strerror(errno));
return (fd);
}
static void
rcs_kwexp_line(char *rcsfile, struct rcs_delta *rdp, struct cvs_lines *lines,
struct cvs_line *line, int mode)
{
BUF *tmpbuf;
int kwtype;
u_int j, found;
const u_char *c, *start, *fin, *end;
char *kwstr;
char expbuf[256], buf[256];
char *fmt;
size_t clen, kwlen, len, tlen;
kwtype = 0;
kwstr = NULL;
if (mode & RCS_KWEXP_OLD)
return;
len = line->l_len;
if (len == 0)
return;
c = line->l_line;
found = 0;
/* Final character in buffer. */
fin = c + len - 1;
/*
* Keyword formats:
* $Keyword$
* $Keyword: value$
*/
for (; c < fin; c++) {
if (*c != '$')
continue;
/* remember start of this possible keyword */
start = c;
/* first following character has to be alphanumeric */
c++;
if (!isalpha(*c)) {
c = start;
continue;
}
/* Number of characters between c and fin, inclusive. */
clen = fin - c + 1;
/* look for any matching keywords */
found = 0;
for (j = 0; j < RCS_NKWORDS; j++) {
kwlen = strlen(rcs_expkw[j].kw_str);
/*
* kwlen must be less than clen since clen
* includes either a terminating `$' or a `:'.
*/
if (kwlen < clen &&
memcmp(c, rcs_expkw[j].kw_str, kwlen) == 0 &&
(c[kwlen] == '$' || c[kwlen] == ':')) {
found = 1;
kwstr = rcs_expkw[j].kw_str;
kwtype = rcs_expkw[j].kw_type;
c += kwlen;
break;
}
}
if (found == 0 && cvs_tagname != NULL) {
kwlen = strlen(cvs_tagname);
if (kwlen < clen &&
memcmp(c, cvs_tagname, kwlen) == 0 &&
(c[kwlen] == '$' || c[kwlen] == ':')) {
found = 1;
kwstr = cvs_tagname;
kwtype = RCS_KW_ID;
c += kwlen;
}
}
/* unknown keyword, continue looking */
if (found == 0) {
c = start;
continue;
}
/*
* if the next character was ':' we need to look for
* an '$' before the end of the line to be sure it is
* in fact a keyword.
*/
if (*c == ':') {
for (; c <= fin; ++c) {
if (*c == '$' || *c == '\n')
break;
}
if (*c != '$') {
c = start;
continue;
}
}
end = c + 1;
/* start constructing the expansion */
expbuf[0] = '\0';
if (mode & RCS_KWEXP_NAME) {
if (strlcat(expbuf, "$", sizeof(expbuf)) >=
sizeof(expbuf) || strlcat(expbuf, kwstr,
sizeof(expbuf)) >= sizeof(expbuf))
fatal("rcs_kwexp_line: truncated");
if ((mode & RCS_KWEXP_VAL) &&
strlcat(expbuf, ": ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: truncated");
}
/*
* order matters because of RCS_KW_ID and
* RCS_KW_HEADER here
*/
if (mode & RCS_KWEXP_VAL) {
if (kwtype & RCS_KW_RCSFILE) {
if (!(kwtype & RCS_KW_FULLPATH))
(void)strlcat(expbuf, basename(rcsfile),
sizeof(expbuf));
else
(void)strlcat(expbuf, rcsfile,
sizeof(expbuf));
if (strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: truncated");
}
if (kwtype & RCS_KW_REVISION) {
rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
if (strlcat(buf, " ", sizeof(buf)) >=
sizeof(buf) || strlcat(expbuf, buf,
sizeof(expbuf)) >= sizeof(buf))
fatal("rcs_kwexp_line: truncated");
}
if (kwtype & RCS_KW_DATE) {
fmt = "%Y/%m/%d %H:%M:%S ";
if (strftime(buf, sizeof(buf), fmt,
&rdp->rd_date) == 0)
fatal("rcs_kwexp_line: strftime "
"failure");
if (strlcat(expbuf, buf, sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
if (kwtype & RCS_KW_MDOCDATE) {
/*
* Do not prepend ' ' for a single
* digit, %e would do so and there is
* no better format for strftime().
*/
if (rdp->rd_date.tm_mday < 10)
fmt = "%B%e %Y ";
else
fmt = "%B %e %Y ";
if (strftime(buf, sizeof(buf), fmt,
&rdp->rd_date) == 0)
fatal("rcs_kwexp_line: strftime "
"failure");
if (strlcat(expbuf, buf, sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
if (kwtype & RCS_KW_AUTHOR) {
if (strlcat(expbuf, rdp->rd_author,
sizeof(expbuf)) >= sizeof(expbuf) ||
strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
if (kwtype & RCS_KW_STATE) {
if (strlcat(expbuf, rdp->rd_state,
sizeof(expbuf)) >= sizeof(expbuf) ||
strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
/* order does not matter anymore below */
if (kwtype & RCS_KW_LOG) {
char linebuf[256];
struct cvs_line *cur, *lp;
char *logp, *l_line, *prefix, *q, *sprefix;
size_t i;
/* Log line */
if (!(kwtype & RCS_KW_FULLPATH))
(void)strlcat(expbuf,
basename(rcsfile), sizeof(expbuf));
else
(void)strlcat(expbuf, rcsfile,
sizeof(expbuf));
if (strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
cur = line;
/* copy rdp->rd_log for strsep */
logp = xstrdup(rdp->rd_log);
/* copy our prefix for later processing */
prefix = xmalloc(start - line->l_line + 1);
memcpy(prefix, line->l_line,
start - line->l_line);
prefix[start - line->l_line] = '\0';
/* copy also prefix without trailing blanks. */
sprefix = xstrdup(prefix);
for (i = strlen(sprefix); i > 0 &&
sprefix[i - 1] == ' '; i--)
sprefix[i - 1] = '\0';
/* new line: revision + date + author */
linebuf[0] = '\0';
if (strlcat(linebuf, "Revision ",
sizeof(linebuf)) >= sizeof(linebuf))
fatal("rcs_kwexp_line: truncated");
rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
if (strlcat(linebuf, buf, sizeof(linebuf))
>= sizeof(buf))
fatal("rcs_kwexp_line: truncated");
fmt = " %Y/%m/%d %H:%M:%S ";
if (strftime(buf, sizeof(buf), fmt,
&rdp->rd_date) == 0)
fatal("rcs_kwexp_line: strftime "
"failure");
if (strlcat(linebuf, buf, sizeof(linebuf))
>= sizeof(linebuf))
fatal("rcs_kwexp_line: string "
"truncated");
if (strlcat(linebuf, rdp->rd_author,
sizeof(linebuf)) >= sizeof(linebuf))
fatal("rcs_kwexp_line: string "
"truncated");
lp = xcalloc(1, sizeof(*lp));
xasprintf(&(lp->l_line), "%s%s\n",
prefix, linebuf);
lp->l_len = strlen(lp->l_line);
TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
l_list);
cur = lp;
/* Log message */
q = logp;
while ((l_line = strsep(&q, "\n")) != NULL &&
q != NULL) {
lp = xcalloc(1, sizeof(*lp));
if (l_line[0] == '\0') {
xasprintf(&(lp->l_line), "%s\n",
sprefix);
} else {
xasprintf(&(lp->l_line),
"%s%s\n", prefix, l_line);
}
lp->l_len = strlen(lp->l_line);
TAILQ_INSERT_AFTER(&(lines->l_lines),
cur, lp, l_list);
cur = lp;
}
xfree(logp);
/*
* This is just another hairy mess, but it must
* be done: All characters behind Log will be
* written in a new line next to log messages.
* But that's not enough, we have to strip all
* trailing whitespaces of our prefix.
*/
lp = xcalloc(1, sizeof(*lp));
lp->l_line = xcalloc(strlen(sprefix) +
line->l_line + line->l_len - end + 1, 1);
strlcpy(lp->l_line, sprefix,
strlen(sprefix) + 1);
memcpy(lp->l_line + strlen(sprefix),
end, line->l_line + line->l_len - end);
lp->l_len = strlen(lp->l_line);
TAILQ_INSERT_AFTER(&(lines->l_lines), cur, lp,
l_list);
cur = lp;
end = line->l_line + line->l_len - 1;
xfree(prefix);
xfree(sprefix);
}
if (kwtype & RCS_KW_SOURCE) {
if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >=
sizeof(expbuf) || strlcat(expbuf, " ",
sizeof(expbuf)) >= sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
if (kwtype & RCS_KW_NAME)
if (strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
if (kwtype & RCS_KW_LOCKER)
if (strlcat(expbuf, " ", sizeof(expbuf)) >=
sizeof(expbuf))
fatal("rcs_kwexp_line: string "
"truncated");
}
/* end the expansion */
if (mode & RCS_KWEXP_NAME)
if (strlcat(expbuf, "$",
sizeof(expbuf)) >= sizeof(expbuf))
fatal("rcs_kwexp_line: truncated");
/* Concatenate everything together. */
tmpbuf = cvs_buf_alloc(len + strlen(expbuf));
/* Append everything before keyword. */
cvs_buf_append(tmpbuf, line->l_line,
start - line->l_line);
/* Append keyword. */
cvs_buf_puts(tmpbuf, expbuf);
/* Point c to end of keyword. */
tlen = cvs_buf_len(tmpbuf) - 1;
/* Append everything after keyword. */
cvs_buf_append(tmpbuf, end,
line->l_line + line->l_len - end);
c = cvs_buf_get(tmpbuf) + tlen;
/* Point fin to end of data. */
fin = cvs_buf_get(tmpbuf) + cvs_buf_len(tmpbuf) - 1;
/* Recalculate new length. */
len = cvs_buf_len(tmpbuf);
/* tmpbuf is now ready, convert to string */
if (line->l_needsfree)
xfree(line->l_line);
line->l_len = len;
line->l_line = cvs_buf_release(tmpbuf);
line->l_needsfree = 1;
}
}
/* rcs_translate_tag() */
RCSNUM *
rcs_translate_tag(const char *revstr, RCSFILE *rfp)
{
int follow;
time_t deltatime;
char branch[CVS_REV_BUFSZ];
RCSNUM *brev, *frev, *rev, *rrev;
struct rcs_delta *rdp, *trdp;
time_t cdate;
brev = frev = rrev = NULL;
if (revstr == NULL) {
if (rfp->rf_branch != NULL) {
rcsnum_tostr(rfp->rf_branch, branch, sizeof(branch));
revstr = branch;
} else {
revstr = RCS_HEAD_BRANCH;
}
}
if ((rev = rcs_get_revision(revstr, rfp)) == NULL)
return (NULL);
if ((rdp = rcs_findrev(rfp, rev)) == NULL)
return (NULL);
/* let's see if we must follow a branch */
if (!strcmp(revstr, RCS_HEAD_BRANCH))
follow = 1;
else {
frev = rcs_sym_getrev(rfp, revstr);
if (frev == NULL)
frev = rrev = rcsnum_parse(revstr);
brev = rcsnum_alloc();
rcsnum_cpy(rev, brev, rev->rn_len - 1);
if (frev != NULL && RCSNUM_ISBRANCH(frev) &&
!rcsnum_cmp(frev, brev, 0)) {
follow = 1;
} else
follow = 0;
rcsnum_free(brev);
if (rrev != NULL)
rcsnum_free(rrev);
}
if (cvs_specified_date != -1)
cdate = cvs_specified_date;
else
cdate = cvs_directory_date;
if (cdate == -1) {
/* XXX */
if (rev->rn_len < 4 || !follow) {
return (rev);
}
/* Find the latest delta on that branch */
rcsnum_free(rev);
for (;;) {
if (rdp->rd_next->rn_len == 0)
break;
if ((rdp = rcs_findrev(rfp, rdp->rd_next)) == NULL)
fatal("rcs_translate_tag: could not fetch "
"branch delta");
}
rev = rcsnum_alloc();
rcsnum_cpy(rdp->rd_num, rev, 0);
return (rev);
}
if (frev != NULL)
rcsnum_tostr(frev, branch, sizeof(branch));
if (frev != NULL) {
brev = rcsnum_revtobr(frev);
brev->rn_len = rev->rn_len - 1;
}
rcsnum_free(rev);
do {
deltatime = timelocal(&(rdp->rd_date));
if (RCSNUM_ISBRANCHREV(rdp->rd_num)) {
if (deltatime > cdate) {
trdp = TAILQ_PREV(rdp, rcs_dlist, rd_list);
if (trdp == NULL)
trdp = rdp;
if (trdp->rd_num->rn_len != rdp->rd_num->rn_len)
return (NULL);
rev = rcsnum_alloc();
rcsnum_cpy(trdp->rd_num, rev, 0);
return (rev);
}
if (rdp->rd_next->rn_len == 0) {
rev = rcsnum_alloc();
rcsnum_cpy(rdp->rd_num, rev, 0);
return (rev);
}
} else {
if (deltatime < cdate) {
rev = rcsnum_alloc();
rcsnum_cpy(rdp->rd_num, rev, 0);
return (rev);
}
}
if (follow && rdp->rd_next->rn_len != 0) {
if (brev != NULL && !rcsnum_cmp(brev, rdp->rd_num, 0))
break;
trdp = rcs_findrev(rfp, rdp->rd_next);
if (trdp == NULL)
fatal("failed to grab next revision");
rdp = trdp;
} else
follow = 0;
} while (follow);
return (NULL);
}
|