Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.150
      1 /*	$NetBSD: suff.c,v 1.150 2020/09/12 15:21:25 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #ifndef MAKE_NATIVE
     72 static char rcsid[] = "$NetBSD: suff.c,v 1.150 2020/09/12 15:21:25 rillig Exp $";
     73 #else
     74 #include <sys/cdefs.h>
     75 #ifndef lint
     76 #if 0
     77 static char sccsid[] = "@(#)suff.c	8.4 (Berkeley) 3/21/94";
     78 #else
     79 __RCSID("$NetBSD: suff.c,v 1.150 2020/09/12 15:21:25 rillig Exp $");
     80 #endif
     81 #endif /* not lint */
     82 #endif
     83 
     84 /*-
     85  * suff.c --
     86  *	Functions to maintain suffix lists and find implicit dependents
     87  *	using suffix transformation rules
     88  *
     89  * Interface:
     90  *	Suff_Init 	    	Initialize all things to do with suffixes.
     91  *
     92  *	Suff_End 	    	Cleanup the module
     93  *
     94  *	Suff_DoPaths	    	This function is used to make life easier
     95  *	    	  	    	when searching for a file according to its
     96  *	    	  	    	suffix. It takes the global search path,
     97  *	    	  	    	as defined using the .PATH: target, and appends
     98  *	    	  	    	its directories to the path of each of the
     99  *	    	  	    	defined suffixes, as specified using
    100  *	    	  	    	.PATH<suffix>: targets. In addition, all
    101  *	    	  	    	directories given for suffixes labeled as
    102  *	    	  	    	include files or libraries, using the .INCLUDES
    103  *	    	  	    	or .LIBS targets, are played with using
    104  *	    	  	    	Dir_MakeFlags to create the .INCLUDES and
    105  *	    	  	    	.LIBS global variables.
    106  *
    107  *	Suff_ClearSuffixes  	Clear out all the suffixes and defined
    108  *	    	  	    	transformations.
    109  *
    110  *	Suff_IsTransform    	Return TRUE if the passed string is the lhs
    111  *	    	  	    	of a transformation rule.
    112  *
    113  *	Suff_AddSuffix	    	Add the passed string as another known suffix.
    114  *
    115  *	Suff_GetPath	    	Return the search path for the given suffix.
    116  *
    117  *	Suff_AddInclude	    	Mark the given suffix as denoting an include
    118  *	    	  	    	file.
    119  *
    120  *	Suff_AddLib	    	Mark the given suffix as denoting a library.
    121  *
    122  *	Suff_AddTransform   	Add another transformation to the suffix
    123  *	    	  	    	graph. Returns  GNode suitable for framing, I
    124  *	    	  	    	mean, tacking commands, attributes, etc. on.
    125  *
    126  *	Suff_SetNull	    	Define the suffix to consider the suffix of
    127  *	    	  	    	any file that doesn't have a known one.
    128  *
    129  *	Suff_FindDeps	    	Find implicit sources for and the location of
    130  *	    	  	    	a target based on its suffix. Returns the
    131  *	    	  	    	bottom-most node added to the graph or NULL
    132  *	    	  	    	if the target had no implicit sources.
    133  *
    134  *	Suff_FindPath	    	Return the appropriate path to search in
    135  *				order to find the node.
    136  */
    137 
    138 #include	  "make.h"
    139 #include	  "dir.h"
    140 
    141 #define SUFF_DEBUG0(fmt) \
    142     if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt)
    143 
    144 #define SUFF_DEBUG1(fmt, arg1) \
    145     if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1)
    146 
    147 #define SUFF_DEBUG2(fmt, arg1, arg2) \
    148     if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1, arg2)
    149 
    150 #define SUFF_DEBUG3(fmt, arg1, arg2, arg3) \
    151     if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1, arg2, arg3)
    152 
    153 static Lst       sufflist;	/* Lst of suffixes */
    154 #ifdef CLEANUP
    155 static Lst	 suffClean;	/* Lst of suffixes to be cleaned */
    156 #endif
    157 static Lst	 srclist;	/* Lst of sources */
    158 static Lst       transforms;	/* Lst of transformation rules */
    159 
    160 static int        sNum = 0;	/* Counter for assigning suffix numbers */
    161 
    162 typedef enum {
    163     SUFF_INCLUDE	= 0x01,	/* One which is #include'd */
    164     SUFF_LIBRARY	= 0x02,	/* One which contains a library */
    165     SUFF_NULL		= 0x04	/* The empty suffix */
    166     /* XXX: Why is SUFF_NULL needed? Wouldn't nameLen == 0 mean the same? */
    167 } SuffFlags;
    168 
    169 ENUM_FLAGS_RTTI_3(SuffFlags,
    170 		  SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
    171 
    172 /*
    173  * Structure describing an individual suffix.
    174  */
    175 typedef struct Suff {
    176     char         *name;	    	/* The suffix itself, such as ".c" */
    177     int		 nameLen;	/* Length of the name, to avoid strlen calls */
    178     SuffFlags	 flags;      	/* Type of suffix */
    179     Lst    	 searchPath;	/* The path along which files of this suffix
    180 				 * may be found */
    181     int          sNum;	      	/* The suffix number */
    182     int		 refCount;	/* Reference count of list membership */
    183     Lst          parents;	/* Suffixes we have a transformation to */
    184     Lst          children;	/* Suffixes we have a transformation from */
    185     Lst		 ref;		/* List of lists this suffix is referenced */
    186 } Suff;
    187 
    188 /*
    189  * Structure used in the search for implied sources.
    190  */
    191 typedef struct _Src {
    192     char            *file;	/* The file to look for */
    193     char    	    *pref;  	/* Prefix from which file was formed */
    194     Suff            *suff;	/* The suffix on the file */
    195     struct _Src     *parent;	/* The Src for which this is a source */
    196     GNode           *node;	/* The node describing the file */
    197     int	    	    children;	/* Count of existing children (so we don't free
    198 				 * this thing too early or never nuke it) */
    199 #ifdef DEBUG_SRC
    200     Lst		    cp;		/* Debug; children list */
    201 #endif
    202 } Src;
    203 
    204 /*
    205  * A structure for passing more than one argument to the Lst-library-invoked
    206  * function...
    207  */
    208 typedef struct {
    209     Lst            l;
    210     Src            *s;
    211 } LstSrc;
    212 
    213 typedef struct {
    214     GNode	  **gnp;
    215     Suff	   *s;
    216     Boolean	    r;
    217 } GNodeSuff;
    218 
    219 static Suff 	    *suffNull;	/* The NULL suffix for this run */
    220 static Suff 	    *emptySuff;	/* The empty suffix required for POSIX
    221 				 * single-suffix transformation rules */
    222 
    223 
    224 static void SuffUnRef(void *, void *);
    225 static void SuffFree(void *);
    226 static void SuffInsert(Lst, Suff *);
    227 static void SuffRemove(Lst, Suff *);
    228 static Boolean SuffParseTransform(char *, Suff **, Suff **);
    229 static int SuffRebuildGraph(void *, void *);
    230 static int SuffScanTargets(void *, void *);
    231 static int SuffAddSrc(void *, void *);
    232 static void SuffAddLevel(Lst, Src *);
    233 static void SuffExpandChildren(LstNode, GNode *);
    234 static void SuffExpandWildcards(LstNode, GNode *);
    235 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
    236 static void SuffFindDeps(GNode *, Lst);
    237 static void SuffFindArchiveDeps(GNode *, Lst);
    238 static void SuffFindNormalDeps(GNode *, Lst);
    239 static int SuffPrintName(void *, void *);
    240 static int SuffPrintSuff(void *, void *);
    241 static int SuffPrintTrans(void *, void *);
    242 
    243 	/*************** Lst Predicates ****************/
    244 /*-
    245  *-----------------------------------------------------------------------
    246  * SuffStrIsPrefix  --
    247  *	See if pref is a prefix of str.
    248  *
    249  * Input:
    250  *	pref		possible prefix
    251  *	str		string to check
    252  *
    253  * Results:
    254  *	NULL if it ain't, pointer to character in str after prefix if so
    255  *
    256  * Side Effects:
    257  *	None
    258  *-----------------------------------------------------------------------
    259  */
    260 static const char *
    261 SuffStrIsPrefix(const char *pref, const char *str)
    262 {
    263     while (*str && *pref == *str) {
    264 	pref++;
    265 	str++;
    266     }
    267 
    268     return *pref ? NULL : str;
    269 }
    270 
    271 typedef struct {
    272     char	*ename;		/* The end of the name */
    273     int		 len;		/* Length of the name */
    274 } SuffSuffGetSuffixArgs;
    275 
    276 /* See if suff is a suffix of str. str->ename should point to THE END
    277  * of the string to check. (THE END == the null byte)
    278  *
    279  * Input:
    280  *	s		possible suffix
    281  *	str		string to examine
    282  *
    283  * Results:
    284  *	NULL if it ain't, pointer to character in str before suffix if
    285  *	it is.
    286  */
    287 static char *
    288 SuffSuffGetSuffix(const Suff *s, const SuffSuffGetSuffixArgs *str)
    289 {
    290     char  *p1;	    	/* Pointer into suffix name */
    291     char  *p2;	    	/* Pointer into string being examined */
    292 
    293     if (str->len < s->nameLen)
    294 	return NULL;		/* this string is shorter than the suffix */
    295 
    296     p1 = s->name + s->nameLen;
    297     p2 = str->ename;
    298 
    299     while (p1 >= s->name && *p1 == *p2) {
    300 	p1--;
    301 	p2--;
    302     }
    303 
    304     return p1 == s->name - 1 ? p2 : NULL;
    305 }
    306 
    307 /* Predicate form of SuffSuffGetSuffix, for Lst_Find. */
    308 static Boolean
    309 SuffSuffIsSuffix(const void *s, const void *sd)
    310 {
    311     return SuffSuffGetSuffix(s, sd) != NULL;
    312 }
    313 
    314 /* See if the suffix has the desired name. */
    315 static Boolean
    316 SuffSuffHasName(const void *s, const void *desiredName)
    317 {
    318     return strcmp(((const Suff *)s)->name, desiredName) == 0;
    319 }
    320 
    321 /* See if the suffix name is a prefix of the string. Care must be taken when
    322  * using this to search for transformations and what-not, since there could
    323  * well be two suffixes, one of which is a prefix of the other... */
    324 static Boolean
    325 SuffSuffIsPrefix(const void *s, const void *str)
    326 {
    327     return SuffStrIsPrefix(((const Suff *)s)->name, str) != NULL;
    328 }
    329 
    330 /* See if the graph node has the desired name. */
    331 static Boolean
    332 SuffGNHasName(const void *gn, const void *desiredName)
    333 {
    334     return strcmp(((const GNode *)gn)->name, desiredName) == 0;
    335 }
    336 
    337 	    /*********** Maintenance Functions ************/
    338 
    339 static void
    340 SuffUnRef(void *lp, void *sp)
    341 {
    342     Lst l = (Lst) lp;
    343 
    344     LstNode ln = Lst_FindDatum(l, sp);
    345     if (ln != NULL) {
    346 	Lst_Remove(l, ln);
    347 	((Suff *)sp)->refCount--;
    348     }
    349 }
    350 
    351 /* Free up all memory associated with the given suffix structure. */
    352 static void
    353 SuffFree(void *sp)
    354 {
    355     Suff *s = (Suff *)sp;
    356 
    357     if (s == suffNull)
    358 	suffNull = NULL;
    359 
    360     if (s == emptySuff)
    361 	emptySuff = NULL;
    362 
    363 #ifdef notdef
    364     /* We don't delete suffixes in order, so we cannot use this */
    365     if (s->refCount)
    366 	Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
    367 	    s->refCount);
    368 #endif
    369 
    370     Lst_Free(s->ref);
    371     Lst_Free(s->children);
    372     Lst_Free(s->parents);
    373     Lst_Destroy(s->searchPath, Dir_Destroy);
    374 
    375     free(s->name);
    376     free(s);
    377 }
    378 
    379 /* Remove the suffix from the list, and free if it is otherwise unused. */
    380 static void
    381 SuffRemove(Lst l, Suff *s)
    382 {
    383     SuffUnRef(l, s);
    384     if (s->refCount == 0) {
    385 	SuffUnRef(sufflist, s);
    386 	SuffFree(s);
    387     }
    388 }
    389 
    390 /* Insert the suffix into the list keeping the list ordered by suffix numbers.
    391  *
    392  * Input:
    393  *	l		the list where in s should be inserted
    394  *	s		the suffix to insert
    395  */
    396 static void
    397 SuffInsert(Lst l, Suff *s)
    398 {
    399     LstNode 	  ln;		/* current element in l we're examining */
    400     Suff          *s2 = NULL;	/* the suffix descriptor in this element */
    401 
    402     Lst_Open(l);
    403     while ((ln = Lst_Next(l)) != NULL) {
    404 	s2 = LstNode_Datum(ln);
    405 	if (s2->sNum >= s->sNum) {
    406 	    break;
    407 	}
    408     }
    409     Lst_Close(l);
    410 
    411     SUFF_DEBUG2("inserting %s(%d)...", s->name, s->sNum);
    412 
    413     if (ln == NULL) {
    414 	SUFF_DEBUG0("at end of list\n");
    415 	Lst_Append(l, s);
    416 	s->refCount++;
    417 	Lst_Append(s->ref, l);
    418     } else if (s2->sNum != s->sNum) {
    419 	SUFF_DEBUG2("before %s(%d)\n", s2->name, s2->sNum);
    420 	Lst_InsertBefore(l, ln, s);
    421 	s->refCount++;
    422 	Lst_Append(s->ref, l);
    423     } else {
    424 	SUFF_DEBUG0("already there\n");
    425     }
    426 }
    427 
    428 static Suff *
    429 SuffNew(const char *name)
    430 {
    431     Suff *s = bmake_malloc(sizeof(Suff));
    432 
    433     s->name =   	bmake_strdup(name);
    434     s->nameLen = 	strlen(s->name);
    435     s->searchPath = Lst_Init();
    436     s->children = 	Lst_Init();
    437     s->parents = 	Lst_Init();
    438     s->ref = 	Lst_Init();
    439     s->sNum =   	sNum++;
    440     s->flags =  	0;
    441     s->refCount =	1;
    442 
    443     return s;
    444 }
    445 
    446 /* This is gross. Nuke the list of suffixes but keep all transformation
    447  * rules around. The transformation graph is destroyed in this process, but
    448  * we leave the list of rules so when a new graph is formed the rules will
    449  * remain. This function is called from the parse module when a .SUFFIXES:\n
    450  * line is encountered. */
    451 void
    452 Suff_ClearSuffixes(void)
    453 {
    454 #ifdef CLEANUP
    455     Lst_MoveAll(suffClean, sufflist);
    456 #endif
    457     sufflist = Lst_Init();
    458     sNum = 0;
    459     if (suffNull)
    460 	SuffFree(suffNull);
    461     emptySuff = suffNull = SuffNew("");
    462 
    463     Dir_Concat(suffNull->searchPath, dirSearchPath);
    464     suffNull->flags =  	    SUFF_NULL;
    465 }
    466 
    467 /* Parse a transformation string to find its two component suffixes.
    468  *
    469  * Input:
    470  *	str		String being parsed
    471  *	out_src		Place to store source of trans.
    472  *	out_targ	Place to store target of trans.
    473  *
    474  * Results:
    475  *	TRUE if the string is a valid transformation, FALSE otherwise.
    476  */
    477 static Boolean
    478 SuffParseTransform(char *str, Suff **out_src, Suff **out_targ)
    479 {
    480     LstNode		srcLn;	    /* element in suffix list of trans source*/
    481     Suff		*src;	    /* Source of transformation */
    482     LstNode		targLn;	    /* element in suffix list of trans target*/
    483     char		*str2;	    /* Extra pointer (maybe target suffix) */
    484     LstNode 	    	singleLn;   /* element in suffix list of any suffix
    485 				     * that exactly matches str */
    486     Suff    	    	*single = NULL;/* Source of possible transformation to
    487 				     * null suffix */
    488 
    489     srcLn = NULL;
    490     singleLn = NULL;
    491 
    492     /*
    493      * Loop looking first for a suffix that matches the start of the
    494      * string and then for one that exactly matches the rest of it. If
    495      * we can find two that meet these criteria, we've successfully
    496      * parsed the string.
    497      */
    498     for (;;) {
    499 	if (srcLn == NULL) {
    500 	    srcLn = Lst_Find(sufflist, SuffSuffIsPrefix, str);
    501 	} else {
    502 	    srcLn = Lst_FindFrom(sufflist, LstNode_Next(srcLn),
    503 				  SuffSuffIsPrefix, str);
    504 	}
    505 	if (srcLn == NULL) {
    506 	    /*
    507 	     * Ran out of source suffixes -- no such rule
    508 	     */
    509 	    if (singleLn != NULL) {
    510 		/*
    511 		 * Not so fast Mr. Smith! There was a suffix that encompassed
    512 		 * the entire string, so we assume it was a transformation
    513 		 * to the null suffix (thank you POSIX). We still prefer to
    514 		 * find a double rule over a singleton, hence we leave this
    515 		 * check until the end.
    516 		 *
    517 		 * XXX: Use emptySuff over suffNull?
    518 		 */
    519 		*out_src = single;
    520 		*out_targ = suffNull;
    521 		return TRUE;
    522 	    }
    523 	    return FALSE;
    524 	}
    525 	src = LstNode_Datum(srcLn);
    526 	str2 = str + src->nameLen;
    527 	if (*str2 == '\0') {
    528 	    single = src;
    529 	    singleLn = srcLn;
    530 	} else {
    531 	    targLn = Lst_Find(sufflist, SuffSuffHasName, str2);
    532 	    if (targLn != NULL) {
    533 		*out_src = src;
    534 		*out_targ = LstNode_Datum(targLn);
    535 		return TRUE;
    536 	    }
    537 	}
    538     }
    539 }
    540 
    541 /* Return TRUE if the given string is a transformation rule, that is, a
    542  * concatenation of two known suffixes. */
    543 Boolean
    544 Suff_IsTransform(char *str)
    545 {
    546     Suff    	  *src, *targ;
    547 
    548     return SuffParseTransform(str, &src, &targ);
    549 }
    550 
    551 /* Add the transformation rule described by the line to the list of rules
    552  * and place the transformation itself in the graph.
    553  *
    554  * The node is placed on the end of the transforms Lst and links are made
    555  * between the two suffixes mentioned in the target name.
    556 
    557  * Input:
    558  *	line		name of transformation to add
    559  *
    560  * Results:
    561  *	The node created for the transformation in the transforms list
    562  */
    563 GNode *
    564 Suff_AddTransform(char *line)
    565 {
    566     GNode         *gn;		/* GNode of transformation rule */
    567     Suff          *s,		/* source suffix */
    568 		  *t;		/* target suffix */
    569     LstNode 	  ln;	    	/* Node for existing transformation */
    570     Boolean ok;
    571 
    572     ln = Lst_Find(transforms, SuffGNHasName, line);
    573     if (ln == NULL) {
    574 	/*
    575 	 * Make a new graph node for the transformation. It will be filled in
    576 	 * by the Parse module.
    577 	 */
    578 	gn = Targ_NewGN(line);
    579 	Lst_Append(transforms, gn);
    580     } else {
    581 	/*
    582 	 * New specification for transformation rule. Just nuke the old list
    583 	 * of commands so they can be filled in again... We don't actually
    584 	 * free the commands themselves, because a given command can be
    585 	 * attached to several different transformations.
    586 	 */
    587 	gn = LstNode_Datum(ln);
    588 	Lst_Free(gn->commands);
    589 	Lst_Free(gn->children);
    590 	gn->commands = Lst_Init();
    591 	gn->children = Lst_Init();
    592     }
    593 
    594     gn->type = OP_TRANSFORM;
    595 
    596     ok = SuffParseTransform(line, &s, &t);
    597     assert(ok);
    598     (void)ok;
    599 
    600     /*
    601      * link the two together in the proper relationship and order
    602      */
    603     SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    604 		s->name, t->name);
    605     SuffInsert(t->children, s);
    606     SuffInsert(s->parents, t);
    607 
    608     return gn;
    609 }
    610 
    611 /* Handle the finish of a transformation definition, removing the
    612  * transformation from the graph if it has neither commands nor sources.
    613  * This is a callback procedure for the Parse module via Lst_ForEach.
    614  *
    615  * If the node has no commands or children, the children and parents lists
    616  * of the affected suffixes are altered.
    617  *
    618  * Input:
    619  *	gn		Node for transformation
    620  */
    621 void
    622 Suff_EndTransform(GNode *gn)
    623 {
    624     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(gn->cohorts))
    625 	gn = LstNode_Datum(Lst_Last(gn->cohorts));
    626     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
    627 	Lst_IsEmpty(gn->children))
    628     {
    629 	Suff	*s, *t;
    630 
    631 	/*
    632 	 * SuffParseTransform() may fail for special rules which are not
    633 	 * actual transformation rules. (e.g. .DEFAULT)
    634 	 */
    635 	if (SuffParseTransform(gn->name, &s, &t)) {
    636 	    Lst	 p;
    637 
    638 	    SUFF_DEBUG2("deleting transformation from `%s' to `%s'\n",
    639 			s->name, t->name);
    640 
    641 	    /*
    642 	     * Store s->parents because s could be deleted in SuffRemove
    643 	     */
    644 	    p = s->parents;
    645 
    646 	    /*
    647 	     * Remove the source from the target's children list. We check for a
    648 	     * nil return to handle a beanhead saying something like
    649 	     *  .c.o .c.o:
    650 	     *
    651 	     * We'll be called twice when the next target is seen, but .c and .o
    652 	     * are only linked once...
    653 	     */
    654 	    SuffRemove(t->children, s);
    655 
    656 	    /*
    657 	     * Remove the target from the source's parents list
    658 	     */
    659 	    SuffRemove(p, t);
    660 	}
    661     } else if (gn->type & OP_TRANSFORM) {
    662 	SUFF_DEBUG1("transformation %s complete\n", gn->name);
    663     }
    664 }
    665 
    666 /* Called from Suff_AddSuffix via Lst_ForEach to search through the list of
    667  * existing transformation rules and rebuild the transformation graph when
    668  * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
    669  * transformation involving this suffix and another, existing suffix, the
    670  * proper relationship is established between the two.
    671  *
    672  * The appropriate links will be made between this suffix and others if
    673  * transformation rules exist for it.
    674  *
    675  * Input:
    676  *	transformp	Transformation to test
    677  *	sp		Suffix to rebuild
    678  *
    679  * Results:
    680  *	0, so that Lst_ForEach continues
    681  */
    682 static int
    683 SuffRebuildGraph(void *transformp, void *sp)
    684 {
    685     GNode   	*transform = (GNode *)transformp;
    686     Suff    	*s = (Suff *)sp;
    687     char 	*cp;
    688     LstNode	ln;
    689     Suff  	*s2;
    690     SuffSuffGetSuffixArgs sd;
    691 
    692     /*
    693      * First see if it is a transformation from this suffix.
    694      */
    695     cp = UNCONST(SuffStrIsPrefix(s->name, transform->name));
    696     if (cp != NULL) {
    697 	ln = Lst_Find(sufflist, SuffSuffHasName, cp);
    698 	if (ln != NULL) {
    699 	    /*
    700 	     * Found target. Link in and return, since it can't be anything
    701 	     * else.
    702 	     */
    703 	    s2 = LstNode_Datum(ln);
    704 	    SuffInsert(s2->children, s);
    705 	    SuffInsert(s->parents, s2);
    706 	    return 0;
    707 	}
    708     }
    709 
    710     /*
    711      * Not from, maybe to?
    712      */
    713     sd.len = strlen(transform->name);
    714     sd.ename = transform->name + sd.len;
    715     cp = SuffSuffGetSuffix(s, &sd);
    716     if (cp != NULL) {
    717 	/*
    718 	 * Null-terminate the source suffix in order to find it.
    719 	 */
    720 	cp[1] = '\0';
    721 	ln = Lst_Find(sufflist, SuffSuffHasName, transform->name);
    722 	/*
    723 	 * Replace the start of the target suffix
    724 	 */
    725 	cp[1] = s->name[0];
    726 	if (ln != NULL) {
    727 	    /*
    728 	     * Found it -- establish the proper relationship
    729 	     */
    730 	    s2 = LstNode_Datum(ln);
    731 	    SuffInsert(s->children, s2);
    732 	    SuffInsert(s2->parents, s);
    733 	}
    734     }
    735     return 0;
    736 }
    737 
    738 /* Called from Suff_AddSuffix via Lst_ForEach to search through the list of
    739  * existing targets and find if any of the existing targets can be turned
    740  * into a transformation rule.
    741  *
    742  * If such a target is found and the target is the current main target, the
    743  * main target is set to NULL and the next target examined (if that exists)
    744  * becomes the main target.
    745  *
    746  * Results:
    747  *	1 if a new main target has been selected, 0 otherwise.
    748  */
    749 static int
    750 SuffScanTargets(void *targetp, void *gsp)
    751 {
    752     GNode   	*target = (GNode *)targetp;
    753     GNodeSuff	*gs = (GNodeSuff *)gsp;
    754     Suff	*s, *t;
    755     char 	*ptr;
    756 
    757     if (*gs->gnp == NULL && gs->r && (target->type & OP_NOTARGET) == 0) {
    758 	*gs->gnp = target;
    759 	Targ_SetMain(target);
    760 	return 1;
    761     }
    762 
    763     if (target->type == OP_TRANSFORM)
    764 	return 0;
    765 
    766     if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
    767 	ptr == target->name)
    768 	return 0;
    769 
    770     if (SuffParseTransform(target->name, &s, &t)) {
    771 	if (*gs->gnp == target) {
    772 	    gs->r = TRUE;
    773 	    *gs->gnp = NULL;
    774 	    Targ_SetMain(NULL);
    775 	}
    776 	Lst_Free(target->children);
    777 	target->children = Lst_Init();
    778 	target->type = OP_TRANSFORM;
    779 	/*
    780 	 * link the two together in the proper relationship and order
    781 	 */
    782 	SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    783 		    s->name, t->name);
    784 	SuffInsert(t->children, s);
    785 	SuffInsert(s->parents, t);
    786     }
    787     return 0;
    788 }
    789 
    790 /* Add the suffix to the end of the list of known suffixes.
    791  * Should we restructure the suffix graph? Make doesn't...
    792  *
    793  * A GNode is created for the suffix and a Suff structure is created and
    794  * added to the suffixes list unless the suffix was already known.
    795  * The mainNode passed can be modified if a target mutated into a
    796  * transform and that target happened to be the main target.
    797  *
    798  * Input:
    799  *	name		the name of the suffix to add
    800  */
    801 void
    802 Suff_AddSuffix(const char *name, GNode **gnp)
    803 {
    804     Suff          *s;	    /* new suffix descriptor */
    805     LstNode 	  ln;
    806     GNodeSuff	  gs;
    807 
    808     ln = Lst_Find(sufflist, SuffSuffHasName, name);
    809     if (ln == NULL) {
    810 	s = SuffNew(name);
    811 
    812 	Lst_Append(sufflist, s);
    813 	/*
    814 	 * We also look at our existing targets list to see if adding
    815 	 * this suffix will make one of our current targets mutate into
    816 	 * a suffix rule. This is ugly, but other makes treat all targets
    817 	 * that start with a . as suffix rules.
    818 	 */
    819 	gs.gnp = gnp;
    820 	gs.s  = s;
    821 	gs.r  = FALSE;
    822 	Lst_ForEach(Targ_List(), SuffScanTargets, &gs);
    823 	/*
    824 	 * Look for any existing transformations from or to this suffix.
    825 	 * XXX: Only do this after a Suff_ClearSuffixes?
    826 	 */
    827 	Lst_ForEach(transforms, SuffRebuildGraph, s);
    828     }
    829 }
    830 
    831 /* Return the search path for the given suffix, or NULL. */
    832 Lst
    833 Suff_GetPath(char *sname)
    834 {
    835     LstNode   	  ln;
    836     Suff    	  *s;
    837 
    838     ln = Lst_Find(sufflist, SuffSuffHasName, sname);
    839     if (ln == NULL) {
    840 	return NULL;
    841     } else {
    842 	s = LstNode_Datum(ln);
    843 	return s->searchPath;
    844     }
    845 }
    846 
    847 /* Extend the search paths for all suffixes to include the default search
    848  * path.
    849  *
    850  * The searchPath field of all the suffixes is extended by the directories
    851  * in dirSearchPath. If paths were specified for the ".h" suffix, the
    852  * directories are stuffed into a global variable called ".INCLUDES" with
    853  * each directory preceded by a -I. The same is done for the ".a" suffix,
    854  * except the variable is called ".LIBS" and the flag is -L.
    855  */
    856 void
    857 Suff_DoPaths(void)
    858 {
    859     Suff	   	*s;
    860     LstNode  		ln;
    861     char		*ptr;
    862     Lst	    	    	inIncludes; /* Cumulative .INCLUDES path */
    863     Lst	    	    	inLibs;	    /* Cumulative .LIBS path */
    864 
    865 
    866     inIncludes = Lst_Init();
    867     inLibs = Lst_Init();
    868 
    869     Lst_Open(sufflist);
    870     while ((ln = Lst_Next(sufflist)) != NULL) {
    871 	s = LstNode_Datum(ln);
    872 	if (!Lst_IsEmpty(s->searchPath)) {
    873 #ifdef INCLUDES
    874 	    if (s->flags & SUFF_INCLUDE) {
    875 		Dir_Concat(inIncludes, s->searchPath);
    876 	    }
    877 #endif /* INCLUDES */
    878 #ifdef LIBRARIES
    879 	    if (s->flags & SUFF_LIBRARY) {
    880 		Dir_Concat(inLibs, s->searchPath);
    881 	    }
    882 #endif /* LIBRARIES */
    883 	    Dir_Concat(s->searchPath, dirSearchPath);
    884 	} else {
    885 	    Lst_Destroy(s->searchPath, Dir_Destroy);
    886 	    s->searchPath = Lst_Copy(dirSearchPath, Dir_CopyDir);
    887 	}
    888     }
    889     Lst_Close(sufflist);
    890 
    891     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
    892     free(ptr);
    893     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
    894     free(ptr);
    895 
    896     Lst_Destroy(inIncludes, Dir_Destroy);
    897     Lst_Destroy(inLibs, Dir_Destroy);
    898 }
    899 
    900 /* Add the given suffix as a type of file which gets included.
    901  * Called from the parse module when a .INCLUDES line is parsed.
    902  * The suffix must have already been defined.
    903  * The SUFF_INCLUDE bit is set in the suffix's flags field.
    904  *
    905  * Input:
    906  *	sname		Name of the suffix to mark
    907  */
    908 void
    909 Suff_AddInclude(char *sname)
    910 {
    911     LstNode	  ln;
    912     Suff	  *s;
    913 
    914     ln = Lst_Find(sufflist, SuffSuffHasName, sname);
    915     if (ln != NULL) {
    916 	s = LstNode_Datum(ln);
    917 	s->flags |= SUFF_INCLUDE;
    918     }
    919 }
    920 
    921 /* Add the given suffix as a type of file which is a library.
    922  * Called from the parse module when parsing a .LIBS line.
    923  * The suffix must have been defined via .SUFFIXES before this is called.
    924  * The SUFF_LIBRARY bit is set in the suffix's flags field.
    925  *
    926  * Input:
    927  *	sname		Name of the suffix to mark
    928  */
    929 void
    930 Suff_AddLib(const char *sname)
    931 {
    932     LstNode	  ln;
    933     Suff	  *s;
    934 
    935     ln = Lst_Find(sufflist, SuffSuffHasName, sname);
    936     if (ln != NULL) {
    937 	s = LstNode_Datum(ln);
    938 	s->flags |= SUFF_LIBRARY;
    939     }
    940 }
    941 
    942 	  /********** Implicit Source Search Functions *********/
    943 
    944 #ifdef DEBUG_SRC
    945 static int
    946 PrintAddr(void *a, void *b MAKE_ATTR_UNUSED)
    947 {
    948     printf("%lx ", (unsigned long) a);
    949     return 0;
    950 }
    951 #endif
    952 
    953 /* Add a suffix as a Src structure to the given list with its parent
    954  * being the given Src structure. If the suffix is the null suffix,
    955  * the prefix is used unaltered as the file name in the Src structure.
    956  *
    957  * Input:
    958  *	sp		suffix for which to create a Src structure
    959  *	lsp		list and parent for the new Src
    960  *
    961  * Results:
    962  *	0, so that Lst_ForEach continues
    963  */
    964 static int
    965 SuffAddSrc(void *sp, void *lsp)
    966 {
    967     Suff	*s = (Suff *)sp;
    968     LstSrc      *ls = (LstSrc *)lsp;
    969     Src         *s2;	    /* new Src structure */
    970     Src    	*targ; 	    /* Target structure */
    971 
    972     targ = ls->s;
    973 
    974     if ((s->flags & SUFF_NULL) && s->name[0] != '\0') {
    975 	/*
    976 	 * If the suffix has been marked as the NULL suffix, also create a Src
    977 	 * structure for a file with no suffix attached. Two birds, and all
    978 	 * that...
    979 	 */
    980 	s2 = bmake_malloc(sizeof(Src));
    981 	s2->file =  	bmake_strdup(targ->pref);
    982 	s2->pref =  	targ->pref;
    983 	s2->parent = 	targ;
    984 	s2->node =  	NULL;
    985 	s2->suff =  	s;
    986 	s->refCount++;
    987 	s2->children =	0;
    988 	targ->children += 1;
    989 	Lst_Append(ls->l, s2);
    990 #ifdef DEBUG_SRC
    991 	s2->cp = Lst_Init();
    992 	Lst_Append(targ->cp, s2);
    993 	fprintf(debug_file, "1 add %p %p to %p:", targ, s2, ls->l);
    994 	Lst_ForEach(ls->l, PrintAddr, NULL);
    995 	fprintf(debug_file, "\n");
    996 #endif
    997     }
    998     s2 = bmake_malloc(sizeof(Src));
    999     s2->file = 	    str_concat2(targ->pref, s->name);
   1000     s2->pref =	    targ->pref;
   1001     s2->parent =    targ;
   1002     s2->node = 	    NULL;
   1003     s2->suff = 	    s;
   1004     s->refCount++;
   1005     s2->children =  0;
   1006     targ->children += 1;
   1007     Lst_Append(ls->l, s2);
   1008 #ifdef DEBUG_SRC
   1009     s2->cp = Lst_Init();
   1010     Lst_Append(targ->cp, s2);
   1011     fprintf(debug_file, "2 add %p %p to %p:", targ, s2, ls->l);
   1012     Lst_ForEach(ls->l, PrintAddr, NULL);
   1013     fprintf(debug_file, "\n");
   1014 #endif
   1015 
   1016     return 0;
   1017 }
   1018 
   1019 /* Add all the children of targ as Src structures to the given list.
   1020  *
   1021  * Input:
   1022  *	l		list to which to add the new level
   1023  *	targ		Src structure to use as the parent
   1024  */
   1025 static void
   1026 SuffAddLevel(Lst l, Src *targ)
   1027 {
   1028     LstSrc         ls;
   1029 
   1030     ls.s = targ;
   1031     ls.l = l;
   1032 
   1033     Lst_ForEach(targ->suff->children, SuffAddSrc, &ls);
   1034 }
   1035 
   1036 /* Free the first Src in the list that doesn't have a reference count.
   1037  * Return whether a Src was removed. */
   1038 static Boolean
   1039 SuffRemoveSrc(Lst l)
   1040 {
   1041     LstNode ln;
   1042     Src *s;
   1043 
   1044     Lst_Open(l);
   1045 
   1046 #ifdef DEBUG_SRC
   1047     fprintf(debug_file, "cleaning %lx: ", (unsigned long) l);
   1048     Lst_ForEach(l, PrintAddr, NULL);
   1049     fprintf(debug_file, "\n");
   1050 #endif
   1051 
   1052     while ((ln = Lst_Next(l)) != NULL) {
   1053 	s = LstNode_Datum(ln);
   1054 	if (s->children == 0) {
   1055 	    free(s->file);
   1056 	    if (!s->parent)
   1057 		free(s->pref);
   1058 	    else {
   1059 #ifdef DEBUG_SRC
   1060 		LstNode ln2 = Lst_FindDatum(s->parent->cp, s);
   1061 		if (ln2 != NULL)
   1062 		    Lst_Remove(s->parent->cp, ln2);
   1063 #endif
   1064 		--s->parent->children;
   1065 	    }
   1066 #ifdef DEBUG_SRC
   1067 	    fprintf(debug_file, "free: [l=%p] p=%p %d\n", l, s, s->children);
   1068 	    Lst_Free(s->cp);
   1069 #endif
   1070 	    Lst_Remove(l, ln);
   1071 	    free(s);
   1072 	    Lst_Close(l);
   1073 	    return TRUE;
   1074 	}
   1075 #ifdef DEBUG_SRC
   1076 	else {
   1077 	    fprintf(debug_file, "keep: [l=%p] p=%p %d: ", l, s, s->children);
   1078 	    Lst_ForEach(s->cp, PrintAddr, NULL);
   1079 	    fprintf(debug_file, "\n");
   1080 	}
   1081 #endif
   1082     }
   1083 
   1084     Lst_Close(l);
   1085 
   1086     return FALSE;
   1087 }
   1088 
   1089 /* Find the first existing file/target in the list srcs.
   1090  *
   1091  * Input:
   1092  *	srcs		list of Src structures to search through
   1093  *
   1094  * Results:
   1095  *	The lowest structure in the chain of transformations, or NULL.
   1096  */
   1097 static Src *
   1098 SuffFindThem(Lst srcs, Lst slst)
   1099 {
   1100     Src            *s;		/* current Src */
   1101     Src		   *rs;		/* returned Src */
   1102     char	   *ptr;
   1103 
   1104     rs = NULL;
   1105 
   1106     while (!Lst_IsEmpty(srcs)) {
   1107 	s = Lst_Dequeue(srcs);
   1108 
   1109 	SUFF_DEBUG1("\ttrying %s...", s->file);
   1110 
   1111 	/*
   1112 	 * A file is considered to exist if either a node exists in the
   1113 	 * graph for it or the file actually exists.
   1114 	 */
   1115 	if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
   1116 #ifdef DEBUG_SRC
   1117 	    fprintf(debug_file, "remove %p from %p\n", s, srcs);
   1118 #endif
   1119 	    rs = s;
   1120 	    break;
   1121 	}
   1122 
   1123 	if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
   1124 	    rs = s;
   1125 #ifdef DEBUG_SRC
   1126 	    fprintf(debug_file, "remove %p from %p\n", s, srcs);
   1127 #endif
   1128 	    free(ptr);
   1129 	    break;
   1130 	}
   1131 
   1132 	SUFF_DEBUG0("not there\n");
   1133 
   1134 	SuffAddLevel(srcs, s);
   1135 	Lst_Append(slst, s);
   1136     }
   1137 
   1138     if (rs) {
   1139 	SUFF_DEBUG0("got it\n");
   1140     }
   1141     return rs;
   1142 }
   1143 
   1144 /* See if any of the children of the target in the Src structure is one from
   1145  * which the target can be transformed. If there is one, a Src structure is
   1146  * put together for it and returned.
   1147  *
   1148  * Input:
   1149  *	targ		Src to play with
   1150  *
   1151  * Results:
   1152  *	The Src of the "winning" child, or NULL.
   1153  */
   1154 static Src *
   1155 SuffFindCmds(Src *targ, Lst slst)
   1156 {
   1157     LstNode 	  	ln; 	/* General-purpose list node */
   1158     GNode		*t, 	/* Target GNode */
   1159 			*s; 	/* Source GNode */
   1160     int	    	  	prefLen;/* The length of the defined prefix */
   1161     Suff    	  	*suff;	/* Suffix on matching beastie */
   1162     Src	    	  	*ret;	/* Return value */
   1163     char    	  	*cp;
   1164 
   1165     t = targ->node;
   1166     Lst_Open(t->children);
   1167     prefLen = strlen(targ->pref);
   1168 
   1169     for (;;) {
   1170 	ln = Lst_Next(t->children);
   1171 	if (ln == NULL) {
   1172 	    Lst_Close(t->children);
   1173 	    return NULL;
   1174 	}
   1175 	s = LstNode_Datum(ln);
   1176 
   1177 	if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
   1178 	    /*
   1179 	     * We haven't looked to see if .OPTIONAL files exist yet, so
   1180 	     * don't use one as the implicit source.
   1181 	     * This allows us to use .OPTIONAL in .depend files so make won't
   1182 	     * complain "don't know how to make xxx.h' when a dependent file
   1183 	     * has been moved/deleted.
   1184 	     */
   1185 	    continue;
   1186 	}
   1187 
   1188 	cp = strrchr(s->name, '/');
   1189 	if (cp == NULL) {
   1190 	    cp = s->name;
   1191 	} else {
   1192 	    cp++;
   1193 	}
   1194 	if (strncmp(cp, targ->pref, prefLen) != 0)
   1195 	    continue;
   1196 	/*
   1197 	 * The node matches the prefix ok, see if it has a known
   1198 	 * suffix.
   1199 	 */
   1200 	ln = Lst_Find(sufflist, SuffSuffHasName, &cp[prefLen]);
   1201 	if (ln == NULL)
   1202 	    continue;
   1203 	/*
   1204 	 * It even has a known suffix, see if there's a transformation
   1205 	 * defined between the node's suffix and the target's suffix.
   1206 	 *
   1207 	 * XXX: Handle multi-stage transformations here, too.
   1208 	 */
   1209 	suff = LstNode_Datum(ln);
   1210 
   1211 	/* XXX: Can targ->suff be NULL here? */
   1212 	if (targ->suff != NULL &&
   1213 	    Lst_FindDatum(suff->parents, targ->suff) != NULL)
   1214 	    break;
   1215     }
   1216 
   1217     /*
   1218      * Hot Damn! Create a new Src structure to describe
   1219      * this transformation (making sure to duplicate the
   1220      * source node's name so Suff_FindDeps can free it
   1221      * again (ick)), and return the new structure.
   1222      */
   1223     ret = bmake_malloc(sizeof(Src));
   1224     ret->file = bmake_strdup(s->name);
   1225     ret->pref = targ->pref;
   1226     ret->suff = suff;
   1227     suff->refCount++;
   1228     ret->parent = targ;
   1229     ret->node = s;
   1230     ret->children = 0;
   1231     targ->children += 1;
   1232 #ifdef DEBUG_SRC
   1233     ret->cp = Lst_Init();
   1234     fprintf(debug_file, "3 add %p %p\n", targ, ret);
   1235     Lst_Append(targ->cp, ret);
   1236 #endif
   1237     Lst_Append(slst, ret);
   1238     SUFF_DEBUG1("\tusing existing source %s\n", s->name);
   1239     Lst_Close(t->children);
   1240     return ret;
   1241 }
   1242 
   1243 /* Expand the names of any children of a given node that contain variable
   1244  * invocations or file wildcards into actual targets.
   1245  *
   1246  * The expanded node is removed from the parent's list of children, and the
   1247  * parent's unmade counter is decremented, but other nodes may be added.
   1248  *
   1249  * Input:
   1250  *	cln		Child to examine
   1251  *	pgn		Parent node being processed
   1252  */
   1253 static void
   1254 SuffExpandChildren(LstNode cln, GNode *pgn)
   1255 {
   1256     GNode   	*cgn = LstNode_Datum(cln);
   1257     GNode	*gn;	    /* New source 8) */
   1258     char	*cp;	    /* Expanded value */
   1259 
   1260     if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
   1261 	/* It is all too hard to process the result of .ORDER */
   1262 	return;
   1263 
   1264     if (cgn->type & OP_WAIT)
   1265 	/* Ignore these (& OP_PHONY ?) */
   1266 	return;
   1267 
   1268     /*
   1269      * First do variable expansion -- this takes precedence over
   1270      * wildcard expansion. If the result contains wildcards, they'll be gotten
   1271      * to later since the resulting words are tacked on to the end of
   1272      * the children list.
   1273      */
   1274     if (strchr(cgn->name, '$') == NULL) {
   1275 	SuffExpandWildcards(cln, pgn);
   1276 	return;
   1277     }
   1278 
   1279     SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
   1280     cp = Var_Subst(cgn->name, pgn, VARE_UNDEFERR|VARE_WANTRES);
   1281 
   1282     {
   1283 	Lst	    members = Lst_Init();
   1284 
   1285 	if (cgn->type & OP_ARCHV) {
   1286 	    /*
   1287 	     * Node was an archive(member) target, so we want to call
   1288 	     * on the Arch module to find the nodes for us, expanding
   1289 	     * variables in the parent's context.
   1290 	     */
   1291 	    char	*sacrifice = cp;
   1292 
   1293 	    (void)Arch_ParseArchive(&sacrifice, members, pgn);
   1294 	} else {
   1295 	    /*
   1296 	     * Break the result into a vector of strings whose nodes
   1297 	     * we can find, then add those nodes to the members list.
   1298 	     * Unfortunately, we can't use brk_string b/c it
   1299 	     * doesn't understand about variable specifications with
   1300 	     * spaces in them...
   1301 	     */
   1302 	    char	    *start;
   1303 	    char	    *initcp = cp;   /* For freeing... */
   1304 
   1305 	    for (start = cp; *start == ' ' || *start == '\t'; start++)
   1306 		continue;
   1307 	    cp = start;
   1308 	    while (*cp != '\0') {
   1309 		if (*cp == ' ' || *cp == '\t') {
   1310 		    /*
   1311 		     * White-space -- terminate element, find the node,
   1312 		     * add it, skip any further spaces.
   1313 		     */
   1314 		    *cp++ = '\0';
   1315 		    gn = Targ_FindNode(start, TARG_CREATE);
   1316 		    Lst_Append(members, gn);
   1317 		    while (*cp == ' ' || *cp == '\t') {
   1318 			cp++;
   1319 		    }
   1320 		    start = cp;		/* Continue at the next non-space. */
   1321 		} else if (*cp == '$') {
   1322 		    /*
   1323 		     * Start of a variable spec -- contact variable module
   1324 		     * to find the end so we can skip over it.
   1325 		     */
   1326 		    const char *nested_p = cp;
   1327 		    const char	*junk;
   1328 		    void	*freeIt;
   1329 
   1330 		    /* XXX: Why VARE_WANTRES when the result is not used? */
   1331 		    junk = Var_ParsePP(&nested_p, pgn,
   1332 				       VARE_UNDEFERR|VARE_WANTRES, &freeIt);
   1333 		    if (junk == var_Error) {
   1334 			Parse_Error(PARSE_FATAL,
   1335 				    "Malformed variable expression at \"%s\"",
   1336 				    cp);
   1337 			cp++;
   1338 		    } else {
   1339 			cp += nested_p - cp;
   1340 		    }
   1341 
   1342 		    free(freeIt);
   1343 		} else if (*cp == '\\' && cp[1] != '\0') {
   1344 		    /*
   1345 		     * Escaped something -- skip over it
   1346 		     */
   1347 		    /* XXX: In other places, escaping at this syntactical
   1348 		     * position is done by a '$', not a '\'.  The '\' is only
   1349 		     * used in variable modifiers. */
   1350 		    cp += 2;
   1351 		} else {
   1352 		    cp++;
   1353 		}
   1354 	    }
   1355 
   1356 	    if (cp != start) {
   1357 		/*
   1358 		 * Stuff left over -- add it to the list too
   1359 		 */
   1360 		gn = Targ_FindNode(start, TARG_CREATE);
   1361 		Lst_Append(members, gn);
   1362 	    }
   1363 	    /*
   1364 	     * Point cp back at the beginning again so the variable value
   1365 	     * can be freed.
   1366 	     */
   1367 	    cp = initcp;
   1368 	}
   1369 
   1370 	/*
   1371 	 * Add all elements of the members list to the parent node.
   1372 	 */
   1373 	while(!Lst_IsEmpty(members)) {
   1374 	    gn = Lst_Dequeue(members);
   1375 
   1376 	    SUFF_DEBUG1("%s...", gn->name);
   1377 	    /* Add gn to the parents child list before the original child */
   1378 	    Lst_InsertBefore(pgn->children, cln, gn);
   1379 	    Lst_Append(gn->parents, pgn);
   1380 	    pgn->unmade++;
   1381 	    /* Expand wildcards on new node */
   1382 	    SuffExpandWildcards(LstNode_Prev(cln), pgn);
   1383 	}
   1384 	Lst_Free(members);
   1385 
   1386 	/*
   1387 	 * Free the result
   1388 	 */
   1389 	free(cp);
   1390     }
   1391 
   1392     SUFF_DEBUG0("\n");
   1393 
   1394     /*
   1395      * Now the source is expanded, remove it from the list of children to
   1396      * keep it from being processed.
   1397      */
   1398     pgn->unmade--;
   1399     Lst_Remove(pgn->children, cln);
   1400     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1401 }
   1402 
   1403 static void
   1404 SuffExpandWildcards(LstNode cln, GNode *pgn)
   1405 {
   1406     GNode   	*cgn = LstNode_Datum(cln);
   1407     GNode	*gn;	    /* New source 8) */
   1408     char	*cp;	    /* Expanded value */
   1409     Lst 	explist;    /* List of expansions */
   1410 
   1411     if (!Dir_HasWildcards(cgn->name))
   1412 	return;
   1413 
   1414     /*
   1415      * Expand the word along the chosen path
   1416      */
   1417     explist = Lst_Init();
   1418     Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
   1419 
   1420     while (!Lst_IsEmpty(explist)) {
   1421 	/*
   1422 	 * Fetch next expansion off the list and find its GNode
   1423 	 */
   1424 	cp = Lst_Dequeue(explist);
   1425 
   1426 	SUFF_DEBUG1("%s...", cp);
   1427 	gn = Targ_FindNode(cp, TARG_CREATE);
   1428 
   1429 	/* Add gn to the parents child list before the original child */
   1430 	Lst_InsertBefore(pgn->children, cln, gn);
   1431 	Lst_Append(gn->parents, pgn);
   1432 	pgn->unmade++;
   1433     }
   1434 
   1435     Lst_Free(explist);
   1436 
   1437     SUFF_DEBUG0("\n");
   1438 
   1439     /*
   1440      * Now the source is expanded, remove it from the list of children to
   1441      * keep it from being processed.
   1442      */
   1443     pgn->unmade--;
   1444     Lst_Remove(pgn->children, cln);
   1445     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1446 }
   1447 
   1448 /* Find a path along which to expand the node.
   1449  *
   1450  * If the word has a known suffix, use that path.
   1451  * If it has no known suffix, use the default system search path.
   1452  *
   1453  * Input:
   1454  *	gn		Node being examined
   1455  *
   1456  * Results:
   1457  *	The appropriate path to search for the GNode.
   1458  */
   1459 Lst
   1460 Suff_FindPath(GNode* gn)
   1461 {
   1462     Suff *suff = gn->suffix;
   1463 
   1464     if (suff == NULL) {
   1465 	SuffSuffGetSuffixArgs sd;   /* Search string data */
   1466 	LstNode ln;
   1467 	sd.len = strlen(gn->name);
   1468 	sd.ename = gn->name + sd.len;
   1469 	ln = Lst_Find(sufflist, SuffSuffIsSuffix, &sd);
   1470 
   1471 	SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
   1472 	if (ln != NULL)
   1473 	    suff = LstNode_Datum(ln);
   1474 	/* XXX: Here we can save the suffix so we don't have to do this again */
   1475     }
   1476 
   1477     if (suff != NULL) {
   1478 	SUFF_DEBUG1("suffix is \"%s\"...", suff->name);
   1479 	return suff->searchPath;
   1480     } else {
   1481 	/*
   1482 	 * Use default search path
   1483 	 */
   1484 	return dirSearchPath;
   1485     }
   1486 }
   1487 
   1488 /* Apply a transformation rule, given the source and target nodes and
   1489  * suffixes.
   1490  *
   1491  * Input:
   1492  *	tGn		Target node
   1493  *	sGn		Source node
   1494  *	t		Target suffix
   1495  *	s		Source suffix
   1496  *
   1497  * Results:
   1498  *	TRUE if successful, FALSE if not.
   1499  *
   1500  * Side Effects:
   1501  *	The source and target are linked and the commands from the
   1502  *	transformation are added to the target node's commands list.
   1503  *	All attributes but OP_DEPMASK and OP_TRANSFORM are applied
   1504  *	to the target. The target also inherits all the sources for
   1505  *	the transformation rule.
   1506  */
   1507 static Boolean
   1508 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
   1509 {
   1510     LstNode 	ln, nln;    /* General node */
   1511     char    	*tname;	    /* Name of transformation rule */
   1512     GNode   	*gn;	    /* Node for same */
   1513 
   1514     /*
   1515      * Form the proper links between the target and source.
   1516      */
   1517     Lst_Append(tGn->children, sGn);
   1518     Lst_Append(sGn->parents, tGn);
   1519     tGn->unmade += 1;
   1520 
   1521     /*
   1522      * Locate the transformation rule itself
   1523      */
   1524     tname = str_concat2(s->name, t->name);
   1525     ln = Lst_Find(transforms, SuffGNHasName, tname);
   1526     free(tname);
   1527 
   1528     if (ln == NULL) {
   1529 	/*
   1530 	 * Not really such a transformation rule (can happen when we're
   1531 	 * called to link an OP_MEMBER and OP_ARCHV node), so return
   1532 	 * FALSE.
   1533 	 */
   1534 	return FALSE;
   1535     }
   1536 
   1537     gn = LstNode_Datum(ln);
   1538 
   1539     SUFF_DEBUG3("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
   1540 
   1541     /*
   1542      * Record last child for expansion purposes
   1543      */
   1544     ln = Lst_Last(tGn->children);
   1545 
   1546     /*
   1547      * Pass the buck to Make_HandleUse to apply the rule
   1548      */
   1549     (void)Make_HandleUse(gn, tGn);
   1550 
   1551     /*
   1552      * Deal with wildcards and variables in any acquired sources
   1553      */
   1554     for (ln = ln != NULL ? LstNode_Next(ln) : NULL; ln != NULL; ln = nln) {
   1555 	nln = LstNode_Next(ln);
   1556 	SuffExpandChildren(ln, tGn);
   1557     }
   1558 
   1559     /*
   1560      * Keep track of another parent to which this beast is transformed so
   1561      * the .IMPSRC variable can be set correctly for the parent.
   1562      */
   1563     Lst_Append(sGn->implicitParents, tGn);
   1564 
   1565     return TRUE;
   1566 }
   1567 
   1568 
   1569 /* Locate dependencies for an OP_ARCHV node.
   1570  *
   1571  * Input:
   1572  *	gn		Node for which to locate dependencies
   1573  *
   1574  * Side Effects:
   1575  *	Same as Suff_FindDeps
   1576  */
   1577 static void
   1578 SuffFindArchiveDeps(GNode *gn, Lst slst)
   1579 {
   1580     char    	*eoarch;    /* End of archive portion */
   1581     char    	*eoname;    /* End of member portion */
   1582     GNode   	*mem;	    /* Node for member */
   1583     LstNode 	ln, nln;    /* Next suffix node to check */
   1584     Suff    	*ms;	    /* Suffix descriptor for member */
   1585     char    	*name;	    /* Start of member's name */
   1586 
   1587     /*
   1588      * The node is an archive(member) pair. so we must find a
   1589      * suffix for both of them.
   1590      */
   1591     eoarch = strchr(gn->name, '(');
   1592     eoname = strchr(eoarch, ')');
   1593 
   1594     /*
   1595      * Caller guarantees the format `libname(member)', via
   1596      * Arch_ParseArchive.
   1597      */
   1598     assert(eoarch != NULL);
   1599     assert(eoname != NULL);
   1600 
   1601     *eoname = '\0';	  /* Nuke parentheses during suffix search */
   1602     *eoarch = '\0';	  /* So a suffix can be found */
   1603 
   1604     name = eoarch + 1;
   1605 
   1606     /*
   1607      * To simplify things, call Suff_FindDeps recursively on the member now,
   1608      * so we can simply compare the member's .PREFIX and .TARGET variables
   1609      * to locate its suffix. This allows us to figure out the suffix to
   1610      * use for the archive without having to do a quadratic search over the
   1611      * suffix list, backtracking for each one...
   1612      */
   1613     mem = Targ_FindNode(name, TARG_CREATE);
   1614     SuffFindDeps(mem, slst);
   1615 
   1616     /*
   1617      * Create the link between the two nodes right off
   1618      */
   1619     Lst_Append(gn->children, mem);
   1620     Lst_Append(mem->parents, gn);
   1621     gn->unmade += 1;
   1622 
   1623     /*
   1624      * Copy in the variables from the member node to this one.
   1625      */
   1626     {
   1627 	char *freeIt;
   1628 	Var_Set(PREFIX, Var_Value(PREFIX, mem, &freeIt), gn);
   1629 	bmake_free(freeIt);
   1630 	Var_Set(TARGET, Var_Value(TARGET, mem, &freeIt), gn);
   1631 	bmake_free(freeIt);
   1632     }
   1633 
   1634     ms = mem->suffix;
   1635     if (ms == NULL) {
   1636 	/*
   1637 	 * Didn't know what it was -- use .NULL suffix if not in make mode
   1638 	 */
   1639 	SUFF_DEBUG0("using null suffix\n");
   1640 	ms = suffNull;
   1641     }
   1642 
   1643 
   1644     /*
   1645      * Set the other two local variables required for this target.
   1646      */
   1647     Var_Set(MEMBER, name, gn);
   1648     Var_Set(ARCHIVE, gn->name, gn);
   1649 
   1650     /*
   1651      * Set $@ for compatibility with other makes
   1652      */
   1653     Var_Set(TARGET, gn->name, gn);
   1654 
   1655     /*
   1656      * Now we've got the important local variables set, expand any sources
   1657      * that still contain variables or wildcards in their names.
   1658      */
   1659     for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
   1660 	nln = LstNode_Next(ln);
   1661 	SuffExpandChildren(ln, gn);
   1662     }
   1663 
   1664     if (ms != NULL) {
   1665 	/*
   1666 	 * Member has a known suffix, so look for a transformation rule from
   1667 	 * it to a possible suffix of the archive. Rather than searching
   1668 	 * through the entire list, we just look at suffixes to which the
   1669 	 * member's suffix may be transformed...
   1670 	 */
   1671 	SuffSuffGetSuffixArgs sd;	/* Search string data */
   1672 
   1673 	/*
   1674 	 * Use first matching suffix...
   1675 	 */
   1676 	sd.len = eoarch - gn->name;
   1677 	sd.ename = eoarch;
   1678 	ln = Lst_Find(ms->parents, SuffSuffIsSuffix, &sd);
   1679 
   1680 	if (ln != NULL) {
   1681 	    /*
   1682 	     * Got one -- apply it
   1683 	     */
   1684 	    Suff *suff = LstNode_Datum(ln);
   1685 	    if (!SuffApplyTransform(gn, mem, suff, ms)) {
   1686 		SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
   1687 			    ms->name, suff->name);
   1688 	    }
   1689 	}
   1690     }
   1691 
   1692     /*
   1693      * Replace the opening and closing parens now we've no need of the separate
   1694      * pieces.
   1695      */
   1696     *eoarch = '('; *eoname = ')';
   1697 
   1698     /*
   1699      * Pretend gn appeared to the left of a dependency operator so
   1700      * the user needn't provide a transformation from the member to the
   1701      * archive.
   1702      */
   1703     if (OP_NOP(gn->type)) {
   1704 	gn->type |= OP_DEPENDS;
   1705     }
   1706 
   1707     /*
   1708      * Flag the member as such so we remember to look in the archive for
   1709      * its modification time. The OP_JOIN | OP_MADE is needed because this
   1710      * target should never get made.
   1711      */
   1712     mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
   1713 }
   1714 
   1715 /* Locate implicit dependencies for regular targets.
   1716  *
   1717  * Input:
   1718  *	gn		Node for which to find sources
   1719  *
   1720  * Side Effects:
   1721  *	Same as Suff_FindDeps
   1722  */
   1723 static void
   1724 SuffFindNormalDeps(GNode *gn, Lst slst)
   1725 {
   1726     char    	*eoname;    /* End of name */
   1727     char    	*sopref;    /* Start of prefix */
   1728     LstNode 	ln, nln;    /* Next suffix node to check */
   1729     Lst	    	srcs;	    /* List of sources at which to look */
   1730     Lst	    	targs;	    /* List of targets to which things can be
   1731 			     * transformed. They all have the same file,
   1732 			     * but different suff and pref fields */
   1733     Src	    	*bottom;    /* Start of found transformation path */
   1734     Src 	*src;	    /* General Src pointer */
   1735     char    	*pref;	    /* Prefix to use */
   1736     Src	    	*targ;	    /* General Src target pointer */
   1737     SuffSuffGetSuffixArgs sd; /* Search string data */
   1738 
   1739 
   1740     sd.len = strlen(gn->name);
   1741     sd.ename = eoname = gn->name + sd.len;
   1742 
   1743     sopref = gn->name;
   1744 
   1745     /*
   1746      * Begin at the beginning...
   1747      */
   1748     ln = Lst_First(sufflist);
   1749     srcs = Lst_Init();
   1750     targs = Lst_Init();
   1751 
   1752     /*
   1753      * We're caught in a catch-22 here. On the one hand, we want to use any
   1754      * transformation implied by the target's sources, but we can't examine
   1755      * the sources until we've expanded any variables/wildcards they may hold,
   1756      * and we can't do that until we've set up the target's local variables
   1757      * and we can't do that until we know what the proper suffix for the
   1758      * target is (in case there are two suffixes one of which is a suffix of
   1759      * the other) and we can't know that until we've found its implied
   1760      * source, which we may not want to use if there's an existing source
   1761      * that implies a different transformation.
   1762      *
   1763      * In an attempt to get around this, which may not work all the time,
   1764      * but should work most of the time, we look for implied sources first,
   1765      * checking transformations to all possible suffixes of the target,
   1766      * use what we find to set the target's local variables, expand the
   1767      * children, then look for any overriding transformations they imply.
   1768      * Should we find one, we discard the one we found before.
   1769      */
   1770     bottom = NULL;
   1771     targ = NULL;
   1772 
   1773     if (!(gn->type & OP_PHONY)) {
   1774 
   1775 	while (ln != NULL) {
   1776 	    /*
   1777 	     * Look for next possible suffix...
   1778 	     */
   1779 	    ln = Lst_FindFrom(sufflist, ln, SuffSuffIsSuffix, &sd);
   1780 
   1781 	    if (ln != NULL) {
   1782 		const char *eopref;
   1783 
   1784 		/*
   1785 		 * Allocate a Src structure to which things can be transformed
   1786 		 */
   1787 		targ = bmake_malloc(sizeof(Src));
   1788 		targ->file = bmake_strdup(gn->name);
   1789 		targ->suff = LstNode_Datum(ln);
   1790 		targ->suff->refCount++;
   1791 		targ->node = gn;
   1792 		targ->parent = NULL;
   1793 		targ->children = 0;
   1794 #ifdef DEBUG_SRC
   1795 		targ->cp = Lst_Init();
   1796 #endif
   1797 
   1798 		eopref = eoname - targ->suff->nameLen;
   1799 		targ->pref = bmake_strsedup(sopref, eopref);
   1800 
   1801 		/*
   1802 		 * Add nodes from which the target can be made
   1803 		 */
   1804 		SuffAddLevel(srcs, targ);
   1805 
   1806 		/*
   1807 		 * Record the target so we can nuke it
   1808 		 */
   1809 		Lst_Append(targs, targ);
   1810 
   1811 		/*
   1812 		 * Search from this suffix's successor...
   1813 		 */
   1814 		ln = LstNode_Next(ln);
   1815 	    }
   1816 	}
   1817 
   1818 	/*
   1819 	 * Handle target of unknown suffix...
   1820 	 */
   1821 	if (Lst_IsEmpty(targs) && suffNull != NULL) {
   1822 	    SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n",
   1823 			gn->name);
   1824 
   1825 	    targ = bmake_malloc(sizeof(Src));
   1826 	    targ->file = bmake_strdup(gn->name);
   1827 	    targ->suff = suffNull;
   1828 	    targ->suff->refCount++;
   1829 	    targ->node = gn;
   1830 	    targ->parent = NULL;
   1831 	    targ->children = 0;
   1832 	    targ->pref = bmake_strdup(sopref);
   1833 #ifdef DEBUG_SRC
   1834 	    targ->cp = Lst_Init();
   1835 #endif
   1836 
   1837 	    /*
   1838 	     * Only use the default suffix rules if we don't have commands
   1839 	     * defined for this gnode; traditional make programs used to
   1840 	     * not define suffix rules if the gnode had children but we
   1841 	     * don't do this anymore.
   1842 	     */
   1843 	    if (Lst_IsEmpty(gn->commands))
   1844 		SuffAddLevel(srcs, targ);
   1845 	    else {
   1846 		SUFF_DEBUG0("not ");
   1847 	    }
   1848 
   1849 	    SUFF_DEBUG0("adding suffix rules\n");
   1850 
   1851 	    Lst_Append(targs, targ);
   1852 	}
   1853 
   1854 	/*
   1855 	 * Using the list of possible sources built up from the target
   1856 	 * suffix(es), try and find an existing file/target that matches.
   1857 	 */
   1858 	bottom = SuffFindThem(srcs, slst);
   1859 
   1860 	if (bottom == NULL) {
   1861 	    /*
   1862 	     * No known transformations -- use the first suffix found
   1863 	     * for setting the local variables.
   1864 	     */
   1865 	    if (!Lst_IsEmpty(targs)) {
   1866 		targ = LstNode_Datum(Lst_First(targs));
   1867 	    } else {
   1868 		targ = NULL;
   1869 	    }
   1870 	} else {
   1871 	    /*
   1872 	     * Work up the transformation path to find the suffix of the
   1873 	     * target to which the transformation was made.
   1874 	     */
   1875 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   1876 		continue;
   1877 	}
   1878     }
   1879 
   1880     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
   1881 
   1882     pref = (targ != NULL) ? targ->pref : gn->name;
   1883     Var_Set(PREFIX, pref, gn);
   1884 
   1885     /*
   1886      * Now we've got the important local variables set, expand any sources
   1887      * that still contain variables or wildcards in their names.
   1888      */
   1889     for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
   1890 	nln = LstNode_Next(ln);
   1891 	SuffExpandChildren(ln, gn);
   1892     }
   1893 
   1894     if (targ == NULL) {
   1895 	SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
   1896 
   1897 sfnd_abort:
   1898 	/*
   1899 	 * Deal with finding the thing on the default search path. We
   1900 	 * always do that, not only if the node is only a source (not
   1901 	 * on the lhs of a dependency operator or [XXX] it has neither
   1902 	 * children or commands) as the old pmake did.
   1903 	 */
   1904 	if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
   1905 	    free(gn->path);
   1906 	    gn->path = Dir_FindFile(gn->name,
   1907 				    (targ == NULL ? dirSearchPath :
   1908 				     targ->suff->searchPath));
   1909 	    if (gn->path != NULL) {
   1910 		char *ptr;
   1911 		Var_Set(TARGET, gn->path, gn);
   1912 
   1913 		if (targ != NULL) {
   1914 		    /*
   1915 		     * Suffix known for the thing -- trim the suffix off
   1916 		     * the path to form the proper .PREFIX variable.
   1917 		     */
   1918 		    int     savep = strlen(gn->path) - targ->suff->nameLen;
   1919 		    char    savec;
   1920 
   1921 		    if (gn->suffix)
   1922 			gn->suffix->refCount--;
   1923 		    gn->suffix = targ->suff;
   1924 		    gn->suffix->refCount++;
   1925 
   1926 		    savec = gn->path[savep];
   1927 		    gn->path[savep] = '\0';
   1928 
   1929 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   1930 			ptr++;
   1931 		    else
   1932 			ptr = gn->path;
   1933 
   1934 		    Var_Set(PREFIX, ptr, gn);
   1935 
   1936 		    gn->path[savep] = savec;
   1937 		} else {
   1938 		    /*
   1939 		     * The .PREFIX gets the full path if the target has
   1940 		     * no known suffix.
   1941 		     */
   1942 		    if (gn->suffix)
   1943 			gn->suffix->refCount--;
   1944 		    gn->suffix = NULL;
   1945 
   1946 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   1947 			ptr++;
   1948 		    else
   1949 			ptr = gn->path;
   1950 
   1951 		    Var_Set(PREFIX, ptr, gn);
   1952 		}
   1953 	    }
   1954 	}
   1955 
   1956 	goto sfnd_return;
   1957     }
   1958 
   1959     /*
   1960      * If the suffix indicates that the target is a library, mark that in
   1961      * the node's type field.
   1962      */
   1963     if (targ->suff->flags & SUFF_LIBRARY) {
   1964 	gn->type |= OP_LIB;
   1965     }
   1966 
   1967     /*
   1968      * Check for overriding transformation rule implied by sources
   1969      */
   1970     if (!Lst_IsEmpty(gn->children)) {
   1971 	src = SuffFindCmds(targ, slst);
   1972 
   1973 	if (src != NULL) {
   1974 	    /*
   1975 	     * Free up all the Src structures in the transformation path
   1976 	     * up to, but not including, the parent node.
   1977 	     */
   1978 	    while (bottom && bottom->parent != NULL) {
   1979 		if (Lst_FindDatum(slst, bottom) == NULL) {
   1980 		    Lst_Append(slst, bottom);
   1981 		}
   1982 		bottom = bottom->parent;
   1983 	    }
   1984 	    bottom = src;
   1985 	}
   1986     }
   1987 
   1988     if (bottom == NULL) {
   1989 	/*
   1990 	 * No idea from where it can come -- return now.
   1991 	 */
   1992 	goto sfnd_abort;
   1993     }
   1994 
   1995     /*
   1996      * We now have a list of Src structures headed by 'bottom' and linked via
   1997      * their 'parent' pointers. What we do next is create links between
   1998      * source and target nodes (which may or may not have been created)
   1999      * and set the necessary local variables in each target. The
   2000      * commands for each target are set from the commands of the
   2001      * transformation rule used to get from the src suffix to the targ
   2002      * suffix. Note that this causes the commands list of the original
   2003      * node, gn, to be replaced by the commands of the final
   2004      * transformation rule. Also, the unmade field of gn is incremented.
   2005      * Etc.
   2006      */
   2007     if (bottom->node == NULL) {
   2008 	bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
   2009     }
   2010 
   2011     for (src = bottom; src->parent != NULL; src = src->parent) {
   2012 	targ = src->parent;
   2013 
   2014 	if (src->node->suffix)
   2015 	    src->node->suffix->refCount--;
   2016 	src->node->suffix = src->suff;
   2017 	src->node->suffix->refCount++;
   2018 
   2019 	if (targ->node == NULL) {
   2020 	    targ->node = Targ_FindNode(targ->file, TARG_CREATE);
   2021 	}
   2022 
   2023 	SuffApplyTransform(targ->node, src->node,
   2024 			   targ->suff, src->suff);
   2025 
   2026 	if (targ->node != gn) {
   2027 	    /*
   2028 	     * Finish off the dependency-search process for any nodes
   2029 	     * between bottom and gn (no point in questing around the
   2030 	     * filesystem for their implicit source when it's already
   2031 	     * known). Note that the node can't have any sources that
   2032 	     * need expanding, since SuffFindThem will stop on an existing
   2033 	     * node, so all we need to do is set the standard and System V
   2034 	     * variables.
   2035 	     */
   2036 	    targ->node->type |= OP_DEPS_FOUND;
   2037 
   2038 	    Var_Set(PREFIX, targ->pref, targ->node);
   2039 
   2040 	    Var_Set(TARGET, targ->node->name, targ->node);
   2041 	}
   2042     }
   2043 
   2044     if (gn->suffix)
   2045 	gn->suffix->refCount--;
   2046     gn->suffix = src->suff;
   2047     gn->suffix->refCount++;
   2048 
   2049     /*
   2050      * Nuke the transformation path and the Src structures left over in the
   2051      * two lists.
   2052      */
   2053 sfnd_return:
   2054     if (bottom)
   2055 	if (Lst_FindDatum(slst, bottom) == NULL)
   2056 	    Lst_Append(slst, bottom);
   2057 
   2058     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
   2059 	continue;
   2060 
   2061     Lst_MoveAll(slst, srcs);
   2062     Lst_MoveAll(slst, targs);
   2063 }
   2064 
   2065 
   2066 /* Find implicit sources for the target described by the graph node.
   2067  *
   2068  * Nodes are added to the graph below the passed-in node. The nodes are
   2069  * marked to have their IMPSRC variable filled in. The PREFIX variable is set
   2070  * for the given node and all its implied children.
   2071  *
   2072  * The path found by this target is the shortest path in the transformation
   2073  * graph, which may pass through non-existent targets, to an existing target.
   2074  * The search continues on all paths from the root suffix until a file is
   2075  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
   2076  * .l,v file exists but the .c and .l files don't, the search will branch out
   2077  * in all directions from .o and again from all the nodes on the next level
   2078  * until the .l,v node is encountered.
   2079  */
   2080 void
   2081 Suff_FindDeps(GNode *gn)
   2082 {
   2083 
   2084     SuffFindDeps(gn, srclist);
   2085     while (SuffRemoveSrc(srclist))
   2086 	continue;
   2087 }
   2088 
   2089 static void
   2090 SuffFindDeps(GNode *gn, Lst slst)
   2091 {
   2092     if (gn->type & OP_DEPS_FOUND)
   2093 	return;
   2094     gn->type |= OP_DEPS_FOUND;
   2095 
   2096     /*
   2097      * Make sure we have these set, may get revised below.
   2098      */
   2099     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
   2100     Var_Set(PREFIX, gn->name, gn);
   2101 
   2102     SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
   2103 
   2104     if (gn->type & OP_ARCHV) {
   2105 	SuffFindArchiveDeps(gn, slst);
   2106     } else if (gn->type & OP_LIB) {
   2107 	/*
   2108 	 * If the node is a library, it is the arch module's job to find it
   2109 	 * and set the TARGET variable accordingly. We merely provide the
   2110 	 * search path, assuming all libraries end in ".a" (if the suffix
   2111 	 * hasn't been defined, there's nothing we can do for it, so we just
   2112 	 * set the TARGET variable to the node's name in order to give it a
   2113 	 * value).
   2114 	 */
   2115 	LstNode	ln;
   2116 	Suff	*s;
   2117 
   2118 	ln = Lst_Find(sufflist, SuffSuffHasName, LIBSUFF);
   2119 	if (gn->suffix)
   2120 	    gn->suffix->refCount--;
   2121 	if (ln != NULL) {
   2122 	    gn->suffix = s = LstNode_Datum(ln);
   2123 	    gn->suffix->refCount++;
   2124 	    Arch_FindLib(gn, s->searchPath);
   2125 	} else {
   2126 	    gn->suffix = NULL;
   2127 	    Var_Set(TARGET, gn->name, gn);
   2128 	}
   2129 	/*
   2130 	 * Because a library (-lfoo) target doesn't follow the standard
   2131 	 * filesystem conventions, we don't set the regular variables for
   2132 	 * the thing. .PREFIX is simply made empty...
   2133 	 */
   2134 	Var_Set(PREFIX, "", gn);
   2135     } else {
   2136 	SuffFindNormalDeps(gn, slst);
   2137     }
   2138 }
   2139 
   2140 /* Define which suffix is the null suffix.
   2141  *
   2142  * Need to handle the changing of the null suffix gracefully so the old
   2143  * transformation rules don't just go away.
   2144  *
   2145  * Input:
   2146  *	name		Name of null suffix
   2147  */
   2148 void
   2149 Suff_SetNull(char *name)
   2150 {
   2151     Suff    *s;
   2152     LstNode ln;
   2153 
   2154     ln = Lst_Find(sufflist, SuffSuffHasName, name);
   2155     if (ln != NULL) {
   2156 	s = LstNode_Datum(ln);
   2157 	if (suffNull != NULL) {
   2158 	    suffNull->flags &= ~SUFF_NULL;
   2159 	}
   2160 	s->flags |= SUFF_NULL;
   2161 	/*
   2162 	 * XXX: Here's where the transformation mangling would take place
   2163 	 */
   2164 	suffNull = s;
   2165     } else {
   2166 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
   2167 		     name);
   2168     }
   2169 }
   2170 
   2171 /* Initialize the suffixes module. */
   2172 void
   2173 Suff_Init(void)
   2174 {
   2175 #ifdef CLEANUP
   2176     suffClean = Lst_Init();
   2177     sufflist = Lst_Init();
   2178 #endif
   2179     srclist = Lst_Init();
   2180     transforms = Lst_Init();
   2181 
   2182     /*
   2183      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   2184      * actually go on the suffix list or everyone will think that's its
   2185      * suffix.
   2186      */
   2187     Suff_ClearSuffixes();
   2188 }
   2189 
   2190 
   2191 /* Clean up the suffixes module. */
   2192 void
   2193 Suff_End(void)
   2194 {
   2195 #ifdef CLEANUP
   2196     Lst_Destroy(sufflist, SuffFree);
   2197     Lst_Destroy(suffClean, SuffFree);
   2198     if (suffNull)
   2199 	SuffFree(suffNull);
   2200     Lst_Free(srclist);
   2201     Lst_Free(transforms);
   2202 #endif
   2203 }
   2204 
   2205 
   2206 /********************* DEBUGGING FUNCTIONS **********************/
   2207 
   2208 static int SuffPrintName(void *s, void *dummy MAKE_ATTR_UNUSED)
   2209 {
   2210 
   2211     fprintf(debug_file, "%s ", ((Suff *)s)->name);
   2212     return 0;
   2213 }
   2214 
   2215 static int
   2216 SuffPrintSuff(void *sp, void *dummy MAKE_ATTR_UNUSED)
   2217 {
   2218     Suff    *s = (Suff *)sp;
   2219 
   2220     fprintf(debug_file, "# `%s' [%d] ", s->name, s->refCount);
   2221 
   2222     if (s->flags != 0) {
   2223 	char flags_buf[SuffFlags_ToStringSize];
   2224 
   2225 	fprintf(debug_file, " (%s)",
   2226 		Enum_FlagsToString(flags_buf, sizeof flags_buf,
   2227 				   s->flags, SuffFlags_ToStringSpecs));
   2228     }
   2229     fputc('\n', debug_file);
   2230     fprintf(debug_file, "#\tTo: ");
   2231     Lst_ForEach(s->parents, SuffPrintName, NULL);
   2232     fputc('\n', debug_file);
   2233     fprintf(debug_file, "#\tFrom: ");
   2234     Lst_ForEach(s->children, SuffPrintName, NULL);
   2235     fputc('\n', debug_file);
   2236     fprintf(debug_file, "#\tSearch Path: ");
   2237     Dir_PrintPath(s->searchPath);
   2238     fputc('\n', debug_file);
   2239     return 0;
   2240 }
   2241 
   2242 static int
   2243 SuffPrintTrans(void *tp, void *dummy MAKE_ATTR_UNUSED)
   2244 {
   2245     GNode   *t = (GNode *)tp;
   2246 
   2247     fprintf(debug_file, "%-16s:", t->name);
   2248     Targ_PrintType(t->type);
   2249     fputc('\n', debug_file);
   2250     Lst_ForEach(t->commands, Targ_PrintCmd, NULL);
   2251     fputc('\n', debug_file);
   2252     return 0;
   2253 }
   2254 
   2255 void
   2256 Suff_PrintAll(void)
   2257 {
   2258     fprintf(debug_file, "#*** Suffixes:\n");
   2259     Lst_ForEach(sufflist, SuffPrintSuff, NULL);
   2260 
   2261     fprintf(debug_file, "#*** Transformations:\n");
   2262     Lst_ForEach(transforms, SuffPrintTrans, NULL);
   2263 }
   2264