aboutsummaryrefslogtreecommitdiff
path: root/suff.c
blob: db9ad982ecefc0b68d81b3c1288971615d280018 (plain) (blame)
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
/*	$NetBSD: suff.c,v 1.335 2021/01/10 21:20:46 rillig Exp $	*/

/*
 * Copyright (c) 1988, 1989, 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Adam de Boor.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Copyright (c) 1989 by Berkeley Softworks
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Adam de Boor.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Maintain suffix lists and find implicit dependents using suffix
 * transformation rules such as ".c.o".
 *
 * Interface:
 *	Suff_Init	Initialize the module.
 *
 *	Suff_End	Clean up the module.
 *
 *	Suff_DoPaths	Extend the search path of each suffix to include the
 *			default search path.
 *
 *	Suff_ClearSuffixes
 *			Clear out all the suffixes and transformations.
 *
 *	Suff_IsTransform
 *			See if the passed string is a transformation rule.
 *
 *	Suff_AddSuffix	Add the passed string as another known suffix.
 *
 *	Suff_GetPath	Return the search path for the given suffix.
 *
 *	Suff_AddInclude
 *			Mark the given suffix as denoting an include file.
 *
 *	Suff_AddLib	Mark the given suffix as denoting a library.
 *
 *	Suff_AddTransform
 *			Add another transformation to the suffix graph.
 *
 *	Suff_SetNull	Define the suffix to consider the suffix of
 *			any file that doesn't have a known one.
 *
 *	Suff_FindDeps	Find implicit sources for and the location of
 *			a target based on its suffix. Returns the
 *			bottom-most node added to the graph or NULL
 *			if the target had no implicit sources.
 *
 *	Suff_FindPath	Return the appropriate path to search in order to
 *			find the node.
 */

#include "make.h"
#include "dir.h"

/*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
MAKE_RCSID("$NetBSD: suff.c,v 1.335 2021/01/10 21:20:46 rillig Exp $");

typedef List SuffixList;
typedef ListNode SuffixListNode;

typedef List CandidateList;
typedef ListNode CandidateListNode;

/* The defined suffixes, such as '.c', '.o', '.l'. */
static SuffixList sufflist = LST_INIT;
#ifdef CLEANUP
/* The suffixes to be cleaned up at the end. */
static SuffixList suffClean = LST_INIT;
#endif

/*
 * The transformation rules, such as '.c.o' to transform '.c' into '.o',
 * or simply '.c' to transform 'file.c' into 'file'.
 */
static GNodeList transforms = LST_INIT;

/*
 * Counter for assigning suffix numbers.
 * TODO: What are these suffix numbers used for?
 */
static int sNum = 0;

typedef enum SuffixFlags {
	SUFF_NONE	= 0,

	/*
	 * This suffix marks include files.  Their search path ends up in the
	 * undocumented special variable '.INCLUDES'.
	 */
	SUFF_INCLUDE	= 1 << 0,

	/*
	 * This suffix marks library files.  Their search path ends up in the
	 * undocumented special variable '.LIBS'.
	 */
	SUFF_LIBRARY	= 1 << 1,

	/*
	 * The empty suffix.
	 *
	 * XXX: What is the difference between the empty suffix and the null
	 * suffix?
	 *
	 * XXX: Why is SUFF_NULL needed at all? Wouldn't nameLen == 0 mean
	 * the same?
	 */
	SUFF_NULL	= 1 << 2

} SuffixFlags;

ENUM_FLAGS_RTTI_3(SuffixFlags,
    SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);

typedef List SuffixListList;

/*
 * A suffix such as ".c" or ".o" that is used in suffix transformation rules
 * such as ".c.o:".
 */
typedef struct Suffix {
	/* The suffix itself, such as ".c" */
	char *name;
	/* Length of the name, to avoid strlen calls */
	size_t nameLen;
	/* Type of suffix */
	SuffixFlags flags;
	/* The path along which files of this suffix may be found */
	SearchPath *searchPath;
	/* The suffix number; TODO: document the purpose of this number */
	int sNum;
	/* Reference count of list membership and several other places */
	int refCount;
	/* Suffixes we have a transformation to */
	SuffixList parents;
	/* Suffixes we have a transformation from */
	SuffixList children;

	/* Lists in which this suffix is referenced.
	 *
	 * XXX: These lists are used nowhere, they are just appended to, for
	 * no apparent reason.  They do have the side effect of increasing
	 * refCount though. */
	SuffixListList ref;
} Suffix;

/*
 * A candidate when searching for implied sources.
 *
 * For example, when "src.o" is to be made, a typical candidate is "src.c"
 * via the transformation rule ".c.o".  If that doesn't exist, maybe there is
 * another transformation rule ".pas.c" that would make "src.pas" an indirect
 * candidate as well.  The first such chain that leads to an existing file or
 * node is finally chosen to be made.
 */
typedef struct Candidate {
	/* The file or node to look for. */
	char *file;
	/* The prefix from which file was formed.
	 * Its memory is shared among all candidates. */
	char *prefix;
	/* The suffix on the file. */
	Suffix *suff;

	/* The candidate that can be made from this,
	 * or NULL for the top-level candidate. */
	struct Candidate *parent;
	/* The node describing the file. */
	GNode *node;

	/* Count of existing children, only used for memory management, so we
	 * don't free this candidate too early or too late. */
	int numChildren;
#ifdef DEBUG_SRC
	CandidateList childrenList;
#endif
} Candidate;

typedef struct CandidateSearcher {

	CandidateList list;

	/*
	 * TODO: Add HashSet for seen entries, to avoid endless loops such as
	 * in suff-transform-endless.mk.
	 */

} CandidateSearcher;


/* TODO: Document the difference between nullSuff and emptySuff. */
/* The NULL suffix for this run */
static Suffix *nullSuff;
/* The empty suffix required for POSIX single-suffix transformation rules */
static Suffix *emptySuff;


static Suffix *
Suffix_Ref(Suffix *suff)
{
	suff->refCount++;
	return suff;
}

/* Change the value of a Suffix variable, adjusting the reference counts. */
static void
Suffix_Reassign(Suffix **var, Suffix *suff)
{
	if (*var != NULL)
		(*var)->refCount--;
	*var = suff;
	suff->refCount++;
}

/* Set a Suffix variable to NULL, adjusting the reference count. */
static void
Suffix_Unassign(Suffix **var)
{
	if (*var != NULL)
		(*var)->refCount--;
	*var = NULL;
}

/*
 * See if pref is a prefix of str.
 * Return NULL if it ain't, pointer to character in str after prefix if so.
 */
static const char *
StrTrimPrefix(const char *pref, const char *str)
{
	while (*str != '\0' && *pref == *str) {
		pref++;
		str++;
	}

	return *pref != '\0' ? NULL : str;
}

/*
 * See if suff is a suffix of str, and if so, return the pointer to the suffix
 * in str, which at the same time marks the end of the prefix.
 */
static const char *
StrTrimSuffix(const char *str, size_t strLen, const char *suff, size_t suffLen)
{
	const char *suffInStr;
	size_t i;

	if (strLen < suffLen)
		return NULL;

	suffInStr = str + strLen - suffLen;
	for (i = 0; i < suffLen; i++)
		if (suff[i] != suffInStr[i])
			return NULL;

	return suffInStr;
}

/*
 * See if suff is a suffix of name, and if so, return the end of the prefix
 * in name.
 */
static const char *
Suffix_TrimSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
{
	return StrTrimSuffix(nameEnd - nameLen, nameLen,
	    suff->name, suff->nameLen);
}

