summaryrefslogtreecommitdiffstats
path: root/src/fastdep/fastdep.c
blob: 72d918ebeef842ccac7ee091330ff5d555f3bfc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
/* $Id: fastdep.c 2413 2010-09-11 17:43:04Z bird $
 *
 * Fast dependents. (Fast = Quick and Dirty!)
 *
 * Copyright (c) 1999-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
 *
 * GPL
 *
 */

/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
#define INCL_DOSERRORS
#define INCL_FILEMGR
#define INCL_DOSMISC


/*
 * Size of the \n charater (forget '\r').
 * If you're compiling this under a UNICODE system this may perhaps change,
 * but I doubd that fastdep will work at all under a UNICODE system. ;-)
 */
#if defined(UNICODE) && !defined(__WIN32OS2__)
#define CBNEWLINE       (2)
#else
#define CBNEWLINE       (1)
#endif


/*
 * Time stamp size.
 */
#define TS_SIZE         (48)


/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#if defined(OS2FAKE)
#include "os2fake.h"
#else
#include <os2.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <assert.h>

#include "avl.h"

#ifdef __WIN32OS2__
#   define WIN32API
#   include <odinbuild.h>
#else
#   define ODIN32_BUILD_NR -1
#endif

#ifndef INLINE
#   if defined(__IBMC__)
#       define INLINE _Inline
#   elif defined(__IBMCPP__)
#       define INLINE inline
#   elif defined(__WATCOMC__)
#       define INLINE __inline
#   elif defined(__WATCOM_CPLUSPLUS__)
#       define INLINE inline
#   else
#       error message("unknown compiler - inline keyword unknown!")
#   endif
#endif

/*
 * This following section is used while testing fastdep.
 * stdio.h should be included; string.h never included.
 */
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
*/

#if 1
#include <stdio.h>
#else
#include <string.h>
#include <string.h>
#endif

/*
 */ /* */ /*
#include <string.h>
 */
#if 1
#    if 1
        #if 0
# include <string.h>
        #else
#            if 1
                #if 1
                    #if 0
# include <string.h>
                    #else /* */ /*
*/
 # include <stdio.h>
                    #endif
                #endif
            #endif
        #endif
    #endif
#endif

/*******************************************************************************
*   Structures and Typedefs                                                    *
*******************************************************************************/
typedef struct _Options
{
    const char *    pszInclude;
    const char *    pszExclude;
    BOOL            fExcludeAll;
    const char *    pszObjectExt;
    const char *    pszObjectDir;
    BOOL            fObjectDir;         /* replace object directory? */
    const char *    pszRsrcExt;
    BOOL            fObjRule;
    BOOL            fNoObjectPath;
    BOOL            fSrcWhenObj;
    BOOL            fAppend;            /* append to the output file, not overwrite it. */
    BOOL            fCheckCyclic;       /* allways check for cylic dependency before inserting an dependent. */
    BOOL            fCacheSearchDirs;   /* cache entire search dirs. */
    const char *    pszExcludeFiles;    /* List of excluded files. */
    BOOL            fForceScan;         /* Force scan of all files. */
} OPTIONS, *POPTIONS;


/*
 * Language specific analysis functions type.
 */
typedef int ( _FNLANG)  (const char *pszFilename, const char *pszNormFilename,
                         const char *pszTS, BOOL fHeader, void **ppvRule);
typedef _FNLANG    *PFNLANG;


/**
 * This struct holds the static configuration of the util.
 */
typedef struct _ConfigEntry
{
    char         szId[16];              /* Config ID. */
    const char **papszExts;             /* Pointer to an array of pointer to extentions for this handler. */
                                        /* If NULL this is the last entry. */
    int          iFirstHdr;             /* Index into the papszExts array of the first headerfile/copybook. */
                                        /* Set it to the NULL element of the array if no headers for this extention. */
                                        /* A non-header file may get a object rule. */
    PFNLANG      pfn;                   /* Pointer to handler function. */
    char        *pszzAddDeps;           /* Pointer to an string of string of additional dependencies. */
} CONFIGENTRY, *PCONFIGENTRY;


/**
 * Dependant Rule
 */
typedef struct _DepRule
{
    AVLNODECORE     avlCore;
    char *          pszRule;            /* Pointer to rule name */
    int             cDeps;              /* Entries in the dependant array. */
    char **         papszDep;           /* Pointer to an array of pointers to dependants. */
    BOOL            fUpdated;           /* If we have updated this entry during the run. */
    char            szTS[TS_SIZE];      /* Time stamp. */
} DEPRULE, *PDEPRULE;


/**
 * Filename cache entry.
 */
#define FCACHEENTRY     AVLNODECORE
#define PFCACHEENTRY    PAVLNODECORE


/*******************************************************************************
*   Internal Functions                                                         *
*******************************************************************************/
static void syntax(void);
static int makeDependent(const char *pszFilename, const char *pszTS);

static int langC_CPP(const char *pszFilename, const char *pszNormFilename, const char *pszTS, BOOL fHeader, void **ppvRule);
static int langAsm(  const char *pszFilename, const char *pszNormFilename, const char *pszTS, BOOL fHeader, void **ppvRule);
static int langRC(   const char *pszFilename, const char *pszNormFilename, const char *pszTS, BOOL fHeader, void **ppvRule);
static int langCOBOL(const char *pszFilename, const char *pszNormFilename, const char *pszTS, BOOL fHeader, void **ppvRule);
static int langIPF(  const char *pszFilename, const char *pszNormFilename, const char *pszTS, BOOL fHeader, void **ppvRule);


/* string operations */
static int strnicmpwords(const char *pszS1, const char *pszS2, int cch);

/* file operations */
static char *fileNormalize(char *pszFilename);
static char *fileNormalize2(const char *pszFilename, char *pszBuffer);
       char *filePath(const char *pszFilename, char *pszBuffer);
static char *filePathSlash(const char *pszFilename, char *pszBuffer);
static char *filePathSlash2(const char *pszFilename, char *pszBuffer);
static char *fileName(const char *pszFilename, char *pszBuffer);
static char *fileNameNoExt(const char *pszFilename, char *pszBuffer);
static char *fileExt(const char *pszFilename, char *pszBuffer);

/* filecache operations */
static BOOL filecacheAddFile(const char *pszFilename);
static BOOL filecacheAddDir(const char *pszDir);
INLINE BOOL filecacheFind(const char *pszFilename);
INLINE BOOL filecacheIsDirCached(const char *pszDir);
static char*filecacheFileExist(const char *pszFilename, char *pszBuffer);

/* pathlist operations */
static char *pathlistFindFile(const char *pszPathList, const char *pszFilename, char *pszBuffer);
static BOOL  pathlistFindFile2(const char *pszPathList, const char *pszFilename);

/* word operations */
static char *findEndOfWord(char *psz);
#if 0 /* not used */
static char *findStartOfWord(char *psz, const char *pszStart);
#endif

/* file helpers */
static signed long fsize(FILE *phFile);

/* text helpers */
INLINE char *trim(char *psz);
INLINE char *trimR(char *psz);
INLINE char *trimQuotes(char *psz);

/* preprocessors */
static char *PreProcessLine(char *pszOut, const char *pszIn);

/* textbuffer */
static void *textbufferCreate(const char *pszFilename);
static void  textbufferDestroy(void *pvBuffer);
static char *textbufferNextLine(void *pvBuffer, char *psz);
static char *textbufferGetNextLine(void *pvBuffer, void **ppv, char *pszLineBuffer, int cchLineBuffer);

/* depend workers */
static BOOL  depReadFile(const char *pszFilename, BOOL fAppend);
static BOOL  depWriteFile(const char *pszFilename, BOOL fWriteUpdatedOnly);
static void  depRemoveAll(void);
static void *depAddRule(const char *pszRulePath, const char *pszName, const char *pszExt, const char *pszTS, BOOL fConvertName);
static BOOL  depAddDepend(void *pvRule, const char *pszDep, BOOL fCheckCyclic, BOOL fConvertName);
static int   depNameToReal(char *pszName);
static int   depNameToMake(char *pszName, int cchName, const char *pszSrc);
static void  depMarkNotFound(void *pvRule);
static BOOL  depCheckCyclic(PDEPRULE pdepRule, const char *pszDep);
static BOOL  depValidate(PDEPRULE pdepRule);
INLINE char *depMakeTS(char *pszTS, PFILEFINDBUF3 pfindbuf3);
static void  depAddSrcAddDeps(void *pvRule, const char *pszz);


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
/*
 * Pointer to the list of dependencies.
 */
static PDEPRULE pdepTree = NULL;


/*
 * Filecache - tree starts here.
 */
static PFCACHEENTRY pfcTree = NULL;
static unsigned     cfcNodes = 0;
static PFCACHEENTRY pfcDirTree = NULL;


/*
 * Current directory stuff
 */
static char     szCurDir[CCHMAXPATH];
static int      aiSlashes[CCHMAXPATH];
static int      cSlashes;


/*
 * Environment variables used.
 * (These has the correct case.)
 */
static char *   pszIncludeEnv;


/*
 * Configuration stuff.
 */
static const char pszDefaultDepFile[] = ".depend";
static const char *apszExtC_CPP[] = {"c", "sqc", "cpp", "h", "hpp", NULL};
static const char *apszExtAsm[]   = {"asm", "inc", NULL};
static const char *apszExtRC[]    = {"rc",  "dlg", NULL};
static const char *apszExtORC[]   = {"orc", "dlg", NULL};
static const char *apszExtCOBOL[] = {"cbl", "cob", "sqb", "wbl", NULL};
static const char *apszExtIPF[]   = {"ipf", "man", NULL};
static const char *apszExtIPP[]   = {"ipp", NULL};
static CONFIGENTRY aConfig[] =
{
    {
        "CX",
        apszExtC_CPP,
        3,
        langC_CPP,
        NULL,
    },

    {
        "AS",
        apszExtAsm,
        1,
        langAsm,
        NULL,
    },

    {
        "RC",
        apszExtRC,
        1,
        langRC,
        NULL,
    },

    {
        "ORC",
        apszExtORC,
        1,
        langRC,
        NULL,
    },

    {
        "COB",
        apszExtCOBOL,
        -1,
        langCOBOL,
        NULL,
    },

    {
        "IPF",
        apszExtIPF,
        -1,
        langIPF,
        NULL,
    },

    {
        "IPP",
        apszExtIPP,
        -1,
        langC_CPP,
        NULL,
    },

    /* terminating entry */
    {
        "",
        NULL,
        -1,
        NULL,
        NULL
    }
};


static char szObjectDir[CCHMAXPATH];
static char szObjectExt[64] = "obj";
static char szRsrcExt[64]   = "res";
static char szInclude[32768] = ";";
static char szExclude[32768] = ";";
static char szExcludeFiles[65536] = "";

OPTIONS options =
{
    szInclude,       /* pszInclude */
    szExclude,       /* pszExclude */
    FALSE,           /* fExcludeAll */
    szObjectExt,     /* pszObjectExt */
    szObjectDir,     /* pszObjectDir */
    FALSE,           /* fObjectDir */
    szRsrcExt,       /* pszRsrcExt */
    TRUE,            /* fObjRule */
    FALSE,           /* fNoObjectPath */
    TRUE,            /* fSrcWhenObj */
    FALSE,           /* fAppend */
    TRUE,            /* fCheckCyclic */
    TRUE,            /* fCacheSearchDirs */
    szExcludeFiles,  /* pszExcludeFiles */
    FALSE            /* fForceScan */
};


/**
 * Main function.
 * @returns   0 on success.
 *           -n count of failiures.
 * @param
 * @param
 * @equiv
 * @precond
 * @methdesc
 * @result
 * @time
 * @sketch
 * @algo
 * @remark
 */
