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