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