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