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