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

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