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