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