static Boolean
Suffix_IsSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
{
	return Suffix_TrimSuffix(suff, nameLen, nameEnd) != NULL;
}

static Suffix *
FindSuffixByNameLen(const char *name, size_t nameLen)
{
	SuffixListNode *ln;

	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
		Suffix *suff = ln->datum;
		if (suff->nameLen == nameLen &&
		    memcmp(suff->name, name, nameLen) == 0)
			return suff;
	}
	return NULL;
}

static Suffix *
FindSuffixByName(const char *name)
{
	return FindSuffixByNameLen(name, strlen(name));
}

static GNode *
FindTransformByName(const char *name)
{
	GNodeListNode *ln;

	for (ln = transforms.first; ln != NULL; ln = ln->next) {
		GNode *gn = ln->datum;
		if (strcmp(gn->name, name) == 0)
			return gn;
	}
	return NULL;
}

static void
SuffixList_Unref(SuffixList *list, Suffix *suff)
{
	SuffixListNode *ln = Lst_FindDatum(list, suff);
	if (ln != NULL) {
		Lst_Remove(list, ln);
		suff->refCount--;
	}
}

/* Free up all memory associated with the given suffix structure. */
static void
Suffix_Free(Suffix *suff)
{

	if (suff == nullSuff)
		nullSuff = NULL;

	if (suff == emptySuff)
		emptySuff = NULL;

#if 0
	/* We don't delete suffixes in order, so we cannot use this */
	if (suff->refCount != 0)
		Punt("Internal error deleting suffix `%s' with refcount = %d",
		    suff->name, suff->refCount);
#endif

	Lst_Done(&suff->ref);
	Lst_Done(&suff->children);
	Lst_Done(&suff->parents);
	SearchPath_Free(suff->searchPath);

	free(suff->name);
	free(suff);
}

static void
SuffFree(void *p)
{
	Suffix_Free(p);
}

/* Remove the suffix from the list, and free if it is otherwise unused. */
static void
SuffixList_Remove(SuffixList *list, Suffix *suff)
{
	SuffixList_Unref(list, suff);
	if (suff->refCount == 0) {
		/* XXX: can lead to suff->refCount == -1 */
		SuffixList_Unref(&sufflist, suff);
		DEBUG1(SUFF, "Removing suffix \"%s\"\n", suff->name);
		SuffFree(suff);
	}
}

/*
 * Insert the suffix into the list, keeping the list ordered by suffix
 * number.
 */
static void
SuffixList_Insert(SuffixList *list, Suffix *suff)
{
	SuffixListNode *ln;
	Suffix *listSuff = NULL;

	for (ln = list->first; ln != NULL; ln = ln->next) {
		listSuff = ln->datum;
		if (listSuff->sNum >= suff->sNum)
			break;
	}

	if (ln == NULL) {
		DEBUG2(SUFF, "inserting \"%s\" (%d) at end of list\n",
		    suff->name, suff->sNum);
		Lst_Append(list, Suffix_Ref(suff));
		Lst_Append(&suff->ref, list);
	} else if (listSuff->sNum != suff->sNum) {
		DEBUG4(SUFF, "inserting \"%s\" (%d) before \"%s\" (%d)\n",
		    suff->name, suff->sNum, listSuff->name, listSuff->sNum);
		Lst_InsertBefore(list, ln, Suffix_Ref(suff));
		Lst_Append(&suff->ref, list);
	} else {
		DEBUG2(SUFF, "\"%s\" (%d) is already there\n",
		    suff->name, suff->sNum);
	}
}

static void
Relate(Suffix *srcSuff, Suffix *targSuff)
{
	SuffixList_Insert(&targSuff->children, srcSuff);
	SuffixList_Insert(&srcSuff->parents, targSuff);
}

static Suffix *
Suffix_New(const char *name)
{
	Suffix *suff = bmake_malloc(sizeof *suff);

	suff->name = bmake_strdup(name);
	suff->nameLen = strlen(suff->name);
	suff->searchPath = SearchPath_New();
	Lst_Init(&suff->children);
	Lst_Init(&suff->parents);
	Lst_Init(&suff->ref);
	suff->sNum = sNum++;
	suff->flags = SUFF_NONE;
	suff->refCount = 1; /* XXX: why 1? It's not assigned anywhere yet. */

	return suff;
}

/*
 * Nuke the list of suffixes but keep all transformation rules around. The
 * transformation graph is destroyed in this process, but we leave the list
 * of rules so when a new graph is formed, the rules will remain. This
 * function is called when a line '.SUFFIXES:' with an empty suffixes list is
 * encountered in a makefile.
 */
void
Suff_ClearSuffixes(void)
{
#ifdef CLEANUP
	Lst_MoveAll(&suffClean, &sufflist);
#endif
	DEBUG0(SUFF, "Clearing all suffixes\n");
	Lst_Init(&sufflist);
	sNum = 0;
	if (nullSuff != NULL)
		SuffFree(nullSuff);
	emptySuff = nullSuff = Suffix_New("");

	SearchPath_AddAll(nullSuff->searchPath, &dirSearchPath);
	nullSuff->flags = SUFF_NULL;
}

/*
 * Parse a transformation string such as ".c.o" to find its two component
 * suffixes (the source ".c" and the target ".o").  If there are no such
 * suffixes, try a single-suffix transformation as well.
 *
 * Return TRUE if the string is a valid transformation.
 */
static Boolean
ParseTransform(const char *str, Suffix **out_src, Suffix **out_targ)
{
	SuffixListNode *ln;
	Suffix *single = NULL;

	/*
	 * Loop looking first for a suffix that matches the start of the
	 * string and then for one that exactly matches the rest of it. If
	 * we can find two that meet these criteria, we've successfully
	 * parsed the string.
	 */
	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
		Suffix *src = ln->datum;

		if (StrTrimPrefix(src->name, str) == NULL)
			continue;

		if (str[src->nameLen] == '\0') {
			single = src;
		} else {
			Suffix *targ = FindSuffixByName(str + src->nameLen);
			if (targ != NULL) {
				*out_src = src;
				*out_targ = targ;
				return TRUE;
			}
		}
	}

	if (single != NULL) {
		/*
		 * There was a suffix that encompassed the entire string, so we
		 * assume it was a transformation to the null suffix (thank you
		 * POSIX; search for "single suffix" or "single-suffix").
		 *
		 * We still prefer to find a double rule over a singleton,
		 * hence we leave this check until the end.
		 *
		 * XXX: Use emptySuff over nullSuff?
		 */
		*out_src = single;
		*out_targ = nullSuff;
		return TRUE;
	}
	return FALSE;
}

/*
 * Return TRUE if the given string is a transformation rule, that is, a
 * concatenation of two known suffixes such as ".c.o" or a single suffix
 * such as ".o".
 */
Boolean
Suff_IsTransform(const char *str)
{
	Suffix *src, *targ;

	return ParseTransform(str, &src, &targ);
}

/*
 * Add the transformation rule to the list of rules and place the
 * transformation itself in the graph.
 *
 * The transformation is linked to the two suffixes mentioned in the name.
 *
 * Input:
 *	name		must have the form ".from.to" or just ".from"
 *
 * Results:
 *	The created or existing transformation node in the transforms list
 */
