summaryrefslogtreecommitdiffstats
path: root/src/bldprogs/scm.cpp
blob: 0b0e603f9e1c21a648b946af93eff5c1e0000267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
/* $Id: scm.cpp $ */
/** @file
 * IPRT Testcase / Tool - Source Code Massager.
 */

/*
 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/dir.h>
#include <iprt/env.h>
#include <iprt/file.h>
#include <iprt/err.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/string.h>

#include "scm.h"
#include "scmdiff.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** The name of the settings files. */
#define SCM_SETTINGS_FILENAME           ".scm-settings"


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * Option identifiers.
 *
 * @note    The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
 *          clear.  So, the option setting a flag (boolean) will have an even
 *          number and the one clearing it will have an odd number.
 * @note    Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
 */
typedef enum SCMOPT
{
    SCMOPT_CONVERT_EOL = 10000,
    SCMOPT_NO_CONVERT_EOL,
    SCMOPT_CONVERT_TABS,
    SCMOPT_NO_CONVERT_TABS,
    SCMOPT_FORCE_FINAL_EOL,
    SCMOPT_NO_FORCE_FINAL_EOL,
    SCMOPT_FORCE_TRAILING_LINE,
    SCMOPT_NO_FORCE_TRAILING_LINE,
    SCMOPT_STRIP_TRAILING_BLANKS,
    SCMOPT_NO_STRIP_TRAILING_BLANKS,
    SCMOPT_STRIP_TRAILING_LINES,
    SCMOPT_NO_STRIP_TRAILING_LINES,
    SCMOPT_FIX_FLOWER_BOX_MARKERS,
    SCMOPT_NO_FIX_FLOWER_BOX_MARKERS,
    SCMOPT_FIX_HEADER_GUARDS,
    SCMOPT_NO_FIX_HEADER_GUARDS,
    SCMOPT_PRAGMA_ONCE,
    SCMOPT_NO_PRAGMA_ONCE,
    SCMOPT_FIX_HEADER_GUARD_ENDIF,
    SCMOPT_NO_FIX_HEADER_GUARD_ENDIF,
    SCMOPT_ENDIF_GUARD_COMMENT,
    SCMOPT_NO_ENDIF_GUARD_COMMENT,
    SCMOPT_GUARD_PREFIX,
    SCMOPT_GUARD_RELATIVE_TO_DIR,
    SCMOPT_FIX_TODOS,
    SCMOPT_NO_FIX_TODOS,
    SCMOPT_FIX_ERR_H,
    SCMOPT_NO_FIX_ERR_H,
    SCMOPT_ONLY_GUEST_HOST_PAGE,
    SCMOPT_NO_ASM_MEM_PAGE_USE,
    SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE,
    SCMOPT_NO_PAGE_RESTRICTIONS,
    SCMOPT_NO_RC_USE,
    SCMOPT_UNRESTRICTED_RC_USE,
    SCMOPT_UPDATE_COPYRIGHT_YEAR,
    SCMOPT_NO_UPDATE_COPYRIGHT_YEAR,
    SCMOPT_EXTERNAL_COPYRIGHT,
    SCMOPT_NO_EXTERNAL_COPYRIGHT,
    SCMOPT_NO_UPDATE_LICENSE,
    SCMOPT_LICENSE_OSE_GPL,
    SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL,
    SCMOPT_LICENSE_OSE_CDDL,
    SCMOPT_LICENSE_LGPL,
    SCMOPT_LICENSE_MIT,
    SCMOPT_LICENSE_BASED_ON_MIT,
    SCMOPT_LGPL_DISCLAIMER,
    SCMOPT_NO_LGPL_DISCLAIMER,
    SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS,
    SCMOPT_ONLY_SVN_DIRS,
    SCMOPT_NOT_ONLY_SVN_DIRS,
    SCMOPT_ONLY_SVN_FILES,
    SCMOPT_NOT_ONLY_SVN_FILES,
    SCMOPT_SET_SVN_EOL,
    SCMOPT_DONT_SET_SVN_EOL,
    SCMOPT_SET_SVN_EXECUTABLE,
    SCMOPT_DONT_SET_SVN_EXECUTABLE,
    SCMOPT_SET_SVN_KEYWORDS,
    SCMOPT_DONT_SET_SVN_KEYWORDS,
    SCMOPT_SKIP_SVN_SYNC_PROCESS,
    SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS,
    SCMOPT_SKIP_UNICODE_CHECKS,
    SCMOPT_DONT_SKIP_UNICODE_CHECKS,
    SCMOPT_TAB_SIZE,
    SCMOPT_WIDTH,
    SCMOPT_FILTER_OUT_DIRS,
    SCMOPT_FILTER_FILES,
    SCMOPT_FILTER_OUT_FILES,
    SCMOPT_TREAT_AS,
    SCMOPT_ADD_ACTION,
    SCMOPT_DEL_ACTION,
    SCMOPT_LAST_SETTINGS = SCMOPT_DEL_ACTION,
    //
    SCMOPT_CHECK_RUN,
    SCMOPT_DIFF_IGNORE_EOL,
    SCMOPT_DIFF_NO_IGNORE_EOL,
    SCMOPT_DIFF_IGNORE_SPACE,
    SCMOPT_DIFF_NO_IGNORE_SPACE,
    SCMOPT_DIFF_IGNORE_LEADING_SPACE,
    SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
    SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
    SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
    SCMOPT_DIFF_SPECIAL_CHARS,
    SCMOPT_DIFF_NO_SPECIAL_CHARS,
    SCMOPT_HELP_CONFIG,
    SCMOPT_HELP_ACTIONS,
    SCMOPT_END
} SCMOPT;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
const char          g_szTabSpaces[16+1]     = "                ";
const char          g_szAsterisks[255+1]    =
"****************************************************************************************************"
"****************************************************************************************************"
"*******************************************************";
const char          g_szSpaces[255+1]       =
"                                                                                                    "
"                                                                                                    "
"                                                       ";
static const char   g_szProgName[]          = "scm";
static const char  *g_pszChangedSuff        = "";
static bool         g_fDryRun               = true;
static bool         g_fDiffSpecialChars     = true;
static bool         g_fDiffIgnoreEol        = false;
static bool         g_fDiffIgnoreLeadingWS  = false;
static bool         g_fDiffIgnoreTrailingWS = false;
static int          g_iVerbosity            = 2;//99; //0;
uint32_t            g_uYear                 = 0; /**< The current year. */
/** @name Statistics
 * @{ */
static uint32_t     g_cDirsProcessed        = 0;
static uint32_t     g_cFilesProcessed       = 0;
static uint32_t     g_cFilesModified        = 0;
static uint32_t     g_cFilesSkipped         = 0;
static uint32_t     g_cFilesNotInSvn        = 0;
static uint32_t     g_cFilesNoRewriters     = 0;
static uint32_t     g_cFilesBinaries        = 0;
static uint32_t     g_cFilesRequiringManualFixing = 0;
/** @} */

/** The global settings. */
static SCMSETTINGSBASE const g_Defaults =
{
    /* .fConvertEol = */                            true,
    /* .fConvertTabs = */                           true,
    /* .fForceFinalEol = */                         true,
    /* .fForceTrailingLine = */                     false,
    /* .fStripTrailingBlanks = */                   true,
    /* .fStripTrailingLines = */                    true,
    /* .fFixFlowerBoxMarkers = */                   true,
    /* .cMinBlankLinesBeforeFlowerBoxMakers = */    2,
    /* .fFixHeaderGuards = */                       true,
    /* .fPragmaOnce = */                            true,
    /* .fFixHeaderGuardEndif = */                   true,
    /* .fEndifGuardComment = */                     true,
    /* .pszGuardPrefix = */                         (char *)"VBOX_INCLUDED_SRC_",
    /* .pszGuardRelativeToDir = */                  (char *)"{parent}",
    /* .fFixTodos = */                              true,
    /* .fFixErrH = */                               true,
    /* .fOnlyGuestHostPage = */                     false,
    /* .fNoASMMemPageUse = */                       false,
    /* .fOnlyHrcVrcInsteadOfRc */                   false,
    /* .fUpdateCopyrightYear = */                   false,
    /* .fExternalCopyright = */                     false,
    /* .fLgplDisclaimer = */                        false,
    /* .enmUpdateLicense = */                       kScmLicense_OseGpl,
    /* .fOnlySvnFiles = */                          false,
    /* .fOnlySvnDirs = */                           false,
    /* .fSetSvnEol = */                             false,
    /* .fSetSvnExecutable = */                      false,
    /* .fSetSvnKeywords = */                        false,
    /* .fSkipSvnSyncProcess = */                    false,
    /* .fSkipUnicodeChecks = */                     false,
    /* .cchTab = */                                 8,
    /* .cchWidth = */                               130,
    /* .fFreeTreatAs = */                           false,
    /* .pTreatAs = */                               NULL,
    /* .pszFilterFiles = */                         (char *)"",
    /* .pszFilterOutFiles = */                      (char *)"*.exe|*.com|20*-*-*.log",
    /* .pszFilterOutDirs = */                       (char *)".svn|.hg|.git|CVS",
};

/** Option definitions for the base settings. */
static RTGETOPTDEF  g_aScmOpts[] =
{
    /* rewriters */
    { "--convert-eol",                      SCMOPT_CONVERT_EOL,                     RTGETOPT_REQ_NOTHING },
    { "--no-convert-eol",                   SCMOPT_NO_CONVERT_EOL,                  RTGETOPT_REQ_NOTHING },
    { "--convert-tabs",                     SCMOPT_CONVERT_TABS,                    RTGETOPT_REQ_NOTHING },
    { "--no-convert-tabs",                  SCMOPT_NO_CONVERT_TABS,                 RTGETOPT_REQ_NOTHING },
    { "--force-final-eol",                  SCMOPT_FORCE_FINAL_EOL,                 RTGETOPT_REQ_NOTHING },
    { "--no-force-final-eol",               SCMOPT_NO_FORCE_FINAL_EOL,              RTGETOPT_REQ_NOTHING },
    { "--force-trailing-line",              SCMOPT_FORCE_TRAILING_LINE,             RTGETOPT_REQ_NOTHING },
    { "--no-force-trailing-line",           SCMOPT_NO_FORCE_TRAILING_LINE,          RTGETOPT_REQ_NOTHING },
    { "--strip-trailing-blanks",            SCMOPT_STRIP_TRAILING_BLANKS,           RTGETOPT_REQ_NOTHING },
    { "--no-strip-trailing-blanks",         SCMOPT_NO_STRIP_TRAILING_BLANKS,        RTGETOPT_REQ_NOTHING },
    { "--strip-trailing-lines",             SCMOPT_STRIP_TRAILING_LINES,            RTGETOPT_REQ_NOTHING },
    { "--strip-no-trailing-lines",          SCMOPT_NO_STRIP_TRAILING_LINES,         RTGETOPT_REQ_NOTHING },
    { "--min-blank-lines-before-flower-box-makers", SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS,  RTGETOPT_REQ_UINT8 },
    { "--fix-flower-box-markers",           SCMOPT_FIX_FLOWER_BOX_MARKERS,          RTGETOPT_REQ_NOTHING },
    { "--no-fix-flower-box-markers",        SCMOPT_NO_FIX_FLOWER_BOX_MARKERS,       RTGETOPT_REQ_NOTHING },
    { "--fix-header-guards",                SCMOPT_FIX_HEADER_GUARDS,               RTGETOPT_REQ_NOTHING },
    { "--no-fix-header-guards",             SCMOPT_NO_FIX_HEADER_GUARDS,            RTGETOPT_REQ_NOTHING },
    { "--pragma-once",                      SCMOPT_PRAGMA_ONCE,                     RTGETOPT_REQ_NOTHING },
    { "--no-pragma-once",                   SCMOPT_NO_PRAGMA_ONCE,                  RTGETOPT_REQ_NOTHING },
    { "--fix-header-guard-endif",           SCMOPT_FIX_HEADER_GUARD_ENDIF,          RTGETOPT_REQ_NOTHING },
    { "--no-fix-header-guard-endif",        SCMOPT_NO_FIX_HEADER_GUARD_ENDIF,       RTGETOPT_REQ_NOTHING },
    { "--endif-guard-comment",              SCMOPT_ENDIF_GUARD_COMMENT,             RTGETOPT_REQ_NOTHING },
    { "--no-endif-guard-comment",           SCMOPT_NO_ENDIF_GUARD_COMMENT,          RTGETOPT_REQ_NOTHING },
    { "--guard-prefix",                     SCMOPT_GUARD_PREFIX,                    RTGETOPT_REQ_STRING },
    { "--guard-relative-to-dir",            SCMOPT_GUARD_RELATIVE_TO_DIR,           RTGETOPT_REQ_STRING },
    { "--fix-todos",                        SCMOPT_FIX_TODOS,                       RTGETOPT_REQ_NOTHING },
    { "--no-fix-todos",                     SCMOPT_NO_FIX_TODOS,                    RTGETOPT_REQ_NOTHING },
    { "--fix-err-h",                        SCMOPT_FIX_ERR_H,                       RTGETOPT_REQ_NOTHING },
    { "--no-fix-err-h",                     SCMOPT_NO_FIX_ERR_H,                    RTGETOPT_REQ_NOTHING },
    { "--only-guest-host-page",             SCMOPT_ONLY_GUEST_HOST_PAGE,            RTGETOPT_REQ_NOTHING },
    { "--no-page-restrictions",             SCMOPT_NO_PAGE_RESTRICTIONS,            RTGETOPT_REQ_NOTHING },
    { "--no-ASMMemPage-use",                SCMOPT_NO_ASM_MEM_PAGE_USE,             RTGETOPT_REQ_NOTHING },
    { "--unrestricted-ASMMemPage-use",      SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE,   RTGETOPT_REQ_NOTHING },
    { "--no-rc-use",                        SCMOPT_NO_RC_USE,                       RTGETOPT_REQ_NOTHING },
    { "--unrestricted-rc-use",              SCMOPT_UNRESTRICTED_RC_USE,             RTGETOPT_REQ_NOTHING },
    { "--update-copyright-year",            SCMOPT_UPDATE_COPYRIGHT_YEAR,           RTGETOPT_REQ_NOTHING },
    { "--no-update-copyright-year",         SCMOPT_NO_UPDATE_COPYRIGHT_YEAR,        RTGETOPT_REQ_NOTHING },
    { "--external-copyright",               SCMOPT_EXTERNAL_COPYRIGHT,              RTGETOPT_REQ_NOTHING },
    { "--no-external-copyright",            SCMOPT_NO_EXTERNAL_COPYRIGHT,           RTGETOPT_REQ_NOTHING },
    { "--no-update-license",                SCMOPT_NO_UPDATE_LICENSE,               RTGETOPT_REQ_NOTHING },
    { "--license-ose-gpl",                  SCMOPT_LICENSE_OSE_GPL,                 RTGETOPT_REQ_NOTHING },
    { "--license-ose-dual",                 SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL,       RTGETOPT_REQ_NOTHING },
    { "--license-ose-cddl",                 SCMOPT_LICENSE_OSE_CDDL,                RTGETOPT_REQ_NOTHING },
    { "--license-lgpl",                     SCMOPT_LICENSE_LGPL,                    RTGETOPT_REQ_NOTHING },
    { "--license-mit",                      SCMOPT_LICENSE_MIT,                     RTGETOPT_REQ_NOTHING },
    { "--license-based-on-mit",             SCMOPT_LICENSE_BASED_ON_MIT,            RTGETOPT_REQ_NOTHING },
    { "--lgpl-disclaimer",                  SCMOPT_LGPL_DISCLAIMER,                 RTGETOPT_REQ_NOTHING },
    { "--no-lgpl-disclaimer",               SCMOPT_NO_LGPL_DISCLAIMER,              RTGETOPT_REQ_NOTHING },
    { "--set-svn-eol",                      SCMOPT_SET_SVN_EOL,                     RTGETOPT_REQ_NOTHING },
    { "--dont-set-svn-eol",                 SCMOPT_DONT_SET_SVN_EOL,                RTGETOPT_REQ_NOTHING },
    { "--set-svn-executable",               SCMOPT_SET_SVN_EXECUTABLE,              RTGETOPT_REQ_NOTHING },
    { "--dont-set-svn-executable",          SCMOPT_DONT_SET_SVN_EXECUTABLE,         RTGETOPT_REQ_NOTHING },
    { "--set-svn-keywords",                 SCMOPT_SET_SVN_KEYWORDS,                RTGETOPT_REQ_NOTHING },
    { "--dont-set-svn-keywords",            SCMOPT_DONT_SET_SVN_KEYWORDS,           RTGETOPT_REQ_NOTHING },
    { "--skip-svn-sync-process",            SCMOPT_SKIP_SVN_SYNC_PROCESS,           RTGETOPT_REQ_NOTHING },
    { "--dont-skip-svn-sync-process",       SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS,      RTGETOPT_REQ_NOTHING },
    { "--skip-unicode-checks",              SCMOPT_SKIP_UNICODE_CHECKS,             RTGETOPT_REQ_NOTHING },
    { "--dont-skip-unicode-checks",         SCMOPT_DONT_SKIP_UNICODE_CHECKS,        RTGETOPT_REQ_NOTHING },
    { "--tab-size",                         SCMOPT_TAB_SIZE,                        RTGETOPT_REQ_UINT8   },
    { "--width",                            SCMOPT_WIDTH,                           RTGETOPT_REQ_UINT8   },

    /* input selection */
    { "--only-svn-dirs",                    SCMOPT_ONLY_SVN_DIRS,                   RTGETOPT_REQ_NOTHING },
    { "--not-only-svn-dirs",                SCMOPT_NOT_ONLY_SVN_DIRS,               RTGETOPT_REQ_NOTHING },
    { "--only-svn-files",                   SCMOPT_ONLY_SVN_FILES,                  RTGETOPT_REQ_NOTHING },
    { "--not-only-svn-files",               SCMOPT_NOT_ONLY_SVN_FILES,              RTGETOPT_REQ_NOTHING },
    { "--filter-out-dirs",                  SCMOPT_FILTER_OUT_DIRS,                 RTGETOPT_REQ_STRING  },
    { "--filter-files",                     SCMOPT_FILTER_FILES,                    RTGETOPT_REQ_STRING  },
    { "--filter-out-files",                 SCMOPT_FILTER_OUT_FILES,                RTGETOPT_REQ_STRING  },

    /* rewriter selection */
    { "--treat-as",                         SCMOPT_TREAT_AS,                        RTGETOPT_REQ_STRING  },
    { "--add-action",                       SCMOPT_ADD_ACTION,                      RTGETOPT_REQ_STRING  },
    { "--del-action",                       SCMOPT_DEL_ACTION,                      RTGETOPT_REQ_STRING  },

    /* Additional help */
    { "--help-config",                      SCMOPT_HELP_CONFIG,                     RTGETOPT_REQ_NOTHING },
    { "--help-actions",                     SCMOPT_HELP_ACTIONS,                    RTGETOPT_REQ_NOTHING },
};

