xref: /freebsd/contrib/bmake/suff.c (revision dba7b0ef928af88caa38728a73657b837aeeac93)
1*dba7b0efSSimon J. Gerraty /*	$NetBSD: suff.c,v 1.345 2021/02/05 05:15:12 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990, 1993
53955d011SMarcel Moolenaar  *	The Regents of the University of California.  All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
83955d011SMarcel Moolenaar  * Adam de Boor.
93955d011SMarcel Moolenaar  *
103955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
113955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
123955d011SMarcel Moolenaar  * are met:
133955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
143955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
153955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
173955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
183955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
193955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
203955d011SMarcel Moolenaar  *    without specific prior written permission.
213955d011SMarcel Moolenaar  *
223955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323955d011SMarcel Moolenaar  * SUCH DAMAGE.
333955d011SMarcel Moolenaar  */
343955d011SMarcel Moolenaar 
353955d011SMarcel Moolenaar /*
363955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
373955d011SMarcel Moolenaar  * All rights reserved.
383955d011SMarcel Moolenaar  *
393955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
403955d011SMarcel Moolenaar  * Adam de Boor.
413955d011SMarcel Moolenaar  *
423955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
433955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
443955d011SMarcel Moolenaar  * are met:
453955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
463955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
473955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
483955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
493955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
503955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
513955d011SMarcel Moolenaar  *    must display the following acknowledgement:
523955d011SMarcel Moolenaar  *	This product includes software developed by the University of
533955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
543955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
553955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
563955d011SMarcel Moolenaar  *    without specific prior written permission.
573955d011SMarcel Moolenaar  *
583955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
593955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
603955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
613955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
623955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
633955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
643955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
653955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
663955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
673955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
683955d011SMarcel Moolenaar  * SUCH DAMAGE.
693955d011SMarcel Moolenaar  */
703955d011SMarcel Moolenaar 
71e2eeea75SSimon J. Gerraty /*
72e2eeea75SSimon J. Gerraty  * Maintain suffix lists and find implicit dependents using suffix
73e2eeea75SSimon J. Gerraty  * transformation rules such as ".c.o".
743955d011SMarcel Moolenaar  *
753955d011SMarcel Moolenaar  * Interface:
76e2eeea75SSimon J. Gerraty  *	Suff_Init	Initialize the module.
773955d011SMarcel Moolenaar  *
78e2eeea75SSimon J. Gerraty  *	Suff_End	Clean up the module.
793955d011SMarcel Moolenaar  *
80e2eeea75SSimon J. Gerraty  *	Suff_DoPaths	Extend the search path of each suffix to include the
81e2eeea75SSimon J. Gerraty  *			default search path.
823955d011SMarcel Moolenaar  *
83956e45f6SSimon J. Gerraty  *	Suff_ClearSuffixes
84e2eeea75SSimon J. Gerraty  *			Clear out all the suffixes and transformations.
853955d011SMarcel Moolenaar  *
86956e45f6SSimon J. Gerraty  *	Suff_IsTransform
87e2eeea75SSimon J. Gerraty  *			See if the passed string is a transformation rule.
883955d011SMarcel Moolenaar  *
893955d011SMarcel Moolenaar  *	Suff_AddSuffix	Add the passed string as another known suffix.
903955d011SMarcel Moolenaar  *
913955d011SMarcel Moolenaar  *	Suff_GetPath	Return the search path for the given suffix.
923955d011SMarcel Moolenaar  *
93956e45f6SSimon J. Gerraty  *	Suff_AddInclude
94956e45f6SSimon J. Gerraty  *			Mark the given suffix as denoting an include file.
953955d011SMarcel Moolenaar  *
963955d011SMarcel Moolenaar  *	Suff_AddLib	Mark the given suffix as denoting a library.
973955d011SMarcel Moolenaar  *
98956e45f6SSimon J. Gerraty  *	Suff_AddTransform
99e2eeea75SSimon J. Gerraty  *			Add another transformation to the suffix graph.
1003955d011SMarcel Moolenaar  *
1013955d011SMarcel Moolenaar  *	Suff_SetNull	Define the suffix to consider the suffix of
1023955d011SMarcel Moolenaar  *			any file that doesn't have a known one.
1033955d011SMarcel Moolenaar  *
1043955d011SMarcel Moolenaar  *	Suff_FindDeps	Find implicit sources for and the location of
1053955d011SMarcel Moolenaar  *			a target based on its suffix. Returns the
1063955d011SMarcel Moolenaar  *			bottom-most node added to the graph or NULL
1073955d011SMarcel Moolenaar  *			if the target had no implicit sources.
1083955d011SMarcel Moolenaar  *
109956e45f6SSimon J. Gerraty  *	Suff_FindPath	Return the appropriate path to search in order to
110956e45f6SSimon J. Gerraty  *			find the node.
1113955d011SMarcel Moolenaar  */
1123955d011SMarcel Moolenaar 
1133955d011SMarcel Moolenaar #include "make.h"
1143955d011SMarcel Moolenaar #include "dir.h"
1153955d011SMarcel Moolenaar 
116956e45f6SSimon J. Gerraty /*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
117*dba7b0efSSimon J. Gerraty MAKE_RCSID("$NetBSD: suff.c,v 1.345 2021/02/05 05:15:12 rillig Exp $");
1182c3632d1SSimon J. Gerraty 
11906b9b3e0SSimon J. Gerraty typedef List SuffixList;
12006b9b3e0SSimon J. Gerraty typedef ListNode SuffixListNode;
1212c3632d1SSimon J. Gerraty 
12206b9b3e0SSimon J. Gerraty typedef List CandidateList;
12306b9b3e0SSimon J. Gerraty typedef ListNode CandidateListNode;
1242c3632d1SSimon J. Gerraty 
12506b9b3e0SSimon J. Gerraty /* The defined suffixes, such as '.c', '.o', '.l'. */
12606b9b3e0SSimon J. Gerraty static SuffixList sufflist = LST_INIT;
1273955d011SMarcel Moolenaar #ifdef CLEANUP
12806b9b3e0SSimon J. Gerraty /* The suffixes to be cleaned up at the end. */
12906b9b3e0SSimon J. Gerraty static SuffixList suffClean = LST_INIT;
1303955d011SMarcel Moolenaar #endif
131e2eeea75SSimon J. Gerraty 
13206b9b3e0SSimon J. Gerraty /*
13306b9b3e0SSimon J. Gerraty  * The transformation rules, such as '.c.o' to transform '.c' into '.o',
13406b9b3e0SSimon J. Gerraty  * or simply '.c' to transform 'file.c' into 'file'.
13506b9b3e0SSimon J. Gerraty  */
13606b9b3e0SSimon J. Gerraty static GNodeList transforms = LST_INIT;
1373955d011SMarcel Moolenaar 
13806b9b3e0SSimon J. Gerraty /*
13906b9b3e0SSimon J. Gerraty  * Counter for assigning suffix numbers.
14006b9b3e0SSimon J. Gerraty  * TODO: What are these suffix numbers used for?
14106b9b3e0SSimon J. Gerraty  */
14206b9b3e0SSimon J. Gerraty static int sNum = 0;
1433955d011SMarcel Moolenaar 
14406b9b3e0SSimon J. Gerraty typedef enum SuffixFlags {
14506b9b3e0SSimon J. Gerraty 	SUFF_NONE	= 0,
1462c3632d1SSimon J. Gerraty 
14706b9b3e0SSimon J. Gerraty 	/*
14806b9b3e0SSimon J. Gerraty 	 * This suffix marks include files.  Their search path ends up in the
14906b9b3e0SSimon J. Gerraty 	 * undocumented special variable '.INCLUDES'.
15006b9b3e0SSimon J. Gerraty 	 */
15106b9b3e0SSimon J. Gerraty 	SUFF_INCLUDE	= 1 << 0,
15206b9b3e0SSimon J. Gerraty 
15306b9b3e0SSimon J. Gerraty 	/*
15406b9b3e0SSimon J. Gerraty 	 * This suffix marks library files.  Their search path ends up in the
15506b9b3e0SSimon J. Gerraty 	 * undocumented special variable '.LIBS'.
15606b9b3e0SSimon J. Gerraty 	 */
15706b9b3e0SSimon J. Gerraty 	SUFF_LIBRARY	= 1 << 1,
15806b9b3e0SSimon J. Gerraty 
15906b9b3e0SSimon J. Gerraty 	/*
16006b9b3e0SSimon J. Gerraty 	 * The empty suffix.
16106b9b3e0SSimon J. Gerraty 	 *
16206b9b3e0SSimon J. Gerraty 	 * XXX: What is the difference between the empty suffix and the null
16306b9b3e0SSimon J. Gerraty 	 * suffix?
16406b9b3e0SSimon J. Gerraty 	 *
16506b9b3e0SSimon J. Gerraty 	 * XXX: Why is SUFF_NULL needed at all? Wouldn't nameLen == 0 mean
16606b9b3e0SSimon J. Gerraty 	 * the same?
16706b9b3e0SSimon J. Gerraty 	 */
16806b9b3e0SSimon J. Gerraty 	SUFF_NULL	= 1 << 2
16906b9b3e0SSimon J. Gerraty 
17006b9b3e0SSimon J. Gerraty } SuffixFlags;
17106b9b3e0SSimon J. Gerraty 
17206b9b3e0SSimon J. Gerraty ENUM_FLAGS_RTTI_3(SuffixFlags,
1732c3632d1SSimon J. Gerraty     SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
1742c3632d1SSimon J. Gerraty 
17506b9b3e0SSimon J. Gerraty typedef List SuffixListList;
176956e45f6SSimon J. Gerraty 
17706b9b3e0SSimon J. Gerraty /*
17806b9b3e0SSimon J. Gerraty  * A suffix such as ".c" or ".o" that is used in suffix transformation rules
17906b9b3e0SSimon J. Gerraty  * such as ".c.o:".
18006b9b3e0SSimon J. Gerraty  */
18106b9b3e0SSimon J. Gerraty typedef struct Suffix {
182e2eeea75SSimon J. Gerraty 	/* The suffix itself, such as ".c" */
183e2eeea75SSimon J. Gerraty 	char *name;
184e2eeea75SSimon J. Gerraty 	/* Length of the name, to avoid strlen calls */
185e2eeea75SSimon J. Gerraty 	size_t nameLen;
186e2eeea75SSimon J. Gerraty 	/* Type of suffix */
18706b9b3e0SSimon J. Gerraty 	SuffixFlags flags;
188e2eeea75SSimon J. Gerraty 	/* The path along which files of this suffix may be found */
189e2eeea75SSimon J. Gerraty 	SearchPath *searchPath;
190e2eeea75SSimon J. Gerraty 	/* The suffix number; TODO: document the purpose of this number */
191e2eeea75SSimon J. Gerraty 	int sNum;
192e2eeea75SSimon J. Gerraty 	/* Reference count of list membership and several other places */
193e2eeea75SSimon J. Gerraty 	int refCount;
194e2eeea75SSimon J. Gerraty 	/* Suffixes we have a transformation to */
19506b9b3e0SSimon J. Gerraty 	SuffixList parents;
196e2eeea75SSimon J. Gerraty 	/* Suffixes we have a transformation from */
19706b9b3e0SSimon J. Gerraty 	SuffixList children;
198e2eeea75SSimon J. Gerraty 
199e2eeea75SSimon J. Gerraty 	/* Lists in which this suffix is referenced.
20006b9b3e0SSimon J. Gerraty 	 *
20106b9b3e0SSimon J. Gerraty 	 * XXX: These lists are used nowhere, they are just appended to, for
20206b9b3e0SSimon J. Gerraty 	 * no apparent reason.  They do have the side effect of increasing
20306b9b3e0SSimon J. Gerraty 	 * refCount though. */
20406b9b3e0SSimon J. Gerraty 	SuffixListList ref;
20506b9b3e0SSimon J. Gerraty } Suffix;
2063955d011SMarcel Moolenaar 
2073955d011SMarcel Moolenaar /*
20806b9b3e0SSimon J. Gerraty  * A candidate when searching for implied sources.
20906b9b3e0SSimon J. Gerraty  *
21006b9b3e0SSimon J. Gerraty  * For example, when "src.o" is to be made, a typical candidate is "src.c"
21106b9b3e0SSimon J. Gerraty  * via the transformation rule ".c.o".  If that doesn't exist, maybe there is
21206b9b3e0SSimon J. Gerraty  * another transformation rule ".pas.c" that would make "src.pas" an indirect
21306b9b3e0SSimon J. Gerraty  * candidate as well.  The first such chain that leads to an existing file or
21406b9b3e0SSimon J. Gerraty  * node is finally chosen to be made.
2153955d011SMarcel Moolenaar  */
21606b9b3e0SSimon J. Gerraty typedef struct Candidate {
21706b9b3e0SSimon J. Gerraty 	/* The file or node to look for. */
21806b9b3e0SSimon J. Gerraty 	char *file;
21906b9b3e0SSimon J. Gerraty 	/* The prefix from which file was formed.
22006b9b3e0SSimon J. Gerraty 	 * Its memory is shared among all candidates. */
22106b9b3e0SSimon J. Gerraty 	char *prefix;
22206b9b3e0SSimon J. Gerraty 	/* The suffix on the file. */
22306b9b3e0SSimon J. Gerraty 	Suffix *suff;
22406b9b3e0SSimon J. Gerraty 
22506b9b3e0SSimon J. Gerraty 	/* The candidate that can be made from this,
22606b9b3e0SSimon J. Gerraty 	 * or NULL for the top-level candidate. */
22706b9b3e0SSimon J. Gerraty 	struct Candidate *parent;
22806b9b3e0SSimon J. Gerraty 	/* The node describing the file. */
22906b9b3e0SSimon J. Gerraty 	GNode *node;
23006b9b3e0SSimon J. Gerraty 
23106b9b3e0SSimon J. Gerraty 	/* Count of existing children, only used for memory management, so we
23206b9b3e0SSimon J. Gerraty 	 * don't free this candidate too early or too late. */
23306b9b3e0SSimon J. Gerraty 	int numChildren;
2343955d011SMarcel Moolenaar #ifdef DEBUG_SRC
23506b9b3e0SSimon J. Gerraty 	CandidateList childrenList;
2363955d011SMarcel Moolenaar #endif
23706b9b3e0SSimon J. Gerraty } Candidate;
2383955d011SMarcel Moolenaar 
23906b9b3e0SSimon J. Gerraty typedef struct CandidateSearcher {
24006b9b3e0SSimon J. Gerraty 
24106b9b3e0SSimon J. Gerraty 	CandidateList list;
24206b9b3e0SSimon J. Gerraty 
24306b9b3e0SSimon J. Gerraty 	/*
24406b9b3e0SSimon J. Gerraty 	 * TODO: Add HashSet for seen entries, to avoid endless loops such as
24506b9b3e0SSimon J. Gerraty 	 * in suff-transform-endless.mk.
24606b9b3e0SSimon J. Gerraty 	 */
24706b9b3e0SSimon J. Gerraty 
24806b9b3e0SSimon J. Gerraty } CandidateSearcher;
24906b9b3e0SSimon J. Gerraty 
25006b9b3e0SSimon J. Gerraty 
25106b9b3e0SSimon J. Gerraty /* TODO: Document the difference between nullSuff and emptySuff. */
252e2eeea75SSimon J. Gerraty /* The NULL suffix for this run */
25306b9b3e0SSimon J. Gerraty static Suffix *nullSuff;
254e2eeea75SSimon J. Gerraty /* The empty suffix required for POSIX single-suffix transformation rules */
25506b9b3e0SSimon J. Gerraty static Suffix *emptySuff;
2563955d011SMarcel Moolenaar 
2573955d011SMarcel Moolenaar 
25806b9b3e0SSimon J. Gerraty static Suffix *
25906b9b3e0SSimon J. Gerraty Suffix_Ref(Suffix *suff)
26006b9b3e0SSimon J. Gerraty {
26106b9b3e0SSimon J. Gerraty 	suff->refCount++;
26206b9b3e0SSimon J. Gerraty 	return suff;
26306b9b3e0SSimon J. Gerraty }
26406b9b3e0SSimon J. Gerraty 
26506b9b3e0SSimon J. Gerraty /* Change the value of a Suffix variable, adjusting the reference counts. */
26606b9b3e0SSimon J. Gerraty static void
26706b9b3e0SSimon J. Gerraty Suffix_Reassign(Suffix **var, Suffix *suff)
26806b9b3e0SSimon J. Gerraty {
26906b9b3e0SSimon J. Gerraty 	if (*var != NULL)
27006b9b3e0SSimon J. Gerraty 		(*var)->refCount--;
27106b9b3e0SSimon J. Gerraty 	*var = suff;
27206b9b3e0SSimon J. Gerraty 	suff->refCount++;
27306b9b3e0SSimon J. Gerraty }
27406b9b3e0SSimon J. Gerraty 
27506b9b3e0SSimon J. Gerraty /* Set a Suffix variable to NULL, adjusting the reference count. */
27606b9b3e0SSimon J. Gerraty static void
27706b9b3e0SSimon J. Gerraty Suffix_Unassign(Suffix **var)
27806b9b3e0SSimon J. Gerraty {
27906b9b3e0SSimon J. Gerraty 	if (*var != NULL)
28006b9b3e0SSimon J. Gerraty 		(*var)->refCount--;
28106b9b3e0SSimon J. Gerraty 	*var = NULL;
28206b9b3e0SSimon J. Gerraty }
2833955d011SMarcel Moolenaar 
284e2eeea75SSimon J. Gerraty /*
2853955d011SMarcel Moolenaar  * See if pref is a prefix of str.
286e2eeea75SSimon J. Gerraty  * Return NULL if it ain't, pointer to character in str after prefix if so.
2873955d011SMarcel Moolenaar  */
2883955d011SMarcel Moolenaar static const char *
28906b9b3e0SSimon J. Gerraty StrTrimPrefix(const char *pref, const char *str)
2903955d011SMarcel Moolenaar {
29106b9b3e0SSimon J. Gerraty 	while (*str != '\0' && *pref == *str) {
2923955d011SMarcel Moolenaar 		pref++;
2933955d011SMarcel Moolenaar 		str++;
2943955d011SMarcel Moolenaar 	}
2953955d011SMarcel Moolenaar 
296e2eeea75SSimon J. Gerraty 	return *pref != '\0' ? NULL : str;
2973955d011SMarcel Moolenaar }
2983955d011SMarcel Moolenaar 
299e2eeea75SSimon J. Gerraty /*
30006b9b3e0SSimon J. Gerraty  * See if suff is a suffix of str, and if so, return the pointer to the suffix
30106b9b3e0SSimon J. Gerraty  * in str, which at the same time marks the end of the prefix.
3023955d011SMarcel Moolenaar  */
303956e45f6SSimon J. Gerraty static const char *
30406b9b3e0SSimon J. Gerraty StrTrimSuffix(const char *str, size_t strLen, const char *suff, size_t suffLen)
3053955d011SMarcel Moolenaar {
30606b9b3e0SSimon J. Gerraty 	const char *suffInStr;
30706b9b3e0SSimon J. Gerraty 	size_t i;
3083955d011SMarcel Moolenaar 
30906b9b3e0SSimon J. Gerraty 	if (strLen < suffLen)
31006b9b3e0SSimon J. Gerraty 		return NULL;
3113955d011SMarcel Moolenaar 
31206b9b3e0SSimon J. Gerraty 	suffInStr = str + strLen - suffLen;
31306b9b3e0SSimon J. Gerraty 	for (i = 0; i < suffLen; i++)
31406b9b3e0SSimon J. Gerraty 		if (suff[i] != suffInStr[i])
31506b9b3e0SSimon J. Gerraty 			return NULL;
3163955d011SMarcel Moolenaar 
31706b9b3e0SSimon J. Gerraty 	return suffInStr;
3183955d011SMarcel Moolenaar }
3193955d011SMarcel Moolenaar 
32006b9b3e0SSimon J. Gerraty /*
32106b9b3e0SSimon J. Gerraty  * See if suff is a suffix of name, and if so, return the end of the prefix
32206b9b3e0SSimon J. Gerraty  * in name.
32306b9b3e0SSimon J. Gerraty  */
32406b9b3e0SSimon J. Gerraty static const char *
32506b9b3e0SSimon J. Gerraty Suffix_TrimSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
32606b9b3e0SSimon J. Gerraty {
32706b9b3e0SSimon J. Gerraty 	return StrTrimSuffix(nameEnd - nameLen, nameLen,
32806b9b3e0SSimon J. Gerraty 	    suff->name, suff->nameLen);
3293955d011SMarcel Moolenaar }
3303955d011SMarcel Moolenaar 
3312c3632d1SSimon J. Gerraty static Boolean
33206b9b3e0SSimon J. Gerraty Suffix_IsSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
3333955d011SMarcel Moolenaar {
33406b9b3e0SSimon J. Gerraty 	return Suffix_TrimSuffix(suff, nameLen, nameEnd) != NULL;
3353955d011SMarcel Moolenaar }
3363955d011SMarcel Moolenaar 
33706b9b3e0SSimon J. Gerraty static Suffix *
33806b9b3e0SSimon J. Gerraty FindSuffixByNameLen(const char *name, size_t nameLen)
3393955d011SMarcel Moolenaar {
34006b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
341956e45f6SSimon J. Gerraty 
34206b9b3e0SSimon J. Gerraty 	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
34306b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
34406b9b3e0SSimon J. Gerraty 		if (suff->nameLen == nameLen &&
34506b9b3e0SSimon J. Gerraty 		    memcmp(suff->name, name, nameLen) == 0)
346956e45f6SSimon J. Gerraty 			return suff;
347956e45f6SSimon J. Gerraty 	}
348956e45f6SSimon J. Gerraty 	return NULL;
3493955d011SMarcel Moolenaar }
3503955d011SMarcel Moolenaar 
35106b9b3e0SSimon J. Gerraty static Suffix *
35206b9b3e0SSimon J. Gerraty FindSuffixByName(const char *name)
3533955d011SMarcel Moolenaar {
35406b9b3e0SSimon J. Gerraty 	return FindSuffixByNameLen(name, strlen(name));
3553955d011SMarcel Moolenaar }
3563955d011SMarcel Moolenaar 
357956e45f6SSimon J. Gerraty static GNode *
358956e45f6SSimon J. Gerraty FindTransformByName(const char *name)
3593955d011SMarcel Moolenaar {
360956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
36106b9b3e0SSimon J. Gerraty 
36206b9b3e0SSimon J. Gerraty 	for (ln = transforms.first; ln != NULL; ln = ln->next) {
363956e45f6SSimon J. Gerraty 		GNode *gn = ln->datum;
364956e45f6SSimon J. Gerraty 		if (strcmp(gn->name, name) == 0)
365956e45f6SSimon J. Gerraty 			return gn;
366956e45f6SSimon J. Gerraty 	}
367956e45f6SSimon J. Gerraty 	return NULL;
3683955d011SMarcel Moolenaar }
3693955d011SMarcel Moolenaar 
3703955d011SMarcel Moolenaar static void
37106b9b3e0SSimon J. Gerraty SuffixList_Unref(SuffixList *list, Suffix *suff)
3723955d011SMarcel Moolenaar {
37306b9b3e0SSimon J. Gerraty 	SuffixListNode *ln = Lst_FindDatum(list, suff);
3743955d011SMarcel Moolenaar 	if (ln != NULL) {
375956e45f6SSimon J. Gerraty 		Lst_Remove(list, ln);
376956e45f6SSimon J. Gerraty 		suff->refCount--;
3773955d011SMarcel Moolenaar 	}
3783955d011SMarcel Moolenaar }
3793955d011SMarcel Moolenaar 
3802c3632d1SSimon J. Gerraty /* Free up all memory associated with the given suffix structure. */
3813955d011SMarcel Moolenaar static void
38206b9b3e0SSimon J. Gerraty Suffix_Free(Suffix *suff)
3833955d011SMarcel Moolenaar {
3843955d011SMarcel Moolenaar 
38506b9b3e0SSimon J. Gerraty 	if (suff == nullSuff)
38606b9b3e0SSimon J. Gerraty 		nullSuff = NULL;
3873955d011SMarcel Moolenaar 
388e2eeea75SSimon J. Gerraty 	if (suff == emptySuff)
3893955d011SMarcel Moolenaar 		emptySuff = NULL;
3903955d011SMarcel Moolenaar 
391956e45f6SSimon J. Gerraty #if 0
3923955d011SMarcel Moolenaar 	/* We don't delete suffixes in order, so we cannot use this */
393e2eeea75SSimon J. Gerraty 	if (suff->refCount != 0)
394e2eeea75SSimon J. Gerraty 		Punt("Internal error deleting suffix `%s' with refcount = %d",
395e2eeea75SSimon J. Gerraty 		    suff->name, suff->refCount);
3963955d011SMarcel Moolenaar #endif
3973955d011SMarcel Moolenaar 
39806b9b3e0SSimon J. Gerraty 	Lst_Done(&suff->ref);
39906b9b3e0SSimon J. Gerraty 	Lst_Done(&suff->children);
40006b9b3e0SSimon J. Gerraty 	Lst_Done(&suff->parents);
40106b9b3e0SSimon J. Gerraty 	SearchPath_Free(suff->searchPath);
4023955d011SMarcel Moolenaar 
403e2eeea75SSimon J. Gerraty 	free(suff->name);
404e2eeea75SSimon J. Gerraty 	free(suff);
4053955d011SMarcel Moolenaar }
4063955d011SMarcel Moolenaar 
40706b9b3e0SSimon J. Gerraty static void
40806b9b3e0SSimon J. Gerraty SuffFree(void *p)
40906b9b3e0SSimon J. Gerraty {
41006b9b3e0SSimon J. Gerraty 	Suffix_Free(p);
41106b9b3e0SSimon J. Gerraty }
41206b9b3e0SSimon J. Gerraty 
4132c3632d1SSimon J. Gerraty /* Remove the suffix from the list, and free if it is otherwise unused. */
4143955d011SMarcel Moolenaar static void
41506b9b3e0SSimon J. Gerraty SuffixList_Remove(SuffixList *list, Suffix *suff)
4163955d011SMarcel Moolenaar {
41706b9b3e0SSimon J. Gerraty 	SuffixList_Unref(list, suff);
418956e45f6SSimon J. Gerraty 	if (suff->refCount == 0) {
419e2eeea75SSimon J. Gerraty 		/* XXX: can lead to suff->refCount == -1 */
42006b9b3e0SSimon J. Gerraty 		SuffixList_Unref(&sufflist, suff);
42106b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "Removing suffix \"%s\"\n", suff->name);
422956e45f6SSimon J. Gerraty 		SuffFree(suff);
4233955d011SMarcel Moolenaar 	}
4243955d011SMarcel Moolenaar }
4252c3632d1SSimon J. Gerraty 
42606b9b3e0SSimon J. Gerraty /*
42706b9b3e0SSimon J. Gerraty  * Insert the suffix into the list, keeping the list ordered by suffix
42806b9b3e0SSimon J. Gerraty  * number.
42906b9b3e0SSimon J. Gerraty  */
4303955d011SMarcel Moolenaar static void
43106b9b3e0SSimon J. Gerraty SuffixList_Insert(SuffixList *list, Suffix *suff)
4323955d011SMarcel Moolenaar {
43306b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
43406b9b3e0SSimon J. Gerraty 	Suffix *listSuff = NULL;
4353955d011SMarcel Moolenaar 
436956e45f6SSimon J. Gerraty 	for (ln = list->first; ln != NULL; ln = ln->next) {
437956e45f6SSimon J. Gerraty 		listSuff = ln->datum;
438956e45f6SSimon J. Gerraty 		if (listSuff->sNum >= suff->sNum)
4393955d011SMarcel Moolenaar 			break;
4403955d011SMarcel Moolenaar 	}
4412c3632d1SSimon J. Gerraty 
4423955d011SMarcel Moolenaar 	if (ln == NULL) {
44306b9b3e0SSimon J. Gerraty 		DEBUG2(SUFF, "inserting \"%s\" (%d) at end of list\n",
444956e45f6SSimon J. Gerraty 		    suff->name, suff->sNum);
44506b9b3e0SSimon J. Gerraty 		Lst_Append(list, Suffix_Ref(suff));
44606b9b3e0SSimon J. Gerraty 		Lst_Append(&suff->ref, list);
447956e45f6SSimon J. Gerraty 	} else if (listSuff->sNum != suff->sNum) {
448e2eeea75SSimon J. Gerraty 		DEBUG4(SUFF, "inserting \"%s\" (%d) before \"%s\" (%d)\n",
449956e45f6SSimon J. Gerraty 		    suff->name, suff->sNum, listSuff->name, listSuff->sNum);
45006b9b3e0SSimon J. Gerraty 		Lst_InsertBefore(list, ln, Suffix_Ref(suff));
45106b9b3e0SSimon J. Gerraty 		Lst_Append(&suff->ref, list);
4522c3632d1SSimon J. Gerraty 	} else {
45306b9b3e0SSimon J. Gerraty 		DEBUG2(SUFF, "\"%s\" (%d) is already there\n",
45406b9b3e0SSimon J. Gerraty 		    suff->name, suff->sNum);
4553955d011SMarcel Moolenaar 	}
4563955d011SMarcel Moolenaar }
4573955d011SMarcel Moolenaar 
458e2eeea75SSimon J. Gerraty static void
45906b9b3e0SSimon J. Gerraty Relate(Suffix *srcSuff, Suffix *targSuff)
460e2eeea75SSimon J. Gerraty {
46106b9b3e0SSimon J. Gerraty 	SuffixList_Insert(&targSuff->children, srcSuff);
46206b9b3e0SSimon J. Gerraty 	SuffixList_Insert(&srcSuff->parents, targSuff);
463e2eeea75SSimon J. Gerraty }
464e2eeea75SSimon J. Gerraty 
46506b9b3e0SSimon J. Gerraty static Suffix *
46606b9b3e0SSimon J. Gerraty Suffix_New(const char *name)
4672c3632d1SSimon J. Gerraty {
46806b9b3e0SSimon J. Gerraty 	Suffix *suff = bmake_malloc(sizeof *suff);
4692c3632d1SSimon J. Gerraty 
470e2eeea75SSimon J. Gerraty 	suff->name = bmake_strdup(name);
471e2eeea75SSimon J. Gerraty 	suff->nameLen = strlen(suff->name);
47206b9b3e0SSimon J. Gerraty 	suff->searchPath = SearchPath_New();
47306b9b3e0SSimon J. Gerraty 	Lst_Init(&suff->children);
47406b9b3e0SSimon J. Gerraty 	Lst_Init(&suff->parents);
47506b9b3e0SSimon J. Gerraty 	Lst_Init(&suff->ref);
476e2eeea75SSimon J. Gerraty 	suff->sNum = sNum++;
47706b9b3e0SSimon J. Gerraty 	suff->flags = SUFF_NONE;
478e2eeea75SSimon J. Gerraty 	suff->refCount = 1; /* XXX: why 1? It's not assigned anywhere yet. */
4792c3632d1SSimon J. Gerraty 
480e2eeea75SSimon J. Gerraty 	return suff;
4812c3632d1SSimon J. Gerraty }
4822c3632d1SSimon J. Gerraty 
483e2eeea75SSimon J. Gerraty /*
484e2eeea75SSimon J. Gerraty  * Nuke the list of suffixes but keep all transformation rules around. The
485e2eeea75SSimon J. Gerraty  * transformation graph is destroyed in this process, but we leave the list
486e2eeea75SSimon J. Gerraty  * of rules so when a new graph is formed, the rules will remain. This
487e2eeea75SSimon J. Gerraty  * function is called when a line '.SUFFIXES:' with an empty suffixes list is
488e2eeea75SSimon J. Gerraty  * encountered in a makefile.
489e2eeea75SSimon J. Gerraty  */
4903955d011SMarcel Moolenaar void
4913955d011SMarcel Moolenaar Suff_ClearSuffixes(void)
4923955d011SMarcel Moolenaar {
4933955d011SMarcel Moolenaar #ifdef CLEANUP
49406b9b3e0SSimon J. Gerraty 	Lst_MoveAll(&suffClean, &sufflist);
4953955d011SMarcel Moolenaar #endif
49606b9b3e0SSimon J. Gerraty 	DEBUG0(SUFF, "Clearing all suffixes\n");
49706b9b3e0SSimon J. Gerraty 	Lst_Init(&sufflist);
4983955d011SMarcel Moolenaar 	sNum = 0;
49906b9b3e0SSimon J. Gerraty 	if (nullSuff != NULL)
50006b9b3e0SSimon J. Gerraty 		SuffFree(nullSuff);
50106b9b3e0SSimon J. Gerraty 	emptySuff = nullSuff = Suffix_New("");
5026e050540SSimon J. Gerraty 
50306b9b3e0SSimon J. Gerraty 	SearchPath_AddAll(nullSuff->searchPath, &dirSearchPath);
50406b9b3e0SSimon J. Gerraty 	nullSuff->flags = SUFF_NULL;
5053955d011SMarcel Moolenaar }
5063955d011SMarcel Moolenaar 
50706b9b3e0SSimon J. Gerraty /*
50806b9b3e0SSimon J. Gerraty  * Parse a transformation string such as ".c.o" to find its two component
509956e45f6SSimon J. Gerraty  * suffixes (the source ".c" and the target ".o").  If there are no such
510956e45f6SSimon J. Gerraty  * suffixes, try a single-suffix transformation as well.
5113955d011SMarcel Moolenaar  *
512956e45f6SSimon J. Gerraty  * Return TRUE if the string is a valid transformation.
5133955d011SMarcel Moolenaar  */
5143955d011SMarcel Moolenaar static Boolean
51506b9b3e0SSimon J. Gerraty ParseTransform(const char *str, Suffix **out_src, Suffix **out_targ)
5163955d011SMarcel Moolenaar {
51706b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
51806b9b3e0SSimon J. Gerraty 	Suffix *single = NULL;
5193955d011SMarcel Moolenaar 
5203955d011SMarcel Moolenaar 	/*
5213955d011SMarcel Moolenaar 	 * Loop looking first for a suffix that matches the start of the
5223955d011SMarcel Moolenaar 	 * string and then for one that exactly matches the rest of it. If
5233955d011SMarcel Moolenaar 	 * we can find two that meet these criteria, we've successfully
5243955d011SMarcel Moolenaar 	 * parsed the string.
5253955d011SMarcel Moolenaar 	 */
52606b9b3e0SSimon J. Gerraty 	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
52706b9b3e0SSimon J. Gerraty 		Suffix *src = ln->datum;
528956e45f6SSimon J. Gerraty 
52906b9b3e0SSimon J. Gerraty 		if (StrTrimPrefix(src->name, str) == NULL)
530956e45f6SSimon J. Gerraty 			continue;
531956e45f6SSimon J. Gerraty 
532956e45f6SSimon J. Gerraty 		if (str[src->nameLen] == '\0') {
53306b9b3e0SSimon J. Gerraty 			single = src;
5343955d011SMarcel Moolenaar 		} else {
53506b9b3e0SSimon J. Gerraty 			Suffix *targ = FindSuffixByName(str + src->nameLen);
536956e45f6SSimon J. Gerraty 			if (targ != NULL) {
537956e45f6SSimon J. Gerraty 				*out_src = src;
538956e45f6SSimon J. Gerraty 				*out_targ = targ;
539956e45f6SSimon J. Gerraty 				return TRUE;
5403955d011SMarcel Moolenaar 			}
541956e45f6SSimon J. Gerraty 		}
542956e45f6SSimon J. Gerraty 	}
543956e45f6SSimon J. Gerraty 
54406b9b3e0SSimon J. Gerraty 	if (single != NULL) {
5453955d011SMarcel Moolenaar 		/*
54606b9b3e0SSimon J. Gerraty 		 * There was a suffix that encompassed the entire string, so we
54706b9b3e0SSimon J. Gerraty 		 * assume it was a transformation to the null suffix (thank you
54806b9b3e0SSimon J. Gerraty 		 * POSIX; search for "single suffix" or "single-suffix").
5493955d011SMarcel Moolenaar 		 *
55006b9b3e0SSimon J. Gerraty 		 * We still prefer to find a double rule over a singleton,
55106b9b3e0SSimon J. Gerraty 		 * hence we leave this check until the end.
55206b9b3e0SSimon J. Gerraty 		 *
55306b9b3e0SSimon J. Gerraty 		 * XXX: Use emptySuff over nullSuff?
5543955d011SMarcel Moolenaar 		 */
55506b9b3e0SSimon J. Gerraty 		*out_src = single;
55606b9b3e0SSimon J. Gerraty 		*out_targ = nullSuff;
5573841c287SSimon J. Gerraty 		return TRUE;
5583955d011SMarcel Moolenaar 	}
5593841c287SSimon J. Gerraty 	return FALSE;
5603955d011SMarcel Moolenaar }
5613955d011SMarcel Moolenaar 
56206b9b3e0SSimon J. Gerraty /*
56306b9b3e0SSimon J. Gerraty  * Return TRUE if the given string is a transformation rule, that is, a
564e2eeea75SSimon J. Gerraty  * concatenation of two known suffixes such as ".c.o" or a single suffix
56506b9b3e0SSimon J. Gerraty  * such as ".o".
56606b9b3e0SSimon J. Gerraty  */
5673955d011SMarcel Moolenaar Boolean
568956e45f6SSimon J. Gerraty Suff_IsTransform(const char *str)
5693955d011SMarcel Moolenaar {
57006b9b3e0SSimon J. Gerraty 	Suffix *src, *targ;
5713955d011SMarcel Moolenaar 
57206b9b3e0SSimon J. Gerraty 	return ParseTransform(str, &src, &targ);
5733955d011SMarcel Moolenaar }
5743955d011SMarcel Moolenaar 
57506b9b3e0SSimon J. Gerraty /*
57606b9b3e0SSimon J. Gerraty  * Add the transformation rule to the list of rules and place the
577956e45f6SSimon J. Gerraty  * transformation itself in the graph.
5783955d011SMarcel Moolenaar  *
579956e45f6SSimon J. Gerraty  * The transformation is linked to the two suffixes mentioned in the name.
580956e45f6SSimon J. Gerraty  *
5813955d011SMarcel Moolenaar  * Input:
582956e45f6SSimon J. Gerraty  *	name		must have the form ".from.to" or just ".from"
5833955d011SMarcel Moolenaar  *
5843955d011SMarcel Moolenaar  * Results:
585956e45f6SSimon J. Gerraty  *	The created or existing transformation node in the transforms list
5863955d011SMarcel Moolenaar  */
5873955d011SMarcel Moolenaar GNode *
588956e45f6SSimon J. Gerraty Suff_AddTransform(const char *name)
5893955d011SMarcel Moolenaar {
59006b9b3e0SSimon J. Gerraty 	Suffix *srcSuff;
59106b9b3e0SSimon J. Gerraty 	Suffix *targSuff;
5923955d011SMarcel Moolenaar 
593e2eeea75SSimon J. Gerraty 	GNode *gn = FindTransformByName(name);
594956e45f6SSimon J. Gerraty 	if (gn == NULL) {
5953955d011SMarcel Moolenaar 		/*
59606b9b3e0SSimon J. Gerraty 		 * Make a new graph node for the transformation. It will be
59706b9b3e0SSimon J. Gerraty 		 * filled in by the Parse module.
5983955d011SMarcel Moolenaar 		 */
599e2eeea75SSimon J. Gerraty 		gn = GNode_New(name);
60006b9b3e0SSimon J. Gerraty 		Lst_Append(&transforms, gn);
6013955d011SMarcel Moolenaar 	} else {
6023955d011SMarcel Moolenaar 		/*
60306b9b3e0SSimon J. Gerraty 		 * New specification for transformation rule. Just nuke the
60406b9b3e0SSimon J. Gerraty 		 * old list of commands so they can be filled in again. We
60506b9b3e0SSimon J. Gerraty 		 * don't actually free the commands themselves, because a
60606b9b3e0SSimon J. Gerraty 		 * given command can be attached to several different
60706b9b3e0SSimon J. Gerraty 		 * transformations.
6083955d011SMarcel Moolenaar 		 */
60906b9b3e0SSimon J. Gerraty 		Lst_Done(&gn->commands);
61006b9b3e0SSimon J. Gerraty 		Lst_Init(&gn->commands);
61106b9b3e0SSimon J. Gerraty 		Lst_Done(&gn->children);
61206b9b3e0SSimon J. Gerraty 		Lst_Init(&gn->children);
6133955d011SMarcel Moolenaar 	}
6143955d011SMarcel Moolenaar 
6153955d011SMarcel Moolenaar 	gn->type = OP_TRANSFORM;
6163955d011SMarcel Moolenaar 
617e2eeea75SSimon J. Gerraty 	{
61806b9b3e0SSimon J. Gerraty 		/* TODO: Avoid the redundant parsing here. */
61906b9b3e0SSimon J. Gerraty 		Boolean ok = ParseTransform(name, &srcSuff, &targSuff);
6202c3632d1SSimon J. Gerraty 		assert(ok);
6212c3632d1SSimon J. Gerraty 		(void)ok;
622e2eeea75SSimon J. Gerraty 	}
6233955d011SMarcel Moolenaar 
62406b9b3e0SSimon J. Gerraty 	/* Link the two together in the proper relationship and order. */
62506b9b3e0SSimon J. Gerraty 	DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
626e2eeea75SSimon J. Gerraty 	    srcSuff->name, targSuff->name);
62706b9b3e0SSimon J. Gerraty 	Relate(srcSuff, targSuff);
6283955d011SMarcel Moolenaar 
6293841c287SSimon J. Gerraty 	return gn;
6303955d011SMarcel Moolenaar }
6313955d011SMarcel Moolenaar 
63206b9b3e0SSimon J. Gerraty /*
63306b9b3e0SSimon J. Gerraty  * Handle the finish of a transformation definition, removing the
6342c3632d1SSimon J. Gerraty  * transformation from the graph if it has neither commands nor sources.
6352c3632d1SSimon J. Gerraty  *
6362c3632d1SSimon J. Gerraty  * If the node has no commands or children, the children and parents lists
6372c3632d1SSimon J. Gerraty  * of the affected suffixes are altered.
6383955d011SMarcel Moolenaar  *
6393955d011SMarcel Moolenaar  * Input:
640956e45f6SSimon J. Gerraty  *	gn		Node for transformation
6413955d011SMarcel Moolenaar  */
642956e45f6SSimon J. Gerraty void
643956e45f6SSimon J. Gerraty Suff_EndTransform(GNode *gn)
6443955d011SMarcel Moolenaar {
64506b9b3e0SSimon J. Gerraty 	Suffix *srcSuff, *targSuff;
64606b9b3e0SSimon J. Gerraty 	SuffixList *srcSuffParents;
647e2eeea75SSimon J. Gerraty 
64806b9b3e0SSimon J. Gerraty 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&gn->cohorts))
64906b9b3e0SSimon J. Gerraty 		gn = gn->cohorts.last->datum;
65006b9b3e0SSimon J. Gerraty 
65106b9b3e0SSimon J. Gerraty 	if (!(gn->type & OP_TRANSFORM))
65206b9b3e0SSimon J. Gerraty 		return;
65306b9b3e0SSimon J. Gerraty 
65406b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->commands) || !Lst_IsEmpty(&gn->children)) {
65506b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "transformation %s complete\n", gn->name);
65606b9b3e0SSimon J. Gerraty 		return;
65706b9b3e0SSimon J. Gerraty 	}
6583955d011SMarcel Moolenaar 
6593955d011SMarcel Moolenaar 	/*
6603955d011SMarcel Moolenaar 	 * SuffParseTransform() may fail for special rules which are not
6613955d011SMarcel Moolenaar 	 * actual transformation rules. (e.g. .DEFAULT)
6623955d011SMarcel Moolenaar 	 */
66306b9b3e0SSimon J. Gerraty 	if (!ParseTransform(gn->name, &srcSuff, &targSuff))
66406b9b3e0SSimon J. Gerraty 		return;
665e2eeea75SSimon J. Gerraty 
66606b9b3e0SSimon J. Gerraty 	DEBUG2(SUFF, "deleting incomplete transformation from `%s' to `%s'\n",
667e2eeea75SSimon J. Gerraty 	    srcSuff->name, targSuff->name);
6683955d011SMarcel Moolenaar 
66906b9b3e0SSimon J. Gerraty 	/*
67006b9b3e0SSimon J. Gerraty 	 * Remember the parents since srcSuff could be deleted in
67106b9b3e0SSimon J. Gerraty 	 * SuffixList_Remove.
67206b9b3e0SSimon J. Gerraty 	 */
67306b9b3e0SSimon J. Gerraty 	srcSuffParents = &srcSuff->parents;
67406b9b3e0SSimon J. Gerraty 	SuffixList_Remove(&targSuff->children, srcSuff);
67506b9b3e0SSimon J. Gerraty 	SuffixList_Remove(srcSuffParents, targSuff);
6763955d011SMarcel Moolenaar }
6773955d011SMarcel Moolenaar 
67806b9b3e0SSimon J. Gerraty /*
67906b9b3e0SSimon J. Gerraty  * Called from Suff_AddSuffix to search through the list of
6802c3632d1SSimon J. Gerraty  * existing transformation rules and rebuild the transformation graph when
6812c3632d1SSimon J. Gerraty  * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
6822c3632d1SSimon J. Gerraty  * transformation involving this suffix and another, existing suffix, the
6832c3632d1SSimon J. Gerraty  * proper relationship is established between the two.
6842c3632d1SSimon J. Gerraty  *
6852c3632d1SSimon J. Gerraty  * The appropriate links will be made between this suffix and others if
6862c3632d1SSimon J. Gerraty  * transformation rules exist for it.
6873955d011SMarcel Moolenaar  *
6883955d011SMarcel Moolenaar  * Input:
689956e45f6SSimon J. Gerraty  *	transform	Transformation to test
690956e45f6SSimon J. Gerraty  *	suff		Suffix to rebuild
6913955d011SMarcel Moolenaar  */
692956e45f6SSimon J. Gerraty static void
69306b9b3e0SSimon J. Gerraty RebuildGraph(GNode *transform, Suffix *suff)
6943955d011SMarcel Moolenaar {
695956e45f6SSimon J. Gerraty 	const char *name = transform->name;
696956e45f6SSimon J. Gerraty 	size_t nameLen = strlen(name);
697956e45f6SSimon J. Gerraty 	const char *toName;
6983955d011SMarcel Moolenaar 
69906b9b3e0SSimon J. Gerraty 	/* See if it is a transformation from this suffix to another suffix. */
70006b9b3e0SSimon J. Gerraty 	toName = StrTrimPrefix(suff->name, name);
701956e45f6SSimon J. Gerraty 	if (toName != NULL) {
70206b9b3e0SSimon J. Gerraty 		Suffix *to = FindSuffixByName(toName);
703956e45f6SSimon J. Gerraty 		if (to != NULL) {
70406b9b3e0SSimon J. Gerraty 			Relate(suff, to);
705956e45f6SSimon J. Gerraty 			return;
7063955d011SMarcel Moolenaar 		}
7073955d011SMarcel Moolenaar 	}
7083955d011SMarcel Moolenaar 
70906b9b3e0SSimon J. Gerraty 	/* See if it is a transformation from another suffix to this suffix. */
71006b9b3e0SSimon J. Gerraty 	toName = Suffix_TrimSuffix(suff, nameLen, name + nameLen);
711956e45f6SSimon J. Gerraty 	if (toName != NULL) {
71206b9b3e0SSimon J. Gerraty 		Suffix *from = FindSuffixByNameLen(name,
71306b9b3e0SSimon J. Gerraty 		    (size_t)(toName - name));
714e2eeea75SSimon J. Gerraty 		if (from != NULL)
71506b9b3e0SSimon J. Gerraty 			Relate(from, suff);
7163955d011SMarcel Moolenaar 	}
7173955d011SMarcel Moolenaar }
7183955d011SMarcel Moolenaar 
71906b9b3e0SSimon J. Gerraty /*
72006b9b3e0SSimon J. Gerraty  * During Suff_AddSuffix, search through the list of existing targets and find
721956e45f6SSimon J. Gerraty  * if any of the existing targets can be turned into a transformation rule.
7222c3632d1SSimon J. Gerraty  *
7232c3632d1SSimon J. Gerraty  * If such a target is found and the target is the current main target, the
7242c3632d1SSimon J. Gerraty  * main target is set to NULL and the next target examined (if that exists)
7252c3632d1SSimon J. Gerraty  * becomes the main target.
7263955d011SMarcel Moolenaar  *
7273955d011SMarcel Moolenaar  * Results:
728956e45f6SSimon J. Gerraty  *	TRUE iff a new main target has been selected.
7293955d011SMarcel Moolenaar  */
730956e45f6SSimon J. Gerraty static Boolean
73106b9b3e0SSimon J. Gerraty UpdateTarget(GNode *target, GNode **inout_main, Suffix *suff,
73206b9b3e0SSimon J. Gerraty 	     Boolean *inout_removedMain)
7333955d011SMarcel Moolenaar {
73406b9b3e0SSimon J. Gerraty 	Suffix *srcSuff, *targSuff;
7353955d011SMarcel Moolenaar 	char *ptr;
7363955d011SMarcel Moolenaar 
73706b9b3e0SSimon J. Gerraty 	if (*inout_main == NULL && *inout_removedMain &&
73806b9b3e0SSimon J. Gerraty 	    !(target->type & OP_NOTARGET)) {
73906b9b3e0SSimon J. Gerraty 		DEBUG1(MAKE, "Setting main node to \"%s\"\n", target->name);
740956e45f6SSimon J. Gerraty 		*inout_main = target;
7413955d011SMarcel Moolenaar 		Targ_SetMain(target);
74206b9b3e0SSimon J. Gerraty 		/*
74306b9b3e0SSimon J. Gerraty 		 * XXX: Why could it be a good idea to return TRUE here?
74406b9b3e0SSimon J. Gerraty 		 * The main task of this function is to turn ordinary nodes
74506b9b3e0SSimon J. Gerraty 		 * into transformations, no matter whether or not a new .MAIN
74606b9b3e0SSimon J. Gerraty 		 * node has been found.
74706b9b3e0SSimon J. Gerraty 		 */
74806b9b3e0SSimon J. Gerraty 		/*
74906b9b3e0SSimon J. Gerraty 		 * XXX: Even when changing this to FALSE, none of the existing
75006b9b3e0SSimon J. Gerraty 		 * unit tests fails.
75106b9b3e0SSimon J. Gerraty 		 */
752956e45f6SSimon J. Gerraty 		return TRUE;
7533955d011SMarcel Moolenaar 	}
7543955d011SMarcel Moolenaar 
7552c3632d1SSimon J. Gerraty 	if (target->type == OP_TRANSFORM)
756956e45f6SSimon J. Gerraty 		return FALSE;
7573955d011SMarcel Moolenaar 
75806b9b3e0SSimon J. Gerraty 	/*
75906b9b3e0SSimon J. Gerraty 	 * XXX: What about a transformation ".cpp.c"?  If ".c" is added as
76006b9b3e0SSimon J. Gerraty 	 * a new suffix, it seems wrong that this transformation would be
76106b9b3e0SSimon J. Gerraty 	 * skipped just because ".c" happens to be a prefix of ".cpp".
76206b9b3e0SSimon J. Gerraty 	 */
76306b9b3e0SSimon J. Gerraty 	ptr = strstr(target->name, suff->name);
76406b9b3e0SSimon J. Gerraty 	if (ptr == NULL)
765956e45f6SSimon J. Gerraty 		return FALSE;
7663955d011SMarcel Moolenaar 
76706b9b3e0SSimon J. Gerraty 	/*
76806b9b3e0SSimon J. Gerraty 	 * XXX: In suff-rebuild.mk, in the line '.SUFFIXES: .c .b .a', this
76906b9b3e0SSimon J. Gerraty 	 * condition prevents the rule '.b.c' from being added again during
77006b9b3e0SSimon J. Gerraty 	 * Suff_AddSuffix(".b").
77106b9b3e0SSimon J. Gerraty 	 *
77206b9b3e0SSimon J. Gerraty 	 * XXX: Removing this paragraph makes suff-add-later.mk use massive
77306b9b3e0SSimon J. Gerraty 	 * amounts of memory.
77406b9b3e0SSimon J. Gerraty 	 */
77506b9b3e0SSimon J. Gerraty 	if (ptr == target->name)
77606b9b3e0SSimon J. Gerraty 		return FALSE;
77706b9b3e0SSimon J. Gerraty 
77806b9b3e0SSimon J. Gerraty 	if (ParseTransform(target->name, &srcSuff, &targSuff)) {
779956e45f6SSimon J. Gerraty 		if (*inout_main == target) {
78006b9b3e0SSimon J. Gerraty 			DEBUG1(MAKE,
78106b9b3e0SSimon J. Gerraty 			    "Setting main node from \"%s\" back to null\n",
78206b9b3e0SSimon J. Gerraty 			    target->name);
78306b9b3e0SSimon J. Gerraty 			*inout_removedMain = TRUE;
784956e45f6SSimon J. Gerraty 			*inout_main = NULL;
7853955d011SMarcel Moolenaar 			Targ_SetMain(NULL);
7863955d011SMarcel Moolenaar 		}
78706b9b3e0SSimon J. Gerraty 		Lst_Done(&target->children);
78806b9b3e0SSimon J. Gerraty 		Lst_Init(&target->children);
7893955d011SMarcel Moolenaar 		target->type = OP_TRANSFORM;
79006b9b3e0SSimon J. Gerraty 
7913955d011SMarcel Moolenaar 		/*
79206b9b3e0SSimon J. Gerraty 		 * Link the two together in the proper relationship and order.
7933955d011SMarcel Moolenaar 		 */
79406b9b3e0SSimon J. Gerraty 		DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
795e2eeea75SSimon J. Gerraty 		    srcSuff->name, targSuff->name);
79606b9b3e0SSimon J. Gerraty 		Relate(srcSuff, targSuff);
7973955d011SMarcel Moolenaar 	}
798956e45f6SSimon J. Gerraty 	return FALSE;
799956e45f6SSimon J. Gerraty }
800956e45f6SSimon J. Gerraty 
80106b9b3e0SSimon J. Gerraty /*
80206b9b3e0SSimon J. Gerraty  * Look at all existing targets to see if adding this suffix will make one
803956e45f6SSimon J. Gerraty  * of the current targets mutate into a suffix rule.
804956e45f6SSimon J. Gerraty  *
805956e45f6SSimon J. Gerraty  * This is ugly, but other makes treat all targets that start with a '.' as
80606b9b3e0SSimon J. Gerraty  * suffix rules.
80706b9b3e0SSimon J. Gerraty  */
808956e45f6SSimon J. Gerraty static void
80906b9b3e0SSimon J. Gerraty UpdateTargets(GNode **inout_main, Suffix *suff)
810956e45f6SSimon J. Gerraty {
81106b9b3e0SSimon J. Gerraty 	Boolean removedMain = FALSE;
812956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
81306b9b3e0SSimon J. Gerraty 
814956e45f6SSimon J. Gerraty 	for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
815956e45f6SSimon J. Gerraty 		GNode *gn = ln->datum;
81606b9b3e0SSimon J. Gerraty 		if (UpdateTarget(gn, inout_main, suff, &removedMain))
817956e45f6SSimon J. Gerraty 			break;
818956e45f6SSimon J. Gerraty 	}
8193955d011SMarcel Moolenaar }
8203955d011SMarcel Moolenaar 
82106b9b3e0SSimon J. Gerraty /*
82206b9b3e0SSimon J. Gerraty  * Add the suffix to the end of the list of known suffixes.
82306b9b3e0SSimon J. Gerraty  * Should we restructure the suffix graph? Make doesn't.
8243955d011SMarcel Moolenaar  *
82506b9b3e0SSimon J. Gerraty  * A GNode is created for the suffix (XXX: this sounds completely wrong) and
82606b9b3e0SSimon J. Gerraty  * a Suffix structure is created and added to the suffixes list unless the
82706b9b3e0SSimon J. Gerraty  * suffix was already known.
8283955d011SMarcel Moolenaar  * The mainNode passed can be modified if a target mutated into a
8293955d011SMarcel Moolenaar  * transform and that target happened to be the main target.
8302c3632d1SSimon J. Gerraty  *
8312c3632d1SSimon J. Gerraty  * Input:
8322c3632d1SSimon J. Gerraty  *	name		the name of the suffix to add
8333955d011SMarcel Moolenaar  */
8343955d011SMarcel Moolenaar void
835956e45f6SSimon J. Gerraty Suff_AddSuffix(const char *name, GNode **inout_main)
8363955d011SMarcel Moolenaar {
837956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
8383955d011SMarcel Moolenaar 
83906b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(name);
84006b9b3e0SSimon J. Gerraty 	if (suff != NULL)
841956e45f6SSimon J. Gerraty 		return;
842956e45f6SSimon J. Gerraty 
84306b9b3e0SSimon J. Gerraty 	suff = Suffix_New(name);
84406b9b3e0SSimon J. Gerraty 	Lst_Append(&sufflist, suff);
84506b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name);
846956e45f6SSimon J. Gerraty 
84706b9b3e0SSimon J. Gerraty 	UpdateTargets(inout_main, suff);
848956e45f6SSimon J. Gerraty 
8493955d011SMarcel Moolenaar 	/*
8503955d011SMarcel Moolenaar 	 * Look for any existing transformations from or to this suffix.
8513955d011SMarcel Moolenaar 	 * XXX: Only do this after a Suff_ClearSuffixes?
8523955d011SMarcel Moolenaar 	 */
85306b9b3e0SSimon J. Gerraty 	for (ln = transforms.first; ln != NULL; ln = ln->next)
85406b9b3e0SSimon J. Gerraty 		RebuildGraph(ln->datum, suff);
8553955d011SMarcel Moolenaar }
8563955d011SMarcel Moolenaar 
8572c3632d1SSimon J. Gerraty /* Return the search path for the given suffix, or NULL. */
858956e45f6SSimon J. Gerraty SearchPath *
859956e45f6SSimon J. Gerraty Suff_GetPath(const char *sname)
8603955d011SMarcel Moolenaar {
86106b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(sname);
86206b9b3e0SSimon J. Gerraty 	return suff != NULL ? suff->searchPath : NULL;
8633955d011SMarcel Moolenaar }
8643955d011SMarcel Moolenaar 
865e2eeea75SSimon J. Gerraty /*
866e2eeea75SSimon J. Gerraty  * Extend the search paths for all suffixes to include the default search
867e2eeea75SSimon J. Gerraty  * path (dirSearchPath).
8683955d011SMarcel Moolenaar  *
869e2eeea75SSimon J. Gerraty  * The default search path can be defined using the special target '.PATH'.
870e2eeea75SSimon J. Gerraty  * The search path of each suffix can be defined using the special target
871e2eeea75SSimon J. Gerraty  * '.PATH<suffix>'.
872e2eeea75SSimon J. Gerraty  *
873e2eeea75SSimon J. Gerraty  * If paths were specified for the ".h" suffix, the directories are stuffed
874e2eeea75SSimon J. Gerraty  * into a global variable called ".INCLUDES" with each directory preceded by
875e2eeea75SSimon J. Gerraty  * '-I'. The same is done for the ".a" suffix, except the variable is called
876e2eeea75SSimon J. Gerraty  * ".LIBS" and the flag is '-L'.
8773955d011SMarcel Moolenaar  */
8783955d011SMarcel Moolenaar void
8793955d011SMarcel Moolenaar Suff_DoPaths(void)
8803955d011SMarcel Moolenaar {
88106b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
88206b9b3e0SSimon J. Gerraty 	char *flags;
88306b9b3e0SSimon J. Gerraty 	SearchPath *includesPath = SearchPath_New();
88406b9b3e0SSimon J. Gerraty 	SearchPath *libsPath = SearchPath_New();
8853955d011SMarcel Moolenaar 
88606b9b3e0SSimon J. Gerraty 	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
88706b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
888*dba7b0efSSimon J. Gerraty 		if (!Lst_IsEmpty(&suff->searchPath->dirs)) {
8893955d011SMarcel Moolenaar #ifdef INCLUDES
89006b9b3e0SSimon J. Gerraty 			if (suff->flags & SUFF_INCLUDE)
89106b9b3e0SSimon J. Gerraty 				SearchPath_AddAll(includesPath,
89206b9b3e0SSimon J. Gerraty 				    suff->searchPath);
893956e45f6SSimon J. Gerraty #endif
8943955d011SMarcel Moolenaar #ifdef LIBRARIES
89506b9b3e0SSimon J. Gerraty 			if (suff->flags & SUFF_LIBRARY)
89606b9b3e0SSimon J. Gerraty 				SearchPath_AddAll(libsPath, suff->searchPath);
897956e45f6SSimon J. Gerraty #endif
89806b9b3e0SSimon J. Gerraty 			SearchPath_AddAll(suff->searchPath, &dirSearchPath);
8993955d011SMarcel Moolenaar 		} else {
90006b9b3e0SSimon J. Gerraty 			SearchPath_Free(suff->searchPath);
90106b9b3e0SSimon J. Gerraty 			suff->searchPath = Dir_CopyDirSearchPath();
9023955d011SMarcel Moolenaar 		}
9033955d011SMarcel Moolenaar 	}
9043955d011SMarcel Moolenaar 
905*dba7b0efSSimon J. Gerraty 	flags = SearchPath_ToFlags(includesPath, "-I");
906*dba7b0efSSimon J. Gerraty 	Global_Set(".INCLUDES", flags);
90706b9b3e0SSimon J. Gerraty 	free(flags);
9083955d011SMarcel Moolenaar 
909*dba7b0efSSimon J. Gerraty 	flags = SearchPath_ToFlags(libsPath, "-L");
910*dba7b0efSSimon J. Gerraty 	Global_Set(".LIBS", flags);
91106b9b3e0SSimon J. Gerraty 	free(flags);
91206b9b3e0SSimon J. Gerraty 
91306b9b3e0SSimon J. Gerraty 	SearchPath_Free(includesPath);
91406b9b3e0SSimon J. Gerraty 	SearchPath_Free(libsPath);
9153955d011SMarcel Moolenaar }
9163955d011SMarcel Moolenaar 
91706b9b3e0SSimon J. Gerraty /*
91806b9b3e0SSimon J. Gerraty  * Add the given suffix as a type of file which gets included.
91906b9b3e0SSimon J. Gerraty  * Called when a '.INCLUDES: .h' line is parsed.
92006b9b3e0SSimon J. Gerraty  * To have an effect, the suffix must already exist.
92106b9b3e0SSimon J. Gerraty  * This affects the magic variable '.INCLUDES'.
9223955d011SMarcel Moolenaar  */
9233955d011SMarcel Moolenaar void
92406b9b3e0SSimon J. Gerraty Suff_AddInclude(const char *suffName)
9253955d011SMarcel Moolenaar {
92606b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(suffName);
927956e45f6SSimon J. Gerraty 	if (suff != NULL)
928956e45f6SSimon J. Gerraty 		suff->flags |= SUFF_INCLUDE;
9293955d011SMarcel Moolenaar }
9303955d011SMarcel Moolenaar 
93106b9b3e0SSimon J. Gerraty /*
93206b9b3e0SSimon J. Gerraty  * Add the given suffix as a type of file which is a library.
93306b9b3e0SSimon J. Gerraty  * Called when a '.LIBS: .a' line is parsed.
93406b9b3e0SSimon J. Gerraty  * To have an effect, the suffix must already exist.
93506b9b3e0SSimon J. Gerraty  * This affects the magic variable '.LIBS'.
9363955d011SMarcel Moolenaar  */
9373955d011SMarcel Moolenaar void
93806b9b3e0SSimon J. Gerraty Suff_AddLib(const char *suffName)
9393955d011SMarcel Moolenaar {
94006b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(suffName);
941956e45f6SSimon J. Gerraty 	if (suff != NULL)
942956e45f6SSimon J. Gerraty 		suff->flags |= SUFF_LIBRARY;
9433955d011SMarcel Moolenaar }
9443955d011SMarcel Moolenaar 
9453955d011SMarcel Moolenaar /********** Implicit Source Search Functions *********/
9463955d011SMarcel Moolenaar 
94706b9b3e0SSimon J. Gerraty static void
94806b9b3e0SSimon J. Gerraty CandidateSearcher_Init(CandidateSearcher *cs)
94906b9b3e0SSimon J. Gerraty {
95006b9b3e0SSimon J. Gerraty 	Lst_Init(&cs->list);
95106b9b3e0SSimon J. Gerraty }
95206b9b3e0SSimon J. Gerraty 
95306b9b3e0SSimon J. Gerraty static void
95406b9b3e0SSimon J. Gerraty CandidateSearcher_Done(CandidateSearcher *cs)
95506b9b3e0SSimon J. Gerraty {
95606b9b3e0SSimon J. Gerraty 	Lst_Done(&cs->list);
95706b9b3e0SSimon J. Gerraty }
95806b9b3e0SSimon J. Gerraty 
95906b9b3e0SSimon J. Gerraty static void
96006b9b3e0SSimon J. Gerraty CandidateSearcher_Add(CandidateSearcher *cs, Candidate *cand)
96106b9b3e0SSimon J. Gerraty {
96206b9b3e0SSimon J. Gerraty 	/* TODO: filter duplicates */
96306b9b3e0SSimon J. Gerraty 	Lst_Append(&cs->list, cand);
96406b9b3e0SSimon J. Gerraty }
96506b9b3e0SSimon J. Gerraty 
96606b9b3e0SSimon J. Gerraty static void
96706b9b3e0SSimon J. Gerraty CandidateSearcher_AddIfNew(CandidateSearcher *cs, Candidate *cand)
96806b9b3e0SSimon J. Gerraty {
96906b9b3e0SSimon J. Gerraty 	/* TODO: filter duplicates */
97006b9b3e0SSimon J. Gerraty 	if (Lst_FindDatum(&cs->list, cand) == NULL)
97106b9b3e0SSimon J. Gerraty 		Lst_Append(&cs->list, cand);
97206b9b3e0SSimon J. Gerraty }
97306b9b3e0SSimon J. Gerraty 
97406b9b3e0SSimon J. Gerraty static void
97506b9b3e0SSimon J. Gerraty CandidateSearcher_MoveAll(CandidateSearcher *cs, CandidateList *list)
97606b9b3e0SSimon J. Gerraty {
97706b9b3e0SSimon J. Gerraty 	/* TODO: filter duplicates */
97806b9b3e0SSimon J. Gerraty 	Lst_MoveAll(&cs->list, list);
97906b9b3e0SSimon J. Gerraty }
98006b9b3e0SSimon J. Gerraty 
98106b9b3e0SSimon J. Gerraty 
982956e45f6SSimon J. Gerraty #ifdef DEBUG_SRC
983956e45f6SSimon J. Gerraty static void
98406b9b3e0SSimon J. Gerraty CandidateList_PrintAddrs(CandidateList *list)
985956e45f6SSimon J. Gerraty {
98606b9b3e0SSimon J. Gerraty 	CandidateListNode *ln;
98706b9b3e0SSimon J. Gerraty 
98806b9b3e0SSimon J. Gerraty 	for (ln = list->first; ln != NULL; ln = ln->next) {
98906b9b3e0SSimon J. Gerraty 		Candidate *cand = ln->datum;
99006b9b3e0SSimon J. Gerraty 		debug_printf(" %p:%s", cand, cand->file);
99106b9b3e0SSimon J. Gerraty 	}
992956e45f6SSimon J. Gerraty 	debug_printf("\n");
993956e45f6SSimon J. Gerraty }
994956e45f6SSimon J. Gerraty #endif
995956e45f6SSimon J. Gerraty 
99606b9b3e0SSimon J. Gerraty static Candidate *
99706b9b3e0SSimon J. Gerraty Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent,
99806b9b3e0SSimon J. Gerraty 	      GNode *gn)
999956e45f6SSimon J. Gerraty {
100006b9b3e0SSimon J. Gerraty 	Candidate *cand = bmake_malloc(sizeof *cand);
1001956e45f6SSimon J. Gerraty 
100206b9b3e0SSimon J. Gerraty 	cand->file = name;
100306b9b3e0SSimon J. Gerraty 	cand->prefix = prefix;
100406b9b3e0SSimon J. Gerraty 	cand->suff = Suffix_Ref(suff);
100506b9b3e0SSimon J. Gerraty 	cand->parent = parent;
100606b9b3e0SSimon J. Gerraty 	cand->node = gn;
100706b9b3e0SSimon J. Gerraty 	cand->numChildren = 0;
1008956e45f6SSimon J. Gerraty #ifdef DEBUG_SRC
100906b9b3e0SSimon J. Gerraty 	Lst_Init(&cand->childrenList);
1010956e45f6SSimon J. Gerraty #endif
1011956e45f6SSimon J. Gerraty 
101206b9b3e0SSimon J. Gerraty 	return cand;
1013956e45f6SSimon J. Gerraty }
1014956e45f6SSimon J. Gerraty 
101506b9b3e0SSimon J. Gerraty /* Add a new candidate to the list. */
101606b9b3e0SSimon J. Gerraty /*ARGSUSED*/
1017956e45f6SSimon J. Gerraty static void
101806b9b3e0SSimon J. Gerraty CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ,
101906b9b3e0SSimon J. Gerraty 		  Suffix *suff, const char *debug_tag)
1020956e45f6SSimon J. Gerraty {
102106b9b3e0SSimon J. Gerraty 	Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ,
102206b9b3e0SSimon J. Gerraty 	    NULL);
1023e2eeea75SSimon J. Gerraty 	targ->numChildren++;
102406b9b3e0SSimon J. Gerraty 	Lst_Append(list, cand);
102506b9b3e0SSimon J. Gerraty 
1026956e45f6SSimon J. Gerraty #ifdef DEBUG_SRC
102706b9b3e0SSimon J. Gerraty 	Lst_Append(&targ->childrenList, cand);
102806b9b3e0SSimon J. Gerraty 	debug_printf("%s add suff %p:%s candidate %p:%s to list %p:",
102906b9b3e0SSimon J. Gerraty 	    debug_tag, targ, targ->file, cand, cand->file, list);
103006b9b3e0SSimon J. Gerraty 	CandidateList_PrintAddrs(list);
1031956e45f6SSimon J. Gerraty #endif
1032956e45f6SSimon J. Gerraty }
1033956e45f6SSimon J. Gerraty 
103406b9b3e0SSimon J. Gerraty /*
103506b9b3e0SSimon J. Gerraty  * Add all candidates to the list that can be formed by applying a suffix to
103606b9b3e0SSimon J. Gerraty  * the candidate.
10373955d011SMarcel Moolenaar  */
1038956e45f6SSimon J. Gerraty static void
103906b9b3e0SSimon J. Gerraty CandidateList_AddCandidatesFor(CandidateList *list, Candidate *cand)
10403955d011SMarcel Moolenaar {
104106b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
104206b9b3e0SSimon J. Gerraty 	for (ln = cand->suff->children.first; ln != NULL; ln = ln->next) {
104306b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
104406b9b3e0SSimon J. Gerraty 
1045956e45f6SSimon J. Gerraty 		if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
10463955d011SMarcel Moolenaar 			/*
104706b9b3e0SSimon J. Gerraty 			 * If the suffix has been marked as the NULL suffix,
104806b9b3e0SSimon J. Gerraty 			 * also create a candidate for a file with no suffix
104906b9b3e0SSimon J. Gerraty 			 * attached.
10503955d011SMarcel Moolenaar 			 */
105106b9b3e0SSimon J. Gerraty 			CandidateList_Add(list, bmake_strdup(cand->prefix),
105206b9b3e0SSimon J. Gerraty 			    cand, suff, "1");
10533955d011SMarcel Moolenaar 		}
10543955d011SMarcel Moolenaar 
105506b9b3e0SSimon J. Gerraty 		CandidateList_Add(list, str_concat2(cand->prefix, suff->name),
105606b9b3e0SSimon J. Gerraty 		    cand, suff, "2");
1057956e45f6SSimon J. Gerraty 	}
10583955d011SMarcel Moolenaar }
10593955d011SMarcel Moolenaar 
106006b9b3e0SSimon J. Gerraty /*
106106b9b3e0SSimon J. Gerraty  * Free the first candidate in the list that is not referenced anymore.
106206b9b3e0SSimon J. Gerraty  * Return whether a candidate was removed.
106306b9b3e0SSimon J. Gerraty  */
10642c3632d1SSimon J. Gerraty static Boolean
106506b9b3e0SSimon J. Gerraty RemoveCandidate(CandidateList *srcs)
10663955d011SMarcel Moolenaar {
106706b9b3e0SSimon J. Gerraty 	CandidateListNode *ln;
10682c3632d1SSimon J. Gerraty 
10693955d011SMarcel Moolenaar #ifdef DEBUG_SRC
107006b9b3e0SSimon J. Gerraty 	debug_printf("cleaning list %p:", srcs);
107106b9b3e0SSimon J. Gerraty 	CandidateList_PrintAddrs(srcs);
10723955d011SMarcel Moolenaar #endif
10733955d011SMarcel Moolenaar 
107406b9b3e0SSimon J. Gerraty 	for (ln = srcs->first; ln != NULL; ln = ln->next) {
107506b9b3e0SSimon J. Gerraty 		Candidate *src = ln->datum;
1076956e45f6SSimon J. Gerraty 
1077e2eeea75SSimon J. Gerraty 		if (src->numChildren == 0) {
1078e2eeea75SSimon J. Gerraty 			if (src->parent == NULL)
107906b9b3e0SSimon J. Gerraty 				free(src->prefix);
10803955d011SMarcel Moolenaar 			else {
10813955d011SMarcel Moolenaar #ifdef DEBUG_SRC
108206b9b3e0SSimon J. Gerraty 				/* XXX: Lst_RemoveDatum */
108306b9b3e0SSimon J. Gerraty 				CandidateListNode *ln2;
108406b9b3e0SSimon J. Gerraty 				ln2 = Lst_FindDatum(&src->parent->childrenList,
108506b9b3e0SSimon J. Gerraty 				    src);
108695e3ed2cSSimon J. Gerraty 				if (ln2 != NULL)
108706b9b3e0SSimon J. Gerraty 					Lst_Remove(&src->parent->childrenList,
108806b9b3e0SSimon J. Gerraty 					    ln2);
10893955d011SMarcel Moolenaar #endif
1090e2eeea75SSimon J. Gerraty 				src->parent->numChildren--;
10913955d011SMarcel Moolenaar 			}
10923955d011SMarcel Moolenaar #ifdef DEBUG_SRC
109306b9b3e0SSimon J. Gerraty 			debug_printf("free: list %p src %p:%s children %d\n",
109406b9b3e0SSimon J. Gerraty 			    srcs, src, src->file, src->numChildren);
109506b9b3e0SSimon J. Gerraty 			Lst_Done(&src->childrenList);
10963955d011SMarcel Moolenaar #endif
109706b9b3e0SSimon J. Gerraty 			Lst_Remove(srcs, ln);
109806b9b3e0SSimon J. Gerraty 			free(src->file);
1099e2eeea75SSimon J. Gerraty 			free(src);
11003955d011SMarcel Moolenaar 			return TRUE;
11013955d011SMarcel Moolenaar 		}
11023955d011SMarcel Moolenaar #ifdef DEBUG_SRC
11033955d011SMarcel Moolenaar 		else {
110406b9b3e0SSimon J. Gerraty 			debug_printf("keep: list %p src %p:%s children %d:",
110506b9b3e0SSimon J. Gerraty 			    srcs, src, src->file, src->numChildren);
110606b9b3e0SSimon J. Gerraty 			CandidateList_PrintAddrs(&src->childrenList);
11073955d011SMarcel Moolenaar 		}
11083955d011SMarcel Moolenaar #endif
11093955d011SMarcel Moolenaar 	}
11103955d011SMarcel Moolenaar 
11112c3632d1SSimon J. Gerraty 	return FALSE;
11123955d011SMarcel Moolenaar }
11133955d011SMarcel Moolenaar 
1114e2eeea75SSimon J. Gerraty /* Find the first existing file/target in srcs. */
111506b9b3e0SSimon J. Gerraty static Candidate *
111606b9b3e0SSimon J. Gerraty FindThem(CandidateList *srcs, CandidateSearcher *cs)
11173955d011SMarcel Moolenaar {
111806b9b3e0SSimon J. Gerraty 	HashSet seen;
111906b9b3e0SSimon J. Gerraty 
112006b9b3e0SSimon J. Gerraty 	HashSet_Init(&seen);
11213955d011SMarcel Moolenaar 
11223955d011SMarcel Moolenaar 	while (!Lst_IsEmpty(srcs)) {
112306b9b3e0SSimon J. Gerraty 		Candidate *src = Lst_Dequeue(srcs);
11243955d011SMarcel Moolenaar 
112506b9b3e0SSimon J. Gerraty #ifdef DEBUG_SRC
112606b9b3e0SSimon J. Gerraty 		debug_printf("remove from list %p src %p:%s\n",
112706b9b3e0SSimon J. Gerraty 		    srcs, src, src->file);
112806b9b3e0SSimon J. Gerraty #endif
112906b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "\ttrying %s...", src->file);
11303955d011SMarcel Moolenaar 
11313955d011SMarcel Moolenaar 		/*
11323955d011SMarcel Moolenaar 		 * A file is considered to exist if either a node exists in the
11333955d011SMarcel Moolenaar 		 * graph for it or the file actually exists.
11343955d011SMarcel Moolenaar 		 */
1135956e45f6SSimon J. Gerraty 		if (Targ_FindNode(src->file) != NULL) {
113606b9b3e0SSimon J. Gerraty 		found:
113706b9b3e0SSimon J. Gerraty 			HashSet_Done(&seen);
113806b9b3e0SSimon J. Gerraty 			DEBUG0(SUFF, "got it\n");
113906b9b3e0SSimon J. Gerraty 			return src;
11403955d011SMarcel Moolenaar 		}
11413955d011SMarcel Moolenaar 
1142956e45f6SSimon J. Gerraty 		{
114306b9b3e0SSimon J. Gerraty 			char *file = Dir_FindFile(src->file,
114406b9b3e0SSimon J. Gerraty 			    src->suff->searchPath);
1145956e45f6SSimon J. Gerraty 			if (file != NULL) {
1146956e45f6SSimon J. Gerraty 				free(file);
114706b9b3e0SSimon J. Gerraty 				goto found;
11483955d011SMarcel Moolenaar 			}
1149956e45f6SSimon J. Gerraty 		}
11503955d011SMarcel Moolenaar 
115106b9b3e0SSimon J. Gerraty 		DEBUG0(SUFF, "not there\n");
11523955d011SMarcel Moolenaar 
115306b9b3e0SSimon J. Gerraty 		if (HashSet_Add(&seen, src->file))
115406b9b3e0SSimon J. Gerraty 			CandidateList_AddCandidatesFor(srcs, src);
115506b9b3e0SSimon J. Gerraty 		else {
115606b9b3e0SSimon J. Gerraty 			DEBUG1(SUFF, "FindThem: skipping duplicate \"%s\"\n",
115706b9b3e0SSimon J. Gerraty 			    src->file);
11583955d011SMarcel Moolenaar 		}
11593955d011SMarcel Moolenaar 
116006b9b3e0SSimon J. Gerraty 		CandidateSearcher_Add(cs, src);
11613955d011SMarcel Moolenaar 	}
11623955d011SMarcel Moolenaar 
116306b9b3e0SSimon J. Gerraty 	HashSet_Done(&seen);
116406b9b3e0SSimon J. Gerraty 	return NULL;
116506b9b3e0SSimon J. Gerraty }
116606b9b3e0SSimon J. Gerraty 
116706b9b3e0SSimon J. Gerraty /*
116806b9b3e0SSimon J. Gerraty  * See if any of the children of the candidate's GNode is one from which the
116906b9b3e0SSimon J. Gerraty  * target can be transformed. If there is one, a candidate is put together
117006b9b3e0SSimon J. Gerraty  * for it and returned.
11713955d011SMarcel Moolenaar  */
117206b9b3e0SSimon J. Gerraty static Candidate *
117306b9b3e0SSimon J. Gerraty FindCmds(Candidate *targ, CandidateSearcher *cs)
11743955d011SMarcel Moolenaar {
1175956e45f6SSimon J. Gerraty 	GNodeListNode *gln;
1176956e45f6SSimon J. Gerraty 	GNode *tgn;		/* Target GNode */
1177956e45f6SSimon J. Gerraty 	GNode *sgn;		/* Source GNode */
1178956e45f6SSimon J. Gerraty 	size_t prefLen;		/* The length of the defined prefix */
1179*dba7b0efSSimon J. Gerraty 	Suffix *suff;		/* Suffix of the matching candidate */
118006b9b3e0SSimon J. Gerraty 	Candidate *ret;		/* Return value */
11813955d011SMarcel Moolenaar 
1182956e45f6SSimon J. Gerraty 	tgn = targ->node;
118306b9b3e0SSimon J. Gerraty 	prefLen = strlen(targ->prefix);
11843955d011SMarcel Moolenaar 
118506b9b3e0SSimon J. Gerraty 	for (gln = tgn->children.first; gln != NULL; gln = gln->next) {
1186*dba7b0efSSimon J. Gerraty 		const char *base;
118706b9b3e0SSimon J. Gerraty 
1188956e45f6SSimon J. Gerraty 		sgn = gln->datum;
11893955d011SMarcel Moolenaar 
119006b9b3e0SSimon J. Gerraty 		if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(&tgn->commands)) {
11913955d011SMarcel Moolenaar 			/*
119206b9b3e0SSimon J. Gerraty 			 * We haven't looked to see if .OPTIONAL files exist
119306b9b3e0SSimon J. Gerraty 			 * yet, so don't use one as the implicit source.
119406b9b3e0SSimon J. Gerraty 			 * This allows us to use .OPTIONAL in .depend files so
119506b9b3e0SSimon J. Gerraty 			 * make won't complain "don't know how to make xxx.h"
119606b9b3e0SSimon J. Gerraty 			 * when a dependent file has been moved/deleted.
11973955d011SMarcel Moolenaar 			 */
11983955d011SMarcel Moolenaar 			continue;
11993955d011SMarcel Moolenaar 		}
12003955d011SMarcel Moolenaar 
1201*dba7b0efSSimon J. Gerraty 		base = str_basename(sgn->name);
1202*dba7b0efSSimon J. Gerraty 		if (strncmp(base, targ->prefix, prefLen) != 0)
12033955d011SMarcel Moolenaar 			continue;
120406b9b3e0SSimon J. Gerraty 		/* The node matches the prefix, see if it has a known suffix. */
1205*dba7b0efSSimon J. Gerraty 		suff = FindSuffixByName(base + prefLen);
1206956e45f6SSimon J. Gerraty 		if (suff == NULL)
12073955d011SMarcel Moolenaar 			continue;
1208956e45f6SSimon J. Gerraty 
12093955d011SMarcel Moolenaar 		/*
12103955d011SMarcel Moolenaar 		 * It even has a known suffix, see if there's a transformation
12113955d011SMarcel Moolenaar 		 * defined between the node's suffix and the target's suffix.
12123955d011SMarcel Moolenaar 		 *
12133955d011SMarcel Moolenaar 		 * XXX: Handle multi-stage transformations here, too.
12143955d011SMarcel Moolenaar 		 */
12153955d011SMarcel Moolenaar 
121606b9b3e0SSimon J. Gerraty 		if (Lst_FindDatum(&suff->parents, targ->suff) != NULL)
12173955d011SMarcel Moolenaar 			break;
12183955d011SMarcel Moolenaar 	}
12193955d011SMarcel Moolenaar 
1220956e45f6SSimon J. Gerraty 	if (gln == NULL)
1221956e45f6SSimon J. Gerraty 		return NULL;
1222956e45f6SSimon J. Gerraty 
122306b9b3e0SSimon J. Gerraty 	ret = Candidate_New(bmake_strdup(sgn->name), targ->prefix, suff, targ,
122406b9b3e0SSimon J. Gerraty 	    sgn);
1225e2eeea75SSimon J. Gerraty 	targ->numChildren++;
12263955d011SMarcel Moolenaar #ifdef DEBUG_SRC
122706b9b3e0SSimon J. Gerraty 	debug_printf("3 add targ %p:%s ret %p:%s\n",
122806b9b3e0SSimon J. Gerraty 	    targ, targ->file, ret, ret->file);
122906b9b3e0SSimon J. Gerraty 	Lst_Append(&targ->childrenList, ret);
12303955d011SMarcel Moolenaar #endif
123106b9b3e0SSimon J. Gerraty 	CandidateSearcher_Add(cs, ret);
123206b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "\tusing existing source %s\n", sgn->name);
12333841c287SSimon J. Gerraty 	return ret;
12343955d011SMarcel Moolenaar }
12353955d011SMarcel Moolenaar 
12363955d011SMarcel Moolenaar static void
123706b9b3e0SSimon J. Gerraty ExpandWildcards(GNodeListNode *cln, GNode *pgn)
12383955d011SMarcel Moolenaar {
1239956e45f6SSimon J. Gerraty 	GNode *cgn = cln->datum;
124006b9b3e0SSimon J. Gerraty 	StringList expansions;
12413955d011SMarcel Moolenaar 
124206b9b3e0SSimon J. Gerraty 	if (!Dir_HasWildcards(cgn->name))
12433955d011SMarcel Moolenaar 		return;
12443955d011SMarcel Moolenaar 
12453955d011SMarcel Moolenaar 	/*
124606b9b3e0SSimon J. Gerraty 	 * Expand the word along the chosen path
12473955d011SMarcel Moolenaar 	 */
124806b9b3e0SSimon J. Gerraty 	Lst_Init(&expansions);
1249*dba7b0efSSimon J. Gerraty 	SearchPath_Expand(Suff_FindPath(cgn), cgn->name, &expansions);
125006b9b3e0SSimon J. Gerraty 
125106b9b3e0SSimon J. Gerraty 	while (!Lst_IsEmpty(&expansions)) {
125206b9b3e0SSimon J. Gerraty 		GNode *gn;
125306b9b3e0SSimon J. Gerraty 		/*
125406b9b3e0SSimon J. Gerraty 		 * Fetch next expansion off the list and find its GNode
125506b9b3e0SSimon J. Gerraty 		 */
125606b9b3e0SSimon J. Gerraty 		char *cp = Lst_Dequeue(&expansions);
125706b9b3e0SSimon J. Gerraty 
125806b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "%s...", cp);
125906b9b3e0SSimon J. Gerraty 		gn = Targ_GetNode(cp);
126006b9b3e0SSimon J. Gerraty 
126106b9b3e0SSimon J. Gerraty 		/* Add gn to the parents child list before the original child */
126206b9b3e0SSimon J. Gerraty 		Lst_InsertBefore(&pgn->children, cln, gn);
126306b9b3e0SSimon J. Gerraty 		Lst_Append(&gn->parents, pgn);
126406b9b3e0SSimon J. Gerraty 		pgn->unmade++;
12653955d011SMarcel Moolenaar 	}
12663955d011SMarcel Moolenaar 
126706b9b3e0SSimon J. Gerraty 	Lst_Done(&expansions);
12683955d011SMarcel Moolenaar 
126906b9b3e0SSimon J. Gerraty 	DEBUG0(SUFF, "\n");
127006b9b3e0SSimon J. Gerraty 
127106b9b3e0SSimon J. Gerraty 	/*
127206b9b3e0SSimon J. Gerraty 	 * Now the source is expanded, remove it from the list of children to
127306b9b3e0SSimon J. Gerraty 	 * keep it from being processed.
127406b9b3e0SSimon J. Gerraty 	 */
127506b9b3e0SSimon J. Gerraty 	pgn->unmade--;
127606b9b3e0SSimon J. Gerraty 	Lst_Remove(&pgn->children, cln);
127706b9b3e0SSimon J. Gerraty 	Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
127806b9b3e0SSimon J. Gerraty }
127906b9b3e0SSimon J. Gerraty 
128006b9b3e0SSimon J. Gerraty /*
128106b9b3e0SSimon J. Gerraty  * Break the result into a vector of strings whose nodes we can find, then
128206b9b3e0SSimon J. Gerraty  * add those nodes to the members list.
128306b9b3e0SSimon J. Gerraty  *
128406b9b3e0SSimon J. Gerraty  * Unfortunately, we can't use Str_Words because it doesn't understand about
128506b9b3e0SSimon J. Gerraty  * variable specifications with spaces in them.
128606b9b3e0SSimon J. Gerraty  */
128706b9b3e0SSimon J. Gerraty static void
128806b9b3e0SSimon J. Gerraty ExpandChildrenRegular(char *cp, GNode *pgn, GNodeList *members)
12892c3632d1SSimon J. Gerraty {
12903955d011SMarcel Moolenaar 	char *start;
12913955d011SMarcel Moolenaar 
129206b9b3e0SSimon J. Gerraty 	pp_skip_hspace(&cp);
1293e2eeea75SSimon J. Gerraty 	start = cp;
1294956e45f6SSimon J. Gerraty 	while (*cp != '\0') {
12953955d011SMarcel Moolenaar 		if (*cp == ' ' || *cp == '\t') {
129606b9b3e0SSimon J. Gerraty 			GNode *gn;
12973955d011SMarcel Moolenaar 			/*
12983955d011SMarcel Moolenaar 			 * White-space -- terminate element, find the node,
12993955d011SMarcel Moolenaar 			 * add it, skip any further spaces.
13003955d011SMarcel Moolenaar 			 */
13013955d011SMarcel Moolenaar 			*cp++ = '\0';
1302956e45f6SSimon J. Gerraty 			gn = Targ_GetNode(start);
13032c3632d1SSimon J. Gerraty 			Lst_Append(members, gn);
1304e2eeea75SSimon J. Gerraty 			pp_skip_hspace(&cp);
130506b9b3e0SSimon J. Gerraty 			/* Continue at the next non-space. */
130606b9b3e0SSimon J. Gerraty 			start = cp;
13073955d011SMarcel Moolenaar 		} else if (*cp == '$') {
130806b9b3e0SSimon J. Gerraty 			/* Skip over the variable expression. */
1309956e45f6SSimon J. Gerraty 			const char *nested_p = cp;
131006b9b3e0SSimon J. Gerraty 			FStr junk;
13113955d011SMarcel Moolenaar 
131206b9b3e0SSimon J. Gerraty 			(void)Var_Parse(&nested_p, pgn, VARE_NONE, &junk);
1313956e45f6SSimon J. Gerraty 			/* TODO: handle errors */
131406b9b3e0SSimon J. Gerraty 			if (junk.str == var_Error) {
1315956e45f6SSimon J. Gerraty 				Parse_Error(PARSE_FATAL,
1316956e45f6SSimon J. Gerraty 				    "Malformed variable expression at \"%s\"",
1317956e45f6SSimon J. Gerraty 				    cp);
1318956e45f6SSimon J. Gerraty 				cp++;
1319956e45f6SSimon J. Gerraty 			} else {
1320956e45f6SSimon J. Gerraty 				cp += nested_p - cp;
13213955d011SMarcel Moolenaar 			}
13223955d011SMarcel Moolenaar 
132306b9b3e0SSimon J. Gerraty 			FStr_Done(&junk);
1324e2eeea75SSimon J. Gerraty 		} else if (cp[0] == '\\' && cp[1] != '\0') {
132506b9b3e0SSimon J. Gerraty 			/* Escaped something -- skip over it. */
13263955d011SMarcel Moolenaar 			/*
132706b9b3e0SSimon J. Gerraty 			 * XXX: In other places, escaping at this syntactical
132806b9b3e0SSimon J. Gerraty 			 * position is done by a '$', not a '\'.  The '\' is
132906b9b3e0SSimon J. Gerraty 			 * only used in variable modifiers.
13303955d011SMarcel Moolenaar 			 */
1331956e45f6SSimon J. Gerraty 			cp += 2;
1332956e45f6SSimon J. Gerraty 		} else {
13333955d011SMarcel Moolenaar 			cp++;
13343955d011SMarcel Moolenaar 		}
13353955d011SMarcel Moolenaar 	}
13363955d011SMarcel Moolenaar 
13373955d011SMarcel Moolenaar 	if (cp != start) {
13383955d011SMarcel Moolenaar 		/*
13393955d011SMarcel Moolenaar 		 * Stuff left over -- add it to the list too
13403955d011SMarcel Moolenaar 		 */
134106b9b3e0SSimon J. Gerraty 		GNode *gn = Targ_GetNode(start);
13422c3632d1SSimon J. Gerraty 		Lst_Append(members, gn);
13433955d011SMarcel Moolenaar 	}
134406b9b3e0SSimon J. Gerraty }
134506b9b3e0SSimon J. Gerraty 
13463955d011SMarcel Moolenaar /*
134706b9b3e0SSimon J. Gerraty  * Expand the names of any children of a given node that contain variable
134806b9b3e0SSimon J. Gerraty  * expressions or file wildcards into actual targets.
134906b9b3e0SSimon J. Gerraty  *
135006b9b3e0SSimon J. Gerraty  * The expanded node is removed from the parent's list of children, and the
135106b9b3e0SSimon J. Gerraty  * parent's unmade counter is decremented, but other nodes may be added.
135206b9b3e0SSimon J. Gerraty  *
135306b9b3e0SSimon J. Gerraty  * Input:
135406b9b3e0SSimon J. Gerraty  *	cln		Child to examine
135506b9b3e0SSimon J. Gerraty  *	pgn		Parent node being processed
13563955d011SMarcel Moolenaar  */
135706b9b3e0SSimon J. Gerraty static void
135806b9b3e0SSimon J. Gerraty ExpandChildren(GNodeListNode *cln, GNode *pgn)
135906b9b3e0SSimon J. Gerraty {
136006b9b3e0SSimon J. Gerraty 	GNode *cgn = cln->datum;
136106b9b3e0SSimon J. Gerraty 	char *cp;		/* Expanded value */
136206b9b3e0SSimon J. Gerraty 
136306b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&cgn->order_pred) || !Lst_IsEmpty(&cgn->order_succ))
136406b9b3e0SSimon J. Gerraty 		/* It is all too hard to process the result of .ORDER */
136506b9b3e0SSimon J. Gerraty 		return;
136606b9b3e0SSimon J. Gerraty 
136706b9b3e0SSimon J. Gerraty 	if (cgn->type & OP_WAIT)
136806b9b3e0SSimon J. Gerraty 		/* Ignore these (& OP_PHONY ?) */
136906b9b3e0SSimon J. Gerraty 		return;
137006b9b3e0SSimon J. Gerraty 
137106b9b3e0SSimon J. Gerraty 	/*
137206b9b3e0SSimon J. Gerraty 	 * First do variable expansion -- this takes precedence over wildcard
137306b9b3e0SSimon J. Gerraty 	 * expansion. If the result contains wildcards, they'll be gotten to
137406b9b3e0SSimon J. Gerraty 	 * later since the resulting words are tacked on to the end of the
137506b9b3e0SSimon J. Gerraty 	 * children list.
137606b9b3e0SSimon J. Gerraty 	 */
137706b9b3e0SSimon J. Gerraty 	if (strchr(cgn->name, '$') == NULL) {
137806b9b3e0SSimon J. Gerraty 		ExpandWildcards(cln, pgn);
137906b9b3e0SSimon J. Gerraty 		return;
138006b9b3e0SSimon J. Gerraty 	}
138106b9b3e0SSimon J. Gerraty 
138206b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "Expanding \"%s\"...", cgn->name);
138306b9b3e0SSimon J. Gerraty 	(void)Var_Subst(cgn->name, pgn, VARE_WANTRES | VARE_UNDEFERR, &cp);
138406b9b3e0SSimon J. Gerraty 	/* TODO: handle errors */
138506b9b3e0SSimon J. Gerraty 
138606b9b3e0SSimon J. Gerraty 	{
138706b9b3e0SSimon J. Gerraty 		GNodeList members = LST_INIT;
138806b9b3e0SSimon J. Gerraty 
138906b9b3e0SSimon J. Gerraty 		if (cgn->type & OP_ARCHV) {
139006b9b3e0SSimon J. Gerraty 			/*
1391*dba7b0efSSimon J. Gerraty 			 * Node was an 'archive(member)' target, so
139206b9b3e0SSimon J. Gerraty 			 * call on the Arch module to find the nodes for us,
1393*dba7b0efSSimon J. Gerraty 			 * expanding variables in the parent's scope.
139406b9b3e0SSimon J. Gerraty 			 */
139506b9b3e0SSimon J. Gerraty 			char *p = cp;
139606b9b3e0SSimon J. Gerraty 			(void)Arch_ParseArchive(&p, &members, pgn);
139706b9b3e0SSimon J. Gerraty 		} else {
139806b9b3e0SSimon J. Gerraty 			ExpandChildrenRegular(cp, pgn, &members);
13993955d011SMarcel Moolenaar 		}
14003955d011SMarcel Moolenaar 
14013955d011SMarcel Moolenaar 		/*
14023955d011SMarcel Moolenaar 		 * Add all elements of the members list to the parent node.
14033955d011SMarcel Moolenaar 		 */
140406b9b3e0SSimon J. Gerraty 		while (!Lst_IsEmpty(&members)) {
140506b9b3e0SSimon J. Gerraty 			GNode *gn = Lst_Dequeue(&members);
14063955d011SMarcel Moolenaar 
140706b9b3e0SSimon J. Gerraty 			DEBUG1(SUFF, "%s...", gn->name);
140806b9b3e0SSimon J. Gerraty 			/*
140906b9b3e0SSimon J. Gerraty 			 * Add gn to the parents child list before the
141006b9b3e0SSimon J. Gerraty 			 * original child.
141106b9b3e0SSimon J. Gerraty 			 */
141206b9b3e0SSimon J. Gerraty 			Lst_InsertBefore(&pgn->children, cln, gn);
141306b9b3e0SSimon J. Gerraty 			Lst_Append(&gn->parents, pgn);
14143955d011SMarcel Moolenaar 			pgn->unmade++;
14153955d011SMarcel Moolenaar 			/* Expand wildcards on new node */
141606b9b3e0SSimon J. Gerraty 			ExpandWildcards(cln->prev, pgn);
14173955d011SMarcel Moolenaar 		}
141806b9b3e0SSimon J. Gerraty 		Lst_Done(&members);
14193955d011SMarcel Moolenaar 
14203955d011SMarcel Moolenaar 		free(cp);
14213955d011SMarcel Moolenaar 	}
14222c3632d1SSimon J. Gerraty 
142306b9b3e0SSimon J. Gerraty 	DEBUG0(SUFF, "\n");
14243955d011SMarcel Moolenaar 
14253955d011SMarcel Moolenaar 	/*
14263955d011SMarcel Moolenaar 	 * Now the source is expanded, remove it from the list of children to
14273955d011SMarcel Moolenaar 	 * keep it from being processed.
14283955d011SMarcel Moolenaar 	 */
14293955d011SMarcel Moolenaar 	pgn->unmade--;
143006b9b3e0SSimon J. Gerraty 	Lst_Remove(&pgn->children, cln);
143106b9b3e0SSimon J. Gerraty 	Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
14323955d011SMarcel Moolenaar }
14333955d011SMarcel Moolenaar 
14343955d011SMarcel Moolenaar static void
143506b9b3e0SSimon J. Gerraty ExpandAllChildren(GNode *gn)
14363955d011SMarcel Moolenaar {
143706b9b3e0SSimon J. Gerraty 	GNodeListNode *ln, *nln;
14383955d011SMarcel Moolenaar 
143906b9b3e0SSimon J. Gerraty 	for (ln = gn->children.first; ln != NULL; ln = nln) {
144006b9b3e0SSimon J. Gerraty 		nln = ln->next;
144106b9b3e0SSimon J. Gerraty 		ExpandChildren(ln, gn);
144206b9b3e0SSimon J. Gerraty 	}
14433955d011SMarcel Moolenaar }
14443955d011SMarcel Moolenaar 
14453955d011SMarcel Moolenaar /*
144606b9b3e0SSimon J. Gerraty  * Find a path along which to expand the node.
14473955d011SMarcel Moolenaar  *
1448e2eeea75SSimon J. Gerraty  * If the node has a known suffix, use that path.
14493955d011SMarcel Moolenaar  * If it has no known suffix, use the default system search path.
14503955d011SMarcel Moolenaar  *
14513955d011SMarcel Moolenaar  * Input:
14523955d011SMarcel Moolenaar  *	gn		Node being examined
14533955d011SMarcel Moolenaar  *
14543955d011SMarcel Moolenaar  * Results:
14553955d011SMarcel Moolenaar  *	The appropriate path to search for the GNode.
14563955d011SMarcel Moolenaar  */
1457956e45f6SSimon J. Gerraty SearchPath *
14583955d011SMarcel Moolenaar Suff_FindPath(GNode *gn)
14593955d011SMarcel Moolenaar {
146006b9b3e0SSimon J. Gerraty 	Suffix *suff = gn->suffix;
14613955d011SMarcel Moolenaar 
14623955d011SMarcel Moolenaar 	if (suff == NULL) {
1463956e45f6SSimon J. Gerraty 		char *name = gn->name;
1464956e45f6SSimon J. Gerraty 		size_t nameLen = strlen(gn->name);
146506b9b3e0SSimon J. Gerraty 		SuffixListNode *ln;
146606b9b3e0SSimon J. Gerraty 		for (ln = sufflist.first; ln != NULL; ln = ln->next)
146706b9b3e0SSimon J. Gerraty 			if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen))
1468956e45f6SSimon J. Gerraty 				break;
14693955d011SMarcel Moolenaar 
147006b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "Wildcard expanding \"%s\"...", gn->name);
14713955d011SMarcel Moolenaar 		if (ln != NULL)
1472956e45f6SSimon J. Gerraty 			suff = ln->datum;
147306b9b3e0SSimon J. Gerraty 		/*
147406b9b3e0SSimon J. Gerraty 		 * XXX: Here we can save the suffix so we don't have to do
147506b9b3e0SSimon J. Gerraty 		 * this again.
147606b9b3e0SSimon J. Gerraty 		 */
14773955d011SMarcel Moolenaar 	}
14783955d011SMarcel Moolenaar 
14793955d011SMarcel Moolenaar 	if (suff != NULL) {
148006b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "suffix is \"%s\"...\n", suff->name);
14813955d011SMarcel Moolenaar 		return suff->searchPath;
14823955d011SMarcel Moolenaar 	} else {
148306b9b3e0SSimon J. Gerraty 		DEBUG0(SUFF, "\n");
148406b9b3e0SSimon J. Gerraty 		return &dirSearchPath;	/* Use default search path */
14853955d011SMarcel Moolenaar 	}
14863955d011SMarcel Moolenaar }
14873955d011SMarcel Moolenaar 
148806b9b3e0SSimon J. Gerraty /*
148906b9b3e0SSimon J. Gerraty  * Apply a transformation rule, given the source and target nodes and
14902c3632d1SSimon J. Gerraty  * suffixes.
14913955d011SMarcel Moolenaar  *
1492e2eeea75SSimon J. Gerraty  * The source and target are linked and the commands from the transformation
1493e2eeea75SSimon J. Gerraty  * are added to the target node's commands list. The target also inherits all
1494e2eeea75SSimon J. Gerraty  * the sources for the transformation rule.
14953955d011SMarcel Moolenaar  *
14963955d011SMarcel Moolenaar  * Results:
14973955d011SMarcel Moolenaar  *	TRUE if successful, FALSE if not.
14983955d011SMarcel Moolenaar  */
14993955d011SMarcel Moolenaar static Boolean
150006b9b3e0SSimon J. Gerraty ApplyTransform(GNode *tgn, GNode *sgn, Suffix *tsuff, Suffix *ssuff)
15013955d011SMarcel Moolenaar {
1502e2eeea75SSimon J. Gerraty 	GNodeListNode *ln;
15033955d011SMarcel Moolenaar 	char *tname;		/* Name of transformation rule */
150406b9b3e0SSimon J. Gerraty 	GNode *gn;		/* Node for the transformation rule */
15053955d011SMarcel Moolenaar 
150606b9b3e0SSimon J. Gerraty 	/* Form the proper links between the target and source. */
150706b9b3e0SSimon J. Gerraty 	Lst_Append(&tgn->children, sgn);
150806b9b3e0SSimon J. Gerraty 	Lst_Append(&sgn->parents, tgn);
1509e2eeea75SSimon J. Gerraty 	tgn->unmade++;
15103955d011SMarcel Moolenaar 
151106b9b3e0SSimon J. Gerraty 	/* Locate the transformation rule itself. */
1512e2eeea75SSimon J. Gerraty 	tname = str_concat2(ssuff->name, tsuff->name);
1513956e45f6SSimon J. Gerraty 	gn = FindTransformByName(tname);
15143955d011SMarcel Moolenaar 	free(tname);
15153955d011SMarcel Moolenaar 
1516e2eeea75SSimon J. Gerraty 	/* This can happen when linking an OP_MEMBER and OP_ARCHV node. */
151706b9b3e0SSimon J. Gerraty 	if (gn == NULL)
15183841c287SSimon J. Gerraty 		return FALSE;
15193955d011SMarcel Moolenaar 
1520e2eeea75SSimon J. Gerraty 	DEBUG3(SUFF, "\tapplying %s -> %s to \"%s\"\n",
1521e2eeea75SSimon J. Gerraty 	    ssuff->name, tsuff->name, tgn->name);
15223955d011SMarcel Moolenaar 
1523e2eeea75SSimon J. Gerraty 	/* Record last child; Make_HandleUse may add child nodes. */
152406b9b3e0SSimon J. Gerraty 	ln = tgn->children.last;
15253955d011SMarcel Moolenaar 
1526e2eeea75SSimon J. Gerraty 	/* Apply the rule. */
1527e2eeea75SSimon J. Gerraty 	Make_HandleUse(gn, tgn);
15283955d011SMarcel Moolenaar 
1529e2eeea75SSimon J. Gerraty 	/* Deal with wildcards and variables in any acquired sources. */
1530e2eeea75SSimon J. Gerraty 	ln = ln != NULL ? ln->next : NULL;
1531e2eeea75SSimon J. Gerraty 	while (ln != NULL) {
1532e2eeea75SSimon J. Gerraty 		GNodeListNode *nln = ln->next;
153306b9b3e0SSimon J. Gerraty 		ExpandChildren(ln, tgn);
1534e2eeea75SSimon J. Gerraty 		ln = nln;
15353955d011SMarcel Moolenaar 	}
15363955d011SMarcel Moolenaar 
15373955d011SMarcel Moolenaar 	/*
1538e2eeea75SSimon J. Gerraty 	 * Keep track of another parent to which this node is transformed so
15393955d011SMarcel Moolenaar 	 * the .IMPSRC variable can be set correctly for the parent.
15403955d011SMarcel Moolenaar 	 */
154106b9b3e0SSimon J. Gerraty 	Lst_Append(&sgn->implicitParents, tgn);
15423955d011SMarcel Moolenaar 
15433841c287SSimon J. Gerraty 	return TRUE;
15443955d011SMarcel Moolenaar }
15453955d011SMarcel Moolenaar 
154606b9b3e0SSimon J. Gerraty /*
154706b9b3e0SSimon J. Gerraty  * Member has a known suffix, so look for a transformation rule from
154806b9b3e0SSimon J. Gerraty  * it to a possible suffix of the archive.
154906b9b3e0SSimon J. Gerraty  *
155006b9b3e0SSimon J. Gerraty  * Rather than searching through the entire list, we just look at
155106b9b3e0SSimon J. Gerraty  * suffixes to which the member's suffix may be transformed.
155206b9b3e0SSimon J. Gerraty  */
155306b9b3e0SSimon J. Gerraty static void
155406b9b3e0SSimon J. Gerraty ExpandMember(GNode *gn, const char *eoarch, GNode *mem, Suffix *memSuff)
155506b9b3e0SSimon J. Gerraty {
155606b9b3e0SSimon J. Gerraty 	GNodeListNode *ln;
155706b9b3e0SSimon J. Gerraty 	size_t nameLen = (size_t)(eoarch - gn->name);
15583955d011SMarcel Moolenaar 
155906b9b3e0SSimon J. Gerraty 	/* Use first matching suffix... */
156006b9b3e0SSimon J. Gerraty 	for (ln = memSuff->parents.first; ln != NULL; ln = ln->next)
156106b9b3e0SSimon J. Gerraty 		if (Suffix_IsSuffix(ln->datum, nameLen, eoarch))
156206b9b3e0SSimon J. Gerraty 			break;
156306b9b3e0SSimon J. Gerraty 
156406b9b3e0SSimon J. Gerraty 	if (ln != NULL) {
156506b9b3e0SSimon J. Gerraty 		/* Got one -- apply it */
156606b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
156706b9b3e0SSimon J. Gerraty 		if (!ApplyTransform(gn, mem, suff, memSuff)) {
156806b9b3e0SSimon J. Gerraty 			DEBUG2(SUFF, "\tNo transformation from %s -> %s\n",
156906b9b3e0SSimon J. Gerraty 			    memSuff->name, suff->name);
157006b9b3e0SSimon J. Gerraty 		}
157106b9b3e0SSimon J. Gerraty 	}
157206b9b3e0SSimon J. Gerraty }
157306b9b3e0SSimon J. Gerraty 
157406b9b3e0SSimon J. Gerraty static void FindDeps(GNode *, CandidateSearcher *);
157506b9b3e0SSimon J. Gerraty 
157606b9b3e0SSimon J. Gerraty /*
157706b9b3e0SSimon J. Gerraty  * Locate dependencies for an OP_ARCHV node.
15783955d011SMarcel Moolenaar  *
15793955d011SMarcel Moolenaar  * Input:
15803955d011SMarcel Moolenaar  *	gn		Node for which to locate dependencies
15813955d011SMarcel Moolenaar  *
15823955d011SMarcel Moolenaar  * Side Effects:
15833955d011SMarcel Moolenaar  *	Same as Suff_FindDeps
15843955d011SMarcel Moolenaar  */
15853955d011SMarcel Moolenaar static void
158606b9b3e0SSimon J. Gerraty FindDepsArchive(GNode *gn, CandidateSearcher *cs)
15873955d011SMarcel Moolenaar {
15883955d011SMarcel Moolenaar 	char *eoarch;		/* End of archive portion */
15893955d011SMarcel Moolenaar 	char *eoname;		/* End of member portion */
15903955d011SMarcel Moolenaar 	GNode *mem;		/* Node for member */
159106b9b3e0SSimon J. Gerraty 	Suffix *memSuff;
159206b9b3e0SSimon J. Gerraty 	const char *name;	/* Start of member's name */
15933955d011SMarcel Moolenaar 
15943955d011SMarcel Moolenaar 	/*
15953955d011SMarcel Moolenaar 	 * The node is an archive(member) pair. so we must find a
15963955d011SMarcel Moolenaar 	 * suffix for both of them.
15973955d011SMarcel Moolenaar 	 */
15983955d011SMarcel Moolenaar 	eoarch = strchr(gn->name, '(');
15993955d011SMarcel Moolenaar 	eoname = strchr(eoarch, ')');
16003955d011SMarcel Moolenaar 
1601e1cee40dSSimon J. Gerraty 	/*
1602e1cee40dSSimon J. Gerraty 	 * Caller guarantees the format `libname(member)', via
1603e1cee40dSSimon J. Gerraty 	 * Arch_ParseArchive.
1604e1cee40dSSimon J. Gerraty 	 */
1605e1cee40dSSimon J. Gerraty 	assert(eoarch != NULL);
1606e1cee40dSSimon J. Gerraty 	assert(eoname != NULL);
1607e1cee40dSSimon J. Gerraty 
16083955d011SMarcel Moolenaar 	*eoname = '\0';		/* Nuke parentheses during suffix search */
16093955d011SMarcel Moolenaar 	*eoarch = '\0';		/* So a suffix can be found */
16103955d011SMarcel Moolenaar 
16113955d011SMarcel Moolenaar 	name = eoarch + 1;
16123955d011SMarcel Moolenaar 
16133955d011SMarcel Moolenaar 	/*
161406b9b3e0SSimon J. Gerraty 	 * To simplify things, call Suff_FindDeps recursively on the member
161506b9b3e0SSimon J. Gerraty 	 * now, so we can simply compare the member's .PREFIX and .TARGET
161606b9b3e0SSimon J. Gerraty 	 * variables to locate its suffix. This allows us to figure out the
161706b9b3e0SSimon J. Gerraty 	 * suffix to use for the archive without having to do a quadratic
161806b9b3e0SSimon J. Gerraty 	 * search over the suffix list, backtracking for each one.
16193955d011SMarcel Moolenaar 	 */
1620956e45f6SSimon J. Gerraty 	mem = Targ_GetNode(name);
162106b9b3e0SSimon J. Gerraty 	FindDeps(mem, cs);
16223955d011SMarcel Moolenaar 
162306b9b3e0SSimon J. Gerraty 	/* Create the link between the two nodes right off. */
162406b9b3e0SSimon J. Gerraty 	Lst_Append(&gn->children, mem);
162506b9b3e0SSimon J. Gerraty 	Lst_Append(&mem->parents, gn);
1626956e45f6SSimon J. Gerraty 	gn->unmade++;
16273955d011SMarcel Moolenaar 
162806b9b3e0SSimon J. Gerraty 	/* Copy in the variables from the member node to this one. */
1629*dba7b0efSSimon J. Gerraty 	Var_Set(gn, PREFIX, GNode_VarPrefix(mem));
1630*dba7b0efSSimon J. Gerraty 	Var_Set(gn, TARGET, GNode_VarTarget(mem));
16313955d011SMarcel Moolenaar 
163206b9b3e0SSimon J. Gerraty 	memSuff = mem->suffix;
163306b9b3e0SSimon J. Gerraty 	if (memSuff == NULL) {	/* Didn't know what it was. */
163406b9b3e0SSimon J. Gerraty 		DEBUG0(SUFF, "using null suffix\n");
163506b9b3e0SSimon J. Gerraty 		memSuff = nullSuff;
16363955d011SMarcel Moolenaar 	}
16373955d011SMarcel Moolenaar 
16383955d011SMarcel Moolenaar 
163906b9b3e0SSimon J. Gerraty 	/* Set the other two local variables required for this target. */
1640*dba7b0efSSimon J. Gerraty 	Var_Set(gn, MEMBER, name);
1641*dba7b0efSSimon J. Gerraty 	Var_Set(gn, ARCHIVE, gn->name);
164206b9b3e0SSimon J. Gerraty 	/* Set $@ for compatibility with other makes. */
1643*dba7b0efSSimon J. Gerraty 	Var_Set(gn, TARGET, gn->name);
16443bebe729SSimon J. Gerraty 
16453bebe729SSimon J. Gerraty 	/*
16463bebe729SSimon J. Gerraty 	 * Now we've got the important local variables set, expand any sources
16473bebe729SSimon J. Gerraty 	 * that still contain variables or wildcards in their names.
16483bebe729SSimon J. Gerraty 	 */
164906b9b3e0SSimon J. Gerraty 	ExpandAllChildren(gn);
16503bebe729SSimon J. Gerraty 
165106b9b3e0SSimon J. Gerraty 	if (memSuff != NULL)
165206b9b3e0SSimon J. Gerraty 		ExpandMember(gn, eoarch, mem, memSuff);
16533955d011SMarcel Moolenaar 
16543955d011SMarcel Moolenaar 	/*
165506b9b3e0SSimon J. Gerraty 	 * Replace the opening and closing parens now we've no need of the
165606b9b3e0SSimon J. Gerraty 	 * separate pieces.
16573955d011SMarcel Moolenaar 	 */
1658e2eeea75SSimon J. Gerraty 	*eoarch = '(';
1659e2eeea75SSimon J. Gerraty 	*eoname = ')';
16603955d011SMarcel Moolenaar 
16613955d011SMarcel Moolenaar 	/*
166206b9b3e0SSimon J. Gerraty 	 * Pretend gn appeared to the left of a dependency operator so the
166306b9b3e0SSimon J. Gerraty 	 * user needn't provide a transformation from the member to the
16643955d011SMarcel Moolenaar 	 * archive.
16653955d011SMarcel Moolenaar 	 */
1666e2eeea75SSimon J. Gerraty 	if (!GNode_IsTarget(gn))
16673955d011SMarcel Moolenaar 		gn->type |= OP_DEPENDS;
16683955d011SMarcel Moolenaar 
16693955d011SMarcel Moolenaar 	/*
16703955d011SMarcel Moolenaar 	 * Flag the member as such so we remember to look in the archive for
167106b9b3e0SSimon J. Gerraty 	 * its modification time. The OP_JOIN | OP_MADE is needed because
167206b9b3e0SSimon J. Gerraty 	 * this target should never get made.
16733955d011SMarcel Moolenaar 	 */
16743bebe729SSimon J. Gerraty 	mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
16753955d011SMarcel Moolenaar }
16763955d011SMarcel Moolenaar 
167706b9b3e0SSimon J. Gerraty /*
167806b9b3e0SSimon J. Gerraty  * If the node is a library, it is the arch module's job to find it
167906b9b3e0SSimon J. Gerraty  * and set the TARGET variable accordingly. We merely provide the
168006b9b3e0SSimon J. Gerraty  * search path, assuming all libraries end in ".a" (if the suffix
168106b9b3e0SSimon J. Gerraty  * hasn't been defined, there's nothing we can do for it, so we just
168206b9b3e0SSimon J. Gerraty  * set the TARGET variable to the node's name in order to give it a
168306b9b3e0SSimon J. Gerraty  * value).
168406b9b3e0SSimon J. Gerraty  */
1685956e45f6SSimon J. Gerraty static void
168606b9b3e0SSimon J. Gerraty FindDepsLib(GNode *gn)
1687956e45f6SSimon J. Gerraty {
168806b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(LIBSUFF);
168906b9b3e0SSimon J. Gerraty 	if (suff != NULL) {
169006b9b3e0SSimon J. Gerraty 		Suffix_Reassign(&gn->suffix, suff);
169106b9b3e0SSimon J. Gerraty 		Arch_FindLib(gn, suff->searchPath);
169206b9b3e0SSimon J. Gerraty 	} else {
169306b9b3e0SSimon J. Gerraty 		Suffix_Unassign(&gn->suffix);
1694*dba7b0efSSimon J. Gerraty 		Var_Set(gn, TARGET, gn->name);
169506b9b3e0SSimon J. Gerraty 	}
169606b9b3e0SSimon J. Gerraty 
169706b9b3e0SSimon J. Gerraty 	/*
169806b9b3e0SSimon J. Gerraty 	 * Because a library (-lfoo) target doesn't follow the standard
169906b9b3e0SSimon J. Gerraty 	 * filesystem conventions, we don't set the regular variables for
170006b9b3e0SSimon J. Gerraty 	 * the thing. .PREFIX is simply made empty.
170106b9b3e0SSimon J. Gerraty 	 */
1702*dba7b0efSSimon J. Gerraty 	Var_Set(gn, PREFIX, "");
170306b9b3e0SSimon J. Gerraty }
170406b9b3e0SSimon J. Gerraty 
170506b9b3e0SSimon J. Gerraty static void
170606b9b3e0SSimon J. Gerraty FindDepsRegularKnown(const char *name, size_t nameLen, GNode *gn,
170706b9b3e0SSimon J. Gerraty 		     CandidateList *srcs, CandidateList *targs)
170806b9b3e0SSimon J. Gerraty {
170906b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
171006b9b3e0SSimon J. Gerraty 	Candidate *targ;
1711956e45f6SSimon J. Gerraty 	char *pref;
1712956e45f6SSimon J. Gerraty 
171306b9b3e0SSimon J. Gerraty 	for (ln = sufflist.first; ln != NULL; ln = ln->next) {
171406b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
171506b9b3e0SSimon J. Gerraty 		if (!Suffix_IsSuffix(suff, nameLen, name + nameLen))
1716956e45f6SSimon J. Gerraty 			continue;
1717956e45f6SSimon J. Gerraty 
1718956e45f6SSimon J. Gerraty 		pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen));
171906b9b3e0SSimon J. Gerraty 		targ = Candidate_New(bmake_strdup(gn->name), pref, suff, NULL,
172006b9b3e0SSimon J. Gerraty 		    gn);
1721956e45f6SSimon J. Gerraty 
172206b9b3e0SSimon J. Gerraty 		CandidateList_AddCandidatesFor(srcs, targ);
1723956e45f6SSimon J. Gerraty 
172406b9b3e0SSimon J. Gerraty 		/* Record the target so we can nuke it. */
1725956e45f6SSimon J. Gerraty 		Lst_Append(targs, targ);
1726956e45f6SSimon J. Gerraty 	}
1727956e45f6SSimon J. Gerraty }
1728956e45f6SSimon J. Gerraty 
1729956e45f6SSimon J. Gerraty static void
173006b9b3e0SSimon J. Gerraty FindDepsRegularUnknown(GNode *gn, const char *sopref,
173106b9b3e0SSimon J. Gerraty 		       CandidateList *srcs, CandidateList *targs)
1732956e45f6SSimon J. Gerraty {
173306b9b3e0SSimon J. Gerraty 	Candidate *targ;
1734956e45f6SSimon J. Gerraty 
173506b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(targs) || nullSuff == NULL)
1736956e45f6SSimon J. Gerraty 		return;
1737956e45f6SSimon J. Gerraty 
173806b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
1739956e45f6SSimon J. Gerraty 
174006b9b3e0SSimon J. Gerraty 	targ = Candidate_New(bmake_strdup(gn->name), bmake_strdup(sopref),
174106b9b3e0SSimon J. Gerraty 	    nullSuff, NULL, gn);
1742956e45f6SSimon J. Gerraty 
1743956e45f6SSimon J. Gerraty 	/*
1744956e45f6SSimon J. Gerraty 	 * Only use the default suffix rules if we don't have commands
174506b9b3e0SSimon J. Gerraty 	 * defined for this gnode; traditional make programs used to not
174606b9b3e0SSimon J. Gerraty 	 * define suffix rules if the gnode had children but we don't do
174706b9b3e0SSimon J. Gerraty 	 * this anymore.
1748956e45f6SSimon J. Gerraty 	 */
174906b9b3e0SSimon J. Gerraty 	if (Lst_IsEmpty(&gn->commands))
175006b9b3e0SSimon J. Gerraty 		CandidateList_AddCandidatesFor(srcs, targ);
1751956e45f6SSimon J. Gerraty 	else {
175206b9b3e0SSimon J. Gerraty 		DEBUG0(SUFF, "not ");
1753956e45f6SSimon J. Gerraty 	}
1754956e45f6SSimon J. Gerraty 
175506b9b3e0SSimon J. Gerraty 	DEBUG0(SUFF, "adding suffix rules\n");
1756956e45f6SSimon J. Gerraty 
1757956e45f6SSimon J. Gerraty 	Lst_Append(targs, targ);
1758956e45f6SSimon J. Gerraty }
1759956e45f6SSimon J. Gerraty 
1760956e45f6SSimon J. Gerraty /*
176106b9b3e0SSimon J. Gerraty  * Deal with finding the thing on the default search path. We always do
176206b9b3e0SSimon J. Gerraty  * that, not only if the node is only a source (not on the lhs of a
176306b9b3e0SSimon J. Gerraty  * dependency operator or [XXX] it has neither children or commands) as
176406b9b3e0SSimon J. Gerraty  * the old pmake did.
1765956e45f6SSimon J. Gerraty  */
1766956e45f6SSimon J. Gerraty static void
176706b9b3e0SSimon J. Gerraty FindDepsRegularPath(GNode *gn, Candidate *targ)
1768956e45f6SSimon J. Gerraty {
1769956e45f6SSimon J. Gerraty 	if (gn->type & (OP_PHONY | OP_NOPATH))
1770956e45f6SSimon J. Gerraty 		return;
1771956e45f6SSimon J. Gerraty 
1772956e45f6SSimon J. Gerraty 	free(gn->path);
1773956e45f6SSimon J. Gerraty 	gn->path = Dir_FindFile(gn->name,
177406b9b3e0SSimon J. Gerraty 	    (targ == NULL ? &dirSearchPath :
1775956e45f6SSimon J. Gerraty 		targ->suff->searchPath));
1776956e45f6SSimon J. Gerraty 	if (gn->path == NULL)
1777956e45f6SSimon J. Gerraty 		return;
1778956e45f6SSimon J. Gerraty 
1779*dba7b0efSSimon J. Gerraty 	Var_Set(gn, TARGET, gn->path);
1780956e45f6SSimon J. Gerraty 
1781956e45f6SSimon J. Gerraty 	if (targ != NULL) {
1782956e45f6SSimon J. Gerraty 		/*
1783956e45f6SSimon J. Gerraty 		 * Suffix known for the thing -- trim the suffix off
1784956e45f6SSimon J. Gerraty 		 * the path to form the proper .PREFIX variable.
1785956e45f6SSimon J. Gerraty 		 */
1786956e45f6SSimon J. Gerraty 		size_t savep = strlen(gn->path) - targ->suff->nameLen;
1787956e45f6SSimon J. Gerraty 		char savec;
1788956e45f6SSimon J. Gerraty 
178906b9b3e0SSimon J. Gerraty 		Suffix_Reassign(&gn->suffix, targ->suff);
1790956e45f6SSimon J. Gerraty 
1791956e45f6SSimon J. Gerraty 		savec = gn->path[savep];
1792956e45f6SSimon J. Gerraty 		gn->path[savep] = '\0';
1793956e45f6SSimon J. Gerraty 
1794*dba7b0efSSimon J. Gerraty 		Var_Set(gn, PREFIX, str_basename(gn->path));
1795956e45f6SSimon J. Gerraty 
1796956e45f6SSimon J. Gerraty 		gn->path[savep] = savec;
1797956e45f6SSimon J. Gerraty 	} else {
179806b9b3e0SSimon J. Gerraty 		/*
179906b9b3e0SSimon J. Gerraty 		 * The .PREFIX gets the full path if the target has no
180006b9b3e0SSimon J. Gerraty 		 * known suffix.
180106b9b3e0SSimon J. Gerraty 		 */
180206b9b3e0SSimon J. Gerraty 		Suffix_Unassign(&gn->suffix);
1803*dba7b0efSSimon J. Gerraty 		Var_Set(gn, PREFIX, str_basename(gn->path));
1804956e45f6SSimon J. Gerraty 	}
1805956e45f6SSimon J. Gerraty }
1806956e45f6SSimon J. Gerraty 
180706b9b3e0SSimon J. Gerraty /*
180806b9b3e0SSimon J. Gerraty  * Locate implicit dependencies for regular targets.
18093955d011SMarcel Moolenaar  *
18103955d011SMarcel Moolenaar  * Input:
18113955d011SMarcel Moolenaar  *	gn		Node for which to find sources
18123955d011SMarcel Moolenaar  *
18133955d011SMarcel Moolenaar  * Side Effects:
18142c3632d1SSimon J. Gerraty  *	Same as Suff_FindDeps
18153955d011SMarcel Moolenaar  */
18163955d011SMarcel Moolenaar static void
181706b9b3e0SSimon J. Gerraty FindDepsRegular(GNode *gn, CandidateSearcher *cs)
18183955d011SMarcel Moolenaar {
181906b9b3e0SSimon J. Gerraty 	/* List of sources at which to look */
182006b9b3e0SSimon J. Gerraty 	CandidateList srcs = LST_INIT;
182106b9b3e0SSimon J. Gerraty 	/*
182206b9b3e0SSimon J. Gerraty 	 * List of targets to which things can be transformed.
182306b9b3e0SSimon J. Gerraty 	 * They all have the same file, but different suff and prefix fields.
182406b9b3e0SSimon J. Gerraty 	 */
182506b9b3e0SSimon J. Gerraty 	CandidateList targs = LST_INIT;
182606b9b3e0SSimon J. Gerraty 	Candidate *bottom;	/* Start of found transformation path */
182706b9b3e0SSimon J. Gerraty 	Candidate *src;
182806b9b3e0SSimon J. Gerraty 	Candidate *targ;
18293955d011SMarcel Moolenaar 
1830956e45f6SSimon J. Gerraty 	const char *name = gn->name;
1831956e45f6SSimon J. Gerraty 	size_t nameLen = strlen(name);
18323955d011SMarcel Moolenaar 
183306b9b3e0SSimon J. Gerraty #ifdef DEBUG_SRC
183406b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "FindDepsRegular \"%s\"\n", gn->name);
183506b9b3e0SSimon J. Gerraty #endif
18363955d011SMarcel Moolenaar 
18373955d011SMarcel Moolenaar 	/*
183806b9b3e0SSimon J. Gerraty 	 * We're caught in a catch-22 here. On the one hand, we want to use
183906b9b3e0SSimon J. Gerraty 	 * any transformation implied by the target's sources, but we can't
184006b9b3e0SSimon J. Gerraty 	 * examine the sources until we've expanded any variables/wildcards
184106b9b3e0SSimon J. Gerraty 	 * they may hold, and we can't do that until we've set up the
184206b9b3e0SSimon J. Gerraty 	 * target's local variables and we can't do that until we know what
184306b9b3e0SSimon J. Gerraty 	 * the proper suffix for the target is (in case there are two
184406b9b3e0SSimon J. Gerraty 	 * suffixes one of which is a suffix of the other) and we can't know
184506b9b3e0SSimon J. Gerraty 	 * that until we've found its implied source, which we may not want
184606b9b3e0SSimon J. Gerraty 	 * to use if there's an existing source that implies a different
184706b9b3e0SSimon J. Gerraty 	 * transformation.
18483955d011SMarcel Moolenaar 	 *
18493955d011SMarcel Moolenaar 	 * In an attempt to get around this, which may not work all the time,
185006b9b3e0SSimon J. Gerraty 	 * but should work most of the time, we look for implied sources
185106b9b3e0SSimon J. Gerraty 	 * first, checking transformations to all possible suffixes of the
185206b9b3e0SSimon J. Gerraty 	 * target, use what we find to set the target's local variables,
185306b9b3e0SSimon J. Gerraty 	 * expand the children, then look for any overriding transformations
185406b9b3e0SSimon J. Gerraty 	 * they imply. Should we find one, we discard the one we found before.
18553955d011SMarcel Moolenaar 	 */
18564fc82fe4SSimon J. Gerraty 	bottom = NULL;
18574fc82fe4SSimon J. Gerraty 	targ = NULL;
18584fc82fe4SSimon J. Gerraty 
18594fc82fe4SSimon J. Gerraty 	if (!(gn->type & OP_PHONY)) {
18603955d011SMarcel Moolenaar 
186106b9b3e0SSimon J. Gerraty 		FindDepsRegularKnown(name, nameLen, gn, &srcs, &targs);
18623955d011SMarcel Moolenaar 
1863956e45f6SSimon J. Gerraty 		/* Handle target of unknown suffix... */
186406b9b3e0SSimon J. Gerraty 		FindDepsRegularUnknown(gn, name, &srcs, &targs);
18653955d011SMarcel Moolenaar 
18663955d011SMarcel Moolenaar 		/*
186752d86256SSimon J. Gerraty 		 * Using the list of possible sources built up from the target
186806b9b3e0SSimon J. Gerraty 		 * suffix(es), try and find an existing file/target that
186906b9b3e0SSimon J. Gerraty 		 * matches.
18703955d011SMarcel Moolenaar 		 */
187106b9b3e0SSimon J. Gerraty 		bottom = FindThem(&srcs, cs);
18723955d011SMarcel Moolenaar 
18733955d011SMarcel Moolenaar 		if (bottom == NULL) {
18743955d011SMarcel Moolenaar 			/*
187506b9b3e0SSimon J. Gerraty 			 * No known transformations -- use the first suffix
187606b9b3e0SSimon J. Gerraty 			 * found for setting the local variables.
18773955d011SMarcel Moolenaar 			 */
187806b9b3e0SSimon J. Gerraty 			if (targs.first != NULL)
187906b9b3e0SSimon J. Gerraty 				targ = targs.first->datum;
1880e2eeea75SSimon J. Gerraty 			else
18813955d011SMarcel Moolenaar 				targ = NULL;
18823955d011SMarcel Moolenaar 		} else {
18833955d011SMarcel Moolenaar 			/*
188406b9b3e0SSimon J. Gerraty 			 * Work up the transformation path to find the suffix
188506b9b3e0SSimon J. Gerraty 			 * of the target to which the transformation was made.
18863955d011SMarcel Moolenaar 			 */
188706b9b3e0SSimon J. Gerraty 			for (targ = bottom;
188806b9b3e0SSimon J. Gerraty 			     targ->parent != NULL; targ = targ->parent)
18893955d011SMarcel Moolenaar 				continue;
18903955d011SMarcel Moolenaar 		}
18914fc82fe4SSimon J. Gerraty 	}
18923955d011SMarcel Moolenaar 
1893*dba7b0efSSimon J. Gerraty 	Var_Set(gn, TARGET, GNode_Path(gn));
1894*dba7b0efSSimon J. Gerraty 	Var_Set(gn, PREFIX, targ != NULL ? targ->prefix : gn->name);
18953955d011SMarcel Moolenaar 
18963955d011SMarcel Moolenaar 	/*
18973955d011SMarcel Moolenaar 	 * Now we've got the important local variables set, expand any sources
18983955d011SMarcel Moolenaar 	 * that still contain variables or wildcards in their names.
18993955d011SMarcel Moolenaar 	 */
1900956e45f6SSimon J. Gerraty 	{
190106b9b3e0SSimon J. Gerraty 		GNodeListNode *ln, *nln;
190206b9b3e0SSimon J. Gerraty 		for (ln = gn->children.first; ln != NULL; ln = nln) {
1903956e45f6SSimon J. Gerraty 			nln = ln->next;
190406b9b3e0SSimon J. Gerraty 			ExpandChildren(ln, gn);
19053955d011SMarcel Moolenaar 		}
1906956e45f6SSimon J. Gerraty 	}
19073955d011SMarcel Moolenaar 
19083955d011SMarcel Moolenaar 	if (targ == NULL) {
190906b9b3e0SSimon J. Gerraty 		DEBUG1(SUFF, "\tNo valid suffix on %s\n", gn->name);
19103955d011SMarcel Moolenaar 
19113955d011SMarcel Moolenaar 	sfnd_abort:
191206b9b3e0SSimon J. Gerraty 		FindDepsRegularPath(gn, targ);
19133955d011SMarcel Moolenaar 		goto sfnd_return;
19143955d011SMarcel Moolenaar 	}
19153955d011SMarcel Moolenaar 
19163955d011SMarcel Moolenaar 	/*
19173955d011SMarcel Moolenaar 	 * If the suffix indicates that the target is a library, mark that in
19183955d011SMarcel Moolenaar 	 * the node's type field.
19193955d011SMarcel Moolenaar 	 */
1920e2eeea75SSimon J. Gerraty 	if (targ->suff->flags & SUFF_LIBRARY)
19213955d011SMarcel Moolenaar 		gn->type |= OP_LIB;
19223955d011SMarcel Moolenaar 
19233955d011SMarcel Moolenaar 	/*
19243955d011SMarcel Moolenaar 	 * Check for overriding transformation rule implied by sources
19253955d011SMarcel Moolenaar 	 */
192606b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&gn->children)) {
192706b9b3e0SSimon J. Gerraty 		src = FindCmds(targ, cs);
19283955d011SMarcel Moolenaar 
19293955d011SMarcel Moolenaar 		if (src != NULL) {
19303955d011SMarcel Moolenaar 			/*
193106b9b3e0SSimon J. Gerraty 			 * Free up all the candidates in the transformation
193206b9b3e0SSimon J. Gerraty 			 * path, up to but not including the parent node.
19333955d011SMarcel Moolenaar 			 */
1934e2eeea75SSimon J. Gerraty 			while (bottom != NULL && bottom->parent != NULL) {
193506b9b3e0SSimon J. Gerraty 				CandidateSearcher_AddIfNew(cs, bottom);
19363955d011SMarcel Moolenaar 				bottom = bottom->parent;
19373955d011SMarcel Moolenaar 			}
19383955d011SMarcel Moolenaar 			bottom = src;
19393955d011SMarcel Moolenaar 		}
19403955d011SMarcel Moolenaar 	}
19413955d011SMarcel Moolenaar 
19423955d011SMarcel Moolenaar 	if (bottom == NULL) {
194306b9b3e0SSimon J. Gerraty 		/* No idea from where it can come -- return now. */
19443955d011SMarcel Moolenaar 		goto sfnd_abort;
19453955d011SMarcel Moolenaar 	}
19463955d011SMarcel Moolenaar 
19473955d011SMarcel Moolenaar 	/*
194806b9b3e0SSimon J. Gerraty 	 * We now have a list of candidates headed by 'bottom' and linked via
19493955d011SMarcel Moolenaar 	 * their 'parent' pointers. What we do next is create links between
19503955d011SMarcel Moolenaar 	 * source and target nodes (which may or may not have been created)
195106b9b3e0SSimon J. Gerraty 	 * and set the necessary local variables in each target.
195206b9b3e0SSimon J. Gerraty 	 *
195306b9b3e0SSimon J. Gerraty 	 * The commands for each target are set from the commands of the
19543955d011SMarcel Moolenaar 	 * transformation rule used to get from the src suffix to the targ
19553955d011SMarcel Moolenaar 	 * suffix. Note that this causes the commands list of the original
195606b9b3e0SSimon J. Gerraty 	 * node, gn, to be replaced with the commands of the final
195706b9b3e0SSimon J. Gerraty 	 * transformation rule.
19583955d011SMarcel Moolenaar 	 */
1959e2eeea75SSimon J. Gerraty 	if (bottom->node == NULL)
1960956e45f6SSimon J. Gerraty 		bottom->node = Targ_GetNode(bottom->file);
19613955d011SMarcel Moolenaar 
19623955d011SMarcel Moolenaar 	for (src = bottom; src->parent != NULL; src = src->parent) {
19633955d011SMarcel Moolenaar 		targ = src->parent;
19643955d011SMarcel Moolenaar 
196506b9b3e0SSimon J. Gerraty 		Suffix_Reassign(&src->node->suffix, src->suff);
19663955d011SMarcel Moolenaar 
1967e2eeea75SSimon J. Gerraty 		if (targ->node == NULL)
1968956e45f6SSimon J. Gerraty 			targ->node = Targ_GetNode(targ->file);
19693955d011SMarcel Moolenaar 
197006b9b3e0SSimon J. Gerraty 		ApplyTransform(targ->node, src->node,
19713955d011SMarcel Moolenaar 		    targ->suff, src->suff);
19723955d011SMarcel Moolenaar 
19733955d011SMarcel Moolenaar 		if (targ->node != gn) {
19743955d011SMarcel Moolenaar 			/*
197506b9b3e0SSimon J. Gerraty 			 * Finish off the dependency-search process for any
197606b9b3e0SSimon J. Gerraty 			 * nodes between bottom and gn (no point in questing
197706b9b3e0SSimon J. Gerraty 			 * around the filesystem for their implicit source
197806b9b3e0SSimon J. Gerraty 			 * when it's already known). Note that the node
197906b9b3e0SSimon J. Gerraty 			 * can't have any sources that need expanding, since
198006b9b3e0SSimon J. Gerraty 			 * SuffFindThem will stop on an existing node, so all
198106b9b3e0SSimon J. Gerraty 			 * we need to do is set the standard variables.
19823955d011SMarcel Moolenaar 			 */
19833955d011SMarcel Moolenaar 			targ->node->type |= OP_DEPS_FOUND;
1984*dba7b0efSSimon J. Gerraty 			Var_Set(targ->node, PREFIX, targ->prefix);
1985*dba7b0efSSimon J. Gerraty 			Var_Set(targ->node, TARGET, targ->node->name);
19863955d011SMarcel Moolenaar 		}
19873955d011SMarcel Moolenaar 	}
19883955d011SMarcel Moolenaar 
198906b9b3e0SSimon J. Gerraty 	Suffix_Reassign(&gn->suffix, src->suff);
19903955d011SMarcel Moolenaar 
19913955d011SMarcel Moolenaar 	/*
199206b9b3e0SSimon J. Gerraty 	 * Nuke the transformation path and the candidates left over in the
19933955d011SMarcel Moolenaar 	 * two lists.
19943955d011SMarcel Moolenaar 	 */
19953955d011SMarcel Moolenaar sfnd_return:
199606b9b3e0SSimon J. Gerraty 	if (bottom != NULL)
199706b9b3e0SSimon J. Gerraty 		CandidateSearcher_AddIfNew(cs, bottom);
19983955d011SMarcel Moolenaar 
199906b9b3e0SSimon J. Gerraty 	while (RemoveCandidate(&srcs) || RemoveCandidate(&targs))
20003955d011SMarcel Moolenaar 		continue;
20013955d011SMarcel Moolenaar 
200206b9b3e0SSimon J. Gerraty 	CandidateSearcher_MoveAll(cs, &srcs);
200306b9b3e0SSimon J. Gerraty 	CandidateSearcher_MoveAll(cs, &targs);
200406b9b3e0SSimon J. Gerraty }
200506b9b3e0SSimon J. Gerraty 
200606b9b3e0SSimon J. Gerraty static void
200706b9b3e0SSimon J. Gerraty CandidateSearcher_CleanUp(CandidateSearcher *cs)
200806b9b3e0SSimon J. Gerraty {
200906b9b3e0SSimon J. Gerraty 	while (RemoveCandidate(&cs->list))
201006b9b3e0SSimon J. Gerraty 		continue;
201106b9b3e0SSimon J. Gerraty 	assert(Lst_IsEmpty(&cs->list));
20123955d011SMarcel Moolenaar }
20133955d011SMarcel Moolenaar 
20143955d011SMarcel Moolenaar 
201506b9b3e0SSimon J. Gerraty /*
201606b9b3e0SSimon J. Gerraty  * Find implicit sources for the target.
20173955d011SMarcel Moolenaar  *
201806b9b3e0SSimon J. Gerraty  * Nodes are added to the graph as children of the passed-in node. The nodes
201906b9b3e0SSimon J. Gerraty  * are marked to have their IMPSRC variable filled in. The PREFIX variable
202006b9b3e0SSimon J. Gerraty  * is set for the given node and all its implied children.
20213955d011SMarcel Moolenaar  *
20222c3632d1SSimon J. Gerraty  * The path found by this target is the shortest path in the transformation
202306b9b3e0SSimon J. Gerraty  * graph, which may pass through nonexistent targets, to an existing target.
20242c3632d1SSimon J. Gerraty  * The search continues on all paths from the root suffix until a file is
20252c3632d1SSimon J. Gerraty  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
20262c3632d1SSimon J. Gerraty  * .l,v file exists but the .c and .l files don't, the search will branch out
20272c3632d1SSimon J. Gerraty  * in all directions from .o and again from all the nodes on the next level
20282c3632d1SSimon J. Gerraty  * until the .l,v node is encountered.
20293955d011SMarcel Moolenaar  */
20303955d011SMarcel Moolenaar void
20313955d011SMarcel Moolenaar Suff_FindDeps(GNode *gn)
20323955d011SMarcel Moolenaar {
203306b9b3e0SSimon J. Gerraty 	CandidateSearcher cs;
20343955d011SMarcel Moolenaar 
203506b9b3e0SSimon J. Gerraty 	CandidateSearcher_Init(&cs);
203606b9b3e0SSimon J. Gerraty 
203706b9b3e0SSimon J. Gerraty 	FindDeps(gn, &cs);
203806b9b3e0SSimon J. Gerraty 
203906b9b3e0SSimon J. Gerraty 	CandidateSearcher_CleanUp(&cs);
204006b9b3e0SSimon J. Gerraty 	CandidateSearcher_Done(&cs);
20413955d011SMarcel Moolenaar }
20423955d011SMarcel Moolenaar 
20433955d011SMarcel Moolenaar static void
204406b9b3e0SSimon J. Gerraty FindDeps(GNode *gn, CandidateSearcher *cs)
20453955d011SMarcel Moolenaar {
20462c3632d1SSimon J. Gerraty 	if (gn->type & OP_DEPS_FOUND)
20473955d011SMarcel Moolenaar 		return;
20483955d011SMarcel Moolenaar 	gn->type |= OP_DEPS_FOUND;
20492c3632d1SSimon J. Gerraty 
205006b9b3e0SSimon J. Gerraty 	/* Make sure we have these set, may get revised below. */
2051*dba7b0efSSimon J. Gerraty 	Var_Set(gn, TARGET, GNode_Path(gn));
2052*dba7b0efSSimon J. Gerraty 	Var_Set(gn, PREFIX, gn->name);
20534fc82fe4SSimon J. Gerraty 
205406b9b3e0SSimon J. Gerraty 	DEBUG1(SUFF, "SuffFindDeps \"%s\"\n", gn->name);
20553955d011SMarcel Moolenaar 
205606b9b3e0SSimon J. Gerraty 	if (gn->type & OP_ARCHV)
205706b9b3e0SSimon J. Gerraty 		FindDepsArchive(gn, cs);
205806b9b3e0SSimon J. Gerraty 	else if (gn->type & OP_LIB)
205906b9b3e0SSimon J. Gerraty 		FindDepsLib(gn);
206006b9b3e0SSimon J. Gerraty 	else
206106b9b3e0SSimon J. Gerraty 		FindDepsRegular(gn, cs);
20623955d011SMarcel Moolenaar }
20633955d011SMarcel Moolenaar 
206406b9b3e0SSimon J. Gerraty /*
206506b9b3e0SSimon J. Gerraty  * Define which suffix is the null suffix.
20662c3632d1SSimon J. Gerraty  *
20672c3632d1SSimon J. Gerraty  * Need to handle the changing of the null suffix gracefully so the old
20682c3632d1SSimon J. Gerraty  * transformation rules don't just go away.
20693955d011SMarcel Moolenaar  *
20703955d011SMarcel Moolenaar  * Input:
20713955d011SMarcel Moolenaar  *	name		Name of null suffix
20723955d011SMarcel Moolenaar  */
20733955d011SMarcel Moolenaar void
2074956e45f6SSimon J. Gerraty Suff_SetNull(const char *name)
20753955d011SMarcel Moolenaar {
207606b9b3e0SSimon J. Gerraty 	Suffix *suff = FindSuffixByName(name);
2077e2eeea75SSimon J. Gerraty 	if (suff == NULL) {
207806b9b3e0SSimon J. Gerraty 		Parse_Error(PARSE_WARNING,
207906b9b3e0SSimon J. Gerraty 		    "Desired null suffix %s not defined.",
2080956e45f6SSimon J. Gerraty 		    name);
2081956e45f6SSimon J. Gerraty 		return;
2082956e45f6SSimon J. Gerraty 	}
20833955d011SMarcel Moolenaar 
208406b9b3e0SSimon J. Gerraty 	if (nullSuff != NULL)
208506b9b3e0SSimon J. Gerraty 		nullSuff->flags &= ~(unsigned)SUFF_NULL;
2086e2eeea75SSimon J. Gerraty 	suff->flags |= SUFF_NULL;
208706b9b3e0SSimon J. Gerraty 	/* XXX: Here's where the transformation mangling would take place. */
208806b9b3e0SSimon J. Gerraty 	nullSuff = suff;
20893955d011SMarcel Moolenaar }
20903955d011SMarcel Moolenaar 
20912c3632d1SSimon J. Gerraty /* Initialize the suffixes module. */
20923955d011SMarcel Moolenaar void
20933955d011SMarcel Moolenaar Suff_Init(void)
20943955d011SMarcel Moolenaar {
20953955d011SMarcel Moolenaar 	/*
209606b9b3e0SSimon J. Gerraty 	 * Create null suffix for single-suffix rules (POSIX). The thing
209706b9b3e0SSimon J. Gerraty 	 * doesn't actually go on the suffix list or everyone will think
209806b9b3e0SSimon J. Gerraty 	 * that's its suffix.
20993955d011SMarcel Moolenaar 	 */
21006e050540SSimon J. Gerraty 	Suff_ClearSuffixes();
21013955d011SMarcel Moolenaar }
21023955d011SMarcel Moolenaar 
21033955d011SMarcel Moolenaar 
21042c3632d1SSimon J. Gerraty /* Clean up the suffixes module. */
21053955d011SMarcel Moolenaar void
21063955d011SMarcel Moolenaar Suff_End(void)
21073955d011SMarcel Moolenaar {
21083955d011SMarcel Moolenaar #ifdef CLEANUP
210906b9b3e0SSimon J. Gerraty 	Lst_DoneCall(&sufflist, SuffFree);
211006b9b3e0SSimon J. Gerraty 	Lst_DoneCall(&suffClean, SuffFree);
211106b9b3e0SSimon J. Gerraty 	if (nullSuff != NULL)
211206b9b3e0SSimon J. Gerraty 		SuffFree(nullSuff);
211306b9b3e0SSimon J. Gerraty 	Lst_Done(&transforms);
21143955d011SMarcel Moolenaar #endif
21153955d011SMarcel Moolenaar }
21163955d011SMarcel Moolenaar 
21173955d011SMarcel Moolenaar 
2118956e45f6SSimon J. Gerraty static void
211906b9b3e0SSimon J. Gerraty PrintSuffNames(const char *prefix, SuffixList *suffs)
21203955d011SMarcel Moolenaar {
212106b9b3e0SSimon J. Gerraty 	SuffixListNode *ln;
212295e3ed2cSSimon J. Gerraty 
2123956e45f6SSimon J. Gerraty 	debug_printf("#\t%s: ", prefix);
2124956e45f6SSimon J. Gerraty 	for (ln = suffs->first; ln != NULL; ln = ln->next) {
212506b9b3e0SSimon J. Gerraty 		Suffix *suff = ln->datum;
2126956e45f6SSimon J. Gerraty 		debug_printf("%s ", suff->name);
2127956e45f6SSimon J. Gerraty 	}
2128956e45f6SSimon J. Gerraty 	debug_printf("\n");
21293955d011SMarcel Moolenaar }
21303955d011SMarcel Moolenaar 
2131956e45f6SSimon J. Gerraty static void
213206b9b3e0SSimon J. Gerraty Suffix_Print(Suffix *suff)
21333955d011SMarcel Moolenaar {
2134e2eeea75SSimon J. Gerraty 	debug_printf("# \"%s\" (num %d, ref %d)",
2135e2eeea75SSimon J. Gerraty 	    suff->name, suff->sNum, suff->refCount);
2136e2eeea75SSimon J. Gerraty 	if (suff->flags != 0) {
213706b9b3e0SSimon J. Gerraty 		char flags_buf[SuffixFlags_ToStringSize];
21382c3632d1SSimon J. Gerraty 
2139956e45f6SSimon J. Gerraty 		debug_printf(" (%s)",
2140*dba7b0efSSimon J. Gerraty 		    SuffixFlags_ToString(flags_buf, suff->flags));
21413955d011SMarcel Moolenaar 	}
2142956e45f6SSimon J. Gerraty 	debug_printf("\n");
2143956e45f6SSimon J. Gerraty 
214406b9b3e0SSimon J. Gerraty 	PrintSuffNames("To", &suff->parents);
214506b9b3e0SSimon J. Gerraty 	PrintSuffNames("From", &suff->children);
2146956e45f6SSimon J. Gerraty 
2147956e45f6SSimon J. Gerraty 	debug_printf("#\tSearch Path: ");
214806b9b3e0SSimon J. Gerraty 	SearchPath_Print(suff->searchPath);
2149956e45f6SSimon J. Gerraty 	debug_printf("\n");
21503955d011SMarcel Moolenaar }
21513955d011SMarcel Moolenaar 
2152956e45f6SSimon J. Gerraty static void
2153956e45f6SSimon J. Gerraty PrintTransformation(GNode *t)
21543955d011SMarcel Moolenaar {
2155956e45f6SSimon J. Gerraty 	debug_printf("%-16s:", t->name);
21563955d011SMarcel Moolenaar 	Targ_PrintType(t->type);
2157956e45f6SSimon J. Gerraty 	debug_printf("\n");
2158956e45f6SSimon J. Gerraty 	Targ_PrintCmds(t);
2159956e45f6SSimon J. Gerraty 	debug_printf("\n");
21603955d011SMarcel Moolenaar }
21613955d011SMarcel Moolenaar 
21623955d011SMarcel Moolenaar void
21633955d011SMarcel Moolenaar Suff_PrintAll(void)
21643955d011SMarcel Moolenaar {
2165956e45f6SSimon J. Gerraty 	debug_printf("#*** Suffixes:\n");
2166956e45f6SSimon J. Gerraty 	{
216706b9b3e0SSimon J. Gerraty 		SuffixListNode *ln;
216806b9b3e0SSimon J. Gerraty 		for (ln = sufflist.first; ln != NULL; ln = ln->next)
216906b9b3e0SSimon J. Gerraty 			Suffix_Print(ln->datum);
2170956e45f6SSimon J. Gerraty 	}
21713955d011SMarcel Moolenaar 
2172956e45f6SSimon J. Gerraty 	debug_printf("#*** Transformations:\n");
2173956e45f6SSimon J. Gerraty 	{
2174956e45f6SSimon J. Gerraty 		GNodeListNode *ln;
217506b9b3e0SSimon J. Gerraty 		for (ln = transforms.first; ln != NULL; ln = ln->next)
2176956e45f6SSimon J. Gerraty 			PrintTransformation(ln->datum);
2177956e45f6SSimon J. Gerraty 	}
21783955d011SMarcel Moolenaar }
2179