GNode *
Suff_AddTransform(const char *name)
{
	Suffix *srcSuff;
	Suffix *targSuff;

	GNode *gn = FindTransformByName(name);
	if (gn == NULL) {
		/*
		 * Make a new graph node for the transformation. It will be
		 * filled in by the Parse module.
		 */
		gn = GNode_New(name);
		Lst_Append(&transforms, gn);
	} else {
		/*
		 * New specification for transformation rule. Just nuke the
		 * old list of commands so they can be filled in again. We
		 * don't actually free the commands themselves, because a
		 * given command can be attached to several different
		 * transformations.
		 */
		Lst_Done(&gn->commands);
		Lst_Init(&gn->commands);
		Lst_Done(&gn->children);
		Lst_Init(&gn->children);
	}

	gn->type = OP_TRANSFORM;

	{
		/* TODO: Avoid the redundant parsing here. */
		Boolean ok = ParseTransform(name, &srcSuff, &targSuff);
		assert(ok);
		(void)ok;
	}

	/* Link the two together in the proper relationship and order. */
	DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
	    srcSuff->name, targSuff->name);
	Relate(srcSuff, targSuff);

	return gn;
}

/*
 * Handle the finish of a transformation definition, removing the
 * transformation from the graph if it has neither commands nor sources.
 *
 * If the node has no commands or children, the children and parents lists
 * of the affected suffixes are altered.
 *
 * Input:
 *	gn		Node for transformation
 */
void
Suff_EndTransform(GNode *gn)
{
	Suffix *srcSuff, *targSuff;
	SuffixList *srcSuffParents;

	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&gn->cohorts))
		gn = gn->cohorts.last->datum;

	if (!(gn->type & OP_TRANSFORM))
		return;

	if (!Lst_IsEmpty(&gn->commands) || !Lst_IsEmpty(&gn->children)) {
		DEBUG1(SUFF, "transformation %s complete\n", gn->name);
		return;
	}

	/*
	 * SuffParseTransform() may fail for special rules which are not
	 * actual transformation rules. (e.g. .DEFAULT)
	 */
	if (!ParseTransform(gn->name, &srcSuff, &targSuff))
		return;

	DEBUG2(SUFF, "deleting incomplete transformation from `%s' to `%s'\n",
	    srcSuff->name, targSuff->name);

	/*
	 * Remember the parents since srcSuff could be deleted in
	 * SuffixList_Remove.
	 */
	srcSuffParents = &srcSuff->parents;
	SuffixList_Remove(&targSuff->children, srcSuff);
	SuffixList_Remove(srcSuffParents, targSuff);
}

/*
 * Called from Suff_AddSuffix to search through the list of
 * existing transformation rules and rebuild the transformation graph when
 * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
 * transformation involving this suffix and another, existing suffix, the
 * proper relationship is established between the two.
 *
 * The appropriate links will be made between this suffix and others if
 * transformation rules exist for it.
 *
 * Input:
 *	transform	Transformation to test
 *	suff		Suffix to rebuild
 */
static void
RebuildGraph(GNode *transform, Suffix *suff)
{
	const char *name = transform->name;
	size_t nameLen = strlen(name);
	const char *toName;

	/* See if it is a transformation from this suffix to another suffix. */
	toName = StrTrimPrefix(suff->name, name);
	if (toName != NULL) {
		Suffix *to = FindSuffixByName(toName);
		if (to != NULL) {
			Relate(suff, to);
			return;
		}
	}

	/* See if it is a transformation from another suffix to this suffix. */
	toName = Suffix_TrimSuffix(suff, nameLen, name + nameLen);
	if (toName != NULL) {
		Suffix *from = FindSuffixByNameLen(name,
		    (size_t)(toName - name));
		if (from != NULL)
			Relate(from, suff);
	}
}

/*
 * During Suff_AddSuffix, search through the list of existing targets and find
 * if any of the existing targets can be turned into a transformation rule.
 *
 * If such a target is found and the target is the current main target, the
 * main target is set to NULL and the next target examined (if that exists)
 * becomes the main target.
 *
 * Results:
 *	TRUE iff a new main target has been selected.
 */
static Boolean
UpdateTarget(GNode *target, GNode **inout_main, Suffix *suff,
	     Boolean *inout_removedMain)
{
	Suffix *srcSuff, *targSuff;
	char *ptr;

	if (*inout_main == NULL && *inout_removedMain &&
	    !(target->type & OP_NOTARGET)) {
		DEBUG1(MAKE, "Setting main node to \"%s\"\n", target->name);
		*inout_main = target;
		Targ_SetMain(target);
		/*
		 * XXX: Why could it be a good idea to return TRUE here?
		 * The main task of this function is to turn ordinary nodes
		 * into transformations, no matter whether or not a new .MAIN
		 * node has been found.
		 */
		/*
		 * XXX: Even when changing this to FALSE, none of the existing
		 * unit tests fails.
		 */
		return TRUE;
	}

	if (target->type == OP_TRANSFORM)
		return FALSE;

	/*
	 * XXX: What about a transformation ".cpp.c"?  If ".c" is added as
	 * a new suffix, it seems wrong that this transformation would be
	 * skipped just because ".c" happens to be a prefix of ".cpp".
	 */
	ptr = strstr(target->name, suff->name);
	if (ptr == NULL)
		return FALSE;

	/*
	 * XXX: In suff-rebuild.mk, in the line '.SUFFIXES: .c .b .a', this
	 * condition prevents the rule '.b.c' from being added again during
	 * Suff_AddSuffix(".b").
	 *
	 * XXX: Removing this paragraph makes suff-add-later.mk use massive
	 * amounts of memory.
	 */
	if (ptr == target->name)
		return FALSE;

	if (ParseTransform(target->name, &srcSuff, &targSuff)) {
		if (*inout_main == target) {
			DEBUG1(MAKE,
			    "Setting main node from \"%s\" back to null\n",
			    target->name);
			*inout_removedMain = TRUE;
			*inout_main = NULL;
			Targ_SetMain(NULL);
		}
		Lst_Done(&target->children);
		Lst_Init(&target->children);
		target->type = OP_TRANSFORM;

		/*
		 * Link the two together in the proper relationship and order.
		 */
		DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
		    srcSuff->name, targSuff->name);
		Relate(srcSuff, targSuff);
	}
	return FALSE;
}

/*
 * Look at all existing targets to see if adding this suffix will make one
 * of the current targets mutate into a suffix rule.
 *
 * This is ugly, but other makes treat all targets that start with a '.' as
 * suffix rules.
 */
static void
UpdateTargets(GNode **inout_main, Suffix *suff)
{
	Boolean removedMain = FALSE;
	GNodeListNode *ln;

	for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
		GNode *gn = ln->datum;
		if (UpdateTarget(gn, inout_main, suff, &removedMain))
			break;
	}
}

/*
 * Add the suffix to the end of the list of known suffixes.
 * Should we restructure the suffix graph? Make doesn't.
 *
 * A GNode is created for the suffix (XXX: this sounds completely wrong) and
 * a Suffix structure is created and added to the suffixes list unless the
 * suffix was already known.
 * The mainNode passed can be modified if a target mutated into a
 * transform and that target happened to be the main target.
 *
 * Input:
 *	name		the name of the suffix to add
 */
void
Suff_AddSuffix(const char *name, GNode **inout_main)
{
	GNodeListNode *ln;

	Suffix *suff = FindSuffixByName(name);
	if (suff != NULL)
		return;

	suff = Suffix_New(name);
	Lst_Append(&sufflist, suff);
	DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name);

	UpdateTargets(inout_main, suff);

	/*
	 * Look for any existing transformations from or to this suffix.
	 * XXX: Only do this after a Suff_ClearSuffixes?
	 */
	for (ln = transforms.first; ln != NULL; ln = ln->next)
		RebuildGraph(ln->datum, suff);
}

/* Return the search path for the given suffix, or NULL. */
SearchPath *
Suff_GetPath(const char *sname)
{
	Suffix *suff = FindSuffixByName(sname);
	return suff != NULL ? suff->searchPath : NULL;
}

