[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.29

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