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