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