int main(int argc, char **argv)
{
    int         rc   = 0;
    int         argi = 1;
    int         i;
    char *      psz;
    char *      psz2;
    const char *pszDepFile = pszDefaultDepFile;
    char        achBuffer[4096];

    szObjectDir[0] = '\0';

    if (argc == 1)
    {
        syntax();
        return -87;
    }

    /*
     * Initiate current directory stuff
     */
    if (_getcwd(szCurDir, sizeof(szCurDir)) == NULL)
    {
        fprintf(stderr, "fatal error: failed to get current directory\n");
        return -88;
    }
    strlwr(szCurDir);
    aiSlashes[0] = 0;
    for (i = 1, cSlashes; szCurDir[i] != '\0'; i++)
    {
        if (szCurDir[i] == '/')
            szCurDir[i] = '\\';
        if (szCurDir[i] == '\\')
            aiSlashes[cSlashes++] = i;
    }
    if (szCurDir[i-1] != '\\')
    {
        aiSlashes[cSlashes] = i;
        szCurDir[i++] = '\\';
        szCurDir[i] = '\0';
    }


    /*
     * Initiate environment variables used: INCLUDE
     */
    psz = getenv("INCLUDE");
    if (psz != NULL)
    {
        pszIncludeEnv = strdup(psz);
        strlwr(pszIncludeEnv);
    }
    else
        pszIncludeEnv = "";


    /*
     * Disable hard errors.
     */
    DosError(FERR_DISABLEHARDERR | FERR_ENABLEEXCEPTION);


    /*
     * parse arguments
     */
    while (argi < argc)
    {
        if (argv[argi][0] == '-' || argv[argi][0] == '/')
        {
            /* parameters */
            switch (argv[argi][1])
            {
                case 'A':
                case 'a': /* Append to the output file */
                    options.fAppend = argv[argi][2] != '-';
                    break;

                case 'D':
                case 'd': /* "-d <filename>" */
                {
                    const char *pszOld = pszDepFile;
                    if (argv[argi][2] != '\0')
                        pszDepFile = &argv[argi][2];
                    else
                    {
                        argi++;
                        if (argi < argc)
                            pszDepFile = argv[argi];
                        else
                        {
                            fprintf(stderr, "invalid parameter -d, filename missing!\n");
                            return -1;
                        }
                    }

                    /* if dependencies are generated we'll flush them to the old filename */
                    if (pdepTree != NULL && pszOld != pszDepFile)
                    {
                        if (!depWriteFile(pszOld, !options.fAppend))
                            fprintf(stderr, "error: failed to write (flush) dependencies.\n");
                        depRemoveAll();
                    }
                    break;
                }

                case 'C': /* forced directory cache  'ca' or cylic check 'cy'*/
                case 'c':
                    if (argv[argi][2] == 'a' || argv[argi][2] == 'A')
                        options.fCacheSearchDirs = TRUE;
                    else if ((argv[argi][2] == 'y' || argv[argi][2] == 'Y'))
                        options.fCheckCyclic = argv[argi][3] != '-';
                    break;

                case 'E': /* list of paths. If a file is found in one of these directories the */
                case 'e': /* filename will be used without the directory path. */
                    /* Eall<[+]|-> ? */
                    if (strlen(&argv[argi][1]) <= 5 && strnicmp(&argv[argi][1], "Eall", 4) == 0)
                    {
                        options.fExcludeAll = argv[argi][5] != '-';
                        break;
                    }
                    /* path or path list */
                    if (strlen(argv[argi]) > 2)
                        psz = &argv[argi][2];
                    else
                    {
                        if (++argi >= argc)
                        {
                            fprintf(stderr, "syntax error! Option -e.\n");
                            return 1;
                        }
                        psz = argv[argi];
                    }
                    /* check if enviroment variable */
                    if (*psz == '%')
                    {
                        psz2 = strdup(psz+1);
                        if (psz2 != NULL && *psz2 != '\0')
                        {
                            if (psz2[strlen(psz2)-1] == '%')
                                psz2[strlen(psz2)-1] = '\0';
                            psz = getenv(psz2);
                            free(psz2);
                            if (psz == NULL)
                                break;
                        }
                        else
                        {
                            fprintf(stderr, "error: -E% is not an valid argument!\n");
                            return -1;
                        }
                    }
                    if (psz != NULL)
                    {
                        strcat(szExclude, psz);
                        strlwr(szExclude);
                        if (szExclude[strlen(szExclude)-1] != ';')
                            strcat(szExclude, ";");
                    }
                    break;

                case 'f':
                case 'F': /* force scan of all files. */
                    options.fForceScan = argv[argi][2] != '-';
                    break;

                case 'I': /* optional include path. This has precedence over the INCLUDE environment variable. */
                case 'i':
                    if (strlen(argv[argi]) > 2)
                        psz = &argv[argi][2];
                    else
                    {
                        if (++argi >= argc)
                        {
                            fprintf(stderr, "syntax error! Option -i.\n");
                            return 1;
                        }
                        psz = argv[argi];
                    }
                    /* check if enviroment variable */
                    if (*psz == '%')
                    {
                        psz2 = strdup(psz+1);
                        if (psz2 != NULL && *psz2 != '\0')
                        {
                            if (psz2[strlen(psz2)-1] == '%')
                                psz2[strlen(psz2)-1] = '\0';
                            psz = getenv(psz2);
                            free(psz2);
                            if (psz == NULL)
                                break;
                        }
                        else
                        {
                            fprintf(stderr, "error: -I% is not an valid argument!\n");
                            return -1;
                        }
                    }
                    if (psz != NULL)
                    {
                        strcat(szInclude, psz);
                        strlwr(szInclude);
                        if (szInclude[strlen(szInclude)-1] != ';')
                            strcat(szInclude, ";");
                    }
                    break;

                case 'n': /* no object path , -N<[+]|-> */
                case 'N':
                    if (strlen(argv[argi]) <= 1+1+1)
                        options.fNoObjectPath = argv[argi][2] != '-';
                    else
                    {
                        fprintf(stderr, "error: invalid parameter!, '%s'\n", argv[argi]);
                        return -1;
                    }
                    break;

                case 'o': /* object base directory, Obj or Obr<[+]|-> */
                case 'O':
                    if (strlen(&argv[argi][1]) <= 4 && strnicmp(&argv[argi][1], "Obr", 3) == 0)
                    {
                        options.fObjRule = argv[argi][4] != '-';
                        break;
                    }

                    if (strlen(&argv[argi][1]) >= 4 && strnicmp(&argv[argi][1], "Obj", 3) == 0)
                    {
                        if (strlen(argv[argi]) > 4)
                            strcpy(szObjectExt, argv[argi]+4);
                        else
                        {
                            if (++argi >= argc)
                            {
                                fprintf(stderr, "syntax error! Option -obj.\n");
                                return 1;
                            }
                            strcpy(szObjectExt, argv[argi]);
                        }
                        break;
                    }

                    /* path: -o or -o- */
                    options.fObjectDir = TRUE;
                    if (strlen(argv[argi]) > 2)
                    {
                        if (argv[argi][2] == '-')  /* no object path */
                            szObjectDir[0] = '\0';
                        else
                            strcpy(szObjectDir, argv[argi]+2);
                    }
                    else
                    {
                        if (++argi >= argc)
                        {
                            fprintf(stderr, "syntax error! Option -o.\n");
                            return 1;
                        }
                        strcpy(szObjectDir, argv[argi]);
                    }
                    if (szObjectDir[0] != '\0'
                        && szObjectDir[strlen(szObjectDir)-1] != '\\'
                        && szObjectDir[strlen(szObjectDir)-1] != '/'
                        )
                        strcat(szObjectDir, "\\");
                    break;

                case 'r':
                case 'R':
                    if (strlen(argv[argi]) > 2)
                        strcpy(szRsrcExt, argv[argi]+2);
                    else
                    {
                        if (++argi >= argc)
                        {
                            fprintf(stderr, "syntax error! Option -r.\n");
                            return 1;
                        }
                        strcpy(szRsrcExt, argv[argi]);
                    }
                    break;

                case 's':
                case 'S':
                    if (!strnicmp(argv[argi]+1, "srcadd", 6))
                    {
                        if (strlen(argv[argi]) > 7)
                            psz = argv[argi]+2;
                        else
                        {
                            if (++argi >= argc)
                            {
                                fprintf(stderr, "syntax error! Option -srcadd.\n");
                                return 1;
                            }
                            psz = argv[argi];
                        }
                        if (!(psz2 = strchr(psz, ':')))
                        {
                            fprintf(stderr, "syntax error! Option -srcadd malformed!\n");
                            return 1;
                        }
                        for (i = 0; aConfig[i].pfn; i++)
                        {
                            if (    !strnicmp(aConfig[i].szId, psz, psz2 - psz)
                                &&  !aConfig[i].szId[psz2 - psz])
                            {
                                int cch, cch2;
                                if (!*++psz2)
                                {
                                    fprintf(stderr, "error: Option -srcadd no additioanl dependancy!\n",
                                            psz2 - psz, psz);
                                    return 1;
                                }
                                cch = 0;
                                psz = aConfig[i].pszzAddDeps;
                                if (psz)
                                {
                                    do
                                    {
                                        cch += (cch2 = strlen(psz)) + 1;
                                        psz += cch2 + 1;
                                    } while (*psz);
                                }
                                cch2 = strlen(psz2);
                                aConfig[i].pszzAddDeps = realloc(aConfig[i].pszzAddDeps, cch + cch2 + 2);
                                if (!aConfig[i].pszzAddDeps)
                                {
                                    fprintf(stderr, "error: Out of memory!\n");
                                    return 1;
                                }
                                strcpy(aConfig[i].pszzAddDeps + cch, psz2);
                                aConfig[i].pszzAddDeps[cch + cch2 + 1] = '\0';
                                psz = NULL;
                                break;
                            }
                        }
                        if (psz)
                        {
                            fprintf(stderr, "error: Option -srcadd, invalid language id '%.*s%'\n",
                                    psz2 - psz, psz);
                            return 1;
                        }
                    }
                    else
                    {
                        fprintf(stderr, "syntax error! Invalid option %s\n", argv[argi]);
                        return 1;
                    }
                    break;

                case 'x':
                case 'X': /* Exclude files */
                    psz = &achBuffer[CCHMAXPATH+8];
                    if (strlen(argv[argi]) > 2)
                        strcpy(psz, &argv[argi][2]);
                    else
                    {
                        if (++argi >= argc)
                        {
                            fprintf(stderr, "syntax error! Option -x.\n");
                            return 1;
                        }
                        strcpy(psz, argv[argi]);
                    }
                    while (psz != NULL && *psz != ';')
                    {
                        char *  pszNext = strchr(psz, ';');
                        int     cch = strlen(szExcludeFiles);
                        if (pszNext)
                            *pszNext++ = '\0';
                        if (DosQueryPathInfo(psz, FIL_QUERYFULLNAME, &szExcludeFiles[cch], CCHMAXPATH))
                        {
                            fprintf(stderr, "error: Invalid exclude name\n");
                            return -1;
                        }
                        strlwr(&szExcludeFiles[cch]);
                        strcat(&szExcludeFiles[cch], ";");
                        psz = pszNext;
                    }
                    break;

                case 'h':
                case 'H':
                case '?':
                    syntax();
                    return 1;

                default:
                    fprintf(stderr, "error: invalid parameter! '%s'\n", argv[argi]);
                    return -1;
            }

        }
        else if (argv[argi][0] == '@')
        {   /*
             * Parameter file (debugger parameter length restrictions led to this):
             *    Create a textbuffer.
             *    Parse the file and create a new parameter vector.
             *    Set argv to the new parameter vector, argi to 0 and argc to
             *    the parameter count.
             *    Restrictions: Parameters enclosed in "" is not implemented.
             *                  No commandline parameters are processed after the @file
             */
            char *pszBuffer = (char*)textbufferCreate(&argv[argi][1]); /* !ASSUMS! that pvBuffer is the file string! */
            if (pszBuffer != NULL)
            {
                char **apszArgs = NULL;
                char *psz = pszBuffer;
                int  i = 0;

                while (*psz != '\0')
                {
                    /* find end of parameter word */
                    char *pszEnd = psz + 1;
                    char  ch = *pszEnd;
                    while (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\0')
                        ch = *++pszEnd;

                    /* allocate more arg array space? */
                    if ((i % 512) == 0)
                    {
                        apszArgs = realloc(apszArgs, sizeof(char*) * 512);
                        if (apszArgs == NULL)
                        {
                            fprintf(stderr, "error: out of memory. (line=%d)\n", __LINE__);
                            return -8;
                        }
                    }
                    *pszEnd = '\0';
                    apszArgs[i++] = psz;

                    /* next */
                    psz = pszEnd + 1;
                    ch = *psz;
                    while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
                        ch = *++psz;
                }

                argc = i;
                argi = 0;
                argv = apszArgs;
                continue;
            }
            else
            {
                fprintf(stderr, "error: could not open parameter file\n");
                return -1;
            }
        }
        else
        {   /* not a parameter! */
            ULONG           ulRc;
            PFILEFINDBUF3   pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
            HDIR            hDir = HDIR_CREATE;
            ULONG           cFiles = ~0UL;
            int             i;


            /*
             * If append option is or if the forcescan option isn't is
             * we'll have to read the existing dep file before starting
             * adding new dependencies.
             */
            if (pdepTree == NULL && (options.fAppend || !options.fForceScan))
                depReadFile(pszDepFile, options.fAppend);

            /*
             * Search for the files specified.
             */
            ulRc = DosFindFirst(argv[argi], &hDir,
                                FILE_READONLY |  FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED,
                                pfindbuf3, sizeof(achBuffer), &cFiles, FIL_STANDARD);
            if (!options.fCacheSearchDirs)
                options.fCacheSearchDirs = cFiles > 25;
            while (ulRc == NO_ERROR)
            {
                for (i = 0;
                     i < cFiles;
                     i++, pfindbuf3 = (PFILEFINDBUF3)((int)pfindbuf3 + pfindbuf3->oNextEntryOffset)
                     )
                {
                    const char *    psz;
                    char            szSource[CCHMAXPATH];
                    BOOL            fExcluded;
                    char            szTS[TS_SIZE];

                    /*
                     * Make full path.
                     */
                    if ((psz = strrchr(argv[argi], '\\')) || (psz = strrchr(argv[argi], '/')) || (*(psz = &argv[argi][1]) == ':'))
                    {
                        strncpy(szSource, argv[argi], psz - argv[argi] + 1);
                        szSource[psz - argv[argi] + 1]  = '\0';
                    }
                    else
                        szSource[0]  = '\0';
                    strcat(szSource, pfindbuf3->achName);
                    strlwr(szSource);
                    fileNormalize(szSource);

                    /*
                     * Check if this is an excluded file.
                     */
                    fExcluded = FALSE;
                    psz = options.pszExcludeFiles;
                    while (*psz != '\0' && *psz != ';')
                    {
                        const char * pszNext = strchr(psz, ';');
                        if (strlen(szSource) == pszNext - psz && strncmp(szSource, psz, pszNext - psz) == 0)
                            fExcluded = TRUE;
                        psz = pszNext + 1;
                    }
                    if (fExcluded)
                        continue;

                    /*
                     * Analyse the file.
                     */
                    depMakeTS(szTS, pfindbuf3);
                    rc -= makeDependent(&szSource[0], szTS);
                }

                /* next file */
                cFiles = ~0UL;
                pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
                ulRc = DosFindNext(hDir, pfindbuf3, sizeof(achBuffer), &cFiles);
            }
            DosFindClose(hDir);
        }
        /* next */
        argi++;
    }

    /* Write the depend file! */
    if (!depWriteFile(pszDepFile, !options.fAppend))
        fprintf(stderr, "error: failed to write dependencies file!\n");
    #if 0
    printf("cfcNodes=%d\n", cfcNodes);
    #endif

    return rc;
}


/**
 * Displays the syntax description for this util.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
void syntax(void)
{
    printf(
        "FastDep v0.48 (build %d)\n"
        "Dependency scanner. Creates a makefile readable depend file.\n"
        " - was quick and dirty, now it's just quick -\n"
        "\n"
        "Syntax: FastDep [options] <files> [more options [more files [...]]]\n"
        "    or\n"
        "        FastDep [options] @<parameterfile>\n"
        "\n"
        "Options:\n"
        "   -a<[+]|->       Append to the output file.            Default: Overwrite.\n"
        "   -ca             Force search directory caching.\n"
        "                   Default: cache if more that 25 files are to be searched.\n"
        "                            (more than 25 in the first file expression.)\n"
        "   -cy<[+]|->      Check for cylic dependencies.         Default: -cy-\n"
        "   -d <outputfn>   Output filename.                      Default: %s\n"
        "   -e excludepath  Exclude paths. If a filename is found in any\n"
        "                   of these paths only the filename is used, not\n"
        "                   the path+filename (which is default).\n"
        "   -eall<[+]|->    Include and source filenames, paths or no paths.\n"
        "                   -eall+: No path are added to the filename.\n"
        "                   -eall-: The filename is appended the include path\n"
        "                           was found in.\n"
        "                   Default: eall-\n"
        "   -f<[+]|->       Force scanning of all files. If disabled we'll only scan\n"
        "                   files which are younger or up to one month older than the\n"
        "                   dependancy file (if it exists).       Default: disabled\n"
        "   -i <include>    Additional include paths. INCLUDE is searched after this.\n"
        "   -n<[+]|->       No path for object files in the rules.\n"
        "   -o <objdir>     Path were object files are placed. This path replaces the\n"
        "                   entire filename path\n"
        "   -o-             No object path\n"
        "   -obr<[+]|->     -obr+: Object rule.\n"
        "                   -obr-: No object rule, rule for source filename is generated.\n"
        "   -obj[ ]<objext> Object extention.                     Default: obj\n"
        "   -srcadd[ ]<langid>:<dep>\n"
        "                   Additional dependants for source file of the given language\n"
        "                   type. langid: AS,CX,RC,ORC,COB,IPF\n"
        "                   This is very usfull for compiler configuration files.\n"
        "   -r[ ]<rsrcext>  Resource binary extention.            Default: res\n"
        "   -x[ ]<f1[;f2]>  Files to exclude. Only exact filenames.\n"
        "   <files>         Files to scan. Wildchars are allowed.\n"
        "\n"
        "Options and files could be mixed.\n"
        " copyright (c) 1999-2010 knut st. osmundsen (bird-kBuild-spamx@anduin.net)\n",
        ODIN32_BUILD_NR,
        pszDefaultDepFile
        );
}


/**
 * Generates depend info on this file, these are stored internally
 * and written to file later.
 * @returns
 * @param   pszFilename     Pointer to source filename. Correct case is assumed!
 * @param   pszTS           File time stamp.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
int makeDependent(const char *pszFilename, const char *pszTS)
{
    int    rc = -1;

    char            szExt[CCHMAXPATH];
    PCONFIGENTRY    pCfg = &aConfig[0];
    BOOL            fHeader;

    /*
     * Find which filetype this is...
     */
    fileExt(pszFilename, szExt);
    while (pCfg->papszExts != NULL)
    {
        const char **ppsz = pCfg->papszExts;
        while (*ppsz != NULL && stricmp(*ppsz, szExt) != 0)
            ppsz++;
        if (*ppsz != NULL)
        {
            fHeader = pCfg->iFirstHdr > 0 && &pCfg->papszExts[pCfg->iFirstHdr] <= ppsz;
            break;
        }
        pCfg++;
    }

    /* Found? */
    if (pCfg->papszExts != NULL)
    {
        void *  pvRule = NULL;
        char    szNormFile[CCHMAXPATH];
        fileNormalize2(pszFilename, szNormFile);
        rc = (*pCfg->pfn)(pszFilename, &szNormFile[0], pszTS, fHeader, &pvRule);
        if (!rc && pvRule)
        {
            if (!fHeader && pCfg->pszzAddDeps)
                depAddSrcAddDeps(pvRule, pCfg->pszzAddDeps);
        }
    }
    else
    {
        if (*fileName(pszFilename, szExt) != '.') /* these are 'hidden' files, like .cvsignore, let's ignore them. */
            fprintf(stderr, "warning: '%s' has an unknown file type.\n", pszFilename);
        rc = 0;
    }


    return rc;
}


