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