/** Consider files matching the following patterns (base names only). */
static const char  *g_pszFileFilter         = NULL;

/* The rewriter configuration. */
#define SCM_REWRITER_CFG(a_Global, a_szName, fnRewriter) static const SCMREWRITERCFG a_Global = { &fnRewriter, a_szName }
SCM_REWRITER_CFG(g_StripTrailingBlanks,             "strip-trailing-blanks",        rewrite_StripTrailingBlanks);
SCM_REWRITER_CFG(g_ExpandTabs,                      "expand-tabs",                  rewrite_ExpandTabs);
SCM_REWRITER_CFG(g_ForceNativeEol,                  "force-native-eol",             rewrite_ForceNativeEol);
SCM_REWRITER_CFG(g_ForceLF,                         "force-lf",                     rewrite_ForceLF);
SCM_REWRITER_CFG(g_ForceCRLF,                       "force-crlf",                   rewrite_ForceCRLF);
SCM_REWRITER_CFG(g_AdjustTrailingLines,             "adjust-trailing-lines",        rewrite_AdjustTrailingLines);
SCM_REWRITER_CFG(g_SvnNoExecutable,                 "svn-no-executable",            rewrite_SvnNoExecutable);
SCM_REWRITER_CFG(g_SvnNoKeywords,                   "svn-no-keywords",              rewrite_SvnNoKeywords);
SCM_REWRITER_CFG(g_SvnNoEolStyle,                   "svn-no-eol-style",             rewrite_SvnNoEolStyle);
SCM_REWRITER_CFG(g_SvnBinary,                       "svn-binary",                   rewrite_SvnBinary);
SCM_REWRITER_CFG(g_SvnKeywords,                     "svn-keywords",                 rewrite_SvnKeywords);
SCM_REWRITER_CFG(g_SvnSyncProcess,                  "svn-sync-process",             rewrite_SvnSyncProcess);
SCM_REWRITER_CFG(g_UnicodeChecks,                   "unicode-checks",               rewrite_UnicodeChecks);
SCM_REWRITER_CFG(g_PageChecks,                      "page-checks",                  rewrite_PageChecks);
SCM_REWRITER_CFG(g_ForceHrcVrcInsteadOfRc,          "force-hrc-vrc-no-rc",          rewrite_ForceHrcVrcInsteadOfRc);
SCM_REWRITER_CFG(g_Copyright_CstyleComment,         "copyright-c-style",            rewrite_Copyright_CstyleComment);
SCM_REWRITER_CFG(g_Copyright_HashComment,           "copyright-hash-style",         rewrite_Copyright_HashComment);
SCM_REWRITER_CFG(g_Copyright_PythonComment,         "copyright-python-style",       rewrite_Copyright_PythonComment);
SCM_REWRITER_CFG(g_Copyright_RemComment,            "copyright-rem-style",          rewrite_Copyright_RemComment);
SCM_REWRITER_CFG(g_Copyright_SemicolonComment,      "copyright-semicolon-style",    rewrite_Copyright_SemicolonComment);
SCM_REWRITER_CFG(g_Copyright_SqlComment,            "copyright-sql-style",          rewrite_Copyright_SqlComment);
SCM_REWRITER_CFG(g_Copyright_TickComment,           "copyright-tick-style",         rewrite_Copyright_TickComment);
SCM_REWRITER_CFG(g_Copyright_XmlComment,            "copyright-xml-style",          rewrite_Copyright_XmlComment);
SCM_REWRITER_CFG(g_Makefile_kup,                    "makefile-kup",                 rewrite_Makefile_kup);
SCM_REWRITER_CFG(g_Makefile_kmk,                    "makefile-kmk",                 rewrite_Makefile_kmk);
SCM_REWRITER_CFG(g_FixFlowerBoxMarkers,             "fix-flower-boxes",             rewrite_FixFlowerBoxMarkers);
SCM_REWRITER_CFG(g_FixHeaderGuards,                 "fix-header-guard",             rewrite_FixHeaderGuards);
SCM_REWRITER_CFG(g_Fix_C_and_CPP_Todos,             "fix-c-todos",                  rewrite_Fix_C_and_CPP_Todos);
SCM_REWRITER_CFG(g_Fix_Err_H,                       "fix-err-h",                    rewrite_Fix_Err_H);
SCM_REWRITER_CFG(g_C_and_CPP,                       "c-and-cpp",                    rewrite_C_and_CPP);

/** The rewriter actions. */
static PCSCMREWRITERCFG const g_papRewriterActions[] =
{
    &g_StripTrailingBlanks,
    &g_ExpandTabs,
    &g_ForceNativeEol,
    &g_ForceLF,
    &g_ForceCRLF,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnNoKeywords,
    &g_SvnNoEolStyle,
    &g_SvnBinary,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_Copyright_CstyleComment,
    &g_Copyright_HashComment,
    &g_Copyright_PythonComment,
    &g_Copyright_RemComment,
    &g_Copyright_SemicolonComment,
    &g_Copyright_SqlComment,
    &g_Copyright_TickComment,
    &g_Makefile_kup,
    &g_Makefile_kmk,
    &g_FixFlowerBoxMarkers,
    &g_FixHeaderGuards,
    &g_Fix_C_and_CPP_Todos,
    &g_Fix_Err_H,
    &g_UnicodeChecks,
    &g_PageChecks,
    &g_ForceHrcVrcInsteadOfRc,
    &g_C_and_CPP,
};


static PCSCMREWRITERCFG const g_apRewritersFor_Makefile_kup[] =
{
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Makefile_kup
};

static PCSCMREWRITERCFG const g_apRewritersFor_Makefile_kmk[] =
{
    &g_ForceNativeEol,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
    &g_Makefile_kmk
};

