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