Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.314
      1 /*	$NetBSD: suff.c,v 1.314 2020/11/28 22:13:56 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.314 2020/11/28 22:13:56 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     SearchPath_AddAll(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_Done(&gn->commands);
    572 	Lst_Init(&gn->commands);
    573 	Lst_Done(&gn->children);
    574 	Lst_Init(&gn->children);
    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_Done(&target->children);
    748 	Lst_Init(&target->children);
    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 *flags;
    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 		SearchPath_AddAll(inIncludes, suff->searchPath);
    850 #endif
    851 #ifdef LIBRARIES
    852 	    if (suff->flags & SUFF_LIBRARY)
    853 		SearchPath_AddAll(inLibs, suff->searchPath);
    854 #endif
    855 	    SearchPath_AddAll(suff->searchPath, dirSearchPath);
    856 	} else {
    857 	    Lst_Destroy(suff->searchPath, Dir_Destroy);
    858 	    suff->searchPath = Dir_CopyDirSearchPath();
    859 	}
    860     }
    861 
    862     flags = SearchPath_ToFlags("-I", inIncludes);
    863     Var_Set(".INCLUDES", flags, VAR_GLOBAL);
    864     free(flags);
    865 
    866     flags = SearchPath_ToFlags("-L", inLibs);
    867     Var_Set(".LIBS", flags, VAR_GLOBAL);
    868     free(flags);
    869 
    870     Lst_Destroy(inIncludes, Dir_Destroy);
    871     Lst_Destroy(inLibs, Dir_Destroy);
    872 }
    873 
    874 /*
    875  * Add the given suffix as a type of file which gets included.
    876  * Called when a '.INCLUDES: .h' line is parsed.
    877  * To have an effect, the suffix must already exist.
    878  * This affects the magic variable '.INCLUDES'.
    879  */
    880 void
    881 Suff_AddInclude(const char *suffName)
    882 {
    883     Suffix *suff = FindSuffixByName(suffName);
    884     if (suff != NULL)
    885 	suff->flags |= SUFF_INCLUDE;
    886 }
    887 
    888 /*
    889  * Add the given suffix as a type of file which is a library.
    890  * Called when a '.LIBS: .a' line is parsed.
    891  * To have an effect, the suffix must already exist.
    892  * This affects the magic variable '.LIBS'.
    893  */
    894 void
    895 Suff_AddLib(const char *suffName)
    896 {
    897     Suffix *suff = FindSuffixByName(suffName);
    898     if (suff != NULL)
    899 	suff->flags |= SUFF_LIBRARY;
    900 }
    901 
    902 	  /********** Implicit Source Search Functions *********/
    903 
    904 static void
    905 CandidateSearcher_Init(CandidateSearcher *cs)
    906 {
    907     cs->list = Lst_New();
    908 }
    909 
    910 static void
    911 CandidateSearcher_Done(CandidateSearcher *cs)
    912 {
    913     Lst_Free(cs->list);
    914 }
    915 
    916 static void
    917 CandidateSearcher_Add(CandidateSearcher *cs, Candidate *cand)
    918 {
    919     /* TODO: filter duplicates */
    920     Lst_Append(cs->list, cand);
    921 }
    922 
    923 static void
    924 CandidateSearcher_AddIfNew(CandidateSearcher *cs, Candidate *cand)
    925 {
    926     /* TODO: filter duplicates */
    927     if (Lst_FindDatum(cs->list, cand) == NULL)
    928 	Lst_Append(cs->list, cand);
    929 }
    930 
    931 static void
    932 CandidateSearcher_MoveAll(CandidateSearcher *cs, CandidateList *list)
    933 {
    934     /* TODO: filter duplicates */
    935     Lst_MoveAll(cs->list, list);
    936 }
    937 
    938 
    939 #ifdef DEBUG_SRC
    940 static void
    941 CandidateList_PrintAddrs(CandidateList *list)
    942 {
    943     CandidateListNode *ln;
    944 
    945     for (ln = list->first; ln != NULL; ln = ln->next) {
    946 	Candidate *cand = ln->datum;
    947 	debug_printf(" %p:%s", cand, cand->file);
    948     }
    949     debug_printf("\n");
    950 }
    951 #endif
    952 
    953 static Candidate *
    954 Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent,
    955 	      GNode *gn)
    956 {
    957     Candidate *cand = bmake_malloc(sizeof *cand);
    958 
    959     cand->file = name;
    960     cand->prefix = prefix;
    961     cand->suff = Suffix_Ref(suff);
    962     cand->parent = parent;
    963     cand->node = gn;
    964     cand->numChildren = 0;
    965 #ifdef DEBUG_SRC
    966     cand->childrenList = Lst_New();
    967 #endif
    968 
    969     return cand;
    970 }
    971 
    972 /* Add a new candidate to the list. */
    973 static void
    974 CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ,
    975 		  Suffix *suff, const char *debug_tag)
    976 {
    977     Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ, NULL);
    978     targ->numChildren++;
    979     Lst_Append(list, cand);
    980 
    981 #ifdef DEBUG_SRC
    982     Lst_Append(targ->childrenList, cand);
    983     debug_printf("%s add suff %p:%s candidate %p:%s to list %p:",
    984 		 debug_tag, targ, targ->file, cand, cand->file, list);
    985     CandidateList_PrintAddrs(list);
    986 #endif
    987 }
    988 
    989 /* Add all candidates to the list that can be formed by applying a suffix to
    990  * the candidate. */
    991 static void
    992 CandidateList_AddCandidatesFor(CandidateList *list, Candidate *cand)
    993 {
    994     SuffixListNode *ln;
    995     for (ln = cand->suff->children->first; ln != NULL; ln = ln->next) {
    996 	Suffix *suff = ln->datum;
    997 
    998 	if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
    999 	    /*
   1000 	     * If the suffix has been marked as the NULL suffix, also
   1001 	     * create a candidate for a file with no suffix attached.
   1002 	     */
   1003 	    CandidateList_Add(list, bmake_strdup(cand->prefix),
   1004 		       cand, suff, "1");
   1005 	}
   1006 
   1007 	CandidateList_Add(list, str_concat2(cand->prefix, suff->name),
   1008 			  cand, suff, "2");
   1009     }
   1010 }
   1011 
   1012 /* Free the first candidate in the list that is not referenced anymore.
   1013  * Return whether a candidate was removed. */
   1014 static Boolean
   1015 RemoveCandidate(CandidateList *srcs)
   1016 {
   1017     CandidateListNode *ln;
   1018 
   1019 #ifdef DEBUG_SRC
   1020     debug_printf("cleaning list %p:", srcs);
   1021     CandidateList_PrintAddrs(srcs);
   1022 #endif
   1023 
   1024     for (ln = srcs->first; ln != NULL; ln = ln->next) {
   1025 	Candidate *src = ln->datum;
   1026 
   1027 	if (src->numChildren == 0) {
   1028 	    free(src->file);
   1029 	    if (src->parent == NULL)
   1030 		free(src->prefix);
   1031 	    else {
   1032 #ifdef DEBUG_SRC
   1033 	        /* XXX: Lst_RemoveDatum */
   1034 		CandidateListNode *ln2;
   1035 		ln2 = Lst_FindDatum(src->parent->childrenList, src);
   1036 		if (ln2 != NULL)
   1037 		    Lst_Remove(src->parent->childrenList, ln2);
   1038 #endif
   1039 		src->parent->numChildren--;
   1040 	    }
   1041 #ifdef DEBUG_SRC
   1042 	    debug_printf("free: list %p src %p:%s children %d\n",
   1043 			 srcs, src, src->file, src->numChildren);
   1044 	    Lst_Free(src->childrenList);
   1045 #endif
   1046 	    Lst_Remove(srcs, ln);
   1047 	    free(src);
   1048 	    return TRUE;
   1049 	}
   1050 #ifdef DEBUG_SRC
   1051 	else {
   1052 	    debug_printf("keep: list %p src %p:%s children %d:",
   1053 			 srcs, src, src->file, src->numChildren);
   1054 	    CandidateList_PrintAddrs(src->childrenList);
   1055 	}
   1056 #endif
   1057     }
   1058 
   1059     return FALSE;
   1060 }
   1061 
   1062 /* Find the first existing file/target in srcs. */
   1063 static Candidate *
   1064 FindThem(CandidateList *srcs, CandidateSearcher *cs)
   1065 {
   1066     HashSet seen;
   1067 
   1068     HashSet_Init(&seen);
   1069 
   1070     while (!Lst_IsEmpty(srcs)) {
   1071 	Candidate *src = Lst_Dequeue(srcs);
   1072 
   1073 #ifdef DEBUG_SRC
   1074 	debug_printf("remove from list %p src %p:%s\n", srcs, src, src->file);
   1075 #endif
   1076 	SUFF_DEBUG1("\ttrying %s...", src->file);
   1077 
   1078 	/*
   1079 	 * A file is considered to exist if either a node exists in the
   1080 	 * graph for it or the file actually exists.
   1081 	 */
   1082 	if (Targ_FindNode(src->file) != NULL) {
   1083 	found:
   1084 	    HashSet_Done(&seen);
   1085 	    SUFF_DEBUG0("got it\n");
   1086 	    return src;
   1087 	}
   1088 
   1089 	{
   1090 	    char *file = Dir_FindFile(src->file, src->suff->searchPath);
   1091 	    if (file != NULL) {
   1092 		free(file);
   1093 		goto found;
   1094 	    }
   1095 	}
   1096 
   1097 	SUFF_DEBUG0("not there\n");
   1098 
   1099 	if (HashSet_Add(&seen, src->file))
   1100 	    CandidateList_AddCandidatesFor(srcs, src);
   1101 	else {
   1102 	    SUFF_DEBUG1("FindThem: skipping duplicate \"%s\"\n", src->file);
   1103 	}
   1104 
   1105 	CandidateSearcher_Add(cs, src);
   1106     }
   1107 
   1108     HashSet_Done(&seen);
   1109     return NULL;
   1110 }
   1111 
   1112 /*
   1113  * See if any of the children of the candidate's GNode is one from which the
   1114  * target can be transformed. If there is one, a candidate is put together
   1115  * for it and returned.
   1116  */
   1117 static Candidate *
   1118 FindCmds(Candidate *targ, CandidateSearcher *cs)
   1119 {
   1120     GNodeListNode *gln;
   1121     GNode *tgn;			/* Target GNode */
   1122     GNode *sgn;			/* Source GNode */
   1123     size_t prefLen;		/* The length of the defined prefix */
   1124     Suffix *suff;		/* Suffix on matching beastie */
   1125     Candidate *ret;		/* Return value */
   1126     char *cp;
   1127 
   1128     tgn = targ->node;
   1129     prefLen = strlen(targ->prefix);
   1130 
   1131     for (gln = tgn->children.first; gln != NULL; gln = gln->next) {
   1132 	sgn = gln->datum;
   1133 
   1134 	if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(&tgn->commands)) {
   1135 	    /*
   1136 	     * We haven't looked to see if .OPTIONAL files exist yet, so
   1137 	     * don't use one as the implicit source.
   1138 	     * This allows us to use .OPTIONAL in .depend files so make won't
   1139 	     * complain "don't know how to make xxx.h' when a dependent file
   1140 	     * has been moved/deleted.
   1141 	     */
   1142 	    continue;
   1143 	}
   1144 
   1145 	cp = strrchr(sgn->name, '/');
   1146 	if (cp == NULL) {
   1147 	    cp = sgn->name;
   1148 	} else {
   1149 	    cp++;
   1150 	}
   1151 	if (strncmp(cp, targ->prefix, prefLen) != 0)
   1152 	    continue;
   1153 	/* The node matches the prefix ok, see if it has a known suffix. */
   1154 	suff = FindSuffixByName(cp + prefLen);
   1155 	if (suff == NULL)
   1156 	    continue;
   1157 
   1158 	/*
   1159 	 * It even has a known suffix, see if there's a transformation
   1160 	 * defined between the node's suffix and the target's suffix.
   1161 	 *
   1162 	 * XXX: Handle multi-stage transformations here, too.
   1163 	 */
   1164 
   1165 	if (Lst_FindDatum(suff->parents, targ->suff) != NULL)
   1166 	    break;
   1167     }
   1168 
   1169     if (gln == NULL)
   1170 	return NULL;
   1171 
   1172     ret = Candidate_New(bmake_strdup(sgn->name), targ->prefix, suff, targ, sgn);
   1173     targ->numChildren++;
   1174 #ifdef DEBUG_SRC
   1175     debug_printf("3 add targ %p:%s ret %p:%s\n",
   1176 		 targ, targ->file, ret, ret->file);
   1177     Lst_Append(targ->childrenList, ret);
   1178 #endif
   1179     CandidateSearcher_Add(cs, ret);
   1180     SUFF_DEBUG1("\tusing existing source %s\n", sgn->name);
   1181     return ret;
   1182 }
   1183 
   1184 static void
   1185 ExpandWildcards(GNodeListNode *cln, GNode *pgn)
   1186 {
   1187     GNode *cgn = cln->datum;
   1188     StringList *expansions;
   1189 
   1190     if (!Dir_HasWildcards(cgn->name))
   1191 	return;
   1192 
   1193     /*
   1194      * Expand the word along the chosen path
   1195      */
   1196     expansions = Lst_New();
   1197     Dir_Expand(cgn->name, Suff_FindPath(cgn), expansions);
   1198 
   1199     while (!Lst_IsEmpty(expansions)) {
   1200 	GNode	*gn;
   1201 	/*
   1202 	 * Fetch next expansion off the list and find its GNode
   1203 	 */
   1204 	char *cp = Lst_Dequeue(expansions);
   1205 
   1206 	SUFF_DEBUG1("%s...", cp);
   1207 	gn = Targ_GetNode(cp);
   1208 
   1209 	/* Add gn to the parents child list before the original child */
   1210 	Lst_InsertBefore(&pgn->children, cln, gn);
   1211 	Lst_Append(&gn->parents, pgn);
   1212 	pgn->unmade++;
   1213     }
   1214 
   1215     Lst_Free(expansions);
   1216 
   1217     SUFF_DEBUG0("\n");
   1218 
   1219     /*
   1220      * Now the source is expanded, remove it from the list of children to
   1221      * keep it from being processed.
   1222      */
   1223     pgn->unmade--;
   1224     Lst_Remove(&pgn->children, cln);
   1225     Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
   1226 }
   1227 
   1228 /* Expand the names of any children of a given node that contain variable
   1229  * expressions or file wildcards into actual targets.
   1230  *
   1231  * The expanded node is removed from the parent's list of children, and the
   1232  * parent's unmade counter is decremented, but other nodes may be added.
   1233  *
   1234  * Input:
   1235  *	cln		Child to examine
   1236  *	pgn		Parent node being processed
   1237  */
   1238 static void
   1239 ExpandChildren(GNodeListNode *cln, GNode *pgn)
   1240 {
   1241     GNode *cgn = cln->datum;
   1242     GNode *gn;			/* New source 8) */
   1243     char *cp;			/* Expanded value */
   1244 
   1245     if (!Lst_IsEmpty(&cgn->order_pred) || !Lst_IsEmpty(&cgn->order_succ))
   1246 	/* It is all too hard to process the result of .ORDER */
   1247 	return;
   1248 
   1249     if (cgn->type & OP_WAIT)
   1250 	/* Ignore these (& OP_PHONY ?) */
   1251 	return;
   1252 
   1253     /*
   1254      * First do variable expansion -- this takes precedence over
   1255      * wildcard expansion. If the result contains wildcards, they'll be gotten
   1256      * to later since the resulting words are tacked on to the end of
   1257      * the children list.
   1258      */
   1259     if (strchr(cgn->name, '$') == NULL) {
   1260 	ExpandWildcards(cln, pgn);
   1261 	return;
   1262     }
   1263 
   1264     SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
   1265     (void)Var_Subst(cgn->name, pgn, VARE_WANTRES | VARE_UNDEFERR, &cp);
   1266     /* TODO: handle errors */
   1267 
   1268     {
   1269 	GNodeList *members = Lst_New();
   1270 
   1271 	if (cgn->type & OP_ARCHV) {
   1272 	    /*
   1273 	     * Node was an archive(member) target, so we want to call
   1274 	     * on the Arch module to find the nodes for us, expanding
   1275 	     * variables in the parent's context.
   1276 	     */
   1277 	    char *sacrifice = cp;
   1278 
   1279 	    (void)Arch_ParseArchive(&sacrifice, members, pgn);
   1280 	} else {
   1281 	    /*
   1282 	     * Break the result into a vector of strings whose nodes
   1283 	     * we can find, then add those nodes to the members list.
   1284 	     * Unfortunately, we can't use Str_Words because it
   1285 	     * doesn't understand about variable specifications with
   1286 	     * spaces in them...
   1287 	     */
   1288 	    char *start;
   1289 	    char *initcp = cp;	/* For freeing... */
   1290 
   1291 	    start = cp;
   1292 	    pp_skip_hspace(&start);
   1293 	    cp = start;
   1294 	    while (*cp != '\0') {
   1295 		if (*cp == ' ' || *cp == '\t') {
   1296 		    /*
   1297 		     * White-space -- terminate element, find the node,
   1298 		     * add it, skip any further spaces.
   1299 		     */
   1300 		    *cp++ = '\0';
   1301 		    gn = Targ_GetNode(start);
   1302 		    Lst_Append(members, gn);
   1303 		    pp_skip_hspace(&cp);
   1304 		    start = cp;		/* Continue at the next non-space. */
   1305 		} else if (*cp == '$') {
   1306 		    /* Skip over the variable expression. */
   1307 		    const char *nested_p = cp;
   1308 		    const char	*junk;
   1309 		    void	*freeIt;
   1310 
   1311 		    (void)Var_Parse(&nested_p, pgn, VARE_NONE, &junk, &freeIt);
   1312 		    /* TODO: handle errors */
   1313 		    if (junk == var_Error) {
   1314 			Parse_Error(PARSE_FATAL,
   1315 				    "Malformed variable expression at \"%s\"",
   1316 				    cp);
   1317 			cp++;
   1318 		    } else {
   1319 			cp += nested_p - cp;
   1320 		    }
   1321 
   1322 		    free(freeIt);
   1323 		} else if (cp[0] == '\\' && cp[1] != '\0') {
   1324 		    /*
   1325 		     * Escaped something -- skip over it
   1326 		     */
   1327 		    /* XXX: In other places, escaping at this syntactical
   1328 		     * position is done by a '$', not a '\'.  The '\' is only
   1329 		     * used in variable modifiers. */
   1330 		    cp += 2;
   1331 		} else {
   1332 		    cp++;
   1333 		}
   1334 	    }
   1335 
   1336 	    if (cp != start) {
   1337 		/*
   1338 		 * Stuff left over -- add it to the list too
   1339 		 */
   1340 		gn = Targ_GetNode(start);
   1341 		Lst_Append(members, gn);
   1342 	    }
   1343 	    /*
   1344 	     * Point cp back at the beginning again so the variable value
   1345 	     * can be freed.
   1346 	     */
   1347 	    cp = initcp;
   1348 	}
   1349 
   1350 	/*
   1351 	 * Add all elements of the members list to the parent node.
   1352 	 */
   1353 	while(!Lst_IsEmpty(members)) {
   1354 	    gn = Lst_Dequeue(members);
   1355 
   1356 	    SUFF_DEBUG1("%s...", gn->name);
   1357 	    /* Add gn to the parents child list before the original child */
   1358 	    Lst_InsertBefore(&pgn->children, cln, gn);
   1359 	    Lst_Append(&gn->parents, pgn);
   1360 	    pgn->unmade++;
   1361 	    /* Expand wildcards on new node */
   1362 	    ExpandWildcards(cln->prev, pgn);
   1363 	}
   1364 	Lst_Free(members);
   1365 
   1366 	/*
   1367 	 * Free the result
   1368 	 */
   1369 	free(cp);
   1370     }
   1371 
   1372     SUFF_DEBUG0("\n");
   1373 
   1374     /*
   1375      * Now the source is expanded, remove it from the list of children to
   1376      * keep it from being processed.
   1377      */
   1378     pgn->unmade--;
   1379     Lst_Remove(&pgn->children, cln);
   1380     Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn));
   1381 }
   1382 
   1383 static void
   1384 ExpandAllChildren(GNode *gn)
   1385 {
   1386     GNodeListNode *ln, *nln;
   1387 
   1388     for (ln = gn->children.first; ln != NULL; ln = nln) {
   1389 	nln = ln->next;
   1390 	ExpandChildren(ln, gn);
   1391     }
   1392 }
   1393 
   1394 /* Find a path along which to expand the node.
   1395  *
   1396  * If the node has a known suffix, use that path.
   1397  * If it has no known suffix, use the default system search path.
   1398  *
   1399  * Input:
   1400  *	gn		Node being examined
   1401  *
   1402  * Results:
   1403  *	The appropriate path to search for the GNode.
   1404  */
   1405 SearchPath *
   1406 Suff_FindPath(GNode* gn)
   1407 {
   1408     Suffix *suff = gn->suffix;
   1409 
   1410     if (suff == NULL) {
   1411 	char *name = gn->name;
   1412 	size_t nameLen = strlen(gn->name);
   1413 	SuffixListNode *ln;
   1414 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   1415 	    if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen))
   1416 		break;
   1417 
   1418 	SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
   1419 	if (ln != NULL)
   1420 	    suff = ln->datum;
   1421 	/* XXX: Here we can save the suffix so we don't have to do this again */
   1422     }
   1423 
   1424     if (suff != NULL) {
   1425 	SUFF_DEBUG1("suffix is \"%s\"...\n", suff->name);
   1426 	return suff->searchPath;
   1427     } else {
   1428 	SUFF_DEBUG0("\n");
   1429 	return dirSearchPath;	/* Use default search path */
   1430     }
   1431 }
   1432 
   1433 /* Apply a transformation rule, given the source and target nodes and
   1434  * suffixes.
   1435  *
   1436  * The source and target are linked and the commands from the transformation
   1437  * are added to the target node's commands list. The target also inherits all
   1438  * the sources for the transformation rule.
   1439  *
   1440  * Results:
   1441  *	TRUE if successful, FALSE if not.
   1442  */
   1443 static Boolean
   1444 ApplyTransform(GNode *tgn, GNode *sgn, Suffix *tsuff, Suffix *ssuff)
   1445 {
   1446     GNodeListNode *ln;
   1447     char *tname;		/* Name of transformation rule */
   1448     GNode *gn;			/* Node for same */
   1449 
   1450     /*
   1451      * Form the proper links between the target and source.
   1452      */
   1453     Lst_Append(&tgn->children, sgn);
   1454     Lst_Append(&sgn->parents, tgn);
   1455     tgn->unmade++;
   1456 
   1457     /*
   1458      * Locate the transformation rule itself
   1459      */
   1460     tname = str_concat2(ssuff->name, tsuff->name);
   1461     gn = FindTransformByName(tname);
   1462     free(tname);
   1463 
   1464     if (gn == NULL) {
   1465 	/* This can happen when linking an OP_MEMBER and OP_ARCHV node. */
   1466 	return FALSE;
   1467     }
   1468 
   1469     DEBUG3(SUFF,"\tapplying %s -> %s to \"%s\"\n",
   1470 		ssuff->name, tsuff->name, tgn->name);
   1471 
   1472     /* Record last child; Make_HandleUse may add child nodes. */
   1473     ln = tgn->children.last;
   1474 
   1475     /* Apply the rule. */
   1476     Make_HandleUse(gn, tgn);
   1477 
   1478     /* Deal with wildcards and variables in any acquired sources. */
   1479     ln = ln != NULL ? ln->next : NULL;
   1480     while (ln != NULL) {
   1481 	GNodeListNode *nln = ln->next;
   1482 	ExpandChildren(ln, tgn);
   1483 	ln = nln;
   1484     }
   1485 
   1486     /*
   1487      * Keep track of another parent to which this node is transformed so
   1488      * the .IMPSRC variable can be set correctly for the parent.
   1489      */
   1490     Lst_Append(&sgn->implicitParents, tgn);
   1491 
   1492     return TRUE;
   1493 }
   1494 
   1495 /*
   1496  * Member has a known suffix, so look for a transformation rule from
   1497  * it to a possible suffix of the archive.
   1498  *
   1499  * Rather than searching through the entire list, we just look at
   1500  * suffixes to which the member's suffix may be transformed.
   1501  */
   1502 static void
   1503 ExpandMember(GNode *gn, const char *eoarch, GNode *mem, Suffix *memSuff)
   1504 {
   1505     GNodeListNode *ln;
   1506     size_t nameLen = (size_t)(eoarch - gn->name);
   1507 
   1508     /* Use first matching suffix... */
   1509     for (ln = memSuff->parents->first; ln != NULL; ln = ln->next)
   1510 	if (Suffix_IsSuffix(ln->datum, nameLen, eoarch))
   1511 	    break;
   1512 
   1513     if (ln != NULL) {
   1514 	/* Got one -- apply it */
   1515 	Suffix *suff = ln->datum;
   1516 	if (!ApplyTransform(gn, mem, suff, memSuff)) {
   1517 	    SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
   1518 			memSuff->name, suff->name);
   1519 	}
   1520     }
   1521 }
   1522 
   1523 static void FindDeps(GNode *, CandidateSearcher *);
   1524 
   1525 /* Locate dependencies for an OP_ARCHV node.
   1526  *
   1527  * Input:
   1528  *	gn		Node for which to locate dependencies
   1529  *
   1530  * Side Effects:
   1531  *	Same as Suff_FindDeps
   1532  */
   1533 static void
   1534 FindDepsArchive(GNode *gn, CandidateSearcher *cs)
   1535 {
   1536     char *eoarch;		/* End of archive portion */
   1537     char *eoname;		/* End of member portion */
   1538     GNode *mem;			/* Node for member */
   1539     Suffix *memSuff;
   1540     const char *name;		/* Start of member's name */
   1541 
   1542     /*
   1543      * The node is an archive(member) pair. so we must find a
   1544      * suffix for both of them.
   1545      */
   1546     eoarch = strchr(gn->name, '(');
   1547     eoname = strchr(eoarch, ')');
   1548 
   1549     /*
   1550      * Caller guarantees the format `libname(member)', via
   1551      * Arch_ParseArchive.
   1552      */
   1553     assert(eoarch != NULL);
   1554     assert(eoname != NULL);
   1555 
   1556     *eoname = '\0';	  /* Nuke parentheses during suffix search */
   1557     *eoarch = '\0';	  /* So a suffix can be found */
   1558 
   1559     name = eoarch + 1;
   1560 
   1561     /*
   1562      * To simplify things, call Suff_FindDeps recursively on the member now,
   1563      * so we can simply compare the member's .PREFIX and .TARGET variables
   1564      * to locate its suffix. This allows us to figure out the suffix to
   1565      * use for the archive without having to do a quadratic search over the
   1566      * suffix list, backtracking for each one...
   1567      */
   1568     mem = Targ_GetNode(name);
   1569     FindDeps(mem, cs);
   1570 
   1571     /*
   1572      * Create the link between the two nodes right off
   1573      */
   1574     Lst_Append(&gn->children, mem);
   1575     Lst_Append(&mem->parents, gn);
   1576     gn->unmade++;
   1577 
   1578     /*
   1579      * Copy in the variables from the member node to this one.
   1580      */
   1581     Var_Set(PREFIX, GNode_VarPrefix(mem), gn);
   1582     Var_Set(TARGET, GNode_VarTarget(mem), gn);
   1583 
   1584     memSuff = mem->suffix;
   1585     if (memSuff == NULL) {	/* Didn't know what it was. */
   1586 	SUFF_DEBUG0("using null suffix\n");
   1587 	memSuff = nullSuff;
   1588     }
   1589 
   1590 
   1591     /*
   1592      * Set the other two local variables required for this target.
   1593      */
   1594     Var_Set(MEMBER, name, gn);
   1595     Var_Set(ARCHIVE, gn->name, gn);
   1596 
   1597     /*
   1598      * Set $@ for compatibility with other makes
   1599      */
   1600     Var_Set(TARGET, gn->name, gn);
   1601 
   1602     /*
   1603      * Now we've got the important local variables set, expand any sources
   1604      * that still contain variables or wildcards in their names.
   1605      */
   1606     ExpandAllChildren(gn);
   1607 
   1608     if (memSuff != NULL)
   1609 	ExpandMember(gn, eoarch, mem, memSuff);
   1610 
   1611     /*
   1612      * Replace the opening and closing parens now we've no need of the separate
   1613      * pieces.
   1614      */
   1615     *eoarch = '(';
   1616     *eoname = ')';
   1617 
   1618     /*
   1619      * Pretend gn appeared to the left of a dependency operator so
   1620      * the user needn't provide a transformation from the member to the
   1621      * archive.
   1622      */
   1623     if (!GNode_IsTarget(gn))
   1624 	gn->type |= OP_DEPENDS;
   1625 
   1626     /*
   1627      * Flag the member as such so we remember to look in the archive for
   1628      * its modification time. The OP_JOIN | OP_MADE is needed because this
   1629      * target should never get made.
   1630      */
   1631     mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
   1632 }
   1633 
   1634 /*
   1635  * If the node is a library, it is the arch module's job to find it
   1636  * and set the TARGET variable accordingly. We merely provide the
   1637  * search path, assuming all libraries end in ".a" (if the suffix
   1638  * hasn't been defined, there's nothing we can do for it, so we just
   1639  * set the TARGET variable to the node's name in order to give it a
   1640  * value).
   1641  */
   1642 static void
   1643 FindDepsLib(GNode *gn)
   1644 {
   1645     Suffix *suff = FindSuffixByName(LIBSUFF);
   1646     if (suff != NULL) {
   1647 	Suffix_Reassign(&gn->suffix, suff);
   1648 	Arch_FindLib(gn, suff->searchPath);
   1649     } else {
   1650 	Suffix_Unassign(&gn->suffix);
   1651 	Var_Set(TARGET, gn->name, gn);
   1652     }
   1653 
   1654     /*
   1655      * Because a library (-lfoo) target doesn't follow the standard
   1656      * filesystem conventions, we don't set the regular variables for
   1657      * the thing. .PREFIX is simply made empty.
   1658      */
   1659     Var_Set(PREFIX, "", gn);
   1660 }
   1661 
   1662 static void
   1663 FindDepsRegularKnown(const char *name, size_t nameLen, GNode *gn,
   1664 		     CandidateList *srcs, CandidateList *targs)
   1665 {
   1666     SuffixListNode *ln;
   1667     Candidate *targ;
   1668     char *pref;
   1669 
   1670     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
   1671 	Suffix *suff = ln->datum;
   1672 	if (!Suffix_IsSuffix(suff, nameLen, name + nameLen))
   1673 	    continue;
   1674 
   1675 	pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen));
   1676 	targ = Candidate_New(bmake_strdup(gn->name), pref, suff, NULL, gn);
   1677 
   1678 	CandidateList_AddCandidatesFor(srcs, targ);
   1679 
   1680 	/*
   1681 	 * Record the target so we can nuke it
   1682 	 */
   1683 	Lst_Append(targs, targ);
   1684     }
   1685 }
   1686 
   1687 static void
   1688 FindDepsRegularUnknown(GNode *gn, const char *sopref,
   1689 		       CandidateList *srcs, CandidateList *targs)
   1690 {
   1691     Candidate *targ;
   1692 
   1693     if (!Lst_IsEmpty(targs) || nullSuff == NULL)
   1694 	return;
   1695 
   1696     SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
   1697 
   1698     targ = Candidate_New(bmake_strdup(gn->name), bmake_strdup(sopref),
   1699 		  nullSuff, NULL, gn);
   1700 
   1701     /*
   1702      * Only use the default suffix rules if we don't have commands
   1703      * defined for this gnode; traditional make programs used to
   1704      * not define suffix rules if the gnode had children but we
   1705      * don't do this anymore.
   1706      */
   1707     if (Lst_IsEmpty(&gn->commands))
   1708 	CandidateList_AddCandidatesFor(srcs, targ);
   1709     else {
   1710 	SUFF_DEBUG0("not ");
   1711     }
   1712 
   1713     SUFF_DEBUG0("adding suffix rules\n");
   1714 
   1715     Lst_Append(targs, targ);
   1716 }
   1717 
   1718 /*
   1719  * Deal with finding the thing on the default search path. We
   1720  * always do that, not only if the node is only a source (not
   1721  * on the lhs of a dependency operator or [XXX] it has neither
   1722  * children or commands) as the old pmake did.
   1723  */
   1724 static void
   1725 FindDepsRegularPath(GNode *gn, Candidate *targ)
   1726 {
   1727     if (gn->type & (OP_PHONY | OP_NOPATH))
   1728 	return;
   1729 
   1730     free(gn->path);
   1731     gn->path = Dir_FindFile(gn->name,
   1732 			    (targ == NULL ? dirSearchPath :
   1733 			     targ->suff->searchPath));
   1734     if (gn->path == NULL)
   1735 	return;
   1736 
   1737     Var_Set(TARGET, gn->path, gn);
   1738 
   1739     if (targ != NULL) {
   1740 	/*
   1741 	 * Suffix known for the thing -- trim the suffix off
   1742 	 * the path to form the proper .PREFIX variable.
   1743 	 */
   1744 	size_t savep = strlen(gn->path) - targ->suff->nameLen;
   1745 	char savec;
   1746 	char *ptr;
   1747 
   1748 	Suffix_Reassign(&gn->suffix, targ->suff);
   1749 
   1750 	savec = gn->path[savep];
   1751 	gn->path[savep] = '\0';
   1752 
   1753 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1754 	    ptr++;
   1755 	else
   1756 	    ptr = gn->path;
   1757 
   1758 	Var_Set(PREFIX, ptr, gn);
   1759 
   1760 	gn->path[savep] = savec;
   1761     } else {
   1762 	char *ptr;
   1763 
   1764 	/* The .PREFIX gets the full path if the target has no known suffix. */
   1765 	Suffix_Unassign(&gn->suffix);
   1766 
   1767 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1768 	    ptr++;
   1769 	else
   1770 	    ptr = gn->path;
   1771 
   1772 	Var_Set(PREFIX, ptr, gn);
   1773     }
   1774 }
   1775 
   1776 /* Locate implicit dependencies for regular targets.
   1777  *
   1778  * Input:
   1779  *	gn		Node for which to find sources
   1780  *
   1781  * Side Effects:
   1782  *	Same as Suff_FindDeps
   1783  */
   1784 static void
   1785 FindDepsRegular(GNode *gn, CandidateSearcher *cs)
   1786 {
   1787     CandidateList *srcs;	/* List of sources at which to look */
   1788     CandidateList *targs;	/* List of targets to which things can be
   1789 				 * transformed. They all have the same file,
   1790 				 * but different suff and prefix fields */
   1791     Candidate *bottom;		/* Start of found transformation path */
   1792     Candidate *src;
   1793     Candidate *targ;
   1794 
   1795     const char *name = gn->name;
   1796     size_t nameLen = strlen(name);
   1797 
   1798 #ifdef DEBUG_SRC
   1799     DEBUG1(SUFF, "FindDepsRegular \"%s\"\n", gn->name);
   1800 #endif
   1801 
   1802     /*
   1803      * Begin at the beginning...
   1804      */
   1805     srcs = Lst_New();
   1806     targs = Lst_New();
   1807 
   1808     /*
   1809      * We're caught in a catch-22 here. On the one hand, we want to use any
   1810      * transformation implied by the target's sources, but we can't examine
   1811      * the sources until we've expanded any variables/wildcards they may hold,
   1812      * and we can't do that until we've set up the target's local variables
   1813      * and we can't do that until we know what the proper suffix for the
   1814      * target is (in case there are two suffixes one of which is a suffix of
   1815      * the other) and we can't know that until we've found its implied
   1816      * source, which we may not want to use if there's an existing source
   1817      * that implies a different transformation.
   1818      *
   1819      * In an attempt to get around this, which may not work all the time,
   1820      * but should work most of the time, we look for implied sources first,
   1821      * checking transformations to all possible suffixes of the target,
   1822      * use what we find to set the target's local variables, expand the
   1823      * children, then look for any overriding transformations they imply.
   1824      * Should we find one, we discard the one we found before.
   1825      */
   1826     bottom = NULL;
   1827     targ = NULL;
   1828 
   1829     if (!(gn->type & OP_PHONY)) {
   1830 
   1831 	FindDepsRegularKnown(name, nameLen, gn, srcs, targs);
   1832 
   1833 	/* Handle target of unknown suffix... */
   1834 	FindDepsRegularUnknown(gn, name, srcs, targs);
   1835 
   1836 	/*
   1837 	 * Using the list of possible sources built up from the target
   1838 	 * suffix(es), try and find an existing file/target that matches.
   1839 	 */
   1840 	bottom = FindThem(srcs, cs);
   1841 
   1842 	if (bottom == NULL) {
   1843 	    /*
   1844 	     * No known transformations -- use the first suffix found
   1845 	     * for setting the local variables.
   1846 	     */
   1847 	    if (targs->first != NULL)
   1848 		targ = targs->first->datum;
   1849 	    else
   1850 		targ = NULL;
   1851 	} else {
   1852 	    /*
   1853 	     * Work up the transformation path to find the suffix of the
   1854 	     * target to which the transformation was made.
   1855 	     */
   1856 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   1857 		continue;
   1858 	}
   1859     }
   1860 
   1861     Var_Set(TARGET, GNode_Path(gn), gn);
   1862     Var_Set(PREFIX, targ != NULL ? targ->prefix : gn->name, gn);
   1863 
   1864     /*
   1865      * Now we've got the important local variables set, expand any sources
   1866      * that still contain variables or wildcards in their names.
   1867      */
   1868     {
   1869 	GNodeListNode *ln, *nln;
   1870 	for (ln = gn->children.first; ln != NULL; ln = nln) {
   1871 	    nln = ln->next;
   1872 	    ExpandChildren(ln, gn);
   1873 	}
   1874     }
   1875 
   1876     if (targ == NULL) {
   1877 	SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
   1878 
   1879 sfnd_abort:
   1880 	FindDepsRegularPath(gn, targ);
   1881 	goto sfnd_return;
   1882     }
   1883 
   1884     /*
   1885      * If the suffix indicates that the target is a library, mark that in
   1886      * the node's type field.
   1887      */
   1888     if (targ->suff->flags & SUFF_LIBRARY)
   1889 	gn->type |= OP_LIB;
   1890 
   1891     /*
   1892      * Check for overriding transformation rule implied by sources
   1893      */
   1894     if (!Lst_IsEmpty(&gn->children)) {
   1895 	src = FindCmds(targ, cs);
   1896 
   1897 	if (src != NULL) {
   1898 	    /*
   1899 	     * Free up all the candidates in the transformation path,
   1900 	     * up to but not including the parent node.
   1901 	     */
   1902 	    while (bottom != NULL && bottom->parent != NULL) {
   1903 		CandidateSearcher_AddIfNew(cs, bottom);
   1904 		bottom = bottom->parent;
   1905 	    }
   1906 	    bottom = src;
   1907 	}
   1908     }
   1909 
   1910     if (bottom == NULL) {
   1911 	/*
   1912 	 * No idea from where it can come -- return now.
   1913 	 */
   1914 	goto sfnd_abort;
   1915     }
   1916 
   1917     /*
   1918      * We now have a list of candidates headed by 'bottom' and linked via
   1919      * their 'parent' pointers. What we do next is create links between
   1920      * source and target nodes (which may or may not have been created)
   1921      * and set the necessary local variables in each target.
   1922      *
   1923      * The commands for each target are set from the commands of the
   1924      * transformation rule used to get from the src suffix to the targ
   1925      * suffix. Note that this causes the commands list of the original
   1926      * node, gn, to be replaced with the commands of the final
   1927      * transformation rule.
   1928      */
   1929     if (bottom->node == NULL)
   1930 	bottom->node = Targ_GetNode(bottom->file);
   1931 
   1932     for (src = bottom; src->parent != NULL; src = src->parent) {
   1933 	targ = src->parent;
   1934 
   1935 	Suffix_Reassign(&src->node->suffix, src->suff);
   1936 
   1937 	if (targ->node == NULL)
   1938 	    targ->node = Targ_GetNode(targ->file);
   1939 
   1940 	ApplyTransform(targ->node, src->node,
   1941 			   targ->suff, src->suff);
   1942 
   1943 	if (targ->node != gn) {
   1944 	    /*
   1945 	     * Finish off the dependency-search process for any nodes
   1946 	     * between bottom and gn (no point in questing around the
   1947 	     * filesystem for their implicit source when it's already
   1948 	     * known). Note that the node can't have any sources that
   1949 	     * need expanding, since SuffFindThem will stop on an existing
   1950 	     * node, so all we need to do is set the standard variables.
   1951 	     */
   1952 	    targ->node->type |= OP_DEPS_FOUND;
   1953 	    Var_Set(PREFIX, targ->prefix, targ->node);
   1954 	    Var_Set(TARGET, targ->node->name, targ->node);
   1955 	}
   1956     }
   1957 
   1958     Suffix_Reassign(&gn->suffix, src->suff);
   1959 
   1960     /*
   1961      * Nuke the transformation path and the candidates left over in the
   1962      * two lists.
   1963      */
   1964 sfnd_return:
   1965     if (bottom != NULL)
   1966 	CandidateSearcher_AddIfNew(cs, bottom);
   1967 
   1968     while (RemoveCandidate(srcs) || RemoveCandidate(targs))
   1969 	continue;
   1970 
   1971     CandidateSearcher_MoveAll(cs, srcs);
   1972     CandidateSearcher_MoveAll(cs, targs);
   1973 }
   1974 
   1975 static void
   1976 CandidateSearcher_CleanUp(CandidateSearcher *cs)
   1977 {
   1978     while (RemoveCandidate(cs->list))
   1979 	continue;
   1980     assert(Lst_IsEmpty(cs->list));
   1981 }
   1982 
   1983 
   1984 /* Find implicit sources for the target.
   1985  *
   1986  * Nodes are added to the graph as children of the passed-in node. The nodes
   1987  * are marked to have their IMPSRC variable filled in. The PREFIX variable
   1988  * is set for the given node and all its implied children.
   1989  *
   1990  * The path found by this target is the shortest path in the transformation
   1991  * graph, which may pass through non-existent targets, to an existing target.
   1992  * The search continues on all paths from the root suffix until a file is
   1993  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
   1994  * .l,v file exists but the .c and .l files don't, the search will branch out
   1995  * in all directions from .o and again from all the nodes on the next level
   1996  * until the .l,v node is encountered.
   1997  */
   1998 void
   1999 Suff_FindDeps(GNode *gn)
   2000 {
   2001     CandidateSearcher cs;
   2002 
   2003     CandidateSearcher_Init(&cs);
   2004 
   2005     FindDeps(gn, &cs);
   2006 
   2007     CandidateSearcher_CleanUp(&cs);
   2008     CandidateSearcher_Done(&cs);
   2009 }
   2010 
   2011 static void
   2012 FindDeps(GNode *gn, CandidateSearcher *cs)
   2013 {
   2014     if (gn->type & OP_DEPS_FOUND)
   2015 	return;
   2016     gn->type |= OP_DEPS_FOUND;
   2017 
   2018     /*
   2019      * Make sure we have these set, may get revised below.
   2020      */
   2021     Var_Set(TARGET, GNode_Path(gn), gn);
   2022     Var_Set(PREFIX, gn->name, gn);
   2023 
   2024     SUFF_DEBUG1("SuffFindDeps \"%s\"\n", gn->name);
   2025 
   2026     if (gn->type & OP_ARCHV)
   2027 	FindDepsArchive(gn, cs);
   2028     else if (gn->type & OP_LIB)
   2029 	FindDepsLib(gn);
   2030     else
   2031 	FindDepsRegular(gn, cs);
   2032 }
   2033 
   2034 /* Define which suffix is the null suffix.
   2035  *
   2036  * Need to handle the changing of the null suffix gracefully so the old
   2037  * transformation rules don't just go away.
   2038  *
   2039  * Input:
   2040  *	name		Name of null suffix
   2041  */
   2042 void
   2043 Suff_SetNull(const char *name)
   2044 {
   2045     Suffix *suff = FindSuffixByName(name);
   2046     if (suff == NULL) {
   2047 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
   2048 		    name);
   2049 	return;
   2050     }
   2051 
   2052     if (nullSuff != NULL)
   2053 	nullSuff->flags &= ~(unsigned)SUFF_NULL;
   2054     suff->flags |= SUFF_NULL;
   2055     /*
   2056      * XXX: Here's where the transformation mangling would take place
   2057      */
   2058     nullSuff = suff;
   2059 }
   2060 
   2061 /* Initialize the suffixes module. */
   2062 void
   2063 Suff_Init(void)
   2064 {
   2065 #ifdef CLEANUP
   2066     suffClean = Lst_New();
   2067     sufflist = Lst_New();
   2068 #endif
   2069     transforms = Lst_New();
   2070 
   2071     /*
   2072      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   2073      * actually go on the suffix list or everyone will think that's its
   2074      * suffix.
   2075      */
   2076     Suff_ClearSuffixes();
   2077 }
   2078 
   2079 
   2080 /* Clean up the suffixes module. */
   2081 void
   2082 Suff_End(void)
   2083 {
   2084 #ifdef CLEANUP
   2085     Lst_Destroy(sufflist, SuffFree);
   2086     Lst_Destroy(suffClean, SuffFree);
   2087     if (nullSuff != NULL)
   2088 	SuffFree(nullSuff);
   2089     Lst_Free(transforms);
   2090 #endif
   2091 }
   2092 
   2093 
   2094 static void
   2095 PrintSuffNames(const char *prefix, SuffixList *suffs)
   2096 {
   2097     SuffixListNode *ln;
   2098 
   2099     debug_printf("#\t%s: ", prefix);
   2100     for (ln = suffs->first; ln != NULL; ln = ln->next) {
   2101 	Suffix *suff = ln->datum;
   2102 	debug_printf("%s ", suff->name);
   2103     }
   2104     debug_printf("\n");
   2105 }
   2106 
   2107 static void
   2108 Suffix_Print(Suffix *suff)
   2109 {
   2110     debug_printf("# \"%s\" (num %d, ref %d)",
   2111 		 suff->name, suff->sNum, suff->refCount);
   2112     if (suff->flags != 0) {
   2113 	char flags_buf[SuffixFlags_ToStringSize];
   2114 
   2115 	debug_printf(" (%s)",
   2116 		     Enum_FlagsToString(flags_buf, sizeof flags_buf,
   2117 					suff->flags,
   2118 					SuffixFlags_ToStringSpecs));
   2119     }
   2120     debug_printf("\n");
   2121 
   2122     PrintSuffNames("To", suff->parents);
   2123     PrintSuffNames("From", suff->children);
   2124 
   2125     debug_printf("#\tSearch Path: ");
   2126     SearchPath_Print(suff->searchPath);
   2127     debug_printf("\n");
   2128 }
   2129 
   2130 static void
   2131 PrintTransformation(GNode *t)
   2132 {
   2133     debug_printf("%-16s:", t->name);
   2134     Targ_PrintType(t->type);
   2135     debug_printf("\n");
   2136     Targ_PrintCmds(t);
   2137     debug_printf("\n");
   2138 }
   2139 
   2140 void
   2141 Suff_PrintAll(void)
   2142 {
   2143     debug_printf("#*** Suffixes:\n");
   2144     {
   2145 	SuffixListNode *ln;
   2146 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   2147 	    Suffix_Print(ln->datum);
   2148     }
   2149 
   2150     debug_printf("#*** Transformations:\n");
   2151     {
   2152 	GNodeListNode *ln;
   2153 	for (ln = transforms->first; ln != NULL; ln = ln->next)
   2154 	    PrintTransformation(ln->datum);
   2155     }
   2156 }
   2157