Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.28
      1 /*	$NetBSD: suff.c,v 1.28 2000/01/22 19:28:11 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.28 2000/01/22 19:28:11 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.28 2000/01/22 19:28:11 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_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
    711 	gn = (GNode *) Lst_Datum (Lst_Last (gn->cohorts));
    712     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
    713 	Lst_IsEmpty(gn->children))
    714     {
    715 	Suff	*s, *t;
    716 	Lst	 p;
    717 
    718 	(void)SuffParseTransform(gn->name, &s, &t);
    719 
    720 	if (DEBUG(SUFF)) {
    721 	    printf("deleting transformation from `%s' to `%s'\n",
    722 		    s->name, t->name);
    723 	}
    724 
    725 	/*
    726 	 * Store s->parents because s could be deleted in SuffRemove
    727 	 */
    728 	p = s->parents;
    729 
    730 	/*
    731 	 * Remove the source from the target's children list. We check for a
    732 	 * nil return to handle a beanhead saying something like
    733 	 *  .c.o .c.o:
    734 	 *
    735 	 * We'll be called twice when the next target is seen, but .c and .o
    736 	 * are only linked once...
    737 	 */
    738 	SuffRemove(t->children, s);
    739 
    740 	/*
    741 	 * Remove the target from the source's parents list
    742 	 */
    743 	SuffRemove(p, t);
    744     } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
    745 	printf("transformation %s complete\n", gn->name);
    746     }
    747 
    748     return(dummy ? 0 : 0);
    749 }
    750 
    751 /*-
    752  *-----------------------------------------------------------------------
    753  * SuffRebuildGraph --
    754  *	Called from Suff_AddSuffix via Lst_ForEach to search through the
    755  *	list of existing transformation rules and rebuild the transformation
    756  *	graph when it has been destroyed by Suff_ClearSuffixes. If the
    757  *	given rule is a transformation involving this suffix and another,
    758  *	existing suffix, the proper relationship is established between
    759  *	the two.
    760  *
    761  * Results:
    762  *	Always 0.
    763  *
    764  * Side Effects:
    765  *	The appropriate links will be made between this suffix and
    766  *	others if transformation rules exist for it.
    767  *
    768  *-----------------------------------------------------------------------
    769  */
    770 static int
    771 SuffRebuildGraph(transformp, sp)
    772     ClientData  transformp; /* Transformation to test */
    773     ClientData  sp;	    /* Suffix to rebuild */
    774 {
    775     GNode   	*transform = (GNode *) transformp;
    776     Suff    	*s = (Suff *) sp;
    777     char 	*cp;
    778     LstNode	ln;
    779     Suff  	*s2;
    780     SuffixCmpData sd;
    781 
    782     /*
    783      * First see if it is a transformation from this suffix.
    784      */
    785     cp = SuffStrIsPrefix(s->name, transform->name);
    786     if (cp != (char *)NULL) {
    787 	ln = Lst_Find(sufflist, (ClientData)cp, SuffSuffHasNameP);
    788 	if (ln != NILLNODE) {
    789 	    /*
    790 	     * Found target. Link in and return, since it can't be anything
    791 	     * else.
    792 	     */
    793 	    s2 = (Suff *)Lst_Datum(ln);
    794 	    SuffInsert(s2->children, s);
    795 	    SuffInsert(s->parents, s2);
    796 	    return(0);
    797 	}
    798     }
    799 
    800     /*
    801      * Not from, maybe to?
    802      */
    803     sd.len = strlen(transform->name);
    804     sd.ename = transform->name + sd.len;
    805     cp = SuffSuffIsSuffix(s, &sd);
    806     if (cp != (char *)NULL) {
    807 	/*
    808 	 * Null-terminate the source suffix in order to find it.
    809 	 */
    810 	cp[1] = '\0';
    811 	ln = Lst_Find(sufflist, (ClientData)transform->name, SuffSuffHasNameP);
    812 	/*
    813 	 * Replace the start of the target suffix
    814 	 */
    815 	cp[1] = s->name[0];
    816 	if (ln != NILLNODE) {
    817 	    /*
    818 	     * Found it -- establish the proper relationship
    819 	     */
    820 	    s2 = (Suff *)Lst_Datum(ln);
    821 	    SuffInsert(s->children, s2);
    822 	    SuffInsert(s2->parents, s);
    823 	}
    824     }
    825     return(0);
    826 }
    827 
    828 /*-
    829  *-----------------------------------------------------------------------
    830  * SuffScanTargets --
    831  *	Called from Suff_AddSuffix via Lst_ForEach to search through the
    832  *	list of existing targets and find if any of the existing targets
    833  *	can be turned into a transformation rule.
    834  *
    835  * Results:
    836  *	1 if a new main target has been selected, 0 otherwise.
    837  *
    838  * Side Effects:
    839  *	If such a target is found and the target is the current main
    840  *	target, the main target is set to NULL and the next target
    841  *	examined (if that exists) becomes the main target.
    842  *
    843  *-----------------------------------------------------------------------
    844  */
    845 static int
    846 SuffScanTargets(targetp, gsp)
    847     ClientData  targetp;
    848     ClientData  gsp;
    849 {
    850     GNode   	*target = (GNode *) targetp;
    851     GNodeSuff	*gs = (GNodeSuff *) gsp;
    852     Suff	*s, *t;
    853     char 	*ptr;
    854 
    855     if (*gs->gn == NILGNODE && gs->r && (target->type & OP_NOTARGET) == 0) {
    856 	*gs->gn = target;
    857 	Targ_SetMain(target);
    858 	return 1;
    859     }
    860 
    861     if (target->type == OP_TRANSFORM)
    862 	return 0;
    863 
    864     if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
    865 	ptr == target->name)
    866 	return 0;
    867 
    868     if (SuffParseTransform(target->name, &s, &t)) {
    869 	if (*gs->gn == target) {
    870 	    gs->r = TRUE;
    871 	    *gs->gn = NILGNODE;
    872 	    Targ_SetMain(NILGNODE);
    873 	}
    874 	Lst_Destroy (target->children, NOFREE);
    875 	target->children = Lst_Init (FALSE);
    876 	target->type = OP_TRANSFORM;
    877 	/*
    878 	 * link the two together in the proper relationship and order
    879 	 */
    880 	if (DEBUG(SUFF)) {
    881 	    printf("defining transformation from `%s' to `%s'\n",
    882 		s->name, t->name);
    883 	}
    884 	SuffInsert (t->children, s);
    885 	SuffInsert (s->parents, t);
    886     }
    887     return 0;
    888 }
    889 
    890 /*-
    891  *-----------------------------------------------------------------------
    892  * Suff_AddSuffix --
    893  *	Add the suffix in string to the end of the list of known suffixes.
    894  *	Should we restructure the suffix graph? Make doesn't...
    895  *
    896  * Results:
    897  *	None
    898  *
    899  * Side Effects:
    900  *	A GNode is created for the suffix and a Suff structure is created and
    901  *	added to the suffixes list unless the suffix was already known.
    902  *	The mainNode passed can be modified if a target mutated into a
    903  *	transform and that target happened to be the main target.
    904  *-----------------------------------------------------------------------
    905  */
    906 void
    907 Suff_AddSuffix (str, gn)
    908     char       *str;	    /* the name of the suffix to add */
    909     GNode      **gn;
    910 {
    911     Suff          *s;	    /* new suffix descriptor */
    912     LstNode 	  ln;
    913     GNodeSuff	  gs;
    914 
    915     ln = Lst_Find (sufflist, (ClientData)str, SuffSuffHasNameP);
    916     if (ln == NILLNODE) {
    917 	s = (Suff *) emalloc (sizeof (Suff));
    918 
    919 	s->name =   	estrdup (str);
    920 	s->nameLen = 	strlen (s->name);
    921 	s->searchPath = Lst_Init (FALSE);
    922 	s->children = 	Lst_Init (FALSE);
    923 	s->parents = 	Lst_Init (FALSE);
    924 	s->ref = 	Lst_Init (FALSE);
    925 	s->sNum =   	sNum++;
    926 	s->flags =  	0;
    927 	s->refCount =	0;
    928 
    929 	(void)Lst_AtEnd (sufflist, (ClientData)s);
    930 	/*
    931 	 * We also look at our existing targets list to see if adding
    932 	 * this suffix will make one of our current targets mutate into
    933 	 * a suffix rule. This is ugly, but other makes treat all targets
    934 	 * that start with a . as suffix rules.
    935 	 */
    936 	gs.gn = gn;
    937 	gs.s  = s;
    938 	gs.r  = FALSE;
    939 	Lst_ForEach (Targ_List(), SuffScanTargets, (ClientData) &gs);
    940 	/*
    941 	 * Look for any existing transformations from or to this suffix.
    942 	 * XXX: Only do this after a Suff_ClearSuffixes?
    943 	 */
    944 	Lst_ForEach (transforms, SuffRebuildGraph, (ClientData) s);
    945     }
    946 }
    947 
    948 /*-
    949  *-----------------------------------------------------------------------
    950  * Suff_GetPath --
    951  *	Return the search path for the given suffix, if it's defined.
    952  *
    953  * Results:
    954  *	The searchPath for the desired suffix or NILLST if the suffix isn't
    955  *	defined.
    956  *
    957  * Side Effects:
    958  *	None
    959  *-----------------------------------------------------------------------
    960  */
    961 Lst
    962 Suff_GetPath (sname)
    963     char    	  *sname;
    964 {
    965     LstNode   	  ln;
    966     Suff    	  *s;
    967 
    968     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
    969     if (ln == NILLNODE) {
    970 	return (NILLST);
    971     } else {
    972 	s = (Suff *) Lst_Datum (ln);
    973 	return (s->searchPath);
    974     }
    975 }
    976 
    977 /*-
    978  *-----------------------------------------------------------------------
    979  * Suff_DoPaths --
    980  *	Extend the search paths for all suffixes to include the default
    981  *	search path.
    982  *
    983  * Results:
    984  *	None.
    985  *
    986  * Side Effects:
    987  *	The searchPath field of all the suffixes is extended by the
    988  *	directories in dirSearchPath. If paths were specified for the
    989  *	".h" suffix, the directories are stuffed into a global variable
    990  *	called ".INCLUDES" with each directory preceeded by a -I. The same
    991  *	is done for the ".a" suffix, except the variable is called
    992  *	".LIBS" and the flag is -L.
    993  *-----------------------------------------------------------------------
    994  */
    995 void
    996 Suff_DoPaths()
    997 {
    998     register Suff   	*s;
    999     register LstNode  	ln;
   1000     char		*ptr;
   1001     Lst	    	    	inIncludes; /* Cumulative .INCLUDES path */
   1002     Lst	    	    	inLibs;	    /* Cumulative .LIBS path */
   1003 
   1004     if (Lst_Open (sufflist) == FAILURE) {
   1005 	return;
   1006     }
   1007 
   1008     inIncludes = Lst_Init(FALSE);
   1009     inLibs = Lst_Init(FALSE);
   1010 
   1011     while ((ln = Lst_Next (sufflist)) != NILLNODE) {
   1012 	s = (Suff *) Lst_Datum (ln);
   1013 	if (!Lst_IsEmpty (s->searchPath)) {
   1014 #ifdef INCLUDES
   1015 	    if (s->flags & SUFF_INCLUDE) {
   1016 		Dir_Concat(inIncludes, s->searchPath);
   1017 	    }
   1018 #endif /* INCLUDES */
   1019 #ifdef LIBRARIES
   1020 	    if (s->flags & SUFF_LIBRARY) {
   1021 		Dir_Concat(inLibs, s->searchPath);
   1022 	    }
   1023 #endif /* LIBRARIES */
   1024 	    Dir_Concat(s->searchPath, dirSearchPath);
   1025 	} else {
   1026 	    Lst_Destroy (s->searchPath, Dir_Destroy);
   1027 	    s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
   1028 	}
   1029     }
   1030 
   1031     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
   1032     free(ptr);
   1033     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
   1034     free(ptr);
   1035 
   1036     Lst_Destroy(inIncludes, Dir_Destroy);
   1037     Lst_Destroy(inLibs, Dir_Destroy);
   1038 
   1039     Lst_Close (sufflist);
   1040 }
   1041 
   1042 /*-
   1043  *-----------------------------------------------------------------------
   1044  * Suff_AddInclude --
   1045  *	Add the given suffix as a type of file which gets included.
   1046  *	Called from the parse module when a .INCLUDES line is parsed.
   1047  *	The suffix must have already been defined.
   1048  *
   1049  * Results:
   1050  *	None.
   1051  *
   1052  * Side Effects:
   1053  *	The SUFF_INCLUDE bit is set in the suffix's flags field
   1054  *
   1055  *-----------------------------------------------------------------------
   1056  */
   1057 void
   1058 Suff_AddInclude (sname)
   1059     char	  *sname;     /* Name of suffix to mark */
   1060 {
   1061     LstNode	  ln;
   1062     Suff	  *s;
   1063 
   1064     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
   1065     if (ln != NILLNODE) {
   1066 	s = (Suff *) Lst_Datum (ln);
   1067 	s->flags |= SUFF_INCLUDE;
   1068     }
   1069 }
   1070 
   1071 /*-
   1072  *-----------------------------------------------------------------------
   1073  * Suff_AddLib --
   1074  *	Add the given suffix as a type of file which is a library.
   1075  *	Called from the parse module when parsing a .LIBS line. The
   1076  *	suffix must have been defined via .SUFFIXES before this is
   1077  *	called.
   1078  *
   1079  * Results:
   1080  *	None.
   1081  *
   1082  * Side Effects:
   1083  *	The SUFF_LIBRARY bit is set in the suffix's flags field
   1084  *
   1085  *-----------------------------------------------------------------------
   1086  */
   1087 void
   1088 Suff_AddLib (sname)
   1089     char	  *sname;     /* Name of suffix to mark */
   1090 {
   1091     LstNode	  ln;
   1092     Suff	  *s;
   1093 
   1094     ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
   1095     if (ln != NILLNODE) {
   1096 	s = (Suff *) Lst_Datum (ln);
   1097 	s->flags |= SUFF_LIBRARY;
   1098     }
   1099 }
   1100 
   1101  	  /********** Implicit Source Search Functions *********/
   1102 
   1103 /*-
   1104  *-----------------------------------------------------------------------
   1105  * SuffAddSrc  --
   1106  *	Add a suffix as a Src structure to the given list with its parent
   1107  *	being the given Src structure. If the suffix is the null suffix,
   1108  *	the prefix is used unaltered as the file name in the Src structure.
   1109  *
   1110  * Results:
   1111  *	always returns 0
   1112  *
   1113  * Side Effects:
   1114  *	A Src structure is created and tacked onto the end of the list
   1115  *-----------------------------------------------------------------------
   1116  */
   1117 static int
   1118 SuffAddSrc (sp, lsp)
   1119     ClientData	sp;	    /* suffix for which to create a Src structure */
   1120     ClientData  lsp;	    /* list and parent for the new Src */
   1121 {
   1122     Suff	*s = (Suff *) sp;
   1123     LstSrc      *ls = (LstSrc *) lsp;
   1124     Src         *s2;	    /* new Src structure */
   1125     Src    	*targ; 	    /* Target structure */
   1126 
   1127     targ = ls->s;
   1128 
   1129     if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
   1130 	/*
   1131 	 * If the suffix has been marked as the NULL suffix, also create a Src
   1132 	 * structure for a file with no suffix attached. Two birds, and all
   1133 	 * that...
   1134 	 */
   1135 	s2 = (Src *) emalloc (sizeof (Src));
   1136 	s2->file =  	estrdup(targ->pref);
   1137 	s2->pref =  	targ->pref;
   1138 	s2->parent = 	targ;
   1139 	s2->node =  	NILGNODE;
   1140 	s2->suff =  	s;
   1141 	s->refCount++;
   1142 	s2->children =	0;
   1143 	targ->children += 1;
   1144 	(void)Lst_AtEnd (ls->l, (ClientData)s2);
   1145 #ifdef DEBUG_SRC
   1146 	s2->cp = Lst_Init(FALSE);
   1147 	Lst_AtEnd(targ->cp, (ClientData) s2);
   1148 	printf("1 add %x %x to %x:", targ, s2, ls->l);
   1149 	Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
   1150 	printf("\n");
   1151 #endif
   1152     }
   1153     s2 = (Src *) emalloc (sizeof (Src));
   1154     s2->file = 	    str_concat (targ->pref, s->name, 0);
   1155     s2->pref =	    targ->pref;
   1156     s2->parent =    targ;
   1157     s2->node = 	    NILGNODE;
   1158     s2->suff = 	    s;
   1159     s->refCount++;
   1160     s2->children =  0;
   1161     targ->children += 1;
   1162     (void)Lst_AtEnd (ls->l, (ClientData)s2);
   1163 #ifdef DEBUG_SRC
   1164     s2->cp = Lst_Init(FALSE);
   1165     Lst_AtEnd(targ->cp, (ClientData) s2);
   1166     printf("2 add %x %x to %x:", targ, s2, ls->l);
   1167     Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
   1168     printf("\n");
   1169 #endif
   1170 
   1171     return(0);
   1172 }
   1173 
   1174 /*-
   1175  *-----------------------------------------------------------------------
   1176  * SuffAddLevel  --
   1177  *	Add all the children of targ as Src structures to the given list
   1178  *
   1179  * Results:
   1180  *	None
   1181  *
   1182  * Side Effects:
   1183  * 	Lots of structures are created and added to the list
   1184  *-----------------------------------------------------------------------
   1185  */
   1186 static void
   1187 SuffAddLevel (l, targ)
   1188     Lst            l;		/* list to which to add the new level */
   1189     Src            *targ;	/* Src structure to use as the parent */
   1190 {
   1191     LstSrc         ls;
   1192 
   1193     ls.s = targ;
   1194     ls.l = l;
   1195 
   1196     Lst_ForEach (targ->suff->children, SuffAddSrc, (ClientData)&ls);
   1197 }
   1198 
   1199 /*-
   1200  *----------------------------------------------------------------------
   1201  * SuffRemoveSrc --
   1202  *	Free all src structures in list that don't have a reference count
   1203  *
   1204  * Results:
   1205  *	Ture if an src was removed
   1206  *
   1207  * Side Effects:
   1208  *	The memory is free'd.
   1209  *----------------------------------------------------------------------
   1210  */
   1211 static int
   1212 SuffRemoveSrc (l)
   1213     Lst l;
   1214 {
   1215     LstNode ln;
   1216     Src *s;
   1217     int t = 0;
   1218 
   1219     if (Lst_Open (l) == FAILURE) {
   1220 	return 0;
   1221     }
   1222 #ifdef DEBUG_SRC
   1223     printf("cleaning %lx: ", (unsigned long) l);
   1224     Lst_ForEach(l, PrintAddr, (ClientData) 0);
   1225     printf("\n");
   1226 #endif
   1227 
   1228 
   1229     while ((ln = Lst_Next (l)) != NILLNODE) {
   1230 	s = (Src *) Lst_Datum (ln);
   1231 	if (s->children == 0) {
   1232 	    free ((Address)s->file);
   1233 	    if (!s->parent)
   1234 		free((Address)s->pref);
   1235 	    else {
   1236 #ifdef DEBUG_SRC
   1237 		LstNode ln = Lst_Member(s->parent->cp, (ClientData)s);
   1238 		if (ln != NILLNODE)
   1239 		    Lst_Remove(s->parent->cp, ln);
   1240 #endif
   1241 		--s->parent->children;
   1242 	    }
   1243 #ifdef DEBUG_SRC
   1244 	    printf("free: [l=%x] p=%x %d\n", l, s, s->children);
   1245 	    Lst_Destroy(s->cp, NOFREE);
   1246 #endif
   1247 	    Lst_Remove(l, ln);
   1248 	    free ((Address)s);
   1249 	    t |= 1;
   1250 	    Lst_Close(l);
   1251 	    return TRUE;
   1252 	}
   1253 #ifdef DEBUG_SRC
   1254 	else {
   1255 	    printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
   1256 	    Lst_ForEach(s->cp, PrintAddr, (ClientData) 0);
   1257 	    printf("\n");
   1258 	}
   1259 #endif
   1260     }
   1261 
   1262     Lst_Close(l);
   1263 
   1264     return t;
   1265 }
   1266 
   1267 /*-
   1268  *-----------------------------------------------------------------------
   1269  * SuffFindThem --
   1270  *	Find the first existing file/target in the list srcs
   1271  *
   1272  * Results:
   1273  *	The lowest structure in the chain of transformations
   1274  *
   1275  * Side Effects:
   1276  *	None
   1277  *-----------------------------------------------------------------------
   1278  */
   1279 static Src *
   1280 SuffFindThem (srcs, slst)
   1281     Lst            srcs;	/* list of Src structures to search through */
   1282     Lst		   slst;
   1283 {
   1284     Src            *s;		/* current Src */
   1285     Src		   *rs;		/* returned Src */
   1286     char	   *ptr;
   1287 
   1288     rs = (Src *) NULL;
   1289 
   1290     while (!Lst_IsEmpty (srcs)) {
   1291 	s = (Src *) Lst_DeQueue (srcs);
   1292 
   1293 	if (DEBUG(SUFF)) {
   1294 	    printf ("\ttrying %s...", s->file);
   1295 	}
   1296 
   1297 	/*
   1298 	 * A file is considered to exist if either a node exists in the
   1299 	 * graph for it or the file actually exists.
   1300 	 */
   1301 	if (Targ_FindNode(s->file, TARG_NOCREATE) != NILGNODE) {
   1302 #ifdef DEBUG_SRC
   1303 	    printf("remove %x from %x\n", s, srcs);
   1304 #endif
   1305 	    rs = s;
   1306 	    break;
   1307 	}
   1308 
   1309 	if ((ptr = Dir_FindFile (s->file, s->suff->searchPath)) != NULL) {
   1310 	    rs = s;
   1311 #ifdef DEBUG_SRC
   1312 	    printf("remove %x from %x\n", s, srcs);
   1313 #endif
   1314 	    free(ptr);
   1315 	    break;
   1316 	}
   1317 
   1318 	if (DEBUG(SUFF)) {
   1319 	    printf ("not there\n");
   1320 	}
   1321 
   1322 	SuffAddLevel (srcs, s);
   1323 	Lst_AtEnd(slst, (ClientData) s);
   1324     }
   1325 
   1326     if (DEBUG(SUFF) && rs) {
   1327 	printf ("got it\n");
   1328     }
   1329     return (rs);
   1330 }
   1331 
   1332 /*-
   1333  *-----------------------------------------------------------------------
   1334  * SuffFindCmds --
   1335  *	See if any of the children of the target in the Src structure is
   1336  *	one from which the target can be transformed. If there is one,
   1337  *	a Src structure is put together for it and returned.
   1338  *
   1339  * Results:
   1340  *	The Src structure of the "winning" child, or NIL if no such beast.
   1341  *
   1342  * Side Effects:
   1343  *	A Src structure may be allocated.
   1344  *
   1345  *-----------------------------------------------------------------------
   1346  */
   1347 static Src *
   1348 SuffFindCmds (targ, slst)
   1349     Src	    	*targ;	/* Src structure to play with */
   1350     Lst		slst;
   1351 {
   1352     LstNode 	  	ln; 	/* General-purpose list node */
   1353     register GNode	*t, 	/* Target GNode */
   1354 	    	  	*s; 	/* Source GNode */
   1355     int	    	  	prefLen;/* The length of the defined prefix */
   1356     Suff    	  	*suff;	/* Suffix on matching beastie */
   1357     Src	    	  	*ret;	/* Return value */
   1358     char    	  	*cp;
   1359 
   1360     t = targ->node;
   1361     (void) Lst_Open (t->children);
   1362     prefLen = strlen (targ->pref);
   1363 
   1364     while ((ln = Lst_Next (t->children)) != NILLNODE) {
   1365 	s = (GNode *)Lst_Datum (ln);
   1366 
   1367 	cp = strrchr (s->name, '/');
   1368 	if (cp == (char *)NULL) {
   1369 	    cp = s->name;
   1370 	} else {
   1371 	    cp++;
   1372 	}
   1373 	if (strncmp (cp, targ->pref, prefLen) == 0) {
   1374 	    /*
   1375 	     * The node matches the prefix ok, see if it has a known
   1376 	     * suffix.
   1377 	     */
   1378 	    ln = Lst_Find (sufflist, (ClientData)&cp[prefLen],
   1379 			   SuffSuffHasNameP);
   1380 	    if (ln != NILLNODE) {
   1381 		/*
   1382 		 * It even has a known suffix, see if there's a transformation
   1383 		 * defined between the node's suffix and the target's suffix.
   1384 		 *
   1385 		 * XXX: Handle multi-stage transformations here, too.
   1386 		 */
   1387 		suff = (Suff *)Lst_Datum (ln);
   1388 
   1389 		if (Lst_Member (suff->parents,
   1390 				(ClientData)targ->suff) != NILLNODE)
   1391 		{
   1392 		    /*
   1393 		     * Hot Damn! Create a new Src structure to describe
   1394 		     * this transformation (making sure to duplicate the
   1395 		     * source node's name so Suff_FindDeps can free it
   1396 		     * again (ick)), and return the new structure.
   1397 		     */
   1398 		    ret = (Src *)emalloc (sizeof (Src));
   1399 		    ret->file = estrdup(s->name);
   1400 		    ret->pref = targ->pref;
   1401 		    ret->suff = suff;
   1402 		    suff->refCount++;
   1403 		    ret->parent = targ;
   1404 		    ret->node = s;
   1405 		    ret->children = 0;
   1406 		    targ->children += 1;
   1407 #ifdef DEBUG_SRC
   1408 		    ret->cp = Lst_Init(FALSE);
   1409 		    printf("3 add %x %x\n", targ, ret);
   1410 		    Lst_AtEnd(targ->cp, (ClientData) ret);
   1411 #endif
   1412 		    Lst_AtEnd(slst, (ClientData) ret);
   1413 		    if (DEBUG(SUFF)) {
   1414 			printf ("\tusing existing source %s\n", s->name);
   1415 		    }
   1416 		    return (ret);
   1417 		}
   1418 	    }
   1419 	}
   1420     }
   1421     Lst_Close (t->children);
   1422     return ((Src *)NULL);
   1423 }
   1424 
   1425 /*-
   1426  *-----------------------------------------------------------------------
   1427  * SuffExpandChildren --
   1428  *	Expand the names of any children of a given node that contain
   1429  *	variable invocations or file wildcards into actual targets.
   1430  *
   1431  * Results:
   1432  *	=== 0 (continue)
   1433  *
   1434  * Side Effects:
   1435  *	The expanded node is removed from the parent's list of children,
   1436  *	and the parent's unmade counter is decremented, but other nodes
   1437  * 	may be added.
   1438  *
   1439  *-----------------------------------------------------------------------
   1440  */
   1441 static int
   1442 SuffExpandChildren(prevLN, pgn)
   1443     LstNode     prevLN;	    /* Child to examine */
   1444     GNode   	*pgn; 	    /* Parent node being processed */
   1445 {
   1446     GNode   	*cgn = (GNode *) Lst_Datum(prevLN);
   1447     GNode	*gn;	    /* New source 8) */
   1448     LstNode	ln;	    /* List element for old source */
   1449     char	*cp;	    /* Expanded value */
   1450 
   1451     /*
   1452      * First do variable expansion -- this takes precedence over
   1453      * wildcard expansion. If the result contains wildcards, they'll be gotten
   1454      * to later since the resulting words are tacked on to the end of
   1455      * the children list.
   1456      */
   1457     if (strchr(cgn->name, '$') != (char *)NULL) {
   1458 	if (DEBUG(SUFF)) {
   1459 	    printf("Expanding \"%s\"...", cgn->name);
   1460 	}
   1461 	cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
   1462 
   1463 	if (cp != (char *)NULL) {
   1464 	    Lst	    members = Lst_Init(FALSE);
   1465 
   1466 	    if (cgn->type & OP_ARCHV) {
   1467 		/*
   1468 		 * Node was an archive(member) target, so we want to call
   1469 		 * on the Arch module to find the nodes for us, expanding
   1470 		 * variables in the parent's context.
   1471 		 */
   1472 		char	*sacrifice = cp;
   1473 
   1474 		(void)Arch_ParseArchive(&sacrifice, members, pgn);
   1475 	    } else {
   1476 		/*
   1477 		 * Break the result into a vector of strings whose nodes
   1478 		 * we can find, then add those nodes to the members list.
   1479 		 * Unfortunately, we can't use brk_string b/c it
   1480 		 * doesn't understand about variable specifications with
   1481 		 * spaces in them...
   1482 		 */
   1483 		char	    *start;
   1484 		char	    *initcp = cp;   /* For freeing... */
   1485 
   1486 		for (start = cp; *start == ' ' || *start == '\t'; start++)
   1487 		    continue;
   1488 		for (cp = start; *cp != '\0'; cp++) {
   1489 		    if (*cp == ' ' || *cp == '\t') {
   1490 			/*
   1491 			 * White-space -- terminate element, find the node,
   1492 			 * add it, skip any further spaces.
   1493 			 */
   1494 			*cp++ = '\0';
   1495 			gn = Targ_FindNode(start, TARG_CREATE);
   1496 			(void)Lst_AtEnd(members, (ClientData)gn);
   1497 			while (*cp == ' ' || *cp == '\t') {
   1498 			    cp++;
   1499 			}
   1500 			/*
   1501 			 * Adjust cp for increment at start of loop, but
   1502 			 * set start to first non-space.
   1503 			 */
   1504 			start = cp--;
   1505 		    } else if (*cp == '$') {
   1506 			/*
   1507 			 * Start of a variable spec -- contact variable module
   1508 			 * to find the end so we can skip over it.
   1509 			 */
   1510 			char	*junk;
   1511 			int 	len;
   1512 			Boolean	doFree;
   1513 
   1514 			junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
   1515 			if (junk != var_Error) {
   1516 			    cp += len - 1;
   1517 			}
   1518 
   1519 			if (doFree) {
   1520 			    free(junk);
   1521 			}
   1522 		    } else if (*cp == '\\' && *cp != '\0') {
   1523 			/*
   1524 			 * Escaped something -- skip over it
   1525 			 */
   1526 			cp++;
   1527 		    }
   1528 		}
   1529 
   1530 		if (cp != start) {
   1531 		    /*
   1532 		     * Stuff left over -- add it to the list too
   1533 		     */
   1534 		    gn = Targ_FindNode(start, TARG_CREATE);
   1535 		    (void)Lst_AtEnd(members, (ClientData)gn);
   1536 		}
   1537 		/*
   1538 		 * Point cp back at the beginning again so the variable value
   1539 		 * can be freed.
   1540 		 */
   1541 		cp = initcp;
   1542 	    }
   1543 	    /*
   1544 	     * Add all elements of the members list to the parent node.
   1545 	     */
   1546 	    while(!Lst_IsEmpty(members)) {
   1547 		gn = (GNode *)Lst_DeQueue(members);
   1548 
   1549 		if (DEBUG(SUFF)) {
   1550 		    printf("%s...", gn->name);
   1551 		}
   1552 		if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
   1553 		    (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
   1554 		    prevLN = Lst_Succ(prevLN);
   1555 		    (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
   1556 		    pgn->unmade++;
   1557 		}
   1558 	    }
   1559 	    Lst_Destroy(members, NOFREE);
   1560 	    /*
   1561 	     * Free the result
   1562 	     */
   1563 	    free((char *)cp);
   1564 	}
   1565 	/*
   1566 	 * Now the source is expanded, remove it from the list of children to
   1567 	 * keep it from being processed.
   1568 	 */
   1569 	if (DEBUG(SUFF)) {
   1570 	    printf("\n");
   1571 	}
   1572 	return(1);
   1573     } else if (Dir_HasWildcards(cgn->name)) {
   1574 	Lst 	exp;	    /* List of expansions */
   1575 	Lst 	path;	    /* Search path along which to expand */
   1576 	SuffixCmpData sd;   /* Search string data */
   1577 
   1578 	/*
   1579 	 * Find a path along which to expand the word.
   1580 	 *
   1581 	 * If the word has a known suffix, use that path.
   1582 	 * If it has no known suffix and we're allowed to use the null
   1583 	 *   suffix, use its path.
   1584 	 * Else use the default system search path.
   1585 	 */
   1586 	sd.len = strlen(cgn->name);
   1587 	sd.ename = cgn->name + sd.len;
   1588 	ln = Lst_Find(sufflist, (ClientData)&sd, SuffSuffIsSuffixP);
   1589 
   1590 	if (DEBUG(SUFF)) {
   1591 	    printf("Wildcard expanding \"%s\"...", cgn->name);
   1592 	}
   1593 
   1594 	if (ln != NILLNODE) {
   1595 	    Suff    *s = (Suff *)Lst_Datum(ln);
   1596 
   1597 	    if (DEBUG(SUFF)) {
   1598 		printf("suffix is \"%s\"...", s->name);
   1599 	    }
   1600 	    path = s->searchPath;
   1601 	} else {
   1602 	    /*
   1603 	     * Use default search path
   1604 	     */
   1605 	    path = dirSearchPath;
   1606 	}
   1607 
   1608 	/*
   1609 	 * Expand the word along the chosen path
   1610 	 */
   1611 	exp = Lst_Init(FALSE);
   1612 	Dir_Expand(cgn->name, path, exp);
   1613 
   1614 	while (!Lst_IsEmpty(exp)) {
   1615 	    /*
   1616 	     * Fetch next expansion off the list and find its GNode
   1617 	     */
   1618 	    cp = (char *)Lst_DeQueue(exp);
   1619 
   1620 	    if (DEBUG(SUFF)) {
   1621 		printf("%s...", cp);
   1622 	    }
   1623 	    gn = Targ_FindNode(cp, TARG_CREATE);
   1624 
   1625 	    /*
   1626 	     * If gn isn't already a child of the parent, make it so and
   1627 	     * up the parent's count of unmade children.
   1628 	     */
   1629 	    if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
   1630 		(void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
   1631 		prevLN = Lst_Succ(prevLN);
   1632 		(void)Lst_AtEnd(gn->parents, (ClientData)pgn);
   1633 		pgn->unmade++;
   1634 	    }
   1635 	}
   1636 
   1637 	/*
   1638 	 * Nuke what's left of the list
   1639 	 */
   1640 	Lst_Destroy(exp, NOFREE);
   1641 
   1642 	/*
   1643 	 * Now the source is expanded, remove it from the list of children to
   1644 	 * keep it from being processed.
   1645 	 */
   1646 	if (DEBUG(SUFF)) {
   1647 	    printf("\n");
   1648 	}
   1649 	return(1);
   1650     }
   1651 
   1652     return(0);
   1653 }
   1654 
   1655 /*-
   1656  *-----------------------------------------------------------------------
   1657  * SuffApplyTransform --
   1658  *	Apply a transformation rule, given the source and target nodes
   1659  *	and suffixes.
   1660  *
   1661  * Results:
   1662  *	TRUE if successful, FALSE if not.
   1663  *
   1664  * Side Effects:
   1665  *	The source and target are linked and the commands from the
   1666  *	transformation are added to the target node's commands list.
   1667  *	All attributes but OP_DEPMASK and OP_TRANSFORM are applied
   1668  *	to the target. The target also inherits all the sources for
   1669  *	the transformation rule.
   1670  *
   1671  *-----------------------------------------------------------------------
   1672  */
   1673 static Boolean
   1674 SuffApplyTransform(tGn, sGn, t, s)
   1675     GNode   	*tGn;	    /* Target node */
   1676     GNode   	*sGn;	    /* Source node */
   1677     Suff    	*t; 	    /* Target suffix */
   1678     Suff    	*s; 	    /* Source suffix */
   1679 {
   1680     LstNode 	ln, nln;    /* General node */
   1681     char    	*tname;	    /* Name of transformation rule */
   1682     GNode   	*gn;	    /* Node for same */
   1683 
   1684     if (Lst_Member(tGn->children, (ClientData)sGn) == NILLNODE) {
   1685 	/*
   1686 	 * Not already linked, so form the proper links between the
   1687 	 * target and source.
   1688 	 */
   1689 	(void)Lst_AtEnd(tGn->children, (ClientData)sGn);
   1690 	(void)Lst_AtEnd(sGn->parents, (ClientData)tGn);
   1691 	tGn->unmade += 1;
   1692     }
   1693 
   1694     /*
   1695      * Locate the transformation rule itself
   1696      */
   1697     tname = str_concat(s->name, t->name, 0);
   1698     ln = Lst_Find(transforms, (ClientData)tname, SuffGNHasNameP);
   1699     free(tname);
   1700 
   1701     if (ln == NILLNODE) {
   1702 	/*
   1703 	 * Not really such a transformation rule (can happen when we're
   1704 	 * called to link an OP_MEMBER and OP_ARCHV node), so return
   1705 	 * FALSE.
   1706 	 */
   1707 	return(FALSE);
   1708     }
   1709 
   1710     gn = (GNode *)Lst_Datum(ln);
   1711 
   1712     if (DEBUG(SUFF)) {
   1713 	printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
   1714     }
   1715 
   1716     /*
   1717      * Record last child for expansion purposes
   1718      */
   1719     ln = Lst_Last(tGn->children);
   1720 
   1721     /*
   1722      * Pass the buck to Make_HandleUse to apply the rule
   1723      */
   1724     (void)Make_HandleUse(gn, tGn);
   1725 
   1726     /*
   1727      * Deal with wildcards and variables in any acquired sources
   1728      */
   1729     ln = Lst_Succ(ln);
   1730     while (ln != NILLNODE) {
   1731 	if (SuffExpandChildren(ln, tGn)) {
   1732 	    nln = Lst_Succ(ln);
   1733 	    tGn->unmade--;
   1734 	    Lst_Remove(tGn->children, ln);
   1735 	    ln = nln;
   1736 	} else
   1737 	    ln = Lst_Succ(ln);
   1738     }
   1739 
   1740     /*
   1741      * Keep track of another parent to which this beast is transformed so
   1742      * the .IMPSRC variable can be set correctly for the parent.
   1743      */
   1744     (void)Lst_AtEnd(sGn->iParents, (ClientData)tGn);
   1745 
   1746     return(TRUE);
   1747 }
   1748 
   1749 
   1750 /*-
   1751  *-----------------------------------------------------------------------
   1752  * SuffFindArchiveDeps --
   1753  *	Locate dependencies for an OP_ARCHV node.
   1754  *
   1755  * Results:
   1756  *	None
   1757  *
   1758  * Side Effects:
   1759  *	Same as Suff_FindDeps
   1760  *
   1761  *-----------------------------------------------------------------------
   1762  */
   1763 static void
   1764 SuffFindArchiveDeps(gn, slst)
   1765     GNode   	*gn;	    /* Node for which to locate dependencies */
   1766     Lst		slst;
   1767 {
   1768     char    	*eoarch;    /* End of archive portion */
   1769     char    	*eoname;    /* End of member portion */
   1770     GNode   	*mem;	    /* Node for member */
   1771     static char	*copy[] = { /* Variables to be copied from the member node */
   1772 	TARGET,	    	    /* Must be first */
   1773 	PREFIX,	    	    /* Must be second */
   1774     };
   1775     int	    	i;  	    /* Index into copy and vals */
   1776     Suff    	*ms;	    /* Suffix descriptor for member */
   1777     char    	*name;	    /* Start of member's name */
   1778 
   1779     /*
   1780      * The node is an archive(member) pair. so we must find a
   1781      * suffix for both of them.
   1782      */
   1783     eoarch = strchr (gn->name, '(');
   1784     eoname = strchr (eoarch, ')');
   1785 
   1786     *eoname = '\0';	  /* Nuke parentheses during suffix search */
   1787     *eoarch = '\0';	  /* So a suffix can be found */
   1788 
   1789     name = eoarch + 1;
   1790 
   1791     /*
   1792      * To simplify things, call Suff_FindDeps recursively on the member now,
   1793      * so we can simply compare the member's .PREFIX and .TARGET variables
   1794      * to locate its suffix. This allows us to figure out the suffix to
   1795      * use for the archive without having to do a quadratic search over the
   1796      * suffix list, backtracking for each one...
   1797      */
   1798     mem = Targ_FindNode(name, TARG_CREATE);
   1799     SuffFindDeps(mem, slst);
   1800 
   1801     /*
   1802      * Create the link between the two nodes right off
   1803      */
   1804     if (Lst_Member(gn->children, (ClientData)mem) == NILLNODE) {
   1805 	(void)Lst_AtEnd(gn->children, (ClientData)mem);
   1806 	(void)Lst_AtEnd(mem->parents, (ClientData)gn);
   1807 	gn->unmade += 1;
   1808     }
   1809 
   1810     /*
   1811      * Copy in the variables from the member node to this one.
   1812      */
   1813     for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
   1814 	char *p1;
   1815 	Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn);
   1816 	if (p1)
   1817 	    free(p1);
   1818 
   1819     }
   1820 
   1821     ms = mem->suffix;
   1822     if (ms == NULL) {
   1823 	/*
   1824 	 * Didn't know what it was -- use .NULL suffix if not in make mode
   1825 	 */
   1826 	if (DEBUG(SUFF)) {
   1827 	    printf("using null suffix\n");
   1828 	}
   1829 	ms = suffNull;
   1830     }
   1831 
   1832 
   1833     /*
   1834      * Set the other two local variables required for this target.
   1835      */
   1836     Var_Set (MEMBER, name, gn);
   1837     Var_Set (ARCHIVE, gn->name, gn);
   1838 
   1839     if (ms != NULL) {
   1840 	/*
   1841 	 * Member has a known suffix, so look for a transformation rule from
   1842 	 * it to a possible suffix of the archive. Rather than searching
   1843 	 * through the entire list, we just look at suffixes to which the
   1844 	 * member's suffix may be transformed...
   1845 	 */
   1846 	LstNode		ln;
   1847 	SuffixCmpData	sd;		/* Search string data */
   1848 
   1849 	/*
   1850 	 * Use first matching suffix...
   1851 	 */
   1852 	sd.len = eoarch - gn->name;
   1853 	sd.ename = eoarch;
   1854 	ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP);
   1855 
   1856 	if (ln != NILLNODE) {
   1857 	    /*
   1858 	     * Got one -- apply it
   1859 	     */
   1860 	    if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
   1861 		DEBUG(SUFF))
   1862 	    {
   1863 		printf("\tNo transformation from %s -> %s\n",
   1864 		       ms->name, ((Suff *)Lst_Datum(ln))->name);
   1865 	    }
   1866 	}
   1867     }
   1868 
   1869     /*
   1870      * Replace the opening and closing parens now we've no need of the separate
   1871      * pieces.
   1872      */
   1873     *eoarch = '('; *eoname = ')';
   1874 
   1875     /*
   1876      * Pretend gn appeared to the left of a dependency operator so
   1877      * the user needn't provide a transformation from the member to the
   1878      * archive.
   1879      */
   1880     if (OP_NOP(gn->type)) {
   1881 	gn->type |= OP_DEPENDS;
   1882     }
   1883 
   1884     /*
   1885      * Flag the member as such so we remember to look in the archive for
   1886      * its modification time.
   1887      */
   1888     mem->type |= OP_MEMBER;
   1889 }
   1890 
   1891 /*-
   1892  *-----------------------------------------------------------------------
   1893  * SuffFindNormalDeps --
   1894  *	Locate implicit dependencies for regular targets.
   1895  *
   1896  * Results:
   1897  *	None.
   1898  *
   1899  * Side Effects:
   1900  *	Same as Suff_FindDeps...
   1901  *
   1902  *-----------------------------------------------------------------------
   1903  */
   1904 static void
   1905 SuffFindNormalDeps(gn, slst)
   1906     GNode   	*gn;	    /* Node for which to find sources */
   1907     Lst		slst;
   1908 {
   1909     char    	*eoname;    /* End of name */
   1910     char    	*sopref;    /* Start of prefix */
   1911     LstNode 	ln, nln;    /* Next suffix node to check */
   1912     Lst	    	srcs;	    /* List of sources at which to look */
   1913     Lst	    	targs;	    /* List of targets to which things can be
   1914 			     * transformed. They all have the same file,
   1915 			     * but different suff and pref fields */
   1916     Src	    	*bottom;    /* Start of found transformation path */
   1917     Src 	*src;	    /* General Src pointer */
   1918     char    	*pref;	    /* Prefix to use */
   1919     Src	    	*targ;	    /* General Src target pointer */
   1920     SuffixCmpData sd;	    /* Search string data */
   1921 
   1922 
   1923     sd.len = strlen(gn->name);
   1924     sd.ename = eoname = gn->name + sd.len;
   1925 
   1926     sopref = gn->name;
   1927 
   1928     /*
   1929      * Begin at the beginning...
   1930      */
   1931     ln = Lst_First(sufflist);
   1932     srcs = Lst_Init(FALSE);
   1933     targs = Lst_Init(FALSE);
   1934 
   1935     /*
   1936      * We're caught in a catch-22 here. On the one hand, we want to use any
   1937      * transformation implied by the target's sources, but we can't examine
   1938      * the sources until we've expanded any variables/wildcards they may hold,
   1939      * and we can't do that until we've set up the target's local variables
   1940      * and we can't do that until we know what the proper suffix for the
   1941      * target is (in case there are two suffixes one of which is a suffix of
   1942      * the other) and we can't know that until we've found its implied
   1943      * source, which we may not want to use if there's an existing source
   1944      * that implies a different transformation.
   1945      *
   1946      * In an attempt to get around this, which may not work all the time,
   1947      * but should work most of the time, we look for implied sources first,
   1948      * checking transformations to all possible suffixes of the target,
   1949      * use what we find to set the target's local variables, expand the
   1950      * children, then look for any overriding transformations they imply.
   1951      * Should we find one, we discard the one we found before.
   1952      */
   1953 
   1954     while (ln != NILLNODE) {
   1955 	/*
   1956 	 * Look for next possible suffix...
   1957 	 */
   1958 	ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP);
   1959 
   1960 	if (ln != NILLNODE) {
   1961 	    int	    prefLen;	    /* Length of the prefix */
   1962 	    Src	    *targ;
   1963 
   1964 	    /*
   1965 	     * Allocate a Src structure to which things can be transformed
   1966 	     */
   1967 	    targ = (Src *)emalloc(sizeof (Src));
   1968 	    targ->file = estrdup(gn->name);
   1969 	    targ->suff = (Suff *)Lst_Datum(ln);
   1970 	    targ->suff->refCount++;
   1971 	    targ->node = gn;
   1972 	    targ->parent = (Src *)NULL;
   1973 	    targ->children = 0;
   1974 #ifdef DEBUG_SRC
   1975 	    targ->cp = Lst_Init(FALSE);
   1976 #endif
   1977 
   1978 	    /*
   1979 	     * Allocate room for the prefix, whose end is found by subtracting
   1980 	     * the length of the suffix from the end of the name.
   1981 	     */
   1982 	    prefLen = (eoname - targ->suff->nameLen) - sopref;
   1983 	    targ->pref = emalloc(prefLen + 1);
   1984 	    memcpy(targ->pref, sopref, prefLen);
   1985 	    targ->pref[prefLen] = '\0';
   1986 
   1987 	    /*
   1988 	     * Add nodes from which the target can be made
   1989 	     */
   1990 	    SuffAddLevel(srcs, targ);
   1991 
   1992 	    /*
   1993 	     * Record the target so we can nuke it
   1994 	     */
   1995 	    (void)Lst_AtEnd(targs, (ClientData)targ);
   1996 
   1997 	    /*
   1998 	     * Search from this suffix's successor...
   1999 	     */
   2000 	    ln = Lst_Succ(ln);
   2001 	}
   2002     }
   2003 
   2004     /*
   2005      * Handle target of unknown suffix...
   2006      */
   2007     if (Lst_IsEmpty(targs) && suffNull != NULL) {
   2008 	if (DEBUG(SUFF)) {
   2009 	    printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
   2010 	}
   2011 
   2012 	targ = (Src *)emalloc(sizeof (Src));
   2013 	targ->file = estrdup(gn->name);
   2014 	targ->suff = suffNull;
   2015 	targ->suff->refCount++;
   2016 	targ->node = gn;
   2017 	targ->parent = (Src *)NULL;
   2018 	targ->children = 0;
   2019 	targ->pref = estrdup(sopref);
   2020 #ifdef DEBUG_SRC
   2021 	targ->cp = Lst_Init(FALSE);
   2022 #endif
   2023 
   2024 	/*
   2025 	 * Only use the default suffix rules if we don't have commands
   2026 	 * defined for this gnode; traditional make programs used to
   2027 	 * not define suffix rules if the gnode had children but we
   2028 	 * don't do this anymore.
   2029 	 */
   2030 	if (Lst_IsEmpty(gn->commands))
   2031 	    SuffAddLevel(srcs, targ);
   2032 	else {
   2033 	    if (DEBUG(SUFF))
   2034 		printf("not ");
   2035 	}
   2036 
   2037 	if (DEBUG(SUFF))
   2038 	    printf("adding suffix rules\n");
   2039 
   2040 	(void)Lst_AtEnd(targs, (ClientData)targ);
   2041     }
   2042 
   2043     /*
   2044      * Using the list of possible sources built up from the target suffix(es),
   2045      * try and find an existing file/target that matches.
   2046      */
   2047     bottom = SuffFindThem(srcs, slst);
   2048 
   2049     if (bottom == (Src *)NULL) {
   2050 	/*
   2051 	 * No known transformations -- use the first suffix found for setting
   2052 	 * the local variables.
   2053 	 */
   2054 	if (!Lst_IsEmpty(targs)) {
   2055 	    targ = (Src *)Lst_Datum(Lst_First(targs));
   2056 	} else {
   2057 	    targ = (Src *)NULL;
   2058 	}
   2059     } else {
   2060 	/*
   2061 	 * Work up the transformation path to find the suffix of the
   2062 	 * target to which the transformation was made.
   2063 	 */
   2064 	for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   2065 	    continue;
   2066     }
   2067 
   2068     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
   2069 
   2070     pref = (targ != NULL) ? targ->pref : gn->name;
   2071     Var_Set(PREFIX, pref, gn);
   2072 
   2073     /*
   2074      * Now we've got the important local variables set, expand any sources
   2075      * that still contain variables or wildcards in their names.
   2076      */
   2077     ln = Lst_First(gn->children);
   2078     while (ln != NILLNODE) {
   2079 	if (SuffExpandChildren(ln, gn)) {
   2080 	    nln = Lst_Succ(ln);
   2081 	    gn->unmade--;
   2082 	    Lst_Remove(gn->children, ln);
   2083 	    ln = nln;
   2084 	} else
   2085 	    ln = Lst_Succ(ln);
   2086     }
   2087 
   2088     if (targ == NULL) {
   2089 	if (DEBUG(SUFF)) {
   2090 	    printf("\tNo valid suffix on %s\n", gn->name);
   2091 	}
   2092 
   2093 sfnd_abort:
   2094 	/*
   2095 	 * Deal with finding the thing on the default search path. We
   2096 	 * always do that, not only if the node is only a source (not
   2097 	 * on the lhs of a dependency operator or [XXX] it has neither
   2098 	 * children or commands) as the old pmake did.
   2099 	 */
   2100 	if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
   2101 	    free(gn->path);
   2102 	    gn->path = Dir_FindFile(gn->name,
   2103 				    (targ == NULL ? dirSearchPath :
   2104 				     targ->suff->searchPath));
   2105 	    if (gn->path != NULL) {
   2106 		char *ptr;
   2107 		Var_Set(TARGET, gn->path, gn);
   2108 
   2109 		if (targ != NULL) {
   2110 		    /*
   2111 		     * Suffix known for the thing -- trim the suffix off
   2112 		     * the path to form the proper .PREFIX variable.
   2113 		     */
   2114 		    int     savep = strlen(gn->path) - targ->suff->nameLen;
   2115 		    char    savec;
   2116 
   2117 		    if (gn->suffix)
   2118 			gn->suffix->refCount--;
   2119 		    gn->suffix = targ->suff;
   2120 		    gn->suffix->refCount++;
   2121 
   2122 		    savec = gn->path[savep];
   2123 		    gn->path[savep] = '\0';
   2124 
   2125 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   2126 			ptr++;
   2127 		    else
   2128 			ptr = gn->path;
   2129 
   2130 		    Var_Set(PREFIX, ptr, gn);
   2131 
   2132 		    gn->path[savep] = savec;
   2133 		} else {
   2134 		    /*
   2135 		     * The .PREFIX gets the full path if the target has
   2136 		     * no known suffix.
   2137 		     */
   2138 		    if (gn->suffix)
   2139 			gn->suffix->refCount--;
   2140 		    gn->suffix = NULL;
   2141 
   2142 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   2143 			ptr++;
   2144 		    else
   2145 			ptr = gn->path;
   2146 
   2147 		    Var_Set(PREFIX, ptr, gn);
   2148 		}
   2149 	    }
   2150 	}
   2151 
   2152 	goto sfnd_return;
   2153     }
   2154 
   2155     /*
   2156      * If the suffix indicates that the target is a library, mark that in
   2157      * the node's type field.
   2158      */
   2159     if (targ->suff->flags & SUFF_LIBRARY) {
   2160 	gn->type |= OP_LIB;
   2161     }
   2162 
   2163     /*
   2164      * Check for overriding transformation rule implied by sources
   2165      */
   2166     if (!Lst_IsEmpty(gn->children)) {
   2167 	src = SuffFindCmds(targ, slst);
   2168 
   2169 	if (src != (Src *)NULL) {
   2170 	    /*
   2171 	     * Free up all the Src structures in the transformation path
   2172 	     * up to, but not including, the parent node.
   2173 	     */
   2174 	    while (bottom && bottom->parent != NULL) {
   2175 		if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) {
   2176 		    Lst_AtEnd(slst, (ClientData) bottom);
   2177 		}
   2178 		bottom = bottom->parent;
   2179 	    }
   2180 	    bottom = src;
   2181 	}
   2182     }
   2183 
   2184     if (bottom == NULL) {
   2185 	/*
   2186 	 * No idea from where it can come -- return now.
   2187 	 */
   2188 	goto sfnd_abort;
   2189     }
   2190 
   2191     /*
   2192      * We now have a list of Src structures headed by 'bottom' and linked via
   2193      * their 'parent' pointers. What we do next is create links between
   2194      * source and target nodes (which may or may not have been created)
   2195      * and set the necessary local variables in each target. The
   2196      * commands for each target are set from the commands of the
   2197      * transformation rule used to get from the src suffix to the targ
   2198      * suffix. Note that this causes the commands list of the original
   2199      * node, gn, to be replaced by the commands of the final
   2200      * transformation rule. Also, the unmade field of gn is incremented.
   2201      * Etc.
   2202      */
   2203     if (bottom->node == NILGNODE) {
   2204 	bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
   2205     }
   2206 
   2207     for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
   2208 	targ = src->parent;
   2209 
   2210 	if (src->node->suffix)
   2211 	    src->node->suffix->refCount--;
   2212 	src->node->suffix = src->suff;
   2213 	src->node->suffix->refCount++;
   2214 
   2215 	if (targ->node == NILGNODE) {
   2216 	    targ->node = Targ_FindNode(targ->file, TARG_CREATE);
   2217 	}
   2218 
   2219 	SuffApplyTransform(targ->node, src->node,
   2220 			   targ->suff, src->suff);
   2221 
   2222 	if (targ->node != gn) {
   2223 	    /*
   2224 	     * Finish off the dependency-search process for any nodes
   2225 	     * between bottom and gn (no point in questing around the
   2226 	     * filesystem for their implicit source when it's already
   2227 	     * known). Note that the node can't have any sources that
   2228 	     * need expanding, since SuffFindThem will stop on an existing
   2229 	     * node, so all we need to do is set the standard and System V
   2230 	     * variables.
   2231 	     */
   2232 	    targ->node->type |= OP_DEPS_FOUND;
   2233 
   2234 	    Var_Set(PREFIX, targ->pref, targ->node);
   2235 
   2236 	    Var_Set(TARGET, targ->node->name, targ->node);
   2237 	}
   2238     }
   2239 
   2240     if (gn->suffix)
   2241 	gn->suffix->refCount--;
   2242     gn->suffix = src->suff;
   2243     gn->suffix->refCount++;
   2244 
   2245     /*
   2246      * Nuke the transformation path and the Src structures left over in the
   2247      * two lists.
   2248      */
   2249 sfnd_return:
   2250     if (bottom)
   2251 	if (Lst_Member(slst, (ClientData) bottom) == NILLNODE)
   2252 	    Lst_AtEnd(slst, (ClientData) bottom);
   2253 
   2254     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
   2255 	continue;
   2256 
   2257     Lst_Concat(slst, srcs, LST_CONCLINK);
   2258     Lst_Concat(slst, targs, LST_CONCLINK);
   2259 }
   2260 
   2261 
   2262 /*-
   2263  *-----------------------------------------------------------------------
   2264  * Suff_FindDeps  --
   2265  *	Find implicit sources for the target described by the graph node
   2266  *	gn
   2267  *
   2268  * Results:
   2269  *	Nothing.
   2270  *
   2271  * Side Effects:
   2272  *	Nodes are added to the graph below the passed-in node. The nodes
   2273  *	are marked to have their IMPSRC variable filled in. The
   2274  *	PREFIX variable is set for the given node and all its
   2275  *	implied children.
   2276  *
   2277  * Notes:
   2278  *	The path found by this target is the shortest path in the
   2279  *	transformation graph, which may pass through non-existent targets,
   2280  *	to an existing target. The search continues on all paths from the
   2281  *	root suffix until a file is found. I.e. if there's a path
   2282  *	.o -> .c -> .l -> .l,v from the root and the .l,v file exists but
   2283  *	the .c and .l files don't, the search will branch out in
   2284  *	all directions from .o and again from all the nodes on the
   2285  *	next level until the .l,v node is encountered.
   2286  *
   2287  *-----------------------------------------------------------------------
   2288  */
   2289 
   2290 void
   2291 Suff_FindDeps(gn)
   2292     GNode *gn;
   2293 {
   2294 
   2295     SuffFindDeps(gn, srclist);
   2296     while (SuffRemoveSrc(srclist))
   2297 	continue;
   2298 }
   2299 
   2300 
   2301 static void
   2302 SuffFindDeps (gn, slst)
   2303     GNode         *gn;	      	/* node we're dealing with */
   2304     Lst		  slst;
   2305 {
   2306     if (gn->type & (OP_DEPS_FOUND|OP_PHONY)) {
   2307 	/*
   2308 	 * If dependencies already found, no need to do it again...
   2309 	 * If this is a .PHONY target, we do not apply suffix rules.
   2310 	 */
   2311 	return;
   2312     } else {
   2313 	gn->type |= OP_DEPS_FOUND;
   2314     }
   2315 
   2316     if (DEBUG(SUFF)) {
   2317 	printf ("SuffFindDeps (%s)\n", gn->name);
   2318     }
   2319 
   2320     if (gn->type & OP_ARCHV) {
   2321 	SuffFindArchiveDeps(gn, slst);
   2322     } else if (gn->type & OP_LIB) {
   2323 	/*
   2324 	 * If the node is a library, it is the arch module's job to find it
   2325 	 * and set the TARGET variable accordingly. We merely provide the
   2326 	 * search path, assuming all libraries end in ".a" (if the suffix
   2327 	 * hasn't been defined, there's nothing we can do for it, so we just
   2328 	 * set the TARGET variable to the node's name in order to give it a
   2329 	 * value).
   2330 	 */
   2331 	LstNode	ln;
   2332 	Suff	*s;
   2333 
   2334 	ln = Lst_Find (sufflist, (ClientData)LIBSUFF, SuffSuffHasNameP);
   2335 	if (gn->suffix)
   2336 	    gn->suffix->refCount--;
   2337 	if (ln != NILLNODE) {
   2338 	    gn->suffix = s = (Suff *) Lst_Datum (ln);
   2339 	    gn->suffix->refCount++;
   2340 	    Arch_FindLib (gn, s->searchPath);
   2341 	} else {
   2342 	    gn->suffix = NULL;
   2343 	    Var_Set (TARGET, gn->name, gn);
   2344 	}
   2345 	/*
   2346 	 * Because a library (-lfoo) target doesn't follow the standard
   2347 	 * filesystem conventions, we don't set the regular variables for
   2348 	 * the thing. .PREFIX is simply made empty...
   2349 	 */
   2350 	Var_Set(PREFIX, "", gn);
   2351     } else {
   2352 	SuffFindNormalDeps(gn, slst);
   2353     }
   2354 }
   2355 
   2356 /*-
   2357  *-----------------------------------------------------------------------
   2358  * Suff_SetNull --
   2359  *	Define which suffix is the null suffix.
   2360  *
   2361  * Results:
   2362  *	None.
   2363  *
   2364  * Side Effects:
   2365  *	'suffNull' is altered.
   2366  *
   2367  * Notes:
   2368  *	Need to handle the changing of the null suffix gracefully so the
   2369  *	old transformation rules don't just go away.
   2370  *
   2371  *-----------------------------------------------------------------------
   2372  */
   2373 void
   2374 Suff_SetNull(name)
   2375     char    *name;	    /* Name of null suffix */
   2376 {
   2377     Suff    *s;
   2378     LstNode ln;
   2379 
   2380     ln = Lst_Find(sufflist, (ClientData)name, SuffSuffHasNameP);
   2381     if (ln != NILLNODE) {
   2382 	s = (Suff *)Lst_Datum(ln);
   2383 	if (suffNull != (Suff *)NULL) {
   2384 	    suffNull->flags &= ~SUFF_NULL;
   2385 	}
   2386 	s->flags |= SUFF_NULL;
   2387 	/*
   2388 	 * XXX: Here's where the transformation mangling would take place
   2389 	 */
   2390 	suffNull = s;
   2391     } else {
   2392 	Parse_Error (PARSE_WARNING, "Desired null suffix %s not defined.",
   2393 		     name);
   2394     }
   2395 }
   2396 
   2397 /*-
   2398  *-----------------------------------------------------------------------
   2399  * Suff_Init --
   2400  *	Initialize suffixes module
   2401  *
   2402  * Results:
   2403  *	None
   2404  *
   2405  * Side Effects:
   2406  *	Many
   2407  *-----------------------------------------------------------------------
   2408  */
   2409 void
   2410 Suff_Init ()
   2411 {
   2412     sufflist = Lst_Init (FALSE);
   2413 #ifdef CLEANUP
   2414     suffClean = Lst_Init(FALSE);
   2415 #endif
   2416     srclist = Lst_Init (FALSE);
   2417     transforms = Lst_Init (FALSE);
   2418 
   2419     sNum = 0;
   2420     /*
   2421      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   2422      * actually go on the suffix list or everyone will think that's its
   2423      * suffix.
   2424      */
   2425     emptySuff = suffNull = (Suff *) emalloc (sizeof (Suff));
   2426 
   2427     suffNull->name =   	    estrdup ("");
   2428     suffNull->nameLen =     0;
   2429     suffNull->searchPath =  Lst_Init (FALSE);
   2430     Dir_Concat(suffNull->searchPath, dirSearchPath);
   2431     suffNull->children =    Lst_Init (FALSE);
   2432     suffNull->parents =	    Lst_Init (FALSE);
   2433     suffNull->ref =	    Lst_Init (FALSE);
   2434     suffNull->sNum =   	    sNum++;
   2435     suffNull->flags =  	    SUFF_NULL;
   2436     suffNull->refCount =    1;
   2437 
   2438 }
   2439 
   2440 
   2441 /*-
   2442  *----------------------------------------------------------------------
   2443  * Suff_End --
   2444  *	Cleanup the this module
   2445  *
   2446  * Results:
   2447  *	None
   2448  *
   2449  * Side Effects:
   2450  *	The memory is free'd.
   2451  *----------------------------------------------------------------------
   2452  */
   2453 
   2454 void
   2455 Suff_End()
   2456 {
   2457 #ifdef CLEANUP
   2458     Lst_Destroy(sufflist, SuffFree);
   2459     Lst_Destroy(suffClean, SuffFree);
   2460     if (suffNull)
   2461 	SuffFree(suffNull);
   2462     Lst_Destroy(srclist, NOFREE);
   2463     Lst_Destroy(transforms, NOFREE);
   2464 #endif
   2465 }
   2466 
   2467 
   2468 /********************* DEBUGGING FUNCTIONS **********************/
   2469 
   2470 static int SuffPrintName(s, dummy)
   2471     ClientData s;
   2472     ClientData dummy;
   2473 {
   2474     printf ("%s ", ((Suff *) s)->name);
   2475     return (dummy ? 0 : 0);
   2476 }
   2477 
   2478 static int
   2479 SuffPrintSuff (sp, dummy)
   2480     ClientData sp;
   2481     ClientData dummy;
   2482 {
   2483     Suff    *s = (Suff *) sp;
   2484     int	    flags;
   2485     int	    flag;
   2486 
   2487     printf ("# `%s' [%d] ", s->name, s->refCount);
   2488 
   2489     flags = s->flags;
   2490     if (flags) {
   2491 	fputs (" (", stdout);
   2492 	while (flags) {
   2493 	    flag = 1 << (ffs(flags) - 1);
   2494 	    flags &= ~flag;
   2495 	    switch (flag) {
   2496 		case SUFF_NULL:
   2497 		    printf ("NULL");
   2498 		    break;
   2499 		case SUFF_INCLUDE:
   2500 		    printf ("INCLUDE");
   2501 		    break;
   2502 		case SUFF_LIBRARY:
   2503 		    printf ("LIBRARY");
   2504 		    break;
   2505 	    }
   2506 	    fputc(flags ? '|' : ')', stdout);
   2507 	}
   2508     }
   2509     fputc ('\n', stdout);
   2510     printf ("#\tTo: ");
   2511     Lst_ForEach (s->parents, SuffPrintName, (ClientData)0);
   2512     fputc ('\n', stdout);
   2513     printf ("#\tFrom: ");
   2514     Lst_ForEach (s->children, SuffPrintName, (ClientData)0);
   2515     fputc ('\n', stdout);
   2516     printf ("#\tSearch Path: ");
   2517     Dir_PrintPath (s->searchPath);
   2518     fputc ('\n', stdout);
   2519     return (dummy ? 0 : 0);
   2520 }
   2521 
   2522 static int
   2523 SuffPrintTrans (tp, dummy)
   2524     ClientData tp;
   2525     ClientData dummy;
   2526 {
   2527     GNode   *t = (GNode *) tp;
   2528 
   2529     printf ("%-16s: ", t->name);
   2530     Targ_PrintType (t->type);
   2531     fputc ('\n', stdout);
   2532     Lst_ForEach (t->commands, Targ_PrintCmd, (ClientData)0);
   2533     fputc ('\n', stdout);
   2534     return(dummy ? 0 : 0);
   2535 }
   2536 
   2537 void
   2538 Suff_PrintAll()
   2539 {
   2540     printf ("#*** Suffixes:\n");
   2541     Lst_ForEach (sufflist, SuffPrintSuff, (ClientData)0);
   2542 
   2543     printf ("#*** Transformations:\n");
   2544     Lst_ForEach (transforms, SuffPrintTrans, (ClientData)0);
   2545 }
   2546