static PCSCMREWRITERCFG const g_apRewritersFor_OtherMakefiles[] =
{
    &g_ForceNativeEol,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_C_and_CPP[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_PageChecks,
    &g_ForceHrcVrcInsteadOfRc,
    &g_Copyright_CstyleComment,
    &g_FixFlowerBoxMarkers,
    &g_Fix_C_and_CPP_Todos,
    &g_Fix_Err_H,
    &g_C_and_CPP,
};

static PCSCMREWRITERCFG const g_apRewritersFor_H_and_HPP[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_PageChecks,
    &g_ForceHrcVrcInsteadOfRc,
    &g_Copyright_CstyleComment,
    /// @todo &g_FixFlowerBoxMarkers,
    &g_FixHeaderGuards,
    &g_C_and_CPP
};

static PCSCMREWRITERCFG const g_apRewritersFor_RC[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_CstyleComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_DTrace[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_CstyleComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_DSL[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_CstyleComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_ASM[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SemicolonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_DEF[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SemicolonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_ShellScripts[] =
{
    &g_ForceLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_BatchFiles[] =
{
    &g_ForceCRLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_RemComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_BasicScripts[] =
{
    &g_ForceCRLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_TickComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_SedScripts[] =
{
    &g_ForceLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Python[] =
{
    /** @todo &g_ForceLFIfExecutable */
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_PythonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Perl[] =
{
    /** @todo &g_ForceLFIfExecutable */
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_DriverInfFiles[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SemicolonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_NsisFiles[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SemicolonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Java[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_CstyleComment,
    &g_FixFlowerBoxMarkers,
    &g_Fix_C_and_CPP_Todos,
};

static PCSCMREWRITERCFG const g_apRewritersFor_ScmSettings[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Images[] =
{
    &g_SvnNoExecutable,
    &g_SvnBinary,
    &g_SvnSyncProcess,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Xslt[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_XmlComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Xml[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_XmlComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_Wix[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_XmlComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_QtProject[] =
{
    &g_ForceNativeEol,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_QtResourceFiles[] =
{
    &g_ForceNativeEol,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    /** @todo figure out copyright for Qt resource XML files. */
};

static PCSCMREWRITERCFG const g_apRewritersFor_QtTranslations[] =
{
    &g_ForceNativeEol,
    &g_SvnNoExecutable,
};

static PCSCMREWRITERCFG const g_apRewritersFor_QtUiFiles[] =
{
    &g_ForceNativeEol,
    &g_SvnNoExecutable,
    &g_SvnKeywords,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    /** @todo copyright is in an XML 'comment' element. */
};

static PCSCMREWRITERCFG const g_apRewritersFor_SifFiles[] =
{
    &g_ForceCRLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SemicolonComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_SqlFiles[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_SqlComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_GnuAsm[] =
{
    &g_ForceNativeEol,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_CstyleComment,
};

static PCSCMREWRITERCFG const g_apRewritersFor_TextFiles[] =
{
    &g_ForceNativeEol,
    &g_StripTrailingBlanks,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    /** @todo check for plain copyright + license in text files. */
};

static PCSCMREWRITERCFG const g_apRewritersFor_PlainTextFiles[] =
{
    &g_ForceNativeEol,
    &g_StripTrailingBlanks,
    &g_SvnKeywords,
    &g_SvnNoExecutable,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
};

static PCSCMREWRITERCFG const g_apRewritersFor_BinaryFiles[] =
{
    &g_SvnBinary,
    &g_SvnSyncProcess,
};

static PCSCMREWRITERCFG const g_apRewritersFor_FileLists[] = /* both makefile and shell script */
{
    &g_ForceLF,
    &g_ExpandTabs,
    &g_StripTrailingBlanks,
    &g_AdjustTrailingLines,
    &g_SvnSyncProcess,
    &g_UnicodeChecks,
    &g_Copyright_HashComment,
};


/**
 * Array of standard rewriter configurations.
 */
static SCMCFGENTRY const g_aConfigs[] =
{
#define SCM_CFG_ENTRY(a_szName, a_aRewriters, a_fBinary, a_szFilePatterns) \
    { RT_ELEMENTS(a_aRewriters), &a_aRewriters[0], a_fBinary, a_szFilePatterns, a_szName }
    SCM_CFG_ENTRY("kup",        g_apRewritersFor_Makefile_kup,     false, "Makefile.kup" ),
    SCM_CFG_ENTRY("kmk",        g_apRewritersFor_Makefile_kmk,     false, "*.kmk" ),
    SCM_CFG_ENTRY("c",          g_apRewritersFor_C_and_CPP,        false, "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc|*.m|*.mm|*.lds" ),
    SCM_CFG_ENTRY("h",          g_apRewritersFor_H_and_HPP,        false, "*.h|*.hpp" ),
    SCM_CFG_ENTRY("rc",         g_apRewritersFor_RC,               false, "*.rc" ),
    SCM_CFG_ENTRY("asm",        g_apRewritersFor_ASM,              false, "*.asm|*.mac|*.inc" ),
    SCM_CFG_ENTRY("dtrace",     g_apRewritersFor_DTrace,           false, "*.d" ),
    SCM_CFG_ENTRY("def",        g_apRewritersFor_DEF,              false, "*.def" ),
    SCM_CFG_ENTRY("iasl",       g_apRewritersFor_DSL,              false, "*.dsl" ),
    SCM_CFG_ENTRY("shell",      g_apRewritersFor_ShellScripts,     false, "*.sh|configure" ),
    SCM_CFG_ENTRY("batch",      g_apRewritersFor_BatchFiles,       false, "*.bat|*.cmd|*.btm" ),
    SCM_CFG_ENTRY("vbs",        g_apRewritersFor_BasicScripts,     false, "*.vbs|*.vb" ),
    SCM_CFG_ENTRY("sed",        g_apRewritersFor_SedScripts,       false, "*.sed" ),
    SCM_CFG_ENTRY("python",     g_apRewritersFor_Python,           false, "*.py" ),
    SCM_CFG_ENTRY("perl",       g_apRewritersFor_Perl,             false, "*.pl|*.pm" ),
    SCM_CFG_ENTRY("drvinf",     g_apRewritersFor_DriverInfFiles,   false, "*.inf" ),
    SCM_CFG_ENTRY("nsis",       g_apRewritersFor_NsisFiles,        false, "*.nsh|*.nsi|*.nsis" ),
    SCM_CFG_ENTRY("java",       g_apRewritersFor_Java,             false, "*.java" ),
    SCM_CFG_ENTRY("scm",        g_apRewritersFor_ScmSettings,      false, "*.scm-settings" ),
    SCM_CFG_ENTRY("image",      g_apRewritersFor_Images,           true,  "*.png|*.bmp|*.jpg|*.pnm|*.ico|*.icns|*.tiff|*.tif|*.xcf|*.gif" ),
    SCM_CFG_ENTRY("xslt",       g_apRewritersFor_Xslt,             false, "*.xsl" ),
    SCM_CFG_ENTRY("xml",        g_apRewritersFor_Xml,              false, "*.xml|*.dist|*.qhcp" ),
    SCM_CFG_ENTRY("wix",        g_apRewritersFor_Wix,              false, "*.wxi|*.wxs|*.wxl" ),
    SCM_CFG_ENTRY("qt-pro",     g_apRewritersFor_QtProject,        false, "*.pro" ),
    SCM_CFG_ENTRY("qt-rc",      g_apRewritersFor_QtResourceFiles,  false, "*.qrc" ),
    SCM_CFG_ENTRY("qt-ts",      g_apRewritersFor_QtTranslations,   false, "*.ts" ),
    SCM_CFG_ENTRY("qt-ui",      g_apRewritersFor_QtUiFiles,        false, "*.ui" ),
    SCM_CFG_ENTRY("sif",        g_apRewritersFor_SifFiles,         false, "*.sif" ),
    SCM_CFG_ENTRY("sql",        g_apRewritersFor_SqlFiles,         false, "*.pgsql|*.sql" ),
    SCM_CFG_ENTRY("gas",        g_apRewritersFor_GnuAsm,           false, "*.S" ),
    SCM_CFG_ENTRY("binary",     g_apRewritersFor_BinaryFiles,      true,  "*.bin|*.pdf|*.zip|*.bz2|*.gz" ),
    /* These should be be last: */
    SCM_CFG_ENTRY("make",       g_apRewritersFor_OtherMakefiles,   false, "Makefile|makefile|GNUmakefile|SMakefile|Makefile.am|Makefile.in|*.cmake|*.gmk" ),
    SCM_CFG_ENTRY("text",       g_apRewritersFor_TextFiles,        false, "*.txt|README*|readme*|ReadMe*|NOTE*|TODO*" ),
    SCM_CFG_ENTRY("plaintext",  g_apRewritersFor_PlainTextFiles,   false, "LICENSE|ChangeLog|FAQ|AUTHORS|INSTALL|NEWS" ),
    SCM_CFG_ENTRY("file-list",  g_apRewritersFor_FileLists,        false, "files_*" ),
};



/* -=-=-=-=-=- settings -=-=-=-=-=- */

/**
 * Delete the given config entry.
 *
 * @param   pEntry              The configuration entry to delete.
 */
static void scmCfgEntryDelete(PSCMCFGENTRY pEntry)
{
    RTMemFree((void *)pEntry->paRewriters);
    pEntry->paRewriters = NULL;
    RTMemFree(pEntry);
}

/**
 * Create a new configuration entry.
 *
 * @returns The new entry. NULL if out of memory.
 * @param   pEntry              The configuration entry to duplicate.
 */
static PSCMCFGENTRY scmCfgEntryNew(void)
{
    PSCMCFGENTRY pNew = (PSCMCFGENTRY)RTMemAlloc(sizeof(*pNew));
    if (pNew)
    {
        pNew->pszName        = "custom";
        pNew->pszFilePattern = "custom";
        pNew->cRewriters     = 0;
        pNew->paRewriters    = NULL;
        pNew->fBinary        = false;
    }
    return pNew;
}

/**
 * Duplicate the given config entry.
 *
 * @returns The duplicate. NULL if out of memory.
 * @param   pEntry              The configuration entry to duplicate.
 */
static PSCMCFGENTRY scmCfgEntryDup(PCSCMCFGENTRY pEntry)
{
    if (pEntry)
    {
        PSCMCFGENTRY pDup = (PSCMCFGENTRY)RTMemDup(pEntry, sizeof(*pEntry));
        if (pDup)
        {
            size_t cbSrcRewriters = sizeof(pEntry->paRewriters[0]) * pEntry->cRewriters;
            size_t cbDstRewriters = sizeof(pEntry->paRewriters[0]) * RT_ALIGN_Z(pEntry->cRewriters, 8);
            pDup->paRewriters = (PCSCMREWRITERCFG const *)RTMemDupEx(pEntry->paRewriters, cbSrcRewriters,
                                                                     cbDstRewriters - cbSrcRewriters);
            if (pDup->paRewriters)
                return pDup;

            RTMemFree(pDup);
        }
        return NULL;
    }
    return scmCfgEntryNew();
}

/**
 * Adds a rewriter action to the given config entry (--add-action).
 *
 * @returns VINF_SUCCESS.
 * @param   pEntry              The configuration entry.
 * @param   pAction             The rewriter action to add.
 */
static int scmCfgEntryAddAction(PSCMCFGENTRY pEntry, PCSCMREWRITERCFG pAction)
{
    PCSCMREWRITERCFG *paRewriters = (PCSCMREWRITERCFG *)pEntry->paRewriters;
    if (pEntry->cRewriters % 8 == 0)
    {
        size_t cbRewriters = sizeof(pEntry->paRewriters[0]) * RT_ALIGN_Z((pEntry->cRewriters + 1), 8);
        void *pvNew = RTMemRealloc(paRewriters, cbRewriters);
        if (pvNew)
            pEntry->paRewriters = paRewriters = (PCSCMREWRITERCFG *)pvNew;
        else
            return VERR_NO_MEMORY;
    }

    paRewriters[pEntry->cRewriters++] = pAction;
    return VINF_SUCCESS;
}

/**
 * Delets an rewriter action from the given config entry (--del-action).
 *
 * @param   pEntry              The configuration entry.
 * @param   pAction             The rewriter action to remove.
 */
static void scmCfgEntryDelAction(PSCMCFGENTRY pEntry, PCSCMREWRITERCFG pAction)
{
    PCSCMREWRITERCFG *paRewriters = (PCSCMREWRITERCFG *)pEntry->paRewriters;
    size_t const cEntries = pEntry->cRewriters;
    size_t       iDst = 0;
    for (size_t iSrc = 0; iSrc < cEntries; iSrc++)
    {
        PCSCMREWRITERCFG pCurAction = paRewriters[iSrc];
        if (pCurAction != pAction)
            paRewriters[iDst++] = pCurAction;
    }
    pEntry->cRewriters = iDst;
}

/**
 * Init a settings structure with settings from @a pSrc.
 *
 * @returns IPRT status code
 * @param   pSettings           The settings.
 * @param   pSrc                The source settings.
 */
static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
{
    *pSettings = *pSrc;

    int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
    if (RT_SUCCESS(rc))
    {
        rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
        if (RT_SUCCESS(rc))
        {
            rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
            if (RT_SUCCESS(rc))
            {
                rc = RTStrDupEx(&pSettings->pszGuardPrefix, pSrc->pszGuardPrefix);
                if (RT_SUCCESS(rc))
                {
                    if (pSrc->pszGuardRelativeToDir)
                        rc = RTStrDupEx(&pSettings->pszGuardRelativeToDir, pSrc->pszGuardRelativeToDir);
                    if (RT_SUCCESS(rc))
                    {

                        if (!pSrc->fFreeTreatAs)
                            return VINF_SUCCESS;

                        pSettings->pTreatAs = scmCfgEntryDup(pSrc->pTreatAs);
                        if (pSettings->pTreatAs)
                            return VINF_SUCCESS;

                        RTStrFree(pSettings->pszGuardRelativeToDir);
                    }
                    RTStrFree(pSettings->pszGuardPrefix);
                }
            }
            RTStrFree(pSettings->pszFilterOutFiles);
        }
        RTStrFree(pSettings->pszFilterFiles);
    }

    pSettings->pszGuardRelativeToDir = NULL;
    pSettings->pszGuardPrefix = NULL;
    pSettings->pszFilterFiles = NULL;
    pSettings->pszFilterOutFiles = NULL;
    pSettings->pszFilterOutDirs = NULL;
    pSettings->pTreatAs = NULL;
    return rc;
}

/**
 * Init a settings structure.
 *
 * @returns IPRT status code
 * @param   pSettings           The settings.
 */
static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
{
    return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
}

/**
 * Deletes the settings, i.e. free any dynamically allocated content.
 *
 * @param   pSettings           The settings.
 */
static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
{
    if (pSettings)
    {
        Assert(pSettings->cchTab != UINT8_MAX);
        pSettings->cchTab = UINT8_MAX;

        RTStrFree(pSettings->pszGuardPrefix);
        RTStrFree(pSettings->pszGuardRelativeToDir);
        RTStrFree(pSettings->pszFilterFiles);
        RTStrFree(pSettings->pszFilterOutFiles);
        RTStrFree(pSettings->pszFilterOutDirs);
        if (pSettings->fFreeTreatAs)
            scmCfgEntryDelete((PSCMCFGENTRY)pSettings->pTreatAs);

        pSettings->pszGuardPrefix = NULL;
        pSettings->pszGuardRelativeToDir = NULL;
        pSettings->pszFilterOutDirs = NULL;
        pSettings->pszFilterOutFiles = NULL;
        pSettings->pszFilterFiles = NULL;
        pSettings->pTreatAs = NULL;
        pSettings->fFreeTreatAs = false;
    }
}

/**
 * Processes a RTGetOpt result.
 *
 * @retval  VINF_SUCCESS if handled.
 * @retval  VERR_OUT_OF_RANGE if the option value was out of range.
 * @retval  VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
 *
 * @param   pSettings           The settings to change.
 * @param   rc                  The RTGetOpt return value.
 * @param   pValueUnion         The RTGetOpt value union.
 * @param   pchDir              The absolute path to the directory relative
 *                              components in pchLine should be relative to.
 * @param   cchDir              The length of the @a pchDir string.
 */
static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion,
                                    const char *pchDir, size_t cchDir)
{
    Assert(pchDir[cchDir - 1] == '/');

    switch (rc)
    {
        case SCMOPT_CONVERT_EOL:
            pSettings->fConvertEol = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_CONVERT_EOL:
            pSettings->fConvertEol = false;
            return VINF_SUCCESS;

        case SCMOPT_CONVERT_TABS:
            pSettings->fConvertTabs = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_CONVERT_TABS:
            pSettings->fConvertTabs = false;
            return VINF_SUCCESS;

        case SCMOPT_FORCE_FINAL_EOL:
            pSettings->fForceFinalEol = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FORCE_FINAL_EOL:
            pSettings->fForceFinalEol = false;
            return VINF_SUCCESS;

        case SCMOPT_FORCE_TRAILING_LINE:
            pSettings->fForceTrailingLine = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FORCE_TRAILING_LINE:
            pSettings->fForceTrailingLine = false;
            return VINF_SUCCESS;


        case SCMOPT_STRIP_TRAILING_BLANKS:
            pSettings->fStripTrailingBlanks = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_STRIP_TRAILING_BLANKS:
            pSettings->fStripTrailingBlanks = false;
            return VINF_SUCCESS;

        case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS:
            pSettings->cMinBlankLinesBeforeFlowerBoxMakers = pValueUnion->u8;
            return VINF_SUCCESS;


        case SCMOPT_STRIP_TRAILING_LINES:
            pSettings->fStripTrailingLines = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_STRIP_TRAILING_LINES:
            pSettings->fStripTrailingLines = false;
            return VINF_SUCCESS;

        case SCMOPT_FIX_FLOWER_BOX_MARKERS:
            pSettings->fFixFlowerBoxMarkers = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FIX_FLOWER_BOX_MARKERS:
            pSettings->fFixFlowerBoxMarkers = false;
            return VINF_SUCCESS;

        case SCMOPT_FIX_HEADER_GUARDS:
            pSettings->fFixHeaderGuards = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FIX_HEADER_GUARDS:
            pSettings->fFixHeaderGuards = false;
            return VINF_SUCCESS;

        case SCMOPT_PRAGMA_ONCE:
            pSettings->fPragmaOnce = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_PRAGMA_ONCE:
            pSettings->fPragmaOnce = false;
            return VINF_SUCCESS;

        case SCMOPT_FIX_HEADER_GUARD_ENDIF:
            pSettings->fFixHeaderGuardEndif = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FIX_HEADER_GUARD_ENDIF:
            pSettings->fFixHeaderGuardEndif = false;
            return VINF_SUCCESS;

        case SCMOPT_ENDIF_GUARD_COMMENT:
            pSettings->fEndifGuardComment = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_ENDIF_GUARD_COMMENT:
            pSettings->fEndifGuardComment = false;
            return VINF_SUCCESS;

        case SCMOPT_GUARD_PREFIX:
            RTStrFree(pSettings->pszGuardPrefix);
            pSettings->pszGuardPrefix = NULL;
            return RTStrDupEx(&pSettings->pszGuardPrefix, pValueUnion->psz);

        case SCMOPT_GUARD_RELATIVE_TO_DIR:
            RTStrFree(pSettings->pszGuardRelativeToDir);
            pSettings->pszGuardRelativeToDir = NULL;
            if (*pValueUnion->psz != '\0')
            {
                if (   strcmp(pValueUnion->psz, "{dir}") == 0
                    || strcmp(pValueUnion->psz, "{parent}") == 0)
                    return RTStrDupEx(&pSettings->pszGuardRelativeToDir, pValueUnion->psz);
                if (cchDir == 1 && *pchDir == '/')
                {
                    pSettings->pszGuardRelativeToDir = RTPathAbsDup(pValueUnion->psz);
                    if (pSettings->pszGuardRelativeToDir)
                        return VINF_SUCCESS;
                }
                else
                {
                    char *pszDir = RTStrDupN(pchDir, cchDir);
                    if (pszDir)
                    {
                        pSettings->pszGuardRelativeToDir = RTPathAbsExDup(pszDir, pValueUnion->psz, RTPATH_STR_F_STYLE_HOST);
                        RTStrFree(pszDir);
                        if (pSettings->pszGuardRelativeToDir)
                            return VINF_SUCCESS;
                    }
                }
                RTMsgError("Failed to abspath --guard-relative-to-dir value '%s' - probably out of memory\n", pValueUnion->psz);
                return VERR_NO_STR_MEMORY;
            }
            return VINF_SUCCESS;

        case SCMOPT_FIX_TODOS:
            pSettings->fFixTodos = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FIX_TODOS:
            pSettings->fFixTodos = false;
            return VINF_SUCCESS;

        case SCMOPT_FIX_ERR_H:
            pSettings->fFixErrH = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_FIX_ERR_H:
            pSettings->fFixErrH = false;
            return VINF_SUCCESS;

        case SCMOPT_ONLY_GUEST_HOST_PAGE:
            pSettings->fOnlyGuestHostPage = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_PAGE_RESTRICTIONS:
            pSettings->fOnlyGuestHostPage = false;
            return VINF_SUCCESS;

        case SCMOPT_NO_ASM_MEM_PAGE_USE:
            pSettings->fNoASMMemPageUse = true;
            return VINF_SUCCESS;
        case SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE:
            pSettings->fNoASMMemPageUse = false;
            return VINF_SUCCESS;

        case SCMOPT_NO_RC_USE:
            pSettings->fOnlyHrcVrcInsteadOfRc = true;
            return VINF_SUCCESS;
        case SCMOPT_UNRESTRICTED_RC_USE:
            pSettings->fOnlyHrcVrcInsteadOfRc = false;
            return VINF_SUCCESS;

        case SCMOPT_UPDATE_COPYRIGHT_YEAR:
            pSettings->fUpdateCopyrightYear = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_UPDATE_COPYRIGHT_YEAR:
            pSettings->fUpdateCopyrightYear = false;
            return VINF_SUCCESS;

        case SCMOPT_EXTERNAL_COPYRIGHT:
            pSettings->fExternalCopyright = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_EXTERNAL_COPYRIGHT:
            pSettings->fExternalCopyright = false;
            return VINF_SUCCESS;

        case SCMOPT_NO_UPDATE_LICENSE:
            pSettings->enmUpdateLicense = kScmLicense_LeaveAlone;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_OSE_GPL:
            pSettings->enmUpdateLicense = kScmLicense_OseGpl;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL:
            pSettings->enmUpdateLicense = kScmLicense_OseDualGplCddl;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_OSE_CDDL:
            pSettings->enmUpdateLicense = kScmLicense_OseCddl;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_LGPL:
            pSettings->enmUpdateLicense = kScmLicense_Lgpl;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_MIT:
            pSettings->enmUpdateLicense = kScmLicense_Mit;
            return VINF_SUCCESS;
        case SCMOPT_LICENSE_BASED_ON_MIT:
            pSettings->enmUpdateLicense = kScmLicense_BasedOnMit;
            return VINF_SUCCESS;

        case SCMOPT_LGPL_DISCLAIMER:
            pSettings->fLgplDisclaimer = true;
            return VINF_SUCCESS;
        case SCMOPT_NO_LGPL_DISCLAIMER:
            pSettings->fLgplDisclaimer = false;
            return VINF_SUCCESS;

        case SCMOPT_ONLY_SVN_DIRS:
            pSettings->fOnlySvnDirs = true;
            return VINF_SUCCESS;
        case SCMOPT_NOT_ONLY_SVN_DIRS:
            pSettings->fOnlySvnDirs = false;
            return VINF_SUCCESS;

        case SCMOPT_ONLY_SVN_FILES:
            pSettings->fOnlySvnFiles = true;
            return VINF_SUCCESS;
        case SCMOPT_NOT_ONLY_SVN_FILES:
            pSettings->fOnlySvnFiles = false;
            return VINF_SUCCESS;

        case SCMOPT_SET_SVN_EOL:
            pSettings->fSetSvnEol = true;
            return VINF_SUCCESS;
        case SCMOPT_DONT_SET_SVN_EOL:
            pSettings->fSetSvnEol = false;
            return VINF_SUCCESS;

        case SCMOPT_SET_SVN_EXECUTABLE:
            pSettings->fSetSvnExecutable = true;
            return VINF_SUCCESS;
        case SCMOPT_DONT_SET_SVN_EXECUTABLE:
            pSettings->fSetSvnExecutable = false;
            return VINF_SUCCESS;

        case SCMOPT_SET_SVN_KEYWORDS:
            pSettings->fSetSvnKeywords = true;
            return VINF_SUCCESS;
        case SCMOPT_DONT_SET_SVN_KEYWORDS:
            pSettings->fSetSvnKeywords = false;
            return VINF_SUCCESS;

        case SCMOPT_SKIP_SVN_SYNC_PROCESS:
            pSettings->fSkipSvnSyncProcess = true;
            return VINF_SUCCESS;
        case SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS:
            pSettings->fSkipSvnSyncProcess = false;
            return VINF_SUCCESS;

        case SCMOPT_SKIP_UNICODE_CHECKS:
            pSettings->fSkipUnicodeChecks = true;
            return VINF_SUCCESS;
        case SCMOPT_DONT_SKIP_UNICODE_CHECKS:
            pSettings->fSkipUnicodeChecks = false;
            return VINF_SUCCESS;

        case SCMOPT_TAB_SIZE:
            if (   pValueUnion->u8 < 1
                || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
            {
                RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
                           pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
                return VERR_OUT_OF_RANGE;
            }
            pSettings->cchTab = pValueUnion->u8;
            return VINF_SUCCESS;

        case SCMOPT_WIDTH:
            if (pValueUnion->u8 < 20 || pValueUnion->u8 > 200)
            {
                RTMsgError("Invalid width size: %u - must be in {20..200} range\n", pValueUnion->u8);
                return VERR_OUT_OF_RANGE;
            }
            pSettings->cchWidth = pValueUnion->u8;
            return VINF_SUCCESS;

        case SCMOPT_FILTER_OUT_DIRS:
        case SCMOPT_FILTER_FILES:
        case SCMOPT_FILTER_OUT_FILES:
        {
            char **ppsz = NULL;
            switch (rc)
            {
                case SCMOPT_FILTER_OUT_DIRS:    ppsz = &pSettings->pszFilterOutDirs; break;
                case SCMOPT_FILTER_FILES:       ppsz = &pSettings->pszFilterFiles; break;
                case SCMOPT_FILTER_OUT_FILES:   ppsz = &pSettings->pszFilterOutFiles; break;
            }

            /*
             * An empty string zaps the current list.
             */
            if (!*pValueUnion->psz)
                return RTStrATruncate(ppsz, 0);

            /*
             * Non-empty strings are appended to the pattern list.
             *
             * Strip leading and trailing pattern separators before attempting
             * to append it.  If it's just separators, don't do anything.
             */
            const char *pszSrc = pValueUnion->psz;
            while (*pszSrc == '|')
                pszSrc++;
            size_t cchSrc = strlen(pszSrc);
            while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
                cchSrc--;
            if (!cchSrc)
                return VINF_SUCCESS;

            /* Append it pattern by pattern, turning settings-relative paths into absolute ones. */
            for (;;)
            {
                const char *pszEnd = (const char *)memchr(pszSrc, '|', cchSrc);
                size_t cchPattern = pszEnd ? pszEnd - pszSrc : cchSrc;
                int rc2;
                if (*pszSrc == '/')
                    rc2 = RTStrAAppendExN(ppsz, 3,
                                          "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
                                          pchDir, cchDir - 1,
                                          pszSrc, cchPattern);
                else
                    rc2 = RTStrAAppendExN(ppsz, 2,
                                          "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
                                          pszSrc, cchPattern);
                if (RT_FAILURE(rc2))
                    return rc2;

                /* next */
                cchSrc -= cchPattern;
                if (!cchSrc)
                    return VINF_SUCCESS;
                cchSrc -= 1;
                pszSrc += cchPattern + 1;
            }
            /* not reached */
        }

        case SCMOPT_TREAT_AS:
            if (pSettings->fFreeTreatAs)
            {
                scmCfgEntryDelete((PSCMCFGENTRY)pSettings->pTreatAs);
                pSettings->pTreatAs = NULL;
                pSettings->fFreeTreatAs = false;
            }

            if (*pValueUnion->psz)
            {
                /* first check the names, then patterns (legacy). */
                for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
                    if (strcmp(g_aConfigs[iCfg].pszName, pValueUnion->psz) == 0)
                    {
                        pSettings->pTreatAs = &g_aConfigs[iCfg];
                        return VINF_SUCCESS;
                    }
                for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
                    if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX,
                                                     pValueUnion->psz, RTSTR_MAX, NULL))
                    {
                        pSettings->pTreatAs = &g_aConfigs[iCfg];
                        return VINF_SUCCESS;
                    }
                /* Special help for listing the possibilities?  */
                if (strcmp(pValueUnion->psz, "help") == 0)
                {
                    RTPrintf("Possible --treat-as values:\n");
                    for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
                        RTPrintf("    %s (%s)\n", g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].pszFilePattern);
                }
                return VERR_NOT_FOUND;
            }

            pSettings->pTreatAs = NULL;
            return VINF_SUCCESS;

        case SCMOPT_ADD_ACTION:
            for (uint32_t iAction = 0; iAction < RT_ELEMENTS(g_papRewriterActions); iAction++)
                if (strcmp(g_papRewriterActions[iAction]->pszName, pValueUnion->psz) == 0)
                {
                    PSCMCFGENTRY pEntry = (PSCMCFGENTRY)pSettings->pTreatAs;
                    if (!pSettings->fFreeTreatAs)
                    {
                        pEntry = scmCfgEntryDup(pEntry);
                        if (!pEntry)
                            return VERR_NO_MEMORY;
                        pSettings->pTreatAs = pEntry;
                        pSettings->fFreeTreatAs = true;
                    }
                    return scmCfgEntryAddAction(pEntry, g_papRewriterActions[iAction]);
                }
            RTMsgError("Unknown --add-action value '%s'.  Try --help-actions for a list.", pValueUnion->psz);
            return VERR_NOT_FOUND;

        case SCMOPT_DEL_ACTION:
        {
            uint32_t cActions = 0;
            for (uint32_t iAction = 0; iAction < RT_ELEMENTS(g_papRewriterActions); iAction++)
                if (RTStrSimplePatternMatch(pValueUnion->psz, g_papRewriterActions[iAction]->pszName))
                {
                    cActions++;
                    PSCMCFGENTRY pEntry = (PSCMCFGENTRY)pSettings->pTreatAs;
                    if (!pSettings->fFreeTreatAs)
                    {
                        pEntry = scmCfgEntryDup(pEntry);
                        if (!pEntry)
                            return VERR_NO_MEMORY;
                        pSettings->pTreatAs = pEntry;
                        pSettings->fFreeTreatAs = true;
                    }
                    scmCfgEntryDelAction(pEntry, g_papRewriterActions[iAction]);
                    if (!strchr(pValueUnion->psz, '*'))
                        return VINF_SUCCESS;
                }
            if (cActions > 0)
                return VINF_SUCCESS;
            RTMsgError("Unknown --del-action value '%s'.  Try --help-actions for a list.", pValueUnion->psz);
            return VERR_NOT_FOUND;
        }

        default:
            return VERR_GETOPT_UNKNOWN_OPTION;
    }
}

/**
 * Parses an option string.
 *
 * @returns IPRT status code.
 * @param   pBase               The base settings structure to apply the options
 *                              to.
 * @param   pszOptions          The options to parse.
 * @param   pchDir              The absolute path to the directory relative
 *                              components in pchLine should be relative to.
 * @param   cchDir              The length of the @a pchDir string.
 */
static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine, const char *pchDir, size_t cchDir)
{
    int    cArgs;
    char **papszArgs;
    int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
    if (RT_SUCCESS(rc))
    {
        RTGETOPTUNION   ValueUnion;
        RTGETOPTSTATE   GetOptState;
        rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
        if (RT_SUCCESS(rc))
        {
            while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
            {
                rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion, pchDir, cchDir);
                if (RT_FAILURE(rc))
                    break;
            }
        }
        RTGetOptArgvFree(papszArgs);
    }

    return rc;
}

/**
 * Parses an unterminated option string.
 *
 * @returns IPRT status code.
 * @param   pBase               The base settings structure to apply the options
 *                              to.
 * @param   pchLine             The line.
 * @param   cchLine             The line length.
 * @param   pchDir              The absolute path to the directory relative
 *                              components in pchLine should be relative to.
 * @param   cchDir              The length of the @a pchDir string.
 */
static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine,
                                       const char *pchDir, size_t cchDir)
{
    char *pszLine = RTStrDupN(pchLine, cchLine);
    if (!pszLine)
        return VERR_NO_MEMORY;
    int rc = scmSettingsBaseParseString(pBase, pszLine, pchDir, cchDir);
    RTStrFree(pszLine);
    return rc;
}

/**
 * Verifies the options string.
 *
 * @returns IPRT status code.
 * @param   pszOptions          The options to verify .
 */
static int scmSettingsBaseVerifyString(const char *pszOptions)
{
    SCMSETTINGSBASE Base;
    int rc = scmSettingsBaseInit(&Base);
    if (RT_SUCCESS(rc))
    {
        rc = scmSettingsBaseParseString(&Base, pszOptions, "/", 1);
        scmSettingsBaseDelete(&Base);
    }
    return rc;
}

/**
 * Loads settings found in editor and SCM settings directives within the
 * document (@a pStream).
 *
 * @returns IPRT status code.
 * @param   pBase               The settings base to load settings into.
 * @param   pStream             The stream to scan for settings directives.
 */
static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
{
    /** @todo Editor and SCM settings directives in documents.  */
    RT_NOREF2(pBase, pStream);
    return VINF_SUCCESS;
}

/**
 * Creates a new settings file struct, cloning @a pSettings.
 *
 * @returns IPRT status code.
 * @param   ppSettings          Where to return the new struct.
 * @param   pSettingsBase       The settings to inherit from.
 */
static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
{
    PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
    if (!pSettings)
        return VERR_NO_MEMORY;
    int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
    if (RT_SUCCESS(rc))
    {
        pSettings->pDown   = NULL;
        pSettings->pUp     = NULL;
        pSettings->paPairs = NULL;
        pSettings->cPairs  = 0;
        *ppSettings = pSettings;
        return VINF_SUCCESS;
    }
    RTMemFree(pSettings);
    return rc;
}

/**
 * Destroys a settings structure.
 *
 * @param   pSettings           The settings structure to destroy.  NULL is OK.
 */
static void scmSettingsDestroy(PSCMSETTINGS pSettings)
{
    if (pSettings)
    {
        scmSettingsBaseDelete(&pSettings->Base);
        for (size_t i = 0; i < pSettings->cPairs; i++)
        {
            RTStrFree(pSettings->paPairs[i].pszPattern);
            RTStrFree(pSettings->paPairs[i].pszOptions);
            RTStrFree(pSettings->paPairs[i].pszRelativeTo);
            pSettings->paPairs[i].pszPattern = NULL;
            pSettings->paPairs[i].pszOptions = NULL;
            pSettings->paPairs[i].pszRelativeTo = NULL;
        }
        RTMemFree(pSettings->paPairs);
        pSettings->paPairs = NULL;
        RTMemFree(pSettings);
    }
}

/**
 * Adds a pattern/options pair to the settings structure.
 *
 * @returns IPRT status code.
 * @param   pSettings           The settings.
 * @param   pchLine             The line containing the unparsed pair.
 * @param   cchLine             The length of the line.
 * @param   offColon            The offset of the colon into the line.
 * @param   pchDir              The absolute path to the directory relative
 *                              components in pchLine should be relative to.
 * @param   cchDir              The length of the @a pchDir string.
 */
static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine, size_t offColon,
                              const char *pchDir, size_t cchDir)
{
    Assert(pchLine[offColon] == ':' && offColon < cchLine);
    Assert(pchDir[cchDir - 1] == '/');

    /*
     * Split the string.
     */
    size_t cchPattern = offColon;
    size_t cchOptions = cchLine - cchPattern - 1;

    /* strip spaces everywhere */
    while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
        cchPattern--;
    while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
        cchPattern--, pchLine++;

    const char *pchOptions = &pchLine[offColon + 1];
    while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
        cchOptions--;
    while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
        cchOptions--, pchOptions++;

    /* Quietly ignore empty patterns and empty options. */
    if (!cchOptions || !cchPattern)
        return VINF_SUCCESS;

    /*
     * Prepair the pair and verify the option string.
     */
    uint32_t iPair = pSettings->cPairs;
    if ((iPair % 32) == 0)
    {
        void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
        if (!pvNew)
            return VERR_NO_MEMORY;
        pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
    }

    pSettings->paPairs[iPair].pszPattern    = RTStrDupN(pchLine, cchPattern);
    pSettings->paPairs[iPair].pszOptions    = RTStrDupN(pchOptions, cchOptions);
    pSettings->paPairs[iPair].pszRelativeTo = RTStrDupN(pchDir, cchDir);
    int rc;
    if (   pSettings->paPairs[iPair].pszPattern
        && pSettings->paPairs[iPair].pszOptions
        && pSettings->paPairs[iPair].pszRelativeTo)
        rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
    else
        rc = VERR_NO_MEMORY;

    /*
     * If it checked out fine, expand any relative paths in the pattern.
     */
    if (RT_SUCCESS(rc))
    {
        size_t cPattern = 1;
        size_t cRelativePaths = 0;
        const char *pszSrc = pSettings->paPairs[iPair].pszPattern;
        for (;;)
        {
            if (*pszSrc == '/')
                cRelativePaths++;
            pszSrc = strchr(pszSrc, '|');
            if (!pszSrc)
                break;
            pszSrc++;
            cPattern++;
        }
        pSettings->paPairs[iPair].fMultiPattern = cPattern > 1;
        if (cRelativePaths > 0)
        {
            char *pszNewPattern = RTStrAlloc(cchPattern + cRelativePaths * (cchDir - 1) + 1);
            if (pszNewPattern)
            {
                char *pszDst = pszNewPattern;
                pszSrc = pSettings->paPairs[iPair].pszPattern;
                for (;;)
                {
                    if (*pszSrc == '/')
                    {
                        memcpy(pszDst, pchDir, cchDir);
                        pszDst += cchDir;
                        pszSrc += 1;
                    }

                    /* Look for the next relative path. */
                    const char *pszSrcNext = strchr(pszSrc, '|');
                    while (pszSrcNext && pszSrcNext[1] != '/')
                        pszSrcNext = strchr(pszSrcNext, '|');
                    if (!pszSrcNext)
                        break;

                    /* Copy stuff between current and the next path. */
                    pszSrcNext++;
                    memcpy(pszDst, pszSrc, pszSrcNext - pszSrc);
                    pszDst += pszSrcNext - pszSrc;
                    pszSrc = pszSrcNext;
                }

                /* Copy the final portion and replace the pattern. */
                strcpy(pszDst, pszSrc);

                RTStrFree(pSettings->paPairs[iPair].pszPattern);
                pSettings->paPairs[iPair].pszPattern = pszNewPattern;
            }
            else
                rc = VERR_NO_MEMORY;
        }
    }
    if (RT_SUCCESS(rc))
        /*
         * Commit the pair.
         */
        pSettings->cPairs = iPair + 1;
    else
    {
        RTStrFree(pSettings->paPairs[iPair].pszPattern);
        RTStrFree(pSettings->paPairs[iPair].pszOptions);
        RTStrFree(pSettings->paPairs[iPair].pszRelativeTo);
    }
    return rc;
}

/**
 * Loads in the settings from @a pszFilename.
 *
 * @returns IPRT status code.
 * @param   pSettings           Where to load the settings file.
 * @param   pszFilename         The file to load.
 */
static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
{
    ScmVerbose(NULL, 3, "Loading settings file '%s'...\n", pszFilename);

    /* Turn filename into an absolute path and drop the filename. */
    char szAbsPath[RTPATH_MAX];
    int rc = RTPathAbs(pszFilename, szAbsPath, sizeof(szAbsPath));
    if (RT_FAILURE(rc))
    {
        RTMsgError("%s: RTPathAbs -> %Rrc\n", pszFilename, rc);
        return rc;
    }
    RTPathChangeToUnixSlashes(szAbsPath, true);
    size_t cchDir = RTPathFilename(szAbsPath) - &szAbsPath[0];

    /* Try open it.*/
    SCMSTREAM Stream;
    rc = ScmStreamInitForReading(&Stream, pszFilename);
    if (RT_SUCCESS(rc))
    {
        SCMEOL      enmEol;
        const char *pchLine;
        size_t      cchLine;
        while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
        {
            /* Ignore leading spaces. */
            while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
                pchLine++, cchLine--;

            /* Ignore empty lines and comment lines. */
            if (cchLine < 1 || *pchLine == '#')
                continue;

            /* Deal with escaped newlines. */
            size_t  iFirstLine  = ~(size_t)0;
            char   *pszFreeLine = NULL;
            if (   pchLine[cchLine - 1] == '\\'
                && (   cchLine < 2
                    || pchLine[cchLine - 2] != '\\') )
            {
                iFirstLine = ScmStreamTellLine(&Stream);

                cchLine--;
                while (cchLine > 0 && RT_C_IS_SPACE(pchLine[cchLine - 1]))
                    cchLine--;

                size_t cchTotal = cchLine;
                pszFreeLine = RTStrDupN(pchLine, cchLine);
                if (pszFreeLine)
                {
                    /* Append following lines. */
                    while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
                    {
                        while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
                            pchLine++, cchLine--;

                        bool const fDone = cchLine == 0
                                        || pchLine[cchLine - 1] != '\\'
                                        || (cchLine >= 2 && pchLine[cchLine - 2] == '\\');
                        if (!fDone)
                        {
                            cchLine--;
                            while (cchLine > 0 && RT_C_IS_SPACE(pchLine[cchLine - 1]))
                                cchLine--;
                        }

                        rc = RTStrRealloc(&pszFreeLine, cchTotal + 1 + cchLine + 1);
                        if (RT_FAILURE(rc))
                            break;
                        pszFreeLine[cchTotal++] = ' ';
                        memcpy(&pszFreeLine[cchTotal], pchLine, cchLine);
                        cchTotal += cchLine;
                        pszFreeLine[cchTotal] = '\0';

                        if (fDone)
                            break;
                    }
                }
                else
                    rc = VERR_NO_STR_MEMORY;

                if (RT_FAILURE(rc))
                {
                    RTStrFree(pszFreeLine);
                    rc = RTMsgErrorRc(VERR_NO_MEMORY, "%s: Ran out of memory deal with escaped newlines", pszFilename);
                    break;
                }

                pchLine = pszFreeLine;
                cchLine = cchTotal;
            }

            /* What kind of line is it? */
            const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
            if (pchColon)
                rc = scmSettingsAddPair(pSettings, pchLine, cchLine, pchColon - pchLine, szAbsPath, cchDir);
            else
                rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine, szAbsPath, cchDir);
            if (pszFreeLine)
                RTStrFree(pszFreeLine);
            if (RT_FAILURE(rc))
            {
                RTMsgError("%s:%d: %Rrc\n",
                           pszFilename, iFirstLine == ~(size_t)0 ? ScmStreamTellLine(&Stream) : iFirstLine, rc);
                break;
            }
        }

        if (RT_SUCCESS(rc))
        {
            rc = ScmStreamGetStatus(&Stream);
            if (RT_FAILURE(rc))
                RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
        }
        ScmStreamDelete(&Stream);
    }
    else
        RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
    return rc;
}

#if 0 /* unused */
/**
 * Parse the specified settings file creating a new settings struct from it.
 *
 * @returns IPRT status code
 * @param   ppSettings          Where to return the new settings.
 * @param   pszFilename         The file to parse.
 * @param   pSettingsBase       The base settings we inherit from.
 */
static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
{
    PSCMSETTINGS pSettings;
    int rc = scmSettingsCreate(&pSettings, pSettingsBase);
    if (RT_SUCCESS(rc))
    {
        rc = scmSettingsLoadFile(pSettings, pszFilename, RTPathFilename(pszFilename) - pszFilename);
        if (RT_SUCCESS(rc))
        {
            *ppSettings = pSettings;
            return VINF_SUCCESS;
        }

        scmSettingsDestroy(pSettings);
    }
    *ppSettings = NULL;
    return rc;
}
#endif


/**
 * Create an initial settings structure when starting processing a new file or
 * directory.
 *
 * This will look for .scm-settings files from the root and down to the
 * specified directory, combining them into the returned settings structure.
 *
 * @returns IPRT status code.
 * @param   ppSettings          Where to return the pointer to the top stack
 *                              object.
 * @param   pBaseSettings       The base settings we inherit from (globals
 *                              typically).
 * @param   pszPath             The absolute path to the new directory or file.
 */
static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
{
    *ppSettings = NULL;                 /* try shut up gcc. */

    /*
     * We'll be working with a stack copy of the path.
     */
    char    szFile[RTPATH_MAX];
    size_t  cchDir = strlen(pszPath);
    if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
        return VERR_FILENAME_TOO_LONG;

    /*
     * Create the bottom-most settings.
     */
    PSCMSETTINGS pSettings;
    int rc = scmSettingsCreate(&pSettings, pBaseSettings);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Enumerate the path components from the root and down. Load any setting
     * files we find.
     */
    size_t cComponents = RTPathCountComponents(pszPath);
    for (size_t i = 1; i <= cComponents; i++)
    {
        rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
        if (RT_SUCCESS(rc))
            rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
        if (RT_FAILURE(rc))
            break;
        RTPathChangeToUnixSlashes(szFile, true);

        if (RTFileExists(szFile))
        {
            rc = scmSettingsLoadFile(pSettings, szFile);
            if (RT_FAILURE(rc))
                break;
        }
    }

    if (RT_SUCCESS(rc))
        *ppSettings = pSettings;
    else
        scmSettingsDestroy(pSettings);
    return rc;
}

/**
 * Pushes a new settings set onto the stack.
 *
 * @param   ppSettingsStack     The pointer to the pointer to the top stack
 *                              element.  This will be used as input and output.
 * @param   pSettings           The settings to push onto the stack.
 */
static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
{
    PSCMSETTINGS pOld = *ppSettingsStack;
    pSettings->pDown  = pOld;
    pSettings->pUp    = NULL;
    if (pOld)
        pOld->pUp = pSettings;
    *ppSettingsStack = pSettings;
}

/**
 * Pushes the settings of the specified directory onto the stack.
 *
 * We will load any .scm-settings in the directory.  A stack entry is added even
 * if no settings file was found.
 *
 * @returns IPRT status code.
 * @param   ppSettingsStack     The pointer to the pointer to the top stack
 *                              element.  This will be used as input and output.
 * @param   pszDir              The directory to do this for.
 */
static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
{
    char szFile[RTPATH_MAX];
    int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
    if (RT_SUCCESS(rc))
    {
        RTPathChangeToUnixSlashes(szFile, true);

        PSCMSETTINGS pSettings;
        rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
        if (RT_SUCCESS(rc))
        {
            if (RTFileExists(szFile))
                rc = scmSettingsLoadFile(pSettings, szFile);
            if (RT_SUCCESS(rc))
            {
                scmSettingsStackPush(ppSettingsStack, pSettings);
                return VINF_SUCCESS;
            }

            scmSettingsDestroy(pSettings);
        }
    }
    return rc;
}


/**
 * Pops a settings set off the stack.
 *
 * @returns The popped settings.
 * @param   ppSettingsStack     The pointer to the pointer to the top stack
 *                              element.  This will be used as input and output.
 */
static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
{
    PSCMSETTINGS pRet = *ppSettingsStack;
    PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
    *ppSettingsStack = pNew;
    if (pNew)
        pNew->pUp    = NULL;
    if (pRet)
    {
        pRet->pUp    = NULL;
        pRet->pDown  = NULL;
    }
    return pRet;
}

/**
 * Pops and destroys the top entry of the stack.
 *
 * @param   ppSettingsStack     The pointer to the pointer to the top stack
 *                              element.  This will be used as input and output.
 */
static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
{
    scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
}

/**
 * Constructs the base settings for the specified file name.
 *
 * @returns IPRT status code.
 * @param   pSettingsStack      The top element on the settings stack.
 * @param   pszFilename         The file name.
 * @param   pszBasename         The base name (pointer within @a pszFilename).
 * @param   cchBasename         The length of the base name.  (For passing to
 *                              RTStrSimplePatternMultiMatch.)
 * @param   pBase               Base settings to initialize.
 */
static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
                                        const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
{
    ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase(%s, %.*s)\n", pszFilename, cchBasename, pszBasename);

    int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
    if (RT_SUCCESS(rc))
    {
        /* find the bottom entry in the stack. */
        PCSCMSETTINGS pCur = pSettingsStack;
        while (pCur->pDown)
            pCur = pCur->pDown;

        /* Work our way up thru the stack and look for matching pairs. */
        while (pCur)
        {
            size_t const cPairs = pCur->cPairs;
            if (cPairs)
            {
                for (size_t i = 0; i < cPairs; i++)
                    if (   !pCur->paPairs[i].fMultiPattern
                        ?    RTStrSimplePatternNMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
                                                      pszBasename,  cchBasename)
                          || RTStrSimplePatternMatch(pCur->paPairs[i].pszPattern, pszFilename)
                        :    RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
                                                          pszBasename,  cchBasename, NULL)
                          || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
                                                          pszFilename,  RTSTR_MAX, NULL))
                    {
                        ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase: Matched '%s' : '%s'\n",
                                   pCur->paPairs[i].pszPattern, pCur->paPairs[i].pszOptions);
                        rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions,
                                                        pCur->paPairs[i].pszRelativeTo, strlen(pCur->paPairs[i].pszRelativeTo));
                        if (RT_FAILURE(rc))
                            break;
                    }
                if (RT_FAILURE(rc))
                    break;
            }

            /* advance */
            pCur = pCur->pUp;
        }
    }
    if (RT_FAILURE(rc))
        scmSettingsBaseDelete(pBase);
    return rc;
}


/* -=-=-=-=-=- misc -=-=-=-=-=- */


/**
 * Prints the per file banner needed and the message level is high enough.
 *
 * @param   pState              The rewrite state.
 * @param   iLevel              The required verbosity level.
 */
void ScmVerboseBanner(PSCMRWSTATE pState, int iLevel)
{
    if (iLevel <= g_iVerbosity && !pState->fFirst)
    {
        RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
        pState->fFirst = true;
    }
}


/**
 * Prints a verbose message if the level is high enough.
 *
 * @param   pState              The rewrite state.  Optional.
 * @param   iLevel              The required verbosity level.
 * @param   pszFormat           The message format string.  Can be NULL if we
 *                              only want to trigger the per file message.
 * @param   ...                 Format arguments.
 */
void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
{
    if (iLevel <= g_iVerbosity)
    {
        if (pState && !pState->fFirst)
        {
            RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
            pState->fFirst = true;
        }
        RTPrintf(pState
                 ? "%s: info:   "
                 : "%s: info: ",
                 g_szProgName);
        va_list va;
        va_start(va, pszFormat);
        RTPrintfV(pszFormat, va);
        va_end(va);
    }
}


/**
 * Prints an error message.
 *
 * @returns false
 * @param   pState              The rewrite state.  Optional.
 * @param   rc                  The error code.
 * @param   pszFormat           The message format string.
 * @param   ...                 Format arguments.
 */
bool ScmError(PSCMRWSTATE pState, int rc, const char *pszFormat, ...)
{
    if (RT_SUCCESS(pState->rc))
        pState->rc = rc;

    if (!pState->fFirst)
    {
        RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
        pState->fFirst = true;
    }
    va_list va;
    va_start(va, pszFormat);
    RTPrintf("%s: error: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
    va_end(va);

    return false;
}


/**
 * Prints message indicating that something requires manual fixing.
 *
 * @returns false
 * @param   pState              The rewrite state.  Optional.
 * @param   rc                  The error code.
 * @param   pszFormat           The message format string.
 * @param   ...                 Format arguments.
 */
bool ScmFixManually(PSCMRWSTATE pState, const char *pszFormat, ...)
{
    pState->fNeedsManualRepair = true;

    if (!pState->fFirst)
    {
        RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
        pState->fFirst = true;
    }
    va_list va;
    va_start(va, pszFormat);
    RTPrintf("%s: error/fixme: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
    va_end(va);

    return false;
}


/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */


/**
 * Processes a file.
 *
 * @returns IPRT status code.
 * @param   pState              The rewriter state.
 * @param   pszFilename         The file name.
 * @param   pszBasename         The base name (pointer within @a pszFilename).
 * @param   cchBasename         The length of the base name.  (For passing to
 *                              RTStrSimplePatternMultiMatch.)
 * @param   pBaseSettings       The base settings to use.  It's OK to modify
 *                              these.
 */
static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
                               PSCMSETTINGSBASE pBaseSettings)
{
    /*
     * Do the file level filtering.
     */
    if (   pBaseSettings->pszFilterFiles
        && *pBaseSettings->pszFilterFiles
        && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
    {
        ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
        g_cFilesSkipped++;
        return VINF_SUCCESS;
    }
    if (   pBaseSettings->pszFilterOutFiles
        && *pBaseSettings->pszFilterOutFiles
        && (   RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
            || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
    {
        ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
        g_cFilesSkipped++;
        return VINF_SUCCESS;
    }
    if (   pBaseSettings->fOnlySvnFiles
        && !ScmSvnIsInWorkingCopy(pState))
    {
        ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
        g_cFilesNotInSvn++;
        return VINF_SUCCESS;
    }

    /*
     * Create an input stream from the file and check that it's text.
     */
    SCMSTREAM Stream1;
    int rc = ScmStreamInitForReading(&Stream1, pszFilename);
    if (RT_FAILURE(rc))
    {
        RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
        return rc;
    }
    bool const fIsText = ScmStreamIsText(&Stream1);

    /*
     * Try find a matching rewrite config for this filename.
     */
    PCSCMCFGENTRY pCfg = pBaseSettings->pTreatAs;
    if (!pCfg)
    {
        for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
            if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
            {
                pCfg = &g_aConfigs[iCfg];
                break;
            }
        if (!pCfg)
        {
            /* On failure try check for hash-bang stuff before giving up. */
            if (fIsText)
            {
                SCMEOL      enmIgn;
                size_t      cchFirst;
                const char *pchFirst = ScmStreamGetLine(&Stream1, &cchFirst, &enmIgn);
                if (cchFirst >= 9 && pchFirst && *pchFirst == '#')
                {
                    do
                    {
                        pchFirst++;
                        cchFirst--;
                    } while (cchFirst > 0 && RT_C_IS_BLANK(*pchFirst));
                    if (*pchFirst == '!')
                    {
                        do
                        {
                            pchFirst++;
                            cchFirst--;
                        } while (cchFirst > 0 && RT_C_IS_BLANK(*pchFirst));
                        const char *pszTreatAs = NULL;
                        if (   (cchFirst >= 7 && strncmp(pchFirst, "/bin/sh", 7) == 0)
                            || (cchFirst >= 9 && strncmp(pchFirst, "/bin/bash", 9) == 0)
                            || (cchFirst >= 4+9 && strncmp(pchFirst, "/usr/bin/bash", 4+9) == 0) )
                            pszTreatAs = "shell";
                        else if (   (cchFirst >= 15 && strncmp(pchFirst, "/usr/bin/python", 15) == 0)
                                 || (cchFirst >= 19 && strncmp(pchFirst, "/usr/bin/env python", 19) == 0) )
                            pszTreatAs = "python";
                        else if (   (cchFirst >= 13 && strncmp(pchFirst, "/usr/bin/perl", 13) == 0)
                                 || (cchFirst >= 17 && strncmp(pchFirst, "/usr/bin/env perl", 17) == 0) )
                            pszTreatAs = "perl";
                        if (pszTreatAs)
                        {
                            for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
                                if (strcmp(pszTreatAs, g_aConfigs[iCfg].pszName) == 0)
                                {
                                    pCfg = &g_aConfigs[iCfg];
                                    break;
                                }
                            Assert(pCfg);
                        }
                    }
                }
                ScmStreamRewindForReading(&Stream1);
            }
            if (!pCfg)
            {
                ScmVerbose(NULL, 2, "skipping '%s': no rewriters configured\n", pszFilename);
                g_cFilesNoRewriters++;
                ScmStreamDelete(&Stream1);
                return VINF_SUCCESS;
            }
        }
        ScmVerbose(pState, 4, "matched \"%s\" (%s)\n", pCfg->pszFilePattern, pCfg->pszName);
    }
    else
        ScmVerbose(pState, 4, "treat-as \"%s\"\n", pCfg->pszName);

    if (fIsText || pCfg->fBinary)
    {
        ScmVerboseBanner(pState, 3);

        /*
         * Gather SCM and editor settings from the stream.
         */
        rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
        if (RT_SUCCESS(rc))
        {
            ScmStreamRewindForReading(&Stream1);

            /*
             * Create two more streams for output and push the text thru all the
             * rewriters, switching the two streams around when something is
             * actually rewritten.  Stream1 remains unchanged.
             */
            SCMSTREAM Stream2;
            rc = ScmStreamInitForWriting(&Stream2, &Stream1);
            if (RT_SUCCESS(rc))
            {
                SCMSTREAM Stream3;
                rc = ScmStreamInitForWriting(&Stream3, &Stream1);
                if (RT_SUCCESS(rc))
                {
                    bool        fModified = false;
                    PSCMSTREAM  pIn       = &Stream1;
                    PSCMSTREAM  pOut      = &Stream2;
                    for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
                    {
                        pState->rc = VINF_SUCCESS;
                        bool fRc = pCfg->paRewriters[iRw]->pfnRewriter(pState, pIn, pOut, pBaseSettings);
                        if (RT_FAILURE(pState->rc))
                            break;
                        if (fRc)
                        {
                            PSCMSTREAM pTmp = pOut;
                            pOut = pIn == &Stream1 ? &Stream3 : pIn;
                            pIn  = pTmp;
                            fModified = true;
                        }

                        ScmStreamRewindForReading(pIn);
                        ScmStreamRewindForWriting(pOut);
                    }

                    rc = pState->rc;
                    if (RT_SUCCESS(rc))
                    {
                        rc = ScmStreamGetStatus(&Stream1);
                        if (RT_SUCCESS(rc))
                            rc = ScmStreamGetStatus(&Stream2);
                        if (RT_SUCCESS(rc))
                            rc = ScmStreamGetStatus(&Stream3);
                        if (RT_SUCCESS(rc))
                        {
                            /*
                             * If rewritten, write it back to disk.
                             */
                            if (fModified && !pCfg->fBinary)
                            {
                                if (!g_fDryRun)
                                {
                                    ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
                                    rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
                                    if (RT_FAILURE(rc))
                                        RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
                                }
                                else
                                {
                                    ScmVerboseBanner(pState, 1);
                                    ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol,
                                                   g_fDiffIgnoreLeadingWS, g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars,
                                                   pBaseSettings->cchTab, g_pStdOut);
                                    ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n",
                                               pszFilename, g_pszChangedSuff);
                                }
                                g_cFilesModified++;
                            }
                            else if (fModified)
                                rc = RTMsgErrorRc(VERR_INTERNAL_ERROR, "Rewriters modified binary file! Impossible!");

                            /*
                             * If pending SVN property changes, apply them.
                             */
                            if (pState->cSvnPropChanges && RT_SUCCESS(rc))
                            {
                                if (!g_fDryRun)
                                {
                                    rc = ScmSvnApplyChanges(pState);
                                    if (RT_FAILURE(rc))
                                        RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
                                }
                                else
                                    ScmSvnDisplayChanges(pState);
                                if (!fModified)
                                    g_cFilesModified++;
                            }

                            if (!fModified && !pState->cSvnPropChanges)
                                ScmVerbose(pState, 3, "%s: no change\n", pszFilename);
                        }
                        else
                            RTMsgError("%s: stream error %Rrc\n", pszFilename, rc);
                    }
                    ScmStreamDelete(&Stream3);
                }
                else
                    RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
                ScmStreamDelete(&Stream2);
            }
            else
                RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
        }
        else
            RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
    }
    else
    {
        ScmVerbose(pState, 2, "not text file: \"%s\"\n", pszFilename);
        g_cFilesBinaries++;
    }
    ScmStreamDelete(&Stream1);

    return rc;
}

/**
 * Processes a file.
 *
 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
 * directory recursion method.
 *
 * @returns IPRT status code.
 * @param   pszFilename         The file name.
 * @param   pszBasename         The base name (pointer within @a pszFilename).
 * @param   cchBasename         The length of the base name.  (For passing to
 *                              RTStrSimplePatternMultiMatch.)
 * @param   pSettingsStack      The settings stack (pointer to the top element).
 */
static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
                          PSCMSETTINGS pSettingsStack)
{
    SCMSETTINGSBASE Base;
    int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
    if (RT_SUCCESS(rc))
    {
        SCMRWSTATE State;
        State.pszFilename           = pszFilename;
        State.fFirst                = false;
        State.fNeedsManualRepair    = false;
        State.fIsInSvnWorkingCopy   = 0;
        State.cSvnPropChanges       = 0;
        State.paSvnPropChanges      = NULL;
        State.rc                    = VINF_SUCCESS;

        rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);

        size_t i = State.cSvnPropChanges;
        while (i-- > 0)
        {
            RTStrFree(State.paSvnPropChanges[i].pszName);
            RTStrFree(State.paSvnPropChanges[i].pszValue);
        }
        RTMemFree(State.paSvnPropChanges);

        scmSettingsBaseDelete(&Base);

        if (State.fNeedsManualRepair)
            g_cFilesRequiringManualFixing++;
        g_cFilesProcessed++;
    }
    return rc;
}

/**
 * Tries to correct RTDIRENTRY_UNKNOWN.
 *
 * @returns Corrected type.
 * @param   pszPath             The path to the object in question.
 */
static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
{
    RTFSOBJINFO Info;
    int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
    if (RT_FAILURE(rc))
        return RTDIRENTRYTYPE_UNKNOWN;
    if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
        return RTDIRENTRYTYPE_DIRECTORY;
    if (RTFS_IS_FILE(Info.Attr.fMode))
        return RTDIRENTRYTYPE_FILE;
    return RTDIRENTRYTYPE_UNKNOWN;
}

/**
 * Recurse into a sub-directory and process all the files and directories.
 *
 * @returns IPRT status code.
 * @param   pszBuf              Path buffer containing the directory path on
 *                              entry.  This ends with a dot.  This is passed
 *                              along when recursing in order to save stack space
 *                              and avoid needless copying.
 * @param   cchDir              Length of our path in pszbuf.
 * @param   pEntry              Directory entry buffer.  This is also passed
 *                              along when recursing to save stack space.
 * @param   pSettingsStack      The settings stack (pointer to the top element).
 * @param   iRecursion          The recursion depth.  This is used to restrict
 *                              the recursions.
 */
static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
                                      PSCMSETTINGS pSettingsStack, unsigned iRecursion)
{
    int rc;
    Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');

    /*
     * Make sure we stop somewhere.
     */
    if (iRecursion > 128)
    {
        RTMsgError("recursion too deep: %d\n", iRecursion);
        return VINF_SUCCESS; /* ignore */
    }

    /*
     * Check if it's excluded by --only-svn-dir.
     */
    if (pSettingsStack->Base.fOnlySvnDirs)
    {
        if (!ScmSvnIsDirInWorkingCopy(pszBuf))
            return VINF_SUCCESS;
    }
    g_cDirsProcessed++;

    /*
     * Try open and read the directory.
     */
    RTDIR hDir;
    rc = RTDirOpenFiltered(&hDir, pszBuf, RTDIRFILTER_NONE, 0 /*fFlags*/);
    if (RT_FAILURE(rc))
    {
        RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
        return rc;
    }
    for (;;)
    {
        /* Read the next entry. */
        rc = RTDirRead(hDir, pEntry, NULL);
        if (RT_FAILURE(rc))
        {
            if (rc == VERR_NO_MORE_FILES)
                rc = VINF_SUCCESS;
            else
                RTMsgError("RTDirRead -> %Rrc\n", rc);
            break;
        }

        /* Skip '.' and '..'. */
        if (    pEntry->szName[0] == '.'
            &&  (   pEntry->cbName == 1
                 || (   pEntry->cbName == 2
                     && pEntry->szName[1] == '.')))
            continue;

        /* Enter it into the buffer so we've got a full name to work
           with when needed. */
        if (pEntry->cbName + cchDir >= RTPATH_MAX)
        {
            RTMsgError("Skipping too long entry: %s", pEntry->szName);
            continue;
        }
        memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);

        /* Figure the type. */
        RTDIRENTRYTYPE enmType = pEntry->enmType;
        if (enmType == RTDIRENTRYTYPE_UNKNOWN)
            enmType = scmFigureUnknownType(pszBuf);

        /* Process the file or directory, skip the rest. */
        if (enmType == RTDIRENTRYTYPE_FILE)
            rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
        else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
        {
            /* Append the dot for the benefit of the pattern matching. */
            if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
            {
                RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
                continue;
            }
            memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
            size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;

            if (   !pSettingsStack->Base.pszFilterOutDirs
                || !*pSettingsStack->Base.pszFilterOutDirs
                || (   !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
                                                     pEntry->szName, pEntry->cbName, NULL)
                    && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
                                                     pszBuf, cchSubDir, NULL)
                   )
               )
            {
                rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
                if (RT_SUCCESS(rc))
                {
                    rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
                    scmSettingsStackPopAndDestroy(&pSettingsStack);
                }
            }
        }
        if (RT_FAILURE(rc))
            break;
    }
    RTDirClose(hDir);
    return rc;

}

/**
 * Process a directory tree.
 *
 * @returns IPRT status code.
 * @param   pszDir              The directory to start with.  This is pointer to
 *                              a RTPATH_MAX sized buffer.
 */
static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
{
    /*
     * Setup the recursion.
     */
    int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
    if (RT_SUCCESS(rc))
    {
        RTPathChangeToUnixSlashes(pszDir, true);

        RTDIRENTRY Entry;
        rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
    }
    else
        RTMsgError("RTPathAppend: %Rrc\n", rc);
    return rc;
}


/**
 * Processes a file or directory specified as an command line argument.
 *
 * @returns IPRT status code
 * @param   pszSomething        What we found in the command line arguments.
 * @param   pSettingsStack      The settings stack (pointer to the top element).
 */
static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
{
    char szBuf[RTPATH_MAX];
    int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
    if (RT_SUCCESS(rc))
    {
        RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);

        PSCMSETTINGS pSettings;
        rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
        if (RT_SUCCESS(rc))
        {
            scmSettingsStackPush(&pSettingsStack, pSettings);

            if (RTFileExists(szBuf))
            {
                const char *pszBasename = RTPathFilename(szBuf);
                if (pszBasename)
                {
                    size_t cchBasename = strlen(pszBasename);
                    rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
                }
                else
                {
                    RTMsgError("RTPathFilename: NULL\n");
                    rc = VERR_IS_A_DIRECTORY;
                }
            }
            else
                rc = scmProcessDirTree(szBuf, pSettingsStack);

            PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
            Assert(pPopped == pSettings); RT_NOREF_PV(pPopped);
            scmSettingsDestroy(pSettings);
        }
        else
            RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
    }
    else
        RTMsgError("RTPathAbs: %Rrc\n", rc);
    return rc;
}

/**
 * Print some stats.
 */
static void scmPrintStats(void)
{
    ScmVerbose(NULL, 0,
               g_fDryRun
               ? "%u out of %u file%s in %u dir%s would be modified (%u without rewriter%s, %u binar%s, %u not in svn, %u skipped)\n"
               : "%u out of %u file%s in %u dir%s was modified (%u without rewriter%s, %u binar%s, %u not in svn, %u skipped)\n",
               g_cFilesModified,
               g_cFilesProcessed, g_cFilesProcessed == 1 ? "" : "s",
               g_cDirsProcessed,  g_cDirsProcessed == 1 ? "" : "s",
               g_cFilesNoRewriters, g_cFilesNoRewriters == 1 ? "" : "s",
               g_cFilesBinaries,  g_cFilesBinaries == 1 ? "y" : "ies",
               g_cFilesNotInSvn, g_cFilesSkipped);
}

/**
 * Display the rewriter actions.
 *
 * @returns RTEXITCODE_SUCCESS.
 */
static int scmHelpActions(void)
{
    RTPrintf("Available rewriter actions:\n");
    for (uint32_t i = 0; i < RT_ELEMENTS(g_papRewriterActions); i++)
        RTPrintf("  %s\n", g_papRewriterActions[i]->pszName);
    return RTEXITCODE_SUCCESS;
}

/**
 * Display the default configuration.
 *
 * @returns RTEXITCODE_SUCCESS.
 */
static int scmHelpConfig(void)
{
    RTPrintf("Rewriter configuration:\n");
    for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
    {
        RTPrintf("\n  %s%s - %s:\n",
                 g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].fBinary ? " (binary)" : "", g_aConfigs[iCfg].pszFilePattern);
        for (size_t i = 0; i < g_aConfigs[iCfg].cRewriters; i++)
            RTPrintf("    %s\n", g_aConfigs[iCfg].paRewriters[i]->pszName);
    }
    return RTEXITCODE_SUCCESS;
}

/**
 * Display the primary help text.
 *
 * @returns RTEXITCODE_SUCCESS.
 * @param   paOpts              Options.
 * @param   cOpts               Number of options.
 */
static int scmHelp(PCRTGETOPTDEF paOpts, size_t cOpts)
{
    RTPrintf("VirtualBox Source Code Massager\n"
             "\n"
             "Usage: %s [options] <files & dirs>\n"
             "\n"
             "General options:\n", g_szProgName);
    for (size_t i = 0; i < cOpts; i++)
    {
        /* Grouping. */
        switch (paOpts[i].iShort)
        {
            case SCMOPT_DIFF_IGNORE_EOL:
                RTPrintf("\nDiff options (dry runs):\n");
                break;
            case SCMOPT_CONVERT_EOL:
                RTPrintf("\nRewriter action options:\n");
                break;
            case SCMOPT_ONLY_SVN_DIRS:
                RTPrintf("\nInput selection options:\n");
                break;
            case SCMOPT_TREAT_AS:
                RTPrintf("\nMisc options:\n");
                break;
        }

        size_t cExtraAdvance = 0;
        if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
        {
            cExtraAdvance = i + 1 < cOpts
                         && (   strstr(paOpts[i+1].pszLong, "-no-") != NULL
                             || strstr(paOpts[i+1].pszLong, "-not-") != NULL
                             || strstr(paOpts[i+1].pszLong, "-dont-") != NULL
                             || strstr(paOpts[i+1].pszLong, "-unrestricted-") != NULL
                             || (paOpts[i].iShort == 'q' && paOpts[i+1].iShort == 'v')
                             || (paOpts[i].iShort == 'd' && paOpts[i+1].iShort == 'D')
                            );
            if (cExtraAdvance)
                RTPrintf("  %s, %s\n", paOpts[i].pszLong, paOpts[i + 1].pszLong);
            else if (paOpts[i].iShort != SCMOPT_NO_UPDATE_LICENSE)
                RTPrintf("  %s\n", paOpts[i].pszLong);
            else
            {
                RTPrintf("  %s,\n"
                         "  %s,\n"
                         "  %s,\n"
                         "  %s,\n"
                         "  %s,\n"
                         "  %s,\n"
                         "  %s\n",
                         paOpts[i].pszLong,
                         paOpts[i + 1].pszLong,
                         paOpts[i + 2].pszLong,
                         paOpts[i + 3].pszLong,
                         paOpts[i + 4].pszLong,
                         paOpts[i + 5].pszLong,
                         paOpts[i + 6].pszLong);
                cExtraAdvance = 6;
            }
        }
        else if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
            switch (paOpts[i].iShort)
            {
                case SCMOPT_DEL_ACTION:
                    RTPrintf("  %s pattern\n", paOpts[i].pszLong);
                    break;
                case SCMOPT_FILTER_OUT_DIRS:
                case SCMOPT_FILTER_FILES:
                case SCMOPT_FILTER_OUT_FILES:
                    RTPrintf("  %s multi-pattern\n", paOpts[i].pszLong);
                    break;
                default:
                    RTPrintf("  %s string\n", paOpts[i].pszLong);
            }
        else
            RTPrintf("  %s value\n", paOpts[i].pszLong);
        switch (paOpts[i].iShort)
        {
            case 'd':
            case 'D':                           RTPrintf("      Default: --dry-run\n"); break;
            case SCMOPT_CHECK_RUN:              RTPrintf("      Default: --dry-run\n"); break;
            case 'f':                           RTPrintf("      Default: none\n"); break;
            case 'q':
            case 'v':                           RTPrintf("      Default: -vv\n"); break;
            case SCMOPT_HELP_CONFIG:            RTPrintf("      Shows the standard file rewriter configurations.\n"); break;
            case SCMOPT_HELP_ACTIONS:           RTPrintf("      Shows the available rewriter actions.\n"); break;

            case SCMOPT_DIFF_IGNORE_EOL:        RTPrintf("      Default: false\n"); break;
            case SCMOPT_DIFF_IGNORE_SPACE:      RTPrintf("      Default: false\n"); break;
            case SCMOPT_DIFF_IGNORE_LEADING_SPACE:  RTPrintf("      Default: false\n"); break;
            case SCMOPT_DIFF_IGNORE_TRAILING_SPACE: RTPrintf("      Default: false\n"); break;
            case SCMOPT_DIFF_SPECIAL_CHARS:     RTPrintf("      Default: true\n"); break;

            case SCMOPT_CONVERT_EOL:            RTPrintf("      Default: %RTbool\n", g_Defaults.fConvertEol); break;
            case SCMOPT_CONVERT_TABS:           RTPrintf("      Default: %RTbool\n", g_Defaults.fConvertTabs); break;
            case SCMOPT_FORCE_FINAL_EOL:        RTPrintf("      Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
            case SCMOPT_FORCE_TRAILING_LINE:    RTPrintf("      Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
            case SCMOPT_STRIP_TRAILING_BLANKS:  RTPrintf("      Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
            case SCMOPT_STRIP_TRAILING_LINES:   RTPrintf("      Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
            case SCMOPT_FIX_FLOWER_BOX_MARKERS: RTPrintf("      Default: %RTbool\n", g_Defaults.fFixFlowerBoxMarkers); break;
            case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS: RTPrintf("      Default: %u\n", g_Defaults.cMinBlankLinesBeforeFlowerBoxMakers); break;

            case SCMOPT_FIX_HEADER_GUARDS:
                RTPrintf("      Fix header guards and #pragma once.  Default: %RTbool\n", g_Defaults.fFixHeaderGuards);
                break;
            case SCMOPT_PRAGMA_ONCE:
                RTPrintf("      Whether to include #pragma once with the header guard.  Default: %RTbool\n", g_Defaults.fPragmaOnce);
                break;
            case SCMOPT_FIX_HEADER_GUARD_ENDIF:
                RTPrintf("      Whether to fix the #endif of a header guard.  Default: %RTbool\n", g_Defaults.fFixHeaderGuardEndif);
                break;
            case SCMOPT_ENDIF_GUARD_COMMENT:
                RTPrintf("      Put a comment on the header guard #endif or not.  Default: %RTbool\n", g_Defaults.fEndifGuardComment);
                break;
            case SCMOPT_GUARD_RELATIVE_TO_DIR:
                RTPrintf("      Header guard should be normalized relative to given dir.\n"
                         "      When relative to settings files, no preceeding slash.\n"
                         "      Header relative directory specification: {dir} and {parent}\n"
                         "      If empty no normalization takes place.  Default: '%s'\n", g_Defaults.pszGuardRelativeToDir);
                break;
            case SCMOPT_GUARD_PREFIX:
                RTPrintf("      Prefix to use with --guard-relative-to-dir.  Default: %s\n", g_Defaults.pszGuardPrefix);
                break;
            case SCMOPT_FIX_TODOS:
                RTPrintf("      Fix @todo statements so doxygen sees them.  Default: %RTbool\n", g_Defaults.fFixTodos);
                break;
            case SCMOPT_FIX_ERR_H:
                RTPrintf("      Fix err.h/errcore.h usage.  Default: %RTbool\n", g_Defaults.fFixErrH);
                break;
            case SCMOPT_ONLY_GUEST_HOST_PAGE:
                RTPrintf("      No PAGE_SIZE, PAGE_SHIFT or PAGE_OFFSET_MASK allowed, must have\n"
                         "      GUEST_ or HOST_ prefix.  Also forbids use of PAGE_BASE_MASK,\n"
                         "      PAGE_BASE_HC_MASK, PAGE_BASE_GC_MASK, PAGE_ADDRESS,\n"
                         "      PHYS_PAGE_ADDRESS.  Default: %RTbool\n", g_Defaults.fOnlyGuestHostPage);
                break;
            case SCMOPT_NO_ASM_MEM_PAGE_USE:
                RTPrintf("      No ASMMemIsZeroPage or ASMMemZeroPage allowed, must instead use\n"
                         "      ASMMemIsZero and RT_BZERO with appropriate page size.  Default: %RTbool\n",
                         g_Defaults.fNoASMMemPageUse);
                break;
            case SCMOPT_NO_RC_USE:
                RTPrintf("      No rc declaration allowed, must instead use\n"
                         "      vrc for IPRT status codes and hrc for COM status codes.  Default: %RTbool\n",
                         g_Defaults.fOnlyHrcVrcInsteadOfRc);
                break;
            case SCMOPT_UPDATE_COPYRIGHT_YEAR:
                RTPrintf("      Update the copyright year.  Default: %RTbool\n", g_Defaults.fUpdateCopyrightYear);
                break;
            case SCMOPT_EXTERNAL_COPYRIGHT:
                RTPrintf("      Only external copyright holders.  Default: %RTbool\n", g_Defaults.fExternalCopyright);
                break;
            case SCMOPT_NO_UPDATE_LICENSE:
                RTPrintf("      License selection.  Default: --license-ose-gpl\n");
                break;

            case SCMOPT_LGPL_DISCLAIMER:
                RTPrintf("      Include LGPL version disclaimer.  Default: --no-lgpl-disclaimer\n");
                break;

            case SCMOPT_SET_SVN_EOL:            RTPrintf("      Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
            case SCMOPT_SET_SVN_EXECUTABLE:     RTPrintf("      Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
            case SCMOPT_SET_SVN_KEYWORDS:       RTPrintf("      Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
            case SCMOPT_SKIP_SVN_SYNC_PROCESS:  RTPrintf("      Default: %RTbool\n", g_Defaults.fSkipSvnSyncProcess); break;
            case SCMOPT_SKIP_UNICODE_CHECKS:    RTPrintf("      Default: %RTbool\n", g_Defaults.fSkipUnicodeChecks); break;
            case SCMOPT_TAB_SIZE:               RTPrintf("      Default: %u\n", g_Defaults.cchTab); break;
            case SCMOPT_WIDTH:                  RTPrintf("      Default: %u\n", g_Defaults.cchWidth); break;

            case SCMOPT_ONLY_SVN_DIRS:          RTPrintf("      Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
            case SCMOPT_ONLY_SVN_FILES:         RTPrintf("      Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
            case SCMOPT_FILTER_OUT_DIRS:        RTPrintf("      Default: %s\n", g_Defaults.pszFilterOutDirs); break;
            case SCMOPT_FILTER_FILES:           RTPrintf("      Default: %s\n", g_Defaults.pszFilterFiles); break;
            case SCMOPT_FILTER_OUT_FILES:       RTPrintf("      Default: %s\n", g_Defaults.pszFilterOutFiles); break;

            case SCMOPT_TREAT_AS:
                RTPrintf("      For treat the input file(s) differently, restting any --add-action.\n"
                         "      If the value is empty defaults will be used again.  Possible values:\n");
                for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
                    RTPrintf("          %s (%s)\n", g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].pszFilePattern);
                break;

            case SCMOPT_ADD_ACTION:
                RTPrintf("      Adds a rewriter action.  The first use after a --treat-as will copy and\n"
                         "      the action list selected by the --treat-as.  The action list will be\n"
                         "      flushed by --treat-as.\n");
                break;

            case SCMOPT_DEL_ACTION:
                RTPrintf("      Deletes one or more rewriter action (pattern). Best used after\n"
                         "      a --treat-as.\n");
                break;

            default: AssertMsgFailed(("i=%d %d %s\n", i, paOpts[i].iShort, paOpts[i].pszLong));
        }
        i += cExtraAdvance;
    }

    return RTEXITCODE_SUCCESS;
}

int main(int argc, char **argv)
{
    int rc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(rc))
        return 1;

    /*
     * Init the current year.
     */
    RTTIMESPEC  Now;
    RTTIME      Time;
    RTTimeExplode(&Time, RTTimeNow(&Now));
    g_uYear = Time.i32Year;

    /*
     * Init the settings.
     */
    PSCMSETTINGS pSettings;
    rc = scmSettingsCreate(&pSettings, &g_Defaults);
    if (RT_FAILURE(rc))
    {
        RTMsgError("scmSettingsCreate: %Rrc\n", rc);
        return 1;
    }

    /*
     * Parse arguments and process input in order (because this is the only
     * thing that works at the moment).
     */
    static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
    {
        { "--dry-run",                          'd',                                    RTGETOPT_REQ_NOTHING },
        { "--real-run",                         'D',                                    RTGETOPT_REQ_NOTHING },
        { "--check-run",                        SCMOPT_CHECK_RUN,                       RTGETOPT_REQ_NOTHING },
        { "--file-filter",                      'f',                                    RTGETOPT_REQ_STRING  },
        { "--quiet",                            'q',                                    RTGETOPT_REQ_NOTHING },
        { "--verbose",                          'v',                                    RTGETOPT_REQ_NOTHING },
        { "--diff-ignore-eol",                  SCMOPT_DIFF_IGNORE_EOL,                 RTGETOPT_REQ_NOTHING },
        { "--diff-no-ignore-eol",               SCMOPT_DIFF_NO_IGNORE_EOL,              RTGETOPT_REQ_NOTHING },
        { "--diff-ignore-space",                SCMOPT_DIFF_IGNORE_SPACE,               RTGETOPT_REQ_NOTHING },
        { "--diff-no-ignore-space",             SCMOPT_DIFF_NO_IGNORE_SPACE,            RTGETOPT_REQ_NOTHING },
        { "--diff-ignore-leading-space",        SCMOPT_DIFF_IGNORE_LEADING_SPACE,       RTGETOPT_REQ_NOTHING },
        { "--diff-no-ignore-leading-space",     SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,    RTGETOPT_REQ_NOTHING },
        { "--diff-ignore-trailing-space",       SCMOPT_DIFF_IGNORE_TRAILING_SPACE,      RTGETOPT_REQ_NOTHING },
        { "--diff-no-ignore-trailing-space",    SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,   RTGETOPT_REQ_NOTHING },
        { "--diff-special-chars",               SCMOPT_DIFF_SPECIAL_CHARS,              RTGETOPT_REQ_NOTHING },
        { "--diff-no-special-chars",            SCMOPT_DIFF_NO_SPECIAL_CHARS,           RTGETOPT_REQ_NOTHING },
    };
    memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));

    bool            fCheckRun = false;
    RTGETOPTUNION   ValueUnion;
    RTGETOPTSTATE   GetOptState;
    rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
    AssertReleaseRCReturn(rc, 1);

    while (   (rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0
           && rc != VINF_GETOPT_NOT_OPTION)
    {
        switch (rc)
        {
            case 'd':
                g_fDryRun = true;
                fCheckRun = false;
                break;
            case 'D':
                g_fDryRun = fCheckRun = false;
                break;
            case SCMOPT_CHECK_RUN:
                g_fDryRun = fCheckRun = true;
                break;

            case 'f':
                g_pszFileFilter = ValueUnion.psz;
                break;

            case 'h':
                return scmHelp(s_aOpts, RT_ELEMENTS(s_aOpts));

            case SCMOPT_HELP_CONFIG:
                return scmHelpConfig();

            case SCMOPT_HELP_ACTIONS:
                return scmHelpActions();

            case 'q':
                g_iVerbosity = 0;
                break;

            case 'v':
                g_iVerbosity++;
                break;

            case 'V':
            {
                /* The following is assuming that svn does it's job here. */
                static const char s_szRev[] = "$Revision: 153224 $";
                const char *psz = RTStrStripL(strchr(s_szRev, ' '));
                RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
                return 0;
            }

            case SCMOPT_DIFF_IGNORE_EOL:
                g_fDiffIgnoreEol = true;
                break;
            case SCMOPT_DIFF_NO_IGNORE_EOL:
                g_fDiffIgnoreEol = false;
                break;

            case SCMOPT_DIFF_IGNORE_SPACE:
                g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
                break;
            case SCMOPT_DIFF_NO_IGNORE_SPACE:
                g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
                break;

            case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
                g_fDiffIgnoreLeadingWS = true;
                break;
            case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
                g_fDiffIgnoreLeadingWS = false;
                break;

            case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
                g_fDiffIgnoreTrailingWS = true;
                break;
            case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
                g_fDiffIgnoreTrailingWS = false;
                break;

            case SCMOPT_DIFF_SPECIAL_CHARS:
                g_fDiffSpecialChars = true;
                break;
            case SCMOPT_DIFF_NO_SPECIAL_CHARS:
                g_fDiffSpecialChars = false;
                break;

            default:
            {
                int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion, "/", 1);
                if (RT_SUCCESS(rc2))
                    break;
                if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
                    return 2;
                return RTGetOptPrintError(rc, &ValueUnion);
            }
        }
    }

    /*
     * Process non-options.
     */
    RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
    if (rc == VINF_GETOPT_NOT_OPTION)
    {
        ScmSvnInit();

        bool fWarned = g_fDryRun;
        while (rc == VINF_GETOPT_NOT_OPTION)
        {
            if (!fWarned)
            {
                RTPrintf("%s: Warning! This program will make changes to your source files and\n"
                         "%s:          there is a slight risk that bugs or a full disk may cause\n"
                         "%s:          LOSS OF DATA.   So, please make sure you have checked in\n"
                         "%s:          all your changes already.  If you didn't, then don't blame\n"
                         "%s:          anyone for not warning you!\n"
                         "%s:\n"
                         "%s:          Press any key to continue...\n",
                         g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
                         g_szProgName, g_szProgName);
                RTStrmGetCh(g_pStdIn);
                fWarned = true;
            }

            rc = scmProcessSomething(ValueUnion.psz, pSettings);
            if (RT_FAILURE(rc))
            {
                rcExit = RTEXITCODE_FAILURE;
                break;
            }

            /* next */
            rc = RTGetOpt(&GetOptState, &ValueUnion);
            if (RT_FAILURE(rc))
                rcExit = RTGetOptPrintError(rc, &ValueUnion);
        }

        scmPrintStats();
        ScmSvnTerm();
    }
    else
        RTMsgWarning("No files or directories specified. Doing nothing");

    scmSettingsDestroy(pSettings);

    /* If we're in checking mode, fail if any files needed modification. */
    if (   rcExit == RTEXITCODE_SUCCESS
        && fCheckRun
        && g_cFilesModified > 0)
    {
        RTMsgError("Checking mode failed! %u file%s needs modifications", g_cFilesBinaries, g_cFilesBinaries > 1 ? "s" : "");
        rcExit = RTEXITCODE_FAILURE;
    }

    /* Fail if any files require manual repair. */
    if (g_cFilesRequiringManualFixing > 0)
    {
        RTMsgError("%u file%s needs manual modifications", g_cFilesRequiringManualFixing,
                   g_cFilesRequiringManualFixing > 1 ? "s" : "");
        if (rcExit == RTEXITCODE_SUCCESS)
            rcExit = RTEXITCODE_FAILURE;
    }

    return rcExit;
}