Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.240
      1 /*	$NetBSD: suff.c,v 1.240 2020/11/16 18:41:41 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.240 2020/11/16 18:41:41 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 #define SUFF_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(SUFF, fmt, arg1, arg2, arg3)
    123 #define SUFF_DEBUG4(fmt, arg1, arg2, arg3, arg4) \
    124 	DEBUG4(SUFF, fmt, arg1, arg2, arg3, arg4)
    125 
    126 typedef List SuffList;
    127 typedef ListNode SuffListNode;
    128 
    129 typedef List SrcList;
    130 typedef ListNode SrcListNode;
    131 
    132 static SuffList *sufflist;	/* List of suffixes */
    133 #ifdef CLEANUP
    134 static SuffList *suffClean;	/* List of suffixes to be cleaned */
    135 #endif
    136 static SrcList *srclist;	/* List of sources */
    137 
    138 /* List of transformation rules, such as ".c.o" */
    139 static GNodeList *transforms;
    140 
    141 static int sNum = 0;		/* Counter for assigning suffix numbers */
    142 
    143 typedef enum SuffFlags {
    144     SUFF_INCLUDE	= 0x01,	/* One which is #include'd */
    145     SUFF_LIBRARY	= 0x02,	/* One which contains a library */
    146     SUFF_NULL		= 0x04	/* The empty suffix */
    147     /* XXX: Why is SUFF_NULL needed? Wouldn't nameLen == 0 mean the same? */
    148 } SuffFlags;
    149 
    150 ENUM_FLAGS_RTTI_3(SuffFlags,
    151 		  SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
    152 
    153 typedef List SuffListList;
    154 
    155 typedef struct Suff {
    156     /* The suffix itself, such as ".c" */
    157     char *name;
    158     /* Length of the name, to avoid strlen calls */
    159     size_t nameLen;
    160     /* Type of suffix */
    161     SuffFlags flags;
    162     /* The path along which files of this suffix may be found */
    163     SearchPath *searchPath;
    164     /* The suffix number; TODO: document the purpose of this number */
    165     int sNum;
    166     /* Reference count of list membership and several other places */
    167     int refCount;
    168     /* Suffixes we have a transformation to */
    169     SuffList *parents;
    170     /* Suffixes we have a transformation from */
    171     SuffList *children;
    172 
    173     /* Lists in which this suffix is referenced.
    174      * XXX: These lists are used nowhere, they are just appended to, for no
    175      * apparent reason.  They do have the side effect of increasing refCount
    176      * though. */
    177     SuffListList *ref;
    178 } Suff;
    179 
    180 /*
    181  * Structure used in the search for implied sources.
    182  */
    183 typedef struct Src {
    184     char *file;			/* The file to look for */
    185     char *pref;			/* Prefix from which file was formed */
    186     Suff *suff;			/* The suffix on the file */
    187     struct Src *parent;		/* The Src for which this is a source */
    188     GNode *node;		/* The node describing the file */
    189     int numChildren;		/* Count of existing children (so we don't free
    190 				 * this thing too early or never nuke it) */
    191 #ifdef DEBUG_SRC
    192     SrcList *childrenList;
    193 #endif
    194 } Src;
    195 
    196 /* TODO: Document the difference between suffNull and emptySuff. */
    197 /* The NULL suffix for this run */
    198 static Suff *suffNull;
    199 /* The empty suffix required for POSIX single-suffix transformation rules */
    200 static Suff *emptySuff;
    201 
    202 
    203 static void SuffFindDeps(GNode *, SrcList *);
    204 static void SuffExpandWildcards(GNodeListNode *, GNode *);
    205 
    206 /*
    207  * See if pref is a prefix of str.
    208  * Return NULL if it ain't, pointer to character in str after prefix if so.
    209  */
    210 static const char *
    211 SuffStrIsPrefix(const char *pref, const char *str)
    212 {
    213     while (*str && *pref == *str) {
    214 	pref++;
    215 	str++;
    216     }
    217 
    218     return *pref != '\0' ? NULL : str;
    219 }
    220 
    221 /*
    222  * See if suff is a suffix of name.
    223  * Return NULL if it ain't, pointer to the start of suffix in name if it is.
    224  */
    225 static const char *
    226 SuffSuffGetSuffix(const Suff *s, size_t nameLen, const char *nameEnd)
    227 {
    228     const char *p1;		/* Pointer into suffix name */
    229     const char *p2;		/* Pointer into string being examined */
    230 
    231     if (nameLen < s->nameLen)
    232 	return NULL;		/* this string is shorter than the suffix */
    233 
    234     p1 = s->name + s->nameLen;
    235     p2 = nameEnd;
    236 
    237     while (p1 >= s->name && *p1 == *p2) {
    238 	p1--;
    239 	p2--;
    240     }
    241 
    242     /* XXX: s->name - 1 invokes undefined behavior */
    243     return p1 == s->name - 1 ? p2 + 1 : NULL;
    244 }
    245 
    246 static Boolean
    247 SuffSuffIsSuffix(const Suff *suff, size_t nameLen, const char *nameEnd)
    248 {
    249     return SuffSuffGetSuffix(suff, nameLen, nameEnd) != NULL;
    250 }
    251 
    252 static Suff *
    253 FindSuffByNameLen(const char *name, size_t nameLen)
    254 {
    255     SuffListNode *ln;
    256 
    257     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
    258 	Suff *suff = ln->datum;
    259 	if (suff->nameLen == nameLen && memcmp(suff->name, name, nameLen) == 0)
    260 	    return suff;
    261     }
    262     return NULL;
    263 }
    264 
    265 static Suff *
    266 FindSuffByName(const char *name)
    267 {
    268     return FindSuffByNameLen(name, strlen(name));
    269 }
    270 
    271 static GNode *
    272 FindTransformByName(const char *name)
    273 {
    274     GNodeListNode *ln;
    275     for (ln = transforms->first; ln != NULL; ln = ln->next) {
    276 	GNode *gn = ln->datum;
    277 	if (strcmp(gn->name, name) == 0)
    278 	    return gn;
    279     }
    280     return NULL;
    281 }
    282 
    283 static void
    284 SuffUnRef(SuffList *list, Suff *suff)
    285 {
    286     SuffListNode *ln = Lst_FindDatum(list, suff);
    287     if (ln != NULL) {
    288 	Lst_Remove(list, ln);
    289 	suff->refCount--;
    290     }
    291 }
    292 
    293 /* Free up all memory associated with the given suffix structure. */
    294 static void
    295 SuffFree(void *sp)
    296 {
    297     Suff *suff = sp;
    298 
    299     if (suff == suffNull)
    300 	suffNull = NULL;
    301 
    302     if (suff == emptySuff)
    303 	emptySuff = NULL;
    304 
    305 #if 0
    306     /* We don't delete suffixes in order, so we cannot use this */
    307     if (suff->refCount != 0)
    308 	Punt("Internal error deleting suffix `%s' with refcount = %d",
    309 	     suff->name, suff->refCount);
    310 #endif
    311 
    312     Lst_Free(suff->ref);
    313     Lst_Free(suff->children);
    314     Lst_Free(suff->parents);
    315     Lst_Destroy(suff->searchPath, Dir_Destroy);
    316 
    317     free(suff->name);
    318     free(suff);
    319 }
    320 
    321 /* Remove the suffix from the list, and free if it is otherwise unused. */
    322 static void
    323 SuffRemove(SuffList *list, Suff *suff)
    324 {
    325     SuffUnRef(list, suff);
    326     if (suff->refCount == 0) {
    327 	/* XXX: can lead to suff->refCount == -1 */
    328 	SuffUnRef(sufflist, suff);
    329 	SuffFree(suff);
    330     }
    331 }
    332 
    333 /* Insert the suffix into the list, keeping the list ordered by suffix
    334  * number. */
    335 static void
    336 SuffInsert(SuffList *list, Suff *suff)
    337 {
    338     SuffListNode *ln;
    339     Suff *listSuff = NULL;
    340 
    341     for (ln = list->first; ln != NULL; ln = ln->next) {
    342 	listSuff = ln->datum;
    343 	if (listSuff->sNum >= suff->sNum)
    344 	    break;
    345     }
    346 
    347     if (ln == NULL) {
    348 	SUFF_DEBUG2("inserting \"%s\" (%d) at end of list\n",
    349 		    suff->name, suff->sNum);
    350 	Lst_Append(list, suff);
    351 	suff->refCount++;
    352 	Lst_Append(suff->ref, list);
    353     } else if (listSuff->sNum != suff->sNum) {
    354 	SUFF_DEBUG4("inserting \"%s\" (%d) before \"%s\" (%d)\n",
    355 		    suff->name, suff->sNum, listSuff->name, listSuff->sNum);
    356 	Lst_InsertBefore(list, ln, suff);
    357 	suff->refCount++;
    358 	Lst_Append(suff->ref, list);
    359     } else {
    360 	SUFF_DEBUG2("\"%s\" (%d) is already there\n", suff->name, suff->sNum);
    361     }
    362 }
    363 
    364 static Suff *
    365 SuffNew(const char *name)
    366 {
    367     Suff *suff = bmake_malloc(sizeof *suff);
    368 
    369     suff->name = bmake_strdup(name);
    370     suff->nameLen = strlen(suff->name);
    371     suff->searchPath = Lst_New();
    372     suff->children = Lst_New();
    373     suff->parents = Lst_New();
    374     suff->ref = Lst_New();
    375     suff->sNum = sNum++;
    376     suff->flags = 0;
    377     suff->refCount = 1; /* XXX: why 1? It's not assigned anywhere yet. */
    378 
    379     return suff;
    380 }
    381 
    382 /*
    383  * Nuke the list of suffixes but keep all transformation rules around. The
    384  * transformation graph is destroyed in this process, but we leave the list
    385  * of rules so when a new graph is formed, the rules will remain. This
    386  * function is called when a line '.SUFFIXES:' with an empty suffixes list is
    387  * encountered in a makefile.
    388  */
    389 void
    390 Suff_ClearSuffixes(void)
    391 {
    392 #ifdef CLEANUP
    393     Lst_MoveAll(suffClean, sufflist);
    394 #endif
    395     sufflist = Lst_New();
    396     sNum = 0;
    397     if (suffNull)
    398 	SuffFree(suffNull);
    399     emptySuff = suffNull = SuffNew("");
    400 
    401     Dir_Concat(suffNull->searchPath, dirSearchPath);
    402     suffNull->flags = SUFF_NULL;
    403 }
    404 
    405 /* Parse a transformation string such as ".c.o" to find its two component
    406  * suffixes (the source ".c" and the target ".o").  If there are no such
    407  * suffixes, try a single-suffix transformation as well.
    408  *
    409  * Return TRUE if the string is a valid transformation.
    410  */
    411 static Boolean
    412 SuffParseTransform(const char *str, Suff **out_src, Suff **out_targ)
    413 {
    414     SuffListNode *ln;
    415     Suff *singleSrc = NULL;
    416 
    417     /*
    418      * Loop looking first for a suffix that matches the start of the
    419      * string and then for one that exactly matches the rest of it. If
    420      * we can find two that meet these criteria, we've successfully
    421      * parsed the string.
    422      */
    423     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
    424 	Suff *src = ln->datum;
    425 
    426 	if (SuffStrIsPrefix(src->name, str) == NULL)
    427 	    continue;
    428 
    429 	if (str[src->nameLen] == '\0') {
    430 	    singleSrc = src;
    431 	} else {
    432 	    Suff *targ = FindSuffByName(str + src->nameLen);
    433 	    if (targ != NULL) {
    434 		*out_src = src;
    435 		*out_targ = targ;
    436 		return TRUE;
    437 	    }
    438 	}
    439     }
    440 
    441     if (singleSrc != NULL) {
    442 	/*
    443 	 * Not so fast Mr. Smith! There was a suffix that encompassed
    444 	 * the entire string, so we assume it was a transformation
    445 	 * to the null suffix (thank you POSIX). We still prefer to
    446 	 * find a double rule over a singleton, hence we leave this
    447 	 * check until the end.
    448 	 *
    449 	 * XXX: Use emptySuff over suffNull?
    450 	 */
    451 	*out_src = singleSrc;
    452 	*out_targ = suffNull;
    453 	return TRUE;
    454     }
    455     return FALSE;
    456 }
    457 
    458 /* Return TRUE if the given string is a transformation rule, that is, a
    459  * concatenation of two known suffixes such as ".c.o" or a single suffix
    460  * such as ".o". */
    461 Boolean
    462 Suff_IsTransform(const char *str)
    463 {
    464     Suff *src, *targ;
    465 
    466     return SuffParseTransform(str, &src, &targ);
    467 }
    468 
    469 /* Add the transformation rule to the list of rules and place the
    470  * transformation itself in the graph.
    471  *
    472  * The transformation is linked to the two suffixes mentioned in the name.
    473  *
    474  * Input:
    475  *	name		must have the form ".from.to" or just ".from"
    476  *
    477  * Results:
    478  *	The created or existing transformation node in the transforms list
    479  */
    480 GNode *
    481 Suff_AddTransform(const char *name)
    482 {
    483     Suff *srcSuff;
    484     Suff *targSuff;
    485 
    486     GNode *gn = FindTransformByName(name);
    487     if (gn == NULL) {
    488 	/*
    489 	 * Make a new graph node for the transformation. It will be filled in
    490 	 * by the Parse module.
    491 	 */
    492 	gn = Targ_NewGN(name);
    493 	Lst_Append(transforms, gn);
    494     } else {
    495 	/*
    496 	 * New specification for transformation rule. Just nuke the old list
    497 	 * of commands so they can be filled in again... We don't actually
    498 	 * free the commands themselves, because a given command can be
    499 	 * attached to several different transformations.
    500 	 */
    501 	Lst_Free(gn->commands);
    502 	Lst_Free(gn->children);
    503 	gn->commands = Lst_New();
    504 	gn->children = Lst_New();
    505     }
    506 
    507     gn->type = OP_TRANSFORM;
    508 
    509     {
    510 	Boolean ok = SuffParseTransform(name, &srcSuff, &targSuff);
    511 	assert(ok);
    512 	(void)ok;
    513     }
    514 
    515     /*
    516      * link the two together in the proper relationship and order
    517      */
    518     SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    519 		srcSuff->name, targSuff->name);
    520     SuffInsert(targSuff->children, srcSuff);
    521     SuffInsert(srcSuff->parents, targSuff);
    522 
    523     return gn;
    524 }
    525 
    526 /* Handle the finish of a transformation definition, removing the
    527  * transformation from the graph if it has neither commands nor sources.
    528  *
    529  * If the node has no commands or children, the children and parents lists
    530  * of the affected suffixes are altered.
    531  *
    532  * Input:
    533  *	gn		Node for transformation
    534  */
    535 void
    536 Suff_EndTransform(GNode *gn)
    537 {
    538     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(gn->cohorts))
    539 	gn = gn->cohorts->last->datum;
    540 
    541     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
    542 	Lst_IsEmpty(gn->children))
    543     {
    544 	Suff *srcSuff, *targSuff;
    545 
    546 	/*
    547 	 * SuffParseTransform() may fail for special rules which are not
    548 	 * actual transformation rules. (e.g. .DEFAULT)
    549 	 */
    550 	if (SuffParseTransform(gn->name, &srcSuff, &targSuff)) {
    551 
    552 	    /* Remember parents since srcSuff could be deleted in SuffRemove */
    553 	    SuffList *srcSuffParents = srcSuff->parents;
    554 
    555 	    SUFF_DEBUG2("deleting transformation from `%s' to `%s'\n",
    556 			srcSuff->name, targSuff->name);
    557 
    558 	    SuffRemove(targSuff->children, srcSuff);
    559 	    SuffRemove(srcSuffParents, targSuff);
    560 	}
    561     } else if (gn->type & OP_TRANSFORM) {
    562 	SUFF_DEBUG1("transformation %s complete\n", gn->name);
    563     }
    564 }
    565 
    566 /* Called from Suff_AddSuffix to search through the list of
    567  * existing transformation rules and rebuild the transformation graph when
    568  * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
    569  * transformation involving this suffix and another, existing suffix, the
    570  * proper relationship is established between the two.
    571  *
    572  * The appropriate links will be made between this suffix and others if
    573  * transformation rules exist for it.
    574  *
    575  * Input:
    576  *	transform	Transformation to test
    577  *	suff		Suffix to rebuild
    578  */
    579 static void
    580 SuffRebuildGraph(GNode *transform, Suff *suff)
    581 {
    582     const char *name = transform->name;
    583     size_t nameLen = strlen(name);
    584     const char *toName;
    585 
    586     /*
    587      * First see if it is a transformation from this suffix.
    588      */
    589     toName = SuffStrIsPrefix(suff->name, name);
    590     if (toName != NULL) {
    591 	Suff *to = FindSuffByName(toName);
    592 	if (to != NULL) {
    593 	    /* Link in and return, since it can't be anything else. */
    594 	    SuffInsert(to->children, suff);
    595 	    SuffInsert(suff->parents, to);
    596 	    return;
    597 	}
    598     }
    599 
    600     /*
    601      * Not from, maybe to?
    602      */
    603     toName = SuffSuffGetSuffix(suff, nameLen, name + nameLen);
    604     if (toName != NULL) {
    605 	Suff *from = FindSuffByNameLen(name, (size_t)(toName - name));
    606 
    607 	if (from != NULL) {
    608 	    /* establish the proper relationship */
    609 	    SuffInsert(suff->children, from);
    610 	    SuffInsert(from->parents, suff);
    611 	}
    612     }
    613 }
    614 
    615 /* During Suff_AddSuffix, search through the list of existing targets and find
    616  * if any of the existing targets can be turned into a transformation rule.
    617  *
    618  * If such a target is found and the target is the current main target, the
    619  * main target is set to NULL and the next target examined (if that exists)
    620  * becomes the main target.
    621  *
    622  * Results:
    623  *	TRUE iff a new main target has been selected.
    624  */
    625 static Boolean
    626 SuffScanTargets(GNode *target, GNode **inout_main, Suff *gs_s, Boolean *gs_r)
    627 {
    628     Suff *srcSuff, *targSuff;
    629     char *ptr;
    630 
    631     if (*inout_main == NULL && *gs_r && !(target->type & OP_NOTARGET)) {
    632 	*inout_main = target;
    633 	Targ_SetMain(target);
    634 	return TRUE;
    635     }
    636 
    637     if (target->type == OP_TRANSFORM)
    638 	return FALSE;
    639 
    640     if ((ptr = strstr(target->name, gs_s->name)) == NULL ||
    641 	ptr == target->name)
    642 	return FALSE;
    643 
    644     if (SuffParseTransform(target->name, &srcSuff, &targSuff)) {
    645 	if (*inout_main == target) {
    646 	    *gs_r = TRUE;
    647 	    *inout_main = NULL;
    648 	    Targ_SetMain(NULL);
    649 	}
    650 	Lst_Free(target->children);
    651 	target->children = Lst_New();
    652 	target->type = OP_TRANSFORM;
    653 	/*
    654 	 * link the two together in the proper relationship and order
    655 	 */
    656 	SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    657 		    srcSuff->name, targSuff->name);
    658 	SuffInsert(targSuff->children, srcSuff);
    659 	SuffInsert(srcSuff->parents, 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 *l, 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, l, 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->children);
    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->children);
    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 brk_string 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 == '\\' && 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 *explist;
   1232 
   1233     if (!Dir_HasWildcards(cgn->name))
   1234 	return;
   1235 
   1236     /*
   1237      * Expand the word along the chosen path
   1238      */
   1239     explist = Lst_New();
   1240     Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
   1241 
   1242     while (!Lst_IsEmpty(explist)) {
   1243 	GNode	*gn;
   1244 	/*
   1245 	 * Fetch next expansion off the list and find its GNode
   1246 	 */
   1247 	char *cp = Lst_Dequeue(explist);
   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(explist);
   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 *t, Suff *s)
   1322 {
   1323     GNodeListNode *ln, *nln;    /* General node */
   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(s->name, t->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     SUFF_DEBUG3("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
   1347 
   1348     /* Record last child; Make_HandleUse may add child nodes. */
   1349     ln = tGn->children->last;
   1350 
   1351     /* Apply the rule. */
   1352     (void)Make_HandleUse(gn, tGn);
   1353 
   1354     /* Deal with wildcards and variables in any acquired sources. */
   1355     for (ln = ln != NULL ? ln->next : NULL; ln != NULL; ln = nln) {
   1356 	nln = ln->next;
   1357 	SuffExpandChildren(ln, tGn);
   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 = '('; *eoname = ')';
   1488 
   1489     /*
   1490      * Pretend gn appeared to the left of a dependency operator so
   1491      * the user needn't provide a transformation from the member to the
   1492      * archive.
   1493      */
   1494     if (!GNode_IsTarget(gn))
   1495 	gn->type |= OP_DEPENDS;
   1496 
   1497     /*
   1498      * Flag the member as such so we remember to look in the archive for
   1499      * its modification time. The OP_JOIN | OP_MADE is needed because this
   1500      * target should never get made.
   1501      */
   1502     mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
   1503 }
   1504 
   1505 static void
   1506 SuffFindNormalDepsKnown(const char *name, size_t nameLen, GNode *gn,
   1507 			SrcList *srcs, SrcList *targs)
   1508 {
   1509     SuffListNode *ln;
   1510     Src *targ;
   1511     char *pref;
   1512 
   1513     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
   1514 	Suff *suff = ln->datum;
   1515 	if (!SuffSuffIsSuffix(suff, nameLen, name + nameLen))
   1516 	    continue;
   1517 
   1518 	pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen));
   1519 	targ = SrcNew(bmake_strdup(gn->name), pref, suff, NULL, gn);
   1520 	suff->refCount++;
   1521 
   1522 	/*
   1523 	 * Add nodes from which the target can be made
   1524 	 */
   1525 	SuffAddLevel(srcs, targ);
   1526 
   1527 	/*
   1528 	 * Record the target so we can nuke it
   1529 	 */
   1530 	Lst_Append(targs, targ);
   1531     }
   1532 }
   1533 
   1534 static void
   1535 SuffFindNormalDepsUnknown(GNode *gn, const char *sopref,
   1536 			  SrcList *srcs, SrcList *targs)
   1537 {
   1538     Src *targ;
   1539 
   1540     if (!Lst_IsEmpty(targs) || suffNull == NULL)
   1541 	return;
   1542 
   1543     SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
   1544 
   1545     targ = SrcNew(bmake_strdup(gn->name), bmake_strdup(sopref),
   1546 		  suffNull, NULL, gn);
   1547     targ->suff->refCount++;
   1548 
   1549     /*
   1550      * Only use the default suffix rules if we don't have commands
   1551      * defined for this gnode; traditional make programs used to
   1552      * not define suffix rules if the gnode had children but we
   1553      * don't do this anymore.
   1554      */
   1555     if (Lst_IsEmpty(gn->commands))
   1556 	SuffAddLevel(srcs, targ);
   1557     else {
   1558 	SUFF_DEBUG0("not ");
   1559     }
   1560 
   1561     SUFF_DEBUG0("adding suffix rules\n");
   1562 
   1563     Lst_Append(targs, targ);
   1564 }
   1565 
   1566 /*
   1567  * Deal with finding the thing on the default search path. We
   1568  * always do that, not only if the node is only a source (not
   1569  * on the lhs of a dependency operator or [XXX] it has neither
   1570  * children or commands) as the old pmake did.
   1571  */
   1572 static void
   1573 SuffFindNormalDepsPath(GNode *gn, Src *targ)
   1574 {
   1575     if (gn->type & (OP_PHONY | OP_NOPATH))
   1576 	return;
   1577 
   1578     free(gn->path);
   1579     gn->path = Dir_FindFile(gn->name,
   1580 			    (targ == NULL ? dirSearchPath :
   1581 			     targ->suff->searchPath));
   1582     if (gn->path == NULL)
   1583 	return;
   1584 
   1585     Var_Set(TARGET, gn->path, gn);
   1586 
   1587     if (targ != NULL) {
   1588 	/*
   1589 	 * Suffix known for the thing -- trim the suffix off
   1590 	 * the path to form the proper .PREFIX variable.
   1591 	 */
   1592 	size_t savep = strlen(gn->path) - targ->suff->nameLen;
   1593 	char savec;
   1594 	char *ptr;
   1595 
   1596 	if (gn->suffix)
   1597 	    gn->suffix->refCount--;
   1598 	gn->suffix = targ->suff;
   1599 	gn->suffix->refCount++;
   1600 
   1601 	savec = gn->path[savep];
   1602 	gn->path[savep] = '\0';
   1603 
   1604 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1605 	    ptr++;
   1606 	else
   1607 	    ptr = gn->path;
   1608 
   1609 	Var_Set(PREFIX, ptr, gn);
   1610 
   1611 	gn->path[savep] = savec;
   1612     } else {
   1613 	char *ptr;
   1614 
   1615 	/* The .PREFIX gets the full path if the target has no known suffix. */
   1616 	if (gn->suffix)
   1617 	    gn->suffix->refCount--;
   1618 	gn->suffix = NULL;
   1619 
   1620 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1621 	    ptr++;
   1622 	else
   1623 	    ptr = gn->path;
   1624 
   1625 	Var_Set(PREFIX, ptr, gn);
   1626     }
   1627 }
   1628 
   1629 /* Locate implicit dependencies for regular targets.
   1630  *
   1631  * Input:
   1632  *	gn		Node for which to find sources
   1633  *
   1634  * Side Effects:
   1635  *	Same as Suff_FindDeps
   1636  */
   1637 static void
   1638 SuffFindNormalDeps(GNode *gn, SrcList *slst)
   1639 {
   1640     SrcList *srcs;		/* List of sources at which to look */
   1641     SrcList *targs;		/* List of targets to which things can be
   1642 				 * transformed. They all have the same file,
   1643 				 * but different suff and pref fields */
   1644     Src *bottom;		/* Start of found transformation path */
   1645     Src *src;			/* General Src pointer */
   1646     char *pref;			/* Prefix to use */
   1647     Src *targ;			/* General Src target pointer */
   1648 
   1649     const char *name = gn->name;
   1650     size_t nameLen = strlen(name);
   1651 
   1652     /*
   1653      * Begin at the beginning...
   1654      */
   1655     srcs = Lst_New();
   1656     targs = Lst_New();
   1657 
   1658     /*
   1659      * We're caught in a catch-22 here. On the one hand, we want to use any
   1660      * transformation implied by the target's sources, but we can't examine
   1661      * the sources until we've expanded any variables/wildcards they may hold,
   1662      * and we can't do that until we've set up the target's local variables
   1663      * and we can't do that until we know what the proper suffix for the
   1664      * target is (in case there are two suffixes one of which is a suffix of
   1665      * the other) and we can't know that until we've found its implied
   1666      * source, which we may not want to use if there's an existing source
   1667      * that implies a different transformation.
   1668      *
   1669      * In an attempt to get around this, which may not work all the time,
   1670      * but should work most of the time, we look for implied sources first,
   1671      * checking transformations to all possible suffixes of the target,
   1672      * use what we find to set the target's local variables, expand the
   1673      * children, then look for any overriding transformations they imply.
   1674      * Should we find one, we discard the one we found before.
   1675      */
   1676     bottom = NULL;
   1677     targ = NULL;
   1678 
   1679     if (!(gn->type & OP_PHONY)) {
   1680 
   1681 	SuffFindNormalDepsKnown(name, nameLen, gn, srcs, targs);
   1682 
   1683 	/* Handle target of unknown suffix... */
   1684 	SuffFindNormalDepsUnknown(gn, name, srcs, targs);
   1685 
   1686 	/*
   1687 	 * Using the list of possible sources built up from the target
   1688 	 * suffix(es), try and find an existing file/target that matches.
   1689 	 */
   1690 	bottom = SuffFindThem(srcs, slst);
   1691 
   1692 	if (bottom == NULL) {
   1693 	    /*
   1694 	     * No known transformations -- use the first suffix found
   1695 	     * for setting the local variables.
   1696 	     */
   1697 	    if (targs->first != NULL)
   1698 		targ = targs->first->datum;
   1699 	    else
   1700 		targ = NULL;
   1701 	} else {
   1702 	    /*
   1703 	     * Work up the transformation path to find the suffix of the
   1704 	     * target to which the transformation was made.
   1705 	     */
   1706 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   1707 		continue;
   1708 	}
   1709     }
   1710 
   1711     Var_Set(TARGET, GNode_Path(gn), gn);
   1712 
   1713     pref = targ != NULL ? targ->pref : gn->name;
   1714     Var_Set(PREFIX, pref, gn);
   1715 
   1716     /*
   1717      * Now we've got the important local variables set, expand any sources
   1718      * that still contain variables or wildcards in their names.
   1719      */
   1720     {
   1721 	SuffListNode *ln, *nln;
   1722 	for (ln = gn->children->first; ln != NULL; ln = nln) {
   1723 	    nln = ln->next;
   1724 	    SuffExpandChildren(ln, gn);
   1725 	}
   1726     }
   1727 
   1728     if (targ == NULL) {
   1729 	SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
   1730 
   1731 sfnd_abort:
   1732 	SuffFindNormalDepsPath(gn, targ);
   1733 	goto sfnd_return;
   1734     }
   1735 
   1736     /*
   1737      * If the suffix indicates that the target is a library, mark that in
   1738      * the node's type field.
   1739      */
   1740     if (targ->suff->flags & SUFF_LIBRARY)
   1741 	gn->type |= OP_LIB;
   1742 
   1743     /*
   1744      * Check for overriding transformation rule implied by sources
   1745      */
   1746     if (!Lst_IsEmpty(gn->children)) {
   1747 	src = SuffFindCmds(targ, slst);
   1748 
   1749 	if (src != NULL) {
   1750 	    /*
   1751 	     * Free up all the Src structures in the transformation path
   1752 	     * up to, but not including, the parent node.
   1753 	     */
   1754 	    while (bottom != NULL && bottom->parent != NULL) {
   1755 		if (Lst_FindDatum(slst, bottom) == NULL)
   1756 		    Lst_Append(slst, bottom);
   1757 		bottom = bottom->parent;
   1758 	    }
   1759 	    bottom = src;
   1760 	}
   1761     }
   1762 
   1763     if (bottom == NULL) {
   1764 	/*
   1765 	 * No idea from where it can come -- return now.
   1766 	 */
   1767 	goto sfnd_abort;
   1768     }
   1769 
   1770     /*
   1771      * We now have a list of Src structures headed by 'bottom' and linked via
   1772      * their 'parent' pointers. What we do next is create links between
   1773      * source and target nodes (which may or may not have been created)
   1774      * and set the necessary local variables in each target. The
   1775      * commands for each target are set from the commands of the
   1776      * transformation rule used to get from the src suffix to the targ
   1777      * suffix. Note that this causes the commands list of the original
   1778      * node, gn, to be replaced by the commands of the final
   1779      * transformation rule. Also, the unmade field of gn is incremented.
   1780      * Etc.
   1781      */
   1782     if (bottom->node == NULL)
   1783 	bottom->node = Targ_GetNode(bottom->file);
   1784 
   1785     for (src = bottom; src->parent != NULL; src = src->parent) {
   1786 	targ = src->parent;
   1787 
   1788 	if (src->node->suffix)
   1789 	    src->node->suffix->refCount--;
   1790 	src->node->suffix = src->suff;
   1791 	src->node->suffix->refCount++;
   1792 
   1793 	if (targ->node == NULL)
   1794 	    targ->node = Targ_GetNode(targ->file);
   1795 
   1796 	SuffApplyTransform(targ->node, src->node,
   1797 			   targ->suff, src->suff);
   1798 
   1799 	if (targ->node != gn) {
   1800 	    /*
   1801 	     * Finish off the dependency-search process for any nodes
   1802 	     * between bottom and gn (no point in questing around the
   1803 	     * filesystem for their implicit source when it's already
   1804 	     * known). Note that the node can't have any sources that
   1805 	     * need expanding, since SuffFindThem will stop on an existing
   1806 	     * node, so all we need to do is set the standard variables.
   1807 	     */
   1808 	    targ->node->type |= OP_DEPS_FOUND;
   1809 
   1810 	    Var_Set(PREFIX, targ->pref, targ->node);
   1811 
   1812 	    Var_Set(TARGET, targ->node->name, targ->node);
   1813 	}
   1814     }
   1815 
   1816     if (gn->suffix != NULL)
   1817 	gn->suffix->refCount--;
   1818     gn->suffix = src->suff;
   1819     gn->suffix->refCount++;
   1820 
   1821     /*
   1822      * Nuke the transformation path and the Src structures left over in the
   1823      * two lists.
   1824      */
   1825 sfnd_return:
   1826     if (bottom != NULL)
   1827 	if (Lst_FindDatum(slst, bottom) == NULL)
   1828 	    Lst_Append(slst, bottom);
   1829 
   1830     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
   1831 	continue;
   1832 
   1833     Lst_MoveAll(slst, srcs);
   1834     Lst_MoveAll(slst, targs);
   1835 }
   1836 
   1837 
   1838 /* Find implicit sources for the target.
   1839  *
   1840  * Nodes are added to the graph below the passed-in node. The nodes are
   1841  * marked to have their IMPSRC variable filled in. The PREFIX variable is set
   1842  * for the given node and all its implied children.
   1843  *
   1844  * The path found by this target is the shortest path in the transformation
   1845  * graph, which may pass through non-existent targets, to an existing target.
   1846  * The search continues on all paths from the root suffix until a file is
   1847  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
   1848  * .l,v file exists but the .c and .l files don't, the search will branch out
   1849  * in all directions from .o and again from all the nodes on the next level
   1850  * until the .l,v node is encountered.
   1851  */
   1852 void
   1853 Suff_FindDeps(GNode *gn)
   1854 {
   1855 
   1856     SuffFindDeps(gn, srclist);
   1857     while (SuffRemoveSrc(srclist))
   1858 	continue;
   1859 }
   1860 
   1861 static void
   1862 SuffFindDeps(GNode *gn, SrcList *slst)
   1863 {
   1864     if (gn->type & OP_DEPS_FOUND)
   1865 	return;
   1866     gn->type |= OP_DEPS_FOUND;
   1867 
   1868     /*
   1869      * Make sure we have these set, may get revised below.
   1870      */
   1871     Var_Set(TARGET, GNode_Path(gn), gn);
   1872     Var_Set(PREFIX, gn->name, gn);
   1873 
   1874     SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
   1875 
   1876     if (gn->type & OP_ARCHV) {
   1877 	SuffFindArchiveDeps(gn, slst);
   1878     } else if (gn->type & OP_LIB) {
   1879 	/*
   1880 	 * If the node is a library, it is the arch module's job to find it
   1881 	 * and set the TARGET variable accordingly. We merely provide the
   1882 	 * search path, assuming all libraries end in ".a" (if the suffix
   1883 	 * hasn't been defined, there's nothing we can do for it, so we just
   1884 	 * set the TARGET variable to the node's name in order to give it a
   1885 	 * value).
   1886 	 */
   1887 	Suff *s = FindSuffByName(LIBSUFF);
   1888 	if (gn->suffix)
   1889 	    gn->suffix->refCount--;
   1890 	if (s != NULL) {
   1891 	    gn->suffix = s;
   1892 	    gn->suffix->refCount++;
   1893 	    Arch_FindLib(gn, s->searchPath);
   1894 	} else {
   1895 	    gn->suffix = NULL;
   1896 	    Var_Set(TARGET, gn->name, gn);
   1897 	}
   1898 	/*
   1899 	 * Because a library (-lfoo) target doesn't follow the standard
   1900 	 * filesystem conventions, we don't set the regular variables for
   1901 	 * the thing. .PREFIX is simply made empty...
   1902 	 */
   1903 	Var_Set(PREFIX, "", gn);
   1904     } else {
   1905 	SuffFindNormalDeps(gn, slst);
   1906     }
   1907 }
   1908 
   1909 /* Define which suffix is the null suffix.
   1910  *
   1911  * Need to handle the changing of the null suffix gracefully so the old
   1912  * transformation rules don't just go away.
   1913  *
   1914  * Input:
   1915  *	name		Name of null suffix
   1916  */
   1917 void
   1918 Suff_SetNull(const char *name)
   1919 {
   1920     Suff *s = FindSuffByName(name);
   1921     if (s == NULL) {
   1922 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
   1923 		    name);
   1924 	return;
   1925     }
   1926 
   1927     if (suffNull != NULL)
   1928 	suffNull->flags &= ~(unsigned)SUFF_NULL;
   1929     s->flags |= SUFF_NULL;
   1930     /*
   1931      * XXX: Here's where the transformation mangling would take place
   1932      */
   1933     suffNull = s;
   1934 }
   1935 
   1936 /* Initialize the suffixes module. */
   1937 void
   1938 Suff_Init(void)
   1939 {
   1940 #ifdef CLEANUP
   1941     suffClean = Lst_New();
   1942     sufflist = Lst_New();
   1943 #endif
   1944     srclist = Lst_New();
   1945     transforms = Lst_New();
   1946 
   1947     /*
   1948      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   1949      * actually go on the suffix list or everyone will think that's its
   1950      * suffix.
   1951      */
   1952     Suff_ClearSuffixes();
   1953 }
   1954 
   1955 
   1956 /* Clean up the suffixes module. */
   1957 void
   1958 Suff_End(void)
   1959 {
   1960 #ifdef CLEANUP
   1961     Lst_Destroy(sufflist, SuffFree);
   1962     Lst_Destroy(suffClean, SuffFree);
   1963     if (suffNull)
   1964 	SuffFree(suffNull);
   1965     Lst_Free(srclist);
   1966     Lst_Free(transforms);
   1967 #endif
   1968 }
   1969 
   1970 
   1971 static void
   1972 PrintSuffNames(const char *prefix, SuffList *suffs)
   1973 {
   1974     SuffListNode *ln;
   1975 
   1976     debug_printf("#\t%s: ", prefix);
   1977     for (ln = suffs->first; ln != NULL; ln = ln->next) {
   1978 	Suff *suff = ln->datum;
   1979 	debug_printf("%s ", suff->name);
   1980     }
   1981     debug_printf("\n");
   1982 }
   1983 
   1984 static void
   1985 PrintSuff(Suff *suff)
   1986 {
   1987     debug_printf("# \"%s\" (num %d, ref %d)",
   1988 		 suff->name, suff->sNum, suff->refCount);
   1989     if (suff->flags != 0) {
   1990 	char flags_buf[SuffFlags_ToStringSize];
   1991 
   1992 	debug_printf(" (%s)",
   1993 		     Enum_FlagsToString(flags_buf, sizeof flags_buf,
   1994 					suff->flags, SuffFlags_ToStringSpecs));
   1995     }
   1996     debug_printf("\n");
   1997 
   1998     PrintSuffNames("To", suff->parents);
   1999     PrintSuffNames("From", suff->children);
   2000 
   2001     debug_printf("#\tSearch Path: ");
   2002     Dir_PrintPath(suff->searchPath);
   2003     debug_printf("\n");
   2004 }
   2005 
   2006 static void
   2007 PrintTransformation(GNode *t)
   2008 {
   2009     debug_printf("%-16s:", t->name);
   2010     Targ_PrintType(t->type);
   2011     debug_printf("\n");
   2012     Targ_PrintCmds(t);
   2013     debug_printf("\n");
   2014 }
   2015 
   2016 void
   2017 Suff_PrintAll(void)
   2018 {
   2019     debug_printf("#*** Suffixes:\n");
   2020     {
   2021 	SuffListNode *ln;
   2022 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   2023 	    PrintSuff(ln->datum);
   2024     }
   2025 
   2026     debug_printf("#*** Transformations:\n");
   2027     {
   2028 	GNodeListNode *ln;
   2029 	for (ln = transforms->first; ln != NULL; ln = ln->next)
   2030 	    PrintTransformation(ln->datum);
   2031     }
   2032 }
   2033