/*
 * Extend the search paths for all suffixes to include the default search
 * path (dirSearchPath).
 *
 * The default search path can be defined using the special target '.PATH'.
 * The search path of each suffix can be defined using the special target
 * '.PATH<suffix>'.
 *
 * If paths were specified for the ".h" suffix, the directories are stuffed
 * into a global variable called ".INCLUDES" with each directory preceded by
 * '-I'. The same is done for the ".a" suffix, except the variable is called
 * ".LIBS" and the flag is '-L'.
 */
void
Suff_DoPaths(void)
{
	SuffixListNode *ln;
	char *flags;
	SearchPath *includesPath = SearchPath_New();
	SearchPath *libsPath = SearchPath_New();

	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
		Suffix *suff = ln->datum;
		if (!Lst_IsEmpty(suff->searchPath)) {
#ifdef INCLUDES
			if (suff->flags & SUFF_INCLUDE)
				SearchPath_AddAll(includesPath,
				    suff->searchPath);
#endif
#ifdef LIBRARIES
			if (suff->flags & SUFF_LIBRARY)
				SearchPath_AddAll(libsPath, suff->searchPath);
#endif
			SearchPath_AddAll(suff->searchPath, &dirSearchPath);
		} else {
			SearchPath_Free(suff->searchPath);
			suff->searchPath = Dir_CopyDirSearchPath();
		}
	}

	flags = SearchPath_ToFlags("-I", includesPath);
	Var_Set(".INCLUDES", flags, VAR_GLOBAL);
	free(flags);

	flags = SearchPath_ToFlags("-L", libsPath);
	Var_Set(".LIBS", flags, VAR_GLOBAL);
	free(flags);

	SearchPath_Free(includesPath);
	SearchPath_Free(libsPath);
}

/*
 * Add the given suffix as a type of file which gets included.
 * Called when a '.INCLUDES: .h' line is parsed.
 * To have an effect, the suffix must already exist.
 * This affects the magic variable '.INCLUDES'.
 */
void
Suff_AddInclude(const char *suffName)
{
	Suffix *suff = FindSuffixByName(suffName);
	if (suff != NULL)
		suff->flags |= SUFF_INCLUDE;
}

/*
 * Add the given suffix as a type of file which is a library.
 * Called when a '.LIBS: .a' line is parsed.
 * To have an effect, the suffix must already exist.
 * This affects the magic variable '.LIBS'.
 */
void
Suff_AddLib(const char *suffName)
{
	Suffix *suff = FindSuffixByName(suffName);
	if (suff != NULL)
		suff->flags |= SUFF_LIBRARY;
}

/********** Implicit Source Search Functions *********/

static void
CandidateSearcher_Init(CandidateSearcher *cs)
{
	Lst_Init(&cs->list);
}

static void
CandidateSearcher_Done(CandidateSearcher *cs)
{
	Lst_Done(&cs->list);
}

static void
CandidateSearcher_Add(CandidateSearcher *cs, Candidate *cand)
{
	/* TODO: filter duplicates */
	Lst_Append(&cs->list, cand);
}

static void
CandidateSearcher_AddIfNew(CandidateSearcher *cs, Candidate *cand)
{
	/* TODO: filter duplicates */
	if (Lst_FindDatum(&cs->list, cand) == NULL)
		Lst_Append(&cs->list, cand);
}

static void
CandidateSearcher_MoveAll(CandidateSearcher *cs, CandidateList *list)
{
	/* TODO: filter duplicates */
	Lst_MoveAll(&cs->list, list);
}


#ifdef DEBUG_SRC
static void
CandidateList_PrintAddrs(CandidateList *list)
{
	CandidateListNode *ln;

	for (ln = list->first; ln != NULL; ln = ln->next) {
		Candidate *cand = ln->datum;
		debug_printf(" %p:%s", cand, cand->file);
	}
	debug_printf("\n");
}
#endif

static Candidate *
Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent,
	      GNode *gn)
{
	Candidate *cand = bmake_malloc(sizeof *cand);

	cand->file = name;
	cand->prefix = prefix;
	cand->suff = Suffix_Ref(suff);
	cand->parent = parent;
	cand->node = gn;
	cand->numChildren = 0;
#ifdef DEBUG_SRC
	Lst_Init(&cand->childrenList);
#endif

	return cand;
}

/* Add a new candidate to the list. */
/*ARGSUSED*/
static void
CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ,
		  Suffix *suff, const char *debug_tag)
{
	Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ,
	    NULL);
	targ->numChildren++;
	Lst_Append(list, cand);

#ifdef DEBUG_SRC
	Lst_Append(&targ->childrenList, cand);
	debug_printf("%s add suff %p:%s candidate %p:%s to list %p:",
	    debug_tag, targ, targ->file, cand, cand->file, list);
	CandidateList_PrintAddrs(list);
#endif
}

/*
 * Add all candidates to the list that can be formed by applying a suffix to
 * the candidate.
 */
static void
CandidateList_AddCandidatesFor(CandidateList *list, Candidate *cand)
{
	SuffixListNode *ln;
	for (ln = cand->suff->children.first; ln != NULL; ln = ln->next) {
		Suffix *suff = ln->datum;

		if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
			/*
			 * If the suffix has been marked as the NULL suffix,
			 * also create a candidate for a file with no suffix
			 * attached.
			 */
			CandidateList_Add(list, bmake_strdup(cand->prefix),
			    cand, suff, "1");
		}

		CandidateList_Add(list, str_concat2(cand->prefix, suff->name),
		    cand, suff, "2");
	}
}

/*
 * Free the first candidate in the list that is not referenced anymore.
 * Return whether a candidate was removed.
 */
static Boolean
RemoveCandidate(CandidateList *srcs)
{
	CandidateListNode *ln;

#ifdef DEBUG_SRC
	debug_printf("cleaning list %p:", srcs);
	CandidateList_PrintAddrs(srcs);
#endif

	for (ln = srcs->first; ln != NULL; ln = ln->next) {
		Candidate *src = ln->datum;

		if (src->numChildren == 0) {
			if (src->parent == NULL)
				free(src->prefix);
			else {
#ifdef DEBUG_SRC
				/* XXX: Lst_RemoveDatum */
				CandidateListNode *ln2;
				ln2 = Lst_FindDatum(&src->parent->childrenList,
				    src);
				if (ln2 != NULL)
					Lst_Remove(&src->parent->childrenList,
					    ln2);
#endif
				src->parent->numChildren--;
			}
#ifdef DEBUG_SRC
			debug_printf("free: list %p src %p:%s children %d\n",
			    srcs, src, src->file, src->numChildren);
			Lst_Done(&src->childrenList);
#endif
			Lst_Remove(srcs, ln);
			free(src->file);
			free(src);
			return TRUE;
		}
#ifdef DEBUG_SRC
		else {
			debug_printf("keep: list %p src %p:%s children %d:",
			    srcs, src, src->file, src->numChildren);
			CandidateList_PrintAddrs(&src->childrenList);
		}
#endif
	}

	return FALSE;
}