/**
 * Generates depend info on this C or C++ file, these are stored internally
 * and written to file later.
 * @returns 0 on success.
 *          !0 on error.
 * @param   pszFilename         Pointer to source filename. Correct case is assumed!
 * @param   pszNormFilename     Pointer to normalized source filename.
 * @param   pszTS               File time stamp.
 * @parma   fHeader             True if header file is being scanned.
 * @param   ppvRule             Variabel to return any new rule handle.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
int langC_CPP(const char *pszFilename, const char *pszNormFilename,
              const char *pszTS, BOOL fHeader, void **ppvRule)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Max line length is 4096... should not be a problem. */
    int     iLine;                      /* Linenumber. */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */
    BOOL    fComment;                   /* TRUE when within a multiline comment. */
                                        /* FALSE when not within a multiline comment. */
    int     iIfStack;                   /* StackPointer. */
    struct  IfStackEntry
    {
        int fIncluded : 1;              /* TRUE:  include this code;
                                         * FALSE: excluded */
        int fIf : 1;                    /* TRUE:  #if part of the expression.
                                         * FALSE: #else part of the expression. */
        int fSupported : 1;             /* TRUE:  supported if/else statement
                                         * FALSE: unsupported all else[<something>] are ignored
                                         *        All code is included.
                                         */
    } achIfStack[256];
    char    szSrcDir[CCHMAXPATH];
    filePath(pszNormFilename, szSrcDir);/* determin source code directory. */

    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszObjectExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszObjectExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename,
                         options.fCheckCyclic,
                         FALSE);
    }
    else
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    /* Initiate the IF-stack, comment state and line number. */
    iIfStack = 0;
    achIfStack[iIfStack].fIf = TRUE;
    achIfStack[iIfStack].fIncluded = TRUE;
    achIfStack[iIfStack].fSupported = TRUE;
    fComment = FALSE;
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        /* search for #include */
        register char *pszC;
        int cbLen;
        int i = 0;
        iLine++;

        /* skip blank chars */
        cbLen = strlen(szBuffer);
        while (i + 2 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
            i++;

        /* preprocessor statement? */
        if (!fComment && szBuffer[i] == '#')
        {
            /*
             * Preprocessor checks
             * We known that we have a preprocessor statment (starting with an '#' * at szBuffer[i]).
             * Depending on the word afterwards we'll take some different actions.
             * So we'll start of by extracting that word and make a string swich on it.
             * Note that there might be some blanks between the hash and the word.
             */
            int     cchWord;
            char *  pszEndWord;
            char *  pszArgument;
            i++;                /* skip hash ('#') */
            while (szBuffer[i] == '\t' || szBuffer[i] == ' ') /* skip blanks */
                i++;
            pszArgument = pszEndWord = findEndOfWord(&szBuffer[i]);
            cchWord = pszEndWord - &szBuffer[i];

            /*
             * Find the argument by skipping the blanks.
             */
            while (*pszArgument == '\t' || *pszArgument == ' ') /* skip blanks */
                pszArgument++;

            /*
             * string switch.
             */
            if (strncmp(&szBuffer[i], "include", cchWord) == 0)
            {
                /*
                 * #include
                 *
                 * Are we in a state where this file is to be included?
                 */
                if (achIfStack[iIfStack].fIncluded)
                {
                    char    szFullname[CCHMAXPATH];
                    char *  psz;
                    BOOL    f = FALSE;
                    int     j;
                    BOOL    fQuote;

                    /* extract info between "" or <> */
                    while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
                        i++;
                    fQuote = szBuffer[i] == '"';
                    i++; /* skip '"' or '<' */

                    /* if invalid statement then continue with the next line! */
                    if (!f) continue;

                    /* find end */
                    j = f = 0;
                    while (i + j < cbLen &&  j < CCHMAXPATH &&
                           !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
                        j++;

                    /* if invalid statement then continue with the next line! */
                    if (!f) continue;

                    /* copy filename */
                    strncpy(szFullname, &szBuffer[i], j);
                    szFullname[j] = '\0'; /* ensure terminatition. */
                    strlwr(szFullname);

                    /* find include file! */
                    psz = fQuote ? pathlistFindFile(szSrcDir, szFullname, szBuffer) : NULL;
                    if (psz == NULL)
                        psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
                    if (psz == NULL)
                        psz = pathlistFindFile(pszIncludeEnv, szFullname, szBuffer);

                    /* did we find the include? */
                    if (psz != NULL)
                    {
                        if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                        {   /* #include <sys/stats.h> makes trouble, check for '/' and '\'. */
                            if (!strchr(szFullname, '/') && !strchr(szFullname, '\\'))
                                depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                            else
                                fprintf(stderr, "%s(%d): warning include '%s' is ignored.\n",
                                        pszFilename, iLine, szFullname);
                        }
                        else
                            depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
                    }
                    else
                    {
                        fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
                                pszFilename, iLine, szFullname);
                        depMarkNotFound(pvRule);
                    }
                }
            }
            else
                /*
                 * #if
                 */
                if (strncmp(&szBuffer[i], "if", cchWord) == 0)
            {   /* #if 0 and #if <1-9> are supported */
                pszEndWord = findEndOfWord(pszArgument);
                iIfStack++;
                if ((pszEndWord - pszArgument) == 1
                    && *pszArgument >= '0' && *pszArgument <= '9')
                {
                    if (*pszArgument != '0')
                        achIfStack[iIfStack].fIncluded =  TRUE;
                    else
                        achIfStack[iIfStack].fIncluded =  FALSE;
                }
                else
                    achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
                achIfStack[iIfStack].fIf = TRUE;
            }
            else
                /*
                 * #else
                 */
                if (strncmp(&szBuffer[i], "else", cchWord) == 0)
            {
                if (achIfStack[iIfStack].fSupported)
                {
                    if (achIfStack[iIfStack].fIncluded) /* ARG!! this'll prevent warning */
                        achIfStack[iIfStack].fIncluded = FALSE;
                    else
                        achIfStack[iIfStack].fIncluded = TRUE;
                }
                achIfStack[iIfStack].fIf = FALSE;
            }
            else
                /*
                 * #endif
                 */
                if (strncmp(&szBuffer[i], "endif", cchWord) == 0)
            {   /* Pop the if-stack. */
                if (iIfStack > 0)
                    iIfStack--;
                else
                    fprintf(stderr, "%s(%d): If-Stack underflow!\n", pszFilename, iLine);
            }
            /*
             * general if<something> and elseif<something> implementations
             */
            else
                if (strncmp(&szBuffer[i], "elseif", 6) == 0)
            {
                achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
            }
            else
                if (strncmp(&szBuffer[i], "if", 2) == 0)
            {
                iIfStack++;
                achIfStack[iIfStack].fIf = TRUE;
                achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
            }
            /* The rest of them aren't implemented yet.
            else if (strncmp(&szBuffer[i], "if") == 0)
            {
            }
            */
        }

        /*
         * Comment checks.
         *  -Start at first non-blank.
         *  -Loop thru the line since we might have more than one
         *   comment statement on a single line.
         */
        pszC = &szBuffer[i];
        while (pszC != NULL && *pszC != '\0')
        {
            if (fComment)
                pszC = strstr(pszC, "*/");  /* look for end comment mark. */
            else
            {
                char *pszLC;
                pszLC= strstr(pszC, "//");  /* look for single line comment mark. */
                pszC = strstr(pszC, "/*");  /* look for start comment mark */
                if (pszLC && pszLC < pszC)  /* if there is an single line comment mark before the */
                    break;                  /* muliline comment mark we'll ignore the multiline mark. */
            }

            /* Comment mark found? */
            if (pszC != NULL)
            {
                fComment = !fComment;
                pszC += 2;          /* skip comment mark */

                /* debug */
                /*
                if (fComment)
                    fprintf(stderr, "starts at line %d\n", iLine);
                else
                    fprintf(stderr, "ends   at line %d\n", iLine);
                    */
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);

    return 0;
}


/**
 * Generates depend info on this file, these are stored internally
 * and written to file later.
 * @returns 0 on success.
 *          !0 on error.
 * @param   pszFilename         Pointer to source filename. Correct case is assumed!
 * @param   pszNormFilename     Pointer to normalized source filename.
 * @param   pszTS               File time stamp.
 * @parma   fHeader             True if header file is being scanned.
 * @param   ppvRule             Variabel to return any new rule handle.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
int langAsm(const char *pszFilename, const char *pszNormFilename,
            const char *pszTS, BOOL fHeader, void **ppvRule)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Temporary buffer (max line lenght size...) */
    int     iLine;                      /* current line number */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */


    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszObjectExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszObjectExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename,
                         options.fCheckCyclic, FALSE);
    }
    else
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        /* search for include */
        int cbLen;
        int i = 0;
        iLine++;

        /* skip blank chars */
        cbLen = strlen(szBuffer);
        while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
            i++;

        /* is this an include? */
        if (strnicmp(&szBuffer[i], "include", 7) == 0
            && (szBuffer[i + 7] == '\t' || szBuffer[i + 7] == ' ')
            )
        {
            char szFullname[CCHMAXPATH];
            char *psz;
            int  j;

            /* skip to first no blank char  */
            i += 7;
            while (i < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
                i++;

            /* comment check - if comment found, no filename was given. continue. */
            if (szBuffer[i] == ';') continue;

            /* find end */
            j = 0;
            while (i + j < cbLen
                   &&  j < CCHMAXPATH
                   && szBuffer[i+j] != ' '  && szBuffer[i+j] != '\t' && szBuffer[i+j] != '\n'
                   && szBuffer[i+j] != '\0' && szBuffer[i+j] != ';'  && szBuffer[i+j] != '\r'
                   )
                j++;

            /* copy filename */
            strncpy(szFullname, &szBuffer[i], j);
            szFullname[j] = '\0'; /* ensure terminatition. */
            strlwr(szFullname);

            /* find include file! */
            psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
            if (psz == NULL)
                psz = pathlistFindFile(pszIncludeEnv, szFullname, szBuffer);

            /* Did we find the include? */
            if (psz != NULL)
            {
                if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                {   /* include sys/stats.inc makes trouble, check for '/' and '\'. */
                    if (!strchr(szFullname, '/') && !strchr(szFullname, '\\'))
                        depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                    else
                        fprintf(stderr, "%s(%d): warning include '%s' is ignored.\n",
                                pszFilename, iLine, szFullname);
                }
                else
                    depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
            }
            else
            {
                fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
                        pszFilename, iLine, szFullname);
                depMarkNotFound(pvRule);
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);

    return 0;
}


