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