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