/**
 * Generates depend info on this Resource file, these are stored internally
 * and written to file later.
 * @returns 0 on success.
 *          !0 on error.
 * @param   pszFilename         Pointer to source filename. Correct case is assumed!
 * @param   pszNormFilename     Pointer to normalized source filename.
 * @param   pszTS               File time stamp.
 * @parma   fHeader             True if header file is being scanned.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
#if 0
int langRC(const char *pszFilename, const char *pszNormFilename, void *pvFile, BOOL fHeader)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Temporary buffer (max line lenght size...) */
    int     iLine;                      /* current line number */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */


    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszRsrcExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszRsrcExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
                         options.fCheckCyclic,
                         FALSE);
    }
    else
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        /* search for #include */
        int cbLen;
        int i = 0;
        int i1;
        iLine++;

        /* skip blank chars */
        cbLen = strlen(szBuffer);
        while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
            i++;

        /* is this an include? */
        i1 = 1;
        if (   strncmp(&szBuffer[i], "#include", 8) == 0
            || (i1 = strnicmp(&szBuffer[i], "RCINCLUDE", 9)) == 0
            || strnicmp(&szBuffer[i], "DLGINCLUDE", 10) == 0
            )
        {
            char szFullname[CCHMAXPATH];
            char *psz;
            BOOL f = FALSE;
            int  j;

            if (i1 != 0)
            {   /*
                 * #include <file.h>,  #include "file.h" or DLGINCLUDE 1 "file.h"
                 *
                 * extract info between "" or <>
                 */
                while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
                    i++;
                i++; /* skip '"' or '<' */

                /* if invalid statement then continue with the next line! */
                if (!f) continue;

                /* find end */
                j = f = 0;
                while (i + j < cbLen &&  j < CCHMAXPATH &&
                       !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
                    j++;

                /* if invalid statement then continue with the next line! */
                if (!f) continue;
            }
            else
            {   /*
                 * RCINCLUDE ["]filename.dlg["]
                 * Extract filename.
                 */

                /* skip to filename.dlg start - if eol will continue to loop. */
                i += 9;
                while (szBuffer[i] == ' ' || szBuffer[i] == '\t' || szBuffer[i] == '"')
                    i++;
                if (szBuffer[i] == '\0')
                    continue;

                /* search to end of filename. */
                j = i+1;
                while (   szBuffer[i+j] != ' ' && szBuffer[i+j] != '\t'
                       && szBuffer[i+j] != '"' && szBuffer[i+j] != '\0')
                    j++;
            }

            /* copy filename */
            strncpy(szFullname, &szBuffer[i], j);
            szFullname[j] = '\0'; /* ensure terminatition. */
            strlwr(szFullname);

            /* find include file! */
            psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
            if (psz == NULL)
                psz = pathlistFindFile(pszIncludeEnv, szFullname, szBuffer);

            /* did we find the include? */
            if (psz != NULL)
            {
                if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                {   /* #include <sys/stats.h> makes trouble, check for '/' and '\'. */
                    if (!strchr(szFullname, '/') && !strchr(szFullname, '\\'))
                        depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                    else
                        fprintf(stderr, "%s(%d): warning include '%s' is ignored.\n",
                                pszFilename, iLine, szFullname);
                }
                else
                    depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
            }
            else
            {
                fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
                        pszFilename, iLine, szFullname);
                depMarkNotFound(pvRule);
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);
    return 0;
}
#else
int langRC(const char *pszFilename, const char *pszNormFilename,
           const char *pszTS, BOOL fHeader, void **ppvRule)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Max line length is 4096... should not be a problem. */
    int     iLine;                      /* Linenumber. */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */
    BOOL    fComment;                   /* TRUE when within a multiline comment. */
                                        /* FALSE when not within a multiline comment. */
    int     iIfStack;                   /* StackPointer. */
    struct  IfStackEntry
    {
        int fIncluded : 1;              /* TRUE:  include this code;
                                         * FALSE: excluded */
        int fIf : 1;                    /* TRUE:  #if part of the expression.
                                         * FALSE: #else part of the expression. */
        int fSupported : 1;             /* TRUE:  supported if/else statement
                                         * FALSE: unsupported all else[<something>] are ignored
                                         *        All code is included.
                                         */
    } achIfStack[256];


    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszRsrcExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszRsrcExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename,
                         options.fCheckCyclic,
                         FALSE);
    }
    else
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    /* Initiate the IF-stack, comment state and line number. */
    iIfStack = 0;
    achIfStack[iIfStack].fIf = TRUE;
    achIfStack[iIfStack].fIncluded = TRUE;
    achIfStack[iIfStack].fSupported = TRUE;
    fComment = FALSE;
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        register char * pszC;
        char            szFullname[CCHMAXPATH];
        int             cbLen;
        int             i1 = 1;
        int             i = 0;
        iLine++;

        /* skip blank chars */
        cbLen = strlen(szBuffer);
        while (i + 2 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
            i++;

        /* preprocessor statement? */
        if (!fComment && szBuffer[i] == '#')
        {
            /*
             * Preprocessor checks
             * We known that we have a preprocessor statment (starting with an '#' * at szBuffer[i]).
             * Depending on the word afterwards we'll take some different actions.
             * So we'll start of by extracting that word and make a string swich on it.
             * Note that there might be some blanks between the hash and the word.
             */
            int     cchWord;
            char *  pszEndWord;
            char *  pszArgument;
            i++;                /* skip hash ('#') */
            while (szBuffer[i] == '\t' || szBuffer[i] == ' ') /* skip blanks */
                i++;
            pszArgument = pszEndWord = findEndOfWord(&szBuffer[i]);
            cchWord = pszEndWord - &szBuffer[i];

            /*
             * Find the argument by skipping the blanks.
             */
            while (*pszArgument == '\t' || *pszArgument == ' ') /* skip blanks */
                pszArgument++;

            /*
             * string switch.
             */
            if (strncmp(&szBuffer[i], "include", cchWord) == 0)
            {
                /*
                 * #include
                 *
                 * Are we in a state where this file is to be included?
                 */
                if (achIfStack[iIfStack].fIncluded)
                {
                    char *psz;
                    BOOL f = FALSE;
                    int  j;

                    /* extract info between "" or <> */
                    while (i < cbLen && !(f = (szBuffer[i] == '"' || szBuffer[i] == '<')))
                        i++;
                    i++; /* skip '"' or '<' */

                    /* if invalid statement then continue with the next line! */
                    if (!f) continue;

                    /* find end */
                    j = f = 0;
                    while (i + j < cbLen &&  j < CCHMAXPATH &&
                           !(f = (szBuffer[i+j] == '"' || szBuffer[i+j] == '>')))
                        j++;

                    /* if invalid statement then continue with the next line! */
                    if (!f) continue;

                    /* copy filename */
                    strncpy(szFullname, &szBuffer[i], j);
                    szFullname[j] = '\0'; /* ensure terminatition. */
                    strlwr(szFullname);

                    /* find include file! */
                    psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
                    if (psz == NULL)
                        psz = pathlistFindFile(pszIncludeEnv, szFullname, szBuffer);

                    /* did we find the include? */
                    if (psz != NULL)
                    {
                        if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                        {   /* #include <sys/stats.h> makes trouble, check for '/' and '\'. */
                            if (!strchr(szFullname, '/') && !strchr(szFullname, '\\'))
                                depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                            else
                                fprintf(stderr, "%s(%d): warning include '%s' is ignored.\n",
                                        pszFilename, iLine, szFullname);
                        }
                        else
                            depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
                    }
                    else
                    {
                        fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
                                pszFilename, iLine, szFullname);
                        depMarkNotFound(pvRule);
                    }
                }
            }
            else
                /*
                 * #if
                 */
                if (strncmp(&szBuffer[i], "if", cchWord) == 0)
            {   /* #if 0 and #if <1-9> are supported */
                pszEndWord = findEndOfWord(pszArgument);
                iIfStack++;
                if ((pszEndWord - pszArgument) == 1
                    && *pszArgument >= '0' && *pszArgument <= '9')
                {
                    if (*pszArgument != '0')
                        achIfStack[iIfStack].fIncluded =  TRUE;
                    else
                        achIfStack[iIfStack].fIncluded =  FALSE;
                }
                else
                    achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
                achIfStack[iIfStack].fIf = TRUE;
            }
            else
                /*
                 * #else
                 */
                if (strncmp(&szBuffer[i], "else", cchWord) == 0)
            {
                if (achIfStack[iIfStack].fSupported)
                {
                    if (achIfStack[iIfStack].fIncluded) /* ARG!! this'll prevent warning */
                        achIfStack[iIfStack].fIncluded = FALSE;
                    else
                        achIfStack[iIfStack].fIncluded = TRUE;
                }
                achIfStack[iIfStack].fIf = FALSE;
            }
            else
                /*
                 * #endif
                 */
                if (strncmp(&szBuffer[i], "endif", cchWord) == 0)
            {   /* Pop the if-stack. */
                if (iIfStack > 0)
                    iIfStack--;
                else
                    fprintf(stderr, "%s(%d): If-Stack underflow!\n", pszFilename, iLine);
            }
            /*
             * general if<something> and elseif<something> implementations
             */
            else
                if (strncmp(&szBuffer[i], "elseif", 6) == 0)
            {
                achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
            }
            else
                if (strncmp(&szBuffer[i], "if", 2) == 0)
            {
                iIfStack++;
                achIfStack[iIfStack].fIf = TRUE;
                achIfStack[iIfStack].fSupported = FALSE;
                achIfStack[iIfStack].fIncluded = TRUE;
            }
            /* The rest of them aren't implemented yet.
            else if (strncmp(&szBuffer[i], "if") == 0)
            {
            }
            */
        } else
            /*
             * Check for resource compiler directives.
             */
            if (    !fComment
                &&  !strchr(&szBuffer[i], ',')
                &&  (   !strnicmp(&szBuffer[i], "ICON", 4)
                     || !strnicmp(&szBuffer[i], "FONT", 4)
                     || !strnicmp(&szBuffer[i], "BITMAP", 6)
                     || !strnicmp(&szBuffer[i], "POINTER", 7)
                     || !strnicmp(&szBuffer[i], "RESOURCE", 8)
                     || !(i1 = strnicmp(&szBuffer[i], "RCINCLUDE", 9))
                   /*|| !strnicmp(&szBuffer[i], "DLGINCLUDE", 10) - only used by the dlgeditor */
                     || !strnicmp(&szBuffer[i], "DEFAULTICON", 11)
                     )
                )
        {
            /*
             * RESOURCE 123 1 ["]filename.ext["]
             */
            char    szLine[1024];
            char *  pszFile;
            char    chQuote = ' ';

            PreProcessLine(szLine, &szBuffer[i]);

            pszFile = &szLine[strlen(szLine)-1];
            if (*pszFile == '\"' || *pszFile == '\'')
            {
                chQuote = *pszFile;
                *pszFile-- = '\0';
            }
            while (*pszFile != chQuote)
                pszFile--;
            *pszFile++ = '\0'; /* We now have extracted the filename - pszFile. */
            strlwr(pszFile);

            /* Add filename to the dependencies. */
            if (i1)
                depAddDepend(pvRule, pszFile, options.fCheckCyclic, FALSE);
            else
            {
                char *psz;
                /* find include file! */
                psz = pathlistFindFile(options.pszInclude, pszFile, szFullname);
                if (psz == NULL)
                    psz = pathlistFindFile(pszIncludeEnv, pszFile, szFullname);

                /* did we find the include? */
                if (psz != NULL)
                {
                    if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szFullname))
                    {   /* #include <sys/stats.h> makes trouble, check for '/' and '\'. */
                        if (!strchr(pszFile, '/') && !strchr(pszFile, '\\'))
                            depAddDepend(pvRule, pszFile, options.fCheckCyclic, FALSE);
                        else
                            fprintf(stderr, "%s(%d): warning include '%s' is ignored.\n",
                                    pszFilename, iLine, pszFile);
                    }
                    else
                        depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                }
                else
                {
                    fprintf(stderr, "%s(%d): warning include file '%s' not found!\n",
                            pszFilename, iLine, pszFile);
                    depMarkNotFound(pvRule);
                }
            }
        }


        /*
         * Comment checks.
         *  -Start at first non-blank.
         *  -Loop thru the line since we might have more than one
         *   comment statement on a single line.
         */
        pszC = &szBuffer[i];
        while (pszC != NULL && *pszC != '\0')
        {
            if (fComment)
                pszC = strstr(pszC, "*/");  /* look for end comment mark. */
            else
            {
                char *pszLC;
                pszLC= strstr(pszC, "//");  /* look for single line comment mark. */
                pszC = strstr(pszC, "/*");  /* look for start comment mark */
                if (pszLC && pszLC < pszC)  /* if there is an single line comment mark before the */
                    break;                  /* muliline comment mark we'll ignore the multiline mark. */
            }

            /* Comment mark found? */
            if (pszC != NULL)
            {
                fComment = !fComment;
                pszC += 2;          /* skip comment mark */

                /* debug */
                /*
                if (fComment)
                    fprintf(stderr, "starts at line %d\n", iLine);
                else
                    fprintf(stderr, "ends   at line %d\n", iLine);
                    */
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);

    return 0;
}
#endif


