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