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