/**
 * Generates depend info on this COBOL file, these are stored internally
 * and written to file later.
 * @returns 0 on success.
 *          !0 on error.
 * @param   pszFilename         Pointer to source filename. Correct case is assumed!
 * @param   pszNormFilename     Pointer to normalized source filename.
 * @param   pszTS               File time stamp.
 * @parma   fHeader             True if header file is being scanned.
 * @param   ppvRule             Variabel to return any new rule handle.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
int langCOBOL(const char *pszFilename, const char *pszNormFilename,
              const char *pszTS, BOOL fHeader, void **ppvRule)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Temporary buffer (max line lenght size...) */
    int     iLine;                      /* current line number */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */


    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszObjectExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszObjectExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename)
                            ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
                         options.fCheckCyclic,
                         FALSE);
    }
    else
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        /* search for #include */
        int cbLen;
        int i = 0;
        int i1, i2;
        iLine++;

        /* check for comment mark (column 7) */
        if (szBuffer[6] == '*')
            continue;

        /* skip blank chars */
        cbLen = strlen(szBuffer);
        while (i + 9 < cbLen && (szBuffer[i] == ' ' || szBuffer[i] == '\t'))
            i++;

        /* is this an include? */
        if (   (i1 = strnicmp(&szBuffer[i], "COPY", 4)) == 0
            || (i2 = strnicmpwords(&szBuffer[i], "EXEC SQL INCLUDE", 16)) == 0
            )
        {
            char szFullname[CCHMAXPATH];
            char *psz;
            int  j;

            /* skip statement */
            i += 4;
            if (i1 != 0)
            {
                int y = 2; /* skip two words */
                do
                {
                    /* skip blanks */
                    while (szBuffer[i] == ' ' || szBuffer[i] == '\t')
                        i++;
                    /* skip word */
                    while (szBuffer[i] != ' ' && szBuffer[i] != '\t'
                           && szBuffer[i] != '\0' && szBuffer[i] != '\n')
                        i++;
                    y--;
                } while (y > 0);
            }

            /* check for blank */
            if (szBuffer[i] != ' ' && szBuffer[i] != '\t') /* no copybook specified... */
                continue;

            /* skip blanks */
            while (szBuffer[i] == ' ' || szBuffer[i] == '\t')
                i++;

            /* if invalid statement then continue with the next line! */
            if (szBuffer[i] == '\0' || szBuffer[i] == '\n')
                continue;

            /* find end */
            j = 0;
            while (i + j < cbLen && j < CCHMAXPATH
                   && szBuffer[i+j] != '.'
                   && szBuffer[i+j] != ' '  && szBuffer[i+j] != '\t'
                   && szBuffer[i+j] != '\0' && szBuffer[i+j] != '\n'
                   )
                j++;

            /* if invalid statement then continue with the next line! */
            if (szBuffer[i+j] != '.' && szBuffer[i+j] != ' ' && szBuffer[i] != '\t')
                continue;

            /* copy filename */
            strncpy(szFullname, &szBuffer[i], j);
            szFullname[j] = '\0'; /* ensure terminatition. */
            strlwr(szFullname);

            /* add extention .cpy - hardcoded for the moment. */
            strcpy(&szFullname[j], ".cbl");

            /* find include file! */
            psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
            if (!psz)
            {
                strcpy(&szFullname[j], ".cpy");
                psz = pathlistFindFile(options.pszInclude, szFullname, szBuffer);
            }

            /* did we find the include? */
            if (psz != NULL)
            {
                if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                    depAddDepend(pvRule, szFullname, options.fCheckCyclic, FALSE);
                else
                    depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
            }
            else
            {
                szFullname[j] = '\0';
                fprintf(stderr, "%s(%d): warning copybook '%s' was not found!\n",
                        pszFilename, iLine, szFullname);
                depMarkNotFound(pvRule);
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);

    return 0;
}


/**
 * Generates depend info on this IPF file, these are stored internally
 * and written to file later.
 * @returns 0 on success.
 *          !0 on error.
 * @param   pszFilename         Pointer to source filename. Correct case is assumed!
 * @param   pszNormFilename     Pointer to normalized source filename.
 * @param   pszTS               File time stamp.
 * @param   fHeader             True if header file is being scanned.
 * @param   ppvRule             Variabel to return any new rule handle.
 * @status  completely implemented.
 * @author  knut st. osmundsen
 */
int langIPF(  const char *pszFilename, const char *pszNormFilename,
              const char *pszTS, BOOL fHeader, void **ppvRule)
{
    void *  pvFile;                     /* Text buffer pointer. */
    void *  pvRule;                     /* Handle to the current rule. */
    char    szBuffer[4096];             /* Temporary buffer (max line lenght size...) */
    int     iLine;                      /* current line number */
    void *  pv = NULL;                  /* An index used by textbufferGetNextLine. */


    /**********************************/
    /* Add the depend rule            */
    /**********************************/
    /*if (options.fObjRule && !fHeader)
    {
        if (options.fNoObjectPath)
            pvRule = depAddRule(fileNameNoExt(pszFilename, szBuffer), NULL, options.pszObjectExt, pszTS, FALSE);
        else
            pvRule = depAddRule(options.fObjectDir ?
                                    options.pszObjectDir :
                                    filePathSlash(pszFilename, szBuffer),
                                fileNameNoExt(pszFilename, szBuffer + CCHMAXPATH),
                                options.pszObjectExt, pszTS, FALSE);

        if (options.fSrcWhenObj && pvRule)
            depAddDepend(pvRule,
                         options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename)
                            ? fileName(pszFilename, szBuffer) : fileNormalize2(pszFilename, szBuffer),
                         options.fCheckCyclic,
                         FALSE);
    }
    else */
        pvRule = depAddRule(options.fExcludeAll || pathlistFindFile2(options.pszExclude, pszNormFilename) ?
                            fileName(pszFilename, szBuffer) : pszNormFilename, NULL, NULL, pszTS, FALSE);

    /* duplicate rule? */
    *ppvRule = pvRule;
    if (pvRule == NULL)
        return 0;


    /********************/
    /* Make file buffer */
    /********************/
    pvFile = textbufferCreate(pszFilename);
    if (!pvFile)
    {
        fprintf(stderr, "failed to open '%s'\n", pszFilename);
        return -1;
    }


    /*******************/
    /* find dependants */
    /*******************/
    iLine = 0;
    while (textbufferGetNextLine(pvFile, &pv, szBuffer, sizeof(szBuffer)) != NULL) /* line loop */
    {
        iLine++;

        /* is this an imbed statement? */
        if (!strncmp(&szBuffer[0], ".im", 3))
        {
            char    szFullname[CCHMAXPATH];
            char *  psz;
            int     i;
            int     j;
            char    chQuote = 0;

            /* skip statement and blanks */
            i = 4;
            while (szBuffer[i] == ' ' || szBuffer[i] == '\t')
                i++;

            /* check for quotes */
            if (szBuffer[i] == '\'' || szBuffer[i] == '\"')
                chQuote = szBuffer[i++];

            /* find end */
            j = 0;
            if (chQuote != 0)
            {
                while (szBuffer[i+j] != chQuote && szBuffer[i+j] != '\n' && szBuffer[i+j] != '\r' && szBuffer[i+j] != '\0')
                    j++;
            }
            else
            {
                while (szBuffer[i+j] != '\n' && szBuffer[i+j] != '\r' && szBuffer[i+j] != '\0')
                    j++;
            }

            /* find end */
            if (j >= CCHMAXPATH)
            {
                fprintf(stderr, "%s(%d) warning: Filename too long ignored.\n", pszFilename, iLine);
                continue;
            }

            /* copy filename */
            strncpy(szFullname, &szBuffer[i], j);
            szFullname[j] = '\0'; /* ensure terminatition. */
            strlwr(szFullname);

            /* find include file! */
            psz = filecacheFileExist(szFullname, szBuffer);

            /* did we find the include? */
            if (psz != NULL)
            {
                if (options.fExcludeAll || pathlistFindFile2(options.pszExclude, szBuffer))
                    depAddDepend(pvRule, fileName(szFullname, szBuffer), options.fCheckCyclic, FALSE);
                else
                    depAddDepend(pvRule, szBuffer, options.fCheckCyclic, FALSE);
            }
            else
            {
                fprintf(stderr, "%s(%d): warning imbeded file '%s' was not found!\n",
                        pszFilename, iLine, szFullname);
                depMarkNotFound(pvRule);
            }
        }
    } /*while*/

    textbufferDestroy(pvFile);
    fHeader = fHeader;

    return 0;
}


#define upcase(ch)   \
     (ch >= 'a' && ch <= 'z' ? ch - ('a' - 'A') : ch)

/**
 * Compares words. Multiple spaces are treates as on single blank i both string when comparing them.
 * @returns   0 equal. (same as strnicmp)
 * @param     pszS1  String 1
 * @param     pszS2  String 2
 * @param     cch    Length to compare (relative to string 1)
 */
int strnicmpwords(const char *pszS1, const char *pszS2, int cch)
{
    do
    {
        while (cch > 0 && upcase(*pszS1) == upcase(*pszS2) && *pszS1 != ' ')
            pszS1++, pszS2++, cch--;

        /* blank test and skipping */
        if (cch > 0 && *pszS1 == ' ' && *pszS2 == ' ')
        {
            while (cch > 0 && *pszS1 == ' ')
                pszS1++, cch--;

            while (*pszS2 == ' ')
                pszS2++;
        }
        else
            break;
    } while (cch > 0);

    return cch == 0 ? 0 : *pszS1 - *pszS2;
}


/**
 * Normalizes the path slashes for the filename. It will partially expand paths too.
 * @returns   pszFilename
 * @param     pszFilename  Pointer to filename string. Not empty string!
 *                         Much space to play with.
 */
char *fileNormalize(char *pszFilename)
{
    char *psz = pszFilename;

    /* correct slashes */
    while ((pszFilename = strchr(pszFilename, '//')) != NULL)
        *pszFilename++ = '\\';

    /* expand path? */
    pszFilename = psz;
    if (pszFilename[1] != ':')
    {   /* relative path */
        int     iSlash;
        char    szFile[CCHMAXPATH];
        char *  psz = szFile;

        strcpy(szFile, pszFilename);
        iSlash = *psz == '\\' ? 1 : cSlashes;
        while (*psz != '\0')
        {
            if (*psz == '.' && psz[1] == '.'  && psz[2] == '\\')
            {   /* up one directory */
                if (iSlash > 0)
                    iSlash--;
                psz += 3;
            }
            else if (*psz == '.' && psz[1] == '\\')
            {   /* no change */
                psz += 2;
            }
            else
            {   /* completed expantion! */
                strncpy(pszFilename, szCurDir, aiSlashes[iSlash]+1);
                strcpy(pszFilename + aiSlashes[iSlash]+1, psz);
                break;
            }
        }
    }
    /* else: assume full path */

    return psz;
}


/**
 * Normalizes the path slashes for the filename. It will partially expand paths too.
 * Makes name all lower case too.
 * @returns   pszFilename
 * @param     pszFilename  Pointer to filename string. Not empty string!
 *                         Much space to play with.
 * @param     pszBuffer    Pointer to output buffer.
 */
char *fileNormalize2(const char *pszFilename, char *pszBuffer)
{
    char *  psz = pszBuffer;
    int     iSlash;

    if (pszFilename[1] != ':')
    {
        /* iSlash */
        if (*pszFilename == '\\' || *pszFilename == '/')
            iSlash = 1;
        else
            iSlash = cSlashes;

        /* interpret . and .. */
        while (*pszFilename != '\0')
        {
            if (*pszFilename == '.' && pszFilename[1] == '.'  && (pszFilename[2] == '\\' || pszFilename[1] == '/'))
            {   /* up one directory */
                if (iSlash > 0)
                    iSlash--;
                pszFilename += 3;
            }
            else if (*pszFilename == '.' && (pszFilename[1] == '\\' || pszFilename[1] == '/'))
            {   /* no change */
                pszFilename += 2;
            }
            else
            {   /* completed expantion! - TODO ..\ or .\ may appare within the remaining path too... */
                strncpy(pszBuffer, szCurDir, aiSlashes[iSlash]+1);
                strcpy(pszBuffer + aiSlashes[iSlash]+1, pszFilename);
                break;
            }
        }
    }
    else
    {   /* have drive letter specified - assume ok (TODO)*/
        strcpy(pszBuffer, pszFilename);
    }

    /* correct slashes */
    while ((pszBuffer = strchr(pszBuffer, '//')) != NULL)
        *pszBuffer++ = '\\';

    /* lower case it */
    /*strlwr(psz);*/

    return psz;
}


/**
 * Copies the path part (excluding the slash) into pszBuffer and returns
 * a pointer to the buffer.
 * If no path is found "" is returned.
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *filePath(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '\\');
    if (psz == NULL)
        psz = strrchr(pszFilename, '/');

    if (psz == NULL)
        *pszBuffer = '\0';
    else
    {
        strncpy(pszBuffer, pszFilename, psz - pszFilename);
        pszBuffer[psz - pszFilename] = '\0';
    }

    return pszBuffer;
}


/**
 * Copies the path part including the slash into pszBuffer and returns
 * a pointer to the buffer.
 * If no path is found "" is returned.
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *filePathSlash(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '\\');
    if (psz == NULL)
        psz = strrchr(pszFilename, '/');

    if (psz == NULL)
        *pszBuffer = '\0';
    else
    {
        strncpy(pszBuffer, pszFilename, psz - pszFilename + 1);
        pszBuffer[psz - pszFilename + 1] = '\0';
    }

    return pszBuffer;
}


/**
 * Copies the path part including the slash into pszBuffer and returns
 * a pointer to the buffer. If no path is found "" is returned.
 * The path is normalized to only use '\\'.
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *filePathSlash2(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '\\');
    if (psz == NULL)
        psz = strrchr(pszFilename, '/');

    if (psz == NULL)
        *pszBuffer = '\0';
    else
    {
        strncpy(pszBuffer, pszFilename, psz - pszFilename + 1);
        pszBuffer[psz - pszFilename + 1] = '\0';

        /* normalize all '/' to '\\' */
        psz = pszBuffer;
        while ((psz = strchr(psz, '/')) != NULL)
               *psz++ = '\\';
    }

    return pszBuffer;
}


