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