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