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