/* Find the first existing file/target in srcs. */
static Candidate *
FindThem(CandidateList *srcs, CandidateSearcher *cs)
{
	HashSet seen;

	HashSet_Init(&seen);

	while (!Lst_IsEmpty(srcs)) {
		Candidate *src = Lst_Dequeue(srcs);

#ifdef DEBUG_SRC
		debug_printf("remove from list %p src %p:%s\n",
		    srcs, src, src->file);
#endif
		DEBUG1(SUFF, "\ttrying %s...", src->file);

		/*
		 * A file is considered to exist if either a node exists in the
		 * graph for it or the file actually exists.
		 */
		if (Targ_FindNode(src->file) != NULL) {
		found:
			HashSet_Done(&seen);
			DEBUG0(SUFF, "got it\n");
			return src;
		}

		{
			char *file = Dir_FindFile(src->file,
			    src->suff->searchPath);
			if (file != NULL) {
				free(file);
				goto found;
			}
		}

		DEBUG0(SUFF, "not there\n");

		if (HashSet_Add(&seen, src->file))
			CandidateList_AddCandidatesFor(srcs, src);
		else {
			DEBUG1(SUFF, "FindThem: skipping duplicate \"%s\"\n",
			    src->file);
		}

		CandidateSearcher_Add(cs, src);
	}

	HashSet_Done(&seen);
	return NULL;
}

/*
 * See if any of the children of the candidate's GNode is one from which the
 * target can be transformed. If there is one, a candidate is put together
 * for it and returned.
 */
static Candidate *
FindCmds(Candidate *targ, CandidateSearcher *cs)
{
	GNodeListNode *gln;
	GNode *tgn;		/* Target GNode */
	GNode *sgn;		/* Source GNode */
	size_t prefLen;		/* The length of the defined prefix */
	Suffix *suff;		/* Suffix on matching beastie */
	Candidate *ret;		/* Return value */

	tgn = targ->node;
	prefLen = strlen(targ->prefix);

	for (gln = tgn->children.first; gln != NULL; gln = gln->next) {
		const char *cp;

		sgn = gln->datum;

		if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(&tgn->commands)) {
			/*
			 * We haven't looked to see if .OPTIONAL files exist
			 * yet, so don't use one as the implicit source.
			 * This allows us to use .OPTIONAL in .depend files so
			 * make won't complain "don't know how to make xxx.h"
			 * when a dependent file has been moved/deleted.
			 */
			continue;
		}

		cp = str_basename(sgn->name);
		if (strncmp(cp, targ->prefix, prefLen) != 0)
			continue;
		/* The node matches the prefix, see if it has a known suffix. */
		suff = FindSuffixByName(cp + prefLen);
		if (suff == NULL)
			continue;

		/*
		 * It even has a known suffix, see if there's a transformation
		 * defined between the node's suffix and the target's suffix.
		 *
		 * XXX: Handle multi-stage transformations here, too.
		 */

		if (Lst_FindDatum(&suff->parents, targ->suff) != NULL)
			break;
	}

	if (gln == NULL)
		return NULL;

	ret = Candidate_New(bmake_strdup(sgn->name), targ->prefix, suff, targ,
	    sgn);
	targ->numChildren++;
#ifdef DEBUG_SRC
	debug_printf("3 add targ %p:%s ret %p:%s\n",
	    targ, targ->file, ret, ret->file);
	Lst_Append(&targ->childrenList, ret);
#endif
	CandidateSearcher_Add(cs, ret);
	DEBUG1(SUFF, "\tusing existing source %s\n", sgn->name);
	return ret;
}

static void
ExpandWildcards(GNodeListNode *cln, GNode *pgn)
{
	GNode *cgn = cln->datum;
	StringList expansions;

	if (!Dir_HasWildcards(cgn->name))
		return;

	/*
	 * Expand the word along the chosen path
	 */
	Lst_Init(&expansions);
	Dir_Expand(cgn->name, Suff_FindPath(cgn), &expansions);

	while (!Lst_IsEmpty(&expansions)) {
		GNode *gn;
		/*
		 * Fetch next expansion off the list and find its GNode
		 */
		char *cp = Lst_Dequeue(&expansions);

		DEBUG1(SUFF, "%s...", cp);
		gn = Targ_GetNode(cp);

		/* Add gn to the parents child list before the original child */
		Lst_InsertBefore(&pgn->children, cln, gn);
		Lst_Append(&gn->parents, pgn);
		pgn->unmade++;
	}

	Lst_Done(&expansions);

	DEBUG0(SUFF, "\n");

	/*
	 * Now the source is expanded, remove it from the list of children to
	 * keep it from being processed.
	 */
	pgn->unmade--;
	Lst_Remove(&pgn->children, cln);
	Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
}

/*
 * Break the result into a vector of strings whose nodes we can find, then
 * add those nodes to the members list.
 *
 * Unfortunately, we can't use Str_Words because it doesn't understand about
 * variable specifications with spaces in them.
 */
static void
ExpandChildrenRegular(char *cp, GNode *pgn, GNodeList *members)
{
	char *start;

	pp_skip_hspace(&cp);
	start = cp;
	while (*cp != '\0') {
		if (*cp == ' ' || *cp == '\t') {
			GNode *gn;
			/*
			 * White-space -- terminate element, find the node,
			 * add it, skip any further spaces.
			 */
			*cp++ = '\0';
			gn = Targ_GetNode(start);
			Lst_Append(members, gn);
			pp_skip_hspace(&cp);
			/* Continue at the next non-space. */
			start = cp;
		} else if (*cp == '$') {
			/* Skip over the variable expression. */
			const char *nested_p = cp;
			FStr junk;

			(void)Var_Parse(&nested_p, pgn, VARE_NONE, &junk);
			/* TODO: handle errors */
			if (junk.str == var_Error) {
				Parse_Error(PARSE_FATAL,
				    "Malformed variable expression at \"%s\"",
				    cp);
				cp++;
			} else {
				cp += nested_p - cp;
			}

			FStr_Done(&junk);
		} else if (cp[0] == '\\' && cp[1] != '\0') {
			/* Escaped something -- skip over it. */
			/*
			 * XXX: In other places, escaping at this syntactical
			 * position is done by a '$', not a '\'.  The '\' is
			 * only used in variable modifiers.
			 */
			cp += 2;
		} else {
			cp++;
		}
	}

	if (cp != start) {
		/*
		 * Stuff left over -- add it to the list too
		 */
		GNode *gn = Targ_GetNode(start);
		Lst_Append(members, gn);
	}
}

/*
 * Expand the names of any children of a given node that contain variable
 * expressions or file wildcards into actual targets.
 *
 * The expanded node is removed from the parent's list of children, and the
 * parent's unmade counter is decremented, but other nodes may be added.
 *
 * Input:
 *	cln		Child to examine
 *	pgn		Parent node being processed
 */
static void
ExpandChildren(GNodeListNode *cln, GNode *pgn)
{
	GNode *cgn = cln->datum;
	char *cp;		/* Expanded value */

	if (!Lst_IsEmpty(&cgn->order_pred) || !Lst_IsEmpty(&cgn->order_succ))
		/* It is all too hard to process the result of .ORDER */
		return;

	if (cgn->type & OP_WAIT)
		/* Ignore these (& OP_PHONY ?) */
		return;

	/*
	 * First do variable expansion -- this takes precedence over wildcard
	 * expansion. If the result contains wildcards, they'll be gotten to
	 * later since the resulting words are tacked on to the end of the
	 * children list.
	 */
	if (strchr(cgn->name, '$') == NULL) {
		ExpandWildcards(cln, pgn);
		return;
	}

	DEBUG1(SUFF, "Expanding \"%s\"...", cgn->name);
	(void)Var_Subst(cgn->name, pgn, VARE_WANTRES | VARE_UNDEFERR, &cp);
	/* TODO: handle errors */

	{
		GNodeList members = LST_INIT;

		if (cgn->type & OP_ARCHV) {
			/*
			 * Node was an archive(member) target, so we want to
			 * call on the Arch module to find the nodes for us,
			 * expanding variables in the parent's context.
			 */
			char *p = cp;
			(void)Arch_ParseArchive(&p, &members, pgn);
		} else {
			ExpandChildrenRegular(cp, pgn, &members);
		}

		/*
		 * Add all elements of the members list to the parent node.
		 */
		while (!Lst_IsEmpty(&members)) {
			GNode *gn = Lst_Dequeue(&members);

			DEBUG1(SUFF, "%s...", gn->name);
			/*
			 * Add gn to the parents child list before the
			 * original child.
			 */
			Lst_InsertBefore(&pgn->children, cln, gn);
			Lst_Append(&gn->parents, pgn);
			pgn->unmade++;
			/* Expand wildcards on new node */
			ExpandWildcards(cln->prev, pgn);
		}
		Lst_Done(&members);

		free(cp);
	}

	DEBUG0(SUFF, "\n");

	/*
	 * Now the source is expanded, remove it from the list of children to
	 * keep it from being processed.
	 */
	pgn->unmade--;
	Lst_Remove(&pgn->children, cln);
	Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
}

