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