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

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