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