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