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