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