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