/**
 * Copies the filename (with extention) into pszBuffer and returns
 * a pointer to the buffer.
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *fileName(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '\\');
    if (psz == NULL)
        psz = strrchr(pszFilename, '/');

    strcpy(pszBuffer, psz == NULL ? pszFilename : psz + 1);

    return pszBuffer;
}


/**
 * Copies the name part with out extention into pszBuffer and returns
 * a pointer to the buffer.
 * If no name is found "" is returned.
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *fileNameNoExt(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '\\');
    if (psz == NULL)
        psz = strrchr(pszFilename, '/');

    strcpy(pszBuffer, psz == NULL ? pszFilename : psz + 1);

    psz = strrchr(pszBuffer, '.');
    if (psz > pszBuffer) /* an extetion on it's own (.depend) is a filename not an extetion! */
        *psz = '\0';

    return pszBuffer;
}


/**
 * Copies the extention part into pszBuffer and returns
 * a pointer to the buffer.
 * If no extention is found "" is returned.
 * The dot ('.') is not included!
 * @returns   Pointer to pszBuffer with path.
 * @param     pszFilename  Pointer to readonly filename.
 * @param     pszBuffer    Pointer to output Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *fileExt(const char *pszFilename, char *pszBuffer)
{
    char *psz = strrchr(pszFilename, '.');
    if (psz != NULL)
    {
        if (strchr(psz, '\\') != NULL || strchr(psz, '/') != NULL)
            *pszBuffer = '\0';
        else
            strcpy(pszBuffer, psz + 1);
    }
    else
        *pszBuffer = '\0';

    return pszBuffer;
}


/**
 * Adds a file to the cache.
 * @returns   Success indicator.
 * @param     pszFilename   Name of the file which is to be added. (with path!)
 */
BOOL filecacheAddFile(const char *pszFilename)
{
    PFCACHEENTRY pfcNew;

    /* allocate new block and fill in data */
    pfcNew = malloc(sizeof(FCACHEENTRY) + strlen(pszFilename) + 1);
    if (pfcNew == NULL)
    {
        fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
        return FALSE;
    }
    pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
    strcpy((char*)(unsigned)pfcNew->Key, pszFilename);
    if (!AVLInsert(&pfcTree, pfcNew))
    {
        free(pfcNew);
        return TRUE;
    }
    cfcNodes++;

    return TRUE;
}


/**
 * Adds a file to the cache.
 * @returns   Success indicator.
 * @param     pszDir   Name of the path which is to be added. (with slash!)
 */
BOOL filecacheAddDir(const char *pszDir)
{
    PFCACHEENTRY    pfcNew;
    APIRET          rc;
    char            szDir[CCHMAXPATH];
    int             cchDir;
    char            achBuffer[32768];
    PFILEFINDBUF3   pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
    HDIR            hDir = HDIR_CREATE;
    ULONG           cFiles = 0xFFFFFFF;
    int             i;

    /* Make path */
    filePathSlash2(pszDir, szDir);
    /*strlwr(szDir);*/ /* Convert name to lower case to allow faster searchs! */
    cchDir = strlen(szDir);


    /* Add directory to pfcDirTree. */
    pfcNew = malloc(sizeof(FCACHEENTRY) + cchDir + 1);
    if (pfcNew == NULL)
    {
        fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
        DosFindClose(hDir);
        return FALSE;
    }
    pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
    strcpy((char*)(unsigned)pfcNew->Key, szDir);
    AVLInsert(&pfcDirTree, pfcNew);


    /* Start to search directory - all files */
    strcat(szDir + cchDir, "*");
    rc = DosFindFirst(szDir, &hDir, FILE_NORMAL,
                      pfindbuf3, sizeof(achBuffer),
                      &cFiles, FIL_STANDARD);
    while (rc == NO_ERROR)
    {
        for (i = 0;
             i < cFiles;
             i++, pfindbuf3 = (PFILEFINDBUF3)((int)pfindbuf3 + pfindbuf3->oNextEntryOffset)
             )
        {
            pfcNew = malloc(sizeof(FCACHEENTRY) + cchDir + pfindbuf3->cchName + 1);
            if (pfcNew == NULL)
            {
                fprintf(stderr, "error: out of memory! (line=%d)\n", __LINE__);
                DosFindClose(hDir);
                return FALSE;
            }
            pfcNew->Key = (char*)(void*)pfcNew + sizeof(FCACHEENTRY);
            strcpy((char*)(unsigned)pfcNew->Key, szDir);
            strcpy((char*)(unsigned)pfcNew->Key + cchDir, pfindbuf3->achName);
            strlwr((char*)(unsigned)pfcNew->Key + cchDir); /* Convert name to lower case to allow faster searchs! */
            if (!AVLInsert(&pfcTree, pfcNew))
                free(pfcNew);
            else
                cfcNodes++;
        }

        /* next */
        cFiles = 0xFFFFFFF;
        pfindbuf3 = (PFILEFINDBUF3)(void*)&achBuffer[0];
        rc = DosFindNext(hDir, pfindbuf3, sizeof(achBuffer), &cFiles);
    }

    DosFindClose(hDir);

    return TRUE;
}


/**
 * Checks if pszFilename is exists in the cache.
 * @return    TRUE if found. FALSE if not found.
 * @param     pszFilename   Name of the file to be found. (with path!)
 *                          This is in lower case!
 */
INLINE BOOL filecacheFind(const char *pszFilename)
{
    return AVLGet(&pfcTree, (AVLKEY)pszFilename) != NULL;
}


/**
 * Checks if pszFilename is exists in the cache.
 * @return    TRUE if found. FALSE if not found.
 * @param     pszFilename   Name of the file to be found. (with path!)
 *                          This is in lower case!
 */
INLINE BOOL filecacheIsDirCached(const char *pszDir)
{
    return AVLGet(&pfcDirTree, (AVLKEY)pszDir) != NULL;
}


/**
 * Checks if a file exist, uses file cache if possible.
 * @returns   Pointer to a filename consiting of the path part + the given filename.
 *            (pointer into pszBuffer)
 *            NULL if file is not found. ("" in buffer)
 * @parma     pszFilename  Filename to find.
 * @parma     pszBuffer    Ouput Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *filecacheFileExist(const char *pszFilename, char *pszBuffer)
{
    APIRET          rc;

    *pszBuffer = '\0';

    fileNormalize2(pszFilename, pszBuffer);

    /*
     * Search for the file in this directory.
     *   Search cache first
     */
    if (!filecacheFind(pszBuffer))
    {
        char szDir[CCHMAXPATH];

        filePathSlash(pszBuffer, szDir);
        if (!filecacheIsDirCached(szDir))
        {
            /*
             * If caching of entire dirs are enabled, we'll
             * add the directory to the cache and search it.
             */
            if (options.fCacheSearchDirs && filecacheAddDir(szDir))
            {
                if (filecacheFind(pszBuffer))
                    return pszBuffer;
            }
            else
            {
                FILESTATUS3 fsts3;

                /* ask the OS */
                rc = DosQueryPathInfo(pszBuffer, FIL_STANDARD, &fsts3, sizeof(fsts3));
                if (rc == NO_ERROR)
                {   /* add file to cache. */
                    filecacheAddFile(pszBuffer);
                    return pszBuffer;
                }
            }
        }
    }
    else
        return pszBuffer;

    return NULL;
}


/**
 * Finds a filename in a specified pathlist.
 * @returns   Pointer to a filename consiting of the path part + the given filename.
 *            (pointer into pszBuffer)
 *            NULL if file is not found. ("" in buffer)
 * @param     pszPathList  Path list to search for filename.
 * @parma     pszFilename  Filename to find.
 * @parma     pszBuffer    Ouput Buffer.
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
char *pathlistFindFile(const char *pszPathList, const char *pszFilename, char *pszBuffer)
{
    const char *psz = pszPathList;
    const char *pszNext = NULL;

    *pszBuffer = '\0';

    if (pszPathList == NULL)
        return NULL;

    while (*psz != '\0')
    {
        /* find end of this path */
        pszNext = strchr(psz, ';');
        if (pszNext == NULL)
            pszNext = psz + strlen(psz);

        if (pszNext - psz > 0)
        {
            APIRET          rc;

            /* make search statment */
            strncpy(pszBuffer, psz, pszNext - psz);
            pszBuffer[pszNext - psz] = '\0';
            if (pszBuffer[pszNext - psz - 1] != '\\' && pszBuffer[pszNext - psz - 1] != '/')
                strcpy(&pszBuffer[pszNext - psz], "\\");
            strcat(pszBuffer, pszFilename);
            fileNormalize(pszBuffer);

            /*
             * Search for the file in this directory.
             *   Search cache first
             */
            if (!filecacheFind(pszBuffer))
            {
                char szDir[CCHMAXPATH];

                filePathSlash(pszBuffer, szDir);
                if (!filecacheIsDirCached(szDir))
                {
                    /*
                     * If caching of entire dirs are enabled, we'll
                     * add the directory to the cache and search it.
                     */
                    if (options.fCacheSearchDirs && filecacheAddDir(szDir))
                    {
                        if (filecacheFind(pszBuffer))
                            return pszBuffer;
                    }
                    else
                    {
                        FILESTATUS3 fsts3;

                        /* ask the OS */
                        rc = DosQueryPathInfo(pszBuffer, FIL_STANDARD, &fsts3, sizeof(fsts3));
                        if (rc == NO_ERROR)
                        {   /* add file to cache. */
                            filecacheAddFile(pszBuffer);
                            return pszBuffer;
                        }
                    }
                }
            }
            else
                return pszBuffer;
        }

        /* next */
        if (*pszNext != ';')
            break;
        psz = pszNext + 1;
    }

    return NULL;
}


/**
 * Checks if the given filename may exist within any of the given paths.
 * This check only matches the filename path agianst the paths in the pathlist.
 * @returns   TRUE: if exists.
 *            FALSE: don't exist.
 * @param     pszPathList  Path list to search for filename.
 * @parma     pszFilename  Filename to find. The filename should be normalized!
 * @status    completely implemented.
 * @author    knut st. osmundsen
 */
BOOL pathlistFindFile2(const char *pszPathList, const char *pszFilename)
{
    const char *psz = pszPathList;
    const char *pszNext = NULL;
    char        szBuffer[CCHMAXPATH];
    char        szBuffer2[CCHMAXPATH];
    char       *pszPathToFind = &szBuffer2[0];

    /*
     * Input checking
     */
    if (pszPathList == NULL)
        return FALSE;

    /*
     * Normalize the filename and get it's path.
     */
    filePath(pszFilename, pszPathToFind);


    /*
     * Loop thru the path list.
     */
    while (*psz != '\0')
    {
        /* find end of this path */
        pszNext = strchr(psz, ';');
        if (pszNext == NULL)
            pszNext = psz + strlen(psz);

        if (pszNext - psz > 0)
        {
            char *  pszPath = &szBuffer[0];

            /*
             * Extract and normalize the path
             */
            strncpy(pszPath, psz, pszNext - psz);
            pszPath[pszNext - psz] = '\0';
            if (pszPath[pszNext - psz - 1] == '\\' && pszPath[pszNext - psz - 1] == '/')
                pszPath[pszNext - psz - 1] = '\0';
            fileNormalize(pszPath);

            /*
             * Check if it matches the path of the filename
             */
            if (strcmp(pszPath, pszPathToFind) == 0)
                return TRUE;
        }

        /*
         * Next part of the path list.
         */
        if (*pszNext != ';')
            break;
        psz = pszNext + 1;
    }

    return FALSE;
}


/**
 * Finds the first char after word.
 * @returns   Pointer to the first char after word.
 * @param     psz  Where to start.
 */
char *findEndOfWord(char *psz)
{

    while (*psz != '\0' &&
            (
              (*psz >= 'A' && *psz <= 'Z') || (*psz >= 'a' && *psz <= 'z')
              ||
              (*psz >= '0' && *psz <= '9')
              ||
              *psz == '_'
            )
          )
        ++psz;
    return (char *)psz;
}

#if 0 /* not used */
/**
 * Find the starting char of a word
 * @returns   Pointer to first char in word.
 * @param     psz       Where to start.
 * @param     pszStart  Where to stop.
 */
char *findStartOfWord(const char *psz, const char *pszStart)
{
    const char *pszR = psz;
    while (psz >= pszStart &&
            (
                 (*psz >= 'A' && *psz <= 'Z')
              || (*psz >= 'a' && *psz <= 'z')
              || (*psz >= '0' && *psz <= '9')
              || *psz == '_'
             )
          )
        pszR = psz--;
    return (char*)pszR;
}
#endif

/**
 * Find the size of a file.
 * @returns   Size of file. -1 on error.
 * @param     phFile  File handle.
 */
signed long fsize(FILE *phFile)
{
    int ipos;
    signed long cb;

    if ((ipos = ftell(phFile)) < 0
        ||
        fseek(phFile, 0, SEEK_END) != 0
        ||
        (cb = ftell(phFile)) < 0
        ||
        fseek(phFile, ipos, SEEK_SET) != 0
        )
        cb = -1;
    return cb;
}


/**
 * Trims a string, ie. removing spaces (and tabs) from both ends of the string.
 * @returns   Pointer to first not space or tab char in the string.
 * @param     psz   Pointer to the string which is to be trimmed.
 * @status    completely implmented.
 */
INLINE char *trim(char *psz)
{
    int i;
    if (psz == NULL)
        return NULL;
    while (*psz == ' ' || *psz == '\t')
        psz++;
    i = strlen(psz) - 1;
    while (i >= 0 && (psz[i] == ' ' || *psz == '\t'))
        i--;
    psz[i+1] = '\0';
    return psz;
}


/**
 * Right trims a string, ie. removing spaces (and tabs) from the end of the stri
 * @returns   Pointer to the string passed in.
 * @param     psz   Pointer to the string which is to be right trimmed.
 * @status    completely implmented.
 */
INLINE char *trimR(char *psz)
{
    int i;
    if (psz == NULL)
        return NULL;
    i = strlen(psz) - 1;
    while (i >= 0 && (psz[i] == ' ' || *psz == '\t'))
        i--;
    psz[i+1] = '\0';
    return psz;
}


/**
 * Trims any quotes of a possibly quoted string.
 * @returns   Pointer to the string passed in.
 * @param     psz   Pointer to the string which is to be quote-trimmed.
 * @status    completely implmented.
 */