static void
ExpandAllChildren(GNode *gn)
{
	GNodeListNode *ln, *nln;

	for (ln = gn->children.first; ln != NULL; ln = nln) {
		nln = ln->next;
		ExpandChildren(ln, gn);
	}
}

/*
 * Find a path along which to expand the node.
 *
 * If the node has a known suffix, use that path.
 * If it has no known suffix, use the default system search path.
 *
 * Input:
 *	gn		Node being examined
 *
 * Results:
 *	The appropriate path to search for the GNode.
 */
SearchPath *
Suff_FindPath(GNode *gn)
{
	Suffix *suff = gn->suffix;

	if (suff == NULL) {
		char *name = gn->name;
		size_t nameLen = strlen(gn->name);
		SuffixListNode *ln;
		for (ln = sufflist.first; ln != NULL; ln = ln->next)
			if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen))
				break;

		DEBUG1(SUFF, "Wildcard expanding \"%s\"...", gn->name);
		if (ln != NULL)
			suff = ln->datum;
		/*
		 * XXX: Here we can save the suffix so we don't have to do
		 * this again.
		 */
	}

	if (suff != NULL) {
		DEBUG1(SUFF, "suffix is \"%s\"...\n", suff->name);
		return suff->searchPath;
	} else {
		DEBUG0(SUFF, "\n");
		return &dirSearchPath;	/* Use default search path */
	}
}

/*
 * Apply a transformation rule, given the source and target nodes and
 * suffixes.
 *
 * The source and target are linked and the commands from the transformation
 * are added to the target node's commands list. The target also inherits all
 * the sources for the transformation rule.
 *
 * Results:
 *	TRUE if successful, FALSE if not.
 */
static Boolean
ApplyTransform(GNode *tgn, GNode *sgn, Suffix *tsuff, Suffix *ssuff)
{
	GNodeListNode *ln;
	char *tname;		/* Name of transformation rule */
	GNode *gn;		/* Node for the transformation rule */

	/* Form the proper links between the target and source. */
	Lst_Append(&tgn->children, sgn);
	Lst_Append(&sgn->parents, tgn);
	tgn->unmade++;

	/* Locate the transformation rule itself. */
	tname = str_concat2(ssuff->name, tsuff->name);
	gn = FindTransformByName(tname);
	free(tname);

	/* This can happen when linking an OP_MEMBER and OP_ARCHV node. */
	if (gn == NULL)
		return FALSE;

	DEBUG3(SUFF, "\tapplying %s -> %s to \"%s\"\n",
	    ssuff->name, tsuff->name, tgn->name);

	/* Record last child; Make_HandleUse may add child nodes. */
	ln = tgn->children.last;

	/* Apply the rule. */
	Make_HandleUse(gn, tgn);

	/* Deal with wildcards and variables in any acquired sources. */
	ln = ln != NULL ? ln->next : NULL;
	while (ln != NULL) {
		GNodeListNode *nln = ln->next;
		ExpandChildren(ln, tgn);
		ln = nln;
	}

	/*
	 * Keep track of another parent to which this node is transformed so
	 * the .IMPSRC variable can be set correctly for the parent.
	 */
	Lst_Append(&sgn->implicitParents, tgn);

	return TRUE;
}

/*
 * Member has a known suffix, so look for a transformation rule from
 * it to a possible suffix of the archive.
 *
 * Rather than searching through the entire list, we just look at
 * suffixes to which the member's suffix may be transformed.
 */
static void
ExpandMember(GNode *gn, const char *eoarch, GNode *mem, Suffix *memSuff)
{
	GNodeListNode *ln;
	size_t nameLen = (size_t)(eoarch - gn->name);

	/* Use first matching suffix... */
	for (ln = memSuff->parents.first; ln != NULL; ln = ln->next)
		if (Suffix_IsSuffix(ln->datum, nameLen, eoarch))
			break;

	if (ln != NULL) {
		/* Got one -- apply it */
		Suffix *suff = ln->datum;
		if (!ApplyTransform(gn, mem, suff, memSuff)) {
			DEBUG2(SUFF, "\tNo transformation from %s -> %s\n",
			    memSuff->name, suff->name);
		}
	}
}

static void FindDeps(GNode *, CandidateSearcher *);

/*
 * Locate dependencies for an OP_ARCHV node.
 *
 * Input:
 *	gn		Node for which to locate dependencies
 *
 * Side Effects:
 *	Same as Suff_FindDeps
 */
static void
FindDepsArchive(GNode *gn, CandidateSearcher *cs)
{
	char *eoarch;		/* End of archive portion */
	char *eoname;		/* End of member portion */
	GNode *mem;		/* Node for member */
	Suffix *memSuff;
	const char *name;	/* Start of member's name */

	/*
	 * The node is an archive(member) pair. so we must find a
	 * suffix for both of them.
	 */
	eoarch = strchr(gn->name, '(');
	eoname = strchr(eoarch, ')');

	/*
	 * Caller guarantees the format `libname(member)', via
	 * Arch_ParseArchive.
	 */
	assert(eoarch != NULL);
	assert(eoname != NULL);

	*eoname = '\0';		/* Nuke parentheses during suffix search */
	*eoarch = '\0';		/* So a suffix can be found */

	name = eoarch + 1;

	/*
	 * To simplify things, call Suff_FindDeps recursively on the member
	 * now, so we can simply compare the member's .PREFIX and .TARGET
	 * variables to locate its suffix. This allows us to figure out the
	 * suffix to use for the archive without having to do a quadratic
	 * search over the suffix list, backtracking for each one.
	 */
	mem = Targ_GetNode(name);
	FindDeps(mem, cs);

	/* Create the link between the two nodes right off. */
	Lst_Append(&gn->children, mem);
	Lst_Append(&mem->parents, gn);
	gn->unmade++;

	/* Copy in the variables from the member node to this one. */
	Var_Set(PREFIX, GNode_VarPrefix(mem), gn);
	Var_Set(TARGET, GNode_VarTarget(mem), gn);

	memSuff = mem->suffix;
	if (memSuff == NULL) {	/* Didn't know what it was. */
		DEBUG0(SUFF, "using null suffix\n");
		memSuff = nullSuff;
	}


	/* Set the other two local variables required for this target. */
	Var_Set(MEMBER, name, gn);
	Var_Set(ARCHIVE, gn->name, gn);
	/* Set $@ for compatibility with other makes. */
	Var_Set(TARGET, gn->name, gn);

	/*
	 * Now we've got the important local variables set, expand any sources
	 * that still contain variables or wildcards in their names.
	 */
	ExpandAllChildren(gn);

	if (memSuff != NULL)
		ExpandMember(gn, eoarch, mem, memSuff);

	/*
	 * Replace the opening and closing parens now we've no need of the
	 * separate pieces.
	 */
	*eoarch = '(';
	*eoname = ')';

	/*
	 * Pretend gn appeared to the left of a dependency operator so the
	 * user needn't provide a transformation from the member to the
	 * archive.
	 */
	if (!GNode_IsTarget(gn))
		gn->type |= OP_DEPENDS;

	/*
	 * Flag the member as such so we remember to look in the archive for
	 * its modification time. The OP_JOIN | OP_MADE is needed because
	 * this target should never get made.
	 */
	mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
}

