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