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