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