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