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