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