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