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