INLINE char *trimQuotes(char *psz)
{
    int i;
    if (psz == NULL)
        return NULL;

    if (*psz == '\"' || *psz == '\'')
        psz++;
    i = strlen(psz) - 1;
    if (psz[i] == '\"' || psz[i] == '\'')
        psz[i] = '\0';

    return psz;
}


/**
 * C/C++ preprocess a single line. Assumes that we're not starting
 * with at comment.
 * @returns Pointer to output buffer.
 * @param   pszOut  Ouput (preprocessed) string.
 * @param   pszIn   Input string.
 */
char *PreProcessLine(char *pszOut, const char *pszIn)
{
    char *  psz = pszOut;
    BOOL    fComment = FALSE;
    BOOL    fQuote = FALSE;

    /*
     * Loop thru the string.
     */
    while (*pszIn != '\0')
    {
        if (fQuote)
        {
            *psz++ = *pszIn;
            if (*pszIn == '\\')
            {
                *psz++ = *++pszIn;
                pszIn++;
            }
            else if (*pszIn++ == '"')
                fQuote = FALSE;
        }
        else if (fComment)
        {
            if (*pszIn == '*' && pszIn[1] == '/')
            {
                fComment = FALSE;
                pszIn += 2;
            }
            else
                pszIn++;
        }
        else
        {
            if (   (*pszIn == '/' && pszIn[1] == '/')
                ||  *pszIn == '\0')
            {   /* End of line. */
                break;
            }

            if (*pszIn == '/' && pszIn[1] == '*')
            {   /* Start comment */
                fComment = TRUE;
                pszIn += 2;
            }
            else
                *psz++ = *pszIn++;
        }
    }

    /*
     * Trim right.
     */
    psz--;
    while (psz >= pszOut && (*psz == ' ' || *psz == '\t'))
        psz--;
    psz[1] = '\0';

    return pszOut;
}


/**
 * Creates a memory buffer for a text file.
 * @returns   Pointer to file memoryblock. NULL on error.
 * @param     pszFilename  Pointer to filename string.
 * @remark    This function is the one using most of the execution
 *            time (DosRead + DosOpen) - about 70% of the execution time!
 */
void *textbufferCreate(const char *pszFilename)
{
    void *pvFile = NULL;
    FILE *phFile;

    phFile = fopen(pszFilename, "rb");
    if (phFile != NULL)
    {
        signed long cbFile = fsize(phFile);
        if (cbFile >= 0)
        {
            pvFile = malloc(cbFile + 1);
            if (pvFile != NULL)
            {
                memset(pvFile, 0, cbFile + 1);
                if (cbFile > 0 && fread(pvFile, 1, cbFile, phFile) == 0)
                {   /* failed! */
                    free(pvFile);
                    pvFile = NULL;
                }
            }
            else
                fprintf(stderr, "warning/error: failed to open file %s\n", pszFilename);
        }
        fclose(phFile);
    }
    return pvFile;
}


/**
 * Destroys a text textbuffer.
 * @param     pvBuffer   Buffer handle.
 */
void textbufferDestroy(void *pvBuffer)
{
    free(pvBuffer);
}


/**
 * Gets the next line from an textbuffer.
 * @returns   Pointer to the next line.
 * @param     pvBuffer  Buffer handle.
 * @param     psz       Pointer to current line.
 *                      NULL is passed in to get the first line.
 */
char *textbufferNextLine(void *pvBuffer, register char *psz)
{
    register char ch;

    /* if first line psz is NULL. */
    if (psz == NULL)
        return (char*)pvBuffer;

    /* skip till end of file or end of line. */
    ch = *psz;
    while (ch != '\0' && ch != '\n' && ch != '\r')
        ch = *++psz;

    /* skip line end */
    if (ch == '\r')
        ch = *++psz;
    if (ch == '\n')
        psz++;

    return psz;
}


/**
 * Gets the next line from an textbuffer.
 * (fgets for textbuffer)
 * @returns   Pointer to pszOutBuffer. NULL when end of file.
 * @param     pvBuffer  Buffer handle.
 * @param     ppv       Pointer to a buffer index pointer. (holds the current buffer index)
 *                      Pointer to a null pointer is passed in to get the first line.
 * @param     pszLineBuffer  Output line buffer. (!= NULL)
 * @param     cchLineBuffer  Size of the output line buffer. (> 0)
 * @remark    '\n' and '\r' are removed!
 */
char *textbufferGetNextLine(void *pvBuffer, void **ppv, char *pszLineBuffer, int cchLineBuffer)
{
    char *          pszLine = pszLineBuffer;
    char *          psz = *(char**)(void*)ppv;
    register char   ch;

    /* first line? */
    if (psz == NULL)
        psz = pvBuffer;

    /* Copy to end of the line or end of the linebuffer. */
    ch = *psz;
    cchLineBuffer--; /* reserve space for '\0' */
    while (cchLineBuffer > 0 && ch != '\0' && ch != '\n' && ch != '\r')
    {
        *pszLine++ = ch;
        ch = *++psz;
    }
    *pszLine = '\0';

    /* skip line end */
    if (ch == '\r')
        ch = *++psz;
    if (ch == '\n')
        psz++;

    /* check if position has changed - if unchanged it's the end of file! */
    if (*ppv == (void*)psz)
        pszLineBuffer = NULL;

    /* store current position */
    *ppv = (void*)psz;

    return pszLineBuffer;
}


/**
 * Appends a depend file to the internal file.
 * This will update the date in the option struct.
 */
BOOL  depReadFile(const char *pszFilename, BOOL fAppend)
{
    void *      pvFile;
    char *      pszNext;
    char *      pszPrev;                /* Previous line, only valid when finding new rule. */
    BOOL        fMoreDeps = FALSE;
    void *      pvRule = NULL;


    /* read depend file */
    pvFile = textbufferCreate(pszFilename);
    if (pvFile == NULL)
        return FALSE;

    /* parse the original depend file */
    pszPrev = NULL;
    pszNext = pvFile;
    while (*pszNext != '\0')
    {
        int   i;
        int   cch;
        char *psz;

        /* get the next line. */
        psz = pszNext;
        pszNext = textbufferNextLine(pvFile, pszNext);

        /*
         * Process the current line:
         *   Start off by terminating the line.
         *   Trim the line,
         *   Skip empty lines.
         *   If not looking for more deps Then
         *     Check if new rule starts here.
         *   Endif
         *
         *   If more deps to last rule Then
         *     Get dependant name.
         *   Endif
         */
        i = -1;
        while (psz <= &pszNext[i] && pszNext[i] == '\n' || pszNext[i] == '\r')
            pszNext[i--] = '\0';
        trimR(psz);
        cch = strlen(psz);
        if (cch == 0)
        {
            fMoreDeps = FALSE;
            continue;
        }

        if (*psz == '#')
        {
            pszPrev = psz;
            continue;
        }

        /* new rule? */
        if (!fMoreDeps)
        {
            if (*psz != ' ' && *psz != '\t' && *psz != '\0')
            {
                i = 0;
                while (psz[i] != '\0')
                {
                    if (psz[i] == ':'
                        && (psz[i+1] == ' '
                            || psz[i+1] == '\t'
                            || psz[i+1] == '\0'
                            || (psz[i+1] == '\\' && psz[i+2] == '\0')
                            )
                        )
                    {
                        char    szTS[TS_SIZE];
                        char *  pszCont = strchr(&psz[i], '\\');
                        fMoreDeps = pszCont != NULL && pszCont[1] == '\0';

                        /* read evt. timestamp. */
                        szTS[0] = '\0';
                        if (pszPrev && strlen(pszPrev) > 25 && *pszPrev == '#')
                            strcpy(szTS, pszPrev + 2);

                        psz[i] = '\0';
                        pvRule = depAddRule(trimQuotes(trimR(psz)), NULL, NULL, szTS, TRUE);
                        if (pvRule)
                            ((PDEPRULE)pvRule)->fUpdated = fAppend;
                        psz += i + 1;
                        cch -= i + 1;
                        break;
                    }
                    i++;
                }
            }
            pszPrev = NULL;
        }


        /* more dependants */
        if (fMoreDeps)
        {
            if (cch > 0 && psz[cch-1] == '\\')
            {
                fMoreDeps = TRUE;
                psz[cch-1] = '\0';
            }
            else
                fMoreDeps = FALSE;

            /* if not duplicate rule */
            if (pvRule != NULL)
            {
                psz = trimQuotes(trim(psz));
                if (*psz != '\0')
                    depAddDepend(pvRule, psz, options.fCheckCyclic, TRUE);
            }
        }
    } /* while */


    /* return succesfully */
    textbufferDestroy(pvFile);
    return TRUE;
}

/**
 *
 * @returns   Success indicator.
 * @param     pszFilename           Pointer to name of the output file.
 * @param     fWriteUpdatedOnly     If set we'll only write updated rules.
 */
BOOL  depWriteFile(const char *pszFilename, BOOL fWriteUpdatedOnly)
{
    FILE *phFile;
    phFile = fopen(pszFilename, "w");
    if (phFile != NULL)
    {
        AVLENUMDATA EnumData;
        PDEPRULE    pdep;
        static char szBuffer[0x10000];
        int         iBuffer = 0;
        int         cch;

        /*
         * Write warning on top of file.
         */
        fputs("#\n"
              "# This file was automatically generated by FastDep.\n"
              "# FastDep was written by knut st. osmundsen, and it's GPL software.\n"
              "#\n"
              "# THIS FILE SHOULD   N O T   BE EDITED MANUALLY!!!\n"
              "#\n"
              "# (As this may possibly make it unreadable for fastdep\n"
              "#  and ruin the caching methods of FastDep.)\n"
              "#\n"
              "\n",
              phFile);

        /* normal dependency output */
        pdep = (PDEPRULE)(void*)AVLBeginEnumTree((PPAVLNODECORE)(void*)&pdepTree, &EnumData, TRUE);
        while (pdep != NULL)
        {
            if (!fWriteUpdatedOnly || pdep->fUpdated)
            {
                int cchTS = strlen(pdep->szTS);
                int fQuoted = strpbrk(pdep->pszRule, " \t") != NULL; /* TODO/BUGBUG/FIXME: are there more special chars to look out for?? */

                /* Write rule. Flush the buffer first if necessary. */
                cch = strlen(pdep->pszRule);
                if (iBuffer + cch*3 + fQuoted * 2 + cchTS + 9 >= sizeof(szBuffer))
                {
                    fwrite(szBuffer, iBuffer, 1, phFile);
                    iBuffer = 0;
                }

                memcpy(szBuffer + iBuffer, "# ", 2);
                memcpy(szBuffer + iBuffer + 2, pdep->szTS, cchTS);
                iBuffer += cchTS + 2;
                szBuffer[iBuffer++] = '\n';

                if (fQuoted) szBuffer[iBuffer++] = '"';
                iBuffer += depNameToMake(szBuffer + iBuffer, sizeof(szBuffer) - iBuffer, pdep->pszRule);
                if (fQuoted) szBuffer[iBuffer++] = '"';
                strcpy(szBuffer + iBuffer++, ":");

                /* write rule dependants. */
                if (pdep->papszDep != NULL)
                {
                    char **ppsz = pdep->papszDep;
                    while (*ppsz != NULL)
                    {
                        /* flush buffer? */
                        fQuoted = strpbrk(*ppsz, " \t") != NULL; /* TODO/BUGBUG/FIXME: are there more special chars to look out for?? */
                        cch = strlen(*ppsz);
                        if (iBuffer + cch*3 + fQuoted * 2 + 20 >= sizeof(szBuffer))
                        {
                            fwrite(szBuffer, iBuffer, 1, phFile);
                            iBuffer = 0;
                        }
                        strcpy(szBuffer + iBuffer, " \\\n    ");
                        iBuffer += 7;
                        if (fQuoted) szBuffer[iBuffer++] = '"';
                        iBuffer += depNameToMake(szBuffer + iBuffer, sizeof(szBuffer) - iBuffer, *ppsz);
                        if (fQuoted) szBuffer[iBuffer++] = '"';

                        /* next dependant */
                        ppsz++;
                    }
                }

                /* Add two new lines. Flush buffer first if necessary. */
                if (iBuffer + CBNEWLINE*2 >= sizeof(szBuffer))
                {
                    fwrite(szBuffer, iBuffer, 1, phFile);
                    iBuffer = 0;
                }

                /* add 2 linefeeds */
                strcpy(szBuffer + iBuffer, "\n\n");
                iBuffer += CBNEWLINE*2;
            }

            /* next rule */
            pdep = (PDEPRULE)(void*)AVLGetNextNode(&EnumData);
        }


        /* flush buffer. */
        fwrite(szBuffer, iBuffer, 1, phFile);

        fclose(phFile);
        return TRUE;
    }

    return FALSE;
}


/**
 * Removes all nodes in the tree of dependencies. (pdepTree)
 */
void  depRemoveAll(void)
{
    AVLENUMDATA EnumData;
    PDEPRULE    pdep;

    pdep = (PDEPRULE)(void*)AVLBeginEnumTree((PPAVLNODECORE)(void*)&pdepTree, &EnumData, TRUE);
    while (pdep != NULL)
    {
        /* free this */
        if (pdep->papszDep != NULL)
        {
            char ** ppsz = pdep->papszDep;
            while (*ppsz != NULL)
                free(*ppsz++);
            free(pdep->papszDep);
        }
        free(pdep);

        /* next */
        pdep = (PDEPRULE)(void*)AVLGetNextNode(&EnumData);
    }
    pdepTree = NULL;
}


/**
 * Adds a rule to the list of dependant rules.
 * @returns   Rule handle. NULL if rule exists/error.
 * @param     pszRulePath   Pointer to rule text. Empty strings are banned!
 *                          This string might only contain the path of the rule. (with '\\')
 * @param     pszName       Name of the rule.
 *                          NULL if pszRulePath contains the entire rule.
 * @param     pszExt        Extention (without '.')
 *                          NULL if pszRulePath or pszRulePath and pszName contains the entire rule.
 * @param     fConvertName  If set we'll convert from makefile name to realname.
 */
