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