/*
 * If the node is a library, it is the arch module's job to find it
 * and set the TARGET variable accordingly. We merely provide the
 * search path, assuming all libraries end in ".a" (if the suffix
 * hasn't been defined, there's nothing we can do for it, so we just
 * set the TARGET variable to the node's name in order to give it a
 * value).
 */
static void
FindDepsLib(GNode *gn)
{
	Suffix *suff = FindSuffixByName(LIBSUFF);
	if (suff != NULL) {
		Suffix_Reassign(&gn->suffix, suff);
		Arch_FindLib(gn, suff->searchPath);
	} else {
		Suffix_Unassign(&gn->suffix);
		Var_Set(TARGET, gn->name, gn);
	}

	/*
	 * Because a library (-lfoo) target doesn't follow the standard
	 * filesystem conventions, we don't set the regular variables for
	 * the thing. .PREFIX is simply made empty.
	 */
	Var_Set(PREFIX, "", gn);
}

static void
FindDepsRegularKnown(const char *name, size_t nameLen, GNode *gn,
		     CandidateList *srcs, CandidateList *targs)
{
	SuffixListNode *ln;
	Candidate *targ;
	char *pref;

	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
		Suffix *suff = ln->datum;
		if (!Suffix_IsSuffix(suff, nameLen, name + nameLen))
			continue;

		pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen));
		targ = Candidate_New(bmake_strdup(gn->name), pref, suff, NULL,
		    gn);

		CandidateList_AddCandidatesFor(srcs, targ);

		/* Record the target so we can nuke it. */
		Lst_Append(targs, targ);
	}
}

static void
FindDepsRegularUnknown(GNode *gn, const char *sopref,
		       CandidateList *srcs, CandidateList *targs)
{
	Candidate *targ;

	if (!Lst_IsEmpty(targs) || nullSuff == NULL)
		return;

	DEBUG1(SUFF, "\tNo known suffix on %s. Using .NULL suffix\n", gn->name);

	targ = Candidate_New(bmake_strdup(gn->name), bmake_strdup(sopref),
	    nullSuff, NULL, gn);

	/*
	 * Only use the default suffix rules if we don't have commands
	 * defined for this gnode; traditional make programs used to not
	 * define suffix rules if the gnode had children but we don't do
	 * this anymore.
	 */
	if (Lst_IsEmpty(&gn->commands))
		CandidateList_AddCandidatesFor(srcs, targ);
	else {
		DEBUG0(SUFF, "not ");
	}

	DEBUG0(SUFF, "adding suffix rules\n");

	Lst_Append(targs, targ);
}

/*
 * Deal with finding the thing on the default search path. We always do
 * that, not only if the node is only a source (not on the lhs of a
 * dependency operator or [XXX] it has neither children or commands) as
 * the old pmake did.
 */
static void
FindDepsRegularPath(GNode *gn, Candidate *targ)
{
	if (gn->type & (OP_PHONY | OP_NOPATH))
		return;

	free(gn->path);
	gn->path = Dir_FindFile(gn->name,
	    (targ == NULL ? &dirSearchPath :
		targ->suff->searchPath));
	if (gn->path == NULL)
		return;

	Var_Set(TARGET, gn->path, gn);

	if (targ != NULL) {
		/*
		 * Suffix known for the thing -- trim the suffix off
		 * the path to form the proper .PREFIX variable.
		 */
		size_t savep = strlen(gn->path) - targ->suff->nameLen;
		char savec;

		Suffix_Reassign(&gn->suffix, targ->suff);

		savec = gn->path[savep];
		gn->path[savep] = '\0';

		Var_Set(PREFIX, str_basename(gn->path), gn);

		gn->path[savep] = savec;
	} else {
		/*
		 * The .PREFIX gets the full path if the target has no
		 * known suffix.
		 */
		Suffix_Unassign(&gn->suffix);
		Var_Set(PREFIX, str_basename(gn->path), gn);
	}
}

/*
 * Locate implicit dependencies for regular targets.
 *
 * Input:
 *	gn		Node for which to find sources
 *
 * Side Effects:
 *	Same as Suff_FindDeps
 */
static void
FindDepsRegular(GNode *gn, CandidateSearcher *cs)
{
	/* List of sources at which to look */
	CandidateList srcs = LST_INIT;
	/*
	 * List of targets to which things can be transformed.
	 * They all have the same file, but different suff and prefix fields.
	 */
	CandidateList targs = LST_INIT;
	Candidate *bottom;	/* Start of found transformation path */
	Candidate *src;
	Candidate *targ;

	const char *name = gn->name;
	size_t nameLen = strlen(name);

#ifdef DEBUG_SRC
	DEBUG1(SUFF, "FindDepsRegular \"%s\"\n", gn->name);
#endif

	/*
	 * We're caught in a catch-22 here. On the one hand, we want to use
	 * any transformation implied by the target's sources, but we can't
	 * examine the sources until we've expanded any variables/wildcards
	 * they may hold, and we can't do that until we've set up the
	 * target's local variables and we can't do that until we know what
	 * the proper suffix for the target is (in case there are two
	 * suffixes one of which is a suffix of the other) and we can't know
	 * that until we've found its implied source, which we may not want
	 * to use if there's an existing source that implies a different
	 * transformation.
	 *
	 * In an attempt to get around this, which may not work all the time,
	 * but should work most of the time, we look for implied sources
	 * first, checking transformations to all possible suffixes of the
	 * target, use what we find to set the target's local variables,
	 * expand the children, then look for any overriding transformations
	 * they imply. Should we find one, we discard the one we found before.
	 */
	bottom = NULL;
	targ = NULL;

	if (!(gn->type & OP_PHONY)) {

		FindDepsRegularKnown(name, nameLen, gn, &srcs, &targs);

		/* Handle target of unknown suffix... */
		FindDepsRegularUnknown(gn, name, &srcs, &targs);

		/*
		 * Using the list of possible sources built up from the target
		 * suffix(es), try and find an existing file/target that
		 * matches.
		 */
		bottom = FindThem(&srcs, cs);

		if (bottom == NULL) {
			/*
			 * No known transformations -- use the first suffix
			 * found for setting the local variables.
			 */
			if (targs.first != NULL)
				targ = targs.first->datum;
			else
				targ = NULL;
		} else {
			/*
			 * Work up the transformation path to find the suffix
			 * of the target to which the transformation was made.
			 */
			for (targ = bottom;
			     targ->parent != NULL; targ = targ->parent)
				continue;
		}
	}

	Var_Set(TARGET, GNode_Path(gn), gn);
	Var_Set(PREFIX, targ != NULL ? targ->prefix : gn->name, gn);

	/*
	 * Now we've got the important local variables set, expand any sources
	 * that still contain variables or wildcards in their names.
	 */
	{
		GNodeListNode *ln, *nln;
		for (ln = gn->children.first; ln != NULL; ln = nln) {
			nln = ln->next;
			ExpandChildren(ln, gn);
		}
	}

	if (targ == NULL) {
		DEBUG1(SUFF, "\tNo valid suffix on %s\n", gn->name);

	sfnd_abort:
		FindDepsRegularPath(gn, targ);
		goto sfnd_return;
	}

	/*
	 * If the suffix indicates that the target is a library, mark that in
	 * the node's type field.
	 */
	if (targ->suff->flags & SUFF_LIBRARY)
		gn->type |= OP_LIB;

	/*
	 * Check for overriding transformation rule implied by sources
	 */
	if (!Lst_IsEmpty(&gn->children)) {
		src = FindCmds(targ, cs);

		if (src != NULL) {
			/*
			 * Free up all the candidates in the transformation
			 * path, up to but not including the parent node.
			 */
			while (bottom != NULL && bottom->parent != NULL) {
				CandidateSearcher_AddIfNew(cs, bottom);
				bottom = bottom->parent;
			}
			bottom = src;
		}
	}

	if (bottom == NULL) {
		/* No idea from where it can come -- return now. */
		goto sfnd_abort;
	}

	/*
	 * We now have a list of candidates headed by 'bottom' and linked via
	 * their 'parent' pointers. What we do next is create links between
	 * source and target nodes (which may or may not have been created)
	 * and set the necessary local variables in each target.
	 *
	 * The commands for each target are set from the commands of the
	 * transformation rule used to get from the src suffix to the targ
	 * suffix. Note that this causes the commands list of the original
	 * node, gn, to be replaced with the commands of the final
	 * transformation rule.
	 */
	if (bottom->node == NULL)
		bottom->node = Targ_GetNode(bottom->file);

	for (src = bottom; src->parent != NULL; src = src->parent) {
		targ = src->parent;

		Suffix_Reassign(&src->node->suffix, src->suff);

		if (targ->node == NULL)
			targ->node = Targ_GetNode(targ->file);

		ApplyTransform(targ->node, src->node,
		    targ->suff, src->suff);

		if (targ->node != gn) {
			/*
			 * Finish off the dependency-search process for any
			 * nodes between bottom and gn (no point in questing
			 * around the filesystem for their implicit source
			 * when it's already known). Note that the node
			 * can't have any sources that need expanding, since
			 * SuffFindThem will stop on an existing node, so all
			 * we need to do is set the standard variables.
			 */
			targ->node->type |= OP_DEPS_FOUND;
			Var_Set(PREFIX, targ->prefix, targ->node);
			Var_Set(TARGET, targ->node->name, targ->node);
		}
	}

	Suffix_Reassign(&gn->suffix, src->suff);

	/*
	 * Nuke the transformation path and the candidates left over in the
	 * two lists.
	 */
sfnd_return:
	if (bottom != NULL)
		CandidateSearcher_AddIfNew(cs, bottom);

	while (RemoveCandidate(&srcs) || RemoveCandidate(&targs))
		continue;

	CandidateSearcher_MoveAll(cs, &srcs);
	CandidateSearcher_MoveAll(cs, &targs);
}

