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