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