[BACK]Return to suff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/suff.c, Revision 1.47

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