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