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