static void
CandidateSearcher_CleanUp(CandidateSearcher *cs)
{
	while (RemoveCandidate(&cs->list))
		continue;
	assert(Lst_IsEmpty(&cs->list));
}


/*
 * Find implicit sources for the target.
 *
 * Nodes are added to the graph as children of the passed-in node. The nodes
 * are marked to have their IMPSRC variable filled in. The PREFIX variable
 * is set for the given node and all its implied children.
 *
 * The path found by this target is the shortest path in the transformation
 * graph, which may pass through nonexistent targets, to an existing target.
 * The search continues on all paths from the root suffix until a file is
 * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
 * .l,v file exists but the .c and .l files don't, the search will branch out
 * in all directions from .o and again from all the nodes on the next level
 * until the .l,v node is encountered.
 */
void
Suff_FindDeps(GNode *gn)
{
	CandidateSearcher cs;

	CandidateSearcher_Init(&cs);

	FindDeps(gn, &cs);

	CandidateSearcher_CleanUp(&cs);
	CandidateSearcher_Done(&cs);
}

static void
FindDeps(GNode *gn, CandidateSearcher *cs)
{
	if (gn->type & OP_DEPS_FOUND)
		return;
	gn->type |= OP_DEPS_FOUND;

	/* Make sure we have these set, may get revised below. */
	Var_Set(TARGET, GNode_Path(gn), gn);
	Var_Set(PREFIX, gn->name, gn);

	DEBUG1(SUFF, "SuffFindDeps \"%s\"\n", gn->name);

	if (gn->type & OP_ARCHV)
		FindDepsArchive(gn, cs);
	else if (gn->type & OP_LIB)
		FindDepsLib(gn);
	else
		FindDepsRegular(gn, cs);
}

/*
 * Define which suffix is the null suffix.
 *
 * Need to handle the changing of the null suffix gracefully so the old
 * transformation rules don't just go away.
 *
 * Input:
 *	name		Name of null suffix
 */
void
Suff_SetNull(const char *name)
{
	Suffix *suff = FindSuffixByName(name);
	if (suff == NULL) {
		Parse_Error(PARSE_WARNING,
		    "Desired null suffix %s not defined.",
		    name);
		return;
	}

	if (nullSuff != NULL)
		nullSuff->flags &= ~(unsigned)SUFF_NULL;
	suff->flags |= SUFF_NULL;
	/* XXX: Here's where the transformation mangling would take place. */
	nullSuff = suff;
}

/* Initialize the suffixes module. */
void
Suff_Init(void)
{
	/*
	 * Create null suffix for single-suffix rules (POSIX). The thing
	 * doesn't actually go on the suffix list or everyone will think
	 * that's its suffix.
	 */
	Suff_ClearSuffixes();
}


/* Clean up the suffixes module. */
void
Suff_End(void)
{
#ifdef CLEANUP
	Lst_DoneCall(&sufflist, SuffFree);
	Lst_DoneCall(&suffClean, SuffFree);
	if (nullSuff != NULL)
		SuffFree(nullSuff);
	Lst_Done(&transforms);
#endif
}


static void
PrintSuffNames(const char *prefix, SuffixList *suffs)
{
	SuffixListNode *ln;

	debug_printf("#\t%s: ", prefix);
	for (ln = suffs->first; ln != NULL; ln = ln->next) {
		Suffix *suff = ln->datum;
		debug_printf("%s ", suff->name);
	}
	debug_printf("\n");
}

static void
Suffix_Print(Suffix *suff)
{
	debug_printf("# \"%s\" (num %d, ref %d)",
	    suff->name, suff->sNum, suff->refCount);
	if (suff->flags != 0) {
		char flags_buf[SuffixFlags_ToStringSize];

		debug_printf(" (%s)",
		    Enum_FlagsToString(flags_buf, sizeof flags_buf,
			suff->flags,
			SuffixFlags_ToStringSpecs));
	}
	debug_printf("\n");

	PrintSuffNames("To", &suff->parents);
	PrintSuffNames("From", &suff->children);

	debug_printf("#\tSearch Path: ");
	SearchPath_Print(suff->searchPath);
	debug_printf("\n");
}

static void
PrintTransformation(GNode *t)
{
	debug_printf("%-16s:", t->name);
	Targ_PrintType(t->type);
	debug_printf("\n");
	Targ_PrintCmds(t);
	debug_printf("\n");
}

void
Suff_PrintAll(void)
{
	debug_printf("#*** Suffixes:\n");
	{
		SuffixListNode *ln;
		for (ln = sufflist.first; ln != NULL; ln = ln->next)
			Suffix_Print(ln->datum);
	}

	debug_printf("#*** Transformations:\n");
	{
		GNodeListNode *ln;
		for (ln = transforms.first; ln != NULL; ln = ln->next)
			PrintTransformation(ln->datum);
	}
}