xref: /minix3/usr.bin/make/suff.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $	*/
22e2caf59SThomas Veerman 
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman  * Copyright (c) 1988, 1989, 1990, 1993
52e2caf59SThomas Veerman  *	The Regents of the University of California.  All rights reserved.
62e2caf59SThomas Veerman  *
72e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
82e2caf59SThomas Veerman  * Adam de Boor.
92e2caf59SThomas Veerman  *
102e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
112e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
122e2caf59SThomas Veerman  * are met:
132e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
142e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
152e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
162e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
172e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
182e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
192e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
202e2caf59SThomas Veerman  *    without specific prior written permission.
212e2caf59SThomas Veerman  *
222e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322e2caf59SThomas Veerman  * SUCH DAMAGE.
332e2caf59SThomas Veerman  */
342e2caf59SThomas Veerman 
352e2caf59SThomas Veerman /*
362e2caf59SThomas Veerman  * Copyright (c) 1989 by Berkeley Softworks
372e2caf59SThomas Veerman  * All rights reserved.
382e2caf59SThomas Veerman  *
392e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
402e2caf59SThomas Veerman  * Adam de Boor.
412e2caf59SThomas Veerman  *
422e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
432e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
442e2caf59SThomas Veerman  * are met:
452e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
462e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
472e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
482e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
492e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
502e2caf59SThomas Veerman  * 3. All advertising materials mentioning features or use of this software
512e2caf59SThomas Veerman  *    must display the following acknowledgement:
522e2caf59SThomas Veerman  *	This product includes software developed by the University of
532e2caf59SThomas Veerman  *	California, Berkeley and its contributors.
542e2caf59SThomas Veerman  * 4. Neither the name of the University nor the names of its contributors
552e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
562e2caf59SThomas Veerman  *    without specific prior written permission.
572e2caf59SThomas Veerman  *
582e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
592e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
602e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
612e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
622e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
632e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
642e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
652e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
662e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
672e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
682e2caf59SThomas Veerman  * SUCH DAMAGE.
692e2caf59SThomas Veerman  */
702e2caf59SThomas Veerman 
712e2caf59SThomas Veerman #ifndef MAKE_NATIVE
72*0a6a1f1dSLionel Sambuc static char rcsid[] = "$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $";
732e2caf59SThomas Veerman #else
742e2caf59SThomas Veerman #include <sys/cdefs.h>
752e2caf59SThomas Veerman #ifndef lint
762e2caf59SThomas Veerman #if 0
772e2caf59SThomas Veerman static char sccsid[] = "@(#)suff.c	8.4 (Berkeley) 3/21/94";
782e2caf59SThomas Veerman #else
79*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $");
802e2caf59SThomas Veerman #endif
812e2caf59SThomas Veerman #endif /* not lint */
822e2caf59SThomas Veerman #endif
832e2caf59SThomas Veerman 
842e2caf59SThomas Veerman /*-
852e2caf59SThomas Veerman  * suff.c --
862e2caf59SThomas Veerman  *	Functions to maintain suffix lists and find implicit dependents
872e2caf59SThomas Veerman  *	using suffix transformation rules
882e2caf59SThomas Veerman  *
892e2caf59SThomas Veerman  * Interface:
902e2caf59SThomas Veerman  *	Suff_Init 	    	Initialize all things to do with suffixes.
912e2caf59SThomas Veerman  *
922e2caf59SThomas Veerman  *	Suff_End 	    	Cleanup the module
932e2caf59SThomas Veerman  *
942e2caf59SThomas Veerman  *	Suff_DoPaths	    	This function is used to make life easier
952e2caf59SThomas Veerman  *	    	  	    	when searching for a file according to its
962e2caf59SThomas Veerman  *	    	  	    	suffix. It takes the global search path,
972e2caf59SThomas Veerman  *	    	  	    	as defined using the .PATH: target, and appends
982e2caf59SThomas Veerman  *	    	  	    	its directories to the path of each of the
992e2caf59SThomas Veerman  *	    	  	    	defined suffixes, as specified using
1002e2caf59SThomas Veerman  *	    	  	    	.PATH<suffix>: targets. In addition, all
1012e2caf59SThomas Veerman  *	    	  	    	directories given for suffixes labeled as
1022e2caf59SThomas Veerman  *	    	  	    	include files or libraries, using the .INCLUDES
1032e2caf59SThomas Veerman  *	    	  	    	or .LIBS targets, are played with using
1042e2caf59SThomas Veerman  *	    	  	    	Dir_MakeFlags to create the .INCLUDES and
1052e2caf59SThomas Veerman  *	    	  	    	.LIBS global variables.
1062e2caf59SThomas Veerman  *
1072e2caf59SThomas Veerman  *	Suff_ClearSuffixes  	Clear out all the suffixes and defined
1082e2caf59SThomas Veerman  *	    	  	    	transformations.
1092e2caf59SThomas Veerman  *
1102e2caf59SThomas Veerman  *	Suff_IsTransform    	Return TRUE if the passed string is the lhs
1112e2caf59SThomas Veerman  *	    	  	    	of a transformation rule.
1122e2caf59SThomas Veerman  *
1132e2caf59SThomas Veerman  *	Suff_AddSuffix	    	Add the passed string as another known suffix.
1142e2caf59SThomas Veerman  *
1152e2caf59SThomas Veerman  *	Suff_GetPath	    	Return the search path for the given suffix.
1162e2caf59SThomas Veerman  *
1172e2caf59SThomas Veerman  *	Suff_AddInclude	    	Mark the given suffix as denoting an include
1182e2caf59SThomas Veerman  *	    	  	    	file.
1192e2caf59SThomas Veerman  *
1202e2caf59SThomas Veerman  *	Suff_AddLib	    	Mark the given suffix as denoting a library.
1212e2caf59SThomas Veerman  *
1222e2caf59SThomas Veerman  *	Suff_AddTransform   	Add another transformation to the suffix
1232e2caf59SThomas Veerman  *	    	  	    	graph. Returns  GNode suitable for framing, I
1242e2caf59SThomas Veerman  *	    	  	    	mean, tacking commands, attributes, etc. on.
1252e2caf59SThomas Veerman  *
1262e2caf59SThomas Veerman  *	Suff_SetNull	    	Define the suffix to consider the suffix of
1272e2caf59SThomas Veerman  *	    	  	    	any file that doesn't have a known one.
1282e2caf59SThomas Veerman  *
1292e2caf59SThomas Veerman  *	Suff_FindDeps	    	Find implicit sources for and the location of
1302e2caf59SThomas Veerman  *	    	  	    	a target based on its suffix. Returns the
1312e2caf59SThomas Veerman  *	    	  	    	bottom-most node added to the graph or NULL
1322e2caf59SThomas Veerman  *	    	  	    	if the target had no implicit sources.
1332e2caf59SThomas Veerman  *
1342e2caf59SThomas Veerman  *	Suff_FindPath	    	Return the appropriate path to search in
1352e2caf59SThomas Veerman  *				order to find the node.
1362e2caf59SThomas Veerman  */
1372e2caf59SThomas Veerman 
1382e2caf59SThomas Veerman #include    	  <stdio.h>
1392e2caf59SThomas Veerman #include	  "make.h"
1402e2caf59SThomas Veerman #include	  "hash.h"
1412e2caf59SThomas Veerman #include	  "dir.h"
1422e2caf59SThomas Veerman 
1432e2caf59SThomas Veerman static Lst       sufflist;	/* Lst of suffixes */
1442e2caf59SThomas Veerman #ifdef CLEANUP
1452e2caf59SThomas Veerman static Lst	 suffClean;	/* Lst of suffixes to be cleaned */
1462e2caf59SThomas Veerman #endif
1472e2caf59SThomas Veerman static Lst	 srclist;	/* Lst of sources */
1482e2caf59SThomas Veerman static Lst       transforms;	/* Lst of transformation rules */
1492e2caf59SThomas Veerman 
1502e2caf59SThomas Veerman static int        sNum = 0;	/* Counter for assigning suffix numbers */
1512e2caf59SThomas Veerman 
1522e2caf59SThomas Veerman /*
1532e2caf59SThomas Veerman  * Structure describing an individual suffix.
1542e2caf59SThomas Veerman  */
1552e2caf59SThomas Veerman typedef struct _Suff {
1562e2caf59SThomas Veerman     char         *name;	    	/* The suffix itself */
1572e2caf59SThomas Veerman     int		 nameLen;	/* Length of the suffix */
1582e2caf59SThomas Veerman     short	 flags;      	/* Type of suffix */
1592e2caf59SThomas Veerman #define SUFF_INCLUDE	  0x01	    /* One which is #include'd */
1602e2caf59SThomas Veerman #define SUFF_LIBRARY	  0x02	    /* One which contains a library */
1612e2caf59SThomas Veerman #define SUFF_NULL 	  0x04	    /* The empty suffix */
1622e2caf59SThomas Veerman     Lst    	 searchPath;	/* The path along which files of this suffix
1632e2caf59SThomas Veerman 				 * may be found */
1642e2caf59SThomas Veerman     int          sNum;	      	/* The suffix number */
1652e2caf59SThomas Veerman     int		 refCount;	/* Reference count of list membership */
1662e2caf59SThomas Veerman     Lst          parents;	/* Suffixes we have a transformation to */
1672e2caf59SThomas Veerman     Lst          children;	/* Suffixes we have a transformation from */
1682e2caf59SThomas Veerman     Lst		 ref;		/* List of lists this suffix is referenced */
1692e2caf59SThomas Veerman } Suff;
1702e2caf59SThomas Veerman 
1712e2caf59SThomas Veerman /*
1722e2caf59SThomas Veerman  * for SuffSuffIsSuffix
1732e2caf59SThomas Veerman  */
1742e2caf59SThomas Veerman typedef struct {
1752e2caf59SThomas Veerman     char	*ename;		/* The end of the name */
1762e2caf59SThomas Veerman     int		 len;		/* Length of the name */
1772e2caf59SThomas Veerman } SuffixCmpData;
1782e2caf59SThomas Veerman 
1792e2caf59SThomas Veerman /*
1802e2caf59SThomas Veerman  * Structure used in the search for implied sources.
1812e2caf59SThomas Veerman  */
1822e2caf59SThomas Veerman typedef struct _Src {
1832e2caf59SThomas Veerman     char            *file;	/* The file to look for */
1842e2caf59SThomas Veerman     char    	    *pref;  	/* Prefix from which file was formed */
1852e2caf59SThomas Veerman     Suff            *suff;	/* The suffix on the file */
1862e2caf59SThomas Veerman     struct _Src     *parent;	/* The Src for which this is a source */
1872e2caf59SThomas Veerman     GNode           *node;	/* The node describing the file */
1882e2caf59SThomas Veerman     int	    	    children;	/* Count of existing children (so we don't free
1892e2caf59SThomas Veerman 				 * this thing too early or never nuke it) */
1902e2caf59SThomas Veerman #ifdef DEBUG_SRC
1912e2caf59SThomas Veerman     Lst		    cp;		/* Debug; children list */
1922e2caf59SThomas Veerman #endif
1932e2caf59SThomas Veerman } Src;
1942e2caf59SThomas Veerman 
1952e2caf59SThomas Veerman /*
1962e2caf59SThomas Veerman  * A structure for passing more than one argument to the Lst-library-invoked
1972e2caf59SThomas Veerman  * function...
1982e2caf59SThomas Veerman  */
1992e2caf59SThomas Veerman typedef struct {
2002e2caf59SThomas Veerman     Lst            l;
2012e2caf59SThomas Veerman     Src            *s;
2022e2caf59SThomas Veerman } LstSrc;
2032e2caf59SThomas Veerman 
2042e2caf59SThomas Veerman typedef struct {
2052e2caf59SThomas Veerman     GNode	  **gn;
2062e2caf59SThomas Veerman     Suff	   *s;
2072e2caf59SThomas Veerman     Boolean	    r;
2082e2caf59SThomas Veerman } GNodeSuff;
2092e2caf59SThomas Veerman 
2102e2caf59SThomas Veerman static Suff 	    *suffNull;	/* The NULL suffix for this run */
2112e2caf59SThomas Veerman static Suff 	    *emptySuff;	/* The empty suffix required for POSIX
2122e2caf59SThomas Veerman 				 * single-suffix transformation rules */
2132e2caf59SThomas Veerman 
2142e2caf59SThomas Veerman 
2152e2caf59SThomas Veerman static const char *SuffStrIsPrefix(const char *, const char *);
2162e2caf59SThomas Veerman static char *SuffSuffIsSuffix(const Suff *, const SuffixCmpData *);
2172e2caf59SThomas Veerman static int SuffSuffIsSuffixP(const void *, const void *);
2182e2caf59SThomas Veerman static int SuffSuffHasNameP(const void *, const void *);
2192e2caf59SThomas Veerman static int SuffSuffIsPrefix(const void *, const void *);
2202e2caf59SThomas Veerman static int SuffGNHasNameP(const void *, const void *);
2212e2caf59SThomas Veerman static void SuffUnRef(void *, void *);
2222e2caf59SThomas Veerman static void SuffFree(void *);
2232e2caf59SThomas Veerman static void SuffInsert(Lst, Suff *);
2242e2caf59SThomas Veerman static void SuffRemove(Lst, Suff *);
2252e2caf59SThomas Veerman static Boolean SuffParseTransform(char *, Suff **, Suff **);
2262e2caf59SThomas Veerman static int SuffRebuildGraph(void *, void *);
2272e2caf59SThomas Veerman static int SuffScanTargets(void *, void *);
2282e2caf59SThomas Veerman static int SuffAddSrc(void *, void *);
2292e2caf59SThomas Veerman static int SuffRemoveSrc(Lst);
2302e2caf59SThomas Veerman static void SuffAddLevel(Lst, Src *);
2312e2caf59SThomas Veerman static Src *SuffFindThem(Lst, Lst);
2322e2caf59SThomas Veerman static Src *SuffFindCmds(Src *, Lst);
2332e2caf59SThomas Veerman static void SuffExpandChildren(LstNode, GNode *);
2342e2caf59SThomas Veerman static void SuffExpandWildcards(LstNode, GNode *);
2352e2caf59SThomas Veerman static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
2362e2caf59SThomas Veerman static void SuffFindDeps(GNode *, Lst);
2372e2caf59SThomas Veerman static void SuffFindArchiveDeps(GNode *, Lst);
2382e2caf59SThomas Veerman static void SuffFindNormalDeps(GNode *, Lst);
2392e2caf59SThomas Veerman static int SuffPrintName(void *, void *);
2402e2caf59SThomas Veerman static int SuffPrintSuff(void *, void *);
2412e2caf59SThomas Veerman static int SuffPrintTrans(void *, void *);
2422e2caf59SThomas Veerman 
2432e2caf59SThomas Veerman 	/*************** Lst Predicates ****************/
2442e2caf59SThomas Veerman /*-
2452e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2462e2caf59SThomas Veerman  * SuffStrIsPrefix  --
2472e2caf59SThomas Veerman  *	See if pref is a prefix of str.
2482e2caf59SThomas Veerman  *
2492e2caf59SThomas Veerman  * Input:
2502e2caf59SThomas Veerman  *	pref		possible prefix
2512e2caf59SThomas Veerman  *	str		string to check
2522e2caf59SThomas Veerman  *
2532e2caf59SThomas Veerman  * Results:
2542e2caf59SThomas Veerman  *	NULL if it ain't, pointer to character in str after prefix if so
2552e2caf59SThomas Veerman  *
2562e2caf59SThomas Veerman  * Side Effects:
2572e2caf59SThomas Veerman  *	None
2582e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2592e2caf59SThomas Veerman  */
2602e2caf59SThomas Veerman static const char *
SuffStrIsPrefix(const char * pref,const char * str)2612e2caf59SThomas Veerman SuffStrIsPrefix(const char *pref, const char *str)
2622e2caf59SThomas Veerman {
2632e2caf59SThomas Veerman     while (*str && *pref == *str) {
2642e2caf59SThomas Veerman 	pref++;
2652e2caf59SThomas Veerman 	str++;
2662e2caf59SThomas Veerman     }
2672e2caf59SThomas Veerman 
2682e2caf59SThomas Veerman     return (*pref ? NULL : str);
2692e2caf59SThomas Veerman }
2702e2caf59SThomas Veerman 
2712e2caf59SThomas Veerman /*-
2722e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2732e2caf59SThomas Veerman  * SuffSuffIsSuffix  --
2742e2caf59SThomas Veerman  *	See if suff is a suffix of str. sd->ename should point to THE END
2752e2caf59SThomas Veerman  *	of the string to check. (THE END == the null byte)
2762e2caf59SThomas Veerman  *
2772e2caf59SThomas Veerman  * Input:
2782e2caf59SThomas Veerman  *	s		possible suffix
2792e2caf59SThomas Veerman  *	sd		string to examine
2802e2caf59SThomas Veerman  *
2812e2caf59SThomas Veerman  * Results:
2822e2caf59SThomas Veerman  *	NULL if it ain't, pointer to character in str before suffix if
2832e2caf59SThomas Veerman  *	it is.
2842e2caf59SThomas Veerman  *
2852e2caf59SThomas Veerman  * Side Effects:
2862e2caf59SThomas Veerman  *	None
2872e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2882e2caf59SThomas Veerman  */
2892e2caf59SThomas Veerman static char *
SuffSuffIsSuffix(const Suff * s,const SuffixCmpData * sd)2902e2caf59SThomas Veerman SuffSuffIsSuffix(const Suff *s, const SuffixCmpData *sd)
2912e2caf59SThomas Veerman {
2922e2caf59SThomas Veerman     char  *p1;	    	/* Pointer into suffix name */
2932e2caf59SThomas Veerman     char  *p2;	    	/* Pointer into string being examined */
2942e2caf59SThomas Veerman 
2952e2caf59SThomas Veerman     if (sd->len < s->nameLen)
2962e2caf59SThomas Veerman 	return NULL;		/* this string is shorter than the suffix */
2972e2caf59SThomas Veerman 
2982e2caf59SThomas Veerman     p1 = s->name + s->nameLen;
2992e2caf59SThomas Veerman     p2 = sd->ename;
3002e2caf59SThomas Veerman 
3012e2caf59SThomas Veerman     while (p1 >= s->name && *p1 == *p2) {
3022e2caf59SThomas Veerman 	p1--;
3032e2caf59SThomas Veerman 	p2--;
3042e2caf59SThomas Veerman     }
3052e2caf59SThomas Veerman 
3062e2caf59SThomas Veerman     return (p1 == s->name - 1 ? p2 : NULL);
3072e2caf59SThomas Veerman }
3082e2caf59SThomas Veerman 
3092e2caf59SThomas Veerman /*-
3102e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3112e2caf59SThomas Veerman  * SuffSuffIsSuffixP --
3122e2caf59SThomas Veerman  *	Predicate form of SuffSuffIsSuffix. Passed as the callback function
3132e2caf59SThomas Veerman  *	to Lst_Find.
3142e2caf59SThomas Veerman  *
3152e2caf59SThomas Veerman  * Results:
3162e2caf59SThomas Veerman  *	0 if the suffix is the one desired, non-zero if not.
3172e2caf59SThomas Veerman  *
3182e2caf59SThomas Veerman  * Side Effects:
3192e2caf59SThomas Veerman  *	None.
3202e2caf59SThomas Veerman  *
3212e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3222e2caf59SThomas Veerman  */
3232e2caf59SThomas Veerman static int
SuffSuffIsSuffixP(const void * s,const void * sd)3242e2caf59SThomas Veerman SuffSuffIsSuffixP(const void *s, const void *sd)
3252e2caf59SThomas Veerman {
3262e2caf59SThomas Veerman     return(!SuffSuffIsSuffix(s, sd));
3272e2caf59SThomas Veerman }
3282e2caf59SThomas Veerman 
3292e2caf59SThomas Veerman /*-
3302e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3312e2caf59SThomas Veerman  * SuffSuffHasNameP --
3322e2caf59SThomas Veerman  *	Callback procedure for finding a suffix based on its name. Used by
3332e2caf59SThomas Veerman  *	Suff_GetPath.
3342e2caf59SThomas Veerman  *
3352e2caf59SThomas Veerman  * Input:
3362e2caf59SThomas Veerman  *	s		Suffix to check
3372e2caf59SThomas Veerman  *	sd		Desired name
3382e2caf59SThomas Veerman  *
3392e2caf59SThomas Veerman  * Results:
3402e2caf59SThomas Veerman  *	0 if the suffix is of the given name. non-zero otherwise.
3412e2caf59SThomas Veerman  *
3422e2caf59SThomas Veerman  * Side Effects:
3432e2caf59SThomas Veerman  *	None
3442e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3452e2caf59SThomas Veerman  */
3462e2caf59SThomas Veerman static int
SuffSuffHasNameP(const void * s,const void * sname)3472e2caf59SThomas Veerman SuffSuffHasNameP(const void *s, const void *sname)
3482e2caf59SThomas Veerman {
3492e2caf59SThomas Veerman     return (strcmp(sname, ((const Suff *)s)->name));
3502e2caf59SThomas Veerman }
3512e2caf59SThomas Veerman 
3522e2caf59SThomas Veerman /*-
3532e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3542e2caf59SThomas Veerman  * SuffSuffIsPrefix  --
3552e2caf59SThomas Veerman  *	See if the suffix described by s is a prefix of the string. Care
3562e2caf59SThomas Veerman  *	must be taken when using this to search for transformations and
3572e2caf59SThomas Veerman  *	what-not, since there could well be two suffixes, one of which
3582e2caf59SThomas Veerman  *	is a prefix of the other...
3592e2caf59SThomas Veerman  *
3602e2caf59SThomas Veerman  * Input:
3612e2caf59SThomas Veerman  *	s		suffix to compare
3622e2caf59SThomas Veerman  *	str		string to examine
3632e2caf59SThomas Veerman  *
3642e2caf59SThomas Veerman  * Results:
3652e2caf59SThomas Veerman  *	0 if s is a prefix of str. non-zero otherwise
3662e2caf59SThomas Veerman  *
3672e2caf59SThomas Veerman  * Side Effects:
3682e2caf59SThomas Veerman  *	None
3692e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3702e2caf59SThomas Veerman  */
3712e2caf59SThomas Veerman static int
SuffSuffIsPrefix(const void * s,const void * str)3722e2caf59SThomas Veerman SuffSuffIsPrefix(const void *s, const void *str)
3732e2caf59SThomas Veerman {
3742e2caf59SThomas Veerman     return SuffStrIsPrefix(((const Suff *)s)->name, str) == NULL;
3752e2caf59SThomas Veerman }
3762e2caf59SThomas Veerman 
3772e2caf59SThomas Veerman /*-
3782e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3792e2caf59SThomas Veerman  * SuffGNHasNameP  --
3802e2caf59SThomas Veerman  *	See if the graph node has the desired name
3812e2caf59SThomas Veerman  *
3822e2caf59SThomas Veerman  * Input:
3832e2caf59SThomas Veerman  *	gn		current node we're looking at
3842e2caf59SThomas Veerman  *	name		name we're looking for
3852e2caf59SThomas Veerman  *
3862e2caf59SThomas Veerman  * Results:
3872e2caf59SThomas Veerman  *	0 if it does. non-zero if it doesn't
3882e2caf59SThomas Veerman  *
3892e2caf59SThomas Veerman  * Side Effects:
3902e2caf59SThomas Veerman  *	None
3912e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3922e2caf59SThomas Veerman  */
3932e2caf59SThomas Veerman static int
SuffGNHasNameP(const void * gn,const void * name)3942e2caf59SThomas Veerman SuffGNHasNameP(const void *gn, const void *name)
3952e2caf59SThomas Veerman {
3962e2caf59SThomas Veerman     return (strcmp(name, ((const GNode *)gn)->name));
3972e2caf59SThomas Veerman }
3982e2caf59SThomas Veerman 
3992e2caf59SThomas Veerman  	    /*********** Maintenance Functions ************/
4002e2caf59SThomas Veerman 
4012e2caf59SThomas Veerman static void
SuffUnRef(void * lp,void * sp)4022e2caf59SThomas Veerman SuffUnRef(void *lp, void *sp)
4032e2caf59SThomas Veerman {
4042e2caf59SThomas Veerman     Lst l = (Lst) lp;
4052e2caf59SThomas Veerman 
4062e2caf59SThomas Veerman     LstNode ln = Lst_Member(l, sp);
4072e2caf59SThomas Veerman     if (ln != NULL) {
4082e2caf59SThomas Veerman 	Lst_Remove(l, ln);
4092e2caf59SThomas Veerman 	((Suff *)sp)->refCount--;
4102e2caf59SThomas Veerman     }
4112e2caf59SThomas Veerman }
4122e2caf59SThomas Veerman 
4132e2caf59SThomas Veerman /*-
4142e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4152e2caf59SThomas Veerman  * SuffFree  --
4162e2caf59SThomas Veerman  *	Free up all memory associated with the given suffix structure.
4172e2caf59SThomas Veerman  *
4182e2caf59SThomas Veerman  * Results:
4192e2caf59SThomas Veerman  *	none
4202e2caf59SThomas Veerman  *
4212e2caf59SThomas Veerman  * Side Effects:
4222e2caf59SThomas Veerman  *	the suffix entry is detroyed
4232e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4242e2caf59SThomas Veerman  */
4252e2caf59SThomas Veerman static void
SuffFree(void * sp)4262e2caf59SThomas Veerman SuffFree(void *sp)
4272e2caf59SThomas Veerman {
4282e2caf59SThomas Veerman     Suff           *s = (Suff *)sp;
4292e2caf59SThomas Veerman 
4302e2caf59SThomas Veerman     if (s == suffNull)
4312e2caf59SThomas Veerman 	suffNull = NULL;
4322e2caf59SThomas Veerman 
4332e2caf59SThomas Veerman     if (s == emptySuff)
4342e2caf59SThomas Veerman 	emptySuff = NULL;
4352e2caf59SThomas Veerman 
4362e2caf59SThomas Veerman #ifdef notdef
4372e2caf59SThomas Veerman     /* We don't delete suffixes in order, so we cannot use this */
4382e2caf59SThomas Veerman     if (s->refCount)
4392e2caf59SThomas Veerman 	Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
4402e2caf59SThomas Veerman 	    s->refCount);
4412e2caf59SThomas Veerman #endif
4422e2caf59SThomas Veerman 
4432e2caf59SThomas Veerman     Lst_Destroy(s->ref, NULL);
4442e2caf59SThomas Veerman     Lst_Destroy(s->children, NULL);
4452e2caf59SThomas Veerman     Lst_Destroy(s->parents, NULL);
4462e2caf59SThomas Veerman     Lst_Destroy(s->searchPath, Dir_Destroy);
4472e2caf59SThomas Veerman 
4482e2caf59SThomas Veerman     free(s->name);
4492e2caf59SThomas Veerman     free(s);
4502e2caf59SThomas Veerman }
4512e2caf59SThomas Veerman 
4522e2caf59SThomas Veerman /*-
4532e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4542e2caf59SThomas Veerman  * SuffRemove  --
4552e2caf59SThomas Veerman  *	Remove the suffix into the list
4562e2caf59SThomas Veerman  *
4572e2caf59SThomas Veerman  * Results:
4582e2caf59SThomas Veerman  *	None
4592e2caf59SThomas Veerman  *
4602e2caf59SThomas Veerman  * Side Effects:
4612e2caf59SThomas Veerman  *	The reference count for the suffix is decremented and the
4622e2caf59SThomas Veerman  *	suffix is possibly freed
4632e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4642e2caf59SThomas Veerman  */
4652e2caf59SThomas Veerman static void
SuffRemove(Lst l,Suff * s)4662e2caf59SThomas Veerman SuffRemove(Lst l, Suff *s)
4672e2caf59SThomas Veerman {
4682e2caf59SThomas Veerman     SuffUnRef(l, s);
4692e2caf59SThomas Veerman     if (s->refCount == 0) {
4702e2caf59SThomas Veerman 	SuffUnRef(sufflist, s);
4712e2caf59SThomas Veerman 	SuffFree(s);
4722e2caf59SThomas Veerman     }
4732e2caf59SThomas Veerman }
4742e2caf59SThomas Veerman 
4752e2caf59SThomas Veerman /*-
4762e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4772e2caf59SThomas Veerman  * SuffInsert  --
4782e2caf59SThomas Veerman  *	Insert the suffix into the list keeping the list ordered by suffix
4792e2caf59SThomas Veerman  *	numbers.
4802e2caf59SThomas Veerman  *
4812e2caf59SThomas Veerman  * Input:
4822e2caf59SThomas Veerman  *	l		the list where in s should be inserted
4832e2caf59SThomas Veerman  *	s		the suffix to insert
4842e2caf59SThomas Veerman  *
4852e2caf59SThomas Veerman  * Results:
4862e2caf59SThomas Veerman  *	None
4872e2caf59SThomas Veerman  *
4882e2caf59SThomas Veerman  * Side Effects:
4892e2caf59SThomas Veerman  *	The reference count of the suffix is incremented
4902e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4912e2caf59SThomas Veerman  */
4922e2caf59SThomas Veerman static void
SuffInsert(Lst l,Suff * s)4932e2caf59SThomas Veerman SuffInsert(Lst l, Suff *s)
4942e2caf59SThomas Veerman {
4952e2caf59SThomas Veerman     LstNode 	  ln;		/* current element in l we're examining */
4962e2caf59SThomas Veerman     Suff          *s2 = NULL;	/* the suffix descriptor in this element */
4972e2caf59SThomas Veerman 
4982e2caf59SThomas Veerman     if (Lst_Open(l) == FAILURE) {
4992e2caf59SThomas Veerman 	return;
5002e2caf59SThomas Veerman     }
5012e2caf59SThomas Veerman     while ((ln = Lst_Next(l)) != NULL) {
5022e2caf59SThomas Veerman 	s2 = (Suff *)Lst_Datum(ln);
5032e2caf59SThomas Veerman 	if (s2->sNum >= s->sNum) {
5042e2caf59SThomas Veerman 	    break;
5052e2caf59SThomas Veerman 	}
5062e2caf59SThomas Veerman     }
5072e2caf59SThomas Veerman 
5082e2caf59SThomas Veerman     Lst_Close(l);
5092e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
5102e2caf59SThomas Veerman 	fprintf(debug_file, "inserting %s(%d)...", s->name, s->sNum);
5112e2caf59SThomas Veerman     }
5122e2caf59SThomas Veerman     if (ln == NULL) {
5132e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
5142e2caf59SThomas Veerman 	    fprintf(debug_file, "at end of list\n");
5152e2caf59SThomas Veerman 	}
5162e2caf59SThomas Veerman 	(void)Lst_AtEnd(l, s);
5172e2caf59SThomas Veerman 	s->refCount++;
5182e2caf59SThomas Veerman 	(void)Lst_AtEnd(s->ref, l);
5192e2caf59SThomas Veerman     } else if (s2->sNum != s->sNum) {
5202e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
5212e2caf59SThomas Veerman 	    fprintf(debug_file, "before %s(%d)\n", s2->name, s2->sNum);
5222e2caf59SThomas Veerman 	}
5232e2caf59SThomas Veerman 	(void)Lst_InsertBefore(l, ln, s);
5242e2caf59SThomas Veerman 	s->refCount++;
5252e2caf59SThomas Veerman 	(void)Lst_AtEnd(s->ref, l);
5262e2caf59SThomas Veerman     } else if (DEBUG(SUFF)) {
5272e2caf59SThomas Veerman 	fprintf(debug_file, "already there\n");
5282e2caf59SThomas Veerman     }
5292e2caf59SThomas Veerman }
5302e2caf59SThomas Veerman 
5312e2caf59SThomas Veerman /*-
5322e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5332e2caf59SThomas Veerman  * Suff_ClearSuffixes --
5342e2caf59SThomas Veerman  *	This is gross. Nuke the list of suffixes but keep all transformation
5352e2caf59SThomas Veerman  *	rules around. The transformation graph is destroyed in this process,
5362e2caf59SThomas Veerman  *	but we leave the list of rules so when a new graph is formed the rules
5372e2caf59SThomas Veerman  *	will remain.
5382e2caf59SThomas Veerman  *	This function is called from the parse module when a
5392e2caf59SThomas Veerman  *	.SUFFIXES:\n line is encountered.
5402e2caf59SThomas Veerman  *
5412e2caf59SThomas Veerman  * Results:
5422e2caf59SThomas Veerman  *	none
5432e2caf59SThomas Veerman  *
5442e2caf59SThomas Veerman  * Side Effects:
5452e2caf59SThomas Veerman  *	the sufflist and its graph nodes are destroyed
5462e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5472e2caf59SThomas Veerman  */
5482e2caf59SThomas Veerman void
Suff_ClearSuffixes(void)5492e2caf59SThomas Veerman Suff_ClearSuffixes(void)
5502e2caf59SThomas Veerman {
5512e2caf59SThomas Veerman #ifdef CLEANUP
5522e2caf59SThomas Veerman     Lst_Concat(suffClean, sufflist, LST_CONCLINK);
5532e2caf59SThomas Veerman #endif
5542e2caf59SThomas Veerman     sufflist = Lst_Init(FALSE);
5552e2caf59SThomas Veerman     sNum = 0;
5562e2caf59SThomas Veerman     suffNull = emptySuff;
5572e2caf59SThomas Veerman }
5582e2caf59SThomas Veerman 
5592e2caf59SThomas Veerman /*-
5602e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5612e2caf59SThomas Veerman  * SuffParseTransform --
5622e2caf59SThomas Veerman  *	Parse a transformation string to find its two component suffixes.
5632e2caf59SThomas Veerman  *
5642e2caf59SThomas Veerman  * Input:
5652e2caf59SThomas Veerman  *	str		String being parsed
5662e2caf59SThomas Veerman  *	srcPtr		Place to store source of trans.
5672e2caf59SThomas Veerman  *	targPtr		Place to store target of trans.
5682e2caf59SThomas Veerman  *
5692e2caf59SThomas Veerman  * Results:
5702e2caf59SThomas Veerman  *	TRUE if the string is a valid transformation and FALSE otherwise.
5712e2caf59SThomas Veerman  *
5722e2caf59SThomas Veerman  * Side Effects:
5732e2caf59SThomas Veerman  *	The passed pointers are overwritten.
5742e2caf59SThomas Veerman  *
5752e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5762e2caf59SThomas Veerman  */
5772e2caf59SThomas Veerman static Boolean
SuffParseTransform(char * str,Suff ** srcPtr,Suff ** targPtr)5782e2caf59SThomas Veerman SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr)
5792e2caf59SThomas Veerman {
5802e2caf59SThomas Veerman     LstNode		srcLn;	    /* element in suffix list of trans source*/
5812e2caf59SThomas Veerman     Suff		*src;	    /* Source of transformation */
5822e2caf59SThomas Veerman     LstNode		targLn;	    /* element in suffix list of trans target*/
5832e2caf59SThomas Veerman     char		*str2;	    /* Extra pointer (maybe target suffix) */
5842e2caf59SThomas Veerman     LstNode 	    	singleLn;   /* element in suffix list of any suffix
5852e2caf59SThomas Veerman 				     * that exactly matches str */
5862e2caf59SThomas Veerman     Suff    	    	*single = NULL;/* Source of possible transformation to
5872e2caf59SThomas Veerman 				     * null suffix */
5882e2caf59SThomas Veerman 
5892e2caf59SThomas Veerman     srcLn = NULL;
5902e2caf59SThomas Veerman     singleLn = NULL;
5912e2caf59SThomas Veerman 
5922e2caf59SThomas Veerman     /*
5932e2caf59SThomas Veerman      * Loop looking first for a suffix that matches the start of the
5942e2caf59SThomas Veerman      * string and then for one that exactly matches the rest of it. If
5952e2caf59SThomas Veerman      * we can find two that meet these criteria, we've successfully
5962e2caf59SThomas Veerman      * parsed the string.
5972e2caf59SThomas Veerman      */
5982e2caf59SThomas Veerman     for (;;) {
5992e2caf59SThomas Veerman 	if (srcLn == NULL) {
6002e2caf59SThomas Veerman 	    srcLn = Lst_Find(sufflist, str, SuffSuffIsPrefix);
6012e2caf59SThomas Veerman 	} else {
6022e2caf59SThomas Veerman 	    srcLn = Lst_FindFrom(sufflist, Lst_Succ(srcLn), str,
6032e2caf59SThomas Veerman 				  SuffSuffIsPrefix);
6042e2caf59SThomas Veerman 	}
6052e2caf59SThomas Veerman 	if (srcLn == NULL) {
6062e2caf59SThomas Veerman 	    /*
6072e2caf59SThomas Veerman 	     * Ran out of source suffixes -- no such rule
6082e2caf59SThomas Veerman 	     */
6092e2caf59SThomas Veerman 	    if (singleLn != NULL) {
6102e2caf59SThomas Veerman 		/*
6112e2caf59SThomas Veerman 		 * Not so fast Mr. Smith! There was a suffix that encompassed
6122e2caf59SThomas Veerman 		 * the entire string, so we assume it was a transformation
6132e2caf59SThomas Veerman 		 * to the null suffix (thank you POSIX). We still prefer to
6142e2caf59SThomas Veerman 		 * find a double rule over a singleton, hence we leave this
6152e2caf59SThomas Veerman 		 * check until the end.
6162e2caf59SThomas Veerman 		 *
6172e2caf59SThomas Veerman 		 * XXX: Use emptySuff over suffNull?
6182e2caf59SThomas Veerman 		 */
6192e2caf59SThomas Veerman 		*srcPtr = single;
6202e2caf59SThomas Veerman 		*targPtr = suffNull;
6212e2caf59SThomas Veerman 		return(TRUE);
6222e2caf59SThomas Veerman 	    }
6232e2caf59SThomas Veerman 	    return (FALSE);
6242e2caf59SThomas Veerman 	}
6252e2caf59SThomas Veerman 	src = (Suff *)Lst_Datum(srcLn);
6262e2caf59SThomas Veerman 	str2 = str + src->nameLen;
6272e2caf59SThomas Veerman 	if (*str2 == '\0') {
6282e2caf59SThomas Veerman 	    single = src;
6292e2caf59SThomas Veerman 	    singleLn = srcLn;
6302e2caf59SThomas Veerman 	} else {
6312e2caf59SThomas Veerman 	    targLn = Lst_Find(sufflist, str2, SuffSuffHasNameP);
6322e2caf59SThomas Veerman 	    if (targLn != NULL) {
6332e2caf59SThomas Veerman 		*srcPtr = src;
6342e2caf59SThomas Veerman 		*targPtr = (Suff *)Lst_Datum(targLn);
6352e2caf59SThomas Veerman 		return (TRUE);
6362e2caf59SThomas Veerman 	    }
6372e2caf59SThomas Veerman 	}
6382e2caf59SThomas Veerman     }
6392e2caf59SThomas Veerman }
6402e2caf59SThomas Veerman 
6412e2caf59SThomas Veerman /*-
6422e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6432e2caf59SThomas Veerman  * Suff_IsTransform  --
6442e2caf59SThomas Veerman  *	Return TRUE if the given string is a transformation rule
6452e2caf59SThomas Veerman  *
6462e2caf59SThomas Veerman  *
6472e2caf59SThomas Veerman  * Input:
6482e2caf59SThomas Veerman  *	str		string to check
6492e2caf59SThomas Veerman  *
6502e2caf59SThomas Veerman  * Results:
6512e2caf59SThomas Veerman  *	TRUE if the string is a concatenation of two known suffixes.
6522e2caf59SThomas Veerman  *	FALSE otherwise
6532e2caf59SThomas Veerman  *
6542e2caf59SThomas Veerman  * Side Effects:
6552e2caf59SThomas Veerman  *	None
6562e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6572e2caf59SThomas Veerman  */
6582e2caf59SThomas Veerman Boolean
Suff_IsTransform(char * str)6592e2caf59SThomas Veerman Suff_IsTransform(char *str)
6602e2caf59SThomas Veerman {
6612e2caf59SThomas Veerman     Suff    	  *src, *targ;
6622e2caf59SThomas Veerman 
6632e2caf59SThomas Veerman     return (SuffParseTransform(str, &src, &targ));
6642e2caf59SThomas Veerman }
6652e2caf59SThomas Veerman 
6662e2caf59SThomas Veerman /*-
6672e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6682e2caf59SThomas Veerman  * Suff_AddTransform --
6692e2caf59SThomas Veerman  *	Add the transformation rule described by the line to the
6702e2caf59SThomas Veerman  *	list of rules and place the transformation itself in the graph
6712e2caf59SThomas Veerman  *
6722e2caf59SThomas Veerman  * Input:
6732e2caf59SThomas Veerman  *	line		name of transformation to add
6742e2caf59SThomas Veerman  *
6752e2caf59SThomas Veerman  * Results:
6762e2caf59SThomas Veerman  *	The node created for the transformation in the transforms list
6772e2caf59SThomas Veerman  *
6782e2caf59SThomas Veerman  * Side Effects:
6792e2caf59SThomas Veerman  *	The node is placed on the end of the transforms Lst and links are
6802e2caf59SThomas Veerman  *	made between the two suffixes mentioned in the target name
6812e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6822e2caf59SThomas Veerman  */
6832e2caf59SThomas Veerman GNode *
Suff_AddTransform(char * line)6842e2caf59SThomas Veerman Suff_AddTransform(char *line)
6852e2caf59SThomas Veerman {
6862e2caf59SThomas Veerman     GNode         *gn;		/* GNode of transformation rule */
68784d9c625SLionel Sambuc     Suff          *s,		/* source suffix */
68884d9c625SLionel Sambuc                   *t;		/* target suffix */
6892e2caf59SThomas Veerman     LstNode 	  ln;	    	/* Node for existing transformation */
6902e2caf59SThomas Veerman 
69161df9b64SLionel Sambuc #if defined(__minix)
69261df9b64SLionel Sambuc     /* Prevent complains from GCC at -O3 optimisation level. */
69361df9b64SLionel Sambuc     s = NULL;
69461df9b64SLionel Sambuc     t = NULL;
69561df9b64SLionel Sambuc #endif /* defined(__minix) */
69661df9b64SLionel Sambuc 
6972e2caf59SThomas Veerman     ln = Lst_Find(transforms, line, SuffGNHasNameP);
6982e2caf59SThomas Veerman     if (ln == NULL) {
6992e2caf59SThomas Veerman 	/*
7002e2caf59SThomas Veerman 	 * Make a new graph node for the transformation. It will be filled in
7012e2caf59SThomas Veerman 	 * by the Parse module.
7022e2caf59SThomas Veerman 	 */
7032e2caf59SThomas Veerman 	gn = Targ_NewGN(line);
7042e2caf59SThomas Veerman 	(void)Lst_AtEnd(transforms, gn);
7052e2caf59SThomas Veerman     } else {
7062e2caf59SThomas Veerman 	/*
7072e2caf59SThomas Veerman 	 * New specification for transformation rule. Just nuke the old list
7082e2caf59SThomas Veerman 	 * of commands so they can be filled in again... We don't actually
7092e2caf59SThomas Veerman 	 * free the commands themselves, because a given command can be
7102e2caf59SThomas Veerman 	 * attached to several different transformations.
7112e2caf59SThomas Veerman 	 */
7122e2caf59SThomas Veerman 	gn = (GNode *)Lst_Datum(ln);
7132e2caf59SThomas Veerman 	Lst_Destroy(gn->commands, NULL);
7142e2caf59SThomas Veerman 	Lst_Destroy(gn->children, NULL);
7152e2caf59SThomas Veerman 	gn->commands = Lst_Init(FALSE);
7162e2caf59SThomas Veerman 	gn->children = Lst_Init(FALSE);
7172e2caf59SThomas Veerman     }
7182e2caf59SThomas Veerman 
7192e2caf59SThomas Veerman     gn->type = OP_TRANSFORM;
7202e2caf59SThomas Veerman 
7212e2caf59SThomas Veerman     (void)SuffParseTransform(line, &s, &t);
7222e2caf59SThomas Veerman 
7232e2caf59SThomas Veerman     /*
7242e2caf59SThomas Veerman      * link the two together in the proper relationship and order
7252e2caf59SThomas Veerman      */
7262e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
7272e2caf59SThomas Veerman 	fprintf(debug_file, "defining transformation from `%s' to `%s'\n",
7282e2caf59SThomas Veerman 		s->name, t->name);
7292e2caf59SThomas Veerman     }
7302e2caf59SThomas Veerman     SuffInsert(t->children, s);
7312e2caf59SThomas Veerman     SuffInsert(s->parents, t);
7322e2caf59SThomas Veerman 
7332e2caf59SThomas Veerman     return (gn);
7342e2caf59SThomas Veerman }
7352e2caf59SThomas Veerman 
7362e2caf59SThomas Veerman /*-
7372e2caf59SThomas Veerman  *-----------------------------------------------------------------------
7382e2caf59SThomas Veerman  * Suff_EndTransform --
7392e2caf59SThomas Veerman  *	Handle the finish of a transformation definition, removing the
7402e2caf59SThomas Veerman  *	transformation from the graph if it has neither commands nor
7412e2caf59SThomas Veerman  *	sources. This is a callback procedure for the Parse module via
7422e2caf59SThomas Veerman  *	Lst_ForEach
7432e2caf59SThomas Veerman  *
7442e2caf59SThomas Veerman  * Input:
7452e2caf59SThomas Veerman  *	gnp		Node for transformation
7462e2caf59SThomas Veerman  *	dummy		Node for transformation
7472e2caf59SThomas Veerman  *
7482e2caf59SThomas Veerman  * Results:
7492e2caf59SThomas Veerman  *	=== 0
7502e2caf59SThomas Veerman  *
7512e2caf59SThomas Veerman  * Side Effects:
7522e2caf59SThomas Veerman  *	If the node has no commands or children, the children and parents
7532e2caf59SThomas Veerman  *	lists of the affected suffixes are altered.
7542e2caf59SThomas Veerman  *
7552e2caf59SThomas Veerman  *-----------------------------------------------------------------------
7562e2caf59SThomas Veerman  */
7572e2caf59SThomas Veerman int
Suff_EndTransform(void * gnp,void * dummy)7582e2caf59SThomas Veerman Suff_EndTransform(void *gnp, void *dummy)
7592e2caf59SThomas Veerman {
7602e2caf59SThomas Veerman     GNode *gn = (GNode *)gnp;
7612e2caf59SThomas Veerman 
7622e2caf59SThomas Veerman     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
7632e2caf59SThomas Veerman 	gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
7642e2caf59SThomas Veerman     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
7652e2caf59SThomas Veerman 	Lst_IsEmpty(gn->children))
7662e2caf59SThomas Veerman     {
7672e2caf59SThomas Veerman 	Suff	*s, *t;
7682e2caf59SThomas Veerman 
7692e2caf59SThomas Veerman 	/*
7702e2caf59SThomas Veerman 	 * SuffParseTransform() may fail for special rules which are not
7712e2caf59SThomas Veerman 	 * actual transformation rules. (e.g. .DEFAULT)
7722e2caf59SThomas Veerman 	 */
7732e2caf59SThomas Veerman 	if (SuffParseTransform(gn->name, &s, &t)) {
7742e2caf59SThomas Veerman 	    Lst	 p;
7752e2caf59SThomas Veerman 
7762e2caf59SThomas Veerman 	    if (DEBUG(SUFF)) {
7772e2caf59SThomas Veerman 		fprintf(debug_file, "deleting transformation from `%s' to `%s'\n",
7782e2caf59SThomas Veerman 		s->name, t->name);
7792e2caf59SThomas Veerman 	    }
7802e2caf59SThomas Veerman 
7812e2caf59SThomas Veerman 	    /*
7822e2caf59SThomas Veerman 	     * Store s->parents because s could be deleted in SuffRemove
7832e2caf59SThomas Veerman 	     */
7842e2caf59SThomas Veerman 	    p = s->parents;
7852e2caf59SThomas Veerman 
7862e2caf59SThomas Veerman 	    /*
7872e2caf59SThomas Veerman 	     * Remove the source from the target's children list. We check for a
7882e2caf59SThomas Veerman 	     * nil return to handle a beanhead saying something like
7892e2caf59SThomas Veerman 	     *  .c.o .c.o:
7902e2caf59SThomas Veerman 	     *
7912e2caf59SThomas Veerman 	     * We'll be called twice when the next target is seen, but .c and .o
7922e2caf59SThomas Veerman 	     * are only linked once...
7932e2caf59SThomas Veerman 	     */
7942e2caf59SThomas Veerman 	    SuffRemove(t->children, s);
7952e2caf59SThomas Veerman 
7962e2caf59SThomas Veerman 	    /*
7972e2caf59SThomas Veerman 	     * Remove the target from the source's parents list
7982e2caf59SThomas Veerman 	     */
7992e2caf59SThomas Veerman 	    SuffRemove(p, t);
8002e2caf59SThomas Veerman 	}
8012e2caf59SThomas Veerman     } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
8022e2caf59SThomas Veerman 	fprintf(debug_file, "transformation %s complete\n", gn->name);
8032e2caf59SThomas Veerman     }
8042e2caf59SThomas Veerman 
8052e2caf59SThomas Veerman     return(dummy ? 0 : 0);
8062e2caf59SThomas Veerman }
8072e2caf59SThomas Veerman 
8082e2caf59SThomas Veerman /*-
8092e2caf59SThomas Veerman  *-----------------------------------------------------------------------
8102e2caf59SThomas Veerman  * SuffRebuildGraph --
8112e2caf59SThomas Veerman  *	Called from Suff_AddSuffix via Lst_ForEach to search through the
8122e2caf59SThomas Veerman  *	list of existing transformation rules and rebuild the transformation
8132e2caf59SThomas Veerman  *	graph when it has been destroyed by Suff_ClearSuffixes. If the
8142e2caf59SThomas Veerman  *	given rule is a transformation involving this suffix and another,
8152e2caf59SThomas Veerman  *	existing suffix, the proper relationship is established between
8162e2caf59SThomas Veerman  *	the two.
8172e2caf59SThomas Veerman  *
8182e2caf59SThomas Veerman  * Input:
8192e2caf59SThomas Veerman  *	transformp	Transformation to test
8202e2caf59SThomas Veerman  *	sp		Suffix to rebuild
8212e2caf59SThomas Veerman  *
8222e2caf59SThomas Veerman  * Results:
8232e2caf59SThomas Veerman  *	Always 0.
8242e2caf59SThomas Veerman  *
8252e2caf59SThomas Veerman  * Side Effects:
8262e2caf59SThomas Veerman  *	The appropriate links will be made between this suffix and
8272e2caf59SThomas Veerman  *	others if transformation rules exist for it.
8282e2caf59SThomas Veerman  *
8292e2caf59SThomas Veerman  *-----------------------------------------------------------------------
8302e2caf59SThomas Veerman  */
8312e2caf59SThomas Veerman static int
SuffRebuildGraph(void * transformp,void * sp)8322e2caf59SThomas Veerman SuffRebuildGraph(void *transformp, void *sp)
8332e2caf59SThomas Veerman {
8342e2caf59SThomas Veerman     GNode   	*transform = (GNode *)transformp;
8352e2caf59SThomas Veerman     Suff    	*s = (Suff *)sp;
8362e2caf59SThomas Veerman     char 	*cp;
8372e2caf59SThomas Veerman     LstNode	ln;
8382e2caf59SThomas Veerman     Suff  	*s2;
8392e2caf59SThomas Veerman     SuffixCmpData sd;
8402e2caf59SThomas Veerman 
8412e2caf59SThomas Veerman     /*
8422e2caf59SThomas Veerman      * First see if it is a transformation from this suffix.
8432e2caf59SThomas Veerman      */
8442e2caf59SThomas Veerman     cp = UNCONST(SuffStrIsPrefix(s->name, transform->name));
8452e2caf59SThomas Veerman     if (cp != NULL) {
8462e2caf59SThomas Veerman 	ln = Lst_Find(sufflist, cp, SuffSuffHasNameP);
8472e2caf59SThomas Veerman 	if (ln != NULL) {
8482e2caf59SThomas Veerman 	    /*
8492e2caf59SThomas Veerman 	     * Found target. Link in and return, since it can't be anything
8502e2caf59SThomas Veerman 	     * else.
8512e2caf59SThomas Veerman 	     */
8522e2caf59SThomas Veerman 	    s2 = (Suff *)Lst_Datum(ln);
8532e2caf59SThomas Veerman 	    SuffInsert(s2->children, s);
8542e2caf59SThomas Veerman 	    SuffInsert(s->parents, s2);
8552e2caf59SThomas Veerman 	    return(0);
8562e2caf59SThomas Veerman 	}
8572e2caf59SThomas Veerman     }
8582e2caf59SThomas Veerman 
8592e2caf59SThomas Veerman     /*
8602e2caf59SThomas Veerman      * Not from, maybe to?
8612e2caf59SThomas Veerman      */
8622e2caf59SThomas Veerman     sd.len = strlen(transform->name);
8632e2caf59SThomas Veerman     sd.ename = transform->name + sd.len;
8642e2caf59SThomas Veerman     cp = SuffSuffIsSuffix(s, &sd);
8652e2caf59SThomas Veerman     if (cp != NULL) {
8662e2caf59SThomas Veerman 	/*
8672e2caf59SThomas Veerman 	 * Null-terminate the source suffix in order to find it.
8682e2caf59SThomas Veerman 	 */
8692e2caf59SThomas Veerman 	cp[1] = '\0';
8702e2caf59SThomas Veerman 	ln = Lst_Find(sufflist, transform->name, SuffSuffHasNameP);
8712e2caf59SThomas Veerman 	/*
8722e2caf59SThomas Veerman 	 * Replace the start of the target suffix
8732e2caf59SThomas Veerman 	 */
8742e2caf59SThomas Veerman 	cp[1] = s->name[0];
8752e2caf59SThomas Veerman 	if (ln != NULL) {
8762e2caf59SThomas Veerman 	    /*
8772e2caf59SThomas Veerman 	     * Found it -- establish the proper relationship
8782e2caf59SThomas Veerman 	     */
8792e2caf59SThomas Veerman 	    s2 = (Suff *)Lst_Datum(ln);
8802e2caf59SThomas Veerman 	    SuffInsert(s->children, s2);
8812e2caf59SThomas Veerman 	    SuffInsert(s2->parents, s);
8822e2caf59SThomas Veerman 	}
8832e2caf59SThomas Veerman     }
8842e2caf59SThomas Veerman     return(0);
8852e2caf59SThomas Veerman }
8862e2caf59SThomas Veerman 
8872e2caf59SThomas Veerman /*-
8882e2caf59SThomas Veerman  *-----------------------------------------------------------------------
8892e2caf59SThomas Veerman  * SuffScanTargets --
8902e2caf59SThomas Veerman  *	Called from Suff_AddSuffix via Lst_ForEach to search through the
8912e2caf59SThomas Veerman  *	list of existing targets and find if any of the existing targets
8922e2caf59SThomas Veerman  *	can be turned into a transformation rule.
8932e2caf59SThomas Veerman  *
8942e2caf59SThomas Veerman  * Results:
8952e2caf59SThomas Veerman  *	1 if a new main target has been selected, 0 otherwise.
8962e2caf59SThomas Veerman  *
8972e2caf59SThomas Veerman  * Side Effects:
8982e2caf59SThomas Veerman  *	If such a target is found and the target is the current main
8992e2caf59SThomas Veerman  *	target, the main target is set to NULL and the next target
9002e2caf59SThomas Veerman  *	examined (if that exists) becomes the main target.
9012e2caf59SThomas Veerman  *
9022e2caf59SThomas Veerman  *-----------------------------------------------------------------------
9032e2caf59SThomas Veerman  */
9042e2caf59SThomas Veerman static int
SuffScanTargets(void * targetp,void * gsp)9052e2caf59SThomas Veerman SuffScanTargets(void *targetp, void *gsp)
9062e2caf59SThomas Veerman {
9072e2caf59SThomas Veerman     GNode   	*target = (GNode *)targetp;
9082e2caf59SThomas Veerman     GNodeSuff	*gs = (GNodeSuff *)gsp;
9092e2caf59SThomas Veerman     Suff	*s, *t;
9102e2caf59SThomas Veerman     char 	*ptr;
9112e2caf59SThomas Veerman 
9122e2caf59SThomas Veerman     if (*gs->gn == NULL && gs->r && (target->type & OP_NOTARGET) == 0) {
9132e2caf59SThomas Veerman 	*gs->gn = target;
9142e2caf59SThomas Veerman 	Targ_SetMain(target);
9152e2caf59SThomas Veerman 	return 1;
9162e2caf59SThomas Veerman     }
9172e2caf59SThomas Veerman 
9182e2caf59SThomas Veerman     if ((unsigned int)target->type == OP_TRANSFORM)
9192e2caf59SThomas Veerman 	return 0;
9202e2caf59SThomas Veerman 
9212e2caf59SThomas Veerman     if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
9222e2caf59SThomas Veerman 	ptr == target->name)
9232e2caf59SThomas Veerman 	return 0;
9242e2caf59SThomas Veerman 
9252e2caf59SThomas Veerman     if (SuffParseTransform(target->name, &s, &t)) {
9262e2caf59SThomas Veerman 	if (*gs->gn == target) {
9272e2caf59SThomas Veerman 	    gs->r = TRUE;
9282e2caf59SThomas Veerman 	    *gs->gn = NULL;
9292e2caf59SThomas Veerman 	    Targ_SetMain(NULL);
9302e2caf59SThomas Veerman 	}
9312e2caf59SThomas Veerman 	Lst_Destroy(target->children, NULL);
9322e2caf59SThomas Veerman 	target->children = Lst_Init(FALSE);
9332e2caf59SThomas Veerman 	target->type = OP_TRANSFORM;
9342e2caf59SThomas Veerman 	/*
9352e2caf59SThomas Veerman 	 * link the two together in the proper relationship and order
9362e2caf59SThomas Veerman 	 */
9372e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
9382e2caf59SThomas Veerman 	    fprintf(debug_file, "defining transformation from `%s' to `%s'\n",
9392e2caf59SThomas Veerman 		s->name, t->name);
9402e2caf59SThomas Veerman 	}
9412e2caf59SThomas Veerman 	SuffInsert(t->children, s);
9422e2caf59SThomas Veerman 	SuffInsert(s->parents, t);
9432e2caf59SThomas Veerman     }
9442e2caf59SThomas Veerman     return 0;
9452e2caf59SThomas Veerman }
9462e2caf59SThomas Veerman 
9472e2caf59SThomas Veerman /*-
9482e2caf59SThomas Veerman  *-----------------------------------------------------------------------
9492e2caf59SThomas Veerman  * Suff_AddSuffix --
9502e2caf59SThomas Veerman  *	Add the suffix in string to the end of the list of known suffixes.
9512e2caf59SThomas Veerman  *	Should we restructure the suffix graph? Make doesn't...
9522e2caf59SThomas Veerman  *
9532e2caf59SThomas Veerman  * Input:
9542e2caf59SThomas Veerman  *	str		the name of the suffix to add
9552e2caf59SThomas Veerman  *
9562e2caf59SThomas Veerman  * Results:
9572e2caf59SThomas Veerman  *	None
9582e2caf59SThomas Veerman  *
9592e2caf59SThomas Veerman  * Side Effects:
9602e2caf59SThomas Veerman  *	A GNode is created for the suffix and a Suff structure is created and
9612e2caf59SThomas Veerman  *	added to the suffixes list unless the suffix was already known.
9622e2caf59SThomas Veerman  *	The mainNode passed can be modified if a target mutated into a
9632e2caf59SThomas Veerman  *	transform and that target happened to be the main target.
9642e2caf59SThomas Veerman  *-----------------------------------------------------------------------
9652e2caf59SThomas Veerman  */
9662e2caf59SThomas Veerman void
Suff_AddSuffix(char * str,GNode ** gn)9672e2caf59SThomas Veerman Suff_AddSuffix(char *str, GNode **gn)
9682e2caf59SThomas Veerman {
9692e2caf59SThomas Veerman     Suff          *s;	    /* new suffix descriptor */
9702e2caf59SThomas Veerman     LstNode 	  ln;
9712e2caf59SThomas Veerman     GNodeSuff	  gs;
9722e2caf59SThomas Veerman 
9732e2caf59SThomas Veerman     ln = Lst_Find(sufflist, str, SuffSuffHasNameP);
9742e2caf59SThomas Veerman     if (ln == NULL) {
9752e2caf59SThomas Veerman 	s = bmake_malloc(sizeof(Suff));
9762e2caf59SThomas Veerman 
9772e2caf59SThomas Veerman 	s->name =   	bmake_strdup(str);
9782e2caf59SThomas Veerman 	s->nameLen = 	strlen(s->name);
9792e2caf59SThomas Veerman 	s->searchPath = Lst_Init(FALSE);
9802e2caf59SThomas Veerman 	s->children = 	Lst_Init(FALSE);
9812e2caf59SThomas Veerman 	s->parents = 	Lst_Init(FALSE);
9822e2caf59SThomas Veerman 	s->ref = 	Lst_Init(FALSE);
9832e2caf59SThomas Veerman 	s->sNum =   	sNum++;
9842e2caf59SThomas Veerman 	s->flags =  	0;
9852e2caf59SThomas Veerman 	s->refCount =	1;
9862e2caf59SThomas Veerman 
9872e2caf59SThomas Veerman 	(void)Lst_AtEnd(sufflist, s);
9882e2caf59SThomas Veerman 	/*
9892e2caf59SThomas Veerman 	 * We also look at our existing targets list to see if adding
9902e2caf59SThomas Veerman 	 * this suffix will make one of our current targets mutate into
9912e2caf59SThomas Veerman 	 * a suffix rule. This is ugly, but other makes treat all targets
9922e2caf59SThomas Veerman 	 * that start with a . as suffix rules.
9932e2caf59SThomas Veerman 	 */
9942e2caf59SThomas Veerman 	gs.gn = gn;
9952e2caf59SThomas Veerman 	gs.s  = s;
9962e2caf59SThomas Veerman 	gs.r  = FALSE;
9972e2caf59SThomas Veerman 	Lst_ForEach(Targ_List(), SuffScanTargets, &gs);
9982e2caf59SThomas Veerman 	/*
9992e2caf59SThomas Veerman 	 * Look for any existing transformations from or to this suffix.
10002e2caf59SThomas Veerman 	 * XXX: Only do this after a Suff_ClearSuffixes?
10012e2caf59SThomas Veerman 	 */
10022e2caf59SThomas Veerman 	Lst_ForEach(transforms, SuffRebuildGraph, s);
10032e2caf59SThomas Veerman     }
10042e2caf59SThomas Veerman }
10052e2caf59SThomas Veerman 
10062e2caf59SThomas Veerman /*-
10072e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10082e2caf59SThomas Veerman  * Suff_GetPath --
10092e2caf59SThomas Veerman  *	Return the search path for the given suffix, if it's defined.
10102e2caf59SThomas Veerman  *
10112e2caf59SThomas Veerman  * Results:
10122e2caf59SThomas Veerman  *	The searchPath for the desired suffix or NULL if the suffix isn't
10132e2caf59SThomas Veerman  *	defined.
10142e2caf59SThomas Veerman  *
10152e2caf59SThomas Veerman  * Side Effects:
10162e2caf59SThomas Veerman  *	None
10172e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10182e2caf59SThomas Veerman  */
10192e2caf59SThomas Veerman Lst
Suff_GetPath(char * sname)10202e2caf59SThomas Veerman Suff_GetPath(char *sname)
10212e2caf59SThomas Veerman {
10222e2caf59SThomas Veerman     LstNode   	  ln;
10232e2caf59SThomas Veerman     Suff    	  *s;
10242e2caf59SThomas Veerman 
10252e2caf59SThomas Veerman     ln = Lst_Find(sufflist, sname, SuffSuffHasNameP);
10262e2caf59SThomas Veerman     if (ln == NULL) {
10272e2caf59SThomas Veerman 	return NULL;
10282e2caf59SThomas Veerman     } else {
10292e2caf59SThomas Veerman 	s = (Suff *)Lst_Datum(ln);
10302e2caf59SThomas Veerman 	return (s->searchPath);
10312e2caf59SThomas Veerman     }
10322e2caf59SThomas Veerman }
10332e2caf59SThomas Veerman 
10342e2caf59SThomas Veerman /*-
10352e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10362e2caf59SThomas Veerman  * Suff_DoPaths --
10372e2caf59SThomas Veerman  *	Extend the search paths for all suffixes to include the default
10382e2caf59SThomas Veerman  *	search path.
10392e2caf59SThomas Veerman  *
10402e2caf59SThomas Veerman  * Results:
10412e2caf59SThomas Veerman  *	None.
10422e2caf59SThomas Veerman  *
10432e2caf59SThomas Veerman  * Side Effects:
10442e2caf59SThomas Veerman  *	The searchPath field of all the suffixes is extended by the
10452e2caf59SThomas Veerman  *	directories in dirSearchPath. If paths were specified for the
10462e2caf59SThomas Veerman  *	".h" suffix, the directories are stuffed into a global variable
10472e2caf59SThomas Veerman  *	called ".INCLUDES" with each directory preceded by a -I. The same
10482e2caf59SThomas Veerman  *	is done for the ".a" suffix, except the variable is called
10492e2caf59SThomas Veerman  *	".LIBS" and the flag is -L.
10502e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10512e2caf59SThomas Veerman  */
10522e2caf59SThomas Veerman void
Suff_DoPaths(void)10532e2caf59SThomas Veerman Suff_DoPaths(void)
10542e2caf59SThomas Veerman {
10552e2caf59SThomas Veerman     Suff	   	*s;
10562e2caf59SThomas Veerman     LstNode  		ln;
10572e2caf59SThomas Veerman     char		*ptr;
10582e2caf59SThomas Veerman     Lst	    	    	inIncludes; /* Cumulative .INCLUDES path */
10592e2caf59SThomas Veerman     Lst	    	    	inLibs;	    /* Cumulative .LIBS path */
10602e2caf59SThomas Veerman 
10612e2caf59SThomas Veerman     if (Lst_Open(sufflist) == FAILURE) {
10622e2caf59SThomas Veerman 	return;
10632e2caf59SThomas Veerman     }
10642e2caf59SThomas Veerman 
10652e2caf59SThomas Veerman     inIncludes = Lst_Init(FALSE);
10662e2caf59SThomas Veerman     inLibs = Lst_Init(FALSE);
10672e2caf59SThomas Veerman 
10682e2caf59SThomas Veerman     while ((ln = Lst_Next(sufflist)) != NULL) {
10692e2caf59SThomas Veerman 	s = (Suff *)Lst_Datum(ln);
10702e2caf59SThomas Veerman 	if (!Lst_IsEmpty (s->searchPath)) {
10712e2caf59SThomas Veerman #ifdef INCLUDES
10722e2caf59SThomas Veerman 	    if (s->flags & SUFF_INCLUDE) {
10732e2caf59SThomas Veerman 		Dir_Concat(inIncludes, s->searchPath);
10742e2caf59SThomas Veerman 	    }
10752e2caf59SThomas Veerman #endif /* INCLUDES */
10762e2caf59SThomas Veerman #ifdef LIBRARIES
10772e2caf59SThomas Veerman 	    if (s->flags & SUFF_LIBRARY) {
10782e2caf59SThomas Veerman 		Dir_Concat(inLibs, s->searchPath);
10792e2caf59SThomas Veerman 	    }
10802e2caf59SThomas Veerman #endif /* LIBRARIES */
10812e2caf59SThomas Veerman 	    Dir_Concat(s->searchPath, dirSearchPath);
10822e2caf59SThomas Veerman 	} else {
10832e2caf59SThomas Veerman 	    Lst_Destroy(s->searchPath, Dir_Destroy);
10842e2caf59SThomas Veerman 	    s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
10852e2caf59SThomas Veerman 	}
10862e2caf59SThomas Veerman     }
10872e2caf59SThomas Veerman 
10882e2caf59SThomas Veerman     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL, 0);
10892e2caf59SThomas Veerman     free(ptr);
10902e2caf59SThomas Veerman     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL, 0);
10912e2caf59SThomas Veerman     free(ptr);
10922e2caf59SThomas Veerman 
10932e2caf59SThomas Veerman     Lst_Destroy(inIncludes, Dir_Destroy);
10942e2caf59SThomas Veerman     Lst_Destroy(inLibs, Dir_Destroy);
10952e2caf59SThomas Veerman 
10962e2caf59SThomas Veerman     Lst_Close(sufflist);
10972e2caf59SThomas Veerman }
10982e2caf59SThomas Veerman 
10992e2caf59SThomas Veerman /*-
11002e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11012e2caf59SThomas Veerman  * Suff_AddInclude --
11022e2caf59SThomas Veerman  *	Add the given suffix as a type of file which gets included.
11032e2caf59SThomas Veerman  *	Called from the parse module when a .INCLUDES line is parsed.
11042e2caf59SThomas Veerman  *	The suffix must have already been defined.
11052e2caf59SThomas Veerman  *
11062e2caf59SThomas Veerman  * Input:
11072e2caf59SThomas Veerman  *	sname		Name of the suffix to mark
11082e2caf59SThomas Veerman  *
11092e2caf59SThomas Veerman  * Results:
11102e2caf59SThomas Veerman  *	None.
11112e2caf59SThomas Veerman  *
11122e2caf59SThomas Veerman  * Side Effects:
11132e2caf59SThomas Veerman  *	The SUFF_INCLUDE bit is set in the suffix's flags field
11142e2caf59SThomas Veerman  *
11152e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11162e2caf59SThomas Veerman  */
11172e2caf59SThomas Veerman void
Suff_AddInclude(char * sname)11182e2caf59SThomas Veerman Suff_AddInclude(char *sname)
11192e2caf59SThomas Veerman {
11202e2caf59SThomas Veerman     LstNode	  ln;
11212e2caf59SThomas Veerman     Suff	  *s;
11222e2caf59SThomas Veerman 
11232e2caf59SThomas Veerman     ln = Lst_Find(sufflist, sname, SuffSuffHasNameP);
11242e2caf59SThomas Veerman     if (ln != NULL) {
11252e2caf59SThomas Veerman 	s = (Suff *)Lst_Datum(ln);
11262e2caf59SThomas Veerman 	s->flags |= SUFF_INCLUDE;
11272e2caf59SThomas Veerman     }
11282e2caf59SThomas Veerman }
11292e2caf59SThomas Veerman 
11302e2caf59SThomas Veerman /*-
11312e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11322e2caf59SThomas Veerman  * Suff_AddLib --
11332e2caf59SThomas Veerman  *	Add the given suffix as a type of file which is a library.
11342e2caf59SThomas Veerman  *	Called from the parse module when parsing a .LIBS line. The
11352e2caf59SThomas Veerman  *	suffix must have been defined via .SUFFIXES before this is
11362e2caf59SThomas Veerman  *	called.
11372e2caf59SThomas Veerman  *
11382e2caf59SThomas Veerman  * Input:
11392e2caf59SThomas Veerman  *	sname		Name of the suffix to mark
11402e2caf59SThomas Veerman  *
11412e2caf59SThomas Veerman  * Results:
11422e2caf59SThomas Veerman  *	None.
11432e2caf59SThomas Veerman  *
11442e2caf59SThomas Veerman  * Side Effects:
11452e2caf59SThomas Veerman  *	The SUFF_LIBRARY bit is set in the suffix's flags field
11462e2caf59SThomas Veerman  *
11472e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11482e2caf59SThomas Veerman  */
11492e2caf59SThomas Veerman void
Suff_AddLib(char * sname)11502e2caf59SThomas Veerman Suff_AddLib(char *sname)
11512e2caf59SThomas Veerman {
11522e2caf59SThomas Veerman     LstNode	  ln;
11532e2caf59SThomas Veerman     Suff	  *s;
11542e2caf59SThomas Veerman 
11552e2caf59SThomas Veerman     ln = Lst_Find(sufflist, sname, SuffSuffHasNameP);
11562e2caf59SThomas Veerman     if (ln != NULL) {
11572e2caf59SThomas Veerman 	s = (Suff *)Lst_Datum(ln);
11582e2caf59SThomas Veerman 	s->flags |= SUFF_LIBRARY;
11592e2caf59SThomas Veerman     }
11602e2caf59SThomas Veerman }
11612e2caf59SThomas Veerman 
11622e2caf59SThomas Veerman  	  /********** Implicit Source Search Functions *********/
11632e2caf59SThomas Veerman 
11642e2caf59SThomas Veerman /*-
11652e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11662e2caf59SThomas Veerman  * SuffAddSrc  --
11672e2caf59SThomas Veerman  *	Add a suffix as a Src structure to the given list with its parent
11682e2caf59SThomas Veerman  *	being the given Src structure. If the suffix is the null suffix,
11692e2caf59SThomas Veerman  *	the prefix is used unaltered as the file name in the Src structure.
11702e2caf59SThomas Veerman  *
11712e2caf59SThomas Veerman  * Input:
11722e2caf59SThomas Veerman  *	sp		suffix for which to create a Src structure
11732e2caf59SThomas Veerman  *	lsp		list and parent for the new Src
11742e2caf59SThomas Veerman  *
11752e2caf59SThomas Veerman  * Results:
11762e2caf59SThomas Veerman  *	always returns 0
11772e2caf59SThomas Veerman  *
11782e2caf59SThomas Veerman  * Side Effects:
11792e2caf59SThomas Veerman  *	A Src structure is created and tacked onto the end of the list
11802e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11812e2caf59SThomas Veerman  */
11822e2caf59SThomas Veerman static int
SuffAddSrc(void * sp,void * lsp)11832e2caf59SThomas Veerman SuffAddSrc(void *sp, void *lsp)
11842e2caf59SThomas Veerman {
11852e2caf59SThomas Veerman     Suff	*s = (Suff *)sp;
11862e2caf59SThomas Veerman     LstSrc      *ls = (LstSrc *)lsp;
11872e2caf59SThomas Veerman     Src         *s2;	    /* new Src structure */
11882e2caf59SThomas Veerman     Src    	*targ; 	    /* Target structure */
11892e2caf59SThomas Veerman 
11902e2caf59SThomas Veerman     targ = ls->s;
11912e2caf59SThomas Veerman 
11922e2caf59SThomas Veerman     if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
11932e2caf59SThomas Veerman 	/*
11942e2caf59SThomas Veerman 	 * If the suffix has been marked as the NULL suffix, also create a Src
11952e2caf59SThomas Veerman 	 * structure for a file with no suffix attached. Two birds, and all
11962e2caf59SThomas Veerman 	 * that...
11972e2caf59SThomas Veerman 	 */
11982e2caf59SThomas Veerman 	s2 = bmake_malloc(sizeof(Src));
11992e2caf59SThomas Veerman 	s2->file =  	bmake_strdup(targ->pref);
12002e2caf59SThomas Veerman 	s2->pref =  	targ->pref;
12012e2caf59SThomas Veerman 	s2->parent = 	targ;
12022e2caf59SThomas Veerman 	s2->node =  	NULL;
12032e2caf59SThomas Veerman 	s2->suff =  	s;
12042e2caf59SThomas Veerman 	s->refCount++;
12052e2caf59SThomas Veerman 	s2->children =	0;
12062e2caf59SThomas Veerman 	targ->children += 1;
12072e2caf59SThomas Veerman 	(void)Lst_AtEnd(ls->l, s2);
12082e2caf59SThomas Veerman #ifdef DEBUG_SRC
12092e2caf59SThomas Veerman 	s2->cp = Lst_Init(FALSE);
12102e2caf59SThomas Veerman 	Lst_AtEnd(targ->cp, s2);
12112e2caf59SThomas Veerman 	fprintf(debug_file, "1 add %x %x to %x:", targ, s2, ls->l);
12122e2caf59SThomas Veerman 	Lst_ForEach(ls->l, PrintAddr, NULL);
12132e2caf59SThomas Veerman 	fprintf(debug_file, "\n");
12142e2caf59SThomas Veerman #endif
12152e2caf59SThomas Veerman     }
12162e2caf59SThomas Veerman     s2 = bmake_malloc(sizeof(Src));
12172e2caf59SThomas Veerman     s2->file = 	    str_concat(targ->pref, s->name, 0);
12182e2caf59SThomas Veerman     s2->pref =	    targ->pref;
12192e2caf59SThomas Veerman     s2->parent =    targ;
12202e2caf59SThomas Veerman     s2->node = 	    NULL;
12212e2caf59SThomas Veerman     s2->suff = 	    s;
12222e2caf59SThomas Veerman     s->refCount++;
12232e2caf59SThomas Veerman     s2->children =  0;
12242e2caf59SThomas Veerman     targ->children += 1;
12252e2caf59SThomas Veerman     (void)Lst_AtEnd(ls->l, s2);
12262e2caf59SThomas Veerman #ifdef DEBUG_SRC
12272e2caf59SThomas Veerman     s2->cp = Lst_Init(FALSE);
12282e2caf59SThomas Veerman     Lst_AtEnd(targ->cp, s2);
12292e2caf59SThomas Veerman     fprintf(debug_file, "2 add %x %x to %x:", targ, s2, ls->l);
12302e2caf59SThomas Veerman     Lst_ForEach(ls->l, PrintAddr, NULL);
12312e2caf59SThomas Veerman     fprintf(debug_file, "\n");
12322e2caf59SThomas Veerman #endif
12332e2caf59SThomas Veerman 
12342e2caf59SThomas Veerman     return(0);
12352e2caf59SThomas Veerman }
12362e2caf59SThomas Veerman 
12372e2caf59SThomas Veerman /*-
12382e2caf59SThomas Veerman  *-----------------------------------------------------------------------
12392e2caf59SThomas Veerman  * SuffAddLevel  --
12402e2caf59SThomas Veerman  *	Add all the children of targ as Src structures to the given list
12412e2caf59SThomas Veerman  *
12422e2caf59SThomas Veerman  * Input:
12432e2caf59SThomas Veerman  *	l		list to which to add the new level
12442e2caf59SThomas Veerman  *	targ		Src structure to use as the parent
12452e2caf59SThomas Veerman  *
12462e2caf59SThomas Veerman  * Results:
12472e2caf59SThomas Veerman  *	None
12482e2caf59SThomas Veerman  *
12492e2caf59SThomas Veerman  * Side Effects:
12502e2caf59SThomas Veerman  * 	Lots of structures are created and added to the list
12512e2caf59SThomas Veerman  *-----------------------------------------------------------------------
12522e2caf59SThomas Veerman  */
12532e2caf59SThomas Veerman static void
SuffAddLevel(Lst l,Src * targ)12542e2caf59SThomas Veerman SuffAddLevel(Lst l, Src *targ)
12552e2caf59SThomas Veerman {
12562e2caf59SThomas Veerman     LstSrc         ls;
12572e2caf59SThomas Veerman 
12582e2caf59SThomas Veerman     ls.s = targ;
12592e2caf59SThomas Veerman     ls.l = l;
12602e2caf59SThomas Veerman 
12612e2caf59SThomas Veerman     Lst_ForEach(targ->suff->children, SuffAddSrc, &ls);
12622e2caf59SThomas Veerman }
12632e2caf59SThomas Veerman 
12642e2caf59SThomas Veerman /*-
12652e2caf59SThomas Veerman  *----------------------------------------------------------------------
12662e2caf59SThomas Veerman  * SuffRemoveSrc --
12672e2caf59SThomas Veerman  *	Free all src structures in list that don't have a reference count
12682e2caf59SThomas Veerman  *
12692e2caf59SThomas Veerman  * Results:
12702e2caf59SThomas Veerman  *	Ture if an src was removed
12712e2caf59SThomas Veerman  *
12722e2caf59SThomas Veerman  * Side Effects:
12732e2caf59SThomas Veerman  *	The memory is free'd.
12742e2caf59SThomas Veerman  *----------------------------------------------------------------------
12752e2caf59SThomas Veerman  */
12762e2caf59SThomas Veerman static int
SuffRemoveSrc(Lst l)12772e2caf59SThomas Veerman SuffRemoveSrc(Lst l)
12782e2caf59SThomas Veerman {
12792e2caf59SThomas Veerman     LstNode ln;
12802e2caf59SThomas Veerman     Src *s;
12812e2caf59SThomas Veerman     int t = 0;
12822e2caf59SThomas Veerman 
12832e2caf59SThomas Veerman     if (Lst_Open(l) == FAILURE) {
12842e2caf59SThomas Veerman 	return 0;
12852e2caf59SThomas Veerman     }
12862e2caf59SThomas Veerman #ifdef DEBUG_SRC
12872e2caf59SThomas Veerman     fprintf(debug_file, "cleaning %lx: ", (unsigned long) l);
12882e2caf59SThomas Veerman     Lst_ForEach(l, PrintAddr, NULL);
12892e2caf59SThomas Veerman     fprintf(debug_file, "\n");
12902e2caf59SThomas Veerman #endif
12912e2caf59SThomas Veerman 
12922e2caf59SThomas Veerman 
12932e2caf59SThomas Veerman     while ((ln = Lst_Next(l)) != NULL) {
12942e2caf59SThomas Veerman 	s = (Src *)Lst_Datum(ln);
12952e2caf59SThomas Veerman 	if (s->children == 0) {
12962e2caf59SThomas Veerman 	    free(s->file);
12972e2caf59SThomas Veerman 	    if (!s->parent)
12982e2caf59SThomas Veerman 		free(s->pref);
12992e2caf59SThomas Veerman 	    else {
13002e2caf59SThomas Veerman #ifdef DEBUG_SRC
13012e2caf59SThomas Veerman 		LstNode ln = Lst_Member(s->parent->cp, s);
13022e2caf59SThomas Veerman 		if (ln != NULL)
13032e2caf59SThomas Veerman 		    Lst_Remove(s->parent->cp, ln);
13042e2caf59SThomas Veerman #endif
13052e2caf59SThomas Veerman 		--s->parent->children;
13062e2caf59SThomas Veerman 	    }
13072e2caf59SThomas Veerman #ifdef DEBUG_SRC
13082e2caf59SThomas Veerman 	    fprintf(debug_file, "free: [l=%x] p=%x %d\n", l, s, s->children);
13092e2caf59SThomas Veerman 	    Lst_Destroy(s->cp, NULL);
13102e2caf59SThomas Veerman #endif
13112e2caf59SThomas Veerman 	    Lst_Remove(l, ln);
13122e2caf59SThomas Veerman 	    free(s);
13132e2caf59SThomas Veerman 	    t |= 1;
13142e2caf59SThomas Veerman 	    Lst_Close(l);
13152e2caf59SThomas Veerman 	    return TRUE;
13162e2caf59SThomas Veerman 	}
13172e2caf59SThomas Veerman #ifdef DEBUG_SRC
13182e2caf59SThomas Veerman 	else {
13192e2caf59SThomas Veerman 	    fprintf(debug_file, "keep: [l=%x] p=%x %d: ", l, s, s->children);
13202e2caf59SThomas Veerman 	    Lst_ForEach(s->cp, PrintAddr, NULL);
13212e2caf59SThomas Veerman 	    fprintf(debug_file, "\n");
13222e2caf59SThomas Veerman 	}
13232e2caf59SThomas Veerman #endif
13242e2caf59SThomas Veerman     }
13252e2caf59SThomas Veerman 
13262e2caf59SThomas Veerman     Lst_Close(l);
13272e2caf59SThomas Veerman 
13282e2caf59SThomas Veerman     return t;
13292e2caf59SThomas Veerman }
13302e2caf59SThomas Veerman 
13312e2caf59SThomas Veerman /*-
13322e2caf59SThomas Veerman  *-----------------------------------------------------------------------
13332e2caf59SThomas Veerman  * SuffFindThem --
13342e2caf59SThomas Veerman  *	Find the first existing file/target in the list srcs
13352e2caf59SThomas Veerman  *
13362e2caf59SThomas Veerman  * Input:
13372e2caf59SThomas Veerman  *	srcs		list of Src structures to search through
13382e2caf59SThomas Veerman  *
13392e2caf59SThomas Veerman  * Results:
13402e2caf59SThomas Veerman  *	The lowest structure in the chain of transformations
13412e2caf59SThomas Veerman  *
13422e2caf59SThomas Veerman  * Side Effects:
13432e2caf59SThomas Veerman  *	None
13442e2caf59SThomas Veerman  *-----------------------------------------------------------------------
13452e2caf59SThomas Veerman  */
13462e2caf59SThomas Veerman static Src *
SuffFindThem(Lst srcs,Lst slst)13472e2caf59SThomas Veerman SuffFindThem(Lst srcs, Lst slst)
13482e2caf59SThomas Veerman {
13492e2caf59SThomas Veerman     Src            *s;		/* current Src */
13502e2caf59SThomas Veerman     Src		   *rs;		/* returned Src */
13512e2caf59SThomas Veerman     char	   *ptr;
13522e2caf59SThomas Veerman 
13532e2caf59SThomas Veerman     rs = NULL;
13542e2caf59SThomas Veerman 
13552e2caf59SThomas Veerman     while (!Lst_IsEmpty (srcs)) {
13562e2caf59SThomas Veerman 	s = (Src *)Lst_DeQueue(srcs);
13572e2caf59SThomas Veerman 
13582e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
13592e2caf59SThomas Veerman 	    fprintf(debug_file, "\ttrying %s...", s->file);
13602e2caf59SThomas Veerman 	}
13612e2caf59SThomas Veerman 
13622e2caf59SThomas Veerman 	/*
13632e2caf59SThomas Veerman 	 * A file is considered to exist if either a node exists in the
13642e2caf59SThomas Veerman 	 * graph for it or the file actually exists.
13652e2caf59SThomas Veerman 	 */
13662e2caf59SThomas Veerman 	if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
13672e2caf59SThomas Veerman #ifdef DEBUG_SRC
13682e2caf59SThomas Veerman 	    fprintf(debug_file, "remove %x from %x\n", s, srcs);
13692e2caf59SThomas Veerman #endif
13702e2caf59SThomas Veerman 	    rs = s;
13712e2caf59SThomas Veerman 	    break;
13722e2caf59SThomas Veerman 	}
13732e2caf59SThomas Veerman 
13742e2caf59SThomas Veerman 	if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
13752e2caf59SThomas Veerman 	    rs = s;
13762e2caf59SThomas Veerman #ifdef DEBUG_SRC
13772e2caf59SThomas Veerman 	    fprintf(debug_file, "remove %x from %x\n", s, srcs);
13782e2caf59SThomas Veerman #endif
13792e2caf59SThomas Veerman 	    free(ptr);
13802e2caf59SThomas Veerman 	    break;
13812e2caf59SThomas Veerman 	}
13822e2caf59SThomas Veerman 
13832e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
13842e2caf59SThomas Veerman 	    fprintf(debug_file, "not there\n");
13852e2caf59SThomas Veerman 	}
13862e2caf59SThomas Veerman 
13872e2caf59SThomas Veerman 	SuffAddLevel(srcs, s);
13882e2caf59SThomas Veerman 	Lst_AtEnd(slst, s);
13892e2caf59SThomas Veerman     }
13902e2caf59SThomas Veerman 
13912e2caf59SThomas Veerman     if (DEBUG(SUFF) && rs) {
13922e2caf59SThomas Veerman 	fprintf(debug_file, "got it\n");
13932e2caf59SThomas Veerman     }
13942e2caf59SThomas Veerman     return (rs);
13952e2caf59SThomas Veerman }
13962e2caf59SThomas Veerman 
13972e2caf59SThomas Veerman /*-
13982e2caf59SThomas Veerman  *-----------------------------------------------------------------------
13992e2caf59SThomas Veerman  * SuffFindCmds --
14002e2caf59SThomas Veerman  *	See if any of the children of the target in the Src structure is
14012e2caf59SThomas Veerman  *	one from which the target can be transformed. If there is one,
14022e2caf59SThomas Veerman  *	a Src structure is put together for it and returned.
14032e2caf59SThomas Veerman  *
14042e2caf59SThomas Veerman  * Input:
14052e2caf59SThomas Veerman  *	targ		Src structure to play with
14062e2caf59SThomas Veerman  *
14072e2caf59SThomas Veerman  * Results:
14082e2caf59SThomas Veerman  *	The Src structure of the "winning" child, or NULL if no such beast.
14092e2caf59SThomas Veerman  *
14102e2caf59SThomas Veerman  * Side Effects:
14112e2caf59SThomas Veerman  *	A Src structure may be allocated.
14122e2caf59SThomas Veerman  *
14132e2caf59SThomas Veerman  *-----------------------------------------------------------------------
14142e2caf59SThomas Veerman  */
14152e2caf59SThomas Veerman static Src *
SuffFindCmds(Src * targ,Lst slst)14162e2caf59SThomas Veerman SuffFindCmds(Src *targ, Lst slst)
14172e2caf59SThomas Veerman {
14182e2caf59SThomas Veerman     LstNode 	  	ln; 	/* General-purpose list node */
14192e2caf59SThomas Veerman     GNode		*t, 	/* Target GNode */
14202e2caf59SThomas Veerman 	    	  	*s; 	/* Source GNode */
14212e2caf59SThomas Veerman     int	    	  	prefLen;/* The length of the defined prefix */
14222e2caf59SThomas Veerman     Suff    	  	*suff;	/* Suffix on matching beastie */
14232e2caf59SThomas Veerman     Src	    	  	*ret;	/* Return value */
14242e2caf59SThomas Veerman     char    	  	*cp;
14252e2caf59SThomas Veerman 
14262e2caf59SThomas Veerman     t = targ->node;
14272e2caf59SThomas Veerman     (void)Lst_Open(t->children);
14282e2caf59SThomas Veerman     prefLen = strlen(targ->pref);
14292e2caf59SThomas Veerman 
14302e2caf59SThomas Veerman     for (;;) {
14312e2caf59SThomas Veerman 	ln = Lst_Next(t->children);
14322e2caf59SThomas Veerman 	if (ln == NULL) {
14332e2caf59SThomas Veerman 	    Lst_Close(t->children);
14342e2caf59SThomas Veerman 	    return NULL;
14352e2caf59SThomas Veerman 	}
14362e2caf59SThomas Veerman 	s = (GNode *)Lst_Datum(ln);
14372e2caf59SThomas Veerman 
14382e2caf59SThomas Veerman 	if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
14392e2caf59SThomas Veerman 	    /*
14402e2caf59SThomas Veerman 	     * We haven't looked to see if .OPTIONAL files exist yet, so
14412e2caf59SThomas Veerman 	     * don't use one as the implicit source.
14422e2caf59SThomas Veerman 	     * This allows us to use .OPTIONAL in .depend files so make won't
14432e2caf59SThomas Veerman 	     * complain "don't know how to make xxx.h' when a dependent file
14442e2caf59SThomas Veerman 	     * has been moved/deleted.
14452e2caf59SThomas Veerman 	     */
14462e2caf59SThomas Veerman 	    continue;
14472e2caf59SThomas Veerman 	}
14482e2caf59SThomas Veerman 
14492e2caf59SThomas Veerman 	cp = strrchr(s->name, '/');
14502e2caf59SThomas Veerman 	if (cp == NULL) {
14512e2caf59SThomas Veerman 	    cp = s->name;
14522e2caf59SThomas Veerman 	} else {
14532e2caf59SThomas Veerman 	    cp++;
14542e2caf59SThomas Veerman 	}
14552e2caf59SThomas Veerman 	if (strncmp(cp, targ->pref, prefLen) != 0)
14562e2caf59SThomas Veerman 	    continue;
14572e2caf59SThomas Veerman 	/*
14582e2caf59SThomas Veerman 	 * The node matches the prefix ok, see if it has a known
14592e2caf59SThomas Veerman 	 * suffix.
14602e2caf59SThomas Veerman 	 */
14612e2caf59SThomas Veerman 	ln = Lst_Find(sufflist, &cp[prefLen], SuffSuffHasNameP);
14622e2caf59SThomas Veerman 	if (ln == NULL)
14632e2caf59SThomas Veerman 	    continue;
14642e2caf59SThomas Veerman 	/*
14652e2caf59SThomas Veerman 	 * It even has a known suffix, see if there's a transformation
14662e2caf59SThomas Veerman 	 * defined between the node's suffix and the target's suffix.
14672e2caf59SThomas Veerman 	 *
14682e2caf59SThomas Veerman 	 * XXX: Handle multi-stage transformations here, too.
14692e2caf59SThomas Veerman 	 */
14702e2caf59SThomas Veerman 	suff = (Suff *)Lst_Datum(ln);
14712e2caf59SThomas Veerman 
14722e2caf59SThomas Veerman 	if (Lst_Member(suff->parents, targ->suff) != NULL)
14732e2caf59SThomas Veerman 	    break;
14742e2caf59SThomas Veerman     }
14752e2caf59SThomas Veerman 
14762e2caf59SThomas Veerman     /*
14772e2caf59SThomas Veerman      * Hot Damn! Create a new Src structure to describe
14782e2caf59SThomas Veerman      * this transformation (making sure to duplicate the
14792e2caf59SThomas Veerman      * source node's name so Suff_FindDeps can free it
14802e2caf59SThomas Veerman      * again (ick)), and return the new structure.
14812e2caf59SThomas Veerman      */
14822e2caf59SThomas Veerman     ret = bmake_malloc(sizeof(Src));
14832e2caf59SThomas Veerman     ret->file = bmake_strdup(s->name);
14842e2caf59SThomas Veerman     ret->pref = targ->pref;
14852e2caf59SThomas Veerman     ret->suff = suff;
14862e2caf59SThomas Veerman     suff->refCount++;
14872e2caf59SThomas Veerman     ret->parent = targ;
14882e2caf59SThomas Veerman     ret->node = s;
14892e2caf59SThomas Veerman     ret->children = 0;
14902e2caf59SThomas Veerman     targ->children += 1;
14912e2caf59SThomas Veerman #ifdef DEBUG_SRC
14922e2caf59SThomas Veerman     ret->cp = Lst_Init(FALSE);
14932e2caf59SThomas Veerman     fprintf(debug_file, "3 add %x %x\n", targ, ret);
14942e2caf59SThomas Veerman     Lst_AtEnd(targ->cp, ret);
14952e2caf59SThomas Veerman #endif
14962e2caf59SThomas Veerman     Lst_AtEnd(slst, ret);
14972e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
14982e2caf59SThomas Veerman 	fprintf(debug_file, "\tusing existing source %s\n", s->name);
14992e2caf59SThomas Veerman     }
15002e2caf59SThomas Veerman     return (ret);
15012e2caf59SThomas Veerman }
15022e2caf59SThomas Veerman 
15032e2caf59SThomas Veerman /*-
15042e2caf59SThomas Veerman  *-----------------------------------------------------------------------
15052e2caf59SThomas Veerman  * SuffExpandChildren --
15062e2caf59SThomas Veerman  *	Expand the names of any children of a given node that contain
15072e2caf59SThomas Veerman  *	variable invocations or file wildcards into actual targets.
15082e2caf59SThomas Veerman  *
15092e2caf59SThomas Veerman  * Input:
15102e2caf59SThomas Veerman  *	cln		Child to examine
15112e2caf59SThomas Veerman  *	pgn		Parent node being processed
15122e2caf59SThomas Veerman  *
15132e2caf59SThomas Veerman  * Results:
15142e2caf59SThomas Veerman  *	=== 0 (continue)
15152e2caf59SThomas Veerman  *
15162e2caf59SThomas Veerman  * Side Effects:
15172e2caf59SThomas Veerman  *	The expanded node is removed from the parent's list of children,
15182e2caf59SThomas Veerman  *	and the parent's unmade counter is decremented, but other nodes
15192e2caf59SThomas Veerman  * 	may be added.
15202e2caf59SThomas Veerman  *
15212e2caf59SThomas Veerman  *-----------------------------------------------------------------------
15222e2caf59SThomas Veerman  */
15232e2caf59SThomas Veerman static void
SuffExpandChildren(LstNode cln,GNode * pgn)15242e2caf59SThomas Veerman SuffExpandChildren(LstNode cln, GNode *pgn)
15252e2caf59SThomas Veerman {
15262e2caf59SThomas Veerman     GNode   	*cgn = (GNode *)Lst_Datum(cln);
15272e2caf59SThomas Veerman     GNode	*gn;	    /* New source 8) */
15282e2caf59SThomas Veerman     char	*cp;	    /* Expanded value */
15292e2caf59SThomas Veerman 
15302e2caf59SThomas Veerman     if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
15312e2caf59SThomas Veerman 	/* It is all too hard to process the result of .ORDER */
15322e2caf59SThomas Veerman 	return;
15332e2caf59SThomas Veerman 
15342e2caf59SThomas Veerman     if (cgn->type & OP_WAIT)
15352e2caf59SThomas Veerman 	/* Ignore these (& OP_PHONY ?) */
15362e2caf59SThomas Veerman 	return;
15372e2caf59SThomas Veerman 
15382e2caf59SThomas Veerman     /*
15392e2caf59SThomas Veerman      * First do variable expansion -- this takes precedence over
15402e2caf59SThomas Veerman      * wildcard expansion. If the result contains wildcards, they'll be gotten
15412e2caf59SThomas Veerman      * to later since the resulting words are tacked on to the end of
15422e2caf59SThomas Veerman      * the children list.
15432e2caf59SThomas Veerman      */
15442e2caf59SThomas Veerman     if (strchr(cgn->name, '$') == NULL) {
15452e2caf59SThomas Veerman 	SuffExpandWildcards(cln, pgn);
15462e2caf59SThomas Veerman 	return;
15472e2caf59SThomas Veerman     }
15482e2caf59SThomas Veerman 
15492e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
15502e2caf59SThomas Veerman 	fprintf(debug_file, "Expanding \"%s\"...", cgn->name);
15512e2caf59SThomas Veerman     }
15522e2caf59SThomas Veerman     cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
15532e2caf59SThomas Veerman 
15542e2caf59SThomas Veerman     if (cp != NULL) {
15552e2caf59SThomas Veerman 	Lst	    members = Lst_Init(FALSE);
15562e2caf59SThomas Veerman 
15572e2caf59SThomas Veerman 	if (cgn->type & OP_ARCHV) {
15582e2caf59SThomas Veerman 	    /*
15592e2caf59SThomas Veerman 	     * Node was an archive(member) target, so we want to call
15602e2caf59SThomas Veerman 	     * on the Arch module to find the nodes for us, expanding
15612e2caf59SThomas Veerman 	     * variables in the parent's context.
15622e2caf59SThomas Veerman 	     */
15632e2caf59SThomas Veerman 	    char	*sacrifice = cp;
15642e2caf59SThomas Veerman 
15652e2caf59SThomas Veerman 	    (void)Arch_ParseArchive(&sacrifice, members, pgn);
15662e2caf59SThomas Veerman 	} else {
15672e2caf59SThomas Veerman 	    /*
15682e2caf59SThomas Veerman 	     * Break the result into a vector of strings whose nodes
15692e2caf59SThomas Veerman 	     * we can find, then add those nodes to the members list.
15702e2caf59SThomas Veerman 	     * Unfortunately, we can't use brk_string b/c it
15712e2caf59SThomas Veerman 	     * doesn't understand about variable specifications with
15722e2caf59SThomas Veerman 	     * spaces in them...
15732e2caf59SThomas Veerman 	     */
15742e2caf59SThomas Veerman 	    char	    *start;
15752e2caf59SThomas Veerman 	    char	    *initcp = cp;   /* For freeing... */
15762e2caf59SThomas Veerman 
15772e2caf59SThomas Veerman 	    for (start = cp; *start == ' ' || *start == '\t'; start++)
15782e2caf59SThomas Veerman 		continue;
15792e2caf59SThomas Veerman 	    for (cp = start; *cp != '\0'; cp++) {
15802e2caf59SThomas Veerman 		if (*cp == ' ' || *cp == '\t') {
15812e2caf59SThomas Veerman 		    /*
15822e2caf59SThomas Veerman 		     * White-space -- terminate element, find the node,
15832e2caf59SThomas Veerman 		     * add it, skip any further spaces.
15842e2caf59SThomas Veerman 		     */
15852e2caf59SThomas Veerman 		    *cp++ = '\0';
15862e2caf59SThomas Veerman 		    gn = Targ_FindNode(start, TARG_CREATE);
15872e2caf59SThomas Veerman 		    (void)Lst_AtEnd(members, gn);
15882e2caf59SThomas Veerman 		    while (*cp == ' ' || *cp == '\t') {
15892e2caf59SThomas Veerman 			cp++;
15902e2caf59SThomas Veerman 		    }
15912e2caf59SThomas Veerman 		    /*
15922e2caf59SThomas Veerman 		     * Adjust cp for increment at start of loop, but
15932e2caf59SThomas Veerman 		     * set start to first non-space.
15942e2caf59SThomas Veerman 		     */
15952e2caf59SThomas Veerman 		    start = cp--;
15962e2caf59SThomas Veerman 		} else if (*cp == '$') {
15972e2caf59SThomas Veerman 		    /*
15982e2caf59SThomas Veerman 		     * Start of a variable spec -- contact variable module
15992e2caf59SThomas Veerman 		     * to find the end so we can skip over it.
16002e2caf59SThomas Veerman 		     */
16012e2caf59SThomas Veerman 		    char	*junk;
16022e2caf59SThomas Veerman 		    int 	len;
16032e2caf59SThomas Veerman 		    void	*freeIt;
16042e2caf59SThomas Veerman 
16052e2caf59SThomas Veerman 		    junk = Var_Parse(cp, pgn, TRUE, &len, &freeIt);
16062e2caf59SThomas Veerman 		    if (junk != var_Error) {
16072e2caf59SThomas Veerman 			cp += len - 1;
16082e2caf59SThomas Veerman 		    }
16092e2caf59SThomas Veerman 
16102e2caf59SThomas Veerman 		    if (freeIt)
16112e2caf59SThomas Veerman 			free(freeIt);
16122e2caf59SThomas Veerman 		} else if (*cp == '\\' && *cp != '\0') {
16132e2caf59SThomas Veerman 		    /*
16142e2caf59SThomas Veerman 		     * Escaped something -- skip over it
16152e2caf59SThomas Veerman 		     */
16162e2caf59SThomas Veerman 		    cp++;
16172e2caf59SThomas Veerman 		}
16182e2caf59SThomas Veerman 	    }
16192e2caf59SThomas Veerman 
16202e2caf59SThomas Veerman 	    if (cp != start) {
16212e2caf59SThomas Veerman 		/*
16222e2caf59SThomas Veerman 		 * Stuff left over -- add it to the list too
16232e2caf59SThomas Veerman 		 */
16242e2caf59SThomas Veerman 		gn = Targ_FindNode(start, TARG_CREATE);
16252e2caf59SThomas Veerman 		(void)Lst_AtEnd(members, gn);
16262e2caf59SThomas Veerman 	    }
16272e2caf59SThomas Veerman 	    /*
16282e2caf59SThomas Veerman 	     * Point cp back at the beginning again so the variable value
16292e2caf59SThomas Veerman 	     * can be freed.
16302e2caf59SThomas Veerman 	     */
16312e2caf59SThomas Veerman 	    cp = initcp;
16322e2caf59SThomas Veerman 	}
16332e2caf59SThomas Veerman 
16342e2caf59SThomas Veerman 	/*
16352e2caf59SThomas Veerman 	 * Add all elements of the members list to the parent node.
16362e2caf59SThomas Veerman 	 */
16372e2caf59SThomas Veerman 	while(!Lst_IsEmpty(members)) {
16382e2caf59SThomas Veerman 	    gn = (GNode *)Lst_DeQueue(members);
16392e2caf59SThomas Veerman 
16402e2caf59SThomas Veerman 	    if (DEBUG(SUFF)) {
16412e2caf59SThomas Veerman 		fprintf(debug_file, "%s...", gn->name);
16422e2caf59SThomas Veerman 	    }
16432e2caf59SThomas Veerman 	    /* Add gn to the parents child list before the original child */
16442e2caf59SThomas Veerman 	    (void)Lst_InsertBefore(pgn->children, cln, gn);
16452e2caf59SThomas Veerman 	    (void)Lst_AtEnd(gn->parents, pgn);
16462e2caf59SThomas Veerman 	    pgn->unmade++;
16472e2caf59SThomas Veerman 	    /* Expand wildcards on new node */
16482e2caf59SThomas Veerman 	    SuffExpandWildcards(Lst_Prev(cln), pgn);
16492e2caf59SThomas Veerman 	}
16502e2caf59SThomas Veerman 	Lst_Destroy(members, NULL);
16512e2caf59SThomas Veerman 
16522e2caf59SThomas Veerman 	/*
16532e2caf59SThomas Veerman 	 * Free the result
16542e2caf59SThomas Veerman 	 */
16552e2caf59SThomas Veerman 	free(cp);
16562e2caf59SThomas Veerman     }
16572e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
16582e2caf59SThomas Veerman 	fprintf(debug_file, "\n");
16592e2caf59SThomas Veerman     }
16602e2caf59SThomas Veerman 
16612e2caf59SThomas Veerman     /*
16622e2caf59SThomas Veerman      * Now the source is expanded, remove it from the list of children to
16632e2caf59SThomas Veerman      * keep it from being processed.
16642e2caf59SThomas Veerman      */
16652e2caf59SThomas Veerman     pgn->unmade--;
16662e2caf59SThomas Veerman     Lst_Remove(pgn->children, cln);
16672e2caf59SThomas Veerman     Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
16682e2caf59SThomas Veerman }
16692e2caf59SThomas Veerman 
16702e2caf59SThomas Veerman static void
SuffExpandWildcards(LstNode cln,GNode * pgn)16712e2caf59SThomas Veerman SuffExpandWildcards(LstNode cln, GNode *pgn)
16722e2caf59SThomas Veerman {
16732e2caf59SThomas Veerman     GNode   	*cgn = (GNode *)Lst_Datum(cln);
16742e2caf59SThomas Veerman     GNode	*gn;	    /* New source 8) */
16752e2caf59SThomas Veerman     char	*cp;	    /* Expanded value */
16762e2caf59SThomas Veerman     Lst 	explist;    /* List of expansions */
16772e2caf59SThomas Veerman 
16782e2caf59SThomas Veerman     if (!Dir_HasWildcards(cgn->name))
16792e2caf59SThomas Veerman 	return;
16802e2caf59SThomas Veerman 
16812e2caf59SThomas Veerman     /*
16822e2caf59SThomas Veerman      * Expand the word along the chosen path
16832e2caf59SThomas Veerman      */
16842e2caf59SThomas Veerman     explist = Lst_Init(FALSE);
16852e2caf59SThomas Veerman     Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
16862e2caf59SThomas Veerman 
16872e2caf59SThomas Veerman     while (!Lst_IsEmpty(explist)) {
16882e2caf59SThomas Veerman 	/*
16892e2caf59SThomas Veerman 	 * Fetch next expansion off the list and find its GNode
16902e2caf59SThomas Veerman 	 */
16912e2caf59SThomas Veerman 	cp = (char *)Lst_DeQueue(explist);
16922e2caf59SThomas Veerman 
16932e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
16942e2caf59SThomas Veerman 	    fprintf(debug_file, "%s...", cp);
16952e2caf59SThomas Veerman 	}
16962e2caf59SThomas Veerman 	gn = Targ_FindNode(cp, TARG_CREATE);
16972e2caf59SThomas Veerman 
16982e2caf59SThomas Veerman 	/* Add gn to the parents child list before the original child */
16992e2caf59SThomas Veerman 	(void)Lst_InsertBefore(pgn->children, cln, gn);
17002e2caf59SThomas Veerman 	(void)Lst_AtEnd(gn->parents, pgn);
17012e2caf59SThomas Veerman 	pgn->unmade++;
17022e2caf59SThomas Veerman     }
17032e2caf59SThomas Veerman 
17042e2caf59SThomas Veerman     /*
17052e2caf59SThomas Veerman      * Nuke what's left of the list
17062e2caf59SThomas Veerman      */
17072e2caf59SThomas Veerman     Lst_Destroy(explist, NULL);
17082e2caf59SThomas Veerman 
17092e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
17102e2caf59SThomas Veerman 	fprintf(debug_file, "\n");
17112e2caf59SThomas Veerman     }
17122e2caf59SThomas Veerman 
17132e2caf59SThomas Veerman     /*
17142e2caf59SThomas Veerman      * Now the source is expanded, remove it from the list of children to
17152e2caf59SThomas Veerman      * keep it from being processed.
17162e2caf59SThomas Veerman      */
17172e2caf59SThomas Veerman     pgn->unmade--;
17182e2caf59SThomas Veerman     Lst_Remove(pgn->children, cln);
17192e2caf59SThomas Veerman     Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
17202e2caf59SThomas Veerman }
17212e2caf59SThomas Veerman 
17222e2caf59SThomas Veerman /*-
17232e2caf59SThomas Veerman  *-----------------------------------------------------------------------
17242e2caf59SThomas Veerman  * Suff_FindPath --
17252e2caf59SThomas Veerman  *	Find a path along which to expand the node.
17262e2caf59SThomas Veerman  *
17272e2caf59SThomas Veerman  *	If the word has a known suffix, use that path.
17282e2caf59SThomas Veerman  *	If it has no known suffix, use the default system search path.
17292e2caf59SThomas Veerman  *
17302e2caf59SThomas Veerman  * Input:
17312e2caf59SThomas Veerman  *	gn		Node being examined
17322e2caf59SThomas Veerman  *
17332e2caf59SThomas Veerman  * Results:
17342e2caf59SThomas Veerman  *	The appropriate path to search for the GNode.
17352e2caf59SThomas Veerman  *
17362e2caf59SThomas Veerman  * Side Effects:
17372e2caf59SThomas Veerman  *	XXX: We could set the suffix here so that we don't have to scan
17382e2caf59SThomas Veerman  *	again.
17392e2caf59SThomas Veerman  *
17402e2caf59SThomas Veerman  *-----------------------------------------------------------------------
17412e2caf59SThomas Veerman  */
17422e2caf59SThomas Veerman Lst
Suff_FindPath(GNode * gn)17432e2caf59SThomas Veerman Suff_FindPath(GNode* gn)
17442e2caf59SThomas Veerman {
17452e2caf59SThomas Veerman     Suff *suff = gn->suffix;
17462e2caf59SThomas Veerman 
17472e2caf59SThomas Veerman     if (suff == NULL) {
17482e2caf59SThomas Veerman 	SuffixCmpData sd;   /* Search string data */
17492e2caf59SThomas Veerman 	LstNode ln;
17502e2caf59SThomas Veerman 	sd.len = strlen(gn->name);
17512e2caf59SThomas Veerman 	sd.ename = gn->name + sd.len;
17522e2caf59SThomas Veerman 	ln = Lst_Find(sufflist, &sd, SuffSuffIsSuffixP);
17532e2caf59SThomas Veerman 
17542e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
17552e2caf59SThomas Veerman 	    fprintf(debug_file, "Wildcard expanding \"%s\"...", gn->name);
17562e2caf59SThomas Veerman 	}
17572e2caf59SThomas Veerman 	if (ln != NULL)
17582e2caf59SThomas Veerman 	    suff = (Suff *)Lst_Datum(ln);
17592e2caf59SThomas Veerman 	/* XXX: Here we can save the suffix so we don't have to do this again */
17602e2caf59SThomas Veerman     }
17612e2caf59SThomas Veerman 
17622e2caf59SThomas Veerman     if (suff != NULL) {
17632e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
17642e2caf59SThomas Veerman 	    fprintf(debug_file, "suffix is \"%s\"...", suff->name);
17652e2caf59SThomas Veerman 	}
17662e2caf59SThomas Veerman 	return suff->searchPath;
17672e2caf59SThomas Veerman     } else {
17682e2caf59SThomas Veerman 	/*
17692e2caf59SThomas Veerman 	 * Use default search path
17702e2caf59SThomas Veerman 	 */
17712e2caf59SThomas Veerman 	return dirSearchPath;
17722e2caf59SThomas Veerman     }
17732e2caf59SThomas Veerman }
17742e2caf59SThomas Veerman 
17752e2caf59SThomas Veerman /*-
17762e2caf59SThomas Veerman  *-----------------------------------------------------------------------
17772e2caf59SThomas Veerman  * SuffApplyTransform --
17782e2caf59SThomas Veerman  *	Apply a transformation rule, given the source and target nodes
17792e2caf59SThomas Veerman  *	and suffixes.
17802e2caf59SThomas Veerman  *
17812e2caf59SThomas Veerman  * Input:
17822e2caf59SThomas Veerman  *	tGn		Target node
17832e2caf59SThomas Veerman  *	sGn		Source node
17842e2caf59SThomas Veerman  *	t		Target suffix
17852e2caf59SThomas Veerman  *	s		Source suffix
17862e2caf59SThomas Veerman  *
17872e2caf59SThomas Veerman  * Results:
17882e2caf59SThomas Veerman  *	TRUE if successful, FALSE if not.
17892e2caf59SThomas Veerman  *
17902e2caf59SThomas Veerman  * Side Effects:
17912e2caf59SThomas Veerman  *	The source and target are linked and the commands from the
17922e2caf59SThomas Veerman  *	transformation are added to the target node's commands list.
17932e2caf59SThomas Veerman  *	All attributes but OP_DEPMASK and OP_TRANSFORM are applied
17942e2caf59SThomas Veerman  *	to the target. The target also inherits all the sources for
17952e2caf59SThomas Veerman  *	the transformation rule.
17962e2caf59SThomas Veerman  *
17972e2caf59SThomas Veerman  *-----------------------------------------------------------------------
17982e2caf59SThomas Veerman  */
17992e2caf59SThomas Veerman static Boolean
SuffApplyTransform(GNode * tGn,GNode * sGn,Suff * t,Suff * s)18002e2caf59SThomas Veerman SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
18012e2caf59SThomas Veerman {
18022e2caf59SThomas Veerman     LstNode 	ln, nln;    /* General node */
18032e2caf59SThomas Veerman     char    	*tname;	    /* Name of transformation rule */
18042e2caf59SThomas Veerman     GNode   	*gn;	    /* Node for same */
18052e2caf59SThomas Veerman 
18062e2caf59SThomas Veerman     /*
18072e2caf59SThomas Veerman      * Form the proper links between the target and source.
18082e2caf59SThomas Veerman      */
18092e2caf59SThomas Veerman     (void)Lst_AtEnd(tGn->children, sGn);
18102e2caf59SThomas Veerman     (void)Lst_AtEnd(sGn->parents, tGn);
18112e2caf59SThomas Veerman     tGn->unmade += 1;
18122e2caf59SThomas Veerman 
18132e2caf59SThomas Veerman     /*
18142e2caf59SThomas Veerman      * Locate the transformation rule itself
18152e2caf59SThomas Veerman      */
18162e2caf59SThomas Veerman     tname = str_concat(s->name, t->name, 0);
18172e2caf59SThomas Veerman     ln = Lst_Find(transforms, tname, SuffGNHasNameP);
18182e2caf59SThomas Veerman     free(tname);
18192e2caf59SThomas Veerman 
18202e2caf59SThomas Veerman     if (ln == NULL) {
18212e2caf59SThomas Veerman 	/*
18222e2caf59SThomas Veerman 	 * Not really such a transformation rule (can happen when we're
18232e2caf59SThomas Veerman 	 * called to link an OP_MEMBER and OP_ARCHV node), so return
18242e2caf59SThomas Veerman 	 * FALSE.
18252e2caf59SThomas Veerman 	 */
18262e2caf59SThomas Veerman 	return(FALSE);
18272e2caf59SThomas Veerman     }
18282e2caf59SThomas Veerman 
18292e2caf59SThomas Veerman     gn = (GNode *)Lst_Datum(ln);
18302e2caf59SThomas Veerman 
18312e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
18322e2caf59SThomas Veerman 	fprintf(debug_file, "\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
18332e2caf59SThomas Veerman     }
18342e2caf59SThomas Veerman 
18352e2caf59SThomas Veerman     /*
18362e2caf59SThomas Veerman      * Record last child for expansion purposes
18372e2caf59SThomas Veerman      */
18382e2caf59SThomas Veerman     ln = Lst_Last(tGn->children);
18392e2caf59SThomas Veerman 
18402e2caf59SThomas Veerman     /*
18412e2caf59SThomas Veerman      * Pass the buck to Make_HandleUse to apply the rule
18422e2caf59SThomas Veerman      */
18432e2caf59SThomas Veerman     (void)Make_HandleUse(gn, tGn);
18442e2caf59SThomas Veerman 
18452e2caf59SThomas Veerman     /*
18462e2caf59SThomas Veerman      * Deal with wildcards and variables in any acquired sources
18472e2caf59SThomas Veerman      */
18482e2caf59SThomas Veerman     for (ln = Lst_Succ(ln); ln != NULL; ln = nln) {
18492e2caf59SThomas Veerman 	nln = Lst_Succ(ln);
18502e2caf59SThomas Veerman 	SuffExpandChildren(ln, tGn);
18512e2caf59SThomas Veerman     }
18522e2caf59SThomas Veerman 
18532e2caf59SThomas Veerman     /*
18542e2caf59SThomas Veerman      * Keep track of another parent to which this beast is transformed so
18552e2caf59SThomas Veerman      * the .IMPSRC variable can be set correctly for the parent.
18562e2caf59SThomas Veerman      */
18572e2caf59SThomas Veerman     (void)Lst_AtEnd(sGn->iParents, tGn);
18582e2caf59SThomas Veerman 
18592e2caf59SThomas Veerman     return(TRUE);
18602e2caf59SThomas Veerman }
18612e2caf59SThomas Veerman 
18622e2caf59SThomas Veerman 
18632e2caf59SThomas Veerman /*-
18642e2caf59SThomas Veerman  *-----------------------------------------------------------------------
18652e2caf59SThomas Veerman  * SuffFindArchiveDeps --
18662e2caf59SThomas Veerman  *	Locate dependencies for an OP_ARCHV node.
18672e2caf59SThomas Veerman  *
18682e2caf59SThomas Veerman  * Input:
18692e2caf59SThomas Veerman  *	gn		Node for which to locate dependencies
18702e2caf59SThomas Veerman  *
18712e2caf59SThomas Veerman  * Results:
18722e2caf59SThomas Veerman  *	None
18732e2caf59SThomas Veerman  *
18742e2caf59SThomas Veerman  * Side Effects:
18752e2caf59SThomas Veerman  *	Same as Suff_FindDeps
18762e2caf59SThomas Veerman  *
18772e2caf59SThomas Veerman  *-----------------------------------------------------------------------
18782e2caf59SThomas Veerman  */
18792e2caf59SThomas Veerman static void
SuffFindArchiveDeps(GNode * gn,Lst slst)18802e2caf59SThomas Veerman SuffFindArchiveDeps(GNode *gn, Lst slst)
18812e2caf59SThomas Veerman {
18822e2caf59SThomas Veerman     char    	*eoarch;    /* End of archive portion */
18832e2caf59SThomas Veerman     char    	*eoname;    /* End of member portion */
18842e2caf59SThomas Veerman     GNode   	*mem;	    /* Node for member */
18852e2caf59SThomas Veerman     static const char	*copy[] = {
18862e2caf59SThomas Veerman 	/* Variables to be copied from the member node */
18872e2caf59SThomas Veerman 	TARGET,	    	    /* Must be first */
18882e2caf59SThomas Veerman 	PREFIX,	    	    /* Must be second */
18892e2caf59SThomas Veerman     };
18902e2caf59SThomas Veerman     int	    	i;  	    /* Index into copy and vals */
18912e2caf59SThomas Veerman     Suff    	*ms;	    /* Suffix descriptor for member */
18922e2caf59SThomas Veerman     char    	*name;	    /* Start of member's name */
18932e2caf59SThomas Veerman 
18942e2caf59SThomas Veerman     /*
18952e2caf59SThomas Veerman      * The node is an archive(member) pair. so we must find a
18962e2caf59SThomas Veerman      * suffix for both of them.
18972e2caf59SThomas Veerman      */
18982e2caf59SThomas Veerman     eoarch = strchr(gn->name, '(');
18992e2caf59SThomas Veerman     eoname = strchr(eoarch, ')');
19002e2caf59SThomas Veerman 
19012e2caf59SThomas Veerman     *eoname = '\0';	  /* Nuke parentheses during suffix search */
19022e2caf59SThomas Veerman     *eoarch = '\0';	  /* So a suffix can be found */
19032e2caf59SThomas Veerman 
19042e2caf59SThomas Veerman     name = eoarch + 1;
19052e2caf59SThomas Veerman 
19062e2caf59SThomas Veerman     /*
19072e2caf59SThomas Veerman      * To simplify things, call Suff_FindDeps recursively on the member now,
19082e2caf59SThomas Veerman      * so we can simply compare the member's .PREFIX and .TARGET variables
19092e2caf59SThomas Veerman      * to locate its suffix. This allows us to figure out the suffix to
19102e2caf59SThomas Veerman      * use for the archive without having to do a quadratic search over the
19112e2caf59SThomas Veerman      * suffix list, backtracking for each one...
19122e2caf59SThomas Veerman      */
19132e2caf59SThomas Veerman     mem = Targ_FindNode(name, TARG_CREATE);
19142e2caf59SThomas Veerman     SuffFindDeps(mem, slst);
19152e2caf59SThomas Veerman 
19162e2caf59SThomas Veerman     /*
19172e2caf59SThomas Veerman      * Create the link between the two nodes right off
19182e2caf59SThomas Veerman      */
19192e2caf59SThomas Veerman     (void)Lst_AtEnd(gn->children, mem);
19202e2caf59SThomas Veerman     (void)Lst_AtEnd(mem->parents, gn);
19212e2caf59SThomas Veerman     gn->unmade += 1;
19222e2caf59SThomas Veerman 
19232e2caf59SThomas Veerman     /*
19242e2caf59SThomas Veerman      * Copy in the variables from the member node to this one.
19252e2caf59SThomas Veerman      */
19262e2caf59SThomas Veerman     for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
19272e2caf59SThomas Veerman 	char *p1;
19282e2caf59SThomas Veerman 	Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn, 0);
19292e2caf59SThomas Veerman 	if (p1)
19302e2caf59SThomas Veerman 	    free(p1);
19312e2caf59SThomas Veerman 
19322e2caf59SThomas Veerman     }
19332e2caf59SThomas Veerman 
19342e2caf59SThomas Veerman     ms = mem->suffix;
19352e2caf59SThomas Veerman     if (ms == NULL) {
19362e2caf59SThomas Veerman 	/*
19372e2caf59SThomas Veerman 	 * Didn't know what it was -- use .NULL suffix if not in make mode
19382e2caf59SThomas Veerman 	 */
19392e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
19402e2caf59SThomas Veerman 	    fprintf(debug_file, "using null suffix\n");
19412e2caf59SThomas Veerman 	}
19422e2caf59SThomas Veerman 	ms = suffNull;
19432e2caf59SThomas Veerman     }
19442e2caf59SThomas Veerman 
19452e2caf59SThomas Veerman 
19462e2caf59SThomas Veerman     /*
19472e2caf59SThomas Veerman      * Set the other two local variables required for this target.
19482e2caf59SThomas Veerman      */
19492e2caf59SThomas Veerman     Var_Set(MEMBER, name, gn, 0);
19502e2caf59SThomas Veerman     Var_Set(ARCHIVE, gn->name, gn, 0);
19512e2caf59SThomas Veerman 
19522e2caf59SThomas Veerman     if (ms != NULL) {
19532e2caf59SThomas Veerman 	/*
19542e2caf59SThomas Veerman 	 * Member has a known suffix, so look for a transformation rule from
19552e2caf59SThomas Veerman 	 * it to a possible suffix of the archive. Rather than searching
19562e2caf59SThomas Veerman 	 * through the entire list, we just look at suffixes to which the
19572e2caf59SThomas Veerman 	 * member's suffix may be transformed...
19582e2caf59SThomas Veerman 	 */
19592e2caf59SThomas Veerman 	LstNode		ln;
19602e2caf59SThomas Veerman 	SuffixCmpData	sd;		/* Search string data */
19612e2caf59SThomas Veerman 
19622e2caf59SThomas Veerman 	/*
19632e2caf59SThomas Veerman 	 * Use first matching suffix...
19642e2caf59SThomas Veerman 	 */
19652e2caf59SThomas Veerman 	sd.len = eoarch - gn->name;
19662e2caf59SThomas Veerman 	sd.ename = eoarch;
19672e2caf59SThomas Veerman 	ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP);
19682e2caf59SThomas Veerman 
19692e2caf59SThomas Veerman 	if (ln != NULL) {
19702e2caf59SThomas Veerman 	    /*
19712e2caf59SThomas Veerman 	     * Got one -- apply it
19722e2caf59SThomas Veerman 	     */
19732e2caf59SThomas Veerman 	    if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
19742e2caf59SThomas Veerman 		DEBUG(SUFF))
19752e2caf59SThomas Veerman 	    {
19762e2caf59SThomas Veerman 		fprintf(debug_file, "\tNo transformation from %s -> %s\n",
19772e2caf59SThomas Veerman 		       ms->name, ((Suff *)Lst_Datum(ln))->name);
19782e2caf59SThomas Veerman 	    }
19792e2caf59SThomas Veerman 	}
19802e2caf59SThomas Veerman     }
19812e2caf59SThomas Veerman 
19822e2caf59SThomas Veerman     /*
19832e2caf59SThomas Veerman      * Replace the opening and closing parens now we've no need of the separate
19842e2caf59SThomas Veerman      * pieces.
19852e2caf59SThomas Veerman      */
19862e2caf59SThomas Veerman     *eoarch = '('; *eoname = ')';
19872e2caf59SThomas Veerman 
19882e2caf59SThomas Veerman     /*
19892e2caf59SThomas Veerman      * Pretend gn appeared to the left of a dependency operator so
19902e2caf59SThomas Veerman      * the user needn't provide a transformation from the member to the
19912e2caf59SThomas Veerman      * archive.
19922e2caf59SThomas Veerman      */
19932e2caf59SThomas Veerman     if (OP_NOP(gn->type)) {
19942e2caf59SThomas Veerman 	gn->type |= OP_DEPENDS;
19952e2caf59SThomas Veerman     }
19962e2caf59SThomas Veerman 
19972e2caf59SThomas Veerman     /*
19982e2caf59SThomas Veerman      * Flag the member as such so we remember to look in the archive for
19992e2caf59SThomas Veerman      * its modification time.
20002e2caf59SThomas Veerman      */
20012e2caf59SThomas Veerman     mem->type |= OP_MEMBER;
20022e2caf59SThomas Veerman }
20032e2caf59SThomas Veerman 
20042e2caf59SThomas Veerman /*-
20052e2caf59SThomas Veerman  *-----------------------------------------------------------------------
20062e2caf59SThomas Veerman  * SuffFindNormalDeps --
20072e2caf59SThomas Veerman  *	Locate implicit dependencies for regular targets.
20082e2caf59SThomas Veerman  *
20092e2caf59SThomas Veerman  * Input:
20102e2caf59SThomas Veerman  *	gn		Node for which to find sources
20112e2caf59SThomas Veerman  *
20122e2caf59SThomas Veerman  * Results:
20132e2caf59SThomas Veerman  *	None.
20142e2caf59SThomas Veerman  *
20152e2caf59SThomas Veerman  * Side Effects:
20162e2caf59SThomas Veerman  *	Same as Suff_FindDeps...
20172e2caf59SThomas Veerman  *
20182e2caf59SThomas Veerman  *-----------------------------------------------------------------------
20192e2caf59SThomas Veerman  */
20202e2caf59SThomas Veerman static void
SuffFindNormalDeps(GNode * gn,Lst slst)20212e2caf59SThomas Veerman SuffFindNormalDeps(GNode *gn, Lst slst)
20222e2caf59SThomas Veerman {
20232e2caf59SThomas Veerman     char    	*eoname;    /* End of name */
20242e2caf59SThomas Veerman     char    	*sopref;    /* Start of prefix */
20252e2caf59SThomas Veerman     LstNode 	ln, nln;    /* Next suffix node to check */
20262e2caf59SThomas Veerman     Lst	    	srcs;	    /* List of sources at which to look */
20272e2caf59SThomas Veerman     Lst	    	targs;	    /* List of targets to which things can be
20282e2caf59SThomas Veerman 			     * transformed. They all have the same file,
20292e2caf59SThomas Veerman 			     * but different suff and pref fields */
20302e2caf59SThomas Veerman     Src	    	*bottom;    /* Start of found transformation path */
20312e2caf59SThomas Veerman     Src 	*src;	    /* General Src pointer */
20322e2caf59SThomas Veerman     char    	*pref;	    /* Prefix to use */
20332e2caf59SThomas Veerman     Src	    	*targ;	    /* General Src target pointer */
20342e2caf59SThomas Veerman     SuffixCmpData sd;	    /* Search string data */
20352e2caf59SThomas Veerman 
20362e2caf59SThomas Veerman 
20372e2caf59SThomas Veerman     sd.len = strlen(gn->name);
20382e2caf59SThomas Veerman     sd.ename = eoname = gn->name + sd.len;
20392e2caf59SThomas Veerman 
20402e2caf59SThomas Veerman     sopref = gn->name;
20412e2caf59SThomas Veerman 
20422e2caf59SThomas Veerman     /*
20432e2caf59SThomas Veerman      * Begin at the beginning...
20442e2caf59SThomas Veerman      */
20452e2caf59SThomas Veerman     ln = Lst_First(sufflist);
20462e2caf59SThomas Veerman     srcs = Lst_Init(FALSE);
20472e2caf59SThomas Veerman     targs = Lst_Init(FALSE);
20482e2caf59SThomas Veerman 
20492e2caf59SThomas Veerman     /*
20502e2caf59SThomas Veerman      * We're caught in a catch-22 here. On the one hand, we want to use any
20512e2caf59SThomas Veerman      * transformation implied by the target's sources, but we can't examine
20522e2caf59SThomas Veerman      * the sources until we've expanded any variables/wildcards they may hold,
20532e2caf59SThomas Veerman      * and we can't do that until we've set up the target's local variables
20542e2caf59SThomas Veerman      * and we can't do that until we know what the proper suffix for the
20552e2caf59SThomas Veerman      * target is (in case there are two suffixes one of which is a suffix of
20562e2caf59SThomas Veerman      * the other) and we can't know that until we've found its implied
20572e2caf59SThomas Veerman      * source, which we may not want to use if there's an existing source
20582e2caf59SThomas Veerman      * that implies a different transformation.
20592e2caf59SThomas Veerman      *
20602e2caf59SThomas Veerman      * In an attempt to get around this, which may not work all the time,
20612e2caf59SThomas Veerman      * but should work most of the time, we look for implied sources first,
20622e2caf59SThomas Veerman      * checking transformations to all possible suffixes of the target,
20632e2caf59SThomas Veerman      * use what we find to set the target's local variables, expand the
20642e2caf59SThomas Veerman      * children, then look for any overriding transformations they imply.
20652e2caf59SThomas Veerman      * Should we find one, we discard the one we found before.
20662e2caf59SThomas Veerman      */
206784d9c625SLionel Sambuc     bottom = NULL;
206884d9c625SLionel Sambuc     targ = NULL;
206984d9c625SLionel Sambuc 
207084d9c625SLionel Sambuc     if (!(gn->type & OP_PHONY)) {
20712e2caf59SThomas Veerman 
20722e2caf59SThomas Veerman 	while (ln != NULL) {
20732e2caf59SThomas Veerman 	    /*
20742e2caf59SThomas Veerman 	     * Look for next possible suffix...
20752e2caf59SThomas Veerman 	     */
20762e2caf59SThomas Veerman 	    ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP);
20772e2caf59SThomas Veerman 
20782e2caf59SThomas Veerman 	    if (ln != NULL) {
20792e2caf59SThomas Veerman 		int	    prefLen;	    /* Length of the prefix */
20802e2caf59SThomas Veerman 
20812e2caf59SThomas Veerman 		/*
20822e2caf59SThomas Veerman 		 * Allocate a Src structure to which things can be transformed
20832e2caf59SThomas Veerman 		 */
20842e2caf59SThomas Veerman 		targ = bmake_malloc(sizeof(Src));
20852e2caf59SThomas Veerman 		targ->file = bmake_strdup(gn->name);
20862e2caf59SThomas Veerman 		targ->suff = (Suff *)Lst_Datum(ln);
20872e2caf59SThomas Veerman 		targ->suff->refCount++;
20882e2caf59SThomas Veerman 		targ->node = gn;
20892e2caf59SThomas Veerman 		targ->parent = NULL;
20902e2caf59SThomas Veerman 		targ->children = 0;
20912e2caf59SThomas Veerman #ifdef DEBUG_SRC
20922e2caf59SThomas Veerman 		targ->cp = Lst_Init(FALSE);
20932e2caf59SThomas Veerman #endif
20942e2caf59SThomas Veerman 
20952e2caf59SThomas Veerman 		/*
209684d9c625SLionel Sambuc 		 * Allocate room for the prefix, whose end is found by
209784d9c625SLionel Sambuc 		 * subtracting the length of the suffix from
209884d9c625SLionel Sambuc 		 * the end of the name.
20992e2caf59SThomas Veerman 		 */
21002e2caf59SThomas Veerman 		prefLen = (eoname - targ->suff->nameLen) - sopref;
21012e2caf59SThomas Veerman 		targ->pref = bmake_malloc(prefLen + 1);
21022e2caf59SThomas Veerman 		memcpy(targ->pref, sopref, prefLen);
21032e2caf59SThomas Veerman 		targ->pref[prefLen] = '\0';
21042e2caf59SThomas Veerman 
21052e2caf59SThomas Veerman 		/*
21062e2caf59SThomas Veerman 		 * Add nodes from which the target can be made
21072e2caf59SThomas Veerman 		 */
21082e2caf59SThomas Veerman 		SuffAddLevel(srcs, targ);
21092e2caf59SThomas Veerman 
21102e2caf59SThomas Veerman 		/*
21112e2caf59SThomas Veerman 		 * Record the target so we can nuke it
21122e2caf59SThomas Veerman 		 */
21132e2caf59SThomas Veerman 		(void)Lst_AtEnd(targs, targ);
21142e2caf59SThomas Veerman 
21152e2caf59SThomas Veerman 		/*
21162e2caf59SThomas Veerman 		 * Search from this suffix's successor...
21172e2caf59SThomas Veerman 		 */
21182e2caf59SThomas Veerman 		ln = Lst_Succ(ln);
21192e2caf59SThomas Veerman 	    }
21202e2caf59SThomas Veerman 	}
21212e2caf59SThomas Veerman 
21222e2caf59SThomas Veerman 	/*
21232e2caf59SThomas Veerman 	 * Handle target of unknown suffix...
21242e2caf59SThomas Veerman 	 */
21252e2caf59SThomas Veerman 	if (Lst_IsEmpty(targs) && suffNull != NULL) {
21262e2caf59SThomas Veerman 	    if (DEBUG(SUFF)) {
21272e2caf59SThomas Veerman 		fprintf(debug_file, "\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
21282e2caf59SThomas Veerman 	    }
21292e2caf59SThomas Veerman 
21302e2caf59SThomas Veerman 	    targ = bmake_malloc(sizeof(Src));
21312e2caf59SThomas Veerman 	    targ->file = bmake_strdup(gn->name);
21322e2caf59SThomas Veerman 	    targ->suff = suffNull;
21332e2caf59SThomas Veerman 	    targ->suff->refCount++;
21342e2caf59SThomas Veerman 	    targ->node = gn;
21352e2caf59SThomas Veerman 	    targ->parent = NULL;
21362e2caf59SThomas Veerman 	    targ->children = 0;
21372e2caf59SThomas Veerman 	    targ->pref = bmake_strdup(sopref);
21382e2caf59SThomas Veerman #ifdef DEBUG_SRC
21392e2caf59SThomas Veerman 	    targ->cp = Lst_Init(FALSE);
21402e2caf59SThomas Veerman #endif
21412e2caf59SThomas Veerman 
21422e2caf59SThomas Veerman 	    /*
21432e2caf59SThomas Veerman 	     * Only use the default suffix rules if we don't have commands
21442e2caf59SThomas Veerman 	     * defined for this gnode; traditional make programs used to
21452e2caf59SThomas Veerman 	     * not define suffix rules if the gnode had children but we
21462e2caf59SThomas Veerman 	     * don't do this anymore.
21472e2caf59SThomas Veerman 	     */
21482e2caf59SThomas Veerman 	    if (Lst_IsEmpty(gn->commands))
21492e2caf59SThomas Veerman 		SuffAddLevel(srcs, targ);
21502e2caf59SThomas Veerman 	    else {
21512e2caf59SThomas Veerman 		if (DEBUG(SUFF))
21522e2caf59SThomas Veerman 		    fprintf(debug_file, "not ");
21532e2caf59SThomas Veerman 	    }
21542e2caf59SThomas Veerman 
21552e2caf59SThomas Veerman 	    if (DEBUG(SUFF))
21562e2caf59SThomas Veerman 		fprintf(debug_file, "adding suffix rules\n");
21572e2caf59SThomas Veerman 
21582e2caf59SThomas Veerman 	    (void)Lst_AtEnd(targs, targ);
21592e2caf59SThomas Veerman 	}
21602e2caf59SThomas Veerman 
21612e2caf59SThomas Veerman 	/*
216284d9c625SLionel Sambuc 	 * Using the list of possible sources built up from the target
216384d9c625SLionel Sambuc 	 * suffix(es), try and find an existing file/target that matches.
21642e2caf59SThomas Veerman 	 */
21652e2caf59SThomas Veerman 	bottom = SuffFindThem(srcs, slst);
21662e2caf59SThomas Veerman 
21672e2caf59SThomas Veerman 	if (bottom == NULL) {
21682e2caf59SThomas Veerman 	    /*
216984d9c625SLionel Sambuc 	     * No known transformations -- use the first suffix found
217084d9c625SLionel Sambuc 	     * for setting the local variables.
21712e2caf59SThomas Veerman 	     */
21722e2caf59SThomas Veerman 	    if (!Lst_IsEmpty(targs)) {
21732e2caf59SThomas Veerman 		targ = (Src *)Lst_Datum(Lst_First(targs));
21742e2caf59SThomas Veerman 	    } else {
21752e2caf59SThomas Veerman 		targ = NULL;
21762e2caf59SThomas Veerman 	    }
21772e2caf59SThomas Veerman 	} else {
21782e2caf59SThomas Veerman 	    /*
21792e2caf59SThomas Veerman 	     * Work up the transformation path to find the suffix of the
21802e2caf59SThomas Veerman 	     * target to which the transformation was made.
21812e2caf59SThomas Veerman 	     */
21822e2caf59SThomas Veerman 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
21832e2caf59SThomas Veerman 		continue;
21842e2caf59SThomas Veerman 	}
218584d9c625SLionel Sambuc     }
21862e2caf59SThomas Veerman 
21872e2caf59SThomas Veerman     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
21882e2caf59SThomas Veerman 
21892e2caf59SThomas Veerman     pref = (targ != NULL) ? targ->pref : gn->name;
21902e2caf59SThomas Veerman     Var_Set(PREFIX, pref, gn, 0);
21912e2caf59SThomas Veerman 
21922e2caf59SThomas Veerman     /*
21932e2caf59SThomas Veerman      * Now we've got the important local variables set, expand any sources
21942e2caf59SThomas Veerman      * that still contain variables or wildcards in their names.
21952e2caf59SThomas Veerman      */
21962e2caf59SThomas Veerman     for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
21972e2caf59SThomas Veerman 	nln = Lst_Succ(ln);
21982e2caf59SThomas Veerman 	SuffExpandChildren(ln, gn);
21992e2caf59SThomas Veerman     }
22002e2caf59SThomas Veerman 
22012e2caf59SThomas Veerman     if (targ == NULL) {
22022e2caf59SThomas Veerman 	if (DEBUG(SUFF)) {
22032e2caf59SThomas Veerman 	    fprintf(debug_file, "\tNo valid suffix on %s\n", gn->name);
22042e2caf59SThomas Veerman 	}
22052e2caf59SThomas Veerman 
22062e2caf59SThomas Veerman sfnd_abort:
22072e2caf59SThomas Veerman 	/*
22082e2caf59SThomas Veerman 	 * Deal with finding the thing on the default search path. We
22092e2caf59SThomas Veerman 	 * always do that, not only if the node is only a source (not
22102e2caf59SThomas Veerman 	 * on the lhs of a dependency operator or [XXX] it has neither
22112e2caf59SThomas Veerman 	 * children or commands) as the old pmake did.
22122e2caf59SThomas Veerman 	 */
22132e2caf59SThomas Veerman 	if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
22142e2caf59SThomas Veerman 	    free(gn->path);
22152e2caf59SThomas Veerman 	    gn->path = Dir_FindFile(gn->name,
22162e2caf59SThomas Veerman 				    (targ == NULL ? dirSearchPath :
22172e2caf59SThomas Veerman 				     targ->suff->searchPath));
22182e2caf59SThomas Veerman 	    if (gn->path != NULL) {
22192e2caf59SThomas Veerman 		char *ptr;
22202e2caf59SThomas Veerman 		Var_Set(TARGET, gn->path, gn, 0);
22212e2caf59SThomas Veerman 
22222e2caf59SThomas Veerman 		if (targ != NULL) {
22232e2caf59SThomas Veerman 		    /*
22242e2caf59SThomas Veerman 		     * Suffix known for the thing -- trim the suffix off
22252e2caf59SThomas Veerman 		     * the path to form the proper .PREFIX variable.
22262e2caf59SThomas Veerman 		     */
22272e2caf59SThomas Veerman 		    int     savep = strlen(gn->path) - targ->suff->nameLen;
22282e2caf59SThomas Veerman 		    char    savec;
22292e2caf59SThomas Veerman 
22302e2caf59SThomas Veerman 		    if (gn->suffix)
22312e2caf59SThomas Veerman 			gn->suffix->refCount--;
22322e2caf59SThomas Veerman 		    gn->suffix = targ->suff;
22332e2caf59SThomas Veerman 		    gn->suffix->refCount++;
22342e2caf59SThomas Veerman 
22352e2caf59SThomas Veerman 		    savec = gn->path[savep];
22362e2caf59SThomas Veerman 		    gn->path[savep] = '\0';
22372e2caf59SThomas Veerman 
22382e2caf59SThomas Veerman 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
22392e2caf59SThomas Veerman 			ptr++;
22402e2caf59SThomas Veerman 		    else
22412e2caf59SThomas Veerman 			ptr = gn->path;
22422e2caf59SThomas Veerman 
22432e2caf59SThomas Veerman 		    Var_Set(PREFIX, ptr, gn, 0);
22442e2caf59SThomas Veerman 
22452e2caf59SThomas Veerman 		    gn->path[savep] = savec;
22462e2caf59SThomas Veerman 		} else {
22472e2caf59SThomas Veerman 		    /*
22482e2caf59SThomas Veerman 		     * The .PREFIX gets the full path if the target has
22492e2caf59SThomas Veerman 		     * no known suffix.
22502e2caf59SThomas Veerman 		     */
22512e2caf59SThomas Veerman 		    if (gn->suffix)
22522e2caf59SThomas Veerman 			gn->suffix->refCount--;
22532e2caf59SThomas Veerman 		    gn->suffix = NULL;
22542e2caf59SThomas Veerman 
22552e2caf59SThomas Veerman 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
22562e2caf59SThomas Veerman 			ptr++;
22572e2caf59SThomas Veerman 		    else
22582e2caf59SThomas Veerman 			ptr = gn->path;
22592e2caf59SThomas Veerman 
22602e2caf59SThomas Veerman 		    Var_Set(PREFIX, ptr, gn, 0);
22612e2caf59SThomas Veerman 		}
22622e2caf59SThomas Veerman 	    }
22632e2caf59SThomas Veerman 	}
22642e2caf59SThomas Veerman 
22652e2caf59SThomas Veerman 	goto sfnd_return;
22662e2caf59SThomas Veerman     }
22672e2caf59SThomas Veerman 
22682e2caf59SThomas Veerman     /*
22692e2caf59SThomas Veerman      * If the suffix indicates that the target is a library, mark that in
22702e2caf59SThomas Veerman      * the node's type field.
22712e2caf59SThomas Veerman      */
22722e2caf59SThomas Veerman     if (targ->suff->flags & SUFF_LIBRARY) {
22732e2caf59SThomas Veerman 	gn->type |= OP_LIB;
22742e2caf59SThomas Veerman     }
22752e2caf59SThomas Veerman 
22762e2caf59SThomas Veerman     /*
22772e2caf59SThomas Veerman      * Check for overriding transformation rule implied by sources
22782e2caf59SThomas Veerman      */
22792e2caf59SThomas Veerman     if (!Lst_IsEmpty(gn->children)) {
22802e2caf59SThomas Veerman 	src = SuffFindCmds(targ, slst);
22812e2caf59SThomas Veerman 
22822e2caf59SThomas Veerman 	if (src != NULL) {
22832e2caf59SThomas Veerman 	    /*
22842e2caf59SThomas Veerman 	     * Free up all the Src structures in the transformation path
22852e2caf59SThomas Veerman 	     * up to, but not including, the parent node.
22862e2caf59SThomas Veerman 	     */
22872e2caf59SThomas Veerman 	    while (bottom && bottom->parent != NULL) {
22882e2caf59SThomas Veerman 		if (Lst_Member(slst, bottom) == NULL) {
22892e2caf59SThomas Veerman 		    Lst_AtEnd(slst, bottom);
22902e2caf59SThomas Veerman 		}
22912e2caf59SThomas Veerman 		bottom = bottom->parent;
22922e2caf59SThomas Veerman 	    }
22932e2caf59SThomas Veerman 	    bottom = src;
22942e2caf59SThomas Veerman 	}
22952e2caf59SThomas Veerman     }
22962e2caf59SThomas Veerman 
22972e2caf59SThomas Veerman     if (bottom == NULL) {
22982e2caf59SThomas Veerman 	/*
22992e2caf59SThomas Veerman 	 * No idea from where it can come -- return now.
23002e2caf59SThomas Veerman 	 */
23012e2caf59SThomas Veerman 	goto sfnd_abort;
23022e2caf59SThomas Veerman     }
23032e2caf59SThomas Veerman 
23042e2caf59SThomas Veerman     /*
23052e2caf59SThomas Veerman      * We now have a list of Src structures headed by 'bottom' and linked via
23062e2caf59SThomas Veerman      * their 'parent' pointers. What we do next is create links between
23072e2caf59SThomas Veerman      * source and target nodes (which may or may not have been created)
23082e2caf59SThomas Veerman      * and set the necessary local variables in each target. The
23092e2caf59SThomas Veerman      * commands for each target are set from the commands of the
23102e2caf59SThomas Veerman      * transformation rule used to get from the src suffix to the targ
23112e2caf59SThomas Veerman      * suffix. Note that this causes the commands list of the original
23122e2caf59SThomas Veerman      * node, gn, to be replaced by the commands of the final
23132e2caf59SThomas Veerman      * transformation rule. Also, the unmade field of gn is incremented.
23142e2caf59SThomas Veerman      * Etc.
23152e2caf59SThomas Veerman      */
23162e2caf59SThomas Veerman     if (bottom->node == NULL) {
23172e2caf59SThomas Veerman 	bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
23182e2caf59SThomas Veerman     }
23192e2caf59SThomas Veerman 
23202e2caf59SThomas Veerman     for (src = bottom; src->parent != NULL; src = src->parent) {
23212e2caf59SThomas Veerman 	targ = src->parent;
23222e2caf59SThomas Veerman 
23232e2caf59SThomas Veerman 	if (src->node->suffix)
23242e2caf59SThomas Veerman 	    src->node->suffix->refCount--;
23252e2caf59SThomas Veerman 	src->node->suffix = src->suff;
23262e2caf59SThomas Veerman 	src->node->suffix->refCount++;
23272e2caf59SThomas Veerman 
23282e2caf59SThomas Veerman 	if (targ->node == NULL) {
23292e2caf59SThomas Veerman 	    targ->node = Targ_FindNode(targ->file, TARG_CREATE);
23302e2caf59SThomas Veerman 	}
23312e2caf59SThomas Veerman 
23322e2caf59SThomas Veerman 	SuffApplyTransform(targ->node, src->node,
23332e2caf59SThomas Veerman 			   targ->suff, src->suff);
23342e2caf59SThomas Veerman 
23352e2caf59SThomas Veerman 	if (targ->node != gn) {
23362e2caf59SThomas Veerman 	    /*
23372e2caf59SThomas Veerman 	     * Finish off the dependency-search process for any nodes
23382e2caf59SThomas Veerman 	     * between bottom and gn (no point in questing around the
23392e2caf59SThomas Veerman 	     * filesystem for their implicit source when it's already
23402e2caf59SThomas Veerman 	     * known). Note that the node can't have any sources that
23412e2caf59SThomas Veerman 	     * need expanding, since SuffFindThem will stop on an existing
23422e2caf59SThomas Veerman 	     * node, so all we need to do is set the standard and System V
23432e2caf59SThomas Veerman 	     * variables.
23442e2caf59SThomas Veerman 	     */
23452e2caf59SThomas Veerman 	    targ->node->type |= OP_DEPS_FOUND;
23462e2caf59SThomas Veerman 
23472e2caf59SThomas Veerman 	    Var_Set(PREFIX, targ->pref, targ->node, 0);
23482e2caf59SThomas Veerman 
23492e2caf59SThomas Veerman 	    Var_Set(TARGET, targ->node->name, targ->node, 0);
23502e2caf59SThomas Veerman 	}
23512e2caf59SThomas Veerman     }
23522e2caf59SThomas Veerman 
23532e2caf59SThomas Veerman     if (gn->suffix)
23542e2caf59SThomas Veerman 	gn->suffix->refCount--;
23552e2caf59SThomas Veerman     gn->suffix = src->suff;
23562e2caf59SThomas Veerman     gn->suffix->refCount++;
23572e2caf59SThomas Veerman 
23582e2caf59SThomas Veerman     /*
23592e2caf59SThomas Veerman      * Nuke the transformation path and the Src structures left over in the
23602e2caf59SThomas Veerman      * two lists.
23612e2caf59SThomas Veerman      */
23622e2caf59SThomas Veerman sfnd_return:
23632e2caf59SThomas Veerman     if (bottom)
23642e2caf59SThomas Veerman 	if (Lst_Member(slst, bottom) == NULL)
23652e2caf59SThomas Veerman 	    Lst_AtEnd(slst, bottom);
23662e2caf59SThomas Veerman 
23672e2caf59SThomas Veerman     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
23682e2caf59SThomas Veerman 	continue;
23692e2caf59SThomas Veerman 
23702e2caf59SThomas Veerman     Lst_Concat(slst, srcs, LST_CONCLINK);
23712e2caf59SThomas Veerman     Lst_Concat(slst, targs, LST_CONCLINK);
23722e2caf59SThomas Veerman }
23732e2caf59SThomas Veerman 
23742e2caf59SThomas Veerman 
23752e2caf59SThomas Veerman /*-
23762e2caf59SThomas Veerman  *-----------------------------------------------------------------------
23772e2caf59SThomas Veerman  * Suff_FindDeps  --
23782e2caf59SThomas Veerman  *	Find implicit sources for the target described by the graph node
23792e2caf59SThomas Veerman  *	gn
23802e2caf59SThomas Veerman  *
23812e2caf59SThomas Veerman  * Results:
23822e2caf59SThomas Veerman  *	Nothing.
23832e2caf59SThomas Veerman  *
23842e2caf59SThomas Veerman  * Side Effects:
23852e2caf59SThomas Veerman  *	Nodes are added to the graph below the passed-in node. The nodes
23862e2caf59SThomas Veerman  *	are marked to have their IMPSRC variable filled in. The
23872e2caf59SThomas Veerman  *	PREFIX variable is set for the given node and all its
23882e2caf59SThomas Veerman  *	implied children.
23892e2caf59SThomas Veerman  *
23902e2caf59SThomas Veerman  * Notes:
23912e2caf59SThomas Veerman  *	The path found by this target is the shortest path in the
23922e2caf59SThomas Veerman  *	transformation graph, which may pass through non-existent targets,
23932e2caf59SThomas Veerman  *	to an existing target. The search continues on all paths from the
23942e2caf59SThomas Veerman  *	root suffix until a file is found. I.e. if there's a path
23952e2caf59SThomas Veerman  *	.o -> .c -> .l -> .l,v from the root and the .l,v file exists but
23962e2caf59SThomas Veerman  *	the .c and .l files don't, the search will branch out in
23972e2caf59SThomas Veerman  *	all directions from .o and again from all the nodes on the
23982e2caf59SThomas Veerman  *	next level until the .l,v node is encountered.
23992e2caf59SThomas Veerman  *
24002e2caf59SThomas Veerman  *-----------------------------------------------------------------------
24012e2caf59SThomas Veerman  */
24022e2caf59SThomas Veerman 
24032e2caf59SThomas Veerman void
Suff_FindDeps(GNode * gn)24042e2caf59SThomas Veerman Suff_FindDeps(GNode *gn)
24052e2caf59SThomas Veerman {
24062e2caf59SThomas Veerman 
24072e2caf59SThomas Veerman     SuffFindDeps(gn, srclist);
24082e2caf59SThomas Veerman     while (SuffRemoveSrc(srclist))
24092e2caf59SThomas Veerman 	continue;
24102e2caf59SThomas Veerman }
24112e2caf59SThomas Veerman 
24122e2caf59SThomas Veerman 
24132e2caf59SThomas Veerman /*
24142e2caf59SThomas Veerman  * Input:
24152e2caf59SThomas Veerman  *	gn		node we're dealing with
24162e2caf59SThomas Veerman  *
24172e2caf59SThomas Veerman  */
24182e2caf59SThomas Veerman static void
SuffFindDeps(GNode * gn,Lst slst)24192e2caf59SThomas Veerman SuffFindDeps(GNode *gn, Lst slst)
24202e2caf59SThomas Veerman {
24212e2caf59SThomas Veerman     if (gn->type & OP_DEPS_FOUND) {
24222e2caf59SThomas Veerman 	/*
24232e2caf59SThomas Veerman 	 * If dependencies already found, no need to do it again...
24242e2caf59SThomas Veerman 	 */
24252e2caf59SThomas Veerman 	return;
24262e2caf59SThomas Veerman     } else {
24272e2caf59SThomas Veerman 	gn->type |= OP_DEPS_FOUND;
24282e2caf59SThomas Veerman     }
24292e2caf59SThomas Veerman     /*
24302e2caf59SThomas Veerman      * Make sure we have these set, may get revised below.
24312e2caf59SThomas Veerman      */
24322e2caf59SThomas Veerman     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
24332e2caf59SThomas Veerman     Var_Set(PREFIX, gn->name, gn, 0);
243484d9c625SLionel Sambuc 
24352e2caf59SThomas Veerman     if (DEBUG(SUFF)) {
24362e2caf59SThomas Veerman 	fprintf(debug_file, "SuffFindDeps (%s)\n", gn->name);
24372e2caf59SThomas Veerman     }
24382e2caf59SThomas Veerman 
24392e2caf59SThomas Veerman     if (gn->type & OP_ARCHV) {
24402e2caf59SThomas Veerman 	SuffFindArchiveDeps(gn, slst);
24412e2caf59SThomas Veerman     } else if (gn->type & OP_LIB) {
24422e2caf59SThomas Veerman 	/*
24432e2caf59SThomas Veerman 	 * If the node is a library, it is the arch module's job to find it
24442e2caf59SThomas Veerman 	 * and set the TARGET variable accordingly. We merely provide the
24452e2caf59SThomas Veerman 	 * search path, assuming all libraries end in ".a" (if the suffix
24462e2caf59SThomas Veerman 	 * hasn't been defined, there's nothing we can do for it, so we just
24472e2caf59SThomas Veerman 	 * set the TARGET variable to the node's name in order to give it a
24482e2caf59SThomas Veerman 	 * value).
24492e2caf59SThomas Veerman 	 */
24502e2caf59SThomas Veerman 	LstNode	ln;
24512e2caf59SThomas Veerman 	Suff	*s;
24522e2caf59SThomas Veerman 
24532e2caf59SThomas Veerman 	ln = Lst_Find(sufflist, LIBSUFF, SuffSuffHasNameP);
24542e2caf59SThomas Veerman 	if (gn->suffix)
24552e2caf59SThomas Veerman 	    gn->suffix->refCount--;
24562e2caf59SThomas Veerman 	if (ln != NULL) {
24572e2caf59SThomas Veerman 	    gn->suffix = s = (Suff *)Lst_Datum(ln);
24582e2caf59SThomas Veerman 	    gn->suffix->refCount++;
24592e2caf59SThomas Veerman 	    Arch_FindLib(gn, s->searchPath);
24602e2caf59SThomas Veerman 	} else {
24612e2caf59SThomas Veerman 	    gn->suffix = NULL;
24622e2caf59SThomas Veerman 	    Var_Set(TARGET, gn->name, gn, 0);
24632e2caf59SThomas Veerman 	}
24642e2caf59SThomas Veerman 	/*
24652e2caf59SThomas Veerman 	 * Because a library (-lfoo) target doesn't follow the standard
24662e2caf59SThomas Veerman 	 * filesystem conventions, we don't set the regular variables for
24672e2caf59SThomas Veerman 	 * the thing. .PREFIX is simply made empty...
24682e2caf59SThomas Veerman 	 */
24692e2caf59SThomas Veerman 	Var_Set(PREFIX, "", gn, 0);
24702e2caf59SThomas Veerman     } else {
24712e2caf59SThomas Veerman 	SuffFindNormalDeps(gn, slst);
24722e2caf59SThomas Veerman     }
24732e2caf59SThomas Veerman }
24742e2caf59SThomas Veerman 
24752e2caf59SThomas Veerman /*-
24762e2caf59SThomas Veerman  *-----------------------------------------------------------------------
24772e2caf59SThomas Veerman  * Suff_SetNull --
24782e2caf59SThomas Veerman  *	Define which suffix is the null suffix.
24792e2caf59SThomas Veerman  *
24802e2caf59SThomas Veerman  * Input:
24812e2caf59SThomas Veerman  *	name		Name of null suffix
24822e2caf59SThomas Veerman  *
24832e2caf59SThomas Veerman  * Results:
24842e2caf59SThomas Veerman  *	None.
24852e2caf59SThomas Veerman  *
24862e2caf59SThomas Veerman  * Side Effects:
24872e2caf59SThomas Veerman  *	'suffNull' is altered.
24882e2caf59SThomas Veerman  *
24892e2caf59SThomas Veerman  * Notes:
24902e2caf59SThomas Veerman  *	Need to handle the changing of the null suffix gracefully so the
24912e2caf59SThomas Veerman  *	old transformation rules don't just go away.
24922e2caf59SThomas Veerman  *
24932e2caf59SThomas Veerman  *-----------------------------------------------------------------------
24942e2caf59SThomas Veerman  */
24952e2caf59SThomas Veerman void
Suff_SetNull(char * name)24962e2caf59SThomas Veerman Suff_SetNull(char *name)
24972e2caf59SThomas Veerman {
24982e2caf59SThomas Veerman     Suff    *s;
24992e2caf59SThomas Veerman     LstNode ln;
25002e2caf59SThomas Veerman 
25012e2caf59SThomas Veerman     ln = Lst_Find(sufflist, name, SuffSuffHasNameP);
25022e2caf59SThomas Veerman     if (ln != NULL) {
25032e2caf59SThomas Veerman 	s = (Suff *)Lst_Datum(ln);
25042e2caf59SThomas Veerman 	if (suffNull != NULL) {
25052e2caf59SThomas Veerman 	    suffNull->flags &= ~SUFF_NULL;
25062e2caf59SThomas Veerman 	}
25072e2caf59SThomas Veerman 	s->flags |= SUFF_NULL;
25082e2caf59SThomas Veerman 	/*
25092e2caf59SThomas Veerman 	 * XXX: Here's where the transformation mangling would take place
25102e2caf59SThomas Veerman 	 */
25112e2caf59SThomas Veerman 	suffNull = s;
25122e2caf59SThomas Veerman     } else {
25132e2caf59SThomas Veerman 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
25142e2caf59SThomas Veerman 		     name);
25152e2caf59SThomas Veerman     }
25162e2caf59SThomas Veerman }
25172e2caf59SThomas Veerman 
25182e2caf59SThomas Veerman /*-
25192e2caf59SThomas Veerman  *-----------------------------------------------------------------------
25202e2caf59SThomas Veerman  * Suff_Init --
25212e2caf59SThomas Veerman  *	Initialize suffixes module
25222e2caf59SThomas Veerman  *
25232e2caf59SThomas Veerman  * Results:
25242e2caf59SThomas Veerman  *	None
25252e2caf59SThomas Veerman  *
25262e2caf59SThomas Veerman  * Side Effects:
25272e2caf59SThomas Veerman  *	Many
25282e2caf59SThomas Veerman  *-----------------------------------------------------------------------
25292e2caf59SThomas Veerman  */
25302e2caf59SThomas Veerman void
Suff_Init(void)25312e2caf59SThomas Veerman Suff_Init(void)
25322e2caf59SThomas Veerman {
25332e2caf59SThomas Veerman     sufflist = Lst_Init(FALSE);
25342e2caf59SThomas Veerman #ifdef CLEANUP
25352e2caf59SThomas Veerman     suffClean = Lst_Init(FALSE);
25362e2caf59SThomas Veerman #endif
25372e2caf59SThomas Veerman     srclist = Lst_Init(FALSE);
25382e2caf59SThomas Veerman     transforms = Lst_Init(FALSE);
25392e2caf59SThomas Veerman 
25402e2caf59SThomas Veerman     sNum = 0;
25412e2caf59SThomas Veerman     /*
25422e2caf59SThomas Veerman      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
25432e2caf59SThomas Veerman      * actually go on the suffix list or everyone will think that's its
25442e2caf59SThomas Veerman      * suffix.
25452e2caf59SThomas Veerman      */
25462e2caf59SThomas Veerman     emptySuff = suffNull = bmake_malloc(sizeof(Suff));
25472e2caf59SThomas Veerman 
25482e2caf59SThomas Veerman     suffNull->name =   	    bmake_strdup("");
25492e2caf59SThomas Veerman     suffNull->nameLen =     0;
25502e2caf59SThomas Veerman     suffNull->searchPath =  Lst_Init(FALSE);
25512e2caf59SThomas Veerman     Dir_Concat(suffNull->searchPath, dirSearchPath);
25522e2caf59SThomas Veerman     suffNull->children =    Lst_Init(FALSE);
25532e2caf59SThomas Veerman     suffNull->parents =	    Lst_Init(FALSE);
25542e2caf59SThomas Veerman     suffNull->ref =	    Lst_Init(FALSE);
25552e2caf59SThomas Veerman     suffNull->sNum =   	    sNum++;
25562e2caf59SThomas Veerman     suffNull->flags =  	    SUFF_NULL;
25572e2caf59SThomas Veerman     suffNull->refCount =    1;
25582e2caf59SThomas Veerman 
25592e2caf59SThomas Veerman }
25602e2caf59SThomas Veerman 
25612e2caf59SThomas Veerman 
25622e2caf59SThomas Veerman /*-
25632e2caf59SThomas Veerman  *----------------------------------------------------------------------
25642e2caf59SThomas Veerman  * Suff_End --
25652e2caf59SThomas Veerman  *	Cleanup the this module
25662e2caf59SThomas Veerman  *
25672e2caf59SThomas Veerman  * Results:
25682e2caf59SThomas Veerman  *	None
25692e2caf59SThomas Veerman  *
25702e2caf59SThomas Veerman  * Side Effects:
25712e2caf59SThomas Veerman  *	The memory is free'd.
25722e2caf59SThomas Veerman  *----------------------------------------------------------------------
25732e2caf59SThomas Veerman  */
25742e2caf59SThomas Veerman 
25752e2caf59SThomas Veerman void
Suff_End(void)25762e2caf59SThomas Veerman Suff_End(void)
25772e2caf59SThomas Veerman {
25782e2caf59SThomas Veerman #ifdef CLEANUP
25792e2caf59SThomas Veerman     Lst_Destroy(sufflist, SuffFree);
25802e2caf59SThomas Veerman     Lst_Destroy(suffClean, SuffFree);
25812e2caf59SThomas Veerman     if (suffNull)
25822e2caf59SThomas Veerman 	SuffFree(suffNull);
25832e2caf59SThomas Veerman     Lst_Destroy(srclist, NULL);
25842e2caf59SThomas Veerman     Lst_Destroy(transforms, NULL);
25852e2caf59SThomas Veerman #endif
25862e2caf59SThomas Veerman }
25872e2caf59SThomas Veerman 
25882e2caf59SThomas Veerman 
25892e2caf59SThomas Veerman /********************* DEBUGGING FUNCTIONS **********************/
25902e2caf59SThomas Veerman 
SuffPrintName(void * s,void * dummy)25912e2caf59SThomas Veerman static int SuffPrintName(void *s, void *dummy)
25922e2caf59SThomas Veerman {
25932e2caf59SThomas Veerman     fprintf(debug_file, "%s ", ((Suff *)s)->name);
25942e2caf59SThomas Veerman     return (dummy ? 0 : 0);
25952e2caf59SThomas Veerman }
25962e2caf59SThomas Veerman 
25972e2caf59SThomas Veerman static int
SuffPrintSuff(void * sp,void * dummy)25982e2caf59SThomas Veerman SuffPrintSuff(void *sp, void *dummy)
25992e2caf59SThomas Veerman {
26002e2caf59SThomas Veerman     Suff    *s = (Suff *)sp;
26012e2caf59SThomas Veerman     int	    flags;
26022e2caf59SThomas Veerman     int	    flag;
26032e2caf59SThomas Veerman 
26042e2caf59SThomas Veerman     fprintf(debug_file, "# `%s' [%d] ", s->name, s->refCount);
26052e2caf59SThomas Veerman 
26062e2caf59SThomas Veerman     flags = s->flags;
26072e2caf59SThomas Veerman     if (flags) {
26082e2caf59SThomas Veerman 	fputs(" (", debug_file);
26092e2caf59SThomas Veerman 	while (flags) {
26102e2caf59SThomas Veerman 	    flag = 1 << (ffs(flags) - 1);
26112e2caf59SThomas Veerman 	    flags &= ~flag;
26122e2caf59SThomas Veerman 	    switch (flag) {
26132e2caf59SThomas Veerman 		case SUFF_NULL:
26142e2caf59SThomas Veerman 		    fprintf(debug_file, "NULL");
26152e2caf59SThomas Veerman 		    break;
26162e2caf59SThomas Veerman 		case SUFF_INCLUDE:
26172e2caf59SThomas Veerman 		    fprintf(debug_file, "INCLUDE");
26182e2caf59SThomas Veerman 		    break;
26192e2caf59SThomas Veerman 		case SUFF_LIBRARY:
26202e2caf59SThomas Veerman 		    fprintf(debug_file, "LIBRARY");
26212e2caf59SThomas Veerman 		    break;
26222e2caf59SThomas Veerman 	    }
26232e2caf59SThomas Veerman 	    fputc(flags ? '|' : ')', debug_file);
26242e2caf59SThomas Veerman 	}
26252e2caf59SThomas Veerman     }
26262e2caf59SThomas Veerman     fputc('\n', debug_file);
26272e2caf59SThomas Veerman     fprintf(debug_file, "#\tTo: ");
26282e2caf59SThomas Veerman     Lst_ForEach(s->parents, SuffPrintName, NULL);
26292e2caf59SThomas Veerman     fputc('\n', debug_file);
26302e2caf59SThomas Veerman     fprintf(debug_file, "#\tFrom: ");
26312e2caf59SThomas Veerman     Lst_ForEach(s->children, SuffPrintName, NULL);
26322e2caf59SThomas Veerman     fputc('\n', debug_file);
26332e2caf59SThomas Veerman     fprintf(debug_file, "#\tSearch Path: ");
26342e2caf59SThomas Veerman     Dir_PrintPath(s->searchPath);
26352e2caf59SThomas Veerman     fputc('\n', debug_file);
26362e2caf59SThomas Veerman     return (dummy ? 0 : 0);
26372e2caf59SThomas Veerman }
26382e2caf59SThomas Veerman 
26392e2caf59SThomas Veerman static int
SuffPrintTrans(void * tp,void * dummy)26402e2caf59SThomas Veerman SuffPrintTrans(void *tp, void *dummy)
26412e2caf59SThomas Veerman {
26422e2caf59SThomas Veerman     GNode   *t = (GNode *)tp;
26432e2caf59SThomas Veerman 
26442e2caf59SThomas Veerman     fprintf(debug_file, "%-16s: ", t->name);
26452e2caf59SThomas Veerman     Targ_PrintType(t->type);
26462e2caf59SThomas Veerman     fputc('\n', debug_file);
26472e2caf59SThomas Veerman     Lst_ForEach(t->commands, Targ_PrintCmd, NULL);
26482e2caf59SThomas Veerman     fputc('\n', debug_file);
26492e2caf59SThomas Veerman     return(dummy ? 0 : 0);
26502e2caf59SThomas Veerman }
26512e2caf59SThomas Veerman 
26522e2caf59SThomas Veerman void
Suff_PrintAll(void)26532e2caf59SThomas Veerman Suff_PrintAll(void)
26542e2caf59SThomas Veerman {
26552e2caf59SThomas Veerman     fprintf(debug_file, "#*** Suffixes:\n");
26562e2caf59SThomas Veerman     Lst_ForEach(sufflist, SuffPrintSuff, NULL);
26572e2caf59SThomas Veerman 
26582e2caf59SThomas Veerman     fprintf(debug_file, "#*** Transformations:\n");
26592e2caf59SThomas Veerman     Lst_ForEach(transforms, SuffPrintTrans, NULL);
26602e2caf59SThomas Veerman }
2661