void *depAddRule(const char *pszRulePath, const char *pszName, const char *pszExt, const char *pszTS, BOOL fConvertName)
{
    char     szRule[CCHMAXPATH*2];
    PDEPRULE pNew;
    int      cch;

    /* make rulename */
    strcpy(szRule, pszRulePath);
    cch = strlen(szRule);
    if (pszName != NULL)
    {
        strcpy(szRule + cch, pszName);
        cch += strlen(szRule + cch);
    }
    if (pszExt != NULL)
    {
        strcat(szRule + cch++, ".");
        strcat(szRule + cch, pszExt);
        cch += strlen(szRule + cch);
    }
    if (fConvertName)
        cch = depNameToReal(szRule);

    /*
     * Allocate a new rule structure and fill in data
     * Note. One block for both the DEPRULE and the pszRule string.
     */
    pNew = malloc(sizeof(DEPRULE) + cch + 1);
    if (pNew == NULL)
    {
        fprintf(stderr, "error: out of memory. (line=%d)\n", __LINE__);
        return NULL;
    }
    pNew->pszRule = (char*)(void*)(pNew + 1);
    strcpy(pNew->pszRule, szRule);
    pNew->cDeps = 0;
    pNew->papszDep = NULL;
    pNew->fUpdated = TRUE;
    pNew->avlCore.Key = pNew->pszRule;
    strcpy(pNew->szTS, pszTS);

    /* Insert the rule */
    if (!AVLInsert((PPAVLNODECORE)(void*)&pdepTree, &pNew->avlCore))
    {   /*
         * The rule existed.
         * If it's allready touched (updated) during this session
         *   there is nothing to be done.
         * If not force scan and it's newer than depfile-1month then
         *   we'll use the information we've got.
         * Reuse the node in the tree.
         */
        PDEPRULE    pOld = (PDEPRULE)(void*)AVLGet((PPAVLNODECORE)(void*)&pdepTree, pNew->avlCore.Key);
        assert(pOld);
        free(pNew);
        if (pOld->fUpdated)
            return NULL;

        pOld->fUpdated = TRUE;
        if (!options.fForceScan && !strcmp(pOld->szTS, pszTS) && depValidate(pOld))
            return NULL;
        strcpy(pOld->szTS, pszTS);

        if (pOld->papszDep)
        {
            free(pOld->papszDep);
            pOld->papszDep = NULL;
        }
        pOld->cDeps = 0;

        return pOld;
    }

    return pNew;
}


/**
 * Adds a dependant to a rule.
 * @returns   Successindicator. TRUE = success.
 *            FALSE = cyclic or out of memory.
 * @param     pvRule        Rule handle.
 * @param     pszDep        Pointer to dependant name
 * @param     fCheckCyclic  When set we'll check that we're not creating an cyclic dependency.
 * @param     fConvertName  If set we'll convert from makefile name to realname.
 */
BOOL  depAddDepend(void *pvRule, const char *pszDep, BOOL fCheckCyclic, BOOL fConvertName)
{
    PDEPRULE    pdep = (PDEPRULE)pvRule;
    int         cchDep;

    if (pszDep[0] == '\0')
    {
        fprintf(stderr, "warning-internal: empty dependancy filename to '%s'. Ignored.\n",
                pdep->pszRule);
        /* __interrupt(3); */
        return FALSE;
    }

    if (fCheckCyclic && depCheckCyclic(pdep, pszDep))
    {
        fprintf(stderr, "warning: Cylic dependancy caused us to ignore '%s' in rule '%s'.\n",
                pszDep, pdep->pszRule);
        return FALSE;
    }

    /* allocate more array space */
    if (((pdep->cDeps) % 48) == 0)
    {
        pdep->papszDep = realloc(pdep->papszDep, sizeof(char*) * (pdep->cDeps + 50));
        if (pdep->papszDep == NULL)
        {
            pdep->cDeps = 0;
            fprintf(stderr, "error: out of memory, (line=%d)\n", __LINE__);
            return FALSE;
        }
    }

    /* allocate string space and copy pszDep */
    cchDep = strlen(pszDep) + 1;
    if ((pdep->papszDep[pdep->cDeps] = malloc(cchDep)) == NULL)
    {
        fprintf(stderr, "error: out of memory, (line=%d)\n", __LINE__);
        return FALSE;
    }
    strcpy(pdep->papszDep[pdep->cDeps], pszDep);

    /* convert ^# and other stuff */
    if (fConvertName)
        depNameToReal(pdep->papszDep[pdep->cDeps]);

    /* terminate array and increment dep count */
    pdep->papszDep[++pdep->cDeps] = NULL;

    /* successful! */
    return TRUE;
}


/**
 * Converts from makefile filename to real filename.
 * @returns New name length.
 * @param   pszName     Pointer to the string to make real.
 */
int depNameToReal(char *pszName)
{
    int cchNewName = strlen(pszName);
    int iDisplacement = 0;

    /*
     * Look for '^' and '$$'.
     */
    while (*pszName)
    {
        if (    *pszName == '^'
            ||  (*pszName == '$' && pszName[1] == '$'))
        {
            iDisplacement--;
            pszName++;
            cchNewName--;
        }
        if (iDisplacement)
            pszName[iDisplacement] = *pszName;
        pszName++;
    }
    pszName[iDisplacement] = '\0';

    return cchNewName;
}


/**
 * Converts from real filename to makefile filename.
 * @returns New name length.
 * @param   pszName     Output name buffer.
 * @param   cchName     Size of name buffer.
 * @param   pszSrc      Input name.
 */
int   depNameToMake(char *pszName, int cchName, const char *pszSrc)
{
    char *pszNameOrg = pszName;

    /*
     * Convert real name to makefile name.
     */
    while (*pszSrc)
    {
        if (    *pszSrc == '#'
            ||  *pszSrc == '!'
            ||  (*pszSrc == '$' && pszSrc[1] != '(')
            ||  *pszSrc == '@'
            ||  *pszSrc == '-'
            ||  *pszSrc == '^'
           /* ||  *pszSrc == '('
            ||  *pszSrc == ')'
            ||  *pszSrc == '{'
            ||  *pszSrc == '}'*/)
        {
            if (!cchName--)
            {
                fprintf(stderr, "error: buffer too small, (line=%d)\n", __LINE__);
                return pszName - pszNameOrg + strlen(pszName);
            }
            *pszName++ = '^';
        }
        if (!cchName--)
        {
            fprintf(stderr, "error: buffer too small, (line=%d)\n", __LINE__);
            return pszName - pszNameOrg + strlen(pszName);
        }
        *pszName++ = *pszSrc++;
    }
    *pszName = '\0';

    return pszName - pszNameOrg;
}



/**
 * Marks the file as one which is to be rescanned next time
 * since not all dependencies was found...
 * @param   pvRule  Rule handle...
 */
void  depMarkNotFound(void *pvRule)
{
    ((PDEPRULE)pvRule)->szTS[0] = '\0';
}


/**
 * Checks if adding this dependent will create a cyclic dependency.
 * @returns   TRUE: Cyclic.
 *            FALSE: Non-cylic.
 * @param     pdepRule  Rule pszDep is to be inserted in.
 * @param     pszDep    Depend name.
 */
BOOL depCheckCyclic(PDEPRULE pdepRule, const char *pszDep)
{
#define DEPTH_FIRST 1
#ifdef DEPTH_FIRST
    #define DEPTH 32
#else
    #define DEPTH 128
#endif
    #define HISTORY 256
    char *  pszRule = pdepRule->pszRule;
    char ** appsz[DEPTH];
#if HISTORY
    char *  apszHistory[HISTORY];
    int     iHistory;
    int     j;
    int     iStart;
    int     iEnd;
    int     iCmp;
#endif
    PDEPRULE pdep;
    int     i;

    /* self check */
    if (strcmp(pdepRule->pszRule, pszDep) == 0)
        return TRUE;

    /* find rule for the dep. */
    if ((pdep = (PDEPRULE)(void*)AVLGet((PPAVLNODECORE)(void*)&pdepTree, pszDep)) == NULL
        || pdep->papszDep == NULL)
        return FALSE; /* no rule, or no dependents, not cyclic */

    i = 1;
    appsz[0] = pdep->papszDep;
#ifdef HISTORY
    iHistory = 1;
    apszHistory[0] = pdep->pszRule;
#endif
    while (i > 0)
    {
        /* pop off element */
        register char **  ppsz = appsz[--i];

        while (*ppsz != NULL)
        {
            /* check if equal to the main rule */
            if (strcmp(pszRule, *ppsz) == 0)
                return TRUE;

            /* push onto stack (ppsz is incremented in this test!) */
            if ((pdep = (PDEPRULE)(void*)AVLGet((PPAVLNODECORE)(void*)&pdepTree, *ppsz++)) != NULL
                && pdep->papszDep != NULL)
            {
                if (i >= DEPTH)
                {
                    fprintf(stderr, "error: too deep chain (%d). pszRule=%s  pszDep=%s\n",
                            i, pszRule, pszDep);
                    return FALSE;
                }
#ifdef HISTORY
                /*
                 * Check if in history, if so we'll skip it.
                 */
                #if 0
                for (j = 0;  j < iHistory; j++)
                    if (!strcmp(apszHistory[j], pdep->pszRule))
                        break;
                if (j != iHistory)
                    continue;           /* found */

                /*
                 * Push into history - might concider make this binary sorted one day.
                 */
                if (iHistory < HISTORY)
                    apszHistory[iHistory++] = pdep->pszRule;

                #else

                /*
                 * Check if in history, if so we'll skip it.
                 *  (Binary search)
                 * ASSUMES: Always something in the history!
                 */
                iEnd = iHistory - 1;
                iStart = 0;
                j = iHistory / 2;
                while (    (iCmp = strcmp(pdep->pszRule, apszHistory[j])) != 0
                       &&   iEnd != iStart)
                {
                    if (iCmp < 0)
                        iEnd = j - 1;
                    else
                        iStart = j + 1;
                    if (iStart > iEnd)
                        break;
                    j = (iStart + iEnd) / 2;
                }

                if (!iCmp)
                    continue;           /* found */

                /*
                 * Push into history - might concider make this binary sorted one day.
                 */
                if (iHistory < HISTORY)
                {
                    int k;
                    if (iCmp > 0)       /* Insert after. */
                        j++;
                    for (k = iHistory; k > j; k--)
                        apszHistory[k] = apszHistory[k - 1];
                    apszHistory[j] = pdep->pszRule;
                    iHistory++;
                }

                #endif

#endif
                /*
                 * Push on to the stack.
                 */
                #ifdef DEPTH_FIRST
                /* dept first */
                appsz[i++] = ppsz;      /* save current posistion */
                ppsz = pdep->papszDep;  /* process new node */
                #else
                /* complete current node first. */
                appsz[i++] = pdep->papszDep;
                #endif
            }
        }
    }

    return FALSE;
}


/**
 * Validates that the dependencies for the file exists
 * in the given locations. Dependants without path is ignored.
 * @returns TRUE if all ok.
 *          FALSE if one (or possibly more) dependants are non-existing.
 * @param   pdepRule    Pointer to rule we're to validate.
 */
BOOL depValidate(PDEPRULE pdepRule)
{
    int i;

    for (i = 0; i < pdepRule->cDeps; i++)
    {
        char *psz = pdepRule->papszDep[i];
        if (    !strchr(psz, '$')
            &&
            (   psz[1] == ':'
            ||  strchr(psz, '\\')
            ||  strchr(psz, '/')
                 )
            )
        {
            /*
             * Check existance of the file.
             *   Search cache first
             */
            if (!filecacheFind(psz))
            {
                char szDir[CCHMAXPATH];

                filePathSlash(psz, szDir);
                if (!filecacheIsDirCached(szDir))
                {
                    /*
                     * If caching of entire dirs are enabled, we'll
                     * add the directory to the cache and search it.
                     */
                    if (options.fCacheSearchDirs && filecacheAddDir(szDir))
                    {
                        if (!filecacheFind(psz))
                            return FALSE;
                    }
                    else
                    {
                        FILESTATUS3 fsts3;

                        /* ask the OS */
                        if (DosQueryPathInfo(psz, FIL_STANDARD, &fsts3, sizeof(fsts3)))
                            return FALSE;
                        /* add file to cache. */
                        filecacheAddFile(psz);
                    }
                }
                /*
                 * Dir was cached, hence the file doesn't exist
                 * and the we should rescan the source file.
                 */
                else
                    return FALSE;
            }
        }
    }

    return TRUE;
}


/**
 * Make a timestamp from the file data provided thru the
 * search API.
 * @returns Pointer to pszTS
 * @param   pszTS       Pointer to timestamp (output).
 * @param   pfindbuf3   Pointer to search result.
 */
INLINE char *depMakeTS(char *pszTS, PFILEFINDBUF3 pfindbuf3)
{
    sprintf(pszTS, "%04d-%02d-%02d-%02d.%02d.%02d 0x%04x%04x %d",
            pfindbuf3->fdateLastWrite.year + 1980,
            pfindbuf3->fdateLastWrite.month,
            pfindbuf3->fdateLastWrite.day,
            pfindbuf3->ftimeLastWrite.hours,
            pfindbuf3->ftimeLastWrite.minutes,
            pfindbuf3->ftimeLastWrite.twosecs * 2,
            (ULONG)*(PUSHORT)(void*)&pfindbuf3->fdateCreation,
            (ULONG)*(PUSHORT)(void*)&pfindbuf3->ftimeCreation,
            pfindbuf3->cbFile);
    return pszTS;
}


/**
 * Adds the src additioanl dependenies to a rule.
 * @param   pvRule  Rule to add them to.
 * @param   pszz    Pointer to the string of strings of extra dependencies.
 */
void depAddSrcAddDeps(void *pvRule, const char *pszz)
{
    while (*pszz)
    {
        depAddDepend(pvRule, pszz, FALSE, FALSE);
        pszz += strlen(pszz) + 1;
    }
}





/*
 * Testing purpose.
 */

#if !defined(OS2FAKE)
#include <os2.h>
#endif
#ifdef OLEMANN
#include "olemann.h"
#endif